re PR fortran/55907 (ICE with -fno-automatic -finit-local-zero)
[official-gcc.git] / gcc / fortran / resolve.c
blobb756fe28ec2b5c403703a72962bd4149dc5dc8a4
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2014 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->binding_label
2355 && gsym->ns
2356 && gsym->ns->resolved != -1
2357 && gsym->ns->proc_name
2358 && not_in_recursive (sym, gsym->ns)
2359 && not_entry_self_reference (sym, gsym->ns))
2361 gfc_symbol *def_sym;
2363 /* Resolve the gsymbol namespace if needed. */
2364 if (!gsym->ns->resolved)
2366 gfc_dt_list *old_dt_list;
2367 struct gfc_omp_saved_state old_omp_state;
2369 /* Stash away derived types so that the backend_decls do not
2370 get mixed up. */
2371 old_dt_list = gfc_derived_types;
2372 gfc_derived_types = NULL;
2373 /* And stash away openmp state. */
2374 gfc_omp_save_and_clear_state (&old_omp_state);
2376 gfc_resolve (gsym->ns);
2378 /* Store the new derived types with the global namespace. */
2379 if (gfc_derived_types)
2380 gsym->ns->derived_types = gfc_derived_types;
2382 /* Restore the derived types of this namespace. */
2383 gfc_derived_types = old_dt_list;
2384 /* And openmp state. */
2385 gfc_omp_restore_state (&old_omp_state);
2388 /* Make sure that translation for the gsymbol occurs before
2389 the procedure currently being resolved. */
2390 ns = gfc_global_ns_list;
2391 for (; ns && ns != gsym->ns; ns = ns->sibling)
2393 if (ns->sibling == gsym->ns)
2395 ns->sibling = gsym->ns->sibling;
2396 gsym->ns->sibling = gfc_global_ns_list;
2397 gfc_global_ns_list = gsym->ns;
2398 break;
2402 def_sym = gsym->ns->proc_name;
2404 /* This can happen if a binding name has been specified. */
2405 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2406 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2408 if (def_sym->attr.entry_master)
2410 gfc_entry_list *entry;
2411 for (entry = gsym->ns->entries; entry; entry = entry->next)
2412 if (strcmp (entry->sym->name, sym->name) == 0)
2414 def_sym = entry->sym;
2415 break;
2419 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2421 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2422 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2423 gfc_typename (&def_sym->ts));
2424 goto done;
2427 if (sym->attr.if_source == IFSRC_UNKNOWN
2428 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2430 gfc_error ("Explicit interface required for '%s' at %L: %s",
2431 sym->name, &sym->declared_at, reason);
2432 goto done;
2435 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2436 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2437 gfc_errors_to_warnings (1);
2439 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2440 reason, sizeof(reason), NULL, NULL))
2442 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2443 sym->name, &sym->declared_at, reason);
2444 goto done;
2447 if (!pedantic
2448 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2449 && !(gfc_option.warn_std & GFC_STD_GNU)))
2450 gfc_errors_to_warnings (1);
2452 if (sym->attr.if_source != IFSRC_IFBODY)
2453 gfc_procedure_use (def_sym, actual, where);
2456 done:
2457 gfc_errors_to_warnings (0);
2459 if (gsym->type == GSYM_UNKNOWN)
2461 gsym->type = type;
2462 gsym->where = *where;
2465 gsym->used = 1;
2469 /************* Function resolution *************/
2471 /* Resolve a function call known to be generic.
2472 Section 14.1.2.4.1. */
2474 static match
2475 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2477 gfc_symbol *s;
2479 if (sym->attr.generic)
2481 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2482 if (s != NULL)
2484 expr->value.function.name = s->name;
2485 expr->value.function.esym = s;
2487 if (s->ts.type != BT_UNKNOWN)
2488 expr->ts = s->ts;
2489 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2490 expr->ts = s->result->ts;
2492 if (s->as != NULL)
2493 expr->rank = s->as->rank;
2494 else if (s->result != NULL && s->result->as != NULL)
2495 expr->rank = s->result->as->rank;
2497 gfc_set_sym_referenced (expr->value.function.esym);
2499 return MATCH_YES;
2502 /* TODO: Need to search for elemental references in generic
2503 interface. */
2506 if (sym->attr.intrinsic)
2507 return gfc_intrinsic_func_interface (expr, 0);
2509 return MATCH_NO;
2513 static bool
2514 resolve_generic_f (gfc_expr *expr)
2516 gfc_symbol *sym;
2517 match m;
2518 gfc_interface *intr = NULL;
2520 sym = expr->symtree->n.sym;
2522 for (;;)
2524 m = resolve_generic_f0 (expr, sym);
2525 if (m == MATCH_YES)
2526 return true;
2527 else if (m == MATCH_ERROR)
2528 return false;
2530 generic:
2531 if (!intr)
2532 for (intr = sym->generic; intr; intr = intr->next)
2533 if (intr->sym->attr.flavor == FL_DERIVED)
2534 break;
2536 if (sym->ns->parent == NULL)
2537 break;
2538 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2540 if (sym == NULL)
2541 break;
2542 if (!generic_sym (sym))
2543 goto generic;
2546 /* Last ditch attempt. See if the reference is to an intrinsic
2547 that possesses a matching interface. 14.1.2.4 */
2548 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2550 gfc_error ("There is no specific function for the generic '%s' "
2551 "at %L", expr->symtree->n.sym->name, &expr->where);
2552 return false;
2555 if (intr)
2557 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2558 NULL, false))
2559 return false;
2560 return resolve_structure_cons (expr, 0);
2563 m = gfc_intrinsic_func_interface (expr, 0);
2564 if (m == MATCH_YES)
2565 return true;
2567 if (m == MATCH_NO)
2568 gfc_error ("Generic function '%s' at %L is not consistent with a "
2569 "specific intrinsic interface", expr->symtree->n.sym->name,
2570 &expr->where);
2572 return false;
2576 /* Resolve a function call known to be specific. */
2578 static match
2579 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2581 match m;
2583 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2585 if (sym->attr.dummy)
2587 sym->attr.proc = PROC_DUMMY;
2588 goto found;
2591 sym->attr.proc = PROC_EXTERNAL;
2592 goto found;
2595 if (sym->attr.proc == PROC_MODULE
2596 || sym->attr.proc == PROC_ST_FUNCTION
2597 || sym->attr.proc == PROC_INTERNAL)
2598 goto found;
2600 if (sym->attr.intrinsic)
2602 m = gfc_intrinsic_func_interface (expr, 1);
2603 if (m == MATCH_YES)
2604 return MATCH_YES;
2605 if (m == MATCH_NO)
2606 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2607 "with an intrinsic", sym->name, &expr->where);
2609 return MATCH_ERROR;
2612 return MATCH_NO;
2614 found:
2615 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2617 if (sym->result)
2618 expr->ts = sym->result->ts;
2619 else
2620 expr->ts = sym->ts;
2621 expr->value.function.name = sym->name;
2622 expr->value.function.esym = sym;
2623 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2624 expr->rank = CLASS_DATA (sym)->as->rank;
2625 else if (sym->as != NULL)
2626 expr->rank = sym->as->rank;
2628 return MATCH_YES;
2632 static bool
2633 resolve_specific_f (gfc_expr *expr)
2635 gfc_symbol *sym;
2636 match m;
2638 sym = expr->symtree->n.sym;
2640 for (;;)
2642 m = resolve_specific_f0 (sym, expr);
2643 if (m == MATCH_YES)
2644 return true;
2645 if (m == MATCH_ERROR)
2646 return false;
2648 if (sym->ns->parent == NULL)
2649 break;
2651 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2653 if (sym == NULL)
2654 break;
2657 gfc_error ("Unable to resolve the specific function '%s' at %L",
2658 expr->symtree->n.sym->name, &expr->where);
2660 return true;
2664 /* Resolve a procedure call not known to be generic nor specific. */
2666 static bool
2667 resolve_unknown_f (gfc_expr *expr)
2669 gfc_symbol *sym;
2670 gfc_typespec *ts;
2672 sym = expr->symtree->n.sym;
2674 if (sym->attr.dummy)
2676 sym->attr.proc = PROC_DUMMY;
2677 expr->value.function.name = sym->name;
2678 goto set_type;
2681 /* See if we have an intrinsic function reference. */
2683 if (gfc_is_intrinsic (sym, 0, expr->where))
2685 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2686 return true;
2687 return false;
2690 /* The reference is to an external name. */
2692 sym->attr.proc = PROC_EXTERNAL;
2693 expr->value.function.name = sym->name;
2694 expr->value.function.esym = expr->symtree->n.sym;
2696 if (sym->as != NULL)
2697 expr->rank = sym->as->rank;
2699 /* Type of the expression is either the type of the symbol or the
2700 default type of the symbol. */
2702 set_type:
2703 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2705 if (sym->ts.type != BT_UNKNOWN)
2706 expr->ts = sym->ts;
2707 else
2709 ts = gfc_get_default_type (sym->name, sym->ns);
2711 if (ts->type == BT_UNKNOWN)
2713 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2714 sym->name, &expr->where);
2715 return false;
2717 else
2718 expr->ts = *ts;
2721 return true;
2725 /* Return true, if the symbol is an external procedure. */
2726 static bool
2727 is_external_proc (gfc_symbol *sym)
2729 if (!sym->attr.dummy && !sym->attr.contained
2730 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2731 && sym->attr.proc != PROC_ST_FUNCTION
2732 && !sym->attr.proc_pointer
2733 && !sym->attr.use_assoc
2734 && sym->name)
2735 return true;
2737 return false;
2741 /* Figure out if a function reference is pure or not. Also set the name
2742 of the function for a potential error message. Return nonzero if the
2743 function is PURE, zero if not. */
2744 static int
2745 pure_stmt_function (gfc_expr *, gfc_symbol *);
2747 static int
2748 pure_function (gfc_expr *e, const char **name)
2750 int pure;
2752 *name = NULL;
2754 if (e->symtree != NULL
2755 && e->symtree->n.sym != NULL
2756 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2757 return pure_stmt_function (e, e->symtree->n.sym);
2759 if (e->value.function.esym)
2761 pure = gfc_pure (e->value.function.esym);
2762 *name = e->value.function.esym->name;
2764 else if (e->value.function.isym)
2766 pure = e->value.function.isym->pure
2767 || e->value.function.isym->elemental;
2768 *name = e->value.function.isym->name;
2770 else
2772 /* Implicit functions are not pure. */
2773 pure = 0;
2774 *name = e->value.function.name;
2777 return pure;
2781 static bool
2782 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2783 int *f ATTRIBUTE_UNUSED)
2785 const char *name;
2787 /* Don't bother recursing into other statement functions
2788 since they will be checked individually for purity. */
2789 if (e->expr_type != EXPR_FUNCTION
2790 || !e->symtree
2791 || e->symtree->n.sym == sym
2792 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2793 return false;
2795 return pure_function (e, &name) ? false : true;
2799 static int
2800 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2802 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2806 /* Resolve a function call, which means resolving the arguments, then figuring
2807 out which entity the name refers to. */
2809 static bool
2810 resolve_function (gfc_expr *expr)
2812 gfc_actual_arglist *arg;
2813 gfc_symbol *sym;
2814 const char *name;
2815 bool t;
2816 int temp;
2817 procedure_type p = PROC_INTRINSIC;
2818 bool no_formal_args;
2820 sym = NULL;
2821 if (expr->symtree)
2822 sym = expr->symtree->n.sym;
2824 /* If this is a procedure pointer component, it has already been resolved. */
2825 if (gfc_is_proc_ptr_comp (expr))
2826 return true;
2828 if (sym && sym->attr.intrinsic
2829 && !gfc_resolve_intrinsic (sym, &expr->where))
2830 return false;
2832 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2834 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2835 return false;
2838 /* If this ia a deferred TBP with an abstract interface (which may
2839 of course be referenced), expr->value.function.esym will be set. */
2840 if (sym && sym->attr.abstract && !expr->value.function.esym)
2842 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2843 sym->name, &expr->where);
2844 return false;
2847 /* Switch off assumed size checking and do this again for certain kinds
2848 of procedure, once the procedure itself is resolved. */
2849 need_full_assumed_size++;
2851 if (expr->symtree && expr->symtree->n.sym)
2852 p = expr->symtree->n.sym->attr.proc;
2854 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2855 inquiry_argument = true;
2856 no_formal_args = sym && is_external_proc (sym)
2857 && gfc_sym_get_dummy_args (sym) == NULL;
2859 if (!resolve_actual_arglist (expr->value.function.actual,
2860 p, no_formal_args))
2862 inquiry_argument = false;
2863 return false;
2866 inquiry_argument = false;
2868 /* Resume assumed_size checking. */
2869 need_full_assumed_size--;
2871 /* If the procedure is external, check for usage. */
2872 if (sym && is_external_proc (sym))
2873 resolve_global_procedure (sym, &expr->where,
2874 &expr->value.function.actual, 0);
2876 if (sym && sym->ts.type == BT_CHARACTER
2877 && sym->ts.u.cl
2878 && sym->ts.u.cl->length == NULL
2879 && !sym->attr.dummy
2880 && !sym->ts.deferred
2881 && expr->value.function.esym == NULL
2882 && !sym->attr.contained)
2884 /* Internal procedures are taken care of in resolve_contained_fntype. */
2885 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2886 "be used at %L since it is not a dummy argument",
2887 sym->name, &expr->where);
2888 return false;
2891 /* See if function is already resolved. */
2893 if (expr->value.function.name != NULL)
2895 if (expr->ts.type == BT_UNKNOWN)
2896 expr->ts = sym->ts;
2897 t = true;
2899 else
2901 /* Apply the rules of section 14.1.2. */
2903 switch (procedure_kind (sym))
2905 case PTYPE_GENERIC:
2906 t = resolve_generic_f (expr);
2907 break;
2909 case PTYPE_SPECIFIC:
2910 t = resolve_specific_f (expr);
2911 break;
2913 case PTYPE_UNKNOWN:
2914 t = resolve_unknown_f (expr);
2915 break;
2917 default:
2918 gfc_internal_error ("resolve_function(): bad function type");
2922 /* If the expression is still a function (it might have simplified),
2923 then we check to see if we are calling an elemental function. */
2925 if (expr->expr_type != EXPR_FUNCTION)
2926 return t;
2928 temp = need_full_assumed_size;
2929 need_full_assumed_size = 0;
2931 if (!resolve_elemental_actual (expr, NULL))
2932 return false;
2934 if (omp_workshare_flag
2935 && expr->value.function.esym
2936 && ! gfc_elemental (expr->value.function.esym))
2938 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2939 "in WORKSHARE construct", expr->value.function.esym->name,
2940 &expr->where);
2941 t = false;
2944 #define GENERIC_ID expr->value.function.isym->id
2945 else if (expr->value.function.actual != NULL
2946 && expr->value.function.isym != NULL
2947 && GENERIC_ID != GFC_ISYM_LBOUND
2948 && GENERIC_ID != GFC_ISYM_LEN
2949 && GENERIC_ID != GFC_ISYM_LOC
2950 && GENERIC_ID != GFC_ISYM_C_LOC
2951 && GENERIC_ID != GFC_ISYM_PRESENT)
2953 /* Array intrinsics must also have the last upper bound of an
2954 assumed size array argument. UBOUND and SIZE have to be
2955 excluded from the check if the second argument is anything
2956 than a constant. */
2958 for (arg = expr->value.function.actual; arg; arg = arg->next)
2960 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2961 && arg == expr->value.function.actual
2962 && arg->next != NULL && arg->next->expr)
2964 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2965 break;
2967 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2968 break;
2970 if ((int)mpz_get_si (arg->next->expr->value.integer)
2971 < arg->expr->rank)
2972 break;
2975 if (arg->expr != NULL
2976 && arg->expr->rank > 0
2977 && resolve_assumed_size_actual (arg->expr))
2978 return false;
2981 #undef GENERIC_ID
2983 need_full_assumed_size = temp;
2984 name = NULL;
2986 if (!pure_function (expr, &name) && name)
2988 if (forall_flag)
2990 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2991 "FORALL %s", name, &expr->where,
2992 forall_flag == 2 ? "mask" : "block");
2993 t = false;
2995 else if (gfc_do_concurrent_flag)
2997 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2998 "DO CONCURRENT %s", name, &expr->where,
2999 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3000 t = false;
3002 else if (gfc_pure (NULL))
3004 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3005 "procedure within a PURE procedure", name, &expr->where);
3006 t = false;
3009 if (gfc_implicit_pure (NULL))
3010 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3013 /* Functions without the RECURSIVE attribution are not allowed to
3014 * call themselves. */
3015 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3017 gfc_symbol *esym;
3018 esym = expr->value.function.esym;
3020 if (is_illegal_recursion (esym, gfc_current_ns))
3022 if (esym->attr.entry && esym->ns->entries)
3023 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3024 " function '%s' is not RECURSIVE",
3025 esym->name, &expr->where, esym->ns->entries->sym->name);
3026 else
3027 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3028 " is not RECURSIVE", esym->name, &expr->where);
3030 t = false;
3034 /* Character lengths of use associated functions may contains references to
3035 symbols not referenced from the current program unit otherwise. Make sure
3036 those symbols are marked as referenced. */
3038 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3039 && expr->value.function.esym->attr.use_assoc)
3041 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3044 /* Make sure that the expression has a typespec that works. */
3045 if (expr->ts.type == BT_UNKNOWN)
3047 if (expr->symtree->n.sym->result
3048 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3049 && !expr->symtree->n.sym->result->attr.proc_pointer)
3050 expr->ts = expr->symtree->n.sym->result->ts;
3053 return t;
3057 /************* Subroutine resolution *************/
3059 static void
3060 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3062 if (gfc_pure (sym))
3063 return;
3065 if (forall_flag)
3066 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3067 sym->name, &c->loc);
3068 else if (gfc_do_concurrent_flag)
3069 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3070 "PURE", sym->name, &c->loc);
3071 else if (gfc_pure (NULL))
3072 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3073 &c->loc);
3075 if (gfc_implicit_pure (NULL))
3076 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3080 static match
3081 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3083 gfc_symbol *s;
3085 if (sym->attr.generic)
3087 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3088 if (s != NULL)
3090 c->resolved_sym = s;
3091 pure_subroutine (c, s);
3092 return MATCH_YES;
3095 /* TODO: Need to search for elemental references in generic interface. */
3098 if (sym->attr.intrinsic)
3099 return gfc_intrinsic_sub_interface (c, 0);
3101 return MATCH_NO;
3105 static bool
3106 resolve_generic_s (gfc_code *c)
3108 gfc_symbol *sym;
3109 match m;
3111 sym = c->symtree->n.sym;
3113 for (;;)
3115 m = resolve_generic_s0 (c, sym);
3116 if (m == MATCH_YES)
3117 return true;
3118 else if (m == MATCH_ERROR)
3119 return false;
3121 generic:
3122 if (sym->ns->parent == NULL)
3123 break;
3124 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3126 if (sym == NULL)
3127 break;
3128 if (!generic_sym (sym))
3129 goto generic;
3132 /* Last ditch attempt. See if the reference is to an intrinsic
3133 that possesses a matching interface. 14.1.2.4 */
3134 sym = c->symtree->n.sym;
3136 if (!gfc_is_intrinsic (sym, 1, c->loc))
3138 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3139 sym->name, &c->loc);
3140 return false;
3143 m = gfc_intrinsic_sub_interface (c, 0);
3144 if (m == MATCH_YES)
3145 return true;
3146 if (m == MATCH_NO)
3147 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3148 "intrinsic subroutine interface", sym->name, &c->loc);
3150 return false;
3154 /* Resolve a subroutine call known to be specific. */
3156 static match
3157 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3159 match m;
3161 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3163 if (sym->attr.dummy)
3165 sym->attr.proc = PROC_DUMMY;
3166 goto found;
3169 sym->attr.proc = PROC_EXTERNAL;
3170 goto found;
3173 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3174 goto found;
3176 if (sym->attr.intrinsic)
3178 m = gfc_intrinsic_sub_interface (c, 1);
3179 if (m == MATCH_YES)
3180 return MATCH_YES;
3181 if (m == MATCH_NO)
3182 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3183 "with an intrinsic", sym->name, &c->loc);
3185 return MATCH_ERROR;
3188 return MATCH_NO;
3190 found:
3191 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3193 c->resolved_sym = sym;
3194 pure_subroutine (c, sym);
3196 return MATCH_YES;
3200 static bool
3201 resolve_specific_s (gfc_code *c)
3203 gfc_symbol *sym;
3204 match m;
3206 sym = c->symtree->n.sym;
3208 for (;;)
3210 m = resolve_specific_s0 (c, sym);
3211 if (m == MATCH_YES)
3212 return true;
3213 if (m == MATCH_ERROR)
3214 return false;
3216 if (sym->ns->parent == NULL)
3217 break;
3219 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3221 if (sym == NULL)
3222 break;
3225 sym = c->symtree->n.sym;
3226 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3227 sym->name, &c->loc);
3229 return false;
3233 /* Resolve a subroutine call not known to be generic nor specific. */
3235 static bool
3236 resolve_unknown_s (gfc_code *c)
3238 gfc_symbol *sym;
3240 sym = c->symtree->n.sym;
3242 if (sym->attr.dummy)
3244 sym->attr.proc = PROC_DUMMY;
3245 goto found;
3248 /* See if we have an intrinsic function reference. */
3250 if (gfc_is_intrinsic (sym, 1, c->loc))
3252 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3253 return true;
3254 return false;
3257 /* The reference is to an external name. */
3259 found:
3260 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3262 c->resolved_sym = sym;
3264 pure_subroutine (c, sym);
3266 return true;
3270 /* Resolve a subroutine call. Although it was tempting to use the same code
3271 for functions, subroutines and functions are stored differently and this
3272 makes things awkward. */
3274 static bool
3275 resolve_call (gfc_code *c)
3277 bool t;
3278 procedure_type ptype = PROC_INTRINSIC;
3279 gfc_symbol *csym, *sym;
3280 bool no_formal_args;
3282 csym = c->symtree ? c->symtree->n.sym : NULL;
3284 if (csym && csym->ts.type != BT_UNKNOWN)
3286 gfc_error ("'%s' at %L has a type, which is not consistent with "
3287 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3288 return false;
3291 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3293 gfc_symtree *st;
3294 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3295 sym = st ? st->n.sym : NULL;
3296 if (sym && csym != sym
3297 && sym->ns == gfc_current_ns
3298 && sym->attr.flavor == FL_PROCEDURE
3299 && sym->attr.contained)
3301 sym->refs++;
3302 if (csym->attr.generic)
3303 c->symtree->n.sym = sym;
3304 else
3305 c->symtree = st;
3306 csym = c->symtree->n.sym;
3310 /* If this ia a deferred TBP, c->expr1 will be set. */
3311 if (!c->expr1 && csym)
3313 if (csym->attr.abstract)
3315 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3316 csym->name, &c->loc);
3317 return false;
3320 /* Subroutines without the RECURSIVE attribution are not allowed to
3321 call themselves. */
3322 if (is_illegal_recursion (csym, gfc_current_ns))
3324 if (csym->attr.entry && csym->ns->entries)
3325 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3326 "as subroutine '%s' is not RECURSIVE",
3327 csym->name, &c->loc, csym->ns->entries->sym->name);
3328 else
3329 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3330 "as it is not RECURSIVE", csym->name, &c->loc);
3332 t = false;
3336 /* Switch off assumed size checking and do this again for certain kinds
3337 of procedure, once the procedure itself is resolved. */
3338 need_full_assumed_size++;
3340 if (csym)
3341 ptype = csym->attr.proc;
3343 no_formal_args = csym && is_external_proc (csym)
3344 && gfc_sym_get_dummy_args (csym) == NULL;
3345 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3346 return false;
3348 /* Resume assumed_size checking. */
3349 need_full_assumed_size--;
3351 /* If external, check for usage. */
3352 if (csym && is_external_proc (csym))
3353 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3355 t = true;
3356 if (c->resolved_sym == NULL)
3358 c->resolved_isym = NULL;
3359 switch (procedure_kind (csym))
3361 case PTYPE_GENERIC:
3362 t = resolve_generic_s (c);
3363 break;
3365 case PTYPE_SPECIFIC:
3366 t = resolve_specific_s (c);
3367 break;
3369 case PTYPE_UNKNOWN:
3370 t = resolve_unknown_s (c);
3371 break;
3373 default:
3374 gfc_internal_error ("resolve_subroutine(): bad function type");
3378 /* Some checks of elemental subroutine actual arguments. */
3379 if (!resolve_elemental_actual (NULL, c))
3380 return false;
3382 return t;
3386 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3387 op1->shape and op2->shape are non-NULL return true if their shapes
3388 match. If both op1->shape and op2->shape are non-NULL return false
3389 if their shapes do not match. If either op1->shape or op2->shape is
3390 NULL, return true. */
3392 static bool
3393 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3395 bool t;
3396 int i;
3398 t = true;
3400 if (op1->shape != NULL && op2->shape != NULL)
3402 for (i = 0; i < op1->rank; i++)
3404 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3406 gfc_error ("Shapes for operands at %L and %L are not conformable",
3407 &op1->where, &op2->where);
3408 t = false;
3409 break;
3414 return t;
3418 /* Resolve an operator expression node. This can involve replacing the
3419 operation with a user defined function call. */
3421 static bool
3422 resolve_operator (gfc_expr *e)
3424 gfc_expr *op1, *op2;
3425 char msg[200];
3426 bool dual_locus_error;
3427 bool t;
3429 /* Resolve all subnodes-- give them types. */
3431 switch (e->value.op.op)
3433 default:
3434 if (!gfc_resolve_expr (e->value.op.op2))
3435 return false;
3437 /* Fall through... */
3439 case INTRINSIC_NOT:
3440 case INTRINSIC_UPLUS:
3441 case INTRINSIC_UMINUS:
3442 case INTRINSIC_PARENTHESES:
3443 if (!gfc_resolve_expr (e->value.op.op1))
3444 return false;
3445 break;
3448 /* Typecheck the new node. */
3450 op1 = e->value.op.op1;
3451 op2 = e->value.op.op2;
3452 dual_locus_error = false;
3454 if ((op1 && op1->expr_type == EXPR_NULL)
3455 || (op2 && op2->expr_type == EXPR_NULL))
3457 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3458 goto bad_op;
3461 switch (e->value.op.op)
3463 case INTRINSIC_UPLUS:
3464 case INTRINSIC_UMINUS:
3465 if (op1->ts.type == BT_INTEGER
3466 || op1->ts.type == BT_REAL
3467 || op1->ts.type == BT_COMPLEX)
3469 e->ts = op1->ts;
3470 break;
3473 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3474 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3475 goto bad_op;
3477 case INTRINSIC_PLUS:
3478 case INTRINSIC_MINUS:
3479 case INTRINSIC_TIMES:
3480 case INTRINSIC_DIVIDE:
3481 case INTRINSIC_POWER:
3482 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3484 gfc_type_convert_binary (e, 1);
3485 break;
3488 sprintf (msg,
3489 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3490 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3491 gfc_typename (&op2->ts));
3492 goto bad_op;
3494 case INTRINSIC_CONCAT:
3495 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3496 && op1->ts.kind == op2->ts.kind)
3498 e->ts.type = BT_CHARACTER;
3499 e->ts.kind = op1->ts.kind;
3500 break;
3503 sprintf (msg,
3504 _("Operands of string concatenation operator at %%L are %s/%s"),
3505 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3506 goto bad_op;
3508 case INTRINSIC_AND:
3509 case INTRINSIC_OR:
3510 case INTRINSIC_EQV:
3511 case INTRINSIC_NEQV:
3512 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3514 e->ts.type = BT_LOGICAL;
3515 e->ts.kind = gfc_kind_max (op1, op2);
3516 if (op1->ts.kind < e->ts.kind)
3517 gfc_convert_type (op1, &e->ts, 2);
3518 else if (op2->ts.kind < e->ts.kind)
3519 gfc_convert_type (op2, &e->ts, 2);
3520 break;
3523 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3524 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3525 gfc_typename (&op2->ts));
3527 goto bad_op;
3529 case INTRINSIC_NOT:
3530 if (op1->ts.type == BT_LOGICAL)
3532 e->ts.type = BT_LOGICAL;
3533 e->ts.kind = op1->ts.kind;
3534 break;
3537 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3538 gfc_typename (&op1->ts));
3539 goto bad_op;
3541 case INTRINSIC_GT:
3542 case INTRINSIC_GT_OS:
3543 case INTRINSIC_GE:
3544 case INTRINSIC_GE_OS:
3545 case INTRINSIC_LT:
3546 case INTRINSIC_LT_OS:
3547 case INTRINSIC_LE:
3548 case INTRINSIC_LE_OS:
3549 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3551 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3552 goto bad_op;
3555 /* Fall through... */
3557 case INTRINSIC_EQ:
3558 case INTRINSIC_EQ_OS:
3559 case INTRINSIC_NE:
3560 case INTRINSIC_NE_OS:
3561 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3562 && op1->ts.kind == op2->ts.kind)
3564 e->ts.type = BT_LOGICAL;
3565 e->ts.kind = gfc_default_logical_kind;
3566 break;
3569 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3571 gfc_type_convert_binary (e, 1);
3573 e->ts.type = BT_LOGICAL;
3574 e->ts.kind = gfc_default_logical_kind;
3576 if (gfc_option.warn_compare_reals)
3578 gfc_intrinsic_op op = e->value.op.op;
3580 /* Type conversion has made sure that the types of op1 and op2
3581 agree, so it is only necessary to check the first one. */
3582 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3583 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3584 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3586 const char *msg;
3588 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3589 msg = "Equality comparison for %s at %L";
3590 else
3591 msg = "Inequality comparison for %s at %L";
3593 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3597 break;
3600 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3601 sprintf (msg,
3602 _("Logicals at %%L must be compared with %s instead of %s"),
3603 (e->value.op.op == INTRINSIC_EQ
3604 || e->value.op.op == INTRINSIC_EQ_OS)
3605 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3606 else
3607 sprintf (msg,
3608 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3609 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3610 gfc_typename (&op2->ts));
3612 goto bad_op;
3614 case INTRINSIC_USER:
3615 if (e->value.op.uop->op == NULL)
3616 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3617 else if (op2 == NULL)
3618 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3619 e->value.op.uop->name, gfc_typename (&op1->ts));
3620 else
3622 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3623 e->value.op.uop->name, gfc_typename (&op1->ts),
3624 gfc_typename (&op2->ts));
3625 e->value.op.uop->op->sym->attr.referenced = 1;
3628 goto bad_op;
3630 case INTRINSIC_PARENTHESES:
3631 e->ts = op1->ts;
3632 if (e->ts.type == BT_CHARACTER)
3633 e->ts.u.cl = op1->ts.u.cl;
3634 break;
3636 default:
3637 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3640 /* Deal with arrayness of an operand through an operator. */
3642 t = true;
3644 switch (e->value.op.op)
3646 case INTRINSIC_PLUS:
3647 case INTRINSIC_MINUS:
3648 case INTRINSIC_TIMES:
3649 case INTRINSIC_DIVIDE:
3650 case INTRINSIC_POWER:
3651 case INTRINSIC_CONCAT:
3652 case INTRINSIC_AND:
3653 case INTRINSIC_OR:
3654 case INTRINSIC_EQV:
3655 case INTRINSIC_NEQV:
3656 case INTRINSIC_EQ:
3657 case INTRINSIC_EQ_OS:
3658 case INTRINSIC_NE:
3659 case INTRINSIC_NE_OS:
3660 case INTRINSIC_GT:
3661 case INTRINSIC_GT_OS:
3662 case INTRINSIC_GE:
3663 case INTRINSIC_GE_OS:
3664 case INTRINSIC_LT:
3665 case INTRINSIC_LT_OS:
3666 case INTRINSIC_LE:
3667 case INTRINSIC_LE_OS:
3669 if (op1->rank == 0 && op2->rank == 0)
3670 e->rank = 0;
3672 if (op1->rank == 0 && op2->rank != 0)
3674 e->rank = op2->rank;
3676 if (e->shape == NULL)
3677 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3680 if (op1->rank != 0 && op2->rank == 0)
3682 e->rank = op1->rank;
3684 if (e->shape == NULL)
3685 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3688 if (op1->rank != 0 && op2->rank != 0)
3690 if (op1->rank == op2->rank)
3692 e->rank = op1->rank;
3693 if (e->shape == NULL)
3695 t = compare_shapes (op1, op2);
3696 if (!t)
3697 e->shape = NULL;
3698 else
3699 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3702 else
3704 /* Allow higher level expressions to work. */
3705 e->rank = 0;
3707 /* Try user-defined operators, and otherwise throw an error. */
3708 dual_locus_error = true;
3709 sprintf (msg,
3710 _("Inconsistent ranks for operator at %%L and %%L"));
3711 goto bad_op;
3715 break;
3717 case INTRINSIC_PARENTHESES:
3718 case INTRINSIC_NOT:
3719 case INTRINSIC_UPLUS:
3720 case INTRINSIC_UMINUS:
3721 /* Simply copy arrayness attribute */
3722 e->rank = op1->rank;
3724 if (e->shape == NULL)
3725 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3727 break;
3729 default:
3730 break;
3733 /* Attempt to simplify the expression. */
3734 if (t)
3736 t = gfc_simplify_expr (e, 0);
3737 /* Some calls do not succeed in simplification and return false
3738 even though there is no error; e.g. variable references to
3739 PARAMETER arrays. */
3740 if (!gfc_is_constant_expr (e))
3741 t = true;
3743 return t;
3745 bad_op:
3748 match m = gfc_extend_expr (e);
3749 if (m == MATCH_YES)
3750 return true;
3751 if (m == MATCH_ERROR)
3752 return false;
3755 if (dual_locus_error)
3756 gfc_error (msg, &op1->where, &op2->where);
3757 else
3758 gfc_error (msg, &e->where);
3760 return false;
3764 /************** Array resolution subroutines **************/
3766 typedef enum
3767 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3768 comparison;
3770 /* Compare two integer expressions. */
3772 static comparison
3773 compare_bound (gfc_expr *a, gfc_expr *b)
3775 int i;
3777 if (a == NULL || a->expr_type != EXPR_CONSTANT
3778 || b == NULL || b->expr_type != EXPR_CONSTANT)
3779 return CMP_UNKNOWN;
3781 /* If either of the types isn't INTEGER, we must have
3782 raised an error earlier. */
3784 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3785 return CMP_UNKNOWN;
3787 i = mpz_cmp (a->value.integer, b->value.integer);
3789 if (i < 0)
3790 return CMP_LT;
3791 if (i > 0)
3792 return CMP_GT;
3793 return CMP_EQ;
3797 /* Compare an integer expression with an integer. */
3799 static comparison
3800 compare_bound_int (gfc_expr *a, int b)
3802 int i;
3804 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3805 return CMP_UNKNOWN;
3807 if (a->ts.type != BT_INTEGER)
3808 gfc_internal_error ("compare_bound_int(): Bad expression");
3810 i = mpz_cmp_si (a->value.integer, b);
3812 if (i < 0)
3813 return CMP_LT;
3814 if (i > 0)
3815 return CMP_GT;
3816 return CMP_EQ;
3820 /* Compare an integer expression with a mpz_t. */
3822 static comparison
3823 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3825 int i;
3827 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3828 return CMP_UNKNOWN;
3830 if (a->ts.type != BT_INTEGER)
3831 gfc_internal_error ("compare_bound_int(): Bad expression");
3833 i = mpz_cmp (a->value.integer, b);
3835 if (i < 0)
3836 return CMP_LT;
3837 if (i > 0)
3838 return CMP_GT;
3839 return CMP_EQ;
3843 /* Compute the last value of a sequence given by a triplet.
3844 Return 0 if it wasn't able to compute the last value, or if the
3845 sequence if empty, and 1 otherwise. */
3847 static int
3848 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3849 gfc_expr *stride, mpz_t last)
3851 mpz_t rem;
3853 if (start == NULL || start->expr_type != EXPR_CONSTANT
3854 || end == NULL || end->expr_type != EXPR_CONSTANT
3855 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3856 return 0;
3858 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3859 || (stride != NULL && stride->ts.type != BT_INTEGER))
3860 return 0;
3862 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3864 if (compare_bound (start, end) == CMP_GT)
3865 return 0;
3866 mpz_set (last, end->value.integer);
3867 return 1;
3870 if (compare_bound_int (stride, 0) == CMP_GT)
3872 /* Stride is positive */
3873 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3874 return 0;
3876 else
3878 /* Stride is negative */
3879 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3880 return 0;
3883 mpz_init (rem);
3884 mpz_sub (rem, end->value.integer, start->value.integer);
3885 mpz_tdiv_r (rem, rem, stride->value.integer);
3886 mpz_sub (last, end->value.integer, rem);
3887 mpz_clear (rem);
3889 return 1;
3893 /* Compare a single dimension of an array reference to the array
3894 specification. */
3896 static bool
3897 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3899 mpz_t last_value;
3901 if (ar->dimen_type[i] == DIMEN_STAR)
3903 gcc_assert (ar->stride[i] == NULL);
3904 /* This implies [*] as [*:] and [*:3] are not possible. */
3905 if (ar->start[i] == NULL)
3907 gcc_assert (ar->end[i] == NULL);
3908 return true;
3912 /* Given start, end and stride values, calculate the minimum and
3913 maximum referenced indexes. */
3915 switch (ar->dimen_type[i])
3917 case DIMEN_VECTOR:
3918 case DIMEN_THIS_IMAGE:
3919 break;
3921 case DIMEN_STAR:
3922 case DIMEN_ELEMENT:
3923 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3925 if (i < as->rank)
3926 gfc_warning ("Array reference at %L is out of bounds "
3927 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3928 mpz_get_si (ar->start[i]->value.integer),
3929 mpz_get_si (as->lower[i]->value.integer), i+1);
3930 else
3931 gfc_warning ("Array reference at %L is out of bounds "
3932 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3933 mpz_get_si (ar->start[i]->value.integer),
3934 mpz_get_si (as->lower[i]->value.integer),
3935 i + 1 - as->rank);
3936 return true;
3938 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3940 if (i < as->rank)
3941 gfc_warning ("Array reference at %L is out of bounds "
3942 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3943 mpz_get_si (ar->start[i]->value.integer),
3944 mpz_get_si (as->upper[i]->value.integer), i+1);
3945 else
3946 gfc_warning ("Array reference at %L is out of bounds "
3947 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3948 mpz_get_si (ar->start[i]->value.integer),
3949 mpz_get_si (as->upper[i]->value.integer),
3950 i + 1 - as->rank);
3951 return true;
3954 break;
3956 case DIMEN_RANGE:
3958 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3959 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3961 comparison comp_start_end = compare_bound (AR_START, AR_END);
3963 /* Check for zero stride, which is not allowed. */
3964 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3966 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3967 return false;
3970 /* if start == len || (stride > 0 && start < len)
3971 || (stride < 0 && start > len),
3972 then the array section contains at least one element. In this
3973 case, there is an out-of-bounds access if
3974 (start < lower || start > upper). */
3975 if (compare_bound (AR_START, AR_END) == CMP_EQ
3976 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3977 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3978 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3979 && comp_start_end == CMP_GT))
3981 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3983 gfc_warning ("Lower array reference at %L is out of bounds "
3984 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3985 mpz_get_si (AR_START->value.integer),
3986 mpz_get_si (as->lower[i]->value.integer), i+1);
3987 return true;
3989 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3991 gfc_warning ("Lower array reference at %L is out of bounds "
3992 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3993 mpz_get_si (AR_START->value.integer),
3994 mpz_get_si (as->upper[i]->value.integer), i+1);
3995 return true;
3999 /* If we can compute the highest index of the array section,
4000 then it also has to be between lower and upper. */
4001 mpz_init (last_value);
4002 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4003 last_value))
4005 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4007 gfc_warning ("Upper array reference at %L is out of bounds "
4008 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4009 mpz_get_si (last_value),
4010 mpz_get_si (as->lower[i]->value.integer), i+1);
4011 mpz_clear (last_value);
4012 return true;
4014 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4016 gfc_warning ("Upper array reference at %L is out of bounds "
4017 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4018 mpz_get_si (last_value),
4019 mpz_get_si (as->upper[i]->value.integer), i+1);
4020 mpz_clear (last_value);
4021 return true;
4024 mpz_clear (last_value);
4026 #undef AR_START
4027 #undef AR_END
4029 break;
4031 default:
4032 gfc_internal_error ("check_dimension(): Bad array reference");
4035 return true;
4039 /* Compare an array reference with an array specification. */
4041 static bool
4042 compare_spec_to_ref (gfc_array_ref *ar)
4044 gfc_array_spec *as;
4045 int i;
4047 as = ar->as;
4048 i = as->rank - 1;
4049 /* TODO: Full array sections are only allowed as actual parameters. */
4050 if (as->type == AS_ASSUMED_SIZE
4051 && (/*ar->type == AR_FULL
4052 ||*/ (ar->type == AR_SECTION
4053 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4055 gfc_error ("Rightmost upper bound of assumed size array section "
4056 "not specified at %L", &ar->where);
4057 return false;
4060 if (ar->type == AR_FULL)
4061 return true;
4063 if (as->rank != ar->dimen)
4065 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4066 &ar->where, ar->dimen, as->rank);
4067 return false;
4070 /* ar->codimen == 0 is a local array. */
4071 if (as->corank != ar->codimen && ar->codimen != 0)
4073 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4074 &ar->where, ar->codimen, as->corank);
4075 return false;
4078 for (i = 0; i < as->rank; i++)
4079 if (!check_dimension (i, ar, as))
4080 return false;
4082 /* Local access has no coarray spec. */
4083 if (ar->codimen != 0)
4084 for (i = as->rank; i < as->rank + as->corank; i++)
4086 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4087 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4089 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4090 i + 1 - as->rank, &ar->where);
4091 return false;
4093 if (!check_dimension (i, ar, as))
4094 return false;
4097 return true;
4101 /* Resolve one part of an array index. */
4103 static bool
4104 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4105 int force_index_integer_kind)
4107 gfc_typespec ts;
4109 if (index == NULL)
4110 return true;
4112 if (!gfc_resolve_expr (index))
4113 return false;
4115 if (check_scalar && index->rank != 0)
4117 gfc_error ("Array index at %L must be scalar", &index->where);
4118 return false;
4121 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4123 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4124 &index->where, gfc_basic_typename (index->ts.type));
4125 return false;
4128 if (index->ts.type == BT_REAL)
4129 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4130 &index->where))
4131 return false;
4133 if ((index->ts.kind != gfc_index_integer_kind
4134 && force_index_integer_kind)
4135 || index->ts.type != BT_INTEGER)
4137 gfc_clear_ts (&ts);
4138 ts.type = BT_INTEGER;
4139 ts.kind = gfc_index_integer_kind;
4141 gfc_convert_type_warn (index, &ts, 2, 0);
4144 return true;
4147 /* Resolve one part of an array index. */
4149 bool
4150 gfc_resolve_index (gfc_expr *index, int check_scalar)
4152 return gfc_resolve_index_1 (index, check_scalar, 1);
4155 /* Resolve a dim argument to an intrinsic function. */
4157 bool
4158 gfc_resolve_dim_arg (gfc_expr *dim)
4160 if (dim == NULL)
4161 return true;
4163 if (!gfc_resolve_expr (dim))
4164 return false;
4166 if (dim->rank != 0)
4168 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4169 return false;
4173 if (dim->ts.type != BT_INTEGER)
4175 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4176 return false;
4179 if (dim->ts.kind != gfc_index_integer_kind)
4181 gfc_typespec ts;
4183 gfc_clear_ts (&ts);
4184 ts.type = BT_INTEGER;
4185 ts.kind = gfc_index_integer_kind;
4187 gfc_convert_type_warn (dim, &ts, 2, 0);
4190 return true;
4193 /* Given an expression that contains array references, update those array
4194 references to point to the right array specifications. While this is
4195 filled in during matching, this information is difficult to save and load
4196 in a module, so we take care of it here.
4198 The idea here is that the original array reference comes from the
4199 base symbol. We traverse the list of reference structures, setting
4200 the stored reference to references. Component references can
4201 provide an additional array specification. */
4203 static void
4204 find_array_spec (gfc_expr *e)
4206 gfc_array_spec *as;
4207 gfc_component *c;
4208 gfc_ref *ref;
4210 if (e->symtree->n.sym->ts.type == BT_CLASS)
4211 as = CLASS_DATA (e->symtree->n.sym)->as;
4212 else
4213 as = e->symtree->n.sym->as;
4215 for (ref = e->ref; ref; ref = ref->next)
4216 switch (ref->type)
4218 case REF_ARRAY:
4219 if (as == NULL)
4220 gfc_internal_error ("find_array_spec(): Missing spec");
4222 ref->u.ar.as = as;
4223 as = NULL;
4224 break;
4226 case REF_COMPONENT:
4227 c = ref->u.c.component;
4228 if (c->attr.dimension)
4230 if (as != NULL)
4231 gfc_internal_error ("find_array_spec(): unused as(1)");
4232 as = c->as;
4235 break;
4237 case REF_SUBSTRING:
4238 break;
4241 if (as != NULL)
4242 gfc_internal_error ("find_array_spec(): unused as(2)");
4246 /* Resolve an array reference. */
4248 static bool
4249 resolve_array_ref (gfc_array_ref *ar)
4251 int i, check_scalar;
4252 gfc_expr *e;
4254 for (i = 0; i < ar->dimen + ar->codimen; i++)
4256 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4258 /* Do not force gfc_index_integer_kind for the start. We can
4259 do fine with any integer kind. This avoids temporary arrays
4260 created for indexing with a vector. */
4261 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4262 return false;
4263 if (!gfc_resolve_index (ar->end[i], check_scalar))
4264 return false;
4265 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4266 return false;
4268 e = ar->start[i];
4270 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4271 switch (e->rank)
4273 case 0:
4274 ar->dimen_type[i] = DIMEN_ELEMENT;
4275 break;
4277 case 1:
4278 ar->dimen_type[i] = DIMEN_VECTOR;
4279 if (e->expr_type == EXPR_VARIABLE
4280 && e->symtree->n.sym->ts.type == BT_DERIVED)
4281 ar->start[i] = gfc_get_parentheses (e);
4282 break;
4284 default:
4285 gfc_error ("Array index at %L is an array of rank %d",
4286 &ar->c_where[i], e->rank);
4287 return false;
4290 /* Fill in the upper bound, which may be lower than the
4291 specified one for something like a(2:10:5), which is
4292 identical to a(2:7:5). Only relevant for strides not equal
4293 to one. Don't try a division by zero. */
4294 if (ar->dimen_type[i] == DIMEN_RANGE
4295 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4296 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4297 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4299 mpz_t size, end;
4301 if (gfc_ref_dimen_size (ar, i, &size, &end))
4303 if (ar->end[i] == NULL)
4305 ar->end[i] =
4306 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4307 &ar->where);
4308 mpz_set (ar->end[i]->value.integer, end);
4310 else if (ar->end[i]->ts.type == BT_INTEGER
4311 && ar->end[i]->expr_type == EXPR_CONSTANT)
4313 mpz_set (ar->end[i]->value.integer, end);
4315 else
4316 gcc_unreachable ();
4318 mpz_clear (size);
4319 mpz_clear (end);
4324 if (ar->type == AR_FULL)
4326 if (ar->as->rank == 0)
4327 ar->type = AR_ELEMENT;
4329 /* Make sure array is the same as array(:,:), this way
4330 we don't need to special case all the time. */
4331 ar->dimen = ar->as->rank;
4332 for (i = 0; i < ar->dimen; i++)
4334 ar->dimen_type[i] = DIMEN_RANGE;
4336 gcc_assert (ar->start[i] == NULL);
4337 gcc_assert (ar->end[i] == NULL);
4338 gcc_assert (ar->stride[i] == NULL);
4342 /* If the reference type is unknown, figure out what kind it is. */
4344 if (ar->type == AR_UNKNOWN)
4346 ar->type = AR_ELEMENT;
4347 for (i = 0; i < ar->dimen; i++)
4348 if (ar->dimen_type[i] == DIMEN_RANGE
4349 || ar->dimen_type[i] == DIMEN_VECTOR)
4351 ar->type = AR_SECTION;
4352 break;
4356 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4357 return false;
4359 if (ar->as->corank && ar->codimen == 0)
4361 int n;
4362 ar->codimen = ar->as->corank;
4363 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4364 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4367 return true;
4371 static bool
4372 resolve_substring (gfc_ref *ref)
4374 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4376 if (ref->u.ss.start != NULL)
4378 if (!gfc_resolve_expr (ref->u.ss.start))
4379 return false;
4381 if (ref->u.ss.start->ts.type != BT_INTEGER)
4383 gfc_error ("Substring start index at %L must be of type INTEGER",
4384 &ref->u.ss.start->where);
4385 return false;
4388 if (ref->u.ss.start->rank != 0)
4390 gfc_error ("Substring start index at %L must be scalar",
4391 &ref->u.ss.start->where);
4392 return false;
4395 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4396 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4397 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4399 gfc_error ("Substring start index at %L is less than one",
4400 &ref->u.ss.start->where);
4401 return false;
4405 if (ref->u.ss.end != NULL)
4407 if (!gfc_resolve_expr (ref->u.ss.end))
4408 return false;
4410 if (ref->u.ss.end->ts.type != BT_INTEGER)
4412 gfc_error ("Substring end index at %L must be of type INTEGER",
4413 &ref->u.ss.end->where);
4414 return false;
4417 if (ref->u.ss.end->rank != 0)
4419 gfc_error ("Substring end index at %L must be scalar",
4420 &ref->u.ss.end->where);
4421 return false;
4424 if (ref->u.ss.length != NULL
4425 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4426 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4427 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4429 gfc_error ("Substring end index at %L exceeds the string length",
4430 &ref->u.ss.start->where);
4431 return false;
4434 if (compare_bound_mpz_t (ref->u.ss.end,
4435 gfc_integer_kinds[k].huge) == CMP_GT
4436 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4437 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4439 gfc_error ("Substring end index at %L is too large",
4440 &ref->u.ss.end->where);
4441 return false;
4445 return true;
4449 /* This function supplies missing substring charlens. */
4451 void
4452 gfc_resolve_substring_charlen (gfc_expr *e)
4454 gfc_ref *char_ref;
4455 gfc_expr *start, *end;
4457 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4458 if (char_ref->type == REF_SUBSTRING)
4459 break;
4461 if (!char_ref)
4462 return;
4464 gcc_assert (char_ref->next == NULL);
4466 if (e->ts.u.cl)
4468 if (e->ts.u.cl->length)
4469 gfc_free_expr (e->ts.u.cl->length);
4470 else if (e->expr_type == EXPR_VARIABLE
4471 && e->symtree->n.sym->attr.dummy)
4472 return;
4475 e->ts.type = BT_CHARACTER;
4476 e->ts.kind = gfc_default_character_kind;
4478 if (!e->ts.u.cl)
4479 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4481 if (char_ref->u.ss.start)
4482 start = gfc_copy_expr (char_ref->u.ss.start);
4483 else
4484 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4486 if (char_ref->u.ss.end)
4487 end = gfc_copy_expr (char_ref->u.ss.end);
4488 else if (e->expr_type == EXPR_VARIABLE)
4489 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4490 else
4491 end = NULL;
4493 if (!start || !end)
4495 gfc_free_expr (start);
4496 gfc_free_expr (end);
4497 return;
4500 /* Length = (end - start +1). */
4501 e->ts.u.cl->length = gfc_subtract (end, start);
4502 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4503 gfc_get_int_expr (gfc_default_integer_kind,
4504 NULL, 1));
4506 e->ts.u.cl->length->ts.type = BT_INTEGER;
4507 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4509 /* Make sure that the length is simplified. */
4510 gfc_simplify_expr (e->ts.u.cl->length, 1);
4511 gfc_resolve_expr (e->ts.u.cl->length);
4515 /* Resolve subtype references. */
4517 static bool
4518 resolve_ref (gfc_expr *expr)
4520 int current_part_dimension, n_components, seen_part_dimension;
4521 gfc_ref *ref;
4523 for (ref = expr->ref; ref; ref = ref->next)
4524 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4526 find_array_spec (expr);
4527 break;
4530 for (ref = expr->ref; ref; ref = ref->next)
4531 switch (ref->type)
4533 case REF_ARRAY:
4534 if (!resolve_array_ref (&ref->u.ar))
4535 return false;
4536 break;
4538 case REF_COMPONENT:
4539 break;
4541 case REF_SUBSTRING:
4542 if (!resolve_substring (ref))
4543 return false;
4544 break;
4547 /* Check constraints on part references. */
4549 current_part_dimension = 0;
4550 seen_part_dimension = 0;
4551 n_components = 0;
4553 for (ref = expr->ref; ref; ref = ref->next)
4555 switch (ref->type)
4557 case REF_ARRAY:
4558 switch (ref->u.ar.type)
4560 case AR_FULL:
4561 /* Coarray scalar. */
4562 if (ref->u.ar.as->rank == 0)
4564 current_part_dimension = 0;
4565 break;
4567 /* Fall through. */
4568 case AR_SECTION:
4569 current_part_dimension = 1;
4570 break;
4572 case AR_ELEMENT:
4573 current_part_dimension = 0;
4574 break;
4576 case AR_UNKNOWN:
4577 gfc_internal_error ("resolve_ref(): Bad array reference");
4580 break;
4582 case REF_COMPONENT:
4583 if (current_part_dimension || seen_part_dimension)
4585 /* F03:C614. */
4586 if (ref->u.c.component->attr.pointer
4587 || ref->u.c.component->attr.proc_pointer
4588 || (ref->u.c.component->ts.type == BT_CLASS
4589 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4591 gfc_error ("Component to the right of a part reference "
4592 "with nonzero rank must not have the POINTER "
4593 "attribute at %L", &expr->where);
4594 return false;
4596 else if (ref->u.c.component->attr.allocatable
4597 || (ref->u.c.component->ts.type == BT_CLASS
4598 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4601 gfc_error ("Component to the right of a part reference "
4602 "with nonzero rank must not have the ALLOCATABLE "
4603 "attribute at %L", &expr->where);
4604 return false;
4608 n_components++;
4609 break;
4611 case REF_SUBSTRING:
4612 break;
4615 if (((ref->type == REF_COMPONENT && n_components > 1)
4616 || ref->next == NULL)
4617 && current_part_dimension
4618 && seen_part_dimension)
4620 gfc_error ("Two or more part references with nonzero rank must "
4621 "not be specified at %L", &expr->where);
4622 return false;
4625 if (ref->type == REF_COMPONENT)
4627 if (current_part_dimension)
4628 seen_part_dimension = 1;
4630 /* reset to make sure */
4631 current_part_dimension = 0;
4635 return true;
4639 /* Given an expression, determine its shape. This is easier than it sounds.
4640 Leaves the shape array NULL if it is not possible to determine the shape. */
4642 static void
4643 expression_shape (gfc_expr *e)
4645 mpz_t array[GFC_MAX_DIMENSIONS];
4646 int i;
4648 if (e->rank <= 0 || e->shape != NULL)
4649 return;
4651 for (i = 0; i < e->rank; i++)
4652 if (!gfc_array_dimen_size (e, i, &array[i]))
4653 goto fail;
4655 e->shape = gfc_get_shape (e->rank);
4657 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4659 return;
4661 fail:
4662 for (i--; i >= 0; i--)
4663 mpz_clear (array[i]);
4667 /* Given a variable expression node, compute the rank of the expression by
4668 examining the base symbol and any reference structures it may have. */
4670 static void
4671 expression_rank (gfc_expr *e)
4673 gfc_ref *ref;
4674 int i, rank;
4676 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4677 could lead to serious confusion... */
4678 gcc_assert (e->expr_type != EXPR_COMPCALL);
4680 if (e->ref == NULL)
4682 if (e->expr_type == EXPR_ARRAY)
4683 goto done;
4684 /* Constructors can have a rank different from one via RESHAPE(). */
4686 if (e->symtree == NULL)
4688 e->rank = 0;
4689 goto done;
4692 e->rank = (e->symtree->n.sym->as == NULL)
4693 ? 0 : e->symtree->n.sym->as->rank;
4694 goto done;
4697 rank = 0;
4699 for (ref = e->ref; ref; ref = ref->next)
4701 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4702 && ref->u.c.component->attr.function && !ref->next)
4703 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4705 if (ref->type != REF_ARRAY)
4706 continue;
4708 if (ref->u.ar.type == AR_FULL)
4710 rank = ref->u.ar.as->rank;
4711 break;
4714 if (ref->u.ar.type == AR_SECTION)
4716 /* Figure out the rank of the section. */
4717 if (rank != 0)
4718 gfc_internal_error ("expression_rank(): Two array specs");
4720 for (i = 0; i < ref->u.ar.dimen; i++)
4721 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4722 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4723 rank++;
4725 break;
4729 e->rank = rank;
4731 done:
4732 expression_shape (e);
4736 /* Resolve a variable expression. */
4738 static bool
4739 resolve_variable (gfc_expr *e)
4741 gfc_symbol *sym;
4742 bool t;
4744 t = true;
4746 if (e->symtree == NULL)
4747 return false;
4748 sym = e->symtree->n.sym;
4750 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4751 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4752 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4754 if (!actual_arg || inquiry_argument)
4756 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4757 "be used as actual argument", sym->name, &e->where);
4758 return false;
4761 /* TS 29113, 407b. */
4762 else if (e->ts.type == BT_ASSUMED)
4764 if (!actual_arg)
4766 gfc_error ("Assumed-type variable %s at %L may only be used "
4767 "as actual argument", sym->name, &e->where);
4768 return false;
4770 else if (inquiry_argument && !first_actual_arg)
4772 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4773 for all inquiry functions in resolve_function; the reason is
4774 that the function-name resolution happens too late in that
4775 function. */
4776 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4777 "an inquiry function shall be the first argument",
4778 sym->name, &e->where);
4779 return false;
4782 /* TS 29113, C535b. */
4783 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4784 && CLASS_DATA (sym)->as
4785 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4786 || (sym->ts.type != BT_CLASS && sym->as
4787 && sym->as->type == AS_ASSUMED_RANK))
4789 if (!actual_arg)
4791 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4792 "actual argument", sym->name, &e->where);
4793 return false;
4795 else if (inquiry_argument && !first_actual_arg)
4797 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4798 for all inquiry functions in resolve_function; the reason is
4799 that the function-name resolution happens too late in that
4800 function. */
4801 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4802 "to an inquiry function shall be the first argument",
4803 sym->name, &e->where);
4804 return false;
4808 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4809 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4810 && e->ref->next == NULL))
4812 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4813 "a subobject reference", sym->name, &e->ref->u.ar.where);
4814 return false;
4816 /* TS 29113, 407b. */
4817 else if (e->ts.type == BT_ASSUMED && e->ref
4818 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4819 && e->ref->next == NULL))
4821 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4822 "reference", sym->name, &e->ref->u.ar.where);
4823 return false;
4826 /* TS 29113, C535b. */
4827 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4828 && CLASS_DATA (sym)->as
4829 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4830 || (sym->ts.type != BT_CLASS && sym->as
4831 && sym->as->type == AS_ASSUMED_RANK))
4832 && e->ref
4833 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4834 && e->ref->next == NULL))
4836 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4837 "reference", sym->name, &e->ref->u.ar.where);
4838 return false;
4842 /* If this is an associate-name, it may be parsed with an array reference
4843 in error even though the target is scalar. Fail directly in this case.
4844 TODO Understand why class scalar expressions must be excluded. */
4845 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4847 if (sym->ts.type == BT_CLASS)
4848 gfc_fix_class_refs (e);
4849 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4850 return false;
4853 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4854 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4856 /* On the other hand, the parser may not have known this is an array;
4857 in this case, we have to add a FULL reference. */
4858 if (sym->assoc && sym->attr.dimension && !e->ref)
4860 e->ref = gfc_get_ref ();
4861 e->ref->type = REF_ARRAY;
4862 e->ref->u.ar.type = AR_FULL;
4863 e->ref->u.ar.dimen = 0;
4866 if (e->ref && !resolve_ref (e))
4867 return false;
4869 if (sym->attr.flavor == FL_PROCEDURE
4870 && (!sym->attr.function
4871 || (sym->attr.function && sym->result
4872 && sym->result->attr.proc_pointer
4873 && !sym->result->attr.function)))
4875 e->ts.type = BT_PROCEDURE;
4876 goto resolve_procedure;
4879 if (sym->ts.type != BT_UNKNOWN)
4880 gfc_variable_attr (e, &e->ts);
4881 else
4883 /* Must be a simple variable reference. */
4884 if (!gfc_set_default_type (sym, 1, sym->ns))
4885 return false;
4886 e->ts = sym->ts;
4889 if (check_assumed_size_reference (sym, e))
4890 return false;
4892 /* Deal with forward references to entries during resolve_code, to
4893 satisfy, at least partially, 12.5.2.5. */
4894 if (gfc_current_ns->entries
4895 && current_entry_id == sym->entry_id
4896 && cs_base
4897 && cs_base->current
4898 && cs_base->current->op != EXEC_ENTRY)
4900 gfc_entry_list *entry;
4901 gfc_formal_arglist *formal;
4902 int n;
4903 bool seen, saved_specification_expr;
4905 /* If the symbol is a dummy... */
4906 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4908 entry = gfc_current_ns->entries;
4909 seen = false;
4911 /* ...test if the symbol is a parameter of previous entries. */
4912 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4913 for (formal = entry->sym->formal; formal; formal = formal->next)
4915 if (formal->sym && sym->name == formal->sym->name)
4917 seen = true;
4918 break;
4922 /* If it has not been seen as a dummy, this is an error. */
4923 if (!seen)
4925 if (specification_expr)
4926 gfc_error ("Variable '%s', used in a specification expression"
4927 ", is referenced at %L before the ENTRY statement "
4928 "in which it is a parameter",
4929 sym->name, &cs_base->current->loc);
4930 else
4931 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4932 "statement in which it is a parameter",
4933 sym->name, &cs_base->current->loc);
4934 t = false;
4938 /* Now do the same check on the specification expressions. */
4939 saved_specification_expr = specification_expr;
4940 specification_expr = true;
4941 if (sym->ts.type == BT_CHARACTER
4942 && !gfc_resolve_expr (sym->ts.u.cl->length))
4943 t = false;
4945 if (sym->as)
4946 for (n = 0; n < sym->as->rank; n++)
4948 if (!gfc_resolve_expr (sym->as->lower[n]))
4949 t = false;
4950 if (!gfc_resolve_expr (sym->as->upper[n]))
4951 t = false;
4953 specification_expr = saved_specification_expr;
4955 if (t)
4956 /* Update the symbol's entry level. */
4957 sym->entry_id = current_entry_id + 1;
4960 /* If a symbol has been host_associated mark it. This is used latter,
4961 to identify if aliasing is possible via host association. */
4962 if (sym->attr.flavor == FL_VARIABLE
4963 && gfc_current_ns->parent
4964 && (gfc_current_ns->parent == sym->ns
4965 || (gfc_current_ns->parent->parent
4966 && gfc_current_ns->parent->parent == sym->ns)))
4967 sym->attr.host_assoc = 1;
4969 resolve_procedure:
4970 if (t && !resolve_procedure_expression (e))
4971 t = false;
4973 /* F2008, C617 and C1229. */
4974 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4975 && gfc_is_coindexed (e))
4977 gfc_ref *ref, *ref2 = NULL;
4979 for (ref = e->ref; ref; ref = ref->next)
4981 if (ref->type == REF_COMPONENT)
4982 ref2 = ref;
4983 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4984 break;
4987 for ( ; ref; ref = ref->next)
4988 if (ref->type == REF_COMPONENT)
4989 break;
4991 /* Expression itself is not coindexed object. */
4992 if (ref && e->ts.type == BT_CLASS)
4994 gfc_error ("Polymorphic subobject of coindexed object at %L",
4995 &e->where);
4996 t = false;
4999 /* Expression itself is coindexed object. */
5000 if (ref == NULL)
5002 gfc_component *c;
5003 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5004 for ( ; c; c = c->next)
5005 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5007 gfc_error ("Coindexed object with polymorphic allocatable "
5008 "subcomponent at %L", &e->where);
5009 t = false;
5010 break;
5015 return t;
5019 /* Checks to see that the correct symbol has been host associated.
5020 The only situation where this arises is that in which a twice
5021 contained function is parsed after the host association is made.
5022 Therefore, on detecting this, change the symbol in the expression
5023 and convert the array reference into an actual arglist if the old
5024 symbol is a variable. */
5025 static bool
5026 check_host_association (gfc_expr *e)
5028 gfc_symbol *sym, *old_sym;
5029 gfc_symtree *st;
5030 int n;
5031 gfc_ref *ref;
5032 gfc_actual_arglist *arg, *tail = NULL;
5033 bool retval = e->expr_type == EXPR_FUNCTION;
5035 /* If the expression is the result of substitution in
5036 interface.c(gfc_extend_expr) because there is no way in
5037 which the host association can be wrong. */
5038 if (e->symtree == NULL
5039 || e->symtree->n.sym == NULL
5040 || e->user_operator)
5041 return retval;
5043 old_sym = e->symtree->n.sym;
5045 if (gfc_current_ns->parent
5046 && old_sym->ns != gfc_current_ns)
5048 /* Use the 'USE' name so that renamed module symbols are
5049 correctly handled. */
5050 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5052 if (sym && old_sym != sym
5053 && sym->ts.type == old_sym->ts.type
5054 && sym->attr.flavor == FL_PROCEDURE
5055 && sym->attr.contained)
5057 /* Clear the shape, since it might not be valid. */
5058 gfc_free_shape (&e->shape, e->rank);
5060 /* Give the expression the right symtree! */
5061 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5062 gcc_assert (st != NULL);
5064 if (old_sym->attr.flavor == FL_PROCEDURE
5065 || e->expr_type == EXPR_FUNCTION)
5067 /* Original was function so point to the new symbol, since
5068 the actual argument list is already attached to the
5069 expression. */
5070 e->value.function.esym = NULL;
5071 e->symtree = st;
5073 else
5075 /* Original was variable so convert array references into
5076 an actual arglist. This does not need any checking now
5077 since resolve_function will take care of it. */
5078 e->value.function.actual = NULL;
5079 e->expr_type = EXPR_FUNCTION;
5080 e->symtree = st;
5082 /* Ambiguity will not arise if the array reference is not
5083 the last reference. */
5084 for (ref = e->ref; ref; ref = ref->next)
5085 if (ref->type == REF_ARRAY && ref->next == NULL)
5086 break;
5088 gcc_assert (ref->type == REF_ARRAY);
5090 /* Grab the start expressions from the array ref and
5091 copy them into actual arguments. */
5092 for (n = 0; n < ref->u.ar.dimen; n++)
5094 arg = gfc_get_actual_arglist ();
5095 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5096 if (e->value.function.actual == NULL)
5097 tail = e->value.function.actual = arg;
5098 else
5100 tail->next = arg;
5101 tail = arg;
5105 /* Dump the reference list and set the rank. */
5106 gfc_free_ref_list (e->ref);
5107 e->ref = NULL;
5108 e->rank = sym->as ? sym->as->rank : 0;
5111 gfc_resolve_expr (e);
5112 sym->refs++;
5115 /* This might have changed! */
5116 return e->expr_type == EXPR_FUNCTION;
5120 static void
5121 gfc_resolve_character_operator (gfc_expr *e)
5123 gfc_expr *op1 = e->value.op.op1;
5124 gfc_expr *op2 = e->value.op.op2;
5125 gfc_expr *e1 = NULL;
5126 gfc_expr *e2 = NULL;
5128 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5130 if (op1->ts.u.cl && op1->ts.u.cl->length)
5131 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5132 else if (op1->expr_type == EXPR_CONSTANT)
5133 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5134 op1->value.character.length);
5136 if (op2->ts.u.cl && op2->ts.u.cl->length)
5137 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5138 else if (op2->expr_type == EXPR_CONSTANT)
5139 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5140 op2->value.character.length);
5142 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5144 if (!e1 || !e2)
5146 gfc_free_expr (e1);
5147 gfc_free_expr (e2);
5149 return;
5152 e->ts.u.cl->length = gfc_add (e1, e2);
5153 e->ts.u.cl->length->ts.type = BT_INTEGER;
5154 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5155 gfc_simplify_expr (e->ts.u.cl->length, 0);
5156 gfc_resolve_expr (e->ts.u.cl->length);
5158 return;
5162 /* Ensure that an character expression has a charlen and, if possible, a
5163 length expression. */
5165 static void
5166 fixup_charlen (gfc_expr *e)
5168 /* The cases fall through so that changes in expression type and the need
5169 for multiple fixes are picked up. In all circumstances, a charlen should
5170 be available for the middle end to hang a backend_decl on. */
5171 switch (e->expr_type)
5173 case EXPR_OP:
5174 gfc_resolve_character_operator (e);
5176 case EXPR_ARRAY:
5177 if (e->expr_type == EXPR_ARRAY)
5178 gfc_resolve_character_array_constructor (e);
5180 case EXPR_SUBSTRING:
5181 if (!e->ts.u.cl && e->ref)
5182 gfc_resolve_substring_charlen (e);
5184 default:
5185 if (!e->ts.u.cl)
5186 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5188 break;
5193 /* Update an actual argument to include the passed-object for type-bound
5194 procedures at the right position. */
5196 static gfc_actual_arglist*
5197 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5198 const char *name)
5200 gcc_assert (argpos > 0);
5202 if (argpos == 1)
5204 gfc_actual_arglist* result;
5206 result = gfc_get_actual_arglist ();
5207 result->expr = po;
5208 result->next = lst;
5209 if (name)
5210 result->name = name;
5212 return result;
5215 if (lst)
5216 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5217 else
5218 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5219 return lst;
5223 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5225 static gfc_expr*
5226 extract_compcall_passed_object (gfc_expr* e)
5228 gfc_expr* po;
5230 gcc_assert (e->expr_type == EXPR_COMPCALL);
5232 if (e->value.compcall.base_object)
5233 po = gfc_copy_expr (e->value.compcall.base_object);
5234 else
5236 po = gfc_get_expr ();
5237 po->expr_type = EXPR_VARIABLE;
5238 po->symtree = e->symtree;
5239 po->ref = gfc_copy_ref (e->ref);
5240 po->where = e->where;
5243 if (!gfc_resolve_expr (po))
5244 return NULL;
5246 return po;
5250 /* Update the arglist of an EXPR_COMPCALL expression to include the
5251 passed-object. */
5253 static bool
5254 update_compcall_arglist (gfc_expr* e)
5256 gfc_expr* po;
5257 gfc_typebound_proc* tbp;
5259 tbp = e->value.compcall.tbp;
5261 if (tbp->error)
5262 return false;
5264 po = extract_compcall_passed_object (e);
5265 if (!po)
5266 return false;
5268 if (tbp->nopass || e->value.compcall.ignore_pass)
5270 gfc_free_expr (po);
5271 return true;
5274 gcc_assert (tbp->pass_arg_num > 0);
5275 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5276 tbp->pass_arg_num,
5277 tbp->pass_arg);
5279 return true;
5283 /* Extract the passed object from a PPC call (a copy of it). */
5285 static gfc_expr*
5286 extract_ppc_passed_object (gfc_expr *e)
5288 gfc_expr *po;
5289 gfc_ref **ref;
5291 po = gfc_get_expr ();
5292 po->expr_type = EXPR_VARIABLE;
5293 po->symtree = e->symtree;
5294 po->ref = gfc_copy_ref (e->ref);
5295 po->where = e->where;
5297 /* Remove PPC reference. */
5298 ref = &po->ref;
5299 while ((*ref)->next)
5300 ref = &(*ref)->next;
5301 gfc_free_ref_list (*ref);
5302 *ref = NULL;
5304 if (!gfc_resolve_expr (po))
5305 return NULL;
5307 return po;
5311 /* Update the actual arglist of a procedure pointer component to include the
5312 passed-object. */
5314 static bool
5315 update_ppc_arglist (gfc_expr* e)
5317 gfc_expr* po;
5318 gfc_component *ppc;
5319 gfc_typebound_proc* tb;
5321 ppc = gfc_get_proc_ptr_comp (e);
5322 if (!ppc)
5323 return false;
5325 tb = ppc->tb;
5327 if (tb->error)
5328 return false;
5329 else if (tb->nopass)
5330 return true;
5332 po = extract_ppc_passed_object (e);
5333 if (!po)
5334 return false;
5336 /* F08:R739. */
5337 if (po->rank != 0)
5339 gfc_error ("Passed-object at %L must be scalar", &e->where);
5340 return false;
5343 /* F08:C611. */
5344 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5346 gfc_error ("Base object for procedure-pointer component call at %L is of"
5347 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5348 return false;
5351 gcc_assert (tb->pass_arg_num > 0);
5352 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5353 tb->pass_arg_num,
5354 tb->pass_arg);
5356 return true;
5360 /* Check that the object a TBP is called on is valid, i.e. it must not be
5361 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5363 static bool
5364 check_typebound_baseobject (gfc_expr* e)
5366 gfc_expr* base;
5367 bool return_value = false;
5369 base = extract_compcall_passed_object (e);
5370 if (!base)
5371 return false;
5373 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5375 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5376 return false;
5378 /* F08:C611. */
5379 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5381 gfc_error ("Base object for type-bound procedure call at %L is of"
5382 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5383 goto cleanup;
5386 /* F08:C1230. If the procedure called is NOPASS,
5387 the base object must be scalar. */
5388 if (e->value.compcall.tbp->nopass && base->rank != 0)
5390 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5391 " be scalar", &e->where);
5392 goto cleanup;
5395 return_value = true;
5397 cleanup:
5398 gfc_free_expr (base);
5399 return return_value;
5403 /* Resolve a call to a type-bound procedure, either function or subroutine,
5404 statically from the data in an EXPR_COMPCALL expression. The adapted
5405 arglist and the target-procedure symtree are returned. */
5407 static bool
5408 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5409 gfc_actual_arglist** actual)
5411 gcc_assert (e->expr_type == EXPR_COMPCALL);
5412 gcc_assert (!e->value.compcall.tbp->is_generic);
5414 /* Update the actual arglist for PASS. */
5415 if (!update_compcall_arglist (e))
5416 return false;
5418 *actual = e->value.compcall.actual;
5419 *target = e->value.compcall.tbp->u.specific;
5421 gfc_free_ref_list (e->ref);
5422 e->ref = NULL;
5423 e->value.compcall.actual = NULL;
5425 /* If we find a deferred typebound procedure, check for derived types
5426 that an overriding typebound procedure has not been missed. */
5427 if (e->value.compcall.name
5428 && !e->value.compcall.tbp->non_overridable
5429 && e->value.compcall.base_object
5430 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5432 gfc_symtree *st;
5433 gfc_symbol *derived;
5435 /* Use the derived type of the base_object. */
5436 derived = e->value.compcall.base_object->ts.u.derived;
5437 st = NULL;
5439 /* If necessary, go through the inheritance chain. */
5440 while (!st && derived)
5442 /* Look for the typebound procedure 'name'. */
5443 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5444 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5445 e->value.compcall.name);
5446 if (!st)
5447 derived = gfc_get_derived_super_type (derived);
5450 /* Now find the specific name in the derived type namespace. */
5451 if (st && st->n.tb && st->n.tb->u.specific)
5452 gfc_find_sym_tree (st->n.tb->u.specific->name,
5453 derived->ns, 1, &st);
5454 if (st)
5455 *target = st;
5457 return true;
5461 /* Get the ultimate declared type from an expression. In addition,
5462 return the last class/derived type reference and the copy of the
5463 reference list. If check_types is set true, derived types are
5464 identified as well as class references. */
5465 static gfc_symbol*
5466 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5467 gfc_expr *e, bool check_types)
5469 gfc_symbol *declared;
5470 gfc_ref *ref;
5472 declared = NULL;
5473 if (class_ref)
5474 *class_ref = NULL;
5475 if (new_ref)
5476 *new_ref = gfc_copy_ref (e->ref);
5478 for (ref = e->ref; ref; ref = ref->next)
5480 if (ref->type != REF_COMPONENT)
5481 continue;
5483 if ((ref->u.c.component->ts.type == BT_CLASS
5484 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5485 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5487 declared = ref->u.c.component->ts.u.derived;
5488 if (class_ref)
5489 *class_ref = ref;
5493 if (declared == NULL)
5494 declared = e->symtree->n.sym->ts.u.derived;
5496 return declared;
5500 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5501 which of the specific bindings (if any) matches the arglist and transform
5502 the expression into a call of that binding. */
5504 static bool
5505 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5507 gfc_typebound_proc* genproc;
5508 const char* genname;
5509 gfc_symtree *st;
5510 gfc_symbol *derived;
5512 gcc_assert (e->expr_type == EXPR_COMPCALL);
5513 genname = e->value.compcall.name;
5514 genproc = e->value.compcall.tbp;
5516 if (!genproc->is_generic)
5517 return true;
5519 /* Try the bindings on this type and in the inheritance hierarchy. */
5520 for (; genproc; genproc = genproc->overridden)
5522 gfc_tbp_generic* g;
5524 gcc_assert (genproc->is_generic);
5525 for (g = genproc->u.generic; g; g = g->next)
5527 gfc_symbol* target;
5528 gfc_actual_arglist* args;
5529 bool matches;
5531 gcc_assert (g->specific);
5533 if (g->specific->error)
5534 continue;
5536 target = g->specific->u.specific->n.sym;
5538 /* Get the right arglist by handling PASS/NOPASS. */
5539 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5540 if (!g->specific->nopass)
5542 gfc_expr* po;
5543 po = extract_compcall_passed_object (e);
5544 if (!po)
5546 gfc_free_actual_arglist (args);
5547 return false;
5550 gcc_assert (g->specific->pass_arg_num > 0);
5551 gcc_assert (!g->specific->error);
5552 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5553 g->specific->pass_arg);
5555 resolve_actual_arglist (args, target->attr.proc,
5556 is_external_proc (target)
5557 && gfc_sym_get_dummy_args (target) == NULL);
5559 /* Check if this arglist matches the formal. */
5560 matches = gfc_arglist_matches_symbol (&args, target);
5562 /* Clean up and break out of the loop if we've found it. */
5563 gfc_free_actual_arglist (args);
5564 if (matches)
5566 e->value.compcall.tbp = g->specific;
5567 genname = g->specific_st->name;
5568 /* Pass along the name for CLASS methods, where the vtab
5569 procedure pointer component has to be referenced. */
5570 if (name)
5571 *name = genname;
5572 goto success;
5577 /* Nothing matching found! */
5578 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5579 " '%s' at %L", genname, &e->where);
5580 return false;
5582 success:
5583 /* Make sure that we have the right specific instance for the name. */
5584 derived = get_declared_from_expr (NULL, NULL, e, true);
5586 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5587 if (st)
5588 e->value.compcall.tbp = st->n.tb;
5590 return true;
5594 /* Resolve a call to a type-bound subroutine. */
5596 static bool
5597 resolve_typebound_call (gfc_code* c, const char **name)
5599 gfc_actual_arglist* newactual;
5600 gfc_symtree* target;
5602 /* Check that's really a SUBROUTINE. */
5603 if (!c->expr1->value.compcall.tbp->subroutine)
5605 gfc_error ("'%s' at %L should be a SUBROUTINE",
5606 c->expr1->value.compcall.name, &c->loc);
5607 return false;
5610 if (!check_typebound_baseobject (c->expr1))
5611 return false;
5613 /* Pass along the name for CLASS methods, where the vtab
5614 procedure pointer component has to be referenced. */
5615 if (name)
5616 *name = c->expr1->value.compcall.name;
5618 if (!resolve_typebound_generic_call (c->expr1, name))
5619 return false;
5621 /* Transform into an ordinary EXEC_CALL for now. */
5623 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5624 return false;
5626 c->ext.actual = newactual;
5627 c->symtree = target;
5628 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5630 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5632 gfc_free_expr (c->expr1);
5633 c->expr1 = gfc_get_expr ();
5634 c->expr1->expr_type = EXPR_FUNCTION;
5635 c->expr1->symtree = target;
5636 c->expr1->where = c->loc;
5638 return resolve_call (c);
5642 /* Resolve a component-call expression. */
5643 static bool
5644 resolve_compcall (gfc_expr* e, const char **name)
5646 gfc_actual_arglist* newactual;
5647 gfc_symtree* target;
5649 /* Check that's really a FUNCTION. */
5650 if (!e->value.compcall.tbp->function)
5652 gfc_error ("'%s' at %L should be a FUNCTION",
5653 e->value.compcall.name, &e->where);
5654 return false;
5657 /* These must not be assign-calls! */
5658 gcc_assert (!e->value.compcall.assign);
5660 if (!check_typebound_baseobject (e))
5661 return false;
5663 /* Pass along the name for CLASS methods, where the vtab
5664 procedure pointer component has to be referenced. */
5665 if (name)
5666 *name = e->value.compcall.name;
5668 if (!resolve_typebound_generic_call (e, name))
5669 return false;
5670 gcc_assert (!e->value.compcall.tbp->is_generic);
5672 /* Take the rank from the function's symbol. */
5673 if (e->value.compcall.tbp->u.specific->n.sym->as)
5674 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5676 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5677 arglist to the TBP's binding target. */
5679 if (!resolve_typebound_static (e, &target, &newactual))
5680 return false;
5682 e->value.function.actual = newactual;
5683 e->value.function.name = NULL;
5684 e->value.function.esym = target->n.sym;
5685 e->value.function.isym = NULL;
5686 e->symtree = target;
5687 e->ts = target->n.sym->ts;
5688 e->expr_type = EXPR_FUNCTION;
5690 /* Resolution is not necessary if this is a class subroutine; this
5691 function only has to identify the specific proc. Resolution of
5692 the call will be done next in resolve_typebound_call. */
5693 return gfc_resolve_expr (e);
5697 static bool resolve_fl_derived (gfc_symbol *sym);
5700 /* Resolve a typebound function, or 'method'. First separate all
5701 the non-CLASS references by calling resolve_compcall directly. */
5703 static bool
5704 resolve_typebound_function (gfc_expr* e)
5706 gfc_symbol *declared;
5707 gfc_component *c;
5708 gfc_ref *new_ref;
5709 gfc_ref *class_ref;
5710 gfc_symtree *st;
5711 const char *name;
5712 gfc_typespec ts;
5713 gfc_expr *expr;
5714 bool overridable;
5716 st = e->symtree;
5718 /* Deal with typebound operators for CLASS objects. */
5719 expr = e->value.compcall.base_object;
5720 overridable = !e->value.compcall.tbp->non_overridable;
5721 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5723 /* If the base_object is not a variable, the corresponding actual
5724 argument expression must be stored in e->base_expression so
5725 that the corresponding tree temporary can be used as the base
5726 object in gfc_conv_procedure_call. */
5727 if (expr->expr_type != EXPR_VARIABLE)
5729 gfc_actual_arglist *args;
5731 for (args= e->value.function.actual; args; args = args->next)
5733 if (expr == args->expr)
5734 expr = args->expr;
5738 /* Since the typebound operators are generic, we have to ensure
5739 that any delays in resolution are corrected and that the vtab
5740 is present. */
5741 ts = expr->ts;
5742 declared = ts.u.derived;
5743 c = gfc_find_component (declared, "_vptr", true, true);
5744 if (c->ts.u.derived == NULL)
5745 c->ts.u.derived = gfc_find_derived_vtab (declared);
5747 if (!resolve_compcall (e, &name))
5748 return false;
5750 /* Use the generic name if it is there. */
5751 name = name ? name : e->value.function.esym->name;
5752 e->symtree = expr->symtree;
5753 e->ref = gfc_copy_ref (expr->ref);
5754 get_declared_from_expr (&class_ref, NULL, e, false);
5756 /* Trim away the extraneous references that emerge from nested
5757 use of interface.c (extend_expr). */
5758 if (class_ref && class_ref->next)
5760 gfc_free_ref_list (class_ref->next);
5761 class_ref->next = NULL;
5763 else if (e->ref && !class_ref)
5765 gfc_free_ref_list (e->ref);
5766 e->ref = NULL;
5769 gfc_add_vptr_component (e);
5770 gfc_add_component_ref (e, name);
5771 e->value.function.esym = NULL;
5772 if (expr->expr_type != EXPR_VARIABLE)
5773 e->base_expr = expr;
5774 return true;
5777 if (st == NULL)
5778 return resolve_compcall (e, NULL);
5780 if (!resolve_ref (e))
5781 return false;
5783 /* Get the CLASS declared type. */
5784 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5786 if (!resolve_fl_derived (declared))
5787 return false;
5789 /* Weed out cases of the ultimate component being a derived type. */
5790 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5791 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5793 gfc_free_ref_list (new_ref);
5794 return resolve_compcall (e, NULL);
5797 c = gfc_find_component (declared, "_data", true, true);
5798 declared = c->ts.u.derived;
5800 /* Treat the call as if it is a typebound procedure, in order to roll
5801 out the correct name for the specific function. */
5802 if (!resolve_compcall (e, &name))
5804 gfc_free_ref_list (new_ref);
5805 return false;
5807 ts = e->ts;
5809 if (overridable)
5811 /* Convert the expression to a procedure pointer component call. */
5812 e->value.function.esym = NULL;
5813 e->symtree = st;
5815 if (new_ref)
5816 e->ref = new_ref;
5818 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5819 gfc_add_vptr_component (e);
5820 gfc_add_component_ref (e, name);
5822 /* Recover the typespec for the expression. This is really only
5823 necessary for generic procedures, where the additional call
5824 to gfc_add_component_ref seems to throw the collection of the
5825 correct typespec. */
5826 e->ts = ts;
5828 else if (new_ref)
5829 gfc_free_ref_list (new_ref);
5831 return true;
5834 /* Resolve a typebound subroutine, or 'method'. First separate all
5835 the non-CLASS references by calling resolve_typebound_call
5836 directly. */
5838 static bool
5839 resolve_typebound_subroutine (gfc_code *code)
5841 gfc_symbol *declared;
5842 gfc_component *c;
5843 gfc_ref *new_ref;
5844 gfc_ref *class_ref;
5845 gfc_symtree *st;
5846 const char *name;
5847 gfc_typespec ts;
5848 gfc_expr *expr;
5849 bool overridable;
5851 st = code->expr1->symtree;
5853 /* Deal with typebound operators for CLASS objects. */
5854 expr = code->expr1->value.compcall.base_object;
5855 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5856 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5858 /* If the base_object is not a variable, the corresponding actual
5859 argument expression must be stored in e->base_expression so
5860 that the corresponding tree temporary can be used as the base
5861 object in gfc_conv_procedure_call. */
5862 if (expr->expr_type != EXPR_VARIABLE)
5864 gfc_actual_arglist *args;
5866 args= code->expr1->value.function.actual;
5867 for (; args; args = args->next)
5868 if (expr == args->expr)
5869 expr = args->expr;
5872 /* Since the typebound operators are generic, we have to ensure
5873 that any delays in resolution are corrected and that the vtab
5874 is present. */
5875 declared = expr->ts.u.derived;
5876 c = gfc_find_component (declared, "_vptr", true, true);
5877 if (c->ts.u.derived == NULL)
5878 c->ts.u.derived = gfc_find_derived_vtab (declared);
5880 if (!resolve_typebound_call (code, &name))
5881 return false;
5883 /* Use the generic name if it is there. */
5884 name = name ? name : code->expr1->value.function.esym->name;
5885 code->expr1->symtree = expr->symtree;
5886 code->expr1->ref = gfc_copy_ref (expr->ref);
5888 /* Trim away the extraneous references that emerge from nested
5889 use of interface.c (extend_expr). */
5890 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5891 if (class_ref && class_ref->next)
5893 gfc_free_ref_list (class_ref->next);
5894 class_ref->next = NULL;
5896 else if (code->expr1->ref && !class_ref)
5898 gfc_free_ref_list (code->expr1->ref);
5899 code->expr1->ref = NULL;
5902 /* Now use the procedure in the vtable. */
5903 gfc_add_vptr_component (code->expr1);
5904 gfc_add_component_ref (code->expr1, name);
5905 code->expr1->value.function.esym = NULL;
5906 if (expr->expr_type != EXPR_VARIABLE)
5907 code->expr1->base_expr = expr;
5908 return true;
5911 if (st == NULL)
5912 return resolve_typebound_call (code, NULL);
5914 if (!resolve_ref (code->expr1))
5915 return false;
5917 /* Get the CLASS declared type. */
5918 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5920 /* Weed out cases of the ultimate component being a derived type. */
5921 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5922 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5924 gfc_free_ref_list (new_ref);
5925 return resolve_typebound_call (code, NULL);
5928 if (!resolve_typebound_call (code, &name))
5930 gfc_free_ref_list (new_ref);
5931 return false;
5933 ts = code->expr1->ts;
5935 if (overridable)
5937 /* Convert the expression to a procedure pointer component call. */
5938 code->expr1->value.function.esym = NULL;
5939 code->expr1->symtree = st;
5941 if (new_ref)
5942 code->expr1->ref = new_ref;
5944 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5945 gfc_add_vptr_component (code->expr1);
5946 gfc_add_component_ref (code->expr1, name);
5948 /* Recover the typespec for the expression. This is really only
5949 necessary for generic procedures, where the additional call
5950 to gfc_add_component_ref seems to throw the collection of the
5951 correct typespec. */
5952 code->expr1->ts = ts;
5954 else if (new_ref)
5955 gfc_free_ref_list (new_ref);
5957 return true;
5961 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5963 static bool
5964 resolve_ppc_call (gfc_code* c)
5966 gfc_component *comp;
5968 comp = gfc_get_proc_ptr_comp (c->expr1);
5969 gcc_assert (comp != NULL);
5971 c->resolved_sym = c->expr1->symtree->n.sym;
5972 c->expr1->expr_type = EXPR_VARIABLE;
5974 if (!comp->attr.subroutine)
5975 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5977 if (!resolve_ref (c->expr1))
5978 return false;
5980 if (!update_ppc_arglist (c->expr1))
5981 return false;
5983 c->ext.actual = c->expr1->value.compcall.actual;
5985 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5986 !(comp->ts.interface
5987 && comp->ts.interface->formal)))
5988 return false;
5990 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5992 return true;
5996 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5998 static bool
5999 resolve_expr_ppc (gfc_expr* e)
6001 gfc_component *comp;
6003 comp = gfc_get_proc_ptr_comp (e);
6004 gcc_assert (comp != NULL);
6006 /* Convert to EXPR_FUNCTION. */
6007 e->expr_type = EXPR_FUNCTION;
6008 e->value.function.isym = NULL;
6009 e->value.function.actual = e->value.compcall.actual;
6010 e->ts = comp->ts;
6011 if (comp->as != NULL)
6012 e->rank = comp->as->rank;
6014 if (!comp->attr.function)
6015 gfc_add_function (&comp->attr, comp->name, &e->where);
6017 if (!resolve_ref (e))
6018 return false;
6020 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6021 !(comp->ts.interface
6022 && comp->ts.interface->formal)))
6023 return false;
6025 if (!update_ppc_arglist (e))
6026 return false;
6028 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6030 return true;
6034 static bool
6035 gfc_is_expandable_expr (gfc_expr *e)
6037 gfc_constructor *con;
6039 if (e->expr_type == EXPR_ARRAY)
6041 /* Traverse the constructor looking for variables that are flavor
6042 parameter. Parameters must be expanded since they are fully used at
6043 compile time. */
6044 con = gfc_constructor_first (e->value.constructor);
6045 for (; con; con = gfc_constructor_next (con))
6047 if (con->expr->expr_type == EXPR_VARIABLE
6048 && con->expr->symtree
6049 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6050 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6051 return true;
6052 if (con->expr->expr_type == EXPR_ARRAY
6053 && gfc_is_expandable_expr (con->expr))
6054 return true;
6058 return false;
6061 /* Resolve an expression. That is, make sure that types of operands agree
6062 with their operators, intrinsic operators are converted to function calls
6063 for overloaded types and unresolved function references are resolved. */
6065 bool
6066 gfc_resolve_expr (gfc_expr *e)
6068 bool t;
6069 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6071 if (e == NULL)
6072 return true;
6074 /* inquiry_argument only applies to variables. */
6075 inquiry_save = inquiry_argument;
6076 actual_arg_save = actual_arg;
6077 first_actual_arg_save = first_actual_arg;
6079 if (e->expr_type != EXPR_VARIABLE)
6081 inquiry_argument = false;
6082 actual_arg = false;
6083 first_actual_arg = false;
6086 switch (e->expr_type)
6088 case EXPR_OP:
6089 t = resolve_operator (e);
6090 break;
6092 case EXPR_FUNCTION:
6093 case EXPR_VARIABLE:
6095 if (check_host_association (e))
6096 t = resolve_function (e);
6097 else
6099 t = resolve_variable (e);
6100 if (t)
6101 expression_rank (e);
6104 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6105 && e->ref->type != REF_SUBSTRING)
6106 gfc_resolve_substring_charlen (e);
6108 break;
6110 case EXPR_COMPCALL:
6111 t = resolve_typebound_function (e);
6112 break;
6114 case EXPR_SUBSTRING:
6115 t = resolve_ref (e);
6116 break;
6118 case EXPR_CONSTANT:
6119 case EXPR_NULL:
6120 t = true;
6121 break;
6123 case EXPR_PPC:
6124 t = resolve_expr_ppc (e);
6125 break;
6127 case EXPR_ARRAY:
6128 t = false;
6129 if (!resolve_ref (e))
6130 break;
6132 t = gfc_resolve_array_constructor (e);
6133 /* Also try to expand a constructor. */
6134 if (t)
6136 expression_rank (e);
6137 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6138 gfc_expand_constructor (e, false);
6141 /* This provides the opportunity for the length of constructors with
6142 character valued function elements to propagate the string length
6143 to the expression. */
6144 if (t && e->ts.type == BT_CHARACTER)
6146 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6147 here rather then add a duplicate test for it above. */
6148 gfc_expand_constructor (e, false);
6149 t = gfc_resolve_character_array_constructor (e);
6152 break;
6154 case EXPR_STRUCTURE:
6155 t = resolve_ref (e);
6156 if (!t)
6157 break;
6159 t = resolve_structure_cons (e, 0);
6160 if (!t)
6161 break;
6163 t = gfc_simplify_expr (e, 0);
6164 break;
6166 default:
6167 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6170 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6171 fixup_charlen (e);
6173 inquiry_argument = inquiry_save;
6174 actual_arg = actual_arg_save;
6175 first_actual_arg = first_actual_arg_save;
6177 return t;
6181 /* Resolve an expression from an iterator. They must be scalar and have
6182 INTEGER or (optionally) REAL type. */
6184 static bool
6185 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6186 const char *name_msgid)
6188 if (!gfc_resolve_expr (expr))
6189 return false;
6191 if (expr->rank != 0)
6193 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6194 return false;
6197 if (expr->ts.type != BT_INTEGER)
6199 if (expr->ts.type == BT_REAL)
6201 if (real_ok)
6202 return gfc_notify_std (GFC_STD_F95_DEL,
6203 "%s at %L must be integer",
6204 _(name_msgid), &expr->where);
6205 else
6207 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6208 &expr->where);
6209 return false;
6212 else
6214 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6215 return false;
6218 return true;
6222 /* Resolve the expressions in an iterator structure. If REAL_OK is
6223 false allow only INTEGER type iterators, otherwise allow REAL types.
6224 Set own_scope to true for ac-implied-do and data-implied-do as those
6225 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6227 bool
6228 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6230 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6231 return false;
6233 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6234 _("iterator variable")))
6235 return false;
6237 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6238 "Start expression in DO loop"))
6239 return false;
6241 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6242 "End expression in DO loop"))
6243 return false;
6245 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6246 "Step expression in DO loop"))
6247 return false;
6249 if (iter->step->expr_type == EXPR_CONSTANT)
6251 if ((iter->step->ts.type == BT_INTEGER
6252 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6253 || (iter->step->ts.type == BT_REAL
6254 && mpfr_sgn (iter->step->value.real) == 0))
6256 gfc_error ("Step expression in DO loop at %L cannot be zero",
6257 &iter->step->where);
6258 return false;
6262 /* Convert start, end, and step to the same type as var. */
6263 if (iter->start->ts.kind != iter->var->ts.kind
6264 || iter->start->ts.type != iter->var->ts.type)
6265 gfc_convert_type (iter->start, &iter->var->ts, 2);
6267 if (iter->end->ts.kind != iter->var->ts.kind
6268 || iter->end->ts.type != iter->var->ts.type)
6269 gfc_convert_type (iter->end, &iter->var->ts, 2);
6271 if (iter->step->ts.kind != iter->var->ts.kind
6272 || iter->step->ts.type != iter->var->ts.type)
6273 gfc_convert_type (iter->step, &iter->var->ts, 2);
6275 if (iter->start->expr_type == EXPR_CONSTANT
6276 && iter->end->expr_type == EXPR_CONSTANT
6277 && iter->step->expr_type == EXPR_CONSTANT)
6279 int sgn, cmp;
6280 if (iter->start->ts.type == BT_INTEGER)
6282 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6283 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6285 else
6287 sgn = mpfr_sgn (iter->step->value.real);
6288 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6290 if (gfc_option.warn_zerotrip &&
6291 ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6292 gfc_warning ("DO loop at %L will be executed zero times"
6293 " (use -Wno-zerotrip to suppress)",
6294 &iter->step->where);
6297 return true;
6301 /* Traversal function for find_forall_index. f == 2 signals that
6302 that variable itself is not to be checked - only the references. */
6304 static bool
6305 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6307 if (expr->expr_type != EXPR_VARIABLE)
6308 return false;
6310 /* A scalar assignment */
6311 if (!expr->ref || *f == 1)
6313 if (expr->symtree->n.sym == sym)
6314 return true;
6315 else
6316 return false;
6319 if (*f == 2)
6320 *f = 1;
6321 return false;
6325 /* Check whether the FORALL index appears in the expression or not.
6326 Returns true if SYM is found in EXPR. */
6328 bool
6329 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6331 if (gfc_traverse_expr (expr, sym, forall_index, f))
6332 return true;
6333 else
6334 return false;
6338 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6339 to be a scalar INTEGER variable. The subscripts and stride are scalar
6340 INTEGERs, and if stride is a constant it must be nonzero.
6341 Furthermore "A subscript or stride in a forall-triplet-spec shall
6342 not contain a reference to any index-name in the
6343 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6345 static void
6346 resolve_forall_iterators (gfc_forall_iterator *it)
6348 gfc_forall_iterator *iter, *iter2;
6350 for (iter = it; iter; iter = iter->next)
6352 if (gfc_resolve_expr (iter->var)
6353 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6354 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6355 &iter->var->where);
6357 if (gfc_resolve_expr (iter->start)
6358 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6359 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6360 &iter->start->where);
6361 if (iter->var->ts.kind != iter->start->ts.kind)
6362 gfc_convert_type (iter->start, &iter->var->ts, 1);
6364 if (gfc_resolve_expr (iter->end)
6365 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6366 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6367 &iter->end->where);
6368 if (iter->var->ts.kind != iter->end->ts.kind)
6369 gfc_convert_type (iter->end, &iter->var->ts, 1);
6371 if (gfc_resolve_expr (iter->stride))
6373 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6374 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6375 &iter->stride->where, "INTEGER");
6377 if (iter->stride->expr_type == EXPR_CONSTANT
6378 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6379 gfc_error ("FORALL stride expression at %L cannot be zero",
6380 &iter->stride->where);
6382 if (iter->var->ts.kind != iter->stride->ts.kind)
6383 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6386 for (iter = it; iter; iter = iter->next)
6387 for (iter2 = iter; iter2; iter2 = iter2->next)
6389 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6390 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6391 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6392 gfc_error ("FORALL index '%s' may not appear in triplet "
6393 "specification at %L", iter->var->symtree->name,
6394 &iter2->start->where);
6399 /* Given a pointer to a symbol that is a derived type, see if it's
6400 inaccessible, i.e. if it's defined in another module and the components are
6401 PRIVATE. The search is recursive if necessary. Returns zero if no
6402 inaccessible components are found, nonzero otherwise. */
6404 static int
6405 derived_inaccessible (gfc_symbol *sym)
6407 gfc_component *c;
6409 if (sym->attr.use_assoc && sym->attr.private_comp)
6410 return 1;
6412 for (c = sym->components; c; c = c->next)
6414 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6415 return 1;
6418 return 0;
6422 /* Resolve the argument of a deallocate expression. The expression must be
6423 a pointer or a full array. */
6425 static bool
6426 resolve_deallocate_expr (gfc_expr *e)
6428 symbol_attribute attr;
6429 int allocatable, pointer;
6430 gfc_ref *ref;
6431 gfc_symbol *sym;
6432 gfc_component *c;
6433 bool unlimited;
6435 if (!gfc_resolve_expr (e))
6436 return false;
6438 if (e->expr_type != EXPR_VARIABLE)
6439 goto bad;
6441 sym = e->symtree->n.sym;
6442 unlimited = UNLIMITED_POLY(sym);
6444 if (sym->ts.type == BT_CLASS)
6446 allocatable = CLASS_DATA (sym)->attr.allocatable;
6447 pointer = CLASS_DATA (sym)->attr.class_pointer;
6449 else
6451 allocatable = sym->attr.allocatable;
6452 pointer = sym->attr.pointer;
6454 for (ref = e->ref; ref; ref = ref->next)
6456 switch (ref->type)
6458 case REF_ARRAY:
6459 if (ref->u.ar.type != AR_FULL
6460 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6461 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6462 allocatable = 0;
6463 break;
6465 case REF_COMPONENT:
6466 c = ref->u.c.component;
6467 if (c->ts.type == BT_CLASS)
6469 allocatable = CLASS_DATA (c)->attr.allocatable;
6470 pointer = CLASS_DATA (c)->attr.class_pointer;
6472 else
6474 allocatable = c->attr.allocatable;
6475 pointer = c->attr.pointer;
6477 break;
6479 case REF_SUBSTRING:
6480 allocatable = 0;
6481 break;
6485 attr = gfc_expr_attr (e);
6487 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6489 bad:
6490 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6491 &e->where);
6492 return false;
6495 /* F2008, C644. */
6496 if (gfc_is_coindexed (e))
6498 gfc_error ("Coindexed allocatable object at %L", &e->where);
6499 return false;
6502 if (pointer
6503 && !gfc_check_vardef_context (e, true, true, false,
6504 _("DEALLOCATE object")))
6505 return false;
6506 if (!gfc_check_vardef_context (e, false, true, false,
6507 _("DEALLOCATE object")))
6508 return false;
6510 return true;
6514 /* Returns true if the expression e contains a reference to the symbol sym. */
6515 static bool
6516 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6518 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6519 return true;
6521 return false;
6524 bool
6525 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6527 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6531 /* Given the expression node e for an allocatable/pointer of derived type to be
6532 allocated, get the expression node to be initialized afterwards (needed for
6533 derived types with default initializers, and derived types with allocatable
6534 components that need nullification.) */
6536 gfc_expr *
6537 gfc_expr_to_initialize (gfc_expr *e)
6539 gfc_expr *result;
6540 gfc_ref *ref;
6541 int i;
6543 result = gfc_copy_expr (e);
6545 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6546 for (ref = result->ref; ref; ref = ref->next)
6547 if (ref->type == REF_ARRAY && ref->next == NULL)
6549 ref->u.ar.type = AR_FULL;
6551 for (i = 0; i < ref->u.ar.dimen; i++)
6552 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6554 break;
6557 gfc_free_shape (&result->shape, result->rank);
6559 /* Recalculate rank, shape, etc. */
6560 gfc_resolve_expr (result);
6561 return result;
6565 /* If the last ref of an expression is an array ref, return a copy of the
6566 expression with that one removed. Otherwise, a copy of the original
6567 expression. This is used for allocate-expressions and pointer assignment
6568 LHS, where there may be an array specification that needs to be stripped
6569 off when using gfc_check_vardef_context. */
6571 static gfc_expr*
6572 remove_last_array_ref (gfc_expr* e)
6574 gfc_expr* e2;
6575 gfc_ref** r;
6577 e2 = gfc_copy_expr (e);
6578 for (r = &e2->ref; *r; r = &(*r)->next)
6579 if ((*r)->type == REF_ARRAY && !(*r)->next)
6581 gfc_free_ref_list (*r);
6582 *r = NULL;
6583 break;
6586 return e2;
6590 /* Used in resolve_allocate_expr to check that a allocation-object and
6591 a source-expr are conformable. This does not catch all possible
6592 cases; in particular a runtime checking is needed. */
6594 static bool
6595 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6597 gfc_ref *tail;
6598 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6600 /* First compare rank. */
6601 if ((tail && e1->rank != tail->u.ar.as->rank)
6602 || (!tail && e1->rank != e2->rank))
6604 gfc_error ("Source-expr at %L must be scalar or have the "
6605 "same rank as the allocate-object at %L",
6606 &e1->where, &e2->where);
6607 return false;
6610 if (e1->shape)
6612 int i;
6613 mpz_t s;
6615 mpz_init (s);
6617 for (i = 0; i < e1->rank; i++)
6619 if (tail->u.ar.start[i] == NULL)
6620 break;
6622 if (tail->u.ar.end[i])
6624 mpz_set (s, tail->u.ar.end[i]->value.integer);
6625 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6626 mpz_add_ui (s, s, 1);
6628 else
6630 mpz_set (s, tail->u.ar.start[i]->value.integer);
6633 if (mpz_cmp (e1->shape[i], s) != 0)
6635 gfc_error ("Source-expr at %L and allocate-object at %L must "
6636 "have the same shape", &e1->where, &e2->where);
6637 mpz_clear (s);
6638 return false;
6642 mpz_clear (s);
6645 return true;
6649 /* Resolve the expression in an ALLOCATE statement, doing the additional
6650 checks to see whether the expression is OK or not. The expression must
6651 have a trailing array reference that gives the size of the array. */
6653 static bool
6654 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6656 int i, pointer, allocatable, dimension, is_abstract;
6657 int codimension;
6658 bool coindexed;
6659 bool unlimited;
6660 symbol_attribute attr;
6661 gfc_ref *ref, *ref2;
6662 gfc_expr *e2;
6663 gfc_array_ref *ar;
6664 gfc_symbol *sym = NULL;
6665 gfc_alloc *a;
6666 gfc_component *c;
6667 bool t;
6669 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6670 checking of coarrays. */
6671 for (ref = e->ref; ref; ref = ref->next)
6672 if (ref->next == NULL)
6673 break;
6675 if (ref && ref->type == REF_ARRAY)
6676 ref->u.ar.in_allocate = true;
6678 if (!gfc_resolve_expr (e))
6679 goto failure;
6681 /* Make sure the expression is allocatable or a pointer. If it is
6682 pointer, the next-to-last reference must be a pointer. */
6684 ref2 = NULL;
6685 if (e->symtree)
6686 sym = e->symtree->n.sym;
6688 /* Check whether ultimate component is abstract and CLASS. */
6689 is_abstract = 0;
6691 /* Is the allocate-object unlimited polymorphic? */
6692 unlimited = UNLIMITED_POLY(e);
6694 if (e->expr_type != EXPR_VARIABLE)
6696 allocatable = 0;
6697 attr = gfc_expr_attr (e);
6698 pointer = attr.pointer;
6699 dimension = attr.dimension;
6700 codimension = attr.codimension;
6702 else
6704 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6706 allocatable = CLASS_DATA (sym)->attr.allocatable;
6707 pointer = CLASS_DATA (sym)->attr.class_pointer;
6708 dimension = CLASS_DATA (sym)->attr.dimension;
6709 codimension = CLASS_DATA (sym)->attr.codimension;
6710 is_abstract = CLASS_DATA (sym)->attr.abstract;
6712 else
6714 allocatable = sym->attr.allocatable;
6715 pointer = sym->attr.pointer;
6716 dimension = sym->attr.dimension;
6717 codimension = sym->attr.codimension;
6720 coindexed = false;
6722 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6724 switch (ref->type)
6726 case REF_ARRAY:
6727 if (ref->u.ar.codimen > 0)
6729 int n;
6730 for (n = ref->u.ar.dimen;
6731 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6732 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6734 coindexed = true;
6735 break;
6739 if (ref->next != NULL)
6740 pointer = 0;
6741 break;
6743 case REF_COMPONENT:
6744 /* F2008, C644. */
6745 if (coindexed)
6747 gfc_error ("Coindexed allocatable object at %L",
6748 &e->where);
6749 goto failure;
6752 c = ref->u.c.component;
6753 if (c->ts.type == BT_CLASS)
6755 allocatable = CLASS_DATA (c)->attr.allocatable;
6756 pointer = CLASS_DATA (c)->attr.class_pointer;
6757 dimension = CLASS_DATA (c)->attr.dimension;
6758 codimension = CLASS_DATA (c)->attr.codimension;
6759 is_abstract = CLASS_DATA (c)->attr.abstract;
6761 else
6763 allocatable = c->attr.allocatable;
6764 pointer = c->attr.pointer;
6765 dimension = c->attr.dimension;
6766 codimension = c->attr.codimension;
6767 is_abstract = c->attr.abstract;
6769 break;
6771 case REF_SUBSTRING:
6772 allocatable = 0;
6773 pointer = 0;
6774 break;
6779 /* Check for F08:C628. */
6780 if (allocatable == 0 && pointer == 0 && !unlimited)
6782 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6783 &e->where);
6784 goto failure;
6787 /* Some checks for the SOURCE tag. */
6788 if (code->expr3)
6790 /* Check F03:C631. */
6791 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6793 gfc_error ("Type of entity at %L is type incompatible with "
6794 "source-expr at %L", &e->where, &code->expr3->where);
6795 goto failure;
6798 /* Check F03:C632 and restriction following Note 6.18. */
6799 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
6800 goto failure;
6802 /* Check F03:C633. */
6803 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6805 gfc_error ("The allocate-object at %L and the source-expr at %L "
6806 "shall have the same kind type parameter",
6807 &e->where, &code->expr3->where);
6808 goto failure;
6811 /* Check F2008, C642. */
6812 if (code->expr3->ts.type == BT_DERIVED
6813 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6814 || (code->expr3->ts.u.derived->from_intmod
6815 == INTMOD_ISO_FORTRAN_ENV
6816 && code->expr3->ts.u.derived->intmod_sym_id
6817 == ISOFORTRAN_LOCK_TYPE)))
6819 gfc_error ("The source-expr at %L shall neither be of type "
6820 "LOCK_TYPE nor have a LOCK_TYPE component if "
6821 "allocate-object at %L is a coarray",
6822 &code->expr3->where, &e->where);
6823 goto failure;
6827 /* Check F08:C629. */
6828 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6829 && !code->expr3)
6831 gcc_assert (e->ts.type == BT_CLASS);
6832 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6833 "type-spec or source-expr", sym->name, &e->where);
6834 goto failure;
6837 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6839 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6840 code->ext.alloc.ts.u.cl->length);
6841 if (cmp == 1 || cmp == -1 || cmp == -3)
6843 gfc_error ("Allocating %s at %L with type-spec requires the same "
6844 "character-length parameter as in the declaration",
6845 sym->name, &e->where);
6846 goto failure;
6850 /* In the variable definition context checks, gfc_expr_attr is used
6851 on the expression. This is fooled by the array specification
6852 present in e, thus we have to eliminate that one temporarily. */
6853 e2 = remove_last_array_ref (e);
6854 t = true;
6855 if (t && pointer)
6856 t = gfc_check_vardef_context (e2, true, true, false,
6857 _("ALLOCATE object"));
6858 if (t)
6859 t = gfc_check_vardef_context (e2, false, true, false,
6860 _("ALLOCATE object"));
6861 gfc_free_expr (e2);
6862 if (!t)
6863 goto failure;
6865 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6866 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6868 /* For class arrays, the initialization with SOURCE is done
6869 using _copy and trans_call. It is convenient to exploit that
6870 when the allocated type is different from the declared type but
6871 no SOURCE exists by setting expr3. */
6872 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6874 else if (!code->expr3)
6876 /* Set up default initializer if needed. */
6877 gfc_typespec ts;
6878 gfc_expr *init_e;
6880 if (code->ext.alloc.ts.type == BT_DERIVED)
6881 ts = code->ext.alloc.ts;
6882 else
6883 ts = e->ts;
6885 if (ts.type == BT_CLASS)
6886 ts = ts.u.derived->components->ts;
6888 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6890 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6891 init_st->loc = code->loc;
6892 init_st->expr1 = gfc_expr_to_initialize (e);
6893 init_st->expr2 = init_e;
6894 init_st->next = code->next;
6895 code->next = init_st;
6898 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6900 /* Default initialization via MOLD (non-polymorphic). */
6901 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6902 gfc_resolve_expr (rhs);
6903 gfc_free_expr (code->expr3);
6904 code->expr3 = rhs;
6907 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6909 /* Make sure the vtab symbol is present when
6910 the module variables are generated. */
6911 gfc_typespec ts = e->ts;
6912 if (code->expr3)
6913 ts = code->expr3->ts;
6914 else if (code->ext.alloc.ts.type == BT_DERIVED)
6915 ts = code->ext.alloc.ts;
6917 gfc_find_derived_vtab (ts.u.derived);
6919 if (dimension)
6920 e = gfc_expr_to_initialize (e);
6922 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6924 /* Again, make sure the vtab symbol is present when
6925 the module variables are generated. */
6926 gfc_typespec *ts = NULL;
6927 if (code->expr3)
6928 ts = &code->expr3->ts;
6929 else
6930 ts = &code->ext.alloc.ts;
6932 gcc_assert (ts);
6934 gfc_find_vtab (ts);
6936 if (dimension)
6937 e = gfc_expr_to_initialize (e);
6940 if (dimension == 0 && codimension == 0)
6941 goto success;
6943 /* Make sure the last reference node is an array specification. */
6945 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6946 || (dimension && ref2->u.ar.dimen == 0))
6948 gfc_error ("Array specification required in ALLOCATE statement "
6949 "at %L", &e->where);
6950 goto failure;
6953 /* Make sure that the array section reference makes sense in the
6954 context of an ALLOCATE specification. */
6956 ar = &ref2->u.ar;
6958 if (codimension)
6959 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6960 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6962 gfc_error ("Coarray specification required in ALLOCATE statement "
6963 "at %L", &e->where);
6964 goto failure;
6967 for (i = 0; i < ar->dimen; i++)
6969 if (ref2->u.ar.type == AR_ELEMENT)
6970 goto check_symbols;
6972 switch (ar->dimen_type[i])
6974 case DIMEN_ELEMENT:
6975 break;
6977 case DIMEN_RANGE:
6978 if (ar->start[i] != NULL
6979 && ar->end[i] != NULL
6980 && ar->stride[i] == NULL)
6981 break;
6983 /* Fall Through... */
6985 case DIMEN_UNKNOWN:
6986 case DIMEN_VECTOR:
6987 case DIMEN_STAR:
6988 case DIMEN_THIS_IMAGE:
6989 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6990 &e->where);
6991 goto failure;
6994 check_symbols:
6995 for (a = code->ext.alloc.list; a; a = a->next)
6997 sym = a->expr->symtree->n.sym;
6999 /* TODO - check derived type components. */
7000 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7001 continue;
7003 if ((ar->start[i] != NULL
7004 && gfc_find_sym_in_expr (sym, ar->start[i]))
7005 || (ar->end[i] != NULL
7006 && gfc_find_sym_in_expr (sym, ar->end[i])))
7008 gfc_error ("'%s' must not appear in the array specification at "
7009 "%L in the same ALLOCATE statement where it is "
7010 "itself allocated", sym->name, &ar->where);
7011 goto failure;
7016 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7018 if (ar->dimen_type[i] == DIMEN_ELEMENT
7019 || ar->dimen_type[i] == DIMEN_RANGE)
7021 if (i == (ar->dimen + ar->codimen - 1))
7023 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7024 "statement at %L", &e->where);
7025 goto failure;
7027 continue;
7030 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7031 && ar->stride[i] == NULL)
7032 break;
7034 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7035 &e->where);
7036 goto failure;
7039 success:
7040 return true;
7042 failure:
7043 return false;
7046 static void
7047 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7049 gfc_expr *stat, *errmsg, *pe, *qe;
7050 gfc_alloc *a, *p, *q;
7052 stat = code->expr1;
7053 errmsg = code->expr2;
7055 /* Check the stat variable. */
7056 if (stat)
7058 gfc_check_vardef_context (stat, false, false, false,
7059 _("STAT variable"));
7061 if ((stat->ts.type != BT_INTEGER
7062 && !(stat->ref && (stat->ref->type == REF_ARRAY
7063 || stat->ref->type == REF_COMPONENT)))
7064 || stat->rank > 0)
7065 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7066 "variable", &stat->where);
7068 for (p = code->ext.alloc.list; p; p = p->next)
7069 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7071 gfc_ref *ref1, *ref2;
7072 bool found = true;
7074 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7075 ref1 = ref1->next, ref2 = ref2->next)
7077 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7078 continue;
7079 if (ref1->u.c.component->name != ref2->u.c.component->name)
7081 found = false;
7082 break;
7086 if (found)
7088 gfc_error ("Stat-variable at %L shall not be %sd within "
7089 "the same %s statement", &stat->where, fcn, fcn);
7090 break;
7095 /* Check the errmsg variable. */
7096 if (errmsg)
7098 if (!stat)
7099 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7100 &errmsg->where);
7102 gfc_check_vardef_context (errmsg, false, false, false,
7103 _("ERRMSG variable"));
7105 if ((errmsg->ts.type != BT_CHARACTER
7106 && !(errmsg->ref
7107 && (errmsg->ref->type == REF_ARRAY
7108 || errmsg->ref->type == REF_COMPONENT)))
7109 || errmsg->rank > 0 )
7110 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7111 "variable", &errmsg->where);
7113 for (p = code->ext.alloc.list; p; p = p->next)
7114 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7116 gfc_ref *ref1, *ref2;
7117 bool found = true;
7119 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7120 ref1 = ref1->next, ref2 = ref2->next)
7122 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7123 continue;
7124 if (ref1->u.c.component->name != ref2->u.c.component->name)
7126 found = false;
7127 break;
7131 if (found)
7133 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7134 "the same %s statement", &errmsg->where, fcn, fcn);
7135 break;
7140 /* Check that an allocate-object appears only once in the statement. */
7142 for (p = code->ext.alloc.list; p; p = p->next)
7144 pe = p->expr;
7145 for (q = p->next; q; q = q->next)
7147 qe = q->expr;
7148 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7150 /* This is a potential collision. */
7151 gfc_ref *pr = pe->ref;
7152 gfc_ref *qr = qe->ref;
7154 /* Follow the references until
7155 a) They start to differ, in which case there is no error;
7156 you can deallocate a%b and a%c in a single statement
7157 b) Both of them stop, which is an error
7158 c) One of them stops, which is also an error. */
7159 while (1)
7161 if (pr == NULL && qr == NULL)
7163 gfc_error ("Allocate-object at %L also appears at %L",
7164 &pe->where, &qe->where);
7165 break;
7167 else if (pr != NULL && qr == NULL)
7169 gfc_error ("Allocate-object at %L is subobject of"
7170 " object at %L", &pe->where, &qe->where);
7171 break;
7173 else if (pr == NULL && qr != NULL)
7175 gfc_error ("Allocate-object at %L is subobject of"
7176 " object at %L", &qe->where, &pe->where);
7177 break;
7179 /* Here, pr != NULL && qr != NULL */
7180 gcc_assert(pr->type == qr->type);
7181 if (pr->type == REF_ARRAY)
7183 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7184 which are legal. */
7185 gcc_assert (qr->type == REF_ARRAY);
7187 if (pr->next && qr->next)
7189 int i;
7190 gfc_array_ref *par = &(pr->u.ar);
7191 gfc_array_ref *qar = &(qr->u.ar);
7193 for (i=0; i<par->dimen; i++)
7195 if ((par->start[i] != NULL
7196 || qar->start[i] != NULL)
7197 && gfc_dep_compare_expr (par->start[i],
7198 qar->start[i]) != 0)
7199 goto break_label;
7203 else
7205 if (pr->u.c.component->name != qr->u.c.component->name)
7206 break;
7209 pr = pr->next;
7210 qr = qr->next;
7212 break_label:
7218 if (strcmp (fcn, "ALLOCATE") == 0)
7220 for (a = code->ext.alloc.list; a; a = a->next)
7221 resolve_allocate_expr (a->expr, code);
7223 else
7225 for (a = code->ext.alloc.list; a; a = a->next)
7226 resolve_deallocate_expr (a->expr);
7231 /************ SELECT CASE resolution subroutines ************/
7233 /* Callback function for our mergesort variant. Determines interval
7234 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7235 op1 > op2. Assumes we're not dealing with the default case.
7236 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7237 There are nine situations to check. */
7239 static int
7240 compare_cases (const gfc_case *op1, const gfc_case *op2)
7242 int retval;
7244 if (op1->low == NULL) /* op1 = (:L) */
7246 /* op2 = (:N), so overlap. */
7247 retval = 0;
7248 /* op2 = (M:) or (M:N), L < M */
7249 if (op2->low != NULL
7250 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7251 retval = -1;
7253 else if (op1->high == NULL) /* op1 = (K:) */
7255 /* op2 = (M:), so overlap. */
7256 retval = 0;
7257 /* op2 = (:N) or (M:N), K > N */
7258 if (op2->high != NULL
7259 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7260 retval = 1;
7262 else /* op1 = (K:L) */
7264 if (op2->low == NULL) /* op2 = (:N), K > N */
7265 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7266 ? 1 : 0;
7267 else if (op2->high == NULL) /* op2 = (M:), L < M */
7268 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7269 ? -1 : 0;
7270 else /* op2 = (M:N) */
7272 retval = 0;
7273 /* L < M */
7274 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7275 retval = -1;
7276 /* K > N */
7277 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7278 retval = 1;
7282 return retval;
7286 /* Merge-sort a double linked case list, detecting overlap in the
7287 process. LIST is the head of the double linked case list before it
7288 is sorted. Returns the head of the sorted list if we don't see any
7289 overlap, or NULL otherwise. */
7291 static gfc_case *
7292 check_case_overlap (gfc_case *list)
7294 gfc_case *p, *q, *e, *tail;
7295 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7297 /* If the passed list was empty, return immediately. */
7298 if (!list)
7299 return NULL;
7301 overlap_seen = 0;
7302 insize = 1;
7304 /* Loop unconditionally. The only exit from this loop is a return
7305 statement, when we've finished sorting the case list. */
7306 for (;;)
7308 p = list;
7309 list = NULL;
7310 tail = NULL;
7312 /* Count the number of merges we do in this pass. */
7313 nmerges = 0;
7315 /* Loop while there exists a merge to be done. */
7316 while (p)
7318 int i;
7320 /* Count this merge. */
7321 nmerges++;
7323 /* Cut the list in two pieces by stepping INSIZE places
7324 forward in the list, starting from P. */
7325 psize = 0;
7326 q = p;
7327 for (i = 0; i < insize; i++)
7329 psize++;
7330 q = q->right;
7331 if (!q)
7332 break;
7334 qsize = insize;
7336 /* Now we have two lists. Merge them! */
7337 while (psize > 0 || (qsize > 0 && q != NULL))
7339 /* See from which the next case to merge comes from. */
7340 if (psize == 0)
7342 /* P is empty so the next case must come from Q. */
7343 e = q;
7344 q = q->right;
7345 qsize--;
7347 else if (qsize == 0 || q == NULL)
7349 /* Q is empty. */
7350 e = p;
7351 p = p->right;
7352 psize--;
7354 else
7356 cmp = compare_cases (p, q);
7357 if (cmp < 0)
7359 /* The whole case range for P is less than the
7360 one for Q. */
7361 e = p;
7362 p = p->right;
7363 psize--;
7365 else if (cmp > 0)
7367 /* The whole case range for Q is greater than
7368 the case range for P. */
7369 e = q;
7370 q = q->right;
7371 qsize--;
7373 else
7375 /* The cases overlap, or they are the same
7376 element in the list. Either way, we must
7377 issue an error and get the next case from P. */
7378 /* FIXME: Sort P and Q by line number. */
7379 gfc_error ("CASE label at %L overlaps with CASE "
7380 "label at %L", &p->where, &q->where);
7381 overlap_seen = 1;
7382 e = p;
7383 p = p->right;
7384 psize--;
7388 /* Add the next element to the merged list. */
7389 if (tail)
7390 tail->right = e;
7391 else
7392 list = e;
7393 e->left = tail;
7394 tail = e;
7397 /* P has now stepped INSIZE places along, and so has Q. So
7398 they're the same. */
7399 p = q;
7401 tail->right = NULL;
7403 /* If we have done only one merge or none at all, we've
7404 finished sorting the cases. */
7405 if (nmerges <= 1)
7407 if (!overlap_seen)
7408 return list;
7409 else
7410 return NULL;
7413 /* Otherwise repeat, merging lists twice the size. */
7414 insize *= 2;
7419 /* Check to see if an expression is suitable for use in a CASE statement.
7420 Makes sure that all case expressions are scalar constants of the same
7421 type. Return false if anything is wrong. */
7423 static bool
7424 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7426 if (e == NULL) return true;
7428 if (e->ts.type != case_expr->ts.type)
7430 gfc_error ("Expression in CASE statement at %L must be of type %s",
7431 &e->where, gfc_basic_typename (case_expr->ts.type));
7432 return false;
7435 /* C805 (R808) For a given case-construct, each case-value shall be of
7436 the same type as case-expr. For character type, length differences
7437 are allowed, but the kind type parameters shall be the same. */
7439 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7441 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7442 &e->where, case_expr->ts.kind);
7443 return false;
7446 /* Convert the case value kind to that of case expression kind,
7447 if needed */
7449 if (e->ts.kind != case_expr->ts.kind)
7450 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7452 if (e->rank != 0)
7454 gfc_error ("Expression in CASE statement at %L must be scalar",
7455 &e->where);
7456 return false;
7459 return true;
7463 /* Given a completely parsed select statement, we:
7465 - Validate all expressions and code within the SELECT.
7466 - Make sure that the selection expression is not of the wrong type.
7467 - Make sure that no case ranges overlap.
7468 - Eliminate unreachable cases and unreachable code resulting from
7469 removing case labels.
7471 The standard does allow unreachable cases, e.g. CASE (5:3). But
7472 they are a hassle for code generation, and to prevent that, we just
7473 cut them out here. This is not necessary for overlapping cases
7474 because they are illegal and we never even try to generate code.
7476 We have the additional caveat that a SELECT construct could have
7477 been a computed GOTO in the source code. Fortunately we can fairly
7478 easily work around that here: The case_expr for a "real" SELECT CASE
7479 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7480 we have to do is make sure that the case_expr is a scalar integer
7481 expression. */
7483 static void
7484 resolve_select (gfc_code *code, bool select_type)
7486 gfc_code *body;
7487 gfc_expr *case_expr;
7488 gfc_case *cp, *default_case, *tail, *head;
7489 int seen_unreachable;
7490 int seen_logical;
7491 int ncases;
7492 bt type;
7493 bool t;
7495 if (code->expr1 == NULL)
7497 /* This was actually a computed GOTO statement. */
7498 case_expr = code->expr2;
7499 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7500 gfc_error ("Selection expression in computed GOTO statement "
7501 "at %L must be a scalar integer expression",
7502 &case_expr->where);
7504 /* Further checking is not necessary because this SELECT was built
7505 by the compiler, so it should always be OK. Just move the
7506 case_expr from expr2 to expr so that we can handle computed
7507 GOTOs as normal SELECTs from here on. */
7508 code->expr1 = code->expr2;
7509 code->expr2 = NULL;
7510 return;
7513 case_expr = code->expr1;
7514 type = case_expr->ts.type;
7516 /* F08:C830. */
7517 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7519 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7520 &case_expr->where, gfc_typename (&case_expr->ts));
7522 /* Punt. Going on here just produce more garbage error messages. */
7523 return;
7526 /* F08:R842. */
7527 if (!select_type && case_expr->rank != 0)
7529 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7530 "expression", &case_expr->where);
7532 /* Punt. */
7533 return;
7536 /* Raise a warning if an INTEGER case value exceeds the range of
7537 the case-expr. Later, all expressions will be promoted to the
7538 largest kind of all case-labels. */
7540 if (type == BT_INTEGER)
7541 for (body = code->block; body; body = body->block)
7542 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7544 if (cp->low
7545 && gfc_check_integer_range (cp->low->value.integer,
7546 case_expr->ts.kind) != ARITH_OK)
7547 gfc_warning ("Expression in CASE statement at %L is "
7548 "not in the range of %s", &cp->low->where,
7549 gfc_typename (&case_expr->ts));
7551 if (cp->high
7552 && cp->low != cp->high
7553 && gfc_check_integer_range (cp->high->value.integer,
7554 case_expr->ts.kind) != ARITH_OK)
7555 gfc_warning ("Expression in CASE statement at %L is "
7556 "not in the range of %s", &cp->high->where,
7557 gfc_typename (&case_expr->ts));
7560 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7561 of the SELECT CASE expression and its CASE values. Walk the lists
7562 of case values, and if we find a mismatch, promote case_expr to
7563 the appropriate kind. */
7565 if (type == BT_LOGICAL || type == BT_INTEGER)
7567 for (body = code->block; body; body = body->block)
7569 /* Walk the case label list. */
7570 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7572 /* Intercept the DEFAULT case. It does not have a kind. */
7573 if (cp->low == NULL && cp->high == NULL)
7574 continue;
7576 /* Unreachable case ranges are discarded, so ignore. */
7577 if (cp->low != NULL && cp->high != NULL
7578 && cp->low != cp->high
7579 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7580 continue;
7582 if (cp->low != NULL
7583 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7584 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7586 if (cp->high != NULL
7587 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7588 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7593 /* Assume there is no DEFAULT case. */
7594 default_case = NULL;
7595 head = tail = NULL;
7596 ncases = 0;
7597 seen_logical = 0;
7599 for (body = code->block; body; body = body->block)
7601 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7602 t = true;
7603 seen_unreachable = 0;
7605 /* Walk the case label list, making sure that all case labels
7606 are legal. */
7607 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7609 /* Count the number of cases in the whole construct. */
7610 ncases++;
7612 /* Intercept the DEFAULT case. */
7613 if (cp->low == NULL && cp->high == NULL)
7615 if (default_case != NULL)
7617 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7618 "by a second DEFAULT CASE at %L",
7619 &default_case->where, &cp->where);
7620 t = false;
7621 break;
7623 else
7625 default_case = cp;
7626 continue;
7630 /* Deal with single value cases and case ranges. Errors are
7631 issued from the validation function. */
7632 if (!validate_case_label_expr (cp->low, case_expr)
7633 || !validate_case_label_expr (cp->high, case_expr))
7635 t = false;
7636 break;
7639 if (type == BT_LOGICAL
7640 && ((cp->low == NULL || cp->high == NULL)
7641 || cp->low != cp->high))
7643 gfc_error ("Logical range in CASE statement at %L is not "
7644 "allowed", &cp->low->where);
7645 t = false;
7646 break;
7649 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7651 int value;
7652 value = cp->low->value.logical == 0 ? 2 : 1;
7653 if (value & seen_logical)
7655 gfc_error ("Constant logical value in CASE statement "
7656 "is repeated at %L",
7657 &cp->low->where);
7658 t = false;
7659 break;
7661 seen_logical |= value;
7664 if (cp->low != NULL && cp->high != NULL
7665 && cp->low != cp->high
7666 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7668 if (gfc_option.warn_surprising)
7669 gfc_warning ("Range specification at %L can never "
7670 "be matched", &cp->where);
7672 cp->unreachable = 1;
7673 seen_unreachable = 1;
7675 else
7677 /* If the case range can be matched, it can also overlap with
7678 other cases. To make sure it does not, we put it in a
7679 double linked list here. We sort that with a merge sort
7680 later on to detect any overlapping cases. */
7681 if (!head)
7683 head = tail = cp;
7684 head->right = head->left = NULL;
7686 else
7688 tail->right = cp;
7689 tail->right->left = tail;
7690 tail = tail->right;
7691 tail->right = NULL;
7696 /* It there was a failure in the previous case label, give up
7697 for this case label list. Continue with the next block. */
7698 if (!t)
7699 continue;
7701 /* See if any case labels that are unreachable have been seen.
7702 If so, we eliminate them. This is a bit of a kludge because
7703 the case lists for a single case statement (label) is a
7704 single forward linked lists. */
7705 if (seen_unreachable)
7707 /* Advance until the first case in the list is reachable. */
7708 while (body->ext.block.case_list != NULL
7709 && body->ext.block.case_list->unreachable)
7711 gfc_case *n = body->ext.block.case_list;
7712 body->ext.block.case_list = body->ext.block.case_list->next;
7713 n->next = NULL;
7714 gfc_free_case_list (n);
7717 /* Strip all other unreachable cases. */
7718 if (body->ext.block.case_list)
7720 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7722 if (cp->next->unreachable)
7724 gfc_case *n = cp->next;
7725 cp->next = cp->next->next;
7726 n->next = NULL;
7727 gfc_free_case_list (n);
7734 /* See if there were overlapping cases. If the check returns NULL,
7735 there was overlap. In that case we don't do anything. If head
7736 is non-NULL, we prepend the DEFAULT case. The sorted list can
7737 then used during code generation for SELECT CASE constructs with
7738 a case expression of a CHARACTER type. */
7739 if (head)
7741 head = check_case_overlap (head);
7743 /* Prepend the default_case if it is there. */
7744 if (head != NULL && default_case)
7746 default_case->left = NULL;
7747 default_case->right = head;
7748 head->left = default_case;
7752 /* Eliminate dead blocks that may be the result if we've seen
7753 unreachable case labels for a block. */
7754 for (body = code; body && body->block; body = body->block)
7756 if (body->block->ext.block.case_list == NULL)
7758 /* Cut the unreachable block from the code chain. */
7759 gfc_code *c = body->block;
7760 body->block = c->block;
7762 /* Kill the dead block, but not the blocks below it. */
7763 c->block = NULL;
7764 gfc_free_statements (c);
7768 /* More than two cases is legal but insane for logical selects.
7769 Issue a warning for it. */
7770 if (gfc_option.warn_surprising && type == BT_LOGICAL
7771 && ncases > 2)
7772 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7773 &code->loc);
7777 /* Check if a derived type is extensible. */
7779 bool
7780 gfc_type_is_extensible (gfc_symbol *sym)
7782 return !(sym->attr.is_bind_c || sym->attr.sequence
7783 || (sym->attr.is_class
7784 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7788 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7789 correct as well as possibly the array-spec. */
7791 static void
7792 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7794 gfc_expr* target;
7796 gcc_assert (sym->assoc);
7797 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7799 /* If this is for SELECT TYPE, the target may not yet be set. In that
7800 case, return. Resolution will be called later manually again when
7801 this is done. */
7802 target = sym->assoc->target;
7803 if (!target)
7804 return;
7805 gcc_assert (!sym->assoc->dangling);
7807 if (resolve_target && !gfc_resolve_expr (target))
7808 return;
7810 /* For variable targets, we get some attributes from the target. */
7811 if (target->expr_type == EXPR_VARIABLE)
7813 gfc_symbol* tsym;
7815 gcc_assert (target->symtree);
7816 tsym = target->symtree->n.sym;
7818 sym->attr.asynchronous = tsym->attr.asynchronous;
7819 sym->attr.volatile_ = tsym->attr.volatile_;
7821 sym->attr.target = tsym->attr.target
7822 || gfc_expr_attr (target).pointer;
7823 if (is_subref_array (target))
7824 sym->attr.subref_array_pointer = 1;
7827 /* Get type if this was not already set. Note that it can be
7828 some other type than the target in case this is a SELECT TYPE
7829 selector! So we must not update when the type is already there. */
7830 if (sym->ts.type == BT_UNKNOWN)
7831 sym->ts = target->ts;
7832 gcc_assert (sym->ts.type != BT_UNKNOWN);
7834 /* See if this is a valid association-to-variable. */
7835 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7836 && !gfc_has_vector_subscript (target));
7838 /* Finally resolve if this is an array or not. */
7839 if (sym->attr.dimension && target->rank == 0)
7841 gfc_error ("Associate-name '%s' at %L is used as array",
7842 sym->name, &sym->declared_at);
7843 sym->attr.dimension = 0;
7844 return;
7847 /* We cannot deal with class selectors that need temporaries. */
7848 if (target->ts.type == BT_CLASS
7849 && gfc_ref_needs_temporary_p (target->ref))
7851 gfc_error ("CLASS selector at %L needs a temporary which is not "
7852 "yet implemented", &target->where);
7853 return;
7856 if (target->ts.type != BT_CLASS && target->rank > 0)
7857 sym->attr.dimension = 1;
7858 else if (target->ts.type == BT_CLASS)
7859 gfc_fix_class_refs (target);
7861 /* The associate-name will have a correct type by now. Make absolutely
7862 sure that it has not picked up a dimension attribute. */
7863 if (sym->ts.type == BT_CLASS)
7864 sym->attr.dimension = 0;
7866 if (sym->attr.dimension)
7868 sym->as = gfc_get_array_spec ();
7869 sym->as->rank = target->rank;
7870 sym->as->type = AS_DEFERRED;
7872 /* Target must not be coindexed, thus the associate-variable
7873 has no corank. */
7874 sym->as->corank = 0;
7877 /* Mark this as an associate variable. */
7878 sym->attr.associate_var = 1;
7880 /* If the target is a good class object, so is the associate variable. */
7881 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7882 sym->attr.class_ok = 1;
7886 /* Resolve a SELECT TYPE statement. */
7888 static void
7889 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7891 gfc_symbol *selector_type;
7892 gfc_code *body, *new_st, *if_st, *tail;
7893 gfc_code *class_is = NULL, *default_case = NULL;
7894 gfc_case *c;
7895 gfc_symtree *st;
7896 char name[GFC_MAX_SYMBOL_LEN];
7897 gfc_namespace *ns;
7898 int error = 0;
7899 int charlen = 0;
7901 ns = code->ext.block.ns;
7902 gfc_resolve (ns);
7904 /* Check for F03:C813. */
7905 if (code->expr1->ts.type != BT_CLASS
7906 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7908 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7909 "at %L", &code->loc);
7910 return;
7913 if (!code->expr1->symtree->n.sym->attr.class_ok)
7914 return;
7916 if (code->expr2)
7918 if (code->expr1->symtree->n.sym->attr.untyped)
7919 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7920 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7922 /* F2008: C803 The selector expression must not be coindexed. */
7923 if (gfc_is_coindexed (code->expr2))
7925 gfc_error ("Selector at %L must not be coindexed",
7926 &code->expr2->where);
7927 return;
7931 else
7933 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7935 if (gfc_is_coindexed (code->expr1))
7937 gfc_error ("Selector at %L must not be coindexed",
7938 &code->expr1->where);
7939 return;
7943 /* Loop over TYPE IS / CLASS IS cases. */
7944 for (body = code->block; body; body = body->block)
7946 c = body->ext.block.case_list;
7948 /* Check F03:C815. */
7949 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7950 && !selector_type->attr.unlimited_polymorphic
7951 && !gfc_type_is_extensible (c->ts.u.derived))
7953 gfc_error ("Derived type '%s' at %L must be extensible",
7954 c->ts.u.derived->name, &c->where);
7955 error++;
7956 continue;
7959 /* Check F03:C816. */
7960 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7961 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7962 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7964 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7965 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7966 c->ts.u.derived->name, &c->where, selector_type->name);
7967 else
7968 gfc_error ("Unexpected intrinsic type '%s' at %L",
7969 gfc_basic_typename (c->ts.type), &c->where);
7970 error++;
7971 continue;
7974 /* Check F03:C814. */
7975 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7977 gfc_error ("The type-spec at %L shall specify that each length "
7978 "type parameter is assumed", &c->where);
7979 error++;
7980 continue;
7983 /* Intercept the DEFAULT case. */
7984 if (c->ts.type == BT_UNKNOWN)
7986 /* Check F03:C818. */
7987 if (default_case)
7989 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7990 "by a second DEFAULT CASE at %L",
7991 &default_case->ext.block.case_list->where, &c->where);
7992 error++;
7993 continue;
7996 default_case = body;
8000 if (error > 0)
8001 return;
8003 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8004 target if present. If there are any EXIT statements referring to the
8005 SELECT TYPE construct, this is no problem because the gfc_code
8006 reference stays the same and EXIT is equally possible from the BLOCK
8007 it is changed to. */
8008 code->op = EXEC_BLOCK;
8009 if (code->expr2)
8011 gfc_association_list* assoc;
8013 assoc = gfc_get_association_list ();
8014 assoc->st = code->expr1->symtree;
8015 assoc->target = gfc_copy_expr (code->expr2);
8016 assoc->target->where = code->expr2->where;
8017 /* assoc->variable will be set by resolve_assoc_var. */
8019 code->ext.block.assoc = assoc;
8020 code->expr1->symtree->n.sym->assoc = assoc;
8022 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8024 else
8025 code->ext.block.assoc = NULL;
8027 /* Add EXEC_SELECT to switch on type. */
8028 new_st = gfc_get_code (code->op);
8029 new_st->expr1 = code->expr1;
8030 new_st->expr2 = code->expr2;
8031 new_st->block = code->block;
8032 code->expr1 = code->expr2 = NULL;
8033 code->block = NULL;
8034 if (!ns->code)
8035 ns->code = new_st;
8036 else
8037 ns->code->next = new_st;
8038 code = new_st;
8039 code->op = EXEC_SELECT;
8041 gfc_add_vptr_component (code->expr1);
8042 gfc_add_hash_component (code->expr1);
8044 /* Loop over TYPE IS / CLASS IS cases. */
8045 for (body = code->block; body; body = body->block)
8047 c = body->ext.block.case_list;
8049 if (c->ts.type == BT_DERIVED)
8050 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8051 c->ts.u.derived->hash_value);
8052 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8054 gfc_symbol *ivtab;
8055 gfc_expr *e;
8057 ivtab = gfc_find_vtab (&c->ts);
8058 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8059 e = CLASS_DATA (ivtab)->initializer;
8060 c->low = c->high = gfc_copy_expr (e);
8063 else if (c->ts.type == BT_UNKNOWN)
8064 continue;
8066 /* Associate temporary to selector. This should only be done
8067 when this case is actually true, so build a new ASSOCIATE
8068 that does precisely this here (instead of using the
8069 'global' one). */
8071 if (c->ts.type == BT_CLASS)
8072 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8073 else if (c->ts.type == BT_DERIVED)
8074 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8075 else if (c->ts.type == BT_CHARACTER)
8077 if (c->ts.u.cl && c->ts.u.cl->length
8078 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8079 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8080 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8081 charlen, c->ts.kind);
8083 else
8084 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8085 c->ts.kind);
8087 st = gfc_find_symtree (ns->sym_root, name);
8088 gcc_assert (st->n.sym->assoc);
8089 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8090 st->n.sym->assoc->target->where = code->expr1->where;
8091 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8092 gfc_add_data_component (st->n.sym->assoc->target);
8094 new_st = gfc_get_code (EXEC_BLOCK);
8095 new_st->ext.block.ns = gfc_build_block_ns (ns);
8096 new_st->ext.block.ns->code = body->next;
8097 body->next = new_st;
8099 /* Chain in the new list only if it is marked as dangling. Otherwise
8100 there is a CASE label overlap and this is already used. Just ignore,
8101 the error is diagnosed elsewhere. */
8102 if (st->n.sym->assoc->dangling)
8104 new_st->ext.block.assoc = st->n.sym->assoc;
8105 st->n.sym->assoc->dangling = 0;
8108 resolve_assoc_var (st->n.sym, false);
8111 /* Take out CLASS IS cases for separate treatment. */
8112 body = code;
8113 while (body && body->block)
8115 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8117 /* Add to class_is list. */
8118 if (class_is == NULL)
8120 class_is = body->block;
8121 tail = class_is;
8123 else
8125 for (tail = class_is; tail->block; tail = tail->block) ;
8126 tail->block = body->block;
8127 tail = tail->block;
8129 /* Remove from EXEC_SELECT list. */
8130 body->block = body->block->block;
8131 tail->block = NULL;
8133 else
8134 body = body->block;
8137 if (class_is)
8139 gfc_symbol *vtab;
8141 if (!default_case)
8143 /* Add a default case to hold the CLASS IS cases. */
8144 for (tail = code; tail->block; tail = tail->block) ;
8145 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8146 tail = tail->block;
8147 tail->ext.block.case_list = gfc_get_case ();
8148 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8149 tail->next = NULL;
8150 default_case = tail;
8153 /* More than one CLASS IS block? */
8154 if (class_is->block)
8156 gfc_code **c1,*c2;
8157 bool swapped;
8158 /* Sort CLASS IS blocks by extension level. */
8161 swapped = false;
8162 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8164 c2 = (*c1)->block;
8165 /* F03:C817 (check for doubles). */
8166 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8167 == c2->ext.block.case_list->ts.u.derived->hash_value)
8169 gfc_error ("Double CLASS IS block in SELECT TYPE "
8170 "statement at %L",
8171 &c2->ext.block.case_list->where);
8172 return;
8174 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8175 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8177 /* Swap. */
8178 (*c1)->block = c2->block;
8179 c2->block = *c1;
8180 *c1 = c2;
8181 swapped = true;
8185 while (swapped);
8188 /* Generate IF chain. */
8189 if_st = gfc_get_code (EXEC_IF);
8190 new_st = if_st;
8191 for (body = class_is; body; body = body->block)
8193 new_st->block = gfc_get_code (EXEC_IF);
8194 new_st = new_st->block;
8195 /* Set up IF condition: Call _gfortran_is_extension_of. */
8196 new_st->expr1 = gfc_get_expr ();
8197 new_st->expr1->expr_type = EXPR_FUNCTION;
8198 new_st->expr1->ts.type = BT_LOGICAL;
8199 new_st->expr1->ts.kind = 4;
8200 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8201 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8202 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8203 /* Set up arguments. */
8204 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8205 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8206 new_st->expr1->value.function.actual->expr->where = code->loc;
8207 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8208 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8209 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8210 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8211 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8212 new_st->next = body->next;
8214 if (default_case->next)
8216 new_st->block = gfc_get_code (EXEC_IF);
8217 new_st = new_st->block;
8218 new_st->next = default_case->next;
8221 /* Replace CLASS DEFAULT code by the IF chain. */
8222 default_case->next = if_st;
8225 /* Resolve the internal code. This can not be done earlier because
8226 it requires that the sym->assoc of selectors is set already. */
8227 gfc_current_ns = ns;
8228 gfc_resolve_blocks (code->block, gfc_current_ns);
8229 gfc_current_ns = old_ns;
8231 resolve_select (code, true);
8235 /* Resolve a transfer statement. This is making sure that:
8236 -- a derived type being transferred has only non-pointer components
8237 -- a derived type being transferred doesn't have private components, unless
8238 it's being transferred from the module where the type was defined
8239 -- we're not trying to transfer a whole assumed size array. */
8241 static void
8242 resolve_transfer (gfc_code *code)
8244 gfc_typespec *ts;
8245 gfc_symbol *sym;
8246 gfc_ref *ref;
8247 gfc_expr *exp;
8249 exp = code->expr1;
8251 while (exp != NULL && exp->expr_type == EXPR_OP
8252 && exp->value.op.op == INTRINSIC_PARENTHESES)
8253 exp = exp->value.op.op1;
8255 if (exp && exp->expr_type == EXPR_NULL
8256 && code->ext.dt)
8258 gfc_error ("Invalid context for NULL () intrinsic at %L",
8259 &exp->where);
8260 return;
8263 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8264 && exp->expr_type != EXPR_FUNCTION))
8265 return;
8267 /* If we are reading, the variable will be changed. Note that
8268 code->ext.dt may be NULL if the TRANSFER is related to
8269 an INQUIRE statement -- but in this case, we are not reading, either. */
8270 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8271 && !gfc_check_vardef_context (exp, false, false, false,
8272 _("item in READ")))
8273 return;
8275 sym = exp->symtree->n.sym;
8276 ts = &sym->ts;
8278 /* Go to actual component transferred. */
8279 for (ref = exp->ref; ref; ref = ref->next)
8280 if (ref->type == REF_COMPONENT)
8281 ts = &ref->u.c.component->ts;
8283 if (ts->type == BT_CLASS)
8285 /* FIXME: Test for defined input/output. */
8286 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8287 "it is processed by a defined input/output procedure",
8288 &code->loc);
8289 return;
8292 if (ts->type == BT_DERIVED)
8294 /* Check that transferred derived type doesn't contain POINTER
8295 components. */
8296 if (ts->u.derived->attr.pointer_comp)
8298 gfc_error ("Data transfer element at %L cannot have POINTER "
8299 "components unless it is processed by a defined "
8300 "input/output procedure", &code->loc);
8301 return;
8304 /* F08:C935. */
8305 if (ts->u.derived->attr.proc_pointer_comp)
8307 gfc_error ("Data transfer element at %L cannot have "
8308 "procedure pointer components", &code->loc);
8309 return;
8312 if (ts->u.derived->attr.alloc_comp)
8314 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8315 "components unless it is processed by a defined "
8316 "input/output procedure", &code->loc);
8317 return;
8320 /* C_PTR and C_FUNPTR have private components which means they can not
8321 be printed. However, if -std=gnu and not -pedantic, allow
8322 the component to be printed to help debugging. */
8323 if (ts->u.derived->ts.f90_type == BT_VOID)
8325 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8326 "cannot have PRIVATE components", &code->loc))
8327 return;
8329 else if (derived_inaccessible (ts->u.derived))
8331 gfc_error ("Data transfer element at %L cannot have "
8332 "PRIVATE components",&code->loc);
8333 return;
8337 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8338 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8340 gfc_error ("Data transfer element at %L cannot be a full reference to "
8341 "an assumed-size array", &code->loc);
8342 return;
8347 /*********** Toplevel code resolution subroutines ***********/
8349 /* Find the set of labels that are reachable from this block. We also
8350 record the last statement in each block. */
8352 static void
8353 find_reachable_labels (gfc_code *block)
8355 gfc_code *c;
8357 if (!block)
8358 return;
8360 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8362 /* Collect labels in this block. We don't keep those corresponding
8363 to END {IF|SELECT}, these are checked in resolve_branch by going
8364 up through the code_stack. */
8365 for (c = block; c; c = c->next)
8367 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8368 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8371 /* Merge with labels from parent block. */
8372 if (cs_base->prev)
8374 gcc_assert (cs_base->prev->reachable_labels);
8375 bitmap_ior_into (cs_base->reachable_labels,
8376 cs_base->prev->reachable_labels);
8381 static void
8382 resolve_lock_unlock (gfc_code *code)
8384 if (code->expr1->ts.type != BT_DERIVED
8385 || code->expr1->expr_type != EXPR_VARIABLE
8386 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8387 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8388 || code->expr1->rank != 0
8389 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8390 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8391 &code->expr1->where);
8393 /* Check STAT. */
8394 if (code->expr2
8395 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8396 || code->expr2->expr_type != EXPR_VARIABLE))
8397 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8398 &code->expr2->where);
8400 if (code->expr2
8401 && !gfc_check_vardef_context (code->expr2, false, false, false,
8402 _("STAT variable")))
8403 return;
8405 /* Check ERRMSG. */
8406 if (code->expr3
8407 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8408 || code->expr3->expr_type != EXPR_VARIABLE))
8409 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8410 &code->expr3->where);
8412 if (code->expr3
8413 && !gfc_check_vardef_context (code->expr3, false, false, false,
8414 _("ERRMSG variable")))
8415 return;
8417 /* Check ACQUIRED_LOCK. */
8418 if (code->expr4
8419 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8420 || code->expr4->expr_type != EXPR_VARIABLE))
8421 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8422 "variable", &code->expr4->where);
8424 if (code->expr4
8425 && !gfc_check_vardef_context (code->expr4, false, false, false,
8426 _("ACQUIRED_LOCK variable")))
8427 return;
8431 static void
8432 resolve_sync (gfc_code *code)
8434 /* Check imageset. The * case matches expr1 == NULL. */
8435 if (code->expr1)
8437 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8438 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8439 "INTEGER expression", &code->expr1->where);
8440 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8441 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8442 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8443 &code->expr1->where);
8444 else if (code->expr1->expr_type == EXPR_ARRAY
8445 && gfc_simplify_expr (code->expr1, 0))
8447 gfc_constructor *cons;
8448 cons = gfc_constructor_first (code->expr1->value.constructor);
8449 for (; cons; cons = gfc_constructor_next (cons))
8450 if (cons->expr->expr_type == EXPR_CONSTANT
8451 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8452 gfc_error ("Imageset argument at %L must between 1 and "
8453 "num_images()", &cons->expr->where);
8457 /* Check STAT. */
8458 if (code->expr2
8459 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8460 || code->expr2->expr_type != EXPR_VARIABLE))
8461 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8462 &code->expr2->where);
8464 /* Check ERRMSG. */
8465 if (code->expr3
8466 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8467 || code->expr3->expr_type != EXPR_VARIABLE))
8468 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8469 &code->expr3->where);
8473 /* Given a branch to a label, see if the branch is conforming.
8474 The code node describes where the branch is located. */
8476 static void
8477 resolve_branch (gfc_st_label *label, gfc_code *code)
8479 code_stack *stack;
8481 if (label == NULL)
8482 return;
8484 /* Step one: is this a valid branching target? */
8486 if (label->defined == ST_LABEL_UNKNOWN)
8488 gfc_error ("Label %d referenced at %L is never defined", label->value,
8489 &label->where);
8490 return;
8493 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8495 gfc_error ("Statement at %L is not a valid branch target statement "
8496 "for the branch statement at %L", &label->where, &code->loc);
8497 return;
8500 /* Step two: make sure this branch is not a branch to itself ;-) */
8502 if (code->here == label)
8504 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8505 return;
8508 /* Step three: See if the label is in the same block as the
8509 branching statement. The hard work has been done by setting up
8510 the bitmap reachable_labels. */
8512 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8514 /* Check now whether there is a CRITICAL construct; if so, check
8515 whether the label is still visible outside of the CRITICAL block,
8516 which is invalid. */
8517 for (stack = cs_base; stack; stack = stack->prev)
8519 if (stack->current->op == EXEC_CRITICAL
8520 && bitmap_bit_p (stack->reachable_labels, label->value))
8521 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8522 "label at %L", &code->loc, &label->where);
8523 else if (stack->current->op == EXEC_DO_CONCURRENT
8524 && bitmap_bit_p (stack->reachable_labels, label->value))
8525 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8526 "for label at %L", &code->loc, &label->where);
8529 return;
8532 /* Step four: If we haven't found the label in the bitmap, it may
8533 still be the label of the END of the enclosing block, in which
8534 case we find it by going up the code_stack. */
8536 for (stack = cs_base; stack; stack = stack->prev)
8538 if (stack->current->next && stack->current->next->here == label)
8539 break;
8540 if (stack->current->op == EXEC_CRITICAL)
8542 /* Note: A label at END CRITICAL does not leave the CRITICAL
8543 construct as END CRITICAL is still part of it. */
8544 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8545 " at %L", &code->loc, &label->where);
8546 return;
8548 else if (stack->current->op == EXEC_DO_CONCURRENT)
8550 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8551 "label at %L", &code->loc, &label->where);
8552 return;
8556 if (stack)
8558 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8559 return;
8562 /* The label is not in an enclosing block, so illegal. This was
8563 allowed in Fortran 66, so we allow it as extension. No
8564 further checks are necessary in this case. */
8565 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8566 "as the GOTO statement at %L", &label->where,
8567 &code->loc);
8568 return;
8572 /* Check whether EXPR1 has the same shape as EXPR2. */
8574 static bool
8575 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8577 mpz_t shape[GFC_MAX_DIMENSIONS];
8578 mpz_t shape2[GFC_MAX_DIMENSIONS];
8579 bool result = false;
8580 int i;
8582 /* Compare the rank. */
8583 if (expr1->rank != expr2->rank)
8584 return result;
8586 /* Compare the size of each dimension. */
8587 for (i=0; i<expr1->rank; i++)
8589 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8590 goto ignore;
8592 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8593 goto ignore;
8595 if (mpz_cmp (shape[i], shape2[i]))
8596 goto over;
8599 /* When either of the two expression is an assumed size array, we
8600 ignore the comparison of dimension sizes. */
8601 ignore:
8602 result = true;
8604 over:
8605 gfc_clear_shape (shape, i);
8606 gfc_clear_shape (shape2, i);
8607 return result;
8611 /* Check whether a WHERE assignment target or a WHERE mask expression
8612 has the same shape as the outmost WHERE mask expression. */
8614 static void
8615 resolve_where (gfc_code *code, gfc_expr *mask)
8617 gfc_code *cblock;
8618 gfc_code *cnext;
8619 gfc_expr *e = NULL;
8621 cblock = code->block;
8623 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8624 In case of nested WHERE, only the outmost one is stored. */
8625 if (mask == NULL) /* outmost WHERE */
8626 e = cblock->expr1;
8627 else /* inner WHERE */
8628 e = mask;
8630 while (cblock)
8632 if (cblock->expr1)
8634 /* Check if the mask-expr has a consistent shape with the
8635 outmost WHERE mask-expr. */
8636 if (!resolve_where_shape (cblock->expr1, e))
8637 gfc_error ("WHERE mask at %L has inconsistent shape",
8638 &cblock->expr1->where);
8641 /* the assignment statement of a WHERE statement, or the first
8642 statement in where-body-construct of a WHERE construct */
8643 cnext = cblock->next;
8644 while (cnext)
8646 switch (cnext->op)
8648 /* WHERE assignment statement */
8649 case EXEC_ASSIGN:
8651 /* Check shape consistent for WHERE assignment target. */
8652 if (e && !resolve_where_shape (cnext->expr1, e))
8653 gfc_error ("WHERE assignment target at %L has "
8654 "inconsistent shape", &cnext->expr1->where);
8655 break;
8658 case EXEC_ASSIGN_CALL:
8659 resolve_call (cnext);
8660 if (!cnext->resolved_sym->attr.elemental)
8661 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8662 &cnext->ext.actual->expr->where);
8663 break;
8665 /* WHERE or WHERE construct is part of a where-body-construct */
8666 case EXEC_WHERE:
8667 resolve_where (cnext, e);
8668 break;
8670 default:
8671 gfc_error ("Unsupported statement inside WHERE at %L",
8672 &cnext->loc);
8674 /* the next statement within the same where-body-construct */
8675 cnext = cnext->next;
8677 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8678 cblock = cblock->block;
8683 /* Resolve assignment in FORALL construct.
8684 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8685 FORALL index variables. */
8687 static void
8688 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8690 int n;
8692 for (n = 0; n < nvar; n++)
8694 gfc_symbol *forall_index;
8696 forall_index = var_expr[n]->symtree->n.sym;
8698 /* Check whether the assignment target is one of the FORALL index
8699 variable. */
8700 if ((code->expr1->expr_type == EXPR_VARIABLE)
8701 && (code->expr1->symtree->n.sym == forall_index))
8702 gfc_error ("Assignment to a FORALL index variable at %L",
8703 &code->expr1->where);
8704 else
8706 /* If one of the FORALL index variables doesn't appear in the
8707 assignment variable, then there could be a many-to-one
8708 assignment. Emit a warning rather than an error because the
8709 mask could be resolving this problem. */
8710 if (!find_forall_index (code->expr1, forall_index, 0))
8711 gfc_warning ("The FORALL with index '%s' is not used on the "
8712 "left side of the assignment at %L and so might "
8713 "cause multiple assignment to this object",
8714 var_expr[n]->symtree->name, &code->expr1->where);
8720 /* Resolve WHERE statement in FORALL construct. */
8722 static void
8723 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8724 gfc_expr **var_expr)
8726 gfc_code *cblock;
8727 gfc_code *cnext;
8729 cblock = code->block;
8730 while (cblock)
8732 /* the assignment statement of a WHERE statement, or the first
8733 statement in where-body-construct of a WHERE construct */
8734 cnext = cblock->next;
8735 while (cnext)
8737 switch (cnext->op)
8739 /* WHERE assignment statement */
8740 case EXEC_ASSIGN:
8741 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8742 break;
8744 /* WHERE operator assignment statement */
8745 case EXEC_ASSIGN_CALL:
8746 resolve_call (cnext);
8747 if (!cnext->resolved_sym->attr.elemental)
8748 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8749 &cnext->ext.actual->expr->where);
8750 break;
8752 /* WHERE or WHERE construct is part of a where-body-construct */
8753 case EXEC_WHERE:
8754 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8755 break;
8757 default:
8758 gfc_error ("Unsupported statement inside WHERE at %L",
8759 &cnext->loc);
8761 /* the next statement within the same where-body-construct */
8762 cnext = cnext->next;
8764 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8765 cblock = cblock->block;
8770 /* Traverse the FORALL body to check whether the following errors exist:
8771 1. For assignment, check if a many-to-one assignment happens.
8772 2. For WHERE statement, check the WHERE body to see if there is any
8773 many-to-one assignment. */
8775 static void
8776 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8778 gfc_code *c;
8780 c = code->block->next;
8781 while (c)
8783 switch (c->op)
8785 case EXEC_ASSIGN:
8786 case EXEC_POINTER_ASSIGN:
8787 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8788 break;
8790 case EXEC_ASSIGN_CALL:
8791 resolve_call (c);
8792 break;
8794 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8795 there is no need to handle it here. */
8796 case EXEC_FORALL:
8797 break;
8798 case EXEC_WHERE:
8799 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8800 break;
8801 default:
8802 break;
8804 /* The next statement in the FORALL body. */
8805 c = c->next;
8810 /* Counts the number of iterators needed inside a forall construct, including
8811 nested forall constructs. This is used to allocate the needed memory
8812 in gfc_resolve_forall. */
8814 static int
8815 gfc_count_forall_iterators (gfc_code *code)
8817 int max_iters, sub_iters, current_iters;
8818 gfc_forall_iterator *fa;
8820 gcc_assert(code->op == EXEC_FORALL);
8821 max_iters = 0;
8822 current_iters = 0;
8824 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8825 current_iters ++;
8827 code = code->block->next;
8829 while (code)
8831 if (code->op == EXEC_FORALL)
8833 sub_iters = gfc_count_forall_iterators (code);
8834 if (sub_iters > max_iters)
8835 max_iters = sub_iters;
8837 code = code->next;
8840 return current_iters + max_iters;
8844 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8845 gfc_resolve_forall_body to resolve the FORALL body. */
8847 static void
8848 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8850 static gfc_expr **var_expr;
8851 static int total_var = 0;
8852 static int nvar = 0;
8853 int old_nvar, tmp;
8854 gfc_forall_iterator *fa;
8855 int i;
8857 old_nvar = nvar;
8859 /* Start to resolve a FORALL construct */
8860 if (forall_save == 0)
8862 /* Count the total number of FORALL index in the nested FORALL
8863 construct in order to allocate the VAR_EXPR with proper size. */
8864 total_var = gfc_count_forall_iterators (code);
8866 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8867 var_expr = XCNEWVEC (gfc_expr *, total_var);
8870 /* The information about FORALL iterator, including FORALL index start, end
8871 and stride. The FORALL index can not appear in start, end or stride. */
8872 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8874 /* Check if any outer FORALL index name is the same as the current
8875 one. */
8876 for (i = 0; i < nvar; i++)
8878 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8880 gfc_error ("An outer FORALL construct already has an index "
8881 "with this name %L", &fa->var->where);
8885 /* Record the current FORALL index. */
8886 var_expr[nvar] = gfc_copy_expr (fa->var);
8888 nvar++;
8890 /* No memory leak. */
8891 gcc_assert (nvar <= total_var);
8894 /* Resolve the FORALL body. */
8895 gfc_resolve_forall_body (code, nvar, var_expr);
8897 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8898 gfc_resolve_blocks (code->block, ns);
8900 tmp = nvar;
8901 nvar = old_nvar;
8902 /* Free only the VAR_EXPRs allocated in this frame. */
8903 for (i = nvar; i < tmp; i++)
8904 gfc_free_expr (var_expr[i]);
8906 if (nvar == 0)
8908 /* We are in the outermost FORALL construct. */
8909 gcc_assert (forall_save == 0);
8911 /* VAR_EXPR is not needed any more. */
8912 free (var_expr);
8913 total_var = 0;
8918 /* Resolve a BLOCK construct statement. */
8920 static void
8921 resolve_block_construct (gfc_code* code)
8923 /* Resolve the BLOCK's namespace. */
8924 gfc_resolve (code->ext.block.ns);
8926 /* For an ASSOCIATE block, the associations (and their targets) are already
8927 resolved during resolve_symbol. */
8931 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8932 DO code nodes. */
8934 static void resolve_code (gfc_code *, gfc_namespace *);
8936 void
8937 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8939 bool t;
8941 for (; b; b = b->block)
8943 t = gfc_resolve_expr (b->expr1);
8944 if (!gfc_resolve_expr (b->expr2))
8945 t = false;
8947 switch (b->op)
8949 case EXEC_IF:
8950 if (t && b->expr1 != NULL
8951 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8952 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8953 &b->expr1->where);
8954 break;
8956 case EXEC_WHERE:
8957 if (t
8958 && b->expr1 != NULL
8959 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8960 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8961 &b->expr1->where);
8962 break;
8964 case EXEC_GOTO:
8965 resolve_branch (b->label1, b);
8966 break;
8968 case EXEC_BLOCK:
8969 resolve_block_construct (b);
8970 break;
8972 case EXEC_SELECT:
8973 case EXEC_SELECT_TYPE:
8974 case EXEC_FORALL:
8975 case EXEC_DO:
8976 case EXEC_DO_WHILE:
8977 case EXEC_DO_CONCURRENT:
8978 case EXEC_CRITICAL:
8979 case EXEC_READ:
8980 case EXEC_WRITE:
8981 case EXEC_IOLENGTH:
8982 case EXEC_WAIT:
8983 break;
8985 case EXEC_OMP_ATOMIC:
8986 case EXEC_OMP_CRITICAL:
8987 case EXEC_OMP_DO:
8988 case EXEC_OMP_MASTER:
8989 case EXEC_OMP_ORDERED:
8990 case EXEC_OMP_PARALLEL:
8991 case EXEC_OMP_PARALLEL_DO:
8992 case EXEC_OMP_PARALLEL_SECTIONS:
8993 case EXEC_OMP_PARALLEL_WORKSHARE:
8994 case EXEC_OMP_SECTIONS:
8995 case EXEC_OMP_SINGLE:
8996 case EXEC_OMP_TASK:
8997 case EXEC_OMP_TASKWAIT:
8998 case EXEC_OMP_TASKYIELD:
8999 case EXEC_OMP_WORKSHARE:
9000 break;
9002 default:
9003 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9006 resolve_code (b->next, ns);
9011 /* Does everything to resolve an ordinary assignment. Returns true
9012 if this is an interface assignment. */
9013 static bool
9014 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9016 bool rval = false;
9017 gfc_expr *lhs;
9018 gfc_expr *rhs;
9019 int llen = 0;
9020 int rlen = 0;
9021 int n;
9022 gfc_ref *ref;
9023 symbol_attribute attr;
9025 if (gfc_extend_assign (code, ns))
9027 gfc_expr** rhsptr;
9029 if (code->op == EXEC_ASSIGN_CALL)
9031 lhs = code->ext.actual->expr;
9032 rhsptr = &code->ext.actual->next->expr;
9034 else
9036 gfc_actual_arglist* args;
9037 gfc_typebound_proc* tbp;
9039 gcc_assert (code->op == EXEC_COMPCALL);
9041 args = code->expr1->value.compcall.actual;
9042 lhs = args->expr;
9043 rhsptr = &args->next->expr;
9045 tbp = code->expr1->value.compcall.tbp;
9046 gcc_assert (!tbp->is_generic);
9049 /* Make a temporary rhs when there is a default initializer
9050 and rhs is the same symbol as the lhs. */
9051 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9052 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9053 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9054 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9055 *rhsptr = gfc_get_parentheses (*rhsptr);
9057 return true;
9060 lhs = code->expr1;
9061 rhs = code->expr2;
9063 if (rhs->is_boz
9064 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9065 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9066 &code->loc))
9067 return false;
9069 /* Handle the case of a BOZ literal on the RHS. */
9070 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9072 int rc;
9073 if (gfc_option.warn_surprising)
9074 gfc_warning ("BOZ literal at %L is bitwise transferred "
9075 "non-integer symbol '%s'", &code->loc,
9076 lhs->symtree->n.sym->name);
9078 if (!gfc_convert_boz (rhs, &lhs->ts))
9079 return false;
9080 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9082 if (rc == ARITH_UNDERFLOW)
9083 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9084 ". This check can be disabled with the option "
9085 "-fno-range-check", &rhs->where);
9086 else if (rc == ARITH_OVERFLOW)
9087 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9088 ". This check can be disabled with the option "
9089 "-fno-range-check", &rhs->where);
9090 else if (rc == ARITH_NAN)
9091 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9092 ". This check can be disabled with the option "
9093 "-fno-range-check", &rhs->where);
9094 return false;
9098 if (lhs->ts.type == BT_CHARACTER
9099 && gfc_option.warn_character_truncation)
9101 if (lhs->ts.u.cl != NULL
9102 && lhs->ts.u.cl->length != NULL
9103 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9104 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9106 if (rhs->expr_type == EXPR_CONSTANT)
9107 rlen = rhs->value.character.length;
9109 else if (rhs->ts.u.cl != NULL
9110 && rhs->ts.u.cl->length != NULL
9111 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9112 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9114 if (rlen && llen && rlen > llen)
9115 gfc_warning_now ("CHARACTER expression will be truncated "
9116 "in assignment (%d/%d) at %L",
9117 llen, rlen, &code->loc);
9120 /* Ensure that a vector index expression for the lvalue is evaluated
9121 to a temporary if the lvalue symbol is referenced in it. */
9122 if (lhs->rank)
9124 for (ref = lhs->ref; ref; ref= ref->next)
9125 if (ref->type == REF_ARRAY)
9127 for (n = 0; n < ref->u.ar.dimen; n++)
9128 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9129 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9130 ref->u.ar.start[n]))
9131 ref->u.ar.start[n]
9132 = gfc_get_parentheses (ref->u.ar.start[n]);
9136 if (gfc_pure (NULL))
9138 if (lhs->ts.type == BT_DERIVED
9139 && lhs->expr_type == EXPR_VARIABLE
9140 && lhs->ts.u.derived->attr.pointer_comp
9141 && rhs->expr_type == EXPR_VARIABLE
9142 && (gfc_impure_variable (rhs->symtree->n.sym)
9143 || gfc_is_coindexed (rhs)))
9145 /* F2008, C1283. */
9146 if (gfc_is_coindexed (rhs))
9147 gfc_error ("Coindexed expression at %L is assigned to "
9148 "a derived type variable with a POINTER "
9149 "component in a PURE procedure",
9150 &rhs->where);
9151 else
9152 gfc_error ("The impure variable at %L is assigned to "
9153 "a derived type variable with a POINTER "
9154 "component in a PURE procedure (12.6)",
9155 &rhs->where);
9156 return rval;
9159 /* Fortran 2008, C1283. */
9160 if (gfc_is_coindexed (lhs))
9162 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9163 "procedure", &rhs->where);
9164 return rval;
9168 if (gfc_implicit_pure (NULL))
9170 if (lhs->expr_type == EXPR_VARIABLE
9171 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9172 && lhs->symtree->n.sym->ns != gfc_current_ns)
9173 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9175 if (lhs->ts.type == BT_DERIVED
9176 && lhs->expr_type == EXPR_VARIABLE
9177 && lhs->ts.u.derived->attr.pointer_comp
9178 && rhs->expr_type == EXPR_VARIABLE
9179 && (gfc_impure_variable (rhs->symtree->n.sym)
9180 || gfc_is_coindexed (rhs)))
9181 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9183 /* Fortran 2008, C1283. */
9184 if (gfc_is_coindexed (lhs))
9185 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9188 /* F2008, 7.2.1.2. */
9189 attr = gfc_expr_attr (lhs);
9190 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9192 if (attr.codimension)
9194 gfc_error ("Assignment to polymorphic coarray at %L is not "
9195 "permitted", &lhs->where);
9196 return false;
9198 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9199 "polymorphic variable at %L", &lhs->where))
9200 return false;
9201 if (!gfc_option.flag_realloc_lhs)
9203 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9204 "requires -frealloc-lhs", &lhs->where);
9205 return false;
9207 /* See PR 43366. */
9208 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9209 "is not yet supported", &lhs->where);
9210 return false;
9212 else if (lhs->ts.type == BT_CLASS)
9214 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9215 "assignment at %L - check that there is a matching specific "
9216 "subroutine for '=' operator", &lhs->where);
9217 return false;
9220 /* F2008, Section 7.2.1.2. */
9221 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9223 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9224 "component in assignment at %L", &lhs->where);
9225 return false;
9228 gfc_check_assign (lhs, rhs, 1);
9229 return false;
9233 /* Add a component reference onto an expression. */
9235 static void
9236 add_comp_ref (gfc_expr *e, gfc_component *c)
9238 gfc_ref **ref;
9239 ref = &(e->ref);
9240 while (*ref)
9241 ref = &((*ref)->next);
9242 *ref = gfc_get_ref ();
9243 (*ref)->type = REF_COMPONENT;
9244 (*ref)->u.c.sym = e->ts.u.derived;
9245 (*ref)->u.c.component = c;
9246 e->ts = c->ts;
9248 /* Add a full array ref, as necessary. */
9249 if (c->as)
9251 gfc_add_full_array_ref (e, c->as);
9252 e->rank = c->as->rank;
9257 /* Build an assignment. Keep the argument 'op' for future use, so that
9258 pointer assignments can be made. */
9260 static gfc_code *
9261 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9262 gfc_component *comp1, gfc_component *comp2, locus loc)
9264 gfc_code *this_code;
9266 this_code = gfc_get_code (op);
9267 this_code->next = NULL;
9268 this_code->expr1 = gfc_copy_expr (expr1);
9269 this_code->expr2 = gfc_copy_expr (expr2);
9270 this_code->loc = loc;
9271 if (comp1 && comp2)
9273 add_comp_ref (this_code->expr1, comp1);
9274 add_comp_ref (this_code->expr2, comp2);
9277 return this_code;
9281 /* Makes a temporary variable expression based on the characteristics of
9282 a given variable expression. */
9284 static gfc_expr*
9285 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9287 static int serial = 0;
9288 char name[GFC_MAX_SYMBOL_LEN];
9289 gfc_symtree *tmp;
9290 gfc_array_spec *as;
9291 gfc_array_ref *aref;
9292 gfc_ref *ref;
9294 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9295 gfc_get_sym_tree (name, ns, &tmp, false);
9296 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9298 as = NULL;
9299 ref = NULL;
9300 aref = NULL;
9302 /* This function could be expanded to support other expression type
9303 but this is not needed here. */
9304 gcc_assert (e->expr_type == EXPR_VARIABLE);
9306 /* Obtain the arrayspec for the temporary. */
9307 if (e->rank)
9309 aref = gfc_find_array_ref (e);
9310 if (e->expr_type == EXPR_VARIABLE
9311 && e->symtree->n.sym->as == aref->as)
9312 as = aref->as;
9313 else
9315 for (ref = e->ref; ref; ref = ref->next)
9316 if (ref->type == REF_COMPONENT
9317 && ref->u.c.component->as == aref->as)
9319 as = aref->as;
9320 break;
9325 /* Add the attributes and the arrayspec to the temporary. */
9326 tmp->n.sym->attr = gfc_expr_attr (e);
9327 tmp->n.sym->attr.function = 0;
9328 tmp->n.sym->attr.result = 0;
9329 tmp->n.sym->attr.flavor = FL_VARIABLE;
9331 if (as)
9333 tmp->n.sym->as = gfc_copy_array_spec (as);
9334 if (!ref)
9335 ref = e->ref;
9336 if (as->type == AS_DEFERRED)
9337 tmp->n.sym->attr.allocatable = 1;
9339 else
9340 tmp->n.sym->attr.dimension = 0;
9342 gfc_set_sym_referenced (tmp->n.sym);
9343 gfc_commit_symbol (tmp->n.sym);
9344 e = gfc_lval_expr_from_sym (tmp->n.sym);
9346 /* Should the lhs be a section, use its array ref for the
9347 temporary expression. */
9348 if (aref && aref->type != AR_FULL)
9350 gfc_free_ref_list (e->ref);
9351 e->ref = gfc_copy_ref (ref);
9353 return e;
9357 /* Add one line of code to the code chain, making sure that 'head' and
9358 'tail' are appropriately updated. */
9360 static void
9361 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9363 gcc_assert (this_code);
9364 if (*head == NULL)
9365 *head = *tail = *this_code;
9366 else
9367 *tail = gfc_append_code (*tail, *this_code);
9368 *this_code = NULL;
9372 /* Counts the potential number of part array references that would
9373 result from resolution of typebound defined assignments. */
9375 static int
9376 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9378 gfc_component *c;
9379 int c_depth = 0, t_depth;
9381 for (c= derived->components; c; c = c->next)
9383 if ((c->ts.type != BT_DERIVED
9384 || c->attr.pointer
9385 || c->attr.allocatable
9386 || c->attr.proc_pointer_comp
9387 || c->attr.class_pointer
9388 || c->attr.proc_pointer)
9389 && !c->attr.defined_assign_comp)
9390 continue;
9392 if (c->as && c_depth == 0)
9393 c_depth = 1;
9395 if (c->ts.u.derived->attr.defined_assign_comp)
9396 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9397 c->as ? 1 : 0);
9398 else
9399 t_depth = 0;
9401 c_depth = t_depth > c_depth ? t_depth : c_depth;
9403 return depth + c_depth;
9407 /* Implement 7.2.1.3 of the F08 standard:
9408 "An intrinsic assignment where the variable is of derived type is
9409 performed as if each component of the variable were assigned from the
9410 corresponding component of expr using pointer assignment (7.2.2) for
9411 each pointer component, defined assignment for each nonpointer
9412 nonallocatable component of a type that has a type-bound defined
9413 assignment consistent with the component, intrinsic assignment for
9414 each other nonpointer nonallocatable component, ..."
9416 The pointer assignments are taken care of by the intrinsic
9417 assignment of the structure itself. This function recursively adds
9418 defined assignments where required. The recursion is accomplished
9419 by calling resolve_code.
9421 When the lhs in a defined assignment has intent INOUT, we need a
9422 temporary for the lhs. In pseudo-code:
9424 ! Only call function lhs once.
9425 if (lhs is not a constant or an variable)
9426 temp_x = expr2
9427 expr2 => temp_x
9428 ! Do the intrinsic assignment
9429 expr1 = expr2
9430 ! Now do the defined assignments
9431 do over components with typebound defined assignment [%cmp]
9432 #if one component's assignment procedure is INOUT
9433 t1 = expr1
9434 #if expr2 non-variable
9435 temp_x = expr2
9436 expr2 => temp_x
9437 # endif
9438 expr1 = expr2
9439 # for each cmp
9440 t1%cmp {defined=} expr2%cmp
9441 expr1%cmp = t1%cmp
9442 #else
9443 expr1 = expr2
9445 # for each cmp
9446 expr1%cmp {defined=} expr2%cmp
9447 #endif
9450 /* The temporary assignments have to be put on top of the additional
9451 code to avoid the result being changed by the intrinsic assignment.
9453 static int component_assignment_level = 0;
9454 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9456 static void
9457 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9459 gfc_component *comp1, *comp2;
9460 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9461 gfc_expr *t1;
9462 int error_count, depth;
9464 gfc_get_errors (NULL, &error_count);
9466 /* Filter out continuing processing after an error. */
9467 if (error_count
9468 || (*code)->expr1->ts.type != BT_DERIVED
9469 || (*code)->expr2->ts.type != BT_DERIVED)
9470 return;
9472 /* TODO: Handle more than one part array reference in assignments. */
9473 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9474 (*code)->expr1->rank ? 1 : 0);
9475 if (depth > 1)
9477 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9478 "done because multiple part array references would "
9479 "occur in intermediate expressions.", &(*code)->loc);
9480 return;
9483 component_assignment_level++;
9485 /* Create a temporary so that functions get called only once. */
9486 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9487 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9489 gfc_expr *tmp_expr;
9491 /* Assign the rhs to the temporary. */
9492 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9493 this_code = build_assignment (EXEC_ASSIGN,
9494 tmp_expr, (*code)->expr2,
9495 NULL, NULL, (*code)->loc);
9496 /* Add the code and substitute the rhs expression. */
9497 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9498 gfc_free_expr ((*code)->expr2);
9499 (*code)->expr2 = tmp_expr;
9502 /* Do the intrinsic assignment. This is not needed if the lhs is one
9503 of the temporaries generated here, since the intrinsic assignment
9504 to the final result already does this. */
9505 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9507 this_code = build_assignment (EXEC_ASSIGN,
9508 (*code)->expr1, (*code)->expr2,
9509 NULL, NULL, (*code)->loc);
9510 add_code_to_chain (&this_code, &head, &tail);
9513 comp1 = (*code)->expr1->ts.u.derived->components;
9514 comp2 = (*code)->expr2->ts.u.derived->components;
9516 t1 = NULL;
9517 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9519 bool inout = false;
9521 /* The intrinsic assignment does the right thing for pointers
9522 of all kinds and allocatable components. */
9523 if (comp1->ts.type != BT_DERIVED
9524 || comp1->attr.pointer
9525 || comp1->attr.allocatable
9526 || comp1->attr.proc_pointer_comp
9527 || comp1->attr.class_pointer
9528 || comp1->attr.proc_pointer)
9529 continue;
9531 /* Make an assigment for this component. */
9532 this_code = build_assignment (EXEC_ASSIGN,
9533 (*code)->expr1, (*code)->expr2,
9534 comp1, comp2, (*code)->loc);
9536 /* Convert the assignment if there is a defined assignment for
9537 this type. Otherwise, using the call from resolve_code,
9538 recurse into its components. */
9539 resolve_code (this_code, ns);
9541 if (this_code->op == EXEC_ASSIGN_CALL)
9543 gfc_formal_arglist *dummy_args;
9544 gfc_symbol *rsym;
9545 /* Check that there is a typebound defined assignment. If not,
9546 then this must be a module defined assignment. We cannot
9547 use the defined_assign_comp attribute here because it must
9548 be this derived type that has the defined assignment and not
9549 a parent type. */
9550 if (!(comp1->ts.u.derived->f2k_derived
9551 && comp1->ts.u.derived->f2k_derived
9552 ->tb_op[INTRINSIC_ASSIGN]))
9554 gfc_free_statements (this_code);
9555 this_code = NULL;
9556 continue;
9559 /* If the first argument of the subroutine has intent INOUT
9560 a temporary must be generated and used instead. */
9561 rsym = this_code->resolved_sym;
9562 dummy_args = gfc_sym_get_dummy_args (rsym);
9563 if (dummy_args
9564 && dummy_args->sym->attr.intent == INTENT_INOUT)
9566 gfc_code *temp_code;
9567 inout = true;
9569 /* Build the temporary required for the assignment and put
9570 it at the head of the generated code. */
9571 if (!t1)
9573 t1 = get_temp_from_expr ((*code)->expr1, ns);
9574 temp_code = build_assignment (EXEC_ASSIGN,
9575 t1, (*code)->expr1,
9576 NULL, NULL, (*code)->loc);
9578 /* For allocatable LHS, check whether it is allocated. Note
9579 that allocatable components with defined assignment are
9580 not yet support. See PR 57696. */
9581 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
9583 gfc_code *block;
9584 gfc_expr *e =
9585 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9586 block = gfc_get_code (EXEC_IF);
9587 block->block = gfc_get_code (EXEC_IF);
9588 block->block->expr1
9589 = gfc_build_intrinsic_call (ns,
9590 GFC_ISYM_ALLOCATED, "allocated",
9591 (*code)->loc, 1, e);
9592 block->block->next = temp_code;
9593 temp_code = block;
9595 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9598 /* Replace the first actual arg with the component of the
9599 temporary. */
9600 gfc_free_expr (this_code->ext.actual->expr);
9601 this_code->ext.actual->expr = gfc_copy_expr (t1);
9602 add_comp_ref (this_code->ext.actual->expr, comp1);
9604 /* If the LHS variable is allocatable and wasn't allocated and
9605 the temporary is allocatable, pointer assign the address of
9606 the freshly allocated LHS to the temporary. */
9607 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9608 && gfc_expr_attr ((*code)->expr1).allocatable)
9610 gfc_code *block;
9611 gfc_expr *cond;
9613 cond = gfc_get_expr ();
9614 cond->ts.type = BT_LOGICAL;
9615 cond->ts.kind = gfc_default_logical_kind;
9616 cond->expr_type = EXPR_OP;
9617 cond->where = (*code)->loc;
9618 cond->value.op.op = INTRINSIC_NOT;
9619 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
9620 GFC_ISYM_ALLOCATED, "allocated",
9621 (*code)->loc, 1, gfc_copy_expr (t1));
9622 block = gfc_get_code (EXEC_IF);
9623 block->block = gfc_get_code (EXEC_IF);
9624 block->block->expr1 = cond;
9625 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9626 t1, (*code)->expr1,
9627 NULL, NULL, (*code)->loc);
9628 add_code_to_chain (&block, &head, &tail);
9632 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9634 /* Don't add intrinsic assignments since they are already
9635 effected by the intrinsic assignment of the structure. */
9636 gfc_free_statements (this_code);
9637 this_code = NULL;
9638 continue;
9641 add_code_to_chain (&this_code, &head, &tail);
9643 if (t1 && inout)
9645 /* Transfer the value to the final result. */
9646 this_code = build_assignment (EXEC_ASSIGN,
9647 (*code)->expr1, t1,
9648 comp1, comp2, (*code)->loc);
9649 add_code_to_chain (&this_code, &head, &tail);
9653 /* Put the temporary assignments at the top of the generated code. */
9654 if (tmp_head && component_assignment_level == 1)
9656 gfc_append_code (tmp_head, head);
9657 head = tmp_head;
9658 tmp_head = tmp_tail = NULL;
9661 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9662 // not accidentally deallocated. Hence, nullify t1.
9663 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9664 && gfc_expr_attr ((*code)->expr1).allocatable)
9666 gfc_code *block;
9667 gfc_expr *cond;
9668 gfc_expr *e;
9670 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9671 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9672 (*code)->loc, 2, gfc_copy_expr (t1), e);
9673 block = gfc_get_code (EXEC_IF);
9674 block->block = gfc_get_code (EXEC_IF);
9675 block->block->expr1 = cond;
9676 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9677 t1, gfc_get_null_expr (&(*code)->loc),
9678 NULL, NULL, (*code)->loc);
9679 gfc_append_code (tail, block);
9680 tail = block;
9683 /* Now attach the remaining code chain to the input code. Step on
9684 to the end of the new code since resolution is complete. */
9685 gcc_assert ((*code)->op == EXEC_ASSIGN);
9686 tail->next = (*code)->next;
9687 /* Overwrite 'code' because this would place the intrinsic assignment
9688 before the temporary for the lhs is created. */
9689 gfc_free_expr ((*code)->expr1);
9690 gfc_free_expr ((*code)->expr2);
9691 **code = *head;
9692 if (head != tail)
9693 free (head);
9694 *code = tail;
9696 component_assignment_level--;
9700 /* Given a block of code, recursively resolve everything pointed to by this
9701 code block. */
9703 static void
9704 resolve_code (gfc_code *code, gfc_namespace *ns)
9706 int omp_workshare_save;
9707 int forall_save, do_concurrent_save;
9708 code_stack frame;
9709 bool t;
9711 frame.prev = cs_base;
9712 frame.head = code;
9713 cs_base = &frame;
9715 find_reachable_labels (code);
9717 for (; code; code = code->next)
9719 frame.current = code;
9720 forall_save = forall_flag;
9721 do_concurrent_save = gfc_do_concurrent_flag;
9723 if (code->op == EXEC_FORALL)
9725 forall_flag = 1;
9726 gfc_resolve_forall (code, ns, forall_save);
9727 forall_flag = 2;
9729 else if (code->block)
9731 omp_workshare_save = -1;
9732 switch (code->op)
9734 case EXEC_OMP_PARALLEL_WORKSHARE:
9735 omp_workshare_save = omp_workshare_flag;
9736 omp_workshare_flag = 1;
9737 gfc_resolve_omp_parallel_blocks (code, ns);
9738 break;
9739 case EXEC_OMP_PARALLEL:
9740 case EXEC_OMP_PARALLEL_DO:
9741 case EXEC_OMP_PARALLEL_SECTIONS:
9742 case EXEC_OMP_TASK:
9743 omp_workshare_save = omp_workshare_flag;
9744 omp_workshare_flag = 0;
9745 gfc_resolve_omp_parallel_blocks (code, ns);
9746 break;
9747 case EXEC_OMP_DO:
9748 gfc_resolve_omp_do_blocks (code, ns);
9749 break;
9750 case EXEC_SELECT_TYPE:
9751 /* Blocks are handled in resolve_select_type because we have
9752 to transform the SELECT TYPE into ASSOCIATE first. */
9753 break;
9754 case EXEC_DO_CONCURRENT:
9755 gfc_do_concurrent_flag = 1;
9756 gfc_resolve_blocks (code->block, ns);
9757 gfc_do_concurrent_flag = 2;
9758 break;
9759 case EXEC_OMP_WORKSHARE:
9760 omp_workshare_save = omp_workshare_flag;
9761 omp_workshare_flag = 1;
9762 /* FALL THROUGH */
9763 default:
9764 gfc_resolve_blocks (code->block, ns);
9765 break;
9768 if (omp_workshare_save != -1)
9769 omp_workshare_flag = omp_workshare_save;
9772 t = true;
9773 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9774 t = gfc_resolve_expr (code->expr1);
9775 forall_flag = forall_save;
9776 gfc_do_concurrent_flag = do_concurrent_save;
9778 if (!gfc_resolve_expr (code->expr2))
9779 t = false;
9781 if (code->op == EXEC_ALLOCATE
9782 && !gfc_resolve_expr (code->expr3))
9783 t = false;
9785 switch (code->op)
9787 case EXEC_NOP:
9788 case EXEC_END_BLOCK:
9789 case EXEC_END_NESTED_BLOCK:
9790 case EXEC_CYCLE:
9791 case EXEC_PAUSE:
9792 case EXEC_STOP:
9793 case EXEC_ERROR_STOP:
9794 case EXEC_EXIT:
9795 case EXEC_CONTINUE:
9796 case EXEC_DT_END:
9797 case EXEC_ASSIGN_CALL:
9798 case EXEC_CRITICAL:
9799 break;
9801 case EXEC_SYNC_ALL:
9802 case EXEC_SYNC_IMAGES:
9803 case EXEC_SYNC_MEMORY:
9804 resolve_sync (code);
9805 break;
9807 case EXEC_LOCK:
9808 case EXEC_UNLOCK:
9809 resolve_lock_unlock (code);
9810 break;
9812 case EXEC_ENTRY:
9813 /* Keep track of which entry we are up to. */
9814 current_entry_id = code->ext.entry->id;
9815 break;
9817 case EXEC_WHERE:
9818 resolve_where (code, NULL);
9819 break;
9821 case EXEC_GOTO:
9822 if (code->expr1 != NULL)
9824 if (code->expr1->ts.type != BT_INTEGER)
9825 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9826 "INTEGER variable", &code->expr1->where);
9827 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9828 gfc_error ("Variable '%s' has not been assigned a target "
9829 "label at %L", code->expr1->symtree->n.sym->name,
9830 &code->expr1->where);
9832 else
9833 resolve_branch (code->label1, code);
9834 break;
9836 case EXEC_RETURN:
9837 if (code->expr1 != NULL
9838 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9839 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9840 "INTEGER return specifier", &code->expr1->where);
9841 break;
9843 case EXEC_INIT_ASSIGN:
9844 case EXEC_END_PROCEDURE:
9845 break;
9847 case EXEC_ASSIGN:
9848 if (!t)
9849 break;
9851 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9852 _("assignment")))
9853 break;
9855 if (resolve_ordinary_assign (code, ns))
9857 if (code->op == EXEC_COMPCALL)
9858 goto compcall;
9859 else
9860 goto call;
9863 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9864 if (code->expr1->ts.type == BT_DERIVED
9865 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9866 generate_component_assignments (&code, ns);
9868 break;
9870 case EXEC_LABEL_ASSIGN:
9871 if (code->label1->defined == ST_LABEL_UNKNOWN)
9872 gfc_error ("Label %d referenced at %L is never defined",
9873 code->label1->value, &code->label1->where);
9874 if (t
9875 && (code->expr1->expr_type != EXPR_VARIABLE
9876 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9877 || code->expr1->symtree->n.sym->ts.kind
9878 != gfc_default_integer_kind
9879 || code->expr1->symtree->n.sym->as != NULL))
9880 gfc_error ("ASSIGN statement at %L requires a scalar "
9881 "default INTEGER variable", &code->expr1->where);
9882 break;
9884 case EXEC_POINTER_ASSIGN:
9886 gfc_expr* e;
9888 if (!t)
9889 break;
9891 /* This is both a variable definition and pointer assignment
9892 context, so check both of them. For rank remapping, a final
9893 array ref may be present on the LHS and fool gfc_expr_attr
9894 used in gfc_check_vardef_context. Remove it. */
9895 e = remove_last_array_ref (code->expr1);
9896 t = gfc_check_vardef_context (e, true, false, false,
9897 _("pointer assignment"));
9898 if (t)
9899 t = gfc_check_vardef_context (e, false, false, false,
9900 _("pointer assignment"));
9901 gfc_free_expr (e);
9902 if (!t)
9903 break;
9905 gfc_check_pointer_assign (code->expr1, code->expr2);
9906 break;
9909 case EXEC_ARITHMETIC_IF:
9910 if (t
9911 && code->expr1->ts.type != BT_INTEGER
9912 && code->expr1->ts.type != BT_REAL)
9913 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9914 "expression", &code->expr1->where);
9916 resolve_branch (code->label1, code);
9917 resolve_branch (code->label2, code);
9918 resolve_branch (code->label3, code);
9919 break;
9921 case EXEC_IF:
9922 if (t && code->expr1 != NULL
9923 && (code->expr1->ts.type != BT_LOGICAL
9924 || code->expr1->rank != 0))
9925 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9926 &code->expr1->where);
9927 break;
9929 case EXEC_CALL:
9930 call:
9931 resolve_call (code);
9932 break;
9934 case EXEC_COMPCALL:
9935 compcall:
9936 resolve_typebound_subroutine (code);
9937 break;
9939 case EXEC_CALL_PPC:
9940 resolve_ppc_call (code);
9941 break;
9943 case EXEC_SELECT:
9944 /* Select is complicated. Also, a SELECT construct could be
9945 a transformed computed GOTO. */
9946 resolve_select (code, false);
9947 break;
9949 case EXEC_SELECT_TYPE:
9950 resolve_select_type (code, ns);
9951 break;
9953 case EXEC_BLOCK:
9954 resolve_block_construct (code);
9955 break;
9957 case EXEC_DO:
9958 if (code->ext.iterator != NULL)
9960 gfc_iterator *iter = code->ext.iterator;
9961 if (gfc_resolve_iterator (iter, true, false))
9962 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9964 break;
9966 case EXEC_DO_WHILE:
9967 if (code->expr1 == NULL)
9968 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9969 if (t
9970 && (code->expr1->rank != 0
9971 || code->expr1->ts.type != BT_LOGICAL))
9972 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9973 "a scalar LOGICAL expression", &code->expr1->where);
9974 break;
9976 case EXEC_ALLOCATE:
9977 if (t)
9978 resolve_allocate_deallocate (code, "ALLOCATE");
9980 break;
9982 case EXEC_DEALLOCATE:
9983 if (t)
9984 resolve_allocate_deallocate (code, "DEALLOCATE");
9986 break;
9988 case EXEC_OPEN:
9989 if (!gfc_resolve_open (code->ext.open))
9990 break;
9992 resolve_branch (code->ext.open->err, code);
9993 break;
9995 case EXEC_CLOSE:
9996 if (!gfc_resolve_close (code->ext.close))
9997 break;
9999 resolve_branch (code->ext.close->err, code);
10000 break;
10002 case EXEC_BACKSPACE:
10003 case EXEC_ENDFILE:
10004 case EXEC_REWIND:
10005 case EXEC_FLUSH:
10006 if (!gfc_resolve_filepos (code->ext.filepos))
10007 break;
10009 resolve_branch (code->ext.filepos->err, code);
10010 break;
10012 case EXEC_INQUIRE:
10013 if (!gfc_resolve_inquire (code->ext.inquire))
10014 break;
10016 resolve_branch (code->ext.inquire->err, code);
10017 break;
10019 case EXEC_IOLENGTH:
10020 gcc_assert (code->ext.inquire != NULL);
10021 if (!gfc_resolve_inquire (code->ext.inquire))
10022 break;
10024 resolve_branch (code->ext.inquire->err, code);
10025 break;
10027 case EXEC_WAIT:
10028 if (!gfc_resolve_wait (code->ext.wait))
10029 break;
10031 resolve_branch (code->ext.wait->err, code);
10032 resolve_branch (code->ext.wait->end, code);
10033 resolve_branch (code->ext.wait->eor, code);
10034 break;
10036 case EXEC_READ:
10037 case EXEC_WRITE:
10038 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10039 break;
10041 resolve_branch (code->ext.dt->err, code);
10042 resolve_branch (code->ext.dt->end, code);
10043 resolve_branch (code->ext.dt->eor, code);
10044 break;
10046 case EXEC_TRANSFER:
10047 resolve_transfer (code);
10048 break;
10050 case EXEC_DO_CONCURRENT:
10051 case EXEC_FORALL:
10052 resolve_forall_iterators (code->ext.forall_iterator);
10054 if (code->expr1 != NULL
10055 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10056 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10057 "expression", &code->expr1->where);
10058 break;
10060 case EXEC_OMP_ATOMIC:
10061 case EXEC_OMP_BARRIER:
10062 case EXEC_OMP_CRITICAL:
10063 case EXEC_OMP_FLUSH:
10064 case EXEC_OMP_DO:
10065 case EXEC_OMP_MASTER:
10066 case EXEC_OMP_ORDERED:
10067 case EXEC_OMP_SECTIONS:
10068 case EXEC_OMP_SINGLE:
10069 case EXEC_OMP_TASKWAIT:
10070 case EXEC_OMP_TASKYIELD:
10071 case EXEC_OMP_WORKSHARE:
10072 gfc_resolve_omp_directive (code, ns);
10073 break;
10075 case EXEC_OMP_PARALLEL:
10076 case EXEC_OMP_PARALLEL_DO:
10077 case EXEC_OMP_PARALLEL_SECTIONS:
10078 case EXEC_OMP_PARALLEL_WORKSHARE:
10079 case EXEC_OMP_TASK:
10080 omp_workshare_save = omp_workshare_flag;
10081 omp_workshare_flag = 0;
10082 gfc_resolve_omp_directive (code, ns);
10083 omp_workshare_flag = omp_workshare_save;
10084 break;
10086 default:
10087 gfc_internal_error ("resolve_code(): Bad statement code");
10091 cs_base = frame.prev;
10095 /* Resolve initial values and make sure they are compatible with
10096 the variable. */
10098 static void
10099 resolve_values (gfc_symbol *sym)
10101 bool t;
10103 if (sym->value == NULL)
10104 return;
10106 if (sym->value->expr_type == EXPR_STRUCTURE)
10107 t= resolve_structure_cons (sym->value, 1);
10108 else
10109 t = gfc_resolve_expr (sym->value);
10111 if (!t)
10112 return;
10114 gfc_check_assign_symbol (sym, NULL, sym->value);
10118 /* Verify any BIND(C) derived types in the namespace so we can report errors
10119 for them once, rather than for each variable declared of that type. */
10121 static void
10122 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10124 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10125 && derived_sym->attr.is_bind_c == 1)
10126 verify_bind_c_derived_type (derived_sym);
10128 return;
10132 /* Verify that any binding labels used in a given namespace do not collide
10133 with the names or binding labels of any global symbols. Multiple INTERFACE
10134 for the same procedure are permitted. */
10136 static void
10137 gfc_verify_binding_labels (gfc_symbol *sym)
10139 gfc_gsymbol *gsym;
10140 const char *module;
10142 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10143 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10144 return;
10146 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10148 if (sym->module)
10149 module = sym->module;
10150 else if (sym->ns && sym->ns->proc_name
10151 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10152 module = sym->ns->proc_name->name;
10153 else if (sym->ns && sym->ns->parent
10154 && sym->ns && sym->ns->parent->proc_name
10155 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10156 module = sym->ns->parent->proc_name->name;
10157 else
10158 module = NULL;
10160 if (!gsym
10161 || (!gsym->defined
10162 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10164 if (!gsym)
10165 gsym = gfc_get_gsymbol (sym->binding_label);
10166 gsym->where = sym->declared_at;
10167 gsym->sym_name = sym->name;
10168 gsym->binding_label = sym->binding_label;
10169 gsym->ns = sym->ns;
10170 gsym->mod_name = module;
10171 if (sym->attr.function)
10172 gsym->type = GSYM_FUNCTION;
10173 else if (sym->attr.subroutine)
10174 gsym->type = GSYM_SUBROUTINE;
10175 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10176 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10177 return;
10180 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10182 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10183 "identifier as entity at %L", sym->name,
10184 sym->binding_label, &sym->declared_at, &gsym->where);
10185 /* Clear the binding label to prevent checking multiple times. */
10186 sym->binding_label = NULL;
10189 else if (sym->attr.flavor == FL_VARIABLE
10190 && (strcmp (module, gsym->mod_name) != 0
10191 || strcmp (sym->name, gsym->sym_name) != 0))
10193 /* This can only happen if the variable is defined in a module - if it
10194 isn't the same module, reject it. */
10195 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10196 "the same global identifier as entity at %L from module %s",
10197 sym->name, module, sym->binding_label,
10198 &sym->declared_at, &gsym->where, gsym->mod_name);
10199 sym->binding_label = NULL;
10201 else if ((sym->attr.function || sym->attr.subroutine)
10202 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10203 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10204 && sym != gsym->ns->proc_name
10205 && (module != gsym->mod_name
10206 || strcmp (gsym->sym_name, sym->name) != 0
10207 || (module && strcmp (module, gsym->mod_name) != 0)))
10209 /* Print an error if the procedure is defined multiple times; we have to
10210 exclude references to the same procedure via module association or
10211 multiple checks for the same procedure. */
10212 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10213 "global identifier as entity at %L", sym->name,
10214 sym->binding_label, &sym->declared_at, &gsym->where);
10215 sym->binding_label = NULL;
10220 /* Resolve an index expression. */
10222 static bool
10223 resolve_index_expr (gfc_expr *e)
10225 if (!gfc_resolve_expr (e))
10226 return false;
10228 if (!gfc_simplify_expr (e, 0))
10229 return false;
10231 if (!gfc_specification_expr (e))
10232 return false;
10234 return true;
10238 /* Resolve a charlen structure. */
10240 static bool
10241 resolve_charlen (gfc_charlen *cl)
10243 int i, k;
10244 bool saved_specification_expr;
10246 if (cl->resolved)
10247 return true;
10249 cl->resolved = 1;
10250 saved_specification_expr = specification_expr;
10251 specification_expr = true;
10253 if (cl->length_from_typespec)
10255 if (!gfc_resolve_expr (cl->length))
10257 specification_expr = saved_specification_expr;
10258 return false;
10261 if (!gfc_simplify_expr (cl->length, 0))
10263 specification_expr = saved_specification_expr;
10264 return false;
10267 else
10270 if (!resolve_index_expr (cl->length))
10272 specification_expr = saved_specification_expr;
10273 return false;
10277 /* "If the character length parameter value evaluates to a negative
10278 value, the length of character entities declared is zero." */
10279 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10281 if (gfc_option.warn_surprising)
10282 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10283 " the length has been set to zero",
10284 &cl->length->where, i);
10285 gfc_replace_expr (cl->length,
10286 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10289 /* Check that the character length is not too large. */
10290 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10291 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10292 && cl->length->ts.type == BT_INTEGER
10293 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10295 gfc_error ("String length at %L is too large", &cl->length->where);
10296 specification_expr = saved_specification_expr;
10297 return false;
10300 specification_expr = saved_specification_expr;
10301 return true;
10305 /* Test for non-constant shape arrays. */
10307 static bool
10308 is_non_constant_shape_array (gfc_symbol *sym)
10310 gfc_expr *e;
10311 int i;
10312 bool not_constant;
10314 not_constant = false;
10315 if (sym->as != NULL)
10317 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10318 has not been simplified; parameter array references. Do the
10319 simplification now. */
10320 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10322 e = sym->as->lower[i];
10323 if (e && (!resolve_index_expr(e)
10324 || !gfc_is_constant_expr (e)))
10325 not_constant = true;
10326 e = sym->as->upper[i];
10327 if (e && (!resolve_index_expr(e)
10328 || !gfc_is_constant_expr (e)))
10329 not_constant = true;
10332 return not_constant;
10335 /* Given a symbol and an initialization expression, add code to initialize
10336 the symbol to the function entry. */
10337 static void
10338 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10340 gfc_expr *lval;
10341 gfc_code *init_st;
10342 gfc_namespace *ns = sym->ns;
10344 /* Search for the function namespace if this is a contained
10345 function without an explicit result. */
10346 if (sym->attr.function && sym == sym->result
10347 && sym->name != sym->ns->proc_name->name)
10349 ns = ns->contained;
10350 for (;ns; ns = ns->sibling)
10351 if (strcmp (ns->proc_name->name, sym->name) == 0)
10352 break;
10355 if (ns == NULL)
10357 gfc_free_expr (init);
10358 return;
10361 /* Build an l-value expression for the result. */
10362 lval = gfc_lval_expr_from_sym (sym);
10364 /* Add the code at scope entry. */
10365 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
10366 init_st->next = ns->code;
10367 ns->code = init_st;
10369 /* Assign the default initializer to the l-value. */
10370 init_st->loc = sym->declared_at;
10371 init_st->expr1 = lval;
10372 init_st->expr2 = init;
10375 /* Assign the default initializer to a derived type variable or result. */
10377 static void
10378 apply_default_init (gfc_symbol *sym)
10380 gfc_expr *init = NULL;
10382 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10383 return;
10385 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10386 init = gfc_default_initializer (&sym->ts);
10388 if (init == NULL && sym->ts.type != BT_CLASS)
10389 return;
10391 build_init_assign (sym, init);
10392 sym->attr.referenced = 1;
10395 /* Build an initializer for a local integer, real, complex, logical, or
10396 character variable, based on the command line flags finit-local-zero,
10397 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10398 null if the symbol should not have a default initialization. */
10399 static gfc_expr *
10400 build_default_init_expr (gfc_symbol *sym)
10402 int char_len;
10403 gfc_expr *init_expr;
10404 int i;
10406 /* These symbols should never have a default initialization. */
10407 if (sym->attr.allocatable
10408 || sym->attr.external
10409 || sym->attr.dummy
10410 || sym->attr.pointer
10411 || sym->attr.in_equivalence
10412 || sym->attr.in_common
10413 || sym->attr.data
10414 || sym->module
10415 || sym->attr.cray_pointee
10416 || sym->attr.cray_pointer
10417 || sym->assoc)
10418 return NULL;
10420 /* Now we'll try to build an initializer expression. */
10421 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10422 &sym->declared_at);
10424 /* We will only initialize integers, reals, complex, logicals, and
10425 characters, and only if the corresponding command-line flags
10426 were set. Otherwise, we free init_expr and return null. */
10427 switch (sym->ts.type)
10429 case BT_INTEGER:
10430 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10431 mpz_set_si (init_expr->value.integer,
10432 gfc_option.flag_init_integer_value);
10433 else
10435 gfc_free_expr (init_expr);
10436 init_expr = NULL;
10438 break;
10440 case BT_REAL:
10441 switch (gfc_option.flag_init_real)
10443 case GFC_INIT_REAL_SNAN:
10444 init_expr->is_snan = 1;
10445 /* Fall through. */
10446 case GFC_INIT_REAL_NAN:
10447 mpfr_set_nan (init_expr->value.real);
10448 break;
10450 case GFC_INIT_REAL_INF:
10451 mpfr_set_inf (init_expr->value.real, 1);
10452 break;
10454 case GFC_INIT_REAL_NEG_INF:
10455 mpfr_set_inf (init_expr->value.real, -1);
10456 break;
10458 case GFC_INIT_REAL_ZERO:
10459 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10460 break;
10462 default:
10463 gfc_free_expr (init_expr);
10464 init_expr = NULL;
10465 break;
10467 break;
10469 case BT_COMPLEX:
10470 switch (gfc_option.flag_init_real)
10472 case GFC_INIT_REAL_SNAN:
10473 init_expr->is_snan = 1;
10474 /* Fall through. */
10475 case GFC_INIT_REAL_NAN:
10476 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10477 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10478 break;
10480 case GFC_INIT_REAL_INF:
10481 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10482 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10483 break;
10485 case GFC_INIT_REAL_NEG_INF:
10486 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10487 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10488 break;
10490 case GFC_INIT_REAL_ZERO:
10491 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10492 break;
10494 default:
10495 gfc_free_expr (init_expr);
10496 init_expr = NULL;
10497 break;
10499 break;
10501 case BT_LOGICAL:
10502 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10503 init_expr->value.logical = 0;
10504 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10505 init_expr->value.logical = 1;
10506 else
10508 gfc_free_expr (init_expr);
10509 init_expr = NULL;
10511 break;
10513 case BT_CHARACTER:
10514 /* For characters, the length must be constant in order to
10515 create a default initializer. */
10516 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10517 && sym->ts.u.cl->length
10518 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10520 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10521 init_expr->value.character.length = char_len;
10522 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10523 for (i = 0; i < char_len; i++)
10524 init_expr->value.character.string[i]
10525 = (unsigned char) gfc_option.flag_init_character_value;
10527 else
10529 gfc_free_expr (init_expr);
10530 init_expr = NULL;
10532 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10533 && sym->ts.u.cl->length && gfc_option.flag_max_stack_var_size != 0)
10535 gfc_actual_arglist *arg;
10536 init_expr = gfc_get_expr ();
10537 init_expr->where = sym->declared_at;
10538 init_expr->ts = sym->ts;
10539 init_expr->expr_type = EXPR_FUNCTION;
10540 init_expr->value.function.isym =
10541 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10542 init_expr->value.function.name = "repeat";
10543 arg = gfc_get_actual_arglist ();
10544 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10545 NULL, 1);
10546 arg->expr->value.character.string[0]
10547 = gfc_option.flag_init_character_value;
10548 arg->next = gfc_get_actual_arglist ();
10549 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10550 init_expr->value.function.actual = arg;
10552 break;
10554 default:
10555 gfc_free_expr (init_expr);
10556 init_expr = NULL;
10558 return init_expr;
10561 /* Add an initialization expression to a local variable. */
10562 static void
10563 apply_default_init_local (gfc_symbol *sym)
10565 gfc_expr *init = NULL;
10567 /* The symbol should be a variable or a function return value. */
10568 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10569 || (sym->attr.function && sym->result != sym))
10570 return;
10572 /* Try to build the initializer expression. If we can't initialize
10573 this symbol, then init will be NULL. */
10574 init = build_default_init_expr (sym);
10575 if (init == NULL)
10576 return;
10578 /* For saved variables, we don't want to add an initializer at function
10579 entry, so we just add a static initializer. Note that automatic variables
10580 are stack allocated even with -fno-automatic; we have also to exclude
10581 result variable, which are also nonstatic. */
10582 if (sym->attr.save || sym->ns->save_all
10583 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10584 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10586 /* Don't clobber an existing initializer! */
10587 gcc_assert (sym->value == NULL);
10588 sym->value = init;
10589 return;
10592 build_init_assign (sym, init);
10596 /* Resolution of common features of flavors variable and procedure. */
10598 static bool
10599 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10601 gfc_array_spec *as;
10603 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10604 as = CLASS_DATA (sym)->as;
10605 else
10606 as = sym->as;
10608 /* Constraints on deferred shape variable. */
10609 if (as == NULL || as->type != AS_DEFERRED)
10611 bool pointer, allocatable, dimension;
10613 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10615 pointer = CLASS_DATA (sym)->attr.class_pointer;
10616 allocatable = CLASS_DATA (sym)->attr.allocatable;
10617 dimension = CLASS_DATA (sym)->attr.dimension;
10619 else
10621 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10622 allocatable = sym->attr.allocatable;
10623 dimension = sym->attr.dimension;
10626 if (allocatable)
10628 if (dimension && as->type != AS_ASSUMED_RANK)
10630 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10631 "shape or assumed rank", sym->name, &sym->declared_at);
10632 return false;
10634 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10635 "'%s' at %L may not be ALLOCATABLE",
10636 sym->name, &sym->declared_at))
10637 return false;
10640 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10642 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10643 "assumed rank", sym->name, &sym->declared_at);
10644 return false;
10647 else
10649 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10650 && sym->ts.type != BT_CLASS && !sym->assoc)
10652 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10653 sym->name, &sym->declared_at);
10654 return false;
10658 /* Constraints on polymorphic variables. */
10659 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10661 /* F03:C502. */
10662 if (sym->attr.class_ok
10663 && !sym->attr.select_type_temporary
10664 && !UNLIMITED_POLY (sym)
10665 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10667 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10668 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10669 &sym->declared_at);
10670 return false;
10673 /* F03:C509. */
10674 /* Assume that use associated symbols were checked in the module ns.
10675 Class-variables that are associate-names are also something special
10676 and excepted from the test. */
10677 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10679 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10680 "or pointer", sym->name, &sym->declared_at);
10681 return false;
10685 return true;
10689 /* Additional checks for symbols with flavor variable and derived
10690 type. To be called from resolve_fl_variable. */
10692 static bool
10693 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10695 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10697 /* Check to see if a derived type is blocked from being host
10698 associated by the presence of another class I symbol in the same
10699 namespace. 14.6.1.3 of the standard and the discussion on
10700 comp.lang.fortran. */
10701 if (sym->ns != sym->ts.u.derived->ns
10702 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10704 gfc_symbol *s;
10705 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10706 if (s && s->attr.generic)
10707 s = gfc_find_dt_in_generic (s);
10708 if (s && s->attr.flavor != FL_DERIVED)
10710 gfc_error ("The type '%s' cannot be host associated at %L "
10711 "because it is blocked by an incompatible object "
10712 "of the same name declared at %L",
10713 sym->ts.u.derived->name, &sym->declared_at,
10714 &s->declared_at);
10715 return false;
10719 /* 4th constraint in section 11.3: "If an object of a type for which
10720 component-initialization is specified (R429) appears in the
10721 specification-part of a module and does not have the ALLOCATABLE
10722 or POINTER attribute, the object shall have the SAVE attribute."
10724 The check for initializers is performed with
10725 gfc_has_default_initializer because gfc_default_initializer generates
10726 a hidden default for allocatable components. */
10727 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10728 && sym->ns->proc_name->attr.flavor == FL_MODULE
10729 && !sym->ns->save_all && !sym->attr.save
10730 && !sym->attr.pointer && !sym->attr.allocatable
10731 && gfc_has_default_initializer (sym->ts.u.derived)
10732 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10733 "'%s' at %L, needed due to the default "
10734 "initialization", sym->name, &sym->declared_at))
10735 return false;
10737 /* Assign default initializer. */
10738 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10739 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10741 sym->value = gfc_default_initializer (&sym->ts);
10744 return true;
10748 /* Resolve symbols with flavor variable. */
10750 static bool
10751 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10753 int no_init_flag, automatic_flag;
10754 gfc_expr *e;
10755 const char *auto_save_msg;
10756 bool saved_specification_expr;
10758 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10759 "SAVE attribute";
10761 if (!resolve_fl_var_and_proc (sym, mp_flag))
10762 return false;
10764 /* Set this flag to check that variables are parameters of all entries.
10765 This check is effected by the call to gfc_resolve_expr through
10766 is_non_constant_shape_array. */
10767 saved_specification_expr = specification_expr;
10768 specification_expr = true;
10770 if (sym->ns->proc_name
10771 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10772 || sym->ns->proc_name->attr.is_main_program)
10773 && !sym->attr.use_assoc
10774 && !sym->attr.allocatable
10775 && !sym->attr.pointer
10776 && is_non_constant_shape_array (sym))
10778 /* The shape of a main program or module array needs to be
10779 constant. */
10780 gfc_error ("The module or main program array '%s' at %L must "
10781 "have constant shape", sym->name, &sym->declared_at);
10782 specification_expr = saved_specification_expr;
10783 return false;
10786 /* Constraints on deferred type parameter. */
10787 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10789 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10790 "requires either the pointer or allocatable attribute",
10791 sym->name, &sym->declared_at);
10792 specification_expr = saved_specification_expr;
10793 return false;
10796 if (sym->ts.type == BT_CHARACTER)
10798 /* Make sure that character string variables with assumed length are
10799 dummy arguments. */
10800 e = sym->ts.u.cl->length;
10801 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10802 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10804 gfc_error ("Entity with assumed character length at %L must be a "
10805 "dummy argument or a PARAMETER", &sym->declared_at);
10806 specification_expr = saved_specification_expr;
10807 return false;
10810 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10812 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10813 specification_expr = saved_specification_expr;
10814 return false;
10817 if (!gfc_is_constant_expr (e)
10818 && !(e->expr_type == EXPR_VARIABLE
10819 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10821 if (!sym->attr.use_assoc && sym->ns->proc_name
10822 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10823 || sym->ns->proc_name->attr.is_main_program))
10825 gfc_error ("'%s' at %L must have constant character length "
10826 "in this context", sym->name, &sym->declared_at);
10827 specification_expr = saved_specification_expr;
10828 return false;
10830 if (sym->attr.in_common)
10832 gfc_error ("COMMON variable '%s' at %L must have constant "
10833 "character length", sym->name, &sym->declared_at);
10834 specification_expr = saved_specification_expr;
10835 return false;
10840 if (sym->value == NULL && sym->attr.referenced)
10841 apply_default_init_local (sym); /* Try to apply a default initialization. */
10843 /* Determine if the symbol may not have an initializer. */
10844 no_init_flag = automatic_flag = 0;
10845 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10846 || sym->attr.intrinsic || sym->attr.result)
10847 no_init_flag = 1;
10848 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10849 && is_non_constant_shape_array (sym))
10851 no_init_flag = automatic_flag = 1;
10853 /* Also, they must not have the SAVE attribute.
10854 SAVE_IMPLICIT is checked below. */
10855 if (sym->as && sym->attr.codimension)
10857 int corank = sym->as->corank;
10858 sym->as->corank = 0;
10859 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10860 sym->as->corank = corank;
10862 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10864 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10865 specification_expr = saved_specification_expr;
10866 return false;
10870 /* Ensure that any initializer is simplified. */
10871 if (sym->value)
10872 gfc_simplify_expr (sym->value, 1);
10874 /* Reject illegal initializers. */
10875 if (!sym->mark && sym->value)
10877 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10878 && CLASS_DATA (sym)->attr.allocatable))
10879 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10880 sym->name, &sym->declared_at);
10881 else if (sym->attr.external)
10882 gfc_error ("External '%s' at %L cannot have an initializer",
10883 sym->name, &sym->declared_at);
10884 else if (sym->attr.dummy
10885 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10886 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10887 sym->name, &sym->declared_at);
10888 else if (sym->attr.intrinsic)
10889 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10890 sym->name, &sym->declared_at);
10891 else if (sym->attr.result)
10892 gfc_error ("Function result '%s' at %L cannot have an initializer",
10893 sym->name, &sym->declared_at);
10894 else if (automatic_flag)
10895 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10896 sym->name, &sym->declared_at);
10897 else
10898 goto no_init_error;
10899 specification_expr = saved_specification_expr;
10900 return false;
10903 no_init_error:
10904 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10906 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10907 specification_expr = saved_specification_expr;
10908 return res;
10911 specification_expr = saved_specification_expr;
10912 return true;
10916 /* Resolve a procedure. */
10918 static bool
10919 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10921 gfc_formal_arglist *arg;
10923 if (sym->attr.function
10924 && !resolve_fl_var_and_proc (sym, mp_flag))
10925 return false;
10927 if (sym->ts.type == BT_CHARACTER)
10929 gfc_charlen *cl = sym->ts.u.cl;
10931 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10932 && !resolve_charlen (cl))
10933 return false;
10935 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10936 && sym->attr.proc == PROC_ST_FUNCTION)
10938 gfc_error ("Character-valued statement function '%s' at %L must "
10939 "have constant length", sym->name, &sym->declared_at);
10940 return false;
10944 /* Ensure that derived type for are not of a private type. Internal
10945 module procedures are excluded by 2.2.3.3 - i.e., they are not
10946 externally accessible and can access all the objects accessible in
10947 the host. */
10948 if (!(sym->ns->parent
10949 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10950 && gfc_check_symbol_access (sym))
10952 gfc_interface *iface;
10954 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10956 if (arg->sym
10957 && arg->sym->ts.type == BT_DERIVED
10958 && !arg->sym->ts.u.derived->attr.use_assoc
10959 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10960 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10961 "and cannot be a dummy argument"
10962 " of '%s', which is PUBLIC at %L",
10963 arg->sym->name, sym->name,
10964 &sym->declared_at))
10966 /* Stop this message from recurring. */
10967 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10968 return false;
10972 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10973 PRIVATE to the containing module. */
10974 for (iface = sym->generic; iface; iface = iface->next)
10976 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10978 if (arg->sym
10979 && arg->sym->ts.type == BT_DERIVED
10980 && !arg->sym->ts.u.derived->attr.use_assoc
10981 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10982 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10983 "PUBLIC interface '%s' at %L "
10984 "takes dummy arguments of '%s' which "
10985 "is PRIVATE", iface->sym->name,
10986 sym->name, &iface->sym->declared_at,
10987 gfc_typename(&arg->sym->ts)))
10989 /* Stop this message from recurring. */
10990 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10991 return false;
10996 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10997 PRIVATE to the containing module. */
10998 for (iface = sym->generic; iface; iface = iface->next)
11000 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11002 if (arg->sym
11003 && arg->sym->ts.type == BT_DERIVED
11004 && !arg->sym->ts.u.derived->attr.use_assoc
11005 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11006 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11007 "PUBLIC interface '%s' at %L takes "
11008 "dummy arguments of '%s' which is "
11009 "PRIVATE", iface->sym->name,
11010 sym->name, &iface->sym->declared_at,
11011 gfc_typename(&arg->sym->ts)))
11013 /* Stop this message from recurring. */
11014 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11015 return false;
11021 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11022 && !sym->attr.proc_pointer)
11024 gfc_error ("Function '%s' at %L cannot have an initializer",
11025 sym->name, &sym->declared_at);
11026 return false;
11029 /* An external symbol may not have an initializer because it is taken to be
11030 a procedure. Exception: Procedure Pointers. */
11031 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11033 gfc_error ("External object '%s' at %L may not have an initializer",
11034 sym->name, &sym->declared_at);
11035 return false;
11038 /* An elemental function is required to return a scalar 12.7.1 */
11039 if (sym->attr.elemental && sym->attr.function && sym->as)
11041 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11042 "result", sym->name, &sym->declared_at);
11043 /* Reset so that the error only occurs once. */
11044 sym->attr.elemental = 0;
11045 return false;
11048 if (sym->attr.proc == PROC_ST_FUNCTION
11049 && (sym->attr.allocatable || sym->attr.pointer))
11051 gfc_error ("Statement function '%s' at %L may not have pointer or "
11052 "allocatable attribute", sym->name, &sym->declared_at);
11053 return false;
11056 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11057 char-len-param shall not be array-valued, pointer-valued, recursive
11058 or pure. ....snip... A character value of * may only be used in the
11059 following ways: (i) Dummy arg of procedure - dummy associates with
11060 actual length; (ii) To declare a named constant; or (iii) External
11061 function - but length must be declared in calling scoping unit. */
11062 if (sym->attr.function
11063 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11064 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11066 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11067 || (sym->attr.recursive) || (sym->attr.pure))
11069 if (sym->as && sym->as->rank)
11070 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11071 "array-valued", sym->name, &sym->declared_at);
11073 if (sym->attr.pointer)
11074 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11075 "pointer-valued", sym->name, &sym->declared_at);
11077 if (sym->attr.pure)
11078 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11079 "pure", sym->name, &sym->declared_at);
11081 if (sym->attr.recursive)
11082 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11083 "recursive", sym->name, &sym->declared_at);
11085 return false;
11088 /* Appendix B.2 of the standard. Contained functions give an
11089 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11090 character length is an F2003 feature. */
11091 if (!sym->attr.contained
11092 && gfc_current_form != FORM_FIXED
11093 && !sym->ts.deferred)
11094 gfc_notify_std (GFC_STD_F95_OBS,
11095 "CHARACTER(*) function '%s' at %L",
11096 sym->name, &sym->declared_at);
11099 /* F2008, C1218. */
11100 if (sym->attr.elemental)
11102 if (sym->attr.proc_pointer)
11104 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11105 sym->name, &sym->declared_at);
11106 return false;
11108 if (sym->attr.dummy)
11110 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11111 sym->name, &sym->declared_at);
11112 return false;
11116 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11118 gfc_formal_arglist *curr_arg;
11119 int has_non_interop_arg = 0;
11121 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11122 sym->common_block))
11124 /* Clear these to prevent looking at them again if there was an
11125 error. */
11126 sym->attr.is_bind_c = 0;
11127 sym->attr.is_c_interop = 0;
11128 sym->ts.is_c_interop = 0;
11130 else
11132 /* So far, no errors have been found. */
11133 sym->attr.is_c_interop = 1;
11134 sym->ts.is_c_interop = 1;
11137 curr_arg = gfc_sym_get_dummy_args (sym);
11138 while (curr_arg != NULL)
11140 /* Skip implicitly typed dummy args here. */
11141 if (curr_arg->sym->attr.implicit_type == 0)
11142 if (!gfc_verify_c_interop_param (curr_arg->sym))
11143 /* If something is found to fail, record the fact so we
11144 can mark the symbol for the procedure as not being
11145 BIND(C) to try and prevent multiple errors being
11146 reported. */
11147 has_non_interop_arg = 1;
11149 curr_arg = curr_arg->next;
11152 /* See if any of the arguments were not interoperable and if so, clear
11153 the procedure symbol to prevent duplicate error messages. */
11154 if (has_non_interop_arg != 0)
11156 sym->attr.is_c_interop = 0;
11157 sym->ts.is_c_interop = 0;
11158 sym->attr.is_bind_c = 0;
11162 if (!sym->attr.proc_pointer)
11164 if (sym->attr.save == SAVE_EXPLICIT)
11166 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11167 "in '%s' at %L", sym->name, &sym->declared_at);
11168 return false;
11170 if (sym->attr.intent)
11172 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11173 "in '%s' at %L", sym->name, &sym->declared_at);
11174 return false;
11176 if (sym->attr.subroutine && sym->attr.result)
11178 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11179 "in '%s' at %L", sym->name, &sym->declared_at);
11180 return false;
11182 if (sym->attr.external && sym->attr.function
11183 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11184 || sym->attr.contained))
11186 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11187 "in '%s' at %L", sym->name, &sym->declared_at);
11188 return false;
11190 if (strcmp ("ppr@", sym->name) == 0)
11192 gfc_error ("Procedure pointer result '%s' at %L "
11193 "is missing the pointer attribute",
11194 sym->ns->proc_name->name, &sym->declared_at);
11195 return false;
11199 return true;
11203 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11204 been defined and we now know their defined arguments, check that they fulfill
11205 the requirements of the standard for procedures used as finalizers. */
11207 static bool
11208 gfc_resolve_finalizers (gfc_symbol* derived)
11210 gfc_finalizer* list;
11211 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11212 bool result = true;
11213 bool seen_scalar = false;
11215 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11216 return true;
11218 /* Walk over the list of finalizer-procedures, check them, and if any one
11219 does not fit in with the standard's definition, print an error and remove
11220 it from the list. */
11221 prev_link = &derived->f2k_derived->finalizers;
11222 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11224 gfc_formal_arglist *dummy_args;
11225 gfc_symbol* arg;
11226 gfc_finalizer* i;
11227 int my_rank;
11229 /* Skip this finalizer if we already resolved it. */
11230 if (list->proc_tree)
11232 prev_link = &(list->next);
11233 continue;
11236 /* Check this exists and is a SUBROUTINE. */
11237 if (!list->proc_sym->attr.subroutine)
11239 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11240 list->proc_sym->name, &list->where);
11241 goto error;
11244 /* We should have exactly one argument. */
11245 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11246 if (!dummy_args || dummy_args->next)
11248 gfc_error ("FINAL procedure at %L must have exactly one argument",
11249 &list->where);
11250 goto error;
11252 arg = dummy_args->sym;
11254 /* This argument must be of our type. */
11255 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11257 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11258 &arg->declared_at, derived->name);
11259 goto error;
11262 /* It must neither be a pointer nor allocatable nor optional. */
11263 if (arg->attr.pointer)
11265 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11266 &arg->declared_at);
11267 goto error;
11269 if (arg->attr.allocatable)
11271 gfc_error ("Argument of FINAL procedure at %L must not be"
11272 " ALLOCATABLE", &arg->declared_at);
11273 goto error;
11275 if (arg->attr.optional)
11277 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11278 &arg->declared_at);
11279 goto error;
11282 /* It must not be INTENT(OUT). */
11283 if (arg->attr.intent == INTENT_OUT)
11285 gfc_error ("Argument of FINAL procedure at %L must not be"
11286 " INTENT(OUT)", &arg->declared_at);
11287 goto error;
11290 /* Warn if the procedure is non-scalar and not assumed shape. */
11291 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11292 && arg->as->type != AS_ASSUMED_SHAPE)
11293 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11294 " shape argument", &arg->declared_at);
11296 /* Check that it does not match in kind and rank with a FINAL procedure
11297 defined earlier. To really loop over the *earlier* declarations,
11298 we need to walk the tail of the list as new ones were pushed at the
11299 front. */
11300 /* TODO: Handle kind parameters once they are implemented. */
11301 my_rank = (arg->as ? arg->as->rank : 0);
11302 for (i = list->next; i; i = i->next)
11304 gfc_formal_arglist *dummy_args;
11306 /* Argument list might be empty; that is an error signalled earlier,
11307 but we nevertheless continued resolving. */
11308 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11309 if (dummy_args)
11311 gfc_symbol* i_arg = dummy_args->sym;
11312 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11313 if (i_rank == my_rank)
11315 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11316 " rank (%d) as '%s'",
11317 list->proc_sym->name, &list->where, my_rank,
11318 i->proc_sym->name);
11319 goto error;
11324 /* Is this the/a scalar finalizer procedure? */
11325 if (!arg->as || arg->as->rank == 0)
11326 seen_scalar = true;
11328 /* Find the symtree for this procedure. */
11329 gcc_assert (!list->proc_tree);
11330 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11332 prev_link = &list->next;
11333 continue;
11335 /* Remove wrong nodes immediately from the list so we don't risk any
11336 troubles in the future when they might fail later expectations. */
11337 error:
11338 result = false;
11339 i = list;
11340 *prev_link = list->next;
11341 gfc_free_finalizer (i);
11344 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11345 were nodes in the list, must have been for arrays. It is surely a good
11346 idea to have a scalar version there if there's something to finalize. */
11347 if (gfc_option.warn_surprising && result && !seen_scalar)
11348 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11349 " defined at %L, suggest also scalar one",
11350 derived->name, &derived->declared_at);
11352 gfc_find_derived_vtab (derived);
11353 return result;
11357 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11359 static bool
11360 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11361 const char* generic_name, locus where)
11363 gfc_symbol *sym1, *sym2;
11364 const char *pass1, *pass2;
11366 gcc_assert (t1->specific && t2->specific);
11367 gcc_assert (!t1->specific->is_generic);
11368 gcc_assert (!t2->specific->is_generic);
11369 gcc_assert (t1->is_operator == t2->is_operator);
11371 sym1 = t1->specific->u.specific->n.sym;
11372 sym2 = t2->specific->u.specific->n.sym;
11374 if (sym1 == sym2)
11375 return true;
11377 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11378 if (sym1->attr.subroutine != sym2->attr.subroutine
11379 || sym1->attr.function != sym2->attr.function)
11381 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11382 " GENERIC '%s' at %L",
11383 sym1->name, sym2->name, generic_name, &where);
11384 return false;
11387 /* Compare the interfaces. */
11388 if (t1->specific->nopass)
11389 pass1 = NULL;
11390 else if (t1->specific->pass_arg)
11391 pass1 = t1->specific->pass_arg;
11392 else
11393 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
11394 if (t2->specific->nopass)
11395 pass2 = NULL;
11396 else if (t2->specific->pass_arg)
11397 pass2 = t2->specific->pass_arg;
11398 else
11399 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
11400 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11401 NULL, 0, pass1, pass2))
11403 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11404 sym1->name, sym2->name, generic_name, &where);
11405 return false;
11408 return true;
11412 /* Worker function for resolving a generic procedure binding; this is used to
11413 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11415 The difference between those cases is finding possible inherited bindings
11416 that are overridden, as one has to look for them in tb_sym_root,
11417 tb_uop_root or tb_op, respectively. Thus the caller must already find
11418 the super-type and set p->overridden correctly. */
11420 static bool
11421 resolve_tb_generic_targets (gfc_symbol* super_type,
11422 gfc_typebound_proc* p, const char* name)
11424 gfc_tbp_generic* target;
11425 gfc_symtree* first_target;
11426 gfc_symtree* inherited;
11428 gcc_assert (p && p->is_generic);
11430 /* Try to find the specific bindings for the symtrees in our target-list. */
11431 gcc_assert (p->u.generic);
11432 for (target = p->u.generic; target; target = target->next)
11433 if (!target->specific)
11435 gfc_typebound_proc* overridden_tbp;
11436 gfc_tbp_generic* g;
11437 const char* target_name;
11439 target_name = target->specific_st->name;
11441 /* Defined for this type directly. */
11442 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11444 target->specific = target->specific_st->n.tb;
11445 goto specific_found;
11448 /* Look for an inherited specific binding. */
11449 if (super_type)
11451 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11452 true, NULL);
11454 if (inherited)
11456 gcc_assert (inherited->n.tb);
11457 target->specific = inherited->n.tb;
11458 goto specific_found;
11462 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11463 " at %L", target_name, name, &p->where);
11464 return false;
11466 /* Once we've found the specific binding, check it is not ambiguous with
11467 other specifics already found or inherited for the same GENERIC. */
11468 specific_found:
11469 gcc_assert (target->specific);
11471 /* This must really be a specific binding! */
11472 if (target->specific->is_generic)
11474 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11475 " '%s' is GENERIC, too", name, &p->where, target_name);
11476 return false;
11479 /* Check those already resolved on this type directly. */
11480 for (g = p->u.generic; g; g = g->next)
11481 if (g != target && g->specific
11482 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11483 return false;
11485 /* Check for ambiguity with inherited specific targets. */
11486 for (overridden_tbp = p->overridden; overridden_tbp;
11487 overridden_tbp = overridden_tbp->overridden)
11488 if (overridden_tbp->is_generic)
11490 for (g = overridden_tbp->u.generic; g; g = g->next)
11492 gcc_assert (g->specific);
11493 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11494 return false;
11499 /* If we attempt to "overwrite" a specific binding, this is an error. */
11500 if (p->overridden && !p->overridden->is_generic)
11502 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11503 " the same name", name, &p->where);
11504 return false;
11507 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11508 all must have the same attributes here. */
11509 first_target = p->u.generic->specific->u.specific;
11510 gcc_assert (first_target);
11511 p->subroutine = first_target->n.sym->attr.subroutine;
11512 p->function = first_target->n.sym->attr.function;
11514 return true;
11518 /* Resolve a GENERIC procedure binding for a derived type. */
11520 static bool
11521 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11523 gfc_symbol* super_type;
11525 /* Find the overridden binding if any. */
11526 st->n.tb->overridden = NULL;
11527 super_type = gfc_get_derived_super_type (derived);
11528 if (super_type)
11530 gfc_symtree* overridden;
11531 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11532 true, NULL);
11534 if (overridden && overridden->n.tb)
11535 st->n.tb->overridden = overridden->n.tb;
11538 /* Resolve using worker function. */
11539 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11543 /* Retrieve the target-procedure of an operator binding and do some checks in
11544 common for intrinsic and user-defined type-bound operators. */
11546 static gfc_symbol*
11547 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11549 gfc_symbol* target_proc;
11551 gcc_assert (target->specific && !target->specific->is_generic);
11552 target_proc = target->specific->u.specific->n.sym;
11553 gcc_assert (target_proc);
11555 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11556 if (target->specific->nopass)
11558 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11559 return NULL;
11562 return target_proc;
11566 /* Resolve a type-bound intrinsic operator. */
11568 static bool
11569 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11570 gfc_typebound_proc* p)
11572 gfc_symbol* super_type;
11573 gfc_tbp_generic* target;
11575 /* If there's already an error here, do nothing (but don't fail again). */
11576 if (p->error)
11577 return true;
11579 /* Operators should always be GENERIC bindings. */
11580 gcc_assert (p->is_generic);
11582 /* Look for an overridden binding. */
11583 super_type = gfc_get_derived_super_type (derived);
11584 if (super_type && super_type->f2k_derived)
11585 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11586 op, true, NULL);
11587 else
11588 p->overridden = NULL;
11590 /* Resolve general GENERIC properties using worker function. */
11591 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11592 goto error;
11594 /* Check the targets to be procedures of correct interface. */
11595 for (target = p->u.generic; target; target = target->next)
11597 gfc_symbol* target_proc;
11599 target_proc = get_checked_tb_operator_target (target, p->where);
11600 if (!target_proc)
11601 goto error;
11603 if (!gfc_check_operator_interface (target_proc, op, p->where))
11604 goto error;
11606 /* Add target to non-typebound operator list. */
11607 if (!target->specific->deferred && !derived->attr.use_assoc
11608 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11610 gfc_interface *head, *intr;
11611 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11612 return false;
11613 head = derived->ns->op[op];
11614 intr = gfc_get_interface ();
11615 intr->sym = target_proc;
11616 intr->where = p->where;
11617 intr->next = head;
11618 derived->ns->op[op] = intr;
11622 return true;
11624 error:
11625 p->error = 1;
11626 return false;
11630 /* Resolve a type-bound user operator (tree-walker callback). */
11632 static gfc_symbol* resolve_bindings_derived;
11633 static bool resolve_bindings_result;
11635 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11637 static void
11638 resolve_typebound_user_op (gfc_symtree* stree)
11640 gfc_symbol* super_type;
11641 gfc_tbp_generic* target;
11643 gcc_assert (stree && stree->n.tb);
11645 if (stree->n.tb->error)
11646 return;
11648 /* Operators should always be GENERIC bindings. */
11649 gcc_assert (stree->n.tb->is_generic);
11651 /* Find overridden procedure, if any. */
11652 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11653 if (super_type && super_type->f2k_derived)
11655 gfc_symtree* overridden;
11656 overridden = gfc_find_typebound_user_op (super_type, NULL,
11657 stree->name, true, NULL);
11659 if (overridden && overridden->n.tb)
11660 stree->n.tb->overridden = overridden->n.tb;
11662 else
11663 stree->n.tb->overridden = NULL;
11665 /* Resolve basically using worker function. */
11666 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11667 goto error;
11669 /* Check the targets to be functions of correct interface. */
11670 for (target = stree->n.tb->u.generic; target; target = target->next)
11672 gfc_symbol* target_proc;
11674 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11675 if (!target_proc)
11676 goto error;
11678 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11679 goto error;
11682 return;
11684 error:
11685 resolve_bindings_result = false;
11686 stree->n.tb->error = 1;
11690 /* Resolve the type-bound procedures for a derived type. */
11692 static void
11693 resolve_typebound_procedure (gfc_symtree* stree)
11695 gfc_symbol* proc;
11696 locus where;
11697 gfc_symbol* me_arg;
11698 gfc_symbol* super_type;
11699 gfc_component* comp;
11701 gcc_assert (stree);
11703 /* Undefined specific symbol from GENERIC target definition. */
11704 if (!stree->n.tb)
11705 return;
11707 if (stree->n.tb->error)
11708 return;
11710 /* If this is a GENERIC binding, use that routine. */
11711 if (stree->n.tb->is_generic)
11713 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11714 goto error;
11715 return;
11718 /* Get the target-procedure to check it. */
11719 gcc_assert (!stree->n.tb->is_generic);
11720 gcc_assert (stree->n.tb->u.specific);
11721 proc = stree->n.tb->u.specific->n.sym;
11722 where = stree->n.tb->where;
11724 /* Default access should already be resolved from the parser. */
11725 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11727 if (stree->n.tb->deferred)
11729 if (!check_proc_interface (proc, &where))
11730 goto error;
11732 else
11734 /* Check for F08:C465. */
11735 if ((!proc->attr.subroutine && !proc->attr.function)
11736 || (proc->attr.proc != PROC_MODULE
11737 && proc->attr.if_source != IFSRC_IFBODY)
11738 || proc->attr.abstract)
11740 gfc_error ("'%s' must be a module procedure or an external procedure with"
11741 " an explicit interface at %L", proc->name, &where);
11742 goto error;
11746 stree->n.tb->subroutine = proc->attr.subroutine;
11747 stree->n.tb->function = proc->attr.function;
11749 /* Find the super-type of the current derived type. We could do this once and
11750 store in a global if speed is needed, but as long as not I believe this is
11751 more readable and clearer. */
11752 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11754 /* If PASS, resolve and check arguments if not already resolved / loaded
11755 from a .mod file. */
11756 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11758 gfc_formal_arglist *dummy_args;
11760 dummy_args = gfc_sym_get_dummy_args (proc);
11761 if (stree->n.tb->pass_arg)
11763 gfc_formal_arglist *i;
11765 /* If an explicit passing argument name is given, walk the arg-list
11766 and look for it. */
11768 me_arg = NULL;
11769 stree->n.tb->pass_arg_num = 1;
11770 for (i = dummy_args; i; i = i->next)
11772 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11774 me_arg = i->sym;
11775 break;
11777 ++stree->n.tb->pass_arg_num;
11780 if (!me_arg)
11782 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11783 " argument '%s'",
11784 proc->name, stree->n.tb->pass_arg, &where,
11785 stree->n.tb->pass_arg);
11786 goto error;
11789 else
11791 /* Otherwise, take the first one; there should in fact be at least
11792 one. */
11793 stree->n.tb->pass_arg_num = 1;
11794 if (!dummy_args)
11796 gfc_error ("Procedure '%s' with PASS at %L must have at"
11797 " least one argument", proc->name, &where);
11798 goto error;
11800 me_arg = dummy_args->sym;
11803 /* Now check that the argument-type matches and the passed-object
11804 dummy argument is generally fine. */
11806 gcc_assert (me_arg);
11808 if (me_arg->ts.type != BT_CLASS)
11810 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11811 " at %L", proc->name, &where);
11812 goto error;
11815 if (CLASS_DATA (me_arg)->ts.u.derived
11816 != resolve_bindings_derived)
11818 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11819 " the derived-type '%s'", me_arg->name, proc->name,
11820 me_arg->name, &where, resolve_bindings_derived->name);
11821 goto error;
11824 gcc_assert (me_arg->ts.type == BT_CLASS);
11825 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11827 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11828 " scalar", proc->name, &where);
11829 goto error;
11831 if (CLASS_DATA (me_arg)->attr.allocatable)
11833 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11834 " be ALLOCATABLE", proc->name, &where);
11835 goto error;
11837 if (CLASS_DATA (me_arg)->attr.class_pointer)
11839 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11840 " be POINTER", proc->name, &where);
11841 goto error;
11845 /* If we are extending some type, check that we don't override a procedure
11846 flagged NON_OVERRIDABLE. */
11847 stree->n.tb->overridden = NULL;
11848 if (super_type)
11850 gfc_symtree* overridden;
11851 overridden = gfc_find_typebound_proc (super_type, NULL,
11852 stree->name, true, NULL);
11854 if (overridden)
11856 if (overridden->n.tb)
11857 stree->n.tb->overridden = overridden->n.tb;
11859 if (!gfc_check_typebound_override (stree, overridden))
11860 goto error;
11864 /* See if there's a name collision with a component directly in this type. */
11865 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11866 if (!strcmp (comp->name, stree->name))
11868 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11869 " '%s'",
11870 stree->name, &where, resolve_bindings_derived->name);
11871 goto error;
11874 /* Try to find a name collision with an inherited component. */
11875 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11877 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11878 " component of '%s'",
11879 stree->name, &where, resolve_bindings_derived->name);
11880 goto error;
11883 stree->n.tb->error = 0;
11884 return;
11886 error:
11887 resolve_bindings_result = false;
11888 stree->n.tb->error = 1;
11892 static bool
11893 resolve_typebound_procedures (gfc_symbol* derived)
11895 int op;
11896 gfc_symbol* super_type;
11898 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11899 return true;
11901 super_type = gfc_get_derived_super_type (derived);
11902 if (super_type)
11903 resolve_symbol (super_type);
11905 resolve_bindings_derived = derived;
11906 resolve_bindings_result = true;
11908 if (derived->f2k_derived->tb_sym_root)
11909 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11910 &resolve_typebound_procedure);
11912 if (derived->f2k_derived->tb_uop_root)
11913 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11914 &resolve_typebound_user_op);
11916 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11918 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11919 if (p && !resolve_typebound_intrinsic_op (derived,
11920 (gfc_intrinsic_op)op, p))
11921 resolve_bindings_result = false;
11924 return resolve_bindings_result;
11928 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11929 to give all identical derived types the same backend_decl. */
11930 static void
11931 add_dt_to_dt_list (gfc_symbol *derived)
11933 gfc_dt_list *dt_list;
11935 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11936 if (derived == dt_list->derived)
11937 return;
11939 dt_list = gfc_get_dt_list ();
11940 dt_list->next = gfc_derived_types;
11941 dt_list->derived = derived;
11942 gfc_derived_types = dt_list;
11946 /* Ensure that a derived-type is really not abstract, meaning that every
11947 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11949 static bool
11950 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11952 if (!st)
11953 return true;
11955 if (!ensure_not_abstract_walker (sub, st->left))
11956 return false;
11957 if (!ensure_not_abstract_walker (sub, st->right))
11958 return false;
11960 if (st->n.tb && st->n.tb->deferred)
11962 gfc_symtree* overriding;
11963 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11964 if (!overriding)
11965 return false;
11966 gcc_assert (overriding->n.tb);
11967 if (overriding->n.tb->deferred)
11969 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11970 " '%s' is DEFERRED and not overridden",
11971 sub->name, &sub->declared_at, st->name);
11972 return false;
11976 return true;
11979 static bool
11980 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11982 /* The algorithm used here is to recursively travel up the ancestry of sub
11983 and for each ancestor-type, check all bindings. If any of them is
11984 DEFERRED, look it up starting from sub and see if the found (overriding)
11985 binding is not DEFERRED.
11986 This is not the most efficient way to do this, but it should be ok and is
11987 clearer than something sophisticated. */
11989 gcc_assert (ancestor && !sub->attr.abstract);
11991 if (!ancestor->attr.abstract)
11992 return true;
11994 /* Walk bindings of this ancestor. */
11995 if (ancestor->f2k_derived)
11997 bool t;
11998 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11999 if (!t)
12000 return false;
12003 /* Find next ancestor type and recurse on it. */
12004 ancestor = gfc_get_derived_super_type (ancestor);
12005 if (ancestor)
12006 return ensure_not_abstract (sub, ancestor);
12008 return true;
12012 /* This check for typebound defined assignments is done recursively
12013 since the order in which derived types are resolved is not always in
12014 order of the declarations. */
12016 static void
12017 check_defined_assignments (gfc_symbol *derived)
12019 gfc_component *c;
12021 for (c = derived->components; c; c = c->next)
12023 if (c->ts.type != BT_DERIVED
12024 || c->attr.pointer
12025 || c->attr.allocatable
12026 || c->attr.proc_pointer_comp
12027 || c->attr.class_pointer
12028 || c->attr.proc_pointer)
12029 continue;
12031 if (c->ts.u.derived->attr.defined_assign_comp
12032 || (c->ts.u.derived->f2k_derived
12033 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12035 derived->attr.defined_assign_comp = 1;
12036 return;
12039 check_defined_assignments (c->ts.u.derived);
12040 if (c->ts.u.derived->attr.defined_assign_comp)
12042 derived->attr.defined_assign_comp = 1;
12043 return;
12049 /* Resolve the components of a derived type. This does not have to wait until
12050 resolution stage, but can be done as soon as the dt declaration has been
12051 parsed. */
12053 static bool
12054 resolve_fl_derived0 (gfc_symbol *sym)
12056 gfc_symbol* super_type;
12057 gfc_component *c;
12059 if (sym->attr.unlimited_polymorphic)
12060 return true;
12062 super_type = gfc_get_derived_super_type (sym);
12064 /* F2008, C432. */
12065 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12067 gfc_error ("As extending type '%s' at %L has a coarray component, "
12068 "parent type '%s' shall also have one", sym->name,
12069 &sym->declared_at, super_type->name);
12070 return false;
12073 /* Ensure the extended type gets resolved before we do. */
12074 if (super_type && !resolve_fl_derived0 (super_type))
12075 return false;
12077 /* An ABSTRACT type must be extensible. */
12078 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12080 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12081 sym->name, &sym->declared_at);
12082 return false;
12085 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12086 : sym->components;
12088 for ( ; c != NULL; c = c->next)
12090 if (c->attr.artificial)
12091 continue;
12093 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
12094 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
12096 gfc_error ("Deferred-length character component '%s' at %L is not "
12097 "yet supported", c->name, &c->loc);
12098 return false;
12101 /* F2008, C442. */
12102 if ((!sym->attr.is_class || c != sym->components)
12103 && c->attr.codimension
12104 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12106 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12107 "deferred shape", c->name, &c->loc);
12108 return false;
12111 /* F2008, C443. */
12112 if (c->attr.codimension && c->ts.type == BT_DERIVED
12113 && c->ts.u.derived->ts.is_iso_c)
12115 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12116 "shall not be a coarray", c->name, &c->loc);
12117 return false;
12120 /* F2008, C444. */
12121 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12122 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12123 || c->attr.allocatable))
12125 gfc_error ("Component '%s' at %L with coarray component "
12126 "shall be a nonpointer, nonallocatable scalar",
12127 c->name, &c->loc);
12128 return false;
12131 /* F2008, C448. */
12132 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12134 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12135 "is not an array pointer", c->name, &c->loc);
12136 return false;
12139 if (c->attr.proc_pointer && c->ts.interface)
12141 gfc_symbol *ifc = c->ts.interface;
12143 if (!sym->attr.vtype
12144 && !check_proc_interface (ifc, &c->loc))
12145 return false;
12147 if (ifc->attr.if_source || ifc->attr.intrinsic)
12149 /* Resolve interface and copy attributes. */
12150 if (ifc->formal && !ifc->formal_ns)
12151 resolve_symbol (ifc);
12152 if (ifc->attr.intrinsic)
12153 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12155 if (ifc->result)
12157 c->ts = ifc->result->ts;
12158 c->attr.allocatable = ifc->result->attr.allocatable;
12159 c->attr.pointer = ifc->result->attr.pointer;
12160 c->attr.dimension = ifc->result->attr.dimension;
12161 c->as = gfc_copy_array_spec (ifc->result->as);
12162 c->attr.class_ok = ifc->result->attr.class_ok;
12164 else
12166 c->ts = ifc->ts;
12167 c->attr.allocatable = ifc->attr.allocatable;
12168 c->attr.pointer = ifc->attr.pointer;
12169 c->attr.dimension = ifc->attr.dimension;
12170 c->as = gfc_copy_array_spec (ifc->as);
12171 c->attr.class_ok = ifc->attr.class_ok;
12173 c->ts.interface = ifc;
12174 c->attr.function = ifc->attr.function;
12175 c->attr.subroutine = ifc->attr.subroutine;
12177 c->attr.pure = ifc->attr.pure;
12178 c->attr.elemental = ifc->attr.elemental;
12179 c->attr.recursive = ifc->attr.recursive;
12180 c->attr.always_explicit = ifc->attr.always_explicit;
12181 c->attr.ext_attr |= ifc->attr.ext_attr;
12182 /* Copy char length. */
12183 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12185 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12186 if (cl->length && !cl->resolved
12187 && !gfc_resolve_expr (cl->length))
12188 return false;
12189 c->ts.u.cl = cl;
12193 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12195 /* Since PPCs are not implicitly typed, a PPC without an explicit
12196 interface must be a subroutine. */
12197 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12200 /* Procedure pointer components: Check PASS arg. */
12201 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12202 && !sym->attr.vtype)
12204 gfc_symbol* me_arg;
12206 if (c->tb->pass_arg)
12208 gfc_formal_arglist* i;
12210 /* If an explicit passing argument name is given, walk the arg-list
12211 and look for it. */
12213 me_arg = NULL;
12214 c->tb->pass_arg_num = 1;
12215 for (i = c->ts.interface->formal; i; i = i->next)
12217 if (!strcmp (i->sym->name, c->tb->pass_arg))
12219 me_arg = i->sym;
12220 break;
12222 c->tb->pass_arg_num++;
12225 if (!me_arg)
12227 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12228 "at %L has no argument '%s'", c->name,
12229 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12230 c->tb->error = 1;
12231 return false;
12234 else
12236 /* Otherwise, take the first one; there should in fact be at least
12237 one. */
12238 c->tb->pass_arg_num = 1;
12239 if (!c->ts.interface->formal)
12241 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12242 "must have at least one argument",
12243 c->name, &c->loc);
12244 c->tb->error = 1;
12245 return false;
12247 me_arg = c->ts.interface->formal->sym;
12250 /* Now check that the argument-type matches. */
12251 gcc_assert (me_arg);
12252 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12253 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12254 || (me_arg->ts.type == BT_CLASS
12255 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12257 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12258 " the derived type '%s'", me_arg->name, c->name,
12259 me_arg->name, &c->loc, sym->name);
12260 c->tb->error = 1;
12261 return false;
12264 /* Check for C453. */
12265 if (me_arg->attr.dimension)
12267 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12268 "must be scalar", me_arg->name, c->name, me_arg->name,
12269 &c->loc);
12270 c->tb->error = 1;
12271 return false;
12274 if (me_arg->attr.pointer)
12276 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12277 "may not have the POINTER attribute", me_arg->name,
12278 c->name, me_arg->name, &c->loc);
12279 c->tb->error = 1;
12280 return false;
12283 if (me_arg->attr.allocatable)
12285 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12286 "may not be ALLOCATABLE", me_arg->name, c->name,
12287 me_arg->name, &c->loc);
12288 c->tb->error = 1;
12289 return false;
12292 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12293 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12294 " at %L", c->name, &c->loc);
12298 /* Check type-spec if this is not the parent-type component. */
12299 if (((sym->attr.is_class
12300 && (!sym->components->ts.u.derived->attr.extension
12301 || c != sym->components->ts.u.derived->components))
12302 || (!sym->attr.is_class
12303 && (!sym->attr.extension || c != sym->components)))
12304 && !sym->attr.vtype
12305 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12306 return false;
12308 /* If this type is an extension, set the accessibility of the parent
12309 component. */
12310 if (super_type
12311 && ((sym->attr.is_class
12312 && c == sym->components->ts.u.derived->components)
12313 || (!sym->attr.is_class && c == sym->components))
12314 && strcmp (super_type->name, c->name) == 0)
12315 c->attr.access = super_type->attr.access;
12317 /* If this type is an extension, see if this component has the same name
12318 as an inherited type-bound procedure. */
12319 if (super_type && !sym->attr.is_class
12320 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12322 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12323 " inherited type-bound procedure",
12324 c->name, sym->name, &c->loc);
12325 return false;
12328 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12329 && !c->ts.deferred)
12331 if (c->ts.u.cl->length == NULL
12332 || (!resolve_charlen(c->ts.u.cl))
12333 || !gfc_is_constant_expr (c->ts.u.cl->length))
12335 gfc_error ("Character length of component '%s' needs to "
12336 "be a constant specification expression at %L",
12337 c->name,
12338 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12339 return false;
12343 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12344 && !c->attr.pointer && !c->attr.allocatable)
12346 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12347 "length must be a POINTER or ALLOCATABLE",
12348 c->name, sym->name, &c->loc);
12349 return false;
12352 if (c->ts.type == BT_DERIVED
12353 && sym->component_access != ACCESS_PRIVATE
12354 && gfc_check_symbol_access (sym)
12355 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12356 && !c->ts.u.derived->attr.use_assoc
12357 && !gfc_check_symbol_access (c->ts.u.derived)
12358 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12359 "PRIVATE type and cannot be a component of "
12360 "'%s', which is PUBLIC at %L", c->name,
12361 sym->name, &sym->declared_at))
12362 return false;
12364 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12366 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12367 "type %s", c->name, &c->loc, sym->name);
12368 return false;
12371 if (sym->attr.sequence)
12373 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12375 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12376 "not have the SEQUENCE attribute",
12377 c->ts.u.derived->name, &sym->declared_at);
12378 return false;
12382 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12383 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12384 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12385 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12386 CLASS_DATA (c)->ts.u.derived
12387 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12389 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12390 && c->attr.pointer && c->ts.u.derived->components == NULL
12391 && !c->ts.u.derived->attr.zero_comp)
12393 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12394 "that has not been declared", c->name, sym->name,
12395 &c->loc);
12396 return false;
12399 if (c->ts.type == BT_CLASS && c->attr.class_ok
12400 && CLASS_DATA (c)->attr.class_pointer
12401 && CLASS_DATA (c)->ts.u.derived->components == NULL
12402 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12403 && !UNLIMITED_POLY (c))
12405 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12406 "that has not been declared", c->name, sym->name,
12407 &c->loc);
12408 return false;
12411 /* C437. */
12412 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12413 && (!c->attr.class_ok
12414 || !(CLASS_DATA (c)->attr.class_pointer
12415 || CLASS_DATA (c)->attr.allocatable)))
12417 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12418 "or pointer", c->name, &c->loc);
12419 /* Prevent a recurrence of the error. */
12420 c->ts.type = BT_UNKNOWN;
12421 return false;
12424 /* Ensure that all the derived type components are put on the
12425 derived type list; even in formal namespaces, where derived type
12426 pointer components might not have been declared. */
12427 if (c->ts.type == BT_DERIVED
12428 && c->ts.u.derived
12429 && c->ts.u.derived->components
12430 && c->attr.pointer
12431 && sym != c->ts.u.derived)
12432 add_dt_to_dt_list (c->ts.u.derived);
12434 if (!gfc_resolve_array_spec (c->as,
12435 !(c->attr.pointer || c->attr.proc_pointer
12436 || c->attr.allocatable)))
12437 return false;
12439 if (c->initializer && !sym->attr.vtype
12440 && !gfc_check_assign_symbol (sym, c, c->initializer))
12441 return false;
12444 check_defined_assignments (sym);
12446 if (!sym->attr.defined_assign_comp && super_type)
12447 sym->attr.defined_assign_comp
12448 = super_type->attr.defined_assign_comp;
12450 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12451 all DEFERRED bindings are overridden. */
12452 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12453 && !sym->attr.is_class
12454 && !ensure_not_abstract (sym, super_type))
12455 return false;
12457 /* Add derived type to the derived type list. */
12458 add_dt_to_dt_list (sym);
12460 return true;
12464 /* The following procedure does the full resolution of a derived type,
12465 including resolution of all type-bound procedures (if present). In contrast
12466 to 'resolve_fl_derived0' this can only be done after the module has been
12467 parsed completely. */
12469 static bool
12470 resolve_fl_derived (gfc_symbol *sym)
12472 gfc_symbol *gen_dt = NULL;
12474 if (sym->attr.unlimited_polymorphic)
12475 return true;
12477 if (!sym->attr.is_class)
12478 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12479 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12480 && (!gen_dt->generic->sym->attr.use_assoc
12481 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12482 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12483 "'%s' at %L being the same name as derived "
12484 "type at %L", sym->name,
12485 gen_dt->generic->sym == sym
12486 ? gen_dt->generic->next->sym->name
12487 : gen_dt->generic->sym->name,
12488 gen_dt->generic->sym == sym
12489 ? &gen_dt->generic->next->sym->declared_at
12490 : &gen_dt->generic->sym->declared_at,
12491 &sym->declared_at))
12492 return false;
12494 /* Resolve the finalizer procedures. */
12495 if (!gfc_resolve_finalizers (sym))
12496 return false;
12498 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12500 /* Fix up incomplete CLASS symbols. */
12501 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12502 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12504 /* Nothing more to do for unlimited polymorphic entities. */
12505 if (data->ts.u.derived->attr.unlimited_polymorphic)
12506 return true;
12507 else if (vptr->ts.u.derived == NULL)
12509 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12510 gcc_assert (vtab);
12511 vptr->ts.u.derived = vtab->ts.u.derived;
12515 if (!resolve_fl_derived0 (sym))
12516 return false;
12518 /* Resolve the type-bound procedures. */
12519 if (!resolve_typebound_procedures (sym))
12520 return false;
12522 return true;
12526 static bool
12527 resolve_fl_namelist (gfc_symbol *sym)
12529 gfc_namelist *nl;
12530 gfc_symbol *nlsym;
12532 for (nl = sym->namelist; nl; nl = nl->next)
12534 /* Check again, the check in match only works if NAMELIST comes
12535 after the decl. */
12536 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12538 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12539 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12540 return false;
12543 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12544 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12545 "with assumed shape in namelist '%s' at %L",
12546 nl->sym->name, sym->name, &sym->declared_at))
12547 return false;
12549 if (is_non_constant_shape_array (nl->sym)
12550 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12551 "with nonconstant shape in namelist '%s' at %L",
12552 nl->sym->name, sym->name, &sym->declared_at))
12553 return false;
12555 if (nl->sym->ts.type == BT_CHARACTER
12556 && (nl->sym->ts.u.cl->length == NULL
12557 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12558 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12559 "nonconstant character length in "
12560 "namelist '%s' at %L", nl->sym->name,
12561 sym->name, &sym->declared_at))
12562 return false;
12564 /* FIXME: Once UDDTIO is implemented, the following can be
12565 removed. */
12566 if (nl->sym->ts.type == BT_CLASS)
12568 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12569 "polymorphic and requires a defined input/output "
12570 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12571 return false;
12574 if (nl->sym->ts.type == BT_DERIVED
12575 && (nl->sym->ts.u.derived->attr.alloc_comp
12576 || nl->sym->ts.u.derived->attr.pointer_comp))
12578 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12579 "namelist '%s' at %L with ALLOCATABLE "
12580 "or POINTER components", nl->sym->name,
12581 sym->name, &sym->declared_at))
12582 return false;
12584 /* FIXME: Once UDDTIO is implemented, the following can be
12585 removed. */
12586 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12587 "ALLOCATABLE or POINTER components and thus requires "
12588 "a defined input/output procedure", nl->sym->name,
12589 sym->name, &sym->declared_at);
12590 return false;
12594 /* Reject PRIVATE objects in a PUBLIC namelist. */
12595 if (gfc_check_symbol_access (sym))
12597 for (nl = sym->namelist; nl; nl = nl->next)
12599 if (!nl->sym->attr.use_assoc
12600 && !is_sym_host_assoc (nl->sym, sym->ns)
12601 && !gfc_check_symbol_access (nl->sym))
12603 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12604 "cannot be member of PUBLIC namelist '%s' at %L",
12605 nl->sym->name, sym->name, &sym->declared_at);
12606 return false;
12609 /* Types with private components that came here by USE-association. */
12610 if (nl->sym->ts.type == BT_DERIVED
12611 && derived_inaccessible (nl->sym->ts.u.derived))
12613 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12614 "components and cannot be member of namelist '%s' at %L",
12615 nl->sym->name, sym->name, &sym->declared_at);
12616 return false;
12619 /* Types with private components that are defined in the same module. */
12620 if (nl->sym->ts.type == BT_DERIVED
12621 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12622 && nl->sym->ts.u.derived->attr.private_comp)
12624 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12625 "cannot be a member of PUBLIC namelist '%s' at %L",
12626 nl->sym->name, sym->name, &sym->declared_at);
12627 return false;
12633 /* 14.1.2 A module or internal procedure represent local entities
12634 of the same type as a namelist member and so are not allowed. */
12635 for (nl = sym->namelist; nl; nl = nl->next)
12637 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12638 continue;
12640 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12641 if ((nl->sym == sym->ns->proc_name)
12643 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12644 continue;
12646 nlsym = NULL;
12647 if (nl->sym->name)
12648 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12649 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12651 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12652 "attribute in '%s' at %L", nlsym->name,
12653 &sym->declared_at);
12654 return false;
12658 return true;
12662 static bool
12663 resolve_fl_parameter (gfc_symbol *sym)
12665 /* A parameter array's shape needs to be constant. */
12666 if (sym->as != NULL
12667 && (sym->as->type == AS_DEFERRED
12668 || is_non_constant_shape_array (sym)))
12670 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12671 "or of deferred shape", sym->name, &sym->declared_at);
12672 return false;
12675 /* Make sure a parameter that has been implicitly typed still
12676 matches the implicit type, since PARAMETER statements can precede
12677 IMPLICIT statements. */
12678 if (sym->attr.implicit_type
12679 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12680 sym->ns)))
12682 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12683 "later IMPLICIT type", sym->name, &sym->declared_at);
12684 return false;
12687 /* Make sure the types of derived parameters are consistent. This
12688 type checking is deferred until resolution because the type may
12689 refer to a derived type from the host. */
12690 if (sym->ts.type == BT_DERIVED
12691 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12693 gfc_error ("Incompatible derived type in PARAMETER at %L",
12694 &sym->value->where);
12695 return false;
12697 return true;
12701 /* Do anything necessary to resolve a symbol. Right now, we just
12702 assume that an otherwise unknown symbol is a variable. This sort
12703 of thing commonly happens for symbols in module. */
12705 static void
12706 resolve_symbol (gfc_symbol *sym)
12708 int check_constant, mp_flag;
12709 gfc_symtree *symtree;
12710 gfc_symtree *this_symtree;
12711 gfc_namespace *ns;
12712 gfc_component *c;
12713 symbol_attribute class_attr;
12714 gfc_array_spec *as;
12715 bool saved_specification_expr;
12717 if (sym->resolved)
12718 return;
12719 sym->resolved = 1;
12721 if (sym->attr.artificial)
12722 return;
12724 if (sym->attr.unlimited_polymorphic)
12725 return;
12727 if (sym->attr.flavor == FL_UNKNOWN
12728 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12729 && !sym->attr.generic && !sym->attr.external
12730 && sym->attr.if_source == IFSRC_UNKNOWN
12731 && sym->ts.type == BT_UNKNOWN))
12734 /* If we find that a flavorless symbol is an interface in one of the
12735 parent namespaces, find its symtree in this namespace, free the
12736 symbol and set the symtree to point to the interface symbol. */
12737 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12739 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12740 if (symtree && (symtree->n.sym->generic ||
12741 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12742 && sym->ns->construct_entities)))
12744 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12745 sym->name);
12746 gfc_release_symbol (sym);
12747 symtree->n.sym->refs++;
12748 this_symtree->n.sym = symtree->n.sym;
12749 return;
12753 /* Otherwise give it a flavor according to such attributes as
12754 it has. */
12755 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12756 && sym->attr.intrinsic == 0)
12757 sym->attr.flavor = FL_VARIABLE;
12758 else if (sym->attr.flavor == FL_UNKNOWN)
12760 sym->attr.flavor = FL_PROCEDURE;
12761 if (sym->attr.dimension)
12762 sym->attr.function = 1;
12766 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12767 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12769 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12770 && !resolve_procedure_interface (sym))
12771 return;
12773 if (sym->attr.is_protected && !sym->attr.proc_pointer
12774 && (sym->attr.procedure || sym->attr.external))
12776 if (sym->attr.external)
12777 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12778 "at %L", &sym->declared_at);
12779 else
12780 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12781 "at %L", &sym->declared_at);
12783 return;
12786 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12787 return;
12789 /* Symbols that are module procedures with results (functions) have
12790 the types and array specification copied for type checking in
12791 procedures that call them, as well as for saving to a module
12792 file. These symbols can't stand the scrutiny that their results
12793 can. */
12794 mp_flag = (sym->result != NULL && sym->result != sym);
12796 /* Make sure that the intrinsic is consistent with its internal
12797 representation. This needs to be done before assigning a default
12798 type to avoid spurious warnings. */
12799 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12800 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12801 return;
12803 /* Resolve associate names. */
12804 if (sym->assoc)
12805 resolve_assoc_var (sym, true);
12807 /* Assign default type to symbols that need one and don't have one. */
12808 if (sym->ts.type == BT_UNKNOWN)
12810 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12812 gfc_set_default_type (sym, 1, NULL);
12815 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12816 && !sym->attr.function && !sym->attr.subroutine
12817 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12818 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12820 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12822 /* The specific case of an external procedure should emit an error
12823 in the case that there is no implicit type. */
12824 if (!mp_flag)
12825 gfc_set_default_type (sym, sym->attr.external, NULL);
12826 else
12828 /* Result may be in another namespace. */
12829 resolve_symbol (sym->result);
12831 if (!sym->result->attr.proc_pointer)
12833 sym->ts = sym->result->ts;
12834 sym->as = gfc_copy_array_spec (sym->result->as);
12835 sym->attr.dimension = sym->result->attr.dimension;
12836 sym->attr.pointer = sym->result->attr.pointer;
12837 sym->attr.allocatable = sym->result->attr.allocatable;
12838 sym->attr.contiguous = sym->result->attr.contiguous;
12843 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12845 bool saved_specification_expr = specification_expr;
12846 specification_expr = true;
12847 gfc_resolve_array_spec (sym->result->as, false);
12848 specification_expr = saved_specification_expr;
12851 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12853 as = CLASS_DATA (sym)->as;
12854 class_attr = CLASS_DATA (sym)->attr;
12855 class_attr.pointer = class_attr.class_pointer;
12857 else
12859 class_attr = sym->attr;
12860 as = sym->as;
12863 /* F2008, C530. */
12864 if (sym->attr.contiguous
12865 && (!class_attr.dimension
12866 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12867 && !class_attr.pointer)))
12869 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12870 "array pointer or an assumed-shape or assumed-rank array",
12871 sym->name, &sym->declared_at);
12872 return;
12875 /* Assumed size arrays and assumed shape arrays must be dummy
12876 arguments. Array-spec's of implied-shape should have been resolved to
12877 AS_EXPLICIT already. */
12879 if (as)
12881 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12882 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12883 || as->type == AS_ASSUMED_SHAPE)
12884 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12886 if (as->type == AS_ASSUMED_SIZE)
12887 gfc_error ("Assumed size array at %L must be a dummy argument",
12888 &sym->declared_at);
12889 else
12890 gfc_error ("Assumed shape array at %L must be a dummy argument",
12891 &sym->declared_at);
12892 return;
12894 /* TS 29113, C535a. */
12895 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12896 && !sym->attr.select_type_temporary)
12898 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12899 &sym->declared_at);
12900 return;
12902 if (as->type == AS_ASSUMED_RANK
12903 && (sym->attr.codimension || sym->attr.value))
12905 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12906 "CODIMENSION attribute", &sym->declared_at);
12907 return;
12911 /* Make sure symbols with known intent or optional are really dummy
12912 variable. Because of ENTRY statement, this has to be deferred
12913 until resolution time. */
12915 if (!sym->attr.dummy
12916 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12918 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12919 return;
12922 if (sym->attr.value && !sym->attr.dummy)
12924 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12925 "it is not a dummy argument", sym->name, &sym->declared_at);
12926 return;
12929 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12931 gfc_charlen *cl = sym->ts.u.cl;
12932 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12934 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12935 "attribute must have constant length",
12936 sym->name, &sym->declared_at);
12937 return;
12940 if (sym->ts.is_c_interop
12941 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12943 gfc_error ("C interoperable character dummy variable '%s' at %L "
12944 "with VALUE attribute must have length one",
12945 sym->name, &sym->declared_at);
12946 return;
12950 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12951 && sym->ts.u.derived->attr.generic)
12953 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12954 if (!sym->ts.u.derived)
12956 gfc_error ("The derived type '%s' at %L is of type '%s', "
12957 "which has not been defined", sym->name,
12958 &sym->declared_at, sym->ts.u.derived->name);
12959 sym->ts.type = BT_UNKNOWN;
12960 return;
12964 /* Use the same constraints as TYPE(*), except for the type check
12965 and that only scalars and assumed-size arrays are permitted. */
12966 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12968 if (!sym->attr.dummy)
12970 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12971 "a dummy argument", sym->name, &sym->declared_at);
12972 return;
12975 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12976 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12977 && sym->ts.type != BT_COMPLEX)
12979 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12980 "of type TYPE(*) or of an numeric intrinsic type",
12981 sym->name, &sym->declared_at);
12982 return;
12985 if (sym->attr.allocatable || sym->attr.codimension
12986 || sym->attr.pointer || sym->attr.value)
12988 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12989 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12990 "attribute", sym->name, &sym->declared_at);
12991 return;
12994 if (sym->attr.intent == INTENT_OUT)
12996 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12997 "have the INTENT(OUT) attribute",
12998 sym->name, &sym->declared_at);
12999 return;
13001 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13003 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13004 "either be a scalar or an assumed-size array",
13005 sym->name, &sym->declared_at);
13006 return;
13009 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13010 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13011 packing. */
13012 sym->ts.type = BT_ASSUMED;
13013 sym->as = gfc_get_array_spec ();
13014 sym->as->type = AS_ASSUMED_SIZE;
13015 sym->as->rank = 1;
13016 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13018 else if (sym->ts.type == BT_ASSUMED)
13020 /* TS 29113, C407a. */
13021 if (!sym->attr.dummy)
13023 gfc_error ("Assumed type of variable %s at %L is only permitted "
13024 "for dummy variables", sym->name, &sym->declared_at);
13025 return;
13027 if (sym->attr.allocatable || sym->attr.codimension
13028 || sym->attr.pointer || sym->attr.value)
13030 gfc_error ("Assumed-type variable %s at %L may not have the "
13031 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13032 sym->name, &sym->declared_at);
13033 return;
13035 if (sym->attr.intent == INTENT_OUT)
13037 gfc_error ("Assumed-type variable %s at %L may not have the "
13038 "INTENT(OUT) attribute",
13039 sym->name, &sym->declared_at);
13040 return;
13042 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13044 gfc_error ("Assumed-type variable %s at %L shall not be an "
13045 "explicit-shape array", sym->name, &sym->declared_at);
13046 return;
13050 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13051 do this for something that was implicitly typed because that is handled
13052 in gfc_set_default_type. Handle dummy arguments and procedure
13053 definitions separately. Also, anything that is use associated is not
13054 handled here but instead is handled in the module it is declared in.
13055 Finally, derived type definitions are allowed to be BIND(C) since that
13056 only implies that they're interoperable, and they are checked fully for
13057 interoperability when a variable is declared of that type. */
13058 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13059 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13060 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13062 bool t = true;
13064 /* First, make sure the variable is declared at the
13065 module-level scope (J3/04-007, Section 15.3). */
13066 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13067 sym->attr.in_common == 0)
13069 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13070 "is neither a COMMON block nor declared at the "
13071 "module level scope", sym->name, &(sym->declared_at));
13072 t = false;
13074 else if (sym->common_head != NULL)
13076 t = verify_com_block_vars_c_interop (sym->common_head);
13078 else
13080 /* If type() declaration, we need to verify that the components
13081 of the given type are all C interoperable, etc. */
13082 if (sym->ts.type == BT_DERIVED &&
13083 sym->ts.u.derived->attr.is_c_interop != 1)
13085 /* Make sure the user marked the derived type as BIND(C). If
13086 not, call the verify routine. This could print an error
13087 for the derived type more than once if multiple variables
13088 of that type are declared. */
13089 if (sym->ts.u.derived->attr.is_bind_c != 1)
13090 verify_bind_c_derived_type (sym->ts.u.derived);
13091 t = false;
13094 /* Verify the variable itself as C interoperable if it
13095 is BIND(C). It is not possible for this to succeed if
13096 the verify_bind_c_derived_type failed, so don't have to handle
13097 any error returned by verify_bind_c_derived_type. */
13098 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13099 sym->common_block);
13102 if (!t)
13104 /* clear the is_bind_c flag to prevent reporting errors more than
13105 once if something failed. */
13106 sym->attr.is_bind_c = 0;
13107 return;
13111 /* If a derived type symbol has reached this point, without its
13112 type being declared, we have an error. Notice that most
13113 conditions that produce undefined derived types have already
13114 been dealt with. However, the likes of:
13115 implicit type(t) (t) ..... call foo (t) will get us here if
13116 the type is not declared in the scope of the implicit
13117 statement. Change the type to BT_UNKNOWN, both because it is so
13118 and to prevent an ICE. */
13119 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13120 && sym->ts.u.derived->components == NULL
13121 && !sym->ts.u.derived->attr.zero_comp)
13123 gfc_error ("The derived type '%s' at %L is of type '%s', "
13124 "which has not been defined", sym->name,
13125 &sym->declared_at, sym->ts.u.derived->name);
13126 sym->ts.type = BT_UNKNOWN;
13127 return;
13130 /* Make sure that the derived type has been resolved and that the
13131 derived type is visible in the symbol's namespace, if it is a
13132 module function and is not PRIVATE. */
13133 if (sym->ts.type == BT_DERIVED
13134 && sym->ts.u.derived->attr.use_assoc
13135 && sym->ns->proc_name
13136 && sym->ns->proc_name->attr.flavor == FL_MODULE
13137 && !resolve_fl_derived (sym->ts.u.derived))
13138 return;
13140 /* Unless the derived-type declaration is use associated, Fortran 95
13141 does not allow public entries of private derived types.
13142 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13143 161 in 95-006r3. */
13144 if (sym->ts.type == BT_DERIVED
13145 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13146 && !sym->ts.u.derived->attr.use_assoc
13147 && gfc_check_symbol_access (sym)
13148 && !gfc_check_symbol_access (sym->ts.u.derived)
13149 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13150 "derived type '%s'",
13151 (sym->attr.flavor == FL_PARAMETER)
13152 ? "parameter" : "variable",
13153 sym->name, &sym->declared_at,
13154 sym->ts.u.derived->name))
13155 return;
13157 /* F2008, C1302. */
13158 if (sym->ts.type == BT_DERIVED
13159 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13160 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13161 || sym->ts.u.derived->attr.lock_comp)
13162 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13164 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13165 "type LOCK_TYPE must be a coarray", sym->name,
13166 &sym->declared_at);
13167 return;
13170 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13171 default initialization is defined (5.1.2.4.4). */
13172 if (sym->ts.type == BT_DERIVED
13173 && sym->attr.dummy
13174 && sym->attr.intent == INTENT_OUT
13175 && sym->as
13176 && sym->as->type == AS_ASSUMED_SIZE)
13178 for (c = sym->ts.u.derived->components; c; c = c->next)
13180 if (c->initializer)
13182 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13183 "ASSUMED SIZE and so cannot have a default initializer",
13184 sym->name, &sym->declared_at);
13185 return;
13190 /* F2008, C542. */
13191 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13192 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13194 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13195 "INTENT(OUT)", sym->name, &sym->declared_at);
13196 return;
13199 /* F2008, C525. */
13200 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13201 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13202 && CLASS_DATA (sym)->attr.coarray_comp))
13203 || class_attr.codimension)
13204 && (sym->attr.result || sym->result == sym))
13206 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13207 "a coarray component", sym->name, &sym->declared_at);
13208 return;
13211 /* F2008, C524. */
13212 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13213 && sym->ts.u.derived->ts.is_iso_c)
13215 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13216 "shall not be a coarray", sym->name, &sym->declared_at);
13217 return;
13220 /* F2008, C525. */
13221 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13222 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13223 && CLASS_DATA (sym)->attr.coarray_comp))
13224 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13225 || class_attr.allocatable))
13227 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13228 "nonpointer, nonallocatable scalar, which is not a coarray",
13229 sym->name, &sym->declared_at);
13230 return;
13233 /* F2008, C526. The function-result case was handled above. */
13234 if (class_attr.codimension
13235 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13236 || sym->attr.select_type_temporary
13237 || sym->ns->save_all
13238 || sym->ns->proc_name->attr.flavor == FL_MODULE
13239 || sym->ns->proc_name->attr.is_main_program
13240 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13242 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13243 "nor a dummy argument", sym->name, &sym->declared_at);
13244 return;
13246 /* F2008, C528. */
13247 else if (class_attr.codimension && !sym->attr.select_type_temporary
13248 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13250 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13251 "deferred shape", sym->name, &sym->declared_at);
13252 return;
13254 else if (class_attr.codimension && class_attr.allocatable && as
13255 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13257 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13258 "deferred shape", sym->name, &sym->declared_at);
13259 return;
13262 /* F2008, C541. */
13263 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13264 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13265 && CLASS_DATA (sym)->attr.coarray_comp))
13266 || (class_attr.codimension && class_attr.allocatable))
13267 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13269 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13270 "allocatable coarray or have coarray components",
13271 sym->name, &sym->declared_at);
13272 return;
13275 if (class_attr.codimension && sym->attr.dummy
13276 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13278 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13279 "procedure '%s'", sym->name, &sym->declared_at,
13280 sym->ns->proc_name->name);
13281 return;
13284 if (sym->ts.type == BT_LOGICAL
13285 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13286 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13287 && sym->ns->proc_name->attr.is_bind_c)))
13289 int i;
13290 for (i = 0; gfc_logical_kinds[i].kind; i++)
13291 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13292 break;
13293 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13294 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13295 "%L with non-C_Bool kind in BIND(C) procedure "
13296 "'%s'", sym->name, &sym->declared_at,
13297 sym->ns->proc_name->name))
13298 return;
13299 else if (!gfc_logical_kinds[i].c_bool
13300 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13301 "'%s' at %L with non-C_Bool kind in "
13302 "BIND(C) procedure '%s'", sym->name,
13303 &sym->declared_at,
13304 sym->attr.function ? sym->name
13305 : sym->ns->proc_name->name))
13306 return;
13309 switch (sym->attr.flavor)
13311 case FL_VARIABLE:
13312 if (!resolve_fl_variable (sym, mp_flag))
13313 return;
13314 break;
13316 case FL_PROCEDURE:
13317 if (!resolve_fl_procedure (sym, mp_flag))
13318 return;
13319 break;
13321 case FL_NAMELIST:
13322 if (!resolve_fl_namelist (sym))
13323 return;
13324 break;
13326 case FL_PARAMETER:
13327 if (!resolve_fl_parameter (sym))
13328 return;
13329 break;
13331 default:
13332 break;
13335 /* Resolve array specifier. Check as well some constraints
13336 on COMMON blocks. */
13338 check_constant = sym->attr.in_common && !sym->attr.pointer;
13340 /* Set the formal_arg_flag so that check_conflict will not throw
13341 an error for host associated variables in the specification
13342 expression for an array_valued function. */
13343 if (sym->attr.function && sym->as)
13344 formal_arg_flag = 1;
13346 saved_specification_expr = specification_expr;
13347 specification_expr = true;
13348 gfc_resolve_array_spec (sym->as, check_constant);
13349 specification_expr = saved_specification_expr;
13351 formal_arg_flag = 0;
13353 /* Resolve formal namespaces. */
13354 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13355 && !sym->attr.contained && !sym->attr.intrinsic)
13356 gfc_resolve (sym->formal_ns);
13358 /* Make sure the formal namespace is present. */
13359 if (sym->formal && !sym->formal_ns)
13361 gfc_formal_arglist *formal = sym->formal;
13362 while (formal && !formal->sym)
13363 formal = formal->next;
13365 if (formal)
13367 sym->formal_ns = formal->sym->ns;
13368 if (sym->ns != formal->sym->ns)
13369 sym->formal_ns->refs++;
13373 /* Check threadprivate restrictions. */
13374 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13375 && (!sym->attr.in_common
13376 && sym->module == NULL
13377 && (sym->ns->proc_name == NULL
13378 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13379 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13381 /* If we have come this far we can apply default-initializers, as
13382 described in 14.7.5, to those variables that have not already
13383 been assigned one. */
13384 if (sym->ts.type == BT_DERIVED
13385 && !sym->value
13386 && !sym->attr.allocatable
13387 && !sym->attr.alloc_comp)
13389 symbol_attribute *a = &sym->attr;
13391 if ((!a->save && !a->dummy && !a->pointer
13392 && !a->in_common && !a->use_assoc
13393 && (a->referenced || a->result)
13394 && !(a->function && sym != sym->result))
13395 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13396 apply_default_init (sym);
13399 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13400 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13401 && !CLASS_DATA (sym)->attr.class_pointer
13402 && !CLASS_DATA (sym)->attr.allocatable)
13403 apply_default_init (sym);
13405 /* If this symbol has a type-spec, check it. */
13406 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13407 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13408 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13409 return;
13413 /************* Resolve DATA statements *************/
13415 static struct
13417 gfc_data_value *vnode;
13418 mpz_t left;
13420 values;
13423 /* Advance the values structure to point to the next value in the data list. */
13425 static bool
13426 next_data_value (void)
13428 while (mpz_cmp_ui (values.left, 0) == 0)
13431 if (values.vnode->next == NULL)
13432 return false;
13434 values.vnode = values.vnode->next;
13435 mpz_set (values.left, values.vnode->repeat);
13438 return true;
13442 static bool
13443 check_data_variable (gfc_data_variable *var, locus *where)
13445 gfc_expr *e;
13446 mpz_t size;
13447 mpz_t offset;
13448 bool t;
13449 ar_type mark = AR_UNKNOWN;
13450 int i;
13451 mpz_t section_index[GFC_MAX_DIMENSIONS];
13452 gfc_ref *ref;
13453 gfc_array_ref *ar;
13454 gfc_symbol *sym;
13455 int has_pointer;
13457 if (!gfc_resolve_expr (var->expr))
13458 return false;
13460 ar = NULL;
13461 mpz_init_set_si (offset, 0);
13462 e = var->expr;
13464 if (e->expr_type != EXPR_VARIABLE)
13465 gfc_internal_error ("check_data_variable(): Bad expression");
13467 sym = e->symtree->n.sym;
13469 if (sym->ns->is_block_data && !sym->attr.in_common)
13471 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13472 sym->name, &sym->declared_at);
13475 if (e->ref == NULL && sym->as)
13477 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13478 " declaration", sym->name, where);
13479 return false;
13482 has_pointer = sym->attr.pointer;
13484 if (gfc_is_coindexed (e))
13486 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13487 where);
13488 return false;
13491 for (ref = e->ref; ref; ref = ref->next)
13493 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13494 has_pointer = 1;
13496 if (has_pointer
13497 && ref->type == REF_ARRAY
13498 && ref->u.ar.type != AR_FULL)
13500 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13501 "be a full array", sym->name, where);
13502 return false;
13506 if (e->rank == 0 || has_pointer)
13508 mpz_init_set_ui (size, 1);
13509 ref = NULL;
13511 else
13513 ref = e->ref;
13515 /* Find the array section reference. */
13516 for (ref = e->ref; ref; ref = ref->next)
13518 if (ref->type != REF_ARRAY)
13519 continue;
13520 if (ref->u.ar.type == AR_ELEMENT)
13521 continue;
13522 break;
13524 gcc_assert (ref);
13526 /* Set marks according to the reference pattern. */
13527 switch (ref->u.ar.type)
13529 case AR_FULL:
13530 mark = AR_FULL;
13531 break;
13533 case AR_SECTION:
13534 ar = &ref->u.ar;
13535 /* Get the start position of array section. */
13536 gfc_get_section_index (ar, section_index, &offset);
13537 mark = AR_SECTION;
13538 break;
13540 default:
13541 gcc_unreachable ();
13544 if (!gfc_array_size (e, &size))
13546 gfc_error ("Nonconstant array section at %L in DATA statement",
13547 &e->where);
13548 mpz_clear (offset);
13549 return false;
13553 t = true;
13555 while (mpz_cmp_ui (size, 0) > 0)
13557 if (!next_data_value ())
13559 gfc_error ("DATA statement at %L has more variables than values",
13560 where);
13561 t = false;
13562 break;
13565 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13566 if (!t)
13567 break;
13569 /* If we have more than one element left in the repeat count,
13570 and we have more than one element left in the target variable,
13571 then create a range assignment. */
13572 /* FIXME: Only done for full arrays for now, since array sections
13573 seem tricky. */
13574 if (mark == AR_FULL && ref && ref->next == NULL
13575 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13577 mpz_t range;
13579 if (mpz_cmp (size, values.left) >= 0)
13581 mpz_init_set (range, values.left);
13582 mpz_sub (size, size, values.left);
13583 mpz_set_ui (values.left, 0);
13585 else
13587 mpz_init_set (range, size);
13588 mpz_sub (values.left, values.left, size);
13589 mpz_set_ui (size, 0);
13592 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13593 offset, &range);
13595 mpz_add (offset, offset, range);
13596 mpz_clear (range);
13598 if (!t)
13599 break;
13602 /* Assign initial value to symbol. */
13603 else
13605 mpz_sub_ui (values.left, values.left, 1);
13606 mpz_sub_ui (size, size, 1);
13608 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13609 offset, NULL);
13610 if (!t)
13611 break;
13613 if (mark == AR_FULL)
13614 mpz_add_ui (offset, offset, 1);
13616 /* Modify the array section indexes and recalculate the offset
13617 for next element. */
13618 else if (mark == AR_SECTION)
13619 gfc_advance_section (section_index, ar, &offset);
13623 if (mark == AR_SECTION)
13625 for (i = 0; i < ar->dimen; i++)
13626 mpz_clear (section_index[i]);
13629 mpz_clear (size);
13630 mpz_clear (offset);
13632 return t;
13636 static bool traverse_data_var (gfc_data_variable *, locus *);
13638 /* Iterate over a list of elements in a DATA statement. */
13640 static bool
13641 traverse_data_list (gfc_data_variable *var, locus *where)
13643 mpz_t trip;
13644 iterator_stack frame;
13645 gfc_expr *e, *start, *end, *step;
13646 bool retval = true;
13648 mpz_init (frame.value);
13649 mpz_init (trip);
13651 start = gfc_copy_expr (var->iter.start);
13652 end = gfc_copy_expr (var->iter.end);
13653 step = gfc_copy_expr (var->iter.step);
13655 if (!gfc_simplify_expr (start, 1)
13656 || start->expr_type != EXPR_CONSTANT)
13658 gfc_error ("start of implied-do loop at %L could not be "
13659 "simplified to a constant value", &start->where);
13660 retval = false;
13661 goto cleanup;
13663 if (!gfc_simplify_expr (end, 1)
13664 || end->expr_type != EXPR_CONSTANT)
13666 gfc_error ("end of implied-do loop at %L could not be "
13667 "simplified to a constant value", &start->where);
13668 retval = false;
13669 goto cleanup;
13671 if (!gfc_simplify_expr (step, 1)
13672 || step->expr_type != EXPR_CONSTANT)
13674 gfc_error ("step of implied-do loop at %L could not be "
13675 "simplified to a constant value", &start->where);
13676 retval = false;
13677 goto cleanup;
13680 mpz_set (trip, end->value.integer);
13681 mpz_sub (trip, trip, start->value.integer);
13682 mpz_add (trip, trip, step->value.integer);
13684 mpz_div (trip, trip, step->value.integer);
13686 mpz_set (frame.value, start->value.integer);
13688 frame.prev = iter_stack;
13689 frame.variable = var->iter.var->symtree;
13690 iter_stack = &frame;
13692 while (mpz_cmp_ui (trip, 0) > 0)
13694 if (!traverse_data_var (var->list, where))
13696 retval = false;
13697 goto cleanup;
13700 e = gfc_copy_expr (var->expr);
13701 if (!gfc_simplify_expr (e, 1))
13703 gfc_free_expr (e);
13704 retval = false;
13705 goto cleanup;
13708 mpz_add (frame.value, frame.value, step->value.integer);
13710 mpz_sub_ui (trip, trip, 1);
13713 cleanup:
13714 mpz_clear (frame.value);
13715 mpz_clear (trip);
13717 gfc_free_expr (start);
13718 gfc_free_expr (end);
13719 gfc_free_expr (step);
13721 iter_stack = frame.prev;
13722 return retval;
13726 /* Type resolve variables in the variable list of a DATA statement. */
13728 static bool
13729 traverse_data_var (gfc_data_variable *var, locus *where)
13731 bool t;
13733 for (; var; var = var->next)
13735 if (var->expr == NULL)
13736 t = traverse_data_list (var, where);
13737 else
13738 t = check_data_variable (var, where);
13740 if (!t)
13741 return false;
13744 return true;
13748 /* Resolve the expressions and iterators associated with a data statement.
13749 This is separate from the assignment checking because data lists should
13750 only be resolved once. */
13752 static bool
13753 resolve_data_variables (gfc_data_variable *d)
13755 for (; d; d = d->next)
13757 if (d->list == NULL)
13759 if (!gfc_resolve_expr (d->expr))
13760 return false;
13762 else
13764 if (!gfc_resolve_iterator (&d->iter, false, true))
13765 return false;
13767 if (!resolve_data_variables (d->list))
13768 return false;
13772 return true;
13776 /* Resolve a single DATA statement. We implement this by storing a pointer to
13777 the value list into static variables, and then recursively traversing the
13778 variables list, expanding iterators and such. */
13780 static void
13781 resolve_data (gfc_data *d)
13784 if (!resolve_data_variables (d->var))
13785 return;
13787 values.vnode = d->value;
13788 if (d->value == NULL)
13789 mpz_set_ui (values.left, 0);
13790 else
13791 mpz_set (values.left, d->value->repeat);
13793 if (!traverse_data_var (d->var, &d->where))
13794 return;
13796 /* At this point, we better not have any values left. */
13798 if (next_data_value ())
13799 gfc_error ("DATA statement at %L has more values than variables",
13800 &d->where);
13804 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13805 accessed by host or use association, is a dummy argument to a pure function,
13806 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13807 is storage associated with any such variable, shall not be used in the
13808 following contexts: (clients of this function). */
13810 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13811 procedure. Returns zero if assignment is OK, nonzero if there is a
13812 problem. */
13814 gfc_impure_variable (gfc_symbol *sym)
13816 gfc_symbol *proc;
13817 gfc_namespace *ns;
13819 if (sym->attr.use_assoc || sym->attr.in_common)
13820 return 1;
13822 /* Check if the symbol's ns is inside the pure procedure. */
13823 for (ns = gfc_current_ns; ns; ns = ns->parent)
13825 if (ns == sym->ns)
13826 break;
13827 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13828 return 1;
13831 proc = sym->ns->proc_name;
13832 if (sym->attr.dummy
13833 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13834 || proc->attr.function))
13835 return 1;
13837 /* TODO: Sort out what can be storage associated, if anything, and include
13838 it here. In principle equivalences should be scanned but it does not
13839 seem to be possible to storage associate an impure variable this way. */
13840 return 0;
13844 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13845 current namespace is inside a pure procedure. */
13848 gfc_pure (gfc_symbol *sym)
13850 symbol_attribute attr;
13851 gfc_namespace *ns;
13853 if (sym == NULL)
13855 /* Check if the current namespace or one of its parents
13856 belongs to a pure procedure. */
13857 for (ns = gfc_current_ns; ns; ns = ns->parent)
13859 sym = ns->proc_name;
13860 if (sym == NULL)
13861 return 0;
13862 attr = sym->attr;
13863 if (attr.flavor == FL_PROCEDURE && attr.pure)
13864 return 1;
13866 return 0;
13869 attr = sym->attr;
13871 return attr.flavor == FL_PROCEDURE && attr.pure;
13875 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13876 checks if the current namespace is implicitly pure. Note that this
13877 function returns false for a PURE procedure. */
13880 gfc_implicit_pure (gfc_symbol *sym)
13882 gfc_namespace *ns;
13884 if (sym == NULL)
13886 /* Check if the current procedure is implicit_pure. Walk up
13887 the procedure list until we find a procedure. */
13888 for (ns = gfc_current_ns; ns; ns = ns->parent)
13890 sym = ns->proc_name;
13891 if (sym == NULL)
13892 return 0;
13894 if (sym->attr.flavor == FL_PROCEDURE)
13895 break;
13899 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13900 && !sym->attr.pure;
13904 /* Test whether the current procedure is elemental or not. */
13907 gfc_elemental (gfc_symbol *sym)
13909 symbol_attribute attr;
13911 if (sym == NULL)
13912 sym = gfc_current_ns->proc_name;
13913 if (sym == NULL)
13914 return 0;
13915 attr = sym->attr;
13917 return attr.flavor == FL_PROCEDURE && attr.elemental;
13921 /* Warn about unused labels. */
13923 static void
13924 warn_unused_fortran_label (gfc_st_label *label)
13926 if (label == NULL)
13927 return;
13929 warn_unused_fortran_label (label->left);
13931 if (label->defined == ST_LABEL_UNKNOWN)
13932 return;
13934 switch (label->referenced)
13936 case ST_LABEL_UNKNOWN:
13937 gfc_warning ("Label %d at %L defined but not used", label->value,
13938 &label->where);
13939 break;
13941 case ST_LABEL_BAD_TARGET:
13942 gfc_warning ("Label %d at %L defined but cannot be used",
13943 label->value, &label->where);
13944 break;
13946 default:
13947 break;
13950 warn_unused_fortran_label (label->right);
13954 /* Returns the sequence type of a symbol or sequence. */
13956 static seq_type
13957 sequence_type (gfc_typespec ts)
13959 seq_type result;
13960 gfc_component *c;
13962 switch (ts.type)
13964 case BT_DERIVED:
13966 if (ts.u.derived->components == NULL)
13967 return SEQ_NONDEFAULT;
13969 result = sequence_type (ts.u.derived->components->ts);
13970 for (c = ts.u.derived->components->next; c; c = c->next)
13971 if (sequence_type (c->ts) != result)
13972 return SEQ_MIXED;
13974 return result;
13976 case BT_CHARACTER:
13977 if (ts.kind != gfc_default_character_kind)
13978 return SEQ_NONDEFAULT;
13980 return SEQ_CHARACTER;
13982 case BT_INTEGER:
13983 if (ts.kind != gfc_default_integer_kind)
13984 return SEQ_NONDEFAULT;
13986 return SEQ_NUMERIC;
13988 case BT_REAL:
13989 if (!(ts.kind == gfc_default_real_kind
13990 || ts.kind == gfc_default_double_kind))
13991 return SEQ_NONDEFAULT;
13993 return SEQ_NUMERIC;
13995 case BT_COMPLEX:
13996 if (ts.kind != gfc_default_complex_kind)
13997 return SEQ_NONDEFAULT;
13999 return SEQ_NUMERIC;
14001 case BT_LOGICAL:
14002 if (ts.kind != gfc_default_logical_kind)
14003 return SEQ_NONDEFAULT;
14005 return SEQ_NUMERIC;
14007 default:
14008 return SEQ_NONDEFAULT;
14013 /* Resolve derived type EQUIVALENCE object. */
14015 static bool
14016 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14018 gfc_component *c = derived->components;
14020 if (!derived)
14021 return true;
14023 /* Shall not be an object of nonsequence derived type. */
14024 if (!derived->attr.sequence)
14026 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14027 "attribute to be an EQUIVALENCE object", sym->name,
14028 &e->where);
14029 return false;
14032 /* Shall not have allocatable components. */
14033 if (derived->attr.alloc_comp)
14035 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14036 "components to be an EQUIVALENCE object",sym->name,
14037 &e->where);
14038 return false;
14041 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14043 gfc_error ("Derived type variable '%s' at %L with default "
14044 "initialization cannot be in EQUIVALENCE with a variable "
14045 "in COMMON", sym->name, &e->where);
14046 return false;
14049 for (; c ; c = c->next)
14051 if (c->ts.type == BT_DERIVED
14052 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14053 return false;
14055 /* Shall not be an object of sequence derived type containing a pointer
14056 in the structure. */
14057 if (c->attr.pointer)
14059 gfc_error ("Derived type variable '%s' at %L with pointer "
14060 "component(s) cannot be an EQUIVALENCE object",
14061 sym->name, &e->where);
14062 return false;
14065 return true;
14069 /* Resolve equivalence object.
14070 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14071 an allocatable array, an object of nonsequence derived type, an object of
14072 sequence derived type containing a pointer at any level of component
14073 selection, an automatic object, a function name, an entry name, a result
14074 name, a named constant, a structure component, or a subobject of any of
14075 the preceding objects. A substring shall not have length zero. A
14076 derived type shall not have components with default initialization nor
14077 shall two objects of an equivalence group be initialized.
14078 Either all or none of the objects shall have an protected attribute.
14079 The simple constraints are done in symbol.c(check_conflict) and the rest
14080 are implemented here. */
14082 static void
14083 resolve_equivalence (gfc_equiv *eq)
14085 gfc_symbol *sym;
14086 gfc_symbol *first_sym;
14087 gfc_expr *e;
14088 gfc_ref *r;
14089 locus *last_where = NULL;
14090 seq_type eq_type, last_eq_type;
14091 gfc_typespec *last_ts;
14092 int object, cnt_protected;
14093 const char *msg;
14095 last_ts = &eq->expr->symtree->n.sym->ts;
14097 first_sym = eq->expr->symtree->n.sym;
14099 cnt_protected = 0;
14101 for (object = 1; eq; eq = eq->eq, object++)
14103 e = eq->expr;
14105 e->ts = e->symtree->n.sym->ts;
14106 /* match_varspec might not know yet if it is seeing
14107 array reference or substring reference, as it doesn't
14108 know the types. */
14109 if (e->ref && e->ref->type == REF_ARRAY)
14111 gfc_ref *ref = e->ref;
14112 sym = e->symtree->n.sym;
14114 if (sym->attr.dimension)
14116 ref->u.ar.as = sym->as;
14117 ref = ref->next;
14120 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14121 if (e->ts.type == BT_CHARACTER
14122 && ref
14123 && ref->type == REF_ARRAY
14124 && ref->u.ar.dimen == 1
14125 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14126 && ref->u.ar.stride[0] == NULL)
14128 gfc_expr *start = ref->u.ar.start[0];
14129 gfc_expr *end = ref->u.ar.end[0];
14130 void *mem = NULL;
14132 /* Optimize away the (:) reference. */
14133 if (start == NULL && end == NULL)
14135 if (e->ref == ref)
14136 e->ref = ref->next;
14137 else
14138 e->ref->next = ref->next;
14139 mem = ref;
14141 else
14143 ref->type = REF_SUBSTRING;
14144 if (start == NULL)
14145 start = gfc_get_int_expr (gfc_default_integer_kind,
14146 NULL, 1);
14147 ref->u.ss.start = start;
14148 if (end == NULL && e->ts.u.cl)
14149 end = gfc_copy_expr (e->ts.u.cl->length);
14150 ref->u.ss.end = end;
14151 ref->u.ss.length = e->ts.u.cl;
14152 e->ts.u.cl = NULL;
14154 ref = ref->next;
14155 free (mem);
14158 /* Any further ref is an error. */
14159 if (ref)
14161 gcc_assert (ref->type == REF_ARRAY);
14162 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14163 &ref->u.ar.where);
14164 continue;
14168 if (!gfc_resolve_expr (e))
14169 continue;
14171 sym = e->symtree->n.sym;
14173 if (sym->attr.is_protected)
14174 cnt_protected++;
14175 if (cnt_protected > 0 && cnt_protected != object)
14177 gfc_error ("Either all or none of the objects in the "
14178 "EQUIVALENCE set at %L shall have the "
14179 "PROTECTED attribute",
14180 &e->where);
14181 break;
14184 /* Shall not equivalence common block variables in a PURE procedure. */
14185 if (sym->ns->proc_name
14186 && sym->ns->proc_name->attr.pure
14187 && sym->attr.in_common)
14189 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14190 "object in the pure procedure '%s'",
14191 sym->name, &e->where, sym->ns->proc_name->name);
14192 break;
14195 /* Shall not be a named constant. */
14196 if (e->expr_type == EXPR_CONSTANT)
14198 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14199 "object", sym->name, &e->where);
14200 continue;
14203 if (e->ts.type == BT_DERIVED
14204 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14205 continue;
14207 /* Check that the types correspond correctly:
14208 Note 5.28:
14209 A numeric sequence structure may be equivalenced to another sequence
14210 structure, an object of default integer type, default real type, double
14211 precision real type, default logical type such that components of the
14212 structure ultimately only become associated to objects of the same
14213 kind. A character sequence structure may be equivalenced to an object
14214 of default character kind or another character sequence structure.
14215 Other objects may be equivalenced only to objects of the same type and
14216 kind parameters. */
14218 /* Identical types are unconditionally OK. */
14219 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14220 goto identical_types;
14222 last_eq_type = sequence_type (*last_ts);
14223 eq_type = sequence_type (sym->ts);
14225 /* Since the pair of objects is not of the same type, mixed or
14226 non-default sequences can be rejected. */
14228 msg = "Sequence %s with mixed components in EQUIVALENCE "
14229 "statement at %L with different type objects";
14230 if ((object ==2
14231 && last_eq_type == SEQ_MIXED
14232 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14233 || (eq_type == SEQ_MIXED
14234 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14235 continue;
14237 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14238 "statement at %L with objects of different type";
14239 if ((object ==2
14240 && last_eq_type == SEQ_NONDEFAULT
14241 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14242 || (eq_type == SEQ_NONDEFAULT
14243 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14244 continue;
14246 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14247 "EQUIVALENCE statement at %L";
14248 if (last_eq_type == SEQ_CHARACTER
14249 && eq_type != SEQ_CHARACTER
14250 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14251 continue;
14253 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14254 "EQUIVALENCE statement at %L";
14255 if (last_eq_type == SEQ_NUMERIC
14256 && eq_type != SEQ_NUMERIC
14257 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14258 continue;
14260 identical_types:
14261 last_ts =&sym->ts;
14262 last_where = &e->where;
14264 if (!e->ref)
14265 continue;
14267 /* Shall not be an automatic array. */
14268 if (e->ref->type == REF_ARRAY
14269 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14271 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14272 "an EQUIVALENCE object", sym->name, &e->where);
14273 continue;
14276 r = e->ref;
14277 while (r)
14279 /* Shall not be a structure component. */
14280 if (r->type == REF_COMPONENT)
14282 gfc_error ("Structure component '%s' at %L cannot be an "
14283 "EQUIVALENCE object",
14284 r->u.c.component->name, &e->where);
14285 break;
14288 /* A substring shall not have length zero. */
14289 if (r->type == REF_SUBSTRING)
14291 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14293 gfc_error ("Substring at %L has length zero",
14294 &r->u.ss.start->where);
14295 break;
14298 r = r->next;
14304 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14306 static void
14307 resolve_fntype (gfc_namespace *ns)
14309 gfc_entry_list *el;
14310 gfc_symbol *sym;
14312 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14313 return;
14315 /* If there are any entries, ns->proc_name is the entry master
14316 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14317 if (ns->entries)
14318 sym = ns->entries->sym;
14319 else
14320 sym = ns->proc_name;
14321 if (sym->result == sym
14322 && sym->ts.type == BT_UNKNOWN
14323 && !gfc_set_default_type (sym, 0, NULL)
14324 && !sym->attr.untyped)
14326 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14327 sym->name, &sym->declared_at);
14328 sym->attr.untyped = 1;
14331 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14332 && !sym->attr.contained
14333 && !gfc_check_symbol_access (sym->ts.u.derived)
14334 && gfc_check_symbol_access (sym))
14336 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14337 "%L of PRIVATE type '%s'", sym->name,
14338 &sym->declared_at, sym->ts.u.derived->name);
14341 if (ns->entries)
14342 for (el = ns->entries->next; el; el = el->next)
14344 if (el->sym->result == el->sym
14345 && el->sym->ts.type == BT_UNKNOWN
14346 && !gfc_set_default_type (el->sym, 0, NULL)
14347 && !el->sym->attr.untyped)
14349 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14350 el->sym->name, &el->sym->declared_at);
14351 el->sym->attr.untyped = 1;
14357 /* 12.3.2.1.1 Defined operators. */
14359 static bool
14360 check_uop_procedure (gfc_symbol *sym, locus where)
14362 gfc_formal_arglist *formal;
14364 if (!sym->attr.function)
14366 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14367 sym->name, &where);
14368 return false;
14371 if (sym->ts.type == BT_CHARACTER
14372 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14373 && !(sym->result && sym->result->ts.u.cl
14374 && sym->result->ts.u.cl->length))
14376 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14377 "character length", sym->name, &where);
14378 return false;
14381 formal = gfc_sym_get_dummy_args (sym);
14382 if (!formal || !formal->sym)
14384 gfc_error ("User operator procedure '%s' at %L must have at least "
14385 "one argument", sym->name, &where);
14386 return false;
14389 if (formal->sym->attr.intent != INTENT_IN)
14391 gfc_error ("First argument of operator interface at %L must be "
14392 "INTENT(IN)", &where);
14393 return false;
14396 if (formal->sym->attr.optional)
14398 gfc_error ("First argument of operator interface at %L cannot be "
14399 "optional", &where);
14400 return false;
14403 formal = formal->next;
14404 if (!formal || !formal->sym)
14405 return true;
14407 if (formal->sym->attr.intent != INTENT_IN)
14409 gfc_error ("Second argument of operator interface at %L must be "
14410 "INTENT(IN)", &where);
14411 return false;
14414 if (formal->sym->attr.optional)
14416 gfc_error ("Second argument of operator interface at %L cannot be "
14417 "optional", &where);
14418 return false;
14421 if (formal->next)
14423 gfc_error ("Operator interface at %L must have, at most, two "
14424 "arguments", &where);
14425 return false;
14428 return true;
14431 static void
14432 gfc_resolve_uops (gfc_symtree *symtree)
14434 gfc_interface *itr;
14436 if (symtree == NULL)
14437 return;
14439 gfc_resolve_uops (symtree->left);
14440 gfc_resolve_uops (symtree->right);
14442 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14443 check_uop_procedure (itr->sym, itr->sym->declared_at);
14447 /* Examine all of the expressions associated with a program unit,
14448 assign types to all intermediate expressions, make sure that all
14449 assignments are to compatible types and figure out which names
14450 refer to which functions or subroutines. It doesn't check code
14451 block, which is handled by resolve_code. */
14453 static void
14454 resolve_types (gfc_namespace *ns)
14456 gfc_namespace *n;
14457 gfc_charlen *cl;
14458 gfc_data *d;
14459 gfc_equiv *eq;
14460 gfc_namespace* old_ns = gfc_current_ns;
14462 /* Check that all IMPLICIT types are ok. */
14463 if (!ns->seen_implicit_none)
14465 unsigned letter;
14466 for (letter = 0; letter != GFC_LETTERS; ++letter)
14467 if (ns->set_flag[letter]
14468 && !resolve_typespec_used (&ns->default_type[letter],
14469 &ns->implicit_loc[letter], NULL))
14470 return;
14473 gfc_current_ns = ns;
14475 resolve_entries (ns);
14477 resolve_common_vars (ns->blank_common.head, false);
14478 resolve_common_blocks (ns->common_root);
14480 resolve_contained_functions (ns);
14482 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14483 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14484 resolve_formal_arglist (ns->proc_name);
14486 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14488 for (cl = ns->cl_list; cl; cl = cl->next)
14489 resolve_charlen (cl);
14491 gfc_traverse_ns (ns, resolve_symbol);
14493 resolve_fntype (ns);
14495 for (n = ns->contained; n; n = n->sibling)
14497 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14498 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14499 "also be PURE", n->proc_name->name,
14500 &n->proc_name->declared_at);
14502 resolve_types (n);
14505 forall_flag = 0;
14506 gfc_do_concurrent_flag = 0;
14507 gfc_check_interfaces (ns);
14509 gfc_traverse_ns (ns, resolve_values);
14511 if (ns->save_all)
14512 gfc_save_all (ns);
14514 iter_stack = NULL;
14515 for (d = ns->data; d; d = d->next)
14516 resolve_data (d);
14518 iter_stack = NULL;
14519 gfc_traverse_ns (ns, gfc_formalize_init_value);
14521 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14523 for (eq = ns->equiv; eq; eq = eq->next)
14524 resolve_equivalence (eq);
14526 /* Warn about unused labels. */
14527 if (warn_unused_label)
14528 warn_unused_fortran_label (ns->st_labels);
14530 gfc_resolve_uops (ns->uop_root);
14532 gfc_current_ns = old_ns;
14536 /* Call resolve_code recursively. */
14538 static void
14539 resolve_codes (gfc_namespace *ns)
14541 gfc_namespace *n;
14542 bitmap_obstack old_obstack;
14544 if (ns->resolved == 1)
14545 return;
14547 for (n = ns->contained; n; n = n->sibling)
14548 resolve_codes (n);
14550 gfc_current_ns = ns;
14552 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14553 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14554 cs_base = NULL;
14556 /* Set to an out of range value. */
14557 current_entry_id = -1;
14559 old_obstack = labels_obstack;
14560 bitmap_obstack_initialize (&labels_obstack);
14562 resolve_code (ns->code, ns);
14564 bitmap_obstack_release (&labels_obstack);
14565 labels_obstack = old_obstack;
14569 /* This function is called after a complete program unit has been compiled.
14570 Its purpose is to examine all of the expressions associated with a program
14571 unit, assign types to all intermediate expressions, make sure that all
14572 assignments are to compatible types and figure out which names refer to
14573 which functions or subroutines. */
14575 void
14576 gfc_resolve (gfc_namespace *ns)
14578 gfc_namespace *old_ns;
14579 code_stack *old_cs_base;
14581 if (ns->resolved)
14582 return;
14584 ns->resolved = -1;
14585 old_ns = gfc_current_ns;
14586 old_cs_base = cs_base;
14588 resolve_types (ns);
14589 component_assignment_level = 0;
14590 resolve_codes (ns);
14592 gfc_current_ns = old_ns;
14593 cs_base = old_cs_base;
14594 ns->resolved = 1;
14596 gfc_run_passes (ns);