gfortran.h (gfc_option_t): Remove warn_aliasing,
[official-gcc.git] / gcc / fortran / resolve.c
blob9d7d3c25be0c946d382e19410581b0b1108869b9
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 gfc_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 ("%qs 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 %qs 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 %qs 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 %qs 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 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1332 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1333 || gfc_is_coindexed (cons->expr));
1334 if (impure && gfc_pure (NULL))
1336 t = false;
1337 gfc_error ("Invalid expression in the structure constructor for "
1338 "pointer component '%s' at %L in PURE procedure",
1339 comp->name, &cons->expr->where);
1342 if (impure)
1343 gfc_unset_implicit_pure (NULL);
1346 return t;
1350 /****************** Expression name resolution ******************/
1352 /* Returns 0 if a symbol was not declared with a type or
1353 attribute declaration statement, nonzero otherwise. */
1355 static int
1356 was_declared (gfc_symbol *sym)
1358 symbol_attribute a;
1360 a = sym->attr;
1362 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1363 return 1;
1365 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1366 || a.optional || a.pointer || a.save || a.target || a.volatile_
1367 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1368 || a.asynchronous || a.codimension)
1369 return 1;
1371 return 0;
1375 /* Determine if a symbol is generic or not. */
1377 static int
1378 generic_sym (gfc_symbol *sym)
1380 gfc_symbol *s;
1382 if (sym->attr.generic ||
1383 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1384 return 1;
1386 if (was_declared (sym) || sym->ns->parent == NULL)
1387 return 0;
1389 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1391 if (s != NULL)
1393 if (s == sym)
1394 return 0;
1395 else
1396 return generic_sym (s);
1399 return 0;
1403 /* Determine if a symbol is specific or not. */
1405 static int
1406 specific_sym (gfc_symbol *sym)
1408 gfc_symbol *s;
1410 if (sym->attr.if_source == IFSRC_IFBODY
1411 || sym->attr.proc == PROC_MODULE
1412 || sym->attr.proc == PROC_INTERNAL
1413 || sym->attr.proc == PROC_ST_FUNCTION
1414 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1415 || sym->attr.external)
1416 return 1;
1418 if (was_declared (sym) || sym->ns->parent == NULL)
1419 return 0;
1421 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1423 return (s == NULL) ? 0 : specific_sym (s);
1427 /* Figure out if the procedure is specific, generic or unknown. */
1429 typedef enum
1430 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1431 proc_type;
1433 static proc_type
1434 procedure_kind (gfc_symbol *sym)
1436 if (generic_sym (sym))
1437 return PTYPE_GENERIC;
1439 if (specific_sym (sym))
1440 return PTYPE_SPECIFIC;
1442 return PTYPE_UNKNOWN;
1445 /* Check references to assumed size arrays. The flag need_full_assumed_size
1446 is nonzero when matching actual arguments. */
1448 static int need_full_assumed_size = 0;
1450 static bool
1451 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1453 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1454 return false;
1456 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1457 What should it be? */
1458 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1459 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1460 && (e->ref->u.ar.type == AR_FULL))
1462 gfc_error ("The upper bound in the last dimension must "
1463 "appear in the reference to the assumed size "
1464 "array '%s' at %L", sym->name, &e->where);
1465 return true;
1467 return false;
1471 /* Look for bad assumed size array references in argument expressions
1472 of elemental and array valued intrinsic procedures. Since this is
1473 called from procedure resolution functions, it only recurses at
1474 operators. */
1476 static bool
1477 resolve_assumed_size_actual (gfc_expr *e)
1479 if (e == NULL)
1480 return false;
1482 switch (e->expr_type)
1484 case EXPR_VARIABLE:
1485 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1486 return true;
1487 break;
1489 case EXPR_OP:
1490 if (resolve_assumed_size_actual (e->value.op.op1)
1491 || resolve_assumed_size_actual (e->value.op.op2))
1492 return true;
1493 break;
1495 default:
1496 break;
1498 return false;
1502 /* Check a generic procedure, passed as an actual argument, to see if
1503 there is a matching specific name. If none, it is an error, and if
1504 more than one, the reference is ambiguous. */
1505 static int
1506 count_specific_procs (gfc_expr *e)
1508 int n;
1509 gfc_interface *p;
1510 gfc_symbol *sym;
1512 n = 0;
1513 sym = e->symtree->n.sym;
1515 for (p = sym->generic; p; p = p->next)
1516 if (strcmp (sym->name, p->sym->name) == 0)
1518 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1519 sym->name);
1520 n++;
1523 if (n > 1)
1524 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1525 &e->where);
1527 if (n == 0)
1528 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1529 "argument at %L", sym->name, &e->where);
1531 return n;
1535 /* See if a call to sym could possibly be a not allowed RECURSION because of
1536 a missing RECURSIVE declaration. This means that either sym is the current
1537 context itself, or sym is the parent of a contained procedure calling its
1538 non-RECURSIVE containing procedure.
1539 This also works if sym is an ENTRY. */
1541 static bool
1542 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1544 gfc_symbol* proc_sym;
1545 gfc_symbol* context_proc;
1546 gfc_namespace* real_context;
1548 if (sym->attr.flavor == FL_PROGRAM
1549 || sym->attr.flavor == FL_DERIVED)
1550 return false;
1552 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1554 /* If we've got an ENTRY, find real procedure. */
1555 if (sym->attr.entry && sym->ns->entries)
1556 proc_sym = sym->ns->entries->sym;
1557 else
1558 proc_sym = sym;
1560 /* If sym is RECURSIVE, all is well of course. */
1561 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1562 return false;
1564 /* Find the context procedure's "real" symbol if it has entries.
1565 We look for a procedure symbol, so recurse on the parents if we don't
1566 find one (like in case of a BLOCK construct). */
1567 for (real_context = context; ; real_context = real_context->parent)
1569 /* We should find something, eventually! */
1570 gcc_assert (real_context);
1572 context_proc = (real_context->entries ? real_context->entries->sym
1573 : real_context->proc_name);
1575 /* In some special cases, there may not be a proc_name, like for this
1576 invalid code:
1577 real(bad_kind()) function foo () ...
1578 when checking the call to bad_kind ().
1579 In these cases, we simply return here and assume that the
1580 call is ok. */
1581 if (!context_proc)
1582 return false;
1584 if (context_proc->attr.flavor != FL_LABEL)
1585 break;
1588 /* A call from sym's body to itself is recursion, of course. */
1589 if (context_proc == proc_sym)
1590 return true;
1592 /* The same is true if context is a contained procedure and sym the
1593 containing one. */
1594 if (context_proc->attr.contained)
1596 gfc_symbol* parent_proc;
1598 gcc_assert (context->parent);
1599 parent_proc = (context->parent->entries ? context->parent->entries->sym
1600 : context->parent->proc_name);
1602 if (parent_proc == proc_sym)
1603 return true;
1606 return false;
1610 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1611 its typespec and formal argument list. */
1613 bool
1614 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1616 gfc_intrinsic_sym* isym = NULL;
1617 const char* symstd;
1619 if (sym->formal)
1620 return true;
1622 /* Already resolved. */
1623 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1624 return true;
1626 /* We already know this one is an intrinsic, so we don't call
1627 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1628 gfc_find_subroutine directly to check whether it is a function or
1629 subroutine. */
1631 if (sym->intmod_sym_id && sym->attr.subroutine)
1633 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1634 isym = gfc_intrinsic_subroutine_by_id (id);
1636 else if (sym->intmod_sym_id)
1638 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1639 isym = gfc_intrinsic_function_by_id (id);
1641 else if (!sym->attr.subroutine)
1642 isym = gfc_find_function (sym->name);
1644 if (isym && !sym->attr.subroutine)
1646 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1647 && !sym->attr.implicit_type)
1648 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1649 " ignored", sym->name, &sym->declared_at);
1651 if (!sym->attr.function &&
1652 !gfc_add_function(&sym->attr, sym->name, loc))
1653 return false;
1655 sym->ts = isym->ts;
1657 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1659 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1661 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1662 " specifier", sym->name, &sym->declared_at);
1663 return false;
1666 if (!sym->attr.subroutine &&
1667 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1668 return false;
1670 else
1672 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1673 &sym->declared_at);
1674 return false;
1677 gfc_copy_formal_args_intr (sym, isym, NULL);
1679 sym->attr.pure = isym->pure;
1680 sym->attr.elemental = isym->elemental;
1682 /* Check it is actually available in the standard settings. */
1683 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1685 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1686 " available in the current standard settings but %s. Use"
1687 " an appropriate -std=* option or enable -fall-intrinsics"
1688 " in order to use it.",
1689 sym->name, &sym->declared_at, symstd);
1690 return false;
1693 return true;
1697 /* Resolve a procedure expression, like passing it to a called procedure or as
1698 RHS for a procedure pointer assignment. */
1700 static bool
1701 resolve_procedure_expression (gfc_expr* expr)
1703 gfc_symbol* sym;
1705 if (expr->expr_type != EXPR_VARIABLE)
1706 return true;
1707 gcc_assert (expr->symtree);
1709 sym = expr->symtree->n.sym;
1711 if (sym->attr.intrinsic)
1712 gfc_resolve_intrinsic (sym, &expr->where);
1714 if (sym->attr.flavor != FL_PROCEDURE
1715 || (sym->attr.function && sym->result == sym))
1716 return true;
1718 /* A non-RECURSIVE procedure that is used as procedure expression within its
1719 own body is in danger of being called recursively. */
1720 if (is_illegal_recursion (sym, gfc_current_ns))
1721 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1722 " itself recursively. Declare it RECURSIVE or use"
1723 " -frecursive", sym->name, &expr->where);
1725 return true;
1729 /* Resolve an actual argument list. Most of the time, this is just
1730 resolving the expressions in the list.
1731 The exception is that we sometimes have to decide whether arguments
1732 that look like procedure arguments are really simple variable
1733 references. */
1735 static bool
1736 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1737 bool no_formal_args)
1739 gfc_symbol *sym;
1740 gfc_symtree *parent_st;
1741 gfc_expr *e;
1742 int save_need_full_assumed_size;
1743 bool return_value = false;
1744 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1746 actual_arg = true;
1747 first_actual_arg = true;
1749 for (; arg; arg = arg->next)
1751 e = arg->expr;
1752 if (e == NULL)
1754 /* Check the label is a valid branching target. */
1755 if (arg->label)
1757 if (arg->label->defined == ST_LABEL_UNKNOWN)
1759 gfc_error ("Label %d referenced at %L is never defined",
1760 arg->label->value, &arg->label->where);
1761 goto cleanup;
1764 first_actual_arg = false;
1765 continue;
1768 if (e->expr_type == EXPR_VARIABLE
1769 && e->symtree->n.sym->attr.generic
1770 && no_formal_args
1771 && count_specific_procs (e) != 1)
1772 goto cleanup;
1774 if (e->ts.type != BT_PROCEDURE)
1776 save_need_full_assumed_size = need_full_assumed_size;
1777 if (e->expr_type != EXPR_VARIABLE)
1778 need_full_assumed_size = 0;
1779 if (!gfc_resolve_expr (e))
1780 goto cleanup;
1781 need_full_assumed_size = save_need_full_assumed_size;
1782 goto argument_list;
1785 /* See if the expression node should really be a variable reference. */
1787 sym = e->symtree->n.sym;
1789 if (sym->attr.flavor == FL_PROCEDURE
1790 || sym->attr.intrinsic
1791 || sym->attr.external)
1793 int actual_ok;
1795 /* If a procedure is not already determined to be something else
1796 check if it is intrinsic. */
1797 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1798 sym->attr.intrinsic = 1;
1800 if (sym->attr.proc == PROC_ST_FUNCTION)
1802 gfc_error ("Statement function '%s' at %L is not allowed as an "
1803 "actual argument", sym->name, &e->where);
1806 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1807 sym->attr.subroutine);
1808 if (sym->attr.intrinsic && actual_ok == 0)
1810 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1811 "actual argument", sym->name, &e->where);
1814 if (sym->attr.contained && !sym->attr.use_assoc
1815 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1817 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1818 " used as actual argument at %L",
1819 sym->name, &e->where))
1820 goto cleanup;
1823 if (sym->attr.elemental && !sym->attr.intrinsic)
1825 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1826 "allowed as an actual argument at %L", sym->name,
1827 &e->where);
1830 /* Check if a generic interface has a specific procedure
1831 with the same name before emitting an error. */
1832 if (sym->attr.generic && count_specific_procs (e) != 1)
1833 goto cleanup;
1835 /* Just in case a specific was found for the expression. */
1836 sym = e->symtree->n.sym;
1838 /* If the symbol is the function that names the current (or
1839 parent) scope, then we really have a variable reference. */
1841 if (gfc_is_function_return_value (sym, sym->ns))
1842 goto got_variable;
1844 /* If all else fails, see if we have a specific intrinsic. */
1845 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1847 gfc_intrinsic_sym *isym;
1849 isym = gfc_find_function (sym->name);
1850 if (isym == NULL || !isym->specific)
1852 gfc_error ("Unable to find a specific INTRINSIC procedure "
1853 "for the reference '%s' at %L", sym->name,
1854 &e->where);
1855 goto cleanup;
1857 sym->ts = isym->ts;
1858 sym->attr.intrinsic = 1;
1859 sym->attr.function = 1;
1862 if (!gfc_resolve_expr (e))
1863 goto cleanup;
1864 goto argument_list;
1867 /* See if the name is a module procedure in a parent unit. */
1869 if (was_declared (sym) || sym->ns->parent == NULL)
1870 goto got_variable;
1872 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1874 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1875 goto cleanup;
1878 if (parent_st == NULL)
1879 goto got_variable;
1881 sym = parent_st->n.sym;
1882 e->symtree = parent_st; /* Point to the right thing. */
1884 if (sym->attr.flavor == FL_PROCEDURE
1885 || sym->attr.intrinsic
1886 || sym->attr.external)
1888 if (!gfc_resolve_expr (e))
1889 goto cleanup;
1890 goto argument_list;
1893 got_variable:
1894 e->expr_type = EXPR_VARIABLE;
1895 e->ts = sym->ts;
1896 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1897 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1898 && CLASS_DATA (sym)->as))
1900 e->rank = sym->ts.type == BT_CLASS
1901 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1902 e->ref = gfc_get_ref ();
1903 e->ref->type = REF_ARRAY;
1904 e->ref->u.ar.type = AR_FULL;
1905 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1906 ? CLASS_DATA (sym)->as : sym->as;
1909 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1910 primary.c (match_actual_arg). If above code determines that it
1911 is a variable instead, it needs to be resolved as it was not
1912 done at the beginning of this function. */
1913 save_need_full_assumed_size = need_full_assumed_size;
1914 if (e->expr_type != EXPR_VARIABLE)
1915 need_full_assumed_size = 0;
1916 if (!gfc_resolve_expr (e))
1917 goto cleanup;
1918 need_full_assumed_size = save_need_full_assumed_size;
1920 argument_list:
1921 /* Check argument list functions %VAL, %LOC and %REF. There is
1922 nothing to do for %REF. */
1923 if (arg->name && arg->name[0] == '%')
1925 if (strncmp ("%VAL", arg->name, 4) == 0)
1927 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1929 gfc_error ("By-value argument at %L is not of numeric "
1930 "type", &e->where);
1931 goto cleanup;
1934 if (e->rank)
1936 gfc_error ("By-value argument at %L cannot be an array or "
1937 "an array section", &e->where);
1938 goto cleanup;
1941 /* Intrinsics are still PROC_UNKNOWN here. However,
1942 since same file external procedures are not resolvable
1943 in gfortran, it is a good deal easier to leave them to
1944 intrinsic.c. */
1945 if (ptype != PROC_UNKNOWN
1946 && ptype != PROC_DUMMY
1947 && ptype != PROC_EXTERNAL
1948 && ptype != PROC_MODULE)
1950 gfc_error ("By-value argument at %L is not allowed "
1951 "in this context", &e->where);
1952 goto cleanup;
1956 /* Statement functions have already been excluded above. */
1957 else if (strncmp ("%LOC", arg->name, 4) == 0
1958 && e->ts.type == BT_PROCEDURE)
1960 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1962 gfc_error ("Passing internal procedure at %L by location "
1963 "not allowed", &e->where);
1964 goto cleanup;
1969 /* Fortran 2008, C1237. */
1970 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1971 && gfc_has_ultimate_pointer (e))
1973 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1974 "component", &e->where);
1975 goto cleanup;
1978 first_actual_arg = false;
1981 return_value = true;
1983 cleanup:
1984 actual_arg = actual_arg_sav;
1985 first_actual_arg = first_actual_arg_sav;
1987 return return_value;
1991 /* Do the checks of the actual argument list that are specific to elemental
1992 procedures. If called with c == NULL, we have a function, otherwise if
1993 expr == NULL, we have a subroutine. */
1995 static bool
1996 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1998 gfc_actual_arglist *arg0;
1999 gfc_actual_arglist *arg;
2000 gfc_symbol *esym = NULL;
2001 gfc_intrinsic_sym *isym = NULL;
2002 gfc_expr *e = NULL;
2003 gfc_intrinsic_arg *iformal = NULL;
2004 gfc_formal_arglist *eformal = NULL;
2005 bool formal_optional = false;
2006 bool set_by_optional = false;
2007 int i;
2008 int rank = 0;
2010 /* Is this an elemental procedure? */
2011 if (expr && expr->value.function.actual != NULL)
2013 if (expr->value.function.esym != NULL
2014 && expr->value.function.esym->attr.elemental)
2016 arg0 = expr->value.function.actual;
2017 esym = expr->value.function.esym;
2019 else if (expr->value.function.isym != NULL
2020 && expr->value.function.isym->elemental)
2022 arg0 = expr->value.function.actual;
2023 isym = expr->value.function.isym;
2025 else
2026 return true;
2028 else if (c && c->ext.actual != NULL)
2030 arg0 = c->ext.actual;
2032 if (c->resolved_sym)
2033 esym = c->resolved_sym;
2034 else
2035 esym = c->symtree->n.sym;
2036 gcc_assert (esym);
2038 if (!esym->attr.elemental)
2039 return true;
2041 else
2042 return true;
2044 /* The rank of an elemental is the rank of its array argument(s). */
2045 for (arg = arg0; arg; arg = arg->next)
2047 if (arg->expr != NULL && arg->expr->rank != 0)
2049 rank = arg->expr->rank;
2050 if (arg->expr->expr_type == EXPR_VARIABLE
2051 && arg->expr->symtree->n.sym->attr.optional)
2052 set_by_optional = true;
2054 /* Function specific; set the result rank and shape. */
2055 if (expr)
2057 expr->rank = rank;
2058 if (!expr->shape && arg->expr->shape)
2060 expr->shape = gfc_get_shape (rank);
2061 for (i = 0; i < rank; i++)
2062 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2065 break;
2069 /* If it is an array, it shall not be supplied as an actual argument
2070 to an elemental procedure unless an array of the same rank is supplied
2071 as an actual argument corresponding to a nonoptional dummy argument of
2072 that elemental procedure(12.4.1.5). */
2073 formal_optional = false;
2074 if (isym)
2075 iformal = isym->formal;
2076 else
2077 eformal = esym->formal;
2079 for (arg = arg0; arg; arg = arg->next)
2081 if (eformal)
2083 if (eformal->sym && eformal->sym->attr.optional)
2084 formal_optional = true;
2085 eformal = eformal->next;
2087 else if (isym && iformal)
2089 if (iformal->optional)
2090 formal_optional = true;
2091 iformal = iformal->next;
2093 else if (isym)
2094 formal_optional = true;
2096 if (pedantic && arg->expr != NULL
2097 && arg->expr->expr_type == EXPR_VARIABLE
2098 && arg->expr->symtree->n.sym->attr.optional
2099 && formal_optional
2100 && arg->expr->rank
2101 && (set_by_optional || arg->expr->rank != rank)
2102 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2104 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2105 "MISSING, it cannot be the actual argument of an "
2106 "ELEMENTAL procedure unless there is a non-optional "
2107 "argument with the same rank (12.4.1.5)",
2108 arg->expr->symtree->n.sym->name, &arg->expr->where);
2112 for (arg = arg0; arg; arg = arg->next)
2114 if (arg->expr == NULL || arg->expr->rank == 0)
2115 continue;
2117 /* Being elemental, the last upper bound of an assumed size array
2118 argument must be present. */
2119 if (resolve_assumed_size_actual (arg->expr))
2120 return false;
2122 /* Elemental procedure's array actual arguments must conform. */
2123 if (e != NULL)
2125 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2126 return false;
2128 else
2129 e = arg->expr;
2132 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2133 is an array, the intent inout/out variable needs to be also an array. */
2134 if (rank > 0 && esym && expr == NULL)
2135 for (eformal = esym->formal, arg = arg0; arg && eformal;
2136 arg = arg->next, eformal = eformal->next)
2137 if ((eformal->sym->attr.intent == INTENT_OUT
2138 || eformal->sym->attr.intent == INTENT_INOUT)
2139 && arg->expr && arg->expr->rank == 0)
2141 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2142 "ELEMENTAL subroutine '%s' is a scalar, but another "
2143 "actual argument is an array", &arg->expr->where,
2144 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2145 : "INOUT", eformal->sym->name, esym->name);
2146 return false;
2148 return true;
2152 /* This function does the checking of references to global procedures
2153 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2154 77 and 95 standards. It checks for a gsymbol for the name, making
2155 one if it does not already exist. If it already exists, then the
2156 reference being resolved must correspond to the type of gsymbol.
2157 Otherwise, the new symbol is equipped with the attributes of the
2158 reference. The corresponding code that is called in creating
2159 global entities is parse.c.
2161 In addition, for all but -std=legacy, the gsymbols are used to
2162 check the interfaces of external procedures from the same file.
2163 The namespace of the gsymbol is resolved and then, once this is
2164 done the interface is checked. */
2167 static bool
2168 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2170 if (!gsym_ns->proc_name->attr.recursive)
2171 return true;
2173 if (sym->ns == gsym_ns)
2174 return false;
2176 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2177 return false;
2179 return true;
2182 static bool
2183 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2185 if (gsym_ns->entries)
2187 gfc_entry_list *entry = gsym_ns->entries;
2189 for (; entry; entry = entry->next)
2191 if (strcmp (sym->name, entry->sym->name) == 0)
2193 if (strcmp (gsym_ns->proc_name->name,
2194 sym->ns->proc_name->name) == 0)
2195 return false;
2197 if (sym->ns->parent
2198 && strcmp (gsym_ns->proc_name->name,
2199 sym->ns->parent->proc_name->name) == 0)
2200 return false;
2204 return true;
2208 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2210 bool
2211 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2213 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2215 for ( ; arg; arg = arg->next)
2217 if (!arg->sym)
2218 continue;
2220 if (arg->sym->attr.allocatable) /* (2a) */
2222 strncpy (errmsg, _("allocatable argument"), err_len);
2223 return true;
2225 else if (arg->sym->attr.asynchronous)
2227 strncpy (errmsg, _("asynchronous argument"), err_len);
2228 return true;
2230 else if (arg->sym->attr.optional)
2232 strncpy (errmsg, _("optional argument"), err_len);
2233 return true;
2235 else if (arg->sym->attr.pointer)
2237 strncpy (errmsg, _("pointer argument"), err_len);
2238 return true;
2240 else if (arg->sym->attr.target)
2242 strncpy (errmsg, _("target argument"), err_len);
2243 return true;
2245 else if (arg->sym->attr.value)
2247 strncpy (errmsg, _("value argument"), err_len);
2248 return true;
2250 else if (arg->sym->attr.volatile_)
2252 strncpy (errmsg, _("volatile argument"), err_len);
2253 return true;
2255 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2257 strncpy (errmsg, _("assumed-shape argument"), err_len);
2258 return true;
2260 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2262 strncpy (errmsg, _("assumed-rank argument"), err_len);
2263 return true;
2265 else if (arg->sym->attr.codimension) /* (2c) */
2267 strncpy (errmsg, _("coarray argument"), err_len);
2268 return true;
2270 else if (false) /* (2d) TODO: parametrized derived type */
2272 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2273 return true;
2275 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2277 strncpy (errmsg, _("polymorphic argument"), err_len);
2278 return true;
2280 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2282 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2283 return true;
2285 else if (arg->sym->ts.type == BT_ASSUMED)
2287 /* As assumed-type is unlimited polymorphic (cf. above).
2288 See also TS 29113, Note 6.1. */
2289 strncpy (errmsg, _("assumed-type argument"), err_len);
2290 return true;
2294 if (sym->attr.function)
2296 gfc_symbol *res = sym->result ? sym->result : sym;
2298 if (res->attr.dimension) /* (3a) */
2300 strncpy (errmsg, _("array result"), err_len);
2301 return true;
2303 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2305 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2306 return true;
2308 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2309 && res->ts.u.cl->length
2310 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2312 strncpy (errmsg, _("result with non-constant character length"), err_len);
2313 return true;
2317 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2319 strncpy (errmsg, _("elemental procedure"), err_len);
2320 return true;
2322 else if (sym->attr.is_bind_c) /* (5) */
2324 strncpy (errmsg, _("bind(c) procedure"), err_len);
2325 return true;
2328 return false;
2332 static void
2333 resolve_global_procedure (gfc_symbol *sym, locus *where,
2334 gfc_actual_arglist **actual, int sub)
2336 gfc_gsymbol * gsym;
2337 gfc_namespace *ns;
2338 enum gfc_symbol_type type;
2339 char reason[200];
2341 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2343 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2345 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2346 gfc_global_used (gsym, where);
2348 if ((sym->attr.if_source == IFSRC_UNKNOWN
2349 || sym->attr.if_source == IFSRC_IFBODY)
2350 && gsym->type != GSYM_UNKNOWN
2351 && !gsym->binding_label
2352 && gsym->ns
2353 && gsym->ns->resolved != -1
2354 && gsym->ns->proc_name
2355 && not_in_recursive (sym, gsym->ns)
2356 && not_entry_self_reference (sym, gsym->ns))
2358 gfc_symbol *def_sym;
2360 /* Resolve the gsymbol namespace if needed. */
2361 if (!gsym->ns->resolved)
2363 gfc_dt_list *old_dt_list;
2364 struct gfc_omp_saved_state old_omp_state;
2366 /* Stash away derived types so that the backend_decls do not
2367 get mixed up. */
2368 old_dt_list = gfc_derived_types;
2369 gfc_derived_types = NULL;
2370 /* And stash away openmp state. */
2371 gfc_omp_save_and_clear_state (&old_omp_state);
2373 gfc_resolve (gsym->ns);
2375 /* Store the new derived types with the global namespace. */
2376 if (gfc_derived_types)
2377 gsym->ns->derived_types = gfc_derived_types;
2379 /* Restore the derived types of this namespace. */
2380 gfc_derived_types = old_dt_list;
2381 /* And openmp state. */
2382 gfc_omp_restore_state (&old_omp_state);
2385 /* Make sure that translation for the gsymbol occurs before
2386 the procedure currently being resolved. */
2387 ns = gfc_global_ns_list;
2388 for (; ns && ns != gsym->ns; ns = ns->sibling)
2390 if (ns->sibling == gsym->ns)
2392 ns->sibling = gsym->ns->sibling;
2393 gsym->ns->sibling = gfc_global_ns_list;
2394 gfc_global_ns_list = gsym->ns;
2395 break;
2399 def_sym = gsym->ns->proc_name;
2401 /* This can happen if a binding name has been specified. */
2402 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2403 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2405 if (def_sym->attr.entry_master)
2407 gfc_entry_list *entry;
2408 for (entry = gsym->ns->entries; entry; entry = entry->next)
2409 if (strcmp (entry->sym->name, sym->name) == 0)
2411 def_sym = entry->sym;
2412 break;
2416 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2418 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2419 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2420 gfc_typename (&def_sym->ts));
2421 goto done;
2424 if (sym->attr.if_source == IFSRC_UNKNOWN
2425 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2427 gfc_error ("Explicit interface required for '%s' at %L: %s",
2428 sym->name, &sym->declared_at, reason);
2429 goto done;
2432 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2433 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2434 gfc_errors_to_warnings (1);
2436 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2437 reason, sizeof(reason), NULL, NULL))
2439 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2440 sym->name, &sym->declared_at, reason);
2441 goto done;
2444 if (!pedantic
2445 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2446 && !(gfc_option.warn_std & GFC_STD_GNU)))
2447 gfc_errors_to_warnings (1);
2449 if (sym->attr.if_source != IFSRC_IFBODY)
2450 gfc_procedure_use (def_sym, actual, where);
2453 done:
2454 gfc_errors_to_warnings (0);
2456 if (gsym->type == GSYM_UNKNOWN)
2458 gsym->type = type;
2459 gsym->where = *where;
2462 gsym->used = 1;
2466 /************* Function resolution *************/
2468 /* Resolve a function call known to be generic.
2469 Section 14.1.2.4.1. */
2471 static match
2472 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2474 gfc_symbol *s;
2476 if (sym->attr.generic)
2478 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2479 if (s != NULL)
2481 expr->value.function.name = s->name;
2482 expr->value.function.esym = s;
2484 if (s->ts.type != BT_UNKNOWN)
2485 expr->ts = s->ts;
2486 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2487 expr->ts = s->result->ts;
2489 if (s->as != NULL)
2490 expr->rank = s->as->rank;
2491 else if (s->result != NULL && s->result->as != NULL)
2492 expr->rank = s->result->as->rank;
2494 gfc_set_sym_referenced (expr->value.function.esym);
2496 return MATCH_YES;
2499 /* TODO: Need to search for elemental references in generic
2500 interface. */
2503 if (sym->attr.intrinsic)
2504 return gfc_intrinsic_func_interface (expr, 0);
2506 return MATCH_NO;
2510 static bool
2511 resolve_generic_f (gfc_expr *expr)
2513 gfc_symbol *sym;
2514 match m;
2515 gfc_interface *intr = NULL;
2517 sym = expr->symtree->n.sym;
2519 for (;;)
2521 m = resolve_generic_f0 (expr, sym);
2522 if (m == MATCH_YES)
2523 return true;
2524 else if (m == MATCH_ERROR)
2525 return false;
2527 generic:
2528 if (!intr)
2529 for (intr = sym->generic; intr; intr = intr->next)
2530 if (intr->sym->attr.flavor == FL_DERIVED)
2531 break;
2533 if (sym->ns->parent == NULL)
2534 break;
2535 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2537 if (sym == NULL)
2538 break;
2539 if (!generic_sym (sym))
2540 goto generic;
2543 /* Last ditch attempt. See if the reference is to an intrinsic
2544 that possesses a matching interface. 14.1.2.4 */
2545 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2547 gfc_error ("There is no specific function for the generic '%s' "
2548 "at %L", expr->symtree->n.sym->name, &expr->where);
2549 return false;
2552 if (intr)
2554 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2555 NULL, false))
2556 return false;
2557 return resolve_structure_cons (expr, 0);
2560 m = gfc_intrinsic_func_interface (expr, 0);
2561 if (m == MATCH_YES)
2562 return true;
2564 if (m == MATCH_NO)
2565 gfc_error ("Generic function '%s' at %L is not consistent with a "
2566 "specific intrinsic interface", expr->symtree->n.sym->name,
2567 &expr->where);
2569 return false;
2573 /* Resolve a function call known to be specific. */
2575 static match
2576 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2578 match m;
2580 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2582 if (sym->attr.dummy)
2584 sym->attr.proc = PROC_DUMMY;
2585 goto found;
2588 sym->attr.proc = PROC_EXTERNAL;
2589 goto found;
2592 if (sym->attr.proc == PROC_MODULE
2593 || sym->attr.proc == PROC_ST_FUNCTION
2594 || sym->attr.proc == PROC_INTERNAL)
2595 goto found;
2597 if (sym->attr.intrinsic)
2599 m = gfc_intrinsic_func_interface (expr, 1);
2600 if (m == MATCH_YES)
2601 return MATCH_YES;
2602 if (m == MATCH_NO)
2603 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2604 "with an intrinsic", sym->name, &expr->where);
2606 return MATCH_ERROR;
2609 return MATCH_NO;
2611 found:
2612 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2614 if (sym->result)
2615 expr->ts = sym->result->ts;
2616 else
2617 expr->ts = sym->ts;
2618 expr->value.function.name = sym->name;
2619 expr->value.function.esym = sym;
2620 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2621 expr->rank = CLASS_DATA (sym)->as->rank;
2622 else if (sym->as != NULL)
2623 expr->rank = sym->as->rank;
2625 return MATCH_YES;
2629 static bool
2630 resolve_specific_f (gfc_expr *expr)
2632 gfc_symbol *sym;
2633 match m;
2635 sym = expr->symtree->n.sym;
2637 for (;;)
2639 m = resolve_specific_f0 (sym, expr);
2640 if (m == MATCH_YES)
2641 return true;
2642 if (m == MATCH_ERROR)
2643 return false;
2645 if (sym->ns->parent == NULL)
2646 break;
2648 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2650 if (sym == NULL)
2651 break;
2654 gfc_error ("Unable to resolve the specific function '%s' at %L",
2655 expr->symtree->n.sym->name, &expr->where);
2657 return true;
2661 /* Resolve a procedure call not known to be generic nor specific. */
2663 static bool
2664 resolve_unknown_f (gfc_expr *expr)
2666 gfc_symbol *sym;
2667 gfc_typespec *ts;
2669 sym = expr->symtree->n.sym;
2671 if (sym->attr.dummy)
2673 sym->attr.proc = PROC_DUMMY;
2674 expr->value.function.name = sym->name;
2675 goto set_type;
2678 /* See if we have an intrinsic function reference. */
2680 if (gfc_is_intrinsic (sym, 0, expr->where))
2682 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2683 return true;
2684 return false;
2687 /* The reference is to an external name. */
2689 sym->attr.proc = PROC_EXTERNAL;
2690 expr->value.function.name = sym->name;
2691 expr->value.function.esym = expr->symtree->n.sym;
2693 if (sym->as != NULL)
2694 expr->rank = sym->as->rank;
2696 /* Type of the expression is either the type of the symbol or the
2697 default type of the symbol. */
2699 set_type:
2700 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2702 if (sym->ts.type != BT_UNKNOWN)
2703 expr->ts = sym->ts;
2704 else
2706 ts = gfc_get_default_type (sym->name, sym->ns);
2708 if (ts->type == BT_UNKNOWN)
2710 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2711 sym->name, &expr->where);
2712 return false;
2714 else
2715 expr->ts = *ts;
2718 return true;
2722 /* Return true, if the symbol is an external procedure. */
2723 static bool
2724 is_external_proc (gfc_symbol *sym)
2726 if (!sym->attr.dummy && !sym->attr.contained
2727 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2728 && sym->attr.proc != PROC_ST_FUNCTION
2729 && !sym->attr.proc_pointer
2730 && !sym->attr.use_assoc
2731 && sym->name)
2732 return true;
2734 return false;
2738 /* Figure out if a function reference is pure or not. Also set the name
2739 of the function for a potential error message. Return nonzero if the
2740 function is PURE, zero if not. */
2741 static int
2742 pure_stmt_function (gfc_expr *, gfc_symbol *);
2744 static int
2745 pure_function (gfc_expr *e, const char **name)
2747 int pure;
2749 *name = NULL;
2751 if (e->symtree != NULL
2752 && e->symtree->n.sym != NULL
2753 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2754 return pure_stmt_function (e, e->symtree->n.sym);
2756 if (e->value.function.esym)
2758 pure = gfc_pure (e->value.function.esym);
2759 *name = e->value.function.esym->name;
2761 else if (e->value.function.isym)
2763 pure = e->value.function.isym->pure
2764 || e->value.function.isym->elemental;
2765 *name = e->value.function.isym->name;
2767 else
2769 /* Implicit functions are not pure. */
2770 pure = 0;
2771 *name = e->value.function.name;
2774 return pure;
2778 static bool
2779 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2780 int *f ATTRIBUTE_UNUSED)
2782 const char *name;
2784 /* Don't bother recursing into other statement functions
2785 since they will be checked individually for purity. */
2786 if (e->expr_type != EXPR_FUNCTION
2787 || !e->symtree
2788 || e->symtree->n.sym == sym
2789 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2790 return false;
2792 return pure_function (e, &name) ? false : true;
2796 static int
2797 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2799 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2803 /* Resolve a function call, which means resolving the arguments, then figuring
2804 out which entity the name refers to. */
2806 static bool
2807 resolve_function (gfc_expr *expr)
2809 gfc_actual_arglist *arg;
2810 gfc_symbol *sym;
2811 const char *name;
2812 bool t;
2813 int temp;
2814 procedure_type p = PROC_INTRINSIC;
2815 bool no_formal_args;
2817 sym = NULL;
2818 if (expr->symtree)
2819 sym = expr->symtree->n.sym;
2821 /* If this is a procedure pointer component, it has already been resolved. */
2822 if (gfc_is_proc_ptr_comp (expr))
2823 return true;
2825 if (sym && sym->attr.intrinsic
2826 && !gfc_resolve_intrinsic (sym, &expr->where))
2827 return false;
2829 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2831 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2832 return false;
2835 /* If this ia a deferred TBP with an abstract interface (which may
2836 of course be referenced), expr->value.function.esym will be set. */
2837 if (sym && sym->attr.abstract && !expr->value.function.esym)
2839 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2840 sym->name, &expr->where);
2841 return false;
2844 /* Switch off assumed size checking and do this again for certain kinds
2845 of procedure, once the procedure itself is resolved. */
2846 need_full_assumed_size++;
2848 if (expr->symtree && expr->symtree->n.sym)
2849 p = expr->symtree->n.sym->attr.proc;
2851 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2852 inquiry_argument = true;
2853 no_formal_args = sym && is_external_proc (sym)
2854 && gfc_sym_get_dummy_args (sym) == NULL;
2856 if (!resolve_actual_arglist (expr->value.function.actual,
2857 p, no_formal_args))
2859 inquiry_argument = false;
2860 return false;
2863 inquiry_argument = false;
2865 /* Resume assumed_size checking. */
2866 need_full_assumed_size--;
2868 /* If the procedure is external, check for usage. */
2869 if (sym && is_external_proc (sym))
2870 resolve_global_procedure (sym, &expr->where,
2871 &expr->value.function.actual, 0);
2873 if (sym && sym->ts.type == BT_CHARACTER
2874 && sym->ts.u.cl
2875 && sym->ts.u.cl->length == NULL
2876 && !sym->attr.dummy
2877 && !sym->ts.deferred
2878 && expr->value.function.esym == NULL
2879 && !sym->attr.contained)
2881 /* Internal procedures are taken care of in resolve_contained_fntype. */
2882 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2883 "be used at %L since it is not a dummy argument",
2884 sym->name, &expr->where);
2885 return false;
2888 /* See if function is already resolved. */
2890 if (expr->value.function.name != NULL
2891 || expr->value.function.isym != NULL)
2893 if (expr->ts.type == BT_UNKNOWN)
2894 expr->ts = sym->ts;
2895 t = true;
2897 else
2899 /* Apply the rules of section 14.1.2. */
2901 switch (procedure_kind (sym))
2903 case PTYPE_GENERIC:
2904 t = resolve_generic_f (expr);
2905 break;
2907 case PTYPE_SPECIFIC:
2908 t = resolve_specific_f (expr);
2909 break;
2911 case PTYPE_UNKNOWN:
2912 t = resolve_unknown_f (expr);
2913 break;
2915 default:
2916 gfc_internal_error ("resolve_function(): bad function type");
2920 /* If the expression is still a function (it might have simplified),
2921 then we check to see if we are calling an elemental function. */
2923 if (expr->expr_type != EXPR_FUNCTION)
2924 return t;
2926 temp = need_full_assumed_size;
2927 need_full_assumed_size = 0;
2929 if (!resolve_elemental_actual (expr, NULL))
2930 return false;
2932 if (omp_workshare_flag
2933 && expr->value.function.esym
2934 && ! gfc_elemental (expr->value.function.esym))
2936 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2937 "in WORKSHARE construct", expr->value.function.esym->name,
2938 &expr->where);
2939 t = false;
2942 #define GENERIC_ID expr->value.function.isym->id
2943 else if (expr->value.function.actual != NULL
2944 && expr->value.function.isym != NULL
2945 && GENERIC_ID != GFC_ISYM_LBOUND
2946 && GENERIC_ID != GFC_ISYM_LCOBOUND
2947 && GENERIC_ID != GFC_ISYM_UCOBOUND
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 gfc_unset_implicit_pure (NULL);
3012 /* Functions without the RECURSIVE attribution are not allowed to
3013 * call themselves. */
3014 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3016 gfc_symbol *esym;
3017 esym = expr->value.function.esym;
3019 if (is_illegal_recursion (esym, gfc_current_ns))
3021 if (esym->attr.entry && esym->ns->entries)
3022 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3023 " function '%s' is not RECURSIVE",
3024 esym->name, &expr->where, esym->ns->entries->sym->name);
3025 else
3026 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3027 " is not RECURSIVE", esym->name, &expr->where);
3029 t = false;
3033 /* Character lengths of use associated functions may contains references to
3034 symbols not referenced from the current program unit otherwise. Make sure
3035 those symbols are marked as referenced. */
3037 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3038 && expr->value.function.esym->attr.use_assoc)
3040 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3043 /* Make sure that the expression has a typespec that works. */
3044 if (expr->ts.type == BT_UNKNOWN)
3046 if (expr->symtree->n.sym->result
3047 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3048 && !expr->symtree->n.sym->result->attr.proc_pointer)
3049 expr->ts = expr->symtree->n.sym->result->ts;
3052 return t;
3056 /************* Subroutine resolution *************/
3058 static void
3059 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3061 if (gfc_pure (sym))
3062 return;
3064 if (forall_flag)
3065 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3066 sym->name, &c->loc);
3067 else if (gfc_do_concurrent_flag)
3068 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3069 "PURE", sym->name, &c->loc);
3070 else if (gfc_pure (NULL))
3071 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3072 &c->loc);
3074 gfc_unset_implicit_pure (NULL);
3078 static match
3079 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3081 gfc_symbol *s;
3083 if (sym->attr.generic)
3085 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3086 if (s != NULL)
3088 c->resolved_sym = s;
3089 pure_subroutine (c, s);
3090 return MATCH_YES;
3093 /* TODO: Need to search for elemental references in generic interface. */
3096 if (sym->attr.intrinsic)
3097 return gfc_intrinsic_sub_interface (c, 0);
3099 return MATCH_NO;
3103 static bool
3104 resolve_generic_s (gfc_code *c)
3106 gfc_symbol *sym;
3107 match m;
3109 sym = c->symtree->n.sym;
3111 for (;;)
3113 m = resolve_generic_s0 (c, sym);
3114 if (m == MATCH_YES)
3115 return true;
3116 else if (m == MATCH_ERROR)
3117 return false;
3119 generic:
3120 if (sym->ns->parent == NULL)
3121 break;
3122 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3124 if (sym == NULL)
3125 break;
3126 if (!generic_sym (sym))
3127 goto generic;
3130 /* Last ditch attempt. See if the reference is to an intrinsic
3131 that possesses a matching interface. 14.1.2.4 */
3132 sym = c->symtree->n.sym;
3134 if (!gfc_is_intrinsic (sym, 1, c->loc))
3136 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3137 sym->name, &c->loc);
3138 return false;
3141 m = gfc_intrinsic_sub_interface (c, 0);
3142 if (m == MATCH_YES)
3143 return true;
3144 if (m == MATCH_NO)
3145 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3146 "intrinsic subroutine interface", sym->name, &c->loc);
3148 return false;
3152 /* Resolve a subroutine call known to be specific. */
3154 static match
3155 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3157 match m;
3159 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3161 if (sym->attr.dummy)
3163 sym->attr.proc = PROC_DUMMY;
3164 goto found;
3167 sym->attr.proc = PROC_EXTERNAL;
3168 goto found;
3171 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3172 goto found;
3174 if (sym->attr.intrinsic)
3176 m = gfc_intrinsic_sub_interface (c, 1);
3177 if (m == MATCH_YES)
3178 return MATCH_YES;
3179 if (m == MATCH_NO)
3180 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3181 "with an intrinsic", sym->name, &c->loc);
3183 return MATCH_ERROR;
3186 return MATCH_NO;
3188 found:
3189 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3191 c->resolved_sym = sym;
3192 pure_subroutine (c, sym);
3194 return MATCH_YES;
3198 static bool
3199 resolve_specific_s (gfc_code *c)
3201 gfc_symbol *sym;
3202 match m;
3204 sym = c->symtree->n.sym;
3206 for (;;)
3208 m = resolve_specific_s0 (c, sym);
3209 if (m == MATCH_YES)
3210 return true;
3211 if (m == MATCH_ERROR)
3212 return false;
3214 if (sym->ns->parent == NULL)
3215 break;
3217 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3219 if (sym == NULL)
3220 break;
3223 sym = c->symtree->n.sym;
3224 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3225 sym->name, &c->loc);
3227 return false;
3231 /* Resolve a subroutine call not known to be generic nor specific. */
3233 static bool
3234 resolve_unknown_s (gfc_code *c)
3236 gfc_symbol *sym;
3238 sym = c->symtree->n.sym;
3240 if (sym->attr.dummy)
3242 sym->attr.proc = PROC_DUMMY;
3243 goto found;
3246 /* See if we have an intrinsic function reference. */
3248 if (gfc_is_intrinsic (sym, 1, c->loc))
3250 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3251 return true;
3252 return false;
3255 /* The reference is to an external name. */
3257 found:
3258 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3260 c->resolved_sym = sym;
3262 pure_subroutine (c, sym);
3264 return true;
3268 /* Resolve a subroutine call. Although it was tempting to use the same code
3269 for functions, subroutines and functions are stored differently and this
3270 makes things awkward. */
3272 static bool
3273 resolve_call (gfc_code *c)
3275 bool t;
3276 procedure_type ptype = PROC_INTRINSIC;
3277 gfc_symbol *csym, *sym;
3278 bool no_formal_args;
3280 csym = c->symtree ? c->symtree->n.sym : NULL;
3282 if (csym && csym->ts.type != BT_UNKNOWN)
3284 gfc_error ("'%s' at %L has a type, which is not consistent with "
3285 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3286 return false;
3289 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3291 gfc_symtree *st;
3292 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3293 sym = st ? st->n.sym : NULL;
3294 if (sym && csym != sym
3295 && sym->ns == gfc_current_ns
3296 && sym->attr.flavor == FL_PROCEDURE
3297 && sym->attr.contained)
3299 sym->refs++;
3300 if (csym->attr.generic)
3301 c->symtree->n.sym = sym;
3302 else
3303 c->symtree = st;
3304 csym = c->symtree->n.sym;
3308 /* If this ia a deferred TBP, c->expr1 will be set. */
3309 if (!c->expr1 && csym)
3311 if (csym->attr.abstract)
3313 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3314 csym->name, &c->loc);
3315 return false;
3318 /* Subroutines without the RECURSIVE attribution are not allowed to
3319 call themselves. */
3320 if (is_illegal_recursion (csym, gfc_current_ns))
3322 if (csym->attr.entry && csym->ns->entries)
3323 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3324 "as subroutine '%s' is not RECURSIVE",
3325 csym->name, &c->loc, csym->ns->entries->sym->name);
3326 else
3327 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3328 "as it is not RECURSIVE", csym->name, &c->loc);
3330 t = false;
3334 /* Switch off assumed size checking and do this again for certain kinds
3335 of procedure, once the procedure itself is resolved. */
3336 need_full_assumed_size++;
3338 if (csym)
3339 ptype = csym->attr.proc;
3341 no_formal_args = csym && is_external_proc (csym)
3342 && gfc_sym_get_dummy_args (csym) == NULL;
3343 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3344 return false;
3346 /* Resume assumed_size checking. */
3347 need_full_assumed_size--;
3349 /* If external, check for usage. */
3350 if (csym && is_external_proc (csym))
3351 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3353 t = true;
3354 if (c->resolved_sym == NULL)
3356 c->resolved_isym = NULL;
3357 switch (procedure_kind (csym))
3359 case PTYPE_GENERIC:
3360 t = resolve_generic_s (c);
3361 break;
3363 case PTYPE_SPECIFIC:
3364 t = resolve_specific_s (c);
3365 break;
3367 case PTYPE_UNKNOWN:
3368 t = resolve_unknown_s (c);
3369 break;
3371 default:
3372 gfc_internal_error ("resolve_subroutine(): bad function type");
3376 /* Some checks of elemental subroutine actual arguments. */
3377 if (!resolve_elemental_actual (NULL, c))
3378 return false;
3380 return t;
3384 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3385 op1->shape and op2->shape are non-NULL return true if their shapes
3386 match. If both op1->shape and op2->shape are non-NULL return false
3387 if their shapes do not match. If either op1->shape or op2->shape is
3388 NULL, return true. */
3390 static bool
3391 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3393 bool t;
3394 int i;
3396 t = true;
3398 if (op1->shape != NULL && op2->shape != NULL)
3400 for (i = 0; i < op1->rank; i++)
3402 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3404 gfc_error ("Shapes for operands at %L and %L are not conformable",
3405 &op1->where, &op2->where);
3406 t = false;
3407 break;
3412 return t;
3416 /* Resolve an operator expression node. This can involve replacing the
3417 operation with a user defined function call. */
3419 static bool
3420 resolve_operator (gfc_expr *e)
3422 gfc_expr *op1, *op2;
3423 char msg[200];
3424 bool dual_locus_error;
3425 bool t;
3427 /* Resolve all subnodes-- give them types. */
3429 switch (e->value.op.op)
3431 default:
3432 if (!gfc_resolve_expr (e->value.op.op2))
3433 return false;
3435 /* Fall through... */
3437 case INTRINSIC_NOT:
3438 case INTRINSIC_UPLUS:
3439 case INTRINSIC_UMINUS:
3440 case INTRINSIC_PARENTHESES:
3441 if (!gfc_resolve_expr (e->value.op.op1))
3442 return false;
3443 break;
3446 /* Typecheck the new node. */
3448 op1 = e->value.op.op1;
3449 op2 = e->value.op.op2;
3450 dual_locus_error = false;
3452 if ((op1 && op1->expr_type == EXPR_NULL)
3453 || (op2 && op2->expr_type == EXPR_NULL))
3455 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3456 goto bad_op;
3459 switch (e->value.op.op)
3461 case INTRINSIC_UPLUS:
3462 case INTRINSIC_UMINUS:
3463 if (op1->ts.type == BT_INTEGER
3464 || op1->ts.type == BT_REAL
3465 || op1->ts.type == BT_COMPLEX)
3467 e->ts = op1->ts;
3468 break;
3471 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3472 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3473 goto bad_op;
3475 case INTRINSIC_PLUS:
3476 case INTRINSIC_MINUS:
3477 case INTRINSIC_TIMES:
3478 case INTRINSIC_DIVIDE:
3479 case INTRINSIC_POWER:
3480 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3482 gfc_type_convert_binary (e, 1);
3483 break;
3486 sprintf (msg,
3487 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3488 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3489 gfc_typename (&op2->ts));
3490 goto bad_op;
3492 case INTRINSIC_CONCAT:
3493 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3494 && op1->ts.kind == op2->ts.kind)
3496 e->ts.type = BT_CHARACTER;
3497 e->ts.kind = op1->ts.kind;
3498 break;
3501 sprintf (msg,
3502 _("Operands of string concatenation operator at %%L are %s/%s"),
3503 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3504 goto bad_op;
3506 case INTRINSIC_AND:
3507 case INTRINSIC_OR:
3508 case INTRINSIC_EQV:
3509 case INTRINSIC_NEQV:
3510 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3512 e->ts.type = BT_LOGICAL;
3513 e->ts.kind = gfc_kind_max (op1, op2);
3514 if (op1->ts.kind < e->ts.kind)
3515 gfc_convert_type (op1, &e->ts, 2);
3516 else if (op2->ts.kind < e->ts.kind)
3517 gfc_convert_type (op2, &e->ts, 2);
3518 break;
3521 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3522 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3523 gfc_typename (&op2->ts));
3525 goto bad_op;
3527 case INTRINSIC_NOT:
3528 if (op1->ts.type == BT_LOGICAL)
3530 e->ts.type = BT_LOGICAL;
3531 e->ts.kind = op1->ts.kind;
3532 break;
3535 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3536 gfc_typename (&op1->ts));
3537 goto bad_op;
3539 case INTRINSIC_GT:
3540 case INTRINSIC_GT_OS:
3541 case INTRINSIC_GE:
3542 case INTRINSIC_GE_OS:
3543 case INTRINSIC_LT:
3544 case INTRINSIC_LT_OS:
3545 case INTRINSIC_LE:
3546 case INTRINSIC_LE_OS:
3547 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3549 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3550 goto bad_op;
3553 /* Fall through... */
3555 case INTRINSIC_EQ:
3556 case INTRINSIC_EQ_OS:
3557 case INTRINSIC_NE:
3558 case INTRINSIC_NE_OS:
3559 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3560 && op1->ts.kind == op2->ts.kind)
3562 e->ts.type = BT_LOGICAL;
3563 e->ts.kind = gfc_default_logical_kind;
3564 break;
3567 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3569 gfc_type_convert_binary (e, 1);
3571 e->ts.type = BT_LOGICAL;
3572 e->ts.kind = gfc_default_logical_kind;
3574 if (warn_compare_reals)
3576 gfc_intrinsic_op op = e->value.op.op;
3578 /* Type conversion has made sure that the types of op1 and op2
3579 agree, so it is only necessary to check the first one. */
3580 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3581 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3582 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3584 const char *msg;
3586 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3587 msg = "Equality comparison for %s at %L";
3588 else
3589 msg = "Inequality comparison for %s at %L";
3591 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3595 break;
3598 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3599 sprintf (msg,
3600 _("Logicals at %%L must be compared with %s instead of %s"),
3601 (e->value.op.op == INTRINSIC_EQ
3602 || e->value.op.op == INTRINSIC_EQ_OS)
3603 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3604 else
3605 sprintf (msg,
3606 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3607 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3608 gfc_typename (&op2->ts));
3610 goto bad_op;
3612 case INTRINSIC_USER:
3613 if (e->value.op.uop->op == NULL)
3614 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3615 else if (op2 == NULL)
3616 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3617 e->value.op.uop->name, gfc_typename (&op1->ts));
3618 else
3620 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3621 e->value.op.uop->name, gfc_typename (&op1->ts),
3622 gfc_typename (&op2->ts));
3623 e->value.op.uop->op->sym->attr.referenced = 1;
3626 goto bad_op;
3628 case INTRINSIC_PARENTHESES:
3629 e->ts = op1->ts;
3630 if (e->ts.type == BT_CHARACTER)
3631 e->ts.u.cl = op1->ts.u.cl;
3632 break;
3634 default:
3635 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3638 /* Deal with arrayness of an operand through an operator. */
3640 t = true;
3642 switch (e->value.op.op)
3644 case INTRINSIC_PLUS:
3645 case INTRINSIC_MINUS:
3646 case INTRINSIC_TIMES:
3647 case INTRINSIC_DIVIDE:
3648 case INTRINSIC_POWER:
3649 case INTRINSIC_CONCAT:
3650 case INTRINSIC_AND:
3651 case INTRINSIC_OR:
3652 case INTRINSIC_EQV:
3653 case INTRINSIC_NEQV:
3654 case INTRINSIC_EQ:
3655 case INTRINSIC_EQ_OS:
3656 case INTRINSIC_NE:
3657 case INTRINSIC_NE_OS:
3658 case INTRINSIC_GT:
3659 case INTRINSIC_GT_OS:
3660 case INTRINSIC_GE:
3661 case INTRINSIC_GE_OS:
3662 case INTRINSIC_LT:
3663 case INTRINSIC_LT_OS:
3664 case INTRINSIC_LE:
3665 case INTRINSIC_LE_OS:
3667 if (op1->rank == 0 && op2->rank == 0)
3668 e->rank = 0;
3670 if (op1->rank == 0 && op2->rank != 0)
3672 e->rank = op2->rank;
3674 if (e->shape == NULL)
3675 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3678 if (op1->rank != 0 && op2->rank == 0)
3680 e->rank = op1->rank;
3682 if (e->shape == NULL)
3683 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3686 if (op1->rank != 0 && op2->rank != 0)
3688 if (op1->rank == op2->rank)
3690 e->rank = op1->rank;
3691 if (e->shape == NULL)
3693 t = compare_shapes (op1, op2);
3694 if (!t)
3695 e->shape = NULL;
3696 else
3697 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3700 else
3702 /* Allow higher level expressions to work. */
3703 e->rank = 0;
3705 /* Try user-defined operators, and otherwise throw an error. */
3706 dual_locus_error = true;
3707 sprintf (msg,
3708 _("Inconsistent ranks for operator at %%L and %%L"));
3709 goto bad_op;
3713 break;
3715 case INTRINSIC_PARENTHESES:
3716 case INTRINSIC_NOT:
3717 case INTRINSIC_UPLUS:
3718 case INTRINSIC_UMINUS:
3719 /* Simply copy arrayness attribute */
3720 e->rank = op1->rank;
3722 if (e->shape == NULL)
3723 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3725 break;
3727 default:
3728 break;
3731 /* Attempt to simplify the expression. */
3732 if (t)
3734 t = gfc_simplify_expr (e, 0);
3735 /* Some calls do not succeed in simplification and return false
3736 even though there is no error; e.g. variable references to
3737 PARAMETER arrays. */
3738 if (!gfc_is_constant_expr (e))
3739 t = true;
3741 return t;
3743 bad_op:
3746 match m = gfc_extend_expr (e);
3747 if (m == MATCH_YES)
3748 return true;
3749 if (m == MATCH_ERROR)
3750 return false;
3753 if (dual_locus_error)
3754 gfc_error (msg, &op1->where, &op2->where);
3755 else
3756 gfc_error (msg, &e->where);
3758 return false;
3762 /************** Array resolution subroutines **************/
3764 typedef enum
3765 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3766 comparison;
3768 /* Compare two integer expressions. */
3770 static comparison
3771 compare_bound (gfc_expr *a, gfc_expr *b)
3773 int i;
3775 if (a == NULL || a->expr_type != EXPR_CONSTANT
3776 || b == NULL || b->expr_type != EXPR_CONSTANT)
3777 return CMP_UNKNOWN;
3779 /* If either of the types isn't INTEGER, we must have
3780 raised an error earlier. */
3782 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3783 return CMP_UNKNOWN;
3785 i = mpz_cmp (a->value.integer, b->value.integer);
3787 if (i < 0)
3788 return CMP_LT;
3789 if (i > 0)
3790 return CMP_GT;
3791 return CMP_EQ;
3795 /* Compare an integer expression with an integer. */
3797 static comparison
3798 compare_bound_int (gfc_expr *a, int b)
3800 int i;
3802 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3803 return CMP_UNKNOWN;
3805 if (a->ts.type != BT_INTEGER)
3806 gfc_internal_error ("compare_bound_int(): Bad expression");
3808 i = mpz_cmp_si (a->value.integer, b);
3810 if (i < 0)
3811 return CMP_LT;
3812 if (i > 0)
3813 return CMP_GT;
3814 return CMP_EQ;
3818 /* Compare an integer expression with a mpz_t. */
3820 static comparison
3821 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3823 int i;
3825 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3826 return CMP_UNKNOWN;
3828 if (a->ts.type != BT_INTEGER)
3829 gfc_internal_error ("compare_bound_int(): Bad expression");
3831 i = mpz_cmp (a->value.integer, b);
3833 if (i < 0)
3834 return CMP_LT;
3835 if (i > 0)
3836 return CMP_GT;
3837 return CMP_EQ;
3841 /* Compute the last value of a sequence given by a triplet.
3842 Return 0 if it wasn't able to compute the last value, or if the
3843 sequence if empty, and 1 otherwise. */
3845 static int
3846 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3847 gfc_expr *stride, mpz_t last)
3849 mpz_t rem;
3851 if (start == NULL || start->expr_type != EXPR_CONSTANT
3852 || end == NULL || end->expr_type != EXPR_CONSTANT
3853 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3854 return 0;
3856 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3857 || (stride != NULL && stride->ts.type != BT_INTEGER))
3858 return 0;
3860 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3862 if (compare_bound (start, end) == CMP_GT)
3863 return 0;
3864 mpz_set (last, end->value.integer);
3865 return 1;
3868 if (compare_bound_int (stride, 0) == CMP_GT)
3870 /* Stride is positive */
3871 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3872 return 0;
3874 else
3876 /* Stride is negative */
3877 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3878 return 0;
3881 mpz_init (rem);
3882 mpz_sub (rem, end->value.integer, start->value.integer);
3883 mpz_tdiv_r (rem, rem, stride->value.integer);
3884 mpz_sub (last, end->value.integer, rem);
3885 mpz_clear (rem);
3887 return 1;
3891 /* Compare a single dimension of an array reference to the array
3892 specification. */
3894 static bool
3895 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3897 mpz_t last_value;
3899 if (ar->dimen_type[i] == DIMEN_STAR)
3901 gcc_assert (ar->stride[i] == NULL);
3902 /* This implies [*] as [*:] and [*:3] are not possible. */
3903 if (ar->start[i] == NULL)
3905 gcc_assert (ar->end[i] == NULL);
3906 return true;
3910 /* Given start, end and stride values, calculate the minimum and
3911 maximum referenced indexes. */
3913 switch (ar->dimen_type[i])
3915 case DIMEN_VECTOR:
3916 case DIMEN_THIS_IMAGE:
3917 break;
3919 case DIMEN_STAR:
3920 case DIMEN_ELEMENT:
3921 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3923 if (i < as->rank)
3924 gfc_warning ("Array reference at %L is out of bounds "
3925 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3926 mpz_get_si (ar->start[i]->value.integer),
3927 mpz_get_si (as->lower[i]->value.integer), i+1);
3928 else
3929 gfc_warning ("Array reference at %L is out of bounds "
3930 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3931 mpz_get_si (ar->start[i]->value.integer),
3932 mpz_get_si (as->lower[i]->value.integer),
3933 i + 1 - as->rank);
3934 return true;
3936 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3938 if (i < as->rank)
3939 gfc_warning ("Array reference at %L is out of bounds "
3940 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3941 mpz_get_si (ar->start[i]->value.integer),
3942 mpz_get_si (as->upper[i]->value.integer), i+1);
3943 else
3944 gfc_warning ("Array reference at %L is out of bounds "
3945 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3946 mpz_get_si (ar->start[i]->value.integer),
3947 mpz_get_si (as->upper[i]->value.integer),
3948 i + 1 - as->rank);
3949 return true;
3952 break;
3954 case DIMEN_RANGE:
3956 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3957 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3959 comparison comp_start_end = compare_bound (AR_START, AR_END);
3961 /* Check for zero stride, which is not allowed. */
3962 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3964 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3965 return false;
3968 /* if start == len || (stride > 0 && start < len)
3969 || (stride < 0 && start > len),
3970 then the array section contains at least one element. In this
3971 case, there is an out-of-bounds access if
3972 (start < lower || start > upper). */
3973 if (compare_bound (AR_START, AR_END) == CMP_EQ
3974 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3975 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3976 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3977 && comp_start_end == CMP_GT))
3979 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3981 gfc_warning ("Lower array reference at %L is out of bounds "
3982 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3983 mpz_get_si (AR_START->value.integer),
3984 mpz_get_si (as->lower[i]->value.integer), i+1);
3985 return true;
3987 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3989 gfc_warning ("Lower array reference at %L is out of bounds "
3990 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3991 mpz_get_si (AR_START->value.integer),
3992 mpz_get_si (as->upper[i]->value.integer), i+1);
3993 return true;
3997 /* If we can compute the highest index of the array section,
3998 then it also has to be between lower and upper. */
3999 mpz_init (last_value);
4000 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4001 last_value))
4003 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4005 gfc_warning ("Upper array reference at %L is out of bounds "
4006 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4007 mpz_get_si (last_value),
4008 mpz_get_si (as->lower[i]->value.integer), i+1);
4009 mpz_clear (last_value);
4010 return true;
4012 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4014 gfc_warning ("Upper array reference at %L is out of bounds "
4015 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4016 mpz_get_si (last_value),
4017 mpz_get_si (as->upper[i]->value.integer), i+1);
4018 mpz_clear (last_value);
4019 return true;
4022 mpz_clear (last_value);
4024 #undef AR_START
4025 #undef AR_END
4027 break;
4029 default:
4030 gfc_internal_error ("check_dimension(): Bad array reference");
4033 return true;
4037 /* Compare an array reference with an array specification. */
4039 static bool
4040 compare_spec_to_ref (gfc_array_ref *ar)
4042 gfc_array_spec *as;
4043 int i;
4045 as = ar->as;
4046 i = as->rank - 1;
4047 /* TODO: Full array sections are only allowed as actual parameters. */
4048 if (as->type == AS_ASSUMED_SIZE
4049 && (/*ar->type == AR_FULL
4050 ||*/ (ar->type == AR_SECTION
4051 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4053 gfc_error ("Rightmost upper bound of assumed size array section "
4054 "not specified at %L", &ar->where);
4055 return false;
4058 if (ar->type == AR_FULL)
4059 return true;
4061 if (as->rank != ar->dimen)
4063 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4064 &ar->where, ar->dimen, as->rank);
4065 return false;
4068 /* ar->codimen == 0 is a local array. */
4069 if (as->corank != ar->codimen && ar->codimen != 0)
4071 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4072 &ar->where, ar->codimen, as->corank);
4073 return false;
4076 for (i = 0; i < as->rank; i++)
4077 if (!check_dimension (i, ar, as))
4078 return false;
4080 /* Local access has no coarray spec. */
4081 if (ar->codimen != 0)
4082 for (i = as->rank; i < as->rank + as->corank; i++)
4084 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4085 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4087 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4088 i + 1 - as->rank, &ar->where);
4089 return false;
4091 if (!check_dimension (i, ar, as))
4092 return false;
4095 return true;
4099 /* Resolve one part of an array index. */
4101 static bool
4102 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4103 int force_index_integer_kind)
4105 gfc_typespec ts;
4107 if (index == NULL)
4108 return true;
4110 if (!gfc_resolve_expr (index))
4111 return false;
4113 if (check_scalar && index->rank != 0)
4115 gfc_error ("Array index at %L must be scalar", &index->where);
4116 return false;
4119 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4121 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4122 &index->where, gfc_basic_typename (index->ts.type));
4123 return false;
4126 if (index->ts.type == BT_REAL)
4127 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4128 &index->where))
4129 return false;
4131 if ((index->ts.kind != gfc_index_integer_kind
4132 && force_index_integer_kind)
4133 || index->ts.type != BT_INTEGER)
4135 gfc_clear_ts (&ts);
4136 ts.type = BT_INTEGER;
4137 ts.kind = gfc_index_integer_kind;
4139 gfc_convert_type_warn (index, &ts, 2, 0);
4142 return true;
4145 /* Resolve one part of an array index. */
4147 bool
4148 gfc_resolve_index (gfc_expr *index, int check_scalar)
4150 return gfc_resolve_index_1 (index, check_scalar, 1);
4153 /* Resolve a dim argument to an intrinsic function. */
4155 bool
4156 gfc_resolve_dim_arg (gfc_expr *dim)
4158 if (dim == NULL)
4159 return true;
4161 if (!gfc_resolve_expr (dim))
4162 return false;
4164 if (dim->rank != 0)
4166 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4167 return false;
4171 if (dim->ts.type != BT_INTEGER)
4173 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4174 return false;
4177 if (dim->ts.kind != gfc_index_integer_kind)
4179 gfc_typespec ts;
4181 gfc_clear_ts (&ts);
4182 ts.type = BT_INTEGER;
4183 ts.kind = gfc_index_integer_kind;
4185 gfc_convert_type_warn (dim, &ts, 2, 0);
4188 return true;
4191 /* Given an expression that contains array references, update those array
4192 references to point to the right array specifications. While this is
4193 filled in during matching, this information is difficult to save and load
4194 in a module, so we take care of it here.
4196 The idea here is that the original array reference comes from the
4197 base symbol. We traverse the list of reference structures, setting
4198 the stored reference to references. Component references can
4199 provide an additional array specification. */
4201 static void
4202 find_array_spec (gfc_expr *e)
4204 gfc_array_spec *as;
4205 gfc_component *c;
4206 gfc_ref *ref;
4208 if (e->symtree->n.sym->ts.type == BT_CLASS)
4209 as = CLASS_DATA (e->symtree->n.sym)->as;
4210 else
4211 as = e->symtree->n.sym->as;
4213 for (ref = e->ref; ref; ref = ref->next)
4214 switch (ref->type)
4216 case REF_ARRAY:
4217 if (as == NULL)
4218 gfc_internal_error ("find_array_spec(): Missing spec");
4220 ref->u.ar.as = as;
4221 as = NULL;
4222 break;
4224 case REF_COMPONENT:
4225 c = ref->u.c.component;
4226 if (c->attr.dimension)
4228 if (as != NULL)
4229 gfc_internal_error ("find_array_spec(): unused as(1)");
4230 as = c->as;
4233 break;
4235 case REF_SUBSTRING:
4236 break;
4239 if (as != NULL)
4240 gfc_internal_error ("find_array_spec(): unused as(2)");
4244 /* Resolve an array reference. */
4246 static bool
4247 resolve_array_ref (gfc_array_ref *ar)
4249 int i, check_scalar;
4250 gfc_expr *e;
4252 for (i = 0; i < ar->dimen + ar->codimen; i++)
4254 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4256 /* Do not force gfc_index_integer_kind for the start. We can
4257 do fine with any integer kind. This avoids temporary arrays
4258 created for indexing with a vector. */
4259 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4260 return false;
4261 if (!gfc_resolve_index (ar->end[i], check_scalar))
4262 return false;
4263 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4264 return false;
4266 e = ar->start[i];
4268 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4269 switch (e->rank)
4271 case 0:
4272 ar->dimen_type[i] = DIMEN_ELEMENT;
4273 break;
4275 case 1:
4276 ar->dimen_type[i] = DIMEN_VECTOR;
4277 if (e->expr_type == EXPR_VARIABLE
4278 && e->symtree->n.sym->ts.type == BT_DERIVED)
4279 ar->start[i] = gfc_get_parentheses (e);
4280 break;
4282 default:
4283 gfc_error ("Array index at %L is an array of rank %d",
4284 &ar->c_where[i], e->rank);
4285 return false;
4288 /* Fill in the upper bound, which may be lower than the
4289 specified one for something like a(2:10:5), which is
4290 identical to a(2:7:5). Only relevant for strides not equal
4291 to one. Don't try a division by zero. */
4292 if (ar->dimen_type[i] == DIMEN_RANGE
4293 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4294 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4295 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4297 mpz_t size, end;
4299 if (gfc_ref_dimen_size (ar, i, &size, &end))
4301 if (ar->end[i] == NULL)
4303 ar->end[i] =
4304 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4305 &ar->where);
4306 mpz_set (ar->end[i]->value.integer, end);
4308 else if (ar->end[i]->ts.type == BT_INTEGER
4309 && ar->end[i]->expr_type == EXPR_CONSTANT)
4311 mpz_set (ar->end[i]->value.integer, end);
4313 else
4314 gcc_unreachable ();
4316 mpz_clear (size);
4317 mpz_clear (end);
4322 if (ar->type == AR_FULL)
4324 if (ar->as->rank == 0)
4325 ar->type = AR_ELEMENT;
4327 /* Make sure array is the same as array(:,:), this way
4328 we don't need to special case all the time. */
4329 ar->dimen = ar->as->rank;
4330 for (i = 0; i < ar->dimen; i++)
4332 ar->dimen_type[i] = DIMEN_RANGE;
4334 gcc_assert (ar->start[i] == NULL);
4335 gcc_assert (ar->end[i] == NULL);
4336 gcc_assert (ar->stride[i] == NULL);
4340 /* If the reference type is unknown, figure out what kind it is. */
4342 if (ar->type == AR_UNKNOWN)
4344 ar->type = AR_ELEMENT;
4345 for (i = 0; i < ar->dimen; i++)
4346 if (ar->dimen_type[i] == DIMEN_RANGE
4347 || ar->dimen_type[i] == DIMEN_VECTOR)
4349 ar->type = AR_SECTION;
4350 break;
4354 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4355 return false;
4357 if (ar->as->corank && ar->codimen == 0)
4359 int n;
4360 ar->codimen = ar->as->corank;
4361 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4362 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4365 return true;
4369 static bool
4370 resolve_substring (gfc_ref *ref)
4372 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4374 if (ref->u.ss.start != NULL)
4376 if (!gfc_resolve_expr (ref->u.ss.start))
4377 return false;
4379 if (ref->u.ss.start->ts.type != BT_INTEGER)
4381 gfc_error ("Substring start index at %L must be of type INTEGER",
4382 &ref->u.ss.start->where);
4383 return false;
4386 if (ref->u.ss.start->rank != 0)
4388 gfc_error ("Substring start index at %L must be scalar",
4389 &ref->u.ss.start->where);
4390 return false;
4393 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4394 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4395 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4397 gfc_error ("Substring start index at %L is less than one",
4398 &ref->u.ss.start->where);
4399 return false;
4403 if (ref->u.ss.end != NULL)
4405 if (!gfc_resolve_expr (ref->u.ss.end))
4406 return false;
4408 if (ref->u.ss.end->ts.type != BT_INTEGER)
4410 gfc_error ("Substring end index at %L must be of type INTEGER",
4411 &ref->u.ss.end->where);
4412 return false;
4415 if (ref->u.ss.end->rank != 0)
4417 gfc_error ("Substring end index at %L must be scalar",
4418 &ref->u.ss.end->where);
4419 return false;
4422 if (ref->u.ss.length != NULL
4423 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4424 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4425 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4427 gfc_error ("Substring end index at %L exceeds the string length",
4428 &ref->u.ss.start->where);
4429 return false;
4432 if (compare_bound_mpz_t (ref->u.ss.end,
4433 gfc_integer_kinds[k].huge) == CMP_GT
4434 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4435 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4437 gfc_error ("Substring end index at %L is too large",
4438 &ref->u.ss.end->where);
4439 return false;
4443 return true;
4447 /* This function supplies missing substring charlens. */
4449 void
4450 gfc_resolve_substring_charlen (gfc_expr *e)
4452 gfc_ref *char_ref;
4453 gfc_expr *start, *end;
4455 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4456 if (char_ref->type == REF_SUBSTRING)
4457 break;
4459 if (!char_ref)
4460 return;
4462 gcc_assert (char_ref->next == NULL);
4464 if (e->ts.u.cl)
4466 if (e->ts.u.cl->length)
4467 gfc_free_expr (e->ts.u.cl->length);
4468 else if (e->expr_type == EXPR_VARIABLE
4469 && e->symtree->n.sym->attr.dummy)
4470 return;
4473 e->ts.type = BT_CHARACTER;
4474 e->ts.kind = gfc_default_character_kind;
4476 if (!e->ts.u.cl)
4477 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4479 if (char_ref->u.ss.start)
4480 start = gfc_copy_expr (char_ref->u.ss.start);
4481 else
4482 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4484 if (char_ref->u.ss.end)
4485 end = gfc_copy_expr (char_ref->u.ss.end);
4486 else if (e->expr_type == EXPR_VARIABLE)
4487 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4488 else
4489 end = NULL;
4491 if (!start || !end)
4493 gfc_free_expr (start);
4494 gfc_free_expr (end);
4495 return;
4498 /* Length = (end - start +1). */
4499 e->ts.u.cl->length = gfc_subtract (end, start);
4500 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4501 gfc_get_int_expr (gfc_default_integer_kind,
4502 NULL, 1));
4504 e->ts.u.cl->length->ts.type = BT_INTEGER;
4505 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4507 /* Make sure that the length is simplified. */
4508 gfc_simplify_expr (e->ts.u.cl->length, 1);
4509 gfc_resolve_expr (e->ts.u.cl->length);
4513 /* Resolve subtype references. */
4515 static bool
4516 resolve_ref (gfc_expr *expr)
4518 int current_part_dimension, n_components, seen_part_dimension;
4519 gfc_ref *ref;
4521 for (ref = expr->ref; ref; ref = ref->next)
4522 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4524 find_array_spec (expr);
4525 break;
4528 for (ref = expr->ref; ref; ref = ref->next)
4529 switch (ref->type)
4531 case REF_ARRAY:
4532 if (!resolve_array_ref (&ref->u.ar))
4533 return false;
4534 break;
4536 case REF_COMPONENT:
4537 break;
4539 case REF_SUBSTRING:
4540 if (!resolve_substring (ref))
4541 return false;
4542 break;
4545 /* Check constraints on part references. */
4547 current_part_dimension = 0;
4548 seen_part_dimension = 0;
4549 n_components = 0;
4551 for (ref = expr->ref; ref; ref = ref->next)
4553 switch (ref->type)
4555 case REF_ARRAY:
4556 switch (ref->u.ar.type)
4558 case AR_FULL:
4559 /* Coarray scalar. */
4560 if (ref->u.ar.as->rank == 0)
4562 current_part_dimension = 0;
4563 break;
4565 /* Fall through. */
4566 case AR_SECTION:
4567 current_part_dimension = 1;
4568 break;
4570 case AR_ELEMENT:
4571 current_part_dimension = 0;
4572 break;
4574 case AR_UNKNOWN:
4575 gfc_internal_error ("resolve_ref(): Bad array reference");
4578 break;
4580 case REF_COMPONENT:
4581 if (current_part_dimension || seen_part_dimension)
4583 /* F03:C614. */
4584 if (ref->u.c.component->attr.pointer
4585 || ref->u.c.component->attr.proc_pointer
4586 || (ref->u.c.component->ts.type == BT_CLASS
4587 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4589 gfc_error ("Component to the right of a part reference "
4590 "with nonzero rank must not have the POINTER "
4591 "attribute at %L", &expr->where);
4592 return false;
4594 else if (ref->u.c.component->attr.allocatable
4595 || (ref->u.c.component->ts.type == BT_CLASS
4596 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4599 gfc_error ("Component to the right of a part reference "
4600 "with nonzero rank must not have the ALLOCATABLE "
4601 "attribute at %L", &expr->where);
4602 return false;
4606 n_components++;
4607 break;
4609 case REF_SUBSTRING:
4610 break;
4613 if (((ref->type == REF_COMPONENT && n_components > 1)
4614 || ref->next == NULL)
4615 && current_part_dimension
4616 && seen_part_dimension)
4618 gfc_error ("Two or more part references with nonzero rank must "
4619 "not be specified at %L", &expr->where);
4620 return false;
4623 if (ref->type == REF_COMPONENT)
4625 if (current_part_dimension)
4626 seen_part_dimension = 1;
4628 /* reset to make sure */
4629 current_part_dimension = 0;
4633 return true;
4637 /* Given an expression, determine its shape. This is easier than it sounds.
4638 Leaves the shape array NULL if it is not possible to determine the shape. */
4640 static void
4641 expression_shape (gfc_expr *e)
4643 mpz_t array[GFC_MAX_DIMENSIONS];
4644 int i;
4646 if (e->rank <= 0 || e->shape != NULL)
4647 return;
4649 for (i = 0; i < e->rank; i++)
4650 if (!gfc_array_dimen_size (e, i, &array[i]))
4651 goto fail;
4653 e->shape = gfc_get_shape (e->rank);
4655 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4657 return;
4659 fail:
4660 for (i--; i >= 0; i--)
4661 mpz_clear (array[i]);
4665 /* Given a variable expression node, compute the rank of the expression by
4666 examining the base symbol and any reference structures it may have. */
4668 static void
4669 expression_rank (gfc_expr *e)
4671 gfc_ref *ref;
4672 int i, rank;
4674 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4675 could lead to serious confusion... */
4676 gcc_assert (e->expr_type != EXPR_COMPCALL);
4678 if (e->ref == NULL)
4680 if (e->expr_type == EXPR_ARRAY)
4681 goto done;
4682 /* Constructors can have a rank different from one via RESHAPE(). */
4684 if (e->symtree == NULL)
4686 e->rank = 0;
4687 goto done;
4690 e->rank = (e->symtree->n.sym->as == NULL)
4691 ? 0 : e->symtree->n.sym->as->rank;
4692 goto done;
4695 rank = 0;
4697 for (ref = e->ref; ref; ref = ref->next)
4699 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4700 && ref->u.c.component->attr.function && !ref->next)
4701 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4703 if (ref->type != REF_ARRAY)
4704 continue;
4706 if (ref->u.ar.type == AR_FULL)
4708 rank = ref->u.ar.as->rank;
4709 break;
4712 if (ref->u.ar.type == AR_SECTION)
4714 /* Figure out the rank of the section. */
4715 if (rank != 0)
4716 gfc_internal_error ("expression_rank(): Two array specs");
4718 for (i = 0; i < ref->u.ar.dimen; i++)
4719 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4720 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4721 rank++;
4723 break;
4727 e->rank = rank;
4729 done:
4730 expression_shape (e);
4734 static void
4735 add_caf_get_intrinsic (gfc_expr *e)
4737 gfc_expr *wrapper, *tmp_expr;
4738 gfc_ref *ref;
4739 int n;
4741 for (ref = e->ref; ref; ref = ref->next)
4742 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4743 break;
4744 if (ref == NULL)
4745 return;
4747 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4748 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4749 return;
4751 tmp_expr = XCNEW (gfc_expr);
4752 *tmp_expr = *e;
4753 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4754 "caf_get", tmp_expr->where, 1, tmp_expr);
4755 wrapper->ts = e->ts;
4756 wrapper->rank = e->rank;
4757 if (e->rank)
4758 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4759 *e = *wrapper;
4760 free (wrapper);
4764 static void
4765 remove_caf_get_intrinsic (gfc_expr *e)
4767 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4768 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4769 gfc_expr *e2 = e->value.function.actual->expr;
4770 e->value.function.actual->expr = NULL;
4771 gfc_free_actual_arglist (e->value.function.actual);
4772 gfc_free_shape (&e->shape, e->rank);
4773 *e = *e2;
4774 free (e2);
4778 /* Resolve a variable expression. */
4780 static bool
4781 resolve_variable (gfc_expr *e)
4783 gfc_symbol *sym;
4784 bool t;
4786 t = true;
4788 if (e->symtree == NULL)
4789 return false;
4790 sym = e->symtree->n.sym;
4792 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4793 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4794 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4796 if (!actual_arg || inquiry_argument)
4798 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4799 "be used as actual argument", sym->name, &e->where);
4800 return false;
4803 /* TS 29113, 407b. */
4804 else if (e->ts.type == BT_ASSUMED)
4806 if (!actual_arg)
4808 gfc_error ("Assumed-type variable %s at %L may only be used "
4809 "as actual argument", sym->name, &e->where);
4810 return false;
4812 else if (inquiry_argument && !first_actual_arg)
4814 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4815 for all inquiry functions in resolve_function; the reason is
4816 that the function-name resolution happens too late in that
4817 function. */
4818 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4819 "an inquiry function shall be the first argument",
4820 sym->name, &e->where);
4821 return false;
4824 /* TS 29113, C535b. */
4825 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4826 && CLASS_DATA (sym)->as
4827 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4828 || (sym->ts.type != BT_CLASS && sym->as
4829 && sym->as->type == AS_ASSUMED_RANK))
4831 if (!actual_arg)
4833 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4834 "actual argument", sym->name, &e->where);
4835 return false;
4837 else if (inquiry_argument && !first_actual_arg)
4839 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4840 for all inquiry functions in resolve_function; the reason is
4841 that the function-name resolution happens too late in that
4842 function. */
4843 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4844 "to an inquiry function shall be the first argument",
4845 sym->name, &e->where);
4846 return false;
4850 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4851 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4852 && e->ref->next == NULL))
4854 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4855 "a subobject reference", sym->name, &e->ref->u.ar.where);
4856 return false;
4858 /* TS 29113, 407b. */
4859 else if (e->ts.type == BT_ASSUMED && e->ref
4860 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4861 && e->ref->next == NULL))
4863 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4864 "reference", sym->name, &e->ref->u.ar.where);
4865 return false;
4868 /* TS 29113, C535b. */
4869 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4870 && CLASS_DATA (sym)->as
4871 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4872 || (sym->ts.type != BT_CLASS && sym->as
4873 && sym->as->type == AS_ASSUMED_RANK))
4874 && e->ref
4875 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4876 && e->ref->next == NULL))
4878 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4879 "reference", sym->name, &e->ref->u.ar.where);
4880 return false;
4884 /* If this is an associate-name, it may be parsed with an array reference
4885 in error even though the target is scalar. Fail directly in this case.
4886 TODO Understand why class scalar expressions must be excluded. */
4887 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4889 if (sym->ts.type == BT_CLASS)
4890 gfc_fix_class_refs (e);
4891 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4892 return false;
4895 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4896 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4898 /* On the other hand, the parser may not have known this is an array;
4899 in this case, we have to add a FULL reference. */
4900 if (sym->assoc && sym->attr.dimension && !e->ref)
4902 e->ref = gfc_get_ref ();
4903 e->ref->type = REF_ARRAY;
4904 e->ref->u.ar.type = AR_FULL;
4905 e->ref->u.ar.dimen = 0;
4908 if (e->ref && !resolve_ref (e))
4909 return false;
4911 if (sym->attr.flavor == FL_PROCEDURE
4912 && (!sym->attr.function
4913 || (sym->attr.function && sym->result
4914 && sym->result->attr.proc_pointer
4915 && !sym->result->attr.function)))
4917 e->ts.type = BT_PROCEDURE;
4918 goto resolve_procedure;
4921 if (sym->ts.type != BT_UNKNOWN)
4922 gfc_variable_attr (e, &e->ts);
4923 else
4925 /* Must be a simple variable reference. */
4926 if (!gfc_set_default_type (sym, 1, sym->ns))
4927 return false;
4928 e->ts = sym->ts;
4931 if (check_assumed_size_reference (sym, e))
4932 return false;
4934 /* Deal with forward references to entries during gfc_resolve_code, to
4935 satisfy, at least partially, 12.5.2.5. */
4936 if (gfc_current_ns->entries
4937 && current_entry_id == sym->entry_id
4938 && cs_base
4939 && cs_base->current
4940 && cs_base->current->op != EXEC_ENTRY)
4942 gfc_entry_list *entry;
4943 gfc_formal_arglist *formal;
4944 int n;
4945 bool seen, saved_specification_expr;
4947 /* If the symbol is a dummy... */
4948 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4950 entry = gfc_current_ns->entries;
4951 seen = false;
4953 /* ...test if the symbol is a parameter of previous entries. */
4954 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4955 for (formal = entry->sym->formal; formal; formal = formal->next)
4957 if (formal->sym && sym->name == formal->sym->name)
4959 seen = true;
4960 break;
4964 /* If it has not been seen as a dummy, this is an error. */
4965 if (!seen)
4967 if (specification_expr)
4968 gfc_error ("Variable '%s', used in a specification expression"
4969 ", is referenced at %L before the ENTRY statement "
4970 "in which it is a parameter",
4971 sym->name, &cs_base->current->loc);
4972 else
4973 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4974 "statement in which it is a parameter",
4975 sym->name, &cs_base->current->loc);
4976 t = false;
4980 /* Now do the same check on the specification expressions. */
4981 saved_specification_expr = specification_expr;
4982 specification_expr = true;
4983 if (sym->ts.type == BT_CHARACTER
4984 && !gfc_resolve_expr (sym->ts.u.cl->length))
4985 t = false;
4987 if (sym->as)
4988 for (n = 0; n < sym->as->rank; n++)
4990 if (!gfc_resolve_expr (sym->as->lower[n]))
4991 t = false;
4992 if (!gfc_resolve_expr (sym->as->upper[n]))
4993 t = false;
4995 specification_expr = saved_specification_expr;
4997 if (t)
4998 /* Update the symbol's entry level. */
4999 sym->entry_id = current_entry_id + 1;
5002 /* If a symbol has been host_associated mark it. This is used latter,
5003 to identify if aliasing is possible via host association. */
5004 if (sym->attr.flavor == FL_VARIABLE
5005 && gfc_current_ns->parent
5006 && (gfc_current_ns->parent == sym->ns
5007 || (gfc_current_ns->parent->parent
5008 && gfc_current_ns->parent->parent == sym->ns)))
5009 sym->attr.host_assoc = 1;
5011 resolve_procedure:
5012 if (t && !resolve_procedure_expression (e))
5013 t = false;
5015 /* F2008, C617 and C1229. */
5016 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5017 && gfc_is_coindexed (e))
5019 gfc_ref *ref, *ref2 = NULL;
5021 for (ref = e->ref; ref; ref = ref->next)
5023 if (ref->type == REF_COMPONENT)
5024 ref2 = ref;
5025 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5026 break;
5029 for ( ; ref; ref = ref->next)
5030 if (ref->type == REF_COMPONENT)
5031 break;
5033 /* Expression itself is not coindexed object. */
5034 if (ref && e->ts.type == BT_CLASS)
5036 gfc_error ("Polymorphic subobject of coindexed object at %L",
5037 &e->where);
5038 t = false;
5041 /* Expression itself is coindexed object. */
5042 if (ref == NULL)
5044 gfc_component *c;
5045 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5046 for ( ; c; c = c->next)
5047 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5049 gfc_error ("Coindexed object with polymorphic allocatable "
5050 "subcomponent at %L", &e->where);
5051 t = false;
5052 break;
5057 if (t)
5058 expression_rank (e);
5060 if (t && gfc_option.coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5061 add_caf_get_intrinsic (e);
5063 return t;
5067 /* Checks to see that the correct symbol has been host associated.
5068 The only situation where this arises is that in which a twice
5069 contained function is parsed after the host association is made.
5070 Therefore, on detecting this, change the symbol in the expression
5071 and convert the array reference into an actual arglist if the old
5072 symbol is a variable. */
5073 static bool
5074 check_host_association (gfc_expr *e)
5076 gfc_symbol *sym, *old_sym;
5077 gfc_symtree *st;
5078 int n;
5079 gfc_ref *ref;
5080 gfc_actual_arglist *arg, *tail = NULL;
5081 bool retval = e->expr_type == EXPR_FUNCTION;
5083 /* If the expression is the result of substitution in
5084 interface.c(gfc_extend_expr) because there is no way in
5085 which the host association can be wrong. */
5086 if (e->symtree == NULL
5087 || e->symtree->n.sym == NULL
5088 || e->user_operator)
5089 return retval;
5091 old_sym = e->symtree->n.sym;
5093 if (gfc_current_ns->parent
5094 && old_sym->ns != gfc_current_ns)
5096 /* Use the 'USE' name so that renamed module symbols are
5097 correctly handled. */
5098 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5100 if (sym && old_sym != sym
5101 && sym->ts.type == old_sym->ts.type
5102 && sym->attr.flavor == FL_PROCEDURE
5103 && sym->attr.contained)
5105 /* Clear the shape, since it might not be valid. */
5106 gfc_free_shape (&e->shape, e->rank);
5108 /* Give the expression the right symtree! */
5109 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5110 gcc_assert (st != NULL);
5112 if (old_sym->attr.flavor == FL_PROCEDURE
5113 || e->expr_type == EXPR_FUNCTION)
5115 /* Original was function so point to the new symbol, since
5116 the actual argument list is already attached to the
5117 expression. */
5118 e->value.function.esym = NULL;
5119 e->symtree = st;
5121 else
5123 /* Original was variable so convert array references into
5124 an actual arglist. This does not need any checking now
5125 since resolve_function will take care of it. */
5126 e->value.function.actual = NULL;
5127 e->expr_type = EXPR_FUNCTION;
5128 e->symtree = st;
5130 /* Ambiguity will not arise if the array reference is not
5131 the last reference. */
5132 for (ref = e->ref; ref; ref = ref->next)
5133 if (ref->type == REF_ARRAY && ref->next == NULL)
5134 break;
5136 gcc_assert (ref->type == REF_ARRAY);
5138 /* Grab the start expressions from the array ref and
5139 copy them into actual arguments. */
5140 for (n = 0; n < ref->u.ar.dimen; n++)
5142 arg = gfc_get_actual_arglist ();
5143 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5144 if (e->value.function.actual == NULL)
5145 tail = e->value.function.actual = arg;
5146 else
5148 tail->next = arg;
5149 tail = arg;
5153 /* Dump the reference list and set the rank. */
5154 gfc_free_ref_list (e->ref);
5155 e->ref = NULL;
5156 e->rank = sym->as ? sym->as->rank : 0;
5159 gfc_resolve_expr (e);
5160 sym->refs++;
5163 /* This might have changed! */
5164 return e->expr_type == EXPR_FUNCTION;
5168 static void
5169 gfc_resolve_character_operator (gfc_expr *e)
5171 gfc_expr *op1 = e->value.op.op1;
5172 gfc_expr *op2 = e->value.op.op2;
5173 gfc_expr *e1 = NULL;
5174 gfc_expr *e2 = NULL;
5176 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5178 if (op1->ts.u.cl && op1->ts.u.cl->length)
5179 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5180 else if (op1->expr_type == EXPR_CONSTANT)
5181 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5182 op1->value.character.length);
5184 if (op2->ts.u.cl && op2->ts.u.cl->length)
5185 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5186 else if (op2->expr_type == EXPR_CONSTANT)
5187 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5188 op2->value.character.length);
5190 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5192 if (!e1 || !e2)
5194 gfc_free_expr (e1);
5195 gfc_free_expr (e2);
5197 return;
5200 e->ts.u.cl->length = gfc_add (e1, e2);
5201 e->ts.u.cl->length->ts.type = BT_INTEGER;
5202 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5203 gfc_simplify_expr (e->ts.u.cl->length, 0);
5204 gfc_resolve_expr (e->ts.u.cl->length);
5206 return;
5210 /* Ensure that an character expression has a charlen and, if possible, a
5211 length expression. */
5213 static void
5214 fixup_charlen (gfc_expr *e)
5216 /* The cases fall through so that changes in expression type and the need
5217 for multiple fixes are picked up. In all circumstances, a charlen should
5218 be available for the middle end to hang a backend_decl on. */
5219 switch (e->expr_type)
5221 case EXPR_OP:
5222 gfc_resolve_character_operator (e);
5224 case EXPR_ARRAY:
5225 if (e->expr_type == EXPR_ARRAY)
5226 gfc_resolve_character_array_constructor (e);
5228 case EXPR_SUBSTRING:
5229 if (!e->ts.u.cl && e->ref)
5230 gfc_resolve_substring_charlen (e);
5232 default:
5233 if (!e->ts.u.cl)
5234 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5236 break;
5241 /* Update an actual argument to include the passed-object for type-bound
5242 procedures at the right position. */
5244 static gfc_actual_arglist*
5245 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5246 const char *name)
5248 gcc_assert (argpos > 0);
5250 if (argpos == 1)
5252 gfc_actual_arglist* result;
5254 result = gfc_get_actual_arglist ();
5255 result->expr = po;
5256 result->next = lst;
5257 if (name)
5258 result->name = name;
5260 return result;
5263 if (lst)
5264 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5265 else
5266 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5267 return lst;
5271 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5273 static gfc_expr*
5274 extract_compcall_passed_object (gfc_expr* e)
5276 gfc_expr* po;
5278 gcc_assert (e->expr_type == EXPR_COMPCALL);
5280 if (e->value.compcall.base_object)
5281 po = gfc_copy_expr (e->value.compcall.base_object);
5282 else
5284 po = gfc_get_expr ();
5285 po->expr_type = EXPR_VARIABLE;
5286 po->symtree = e->symtree;
5287 po->ref = gfc_copy_ref (e->ref);
5288 po->where = e->where;
5291 if (!gfc_resolve_expr (po))
5292 return NULL;
5294 return po;
5298 /* Update the arglist of an EXPR_COMPCALL expression to include the
5299 passed-object. */
5301 static bool
5302 update_compcall_arglist (gfc_expr* e)
5304 gfc_expr* po;
5305 gfc_typebound_proc* tbp;
5307 tbp = e->value.compcall.tbp;
5309 if (tbp->error)
5310 return false;
5312 po = extract_compcall_passed_object (e);
5313 if (!po)
5314 return false;
5316 if (tbp->nopass || e->value.compcall.ignore_pass)
5318 gfc_free_expr (po);
5319 return true;
5322 gcc_assert (tbp->pass_arg_num > 0);
5323 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5324 tbp->pass_arg_num,
5325 tbp->pass_arg);
5327 return true;
5331 /* Extract the passed object from a PPC call (a copy of it). */
5333 static gfc_expr*
5334 extract_ppc_passed_object (gfc_expr *e)
5336 gfc_expr *po;
5337 gfc_ref **ref;
5339 po = gfc_get_expr ();
5340 po->expr_type = EXPR_VARIABLE;
5341 po->symtree = e->symtree;
5342 po->ref = gfc_copy_ref (e->ref);
5343 po->where = e->where;
5345 /* Remove PPC reference. */
5346 ref = &po->ref;
5347 while ((*ref)->next)
5348 ref = &(*ref)->next;
5349 gfc_free_ref_list (*ref);
5350 *ref = NULL;
5352 if (!gfc_resolve_expr (po))
5353 return NULL;
5355 return po;
5359 /* Update the actual arglist of a procedure pointer component to include the
5360 passed-object. */
5362 static bool
5363 update_ppc_arglist (gfc_expr* e)
5365 gfc_expr* po;
5366 gfc_component *ppc;
5367 gfc_typebound_proc* tb;
5369 ppc = gfc_get_proc_ptr_comp (e);
5370 if (!ppc)
5371 return false;
5373 tb = ppc->tb;
5375 if (tb->error)
5376 return false;
5377 else if (tb->nopass)
5378 return true;
5380 po = extract_ppc_passed_object (e);
5381 if (!po)
5382 return false;
5384 /* F08:R739. */
5385 if (po->rank != 0)
5387 gfc_error ("Passed-object at %L must be scalar", &e->where);
5388 return false;
5391 /* F08:C611. */
5392 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5394 gfc_error ("Base object for procedure-pointer component call at %L is of"
5395 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5396 return false;
5399 gcc_assert (tb->pass_arg_num > 0);
5400 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5401 tb->pass_arg_num,
5402 tb->pass_arg);
5404 return true;
5408 /* Check that the object a TBP is called on is valid, i.e. it must not be
5409 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5411 static bool
5412 check_typebound_baseobject (gfc_expr* e)
5414 gfc_expr* base;
5415 bool return_value = false;
5417 base = extract_compcall_passed_object (e);
5418 if (!base)
5419 return false;
5421 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5423 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5424 return false;
5426 /* F08:C611. */
5427 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5429 gfc_error ("Base object for type-bound procedure call at %L is of"
5430 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5431 goto cleanup;
5434 /* F08:C1230. If the procedure called is NOPASS,
5435 the base object must be scalar. */
5436 if (e->value.compcall.tbp->nopass && base->rank != 0)
5438 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5439 " be scalar", &e->where);
5440 goto cleanup;
5443 return_value = true;
5445 cleanup:
5446 gfc_free_expr (base);
5447 return return_value;
5451 /* Resolve a call to a type-bound procedure, either function or subroutine,
5452 statically from the data in an EXPR_COMPCALL expression. The adapted
5453 arglist and the target-procedure symtree are returned. */
5455 static bool
5456 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5457 gfc_actual_arglist** actual)
5459 gcc_assert (e->expr_type == EXPR_COMPCALL);
5460 gcc_assert (!e->value.compcall.tbp->is_generic);
5462 /* Update the actual arglist for PASS. */
5463 if (!update_compcall_arglist (e))
5464 return false;
5466 *actual = e->value.compcall.actual;
5467 *target = e->value.compcall.tbp->u.specific;
5469 gfc_free_ref_list (e->ref);
5470 e->ref = NULL;
5471 e->value.compcall.actual = NULL;
5473 /* If we find a deferred typebound procedure, check for derived types
5474 that an overriding typebound procedure has not been missed. */
5475 if (e->value.compcall.name
5476 && !e->value.compcall.tbp->non_overridable
5477 && e->value.compcall.base_object
5478 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5480 gfc_symtree *st;
5481 gfc_symbol *derived;
5483 /* Use the derived type of the base_object. */
5484 derived = e->value.compcall.base_object->ts.u.derived;
5485 st = NULL;
5487 /* If necessary, go through the inheritance chain. */
5488 while (!st && derived)
5490 /* Look for the typebound procedure 'name'. */
5491 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5492 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5493 e->value.compcall.name);
5494 if (!st)
5495 derived = gfc_get_derived_super_type (derived);
5498 /* Now find the specific name in the derived type namespace. */
5499 if (st && st->n.tb && st->n.tb->u.specific)
5500 gfc_find_sym_tree (st->n.tb->u.specific->name,
5501 derived->ns, 1, &st);
5502 if (st)
5503 *target = st;
5505 return true;
5509 /* Get the ultimate declared type from an expression. In addition,
5510 return the last class/derived type reference and the copy of the
5511 reference list. If check_types is set true, derived types are
5512 identified as well as class references. */
5513 static gfc_symbol*
5514 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5515 gfc_expr *e, bool check_types)
5517 gfc_symbol *declared;
5518 gfc_ref *ref;
5520 declared = NULL;
5521 if (class_ref)
5522 *class_ref = NULL;
5523 if (new_ref)
5524 *new_ref = gfc_copy_ref (e->ref);
5526 for (ref = e->ref; ref; ref = ref->next)
5528 if (ref->type != REF_COMPONENT)
5529 continue;
5531 if ((ref->u.c.component->ts.type == BT_CLASS
5532 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5533 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5535 declared = ref->u.c.component->ts.u.derived;
5536 if (class_ref)
5537 *class_ref = ref;
5541 if (declared == NULL)
5542 declared = e->symtree->n.sym->ts.u.derived;
5544 return declared;
5548 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5549 which of the specific bindings (if any) matches the arglist and transform
5550 the expression into a call of that binding. */
5552 static bool
5553 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5555 gfc_typebound_proc* genproc;
5556 const char* genname;
5557 gfc_symtree *st;
5558 gfc_symbol *derived;
5560 gcc_assert (e->expr_type == EXPR_COMPCALL);
5561 genname = e->value.compcall.name;
5562 genproc = e->value.compcall.tbp;
5564 if (!genproc->is_generic)
5565 return true;
5567 /* Try the bindings on this type and in the inheritance hierarchy. */
5568 for (; genproc; genproc = genproc->overridden)
5570 gfc_tbp_generic* g;
5572 gcc_assert (genproc->is_generic);
5573 for (g = genproc->u.generic; g; g = g->next)
5575 gfc_symbol* target;
5576 gfc_actual_arglist* args;
5577 bool matches;
5579 gcc_assert (g->specific);
5581 if (g->specific->error)
5582 continue;
5584 target = g->specific->u.specific->n.sym;
5586 /* Get the right arglist by handling PASS/NOPASS. */
5587 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5588 if (!g->specific->nopass)
5590 gfc_expr* po;
5591 po = extract_compcall_passed_object (e);
5592 if (!po)
5594 gfc_free_actual_arglist (args);
5595 return false;
5598 gcc_assert (g->specific->pass_arg_num > 0);
5599 gcc_assert (!g->specific->error);
5600 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5601 g->specific->pass_arg);
5603 resolve_actual_arglist (args, target->attr.proc,
5604 is_external_proc (target)
5605 && gfc_sym_get_dummy_args (target) == NULL);
5607 /* Check if this arglist matches the formal. */
5608 matches = gfc_arglist_matches_symbol (&args, target);
5610 /* Clean up and break out of the loop if we've found it. */
5611 gfc_free_actual_arglist (args);
5612 if (matches)
5614 e->value.compcall.tbp = g->specific;
5615 genname = g->specific_st->name;
5616 /* Pass along the name for CLASS methods, where the vtab
5617 procedure pointer component has to be referenced. */
5618 if (name)
5619 *name = genname;
5620 goto success;
5625 /* Nothing matching found! */
5626 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5627 " '%s' at %L", genname, &e->where);
5628 return false;
5630 success:
5631 /* Make sure that we have the right specific instance for the name. */
5632 derived = get_declared_from_expr (NULL, NULL, e, true);
5634 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5635 if (st)
5636 e->value.compcall.tbp = st->n.tb;
5638 return true;
5642 /* Resolve a call to a type-bound subroutine. */
5644 static bool
5645 resolve_typebound_call (gfc_code* c, const char **name)
5647 gfc_actual_arglist* newactual;
5648 gfc_symtree* target;
5650 /* Check that's really a SUBROUTINE. */
5651 if (!c->expr1->value.compcall.tbp->subroutine)
5653 gfc_error ("'%s' at %L should be a SUBROUTINE",
5654 c->expr1->value.compcall.name, &c->loc);
5655 return false;
5658 if (!check_typebound_baseobject (c->expr1))
5659 return false;
5661 /* Pass along the name for CLASS methods, where the vtab
5662 procedure pointer component has to be referenced. */
5663 if (name)
5664 *name = c->expr1->value.compcall.name;
5666 if (!resolve_typebound_generic_call (c->expr1, name))
5667 return false;
5669 /* Transform into an ordinary EXEC_CALL for now. */
5671 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5672 return false;
5674 c->ext.actual = newactual;
5675 c->symtree = target;
5676 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5678 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5680 gfc_free_expr (c->expr1);
5681 c->expr1 = gfc_get_expr ();
5682 c->expr1->expr_type = EXPR_FUNCTION;
5683 c->expr1->symtree = target;
5684 c->expr1->where = c->loc;
5686 return resolve_call (c);
5690 /* Resolve a component-call expression. */
5691 static bool
5692 resolve_compcall (gfc_expr* e, const char **name)
5694 gfc_actual_arglist* newactual;
5695 gfc_symtree* target;
5697 /* Check that's really a FUNCTION. */
5698 if (!e->value.compcall.tbp->function)
5700 gfc_error ("'%s' at %L should be a FUNCTION",
5701 e->value.compcall.name, &e->where);
5702 return false;
5705 /* These must not be assign-calls! */
5706 gcc_assert (!e->value.compcall.assign);
5708 if (!check_typebound_baseobject (e))
5709 return false;
5711 /* Pass along the name for CLASS methods, where the vtab
5712 procedure pointer component has to be referenced. */
5713 if (name)
5714 *name = e->value.compcall.name;
5716 if (!resolve_typebound_generic_call (e, name))
5717 return false;
5718 gcc_assert (!e->value.compcall.tbp->is_generic);
5720 /* Take the rank from the function's symbol. */
5721 if (e->value.compcall.tbp->u.specific->n.sym->as)
5722 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5724 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5725 arglist to the TBP's binding target. */
5727 if (!resolve_typebound_static (e, &target, &newactual))
5728 return false;
5730 e->value.function.actual = newactual;
5731 e->value.function.name = NULL;
5732 e->value.function.esym = target->n.sym;
5733 e->value.function.isym = NULL;
5734 e->symtree = target;
5735 e->ts = target->n.sym->ts;
5736 e->expr_type = EXPR_FUNCTION;
5738 /* Resolution is not necessary if this is a class subroutine; this
5739 function only has to identify the specific proc. Resolution of
5740 the call will be done next in resolve_typebound_call. */
5741 return gfc_resolve_expr (e);
5745 static bool resolve_fl_derived (gfc_symbol *sym);
5748 /* Resolve a typebound function, or 'method'. First separate all
5749 the non-CLASS references by calling resolve_compcall directly. */
5751 static bool
5752 resolve_typebound_function (gfc_expr* e)
5754 gfc_symbol *declared;
5755 gfc_component *c;
5756 gfc_ref *new_ref;
5757 gfc_ref *class_ref;
5758 gfc_symtree *st;
5759 const char *name;
5760 gfc_typespec ts;
5761 gfc_expr *expr;
5762 bool overridable;
5764 st = e->symtree;
5766 /* Deal with typebound operators for CLASS objects. */
5767 expr = e->value.compcall.base_object;
5768 overridable = !e->value.compcall.tbp->non_overridable;
5769 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5771 /* If the base_object is not a variable, the corresponding actual
5772 argument expression must be stored in e->base_expression so
5773 that the corresponding tree temporary can be used as the base
5774 object in gfc_conv_procedure_call. */
5775 if (expr->expr_type != EXPR_VARIABLE)
5777 gfc_actual_arglist *args;
5779 for (args= e->value.function.actual; args; args = args->next)
5781 if (expr == args->expr)
5782 expr = args->expr;
5786 /* Since the typebound operators are generic, we have to ensure
5787 that any delays in resolution are corrected and that the vtab
5788 is present. */
5789 ts = expr->ts;
5790 declared = ts.u.derived;
5791 c = gfc_find_component (declared, "_vptr", true, true);
5792 if (c->ts.u.derived == NULL)
5793 c->ts.u.derived = gfc_find_derived_vtab (declared);
5795 if (!resolve_compcall (e, &name))
5796 return false;
5798 /* Use the generic name if it is there. */
5799 name = name ? name : e->value.function.esym->name;
5800 e->symtree = expr->symtree;
5801 e->ref = gfc_copy_ref (expr->ref);
5802 get_declared_from_expr (&class_ref, NULL, e, false);
5804 /* Trim away the extraneous references that emerge from nested
5805 use of interface.c (extend_expr). */
5806 if (class_ref && class_ref->next)
5808 gfc_free_ref_list (class_ref->next);
5809 class_ref->next = NULL;
5811 else if (e->ref && !class_ref)
5813 gfc_free_ref_list (e->ref);
5814 e->ref = NULL;
5817 gfc_add_vptr_component (e);
5818 gfc_add_component_ref (e, name);
5819 e->value.function.esym = NULL;
5820 if (expr->expr_type != EXPR_VARIABLE)
5821 e->base_expr = expr;
5822 return true;
5825 if (st == NULL)
5826 return resolve_compcall (e, NULL);
5828 if (!resolve_ref (e))
5829 return false;
5831 /* Get the CLASS declared type. */
5832 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5834 if (!resolve_fl_derived (declared))
5835 return false;
5837 /* Weed out cases of the ultimate component being a derived type. */
5838 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5839 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5841 gfc_free_ref_list (new_ref);
5842 return resolve_compcall (e, NULL);
5845 c = gfc_find_component (declared, "_data", true, true);
5846 declared = c->ts.u.derived;
5848 /* Treat the call as if it is a typebound procedure, in order to roll
5849 out the correct name for the specific function. */
5850 if (!resolve_compcall (e, &name))
5852 gfc_free_ref_list (new_ref);
5853 return false;
5855 ts = e->ts;
5857 if (overridable)
5859 /* Convert the expression to a procedure pointer component call. */
5860 e->value.function.esym = NULL;
5861 e->symtree = st;
5863 if (new_ref)
5864 e->ref = new_ref;
5866 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5867 gfc_add_vptr_component (e);
5868 gfc_add_component_ref (e, name);
5870 /* Recover the typespec for the expression. This is really only
5871 necessary for generic procedures, where the additional call
5872 to gfc_add_component_ref seems to throw the collection of the
5873 correct typespec. */
5874 e->ts = ts;
5876 else if (new_ref)
5877 gfc_free_ref_list (new_ref);
5879 return true;
5882 /* Resolve a typebound subroutine, or 'method'. First separate all
5883 the non-CLASS references by calling resolve_typebound_call
5884 directly. */
5886 static bool
5887 resolve_typebound_subroutine (gfc_code *code)
5889 gfc_symbol *declared;
5890 gfc_component *c;
5891 gfc_ref *new_ref;
5892 gfc_ref *class_ref;
5893 gfc_symtree *st;
5894 const char *name;
5895 gfc_typespec ts;
5896 gfc_expr *expr;
5897 bool overridable;
5899 st = code->expr1->symtree;
5901 /* Deal with typebound operators for CLASS objects. */
5902 expr = code->expr1->value.compcall.base_object;
5903 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5904 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5906 /* If the base_object is not a variable, the corresponding actual
5907 argument expression must be stored in e->base_expression so
5908 that the corresponding tree temporary can be used as the base
5909 object in gfc_conv_procedure_call. */
5910 if (expr->expr_type != EXPR_VARIABLE)
5912 gfc_actual_arglist *args;
5914 args= code->expr1->value.function.actual;
5915 for (; args; args = args->next)
5916 if (expr == args->expr)
5917 expr = args->expr;
5920 /* Since the typebound operators are generic, we have to ensure
5921 that any delays in resolution are corrected and that the vtab
5922 is present. */
5923 declared = expr->ts.u.derived;
5924 c = gfc_find_component (declared, "_vptr", true, true);
5925 if (c->ts.u.derived == NULL)
5926 c->ts.u.derived = gfc_find_derived_vtab (declared);
5928 if (!resolve_typebound_call (code, &name))
5929 return false;
5931 /* Use the generic name if it is there. */
5932 name = name ? name : code->expr1->value.function.esym->name;
5933 code->expr1->symtree = expr->symtree;
5934 code->expr1->ref = gfc_copy_ref (expr->ref);
5936 /* Trim away the extraneous references that emerge from nested
5937 use of interface.c (extend_expr). */
5938 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5939 if (class_ref && class_ref->next)
5941 gfc_free_ref_list (class_ref->next);
5942 class_ref->next = NULL;
5944 else if (code->expr1->ref && !class_ref)
5946 gfc_free_ref_list (code->expr1->ref);
5947 code->expr1->ref = NULL;
5950 /* Now use the procedure in the vtable. */
5951 gfc_add_vptr_component (code->expr1);
5952 gfc_add_component_ref (code->expr1, name);
5953 code->expr1->value.function.esym = NULL;
5954 if (expr->expr_type != EXPR_VARIABLE)
5955 code->expr1->base_expr = expr;
5956 return true;
5959 if (st == NULL)
5960 return resolve_typebound_call (code, NULL);
5962 if (!resolve_ref (code->expr1))
5963 return false;
5965 /* Get the CLASS declared type. */
5966 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5968 /* Weed out cases of the ultimate component being a derived type. */
5969 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5970 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5972 gfc_free_ref_list (new_ref);
5973 return resolve_typebound_call (code, NULL);
5976 if (!resolve_typebound_call (code, &name))
5978 gfc_free_ref_list (new_ref);
5979 return false;
5981 ts = code->expr1->ts;
5983 if (overridable)
5985 /* Convert the expression to a procedure pointer component call. */
5986 code->expr1->value.function.esym = NULL;
5987 code->expr1->symtree = st;
5989 if (new_ref)
5990 code->expr1->ref = new_ref;
5992 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5993 gfc_add_vptr_component (code->expr1);
5994 gfc_add_component_ref (code->expr1, name);
5996 /* Recover the typespec for the expression. This is really only
5997 necessary for generic procedures, where the additional call
5998 to gfc_add_component_ref seems to throw the collection of the
5999 correct typespec. */
6000 code->expr1->ts = ts;
6002 else if (new_ref)
6003 gfc_free_ref_list (new_ref);
6005 return true;
6009 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6011 static bool
6012 resolve_ppc_call (gfc_code* c)
6014 gfc_component *comp;
6016 comp = gfc_get_proc_ptr_comp (c->expr1);
6017 gcc_assert (comp != NULL);
6019 c->resolved_sym = c->expr1->symtree->n.sym;
6020 c->expr1->expr_type = EXPR_VARIABLE;
6022 if (!comp->attr.subroutine)
6023 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6025 if (!resolve_ref (c->expr1))
6026 return false;
6028 if (!update_ppc_arglist (c->expr1))
6029 return false;
6031 c->ext.actual = c->expr1->value.compcall.actual;
6033 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6034 !(comp->ts.interface
6035 && comp->ts.interface->formal)))
6036 return false;
6038 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6040 return true;
6044 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6046 static bool
6047 resolve_expr_ppc (gfc_expr* e)
6049 gfc_component *comp;
6051 comp = gfc_get_proc_ptr_comp (e);
6052 gcc_assert (comp != NULL);
6054 /* Convert to EXPR_FUNCTION. */
6055 e->expr_type = EXPR_FUNCTION;
6056 e->value.function.isym = NULL;
6057 e->value.function.actual = e->value.compcall.actual;
6058 e->ts = comp->ts;
6059 if (comp->as != NULL)
6060 e->rank = comp->as->rank;
6062 if (!comp->attr.function)
6063 gfc_add_function (&comp->attr, comp->name, &e->where);
6065 if (!resolve_ref (e))
6066 return false;
6068 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6069 !(comp->ts.interface
6070 && comp->ts.interface->formal)))
6071 return false;
6073 if (!update_ppc_arglist (e))
6074 return false;
6076 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6078 return true;
6082 static bool
6083 gfc_is_expandable_expr (gfc_expr *e)
6085 gfc_constructor *con;
6087 if (e->expr_type == EXPR_ARRAY)
6089 /* Traverse the constructor looking for variables that are flavor
6090 parameter. Parameters must be expanded since they are fully used at
6091 compile time. */
6092 con = gfc_constructor_first (e->value.constructor);
6093 for (; con; con = gfc_constructor_next (con))
6095 if (con->expr->expr_type == EXPR_VARIABLE
6096 && con->expr->symtree
6097 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6098 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6099 return true;
6100 if (con->expr->expr_type == EXPR_ARRAY
6101 && gfc_is_expandable_expr (con->expr))
6102 return true;
6106 return false;
6109 /* Resolve an expression. That is, make sure that types of operands agree
6110 with their operators, intrinsic operators are converted to function calls
6111 for overloaded types and unresolved function references are resolved. */
6113 bool
6114 gfc_resolve_expr (gfc_expr *e)
6116 bool t;
6117 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6119 if (e == NULL)
6120 return true;
6122 /* inquiry_argument only applies to variables. */
6123 inquiry_save = inquiry_argument;
6124 actual_arg_save = actual_arg;
6125 first_actual_arg_save = first_actual_arg;
6127 if (e->expr_type != EXPR_VARIABLE)
6129 inquiry_argument = false;
6130 actual_arg = false;
6131 first_actual_arg = false;
6134 switch (e->expr_type)
6136 case EXPR_OP:
6137 t = resolve_operator (e);
6138 break;
6140 case EXPR_FUNCTION:
6141 case EXPR_VARIABLE:
6143 if (check_host_association (e))
6144 t = resolve_function (e);
6145 else
6146 t = resolve_variable (e);
6148 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6149 && e->ref->type != REF_SUBSTRING)
6150 gfc_resolve_substring_charlen (e);
6152 break;
6154 case EXPR_COMPCALL:
6155 t = resolve_typebound_function (e);
6156 break;
6158 case EXPR_SUBSTRING:
6159 t = resolve_ref (e);
6160 break;
6162 case EXPR_CONSTANT:
6163 case EXPR_NULL:
6164 t = true;
6165 break;
6167 case EXPR_PPC:
6168 t = resolve_expr_ppc (e);
6169 break;
6171 case EXPR_ARRAY:
6172 t = false;
6173 if (!resolve_ref (e))
6174 break;
6176 t = gfc_resolve_array_constructor (e);
6177 /* Also try to expand a constructor. */
6178 if (t)
6180 expression_rank (e);
6181 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6182 gfc_expand_constructor (e, false);
6185 /* This provides the opportunity for the length of constructors with
6186 character valued function elements to propagate the string length
6187 to the expression. */
6188 if (t && e->ts.type == BT_CHARACTER)
6190 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6191 here rather then add a duplicate test for it above. */
6192 gfc_expand_constructor (e, false);
6193 t = gfc_resolve_character_array_constructor (e);
6196 break;
6198 case EXPR_STRUCTURE:
6199 t = resolve_ref (e);
6200 if (!t)
6201 break;
6203 t = resolve_structure_cons (e, 0);
6204 if (!t)
6205 break;
6207 t = gfc_simplify_expr (e, 0);
6208 break;
6210 default:
6211 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6214 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6215 fixup_charlen (e);
6217 inquiry_argument = inquiry_save;
6218 actual_arg = actual_arg_save;
6219 first_actual_arg = first_actual_arg_save;
6221 return t;
6225 /* Resolve an expression from an iterator. They must be scalar and have
6226 INTEGER or (optionally) REAL type. */
6228 static bool
6229 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6230 const char *name_msgid)
6232 if (!gfc_resolve_expr (expr))
6233 return false;
6235 if (expr->rank != 0)
6237 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6238 return false;
6241 if (expr->ts.type != BT_INTEGER)
6243 if (expr->ts.type == BT_REAL)
6245 if (real_ok)
6246 return gfc_notify_std (GFC_STD_F95_DEL,
6247 "%s at %L must be integer",
6248 _(name_msgid), &expr->where);
6249 else
6251 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6252 &expr->where);
6253 return false;
6256 else
6258 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6259 return false;
6262 return true;
6266 /* Resolve the expressions in an iterator structure. If REAL_OK is
6267 false allow only INTEGER type iterators, otherwise allow REAL types.
6268 Set own_scope to true for ac-implied-do and data-implied-do as those
6269 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6271 bool
6272 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6274 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6275 return false;
6277 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6278 _("iterator variable")))
6279 return false;
6281 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6282 "Start expression in DO loop"))
6283 return false;
6285 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6286 "End expression in DO loop"))
6287 return false;
6289 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6290 "Step expression in DO loop"))
6291 return false;
6293 if (iter->step->expr_type == EXPR_CONSTANT)
6295 if ((iter->step->ts.type == BT_INTEGER
6296 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6297 || (iter->step->ts.type == BT_REAL
6298 && mpfr_sgn (iter->step->value.real) == 0))
6300 gfc_error ("Step expression in DO loop at %L cannot be zero",
6301 &iter->step->where);
6302 return false;
6306 /* Convert start, end, and step to the same type as var. */
6307 if (iter->start->ts.kind != iter->var->ts.kind
6308 || iter->start->ts.type != iter->var->ts.type)
6309 gfc_convert_type (iter->start, &iter->var->ts, 2);
6311 if (iter->end->ts.kind != iter->var->ts.kind
6312 || iter->end->ts.type != iter->var->ts.type)
6313 gfc_convert_type (iter->end, &iter->var->ts, 2);
6315 if (iter->step->ts.kind != iter->var->ts.kind
6316 || iter->step->ts.type != iter->var->ts.type)
6317 gfc_convert_type (iter->step, &iter->var->ts, 2);
6319 if (iter->start->expr_type == EXPR_CONSTANT
6320 && iter->end->expr_type == EXPR_CONSTANT
6321 && iter->step->expr_type == EXPR_CONSTANT)
6323 int sgn, cmp;
6324 if (iter->start->ts.type == BT_INTEGER)
6326 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6327 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6329 else
6331 sgn = mpfr_sgn (iter->step->value.real);
6332 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6334 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6335 gfc_warning ("DO loop at %L will be executed zero times"
6336 " (use -Wno-zerotrip to suppress)",
6337 &iter->step->where);
6340 return true;
6344 /* Traversal function for find_forall_index. f == 2 signals that
6345 that variable itself is not to be checked - only the references. */
6347 static bool
6348 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6350 if (expr->expr_type != EXPR_VARIABLE)
6351 return false;
6353 /* A scalar assignment */
6354 if (!expr->ref || *f == 1)
6356 if (expr->symtree->n.sym == sym)
6357 return true;
6358 else
6359 return false;
6362 if (*f == 2)
6363 *f = 1;
6364 return false;
6368 /* Check whether the FORALL index appears in the expression or not.
6369 Returns true if SYM is found in EXPR. */
6371 bool
6372 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6374 if (gfc_traverse_expr (expr, sym, forall_index, f))
6375 return true;
6376 else
6377 return false;
6381 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6382 to be a scalar INTEGER variable. The subscripts and stride are scalar
6383 INTEGERs, and if stride is a constant it must be nonzero.
6384 Furthermore "A subscript or stride in a forall-triplet-spec shall
6385 not contain a reference to any index-name in the
6386 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6388 static void
6389 resolve_forall_iterators (gfc_forall_iterator *it)
6391 gfc_forall_iterator *iter, *iter2;
6393 for (iter = it; iter; iter = iter->next)
6395 if (gfc_resolve_expr (iter->var)
6396 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6397 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6398 &iter->var->where);
6400 if (gfc_resolve_expr (iter->start)
6401 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6402 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6403 &iter->start->where);
6404 if (iter->var->ts.kind != iter->start->ts.kind)
6405 gfc_convert_type (iter->start, &iter->var->ts, 1);
6407 if (gfc_resolve_expr (iter->end)
6408 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6409 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6410 &iter->end->where);
6411 if (iter->var->ts.kind != iter->end->ts.kind)
6412 gfc_convert_type (iter->end, &iter->var->ts, 1);
6414 if (gfc_resolve_expr (iter->stride))
6416 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6417 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6418 &iter->stride->where, "INTEGER");
6420 if (iter->stride->expr_type == EXPR_CONSTANT
6421 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6422 gfc_error ("FORALL stride expression at %L cannot be zero",
6423 &iter->stride->where);
6425 if (iter->var->ts.kind != iter->stride->ts.kind)
6426 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6429 for (iter = it; iter; iter = iter->next)
6430 for (iter2 = iter; iter2; iter2 = iter2->next)
6432 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6433 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6434 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6435 gfc_error ("FORALL index '%s' may not appear in triplet "
6436 "specification at %L", iter->var->symtree->name,
6437 &iter2->start->where);
6442 /* Given a pointer to a symbol that is a derived type, see if it's
6443 inaccessible, i.e. if it's defined in another module and the components are
6444 PRIVATE. The search is recursive if necessary. Returns zero if no
6445 inaccessible components are found, nonzero otherwise. */
6447 static int
6448 derived_inaccessible (gfc_symbol *sym)
6450 gfc_component *c;
6452 if (sym->attr.use_assoc && sym->attr.private_comp)
6453 return 1;
6455 for (c = sym->components; c; c = c->next)
6457 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6458 return 1;
6461 return 0;
6465 /* Resolve the argument of a deallocate expression. The expression must be
6466 a pointer or a full array. */
6468 static bool
6469 resolve_deallocate_expr (gfc_expr *e)
6471 symbol_attribute attr;
6472 int allocatable, pointer;
6473 gfc_ref *ref;
6474 gfc_symbol *sym;
6475 gfc_component *c;
6476 bool unlimited;
6478 if (!gfc_resolve_expr (e))
6479 return false;
6481 if (e->expr_type != EXPR_VARIABLE)
6482 goto bad;
6484 sym = e->symtree->n.sym;
6485 unlimited = UNLIMITED_POLY(sym);
6487 if (sym->ts.type == BT_CLASS)
6489 allocatable = CLASS_DATA (sym)->attr.allocatable;
6490 pointer = CLASS_DATA (sym)->attr.class_pointer;
6492 else
6494 allocatable = sym->attr.allocatable;
6495 pointer = sym->attr.pointer;
6497 for (ref = e->ref; ref; ref = ref->next)
6499 switch (ref->type)
6501 case REF_ARRAY:
6502 if (ref->u.ar.type != AR_FULL
6503 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6504 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6505 allocatable = 0;
6506 break;
6508 case REF_COMPONENT:
6509 c = ref->u.c.component;
6510 if (c->ts.type == BT_CLASS)
6512 allocatable = CLASS_DATA (c)->attr.allocatable;
6513 pointer = CLASS_DATA (c)->attr.class_pointer;
6515 else
6517 allocatable = c->attr.allocatable;
6518 pointer = c->attr.pointer;
6520 break;
6522 case REF_SUBSTRING:
6523 allocatable = 0;
6524 break;
6528 attr = gfc_expr_attr (e);
6530 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6532 bad:
6533 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6534 &e->where);
6535 return false;
6538 /* F2008, C644. */
6539 if (gfc_is_coindexed (e))
6541 gfc_error ("Coindexed allocatable object at %L", &e->where);
6542 return false;
6545 if (pointer
6546 && !gfc_check_vardef_context (e, true, true, false,
6547 _("DEALLOCATE object")))
6548 return false;
6549 if (!gfc_check_vardef_context (e, false, true, false,
6550 _("DEALLOCATE object")))
6551 return false;
6553 return true;
6557 /* Returns true if the expression e contains a reference to the symbol sym. */
6558 static bool
6559 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6561 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6562 return true;
6564 return false;
6567 bool
6568 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6570 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6574 /* Given the expression node e for an allocatable/pointer of derived type to be
6575 allocated, get the expression node to be initialized afterwards (needed for
6576 derived types with default initializers, and derived types with allocatable
6577 components that need nullification.) */
6579 gfc_expr *
6580 gfc_expr_to_initialize (gfc_expr *e)
6582 gfc_expr *result;
6583 gfc_ref *ref;
6584 int i;
6586 result = gfc_copy_expr (e);
6588 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6589 for (ref = result->ref; ref; ref = ref->next)
6590 if (ref->type == REF_ARRAY && ref->next == NULL)
6592 ref->u.ar.type = AR_FULL;
6594 for (i = 0; i < ref->u.ar.dimen; i++)
6595 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6597 break;
6600 gfc_free_shape (&result->shape, result->rank);
6602 /* Recalculate rank, shape, etc. */
6603 gfc_resolve_expr (result);
6604 return result;
6608 /* If the last ref of an expression is an array ref, return a copy of the
6609 expression with that one removed. Otherwise, a copy of the original
6610 expression. This is used for allocate-expressions and pointer assignment
6611 LHS, where there may be an array specification that needs to be stripped
6612 off when using gfc_check_vardef_context. */
6614 static gfc_expr*
6615 remove_last_array_ref (gfc_expr* e)
6617 gfc_expr* e2;
6618 gfc_ref** r;
6620 e2 = gfc_copy_expr (e);
6621 for (r = &e2->ref; *r; r = &(*r)->next)
6622 if ((*r)->type == REF_ARRAY && !(*r)->next)
6624 gfc_free_ref_list (*r);
6625 *r = NULL;
6626 break;
6629 return e2;
6633 /* Used in resolve_allocate_expr to check that a allocation-object and
6634 a source-expr are conformable. This does not catch all possible
6635 cases; in particular a runtime checking is needed. */
6637 static bool
6638 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6640 gfc_ref *tail;
6641 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6643 /* First compare rank. */
6644 if ((tail && e1->rank != tail->u.ar.as->rank)
6645 || (!tail && e1->rank != e2->rank))
6647 gfc_error ("Source-expr at %L must be scalar or have the "
6648 "same rank as the allocate-object at %L",
6649 &e1->where, &e2->where);
6650 return false;
6653 if (e1->shape)
6655 int i;
6656 mpz_t s;
6658 mpz_init (s);
6660 for (i = 0; i < e1->rank; i++)
6662 if (tail->u.ar.start[i] == NULL)
6663 break;
6665 if (tail->u.ar.end[i])
6667 mpz_set (s, tail->u.ar.end[i]->value.integer);
6668 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6669 mpz_add_ui (s, s, 1);
6671 else
6673 mpz_set (s, tail->u.ar.start[i]->value.integer);
6676 if (mpz_cmp (e1->shape[i], s) != 0)
6678 gfc_error ("Source-expr at %L and allocate-object at %L must "
6679 "have the same shape", &e1->where, &e2->where);
6680 mpz_clear (s);
6681 return false;
6685 mpz_clear (s);
6688 return true;
6692 /* Resolve the expression in an ALLOCATE statement, doing the additional
6693 checks to see whether the expression is OK or not. The expression must
6694 have a trailing array reference that gives the size of the array. */
6696 static bool
6697 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6699 int i, pointer, allocatable, dimension, is_abstract;
6700 int codimension;
6701 bool coindexed;
6702 bool unlimited;
6703 symbol_attribute attr;
6704 gfc_ref *ref, *ref2;
6705 gfc_expr *e2;
6706 gfc_array_ref *ar;
6707 gfc_symbol *sym = NULL;
6708 gfc_alloc *a;
6709 gfc_component *c;
6710 bool t;
6712 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6713 checking of coarrays. */
6714 for (ref = e->ref; ref; ref = ref->next)
6715 if (ref->next == NULL)
6716 break;
6718 if (ref && ref->type == REF_ARRAY)
6719 ref->u.ar.in_allocate = true;
6721 if (!gfc_resolve_expr (e))
6722 goto failure;
6724 /* Make sure the expression is allocatable or a pointer. If it is
6725 pointer, the next-to-last reference must be a pointer. */
6727 ref2 = NULL;
6728 if (e->symtree)
6729 sym = e->symtree->n.sym;
6731 /* Check whether ultimate component is abstract and CLASS. */
6732 is_abstract = 0;
6734 /* Is the allocate-object unlimited polymorphic? */
6735 unlimited = UNLIMITED_POLY(e);
6737 if (e->expr_type != EXPR_VARIABLE)
6739 allocatable = 0;
6740 attr = gfc_expr_attr (e);
6741 pointer = attr.pointer;
6742 dimension = attr.dimension;
6743 codimension = attr.codimension;
6745 else
6747 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6749 allocatable = CLASS_DATA (sym)->attr.allocatable;
6750 pointer = CLASS_DATA (sym)->attr.class_pointer;
6751 dimension = CLASS_DATA (sym)->attr.dimension;
6752 codimension = CLASS_DATA (sym)->attr.codimension;
6753 is_abstract = CLASS_DATA (sym)->attr.abstract;
6755 else
6757 allocatable = sym->attr.allocatable;
6758 pointer = sym->attr.pointer;
6759 dimension = sym->attr.dimension;
6760 codimension = sym->attr.codimension;
6763 coindexed = false;
6765 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6767 switch (ref->type)
6769 case REF_ARRAY:
6770 if (ref->u.ar.codimen > 0)
6772 int n;
6773 for (n = ref->u.ar.dimen;
6774 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6775 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6777 coindexed = true;
6778 break;
6782 if (ref->next != NULL)
6783 pointer = 0;
6784 break;
6786 case REF_COMPONENT:
6787 /* F2008, C644. */
6788 if (coindexed)
6790 gfc_error ("Coindexed allocatable object at %L",
6791 &e->where);
6792 goto failure;
6795 c = ref->u.c.component;
6796 if (c->ts.type == BT_CLASS)
6798 allocatable = CLASS_DATA (c)->attr.allocatable;
6799 pointer = CLASS_DATA (c)->attr.class_pointer;
6800 dimension = CLASS_DATA (c)->attr.dimension;
6801 codimension = CLASS_DATA (c)->attr.codimension;
6802 is_abstract = CLASS_DATA (c)->attr.abstract;
6804 else
6806 allocatable = c->attr.allocatable;
6807 pointer = c->attr.pointer;
6808 dimension = c->attr.dimension;
6809 codimension = c->attr.codimension;
6810 is_abstract = c->attr.abstract;
6812 break;
6814 case REF_SUBSTRING:
6815 allocatable = 0;
6816 pointer = 0;
6817 break;
6822 /* Check for F08:C628. */
6823 if (allocatable == 0 && pointer == 0 && !unlimited)
6825 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6826 &e->where);
6827 goto failure;
6830 /* Some checks for the SOURCE tag. */
6831 if (code->expr3)
6833 /* Check F03:C631. */
6834 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6836 gfc_error ("Type of entity at %L is type incompatible with "
6837 "source-expr at %L", &e->where, &code->expr3->where);
6838 goto failure;
6841 /* Check F03:C632 and restriction following Note 6.18. */
6842 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
6843 goto failure;
6845 /* Check F03:C633. */
6846 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6848 gfc_error ("The allocate-object at %L and the source-expr at %L "
6849 "shall have the same kind type parameter",
6850 &e->where, &code->expr3->where);
6851 goto failure;
6854 /* Check F2008, C642. */
6855 if (code->expr3->ts.type == BT_DERIVED
6856 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6857 || (code->expr3->ts.u.derived->from_intmod
6858 == INTMOD_ISO_FORTRAN_ENV
6859 && code->expr3->ts.u.derived->intmod_sym_id
6860 == ISOFORTRAN_LOCK_TYPE)))
6862 gfc_error ("The source-expr at %L shall neither be of type "
6863 "LOCK_TYPE nor have a LOCK_TYPE component if "
6864 "allocate-object at %L is a coarray",
6865 &code->expr3->where, &e->where);
6866 goto failure;
6870 /* Check F08:C629. */
6871 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6872 && !code->expr3)
6874 gcc_assert (e->ts.type == BT_CLASS);
6875 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6876 "type-spec or source-expr", sym->name, &e->where);
6877 goto failure;
6880 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6882 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6883 code->ext.alloc.ts.u.cl->length);
6884 if (cmp == 1 || cmp == -1 || cmp == -3)
6886 gfc_error ("Allocating %s at %L with type-spec requires the same "
6887 "character-length parameter as in the declaration",
6888 sym->name, &e->where);
6889 goto failure;
6893 /* In the variable definition context checks, gfc_expr_attr is used
6894 on the expression. This is fooled by the array specification
6895 present in e, thus we have to eliminate that one temporarily. */
6896 e2 = remove_last_array_ref (e);
6897 t = true;
6898 if (t && pointer)
6899 t = gfc_check_vardef_context (e2, true, true, false,
6900 _("ALLOCATE object"));
6901 if (t)
6902 t = gfc_check_vardef_context (e2, false, true, false,
6903 _("ALLOCATE object"));
6904 gfc_free_expr (e2);
6905 if (!t)
6906 goto failure;
6908 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6909 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6911 /* For class arrays, the initialization with SOURCE is done
6912 using _copy and trans_call. It is convenient to exploit that
6913 when the allocated type is different from the declared type but
6914 no SOURCE exists by setting expr3. */
6915 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6917 else if (!code->expr3)
6919 /* Set up default initializer if needed. */
6920 gfc_typespec ts;
6921 gfc_expr *init_e;
6923 if (code->ext.alloc.ts.type == BT_DERIVED)
6924 ts = code->ext.alloc.ts;
6925 else
6926 ts = e->ts;
6928 if (ts.type == BT_CLASS)
6929 ts = ts.u.derived->components->ts;
6931 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6933 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6934 init_st->loc = code->loc;
6935 init_st->expr1 = gfc_expr_to_initialize (e);
6936 init_st->expr2 = init_e;
6937 init_st->next = code->next;
6938 code->next = init_st;
6941 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6943 /* Default initialization via MOLD (non-polymorphic). */
6944 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6945 gfc_resolve_expr (rhs);
6946 gfc_free_expr (code->expr3);
6947 code->expr3 = rhs;
6950 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6952 /* Make sure the vtab symbol is present when
6953 the module variables are generated. */
6954 gfc_typespec ts = e->ts;
6955 if (code->expr3)
6956 ts = code->expr3->ts;
6957 else if (code->ext.alloc.ts.type == BT_DERIVED)
6958 ts = code->ext.alloc.ts;
6960 gfc_find_derived_vtab (ts.u.derived);
6962 if (dimension)
6963 e = gfc_expr_to_initialize (e);
6965 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6967 /* Again, make sure the vtab symbol is present when
6968 the module variables are generated. */
6969 gfc_typespec *ts = NULL;
6970 if (code->expr3)
6971 ts = &code->expr3->ts;
6972 else
6973 ts = &code->ext.alloc.ts;
6975 gcc_assert (ts);
6977 gfc_find_vtab (ts);
6979 if (dimension)
6980 e = gfc_expr_to_initialize (e);
6983 if (dimension == 0 && codimension == 0)
6984 goto success;
6986 /* Make sure the last reference node is an array specification. */
6988 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6989 || (dimension && ref2->u.ar.dimen == 0))
6991 gfc_error ("Array specification required in ALLOCATE statement "
6992 "at %L", &e->where);
6993 goto failure;
6996 /* Make sure that the array section reference makes sense in the
6997 context of an ALLOCATE specification. */
6999 ar = &ref2->u.ar;
7001 if (codimension)
7002 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7003 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7005 gfc_error ("Coarray specification required in ALLOCATE statement "
7006 "at %L", &e->where);
7007 goto failure;
7010 for (i = 0; i < ar->dimen; i++)
7012 if (ref2->u.ar.type == AR_ELEMENT)
7013 goto check_symbols;
7015 switch (ar->dimen_type[i])
7017 case DIMEN_ELEMENT:
7018 break;
7020 case DIMEN_RANGE:
7021 if (ar->start[i] != NULL
7022 && ar->end[i] != NULL
7023 && ar->stride[i] == NULL)
7024 break;
7026 /* Fall Through... */
7028 case DIMEN_UNKNOWN:
7029 case DIMEN_VECTOR:
7030 case DIMEN_STAR:
7031 case DIMEN_THIS_IMAGE:
7032 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7033 &e->where);
7034 goto failure;
7037 check_symbols:
7038 for (a = code->ext.alloc.list; a; a = a->next)
7040 sym = a->expr->symtree->n.sym;
7042 /* TODO - check derived type components. */
7043 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7044 continue;
7046 if ((ar->start[i] != NULL
7047 && gfc_find_sym_in_expr (sym, ar->start[i]))
7048 || (ar->end[i] != NULL
7049 && gfc_find_sym_in_expr (sym, ar->end[i])))
7051 gfc_error ("'%s' must not appear in the array specification at "
7052 "%L in the same ALLOCATE statement where it is "
7053 "itself allocated", sym->name, &ar->where);
7054 goto failure;
7059 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7061 if (ar->dimen_type[i] == DIMEN_ELEMENT
7062 || ar->dimen_type[i] == DIMEN_RANGE)
7064 if (i == (ar->dimen + ar->codimen - 1))
7066 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7067 "statement at %L", &e->where);
7068 goto failure;
7070 continue;
7073 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7074 && ar->stride[i] == NULL)
7075 break;
7077 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7078 &e->where);
7079 goto failure;
7082 success:
7083 return true;
7085 failure:
7086 return false;
7089 static void
7090 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7092 gfc_expr *stat, *errmsg, *pe, *qe;
7093 gfc_alloc *a, *p, *q;
7095 stat = code->expr1;
7096 errmsg = code->expr2;
7098 /* Check the stat variable. */
7099 if (stat)
7101 gfc_check_vardef_context (stat, false, false, false,
7102 _("STAT variable"));
7104 if ((stat->ts.type != BT_INTEGER
7105 && !(stat->ref && (stat->ref->type == REF_ARRAY
7106 || stat->ref->type == REF_COMPONENT)))
7107 || stat->rank > 0)
7108 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7109 "variable", &stat->where);
7111 for (p = code->ext.alloc.list; p; p = p->next)
7112 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7114 gfc_ref *ref1, *ref2;
7115 bool found = true;
7117 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7118 ref1 = ref1->next, ref2 = ref2->next)
7120 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7121 continue;
7122 if (ref1->u.c.component->name != ref2->u.c.component->name)
7124 found = false;
7125 break;
7129 if (found)
7131 gfc_error ("Stat-variable at %L shall not be %sd within "
7132 "the same %s statement", &stat->where, fcn, fcn);
7133 break;
7138 /* Check the errmsg variable. */
7139 if (errmsg)
7141 if (!stat)
7142 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7143 &errmsg->where);
7145 gfc_check_vardef_context (errmsg, false, false, false,
7146 _("ERRMSG variable"));
7148 if ((errmsg->ts.type != BT_CHARACTER
7149 && !(errmsg->ref
7150 && (errmsg->ref->type == REF_ARRAY
7151 || errmsg->ref->type == REF_COMPONENT)))
7152 || errmsg->rank > 0 )
7153 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7154 "variable", &errmsg->where);
7156 for (p = code->ext.alloc.list; p; p = p->next)
7157 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7159 gfc_ref *ref1, *ref2;
7160 bool found = true;
7162 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7163 ref1 = ref1->next, ref2 = ref2->next)
7165 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7166 continue;
7167 if (ref1->u.c.component->name != ref2->u.c.component->name)
7169 found = false;
7170 break;
7174 if (found)
7176 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7177 "the same %s statement", &errmsg->where, fcn, fcn);
7178 break;
7183 /* Check that an allocate-object appears only once in the statement. */
7185 for (p = code->ext.alloc.list; p; p = p->next)
7187 pe = p->expr;
7188 for (q = p->next; q; q = q->next)
7190 qe = q->expr;
7191 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7193 /* This is a potential collision. */
7194 gfc_ref *pr = pe->ref;
7195 gfc_ref *qr = qe->ref;
7197 /* Follow the references until
7198 a) They start to differ, in which case there is no error;
7199 you can deallocate a%b and a%c in a single statement
7200 b) Both of them stop, which is an error
7201 c) One of them stops, which is also an error. */
7202 while (1)
7204 if (pr == NULL && qr == NULL)
7206 gfc_error ("Allocate-object at %L also appears at %L",
7207 &pe->where, &qe->where);
7208 break;
7210 else if (pr != NULL && qr == NULL)
7212 gfc_error ("Allocate-object at %L is subobject of"
7213 " object at %L", &pe->where, &qe->where);
7214 break;
7216 else if (pr == NULL && qr != NULL)
7218 gfc_error ("Allocate-object at %L is subobject of"
7219 " object at %L", &qe->where, &pe->where);
7220 break;
7222 /* Here, pr != NULL && qr != NULL */
7223 gcc_assert(pr->type == qr->type);
7224 if (pr->type == REF_ARRAY)
7226 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7227 which are legal. */
7228 gcc_assert (qr->type == REF_ARRAY);
7230 if (pr->next && qr->next)
7232 int i;
7233 gfc_array_ref *par = &(pr->u.ar);
7234 gfc_array_ref *qar = &(qr->u.ar);
7236 for (i=0; i<par->dimen; i++)
7238 if ((par->start[i] != NULL
7239 || qar->start[i] != NULL)
7240 && gfc_dep_compare_expr (par->start[i],
7241 qar->start[i]) != 0)
7242 goto break_label;
7246 else
7248 if (pr->u.c.component->name != qr->u.c.component->name)
7249 break;
7252 pr = pr->next;
7253 qr = qr->next;
7255 break_label:
7261 if (strcmp (fcn, "ALLOCATE") == 0)
7263 for (a = code->ext.alloc.list; a; a = a->next)
7264 resolve_allocate_expr (a->expr, code);
7266 else
7268 for (a = code->ext.alloc.list; a; a = a->next)
7269 resolve_deallocate_expr (a->expr);
7274 /************ SELECT CASE resolution subroutines ************/
7276 /* Callback function for our mergesort variant. Determines interval
7277 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7278 op1 > op2. Assumes we're not dealing with the default case.
7279 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7280 There are nine situations to check. */
7282 static int
7283 compare_cases (const gfc_case *op1, const gfc_case *op2)
7285 int retval;
7287 if (op1->low == NULL) /* op1 = (:L) */
7289 /* op2 = (:N), so overlap. */
7290 retval = 0;
7291 /* op2 = (M:) or (M:N), L < M */
7292 if (op2->low != NULL
7293 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7294 retval = -1;
7296 else if (op1->high == NULL) /* op1 = (K:) */
7298 /* op2 = (M:), so overlap. */
7299 retval = 0;
7300 /* op2 = (:N) or (M:N), K > N */
7301 if (op2->high != NULL
7302 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7303 retval = 1;
7305 else /* op1 = (K:L) */
7307 if (op2->low == NULL) /* op2 = (:N), K > N */
7308 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7309 ? 1 : 0;
7310 else if (op2->high == NULL) /* op2 = (M:), L < M */
7311 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7312 ? -1 : 0;
7313 else /* op2 = (M:N) */
7315 retval = 0;
7316 /* L < M */
7317 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7318 retval = -1;
7319 /* K > N */
7320 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7321 retval = 1;
7325 return retval;
7329 /* Merge-sort a double linked case list, detecting overlap in the
7330 process. LIST is the head of the double linked case list before it
7331 is sorted. Returns the head of the sorted list if we don't see any
7332 overlap, or NULL otherwise. */
7334 static gfc_case *
7335 check_case_overlap (gfc_case *list)
7337 gfc_case *p, *q, *e, *tail;
7338 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7340 /* If the passed list was empty, return immediately. */
7341 if (!list)
7342 return NULL;
7344 overlap_seen = 0;
7345 insize = 1;
7347 /* Loop unconditionally. The only exit from this loop is a return
7348 statement, when we've finished sorting the case list. */
7349 for (;;)
7351 p = list;
7352 list = NULL;
7353 tail = NULL;
7355 /* Count the number of merges we do in this pass. */
7356 nmerges = 0;
7358 /* Loop while there exists a merge to be done. */
7359 while (p)
7361 int i;
7363 /* Count this merge. */
7364 nmerges++;
7366 /* Cut the list in two pieces by stepping INSIZE places
7367 forward in the list, starting from P. */
7368 psize = 0;
7369 q = p;
7370 for (i = 0; i < insize; i++)
7372 psize++;
7373 q = q->right;
7374 if (!q)
7375 break;
7377 qsize = insize;
7379 /* Now we have two lists. Merge them! */
7380 while (psize > 0 || (qsize > 0 && q != NULL))
7382 /* See from which the next case to merge comes from. */
7383 if (psize == 0)
7385 /* P is empty so the next case must come from Q. */
7386 e = q;
7387 q = q->right;
7388 qsize--;
7390 else if (qsize == 0 || q == NULL)
7392 /* Q is empty. */
7393 e = p;
7394 p = p->right;
7395 psize--;
7397 else
7399 cmp = compare_cases (p, q);
7400 if (cmp < 0)
7402 /* The whole case range for P is less than the
7403 one for Q. */
7404 e = p;
7405 p = p->right;
7406 psize--;
7408 else if (cmp > 0)
7410 /* The whole case range for Q is greater than
7411 the case range for P. */
7412 e = q;
7413 q = q->right;
7414 qsize--;
7416 else
7418 /* The cases overlap, or they are the same
7419 element in the list. Either way, we must
7420 issue an error and get the next case from P. */
7421 /* FIXME: Sort P and Q by line number. */
7422 gfc_error ("CASE label at %L overlaps with CASE "
7423 "label at %L", &p->where, &q->where);
7424 overlap_seen = 1;
7425 e = p;
7426 p = p->right;
7427 psize--;
7431 /* Add the next element to the merged list. */
7432 if (tail)
7433 tail->right = e;
7434 else
7435 list = e;
7436 e->left = tail;
7437 tail = e;
7440 /* P has now stepped INSIZE places along, and so has Q. So
7441 they're the same. */
7442 p = q;
7444 tail->right = NULL;
7446 /* If we have done only one merge or none at all, we've
7447 finished sorting the cases. */
7448 if (nmerges <= 1)
7450 if (!overlap_seen)
7451 return list;
7452 else
7453 return NULL;
7456 /* Otherwise repeat, merging lists twice the size. */
7457 insize *= 2;
7462 /* Check to see if an expression is suitable for use in a CASE statement.
7463 Makes sure that all case expressions are scalar constants of the same
7464 type. Return false if anything is wrong. */
7466 static bool
7467 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7469 if (e == NULL) return true;
7471 if (e->ts.type != case_expr->ts.type)
7473 gfc_error ("Expression in CASE statement at %L must be of type %s",
7474 &e->where, gfc_basic_typename (case_expr->ts.type));
7475 return false;
7478 /* C805 (R808) For a given case-construct, each case-value shall be of
7479 the same type as case-expr. For character type, length differences
7480 are allowed, but the kind type parameters shall be the same. */
7482 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7484 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7485 &e->where, case_expr->ts.kind);
7486 return false;
7489 /* Convert the case value kind to that of case expression kind,
7490 if needed */
7492 if (e->ts.kind != case_expr->ts.kind)
7493 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7495 if (e->rank != 0)
7497 gfc_error ("Expression in CASE statement at %L must be scalar",
7498 &e->where);
7499 return false;
7502 return true;
7506 /* Given a completely parsed select statement, we:
7508 - Validate all expressions and code within the SELECT.
7509 - Make sure that the selection expression is not of the wrong type.
7510 - Make sure that no case ranges overlap.
7511 - Eliminate unreachable cases and unreachable code resulting from
7512 removing case labels.
7514 The standard does allow unreachable cases, e.g. CASE (5:3). But
7515 they are a hassle for code generation, and to prevent that, we just
7516 cut them out here. This is not necessary for overlapping cases
7517 because they are illegal and we never even try to generate code.
7519 We have the additional caveat that a SELECT construct could have
7520 been a computed GOTO in the source code. Fortunately we can fairly
7521 easily work around that here: The case_expr for a "real" SELECT CASE
7522 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7523 we have to do is make sure that the case_expr is a scalar integer
7524 expression. */
7526 static void
7527 resolve_select (gfc_code *code, bool select_type)
7529 gfc_code *body;
7530 gfc_expr *case_expr;
7531 gfc_case *cp, *default_case, *tail, *head;
7532 int seen_unreachable;
7533 int seen_logical;
7534 int ncases;
7535 bt type;
7536 bool t;
7538 if (code->expr1 == NULL)
7540 /* This was actually a computed GOTO statement. */
7541 case_expr = code->expr2;
7542 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7543 gfc_error ("Selection expression in computed GOTO statement "
7544 "at %L must be a scalar integer expression",
7545 &case_expr->where);
7547 /* Further checking is not necessary because this SELECT was built
7548 by the compiler, so it should always be OK. Just move the
7549 case_expr from expr2 to expr so that we can handle computed
7550 GOTOs as normal SELECTs from here on. */
7551 code->expr1 = code->expr2;
7552 code->expr2 = NULL;
7553 return;
7556 case_expr = code->expr1;
7557 type = case_expr->ts.type;
7559 /* F08:C830. */
7560 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7562 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7563 &case_expr->where, gfc_typename (&case_expr->ts));
7565 /* Punt. Going on here just produce more garbage error messages. */
7566 return;
7569 /* F08:R842. */
7570 if (!select_type && case_expr->rank != 0)
7572 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7573 "expression", &case_expr->where);
7575 /* Punt. */
7576 return;
7579 /* Raise a warning if an INTEGER case value exceeds the range of
7580 the case-expr. Later, all expressions will be promoted to the
7581 largest kind of all case-labels. */
7583 if (type == BT_INTEGER)
7584 for (body = code->block; body; body = body->block)
7585 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7587 if (cp->low
7588 && gfc_check_integer_range (cp->low->value.integer,
7589 case_expr->ts.kind) != ARITH_OK)
7590 gfc_warning ("Expression in CASE statement at %L is "
7591 "not in the range of %s", &cp->low->where,
7592 gfc_typename (&case_expr->ts));
7594 if (cp->high
7595 && cp->low != cp->high
7596 && gfc_check_integer_range (cp->high->value.integer,
7597 case_expr->ts.kind) != ARITH_OK)
7598 gfc_warning ("Expression in CASE statement at %L is "
7599 "not in the range of %s", &cp->high->where,
7600 gfc_typename (&case_expr->ts));
7603 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7604 of the SELECT CASE expression and its CASE values. Walk the lists
7605 of case values, and if we find a mismatch, promote case_expr to
7606 the appropriate kind. */
7608 if (type == BT_LOGICAL || type == BT_INTEGER)
7610 for (body = code->block; body; body = body->block)
7612 /* Walk the case label list. */
7613 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7615 /* Intercept the DEFAULT case. It does not have a kind. */
7616 if (cp->low == NULL && cp->high == NULL)
7617 continue;
7619 /* Unreachable case ranges are discarded, so ignore. */
7620 if (cp->low != NULL && cp->high != NULL
7621 && cp->low != cp->high
7622 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7623 continue;
7625 if (cp->low != NULL
7626 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7627 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7629 if (cp->high != NULL
7630 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7631 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7636 /* Assume there is no DEFAULT case. */
7637 default_case = NULL;
7638 head = tail = NULL;
7639 ncases = 0;
7640 seen_logical = 0;
7642 for (body = code->block; body; body = body->block)
7644 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7645 t = true;
7646 seen_unreachable = 0;
7648 /* Walk the case label list, making sure that all case labels
7649 are legal. */
7650 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7652 /* Count the number of cases in the whole construct. */
7653 ncases++;
7655 /* Intercept the DEFAULT case. */
7656 if (cp->low == NULL && cp->high == NULL)
7658 if (default_case != NULL)
7660 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7661 "by a second DEFAULT CASE at %L",
7662 &default_case->where, &cp->where);
7663 t = false;
7664 break;
7666 else
7668 default_case = cp;
7669 continue;
7673 /* Deal with single value cases and case ranges. Errors are
7674 issued from the validation function. */
7675 if (!validate_case_label_expr (cp->low, case_expr)
7676 || !validate_case_label_expr (cp->high, case_expr))
7678 t = false;
7679 break;
7682 if (type == BT_LOGICAL
7683 && ((cp->low == NULL || cp->high == NULL)
7684 || cp->low != cp->high))
7686 gfc_error ("Logical range in CASE statement at %L is not "
7687 "allowed", &cp->low->where);
7688 t = false;
7689 break;
7692 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7694 int value;
7695 value = cp->low->value.logical == 0 ? 2 : 1;
7696 if (value & seen_logical)
7698 gfc_error ("Constant logical value in CASE statement "
7699 "is repeated at %L",
7700 &cp->low->where);
7701 t = false;
7702 break;
7704 seen_logical |= value;
7707 if (cp->low != NULL && cp->high != NULL
7708 && cp->low != cp->high
7709 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7711 if (warn_surprising)
7712 gfc_warning ("Range specification at %L can never "
7713 "be matched", &cp->where);
7715 cp->unreachable = 1;
7716 seen_unreachable = 1;
7718 else
7720 /* If the case range can be matched, it can also overlap with
7721 other cases. To make sure it does not, we put it in a
7722 double linked list here. We sort that with a merge sort
7723 later on to detect any overlapping cases. */
7724 if (!head)
7726 head = tail = cp;
7727 head->right = head->left = NULL;
7729 else
7731 tail->right = cp;
7732 tail->right->left = tail;
7733 tail = tail->right;
7734 tail->right = NULL;
7739 /* It there was a failure in the previous case label, give up
7740 for this case label list. Continue with the next block. */
7741 if (!t)
7742 continue;
7744 /* See if any case labels that are unreachable have been seen.
7745 If so, we eliminate them. This is a bit of a kludge because
7746 the case lists for a single case statement (label) is a
7747 single forward linked lists. */
7748 if (seen_unreachable)
7750 /* Advance until the first case in the list is reachable. */
7751 while (body->ext.block.case_list != NULL
7752 && body->ext.block.case_list->unreachable)
7754 gfc_case *n = body->ext.block.case_list;
7755 body->ext.block.case_list = body->ext.block.case_list->next;
7756 n->next = NULL;
7757 gfc_free_case_list (n);
7760 /* Strip all other unreachable cases. */
7761 if (body->ext.block.case_list)
7763 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
7765 if (cp->next->unreachable)
7767 gfc_case *n = cp->next;
7768 cp->next = cp->next->next;
7769 n->next = NULL;
7770 gfc_free_case_list (n);
7777 /* See if there were overlapping cases. If the check returns NULL,
7778 there was overlap. In that case we don't do anything. If head
7779 is non-NULL, we prepend the DEFAULT case. The sorted list can
7780 then used during code generation for SELECT CASE constructs with
7781 a case expression of a CHARACTER type. */
7782 if (head)
7784 head = check_case_overlap (head);
7786 /* Prepend the default_case if it is there. */
7787 if (head != NULL && default_case)
7789 default_case->left = NULL;
7790 default_case->right = head;
7791 head->left = default_case;
7795 /* Eliminate dead blocks that may be the result if we've seen
7796 unreachable case labels for a block. */
7797 for (body = code; body && body->block; body = body->block)
7799 if (body->block->ext.block.case_list == NULL)
7801 /* Cut the unreachable block from the code chain. */
7802 gfc_code *c = body->block;
7803 body->block = c->block;
7805 /* Kill the dead block, but not the blocks below it. */
7806 c->block = NULL;
7807 gfc_free_statements (c);
7811 /* More than two cases is legal but insane for logical selects.
7812 Issue a warning for it. */
7813 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
7814 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7815 &code->loc);
7819 /* Check if a derived type is extensible. */
7821 bool
7822 gfc_type_is_extensible (gfc_symbol *sym)
7824 return !(sym->attr.is_bind_c || sym->attr.sequence
7825 || (sym->attr.is_class
7826 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7830 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7831 correct as well as possibly the array-spec. */
7833 static void
7834 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7836 gfc_expr* target;
7838 gcc_assert (sym->assoc);
7839 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7841 /* If this is for SELECT TYPE, the target may not yet be set. In that
7842 case, return. Resolution will be called later manually again when
7843 this is done. */
7844 target = sym->assoc->target;
7845 if (!target)
7846 return;
7847 gcc_assert (!sym->assoc->dangling);
7849 if (resolve_target && !gfc_resolve_expr (target))
7850 return;
7852 /* For variable targets, we get some attributes from the target. */
7853 if (target->expr_type == EXPR_VARIABLE)
7855 gfc_symbol* tsym;
7857 gcc_assert (target->symtree);
7858 tsym = target->symtree->n.sym;
7860 sym->attr.asynchronous = tsym->attr.asynchronous;
7861 sym->attr.volatile_ = tsym->attr.volatile_;
7863 sym->attr.target = tsym->attr.target
7864 || gfc_expr_attr (target).pointer;
7865 if (is_subref_array (target))
7866 sym->attr.subref_array_pointer = 1;
7869 /* Get type if this was not already set. Note that it can be
7870 some other type than the target in case this is a SELECT TYPE
7871 selector! So we must not update when the type is already there. */
7872 if (sym->ts.type == BT_UNKNOWN)
7873 sym->ts = target->ts;
7874 gcc_assert (sym->ts.type != BT_UNKNOWN);
7876 /* See if this is a valid association-to-variable. */
7877 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7878 && !gfc_has_vector_subscript (target));
7880 /* Finally resolve if this is an array or not. */
7881 if (sym->attr.dimension && target->rank == 0)
7883 gfc_error ("Associate-name '%s' at %L is used as array",
7884 sym->name, &sym->declared_at);
7885 sym->attr.dimension = 0;
7886 return;
7889 /* We cannot deal with class selectors that need temporaries. */
7890 if (target->ts.type == BT_CLASS
7891 && gfc_ref_needs_temporary_p (target->ref))
7893 gfc_error ("CLASS selector at %L needs a temporary which is not "
7894 "yet implemented", &target->where);
7895 return;
7898 if (target->ts.type != BT_CLASS && target->rank > 0)
7899 sym->attr.dimension = 1;
7900 else if (target->ts.type == BT_CLASS)
7901 gfc_fix_class_refs (target);
7903 /* The associate-name will have a correct type by now. Make absolutely
7904 sure that it has not picked up a dimension attribute. */
7905 if (sym->ts.type == BT_CLASS)
7906 sym->attr.dimension = 0;
7908 if (sym->attr.dimension)
7910 sym->as = gfc_get_array_spec ();
7911 sym->as->rank = target->rank;
7912 sym->as->type = AS_DEFERRED;
7913 sym->as->corank = gfc_get_corank (target);
7916 /* Mark this as an associate variable. */
7917 sym->attr.associate_var = 1;
7919 /* If the target is a good class object, so is the associate variable. */
7920 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7921 sym->attr.class_ok = 1;
7925 /* Resolve a SELECT TYPE statement. */
7927 static void
7928 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7930 gfc_symbol *selector_type;
7931 gfc_code *body, *new_st, *if_st, *tail;
7932 gfc_code *class_is = NULL, *default_case = NULL;
7933 gfc_case *c;
7934 gfc_symtree *st;
7935 char name[GFC_MAX_SYMBOL_LEN];
7936 gfc_namespace *ns;
7937 int error = 0;
7938 int charlen = 0;
7940 ns = code->ext.block.ns;
7941 gfc_resolve (ns);
7943 /* Check for F03:C813. */
7944 if (code->expr1->ts.type != BT_CLASS
7945 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7947 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7948 "at %L", &code->loc);
7949 return;
7952 if (!code->expr1->symtree->n.sym->attr.class_ok)
7953 return;
7955 if (code->expr2)
7957 if (code->expr1->symtree->n.sym->attr.untyped)
7958 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7959 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7961 /* F2008: C803 The selector expression must not be coindexed. */
7962 if (gfc_is_coindexed (code->expr2))
7964 gfc_error ("Selector at %L must not be coindexed",
7965 &code->expr2->where);
7966 return;
7970 else
7972 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7974 if (gfc_is_coindexed (code->expr1))
7976 gfc_error ("Selector at %L must not be coindexed",
7977 &code->expr1->where);
7978 return;
7982 /* Loop over TYPE IS / CLASS IS cases. */
7983 for (body = code->block; body; body = body->block)
7985 c = body->ext.block.case_list;
7987 /* Check F03:C815. */
7988 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7989 && !selector_type->attr.unlimited_polymorphic
7990 && !gfc_type_is_extensible (c->ts.u.derived))
7992 gfc_error ("Derived type '%s' at %L must be extensible",
7993 c->ts.u.derived->name, &c->where);
7994 error++;
7995 continue;
7998 /* Check F03:C816. */
7999 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8000 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8001 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8003 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8004 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
8005 c->ts.u.derived->name, &c->where, selector_type->name);
8006 else
8007 gfc_error ("Unexpected intrinsic type '%s' at %L",
8008 gfc_basic_typename (c->ts.type), &c->where);
8009 error++;
8010 continue;
8013 /* Check F03:C814. */
8014 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8016 gfc_error ("The type-spec at %L shall specify that each length "
8017 "type parameter is assumed", &c->where);
8018 error++;
8019 continue;
8022 /* Intercept the DEFAULT case. */
8023 if (c->ts.type == BT_UNKNOWN)
8025 /* Check F03:C818. */
8026 if (default_case)
8028 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8029 "by a second DEFAULT CASE at %L",
8030 &default_case->ext.block.case_list->where, &c->where);
8031 error++;
8032 continue;
8035 default_case = body;
8039 if (error > 0)
8040 return;
8042 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8043 target if present. If there are any EXIT statements referring to the
8044 SELECT TYPE construct, this is no problem because the gfc_code
8045 reference stays the same and EXIT is equally possible from the BLOCK
8046 it is changed to. */
8047 code->op = EXEC_BLOCK;
8048 if (code->expr2)
8050 gfc_association_list* assoc;
8052 assoc = gfc_get_association_list ();
8053 assoc->st = code->expr1->symtree;
8054 assoc->target = gfc_copy_expr (code->expr2);
8055 assoc->target->where = code->expr2->where;
8056 /* assoc->variable will be set by resolve_assoc_var. */
8058 code->ext.block.assoc = assoc;
8059 code->expr1->symtree->n.sym->assoc = assoc;
8061 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8063 else
8064 code->ext.block.assoc = NULL;
8066 /* Add EXEC_SELECT to switch on type. */
8067 new_st = gfc_get_code (code->op);
8068 new_st->expr1 = code->expr1;
8069 new_st->expr2 = code->expr2;
8070 new_st->block = code->block;
8071 code->expr1 = code->expr2 = NULL;
8072 code->block = NULL;
8073 if (!ns->code)
8074 ns->code = new_st;
8075 else
8076 ns->code->next = new_st;
8077 code = new_st;
8078 code->op = EXEC_SELECT;
8080 gfc_add_vptr_component (code->expr1);
8081 gfc_add_hash_component (code->expr1);
8083 /* Loop over TYPE IS / CLASS IS cases. */
8084 for (body = code->block; body; body = body->block)
8086 c = body->ext.block.case_list;
8088 if (c->ts.type == BT_DERIVED)
8089 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8090 c->ts.u.derived->hash_value);
8091 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8093 gfc_symbol *ivtab;
8094 gfc_expr *e;
8096 ivtab = gfc_find_vtab (&c->ts);
8097 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8098 e = CLASS_DATA (ivtab)->initializer;
8099 c->low = c->high = gfc_copy_expr (e);
8102 else if (c->ts.type == BT_UNKNOWN)
8103 continue;
8105 /* Associate temporary to selector. This should only be done
8106 when this case is actually true, so build a new ASSOCIATE
8107 that does precisely this here (instead of using the
8108 'global' one). */
8110 if (c->ts.type == BT_CLASS)
8111 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8112 else if (c->ts.type == BT_DERIVED)
8113 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8114 else if (c->ts.type == BT_CHARACTER)
8116 if (c->ts.u.cl && c->ts.u.cl->length
8117 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8118 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8119 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8120 charlen, c->ts.kind);
8122 else
8123 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8124 c->ts.kind);
8126 st = gfc_find_symtree (ns->sym_root, name);
8127 gcc_assert (st->n.sym->assoc);
8128 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8129 st->n.sym->assoc->target->where = code->expr1->where;
8130 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8131 gfc_add_data_component (st->n.sym->assoc->target);
8133 new_st = gfc_get_code (EXEC_BLOCK);
8134 new_st->ext.block.ns = gfc_build_block_ns (ns);
8135 new_st->ext.block.ns->code = body->next;
8136 body->next = new_st;
8138 /* Chain in the new list only if it is marked as dangling. Otherwise
8139 there is a CASE label overlap and this is already used. Just ignore,
8140 the error is diagnosed elsewhere. */
8141 if (st->n.sym->assoc->dangling)
8143 new_st->ext.block.assoc = st->n.sym->assoc;
8144 st->n.sym->assoc->dangling = 0;
8147 resolve_assoc_var (st->n.sym, false);
8150 /* Take out CLASS IS cases for separate treatment. */
8151 body = code;
8152 while (body && body->block)
8154 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8156 /* Add to class_is list. */
8157 if (class_is == NULL)
8159 class_is = body->block;
8160 tail = class_is;
8162 else
8164 for (tail = class_is; tail->block; tail = tail->block) ;
8165 tail->block = body->block;
8166 tail = tail->block;
8168 /* Remove from EXEC_SELECT list. */
8169 body->block = body->block->block;
8170 tail->block = NULL;
8172 else
8173 body = body->block;
8176 if (class_is)
8178 gfc_symbol *vtab;
8180 if (!default_case)
8182 /* Add a default case to hold the CLASS IS cases. */
8183 for (tail = code; tail->block; tail = tail->block) ;
8184 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8185 tail = tail->block;
8186 tail->ext.block.case_list = gfc_get_case ();
8187 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8188 tail->next = NULL;
8189 default_case = tail;
8192 /* More than one CLASS IS block? */
8193 if (class_is->block)
8195 gfc_code **c1,*c2;
8196 bool swapped;
8197 /* Sort CLASS IS blocks by extension level. */
8200 swapped = false;
8201 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8203 c2 = (*c1)->block;
8204 /* F03:C817 (check for doubles). */
8205 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8206 == c2->ext.block.case_list->ts.u.derived->hash_value)
8208 gfc_error ("Double CLASS IS block in SELECT TYPE "
8209 "statement at %L",
8210 &c2->ext.block.case_list->where);
8211 return;
8213 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8214 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8216 /* Swap. */
8217 (*c1)->block = c2->block;
8218 c2->block = *c1;
8219 *c1 = c2;
8220 swapped = true;
8224 while (swapped);
8227 /* Generate IF chain. */
8228 if_st = gfc_get_code (EXEC_IF);
8229 new_st = if_st;
8230 for (body = class_is; body; body = body->block)
8232 new_st->block = gfc_get_code (EXEC_IF);
8233 new_st = new_st->block;
8234 /* Set up IF condition: Call _gfortran_is_extension_of. */
8235 new_st->expr1 = gfc_get_expr ();
8236 new_st->expr1->expr_type = EXPR_FUNCTION;
8237 new_st->expr1->ts.type = BT_LOGICAL;
8238 new_st->expr1->ts.kind = 4;
8239 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8240 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8241 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8242 /* Set up arguments. */
8243 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8244 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8245 new_st->expr1->value.function.actual->expr->where = code->loc;
8246 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8247 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8248 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8249 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8250 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8251 new_st->next = body->next;
8253 if (default_case->next)
8255 new_st->block = gfc_get_code (EXEC_IF);
8256 new_st = new_st->block;
8257 new_st->next = default_case->next;
8260 /* Replace CLASS DEFAULT code by the IF chain. */
8261 default_case->next = if_st;
8264 /* Resolve the internal code. This can not be done earlier because
8265 it requires that the sym->assoc of selectors is set already. */
8266 gfc_current_ns = ns;
8267 gfc_resolve_blocks (code->block, gfc_current_ns);
8268 gfc_current_ns = old_ns;
8270 resolve_select (code, true);
8274 /* Resolve a transfer statement. This is making sure that:
8275 -- a derived type being transferred has only non-pointer components
8276 -- a derived type being transferred doesn't have private components, unless
8277 it's being transferred from the module where the type was defined
8278 -- we're not trying to transfer a whole assumed size array. */
8280 static void
8281 resolve_transfer (gfc_code *code)
8283 gfc_typespec *ts;
8284 gfc_symbol *sym;
8285 gfc_ref *ref;
8286 gfc_expr *exp;
8288 exp = code->expr1;
8290 while (exp != NULL && exp->expr_type == EXPR_OP
8291 && exp->value.op.op == INTRINSIC_PARENTHESES)
8292 exp = exp->value.op.op1;
8294 if (exp && exp->expr_type == EXPR_NULL
8295 && code->ext.dt)
8297 gfc_error ("Invalid context for NULL () intrinsic at %L",
8298 &exp->where);
8299 return;
8302 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8303 && exp->expr_type != EXPR_FUNCTION))
8304 return;
8306 /* If we are reading, the variable will be changed. Note that
8307 code->ext.dt may be NULL if the TRANSFER is related to
8308 an INQUIRE statement -- but in this case, we are not reading, either. */
8309 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8310 && !gfc_check_vardef_context (exp, false, false, false,
8311 _("item in READ")))
8312 return;
8314 sym = exp->symtree->n.sym;
8315 ts = &sym->ts;
8317 /* Go to actual component transferred. */
8318 for (ref = exp->ref; ref; ref = ref->next)
8319 if (ref->type == REF_COMPONENT)
8320 ts = &ref->u.c.component->ts;
8322 if (ts->type == BT_CLASS)
8324 /* FIXME: Test for defined input/output. */
8325 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8326 "it is processed by a defined input/output procedure",
8327 &code->loc);
8328 return;
8331 if (ts->type == BT_DERIVED)
8333 /* Check that transferred derived type doesn't contain POINTER
8334 components. */
8335 if (ts->u.derived->attr.pointer_comp)
8337 gfc_error ("Data transfer element at %L cannot have POINTER "
8338 "components unless it is processed by a defined "
8339 "input/output procedure", &code->loc);
8340 return;
8343 /* F08:C935. */
8344 if (ts->u.derived->attr.proc_pointer_comp)
8346 gfc_error ("Data transfer element at %L cannot have "
8347 "procedure pointer components", &code->loc);
8348 return;
8351 if (ts->u.derived->attr.alloc_comp)
8353 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8354 "components unless it is processed by a defined "
8355 "input/output procedure", &code->loc);
8356 return;
8359 /* C_PTR and C_FUNPTR have private components which means they can not
8360 be printed. However, if -std=gnu and not -pedantic, allow
8361 the component to be printed to help debugging. */
8362 if (ts->u.derived->ts.f90_type == BT_VOID)
8364 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8365 "cannot have PRIVATE components", &code->loc))
8366 return;
8368 else if (derived_inaccessible (ts->u.derived))
8370 gfc_error ("Data transfer element at %L cannot have "
8371 "PRIVATE components",&code->loc);
8372 return;
8376 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8377 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8379 gfc_error ("Data transfer element at %L cannot be a full reference to "
8380 "an assumed-size array", &code->loc);
8381 return;
8386 /*********** Toplevel code resolution subroutines ***********/
8388 /* Find the set of labels that are reachable from this block. We also
8389 record the last statement in each block. */
8391 static void
8392 find_reachable_labels (gfc_code *block)
8394 gfc_code *c;
8396 if (!block)
8397 return;
8399 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8401 /* Collect labels in this block. We don't keep those corresponding
8402 to END {IF|SELECT}, these are checked in resolve_branch by going
8403 up through the code_stack. */
8404 for (c = block; c; c = c->next)
8406 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8407 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8410 /* Merge with labels from parent block. */
8411 if (cs_base->prev)
8413 gcc_assert (cs_base->prev->reachable_labels);
8414 bitmap_ior_into (cs_base->reachable_labels,
8415 cs_base->prev->reachable_labels);
8420 static void
8421 resolve_lock_unlock (gfc_code *code)
8423 if (code->expr1->expr_type == EXPR_FUNCTION
8424 && code->expr1->value.function.isym
8425 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8426 remove_caf_get_intrinsic (code->expr1);
8428 if (code->expr1->ts.type != BT_DERIVED
8429 || code->expr1->expr_type != EXPR_VARIABLE
8430 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8431 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8432 || code->expr1->rank != 0
8433 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8434 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8435 &code->expr1->where);
8437 /* Check STAT. */
8438 if (code->expr2
8439 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8440 || code->expr2->expr_type != EXPR_VARIABLE))
8441 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8442 &code->expr2->where);
8444 if (code->expr2
8445 && !gfc_check_vardef_context (code->expr2, false, false, false,
8446 _("STAT variable")))
8447 return;
8449 /* Check ERRMSG. */
8450 if (code->expr3
8451 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8452 || code->expr3->expr_type != EXPR_VARIABLE))
8453 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8454 &code->expr3->where);
8456 if (code->expr3
8457 && !gfc_check_vardef_context (code->expr3, false, false, false,
8458 _("ERRMSG variable")))
8459 return;
8461 /* Check ACQUIRED_LOCK. */
8462 if (code->expr4
8463 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8464 || code->expr4->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8466 "variable", &code->expr4->where);
8468 if (code->expr4
8469 && !gfc_check_vardef_context (code->expr4, false, false, false,
8470 _("ACQUIRED_LOCK variable")))
8471 return;
8475 static void
8476 resolve_critical (gfc_code *code)
8478 gfc_symtree *symtree;
8479 gfc_symbol *lock_type;
8480 char name[GFC_MAX_SYMBOL_LEN];
8481 static int serial = 0;
8483 if (gfc_option.coarray != GFC_FCOARRAY_LIB)
8484 return;
8486 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8487 GFC_PREFIX ("lock_type"));
8488 if (symtree)
8489 lock_type = symtree->n.sym;
8490 else
8492 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8493 false) != 0)
8494 gcc_unreachable ();
8495 lock_type = symtree->n.sym;
8496 lock_type->attr.flavor = FL_DERIVED;
8497 lock_type->attr.zero_comp = 1;
8498 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8499 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8502 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
8503 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8504 gcc_unreachable ();
8506 code->resolved_sym = symtree->n.sym;
8507 symtree->n.sym->attr.flavor = FL_VARIABLE;
8508 symtree->n.sym->attr.referenced = 1;
8509 symtree->n.sym->attr.artificial = 1;
8510 symtree->n.sym->attr.codimension = 1;
8511 symtree->n.sym->ts.type = BT_DERIVED;
8512 symtree->n.sym->ts.u.derived = lock_type;
8513 symtree->n.sym->as = gfc_get_array_spec ();
8514 symtree->n.sym->as->corank = 1;
8515 symtree->n.sym->as->type = AS_EXPLICIT;
8516 symtree->n.sym->as->cotype = AS_EXPLICIT;
8517 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8518 NULL, 1);
8522 static void
8523 resolve_sync (gfc_code *code)
8525 /* Check imageset. The * case matches expr1 == NULL. */
8526 if (code->expr1)
8528 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8529 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8530 "INTEGER expression", &code->expr1->where);
8531 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8532 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8533 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8534 &code->expr1->where);
8535 else if (code->expr1->expr_type == EXPR_ARRAY
8536 && gfc_simplify_expr (code->expr1, 0))
8538 gfc_constructor *cons;
8539 cons = gfc_constructor_first (code->expr1->value.constructor);
8540 for (; cons; cons = gfc_constructor_next (cons))
8541 if (cons->expr->expr_type == EXPR_CONSTANT
8542 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8543 gfc_error ("Imageset argument at %L must between 1 and "
8544 "num_images()", &cons->expr->where);
8548 /* Check STAT. */
8549 if (code->expr2
8550 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8551 || code->expr2->expr_type != EXPR_VARIABLE))
8552 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8553 &code->expr2->where);
8555 /* Check ERRMSG. */
8556 if (code->expr3
8557 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8558 || code->expr3->expr_type != EXPR_VARIABLE))
8559 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8560 &code->expr3->where);
8564 /* Given a branch to a label, see if the branch is conforming.
8565 The code node describes where the branch is located. */
8567 static void
8568 resolve_branch (gfc_st_label *label, gfc_code *code)
8570 code_stack *stack;
8572 if (label == NULL)
8573 return;
8575 /* Step one: is this a valid branching target? */
8577 if (label->defined == ST_LABEL_UNKNOWN)
8579 gfc_error ("Label %d referenced at %L is never defined", label->value,
8580 &label->where);
8581 return;
8584 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8586 gfc_error ("Statement at %L is not a valid branch target statement "
8587 "for the branch statement at %L", &label->where, &code->loc);
8588 return;
8591 /* Step two: make sure this branch is not a branch to itself ;-) */
8593 if (code->here == label)
8595 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8596 return;
8599 /* Step three: See if the label is in the same block as the
8600 branching statement. The hard work has been done by setting up
8601 the bitmap reachable_labels. */
8603 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8605 /* Check now whether there is a CRITICAL construct; if so, check
8606 whether the label is still visible outside of the CRITICAL block,
8607 which is invalid. */
8608 for (stack = cs_base; stack; stack = stack->prev)
8610 if (stack->current->op == EXEC_CRITICAL
8611 && bitmap_bit_p (stack->reachable_labels, label->value))
8612 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8613 "label at %L", &code->loc, &label->where);
8614 else if (stack->current->op == EXEC_DO_CONCURRENT
8615 && bitmap_bit_p (stack->reachable_labels, label->value))
8616 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8617 "for label at %L", &code->loc, &label->where);
8620 return;
8623 /* Step four: If we haven't found the label in the bitmap, it may
8624 still be the label of the END of the enclosing block, in which
8625 case we find it by going up the code_stack. */
8627 for (stack = cs_base; stack; stack = stack->prev)
8629 if (stack->current->next && stack->current->next->here == label)
8630 break;
8631 if (stack->current->op == EXEC_CRITICAL)
8633 /* Note: A label at END CRITICAL does not leave the CRITICAL
8634 construct as END CRITICAL is still part of it. */
8635 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8636 " at %L", &code->loc, &label->where);
8637 return;
8639 else if (stack->current->op == EXEC_DO_CONCURRENT)
8641 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8642 "label at %L", &code->loc, &label->where);
8643 return;
8647 if (stack)
8649 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8650 return;
8653 /* The label is not in an enclosing block, so illegal. This was
8654 allowed in Fortran 66, so we allow it as extension. No
8655 further checks are necessary in this case. */
8656 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8657 "as the GOTO statement at %L", &label->where,
8658 &code->loc);
8659 return;
8663 /* Check whether EXPR1 has the same shape as EXPR2. */
8665 static bool
8666 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8668 mpz_t shape[GFC_MAX_DIMENSIONS];
8669 mpz_t shape2[GFC_MAX_DIMENSIONS];
8670 bool result = false;
8671 int i;
8673 /* Compare the rank. */
8674 if (expr1->rank != expr2->rank)
8675 return result;
8677 /* Compare the size of each dimension. */
8678 for (i=0; i<expr1->rank; i++)
8680 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8681 goto ignore;
8683 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8684 goto ignore;
8686 if (mpz_cmp (shape[i], shape2[i]))
8687 goto over;
8690 /* When either of the two expression is an assumed size array, we
8691 ignore the comparison of dimension sizes. */
8692 ignore:
8693 result = true;
8695 over:
8696 gfc_clear_shape (shape, i);
8697 gfc_clear_shape (shape2, i);
8698 return result;
8702 /* Check whether a WHERE assignment target or a WHERE mask expression
8703 has the same shape as the outmost WHERE mask expression. */
8705 static void
8706 resolve_where (gfc_code *code, gfc_expr *mask)
8708 gfc_code *cblock;
8709 gfc_code *cnext;
8710 gfc_expr *e = NULL;
8712 cblock = code->block;
8714 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8715 In case of nested WHERE, only the outmost one is stored. */
8716 if (mask == NULL) /* outmost WHERE */
8717 e = cblock->expr1;
8718 else /* inner WHERE */
8719 e = mask;
8721 while (cblock)
8723 if (cblock->expr1)
8725 /* Check if the mask-expr has a consistent shape with the
8726 outmost WHERE mask-expr. */
8727 if (!resolve_where_shape (cblock->expr1, e))
8728 gfc_error ("WHERE mask at %L has inconsistent shape",
8729 &cblock->expr1->where);
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:
8742 /* Check shape consistent for WHERE assignment target. */
8743 if (e && !resolve_where_shape (cnext->expr1, e))
8744 gfc_error ("WHERE assignment target at %L has "
8745 "inconsistent shape", &cnext->expr1->where);
8746 break;
8749 case EXEC_ASSIGN_CALL:
8750 resolve_call (cnext);
8751 if (!cnext->resolved_sym->attr.elemental)
8752 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8753 &cnext->ext.actual->expr->where);
8754 break;
8756 /* WHERE or WHERE construct is part of a where-body-construct */
8757 case EXEC_WHERE:
8758 resolve_where (cnext, e);
8759 break;
8761 default:
8762 gfc_error ("Unsupported statement inside WHERE at %L",
8763 &cnext->loc);
8765 /* the next statement within the same where-body-construct */
8766 cnext = cnext->next;
8768 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8769 cblock = cblock->block;
8774 /* Resolve assignment in FORALL construct.
8775 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8776 FORALL index variables. */
8778 static void
8779 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8781 int n;
8783 for (n = 0; n < nvar; n++)
8785 gfc_symbol *forall_index;
8787 forall_index = var_expr[n]->symtree->n.sym;
8789 /* Check whether the assignment target is one of the FORALL index
8790 variable. */
8791 if ((code->expr1->expr_type == EXPR_VARIABLE)
8792 && (code->expr1->symtree->n.sym == forall_index))
8793 gfc_error ("Assignment to a FORALL index variable at %L",
8794 &code->expr1->where);
8795 else
8797 /* If one of the FORALL index variables doesn't appear in the
8798 assignment variable, then there could be a many-to-one
8799 assignment. Emit a warning rather than an error because the
8800 mask could be resolving this problem. */
8801 if (!find_forall_index (code->expr1, forall_index, 0))
8802 gfc_warning ("The FORALL with index '%s' is not used on the "
8803 "left side of the assignment at %L and so might "
8804 "cause multiple assignment to this object",
8805 var_expr[n]->symtree->name, &code->expr1->where);
8811 /* Resolve WHERE statement in FORALL construct. */
8813 static void
8814 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8815 gfc_expr **var_expr)
8817 gfc_code *cblock;
8818 gfc_code *cnext;
8820 cblock = code->block;
8821 while (cblock)
8823 /* the assignment statement of a WHERE statement, or the first
8824 statement in where-body-construct of a WHERE construct */
8825 cnext = cblock->next;
8826 while (cnext)
8828 switch (cnext->op)
8830 /* WHERE assignment statement */
8831 case EXEC_ASSIGN:
8832 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8833 break;
8835 /* WHERE operator assignment statement */
8836 case EXEC_ASSIGN_CALL:
8837 resolve_call (cnext);
8838 if (!cnext->resolved_sym->attr.elemental)
8839 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8840 &cnext->ext.actual->expr->where);
8841 break;
8843 /* WHERE or WHERE construct is part of a where-body-construct */
8844 case EXEC_WHERE:
8845 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8846 break;
8848 default:
8849 gfc_error ("Unsupported statement inside WHERE at %L",
8850 &cnext->loc);
8852 /* the next statement within the same where-body-construct */
8853 cnext = cnext->next;
8855 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8856 cblock = cblock->block;
8861 /* Traverse the FORALL body to check whether the following errors exist:
8862 1. For assignment, check if a many-to-one assignment happens.
8863 2. For WHERE statement, check the WHERE body to see if there is any
8864 many-to-one assignment. */
8866 static void
8867 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8869 gfc_code *c;
8871 c = code->block->next;
8872 while (c)
8874 switch (c->op)
8876 case EXEC_ASSIGN:
8877 case EXEC_POINTER_ASSIGN:
8878 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8879 break;
8881 case EXEC_ASSIGN_CALL:
8882 resolve_call (c);
8883 break;
8885 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8886 there is no need to handle it here. */
8887 case EXEC_FORALL:
8888 break;
8889 case EXEC_WHERE:
8890 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8891 break;
8892 default:
8893 break;
8895 /* The next statement in the FORALL body. */
8896 c = c->next;
8901 /* Counts the number of iterators needed inside a forall construct, including
8902 nested forall constructs. This is used to allocate the needed memory
8903 in gfc_resolve_forall. */
8905 static int
8906 gfc_count_forall_iterators (gfc_code *code)
8908 int max_iters, sub_iters, current_iters;
8909 gfc_forall_iterator *fa;
8911 gcc_assert(code->op == EXEC_FORALL);
8912 max_iters = 0;
8913 current_iters = 0;
8915 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8916 current_iters ++;
8918 code = code->block->next;
8920 while (code)
8922 if (code->op == EXEC_FORALL)
8924 sub_iters = gfc_count_forall_iterators (code);
8925 if (sub_iters > max_iters)
8926 max_iters = sub_iters;
8928 code = code->next;
8931 return current_iters + max_iters;
8935 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8936 gfc_resolve_forall_body to resolve the FORALL body. */
8938 static void
8939 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8941 static gfc_expr **var_expr;
8942 static int total_var = 0;
8943 static int nvar = 0;
8944 int old_nvar, tmp;
8945 gfc_forall_iterator *fa;
8946 int i;
8948 old_nvar = nvar;
8950 /* Start to resolve a FORALL construct */
8951 if (forall_save == 0)
8953 /* Count the total number of FORALL index in the nested FORALL
8954 construct in order to allocate the VAR_EXPR with proper size. */
8955 total_var = gfc_count_forall_iterators (code);
8957 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8958 var_expr = XCNEWVEC (gfc_expr *, total_var);
8961 /* The information about FORALL iterator, including FORALL index start, end
8962 and stride. The FORALL index can not appear in start, end or stride. */
8963 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8965 /* Check if any outer FORALL index name is the same as the current
8966 one. */
8967 for (i = 0; i < nvar; i++)
8969 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8971 gfc_error ("An outer FORALL construct already has an index "
8972 "with this name %L", &fa->var->where);
8976 /* Record the current FORALL index. */
8977 var_expr[nvar] = gfc_copy_expr (fa->var);
8979 nvar++;
8981 /* No memory leak. */
8982 gcc_assert (nvar <= total_var);
8985 /* Resolve the FORALL body. */
8986 gfc_resolve_forall_body (code, nvar, var_expr);
8988 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8989 gfc_resolve_blocks (code->block, ns);
8991 tmp = nvar;
8992 nvar = old_nvar;
8993 /* Free only the VAR_EXPRs allocated in this frame. */
8994 for (i = nvar; i < tmp; i++)
8995 gfc_free_expr (var_expr[i]);
8997 if (nvar == 0)
8999 /* We are in the outermost FORALL construct. */
9000 gcc_assert (forall_save == 0);
9002 /* VAR_EXPR is not needed any more. */
9003 free (var_expr);
9004 total_var = 0;
9009 /* Resolve a BLOCK construct statement. */
9011 static void
9012 resolve_block_construct (gfc_code* code)
9014 /* Resolve the BLOCK's namespace. */
9015 gfc_resolve (code->ext.block.ns);
9017 /* For an ASSOCIATE block, the associations (and their targets) are already
9018 resolved during resolve_symbol. */
9022 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9023 DO code nodes. */
9025 void
9026 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9028 bool t;
9030 for (; b; b = b->block)
9032 t = gfc_resolve_expr (b->expr1);
9033 if (!gfc_resolve_expr (b->expr2))
9034 t = false;
9036 switch (b->op)
9038 case EXEC_IF:
9039 if (t && b->expr1 != NULL
9040 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9041 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9042 &b->expr1->where);
9043 break;
9045 case EXEC_WHERE:
9046 if (t
9047 && b->expr1 != NULL
9048 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9049 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9050 &b->expr1->where);
9051 break;
9053 case EXEC_GOTO:
9054 resolve_branch (b->label1, b);
9055 break;
9057 case EXEC_BLOCK:
9058 resolve_block_construct (b);
9059 break;
9061 case EXEC_SELECT:
9062 case EXEC_SELECT_TYPE:
9063 case EXEC_FORALL:
9064 case EXEC_DO:
9065 case EXEC_DO_WHILE:
9066 case EXEC_DO_CONCURRENT:
9067 case EXEC_CRITICAL:
9068 case EXEC_READ:
9069 case EXEC_WRITE:
9070 case EXEC_IOLENGTH:
9071 case EXEC_WAIT:
9072 break;
9074 case EXEC_OMP_ATOMIC:
9075 case EXEC_OMP_CRITICAL:
9076 case EXEC_OMP_DISTRIBUTE:
9077 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9078 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9079 case EXEC_OMP_DISTRIBUTE_SIMD:
9080 case EXEC_OMP_DO:
9081 case EXEC_OMP_DO_SIMD:
9082 case EXEC_OMP_MASTER:
9083 case EXEC_OMP_ORDERED:
9084 case EXEC_OMP_PARALLEL:
9085 case EXEC_OMP_PARALLEL_DO:
9086 case EXEC_OMP_PARALLEL_DO_SIMD:
9087 case EXEC_OMP_PARALLEL_SECTIONS:
9088 case EXEC_OMP_PARALLEL_WORKSHARE:
9089 case EXEC_OMP_SECTIONS:
9090 case EXEC_OMP_SIMD:
9091 case EXEC_OMP_SINGLE:
9092 case EXEC_OMP_TARGET:
9093 case EXEC_OMP_TARGET_DATA:
9094 case EXEC_OMP_TARGET_TEAMS:
9095 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9096 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9097 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9098 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9099 case EXEC_OMP_TARGET_UPDATE:
9100 case EXEC_OMP_TASK:
9101 case EXEC_OMP_TASKGROUP:
9102 case EXEC_OMP_TASKWAIT:
9103 case EXEC_OMP_TASKYIELD:
9104 case EXEC_OMP_TEAMS:
9105 case EXEC_OMP_TEAMS_DISTRIBUTE:
9106 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9107 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9108 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9109 case EXEC_OMP_WORKSHARE:
9110 break;
9112 default:
9113 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9116 gfc_resolve_code (b->next, ns);
9121 /* Does everything to resolve an ordinary assignment. Returns true
9122 if this is an interface assignment. */
9123 static bool
9124 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9126 bool rval = false;
9127 gfc_expr *lhs;
9128 gfc_expr *rhs;
9129 int llen = 0;
9130 int rlen = 0;
9131 int n;
9132 gfc_ref *ref;
9133 symbol_attribute attr;
9135 if (gfc_extend_assign (code, ns))
9137 gfc_expr** rhsptr;
9139 if (code->op == EXEC_ASSIGN_CALL)
9141 lhs = code->ext.actual->expr;
9142 rhsptr = &code->ext.actual->next->expr;
9144 else
9146 gfc_actual_arglist* args;
9147 gfc_typebound_proc* tbp;
9149 gcc_assert (code->op == EXEC_COMPCALL);
9151 args = code->expr1->value.compcall.actual;
9152 lhs = args->expr;
9153 rhsptr = &args->next->expr;
9155 tbp = code->expr1->value.compcall.tbp;
9156 gcc_assert (!tbp->is_generic);
9159 /* Make a temporary rhs when there is a default initializer
9160 and rhs is the same symbol as the lhs. */
9161 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9162 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9163 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9164 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9165 *rhsptr = gfc_get_parentheses (*rhsptr);
9167 return true;
9170 lhs = code->expr1;
9171 rhs = code->expr2;
9173 if (rhs->is_boz
9174 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9175 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9176 &code->loc))
9177 return false;
9179 /* Handle the case of a BOZ literal on the RHS. */
9180 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9182 int rc;
9183 if (warn_surprising)
9184 gfc_warning ("BOZ literal at %L is bitwise transferred "
9185 "non-integer symbol '%s'", &code->loc,
9186 lhs->symtree->n.sym->name);
9188 if (!gfc_convert_boz (rhs, &lhs->ts))
9189 return false;
9190 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9192 if (rc == ARITH_UNDERFLOW)
9193 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9194 ". This check can be disabled with the option "
9195 "-fno-range-check", &rhs->where);
9196 else if (rc == ARITH_OVERFLOW)
9197 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9198 ". This check can be disabled with the option "
9199 "-fno-range-check", &rhs->where);
9200 else if (rc == ARITH_NAN)
9201 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9202 ". This check can be disabled with the option "
9203 "-fno-range-check", &rhs->where);
9204 return false;
9208 if (lhs->ts.type == BT_CHARACTER
9209 && warn_character_truncation)
9211 if (lhs->ts.u.cl != NULL
9212 && lhs->ts.u.cl->length != NULL
9213 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9214 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9216 if (rhs->expr_type == EXPR_CONSTANT)
9217 rlen = rhs->value.character.length;
9219 else if (rhs->ts.u.cl != NULL
9220 && rhs->ts.u.cl->length != NULL
9221 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9222 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9224 if (rlen && llen && rlen > llen)
9225 gfc_warning_now (OPT_Wcharacter_truncation,
9226 "CHARACTER expression will be truncated "
9227 "in assignment (%d/%d) at %L",
9228 llen, rlen, &code->loc);
9231 /* Ensure that a vector index expression for the lvalue is evaluated
9232 to a temporary if the lvalue symbol is referenced in it. */
9233 if (lhs->rank)
9235 for (ref = lhs->ref; ref; ref= ref->next)
9236 if (ref->type == REF_ARRAY)
9238 for (n = 0; n < ref->u.ar.dimen; n++)
9239 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9240 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9241 ref->u.ar.start[n]))
9242 ref->u.ar.start[n]
9243 = gfc_get_parentheses (ref->u.ar.start[n]);
9247 if (gfc_pure (NULL))
9249 if (lhs->ts.type == BT_DERIVED
9250 && lhs->expr_type == EXPR_VARIABLE
9251 && lhs->ts.u.derived->attr.pointer_comp
9252 && rhs->expr_type == EXPR_VARIABLE
9253 && (gfc_impure_variable (rhs->symtree->n.sym)
9254 || gfc_is_coindexed (rhs)))
9256 /* F2008, C1283. */
9257 if (gfc_is_coindexed (rhs))
9258 gfc_error ("Coindexed expression at %L is assigned to "
9259 "a derived type variable with a POINTER "
9260 "component in a PURE procedure",
9261 &rhs->where);
9262 else
9263 gfc_error ("The impure variable at %L is assigned to "
9264 "a derived type variable with a POINTER "
9265 "component in a PURE procedure (12.6)",
9266 &rhs->where);
9267 return rval;
9270 /* Fortran 2008, C1283. */
9271 if (gfc_is_coindexed (lhs))
9273 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9274 "procedure", &rhs->where);
9275 return rval;
9279 if (gfc_implicit_pure (NULL))
9281 if (lhs->expr_type == EXPR_VARIABLE
9282 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9283 && lhs->symtree->n.sym->ns != gfc_current_ns)
9284 gfc_unset_implicit_pure (NULL);
9286 if (lhs->ts.type == BT_DERIVED
9287 && lhs->expr_type == EXPR_VARIABLE
9288 && lhs->ts.u.derived->attr.pointer_comp
9289 && rhs->expr_type == EXPR_VARIABLE
9290 && (gfc_impure_variable (rhs->symtree->n.sym)
9291 || gfc_is_coindexed (rhs)))
9292 gfc_unset_implicit_pure (NULL);
9294 /* Fortran 2008, C1283. */
9295 if (gfc_is_coindexed (lhs))
9296 gfc_unset_implicit_pure (NULL);
9299 /* F2008, 7.2.1.2. */
9300 attr = gfc_expr_attr (lhs);
9301 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9303 if (attr.codimension)
9305 gfc_error ("Assignment to polymorphic coarray at %L is not "
9306 "permitted", &lhs->where);
9307 return false;
9309 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9310 "polymorphic variable at %L", &lhs->where))
9311 return false;
9312 if (!gfc_option.flag_realloc_lhs)
9314 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9315 "requires -frealloc-lhs", &lhs->where);
9316 return false;
9318 /* See PR 43366. */
9319 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9320 "is not yet supported", &lhs->where);
9321 return false;
9323 else if (lhs->ts.type == BT_CLASS)
9325 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9326 "assignment at %L - check that there is a matching specific "
9327 "subroutine for '=' operator", &lhs->where);
9328 return false;
9331 bool lhs_coindexed = gfc_is_coindexed (lhs);
9333 /* F2008, Section 7.2.1.2. */
9334 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9336 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9337 "component in assignment at %L", &lhs->where);
9338 return false;
9341 gfc_check_assign (lhs, rhs, 1);
9343 /* Assign the 'data' of a class object to a derived type. */
9344 if (lhs->ts.type == BT_DERIVED
9345 && rhs->ts.type == BT_CLASS)
9346 gfc_add_data_component (rhs);
9348 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9349 Additionally, insert this code when the RHS is a CAF as we then use the
9350 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9351 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9352 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9353 path. */
9354 if (gfc_option.coarray == GFC_FCOARRAY_LIB
9355 && (lhs_coindexed
9356 || (code->expr2->expr_type == EXPR_FUNCTION
9357 && code->expr2->value.function.isym
9358 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9359 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9360 && !gfc_expr_attr (rhs).allocatable
9361 && !gfc_has_vector_subscript (rhs))))
9363 if (code->expr2->expr_type == EXPR_FUNCTION
9364 && code->expr2->value.function.isym
9365 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9366 remove_caf_get_intrinsic (code->expr2);
9367 code->op = EXEC_CALL;
9368 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9369 code->resolved_sym = code->symtree->n.sym;
9370 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9371 code->resolved_sym->attr.intrinsic = 1;
9372 code->resolved_sym->attr.subroutine = 1;
9373 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9374 gfc_commit_symbol (code->resolved_sym);
9375 code->ext.actual = gfc_get_actual_arglist ();
9376 code->ext.actual->expr = lhs;
9377 code->ext.actual->next = gfc_get_actual_arglist ();
9378 code->ext.actual->next->expr = rhs;
9379 code->expr1 = NULL;
9380 code->expr2 = NULL;
9383 return false;
9387 /* Add a component reference onto an expression. */
9389 static void
9390 add_comp_ref (gfc_expr *e, gfc_component *c)
9392 gfc_ref **ref;
9393 ref = &(e->ref);
9394 while (*ref)
9395 ref = &((*ref)->next);
9396 *ref = gfc_get_ref ();
9397 (*ref)->type = REF_COMPONENT;
9398 (*ref)->u.c.sym = e->ts.u.derived;
9399 (*ref)->u.c.component = c;
9400 e->ts = c->ts;
9402 /* Add a full array ref, as necessary. */
9403 if (c->as)
9405 gfc_add_full_array_ref (e, c->as);
9406 e->rank = c->as->rank;
9411 /* Build an assignment. Keep the argument 'op' for future use, so that
9412 pointer assignments can be made. */
9414 static gfc_code *
9415 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9416 gfc_component *comp1, gfc_component *comp2, locus loc)
9418 gfc_code *this_code;
9420 this_code = gfc_get_code (op);
9421 this_code->next = NULL;
9422 this_code->expr1 = gfc_copy_expr (expr1);
9423 this_code->expr2 = gfc_copy_expr (expr2);
9424 this_code->loc = loc;
9425 if (comp1 && comp2)
9427 add_comp_ref (this_code->expr1, comp1);
9428 add_comp_ref (this_code->expr2, comp2);
9431 return this_code;
9435 /* Makes a temporary variable expression based on the characteristics of
9436 a given variable expression. */
9438 static gfc_expr*
9439 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9441 static int serial = 0;
9442 char name[GFC_MAX_SYMBOL_LEN];
9443 gfc_symtree *tmp;
9444 gfc_array_spec *as;
9445 gfc_array_ref *aref;
9446 gfc_ref *ref;
9448 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9449 gfc_get_sym_tree (name, ns, &tmp, false);
9450 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9452 as = NULL;
9453 ref = NULL;
9454 aref = NULL;
9456 /* This function could be expanded to support other expression type
9457 but this is not needed here. */
9458 gcc_assert (e->expr_type == EXPR_VARIABLE);
9460 /* Obtain the arrayspec for the temporary. */
9461 if (e->rank)
9463 aref = gfc_find_array_ref (e);
9464 if (e->expr_type == EXPR_VARIABLE
9465 && e->symtree->n.sym->as == aref->as)
9466 as = aref->as;
9467 else
9469 for (ref = e->ref; ref; ref = ref->next)
9470 if (ref->type == REF_COMPONENT
9471 && ref->u.c.component->as == aref->as)
9473 as = aref->as;
9474 break;
9479 /* Add the attributes and the arrayspec to the temporary. */
9480 tmp->n.sym->attr = gfc_expr_attr (e);
9481 tmp->n.sym->attr.function = 0;
9482 tmp->n.sym->attr.result = 0;
9483 tmp->n.sym->attr.flavor = FL_VARIABLE;
9485 if (as)
9487 tmp->n.sym->as = gfc_copy_array_spec (as);
9488 if (!ref)
9489 ref = e->ref;
9490 if (as->type == AS_DEFERRED)
9491 tmp->n.sym->attr.allocatable = 1;
9493 else
9494 tmp->n.sym->attr.dimension = 0;
9496 gfc_set_sym_referenced (tmp->n.sym);
9497 gfc_commit_symbol (tmp->n.sym);
9498 e = gfc_lval_expr_from_sym (tmp->n.sym);
9500 /* Should the lhs be a section, use its array ref for the
9501 temporary expression. */
9502 if (aref && aref->type != AR_FULL)
9504 gfc_free_ref_list (e->ref);
9505 e->ref = gfc_copy_ref (ref);
9507 return e;
9511 /* Add one line of code to the code chain, making sure that 'head' and
9512 'tail' are appropriately updated. */
9514 static void
9515 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9517 gcc_assert (this_code);
9518 if (*head == NULL)
9519 *head = *tail = *this_code;
9520 else
9521 *tail = gfc_append_code (*tail, *this_code);
9522 *this_code = NULL;
9526 /* Counts the potential number of part array references that would
9527 result from resolution of typebound defined assignments. */
9529 static int
9530 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9532 gfc_component *c;
9533 int c_depth = 0, t_depth;
9535 for (c= derived->components; c; c = c->next)
9537 if ((c->ts.type != BT_DERIVED
9538 || c->attr.pointer
9539 || c->attr.allocatable
9540 || c->attr.proc_pointer_comp
9541 || c->attr.class_pointer
9542 || c->attr.proc_pointer)
9543 && !c->attr.defined_assign_comp)
9544 continue;
9546 if (c->as && c_depth == 0)
9547 c_depth = 1;
9549 if (c->ts.u.derived->attr.defined_assign_comp)
9550 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9551 c->as ? 1 : 0);
9552 else
9553 t_depth = 0;
9555 c_depth = t_depth > c_depth ? t_depth : c_depth;
9557 return depth + c_depth;
9561 /* Implement 7.2.1.3 of the F08 standard:
9562 "An intrinsic assignment where the variable is of derived type is
9563 performed as if each component of the variable were assigned from the
9564 corresponding component of expr using pointer assignment (7.2.2) for
9565 each pointer component, defined assignment for each nonpointer
9566 nonallocatable component of a type that has a type-bound defined
9567 assignment consistent with the component, intrinsic assignment for
9568 each other nonpointer nonallocatable component, ..."
9570 The pointer assignments are taken care of by the intrinsic
9571 assignment of the structure itself. This function recursively adds
9572 defined assignments where required. The recursion is accomplished
9573 by calling gfc_resolve_code.
9575 When the lhs in a defined assignment has intent INOUT, we need a
9576 temporary for the lhs. In pseudo-code:
9578 ! Only call function lhs once.
9579 if (lhs is not a constant or an variable)
9580 temp_x = expr2
9581 expr2 => temp_x
9582 ! Do the intrinsic assignment
9583 expr1 = expr2
9584 ! Now do the defined assignments
9585 do over components with typebound defined assignment [%cmp]
9586 #if one component's assignment procedure is INOUT
9587 t1 = expr1
9588 #if expr2 non-variable
9589 temp_x = expr2
9590 expr2 => temp_x
9591 # endif
9592 expr1 = expr2
9593 # for each cmp
9594 t1%cmp {defined=} expr2%cmp
9595 expr1%cmp = t1%cmp
9596 #else
9597 expr1 = expr2
9599 # for each cmp
9600 expr1%cmp {defined=} expr2%cmp
9601 #endif
9604 /* The temporary assignments have to be put on top of the additional
9605 code to avoid the result being changed by the intrinsic assignment.
9607 static int component_assignment_level = 0;
9608 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9610 static void
9611 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9613 gfc_component *comp1, *comp2;
9614 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9615 gfc_expr *t1;
9616 int error_count, depth;
9618 gfc_get_errors (NULL, &error_count);
9620 /* Filter out continuing processing after an error. */
9621 if (error_count
9622 || (*code)->expr1->ts.type != BT_DERIVED
9623 || (*code)->expr2->ts.type != BT_DERIVED)
9624 return;
9626 /* TODO: Handle more than one part array reference in assignments. */
9627 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9628 (*code)->expr1->rank ? 1 : 0);
9629 if (depth > 1)
9631 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9632 "done because multiple part array references would "
9633 "occur in intermediate expressions.", &(*code)->loc);
9634 return;
9637 component_assignment_level++;
9639 /* Create a temporary so that functions get called only once. */
9640 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9641 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9643 gfc_expr *tmp_expr;
9645 /* Assign the rhs to the temporary. */
9646 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9647 this_code = build_assignment (EXEC_ASSIGN,
9648 tmp_expr, (*code)->expr2,
9649 NULL, NULL, (*code)->loc);
9650 /* Add the code and substitute the rhs expression. */
9651 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9652 gfc_free_expr ((*code)->expr2);
9653 (*code)->expr2 = tmp_expr;
9656 /* Do the intrinsic assignment. This is not needed if the lhs is one
9657 of the temporaries generated here, since the intrinsic assignment
9658 to the final result already does this. */
9659 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9661 this_code = build_assignment (EXEC_ASSIGN,
9662 (*code)->expr1, (*code)->expr2,
9663 NULL, NULL, (*code)->loc);
9664 add_code_to_chain (&this_code, &head, &tail);
9667 comp1 = (*code)->expr1->ts.u.derived->components;
9668 comp2 = (*code)->expr2->ts.u.derived->components;
9670 t1 = NULL;
9671 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9673 bool inout = false;
9675 /* The intrinsic assignment does the right thing for pointers
9676 of all kinds and allocatable components. */
9677 if (comp1->ts.type != BT_DERIVED
9678 || comp1->attr.pointer
9679 || comp1->attr.allocatable
9680 || comp1->attr.proc_pointer_comp
9681 || comp1->attr.class_pointer
9682 || comp1->attr.proc_pointer)
9683 continue;
9685 /* Make an assigment for this component. */
9686 this_code = build_assignment (EXEC_ASSIGN,
9687 (*code)->expr1, (*code)->expr2,
9688 comp1, comp2, (*code)->loc);
9690 /* Convert the assignment if there is a defined assignment for
9691 this type. Otherwise, using the call from gfc_resolve_code,
9692 recurse into its components. */
9693 gfc_resolve_code (this_code, ns);
9695 if (this_code->op == EXEC_ASSIGN_CALL)
9697 gfc_formal_arglist *dummy_args;
9698 gfc_symbol *rsym;
9699 /* Check that there is a typebound defined assignment. If not,
9700 then this must be a module defined assignment. We cannot
9701 use the defined_assign_comp attribute here because it must
9702 be this derived type that has the defined assignment and not
9703 a parent type. */
9704 if (!(comp1->ts.u.derived->f2k_derived
9705 && comp1->ts.u.derived->f2k_derived
9706 ->tb_op[INTRINSIC_ASSIGN]))
9708 gfc_free_statements (this_code);
9709 this_code = NULL;
9710 continue;
9713 /* If the first argument of the subroutine has intent INOUT
9714 a temporary must be generated and used instead. */
9715 rsym = this_code->resolved_sym;
9716 dummy_args = gfc_sym_get_dummy_args (rsym);
9717 if (dummy_args
9718 && dummy_args->sym->attr.intent == INTENT_INOUT)
9720 gfc_code *temp_code;
9721 inout = true;
9723 /* Build the temporary required for the assignment and put
9724 it at the head of the generated code. */
9725 if (!t1)
9727 t1 = get_temp_from_expr ((*code)->expr1, ns);
9728 temp_code = build_assignment (EXEC_ASSIGN,
9729 t1, (*code)->expr1,
9730 NULL, NULL, (*code)->loc);
9732 /* For allocatable LHS, check whether it is allocated. Note
9733 that allocatable components with defined assignment are
9734 not yet support. See PR 57696. */
9735 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
9737 gfc_code *block;
9738 gfc_expr *e =
9739 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9740 block = gfc_get_code (EXEC_IF);
9741 block->block = gfc_get_code (EXEC_IF);
9742 block->block->expr1
9743 = gfc_build_intrinsic_call (ns,
9744 GFC_ISYM_ALLOCATED, "allocated",
9745 (*code)->loc, 1, e);
9746 block->block->next = temp_code;
9747 temp_code = block;
9749 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9752 /* Replace the first actual arg with the component of the
9753 temporary. */
9754 gfc_free_expr (this_code->ext.actual->expr);
9755 this_code->ext.actual->expr = gfc_copy_expr (t1);
9756 add_comp_ref (this_code->ext.actual->expr, comp1);
9758 /* If the LHS variable is allocatable and wasn't allocated and
9759 the temporary is allocatable, pointer assign the address of
9760 the freshly allocated LHS to the temporary. */
9761 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9762 && gfc_expr_attr ((*code)->expr1).allocatable)
9764 gfc_code *block;
9765 gfc_expr *cond;
9767 cond = gfc_get_expr ();
9768 cond->ts.type = BT_LOGICAL;
9769 cond->ts.kind = gfc_default_logical_kind;
9770 cond->expr_type = EXPR_OP;
9771 cond->where = (*code)->loc;
9772 cond->value.op.op = INTRINSIC_NOT;
9773 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
9774 GFC_ISYM_ALLOCATED, "allocated",
9775 (*code)->loc, 1, gfc_copy_expr (t1));
9776 block = gfc_get_code (EXEC_IF);
9777 block->block = gfc_get_code (EXEC_IF);
9778 block->block->expr1 = cond;
9779 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9780 t1, (*code)->expr1,
9781 NULL, NULL, (*code)->loc);
9782 add_code_to_chain (&block, &head, &tail);
9786 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9788 /* Don't add intrinsic assignments since they are already
9789 effected by the intrinsic assignment of the structure. */
9790 gfc_free_statements (this_code);
9791 this_code = NULL;
9792 continue;
9795 add_code_to_chain (&this_code, &head, &tail);
9797 if (t1 && inout)
9799 /* Transfer the value to the final result. */
9800 this_code = build_assignment (EXEC_ASSIGN,
9801 (*code)->expr1, t1,
9802 comp1, comp2, (*code)->loc);
9803 add_code_to_chain (&this_code, &head, &tail);
9807 /* Put the temporary assignments at the top of the generated code. */
9808 if (tmp_head && component_assignment_level == 1)
9810 gfc_append_code (tmp_head, head);
9811 head = tmp_head;
9812 tmp_head = tmp_tail = NULL;
9815 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9816 // not accidentally deallocated. Hence, nullify t1.
9817 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9818 && gfc_expr_attr ((*code)->expr1).allocatable)
9820 gfc_code *block;
9821 gfc_expr *cond;
9822 gfc_expr *e;
9824 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9825 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9826 (*code)->loc, 2, gfc_copy_expr (t1), e);
9827 block = gfc_get_code (EXEC_IF);
9828 block->block = gfc_get_code (EXEC_IF);
9829 block->block->expr1 = cond;
9830 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9831 t1, gfc_get_null_expr (&(*code)->loc),
9832 NULL, NULL, (*code)->loc);
9833 gfc_append_code (tail, block);
9834 tail = block;
9837 /* Now attach the remaining code chain to the input code. Step on
9838 to the end of the new code since resolution is complete. */
9839 gcc_assert ((*code)->op == EXEC_ASSIGN);
9840 tail->next = (*code)->next;
9841 /* Overwrite 'code' because this would place the intrinsic assignment
9842 before the temporary for the lhs is created. */
9843 gfc_free_expr ((*code)->expr1);
9844 gfc_free_expr ((*code)->expr2);
9845 **code = *head;
9846 if (head != tail)
9847 free (head);
9848 *code = tail;
9850 component_assignment_level--;
9854 /* Given a block of code, recursively resolve everything pointed to by this
9855 code block. */
9857 void
9858 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
9860 int omp_workshare_save;
9861 int forall_save, do_concurrent_save;
9862 code_stack frame;
9863 bool t;
9865 frame.prev = cs_base;
9866 frame.head = code;
9867 cs_base = &frame;
9869 find_reachable_labels (code);
9871 for (; code; code = code->next)
9873 frame.current = code;
9874 forall_save = forall_flag;
9875 do_concurrent_save = gfc_do_concurrent_flag;
9877 if (code->op == EXEC_FORALL)
9879 forall_flag = 1;
9880 gfc_resolve_forall (code, ns, forall_save);
9881 forall_flag = 2;
9883 else if (code->block)
9885 omp_workshare_save = -1;
9886 switch (code->op)
9888 case EXEC_OMP_PARALLEL_WORKSHARE:
9889 omp_workshare_save = omp_workshare_flag;
9890 omp_workshare_flag = 1;
9891 gfc_resolve_omp_parallel_blocks (code, ns);
9892 break;
9893 case EXEC_OMP_PARALLEL:
9894 case EXEC_OMP_PARALLEL_DO:
9895 case EXEC_OMP_PARALLEL_DO_SIMD:
9896 case EXEC_OMP_PARALLEL_SECTIONS:
9897 case EXEC_OMP_TARGET_TEAMS:
9898 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9899 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9900 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9901 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9902 case EXEC_OMP_TASK:
9903 case EXEC_OMP_TEAMS:
9904 case EXEC_OMP_TEAMS_DISTRIBUTE:
9905 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9906 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9907 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9908 omp_workshare_save = omp_workshare_flag;
9909 omp_workshare_flag = 0;
9910 gfc_resolve_omp_parallel_blocks (code, ns);
9911 break;
9912 case EXEC_OMP_DISTRIBUTE:
9913 case EXEC_OMP_DISTRIBUTE_SIMD:
9914 case EXEC_OMP_DO:
9915 case EXEC_OMP_DO_SIMD:
9916 case EXEC_OMP_SIMD:
9917 gfc_resolve_omp_do_blocks (code, ns);
9918 break;
9919 case EXEC_SELECT_TYPE:
9920 /* Blocks are handled in resolve_select_type because we have
9921 to transform the SELECT TYPE into ASSOCIATE first. */
9922 break;
9923 case EXEC_DO_CONCURRENT:
9924 gfc_do_concurrent_flag = 1;
9925 gfc_resolve_blocks (code->block, ns);
9926 gfc_do_concurrent_flag = 2;
9927 break;
9928 case EXEC_OMP_WORKSHARE:
9929 omp_workshare_save = omp_workshare_flag;
9930 omp_workshare_flag = 1;
9931 /* FALL THROUGH */
9932 default:
9933 gfc_resolve_blocks (code->block, ns);
9934 break;
9937 if (omp_workshare_save != -1)
9938 omp_workshare_flag = omp_workshare_save;
9941 t = true;
9942 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9943 t = gfc_resolve_expr (code->expr1);
9944 forall_flag = forall_save;
9945 gfc_do_concurrent_flag = do_concurrent_save;
9947 if (!gfc_resolve_expr (code->expr2))
9948 t = false;
9950 if (code->op == EXEC_ALLOCATE
9951 && !gfc_resolve_expr (code->expr3))
9952 t = false;
9954 switch (code->op)
9956 case EXEC_NOP:
9957 case EXEC_END_BLOCK:
9958 case EXEC_END_NESTED_BLOCK:
9959 case EXEC_CYCLE:
9960 case EXEC_PAUSE:
9961 case EXEC_STOP:
9962 case EXEC_ERROR_STOP:
9963 case EXEC_EXIT:
9964 case EXEC_CONTINUE:
9965 case EXEC_DT_END:
9966 case EXEC_ASSIGN_CALL:
9967 break;
9969 case EXEC_CRITICAL:
9970 resolve_critical (code);
9971 break;
9973 case EXEC_SYNC_ALL:
9974 case EXEC_SYNC_IMAGES:
9975 case EXEC_SYNC_MEMORY:
9976 resolve_sync (code);
9977 break;
9979 case EXEC_LOCK:
9980 case EXEC_UNLOCK:
9981 resolve_lock_unlock (code);
9982 break;
9984 case EXEC_ENTRY:
9985 /* Keep track of which entry we are up to. */
9986 current_entry_id = code->ext.entry->id;
9987 break;
9989 case EXEC_WHERE:
9990 resolve_where (code, NULL);
9991 break;
9993 case EXEC_GOTO:
9994 if (code->expr1 != NULL)
9996 if (code->expr1->ts.type != BT_INTEGER)
9997 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9998 "INTEGER variable", &code->expr1->where);
9999 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10000 gfc_error ("Variable '%s' has not been assigned a target "
10001 "label at %L", code->expr1->symtree->n.sym->name,
10002 &code->expr1->where);
10004 else
10005 resolve_branch (code->label1, code);
10006 break;
10008 case EXEC_RETURN:
10009 if (code->expr1 != NULL
10010 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10011 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10012 "INTEGER return specifier", &code->expr1->where);
10013 break;
10015 case EXEC_INIT_ASSIGN:
10016 case EXEC_END_PROCEDURE:
10017 break;
10019 case EXEC_ASSIGN:
10020 if (!t)
10021 break;
10023 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10024 the LHS. */
10025 if (code->expr1->expr_type == EXPR_FUNCTION
10026 && code->expr1->value.function.isym
10027 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10028 remove_caf_get_intrinsic (code->expr1);
10030 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10031 _("assignment")))
10032 break;
10034 if (resolve_ordinary_assign (code, ns))
10036 if (code->op == EXEC_COMPCALL)
10037 goto compcall;
10038 else
10039 goto call;
10042 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10043 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10044 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10045 generate_component_assignments (&code, ns);
10047 break;
10049 case EXEC_LABEL_ASSIGN:
10050 if (code->label1->defined == ST_LABEL_UNKNOWN)
10051 gfc_error ("Label %d referenced at %L is never defined",
10052 code->label1->value, &code->label1->where);
10053 if (t
10054 && (code->expr1->expr_type != EXPR_VARIABLE
10055 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10056 || code->expr1->symtree->n.sym->ts.kind
10057 != gfc_default_integer_kind
10058 || code->expr1->symtree->n.sym->as != NULL))
10059 gfc_error ("ASSIGN statement at %L requires a scalar "
10060 "default INTEGER variable", &code->expr1->where);
10061 break;
10063 case EXEC_POINTER_ASSIGN:
10065 gfc_expr* e;
10067 if (!t)
10068 break;
10070 /* This is both a variable definition and pointer assignment
10071 context, so check both of them. For rank remapping, a final
10072 array ref may be present on the LHS and fool gfc_expr_attr
10073 used in gfc_check_vardef_context. Remove it. */
10074 e = remove_last_array_ref (code->expr1);
10075 t = gfc_check_vardef_context (e, true, false, false,
10076 _("pointer assignment"));
10077 if (t)
10078 t = gfc_check_vardef_context (e, false, false, false,
10079 _("pointer assignment"));
10080 gfc_free_expr (e);
10081 if (!t)
10082 break;
10084 gfc_check_pointer_assign (code->expr1, code->expr2);
10085 break;
10088 case EXEC_ARITHMETIC_IF:
10089 if (t
10090 && code->expr1->ts.type != BT_INTEGER
10091 && code->expr1->ts.type != BT_REAL)
10092 gfc_error ("Arithmetic IF statement at %L requires a numeric "
10093 "expression", &code->expr1->where);
10095 resolve_branch (code->label1, code);
10096 resolve_branch (code->label2, code);
10097 resolve_branch (code->label3, code);
10098 break;
10100 case EXEC_IF:
10101 if (t && code->expr1 != NULL
10102 && (code->expr1->ts.type != BT_LOGICAL
10103 || code->expr1->rank != 0))
10104 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10105 &code->expr1->where);
10106 break;
10108 case EXEC_CALL:
10109 call:
10110 resolve_call (code);
10111 break;
10113 case EXEC_COMPCALL:
10114 compcall:
10115 resolve_typebound_subroutine (code);
10116 break;
10118 case EXEC_CALL_PPC:
10119 resolve_ppc_call (code);
10120 break;
10122 case EXEC_SELECT:
10123 /* Select is complicated. Also, a SELECT construct could be
10124 a transformed computed GOTO. */
10125 resolve_select (code, false);
10126 break;
10128 case EXEC_SELECT_TYPE:
10129 resolve_select_type (code, ns);
10130 break;
10132 case EXEC_BLOCK:
10133 resolve_block_construct (code);
10134 break;
10136 case EXEC_DO:
10137 if (code->ext.iterator != NULL)
10139 gfc_iterator *iter = code->ext.iterator;
10140 if (gfc_resolve_iterator (iter, true, false))
10141 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10143 break;
10145 case EXEC_DO_WHILE:
10146 if (code->expr1 == NULL)
10147 gfc_internal_error ("gfc_resolve_code(): No expression on "
10148 "DO WHILE");
10149 if (t
10150 && (code->expr1->rank != 0
10151 || code->expr1->ts.type != BT_LOGICAL))
10152 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10153 "a scalar LOGICAL expression", &code->expr1->where);
10154 break;
10156 case EXEC_ALLOCATE:
10157 if (t)
10158 resolve_allocate_deallocate (code, "ALLOCATE");
10160 break;
10162 case EXEC_DEALLOCATE:
10163 if (t)
10164 resolve_allocate_deallocate (code, "DEALLOCATE");
10166 break;
10168 case EXEC_OPEN:
10169 if (!gfc_resolve_open (code->ext.open))
10170 break;
10172 resolve_branch (code->ext.open->err, code);
10173 break;
10175 case EXEC_CLOSE:
10176 if (!gfc_resolve_close (code->ext.close))
10177 break;
10179 resolve_branch (code->ext.close->err, code);
10180 break;
10182 case EXEC_BACKSPACE:
10183 case EXEC_ENDFILE:
10184 case EXEC_REWIND:
10185 case EXEC_FLUSH:
10186 if (!gfc_resolve_filepos (code->ext.filepos))
10187 break;
10189 resolve_branch (code->ext.filepos->err, code);
10190 break;
10192 case EXEC_INQUIRE:
10193 if (!gfc_resolve_inquire (code->ext.inquire))
10194 break;
10196 resolve_branch (code->ext.inquire->err, code);
10197 break;
10199 case EXEC_IOLENGTH:
10200 gcc_assert (code->ext.inquire != NULL);
10201 if (!gfc_resolve_inquire (code->ext.inquire))
10202 break;
10204 resolve_branch (code->ext.inquire->err, code);
10205 break;
10207 case EXEC_WAIT:
10208 if (!gfc_resolve_wait (code->ext.wait))
10209 break;
10211 resolve_branch (code->ext.wait->err, code);
10212 resolve_branch (code->ext.wait->end, code);
10213 resolve_branch (code->ext.wait->eor, code);
10214 break;
10216 case EXEC_READ:
10217 case EXEC_WRITE:
10218 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10219 break;
10221 resolve_branch (code->ext.dt->err, code);
10222 resolve_branch (code->ext.dt->end, code);
10223 resolve_branch (code->ext.dt->eor, code);
10224 break;
10226 case EXEC_TRANSFER:
10227 resolve_transfer (code);
10228 break;
10230 case EXEC_DO_CONCURRENT:
10231 case EXEC_FORALL:
10232 resolve_forall_iterators (code->ext.forall_iterator);
10234 if (code->expr1 != NULL
10235 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10236 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10237 "expression", &code->expr1->where);
10238 break;
10240 case EXEC_OMP_ATOMIC:
10241 case EXEC_OMP_BARRIER:
10242 case EXEC_OMP_CANCEL:
10243 case EXEC_OMP_CANCELLATION_POINT:
10244 case EXEC_OMP_CRITICAL:
10245 case EXEC_OMP_FLUSH:
10246 case EXEC_OMP_DISTRIBUTE:
10247 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10248 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10249 case EXEC_OMP_DISTRIBUTE_SIMD:
10250 case EXEC_OMP_DO:
10251 case EXEC_OMP_DO_SIMD:
10252 case EXEC_OMP_MASTER:
10253 case EXEC_OMP_ORDERED:
10254 case EXEC_OMP_SECTIONS:
10255 case EXEC_OMP_SIMD:
10256 case EXEC_OMP_SINGLE:
10257 case EXEC_OMP_TARGET:
10258 case EXEC_OMP_TARGET_DATA:
10259 case EXEC_OMP_TARGET_TEAMS:
10260 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10261 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10262 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10263 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10264 case EXEC_OMP_TARGET_UPDATE:
10265 case EXEC_OMP_TASK:
10266 case EXEC_OMP_TASKGROUP:
10267 case EXEC_OMP_TASKWAIT:
10268 case EXEC_OMP_TASKYIELD:
10269 case EXEC_OMP_TEAMS:
10270 case EXEC_OMP_TEAMS_DISTRIBUTE:
10271 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10272 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10273 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10274 case EXEC_OMP_WORKSHARE:
10275 gfc_resolve_omp_directive (code, ns);
10276 break;
10278 case EXEC_OMP_PARALLEL:
10279 case EXEC_OMP_PARALLEL_DO:
10280 case EXEC_OMP_PARALLEL_DO_SIMD:
10281 case EXEC_OMP_PARALLEL_SECTIONS:
10282 case EXEC_OMP_PARALLEL_WORKSHARE:
10283 omp_workshare_save = omp_workshare_flag;
10284 omp_workshare_flag = 0;
10285 gfc_resolve_omp_directive (code, ns);
10286 omp_workshare_flag = omp_workshare_save;
10287 break;
10289 default:
10290 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10294 cs_base = frame.prev;
10298 /* Resolve initial values and make sure they are compatible with
10299 the variable. */
10301 static void
10302 resolve_values (gfc_symbol *sym)
10304 bool t;
10306 if (sym->value == NULL)
10307 return;
10309 if (sym->value->expr_type == EXPR_STRUCTURE)
10310 t= resolve_structure_cons (sym->value, 1);
10311 else
10312 t = gfc_resolve_expr (sym->value);
10314 if (!t)
10315 return;
10317 gfc_check_assign_symbol (sym, NULL, sym->value);
10321 /* Verify any BIND(C) derived types in the namespace so we can report errors
10322 for them once, rather than for each variable declared of that type. */
10324 static void
10325 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10327 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10328 && derived_sym->attr.is_bind_c == 1)
10329 verify_bind_c_derived_type (derived_sym);
10331 return;
10335 /* Verify that any binding labels used in a given namespace do not collide
10336 with the names or binding labels of any global symbols. Multiple INTERFACE
10337 for the same procedure are permitted. */
10339 static void
10340 gfc_verify_binding_labels (gfc_symbol *sym)
10342 gfc_gsymbol *gsym;
10343 const char *module;
10345 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10346 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10347 return;
10349 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10351 if (sym->module)
10352 module = sym->module;
10353 else if (sym->ns && sym->ns->proc_name
10354 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10355 module = sym->ns->proc_name->name;
10356 else if (sym->ns && sym->ns->parent
10357 && sym->ns && sym->ns->parent->proc_name
10358 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10359 module = sym->ns->parent->proc_name->name;
10360 else
10361 module = NULL;
10363 if (!gsym
10364 || (!gsym->defined
10365 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10367 if (!gsym)
10368 gsym = gfc_get_gsymbol (sym->binding_label);
10369 gsym->where = sym->declared_at;
10370 gsym->sym_name = sym->name;
10371 gsym->binding_label = sym->binding_label;
10372 gsym->ns = sym->ns;
10373 gsym->mod_name = module;
10374 if (sym->attr.function)
10375 gsym->type = GSYM_FUNCTION;
10376 else if (sym->attr.subroutine)
10377 gsym->type = GSYM_SUBROUTINE;
10378 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10379 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10380 return;
10383 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10385 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10386 "identifier as entity at %L", sym->name,
10387 sym->binding_label, &sym->declared_at, &gsym->where);
10388 /* Clear the binding label to prevent checking multiple times. */
10389 sym->binding_label = NULL;
10392 else if (sym->attr.flavor == FL_VARIABLE
10393 && (strcmp (module, gsym->mod_name) != 0
10394 || strcmp (sym->name, gsym->sym_name) != 0))
10396 /* This can only happen if the variable is defined in a module - if it
10397 isn't the same module, reject it. */
10398 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10399 "the same global identifier as entity at %L from module %s",
10400 sym->name, module, sym->binding_label,
10401 &sym->declared_at, &gsym->where, gsym->mod_name);
10402 sym->binding_label = NULL;
10404 else if ((sym->attr.function || sym->attr.subroutine)
10405 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10406 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10407 && sym != gsym->ns->proc_name
10408 && (module != gsym->mod_name
10409 || strcmp (gsym->sym_name, sym->name) != 0
10410 || (module && strcmp (module, gsym->mod_name) != 0)))
10412 /* Print an error if the procedure is defined multiple times; we have to
10413 exclude references to the same procedure via module association or
10414 multiple checks for the same procedure. */
10415 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10416 "global identifier as entity at %L", sym->name,
10417 sym->binding_label, &sym->declared_at, &gsym->where);
10418 sym->binding_label = NULL;
10423 /* Resolve an index expression. */
10425 static bool
10426 resolve_index_expr (gfc_expr *e)
10428 if (!gfc_resolve_expr (e))
10429 return false;
10431 if (!gfc_simplify_expr (e, 0))
10432 return false;
10434 if (!gfc_specification_expr (e))
10435 return false;
10437 return true;
10441 /* Resolve a charlen structure. */
10443 static bool
10444 resolve_charlen (gfc_charlen *cl)
10446 int i, k;
10447 bool saved_specification_expr;
10449 if (cl->resolved)
10450 return true;
10452 cl->resolved = 1;
10453 saved_specification_expr = specification_expr;
10454 specification_expr = true;
10456 if (cl->length_from_typespec)
10458 if (!gfc_resolve_expr (cl->length))
10460 specification_expr = saved_specification_expr;
10461 return false;
10464 if (!gfc_simplify_expr (cl->length, 0))
10466 specification_expr = saved_specification_expr;
10467 return false;
10470 else
10473 if (!resolve_index_expr (cl->length))
10475 specification_expr = saved_specification_expr;
10476 return false;
10480 /* "If the character length parameter value evaluates to a negative
10481 value, the length of character entities declared is zero." */
10482 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10484 if (warn_surprising)
10485 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10486 " the length has been set to zero",
10487 &cl->length->where, i);
10488 gfc_replace_expr (cl->length,
10489 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10492 /* Check that the character length is not too large. */
10493 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10494 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10495 && cl->length->ts.type == BT_INTEGER
10496 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10498 gfc_error ("String length at %L is too large", &cl->length->where);
10499 specification_expr = saved_specification_expr;
10500 return false;
10503 specification_expr = saved_specification_expr;
10504 return true;
10508 /* Test for non-constant shape arrays. */
10510 static bool
10511 is_non_constant_shape_array (gfc_symbol *sym)
10513 gfc_expr *e;
10514 int i;
10515 bool not_constant;
10517 not_constant = false;
10518 if (sym->as != NULL)
10520 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10521 has not been simplified; parameter array references. Do the
10522 simplification now. */
10523 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10525 e = sym->as->lower[i];
10526 if (e && (!resolve_index_expr(e)
10527 || !gfc_is_constant_expr (e)))
10528 not_constant = true;
10529 e = sym->as->upper[i];
10530 if (e && (!resolve_index_expr(e)
10531 || !gfc_is_constant_expr (e)))
10532 not_constant = true;
10535 return not_constant;
10538 /* Given a symbol and an initialization expression, add code to initialize
10539 the symbol to the function entry. */
10540 static void
10541 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10543 gfc_expr *lval;
10544 gfc_code *init_st;
10545 gfc_namespace *ns = sym->ns;
10547 /* Search for the function namespace if this is a contained
10548 function without an explicit result. */
10549 if (sym->attr.function && sym == sym->result
10550 && sym->name != sym->ns->proc_name->name)
10552 ns = ns->contained;
10553 for (;ns; ns = ns->sibling)
10554 if (strcmp (ns->proc_name->name, sym->name) == 0)
10555 break;
10558 if (ns == NULL)
10560 gfc_free_expr (init);
10561 return;
10564 /* Build an l-value expression for the result. */
10565 lval = gfc_lval_expr_from_sym (sym);
10567 /* Add the code at scope entry. */
10568 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
10569 init_st->next = ns->code;
10570 ns->code = init_st;
10572 /* Assign the default initializer to the l-value. */
10573 init_st->loc = sym->declared_at;
10574 init_st->expr1 = lval;
10575 init_st->expr2 = init;
10578 /* Assign the default initializer to a derived type variable or result. */
10580 static void
10581 apply_default_init (gfc_symbol *sym)
10583 gfc_expr *init = NULL;
10585 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10586 return;
10588 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10589 init = gfc_default_initializer (&sym->ts);
10591 if (init == NULL && sym->ts.type != BT_CLASS)
10592 return;
10594 build_init_assign (sym, init);
10595 sym->attr.referenced = 1;
10598 /* Build an initializer for a local integer, real, complex, logical, or
10599 character variable, based on the command line flags finit-local-zero,
10600 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10601 null if the symbol should not have a default initialization. */
10602 static gfc_expr *
10603 build_default_init_expr (gfc_symbol *sym)
10605 int char_len;
10606 gfc_expr *init_expr;
10607 int i;
10609 /* These symbols should never have a default initialization. */
10610 if (sym->attr.allocatable
10611 || sym->attr.external
10612 || sym->attr.dummy
10613 || sym->attr.pointer
10614 || sym->attr.in_equivalence
10615 || sym->attr.in_common
10616 || sym->attr.data
10617 || sym->module
10618 || sym->attr.cray_pointee
10619 || sym->attr.cray_pointer
10620 || sym->assoc)
10621 return NULL;
10623 /* Now we'll try to build an initializer expression. */
10624 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10625 &sym->declared_at);
10627 /* We will only initialize integers, reals, complex, logicals, and
10628 characters, and only if the corresponding command-line flags
10629 were set. Otherwise, we free init_expr and return null. */
10630 switch (sym->ts.type)
10632 case BT_INTEGER:
10633 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10634 mpz_set_si (init_expr->value.integer,
10635 gfc_option.flag_init_integer_value);
10636 else
10638 gfc_free_expr (init_expr);
10639 init_expr = NULL;
10641 break;
10643 case BT_REAL:
10644 switch (gfc_option.flag_init_real)
10646 case GFC_INIT_REAL_SNAN:
10647 init_expr->is_snan = 1;
10648 /* Fall through. */
10649 case GFC_INIT_REAL_NAN:
10650 mpfr_set_nan (init_expr->value.real);
10651 break;
10653 case GFC_INIT_REAL_INF:
10654 mpfr_set_inf (init_expr->value.real, 1);
10655 break;
10657 case GFC_INIT_REAL_NEG_INF:
10658 mpfr_set_inf (init_expr->value.real, -1);
10659 break;
10661 case GFC_INIT_REAL_ZERO:
10662 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10663 break;
10665 default:
10666 gfc_free_expr (init_expr);
10667 init_expr = NULL;
10668 break;
10670 break;
10672 case BT_COMPLEX:
10673 switch (gfc_option.flag_init_real)
10675 case GFC_INIT_REAL_SNAN:
10676 init_expr->is_snan = 1;
10677 /* Fall through. */
10678 case GFC_INIT_REAL_NAN:
10679 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10680 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10681 break;
10683 case GFC_INIT_REAL_INF:
10684 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10685 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10686 break;
10688 case GFC_INIT_REAL_NEG_INF:
10689 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10690 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10691 break;
10693 case GFC_INIT_REAL_ZERO:
10694 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10695 break;
10697 default:
10698 gfc_free_expr (init_expr);
10699 init_expr = NULL;
10700 break;
10702 break;
10704 case BT_LOGICAL:
10705 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10706 init_expr->value.logical = 0;
10707 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10708 init_expr->value.logical = 1;
10709 else
10711 gfc_free_expr (init_expr);
10712 init_expr = NULL;
10714 break;
10716 case BT_CHARACTER:
10717 /* For characters, the length must be constant in order to
10718 create a default initializer. */
10719 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10720 && sym->ts.u.cl->length
10721 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10723 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10724 init_expr->value.character.length = char_len;
10725 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10726 for (i = 0; i < char_len; i++)
10727 init_expr->value.character.string[i]
10728 = (unsigned char) gfc_option.flag_init_character_value;
10730 else
10732 gfc_free_expr (init_expr);
10733 init_expr = NULL;
10735 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10736 && sym->ts.u.cl->length && gfc_option.flag_max_stack_var_size != 0)
10738 gfc_actual_arglist *arg;
10739 init_expr = gfc_get_expr ();
10740 init_expr->where = sym->declared_at;
10741 init_expr->ts = sym->ts;
10742 init_expr->expr_type = EXPR_FUNCTION;
10743 init_expr->value.function.isym =
10744 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10745 init_expr->value.function.name = "repeat";
10746 arg = gfc_get_actual_arglist ();
10747 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10748 NULL, 1);
10749 arg->expr->value.character.string[0]
10750 = gfc_option.flag_init_character_value;
10751 arg->next = gfc_get_actual_arglist ();
10752 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10753 init_expr->value.function.actual = arg;
10755 break;
10757 default:
10758 gfc_free_expr (init_expr);
10759 init_expr = NULL;
10761 return init_expr;
10764 /* Add an initialization expression to a local variable. */
10765 static void
10766 apply_default_init_local (gfc_symbol *sym)
10768 gfc_expr *init = NULL;
10770 /* The symbol should be a variable or a function return value. */
10771 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10772 || (sym->attr.function && sym->result != sym))
10773 return;
10775 /* Try to build the initializer expression. If we can't initialize
10776 this symbol, then init will be NULL. */
10777 init = build_default_init_expr (sym);
10778 if (init == NULL)
10779 return;
10781 /* For saved variables, we don't want to add an initializer at function
10782 entry, so we just add a static initializer. Note that automatic variables
10783 are stack allocated even with -fno-automatic; we have also to exclude
10784 result variable, which are also nonstatic. */
10785 if (sym->attr.save || sym->ns->save_all
10786 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10787 && !sym->ns->proc_name->attr.recursive
10788 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10790 /* Don't clobber an existing initializer! */
10791 gcc_assert (sym->value == NULL);
10792 sym->value = init;
10793 return;
10796 build_init_assign (sym, init);
10800 /* Resolution of common features of flavors variable and procedure. */
10802 static bool
10803 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10805 gfc_array_spec *as;
10807 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10808 as = CLASS_DATA (sym)->as;
10809 else
10810 as = sym->as;
10812 /* Constraints on deferred shape variable. */
10813 if (as == NULL || as->type != AS_DEFERRED)
10815 bool pointer, allocatable, dimension;
10817 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10819 pointer = CLASS_DATA (sym)->attr.class_pointer;
10820 allocatable = CLASS_DATA (sym)->attr.allocatable;
10821 dimension = CLASS_DATA (sym)->attr.dimension;
10823 else
10825 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10826 allocatable = sym->attr.allocatable;
10827 dimension = sym->attr.dimension;
10830 if (allocatable)
10832 if (dimension && as->type != AS_ASSUMED_RANK)
10834 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10835 "shape or assumed rank", sym->name, &sym->declared_at);
10836 return false;
10838 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10839 "'%s' at %L may not be ALLOCATABLE",
10840 sym->name, &sym->declared_at))
10841 return false;
10844 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10846 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10847 "assumed rank", sym->name, &sym->declared_at);
10848 return false;
10851 else
10853 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10854 && sym->ts.type != BT_CLASS && !sym->assoc)
10856 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10857 sym->name, &sym->declared_at);
10858 return false;
10862 /* Constraints on polymorphic variables. */
10863 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10865 /* F03:C502. */
10866 if (sym->attr.class_ok
10867 && !sym->attr.select_type_temporary
10868 && !UNLIMITED_POLY (sym)
10869 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10871 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10872 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10873 &sym->declared_at);
10874 return false;
10877 /* F03:C509. */
10878 /* Assume that use associated symbols were checked in the module ns.
10879 Class-variables that are associate-names are also something special
10880 and excepted from the test. */
10881 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10883 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10884 "or pointer", sym->name, &sym->declared_at);
10885 return false;
10889 return true;
10893 /* Additional checks for symbols with flavor variable and derived
10894 type. To be called from resolve_fl_variable. */
10896 static bool
10897 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10899 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10901 /* Check to see if a derived type is blocked from being host
10902 associated by the presence of another class I symbol in the same
10903 namespace. 14.6.1.3 of the standard and the discussion on
10904 comp.lang.fortran. */
10905 if (sym->ns != sym->ts.u.derived->ns
10906 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10908 gfc_symbol *s;
10909 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10910 if (s && s->attr.generic)
10911 s = gfc_find_dt_in_generic (s);
10912 if (s && s->attr.flavor != FL_DERIVED)
10914 gfc_error ("The type '%s' cannot be host associated at %L "
10915 "because it is blocked by an incompatible object "
10916 "of the same name declared at %L",
10917 sym->ts.u.derived->name, &sym->declared_at,
10918 &s->declared_at);
10919 return false;
10923 /* 4th constraint in section 11.3: "If an object of a type for which
10924 component-initialization is specified (R429) appears in the
10925 specification-part of a module and does not have the ALLOCATABLE
10926 or POINTER attribute, the object shall have the SAVE attribute."
10928 The check for initializers is performed with
10929 gfc_has_default_initializer because gfc_default_initializer generates
10930 a hidden default for allocatable components. */
10931 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10932 && sym->ns->proc_name->attr.flavor == FL_MODULE
10933 && !sym->ns->save_all && !sym->attr.save
10934 && !sym->attr.pointer && !sym->attr.allocatable
10935 && gfc_has_default_initializer (sym->ts.u.derived)
10936 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10937 "'%s' at %L, needed due to the default "
10938 "initialization", sym->name, &sym->declared_at))
10939 return false;
10941 /* Assign default initializer. */
10942 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10943 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10945 sym->value = gfc_default_initializer (&sym->ts);
10948 return true;
10952 /* Resolve symbols with flavor variable. */
10954 static bool
10955 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10957 int no_init_flag, automatic_flag;
10958 gfc_expr *e;
10959 const char *auto_save_msg;
10960 bool saved_specification_expr;
10962 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10963 "SAVE attribute";
10965 if (!resolve_fl_var_and_proc (sym, mp_flag))
10966 return false;
10968 /* Set this flag to check that variables are parameters of all entries.
10969 This check is effected by the call to gfc_resolve_expr through
10970 is_non_constant_shape_array. */
10971 saved_specification_expr = specification_expr;
10972 specification_expr = true;
10974 if (sym->ns->proc_name
10975 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10976 || sym->ns->proc_name->attr.is_main_program)
10977 && !sym->attr.use_assoc
10978 && !sym->attr.allocatable
10979 && !sym->attr.pointer
10980 && is_non_constant_shape_array (sym))
10982 /* The shape of a main program or module array needs to be
10983 constant. */
10984 gfc_error ("The module or main program array '%s' at %L must "
10985 "have constant shape", sym->name, &sym->declared_at);
10986 specification_expr = saved_specification_expr;
10987 return false;
10990 /* Constraints on deferred type parameter. */
10991 if (sym->ts.deferred
10992 && !(sym->attr.pointer
10993 || sym->attr.allocatable
10994 || sym->attr.omp_udr_artificial_var))
10996 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10997 "requires either the pointer or allocatable attribute",
10998 sym->name, &sym->declared_at);
10999 specification_expr = saved_specification_expr;
11000 return false;
11003 if (sym->ts.type == BT_CHARACTER)
11005 /* Make sure that character string variables with assumed length are
11006 dummy arguments. */
11007 e = sym->ts.u.cl->length;
11008 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11009 && !sym->ts.deferred && !sym->attr.select_type_temporary
11010 && !sym->attr.omp_udr_artificial_var)
11012 gfc_error ("Entity with assumed character length at %L must be a "
11013 "dummy argument or a PARAMETER", &sym->declared_at);
11014 specification_expr = saved_specification_expr;
11015 return false;
11018 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11020 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11021 specification_expr = saved_specification_expr;
11022 return false;
11025 if (!gfc_is_constant_expr (e)
11026 && !(e->expr_type == EXPR_VARIABLE
11027 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11029 if (!sym->attr.use_assoc && sym->ns->proc_name
11030 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11031 || sym->ns->proc_name->attr.is_main_program))
11033 gfc_error ("'%s' at %L must have constant character length "
11034 "in this context", sym->name, &sym->declared_at);
11035 specification_expr = saved_specification_expr;
11036 return false;
11038 if (sym->attr.in_common)
11040 gfc_error ("COMMON variable '%s' at %L must have constant "
11041 "character length", sym->name, &sym->declared_at);
11042 specification_expr = saved_specification_expr;
11043 return false;
11048 if (sym->value == NULL && sym->attr.referenced)
11049 apply_default_init_local (sym); /* Try to apply a default initialization. */
11051 /* Determine if the symbol may not have an initializer. */
11052 no_init_flag = automatic_flag = 0;
11053 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11054 || sym->attr.intrinsic || sym->attr.result)
11055 no_init_flag = 1;
11056 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11057 && is_non_constant_shape_array (sym))
11059 no_init_flag = automatic_flag = 1;
11061 /* Also, they must not have the SAVE attribute.
11062 SAVE_IMPLICIT is checked below. */
11063 if (sym->as && sym->attr.codimension)
11065 int corank = sym->as->corank;
11066 sym->as->corank = 0;
11067 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11068 sym->as->corank = corank;
11070 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11072 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11073 specification_expr = saved_specification_expr;
11074 return false;
11078 /* Ensure that any initializer is simplified. */
11079 if (sym->value)
11080 gfc_simplify_expr (sym->value, 1);
11082 /* Reject illegal initializers. */
11083 if (!sym->mark && sym->value)
11085 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11086 && CLASS_DATA (sym)->attr.allocatable))
11087 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
11088 sym->name, &sym->declared_at);
11089 else if (sym->attr.external)
11090 gfc_error ("External '%s' at %L cannot have an initializer",
11091 sym->name, &sym->declared_at);
11092 else if (sym->attr.dummy
11093 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11094 gfc_error ("Dummy '%s' at %L cannot have an initializer",
11095 sym->name, &sym->declared_at);
11096 else if (sym->attr.intrinsic)
11097 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
11098 sym->name, &sym->declared_at);
11099 else if (sym->attr.result)
11100 gfc_error ("Function result '%s' at %L cannot have an initializer",
11101 sym->name, &sym->declared_at);
11102 else if (automatic_flag)
11103 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
11104 sym->name, &sym->declared_at);
11105 else
11106 goto no_init_error;
11107 specification_expr = saved_specification_expr;
11108 return false;
11111 no_init_error:
11112 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11114 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11115 specification_expr = saved_specification_expr;
11116 return res;
11119 specification_expr = saved_specification_expr;
11120 return true;
11124 /* Resolve a procedure. */
11126 static bool
11127 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11129 gfc_formal_arglist *arg;
11131 if (sym->attr.function
11132 && !resolve_fl_var_and_proc (sym, mp_flag))
11133 return false;
11135 if (sym->ts.type == BT_CHARACTER)
11137 gfc_charlen *cl = sym->ts.u.cl;
11139 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11140 && !resolve_charlen (cl))
11141 return false;
11143 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11144 && sym->attr.proc == PROC_ST_FUNCTION)
11146 gfc_error ("Character-valued statement function '%s' at %L must "
11147 "have constant length", sym->name, &sym->declared_at);
11148 return false;
11152 /* Ensure that derived type for are not of a private type. Internal
11153 module procedures are excluded by 2.2.3.3 - i.e., they are not
11154 externally accessible and can access all the objects accessible in
11155 the host. */
11156 if (!(sym->ns->parent
11157 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11158 && gfc_check_symbol_access (sym))
11160 gfc_interface *iface;
11162 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11164 if (arg->sym
11165 && arg->sym->ts.type == BT_DERIVED
11166 && !arg->sym->ts.u.derived->attr.use_assoc
11167 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11168 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
11169 "and cannot be a dummy argument"
11170 " of '%s', which is PUBLIC at %L",
11171 arg->sym->name, sym->name,
11172 &sym->declared_at))
11174 /* Stop this message from recurring. */
11175 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11176 return false;
11180 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11181 PRIVATE to the containing module. */
11182 for (iface = sym->generic; iface; iface = iface->next)
11184 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11186 if (arg->sym
11187 && arg->sym->ts.type == BT_DERIVED
11188 && !arg->sym->ts.u.derived->attr.use_assoc
11189 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11190 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11191 "PUBLIC interface '%s' at %L "
11192 "takes dummy arguments of '%s' which "
11193 "is PRIVATE", iface->sym->name,
11194 sym->name, &iface->sym->declared_at,
11195 gfc_typename(&arg->sym->ts)))
11197 /* Stop this message from recurring. */
11198 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11199 return false;
11205 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11206 && !sym->attr.proc_pointer)
11208 gfc_error ("Function '%s' at %L cannot have an initializer",
11209 sym->name, &sym->declared_at);
11210 return false;
11213 /* An external symbol may not have an initializer because it is taken to be
11214 a procedure. Exception: Procedure Pointers. */
11215 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11217 gfc_error ("External object '%s' at %L may not have an initializer",
11218 sym->name, &sym->declared_at);
11219 return false;
11222 /* An elemental function is required to return a scalar 12.7.1 */
11223 if (sym->attr.elemental && sym->attr.function && sym->as)
11225 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11226 "result", sym->name, &sym->declared_at);
11227 /* Reset so that the error only occurs once. */
11228 sym->attr.elemental = 0;
11229 return false;
11232 if (sym->attr.proc == PROC_ST_FUNCTION
11233 && (sym->attr.allocatable || sym->attr.pointer))
11235 gfc_error ("Statement function '%s' at %L may not have pointer or "
11236 "allocatable attribute", sym->name, &sym->declared_at);
11237 return false;
11240 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11241 char-len-param shall not be array-valued, pointer-valued, recursive
11242 or pure. ....snip... A character value of * may only be used in the
11243 following ways: (i) Dummy arg of procedure - dummy associates with
11244 actual length; (ii) To declare a named constant; or (iii) External
11245 function - but length must be declared in calling scoping unit. */
11246 if (sym->attr.function
11247 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11248 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11250 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11251 || (sym->attr.recursive) || (sym->attr.pure))
11253 if (sym->as && sym->as->rank)
11254 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11255 "array-valued", sym->name, &sym->declared_at);
11257 if (sym->attr.pointer)
11258 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11259 "pointer-valued", sym->name, &sym->declared_at);
11261 if (sym->attr.pure)
11262 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11263 "pure", sym->name, &sym->declared_at);
11265 if (sym->attr.recursive)
11266 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11267 "recursive", sym->name, &sym->declared_at);
11269 return false;
11272 /* Appendix B.2 of the standard. Contained functions give an
11273 error anyway. Deferred character length is an F2003 feature.
11274 Don't warn on intrinsic conversion functions, which start
11275 with two underscores. */
11276 if (!sym->attr.contained && !sym->ts.deferred
11277 && (sym->name[0] != '_' || sym->name[1] != '_'))
11278 gfc_notify_std (GFC_STD_F95_OBS,
11279 "CHARACTER(*) function '%s' at %L",
11280 sym->name, &sym->declared_at);
11283 /* F2008, C1218. */
11284 if (sym->attr.elemental)
11286 if (sym->attr.proc_pointer)
11288 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11289 sym->name, &sym->declared_at);
11290 return false;
11292 if (sym->attr.dummy)
11294 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11295 sym->name, &sym->declared_at);
11296 return false;
11300 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11302 gfc_formal_arglist *curr_arg;
11303 int has_non_interop_arg = 0;
11305 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11306 sym->common_block))
11308 /* Clear these to prevent looking at them again if there was an
11309 error. */
11310 sym->attr.is_bind_c = 0;
11311 sym->attr.is_c_interop = 0;
11312 sym->ts.is_c_interop = 0;
11314 else
11316 /* So far, no errors have been found. */
11317 sym->attr.is_c_interop = 1;
11318 sym->ts.is_c_interop = 1;
11321 curr_arg = gfc_sym_get_dummy_args (sym);
11322 while (curr_arg != NULL)
11324 /* Skip implicitly typed dummy args here. */
11325 if (curr_arg->sym->attr.implicit_type == 0)
11326 if (!gfc_verify_c_interop_param (curr_arg->sym))
11327 /* If something is found to fail, record the fact so we
11328 can mark the symbol for the procedure as not being
11329 BIND(C) to try and prevent multiple errors being
11330 reported. */
11331 has_non_interop_arg = 1;
11333 curr_arg = curr_arg->next;
11336 /* See if any of the arguments were not interoperable and if so, clear
11337 the procedure symbol to prevent duplicate error messages. */
11338 if (has_non_interop_arg != 0)
11340 sym->attr.is_c_interop = 0;
11341 sym->ts.is_c_interop = 0;
11342 sym->attr.is_bind_c = 0;
11346 if (!sym->attr.proc_pointer)
11348 if (sym->attr.save == SAVE_EXPLICIT)
11350 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11351 "in '%s' at %L", sym->name, &sym->declared_at);
11352 return false;
11354 if (sym->attr.intent)
11356 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11357 "in '%s' at %L", sym->name, &sym->declared_at);
11358 return false;
11360 if (sym->attr.subroutine && sym->attr.result)
11362 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11363 "in '%s' at %L", sym->name, &sym->declared_at);
11364 return false;
11366 if (sym->attr.external && sym->attr.function
11367 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11368 || sym->attr.contained))
11370 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11371 "in '%s' at %L", sym->name, &sym->declared_at);
11372 return false;
11374 if (strcmp ("ppr@", sym->name) == 0)
11376 gfc_error ("Procedure pointer result '%s' at %L "
11377 "is missing the pointer attribute",
11378 sym->ns->proc_name->name, &sym->declared_at);
11379 return false;
11383 return true;
11387 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11388 been defined and we now know their defined arguments, check that they fulfill
11389 the requirements of the standard for procedures used as finalizers. */
11391 static bool
11392 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11394 gfc_finalizer* list;
11395 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11396 bool result = true;
11397 bool seen_scalar = false;
11398 gfc_symbol *vtab;
11399 gfc_component *c;
11400 gfc_symbol *parent = gfc_get_derived_super_type (derived);
11402 if (parent)
11403 gfc_resolve_finalizers (parent, finalizable);
11405 /* Return early when not finalizable. Additionally, ensure that derived-type
11406 components have a their finalizables resolved. */
11407 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11409 bool has_final = false;
11410 for (c = derived->components; c; c = c->next)
11411 if (c->ts.type == BT_DERIVED
11412 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11414 bool has_final2 = false;
11415 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11416 return false; /* Error. */
11417 has_final = has_final || has_final2;
11419 if (!has_final)
11421 if (finalizable)
11422 *finalizable = false;
11423 return true;
11427 /* Walk over the list of finalizer-procedures, check them, and if any one
11428 does not fit in with the standard's definition, print an error and remove
11429 it from the list. */
11430 prev_link = &derived->f2k_derived->finalizers;
11431 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11433 gfc_formal_arglist *dummy_args;
11434 gfc_symbol* arg;
11435 gfc_finalizer* i;
11436 int my_rank;
11438 /* Skip this finalizer if we already resolved it. */
11439 if (list->proc_tree)
11441 prev_link = &(list->next);
11442 continue;
11445 /* Check this exists and is a SUBROUTINE. */
11446 if (!list->proc_sym->attr.subroutine)
11448 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11449 list->proc_sym->name, &list->where);
11450 goto error;
11453 /* We should have exactly one argument. */
11454 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11455 if (!dummy_args || dummy_args->next)
11457 gfc_error ("FINAL procedure at %L must have exactly one argument",
11458 &list->where);
11459 goto error;
11461 arg = dummy_args->sym;
11463 /* This argument must be of our type. */
11464 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11466 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11467 &arg->declared_at, derived->name);
11468 goto error;
11471 /* It must neither be a pointer nor allocatable nor optional. */
11472 if (arg->attr.pointer)
11474 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11475 &arg->declared_at);
11476 goto error;
11478 if (arg->attr.allocatable)
11480 gfc_error ("Argument of FINAL procedure at %L must not be"
11481 " ALLOCATABLE", &arg->declared_at);
11482 goto error;
11484 if (arg->attr.optional)
11486 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11487 &arg->declared_at);
11488 goto error;
11491 /* It must not be INTENT(OUT). */
11492 if (arg->attr.intent == INTENT_OUT)
11494 gfc_error ("Argument of FINAL procedure at %L must not be"
11495 " INTENT(OUT)", &arg->declared_at);
11496 goto error;
11499 /* Warn if the procedure is non-scalar and not assumed shape. */
11500 if (warn_surprising && arg->as && arg->as->rank != 0
11501 && arg->as->type != AS_ASSUMED_SHAPE)
11502 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11503 " shape argument", &arg->declared_at);
11505 /* Check that it does not match in kind and rank with a FINAL procedure
11506 defined earlier. To really loop over the *earlier* declarations,
11507 we need to walk the tail of the list as new ones were pushed at the
11508 front. */
11509 /* TODO: Handle kind parameters once they are implemented. */
11510 my_rank = (arg->as ? arg->as->rank : 0);
11511 for (i = list->next; i; i = i->next)
11513 gfc_formal_arglist *dummy_args;
11515 /* Argument list might be empty; that is an error signalled earlier,
11516 but we nevertheless continued resolving. */
11517 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11518 if (dummy_args)
11520 gfc_symbol* i_arg = dummy_args->sym;
11521 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11522 if (i_rank == my_rank)
11524 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11525 " rank (%d) as '%s'",
11526 list->proc_sym->name, &list->where, my_rank,
11527 i->proc_sym->name);
11528 goto error;
11533 /* Is this the/a scalar finalizer procedure? */
11534 if (!arg->as || arg->as->rank == 0)
11535 seen_scalar = true;
11537 /* Find the symtree for this procedure. */
11538 gcc_assert (!list->proc_tree);
11539 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11541 prev_link = &list->next;
11542 continue;
11544 /* Remove wrong nodes immediately from the list so we don't risk any
11545 troubles in the future when they might fail later expectations. */
11546 error:
11547 i = list;
11548 *prev_link = list->next;
11549 gfc_free_finalizer (i);
11550 result = false;
11553 if (result == false)
11554 return false;
11556 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11557 were nodes in the list, must have been for arrays. It is surely a good
11558 idea to have a scalar version there if there's something to finalize. */
11559 if (warn_surprising && result && !seen_scalar)
11560 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11561 " defined at %L, suggest also scalar one",
11562 derived->name, &derived->declared_at);
11564 vtab = gfc_find_derived_vtab (derived);
11565 c = vtab->ts.u.derived->components->next->next->next->next->next;
11566 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
11568 if (finalizable)
11569 *finalizable = true;
11571 return true;
11575 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11577 static bool
11578 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11579 const char* generic_name, locus where)
11581 gfc_symbol *sym1, *sym2;
11582 const char *pass1, *pass2;
11583 gfc_formal_arglist *dummy_args;
11585 gcc_assert (t1->specific && t2->specific);
11586 gcc_assert (!t1->specific->is_generic);
11587 gcc_assert (!t2->specific->is_generic);
11588 gcc_assert (t1->is_operator == t2->is_operator);
11590 sym1 = t1->specific->u.specific->n.sym;
11591 sym2 = t2->specific->u.specific->n.sym;
11593 if (sym1 == sym2)
11594 return true;
11596 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11597 if (sym1->attr.subroutine != sym2->attr.subroutine
11598 || sym1->attr.function != sym2->attr.function)
11600 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11601 " GENERIC '%s' at %L",
11602 sym1->name, sym2->name, generic_name, &where);
11603 return false;
11606 /* Determine PASS arguments. */
11607 if (t1->specific->nopass)
11608 pass1 = NULL;
11609 else if (t1->specific->pass_arg)
11610 pass1 = t1->specific->pass_arg;
11611 else
11613 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
11614 if (dummy_args)
11615 pass1 = dummy_args->sym->name;
11616 else
11617 pass1 = NULL;
11619 if (t2->specific->nopass)
11620 pass2 = NULL;
11621 else if (t2->specific->pass_arg)
11622 pass2 = t2->specific->pass_arg;
11623 else
11625 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
11626 if (dummy_args)
11627 pass2 = dummy_args->sym->name;
11628 else
11629 pass2 = NULL;
11632 /* Compare the interfaces. */
11633 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11634 NULL, 0, pass1, pass2))
11636 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11637 sym1->name, sym2->name, generic_name, &where);
11638 return false;
11641 return true;
11645 /* Worker function for resolving a generic procedure binding; this is used to
11646 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11648 The difference between those cases is finding possible inherited bindings
11649 that are overridden, as one has to look for them in tb_sym_root,
11650 tb_uop_root or tb_op, respectively. Thus the caller must already find
11651 the super-type and set p->overridden correctly. */
11653 static bool
11654 resolve_tb_generic_targets (gfc_symbol* super_type,
11655 gfc_typebound_proc* p, const char* name)
11657 gfc_tbp_generic* target;
11658 gfc_symtree* first_target;
11659 gfc_symtree* inherited;
11661 gcc_assert (p && p->is_generic);
11663 /* Try to find the specific bindings for the symtrees in our target-list. */
11664 gcc_assert (p->u.generic);
11665 for (target = p->u.generic; target; target = target->next)
11666 if (!target->specific)
11668 gfc_typebound_proc* overridden_tbp;
11669 gfc_tbp_generic* g;
11670 const char* target_name;
11672 target_name = target->specific_st->name;
11674 /* Defined for this type directly. */
11675 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11677 target->specific = target->specific_st->n.tb;
11678 goto specific_found;
11681 /* Look for an inherited specific binding. */
11682 if (super_type)
11684 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11685 true, NULL);
11687 if (inherited)
11689 gcc_assert (inherited->n.tb);
11690 target->specific = inherited->n.tb;
11691 goto specific_found;
11695 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11696 " at %L", target_name, name, &p->where);
11697 return false;
11699 /* Once we've found the specific binding, check it is not ambiguous with
11700 other specifics already found or inherited for the same GENERIC. */
11701 specific_found:
11702 gcc_assert (target->specific);
11704 /* This must really be a specific binding! */
11705 if (target->specific->is_generic)
11707 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11708 " '%s' is GENERIC, too", name, &p->where, target_name);
11709 return false;
11712 /* Check those already resolved on this type directly. */
11713 for (g = p->u.generic; g; g = g->next)
11714 if (g != target && g->specific
11715 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11716 return false;
11718 /* Check for ambiguity with inherited specific targets. */
11719 for (overridden_tbp = p->overridden; overridden_tbp;
11720 overridden_tbp = overridden_tbp->overridden)
11721 if (overridden_tbp->is_generic)
11723 for (g = overridden_tbp->u.generic; g; g = g->next)
11725 gcc_assert (g->specific);
11726 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11727 return false;
11732 /* If we attempt to "overwrite" a specific binding, this is an error. */
11733 if (p->overridden && !p->overridden->is_generic)
11735 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11736 " the same name", name, &p->where);
11737 return false;
11740 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11741 all must have the same attributes here. */
11742 first_target = p->u.generic->specific->u.specific;
11743 gcc_assert (first_target);
11744 p->subroutine = first_target->n.sym->attr.subroutine;
11745 p->function = first_target->n.sym->attr.function;
11747 return true;
11751 /* Resolve a GENERIC procedure binding for a derived type. */
11753 static bool
11754 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11756 gfc_symbol* super_type;
11758 /* Find the overridden binding if any. */
11759 st->n.tb->overridden = NULL;
11760 super_type = gfc_get_derived_super_type (derived);
11761 if (super_type)
11763 gfc_symtree* overridden;
11764 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11765 true, NULL);
11767 if (overridden && overridden->n.tb)
11768 st->n.tb->overridden = overridden->n.tb;
11771 /* Resolve using worker function. */
11772 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11776 /* Retrieve the target-procedure of an operator binding and do some checks in
11777 common for intrinsic and user-defined type-bound operators. */
11779 static gfc_symbol*
11780 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11782 gfc_symbol* target_proc;
11784 gcc_assert (target->specific && !target->specific->is_generic);
11785 target_proc = target->specific->u.specific->n.sym;
11786 gcc_assert (target_proc);
11788 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11789 if (target->specific->nopass)
11791 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11792 return NULL;
11795 return target_proc;
11799 /* Resolve a type-bound intrinsic operator. */
11801 static bool
11802 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11803 gfc_typebound_proc* p)
11805 gfc_symbol* super_type;
11806 gfc_tbp_generic* target;
11808 /* If there's already an error here, do nothing (but don't fail again). */
11809 if (p->error)
11810 return true;
11812 /* Operators should always be GENERIC bindings. */
11813 gcc_assert (p->is_generic);
11815 /* Look for an overridden binding. */
11816 super_type = gfc_get_derived_super_type (derived);
11817 if (super_type && super_type->f2k_derived)
11818 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11819 op, true, NULL);
11820 else
11821 p->overridden = NULL;
11823 /* Resolve general GENERIC properties using worker function. */
11824 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11825 goto error;
11827 /* Check the targets to be procedures of correct interface. */
11828 for (target = p->u.generic; target; target = target->next)
11830 gfc_symbol* target_proc;
11832 target_proc = get_checked_tb_operator_target (target, p->where);
11833 if (!target_proc)
11834 goto error;
11836 if (!gfc_check_operator_interface (target_proc, op, p->where))
11837 goto error;
11839 /* Add target to non-typebound operator list. */
11840 if (!target->specific->deferred && !derived->attr.use_assoc
11841 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11843 gfc_interface *head, *intr;
11844 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11845 return false;
11846 head = derived->ns->op[op];
11847 intr = gfc_get_interface ();
11848 intr->sym = target_proc;
11849 intr->where = p->where;
11850 intr->next = head;
11851 derived->ns->op[op] = intr;
11855 return true;
11857 error:
11858 p->error = 1;
11859 return false;
11863 /* Resolve a type-bound user operator (tree-walker callback). */
11865 static gfc_symbol* resolve_bindings_derived;
11866 static bool resolve_bindings_result;
11868 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11870 static void
11871 resolve_typebound_user_op (gfc_symtree* stree)
11873 gfc_symbol* super_type;
11874 gfc_tbp_generic* target;
11876 gcc_assert (stree && stree->n.tb);
11878 if (stree->n.tb->error)
11879 return;
11881 /* Operators should always be GENERIC bindings. */
11882 gcc_assert (stree->n.tb->is_generic);
11884 /* Find overridden procedure, if any. */
11885 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11886 if (super_type && super_type->f2k_derived)
11888 gfc_symtree* overridden;
11889 overridden = gfc_find_typebound_user_op (super_type, NULL,
11890 stree->name, true, NULL);
11892 if (overridden && overridden->n.tb)
11893 stree->n.tb->overridden = overridden->n.tb;
11895 else
11896 stree->n.tb->overridden = NULL;
11898 /* Resolve basically using worker function. */
11899 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11900 goto error;
11902 /* Check the targets to be functions of correct interface. */
11903 for (target = stree->n.tb->u.generic; target; target = target->next)
11905 gfc_symbol* target_proc;
11907 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11908 if (!target_proc)
11909 goto error;
11911 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11912 goto error;
11915 return;
11917 error:
11918 resolve_bindings_result = false;
11919 stree->n.tb->error = 1;
11923 /* Resolve the type-bound procedures for a derived type. */
11925 static void
11926 resolve_typebound_procedure (gfc_symtree* stree)
11928 gfc_symbol* proc;
11929 locus where;
11930 gfc_symbol* me_arg;
11931 gfc_symbol* super_type;
11932 gfc_component* comp;
11934 gcc_assert (stree);
11936 /* Undefined specific symbol from GENERIC target definition. */
11937 if (!stree->n.tb)
11938 return;
11940 if (stree->n.tb->error)
11941 return;
11943 /* If this is a GENERIC binding, use that routine. */
11944 if (stree->n.tb->is_generic)
11946 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11947 goto error;
11948 return;
11951 /* Get the target-procedure to check it. */
11952 gcc_assert (!stree->n.tb->is_generic);
11953 gcc_assert (stree->n.tb->u.specific);
11954 proc = stree->n.tb->u.specific->n.sym;
11955 where = stree->n.tb->where;
11957 /* Default access should already be resolved from the parser. */
11958 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11960 if (stree->n.tb->deferred)
11962 if (!check_proc_interface (proc, &where))
11963 goto error;
11965 else
11967 /* Check for F08:C465. */
11968 if ((!proc->attr.subroutine && !proc->attr.function)
11969 || (proc->attr.proc != PROC_MODULE
11970 && proc->attr.if_source != IFSRC_IFBODY)
11971 || proc->attr.abstract)
11973 gfc_error ("'%s' must be a module procedure or an external procedure with"
11974 " an explicit interface at %L", proc->name, &where);
11975 goto error;
11979 stree->n.tb->subroutine = proc->attr.subroutine;
11980 stree->n.tb->function = proc->attr.function;
11982 /* Find the super-type of the current derived type. We could do this once and
11983 store in a global if speed is needed, but as long as not I believe this is
11984 more readable and clearer. */
11985 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11987 /* If PASS, resolve and check arguments if not already resolved / loaded
11988 from a .mod file. */
11989 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11991 gfc_formal_arglist *dummy_args;
11993 dummy_args = gfc_sym_get_dummy_args (proc);
11994 if (stree->n.tb->pass_arg)
11996 gfc_formal_arglist *i;
11998 /* If an explicit passing argument name is given, walk the arg-list
11999 and look for it. */
12001 me_arg = NULL;
12002 stree->n.tb->pass_arg_num = 1;
12003 for (i = dummy_args; i; i = i->next)
12005 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12007 me_arg = i->sym;
12008 break;
12010 ++stree->n.tb->pass_arg_num;
12013 if (!me_arg)
12015 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
12016 " argument '%s'",
12017 proc->name, stree->n.tb->pass_arg, &where,
12018 stree->n.tb->pass_arg);
12019 goto error;
12022 else
12024 /* Otherwise, take the first one; there should in fact be at least
12025 one. */
12026 stree->n.tb->pass_arg_num = 1;
12027 if (!dummy_args)
12029 gfc_error ("Procedure '%s' with PASS at %L must have at"
12030 " least one argument", proc->name, &where);
12031 goto error;
12033 me_arg = dummy_args->sym;
12036 /* Now check that the argument-type matches and the passed-object
12037 dummy argument is generally fine. */
12039 gcc_assert (me_arg);
12041 if (me_arg->ts.type != BT_CLASS)
12043 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12044 " at %L", proc->name, &where);
12045 goto error;
12048 if (CLASS_DATA (me_arg)->ts.u.derived
12049 != resolve_bindings_derived)
12051 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12052 " the derived-type '%s'", me_arg->name, proc->name,
12053 me_arg->name, &where, resolve_bindings_derived->name);
12054 goto error;
12057 gcc_assert (me_arg->ts.type == BT_CLASS);
12058 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12060 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
12061 " scalar", proc->name, &where);
12062 goto error;
12064 if (CLASS_DATA (me_arg)->attr.allocatable)
12066 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
12067 " be ALLOCATABLE", proc->name, &where);
12068 goto error;
12070 if (CLASS_DATA (me_arg)->attr.class_pointer)
12072 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
12073 " be POINTER", proc->name, &where);
12074 goto error;
12078 /* If we are extending some type, check that we don't override a procedure
12079 flagged NON_OVERRIDABLE. */
12080 stree->n.tb->overridden = NULL;
12081 if (super_type)
12083 gfc_symtree* overridden;
12084 overridden = gfc_find_typebound_proc (super_type, NULL,
12085 stree->name, true, NULL);
12087 if (overridden)
12089 if (overridden->n.tb)
12090 stree->n.tb->overridden = overridden->n.tb;
12092 if (!gfc_check_typebound_override (stree, overridden))
12093 goto error;
12097 /* See if there's a name collision with a component directly in this type. */
12098 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12099 if (!strcmp (comp->name, stree->name))
12101 gfc_error ("Procedure '%s' at %L has the same name as a component of"
12102 " '%s'",
12103 stree->name, &where, resolve_bindings_derived->name);
12104 goto error;
12107 /* Try to find a name collision with an inherited component. */
12108 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12110 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
12111 " component of '%s'",
12112 stree->name, &where, resolve_bindings_derived->name);
12113 goto error;
12116 stree->n.tb->error = 0;
12117 return;
12119 error:
12120 resolve_bindings_result = false;
12121 stree->n.tb->error = 1;
12125 static bool
12126 resolve_typebound_procedures (gfc_symbol* derived)
12128 int op;
12129 gfc_symbol* super_type;
12131 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12132 return true;
12134 super_type = gfc_get_derived_super_type (derived);
12135 if (super_type)
12136 resolve_symbol (super_type);
12138 resolve_bindings_derived = derived;
12139 resolve_bindings_result = true;
12141 if (derived->f2k_derived->tb_sym_root)
12142 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12143 &resolve_typebound_procedure);
12145 if (derived->f2k_derived->tb_uop_root)
12146 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12147 &resolve_typebound_user_op);
12149 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12151 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12152 if (p && !resolve_typebound_intrinsic_op (derived,
12153 (gfc_intrinsic_op)op, p))
12154 resolve_bindings_result = false;
12157 return resolve_bindings_result;
12161 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12162 to give all identical derived types the same backend_decl. */
12163 static void
12164 add_dt_to_dt_list (gfc_symbol *derived)
12166 gfc_dt_list *dt_list;
12168 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12169 if (derived == dt_list->derived)
12170 return;
12172 dt_list = gfc_get_dt_list ();
12173 dt_list->next = gfc_derived_types;
12174 dt_list->derived = derived;
12175 gfc_derived_types = dt_list;
12179 /* Ensure that a derived-type is really not abstract, meaning that every
12180 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12182 static bool
12183 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12185 if (!st)
12186 return true;
12188 if (!ensure_not_abstract_walker (sub, st->left))
12189 return false;
12190 if (!ensure_not_abstract_walker (sub, st->right))
12191 return false;
12193 if (st->n.tb && st->n.tb->deferred)
12195 gfc_symtree* overriding;
12196 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12197 if (!overriding)
12198 return false;
12199 gcc_assert (overriding->n.tb);
12200 if (overriding->n.tb->deferred)
12202 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
12203 " '%s' is DEFERRED and not overridden",
12204 sub->name, &sub->declared_at, st->name);
12205 return false;
12209 return true;
12212 static bool
12213 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12215 /* The algorithm used here is to recursively travel up the ancestry of sub
12216 and for each ancestor-type, check all bindings. If any of them is
12217 DEFERRED, look it up starting from sub and see if the found (overriding)
12218 binding is not DEFERRED.
12219 This is not the most efficient way to do this, but it should be ok and is
12220 clearer than something sophisticated. */
12222 gcc_assert (ancestor && !sub->attr.abstract);
12224 if (!ancestor->attr.abstract)
12225 return true;
12227 /* Walk bindings of this ancestor. */
12228 if (ancestor->f2k_derived)
12230 bool t;
12231 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12232 if (!t)
12233 return false;
12236 /* Find next ancestor type and recurse on it. */
12237 ancestor = gfc_get_derived_super_type (ancestor);
12238 if (ancestor)
12239 return ensure_not_abstract (sub, ancestor);
12241 return true;
12245 /* This check for typebound defined assignments is done recursively
12246 since the order in which derived types are resolved is not always in
12247 order of the declarations. */
12249 static void
12250 check_defined_assignments (gfc_symbol *derived)
12252 gfc_component *c;
12254 for (c = derived->components; c; c = c->next)
12256 if (c->ts.type != BT_DERIVED
12257 || c->attr.pointer
12258 || c->attr.allocatable
12259 || c->attr.proc_pointer_comp
12260 || c->attr.class_pointer
12261 || c->attr.proc_pointer)
12262 continue;
12264 if (c->ts.u.derived->attr.defined_assign_comp
12265 || (c->ts.u.derived->f2k_derived
12266 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12268 derived->attr.defined_assign_comp = 1;
12269 return;
12272 check_defined_assignments (c->ts.u.derived);
12273 if (c->ts.u.derived->attr.defined_assign_comp)
12275 derived->attr.defined_assign_comp = 1;
12276 return;
12282 /* Resolve the components of a derived type. This does not have to wait until
12283 resolution stage, but can be done as soon as the dt declaration has been
12284 parsed. */
12286 static bool
12287 resolve_fl_derived0 (gfc_symbol *sym)
12289 gfc_symbol* super_type;
12290 gfc_component *c;
12292 if (sym->attr.unlimited_polymorphic)
12293 return true;
12295 super_type = gfc_get_derived_super_type (sym);
12297 /* F2008, C432. */
12298 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12300 gfc_error ("As extending type '%s' at %L has a coarray component, "
12301 "parent type '%s' shall also have one", sym->name,
12302 &sym->declared_at, super_type->name);
12303 return false;
12306 /* Ensure the extended type gets resolved before we do. */
12307 if (super_type && !resolve_fl_derived0 (super_type))
12308 return false;
12310 /* An ABSTRACT type must be extensible. */
12311 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12313 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12314 sym->name, &sym->declared_at);
12315 return false;
12318 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12319 : sym->components;
12321 for ( ; c != NULL; c = c->next)
12323 if (c->attr.artificial)
12324 continue;
12326 /* F2008, C442. */
12327 if ((!sym->attr.is_class || c != sym->components)
12328 && c->attr.codimension
12329 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12331 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12332 "deferred shape", c->name, &c->loc);
12333 return false;
12336 /* F2008, C443. */
12337 if (c->attr.codimension && c->ts.type == BT_DERIVED
12338 && c->ts.u.derived->ts.is_iso_c)
12340 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12341 "shall not be a coarray", c->name, &c->loc);
12342 return false;
12345 /* F2008, C444. */
12346 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12347 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12348 || c->attr.allocatable))
12350 gfc_error ("Component '%s' at %L with coarray component "
12351 "shall be a nonpointer, nonallocatable scalar",
12352 c->name, &c->loc);
12353 return false;
12356 /* F2008, C448. */
12357 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12359 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12360 "is not an array pointer", c->name, &c->loc);
12361 return false;
12364 if (c->attr.proc_pointer && c->ts.interface)
12366 gfc_symbol *ifc = c->ts.interface;
12368 if (!sym->attr.vtype
12369 && !check_proc_interface (ifc, &c->loc))
12370 return false;
12372 if (ifc->attr.if_source || ifc->attr.intrinsic)
12374 /* Resolve interface and copy attributes. */
12375 if (ifc->formal && !ifc->formal_ns)
12376 resolve_symbol (ifc);
12377 if (ifc->attr.intrinsic)
12378 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12380 if (ifc->result)
12382 c->ts = ifc->result->ts;
12383 c->attr.allocatable = ifc->result->attr.allocatable;
12384 c->attr.pointer = ifc->result->attr.pointer;
12385 c->attr.dimension = ifc->result->attr.dimension;
12386 c->as = gfc_copy_array_spec (ifc->result->as);
12387 c->attr.class_ok = ifc->result->attr.class_ok;
12389 else
12391 c->ts = ifc->ts;
12392 c->attr.allocatable = ifc->attr.allocatable;
12393 c->attr.pointer = ifc->attr.pointer;
12394 c->attr.dimension = ifc->attr.dimension;
12395 c->as = gfc_copy_array_spec (ifc->as);
12396 c->attr.class_ok = ifc->attr.class_ok;
12398 c->ts.interface = ifc;
12399 c->attr.function = ifc->attr.function;
12400 c->attr.subroutine = ifc->attr.subroutine;
12402 c->attr.pure = ifc->attr.pure;
12403 c->attr.elemental = ifc->attr.elemental;
12404 c->attr.recursive = ifc->attr.recursive;
12405 c->attr.always_explicit = ifc->attr.always_explicit;
12406 c->attr.ext_attr |= ifc->attr.ext_attr;
12407 /* Copy char length. */
12408 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12410 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12411 if (cl->length && !cl->resolved
12412 && !gfc_resolve_expr (cl->length))
12413 return false;
12414 c->ts.u.cl = cl;
12418 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12420 /* Since PPCs are not implicitly typed, a PPC without an explicit
12421 interface must be a subroutine. */
12422 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12425 /* Procedure pointer components: Check PASS arg. */
12426 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12427 && !sym->attr.vtype)
12429 gfc_symbol* me_arg;
12431 if (c->tb->pass_arg)
12433 gfc_formal_arglist* i;
12435 /* If an explicit passing argument name is given, walk the arg-list
12436 and look for it. */
12438 me_arg = NULL;
12439 c->tb->pass_arg_num = 1;
12440 for (i = c->ts.interface->formal; i; i = i->next)
12442 if (!strcmp (i->sym->name, c->tb->pass_arg))
12444 me_arg = i->sym;
12445 break;
12447 c->tb->pass_arg_num++;
12450 if (!me_arg)
12452 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12453 "at %L has no argument '%s'", c->name,
12454 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12455 c->tb->error = 1;
12456 return false;
12459 else
12461 /* Otherwise, take the first one; there should in fact be at least
12462 one. */
12463 c->tb->pass_arg_num = 1;
12464 if (!c->ts.interface->formal)
12466 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12467 "must have at least one argument",
12468 c->name, &c->loc);
12469 c->tb->error = 1;
12470 return false;
12472 me_arg = c->ts.interface->formal->sym;
12475 /* Now check that the argument-type matches. */
12476 gcc_assert (me_arg);
12477 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12478 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12479 || (me_arg->ts.type == BT_CLASS
12480 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12482 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12483 " the derived type '%s'", me_arg->name, c->name,
12484 me_arg->name, &c->loc, sym->name);
12485 c->tb->error = 1;
12486 return false;
12489 /* Check for C453. */
12490 if (me_arg->attr.dimension)
12492 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12493 "must be scalar", me_arg->name, c->name, me_arg->name,
12494 &c->loc);
12495 c->tb->error = 1;
12496 return false;
12499 if (me_arg->attr.pointer)
12501 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12502 "may not have the POINTER attribute", me_arg->name,
12503 c->name, me_arg->name, &c->loc);
12504 c->tb->error = 1;
12505 return false;
12508 if (me_arg->attr.allocatable)
12510 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12511 "may not be ALLOCATABLE", me_arg->name, c->name,
12512 me_arg->name, &c->loc);
12513 c->tb->error = 1;
12514 return false;
12517 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12518 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12519 " at %L", c->name, &c->loc);
12523 /* Check type-spec if this is not the parent-type component. */
12524 if (((sym->attr.is_class
12525 && (!sym->components->ts.u.derived->attr.extension
12526 || c != sym->components->ts.u.derived->components))
12527 || (!sym->attr.is_class
12528 && (!sym->attr.extension || c != sym->components)))
12529 && !sym->attr.vtype
12530 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12531 return false;
12533 /* If this type is an extension, set the accessibility of the parent
12534 component. */
12535 if (super_type
12536 && ((sym->attr.is_class
12537 && c == sym->components->ts.u.derived->components)
12538 || (!sym->attr.is_class && c == sym->components))
12539 && strcmp (super_type->name, c->name) == 0)
12540 c->attr.access = super_type->attr.access;
12542 /* If this type is an extension, see if this component has the same name
12543 as an inherited type-bound procedure. */
12544 if (super_type && !sym->attr.is_class
12545 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12547 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12548 " inherited type-bound procedure",
12549 c->name, sym->name, &c->loc);
12550 return false;
12553 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12554 && !c->ts.deferred)
12556 if (c->ts.u.cl->length == NULL
12557 || (!resolve_charlen(c->ts.u.cl))
12558 || !gfc_is_constant_expr (c->ts.u.cl->length))
12560 gfc_error ("Character length of component '%s' needs to "
12561 "be a constant specification expression at %L",
12562 c->name,
12563 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12564 return false;
12568 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12569 && !c->attr.pointer && !c->attr.allocatable)
12571 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12572 "length must be a POINTER or ALLOCATABLE",
12573 c->name, sym->name, &c->loc);
12574 return false;
12577 /* Add the hidden deferred length field. */
12578 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
12579 && !sym->attr.is_class)
12581 char name[GFC_MAX_SYMBOL_LEN+9];
12582 gfc_component *strlen;
12583 sprintf (name, "_%s_length", c->name);
12584 strlen = gfc_find_component (sym, name, true, true);
12585 if (strlen == NULL)
12587 if (!gfc_add_component (sym, name, &strlen))
12588 return false;
12589 strlen->ts.type = BT_INTEGER;
12590 strlen->ts.kind = gfc_charlen_int_kind;
12591 strlen->attr.access = ACCESS_PRIVATE;
12592 strlen->attr.deferred_parameter = 1;
12596 if (c->ts.type == BT_DERIVED
12597 && sym->component_access != ACCESS_PRIVATE
12598 && gfc_check_symbol_access (sym)
12599 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12600 && !c->ts.u.derived->attr.use_assoc
12601 && !gfc_check_symbol_access (c->ts.u.derived)
12602 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12603 "PRIVATE type and cannot be a component of "
12604 "'%s', which is PUBLIC at %L", c->name,
12605 sym->name, &sym->declared_at))
12606 return false;
12608 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12610 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12611 "type %s", c->name, &c->loc, sym->name);
12612 return false;
12615 if (sym->attr.sequence)
12617 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12619 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12620 "not have the SEQUENCE attribute",
12621 c->ts.u.derived->name, &sym->declared_at);
12622 return false;
12626 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12627 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12628 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12629 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12630 CLASS_DATA (c)->ts.u.derived
12631 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12633 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12634 && c->attr.pointer && c->ts.u.derived->components == NULL
12635 && !c->ts.u.derived->attr.zero_comp)
12637 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12638 "that has not been declared", c->name, sym->name,
12639 &c->loc);
12640 return false;
12643 if (c->ts.type == BT_CLASS && c->attr.class_ok
12644 && CLASS_DATA (c)->attr.class_pointer
12645 && CLASS_DATA (c)->ts.u.derived->components == NULL
12646 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12647 && !UNLIMITED_POLY (c))
12649 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12650 "that has not been declared", c->name, sym->name,
12651 &c->loc);
12652 return false;
12655 /* C437. */
12656 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12657 && (!c->attr.class_ok
12658 || !(CLASS_DATA (c)->attr.class_pointer
12659 || CLASS_DATA (c)->attr.allocatable)))
12661 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12662 "or pointer", c->name, &c->loc);
12663 /* Prevent a recurrence of the error. */
12664 c->ts.type = BT_UNKNOWN;
12665 return false;
12668 /* Ensure that all the derived type components are put on the
12669 derived type list; even in formal namespaces, where derived type
12670 pointer components might not have been declared. */
12671 if (c->ts.type == BT_DERIVED
12672 && c->ts.u.derived
12673 && c->ts.u.derived->components
12674 && c->attr.pointer
12675 && sym != c->ts.u.derived)
12676 add_dt_to_dt_list (c->ts.u.derived);
12678 if (!gfc_resolve_array_spec (c->as,
12679 !(c->attr.pointer || c->attr.proc_pointer
12680 || c->attr.allocatable)))
12681 return false;
12683 if (c->initializer && !sym->attr.vtype
12684 && !gfc_check_assign_symbol (sym, c, c->initializer))
12685 return false;
12688 check_defined_assignments (sym);
12690 if (!sym->attr.defined_assign_comp && super_type)
12691 sym->attr.defined_assign_comp
12692 = super_type->attr.defined_assign_comp;
12694 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12695 all DEFERRED bindings are overridden. */
12696 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12697 && !sym->attr.is_class
12698 && !ensure_not_abstract (sym, super_type))
12699 return false;
12701 /* Add derived type to the derived type list. */
12702 add_dt_to_dt_list (sym);
12704 return true;
12708 /* The following procedure does the full resolution of a derived type,
12709 including resolution of all type-bound procedures (if present). In contrast
12710 to 'resolve_fl_derived0' this can only be done after the module has been
12711 parsed completely. */
12713 static bool
12714 resolve_fl_derived (gfc_symbol *sym)
12716 gfc_symbol *gen_dt = NULL;
12718 if (sym->attr.unlimited_polymorphic)
12719 return true;
12721 if (!sym->attr.is_class)
12722 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12723 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12724 && (!gen_dt->generic->sym->attr.use_assoc
12725 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12726 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12727 "'%s' at %L being the same name as derived "
12728 "type at %L", sym->name,
12729 gen_dt->generic->sym == sym
12730 ? gen_dt->generic->next->sym->name
12731 : gen_dt->generic->sym->name,
12732 gen_dt->generic->sym == sym
12733 ? &gen_dt->generic->next->sym->declared_at
12734 : &gen_dt->generic->sym->declared_at,
12735 &sym->declared_at))
12736 return false;
12738 /* Resolve the finalizer procedures. */
12739 if (!gfc_resolve_finalizers (sym, NULL))
12740 return false;
12742 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12744 /* Fix up incomplete CLASS symbols. */
12745 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12746 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12748 /* Nothing more to do for unlimited polymorphic entities. */
12749 if (data->ts.u.derived->attr.unlimited_polymorphic)
12750 return true;
12751 else if (vptr->ts.u.derived == NULL)
12753 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12754 gcc_assert (vtab);
12755 vptr->ts.u.derived = vtab->ts.u.derived;
12759 if (!resolve_fl_derived0 (sym))
12760 return false;
12762 /* Resolve the type-bound procedures. */
12763 if (!resolve_typebound_procedures (sym))
12764 return false;
12766 return true;
12770 static bool
12771 resolve_fl_namelist (gfc_symbol *sym)
12773 gfc_namelist *nl;
12774 gfc_symbol *nlsym;
12776 for (nl = sym->namelist; nl; nl = nl->next)
12778 /* Check again, the check in match only works if NAMELIST comes
12779 after the decl. */
12780 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12782 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12783 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12784 return false;
12787 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12788 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12789 "with assumed shape in namelist '%s' at %L",
12790 nl->sym->name, sym->name, &sym->declared_at))
12791 return false;
12793 if (is_non_constant_shape_array (nl->sym)
12794 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12795 "with nonconstant shape in namelist '%s' at %L",
12796 nl->sym->name, sym->name, &sym->declared_at))
12797 return false;
12799 if (nl->sym->ts.type == BT_CHARACTER
12800 && (nl->sym->ts.u.cl->length == NULL
12801 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12802 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12803 "nonconstant character length in "
12804 "namelist '%s' at %L", nl->sym->name,
12805 sym->name, &sym->declared_at))
12806 return false;
12808 /* FIXME: Once UDDTIO is implemented, the following can be
12809 removed. */
12810 if (nl->sym->ts.type == BT_CLASS)
12812 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12813 "polymorphic and requires a defined input/output "
12814 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12815 return false;
12818 if (nl->sym->ts.type == BT_DERIVED
12819 && (nl->sym->ts.u.derived->attr.alloc_comp
12820 || nl->sym->ts.u.derived->attr.pointer_comp))
12822 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12823 "namelist '%s' at %L with ALLOCATABLE "
12824 "or POINTER components", nl->sym->name,
12825 sym->name, &sym->declared_at))
12826 return false;
12828 /* FIXME: Once UDDTIO is implemented, the following can be
12829 removed. */
12830 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12831 "ALLOCATABLE or POINTER components and thus requires "
12832 "a defined input/output procedure", nl->sym->name,
12833 sym->name, &sym->declared_at);
12834 return false;
12838 /* Reject PRIVATE objects in a PUBLIC namelist. */
12839 if (gfc_check_symbol_access (sym))
12841 for (nl = sym->namelist; nl; nl = nl->next)
12843 if (!nl->sym->attr.use_assoc
12844 && !is_sym_host_assoc (nl->sym, sym->ns)
12845 && !gfc_check_symbol_access (nl->sym))
12847 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12848 "cannot be member of PUBLIC namelist '%s' at %L",
12849 nl->sym->name, sym->name, &sym->declared_at);
12850 return false;
12853 /* Types with private components that came here by USE-association. */
12854 if (nl->sym->ts.type == BT_DERIVED
12855 && derived_inaccessible (nl->sym->ts.u.derived))
12857 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12858 "components and cannot be member of namelist '%s' at %L",
12859 nl->sym->name, sym->name, &sym->declared_at);
12860 return false;
12863 /* Types with private components that are defined in the same module. */
12864 if (nl->sym->ts.type == BT_DERIVED
12865 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12866 && nl->sym->ts.u.derived->attr.private_comp)
12868 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12869 "cannot be a member of PUBLIC namelist '%s' at %L",
12870 nl->sym->name, sym->name, &sym->declared_at);
12871 return false;
12877 /* 14.1.2 A module or internal procedure represent local entities
12878 of the same type as a namelist member and so are not allowed. */
12879 for (nl = sym->namelist; nl; nl = nl->next)
12881 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12882 continue;
12884 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12885 if ((nl->sym == sym->ns->proc_name)
12887 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12888 continue;
12890 nlsym = NULL;
12891 if (nl->sym->name)
12892 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12893 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12895 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12896 "attribute in '%s' at %L", nlsym->name,
12897 &sym->declared_at);
12898 return false;
12902 return true;
12906 static bool
12907 resolve_fl_parameter (gfc_symbol *sym)
12909 /* A parameter array's shape needs to be constant. */
12910 if (sym->as != NULL
12911 && (sym->as->type == AS_DEFERRED
12912 || is_non_constant_shape_array (sym)))
12914 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12915 "or of deferred shape", sym->name, &sym->declared_at);
12916 return false;
12919 /* Make sure a parameter that has been implicitly typed still
12920 matches the implicit type, since PARAMETER statements can precede
12921 IMPLICIT statements. */
12922 if (sym->attr.implicit_type
12923 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12924 sym->ns)))
12926 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12927 "later IMPLICIT type", sym->name, &sym->declared_at);
12928 return false;
12931 /* Make sure the types of derived parameters are consistent. This
12932 type checking is deferred until resolution because the type may
12933 refer to a derived type from the host. */
12934 if (sym->ts.type == BT_DERIVED
12935 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12937 gfc_error ("Incompatible derived type in PARAMETER at %L",
12938 &sym->value->where);
12939 return false;
12941 return true;
12945 /* Do anything necessary to resolve a symbol. Right now, we just
12946 assume that an otherwise unknown symbol is a variable. This sort
12947 of thing commonly happens for symbols in module. */
12949 static void
12950 resolve_symbol (gfc_symbol *sym)
12952 int check_constant, mp_flag;
12953 gfc_symtree *symtree;
12954 gfc_symtree *this_symtree;
12955 gfc_namespace *ns;
12956 gfc_component *c;
12957 symbol_attribute class_attr;
12958 gfc_array_spec *as;
12959 bool saved_specification_expr;
12961 if (sym->resolved)
12962 return;
12963 sym->resolved = 1;
12965 if (sym->attr.artificial)
12966 return;
12968 if (sym->attr.unlimited_polymorphic)
12969 return;
12971 if (sym->attr.flavor == FL_UNKNOWN
12972 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12973 && !sym->attr.generic && !sym->attr.external
12974 && sym->attr.if_source == IFSRC_UNKNOWN
12975 && sym->ts.type == BT_UNKNOWN))
12978 /* If we find that a flavorless symbol is an interface in one of the
12979 parent namespaces, find its symtree in this namespace, free the
12980 symbol and set the symtree to point to the interface symbol. */
12981 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12983 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12984 if (symtree && (symtree->n.sym->generic ||
12985 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12986 && sym->ns->construct_entities)))
12988 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12989 sym->name);
12990 gfc_release_symbol (sym);
12991 symtree->n.sym->refs++;
12992 this_symtree->n.sym = symtree->n.sym;
12993 return;
12997 /* Otherwise give it a flavor according to such attributes as
12998 it has. */
12999 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13000 && sym->attr.intrinsic == 0)
13001 sym->attr.flavor = FL_VARIABLE;
13002 else if (sym->attr.flavor == FL_UNKNOWN)
13004 sym->attr.flavor = FL_PROCEDURE;
13005 if (sym->attr.dimension)
13006 sym->attr.function = 1;
13010 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13011 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13013 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13014 && !resolve_procedure_interface (sym))
13015 return;
13017 if (sym->attr.is_protected && !sym->attr.proc_pointer
13018 && (sym->attr.procedure || sym->attr.external))
13020 if (sym->attr.external)
13021 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13022 "at %L", &sym->declared_at);
13023 else
13024 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13025 "at %L", &sym->declared_at);
13027 return;
13030 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13031 return;
13033 /* Symbols that are module procedures with results (functions) have
13034 the types and array specification copied for type checking in
13035 procedures that call them, as well as for saving to a module
13036 file. These symbols can't stand the scrutiny that their results
13037 can. */
13038 mp_flag = (sym->result != NULL && sym->result != sym);
13040 /* Make sure that the intrinsic is consistent with its internal
13041 representation. This needs to be done before assigning a default
13042 type to avoid spurious warnings. */
13043 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13044 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13045 return;
13047 /* Resolve associate names. */
13048 if (sym->assoc)
13049 resolve_assoc_var (sym, true);
13051 /* Assign default type to symbols that need one and don't have one. */
13052 if (sym->ts.type == BT_UNKNOWN)
13054 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13056 gfc_set_default_type (sym, 1, NULL);
13059 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13060 && !sym->attr.function && !sym->attr.subroutine
13061 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13062 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13064 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13066 /* The specific case of an external procedure should emit an error
13067 in the case that there is no implicit type. */
13068 if (!mp_flag)
13069 gfc_set_default_type (sym, sym->attr.external, NULL);
13070 else
13072 /* Result may be in another namespace. */
13073 resolve_symbol (sym->result);
13075 if (!sym->result->attr.proc_pointer)
13077 sym->ts = sym->result->ts;
13078 sym->as = gfc_copy_array_spec (sym->result->as);
13079 sym->attr.dimension = sym->result->attr.dimension;
13080 sym->attr.pointer = sym->result->attr.pointer;
13081 sym->attr.allocatable = sym->result->attr.allocatable;
13082 sym->attr.contiguous = sym->result->attr.contiguous;
13087 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13089 bool saved_specification_expr = specification_expr;
13090 specification_expr = true;
13091 gfc_resolve_array_spec (sym->result->as, false);
13092 specification_expr = saved_specification_expr;
13095 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13097 as = CLASS_DATA (sym)->as;
13098 class_attr = CLASS_DATA (sym)->attr;
13099 class_attr.pointer = class_attr.class_pointer;
13101 else
13103 class_attr = sym->attr;
13104 as = sym->as;
13107 /* F2008, C530. */
13108 if (sym->attr.contiguous
13109 && (!class_attr.dimension
13110 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13111 && !class_attr.pointer)))
13113 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
13114 "array pointer or an assumed-shape or assumed-rank array",
13115 sym->name, &sym->declared_at);
13116 return;
13119 /* Assumed size arrays and assumed shape arrays must be dummy
13120 arguments. Array-spec's of implied-shape should have been resolved to
13121 AS_EXPLICIT already. */
13123 if (as)
13125 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13126 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13127 || as->type == AS_ASSUMED_SHAPE)
13128 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13130 if (as->type == AS_ASSUMED_SIZE)
13131 gfc_error ("Assumed size array at %L must be a dummy argument",
13132 &sym->declared_at);
13133 else
13134 gfc_error ("Assumed shape array at %L must be a dummy argument",
13135 &sym->declared_at);
13136 return;
13138 /* TS 29113, C535a. */
13139 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13140 && !sym->attr.select_type_temporary)
13142 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13143 &sym->declared_at);
13144 return;
13146 if (as->type == AS_ASSUMED_RANK
13147 && (sym->attr.codimension || sym->attr.value))
13149 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13150 "CODIMENSION attribute", &sym->declared_at);
13151 return;
13155 /* Make sure symbols with known intent or optional are really dummy
13156 variable. Because of ENTRY statement, this has to be deferred
13157 until resolution time. */
13159 if (!sym->attr.dummy
13160 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13162 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13163 return;
13166 if (sym->attr.value && !sym->attr.dummy)
13168 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
13169 "it is not a dummy argument", sym->name, &sym->declared_at);
13170 return;
13173 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13175 gfc_charlen *cl = sym->ts.u.cl;
13176 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13178 gfc_error ("Character dummy variable '%s' at %L with VALUE "
13179 "attribute must have constant length",
13180 sym->name, &sym->declared_at);
13181 return;
13184 if (sym->ts.is_c_interop
13185 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13187 gfc_error ("C interoperable character dummy variable '%s' at %L "
13188 "with VALUE attribute must have length one",
13189 sym->name, &sym->declared_at);
13190 return;
13194 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13195 && sym->ts.u.derived->attr.generic)
13197 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13198 if (!sym->ts.u.derived)
13200 gfc_error ("The derived type '%s' at %L is of type '%s', "
13201 "which has not been defined", sym->name,
13202 &sym->declared_at, sym->ts.u.derived->name);
13203 sym->ts.type = BT_UNKNOWN;
13204 return;
13208 /* Use the same constraints as TYPE(*), except for the type check
13209 and that only scalars and assumed-size arrays are permitted. */
13210 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13212 if (!sym->attr.dummy)
13214 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13215 "a dummy argument", sym->name, &sym->declared_at);
13216 return;
13219 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13220 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13221 && sym->ts.type != BT_COMPLEX)
13223 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13224 "of type TYPE(*) or of an numeric intrinsic type",
13225 sym->name, &sym->declared_at);
13226 return;
13229 if (sym->attr.allocatable || sym->attr.codimension
13230 || sym->attr.pointer || sym->attr.value)
13232 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13233 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13234 "attribute", sym->name, &sym->declared_at);
13235 return;
13238 if (sym->attr.intent == INTENT_OUT)
13240 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13241 "have the INTENT(OUT) attribute",
13242 sym->name, &sym->declared_at);
13243 return;
13245 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13247 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13248 "either be a scalar or an assumed-size array",
13249 sym->name, &sym->declared_at);
13250 return;
13253 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13254 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13255 packing. */
13256 sym->ts.type = BT_ASSUMED;
13257 sym->as = gfc_get_array_spec ();
13258 sym->as->type = AS_ASSUMED_SIZE;
13259 sym->as->rank = 1;
13260 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13262 else if (sym->ts.type == BT_ASSUMED)
13264 /* TS 29113, C407a. */
13265 if (!sym->attr.dummy)
13267 gfc_error ("Assumed type of variable %s at %L is only permitted "
13268 "for dummy variables", sym->name, &sym->declared_at);
13269 return;
13271 if (sym->attr.allocatable || sym->attr.codimension
13272 || sym->attr.pointer || sym->attr.value)
13274 gfc_error ("Assumed-type variable %s at %L may not have the "
13275 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13276 sym->name, &sym->declared_at);
13277 return;
13279 if (sym->attr.intent == INTENT_OUT)
13281 gfc_error ("Assumed-type variable %s at %L may not have the "
13282 "INTENT(OUT) attribute",
13283 sym->name, &sym->declared_at);
13284 return;
13286 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13288 gfc_error ("Assumed-type variable %s at %L shall not be an "
13289 "explicit-shape array", sym->name, &sym->declared_at);
13290 return;
13294 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13295 do this for something that was implicitly typed because that is handled
13296 in gfc_set_default_type. Handle dummy arguments and procedure
13297 definitions separately. Also, anything that is use associated is not
13298 handled here but instead is handled in the module it is declared in.
13299 Finally, derived type definitions are allowed to be BIND(C) since that
13300 only implies that they're interoperable, and they are checked fully for
13301 interoperability when a variable is declared of that type. */
13302 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13303 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13304 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13306 bool t = true;
13308 /* First, make sure the variable is declared at the
13309 module-level scope (J3/04-007, Section 15.3). */
13310 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13311 sym->attr.in_common == 0)
13313 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13314 "is neither a COMMON block nor declared at the "
13315 "module level scope", sym->name, &(sym->declared_at));
13316 t = false;
13318 else if (sym->common_head != NULL)
13320 t = verify_com_block_vars_c_interop (sym->common_head);
13322 else
13324 /* If type() declaration, we need to verify that the components
13325 of the given type are all C interoperable, etc. */
13326 if (sym->ts.type == BT_DERIVED &&
13327 sym->ts.u.derived->attr.is_c_interop != 1)
13329 /* Make sure the user marked the derived type as BIND(C). If
13330 not, call the verify routine. This could print an error
13331 for the derived type more than once if multiple variables
13332 of that type are declared. */
13333 if (sym->ts.u.derived->attr.is_bind_c != 1)
13334 verify_bind_c_derived_type (sym->ts.u.derived);
13335 t = false;
13338 /* Verify the variable itself as C interoperable if it
13339 is BIND(C). It is not possible for this to succeed if
13340 the verify_bind_c_derived_type failed, so don't have to handle
13341 any error returned by verify_bind_c_derived_type. */
13342 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13343 sym->common_block);
13346 if (!t)
13348 /* clear the is_bind_c flag to prevent reporting errors more than
13349 once if something failed. */
13350 sym->attr.is_bind_c = 0;
13351 return;
13355 /* If a derived type symbol has reached this point, without its
13356 type being declared, we have an error. Notice that most
13357 conditions that produce undefined derived types have already
13358 been dealt with. However, the likes of:
13359 implicit type(t) (t) ..... call foo (t) will get us here if
13360 the type is not declared in the scope of the implicit
13361 statement. Change the type to BT_UNKNOWN, both because it is so
13362 and to prevent an ICE. */
13363 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13364 && sym->ts.u.derived->components == NULL
13365 && !sym->ts.u.derived->attr.zero_comp)
13367 gfc_error ("The derived type '%s' at %L is of type '%s', "
13368 "which has not been defined", sym->name,
13369 &sym->declared_at, sym->ts.u.derived->name);
13370 sym->ts.type = BT_UNKNOWN;
13371 return;
13374 /* Make sure that the derived type has been resolved and that the
13375 derived type is visible in the symbol's namespace, if it is a
13376 module function and is not PRIVATE. */
13377 if (sym->ts.type == BT_DERIVED
13378 && sym->ts.u.derived->attr.use_assoc
13379 && sym->ns->proc_name
13380 && sym->ns->proc_name->attr.flavor == FL_MODULE
13381 && !resolve_fl_derived (sym->ts.u.derived))
13382 return;
13384 /* Unless the derived-type declaration is use associated, Fortran 95
13385 does not allow public entries of private derived types.
13386 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13387 161 in 95-006r3. */
13388 if (sym->ts.type == BT_DERIVED
13389 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13390 && !sym->ts.u.derived->attr.use_assoc
13391 && gfc_check_symbol_access (sym)
13392 && !gfc_check_symbol_access (sym->ts.u.derived)
13393 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13394 "derived type '%s'",
13395 (sym->attr.flavor == FL_PARAMETER)
13396 ? "parameter" : "variable",
13397 sym->name, &sym->declared_at,
13398 sym->ts.u.derived->name))
13399 return;
13401 /* F2008, C1302. */
13402 if (sym->ts.type == BT_DERIVED
13403 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13404 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13405 || sym->ts.u.derived->attr.lock_comp)
13406 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13408 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13409 "type LOCK_TYPE must be a coarray", sym->name,
13410 &sym->declared_at);
13411 return;
13414 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13415 default initialization is defined (5.1.2.4.4). */
13416 if (sym->ts.type == BT_DERIVED
13417 && sym->attr.dummy
13418 && sym->attr.intent == INTENT_OUT
13419 && sym->as
13420 && sym->as->type == AS_ASSUMED_SIZE)
13422 for (c = sym->ts.u.derived->components; c; c = c->next)
13424 if (c->initializer)
13426 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13427 "ASSUMED SIZE and so cannot have a default initializer",
13428 sym->name, &sym->declared_at);
13429 return;
13434 /* F2008, C542. */
13435 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13436 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13438 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13439 "INTENT(OUT)", sym->name, &sym->declared_at);
13440 return;
13443 /* F2008, C525. */
13444 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13445 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13446 && CLASS_DATA (sym)->attr.coarray_comp))
13447 || class_attr.codimension)
13448 && (sym->attr.result || sym->result == sym))
13450 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13451 "a coarray component", sym->name, &sym->declared_at);
13452 return;
13455 /* F2008, C524. */
13456 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13457 && sym->ts.u.derived->ts.is_iso_c)
13459 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13460 "shall not be a coarray", sym->name, &sym->declared_at);
13461 return;
13464 /* F2008, C525. */
13465 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13466 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13467 && CLASS_DATA (sym)->attr.coarray_comp))
13468 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13469 || class_attr.allocatable))
13471 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13472 "nonpointer, nonallocatable scalar, which is not a coarray",
13473 sym->name, &sym->declared_at);
13474 return;
13477 /* F2008, C526. The function-result case was handled above. */
13478 if (class_attr.codimension
13479 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13480 || sym->attr.select_type_temporary
13481 || sym->ns->save_all
13482 || sym->ns->proc_name->attr.flavor == FL_MODULE
13483 || sym->ns->proc_name->attr.is_main_program
13484 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13486 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13487 "nor a dummy argument", sym->name, &sym->declared_at);
13488 return;
13490 /* F2008, C528. */
13491 else if (class_attr.codimension && !sym->attr.select_type_temporary
13492 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13494 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13495 "deferred shape", sym->name, &sym->declared_at);
13496 return;
13498 else if (class_attr.codimension && class_attr.allocatable && as
13499 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13501 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13502 "deferred shape", sym->name, &sym->declared_at);
13503 return;
13506 /* F2008, C541. */
13507 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13508 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13509 && CLASS_DATA (sym)->attr.coarray_comp))
13510 || (class_attr.codimension && class_attr.allocatable))
13511 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13513 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13514 "allocatable coarray or have coarray components",
13515 sym->name, &sym->declared_at);
13516 return;
13519 if (class_attr.codimension && sym->attr.dummy
13520 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13522 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13523 "procedure '%s'", sym->name, &sym->declared_at,
13524 sym->ns->proc_name->name);
13525 return;
13528 if (sym->ts.type == BT_LOGICAL
13529 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13530 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13531 && sym->ns->proc_name->attr.is_bind_c)))
13533 int i;
13534 for (i = 0; gfc_logical_kinds[i].kind; i++)
13535 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13536 break;
13537 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13538 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13539 "%L with non-C_Bool kind in BIND(C) procedure "
13540 "'%s'", sym->name, &sym->declared_at,
13541 sym->ns->proc_name->name))
13542 return;
13543 else if (!gfc_logical_kinds[i].c_bool
13544 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13545 "'%s' at %L with non-C_Bool kind in "
13546 "BIND(C) procedure '%s'", sym->name,
13547 &sym->declared_at,
13548 sym->attr.function ? sym->name
13549 : sym->ns->proc_name->name))
13550 return;
13553 switch (sym->attr.flavor)
13555 case FL_VARIABLE:
13556 if (!resolve_fl_variable (sym, mp_flag))
13557 return;
13558 break;
13560 case FL_PROCEDURE:
13561 if (!resolve_fl_procedure (sym, mp_flag))
13562 return;
13563 break;
13565 case FL_NAMELIST:
13566 if (!resolve_fl_namelist (sym))
13567 return;
13568 break;
13570 case FL_PARAMETER:
13571 if (!resolve_fl_parameter (sym))
13572 return;
13573 break;
13575 default:
13576 break;
13579 /* Resolve array specifier. Check as well some constraints
13580 on COMMON blocks. */
13582 check_constant = sym->attr.in_common && !sym->attr.pointer;
13584 /* Set the formal_arg_flag so that check_conflict will not throw
13585 an error for host associated variables in the specification
13586 expression for an array_valued function. */
13587 if (sym->attr.function && sym->as)
13588 formal_arg_flag = 1;
13590 saved_specification_expr = specification_expr;
13591 specification_expr = true;
13592 gfc_resolve_array_spec (sym->as, check_constant);
13593 specification_expr = saved_specification_expr;
13595 formal_arg_flag = 0;
13597 /* Resolve formal namespaces. */
13598 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13599 && !sym->attr.contained && !sym->attr.intrinsic)
13600 gfc_resolve (sym->formal_ns);
13602 /* Make sure the formal namespace is present. */
13603 if (sym->formal && !sym->formal_ns)
13605 gfc_formal_arglist *formal = sym->formal;
13606 while (formal && !formal->sym)
13607 formal = formal->next;
13609 if (formal)
13611 sym->formal_ns = formal->sym->ns;
13612 if (sym->ns != formal->sym->ns)
13613 sym->formal_ns->refs++;
13617 /* Check threadprivate restrictions. */
13618 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13619 && (!sym->attr.in_common
13620 && sym->module == NULL
13621 && (sym->ns->proc_name == NULL
13622 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13623 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13625 /* Check omp declare target restrictions. */
13626 if (sym->attr.omp_declare_target
13627 && sym->attr.flavor == FL_VARIABLE
13628 && !sym->attr.save
13629 && !sym->ns->save_all
13630 && (!sym->attr.in_common
13631 && sym->module == NULL
13632 && (sym->ns->proc_name == NULL
13633 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13634 gfc_error ("!$OMP DECLARE TARGET variable '%s' at %L isn't SAVEd",
13635 sym->name, &sym->declared_at);
13637 /* If we have come this far we can apply default-initializers, as
13638 described in 14.7.5, to those variables that have not already
13639 been assigned one. */
13640 if (sym->ts.type == BT_DERIVED
13641 && !sym->value
13642 && !sym->attr.allocatable
13643 && !sym->attr.alloc_comp)
13645 symbol_attribute *a = &sym->attr;
13647 if ((!a->save && !a->dummy && !a->pointer
13648 && !a->in_common && !a->use_assoc
13649 && (a->referenced || a->result)
13650 && !(a->function && sym != sym->result))
13651 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13652 apply_default_init (sym);
13655 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13656 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13657 && !CLASS_DATA (sym)->attr.class_pointer
13658 && !CLASS_DATA (sym)->attr.allocatable)
13659 apply_default_init (sym);
13661 /* If this symbol has a type-spec, check it. */
13662 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13663 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13664 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13665 return;
13669 /************* Resolve DATA statements *************/
13671 static struct
13673 gfc_data_value *vnode;
13674 mpz_t left;
13676 values;
13679 /* Advance the values structure to point to the next value in the data list. */
13681 static bool
13682 next_data_value (void)
13684 while (mpz_cmp_ui (values.left, 0) == 0)
13687 if (values.vnode->next == NULL)
13688 return false;
13690 values.vnode = values.vnode->next;
13691 mpz_set (values.left, values.vnode->repeat);
13694 return true;
13698 static bool
13699 check_data_variable (gfc_data_variable *var, locus *where)
13701 gfc_expr *e;
13702 mpz_t size;
13703 mpz_t offset;
13704 bool t;
13705 ar_type mark = AR_UNKNOWN;
13706 int i;
13707 mpz_t section_index[GFC_MAX_DIMENSIONS];
13708 gfc_ref *ref;
13709 gfc_array_ref *ar;
13710 gfc_symbol *sym;
13711 int has_pointer;
13713 if (!gfc_resolve_expr (var->expr))
13714 return false;
13716 ar = NULL;
13717 mpz_init_set_si (offset, 0);
13718 e = var->expr;
13720 if (e->expr_type != EXPR_VARIABLE)
13721 gfc_internal_error ("check_data_variable(): Bad expression");
13723 sym = e->symtree->n.sym;
13725 if (sym->ns->is_block_data && !sym->attr.in_common)
13727 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13728 sym->name, &sym->declared_at);
13731 if (e->ref == NULL && sym->as)
13733 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13734 " declaration", sym->name, where);
13735 return false;
13738 has_pointer = sym->attr.pointer;
13740 if (gfc_is_coindexed (e))
13742 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13743 where);
13744 return false;
13747 for (ref = e->ref; ref; ref = ref->next)
13749 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13750 has_pointer = 1;
13752 if (has_pointer
13753 && ref->type == REF_ARRAY
13754 && ref->u.ar.type != AR_FULL)
13756 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13757 "be a full array", sym->name, where);
13758 return false;
13762 if (e->rank == 0 || has_pointer)
13764 mpz_init_set_ui (size, 1);
13765 ref = NULL;
13767 else
13769 ref = e->ref;
13771 /* Find the array section reference. */
13772 for (ref = e->ref; ref; ref = ref->next)
13774 if (ref->type != REF_ARRAY)
13775 continue;
13776 if (ref->u.ar.type == AR_ELEMENT)
13777 continue;
13778 break;
13780 gcc_assert (ref);
13782 /* Set marks according to the reference pattern. */
13783 switch (ref->u.ar.type)
13785 case AR_FULL:
13786 mark = AR_FULL;
13787 break;
13789 case AR_SECTION:
13790 ar = &ref->u.ar;
13791 /* Get the start position of array section. */
13792 gfc_get_section_index (ar, section_index, &offset);
13793 mark = AR_SECTION;
13794 break;
13796 default:
13797 gcc_unreachable ();
13800 if (!gfc_array_size (e, &size))
13802 gfc_error ("Nonconstant array section at %L in DATA statement",
13803 &e->where);
13804 mpz_clear (offset);
13805 return false;
13809 t = true;
13811 while (mpz_cmp_ui (size, 0) > 0)
13813 if (!next_data_value ())
13815 gfc_error ("DATA statement at %L has more variables than values",
13816 where);
13817 t = false;
13818 break;
13821 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13822 if (!t)
13823 break;
13825 /* If we have more than one element left in the repeat count,
13826 and we have more than one element left in the target variable,
13827 then create a range assignment. */
13828 /* FIXME: Only done for full arrays for now, since array sections
13829 seem tricky. */
13830 if (mark == AR_FULL && ref && ref->next == NULL
13831 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13833 mpz_t range;
13835 if (mpz_cmp (size, values.left) >= 0)
13837 mpz_init_set (range, values.left);
13838 mpz_sub (size, size, values.left);
13839 mpz_set_ui (values.left, 0);
13841 else
13843 mpz_init_set (range, size);
13844 mpz_sub (values.left, values.left, size);
13845 mpz_set_ui (size, 0);
13848 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13849 offset, &range);
13851 mpz_add (offset, offset, range);
13852 mpz_clear (range);
13854 if (!t)
13855 break;
13858 /* Assign initial value to symbol. */
13859 else
13861 mpz_sub_ui (values.left, values.left, 1);
13862 mpz_sub_ui (size, size, 1);
13864 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13865 offset, NULL);
13866 if (!t)
13867 break;
13869 if (mark == AR_FULL)
13870 mpz_add_ui (offset, offset, 1);
13872 /* Modify the array section indexes and recalculate the offset
13873 for next element. */
13874 else if (mark == AR_SECTION)
13875 gfc_advance_section (section_index, ar, &offset);
13879 if (mark == AR_SECTION)
13881 for (i = 0; i < ar->dimen; i++)
13882 mpz_clear (section_index[i]);
13885 mpz_clear (size);
13886 mpz_clear (offset);
13888 return t;
13892 static bool traverse_data_var (gfc_data_variable *, locus *);
13894 /* Iterate over a list of elements in a DATA statement. */
13896 static bool
13897 traverse_data_list (gfc_data_variable *var, locus *where)
13899 mpz_t trip;
13900 iterator_stack frame;
13901 gfc_expr *e, *start, *end, *step;
13902 bool retval = true;
13904 mpz_init (frame.value);
13905 mpz_init (trip);
13907 start = gfc_copy_expr (var->iter.start);
13908 end = gfc_copy_expr (var->iter.end);
13909 step = gfc_copy_expr (var->iter.step);
13911 if (!gfc_simplify_expr (start, 1)
13912 || start->expr_type != EXPR_CONSTANT)
13914 gfc_error ("start of implied-do loop at %L could not be "
13915 "simplified to a constant value", &start->where);
13916 retval = false;
13917 goto cleanup;
13919 if (!gfc_simplify_expr (end, 1)
13920 || end->expr_type != EXPR_CONSTANT)
13922 gfc_error ("end of implied-do loop at %L could not be "
13923 "simplified to a constant value", &start->where);
13924 retval = false;
13925 goto cleanup;
13927 if (!gfc_simplify_expr (step, 1)
13928 || step->expr_type != EXPR_CONSTANT)
13930 gfc_error ("step of implied-do loop at %L could not be "
13931 "simplified to a constant value", &start->where);
13932 retval = false;
13933 goto cleanup;
13936 mpz_set (trip, end->value.integer);
13937 mpz_sub (trip, trip, start->value.integer);
13938 mpz_add (trip, trip, step->value.integer);
13940 mpz_div (trip, trip, step->value.integer);
13942 mpz_set (frame.value, start->value.integer);
13944 frame.prev = iter_stack;
13945 frame.variable = var->iter.var->symtree;
13946 iter_stack = &frame;
13948 while (mpz_cmp_ui (trip, 0) > 0)
13950 if (!traverse_data_var (var->list, where))
13952 retval = false;
13953 goto cleanup;
13956 e = gfc_copy_expr (var->expr);
13957 if (!gfc_simplify_expr (e, 1))
13959 gfc_free_expr (e);
13960 retval = false;
13961 goto cleanup;
13964 mpz_add (frame.value, frame.value, step->value.integer);
13966 mpz_sub_ui (trip, trip, 1);
13969 cleanup:
13970 mpz_clear (frame.value);
13971 mpz_clear (trip);
13973 gfc_free_expr (start);
13974 gfc_free_expr (end);
13975 gfc_free_expr (step);
13977 iter_stack = frame.prev;
13978 return retval;
13982 /* Type resolve variables in the variable list of a DATA statement. */
13984 static bool
13985 traverse_data_var (gfc_data_variable *var, locus *where)
13987 bool t;
13989 for (; var; var = var->next)
13991 if (var->expr == NULL)
13992 t = traverse_data_list (var, where);
13993 else
13994 t = check_data_variable (var, where);
13996 if (!t)
13997 return false;
14000 return true;
14004 /* Resolve the expressions and iterators associated with a data statement.
14005 This is separate from the assignment checking because data lists should
14006 only be resolved once. */
14008 static bool
14009 resolve_data_variables (gfc_data_variable *d)
14011 for (; d; d = d->next)
14013 if (d->list == NULL)
14015 if (!gfc_resolve_expr (d->expr))
14016 return false;
14018 else
14020 if (!gfc_resolve_iterator (&d->iter, false, true))
14021 return false;
14023 if (!resolve_data_variables (d->list))
14024 return false;
14028 return true;
14032 /* Resolve a single DATA statement. We implement this by storing a pointer to
14033 the value list into static variables, and then recursively traversing the
14034 variables list, expanding iterators and such. */
14036 static void
14037 resolve_data (gfc_data *d)
14040 if (!resolve_data_variables (d->var))
14041 return;
14043 values.vnode = d->value;
14044 if (d->value == NULL)
14045 mpz_set_ui (values.left, 0);
14046 else
14047 mpz_set (values.left, d->value->repeat);
14049 if (!traverse_data_var (d->var, &d->where))
14050 return;
14052 /* At this point, we better not have any values left. */
14054 if (next_data_value ())
14055 gfc_error ("DATA statement at %L has more values than variables",
14056 &d->where);
14060 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14061 accessed by host or use association, is a dummy argument to a pure function,
14062 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14063 is storage associated with any such variable, shall not be used in the
14064 following contexts: (clients of this function). */
14066 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14067 procedure. Returns zero if assignment is OK, nonzero if there is a
14068 problem. */
14070 gfc_impure_variable (gfc_symbol *sym)
14072 gfc_symbol *proc;
14073 gfc_namespace *ns;
14075 if (sym->attr.use_assoc || sym->attr.in_common)
14076 return 1;
14078 /* Check if the symbol's ns is inside the pure procedure. */
14079 for (ns = gfc_current_ns; ns; ns = ns->parent)
14081 if (ns == sym->ns)
14082 break;
14083 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14084 return 1;
14087 proc = sym->ns->proc_name;
14088 if (sym->attr.dummy
14089 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14090 || proc->attr.function))
14091 return 1;
14093 /* TODO: Sort out what can be storage associated, if anything, and include
14094 it here. In principle equivalences should be scanned but it does not
14095 seem to be possible to storage associate an impure variable this way. */
14096 return 0;
14100 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14101 current namespace is inside a pure procedure. */
14104 gfc_pure (gfc_symbol *sym)
14106 symbol_attribute attr;
14107 gfc_namespace *ns;
14109 if (sym == NULL)
14111 /* Check if the current namespace or one of its parents
14112 belongs to a pure procedure. */
14113 for (ns = gfc_current_ns; ns; ns = ns->parent)
14115 sym = ns->proc_name;
14116 if (sym == NULL)
14117 return 0;
14118 attr = sym->attr;
14119 if (attr.flavor == FL_PROCEDURE && attr.pure)
14120 return 1;
14122 return 0;
14125 attr = sym->attr;
14127 return attr.flavor == FL_PROCEDURE && attr.pure;
14131 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14132 checks if the current namespace is implicitly pure. Note that this
14133 function returns false for a PURE procedure. */
14136 gfc_implicit_pure (gfc_symbol *sym)
14138 gfc_namespace *ns;
14140 if (sym == NULL)
14142 /* Check if the current procedure is implicit_pure. Walk up
14143 the procedure list until we find a procedure. */
14144 for (ns = gfc_current_ns; ns; ns = ns->parent)
14146 sym = ns->proc_name;
14147 if (sym == NULL)
14148 return 0;
14150 if (sym->attr.flavor == FL_PROCEDURE)
14151 break;
14155 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14156 && !sym->attr.pure;
14160 void
14161 gfc_unset_implicit_pure (gfc_symbol *sym)
14163 gfc_namespace *ns;
14165 if (sym == NULL)
14167 /* Check if the current procedure is implicit_pure. Walk up
14168 the procedure list until we find a procedure. */
14169 for (ns = gfc_current_ns; ns; ns = ns->parent)
14171 sym = ns->proc_name;
14172 if (sym == NULL)
14173 return;
14175 if (sym->attr.flavor == FL_PROCEDURE)
14176 break;
14180 if (sym->attr.flavor == FL_PROCEDURE)
14181 sym->attr.implicit_pure = 0;
14182 else
14183 sym->attr.pure = 0;
14187 /* Test whether the current procedure is elemental or not. */
14190 gfc_elemental (gfc_symbol *sym)
14192 symbol_attribute attr;
14194 if (sym == NULL)
14195 sym = gfc_current_ns->proc_name;
14196 if (sym == NULL)
14197 return 0;
14198 attr = sym->attr;
14200 return attr.flavor == FL_PROCEDURE && attr.elemental;
14204 /* Warn about unused labels. */
14206 static void
14207 warn_unused_fortran_label (gfc_st_label *label)
14209 if (label == NULL)
14210 return;
14212 warn_unused_fortran_label (label->left);
14214 if (label->defined == ST_LABEL_UNKNOWN)
14215 return;
14217 switch (label->referenced)
14219 case ST_LABEL_UNKNOWN:
14220 gfc_warning ("Label %d at %L defined but not used", label->value,
14221 &label->where);
14222 break;
14224 case ST_LABEL_BAD_TARGET:
14225 gfc_warning ("Label %d at %L defined but cannot be used",
14226 label->value, &label->where);
14227 break;
14229 default:
14230 break;
14233 warn_unused_fortran_label (label->right);
14237 /* Returns the sequence type of a symbol or sequence. */
14239 static seq_type
14240 sequence_type (gfc_typespec ts)
14242 seq_type result;
14243 gfc_component *c;
14245 switch (ts.type)
14247 case BT_DERIVED:
14249 if (ts.u.derived->components == NULL)
14250 return SEQ_NONDEFAULT;
14252 result = sequence_type (ts.u.derived->components->ts);
14253 for (c = ts.u.derived->components->next; c; c = c->next)
14254 if (sequence_type (c->ts) != result)
14255 return SEQ_MIXED;
14257 return result;
14259 case BT_CHARACTER:
14260 if (ts.kind != gfc_default_character_kind)
14261 return SEQ_NONDEFAULT;
14263 return SEQ_CHARACTER;
14265 case BT_INTEGER:
14266 if (ts.kind != gfc_default_integer_kind)
14267 return SEQ_NONDEFAULT;
14269 return SEQ_NUMERIC;
14271 case BT_REAL:
14272 if (!(ts.kind == gfc_default_real_kind
14273 || ts.kind == gfc_default_double_kind))
14274 return SEQ_NONDEFAULT;
14276 return SEQ_NUMERIC;
14278 case BT_COMPLEX:
14279 if (ts.kind != gfc_default_complex_kind)
14280 return SEQ_NONDEFAULT;
14282 return SEQ_NUMERIC;
14284 case BT_LOGICAL:
14285 if (ts.kind != gfc_default_logical_kind)
14286 return SEQ_NONDEFAULT;
14288 return SEQ_NUMERIC;
14290 default:
14291 return SEQ_NONDEFAULT;
14296 /* Resolve derived type EQUIVALENCE object. */
14298 static bool
14299 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14301 gfc_component *c = derived->components;
14303 if (!derived)
14304 return true;
14306 /* Shall not be an object of nonsequence derived type. */
14307 if (!derived->attr.sequence)
14309 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14310 "attribute to be an EQUIVALENCE object", sym->name,
14311 &e->where);
14312 return false;
14315 /* Shall not have allocatable components. */
14316 if (derived->attr.alloc_comp)
14318 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14319 "components to be an EQUIVALENCE object",sym->name,
14320 &e->where);
14321 return false;
14324 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14326 gfc_error ("Derived type variable '%s' at %L with default "
14327 "initialization cannot be in EQUIVALENCE with a variable "
14328 "in COMMON", sym->name, &e->where);
14329 return false;
14332 for (; c ; c = c->next)
14334 if (c->ts.type == BT_DERIVED
14335 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14336 return false;
14338 /* Shall not be an object of sequence derived type containing a pointer
14339 in the structure. */
14340 if (c->attr.pointer)
14342 gfc_error ("Derived type variable '%s' at %L with pointer "
14343 "component(s) cannot be an EQUIVALENCE object",
14344 sym->name, &e->where);
14345 return false;
14348 return true;
14352 /* Resolve equivalence object.
14353 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14354 an allocatable array, an object of nonsequence derived type, an object of
14355 sequence derived type containing a pointer at any level of component
14356 selection, an automatic object, a function name, an entry name, a result
14357 name, a named constant, a structure component, or a subobject of any of
14358 the preceding objects. A substring shall not have length zero. A
14359 derived type shall not have components with default initialization nor
14360 shall two objects of an equivalence group be initialized.
14361 Either all or none of the objects shall have an protected attribute.
14362 The simple constraints are done in symbol.c(check_conflict) and the rest
14363 are implemented here. */
14365 static void
14366 resolve_equivalence (gfc_equiv *eq)
14368 gfc_symbol *sym;
14369 gfc_symbol *first_sym;
14370 gfc_expr *e;
14371 gfc_ref *r;
14372 locus *last_where = NULL;
14373 seq_type eq_type, last_eq_type;
14374 gfc_typespec *last_ts;
14375 int object, cnt_protected;
14376 const char *msg;
14378 last_ts = &eq->expr->symtree->n.sym->ts;
14380 first_sym = eq->expr->symtree->n.sym;
14382 cnt_protected = 0;
14384 for (object = 1; eq; eq = eq->eq, object++)
14386 e = eq->expr;
14388 e->ts = e->symtree->n.sym->ts;
14389 /* match_varspec might not know yet if it is seeing
14390 array reference or substring reference, as it doesn't
14391 know the types. */
14392 if (e->ref && e->ref->type == REF_ARRAY)
14394 gfc_ref *ref = e->ref;
14395 sym = e->symtree->n.sym;
14397 if (sym->attr.dimension)
14399 ref->u.ar.as = sym->as;
14400 ref = ref->next;
14403 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14404 if (e->ts.type == BT_CHARACTER
14405 && ref
14406 && ref->type == REF_ARRAY
14407 && ref->u.ar.dimen == 1
14408 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14409 && ref->u.ar.stride[0] == NULL)
14411 gfc_expr *start = ref->u.ar.start[0];
14412 gfc_expr *end = ref->u.ar.end[0];
14413 void *mem = NULL;
14415 /* Optimize away the (:) reference. */
14416 if (start == NULL && end == NULL)
14418 if (e->ref == ref)
14419 e->ref = ref->next;
14420 else
14421 e->ref->next = ref->next;
14422 mem = ref;
14424 else
14426 ref->type = REF_SUBSTRING;
14427 if (start == NULL)
14428 start = gfc_get_int_expr (gfc_default_integer_kind,
14429 NULL, 1);
14430 ref->u.ss.start = start;
14431 if (end == NULL && e->ts.u.cl)
14432 end = gfc_copy_expr (e->ts.u.cl->length);
14433 ref->u.ss.end = end;
14434 ref->u.ss.length = e->ts.u.cl;
14435 e->ts.u.cl = NULL;
14437 ref = ref->next;
14438 free (mem);
14441 /* Any further ref is an error. */
14442 if (ref)
14444 gcc_assert (ref->type == REF_ARRAY);
14445 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14446 &ref->u.ar.where);
14447 continue;
14451 if (!gfc_resolve_expr (e))
14452 continue;
14454 sym = e->symtree->n.sym;
14456 if (sym->attr.is_protected)
14457 cnt_protected++;
14458 if (cnt_protected > 0 && cnt_protected != object)
14460 gfc_error ("Either all or none of the objects in the "
14461 "EQUIVALENCE set at %L shall have the "
14462 "PROTECTED attribute",
14463 &e->where);
14464 break;
14467 /* Shall not equivalence common block variables in a PURE procedure. */
14468 if (sym->ns->proc_name
14469 && sym->ns->proc_name->attr.pure
14470 && sym->attr.in_common)
14472 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14473 "object in the pure procedure '%s'",
14474 sym->name, &e->where, sym->ns->proc_name->name);
14475 break;
14478 /* Shall not be a named constant. */
14479 if (e->expr_type == EXPR_CONSTANT)
14481 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14482 "object", sym->name, &e->where);
14483 continue;
14486 if (e->ts.type == BT_DERIVED
14487 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14488 continue;
14490 /* Check that the types correspond correctly:
14491 Note 5.28:
14492 A numeric sequence structure may be equivalenced to another sequence
14493 structure, an object of default integer type, default real type, double
14494 precision real type, default logical type such that components of the
14495 structure ultimately only become associated to objects of the same
14496 kind. A character sequence structure may be equivalenced to an object
14497 of default character kind or another character sequence structure.
14498 Other objects may be equivalenced only to objects of the same type and
14499 kind parameters. */
14501 /* Identical types are unconditionally OK. */
14502 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14503 goto identical_types;
14505 last_eq_type = sequence_type (*last_ts);
14506 eq_type = sequence_type (sym->ts);
14508 /* Since the pair of objects is not of the same type, mixed or
14509 non-default sequences can be rejected. */
14511 msg = "Sequence %s with mixed components in EQUIVALENCE "
14512 "statement at %L with different type objects";
14513 if ((object ==2
14514 && last_eq_type == SEQ_MIXED
14515 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14516 || (eq_type == SEQ_MIXED
14517 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14518 continue;
14520 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14521 "statement at %L with objects of different type";
14522 if ((object ==2
14523 && last_eq_type == SEQ_NONDEFAULT
14524 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14525 || (eq_type == SEQ_NONDEFAULT
14526 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14527 continue;
14529 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14530 "EQUIVALENCE statement at %L";
14531 if (last_eq_type == SEQ_CHARACTER
14532 && eq_type != SEQ_CHARACTER
14533 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14534 continue;
14536 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14537 "EQUIVALENCE statement at %L";
14538 if (last_eq_type == SEQ_NUMERIC
14539 && eq_type != SEQ_NUMERIC
14540 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14541 continue;
14543 identical_types:
14544 last_ts =&sym->ts;
14545 last_where = &e->where;
14547 if (!e->ref)
14548 continue;
14550 /* Shall not be an automatic array. */
14551 if (e->ref->type == REF_ARRAY
14552 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14554 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14555 "an EQUIVALENCE object", sym->name, &e->where);
14556 continue;
14559 r = e->ref;
14560 while (r)
14562 /* Shall not be a structure component. */
14563 if (r->type == REF_COMPONENT)
14565 gfc_error ("Structure component '%s' at %L cannot be an "
14566 "EQUIVALENCE object",
14567 r->u.c.component->name, &e->where);
14568 break;
14571 /* A substring shall not have length zero. */
14572 if (r->type == REF_SUBSTRING)
14574 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14576 gfc_error ("Substring at %L has length zero",
14577 &r->u.ss.start->where);
14578 break;
14581 r = r->next;
14587 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14589 static void
14590 resolve_fntype (gfc_namespace *ns)
14592 gfc_entry_list *el;
14593 gfc_symbol *sym;
14595 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14596 return;
14598 /* If there are any entries, ns->proc_name is the entry master
14599 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14600 if (ns->entries)
14601 sym = ns->entries->sym;
14602 else
14603 sym = ns->proc_name;
14604 if (sym->result == sym
14605 && sym->ts.type == BT_UNKNOWN
14606 && !gfc_set_default_type (sym, 0, NULL)
14607 && !sym->attr.untyped)
14609 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14610 sym->name, &sym->declared_at);
14611 sym->attr.untyped = 1;
14614 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14615 && !sym->attr.contained
14616 && !gfc_check_symbol_access (sym->ts.u.derived)
14617 && gfc_check_symbol_access (sym))
14619 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14620 "%L of PRIVATE type '%s'", sym->name,
14621 &sym->declared_at, sym->ts.u.derived->name);
14624 if (ns->entries)
14625 for (el = ns->entries->next; el; el = el->next)
14627 if (el->sym->result == el->sym
14628 && el->sym->ts.type == BT_UNKNOWN
14629 && !gfc_set_default_type (el->sym, 0, NULL)
14630 && !el->sym->attr.untyped)
14632 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14633 el->sym->name, &el->sym->declared_at);
14634 el->sym->attr.untyped = 1;
14640 /* 12.3.2.1.1 Defined operators. */
14642 static bool
14643 check_uop_procedure (gfc_symbol *sym, locus where)
14645 gfc_formal_arglist *formal;
14647 if (!sym->attr.function)
14649 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14650 sym->name, &where);
14651 return false;
14654 if (sym->ts.type == BT_CHARACTER
14655 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14656 && !(sym->result && sym->result->ts.u.cl
14657 && sym->result->ts.u.cl->length))
14659 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14660 "character length", sym->name, &where);
14661 return false;
14664 formal = gfc_sym_get_dummy_args (sym);
14665 if (!formal || !formal->sym)
14667 gfc_error ("User operator procedure '%s' at %L must have at least "
14668 "one argument", sym->name, &where);
14669 return false;
14672 if (formal->sym->attr.intent != INTENT_IN)
14674 gfc_error ("First argument of operator interface at %L must be "
14675 "INTENT(IN)", &where);
14676 return false;
14679 if (formal->sym->attr.optional)
14681 gfc_error ("First argument of operator interface at %L cannot be "
14682 "optional", &where);
14683 return false;
14686 formal = formal->next;
14687 if (!formal || !formal->sym)
14688 return true;
14690 if (formal->sym->attr.intent != INTENT_IN)
14692 gfc_error ("Second argument of operator interface at %L must be "
14693 "INTENT(IN)", &where);
14694 return false;
14697 if (formal->sym->attr.optional)
14699 gfc_error ("Second argument of operator interface at %L cannot be "
14700 "optional", &where);
14701 return false;
14704 if (formal->next)
14706 gfc_error ("Operator interface at %L must have, at most, two "
14707 "arguments", &where);
14708 return false;
14711 return true;
14714 static void
14715 gfc_resolve_uops (gfc_symtree *symtree)
14717 gfc_interface *itr;
14719 if (symtree == NULL)
14720 return;
14722 gfc_resolve_uops (symtree->left);
14723 gfc_resolve_uops (symtree->right);
14725 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14726 check_uop_procedure (itr->sym, itr->sym->declared_at);
14730 /* Examine all of the expressions associated with a program unit,
14731 assign types to all intermediate expressions, make sure that all
14732 assignments are to compatible types and figure out which names
14733 refer to which functions or subroutines. It doesn't check code
14734 block, which is handled by gfc_resolve_code. */
14736 static void
14737 resolve_types (gfc_namespace *ns)
14739 gfc_namespace *n;
14740 gfc_charlen *cl;
14741 gfc_data *d;
14742 gfc_equiv *eq;
14743 gfc_namespace* old_ns = gfc_current_ns;
14745 /* Check that all IMPLICIT types are ok. */
14746 if (!ns->seen_implicit_none)
14748 unsigned letter;
14749 for (letter = 0; letter != GFC_LETTERS; ++letter)
14750 if (ns->set_flag[letter]
14751 && !resolve_typespec_used (&ns->default_type[letter],
14752 &ns->implicit_loc[letter], NULL))
14753 return;
14756 gfc_current_ns = ns;
14758 resolve_entries (ns);
14760 resolve_common_vars (ns->blank_common.head, false);
14761 resolve_common_blocks (ns->common_root);
14763 resolve_contained_functions (ns);
14765 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14766 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14767 resolve_formal_arglist (ns->proc_name);
14769 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14771 for (cl = ns->cl_list; cl; cl = cl->next)
14772 resolve_charlen (cl);
14774 gfc_traverse_ns (ns, resolve_symbol);
14776 resolve_fntype (ns);
14778 for (n = ns->contained; n; n = n->sibling)
14780 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14781 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14782 "also be PURE", n->proc_name->name,
14783 &n->proc_name->declared_at);
14785 resolve_types (n);
14788 forall_flag = 0;
14789 gfc_do_concurrent_flag = 0;
14790 gfc_check_interfaces (ns);
14792 gfc_traverse_ns (ns, resolve_values);
14794 if (ns->save_all)
14795 gfc_save_all (ns);
14797 iter_stack = NULL;
14798 for (d = ns->data; d; d = d->next)
14799 resolve_data (d);
14801 iter_stack = NULL;
14802 gfc_traverse_ns (ns, gfc_formalize_init_value);
14804 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14806 for (eq = ns->equiv; eq; eq = eq->next)
14807 resolve_equivalence (eq);
14809 /* Warn about unused labels. */
14810 if (warn_unused_label)
14811 warn_unused_fortran_label (ns->st_labels);
14813 gfc_resolve_uops (ns->uop_root);
14815 gfc_resolve_omp_declare_simd (ns);
14817 gfc_resolve_omp_udrs (ns->omp_udr_root);
14819 gfc_current_ns = old_ns;
14823 /* Call gfc_resolve_code recursively. */
14825 static void
14826 resolve_codes (gfc_namespace *ns)
14828 gfc_namespace *n;
14829 bitmap_obstack old_obstack;
14831 if (ns->resolved == 1)
14832 return;
14834 for (n = ns->contained; n; n = n->sibling)
14835 resolve_codes (n);
14837 gfc_current_ns = ns;
14839 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14840 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14841 cs_base = NULL;
14843 /* Set to an out of range value. */
14844 current_entry_id = -1;
14846 old_obstack = labels_obstack;
14847 bitmap_obstack_initialize (&labels_obstack);
14849 gfc_resolve_code (ns->code, ns);
14851 bitmap_obstack_release (&labels_obstack);
14852 labels_obstack = old_obstack;
14856 /* This function is called after a complete program unit has been compiled.
14857 Its purpose is to examine all of the expressions associated with a program
14858 unit, assign types to all intermediate expressions, make sure that all
14859 assignments are to compatible types and figure out which names refer to
14860 which functions or subroutines. */
14862 void
14863 gfc_resolve (gfc_namespace *ns)
14865 gfc_namespace *old_ns;
14866 code_stack *old_cs_base;
14868 if (ns->resolved)
14869 return;
14871 ns->resolved = -1;
14872 old_ns = gfc_current_ns;
14873 old_cs_base = cs_base;
14875 resolve_types (ns);
14876 component_assignment_level = 0;
14877 resolve_codes (ns);
14879 gfc_current_ns = old_ns;
14880 cs_base = old_cs_base;
14881 ns->resolved = 1;
14883 gfc_run_passes (ns);