2014-06-10 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / resolve.c
blobe671d0be39d3bd4ef07e5fd2c449b74f1933deee
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 (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 (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
10870 && !(sym->attr.pointer
10871 || sym->attr.allocatable
10872 || sym->attr.omp_udr_artificial_var))
10874 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10875 "requires either the pointer or allocatable attribute",
10876 sym->name, &sym->declared_at);
10877 specification_expr = saved_specification_expr;
10878 return false;
10881 if (sym->ts.type == BT_CHARACTER)
10883 /* Make sure that character string variables with assumed length are
10884 dummy arguments. */
10885 e = sym->ts.u.cl->length;
10886 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10887 && !sym->ts.deferred && !sym->attr.select_type_temporary
10888 && !sym->attr.omp_udr_artificial_var)
10890 gfc_error ("Entity with assumed character length at %L must be a "
10891 "dummy argument or a PARAMETER", &sym->declared_at);
10892 specification_expr = saved_specification_expr;
10893 return false;
10896 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10898 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10899 specification_expr = saved_specification_expr;
10900 return false;
10903 if (!gfc_is_constant_expr (e)
10904 && !(e->expr_type == EXPR_VARIABLE
10905 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10907 if (!sym->attr.use_assoc && sym->ns->proc_name
10908 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10909 || sym->ns->proc_name->attr.is_main_program))
10911 gfc_error ("'%s' at %L must have constant character length "
10912 "in this context", sym->name, &sym->declared_at);
10913 specification_expr = saved_specification_expr;
10914 return false;
10916 if (sym->attr.in_common)
10918 gfc_error ("COMMON variable '%s' at %L must have constant "
10919 "character length", sym->name, &sym->declared_at);
10920 specification_expr = saved_specification_expr;
10921 return false;
10926 if (sym->value == NULL && sym->attr.referenced)
10927 apply_default_init_local (sym); /* Try to apply a default initialization. */
10929 /* Determine if the symbol may not have an initializer. */
10930 no_init_flag = automatic_flag = 0;
10931 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10932 || sym->attr.intrinsic || sym->attr.result)
10933 no_init_flag = 1;
10934 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10935 && is_non_constant_shape_array (sym))
10937 no_init_flag = automatic_flag = 1;
10939 /* Also, they must not have the SAVE attribute.
10940 SAVE_IMPLICIT is checked below. */
10941 if (sym->as && sym->attr.codimension)
10943 int corank = sym->as->corank;
10944 sym->as->corank = 0;
10945 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10946 sym->as->corank = corank;
10948 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10950 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10951 specification_expr = saved_specification_expr;
10952 return false;
10956 /* Ensure that any initializer is simplified. */
10957 if (sym->value)
10958 gfc_simplify_expr (sym->value, 1);
10960 /* Reject illegal initializers. */
10961 if (!sym->mark && sym->value)
10963 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10964 && CLASS_DATA (sym)->attr.allocatable))
10965 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10966 sym->name, &sym->declared_at);
10967 else if (sym->attr.external)
10968 gfc_error ("External '%s' at %L cannot have an initializer",
10969 sym->name, &sym->declared_at);
10970 else if (sym->attr.dummy
10971 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10972 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10973 sym->name, &sym->declared_at);
10974 else if (sym->attr.intrinsic)
10975 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10976 sym->name, &sym->declared_at);
10977 else if (sym->attr.result)
10978 gfc_error ("Function result '%s' at %L cannot have an initializer",
10979 sym->name, &sym->declared_at);
10980 else if (automatic_flag)
10981 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10982 sym->name, &sym->declared_at);
10983 else
10984 goto no_init_error;
10985 specification_expr = saved_specification_expr;
10986 return false;
10989 no_init_error:
10990 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10992 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10993 specification_expr = saved_specification_expr;
10994 return res;
10997 specification_expr = saved_specification_expr;
10998 return true;
11002 /* Resolve a procedure. */
11004 static bool
11005 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11007 gfc_formal_arglist *arg;
11009 if (sym->attr.function
11010 && !resolve_fl_var_and_proc (sym, mp_flag))
11011 return false;
11013 if (sym->ts.type == BT_CHARACTER)
11015 gfc_charlen *cl = sym->ts.u.cl;
11017 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11018 && !resolve_charlen (cl))
11019 return false;
11021 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11022 && sym->attr.proc == PROC_ST_FUNCTION)
11024 gfc_error ("Character-valued statement function '%s' at %L must "
11025 "have constant length", sym->name, &sym->declared_at);
11026 return false;
11030 /* Ensure that derived type for are not of a private type. Internal
11031 module procedures are excluded by 2.2.3.3 - i.e., they are not
11032 externally accessible and can access all the objects accessible in
11033 the host. */
11034 if (!(sym->ns->parent
11035 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11036 && gfc_check_symbol_access (sym))
11038 gfc_interface *iface;
11040 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11042 if (arg->sym
11043 && arg->sym->ts.type == BT_DERIVED
11044 && !arg->sym->ts.u.derived->attr.use_assoc
11045 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11046 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
11047 "and cannot be a dummy argument"
11048 " of '%s', which is PUBLIC at %L",
11049 arg->sym->name, sym->name,
11050 &sym->declared_at))
11052 /* Stop this message from recurring. */
11053 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11054 return false;
11058 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11059 PRIVATE to the containing module. */
11060 for (iface = sym->generic; iface; iface = iface->next)
11062 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11064 if (arg->sym
11065 && arg->sym->ts.type == BT_DERIVED
11066 && !arg->sym->ts.u.derived->attr.use_assoc
11067 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11068 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11069 "PUBLIC interface '%s' at %L "
11070 "takes dummy arguments of '%s' which "
11071 "is PRIVATE", iface->sym->name,
11072 sym->name, &iface->sym->declared_at,
11073 gfc_typename(&arg->sym->ts)))
11075 /* Stop this message from recurring. */
11076 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11077 return false;
11082 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11083 PRIVATE to the containing module. */
11084 for (iface = sym->generic; iface; iface = iface->next)
11086 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11088 if (arg->sym
11089 && arg->sym->ts.type == BT_DERIVED
11090 && !arg->sym->ts.u.derived->attr.use_assoc
11091 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11092 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11093 "PUBLIC interface '%s' at %L takes "
11094 "dummy arguments of '%s' which is "
11095 "PRIVATE", iface->sym->name,
11096 sym->name, &iface->sym->declared_at,
11097 gfc_typename(&arg->sym->ts)))
11099 /* Stop this message from recurring. */
11100 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11101 return false;
11107 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11108 && !sym->attr.proc_pointer)
11110 gfc_error ("Function '%s' at %L cannot have an initializer",
11111 sym->name, &sym->declared_at);
11112 return false;
11115 /* An external symbol may not have an initializer because it is taken to be
11116 a procedure. Exception: Procedure Pointers. */
11117 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11119 gfc_error ("External object '%s' at %L may not have an initializer",
11120 sym->name, &sym->declared_at);
11121 return false;
11124 /* An elemental function is required to return a scalar 12.7.1 */
11125 if (sym->attr.elemental && sym->attr.function && sym->as)
11127 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11128 "result", sym->name, &sym->declared_at);
11129 /* Reset so that the error only occurs once. */
11130 sym->attr.elemental = 0;
11131 return false;
11134 if (sym->attr.proc == PROC_ST_FUNCTION
11135 && (sym->attr.allocatable || sym->attr.pointer))
11137 gfc_error ("Statement function '%s' at %L may not have pointer or "
11138 "allocatable attribute", sym->name, &sym->declared_at);
11139 return false;
11142 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11143 char-len-param shall not be array-valued, pointer-valued, recursive
11144 or pure. ....snip... A character value of * may only be used in the
11145 following ways: (i) Dummy arg of procedure - dummy associates with
11146 actual length; (ii) To declare a named constant; or (iii) External
11147 function - but length must be declared in calling scoping unit. */
11148 if (sym->attr.function
11149 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11150 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11152 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11153 || (sym->attr.recursive) || (sym->attr.pure))
11155 if (sym->as && sym->as->rank)
11156 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11157 "array-valued", sym->name, &sym->declared_at);
11159 if (sym->attr.pointer)
11160 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11161 "pointer-valued", sym->name, &sym->declared_at);
11163 if (sym->attr.pure)
11164 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11165 "pure", sym->name, &sym->declared_at);
11167 if (sym->attr.recursive)
11168 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11169 "recursive", sym->name, &sym->declared_at);
11171 return false;
11174 /* Appendix B.2 of the standard. Contained functions give an
11175 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11176 character length is an F2003 feature. */
11177 if (!sym->attr.contained
11178 && gfc_current_form != FORM_FIXED
11179 && !sym->ts.deferred)
11180 gfc_notify_std (GFC_STD_F95_OBS,
11181 "CHARACTER(*) function '%s' at %L",
11182 sym->name, &sym->declared_at);
11185 /* F2008, C1218. */
11186 if (sym->attr.elemental)
11188 if (sym->attr.proc_pointer)
11190 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11191 sym->name, &sym->declared_at);
11192 return false;
11194 if (sym->attr.dummy)
11196 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11197 sym->name, &sym->declared_at);
11198 return false;
11202 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11204 gfc_formal_arglist *curr_arg;
11205 int has_non_interop_arg = 0;
11207 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11208 sym->common_block))
11210 /* Clear these to prevent looking at them again if there was an
11211 error. */
11212 sym->attr.is_bind_c = 0;
11213 sym->attr.is_c_interop = 0;
11214 sym->ts.is_c_interop = 0;
11216 else
11218 /* So far, no errors have been found. */
11219 sym->attr.is_c_interop = 1;
11220 sym->ts.is_c_interop = 1;
11223 curr_arg = gfc_sym_get_dummy_args (sym);
11224 while (curr_arg != NULL)
11226 /* Skip implicitly typed dummy args here. */
11227 if (curr_arg->sym->attr.implicit_type == 0)
11228 if (!gfc_verify_c_interop_param (curr_arg->sym))
11229 /* If something is found to fail, record the fact so we
11230 can mark the symbol for the procedure as not being
11231 BIND(C) to try and prevent multiple errors being
11232 reported. */
11233 has_non_interop_arg = 1;
11235 curr_arg = curr_arg->next;
11238 /* See if any of the arguments were not interoperable and if so, clear
11239 the procedure symbol to prevent duplicate error messages. */
11240 if (has_non_interop_arg != 0)
11242 sym->attr.is_c_interop = 0;
11243 sym->ts.is_c_interop = 0;
11244 sym->attr.is_bind_c = 0;
11248 if (!sym->attr.proc_pointer)
11250 if (sym->attr.save == SAVE_EXPLICIT)
11252 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11253 "in '%s' at %L", sym->name, &sym->declared_at);
11254 return false;
11256 if (sym->attr.intent)
11258 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11259 "in '%s' at %L", sym->name, &sym->declared_at);
11260 return false;
11262 if (sym->attr.subroutine && sym->attr.result)
11264 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11265 "in '%s' at %L", sym->name, &sym->declared_at);
11266 return false;
11268 if (sym->attr.external && sym->attr.function
11269 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11270 || sym->attr.contained))
11272 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11273 "in '%s' at %L", sym->name, &sym->declared_at);
11274 return false;
11276 if (strcmp ("ppr@", sym->name) == 0)
11278 gfc_error ("Procedure pointer result '%s' at %L "
11279 "is missing the pointer attribute",
11280 sym->ns->proc_name->name, &sym->declared_at);
11281 return false;
11285 return true;
11289 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11290 been defined and we now know their defined arguments, check that they fulfill
11291 the requirements of the standard for procedures used as finalizers. */
11293 static bool
11294 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11296 gfc_finalizer* list;
11297 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11298 bool result = true;
11299 bool seen_scalar = false;
11300 gfc_symbol *vtab;
11301 gfc_component *c;
11303 /* Return early when not finalizable. Additionally, ensure that derived-type
11304 components have a their finalizables resolved. */
11305 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11307 bool has_final = false;
11308 for (c = derived->components; c; c = c->next)
11309 if (c->ts.type == BT_DERIVED
11310 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11312 bool has_final2 = false;
11313 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11314 return false; /* Error. */
11315 has_final = has_final || has_final2;
11317 if (!has_final)
11319 if (finalizable)
11320 *finalizable = false;
11321 return true;
11325 /* Walk over the list of finalizer-procedures, check them, and if any one
11326 does not fit in with the standard's definition, print an error and remove
11327 it from the list. */
11328 prev_link = &derived->f2k_derived->finalizers;
11329 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11331 gfc_formal_arglist *dummy_args;
11332 gfc_symbol* arg;
11333 gfc_finalizer* i;
11334 int my_rank;
11336 /* Skip this finalizer if we already resolved it. */
11337 if (list->proc_tree)
11339 prev_link = &(list->next);
11340 continue;
11343 /* Check this exists and is a SUBROUTINE. */
11344 if (!list->proc_sym->attr.subroutine)
11346 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11347 list->proc_sym->name, &list->where);
11348 goto error;
11351 /* We should have exactly one argument. */
11352 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11353 if (!dummy_args || dummy_args->next)
11355 gfc_error ("FINAL procedure at %L must have exactly one argument",
11356 &list->where);
11357 goto error;
11359 arg = dummy_args->sym;
11361 /* This argument must be of our type. */
11362 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11364 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11365 &arg->declared_at, derived->name);
11366 goto error;
11369 /* It must neither be a pointer nor allocatable nor optional. */
11370 if (arg->attr.pointer)
11372 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11373 &arg->declared_at);
11374 goto error;
11376 if (arg->attr.allocatable)
11378 gfc_error ("Argument of FINAL procedure at %L must not be"
11379 " ALLOCATABLE", &arg->declared_at);
11380 goto error;
11382 if (arg->attr.optional)
11384 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11385 &arg->declared_at);
11386 goto error;
11389 /* It must not be INTENT(OUT). */
11390 if (arg->attr.intent == INTENT_OUT)
11392 gfc_error ("Argument of FINAL procedure at %L must not be"
11393 " INTENT(OUT)", &arg->declared_at);
11394 goto error;
11397 /* Warn if the procedure is non-scalar and not assumed shape. */
11398 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11399 && arg->as->type != AS_ASSUMED_SHAPE)
11400 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11401 " shape argument", &arg->declared_at);
11403 /* Check that it does not match in kind and rank with a FINAL procedure
11404 defined earlier. To really loop over the *earlier* declarations,
11405 we need to walk the tail of the list as new ones were pushed at the
11406 front. */
11407 /* TODO: Handle kind parameters once they are implemented. */
11408 my_rank = (arg->as ? arg->as->rank : 0);
11409 for (i = list->next; i; i = i->next)
11411 gfc_formal_arglist *dummy_args;
11413 /* Argument list might be empty; that is an error signalled earlier,
11414 but we nevertheless continued resolving. */
11415 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11416 if (dummy_args)
11418 gfc_symbol* i_arg = dummy_args->sym;
11419 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11420 if (i_rank == my_rank)
11422 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11423 " rank (%d) as '%s'",
11424 list->proc_sym->name, &list->where, my_rank,
11425 i->proc_sym->name);
11426 goto error;
11431 /* Is this the/a scalar finalizer procedure? */
11432 if (!arg->as || arg->as->rank == 0)
11433 seen_scalar = true;
11435 /* Find the symtree for this procedure. */
11436 gcc_assert (!list->proc_tree);
11437 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11439 prev_link = &list->next;
11440 continue;
11442 /* Remove wrong nodes immediately from the list so we don't risk any
11443 troubles in the future when they might fail later expectations. */
11444 error:
11445 i = list;
11446 *prev_link = list->next;
11447 gfc_free_finalizer (i);
11448 result = false;
11451 if (result == false)
11452 return false;
11454 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11455 were nodes in the list, must have been for arrays. It is surely a good
11456 idea to have a scalar version there if there's something to finalize. */
11457 if (gfc_option.warn_surprising && result && !seen_scalar)
11458 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11459 " defined at %L, suggest also scalar one",
11460 derived->name, &derived->declared_at);
11462 vtab = gfc_find_derived_vtab (derived);
11463 c = vtab->ts.u.derived->components->next->next->next->next->next;
11464 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
11466 if (finalizable)
11467 *finalizable = true;
11469 return true;
11473 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11475 static bool
11476 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11477 const char* generic_name, locus where)
11479 gfc_symbol *sym1, *sym2;
11480 const char *pass1, *pass2;
11481 gfc_formal_arglist *dummy_args;
11483 gcc_assert (t1->specific && t2->specific);
11484 gcc_assert (!t1->specific->is_generic);
11485 gcc_assert (!t2->specific->is_generic);
11486 gcc_assert (t1->is_operator == t2->is_operator);
11488 sym1 = t1->specific->u.specific->n.sym;
11489 sym2 = t2->specific->u.specific->n.sym;
11491 if (sym1 == sym2)
11492 return true;
11494 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11495 if (sym1->attr.subroutine != sym2->attr.subroutine
11496 || sym1->attr.function != sym2->attr.function)
11498 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11499 " GENERIC '%s' at %L",
11500 sym1->name, sym2->name, generic_name, &where);
11501 return false;
11504 /* Determine PASS arguments. */
11505 if (t1->specific->nopass)
11506 pass1 = NULL;
11507 else if (t1->specific->pass_arg)
11508 pass1 = t1->specific->pass_arg;
11509 else
11511 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
11512 if (dummy_args)
11513 pass1 = dummy_args->sym->name;
11514 else
11515 pass1 = NULL;
11517 if (t2->specific->nopass)
11518 pass2 = NULL;
11519 else if (t2->specific->pass_arg)
11520 pass2 = t2->specific->pass_arg;
11521 else
11523 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
11524 if (dummy_args)
11525 pass2 = dummy_args->sym->name;
11526 else
11527 pass2 = NULL;
11530 /* Compare the interfaces. */
11531 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11532 NULL, 0, pass1, pass2))
11534 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11535 sym1->name, sym2->name, generic_name, &where);
11536 return false;
11539 return true;
11543 /* Worker function for resolving a generic procedure binding; this is used to
11544 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11546 The difference between those cases is finding possible inherited bindings
11547 that are overridden, as one has to look for them in tb_sym_root,
11548 tb_uop_root or tb_op, respectively. Thus the caller must already find
11549 the super-type and set p->overridden correctly. */
11551 static bool
11552 resolve_tb_generic_targets (gfc_symbol* super_type,
11553 gfc_typebound_proc* p, const char* name)
11555 gfc_tbp_generic* target;
11556 gfc_symtree* first_target;
11557 gfc_symtree* inherited;
11559 gcc_assert (p && p->is_generic);
11561 /* Try to find the specific bindings for the symtrees in our target-list. */
11562 gcc_assert (p->u.generic);
11563 for (target = p->u.generic; target; target = target->next)
11564 if (!target->specific)
11566 gfc_typebound_proc* overridden_tbp;
11567 gfc_tbp_generic* g;
11568 const char* target_name;
11570 target_name = target->specific_st->name;
11572 /* Defined for this type directly. */
11573 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11575 target->specific = target->specific_st->n.tb;
11576 goto specific_found;
11579 /* Look for an inherited specific binding. */
11580 if (super_type)
11582 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11583 true, NULL);
11585 if (inherited)
11587 gcc_assert (inherited->n.tb);
11588 target->specific = inherited->n.tb;
11589 goto specific_found;
11593 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11594 " at %L", target_name, name, &p->where);
11595 return false;
11597 /* Once we've found the specific binding, check it is not ambiguous with
11598 other specifics already found or inherited for the same GENERIC. */
11599 specific_found:
11600 gcc_assert (target->specific);
11602 /* This must really be a specific binding! */
11603 if (target->specific->is_generic)
11605 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11606 " '%s' is GENERIC, too", name, &p->where, target_name);
11607 return false;
11610 /* Check those already resolved on this type directly. */
11611 for (g = p->u.generic; g; g = g->next)
11612 if (g != target && g->specific
11613 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11614 return false;
11616 /* Check for ambiguity with inherited specific targets. */
11617 for (overridden_tbp = p->overridden; overridden_tbp;
11618 overridden_tbp = overridden_tbp->overridden)
11619 if (overridden_tbp->is_generic)
11621 for (g = overridden_tbp->u.generic; g; g = g->next)
11623 gcc_assert (g->specific);
11624 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11625 return false;
11630 /* If we attempt to "overwrite" a specific binding, this is an error. */
11631 if (p->overridden && !p->overridden->is_generic)
11633 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11634 " the same name", name, &p->where);
11635 return false;
11638 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11639 all must have the same attributes here. */
11640 first_target = p->u.generic->specific->u.specific;
11641 gcc_assert (first_target);
11642 p->subroutine = first_target->n.sym->attr.subroutine;
11643 p->function = first_target->n.sym->attr.function;
11645 return true;
11649 /* Resolve a GENERIC procedure binding for a derived type. */
11651 static bool
11652 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11654 gfc_symbol* super_type;
11656 /* Find the overridden binding if any. */
11657 st->n.tb->overridden = NULL;
11658 super_type = gfc_get_derived_super_type (derived);
11659 if (super_type)
11661 gfc_symtree* overridden;
11662 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11663 true, NULL);
11665 if (overridden && overridden->n.tb)
11666 st->n.tb->overridden = overridden->n.tb;
11669 /* Resolve using worker function. */
11670 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11674 /* Retrieve the target-procedure of an operator binding and do some checks in
11675 common for intrinsic and user-defined type-bound operators. */
11677 static gfc_symbol*
11678 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11680 gfc_symbol* target_proc;
11682 gcc_assert (target->specific && !target->specific->is_generic);
11683 target_proc = target->specific->u.specific->n.sym;
11684 gcc_assert (target_proc);
11686 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11687 if (target->specific->nopass)
11689 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11690 return NULL;
11693 return target_proc;
11697 /* Resolve a type-bound intrinsic operator. */
11699 static bool
11700 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11701 gfc_typebound_proc* p)
11703 gfc_symbol* super_type;
11704 gfc_tbp_generic* target;
11706 /* If there's already an error here, do nothing (but don't fail again). */
11707 if (p->error)
11708 return true;
11710 /* Operators should always be GENERIC bindings. */
11711 gcc_assert (p->is_generic);
11713 /* Look for an overridden binding. */
11714 super_type = gfc_get_derived_super_type (derived);
11715 if (super_type && super_type->f2k_derived)
11716 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11717 op, true, NULL);
11718 else
11719 p->overridden = NULL;
11721 /* Resolve general GENERIC properties using worker function. */
11722 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11723 goto error;
11725 /* Check the targets to be procedures of correct interface. */
11726 for (target = p->u.generic; target; target = target->next)
11728 gfc_symbol* target_proc;
11730 target_proc = get_checked_tb_operator_target (target, p->where);
11731 if (!target_proc)
11732 goto error;
11734 if (!gfc_check_operator_interface (target_proc, op, p->where))
11735 goto error;
11737 /* Add target to non-typebound operator list. */
11738 if (!target->specific->deferred && !derived->attr.use_assoc
11739 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11741 gfc_interface *head, *intr;
11742 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11743 return false;
11744 head = derived->ns->op[op];
11745 intr = gfc_get_interface ();
11746 intr->sym = target_proc;
11747 intr->where = p->where;
11748 intr->next = head;
11749 derived->ns->op[op] = intr;
11753 return true;
11755 error:
11756 p->error = 1;
11757 return false;
11761 /* Resolve a type-bound user operator (tree-walker callback). */
11763 static gfc_symbol* resolve_bindings_derived;
11764 static bool resolve_bindings_result;
11766 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11768 static void
11769 resolve_typebound_user_op (gfc_symtree* stree)
11771 gfc_symbol* super_type;
11772 gfc_tbp_generic* target;
11774 gcc_assert (stree && stree->n.tb);
11776 if (stree->n.tb->error)
11777 return;
11779 /* Operators should always be GENERIC bindings. */
11780 gcc_assert (stree->n.tb->is_generic);
11782 /* Find overridden procedure, if any. */
11783 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11784 if (super_type && super_type->f2k_derived)
11786 gfc_symtree* overridden;
11787 overridden = gfc_find_typebound_user_op (super_type, NULL,
11788 stree->name, true, NULL);
11790 if (overridden && overridden->n.tb)
11791 stree->n.tb->overridden = overridden->n.tb;
11793 else
11794 stree->n.tb->overridden = NULL;
11796 /* Resolve basically using worker function. */
11797 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11798 goto error;
11800 /* Check the targets to be functions of correct interface. */
11801 for (target = stree->n.tb->u.generic; target; target = target->next)
11803 gfc_symbol* target_proc;
11805 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11806 if (!target_proc)
11807 goto error;
11809 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11810 goto error;
11813 return;
11815 error:
11816 resolve_bindings_result = false;
11817 stree->n.tb->error = 1;
11821 /* Resolve the type-bound procedures for a derived type. */
11823 static void
11824 resolve_typebound_procedure (gfc_symtree* stree)
11826 gfc_symbol* proc;
11827 locus where;
11828 gfc_symbol* me_arg;
11829 gfc_symbol* super_type;
11830 gfc_component* comp;
11832 gcc_assert (stree);
11834 /* Undefined specific symbol from GENERIC target definition. */
11835 if (!stree->n.tb)
11836 return;
11838 if (stree->n.tb->error)
11839 return;
11841 /* If this is a GENERIC binding, use that routine. */
11842 if (stree->n.tb->is_generic)
11844 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11845 goto error;
11846 return;
11849 /* Get the target-procedure to check it. */
11850 gcc_assert (!stree->n.tb->is_generic);
11851 gcc_assert (stree->n.tb->u.specific);
11852 proc = stree->n.tb->u.specific->n.sym;
11853 where = stree->n.tb->where;
11855 /* Default access should already be resolved from the parser. */
11856 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11858 if (stree->n.tb->deferred)
11860 if (!check_proc_interface (proc, &where))
11861 goto error;
11863 else
11865 /* Check for F08:C465. */
11866 if ((!proc->attr.subroutine && !proc->attr.function)
11867 || (proc->attr.proc != PROC_MODULE
11868 && proc->attr.if_source != IFSRC_IFBODY)
11869 || proc->attr.abstract)
11871 gfc_error ("'%s' must be a module procedure or an external procedure with"
11872 " an explicit interface at %L", proc->name, &where);
11873 goto error;
11877 stree->n.tb->subroutine = proc->attr.subroutine;
11878 stree->n.tb->function = proc->attr.function;
11880 /* Find the super-type of the current derived type. We could do this once and
11881 store in a global if speed is needed, but as long as not I believe this is
11882 more readable and clearer. */
11883 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11885 /* If PASS, resolve and check arguments if not already resolved / loaded
11886 from a .mod file. */
11887 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11889 gfc_formal_arglist *dummy_args;
11891 dummy_args = gfc_sym_get_dummy_args (proc);
11892 if (stree->n.tb->pass_arg)
11894 gfc_formal_arglist *i;
11896 /* If an explicit passing argument name is given, walk the arg-list
11897 and look for it. */
11899 me_arg = NULL;
11900 stree->n.tb->pass_arg_num = 1;
11901 for (i = dummy_args; i; i = i->next)
11903 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11905 me_arg = i->sym;
11906 break;
11908 ++stree->n.tb->pass_arg_num;
11911 if (!me_arg)
11913 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11914 " argument '%s'",
11915 proc->name, stree->n.tb->pass_arg, &where,
11916 stree->n.tb->pass_arg);
11917 goto error;
11920 else
11922 /* Otherwise, take the first one; there should in fact be at least
11923 one. */
11924 stree->n.tb->pass_arg_num = 1;
11925 if (!dummy_args)
11927 gfc_error ("Procedure '%s' with PASS at %L must have at"
11928 " least one argument", proc->name, &where);
11929 goto error;
11931 me_arg = dummy_args->sym;
11934 /* Now check that the argument-type matches and the passed-object
11935 dummy argument is generally fine. */
11937 gcc_assert (me_arg);
11939 if (me_arg->ts.type != BT_CLASS)
11941 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11942 " at %L", proc->name, &where);
11943 goto error;
11946 if (CLASS_DATA (me_arg)->ts.u.derived
11947 != resolve_bindings_derived)
11949 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11950 " the derived-type '%s'", me_arg->name, proc->name,
11951 me_arg->name, &where, resolve_bindings_derived->name);
11952 goto error;
11955 gcc_assert (me_arg->ts.type == BT_CLASS);
11956 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11958 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11959 " scalar", proc->name, &where);
11960 goto error;
11962 if (CLASS_DATA (me_arg)->attr.allocatable)
11964 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11965 " be ALLOCATABLE", proc->name, &where);
11966 goto error;
11968 if (CLASS_DATA (me_arg)->attr.class_pointer)
11970 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11971 " be POINTER", proc->name, &where);
11972 goto error;
11976 /* If we are extending some type, check that we don't override a procedure
11977 flagged NON_OVERRIDABLE. */
11978 stree->n.tb->overridden = NULL;
11979 if (super_type)
11981 gfc_symtree* overridden;
11982 overridden = gfc_find_typebound_proc (super_type, NULL,
11983 stree->name, true, NULL);
11985 if (overridden)
11987 if (overridden->n.tb)
11988 stree->n.tb->overridden = overridden->n.tb;
11990 if (!gfc_check_typebound_override (stree, overridden))
11991 goto error;
11995 /* See if there's a name collision with a component directly in this type. */
11996 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11997 if (!strcmp (comp->name, stree->name))
11999 gfc_error ("Procedure '%s' at %L has the same name as a component of"
12000 " '%s'",
12001 stree->name, &where, resolve_bindings_derived->name);
12002 goto error;
12005 /* Try to find a name collision with an inherited component. */
12006 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12008 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
12009 " component of '%s'",
12010 stree->name, &where, resolve_bindings_derived->name);
12011 goto error;
12014 stree->n.tb->error = 0;
12015 return;
12017 error:
12018 resolve_bindings_result = false;
12019 stree->n.tb->error = 1;
12023 static bool
12024 resolve_typebound_procedures (gfc_symbol* derived)
12026 int op;
12027 gfc_symbol* super_type;
12029 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12030 return true;
12032 super_type = gfc_get_derived_super_type (derived);
12033 if (super_type)
12034 resolve_symbol (super_type);
12036 resolve_bindings_derived = derived;
12037 resolve_bindings_result = true;
12039 if (derived->f2k_derived->tb_sym_root)
12040 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12041 &resolve_typebound_procedure);
12043 if (derived->f2k_derived->tb_uop_root)
12044 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12045 &resolve_typebound_user_op);
12047 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12049 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12050 if (p && !resolve_typebound_intrinsic_op (derived,
12051 (gfc_intrinsic_op)op, p))
12052 resolve_bindings_result = false;
12055 return resolve_bindings_result;
12059 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12060 to give all identical derived types the same backend_decl. */
12061 static void
12062 add_dt_to_dt_list (gfc_symbol *derived)
12064 gfc_dt_list *dt_list;
12066 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12067 if (derived == dt_list->derived)
12068 return;
12070 dt_list = gfc_get_dt_list ();
12071 dt_list->next = gfc_derived_types;
12072 dt_list->derived = derived;
12073 gfc_derived_types = dt_list;
12077 /* Ensure that a derived-type is really not abstract, meaning that every
12078 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12080 static bool
12081 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12083 if (!st)
12084 return true;
12086 if (!ensure_not_abstract_walker (sub, st->left))
12087 return false;
12088 if (!ensure_not_abstract_walker (sub, st->right))
12089 return false;
12091 if (st->n.tb && st->n.tb->deferred)
12093 gfc_symtree* overriding;
12094 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12095 if (!overriding)
12096 return false;
12097 gcc_assert (overriding->n.tb);
12098 if (overriding->n.tb->deferred)
12100 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
12101 " '%s' is DEFERRED and not overridden",
12102 sub->name, &sub->declared_at, st->name);
12103 return false;
12107 return true;
12110 static bool
12111 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12113 /* The algorithm used here is to recursively travel up the ancestry of sub
12114 and for each ancestor-type, check all bindings. If any of them is
12115 DEFERRED, look it up starting from sub and see if the found (overriding)
12116 binding is not DEFERRED.
12117 This is not the most efficient way to do this, but it should be ok and is
12118 clearer than something sophisticated. */
12120 gcc_assert (ancestor && !sub->attr.abstract);
12122 if (!ancestor->attr.abstract)
12123 return true;
12125 /* Walk bindings of this ancestor. */
12126 if (ancestor->f2k_derived)
12128 bool t;
12129 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12130 if (!t)
12131 return false;
12134 /* Find next ancestor type and recurse on it. */
12135 ancestor = gfc_get_derived_super_type (ancestor);
12136 if (ancestor)
12137 return ensure_not_abstract (sub, ancestor);
12139 return true;
12143 /* This check for typebound defined assignments is done recursively
12144 since the order in which derived types are resolved is not always in
12145 order of the declarations. */
12147 static void
12148 check_defined_assignments (gfc_symbol *derived)
12150 gfc_component *c;
12152 for (c = derived->components; c; c = c->next)
12154 if (c->ts.type != BT_DERIVED
12155 || c->attr.pointer
12156 || c->attr.allocatable
12157 || c->attr.proc_pointer_comp
12158 || c->attr.class_pointer
12159 || c->attr.proc_pointer)
12160 continue;
12162 if (c->ts.u.derived->attr.defined_assign_comp
12163 || (c->ts.u.derived->f2k_derived
12164 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12166 derived->attr.defined_assign_comp = 1;
12167 return;
12170 check_defined_assignments (c->ts.u.derived);
12171 if (c->ts.u.derived->attr.defined_assign_comp)
12173 derived->attr.defined_assign_comp = 1;
12174 return;
12180 /* Resolve the components of a derived type. This does not have to wait until
12181 resolution stage, but can be done as soon as the dt declaration has been
12182 parsed. */
12184 static bool
12185 resolve_fl_derived0 (gfc_symbol *sym)
12187 gfc_symbol* super_type;
12188 gfc_component *c;
12190 if (sym->attr.unlimited_polymorphic)
12191 return true;
12193 super_type = gfc_get_derived_super_type (sym);
12195 /* F2008, C432. */
12196 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12198 gfc_error ("As extending type '%s' at %L has a coarray component, "
12199 "parent type '%s' shall also have one", sym->name,
12200 &sym->declared_at, super_type->name);
12201 return false;
12204 /* Ensure the extended type gets resolved before we do. */
12205 if (super_type && !resolve_fl_derived0 (super_type))
12206 return false;
12208 /* An ABSTRACT type must be extensible. */
12209 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12211 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12212 sym->name, &sym->declared_at);
12213 return false;
12216 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12217 : sym->components;
12219 for ( ; c != NULL; c = c->next)
12221 if (c->attr.artificial)
12222 continue;
12224 /* F2008, C442. */
12225 if ((!sym->attr.is_class || c != sym->components)
12226 && c->attr.codimension
12227 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12229 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12230 "deferred shape", c->name, &c->loc);
12231 return false;
12234 /* F2008, C443. */
12235 if (c->attr.codimension && c->ts.type == BT_DERIVED
12236 && c->ts.u.derived->ts.is_iso_c)
12238 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12239 "shall not be a coarray", c->name, &c->loc);
12240 return false;
12243 /* F2008, C444. */
12244 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12245 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12246 || c->attr.allocatable))
12248 gfc_error ("Component '%s' at %L with coarray component "
12249 "shall be a nonpointer, nonallocatable scalar",
12250 c->name, &c->loc);
12251 return false;
12254 /* F2008, C448. */
12255 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12257 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12258 "is not an array pointer", c->name, &c->loc);
12259 return false;
12262 if (c->attr.proc_pointer && c->ts.interface)
12264 gfc_symbol *ifc = c->ts.interface;
12266 if (!sym->attr.vtype
12267 && !check_proc_interface (ifc, &c->loc))
12268 return false;
12270 if (ifc->attr.if_source || ifc->attr.intrinsic)
12272 /* Resolve interface and copy attributes. */
12273 if (ifc->formal && !ifc->formal_ns)
12274 resolve_symbol (ifc);
12275 if (ifc->attr.intrinsic)
12276 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12278 if (ifc->result)
12280 c->ts = ifc->result->ts;
12281 c->attr.allocatable = ifc->result->attr.allocatable;
12282 c->attr.pointer = ifc->result->attr.pointer;
12283 c->attr.dimension = ifc->result->attr.dimension;
12284 c->as = gfc_copy_array_spec (ifc->result->as);
12285 c->attr.class_ok = ifc->result->attr.class_ok;
12287 else
12289 c->ts = ifc->ts;
12290 c->attr.allocatable = ifc->attr.allocatable;
12291 c->attr.pointer = ifc->attr.pointer;
12292 c->attr.dimension = ifc->attr.dimension;
12293 c->as = gfc_copy_array_spec (ifc->as);
12294 c->attr.class_ok = ifc->attr.class_ok;
12296 c->ts.interface = ifc;
12297 c->attr.function = ifc->attr.function;
12298 c->attr.subroutine = ifc->attr.subroutine;
12300 c->attr.pure = ifc->attr.pure;
12301 c->attr.elemental = ifc->attr.elemental;
12302 c->attr.recursive = ifc->attr.recursive;
12303 c->attr.always_explicit = ifc->attr.always_explicit;
12304 c->attr.ext_attr |= ifc->attr.ext_attr;
12305 /* Copy char length. */
12306 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12308 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12309 if (cl->length && !cl->resolved
12310 && !gfc_resolve_expr (cl->length))
12311 return false;
12312 c->ts.u.cl = cl;
12316 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12318 /* Since PPCs are not implicitly typed, a PPC without an explicit
12319 interface must be a subroutine. */
12320 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12323 /* Procedure pointer components: Check PASS arg. */
12324 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12325 && !sym->attr.vtype)
12327 gfc_symbol* me_arg;
12329 if (c->tb->pass_arg)
12331 gfc_formal_arglist* i;
12333 /* If an explicit passing argument name is given, walk the arg-list
12334 and look for it. */
12336 me_arg = NULL;
12337 c->tb->pass_arg_num = 1;
12338 for (i = c->ts.interface->formal; i; i = i->next)
12340 if (!strcmp (i->sym->name, c->tb->pass_arg))
12342 me_arg = i->sym;
12343 break;
12345 c->tb->pass_arg_num++;
12348 if (!me_arg)
12350 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12351 "at %L has no argument '%s'", c->name,
12352 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12353 c->tb->error = 1;
12354 return false;
12357 else
12359 /* Otherwise, take the first one; there should in fact be at least
12360 one. */
12361 c->tb->pass_arg_num = 1;
12362 if (!c->ts.interface->formal)
12364 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12365 "must have at least one argument",
12366 c->name, &c->loc);
12367 c->tb->error = 1;
12368 return false;
12370 me_arg = c->ts.interface->formal->sym;
12373 /* Now check that the argument-type matches. */
12374 gcc_assert (me_arg);
12375 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12376 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12377 || (me_arg->ts.type == BT_CLASS
12378 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12380 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12381 " the derived type '%s'", me_arg->name, c->name,
12382 me_arg->name, &c->loc, sym->name);
12383 c->tb->error = 1;
12384 return false;
12387 /* Check for C453. */
12388 if (me_arg->attr.dimension)
12390 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12391 "must be scalar", me_arg->name, c->name, me_arg->name,
12392 &c->loc);
12393 c->tb->error = 1;
12394 return false;
12397 if (me_arg->attr.pointer)
12399 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12400 "may not have the POINTER attribute", me_arg->name,
12401 c->name, me_arg->name, &c->loc);
12402 c->tb->error = 1;
12403 return false;
12406 if (me_arg->attr.allocatable)
12408 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12409 "may not be ALLOCATABLE", me_arg->name, c->name,
12410 me_arg->name, &c->loc);
12411 c->tb->error = 1;
12412 return false;
12415 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12416 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12417 " at %L", c->name, &c->loc);
12421 /* Check type-spec if this is not the parent-type component. */
12422 if (((sym->attr.is_class
12423 && (!sym->components->ts.u.derived->attr.extension
12424 || c != sym->components->ts.u.derived->components))
12425 || (!sym->attr.is_class
12426 && (!sym->attr.extension || c != sym->components)))
12427 && !sym->attr.vtype
12428 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12429 return false;
12431 /* If this type is an extension, set the accessibility of the parent
12432 component. */
12433 if (super_type
12434 && ((sym->attr.is_class
12435 && c == sym->components->ts.u.derived->components)
12436 || (!sym->attr.is_class && c == sym->components))
12437 && strcmp (super_type->name, c->name) == 0)
12438 c->attr.access = super_type->attr.access;
12440 /* If this type is an extension, see if this component has the same name
12441 as an inherited type-bound procedure. */
12442 if (super_type && !sym->attr.is_class
12443 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12445 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12446 " inherited type-bound procedure",
12447 c->name, sym->name, &c->loc);
12448 return false;
12451 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12452 && !c->ts.deferred)
12454 if (c->ts.u.cl->length == NULL
12455 || (!resolve_charlen(c->ts.u.cl))
12456 || !gfc_is_constant_expr (c->ts.u.cl->length))
12458 gfc_error ("Character length of component '%s' needs to "
12459 "be a constant specification expression at %L",
12460 c->name,
12461 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12462 return false;
12466 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12467 && !c->attr.pointer && !c->attr.allocatable)
12469 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12470 "length must be a POINTER or ALLOCATABLE",
12471 c->name, sym->name, &c->loc);
12472 return false;
12475 /* Add the hidden deferred length field. */
12476 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
12477 && !sym->attr.is_class)
12479 char name[GFC_MAX_SYMBOL_LEN+9];
12480 gfc_component *strlen;
12481 sprintf (name, "_%s_length", c->name);
12482 strlen = gfc_find_component (sym, name, true, true);
12483 if (strlen == NULL)
12485 if (!gfc_add_component (sym, name, &strlen))
12486 return false;
12487 strlen->ts.type = BT_INTEGER;
12488 strlen->ts.kind = gfc_charlen_int_kind;
12489 strlen->attr.access = ACCESS_PRIVATE;
12490 strlen->attr.deferred_parameter = 1;
12494 if (c->ts.type == BT_DERIVED
12495 && sym->component_access != ACCESS_PRIVATE
12496 && gfc_check_symbol_access (sym)
12497 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12498 && !c->ts.u.derived->attr.use_assoc
12499 && !gfc_check_symbol_access (c->ts.u.derived)
12500 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12501 "PRIVATE type and cannot be a component of "
12502 "'%s', which is PUBLIC at %L", c->name,
12503 sym->name, &sym->declared_at))
12504 return false;
12506 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12508 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12509 "type %s", c->name, &c->loc, sym->name);
12510 return false;
12513 if (sym->attr.sequence)
12515 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12517 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12518 "not have the SEQUENCE attribute",
12519 c->ts.u.derived->name, &sym->declared_at);
12520 return false;
12524 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12525 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12526 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12527 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12528 CLASS_DATA (c)->ts.u.derived
12529 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12531 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12532 && c->attr.pointer && c->ts.u.derived->components == NULL
12533 && !c->ts.u.derived->attr.zero_comp)
12535 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12536 "that has not been declared", c->name, sym->name,
12537 &c->loc);
12538 return false;
12541 if (c->ts.type == BT_CLASS && c->attr.class_ok
12542 && CLASS_DATA (c)->attr.class_pointer
12543 && CLASS_DATA (c)->ts.u.derived->components == NULL
12544 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12545 && !UNLIMITED_POLY (c))
12547 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12548 "that has not been declared", c->name, sym->name,
12549 &c->loc);
12550 return false;
12553 /* C437. */
12554 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12555 && (!c->attr.class_ok
12556 || !(CLASS_DATA (c)->attr.class_pointer
12557 || CLASS_DATA (c)->attr.allocatable)))
12559 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12560 "or pointer", c->name, &c->loc);
12561 /* Prevent a recurrence of the error. */
12562 c->ts.type = BT_UNKNOWN;
12563 return false;
12566 /* Ensure that all the derived type components are put on the
12567 derived type list; even in formal namespaces, where derived type
12568 pointer components might not have been declared. */
12569 if (c->ts.type == BT_DERIVED
12570 && c->ts.u.derived
12571 && c->ts.u.derived->components
12572 && c->attr.pointer
12573 && sym != c->ts.u.derived)
12574 add_dt_to_dt_list (c->ts.u.derived);
12576 if (!gfc_resolve_array_spec (c->as,
12577 !(c->attr.pointer || c->attr.proc_pointer
12578 || c->attr.allocatable)))
12579 return false;
12581 if (c->initializer && !sym->attr.vtype
12582 && !gfc_check_assign_symbol (sym, c, c->initializer))
12583 return false;
12586 check_defined_assignments (sym);
12588 if (!sym->attr.defined_assign_comp && super_type)
12589 sym->attr.defined_assign_comp
12590 = super_type->attr.defined_assign_comp;
12592 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12593 all DEFERRED bindings are overridden. */
12594 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12595 && !sym->attr.is_class
12596 && !ensure_not_abstract (sym, super_type))
12597 return false;
12599 /* Add derived type to the derived type list. */
12600 add_dt_to_dt_list (sym);
12602 return true;
12606 /* The following procedure does the full resolution of a derived type,
12607 including resolution of all type-bound procedures (if present). In contrast
12608 to 'resolve_fl_derived0' this can only be done after the module has been
12609 parsed completely. */
12611 static bool
12612 resolve_fl_derived (gfc_symbol *sym)
12614 gfc_symbol *gen_dt = NULL;
12616 if (sym->attr.unlimited_polymorphic)
12617 return true;
12619 if (!sym->attr.is_class)
12620 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12621 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12622 && (!gen_dt->generic->sym->attr.use_assoc
12623 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12624 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12625 "'%s' at %L being the same name as derived "
12626 "type at %L", sym->name,
12627 gen_dt->generic->sym == sym
12628 ? gen_dt->generic->next->sym->name
12629 : gen_dt->generic->sym->name,
12630 gen_dt->generic->sym == sym
12631 ? &gen_dt->generic->next->sym->declared_at
12632 : &gen_dt->generic->sym->declared_at,
12633 &sym->declared_at))
12634 return false;
12636 /* Resolve the finalizer procedures. */
12637 if (!gfc_resolve_finalizers (sym, NULL))
12638 return false;
12640 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12642 /* Fix up incomplete CLASS symbols. */
12643 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12644 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12646 /* Nothing more to do for unlimited polymorphic entities. */
12647 if (data->ts.u.derived->attr.unlimited_polymorphic)
12648 return true;
12649 else if (vptr->ts.u.derived == NULL)
12651 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12652 gcc_assert (vtab);
12653 vptr->ts.u.derived = vtab->ts.u.derived;
12657 if (!resolve_fl_derived0 (sym))
12658 return false;
12660 /* Resolve the type-bound procedures. */
12661 if (!resolve_typebound_procedures (sym))
12662 return false;
12664 return true;
12668 static bool
12669 resolve_fl_namelist (gfc_symbol *sym)
12671 gfc_namelist *nl;
12672 gfc_symbol *nlsym;
12674 for (nl = sym->namelist; nl; nl = nl->next)
12676 /* Check again, the check in match only works if NAMELIST comes
12677 after the decl. */
12678 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12680 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12681 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12682 return false;
12685 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12686 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12687 "with assumed shape in namelist '%s' at %L",
12688 nl->sym->name, sym->name, &sym->declared_at))
12689 return false;
12691 if (is_non_constant_shape_array (nl->sym)
12692 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12693 "with nonconstant shape in namelist '%s' at %L",
12694 nl->sym->name, sym->name, &sym->declared_at))
12695 return false;
12697 if (nl->sym->ts.type == BT_CHARACTER
12698 && (nl->sym->ts.u.cl->length == NULL
12699 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12700 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12701 "nonconstant character length in "
12702 "namelist '%s' at %L", nl->sym->name,
12703 sym->name, &sym->declared_at))
12704 return false;
12706 /* FIXME: Once UDDTIO is implemented, the following can be
12707 removed. */
12708 if (nl->sym->ts.type == BT_CLASS)
12710 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12711 "polymorphic and requires a defined input/output "
12712 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12713 return false;
12716 if (nl->sym->ts.type == BT_DERIVED
12717 && (nl->sym->ts.u.derived->attr.alloc_comp
12718 || nl->sym->ts.u.derived->attr.pointer_comp))
12720 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12721 "namelist '%s' at %L with ALLOCATABLE "
12722 "or POINTER components", nl->sym->name,
12723 sym->name, &sym->declared_at))
12724 return false;
12726 /* FIXME: Once UDDTIO is implemented, the following can be
12727 removed. */
12728 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12729 "ALLOCATABLE or POINTER components and thus requires "
12730 "a defined input/output procedure", nl->sym->name,
12731 sym->name, &sym->declared_at);
12732 return false;
12736 /* Reject PRIVATE objects in a PUBLIC namelist. */
12737 if (gfc_check_symbol_access (sym))
12739 for (nl = sym->namelist; nl; nl = nl->next)
12741 if (!nl->sym->attr.use_assoc
12742 && !is_sym_host_assoc (nl->sym, sym->ns)
12743 && !gfc_check_symbol_access (nl->sym))
12745 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12746 "cannot be member of PUBLIC namelist '%s' at %L",
12747 nl->sym->name, sym->name, &sym->declared_at);
12748 return false;
12751 /* Types with private components that came here by USE-association. */
12752 if (nl->sym->ts.type == BT_DERIVED
12753 && derived_inaccessible (nl->sym->ts.u.derived))
12755 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12756 "components and cannot be member of namelist '%s' at %L",
12757 nl->sym->name, sym->name, &sym->declared_at);
12758 return false;
12761 /* Types with private components that are defined in the same module. */
12762 if (nl->sym->ts.type == BT_DERIVED
12763 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12764 && nl->sym->ts.u.derived->attr.private_comp)
12766 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12767 "cannot be a member of PUBLIC namelist '%s' at %L",
12768 nl->sym->name, sym->name, &sym->declared_at);
12769 return false;
12775 /* 14.1.2 A module or internal procedure represent local entities
12776 of the same type as a namelist member and so are not allowed. */
12777 for (nl = sym->namelist; nl; nl = nl->next)
12779 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12780 continue;
12782 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12783 if ((nl->sym == sym->ns->proc_name)
12785 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12786 continue;
12788 nlsym = NULL;
12789 if (nl->sym->name)
12790 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12791 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12793 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12794 "attribute in '%s' at %L", nlsym->name,
12795 &sym->declared_at);
12796 return false;
12800 return true;
12804 static bool
12805 resolve_fl_parameter (gfc_symbol *sym)
12807 /* A parameter array's shape needs to be constant. */
12808 if (sym->as != NULL
12809 && (sym->as->type == AS_DEFERRED
12810 || is_non_constant_shape_array (sym)))
12812 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12813 "or of deferred shape", sym->name, &sym->declared_at);
12814 return false;
12817 /* Make sure a parameter that has been implicitly typed still
12818 matches the implicit type, since PARAMETER statements can precede
12819 IMPLICIT statements. */
12820 if (sym->attr.implicit_type
12821 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12822 sym->ns)))
12824 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12825 "later IMPLICIT type", sym->name, &sym->declared_at);
12826 return false;
12829 /* Make sure the types of derived parameters are consistent. This
12830 type checking is deferred until resolution because the type may
12831 refer to a derived type from the host. */
12832 if (sym->ts.type == BT_DERIVED
12833 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12835 gfc_error ("Incompatible derived type in PARAMETER at %L",
12836 &sym->value->where);
12837 return false;
12839 return true;
12843 /* Do anything necessary to resolve a symbol. Right now, we just
12844 assume that an otherwise unknown symbol is a variable. This sort
12845 of thing commonly happens for symbols in module. */
12847 static void
12848 resolve_symbol (gfc_symbol *sym)
12850 int check_constant, mp_flag;
12851 gfc_symtree *symtree;
12852 gfc_symtree *this_symtree;
12853 gfc_namespace *ns;
12854 gfc_component *c;
12855 symbol_attribute class_attr;
12856 gfc_array_spec *as;
12857 bool saved_specification_expr;
12859 if (sym->resolved)
12860 return;
12861 sym->resolved = 1;
12863 if (sym->attr.artificial)
12864 return;
12866 if (sym->attr.unlimited_polymorphic)
12867 return;
12869 if (sym->attr.flavor == FL_UNKNOWN
12870 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12871 && !sym->attr.generic && !sym->attr.external
12872 && sym->attr.if_source == IFSRC_UNKNOWN
12873 && sym->ts.type == BT_UNKNOWN))
12876 /* If we find that a flavorless symbol is an interface in one of the
12877 parent namespaces, find its symtree in this namespace, free the
12878 symbol and set the symtree to point to the interface symbol. */
12879 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12881 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12882 if (symtree && (symtree->n.sym->generic ||
12883 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12884 && sym->ns->construct_entities)))
12886 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12887 sym->name);
12888 gfc_release_symbol (sym);
12889 symtree->n.sym->refs++;
12890 this_symtree->n.sym = symtree->n.sym;
12891 return;
12895 /* Otherwise give it a flavor according to such attributes as
12896 it has. */
12897 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12898 && sym->attr.intrinsic == 0)
12899 sym->attr.flavor = FL_VARIABLE;
12900 else if (sym->attr.flavor == FL_UNKNOWN)
12902 sym->attr.flavor = FL_PROCEDURE;
12903 if (sym->attr.dimension)
12904 sym->attr.function = 1;
12908 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12909 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12911 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12912 && !resolve_procedure_interface (sym))
12913 return;
12915 if (sym->attr.is_protected && !sym->attr.proc_pointer
12916 && (sym->attr.procedure || sym->attr.external))
12918 if (sym->attr.external)
12919 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12920 "at %L", &sym->declared_at);
12921 else
12922 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12923 "at %L", &sym->declared_at);
12925 return;
12928 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12929 return;
12931 /* Symbols that are module procedures with results (functions) have
12932 the types and array specification copied for type checking in
12933 procedures that call them, as well as for saving to a module
12934 file. These symbols can't stand the scrutiny that their results
12935 can. */
12936 mp_flag = (sym->result != NULL && sym->result != sym);
12938 /* Make sure that the intrinsic is consistent with its internal
12939 representation. This needs to be done before assigning a default
12940 type to avoid spurious warnings. */
12941 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12942 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12943 return;
12945 /* Resolve associate names. */
12946 if (sym->assoc)
12947 resolve_assoc_var (sym, true);
12949 /* Assign default type to symbols that need one and don't have one. */
12950 if (sym->ts.type == BT_UNKNOWN)
12952 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12954 gfc_set_default_type (sym, 1, NULL);
12957 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12958 && !sym->attr.function && !sym->attr.subroutine
12959 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12960 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12962 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12964 /* The specific case of an external procedure should emit an error
12965 in the case that there is no implicit type. */
12966 if (!mp_flag)
12967 gfc_set_default_type (sym, sym->attr.external, NULL);
12968 else
12970 /* Result may be in another namespace. */
12971 resolve_symbol (sym->result);
12973 if (!sym->result->attr.proc_pointer)
12975 sym->ts = sym->result->ts;
12976 sym->as = gfc_copy_array_spec (sym->result->as);
12977 sym->attr.dimension = sym->result->attr.dimension;
12978 sym->attr.pointer = sym->result->attr.pointer;
12979 sym->attr.allocatable = sym->result->attr.allocatable;
12980 sym->attr.contiguous = sym->result->attr.contiguous;
12985 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12987 bool saved_specification_expr = specification_expr;
12988 specification_expr = true;
12989 gfc_resolve_array_spec (sym->result->as, false);
12990 specification_expr = saved_specification_expr;
12993 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12995 as = CLASS_DATA (sym)->as;
12996 class_attr = CLASS_DATA (sym)->attr;
12997 class_attr.pointer = class_attr.class_pointer;
12999 else
13001 class_attr = sym->attr;
13002 as = sym->as;
13005 /* F2008, C530. */
13006 if (sym->attr.contiguous
13007 && (!class_attr.dimension
13008 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13009 && !class_attr.pointer)))
13011 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
13012 "array pointer or an assumed-shape or assumed-rank array",
13013 sym->name, &sym->declared_at);
13014 return;
13017 /* Assumed size arrays and assumed shape arrays must be dummy
13018 arguments. Array-spec's of implied-shape should have been resolved to
13019 AS_EXPLICIT already. */
13021 if (as)
13023 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13024 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13025 || as->type == AS_ASSUMED_SHAPE)
13026 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13028 if (as->type == AS_ASSUMED_SIZE)
13029 gfc_error ("Assumed size array at %L must be a dummy argument",
13030 &sym->declared_at);
13031 else
13032 gfc_error ("Assumed shape array at %L must be a dummy argument",
13033 &sym->declared_at);
13034 return;
13036 /* TS 29113, C535a. */
13037 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13038 && !sym->attr.select_type_temporary)
13040 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13041 &sym->declared_at);
13042 return;
13044 if (as->type == AS_ASSUMED_RANK
13045 && (sym->attr.codimension || sym->attr.value))
13047 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13048 "CODIMENSION attribute", &sym->declared_at);
13049 return;
13053 /* Make sure symbols with known intent or optional are really dummy
13054 variable. Because of ENTRY statement, this has to be deferred
13055 until resolution time. */
13057 if (!sym->attr.dummy
13058 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13060 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13061 return;
13064 if (sym->attr.value && !sym->attr.dummy)
13066 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
13067 "it is not a dummy argument", sym->name, &sym->declared_at);
13068 return;
13071 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13073 gfc_charlen *cl = sym->ts.u.cl;
13074 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13076 gfc_error ("Character dummy variable '%s' at %L with VALUE "
13077 "attribute must have constant length",
13078 sym->name, &sym->declared_at);
13079 return;
13082 if (sym->ts.is_c_interop
13083 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13085 gfc_error ("C interoperable character dummy variable '%s' at %L "
13086 "with VALUE attribute must have length one",
13087 sym->name, &sym->declared_at);
13088 return;
13092 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13093 && sym->ts.u.derived->attr.generic)
13095 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13096 if (!sym->ts.u.derived)
13098 gfc_error ("The derived type '%s' at %L is of type '%s', "
13099 "which has not been defined", sym->name,
13100 &sym->declared_at, sym->ts.u.derived->name);
13101 sym->ts.type = BT_UNKNOWN;
13102 return;
13106 /* Use the same constraints as TYPE(*), except for the type check
13107 and that only scalars and assumed-size arrays are permitted. */
13108 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13110 if (!sym->attr.dummy)
13112 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13113 "a dummy argument", sym->name, &sym->declared_at);
13114 return;
13117 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13118 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13119 && sym->ts.type != BT_COMPLEX)
13121 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13122 "of type TYPE(*) or of an numeric intrinsic type",
13123 sym->name, &sym->declared_at);
13124 return;
13127 if (sym->attr.allocatable || sym->attr.codimension
13128 || sym->attr.pointer || sym->attr.value)
13130 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13131 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13132 "attribute", sym->name, &sym->declared_at);
13133 return;
13136 if (sym->attr.intent == INTENT_OUT)
13138 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13139 "have the INTENT(OUT) attribute",
13140 sym->name, &sym->declared_at);
13141 return;
13143 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13145 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13146 "either be a scalar or an assumed-size array",
13147 sym->name, &sym->declared_at);
13148 return;
13151 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13152 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13153 packing. */
13154 sym->ts.type = BT_ASSUMED;
13155 sym->as = gfc_get_array_spec ();
13156 sym->as->type = AS_ASSUMED_SIZE;
13157 sym->as->rank = 1;
13158 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13160 else if (sym->ts.type == BT_ASSUMED)
13162 /* TS 29113, C407a. */
13163 if (!sym->attr.dummy)
13165 gfc_error ("Assumed type of variable %s at %L is only permitted "
13166 "for dummy variables", sym->name, &sym->declared_at);
13167 return;
13169 if (sym->attr.allocatable || sym->attr.codimension
13170 || sym->attr.pointer || sym->attr.value)
13172 gfc_error ("Assumed-type variable %s at %L may not have the "
13173 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13174 sym->name, &sym->declared_at);
13175 return;
13177 if (sym->attr.intent == INTENT_OUT)
13179 gfc_error ("Assumed-type variable %s at %L may not have the "
13180 "INTENT(OUT) attribute",
13181 sym->name, &sym->declared_at);
13182 return;
13184 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13186 gfc_error ("Assumed-type variable %s at %L shall not be an "
13187 "explicit-shape array", sym->name, &sym->declared_at);
13188 return;
13192 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13193 do this for something that was implicitly typed because that is handled
13194 in gfc_set_default_type. Handle dummy arguments and procedure
13195 definitions separately. Also, anything that is use associated is not
13196 handled here but instead is handled in the module it is declared in.
13197 Finally, derived type definitions are allowed to be BIND(C) since that
13198 only implies that they're interoperable, and they are checked fully for
13199 interoperability when a variable is declared of that type. */
13200 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13201 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13202 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13204 bool t = true;
13206 /* First, make sure the variable is declared at the
13207 module-level scope (J3/04-007, Section 15.3). */
13208 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13209 sym->attr.in_common == 0)
13211 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13212 "is neither a COMMON block nor declared at the "
13213 "module level scope", sym->name, &(sym->declared_at));
13214 t = false;
13216 else if (sym->common_head != NULL)
13218 t = verify_com_block_vars_c_interop (sym->common_head);
13220 else
13222 /* If type() declaration, we need to verify that the components
13223 of the given type are all C interoperable, etc. */
13224 if (sym->ts.type == BT_DERIVED &&
13225 sym->ts.u.derived->attr.is_c_interop != 1)
13227 /* Make sure the user marked the derived type as BIND(C). If
13228 not, call the verify routine. This could print an error
13229 for the derived type more than once if multiple variables
13230 of that type are declared. */
13231 if (sym->ts.u.derived->attr.is_bind_c != 1)
13232 verify_bind_c_derived_type (sym->ts.u.derived);
13233 t = false;
13236 /* Verify the variable itself as C interoperable if it
13237 is BIND(C). It is not possible for this to succeed if
13238 the verify_bind_c_derived_type failed, so don't have to handle
13239 any error returned by verify_bind_c_derived_type. */
13240 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13241 sym->common_block);
13244 if (!t)
13246 /* clear the is_bind_c flag to prevent reporting errors more than
13247 once if something failed. */
13248 sym->attr.is_bind_c = 0;
13249 return;
13253 /* If a derived type symbol has reached this point, without its
13254 type being declared, we have an error. Notice that most
13255 conditions that produce undefined derived types have already
13256 been dealt with. However, the likes of:
13257 implicit type(t) (t) ..... call foo (t) will get us here if
13258 the type is not declared in the scope of the implicit
13259 statement. Change the type to BT_UNKNOWN, both because it is so
13260 and to prevent an ICE. */
13261 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13262 && sym->ts.u.derived->components == NULL
13263 && !sym->ts.u.derived->attr.zero_comp)
13265 gfc_error ("The derived type '%s' at %L is of type '%s', "
13266 "which has not been defined", sym->name,
13267 &sym->declared_at, sym->ts.u.derived->name);
13268 sym->ts.type = BT_UNKNOWN;
13269 return;
13272 /* Make sure that the derived type has been resolved and that the
13273 derived type is visible in the symbol's namespace, if it is a
13274 module function and is not PRIVATE. */
13275 if (sym->ts.type == BT_DERIVED
13276 && sym->ts.u.derived->attr.use_assoc
13277 && sym->ns->proc_name
13278 && sym->ns->proc_name->attr.flavor == FL_MODULE
13279 && !resolve_fl_derived (sym->ts.u.derived))
13280 return;
13282 /* Unless the derived-type declaration is use associated, Fortran 95
13283 does not allow public entries of private derived types.
13284 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13285 161 in 95-006r3. */
13286 if (sym->ts.type == BT_DERIVED
13287 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13288 && !sym->ts.u.derived->attr.use_assoc
13289 && gfc_check_symbol_access (sym)
13290 && !gfc_check_symbol_access (sym->ts.u.derived)
13291 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13292 "derived type '%s'",
13293 (sym->attr.flavor == FL_PARAMETER)
13294 ? "parameter" : "variable",
13295 sym->name, &sym->declared_at,
13296 sym->ts.u.derived->name))
13297 return;
13299 /* F2008, C1302. */
13300 if (sym->ts.type == BT_DERIVED
13301 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13302 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13303 || sym->ts.u.derived->attr.lock_comp)
13304 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13306 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13307 "type LOCK_TYPE must be a coarray", sym->name,
13308 &sym->declared_at);
13309 return;
13312 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13313 default initialization is defined (5.1.2.4.4). */
13314 if (sym->ts.type == BT_DERIVED
13315 && sym->attr.dummy
13316 && sym->attr.intent == INTENT_OUT
13317 && sym->as
13318 && sym->as->type == AS_ASSUMED_SIZE)
13320 for (c = sym->ts.u.derived->components; c; c = c->next)
13322 if (c->initializer)
13324 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13325 "ASSUMED SIZE and so cannot have a default initializer",
13326 sym->name, &sym->declared_at);
13327 return;
13332 /* F2008, C542. */
13333 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13334 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13336 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13337 "INTENT(OUT)", sym->name, &sym->declared_at);
13338 return;
13341 /* F2008, C525. */
13342 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13343 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13344 && CLASS_DATA (sym)->attr.coarray_comp))
13345 || class_attr.codimension)
13346 && (sym->attr.result || sym->result == sym))
13348 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13349 "a coarray component", sym->name, &sym->declared_at);
13350 return;
13353 /* F2008, C524. */
13354 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13355 && sym->ts.u.derived->ts.is_iso_c)
13357 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13358 "shall not be a coarray", sym->name, &sym->declared_at);
13359 return;
13362 /* F2008, C525. */
13363 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13364 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13365 && CLASS_DATA (sym)->attr.coarray_comp))
13366 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13367 || class_attr.allocatable))
13369 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13370 "nonpointer, nonallocatable scalar, which is not a coarray",
13371 sym->name, &sym->declared_at);
13372 return;
13375 /* F2008, C526. The function-result case was handled above. */
13376 if (class_attr.codimension
13377 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13378 || sym->attr.select_type_temporary
13379 || sym->ns->save_all
13380 || sym->ns->proc_name->attr.flavor == FL_MODULE
13381 || sym->ns->proc_name->attr.is_main_program
13382 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13384 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13385 "nor a dummy argument", sym->name, &sym->declared_at);
13386 return;
13388 /* F2008, C528. */
13389 else if (class_attr.codimension && !sym->attr.select_type_temporary
13390 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13392 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13393 "deferred shape", sym->name, &sym->declared_at);
13394 return;
13396 else if (class_attr.codimension && class_attr.allocatable && as
13397 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13399 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13400 "deferred shape", sym->name, &sym->declared_at);
13401 return;
13404 /* F2008, C541. */
13405 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13406 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13407 && CLASS_DATA (sym)->attr.coarray_comp))
13408 || (class_attr.codimension && class_attr.allocatable))
13409 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13411 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13412 "allocatable coarray or have coarray components",
13413 sym->name, &sym->declared_at);
13414 return;
13417 if (class_attr.codimension && sym->attr.dummy
13418 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13420 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13421 "procedure '%s'", sym->name, &sym->declared_at,
13422 sym->ns->proc_name->name);
13423 return;
13426 if (sym->ts.type == BT_LOGICAL
13427 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13428 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13429 && sym->ns->proc_name->attr.is_bind_c)))
13431 int i;
13432 for (i = 0; gfc_logical_kinds[i].kind; i++)
13433 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13434 break;
13435 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13436 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13437 "%L with non-C_Bool kind in BIND(C) procedure "
13438 "'%s'", sym->name, &sym->declared_at,
13439 sym->ns->proc_name->name))
13440 return;
13441 else if (!gfc_logical_kinds[i].c_bool
13442 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13443 "'%s' at %L with non-C_Bool kind in "
13444 "BIND(C) procedure '%s'", sym->name,
13445 &sym->declared_at,
13446 sym->attr.function ? sym->name
13447 : sym->ns->proc_name->name))
13448 return;
13451 switch (sym->attr.flavor)
13453 case FL_VARIABLE:
13454 if (!resolve_fl_variable (sym, mp_flag))
13455 return;
13456 break;
13458 case FL_PROCEDURE:
13459 if (!resolve_fl_procedure (sym, mp_flag))
13460 return;
13461 break;
13463 case FL_NAMELIST:
13464 if (!resolve_fl_namelist (sym))
13465 return;
13466 break;
13468 case FL_PARAMETER:
13469 if (!resolve_fl_parameter (sym))
13470 return;
13471 break;
13473 default:
13474 break;
13477 /* Resolve array specifier. Check as well some constraints
13478 on COMMON blocks. */
13480 check_constant = sym->attr.in_common && !sym->attr.pointer;
13482 /* Set the formal_arg_flag so that check_conflict will not throw
13483 an error for host associated variables in the specification
13484 expression for an array_valued function. */
13485 if (sym->attr.function && sym->as)
13486 formal_arg_flag = 1;
13488 saved_specification_expr = specification_expr;
13489 specification_expr = true;
13490 gfc_resolve_array_spec (sym->as, check_constant);
13491 specification_expr = saved_specification_expr;
13493 formal_arg_flag = 0;
13495 /* Resolve formal namespaces. */
13496 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13497 && !sym->attr.contained && !sym->attr.intrinsic)
13498 gfc_resolve (sym->formal_ns);
13500 /* Make sure the formal namespace is present. */
13501 if (sym->formal && !sym->formal_ns)
13503 gfc_formal_arglist *formal = sym->formal;
13504 while (formal && !formal->sym)
13505 formal = formal->next;
13507 if (formal)
13509 sym->formal_ns = formal->sym->ns;
13510 if (sym->ns != formal->sym->ns)
13511 sym->formal_ns->refs++;
13515 /* Check threadprivate restrictions. */
13516 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13517 && (!sym->attr.in_common
13518 && sym->module == NULL
13519 && (sym->ns->proc_name == NULL
13520 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13521 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13523 /* If we have come this far we can apply default-initializers, as
13524 described in 14.7.5, to those variables that have not already
13525 been assigned one. */
13526 if (sym->ts.type == BT_DERIVED
13527 && !sym->value
13528 && !sym->attr.allocatable
13529 && !sym->attr.alloc_comp)
13531 symbol_attribute *a = &sym->attr;
13533 if ((!a->save && !a->dummy && !a->pointer
13534 && !a->in_common && !a->use_assoc
13535 && (a->referenced || a->result)
13536 && !(a->function && sym != sym->result))
13537 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13538 apply_default_init (sym);
13541 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13542 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13543 && !CLASS_DATA (sym)->attr.class_pointer
13544 && !CLASS_DATA (sym)->attr.allocatable)
13545 apply_default_init (sym);
13547 /* If this symbol has a type-spec, check it. */
13548 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13549 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13550 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13551 return;
13555 /************* Resolve DATA statements *************/
13557 static struct
13559 gfc_data_value *vnode;
13560 mpz_t left;
13562 values;
13565 /* Advance the values structure to point to the next value in the data list. */
13567 static bool
13568 next_data_value (void)
13570 while (mpz_cmp_ui (values.left, 0) == 0)
13573 if (values.vnode->next == NULL)
13574 return false;
13576 values.vnode = values.vnode->next;
13577 mpz_set (values.left, values.vnode->repeat);
13580 return true;
13584 static bool
13585 check_data_variable (gfc_data_variable *var, locus *where)
13587 gfc_expr *e;
13588 mpz_t size;
13589 mpz_t offset;
13590 bool t;
13591 ar_type mark = AR_UNKNOWN;
13592 int i;
13593 mpz_t section_index[GFC_MAX_DIMENSIONS];
13594 gfc_ref *ref;
13595 gfc_array_ref *ar;
13596 gfc_symbol *sym;
13597 int has_pointer;
13599 if (!gfc_resolve_expr (var->expr))
13600 return false;
13602 ar = NULL;
13603 mpz_init_set_si (offset, 0);
13604 e = var->expr;
13606 if (e->expr_type != EXPR_VARIABLE)
13607 gfc_internal_error ("check_data_variable(): Bad expression");
13609 sym = e->symtree->n.sym;
13611 if (sym->ns->is_block_data && !sym->attr.in_common)
13613 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13614 sym->name, &sym->declared_at);
13617 if (e->ref == NULL && sym->as)
13619 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13620 " declaration", sym->name, where);
13621 return false;
13624 has_pointer = sym->attr.pointer;
13626 if (gfc_is_coindexed (e))
13628 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13629 where);
13630 return false;
13633 for (ref = e->ref; ref; ref = ref->next)
13635 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13636 has_pointer = 1;
13638 if (has_pointer
13639 && ref->type == REF_ARRAY
13640 && ref->u.ar.type != AR_FULL)
13642 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13643 "be a full array", sym->name, where);
13644 return false;
13648 if (e->rank == 0 || has_pointer)
13650 mpz_init_set_ui (size, 1);
13651 ref = NULL;
13653 else
13655 ref = e->ref;
13657 /* Find the array section reference. */
13658 for (ref = e->ref; ref; ref = ref->next)
13660 if (ref->type != REF_ARRAY)
13661 continue;
13662 if (ref->u.ar.type == AR_ELEMENT)
13663 continue;
13664 break;
13666 gcc_assert (ref);
13668 /* Set marks according to the reference pattern. */
13669 switch (ref->u.ar.type)
13671 case AR_FULL:
13672 mark = AR_FULL;
13673 break;
13675 case AR_SECTION:
13676 ar = &ref->u.ar;
13677 /* Get the start position of array section. */
13678 gfc_get_section_index (ar, section_index, &offset);
13679 mark = AR_SECTION;
13680 break;
13682 default:
13683 gcc_unreachable ();
13686 if (!gfc_array_size (e, &size))
13688 gfc_error ("Nonconstant array section at %L in DATA statement",
13689 &e->where);
13690 mpz_clear (offset);
13691 return false;
13695 t = true;
13697 while (mpz_cmp_ui (size, 0) > 0)
13699 if (!next_data_value ())
13701 gfc_error ("DATA statement at %L has more variables than values",
13702 where);
13703 t = false;
13704 break;
13707 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13708 if (!t)
13709 break;
13711 /* If we have more than one element left in the repeat count,
13712 and we have more than one element left in the target variable,
13713 then create a range assignment. */
13714 /* FIXME: Only done for full arrays for now, since array sections
13715 seem tricky. */
13716 if (mark == AR_FULL && ref && ref->next == NULL
13717 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13719 mpz_t range;
13721 if (mpz_cmp (size, values.left) >= 0)
13723 mpz_init_set (range, values.left);
13724 mpz_sub (size, size, values.left);
13725 mpz_set_ui (values.left, 0);
13727 else
13729 mpz_init_set (range, size);
13730 mpz_sub (values.left, values.left, size);
13731 mpz_set_ui (size, 0);
13734 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13735 offset, &range);
13737 mpz_add (offset, offset, range);
13738 mpz_clear (range);
13740 if (!t)
13741 break;
13744 /* Assign initial value to symbol. */
13745 else
13747 mpz_sub_ui (values.left, values.left, 1);
13748 mpz_sub_ui (size, size, 1);
13750 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13751 offset, NULL);
13752 if (!t)
13753 break;
13755 if (mark == AR_FULL)
13756 mpz_add_ui (offset, offset, 1);
13758 /* Modify the array section indexes and recalculate the offset
13759 for next element. */
13760 else if (mark == AR_SECTION)
13761 gfc_advance_section (section_index, ar, &offset);
13765 if (mark == AR_SECTION)
13767 for (i = 0; i < ar->dimen; i++)
13768 mpz_clear (section_index[i]);
13771 mpz_clear (size);
13772 mpz_clear (offset);
13774 return t;
13778 static bool traverse_data_var (gfc_data_variable *, locus *);
13780 /* Iterate over a list of elements in a DATA statement. */
13782 static bool
13783 traverse_data_list (gfc_data_variable *var, locus *where)
13785 mpz_t trip;
13786 iterator_stack frame;
13787 gfc_expr *e, *start, *end, *step;
13788 bool retval = true;
13790 mpz_init (frame.value);
13791 mpz_init (trip);
13793 start = gfc_copy_expr (var->iter.start);
13794 end = gfc_copy_expr (var->iter.end);
13795 step = gfc_copy_expr (var->iter.step);
13797 if (!gfc_simplify_expr (start, 1)
13798 || start->expr_type != EXPR_CONSTANT)
13800 gfc_error ("start of implied-do loop at %L could not be "
13801 "simplified to a constant value", &start->where);
13802 retval = false;
13803 goto cleanup;
13805 if (!gfc_simplify_expr (end, 1)
13806 || end->expr_type != EXPR_CONSTANT)
13808 gfc_error ("end of implied-do loop at %L could not be "
13809 "simplified to a constant value", &start->where);
13810 retval = false;
13811 goto cleanup;
13813 if (!gfc_simplify_expr (step, 1)
13814 || step->expr_type != EXPR_CONSTANT)
13816 gfc_error ("step of implied-do loop at %L could not be "
13817 "simplified to a constant value", &start->where);
13818 retval = false;
13819 goto cleanup;
13822 mpz_set (trip, end->value.integer);
13823 mpz_sub (trip, trip, start->value.integer);
13824 mpz_add (trip, trip, step->value.integer);
13826 mpz_div (trip, trip, step->value.integer);
13828 mpz_set (frame.value, start->value.integer);
13830 frame.prev = iter_stack;
13831 frame.variable = var->iter.var->symtree;
13832 iter_stack = &frame;
13834 while (mpz_cmp_ui (trip, 0) > 0)
13836 if (!traverse_data_var (var->list, where))
13838 retval = false;
13839 goto cleanup;
13842 e = gfc_copy_expr (var->expr);
13843 if (!gfc_simplify_expr (e, 1))
13845 gfc_free_expr (e);
13846 retval = false;
13847 goto cleanup;
13850 mpz_add (frame.value, frame.value, step->value.integer);
13852 mpz_sub_ui (trip, trip, 1);
13855 cleanup:
13856 mpz_clear (frame.value);
13857 mpz_clear (trip);
13859 gfc_free_expr (start);
13860 gfc_free_expr (end);
13861 gfc_free_expr (step);
13863 iter_stack = frame.prev;
13864 return retval;
13868 /* Type resolve variables in the variable list of a DATA statement. */
13870 static bool
13871 traverse_data_var (gfc_data_variable *var, locus *where)
13873 bool t;
13875 for (; var; var = var->next)
13877 if (var->expr == NULL)
13878 t = traverse_data_list (var, where);
13879 else
13880 t = check_data_variable (var, where);
13882 if (!t)
13883 return false;
13886 return true;
13890 /* Resolve the expressions and iterators associated with a data statement.
13891 This is separate from the assignment checking because data lists should
13892 only be resolved once. */
13894 static bool
13895 resolve_data_variables (gfc_data_variable *d)
13897 for (; d; d = d->next)
13899 if (d->list == NULL)
13901 if (!gfc_resolve_expr (d->expr))
13902 return false;
13904 else
13906 if (!gfc_resolve_iterator (&d->iter, false, true))
13907 return false;
13909 if (!resolve_data_variables (d->list))
13910 return false;
13914 return true;
13918 /* Resolve a single DATA statement. We implement this by storing a pointer to
13919 the value list into static variables, and then recursively traversing the
13920 variables list, expanding iterators and such. */
13922 static void
13923 resolve_data (gfc_data *d)
13926 if (!resolve_data_variables (d->var))
13927 return;
13929 values.vnode = d->value;
13930 if (d->value == NULL)
13931 mpz_set_ui (values.left, 0);
13932 else
13933 mpz_set (values.left, d->value->repeat);
13935 if (!traverse_data_var (d->var, &d->where))
13936 return;
13938 /* At this point, we better not have any values left. */
13940 if (next_data_value ())
13941 gfc_error ("DATA statement at %L has more values than variables",
13942 &d->where);
13946 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13947 accessed by host or use association, is a dummy argument to a pure function,
13948 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13949 is storage associated with any such variable, shall not be used in the
13950 following contexts: (clients of this function). */
13952 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13953 procedure. Returns zero if assignment is OK, nonzero if there is a
13954 problem. */
13956 gfc_impure_variable (gfc_symbol *sym)
13958 gfc_symbol *proc;
13959 gfc_namespace *ns;
13961 if (sym->attr.use_assoc || sym->attr.in_common)
13962 return 1;
13964 /* Check if the symbol's ns is inside the pure procedure. */
13965 for (ns = gfc_current_ns; ns; ns = ns->parent)
13967 if (ns == sym->ns)
13968 break;
13969 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13970 return 1;
13973 proc = sym->ns->proc_name;
13974 if (sym->attr.dummy
13975 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13976 || proc->attr.function))
13977 return 1;
13979 /* TODO: Sort out what can be storage associated, if anything, and include
13980 it here. In principle equivalences should be scanned but it does not
13981 seem to be possible to storage associate an impure variable this way. */
13982 return 0;
13986 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13987 current namespace is inside a pure procedure. */
13990 gfc_pure (gfc_symbol *sym)
13992 symbol_attribute attr;
13993 gfc_namespace *ns;
13995 if (sym == NULL)
13997 /* Check if the current namespace or one of its parents
13998 belongs to a pure procedure. */
13999 for (ns = gfc_current_ns; ns; ns = ns->parent)
14001 sym = ns->proc_name;
14002 if (sym == NULL)
14003 return 0;
14004 attr = sym->attr;
14005 if (attr.flavor == FL_PROCEDURE && attr.pure)
14006 return 1;
14008 return 0;
14011 attr = sym->attr;
14013 return attr.flavor == FL_PROCEDURE && attr.pure;
14017 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14018 checks if the current namespace is implicitly pure. Note that this
14019 function returns false for a PURE procedure. */
14022 gfc_implicit_pure (gfc_symbol *sym)
14024 gfc_namespace *ns;
14026 if (sym == NULL)
14028 /* Check if the current procedure is implicit_pure. Walk up
14029 the procedure list until we find a procedure. */
14030 for (ns = gfc_current_ns; ns; ns = ns->parent)
14032 sym = ns->proc_name;
14033 if (sym == NULL)
14034 return 0;
14036 if (sym->attr.flavor == FL_PROCEDURE)
14037 break;
14041 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14042 && !sym->attr.pure;
14046 void
14047 gfc_unset_implicit_pure (gfc_symbol *sym)
14049 gfc_namespace *ns;
14051 if (sym == NULL)
14053 /* Check if the current procedure is implicit_pure. Walk up
14054 the procedure list until we find a procedure. */
14055 for (ns = gfc_current_ns; ns; ns = ns->parent)
14057 sym = ns->proc_name;
14058 if (sym == NULL)
14059 return;
14061 if (sym->attr.flavor == FL_PROCEDURE)
14062 break;
14066 if (sym->attr.flavor == FL_PROCEDURE)
14067 sym->attr.implicit_pure = 0;
14068 else
14069 sym->attr.pure = 0;
14073 /* Test whether the current procedure is elemental or not. */
14076 gfc_elemental (gfc_symbol *sym)
14078 symbol_attribute attr;
14080 if (sym == NULL)
14081 sym = gfc_current_ns->proc_name;
14082 if (sym == NULL)
14083 return 0;
14084 attr = sym->attr;
14086 return attr.flavor == FL_PROCEDURE && attr.elemental;
14090 /* Warn about unused labels. */
14092 static void
14093 warn_unused_fortran_label (gfc_st_label *label)
14095 if (label == NULL)
14096 return;
14098 warn_unused_fortran_label (label->left);
14100 if (label->defined == ST_LABEL_UNKNOWN)
14101 return;
14103 switch (label->referenced)
14105 case ST_LABEL_UNKNOWN:
14106 gfc_warning ("Label %d at %L defined but not used", label->value,
14107 &label->where);
14108 break;
14110 case ST_LABEL_BAD_TARGET:
14111 gfc_warning ("Label %d at %L defined but cannot be used",
14112 label->value, &label->where);
14113 break;
14115 default:
14116 break;
14119 warn_unused_fortran_label (label->right);
14123 /* Returns the sequence type of a symbol or sequence. */
14125 static seq_type
14126 sequence_type (gfc_typespec ts)
14128 seq_type result;
14129 gfc_component *c;
14131 switch (ts.type)
14133 case BT_DERIVED:
14135 if (ts.u.derived->components == NULL)
14136 return SEQ_NONDEFAULT;
14138 result = sequence_type (ts.u.derived->components->ts);
14139 for (c = ts.u.derived->components->next; c; c = c->next)
14140 if (sequence_type (c->ts) != result)
14141 return SEQ_MIXED;
14143 return result;
14145 case BT_CHARACTER:
14146 if (ts.kind != gfc_default_character_kind)
14147 return SEQ_NONDEFAULT;
14149 return SEQ_CHARACTER;
14151 case BT_INTEGER:
14152 if (ts.kind != gfc_default_integer_kind)
14153 return SEQ_NONDEFAULT;
14155 return SEQ_NUMERIC;
14157 case BT_REAL:
14158 if (!(ts.kind == gfc_default_real_kind
14159 || ts.kind == gfc_default_double_kind))
14160 return SEQ_NONDEFAULT;
14162 return SEQ_NUMERIC;
14164 case BT_COMPLEX:
14165 if (ts.kind != gfc_default_complex_kind)
14166 return SEQ_NONDEFAULT;
14168 return SEQ_NUMERIC;
14170 case BT_LOGICAL:
14171 if (ts.kind != gfc_default_logical_kind)
14172 return SEQ_NONDEFAULT;
14174 return SEQ_NUMERIC;
14176 default:
14177 return SEQ_NONDEFAULT;
14182 /* Resolve derived type EQUIVALENCE object. */
14184 static bool
14185 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14187 gfc_component *c = derived->components;
14189 if (!derived)
14190 return true;
14192 /* Shall not be an object of nonsequence derived type. */
14193 if (!derived->attr.sequence)
14195 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14196 "attribute to be an EQUIVALENCE object", sym->name,
14197 &e->where);
14198 return false;
14201 /* Shall not have allocatable components. */
14202 if (derived->attr.alloc_comp)
14204 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14205 "components to be an EQUIVALENCE object",sym->name,
14206 &e->where);
14207 return false;
14210 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14212 gfc_error ("Derived type variable '%s' at %L with default "
14213 "initialization cannot be in EQUIVALENCE with a variable "
14214 "in COMMON", sym->name, &e->where);
14215 return false;
14218 for (; c ; c = c->next)
14220 if (c->ts.type == BT_DERIVED
14221 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14222 return false;
14224 /* Shall not be an object of sequence derived type containing a pointer
14225 in the structure. */
14226 if (c->attr.pointer)
14228 gfc_error ("Derived type variable '%s' at %L with pointer "
14229 "component(s) cannot be an EQUIVALENCE object",
14230 sym->name, &e->where);
14231 return false;
14234 return true;
14238 /* Resolve equivalence object.
14239 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14240 an allocatable array, an object of nonsequence derived type, an object of
14241 sequence derived type containing a pointer at any level of component
14242 selection, an automatic object, a function name, an entry name, a result
14243 name, a named constant, a structure component, or a subobject of any of
14244 the preceding objects. A substring shall not have length zero. A
14245 derived type shall not have components with default initialization nor
14246 shall two objects of an equivalence group be initialized.
14247 Either all or none of the objects shall have an protected attribute.
14248 The simple constraints are done in symbol.c(check_conflict) and the rest
14249 are implemented here. */
14251 static void
14252 resolve_equivalence (gfc_equiv *eq)
14254 gfc_symbol *sym;
14255 gfc_symbol *first_sym;
14256 gfc_expr *e;
14257 gfc_ref *r;
14258 locus *last_where = NULL;
14259 seq_type eq_type, last_eq_type;
14260 gfc_typespec *last_ts;
14261 int object, cnt_protected;
14262 const char *msg;
14264 last_ts = &eq->expr->symtree->n.sym->ts;
14266 first_sym = eq->expr->symtree->n.sym;
14268 cnt_protected = 0;
14270 for (object = 1; eq; eq = eq->eq, object++)
14272 e = eq->expr;
14274 e->ts = e->symtree->n.sym->ts;
14275 /* match_varspec might not know yet if it is seeing
14276 array reference or substring reference, as it doesn't
14277 know the types. */
14278 if (e->ref && e->ref->type == REF_ARRAY)
14280 gfc_ref *ref = e->ref;
14281 sym = e->symtree->n.sym;
14283 if (sym->attr.dimension)
14285 ref->u.ar.as = sym->as;
14286 ref = ref->next;
14289 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14290 if (e->ts.type == BT_CHARACTER
14291 && ref
14292 && ref->type == REF_ARRAY
14293 && ref->u.ar.dimen == 1
14294 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14295 && ref->u.ar.stride[0] == NULL)
14297 gfc_expr *start = ref->u.ar.start[0];
14298 gfc_expr *end = ref->u.ar.end[0];
14299 void *mem = NULL;
14301 /* Optimize away the (:) reference. */
14302 if (start == NULL && end == NULL)
14304 if (e->ref == ref)
14305 e->ref = ref->next;
14306 else
14307 e->ref->next = ref->next;
14308 mem = ref;
14310 else
14312 ref->type = REF_SUBSTRING;
14313 if (start == NULL)
14314 start = gfc_get_int_expr (gfc_default_integer_kind,
14315 NULL, 1);
14316 ref->u.ss.start = start;
14317 if (end == NULL && e->ts.u.cl)
14318 end = gfc_copy_expr (e->ts.u.cl->length);
14319 ref->u.ss.end = end;
14320 ref->u.ss.length = e->ts.u.cl;
14321 e->ts.u.cl = NULL;
14323 ref = ref->next;
14324 free (mem);
14327 /* Any further ref is an error. */
14328 if (ref)
14330 gcc_assert (ref->type == REF_ARRAY);
14331 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14332 &ref->u.ar.where);
14333 continue;
14337 if (!gfc_resolve_expr (e))
14338 continue;
14340 sym = e->symtree->n.sym;
14342 if (sym->attr.is_protected)
14343 cnt_protected++;
14344 if (cnt_protected > 0 && cnt_protected != object)
14346 gfc_error ("Either all or none of the objects in the "
14347 "EQUIVALENCE set at %L shall have the "
14348 "PROTECTED attribute",
14349 &e->where);
14350 break;
14353 /* Shall not equivalence common block variables in a PURE procedure. */
14354 if (sym->ns->proc_name
14355 && sym->ns->proc_name->attr.pure
14356 && sym->attr.in_common)
14358 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14359 "object in the pure procedure '%s'",
14360 sym->name, &e->where, sym->ns->proc_name->name);
14361 break;
14364 /* Shall not be a named constant. */
14365 if (e->expr_type == EXPR_CONSTANT)
14367 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14368 "object", sym->name, &e->where);
14369 continue;
14372 if (e->ts.type == BT_DERIVED
14373 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14374 continue;
14376 /* Check that the types correspond correctly:
14377 Note 5.28:
14378 A numeric sequence structure may be equivalenced to another sequence
14379 structure, an object of default integer type, default real type, double
14380 precision real type, default logical type such that components of the
14381 structure ultimately only become associated to objects of the same
14382 kind. A character sequence structure may be equivalenced to an object
14383 of default character kind or another character sequence structure.
14384 Other objects may be equivalenced only to objects of the same type and
14385 kind parameters. */
14387 /* Identical types are unconditionally OK. */
14388 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14389 goto identical_types;
14391 last_eq_type = sequence_type (*last_ts);
14392 eq_type = sequence_type (sym->ts);
14394 /* Since the pair of objects is not of the same type, mixed or
14395 non-default sequences can be rejected. */
14397 msg = "Sequence %s with mixed components in EQUIVALENCE "
14398 "statement at %L with different type objects";
14399 if ((object ==2
14400 && last_eq_type == SEQ_MIXED
14401 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14402 || (eq_type == SEQ_MIXED
14403 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14404 continue;
14406 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14407 "statement at %L with objects of different type";
14408 if ((object ==2
14409 && last_eq_type == SEQ_NONDEFAULT
14410 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14411 || (eq_type == SEQ_NONDEFAULT
14412 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14413 continue;
14415 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14416 "EQUIVALENCE statement at %L";
14417 if (last_eq_type == SEQ_CHARACTER
14418 && eq_type != SEQ_CHARACTER
14419 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14420 continue;
14422 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14423 "EQUIVALENCE statement at %L";
14424 if (last_eq_type == SEQ_NUMERIC
14425 && eq_type != SEQ_NUMERIC
14426 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14427 continue;
14429 identical_types:
14430 last_ts =&sym->ts;
14431 last_where = &e->where;
14433 if (!e->ref)
14434 continue;
14436 /* Shall not be an automatic array. */
14437 if (e->ref->type == REF_ARRAY
14438 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14440 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14441 "an EQUIVALENCE object", sym->name, &e->where);
14442 continue;
14445 r = e->ref;
14446 while (r)
14448 /* Shall not be a structure component. */
14449 if (r->type == REF_COMPONENT)
14451 gfc_error ("Structure component '%s' at %L cannot be an "
14452 "EQUIVALENCE object",
14453 r->u.c.component->name, &e->where);
14454 break;
14457 /* A substring shall not have length zero. */
14458 if (r->type == REF_SUBSTRING)
14460 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14462 gfc_error ("Substring at %L has length zero",
14463 &r->u.ss.start->where);
14464 break;
14467 r = r->next;
14473 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14475 static void
14476 resolve_fntype (gfc_namespace *ns)
14478 gfc_entry_list *el;
14479 gfc_symbol *sym;
14481 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14482 return;
14484 /* If there are any entries, ns->proc_name is the entry master
14485 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14486 if (ns->entries)
14487 sym = ns->entries->sym;
14488 else
14489 sym = ns->proc_name;
14490 if (sym->result == sym
14491 && sym->ts.type == BT_UNKNOWN
14492 && !gfc_set_default_type (sym, 0, NULL)
14493 && !sym->attr.untyped)
14495 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14496 sym->name, &sym->declared_at);
14497 sym->attr.untyped = 1;
14500 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14501 && !sym->attr.contained
14502 && !gfc_check_symbol_access (sym->ts.u.derived)
14503 && gfc_check_symbol_access (sym))
14505 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14506 "%L of PRIVATE type '%s'", sym->name,
14507 &sym->declared_at, sym->ts.u.derived->name);
14510 if (ns->entries)
14511 for (el = ns->entries->next; el; el = el->next)
14513 if (el->sym->result == el->sym
14514 && el->sym->ts.type == BT_UNKNOWN
14515 && !gfc_set_default_type (el->sym, 0, NULL)
14516 && !el->sym->attr.untyped)
14518 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14519 el->sym->name, &el->sym->declared_at);
14520 el->sym->attr.untyped = 1;
14526 /* 12.3.2.1.1 Defined operators. */
14528 static bool
14529 check_uop_procedure (gfc_symbol *sym, locus where)
14531 gfc_formal_arglist *formal;
14533 if (!sym->attr.function)
14535 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14536 sym->name, &where);
14537 return false;
14540 if (sym->ts.type == BT_CHARACTER
14541 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14542 && !(sym->result && sym->result->ts.u.cl
14543 && sym->result->ts.u.cl->length))
14545 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14546 "character length", sym->name, &where);
14547 return false;
14550 formal = gfc_sym_get_dummy_args (sym);
14551 if (!formal || !formal->sym)
14553 gfc_error ("User operator procedure '%s' at %L must have at least "
14554 "one argument", sym->name, &where);
14555 return false;
14558 if (formal->sym->attr.intent != INTENT_IN)
14560 gfc_error ("First argument of operator interface at %L must be "
14561 "INTENT(IN)", &where);
14562 return false;
14565 if (formal->sym->attr.optional)
14567 gfc_error ("First argument of operator interface at %L cannot be "
14568 "optional", &where);
14569 return false;
14572 formal = formal->next;
14573 if (!formal || !formal->sym)
14574 return true;
14576 if (formal->sym->attr.intent != INTENT_IN)
14578 gfc_error ("Second argument of operator interface at %L must be "
14579 "INTENT(IN)", &where);
14580 return false;
14583 if (formal->sym->attr.optional)
14585 gfc_error ("Second argument of operator interface at %L cannot be "
14586 "optional", &where);
14587 return false;
14590 if (formal->next)
14592 gfc_error ("Operator interface at %L must have, at most, two "
14593 "arguments", &where);
14594 return false;
14597 return true;
14600 static void
14601 gfc_resolve_uops (gfc_symtree *symtree)
14603 gfc_interface *itr;
14605 if (symtree == NULL)
14606 return;
14608 gfc_resolve_uops (symtree->left);
14609 gfc_resolve_uops (symtree->right);
14611 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14612 check_uop_procedure (itr->sym, itr->sym->declared_at);
14616 /* Examine all of the expressions associated with a program unit,
14617 assign types to all intermediate expressions, make sure that all
14618 assignments are to compatible types and figure out which names
14619 refer to which functions or subroutines. It doesn't check code
14620 block, which is handled by resolve_code. */
14622 static void
14623 resolve_types (gfc_namespace *ns)
14625 gfc_namespace *n;
14626 gfc_charlen *cl;
14627 gfc_data *d;
14628 gfc_equiv *eq;
14629 gfc_namespace* old_ns = gfc_current_ns;
14631 /* Check that all IMPLICIT types are ok. */
14632 if (!ns->seen_implicit_none)
14634 unsigned letter;
14635 for (letter = 0; letter != GFC_LETTERS; ++letter)
14636 if (ns->set_flag[letter]
14637 && !resolve_typespec_used (&ns->default_type[letter],
14638 &ns->implicit_loc[letter], NULL))
14639 return;
14642 gfc_current_ns = ns;
14644 resolve_entries (ns);
14646 resolve_common_vars (ns->blank_common.head, false);
14647 resolve_common_blocks (ns->common_root);
14649 resolve_contained_functions (ns);
14651 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14652 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14653 resolve_formal_arglist (ns->proc_name);
14655 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14657 for (cl = ns->cl_list; cl; cl = cl->next)
14658 resolve_charlen (cl);
14660 gfc_traverse_ns (ns, resolve_symbol);
14662 resolve_fntype (ns);
14664 for (n = ns->contained; n; n = n->sibling)
14666 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14667 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14668 "also be PURE", n->proc_name->name,
14669 &n->proc_name->declared_at);
14671 resolve_types (n);
14674 forall_flag = 0;
14675 gfc_do_concurrent_flag = 0;
14676 gfc_check_interfaces (ns);
14678 gfc_traverse_ns (ns, resolve_values);
14680 if (ns->save_all)
14681 gfc_save_all (ns);
14683 iter_stack = NULL;
14684 for (d = ns->data; d; d = d->next)
14685 resolve_data (d);
14687 iter_stack = NULL;
14688 gfc_traverse_ns (ns, gfc_formalize_init_value);
14690 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14692 for (eq = ns->equiv; eq; eq = eq->next)
14693 resolve_equivalence (eq);
14695 /* Warn about unused labels. */
14696 if (warn_unused_label)
14697 warn_unused_fortran_label (ns->st_labels);
14699 gfc_resolve_uops (ns->uop_root);
14701 gfc_resolve_omp_declare_simd (ns);
14703 gfc_resolve_omp_udrs (ns->omp_udr_root);
14705 gfc_current_ns = old_ns;
14709 /* Call resolve_code recursively. */
14711 static void
14712 resolve_codes (gfc_namespace *ns)
14714 gfc_namespace *n;
14715 bitmap_obstack old_obstack;
14717 if (ns->resolved == 1)
14718 return;
14720 for (n = ns->contained; n; n = n->sibling)
14721 resolve_codes (n);
14723 gfc_current_ns = ns;
14725 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14726 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14727 cs_base = NULL;
14729 /* Set to an out of range value. */
14730 current_entry_id = -1;
14732 old_obstack = labels_obstack;
14733 bitmap_obstack_initialize (&labels_obstack);
14735 resolve_code (ns->code, ns);
14737 bitmap_obstack_release (&labels_obstack);
14738 labels_obstack = old_obstack;
14742 /* This function is called after a complete program unit has been compiled.
14743 Its purpose is to examine all of the expressions associated with a program
14744 unit, assign types to all intermediate expressions, make sure that all
14745 assignments are to compatible types and figure out which names refer to
14746 which functions or subroutines. */
14748 void
14749 gfc_resolve (gfc_namespace *ns)
14751 gfc_namespace *old_ns;
14752 code_stack *old_cs_base;
14754 if (ns->resolved)
14755 return;
14757 ns->resolved = -1;
14758 old_ns = gfc_current_ns;
14759 old_cs_base = cs_base;
14761 resolve_types (ns);
14762 component_assignment_level = 0;
14763 resolve_codes (ns);
14765 gfc_current_ns = old_ns;
14766 cs_base = old_cs_base;
14767 ns->resolved = 1;
14769 gfc_run_passes (ns);