N3323
[official-gcc.git] / gcc / fortran / resolve.c
blob6e1f56f707d1a4c784e8260a7ac3250d1b006101
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
36 typedef enum seq_type
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 seq_type;
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
55 code_stack;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
62 static int forall_flag;
63 static int 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 (sym->attr.if_source != IFSRC_UNKNOWN)
310 resolve_formal_arglist (sym);
312 if (sym->attr.subroutine || sym->attr.external)
314 if (sym->attr.flavor == FL_UNKNOWN)
315 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
317 else
319 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
320 && (!sym->attr.function || sym->result == sym))
321 gfc_set_default_type (sym, 1, sym->ns);
324 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
325 ? CLASS_DATA (sym)->as : sym->as;
327 saved_specification_expr = specification_expr;
328 specification_expr = true;
329 gfc_resolve_array_spec (as, 0);
330 specification_expr = saved_specification_expr;
332 /* We can't tell if an array with dimension (:) is assumed or deferred
333 shape until we know if it has the pointer or allocatable attributes.
335 if (as && as->rank > 0 && as->type == AS_DEFERRED
336 && ((sym->ts.type != BT_CLASS
337 && !(sym->attr.pointer || sym->attr.allocatable))
338 || (sym->ts.type == BT_CLASS
339 && !(CLASS_DATA (sym)->attr.class_pointer
340 || CLASS_DATA (sym)->attr.allocatable)))
341 && sym->attr.flavor != FL_PROCEDURE)
343 as->type = AS_ASSUMED_SHAPE;
344 for (i = 0; i < as->rank; i++)
345 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
348 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
349 || (as && as->type == AS_ASSUMED_RANK)
350 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
351 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
352 && (CLASS_DATA (sym)->attr.class_pointer
353 || CLASS_DATA (sym)->attr.allocatable
354 || CLASS_DATA (sym)->attr.target))
355 || sym->attr.optional)
357 proc->attr.always_explicit = 1;
358 if (proc->result)
359 proc->result->attr.always_explicit = 1;
362 /* If the flavor is unknown at this point, it has to be a variable.
363 A procedure specification would have already set the type. */
365 if (sym->attr.flavor == FL_UNKNOWN)
366 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
368 if (gfc_pure (proc))
370 if (sym->attr.flavor == FL_PROCEDURE)
372 /* F08:C1279. */
373 if (!gfc_pure (sym))
375 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
376 "also be PURE", sym->name, &sym->declared_at);
377 continue;
380 else if (!sym->attr.pointer)
382 if (proc->attr.function && sym->attr.intent != INTENT_IN)
384 if (sym->attr.value)
385 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
386 " of pure function '%s' at %L with VALUE "
387 "attribute but without INTENT(IN)",
388 sym->name, proc->name, &sym->declared_at);
389 else
390 gfc_error ("Argument '%s' of pure function '%s' at %L must "
391 "be INTENT(IN) or VALUE", sym->name, proc->name,
392 &sym->declared_at);
395 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
397 if (sym->attr.value)
398 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
399 " of pure subroutine '%s' at %L with VALUE "
400 "attribute but without INTENT", sym->name,
401 proc->name, &sym->declared_at);
402 else
403 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
404 "must have its INTENT specified or have the "
405 "VALUE attribute", sym->name, proc->name,
406 &sym->declared_at);
411 if (proc->attr.implicit_pure)
413 if (sym->attr.flavor == FL_PROCEDURE)
415 if (!gfc_pure (sym))
416 proc->attr.implicit_pure = 0;
418 else if (!sym->attr.pointer)
420 if (proc->attr.function && sym->attr.intent != INTENT_IN
421 && !sym->value)
422 proc->attr.implicit_pure = 0;
424 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
425 && !sym->value)
426 proc->attr.implicit_pure = 0;
430 if (gfc_elemental (proc))
432 /* F08:C1289. */
433 if (sym->attr.codimension
434 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
435 && CLASS_DATA (sym)->attr.codimension))
437 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
438 "procedure", sym->name, &sym->declared_at);
439 continue;
442 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->as))
445 gfc_error ("Argument '%s' of elemental procedure at %L must "
446 "be scalar", sym->name, &sym->declared_at);
447 continue;
450 if (sym->attr.allocatable
451 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
452 && CLASS_DATA (sym)->attr.allocatable))
454 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
455 "have the ALLOCATABLE attribute", sym->name,
456 &sym->declared_at);
457 continue;
460 if (sym->attr.pointer
461 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
462 && CLASS_DATA (sym)->attr.class_pointer))
464 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
465 "have the POINTER attribute", sym->name,
466 &sym->declared_at);
467 continue;
470 if (sym->attr.flavor == FL_PROCEDURE)
472 gfc_error ("Dummy procedure '%s' not allowed in elemental "
473 "procedure '%s' at %L", sym->name, proc->name,
474 &sym->declared_at);
475 continue;
478 /* Fortran 2008 Corrigendum 1, C1290a. */
479 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
481 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
482 "have its INTENT specified or have the VALUE "
483 "attribute", sym->name, proc->name,
484 &sym->declared_at);
485 continue;
489 /* Each dummy shall be specified to be scalar. */
490 if (proc->attr.proc == PROC_ST_FUNCTION)
492 if (sym->as != NULL)
494 gfc_error ("Argument '%s' of statement function at %L must "
495 "be scalar", sym->name, &sym->declared_at);
496 continue;
499 if (sym->ts.type == BT_CHARACTER)
501 gfc_charlen *cl = sym->ts.u.cl;
502 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
504 gfc_error ("Character-valued argument '%s' of statement "
505 "function at %L must have constant length",
506 sym->name, &sym->declared_at);
507 continue;
512 formal_arg_flag = 0;
516 /* Work function called when searching for symbols that have argument lists
517 associated with them. */
519 static void
520 find_arglists (gfc_symbol *sym)
522 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
523 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
524 return;
526 resolve_formal_arglist (sym);
530 /* Given a namespace, resolve all formal argument lists within the namespace.
533 static void
534 resolve_formal_arglists (gfc_namespace *ns)
536 if (ns == NULL)
537 return;
539 gfc_traverse_ns (ns, find_arglists);
543 static void
544 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
546 bool t;
548 /* If this namespace is not a function or an entry master function,
549 ignore it. */
550 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
551 || sym->attr.entry_master)
552 return;
554 /* Try to find out of what the return type is. */
555 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
557 t = gfc_set_default_type (sym->result, 0, ns);
559 if (!t && !sym->result->attr.untyped)
561 if (sym->result == sym)
562 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
563 sym->name, &sym->declared_at);
564 else if (!sym->result->attr.proc_pointer)
565 gfc_error ("Result '%s' of contained function '%s' at %L has "
566 "no IMPLICIT type", sym->result->name, sym->name,
567 &sym->result->declared_at);
568 sym->result->attr.untyped = 1;
572 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
573 type, lists the only ways a character length value of * can be used:
574 dummy arguments of procedures, named constants, and function results
575 in external functions. Internal function results and results of module
576 procedures are not on this list, ergo, not permitted. */
578 if (sym->result->ts.type == BT_CHARACTER)
580 gfc_charlen *cl = sym->result->ts.u.cl;
581 if ((!cl || !cl->length) && !sym->result->ts.deferred)
583 /* See if this is a module-procedure and adapt error message
584 accordingly. */
585 bool module_proc;
586 gcc_assert (ns->parent && ns->parent->proc_name);
587 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
589 gfc_error ("Character-valued %s '%s' at %L must not be"
590 " assumed length",
591 module_proc ? _("module procedure")
592 : _("internal function"),
593 sym->name, &sym->declared_at);
599 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
600 introduce duplicates. */
602 static void
603 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
605 gfc_formal_arglist *f, *new_arglist;
606 gfc_symbol *new_sym;
608 for (; new_args != NULL; new_args = new_args->next)
610 new_sym = new_args->sym;
611 /* See if this arg is already in the formal argument list. */
612 for (f = proc->formal; f; f = f->next)
614 if (new_sym == f->sym)
615 break;
618 if (f)
619 continue;
621 /* Add a new argument. Argument order is not important. */
622 new_arglist = gfc_get_formal_arglist ();
623 new_arglist->sym = new_sym;
624 new_arglist->next = proc->formal;
625 proc->formal = new_arglist;
630 /* Flag the arguments that are not present in all entries. */
632 static void
633 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
635 gfc_formal_arglist *f, *head;
636 head = new_args;
638 for (f = proc->formal; f; f = f->next)
640 if (f->sym == NULL)
641 continue;
643 for (new_args = head; new_args; new_args = new_args->next)
645 if (new_args->sym == f->sym)
646 break;
649 if (new_args)
650 continue;
652 f->sym->attr.not_always_present = 1;
657 /* Resolve alternate entry points. If a symbol has multiple entry points we
658 create a new master symbol for the main routine, and turn the existing
659 symbol into an entry point. */
661 static void
662 resolve_entries (gfc_namespace *ns)
664 gfc_namespace *old_ns;
665 gfc_code *c;
666 gfc_symbol *proc;
667 gfc_entry_list *el;
668 char name[GFC_MAX_SYMBOL_LEN + 1];
669 static int master_count = 0;
671 if (ns->proc_name == NULL)
672 return;
674 /* No need to do anything if this procedure doesn't have alternate entry
675 points. */
676 if (!ns->entries)
677 return;
679 /* We may already have resolved alternate entry points. */
680 if (ns->proc_name->attr.entry_master)
681 return;
683 /* If this isn't a procedure something has gone horribly wrong. */
684 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
686 /* Remember the current namespace. */
687 old_ns = gfc_current_ns;
689 gfc_current_ns = ns;
691 /* Add the main entry point to the list of entry points. */
692 el = gfc_get_entry_list ();
693 el->sym = ns->proc_name;
694 el->id = 0;
695 el->next = ns->entries;
696 ns->entries = el;
697 ns->proc_name->attr.entry = 1;
699 /* If it is a module function, it needs to be in the right namespace
700 so that gfc_get_fake_result_decl can gather up the results. The
701 need for this arose in get_proc_name, where these beasts were
702 left in their own namespace, to keep prior references linked to
703 the entry declaration.*/
704 if (ns->proc_name->attr.function
705 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
706 el->sym->ns = ns;
708 /* Do the same for entries where the master is not a module
709 procedure. These are retained in the module namespace because
710 of the module procedure declaration. */
711 for (el = el->next; el; el = el->next)
712 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
713 && el->sym->attr.mod_proc)
714 el->sym->ns = ns;
715 el = ns->entries;
717 /* Add an entry statement for it. */
718 c = gfc_get_code ();
719 c->op = EXEC_ENTRY;
720 c->ext.entry = el;
721 c->next = ns->code;
722 ns->code = c;
724 /* Create a new symbol for the master function. */
725 /* Give the internal function a unique name (within this file).
726 Also include the function name so the user has some hope of figuring
727 out what is going on. */
728 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
729 master_count++, ns->proc_name->name);
730 gfc_get_ha_symbol (name, &proc);
731 gcc_assert (proc != NULL);
733 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
734 if (ns->proc_name->attr.subroutine)
735 gfc_add_subroutine (&proc->attr, proc->name, NULL);
736 else
738 gfc_symbol *sym;
739 gfc_typespec *ts, *fts;
740 gfc_array_spec *as, *fas;
741 gfc_add_function (&proc->attr, proc->name, NULL);
742 proc->result = proc;
743 fas = ns->entries->sym->as;
744 fas = fas ? fas : ns->entries->sym->result->as;
745 fts = &ns->entries->sym->result->ts;
746 if (fts->type == BT_UNKNOWN)
747 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
748 for (el = ns->entries->next; el; el = el->next)
750 ts = &el->sym->result->ts;
751 as = el->sym->as;
752 as = as ? as : el->sym->result->as;
753 if (ts->type == BT_UNKNOWN)
754 ts = gfc_get_default_type (el->sym->result->name, NULL);
756 if (! gfc_compare_types (ts, fts)
757 || (el->sym->result->attr.dimension
758 != ns->entries->sym->result->attr.dimension)
759 || (el->sym->result->attr.pointer
760 != ns->entries->sym->result->attr.pointer))
761 break;
762 else if (as && fas && ns->entries->sym->result != el->sym->result
763 && gfc_compare_array_spec (as, fas) == 0)
764 gfc_error ("Function %s at %L has entries with mismatched "
765 "array specifications", ns->entries->sym->name,
766 &ns->entries->sym->declared_at);
767 /* The characteristics need to match and thus both need to have
768 the same string length, i.e. both len=*, or both len=4.
769 Having both len=<variable> is also possible, but difficult to
770 check at compile time. */
771 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
772 && (((ts->u.cl->length && !fts->u.cl->length)
773 ||(!ts->u.cl->length && fts->u.cl->length))
774 || (ts->u.cl->length
775 && ts->u.cl->length->expr_type
776 != fts->u.cl->length->expr_type)
777 || (ts->u.cl->length
778 && ts->u.cl->length->expr_type == EXPR_CONSTANT
779 && mpz_cmp (ts->u.cl->length->value.integer,
780 fts->u.cl->length->value.integer) != 0)))
781 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
782 "entries returning variables of different "
783 "string lengths", ns->entries->sym->name,
784 &ns->entries->sym->declared_at);
787 if (el == NULL)
789 sym = ns->entries->sym->result;
790 /* All result types the same. */
791 proc->ts = *fts;
792 if (sym->attr.dimension)
793 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
794 if (sym->attr.pointer)
795 gfc_add_pointer (&proc->attr, NULL);
797 else
799 /* Otherwise the result will be passed through a union by
800 reference. */
801 proc->attr.mixed_entry_master = 1;
802 for (el = ns->entries; el; el = el->next)
804 sym = el->sym->result;
805 if (sym->attr.dimension)
807 if (el == ns->entries)
808 gfc_error ("FUNCTION result %s can't be an array in "
809 "FUNCTION %s at %L", sym->name,
810 ns->entries->sym->name, &sym->declared_at);
811 else
812 gfc_error ("ENTRY result %s can't be an array in "
813 "FUNCTION %s at %L", sym->name,
814 ns->entries->sym->name, &sym->declared_at);
816 else if (sym->attr.pointer)
818 if (el == ns->entries)
819 gfc_error ("FUNCTION result %s can't be a POINTER in "
820 "FUNCTION %s at %L", sym->name,
821 ns->entries->sym->name, &sym->declared_at);
822 else
823 gfc_error ("ENTRY result %s can't be a POINTER in "
824 "FUNCTION %s at %L", sym->name,
825 ns->entries->sym->name, &sym->declared_at);
827 else
829 ts = &sym->ts;
830 if (ts->type == BT_UNKNOWN)
831 ts = gfc_get_default_type (sym->name, NULL);
832 switch (ts->type)
834 case BT_INTEGER:
835 if (ts->kind == gfc_default_integer_kind)
836 sym = NULL;
837 break;
838 case BT_REAL:
839 if (ts->kind == gfc_default_real_kind
840 || ts->kind == gfc_default_double_kind)
841 sym = NULL;
842 break;
843 case BT_COMPLEX:
844 if (ts->kind == gfc_default_complex_kind)
845 sym = NULL;
846 break;
847 case BT_LOGICAL:
848 if (ts->kind == gfc_default_logical_kind)
849 sym = NULL;
850 break;
851 case BT_UNKNOWN:
852 /* We will issue error elsewhere. */
853 sym = NULL;
854 break;
855 default:
856 break;
858 if (sym)
860 if (el == ns->entries)
861 gfc_error ("FUNCTION result %s can't be of type %s "
862 "in FUNCTION %s at %L", sym->name,
863 gfc_typename (ts), ns->entries->sym->name,
864 &sym->declared_at);
865 else
866 gfc_error ("ENTRY result %s can't be of type %s "
867 "in FUNCTION %s at %L", sym->name,
868 gfc_typename (ts), ns->entries->sym->name,
869 &sym->declared_at);
875 proc->attr.access = ACCESS_PRIVATE;
876 proc->attr.entry_master = 1;
878 /* Merge all the entry point arguments. */
879 for (el = ns->entries; el; el = el->next)
880 merge_argument_lists (proc, el->sym->formal);
882 /* Check the master formal arguments for any that are not
883 present in all entry points. */
884 for (el = ns->entries; el; el = el->next)
885 check_argument_lists (proc, el->sym->formal);
887 /* Use the master function for the function body. */
888 ns->proc_name = proc;
890 /* Finalize the new symbols. */
891 gfc_commit_symbols ();
893 /* Restore the original namespace. */
894 gfc_current_ns = old_ns;
898 /* Resolve common variables. */
899 static void
900 resolve_common_vars (gfc_symbol *sym, bool named_common)
902 gfc_symbol *csym = sym;
904 for (; csym; csym = csym->common_next)
906 if (csym->value || csym->attr.data)
908 if (!csym->ns->is_block_data)
909 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
910 "but only in BLOCK DATA initialization is "
911 "allowed", csym->name, &csym->declared_at);
912 else if (!named_common)
913 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
914 "in a blank COMMON but initialization is only "
915 "allowed in named common blocks", csym->name,
916 &csym->declared_at);
919 if (UNLIMITED_POLY (csym))
920 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
921 "[F2008:C5100]", csym->name, &csym->declared_at);
923 if (csym->ts.type != BT_DERIVED)
924 continue;
926 if (!(csym->ts.u.derived->attr.sequence
927 || csym->ts.u.derived->attr.is_bind_c))
928 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
929 "has neither the SEQUENCE nor the BIND(C) "
930 "attribute", csym->name, &csym->declared_at);
931 if (csym->ts.u.derived->attr.alloc_comp)
932 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
933 "has an ultimate component that is "
934 "allocatable", csym->name, &csym->declared_at);
935 if (gfc_has_default_initializer (csym->ts.u.derived))
936 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
937 "may not have default initializer", csym->name,
938 &csym->declared_at);
940 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
941 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
945 /* Resolve common blocks. */
946 static void
947 resolve_common_blocks (gfc_symtree *common_root)
949 gfc_symbol *sym;
951 if (common_root == NULL)
952 return;
954 if (common_root->left)
955 resolve_common_blocks (common_root->left);
956 if (common_root->right)
957 resolve_common_blocks (common_root->right);
959 resolve_common_vars (common_root->n.common->head, true);
961 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
962 if (sym == NULL)
963 return;
965 if (sym->attr.flavor == FL_PARAMETER)
966 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
967 sym->name, &common_root->n.common->where, &sym->declared_at);
969 if (sym->attr.external)
970 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
971 sym->name, &common_root->n.common->where);
973 if (sym->attr.intrinsic)
974 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
975 sym->name, &common_root->n.common->where);
976 else if (sym->attr.result
977 || gfc_is_function_return_value (sym, gfc_current_ns))
978 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
979 "that is also a function result", sym->name,
980 &common_root->n.common->where);
981 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
982 && sym->attr.proc != PROC_ST_FUNCTION)
983 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
984 "that is also a global procedure", sym->name,
985 &common_root->n.common->where);
989 /* Resolve contained function types. Because contained functions can call one
990 another, they have to be worked out before any of the contained procedures
991 can be resolved.
993 The good news is that if a function doesn't already have a type, the only
994 way it can get one is through an IMPLICIT type or a RESULT variable, because
995 by definition contained functions are contained namespace they're contained
996 in, not in a sibling or parent namespace. */
998 static void
999 resolve_contained_functions (gfc_namespace *ns)
1001 gfc_namespace *child;
1002 gfc_entry_list *el;
1004 resolve_formal_arglists (ns);
1006 for (child = ns->contained; child; child = child->sibling)
1008 /* Resolve alternate entry points first. */
1009 resolve_entries (child);
1011 /* Then check function return types. */
1012 resolve_contained_fntype (child->proc_name, child);
1013 for (el = child->entries; el; el = el->next)
1014 resolve_contained_fntype (el->sym, child);
1019 static bool resolve_fl_derived0 (gfc_symbol *sym);
1022 /* Resolve all of the elements of a structure constructor and make sure that
1023 the types are correct. The 'init' flag indicates that the given
1024 constructor is an initializer. */
1026 static bool
1027 resolve_structure_cons (gfc_expr *expr, int init)
1029 gfc_constructor *cons;
1030 gfc_component *comp;
1031 bool t;
1032 symbol_attribute a;
1034 t = true;
1036 if (expr->ts.type == BT_DERIVED)
1037 resolve_fl_derived0 (expr->ts.u.derived);
1039 cons = gfc_constructor_first (expr->value.constructor);
1041 /* A constructor may have references if it is the result of substituting a
1042 parameter variable. In this case we just pull out the component we
1043 want. */
1044 if (expr->ref)
1045 comp = expr->ref->u.c.sym->components;
1046 else
1047 comp = expr->ts.u.derived->components;
1049 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1051 int rank;
1053 if (!cons->expr)
1054 continue;
1056 if (!gfc_resolve_expr (cons->expr))
1058 t = false;
1059 continue;
1062 rank = comp->as ? comp->as->rank : 0;
1063 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1064 && (comp->attr.allocatable || cons->expr->rank))
1066 gfc_error ("The rank of the element in the structure "
1067 "constructor at %L does not match that of the "
1068 "component (%d/%d)", &cons->expr->where,
1069 cons->expr->rank, rank);
1070 t = false;
1073 /* If we don't have the right type, try to convert it. */
1075 if (!comp->attr.proc_pointer &&
1076 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1078 if (strcmp (comp->name, "_extends") == 0)
1080 /* Can afford to be brutal with the _extends initializer.
1081 The derived type can get lost because it is PRIVATE
1082 but it is not usage constrained by the standard. */
1083 cons->expr->ts = comp->ts;
1085 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1087 gfc_error ("The element in the structure constructor at %L, "
1088 "for pointer component '%s', is %s but should be %s",
1089 &cons->expr->where, comp->name,
1090 gfc_basic_typename (cons->expr->ts.type),
1091 gfc_basic_typename (comp->ts.type));
1092 t = false;
1094 else
1096 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1097 if (t)
1098 t = t2;
1102 /* For strings, the length of the constructor should be the same as
1103 the one of the structure, ensure this if the lengths are known at
1104 compile time and when we are dealing with PARAMETER or structure
1105 constructors. */
1106 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1107 && comp->ts.u.cl->length
1108 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1109 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1110 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1111 && cons->expr->rank != 0
1112 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1113 comp->ts.u.cl->length->value.integer) != 0)
1115 if (cons->expr->expr_type == EXPR_VARIABLE
1116 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1118 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1119 to make use of the gfc_resolve_character_array_constructor
1120 machinery. The expression is later simplified away to
1121 an array of string literals. */
1122 gfc_expr *para = cons->expr;
1123 cons->expr = gfc_get_expr ();
1124 cons->expr->ts = para->ts;
1125 cons->expr->where = para->where;
1126 cons->expr->expr_type = EXPR_ARRAY;
1127 cons->expr->rank = para->rank;
1128 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1129 gfc_constructor_append_expr (&cons->expr->value.constructor,
1130 para, &cons->expr->where);
1132 if (cons->expr->expr_type == EXPR_ARRAY)
1134 gfc_constructor *p;
1135 p = gfc_constructor_first (cons->expr->value.constructor);
1136 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1138 gfc_charlen *cl, *cl2;
1140 cl2 = NULL;
1141 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1143 if (cl == cons->expr->ts.u.cl)
1144 break;
1145 cl2 = cl;
1148 gcc_assert (cl);
1150 if (cl2)
1151 cl2->next = cl->next;
1153 gfc_free_expr (cl->length);
1154 free (cl);
1157 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1158 cons->expr->ts.u.cl->length_from_typespec = true;
1159 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1160 gfc_resolve_character_array_constructor (cons->expr);
1164 if (cons->expr->expr_type == EXPR_NULL
1165 && !(comp->attr.pointer || comp->attr.allocatable
1166 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1167 || (comp->ts.type == BT_CLASS
1168 && (CLASS_DATA (comp)->attr.class_pointer
1169 || CLASS_DATA (comp)->attr.allocatable))))
1171 t = false;
1172 gfc_error ("The NULL in the structure constructor at %L is "
1173 "being applied to component '%s', which is neither "
1174 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1175 comp->name);
1178 if (comp->attr.proc_pointer && comp->ts.interface)
1180 /* Check procedure pointer interface. */
1181 gfc_symbol *s2 = NULL;
1182 gfc_component *c2;
1183 const char *name;
1184 char err[200];
1186 c2 = gfc_get_proc_ptr_comp (cons->expr);
1187 if (c2)
1189 s2 = c2->ts.interface;
1190 name = c2->name;
1192 else if (cons->expr->expr_type == EXPR_FUNCTION)
1194 s2 = cons->expr->symtree->n.sym->result;
1195 name = cons->expr->symtree->n.sym->result->name;
1197 else if (cons->expr->expr_type != EXPR_NULL)
1199 s2 = cons->expr->symtree->n.sym;
1200 name = cons->expr->symtree->n.sym->name;
1203 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1204 err, sizeof (err), NULL, NULL))
1206 gfc_error ("Interface mismatch for procedure-pointer component "
1207 "'%s' in structure constructor at %L: %s",
1208 comp->name, &cons->expr->where, err);
1209 return false;
1213 if (!comp->attr.pointer || comp->attr.proc_pointer
1214 || cons->expr->expr_type == EXPR_NULL)
1215 continue;
1217 a = gfc_expr_attr (cons->expr);
1219 if (!a.pointer && !a.target)
1221 t = false;
1222 gfc_error ("The element in the structure constructor at %L, "
1223 "for pointer component '%s' should be a POINTER or "
1224 "a TARGET", &cons->expr->where, comp->name);
1227 if (init)
1229 /* F08:C461. Additional checks for pointer initialization. */
1230 if (a.allocatable)
1232 t = false;
1233 gfc_error ("Pointer initialization target at %L "
1234 "must not be ALLOCATABLE ", &cons->expr->where);
1236 if (!a.save)
1238 t = false;
1239 gfc_error ("Pointer initialization target at %L "
1240 "must have the SAVE attribute", &cons->expr->where);
1244 /* F2003, C1272 (3). */
1245 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1246 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1247 || gfc_is_coindexed (cons->expr)))
1249 t = false;
1250 gfc_error ("Invalid expression in the structure constructor for "
1251 "pointer component '%s' at %L in PURE procedure",
1252 comp->name, &cons->expr->where);
1255 if (gfc_implicit_pure (NULL)
1256 && cons->expr->expr_type == EXPR_VARIABLE
1257 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1258 || gfc_is_coindexed (cons->expr)))
1259 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1263 return t;
1267 /****************** Expression name resolution ******************/
1269 /* Returns 0 if a symbol was not declared with a type or
1270 attribute declaration statement, nonzero otherwise. */
1272 static int
1273 was_declared (gfc_symbol *sym)
1275 symbol_attribute a;
1277 a = sym->attr;
1279 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1280 return 1;
1282 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1283 || a.optional || a.pointer || a.save || a.target || a.volatile_
1284 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1285 || a.asynchronous || a.codimension)
1286 return 1;
1288 return 0;
1292 /* Determine if a symbol is generic or not. */
1294 static int
1295 generic_sym (gfc_symbol *sym)
1297 gfc_symbol *s;
1299 if (sym->attr.generic ||
1300 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1301 return 1;
1303 if (was_declared (sym) || sym->ns->parent == NULL)
1304 return 0;
1306 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1308 if (s != NULL)
1310 if (s == sym)
1311 return 0;
1312 else
1313 return generic_sym (s);
1316 return 0;
1320 /* Determine if a symbol is specific or not. */
1322 static int
1323 specific_sym (gfc_symbol *sym)
1325 gfc_symbol *s;
1327 if (sym->attr.if_source == IFSRC_IFBODY
1328 || sym->attr.proc == PROC_MODULE
1329 || sym->attr.proc == PROC_INTERNAL
1330 || sym->attr.proc == PROC_ST_FUNCTION
1331 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1332 || sym->attr.external)
1333 return 1;
1335 if (was_declared (sym) || sym->ns->parent == NULL)
1336 return 0;
1338 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1340 return (s == NULL) ? 0 : specific_sym (s);
1344 /* Figure out if the procedure is specific, generic or unknown. */
1346 typedef enum
1347 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1348 proc_type;
1350 static proc_type
1351 procedure_kind (gfc_symbol *sym)
1353 if (generic_sym (sym))
1354 return PTYPE_GENERIC;
1356 if (specific_sym (sym))
1357 return PTYPE_SPECIFIC;
1359 return PTYPE_UNKNOWN;
1362 /* Check references to assumed size arrays. The flag need_full_assumed_size
1363 is nonzero when matching actual arguments. */
1365 static int need_full_assumed_size = 0;
1367 static bool
1368 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1370 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1371 return false;
1373 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1374 What should it be? */
1375 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1376 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1377 && (e->ref->u.ar.type == AR_FULL))
1379 gfc_error ("The upper bound in the last dimension must "
1380 "appear in the reference to the assumed size "
1381 "array '%s' at %L", sym->name, &e->where);
1382 return true;
1384 return false;
1388 /* Look for bad assumed size array references in argument expressions
1389 of elemental and array valued intrinsic procedures. Since this is
1390 called from procedure resolution functions, it only recurses at
1391 operators. */
1393 static bool
1394 resolve_assumed_size_actual (gfc_expr *e)
1396 if (e == NULL)
1397 return false;
1399 switch (e->expr_type)
1401 case EXPR_VARIABLE:
1402 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1403 return true;
1404 break;
1406 case EXPR_OP:
1407 if (resolve_assumed_size_actual (e->value.op.op1)
1408 || resolve_assumed_size_actual (e->value.op.op2))
1409 return true;
1410 break;
1412 default:
1413 break;
1415 return false;
1419 /* Check a generic procedure, passed as an actual argument, to see if
1420 there is a matching specific name. If none, it is an error, and if
1421 more than one, the reference is ambiguous. */
1422 static int
1423 count_specific_procs (gfc_expr *e)
1425 int n;
1426 gfc_interface *p;
1427 gfc_symbol *sym;
1429 n = 0;
1430 sym = e->symtree->n.sym;
1432 for (p = sym->generic; p; p = p->next)
1433 if (strcmp (sym->name, p->sym->name) == 0)
1435 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1436 sym->name);
1437 n++;
1440 if (n > 1)
1441 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1442 &e->where);
1444 if (n == 0)
1445 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1446 "argument at %L", sym->name, &e->where);
1448 return n;
1452 /* See if a call to sym could possibly be a not allowed RECURSION because of
1453 a missing RECURSIVE declaration. This means that either sym is the current
1454 context itself, or sym is the parent of a contained procedure calling its
1455 non-RECURSIVE containing procedure.
1456 This also works if sym is an ENTRY. */
1458 static bool
1459 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1461 gfc_symbol* proc_sym;
1462 gfc_symbol* context_proc;
1463 gfc_namespace* real_context;
1465 if (sym->attr.flavor == FL_PROGRAM
1466 || sym->attr.flavor == FL_DERIVED)
1467 return false;
1469 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1471 /* If we've got an ENTRY, find real procedure. */
1472 if (sym->attr.entry && sym->ns->entries)
1473 proc_sym = sym->ns->entries->sym;
1474 else
1475 proc_sym = sym;
1477 /* If sym is RECURSIVE, all is well of course. */
1478 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1479 return false;
1481 /* Find the context procedure's "real" symbol if it has entries.
1482 We look for a procedure symbol, so recurse on the parents if we don't
1483 find one (like in case of a BLOCK construct). */
1484 for (real_context = context; ; real_context = real_context->parent)
1486 /* We should find something, eventually! */
1487 gcc_assert (real_context);
1489 context_proc = (real_context->entries ? real_context->entries->sym
1490 : real_context->proc_name);
1492 /* In some special cases, there may not be a proc_name, like for this
1493 invalid code:
1494 real(bad_kind()) function foo () ...
1495 when checking the call to bad_kind ().
1496 In these cases, we simply return here and assume that the
1497 call is ok. */
1498 if (!context_proc)
1499 return false;
1501 if (context_proc->attr.flavor != FL_LABEL)
1502 break;
1505 /* A call from sym's body to itself is recursion, of course. */
1506 if (context_proc == proc_sym)
1507 return true;
1509 /* The same is true if context is a contained procedure and sym the
1510 containing one. */
1511 if (context_proc->attr.contained)
1513 gfc_symbol* parent_proc;
1515 gcc_assert (context->parent);
1516 parent_proc = (context->parent->entries ? context->parent->entries->sym
1517 : context->parent->proc_name);
1519 if (parent_proc == proc_sym)
1520 return true;
1523 return false;
1527 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1528 its typespec and formal argument list. */
1530 bool
1531 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1533 gfc_intrinsic_sym* isym = NULL;
1534 const char* symstd;
1536 if (sym->formal)
1537 return true;
1539 /* Already resolved. */
1540 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1541 return true;
1543 /* We already know this one is an intrinsic, so we don't call
1544 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1545 gfc_find_subroutine directly to check whether it is a function or
1546 subroutine. */
1548 if (sym->intmod_sym_id && sym->attr.subroutine)
1550 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1551 isym = gfc_intrinsic_subroutine_by_id (id);
1553 else if (sym->intmod_sym_id)
1555 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1556 isym = gfc_intrinsic_function_by_id (id);
1558 else if (!sym->attr.subroutine)
1559 isym = gfc_find_function (sym->name);
1561 if (isym && !sym->attr.subroutine)
1563 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1564 && !sym->attr.implicit_type)
1565 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1566 " ignored", sym->name, &sym->declared_at);
1568 if (!sym->attr.function &&
1569 !gfc_add_function(&sym->attr, sym->name, loc))
1570 return false;
1572 sym->ts = isym->ts;
1574 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1576 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1578 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1579 " specifier", sym->name, &sym->declared_at);
1580 return false;
1583 if (!sym->attr.subroutine &&
1584 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1585 return false;
1587 else
1589 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1590 &sym->declared_at);
1591 return false;
1594 gfc_copy_formal_args_intr (sym, isym);
1596 /* Check it is actually available in the standard settings. */
1597 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1599 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1600 " available in the current standard settings but %s. Use"
1601 " an appropriate -std=* option or enable -fall-intrinsics"
1602 " in order to use it.",
1603 sym->name, &sym->declared_at, symstd);
1604 return false;
1607 return true;
1611 /* Resolve a procedure expression, like passing it to a called procedure or as
1612 RHS for a procedure pointer assignment. */
1614 static bool
1615 resolve_procedure_expression (gfc_expr* expr)
1617 gfc_symbol* sym;
1619 if (expr->expr_type != EXPR_VARIABLE)
1620 return true;
1621 gcc_assert (expr->symtree);
1623 sym = expr->symtree->n.sym;
1625 if (sym->attr.intrinsic)
1626 gfc_resolve_intrinsic (sym, &expr->where);
1628 if (sym->attr.flavor != FL_PROCEDURE
1629 || (sym->attr.function && sym->result == sym))
1630 return true;
1632 /* A non-RECURSIVE procedure that is used as procedure expression within its
1633 own body is in danger of being called recursively. */
1634 if (is_illegal_recursion (sym, gfc_current_ns))
1635 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1636 " itself recursively. Declare it RECURSIVE or use"
1637 " -frecursive", sym->name, &expr->where);
1639 return true;
1643 /* Resolve an actual argument list. Most of the time, this is just
1644 resolving the expressions in the list.
1645 The exception is that we sometimes have to decide whether arguments
1646 that look like procedure arguments are really simple variable
1647 references. */
1649 static bool
1650 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1651 bool no_formal_args)
1653 gfc_symbol *sym;
1654 gfc_symtree *parent_st;
1655 gfc_expr *e;
1656 int save_need_full_assumed_size;
1657 bool return_value = false;
1658 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1660 actual_arg = true;
1661 first_actual_arg = true;
1663 for (; arg; arg = arg->next)
1665 e = arg->expr;
1666 if (e == NULL)
1668 /* Check the label is a valid branching target. */
1669 if (arg->label)
1671 if (arg->label->defined == ST_LABEL_UNKNOWN)
1673 gfc_error ("Label %d referenced at %L is never defined",
1674 arg->label->value, &arg->label->where);
1675 goto cleanup;
1678 first_actual_arg = false;
1679 continue;
1682 if (e->expr_type == EXPR_VARIABLE
1683 && e->symtree->n.sym->attr.generic
1684 && no_formal_args
1685 && count_specific_procs (e) != 1)
1686 goto cleanup;
1688 if (e->ts.type != BT_PROCEDURE)
1690 save_need_full_assumed_size = need_full_assumed_size;
1691 if (e->expr_type != EXPR_VARIABLE)
1692 need_full_assumed_size = 0;
1693 if (!gfc_resolve_expr (e))
1694 goto cleanup;
1695 need_full_assumed_size = save_need_full_assumed_size;
1696 goto argument_list;
1699 /* See if the expression node should really be a variable reference. */
1701 sym = e->symtree->n.sym;
1703 if (sym->attr.flavor == FL_PROCEDURE
1704 || sym->attr.intrinsic
1705 || sym->attr.external)
1707 int actual_ok;
1709 /* If a procedure is not already determined to be something else
1710 check if it is intrinsic. */
1711 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1712 sym->attr.intrinsic = 1;
1714 if (sym->attr.proc == PROC_ST_FUNCTION)
1716 gfc_error ("Statement function '%s' at %L is not allowed as an "
1717 "actual argument", sym->name, &e->where);
1720 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1721 sym->attr.subroutine);
1722 if (sym->attr.intrinsic && actual_ok == 0)
1724 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1725 "actual argument", sym->name, &e->where);
1728 if (sym->attr.contained && !sym->attr.use_assoc
1729 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1731 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1732 " used as actual argument at %L",
1733 sym->name, &e->where))
1734 goto cleanup;
1737 if (sym->attr.elemental && !sym->attr.intrinsic)
1739 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1740 "allowed as an actual argument at %L", sym->name,
1741 &e->where);
1744 /* Check if a generic interface has a specific procedure
1745 with the same name before emitting an error. */
1746 if (sym->attr.generic && count_specific_procs (e) != 1)
1747 goto cleanup;
1749 /* Just in case a specific was found for the expression. */
1750 sym = e->symtree->n.sym;
1752 /* If the symbol is the function that names the current (or
1753 parent) scope, then we really have a variable reference. */
1755 if (gfc_is_function_return_value (sym, sym->ns))
1756 goto got_variable;
1758 /* If all else fails, see if we have a specific intrinsic. */
1759 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1761 gfc_intrinsic_sym *isym;
1763 isym = gfc_find_function (sym->name);
1764 if (isym == NULL || !isym->specific)
1766 gfc_error ("Unable to find a specific INTRINSIC procedure "
1767 "for the reference '%s' at %L", sym->name,
1768 &e->where);
1769 goto cleanup;
1771 sym->ts = isym->ts;
1772 sym->attr.intrinsic = 1;
1773 sym->attr.function = 1;
1776 if (!gfc_resolve_expr (e))
1777 goto cleanup;
1778 goto argument_list;
1781 /* See if the name is a module procedure in a parent unit. */
1783 if (was_declared (sym) || sym->ns->parent == NULL)
1784 goto got_variable;
1786 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1788 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1789 goto cleanup;
1792 if (parent_st == NULL)
1793 goto got_variable;
1795 sym = parent_st->n.sym;
1796 e->symtree = parent_st; /* Point to the right thing. */
1798 if (sym->attr.flavor == FL_PROCEDURE
1799 || sym->attr.intrinsic
1800 || sym->attr.external)
1802 if (!gfc_resolve_expr (e))
1803 goto cleanup;
1804 goto argument_list;
1807 got_variable:
1808 e->expr_type = EXPR_VARIABLE;
1809 e->ts = sym->ts;
1810 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1811 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1812 && CLASS_DATA (sym)->as))
1814 e->rank = sym->ts.type == BT_CLASS
1815 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1816 e->ref = gfc_get_ref ();
1817 e->ref->type = REF_ARRAY;
1818 e->ref->u.ar.type = AR_FULL;
1819 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1820 ? CLASS_DATA (sym)->as : sym->as;
1823 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1824 primary.c (match_actual_arg). If above code determines that it
1825 is a variable instead, it needs to be resolved as it was not
1826 done at the beginning of this function. */
1827 save_need_full_assumed_size = need_full_assumed_size;
1828 if (e->expr_type != EXPR_VARIABLE)
1829 need_full_assumed_size = 0;
1830 if (!gfc_resolve_expr (e))
1831 goto cleanup;
1832 need_full_assumed_size = save_need_full_assumed_size;
1834 argument_list:
1835 /* Check argument list functions %VAL, %LOC and %REF. There is
1836 nothing to do for %REF. */
1837 if (arg->name && arg->name[0] == '%')
1839 if (strncmp ("%VAL", arg->name, 4) == 0)
1841 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1843 gfc_error ("By-value argument at %L is not of numeric "
1844 "type", &e->where);
1845 goto cleanup;
1848 if (e->rank)
1850 gfc_error ("By-value argument at %L cannot be an array or "
1851 "an array section", &e->where);
1852 goto cleanup;
1855 /* Intrinsics are still PROC_UNKNOWN here. However,
1856 since same file external procedures are not resolvable
1857 in gfortran, it is a good deal easier to leave them to
1858 intrinsic.c. */
1859 if (ptype != PROC_UNKNOWN
1860 && ptype != PROC_DUMMY
1861 && ptype != PROC_EXTERNAL
1862 && ptype != PROC_MODULE)
1864 gfc_error ("By-value argument at %L is not allowed "
1865 "in this context", &e->where);
1866 goto cleanup;
1870 /* Statement functions have already been excluded above. */
1871 else if (strncmp ("%LOC", arg->name, 4) == 0
1872 && e->ts.type == BT_PROCEDURE)
1874 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1876 gfc_error ("Passing internal procedure at %L by location "
1877 "not allowed", &e->where);
1878 goto cleanup;
1883 /* Fortran 2008, C1237. */
1884 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1885 && gfc_has_ultimate_pointer (e))
1887 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1888 "component", &e->where);
1889 goto cleanup;
1892 first_actual_arg = false;
1895 return_value = true;
1897 cleanup:
1898 actual_arg = actual_arg_sav;
1899 first_actual_arg = first_actual_arg_sav;
1901 return return_value;
1905 /* Do the checks of the actual argument list that are specific to elemental
1906 procedures. If called with c == NULL, we have a function, otherwise if
1907 expr == NULL, we have a subroutine. */
1909 static bool
1910 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1912 gfc_actual_arglist *arg0;
1913 gfc_actual_arglist *arg;
1914 gfc_symbol *esym = NULL;
1915 gfc_intrinsic_sym *isym = NULL;
1916 gfc_expr *e = NULL;
1917 gfc_intrinsic_arg *iformal = NULL;
1918 gfc_formal_arglist *eformal = NULL;
1919 bool formal_optional = false;
1920 bool set_by_optional = false;
1921 int i;
1922 int rank = 0;
1924 /* Is this an elemental procedure? */
1925 if (expr && expr->value.function.actual != NULL)
1927 if (expr->value.function.esym != NULL
1928 && expr->value.function.esym->attr.elemental)
1930 arg0 = expr->value.function.actual;
1931 esym = expr->value.function.esym;
1933 else if (expr->value.function.isym != NULL
1934 && expr->value.function.isym->elemental)
1936 arg0 = expr->value.function.actual;
1937 isym = expr->value.function.isym;
1939 else
1940 return true;
1942 else if (c && c->ext.actual != NULL)
1944 arg0 = c->ext.actual;
1946 if (c->resolved_sym)
1947 esym = c->resolved_sym;
1948 else
1949 esym = c->symtree->n.sym;
1950 gcc_assert (esym);
1952 if (!esym->attr.elemental)
1953 return true;
1955 else
1956 return true;
1958 /* The rank of an elemental is the rank of its array argument(s). */
1959 for (arg = arg0; arg; arg = arg->next)
1961 if (arg->expr != NULL && arg->expr->rank != 0)
1963 rank = arg->expr->rank;
1964 if (arg->expr->expr_type == EXPR_VARIABLE
1965 && arg->expr->symtree->n.sym->attr.optional)
1966 set_by_optional = true;
1968 /* Function specific; set the result rank and shape. */
1969 if (expr)
1971 expr->rank = rank;
1972 if (!expr->shape && arg->expr->shape)
1974 expr->shape = gfc_get_shape (rank);
1975 for (i = 0; i < rank; i++)
1976 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1979 break;
1983 /* If it is an array, it shall not be supplied as an actual argument
1984 to an elemental procedure unless an array of the same rank is supplied
1985 as an actual argument corresponding to a nonoptional dummy argument of
1986 that elemental procedure(12.4.1.5). */
1987 formal_optional = false;
1988 if (isym)
1989 iformal = isym->formal;
1990 else
1991 eformal = esym->formal;
1993 for (arg = arg0; arg; arg = arg->next)
1995 if (eformal)
1997 if (eformal->sym && eformal->sym->attr.optional)
1998 formal_optional = true;
1999 eformal = eformal->next;
2001 else if (isym && iformal)
2003 if (iformal->optional)
2004 formal_optional = true;
2005 iformal = iformal->next;
2007 else if (isym)
2008 formal_optional = true;
2010 if (pedantic && arg->expr != NULL
2011 && arg->expr->expr_type == EXPR_VARIABLE
2012 && arg->expr->symtree->n.sym->attr.optional
2013 && formal_optional
2014 && arg->expr->rank
2015 && (set_by_optional || arg->expr->rank != rank)
2016 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2018 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2019 "MISSING, it cannot be the actual argument of an "
2020 "ELEMENTAL procedure unless there is a non-optional "
2021 "argument with the same rank (12.4.1.5)",
2022 arg->expr->symtree->n.sym->name, &arg->expr->where);
2026 for (arg = arg0; arg; arg = arg->next)
2028 if (arg->expr == NULL || arg->expr->rank == 0)
2029 continue;
2031 /* Being elemental, the last upper bound of an assumed size array
2032 argument must be present. */
2033 if (resolve_assumed_size_actual (arg->expr))
2034 return false;
2036 /* Elemental procedure's array actual arguments must conform. */
2037 if (e != NULL)
2039 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2040 return false;
2042 else
2043 e = arg->expr;
2046 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2047 is an array, the intent inout/out variable needs to be also an array. */
2048 if (rank > 0 && esym && expr == NULL)
2049 for (eformal = esym->formal, arg = arg0; arg && eformal;
2050 arg = arg->next, eformal = eformal->next)
2051 if ((eformal->sym->attr.intent == INTENT_OUT
2052 || eformal->sym->attr.intent == INTENT_INOUT)
2053 && arg->expr && arg->expr->rank == 0)
2055 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2056 "ELEMENTAL subroutine '%s' is a scalar, but another "
2057 "actual argument is an array", &arg->expr->where,
2058 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2059 : "INOUT", eformal->sym->name, esym->name);
2060 return false;
2062 return true;
2066 /* This function does the checking of references to global procedures
2067 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2068 77 and 95 standards. It checks for a gsymbol for the name, making
2069 one if it does not already exist. If it already exists, then the
2070 reference being resolved must correspond to the type of gsymbol.
2071 Otherwise, the new symbol is equipped with the attributes of the
2072 reference. The corresponding code that is called in creating
2073 global entities is parse.c.
2075 In addition, for all but -std=legacy, the gsymbols are used to
2076 check the interfaces of external procedures from the same file.
2077 The namespace of the gsymbol is resolved and then, once this is
2078 done the interface is checked. */
2081 static bool
2082 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2084 if (!gsym_ns->proc_name->attr.recursive)
2085 return true;
2087 if (sym->ns == gsym_ns)
2088 return false;
2090 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2091 return false;
2093 return true;
2096 static bool
2097 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2099 if (gsym_ns->entries)
2101 gfc_entry_list *entry = gsym_ns->entries;
2103 for (; entry; entry = entry->next)
2105 if (strcmp (sym->name, entry->sym->name) == 0)
2107 if (strcmp (gsym_ns->proc_name->name,
2108 sym->ns->proc_name->name) == 0)
2109 return false;
2111 if (sym->ns->parent
2112 && strcmp (gsym_ns->proc_name->name,
2113 sym->ns->parent->proc_name->name) == 0)
2114 return false;
2118 return true;
2122 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2124 bool
2125 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2127 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2129 for ( ; arg; arg = arg->next)
2131 if (!arg->sym)
2132 continue;
2134 if (arg->sym->attr.allocatable) /* (2a) */
2136 strncpy (errmsg, _("allocatable argument"), err_len);
2137 return true;
2139 else if (arg->sym->attr.asynchronous)
2141 strncpy (errmsg, _("asynchronous argument"), err_len);
2142 return true;
2144 else if (arg->sym->attr.optional)
2146 strncpy (errmsg, _("optional argument"), err_len);
2147 return true;
2149 else if (arg->sym->attr.pointer)
2151 strncpy (errmsg, _("pointer argument"), err_len);
2152 return true;
2154 else if (arg->sym->attr.target)
2156 strncpy (errmsg, _("target argument"), err_len);
2157 return true;
2159 else if (arg->sym->attr.value)
2161 strncpy (errmsg, _("value argument"), err_len);
2162 return true;
2164 else if (arg->sym->attr.volatile_)
2166 strncpy (errmsg, _("volatile argument"), err_len);
2167 return true;
2169 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2171 strncpy (errmsg, _("assumed-shape argument"), err_len);
2172 return true;
2174 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2176 strncpy (errmsg, _("assumed-rank argument"), err_len);
2177 return true;
2179 else if (arg->sym->attr.codimension) /* (2c) */
2181 strncpy (errmsg, _("coarray argument"), err_len);
2182 return true;
2184 else if (false) /* (2d) TODO: parametrized derived type */
2186 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2187 return true;
2189 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2191 strncpy (errmsg, _("polymorphic argument"), err_len);
2192 return true;
2194 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2196 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2197 return true;
2199 else if (arg->sym->ts.type == BT_ASSUMED)
2201 /* As assumed-type is unlimited polymorphic (cf. above).
2202 See also TS 29113, Note 6.1. */
2203 strncpy (errmsg, _("assumed-type argument"), err_len);
2204 return true;
2208 if (sym->attr.function)
2210 gfc_symbol *res = sym->result ? sym->result : sym;
2212 if (res->attr.dimension) /* (3a) */
2214 strncpy (errmsg, _("array result"), err_len);
2215 return true;
2217 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2219 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2220 return true;
2222 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2223 && res->ts.u.cl->length
2224 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2226 strncpy (errmsg, _("result with non-constant character length"), err_len);
2227 return true;
2231 if (sym->attr.elemental) /* (4) */
2233 strncpy (errmsg, _("elemental procedure"), err_len);
2234 return true;
2236 else if (sym->attr.is_bind_c) /* (5) */
2238 strncpy (errmsg, _("bind(c) procedure"), err_len);
2239 return true;
2242 return false;
2246 static void
2247 resolve_global_procedure (gfc_symbol *sym, locus *where,
2248 gfc_actual_arglist **actual, int sub)
2250 gfc_gsymbol * gsym;
2251 gfc_namespace *ns;
2252 enum gfc_symbol_type type;
2253 char reason[200];
2255 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2257 gsym = gfc_get_gsymbol (sym->name);
2259 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2260 gfc_global_used (gsym, where);
2262 if ((sym->attr.if_source == IFSRC_UNKNOWN
2263 || sym->attr.if_source == IFSRC_IFBODY)
2264 && gsym->type != GSYM_UNKNOWN
2265 && gsym->ns
2266 && gsym->ns->resolved != -1
2267 && gsym->ns->proc_name
2268 && not_in_recursive (sym, gsym->ns)
2269 && not_entry_self_reference (sym, gsym->ns))
2271 gfc_symbol *def_sym;
2273 /* Resolve the gsymbol namespace if needed. */
2274 if (!gsym->ns->resolved)
2276 gfc_dt_list *old_dt_list;
2277 struct gfc_omp_saved_state old_omp_state;
2279 /* Stash away derived types so that the backend_decls do not
2280 get mixed up. */
2281 old_dt_list = gfc_derived_types;
2282 gfc_derived_types = NULL;
2283 /* And stash away openmp state. */
2284 gfc_omp_save_and_clear_state (&old_omp_state);
2286 gfc_resolve (gsym->ns);
2288 /* Store the new derived types with the global namespace. */
2289 if (gfc_derived_types)
2290 gsym->ns->derived_types = gfc_derived_types;
2292 /* Restore the derived types of this namespace. */
2293 gfc_derived_types = old_dt_list;
2294 /* And openmp state. */
2295 gfc_omp_restore_state (&old_omp_state);
2298 /* Make sure that translation for the gsymbol occurs before
2299 the procedure currently being resolved. */
2300 ns = gfc_global_ns_list;
2301 for (; ns && ns != gsym->ns; ns = ns->sibling)
2303 if (ns->sibling == gsym->ns)
2305 ns->sibling = gsym->ns->sibling;
2306 gsym->ns->sibling = gfc_global_ns_list;
2307 gfc_global_ns_list = gsym->ns;
2308 break;
2312 def_sym = gsym->ns->proc_name;
2313 if (def_sym->attr.entry_master)
2315 gfc_entry_list *entry;
2316 for (entry = gsym->ns->entries; entry; entry = entry->next)
2317 if (strcmp (entry->sym->name, sym->name) == 0)
2319 def_sym = entry->sym;
2320 break;
2324 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2326 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2327 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2328 gfc_typename (&def_sym->ts));
2329 goto done;
2332 if (sym->attr.if_source == IFSRC_UNKNOWN
2333 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2335 gfc_error ("Explicit interface required for '%s' at %L: %s",
2336 sym->name, &sym->declared_at, reason);
2337 goto done;
2340 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2341 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2342 gfc_errors_to_warnings (1);
2344 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2345 reason, sizeof(reason), NULL, NULL))
2347 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2348 sym->name, &sym->declared_at, reason);
2349 goto done;
2352 if (!pedantic
2353 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2354 && !(gfc_option.warn_std & GFC_STD_GNU)))
2355 gfc_errors_to_warnings (1);
2357 if (sym->attr.if_source != IFSRC_IFBODY)
2358 gfc_procedure_use (def_sym, actual, where);
2361 done:
2362 gfc_errors_to_warnings (0);
2364 if (gsym->type == GSYM_UNKNOWN)
2366 gsym->type = type;
2367 gsym->where = *where;
2370 gsym->used = 1;
2374 /************* Function resolution *************/
2376 /* Resolve a function call known to be generic.
2377 Section 14.1.2.4.1. */
2379 static match
2380 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2382 gfc_symbol *s;
2384 if (sym->attr.generic)
2386 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2387 if (s != NULL)
2389 expr->value.function.name = s->name;
2390 expr->value.function.esym = s;
2392 if (s->ts.type != BT_UNKNOWN)
2393 expr->ts = s->ts;
2394 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2395 expr->ts = s->result->ts;
2397 if (s->as != NULL)
2398 expr->rank = s->as->rank;
2399 else if (s->result != NULL && s->result->as != NULL)
2400 expr->rank = s->result->as->rank;
2402 gfc_set_sym_referenced (expr->value.function.esym);
2404 return MATCH_YES;
2407 /* TODO: Need to search for elemental references in generic
2408 interface. */
2411 if (sym->attr.intrinsic)
2412 return gfc_intrinsic_func_interface (expr, 0);
2414 return MATCH_NO;
2418 static bool
2419 resolve_generic_f (gfc_expr *expr)
2421 gfc_symbol *sym;
2422 match m;
2423 gfc_interface *intr = NULL;
2425 sym = expr->symtree->n.sym;
2427 for (;;)
2429 m = resolve_generic_f0 (expr, sym);
2430 if (m == MATCH_YES)
2431 return true;
2432 else if (m == MATCH_ERROR)
2433 return false;
2435 generic:
2436 if (!intr)
2437 for (intr = sym->generic; intr; intr = intr->next)
2438 if (intr->sym->attr.flavor == FL_DERIVED)
2439 break;
2441 if (sym->ns->parent == NULL)
2442 break;
2443 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2445 if (sym == NULL)
2446 break;
2447 if (!generic_sym (sym))
2448 goto generic;
2451 /* Last ditch attempt. See if the reference is to an intrinsic
2452 that possesses a matching interface. 14.1.2.4 */
2453 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2455 gfc_error ("There is no specific function for the generic '%s' "
2456 "at %L", expr->symtree->n.sym->name, &expr->where);
2457 return false;
2460 if (intr)
2462 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2463 NULL, false))
2464 return false;
2465 return resolve_structure_cons (expr, 0);
2468 m = gfc_intrinsic_func_interface (expr, 0);
2469 if (m == MATCH_YES)
2470 return true;
2472 if (m == MATCH_NO)
2473 gfc_error ("Generic function '%s' at %L is not consistent with a "
2474 "specific intrinsic interface", expr->symtree->n.sym->name,
2475 &expr->where);
2477 return false;
2481 /* Resolve a function call known to be specific. */
2483 static match
2484 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2486 match m;
2488 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2490 if (sym->attr.dummy)
2492 sym->attr.proc = PROC_DUMMY;
2493 goto found;
2496 sym->attr.proc = PROC_EXTERNAL;
2497 goto found;
2500 if (sym->attr.proc == PROC_MODULE
2501 || sym->attr.proc == PROC_ST_FUNCTION
2502 || sym->attr.proc == PROC_INTERNAL)
2503 goto found;
2505 if (sym->attr.intrinsic)
2507 m = gfc_intrinsic_func_interface (expr, 1);
2508 if (m == MATCH_YES)
2509 return MATCH_YES;
2510 if (m == MATCH_NO)
2511 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2512 "with an intrinsic", sym->name, &expr->where);
2514 return MATCH_ERROR;
2517 return MATCH_NO;
2519 found:
2520 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2522 if (sym->result)
2523 expr->ts = sym->result->ts;
2524 else
2525 expr->ts = sym->ts;
2526 expr->value.function.name = sym->name;
2527 expr->value.function.esym = sym;
2528 if (sym->as != NULL)
2529 expr->rank = sym->as->rank;
2531 return MATCH_YES;
2535 static bool
2536 resolve_specific_f (gfc_expr *expr)
2538 gfc_symbol *sym;
2539 match m;
2541 sym = expr->symtree->n.sym;
2543 for (;;)
2545 m = resolve_specific_f0 (sym, expr);
2546 if (m == MATCH_YES)
2547 return true;
2548 if (m == MATCH_ERROR)
2549 return false;
2551 if (sym->ns->parent == NULL)
2552 break;
2554 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2556 if (sym == NULL)
2557 break;
2560 gfc_error ("Unable to resolve the specific function '%s' at %L",
2561 expr->symtree->n.sym->name, &expr->where);
2563 return true;
2567 /* Resolve a procedure call not known to be generic nor specific. */
2569 static bool
2570 resolve_unknown_f (gfc_expr *expr)
2572 gfc_symbol *sym;
2573 gfc_typespec *ts;
2575 sym = expr->symtree->n.sym;
2577 if (sym->attr.dummy)
2579 sym->attr.proc = PROC_DUMMY;
2580 expr->value.function.name = sym->name;
2581 goto set_type;
2584 /* See if we have an intrinsic function reference. */
2586 if (gfc_is_intrinsic (sym, 0, expr->where))
2588 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2589 return true;
2590 return false;
2593 /* The reference is to an external name. */
2595 sym->attr.proc = PROC_EXTERNAL;
2596 expr->value.function.name = sym->name;
2597 expr->value.function.esym = expr->symtree->n.sym;
2599 if (sym->as != NULL)
2600 expr->rank = sym->as->rank;
2602 /* Type of the expression is either the type of the symbol or the
2603 default type of the symbol. */
2605 set_type:
2606 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2608 if (sym->ts.type != BT_UNKNOWN)
2609 expr->ts = sym->ts;
2610 else
2612 ts = gfc_get_default_type (sym->name, sym->ns);
2614 if (ts->type == BT_UNKNOWN)
2616 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2617 sym->name, &expr->where);
2618 return false;
2620 else
2621 expr->ts = *ts;
2624 return true;
2628 /* Return true, if the symbol is an external procedure. */
2629 static bool
2630 is_external_proc (gfc_symbol *sym)
2632 if (!sym->attr.dummy && !sym->attr.contained
2633 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2634 && sym->attr.proc != PROC_ST_FUNCTION
2635 && !sym->attr.proc_pointer
2636 && !sym->attr.use_assoc
2637 && sym->name)
2638 return true;
2640 return false;
2644 /* Figure out if a function reference is pure or not. Also set the name
2645 of the function for a potential error message. Return nonzero if the
2646 function is PURE, zero if not. */
2647 static int
2648 pure_stmt_function (gfc_expr *, gfc_symbol *);
2650 static int
2651 pure_function (gfc_expr *e, const char **name)
2653 int pure;
2655 *name = NULL;
2657 if (e->symtree != NULL
2658 && e->symtree->n.sym != NULL
2659 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2660 return pure_stmt_function (e, e->symtree->n.sym);
2662 if (e->value.function.esym)
2664 pure = gfc_pure (e->value.function.esym);
2665 *name = e->value.function.esym->name;
2667 else if (e->value.function.isym)
2669 pure = e->value.function.isym->pure
2670 || e->value.function.isym->elemental;
2671 *name = e->value.function.isym->name;
2673 else
2675 /* Implicit functions are not pure. */
2676 pure = 0;
2677 *name = e->value.function.name;
2680 return pure;
2684 static bool
2685 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2686 int *f ATTRIBUTE_UNUSED)
2688 const char *name;
2690 /* Don't bother recursing into other statement functions
2691 since they will be checked individually for purity. */
2692 if (e->expr_type != EXPR_FUNCTION
2693 || !e->symtree
2694 || e->symtree->n.sym == sym
2695 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2696 return false;
2698 return pure_function (e, &name) ? false : true;
2702 static int
2703 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2705 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2709 /* Resolve a function call, which means resolving the arguments, then figuring
2710 out which entity the name refers to. */
2712 static bool
2713 resolve_function (gfc_expr *expr)
2715 gfc_actual_arglist *arg;
2716 gfc_symbol *sym;
2717 const char *name;
2718 bool t;
2719 int temp;
2720 procedure_type p = PROC_INTRINSIC;
2721 bool no_formal_args;
2723 sym = NULL;
2724 if (expr->symtree)
2725 sym = expr->symtree->n.sym;
2727 /* If this is a procedure pointer component, it has already been resolved. */
2728 if (gfc_is_proc_ptr_comp (expr))
2729 return true;
2731 if (sym && sym->attr.intrinsic
2732 && !gfc_resolve_intrinsic (sym, &expr->where))
2733 return false;
2735 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2737 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2738 return false;
2741 /* If this ia a deferred TBP with an abstract interface (which may
2742 of course be referenced), expr->value.function.esym will be set. */
2743 if (sym && sym->attr.abstract && !expr->value.function.esym)
2745 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2746 sym->name, &expr->where);
2747 return false;
2750 /* Switch off assumed size checking and do this again for certain kinds
2751 of procedure, once the procedure itself is resolved. */
2752 need_full_assumed_size++;
2754 if (expr->symtree && expr->symtree->n.sym)
2755 p = expr->symtree->n.sym->attr.proc;
2757 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2758 inquiry_argument = true;
2759 no_formal_args = sym && is_external_proc (sym)
2760 && gfc_sym_get_dummy_args (sym) == NULL;
2762 if (!resolve_actual_arglist (expr->value.function.actual,
2763 p, no_formal_args))
2765 inquiry_argument = false;
2766 return false;
2769 inquiry_argument = false;
2771 /* Resume assumed_size checking. */
2772 need_full_assumed_size--;
2774 /* If the procedure is external, check for usage. */
2775 if (sym && is_external_proc (sym))
2776 resolve_global_procedure (sym, &expr->where,
2777 &expr->value.function.actual, 0);
2779 if (sym && sym->ts.type == BT_CHARACTER
2780 && sym->ts.u.cl
2781 && sym->ts.u.cl->length == NULL
2782 && !sym->attr.dummy
2783 && !sym->ts.deferred
2784 && expr->value.function.esym == NULL
2785 && !sym->attr.contained)
2787 /* Internal procedures are taken care of in resolve_contained_fntype. */
2788 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2789 "be used at %L since it is not a dummy argument",
2790 sym->name, &expr->where);
2791 return false;
2794 /* See if function is already resolved. */
2796 if (expr->value.function.name != NULL)
2798 if (expr->ts.type == BT_UNKNOWN)
2799 expr->ts = sym->ts;
2800 t = true;
2802 else
2804 /* Apply the rules of section 14.1.2. */
2806 switch (procedure_kind (sym))
2808 case PTYPE_GENERIC:
2809 t = resolve_generic_f (expr);
2810 break;
2812 case PTYPE_SPECIFIC:
2813 t = resolve_specific_f (expr);
2814 break;
2816 case PTYPE_UNKNOWN:
2817 t = resolve_unknown_f (expr);
2818 break;
2820 default:
2821 gfc_internal_error ("resolve_function(): bad function type");
2825 /* If the expression is still a function (it might have simplified),
2826 then we check to see if we are calling an elemental function. */
2828 if (expr->expr_type != EXPR_FUNCTION)
2829 return t;
2831 temp = need_full_assumed_size;
2832 need_full_assumed_size = 0;
2834 if (!resolve_elemental_actual (expr, NULL))
2835 return false;
2837 if (omp_workshare_flag
2838 && expr->value.function.esym
2839 && ! gfc_elemental (expr->value.function.esym))
2841 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2842 "in WORKSHARE construct", expr->value.function.esym->name,
2843 &expr->where);
2844 t = false;
2847 #define GENERIC_ID expr->value.function.isym->id
2848 else if (expr->value.function.actual != NULL
2849 && expr->value.function.isym != NULL
2850 && GENERIC_ID != GFC_ISYM_LBOUND
2851 && GENERIC_ID != GFC_ISYM_LEN
2852 && GENERIC_ID != GFC_ISYM_LOC
2853 && GENERIC_ID != GFC_ISYM_C_LOC
2854 && GENERIC_ID != GFC_ISYM_PRESENT)
2856 /* Array intrinsics must also have the last upper bound of an
2857 assumed size array argument. UBOUND and SIZE have to be
2858 excluded from the check if the second argument is anything
2859 than a constant. */
2861 for (arg = expr->value.function.actual; arg; arg = arg->next)
2863 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2864 && arg->next != NULL && arg->next->expr)
2866 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2867 break;
2869 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2870 break;
2872 if ((int)mpz_get_si (arg->next->expr->value.integer)
2873 < arg->expr->rank)
2874 break;
2877 if (arg->expr != NULL
2878 && arg->expr->rank > 0
2879 && resolve_assumed_size_actual (arg->expr))
2880 return false;
2883 #undef GENERIC_ID
2885 need_full_assumed_size = temp;
2886 name = NULL;
2888 if (!pure_function (expr, &name) && name)
2890 if (forall_flag)
2892 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2893 "FORALL %s", name, &expr->where,
2894 forall_flag == 2 ? "mask" : "block");
2895 t = false;
2897 else if (do_concurrent_flag)
2899 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2900 "DO CONCURRENT %s", name, &expr->where,
2901 do_concurrent_flag == 2 ? "mask" : "block");
2902 t = false;
2904 else if (gfc_pure (NULL))
2906 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2907 "procedure within a PURE procedure", name, &expr->where);
2908 t = false;
2911 if (gfc_implicit_pure (NULL))
2912 gfc_current_ns->proc_name->attr.implicit_pure = 0;
2915 /* Functions without the RECURSIVE attribution are not allowed to
2916 * call themselves. */
2917 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2919 gfc_symbol *esym;
2920 esym = expr->value.function.esym;
2922 if (is_illegal_recursion (esym, gfc_current_ns))
2924 if (esym->attr.entry && esym->ns->entries)
2925 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2926 " function '%s' is not RECURSIVE",
2927 esym->name, &expr->where, esym->ns->entries->sym->name);
2928 else
2929 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2930 " is not RECURSIVE", esym->name, &expr->where);
2932 t = false;
2936 /* Character lengths of use associated functions may contains references to
2937 symbols not referenced from the current program unit otherwise. Make sure
2938 those symbols are marked as referenced. */
2940 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2941 && expr->value.function.esym->attr.use_assoc)
2943 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2946 /* Make sure that the expression has a typespec that works. */
2947 if (expr->ts.type == BT_UNKNOWN)
2949 if (expr->symtree->n.sym->result
2950 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2951 && !expr->symtree->n.sym->result->attr.proc_pointer)
2952 expr->ts = expr->symtree->n.sym->result->ts;
2955 return t;
2959 /************* Subroutine resolution *************/
2961 static void
2962 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2964 if (gfc_pure (sym))
2965 return;
2967 if (forall_flag)
2968 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2969 sym->name, &c->loc);
2970 else if (do_concurrent_flag)
2971 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
2972 "PURE", sym->name, &c->loc);
2973 else if (gfc_pure (NULL))
2974 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2975 &c->loc);
2977 if (gfc_implicit_pure (NULL))
2978 gfc_current_ns->proc_name->attr.implicit_pure = 0;
2982 static match
2983 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2985 gfc_symbol *s;
2987 if (sym->attr.generic)
2989 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2990 if (s != NULL)
2992 c->resolved_sym = s;
2993 pure_subroutine (c, s);
2994 return MATCH_YES;
2997 /* TODO: Need to search for elemental references in generic interface. */
3000 if (sym->attr.intrinsic)
3001 return gfc_intrinsic_sub_interface (c, 0);
3003 return MATCH_NO;
3007 static bool
3008 resolve_generic_s (gfc_code *c)
3010 gfc_symbol *sym;
3011 match m;
3013 sym = c->symtree->n.sym;
3015 for (;;)
3017 m = resolve_generic_s0 (c, sym);
3018 if (m == MATCH_YES)
3019 return true;
3020 else if (m == MATCH_ERROR)
3021 return false;
3023 generic:
3024 if (sym->ns->parent == NULL)
3025 break;
3026 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3028 if (sym == NULL)
3029 break;
3030 if (!generic_sym (sym))
3031 goto generic;
3034 /* Last ditch attempt. See if the reference is to an intrinsic
3035 that possesses a matching interface. 14.1.2.4 */
3036 sym = c->symtree->n.sym;
3038 if (!gfc_is_intrinsic (sym, 1, c->loc))
3040 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3041 sym->name, &c->loc);
3042 return false;
3045 m = gfc_intrinsic_sub_interface (c, 0);
3046 if (m == MATCH_YES)
3047 return true;
3048 if (m == MATCH_NO)
3049 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3050 "intrinsic subroutine interface", sym->name, &c->loc);
3052 return false;
3056 /* Resolve a subroutine call known to be specific. */
3058 static match
3059 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3061 match m;
3063 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3065 if (sym->attr.dummy)
3067 sym->attr.proc = PROC_DUMMY;
3068 goto found;
3071 sym->attr.proc = PROC_EXTERNAL;
3072 goto found;
3075 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3076 goto found;
3078 if (sym->attr.intrinsic)
3080 m = gfc_intrinsic_sub_interface (c, 1);
3081 if (m == MATCH_YES)
3082 return MATCH_YES;
3083 if (m == MATCH_NO)
3084 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3085 "with an intrinsic", sym->name, &c->loc);
3087 return MATCH_ERROR;
3090 return MATCH_NO;
3092 found:
3093 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3095 c->resolved_sym = sym;
3096 pure_subroutine (c, sym);
3098 return MATCH_YES;
3102 static bool
3103 resolve_specific_s (gfc_code *c)
3105 gfc_symbol *sym;
3106 match m;
3108 sym = c->symtree->n.sym;
3110 for (;;)
3112 m = resolve_specific_s0 (c, sym);
3113 if (m == MATCH_YES)
3114 return true;
3115 if (m == MATCH_ERROR)
3116 return false;
3118 if (sym->ns->parent == NULL)
3119 break;
3121 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3123 if (sym == NULL)
3124 break;
3127 sym = c->symtree->n.sym;
3128 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3129 sym->name, &c->loc);
3131 return false;
3135 /* Resolve a subroutine call not known to be generic nor specific. */
3137 static bool
3138 resolve_unknown_s (gfc_code *c)
3140 gfc_symbol *sym;
3142 sym = c->symtree->n.sym;
3144 if (sym->attr.dummy)
3146 sym->attr.proc = PROC_DUMMY;
3147 goto found;
3150 /* See if we have an intrinsic function reference. */
3152 if (gfc_is_intrinsic (sym, 1, c->loc))
3154 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3155 return true;
3156 return false;
3159 /* The reference is to an external name. */
3161 found:
3162 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3164 c->resolved_sym = sym;
3166 pure_subroutine (c, sym);
3168 return true;
3172 /* Resolve a subroutine call. Although it was tempting to use the same code
3173 for functions, subroutines and functions are stored differently and this
3174 makes things awkward. */
3176 static bool
3177 resolve_call (gfc_code *c)
3179 bool t;
3180 procedure_type ptype = PROC_INTRINSIC;
3181 gfc_symbol *csym, *sym;
3182 bool no_formal_args;
3184 csym = c->symtree ? c->symtree->n.sym : NULL;
3186 if (csym && csym->ts.type != BT_UNKNOWN)
3188 gfc_error ("'%s' at %L has a type, which is not consistent with "
3189 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3190 return false;
3193 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3195 gfc_symtree *st;
3196 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3197 sym = st ? st->n.sym : NULL;
3198 if (sym && csym != sym
3199 && sym->ns == gfc_current_ns
3200 && sym->attr.flavor == FL_PROCEDURE
3201 && sym->attr.contained)
3203 sym->refs++;
3204 if (csym->attr.generic)
3205 c->symtree->n.sym = sym;
3206 else
3207 c->symtree = st;
3208 csym = c->symtree->n.sym;
3212 /* If this ia a deferred TBP, c->expr1 will be set. */
3213 if (!c->expr1 && csym)
3215 if (csym->attr.abstract)
3217 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3218 csym->name, &c->loc);
3219 return false;
3222 /* Subroutines without the RECURSIVE attribution are not allowed to
3223 call themselves. */
3224 if (is_illegal_recursion (csym, gfc_current_ns))
3226 if (csym->attr.entry && csym->ns->entries)
3227 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3228 "as subroutine '%s' is not RECURSIVE",
3229 csym->name, &c->loc, csym->ns->entries->sym->name);
3230 else
3231 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3232 "as it is not RECURSIVE", csym->name, &c->loc);
3234 t = false;
3238 /* Switch off assumed size checking and do this again for certain kinds
3239 of procedure, once the procedure itself is resolved. */
3240 need_full_assumed_size++;
3242 if (csym)
3243 ptype = csym->attr.proc;
3245 no_formal_args = csym && is_external_proc (csym)
3246 && gfc_sym_get_dummy_args (csym) == NULL;
3247 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3248 return false;
3250 /* Resume assumed_size checking. */
3251 need_full_assumed_size--;
3253 /* If external, check for usage. */
3254 if (csym && is_external_proc (csym))
3255 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3257 t = true;
3258 if (c->resolved_sym == NULL)
3260 c->resolved_isym = NULL;
3261 switch (procedure_kind (csym))
3263 case PTYPE_GENERIC:
3264 t = resolve_generic_s (c);
3265 break;
3267 case PTYPE_SPECIFIC:
3268 t = resolve_specific_s (c);
3269 break;
3271 case PTYPE_UNKNOWN:
3272 t = resolve_unknown_s (c);
3273 break;
3275 default:
3276 gfc_internal_error ("resolve_subroutine(): bad function type");
3280 /* Some checks of elemental subroutine actual arguments. */
3281 if (!resolve_elemental_actual (NULL, c))
3282 return false;
3284 return t;
3288 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3289 op1->shape and op2->shape are non-NULL return true if their shapes
3290 match. If both op1->shape and op2->shape are non-NULL return false
3291 if their shapes do not match. If either op1->shape or op2->shape is
3292 NULL, return true. */
3294 static bool
3295 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3297 bool t;
3298 int i;
3300 t = true;
3302 if (op1->shape != NULL && op2->shape != NULL)
3304 for (i = 0; i < op1->rank; i++)
3306 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3308 gfc_error ("Shapes for operands at %L and %L are not conformable",
3309 &op1->where, &op2->where);
3310 t = false;
3311 break;
3316 return t;
3320 /* Resolve an operator expression node. This can involve replacing the
3321 operation with a user defined function call. */
3323 static bool
3324 resolve_operator (gfc_expr *e)
3326 gfc_expr *op1, *op2;
3327 char msg[200];
3328 bool dual_locus_error;
3329 bool t;
3331 /* Resolve all subnodes-- give them types. */
3333 switch (e->value.op.op)
3335 default:
3336 if (!gfc_resolve_expr (e->value.op.op2))
3337 return false;
3339 /* Fall through... */
3341 case INTRINSIC_NOT:
3342 case INTRINSIC_UPLUS:
3343 case INTRINSIC_UMINUS:
3344 case INTRINSIC_PARENTHESES:
3345 if (!gfc_resolve_expr (e->value.op.op1))
3346 return false;
3347 break;
3350 /* Typecheck the new node. */
3352 op1 = e->value.op.op1;
3353 op2 = e->value.op.op2;
3354 dual_locus_error = false;
3356 if ((op1 && op1->expr_type == EXPR_NULL)
3357 || (op2 && op2->expr_type == EXPR_NULL))
3359 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3360 goto bad_op;
3363 switch (e->value.op.op)
3365 case INTRINSIC_UPLUS:
3366 case INTRINSIC_UMINUS:
3367 if (op1->ts.type == BT_INTEGER
3368 || op1->ts.type == BT_REAL
3369 || op1->ts.type == BT_COMPLEX)
3371 e->ts = op1->ts;
3372 break;
3375 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3376 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3377 goto bad_op;
3379 case INTRINSIC_PLUS:
3380 case INTRINSIC_MINUS:
3381 case INTRINSIC_TIMES:
3382 case INTRINSIC_DIVIDE:
3383 case INTRINSIC_POWER:
3384 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3386 gfc_type_convert_binary (e, 1);
3387 break;
3390 sprintf (msg,
3391 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3392 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3393 gfc_typename (&op2->ts));
3394 goto bad_op;
3396 case INTRINSIC_CONCAT:
3397 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3398 && op1->ts.kind == op2->ts.kind)
3400 e->ts.type = BT_CHARACTER;
3401 e->ts.kind = op1->ts.kind;
3402 break;
3405 sprintf (msg,
3406 _("Operands of string concatenation operator at %%L are %s/%s"),
3407 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3408 goto bad_op;
3410 case INTRINSIC_AND:
3411 case INTRINSIC_OR:
3412 case INTRINSIC_EQV:
3413 case INTRINSIC_NEQV:
3414 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3416 e->ts.type = BT_LOGICAL;
3417 e->ts.kind = gfc_kind_max (op1, op2);
3418 if (op1->ts.kind < e->ts.kind)
3419 gfc_convert_type (op1, &e->ts, 2);
3420 else if (op2->ts.kind < e->ts.kind)
3421 gfc_convert_type (op2, &e->ts, 2);
3422 break;
3425 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3426 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3427 gfc_typename (&op2->ts));
3429 goto bad_op;
3431 case INTRINSIC_NOT:
3432 if (op1->ts.type == BT_LOGICAL)
3434 e->ts.type = BT_LOGICAL;
3435 e->ts.kind = op1->ts.kind;
3436 break;
3439 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3440 gfc_typename (&op1->ts));
3441 goto bad_op;
3443 case INTRINSIC_GT:
3444 case INTRINSIC_GT_OS:
3445 case INTRINSIC_GE:
3446 case INTRINSIC_GE_OS:
3447 case INTRINSIC_LT:
3448 case INTRINSIC_LT_OS:
3449 case INTRINSIC_LE:
3450 case INTRINSIC_LE_OS:
3451 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3453 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3454 goto bad_op;
3457 /* Fall through... */
3459 case INTRINSIC_EQ:
3460 case INTRINSIC_EQ_OS:
3461 case INTRINSIC_NE:
3462 case INTRINSIC_NE_OS:
3463 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3464 && op1->ts.kind == op2->ts.kind)
3466 e->ts.type = BT_LOGICAL;
3467 e->ts.kind = gfc_default_logical_kind;
3468 break;
3471 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3473 gfc_type_convert_binary (e, 1);
3475 e->ts.type = BT_LOGICAL;
3476 e->ts.kind = gfc_default_logical_kind;
3478 if (gfc_option.warn_compare_reals)
3480 gfc_intrinsic_op op = e->value.op.op;
3482 /* Type conversion has made sure that the types of op1 and op2
3483 agree, so it is only necessary to check the first one. */
3484 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3485 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3486 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3488 const char *msg;
3490 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3491 msg = "Equality comparison for %s at %L";
3492 else
3493 msg = "Inequality comparison for %s at %L";
3495 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3499 break;
3502 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3503 sprintf (msg,
3504 _("Logicals at %%L must be compared with %s instead of %s"),
3505 (e->value.op.op == INTRINSIC_EQ
3506 || e->value.op.op == INTRINSIC_EQ_OS)
3507 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3508 else
3509 sprintf (msg,
3510 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3511 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3512 gfc_typename (&op2->ts));
3514 goto bad_op;
3516 case INTRINSIC_USER:
3517 if (e->value.op.uop->op == NULL)
3518 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3519 else if (op2 == NULL)
3520 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3521 e->value.op.uop->name, gfc_typename (&op1->ts));
3522 else
3524 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3525 e->value.op.uop->name, gfc_typename (&op1->ts),
3526 gfc_typename (&op2->ts));
3527 e->value.op.uop->op->sym->attr.referenced = 1;
3530 goto bad_op;
3532 case INTRINSIC_PARENTHESES:
3533 e->ts = op1->ts;
3534 if (e->ts.type == BT_CHARACTER)
3535 e->ts.u.cl = op1->ts.u.cl;
3536 break;
3538 default:
3539 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3542 /* Deal with arrayness of an operand through an operator. */
3544 t = true;
3546 switch (e->value.op.op)
3548 case INTRINSIC_PLUS:
3549 case INTRINSIC_MINUS:
3550 case INTRINSIC_TIMES:
3551 case INTRINSIC_DIVIDE:
3552 case INTRINSIC_POWER:
3553 case INTRINSIC_CONCAT:
3554 case INTRINSIC_AND:
3555 case INTRINSIC_OR:
3556 case INTRINSIC_EQV:
3557 case INTRINSIC_NEQV:
3558 case INTRINSIC_EQ:
3559 case INTRINSIC_EQ_OS:
3560 case INTRINSIC_NE:
3561 case INTRINSIC_NE_OS:
3562 case INTRINSIC_GT:
3563 case INTRINSIC_GT_OS:
3564 case INTRINSIC_GE:
3565 case INTRINSIC_GE_OS:
3566 case INTRINSIC_LT:
3567 case INTRINSIC_LT_OS:
3568 case INTRINSIC_LE:
3569 case INTRINSIC_LE_OS:
3571 if (op1->rank == 0 && op2->rank == 0)
3572 e->rank = 0;
3574 if (op1->rank == 0 && op2->rank != 0)
3576 e->rank = op2->rank;
3578 if (e->shape == NULL)
3579 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3582 if (op1->rank != 0 && op2->rank == 0)
3584 e->rank = op1->rank;
3586 if (e->shape == NULL)
3587 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3590 if (op1->rank != 0 && op2->rank != 0)
3592 if (op1->rank == op2->rank)
3594 e->rank = op1->rank;
3595 if (e->shape == NULL)
3597 t = compare_shapes (op1, op2);
3598 if (!t)
3599 e->shape = NULL;
3600 else
3601 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3604 else
3606 /* Allow higher level expressions to work. */
3607 e->rank = 0;
3609 /* Try user-defined operators, and otherwise throw an error. */
3610 dual_locus_error = true;
3611 sprintf (msg,
3612 _("Inconsistent ranks for operator at %%L and %%L"));
3613 goto bad_op;
3617 break;
3619 case INTRINSIC_PARENTHESES:
3620 case INTRINSIC_NOT:
3621 case INTRINSIC_UPLUS:
3622 case INTRINSIC_UMINUS:
3623 /* Simply copy arrayness attribute */
3624 e->rank = op1->rank;
3626 if (e->shape == NULL)
3627 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3629 break;
3631 default:
3632 break;
3635 /* Attempt to simplify the expression. */
3636 if (t)
3638 t = gfc_simplify_expr (e, 0);
3639 /* Some calls do not succeed in simplification and return false
3640 even though there is no error; e.g. variable references to
3641 PARAMETER arrays. */
3642 if (!gfc_is_constant_expr (e))
3643 t = true;
3645 return t;
3647 bad_op:
3650 match m = gfc_extend_expr (e);
3651 if (m == MATCH_YES)
3652 return true;
3653 if (m == MATCH_ERROR)
3654 return false;
3657 if (dual_locus_error)
3658 gfc_error (msg, &op1->where, &op2->where);
3659 else
3660 gfc_error (msg, &e->where);
3662 return false;
3666 /************** Array resolution subroutines **************/
3668 typedef enum
3669 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3670 comparison;
3672 /* Compare two integer expressions. */
3674 static comparison
3675 compare_bound (gfc_expr *a, gfc_expr *b)
3677 int i;
3679 if (a == NULL || a->expr_type != EXPR_CONSTANT
3680 || b == NULL || b->expr_type != EXPR_CONSTANT)
3681 return CMP_UNKNOWN;
3683 /* If either of the types isn't INTEGER, we must have
3684 raised an error earlier. */
3686 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3687 return CMP_UNKNOWN;
3689 i = mpz_cmp (a->value.integer, b->value.integer);
3691 if (i < 0)
3692 return CMP_LT;
3693 if (i > 0)
3694 return CMP_GT;
3695 return CMP_EQ;
3699 /* Compare an integer expression with an integer. */
3701 static comparison
3702 compare_bound_int (gfc_expr *a, int b)
3704 int i;
3706 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3707 return CMP_UNKNOWN;
3709 if (a->ts.type != BT_INTEGER)
3710 gfc_internal_error ("compare_bound_int(): Bad expression");
3712 i = mpz_cmp_si (a->value.integer, b);
3714 if (i < 0)
3715 return CMP_LT;
3716 if (i > 0)
3717 return CMP_GT;
3718 return CMP_EQ;
3722 /* Compare an integer expression with a mpz_t. */
3724 static comparison
3725 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3727 int i;
3729 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3730 return CMP_UNKNOWN;
3732 if (a->ts.type != BT_INTEGER)
3733 gfc_internal_error ("compare_bound_int(): Bad expression");
3735 i = mpz_cmp (a->value.integer, b);
3737 if (i < 0)
3738 return CMP_LT;
3739 if (i > 0)
3740 return CMP_GT;
3741 return CMP_EQ;
3745 /* Compute the last value of a sequence given by a triplet.
3746 Return 0 if it wasn't able to compute the last value, or if the
3747 sequence if empty, and 1 otherwise. */
3749 static int
3750 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3751 gfc_expr *stride, mpz_t last)
3753 mpz_t rem;
3755 if (start == NULL || start->expr_type != EXPR_CONSTANT
3756 || end == NULL || end->expr_type != EXPR_CONSTANT
3757 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3758 return 0;
3760 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3761 || (stride != NULL && stride->ts.type != BT_INTEGER))
3762 return 0;
3764 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3766 if (compare_bound (start, end) == CMP_GT)
3767 return 0;
3768 mpz_set (last, end->value.integer);
3769 return 1;
3772 if (compare_bound_int (stride, 0) == CMP_GT)
3774 /* Stride is positive */
3775 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3776 return 0;
3778 else
3780 /* Stride is negative */
3781 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3782 return 0;
3785 mpz_init (rem);
3786 mpz_sub (rem, end->value.integer, start->value.integer);
3787 mpz_tdiv_r (rem, rem, stride->value.integer);
3788 mpz_sub (last, end->value.integer, rem);
3789 mpz_clear (rem);
3791 return 1;
3795 /* Compare a single dimension of an array reference to the array
3796 specification. */
3798 static bool
3799 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3801 mpz_t last_value;
3803 if (ar->dimen_type[i] == DIMEN_STAR)
3805 gcc_assert (ar->stride[i] == NULL);
3806 /* This implies [*] as [*:] and [*:3] are not possible. */
3807 if (ar->start[i] == NULL)
3809 gcc_assert (ar->end[i] == NULL);
3810 return true;
3814 /* Given start, end and stride values, calculate the minimum and
3815 maximum referenced indexes. */
3817 switch (ar->dimen_type[i])
3819 case DIMEN_VECTOR:
3820 case DIMEN_THIS_IMAGE:
3821 break;
3823 case DIMEN_STAR:
3824 case DIMEN_ELEMENT:
3825 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3827 if (i < as->rank)
3828 gfc_warning ("Array reference at %L is out of bounds "
3829 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3830 mpz_get_si (ar->start[i]->value.integer),
3831 mpz_get_si (as->lower[i]->value.integer), i+1);
3832 else
3833 gfc_warning ("Array reference at %L is out of bounds "
3834 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3835 mpz_get_si (ar->start[i]->value.integer),
3836 mpz_get_si (as->lower[i]->value.integer),
3837 i + 1 - as->rank);
3838 return true;
3840 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3842 if (i < as->rank)
3843 gfc_warning ("Array reference at %L is out of bounds "
3844 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3845 mpz_get_si (ar->start[i]->value.integer),
3846 mpz_get_si (as->upper[i]->value.integer), i+1);
3847 else
3848 gfc_warning ("Array reference at %L is out of bounds "
3849 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3850 mpz_get_si (ar->start[i]->value.integer),
3851 mpz_get_si (as->upper[i]->value.integer),
3852 i + 1 - as->rank);
3853 return true;
3856 break;
3858 case DIMEN_RANGE:
3860 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3861 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3863 comparison comp_start_end = compare_bound (AR_START, AR_END);
3865 /* Check for zero stride, which is not allowed. */
3866 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3868 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3869 return false;
3872 /* if start == len || (stride > 0 && start < len)
3873 || (stride < 0 && start > len),
3874 then the array section contains at least one element. In this
3875 case, there is an out-of-bounds access if
3876 (start < lower || start > upper). */
3877 if (compare_bound (AR_START, AR_END) == CMP_EQ
3878 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3879 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3880 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3881 && comp_start_end == CMP_GT))
3883 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3885 gfc_warning ("Lower array reference at %L is out of bounds "
3886 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3887 mpz_get_si (AR_START->value.integer),
3888 mpz_get_si (as->lower[i]->value.integer), i+1);
3889 return true;
3891 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3893 gfc_warning ("Lower array reference at %L is out of bounds "
3894 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3895 mpz_get_si (AR_START->value.integer),
3896 mpz_get_si (as->upper[i]->value.integer), i+1);
3897 return true;
3901 /* If we can compute the highest index of the array section,
3902 then it also has to be between lower and upper. */
3903 mpz_init (last_value);
3904 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3905 last_value))
3907 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3909 gfc_warning ("Upper array reference at %L is out of bounds "
3910 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3911 mpz_get_si (last_value),
3912 mpz_get_si (as->lower[i]->value.integer), i+1);
3913 mpz_clear (last_value);
3914 return true;
3916 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3918 gfc_warning ("Upper array reference at %L is out of bounds "
3919 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3920 mpz_get_si (last_value),
3921 mpz_get_si (as->upper[i]->value.integer), i+1);
3922 mpz_clear (last_value);
3923 return true;
3926 mpz_clear (last_value);
3928 #undef AR_START
3929 #undef AR_END
3931 break;
3933 default:
3934 gfc_internal_error ("check_dimension(): Bad array reference");
3937 return true;
3941 /* Compare an array reference with an array specification. */
3943 static bool
3944 compare_spec_to_ref (gfc_array_ref *ar)
3946 gfc_array_spec *as;
3947 int i;
3949 as = ar->as;
3950 i = as->rank - 1;
3951 /* TODO: Full array sections are only allowed as actual parameters. */
3952 if (as->type == AS_ASSUMED_SIZE
3953 && (/*ar->type == AR_FULL
3954 ||*/ (ar->type == AR_SECTION
3955 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3957 gfc_error ("Rightmost upper bound of assumed size array section "
3958 "not specified at %L", &ar->where);
3959 return false;
3962 if (ar->type == AR_FULL)
3963 return true;
3965 if (as->rank != ar->dimen)
3967 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3968 &ar->where, ar->dimen, as->rank);
3969 return false;
3972 /* ar->codimen == 0 is a local array. */
3973 if (as->corank != ar->codimen && ar->codimen != 0)
3975 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
3976 &ar->where, ar->codimen, as->corank);
3977 return false;
3980 for (i = 0; i < as->rank; i++)
3981 if (!check_dimension (i, ar, as))
3982 return false;
3984 /* Local access has no coarray spec. */
3985 if (ar->codimen != 0)
3986 for (i = as->rank; i < as->rank + as->corank; i++)
3988 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
3989 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
3991 gfc_error ("Coindex of codimension %d must be a scalar at %L",
3992 i + 1 - as->rank, &ar->where);
3993 return false;
3995 if (!check_dimension (i, ar, as))
3996 return false;
3999 return true;
4003 /* Resolve one part of an array index. */
4005 static bool
4006 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4007 int force_index_integer_kind)
4009 gfc_typespec ts;
4011 if (index == NULL)
4012 return true;
4014 if (!gfc_resolve_expr (index))
4015 return false;
4017 if (check_scalar && index->rank != 0)
4019 gfc_error ("Array index at %L must be scalar", &index->where);
4020 return false;
4023 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4025 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4026 &index->where, gfc_basic_typename (index->ts.type));
4027 return false;
4030 if (index->ts.type == BT_REAL)
4031 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4032 &index->where))
4033 return false;
4035 if ((index->ts.kind != gfc_index_integer_kind
4036 && force_index_integer_kind)
4037 || index->ts.type != BT_INTEGER)
4039 gfc_clear_ts (&ts);
4040 ts.type = BT_INTEGER;
4041 ts.kind = gfc_index_integer_kind;
4043 gfc_convert_type_warn (index, &ts, 2, 0);
4046 return true;
4049 /* Resolve one part of an array index. */
4051 bool
4052 gfc_resolve_index (gfc_expr *index, int check_scalar)
4054 return gfc_resolve_index_1 (index, check_scalar, 1);
4057 /* Resolve a dim argument to an intrinsic function. */
4059 bool
4060 gfc_resolve_dim_arg (gfc_expr *dim)
4062 if (dim == NULL)
4063 return true;
4065 if (!gfc_resolve_expr (dim))
4066 return false;
4068 if (dim->rank != 0)
4070 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4071 return false;
4075 if (dim->ts.type != BT_INTEGER)
4077 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4078 return false;
4081 if (dim->ts.kind != gfc_index_integer_kind)
4083 gfc_typespec ts;
4085 gfc_clear_ts (&ts);
4086 ts.type = BT_INTEGER;
4087 ts.kind = gfc_index_integer_kind;
4089 gfc_convert_type_warn (dim, &ts, 2, 0);
4092 return true;
4095 /* Given an expression that contains array references, update those array
4096 references to point to the right array specifications. While this is
4097 filled in during matching, this information is difficult to save and load
4098 in a module, so we take care of it here.
4100 The idea here is that the original array reference comes from the
4101 base symbol. We traverse the list of reference structures, setting
4102 the stored reference to references. Component references can
4103 provide an additional array specification. */
4105 static void
4106 find_array_spec (gfc_expr *e)
4108 gfc_array_spec *as;
4109 gfc_component *c;
4110 gfc_ref *ref;
4112 if (e->symtree->n.sym->ts.type == BT_CLASS)
4113 as = CLASS_DATA (e->symtree->n.sym)->as;
4114 else
4115 as = e->symtree->n.sym->as;
4117 for (ref = e->ref; ref; ref = ref->next)
4118 switch (ref->type)
4120 case REF_ARRAY:
4121 if (as == NULL)
4122 gfc_internal_error ("find_array_spec(): Missing spec");
4124 ref->u.ar.as = as;
4125 as = NULL;
4126 break;
4128 case REF_COMPONENT:
4129 c = ref->u.c.component;
4130 if (c->attr.dimension)
4132 if (as != NULL)
4133 gfc_internal_error ("find_array_spec(): unused as(1)");
4134 as = c->as;
4137 break;
4139 case REF_SUBSTRING:
4140 break;
4143 if (as != NULL)
4144 gfc_internal_error ("find_array_spec(): unused as(2)");
4148 /* Resolve an array reference. */
4150 static bool
4151 resolve_array_ref (gfc_array_ref *ar)
4153 int i, check_scalar;
4154 gfc_expr *e;
4156 for (i = 0; i < ar->dimen + ar->codimen; i++)
4158 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4160 /* Do not force gfc_index_integer_kind for the start. We can
4161 do fine with any integer kind. This avoids temporary arrays
4162 created for indexing with a vector. */
4163 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4164 return false;
4165 if (!gfc_resolve_index (ar->end[i], check_scalar))
4166 return false;
4167 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4168 return false;
4170 e = ar->start[i];
4172 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4173 switch (e->rank)
4175 case 0:
4176 ar->dimen_type[i] = DIMEN_ELEMENT;
4177 break;
4179 case 1:
4180 ar->dimen_type[i] = DIMEN_VECTOR;
4181 if (e->expr_type == EXPR_VARIABLE
4182 && e->symtree->n.sym->ts.type == BT_DERIVED)
4183 ar->start[i] = gfc_get_parentheses (e);
4184 break;
4186 default:
4187 gfc_error ("Array index at %L is an array of rank %d",
4188 &ar->c_where[i], e->rank);
4189 return false;
4192 /* Fill in the upper bound, which may be lower than the
4193 specified one for something like a(2:10:5), which is
4194 identical to a(2:7:5). Only relevant for strides not equal
4195 to one. Don't try a division by zero. */
4196 if (ar->dimen_type[i] == DIMEN_RANGE
4197 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4198 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4199 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4201 mpz_t size, end;
4203 if (gfc_ref_dimen_size (ar, i, &size, &end))
4205 if (ar->end[i] == NULL)
4207 ar->end[i] =
4208 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4209 &ar->where);
4210 mpz_set (ar->end[i]->value.integer, end);
4212 else if (ar->end[i]->ts.type == BT_INTEGER
4213 && ar->end[i]->expr_type == EXPR_CONSTANT)
4215 mpz_set (ar->end[i]->value.integer, end);
4217 else
4218 gcc_unreachable ();
4220 mpz_clear (size);
4221 mpz_clear (end);
4226 if (ar->type == AR_FULL)
4228 if (ar->as->rank == 0)
4229 ar->type = AR_ELEMENT;
4231 /* Make sure array is the same as array(:,:), this way
4232 we don't need to special case all the time. */
4233 ar->dimen = ar->as->rank;
4234 for (i = 0; i < ar->dimen; i++)
4236 ar->dimen_type[i] = DIMEN_RANGE;
4238 gcc_assert (ar->start[i] == NULL);
4239 gcc_assert (ar->end[i] == NULL);
4240 gcc_assert (ar->stride[i] == NULL);
4244 /* If the reference type is unknown, figure out what kind it is. */
4246 if (ar->type == AR_UNKNOWN)
4248 ar->type = AR_ELEMENT;
4249 for (i = 0; i < ar->dimen; i++)
4250 if (ar->dimen_type[i] == DIMEN_RANGE
4251 || ar->dimen_type[i] == DIMEN_VECTOR)
4253 ar->type = AR_SECTION;
4254 break;
4258 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4259 return false;
4261 if (ar->as->corank && ar->codimen == 0)
4263 int n;
4264 ar->codimen = ar->as->corank;
4265 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4266 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4269 return true;
4273 static bool
4274 resolve_substring (gfc_ref *ref)
4276 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4278 if (ref->u.ss.start != NULL)
4280 if (!gfc_resolve_expr (ref->u.ss.start))
4281 return false;
4283 if (ref->u.ss.start->ts.type != BT_INTEGER)
4285 gfc_error ("Substring start index at %L must be of type INTEGER",
4286 &ref->u.ss.start->where);
4287 return false;
4290 if (ref->u.ss.start->rank != 0)
4292 gfc_error ("Substring start index at %L must be scalar",
4293 &ref->u.ss.start->where);
4294 return false;
4297 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4298 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4299 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4301 gfc_error ("Substring start index at %L is less than one",
4302 &ref->u.ss.start->where);
4303 return false;
4307 if (ref->u.ss.end != NULL)
4309 if (!gfc_resolve_expr (ref->u.ss.end))
4310 return false;
4312 if (ref->u.ss.end->ts.type != BT_INTEGER)
4314 gfc_error ("Substring end index at %L must be of type INTEGER",
4315 &ref->u.ss.end->where);
4316 return false;
4319 if (ref->u.ss.end->rank != 0)
4321 gfc_error ("Substring end index at %L must be scalar",
4322 &ref->u.ss.end->where);
4323 return false;
4326 if (ref->u.ss.length != NULL
4327 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4328 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4329 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4331 gfc_error ("Substring end index at %L exceeds the string length",
4332 &ref->u.ss.start->where);
4333 return false;
4336 if (compare_bound_mpz_t (ref->u.ss.end,
4337 gfc_integer_kinds[k].huge) == CMP_GT
4338 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4339 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4341 gfc_error ("Substring end index at %L is too large",
4342 &ref->u.ss.end->where);
4343 return false;
4347 return true;
4351 /* This function supplies missing substring charlens. */
4353 void
4354 gfc_resolve_substring_charlen (gfc_expr *e)
4356 gfc_ref *char_ref;
4357 gfc_expr *start, *end;
4359 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4360 if (char_ref->type == REF_SUBSTRING)
4361 break;
4363 if (!char_ref)
4364 return;
4366 gcc_assert (char_ref->next == NULL);
4368 if (e->ts.u.cl)
4370 if (e->ts.u.cl->length)
4371 gfc_free_expr (e->ts.u.cl->length);
4372 else if (e->expr_type == EXPR_VARIABLE
4373 && e->symtree->n.sym->attr.dummy)
4374 return;
4377 e->ts.type = BT_CHARACTER;
4378 e->ts.kind = gfc_default_character_kind;
4380 if (!e->ts.u.cl)
4381 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4383 if (char_ref->u.ss.start)
4384 start = gfc_copy_expr (char_ref->u.ss.start);
4385 else
4386 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4388 if (char_ref->u.ss.end)
4389 end = gfc_copy_expr (char_ref->u.ss.end);
4390 else if (e->expr_type == EXPR_VARIABLE)
4391 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4392 else
4393 end = NULL;
4395 if (!start || !end)
4397 gfc_free_expr (start);
4398 gfc_free_expr (end);
4399 return;
4402 /* Length = (end - start +1). */
4403 e->ts.u.cl->length = gfc_subtract (end, start);
4404 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4405 gfc_get_int_expr (gfc_default_integer_kind,
4406 NULL, 1));
4408 e->ts.u.cl->length->ts.type = BT_INTEGER;
4409 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4411 /* Make sure that the length is simplified. */
4412 gfc_simplify_expr (e->ts.u.cl->length, 1);
4413 gfc_resolve_expr (e->ts.u.cl->length);
4417 /* Resolve subtype references. */
4419 static bool
4420 resolve_ref (gfc_expr *expr)
4422 int current_part_dimension, n_components, seen_part_dimension;
4423 gfc_ref *ref;
4425 for (ref = expr->ref; ref; ref = ref->next)
4426 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4428 find_array_spec (expr);
4429 break;
4432 for (ref = expr->ref; ref; ref = ref->next)
4433 switch (ref->type)
4435 case REF_ARRAY:
4436 if (!resolve_array_ref (&ref->u.ar))
4437 return false;
4438 break;
4440 case REF_COMPONENT:
4441 break;
4443 case REF_SUBSTRING:
4444 if (!resolve_substring (ref))
4445 return false;
4446 break;
4449 /* Check constraints on part references. */
4451 current_part_dimension = 0;
4452 seen_part_dimension = 0;
4453 n_components = 0;
4455 for (ref = expr->ref; ref; ref = ref->next)
4457 switch (ref->type)
4459 case REF_ARRAY:
4460 switch (ref->u.ar.type)
4462 case AR_FULL:
4463 /* Coarray scalar. */
4464 if (ref->u.ar.as->rank == 0)
4466 current_part_dimension = 0;
4467 break;
4469 /* Fall through. */
4470 case AR_SECTION:
4471 current_part_dimension = 1;
4472 break;
4474 case AR_ELEMENT:
4475 current_part_dimension = 0;
4476 break;
4478 case AR_UNKNOWN:
4479 gfc_internal_error ("resolve_ref(): Bad array reference");
4482 break;
4484 case REF_COMPONENT:
4485 if (current_part_dimension || seen_part_dimension)
4487 /* F03:C614. */
4488 if (ref->u.c.component->attr.pointer
4489 || ref->u.c.component->attr.proc_pointer
4490 || (ref->u.c.component->ts.type == BT_CLASS
4491 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4493 gfc_error ("Component to the right of a part reference "
4494 "with nonzero rank must not have the POINTER "
4495 "attribute at %L", &expr->where);
4496 return false;
4498 else if (ref->u.c.component->attr.allocatable
4499 || (ref->u.c.component->ts.type == BT_CLASS
4500 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4503 gfc_error ("Component to the right of a part reference "
4504 "with nonzero rank must not have the ALLOCATABLE "
4505 "attribute at %L", &expr->where);
4506 return false;
4510 n_components++;
4511 break;
4513 case REF_SUBSTRING:
4514 break;
4517 if (((ref->type == REF_COMPONENT && n_components > 1)
4518 || ref->next == NULL)
4519 && current_part_dimension
4520 && seen_part_dimension)
4522 gfc_error ("Two or more part references with nonzero rank must "
4523 "not be specified at %L", &expr->where);
4524 return false;
4527 if (ref->type == REF_COMPONENT)
4529 if (current_part_dimension)
4530 seen_part_dimension = 1;
4532 /* reset to make sure */
4533 current_part_dimension = 0;
4537 return true;
4541 /* Given an expression, determine its shape. This is easier than it sounds.
4542 Leaves the shape array NULL if it is not possible to determine the shape. */
4544 static void
4545 expression_shape (gfc_expr *e)
4547 mpz_t array[GFC_MAX_DIMENSIONS];
4548 int i;
4550 if (e->rank <= 0 || e->shape != NULL)
4551 return;
4553 for (i = 0; i < e->rank; i++)
4554 if (!gfc_array_dimen_size (e, i, &array[i]))
4555 goto fail;
4557 e->shape = gfc_get_shape (e->rank);
4559 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4561 return;
4563 fail:
4564 for (i--; i >= 0; i--)
4565 mpz_clear (array[i]);
4569 /* Given a variable expression node, compute the rank of the expression by
4570 examining the base symbol and any reference structures it may have. */
4572 static void
4573 expression_rank (gfc_expr *e)
4575 gfc_ref *ref;
4576 int i, rank;
4578 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4579 could lead to serious confusion... */
4580 gcc_assert (e->expr_type != EXPR_COMPCALL);
4582 if (e->ref == NULL)
4584 if (e->expr_type == EXPR_ARRAY)
4585 goto done;
4586 /* Constructors can have a rank different from one via RESHAPE(). */
4588 if (e->symtree == NULL)
4590 e->rank = 0;
4591 goto done;
4594 e->rank = (e->symtree->n.sym->as == NULL)
4595 ? 0 : e->symtree->n.sym->as->rank;
4596 goto done;
4599 rank = 0;
4601 for (ref = e->ref; ref; ref = ref->next)
4603 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4604 && ref->u.c.component->attr.function && !ref->next)
4605 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4607 if (ref->type != REF_ARRAY)
4608 continue;
4610 if (ref->u.ar.type == AR_FULL)
4612 rank = ref->u.ar.as->rank;
4613 break;
4616 if (ref->u.ar.type == AR_SECTION)
4618 /* Figure out the rank of the section. */
4619 if (rank != 0)
4620 gfc_internal_error ("expression_rank(): Two array specs");
4622 for (i = 0; i < ref->u.ar.dimen; i++)
4623 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4624 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4625 rank++;
4627 break;
4631 e->rank = rank;
4633 done:
4634 expression_shape (e);
4638 /* Resolve a variable expression. */
4640 static bool
4641 resolve_variable (gfc_expr *e)
4643 gfc_symbol *sym;
4644 bool t;
4646 t = true;
4648 if (e->symtree == NULL)
4649 return false;
4650 sym = e->symtree->n.sym;
4652 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4653 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4654 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4656 if (!actual_arg || inquiry_argument)
4658 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4659 "be used as actual argument", sym->name, &e->where);
4660 return false;
4663 /* TS 29113, 407b. */
4664 else if (e->ts.type == BT_ASSUMED)
4666 if (!actual_arg)
4668 gfc_error ("Assumed-type variable %s at %L may only be used "
4669 "as actual argument", sym->name, &e->where);
4670 return false;
4672 else if (inquiry_argument && !first_actual_arg)
4674 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4675 for all inquiry functions in resolve_function; the reason is
4676 that the function-name resolution happens too late in that
4677 function. */
4678 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4679 "an inquiry function shall be the first argument",
4680 sym->name, &e->where);
4681 return false;
4684 /* TS 29113, C535b. */
4685 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4686 && CLASS_DATA (sym)->as
4687 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4688 || (sym->ts.type != BT_CLASS && sym->as
4689 && sym->as->type == AS_ASSUMED_RANK))
4691 if (!actual_arg)
4693 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4694 "actual argument", sym->name, &e->where);
4695 return false;
4697 else if (inquiry_argument && !first_actual_arg)
4699 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4700 for all inquiry functions in resolve_function; the reason is
4701 that the function-name resolution happens too late in that
4702 function. */
4703 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4704 "to an inquiry function shall be the first argument",
4705 sym->name, &e->where);
4706 return false;
4710 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4711 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4712 && e->ref->next == NULL))
4714 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4715 "a subobject reference", sym->name, &e->ref->u.ar.where);
4716 return false;
4718 /* TS 29113, 407b. */
4719 else if (e->ts.type == BT_ASSUMED && e->ref
4720 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4721 && e->ref->next == NULL))
4723 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4724 "reference", sym->name, &e->ref->u.ar.where);
4725 return false;
4728 /* TS 29113, C535b. */
4729 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4730 && CLASS_DATA (sym)->as
4731 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4732 || (sym->ts.type != BT_CLASS && sym->as
4733 && sym->as->type == AS_ASSUMED_RANK))
4734 && e->ref
4735 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4736 && e->ref->next == NULL))
4738 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4739 "reference", sym->name, &e->ref->u.ar.where);
4740 return false;
4744 /* If this is an associate-name, it may be parsed with an array reference
4745 in error even though the target is scalar. Fail directly in this case.
4746 TODO Understand why class scalar expressions must be excluded. */
4747 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4749 if (sym->ts.type == BT_CLASS)
4750 gfc_fix_class_refs (e);
4751 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4752 return false;
4755 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4756 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4758 /* On the other hand, the parser may not have known this is an array;
4759 in this case, we have to add a FULL reference. */
4760 if (sym->assoc && sym->attr.dimension && !e->ref)
4762 e->ref = gfc_get_ref ();
4763 e->ref->type = REF_ARRAY;
4764 e->ref->u.ar.type = AR_FULL;
4765 e->ref->u.ar.dimen = 0;
4768 if (e->ref && !resolve_ref (e))
4769 return false;
4771 if (sym->attr.flavor == FL_PROCEDURE
4772 && (!sym->attr.function
4773 || (sym->attr.function && sym->result
4774 && sym->result->attr.proc_pointer
4775 && !sym->result->attr.function)))
4777 e->ts.type = BT_PROCEDURE;
4778 goto resolve_procedure;
4781 if (sym->ts.type != BT_UNKNOWN)
4782 gfc_variable_attr (e, &e->ts);
4783 else
4785 /* Must be a simple variable reference. */
4786 if (!gfc_set_default_type (sym, 1, sym->ns))
4787 return false;
4788 e->ts = sym->ts;
4791 if (check_assumed_size_reference (sym, e))
4792 return false;
4794 /* Deal with forward references to entries during resolve_code, to
4795 satisfy, at least partially, 12.5.2.5. */
4796 if (gfc_current_ns->entries
4797 && current_entry_id == sym->entry_id
4798 && cs_base
4799 && cs_base->current
4800 && cs_base->current->op != EXEC_ENTRY)
4802 gfc_entry_list *entry;
4803 gfc_formal_arglist *formal;
4804 int n;
4805 bool seen, saved_specification_expr;
4807 /* If the symbol is a dummy... */
4808 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4810 entry = gfc_current_ns->entries;
4811 seen = false;
4813 /* ...test if the symbol is a parameter of previous entries. */
4814 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4815 for (formal = entry->sym->formal; formal; formal = formal->next)
4817 if (formal->sym && sym->name == formal->sym->name)
4818 seen = true;
4821 /* If it has not been seen as a dummy, this is an error. */
4822 if (!seen)
4824 if (specification_expr)
4825 gfc_error ("Variable '%s', used in a specification expression"
4826 ", is referenced at %L before the ENTRY statement "
4827 "in which it is a parameter",
4828 sym->name, &cs_base->current->loc);
4829 else
4830 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4831 "statement in which it is a parameter",
4832 sym->name, &cs_base->current->loc);
4833 t = false;
4837 /* Now do the same check on the specification expressions. */
4838 saved_specification_expr = specification_expr;
4839 specification_expr = true;
4840 if (sym->ts.type == BT_CHARACTER
4841 && !gfc_resolve_expr (sym->ts.u.cl->length))
4842 t = false;
4844 if (sym->as)
4845 for (n = 0; n < sym->as->rank; n++)
4847 if (!gfc_resolve_expr (sym->as->lower[n]))
4848 t = false;
4849 if (!gfc_resolve_expr (sym->as->upper[n]))
4850 t = false;
4852 specification_expr = saved_specification_expr;
4854 if (t)
4855 /* Update the symbol's entry level. */
4856 sym->entry_id = current_entry_id + 1;
4859 /* If a symbol has been host_associated mark it. This is used latter,
4860 to identify if aliasing is possible via host association. */
4861 if (sym->attr.flavor == FL_VARIABLE
4862 && gfc_current_ns->parent
4863 && (gfc_current_ns->parent == sym->ns
4864 || (gfc_current_ns->parent->parent
4865 && gfc_current_ns->parent->parent == sym->ns)))
4866 sym->attr.host_assoc = 1;
4868 resolve_procedure:
4869 if (t && !resolve_procedure_expression (e))
4870 t = false;
4872 /* F2008, C617 and C1229. */
4873 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4874 && gfc_is_coindexed (e))
4876 gfc_ref *ref, *ref2 = NULL;
4878 for (ref = e->ref; ref; ref = ref->next)
4880 if (ref->type == REF_COMPONENT)
4881 ref2 = ref;
4882 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4883 break;
4886 for ( ; ref; ref = ref->next)
4887 if (ref->type == REF_COMPONENT)
4888 break;
4890 /* Expression itself is not coindexed object. */
4891 if (ref && e->ts.type == BT_CLASS)
4893 gfc_error ("Polymorphic subobject of coindexed object at %L",
4894 &e->where);
4895 t = false;
4898 /* Expression itself is coindexed object. */
4899 if (ref == NULL)
4901 gfc_component *c;
4902 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4903 for ( ; c; c = c->next)
4904 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4906 gfc_error ("Coindexed object with polymorphic allocatable "
4907 "subcomponent at %L", &e->where);
4908 t = false;
4909 break;
4914 return t;
4918 /* Checks to see that the correct symbol has been host associated.
4919 The only situation where this arises is that in which a twice
4920 contained function is parsed after the host association is made.
4921 Therefore, on detecting this, change the symbol in the expression
4922 and convert the array reference into an actual arglist if the old
4923 symbol is a variable. */
4924 static bool
4925 check_host_association (gfc_expr *e)
4927 gfc_symbol *sym, *old_sym;
4928 gfc_symtree *st;
4929 int n;
4930 gfc_ref *ref;
4931 gfc_actual_arglist *arg, *tail = NULL;
4932 bool retval = e->expr_type == EXPR_FUNCTION;
4934 /* If the expression is the result of substitution in
4935 interface.c(gfc_extend_expr) because there is no way in
4936 which the host association can be wrong. */
4937 if (e->symtree == NULL
4938 || e->symtree->n.sym == NULL
4939 || e->user_operator)
4940 return retval;
4942 old_sym = e->symtree->n.sym;
4944 if (gfc_current_ns->parent
4945 && old_sym->ns != gfc_current_ns)
4947 /* Use the 'USE' name so that renamed module symbols are
4948 correctly handled. */
4949 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4951 if (sym && old_sym != sym
4952 && sym->ts.type == old_sym->ts.type
4953 && sym->attr.flavor == FL_PROCEDURE
4954 && sym->attr.contained)
4956 /* Clear the shape, since it might not be valid. */
4957 gfc_free_shape (&e->shape, e->rank);
4959 /* Give the expression the right symtree! */
4960 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4961 gcc_assert (st != NULL);
4963 if (old_sym->attr.flavor == FL_PROCEDURE
4964 || e->expr_type == EXPR_FUNCTION)
4966 /* Original was function so point to the new symbol, since
4967 the actual argument list is already attached to the
4968 expression. */
4969 e->value.function.esym = NULL;
4970 e->symtree = st;
4972 else
4974 /* Original was variable so convert array references into
4975 an actual arglist. This does not need any checking now
4976 since resolve_function will take care of it. */
4977 e->value.function.actual = NULL;
4978 e->expr_type = EXPR_FUNCTION;
4979 e->symtree = st;
4981 /* Ambiguity will not arise if the array reference is not
4982 the last reference. */
4983 for (ref = e->ref; ref; ref = ref->next)
4984 if (ref->type == REF_ARRAY && ref->next == NULL)
4985 break;
4987 gcc_assert (ref->type == REF_ARRAY);
4989 /* Grab the start expressions from the array ref and
4990 copy them into actual arguments. */
4991 for (n = 0; n < ref->u.ar.dimen; n++)
4993 arg = gfc_get_actual_arglist ();
4994 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4995 if (e->value.function.actual == NULL)
4996 tail = e->value.function.actual = arg;
4997 else
4999 tail->next = arg;
5000 tail = arg;
5004 /* Dump the reference list and set the rank. */
5005 gfc_free_ref_list (e->ref);
5006 e->ref = NULL;
5007 e->rank = sym->as ? sym->as->rank : 0;
5010 gfc_resolve_expr (e);
5011 sym->refs++;
5014 /* This might have changed! */
5015 return e->expr_type == EXPR_FUNCTION;
5019 static void
5020 gfc_resolve_character_operator (gfc_expr *e)
5022 gfc_expr *op1 = e->value.op.op1;
5023 gfc_expr *op2 = e->value.op.op2;
5024 gfc_expr *e1 = NULL;
5025 gfc_expr *e2 = NULL;
5027 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5029 if (op1->ts.u.cl && op1->ts.u.cl->length)
5030 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5031 else if (op1->expr_type == EXPR_CONSTANT)
5032 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5033 op1->value.character.length);
5035 if (op2->ts.u.cl && op2->ts.u.cl->length)
5036 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5037 else if (op2->expr_type == EXPR_CONSTANT)
5038 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5039 op2->value.character.length);
5041 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5043 if (!e1 || !e2)
5045 gfc_free_expr (e1);
5046 gfc_free_expr (e2);
5048 return;
5051 e->ts.u.cl->length = gfc_add (e1, e2);
5052 e->ts.u.cl->length->ts.type = BT_INTEGER;
5053 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5054 gfc_simplify_expr (e->ts.u.cl->length, 0);
5055 gfc_resolve_expr (e->ts.u.cl->length);
5057 return;
5061 /* Ensure that an character expression has a charlen and, if possible, a
5062 length expression. */
5064 static void
5065 fixup_charlen (gfc_expr *e)
5067 /* The cases fall through so that changes in expression type and the need
5068 for multiple fixes are picked up. In all circumstances, a charlen should
5069 be available for the middle end to hang a backend_decl on. */
5070 switch (e->expr_type)
5072 case EXPR_OP:
5073 gfc_resolve_character_operator (e);
5075 case EXPR_ARRAY:
5076 if (e->expr_type == EXPR_ARRAY)
5077 gfc_resolve_character_array_constructor (e);
5079 case EXPR_SUBSTRING:
5080 if (!e->ts.u.cl && e->ref)
5081 gfc_resolve_substring_charlen (e);
5083 default:
5084 if (!e->ts.u.cl)
5085 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5087 break;
5092 /* Update an actual argument to include the passed-object for type-bound
5093 procedures at the right position. */
5095 static gfc_actual_arglist*
5096 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5097 const char *name)
5099 gcc_assert (argpos > 0);
5101 if (argpos == 1)
5103 gfc_actual_arglist* result;
5105 result = gfc_get_actual_arglist ();
5106 result->expr = po;
5107 result->next = lst;
5108 if (name)
5109 result->name = name;
5111 return result;
5114 if (lst)
5115 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5116 else
5117 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5118 return lst;
5122 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5124 static gfc_expr*
5125 extract_compcall_passed_object (gfc_expr* e)
5127 gfc_expr* po;
5129 gcc_assert (e->expr_type == EXPR_COMPCALL);
5131 if (e->value.compcall.base_object)
5132 po = gfc_copy_expr (e->value.compcall.base_object);
5133 else
5135 po = gfc_get_expr ();
5136 po->expr_type = EXPR_VARIABLE;
5137 po->symtree = e->symtree;
5138 po->ref = gfc_copy_ref (e->ref);
5139 po->where = e->where;
5142 if (!gfc_resolve_expr (po))
5143 return NULL;
5145 return po;
5149 /* Update the arglist of an EXPR_COMPCALL expression to include the
5150 passed-object. */
5152 static bool
5153 update_compcall_arglist (gfc_expr* e)
5155 gfc_expr* po;
5156 gfc_typebound_proc* tbp;
5158 tbp = e->value.compcall.tbp;
5160 if (tbp->error)
5161 return false;
5163 po = extract_compcall_passed_object (e);
5164 if (!po)
5165 return false;
5167 if (tbp->nopass || e->value.compcall.ignore_pass)
5169 gfc_free_expr (po);
5170 return true;
5173 gcc_assert (tbp->pass_arg_num > 0);
5174 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5175 tbp->pass_arg_num,
5176 tbp->pass_arg);
5178 return true;
5182 /* Extract the passed object from a PPC call (a copy of it). */
5184 static gfc_expr*
5185 extract_ppc_passed_object (gfc_expr *e)
5187 gfc_expr *po;
5188 gfc_ref **ref;
5190 po = gfc_get_expr ();
5191 po->expr_type = EXPR_VARIABLE;
5192 po->symtree = e->symtree;
5193 po->ref = gfc_copy_ref (e->ref);
5194 po->where = e->where;
5196 /* Remove PPC reference. */
5197 ref = &po->ref;
5198 while ((*ref)->next)
5199 ref = &(*ref)->next;
5200 gfc_free_ref_list (*ref);
5201 *ref = NULL;
5203 if (!gfc_resolve_expr (po))
5204 return NULL;
5206 return po;
5210 /* Update the actual arglist of a procedure pointer component to include the
5211 passed-object. */
5213 static bool
5214 update_ppc_arglist (gfc_expr* e)
5216 gfc_expr* po;
5217 gfc_component *ppc;
5218 gfc_typebound_proc* tb;
5220 ppc = gfc_get_proc_ptr_comp (e);
5221 if (!ppc)
5222 return false;
5224 tb = ppc->tb;
5226 if (tb->error)
5227 return false;
5228 else if (tb->nopass)
5229 return true;
5231 po = extract_ppc_passed_object (e);
5232 if (!po)
5233 return false;
5235 /* F08:R739. */
5236 if (po->rank != 0)
5238 gfc_error ("Passed-object at %L must be scalar", &e->where);
5239 return false;
5242 /* F08:C611. */
5243 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5245 gfc_error ("Base object for procedure-pointer component call at %L is of"
5246 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5247 return false;
5250 gcc_assert (tb->pass_arg_num > 0);
5251 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5252 tb->pass_arg_num,
5253 tb->pass_arg);
5255 return true;
5259 /* Check that the object a TBP is called on is valid, i.e. it must not be
5260 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5262 static bool
5263 check_typebound_baseobject (gfc_expr* e)
5265 gfc_expr* base;
5266 bool return_value = false;
5268 base = extract_compcall_passed_object (e);
5269 if (!base)
5270 return false;
5272 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5274 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5275 return false;
5277 /* F08:C611. */
5278 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5280 gfc_error ("Base object for type-bound procedure call at %L is of"
5281 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5282 goto cleanup;
5285 /* F08:C1230. If the procedure called is NOPASS,
5286 the base object must be scalar. */
5287 if (e->value.compcall.tbp->nopass && base->rank != 0)
5289 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5290 " be scalar", &e->where);
5291 goto cleanup;
5294 return_value = true;
5296 cleanup:
5297 gfc_free_expr (base);
5298 return return_value;
5302 /* Resolve a call to a type-bound procedure, either function or subroutine,
5303 statically from the data in an EXPR_COMPCALL expression. The adapted
5304 arglist and the target-procedure symtree are returned. */
5306 static bool
5307 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5308 gfc_actual_arglist** actual)
5310 gcc_assert (e->expr_type == EXPR_COMPCALL);
5311 gcc_assert (!e->value.compcall.tbp->is_generic);
5313 /* Update the actual arglist for PASS. */
5314 if (!update_compcall_arglist (e))
5315 return false;
5317 *actual = e->value.compcall.actual;
5318 *target = e->value.compcall.tbp->u.specific;
5320 gfc_free_ref_list (e->ref);
5321 e->ref = NULL;
5322 e->value.compcall.actual = NULL;
5324 /* If we find a deferred typebound procedure, check for derived types
5325 that an overriding typebound procedure has not been missed. */
5326 if (e->value.compcall.name
5327 && !e->value.compcall.tbp->non_overridable
5328 && e->value.compcall.base_object
5329 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5331 gfc_symtree *st;
5332 gfc_symbol *derived;
5334 /* Use the derived type of the base_object. */
5335 derived = e->value.compcall.base_object->ts.u.derived;
5336 st = NULL;
5338 /* If necessary, go through the inheritance chain. */
5339 while (!st && derived)
5341 /* Look for the typebound procedure 'name'. */
5342 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5343 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5344 e->value.compcall.name);
5345 if (!st)
5346 derived = gfc_get_derived_super_type (derived);
5349 /* Now find the specific name in the derived type namespace. */
5350 if (st && st->n.tb && st->n.tb->u.specific)
5351 gfc_find_sym_tree (st->n.tb->u.specific->name,
5352 derived->ns, 1, &st);
5353 if (st)
5354 *target = st;
5356 return true;
5360 /* Get the ultimate declared type from an expression. In addition,
5361 return the last class/derived type reference and the copy of the
5362 reference list. If check_types is set true, derived types are
5363 identified as well as class references. */
5364 static gfc_symbol*
5365 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5366 gfc_expr *e, bool check_types)
5368 gfc_symbol *declared;
5369 gfc_ref *ref;
5371 declared = NULL;
5372 if (class_ref)
5373 *class_ref = NULL;
5374 if (new_ref)
5375 *new_ref = gfc_copy_ref (e->ref);
5377 for (ref = e->ref; ref; ref = ref->next)
5379 if (ref->type != REF_COMPONENT)
5380 continue;
5382 if ((ref->u.c.component->ts.type == BT_CLASS
5383 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5384 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5386 declared = ref->u.c.component->ts.u.derived;
5387 if (class_ref)
5388 *class_ref = ref;
5392 if (declared == NULL)
5393 declared = e->symtree->n.sym->ts.u.derived;
5395 return declared;
5399 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5400 which of the specific bindings (if any) matches the arglist and transform
5401 the expression into a call of that binding. */
5403 static bool
5404 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5406 gfc_typebound_proc* genproc;
5407 const char* genname;
5408 gfc_symtree *st;
5409 gfc_symbol *derived;
5411 gcc_assert (e->expr_type == EXPR_COMPCALL);
5412 genname = e->value.compcall.name;
5413 genproc = e->value.compcall.tbp;
5415 if (!genproc->is_generic)
5416 return true;
5418 /* Try the bindings on this type and in the inheritance hierarchy. */
5419 for (; genproc; genproc = genproc->overridden)
5421 gfc_tbp_generic* g;
5423 gcc_assert (genproc->is_generic);
5424 for (g = genproc->u.generic; g; g = g->next)
5426 gfc_symbol* target;
5427 gfc_actual_arglist* args;
5428 bool matches;
5430 gcc_assert (g->specific);
5432 if (g->specific->error)
5433 continue;
5435 target = g->specific->u.specific->n.sym;
5437 /* Get the right arglist by handling PASS/NOPASS. */
5438 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5439 if (!g->specific->nopass)
5441 gfc_expr* po;
5442 po = extract_compcall_passed_object (e);
5443 if (!po)
5445 gfc_free_actual_arglist (args);
5446 return false;
5449 gcc_assert (g->specific->pass_arg_num > 0);
5450 gcc_assert (!g->specific->error);
5451 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5452 g->specific->pass_arg);
5454 resolve_actual_arglist (args, target->attr.proc,
5455 is_external_proc (target)
5456 && gfc_sym_get_dummy_args (target) == NULL);
5458 /* Check if this arglist matches the formal. */
5459 matches = gfc_arglist_matches_symbol (&args, target);
5461 /* Clean up and break out of the loop if we've found it. */
5462 gfc_free_actual_arglist (args);
5463 if (matches)
5465 e->value.compcall.tbp = g->specific;
5466 genname = g->specific_st->name;
5467 /* Pass along the name for CLASS methods, where the vtab
5468 procedure pointer component has to be referenced. */
5469 if (name)
5470 *name = genname;
5471 goto success;
5476 /* Nothing matching found! */
5477 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5478 " '%s' at %L", genname, &e->where);
5479 return false;
5481 success:
5482 /* Make sure that we have the right specific instance for the name. */
5483 derived = get_declared_from_expr (NULL, NULL, e, true);
5485 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5486 if (st)
5487 e->value.compcall.tbp = st->n.tb;
5489 return true;
5493 /* Resolve a call to a type-bound subroutine. */
5495 static bool
5496 resolve_typebound_call (gfc_code* c, const char **name)
5498 gfc_actual_arglist* newactual;
5499 gfc_symtree* target;
5501 /* Check that's really a SUBROUTINE. */
5502 if (!c->expr1->value.compcall.tbp->subroutine)
5504 gfc_error ("'%s' at %L should be a SUBROUTINE",
5505 c->expr1->value.compcall.name, &c->loc);
5506 return false;
5509 if (!check_typebound_baseobject (c->expr1))
5510 return false;
5512 /* Pass along the name for CLASS methods, where the vtab
5513 procedure pointer component has to be referenced. */
5514 if (name)
5515 *name = c->expr1->value.compcall.name;
5517 if (!resolve_typebound_generic_call (c->expr1, name))
5518 return false;
5520 /* Transform into an ordinary EXEC_CALL for now. */
5522 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5523 return false;
5525 c->ext.actual = newactual;
5526 c->symtree = target;
5527 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5529 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5531 gfc_free_expr (c->expr1);
5532 c->expr1 = gfc_get_expr ();
5533 c->expr1->expr_type = EXPR_FUNCTION;
5534 c->expr1->symtree = target;
5535 c->expr1->where = c->loc;
5537 return resolve_call (c);
5541 /* Resolve a component-call expression. */
5542 static bool
5543 resolve_compcall (gfc_expr* e, const char **name)
5545 gfc_actual_arglist* newactual;
5546 gfc_symtree* target;
5548 /* Check that's really a FUNCTION. */
5549 if (!e->value.compcall.tbp->function)
5551 gfc_error ("'%s' at %L should be a FUNCTION",
5552 e->value.compcall.name, &e->where);
5553 return false;
5556 /* These must not be assign-calls! */
5557 gcc_assert (!e->value.compcall.assign);
5559 if (!check_typebound_baseobject (e))
5560 return false;
5562 /* Pass along the name for CLASS methods, where the vtab
5563 procedure pointer component has to be referenced. */
5564 if (name)
5565 *name = e->value.compcall.name;
5567 if (!resolve_typebound_generic_call (e, name))
5568 return false;
5569 gcc_assert (!e->value.compcall.tbp->is_generic);
5571 /* Take the rank from the function's symbol. */
5572 if (e->value.compcall.tbp->u.specific->n.sym->as)
5573 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5575 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5576 arglist to the TBP's binding target. */
5578 if (!resolve_typebound_static (e, &target, &newactual))
5579 return false;
5581 e->value.function.actual = newactual;
5582 e->value.function.name = NULL;
5583 e->value.function.esym = target->n.sym;
5584 e->value.function.isym = NULL;
5585 e->symtree = target;
5586 e->ts = target->n.sym->ts;
5587 e->expr_type = EXPR_FUNCTION;
5589 /* Resolution is not necessary if this is a class subroutine; this
5590 function only has to identify the specific proc. Resolution of
5591 the call will be done next in resolve_typebound_call. */
5592 return gfc_resolve_expr (e);
5597 /* Resolve a typebound function, or 'method'. First separate all
5598 the non-CLASS references by calling resolve_compcall directly. */
5600 static bool
5601 resolve_typebound_function (gfc_expr* e)
5603 gfc_symbol *declared;
5604 gfc_component *c;
5605 gfc_ref *new_ref;
5606 gfc_ref *class_ref;
5607 gfc_symtree *st;
5608 const char *name;
5609 gfc_typespec ts;
5610 gfc_expr *expr;
5611 bool overridable;
5613 st = e->symtree;
5615 /* Deal with typebound operators for CLASS objects. */
5616 expr = e->value.compcall.base_object;
5617 overridable = !e->value.compcall.tbp->non_overridable;
5618 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5620 /* If the base_object is not a variable, the corresponding actual
5621 argument expression must be stored in e->base_expression so
5622 that the corresponding tree temporary can be used as the base
5623 object in gfc_conv_procedure_call. */
5624 if (expr->expr_type != EXPR_VARIABLE)
5626 gfc_actual_arglist *args;
5628 for (args= e->value.function.actual; args; args = args->next)
5630 if (expr == args->expr)
5631 expr = args->expr;
5635 /* Since the typebound operators are generic, we have to ensure
5636 that any delays in resolution are corrected and that the vtab
5637 is present. */
5638 ts = expr->ts;
5639 declared = ts.u.derived;
5640 c = gfc_find_component (declared, "_vptr", true, true);
5641 if (c->ts.u.derived == NULL)
5642 c->ts.u.derived = gfc_find_derived_vtab (declared);
5644 if (!resolve_compcall (e, &name))
5645 return false;
5647 /* Use the generic name if it is there. */
5648 name = name ? name : e->value.function.esym->name;
5649 e->symtree = expr->symtree;
5650 e->ref = gfc_copy_ref (expr->ref);
5651 get_declared_from_expr (&class_ref, NULL, e, false);
5653 /* Trim away the extraneous references that emerge from nested
5654 use of interface.c (extend_expr). */
5655 if (class_ref && class_ref->next)
5657 gfc_free_ref_list (class_ref->next);
5658 class_ref->next = NULL;
5660 else if (e->ref && !class_ref)
5662 gfc_free_ref_list (e->ref);
5663 e->ref = NULL;
5666 gfc_add_vptr_component (e);
5667 gfc_add_component_ref (e, name);
5668 e->value.function.esym = NULL;
5669 if (expr->expr_type != EXPR_VARIABLE)
5670 e->base_expr = expr;
5671 return true;
5674 if (st == NULL)
5675 return resolve_compcall (e, NULL);
5677 if (!resolve_ref (e))
5678 return false;
5680 /* Get the CLASS declared type. */
5681 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5683 /* Weed out cases of the ultimate component being a derived type. */
5684 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5685 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5687 gfc_free_ref_list (new_ref);
5688 return resolve_compcall (e, NULL);
5691 c = gfc_find_component (declared, "_data", true, true);
5692 declared = c->ts.u.derived;
5694 /* Treat the call as if it is a typebound procedure, in order to roll
5695 out the correct name for the specific function. */
5696 if (!resolve_compcall (e, &name))
5698 gfc_free_ref_list (new_ref);
5699 return false;
5701 ts = e->ts;
5703 if (overridable)
5705 /* Convert the expression to a procedure pointer component call. */
5706 e->value.function.esym = NULL;
5707 e->symtree = st;
5709 if (new_ref)
5710 e->ref = new_ref;
5712 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5713 gfc_add_vptr_component (e);
5714 gfc_add_component_ref (e, name);
5716 /* Recover the typespec for the expression. This is really only
5717 necessary for generic procedures, where the additional call
5718 to gfc_add_component_ref seems to throw the collection of the
5719 correct typespec. */
5720 e->ts = ts;
5722 else if (new_ref)
5723 gfc_free_ref_list (new_ref);
5725 return true;
5728 /* Resolve a typebound subroutine, or 'method'. First separate all
5729 the non-CLASS references by calling resolve_typebound_call
5730 directly. */
5732 static bool
5733 resolve_typebound_subroutine (gfc_code *code)
5735 gfc_symbol *declared;
5736 gfc_component *c;
5737 gfc_ref *new_ref;
5738 gfc_ref *class_ref;
5739 gfc_symtree *st;
5740 const char *name;
5741 gfc_typespec ts;
5742 gfc_expr *expr;
5743 bool overridable;
5745 st = code->expr1->symtree;
5747 /* Deal with typebound operators for CLASS objects. */
5748 expr = code->expr1->value.compcall.base_object;
5749 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5750 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5752 /* If the base_object is not a variable, the corresponding actual
5753 argument expression must be stored in e->base_expression so
5754 that the corresponding tree temporary can be used as the base
5755 object in gfc_conv_procedure_call. */
5756 if (expr->expr_type != EXPR_VARIABLE)
5758 gfc_actual_arglist *args;
5760 args= code->expr1->value.function.actual;
5761 for (; args; args = args->next)
5762 if (expr == args->expr)
5763 expr = args->expr;
5766 /* Since the typebound operators are generic, we have to ensure
5767 that any delays in resolution are corrected and that the vtab
5768 is present. */
5769 declared = expr->ts.u.derived;
5770 c = gfc_find_component (declared, "_vptr", true, true);
5771 if (c->ts.u.derived == NULL)
5772 c->ts.u.derived = gfc_find_derived_vtab (declared);
5774 if (!resolve_typebound_call (code, &name))
5775 return false;
5777 /* Use the generic name if it is there. */
5778 name = name ? name : code->expr1->value.function.esym->name;
5779 code->expr1->symtree = expr->symtree;
5780 code->expr1->ref = gfc_copy_ref (expr->ref);
5782 /* Trim away the extraneous references that emerge from nested
5783 use of interface.c (extend_expr). */
5784 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5785 if (class_ref && class_ref->next)
5787 gfc_free_ref_list (class_ref->next);
5788 class_ref->next = NULL;
5790 else if (code->expr1->ref && !class_ref)
5792 gfc_free_ref_list (code->expr1->ref);
5793 code->expr1->ref = NULL;
5796 /* Now use the procedure in the vtable. */
5797 gfc_add_vptr_component (code->expr1);
5798 gfc_add_component_ref (code->expr1, name);
5799 code->expr1->value.function.esym = NULL;
5800 if (expr->expr_type != EXPR_VARIABLE)
5801 code->expr1->base_expr = expr;
5802 return true;
5805 if (st == NULL)
5806 return resolve_typebound_call (code, NULL);
5808 if (!resolve_ref (code->expr1))
5809 return false;
5811 /* Get the CLASS declared type. */
5812 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5814 /* Weed out cases of the ultimate component being a derived type. */
5815 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5816 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5818 gfc_free_ref_list (new_ref);
5819 return resolve_typebound_call (code, NULL);
5822 if (!resolve_typebound_call (code, &name))
5824 gfc_free_ref_list (new_ref);
5825 return false;
5827 ts = code->expr1->ts;
5829 if (overridable)
5831 /* Convert the expression to a procedure pointer component call. */
5832 code->expr1->value.function.esym = NULL;
5833 code->expr1->symtree = st;
5835 if (new_ref)
5836 code->expr1->ref = new_ref;
5838 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5839 gfc_add_vptr_component (code->expr1);
5840 gfc_add_component_ref (code->expr1, name);
5842 /* Recover the typespec for the expression. This is really only
5843 necessary for generic procedures, where the additional call
5844 to gfc_add_component_ref seems to throw the collection of the
5845 correct typespec. */
5846 code->expr1->ts = ts;
5848 else if (new_ref)
5849 gfc_free_ref_list (new_ref);
5851 return true;
5855 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5857 static bool
5858 resolve_ppc_call (gfc_code* c)
5860 gfc_component *comp;
5862 comp = gfc_get_proc_ptr_comp (c->expr1);
5863 gcc_assert (comp != NULL);
5865 c->resolved_sym = c->expr1->symtree->n.sym;
5866 c->expr1->expr_type = EXPR_VARIABLE;
5868 if (!comp->attr.subroutine)
5869 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5871 if (!resolve_ref (c->expr1))
5872 return false;
5874 if (!update_ppc_arglist (c->expr1))
5875 return false;
5877 c->ext.actual = c->expr1->value.compcall.actual;
5879 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5880 !(comp->ts.interface
5881 && comp->ts.interface->formal)))
5882 return false;
5884 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5886 return true;
5890 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5892 static bool
5893 resolve_expr_ppc (gfc_expr* e)
5895 gfc_component *comp;
5897 comp = gfc_get_proc_ptr_comp (e);
5898 gcc_assert (comp != NULL);
5900 /* Convert to EXPR_FUNCTION. */
5901 e->expr_type = EXPR_FUNCTION;
5902 e->value.function.isym = NULL;
5903 e->value.function.actual = e->value.compcall.actual;
5904 e->ts = comp->ts;
5905 if (comp->as != NULL)
5906 e->rank = comp->as->rank;
5908 if (!comp->attr.function)
5909 gfc_add_function (&comp->attr, comp->name, &e->where);
5911 if (!resolve_ref (e))
5912 return false;
5914 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5915 !(comp->ts.interface
5916 && comp->ts.interface->formal)))
5917 return false;
5919 if (!update_ppc_arglist (e))
5920 return false;
5922 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5924 return true;
5928 static bool
5929 gfc_is_expandable_expr (gfc_expr *e)
5931 gfc_constructor *con;
5933 if (e->expr_type == EXPR_ARRAY)
5935 /* Traverse the constructor looking for variables that are flavor
5936 parameter. Parameters must be expanded since they are fully used at
5937 compile time. */
5938 con = gfc_constructor_first (e->value.constructor);
5939 for (; con; con = gfc_constructor_next (con))
5941 if (con->expr->expr_type == EXPR_VARIABLE
5942 && con->expr->symtree
5943 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5944 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5945 return true;
5946 if (con->expr->expr_type == EXPR_ARRAY
5947 && gfc_is_expandable_expr (con->expr))
5948 return true;
5952 return false;
5955 /* Resolve an expression. That is, make sure that types of operands agree
5956 with their operators, intrinsic operators are converted to function calls
5957 for overloaded types and unresolved function references are resolved. */
5959 bool
5960 gfc_resolve_expr (gfc_expr *e)
5962 bool t;
5963 bool inquiry_save, actual_arg_save, first_actual_arg_save;
5965 if (e == NULL)
5966 return true;
5968 /* inquiry_argument only applies to variables. */
5969 inquiry_save = inquiry_argument;
5970 actual_arg_save = actual_arg;
5971 first_actual_arg_save = first_actual_arg;
5973 if (e->expr_type != EXPR_VARIABLE)
5975 inquiry_argument = false;
5976 actual_arg = false;
5977 first_actual_arg = false;
5980 switch (e->expr_type)
5982 case EXPR_OP:
5983 t = resolve_operator (e);
5984 break;
5986 case EXPR_FUNCTION:
5987 case EXPR_VARIABLE:
5989 if (check_host_association (e))
5990 t = resolve_function (e);
5991 else
5993 t = resolve_variable (e);
5994 if (t)
5995 expression_rank (e);
5998 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5999 && e->ref->type != REF_SUBSTRING)
6000 gfc_resolve_substring_charlen (e);
6002 break;
6004 case EXPR_COMPCALL:
6005 t = resolve_typebound_function (e);
6006 break;
6008 case EXPR_SUBSTRING:
6009 t = resolve_ref (e);
6010 break;
6012 case EXPR_CONSTANT:
6013 case EXPR_NULL:
6014 t = true;
6015 break;
6017 case EXPR_PPC:
6018 t = resolve_expr_ppc (e);
6019 break;
6021 case EXPR_ARRAY:
6022 t = false;
6023 if (!resolve_ref (e))
6024 break;
6026 t = gfc_resolve_array_constructor (e);
6027 /* Also try to expand a constructor. */
6028 if (t)
6030 expression_rank (e);
6031 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6032 gfc_expand_constructor (e, false);
6035 /* This provides the opportunity for the length of constructors with
6036 character valued function elements to propagate the string length
6037 to the expression. */
6038 if (t && e->ts.type == BT_CHARACTER)
6040 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6041 here rather then add a duplicate test for it above. */
6042 gfc_expand_constructor (e, false);
6043 t = gfc_resolve_character_array_constructor (e);
6046 break;
6048 case EXPR_STRUCTURE:
6049 t = resolve_ref (e);
6050 if (!t)
6051 break;
6053 t = resolve_structure_cons (e, 0);
6054 if (!t)
6055 break;
6057 t = gfc_simplify_expr (e, 0);
6058 break;
6060 default:
6061 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6064 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6065 fixup_charlen (e);
6067 inquiry_argument = inquiry_save;
6068 actual_arg = actual_arg_save;
6069 first_actual_arg = first_actual_arg_save;
6071 return t;
6075 /* Resolve an expression from an iterator. They must be scalar and have
6076 INTEGER or (optionally) REAL type. */
6078 static bool
6079 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6080 const char *name_msgid)
6082 if (!gfc_resolve_expr (expr))
6083 return false;
6085 if (expr->rank != 0)
6087 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6088 return false;
6091 if (expr->ts.type != BT_INTEGER)
6093 if (expr->ts.type == BT_REAL)
6095 if (real_ok)
6096 return gfc_notify_std (GFC_STD_F95_DEL,
6097 "%s at %L must be integer",
6098 _(name_msgid), &expr->where);
6099 else
6101 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6102 &expr->where);
6103 return false;
6106 else
6108 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6109 return false;
6112 return true;
6116 /* Resolve the expressions in an iterator structure. If REAL_OK is
6117 false allow only INTEGER type iterators, otherwise allow REAL types.
6118 Set own_scope to true for ac-implied-do and data-implied-do as those
6119 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6121 bool
6122 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6124 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6125 return false;
6127 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6128 _("iterator variable")))
6129 return false;
6131 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6132 "Start expression in DO loop"))
6133 return false;
6135 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6136 "End expression in DO loop"))
6137 return false;
6139 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6140 "Step expression in DO loop"))
6141 return false;
6143 if (iter->step->expr_type == EXPR_CONSTANT)
6145 if ((iter->step->ts.type == BT_INTEGER
6146 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6147 || (iter->step->ts.type == BT_REAL
6148 && mpfr_sgn (iter->step->value.real) == 0))
6150 gfc_error ("Step expression in DO loop at %L cannot be zero",
6151 &iter->step->where);
6152 return false;
6156 /* Convert start, end, and step to the same type as var. */
6157 if (iter->start->ts.kind != iter->var->ts.kind
6158 || iter->start->ts.type != iter->var->ts.type)
6159 gfc_convert_type (iter->start, &iter->var->ts, 2);
6161 if (iter->end->ts.kind != iter->var->ts.kind
6162 || iter->end->ts.type != iter->var->ts.type)
6163 gfc_convert_type (iter->end, &iter->var->ts, 2);
6165 if (iter->step->ts.kind != iter->var->ts.kind
6166 || iter->step->ts.type != iter->var->ts.type)
6167 gfc_convert_type (iter->step, &iter->var->ts, 2);
6169 if (iter->start->expr_type == EXPR_CONSTANT
6170 && iter->end->expr_type == EXPR_CONSTANT
6171 && iter->step->expr_type == EXPR_CONSTANT)
6173 int sgn, cmp;
6174 if (iter->start->ts.type == BT_INTEGER)
6176 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6177 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6179 else
6181 sgn = mpfr_sgn (iter->step->value.real);
6182 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6184 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6185 gfc_warning ("DO loop at %L will be executed zero times",
6186 &iter->step->where);
6189 return true;
6193 /* Traversal function for find_forall_index. f == 2 signals that
6194 that variable itself is not to be checked - only the references. */
6196 static bool
6197 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6199 if (expr->expr_type != EXPR_VARIABLE)
6200 return false;
6202 /* A scalar assignment */
6203 if (!expr->ref || *f == 1)
6205 if (expr->symtree->n.sym == sym)
6206 return true;
6207 else
6208 return false;
6211 if (*f == 2)
6212 *f = 1;
6213 return false;
6217 /* Check whether the FORALL index appears in the expression or not.
6218 Returns true if SYM is found in EXPR. */
6220 bool
6221 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6223 if (gfc_traverse_expr (expr, sym, forall_index, f))
6224 return true;
6225 else
6226 return false;
6230 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6231 to be a scalar INTEGER variable. The subscripts and stride are scalar
6232 INTEGERs, and if stride is a constant it must be nonzero.
6233 Furthermore "A subscript or stride in a forall-triplet-spec shall
6234 not contain a reference to any index-name in the
6235 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6237 static void
6238 resolve_forall_iterators (gfc_forall_iterator *it)
6240 gfc_forall_iterator *iter, *iter2;
6242 for (iter = it; iter; iter = iter->next)
6244 if (gfc_resolve_expr (iter->var)
6245 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6246 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6247 &iter->var->where);
6249 if (gfc_resolve_expr (iter->start)
6250 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6251 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6252 &iter->start->where);
6253 if (iter->var->ts.kind != iter->start->ts.kind)
6254 gfc_convert_type (iter->start, &iter->var->ts, 1);
6256 if (gfc_resolve_expr (iter->end)
6257 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6258 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6259 &iter->end->where);
6260 if (iter->var->ts.kind != iter->end->ts.kind)
6261 gfc_convert_type (iter->end, &iter->var->ts, 1);
6263 if (gfc_resolve_expr (iter->stride))
6265 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6266 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6267 &iter->stride->where, "INTEGER");
6269 if (iter->stride->expr_type == EXPR_CONSTANT
6270 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6271 gfc_error ("FORALL stride expression at %L cannot be zero",
6272 &iter->stride->where);
6274 if (iter->var->ts.kind != iter->stride->ts.kind)
6275 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6278 for (iter = it; iter; iter = iter->next)
6279 for (iter2 = iter; iter2; iter2 = iter2->next)
6281 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6282 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6283 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6284 gfc_error ("FORALL index '%s' may not appear in triplet "
6285 "specification at %L", iter->var->symtree->name,
6286 &iter2->start->where);
6291 /* Given a pointer to a symbol that is a derived type, see if it's
6292 inaccessible, i.e. if it's defined in another module and the components are
6293 PRIVATE. The search is recursive if necessary. Returns zero if no
6294 inaccessible components are found, nonzero otherwise. */
6296 static int
6297 derived_inaccessible (gfc_symbol *sym)
6299 gfc_component *c;
6301 if (sym->attr.use_assoc && sym->attr.private_comp)
6302 return 1;
6304 for (c = sym->components; c; c = c->next)
6306 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6307 return 1;
6310 return 0;
6314 /* Resolve the argument of a deallocate expression. The expression must be
6315 a pointer or a full array. */
6317 static bool
6318 resolve_deallocate_expr (gfc_expr *e)
6320 symbol_attribute attr;
6321 int allocatable, pointer;
6322 gfc_ref *ref;
6323 gfc_symbol *sym;
6324 gfc_component *c;
6325 bool unlimited;
6327 if (!gfc_resolve_expr (e))
6328 return false;
6330 if (e->expr_type != EXPR_VARIABLE)
6331 goto bad;
6333 sym = e->symtree->n.sym;
6334 unlimited = UNLIMITED_POLY(sym);
6336 if (sym->ts.type == BT_CLASS)
6338 allocatable = CLASS_DATA (sym)->attr.allocatable;
6339 pointer = CLASS_DATA (sym)->attr.class_pointer;
6341 else
6343 allocatable = sym->attr.allocatable;
6344 pointer = sym->attr.pointer;
6346 for (ref = e->ref; ref; ref = ref->next)
6348 switch (ref->type)
6350 case REF_ARRAY:
6351 if (ref->u.ar.type != AR_FULL
6352 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6353 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6354 allocatable = 0;
6355 break;
6357 case REF_COMPONENT:
6358 c = ref->u.c.component;
6359 if (c->ts.type == BT_CLASS)
6361 allocatable = CLASS_DATA (c)->attr.allocatable;
6362 pointer = CLASS_DATA (c)->attr.class_pointer;
6364 else
6366 allocatable = c->attr.allocatable;
6367 pointer = c->attr.pointer;
6369 break;
6371 case REF_SUBSTRING:
6372 allocatable = 0;
6373 break;
6377 attr = gfc_expr_attr (e);
6379 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6381 bad:
6382 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6383 &e->where);
6384 return false;
6387 /* F2008, C644. */
6388 if (gfc_is_coindexed (e))
6390 gfc_error ("Coindexed allocatable object at %L", &e->where);
6391 return false;
6394 if (pointer
6395 && !gfc_check_vardef_context (e, true, true, false,
6396 _("DEALLOCATE object")))
6397 return false;
6398 if (!gfc_check_vardef_context (e, false, true, false,
6399 _("DEALLOCATE object")))
6400 return false;
6402 return true;
6406 /* Returns true if the expression e contains a reference to the symbol sym. */
6407 static bool
6408 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6410 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6411 return true;
6413 return false;
6416 bool
6417 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6419 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6423 /* Given the expression node e for an allocatable/pointer of derived type to be
6424 allocated, get the expression node to be initialized afterwards (needed for
6425 derived types with default initializers, and derived types with allocatable
6426 components that need nullification.) */
6428 gfc_expr *
6429 gfc_expr_to_initialize (gfc_expr *e)
6431 gfc_expr *result;
6432 gfc_ref *ref;
6433 int i;
6435 result = gfc_copy_expr (e);
6437 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6438 for (ref = result->ref; ref; ref = ref->next)
6439 if (ref->type == REF_ARRAY && ref->next == NULL)
6441 ref->u.ar.type = AR_FULL;
6443 for (i = 0; i < ref->u.ar.dimen; i++)
6444 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6446 break;
6449 gfc_free_shape (&result->shape, result->rank);
6451 /* Recalculate rank, shape, etc. */
6452 gfc_resolve_expr (result);
6453 return result;
6457 /* If the last ref of an expression is an array ref, return a copy of the
6458 expression with that one removed. Otherwise, a copy of the original
6459 expression. This is used for allocate-expressions and pointer assignment
6460 LHS, where there may be an array specification that needs to be stripped
6461 off when using gfc_check_vardef_context. */
6463 static gfc_expr*
6464 remove_last_array_ref (gfc_expr* e)
6466 gfc_expr* e2;
6467 gfc_ref** r;
6469 e2 = gfc_copy_expr (e);
6470 for (r = &e2->ref; *r; r = &(*r)->next)
6471 if ((*r)->type == REF_ARRAY && !(*r)->next)
6473 gfc_free_ref_list (*r);
6474 *r = NULL;
6475 break;
6478 return e2;
6482 /* Used in resolve_allocate_expr to check that a allocation-object and
6483 a source-expr are conformable. This does not catch all possible
6484 cases; in particular a runtime checking is needed. */
6486 static bool
6487 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6489 gfc_ref *tail;
6490 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6492 /* First compare rank. */
6493 if (tail && e1->rank != tail->u.ar.as->rank)
6495 gfc_error ("Source-expr at %L must be scalar or have the "
6496 "same rank as the allocate-object at %L",
6497 &e1->where, &e2->where);
6498 return false;
6501 if (e1->shape)
6503 int i;
6504 mpz_t s;
6506 mpz_init (s);
6508 for (i = 0; i < e1->rank; i++)
6510 if (tail->u.ar.end[i])
6512 mpz_set (s, tail->u.ar.end[i]->value.integer);
6513 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6514 mpz_add_ui (s, s, 1);
6516 else
6518 mpz_set (s, tail->u.ar.start[i]->value.integer);
6521 if (mpz_cmp (e1->shape[i], s) != 0)
6523 gfc_error ("Source-expr at %L and allocate-object at %L must "
6524 "have the same shape", &e1->where, &e2->where);
6525 mpz_clear (s);
6526 return false;
6530 mpz_clear (s);
6533 return true;
6537 /* Resolve the expression in an ALLOCATE statement, doing the additional
6538 checks to see whether the expression is OK or not. The expression must
6539 have a trailing array reference that gives the size of the array. */
6541 static bool
6542 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6544 int i, pointer, allocatable, dimension, is_abstract;
6545 int codimension;
6546 bool coindexed;
6547 bool unlimited;
6548 symbol_attribute attr;
6549 gfc_ref *ref, *ref2;
6550 gfc_expr *e2;
6551 gfc_array_ref *ar;
6552 gfc_symbol *sym = NULL;
6553 gfc_alloc *a;
6554 gfc_component *c;
6555 bool t;
6557 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6558 checking of coarrays. */
6559 for (ref = e->ref; ref; ref = ref->next)
6560 if (ref->next == NULL)
6561 break;
6563 if (ref && ref->type == REF_ARRAY)
6564 ref->u.ar.in_allocate = true;
6566 if (!gfc_resolve_expr (e))
6567 goto failure;
6569 /* Make sure the expression is allocatable or a pointer. If it is
6570 pointer, the next-to-last reference must be a pointer. */
6572 ref2 = NULL;
6573 if (e->symtree)
6574 sym = e->symtree->n.sym;
6576 /* Check whether ultimate component is abstract and CLASS. */
6577 is_abstract = 0;
6579 /* Is the allocate-object unlimited polymorphic? */
6580 unlimited = UNLIMITED_POLY(e);
6582 if (e->expr_type != EXPR_VARIABLE)
6584 allocatable = 0;
6585 attr = gfc_expr_attr (e);
6586 pointer = attr.pointer;
6587 dimension = attr.dimension;
6588 codimension = attr.codimension;
6590 else
6592 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6594 allocatable = CLASS_DATA (sym)->attr.allocatable;
6595 pointer = CLASS_DATA (sym)->attr.class_pointer;
6596 dimension = CLASS_DATA (sym)->attr.dimension;
6597 codimension = CLASS_DATA (sym)->attr.codimension;
6598 is_abstract = CLASS_DATA (sym)->attr.abstract;
6600 else
6602 allocatable = sym->attr.allocatable;
6603 pointer = sym->attr.pointer;
6604 dimension = sym->attr.dimension;
6605 codimension = sym->attr.codimension;
6608 coindexed = false;
6610 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6612 switch (ref->type)
6614 case REF_ARRAY:
6615 if (ref->u.ar.codimen > 0)
6617 int n;
6618 for (n = ref->u.ar.dimen;
6619 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6620 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6622 coindexed = true;
6623 break;
6627 if (ref->next != NULL)
6628 pointer = 0;
6629 break;
6631 case REF_COMPONENT:
6632 /* F2008, C644. */
6633 if (coindexed)
6635 gfc_error ("Coindexed allocatable object at %L",
6636 &e->where);
6637 goto failure;
6640 c = ref->u.c.component;
6641 if (c->ts.type == BT_CLASS)
6643 allocatable = CLASS_DATA (c)->attr.allocatable;
6644 pointer = CLASS_DATA (c)->attr.class_pointer;
6645 dimension = CLASS_DATA (c)->attr.dimension;
6646 codimension = CLASS_DATA (c)->attr.codimension;
6647 is_abstract = CLASS_DATA (c)->attr.abstract;
6649 else
6651 allocatable = c->attr.allocatable;
6652 pointer = c->attr.pointer;
6653 dimension = c->attr.dimension;
6654 codimension = c->attr.codimension;
6655 is_abstract = c->attr.abstract;
6657 break;
6659 case REF_SUBSTRING:
6660 allocatable = 0;
6661 pointer = 0;
6662 break;
6667 /* Check for F08:C628. */
6668 if (allocatable == 0 && pointer == 0 && !unlimited)
6670 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6671 &e->where);
6672 goto failure;
6675 /* Some checks for the SOURCE tag. */
6676 if (code->expr3)
6678 /* Check F03:C631. */
6679 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6681 gfc_error ("Type of entity at %L is type incompatible with "
6682 "source-expr at %L", &e->where, &code->expr3->where);
6683 goto failure;
6686 /* Check F03:C632 and restriction following Note 6.18. */
6687 if (code->expr3->rank > 0 && !unlimited
6688 && !conformable_arrays (code->expr3, e))
6689 goto failure;
6691 /* Check F03:C633. */
6692 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6694 gfc_error ("The allocate-object at %L and the source-expr at %L "
6695 "shall have the same kind type parameter",
6696 &e->where, &code->expr3->where);
6697 goto failure;
6700 /* Check F2008, C642. */
6701 if (code->expr3->ts.type == BT_DERIVED
6702 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6703 || (code->expr3->ts.u.derived->from_intmod
6704 == INTMOD_ISO_FORTRAN_ENV
6705 && code->expr3->ts.u.derived->intmod_sym_id
6706 == ISOFORTRAN_LOCK_TYPE)))
6708 gfc_error ("The source-expr at %L shall neither be of type "
6709 "LOCK_TYPE nor have a LOCK_TYPE component if "
6710 "allocate-object at %L is a coarray",
6711 &code->expr3->where, &e->where);
6712 goto failure;
6716 /* Check F08:C629. */
6717 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6718 && !code->expr3)
6720 gcc_assert (e->ts.type == BT_CLASS);
6721 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6722 "type-spec or source-expr", sym->name, &e->where);
6723 goto failure;
6726 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6728 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6729 code->ext.alloc.ts.u.cl->length);
6730 if (cmp == 1 || cmp == -1 || cmp == -3)
6732 gfc_error ("Allocating %s at %L with type-spec requires the same "
6733 "character-length parameter as in the declaration",
6734 sym->name, &e->where);
6735 goto failure;
6739 /* In the variable definition context checks, gfc_expr_attr is used
6740 on the expression. This is fooled by the array specification
6741 present in e, thus we have to eliminate that one temporarily. */
6742 e2 = remove_last_array_ref (e);
6743 t = true;
6744 if (t && pointer)
6745 t = gfc_check_vardef_context (e2, true, true, false,
6746 _("ALLOCATE object"));
6747 if (t)
6748 t = gfc_check_vardef_context (e2, false, true, false,
6749 _("ALLOCATE object"));
6750 gfc_free_expr (e2);
6751 if (!t)
6752 goto failure;
6754 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6755 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6757 /* For class arrays, the initialization with SOURCE is done
6758 using _copy and trans_call. It is convenient to exploit that
6759 when the allocated type is different from the declared type but
6760 no SOURCE exists by setting expr3. */
6761 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6763 else if (!code->expr3)
6765 /* Set up default initializer if needed. */
6766 gfc_typespec ts;
6767 gfc_expr *init_e;
6769 if (code->ext.alloc.ts.type == BT_DERIVED)
6770 ts = code->ext.alloc.ts;
6771 else
6772 ts = e->ts;
6774 if (ts.type == BT_CLASS)
6775 ts = ts.u.derived->components->ts;
6777 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6779 gfc_code *init_st = gfc_get_code ();
6780 init_st->loc = code->loc;
6781 init_st->op = EXEC_INIT_ASSIGN;
6782 init_st->expr1 = gfc_expr_to_initialize (e);
6783 init_st->expr2 = init_e;
6784 init_st->next = code->next;
6785 code->next = init_st;
6788 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6790 /* Default initialization via MOLD (non-polymorphic). */
6791 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6792 gfc_resolve_expr (rhs);
6793 gfc_free_expr (code->expr3);
6794 code->expr3 = rhs;
6797 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6799 /* Make sure the vtab symbol is present when
6800 the module variables are generated. */
6801 gfc_typespec ts = e->ts;
6802 if (code->expr3)
6803 ts = code->expr3->ts;
6804 else if (code->ext.alloc.ts.type == BT_DERIVED)
6805 ts = code->ext.alloc.ts;
6807 gfc_find_derived_vtab (ts.u.derived);
6809 if (dimension)
6810 e = gfc_expr_to_initialize (e);
6812 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6814 /* Again, make sure the vtab symbol is present when
6815 the module variables are generated. */
6816 gfc_typespec *ts = NULL;
6817 if (code->expr3)
6818 ts = &code->expr3->ts;
6819 else
6820 ts = &code->ext.alloc.ts;
6822 gcc_assert (ts);
6824 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6825 gfc_find_derived_vtab (ts->u.derived);
6826 else
6827 gfc_find_intrinsic_vtab (ts);
6829 if (dimension)
6830 e = gfc_expr_to_initialize (e);
6833 if (dimension == 0 && codimension == 0)
6834 goto success;
6836 /* Make sure the last reference node is an array specification. */
6838 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6839 || (dimension && ref2->u.ar.dimen == 0))
6841 gfc_error ("Array specification required in ALLOCATE statement "
6842 "at %L", &e->where);
6843 goto failure;
6846 /* Make sure that the array section reference makes sense in the
6847 context of an ALLOCATE specification. */
6849 ar = &ref2->u.ar;
6851 if (codimension)
6852 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6853 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6855 gfc_error ("Coarray specification required in ALLOCATE statement "
6856 "at %L", &e->where);
6857 goto failure;
6860 for (i = 0; i < ar->dimen; i++)
6862 if (ref2->u.ar.type == AR_ELEMENT)
6863 goto check_symbols;
6865 switch (ar->dimen_type[i])
6867 case DIMEN_ELEMENT:
6868 break;
6870 case DIMEN_RANGE:
6871 if (ar->start[i] != NULL
6872 && ar->end[i] != NULL
6873 && ar->stride[i] == NULL)
6874 break;
6876 /* Fall Through... */
6878 case DIMEN_UNKNOWN:
6879 case DIMEN_VECTOR:
6880 case DIMEN_STAR:
6881 case DIMEN_THIS_IMAGE:
6882 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6883 &e->where);
6884 goto failure;
6887 check_symbols:
6888 for (a = code->ext.alloc.list; a; a = a->next)
6890 sym = a->expr->symtree->n.sym;
6892 /* TODO - check derived type components. */
6893 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6894 continue;
6896 if ((ar->start[i] != NULL
6897 && gfc_find_sym_in_expr (sym, ar->start[i]))
6898 || (ar->end[i] != NULL
6899 && gfc_find_sym_in_expr (sym, ar->end[i])))
6901 gfc_error ("'%s' must not appear in the array specification at "
6902 "%L in the same ALLOCATE statement where it is "
6903 "itself allocated", sym->name, &ar->where);
6904 goto failure;
6909 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6911 if (ar->dimen_type[i] == DIMEN_ELEMENT
6912 || ar->dimen_type[i] == DIMEN_RANGE)
6914 if (i == (ar->dimen + ar->codimen - 1))
6916 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6917 "statement at %L", &e->where);
6918 goto failure;
6920 continue;
6923 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6924 && ar->stride[i] == NULL)
6925 break;
6927 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6928 &e->where);
6929 goto failure;
6932 success:
6933 return true;
6935 failure:
6936 return false;
6939 static void
6940 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6942 gfc_expr *stat, *errmsg, *pe, *qe;
6943 gfc_alloc *a, *p, *q;
6945 stat = code->expr1;
6946 errmsg = code->expr2;
6948 /* Check the stat variable. */
6949 if (stat)
6951 gfc_check_vardef_context (stat, false, false, false,
6952 _("STAT variable"));
6954 if ((stat->ts.type != BT_INTEGER
6955 && !(stat->ref && (stat->ref->type == REF_ARRAY
6956 || stat->ref->type == REF_COMPONENT)))
6957 || stat->rank > 0)
6958 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6959 "variable", &stat->where);
6961 for (p = code->ext.alloc.list; p; p = p->next)
6962 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6964 gfc_ref *ref1, *ref2;
6965 bool found = true;
6967 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6968 ref1 = ref1->next, ref2 = ref2->next)
6970 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6971 continue;
6972 if (ref1->u.c.component->name != ref2->u.c.component->name)
6974 found = false;
6975 break;
6979 if (found)
6981 gfc_error ("Stat-variable at %L shall not be %sd within "
6982 "the same %s statement", &stat->where, fcn, fcn);
6983 break;
6988 /* Check the errmsg variable. */
6989 if (errmsg)
6991 if (!stat)
6992 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6993 &errmsg->where);
6995 gfc_check_vardef_context (errmsg, false, false, false,
6996 _("ERRMSG variable"));
6998 if ((errmsg->ts.type != BT_CHARACTER
6999 && !(errmsg->ref
7000 && (errmsg->ref->type == REF_ARRAY
7001 || errmsg->ref->type == REF_COMPONENT)))
7002 || errmsg->rank > 0 )
7003 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7004 "variable", &errmsg->where);
7006 for (p = code->ext.alloc.list; p; p = p->next)
7007 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7009 gfc_ref *ref1, *ref2;
7010 bool found = true;
7012 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7013 ref1 = ref1->next, ref2 = ref2->next)
7015 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7016 continue;
7017 if (ref1->u.c.component->name != ref2->u.c.component->name)
7019 found = false;
7020 break;
7024 if (found)
7026 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7027 "the same %s statement", &errmsg->where, fcn, fcn);
7028 break;
7033 /* Check that an allocate-object appears only once in the statement. */
7035 for (p = code->ext.alloc.list; p; p = p->next)
7037 pe = p->expr;
7038 for (q = p->next; q; q = q->next)
7040 qe = q->expr;
7041 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7043 /* This is a potential collision. */
7044 gfc_ref *pr = pe->ref;
7045 gfc_ref *qr = qe->ref;
7047 /* Follow the references until
7048 a) They start to differ, in which case there is no error;
7049 you can deallocate a%b and a%c in a single statement
7050 b) Both of them stop, which is an error
7051 c) One of them stops, which is also an error. */
7052 while (1)
7054 if (pr == NULL && qr == NULL)
7056 gfc_error ("Allocate-object at %L also appears at %L",
7057 &pe->where, &qe->where);
7058 break;
7060 else if (pr != NULL && qr == NULL)
7062 gfc_error ("Allocate-object at %L is subobject of"
7063 " object at %L", &pe->where, &qe->where);
7064 break;
7066 else if (pr == NULL && qr != NULL)
7068 gfc_error ("Allocate-object at %L is subobject of"
7069 " object at %L", &qe->where, &pe->where);
7070 break;
7072 /* Here, pr != NULL && qr != NULL */
7073 gcc_assert(pr->type == qr->type);
7074 if (pr->type == REF_ARRAY)
7076 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7077 which are legal. */
7078 gcc_assert (qr->type == REF_ARRAY);
7080 if (pr->next && qr->next)
7082 int i;
7083 gfc_array_ref *par = &(pr->u.ar);
7084 gfc_array_ref *qar = &(qr->u.ar);
7086 for (i=0; i<par->dimen; i++)
7088 if ((par->start[i] != NULL
7089 || qar->start[i] != NULL)
7090 && gfc_dep_compare_expr (par->start[i],
7091 qar->start[i]) != 0)
7092 goto break_label;
7096 else
7098 if (pr->u.c.component->name != qr->u.c.component->name)
7099 break;
7102 pr = pr->next;
7103 qr = qr->next;
7105 break_label:
7111 if (strcmp (fcn, "ALLOCATE") == 0)
7113 for (a = code->ext.alloc.list; a; a = a->next)
7114 resolve_allocate_expr (a->expr, code);
7116 else
7118 for (a = code->ext.alloc.list; a; a = a->next)
7119 resolve_deallocate_expr (a->expr);
7124 /************ SELECT CASE resolution subroutines ************/
7126 /* Callback function for our mergesort variant. Determines interval
7127 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7128 op1 > op2. Assumes we're not dealing with the default case.
7129 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7130 There are nine situations to check. */
7132 static int
7133 compare_cases (const gfc_case *op1, const gfc_case *op2)
7135 int retval;
7137 if (op1->low == NULL) /* op1 = (:L) */
7139 /* op2 = (:N), so overlap. */
7140 retval = 0;
7141 /* op2 = (M:) or (M:N), L < M */
7142 if (op2->low != NULL
7143 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7144 retval = -1;
7146 else if (op1->high == NULL) /* op1 = (K:) */
7148 /* op2 = (M:), so overlap. */
7149 retval = 0;
7150 /* op2 = (:N) or (M:N), K > N */
7151 if (op2->high != NULL
7152 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7153 retval = 1;
7155 else /* op1 = (K:L) */
7157 if (op2->low == NULL) /* op2 = (:N), K > N */
7158 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7159 ? 1 : 0;
7160 else if (op2->high == NULL) /* op2 = (M:), L < M */
7161 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7162 ? -1 : 0;
7163 else /* op2 = (M:N) */
7165 retval = 0;
7166 /* L < M */
7167 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7168 retval = -1;
7169 /* K > N */
7170 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7171 retval = 1;
7175 return retval;
7179 /* Merge-sort a double linked case list, detecting overlap in the
7180 process. LIST is the head of the double linked case list before it
7181 is sorted. Returns the head of the sorted list if we don't see any
7182 overlap, or NULL otherwise. */
7184 static gfc_case *
7185 check_case_overlap (gfc_case *list)
7187 gfc_case *p, *q, *e, *tail;
7188 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7190 /* If the passed list was empty, return immediately. */
7191 if (!list)
7192 return NULL;
7194 overlap_seen = 0;
7195 insize = 1;
7197 /* Loop unconditionally. The only exit from this loop is a return
7198 statement, when we've finished sorting the case list. */
7199 for (;;)
7201 p = list;
7202 list = NULL;
7203 tail = NULL;
7205 /* Count the number of merges we do in this pass. */
7206 nmerges = 0;
7208 /* Loop while there exists a merge to be done. */
7209 while (p)
7211 int i;
7213 /* Count this merge. */
7214 nmerges++;
7216 /* Cut the list in two pieces by stepping INSIZE places
7217 forward in the list, starting from P. */
7218 psize = 0;
7219 q = p;
7220 for (i = 0; i < insize; i++)
7222 psize++;
7223 q = q->right;
7224 if (!q)
7225 break;
7227 qsize = insize;
7229 /* Now we have two lists. Merge them! */
7230 while (psize > 0 || (qsize > 0 && q != NULL))
7232 /* See from which the next case to merge comes from. */
7233 if (psize == 0)
7235 /* P is empty so the next case must come from Q. */
7236 e = q;
7237 q = q->right;
7238 qsize--;
7240 else if (qsize == 0 || q == NULL)
7242 /* Q is empty. */
7243 e = p;
7244 p = p->right;
7245 psize--;
7247 else
7249 cmp = compare_cases (p, q);
7250 if (cmp < 0)
7252 /* The whole case range for P is less than the
7253 one for Q. */
7254 e = p;
7255 p = p->right;
7256 psize--;
7258 else if (cmp > 0)
7260 /* The whole case range for Q is greater than
7261 the case range for P. */
7262 e = q;
7263 q = q->right;
7264 qsize--;
7266 else
7268 /* The cases overlap, or they are the same
7269 element in the list. Either way, we must
7270 issue an error and get the next case from P. */
7271 /* FIXME: Sort P and Q by line number. */
7272 gfc_error ("CASE label at %L overlaps with CASE "
7273 "label at %L", &p->where, &q->where);
7274 overlap_seen = 1;
7275 e = p;
7276 p = p->right;
7277 psize--;
7281 /* Add the next element to the merged list. */
7282 if (tail)
7283 tail->right = e;
7284 else
7285 list = e;
7286 e->left = tail;
7287 tail = e;
7290 /* P has now stepped INSIZE places along, and so has Q. So
7291 they're the same. */
7292 p = q;
7294 tail->right = NULL;
7296 /* If we have done only one merge or none at all, we've
7297 finished sorting the cases. */
7298 if (nmerges <= 1)
7300 if (!overlap_seen)
7301 return list;
7302 else
7303 return NULL;
7306 /* Otherwise repeat, merging lists twice the size. */
7307 insize *= 2;
7312 /* Check to see if an expression is suitable for use in a CASE statement.
7313 Makes sure that all case expressions are scalar constants of the same
7314 type. Return false if anything is wrong. */
7316 static bool
7317 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7319 if (e == NULL) return true;
7321 if (e->ts.type != case_expr->ts.type)
7323 gfc_error ("Expression in CASE statement at %L must be of type %s",
7324 &e->where, gfc_basic_typename (case_expr->ts.type));
7325 return false;
7328 /* C805 (R808) For a given case-construct, each case-value shall be of
7329 the same type as case-expr. For character type, length differences
7330 are allowed, but the kind type parameters shall be the same. */
7332 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7334 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7335 &e->where, case_expr->ts.kind);
7336 return false;
7339 /* Convert the case value kind to that of case expression kind,
7340 if needed */
7342 if (e->ts.kind != case_expr->ts.kind)
7343 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7345 if (e->rank != 0)
7347 gfc_error ("Expression in CASE statement at %L must be scalar",
7348 &e->where);
7349 return false;
7352 return true;
7356 /* Given a completely parsed select statement, we:
7358 - Validate all expressions and code within the SELECT.
7359 - Make sure that the selection expression is not of the wrong type.
7360 - Make sure that no case ranges overlap.
7361 - Eliminate unreachable cases and unreachable code resulting from
7362 removing case labels.
7364 The standard does allow unreachable cases, e.g. CASE (5:3). But
7365 they are a hassle for code generation, and to prevent that, we just
7366 cut them out here. This is not necessary for overlapping cases
7367 because they are illegal and we never even try to generate code.
7369 We have the additional caveat that a SELECT construct could have
7370 been a computed GOTO in the source code. Fortunately we can fairly
7371 easily work around that here: The case_expr for a "real" SELECT CASE
7372 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7373 we have to do is make sure that the case_expr is a scalar integer
7374 expression. */
7376 static void
7377 resolve_select (gfc_code *code, bool select_type)
7379 gfc_code *body;
7380 gfc_expr *case_expr;
7381 gfc_case *cp, *default_case, *tail, *head;
7382 int seen_unreachable;
7383 int seen_logical;
7384 int ncases;
7385 bt type;
7386 bool t;
7388 if (code->expr1 == NULL)
7390 /* This was actually a computed GOTO statement. */
7391 case_expr = code->expr2;
7392 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7393 gfc_error ("Selection expression in computed GOTO statement "
7394 "at %L must be a scalar integer expression",
7395 &case_expr->where);
7397 /* Further checking is not necessary because this SELECT was built
7398 by the compiler, so it should always be OK. Just move the
7399 case_expr from expr2 to expr so that we can handle computed
7400 GOTOs as normal SELECTs from here on. */
7401 code->expr1 = code->expr2;
7402 code->expr2 = NULL;
7403 return;
7406 case_expr = code->expr1;
7407 type = case_expr->ts.type;
7409 /* F08:C830. */
7410 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7412 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7413 &case_expr->where, gfc_typename (&case_expr->ts));
7415 /* Punt. Going on here just produce more garbage error messages. */
7416 return;
7419 /* F08:R842. */
7420 if (!select_type && case_expr->rank != 0)
7422 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7423 "expression", &case_expr->where);
7425 /* Punt. */
7426 return;
7429 /* Raise a warning if an INTEGER case value exceeds the range of
7430 the case-expr. Later, all expressions will be promoted to the
7431 largest kind of all case-labels. */
7433 if (type == BT_INTEGER)
7434 for (body = code->block; body; body = body->block)
7435 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7437 if (cp->low
7438 && gfc_check_integer_range (cp->low->value.integer,
7439 case_expr->ts.kind) != ARITH_OK)
7440 gfc_warning ("Expression in CASE statement at %L is "
7441 "not in the range of %s", &cp->low->where,
7442 gfc_typename (&case_expr->ts));
7444 if (cp->high
7445 && cp->low != cp->high
7446 && gfc_check_integer_range (cp->high->value.integer,
7447 case_expr->ts.kind) != ARITH_OK)
7448 gfc_warning ("Expression in CASE statement at %L is "
7449 "not in the range of %s", &cp->high->where,
7450 gfc_typename (&case_expr->ts));
7453 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7454 of the SELECT CASE expression and its CASE values. Walk the lists
7455 of case values, and if we find a mismatch, promote case_expr to
7456 the appropriate kind. */
7458 if (type == BT_LOGICAL || type == BT_INTEGER)
7460 for (body = code->block; body; body = body->block)
7462 /* Walk the case label list. */
7463 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7465 /* Intercept the DEFAULT case. It does not have a kind. */
7466 if (cp->low == NULL && cp->high == NULL)
7467 continue;
7469 /* Unreachable case ranges are discarded, so ignore. */
7470 if (cp->low != NULL && cp->high != NULL
7471 && cp->low != cp->high
7472 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7473 continue;
7475 if (cp->low != NULL
7476 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7477 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7479 if (cp->high != NULL
7480 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7481 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7486 /* Assume there is no DEFAULT case. */
7487 default_case = NULL;
7488 head = tail = NULL;
7489 ncases = 0;
7490 seen_logical = 0;
7492 for (body = code->block; body; body = body->block)
7494 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7495 t = true;
7496 seen_unreachable = 0;
7498 /* Walk the case label list, making sure that all case labels
7499 are legal. */
7500 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7502 /* Count the number of cases in the whole construct. */
7503 ncases++;
7505 /* Intercept the DEFAULT case. */
7506 if (cp->low == NULL && cp->high == NULL)
7508 if (default_case != NULL)
7510 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7511 "by a second DEFAULT CASE at %L",
7512 &default_case->where, &cp->where);
7513 t = false;
7514 break;
7516 else
7518 default_case = cp;
7519 continue;
7523 /* Deal with single value cases and case ranges. Errors are
7524 issued from the validation function. */
7525 if (!validate_case_label_expr (cp->low, case_expr)
7526 || !validate_case_label_expr (cp->high, case_expr))
7528 t = false;
7529 break;
7532 if (type == BT_LOGICAL
7533 && ((cp->low == NULL || cp->high == NULL)
7534 || cp->low != cp->high))
7536 gfc_error ("Logical range in CASE statement at %L is not "
7537 "allowed", &cp->low->where);
7538 t = false;
7539 break;
7542 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7544 int value;
7545 value = cp->low->value.logical == 0 ? 2 : 1;
7546 if (value & seen_logical)
7548 gfc_error ("Constant logical value in CASE statement "
7549 "is repeated at %L",
7550 &cp->low->where);
7551 t = false;
7552 break;
7554 seen_logical |= value;
7557 if (cp->low != NULL && cp->high != NULL
7558 && cp->low != cp->high
7559 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7561 if (gfc_option.warn_surprising)
7562 gfc_warning ("Range specification at %L can never "
7563 "be matched", &cp->where);
7565 cp->unreachable = 1;
7566 seen_unreachable = 1;
7568 else
7570 /* If the case range can be matched, it can also overlap with
7571 other cases. To make sure it does not, we put it in a
7572 double linked list here. We sort that with a merge sort
7573 later on to detect any overlapping cases. */
7574 if (!head)
7576 head = tail = cp;
7577 head->right = head->left = NULL;
7579 else
7581 tail->right = cp;
7582 tail->right->left = tail;
7583 tail = tail->right;
7584 tail->right = NULL;
7589 /* It there was a failure in the previous case label, give up
7590 for this case label list. Continue with the next block. */
7591 if (!t)
7592 continue;
7594 /* See if any case labels that are unreachable have been seen.
7595 If so, we eliminate them. This is a bit of a kludge because
7596 the case lists for a single case statement (label) is a
7597 single forward linked lists. */
7598 if (seen_unreachable)
7600 /* Advance until the first case in the list is reachable. */
7601 while (body->ext.block.case_list != NULL
7602 && body->ext.block.case_list->unreachable)
7604 gfc_case *n = body->ext.block.case_list;
7605 body->ext.block.case_list = body->ext.block.case_list->next;
7606 n->next = NULL;
7607 gfc_free_case_list (n);
7610 /* Strip all other unreachable cases. */
7611 if (body->ext.block.case_list)
7613 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7615 if (cp->next->unreachable)
7617 gfc_case *n = cp->next;
7618 cp->next = cp->next->next;
7619 n->next = NULL;
7620 gfc_free_case_list (n);
7627 /* See if there were overlapping cases. If the check returns NULL,
7628 there was overlap. In that case we don't do anything. If head
7629 is non-NULL, we prepend the DEFAULT case. The sorted list can
7630 then used during code generation for SELECT CASE constructs with
7631 a case expression of a CHARACTER type. */
7632 if (head)
7634 head = check_case_overlap (head);
7636 /* Prepend the default_case if it is there. */
7637 if (head != NULL && default_case)
7639 default_case->left = NULL;
7640 default_case->right = head;
7641 head->left = default_case;
7645 /* Eliminate dead blocks that may be the result if we've seen
7646 unreachable case labels for a block. */
7647 for (body = code; body && body->block; body = body->block)
7649 if (body->block->ext.block.case_list == NULL)
7651 /* Cut the unreachable block from the code chain. */
7652 gfc_code *c = body->block;
7653 body->block = c->block;
7655 /* Kill the dead block, but not the blocks below it. */
7656 c->block = NULL;
7657 gfc_free_statements (c);
7661 /* More than two cases is legal but insane for logical selects.
7662 Issue a warning for it. */
7663 if (gfc_option.warn_surprising && type == BT_LOGICAL
7664 && ncases > 2)
7665 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7666 &code->loc);
7670 /* Check if a derived type is extensible. */
7672 bool
7673 gfc_type_is_extensible (gfc_symbol *sym)
7675 return !(sym->attr.is_bind_c || sym->attr.sequence
7676 || (sym->attr.is_class
7677 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7681 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7682 correct as well as possibly the array-spec. */
7684 static void
7685 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7687 gfc_expr* target;
7689 gcc_assert (sym->assoc);
7690 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7692 /* If this is for SELECT TYPE, the target may not yet be set. In that
7693 case, return. Resolution will be called later manually again when
7694 this is done. */
7695 target = sym->assoc->target;
7696 if (!target)
7697 return;
7698 gcc_assert (!sym->assoc->dangling);
7700 if (resolve_target && !gfc_resolve_expr (target))
7701 return;
7703 /* For variable targets, we get some attributes from the target. */
7704 if (target->expr_type == EXPR_VARIABLE)
7706 gfc_symbol* tsym;
7708 gcc_assert (target->symtree);
7709 tsym = target->symtree->n.sym;
7711 sym->attr.asynchronous = tsym->attr.asynchronous;
7712 sym->attr.volatile_ = tsym->attr.volatile_;
7714 sym->attr.target = tsym->attr.target
7715 || gfc_expr_attr (target).pointer;
7718 /* Get type if this was not already set. Note that it can be
7719 some other type than the target in case this is a SELECT TYPE
7720 selector! So we must not update when the type is already there. */
7721 if (sym->ts.type == BT_UNKNOWN)
7722 sym->ts = target->ts;
7723 gcc_assert (sym->ts.type != BT_UNKNOWN);
7725 /* See if this is a valid association-to-variable. */
7726 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7727 && !gfc_has_vector_subscript (target));
7729 /* Finally resolve if this is an array or not. */
7730 if (sym->attr.dimension && target->rank == 0)
7732 gfc_error ("Associate-name '%s' at %L is used as array",
7733 sym->name, &sym->declared_at);
7734 sym->attr.dimension = 0;
7735 return;
7738 /* We cannot deal with class selectors that need temporaries. */
7739 if (target->ts.type == BT_CLASS
7740 && gfc_ref_needs_temporary_p (target->ref))
7742 gfc_error ("CLASS selector at %L needs a temporary which is not "
7743 "yet implemented", &target->where);
7744 return;
7747 if (target->ts.type != BT_CLASS && target->rank > 0)
7748 sym->attr.dimension = 1;
7749 else if (target->ts.type == BT_CLASS)
7750 gfc_fix_class_refs (target);
7752 /* The associate-name will have a correct type by now. Make absolutely
7753 sure that it has not picked up a dimension attribute. */
7754 if (sym->ts.type == BT_CLASS)
7755 sym->attr.dimension = 0;
7757 if (sym->attr.dimension)
7759 sym->as = gfc_get_array_spec ();
7760 sym->as->rank = target->rank;
7761 sym->as->type = AS_DEFERRED;
7763 /* Target must not be coindexed, thus the associate-variable
7764 has no corank. */
7765 sym->as->corank = 0;
7768 /* Mark this as an associate variable. */
7769 sym->attr.associate_var = 1;
7771 /* If the target is a good class object, so is the associate variable. */
7772 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7773 sym->attr.class_ok = 1;
7777 /* Resolve a SELECT TYPE statement. */
7779 static void
7780 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7782 gfc_symbol *selector_type;
7783 gfc_code *body, *new_st, *if_st, *tail;
7784 gfc_code *class_is = NULL, *default_case = NULL;
7785 gfc_case *c;
7786 gfc_symtree *st;
7787 char name[GFC_MAX_SYMBOL_LEN];
7788 gfc_namespace *ns;
7789 int error = 0;
7790 int charlen = 0;
7792 ns = code->ext.block.ns;
7793 gfc_resolve (ns);
7795 /* Check for F03:C813. */
7796 if (code->expr1->ts.type != BT_CLASS
7797 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7799 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7800 "at %L", &code->loc);
7801 return;
7804 if (!code->expr1->symtree->n.sym->attr.class_ok)
7805 return;
7807 if (code->expr2)
7809 if (code->expr1->symtree->n.sym->attr.untyped)
7810 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7811 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7813 /* F2008: C803 The selector expression must not be coindexed. */
7814 if (gfc_is_coindexed (code->expr2))
7816 gfc_error ("Selector at %L must not be coindexed",
7817 &code->expr2->where);
7818 return;
7822 else
7824 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7826 if (gfc_is_coindexed (code->expr1))
7828 gfc_error ("Selector at %L must not be coindexed",
7829 &code->expr1->where);
7830 return;
7834 /* Loop over TYPE IS / CLASS IS cases. */
7835 for (body = code->block; body; body = body->block)
7837 c = body->ext.block.case_list;
7839 /* Check F03:C815. */
7840 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7841 && !selector_type->attr.unlimited_polymorphic
7842 && !gfc_type_is_extensible (c->ts.u.derived))
7844 gfc_error ("Derived type '%s' at %L must be extensible",
7845 c->ts.u.derived->name, &c->where);
7846 error++;
7847 continue;
7850 /* Check F03:C816. */
7851 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7852 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7853 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7855 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7856 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7857 c->ts.u.derived->name, &c->where, selector_type->name);
7858 else
7859 gfc_error ("Unexpected intrinsic type '%s' at %L",
7860 gfc_basic_typename (c->ts.type), &c->where);
7861 error++;
7862 continue;
7865 /* Check F03:C814. */
7866 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7868 gfc_error ("The type-spec at %L shall specify that each length "
7869 "type parameter is assumed", &c->where);
7870 error++;
7871 continue;
7874 /* Intercept the DEFAULT case. */
7875 if (c->ts.type == BT_UNKNOWN)
7877 /* Check F03:C818. */
7878 if (default_case)
7880 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7881 "by a second DEFAULT CASE at %L",
7882 &default_case->ext.block.case_list->where, &c->where);
7883 error++;
7884 continue;
7887 default_case = body;
7891 if (error > 0)
7892 return;
7894 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7895 target if present. If there are any EXIT statements referring to the
7896 SELECT TYPE construct, this is no problem because the gfc_code
7897 reference stays the same and EXIT is equally possible from the BLOCK
7898 it is changed to. */
7899 code->op = EXEC_BLOCK;
7900 if (code->expr2)
7902 gfc_association_list* assoc;
7904 assoc = gfc_get_association_list ();
7905 assoc->st = code->expr1->symtree;
7906 assoc->target = gfc_copy_expr (code->expr2);
7907 assoc->target->where = code->expr2->where;
7908 /* assoc->variable will be set by resolve_assoc_var. */
7910 code->ext.block.assoc = assoc;
7911 code->expr1->symtree->n.sym->assoc = assoc;
7913 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7915 else
7916 code->ext.block.assoc = NULL;
7918 /* Add EXEC_SELECT to switch on type. */
7919 new_st = gfc_get_code ();
7920 new_st->op = code->op;
7921 new_st->expr1 = code->expr1;
7922 new_st->expr2 = code->expr2;
7923 new_st->block = code->block;
7924 code->expr1 = code->expr2 = NULL;
7925 code->block = NULL;
7926 if (!ns->code)
7927 ns->code = new_st;
7928 else
7929 ns->code->next = new_st;
7930 code = new_st;
7931 code->op = EXEC_SELECT;
7933 gfc_add_vptr_component (code->expr1);
7934 gfc_add_hash_component (code->expr1);
7936 /* Loop over TYPE IS / CLASS IS cases. */
7937 for (body = code->block; body; body = body->block)
7939 c = body->ext.block.case_list;
7941 if (c->ts.type == BT_DERIVED)
7942 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7943 c->ts.u.derived->hash_value);
7944 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
7946 gfc_symbol *ivtab;
7947 gfc_expr *e;
7949 ivtab = gfc_find_intrinsic_vtab (&c->ts);
7950 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
7951 e = CLASS_DATA (ivtab)->initializer;
7952 c->low = c->high = gfc_copy_expr (e);
7955 else if (c->ts.type == BT_UNKNOWN)
7956 continue;
7958 /* Associate temporary to selector. This should only be done
7959 when this case is actually true, so build a new ASSOCIATE
7960 that does precisely this here (instead of using the
7961 'global' one). */
7963 if (c->ts.type == BT_CLASS)
7964 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
7965 else if (c->ts.type == BT_DERIVED)
7966 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
7967 else if (c->ts.type == BT_CHARACTER)
7969 if (c->ts.u.cl && c->ts.u.cl->length
7970 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7971 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
7972 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
7973 charlen, c->ts.kind);
7975 else
7976 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
7977 c->ts.kind);
7979 st = gfc_find_symtree (ns->sym_root, name);
7980 gcc_assert (st->n.sym->assoc);
7981 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7982 st->n.sym->assoc->target->where = code->expr1->where;
7983 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
7984 gfc_add_data_component (st->n.sym->assoc->target);
7986 new_st = gfc_get_code ();
7987 new_st->op = EXEC_BLOCK;
7988 new_st->ext.block.ns = gfc_build_block_ns (ns);
7989 new_st->ext.block.ns->code = body->next;
7990 body->next = new_st;
7992 /* Chain in the new list only if it is marked as dangling. Otherwise
7993 there is a CASE label overlap and this is already used. Just ignore,
7994 the error is diagnosed elsewhere. */
7995 if (st->n.sym->assoc->dangling)
7997 new_st->ext.block.assoc = st->n.sym->assoc;
7998 st->n.sym->assoc->dangling = 0;
8001 resolve_assoc_var (st->n.sym, false);
8004 /* Take out CLASS IS cases for separate treatment. */
8005 body = code;
8006 while (body && body->block)
8008 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8010 /* Add to class_is list. */
8011 if (class_is == NULL)
8013 class_is = body->block;
8014 tail = class_is;
8016 else
8018 for (tail = class_is; tail->block; tail = tail->block) ;
8019 tail->block = body->block;
8020 tail = tail->block;
8022 /* Remove from EXEC_SELECT list. */
8023 body->block = body->block->block;
8024 tail->block = NULL;
8026 else
8027 body = body->block;
8030 if (class_is)
8032 gfc_symbol *vtab;
8034 if (!default_case)
8036 /* Add a default case to hold the CLASS IS cases. */
8037 for (tail = code; tail->block; tail = tail->block) ;
8038 tail->block = gfc_get_code ();
8039 tail = tail->block;
8040 tail->op = EXEC_SELECT_TYPE;
8041 tail->ext.block.case_list = gfc_get_case ();
8042 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8043 tail->next = NULL;
8044 default_case = tail;
8047 /* More than one CLASS IS block? */
8048 if (class_is->block)
8050 gfc_code **c1,*c2;
8051 bool swapped;
8052 /* Sort CLASS IS blocks by extension level. */
8055 swapped = false;
8056 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8058 c2 = (*c1)->block;
8059 /* F03:C817 (check for doubles). */
8060 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8061 == c2->ext.block.case_list->ts.u.derived->hash_value)
8063 gfc_error ("Double CLASS IS block in SELECT TYPE "
8064 "statement at %L",
8065 &c2->ext.block.case_list->where);
8066 return;
8068 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8069 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8071 /* Swap. */
8072 (*c1)->block = c2->block;
8073 c2->block = *c1;
8074 *c1 = c2;
8075 swapped = true;
8079 while (swapped);
8082 /* Generate IF chain. */
8083 if_st = gfc_get_code ();
8084 if_st->op = EXEC_IF;
8085 new_st = if_st;
8086 for (body = class_is; body; body = body->block)
8088 new_st->block = gfc_get_code ();
8089 new_st = new_st->block;
8090 new_st->op = EXEC_IF;
8091 /* Set up IF condition: Call _gfortran_is_extension_of. */
8092 new_st->expr1 = gfc_get_expr ();
8093 new_st->expr1->expr_type = EXPR_FUNCTION;
8094 new_st->expr1->ts.type = BT_LOGICAL;
8095 new_st->expr1->ts.kind = 4;
8096 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8097 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8098 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8099 /* Set up arguments. */
8100 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8101 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8102 new_st->expr1->value.function.actual->expr->where = code->loc;
8103 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8104 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8105 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8106 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8107 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8108 new_st->next = body->next;
8110 if (default_case->next)
8112 new_st->block = gfc_get_code ();
8113 new_st = new_st->block;
8114 new_st->op = EXEC_IF;
8115 new_st->next = default_case->next;
8118 /* Replace CLASS DEFAULT code by the IF chain. */
8119 default_case->next = if_st;
8122 /* Resolve the internal code. This can not be done earlier because
8123 it requires that the sym->assoc of selectors is set already. */
8124 gfc_current_ns = ns;
8125 gfc_resolve_blocks (code->block, gfc_current_ns);
8126 gfc_current_ns = old_ns;
8128 resolve_select (code, true);
8132 /* Resolve a transfer statement. This is making sure that:
8133 -- a derived type being transferred has only non-pointer components
8134 -- a derived type being transferred doesn't have private components, unless
8135 it's being transferred from the module where the type was defined
8136 -- we're not trying to transfer a whole assumed size array. */
8138 static void
8139 resolve_transfer (gfc_code *code)
8141 gfc_typespec *ts;
8142 gfc_symbol *sym;
8143 gfc_ref *ref;
8144 gfc_expr *exp;
8146 exp = code->expr1;
8148 while (exp != NULL && exp->expr_type == EXPR_OP
8149 && exp->value.op.op == INTRINSIC_PARENTHESES)
8150 exp = exp->value.op.op1;
8152 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8154 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8155 "MOLD=", &exp->where);
8156 return;
8159 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8160 && exp->expr_type != EXPR_FUNCTION))
8161 return;
8163 /* If we are reading, the variable will be changed. Note that
8164 code->ext.dt may be NULL if the TRANSFER is related to
8165 an INQUIRE statement -- but in this case, we are not reading, either. */
8166 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8167 && !gfc_check_vardef_context (exp, false, false, false,
8168 _("item in READ")))
8169 return;
8171 sym = exp->symtree->n.sym;
8172 ts = &sym->ts;
8174 /* Go to actual component transferred. */
8175 for (ref = exp->ref; ref; ref = ref->next)
8176 if (ref->type == REF_COMPONENT)
8177 ts = &ref->u.c.component->ts;
8179 if (ts->type == BT_CLASS)
8181 /* FIXME: Test for defined input/output. */
8182 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8183 "it is processed by a defined input/output procedure",
8184 &code->loc);
8185 return;
8188 if (ts->type == BT_DERIVED)
8190 /* Check that transferred derived type doesn't contain POINTER
8191 components. */
8192 if (ts->u.derived->attr.pointer_comp)
8194 gfc_error ("Data transfer element at %L cannot have POINTER "
8195 "components unless it is processed by a defined "
8196 "input/output procedure", &code->loc);
8197 return;
8200 /* F08:C935. */
8201 if (ts->u.derived->attr.proc_pointer_comp)
8203 gfc_error ("Data transfer element at %L cannot have "
8204 "procedure pointer components", &code->loc);
8205 return;
8208 if (ts->u.derived->attr.alloc_comp)
8210 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8211 "components unless it is processed by a defined "
8212 "input/output procedure", &code->loc);
8213 return;
8216 /* C_PTR and C_FUNPTR have private components which means they can not
8217 be printed. However, if -std=gnu and not -pedantic, allow
8218 the component to be printed to help debugging. */
8219 if (ts->u.derived->ts.f90_type == BT_VOID)
8221 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8222 "cannot have PRIVATE components", &code->loc))
8223 return;
8225 else if (derived_inaccessible (ts->u.derived))
8227 gfc_error ("Data transfer element at %L cannot have "
8228 "PRIVATE components",&code->loc);
8229 return;
8233 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8234 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8236 gfc_error ("Data transfer element at %L cannot be a full reference to "
8237 "an assumed-size array", &code->loc);
8238 return;
8243 /*********** Toplevel code resolution subroutines ***********/
8245 /* Find the set of labels that are reachable from this block. We also
8246 record the last statement in each block. */
8248 static void
8249 find_reachable_labels (gfc_code *block)
8251 gfc_code *c;
8253 if (!block)
8254 return;
8256 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8258 /* Collect labels in this block. We don't keep those corresponding
8259 to END {IF|SELECT}, these are checked in resolve_branch by going
8260 up through the code_stack. */
8261 for (c = block; c; c = c->next)
8263 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8264 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8267 /* Merge with labels from parent block. */
8268 if (cs_base->prev)
8270 gcc_assert (cs_base->prev->reachable_labels);
8271 bitmap_ior_into (cs_base->reachable_labels,
8272 cs_base->prev->reachable_labels);
8277 static void
8278 resolve_lock_unlock (gfc_code *code)
8280 if (code->expr1->ts.type != BT_DERIVED
8281 || code->expr1->expr_type != EXPR_VARIABLE
8282 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8283 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8284 || code->expr1->rank != 0
8285 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8286 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8287 &code->expr1->where);
8289 /* Check STAT. */
8290 if (code->expr2
8291 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8292 || code->expr2->expr_type != EXPR_VARIABLE))
8293 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8294 &code->expr2->where);
8296 if (code->expr2
8297 && !gfc_check_vardef_context (code->expr2, false, false, false,
8298 _("STAT variable")))
8299 return;
8301 /* Check ERRMSG. */
8302 if (code->expr3
8303 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8304 || code->expr3->expr_type != EXPR_VARIABLE))
8305 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8306 &code->expr3->where);
8308 if (code->expr3
8309 && !gfc_check_vardef_context (code->expr3, false, false, false,
8310 _("ERRMSG variable")))
8311 return;
8313 /* Check ACQUIRED_LOCK. */
8314 if (code->expr4
8315 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8316 || code->expr4->expr_type != EXPR_VARIABLE))
8317 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8318 "variable", &code->expr4->where);
8320 if (code->expr4
8321 && !gfc_check_vardef_context (code->expr4, false, false, false,
8322 _("ACQUIRED_LOCK variable")))
8323 return;
8327 static void
8328 resolve_sync (gfc_code *code)
8330 /* Check imageset. The * case matches expr1 == NULL. */
8331 if (code->expr1)
8333 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8334 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8335 "INTEGER expression", &code->expr1->where);
8336 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8337 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8338 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8339 &code->expr1->where);
8340 else if (code->expr1->expr_type == EXPR_ARRAY
8341 && gfc_simplify_expr (code->expr1, 0))
8343 gfc_constructor *cons;
8344 cons = gfc_constructor_first (code->expr1->value.constructor);
8345 for (; cons; cons = gfc_constructor_next (cons))
8346 if (cons->expr->expr_type == EXPR_CONSTANT
8347 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8348 gfc_error ("Imageset argument at %L must between 1 and "
8349 "num_images()", &cons->expr->where);
8353 /* Check STAT. */
8354 if (code->expr2
8355 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8356 || code->expr2->expr_type != EXPR_VARIABLE))
8357 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8358 &code->expr2->where);
8360 /* Check ERRMSG. */
8361 if (code->expr3
8362 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8363 || code->expr3->expr_type != EXPR_VARIABLE))
8364 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8365 &code->expr3->where);
8369 /* Given a branch to a label, see if the branch is conforming.
8370 The code node describes where the branch is located. */
8372 static void
8373 resolve_branch (gfc_st_label *label, gfc_code *code)
8375 code_stack *stack;
8377 if (label == NULL)
8378 return;
8380 /* Step one: is this a valid branching target? */
8382 if (label->defined == ST_LABEL_UNKNOWN)
8384 gfc_error ("Label %d referenced at %L is never defined", label->value,
8385 &label->where);
8386 return;
8389 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8391 gfc_error ("Statement at %L is not a valid branch target statement "
8392 "for the branch statement at %L", &label->where, &code->loc);
8393 return;
8396 /* Step two: make sure this branch is not a branch to itself ;-) */
8398 if (code->here == label)
8400 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8401 return;
8404 /* Step three: See if the label is in the same block as the
8405 branching statement. The hard work has been done by setting up
8406 the bitmap reachable_labels. */
8408 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8410 /* Check now whether there is a CRITICAL construct; if so, check
8411 whether the label is still visible outside of the CRITICAL block,
8412 which is invalid. */
8413 for (stack = cs_base; stack; stack = stack->prev)
8415 if (stack->current->op == EXEC_CRITICAL
8416 && bitmap_bit_p (stack->reachable_labels, label->value))
8417 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8418 "label at %L", &code->loc, &label->where);
8419 else if (stack->current->op == EXEC_DO_CONCURRENT
8420 && bitmap_bit_p (stack->reachable_labels, label->value))
8421 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8422 "for label at %L", &code->loc, &label->where);
8425 return;
8428 /* Step four: If we haven't found the label in the bitmap, it may
8429 still be the label of the END of the enclosing block, in which
8430 case we find it by going up the code_stack. */
8432 for (stack = cs_base; stack; stack = stack->prev)
8434 if (stack->current->next && stack->current->next->here == label)
8435 break;
8436 if (stack->current->op == EXEC_CRITICAL)
8438 /* Note: A label at END CRITICAL does not leave the CRITICAL
8439 construct as END CRITICAL is still part of it. */
8440 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8441 " at %L", &code->loc, &label->where);
8442 return;
8444 else if (stack->current->op == EXEC_DO_CONCURRENT)
8446 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8447 "label at %L", &code->loc, &label->where);
8448 return;
8452 if (stack)
8454 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8455 return;
8458 /* The label is not in an enclosing block, so illegal. This was
8459 allowed in Fortran 66, so we allow it as extension. No
8460 further checks are necessary in this case. */
8461 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8462 "as the GOTO statement at %L", &label->where,
8463 &code->loc);
8464 return;
8468 /* Check whether EXPR1 has the same shape as EXPR2. */
8470 static bool
8471 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8473 mpz_t shape[GFC_MAX_DIMENSIONS];
8474 mpz_t shape2[GFC_MAX_DIMENSIONS];
8475 bool result = false;
8476 int i;
8478 /* Compare the rank. */
8479 if (expr1->rank != expr2->rank)
8480 return result;
8482 /* Compare the size of each dimension. */
8483 for (i=0; i<expr1->rank; i++)
8485 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8486 goto ignore;
8488 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8489 goto ignore;
8491 if (mpz_cmp (shape[i], shape2[i]))
8492 goto over;
8495 /* When either of the two expression is an assumed size array, we
8496 ignore the comparison of dimension sizes. */
8497 ignore:
8498 result = true;
8500 over:
8501 gfc_clear_shape (shape, i);
8502 gfc_clear_shape (shape2, i);
8503 return result;
8507 /* Check whether a WHERE assignment target or a WHERE mask expression
8508 has the same shape as the outmost WHERE mask expression. */
8510 static void
8511 resolve_where (gfc_code *code, gfc_expr *mask)
8513 gfc_code *cblock;
8514 gfc_code *cnext;
8515 gfc_expr *e = NULL;
8517 cblock = code->block;
8519 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8520 In case of nested WHERE, only the outmost one is stored. */
8521 if (mask == NULL) /* outmost WHERE */
8522 e = cblock->expr1;
8523 else /* inner WHERE */
8524 e = mask;
8526 while (cblock)
8528 if (cblock->expr1)
8530 /* Check if the mask-expr has a consistent shape with the
8531 outmost WHERE mask-expr. */
8532 if (!resolve_where_shape (cblock->expr1, e))
8533 gfc_error ("WHERE mask at %L has inconsistent shape",
8534 &cblock->expr1->where);
8537 /* the assignment statement of a WHERE statement, or the first
8538 statement in where-body-construct of a WHERE construct */
8539 cnext = cblock->next;
8540 while (cnext)
8542 switch (cnext->op)
8544 /* WHERE assignment statement */
8545 case EXEC_ASSIGN:
8547 /* Check shape consistent for WHERE assignment target. */
8548 if (e && !resolve_where_shape (cnext->expr1, e))
8549 gfc_error ("WHERE assignment target at %L has "
8550 "inconsistent shape", &cnext->expr1->where);
8551 break;
8554 case EXEC_ASSIGN_CALL:
8555 resolve_call (cnext);
8556 if (!cnext->resolved_sym->attr.elemental)
8557 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8558 &cnext->ext.actual->expr->where);
8559 break;
8561 /* WHERE or WHERE construct is part of a where-body-construct */
8562 case EXEC_WHERE:
8563 resolve_where (cnext, e);
8564 break;
8566 default:
8567 gfc_error ("Unsupported statement inside WHERE at %L",
8568 &cnext->loc);
8570 /* the next statement within the same where-body-construct */
8571 cnext = cnext->next;
8573 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8574 cblock = cblock->block;
8579 /* Resolve assignment in FORALL construct.
8580 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8581 FORALL index variables. */
8583 static void
8584 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8586 int n;
8588 for (n = 0; n < nvar; n++)
8590 gfc_symbol *forall_index;
8592 forall_index = var_expr[n]->symtree->n.sym;
8594 /* Check whether the assignment target is one of the FORALL index
8595 variable. */
8596 if ((code->expr1->expr_type == EXPR_VARIABLE)
8597 && (code->expr1->symtree->n.sym == forall_index))
8598 gfc_error ("Assignment to a FORALL index variable at %L",
8599 &code->expr1->where);
8600 else
8602 /* If one of the FORALL index variables doesn't appear in the
8603 assignment variable, then there could be a many-to-one
8604 assignment. Emit a warning rather than an error because the
8605 mask could be resolving this problem. */
8606 if (!find_forall_index (code->expr1, forall_index, 0))
8607 gfc_warning ("The FORALL with index '%s' is not used on the "
8608 "left side of the assignment at %L and so might "
8609 "cause multiple assignment to this object",
8610 var_expr[n]->symtree->name, &code->expr1->where);
8616 /* Resolve WHERE statement in FORALL construct. */
8618 static void
8619 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8620 gfc_expr **var_expr)
8622 gfc_code *cblock;
8623 gfc_code *cnext;
8625 cblock = code->block;
8626 while (cblock)
8628 /* the assignment statement of a WHERE statement, or the first
8629 statement in where-body-construct of a WHERE construct */
8630 cnext = cblock->next;
8631 while (cnext)
8633 switch (cnext->op)
8635 /* WHERE assignment statement */
8636 case EXEC_ASSIGN:
8637 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8638 break;
8640 /* WHERE operator assignment statement */
8641 case EXEC_ASSIGN_CALL:
8642 resolve_call (cnext);
8643 if (!cnext->resolved_sym->attr.elemental)
8644 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8645 &cnext->ext.actual->expr->where);
8646 break;
8648 /* WHERE or WHERE construct is part of a where-body-construct */
8649 case EXEC_WHERE:
8650 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8651 break;
8653 default:
8654 gfc_error ("Unsupported statement inside WHERE at %L",
8655 &cnext->loc);
8657 /* the next statement within the same where-body-construct */
8658 cnext = cnext->next;
8660 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8661 cblock = cblock->block;
8666 /* Traverse the FORALL body to check whether the following errors exist:
8667 1. For assignment, check if a many-to-one assignment happens.
8668 2. For WHERE statement, check the WHERE body to see if there is any
8669 many-to-one assignment. */
8671 static void
8672 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8674 gfc_code *c;
8676 c = code->block->next;
8677 while (c)
8679 switch (c->op)
8681 case EXEC_ASSIGN:
8682 case EXEC_POINTER_ASSIGN:
8683 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8684 break;
8686 case EXEC_ASSIGN_CALL:
8687 resolve_call (c);
8688 break;
8690 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8691 there is no need to handle it here. */
8692 case EXEC_FORALL:
8693 break;
8694 case EXEC_WHERE:
8695 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8696 break;
8697 default:
8698 break;
8700 /* The next statement in the FORALL body. */
8701 c = c->next;
8706 /* Counts the number of iterators needed inside a forall construct, including
8707 nested forall constructs. This is used to allocate the needed memory
8708 in gfc_resolve_forall. */
8710 static int
8711 gfc_count_forall_iterators (gfc_code *code)
8713 int max_iters, sub_iters, current_iters;
8714 gfc_forall_iterator *fa;
8716 gcc_assert(code->op == EXEC_FORALL);
8717 max_iters = 0;
8718 current_iters = 0;
8720 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8721 current_iters ++;
8723 code = code->block->next;
8725 while (code)
8727 if (code->op == EXEC_FORALL)
8729 sub_iters = gfc_count_forall_iterators (code);
8730 if (sub_iters > max_iters)
8731 max_iters = sub_iters;
8733 code = code->next;
8736 return current_iters + max_iters;
8740 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8741 gfc_resolve_forall_body to resolve the FORALL body. */
8743 static void
8744 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8746 static gfc_expr **var_expr;
8747 static int total_var = 0;
8748 static int nvar = 0;
8749 int old_nvar, tmp;
8750 gfc_forall_iterator *fa;
8751 int i;
8753 old_nvar = nvar;
8755 /* Start to resolve a FORALL construct */
8756 if (forall_save == 0)
8758 /* Count the total number of FORALL index in the nested FORALL
8759 construct in order to allocate the VAR_EXPR with proper size. */
8760 total_var = gfc_count_forall_iterators (code);
8762 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8763 var_expr = XCNEWVEC (gfc_expr *, total_var);
8766 /* The information about FORALL iterator, including FORALL index start, end
8767 and stride. The FORALL index can not appear in start, end or stride. */
8768 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8770 /* Check if any outer FORALL index name is the same as the current
8771 one. */
8772 for (i = 0; i < nvar; i++)
8774 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8776 gfc_error ("An outer FORALL construct already has an index "
8777 "with this name %L", &fa->var->where);
8781 /* Record the current FORALL index. */
8782 var_expr[nvar] = gfc_copy_expr (fa->var);
8784 nvar++;
8786 /* No memory leak. */
8787 gcc_assert (nvar <= total_var);
8790 /* Resolve the FORALL body. */
8791 gfc_resolve_forall_body (code, nvar, var_expr);
8793 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8794 gfc_resolve_blocks (code->block, ns);
8796 tmp = nvar;
8797 nvar = old_nvar;
8798 /* Free only the VAR_EXPRs allocated in this frame. */
8799 for (i = nvar; i < tmp; i++)
8800 gfc_free_expr (var_expr[i]);
8802 if (nvar == 0)
8804 /* We are in the outermost FORALL construct. */
8805 gcc_assert (forall_save == 0);
8807 /* VAR_EXPR is not needed any more. */
8808 free (var_expr);
8809 total_var = 0;
8814 /* Resolve a BLOCK construct statement. */
8816 static void
8817 resolve_block_construct (gfc_code* code)
8819 /* Resolve the BLOCK's namespace. */
8820 gfc_resolve (code->ext.block.ns);
8822 /* For an ASSOCIATE block, the associations (and their targets) are already
8823 resolved during resolve_symbol. */
8827 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8828 DO code nodes. */
8830 static void resolve_code (gfc_code *, gfc_namespace *);
8832 void
8833 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8835 bool t;
8837 for (; b; b = b->block)
8839 t = gfc_resolve_expr (b->expr1);
8840 if (!gfc_resolve_expr (b->expr2))
8841 t = false;
8843 switch (b->op)
8845 case EXEC_IF:
8846 if (t && b->expr1 != NULL
8847 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8848 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8849 &b->expr1->where);
8850 break;
8852 case EXEC_WHERE:
8853 if (t
8854 && b->expr1 != NULL
8855 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8856 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8857 &b->expr1->where);
8858 break;
8860 case EXEC_GOTO:
8861 resolve_branch (b->label1, b);
8862 break;
8864 case EXEC_BLOCK:
8865 resolve_block_construct (b);
8866 break;
8868 case EXEC_SELECT:
8869 case EXEC_SELECT_TYPE:
8870 case EXEC_FORALL:
8871 case EXEC_DO:
8872 case EXEC_DO_WHILE:
8873 case EXEC_DO_CONCURRENT:
8874 case EXEC_CRITICAL:
8875 case EXEC_READ:
8876 case EXEC_WRITE:
8877 case EXEC_IOLENGTH:
8878 case EXEC_WAIT:
8879 break;
8881 case EXEC_OMP_ATOMIC:
8882 case EXEC_OMP_CRITICAL:
8883 case EXEC_OMP_DO:
8884 case EXEC_OMP_MASTER:
8885 case EXEC_OMP_ORDERED:
8886 case EXEC_OMP_PARALLEL:
8887 case EXEC_OMP_PARALLEL_DO:
8888 case EXEC_OMP_PARALLEL_SECTIONS:
8889 case EXEC_OMP_PARALLEL_WORKSHARE:
8890 case EXEC_OMP_SECTIONS:
8891 case EXEC_OMP_SINGLE:
8892 case EXEC_OMP_TASK:
8893 case EXEC_OMP_TASKWAIT:
8894 case EXEC_OMP_TASKYIELD:
8895 case EXEC_OMP_WORKSHARE:
8896 break;
8898 default:
8899 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8902 resolve_code (b->next, ns);
8907 /* Does everything to resolve an ordinary assignment. Returns true
8908 if this is an interface assignment. */
8909 static bool
8910 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8912 bool rval = false;
8913 gfc_expr *lhs;
8914 gfc_expr *rhs;
8915 int llen = 0;
8916 int rlen = 0;
8917 int n;
8918 gfc_ref *ref;
8920 if (gfc_extend_assign (code, ns))
8922 gfc_expr** rhsptr;
8924 if (code->op == EXEC_ASSIGN_CALL)
8926 lhs = code->ext.actual->expr;
8927 rhsptr = &code->ext.actual->next->expr;
8929 else
8931 gfc_actual_arglist* args;
8932 gfc_typebound_proc* tbp;
8934 gcc_assert (code->op == EXEC_COMPCALL);
8936 args = code->expr1->value.compcall.actual;
8937 lhs = args->expr;
8938 rhsptr = &args->next->expr;
8940 tbp = code->expr1->value.compcall.tbp;
8941 gcc_assert (!tbp->is_generic);
8944 /* Make a temporary rhs when there is a default initializer
8945 and rhs is the same symbol as the lhs. */
8946 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8947 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8948 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8949 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8950 *rhsptr = gfc_get_parentheses (*rhsptr);
8952 return true;
8955 lhs = code->expr1;
8956 rhs = code->expr2;
8958 if (rhs->is_boz
8959 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
8960 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8961 &code->loc))
8962 return false;
8964 /* Handle the case of a BOZ literal on the RHS. */
8965 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8967 int rc;
8968 if (gfc_option.warn_surprising)
8969 gfc_warning ("BOZ literal at %L is bitwise transferred "
8970 "non-integer symbol '%s'", &code->loc,
8971 lhs->symtree->n.sym->name);
8973 if (!gfc_convert_boz (rhs, &lhs->ts))
8974 return false;
8975 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8977 if (rc == ARITH_UNDERFLOW)
8978 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8979 ". This check can be disabled with the option "
8980 "-fno-range-check", &rhs->where);
8981 else if (rc == ARITH_OVERFLOW)
8982 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8983 ". This check can be disabled with the option "
8984 "-fno-range-check", &rhs->where);
8985 else if (rc == ARITH_NAN)
8986 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8987 ". This check can be disabled with the option "
8988 "-fno-range-check", &rhs->where);
8989 return false;
8993 if (lhs->ts.type == BT_CHARACTER
8994 && gfc_option.warn_character_truncation)
8996 if (lhs->ts.u.cl != NULL
8997 && lhs->ts.u.cl->length != NULL
8998 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8999 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9001 if (rhs->expr_type == EXPR_CONSTANT)
9002 rlen = rhs->value.character.length;
9004 else if (rhs->ts.u.cl != NULL
9005 && rhs->ts.u.cl->length != NULL
9006 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9007 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9009 if (rlen && llen && rlen > llen)
9010 gfc_warning_now ("CHARACTER expression will be truncated "
9011 "in assignment (%d/%d) at %L",
9012 llen, rlen, &code->loc);
9015 /* Ensure that a vector index expression for the lvalue is evaluated
9016 to a temporary if the lvalue symbol is referenced in it. */
9017 if (lhs->rank)
9019 for (ref = lhs->ref; ref; ref= ref->next)
9020 if (ref->type == REF_ARRAY)
9022 for (n = 0; n < ref->u.ar.dimen; n++)
9023 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9024 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9025 ref->u.ar.start[n]))
9026 ref->u.ar.start[n]
9027 = gfc_get_parentheses (ref->u.ar.start[n]);
9031 if (gfc_pure (NULL))
9033 if (lhs->ts.type == BT_DERIVED
9034 && lhs->expr_type == EXPR_VARIABLE
9035 && lhs->ts.u.derived->attr.pointer_comp
9036 && rhs->expr_type == EXPR_VARIABLE
9037 && (gfc_impure_variable (rhs->symtree->n.sym)
9038 || gfc_is_coindexed (rhs)))
9040 /* F2008, C1283. */
9041 if (gfc_is_coindexed (rhs))
9042 gfc_error ("Coindexed expression at %L is assigned to "
9043 "a derived type variable with a POINTER "
9044 "component in a PURE procedure",
9045 &rhs->where);
9046 else
9047 gfc_error ("The impure variable at %L is assigned to "
9048 "a derived type variable with a POINTER "
9049 "component in a PURE procedure (12.6)",
9050 &rhs->where);
9051 return rval;
9054 /* Fortran 2008, C1283. */
9055 if (gfc_is_coindexed (lhs))
9057 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9058 "procedure", &rhs->where);
9059 return rval;
9063 if (gfc_implicit_pure (NULL))
9065 if (lhs->expr_type == EXPR_VARIABLE
9066 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9067 && lhs->symtree->n.sym->ns != gfc_current_ns)
9068 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9070 if (lhs->ts.type == BT_DERIVED
9071 && lhs->expr_type == EXPR_VARIABLE
9072 && lhs->ts.u.derived->attr.pointer_comp
9073 && rhs->expr_type == EXPR_VARIABLE
9074 && (gfc_impure_variable (rhs->symtree->n.sym)
9075 || gfc_is_coindexed (rhs)))
9076 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9078 /* Fortran 2008, C1283. */
9079 if (gfc_is_coindexed (lhs))
9080 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9083 /* F03:7.4.1.2. */
9084 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9085 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9086 if (lhs->ts.type == BT_CLASS)
9088 gfc_error ("Variable must not be polymorphic in intrinsic assignment at "
9089 "%L - check that there is a matching specific subroutine "
9090 "for '=' operator", &lhs->where);
9091 return false;
9094 /* F2008, Section 7.2.1.2. */
9095 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9097 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9098 "component in assignment at %L", &lhs->where);
9099 return false;
9102 gfc_check_assign (lhs, rhs, 1);
9103 return false;
9107 /* Add a component reference onto an expression. */
9109 static void
9110 add_comp_ref (gfc_expr *e, gfc_component *c)
9112 gfc_ref **ref;
9113 ref = &(e->ref);
9114 while (*ref)
9115 ref = &((*ref)->next);
9116 *ref = gfc_get_ref ();
9117 (*ref)->type = REF_COMPONENT;
9118 (*ref)->u.c.sym = e->ts.u.derived;
9119 (*ref)->u.c.component = c;
9120 e->ts = c->ts;
9122 /* Add a full array ref, as necessary. */
9123 if (c->as)
9125 gfc_add_full_array_ref (e, c->as);
9126 e->rank = c->as->rank;
9131 /* Build an assignment. Keep the argument 'op' for future use, so that
9132 pointer assignments can be made. */
9134 static gfc_code *
9135 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9136 gfc_component *comp1, gfc_component *comp2, locus loc)
9138 gfc_code *this_code;
9140 this_code = gfc_get_code ();
9141 this_code->op = op;
9142 this_code->next = NULL;
9143 this_code->expr1 = gfc_copy_expr (expr1);
9144 this_code->expr2 = gfc_copy_expr (expr2);
9145 this_code->loc = loc;
9146 if (comp1 && comp2)
9148 add_comp_ref (this_code->expr1, comp1);
9149 add_comp_ref (this_code->expr2, comp2);
9152 return this_code;
9156 /* Makes a temporary variable expression based on the characteristics of
9157 a given variable expression. */
9159 static gfc_expr*
9160 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9162 static int serial = 0;
9163 char name[GFC_MAX_SYMBOL_LEN];
9164 gfc_symtree *tmp;
9165 gfc_array_spec *as;
9166 gfc_array_ref *aref;
9167 gfc_ref *ref;
9169 sprintf (name, "DA@%d", serial++);
9170 gfc_get_sym_tree (name, ns, &tmp, false);
9171 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9173 as = NULL;
9174 ref = NULL;
9175 aref = NULL;
9177 /* This function could be expanded to support other expression type
9178 but this is not needed here. */
9179 gcc_assert (e->expr_type == EXPR_VARIABLE);
9181 /* Obtain the arrayspec for the temporary. */
9182 if (e->rank)
9184 aref = gfc_find_array_ref (e);
9185 if (e->expr_type == EXPR_VARIABLE
9186 && e->symtree->n.sym->as == aref->as)
9187 as = aref->as;
9188 else
9190 for (ref = e->ref; ref; ref = ref->next)
9191 if (ref->type == REF_COMPONENT
9192 && ref->u.c.component->as == aref->as)
9194 as = aref->as;
9195 break;
9200 /* Add the attributes and the arrayspec to the temporary. */
9201 tmp->n.sym->attr = gfc_expr_attr (e);
9202 if (as)
9204 tmp->n.sym->as = gfc_copy_array_spec (as);
9205 if (!ref)
9206 ref = e->ref;
9207 if (as->type == AS_DEFERRED)
9208 tmp->n.sym->attr.allocatable = 1;
9210 else
9211 tmp->n.sym->attr.dimension = 0;
9213 gfc_set_sym_referenced (tmp->n.sym);
9214 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
9215 e = gfc_lval_expr_from_sym (tmp->n.sym);
9217 /* Should the lhs be a section, use its array ref for the
9218 temporary expression. */
9219 if (aref && aref->type != AR_FULL)
9221 gfc_free_ref_list (e->ref);
9222 e->ref = gfc_copy_ref (ref);
9224 return e;
9228 /* Add one line of code to the code chain, making sure that 'head' and
9229 'tail' are appropriately updated. */
9231 static void
9232 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9234 gcc_assert (this_code);
9235 if (*head == NULL)
9236 *head = *tail = *this_code;
9237 else
9238 *tail = gfc_append_code (*tail, *this_code);
9239 *this_code = NULL;
9243 /* Counts the potential number of part array references that would
9244 result from resolution of typebound defined assignments. */
9246 static int
9247 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9249 gfc_component *c;
9250 int c_depth = 0, t_depth;
9252 for (c= derived->components; c; c = c->next)
9254 if ((c->ts.type != BT_DERIVED
9255 || c->attr.pointer
9256 || c->attr.allocatable
9257 || c->attr.proc_pointer_comp
9258 || c->attr.class_pointer
9259 || c->attr.proc_pointer)
9260 && !c->attr.defined_assign_comp)
9261 continue;
9263 if (c->as && c_depth == 0)
9264 c_depth = 1;
9266 if (c->ts.u.derived->attr.defined_assign_comp)
9267 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9268 c->as ? 1 : 0);
9269 else
9270 t_depth = 0;
9272 c_depth = t_depth > c_depth ? t_depth : c_depth;
9274 return depth + c_depth;
9278 /* Implement 7.2.1.3 of the F08 standard:
9279 "An intrinsic assignment where the variable is of derived type is
9280 performed as if each component of the variable were assigned from the
9281 corresponding component of expr using pointer assignment (7.2.2) for
9282 each pointer component, defined assignment for each nonpointer
9283 nonallocatable component of a type that has a type-bound defined
9284 assignment consistent with the component, intrinsic assignment for
9285 each other nonpointer nonallocatable component, ..."
9287 The pointer assignments are taken care of by the intrinsic
9288 assignment of the structure itself. This function recursively adds
9289 defined assignments where required. The recursion is accomplished
9290 by calling resolve_code.
9292 When the lhs in a defined assignment has intent INOUT, we need a
9293 temporary for the lhs. In pseudo-code:
9295 ! Only call function lhs once.
9296 if (lhs is not a constant or an variable)
9297 temp_x = expr2
9298 expr2 => temp_x
9299 ! Do the intrinsic assignment
9300 expr1 = expr2
9301 ! Now do the defined assignments
9302 do over components with typebound defined assignment [%cmp]
9303 #if one component's assignment procedure is INOUT
9304 t1 = expr1
9305 #if expr2 non-variable
9306 temp_x = expr2
9307 expr2 => temp_x
9308 # endif
9309 expr1 = expr2
9310 # for each cmp
9311 t1%cmp {defined=} expr2%cmp
9312 expr1%cmp = t1%cmp
9313 #else
9314 expr1 = expr2
9316 # for each cmp
9317 expr1%cmp {defined=} expr2%cmp
9318 #endif
9321 /* The temporary assignments have to be put on top of the additional
9322 code to avoid the result being changed by the intrinsic assignment.
9324 static int component_assignment_level = 0;
9325 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9327 static void
9328 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9330 gfc_component *comp1, *comp2;
9331 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9332 gfc_expr *t1;
9333 int error_count, depth;
9335 gfc_get_errors (NULL, &error_count);
9337 /* Filter out continuing processing after an error. */
9338 if (error_count
9339 || (*code)->expr1->ts.type != BT_DERIVED
9340 || (*code)->expr2->ts.type != BT_DERIVED)
9341 return;
9343 /* TODO: Handle more than one part array reference in assignments. */
9344 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9345 (*code)->expr1->rank ? 1 : 0);
9346 if (depth > 1)
9348 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9349 "done because multiple part array references would "
9350 "occur in intermediate expressions.", &(*code)->loc);
9351 return;
9354 component_assignment_level++;
9356 /* Create a temporary so that functions get called only once. */
9357 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9358 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9360 gfc_expr *tmp_expr;
9362 /* Assign the rhs to the temporary. */
9363 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9364 this_code = build_assignment (EXEC_ASSIGN,
9365 tmp_expr, (*code)->expr2,
9366 NULL, NULL, (*code)->loc);
9367 /* Add the code and substitute the rhs expression. */
9368 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9369 gfc_free_expr ((*code)->expr2);
9370 (*code)->expr2 = tmp_expr;
9373 /* Do the intrinsic assignment. This is not needed if the lhs is one
9374 of the temporaries generated here, since the intrinsic assignment
9375 to the final result already does this. */
9376 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9378 this_code = build_assignment (EXEC_ASSIGN,
9379 (*code)->expr1, (*code)->expr2,
9380 NULL, NULL, (*code)->loc);
9381 add_code_to_chain (&this_code, &head, &tail);
9384 comp1 = (*code)->expr1->ts.u.derived->components;
9385 comp2 = (*code)->expr2->ts.u.derived->components;
9387 t1 = NULL;
9388 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9390 bool inout = false;
9392 /* The intrinsic assignment does the right thing for pointers
9393 of all kinds and allocatable components. */
9394 if (comp1->ts.type != BT_DERIVED
9395 || comp1->attr.pointer
9396 || comp1->attr.allocatable
9397 || comp1->attr.proc_pointer_comp
9398 || comp1->attr.class_pointer
9399 || comp1->attr.proc_pointer)
9400 continue;
9402 /* Make an assigment for this component. */
9403 this_code = build_assignment (EXEC_ASSIGN,
9404 (*code)->expr1, (*code)->expr2,
9405 comp1, comp2, (*code)->loc);
9407 /* Convert the assignment if there is a defined assignment for
9408 this type. Otherwise, using the call from resolve_code,
9409 recurse into its components. */
9410 resolve_code (this_code, ns);
9412 if (this_code->op == EXEC_ASSIGN_CALL)
9414 gfc_formal_arglist *dummy_args;
9415 gfc_symbol *rsym;
9416 /* Check that there is a typebound defined assignment. If not,
9417 then this must be a module defined assignment. We cannot
9418 use the defined_assign_comp attribute here because it must
9419 be this derived type that has the defined assignment and not
9420 a parent type. */
9421 if (!(comp1->ts.u.derived->f2k_derived
9422 && comp1->ts.u.derived->f2k_derived
9423 ->tb_op[INTRINSIC_ASSIGN]))
9425 gfc_free_statements (this_code);
9426 this_code = NULL;
9427 continue;
9430 /* If the first argument of the subroutine has intent INOUT
9431 a temporary must be generated and used instead. */
9432 rsym = this_code->resolved_sym;
9433 dummy_args = gfc_sym_get_dummy_args (rsym);
9434 if (dummy_args
9435 && dummy_args->sym->attr.intent == INTENT_INOUT)
9437 gfc_code *temp_code;
9438 inout = true;
9440 /* Build the temporary required for the assignment and put
9441 it at the head of the generated code. */
9442 if (!t1)
9444 t1 = get_temp_from_expr ((*code)->expr1, ns);
9445 temp_code = build_assignment (EXEC_ASSIGN,
9446 t1, (*code)->expr1,
9447 NULL, NULL, (*code)->loc);
9448 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9451 /* Replace the first actual arg with the component of the
9452 temporary. */
9453 gfc_free_expr (this_code->ext.actual->expr);
9454 this_code->ext.actual->expr = gfc_copy_expr (t1);
9455 add_comp_ref (this_code->ext.actual->expr, comp1);
9458 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9460 /* Don't add intrinsic assignments since they are already
9461 effected by the intrinsic assignment of the structure. */
9462 gfc_free_statements (this_code);
9463 this_code = NULL;
9464 continue;
9467 add_code_to_chain (&this_code, &head, &tail);
9469 if (t1 && inout)
9471 /* Transfer the value to the final result. */
9472 this_code = build_assignment (EXEC_ASSIGN,
9473 (*code)->expr1, t1,
9474 comp1, comp2, (*code)->loc);
9475 add_code_to_chain (&this_code, &head, &tail);
9479 /* This is probably not necessary. */
9480 if (this_code)
9482 gfc_free_statements (this_code);
9483 this_code = NULL;
9486 /* Put the temporary assignments at the top of the generated code. */
9487 if (tmp_head && component_assignment_level == 1)
9489 gfc_append_code (tmp_head, head);
9490 head = tmp_head;
9491 tmp_head = tmp_tail = NULL;
9494 /* Now attach the remaining code chain to the input code. Step on
9495 to the end of the new code since resolution is complete. */
9496 gcc_assert ((*code)->op == EXEC_ASSIGN);
9497 tail->next = (*code)->next;
9498 /* Overwrite 'code' because this would place the intrinsic assignment
9499 before the temporary for the lhs is created. */
9500 gfc_free_expr ((*code)->expr1);
9501 gfc_free_expr ((*code)->expr2);
9502 **code = *head;
9503 free (head);
9504 *code = tail;
9506 component_assignment_level--;
9510 /* Given a block of code, recursively resolve everything pointed to by this
9511 code block. */
9513 static void
9514 resolve_code (gfc_code *code, gfc_namespace *ns)
9516 int omp_workshare_save;
9517 int forall_save, do_concurrent_save;
9518 code_stack frame;
9519 bool t;
9521 frame.prev = cs_base;
9522 frame.head = code;
9523 cs_base = &frame;
9525 find_reachable_labels (code);
9527 for (; code; code = code->next)
9529 frame.current = code;
9530 forall_save = forall_flag;
9531 do_concurrent_save = do_concurrent_flag;
9533 if (code->op == EXEC_FORALL)
9535 forall_flag = 1;
9536 gfc_resolve_forall (code, ns, forall_save);
9537 forall_flag = 2;
9539 else if (code->block)
9541 omp_workshare_save = -1;
9542 switch (code->op)
9544 case EXEC_OMP_PARALLEL_WORKSHARE:
9545 omp_workshare_save = omp_workshare_flag;
9546 omp_workshare_flag = 1;
9547 gfc_resolve_omp_parallel_blocks (code, ns);
9548 break;
9549 case EXEC_OMP_PARALLEL:
9550 case EXEC_OMP_PARALLEL_DO:
9551 case EXEC_OMP_PARALLEL_SECTIONS:
9552 case EXEC_OMP_TASK:
9553 omp_workshare_save = omp_workshare_flag;
9554 omp_workshare_flag = 0;
9555 gfc_resolve_omp_parallel_blocks (code, ns);
9556 break;
9557 case EXEC_OMP_DO:
9558 gfc_resolve_omp_do_blocks (code, ns);
9559 break;
9560 case EXEC_SELECT_TYPE:
9561 /* Blocks are handled in resolve_select_type because we have
9562 to transform the SELECT TYPE into ASSOCIATE first. */
9563 break;
9564 case EXEC_DO_CONCURRENT:
9565 do_concurrent_flag = 1;
9566 gfc_resolve_blocks (code->block, ns);
9567 do_concurrent_flag = 2;
9568 break;
9569 case EXEC_OMP_WORKSHARE:
9570 omp_workshare_save = omp_workshare_flag;
9571 omp_workshare_flag = 1;
9572 /* FALL THROUGH */
9573 default:
9574 gfc_resolve_blocks (code->block, ns);
9575 break;
9578 if (omp_workshare_save != -1)
9579 omp_workshare_flag = omp_workshare_save;
9582 t = true;
9583 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9584 t = gfc_resolve_expr (code->expr1);
9585 forall_flag = forall_save;
9586 do_concurrent_flag = do_concurrent_save;
9588 if (!gfc_resolve_expr (code->expr2))
9589 t = false;
9591 if (code->op == EXEC_ALLOCATE
9592 && !gfc_resolve_expr (code->expr3))
9593 t = false;
9595 switch (code->op)
9597 case EXEC_NOP:
9598 case EXEC_END_BLOCK:
9599 case EXEC_END_NESTED_BLOCK:
9600 case EXEC_CYCLE:
9601 case EXEC_PAUSE:
9602 case EXEC_STOP:
9603 case EXEC_ERROR_STOP:
9604 case EXEC_EXIT:
9605 case EXEC_CONTINUE:
9606 case EXEC_DT_END:
9607 case EXEC_ASSIGN_CALL:
9608 case EXEC_CRITICAL:
9609 break;
9611 case EXEC_SYNC_ALL:
9612 case EXEC_SYNC_IMAGES:
9613 case EXEC_SYNC_MEMORY:
9614 resolve_sync (code);
9615 break;
9617 case EXEC_LOCK:
9618 case EXEC_UNLOCK:
9619 resolve_lock_unlock (code);
9620 break;
9622 case EXEC_ENTRY:
9623 /* Keep track of which entry we are up to. */
9624 current_entry_id = code->ext.entry->id;
9625 break;
9627 case EXEC_WHERE:
9628 resolve_where (code, NULL);
9629 break;
9631 case EXEC_GOTO:
9632 if (code->expr1 != NULL)
9634 if (code->expr1->ts.type != BT_INTEGER)
9635 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9636 "INTEGER variable", &code->expr1->where);
9637 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9638 gfc_error ("Variable '%s' has not been assigned a target "
9639 "label at %L", code->expr1->symtree->n.sym->name,
9640 &code->expr1->where);
9642 else
9643 resolve_branch (code->label1, code);
9644 break;
9646 case EXEC_RETURN:
9647 if (code->expr1 != NULL
9648 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9649 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9650 "INTEGER return specifier", &code->expr1->where);
9651 break;
9653 case EXEC_INIT_ASSIGN:
9654 case EXEC_END_PROCEDURE:
9655 break;
9657 case EXEC_ASSIGN:
9658 if (!t)
9659 break;
9661 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9662 _("assignment")))
9663 break;
9665 if (resolve_ordinary_assign (code, ns))
9667 if (code->op == EXEC_COMPCALL)
9668 goto compcall;
9669 else
9670 goto call;
9673 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9674 if (code->expr1->ts.type == BT_DERIVED
9675 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9676 generate_component_assignments (&code, ns);
9678 break;
9680 case EXEC_LABEL_ASSIGN:
9681 if (code->label1->defined == ST_LABEL_UNKNOWN)
9682 gfc_error ("Label %d referenced at %L is never defined",
9683 code->label1->value, &code->label1->where);
9684 if (t
9685 && (code->expr1->expr_type != EXPR_VARIABLE
9686 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9687 || code->expr1->symtree->n.sym->ts.kind
9688 != gfc_default_integer_kind
9689 || code->expr1->symtree->n.sym->as != NULL))
9690 gfc_error ("ASSIGN statement at %L requires a scalar "
9691 "default INTEGER variable", &code->expr1->where);
9692 break;
9694 case EXEC_POINTER_ASSIGN:
9696 gfc_expr* e;
9698 if (!t)
9699 break;
9701 /* This is both a variable definition and pointer assignment
9702 context, so check both of them. For rank remapping, a final
9703 array ref may be present on the LHS and fool gfc_expr_attr
9704 used in gfc_check_vardef_context. Remove it. */
9705 e = remove_last_array_ref (code->expr1);
9706 t = gfc_check_vardef_context (e, true, false, false,
9707 _("pointer assignment"));
9708 if (t)
9709 t = gfc_check_vardef_context (e, false, false, false,
9710 _("pointer assignment"));
9711 gfc_free_expr (e);
9712 if (!t)
9713 break;
9715 gfc_check_pointer_assign (code->expr1, code->expr2);
9716 break;
9719 case EXEC_ARITHMETIC_IF:
9720 if (t
9721 && code->expr1->ts.type != BT_INTEGER
9722 && code->expr1->ts.type != BT_REAL)
9723 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9724 "expression", &code->expr1->where);
9726 resolve_branch (code->label1, code);
9727 resolve_branch (code->label2, code);
9728 resolve_branch (code->label3, code);
9729 break;
9731 case EXEC_IF:
9732 if (t && code->expr1 != NULL
9733 && (code->expr1->ts.type != BT_LOGICAL
9734 || code->expr1->rank != 0))
9735 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9736 &code->expr1->where);
9737 break;
9739 case EXEC_CALL:
9740 call:
9741 resolve_call (code);
9742 break;
9744 case EXEC_COMPCALL:
9745 compcall:
9746 resolve_typebound_subroutine (code);
9747 break;
9749 case EXEC_CALL_PPC:
9750 resolve_ppc_call (code);
9751 break;
9753 case EXEC_SELECT:
9754 /* Select is complicated. Also, a SELECT construct could be
9755 a transformed computed GOTO. */
9756 resolve_select (code, false);
9757 break;
9759 case EXEC_SELECT_TYPE:
9760 resolve_select_type (code, ns);
9761 break;
9763 case EXEC_BLOCK:
9764 resolve_block_construct (code);
9765 break;
9767 case EXEC_DO:
9768 if (code->ext.iterator != NULL)
9770 gfc_iterator *iter = code->ext.iterator;
9771 if (gfc_resolve_iterator (iter, true, false))
9772 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9774 break;
9776 case EXEC_DO_WHILE:
9777 if (code->expr1 == NULL)
9778 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9779 if (t
9780 && (code->expr1->rank != 0
9781 || code->expr1->ts.type != BT_LOGICAL))
9782 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9783 "a scalar LOGICAL expression", &code->expr1->where);
9784 break;
9786 case EXEC_ALLOCATE:
9787 if (t)
9788 resolve_allocate_deallocate (code, "ALLOCATE");
9790 break;
9792 case EXEC_DEALLOCATE:
9793 if (t)
9794 resolve_allocate_deallocate (code, "DEALLOCATE");
9796 break;
9798 case EXEC_OPEN:
9799 if (!gfc_resolve_open (code->ext.open))
9800 break;
9802 resolve_branch (code->ext.open->err, code);
9803 break;
9805 case EXEC_CLOSE:
9806 if (!gfc_resolve_close (code->ext.close))
9807 break;
9809 resolve_branch (code->ext.close->err, code);
9810 break;
9812 case EXEC_BACKSPACE:
9813 case EXEC_ENDFILE:
9814 case EXEC_REWIND:
9815 case EXEC_FLUSH:
9816 if (!gfc_resolve_filepos (code->ext.filepos))
9817 break;
9819 resolve_branch (code->ext.filepos->err, code);
9820 break;
9822 case EXEC_INQUIRE:
9823 if (!gfc_resolve_inquire (code->ext.inquire))
9824 break;
9826 resolve_branch (code->ext.inquire->err, code);
9827 break;
9829 case EXEC_IOLENGTH:
9830 gcc_assert (code->ext.inquire != NULL);
9831 if (!gfc_resolve_inquire (code->ext.inquire))
9832 break;
9834 resolve_branch (code->ext.inquire->err, code);
9835 break;
9837 case EXEC_WAIT:
9838 if (!gfc_resolve_wait (code->ext.wait))
9839 break;
9841 resolve_branch (code->ext.wait->err, code);
9842 resolve_branch (code->ext.wait->end, code);
9843 resolve_branch (code->ext.wait->eor, code);
9844 break;
9846 case EXEC_READ:
9847 case EXEC_WRITE:
9848 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
9849 break;
9851 resolve_branch (code->ext.dt->err, code);
9852 resolve_branch (code->ext.dt->end, code);
9853 resolve_branch (code->ext.dt->eor, code);
9854 break;
9856 case EXEC_TRANSFER:
9857 resolve_transfer (code);
9858 break;
9860 case EXEC_DO_CONCURRENT:
9861 case EXEC_FORALL:
9862 resolve_forall_iterators (code->ext.forall_iterator);
9864 if (code->expr1 != NULL
9865 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9866 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9867 "expression", &code->expr1->where);
9868 break;
9870 case EXEC_OMP_ATOMIC:
9871 case EXEC_OMP_BARRIER:
9872 case EXEC_OMP_CRITICAL:
9873 case EXEC_OMP_FLUSH:
9874 case EXEC_OMP_DO:
9875 case EXEC_OMP_MASTER:
9876 case EXEC_OMP_ORDERED:
9877 case EXEC_OMP_SECTIONS:
9878 case EXEC_OMP_SINGLE:
9879 case EXEC_OMP_TASKWAIT:
9880 case EXEC_OMP_TASKYIELD:
9881 case EXEC_OMP_WORKSHARE:
9882 gfc_resolve_omp_directive (code, ns);
9883 break;
9885 case EXEC_OMP_PARALLEL:
9886 case EXEC_OMP_PARALLEL_DO:
9887 case EXEC_OMP_PARALLEL_SECTIONS:
9888 case EXEC_OMP_PARALLEL_WORKSHARE:
9889 case EXEC_OMP_TASK:
9890 omp_workshare_save = omp_workshare_flag;
9891 omp_workshare_flag = 0;
9892 gfc_resolve_omp_directive (code, ns);
9893 omp_workshare_flag = omp_workshare_save;
9894 break;
9896 default:
9897 gfc_internal_error ("resolve_code(): Bad statement code");
9901 cs_base = frame.prev;
9905 /* Resolve initial values and make sure they are compatible with
9906 the variable. */
9908 static void
9909 resolve_values (gfc_symbol *sym)
9911 bool t;
9913 if (sym->value == NULL)
9914 return;
9916 if (sym->value->expr_type == EXPR_STRUCTURE)
9917 t= resolve_structure_cons (sym->value, 1);
9918 else
9919 t = gfc_resolve_expr (sym->value);
9921 if (!t)
9922 return;
9924 gfc_check_assign_symbol (sym, NULL, sym->value);
9928 /* Verify the binding labels for common blocks that are BIND(C). The label
9929 for a BIND(C) common block must be identical in all scoping units in which
9930 the common block is declared. Further, the binding label can not collide
9931 with any other global entity in the program. */
9933 static void
9934 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9936 if (comm_block_tree->n.common->is_bind_c == 1)
9938 gfc_gsymbol *binding_label_gsym;
9939 gfc_gsymbol *comm_name_gsym;
9940 const char * bind_label = comm_block_tree->n.common->binding_label
9941 ? comm_block_tree->n.common->binding_label : "";
9943 /* See if a global symbol exists by the common block's name. It may
9944 be NULL if the common block is use-associated. */
9945 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9946 comm_block_tree->n.common->name);
9947 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9948 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9949 "with the global entity '%s' at %L",
9950 bind_label,
9951 comm_block_tree->n.common->name,
9952 &(comm_block_tree->n.common->where),
9953 comm_name_gsym->name, &(comm_name_gsym->where));
9954 else if (comm_name_gsym != NULL
9955 && strcmp (comm_name_gsym->name,
9956 comm_block_tree->n.common->name) == 0)
9958 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9959 as expected. */
9960 if (comm_name_gsym->binding_label == NULL)
9961 /* No binding label for common block stored yet; save this one. */
9962 comm_name_gsym->binding_label = bind_label;
9963 else if (strcmp (comm_name_gsym->binding_label, bind_label) != 0)
9965 /* Common block names match but binding labels do not. */
9966 gfc_error ("Binding label '%s' for common block '%s' at %L "
9967 "does not match the binding label '%s' for common "
9968 "block '%s' at %L",
9969 bind_label,
9970 comm_block_tree->n.common->name,
9971 &(comm_block_tree->n.common->where),
9972 comm_name_gsym->binding_label,
9973 comm_name_gsym->name,
9974 &(comm_name_gsym->where));
9975 return;
9979 /* There is no binding label (NAME="") so we have nothing further to
9980 check and nothing to add as a global symbol for the label. */
9981 if (!comm_block_tree->n.common->binding_label)
9982 return;
9984 binding_label_gsym =
9985 gfc_find_gsymbol (gfc_gsym_root,
9986 comm_block_tree->n.common->binding_label);
9987 if (binding_label_gsym == NULL)
9989 /* Need to make a global symbol for the binding label to prevent
9990 it from colliding with another. */
9991 binding_label_gsym =
9992 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9993 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9994 binding_label_gsym->type = GSYM_COMMON;
9996 else
9998 /* If comm_name_gsym is NULL, the name common block is use
9999 associated and the name could be colliding. */
10000 if (binding_label_gsym->type != GSYM_COMMON)
10001 gfc_error ("Binding label '%s' for common block '%s' at %L "
10002 "collides with the global entity '%s' at %L",
10003 comm_block_tree->n.common->binding_label,
10004 comm_block_tree->n.common->name,
10005 &(comm_block_tree->n.common->where),
10006 binding_label_gsym->name,
10007 &(binding_label_gsym->where));
10008 else if (comm_name_gsym != NULL
10009 && (strcmp (binding_label_gsym->name,
10010 comm_name_gsym->binding_label) != 0)
10011 && (strcmp (binding_label_gsym->sym_name,
10012 comm_name_gsym->name) != 0))
10013 gfc_error ("Binding label '%s' for common block '%s' at %L "
10014 "collides with global entity '%s' at %L",
10015 binding_label_gsym->name, binding_label_gsym->sym_name,
10016 &(comm_block_tree->n.common->where),
10017 comm_name_gsym->name, &(comm_name_gsym->where));
10021 return;
10025 /* Verify any BIND(C) derived types in the namespace so we can report errors
10026 for them once, rather than for each variable declared of that type. */
10028 static void
10029 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10031 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10032 && derived_sym->attr.is_bind_c == 1)
10033 verify_bind_c_derived_type (derived_sym);
10035 return;
10039 /* Verify that any binding labels used in a given namespace do not collide
10040 with the names or binding labels of any global symbols. */
10042 static void
10043 gfc_verify_binding_labels (gfc_symbol *sym)
10045 int has_error = 0;
10047 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
10048 && sym->attr.flavor != FL_DERIVED && sym->binding_label)
10050 gfc_gsymbol *bind_c_sym;
10052 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10053 if (bind_c_sym != NULL
10054 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
10056 if (sym->attr.if_source == IFSRC_DECL
10057 && (bind_c_sym->type != GSYM_SUBROUTINE
10058 && bind_c_sym->type != GSYM_FUNCTION)
10059 && ((sym->attr.contained == 1
10060 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
10061 || (sym->attr.use_assoc == 1
10062 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
10064 /* Make sure global procedures don't collide with anything. */
10065 gfc_error ("Binding label '%s' at %L collides with the global "
10066 "entity '%s' at %L", sym->binding_label,
10067 &(sym->declared_at), bind_c_sym->name,
10068 &(bind_c_sym->where));
10069 has_error = 1;
10071 else if (sym->attr.contained == 0
10072 && (sym->attr.if_source == IFSRC_IFBODY
10073 && sym->attr.flavor == FL_PROCEDURE)
10074 && (bind_c_sym->sym_name != NULL
10075 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
10077 /* Make sure procedures in interface bodies don't collide. */
10078 gfc_error ("Binding label '%s' in interface body at %L collides "
10079 "with the global entity '%s' at %L",
10080 sym->binding_label,
10081 &(sym->declared_at), bind_c_sym->name,
10082 &(bind_c_sym->where));
10083 has_error = 1;
10085 else if (sym->attr.contained == 0
10086 && sym->attr.if_source == IFSRC_UNKNOWN)
10087 if ((sym->attr.use_assoc && bind_c_sym->mod_name
10088 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
10089 || sym->attr.use_assoc == 0)
10091 gfc_error ("Binding label '%s' at %L collides with global "
10092 "entity '%s' at %L", sym->binding_label,
10093 &(sym->declared_at), bind_c_sym->name,
10094 &(bind_c_sym->where));
10095 has_error = 1;
10098 if (has_error != 0)
10099 /* Clear the binding label to prevent checking multiple times. */
10100 sym->binding_label = NULL;
10102 else if (bind_c_sym == NULL)
10104 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
10105 bind_c_sym->where = sym->declared_at;
10106 bind_c_sym->sym_name = sym->name;
10108 if (sym->attr.use_assoc == 1)
10109 bind_c_sym->mod_name = sym->module;
10110 else
10111 if (sym->ns->proc_name != NULL)
10112 bind_c_sym->mod_name = sym->ns->proc_name->name;
10114 if (sym->attr.contained == 0)
10116 if (sym->attr.subroutine)
10117 bind_c_sym->type = GSYM_SUBROUTINE;
10118 else if (sym->attr.function)
10119 bind_c_sym->type = GSYM_FUNCTION;
10123 return;
10127 /* Resolve an index expression. */
10129 static bool
10130 resolve_index_expr (gfc_expr *e)
10132 if (!gfc_resolve_expr (e))
10133 return false;
10135 if (!gfc_simplify_expr (e, 0))
10136 return false;
10138 if (!gfc_specification_expr (e))
10139 return false;
10141 return true;
10145 /* Resolve a charlen structure. */
10147 static bool
10148 resolve_charlen (gfc_charlen *cl)
10150 int i, k;
10151 bool saved_specification_expr;
10153 if (cl->resolved)
10154 return true;
10156 cl->resolved = 1;
10157 saved_specification_expr = specification_expr;
10158 specification_expr = true;
10160 if (cl->length_from_typespec)
10162 if (!gfc_resolve_expr (cl->length))
10164 specification_expr = saved_specification_expr;
10165 return false;
10168 if (!gfc_simplify_expr (cl->length, 0))
10170 specification_expr = saved_specification_expr;
10171 return false;
10174 else
10177 if (!resolve_index_expr (cl->length))
10179 specification_expr = saved_specification_expr;
10180 return false;
10184 /* "If the character length parameter value evaluates to a negative
10185 value, the length of character entities declared is zero." */
10186 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10188 if (gfc_option.warn_surprising)
10189 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10190 " the length has been set to zero",
10191 &cl->length->where, i);
10192 gfc_replace_expr (cl->length,
10193 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10196 /* Check that the character length is not too large. */
10197 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10198 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10199 && cl->length->ts.type == BT_INTEGER
10200 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10202 gfc_error ("String length at %L is too large", &cl->length->where);
10203 specification_expr = saved_specification_expr;
10204 return false;
10207 specification_expr = saved_specification_expr;
10208 return true;
10212 /* Test for non-constant shape arrays. */
10214 static bool
10215 is_non_constant_shape_array (gfc_symbol *sym)
10217 gfc_expr *e;
10218 int i;
10219 bool not_constant;
10221 not_constant = false;
10222 if (sym->as != NULL)
10224 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10225 has not been simplified; parameter array references. Do the
10226 simplification now. */
10227 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10229 e = sym->as->lower[i];
10230 if (e && (!resolve_index_expr(e)
10231 || !gfc_is_constant_expr (e)))
10232 not_constant = true;
10233 e = sym->as->upper[i];
10234 if (e && (!resolve_index_expr(e)
10235 || !gfc_is_constant_expr (e)))
10236 not_constant = true;
10239 return not_constant;
10242 /* Given a symbol and an initialization expression, add code to initialize
10243 the symbol to the function entry. */
10244 static void
10245 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10247 gfc_expr *lval;
10248 gfc_code *init_st;
10249 gfc_namespace *ns = sym->ns;
10251 /* Search for the function namespace if this is a contained
10252 function without an explicit result. */
10253 if (sym->attr.function && sym == sym->result
10254 && sym->name != sym->ns->proc_name->name)
10256 ns = ns->contained;
10257 for (;ns; ns = ns->sibling)
10258 if (strcmp (ns->proc_name->name, sym->name) == 0)
10259 break;
10262 if (ns == NULL)
10264 gfc_free_expr (init);
10265 return;
10268 /* Build an l-value expression for the result. */
10269 lval = gfc_lval_expr_from_sym (sym);
10271 /* Add the code at scope entry. */
10272 init_st = gfc_get_code ();
10273 init_st->next = ns->code;
10274 ns->code = init_st;
10276 /* Assign the default initializer to the l-value. */
10277 init_st->loc = sym->declared_at;
10278 init_st->op = EXEC_INIT_ASSIGN;
10279 init_st->expr1 = lval;
10280 init_st->expr2 = init;
10283 /* Assign the default initializer to a derived type variable or result. */
10285 static void
10286 apply_default_init (gfc_symbol *sym)
10288 gfc_expr *init = NULL;
10290 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10291 return;
10293 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10294 init = gfc_default_initializer (&sym->ts);
10296 if (init == NULL && sym->ts.type != BT_CLASS)
10297 return;
10299 build_init_assign (sym, init);
10300 sym->attr.referenced = 1;
10303 /* Build an initializer for a local integer, real, complex, logical, or
10304 character variable, based on the command line flags finit-local-zero,
10305 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10306 null if the symbol should not have a default initialization. */
10307 static gfc_expr *
10308 build_default_init_expr (gfc_symbol *sym)
10310 int char_len;
10311 gfc_expr *init_expr;
10312 int i;
10314 /* These symbols should never have a default initialization. */
10315 if (sym->attr.allocatable
10316 || sym->attr.external
10317 || sym->attr.dummy
10318 || sym->attr.pointer
10319 || sym->attr.in_equivalence
10320 || sym->attr.in_common
10321 || sym->attr.data
10322 || sym->module
10323 || sym->attr.cray_pointee
10324 || sym->attr.cray_pointer
10325 || sym->assoc)
10326 return NULL;
10328 /* Now we'll try to build an initializer expression. */
10329 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10330 &sym->declared_at);
10332 /* We will only initialize integers, reals, complex, logicals, and
10333 characters, and only if the corresponding command-line flags
10334 were set. Otherwise, we free init_expr and return null. */
10335 switch (sym->ts.type)
10337 case BT_INTEGER:
10338 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10339 mpz_set_si (init_expr->value.integer,
10340 gfc_option.flag_init_integer_value);
10341 else
10343 gfc_free_expr (init_expr);
10344 init_expr = NULL;
10346 break;
10348 case BT_REAL:
10349 switch (gfc_option.flag_init_real)
10351 case GFC_INIT_REAL_SNAN:
10352 init_expr->is_snan = 1;
10353 /* Fall through. */
10354 case GFC_INIT_REAL_NAN:
10355 mpfr_set_nan (init_expr->value.real);
10356 break;
10358 case GFC_INIT_REAL_INF:
10359 mpfr_set_inf (init_expr->value.real, 1);
10360 break;
10362 case GFC_INIT_REAL_NEG_INF:
10363 mpfr_set_inf (init_expr->value.real, -1);
10364 break;
10366 case GFC_INIT_REAL_ZERO:
10367 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10368 break;
10370 default:
10371 gfc_free_expr (init_expr);
10372 init_expr = NULL;
10373 break;
10375 break;
10377 case BT_COMPLEX:
10378 switch (gfc_option.flag_init_real)
10380 case GFC_INIT_REAL_SNAN:
10381 init_expr->is_snan = 1;
10382 /* Fall through. */
10383 case GFC_INIT_REAL_NAN:
10384 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10385 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10386 break;
10388 case GFC_INIT_REAL_INF:
10389 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10390 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10391 break;
10393 case GFC_INIT_REAL_NEG_INF:
10394 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10395 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10396 break;
10398 case GFC_INIT_REAL_ZERO:
10399 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10400 break;
10402 default:
10403 gfc_free_expr (init_expr);
10404 init_expr = NULL;
10405 break;
10407 break;
10409 case BT_LOGICAL:
10410 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10411 init_expr->value.logical = 0;
10412 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10413 init_expr->value.logical = 1;
10414 else
10416 gfc_free_expr (init_expr);
10417 init_expr = NULL;
10419 break;
10421 case BT_CHARACTER:
10422 /* For characters, the length must be constant in order to
10423 create a default initializer. */
10424 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10425 && sym->ts.u.cl->length
10426 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10428 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10429 init_expr->value.character.length = char_len;
10430 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10431 for (i = 0; i < char_len; i++)
10432 init_expr->value.character.string[i]
10433 = (unsigned char) gfc_option.flag_init_character_value;
10435 else
10437 gfc_free_expr (init_expr);
10438 init_expr = NULL;
10440 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10441 && sym->ts.u.cl->length)
10443 gfc_actual_arglist *arg;
10444 init_expr = gfc_get_expr ();
10445 init_expr->where = sym->declared_at;
10446 init_expr->ts = sym->ts;
10447 init_expr->expr_type = EXPR_FUNCTION;
10448 init_expr->value.function.isym =
10449 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10450 init_expr->value.function.name = "repeat";
10451 arg = gfc_get_actual_arglist ();
10452 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10453 NULL, 1);
10454 arg->expr->value.character.string[0]
10455 = gfc_option.flag_init_character_value;
10456 arg->next = gfc_get_actual_arglist ();
10457 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10458 init_expr->value.function.actual = arg;
10460 break;
10462 default:
10463 gfc_free_expr (init_expr);
10464 init_expr = NULL;
10466 return init_expr;
10469 /* Add an initialization expression to a local variable. */
10470 static void
10471 apply_default_init_local (gfc_symbol *sym)
10473 gfc_expr *init = NULL;
10475 /* The symbol should be a variable or a function return value. */
10476 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10477 || (sym->attr.function && sym->result != sym))
10478 return;
10480 /* Try to build the initializer expression. If we can't initialize
10481 this symbol, then init will be NULL. */
10482 init = build_default_init_expr (sym);
10483 if (init == NULL)
10484 return;
10486 /* For saved variables, we don't want to add an initializer at function
10487 entry, so we just add a static initializer. Note that automatic variables
10488 are stack allocated even with -fno-automatic; we have also to exclude
10489 result variable, which are also nonstatic. */
10490 if (sym->attr.save || sym->ns->save_all
10491 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10492 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10494 /* Don't clobber an existing initializer! */
10495 gcc_assert (sym->value == NULL);
10496 sym->value = init;
10497 return;
10500 build_init_assign (sym, init);
10504 /* Resolution of common features of flavors variable and procedure. */
10506 static bool
10507 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10509 gfc_array_spec *as;
10511 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10512 as = CLASS_DATA (sym)->as;
10513 else
10514 as = sym->as;
10516 /* Constraints on deferred shape variable. */
10517 if (as == NULL || as->type != AS_DEFERRED)
10519 bool pointer, allocatable, dimension;
10521 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10523 pointer = CLASS_DATA (sym)->attr.class_pointer;
10524 allocatable = CLASS_DATA (sym)->attr.allocatable;
10525 dimension = CLASS_DATA (sym)->attr.dimension;
10527 else
10529 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10530 allocatable = sym->attr.allocatable;
10531 dimension = sym->attr.dimension;
10534 if (allocatable)
10536 if (dimension && as->type != AS_ASSUMED_RANK)
10538 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10539 "shape or assumed rank", sym->name, &sym->declared_at);
10540 return false;
10542 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10543 "'%s' at %L may not be ALLOCATABLE",
10544 sym->name, &sym->declared_at))
10545 return false;
10548 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10550 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10551 "assumed rank", sym->name, &sym->declared_at);
10552 return false;
10555 else
10557 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10558 && sym->ts.type != BT_CLASS && !sym->assoc)
10560 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10561 sym->name, &sym->declared_at);
10562 return false;
10566 /* Constraints on polymorphic variables. */
10567 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10569 /* F03:C502. */
10570 if (sym->attr.class_ok
10571 && !sym->attr.select_type_temporary
10572 && !UNLIMITED_POLY (sym)
10573 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10575 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10576 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10577 &sym->declared_at);
10578 return false;
10581 /* F03:C509. */
10582 /* Assume that use associated symbols were checked in the module ns.
10583 Class-variables that are associate-names are also something special
10584 and excepted from the test. */
10585 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10587 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10588 "or pointer", sym->name, &sym->declared_at);
10589 return false;
10593 return true;
10597 /* Additional checks for symbols with flavor variable and derived
10598 type. To be called from resolve_fl_variable. */
10600 static bool
10601 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10603 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10605 /* Check to see if a derived type is blocked from being host
10606 associated by the presence of another class I symbol in the same
10607 namespace. 14.6.1.3 of the standard and the discussion on
10608 comp.lang.fortran. */
10609 if (sym->ns != sym->ts.u.derived->ns
10610 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10612 gfc_symbol *s;
10613 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10614 if (s && s->attr.generic)
10615 s = gfc_find_dt_in_generic (s);
10616 if (s && s->attr.flavor != FL_DERIVED)
10618 gfc_error ("The type '%s' cannot be host associated at %L "
10619 "because it is blocked by an incompatible object "
10620 "of the same name declared at %L",
10621 sym->ts.u.derived->name, &sym->declared_at,
10622 &s->declared_at);
10623 return false;
10627 /* 4th constraint in section 11.3: "If an object of a type for which
10628 component-initialization is specified (R429) appears in the
10629 specification-part of a module and does not have the ALLOCATABLE
10630 or POINTER attribute, the object shall have the SAVE attribute."
10632 The check for initializers is performed with
10633 gfc_has_default_initializer because gfc_default_initializer generates
10634 a hidden default for allocatable components. */
10635 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10636 && sym->ns->proc_name->attr.flavor == FL_MODULE
10637 && !sym->ns->save_all && !sym->attr.save
10638 && !sym->attr.pointer && !sym->attr.allocatable
10639 && gfc_has_default_initializer (sym->ts.u.derived)
10640 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10641 "'%s' at %L, needed due to the default "
10642 "initialization", sym->name, &sym->declared_at))
10643 return false;
10645 /* Assign default initializer. */
10646 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10647 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10649 sym->value = gfc_default_initializer (&sym->ts);
10652 return true;
10656 /* Resolve symbols with flavor variable. */
10658 static bool
10659 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10661 int no_init_flag, automatic_flag;
10662 gfc_expr *e;
10663 const char *auto_save_msg;
10664 bool saved_specification_expr;
10666 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10667 "SAVE attribute";
10669 if (!resolve_fl_var_and_proc (sym, mp_flag))
10670 return false;
10672 /* Set this flag to check that variables are parameters of all entries.
10673 This check is effected by the call to gfc_resolve_expr through
10674 is_non_constant_shape_array. */
10675 saved_specification_expr = specification_expr;
10676 specification_expr = true;
10678 if (sym->ns->proc_name
10679 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10680 || sym->ns->proc_name->attr.is_main_program)
10681 && !sym->attr.use_assoc
10682 && !sym->attr.allocatable
10683 && !sym->attr.pointer
10684 && is_non_constant_shape_array (sym))
10686 /* The shape of a main program or module array needs to be
10687 constant. */
10688 gfc_error ("The module or main program array '%s' at %L must "
10689 "have constant shape", sym->name, &sym->declared_at);
10690 specification_expr = saved_specification_expr;
10691 return false;
10694 /* Constraints on deferred type parameter. */
10695 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10697 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10698 "requires either the pointer or allocatable attribute",
10699 sym->name, &sym->declared_at);
10700 specification_expr = saved_specification_expr;
10701 return false;
10704 if (sym->ts.type == BT_CHARACTER)
10706 /* Make sure that character string variables with assumed length are
10707 dummy arguments. */
10708 e = sym->ts.u.cl->length;
10709 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10710 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10712 gfc_error ("Entity with assumed character length at %L must be a "
10713 "dummy argument or a PARAMETER", &sym->declared_at);
10714 specification_expr = saved_specification_expr;
10715 return false;
10718 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10720 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10721 specification_expr = saved_specification_expr;
10722 return false;
10725 if (!gfc_is_constant_expr (e)
10726 && !(e->expr_type == EXPR_VARIABLE
10727 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10729 if (!sym->attr.use_assoc && sym->ns->proc_name
10730 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10731 || sym->ns->proc_name->attr.is_main_program))
10733 gfc_error ("'%s' at %L must have constant character length "
10734 "in this context", sym->name, &sym->declared_at);
10735 specification_expr = saved_specification_expr;
10736 return false;
10738 if (sym->attr.in_common)
10740 gfc_error ("COMMON variable '%s' at %L must have constant "
10741 "character length", sym->name, &sym->declared_at);
10742 specification_expr = saved_specification_expr;
10743 return false;
10748 if (sym->value == NULL && sym->attr.referenced)
10749 apply_default_init_local (sym); /* Try to apply a default initialization. */
10751 /* Determine if the symbol may not have an initializer. */
10752 no_init_flag = automatic_flag = 0;
10753 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10754 || sym->attr.intrinsic || sym->attr.result)
10755 no_init_flag = 1;
10756 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10757 && is_non_constant_shape_array (sym))
10759 no_init_flag = automatic_flag = 1;
10761 /* Also, they must not have the SAVE attribute.
10762 SAVE_IMPLICIT is checked below. */
10763 if (sym->as && sym->attr.codimension)
10765 int corank = sym->as->corank;
10766 sym->as->corank = 0;
10767 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10768 sym->as->corank = corank;
10770 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10772 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10773 specification_expr = saved_specification_expr;
10774 return false;
10778 /* Ensure that any initializer is simplified. */
10779 if (sym->value)
10780 gfc_simplify_expr (sym->value, 1);
10782 /* Reject illegal initializers. */
10783 if (!sym->mark && sym->value)
10785 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10786 && CLASS_DATA (sym)->attr.allocatable))
10787 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10788 sym->name, &sym->declared_at);
10789 else if (sym->attr.external)
10790 gfc_error ("External '%s' at %L cannot have an initializer",
10791 sym->name, &sym->declared_at);
10792 else if (sym->attr.dummy
10793 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10794 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10795 sym->name, &sym->declared_at);
10796 else if (sym->attr.intrinsic)
10797 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10798 sym->name, &sym->declared_at);
10799 else if (sym->attr.result)
10800 gfc_error ("Function result '%s' at %L cannot have an initializer",
10801 sym->name, &sym->declared_at);
10802 else if (automatic_flag)
10803 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10804 sym->name, &sym->declared_at);
10805 else
10806 goto no_init_error;
10807 specification_expr = saved_specification_expr;
10808 return false;
10811 no_init_error:
10812 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10814 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10815 specification_expr = saved_specification_expr;
10816 return res;
10819 specification_expr = saved_specification_expr;
10820 return true;
10824 /* Resolve a procedure. */
10826 static bool
10827 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10829 gfc_formal_arglist *arg;
10831 if (sym->attr.function
10832 && !resolve_fl_var_and_proc (sym, mp_flag))
10833 return false;
10835 if (sym->ts.type == BT_CHARACTER)
10837 gfc_charlen *cl = sym->ts.u.cl;
10839 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10840 && !resolve_charlen (cl))
10841 return false;
10843 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10844 && sym->attr.proc == PROC_ST_FUNCTION)
10846 gfc_error ("Character-valued statement function '%s' at %L must "
10847 "have constant length", sym->name, &sym->declared_at);
10848 return false;
10852 /* Ensure that derived type for are not of a private type. Internal
10853 module procedures are excluded by 2.2.3.3 - i.e., they are not
10854 externally accessible and can access all the objects accessible in
10855 the host. */
10856 if (!(sym->ns->parent
10857 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10858 && gfc_check_symbol_access (sym))
10860 gfc_interface *iface;
10862 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10864 if (arg->sym
10865 && arg->sym->ts.type == BT_DERIVED
10866 && !arg->sym->ts.u.derived->attr.use_assoc
10867 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10868 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10869 "and cannot be a dummy argument"
10870 " of '%s', which is PUBLIC at %L",
10871 arg->sym->name, sym->name,
10872 &sym->declared_at))
10874 /* Stop this message from recurring. */
10875 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10876 return false;
10880 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10881 PRIVATE to the containing module. */
10882 for (iface = sym->generic; iface; iface = iface->next)
10884 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10886 if (arg->sym
10887 && arg->sym->ts.type == BT_DERIVED
10888 && !arg->sym->ts.u.derived->attr.use_assoc
10889 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10890 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10891 "PUBLIC interface '%s' at %L "
10892 "takes dummy arguments of '%s' which "
10893 "is PRIVATE", iface->sym->name,
10894 sym->name, &iface->sym->declared_at,
10895 gfc_typename(&arg->sym->ts)))
10897 /* Stop this message from recurring. */
10898 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10899 return false;
10904 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10905 PRIVATE to the containing module. */
10906 for (iface = sym->generic; iface; iface = iface->next)
10908 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10910 if (arg->sym
10911 && arg->sym->ts.type == BT_DERIVED
10912 && !arg->sym->ts.u.derived->attr.use_assoc
10913 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10914 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10915 "PUBLIC interface '%s' at %L takes "
10916 "dummy arguments of '%s' which is "
10917 "PRIVATE", iface->sym->name,
10918 sym->name, &iface->sym->declared_at,
10919 gfc_typename(&arg->sym->ts)))
10921 /* Stop this message from recurring. */
10922 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10923 return false;
10929 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10930 && !sym->attr.proc_pointer)
10932 gfc_error ("Function '%s' at %L cannot have an initializer",
10933 sym->name, &sym->declared_at);
10934 return false;
10937 /* An external symbol may not have an initializer because it is taken to be
10938 a procedure. Exception: Procedure Pointers. */
10939 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10941 gfc_error ("External object '%s' at %L may not have an initializer",
10942 sym->name, &sym->declared_at);
10943 return false;
10946 /* An elemental function is required to return a scalar 12.7.1 */
10947 if (sym->attr.elemental && sym->attr.function && sym->as)
10949 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10950 "result", sym->name, &sym->declared_at);
10951 /* Reset so that the error only occurs once. */
10952 sym->attr.elemental = 0;
10953 return false;
10956 if (sym->attr.proc == PROC_ST_FUNCTION
10957 && (sym->attr.allocatable || sym->attr.pointer))
10959 gfc_error ("Statement function '%s' at %L may not have pointer or "
10960 "allocatable attribute", sym->name, &sym->declared_at);
10961 return false;
10964 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10965 char-len-param shall not be array-valued, pointer-valued, recursive
10966 or pure. ....snip... A character value of * may only be used in the
10967 following ways: (i) Dummy arg of procedure - dummy associates with
10968 actual length; (ii) To declare a named constant; or (iii) External
10969 function - but length must be declared in calling scoping unit. */
10970 if (sym->attr.function
10971 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
10972 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10974 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10975 || (sym->attr.recursive) || (sym->attr.pure))
10977 if (sym->as && sym->as->rank)
10978 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10979 "array-valued", sym->name, &sym->declared_at);
10981 if (sym->attr.pointer)
10982 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10983 "pointer-valued", sym->name, &sym->declared_at);
10985 if (sym->attr.pure)
10986 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10987 "pure", sym->name, &sym->declared_at);
10989 if (sym->attr.recursive)
10990 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10991 "recursive", sym->name, &sym->declared_at);
10993 return false;
10996 /* Appendix B.2 of the standard. Contained functions give an
10997 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10998 character length is an F2003 feature. */
10999 if (!sym->attr.contained
11000 && gfc_current_form != FORM_FIXED
11001 && !sym->ts.deferred)
11002 gfc_notify_std (GFC_STD_F95_OBS,
11003 "CHARACTER(*) function '%s' at %L",
11004 sym->name, &sym->declared_at);
11007 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11009 gfc_formal_arglist *curr_arg;
11010 int has_non_interop_arg = 0;
11012 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11013 sym->common_block))
11015 /* Clear these to prevent looking at them again if there was an
11016 error. */
11017 sym->attr.is_bind_c = 0;
11018 sym->attr.is_c_interop = 0;
11019 sym->ts.is_c_interop = 0;
11021 else
11023 /* So far, no errors have been found. */
11024 sym->attr.is_c_interop = 1;
11025 sym->ts.is_c_interop = 1;
11028 curr_arg = gfc_sym_get_dummy_args (sym);
11029 while (curr_arg != NULL)
11031 /* Skip implicitly typed dummy args here. */
11032 if (curr_arg->sym->attr.implicit_type == 0)
11033 if (!gfc_verify_c_interop_param (curr_arg->sym))
11034 /* If something is found to fail, record the fact so we
11035 can mark the symbol for the procedure as not being
11036 BIND(C) to try and prevent multiple errors being
11037 reported. */
11038 has_non_interop_arg = 1;
11040 curr_arg = curr_arg->next;
11043 /* See if any of the arguments were not interoperable and if so, clear
11044 the procedure symbol to prevent duplicate error messages. */
11045 if (has_non_interop_arg != 0)
11047 sym->attr.is_c_interop = 0;
11048 sym->ts.is_c_interop = 0;
11049 sym->attr.is_bind_c = 0;
11053 if (!sym->attr.proc_pointer)
11055 if (sym->attr.save == SAVE_EXPLICIT)
11057 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11058 "in '%s' at %L", sym->name, &sym->declared_at);
11059 return false;
11061 if (sym->attr.intent)
11063 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11064 "in '%s' at %L", sym->name, &sym->declared_at);
11065 return false;
11067 if (sym->attr.subroutine && sym->attr.result)
11069 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11070 "in '%s' at %L", sym->name, &sym->declared_at);
11071 return false;
11073 if (sym->attr.external && sym->attr.function
11074 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11075 || sym->attr.contained))
11077 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11078 "in '%s' at %L", sym->name, &sym->declared_at);
11079 return false;
11081 if (strcmp ("ppr@", sym->name) == 0)
11083 gfc_error ("Procedure pointer result '%s' at %L "
11084 "is missing the pointer attribute",
11085 sym->ns->proc_name->name, &sym->declared_at);
11086 return false;
11090 return true;
11094 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11095 been defined and we now know their defined arguments, check that they fulfill
11096 the requirements of the standard for procedures used as finalizers. */
11098 static bool
11099 gfc_resolve_finalizers (gfc_symbol* derived)
11101 gfc_finalizer* list;
11102 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11103 bool result = true;
11104 bool seen_scalar = false;
11106 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11107 return true;
11109 /* Walk over the list of finalizer-procedures, check them, and if any one
11110 does not fit in with the standard's definition, print an error and remove
11111 it from the list. */
11112 prev_link = &derived->f2k_derived->finalizers;
11113 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11115 gfc_formal_arglist *dummy_args;
11116 gfc_symbol* arg;
11117 gfc_finalizer* i;
11118 int my_rank;
11120 /* Skip this finalizer if we already resolved it. */
11121 if (list->proc_tree)
11123 prev_link = &(list->next);
11124 continue;
11127 /* Check this exists and is a SUBROUTINE. */
11128 if (!list->proc_sym->attr.subroutine)
11130 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11131 list->proc_sym->name, &list->where);
11132 goto error;
11135 /* We should have exactly one argument. */
11136 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11137 if (!dummy_args || dummy_args->next)
11139 gfc_error ("FINAL procedure at %L must have exactly one argument",
11140 &list->where);
11141 goto error;
11143 arg = dummy_args->sym;
11145 /* This argument must be of our type. */
11146 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11148 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11149 &arg->declared_at, derived->name);
11150 goto error;
11153 /* It must neither be a pointer nor allocatable nor optional. */
11154 if (arg->attr.pointer)
11156 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11157 &arg->declared_at);
11158 goto error;
11160 if (arg->attr.allocatable)
11162 gfc_error ("Argument of FINAL procedure at %L must not be"
11163 " ALLOCATABLE", &arg->declared_at);
11164 goto error;
11166 if (arg->attr.optional)
11168 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11169 &arg->declared_at);
11170 goto error;
11173 /* It must not be INTENT(OUT). */
11174 if (arg->attr.intent == INTENT_OUT)
11176 gfc_error ("Argument of FINAL procedure at %L must not be"
11177 " INTENT(OUT)", &arg->declared_at);
11178 goto error;
11181 /* Warn if the procedure is non-scalar and not assumed shape. */
11182 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11183 && arg->as->type != AS_ASSUMED_SHAPE)
11184 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11185 " shape argument", &arg->declared_at);
11187 /* Check that it does not match in kind and rank with a FINAL procedure
11188 defined earlier. To really loop over the *earlier* declarations,
11189 we need to walk the tail of the list as new ones were pushed at the
11190 front. */
11191 /* TODO: Handle kind parameters once they are implemented. */
11192 my_rank = (arg->as ? arg->as->rank : 0);
11193 for (i = list->next; i; i = i->next)
11195 gfc_formal_arglist *dummy_args;
11197 /* Argument list might be empty; that is an error signalled earlier,
11198 but we nevertheless continued resolving. */
11199 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11200 if (dummy_args)
11202 gfc_symbol* i_arg = dummy_args->sym;
11203 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11204 if (i_rank == my_rank)
11206 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11207 " rank (%d) as '%s'",
11208 list->proc_sym->name, &list->where, my_rank,
11209 i->proc_sym->name);
11210 goto error;
11215 /* Is this the/a scalar finalizer procedure? */
11216 if (!arg->as || arg->as->rank == 0)
11217 seen_scalar = true;
11219 /* Find the symtree for this procedure. */
11220 gcc_assert (!list->proc_tree);
11221 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11223 prev_link = &list->next;
11224 continue;
11226 /* Remove wrong nodes immediately from the list so we don't risk any
11227 troubles in the future when they might fail later expectations. */
11228 error:
11229 result = false;
11230 i = list;
11231 *prev_link = list->next;
11232 gfc_free_finalizer (i);
11235 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11236 were nodes in the list, must have been for arrays. It is surely a good
11237 idea to have a scalar version there if there's something to finalize. */
11238 if (gfc_option.warn_surprising && result && !seen_scalar)
11239 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11240 " defined at %L, suggest also scalar one",
11241 derived->name, &derived->declared_at);
11243 /* TODO: Remove this error when finalization is finished. */
11244 gfc_error ("Finalization at %L is not yet implemented",
11245 &derived->declared_at);
11247 gfc_find_derived_vtab (derived);
11248 return result;
11252 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11254 static bool
11255 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11256 const char* generic_name, locus where)
11258 gfc_symbol *sym1, *sym2;
11259 const char *pass1, *pass2;
11261 gcc_assert (t1->specific && t2->specific);
11262 gcc_assert (!t1->specific->is_generic);
11263 gcc_assert (!t2->specific->is_generic);
11264 gcc_assert (t1->is_operator == t2->is_operator);
11266 sym1 = t1->specific->u.specific->n.sym;
11267 sym2 = t2->specific->u.specific->n.sym;
11269 if (sym1 == sym2)
11270 return true;
11272 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11273 if (sym1->attr.subroutine != sym2->attr.subroutine
11274 || sym1->attr.function != sym2->attr.function)
11276 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11277 " GENERIC '%s' at %L",
11278 sym1->name, sym2->name, generic_name, &where);
11279 return false;
11282 /* Compare the interfaces. */
11283 if (t1->specific->nopass)
11284 pass1 = NULL;
11285 else if (t1->specific->pass_arg)
11286 pass1 = t1->specific->pass_arg;
11287 else
11288 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
11289 if (t2->specific->nopass)
11290 pass2 = NULL;
11291 else if (t2->specific->pass_arg)
11292 pass2 = t2->specific->pass_arg;
11293 else
11294 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
11295 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11296 NULL, 0, pass1, pass2))
11298 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11299 sym1->name, sym2->name, generic_name, &where);
11300 return false;
11303 return true;
11307 /* Worker function for resolving a generic procedure binding; this is used to
11308 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11310 The difference between those cases is finding possible inherited bindings
11311 that are overridden, as one has to look for them in tb_sym_root,
11312 tb_uop_root or tb_op, respectively. Thus the caller must already find
11313 the super-type and set p->overridden correctly. */
11315 static bool
11316 resolve_tb_generic_targets (gfc_symbol* super_type,
11317 gfc_typebound_proc* p, const char* name)
11319 gfc_tbp_generic* target;
11320 gfc_symtree* first_target;
11321 gfc_symtree* inherited;
11323 gcc_assert (p && p->is_generic);
11325 /* Try to find the specific bindings for the symtrees in our target-list. */
11326 gcc_assert (p->u.generic);
11327 for (target = p->u.generic; target; target = target->next)
11328 if (!target->specific)
11330 gfc_typebound_proc* overridden_tbp;
11331 gfc_tbp_generic* g;
11332 const char* target_name;
11334 target_name = target->specific_st->name;
11336 /* Defined for this type directly. */
11337 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11339 target->specific = target->specific_st->n.tb;
11340 goto specific_found;
11343 /* Look for an inherited specific binding. */
11344 if (super_type)
11346 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11347 true, NULL);
11349 if (inherited)
11351 gcc_assert (inherited->n.tb);
11352 target->specific = inherited->n.tb;
11353 goto specific_found;
11357 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11358 " at %L", target_name, name, &p->where);
11359 return false;
11361 /* Once we've found the specific binding, check it is not ambiguous with
11362 other specifics already found or inherited for the same GENERIC. */
11363 specific_found:
11364 gcc_assert (target->specific);
11366 /* This must really be a specific binding! */
11367 if (target->specific->is_generic)
11369 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11370 " '%s' is GENERIC, too", name, &p->where, target_name);
11371 return false;
11374 /* Check those already resolved on this type directly. */
11375 for (g = p->u.generic; g; g = g->next)
11376 if (g != target && g->specific
11377 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11378 return false;
11380 /* Check for ambiguity with inherited specific targets. */
11381 for (overridden_tbp = p->overridden; overridden_tbp;
11382 overridden_tbp = overridden_tbp->overridden)
11383 if (overridden_tbp->is_generic)
11385 for (g = overridden_tbp->u.generic; g; g = g->next)
11387 gcc_assert (g->specific);
11388 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11389 return false;
11394 /* If we attempt to "overwrite" a specific binding, this is an error. */
11395 if (p->overridden && !p->overridden->is_generic)
11397 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11398 " the same name", name, &p->where);
11399 return false;
11402 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11403 all must have the same attributes here. */
11404 first_target = p->u.generic->specific->u.specific;
11405 gcc_assert (first_target);
11406 p->subroutine = first_target->n.sym->attr.subroutine;
11407 p->function = first_target->n.sym->attr.function;
11409 return true;
11413 /* Resolve a GENERIC procedure binding for a derived type. */
11415 static bool
11416 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11418 gfc_symbol* super_type;
11420 /* Find the overridden binding if any. */
11421 st->n.tb->overridden = NULL;
11422 super_type = gfc_get_derived_super_type (derived);
11423 if (super_type)
11425 gfc_symtree* overridden;
11426 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11427 true, NULL);
11429 if (overridden && overridden->n.tb)
11430 st->n.tb->overridden = overridden->n.tb;
11433 /* Resolve using worker function. */
11434 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11438 /* Retrieve the target-procedure of an operator binding and do some checks in
11439 common for intrinsic and user-defined type-bound operators. */
11441 static gfc_symbol*
11442 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11444 gfc_symbol* target_proc;
11446 gcc_assert (target->specific && !target->specific->is_generic);
11447 target_proc = target->specific->u.specific->n.sym;
11448 gcc_assert (target_proc);
11450 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11451 if (target->specific->nopass)
11453 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11454 return NULL;
11457 return target_proc;
11461 /* Resolve a type-bound intrinsic operator. */
11463 static bool
11464 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11465 gfc_typebound_proc* p)
11467 gfc_symbol* super_type;
11468 gfc_tbp_generic* target;
11470 /* If there's already an error here, do nothing (but don't fail again). */
11471 if (p->error)
11472 return true;
11474 /* Operators should always be GENERIC bindings. */
11475 gcc_assert (p->is_generic);
11477 /* Look for an overridden binding. */
11478 super_type = gfc_get_derived_super_type (derived);
11479 if (super_type && super_type->f2k_derived)
11480 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11481 op, true, NULL);
11482 else
11483 p->overridden = NULL;
11485 /* Resolve general GENERIC properties using worker function. */
11486 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11487 goto error;
11489 /* Check the targets to be procedures of correct interface. */
11490 for (target = p->u.generic; target; target = target->next)
11492 gfc_symbol* target_proc;
11494 target_proc = get_checked_tb_operator_target (target, p->where);
11495 if (!target_proc)
11496 goto error;
11498 if (!gfc_check_operator_interface (target_proc, op, p->where))
11499 goto error;
11501 /* Add target to non-typebound operator list. */
11502 if (!target->specific->deferred && !derived->attr.use_assoc
11503 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11505 gfc_interface *head, *intr;
11506 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11507 return false;
11508 head = derived->ns->op[op];
11509 intr = gfc_get_interface ();
11510 intr->sym = target_proc;
11511 intr->where = p->where;
11512 intr->next = head;
11513 derived->ns->op[op] = intr;
11517 return true;
11519 error:
11520 p->error = 1;
11521 return false;
11525 /* Resolve a type-bound user operator (tree-walker callback). */
11527 static gfc_symbol* resolve_bindings_derived;
11528 static bool resolve_bindings_result;
11530 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11532 static void
11533 resolve_typebound_user_op (gfc_symtree* stree)
11535 gfc_symbol* super_type;
11536 gfc_tbp_generic* target;
11538 gcc_assert (stree && stree->n.tb);
11540 if (stree->n.tb->error)
11541 return;
11543 /* Operators should always be GENERIC bindings. */
11544 gcc_assert (stree->n.tb->is_generic);
11546 /* Find overridden procedure, if any. */
11547 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11548 if (super_type && super_type->f2k_derived)
11550 gfc_symtree* overridden;
11551 overridden = gfc_find_typebound_user_op (super_type, NULL,
11552 stree->name, true, NULL);
11554 if (overridden && overridden->n.tb)
11555 stree->n.tb->overridden = overridden->n.tb;
11557 else
11558 stree->n.tb->overridden = NULL;
11560 /* Resolve basically using worker function. */
11561 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11562 goto error;
11564 /* Check the targets to be functions of correct interface. */
11565 for (target = stree->n.tb->u.generic; target; target = target->next)
11567 gfc_symbol* target_proc;
11569 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11570 if (!target_proc)
11571 goto error;
11573 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11574 goto error;
11577 return;
11579 error:
11580 resolve_bindings_result = false;
11581 stree->n.tb->error = 1;
11585 /* Resolve the type-bound procedures for a derived type. */
11587 static void
11588 resolve_typebound_procedure (gfc_symtree* stree)
11590 gfc_symbol* proc;
11591 locus where;
11592 gfc_symbol* me_arg;
11593 gfc_symbol* super_type;
11594 gfc_component* comp;
11596 gcc_assert (stree);
11598 /* Undefined specific symbol from GENERIC target definition. */
11599 if (!stree->n.tb)
11600 return;
11602 if (stree->n.tb->error)
11603 return;
11605 /* If this is a GENERIC binding, use that routine. */
11606 if (stree->n.tb->is_generic)
11608 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11609 goto error;
11610 return;
11613 /* Get the target-procedure to check it. */
11614 gcc_assert (!stree->n.tb->is_generic);
11615 gcc_assert (stree->n.tb->u.specific);
11616 proc = stree->n.tb->u.specific->n.sym;
11617 where = stree->n.tb->where;
11619 /* Default access should already be resolved from the parser. */
11620 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11622 if (stree->n.tb->deferred)
11624 if (!check_proc_interface (proc, &where))
11625 goto error;
11627 else
11629 /* Check for F08:C465. */
11630 if ((!proc->attr.subroutine && !proc->attr.function)
11631 || (proc->attr.proc != PROC_MODULE
11632 && proc->attr.if_source != IFSRC_IFBODY)
11633 || proc->attr.abstract)
11635 gfc_error ("'%s' must be a module procedure or an external procedure with"
11636 " an explicit interface at %L", proc->name, &where);
11637 goto error;
11641 stree->n.tb->subroutine = proc->attr.subroutine;
11642 stree->n.tb->function = proc->attr.function;
11644 /* Find the super-type of the current derived type. We could do this once and
11645 store in a global if speed is needed, but as long as not I believe this is
11646 more readable and clearer. */
11647 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11649 /* If PASS, resolve and check arguments if not already resolved / loaded
11650 from a .mod file. */
11651 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11653 gfc_formal_arglist *dummy_args;
11655 dummy_args = gfc_sym_get_dummy_args (proc);
11656 if (stree->n.tb->pass_arg)
11658 gfc_formal_arglist *i;
11660 /* If an explicit passing argument name is given, walk the arg-list
11661 and look for it. */
11663 me_arg = NULL;
11664 stree->n.tb->pass_arg_num = 1;
11665 for (i = dummy_args; i; i = i->next)
11667 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11669 me_arg = i->sym;
11670 break;
11672 ++stree->n.tb->pass_arg_num;
11675 if (!me_arg)
11677 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11678 " argument '%s'",
11679 proc->name, stree->n.tb->pass_arg, &where,
11680 stree->n.tb->pass_arg);
11681 goto error;
11684 else
11686 /* Otherwise, take the first one; there should in fact be at least
11687 one. */
11688 stree->n.tb->pass_arg_num = 1;
11689 if (!dummy_args)
11691 gfc_error ("Procedure '%s' with PASS at %L must have at"
11692 " least one argument", proc->name, &where);
11693 goto error;
11695 me_arg = dummy_args->sym;
11698 /* Now check that the argument-type matches and the passed-object
11699 dummy argument is generally fine. */
11701 gcc_assert (me_arg);
11703 if (me_arg->ts.type != BT_CLASS)
11705 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11706 " at %L", proc->name, &where);
11707 goto error;
11710 if (CLASS_DATA (me_arg)->ts.u.derived
11711 != resolve_bindings_derived)
11713 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11714 " the derived-type '%s'", me_arg->name, proc->name,
11715 me_arg->name, &where, resolve_bindings_derived->name);
11716 goto error;
11719 gcc_assert (me_arg->ts.type == BT_CLASS);
11720 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11722 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11723 " scalar", proc->name, &where);
11724 goto error;
11726 if (CLASS_DATA (me_arg)->attr.allocatable)
11728 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11729 " be ALLOCATABLE", proc->name, &where);
11730 goto error;
11732 if (CLASS_DATA (me_arg)->attr.class_pointer)
11734 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11735 " be POINTER", proc->name, &where);
11736 goto error;
11740 /* If we are extending some type, check that we don't override a procedure
11741 flagged NON_OVERRIDABLE. */
11742 stree->n.tb->overridden = NULL;
11743 if (super_type)
11745 gfc_symtree* overridden;
11746 overridden = gfc_find_typebound_proc (super_type, NULL,
11747 stree->name, true, NULL);
11749 if (overridden)
11751 if (overridden->n.tb)
11752 stree->n.tb->overridden = overridden->n.tb;
11754 if (!gfc_check_typebound_override (stree, overridden))
11755 goto error;
11759 /* See if there's a name collision with a component directly in this type. */
11760 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11761 if (!strcmp (comp->name, stree->name))
11763 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11764 " '%s'",
11765 stree->name, &where, resolve_bindings_derived->name);
11766 goto error;
11769 /* Try to find a name collision with an inherited component. */
11770 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11772 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11773 " component of '%s'",
11774 stree->name, &where, resolve_bindings_derived->name);
11775 goto error;
11778 stree->n.tb->error = 0;
11779 return;
11781 error:
11782 resolve_bindings_result = false;
11783 stree->n.tb->error = 1;
11787 static bool
11788 resolve_typebound_procedures (gfc_symbol* derived)
11790 int op;
11791 gfc_symbol* super_type;
11793 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11794 return true;
11796 super_type = gfc_get_derived_super_type (derived);
11797 if (super_type)
11798 resolve_symbol (super_type);
11800 resolve_bindings_derived = derived;
11801 resolve_bindings_result = true;
11803 /* Make sure the vtab has been generated. */
11804 gfc_find_derived_vtab (derived);
11806 if (derived->f2k_derived->tb_sym_root)
11807 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11808 &resolve_typebound_procedure);
11810 if (derived->f2k_derived->tb_uop_root)
11811 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11812 &resolve_typebound_user_op);
11814 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11816 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11817 if (p && !resolve_typebound_intrinsic_op (derived,
11818 (gfc_intrinsic_op)op, p))
11819 resolve_bindings_result = false;
11822 return resolve_bindings_result;
11826 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11827 to give all identical derived types the same backend_decl. */
11828 static void
11829 add_dt_to_dt_list (gfc_symbol *derived)
11831 gfc_dt_list *dt_list;
11833 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11834 if (derived == dt_list->derived)
11835 return;
11837 dt_list = gfc_get_dt_list ();
11838 dt_list->next = gfc_derived_types;
11839 dt_list->derived = derived;
11840 gfc_derived_types = dt_list;
11844 /* Ensure that a derived-type is really not abstract, meaning that every
11845 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11847 static bool
11848 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11850 if (!st)
11851 return true;
11853 if (!ensure_not_abstract_walker (sub, st->left))
11854 return false;
11855 if (!ensure_not_abstract_walker (sub, st->right))
11856 return false;
11858 if (st->n.tb && st->n.tb->deferred)
11860 gfc_symtree* overriding;
11861 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11862 if (!overriding)
11863 return false;
11864 gcc_assert (overriding->n.tb);
11865 if (overriding->n.tb->deferred)
11867 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11868 " '%s' is DEFERRED and not overridden",
11869 sub->name, &sub->declared_at, st->name);
11870 return false;
11874 return true;
11877 static bool
11878 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11880 /* The algorithm used here is to recursively travel up the ancestry of sub
11881 and for each ancestor-type, check all bindings. If any of them is
11882 DEFERRED, look it up starting from sub and see if the found (overriding)
11883 binding is not DEFERRED.
11884 This is not the most efficient way to do this, but it should be ok and is
11885 clearer than something sophisticated. */
11887 gcc_assert (ancestor && !sub->attr.abstract);
11889 if (!ancestor->attr.abstract)
11890 return true;
11892 /* Walk bindings of this ancestor. */
11893 if (ancestor->f2k_derived)
11895 bool t;
11896 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11897 if (!t)
11898 return false;
11901 /* Find next ancestor type and recurse on it. */
11902 ancestor = gfc_get_derived_super_type (ancestor);
11903 if (ancestor)
11904 return ensure_not_abstract (sub, ancestor);
11906 return true;
11910 /* This check for typebound defined assignments is done recursively
11911 since the order in which derived types are resolved is not always in
11912 order of the declarations. */
11914 static void
11915 check_defined_assignments (gfc_symbol *derived)
11917 gfc_component *c;
11919 for (c = derived->components; c; c = c->next)
11921 if (c->ts.type != BT_DERIVED
11922 || c->attr.pointer
11923 || c->attr.allocatable
11924 || c->attr.proc_pointer_comp
11925 || c->attr.class_pointer
11926 || c->attr.proc_pointer)
11927 continue;
11929 if (c->ts.u.derived->attr.defined_assign_comp
11930 || (c->ts.u.derived->f2k_derived
11931 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
11933 derived->attr.defined_assign_comp = 1;
11934 return;
11937 check_defined_assignments (c->ts.u.derived);
11938 if (c->ts.u.derived->attr.defined_assign_comp)
11940 derived->attr.defined_assign_comp = 1;
11941 return;
11947 /* Resolve the components of a derived type. This does not have to wait until
11948 resolution stage, but can be done as soon as the dt declaration has been
11949 parsed. */
11951 static bool
11952 resolve_fl_derived0 (gfc_symbol *sym)
11954 gfc_symbol* super_type;
11955 gfc_component *c;
11957 if (sym->attr.unlimited_polymorphic)
11958 return true;
11960 super_type = gfc_get_derived_super_type (sym);
11962 /* F2008, C432. */
11963 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11965 gfc_error ("As extending type '%s' at %L has a coarray component, "
11966 "parent type '%s' shall also have one", sym->name,
11967 &sym->declared_at, super_type->name);
11968 return false;
11971 /* Ensure the extended type gets resolved before we do. */
11972 if (super_type && !resolve_fl_derived0 (super_type))
11973 return false;
11975 /* An ABSTRACT type must be extensible. */
11976 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11978 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11979 sym->name, &sym->declared_at);
11980 return false;
11983 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
11984 : sym->components;
11986 for ( ; c != NULL; c = c->next)
11988 if (c->attr.artificial)
11989 continue;
11991 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
11992 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
11994 gfc_error ("Deferred-length character component '%s' at %L is not "
11995 "yet supported", c->name, &c->loc);
11996 return false;
11999 /* F2008, C442. */
12000 if ((!sym->attr.is_class || c != sym->components)
12001 && c->attr.codimension
12002 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12004 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12005 "deferred shape", c->name, &c->loc);
12006 return false;
12009 /* F2008, C443. */
12010 if (c->attr.codimension && c->ts.type == BT_DERIVED
12011 && c->ts.u.derived->ts.is_iso_c)
12013 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12014 "shall not be a coarray", c->name, &c->loc);
12015 return false;
12018 /* F2008, C444. */
12019 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12020 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12021 || c->attr.allocatable))
12023 gfc_error ("Component '%s' at %L with coarray component "
12024 "shall be a nonpointer, nonallocatable scalar",
12025 c->name, &c->loc);
12026 return false;
12029 /* F2008, C448. */
12030 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12032 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12033 "is not an array pointer", c->name, &c->loc);
12034 return false;
12037 if (c->attr.proc_pointer && c->ts.interface)
12039 gfc_symbol *ifc = c->ts.interface;
12041 if (!sym->attr.vtype
12042 && !check_proc_interface (ifc, &c->loc))
12043 return false;
12045 if (ifc->attr.if_source || ifc->attr.intrinsic)
12047 /* Resolve interface and copy attributes. */
12048 if (ifc->formal && !ifc->formal_ns)
12049 resolve_symbol (ifc);
12050 if (ifc->attr.intrinsic)
12051 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12053 if (ifc->result)
12055 c->ts = ifc->result->ts;
12056 c->attr.allocatable = ifc->result->attr.allocatable;
12057 c->attr.pointer = ifc->result->attr.pointer;
12058 c->attr.dimension = ifc->result->attr.dimension;
12059 c->as = gfc_copy_array_spec (ifc->result->as);
12060 c->attr.class_ok = ifc->result->attr.class_ok;
12062 else
12064 c->ts = ifc->ts;
12065 c->attr.allocatable = ifc->attr.allocatable;
12066 c->attr.pointer = ifc->attr.pointer;
12067 c->attr.dimension = ifc->attr.dimension;
12068 c->as = gfc_copy_array_spec (ifc->as);
12069 c->attr.class_ok = ifc->attr.class_ok;
12071 c->ts.interface = ifc;
12072 c->attr.function = ifc->attr.function;
12073 c->attr.subroutine = ifc->attr.subroutine;
12075 c->attr.pure = ifc->attr.pure;
12076 c->attr.elemental = ifc->attr.elemental;
12077 c->attr.recursive = ifc->attr.recursive;
12078 c->attr.always_explicit = ifc->attr.always_explicit;
12079 c->attr.ext_attr |= ifc->attr.ext_attr;
12080 /* Copy char length. */
12081 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12083 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12084 if (cl->length && !cl->resolved
12085 && !gfc_resolve_expr (cl->length))
12086 return false;
12087 c->ts.u.cl = cl;
12091 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12093 /* Since PPCs are not implicitly typed, a PPC without an explicit
12094 interface must be a subroutine. */
12095 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12098 /* Procedure pointer components: Check PASS arg. */
12099 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12100 && !sym->attr.vtype)
12102 gfc_symbol* me_arg;
12104 if (c->tb->pass_arg)
12106 gfc_formal_arglist* i;
12108 /* If an explicit passing argument name is given, walk the arg-list
12109 and look for it. */
12111 me_arg = NULL;
12112 c->tb->pass_arg_num = 1;
12113 for (i = c->ts.interface->formal; i; i = i->next)
12115 if (!strcmp (i->sym->name, c->tb->pass_arg))
12117 me_arg = i->sym;
12118 break;
12120 c->tb->pass_arg_num++;
12123 if (!me_arg)
12125 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12126 "at %L has no argument '%s'", c->name,
12127 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12128 c->tb->error = 1;
12129 return false;
12132 else
12134 /* Otherwise, take the first one; there should in fact be at least
12135 one. */
12136 c->tb->pass_arg_num = 1;
12137 if (!c->ts.interface->formal)
12139 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12140 "must have at least one argument",
12141 c->name, &c->loc);
12142 c->tb->error = 1;
12143 return false;
12145 me_arg = c->ts.interface->formal->sym;
12148 /* Now check that the argument-type matches. */
12149 gcc_assert (me_arg);
12150 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12151 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12152 || (me_arg->ts.type == BT_CLASS
12153 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12155 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12156 " the derived type '%s'", me_arg->name, c->name,
12157 me_arg->name, &c->loc, sym->name);
12158 c->tb->error = 1;
12159 return false;
12162 /* Check for C453. */
12163 if (me_arg->attr.dimension)
12165 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12166 "must be scalar", me_arg->name, c->name, me_arg->name,
12167 &c->loc);
12168 c->tb->error = 1;
12169 return false;
12172 if (me_arg->attr.pointer)
12174 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12175 "may not have the POINTER attribute", me_arg->name,
12176 c->name, me_arg->name, &c->loc);
12177 c->tb->error = 1;
12178 return false;
12181 if (me_arg->attr.allocatable)
12183 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12184 "may not be ALLOCATABLE", me_arg->name, c->name,
12185 me_arg->name, &c->loc);
12186 c->tb->error = 1;
12187 return false;
12190 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12191 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12192 " at %L", c->name, &c->loc);
12196 /* Check type-spec if this is not the parent-type component. */
12197 if (((sym->attr.is_class
12198 && (!sym->components->ts.u.derived->attr.extension
12199 || c != sym->components->ts.u.derived->components))
12200 || (!sym->attr.is_class
12201 && (!sym->attr.extension || c != sym->components)))
12202 && !sym->attr.vtype
12203 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12204 return false;
12206 /* If this type is an extension, set the accessibility of the parent
12207 component. */
12208 if (super_type
12209 && ((sym->attr.is_class
12210 && c == sym->components->ts.u.derived->components)
12211 || (!sym->attr.is_class && c == sym->components))
12212 && strcmp (super_type->name, c->name) == 0)
12213 c->attr.access = super_type->attr.access;
12215 /* If this type is an extension, see if this component has the same name
12216 as an inherited type-bound procedure. */
12217 if (super_type && !sym->attr.is_class
12218 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12220 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12221 " inherited type-bound procedure",
12222 c->name, sym->name, &c->loc);
12223 return false;
12226 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12227 && !c->ts.deferred)
12229 if (c->ts.u.cl->length == NULL
12230 || (!resolve_charlen(c->ts.u.cl))
12231 || !gfc_is_constant_expr (c->ts.u.cl->length))
12233 gfc_error ("Character length of component '%s' needs to "
12234 "be a constant specification expression at %L",
12235 c->name,
12236 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12237 return false;
12241 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12242 && !c->attr.pointer && !c->attr.allocatable)
12244 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12245 "length must be a POINTER or ALLOCATABLE",
12246 c->name, sym->name, &c->loc);
12247 return false;
12250 if (c->ts.type == BT_DERIVED
12251 && sym->component_access != ACCESS_PRIVATE
12252 && gfc_check_symbol_access (sym)
12253 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12254 && !c->ts.u.derived->attr.use_assoc
12255 && !gfc_check_symbol_access (c->ts.u.derived)
12256 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12257 "PRIVATE type and cannot be a component of "
12258 "'%s', which is PUBLIC at %L", c->name,
12259 sym->name, &sym->declared_at))
12260 return false;
12262 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12264 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12265 "type %s", c->name, &c->loc, sym->name);
12266 return false;
12269 if (sym->attr.sequence)
12271 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12273 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12274 "not have the SEQUENCE attribute",
12275 c->ts.u.derived->name, &sym->declared_at);
12276 return false;
12280 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12281 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12282 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12283 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12284 CLASS_DATA (c)->ts.u.derived
12285 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12287 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12288 && c->attr.pointer && c->ts.u.derived->components == NULL
12289 && !c->ts.u.derived->attr.zero_comp)
12291 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12292 "that has not been declared", c->name, sym->name,
12293 &c->loc);
12294 return false;
12297 if (c->ts.type == BT_CLASS && c->attr.class_ok
12298 && CLASS_DATA (c)->attr.class_pointer
12299 && CLASS_DATA (c)->ts.u.derived->components == NULL
12300 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12301 && !UNLIMITED_POLY (c))
12303 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12304 "that has not been declared", c->name, sym->name,
12305 &c->loc);
12306 return false;
12309 /* C437. */
12310 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12311 && (!c->attr.class_ok
12312 || !(CLASS_DATA (c)->attr.class_pointer
12313 || CLASS_DATA (c)->attr.allocatable)))
12315 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12316 "or pointer", c->name, &c->loc);
12317 /* Prevent a recurrence of the error. */
12318 c->ts.type = BT_UNKNOWN;
12319 return false;
12322 /* Ensure that all the derived type components are put on the
12323 derived type list; even in formal namespaces, where derived type
12324 pointer components might not have been declared. */
12325 if (c->ts.type == BT_DERIVED
12326 && c->ts.u.derived
12327 && c->ts.u.derived->components
12328 && c->attr.pointer
12329 && sym != c->ts.u.derived)
12330 add_dt_to_dt_list (c->ts.u.derived);
12332 if (!gfc_resolve_array_spec (c->as,
12333 !(c->attr.pointer || c->attr.proc_pointer
12334 || c->attr.allocatable)))
12335 return false;
12337 if (c->initializer && !sym->attr.vtype
12338 && !gfc_check_assign_symbol (sym, c, c->initializer))
12339 return false;
12342 check_defined_assignments (sym);
12344 if (!sym->attr.defined_assign_comp && super_type)
12345 sym->attr.defined_assign_comp
12346 = super_type->attr.defined_assign_comp;
12348 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12349 all DEFERRED bindings are overridden. */
12350 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12351 && !sym->attr.is_class
12352 && !ensure_not_abstract (sym, super_type))
12353 return false;
12355 /* Add derived type to the derived type list. */
12356 add_dt_to_dt_list (sym);
12358 /* Check if the type is finalizable. This is done in order to ensure that the
12359 finalization wrapper is generated early enough. */
12360 gfc_is_finalizable (sym, NULL);
12362 return true;
12366 /* The following procedure does the full resolution of a derived type,
12367 including resolution of all type-bound procedures (if present). In contrast
12368 to 'resolve_fl_derived0' this can only be done after the module has been
12369 parsed completely. */
12371 static bool
12372 resolve_fl_derived (gfc_symbol *sym)
12374 gfc_symbol *gen_dt = NULL;
12376 if (sym->attr.unlimited_polymorphic)
12377 return true;
12379 if (!sym->attr.is_class)
12380 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12381 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12382 && (!gen_dt->generic->sym->attr.use_assoc
12383 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12384 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12385 "'%s' at %L being the same name as derived "
12386 "type at %L", sym->name,
12387 gen_dt->generic->sym == sym
12388 ? gen_dt->generic->next->sym->name
12389 : gen_dt->generic->sym->name,
12390 gen_dt->generic->sym == sym
12391 ? &gen_dt->generic->next->sym->declared_at
12392 : &gen_dt->generic->sym->declared_at,
12393 &sym->declared_at))
12394 return false;
12396 /* Resolve the finalizer procedures. */
12397 if (!gfc_resolve_finalizers (sym))
12398 return false;
12400 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12402 /* Fix up incomplete CLASS symbols. */
12403 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12404 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12406 /* Nothing more to do for unlimited polymorphic entities. */
12407 if (data->ts.u.derived->attr.unlimited_polymorphic)
12408 return true;
12409 else if (vptr->ts.u.derived == NULL)
12411 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12412 gcc_assert (vtab);
12413 vptr->ts.u.derived = vtab->ts.u.derived;
12417 if (!resolve_fl_derived0 (sym))
12418 return false;
12420 /* Resolve the type-bound procedures. */
12421 if (!resolve_typebound_procedures (sym))
12422 return false;
12424 return true;
12428 static bool
12429 resolve_fl_namelist (gfc_symbol *sym)
12431 gfc_namelist *nl;
12432 gfc_symbol *nlsym;
12434 for (nl = sym->namelist; nl; nl = nl->next)
12436 /* Check again, the check in match only works if NAMELIST comes
12437 after the decl. */
12438 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12440 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12441 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12442 return false;
12445 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12446 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12447 "with assumed shape in namelist '%s' at %L",
12448 nl->sym->name, sym->name, &sym->declared_at))
12449 return false;
12451 if (is_non_constant_shape_array (nl->sym)
12452 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12453 "with nonconstant shape in namelist '%s' at %L",
12454 nl->sym->name, sym->name, &sym->declared_at))
12455 return false;
12457 if (nl->sym->ts.type == BT_CHARACTER
12458 && (nl->sym->ts.u.cl->length == NULL
12459 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12460 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12461 "nonconstant character length in "
12462 "namelist '%s' at %L", nl->sym->name,
12463 sym->name, &sym->declared_at))
12464 return false;
12466 /* FIXME: Once UDDTIO is implemented, the following can be
12467 removed. */
12468 if (nl->sym->ts.type == BT_CLASS)
12470 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12471 "polymorphic and requires a defined input/output "
12472 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12473 return false;
12476 if (nl->sym->ts.type == BT_DERIVED
12477 && (nl->sym->ts.u.derived->attr.alloc_comp
12478 || nl->sym->ts.u.derived->attr.pointer_comp))
12480 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12481 "namelist '%s' at %L with ALLOCATABLE "
12482 "or POINTER components", nl->sym->name,
12483 sym->name, &sym->declared_at))
12484 return false;
12486 /* FIXME: Once UDDTIO is implemented, the following can be
12487 removed. */
12488 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12489 "ALLOCATABLE or POINTER components and thus requires "
12490 "a defined input/output procedure", nl->sym->name,
12491 sym->name, &sym->declared_at);
12492 return false;
12496 /* Reject PRIVATE objects in a PUBLIC namelist. */
12497 if (gfc_check_symbol_access (sym))
12499 for (nl = sym->namelist; nl; nl = nl->next)
12501 if (!nl->sym->attr.use_assoc
12502 && !is_sym_host_assoc (nl->sym, sym->ns)
12503 && !gfc_check_symbol_access (nl->sym))
12505 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12506 "cannot be member of PUBLIC namelist '%s' at %L",
12507 nl->sym->name, sym->name, &sym->declared_at);
12508 return false;
12511 /* Types with private components that came here by USE-association. */
12512 if (nl->sym->ts.type == BT_DERIVED
12513 && derived_inaccessible (nl->sym->ts.u.derived))
12515 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12516 "components and cannot be member of namelist '%s' at %L",
12517 nl->sym->name, sym->name, &sym->declared_at);
12518 return false;
12521 /* Types with private components that are defined in the same module. */
12522 if (nl->sym->ts.type == BT_DERIVED
12523 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12524 && nl->sym->ts.u.derived->attr.private_comp)
12526 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12527 "cannot be a member of PUBLIC namelist '%s' at %L",
12528 nl->sym->name, sym->name, &sym->declared_at);
12529 return false;
12535 /* 14.1.2 A module or internal procedure represent local entities
12536 of the same type as a namelist member and so are not allowed. */
12537 for (nl = sym->namelist; nl; nl = nl->next)
12539 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12540 continue;
12542 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12543 if ((nl->sym == sym->ns->proc_name)
12545 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12546 continue;
12548 nlsym = NULL;
12549 if (nl->sym->name)
12550 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12551 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12553 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12554 "attribute in '%s' at %L", nlsym->name,
12555 &sym->declared_at);
12556 return false;
12560 return true;
12564 static bool
12565 resolve_fl_parameter (gfc_symbol *sym)
12567 /* A parameter array's shape needs to be constant. */
12568 if (sym->as != NULL
12569 && (sym->as->type == AS_DEFERRED
12570 || is_non_constant_shape_array (sym)))
12572 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12573 "or of deferred shape", sym->name, &sym->declared_at);
12574 return false;
12577 /* Make sure a parameter that has been implicitly typed still
12578 matches the implicit type, since PARAMETER statements can precede
12579 IMPLICIT statements. */
12580 if (sym->attr.implicit_type
12581 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12582 sym->ns)))
12584 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12585 "later IMPLICIT type", sym->name, &sym->declared_at);
12586 return false;
12589 /* Make sure the types of derived parameters are consistent. This
12590 type checking is deferred until resolution because the type may
12591 refer to a derived type from the host. */
12592 if (sym->ts.type == BT_DERIVED
12593 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12595 gfc_error ("Incompatible derived type in PARAMETER at %L",
12596 &sym->value->where);
12597 return false;
12599 return true;
12603 /* Do anything necessary to resolve a symbol. Right now, we just
12604 assume that an otherwise unknown symbol is a variable. This sort
12605 of thing commonly happens for symbols in module. */
12607 static void
12608 resolve_symbol (gfc_symbol *sym)
12610 int check_constant, mp_flag;
12611 gfc_symtree *symtree;
12612 gfc_symtree *this_symtree;
12613 gfc_namespace *ns;
12614 gfc_component *c;
12615 symbol_attribute class_attr;
12616 gfc_array_spec *as;
12617 bool saved_specification_expr;
12619 if (sym->resolved)
12620 return;
12621 sym->resolved = 1;
12623 if (sym->attr.artificial)
12624 return;
12626 if (sym->attr.unlimited_polymorphic)
12627 return;
12629 if (sym->attr.flavor == FL_UNKNOWN
12630 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12631 && !sym->attr.generic && !sym->attr.external
12632 && sym->attr.if_source == IFSRC_UNKNOWN))
12635 /* If we find that a flavorless symbol is an interface in one of the
12636 parent namespaces, find its symtree in this namespace, free the
12637 symbol and set the symtree to point to the interface symbol. */
12638 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12640 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12641 if (symtree && (symtree->n.sym->generic ||
12642 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12643 && sym->ns->construct_entities)))
12645 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12646 sym->name);
12647 gfc_release_symbol (sym);
12648 symtree->n.sym->refs++;
12649 this_symtree->n.sym = symtree->n.sym;
12650 return;
12654 /* Otherwise give it a flavor according to such attributes as
12655 it has. */
12656 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12657 && sym->attr.intrinsic == 0)
12658 sym->attr.flavor = FL_VARIABLE;
12659 else if (sym->attr.flavor == FL_UNKNOWN)
12661 sym->attr.flavor = FL_PROCEDURE;
12662 if (sym->attr.dimension)
12663 sym->attr.function = 1;
12667 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12668 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12670 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12671 && !resolve_procedure_interface (sym))
12672 return;
12674 if (sym->attr.is_protected && !sym->attr.proc_pointer
12675 && (sym->attr.procedure || sym->attr.external))
12677 if (sym->attr.external)
12678 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12679 "at %L", &sym->declared_at);
12680 else
12681 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12682 "at %L", &sym->declared_at);
12684 return;
12687 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12688 return;
12690 /* Symbols that are module procedures with results (functions) have
12691 the types and array specification copied for type checking in
12692 procedures that call them, as well as for saving to a module
12693 file. These symbols can't stand the scrutiny that their results
12694 can. */
12695 mp_flag = (sym->result != NULL && sym->result != sym);
12697 /* Make sure that the intrinsic is consistent with its internal
12698 representation. This needs to be done before assigning a default
12699 type to avoid spurious warnings. */
12700 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12701 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12702 return;
12704 /* Resolve associate names. */
12705 if (sym->assoc)
12706 resolve_assoc_var (sym, true);
12708 /* Assign default type to symbols that need one and don't have one. */
12709 if (sym->ts.type == BT_UNKNOWN)
12711 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12713 gfc_set_default_type (sym, 1, NULL);
12716 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12717 && !sym->attr.function && !sym->attr.subroutine
12718 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12719 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12721 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12723 /* The specific case of an external procedure should emit an error
12724 in the case that there is no implicit type. */
12725 if (!mp_flag)
12726 gfc_set_default_type (sym, sym->attr.external, NULL);
12727 else
12729 /* Result may be in another namespace. */
12730 resolve_symbol (sym->result);
12732 if (!sym->result->attr.proc_pointer)
12734 sym->ts = sym->result->ts;
12735 sym->as = gfc_copy_array_spec (sym->result->as);
12736 sym->attr.dimension = sym->result->attr.dimension;
12737 sym->attr.pointer = sym->result->attr.pointer;
12738 sym->attr.allocatable = sym->result->attr.allocatable;
12739 sym->attr.contiguous = sym->result->attr.contiguous;
12744 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12746 bool saved_specification_expr = specification_expr;
12747 specification_expr = true;
12748 gfc_resolve_array_spec (sym->result->as, false);
12749 specification_expr = saved_specification_expr;
12752 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12754 as = CLASS_DATA (sym)->as;
12755 class_attr = CLASS_DATA (sym)->attr;
12756 class_attr.pointer = class_attr.class_pointer;
12758 else
12760 class_attr = sym->attr;
12761 as = sym->as;
12764 /* F2008, C530. */
12765 if (sym->attr.contiguous
12766 && (!class_attr.dimension
12767 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12768 && !class_attr.pointer)))
12770 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12771 "array pointer or an assumed-shape or assumed-rank array",
12772 sym->name, &sym->declared_at);
12773 return;
12776 /* Assumed size arrays and assumed shape arrays must be dummy
12777 arguments. Array-spec's of implied-shape should have been resolved to
12778 AS_EXPLICIT already. */
12780 if (as)
12782 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12783 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12784 || as->type == AS_ASSUMED_SHAPE)
12785 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12787 if (as->type == AS_ASSUMED_SIZE)
12788 gfc_error ("Assumed size array at %L must be a dummy argument",
12789 &sym->declared_at);
12790 else
12791 gfc_error ("Assumed shape array at %L must be a dummy argument",
12792 &sym->declared_at);
12793 return;
12795 /* TS 29113, C535a. */
12796 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12797 && !sym->attr.select_type_temporary)
12799 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12800 &sym->declared_at);
12801 return;
12803 if (as->type == AS_ASSUMED_RANK
12804 && (sym->attr.codimension || sym->attr.value))
12806 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12807 "CODIMENSION attribute", &sym->declared_at);
12808 return;
12812 /* Make sure symbols with known intent or optional are really dummy
12813 variable. Because of ENTRY statement, this has to be deferred
12814 until resolution time. */
12816 if (!sym->attr.dummy
12817 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12819 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12820 return;
12823 if (sym->attr.value && !sym->attr.dummy)
12825 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12826 "it is not a dummy argument", sym->name, &sym->declared_at);
12827 return;
12830 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12832 gfc_charlen *cl = sym->ts.u.cl;
12833 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12835 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12836 "attribute must have constant length",
12837 sym->name, &sym->declared_at);
12838 return;
12841 if (sym->ts.is_c_interop
12842 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12844 gfc_error ("C interoperable character dummy variable '%s' at %L "
12845 "with VALUE attribute must have length one",
12846 sym->name, &sym->declared_at);
12847 return;
12851 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12852 && sym->ts.u.derived->attr.generic)
12854 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12855 if (!sym->ts.u.derived)
12857 gfc_error ("The derived type '%s' at %L is of type '%s', "
12858 "which has not been defined", sym->name,
12859 &sym->declared_at, sym->ts.u.derived->name);
12860 sym->ts.type = BT_UNKNOWN;
12861 return;
12865 /* Use the same constraints as TYPE(*), except for the type check
12866 and that only scalars and assumed-size arrays are permitted. */
12867 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12869 if (!sym->attr.dummy)
12871 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12872 "a dummy argument", sym->name, &sym->declared_at);
12873 return;
12876 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12877 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12878 && sym->ts.type != BT_COMPLEX)
12880 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12881 "of type TYPE(*) or of an numeric intrinsic type",
12882 sym->name, &sym->declared_at);
12883 return;
12886 if (sym->attr.allocatable || sym->attr.codimension
12887 || sym->attr.pointer || sym->attr.value)
12889 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12890 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12891 "attribute", sym->name, &sym->declared_at);
12892 return;
12895 if (sym->attr.intent == INTENT_OUT)
12897 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12898 "have the INTENT(OUT) attribute",
12899 sym->name, &sym->declared_at);
12900 return;
12902 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
12904 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
12905 "either be a scalar or an assumed-size array",
12906 sym->name, &sym->declared_at);
12907 return;
12910 /* Set the type to TYPE(*) and add a dimension(*) to ensure
12911 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
12912 packing. */
12913 sym->ts.type = BT_ASSUMED;
12914 sym->as = gfc_get_array_spec ();
12915 sym->as->type = AS_ASSUMED_SIZE;
12916 sym->as->rank = 1;
12917 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
12919 else if (sym->ts.type == BT_ASSUMED)
12921 /* TS 29113, C407a. */
12922 if (!sym->attr.dummy)
12924 gfc_error ("Assumed type of variable %s at %L is only permitted "
12925 "for dummy variables", sym->name, &sym->declared_at);
12926 return;
12928 if (sym->attr.allocatable || sym->attr.codimension
12929 || sym->attr.pointer || sym->attr.value)
12931 gfc_error ("Assumed-type variable %s at %L may not have the "
12932 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
12933 sym->name, &sym->declared_at);
12934 return;
12936 if (sym->attr.intent == INTENT_OUT)
12938 gfc_error ("Assumed-type variable %s at %L may not have the "
12939 "INTENT(OUT) attribute",
12940 sym->name, &sym->declared_at);
12941 return;
12943 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
12945 gfc_error ("Assumed-type variable %s at %L shall not be an "
12946 "explicit-shape array", sym->name, &sym->declared_at);
12947 return;
12951 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12952 do this for something that was implicitly typed because that is handled
12953 in gfc_set_default_type. Handle dummy arguments and procedure
12954 definitions separately. Also, anything that is use associated is not
12955 handled here but instead is handled in the module it is declared in.
12956 Finally, derived type definitions are allowed to be BIND(C) since that
12957 only implies that they're interoperable, and they are checked fully for
12958 interoperability when a variable is declared of that type. */
12959 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12960 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12961 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12963 bool t = true;
12965 /* First, make sure the variable is declared at the
12966 module-level scope (J3/04-007, Section 15.3). */
12967 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12968 sym->attr.in_common == 0)
12970 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12971 "is neither a COMMON block nor declared at the "
12972 "module level scope", sym->name, &(sym->declared_at));
12973 t = false;
12975 else if (sym->common_head != NULL)
12977 t = verify_com_block_vars_c_interop (sym->common_head);
12979 else
12981 /* If type() declaration, we need to verify that the components
12982 of the given type are all C interoperable, etc. */
12983 if (sym->ts.type == BT_DERIVED &&
12984 sym->ts.u.derived->attr.is_c_interop != 1)
12986 /* Make sure the user marked the derived type as BIND(C). If
12987 not, call the verify routine. This could print an error
12988 for the derived type more than once if multiple variables
12989 of that type are declared. */
12990 if (sym->ts.u.derived->attr.is_bind_c != 1)
12991 verify_bind_c_derived_type (sym->ts.u.derived);
12992 t = false;
12995 /* Verify the variable itself as C interoperable if it
12996 is BIND(C). It is not possible for this to succeed if
12997 the verify_bind_c_derived_type failed, so don't have to handle
12998 any error returned by verify_bind_c_derived_type. */
12999 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13000 sym->common_block);
13003 if (!t)
13005 /* clear the is_bind_c flag to prevent reporting errors more than
13006 once if something failed. */
13007 sym->attr.is_bind_c = 0;
13008 return;
13012 /* If a derived type symbol has reached this point, without its
13013 type being declared, we have an error. Notice that most
13014 conditions that produce undefined derived types have already
13015 been dealt with. However, the likes of:
13016 implicit type(t) (t) ..... call foo (t) will get us here if
13017 the type is not declared in the scope of the implicit
13018 statement. Change the type to BT_UNKNOWN, both because it is so
13019 and to prevent an ICE. */
13020 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13021 && sym->ts.u.derived->components == NULL
13022 && !sym->ts.u.derived->attr.zero_comp)
13024 gfc_error ("The derived type '%s' at %L is of type '%s', "
13025 "which has not been defined", sym->name,
13026 &sym->declared_at, sym->ts.u.derived->name);
13027 sym->ts.type = BT_UNKNOWN;
13028 return;
13031 /* Make sure that the derived type has been resolved and that the
13032 derived type is visible in the symbol's namespace, if it is a
13033 module function and is not PRIVATE. */
13034 if (sym->ts.type == BT_DERIVED
13035 && sym->ts.u.derived->attr.use_assoc
13036 && sym->ns->proc_name
13037 && sym->ns->proc_name->attr.flavor == FL_MODULE
13038 && !resolve_fl_derived (sym->ts.u.derived))
13039 return;
13041 /* Unless the derived-type declaration is use associated, Fortran 95
13042 does not allow public entries of private derived types.
13043 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13044 161 in 95-006r3. */
13045 if (sym->ts.type == BT_DERIVED
13046 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13047 && !sym->ts.u.derived->attr.use_assoc
13048 && gfc_check_symbol_access (sym)
13049 && !gfc_check_symbol_access (sym->ts.u.derived)
13050 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13051 "derived type '%s'",
13052 (sym->attr.flavor == FL_PARAMETER)
13053 ? "parameter" : "variable",
13054 sym->name, &sym->declared_at,
13055 sym->ts.u.derived->name))
13056 return;
13058 /* F2008, C1302. */
13059 if (sym->ts.type == BT_DERIVED
13060 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13061 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13062 || sym->ts.u.derived->attr.lock_comp)
13063 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13065 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13066 "type LOCK_TYPE must be a coarray", sym->name,
13067 &sym->declared_at);
13068 return;
13071 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13072 default initialization is defined (5.1.2.4.4). */
13073 if (sym->ts.type == BT_DERIVED
13074 && sym->attr.dummy
13075 && sym->attr.intent == INTENT_OUT
13076 && sym->as
13077 && sym->as->type == AS_ASSUMED_SIZE)
13079 for (c = sym->ts.u.derived->components; c; c = c->next)
13081 if (c->initializer)
13083 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13084 "ASSUMED SIZE and so cannot have a default initializer",
13085 sym->name, &sym->declared_at);
13086 return;
13091 /* F2008, C542. */
13092 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13093 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13095 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13096 "INTENT(OUT)", sym->name, &sym->declared_at);
13097 return;
13100 /* F2008, C525. */
13101 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13102 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13103 && CLASS_DATA (sym)->attr.coarray_comp))
13104 || class_attr.codimension)
13105 && (sym->attr.result || sym->result == sym))
13107 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13108 "a coarray component", sym->name, &sym->declared_at);
13109 return;
13112 /* F2008, C524. */
13113 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13114 && sym->ts.u.derived->ts.is_iso_c)
13116 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13117 "shall not be a coarray", sym->name, &sym->declared_at);
13118 return;
13121 /* F2008, C525. */
13122 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13123 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13124 && CLASS_DATA (sym)->attr.coarray_comp))
13125 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13126 || class_attr.allocatable))
13128 gfc_error ("Variable '%s' at %L with coarray component "
13129 "shall be a nonpointer, nonallocatable scalar",
13130 sym->name, &sym->declared_at);
13131 return;
13134 /* F2008, C526. The function-result case was handled above. */
13135 if (class_attr.codimension
13136 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13137 || sym->attr.select_type_temporary
13138 || sym->ns->save_all
13139 || sym->ns->proc_name->attr.flavor == FL_MODULE
13140 || sym->ns->proc_name->attr.is_main_program
13141 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13143 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13144 "nor a dummy argument", sym->name, &sym->declared_at);
13145 return;
13147 /* F2008, C528. */
13148 else if (class_attr.codimension && !sym->attr.select_type_temporary
13149 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13151 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13152 "deferred shape", sym->name, &sym->declared_at);
13153 return;
13155 else if (class_attr.codimension && class_attr.allocatable && as
13156 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13158 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13159 "deferred shape", sym->name, &sym->declared_at);
13160 return;
13163 /* F2008, C541. */
13164 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13165 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13166 && CLASS_DATA (sym)->attr.coarray_comp))
13167 || (class_attr.codimension && class_attr.allocatable))
13168 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13170 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13171 "allocatable coarray or have coarray components",
13172 sym->name, &sym->declared_at);
13173 return;
13176 if (class_attr.codimension && sym->attr.dummy
13177 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13179 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13180 "procedure '%s'", sym->name, &sym->declared_at,
13181 sym->ns->proc_name->name);
13182 return;
13185 if (sym->ts.type == BT_LOGICAL
13186 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13187 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13188 && sym->ns->proc_name->attr.is_bind_c)))
13190 int i;
13191 for (i = 0; gfc_logical_kinds[i].kind; i++)
13192 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13193 break;
13194 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13195 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13196 "%L with non-C_Bool kind in BIND(C) procedure "
13197 "'%s'", sym->name, &sym->declared_at,
13198 sym->ns->proc_name->name))
13199 return;
13200 else if (!gfc_logical_kinds[i].c_bool
13201 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13202 "'%s' at %L with non-C_Bool kind in "
13203 "BIND(C) procedure '%s'", sym->name,
13204 &sym->declared_at,
13205 sym->attr.function ? sym->name
13206 : sym->ns->proc_name->name))
13207 return;
13210 switch (sym->attr.flavor)
13212 case FL_VARIABLE:
13213 if (!resolve_fl_variable (sym, mp_flag))
13214 return;
13215 break;
13217 case FL_PROCEDURE:
13218 if (!resolve_fl_procedure (sym, mp_flag))
13219 return;
13220 break;
13222 case FL_NAMELIST:
13223 if (!resolve_fl_namelist (sym))
13224 return;
13225 break;
13227 case FL_PARAMETER:
13228 if (!resolve_fl_parameter (sym))
13229 return;
13230 break;
13232 default:
13233 break;
13236 /* Resolve array specifier. Check as well some constraints
13237 on COMMON blocks. */
13239 check_constant = sym->attr.in_common && !sym->attr.pointer;
13241 /* Set the formal_arg_flag so that check_conflict will not throw
13242 an error for host associated variables in the specification
13243 expression for an array_valued function. */
13244 if (sym->attr.function && sym->as)
13245 formal_arg_flag = 1;
13247 saved_specification_expr = specification_expr;
13248 specification_expr = true;
13249 gfc_resolve_array_spec (sym->as, check_constant);
13250 specification_expr = saved_specification_expr;
13252 formal_arg_flag = 0;
13254 /* Resolve formal namespaces. */
13255 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13256 && !sym->attr.contained && !sym->attr.intrinsic)
13257 gfc_resolve (sym->formal_ns);
13259 /* Make sure the formal namespace is present. */
13260 if (sym->formal && !sym->formal_ns)
13262 gfc_formal_arglist *formal = sym->formal;
13263 while (formal && !formal->sym)
13264 formal = formal->next;
13266 if (formal)
13268 sym->formal_ns = formal->sym->ns;
13269 if (sym->ns != formal->sym->ns)
13270 sym->formal_ns->refs++;
13274 /* Check threadprivate restrictions. */
13275 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13276 && (!sym->attr.in_common
13277 && sym->module == NULL
13278 && (sym->ns->proc_name == NULL
13279 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13280 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13282 /* If we have come this far we can apply default-initializers, as
13283 described in 14.7.5, to those variables that have not already
13284 been assigned one. */
13285 if (sym->ts.type == BT_DERIVED
13286 && !sym->value
13287 && !sym->attr.allocatable
13288 && !sym->attr.alloc_comp)
13290 symbol_attribute *a = &sym->attr;
13292 if ((!a->save && !a->dummy && !a->pointer
13293 && !a->in_common && !a->use_assoc
13294 && (a->referenced || a->result)
13295 && !(a->function && sym != sym->result))
13296 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13297 apply_default_init (sym);
13300 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13301 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13302 && !CLASS_DATA (sym)->attr.class_pointer
13303 && !CLASS_DATA (sym)->attr.allocatable)
13304 apply_default_init (sym);
13306 /* If this symbol has a type-spec, check it. */
13307 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13308 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13309 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13310 return;
13314 /************* Resolve DATA statements *************/
13316 static struct
13318 gfc_data_value *vnode;
13319 mpz_t left;
13321 values;
13324 /* Advance the values structure to point to the next value in the data list. */
13326 static bool
13327 next_data_value (void)
13329 while (mpz_cmp_ui (values.left, 0) == 0)
13332 if (values.vnode->next == NULL)
13333 return false;
13335 values.vnode = values.vnode->next;
13336 mpz_set (values.left, values.vnode->repeat);
13339 return true;
13343 static bool
13344 check_data_variable (gfc_data_variable *var, locus *where)
13346 gfc_expr *e;
13347 mpz_t size;
13348 mpz_t offset;
13349 bool t;
13350 ar_type mark = AR_UNKNOWN;
13351 int i;
13352 mpz_t section_index[GFC_MAX_DIMENSIONS];
13353 gfc_ref *ref;
13354 gfc_array_ref *ar;
13355 gfc_symbol *sym;
13356 int has_pointer;
13358 if (!gfc_resolve_expr (var->expr))
13359 return false;
13361 ar = NULL;
13362 mpz_init_set_si (offset, 0);
13363 e = var->expr;
13365 if (e->expr_type != EXPR_VARIABLE)
13366 gfc_internal_error ("check_data_variable(): Bad expression");
13368 sym = e->symtree->n.sym;
13370 if (sym->ns->is_block_data && !sym->attr.in_common)
13372 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13373 sym->name, &sym->declared_at);
13376 if (e->ref == NULL && sym->as)
13378 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13379 " declaration", sym->name, where);
13380 return false;
13383 has_pointer = sym->attr.pointer;
13385 if (gfc_is_coindexed (e))
13387 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13388 where);
13389 return false;
13392 for (ref = e->ref; ref; ref = ref->next)
13394 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13395 has_pointer = 1;
13397 if (has_pointer
13398 && ref->type == REF_ARRAY
13399 && ref->u.ar.type != AR_FULL)
13401 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13402 "be a full array", sym->name, where);
13403 return false;
13407 if (e->rank == 0 || has_pointer)
13409 mpz_init_set_ui (size, 1);
13410 ref = NULL;
13412 else
13414 ref = e->ref;
13416 /* Find the array section reference. */
13417 for (ref = e->ref; ref; ref = ref->next)
13419 if (ref->type != REF_ARRAY)
13420 continue;
13421 if (ref->u.ar.type == AR_ELEMENT)
13422 continue;
13423 break;
13425 gcc_assert (ref);
13427 /* Set marks according to the reference pattern. */
13428 switch (ref->u.ar.type)
13430 case AR_FULL:
13431 mark = AR_FULL;
13432 break;
13434 case AR_SECTION:
13435 ar = &ref->u.ar;
13436 /* Get the start position of array section. */
13437 gfc_get_section_index (ar, section_index, &offset);
13438 mark = AR_SECTION;
13439 break;
13441 default:
13442 gcc_unreachable ();
13445 if (!gfc_array_size (e, &size))
13447 gfc_error ("Nonconstant array section at %L in DATA statement",
13448 &e->where);
13449 mpz_clear (offset);
13450 return false;
13454 t = true;
13456 while (mpz_cmp_ui (size, 0) > 0)
13458 if (!next_data_value ())
13460 gfc_error ("DATA statement at %L has more variables than values",
13461 where);
13462 t = false;
13463 break;
13466 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13467 if (!t)
13468 break;
13470 /* If we have more than one element left in the repeat count,
13471 and we have more than one element left in the target variable,
13472 then create a range assignment. */
13473 /* FIXME: Only done for full arrays for now, since array sections
13474 seem tricky. */
13475 if (mark == AR_FULL && ref && ref->next == NULL
13476 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13478 mpz_t range;
13480 if (mpz_cmp (size, values.left) >= 0)
13482 mpz_init_set (range, values.left);
13483 mpz_sub (size, size, values.left);
13484 mpz_set_ui (values.left, 0);
13486 else
13488 mpz_init_set (range, size);
13489 mpz_sub (values.left, values.left, size);
13490 mpz_set_ui (size, 0);
13493 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13494 offset, &range);
13496 mpz_add (offset, offset, range);
13497 mpz_clear (range);
13499 if (!t)
13500 break;
13503 /* Assign initial value to symbol. */
13504 else
13506 mpz_sub_ui (values.left, values.left, 1);
13507 mpz_sub_ui (size, size, 1);
13509 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13510 offset, NULL);
13511 if (!t)
13512 break;
13514 if (mark == AR_FULL)
13515 mpz_add_ui (offset, offset, 1);
13517 /* Modify the array section indexes and recalculate the offset
13518 for next element. */
13519 else if (mark == AR_SECTION)
13520 gfc_advance_section (section_index, ar, &offset);
13524 if (mark == AR_SECTION)
13526 for (i = 0; i < ar->dimen; i++)
13527 mpz_clear (section_index[i]);
13530 mpz_clear (size);
13531 mpz_clear (offset);
13533 return t;
13537 static bool traverse_data_var (gfc_data_variable *, locus *);
13539 /* Iterate over a list of elements in a DATA statement. */
13541 static bool
13542 traverse_data_list (gfc_data_variable *var, locus *where)
13544 mpz_t trip;
13545 iterator_stack frame;
13546 gfc_expr *e, *start, *end, *step;
13547 bool retval = true;
13549 mpz_init (frame.value);
13550 mpz_init (trip);
13552 start = gfc_copy_expr (var->iter.start);
13553 end = gfc_copy_expr (var->iter.end);
13554 step = gfc_copy_expr (var->iter.step);
13556 if (!gfc_simplify_expr (start, 1)
13557 || start->expr_type != EXPR_CONSTANT)
13559 gfc_error ("start of implied-do loop at %L could not be "
13560 "simplified to a constant value", &start->where);
13561 retval = false;
13562 goto cleanup;
13564 if (!gfc_simplify_expr (end, 1)
13565 || end->expr_type != EXPR_CONSTANT)
13567 gfc_error ("end of implied-do loop at %L could not be "
13568 "simplified to a constant value", &start->where);
13569 retval = false;
13570 goto cleanup;
13572 if (!gfc_simplify_expr (step, 1)
13573 || step->expr_type != EXPR_CONSTANT)
13575 gfc_error ("step of implied-do loop at %L could not be "
13576 "simplified to a constant value", &start->where);
13577 retval = false;
13578 goto cleanup;
13581 mpz_set (trip, end->value.integer);
13582 mpz_sub (trip, trip, start->value.integer);
13583 mpz_add (trip, trip, step->value.integer);
13585 mpz_div (trip, trip, step->value.integer);
13587 mpz_set (frame.value, start->value.integer);
13589 frame.prev = iter_stack;
13590 frame.variable = var->iter.var->symtree;
13591 iter_stack = &frame;
13593 while (mpz_cmp_ui (trip, 0) > 0)
13595 if (!traverse_data_var (var->list, where))
13597 retval = false;
13598 goto cleanup;
13601 e = gfc_copy_expr (var->expr);
13602 if (!gfc_simplify_expr (e, 1))
13604 gfc_free_expr (e);
13605 retval = false;
13606 goto cleanup;
13609 mpz_add (frame.value, frame.value, step->value.integer);
13611 mpz_sub_ui (trip, trip, 1);
13614 cleanup:
13615 mpz_clear (frame.value);
13616 mpz_clear (trip);
13618 gfc_free_expr (start);
13619 gfc_free_expr (end);
13620 gfc_free_expr (step);
13622 iter_stack = frame.prev;
13623 return retval;
13627 /* Type resolve variables in the variable list of a DATA statement. */
13629 static bool
13630 traverse_data_var (gfc_data_variable *var, locus *where)
13632 bool t;
13634 for (; var; var = var->next)
13636 if (var->expr == NULL)
13637 t = traverse_data_list (var, where);
13638 else
13639 t = check_data_variable (var, where);
13641 if (!t)
13642 return false;
13645 return true;
13649 /* Resolve the expressions and iterators associated with a data statement.
13650 This is separate from the assignment checking because data lists should
13651 only be resolved once. */
13653 static bool
13654 resolve_data_variables (gfc_data_variable *d)
13656 for (; d; d = d->next)
13658 if (d->list == NULL)
13660 if (!gfc_resolve_expr (d->expr))
13661 return false;
13663 else
13665 if (!gfc_resolve_iterator (&d->iter, false, true))
13666 return false;
13668 if (!resolve_data_variables (d->list))
13669 return false;
13673 return true;
13677 /* Resolve a single DATA statement. We implement this by storing a pointer to
13678 the value list into static variables, and then recursively traversing the
13679 variables list, expanding iterators and such. */
13681 static void
13682 resolve_data (gfc_data *d)
13685 if (!resolve_data_variables (d->var))
13686 return;
13688 values.vnode = d->value;
13689 if (d->value == NULL)
13690 mpz_set_ui (values.left, 0);
13691 else
13692 mpz_set (values.left, d->value->repeat);
13694 if (!traverse_data_var (d->var, &d->where))
13695 return;
13697 /* At this point, we better not have any values left. */
13699 if (next_data_value ())
13700 gfc_error ("DATA statement at %L has more values than variables",
13701 &d->where);
13705 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13706 accessed by host or use association, is a dummy argument to a pure function,
13707 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13708 is storage associated with any such variable, shall not be used in the
13709 following contexts: (clients of this function). */
13711 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13712 procedure. Returns zero if assignment is OK, nonzero if there is a
13713 problem. */
13715 gfc_impure_variable (gfc_symbol *sym)
13717 gfc_symbol *proc;
13718 gfc_namespace *ns;
13720 if (sym->attr.use_assoc || sym->attr.in_common)
13721 return 1;
13723 /* Check if the symbol's ns is inside the pure procedure. */
13724 for (ns = gfc_current_ns; ns; ns = ns->parent)
13726 if (ns == sym->ns)
13727 break;
13728 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13729 return 1;
13732 proc = sym->ns->proc_name;
13733 if (sym->attr.dummy
13734 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13735 || proc->attr.function))
13736 return 1;
13738 /* TODO: Sort out what can be storage associated, if anything, and include
13739 it here. In principle equivalences should be scanned but it does not
13740 seem to be possible to storage associate an impure variable this way. */
13741 return 0;
13745 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13746 current namespace is inside a pure procedure. */
13749 gfc_pure (gfc_symbol *sym)
13751 symbol_attribute attr;
13752 gfc_namespace *ns;
13754 if (sym == NULL)
13756 /* Check if the current namespace or one of its parents
13757 belongs to a pure procedure. */
13758 for (ns = gfc_current_ns; ns; ns = ns->parent)
13760 sym = ns->proc_name;
13761 if (sym == NULL)
13762 return 0;
13763 attr = sym->attr;
13764 if (attr.flavor == FL_PROCEDURE && attr.pure)
13765 return 1;
13767 return 0;
13770 attr = sym->attr;
13772 return attr.flavor == FL_PROCEDURE && attr.pure;
13776 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13777 checks if the current namespace is implicitly pure. Note that this
13778 function returns false for a PURE procedure. */
13781 gfc_implicit_pure (gfc_symbol *sym)
13783 gfc_namespace *ns;
13785 if (sym == NULL)
13787 /* Check if the current procedure is implicit_pure. Walk up
13788 the procedure list until we find a procedure. */
13789 for (ns = gfc_current_ns; ns; ns = ns->parent)
13791 sym = ns->proc_name;
13792 if (sym == NULL)
13793 return 0;
13795 if (sym->attr.flavor == FL_PROCEDURE)
13796 break;
13800 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13801 && !sym->attr.pure;
13805 /* Test whether the current procedure is elemental or not. */
13808 gfc_elemental (gfc_symbol *sym)
13810 symbol_attribute attr;
13812 if (sym == NULL)
13813 sym = gfc_current_ns->proc_name;
13814 if (sym == NULL)
13815 return 0;
13816 attr = sym->attr;
13818 return attr.flavor == FL_PROCEDURE && attr.elemental;
13822 /* Warn about unused labels. */
13824 static void
13825 warn_unused_fortran_label (gfc_st_label *label)
13827 if (label == NULL)
13828 return;
13830 warn_unused_fortran_label (label->left);
13832 if (label->defined == ST_LABEL_UNKNOWN)
13833 return;
13835 switch (label->referenced)
13837 case ST_LABEL_UNKNOWN:
13838 gfc_warning ("Label %d at %L defined but not used", label->value,
13839 &label->where);
13840 break;
13842 case ST_LABEL_BAD_TARGET:
13843 gfc_warning ("Label %d at %L defined but cannot be used",
13844 label->value, &label->where);
13845 break;
13847 default:
13848 break;
13851 warn_unused_fortran_label (label->right);
13855 /* Returns the sequence type of a symbol or sequence. */
13857 static seq_type
13858 sequence_type (gfc_typespec ts)
13860 seq_type result;
13861 gfc_component *c;
13863 switch (ts.type)
13865 case BT_DERIVED:
13867 if (ts.u.derived->components == NULL)
13868 return SEQ_NONDEFAULT;
13870 result = sequence_type (ts.u.derived->components->ts);
13871 for (c = ts.u.derived->components->next; c; c = c->next)
13872 if (sequence_type (c->ts) != result)
13873 return SEQ_MIXED;
13875 return result;
13877 case BT_CHARACTER:
13878 if (ts.kind != gfc_default_character_kind)
13879 return SEQ_NONDEFAULT;
13881 return SEQ_CHARACTER;
13883 case BT_INTEGER:
13884 if (ts.kind != gfc_default_integer_kind)
13885 return SEQ_NONDEFAULT;
13887 return SEQ_NUMERIC;
13889 case BT_REAL:
13890 if (!(ts.kind == gfc_default_real_kind
13891 || ts.kind == gfc_default_double_kind))
13892 return SEQ_NONDEFAULT;
13894 return SEQ_NUMERIC;
13896 case BT_COMPLEX:
13897 if (ts.kind != gfc_default_complex_kind)
13898 return SEQ_NONDEFAULT;
13900 return SEQ_NUMERIC;
13902 case BT_LOGICAL:
13903 if (ts.kind != gfc_default_logical_kind)
13904 return SEQ_NONDEFAULT;
13906 return SEQ_NUMERIC;
13908 default:
13909 return SEQ_NONDEFAULT;
13914 /* Resolve derived type EQUIVALENCE object. */
13916 static bool
13917 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13919 gfc_component *c = derived->components;
13921 if (!derived)
13922 return true;
13924 /* Shall not be an object of nonsequence derived type. */
13925 if (!derived->attr.sequence)
13927 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13928 "attribute to be an EQUIVALENCE object", sym->name,
13929 &e->where);
13930 return false;
13933 /* Shall not have allocatable components. */
13934 if (derived->attr.alloc_comp)
13936 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13937 "components to be an EQUIVALENCE object",sym->name,
13938 &e->where);
13939 return false;
13942 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13944 gfc_error ("Derived type variable '%s' at %L with default "
13945 "initialization cannot be in EQUIVALENCE with a variable "
13946 "in COMMON", sym->name, &e->where);
13947 return false;
13950 for (; c ; c = c->next)
13952 if (c->ts.type == BT_DERIVED
13953 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
13954 return false;
13956 /* Shall not be an object of sequence derived type containing a pointer
13957 in the structure. */
13958 if (c->attr.pointer)
13960 gfc_error ("Derived type variable '%s' at %L with pointer "
13961 "component(s) cannot be an EQUIVALENCE object",
13962 sym->name, &e->where);
13963 return false;
13966 return true;
13970 /* Resolve equivalence object.
13971 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13972 an allocatable array, an object of nonsequence derived type, an object of
13973 sequence derived type containing a pointer at any level of component
13974 selection, an automatic object, a function name, an entry name, a result
13975 name, a named constant, a structure component, or a subobject of any of
13976 the preceding objects. A substring shall not have length zero. A
13977 derived type shall not have components with default initialization nor
13978 shall two objects of an equivalence group be initialized.
13979 Either all or none of the objects shall have an protected attribute.
13980 The simple constraints are done in symbol.c(check_conflict) and the rest
13981 are implemented here. */
13983 static void
13984 resolve_equivalence (gfc_equiv *eq)
13986 gfc_symbol *sym;
13987 gfc_symbol *first_sym;
13988 gfc_expr *e;
13989 gfc_ref *r;
13990 locus *last_where = NULL;
13991 seq_type eq_type, last_eq_type;
13992 gfc_typespec *last_ts;
13993 int object, cnt_protected;
13994 const char *msg;
13996 last_ts = &eq->expr->symtree->n.sym->ts;
13998 first_sym = eq->expr->symtree->n.sym;
14000 cnt_protected = 0;
14002 for (object = 1; eq; eq = eq->eq, object++)
14004 e = eq->expr;
14006 e->ts = e->symtree->n.sym->ts;
14007 /* match_varspec might not know yet if it is seeing
14008 array reference or substring reference, as it doesn't
14009 know the types. */
14010 if (e->ref && e->ref->type == REF_ARRAY)
14012 gfc_ref *ref = e->ref;
14013 sym = e->symtree->n.sym;
14015 if (sym->attr.dimension)
14017 ref->u.ar.as = sym->as;
14018 ref = ref->next;
14021 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14022 if (e->ts.type == BT_CHARACTER
14023 && ref
14024 && ref->type == REF_ARRAY
14025 && ref->u.ar.dimen == 1
14026 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14027 && ref->u.ar.stride[0] == NULL)
14029 gfc_expr *start = ref->u.ar.start[0];
14030 gfc_expr *end = ref->u.ar.end[0];
14031 void *mem = NULL;
14033 /* Optimize away the (:) reference. */
14034 if (start == NULL && end == NULL)
14036 if (e->ref == ref)
14037 e->ref = ref->next;
14038 else
14039 e->ref->next = ref->next;
14040 mem = ref;
14042 else
14044 ref->type = REF_SUBSTRING;
14045 if (start == NULL)
14046 start = gfc_get_int_expr (gfc_default_integer_kind,
14047 NULL, 1);
14048 ref->u.ss.start = start;
14049 if (end == NULL && e->ts.u.cl)
14050 end = gfc_copy_expr (e->ts.u.cl->length);
14051 ref->u.ss.end = end;
14052 ref->u.ss.length = e->ts.u.cl;
14053 e->ts.u.cl = NULL;
14055 ref = ref->next;
14056 free (mem);
14059 /* Any further ref is an error. */
14060 if (ref)
14062 gcc_assert (ref->type == REF_ARRAY);
14063 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14064 &ref->u.ar.where);
14065 continue;
14069 if (!gfc_resolve_expr (e))
14070 continue;
14072 sym = e->symtree->n.sym;
14074 if (sym->attr.is_protected)
14075 cnt_protected++;
14076 if (cnt_protected > 0 && cnt_protected != object)
14078 gfc_error ("Either all or none of the objects in the "
14079 "EQUIVALENCE set at %L shall have the "
14080 "PROTECTED attribute",
14081 &e->where);
14082 break;
14085 /* Shall not equivalence common block variables in a PURE procedure. */
14086 if (sym->ns->proc_name
14087 && sym->ns->proc_name->attr.pure
14088 && sym->attr.in_common)
14090 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14091 "object in the pure procedure '%s'",
14092 sym->name, &e->where, sym->ns->proc_name->name);
14093 break;
14096 /* Shall not be a named constant. */
14097 if (e->expr_type == EXPR_CONSTANT)
14099 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14100 "object", sym->name, &e->where);
14101 continue;
14104 if (e->ts.type == BT_DERIVED
14105 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14106 continue;
14108 /* Check that the types correspond correctly:
14109 Note 5.28:
14110 A numeric sequence structure may be equivalenced to another sequence
14111 structure, an object of default integer type, default real type, double
14112 precision real type, default logical type such that components of the
14113 structure ultimately only become associated to objects of the same
14114 kind. A character sequence structure may be equivalenced to an object
14115 of default character kind or another character sequence structure.
14116 Other objects may be equivalenced only to objects of the same type and
14117 kind parameters. */
14119 /* Identical types are unconditionally OK. */
14120 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14121 goto identical_types;
14123 last_eq_type = sequence_type (*last_ts);
14124 eq_type = sequence_type (sym->ts);
14126 /* Since the pair of objects is not of the same type, mixed or
14127 non-default sequences can be rejected. */
14129 msg = "Sequence %s with mixed components in EQUIVALENCE "
14130 "statement at %L with different type objects";
14131 if ((object ==2
14132 && last_eq_type == SEQ_MIXED
14133 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14134 || (eq_type == SEQ_MIXED
14135 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14136 continue;
14138 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14139 "statement at %L with objects of different type";
14140 if ((object ==2
14141 && last_eq_type == SEQ_NONDEFAULT
14142 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14143 || (eq_type == SEQ_NONDEFAULT
14144 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14145 continue;
14147 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14148 "EQUIVALENCE statement at %L";
14149 if (last_eq_type == SEQ_CHARACTER
14150 && eq_type != SEQ_CHARACTER
14151 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14152 continue;
14154 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14155 "EQUIVALENCE statement at %L";
14156 if (last_eq_type == SEQ_NUMERIC
14157 && eq_type != SEQ_NUMERIC
14158 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14159 continue;
14161 identical_types:
14162 last_ts =&sym->ts;
14163 last_where = &e->where;
14165 if (!e->ref)
14166 continue;
14168 /* Shall not be an automatic array. */
14169 if (e->ref->type == REF_ARRAY
14170 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14172 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14173 "an EQUIVALENCE object", sym->name, &e->where);
14174 continue;
14177 r = e->ref;
14178 while (r)
14180 /* Shall not be a structure component. */
14181 if (r->type == REF_COMPONENT)
14183 gfc_error ("Structure component '%s' at %L cannot be an "
14184 "EQUIVALENCE object",
14185 r->u.c.component->name, &e->where);
14186 break;
14189 /* A substring shall not have length zero. */
14190 if (r->type == REF_SUBSTRING)
14192 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14194 gfc_error ("Substring at %L has length zero",
14195 &r->u.ss.start->where);
14196 break;
14199 r = r->next;
14205 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14207 static void
14208 resolve_fntype (gfc_namespace *ns)
14210 gfc_entry_list *el;
14211 gfc_symbol *sym;
14213 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14214 return;
14216 /* If there are any entries, ns->proc_name is the entry master
14217 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14218 if (ns->entries)
14219 sym = ns->entries->sym;
14220 else
14221 sym = ns->proc_name;
14222 if (sym->result == sym
14223 && sym->ts.type == BT_UNKNOWN
14224 && !gfc_set_default_type (sym, 0, NULL)
14225 && !sym->attr.untyped)
14227 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14228 sym->name, &sym->declared_at);
14229 sym->attr.untyped = 1;
14232 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14233 && !sym->attr.contained
14234 && !gfc_check_symbol_access (sym->ts.u.derived)
14235 && gfc_check_symbol_access (sym))
14237 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14238 "%L of PRIVATE type '%s'", sym->name,
14239 &sym->declared_at, sym->ts.u.derived->name);
14242 if (ns->entries)
14243 for (el = ns->entries->next; el; el = el->next)
14245 if (el->sym->result == el->sym
14246 && el->sym->ts.type == BT_UNKNOWN
14247 && !gfc_set_default_type (el->sym, 0, NULL)
14248 && !el->sym->attr.untyped)
14250 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14251 el->sym->name, &el->sym->declared_at);
14252 el->sym->attr.untyped = 1;
14258 /* 12.3.2.1.1 Defined operators. */
14260 static bool
14261 check_uop_procedure (gfc_symbol *sym, locus where)
14263 gfc_formal_arglist *formal;
14265 if (!sym->attr.function)
14267 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14268 sym->name, &where);
14269 return false;
14272 if (sym->ts.type == BT_CHARACTER
14273 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14274 && !(sym->result && sym->result->ts.u.cl
14275 && sym->result->ts.u.cl->length))
14277 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14278 "character length", sym->name, &where);
14279 return false;
14282 formal = gfc_sym_get_dummy_args (sym);
14283 if (!formal || !formal->sym)
14285 gfc_error ("User operator procedure '%s' at %L must have at least "
14286 "one argument", sym->name, &where);
14287 return false;
14290 if (formal->sym->attr.intent != INTENT_IN)
14292 gfc_error ("First argument of operator interface at %L must be "
14293 "INTENT(IN)", &where);
14294 return false;
14297 if (formal->sym->attr.optional)
14299 gfc_error ("First argument of operator interface at %L cannot be "
14300 "optional", &where);
14301 return false;
14304 formal = formal->next;
14305 if (!formal || !formal->sym)
14306 return true;
14308 if (formal->sym->attr.intent != INTENT_IN)
14310 gfc_error ("Second argument of operator interface at %L must be "
14311 "INTENT(IN)", &where);
14312 return false;
14315 if (formal->sym->attr.optional)
14317 gfc_error ("Second argument of operator interface at %L cannot be "
14318 "optional", &where);
14319 return false;
14322 if (formal->next)
14324 gfc_error ("Operator interface at %L must have, at most, two "
14325 "arguments", &where);
14326 return false;
14329 return true;
14332 static void
14333 gfc_resolve_uops (gfc_symtree *symtree)
14335 gfc_interface *itr;
14337 if (symtree == NULL)
14338 return;
14340 gfc_resolve_uops (symtree->left);
14341 gfc_resolve_uops (symtree->right);
14343 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14344 check_uop_procedure (itr->sym, itr->sym->declared_at);
14348 /* Examine all of the expressions associated with a program unit,
14349 assign types to all intermediate expressions, make sure that all
14350 assignments are to compatible types and figure out which names
14351 refer to which functions or subroutines. It doesn't check code
14352 block, which is handled by resolve_code. */
14354 static void
14355 resolve_types (gfc_namespace *ns)
14357 gfc_namespace *n;
14358 gfc_charlen *cl;
14359 gfc_data *d;
14360 gfc_equiv *eq;
14361 gfc_namespace* old_ns = gfc_current_ns;
14363 /* Check that all IMPLICIT types are ok. */
14364 if (!ns->seen_implicit_none)
14366 unsigned letter;
14367 for (letter = 0; letter != GFC_LETTERS; ++letter)
14368 if (ns->set_flag[letter]
14369 && !resolve_typespec_used (&ns->default_type[letter],
14370 &ns->implicit_loc[letter], NULL))
14371 return;
14374 gfc_current_ns = ns;
14376 resolve_entries (ns);
14378 resolve_common_vars (ns->blank_common.head, false);
14379 resolve_common_blocks (ns->common_root);
14381 resolve_contained_functions (ns);
14383 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14384 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14385 resolve_formal_arglist (ns->proc_name);
14387 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14389 for (cl = ns->cl_list; cl; cl = cl->next)
14390 resolve_charlen (cl);
14392 gfc_traverse_ns (ns, resolve_symbol);
14394 resolve_fntype (ns);
14396 for (n = ns->contained; n; n = n->sibling)
14398 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14399 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14400 "also be PURE", n->proc_name->name,
14401 &n->proc_name->declared_at);
14403 resolve_types (n);
14406 forall_flag = 0;
14407 do_concurrent_flag = 0;
14408 gfc_check_interfaces (ns);
14410 gfc_traverse_ns (ns, resolve_values);
14412 if (ns->save_all)
14413 gfc_save_all (ns);
14415 iter_stack = NULL;
14416 for (d = ns->data; d; d = d->next)
14417 resolve_data (d);
14419 iter_stack = NULL;
14420 gfc_traverse_ns (ns, gfc_formalize_init_value);
14422 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14424 if (ns->common_root != NULL)
14425 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
14427 for (eq = ns->equiv; eq; eq = eq->next)
14428 resolve_equivalence (eq);
14430 /* Warn about unused labels. */
14431 if (warn_unused_label)
14432 warn_unused_fortran_label (ns->st_labels);
14434 gfc_resolve_uops (ns->uop_root);
14436 gfc_current_ns = old_ns;
14440 /* Call resolve_code recursively. */
14442 static void
14443 resolve_codes (gfc_namespace *ns)
14445 gfc_namespace *n;
14446 bitmap_obstack old_obstack;
14448 if (ns->resolved == 1)
14449 return;
14451 for (n = ns->contained; n; n = n->sibling)
14452 resolve_codes (n);
14454 gfc_current_ns = ns;
14456 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14457 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14458 cs_base = NULL;
14460 /* Set to an out of range value. */
14461 current_entry_id = -1;
14463 old_obstack = labels_obstack;
14464 bitmap_obstack_initialize (&labels_obstack);
14466 resolve_code (ns->code, ns);
14468 bitmap_obstack_release (&labels_obstack);
14469 labels_obstack = old_obstack;
14473 /* This function is called after a complete program unit has been compiled.
14474 Its purpose is to examine all of the expressions associated with a program
14475 unit, assign types to all intermediate expressions, make sure that all
14476 assignments are to compatible types and figure out which names refer to
14477 which functions or subroutines. */
14479 void
14480 gfc_resolve (gfc_namespace *ns)
14482 gfc_namespace *old_ns;
14483 code_stack *old_cs_base;
14485 if (ns->resolved)
14486 return;
14488 ns->resolved = -1;
14489 old_ns = gfc_current_ns;
14490 old_cs_base = cs_base;
14492 resolve_types (ns);
14493 component_assignment_level = 0;
14494 resolve_codes (ns);
14496 gfc_current_ns = old_ns;
14497 cs_base = old_cs_base;
14498 ns->resolved = 1;
14500 gfc_run_passes (ns);