re PR c++/60992 (ICE in tsubst_copy, at cp/pt.c:12637)
[official-gcc.git] / gcc / fortran / resolve.c
blob7579573599a698c3240e0089984fa0b7a2a5296b
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
36 typedef enum seq_type
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 seq_type;
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
55 code_stack;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
62 static int forall_flag;
63 int gfc_do_concurrent_flag;
65 /* True when we are resolving an expression that is an actual argument to
66 a procedure. */
67 static bool actual_arg = false;
68 /* True when we are resolving an expression that is the first actual argument
69 to a procedure. */
70 static bool first_actual_arg = false;
73 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
75 static int omp_workshare_flag;
77 /* Nonzero if we are processing a formal arglist. The corresponding function
78 resets the flag each time that it is read. */
79 static int formal_arg_flag = 0;
81 /* True if we are resolving a specification expression. */
82 static bool specification_expr = false;
84 /* The id of the last entry seen. */
85 static int current_entry_id;
87 /* We use bitmaps to determine if a branch target is valid. */
88 static bitmap_obstack labels_obstack;
90 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91 static bool inquiry_argument = false;
94 int
95 gfc_is_formal_arg (void)
97 return formal_arg_flag;
100 /* Is the symbol host associated? */
101 static bool
102 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
104 for (ns = ns->parent; ns; ns = ns->parent)
106 if (sym->ns == ns)
107 return true;
110 return false;
113 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
114 an ABSTRACT derived-type. If where is not NULL, an error message with that
115 locus is printed, optionally using name. */
117 static bool
118 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
122 if (where)
124 if (name)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
126 name, where, ts->u.derived->name);
127 else
128 gfc_error ("ABSTRACT type '%s' used at %L",
129 ts->u.derived->name, where);
132 return false;
135 return true;
139 static bool
140 check_proc_interface (gfc_symbol *ifc, locus *where)
142 /* Several checks for F08:C1216. */
143 if (ifc->attr.procedure)
145 gfc_error ("Interface '%s' at %L is declared "
146 "in a later PROCEDURE statement", ifc->name, where);
147 return false;
149 if (ifc->generic)
151 /* For generic interfaces, check if there is
152 a specific procedure with the same name. */
153 gfc_interface *gen = ifc->generic;
154 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
155 gen = gen->next;
156 if (!gen)
158 gfc_error ("Interface '%s' at %L may not be generic",
159 ifc->name, where);
160 return false;
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
165 gfc_error ("Interface '%s' at %L may not be a statement function",
166 ifc->name, where);
167 return false;
169 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
170 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
171 ifc->attr.intrinsic = 1;
172 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
174 gfc_error ("Intrinsic procedure '%s' not allowed in "
175 "PROCEDURE statement at %L", ifc->name, where);
176 return false;
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
180 gfc_error ("Interface '%s' at %L must be explicit", ifc->name, where);
181 return false;
183 return true;
187 static void resolve_symbol (gfc_symbol *sym);
190 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
192 static bool
193 resolve_procedure_interface (gfc_symbol *sym)
195 gfc_symbol *ifc = sym->ts.interface;
197 if (!ifc)
198 return true;
200 if (ifc == sym)
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
204 return false;
206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
209 if (ifc->attr.if_source || ifc->attr.intrinsic)
211 /* Resolve interface and copy attributes. */
212 resolve_symbol (ifc);
213 if (ifc->attr.intrinsic)
214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
216 if (ifc->result)
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
221 else
222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.pure = ifc->attr.pure;
230 sym->attr.elemental = ifc->attr.elemental;
231 sym->attr.dimension = ifc->attr.dimension;
232 sym->attr.contiguous = ifc->attr.contiguous;
233 sym->attr.recursive = ifc->attr.recursive;
234 sym->attr.always_explicit = ifc->attr.always_explicit;
235 sym->attr.ext_attr |= ifc->attr.ext_attr;
236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
237 sym->attr.class_ok = ifc->attr.class_ok;
238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
240 /* Copy char length. */
241 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
243 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
250 return true;
254 /* Resolve types of formal argument lists. These have to be done early so that
255 the formal argument lists of module procedures can be copied to the
256 containing module before the individual procedures are resolved
257 individually. We also resolve argument lists of procedures in interface
258 blocks because they are self-contained scoping units.
260 Since a dummy argument cannot be a non-dummy procedure, the only
261 resort left for untyped names are the IMPLICIT types. */
263 static void
264 resolve_formal_arglist (gfc_symbol *proc)
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
268 bool saved_specification_expr;
269 int i;
271 if (proc->result != NULL)
272 sym = proc->result;
273 else
274 sym = proc;
276 if (gfc_elemental (proc)
277 || sym->attr.pointer || sym->attr.allocatable
278 || (sym->as && sym->as->rank != 0))
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
284 formal_arg_flag = 1;
286 for (f = proc->formal; f; f = f->next)
288 gfc_array_spec *as;
290 sym = f->sym;
292 if (sym == NULL)
294 /* Alternate return placeholder. */
295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
297 "'%s' at %L is not allowed", proc->name,
298 &proc->declared_at);
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
302 &proc->declared_at);
303 continue;
305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
306 && !resolve_procedure_interface (sym))
307 return;
309 if (strcmp (proc->name, sym->name) == 0)
311 gfc_error ("Self-referential argument "
312 "'%s' at %L is not allowed", sym->name,
313 &proc->declared_at);
314 return;
317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
320 if (sym->attr.subroutine || sym->attr.external)
322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
325 else
327 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
328 && (!sym->attr.function || sym->result == sym))
329 gfc_set_default_type (sym, 1, sym->ns);
332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
335 saved_specification_expr = specification_expr;
336 specification_expr = true;
337 gfc_resolve_array_spec (as, 0);
338 specification_expr = saved_specification_expr;
340 /* We can't tell if an array with dimension (:) is assumed or deferred
341 shape until we know if it has the pointer or allocatable attributes.
343 if (as && as->rank > 0 && as->type == AS_DEFERRED
344 && ((sym->ts.type != BT_CLASS
345 && !(sym->attr.pointer || sym->attr.allocatable))
346 || (sym->ts.type == BT_CLASS
347 && !(CLASS_DATA (sym)->attr.class_pointer
348 || CLASS_DATA (sym)->attr.allocatable)))
349 && sym->attr.flavor != FL_PROCEDURE)
351 as->type = AS_ASSUMED_SHAPE;
352 for (i = 0; i < as->rank; i++)
353 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
357 || (as && as->type == AS_ASSUMED_RANK)
358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
359 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
360 && (CLASS_DATA (sym)->attr.class_pointer
361 || CLASS_DATA (sym)->attr.allocatable
362 || CLASS_DATA (sym)->attr.target))
363 || sym->attr.optional)
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
370 /* If the flavor is unknown at this point, it has to be a variable.
371 A procedure specification would have already set the type. */
373 if (sym->attr.flavor == FL_UNKNOWN)
374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
376 if (gfc_pure (proc))
378 if (sym->attr.flavor == FL_PROCEDURE)
380 /* F08:C1279. */
381 if (!gfc_pure (sym))
383 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
388 else if (!sym->attr.pointer)
390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
392 if (sym->attr.value)
393 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
394 " of pure function '%s' at %L with VALUE "
395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
398 gfc_error ("Argument '%s' of pure function '%s' at %L must "
399 "be INTENT(IN) or VALUE", sym->name, proc->name,
400 &sym->declared_at);
403 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
405 if (sym->attr.value)
406 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
407 " of pure subroutine '%s' at %L with VALUE "
408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
411 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
419 if (proc->attr.implicit_pure)
421 if (sym->attr.flavor == FL_PROCEDURE)
423 if (!gfc_pure (sym))
424 proc->attr.implicit_pure = 0;
426 else if (!sym->attr.pointer)
428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
430 proc->attr.implicit_pure = 0;
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
434 proc->attr.implicit_pure = 0;
438 if (gfc_elemental (proc))
440 /* F08:C1289. */
441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
445 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
446 "procedure", sym->name, &sym->declared_at);
447 continue;
450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
453 gfc_error ("Argument '%s' of elemental procedure at %L must "
454 "be scalar", sym->name, &sym->declared_at);
455 continue;
458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
462 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
472 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
475 continue;
478 if (sym->attr.flavor == FL_PROCEDURE)
480 gfc_error ("Dummy procedure '%s' not allowed in elemental "
481 "procedure '%s' at %L", sym->name, proc->name,
482 &sym->declared_at);
483 continue;
486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
489 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
492 &sym->declared_at);
493 continue;
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
500 if (sym->as != NULL)
502 gfc_error ("Argument '%s' of statement function at %L must "
503 "be scalar", sym->name, &sym->declared_at);
504 continue;
507 if (sym->ts.type == BT_CHARACTER)
509 gfc_charlen *cl = sym->ts.u.cl;
510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
512 gfc_error ("Character-valued argument '%s' of statement "
513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
520 formal_arg_flag = 0;
524 /* Work function called when searching for symbols that have argument lists
525 associated with them. */
527 static void
528 find_arglists (gfc_symbol *sym)
530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
532 return;
534 resolve_formal_arglist (sym);
538 /* Given a namespace, resolve all formal argument lists within the namespace.
541 static void
542 resolve_formal_arglists (gfc_namespace *ns)
544 if (ns == NULL)
545 return;
547 gfc_traverse_ns (ns, find_arglists);
551 static void
552 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
554 bool t;
556 /* If this namespace is not a function or an entry master function,
557 ignore it. */
558 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
559 || sym->attr.entry_master)
560 return;
562 /* Try to find out of what the return type is. */
563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
565 t = gfc_set_default_type (sym->result, 0, ns);
567 if (!t && !sym->result->attr.untyped)
569 if (sym->result == sym)
570 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
571 sym->name, &sym->declared_at);
572 else if (!sym->result->attr.proc_pointer)
573 gfc_error ("Result '%s' of contained function '%s' at %L has "
574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
586 if (sym->result->ts.type == BT_CHARACTER)
588 gfc_charlen *cl = sym->result->ts.u.cl;
589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
591 /* See if this is a module-procedure and adapt error message
592 accordingly. */
593 bool module_proc;
594 gcc_assert (ns->parent && ns->parent->proc_name);
595 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
597 gfc_error ("Character-valued %s '%s' at %L must not be"
598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
607 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
608 introduce duplicates. */
610 static void
611 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
613 gfc_formal_arglist *f, *new_arglist;
614 gfc_symbol *new_sym;
616 for (; new_args != NULL; new_args = new_args->next)
618 new_sym = new_args->sym;
619 /* See if this arg is already in the formal argument list. */
620 for (f = proc->formal; f; f = f->next)
622 if (new_sym == f->sym)
623 break;
626 if (f)
627 continue;
629 /* Add a new argument. Argument order is not important. */
630 new_arglist = gfc_get_formal_arglist ();
631 new_arglist->sym = new_sym;
632 new_arglist->next = proc->formal;
633 proc->formal = new_arglist;
638 /* Flag the arguments that are not present in all entries. */
640 static void
641 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
643 gfc_formal_arglist *f, *head;
644 head = new_args;
646 for (f = proc->formal; f; f = f->next)
648 if (f->sym == NULL)
649 continue;
651 for (new_args = head; new_args; new_args = new_args->next)
653 if (new_args->sym == f->sym)
654 break;
657 if (new_args)
658 continue;
660 f->sym->attr.not_always_present = 1;
665 /* Resolve alternate entry points. If a symbol has multiple entry points we
666 create a new master symbol for the main routine, and turn the existing
667 symbol into an entry point. */
669 static void
670 resolve_entries (gfc_namespace *ns)
672 gfc_namespace *old_ns;
673 gfc_code *c;
674 gfc_symbol *proc;
675 gfc_entry_list *el;
676 char name[GFC_MAX_SYMBOL_LEN + 1];
677 static int master_count = 0;
679 if (ns->proc_name == NULL)
680 return;
682 /* No need to do anything if this procedure doesn't have alternate entry
683 points. */
684 if (!ns->entries)
685 return;
687 /* We may already have resolved alternate entry points. */
688 if (ns->proc_name->attr.entry_master)
689 return;
691 /* If this isn't a procedure something has gone horribly wrong. */
692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
694 /* Remember the current namespace. */
695 old_ns = gfc_current_ns;
697 gfc_current_ns = ns;
699 /* Add the main entry point to the list of entry points. */
700 el = gfc_get_entry_list ();
701 el->sym = ns->proc_name;
702 el->id = 0;
703 el->next = ns->entries;
704 ns->entries = el;
705 ns->proc_name->attr.entry = 1;
707 /* If it is a module function, it needs to be in the right namespace
708 so that gfc_get_fake_result_decl can gather up the results. The
709 need for this arose in get_proc_name, where these beasts were
710 left in their own namespace, to keep prior references linked to
711 the entry declaration.*/
712 if (ns->proc_name->attr.function
713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
714 el->sym->ns = ns;
716 /* Do the same for entries where the master is not a module
717 procedure. These are retained in the module namespace because
718 of the module procedure declaration. */
719 for (el = el->next; el; el = el->next)
720 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
721 && el->sym->attr.mod_proc)
722 el->sym->ns = ns;
723 el = ns->entries;
725 /* Add an entry statement for it. */
726 c = gfc_get_code (EXEC_ENTRY);
727 c->ext.entry = el;
728 c->next = ns->code;
729 ns->code = c;
731 /* Create a new symbol for the master function. */
732 /* Give the internal function a unique name (within this file).
733 Also include the function name so the user has some hope of figuring
734 out what is going on. */
735 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
736 master_count++, ns->proc_name->name);
737 gfc_get_ha_symbol (name, &proc);
738 gcc_assert (proc != NULL);
740 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
741 if (ns->proc_name->attr.subroutine)
742 gfc_add_subroutine (&proc->attr, proc->name, NULL);
743 else
745 gfc_symbol *sym;
746 gfc_typespec *ts, *fts;
747 gfc_array_spec *as, *fas;
748 gfc_add_function (&proc->attr, proc->name, NULL);
749 proc->result = proc;
750 fas = ns->entries->sym->as;
751 fas = fas ? fas : ns->entries->sym->result->as;
752 fts = &ns->entries->sym->result->ts;
753 if (fts->type == BT_UNKNOWN)
754 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
755 for (el = ns->entries->next; el; el = el->next)
757 ts = &el->sym->result->ts;
758 as = el->sym->as;
759 as = as ? as : el->sym->result->as;
760 if (ts->type == BT_UNKNOWN)
761 ts = gfc_get_default_type (el->sym->result->name, NULL);
763 if (! gfc_compare_types (ts, fts)
764 || (el->sym->result->attr.dimension
765 != ns->entries->sym->result->attr.dimension)
766 || (el->sym->result->attr.pointer
767 != ns->entries->sym->result->attr.pointer))
768 break;
769 else if (as && fas && ns->entries->sym->result != el->sym->result
770 && gfc_compare_array_spec (as, fas) == 0)
771 gfc_error ("Function %s at %L has entries with mismatched "
772 "array specifications", ns->entries->sym->name,
773 &ns->entries->sym->declared_at);
774 /* The characteristics need to match and thus both need to have
775 the same string length, i.e. both len=*, or both len=4.
776 Having both len=<variable> is also possible, but difficult to
777 check at compile time. */
778 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
779 && (((ts->u.cl->length && !fts->u.cl->length)
780 ||(!ts->u.cl->length && fts->u.cl->length))
781 || (ts->u.cl->length
782 && ts->u.cl->length->expr_type
783 != fts->u.cl->length->expr_type)
784 || (ts->u.cl->length
785 && ts->u.cl->length->expr_type == EXPR_CONSTANT
786 && mpz_cmp (ts->u.cl->length->value.integer,
787 fts->u.cl->length->value.integer) != 0)))
788 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
789 "entries returning variables of different "
790 "string lengths", ns->entries->sym->name,
791 &ns->entries->sym->declared_at);
794 if (el == NULL)
796 sym = ns->entries->sym->result;
797 /* All result types the same. */
798 proc->ts = *fts;
799 if (sym->attr.dimension)
800 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
801 if (sym->attr.pointer)
802 gfc_add_pointer (&proc->attr, NULL);
804 else
806 /* Otherwise the result will be passed through a union by
807 reference. */
808 proc->attr.mixed_entry_master = 1;
809 for (el = ns->entries; el; el = el->next)
811 sym = el->sym->result;
812 if (sym->attr.dimension)
814 if (el == ns->entries)
815 gfc_error ("FUNCTION result %s can't be an array in "
816 "FUNCTION %s at %L", sym->name,
817 ns->entries->sym->name, &sym->declared_at);
818 else
819 gfc_error ("ENTRY result %s can't be an array in "
820 "FUNCTION %s at %L", sym->name,
821 ns->entries->sym->name, &sym->declared_at);
823 else if (sym->attr.pointer)
825 if (el == ns->entries)
826 gfc_error ("FUNCTION result %s can't be a POINTER in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
829 else
830 gfc_error ("ENTRY result %s can't be a POINTER in "
831 "FUNCTION %s at %L", sym->name,
832 ns->entries->sym->name, &sym->declared_at);
834 else
836 ts = &sym->ts;
837 if (ts->type == BT_UNKNOWN)
838 ts = gfc_get_default_type (sym->name, NULL);
839 switch (ts->type)
841 case BT_INTEGER:
842 if (ts->kind == gfc_default_integer_kind)
843 sym = NULL;
844 break;
845 case BT_REAL:
846 if (ts->kind == gfc_default_real_kind
847 || ts->kind == gfc_default_double_kind)
848 sym = NULL;
849 break;
850 case BT_COMPLEX:
851 if (ts->kind == gfc_default_complex_kind)
852 sym = NULL;
853 break;
854 case BT_LOGICAL:
855 if (ts->kind == gfc_default_logical_kind)
856 sym = NULL;
857 break;
858 case BT_UNKNOWN:
859 /* We will issue error elsewhere. */
860 sym = NULL;
861 break;
862 default:
863 break;
865 if (sym)
867 if (el == ns->entries)
868 gfc_error ("FUNCTION result %s can't be of type %s "
869 "in FUNCTION %s at %L", sym->name,
870 gfc_typename (ts), ns->entries->sym->name,
871 &sym->declared_at);
872 else
873 gfc_error ("ENTRY result %s can't be of type %s "
874 "in FUNCTION %s at %L", sym->name,
875 gfc_typename (ts), ns->entries->sym->name,
876 &sym->declared_at);
882 proc->attr.access = ACCESS_PRIVATE;
883 proc->attr.entry_master = 1;
885 /* Merge all the entry point arguments. */
886 for (el = ns->entries; el; el = el->next)
887 merge_argument_lists (proc, el->sym->formal);
889 /* Check the master formal arguments for any that are not
890 present in all entry points. */
891 for (el = ns->entries; el; el = el->next)
892 check_argument_lists (proc, el->sym->formal);
894 /* Use the master function for the function body. */
895 ns->proc_name = proc;
897 /* Finalize the new symbols. */
898 gfc_commit_symbols ();
900 /* Restore the original namespace. */
901 gfc_current_ns = old_ns;
905 /* Resolve common variables. */
906 static void
907 resolve_common_vars (gfc_symbol *sym, bool named_common)
909 gfc_symbol *csym = sym;
911 for (; csym; csym = csym->common_next)
913 if (csym->value || csym->attr.data)
915 if (!csym->ns->is_block_data)
916 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
917 "but only in BLOCK DATA initialization is "
918 "allowed", csym->name, &csym->declared_at);
919 else if (!named_common)
920 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
921 "in a blank COMMON but initialization is only "
922 "allowed in named common blocks", csym->name,
923 &csym->declared_at);
926 if (UNLIMITED_POLY (csym))
927 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
928 "[F2008:C5100]", csym->name, &csym->declared_at);
930 if (csym->ts.type != BT_DERIVED)
931 continue;
933 if (!(csym->ts.u.derived->attr.sequence
934 || csym->ts.u.derived->attr.is_bind_c))
935 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
936 "has neither the SEQUENCE nor the BIND(C) "
937 "attribute", csym->name, &csym->declared_at);
938 if (csym->ts.u.derived->attr.alloc_comp)
939 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
940 "has an ultimate component that is "
941 "allocatable", csym->name, &csym->declared_at);
942 if (gfc_has_default_initializer (csym->ts.u.derived))
943 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
944 "may not have default initializer", csym->name,
945 &csym->declared_at);
947 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
948 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
952 /* Resolve common blocks. */
953 static void
954 resolve_common_blocks (gfc_symtree *common_root)
956 gfc_symbol *sym;
957 gfc_gsymbol * gsym;
959 if (common_root == NULL)
960 return;
962 if (common_root->left)
963 resolve_common_blocks (common_root->left);
964 if (common_root->right)
965 resolve_common_blocks (common_root->right);
967 resolve_common_vars (common_root->n.common->head, true);
969 /* The common name is a global name - in Fortran 2003 also if it has a
970 C binding name, since Fortran 2008 only the C binding name is a global
971 identifier. */
972 if (!common_root->n.common->binding_label
973 || gfc_notification_std (GFC_STD_F2008))
975 gsym = gfc_find_gsymbol (gfc_gsym_root,
976 common_root->n.common->name);
978 if (gsym && gfc_notification_std (GFC_STD_F2008)
979 && gsym->type == GSYM_COMMON
980 && ((common_root->n.common->binding_label
981 && (!gsym->binding_label
982 || strcmp (common_root->n.common->binding_label,
983 gsym->binding_label) != 0))
984 || (!common_root->n.common->binding_label
985 && gsym->binding_label)))
987 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
988 "identifier and must thus have the same binding name "
989 "as the same-named COMMON block at %L: %s vs %s",
990 common_root->n.common->name, &common_root->n.common->where,
991 &gsym->where,
992 common_root->n.common->binding_label
993 ? common_root->n.common->binding_label : "(blank)",
994 gsym->binding_label ? gsym->binding_label : "(blank)");
995 return;
998 if (gsym && gsym->type != GSYM_COMMON
999 && !common_root->n.common->binding_label)
1001 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
1002 "as entity at %L",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where);
1005 return;
1007 if (gsym && gsym->type != GSYM_COMMON)
1009 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
1010 "%L sharing the identifier with global non-COMMON-block "
1011 "entity at %L", common_root->n.common->name,
1012 &common_root->n.common->where, &gsym->where);
1013 return;
1015 if (!gsym)
1017 gsym = gfc_get_gsymbol (common_root->n.common->name);
1018 gsym->type = GSYM_COMMON;
1019 gsym->where = common_root->n.common->where;
1020 gsym->defined = 1;
1022 gsym->used = 1;
1025 if (common_root->n.common->binding_label)
1027 gsym = gfc_find_gsymbol (gfc_gsym_root,
1028 common_root->n.common->binding_label);
1029 if (gsym && gsym->type != GSYM_COMMON)
1031 gfc_error ("COMMON block at %L with binding label %s uses the same "
1032 "global identifier as entity at %L",
1033 &common_root->n.common->where,
1034 common_root->n.common->binding_label, &gsym->where);
1035 return;
1037 if (!gsym)
1039 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1040 gsym->type = GSYM_COMMON;
1041 gsym->where = common_root->n.common->where;
1042 gsym->defined = 1;
1044 gsym->used = 1;
1047 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1048 if (sym == NULL)
1049 return;
1051 if (sym->attr.flavor == FL_PARAMETER)
1052 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1053 sym->name, &common_root->n.common->where, &sym->declared_at);
1055 if (sym->attr.external)
1056 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1057 sym->name, &common_root->n.common->where);
1059 if (sym->attr.intrinsic)
1060 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1061 sym->name, &common_root->n.common->where);
1062 else if (sym->attr.result
1063 || gfc_is_function_return_value (sym, gfc_current_ns))
1064 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1065 "that is also a function result", sym->name,
1066 &common_root->n.common->where);
1067 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1068 && sym->attr.proc != PROC_ST_FUNCTION)
1069 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1070 "that is also a global procedure", sym->name,
1071 &common_root->n.common->where);
1075 /* Resolve contained function types. Because contained functions can call one
1076 another, they have to be worked out before any of the contained procedures
1077 can be resolved.
1079 The good news is that if a function doesn't already have a type, the only
1080 way it can get one is through an IMPLICIT type or a RESULT variable, because
1081 by definition contained functions are contained namespace they're contained
1082 in, not in a sibling or parent namespace. */
1084 static void
1085 resolve_contained_functions (gfc_namespace *ns)
1087 gfc_namespace *child;
1088 gfc_entry_list *el;
1090 resolve_formal_arglists (ns);
1092 for (child = ns->contained; child; child = child->sibling)
1094 /* Resolve alternate entry points first. */
1095 resolve_entries (child);
1097 /* Then check function return types. */
1098 resolve_contained_fntype (child->proc_name, child);
1099 for (el = child->entries; el; el = el->next)
1100 resolve_contained_fntype (el->sym, child);
1105 static bool resolve_fl_derived0 (gfc_symbol *sym);
1108 /* Resolve all of the elements of a structure constructor and make sure that
1109 the types are correct. The 'init' flag indicates that the given
1110 constructor is an initializer. */
1112 static bool
1113 resolve_structure_cons (gfc_expr *expr, int init)
1115 gfc_constructor *cons;
1116 gfc_component *comp;
1117 bool t;
1118 symbol_attribute a;
1120 t = true;
1122 if (expr->ts.type == BT_DERIVED)
1123 resolve_fl_derived0 (expr->ts.u.derived);
1125 cons = gfc_constructor_first (expr->value.constructor);
1127 /* A constructor may have references if it is the result of substituting a
1128 parameter variable. In this case we just pull out the component we
1129 want. */
1130 if (expr->ref)
1131 comp = expr->ref->u.c.sym->components;
1132 else
1133 comp = expr->ts.u.derived->components;
1135 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1137 int rank;
1139 if (!cons->expr)
1140 continue;
1142 if (!gfc_resolve_expr (cons->expr))
1144 t = false;
1145 continue;
1148 rank = comp->as ? comp->as->rank : 0;
1149 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1150 && (comp->attr.allocatable || cons->expr->rank))
1152 gfc_error ("The rank of the element in the structure "
1153 "constructor at %L does not match that of the "
1154 "component (%d/%d)", &cons->expr->where,
1155 cons->expr->rank, rank);
1156 t = false;
1159 /* If we don't have the right type, try to convert it. */
1161 if (!comp->attr.proc_pointer &&
1162 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1164 if (strcmp (comp->name, "_extends") == 0)
1166 /* Can afford to be brutal with the _extends initializer.
1167 The derived type can get lost because it is PRIVATE
1168 but it is not usage constrained by the standard. */
1169 cons->expr->ts = comp->ts;
1171 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s', is %s but should be %s",
1175 &cons->expr->where, comp->name,
1176 gfc_basic_typename (cons->expr->ts.type),
1177 gfc_basic_typename (comp->ts.type));
1178 t = false;
1180 else
1182 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1183 if (t)
1184 t = t2;
1188 /* For strings, the length of the constructor should be the same as
1189 the one of the structure, ensure this if the lengths are known at
1190 compile time and when we are dealing with PARAMETER or structure
1191 constructors. */
1192 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1193 && comp->ts.u.cl->length
1194 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1195 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1196 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1197 && cons->expr->rank != 0
1198 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1199 comp->ts.u.cl->length->value.integer) != 0)
1201 if (cons->expr->expr_type == EXPR_VARIABLE
1202 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1204 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1205 to make use of the gfc_resolve_character_array_constructor
1206 machinery. The expression is later simplified away to
1207 an array of string literals. */
1208 gfc_expr *para = cons->expr;
1209 cons->expr = gfc_get_expr ();
1210 cons->expr->ts = para->ts;
1211 cons->expr->where = para->where;
1212 cons->expr->expr_type = EXPR_ARRAY;
1213 cons->expr->rank = para->rank;
1214 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1215 gfc_constructor_append_expr (&cons->expr->value.constructor,
1216 para, &cons->expr->where);
1218 if (cons->expr->expr_type == EXPR_ARRAY)
1220 gfc_constructor *p;
1221 p = gfc_constructor_first (cons->expr->value.constructor);
1222 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1224 gfc_charlen *cl, *cl2;
1226 cl2 = NULL;
1227 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1229 if (cl == cons->expr->ts.u.cl)
1230 break;
1231 cl2 = cl;
1234 gcc_assert (cl);
1236 if (cl2)
1237 cl2->next = cl->next;
1239 gfc_free_expr (cl->length);
1240 free (cl);
1243 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1244 cons->expr->ts.u.cl->length_from_typespec = true;
1245 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1246 gfc_resolve_character_array_constructor (cons->expr);
1250 if (cons->expr->expr_type == EXPR_NULL
1251 && !(comp->attr.pointer || comp->attr.allocatable
1252 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1253 || (comp->ts.type == BT_CLASS
1254 && (CLASS_DATA (comp)->attr.class_pointer
1255 || CLASS_DATA (comp)->attr.allocatable))))
1257 t = false;
1258 gfc_error ("The NULL in the structure constructor at %L is "
1259 "being applied to component '%s', which is neither "
1260 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1261 comp->name);
1264 if (comp->attr.proc_pointer && comp->ts.interface)
1266 /* Check procedure pointer interface. */
1267 gfc_symbol *s2 = NULL;
1268 gfc_component *c2;
1269 const char *name;
1270 char err[200];
1272 c2 = gfc_get_proc_ptr_comp (cons->expr);
1273 if (c2)
1275 s2 = c2->ts.interface;
1276 name = c2->name;
1278 else if (cons->expr->expr_type == EXPR_FUNCTION)
1280 s2 = cons->expr->symtree->n.sym->result;
1281 name = cons->expr->symtree->n.sym->result->name;
1283 else if (cons->expr->expr_type != EXPR_NULL)
1285 s2 = cons->expr->symtree->n.sym;
1286 name = cons->expr->symtree->n.sym->name;
1289 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1290 err, sizeof (err), NULL, NULL))
1292 gfc_error ("Interface mismatch for procedure-pointer component "
1293 "'%s' in structure constructor at %L: %s",
1294 comp->name, &cons->expr->where, err);
1295 return false;
1299 if (!comp->attr.pointer || comp->attr.proc_pointer
1300 || cons->expr->expr_type == EXPR_NULL)
1301 continue;
1303 a = gfc_expr_attr (cons->expr);
1305 if (!a.pointer && !a.target)
1307 t = false;
1308 gfc_error ("The element in the structure constructor at %L, "
1309 "for pointer component '%s' should be a POINTER or "
1310 "a TARGET", &cons->expr->where, comp->name);
1313 if (init)
1315 /* F08:C461. Additional checks for pointer initialization. */
1316 if (a.allocatable)
1318 t = false;
1319 gfc_error ("Pointer initialization target at %L "
1320 "must not be ALLOCATABLE ", &cons->expr->where);
1322 if (!a.save)
1324 t = false;
1325 gfc_error ("Pointer initialization target at %L "
1326 "must have the SAVE attribute", &cons->expr->where);
1330 /* F2003, C1272 (3). */
1331 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 && gfc_option.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);
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)
2892 if (expr->ts.type == BT_UNKNOWN)
2893 expr->ts = sym->ts;
2894 t = true;
2896 else
2898 /* Apply the rules of section 14.1.2. */
2900 switch (procedure_kind (sym))
2902 case PTYPE_GENERIC:
2903 t = resolve_generic_f (expr);
2904 break;
2906 case PTYPE_SPECIFIC:
2907 t = resolve_specific_f (expr);
2908 break;
2910 case PTYPE_UNKNOWN:
2911 t = resolve_unknown_f (expr);
2912 break;
2914 default:
2915 gfc_internal_error ("resolve_function(): bad function type");
2919 /* If the expression is still a function (it might have simplified),
2920 then we check to see if we are calling an elemental function. */
2922 if (expr->expr_type != EXPR_FUNCTION)
2923 return t;
2925 temp = need_full_assumed_size;
2926 need_full_assumed_size = 0;
2928 if (!resolve_elemental_actual (expr, NULL))
2929 return false;
2931 if (omp_workshare_flag
2932 && expr->value.function.esym
2933 && ! gfc_elemental (expr->value.function.esym))
2935 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2936 "in WORKSHARE construct", expr->value.function.esym->name,
2937 &expr->where);
2938 t = false;
2941 #define GENERIC_ID expr->value.function.isym->id
2942 else if (expr->value.function.actual != NULL
2943 && expr->value.function.isym != NULL
2944 && GENERIC_ID != GFC_ISYM_LBOUND
2945 && GENERIC_ID != GFC_ISYM_LCOBOUND
2946 && GENERIC_ID != GFC_ISYM_UCOBOUND
2947 && GENERIC_ID != GFC_ISYM_LEN
2948 && GENERIC_ID != GFC_ISYM_LOC
2949 && GENERIC_ID != GFC_ISYM_C_LOC
2950 && GENERIC_ID != GFC_ISYM_PRESENT)
2952 /* Array intrinsics must also have the last upper bound of an
2953 assumed size array argument. UBOUND and SIZE have to be
2954 excluded from the check if the second argument is anything
2955 than a constant. */
2957 for (arg = expr->value.function.actual; arg; arg = arg->next)
2959 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2960 && arg == expr->value.function.actual
2961 && arg->next != NULL && arg->next->expr)
2963 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2964 break;
2966 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2967 break;
2969 if ((int)mpz_get_si (arg->next->expr->value.integer)
2970 < arg->expr->rank)
2971 break;
2974 if (arg->expr != NULL
2975 && arg->expr->rank > 0
2976 && resolve_assumed_size_actual (arg->expr))
2977 return false;
2980 #undef GENERIC_ID
2982 need_full_assumed_size = temp;
2983 name = NULL;
2985 if (!pure_function (expr, &name) && name)
2987 if (forall_flag)
2989 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2990 "FORALL %s", name, &expr->where,
2991 forall_flag == 2 ? "mask" : "block");
2992 t = false;
2994 else if (gfc_do_concurrent_flag)
2996 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2997 "DO CONCURRENT %s", name, &expr->where,
2998 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2999 t = false;
3001 else if (gfc_pure (NULL))
3003 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3004 "procedure within a PURE procedure", name, &expr->where);
3005 t = false;
3008 gfc_unset_implicit_pure (NULL);
3011 /* Functions without the RECURSIVE attribution are not allowed to
3012 * call themselves. */
3013 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3015 gfc_symbol *esym;
3016 esym = expr->value.function.esym;
3018 if (is_illegal_recursion (esym, gfc_current_ns))
3020 if (esym->attr.entry && esym->ns->entries)
3021 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3022 " function '%s' is not RECURSIVE",
3023 esym->name, &expr->where, esym->ns->entries->sym->name);
3024 else
3025 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3026 " is not RECURSIVE", esym->name, &expr->where);
3028 t = false;
3032 /* Character lengths of use associated functions may contains references to
3033 symbols not referenced from the current program unit otherwise. Make sure
3034 those symbols are marked as referenced. */
3036 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3037 && expr->value.function.esym->attr.use_assoc)
3039 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3042 /* Make sure that the expression has a typespec that works. */
3043 if (expr->ts.type == BT_UNKNOWN)
3045 if (expr->symtree->n.sym->result
3046 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3047 && !expr->symtree->n.sym->result->attr.proc_pointer)
3048 expr->ts = expr->symtree->n.sym->result->ts;
3051 return t;
3055 /************* Subroutine resolution *************/
3057 static void
3058 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3060 if (gfc_pure (sym))
3061 return;
3063 if (forall_flag)
3064 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3065 sym->name, &c->loc);
3066 else if (gfc_do_concurrent_flag)
3067 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3068 "PURE", sym->name, &c->loc);
3069 else if (gfc_pure (NULL))
3070 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3071 &c->loc);
3073 gfc_unset_implicit_pure (NULL);
3077 static match
3078 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3080 gfc_symbol *s;
3082 if (sym->attr.generic)
3084 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3085 if (s != NULL)
3087 c->resolved_sym = s;
3088 pure_subroutine (c, s);
3089 return MATCH_YES;
3092 /* TODO: Need to search for elemental references in generic interface. */
3095 if (sym->attr.intrinsic)
3096 return gfc_intrinsic_sub_interface (c, 0);
3098 return MATCH_NO;
3102 static bool
3103 resolve_generic_s (gfc_code *c)
3105 gfc_symbol *sym;
3106 match m;
3108 sym = c->symtree->n.sym;
3110 for (;;)
3112 m = resolve_generic_s0 (c, sym);
3113 if (m == MATCH_YES)
3114 return true;
3115 else if (m == MATCH_ERROR)
3116 return false;
3118 generic:
3119 if (sym->ns->parent == NULL)
3120 break;
3121 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3123 if (sym == NULL)
3124 break;
3125 if (!generic_sym (sym))
3126 goto generic;
3129 /* Last ditch attempt. See if the reference is to an intrinsic
3130 that possesses a matching interface. 14.1.2.4 */
3131 sym = c->symtree->n.sym;
3133 if (!gfc_is_intrinsic (sym, 1, c->loc))
3135 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3136 sym->name, &c->loc);
3137 return false;
3140 m = gfc_intrinsic_sub_interface (c, 0);
3141 if (m == MATCH_YES)
3142 return true;
3143 if (m == MATCH_NO)
3144 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3145 "intrinsic subroutine interface", sym->name, &c->loc);
3147 return false;
3151 /* Resolve a subroutine call known to be specific. */
3153 static match
3154 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3156 match m;
3158 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3160 if (sym->attr.dummy)
3162 sym->attr.proc = PROC_DUMMY;
3163 goto found;
3166 sym->attr.proc = PROC_EXTERNAL;
3167 goto found;
3170 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3171 goto found;
3173 if (sym->attr.intrinsic)
3175 m = gfc_intrinsic_sub_interface (c, 1);
3176 if (m == MATCH_YES)
3177 return MATCH_YES;
3178 if (m == MATCH_NO)
3179 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3180 "with an intrinsic", sym->name, &c->loc);
3182 return MATCH_ERROR;
3185 return MATCH_NO;
3187 found:
3188 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3190 c->resolved_sym = sym;
3191 pure_subroutine (c, sym);
3193 return MATCH_YES;
3197 static bool
3198 resolve_specific_s (gfc_code *c)
3200 gfc_symbol *sym;
3201 match m;
3203 sym = c->symtree->n.sym;
3205 for (;;)
3207 m = resolve_specific_s0 (c, sym);
3208 if (m == MATCH_YES)
3209 return true;
3210 if (m == MATCH_ERROR)
3211 return false;
3213 if (sym->ns->parent == NULL)
3214 break;
3216 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3218 if (sym == NULL)
3219 break;
3222 sym = c->symtree->n.sym;
3223 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3224 sym->name, &c->loc);
3226 return false;
3230 /* Resolve a subroutine call not known to be generic nor specific. */
3232 static bool
3233 resolve_unknown_s (gfc_code *c)
3235 gfc_symbol *sym;
3237 sym = c->symtree->n.sym;
3239 if (sym->attr.dummy)
3241 sym->attr.proc = PROC_DUMMY;
3242 goto found;
3245 /* See if we have an intrinsic function reference. */
3247 if (gfc_is_intrinsic (sym, 1, c->loc))
3249 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3250 return true;
3251 return false;
3254 /* The reference is to an external name. */
3256 found:
3257 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3259 c->resolved_sym = sym;
3261 pure_subroutine (c, sym);
3263 return true;
3267 /* Resolve a subroutine call. Although it was tempting to use the same code
3268 for functions, subroutines and functions are stored differently and this
3269 makes things awkward. */
3271 static bool
3272 resolve_call (gfc_code *c)
3274 bool t;
3275 procedure_type ptype = PROC_INTRINSIC;
3276 gfc_symbol *csym, *sym;
3277 bool no_formal_args;
3279 csym = c->symtree ? c->symtree->n.sym : NULL;
3281 if (csym && csym->ts.type != BT_UNKNOWN)
3283 gfc_error ("'%s' at %L has a type, which is not consistent with "
3284 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3285 return false;
3288 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3290 gfc_symtree *st;
3291 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3292 sym = st ? st->n.sym : NULL;
3293 if (sym && csym != sym
3294 && sym->ns == gfc_current_ns
3295 && sym->attr.flavor == FL_PROCEDURE
3296 && sym->attr.contained)
3298 sym->refs++;
3299 if (csym->attr.generic)
3300 c->symtree->n.sym = sym;
3301 else
3302 c->symtree = st;
3303 csym = c->symtree->n.sym;
3307 /* If this ia a deferred TBP, c->expr1 will be set. */
3308 if (!c->expr1 && csym)
3310 if (csym->attr.abstract)
3312 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3313 csym->name, &c->loc);
3314 return false;
3317 /* Subroutines without the RECURSIVE attribution are not allowed to
3318 call themselves. */
3319 if (is_illegal_recursion (csym, gfc_current_ns))
3321 if (csym->attr.entry && csym->ns->entries)
3322 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3323 "as subroutine '%s' is not RECURSIVE",
3324 csym->name, &c->loc, csym->ns->entries->sym->name);
3325 else
3326 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3327 "as it is not RECURSIVE", csym->name, &c->loc);
3329 t = false;
3333 /* Switch off assumed size checking and do this again for certain kinds
3334 of procedure, once the procedure itself is resolved. */
3335 need_full_assumed_size++;
3337 if (csym)
3338 ptype = csym->attr.proc;
3340 no_formal_args = csym && is_external_proc (csym)
3341 && gfc_sym_get_dummy_args (csym) == NULL;
3342 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3343 return false;
3345 /* Resume assumed_size checking. */
3346 need_full_assumed_size--;
3348 /* If external, check for usage. */
3349 if (csym && is_external_proc (csym))
3350 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3352 t = true;
3353 if (c->resolved_sym == NULL)
3355 c->resolved_isym = NULL;
3356 switch (procedure_kind (csym))
3358 case PTYPE_GENERIC:
3359 t = resolve_generic_s (c);
3360 break;
3362 case PTYPE_SPECIFIC:
3363 t = resolve_specific_s (c);
3364 break;
3366 case PTYPE_UNKNOWN:
3367 t = resolve_unknown_s (c);
3368 break;
3370 default:
3371 gfc_internal_error ("resolve_subroutine(): bad function type");
3375 /* Some checks of elemental subroutine actual arguments. */
3376 if (!resolve_elemental_actual (NULL, c))
3377 return false;
3379 return t;
3383 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3384 op1->shape and op2->shape are non-NULL return true if their shapes
3385 match. If both op1->shape and op2->shape are non-NULL return false
3386 if their shapes do not match. If either op1->shape or op2->shape is
3387 NULL, return true. */
3389 static bool
3390 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3392 bool t;
3393 int i;
3395 t = true;
3397 if (op1->shape != NULL && op2->shape != NULL)
3399 for (i = 0; i < op1->rank; i++)
3401 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3403 gfc_error ("Shapes for operands at %L and %L are not conformable",
3404 &op1->where, &op2->where);
3405 t = false;
3406 break;
3411 return t;
3415 /* Resolve an operator expression node. This can involve replacing the
3416 operation with a user defined function call. */
3418 static bool
3419 resolve_operator (gfc_expr *e)
3421 gfc_expr *op1, *op2;
3422 char msg[200];
3423 bool dual_locus_error;
3424 bool t;
3426 /* Resolve all subnodes-- give them types. */
3428 switch (e->value.op.op)
3430 default:
3431 if (!gfc_resolve_expr (e->value.op.op2))
3432 return false;
3434 /* Fall through... */
3436 case INTRINSIC_NOT:
3437 case INTRINSIC_UPLUS:
3438 case INTRINSIC_UMINUS:
3439 case INTRINSIC_PARENTHESES:
3440 if (!gfc_resolve_expr (e->value.op.op1))
3441 return false;
3442 break;
3445 /* Typecheck the new node. */
3447 op1 = e->value.op.op1;
3448 op2 = e->value.op.op2;
3449 dual_locus_error = false;
3451 if ((op1 && op1->expr_type == EXPR_NULL)
3452 || (op2 && op2->expr_type == EXPR_NULL))
3454 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3455 goto bad_op;
3458 switch (e->value.op.op)
3460 case INTRINSIC_UPLUS:
3461 case INTRINSIC_UMINUS:
3462 if (op1->ts.type == BT_INTEGER
3463 || op1->ts.type == BT_REAL
3464 || op1->ts.type == BT_COMPLEX)
3466 e->ts = op1->ts;
3467 break;
3470 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3471 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3472 goto bad_op;
3474 case INTRINSIC_PLUS:
3475 case INTRINSIC_MINUS:
3476 case INTRINSIC_TIMES:
3477 case INTRINSIC_DIVIDE:
3478 case INTRINSIC_POWER:
3479 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3481 gfc_type_convert_binary (e, 1);
3482 break;
3485 sprintf (msg,
3486 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3487 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3488 gfc_typename (&op2->ts));
3489 goto bad_op;
3491 case INTRINSIC_CONCAT:
3492 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3493 && op1->ts.kind == op2->ts.kind)
3495 e->ts.type = BT_CHARACTER;
3496 e->ts.kind = op1->ts.kind;
3497 break;
3500 sprintf (msg,
3501 _("Operands of string concatenation operator at %%L are %s/%s"),
3502 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3503 goto bad_op;
3505 case INTRINSIC_AND:
3506 case INTRINSIC_OR:
3507 case INTRINSIC_EQV:
3508 case INTRINSIC_NEQV:
3509 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3511 e->ts.type = BT_LOGICAL;
3512 e->ts.kind = gfc_kind_max (op1, op2);
3513 if (op1->ts.kind < e->ts.kind)
3514 gfc_convert_type (op1, &e->ts, 2);
3515 else if (op2->ts.kind < e->ts.kind)
3516 gfc_convert_type (op2, &e->ts, 2);
3517 break;
3520 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3521 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3522 gfc_typename (&op2->ts));
3524 goto bad_op;
3526 case INTRINSIC_NOT:
3527 if (op1->ts.type == BT_LOGICAL)
3529 e->ts.type = BT_LOGICAL;
3530 e->ts.kind = op1->ts.kind;
3531 break;
3534 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3535 gfc_typename (&op1->ts));
3536 goto bad_op;
3538 case INTRINSIC_GT:
3539 case INTRINSIC_GT_OS:
3540 case INTRINSIC_GE:
3541 case INTRINSIC_GE_OS:
3542 case INTRINSIC_LT:
3543 case INTRINSIC_LT_OS:
3544 case INTRINSIC_LE:
3545 case INTRINSIC_LE_OS:
3546 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3548 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3549 goto bad_op;
3552 /* Fall through... */
3554 case INTRINSIC_EQ:
3555 case INTRINSIC_EQ_OS:
3556 case INTRINSIC_NE:
3557 case INTRINSIC_NE_OS:
3558 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3559 && op1->ts.kind == op2->ts.kind)
3561 e->ts.type = BT_LOGICAL;
3562 e->ts.kind = gfc_default_logical_kind;
3563 break;
3566 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3568 gfc_type_convert_binary (e, 1);
3570 e->ts.type = BT_LOGICAL;
3571 e->ts.kind = gfc_default_logical_kind;
3573 if (gfc_option.warn_compare_reals)
3575 gfc_intrinsic_op op = e->value.op.op;
3577 /* Type conversion has made sure that the types of op1 and op2
3578 agree, so it is only necessary to check the first one. */
3579 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3580 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3581 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3583 const char *msg;
3585 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3586 msg = "Equality comparison for %s at %L";
3587 else
3588 msg = "Inequality comparison for %s at %L";
3590 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3594 break;
3597 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3598 sprintf (msg,
3599 _("Logicals at %%L must be compared with %s instead of %s"),
3600 (e->value.op.op == INTRINSIC_EQ
3601 || e->value.op.op == INTRINSIC_EQ_OS)
3602 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3603 else
3604 sprintf (msg,
3605 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3606 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3607 gfc_typename (&op2->ts));
3609 goto bad_op;
3611 case INTRINSIC_USER:
3612 if (e->value.op.uop->op == NULL)
3613 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3614 else if (op2 == NULL)
3615 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3616 e->value.op.uop->name, gfc_typename (&op1->ts));
3617 else
3619 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3620 e->value.op.uop->name, gfc_typename (&op1->ts),
3621 gfc_typename (&op2->ts));
3622 e->value.op.uop->op->sym->attr.referenced = 1;
3625 goto bad_op;
3627 case INTRINSIC_PARENTHESES:
3628 e->ts = op1->ts;
3629 if (e->ts.type == BT_CHARACTER)
3630 e->ts.u.cl = op1->ts.u.cl;
3631 break;
3633 default:
3634 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3637 /* Deal with arrayness of an operand through an operator. */
3639 t = true;
3641 switch (e->value.op.op)
3643 case INTRINSIC_PLUS:
3644 case INTRINSIC_MINUS:
3645 case INTRINSIC_TIMES:
3646 case INTRINSIC_DIVIDE:
3647 case INTRINSIC_POWER:
3648 case INTRINSIC_CONCAT:
3649 case INTRINSIC_AND:
3650 case INTRINSIC_OR:
3651 case INTRINSIC_EQV:
3652 case INTRINSIC_NEQV:
3653 case INTRINSIC_EQ:
3654 case INTRINSIC_EQ_OS:
3655 case INTRINSIC_NE:
3656 case INTRINSIC_NE_OS:
3657 case INTRINSIC_GT:
3658 case INTRINSIC_GT_OS:
3659 case INTRINSIC_GE:
3660 case INTRINSIC_GE_OS:
3661 case INTRINSIC_LT:
3662 case INTRINSIC_LT_OS:
3663 case INTRINSIC_LE:
3664 case INTRINSIC_LE_OS:
3666 if (op1->rank == 0 && op2->rank == 0)
3667 e->rank = 0;
3669 if (op1->rank == 0 && op2->rank != 0)
3671 e->rank = op2->rank;
3673 if (e->shape == NULL)
3674 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3677 if (op1->rank != 0 && op2->rank == 0)
3679 e->rank = op1->rank;
3681 if (e->shape == NULL)
3682 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3685 if (op1->rank != 0 && op2->rank != 0)
3687 if (op1->rank == op2->rank)
3689 e->rank = op1->rank;
3690 if (e->shape == NULL)
3692 t = compare_shapes (op1, op2);
3693 if (!t)
3694 e->shape = NULL;
3695 else
3696 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3699 else
3701 /* Allow higher level expressions to work. */
3702 e->rank = 0;
3704 /* Try user-defined operators, and otherwise throw an error. */
3705 dual_locus_error = true;
3706 sprintf (msg,
3707 _("Inconsistent ranks for operator at %%L and %%L"));
3708 goto bad_op;
3712 break;
3714 case INTRINSIC_PARENTHESES:
3715 case INTRINSIC_NOT:
3716 case INTRINSIC_UPLUS:
3717 case INTRINSIC_UMINUS:
3718 /* Simply copy arrayness attribute */
3719 e->rank = op1->rank;
3721 if (e->shape == NULL)
3722 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3724 break;
3726 default:
3727 break;
3730 /* Attempt to simplify the expression. */
3731 if (t)
3733 t = gfc_simplify_expr (e, 0);
3734 /* Some calls do not succeed in simplification and return false
3735 even though there is no error; e.g. variable references to
3736 PARAMETER arrays. */
3737 if (!gfc_is_constant_expr (e))
3738 t = true;
3740 return t;
3742 bad_op:
3745 match m = gfc_extend_expr (e);
3746 if (m == MATCH_YES)
3747 return true;
3748 if (m == MATCH_ERROR)
3749 return false;
3752 if (dual_locus_error)
3753 gfc_error (msg, &op1->where, &op2->where);
3754 else
3755 gfc_error (msg, &e->where);
3757 return false;
3761 /************** Array resolution subroutines **************/
3763 typedef enum
3764 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3765 comparison;
3767 /* Compare two integer expressions. */
3769 static comparison
3770 compare_bound (gfc_expr *a, gfc_expr *b)
3772 int i;
3774 if (a == NULL || a->expr_type != EXPR_CONSTANT
3775 || b == NULL || b->expr_type != EXPR_CONSTANT)
3776 return CMP_UNKNOWN;
3778 /* If either of the types isn't INTEGER, we must have
3779 raised an error earlier. */
3781 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3782 return CMP_UNKNOWN;
3784 i = mpz_cmp (a->value.integer, b->value.integer);
3786 if (i < 0)
3787 return CMP_LT;
3788 if (i > 0)
3789 return CMP_GT;
3790 return CMP_EQ;
3794 /* Compare an integer expression with an integer. */
3796 static comparison
3797 compare_bound_int (gfc_expr *a, int b)
3799 int i;
3801 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3802 return CMP_UNKNOWN;
3804 if (a->ts.type != BT_INTEGER)
3805 gfc_internal_error ("compare_bound_int(): Bad expression");
3807 i = mpz_cmp_si (a->value.integer, b);
3809 if (i < 0)
3810 return CMP_LT;
3811 if (i > 0)
3812 return CMP_GT;
3813 return CMP_EQ;
3817 /* Compare an integer expression with a mpz_t. */
3819 static comparison
3820 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3822 int i;
3824 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3825 return CMP_UNKNOWN;
3827 if (a->ts.type != BT_INTEGER)
3828 gfc_internal_error ("compare_bound_int(): Bad expression");
3830 i = mpz_cmp (a->value.integer, b);
3832 if (i < 0)
3833 return CMP_LT;
3834 if (i > 0)
3835 return CMP_GT;
3836 return CMP_EQ;
3840 /* Compute the last value of a sequence given by a triplet.
3841 Return 0 if it wasn't able to compute the last value, or if the
3842 sequence if empty, and 1 otherwise. */
3844 static int
3845 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3846 gfc_expr *stride, mpz_t last)
3848 mpz_t rem;
3850 if (start == NULL || start->expr_type != EXPR_CONSTANT
3851 || end == NULL || end->expr_type != EXPR_CONSTANT
3852 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3853 return 0;
3855 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3856 || (stride != NULL && stride->ts.type != BT_INTEGER))
3857 return 0;
3859 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3861 if (compare_bound (start, end) == CMP_GT)
3862 return 0;
3863 mpz_set (last, end->value.integer);
3864 return 1;
3867 if (compare_bound_int (stride, 0) == CMP_GT)
3869 /* Stride is positive */
3870 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3871 return 0;
3873 else
3875 /* Stride is negative */
3876 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3877 return 0;
3880 mpz_init (rem);
3881 mpz_sub (rem, end->value.integer, start->value.integer);
3882 mpz_tdiv_r (rem, rem, stride->value.integer);
3883 mpz_sub (last, end->value.integer, rem);
3884 mpz_clear (rem);
3886 return 1;
3890 /* Compare a single dimension of an array reference to the array
3891 specification. */
3893 static bool
3894 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3896 mpz_t last_value;
3898 if (ar->dimen_type[i] == DIMEN_STAR)
3900 gcc_assert (ar->stride[i] == NULL);
3901 /* This implies [*] as [*:] and [*:3] are not possible. */
3902 if (ar->start[i] == NULL)
3904 gcc_assert (ar->end[i] == NULL);
3905 return true;
3909 /* Given start, end and stride values, calculate the minimum and
3910 maximum referenced indexes. */
3912 switch (ar->dimen_type[i])
3914 case DIMEN_VECTOR:
3915 case DIMEN_THIS_IMAGE:
3916 break;
3918 case DIMEN_STAR:
3919 case DIMEN_ELEMENT:
3920 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3922 if (i < as->rank)
3923 gfc_warning ("Array reference at %L is out of bounds "
3924 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3925 mpz_get_si (ar->start[i]->value.integer),
3926 mpz_get_si (as->lower[i]->value.integer), i+1);
3927 else
3928 gfc_warning ("Array reference at %L is out of bounds "
3929 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3930 mpz_get_si (ar->start[i]->value.integer),
3931 mpz_get_si (as->lower[i]->value.integer),
3932 i + 1 - as->rank);
3933 return true;
3935 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3937 if (i < as->rank)
3938 gfc_warning ("Array reference at %L is out of bounds "
3939 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3940 mpz_get_si (ar->start[i]->value.integer),
3941 mpz_get_si (as->upper[i]->value.integer), i+1);
3942 else
3943 gfc_warning ("Array reference at %L is out of bounds "
3944 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3945 mpz_get_si (ar->start[i]->value.integer),
3946 mpz_get_si (as->upper[i]->value.integer),
3947 i + 1 - as->rank);
3948 return true;
3951 break;
3953 case DIMEN_RANGE:
3955 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3956 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3958 comparison comp_start_end = compare_bound (AR_START, AR_END);
3960 /* Check for zero stride, which is not allowed. */
3961 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3963 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3964 return false;
3967 /* if start == len || (stride > 0 && start < len)
3968 || (stride < 0 && start > len),
3969 then the array section contains at least one element. In this
3970 case, there is an out-of-bounds access if
3971 (start < lower || start > upper). */
3972 if (compare_bound (AR_START, AR_END) == CMP_EQ
3973 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3974 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3975 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3976 && comp_start_end == CMP_GT))
3978 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3980 gfc_warning ("Lower array reference at %L is out of bounds "
3981 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3982 mpz_get_si (AR_START->value.integer),
3983 mpz_get_si (as->lower[i]->value.integer), i+1);
3984 return true;
3986 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3988 gfc_warning ("Lower array reference at %L is out of bounds "
3989 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3990 mpz_get_si (AR_START->value.integer),
3991 mpz_get_si (as->upper[i]->value.integer), i+1);
3992 return true;
3996 /* If we can compute the highest index of the array section,
3997 then it also has to be between lower and upper. */
3998 mpz_init (last_value);
3999 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4000 last_value))
4002 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4004 gfc_warning ("Upper array reference at %L is out of bounds "
4005 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4006 mpz_get_si (last_value),
4007 mpz_get_si (as->lower[i]->value.integer), i+1);
4008 mpz_clear (last_value);
4009 return true;
4011 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4013 gfc_warning ("Upper array reference at %L is out of bounds "
4014 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4015 mpz_get_si (last_value),
4016 mpz_get_si (as->upper[i]->value.integer), i+1);
4017 mpz_clear (last_value);
4018 return true;
4021 mpz_clear (last_value);
4023 #undef AR_START
4024 #undef AR_END
4026 break;
4028 default:
4029 gfc_internal_error ("check_dimension(): Bad array reference");
4032 return true;
4036 /* Compare an array reference with an array specification. */
4038 static bool
4039 compare_spec_to_ref (gfc_array_ref *ar)
4041 gfc_array_spec *as;
4042 int i;
4044 as = ar->as;
4045 i = as->rank - 1;
4046 /* TODO: Full array sections are only allowed as actual parameters. */
4047 if (as->type == AS_ASSUMED_SIZE
4048 && (/*ar->type == AR_FULL
4049 ||*/ (ar->type == AR_SECTION
4050 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4052 gfc_error ("Rightmost upper bound of assumed size array section "
4053 "not specified at %L", &ar->where);
4054 return false;
4057 if (ar->type == AR_FULL)
4058 return true;
4060 if (as->rank != ar->dimen)
4062 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4063 &ar->where, ar->dimen, as->rank);
4064 return false;
4067 /* ar->codimen == 0 is a local array. */
4068 if (as->corank != ar->codimen && ar->codimen != 0)
4070 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4071 &ar->where, ar->codimen, as->corank);
4072 return false;
4075 for (i = 0; i < as->rank; i++)
4076 if (!check_dimension (i, ar, as))
4077 return false;
4079 /* Local access has no coarray spec. */
4080 if (ar->codimen != 0)
4081 for (i = as->rank; i < as->rank + as->corank; i++)
4083 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4084 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4086 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4087 i + 1 - as->rank, &ar->where);
4088 return false;
4090 if (!check_dimension (i, ar, as))
4091 return false;
4094 return true;
4098 /* Resolve one part of an array index. */
4100 static bool
4101 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4102 int force_index_integer_kind)
4104 gfc_typespec ts;
4106 if (index == NULL)
4107 return true;
4109 if (!gfc_resolve_expr (index))
4110 return false;
4112 if (check_scalar && index->rank != 0)
4114 gfc_error ("Array index at %L must be scalar", &index->where);
4115 return false;
4118 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4120 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4121 &index->where, gfc_basic_typename (index->ts.type));
4122 return false;
4125 if (index->ts.type == BT_REAL)
4126 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4127 &index->where))
4128 return false;
4130 if ((index->ts.kind != gfc_index_integer_kind
4131 && force_index_integer_kind)
4132 || index->ts.type != BT_INTEGER)
4134 gfc_clear_ts (&ts);
4135 ts.type = BT_INTEGER;
4136 ts.kind = gfc_index_integer_kind;
4138 gfc_convert_type_warn (index, &ts, 2, 0);
4141 return true;
4144 /* Resolve one part of an array index. */
4146 bool
4147 gfc_resolve_index (gfc_expr *index, int check_scalar)
4149 return gfc_resolve_index_1 (index, check_scalar, 1);
4152 /* Resolve a dim argument to an intrinsic function. */
4154 bool
4155 gfc_resolve_dim_arg (gfc_expr *dim)
4157 if (dim == NULL)
4158 return true;
4160 if (!gfc_resolve_expr (dim))
4161 return false;
4163 if (dim->rank != 0)
4165 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4166 return false;
4170 if (dim->ts.type != BT_INTEGER)
4172 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4173 return false;
4176 if (dim->ts.kind != gfc_index_integer_kind)
4178 gfc_typespec ts;
4180 gfc_clear_ts (&ts);
4181 ts.type = BT_INTEGER;
4182 ts.kind = gfc_index_integer_kind;
4184 gfc_convert_type_warn (dim, &ts, 2, 0);
4187 return true;
4190 /* Given an expression that contains array references, update those array
4191 references to point to the right array specifications. While this is
4192 filled in during matching, this information is difficult to save and load
4193 in a module, so we take care of it here.
4195 The idea here is that the original array reference comes from the
4196 base symbol. We traverse the list of reference structures, setting
4197 the stored reference to references. Component references can
4198 provide an additional array specification. */
4200 static void
4201 find_array_spec (gfc_expr *e)
4203 gfc_array_spec *as;
4204 gfc_component *c;
4205 gfc_ref *ref;
4207 if (e->symtree->n.sym->ts.type == BT_CLASS)
4208 as = CLASS_DATA (e->symtree->n.sym)->as;
4209 else
4210 as = e->symtree->n.sym->as;
4212 for (ref = e->ref; ref; ref = ref->next)
4213 switch (ref->type)
4215 case REF_ARRAY:
4216 if (as == NULL)
4217 gfc_internal_error ("find_array_spec(): Missing spec");
4219 ref->u.ar.as = as;
4220 as = NULL;
4221 break;
4223 case REF_COMPONENT:
4224 c = ref->u.c.component;
4225 if (c->attr.dimension)
4227 if (as != NULL)
4228 gfc_internal_error ("find_array_spec(): unused as(1)");
4229 as = c->as;
4232 break;
4234 case REF_SUBSTRING:
4235 break;
4238 if (as != NULL)
4239 gfc_internal_error ("find_array_spec(): unused as(2)");
4243 /* Resolve an array reference. */
4245 static bool
4246 resolve_array_ref (gfc_array_ref *ar)
4248 int i, check_scalar;
4249 gfc_expr *e;
4251 for (i = 0; i < ar->dimen + ar->codimen; i++)
4253 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4255 /* Do not force gfc_index_integer_kind for the start. We can
4256 do fine with any integer kind. This avoids temporary arrays
4257 created for indexing with a vector. */
4258 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4259 return false;
4260 if (!gfc_resolve_index (ar->end[i], check_scalar))
4261 return false;
4262 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4263 return false;
4265 e = ar->start[i];
4267 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4268 switch (e->rank)
4270 case 0:
4271 ar->dimen_type[i] = DIMEN_ELEMENT;
4272 break;
4274 case 1:
4275 ar->dimen_type[i] = DIMEN_VECTOR;
4276 if (e->expr_type == EXPR_VARIABLE
4277 && e->symtree->n.sym->ts.type == BT_DERIVED)
4278 ar->start[i] = gfc_get_parentheses (e);
4279 break;
4281 default:
4282 gfc_error ("Array index at %L is an array of rank %d",
4283 &ar->c_where[i], e->rank);
4284 return false;
4287 /* Fill in the upper bound, which may be lower than the
4288 specified one for something like a(2:10:5), which is
4289 identical to a(2:7:5). Only relevant for strides not equal
4290 to one. Don't try a division by zero. */
4291 if (ar->dimen_type[i] == DIMEN_RANGE
4292 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4293 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4294 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4296 mpz_t size, end;
4298 if (gfc_ref_dimen_size (ar, i, &size, &end))
4300 if (ar->end[i] == NULL)
4302 ar->end[i] =
4303 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4304 &ar->where);
4305 mpz_set (ar->end[i]->value.integer, end);
4307 else if (ar->end[i]->ts.type == BT_INTEGER
4308 && ar->end[i]->expr_type == EXPR_CONSTANT)
4310 mpz_set (ar->end[i]->value.integer, end);
4312 else
4313 gcc_unreachable ();
4315 mpz_clear (size);
4316 mpz_clear (end);
4321 if (ar->type == AR_FULL)
4323 if (ar->as->rank == 0)
4324 ar->type = AR_ELEMENT;
4326 /* Make sure array is the same as array(:,:), this way
4327 we don't need to special case all the time. */
4328 ar->dimen = ar->as->rank;
4329 for (i = 0; i < ar->dimen; i++)
4331 ar->dimen_type[i] = DIMEN_RANGE;
4333 gcc_assert (ar->start[i] == NULL);
4334 gcc_assert (ar->end[i] == NULL);
4335 gcc_assert (ar->stride[i] == NULL);
4339 /* If the reference type is unknown, figure out what kind it is. */
4341 if (ar->type == AR_UNKNOWN)
4343 ar->type = AR_ELEMENT;
4344 for (i = 0; i < ar->dimen; i++)
4345 if (ar->dimen_type[i] == DIMEN_RANGE
4346 || ar->dimen_type[i] == DIMEN_VECTOR)
4348 ar->type = AR_SECTION;
4349 break;
4353 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4354 return false;
4356 if (ar->as->corank && ar->codimen == 0)
4358 int n;
4359 ar->codimen = ar->as->corank;
4360 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4361 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4364 return true;
4368 static bool
4369 resolve_substring (gfc_ref *ref)
4371 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4373 if (ref->u.ss.start != NULL)
4375 if (!gfc_resolve_expr (ref->u.ss.start))
4376 return false;
4378 if (ref->u.ss.start->ts.type != BT_INTEGER)
4380 gfc_error ("Substring start index at %L must be of type INTEGER",
4381 &ref->u.ss.start->where);
4382 return false;
4385 if (ref->u.ss.start->rank != 0)
4387 gfc_error ("Substring start index at %L must be scalar",
4388 &ref->u.ss.start->where);
4389 return false;
4392 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4393 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4394 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4396 gfc_error ("Substring start index at %L is less than one",
4397 &ref->u.ss.start->where);
4398 return false;
4402 if (ref->u.ss.end != NULL)
4404 if (!gfc_resolve_expr (ref->u.ss.end))
4405 return false;
4407 if (ref->u.ss.end->ts.type != BT_INTEGER)
4409 gfc_error ("Substring end index at %L must be of type INTEGER",
4410 &ref->u.ss.end->where);
4411 return false;
4414 if (ref->u.ss.end->rank != 0)
4416 gfc_error ("Substring end index at %L must be scalar",
4417 &ref->u.ss.end->where);
4418 return false;
4421 if (ref->u.ss.length != NULL
4422 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4423 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4424 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4426 gfc_error ("Substring end index at %L exceeds the string length",
4427 &ref->u.ss.start->where);
4428 return false;
4431 if (compare_bound_mpz_t (ref->u.ss.end,
4432 gfc_integer_kinds[k].huge) == CMP_GT
4433 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4434 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4436 gfc_error ("Substring end index at %L is too large",
4437 &ref->u.ss.end->where);
4438 return false;
4442 return true;
4446 /* This function supplies missing substring charlens. */
4448 void
4449 gfc_resolve_substring_charlen (gfc_expr *e)
4451 gfc_ref *char_ref;
4452 gfc_expr *start, *end;
4454 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4455 if (char_ref->type == REF_SUBSTRING)
4456 break;
4458 if (!char_ref)
4459 return;
4461 gcc_assert (char_ref->next == NULL);
4463 if (e->ts.u.cl)
4465 if (e->ts.u.cl->length)
4466 gfc_free_expr (e->ts.u.cl->length);
4467 else if (e->expr_type == EXPR_VARIABLE
4468 && e->symtree->n.sym->attr.dummy)
4469 return;
4472 e->ts.type = BT_CHARACTER;
4473 e->ts.kind = gfc_default_character_kind;
4475 if (!e->ts.u.cl)
4476 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4478 if (char_ref->u.ss.start)
4479 start = gfc_copy_expr (char_ref->u.ss.start);
4480 else
4481 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4483 if (char_ref->u.ss.end)
4484 end = gfc_copy_expr (char_ref->u.ss.end);
4485 else if (e->expr_type == EXPR_VARIABLE)
4486 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4487 else
4488 end = NULL;
4490 if (!start || !end)
4492 gfc_free_expr (start);
4493 gfc_free_expr (end);
4494 return;
4497 /* Length = (end - start +1). */
4498 e->ts.u.cl->length = gfc_subtract (end, start);
4499 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4500 gfc_get_int_expr (gfc_default_integer_kind,
4501 NULL, 1));
4503 e->ts.u.cl->length->ts.type = BT_INTEGER;
4504 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4506 /* Make sure that the length is simplified. */
4507 gfc_simplify_expr (e->ts.u.cl->length, 1);
4508 gfc_resolve_expr (e->ts.u.cl->length);
4512 /* Resolve subtype references. */
4514 static bool
4515 resolve_ref (gfc_expr *expr)
4517 int current_part_dimension, n_components, seen_part_dimension;
4518 gfc_ref *ref;
4520 for (ref = expr->ref; ref; ref = ref->next)
4521 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4523 find_array_spec (expr);
4524 break;
4527 for (ref = expr->ref; ref; ref = ref->next)
4528 switch (ref->type)
4530 case REF_ARRAY:
4531 if (!resolve_array_ref (&ref->u.ar))
4532 return false;
4533 break;
4535 case REF_COMPONENT:
4536 break;
4538 case REF_SUBSTRING:
4539 if (!resolve_substring (ref))
4540 return false;
4541 break;
4544 /* Check constraints on part references. */
4546 current_part_dimension = 0;
4547 seen_part_dimension = 0;
4548 n_components = 0;
4550 for (ref = expr->ref; ref; ref = ref->next)
4552 switch (ref->type)
4554 case REF_ARRAY:
4555 switch (ref->u.ar.type)
4557 case AR_FULL:
4558 /* Coarray scalar. */
4559 if (ref->u.ar.as->rank == 0)
4561 current_part_dimension = 0;
4562 break;
4564 /* Fall through. */
4565 case AR_SECTION:
4566 current_part_dimension = 1;
4567 break;
4569 case AR_ELEMENT:
4570 current_part_dimension = 0;
4571 break;
4573 case AR_UNKNOWN:
4574 gfc_internal_error ("resolve_ref(): Bad array reference");
4577 break;
4579 case REF_COMPONENT:
4580 if (current_part_dimension || seen_part_dimension)
4582 /* F03:C614. */
4583 if (ref->u.c.component->attr.pointer
4584 || ref->u.c.component->attr.proc_pointer
4585 || (ref->u.c.component->ts.type == BT_CLASS
4586 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4588 gfc_error ("Component to the right of a part reference "
4589 "with nonzero rank must not have the POINTER "
4590 "attribute at %L", &expr->where);
4591 return false;
4593 else if (ref->u.c.component->attr.allocatable
4594 || (ref->u.c.component->ts.type == BT_CLASS
4595 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4598 gfc_error ("Component to the right of a part reference "
4599 "with nonzero rank must not have the ALLOCATABLE "
4600 "attribute at %L", &expr->where);
4601 return false;
4605 n_components++;
4606 break;
4608 case REF_SUBSTRING:
4609 break;
4612 if (((ref->type == REF_COMPONENT && n_components > 1)
4613 || ref->next == NULL)
4614 && current_part_dimension
4615 && seen_part_dimension)
4617 gfc_error ("Two or more part references with nonzero rank must "
4618 "not be specified at %L", &expr->where);
4619 return false;
4622 if (ref->type == REF_COMPONENT)
4624 if (current_part_dimension)
4625 seen_part_dimension = 1;
4627 /* reset to make sure */
4628 current_part_dimension = 0;
4632 return true;
4636 /* Given an expression, determine its shape. This is easier than it sounds.
4637 Leaves the shape array NULL if it is not possible to determine the shape. */
4639 static void
4640 expression_shape (gfc_expr *e)
4642 mpz_t array[GFC_MAX_DIMENSIONS];
4643 int i;
4645 if (e->rank <= 0 || e->shape != NULL)
4646 return;
4648 for (i = 0; i < e->rank; i++)
4649 if (!gfc_array_dimen_size (e, i, &array[i]))
4650 goto fail;
4652 e->shape = gfc_get_shape (e->rank);
4654 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4656 return;
4658 fail:
4659 for (i--; i >= 0; i--)
4660 mpz_clear (array[i]);
4664 /* Given a variable expression node, compute the rank of the expression by
4665 examining the base symbol and any reference structures it may have. */
4667 static void
4668 expression_rank (gfc_expr *e)
4670 gfc_ref *ref;
4671 int i, rank;
4673 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4674 could lead to serious confusion... */
4675 gcc_assert (e->expr_type != EXPR_COMPCALL);
4677 if (e->ref == NULL)
4679 if (e->expr_type == EXPR_ARRAY)
4680 goto done;
4681 /* Constructors can have a rank different from one via RESHAPE(). */
4683 if (e->symtree == NULL)
4685 e->rank = 0;
4686 goto done;
4689 e->rank = (e->symtree->n.sym->as == NULL)
4690 ? 0 : e->symtree->n.sym->as->rank;
4691 goto done;
4694 rank = 0;
4696 for (ref = e->ref; ref; ref = ref->next)
4698 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4699 && ref->u.c.component->attr.function && !ref->next)
4700 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4702 if (ref->type != REF_ARRAY)
4703 continue;
4705 if (ref->u.ar.type == AR_FULL)
4707 rank = ref->u.ar.as->rank;
4708 break;
4711 if (ref->u.ar.type == AR_SECTION)
4713 /* Figure out the rank of the section. */
4714 if (rank != 0)
4715 gfc_internal_error ("expression_rank(): Two array specs");
4717 for (i = 0; i < ref->u.ar.dimen; i++)
4718 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4719 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4720 rank++;
4722 break;
4726 e->rank = rank;
4728 done:
4729 expression_shape (e);
4733 static void
4734 add_caf_get_intrinsic (gfc_expr *e)
4736 gfc_expr *wrapper, *tmp_expr;
4737 gfc_ref *ref;
4738 int n;
4740 for (ref = e->ref; ref; ref = ref->next)
4741 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4742 break;
4743 if (ref == NULL)
4744 return;
4746 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4747 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4748 return;
4750 tmp_expr = XCNEW (gfc_expr);
4751 *tmp_expr = *e;
4752 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4753 "caf_get", tmp_expr->where, 1, tmp_expr);
4754 wrapper->ts = e->ts;
4755 wrapper->rank = e->rank;
4756 if (e->rank)
4757 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4758 *e = *wrapper;
4759 free (wrapper);
4763 static void
4764 remove_caf_get_intrinsic (gfc_expr *e)
4766 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4767 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4768 gfc_expr *e2 = e->value.function.actual->expr;
4769 e->value.function.actual->expr =NULL;
4770 gfc_free_actual_arglist (e->value.function.actual);
4771 gfc_free_shape (&e->shape, e->rank);
4772 *e = *e2;
4773 free (e2);
4777 /* Resolve a variable expression. */
4779 static bool
4780 resolve_variable (gfc_expr *e)
4782 gfc_symbol *sym;
4783 bool t;
4785 t = true;
4787 if (e->symtree == NULL)
4788 return false;
4789 sym = e->symtree->n.sym;
4791 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4792 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4793 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4795 if (!actual_arg || inquiry_argument)
4797 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4798 "be used as actual argument", sym->name, &e->where);
4799 return false;
4802 /* TS 29113, 407b. */
4803 else if (e->ts.type == BT_ASSUMED)
4805 if (!actual_arg)
4807 gfc_error ("Assumed-type variable %s at %L may only be used "
4808 "as actual argument", sym->name, &e->where);
4809 return false;
4811 else if (inquiry_argument && !first_actual_arg)
4813 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4814 for all inquiry functions in resolve_function; the reason is
4815 that the function-name resolution happens too late in that
4816 function. */
4817 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4818 "an inquiry function shall be the first argument",
4819 sym->name, &e->where);
4820 return false;
4823 /* TS 29113, C535b. */
4824 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4825 && CLASS_DATA (sym)->as
4826 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4827 || (sym->ts.type != BT_CLASS && sym->as
4828 && sym->as->type == AS_ASSUMED_RANK))
4830 if (!actual_arg)
4832 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4833 "actual argument", sym->name, &e->where);
4834 return false;
4836 else if (inquiry_argument && !first_actual_arg)
4838 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4839 for all inquiry functions in resolve_function; the reason is
4840 that the function-name resolution happens too late in that
4841 function. */
4842 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4843 "to an inquiry function shall be the first argument",
4844 sym->name, &e->where);
4845 return false;
4849 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4850 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4851 && e->ref->next == NULL))
4853 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4854 "a subobject reference", sym->name, &e->ref->u.ar.where);
4855 return false;
4857 /* TS 29113, 407b. */
4858 else if (e->ts.type == BT_ASSUMED && e->ref
4859 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4860 && e->ref->next == NULL))
4862 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4863 "reference", sym->name, &e->ref->u.ar.where);
4864 return false;
4867 /* TS 29113, C535b. */
4868 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4869 && CLASS_DATA (sym)->as
4870 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4871 || (sym->ts.type != BT_CLASS && sym->as
4872 && sym->as->type == AS_ASSUMED_RANK))
4873 && e->ref
4874 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4875 && e->ref->next == NULL))
4877 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4878 "reference", sym->name, &e->ref->u.ar.where);
4879 return false;
4883 /* If this is an associate-name, it may be parsed with an array reference
4884 in error even though the target is scalar. Fail directly in this case.
4885 TODO Understand why class scalar expressions must be excluded. */
4886 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4888 if (sym->ts.type == BT_CLASS)
4889 gfc_fix_class_refs (e);
4890 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4891 return false;
4894 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4895 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4897 /* On the other hand, the parser may not have known this is an array;
4898 in this case, we have to add a FULL reference. */
4899 if (sym->assoc && sym->attr.dimension && !e->ref)
4901 e->ref = gfc_get_ref ();
4902 e->ref->type = REF_ARRAY;
4903 e->ref->u.ar.type = AR_FULL;
4904 e->ref->u.ar.dimen = 0;
4907 if (e->ref && !resolve_ref (e))
4908 return false;
4910 if (sym->attr.flavor == FL_PROCEDURE
4911 && (!sym->attr.function
4912 || (sym->attr.function && sym->result
4913 && sym->result->attr.proc_pointer
4914 && !sym->result->attr.function)))
4916 e->ts.type = BT_PROCEDURE;
4917 goto resolve_procedure;
4920 if (sym->ts.type != BT_UNKNOWN)
4921 gfc_variable_attr (e, &e->ts);
4922 else
4924 /* Must be a simple variable reference. */
4925 if (!gfc_set_default_type (sym, 1, sym->ns))
4926 return false;
4927 e->ts = sym->ts;
4930 if (check_assumed_size_reference (sym, e))
4931 return false;
4933 /* Deal with forward references to entries during resolve_code, to
4934 satisfy, at least partially, 12.5.2.5. */
4935 if (gfc_current_ns->entries
4936 && current_entry_id == sym->entry_id
4937 && cs_base
4938 && cs_base->current
4939 && cs_base->current->op != EXEC_ENTRY)
4941 gfc_entry_list *entry;
4942 gfc_formal_arglist *formal;
4943 int n;
4944 bool seen, saved_specification_expr;
4946 /* If the symbol is a dummy... */
4947 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4949 entry = gfc_current_ns->entries;
4950 seen = false;
4952 /* ...test if the symbol is a parameter of previous entries. */
4953 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4954 for (formal = entry->sym->formal; formal; formal = formal->next)
4956 if (formal->sym && sym->name == formal->sym->name)
4958 seen = true;
4959 break;
4963 /* If it has not been seen as a dummy, this is an error. */
4964 if (!seen)
4966 if (specification_expr)
4967 gfc_error ("Variable '%s', used in a specification expression"
4968 ", is referenced at %L before the ENTRY statement "
4969 "in which it is a parameter",
4970 sym->name, &cs_base->current->loc);
4971 else
4972 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4973 "statement in which it is a parameter",
4974 sym->name, &cs_base->current->loc);
4975 t = false;
4979 /* Now do the same check on the specification expressions. */
4980 saved_specification_expr = specification_expr;
4981 specification_expr = true;
4982 if (sym->ts.type == BT_CHARACTER
4983 && !gfc_resolve_expr (sym->ts.u.cl->length))
4984 t = false;
4986 if (sym->as)
4987 for (n = 0; n < sym->as->rank; n++)
4989 if (!gfc_resolve_expr (sym->as->lower[n]))
4990 t = false;
4991 if (!gfc_resolve_expr (sym->as->upper[n]))
4992 t = false;
4994 specification_expr = saved_specification_expr;
4996 if (t)
4997 /* Update the symbol's entry level. */
4998 sym->entry_id = current_entry_id + 1;
5001 /* If a symbol has been host_associated mark it. This is used latter,
5002 to identify if aliasing is possible via host association. */
5003 if (sym->attr.flavor == FL_VARIABLE
5004 && gfc_current_ns->parent
5005 && (gfc_current_ns->parent == sym->ns
5006 || (gfc_current_ns->parent->parent
5007 && gfc_current_ns->parent->parent == sym->ns)))
5008 sym->attr.host_assoc = 1;
5010 resolve_procedure:
5011 if (t && !resolve_procedure_expression (e))
5012 t = false;
5014 /* F2008, C617 and C1229. */
5015 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5016 && gfc_is_coindexed (e))
5018 gfc_ref *ref, *ref2 = NULL;
5020 for (ref = e->ref; ref; ref = ref->next)
5022 if (ref->type == REF_COMPONENT)
5023 ref2 = ref;
5024 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5025 break;
5028 for ( ; ref; ref = ref->next)
5029 if (ref->type == REF_COMPONENT)
5030 break;
5032 /* Expression itself is not coindexed object. */
5033 if (ref && e->ts.type == BT_CLASS)
5035 gfc_error ("Polymorphic subobject of coindexed object at %L",
5036 &e->where);
5037 t = false;
5040 /* Expression itself is coindexed object. */
5041 if (ref == NULL)
5043 gfc_component *c;
5044 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5045 for ( ; c; c = c->next)
5046 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5048 gfc_error ("Coindexed object with polymorphic allocatable "
5049 "subcomponent at %L", &e->where);
5050 t = false;
5051 break;
5056 if (t)
5057 expression_rank (e);
5059 if (0 && t && gfc_option.coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5060 add_caf_get_intrinsic (e);
5062 return t;
5066 /* Checks to see that the correct symbol has been host associated.
5067 The only situation where this arises is that in which a twice
5068 contained function is parsed after the host association is made.
5069 Therefore, on detecting this, change the symbol in the expression
5070 and convert the array reference into an actual arglist if the old
5071 symbol is a variable. */
5072 static bool
5073 check_host_association (gfc_expr *e)
5075 gfc_symbol *sym, *old_sym;
5076 gfc_symtree *st;
5077 int n;
5078 gfc_ref *ref;
5079 gfc_actual_arglist *arg, *tail = NULL;
5080 bool retval = e->expr_type == EXPR_FUNCTION;
5082 /* If the expression is the result of substitution in
5083 interface.c(gfc_extend_expr) because there is no way in
5084 which the host association can be wrong. */
5085 if (e->symtree == NULL
5086 || e->symtree->n.sym == NULL
5087 || e->user_operator)
5088 return retval;
5090 old_sym = e->symtree->n.sym;
5092 if (gfc_current_ns->parent
5093 && old_sym->ns != gfc_current_ns)
5095 /* Use the 'USE' name so that renamed module symbols are
5096 correctly handled. */
5097 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5099 if (sym && old_sym != sym
5100 && sym->ts.type == old_sym->ts.type
5101 && sym->attr.flavor == FL_PROCEDURE
5102 && sym->attr.contained)
5104 /* Clear the shape, since it might not be valid. */
5105 gfc_free_shape (&e->shape, e->rank);
5107 /* Give the expression the right symtree! */
5108 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5109 gcc_assert (st != NULL);
5111 if (old_sym->attr.flavor == FL_PROCEDURE
5112 || e->expr_type == EXPR_FUNCTION)
5114 /* Original was function so point to the new symbol, since
5115 the actual argument list is already attached to the
5116 expression. */
5117 e->value.function.esym = NULL;
5118 e->symtree = st;
5120 else
5122 /* Original was variable so convert array references into
5123 an actual arglist. This does not need any checking now
5124 since resolve_function will take care of it. */
5125 e->value.function.actual = NULL;
5126 e->expr_type = EXPR_FUNCTION;
5127 e->symtree = st;
5129 /* Ambiguity will not arise if the array reference is not
5130 the last reference. */
5131 for (ref = e->ref; ref; ref = ref->next)
5132 if (ref->type == REF_ARRAY && ref->next == NULL)
5133 break;
5135 gcc_assert (ref->type == REF_ARRAY);
5137 /* Grab the start expressions from the array ref and
5138 copy them into actual arguments. */
5139 for (n = 0; n < ref->u.ar.dimen; n++)
5141 arg = gfc_get_actual_arglist ();
5142 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5143 if (e->value.function.actual == NULL)
5144 tail = e->value.function.actual = arg;
5145 else
5147 tail->next = arg;
5148 tail = arg;
5152 /* Dump the reference list and set the rank. */
5153 gfc_free_ref_list (e->ref);
5154 e->ref = NULL;
5155 e->rank = sym->as ? sym->as->rank : 0;
5158 gfc_resolve_expr (e);
5159 sym->refs++;
5162 /* This might have changed! */
5163 return e->expr_type == EXPR_FUNCTION;
5167 static void
5168 gfc_resolve_character_operator (gfc_expr *e)
5170 gfc_expr *op1 = e->value.op.op1;
5171 gfc_expr *op2 = e->value.op.op2;
5172 gfc_expr *e1 = NULL;
5173 gfc_expr *e2 = NULL;
5175 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5177 if (op1->ts.u.cl && op1->ts.u.cl->length)
5178 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5179 else if (op1->expr_type == EXPR_CONSTANT)
5180 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5181 op1->value.character.length);
5183 if (op2->ts.u.cl && op2->ts.u.cl->length)
5184 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5185 else if (op2->expr_type == EXPR_CONSTANT)
5186 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5187 op2->value.character.length);
5189 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5191 if (!e1 || !e2)
5193 gfc_free_expr (e1);
5194 gfc_free_expr (e2);
5196 return;
5199 e->ts.u.cl->length = gfc_add (e1, e2);
5200 e->ts.u.cl->length->ts.type = BT_INTEGER;
5201 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5202 gfc_simplify_expr (e->ts.u.cl->length, 0);
5203 gfc_resolve_expr (e->ts.u.cl->length);
5205 return;
5209 /* Ensure that an character expression has a charlen and, if possible, a
5210 length expression. */
5212 static void
5213 fixup_charlen (gfc_expr *e)
5215 /* The cases fall through so that changes in expression type and the need
5216 for multiple fixes are picked up. In all circumstances, a charlen should
5217 be available for the middle end to hang a backend_decl on. */
5218 switch (e->expr_type)
5220 case EXPR_OP:
5221 gfc_resolve_character_operator (e);
5223 case EXPR_ARRAY:
5224 if (e->expr_type == EXPR_ARRAY)
5225 gfc_resolve_character_array_constructor (e);
5227 case EXPR_SUBSTRING:
5228 if (!e->ts.u.cl && e->ref)
5229 gfc_resolve_substring_charlen (e);
5231 default:
5232 if (!e->ts.u.cl)
5233 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5235 break;
5240 /* Update an actual argument to include the passed-object for type-bound
5241 procedures at the right position. */
5243 static gfc_actual_arglist*
5244 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5245 const char *name)
5247 gcc_assert (argpos > 0);
5249 if (argpos == 1)
5251 gfc_actual_arglist* result;
5253 result = gfc_get_actual_arglist ();
5254 result->expr = po;
5255 result->next = lst;
5256 if (name)
5257 result->name = name;
5259 return result;
5262 if (lst)
5263 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5264 else
5265 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5266 return lst;
5270 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5272 static gfc_expr*
5273 extract_compcall_passed_object (gfc_expr* e)
5275 gfc_expr* po;
5277 gcc_assert (e->expr_type == EXPR_COMPCALL);
5279 if (e->value.compcall.base_object)
5280 po = gfc_copy_expr (e->value.compcall.base_object);
5281 else
5283 po = gfc_get_expr ();
5284 po->expr_type = EXPR_VARIABLE;
5285 po->symtree = e->symtree;
5286 po->ref = gfc_copy_ref (e->ref);
5287 po->where = e->where;
5290 if (!gfc_resolve_expr (po))
5291 return NULL;
5293 return po;
5297 /* Update the arglist of an EXPR_COMPCALL expression to include the
5298 passed-object. */
5300 static bool
5301 update_compcall_arglist (gfc_expr* e)
5303 gfc_expr* po;
5304 gfc_typebound_proc* tbp;
5306 tbp = e->value.compcall.tbp;
5308 if (tbp->error)
5309 return false;
5311 po = extract_compcall_passed_object (e);
5312 if (!po)
5313 return false;
5315 if (tbp->nopass || e->value.compcall.ignore_pass)
5317 gfc_free_expr (po);
5318 return true;
5321 gcc_assert (tbp->pass_arg_num > 0);
5322 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5323 tbp->pass_arg_num,
5324 tbp->pass_arg);
5326 return true;
5330 /* Extract the passed object from a PPC call (a copy of it). */
5332 static gfc_expr*
5333 extract_ppc_passed_object (gfc_expr *e)
5335 gfc_expr *po;
5336 gfc_ref **ref;
5338 po = gfc_get_expr ();
5339 po->expr_type = EXPR_VARIABLE;
5340 po->symtree = e->symtree;
5341 po->ref = gfc_copy_ref (e->ref);
5342 po->where = e->where;
5344 /* Remove PPC reference. */
5345 ref = &po->ref;
5346 while ((*ref)->next)
5347 ref = &(*ref)->next;
5348 gfc_free_ref_list (*ref);
5349 *ref = NULL;
5351 if (!gfc_resolve_expr (po))
5352 return NULL;
5354 return po;
5358 /* Update the actual arglist of a procedure pointer component to include the
5359 passed-object. */
5361 static bool
5362 update_ppc_arglist (gfc_expr* e)
5364 gfc_expr* po;
5365 gfc_component *ppc;
5366 gfc_typebound_proc* tb;
5368 ppc = gfc_get_proc_ptr_comp (e);
5369 if (!ppc)
5370 return false;
5372 tb = ppc->tb;
5374 if (tb->error)
5375 return false;
5376 else if (tb->nopass)
5377 return true;
5379 po = extract_ppc_passed_object (e);
5380 if (!po)
5381 return false;
5383 /* F08:R739. */
5384 if (po->rank != 0)
5386 gfc_error ("Passed-object at %L must be scalar", &e->where);
5387 return false;
5390 /* F08:C611. */
5391 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5393 gfc_error ("Base object for procedure-pointer component call at %L is of"
5394 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5395 return false;
5398 gcc_assert (tb->pass_arg_num > 0);
5399 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5400 tb->pass_arg_num,
5401 tb->pass_arg);
5403 return true;
5407 /* Check that the object a TBP is called on is valid, i.e. it must not be
5408 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5410 static bool
5411 check_typebound_baseobject (gfc_expr* e)
5413 gfc_expr* base;
5414 bool return_value = false;
5416 base = extract_compcall_passed_object (e);
5417 if (!base)
5418 return false;
5420 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5422 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5423 return false;
5425 /* F08:C611. */
5426 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5428 gfc_error ("Base object for type-bound procedure call at %L is of"
5429 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5430 goto cleanup;
5433 /* F08:C1230. If the procedure called is NOPASS,
5434 the base object must be scalar. */
5435 if (e->value.compcall.tbp->nopass && base->rank != 0)
5437 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5438 " be scalar", &e->where);
5439 goto cleanup;
5442 return_value = true;
5444 cleanup:
5445 gfc_free_expr (base);
5446 return return_value;
5450 /* Resolve a call to a type-bound procedure, either function or subroutine,
5451 statically from the data in an EXPR_COMPCALL expression. The adapted
5452 arglist and the target-procedure symtree are returned. */
5454 static bool
5455 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5456 gfc_actual_arglist** actual)
5458 gcc_assert (e->expr_type == EXPR_COMPCALL);
5459 gcc_assert (!e->value.compcall.tbp->is_generic);
5461 /* Update the actual arglist for PASS. */
5462 if (!update_compcall_arglist (e))
5463 return false;
5465 *actual = e->value.compcall.actual;
5466 *target = e->value.compcall.tbp->u.specific;
5468 gfc_free_ref_list (e->ref);
5469 e->ref = NULL;
5470 e->value.compcall.actual = NULL;
5472 /* If we find a deferred typebound procedure, check for derived types
5473 that an overriding typebound procedure has not been missed. */
5474 if (e->value.compcall.name
5475 && !e->value.compcall.tbp->non_overridable
5476 && e->value.compcall.base_object
5477 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5479 gfc_symtree *st;
5480 gfc_symbol *derived;
5482 /* Use the derived type of the base_object. */
5483 derived = e->value.compcall.base_object->ts.u.derived;
5484 st = NULL;
5486 /* If necessary, go through the inheritance chain. */
5487 while (!st && derived)
5489 /* Look for the typebound procedure 'name'. */
5490 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5491 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5492 e->value.compcall.name);
5493 if (!st)
5494 derived = gfc_get_derived_super_type (derived);
5497 /* Now find the specific name in the derived type namespace. */
5498 if (st && st->n.tb && st->n.tb->u.specific)
5499 gfc_find_sym_tree (st->n.tb->u.specific->name,
5500 derived->ns, 1, &st);
5501 if (st)
5502 *target = st;
5504 return true;
5508 /* Get the ultimate declared type from an expression. In addition,
5509 return the last class/derived type reference and the copy of the
5510 reference list. If check_types is set true, derived types are
5511 identified as well as class references. */
5512 static gfc_symbol*
5513 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5514 gfc_expr *e, bool check_types)
5516 gfc_symbol *declared;
5517 gfc_ref *ref;
5519 declared = NULL;
5520 if (class_ref)
5521 *class_ref = NULL;
5522 if (new_ref)
5523 *new_ref = gfc_copy_ref (e->ref);
5525 for (ref = e->ref; ref; ref = ref->next)
5527 if (ref->type != REF_COMPONENT)
5528 continue;
5530 if ((ref->u.c.component->ts.type == BT_CLASS
5531 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5532 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5534 declared = ref->u.c.component->ts.u.derived;
5535 if (class_ref)
5536 *class_ref = ref;
5540 if (declared == NULL)
5541 declared = e->symtree->n.sym->ts.u.derived;
5543 return declared;
5547 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5548 which of the specific bindings (if any) matches the arglist and transform
5549 the expression into a call of that binding. */
5551 static bool
5552 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5554 gfc_typebound_proc* genproc;
5555 const char* genname;
5556 gfc_symtree *st;
5557 gfc_symbol *derived;
5559 gcc_assert (e->expr_type == EXPR_COMPCALL);
5560 genname = e->value.compcall.name;
5561 genproc = e->value.compcall.tbp;
5563 if (!genproc->is_generic)
5564 return true;
5566 /* Try the bindings on this type and in the inheritance hierarchy. */
5567 for (; genproc; genproc = genproc->overridden)
5569 gfc_tbp_generic* g;
5571 gcc_assert (genproc->is_generic);
5572 for (g = genproc->u.generic; g; g = g->next)
5574 gfc_symbol* target;
5575 gfc_actual_arglist* args;
5576 bool matches;
5578 gcc_assert (g->specific);
5580 if (g->specific->error)
5581 continue;
5583 target = g->specific->u.specific->n.sym;
5585 /* Get the right arglist by handling PASS/NOPASS. */
5586 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5587 if (!g->specific->nopass)
5589 gfc_expr* po;
5590 po = extract_compcall_passed_object (e);
5591 if (!po)
5593 gfc_free_actual_arglist (args);
5594 return false;
5597 gcc_assert (g->specific->pass_arg_num > 0);
5598 gcc_assert (!g->specific->error);
5599 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5600 g->specific->pass_arg);
5602 resolve_actual_arglist (args, target->attr.proc,
5603 is_external_proc (target)
5604 && gfc_sym_get_dummy_args (target) == NULL);
5606 /* Check if this arglist matches the formal. */
5607 matches = gfc_arglist_matches_symbol (&args, target);
5609 /* Clean up and break out of the loop if we've found it. */
5610 gfc_free_actual_arglist (args);
5611 if (matches)
5613 e->value.compcall.tbp = g->specific;
5614 genname = g->specific_st->name;
5615 /* Pass along the name for CLASS methods, where the vtab
5616 procedure pointer component has to be referenced. */
5617 if (name)
5618 *name = genname;
5619 goto success;
5624 /* Nothing matching found! */
5625 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5626 " '%s' at %L", genname, &e->where);
5627 return false;
5629 success:
5630 /* Make sure that we have the right specific instance for the name. */
5631 derived = get_declared_from_expr (NULL, NULL, e, true);
5633 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5634 if (st)
5635 e->value.compcall.tbp = st->n.tb;
5637 return true;
5641 /* Resolve a call to a type-bound subroutine. */
5643 static bool
5644 resolve_typebound_call (gfc_code* c, const char **name)
5646 gfc_actual_arglist* newactual;
5647 gfc_symtree* target;
5649 /* Check that's really a SUBROUTINE. */
5650 if (!c->expr1->value.compcall.tbp->subroutine)
5652 gfc_error ("'%s' at %L should be a SUBROUTINE",
5653 c->expr1->value.compcall.name, &c->loc);
5654 return false;
5657 if (!check_typebound_baseobject (c->expr1))
5658 return false;
5660 /* Pass along the name for CLASS methods, where the vtab
5661 procedure pointer component has to be referenced. */
5662 if (name)
5663 *name = c->expr1->value.compcall.name;
5665 if (!resolve_typebound_generic_call (c->expr1, name))
5666 return false;
5668 /* Transform into an ordinary EXEC_CALL for now. */
5670 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5671 return false;
5673 c->ext.actual = newactual;
5674 c->symtree = target;
5675 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5677 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5679 gfc_free_expr (c->expr1);
5680 c->expr1 = gfc_get_expr ();
5681 c->expr1->expr_type = EXPR_FUNCTION;
5682 c->expr1->symtree = target;
5683 c->expr1->where = c->loc;
5685 return resolve_call (c);
5689 /* Resolve a component-call expression. */
5690 static bool
5691 resolve_compcall (gfc_expr* e, const char **name)
5693 gfc_actual_arglist* newactual;
5694 gfc_symtree* target;
5696 /* Check that's really a FUNCTION. */
5697 if (!e->value.compcall.tbp->function)
5699 gfc_error ("'%s' at %L should be a FUNCTION",
5700 e->value.compcall.name, &e->where);
5701 return false;
5704 /* These must not be assign-calls! */
5705 gcc_assert (!e->value.compcall.assign);
5707 if (!check_typebound_baseobject (e))
5708 return false;
5710 /* Pass along the name for CLASS methods, where the vtab
5711 procedure pointer component has to be referenced. */
5712 if (name)
5713 *name = e->value.compcall.name;
5715 if (!resolve_typebound_generic_call (e, name))
5716 return false;
5717 gcc_assert (!e->value.compcall.tbp->is_generic);
5719 /* Take the rank from the function's symbol. */
5720 if (e->value.compcall.tbp->u.specific->n.sym->as)
5721 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5723 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5724 arglist to the TBP's binding target. */
5726 if (!resolve_typebound_static (e, &target, &newactual))
5727 return false;
5729 e->value.function.actual = newactual;
5730 e->value.function.name = NULL;
5731 e->value.function.esym = target->n.sym;
5732 e->value.function.isym = NULL;
5733 e->symtree = target;
5734 e->ts = target->n.sym->ts;
5735 e->expr_type = EXPR_FUNCTION;
5737 /* Resolution is not necessary if this is a class subroutine; this
5738 function only has to identify the specific proc. Resolution of
5739 the call will be done next in resolve_typebound_call. */
5740 return gfc_resolve_expr (e);
5744 static bool resolve_fl_derived (gfc_symbol *sym);
5747 /* Resolve a typebound function, or 'method'. First separate all
5748 the non-CLASS references by calling resolve_compcall directly. */
5750 static bool
5751 resolve_typebound_function (gfc_expr* e)
5753 gfc_symbol *declared;
5754 gfc_component *c;
5755 gfc_ref *new_ref;
5756 gfc_ref *class_ref;
5757 gfc_symtree *st;
5758 const char *name;
5759 gfc_typespec ts;
5760 gfc_expr *expr;
5761 bool overridable;
5763 st = e->symtree;
5765 /* Deal with typebound operators for CLASS objects. */
5766 expr = e->value.compcall.base_object;
5767 overridable = !e->value.compcall.tbp->non_overridable;
5768 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5770 /* If the base_object is not a variable, the corresponding actual
5771 argument expression must be stored in e->base_expression so
5772 that the corresponding tree temporary can be used as the base
5773 object in gfc_conv_procedure_call. */
5774 if (expr->expr_type != EXPR_VARIABLE)
5776 gfc_actual_arglist *args;
5778 for (args= e->value.function.actual; args; args = args->next)
5780 if (expr == args->expr)
5781 expr = args->expr;
5785 /* Since the typebound operators are generic, we have to ensure
5786 that any delays in resolution are corrected and that the vtab
5787 is present. */
5788 ts = expr->ts;
5789 declared = ts.u.derived;
5790 c = gfc_find_component (declared, "_vptr", true, true);
5791 if (c->ts.u.derived == NULL)
5792 c->ts.u.derived = gfc_find_derived_vtab (declared);
5794 if (!resolve_compcall (e, &name))
5795 return false;
5797 /* Use the generic name if it is there. */
5798 name = name ? name : e->value.function.esym->name;
5799 e->symtree = expr->symtree;
5800 e->ref = gfc_copy_ref (expr->ref);
5801 get_declared_from_expr (&class_ref, NULL, e, false);
5803 /* Trim away the extraneous references that emerge from nested
5804 use of interface.c (extend_expr). */
5805 if (class_ref && class_ref->next)
5807 gfc_free_ref_list (class_ref->next);
5808 class_ref->next = NULL;
5810 else if (e->ref && !class_ref)
5812 gfc_free_ref_list (e->ref);
5813 e->ref = NULL;
5816 gfc_add_vptr_component (e);
5817 gfc_add_component_ref (e, name);
5818 e->value.function.esym = NULL;
5819 if (expr->expr_type != EXPR_VARIABLE)
5820 e->base_expr = expr;
5821 return true;
5824 if (st == NULL)
5825 return resolve_compcall (e, NULL);
5827 if (!resolve_ref (e))
5828 return false;
5830 /* Get the CLASS declared type. */
5831 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5833 if (!resolve_fl_derived (declared))
5834 return false;
5836 /* Weed out cases of the ultimate component being a derived type. */
5837 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5838 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5840 gfc_free_ref_list (new_ref);
5841 return resolve_compcall (e, NULL);
5844 c = gfc_find_component (declared, "_data", true, true);
5845 declared = c->ts.u.derived;
5847 /* Treat the call as if it is a typebound procedure, in order to roll
5848 out the correct name for the specific function. */
5849 if (!resolve_compcall (e, &name))
5851 gfc_free_ref_list (new_ref);
5852 return false;
5854 ts = e->ts;
5856 if (overridable)
5858 /* Convert the expression to a procedure pointer component call. */
5859 e->value.function.esym = NULL;
5860 e->symtree = st;
5862 if (new_ref)
5863 e->ref = new_ref;
5865 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5866 gfc_add_vptr_component (e);
5867 gfc_add_component_ref (e, name);
5869 /* Recover the typespec for the expression. This is really only
5870 necessary for generic procedures, where the additional call
5871 to gfc_add_component_ref seems to throw the collection of the
5872 correct typespec. */
5873 e->ts = ts;
5875 else if (new_ref)
5876 gfc_free_ref_list (new_ref);
5878 return true;
5881 /* Resolve a typebound subroutine, or 'method'. First separate all
5882 the non-CLASS references by calling resolve_typebound_call
5883 directly. */
5885 static bool
5886 resolve_typebound_subroutine (gfc_code *code)
5888 gfc_symbol *declared;
5889 gfc_component *c;
5890 gfc_ref *new_ref;
5891 gfc_ref *class_ref;
5892 gfc_symtree *st;
5893 const char *name;
5894 gfc_typespec ts;
5895 gfc_expr *expr;
5896 bool overridable;
5898 st = code->expr1->symtree;
5900 /* Deal with typebound operators for CLASS objects. */
5901 expr = code->expr1->value.compcall.base_object;
5902 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5903 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5905 /* If the base_object is not a variable, the corresponding actual
5906 argument expression must be stored in e->base_expression so
5907 that the corresponding tree temporary can be used as the base
5908 object in gfc_conv_procedure_call. */
5909 if (expr->expr_type != EXPR_VARIABLE)
5911 gfc_actual_arglist *args;
5913 args= code->expr1->value.function.actual;
5914 for (; args; args = args->next)
5915 if (expr == args->expr)
5916 expr = args->expr;
5919 /* Since the typebound operators are generic, we have to ensure
5920 that any delays in resolution are corrected and that the vtab
5921 is present. */
5922 declared = expr->ts.u.derived;
5923 c = gfc_find_component (declared, "_vptr", true, true);
5924 if (c->ts.u.derived == NULL)
5925 c->ts.u.derived = gfc_find_derived_vtab (declared);
5927 if (!resolve_typebound_call (code, &name))
5928 return false;
5930 /* Use the generic name if it is there. */
5931 name = name ? name : code->expr1->value.function.esym->name;
5932 code->expr1->symtree = expr->symtree;
5933 code->expr1->ref = gfc_copy_ref (expr->ref);
5935 /* Trim away the extraneous references that emerge from nested
5936 use of interface.c (extend_expr). */
5937 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5938 if (class_ref && class_ref->next)
5940 gfc_free_ref_list (class_ref->next);
5941 class_ref->next = NULL;
5943 else if (code->expr1->ref && !class_ref)
5945 gfc_free_ref_list (code->expr1->ref);
5946 code->expr1->ref = NULL;
5949 /* Now use the procedure in the vtable. */
5950 gfc_add_vptr_component (code->expr1);
5951 gfc_add_component_ref (code->expr1, name);
5952 code->expr1->value.function.esym = NULL;
5953 if (expr->expr_type != EXPR_VARIABLE)
5954 code->expr1->base_expr = expr;
5955 return true;
5958 if (st == NULL)
5959 return resolve_typebound_call (code, NULL);
5961 if (!resolve_ref (code->expr1))
5962 return false;
5964 /* Get the CLASS declared type. */
5965 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5967 /* Weed out cases of the ultimate component being a derived type. */
5968 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5969 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5971 gfc_free_ref_list (new_ref);
5972 return resolve_typebound_call (code, NULL);
5975 if (!resolve_typebound_call (code, &name))
5977 gfc_free_ref_list (new_ref);
5978 return false;
5980 ts = code->expr1->ts;
5982 if (overridable)
5984 /* Convert the expression to a procedure pointer component call. */
5985 code->expr1->value.function.esym = NULL;
5986 code->expr1->symtree = st;
5988 if (new_ref)
5989 code->expr1->ref = new_ref;
5991 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5992 gfc_add_vptr_component (code->expr1);
5993 gfc_add_component_ref (code->expr1, name);
5995 /* Recover the typespec for the expression. This is really only
5996 necessary for generic procedures, where the additional call
5997 to gfc_add_component_ref seems to throw the collection of the
5998 correct typespec. */
5999 code->expr1->ts = ts;
6001 else if (new_ref)
6002 gfc_free_ref_list (new_ref);
6004 return true;
6008 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6010 static bool
6011 resolve_ppc_call (gfc_code* c)
6013 gfc_component *comp;
6015 comp = gfc_get_proc_ptr_comp (c->expr1);
6016 gcc_assert (comp != NULL);
6018 c->resolved_sym = c->expr1->symtree->n.sym;
6019 c->expr1->expr_type = EXPR_VARIABLE;
6021 if (!comp->attr.subroutine)
6022 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6024 if (!resolve_ref (c->expr1))
6025 return false;
6027 if (!update_ppc_arglist (c->expr1))
6028 return false;
6030 c->ext.actual = c->expr1->value.compcall.actual;
6032 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6033 !(comp->ts.interface
6034 && comp->ts.interface->formal)))
6035 return false;
6037 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6039 return true;
6043 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6045 static bool
6046 resolve_expr_ppc (gfc_expr* e)
6048 gfc_component *comp;
6050 comp = gfc_get_proc_ptr_comp (e);
6051 gcc_assert (comp != NULL);
6053 /* Convert to EXPR_FUNCTION. */
6054 e->expr_type = EXPR_FUNCTION;
6055 e->value.function.isym = NULL;
6056 e->value.function.actual = e->value.compcall.actual;
6057 e->ts = comp->ts;
6058 if (comp->as != NULL)
6059 e->rank = comp->as->rank;
6061 if (!comp->attr.function)
6062 gfc_add_function (&comp->attr, comp->name, &e->where);
6064 if (!resolve_ref (e))
6065 return false;
6067 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6068 !(comp->ts.interface
6069 && comp->ts.interface->formal)))
6070 return false;
6072 if (!update_ppc_arglist (e))
6073 return false;
6075 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6077 return true;
6081 static bool
6082 gfc_is_expandable_expr (gfc_expr *e)
6084 gfc_constructor *con;
6086 if (e->expr_type == EXPR_ARRAY)
6088 /* Traverse the constructor looking for variables that are flavor
6089 parameter. Parameters must be expanded since they are fully used at
6090 compile time. */
6091 con = gfc_constructor_first (e->value.constructor);
6092 for (; con; con = gfc_constructor_next (con))
6094 if (con->expr->expr_type == EXPR_VARIABLE
6095 && con->expr->symtree
6096 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6097 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6098 return true;
6099 if (con->expr->expr_type == EXPR_ARRAY
6100 && gfc_is_expandable_expr (con->expr))
6101 return true;
6105 return false;
6108 /* Resolve an expression. That is, make sure that types of operands agree
6109 with their operators, intrinsic operators are converted to function calls
6110 for overloaded types and unresolved function references are resolved. */
6112 bool
6113 gfc_resolve_expr (gfc_expr *e)
6115 bool t;
6116 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6118 if (e == NULL)
6119 return true;
6121 /* inquiry_argument only applies to variables. */
6122 inquiry_save = inquiry_argument;
6123 actual_arg_save = actual_arg;
6124 first_actual_arg_save = first_actual_arg;
6126 if (e->expr_type != EXPR_VARIABLE)
6128 inquiry_argument = false;
6129 actual_arg = false;
6130 first_actual_arg = false;
6133 switch (e->expr_type)
6135 case EXPR_OP:
6136 t = resolve_operator (e);
6137 break;
6139 case EXPR_FUNCTION:
6140 case EXPR_VARIABLE:
6142 if (check_host_association (e))
6143 t = resolve_function (e);
6144 else
6145 t = resolve_variable (e);
6147 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6148 && e->ref->type != REF_SUBSTRING)
6149 gfc_resolve_substring_charlen (e);
6151 break;
6153 case EXPR_COMPCALL:
6154 t = resolve_typebound_function (e);
6155 break;
6157 case EXPR_SUBSTRING:
6158 t = resolve_ref (e);
6159 break;
6161 case EXPR_CONSTANT:
6162 case EXPR_NULL:
6163 t = true;
6164 break;
6166 case EXPR_PPC:
6167 t = resolve_expr_ppc (e);
6168 break;
6170 case EXPR_ARRAY:
6171 t = false;
6172 if (!resolve_ref (e))
6173 break;
6175 t = gfc_resolve_array_constructor (e);
6176 /* Also try to expand a constructor. */
6177 if (t)
6179 expression_rank (e);
6180 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6181 gfc_expand_constructor (e, false);
6184 /* This provides the opportunity for the length of constructors with
6185 character valued function elements to propagate the string length
6186 to the expression. */
6187 if (t && e->ts.type == BT_CHARACTER)
6189 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6190 here rather then add a duplicate test for it above. */
6191 gfc_expand_constructor (e, false);
6192 t = gfc_resolve_character_array_constructor (e);
6195 break;
6197 case EXPR_STRUCTURE:
6198 t = resolve_ref (e);
6199 if (!t)
6200 break;
6202 t = resolve_structure_cons (e, 0);
6203 if (!t)
6204 break;
6206 t = gfc_simplify_expr (e, 0);
6207 break;
6209 default:
6210 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6213 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6214 fixup_charlen (e);
6216 inquiry_argument = inquiry_save;
6217 actual_arg = actual_arg_save;
6218 first_actual_arg = first_actual_arg_save;
6220 return t;
6224 /* Resolve an expression from an iterator. They must be scalar and have
6225 INTEGER or (optionally) REAL type. */
6227 static bool
6228 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6229 const char *name_msgid)
6231 if (!gfc_resolve_expr (expr))
6232 return false;
6234 if (expr->rank != 0)
6236 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6237 return false;
6240 if (expr->ts.type != BT_INTEGER)
6242 if (expr->ts.type == BT_REAL)
6244 if (real_ok)
6245 return gfc_notify_std (GFC_STD_F95_DEL,
6246 "%s at %L must be integer",
6247 _(name_msgid), &expr->where);
6248 else
6250 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6251 &expr->where);
6252 return false;
6255 else
6257 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6258 return false;
6261 return true;
6265 /* Resolve the expressions in an iterator structure. If REAL_OK is
6266 false allow only INTEGER type iterators, otherwise allow REAL types.
6267 Set own_scope to true for ac-implied-do and data-implied-do as those
6268 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6270 bool
6271 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6273 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6274 return false;
6276 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6277 _("iterator variable")))
6278 return false;
6280 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6281 "Start expression in DO loop"))
6282 return false;
6284 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6285 "End expression in DO loop"))
6286 return false;
6288 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6289 "Step expression in DO loop"))
6290 return false;
6292 if (iter->step->expr_type == EXPR_CONSTANT)
6294 if ((iter->step->ts.type == BT_INTEGER
6295 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6296 || (iter->step->ts.type == BT_REAL
6297 && mpfr_sgn (iter->step->value.real) == 0))
6299 gfc_error ("Step expression in DO loop at %L cannot be zero",
6300 &iter->step->where);
6301 return false;
6305 /* Convert start, end, and step to the same type as var. */
6306 if (iter->start->ts.kind != iter->var->ts.kind
6307 || iter->start->ts.type != iter->var->ts.type)
6308 gfc_convert_type (iter->start, &iter->var->ts, 2);
6310 if (iter->end->ts.kind != iter->var->ts.kind
6311 || iter->end->ts.type != iter->var->ts.type)
6312 gfc_convert_type (iter->end, &iter->var->ts, 2);
6314 if (iter->step->ts.kind != iter->var->ts.kind
6315 || iter->step->ts.type != iter->var->ts.type)
6316 gfc_convert_type (iter->step, &iter->var->ts, 2);
6318 if (iter->start->expr_type == EXPR_CONSTANT
6319 && iter->end->expr_type == EXPR_CONSTANT
6320 && iter->step->expr_type == EXPR_CONSTANT)
6322 int sgn, cmp;
6323 if (iter->start->ts.type == BT_INTEGER)
6325 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6326 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6328 else
6330 sgn = mpfr_sgn (iter->step->value.real);
6331 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6333 if (gfc_option.warn_zerotrip &&
6334 ((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 (gfc_option.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->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 (gfc_option.warn_surprising && type == BT_LOGICAL
7814 && ncases > 2)
7815 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7816 &code->loc);
7820 /* Check if a derived type is extensible. */
7822 bool
7823 gfc_type_is_extensible (gfc_symbol *sym)
7825 return !(sym->attr.is_bind_c || sym->attr.sequence
7826 || (sym->attr.is_class
7827 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7831 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7832 correct as well as possibly the array-spec. */
7834 static void
7835 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7837 gfc_expr* target;
7839 gcc_assert (sym->assoc);
7840 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7842 /* If this is for SELECT TYPE, the target may not yet be set. In that
7843 case, return. Resolution will be called later manually again when
7844 this is done. */
7845 target = sym->assoc->target;
7846 if (!target)
7847 return;
7848 gcc_assert (!sym->assoc->dangling);
7850 if (resolve_target && !gfc_resolve_expr (target))
7851 return;
7853 /* For variable targets, we get some attributes from the target. */
7854 if (target->expr_type == EXPR_VARIABLE)
7856 gfc_symbol* tsym;
7858 gcc_assert (target->symtree);
7859 tsym = target->symtree->n.sym;
7861 sym->attr.asynchronous = tsym->attr.asynchronous;
7862 sym->attr.volatile_ = tsym->attr.volatile_;
7864 sym->attr.target = tsym->attr.target
7865 || gfc_expr_attr (target).pointer;
7866 if (is_subref_array (target))
7867 sym->attr.subref_array_pointer = 1;
7870 /* Get type if this was not already set. Note that it can be
7871 some other type than the target in case this is a SELECT TYPE
7872 selector! So we must not update when the type is already there. */
7873 if (sym->ts.type == BT_UNKNOWN)
7874 sym->ts = target->ts;
7875 gcc_assert (sym->ts.type != BT_UNKNOWN);
7877 /* See if this is a valid association-to-variable. */
7878 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7879 && !gfc_has_vector_subscript (target));
7881 /* Finally resolve if this is an array or not. */
7882 if (sym->attr.dimension && target->rank == 0)
7884 gfc_error ("Associate-name '%s' at %L is used as array",
7885 sym->name, &sym->declared_at);
7886 sym->attr.dimension = 0;
7887 return;
7890 /* We cannot deal with class selectors that need temporaries. */
7891 if (target->ts.type == BT_CLASS
7892 && gfc_ref_needs_temporary_p (target->ref))
7894 gfc_error ("CLASS selector at %L needs a temporary which is not "
7895 "yet implemented", &target->where);
7896 return;
7899 if (target->ts.type != BT_CLASS && target->rank > 0)
7900 sym->attr.dimension = 1;
7901 else if (target->ts.type == BT_CLASS)
7902 gfc_fix_class_refs (target);
7904 /* The associate-name will have a correct type by now. Make absolutely
7905 sure that it has not picked up a dimension attribute. */
7906 if (sym->ts.type == BT_CLASS)
7907 sym->attr.dimension = 0;
7909 if (sym->attr.dimension)
7911 sym->as = gfc_get_array_spec ();
7912 sym->as->rank = target->rank;
7913 sym->as->type = AS_DEFERRED;
7915 /* Target must not be coindexed, thus the associate-variable
7916 has no corank. */
7917 sym->as->corank = 0;
7920 /* Mark this as an associate variable. */
7921 sym->attr.associate_var = 1;
7923 /* If the target is a good class object, so is the associate variable. */
7924 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7925 sym->attr.class_ok = 1;
7929 /* Resolve a SELECT TYPE statement. */
7931 static void
7932 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7934 gfc_symbol *selector_type;
7935 gfc_code *body, *new_st, *if_st, *tail;
7936 gfc_code *class_is = NULL, *default_case = NULL;
7937 gfc_case *c;
7938 gfc_symtree *st;
7939 char name[GFC_MAX_SYMBOL_LEN];
7940 gfc_namespace *ns;
7941 int error = 0;
7942 int charlen = 0;
7944 ns = code->ext.block.ns;
7945 gfc_resolve (ns);
7947 /* Check for F03:C813. */
7948 if (code->expr1->ts.type != BT_CLASS
7949 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7951 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7952 "at %L", &code->loc);
7953 return;
7956 if (!code->expr1->symtree->n.sym->attr.class_ok)
7957 return;
7959 if (code->expr2)
7961 if (code->expr1->symtree->n.sym->attr.untyped)
7962 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7963 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7965 /* F2008: C803 The selector expression must not be coindexed. */
7966 if (gfc_is_coindexed (code->expr2))
7968 gfc_error ("Selector at %L must not be coindexed",
7969 &code->expr2->where);
7970 return;
7974 else
7976 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7978 if (gfc_is_coindexed (code->expr1))
7980 gfc_error ("Selector at %L must not be coindexed",
7981 &code->expr1->where);
7982 return;
7986 /* Loop over TYPE IS / CLASS IS cases. */
7987 for (body = code->block; body; body = body->block)
7989 c = body->ext.block.case_list;
7991 /* Check F03:C815. */
7992 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7993 && !selector_type->attr.unlimited_polymorphic
7994 && !gfc_type_is_extensible (c->ts.u.derived))
7996 gfc_error ("Derived type '%s' at %L must be extensible",
7997 c->ts.u.derived->name, &c->where);
7998 error++;
7999 continue;
8002 /* Check F03:C816. */
8003 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8004 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8005 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8007 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8008 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
8009 c->ts.u.derived->name, &c->where, selector_type->name);
8010 else
8011 gfc_error ("Unexpected intrinsic type '%s' at %L",
8012 gfc_basic_typename (c->ts.type), &c->where);
8013 error++;
8014 continue;
8017 /* Check F03:C814. */
8018 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8020 gfc_error ("The type-spec at %L shall specify that each length "
8021 "type parameter is assumed", &c->where);
8022 error++;
8023 continue;
8026 /* Intercept the DEFAULT case. */
8027 if (c->ts.type == BT_UNKNOWN)
8029 /* Check F03:C818. */
8030 if (default_case)
8032 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8033 "by a second DEFAULT CASE at %L",
8034 &default_case->ext.block.case_list->where, &c->where);
8035 error++;
8036 continue;
8039 default_case = body;
8043 if (error > 0)
8044 return;
8046 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8047 target if present. If there are any EXIT statements referring to the
8048 SELECT TYPE construct, this is no problem because the gfc_code
8049 reference stays the same and EXIT is equally possible from the BLOCK
8050 it is changed to. */
8051 code->op = EXEC_BLOCK;
8052 if (code->expr2)
8054 gfc_association_list* assoc;
8056 assoc = gfc_get_association_list ();
8057 assoc->st = code->expr1->symtree;
8058 assoc->target = gfc_copy_expr (code->expr2);
8059 assoc->target->where = code->expr2->where;
8060 /* assoc->variable will be set by resolve_assoc_var. */
8062 code->ext.block.assoc = assoc;
8063 code->expr1->symtree->n.sym->assoc = assoc;
8065 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8067 else
8068 code->ext.block.assoc = NULL;
8070 /* Add EXEC_SELECT to switch on type. */
8071 new_st = gfc_get_code (code->op);
8072 new_st->expr1 = code->expr1;
8073 new_st->expr2 = code->expr2;
8074 new_st->block = code->block;
8075 code->expr1 = code->expr2 = NULL;
8076 code->block = NULL;
8077 if (!ns->code)
8078 ns->code = new_st;
8079 else
8080 ns->code->next = new_st;
8081 code = new_st;
8082 code->op = EXEC_SELECT;
8084 gfc_add_vptr_component (code->expr1);
8085 gfc_add_hash_component (code->expr1);
8087 /* Loop over TYPE IS / CLASS IS cases. */
8088 for (body = code->block; body; body = body->block)
8090 c = body->ext.block.case_list;
8092 if (c->ts.type == BT_DERIVED)
8093 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8094 c->ts.u.derived->hash_value);
8095 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8097 gfc_symbol *ivtab;
8098 gfc_expr *e;
8100 ivtab = gfc_find_vtab (&c->ts);
8101 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8102 e = CLASS_DATA (ivtab)->initializer;
8103 c->low = c->high = gfc_copy_expr (e);
8106 else if (c->ts.type == BT_UNKNOWN)
8107 continue;
8109 /* Associate temporary to selector. This should only be done
8110 when this case is actually true, so build a new ASSOCIATE
8111 that does precisely this here (instead of using the
8112 'global' one). */
8114 if (c->ts.type == BT_CLASS)
8115 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8116 else if (c->ts.type == BT_DERIVED)
8117 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8118 else if (c->ts.type == BT_CHARACTER)
8120 if (c->ts.u.cl && c->ts.u.cl->length
8121 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8122 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8123 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8124 charlen, c->ts.kind);
8126 else
8127 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8128 c->ts.kind);
8130 st = gfc_find_symtree (ns->sym_root, name);
8131 gcc_assert (st->n.sym->assoc);
8132 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8133 st->n.sym->assoc->target->where = code->expr1->where;
8134 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8135 gfc_add_data_component (st->n.sym->assoc->target);
8137 new_st = gfc_get_code (EXEC_BLOCK);
8138 new_st->ext.block.ns = gfc_build_block_ns (ns);
8139 new_st->ext.block.ns->code = body->next;
8140 body->next = new_st;
8142 /* Chain in the new list only if it is marked as dangling. Otherwise
8143 there is a CASE label overlap and this is already used. Just ignore,
8144 the error is diagnosed elsewhere. */
8145 if (st->n.sym->assoc->dangling)
8147 new_st->ext.block.assoc = st->n.sym->assoc;
8148 st->n.sym->assoc->dangling = 0;
8151 resolve_assoc_var (st->n.sym, false);
8154 /* Take out CLASS IS cases for separate treatment. */
8155 body = code;
8156 while (body && body->block)
8158 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8160 /* Add to class_is list. */
8161 if (class_is == NULL)
8163 class_is = body->block;
8164 tail = class_is;
8166 else
8168 for (tail = class_is; tail->block; tail = tail->block) ;
8169 tail->block = body->block;
8170 tail = tail->block;
8172 /* Remove from EXEC_SELECT list. */
8173 body->block = body->block->block;
8174 tail->block = NULL;
8176 else
8177 body = body->block;
8180 if (class_is)
8182 gfc_symbol *vtab;
8184 if (!default_case)
8186 /* Add a default case to hold the CLASS IS cases. */
8187 for (tail = code; tail->block; tail = tail->block) ;
8188 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8189 tail = tail->block;
8190 tail->ext.block.case_list = gfc_get_case ();
8191 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8192 tail->next = NULL;
8193 default_case = tail;
8196 /* More than one CLASS IS block? */
8197 if (class_is->block)
8199 gfc_code **c1,*c2;
8200 bool swapped;
8201 /* Sort CLASS IS blocks by extension level. */
8204 swapped = false;
8205 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8207 c2 = (*c1)->block;
8208 /* F03:C817 (check for doubles). */
8209 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8210 == c2->ext.block.case_list->ts.u.derived->hash_value)
8212 gfc_error ("Double CLASS IS block in SELECT TYPE "
8213 "statement at %L",
8214 &c2->ext.block.case_list->where);
8215 return;
8217 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8218 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8220 /* Swap. */
8221 (*c1)->block = c2->block;
8222 c2->block = *c1;
8223 *c1 = c2;
8224 swapped = true;
8228 while (swapped);
8231 /* Generate IF chain. */
8232 if_st = gfc_get_code (EXEC_IF);
8233 new_st = if_st;
8234 for (body = class_is; body; body = body->block)
8236 new_st->block = gfc_get_code (EXEC_IF);
8237 new_st = new_st->block;
8238 /* Set up IF condition: Call _gfortran_is_extension_of. */
8239 new_st->expr1 = gfc_get_expr ();
8240 new_st->expr1->expr_type = EXPR_FUNCTION;
8241 new_st->expr1->ts.type = BT_LOGICAL;
8242 new_st->expr1->ts.kind = 4;
8243 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8244 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8245 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8246 /* Set up arguments. */
8247 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8248 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8249 new_st->expr1->value.function.actual->expr->where = code->loc;
8250 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8251 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8252 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8253 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8254 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8255 new_st->next = body->next;
8257 if (default_case->next)
8259 new_st->block = gfc_get_code (EXEC_IF);
8260 new_st = new_st->block;
8261 new_st->next = default_case->next;
8264 /* Replace CLASS DEFAULT code by the IF chain. */
8265 default_case->next = if_st;
8268 /* Resolve the internal code. This can not be done earlier because
8269 it requires that the sym->assoc of selectors is set already. */
8270 gfc_current_ns = ns;
8271 gfc_resolve_blocks (code->block, gfc_current_ns);
8272 gfc_current_ns = old_ns;
8274 resolve_select (code, true);
8278 /* Resolve a transfer statement. This is making sure that:
8279 -- a derived type being transferred has only non-pointer components
8280 -- a derived type being transferred doesn't have private components, unless
8281 it's being transferred from the module where the type was defined
8282 -- we're not trying to transfer a whole assumed size array. */
8284 static void
8285 resolve_transfer (gfc_code *code)
8287 gfc_typespec *ts;
8288 gfc_symbol *sym;
8289 gfc_ref *ref;
8290 gfc_expr *exp;
8292 exp = code->expr1;
8294 while (exp != NULL && exp->expr_type == EXPR_OP
8295 && exp->value.op.op == INTRINSIC_PARENTHESES)
8296 exp = exp->value.op.op1;
8298 if (exp && exp->expr_type == EXPR_NULL
8299 && code->ext.dt)
8301 gfc_error ("Invalid context for NULL () intrinsic at %L",
8302 &exp->where);
8303 return;
8306 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8307 && exp->expr_type != EXPR_FUNCTION))
8308 return;
8310 /* If we are reading, the variable will be changed. Note that
8311 code->ext.dt may be NULL if the TRANSFER is related to
8312 an INQUIRE statement -- but in this case, we are not reading, either. */
8313 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8314 && !gfc_check_vardef_context (exp, false, false, false,
8315 _("item in READ")))
8316 return;
8318 sym = exp->symtree->n.sym;
8319 ts = &sym->ts;
8321 /* Go to actual component transferred. */
8322 for (ref = exp->ref; ref; ref = ref->next)
8323 if (ref->type == REF_COMPONENT)
8324 ts = &ref->u.c.component->ts;
8326 if (ts->type == BT_CLASS)
8328 /* FIXME: Test for defined input/output. */
8329 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8330 "it is processed by a defined input/output procedure",
8331 &code->loc);
8332 return;
8335 if (ts->type == BT_DERIVED)
8337 /* Check that transferred derived type doesn't contain POINTER
8338 components. */
8339 if (ts->u.derived->attr.pointer_comp)
8341 gfc_error ("Data transfer element at %L cannot have POINTER "
8342 "components unless it is processed by a defined "
8343 "input/output procedure", &code->loc);
8344 return;
8347 /* F08:C935. */
8348 if (ts->u.derived->attr.proc_pointer_comp)
8350 gfc_error ("Data transfer element at %L cannot have "
8351 "procedure pointer components", &code->loc);
8352 return;
8355 if (ts->u.derived->attr.alloc_comp)
8357 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8358 "components unless it is processed by a defined "
8359 "input/output procedure", &code->loc);
8360 return;
8363 /* C_PTR and C_FUNPTR have private components which means they can not
8364 be printed. However, if -std=gnu and not -pedantic, allow
8365 the component to be printed to help debugging. */
8366 if (ts->u.derived->ts.f90_type == BT_VOID)
8368 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8369 "cannot have PRIVATE components", &code->loc))
8370 return;
8372 else if (derived_inaccessible (ts->u.derived))
8374 gfc_error ("Data transfer element at %L cannot have "
8375 "PRIVATE components",&code->loc);
8376 return;
8380 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8381 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8383 gfc_error ("Data transfer element at %L cannot be a full reference to "
8384 "an assumed-size array", &code->loc);
8385 return;
8390 /*********** Toplevel code resolution subroutines ***********/
8392 /* Find the set of labels that are reachable from this block. We also
8393 record the last statement in each block. */
8395 static void
8396 find_reachable_labels (gfc_code *block)
8398 gfc_code *c;
8400 if (!block)
8401 return;
8403 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8405 /* Collect labels in this block. We don't keep those corresponding
8406 to END {IF|SELECT}, these are checked in resolve_branch by going
8407 up through the code_stack. */
8408 for (c = block; c; c = c->next)
8410 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8411 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8414 /* Merge with labels from parent block. */
8415 if (cs_base->prev)
8417 gcc_assert (cs_base->prev->reachable_labels);
8418 bitmap_ior_into (cs_base->reachable_labels,
8419 cs_base->prev->reachable_labels);
8424 static void
8425 resolve_lock_unlock (gfc_code *code)
8427 if (code->expr1->ts.type != BT_DERIVED
8428 || code->expr1->expr_type != EXPR_VARIABLE
8429 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8430 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8431 || code->expr1->rank != 0
8432 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8433 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8434 &code->expr1->where);
8436 /* Check STAT. */
8437 if (code->expr2
8438 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8439 || code->expr2->expr_type != EXPR_VARIABLE))
8440 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8441 &code->expr2->where);
8443 if (code->expr2
8444 && !gfc_check_vardef_context (code->expr2, false, false, false,
8445 _("STAT variable")))
8446 return;
8448 /* Check ERRMSG. */
8449 if (code->expr3
8450 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8451 || code->expr3->expr_type != EXPR_VARIABLE))
8452 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8453 &code->expr3->where);
8455 if (code->expr3
8456 && !gfc_check_vardef_context (code->expr3, false, false, false,
8457 _("ERRMSG variable")))
8458 return;
8460 /* Check ACQUIRED_LOCK. */
8461 if (code->expr4
8462 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8463 || code->expr4->expr_type != EXPR_VARIABLE))
8464 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8465 "variable", &code->expr4->where);
8467 if (code->expr4
8468 && !gfc_check_vardef_context (code->expr4, false, false, false,
8469 _("ACQUIRED_LOCK variable")))
8470 return;
8474 static void
8475 resolve_sync (gfc_code *code)
8477 /* Check imageset. The * case matches expr1 == NULL. */
8478 if (code->expr1)
8480 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8481 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8482 "INTEGER expression", &code->expr1->where);
8483 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8484 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8485 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8486 &code->expr1->where);
8487 else if (code->expr1->expr_type == EXPR_ARRAY
8488 && gfc_simplify_expr (code->expr1, 0))
8490 gfc_constructor *cons;
8491 cons = gfc_constructor_first (code->expr1->value.constructor);
8492 for (; cons; cons = gfc_constructor_next (cons))
8493 if (cons->expr->expr_type == EXPR_CONSTANT
8494 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8495 gfc_error ("Imageset argument at %L must between 1 and "
8496 "num_images()", &cons->expr->where);
8500 /* Check STAT. */
8501 if (code->expr2
8502 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8503 || code->expr2->expr_type != EXPR_VARIABLE))
8504 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8505 &code->expr2->where);
8507 /* Check ERRMSG. */
8508 if (code->expr3
8509 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8510 || code->expr3->expr_type != EXPR_VARIABLE))
8511 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8512 &code->expr3->where);
8516 /* Given a branch to a label, see if the branch is conforming.
8517 The code node describes where the branch is located. */
8519 static void
8520 resolve_branch (gfc_st_label *label, gfc_code *code)
8522 code_stack *stack;
8524 if (label == NULL)
8525 return;
8527 /* Step one: is this a valid branching target? */
8529 if (label->defined == ST_LABEL_UNKNOWN)
8531 gfc_error ("Label %d referenced at %L is never defined", label->value,
8532 &label->where);
8533 return;
8536 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8538 gfc_error ("Statement at %L is not a valid branch target statement "
8539 "for the branch statement at %L", &label->where, &code->loc);
8540 return;
8543 /* Step two: make sure this branch is not a branch to itself ;-) */
8545 if (code->here == label)
8547 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8548 return;
8551 /* Step three: See if the label is in the same block as the
8552 branching statement. The hard work has been done by setting up
8553 the bitmap reachable_labels. */
8555 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8557 /* Check now whether there is a CRITICAL construct; if so, check
8558 whether the label is still visible outside of the CRITICAL block,
8559 which is invalid. */
8560 for (stack = cs_base; stack; stack = stack->prev)
8562 if (stack->current->op == EXEC_CRITICAL
8563 && bitmap_bit_p (stack->reachable_labels, label->value))
8564 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8565 "label at %L", &code->loc, &label->where);
8566 else if (stack->current->op == EXEC_DO_CONCURRENT
8567 && bitmap_bit_p (stack->reachable_labels, label->value))
8568 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8569 "for label at %L", &code->loc, &label->where);
8572 return;
8575 /* Step four: If we haven't found the label in the bitmap, it may
8576 still be the label of the END of the enclosing block, in which
8577 case we find it by going up the code_stack. */
8579 for (stack = cs_base; stack; stack = stack->prev)
8581 if (stack->current->next && stack->current->next->here == label)
8582 break;
8583 if (stack->current->op == EXEC_CRITICAL)
8585 /* Note: A label at END CRITICAL does not leave the CRITICAL
8586 construct as END CRITICAL is still part of it. */
8587 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8588 " at %L", &code->loc, &label->where);
8589 return;
8591 else if (stack->current->op == EXEC_DO_CONCURRENT)
8593 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8594 "label at %L", &code->loc, &label->where);
8595 return;
8599 if (stack)
8601 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8602 return;
8605 /* The label is not in an enclosing block, so illegal. This was
8606 allowed in Fortran 66, so we allow it as extension. No
8607 further checks are necessary in this case. */
8608 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8609 "as the GOTO statement at %L", &label->where,
8610 &code->loc);
8611 return;
8615 /* Check whether EXPR1 has the same shape as EXPR2. */
8617 static bool
8618 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8620 mpz_t shape[GFC_MAX_DIMENSIONS];
8621 mpz_t shape2[GFC_MAX_DIMENSIONS];
8622 bool result = false;
8623 int i;
8625 /* Compare the rank. */
8626 if (expr1->rank != expr2->rank)
8627 return result;
8629 /* Compare the size of each dimension. */
8630 for (i=0; i<expr1->rank; i++)
8632 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8633 goto ignore;
8635 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8636 goto ignore;
8638 if (mpz_cmp (shape[i], shape2[i]))
8639 goto over;
8642 /* When either of the two expression is an assumed size array, we
8643 ignore the comparison of dimension sizes. */
8644 ignore:
8645 result = true;
8647 over:
8648 gfc_clear_shape (shape, i);
8649 gfc_clear_shape (shape2, i);
8650 return result;
8654 /* Check whether a WHERE assignment target or a WHERE mask expression
8655 has the same shape as the outmost WHERE mask expression. */
8657 static void
8658 resolve_where (gfc_code *code, gfc_expr *mask)
8660 gfc_code *cblock;
8661 gfc_code *cnext;
8662 gfc_expr *e = NULL;
8664 cblock = code->block;
8666 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8667 In case of nested WHERE, only the outmost one is stored. */
8668 if (mask == NULL) /* outmost WHERE */
8669 e = cblock->expr1;
8670 else /* inner WHERE */
8671 e = mask;
8673 while (cblock)
8675 if (cblock->expr1)
8677 /* Check if the mask-expr has a consistent shape with the
8678 outmost WHERE mask-expr. */
8679 if (!resolve_where_shape (cblock->expr1, e))
8680 gfc_error ("WHERE mask at %L has inconsistent shape",
8681 &cblock->expr1->where);
8684 /* the assignment statement of a WHERE statement, or the first
8685 statement in where-body-construct of a WHERE construct */
8686 cnext = cblock->next;
8687 while (cnext)
8689 switch (cnext->op)
8691 /* WHERE assignment statement */
8692 case EXEC_ASSIGN:
8694 /* Check shape consistent for WHERE assignment target. */
8695 if (e && !resolve_where_shape (cnext->expr1, e))
8696 gfc_error ("WHERE assignment target at %L has "
8697 "inconsistent shape", &cnext->expr1->where);
8698 break;
8701 case EXEC_ASSIGN_CALL:
8702 resolve_call (cnext);
8703 if (!cnext->resolved_sym->attr.elemental)
8704 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8705 &cnext->ext.actual->expr->where);
8706 break;
8708 /* WHERE or WHERE construct is part of a where-body-construct */
8709 case EXEC_WHERE:
8710 resolve_where (cnext, e);
8711 break;
8713 default:
8714 gfc_error ("Unsupported statement inside WHERE at %L",
8715 &cnext->loc);
8717 /* the next statement within the same where-body-construct */
8718 cnext = cnext->next;
8720 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8721 cblock = cblock->block;
8726 /* Resolve assignment in FORALL construct.
8727 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8728 FORALL index variables. */
8730 static void
8731 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8733 int n;
8735 for (n = 0; n < nvar; n++)
8737 gfc_symbol *forall_index;
8739 forall_index = var_expr[n]->symtree->n.sym;
8741 /* Check whether the assignment target is one of the FORALL index
8742 variable. */
8743 if ((code->expr1->expr_type == EXPR_VARIABLE)
8744 && (code->expr1->symtree->n.sym == forall_index))
8745 gfc_error ("Assignment to a FORALL index variable at %L",
8746 &code->expr1->where);
8747 else
8749 /* If one of the FORALL index variables doesn't appear in the
8750 assignment variable, then there could be a many-to-one
8751 assignment. Emit a warning rather than an error because the
8752 mask could be resolving this problem. */
8753 if (!find_forall_index (code->expr1, forall_index, 0))
8754 gfc_warning ("The FORALL with index '%s' is not used on the "
8755 "left side of the assignment at %L and so might "
8756 "cause multiple assignment to this object",
8757 var_expr[n]->symtree->name, &code->expr1->where);
8763 /* Resolve WHERE statement in FORALL construct. */
8765 static void
8766 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8767 gfc_expr **var_expr)
8769 gfc_code *cblock;
8770 gfc_code *cnext;
8772 cblock = code->block;
8773 while (cblock)
8775 /* the assignment statement of a WHERE statement, or the first
8776 statement in where-body-construct of a WHERE construct */
8777 cnext = cblock->next;
8778 while (cnext)
8780 switch (cnext->op)
8782 /* WHERE assignment statement */
8783 case EXEC_ASSIGN:
8784 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8785 break;
8787 /* WHERE operator assignment statement */
8788 case EXEC_ASSIGN_CALL:
8789 resolve_call (cnext);
8790 if (!cnext->resolved_sym->attr.elemental)
8791 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8792 &cnext->ext.actual->expr->where);
8793 break;
8795 /* WHERE or WHERE construct is part of a where-body-construct */
8796 case EXEC_WHERE:
8797 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8798 break;
8800 default:
8801 gfc_error ("Unsupported statement inside WHERE at %L",
8802 &cnext->loc);
8804 /* the next statement within the same where-body-construct */
8805 cnext = cnext->next;
8807 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8808 cblock = cblock->block;
8813 /* Traverse the FORALL body to check whether the following errors exist:
8814 1. For assignment, check if a many-to-one assignment happens.
8815 2. For WHERE statement, check the WHERE body to see if there is any
8816 many-to-one assignment. */
8818 static void
8819 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8821 gfc_code *c;
8823 c = code->block->next;
8824 while (c)
8826 switch (c->op)
8828 case EXEC_ASSIGN:
8829 case EXEC_POINTER_ASSIGN:
8830 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8831 break;
8833 case EXEC_ASSIGN_CALL:
8834 resolve_call (c);
8835 break;
8837 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8838 there is no need to handle it here. */
8839 case EXEC_FORALL:
8840 break;
8841 case EXEC_WHERE:
8842 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8843 break;
8844 default:
8845 break;
8847 /* The next statement in the FORALL body. */
8848 c = c->next;
8853 /* Counts the number of iterators needed inside a forall construct, including
8854 nested forall constructs. This is used to allocate the needed memory
8855 in gfc_resolve_forall. */
8857 static int
8858 gfc_count_forall_iterators (gfc_code *code)
8860 int max_iters, sub_iters, current_iters;
8861 gfc_forall_iterator *fa;
8863 gcc_assert(code->op == EXEC_FORALL);
8864 max_iters = 0;
8865 current_iters = 0;
8867 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8868 current_iters ++;
8870 code = code->block->next;
8872 while (code)
8874 if (code->op == EXEC_FORALL)
8876 sub_iters = gfc_count_forall_iterators (code);
8877 if (sub_iters > max_iters)
8878 max_iters = sub_iters;
8880 code = code->next;
8883 return current_iters + max_iters;
8887 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8888 gfc_resolve_forall_body to resolve the FORALL body. */
8890 static void
8891 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8893 static gfc_expr **var_expr;
8894 static int total_var = 0;
8895 static int nvar = 0;
8896 int old_nvar, tmp;
8897 gfc_forall_iterator *fa;
8898 int i;
8900 old_nvar = nvar;
8902 /* Start to resolve a FORALL construct */
8903 if (forall_save == 0)
8905 /* Count the total number of FORALL index in the nested FORALL
8906 construct in order to allocate the VAR_EXPR with proper size. */
8907 total_var = gfc_count_forall_iterators (code);
8909 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8910 var_expr = XCNEWVEC (gfc_expr *, total_var);
8913 /* The information about FORALL iterator, including FORALL index start, end
8914 and stride. The FORALL index can not appear in start, end or stride. */
8915 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8917 /* Check if any outer FORALL index name is the same as the current
8918 one. */
8919 for (i = 0; i < nvar; i++)
8921 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8923 gfc_error ("An outer FORALL construct already has an index "
8924 "with this name %L", &fa->var->where);
8928 /* Record the current FORALL index. */
8929 var_expr[nvar] = gfc_copy_expr (fa->var);
8931 nvar++;
8933 /* No memory leak. */
8934 gcc_assert (nvar <= total_var);
8937 /* Resolve the FORALL body. */
8938 gfc_resolve_forall_body (code, nvar, var_expr);
8940 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8941 gfc_resolve_blocks (code->block, ns);
8943 tmp = nvar;
8944 nvar = old_nvar;
8945 /* Free only the VAR_EXPRs allocated in this frame. */
8946 for (i = nvar; i < tmp; i++)
8947 gfc_free_expr (var_expr[i]);
8949 if (nvar == 0)
8951 /* We are in the outermost FORALL construct. */
8952 gcc_assert (forall_save == 0);
8954 /* VAR_EXPR is not needed any more. */
8955 free (var_expr);
8956 total_var = 0;
8961 /* Resolve a BLOCK construct statement. */
8963 static void
8964 resolve_block_construct (gfc_code* code)
8966 /* Resolve the BLOCK's namespace. */
8967 gfc_resolve (code->ext.block.ns);
8969 /* For an ASSOCIATE block, the associations (and their targets) are already
8970 resolved during resolve_symbol. */
8974 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8975 DO code nodes. */
8977 static void resolve_code (gfc_code *, gfc_namespace *);
8979 void
8980 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8982 bool t;
8984 for (; b; b = b->block)
8986 t = gfc_resolve_expr (b->expr1);
8987 if (!gfc_resolve_expr (b->expr2))
8988 t = false;
8990 switch (b->op)
8992 case EXEC_IF:
8993 if (t && b->expr1 != NULL
8994 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8995 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8996 &b->expr1->where);
8997 break;
8999 case EXEC_WHERE:
9000 if (t
9001 && b->expr1 != NULL
9002 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9003 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9004 &b->expr1->where);
9005 break;
9007 case EXEC_GOTO:
9008 resolve_branch (b->label1, b);
9009 break;
9011 case EXEC_BLOCK:
9012 resolve_block_construct (b);
9013 break;
9015 case EXEC_SELECT:
9016 case EXEC_SELECT_TYPE:
9017 case EXEC_FORALL:
9018 case EXEC_DO:
9019 case EXEC_DO_WHILE:
9020 case EXEC_DO_CONCURRENT:
9021 case EXEC_CRITICAL:
9022 case EXEC_READ:
9023 case EXEC_WRITE:
9024 case EXEC_IOLENGTH:
9025 case EXEC_WAIT:
9026 break;
9028 case EXEC_OMP_ATOMIC:
9029 case EXEC_OMP_CRITICAL:
9030 case EXEC_OMP_DO:
9031 case EXEC_OMP_DO_SIMD:
9032 case EXEC_OMP_MASTER:
9033 case EXEC_OMP_ORDERED:
9034 case EXEC_OMP_PARALLEL:
9035 case EXEC_OMP_PARALLEL_DO:
9036 case EXEC_OMP_PARALLEL_DO_SIMD:
9037 case EXEC_OMP_PARALLEL_SECTIONS:
9038 case EXEC_OMP_PARALLEL_WORKSHARE:
9039 case EXEC_OMP_SECTIONS:
9040 case EXEC_OMP_SIMD:
9041 case EXEC_OMP_SINGLE:
9042 case EXEC_OMP_TASK:
9043 case EXEC_OMP_TASKGROUP:
9044 case EXEC_OMP_TASKWAIT:
9045 case EXEC_OMP_TASKYIELD:
9046 case EXEC_OMP_WORKSHARE:
9047 break;
9049 default:
9050 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9053 resolve_code (b->next, ns);
9058 /* Does everything to resolve an ordinary assignment. Returns true
9059 if this is an interface assignment. */
9060 static bool
9061 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9063 bool rval = false;
9064 gfc_expr *lhs;
9065 gfc_expr *rhs;
9066 int llen = 0;
9067 int rlen = 0;
9068 int n;
9069 gfc_ref *ref;
9070 symbol_attribute attr;
9072 if (gfc_extend_assign (code, ns))
9074 gfc_expr** rhsptr;
9076 if (code->op == EXEC_ASSIGN_CALL)
9078 lhs = code->ext.actual->expr;
9079 rhsptr = &code->ext.actual->next->expr;
9081 else
9083 gfc_actual_arglist* args;
9084 gfc_typebound_proc* tbp;
9086 gcc_assert (code->op == EXEC_COMPCALL);
9088 args = code->expr1->value.compcall.actual;
9089 lhs = args->expr;
9090 rhsptr = &args->next->expr;
9092 tbp = code->expr1->value.compcall.tbp;
9093 gcc_assert (!tbp->is_generic);
9096 /* Make a temporary rhs when there is a default initializer
9097 and rhs is the same symbol as the lhs. */
9098 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9099 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9100 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9101 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9102 *rhsptr = gfc_get_parentheses (*rhsptr);
9104 return true;
9107 lhs = code->expr1;
9108 rhs = code->expr2;
9110 if (rhs->is_boz
9111 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9112 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9113 &code->loc))
9114 return false;
9116 /* Handle the case of a BOZ literal on the RHS. */
9117 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9119 int rc;
9120 if (gfc_option.warn_surprising)
9121 gfc_warning ("BOZ literal at %L is bitwise transferred "
9122 "non-integer symbol '%s'", &code->loc,
9123 lhs->symtree->n.sym->name);
9125 if (!gfc_convert_boz (rhs, &lhs->ts))
9126 return false;
9127 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9129 if (rc == ARITH_UNDERFLOW)
9130 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9131 ". This check can be disabled with the option "
9132 "-fno-range-check", &rhs->where);
9133 else if (rc == ARITH_OVERFLOW)
9134 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9135 ". This check can be disabled with the option "
9136 "-fno-range-check", &rhs->where);
9137 else if (rc == ARITH_NAN)
9138 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9139 ". This check can be disabled with the option "
9140 "-fno-range-check", &rhs->where);
9141 return false;
9145 if (lhs->ts.type == BT_CHARACTER
9146 && gfc_option.warn_character_truncation)
9148 if (lhs->ts.u.cl != NULL
9149 && lhs->ts.u.cl->length != NULL
9150 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9151 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9153 if (rhs->expr_type == EXPR_CONSTANT)
9154 rlen = rhs->value.character.length;
9156 else if (rhs->ts.u.cl != NULL
9157 && rhs->ts.u.cl->length != NULL
9158 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9159 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9161 if (rlen && llen && rlen > llen)
9162 gfc_warning_now ("CHARACTER expression will be truncated "
9163 "in assignment (%d/%d) at %L",
9164 llen, rlen, &code->loc);
9167 /* Ensure that a vector index expression for the lvalue is evaluated
9168 to a temporary if the lvalue symbol is referenced in it. */
9169 if (lhs->rank)
9171 for (ref = lhs->ref; ref; ref= ref->next)
9172 if (ref->type == REF_ARRAY)
9174 for (n = 0; n < ref->u.ar.dimen; n++)
9175 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9176 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9177 ref->u.ar.start[n]))
9178 ref->u.ar.start[n]
9179 = gfc_get_parentheses (ref->u.ar.start[n]);
9183 if (gfc_pure (NULL))
9185 if (lhs->ts.type == BT_DERIVED
9186 && lhs->expr_type == EXPR_VARIABLE
9187 && lhs->ts.u.derived->attr.pointer_comp
9188 && rhs->expr_type == EXPR_VARIABLE
9189 && (gfc_impure_variable (rhs->symtree->n.sym)
9190 || gfc_is_coindexed (rhs)))
9192 /* F2008, C1283. */
9193 if (gfc_is_coindexed (rhs))
9194 gfc_error ("Coindexed expression at %L is assigned to "
9195 "a derived type variable with a POINTER "
9196 "component in a PURE procedure",
9197 &rhs->where);
9198 else
9199 gfc_error ("The impure variable at %L is assigned to "
9200 "a derived type variable with a POINTER "
9201 "component in a PURE procedure (12.6)",
9202 &rhs->where);
9203 return rval;
9206 /* Fortran 2008, C1283. */
9207 if (gfc_is_coindexed (lhs))
9209 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9210 "procedure", &rhs->where);
9211 return rval;
9215 if (gfc_implicit_pure (NULL))
9217 if (lhs->expr_type == EXPR_VARIABLE
9218 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9219 && lhs->symtree->n.sym->ns != gfc_current_ns)
9220 gfc_unset_implicit_pure (NULL);
9222 if (lhs->ts.type == BT_DERIVED
9223 && lhs->expr_type == EXPR_VARIABLE
9224 && lhs->ts.u.derived->attr.pointer_comp
9225 && rhs->expr_type == EXPR_VARIABLE
9226 && (gfc_impure_variable (rhs->symtree->n.sym)
9227 || gfc_is_coindexed (rhs)))
9228 gfc_unset_implicit_pure (NULL);
9230 /* Fortran 2008, C1283. */
9231 if (gfc_is_coindexed (lhs))
9232 gfc_unset_implicit_pure (NULL);
9235 /* F2008, 7.2.1.2. */
9236 attr = gfc_expr_attr (lhs);
9237 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9239 if (attr.codimension)
9241 gfc_error ("Assignment to polymorphic coarray at %L is not "
9242 "permitted", &lhs->where);
9243 return false;
9245 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9246 "polymorphic variable at %L", &lhs->where))
9247 return false;
9248 if (!gfc_option.flag_realloc_lhs)
9250 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9251 "requires -frealloc-lhs", &lhs->where);
9252 return false;
9254 /* See PR 43366. */
9255 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9256 "is not yet supported", &lhs->where);
9257 return false;
9259 else if (lhs->ts.type == BT_CLASS)
9261 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9262 "assignment at %L - check that there is a matching specific "
9263 "subroutine for '=' operator", &lhs->where);
9264 return false;
9267 bool lhs_coindexed = gfc_is_coindexed (lhs);
9269 /* F2008, Section 7.2.1.2. */
9270 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9272 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9273 "component in assignment at %L", &lhs->where);
9274 return false;
9277 gfc_check_assign (lhs, rhs, 1);
9279 if (0 && lhs_coindexed && gfc_option.coarray == GFC_FCOARRAY_LIB)
9281 code->op = EXEC_CALL;
9282 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9283 code->resolved_sym = code->symtree->n.sym;
9284 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9285 code->resolved_sym->attr.intrinsic = 1;
9286 code->resolved_sym->attr.subroutine = 1;
9287 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9288 gfc_commit_symbol (code->resolved_sym);
9289 code->ext.actual = gfc_get_actual_arglist ();
9290 code->ext.actual->expr = lhs;
9291 code->ext.actual->next = gfc_get_actual_arglist ();
9292 code->ext.actual->next->expr = rhs;
9293 code->expr1 = NULL;
9294 code->expr2 = NULL;
9297 return false;
9301 /* Add a component reference onto an expression. */
9303 static void
9304 add_comp_ref (gfc_expr *e, gfc_component *c)
9306 gfc_ref **ref;
9307 ref = &(e->ref);
9308 while (*ref)
9309 ref = &((*ref)->next);
9310 *ref = gfc_get_ref ();
9311 (*ref)->type = REF_COMPONENT;
9312 (*ref)->u.c.sym = e->ts.u.derived;
9313 (*ref)->u.c.component = c;
9314 e->ts = c->ts;
9316 /* Add a full array ref, as necessary. */
9317 if (c->as)
9319 gfc_add_full_array_ref (e, c->as);
9320 e->rank = c->as->rank;
9325 /* Build an assignment. Keep the argument 'op' for future use, so that
9326 pointer assignments can be made. */
9328 static gfc_code *
9329 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9330 gfc_component *comp1, gfc_component *comp2, locus loc)
9332 gfc_code *this_code;
9334 this_code = gfc_get_code (op);
9335 this_code->next = NULL;
9336 this_code->expr1 = gfc_copy_expr (expr1);
9337 this_code->expr2 = gfc_copy_expr (expr2);
9338 this_code->loc = loc;
9339 if (comp1 && comp2)
9341 add_comp_ref (this_code->expr1, comp1);
9342 add_comp_ref (this_code->expr2, comp2);
9345 return this_code;
9349 /* Makes a temporary variable expression based on the characteristics of
9350 a given variable expression. */
9352 static gfc_expr*
9353 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9355 static int serial = 0;
9356 char name[GFC_MAX_SYMBOL_LEN];
9357 gfc_symtree *tmp;
9358 gfc_array_spec *as;
9359 gfc_array_ref *aref;
9360 gfc_ref *ref;
9362 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9363 gfc_get_sym_tree (name, ns, &tmp, false);
9364 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9366 as = NULL;
9367 ref = NULL;
9368 aref = NULL;
9370 /* This function could be expanded to support other expression type
9371 but this is not needed here. */
9372 gcc_assert (e->expr_type == EXPR_VARIABLE);
9374 /* Obtain the arrayspec for the temporary. */
9375 if (e->rank)
9377 aref = gfc_find_array_ref (e);
9378 if (e->expr_type == EXPR_VARIABLE
9379 && e->symtree->n.sym->as == aref->as)
9380 as = aref->as;
9381 else
9383 for (ref = e->ref; ref; ref = ref->next)
9384 if (ref->type == REF_COMPONENT
9385 && ref->u.c.component->as == aref->as)
9387 as = aref->as;
9388 break;
9393 /* Add the attributes and the arrayspec to the temporary. */
9394 tmp->n.sym->attr = gfc_expr_attr (e);
9395 tmp->n.sym->attr.function = 0;
9396 tmp->n.sym->attr.result = 0;
9397 tmp->n.sym->attr.flavor = FL_VARIABLE;
9399 if (as)
9401 tmp->n.sym->as = gfc_copy_array_spec (as);
9402 if (!ref)
9403 ref = e->ref;
9404 if (as->type == AS_DEFERRED)
9405 tmp->n.sym->attr.allocatable = 1;
9407 else
9408 tmp->n.sym->attr.dimension = 0;
9410 gfc_set_sym_referenced (tmp->n.sym);
9411 gfc_commit_symbol (tmp->n.sym);
9412 e = gfc_lval_expr_from_sym (tmp->n.sym);
9414 /* Should the lhs be a section, use its array ref for the
9415 temporary expression. */
9416 if (aref && aref->type != AR_FULL)
9418 gfc_free_ref_list (e->ref);
9419 e->ref = gfc_copy_ref (ref);
9421 return e;
9425 /* Add one line of code to the code chain, making sure that 'head' and
9426 'tail' are appropriately updated. */
9428 static void
9429 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9431 gcc_assert (this_code);
9432 if (*head == NULL)
9433 *head = *tail = *this_code;
9434 else
9435 *tail = gfc_append_code (*tail, *this_code);
9436 *this_code = NULL;
9440 /* Counts the potential number of part array references that would
9441 result from resolution of typebound defined assignments. */
9443 static int
9444 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9446 gfc_component *c;
9447 int c_depth = 0, t_depth;
9449 for (c= derived->components; c; c = c->next)
9451 if ((c->ts.type != BT_DERIVED
9452 || c->attr.pointer
9453 || c->attr.allocatable
9454 || c->attr.proc_pointer_comp
9455 || c->attr.class_pointer
9456 || c->attr.proc_pointer)
9457 && !c->attr.defined_assign_comp)
9458 continue;
9460 if (c->as && c_depth == 0)
9461 c_depth = 1;
9463 if (c->ts.u.derived->attr.defined_assign_comp)
9464 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9465 c->as ? 1 : 0);
9466 else
9467 t_depth = 0;
9469 c_depth = t_depth > c_depth ? t_depth : c_depth;
9471 return depth + c_depth;
9475 /* Implement 7.2.1.3 of the F08 standard:
9476 "An intrinsic assignment where the variable is of derived type is
9477 performed as if each component of the variable were assigned from the
9478 corresponding component of expr using pointer assignment (7.2.2) for
9479 each pointer component, defined assignment for each nonpointer
9480 nonallocatable component of a type that has a type-bound defined
9481 assignment consistent with the component, intrinsic assignment for
9482 each other nonpointer nonallocatable component, ..."
9484 The pointer assignments are taken care of by the intrinsic
9485 assignment of the structure itself. This function recursively adds
9486 defined assignments where required. The recursion is accomplished
9487 by calling resolve_code.
9489 When the lhs in a defined assignment has intent INOUT, we need a
9490 temporary for the lhs. In pseudo-code:
9492 ! Only call function lhs once.
9493 if (lhs is not a constant or an variable)
9494 temp_x = expr2
9495 expr2 => temp_x
9496 ! Do the intrinsic assignment
9497 expr1 = expr2
9498 ! Now do the defined assignments
9499 do over components with typebound defined assignment [%cmp]
9500 #if one component's assignment procedure is INOUT
9501 t1 = expr1
9502 #if expr2 non-variable
9503 temp_x = expr2
9504 expr2 => temp_x
9505 # endif
9506 expr1 = expr2
9507 # for each cmp
9508 t1%cmp {defined=} expr2%cmp
9509 expr1%cmp = t1%cmp
9510 #else
9511 expr1 = expr2
9513 # for each cmp
9514 expr1%cmp {defined=} expr2%cmp
9515 #endif
9518 /* The temporary assignments have to be put on top of the additional
9519 code to avoid the result being changed by the intrinsic assignment.
9521 static int component_assignment_level = 0;
9522 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9524 static void
9525 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9527 gfc_component *comp1, *comp2;
9528 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9529 gfc_expr *t1;
9530 int error_count, depth;
9532 gfc_get_errors (NULL, &error_count);
9534 /* Filter out continuing processing after an error. */
9535 if (error_count
9536 || (*code)->expr1->ts.type != BT_DERIVED
9537 || (*code)->expr2->ts.type != BT_DERIVED)
9538 return;
9540 /* TODO: Handle more than one part array reference in assignments. */
9541 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9542 (*code)->expr1->rank ? 1 : 0);
9543 if (depth > 1)
9545 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9546 "done because multiple part array references would "
9547 "occur in intermediate expressions.", &(*code)->loc);
9548 return;
9551 component_assignment_level++;
9553 /* Create a temporary so that functions get called only once. */
9554 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9555 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9557 gfc_expr *tmp_expr;
9559 /* Assign the rhs to the temporary. */
9560 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9561 this_code = build_assignment (EXEC_ASSIGN,
9562 tmp_expr, (*code)->expr2,
9563 NULL, NULL, (*code)->loc);
9564 /* Add the code and substitute the rhs expression. */
9565 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9566 gfc_free_expr ((*code)->expr2);
9567 (*code)->expr2 = tmp_expr;
9570 /* Do the intrinsic assignment. This is not needed if the lhs is one
9571 of the temporaries generated here, since the intrinsic assignment
9572 to the final result already does this. */
9573 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9575 this_code = build_assignment (EXEC_ASSIGN,
9576 (*code)->expr1, (*code)->expr2,
9577 NULL, NULL, (*code)->loc);
9578 add_code_to_chain (&this_code, &head, &tail);
9581 comp1 = (*code)->expr1->ts.u.derived->components;
9582 comp2 = (*code)->expr2->ts.u.derived->components;
9584 t1 = NULL;
9585 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9587 bool inout = false;
9589 /* The intrinsic assignment does the right thing for pointers
9590 of all kinds and allocatable components. */
9591 if (comp1->ts.type != BT_DERIVED
9592 || comp1->attr.pointer
9593 || comp1->attr.allocatable
9594 || comp1->attr.proc_pointer_comp
9595 || comp1->attr.class_pointer
9596 || comp1->attr.proc_pointer)
9597 continue;
9599 /* Make an assigment for this component. */
9600 this_code = build_assignment (EXEC_ASSIGN,
9601 (*code)->expr1, (*code)->expr2,
9602 comp1, comp2, (*code)->loc);
9604 /* Convert the assignment if there is a defined assignment for
9605 this type. Otherwise, using the call from resolve_code,
9606 recurse into its components. */
9607 resolve_code (this_code, ns);
9609 if (this_code->op == EXEC_ASSIGN_CALL)
9611 gfc_formal_arglist *dummy_args;
9612 gfc_symbol *rsym;
9613 /* Check that there is a typebound defined assignment. If not,
9614 then this must be a module defined assignment. We cannot
9615 use the defined_assign_comp attribute here because it must
9616 be this derived type that has the defined assignment and not
9617 a parent type. */
9618 if (!(comp1->ts.u.derived->f2k_derived
9619 && comp1->ts.u.derived->f2k_derived
9620 ->tb_op[INTRINSIC_ASSIGN]))
9622 gfc_free_statements (this_code);
9623 this_code = NULL;
9624 continue;
9627 /* If the first argument of the subroutine has intent INOUT
9628 a temporary must be generated and used instead. */
9629 rsym = this_code->resolved_sym;
9630 dummy_args = gfc_sym_get_dummy_args (rsym);
9631 if (dummy_args
9632 && dummy_args->sym->attr.intent == INTENT_INOUT)
9634 gfc_code *temp_code;
9635 inout = true;
9637 /* Build the temporary required for the assignment and put
9638 it at the head of the generated code. */
9639 if (!t1)
9641 t1 = get_temp_from_expr ((*code)->expr1, ns);
9642 temp_code = build_assignment (EXEC_ASSIGN,
9643 t1, (*code)->expr1,
9644 NULL, NULL, (*code)->loc);
9646 /* For allocatable LHS, check whether it is allocated. Note
9647 that allocatable components with defined assignment are
9648 not yet support. See PR 57696. */
9649 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
9651 gfc_code *block;
9652 gfc_expr *e =
9653 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9654 block = gfc_get_code (EXEC_IF);
9655 block->block = gfc_get_code (EXEC_IF);
9656 block->block->expr1
9657 = gfc_build_intrinsic_call (ns,
9658 GFC_ISYM_ALLOCATED, "allocated",
9659 (*code)->loc, 1, e);
9660 block->block->next = temp_code;
9661 temp_code = block;
9663 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9666 /* Replace the first actual arg with the component of the
9667 temporary. */
9668 gfc_free_expr (this_code->ext.actual->expr);
9669 this_code->ext.actual->expr = gfc_copy_expr (t1);
9670 add_comp_ref (this_code->ext.actual->expr, comp1);
9672 /* If the LHS variable is allocatable and wasn't allocated and
9673 the temporary is allocatable, pointer assign the address of
9674 the freshly allocated LHS to the temporary. */
9675 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9676 && gfc_expr_attr ((*code)->expr1).allocatable)
9678 gfc_code *block;
9679 gfc_expr *cond;
9681 cond = gfc_get_expr ();
9682 cond->ts.type = BT_LOGICAL;
9683 cond->ts.kind = gfc_default_logical_kind;
9684 cond->expr_type = EXPR_OP;
9685 cond->where = (*code)->loc;
9686 cond->value.op.op = INTRINSIC_NOT;
9687 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
9688 GFC_ISYM_ALLOCATED, "allocated",
9689 (*code)->loc, 1, gfc_copy_expr (t1));
9690 block = gfc_get_code (EXEC_IF);
9691 block->block = gfc_get_code (EXEC_IF);
9692 block->block->expr1 = cond;
9693 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9694 t1, (*code)->expr1,
9695 NULL, NULL, (*code)->loc);
9696 add_code_to_chain (&block, &head, &tail);
9700 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9702 /* Don't add intrinsic assignments since they are already
9703 effected by the intrinsic assignment of the structure. */
9704 gfc_free_statements (this_code);
9705 this_code = NULL;
9706 continue;
9709 add_code_to_chain (&this_code, &head, &tail);
9711 if (t1 && inout)
9713 /* Transfer the value to the final result. */
9714 this_code = build_assignment (EXEC_ASSIGN,
9715 (*code)->expr1, t1,
9716 comp1, comp2, (*code)->loc);
9717 add_code_to_chain (&this_code, &head, &tail);
9721 /* Put the temporary assignments at the top of the generated code. */
9722 if (tmp_head && component_assignment_level == 1)
9724 gfc_append_code (tmp_head, head);
9725 head = tmp_head;
9726 tmp_head = tmp_tail = NULL;
9729 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9730 // not accidentally deallocated. Hence, nullify t1.
9731 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9732 && gfc_expr_attr ((*code)->expr1).allocatable)
9734 gfc_code *block;
9735 gfc_expr *cond;
9736 gfc_expr *e;
9738 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9739 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9740 (*code)->loc, 2, gfc_copy_expr (t1), e);
9741 block = gfc_get_code (EXEC_IF);
9742 block->block = gfc_get_code (EXEC_IF);
9743 block->block->expr1 = cond;
9744 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9745 t1, gfc_get_null_expr (&(*code)->loc),
9746 NULL, NULL, (*code)->loc);
9747 gfc_append_code (tail, block);
9748 tail = block;
9751 /* Now attach the remaining code chain to the input code. Step on
9752 to the end of the new code since resolution is complete. */
9753 gcc_assert ((*code)->op == EXEC_ASSIGN);
9754 tail->next = (*code)->next;
9755 /* Overwrite 'code' because this would place the intrinsic assignment
9756 before the temporary for the lhs is created. */
9757 gfc_free_expr ((*code)->expr1);
9758 gfc_free_expr ((*code)->expr2);
9759 **code = *head;
9760 if (head != tail)
9761 free (head);
9762 *code = tail;
9764 component_assignment_level--;
9768 /* Given a block of code, recursively resolve everything pointed to by this
9769 code block. */
9771 static void
9772 resolve_code (gfc_code *code, gfc_namespace *ns)
9774 int omp_workshare_save;
9775 int forall_save, do_concurrent_save;
9776 code_stack frame;
9777 bool t;
9779 frame.prev = cs_base;
9780 frame.head = code;
9781 cs_base = &frame;
9783 find_reachable_labels (code);
9785 for (; code; code = code->next)
9787 frame.current = code;
9788 forall_save = forall_flag;
9789 do_concurrent_save = gfc_do_concurrent_flag;
9791 if (code->op == EXEC_FORALL)
9793 forall_flag = 1;
9794 gfc_resolve_forall (code, ns, forall_save);
9795 forall_flag = 2;
9797 else if (code->block)
9799 omp_workshare_save = -1;
9800 switch (code->op)
9802 case EXEC_OMP_PARALLEL_WORKSHARE:
9803 omp_workshare_save = omp_workshare_flag;
9804 omp_workshare_flag = 1;
9805 gfc_resolve_omp_parallel_blocks (code, ns);
9806 break;
9807 case EXEC_OMP_PARALLEL:
9808 case EXEC_OMP_PARALLEL_DO:
9809 case EXEC_OMP_PARALLEL_DO_SIMD:
9810 case EXEC_OMP_PARALLEL_SECTIONS:
9811 case EXEC_OMP_TASK:
9812 omp_workshare_save = omp_workshare_flag;
9813 omp_workshare_flag = 0;
9814 gfc_resolve_omp_parallel_blocks (code, ns);
9815 break;
9816 case EXEC_OMP_DO:
9817 case EXEC_OMP_DO_SIMD:
9818 case EXEC_OMP_SIMD:
9819 gfc_resolve_omp_do_blocks (code, ns);
9820 break;
9821 case EXEC_SELECT_TYPE:
9822 /* Blocks are handled in resolve_select_type because we have
9823 to transform the SELECT TYPE into ASSOCIATE first. */
9824 break;
9825 case EXEC_DO_CONCURRENT:
9826 gfc_do_concurrent_flag = 1;
9827 gfc_resolve_blocks (code->block, ns);
9828 gfc_do_concurrent_flag = 2;
9829 break;
9830 case EXEC_OMP_WORKSHARE:
9831 omp_workshare_save = omp_workshare_flag;
9832 omp_workshare_flag = 1;
9833 /* FALL THROUGH */
9834 default:
9835 gfc_resolve_blocks (code->block, ns);
9836 break;
9839 if (omp_workshare_save != -1)
9840 omp_workshare_flag = omp_workshare_save;
9843 t = true;
9844 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9845 t = gfc_resolve_expr (code->expr1);
9846 forall_flag = forall_save;
9847 gfc_do_concurrent_flag = do_concurrent_save;
9849 if (!gfc_resolve_expr (code->expr2))
9850 t = false;
9852 if (code->op == EXEC_ALLOCATE
9853 && !gfc_resolve_expr (code->expr3))
9854 t = false;
9856 switch (code->op)
9858 case EXEC_NOP:
9859 case EXEC_END_BLOCK:
9860 case EXEC_END_NESTED_BLOCK:
9861 case EXEC_CYCLE:
9862 case EXEC_PAUSE:
9863 case EXEC_STOP:
9864 case EXEC_ERROR_STOP:
9865 case EXEC_EXIT:
9866 case EXEC_CONTINUE:
9867 case EXEC_DT_END:
9868 case EXEC_ASSIGN_CALL:
9869 case EXEC_CRITICAL:
9870 break;
9872 case EXEC_SYNC_ALL:
9873 case EXEC_SYNC_IMAGES:
9874 case EXEC_SYNC_MEMORY:
9875 resolve_sync (code);
9876 break;
9878 case EXEC_LOCK:
9879 case EXEC_UNLOCK:
9880 resolve_lock_unlock (code);
9881 break;
9883 case EXEC_ENTRY:
9884 /* Keep track of which entry we are up to. */
9885 current_entry_id = code->ext.entry->id;
9886 break;
9888 case EXEC_WHERE:
9889 resolve_where (code, NULL);
9890 break;
9892 case EXEC_GOTO:
9893 if (code->expr1 != NULL)
9895 if (code->expr1->ts.type != BT_INTEGER)
9896 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9897 "INTEGER variable", &code->expr1->where);
9898 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9899 gfc_error ("Variable '%s' has not been assigned a target "
9900 "label at %L", code->expr1->symtree->n.sym->name,
9901 &code->expr1->where);
9903 else
9904 resolve_branch (code->label1, code);
9905 break;
9907 case EXEC_RETURN:
9908 if (code->expr1 != NULL
9909 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9910 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9911 "INTEGER return specifier", &code->expr1->where);
9912 break;
9914 case EXEC_INIT_ASSIGN:
9915 case EXEC_END_PROCEDURE:
9916 break;
9918 case EXEC_ASSIGN:
9919 if (!t)
9920 break;
9922 if (code->expr1->expr_type == EXPR_FUNCTION
9923 && code->expr1->value.function.isym
9924 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
9925 remove_caf_get_intrinsic (code->expr1);
9927 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9928 _("assignment")))
9929 break;
9931 if (resolve_ordinary_assign (code, ns))
9933 if (code->op == EXEC_COMPCALL)
9934 goto compcall;
9935 else
9936 goto call;
9939 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9940 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
9941 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9942 generate_component_assignments (&code, ns);
9944 break;
9946 case EXEC_LABEL_ASSIGN:
9947 if (code->label1->defined == ST_LABEL_UNKNOWN)
9948 gfc_error ("Label %d referenced at %L is never defined",
9949 code->label1->value, &code->label1->where);
9950 if (t
9951 && (code->expr1->expr_type != EXPR_VARIABLE
9952 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9953 || code->expr1->symtree->n.sym->ts.kind
9954 != gfc_default_integer_kind
9955 || code->expr1->symtree->n.sym->as != NULL))
9956 gfc_error ("ASSIGN statement at %L requires a scalar "
9957 "default INTEGER variable", &code->expr1->where);
9958 break;
9960 case EXEC_POINTER_ASSIGN:
9962 gfc_expr* e;
9964 if (!t)
9965 break;
9967 /* This is both a variable definition and pointer assignment
9968 context, so check both of them. For rank remapping, a final
9969 array ref may be present on the LHS and fool gfc_expr_attr
9970 used in gfc_check_vardef_context. Remove it. */
9971 e = remove_last_array_ref (code->expr1);
9972 t = gfc_check_vardef_context (e, true, false, false,
9973 _("pointer assignment"));
9974 if (t)
9975 t = gfc_check_vardef_context (e, false, false, false,
9976 _("pointer assignment"));
9977 gfc_free_expr (e);
9978 if (!t)
9979 break;
9981 gfc_check_pointer_assign (code->expr1, code->expr2);
9982 break;
9985 case EXEC_ARITHMETIC_IF:
9986 if (t
9987 && code->expr1->ts.type != BT_INTEGER
9988 && code->expr1->ts.type != BT_REAL)
9989 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9990 "expression", &code->expr1->where);
9992 resolve_branch (code->label1, code);
9993 resolve_branch (code->label2, code);
9994 resolve_branch (code->label3, code);
9995 break;
9997 case EXEC_IF:
9998 if (t && code->expr1 != NULL
9999 && (code->expr1->ts.type != BT_LOGICAL
10000 || code->expr1->rank != 0))
10001 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10002 &code->expr1->where);
10003 break;
10005 case EXEC_CALL:
10006 call:
10007 resolve_call (code);
10008 break;
10010 case EXEC_COMPCALL:
10011 compcall:
10012 resolve_typebound_subroutine (code);
10013 break;
10015 case EXEC_CALL_PPC:
10016 resolve_ppc_call (code);
10017 break;
10019 case EXEC_SELECT:
10020 /* Select is complicated. Also, a SELECT construct could be
10021 a transformed computed GOTO. */
10022 resolve_select (code, false);
10023 break;
10025 case EXEC_SELECT_TYPE:
10026 resolve_select_type (code, ns);
10027 break;
10029 case EXEC_BLOCK:
10030 resolve_block_construct (code);
10031 break;
10033 case EXEC_DO:
10034 if (code->ext.iterator != NULL)
10036 gfc_iterator *iter = code->ext.iterator;
10037 if (gfc_resolve_iterator (iter, true, false))
10038 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10040 break;
10042 case EXEC_DO_WHILE:
10043 if (code->expr1 == NULL)
10044 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
10045 if (t
10046 && (code->expr1->rank != 0
10047 || code->expr1->ts.type != BT_LOGICAL))
10048 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10049 "a scalar LOGICAL expression", &code->expr1->where);
10050 break;
10052 case EXEC_ALLOCATE:
10053 if (t)
10054 resolve_allocate_deallocate (code, "ALLOCATE");
10056 break;
10058 case EXEC_DEALLOCATE:
10059 if (t)
10060 resolve_allocate_deallocate (code, "DEALLOCATE");
10062 break;
10064 case EXEC_OPEN:
10065 if (!gfc_resolve_open (code->ext.open))
10066 break;
10068 resolve_branch (code->ext.open->err, code);
10069 break;
10071 case EXEC_CLOSE:
10072 if (!gfc_resolve_close (code->ext.close))
10073 break;
10075 resolve_branch (code->ext.close->err, code);
10076 break;
10078 case EXEC_BACKSPACE:
10079 case EXEC_ENDFILE:
10080 case EXEC_REWIND:
10081 case EXEC_FLUSH:
10082 if (!gfc_resolve_filepos (code->ext.filepos))
10083 break;
10085 resolve_branch (code->ext.filepos->err, code);
10086 break;
10088 case EXEC_INQUIRE:
10089 if (!gfc_resolve_inquire (code->ext.inquire))
10090 break;
10092 resolve_branch (code->ext.inquire->err, code);
10093 break;
10095 case EXEC_IOLENGTH:
10096 gcc_assert (code->ext.inquire != NULL);
10097 if (!gfc_resolve_inquire (code->ext.inquire))
10098 break;
10100 resolve_branch (code->ext.inquire->err, code);
10101 break;
10103 case EXEC_WAIT:
10104 if (!gfc_resolve_wait (code->ext.wait))
10105 break;
10107 resolve_branch (code->ext.wait->err, code);
10108 resolve_branch (code->ext.wait->end, code);
10109 resolve_branch (code->ext.wait->eor, code);
10110 break;
10112 case EXEC_READ:
10113 case EXEC_WRITE:
10114 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10115 break;
10117 resolve_branch (code->ext.dt->err, code);
10118 resolve_branch (code->ext.dt->end, code);
10119 resolve_branch (code->ext.dt->eor, code);
10120 break;
10122 case EXEC_TRANSFER:
10123 resolve_transfer (code);
10124 break;
10126 case EXEC_DO_CONCURRENT:
10127 case EXEC_FORALL:
10128 resolve_forall_iterators (code->ext.forall_iterator);
10130 if (code->expr1 != NULL
10131 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10132 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10133 "expression", &code->expr1->where);
10134 break;
10136 case EXEC_OMP_ATOMIC:
10137 case EXEC_OMP_BARRIER:
10138 case EXEC_OMP_CANCEL:
10139 case EXEC_OMP_CANCELLATION_POINT:
10140 case EXEC_OMP_CRITICAL:
10141 case EXEC_OMP_FLUSH:
10142 case EXEC_OMP_DO:
10143 case EXEC_OMP_DO_SIMD:
10144 case EXEC_OMP_MASTER:
10145 case EXEC_OMP_ORDERED:
10146 case EXEC_OMP_SECTIONS:
10147 case EXEC_OMP_SIMD:
10148 case EXEC_OMP_SINGLE:
10149 case EXEC_OMP_TASKGROUP:
10150 case EXEC_OMP_TASKWAIT:
10151 case EXEC_OMP_TASKYIELD:
10152 case EXEC_OMP_WORKSHARE:
10153 gfc_resolve_omp_directive (code, ns);
10154 break;
10156 case EXEC_OMP_PARALLEL:
10157 case EXEC_OMP_PARALLEL_DO:
10158 case EXEC_OMP_PARALLEL_DO_SIMD:
10159 case EXEC_OMP_PARALLEL_SECTIONS:
10160 case EXEC_OMP_PARALLEL_WORKSHARE:
10161 case EXEC_OMP_TASK:
10162 omp_workshare_save = omp_workshare_flag;
10163 omp_workshare_flag = 0;
10164 gfc_resolve_omp_directive (code, ns);
10165 omp_workshare_flag = omp_workshare_save;
10166 break;
10168 default:
10169 gfc_internal_error ("resolve_code(): Bad statement code");
10173 cs_base = frame.prev;
10177 /* Resolve initial values and make sure they are compatible with
10178 the variable. */
10180 static void
10181 resolve_values (gfc_symbol *sym)
10183 bool t;
10185 if (sym->value == NULL)
10186 return;
10188 if (sym->value->expr_type == EXPR_STRUCTURE)
10189 t= resolve_structure_cons (sym->value, 1);
10190 else
10191 t = gfc_resolve_expr (sym->value);
10193 if (!t)
10194 return;
10196 gfc_check_assign_symbol (sym, NULL, sym->value);
10200 /* Verify any BIND(C) derived types in the namespace so we can report errors
10201 for them once, rather than for each variable declared of that type. */
10203 static void
10204 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10206 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10207 && derived_sym->attr.is_bind_c == 1)
10208 verify_bind_c_derived_type (derived_sym);
10210 return;
10214 /* Verify that any binding labels used in a given namespace do not collide
10215 with the names or binding labels of any global symbols. Multiple INTERFACE
10216 for the same procedure are permitted. */
10218 static void
10219 gfc_verify_binding_labels (gfc_symbol *sym)
10221 gfc_gsymbol *gsym;
10222 const char *module;
10224 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10225 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10226 return;
10228 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10230 if (sym->module)
10231 module = sym->module;
10232 else if (sym->ns && sym->ns->proc_name
10233 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10234 module = sym->ns->proc_name->name;
10235 else if (sym->ns && sym->ns->parent
10236 && sym->ns && sym->ns->parent->proc_name
10237 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10238 module = sym->ns->parent->proc_name->name;
10239 else
10240 module = NULL;
10242 if (!gsym
10243 || (!gsym->defined
10244 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10246 if (!gsym)
10247 gsym = gfc_get_gsymbol (sym->binding_label);
10248 gsym->where = sym->declared_at;
10249 gsym->sym_name = sym->name;
10250 gsym->binding_label = sym->binding_label;
10251 gsym->ns = sym->ns;
10252 gsym->mod_name = module;
10253 if (sym->attr.function)
10254 gsym->type = GSYM_FUNCTION;
10255 else if (sym->attr.subroutine)
10256 gsym->type = GSYM_SUBROUTINE;
10257 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10258 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10259 return;
10262 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10264 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10265 "identifier as entity at %L", sym->name,
10266 sym->binding_label, &sym->declared_at, &gsym->where);
10267 /* Clear the binding label to prevent checking multiple times. */
10268 sym->binding_label = NULL;
10271 else if (sym->attr.flavor == FL_VARIABLE
10272 && (strcmp (module, gsym->mod_name) != 0
10273 || strcmp (sym->name, gsym->sym_name) != 0))
10275 /* This can only happen if the variable is defined in a module - if it
10276 isn't the same module, reject it. */
10277 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10278 "the same global identifier as entity at %L from module %s",
10279 sym->name, module, sym->binding_label,
10280 &sym->declared_at, &gsym->where, gsym->mod_name);
10281 sym->binding_label = NULL;
10283 else if ((sym->attr.function || sym->attr.subroutine)
10284 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10285 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10286 && sym != gsym->ns->proc_name
10287 && (module != gsym->mod_name
10288 || strcmp (gsym->sym_name, sym->name) != 0
10289 || (module && strcmp (module, gsym->mod_name) != 0)))
10291 /* Print an error if the procedure is defined multiple times; we have to
10292 exclude references to the same procedure via module association or
10293 multiple checks for the same procedure. */
10294 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10295 "global identifier as entity at %L", sym->name,
10296 sym->binding_label, &sym->declared_at, &gsym->where);
10297 sym->binding_label = NULL;
10302 /* Resolve an index expression. */
10304 static bool
10305 resolve_index_expr (gfc_expr *e)
10307 if (!gfc_resolve_expr (e))
10308 return false;
10310 if (!gfc_simplify_expr (e, 0))
10311 return false;
10313 if (!gfc_specification_expr (e))
10314 return false;
10316 return true;
10320 /* Resolve a charlen structure. */
10322 static bool
10323 resolve_charlen (gfc_charlen *cl)
10325 int i, k;
10326 bool saved_specification_expr;
10328 if (cl->resolved)
10329 return true;
10331 cl->resolved = 1;
10332 saved_specification_expr = specification_expr;
10333 specification_expr = true;
10335 if (cl->length_from_typespec)
10337 if (!gfc_resolve_expr (cl->length))
10339 specification_expr = saved_specification_expr;
10340 return false;
10343 if (!gfc_simplify_expr (cl->length, 0))
10345 specification_expr = saved_specification_expr;
10346 return false;
10349 else
10352 if (!resolve_index_expr (cl->length))
10354 specification_expr = saved_specification_expr;
10355 return false;
10359 /* "If the character length parameter value evaluates to a negative
10360 value, the length of character entities declared is zero." */
10361 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10363 if (gfc_option.warn_surprising)
10364 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10365 " the length has been set to zero",
10366 &cl->length->where, i);
10367 gfc_replace_expr (cl->length,
10368 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10371 /* Check that the character length is not too large. */
10372 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10373 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10374 && cl->length->ts.type == BT_INTEGER
10375 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10377 gfc_error ("String length at %L is too large", &cl->length->where);
10378 specification_expr = saved_specification_expr;
10379 return false;
10382 specification_expr = saved_specification_expr;
10383 return true;
10387 /* Test for non-constant shape arrays. */
10389 static bool
10390 is_non_constant_shape_array (gfc_symbol *sym)
10392 gfc_expr *e;
10393 int i;
10394 bool not_constant;
10396 not_constant = false;
10397 if (sym->as != NULL)
10399 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10400 has not been simplified; parameter array references. Do the
10401 simplification now. */
10402 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10404 e = sym->as->lower[i];
10405 if (e && (!resolve_index_expr(e)
10406 || !gfc_is_constant_expr (e)))
10407 not_constant = true;
10408 e = sym->as->upper[i];
10409 if (e && (!resolve_index_expr(e)
10410 || !gfc_is_constant_expr (e)))
10411 not_constant = true;
10414 return not_constant;
10417 /* Given a symbol and an initialization expression, add code to initialize
10418 the symbol to the function entry. */
10419 static void
10420 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10422 gfc_expr *lval;
10423 gfc_code *init_st;
10424 gfc_namespace *ns = sym->ns;
10426 /* Search for the function namespace if this is a contained
10427 function without an explicit result. */
10428 if (sym->attr.function && sym == sym->result
10429 && sym->name != sym->ns->proc_name->name)
10431 ns = ns->contained;
10432 for (;ns; ns = ns->sibling)
10433 if (strcmp (ns->proc_name->name, sym->name) == 0)
10434 break;
10437 if (ns == NULL)
10439 gfc_free_expr (init);
10440 return;
10443 /* Build an l-value expression for the result. */
10444 lval = gfc_lval_expr_from_sym (sym);
10446 /* Add the code at scope entry. */
10447 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
10448 init_st->next = ns->code;
10449 ns->code = init_st;
10451 /* Assign the default initializer to the l-value. */
10452 init_st->loc = sym->declared_at;
10453 init_st->expr1 = lval;
10454 init_st->expr2 = init;
10457 /* Assign the default initializer to a derived type variable or result. */
10459 static void
10460 apply_default_init (gfc_symbol *sym)
10462 gfc_expr *init = NULL;
10464 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10465 return;
10467 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10468 init = gfc_default_initializer (&sym->ts);
10470 if (init == NULL && sym->ts.type != BT_CLASS)
10471 return;
10473 build_init_assign (sym, init);
10474 sym->attr.referenced = 1;
10477 /* Build an initializer for a local integer, real, complex, logical, or
10478 character variable, based on the command line flags finit-local-zero,
10479 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10480 null if the symbol should not have a default initialization. */
10481 static gfc_expr *
10482 build_default_init_expr (gfc_symbol *sym)
10484 int char_len;
10485 gfc_expr *init_expr;
10486 int i;
10488 /* These symbols should never have a default initialization. */
10489 if (sym->attr.allocatable
10490 || sym->attr.external
10491 || sym->attr.dummy
10492 || sym->attr.pointer
10493 || sym->attr.in_equivalence
10494 || sym->attr.in_common
10495 || sym->attr.data
10496 || sym->module
10497 || sym->attr.cray_pointee
10498 || sym->attr.cray_pointer
10499 || sym->assoc)
10500 return NULL;
10502 /* Now we'll try to build an initializer expression. */
10503 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10504 &sym->declared_at);
10506 /* We will only initialize integers, reals, complex, logicals, and
10507 characters, and only if the corresponding command-line flags
10508 were set. Otherwise, we free init_expr and return null. */
10509 switch (sym->ts.type)
10511 case BT_INTEGER:
10512 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10513 mpz_set_si (init_expr->value.integer,
10514 gfc_option.flag_init_integer_value);
10515 else
10517 gfc_free_expr (init_expr);
10518 init_expr = NULL;
10520 break;
10522 case BT_REAL:
10523 switch (gfc_option.flag_init_real)
10525 case GFC_INIT_REAL_SNAN:
10526 init_expr->is_snan = 1;
10527 /* Fall through. */
10528 case GFC_INIT_REAL_NAN:
10529 mpfr_set_nan (init_expr->value.real);
10530 break;
10532 case GFC_INIT_REAL_INF:
10533 mpfr_set_inf (init_expr->value.real, 1);
10534 break;
10536 case GFC_INIT_REAL_NEG_INF:
10537 mpfr_set_inf (init_expr->value.real, -1);
10538 break;
10540 case GFC_INIT_REAL_ZERO:
10541 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10542 break;
10544 default:
10545 gfc_free_expr (init_expr);
10546 init_expr = NULL;
10547 break;
10549 break;
10551 case BT_COMPLEX:
10552 switch (gfc_option.flag_init_real)
10554 case GFC_INIT_REAL_SNAN:
10555 init_expr->is_snan = 1;
10556 /* Fall through. */
10557 case GFC_INIT_REAL_NAN:
10558 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10559 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10560 break;
10562 case GFC_INIT_REAL_INF:
10563 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10564 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10565 break;
10567 case GFC_INIT_REAL_NEG_INF:
10568 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10569 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10570 break;
10572 case GFC_INIT_REAL_ZERO:
10573 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10574 break;
10576 default:
10577 gfc_free_expr (init_expr);
10578 init_expr = NULL;
10579 break;
10581 break;
10583 case BT_LOGICAL:
10584 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10585 init_expr->value.logical = 0;
10586 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10587 init_expr->value.logical = 1;
10588 else
10590 gfc_free_expr (init_expr);
10591 init_expr = NULL;
10593 break;
10595 case BT_CHARACTER:
10596 /* For characters, the length must be constant in order to
10597 create a default initializer. */
10598 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10599 && sym->ts.u.cl->length
10600 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10602 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10603 init_expr->value.character.length = char_len;
10604 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10605 for (i = 0; i < char_len; i++)
10606 init_expr->value.character.string[i]
10607 = (unsigned char) gfc_option.flag_init_character_value;
10609 else
10611 gfc_free_expr (init_expr);
10612 init_expr = NULL;
10614 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10615 && sym->ts.u.cl->length && gfc_option.flag_max_stack_var_size != 0)
10617 gfc_actual_arglist *arg;
10618 init_expr = gfc_get_expr ();
10619 init_expr->where = sym->declared_at;
10620 init_expr->ts = sym->ts;
10621 init_expr->expr_type = EXPR_FUNCTION;
10622 init_expr->value.function.isym =
10623 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10624 init_expr->value.function.name = "repeat";
10625 arg = gfc_get_actual_arglist ();
10626 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10627 NULL, 1);
10628 arg->expr->value.character.string[0]
10629 = gfc_option.flag_init_character_value;
10630 arg->next = gfc_get_actual_arglist ();
10631 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10632 init_expr->value.function.actual = arg;
10634 break;
10636 default:
10637 gfc_free_expr (init_expr);
10638 init_expr = NULL;
10640 return init_expr;
10643 /* Add an initialization expression to a local variable. */
10644 static void
10645 apply_default_init_local (gfc_symbol *sym)
10647 gfc_expr *init = NULL;
10649 /* The symbol should be a variable or a function return value. */
10650 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10651 || (sym->attr.function && sym->result != sym))
10652 return;
10654 /* Try to build the initializer expression. If we can't initialize
10655 this symbol, then init will be NULL. */
10656 init = build_default_init_expr (sym);
10657 if (init == NULL)
10658 return;
10660 /* For saved variables, we don't want to add an initializer at function
10661 entry, so we just add a static initializer. Note that automatic variables
10662 are stack allocated even with -fno-automatic; we have also to exclude
10663 result variable, which are also nonstatic. */
10664 if (sym->attr.save || sym->ns->save_all
10665 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10666 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10668 /* Don't clobber an existing initializer! */
10669 gcc_assert (sym->value == NULL);
10670 sym->value = init;
10671 return;
10674 build_init_assign (sym, init);
10678 /* Resolution of common features of flavors variable and procedure. */
10680 static bool
10681 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10683 gfc_array_spec *as;
10685 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10686 as = CLASS_DATA (sym)->as;
10687 else
10688 as = sym->as;
10690 /* Constraints on deferred shape variable. */
10691 if (as == NULL || as->type != AS_DEFERRED)
10693 bool pointer, allocatable, dimension;
10695 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10697 pointer = CLASS_DATA (sym)->attr.class_pointer;
10698 allocatable = CLASS_DATA (sym)->attr.allocatable;
10699 dimension = CLASS_DATA (sym)->attr.dimension;
10701 else
10703 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10704 allocatable = sym->attr.allocatable;
10705 dimension = sym->attr.dimension;
10708 if (allocatable)
10710 if (dimension && as->type != AS_ASSUMED_RANK)
10712 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10713 "shape or assumed rank", sym->name, &sym->declared_at);
10714 return false;
10716 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10717 "'%s' at %L may not be ALLOCATABLE",
10718 sym->name, &sym->declared_at))
10719 return false;
10722 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10724 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10725 "assumed rank", sym->name, &sym->declared_at);
10726 return false;
10729 else
10731 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10732 && sym->ts.type != BT_CLASS && !sym->assoc)
10734 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10735 sym->name, &sym->declared_at);
10736 return false;
10740 /* Constraints on polymorphic variables. */
10741 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10743 /* F03:C502. */
10744 if (sym->attr.class_ok
10745 && !sym->attr.select_type_temporary
10746 && !UNLIMITED_POLY (sym)
10747 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10749 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10750 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10751 &sym->declared_at);
10752 return false;
10755 /* F03:C509. */
10756 /* Assume that use associated symbols were checked in the module ns.
10757 Class-variables that are associate-names are also something special
10758 and excepted from the test. */
10759 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10761 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10762 "or pointer", sym->name, &sym->declared_at);
10763 return false;
10767 return true;
10771 /* Additional checks for symbols with flavor variable and derived
10772 type. To be called from resolve_fl_variable. */
10774 static bool
10775 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10777 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10779 /* Check to see if a derived type is blocked from being host
10780 associated by the presence of another class I symbol in the same
10781 namespace. 14.6.1.3 of the standard and the discussion on
10782 comp.lang.fortran. */
10783 if (sym->ns != sym->ts.u.derived->ns
10784 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10786 gfc_symbol *s;
10787 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10788 if (s && s->attr.generic)
10789 s = gfc_find_dt_in_generic (s);
10790 if (s && s->attr.flavor != FL_DERIVED)
10792 gfc_error ("The type '%s' cannot be host associated at %L "
10793 "because it is blocked by an incompatible object "
10794 "of the same name declared at %L",
10795 sym->ts.u.derived->name, &sym->declared_at,
10796 &s->declared_at);
10797 return false;
10801 /* 4th constraint in section 11.3: "If an object of a type for which
10802 component-initialization is specified (R429) appears in the
10803 specification-part of a module and does not have the ALLOCATABLE
10804 or POINTER attribute, the object shall have the SAVE attribute."
10806 The check for initializers is performed with
10807 gfc_has_default_initializer because gfc_default_initializer generates
10808 a hidden default for allocatable components. */
10809 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10810 && sym->ns->proc_name->attr.flavor == FL_MODULE
10811 && !sym->ns->save_all && !sym->attr.save
10812 && !sym->attr.pointer && !sym->attr.allocatable
10813 && gfc_has_default_initializer (sym->ts.u.derived)
10814 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10815 "'%s' at %L, needed due to the default "
10816 "initialization", sym->name, &sym->declared_at))
10817 return false;
10819 /* Assign default initializer. */
10820 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10821 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10823 sym->value = gfc_default_initializer (&sym->ts);
10826 return true;
10830 /* Resolve symbols with flavor variable. */
10832 static bool
10833 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10835 int no_init_flag, automatic_flag;
10836 gfc_expr *e;
10837 const char *auto_save_msg;
10838 bool saved_specification_expr;
10840 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10841 "SAVE attribute";
10843 if (!resolve_fl_var_and_proc (sym, mp_flag))
10844 return false;
10846 /* Set this flag to check that variables are parameters of all entries.
10847 This check is effected by the call to gfc_resolve_expr through
10848 is_non_constant_shape_array. */
10849 saved_specification_expr = specification_expr;
10850 specification_expr = true;
10852 if (sym->ns->proc_name
10853 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10854 || sym->ns->proc_name->attr.is_main_program)
10855 && !sym->attr.use_assoc
10856 && !sym->attr.allocatable
10857 && !sym->attr.pointer
10858 && is_non_constant_shape_array (sym))
10860 /* The shape of a main program or module array needs to be
10861 constant. */
10862 gfc_error ("The module or main program array '%s' at %L must "
10863 "have constant shape", sym->name, &sym->declared_at);
10864 specification_expr = saved_specification_expr;
10865 return false;
10868 /* Constraints on deferred type parameter. */
10869 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10871 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10872 "requires either the pointer or allocatable attribute",
10873 sym->name, &sym->declared_at);
10874 specification_expr = saved_specification_expr;
10875 return false;
10878 if (sym->ts.type == BT_CHARACTER)
10880 /* Make sure that character string variables with assumed length are
10881 dummy arguments. */
10882 e = sym->ts.u.cl->length;
10883 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10884 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10886 gfc_error ("Entity with assumed character length at %L must be a "
10887 "dummy argument or a PARAMETER", &sym->declared_at);
10888 specification_expr = saved_specification_expr;
10889 return false;
10892 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10894 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10895 specification_expr = saved_specification_expr;
10896 return false;
10899 if (!gfc_is_constant_expr (e)
10900 && !(e->expr_type == EXPR_VARIABLE
10901 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10903 if (!sym->attr.use_assoc && sym->ns->proc_name
10904 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10905 || sym->ns->proc_name->attr.is_main_program))
10907 gfc_error ("'%s' at %L must have constant character length "
10908 "in this context", sym->name, &sym->declared_at);
10909 specification_expr = saved_specification_expr;
10910 return false;
10912 if (sym->attr.in_common)
10914 gfc_error ("COMMON variable '%s' at %L must have constant "
10915 "character length", sym->name, &sym->declared_at);
10916 specification_expr = saved_specification_expr;
10917 return false;
10922 if (sym->value == NULL && sym->attr.referenced)
10923 apply_default_init_local (sym); /* Try to apply a default initialization. */
10925 /* Determine if the symbol may not have an initializer. */
10926 no_init_flag = automatic_flag = 0;
10927 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10928 || sym->attr.intrinsic || sym->attr.result)
10929 no_init_flag = 1;
10930 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10931 && is_non_constant_shape_array (sym))
10933 no_init_flag = automatic_flag = 1;
10935 /* Also, they must not have the SAVE attribute.
10936 SAVE_IMPLICIT is checked below. */
10937 if (sym->as && sym->attr.codimension)
10939 int corank = sym->as->corank;
10940 sym->as->corank = 0;
10941 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10942 sym->as->corank = corank;
10944 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10946 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10947 specification_expr = saved_specification_expr;
10948 return false;
10952 /* Ensure that any initializer is simplified. */
10953 if (sym->value)
10954 gfc_simplify_expr (sym->value, 1);
10956 /* Reject illegal initializers. */
10957 if (!sym->mark && sym->value)
10959 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10960 && CLASS_DATA (sym)->attr.allocatable))
10961 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10962 sym->name, &sym->declared_at);
10963 else if (sym->attr.external)
10964 gfc_error ("External '%s' at %L cannot have an initializer",
10965 sym->name, &sym->declared_at);
10966 else if (sym->attr.dummy
10967 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10968 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10969 sym->name, &sym->declared_at);
10970 else if (sym->attr.intrinsic)
10971 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10972 sym->name, &sym->declared_at);
10973 else if (sym->attr.result)
10974 gfc_error ("Function result '%s' at %L cannot have an initializer",
10975 sym->name, &sym->declared_at);
10976 else if (automatic_flag)
10977 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10978 sym->name, &sym->declared_at);
10979 else
10980 goto no_init_error;
10981 specification_expr = saved_specification_expr;
10982 return false;
10985 no_init_error:
10986 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10988 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10989 specification_expr = saved_specification_expr;
10990 return res;
10993 specification_expr = saved_specification_expr;
10994 return true;
10998 /* Resolve a procedure. */
11000 static bool
11001 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11003 gfc_formal_arglist *arg;
11005 if (sym->attr.function
11006 && !resolve_fl_var_and_proc (sym, mp_flag))
11007 return false;
11009 if (sym->ts.type == BT_CHARACTER)
11011 gfc_charlen *cl = sym->ts.u.cl;
11013 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11014 && !resolve_charlen (cl))
11015 return false;
11017 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11018 && sym->attr.proc == PROC_ST_FUNCTION)
11020 gfc_error ("Character-valued statement function '%s' at %L must "
11021 "have constant length", sym->name, &sym->declared_at);
11022 return false;
11026 /* Ensure that derived type for are not of a private type. Internal
11027 module procedures are excluded by 2.2.3.3 - i.e., they are not
11028 externally accessible and can access all the objects accessible in
11029 the host. */
11030 if (!(sym->ns->parent
11031 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11032 && gfc_check_symbol_access (sym))
11034 gfc_interface *iface;
11036 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11038 if (arg->sym
11039 && arg->sym->ts.type == BT_DERIVED
11040 && !arg->sym->ts.u.derived->attr.use_assoc
11041 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11042 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
11043 "and cannot be a dummy argument"
11044 " of '%s', which is PUBLIC at %L",
11045 arg->sym->name, sym->name,
11046 &sym->declared_at))
11048 /* Stop this message from recurring. */
11049 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11050 return false;
11054 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11055 PRIVATE to the containing module. */
11056 for (iface = sym->generic; iface; iface = iface->next)
11058 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11060 if (arg->sym
11061 && arg->sym->ts.type == BT_DERIVED
11062 && !arg->sym->ts.u.derived->attr.use_assoc
11063 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11064 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11065 "PUBLIC interface '%s' at %L "
11066 "takes dummy arguments of '%s' which "
11067 "is PRIVATE", iface->sym->name,
11068 sym->name, &iface->sym->declared_at,
11069 gfc_typename(&arg->sym->ts)))
11071 /* Stop this message from recurring. */
11072 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11073 return false;
11078 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11079 PRIVATE to the containing module. */
11080 for (iface = sym->generic; iface; iface = iface->next)
11082 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11084 if (arg->sym
11085 && arg->sym->ts.type == BT_DERIVED
11086 && !arg->sym->ts.u.derived->attr.use_assoc
11087 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11088 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11089 "PUBLIC interface '%s' at %L takes "
11090 "dummy arguments of '%s' which is "
11091 "PRIVATE", iface->sym->name,
11092 sym->name, &iface->sym->declared_at,
11093 gfc_typename(&arg->sym->ts)))
11095 /* Stop this message from recurring. */
11096 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11097 return false;
11103 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11104 && !sym->attr.proc_pointer)
11106 gfc_error ("Function '%s' at %L cannot have an initializer",
11107 sym->name, &sym->declared_at);
11108 return false;
11111 /* An external symbol may not have an initializer because it is taken to be
11112 a procedure. Exception: Procedure Pointers. */
11113 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11115 gfc_error ("External object '%s' at %L may not have an initializer",
11116 sym->name, &sym->declared_at);
11117 return false;
11120 /* An elemental function is required to return a scalar 12.7.1 */
11121 if (sym->attr.elemental && sym->attr.function && sym->as)
11123 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11124 "result", sym->name, &sym->declared_at);
11125 /* Reset so that the error only occurs once. */
11126 sym->attr.elemental = 0;
11127 return false;
11130 if (sym->attr.proc == PROC_ST_FUNCTION
11131 && (sym->attr.allocatable || sym->attr.pointer))
11133 gfc_error ("Statement function '%s' at %L may not have pointer or "
11134 "allocatable attribute", sym->name, &sym->declared_at);
11135 return false;
11138 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11139 char-len-param shall not be array-valued, pointer-valued, recursive
11140 or pure. ....snip... A character value of * may only be used in the
11141 following ways: (i) Dummy arg of procedure - dummy associates with
11142 actual length; (ii) To declare a named constant; or (iii) External
11143 function - but length must be declared in calling scoping unit. */
11144 if (sym->attr.function
11145 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11146 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11148 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11149 || (sym->attr.recursive) || (sym->attr.pure))
11151 if (sym->as && sym->as->rank)
11152 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11153 "array-valued", sym->name, &sym->declared_at);
11155 if (sym->attr.pointer)
11156 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11157 "pointer-valued", sym->name, &sym->declared_at);
11159 if (sym->attr.pure)
11160 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11161 "pure", sym->name, &sym->declared_at);
11163 if (sym->attr.recursive)
11164 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11165 "recursive", sym->name, &sym->declared_at);
11167 return false;
11170 /* Appendix B.2 of the standard. Contained functions give an
11171 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11172 character length is an F2003 feature. */
11173 if (!sym->attr.contained
11174 && gfc_current_form != FORM_FIXED
11175 && !sym->ts.deferred)
11176 gfc_notify_std (GFC_STD_F95_OBS,
11177 "CHARACTER(*) function '%s' at %L",
11178 sym->name, &sym->declared_at);
11181 /* F2008, C1218. */
11182 if (sym->attr.elemental)
11184 if (sym->attr.proc_pointer)
11186 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11187 sym->name, &sym->declared_at);
11188 return false;
11190 if (sym->attr.dummy)
11192 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11193 sym->name, &sym->declared_at);
11194 return false;
11198 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11200 gfc_formal_arglist *curr_arg;
11201 int has_non_interop_arg = 0;
11203 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11204 sym->common_block))
11206 /* Clear these to prevent looking at them again if there was an
11207 error. */
11208 sym->attr.is_bind_c = 0;
11209 sym->attr.is_c_interop = 0;
11210 sym->ts.is_c_interop = 0;
11212 else
11214 /* So far, no errors have been found. */
11215 sym->attr.is_c_interop = 1;
11216 sym->ts.is_c_interop = 1;
11219 curr_arg = gfc_sym_get_dummy_args (sym);
11220 while (curr_arg != NULL)
11222 /* Skip implicitly typed dummy args here. */
11223 if (curr_arg->sym->attr.implicit_type == 0)
11224 if (!gfc_verify_c_interop_param (curr_arg->sym))
11225 /* If something is found to fail, record the fact so we
11226 can mark the symbol for the procedure as not being
11227 BIND(C) to try and prevent multiple errors being
11228 reported. */
11229 has_non_interop_arg = 1;
11231 curr_arg = curr_arg->next;
11234 /* See if any of the arguments were not interoperable and if so, clear
11235 the procedure symbol to prevent duplicate error messages. */
11236 if (has_non_interop_arg != 0)
11238 sym->attr.is_c_interop = 0;
11239 sym->ts.is_c_interop = 0;
11240 sym->attr.is_bind_c = 0;
11244 if (!sym->attr.proc_pointer)
11246 if (sym->attr.save == SAVE_EXPLICIT)
11248 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11249 "in '%s' at %L", sym->name, &sym->declared_at);
11250 return false;
11252 if (sym->attr.intent)
11254 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11255 "in '%s' at %L", sym->name, &sym->declared_at);
11256 return false;
11258 if (sym->attr.subroutine && sym->attr.result)
11260 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11261 "in '%s' at %L", sym->name, &sym->declared_at);
11262 return false;
11264 if (sym->attr.external && sym->attr.function
11265 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11266 || sym->attr.contained))
11268 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11269 "in '%s' at %L", sym->name, &sym->declared_at);
11270 return false;
11272 if (strcmp ("ppr@", sym->name) == 0)
11274 gfc_error ("Procedure pointer result '%s' at %L "
11275 "is missing the pointer attribute",
11276 sym->ns->proc_name->name, &sym->declared_at);
11277 return false;
11281 return true;
11285 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11286 been defined and we now know their defined arguments, check that they fulfill
11287 the requirements of the standard for procedures used as finalizers. */
11289 static bool
11290 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11292 gfc_finalizer* list;
11293 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11294 bool result = true;
11295 bool seen_scalar = false;
11296 gfc_symbol *vtab;
11297 gfc_component *c;
11299 /* Return early when not finalizable. Additionally, ensure that derived-type
11300 components have a their finalizables resolved. */
11301 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11303 bool has_final = false;
11304 for (c = derived->components; c; c = c->next)
11305 if (c->ts.type == BT_DERIVED
11306 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11308 bool has_final2 = false;
11309 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11310 return false; /* Error. */
11311 has_final = has_final || has_final2;
11313 if (!has_final)
11315 if (finalizable)
11316 *finalizable = false;
11317 return true;
11321 /* Walk over the list of finalizer-procedures, check them, and if any one
11322 does not fit in with the standard's definition, print an error and remove
11323 it from the list. */
11324 prev_link = &derived->f2k_derived->finalizers;
11325 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11327 gfc_formal_arglist *dummy_args;
11328 gfc_symbol* arg;
11329 gfc_finalizer* i;
11330 int my_rank;
11332 /* Skip this finalizer if we already resolved it. */
11333 if (list->proc_tree)
11335 prev_link = &(list->next);
11336 continue;
11339 /* Check this exists and is a SUBROUTINE. */
11340 if (!list->proc_sym->attr.subroutine)
11342 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11343 list->proc_sym->name, &list->where);
11344 goto error;
11347 /* We should have exactly one argument. */
11348 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11349 if (!dummy_args || dummy_args->next)
11351 gfc_error ("FINAL procedure at %L must have exactly one argument",
11352 &list->where);
11353 goto error;
11355 arg = dummy_args->sym;
11357 /* This argument must be of our type. */
11358 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11360 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11361 &arg->declared_at, derived->name);
11362 goto error;
11365 /* It must neither be a pointer nor allocatable nor optional. */
11366 if (arg->attr.pointer)
11368 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11369 &arg->declared_at);
11370 goto error;
11372 if (arg->attr.allocatable)
11374 gfc_error ("Argument of FINAL procedure at %L must not be"
11375 " ALLOCATABLE", &arg->declared_at);
11376 goto error;
11378 if (arg->attr.optional)
11380 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11381 &arg->declared_at);
11382 goto error;
11385 /* It must not be INTENT(OUT). */
11386 if (arg->attr.intent == INTENT_OUT)
11388 gfc_error ("Argument of FINAL procedure at %L must not be"
11389 " INTENT(OUT)", &arg->declared_at);
11390 goto error;
11393 /* Warn if the procedure is non-scalar and not assumed shape. */
11394 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11395 && arg->as->type != AS_ASSUMED_SHAPE)
11396 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11397 " shape argument", &arg->declared_at);
11399 /* Check that it does not match in kind and rank with a FINAL procedure
11400 defined earlier. To really loop over the *earlier* declarations,
11401 we need to walk the tail of the list as new ones were pushed at the
11402 front. */
11403 /* TODO: Handle kind parameters once they are implemented. */
11404 my_rank = (arg->as ? arg->as->rank : 0);
11405 for (i = list->next; i; i = i->next)
11407 gfc_formal_arglist *dummy_args;
11409 /* Argument list might be empty; that is an error signalled earlier,
11410 but we nevertheless continued resolving. */
11411 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11412 if (dummy_args)
11414 gfc_symbol* i_arg = dummy_args->sym;
11415 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11416 if (i_rank == my_rank)
11418 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11419 " rank (%d) as '%s'",
11420 list->proc_sym->name, &list->where, my_rank,
11421 i->proc_sym->name);
11422 goto error;
11427 /* Is this the/a scalar finalizer procedure? */
11428 if (!arg->as || arg->as->rank == 0)
11429 seen_scalar = true;
11431 /* Find the symtree for this procedure. */
11432 gcc_assert (!list->proc_tree);
11433 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11435 prev_link = &list->next;
11436 continue;
11438 /* Remove wrong nodes immediately from the list so we don't risk any
11439 troubles in the future when they might fail later expectations. */
11440 error:
11441 i = list;
11442 *prev_link = list->next;
11443 gfc_free_finalizer (i);
11444 result = false;
11447 if (result == false)
11448 return false;
11450 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11451 were nodes in the list, must have been for arrays. It is surely a good
11452 idea to have a scalar version there if there's something to finalize. */
11453 if (gfc_option.warn_surprising && result && !seen_scalar)
11454 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11455 " defined at %L, suggest also scalar one",
11456 derived->name, &derived->declared_at);
11458 vtab = gfc_find_derived_vtab (derived);
11459 c = vtab->ts.u.derived->components->next->next->next->next->next;
11460 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
11462 if (finalizable)
11463 *finalizable = true;
11465 return true;
11469 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11471 static bool
11472 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11473 const char* generic_name, locus where)
11475 gfc_symbol *sym1, *sym2;
11476 const char *pass1, *pass2;
11477 gfc_formal_arglist *dummy_args;
11479 gcc_assert (t1->specific && t2->specific);
11480 gcc_assert (!t1->specific->is_generic);
11481 gcc_assert (!t2->specific->is_generic);
11482 gcc_assert (t1->is_operator == t2->is_operator);
11484 sym1 = t1->specific->u.specific->n.sym;
11485 sym2 = t2->specific->u.specific->n.sym;
11487 if (sym1 == sym2)
11488 return true;
11490 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11491 if (sym1->attr.subroutine != sym2->attr.subroutine
11492 || sym1->attr.function != sym2->attr.function)
11494 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11495 " GENERIC '%s' at %L",
11496 sym1->name, sym2->name, generic_name, &where);
11497 return false;
11500 /* Determine PASS arguments. */
11501 if (t1->specific->nopass)
11502 pass1 = NULL;
11503 else if (t1->specific->pass_arg)
11504 pass1 = t1->specific->pass_arg;
11505 else
11507 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
11508 if (dummy_args)
11509 pass1 = dummy_args->sym->name;
11510 else
11511 pass1 = NULL;
11513 if (t2->specific->nopass)
11514 pass2 = NULL;
11515 else if (t2->specific->pass_arg)
11516 pass2 = t2->specific->pass_arg;
11517 else
11519 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
11520 if (dummy_args)
11521 pass2 = dummy_args->sym->name;
11522 else
11523 pass2 = NULL;
11526 /* Compare the interfaces. */
11527 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11528 NULL, 0, pass1, pass2))
11530 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11531 sym1->name, sym2->name, generic_name, &where);
11532 return false;
11535 return true;
11539 /* Worker function for resolving a generic procedure binding; this is used to
11540 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11542 The difference between those cases is finding possible inherited bindings
11543 that are overridden, as one has to look for them in tb_sym_root,
11544 tb_uop_root or tb_op, respectively. Thus the caller must already find
11545 the super-type and set p->overridden correctly. */
11547 static bool
11548 resolve_tb_generic_targets (gfc_symbol* super_type,
11549 gfc_typebound_proc* p, const char* name)
11551 gfc_tbp_generic* target;
11552 gfc_symtree* first_target;
11553 gfc_symtree* inherited;
11555 gcc_assert (p && p->is_generic);
11557 /* Try to find the specific bindings for the symtrees in our target-list. */
11558 gcc_assert (p->u.generic);
11559 for (target = p->u.generic; target; target = target->next)
11560 if (!target->specific)
11562 gfc_typebound_proc* overridden_tbp;
11563 gfc_tbp_generic* g;
11564 const char* target_name;
11566 target_name = target->specific_st->name;
11568 /* Defined for this type directly. */
11569 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11571 target->specific = target->specific_st->n.tb;
11572 goto specific_found;
11575 /* Look for an inherited specific binding. */
11576 if (super_type)
11578 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11579 true, NULL);
11581 if (inherited)
11583 gcc_assert (inherited->n.tb);
11584 target->specific = inherited->n.tb;
11585 goto specific_found;
11589 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11590 " at %L", target_name, name, &p->where);
11591 return false;
11593 /* Once we've found the specific binding, check it is not ambiguous with
11594 other specifics already found or inherited for the same GENERIC. */
11595 specific_found:
11596 gcc_assert (target->specific);
11598 /* This must really be a specific binding! */
11599 if (target->specific->is_generic)
11601 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11602 " '%s' is GENERIC, too", name, &p->where, target_name);
11603 return false;
11606 /* Check those already resolved on this type directly. */
11607 for (g = p->u.generic; g; g = g->next)
11608 if (g != target && g->specific
11609 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11610 return false;
11612 /* Check for ambiguity with inherited specific targets. */
11613 for (overridden_tbp = p->overridden; overridden_tbp;
11614 overridden_tbp = overridden_tbp->overridden)
11615 if (overridden_tbp->is_generic)
11617 for (g = overridden_tbp->u.generic; g; g = g->next)
11619 gcc_assert (g->specific);
11620 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11621 return false;
11626 /* If we attempt to "overwrite" a specific binding, this is an error. */
11627 if (p->overridden && !p->overridden->is_generic)
11629 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11630 " the same name", name, &p->where);
11631 return false;
11634 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11635 all must have the same attributes here. */
11636 first_target = p->u.generic->specific->u.specific;
11637 gcc_assert (first_target);
11638 p->subroutine = first_target->n.sym->attr.subroutine;
11639 p->function = first_target->n.sym->attr.function;
11641 return true;
11645 /* Resolve a GENERIC procedure binding for a derived type. */
11647 static bool
11648 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11650 gfc_symbol* super_type;
11652 /* Find the overridden binding if any. */
11653 st->n.tb->overridden = NULL;
11654 super_type = gfc_get_derived_super_type (derived);
11655 if (super_type)
11657 gfc_symtree* overridden;
11658 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11659 true, NULL);
11661 if (overridden && overridden->n.tb)
11662 st->n.tb->overridden = overridden->n.tb;
11665 /* Resolve using worker function. */
11666 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11670 /* Retrieve the target-procedure of an operator binding and do some checks in
11671 common for intrinsic and user-defined type-bound operators. */
11673 static gfc_symbol*
11674 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11676 gfc_symbol* target_proc;
11678 gcc_assert (target->specific && !target->specific->is_generic);
11679 target_proc = target->specific->u.specific->n.sym;
11680 gcc_assert (target_proc);
11682 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11683 if (target->specific->nopass)
11685 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11686 return NULL;
11689 return target_proc;
11693 /* Resolve a type-bound intrinsic operator. */
11695 static bool
11696 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11697 gfc_typebound_proc* p)
11699 gfc_symbol* super_type;
11700 gfc_tbp_generic* target;
11702 /* If there's already an error here, do nothing (but don't fail again). */
11703 if (p->error)
11704 return true;
11706 /* Operators should always be GENERIC bindings. */
11707 gcc_assert (p->is_generic);
11709 /* Look for an overridden binding. */
11710 super_type = gfc_get_derived_super_type (derived);
11711 if (super_type && super_type->f2k_derived)
11712 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11713 op, true, NULL);
11714 else
11715 p->overridden = NULL;
11717 /* Resolve general GENERIC properties using worker function. */
11718 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11719 goto error;
11721 /* Check the targets to be procedures of correct interface. */
11722 for (target = p->u.generic; target; target = target->next)
11724 gfc_symbol* target_proc;
11726 target_proc = get_checked_tb_operator_target (target, p->where);
11727 if (!target_proc)
11728 goto error;
11730 if (!gfc_check_operator_interface (target_proc, op, p->where))
11731 goto error;
11733 /* Add target to non-typebound operator list. */
11734 if (!target->specific->deferred && !derived->attr.use_assoc
11735 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11737 gfc_interface *head, *intr;
11738 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11739 return false;
11740 head = derived->ns->op[op];
11741 intr = gfc_get_interface ();
11742 intr->sym = target_proc;
11743 intr->where = p->where;
11744 intr->next = head;
11745 derived->ns->op[op] = intr;
11749 return true;
11751 error:
11752 p->error = 1;
11753 return false;
11757 /* Resolve a type-bound user operator (tree-walker callback). */
11759 static gfc_symbol* resolve_bindings_derived;
11760 static bool resolve_bindings_result;
11762 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11764 static void
11765 resolve_typebound_user_op (gfc_symtree* stree)
11767 gfc_symbol* super_type;
11768 gfc_tbp_generic* target;
11770 gcc_assert (stree && stree->n.tb);
11772 if (stree->n.tb->error)
11773 return;
11775 /* Operators should always be GENERIC bindings. */
11776 gcc_assert (stree->n.tb->is_generic);
11778 /* Find overridden procedure, if any. */
11779 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11780 if (super_type && super_type->f2k_derived)
11782 gfc_symtree* overridden;
11783 overridden = gfc_find_typebound_user_op (super_type, NULL,
11784 stree->name, true, NULL);
11786 if (overridden && overridden->n.tb)
11787 stree->n.tb->overridden = overridden->n.tb;
11789 else
11790 stree->n.tb->overridden = NULL;
11792 /* Resolve basically using worker function. */
11793 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11794 goto error;
11796 /* Check the targets to be functions of correct interface. */
11797 for (target = stree->n.tb->u.generic; target; target = target->next)
11799 gfc_symbol* target_proc;
11801 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11802 if (!target_proc)
11803 goto error;
11805 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11806 goto error;
11809 return;
11811 error:
11812 resolve_bindings_result = false;
11813 stree->n.tb->error = 1;
11817 /* Resolve the type-bound procedures for a derived type. */
11819 static void
11820 resolve_typebound_procedure (gfc_symtree* stree)
11822 gfc_symbol* proc;
11823 locus where;
11824 gfc_symbol* me_arg;
11825 gfc_symbol* super_type;
11826 gfc_component* comp;
11828 gcc_assert (stree);
11830 /* Undefined specific symbol from GENERIC target definition. */
11831 if (!stree->n.tb)
11832 return;
11834 if (stree->n.tb->error)
11835 return;
11837 /* If this is a GENERIC binding, use that routine. */
11838 if (stree->n.tb->is_generic)
11840 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11841 goto error;
11842 return;
11845 /* Get the target-procedure to check it. */
11846 gcc_assert (!stree->n.tb->is_generic);
11847 gcc_assert (stree->n.tb->u.specific);
11848 proc = stree->n.tb->u.specific->n.sym;
11849 where = stree->n.tb->where;
11851 /* Default access should already be resolved from the parser. */
11852 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11854 if (stree->n.tb->deferred)
11856 if (!check_proc_interface (proc, &where))
11857 goto error;
11859 else
11861 /* Check for F08:C465. */
11862 if ((!proc->attr.subroutine && !proc->attr.function)
11863 || (proc->attr.proc != PROC_MODULE
11864 && proc->attr.if_source != IFSRC_IFBODY)
11865 || proc->attr.abstract)
11867 gfc_error ("'%s' must be a module procedure or an external procedure with"
11868 " an explicit interface at %L", proc->name, &where);
11869 goto error;
11873 stree->n.tb->subroutine = proc->attr.subroutine;
11874 stree->n.tb->function = proc->attr.function;
11876 /* Find the super-type of the current derived type. We could do this once and
11877 store in a global if speed is needed, but as long as not I believe this is
11878 more readable and clearer. */
11879 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11881 /* If PASS, resolve and check arguments if not already resolved / loaded
11882 from a .mod file. */
11883 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11885 gfc_formal_arglist *dummy_args;
11887 dummy_args = gfc_sym_get_dummy_args (proc);
11888 if (stree->n.tb->pass_arg)
11890 gfc_formal_arglist *i;
11892 /* If an explicit passing argument name is given, walk the arg-list
11893 and look for it. */
11895 me_arg = NULL;
11896 stree->n.tb->pass_arg_num = 1;
11897 for (i = dummy_args; i; i = i->next)
11899 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11901 me_arg = i->sym;
11902 break;
11904 ++stree->n.tb->pass_arg_num;
11907 if (!me_arg)
11909 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11910 " argument '%s'",
11911 proc->name, stree->n.tb->pass_arg, &where,
11912 stree->n.tb->pass_arg);
11913 goto error;
11916 else
11918 /* Otherwise, take the first one; there should in fact be at least
11919 one. */
11920 stree->n.tb->pass_arg_num = 1;
11921 if (!dummy_args)
11923 gfc_error ("Procedure '%s' with PASS at %L must have at"
11924 " least one argument", proc->name, &where);
11925 goto error;
11927 me_arg = dummy_args->sym;
11930 /* Now check that the argument-type matches and the passed-object
11931 dummy argument is generally fine. */
11933 gcc_assert (me_arg);
11935 if (me_arg->ts.type != BT_CLASS)
11937 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11938 " at %L", proc->name, &where);
11939 goto error;
11942 if (CLASS_DATA (me_arg)->ts.u.derived
11943 != resolve_bindings_derived)
11945 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11946 " the derived-type '%s'", me_arg->name, proc->name,
11947 me_arg->name, &where, resolve_bindings_derived->name);
11948 goto error;
11951 gcc_assert (me_arg->ts.type == BT_CLASS);
11952 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11954 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11955 " scalar", proc->name, &where);
11956 goto error;
11958 if (CLASS_DATA (me_arg)->attr.allocatable)
11960 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11961 " be ALLOCATABLE", proc->name, &where);
11962 goto error;
11964 if (CLASS_DATA (me_arg)->attr.class_pointer)
11966 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11967 " be POINTER", proc->name, &where);
11968 goto error;
11972 /* If we are extending some type, check that we don't override a procedure
11973 flagged NON_OVERRIDABLE. */
11974 stree->n.tb->overridden = NULL;
11975 if (super_type)
11977 gfc_symtree* overridden;
11978 overridden = gfc_find_typebound_proc (super_type, NULL,
11979 stree->name, true, NULL);
11981 if (overridden)
11983 if (overridden->n.tb)
11984 stree->n.tb->overridden = overridden->n.tb;
11986 if (!gfc_check_typebound_override (stree, overridden))
11987 goto error;
11991 /* See if there's a name collision with a component directly in this type. */
11992 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11993 if (!strcmp (comp->name, stree->name))
11995 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11996 " '%s'",
11997 stree->name, &where, resolve_bindings_derived->name);
11998 goto error;
12001 /* Try to find a name collision with an inherited component. */
12002 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12004 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
12005 " component of '%s'",
12006 stree->name, &where, resolve_bindings_derived->name);
12007 goto error;
12010 stree->n.tb->error = 0;
12011 return;
12013 error:
12014 resolve_bindings_result = false;
12015 stree->n.tb->error = 1;
12019 static bool
12020 resolve_typebound_procedures (gfc_symbol* derived)
12022 int op;
12023 gfc_symbol* super_type;
12025 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12026 return true;
12028 super_type = gfc_get_derived_super_type (derived);
12029 if (super_type)
12030 resolve_symbol (super_type);
12032 resolve_bindings_derived = derived;
12033 resolve_bindings_result = true;
12035 if (derived->f2k_derived->tb_sym_root)
12036 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12037 &resolve_typebound_procedure);
12039 if (derived->f2k_derived->tb_uop_root)
12040 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12041 &resolve_typebound_user_op);
12043 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12045 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12046 if (p && !resolve_typebound_intrinsic_op (derived,
12047 (gfc_intrinsic_op)op, p))
12048 resolve_bindings_result = false;
12051 return resolve_bindings_result;
12055 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12056 to give all identical derived types the same backend_decl. */
12057 static void
12058 add_dt_to_dt_list (gfc_symbol *derived)
12060 gfc_dt_list *dt_list;
12062 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12063 if (derived == dt_list->derived)
12064 return;
12066 dt_list = gfc_get_dt_list ();
12067 dt_list->next = gfc_derived_types;
12068 dt_list->derived = derived;
12069 gfc_derived_types = dt_list;
12073 /* Ensure that a derived-type is really not abstract, meaning that every
12074 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12076 static bool
12077 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12079 if (!st)
12080 return true;
12082 if (!ensure_not_abstract_walker (sub, st->left))
12083 return false;
12084 if (!ensure_not_abstract_walker (sub, st->right))
12085 return false;
12087 if (st->n.tb && st->n.tb->deferred)
12089 gfc_symtree* overriding;
12090 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12091 if (!overriding)
12092 return false;
12093 gcc_assert (overriding->n.tb);
12094 if (overriding->n.tb->deferred)
12096 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
12097 " '%s' is DEFERRED and not overridden",
12098 sub->name, &sub->declared_at, st->name);
12099 return false;
12103 return true;
12106 static bool
12107 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12109 /* The algorithm used here is to recursively travel up the ancestry of sub
12110 and for each ancestor-type, check all bindings. If any of them is
12111 DEFERRED, look it up starting from sub and see if the found (overriding)
12112 binding is not DEFERRED.
12113 This is not the most efficient way to do this, but it should be ok and is
12114 clearer than something sophisticated. */
12116 gcc_assert (ancestor && !sub->attr.abstract);
12118 if (!ancestor->attr.abstract)
12119 return true;
12121 /* Walk bindings of this ancestor. */
12122 if (ancestor->f2k_derived)
12124 bool t;
12125 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12126 if (!t)
12127 return false;
12130 /* Find next ancestor type and recurse on it. */
12131 ancestor = gfc_get_derived_super_type (ancestor);
12132 if (ancestor)
12133 return ensure_not_abstract (sub, ancestor);
12135 return true;
12139 /* This check for typebound defined assignments is done recursively
12140 since the order in which derived types are resolved is not always in
12141 order of the declarations. */
12143 static void
12144 check_defined_assignments (gfc_symbol *derived)
12146 gfc_component *c;
12148 for (c = derived->components; c; c = c->next)
12150 if (c->ts.type != BT_DERIVED
12151 || c->attr.pointer
12152 || c->attr.allocatable
12153 || c->attr.proc_pointer_comp
12154 || c->attr.class_pointer
12155 || c->attr.proc_pointer)
12156 continue;
12158 if (c->ts.u.derived->attr.defined_assign_comp
12159 || (c->ts.u.derived->f2k_derived
12160 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12162 derived->attr.defined_assign_comp = 1;
12163 return;
12166 check_defined_assignments (c->ts.u.derived);
12167 if (c->ts.u.derived->attr.defined_assign_comp)
12169 derived->attr.defined_assign_comp = 1;
12170 return;
12176 /* Resolve the components of a derived type. This does not have to wait until
12177 resolution stage, but can be done as soon as the dt declaration has been
12178 parsed. */
12180 static bool
12181 resolve_fl_derived0 (gfc_symbol *sym)
12183 gfc_symbol* super_type;
12184 gfc_component *c;
12186 if (sym->attr.unlimited_polymorphic)
12187 return true;
12189 super_type = gfc_get_derived_super_type (sym);
12191 /* F2008, C432. */
12192 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12194 gfc_error ("As extending type '%s' at %L has a coarray component, "
12195 "parent type '%s' shall also have one", sym->name,
12196 &sym->declared_at, super_type->name);
12197 return false;
12200 /* Ensure the extended type gets resolved before we do. */
12201 if (super_type && !resolve_fl_derived0 (super_type))
12202 return false;
12204 /* An ABSTRACT type must be extensible. */
12205 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12207 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12208 sym->name, &sym->declared_at);
12209 return false;
12212 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12213 : sym->components;
12215 for ( ; c != NULL; c = c->next)
12217 if (c->attr.artificial)
12218 continue;
12220 /* F2008, C442. */
12221 if ((!sym->attr.is_class || c != sym->components)
12222 && c->attr.codimension
12223 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12225 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12226 "deferred shape", c->name, &c->loc);
12227 return false;
12230 /* F2008, C443. */
12231 if (c->attr.codimension && c->ts.type == BT_DERIVED
12232 && c->ts.u.derived->ts.is_iso_c)
12234 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12235 "shall not be a coarray", c->name, &c->loc);
12236 return false;
12239 /* F2008, C444. */
12240 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12241 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12242 || c->attr.allocatable))
12244 gfc_error ("Component '%s' at %L with coarray component "
12245 "shall be a nonpointer, nonallocatable scalar",
12246 c->name, &c->loc);
12247 return false;
12250 /* F2008, C448. */
12251 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12253 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12254 "is not an array pointer", c->name, &c->loc);
12255 return false;
12258 if (c->attr.proc_pointer && c->ts.interface)
12260 gfc_symbol *ifc = c->ts.interface;
12262 if (!sym->attr.vtype
12263 && !check_proc_interface (ifc, &c->loc))
12264 return false;
12266 if (ifc->attr.if_source || ifc->attr.intrinsic)
12268 /* Resolve interface and copy attributes. */
12269 if (ifc->formal && !ifc->formal_ns)
12270 resolve_symbol (ifc);
12271 if (ifc->attr.intrinsic)
12272 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12274 if (ifc->result)
12276 c->ts = ifc->result->ts;
12277 c->attr.allocatable = ifc->result->attr.allocatable;
12278 c->attr.pointer = ifc->result->attr.pointer;
12279 c->attr.dimension = ifc->result->attr.dimension;
12280 c->as = gfc_copy_array_spec (ifc->result->as);
12281 c->attr.class_ok = ifc->result->attr.class_ok;
12283 else
12285 c->ts = ifc->ts;
12286 c->attr.allocatable = ifc->attr.allocatable;
12287 c->attr.pointer = ifc->attr.pointer;
12288 c->attr.dimension = ifc->attr.dimension;
12289 c->as = gfc_copy_array_spec (ifc->as);
12290 c->attr.class_ok = ifc->attr.class_ok;
12292 c->ts.interface = ifc;
12293 c->attr.function = ifc->attr.function;
12294 c->attr.subroutine = ifc->attr.subroutine;
12296 c->attr.pure = ifc->attr.pure;
12297 c->attr.elemental = ifc->attr.elemental;
12298 c->attr.recursive = ifc->attr.recursive;
12299 c->attr.always_explicit = ifc->attr.always_explicit;
12300 c->attr.ext_attr |= ifc->attr.ext_attr;
12301 /* Copy char length. */
12302 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12304 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12305 if (cl->length && !cl->resolved
12306 && !gfc_resolve_expr (cl->length))
12307 return false;
12308 c->ts.u.cl = cl;
12312 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12314 /* Since PPCs are not implicitly typed, a PPC without an explicit
12315 interface must be a subroutine. */
12316 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12319 /* Procedure pointer components: Check PASS arg. */
12320 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12321 && !sym->attr.vtype)
12323 gfc_symbol* me_arg;
12325 if (c->tb->pass_arg)
12327 gfc_formal_arglist* i;
12329 /* If an explicit passing argument name is given, walk the arg-list
12330 and look for it. */
12332 me_arg = NULL;
12333 c->tb->pass_arg_num = 1;
12334 for (i = c->ts.interface->formal; i; i = i->next)
12336 if (!strcmp (i->sym->name, c->tb->pass_arg))
12338 me_arg = i->sym;
12339 break;
12341 c->tb->pass_arg_num++;
12344 if (!me_arg)
12346 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12347 "at %L has no argument '%s'", c->name,
12348 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12349 c->tb->error = 1;
12350 return false;
12353 else
12355 /* Otherwise, take the first one; there should in fact be at least
12356 one. */
12357 c->tb->pass_arg_num = 1;
12358 if (!c->ts.interface->formal)
12360 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12361 "must have at least one argument",
12362 c->name, &c->loc);
12363 c->tb->error = 1;
12364 return false;
12366 me_arg = c->ts.interface->formal->sym;
12369 /* Now check that the argument-type matches. */
12370 gcc_assert (me_arg);
12371 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12372 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12373 || (me_arg->ts.type == BT_CLASS
12374 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12376 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12377 " the derived type '%s'", me_arg->name, c->name,
12378 me_arg->name, &c->loc, sym->name);
12379 c->tb->error = 1;
12380 return false;
12383 /* Check for C453. */
12384 if (me_arg->attr.dimension)
12386 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12387 "must be scalar", me_arg->name, c->name, me_arg->name,
12388 &c->loc);
12389 c->tb->error = 1;
12390 return false;
12393 if (me_arg->attr.pointer)
12395 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12396 "may not have the POINTER attribute", me_arg->name,
12397 c->name, me_arg->name, &c->loc);
12398 c->tb->error = 1;
12399 return false;
12402 if (me_arg->attr.allocatable)
12404 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12405 "may not be ALLOCATABLE", me_arg->name, c->name,
12406 me_arg->name, &c->loc);
12407 c->tb->error = 1;
12408 return false;
12411 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12412 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12413 " at %L", c->name, &c->loc);
12417 /* Check type-spec if this is not the parent-type component. */
12418 if (((sym->attr.is_class
12419 && (!sym->components->ts.u.derived->attr.extension
12420 || c != sym->components->ts.u.derived->components))
12421 || (!sym->attr.is_class
12422 && (!sym->attr.extension || c != sym->components)))
12423 && !sym->attr.vtype
12424 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12425 return false;
12427 /* If this type is an extension, set the accessibility of the parent
12428 component. */
12429 if (super_type
12430 && ((sym->attr.is_class
12431 && c == sym->components->ts.u.derived->components)
12432 || (!sym->attr.is_class && c == sym->components))
12433 && strcmp (super_type->name, c->name) == 0)
12434 c->attr.access = super_type->attr.access;
12436 /* If this type is an extension, see if this component has the same name
12437 as an inherited type-bound procedure. */
12438 if (super_type && !sym->attr.is_class
12439 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12441 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12442 " inherited type-bound procedure",
12443 c->name, sym->name, &c->loc);
12444 return false;
12447 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12448 && !c->ts.deferred)
12450 if (c->ts.u.cl->length == NULL
12451 || (!resolve_charlen(c->ts.u.cl))
12452 || !gfc_is_constant_expr (c->ts.u.cl->length))
12454 gfc_error ("Character length of component '%s' needs to "
12455 "be a constant specification expression at %L",
12456 c->name,
12457 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12458 return false;
12462 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12463 && !c->attr.pointer && !c->attr.allocatable)
12465 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12466 "length must be a POINTER or ALLOCATABLE",
12467 c->name, sym->name, &c->loc);
12468 return false;
12471 /* Add the hidden deferred length field. */
12472 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
12473 && !sym->attr.is_class)
12475 char name[GFC_MAX_SYMBOL_LEN+9];
12476 gfc_component *strlen;
12477 sprintf (name, "_%s_length", c->name);
12478 strlen = gfc_find_component (sym, name, true, true);
12479 if (strlen == NULL)
12481 if (!gfc_add_component (sym, name, &strlen))
12482 return false;
12483 strlen->ts.type = BT_INTEGER;
12484 strlen->ts.kind = gfc_charlen_int_kind;
12485 strlen->attr.access = ACCESS_PRIVATE;
12486 strlen->attr.deferred_parameter = 1;
12490 if (c->ts.type == BT_DERIVED
12491 && sym->component_access != ACCESS_PRIVATE
12492 && gfc_check_symbol_access (sym)
12493 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12494 && !c->ts.u.derived->attr.use_assoc
12495 && !gfc_check_symbol_access (c->ts.u.derived)
12496 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12497 "PRIVATE type and cannot be a component of "
12498 "'%s', which is PUBLIC at %L", c->name,
12499 sym->name, &sym->declared_at))
12500 return false;
12502 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12504 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12505 "type %s", c->name, &c->loc, sym->name);
12506 return false;
12509 if (sym->attr.sequence)
12511 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12513 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12514 "not have the SEQUENCE attribute",
12515 c->ts.u.derived->name, &sym->declared_at);
12516 return false;
12520 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12521 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12522 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12523 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12524 CLASS_DATA (c)->ts.u.derived
12525 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12527 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12528 && c->attr.pointer && c->ts.u.derived->components == NULL
12529 && !c->ts.u.derived->attr.zero_comp)
12531 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12532 "that has not been declared", c->name, sym->name,
12533 &c->loc);
12534 return false;
12537 if (c->ts.type == BT_CLASS && c->attr.class_ok
12538 && CLASS_DATA (c)->attr.class_pointer
12539 && CLASS_DATA (c)->ts.u.derived->components == NULL
12540 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12541 && !UNLIMITED_POLY (c))
12543 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12544 "that has not been declared", c->name, sym->name,
12545 &c->loc);
12546 return false;
12549 /* C437. */
12550 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12551 && (!c->attr.class_ok
12552 || !(CLASS_DATA (c)->attr.class_pointer
12553 || CLASS_DATA (c)->attr.allocatable)))
12555 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12556 "or pointer", c->name, &c->loc);
12557 /* Prevent a recurrence of the error. */
12558 c->ts.type = BT_UNKNOWN;
12559 return false;
12562 /* Ensure that all the derived type components are put on the
12563 derived type list; even in formal namespaces, where derived type
12564 pointer components might not have been declared. */
12565 if (c->ts.type == BT_DERIVED
12566 && c->ts.u.derived
12567 && c->ts.u.derived->components
12568 && c->attr.pointer
12569 && sym != c->ts.u.derived)
12570 add_dt_to_dt_list (c->ts.u.derived);
12572 if (!gfc_resolve_array_spec (c->as,
12573 !(c->attr.pointer || c->attr.proc_pointer
12574 || c->attr.allocatable)))
12575 return false;
12577 if (c->initializer && !sym->attr.vtype
12578 && !gfc_check_assign_symbol (sym, c, c->initializer))
12579 return false;
12582 check_defined_assignments (sym);
12584 if (!sym->attr.defined_assign_comp && super_type)
12585 sym->attr.defined_assign_comp
12586 = super_type->attr.defined_assign_comp;
12588 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12589 all DEFERRED bindings are overridden. */
12590 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12591 && !sym->attr.is_class
12592 && !ensure_not_abstract (sym, super_type))
12593 return false;
12595 /* Add derived type to the derived type list. */
12596 add_dt_to_dt_list (sym);
12598 return true;
12602 /* The following procedure does the full resolution of a derived type,
12603 including resolution of all type-bound procedures (if present). In contrast
12604 to 'resolve_fl_derived0' this can only be done after the module has been
12605 parsed completely. */
12607 static bool
12608 resolve_fl_derived (gfc_symbol *sym)
12610 gfc_symbol *gen_dt = NULL;
12612 if (sym->attr.unlimited_polymorphic)
12613 return true;
12615 if (!sym->attr.is_class)
12616 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12617 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12618 && (!gen_dt->generic->sym->attr.use_assoc
12619 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12620 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12621 "'%s' at %L being the same name as derived "
12622 "type at %L", sym->name,
12623 gen_dt->generic->sym == sym
12624 ? gen_dt->generic->next->sym->name
12625 : gen_dt->generic->sym->name,
12626 gen_dt->generic->sym == sym
12627 ? &gen_dt->generic->next->sym->declared_at
12628 : &gen_dt->generic->sym->declared_at,
12629 &sym->declared_at))
12630 return false;
12632 /* Resolve the finalizer procedures. */
12633 if (!gfc_resolve_finalizers (sym, NULL))
12634 return false;
12636 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12638 /* Fix up incomplete CLASS symbols. */
12639 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12640 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12642 /* Nothing more to do for unlimited polymorphic entities. */
12643 if (data->ts.u.derived->attr.unlimited_polymorphic)
12644 return true;
12645 else if (vptr->ts.u.derived == NULL)
12647 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12648 gcc_assert (vtab);
12649 vptr->ts.u.derived = vtab->ts.u.derived;
12653 if (!resolve_fl_derived0 (sym))
12654 return false;
12656 /* Resolve the type-bound procedures. */
12657 if (!resolve_typebound_procedures (sym))
12658 return false;
12660 return true;
12664 static bool
12665 resolve_fl_namelist (gfc_symbol *sym)
12667 gfc_namelist *nl;
12668 gfc_symbol *nlsym;
12670 for (nl = sym->namelist; nl; nl = nl->next)
12672 /* Check again, the check in match only works if NAMELIST comes
12673 after the decl. */
12674 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12676 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12677 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12678 return false;
12681 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12682 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12683 "with assumed shape in namelist '%s' at %L",
12684 nl->sym->name, sym->name, &sym->declared_at))
12685 return false;
12687 if (is_non_constant_shape_array (nl->sym)
12688 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12689 "with nonconstant shape in namelist '%s' at %L",
12690 nl->sym->name, sym->name, &sym->declared_at))
12691 return false;
12693 if (nl->sym->ts.type == BT_CHARACTER
12694 && (nl->sym->ts.u.cl->length == NULL
12695 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12696 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12697 "nonconstant character length in "
12698 "namelist '%s' at %L", nl->sym->name,
12699 sym->name, &sym->declared_at))
12700 return false;
12702 /* FIXME: Once UDDTIO is implemented, the following can be
12703 removed. */
12704 if (nl->sym->ts.type == BT_CLASS)
12706 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12707 "polymorphic and requires a defined input/output "
12708 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12709 return false;
12712 if (nl->sym->ts.type == BT_DERIVED
12713 && (nl->sym->ts.u.derived->attr.alloc_comp
12714 || nl->sym->ts.u.derived->attr.pointer_comp))
12716 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12717 "namelist '%s' at %L with ALLOCATABLE "
12718 "or POINTER components", nl->sym->name,
12719 sym->name, &sym->declared_at))
12720 return false;
12722 /* FIXME: Once UDDTIO is implemented, the following can be
12723 removed. */
12724 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12725 "ALLOCATABLE or POINTER components and thus requires "
12726 "a defined input/output procedure", nl->sym->name,
12727 sym->name, &sym->declared_at);
12728 return false;
12732 /* Reject PRIVATE objects in a PUBLIC namelist. */
12733 if (gfc_check_symbol_access (sym))
12735 for (nl = sym->namelist; nl; nl = nl->next)
12737 if (!nl->sym->attr.use_assoc
12738 && !is_sym_host_assoc (nl->sym, sym->ns)
12739 && !gfc_check_symbol_access (nl->sym))
12741 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12742 "cannot be member of PUBLIC namelist '%s' at %L",
12743 nl->sym->name, sym->name, &sym->declared_at);
12744 return false;
12747 /* Types with private components that came here by USE-association. */
12748 if (nl->sym->ts.type == BT_DERIVED
12749 && derived_inaccessible (nl->sym->ts.u.derived))
12751 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12752 "components and cannot be member of namelist '%s' at %L",
12753 nl->sym->name, sym->name, &sym->declared_at);
12754 return false;
12757 /* Types with private components that are defined in the same module. */
12758 if (nl->sym->ts.type == BT_DERIVED
12759 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12760 && nl->sym->ts.u.derived->attr.private_comp)
12762 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12763 "cannot be a member of PUBLIC namelist '%s' at %L",
12764 nl->sym->name, sym->name, &sym->declared_at);
12765 return false;
12771 /* 14.1.2 A module or internal procedure represent local entities
12772 of the same type as a namelist member and so are not allowed. */
12773 for (nl = sym->namelist; nl; nl = nl->next)
12775 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12776 continue;
12778 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12779 if ((nl->sym == sym->ns->proc_name)
12781 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12782 continue;
12784 nlsym = NULL;
12785 if (nl->sym->name)
12786 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12787 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12789 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12790 "attribute in '%s' at %L", nlsym->name,
12791 &sym->declared_at);
12792 return false;
12796 return true;
12800 static bool
12801 resolve_fl_parameter (gfc_symbol *sym)
12803 /* A parameter array's shape needs to be constant. */
12804 if (sym->as != NULL
12805 && (sym->as->type == AS_DEFERRED
12806 || is_non_constant_shape_array (sym)))
12808 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12809 "or of deferred shape", sym->name, &sym->declared_at);
12810 return false;
12813 /* Make sure a parameter that has been implicitly typed still
12814 matches the implicit type, since PARAMETER statements can precede
12815 IMPLICIT statements. */
12816 if (sym->attr.implicit_type
12817 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12818 sym->ns)))
12820 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12821 "later IMPLICIT type", sym->name, &sym->declared_at);
12822 return false;
12825 /* Make sure the types of derived parameters are consistent. This
12826 type checking is deferred until resolution because the type may
12827 refer to a derived type from the host. */
12828 if (sym->ts.type == BT_DERIVED
12829 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12831 gfc_error ("Incompatible derived type in PARAMETER at %L",
12832 &sym->value->where);
12833 return false;
12835 return true;
12839 /* Do anything necessary to resolve a symbol. Right now, we just
12840 assume that an otherwise unknown symbol is a variable. This sort
12841 of thing commonly happens for symbols in module. */
12843 static void
12844 resolve_symbol (gfc_symbol *sym)
12846 int check_constant, mp_flag;
12847 gfc_symtree *symtree;
12848 gfc_symtree *this_symtree;
12849 gfc_namespace *ns;
12850 gfc_component *c;
12851 symbol_attribute class_attr;
12852 gfc_array_spec *as;
12853 bool saved_specification_expr;
12855 if (sym->resolved)
12856 return;
12857 sym->resolved = 1;
12859 if (sym->attr.artificial)
12860 return;
12862 if (sym->attr.unlimited_polymorphic)
12863 return;
12865 if (sym->attr.flavor == FL_UNKNOWN
12866 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12867 && !sym->attr.generic && !sym->attr.external
12868 && sym->attr.if_source == IFSRC_UNKNOWN
12869 && sym->ts.type == BT_UNKNOWN))
12872 /* If we find that a flavorless symbol is an interface in one of the
12873 parent namespaces, find its symtree in this namespace, free the
12874 symbol and set the symtree to point to the interface symbol. */
12875 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12877 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12878 if (symtree && (symtree->n.sym->generic ||
12879 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12880 && sym->ns->construct_entities)))
12882 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12883 sym->name);
12884 gfc_release_symbol (sym);
12885 symtree->n.sym->refs++;
12886 this_symtree->n.sym = symtree->n.sym;
12887 return;
12891 /* Otherwise give it a flavor according to such attributes as
12892 it has. */
12893 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12894 && sym->attr.intrinsic == 0)
12895 sym->attr.flavor = FL_VARIABLE;
12896 else if (sym->attr.flavor == FL_UNKNOWN)
12898 sym->attr.flavor = FL_PROCEDURE;
12899 if (sym->attr.dimension)
12900 sym->attr.function = 1;
12904 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12905 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12907 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12908 && !resolve_procedure_interface (sym))
12909 return;
12911 if (sym->attr.is_protected && !sym->attr.proc_pointer
12912 && (sym->attr.procedure || sym->attr.external))
12914 if (sym->attr.external)
12915 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12916 "at %L", &sym->declared_at);
12917 else
12918 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12919 "at %L", &sym->declared_at);
12921 return;
12924 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12925 return;
12927 /* Symbols that are module procedures with results (functions) have
12928 the types and array specification copied for type checking in
12929 procedures that call them, as well as for saving to a module
12930 file. These symbols can't stand the scrutiny that their results
12931 can. */
12932 mp_flag = (sym->result != NULL && sym->result != sym);
12934 /* Make sure that the intrinsic is consistent with its internal
12935 representation. This needs to be done before assigning a default
12936 type to avoid spurious warnings. */
12937 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12938 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12939 return;
12941 /* Resolve associate names. */
12942 if (sym->assoc)
12943 resolve_assoc_var (sym, true);
12945 /* Assign default type to symbols that need one and don't have one. */
12946 if (sym->ts.type == BT_UNKNOWN)
12948 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12950 gfc_set_default_type (sym, 1, NULL);
12953 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12954 && !sym->attr.function && !sym->attr.subroutine
12955 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12956 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12958 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12960 /* The specific case of an external procedure should emit an error
12961 in the case that there is no implicit type. */
12962 if (!mp_flag)
12963 gfc_set_default_type (sym, sym->attr.external, NULL);
12964 else
12966 /* Result may be in another namespace. */
12967 resolve_symbol (sym->result);
12969 if (!sym->result->attr.proc_pointer)
12971 sym->ts = sym->result->ts;
12972 sym->as = gfc_copy_array_spec (sym->result->as);
12973 sym->attr.dimension = sym->result->attr.dimension;
12974 sym->attr.pointer = sym->result->attr.pointer;
12975 sym->attr.allocatable = sym->result->attr.allocatable;
12976 sym->attr.contiguous = sym->result->attr.contiguous;
12981 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12983 bool saved_specification_expr = specification_expr;
12984 specification_expr = true;
12985 gfc_resolve_array_spec (sym->result->as, false);
12986 specification_expr = saved_specification_expr;
12989 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12991 as = CLASS_DATA (sym)->as;
12992 class_attr = CLASS_DATA (sym)->attr;
12993 class_attr.pointer = class_attr.class_pointer;
12995 else
12997 class_attr = sym->attr;
12998 as = sym->as;
13001 /* F2008, C530. */
13002 if (sym->attr.contiguous
13003 && (!class_attr.dimension
13004 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13005 && !class_attr.pointer)))
13007 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
13008 "array pointer or an assumed-shape or assumed-rank array",
13009 sym->name, &sym->declared_at);
13010 return;
13013 /* Assumed size arrays and assumed shape arrays must be dummy
13014 arguments. Array-spec's of implied-shape should have been resolved to
13015 AS_EXPLICIT already. */
13017 if (as)
13019 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13020 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13021 || as->type == AS_ASSUMED_SHAPE)
13022 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13024 if (as->type == AS_ASSUMED_SIZE)
13025 gfc_error ("Assumed size array at %L must be a dummy argument",
13026 &sym->declared_at);
13027 else
13028 gfc_error ("Assumed shape array at %L must be a dummy argument",
13029 &sym->declared_at);
13030 return;
13032 /* TS 29113, C535a. */
13033 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13034 && !sym->attr.select_type_temporary)
13036 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13037 &sym->declared_at);
13038 return;
13040 if (as->type == AS_ASSUMED_RANK
13041 && (sym->attr.codimension || sym->attr.value))
13043 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13044 "CODIMENSION attribute", &sym->declared_at);
13045 return;
13049 /* Make sure symbols with known intent or optional are really dummy
13050 variable. Because of ENTRY statement, this has to be deferred
13051 until resolution time. */
13053 if (!sym->attr.dummy
13054 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13056 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13057 return;
13060 if (sym->attr.value && !sym->attr.dummy)
13062 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
13063 "it is not a dummy argument", sym->name, &sym->declared_at);
13064 return;
13067 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13069 gfc_charlen *cl = sym->ts.u.cl;
13070 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13072 gfc_error ("Character dummy variable '%s' at %L with VALUE "
13073 "attribute must have constant length",
13074 sym->name, &sym->declared_at);
13075 return;
13078 if (sym->ts.is_c_interop
13079 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13081 gfc_error ("C interoperable character dummy variable '%s' at %L "
13082 "with VALUE attribute must have length one",
13083 sym->name, &sym->declared_at);
13084 return;
13088 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13089 && sym->ts.u.derived->attr.generic)
13091 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13092 if (!sym->ts.u.derived)
13094 gfc_error ("The derived type '%s' at %L is of type '%s', "
13095 "which has not been defined", sym->name,
13096 &sym->declared_at, sym->ts.u.derived->name);
13097 sym->ts.type = BT_UNKNOWN;
13098 return;
13102 /* Use the same constraints as TYPE(*), except for the type check
13103 and that only scalars and assumed-size arrays are permitted. */
13104 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13106 if (!sym->attr.dummy)
13108 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13109 "a dummy argument", sym->name, &sym->declared_at);
13110 return;
13113 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13114 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13115 && sym->ts.type != BT_COMPLEX)
13117 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13118 "of type TYPE(*) or of an numeric intrinsic type",
13119 sym->name, &sym->declared_at);
13120 return;
13123 if (sym->attr.allocatable || sym->attr.codimension
13124 || sym->attr.pointer || sym->attr.value)
13126 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13127 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13128 "attribute", sym->name, &sym->declared_at);
13129 return;
13132 if (sym->attr.intent == INTENT_OUT)
13134 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13135 "have the INTENT(OUT) attribute",
13136 sym->name, &sym->declared_at);
13137 return;
13139 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13141 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13142 "either be a scalar or an assumed-size array",
13143 sym->name, &sym->declared_at);
13144 return;
13147 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13148 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13149 packing. */
13150 sym->ts.type = BT_ASSUMED;
13151 sym->as = gfc_get_array_spec ();
13152 sym->as->type = AS_ASSUMED_SIZE;
13153 sym->as->rank = 1;
13154 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13156 else if (sym->ts.type == BT_ASSUMED)
13158 /* TS 29113, C407a. */
13159 if (!sym->attr.dummy)
13161 gfc_error ("Assumed type of variable %s at %L is only permitted "
13162 "for dummy variables", sym->name, &sym->declared_at);
13163 return;
13165 if (sym->attr.allocatable || sym->attr.codimension
13166 || sym->attr.pointer || sym->attr.value)
13168 gfc_error ("Assumed-type variable %s at %L may not have the "
13169 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13170 sym->name, &sym->declared_at);
13171 return;
13173 if (sym->attr.intent == INTENT_OUT)
13175 gfc_error ("Assumed-type variable %s at %L may not have the "
13176 "INTENT(OUT) attribute",
13177 sym->name, &sym->declared_at);
13178 return;
13180 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13182 gfc_error ("Assumed-type variable %s at %L shall not be an "
13183 "explicit-shape array", sym->name, &sym->declared_at);
13184 return;
13188 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13189 do this for something that was implicitly typed because that is handled
13190 in gfc_set_default_type. Handle dummy arguments and procedure
13191 definitions separately. Also, anything that is use associated is not
13192 handled here but instead is handled in the module it is declared in.
13193 Finally, derived type definitions are allowed to be BIND(C) since that
13194 only implies that they're interoperable, and they are checked fully for
13195 interoperability when a variable is declared of that type. */
13196 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13197 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13198 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13200 bool t = true;
13202 /* First, make sure the variable is declared at the
13203 module-level scope (J3/04-007, Section 15.3). */
13204 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13205 sym->attr.in_common == 0)
13207 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13208 "is neither a COMMON block nor declared at the "
13209 "module level scope", sym->name, &(sym->declared_at));
13210 t = false;
13212 else if (sym->common_head != NULL)
13214 t = verify_com_block_vars_c_interop (sym->common_head);
13216 else
13218 /* If type() declaration, we need to verify that the components
13219 of the given type are all C interoperable, etc. */
13220 if (sym->ts.type == BT_DERIVED &&
13221 sym->ts.u.derived->attr.is_c_interop != 1)
13223 /* Make sure the user marked the derived type as BIND(C). If
13224 not, call the verify routine. This could print an error
13225 for the derived type more than once if multiple variables
13226 of that type are declared. */
13227 if (sym->ts.u.derived->attr.is_bind_c != 1)
13228 verify_bind_c_derived_type (sym->ts.u.derived);
13229 t = false;
13232 /* Verify the variable itself as C interoperable if it
13233 is BIND(C). It is not possible for this to succeed if
13234 the verify_bind_c_derived_type failed, so don't have to handle
13235 any error returned by verify_bind_c_derived_type. */
13236 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13237 sym->common_block);
13240 if (!t)
13242 /* clear the is_bind_c flag to prevent reporting errors more than
13243 once if something failed. */
13244 sym->attr.is_bind_c = 0;
13245 return;
13249 /* If a derived type symbol has reached this point, without its
13250 type being declared, we have an error. Notice that most
13251 conditions that produce undefined derived types have already
13252 been dealt with. However, the likes of:
13253 implicit type(t) (t) ..... call foo (t) will get us here if
13254 the type is not declared in the scope of the implicit
13255 statement. Change the type to BT_UNKNOWN, both because it is so
13256 and to prevent an ICE. */
13257 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13258 && sym->ts.u.derived->components == NULL
13259 && !sym->ts.u.derived->attr.zero_comp)
13261 gfc_error ("The derived type '%s' at %L is of type '%s', "
13262 "which has not been defined", sym->name,
13263 &sym->declared_at, sym->ts.u.derived->name);
13264 sym->ts.type = BT_UNKNOWN;
13265 return;
13268 /* Make sure that the derived type has been resolved and that the
13269 derived type is visible in the symbol's namespace, if it is a
13270 module function and is not PRIVATE. */
13271 if (sym->ts.type == BT_DERIVED
13272 && sym->ts.u.derived->attr.use_assoc
13273 && sym->ns->proc_name
13274 && sym->ns->proc_name->attr.flavor == FL_MODULE
13275 && !resolve_fl_derived (sym->ts.u.derived))
13276 return;
13278 /* Unless the derived-type declaration is use associated, Fortran 95
13279 does not allow public entries of private derived types.
13280 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13281 161 in 95-006r3. */
13282 if (sym->ts.type == BT_DERIVED
13283 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13284 && !sym->ts.u.derived->attr.use_assoc
13285 && gfc_check_symbol_access (sym)
13286 && !gfc_check_symbol_access (sym->ts.u.derived)
13287 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13288 "derived type '%s'",
13289 (sym->attr.flavor == FL_PARAMETER)
13290 ? "parameter" : "variable",
13291 sym->name, &sym->declared_at,
13292 sym->ts.u.derived->name))
13293 return;
13295 /* F2008, C1302. */
13296 if (sym->ts.type == BT_DERIVED
13297 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13298 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13299 || sym->ts.u.derived->attr.lock_comp)
13300 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13302 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13303 "type LOCK_TYPE must be a coarray", sym->name,
13304 &sym->declared_at);
13305 return;
13308 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13309 default initialization is defined (5.1.2.4.4). */
13310 if (sym->ts.type == BT_DERIVED
13311 && sym->attr.dummy
13312 && sym->attr.intent == INTENT_OUT
13313 && sym->as
13314 && sym->as->type == AS_ASSUMED_SIZE)
13316 for (c = sym->ts.u.derived->components; c; c = c->next)
13318 if (c->initializer)
13320 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13321 "ASSUMED SIZE and so cannot have a default initializer",
13322 sym->name, &sym->declared_at);
13323 return;
13328 /* F2008, C542. */
13329 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13330 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13332 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13333 "INTENT(OUT)", sym->name, &sym->declared_at);
13334 return;
13337 /* F2008, C525. */
13338 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13339 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13340 && CLASS_DATA (sym)->attr.coarray_comp))
13341 || class_attr.codimension)
13342 && (sym->attr.result || sym->result == sym))
13344 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13345 "a coarray component", sym->name, &sym->declared_at);
13346 return;
13349 /* F2008, C524. */
13350 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13351 && sym->ts.u.derived->ts.is_iso_c)
13353 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13354 "shall not be a coarray", sym->name, &sym->declared_at);
13355 return;
13358 /* F2008, C525. */
13359 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13360 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13361 && CLASS_DATA (sym)->attr.coarray_comp))
13362 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13363 || class_attr.allocatable))
13365 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13366 "nonpointer, nonallocatable scalar, which is not a coarray",
13367 sym->name, &sym->declared_at);
13368 return;
13371 /* F2008, C526. The function-result case was handled above. */
13372 if (class_attr.codimension
13373 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13374 || sym->attr.select_type_temporary
13375 || sym->ns->save_all
13376 || sym->ns->proc_name->attr.flavor == FL_MODULE
13377 || sym->ns->proc_name->attr.is_main_program
13378 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13380 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13381 "nor a dummy argument", sym->name, &sym->declared_at);
13382 return;
13384 /* F2008, C528. */
13385 else if (class_attr.codimension && !sym->attr.select_type_temporary
13386 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13388 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13389 "deferred shape", sym->name, &sym->declared_at);
13390 return;
13392 else if (class_attr.codimension && class_attr.allocatable && as
13393 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13395 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13396 "deferred shape", sym->name, &sym->declared_at);
13397 return;
13400 /* F2008, C541. */
13401 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13402 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13403 && CLASS_DATA (sym)->attr.coarray_comp))
13404 || (class_attr.codimension && class_attr.allocatable))
13405 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13407 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13408 "allocatable coarray or have coarray components",
13409 sym->name, &sym->declared_at);
13410 return;
13413 if (class_attr.codimension && sym->attr.dummy
13414 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13416 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13417 "procedure '%s'", sym->name, &sym->declared_at,
13418 sym->ns->proc_name->name);
13419 return;
13422 if (sym->ts.type == BT_LOGICAL
13423 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13424 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13425 && sym->ns->proc_name->attr.is_bind_c)))
13427 int i;
13428 for (i = 0; gfc_logical_kinds[i].kind; i++)
13429 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13430 break;
13431 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13432 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13433 "%L with non-C_Bool kind in BIND(C) procedure "
13434 "'%s'", sym->name, &sym->declared_at,
13435 sym->ns->proc_name->name))
13436 return;
13437 else if (!gfc_logical_kinds[i].c_bool
13438 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13439 "'%s' at %L with non-C_Bool kind in "
13440 "BIND(C) procedure '%s'", sym->name,
13441 &sym->declared_at,
13442 sym->attr.function ? sym->name
13443 : sym->ns->proc_name->name))
13444 return;
13447 switch (sym->attr.flavor)
13449 case FL_VARIABLE:
13450 if (!resolve_fl_variable (sym, mp_flag))
13451 return;
13452 break;
13454 case FL_PROCEDURE:
13455 if (!resolve_fl_procedure (sym, mp_flag))
13456 return;
13457 break;
13459 case FL_NAMELIST:
13460 if (!resolve_fl_namelist (sym))
13461 return;
13462 break;
13464 case FL_PARAMETER:
13465 if (!resolve_fl_parameter (sym))
13466 return;
13467 break;
13469 default:
13470 break;
13473 /* Resolve array specifier. Check as well some constraints
13474 on COMMON blocks. */
13476 check_constant = sym->attr.in_common && !sym->attr.pointer;
13478 /* Set the formal_arg_flag so that check_conflict will not throw
13479 an error for host associated variables in the specification
13480 expression for an array_valued function. */
13481 if (sym->attr.function && sym->as)
13482 formal_arg_flag = 1;
13484 saved_specification_expr = specification_expr;
13485 specification_expr = true;
13486 gfc_resolve_array_spec (sym->as, check_constant);
13487 specification_expr = saved_specification_expr;
13489 formal_arg_flag = 0;
13491 /* Resolve formal namespaces. */
13492 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13493 && !sym->attr.contained && !sym->attr.intrinsic)
13494 gfc_resolve (sym->formal_ns);
13496 /* Make sure the formal namespace is present. */
13497 if (sym->formal && !sym->formal_ns)
13499 gfc_formal_arglist *formal = sym->formal;
13500 while (formal && !formal->sym)
13501 formal = formal->next;
13503 if (formal)
13505 sym->formal_ns = formal->sym->ns;
13506 if (sym->ns != formal->sym->ns)
13507 sym->formal_ns->refs++;
13511 /* Check threadprivate restrictions. */
13512 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13513 && (!sym->attr.in_common
13514 && sym->module == NULL
13515 && (sym->ns->proc_name == NULL
13516 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13517 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13519 /* If we have come this far we can apply default-initializers, as
13520 described in 14.7.5, to those variables that have not already
13521 been assigned one. */
13522 if (sym->ts.type == BT_DERIVED
13523 && !sym->value
13524 && !sym->attr.allocatable
13525 && !sym->attr.alloc_comp)
13527 symbol_attribute *a = &sym->attr;
13529 if ((!a->save && !a->dummy && !a->pointer
13530 && !a->in_common && !a->use_assoc
13531 && (a->referenced || a->result)
13532 && !(a->function && sym != sym->result))
13533 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13534 apply_default_init (sym);
13537 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13538 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13539 && !CLASS_DATA (sym)->attr.class_pointer
13540 && !CLASS_DATA (sym)->attr.allocatable)
13541 apply_default_init (sym);
13543 /* If this symbol has a type-spec, check it. */
13544 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13545 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13546 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13547 return;
13551 /************* Resolve DATA statements *************/
13553 static struct
13555 gfc_data_value *vnode;
13556 mpz_t left;
13558 values;
13561 /* Advance the values structure to point to the next value in the data list. */
13563 static bool
13564 next_data_value (void)
13566 while (mpz_cmp_ui (values.left, 0) == 0)
13569 if (values.vnode->next == NULL)
13570 return false;
13572 values.vnode = values.vnode->next;
13573 mpz_set (values.left, values.vnode->repeat);
13576 return true;
13580 static bool
13581 check_data_variable (gfc_data_variable *var, locus *where)
13583 gfc_expr *e;
13584 mpz_t size;
13585 mpz_t offset;
13586 bool t;
13587 ar_type mark = AR_UNKNOWN;
13588 int i;
13589 mpz_t section_index[GFC_MAX_DIMENSIONS];
13590 gfc_ref *ref;
13591 gfc_array_ref *ar;
13592 gfc_symbol *sym;
13593 int has_pointer;
13595 if (!gfc_resolve_expr (var->expr))
13596 return false;
13598 ar = NULL;
13599 mpz_init_set_si (offset, 0);
13600 e = var->expr;
13602 if (e->expr_type != EXPR_VARIABLE)
13603 gfc_internal_error ("check_data_variable(): Bad expression");
13605 sym = e->symtree->n.sym;
13607 if (sym->ns->is_block_data && !sym->attr.in_common)
13609 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13610 sym->name, &sym->declared_at);
13613 if (e->ref == NULL && sym->as)
13615 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13616 " declaration", sym->name, where);
13617 return false;
13620 has_pointer = sym->attr.pointer;
13622 if (gfc_is_coindexed (e))
13624 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13625 where);
13626 return false;
13629 for (ref = e->ref; ref; ref = ref->next)
13631 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13632 has_pointer = 1;
13634 if (has_pointer
13635 && ref->type == REF_ARRAY
13636 && ref->u.ar.type != AR_FULL)
13638 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13639 "be a full array", sym->name, where);
13640 return false;
13644 if (e->rank == 0 || has_pointer)
13646 mpz_init_set_ui (size, 1);
13647 ref = NULL;
13649 else
13651 ref = e->ref;
13653 /* Find the array section reference. */
13654 for (ref = e->ref; ref; ref = ref->next)
13656 if (ref->type != REF_ARRAY)
13657 continue;
13658 if (ref->u.ar.type == AR_ELEMENT)
13659 continue;
13660 break;
13662 gcc_assert (ref);
13664 /* Set marks according to the reference pattern. */
13665 switch (ref->u.ar.type)
13667 case AR_FULL:
13668 mark = AR_FULL;
13669 break;
13671 case AR_SECTION:
13672 ar = &ref->u.ar;
13673 /* Get the start position of array section. */
13674 gfc_get_section_index (ar, section_index, &offset);
13675 mark = AR_SECTION;
13676 break;
13678 default:
13679 gcc_unreachable ();
13682 if (!gfc_array_size (e, &size))
13684 gfc_error ("Nonconstant array section at %L in DATA statement",
13685 &e->where);
13686 mpz_clear (offset);
13687 return false;
13691 t = true;
13693 while (mpz_cmp_ui (size, 0) > 0)
13695 if (!next_data_value ())
13697 gfc_error ("DATA statement at %L has more variables than values",
13698 where);
13699 t = false;
13700 break;
13703 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13704 if (!t)
13705 break;
13707 /* If we have more than one element left in the repeat count,
13708 and we have more than one element left in the target variable,
13709 then create a range assignment. */
13710 /* FIXME: Only done for full arrays for now, since array sections
13711 seem tricky. */
13712 if (mark == AR_FULL && ref && ref->next == NULL
13713 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13715 mpz_t range;
13717 if (mpz_cmp (size, values.left) >= 0)
13719 mpz_init_set (range, values.left);
13720 mpz_sub (size, size, values.left);
13721 mpz_set_ui (values.left, 0);
13723 else
13725 mpz_init_set (range, size);
13726 mpz_sub (values.left, values.left, size);
13727 mpz_set_ui (size, 0);
13730 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13731 offset, &range);
13733 mpz_add (offset, offset, range);
13734 mpz_clear (range);
13736 if (!t)
13737 break;
13740 /* Assign initial value to symbol. */
13741 else
13743 mpz_sub_ui (values.left, values.left, 1);
13744 mpz_sub_ui (size, size, 1);
13746 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13747 offset, NULL);
13748 if (!t)
13749 break;
13751 if (mark == AR_FULL)
13752 mpz_add_ui (offset, offset, 1);
13754 /* Modify the array section indexes and recalculate the offset
13755 for next element. */
13756 else if (mark == AR_SECTION)
13757 gfc_advance_section (section_index, ar, &offset);
13761 if (mark == AR_SECTION)
13763 for (i = 0; i < ar->dimen; i++)
13764 mpz_clear (section_index[i]);
13767 mpz_clear (size);
13768 mpz_clear (offset);
13770 return t;
13774 static bool traverse_data_var (gfc_data_variable *, locus *);
13776 /* Iterate over a list of elements in a DATA statement. */
13778 static bool
13779 traverse_data_list (gfc_data_variable *var, locus *where)
13781 mpz_t trip;
13782 iterator_stack frame;
13783 gfc_expr *e, *start, *end, *step;
13784 bool retval = true;
13786 mpz_init (frame.value);
13787 mpz_init (trip);
13789 start = gfc_copy_expr (var->iter.start);
13790 end = gfc_copy_expr (var->iter.end);
13791 step = gfc_copy_expr (var->iter.step);
13793 if (!gfc_simplify_expr (start, 1)
13794 || start->expr_type != EXPR_CONSTANT)
13796 gfc_error ("start of implied-do loop at %L could not be "
13797 "simplified to a constant value", &start->where);
13798 retval = false;
13799 goto cleanup;
13801 if (!gfc_simplify_expr (end, 1)
13802 || end->expr_type != EXPR_CONSTANT)
13804 gfc_error ("end of implied-do loop at %L could not be "
13805 "simplified to a constant value", &start->where);
13806 retval = false;
13807 goto cleanup;
13809 if (!gfc_simplify_expr (step, 1)
13810 || step->expr_type != EXPR_CONSTANT)
13812 gfc_error ("step of implied-do loop at %L could not be "
13813 "simplified to a constant value", &start->where);
13814 retval = false;
13815 goto cleanup;
13818 mpz_set (trip, end->value.integer);
13819 mpz_sub (trip, trip, start->value.integer);
13820 mpz_add (trip, trip, step->value.integer);
13822 mpz_div (trip, trip, step->value.integer);
13824 mpz_set (frame.value, start->value.integer);
13826 frame.prev = iter_stack;
13827 frame.variable = var->iter.var->symtree;
13828 iter_stack = &frame;
13830 while (mpz_cmp_ui (trip, 0) > 0)
13832 if (!traverse_data_var (var->list, where))
13834 retval = false;
13835 goto cleanup;
13838 e = gfc_copy_expr (var->expr);
13839 if (!gfc_simplify_expr (e, 1))
13841 gfc_free_expr (e);
13842 retval = false;
13843 goto cleanup;
13846 mpz_add (frame.value, frame.value, step->value.integer);
13848 mpz_sub_ui (trip, trip, 1);
13851 cleanup:
13852 mpz_clear (frame.value);
13853 mpz_clear (trip);
13855 gfc_free_expr (start);
13856 gfc_free_expr (end);
13857 gfc_free_expr (step);
13859 iter_stack = frame.prev;
13860 return retval;
13864 /* Type resolve variables in the variable list of a DATA statement. */
13866 static bool
13867 traverse_data_var (gfc_data_variable *var, locus *where)
13869 bool t;
13871 for (; var; var = var->next)
13873 if (var->expr == NULL)
13874 t = traverse_data_list (var, where);
13875 else
13876 t = check_data_variable (var, where);
13878 if (!t)
13879 return false;
13882 return true;
13886 /* Resolve the expressions and iterators associated with a data statement.
13887 This is separate from the assignment checking because data lists should
13888 only be resolved once. */
13890 static bool
13891 resolve_data_variables (gfc_data_variable *d)
13893 for (; d; d = d->next)
13895 if (d->list == NULL)
13897 if (!gfc_resolve_expr (d->expr))
13898 return false;
13900 else
13902 if (!gfc_resolve_iterator (&d->iter, false, true))
13903 return false;
13905 if (!resolve_data_variables (d->list))
13906 return false;
13910 return true;
13914 /* Resolve a single DATA statement. We implement this by storing a pointer to
13915 the value list into static variables, and then recursively traversing the
13916 variables list, expanding iterators and such. */
13918 static void
13919 resolve_data (gfc_data *d)
13922 if (!resolve_data_variables (d->var))
13923 return;
13925 values.vnode = d->value;
13926 if (d->value == NULL)
13927 mpz_set_ui (values.left, 0);
13928 else
13929 mpz_set (values.left, d->value->repeat);
13931 if (!traverse_data_var (d->var, &d->where))
13932 return;
13934 /* At this point, we better not have any values left. */
13936 if (next_data_value ())
13937 gfc_error ("DATA statement at %L has more values than variables",
13938 &d->where);
13942 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13943 accessed by host or use association, is a dummy argument to a pure function,
13944 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13945 is storage associated with any such variable, shall not be used in the
13946 following contexts: (clients of this function). */
13948 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13949 procedure. Returns zero if assignment is OK, nonzero if there is a
13950 problem. */
13952 gfc_impure_variable (gfc_symbol *sym)
13954 gfc_symbol *proc;
13955 gfc_namespace *ns;
13957 if (sym->attr.use_assoc || sym->attr.in_common)
13958 return 1;
13960 /* Check if the symbol's ns is inside the pure procedure. */
13961 for (ns = gfc_current_ns; ns; ns = ns->parent)
13963 if (ns == sym->ns)
13964 break;
13965 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13966 return 1;
13969 proc = sym->ns->proc_name;
13970 if (sym->attr.dummy
13971 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13972 || proc->attr.function))
13973 return 1;
13975 /* TODO: Sort out what can be storage associated, if anything, and include
13976 it here. In principle equivalences should be scanned but it does not
13977 seem to be possible to storage associate an impure variable this way. */
13978 return 0;
13982 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13983 current namespace is inside a pure procedure. */
13986 gfc_pure (gfc_symbol *sym)
13988 symbol_attribute attr;
13989 gfc_namespace *ns;
13991 if (sym == NULL)
13993 /* Check if the current namespace or one of its parents
13994 belongs to a pure procedure. */
13995 for (ns = gfc_current_ns; ns; ns = ns->parent)
13997 sym = ns->proc_name;
13998 if (sym == NULL)
13999 return 0;
14000 attr = sym->attr;
14001 if (attr.flavor == FL_PROCEDURE && attr.pure)
14002 return 1;
14004 return 0;
14007 attr = sym->attr;
14009 return attr.flavor == FL_PROCEDURE && attr.pure;
14013 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14014 checks if the current namespace is implicitly pure. Note that this
14015 function returns false for a PURE procedure. */
14018 gfc_implicit_pure (gfc_symbol *sym)
14020 gfc_namespace *ns;
14022 if (sym == NULL)
14024 /* Check if the current procedure is implicit_pure. Walk up
14025 the procedure list until we find a procedure. */
14026 for (ns = gfc_current_ns; ns; ns = ns->parent)
14028 sym = ns->proc_name;
14029 if (sym == NULL)
14030 return 0;
14032 if (sym->attr.flavor == FL_PROCEDURE)
14033 break;
14037 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14038 && !sym->attr.pure;
14042 void
14043 gfc_unset_implicit_pure (gfc_symbol *sym)
14045 gfc_namespace *ns;
14047 if (sym == NULL)
14049 /* Check if the current procedure is implicit_pure. Walk up
14050 the procedure list until we find a procedure. */
14051 for (ns = gfc_current_ns; ns; ns = ns->parent)
14053 sym = ns->proc_name;
14054 if (sym == NULL)
14055 return;
14057 if (sym->attr.flavor == FL_PROCEDURE)
14058 break;
14062 if (sym->attr.flavor == FL_PROCEDURE)
14063 sym->attr.implicit_pure = 0;
14064 else
14065 sym->attr.pure = 0;
14069 /* Test whether the current procedure is elemental or not. */
14072 gfc_elemental (gfc_symbol *sym)
14074 symbol_attribute attr;
14076 if (sym == NULL)
14077 sym = gfc_current_ns->proc_name;
14078 if (sym == NULL)
14079 return 0;
14080 attr = sym->attr;
14082 return attr.flavor == FL_PROCEDURE && attr.elemental;
14086 /* Warn about unused labels. */
14088 static void
14089 warn_unused_fortran_label (gfc_st_label *label)
14091 if (label == NULL)
14092 return;
14094 warn_unused_fortran_label (label->left);
14096 if (label->defined == ST_LABEL_UNKNOWN)
14097 return;
14099 switch (label->referenced)
14101 case ST_LABEL_UNKNOWN:
14102 gfc_warning ("Label %d at %L defined but not used", label->value,
14103 &label->where);
14104 break;
14106 case ST_LABEL_BAD_TARGET:
14107 gfc_warning ("Label %d at %L defined but cannot be used",
14108 label->value, &label->where);
14109 break;
14111 default:
14112 break;
14115 warn_unused_fortran_label (label->right);
14119 /* Returns the sequence type of a symbol or sequence. */
14121 static seq_type
14122 sequence_type (gfc_typespec ts)
14124 seq_type result;
14125 gfc_component *c;
14127 switch (ts.type)
14129 case BT_DERIVED:
14131 if (ts.u.derived->components == NULL)
14132 return SEQ_NONDEFAULT;
14134 result = sequence_type (ts.u.derived->components->ts);
14135 for (c = ts.u.derived->components->next; c; c = c->next)
14136 if (sequence_type (c->ts) != result)
14137 return SEQ_MIXED;
14139 return result;
14141 case BT_CHARACTER:
14142 if (ts.kind != gfc_default_character_kind)
14143 return SEQ_NONDEFAULT;
14145 return SEQ_CHARACTER;
14147 case BT_INTEGER:
14148 if (ts.kind != gfc_default_integer_kind)
14149 return SEQ_NONDEFAULT;
14151 return SEQ_NUMERIC;
14153 case BT_REAL:
14154 if (!(ts.kind == gfc_default_real_kind
14155 || ts.kind == gfc_default_double_kind))
14156 return SEQ_NONDEFAULT;
14158 return SEQ_NUMERIC;
14160 case BT_COMPLEX:
14161 if (ts.kind != gfc_default_complex_kind)
14162 return SEQ_NONDEFAULT;
14164 return SEQ_NUMERIC;
14166 case BT_LOGICAL:
14167 if (ts.kind != gfc_default_logical_kind)
14168 return SEQ_NONDEFAULT;
14170 return SEQ_NUMERIC;
14172 default:
14173 return SEQ_NONDEFAULT;
14178 /* Resolve derived type EQUIVALENCE object. */
14180 static bool
14181 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14183 gfc_component *c = derived->components;
14185 if (!derived)
14186 return true;
14188 /* Shall not be an object of nonsequence derived type. */
14189 if (!derived->attr.sequence)
14191 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14192 "attribute to be an EQUIVALENCE object", sym->name,
14193 &e->where);
14194 return false;
14197 /* Shall not have allocatable components. */
14198 if (derived->attr.alloc_comp)
14200 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14201 "components to be an EQUIVALENCE object",sym->name,
14202 &e->where);
14203 return false;
14206 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14208 gfc_error ("Derived type variable '%s' at %L with default "
14209 "initialization cannot be in EQUIVALENCE with a variable "
14210 "in COMMON", sym->name, &e->where);
14211 return false;
14214 for (; c ; c = c->next)
14216 if (c->ts.type == BT_DERIVED
14217 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14218 return false;
14220 /* Shall not be an object of sequence derived type containing a pointer
14221 in the structure. */
14222 if (c->attr.pointer)
14224 gfc_error ("Derived type variable '%s' at %L with pointer "
14225 "component(s) cannot be an EQUIVALENCE object",
14226 sym->name, &e->where);
14227 return false;
14230 return true;
14234 /* Resolve equivalence object.
14235 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14236 an allocatable array, an object of nonsequence derived type, an object of
14237 sequence derived type containing a pointer at any level of component
14238 selection, an automatic object, a function name, an entry name, a result
14239 name, a named constant, a structure component, or a subobject of any of
14240 the preceding objects. A substring shall not have length zero. A
14241 derived type shall not have components with default initialization nor
14242 shall two objects of an equivalence group be initialized.
14243 Either all or none of the objects shall have an protected attribute.
14244 The simple constraints are done in symbol.c(check_conflict) and the rest
14245 are implemented here. */
14247 static void
14248 resolve_equivalence (gfc_equiv *eq)
14250 gfc_symbol *sym;
14251 gfc_symbol *first_sym;
14252 gfc_expr *e;
14253 gfc_ref *r;
14254 locus *last_where = NULL;
14255 seq_type eq_type, last_eq_type;
14256 gfc_typespec *last_ts;
14257 int object, cnt_protected;
14258 const char *msg;
14260 last_ts = &eq->expr->symtree->n.sym->ts;
14262 first_sym = eq->expr->symtree->n.sym;
14264 cnt_protected = 0;
14266 for (object = 1; eq; eq = eq->eq, object++)
14268 e = eq->expr;
14270 e->ts = e->symtree->n.sym->ts;
14271 /* match_varspec might not know yet if it is seeing
14272 array reference or substring reference, as it doesn't
14273 know the types. */
14274 if (e->ref && e->ref->type == REF_ARRAY)
14276 gfc_ref *ref = e->ref;
14277 sym = e->symtree->n.sym;
14279 if (sym->attr.dimension)
14281 ref->u.ar.as = sym->as;
14282 ref = ref->next;
14285 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14286 if (e->ts.type == BT_CHARACTER
14287 && ref
14288 && ref->type == REF_ARRAY
14289 && ref->u.ar.dimen == 1
14290 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14291 && ref->u.ar.stride[0] == NULL)
14293 gfc_expr *start = ref->u.ar.start[0];
14294 gfc_expr *end = ref->u.ar.end[0];
14295 void *mem = NULL;
14297 /* Optimize away the (:) reference. */
14298 if (start == NULL && end == NULL)
14300 if (e->ref == ref)
14301 e->ref = ref->next;
14302 else
14303 e->ref->next = ref->next;
14304 mem = ref;
14306 else
14308 ref->type = REF_SUBSTRING;
14309 if (start == NULL)
14310 start = gfc_get_int_expr (gfc_default_integer_kind,
14311 NULL, 1);
14312 ref->u.ss.start = start;
14313 if (end == NULL && e->ts.u.cl)
14314 end = gfc_copy_expr (e->ts.u.cl->length);
14315 ref->u.ss.end = end;
14316 ref->u.ss.length = e->ts.u.cl;
14317 e->ts.u.cl = NULL;
14319 ref = ref->next;
14320 free (mem);
14323 /* Any further ref is an error. */
14324 if (ref)
14326 gcc_assert (ref->type == REF_ARRAY);
14327 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14328 &ref->u.ar.where);
14329 continue;
14333 if (!gfc_resolve_expr (e))
14334 continue;
14336 sym = e->symtree->n.sym;
14338 if (sym->attr.is_protected)
14339 cnt_protected++;
14340 if (cnt_protected > 0 && cnt_protected != object)
14342 gfc_error ("Either all or none of the objects in the "
14343 "EQUIVALENCE set at %L shall have the "
14344 "PROTECTED attribute",
14345 &e->where);
14346 break;
14349 /* Shall not equivalence common block variables in a PURE procedure. */
14350 if (sym->ns->proc_name
14351 && sym->ns->proc_name->attr.pure
14352 && sym->attr.in_common)
14354 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14355 "object in the pure procedure '%s'",
14356 sym->name, &e->where, sym->ns->proc_name->name);
14357 break;
14360 /* Shall not be a named constant. */
14361 if (e->expr_type == EXPR_CONSTANT)
14363 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14364 "object", sym->name, &e->where);
14365 continue;
14368 if (e->ts.type == BT_DERIVED
14369 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14370 continue;
14372 /* Check that the types correspond correctly:
14373 Note 5.28:
14374 A numeric sequence structure may be equivalenced to another sequence
14375 structure, an object of default integer type, default real type, double
14376 precision real type, default logical type such that components of the
14377 structure ultimately only become associated to objects of the same
14378 kind. A character sequence structure may be equivalenced to an object
14379 of default character kind or another character sequence structure.
14380 Other objects may be equivalenced only to objects of the same type and
14381 kind parameters. */
14383 /* Identical types are unconditionally OK. */
14384 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14385 goto identical_types;
14387 last_eq_type = sequence_type (*last_ts);
14388 eq_type = sequence_type (sym->ts);
14390 /* Since the pair of objects is not of the same type, mixed or
14391 non-default sequences can be rejected. */
14393 msg = "Sequence %s with mixed components in EQUIVALENCE "
14394 "statement at %L with different type objects";
14395 if ((object ==2
14396 && last_eq_type == SEQ_MIXED
14397 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14398 || (eq_type == SEQ_MIXED
14399 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14400 continue;
14402 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14403 "statement at %L with objects of different type";
14404 if ((object ==2
14405 && last_eq_type == SEQ_NONDEFAULT
14406 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14407 || (eq_type == SEQ_NONDEFAULT
14408 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14409 continue;
14411 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14412 "EQUIVALENCE statement at %L";
14413 if (last_eq_type == SEQ_CHARACTER
14414 && eq_type != SEQ_CHARACTER
14415 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14416 continue;
14418 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14419 "EQUIVALENCE statement at %L";
14420 if (last_eq_type == SEQ_NUMERIC
14421 && eq_type != SEQ_NUMERIC
14422 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14423 continue;
14425 identical_types:
14426 last_ts =&sym->ts;
14427 last_where = &e->where;
14429 if (!e->ref)
14430 continue;
14432 /* Shall not be an automatic array. */
14433 if (e->ref->type == REF_ARRAY
14434 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14436 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14437 "an EQUIVALENCE object", sym->name, &e->where);
14438 continue;
14441 r = e->ref;
14442 while (r)
14444 /* Shall not be a structure component. */
14445 if (r->type == REF_COMPONENT)
14447 gfc_error ("Structure component '%s' at %L cannot be an "
14448 "EQUIVALENCE object",
14449 r->u.c.component->name, &e->where);
14450 break;
14453 /* A substring shall not have length zero. */
14454 if (r->type == REF_SUBSTRING)
14456 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14458 gfc_error ("Substring at %L has length zero",
14459 &r->u.ss.start->where);
14460 break;
14463 r = r->next;
14469 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14471 static void
14472 resolve_fntype (gfc_namespace *ns)
14474 gfc_entry_list *el;
14475 gfc_symbol *sym;
14477 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14478 return;
14480 /* If there are any entries, ns->proc_name is the entry master
14481 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14482 if (ns->entries)
14483 sym = ns->entries->sym;
14484 else
14485 sym = ns->proc_name;
14486 if (sym->result == sym
14487 && sym->ts.type == BT_UNKNOWN
14488 && !gfc_set_default_type (sym, 0, NULL)
14489 && !sym->attr.untyped)
14491 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14492 sym->name, &sym->declared_at);
14493 sym->attr.untyped = 1;
14496 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14497 && !sym->attr.contained
14498 && !gfc_check_symbol_access (sym->ts.u.derived)
14499 && gfc_check_symbol_access (sym))
14501 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14502 "%L of PRIVATE type '%s'", sym->name,
14503 &sym->declared_at, sym->ts.u.derived->name);
14506 if (ns->entries)
14507 for (el = ns->entries->next; el; el = el->next)
14509 if (el->sym->result == el->sym
14510 && el->sym->ts.type == BT_UNKNOWN
14511 && !gfc_set_default_type (el->sym, 0, NULL)
14512 && !el->sym->attr.untyped)
14514 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14515 el->sym->name, &el->sym->declared_at);
14516 el->sym->attr.untyped = 1;
14522 /* 12.3.2.1.1 Defined operators. */
14524 static bool
14525 check_uop_procedure (gfc_symbol *sym, locus where)
14527 gfc_formal_arglist *formal;
14529 if (!sym->attr.function)
14531 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14532 sym->name, &where);
14533 return false;
14536 if (sym->ts.type == BT_CHARACTER
14537 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14538 && !(sym->result && sym->result->ts.u.cl
14539 && sym->result->ts.u.cl->length))
14541 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14542 "character length", sym->name, &where);
14543 return false;
14546 formal = gfc_sym_get_dummy_args (sym);
14547 if (!formal || !formal->sym)
14549 gfc_error ("User operator procedure '%s' at %L must have at least "
14550 "one argument", sym->name, &where);
14551 return false;
14554 if (formal->sym->attr.intent != INTENT_IN)
14556 gfc_error ("First argument of operator interface at %L must be "
14557 "INTENT(IN)", &where);
14558 return false;
14561 if (formal->sym->attr.optional)
14563 gfc_error ("First argument of operator interface at %L cannot be "
14564 "optional", &where);
14565 return false;
14568 formal = formal->next;
14569 if (!formal || !formal->sym)
14570 return true;
14572 if (formal->sym->attr.intent != INTENT_IN)
14574 gfc_error ("Second argument of operator interface at %L must be "
14575 "INTENT(IN)", &where);
14576 return false;
14579 if (formal->sym->attr.optional)
14581 gfc_error ("Second argument of operator interface at %L cannot be "
14582 "optional", &where);
14583 return false;
14586 if (formal->next)
14588 gfc_error ("Operator interface at %L must have, at most, two "
14589 "arguments", &where);
14590 return false;
14593 return true;
14596 static void
14597 gfc_resolve_uops (gfc_symtree *symtree)
14599 gfc_interface *itr;
14601 if (symtree == NULL)
14602 return;
14604 gfc_resolve_uops (symtree->left);
14605 gfc_resolve_uops (symtree->right);
14607 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14608 check_uop_procedure (itr->sym, itr->sym->declared_at);
14612 /* Examine all of the expressions associated with a program unit,
14613 assign types to all intermediate expressions, make sure that all
14614 assignments are to compatible types and figure out which names
14615 refer to which functions or subroutines. It doesn't check code
14616 block, which is handled by resolve_code. */
14618 static void
14619 resolve_types (gfc_namespace *ns)
14621 gfc_namespace *n;
14622 gfc_charlen *cl;
14623 gfc_data *d;
14624 gfc_equiv *eq;
14625 gfc_namespace* old_ns = gfc_current_ns;
14627 /* Check that all IMPLICIT types are ok. */
14628 if (!ns->seen_implicit_none)
14630 unsigned letter;
14631 for (letter = 0; letter != GFC_LETTERS; ++letter)
14632 if (ns->set_flag[letter]
14633 && !resolve_typespec_used (&ns->default_type[letter],
14634 &ns->implicit_loc[letter], NULL))
14635 return;
14638 gfc_current_ns = ns;
14640 resolve_entries (ns);
14642 resolve_common_vars (ns->blank_common.head, false);
14643 resolve_common_blocks (ns->common_root);
14645 resolve_contained_functions (ns);
14647 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14648 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14649 resolve_formal_arglist (ns->proc_name);
14651 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14653 for (cl = ns->cl_list; cl; cl = cl->next)
14654 resolve_charlen (cl);
14656 gfc_traverse_ns (ns, resolve_symbol);
14658 resolve_fntype (ns);
14660 for (n = ns->contained; n; n = n->sibling)
14662 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14663 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14664 "also be PURE", n->proc_name->name,
14665 &n->proc_name->declared_at);
14667 resolve_types (n);
14670 forall_flag = 0;
14671 gfc_do_concurrent_flag = 0;
14672 gfc_check_interfaces (ns);
14674 gfc_traverse_ns (ns, resolve_values);
14676 if (ns->save_all)
14677 gfc_save_all (ns);
14679 iter_stack = NULL;
14680 for (d = ns->data; d; d = d->next)
14681 resolve_data (d);
14683 iter_stack = NULL;
14684 gfc_traverse_ns (ns, gfc_formalize_init_value);
14686 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14688 for (eq = ns->equiv; eq; eq = eq->next)
14689 resolve_equivalence (eq);
14691 /* Warn about unused labels. */
14692 if (warn_unused_label)
14693 warn_unused_fortran_label (ns->st_labels);
14695 gfc_resolve_uops (ns->uop_root);
14697 gfc_resolve_omp_declare_simd (ns);
14699 gfc_current_ns = old_ns;
14703 /* Call resolve_code recursively. */
14705 static void
14706 resolve_codes (gfc_namespace *ns)
14708 gfc_namespace *n;
14709 bitmap_obstack old_obstack;
14711 if (ns->resolved == 1)
14712 return;
14714 for (n = ns->contained; n; n = n->sibling)
14715 resolve_codes (n);
14717 gfc_current_ns = ns;
14719 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14720 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14721 cs_base = NULL;
14723 /* Set to an out of range value. */
14724 current_entry_id = -1;
14726 old_obstack = labels_obstack;
14727 bitmap_obstack_initialize (&labels_obstack);
14729 resolve_code (ns->code, ns);
14731 bitmap_obstack_release (&labels_obstack);
14732 labels_obstack = old_obstack;
14736 /* This function is called after a complete program unit has been compiled.
14737 Its purpose is to examine all of the expressions associated with a program
14738 unit, assign types to all intermediate expressions, make sure that all
14739 assignments are to compatible types and figure out which names refer to
14740 which functions or subroutines. */
14742 void
14743 gfc_resolve (gfc_namespace *ns)
14745 gfc_namespace *old_ns;
14746 code_stack *old_cs_base;
14748 if (ns->resolved)
14749 return;
14751 ns->resolved = -1;
14752 old_ns = gfc_current_ns;
14753 old_cs_base = cs_base;
14755 resolve_types (ns);
14756 component_assignment_level = 0;
14757 resolve_codes (ns);
14759 gfc_current_ns = old_ns;
14760 cs_base = old_cs_base;
14761 ns->resolved = 1;
14763 gfc_run_passes (ns);