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
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
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/>. */
23 #include "coretypes.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
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
53 bitmap reachable_labels;
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
67 static bool actual_arg = false;
68 /* True when we are resolving an expression that is the first actual argument
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;
95 gfc_is_formal_arg (void)
97 return formal_arg_flag;
100 /* Is the symbol host associated? */
102 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
104 for (ns = ns->parent; ns; ns = ns->parent)
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. */
118 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
126 name, where, ts->u.derived->name);
128 gfc_error ("ABSTRACT type '%s' used at %L",
129 ts->u.derived->name, where);
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);
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)
158 gfc_error ("Interface '%s' at %L may not be generic",
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
165 gfc_error ("Interface '%s' at %L may not be a statement function",
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);
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);
187 static void resolve_symbol (gfc_symbol *sym);
190 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
193 resolve_procedure_interface (gfc_symbol *sym)
195 gfc_symbol *ifc = sym->ts.interface;
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
206 if (!check_proc_interface (ifc, &sym->declared_at))
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);
218 sym->ts = ifc->result->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))
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. */
264 resolve_formal_arglist (gfc_symbol *proc)
266 gfc_formal_arglist *f;
268 bool saved_specification_expr;
271 if (proc->result != NULL)
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;
286 for (f = proc->formal; f; f = f->next)
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,
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
306 && !resolve_procedure_interface (sym))
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);
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;
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);
370 if (sym->attr.flavor == FL_PROCEDURE)
375 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
376 "also be PURE", sym->name, &sym->declared_at);
380 else if (!sym->attr.pointer)
382 if (proc->attr.function && sym->attr.intent != INTENT_IN)
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);
390 gfc_error ("Argument '%s' of pure function '%s' at %L must "
391 "be INTENT(IN) or VALUE", sym->name, proc->name,
395 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
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);
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,
411 if (proc->attr.implicit_pure)
413 if (sym->attr.flavor == FL_PROCEDURE)
416 proc->attr.implicit_pure = 0;
418 else if (!sym->attr.pointer)
420 if (proc->attr.function && sym->attr.intent != INTENT_IN
422 proc->attr.implicit_pure = 0;
424 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
426 proc->attr.implicit_pure = 0;
430 if (gfc_elemental (proc))
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);
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);
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,
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,
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,
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,
489 /* Each dummy shall be specified to be scalar. */
490 if (proc->attr.proc == PROC_ST_FUNCTION)
494 gfc_error ("Argument '%s' of statement function at %L must "
495 "be scalar", sym->name, &sym->declared_at);
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);
516 /* Work function called when searching for symbols that have argument lists
517 associated with them. */
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)
526 resolve_formal_arglist (sym);
530 /* Given a namespace, resolve all formal argument lists within the namespace.
534 resolve_formal_arglists (gfc_namespace *ns)
539 gfc_traverse_ns (ns, find_arglists);
544 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
548 /* If this namespace is not a function or an entry master function,
550 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
551 || sym->attr.entry_master)
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
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"
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. */
603 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
605 gfc_formal_arglist *f, *new_arglist;
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)
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. */
633 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
635 gfc_formal_arglist *f, *head;
638 for (f = proc->formal; f; f = f->next)
643 for (new_args = head; new_args; new_args = new_args->next)
645 if (new_args->sym == f->sym)
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. */
662 resolve_entries (gfc_namespace *ns)
664 gfc_namespace *old_ns;
668 char name[GFC_MAX_SYMBOL_LEN + 1];
669 static int master_count = 0;
671 if (ns->proc_name == NULL)
674 /* No need to do anything if this procedure doesn't have alternate entry
679 /* We may already have resolved alternate entry points. */
680 if (ns->proc_name->attr.entry_master)
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;
691 /* Add the main entry point to the list of entry points. */
692 el = gfc_get_entry_list ();
693 el->sym = ns->proc_name;
695 el->next = ns->entries;
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)
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)
717 /* Add an entry statement for it. */
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);
739 gfc_typespec *ts, *fts;
740 gfc_array_spec *as, *fas;
741 gfc_add_function (&proc->attr, proc->name, NULL);
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;
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))
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))
775 && ts->u.cl->length->expr_type
776 != fts->u.cl->length->expr_type)
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);
789 sym = ns->entries->sym->result;
790 /* All result types the same. */
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);
799 /* Otherwise the result will be passed through a union by
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);
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);
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);
830 if (ts->type == BT_UNKNOWN)
831 ts = gfc_get_default_type (sym->name, NULL);
835 if (ts->kind == gfc_default_integer_kind)
839 if (ts->kind == gfc_default_real_kind
840 || ts->kind == gfc_default_double_kind)
844 if (ts->kind == gfc_default_complex_kind)
848 if (ts->kind == gfc_default_logical_kind)
852 /* We will issue error elsewhere. */
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,
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,
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. */
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,
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)
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,
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. */
947 resolve_common_blocks (gfc_symtree *common_root)
952 if (common_root == NULL)
955 if (common_root->left)
956 resolve_common_blocks (common_root->left);
957 if (common_root->right)
958 resolve_common_blocks (common_root->right);
960 resolve_common_vars (common_root->n.common->head, true);
962 /* The common name is a global name - in Fortran 2003 also if it has a
963 C binding name, since Fortran 2008 only the C binding name is a global
965 if (!common_root->n.common->binding_label
966 || gfc_notification_std (GFC_STD_F2008))
968 gsym = gfc_find_gsymbol (gfc_gsym_root,
969 common_root->n.common->name);
971 if (gsym && gfc_notification_std (GFC_STD_F2008)
972 && gsym->type == GSYM_COMMON
973 && ((common_root->n.common->binding_label
974 && (!gsym->binding_label
975 || strcmp (common_root->n.common->binding_label,
976 gsym->binding_label) != 0))
977 || (!common_root->n.common->binding_label
978 && gsym->binding_label)))
980 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
981 "identifier and must thus have the same binding name "
982 "as the same-named COMMON block at %L: %s vs %s",
983 common_root->n.common->name, &common_root->n.common->where,
985 common_root->n.common->binding_label
986 ? common_root->n.common->binding_label : "(blank)",
987 gsym->binding_label ? gsym->binding_label : "(blank)");
991 if (gsym && gsym->type != GSYM_COMMON
992 && !common_root->n.common->binding_label)
994 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
996 common_root->n.common->name, &common_root->n.common->where,
1000 if (gsym && gsym->type != GSYM_COMMON)
1002 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
1003 "%L sharing the identifier with global non-COMMON-block "
1004 "entity at %L", common_root->n.common->name,
1005 &common_root->n.common->where, &gsym->where);
1010 gsym = gfc_get_gsymbol (common_root->n.common->name);
1011 gsym->type = GSYM_COMMON;
1012 gsym->where = common_root->n.common->where;
1018 if (common_root->n.common->binding_label)
1020 gsym = gfc_find_gsymbol (gfc_gsym_root,
1021 common_root->n.common->binding_label);
1022 if (gsym && gsym->type != GSYM_COMMON)
1024 gfc_error ("COMMON block at %L with binding label %s uses the same "
1025 "global identifier as entity at %L",
1026 &common_root->n.common->where,
1027 common_root->n.common->binding_label, &gsym->where);
1032 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1033 gsym->type = GSYM_COMMON;
1034 gsym->where = common_root->n.common->where;
1040 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1044 if (sym->attr.flavor == FL_PARAMETER)
1045 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1046 sym->name, &common_root->n.common->where, &sym->declared_at);
1048 if (sym->attr.external)
1049 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1050 sym->name, &common_root->n.common->where);
1052 if (sym->attr.intrinsic)
1053 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1054 sym->name, &common_root->n.common->where);
1055 else if (sym->attr.result
1056 || gfc_is_function_return_value (sym, gfc_current_ns))
1057 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1058 "that is also a function result", sym->name,
1059 &common_root->n.common->where);
1060 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1061 && sym->attr.proc != PROC_ST_FUNCTION)
1062 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1063 "that is also a global procedure", sym->name,
1064 &common_root->n.common->where);
1068 /* Resolve contained function types. Because contained functions can call one
1069 another, they have to be worked out before any of the contained procedures
1072 The good news is that if a function doesn't already have a type, the only
1073 way it can get one is through an IMPLICIT type or a RESULT variable, because
1074 by definition contained functions are contained namespace they're contained
1075 in, not in a sibling or parent namespace. */
1078 resolve_contained_functions (gfc_namespace *ns)
1080 gfc_namespace *child;
1083 resolve_formal_arglists (ns);
1085 for (child = ns->contained; child; child = child->sibling)
1087 /* Resolve alternate entry points first. */
1088 resolve_entries (child);
1090 /* Then check function return types. */
1091 resolve_contained_fntype (child->proc_name, child);
1092 for (el = child->entries; el; el = el->next)
1093 resolve_contained_fntype (el->sym, child);
1098 static bool resolve_fl_derived0 (gfc_symbol *sym);
1101 /* Resolve all of the elements of a structure constructor and make sure that
1102 the types are correct. The 'init' flag indicates that the given
1103 constructor is an initializer. */
1106 resolve_structure_cons (gfc_expr *expr, int init)
1108 gfc_constructor *cons;
1109 gfc_component *comp;
1115 if (expr->ts.type == BT_DERIVED)
1116 resolve_fl_derived0 (expr->ts.u.derived);
1118 cons = gfc_constructor_first (expr->value.constructor);
1120 /* A constructor may have references if it is the result of substituting a
1121 parameter variable. In this case we just pull out the component we
1124 comp = expr->ref->u.c.sym->components;
1126 comp = expr->ts.u.derived->components;
1128 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1135 if (!gfc_resolve_expr (cons->expr))
1141 rank = comp->as ? comp->as->rank : 0;
1142 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1143 && (comp->attr.allocatable || cons->expr->rank))
1145 gfc_error ("The rank of the element in the structure "
1146 "constructor at %L does not match that of the "
1147 "component (%d/%d)", &cons->expr->where,
1148 cons->expr->rank, rank);
1152 /* If we don't have the right type, try to convert it. */
1154 if (!comp->attr.proc_pointer &&
1155 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1157 if (strcmp (comp->name, "_extends") == 0)
1159 /* Can afford to be brutal with the _extends initializer.
1160 The derived type can get lost because it is PRIVATE
1161 but it is not usage constrained by the standard. */
1162 cons->expr->ts = comp->ts;
1164 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1166 gfc_error ("The element in the structure constructor at %L, "
1167 "for pointer component '%s', is %s but should be %s",
1168 &cons->expr->where, comp->name,
1169 gfc_basic_typename (cons->expr->ts.type),
1170 gfc_basic_typename (comp->ts.type));
1175 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1181 /* For strings, the length of the constructor should be the same as
1182 the one of the structure, ensure this if the lengths are known at
1183 compile time and when we are dealing with PARAMETER or structure
1185 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1186 && comp->ts.u.cl->length
1187 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1188 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1189 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1190 && cons->expr->rank != 0
1191 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1192 comp->ts.u.cl->length->value.integer) != 0)
1194 if (cons->expr->expr_type == EXPR_VARIABLE
1195 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1197 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1198 to make use of the gfc_resolve_character_array_constructor
1199 machinery. The expression is later simplified away to
1200 an array of string literals. */
1201 gfc_expr *para = cons->expr;
1202 cons->expr = gfc_get_expr ();
1203 cons->expr->ts = para->ts;
1204 cons->expr->where = para->where;
1205 cons->expr->expr_type = EXPR_ARRAY;
1206 cons->expr->rank = para->rank;
1207 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1208 gfc_constructor_append_expr (&cons->expr->value.constructor,
1209 para, &cons->expr->where);
1211 if (cons->expr->expr_type == EXPR_ARRAY)
1214 p = gfc_constructor_first (cons->expr->value.constructor);
1215 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1217 gfc_charlen *cl, *cl2;
1220 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1222 if (cl == cons->expr->ts.u.cl)
1230 cl2->next = cl->next;
1232 gfc_free_expr (cl->length);
1236 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1237 cons->expr->ts.u.cl->length_from_typespec = true;
1238 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1239 gfc_resolve_character_array_constructor (cons->expr);
1243 if (cons->expr->expr_type == EXPR_NULL
1244 && !(comp->attr.pointer || comp->attr.allocatable
1245 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1246 || (comp->ts.type == BT_CLASS
1247 && (CLASS_DATA (comp)->attr.class_pointer
1248 || CLASS_DATA (comp)->attr.allocatable))))
1251 gfc_error ("The NULL in the structure constructor at %L is "
1252 "being applied to component '%s', which is neither "
1253 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1257 if (comp->attr.proc_pointer && comp->ts.interface)
1259 /* Check procedure pointer interface. */
1260 gfc_symbol *s2 = NULL;
1265 c2 = gfc_get_proc_ptr_comp (cons->expr);
1268 s2 = c2->ts.interface;
1271 else if (cons->expr->expr_type == EXPR_FUNCTION)
1273 s2 = cons->expr->symtree->n.sym->result;
1274 name = cons->expr->symtree->n.sym->result->name;
1276 else if (cons->expr->expr_type != EXPR_NULL)
1278 s2 = cons->expr->symtree->n.sym;
1279 name = cons->expr->symtree->n.sym->name;
1282 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1283 err, sizeof (err), NULL, NULL))
1285 gfc_error ("Interface mismatch for procedure-pointer component "
1286 "'%s' in structure constructor at %L: %s",
1287 comp->name, &cons->expr->where, err);
1292 if (!comp->attr.pointer || comp->attr.proc_pointer
1293 || cons->expr->expr_type == EXPR_NULL)
1296 a = gfc_expr_attr (cons->expr);
1298 if (!a.pointer && !a.target)
1301 gfc_error ("The element in the structure constructor at %L, "
1302 "for pointer component '%s' should be a POINTER or "
1303 "a TARGET", &cons->expr->where, comp->name);
1308 /* F08:C461. Additional checks for pointer initialization. */
1312 gfc_error ("Pointer initialization target at %L "
1313 "must not be ALLOCATABLE ", &cons->expr->where);
1318 gfc_error ("Pointer initialization target at %L "
1319 "must have the SAVE attribute", &cons->expr->where);
1323 /* F2003, C1272 (3). */
1324 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1325 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1326 || gfc_is_coindexed (cons->expr)))
1329 gfc_error ("Invalid expression in the structure constructor for "
1330 "pointer component '%s' at %L in PURE procedure",
1331 comp->name, &cons->expr->where);
1334 if (gfc_implicit_pure (NULL)
1335 && cons->expr->expr_type == EXPR_VARIABLE
1336 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1337 || gfc_is_coindexed (cons->expr)))
1338 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1346 /****************** Expression name resolution ******************/
1348 /* Returns 0 if a symbol was not declared with a type or
1349 attribute declaration statement, nonzero otherwise. */
1352 was_declared (gfc_symbol *sym)
1358 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1361 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1362 || a.optional || a.pointer || a.save || a.target || a.volatile_
1363 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1364 || a.asynchronous || a.codimension)
1371 /* Determine if a symbol is generic or not. */
1374 generic_sym (gfc_symbol *sym)
1378 if (sym->attr.generic ||
1379 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1382 if (was_declared (sym) || sym->ns->parent == NULL)
1385 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1392 return generic_sym (s);
1399 /* Determine if a symbol is specific or not. */
1402 specific_sym (gfc_symbol *sym)
1406 if (sym->attr.if_source == IFSRC_IFBODY
1407 || sym->attr.proc == PROC_MODULE
1408 || sym->attr.proc == PROC_INTERNAL
1409 || sym->attr.proc == PROC_ST_FUNCTION
1410 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1411 || sym->attr.external)
1414 if (was_declared (sym) || sym->ns->parent == NULL)
1417 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1419 return (s == NULL) ? 0 : specific_sym (s);
1423 /* Figure out if the procedure is specific, generic or unknown. */
1426 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1430 procedure_kind (gfc_symbol *sym)
1432 if (generic_sym (sym))
1433 return PTYPE_GENERIC;
1435 if (specific_sym (sym))
1436 return PTYPE_SPECIFIC;
1438 return PTYPE_UNKNOWN;
1441 /* Check references to assumed size arrays. The flag need_full_assumed_size
1442 is nonzero when matching actual arguments. */
1444 static int need_full_assumed_size = 0;
1447 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1449 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1452 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1453 What should it be? */
1454 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1455 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1456 && (e->ref->u.ar.type == AR_FULL))
1458 gfc_error ("The upper bound in the last dimension must "
1459 "appear in the reference to the assumed size "
1460 "array '%s' at %L", sym->name, &e->where);
1467 /* Look for bad assumed size array references in argument expressions
1468 of elemental and array valued intrinsic procedures. Since this is
1469 called from procedure resolution functions, it only recurses at
1473 resolve_assumed_size_actual (gfc_expr *e)
1478 switch (e->expr_type)
1481 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1486 if (resolve_assumed_size_actual (e->value.op.op1)
1487 || resolve_assumed_size_actual (e->value.op.op2))
1498 /* Check a generic procedure, passed as an actual argument, to see if
1499 there is a matching specific name. If none, it is an error, and if
1500 more than one, the reference is ambiguous. */
1502 count_specific_procs (gfc_expr *e)
1509 sym = e->symtree->n.sym;
1511 for (p = sym->generic; p; p = p->next)
1512 if (strcmp (sym->name, p->sym->name) == 0)
1514 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1520 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1524 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1525 "argument at %L", sym->name, &e->where);
1531 /* See if a call to sym could possibly be a not allowed RECURSION because of
1532 a missing RECURSIVE declaration. This means that either sym is the current
1533 context itself, or sym is the parent of a contained procedure calling its
1534 non-RECURSIVE containing procedure.
1535 This also works if sym is an ENTRY. */
1538 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1540 gfc_symbol* proc_sym;
1541 gfc_symbol* context_proc;
1542 gfc_namespace* real_context;
1544 if (sym->attr.flavor == FL_PROGRAM
1545 || sym->attr.flavor == FL_DERIVED)
1548 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1550 /* If we've got an ENTRY, find real procedure. */
1551 if (sym->attr.entry && sym->ns->entries)
1552 proc_sym = sym->ns->entries->sym;
1556 /* If sym is RECURSIVE, all is well of course. */
1557 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1560 /* Find the context procedure's "real" symbol if it has entries.
1561 We look for a procedure symbol, so recurse on the parents if we don't
1562 find one (like in case of a BLOCK construct). */
1563 for (real_context = context; ; real_context = real_context->parent)
1565 /* We should find something, eventually! */
1566 gcc_assert (real_context);
1568 context_proc = (real_context->entries ? real_context->entries->sym
1569 : real_context->proc_name);
1571 /* In some special cases, there may not be a proc_name, like for this
1573 real(bad_kind()) function foo () ...
1574 when checking the call to bad_kind ().
1575 In these cases, we simply return here and assume that the
1580 if (context_proc->attr.flavor != FL_LABEL)
1584 /* A call from sym's body to itself is recursion, of course. */
1585 if (context_proc == proc_sym)
1588 /* The same is true if context is a contained procedure and sym the
1590 if (context_proc->attr.contained)
1592 gfc_symbol* parent_proc;
1594 gcc_assert (context->parent);
1595 parent_proc = (context->parent->entries ? context->parent->entries->sym
1596 : context->parent->proc_name);
1598 if (parent_proc == proc_sym)
1606 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1607 its typespec and formal argument list. */
1610 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1612 gfc_intrinsic_sym* isym = NULL;
1618 /* Already resolved. */
1619 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1622 /* We already know this one is an intrinsic, so we don't call
1623 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1624 gfc_find_subroutine directly to check whether it is a function or
1627 if (sym->intmod_sym_id && sym->attr.subroutine)
1629 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1630 isym = gfc_intrinsic_subroutine_by_id (id);
1632 else if (sym->intmod_sym_id)
1634 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1635 isym = gfc_intrinsic_function_by_id (id);
1637 else if (!sym->attr.subroutine)
1638 isym = gfc_find_function (sym->name);
1640 if (isym && !sym->attr.subroutine)
1642 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1643 && !sym->attr.implicit_type)
1644 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1645 " ignored", sym->name, &sym->declared_at);
1647 if (!sym->attr.function &&
1648 !gfc_add_function(&sym->attr, sym->name, loc))
1653 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1655 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1657 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1658 " specifier", sym->name, &sym->declared_at);
1662 if (!sym->attr.subroutine &&
1663 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1668 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1673 gfc_copy_formal_args_intr (sym, isym);
1675 /* Check it is actually available in the standard settings. */
1676 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1678 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1679 " available in the current standard settings but %s. Use"
1680 " an appropriate -std=* option or enable -fall-intrinsics"
1681 " in order to use it.",
1682 sym->name, &sym->declared_at, symstd);
1690 /* Resolve a procedure expression, like passing it to a called procedure or as
1691 RHS for a procedure pointer assignment. */
1694 resolve_procedure_expression (gfc_expr* expr)
1698 if (expr->expr_type != EXPR_VARIABLE)
1700 gcc_assert (expr->symtree);
1702 sym = expr->symtree->n.sym;
1704 if (sym->attr.intrinsic)
1705 gfc_resolve_intrinsic (sym, &expr->where);
1707 if (sym->attr.flavor != FL_PROCEDURE
1708 || (sym->attr.function && sym->result == sym))
1711 /* A non-RECURSIVE procedure that is used as procedure expression within its
1712 own body is in danger of being called recursively. */
1713 if (is_illegal_recursion (sym, gfc_current_ns))
1714 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1715 " itself recursively. Declare it RECURSIVE or use"
1716 " -frecursive", sym->name, &expr->where);
1722 /* Resolve an actual argument list. Most of the time, this is just
1723 resolving the expressions in the list.
1724 The exception is that we sometimes have to decide whether arguments
1725 that look like procedure arguments are really simple variable
1729 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1730 bool no_formal_args)
1733 gfc_symtree *parent_st;
1735 int save_need_full_assumed_size;
1736 bool return_value = false;
1737 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1740 first_actual_arg = true;
1742 for (; arg; arg = arg->next)
1747 /* Check the label is a valid branching target. */
1750 if (arg->label->defined == ST_LABEL_UNKNOWN)
1752 gfc_error ("Label %d referenced at %L is never defined",
1753 arg->label->value, &arg->label->where);
1757 first_actual_arg = false;
1761 if (e->expr_type == EXPR_VARIABLE
1762 && e->symtree->n.sym->attr.generic
1764 && count_specific_procs (e) != 1)
1767 if (e->ts.type != BT_PROCEDURE)
1769 save_need_full_assumed_size = need_full_assumed_size;
1770 if (e->expr_type != EXPR_VARIABLE)
1771 need_full_assumed_size = 0;
1772 if (!gfc_resolve_expr (e))
1774 need_full_assumed_size = save_need_full_assumed_size;
1778 /* See if the expression node should really be a variable reference. */
1780 sym = e->symtree->n.sym;
1782 if (sym->attr.flavor == FL_PROCEDURE
1783 || sym->attr.intrinsic
1784 || sym->attr.external)
1788 /* If a procedure is not already determined to be something else
1789 check if it is intrinsic. */
1790 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1791 sym->attr.intrinsic = 1;
1793 if (sym->attr.proc == PROC_ST_FUNCTION)
1795 gfc_error ("Statement function '%s' at %L is not allowed as an "
1796 "actual argument", sym->name, &e->where);
1799 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1800 sym->attr.subroutine);
1801 if (sym->attr.intrinsic && actual_ok == 0)
1803 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1804 "actual argument", sym->name, &e->where);
1807 if (sym->attr.contained && !sym->attr.use_assoc
1808 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1810 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1811 " used as actual argument at %L",
1812 sym->name, &e->where))
1816 if (sym->attr.elemental && !sym->attr.intrinsic)
1818 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1819 "allowed as an actual argument at %L", sym->name,
1823 /* Check if a generic interface has a specific procedure
1824 with the same name before emitting an error. */
1825 if (sym->attr.generic && count_specific_procs (e) != 1)
1828 /* Just in case a specific was found for the expression. */
1829 sym = e->symtree->n.sym;
1831 /* If the symbol is the function that names the current (or
1832 parent) scope, then we really have a variable reference. */
1834 if (gfc_is_function_return_value (sym, sym->ns))
1837 /* If all else fails, see if we have a specific intrinsic. */
1838 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1840 gfc_intrinsic_sym *isym;
1842 isym = gfc_find_function (sym->name);
1843 if (isym == NULL || !isym->specific)
1845 gfc_error ("Unable to find a specific INTRINSIC procedure "
1846 "for the reference '%s' at %L", sym->name,
1851 sym->attr.intrinsic = 1;
1852 sym->attr.function = 1;
1855 if (!gfc_resolve_expr (e))
1860 /* See if the name is a module procedure in a parent unit. */
1862 if (was_declared (sym) || sym->ns->parent == NULL)
1865 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1867 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1871 if (parent_st == NULL)
1874 sym = parent_st->n.sym;
1875 e->symtree = parent_st; /* Point to the right thing. */
1877 if (sym->attr.flavor == FL_PROCEDURE
1878 || sym->attr.intrinsic
1879 || sym->attr.external)
1881 if (!gfc_resolve_expr (e))
1887 e->expr_type = EXPR_VARIABLE;
1889 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1890 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1891 && CLASS_DATA (sym)->as))
1893 e->rank = sym->ts.type == BT_CLASS
1894 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1895 e->ref = gfc_get_ref ();
1896 e->ref->type = REF_ARRAY;
1897 e->ref->u.ar.type = AR_FULL;
1898 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1899 ? CLASS_DATA (sym)->as : sym->as;
1902 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1903 primary.c (match_actual_arg). If above code determines that it
1904 is a variable instead, it needs to be resolved as it was not
1905 done at the beginning of this function. */
1906 save_need_full_assumed_size = need_full_assumed_size;
1907 if (e->expr_type != EXPR_VARIABLE)
1908 need_full_assumed_size = 0;
1909 if (!gfc_resolve_expr (e))
1911 need_full_assumed_size = save_need_full_assumed_size;
1914 /* Check argument list functions %VAL, %LOC and %REF. There is
1915 nothing to do for %REF. */
1916 if (arg->name && arg->name[0] == '%')
1918 if (strncmp ("%VAL", arg->name, 4) == 0)
1920 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1922 gfc_error ("By-value argument at %L is not of numeric "
1929 gfc_error ("By-value argument at %L cannot be an array or "
1930 "an array section", &e->where);
1934 /* Intrinsics are still PROC_UNKNOWN here. However,
1935 since same file external procedures are not resolvable
1936 in gfortran, it is a good deal easier to leave them to
1938 if (ptype != PROC_UNKNOWN
1939 && ptype != PROC_DUMMY
1940 && ptype != PROC_EXTERNAL
1941 && ptype != PROC_MODULE)
1943 gfc_error ("By-value argument at %L is not allowed "
1944 "in this context", &e->where);
1949 /* Statement functions have already been excluded above. */
1950 else if (strncmp ("%LOC", arg->name, 4) == 0
1951 && e->ts.type == BT_PROCEDURE)
1953 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1955 gfc_error ("Passing internal procedure at %L by location "
1956 "not allowed", &e->where);
1962 /* Fortran 2008, C1237. */
1963 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1964 && gfc_has_ultimate_pointer (e))
1966 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1967 "component", &e->where);
1971 first_actual_arg = false;
1974 return_value = true;
1977 actual_arg = actual_arg_sav;
1978 first_actual_arg = first_actual_arg_sav;
1980 return return_value;
1984 /* Do the checks of the actual argument list that are specific to elemental
1985 procedures. If called with c == NULL, we have a function, otherwise if
1986 expr == NULL, we have a subroutine. */
1989 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1991 gfc_actual_arglist *arg0;
1992 gfc_actual_arglist *arg;
1993 gfc_symbol *esym = NULL;
1994 gfc_intrinsic_sym *isym = NULL;
1996 gfc_intrinsic_arg *iformal = NULL;
1997 gfc_formal_arglist *eformal = NULL;
1998 bool formal_optional = false;
1999 bool set_by_optional = false;
2003 /* Is this an elemental procedure? */
2004 if (expr && expr->value.function.actual != NULL)
2006 if (expr->value.function.esym != NULL
2007 && expr->value.function.esym->attr.elemental)
2009 arg0 = expr->value.function.actual;
2010 esym = expr->value.function.esym;
2012 else if (expr->value.function.isym != NULL
2013 && expr->value.function.isym->elemental)
2015 arg0 = expr->value.function.actual;
2016 isym = expr->value.function.isym;
2021 else if (c && c->ext.actual != NULL)
2023 arg0 = c->ext.actual;
2025 if (c->resolved_sym)
2026 esym = c->resolved_sym;
2028 esym = c->symtree->n.sym;
2031 if (!esym->attr.elemental)
2037 /* The rank of an elemental is the rank of its array argument(s). */
2038 for (arg = arg0; arg; arg = arg->next)
2040 if (arg->expr != NULL && arg->expr->rank != 0)
2042 rank = arg->expr->rank;
2043 if (arg->expr->expr_type == EXPR_VARIABLE
2044 && arg->expr->symtree->n.sym->attr.optional)
2045 set_by_optional = true;
2047 /* Function specific; set the result rank and shape. */
2051 if (!expr->shape && arg->expr->shape)
2053 expr->shape = gfc_get_shape (rank);
2054 for (i = 0; i < rank; i++)
2055 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2062 /* If it is an array, it shall not be supplied as an actual argument
2063 to an elemental procedure unless an array of the same rank is supplied
2064 as an actual argument corresponding to a nonoptional dummy argument of
2065 that elemental procedure(12.4.1.5). */
2066 formal_optional = false;
2068 iformal = isym->formal;
2070 eformal = esym->formal;
2072 for (arg = arg0; arg; arg = arg->next)
2076 if (eformal->sym && eformal->sym->attr.optional)
2077 formal_optional = true;
2078 eformal = eformal->next;
2080 else if (isym && iformal)
2082 if (iformal->optional)
2083 formal_optional = true;
2084 iformal = iformal->next;
2087 formal_optional = true;
2089 if (pedantic && arg->expr != NULL
2090 && arg->expr->expr_type == EXPR_VARIABLE
2091 && arg->expr->symtree->n.sym->attr.optional
2094 && (set_by_optional || arg->expr->rank != rank)
2095 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2097 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2098 "MISSING, it cannot be the actual argument of an "
2099 "ELEMENTAL procedure unless there is a non-optional "
2100 "argument with the same rank (12.4.1.5)",
2101 arg->expr->symtree->n.sym->name, &arg->expr->where);
2105 for (arg = arg0; arg; arg = arg->next)
2107 if (arg->expr == NULL || arg->expr->rank == 0)
2110 /* Being elemental, the last upper bound of an assumed size array
2111 argument must be present. */
2112 if (resolve_assumed_size_actual (arg->expr))
2115 /* Elemental procedure's array actual arguments must conform. */
2118 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2125 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2126 is an array, the intent inout/out variable needs to be also an array. */
2127 if (rank > 0 && esym && expr == NULL)
2128 for (eformal = esym->formal, arg = arg0; arg && eformal;
2129 arg = arg->next, eformal = eformal->next)
2130 if ((eformal->sym->attr.intent == INTENT_OUT
2131 || eformal->sym->attr.intent == INTENT_INOUT)
2132 && arg->expr && arg->expr->rank == 0)
2134 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2135 "ELEMENTAL subroutine '%s' is a scalar, but another "
2136 "actual argument is an array", &arg->expr->where,
2137 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2138 : "INOUT", eformal->sym->name, esym->name);
2145 /* This function does the checking of references to global procedures
2146 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2147 77 and 95 standards. It checks for a gsymbol for the name, making
2148 one if it does not already exist. If it already exists, then the
2149 reference being resolved must correspond to the type of gsymbol.
2150 Otherwise, the new symbol is equipped with the attributes of the
2151 reference. The corresponding code that is called in creating
2152 global entities is parse.c.
2154 In addition, for all but -std=legacy, the gsymbols are used to
2155 check the interfaces of external procedures from the same file.
2156 The namespace of the gsymbol is resolved and then, once this is
2157 done the interface is checked. */
2161 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2163 if (!gsym_ns->proc_name->attr.recursive)
2166 if (sym->ns == gsym_ns)
2169 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2176 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2178 if (gsym_ns->entries)
2180 gfc_entry_list *entry = gsym_ns->entries;
2182 for (; entry; entry = entry->next)
2184 if (strcmp (sym->name, entry->sym->name) == 0)
2186 if (strcmp (gsym_ns->proc_name->name,
2187 sym->ns->proc_name->name) == 0)
2191 && strcmp (gsym_ns->proc_name->name,
2192 sym->ns->parent->proc_name->name) == 0)
2201 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2204 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2206 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2208 for ( ; arg; arg = arg->next)
2213 if (arg->sym->attr.allocatable) /* (2a) */
2215 strncpy (errmsg, _("allocatable argument"), err_len);
2218 else if (arg->sym->attr.asynchronous)
2220 strncpy (errmsg, _("asynchronous argument"), err_len);
2223 else if (arg->sym->attr.optional)
2225 strncpy (errmsg, _("optional argument"), err_len);
2228 else if (arg->sym->attr.pointer)
2230 strncpy (errmsg, _("pointer argument"), err_len);
2233 else if (arg->sym->attr.target)
2235 strncpy (errmsg, _("target argument"), err_len);
2238 else if (arg->sym->attr.value)
2240 strncpy (errmsg, _("value argument"), err_len);
2243 else if (arg->sym->attr.volatile_)
2245 strncpy (errmsg, _("volatile argument"), err_len);
2248 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2250 strncpy (errmsg, _("assumed-shape argument"), err_len);
2253 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2255 strncpy (errmsg, _("assumed-rank argument"), err_len);
2258 else if (arg->sym->attr.codimension) /* (2c) */
2260 strncpy (errmsg, _("coarray argument"), err_len);
2263 else if (false) /* (2d) TODO: parametrized derived type */
2265 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2268 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2270 strncpy (errmsg, _("polymorphic argument"), err_len);
2273 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2275 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2278 else if (arg->sym->ts.type == BT_ASSUMED)
2280 /* As assumed-type is unlimited polymorphic (cf. above).
2281 See also TS 29113, Note 6.1. */
2282 strncpy (errmsg, _("assumed-type argument"), err_len);
2287 if (sym->attr.function)
2289 gfc_symbol *res = sym->result ? sym->result : sym;
2291 if (res->attr.dimension) /* (3a) */
2293 strncpy (errmsg, _("array result"), err_len);
2296 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2298 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2301 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2302 && res->ts.u.cl->length
2303 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2305 strncpy (errmsg, _("result with non-constant character length"), err_len);
2310 if (sym->attr.elemental) /* (4) */
2312 strncpy (errmsg, _("elemental procedure"), err_len);
2315 else if (sym->attr.is_bind_c) /* (5) */
2317 strncpy (errmsg, _("bind(c) procedure"), err_len);
2326 resolve_global_procedure (gfc_symbol *sym, locus *where,
2327 gfc_actual_arglist **actual, int sub)
2331 enum gfc_symbol_type type;
2334 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2336 gsym = gfc_get_gsymbol (sym->name);
2338 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2339 gfc_global_used (gsym, where);
2341 if ((sym->attr.if_source == IFSRC_UNKNOWN
2342 || sym->attr.if_source == IFSRC_IFBODY)
2343 && gsym->type != GSYM_UNKNOWN
2345 && gsym->ns->resolved != -1
2346 && gsym->ns->proc_name
2347 && not_in_recursive (sym, gsym->ns)
2348 && not_entry_self_reference (sym, gsym->ns))
2350 gfc_symbol *def_sym;
2352 /* Resolve the gsymbol namespace if needed. */
2353 if (!gsym->ns->resolved)
2355 gfc_dt_list *old_dt_list;
2356 struct gfc_omp_saved_state old_omp_state;
2358 /* Stash away derived types so that the backend_decls do not
2360 old_dt_list = gfc_derived_types;
2361 gfc_derived_types = NULL;
2362 /* And stash away openmp state. */
2363 gfc_omp_save_and_clear_state (&old_omp_state);
2365 gfc_resolve (gsym->ns);
2367 /* Store the new derived types with the global namespace. */
2368 if (gfc_derived_types)
2369 gsym->ns->derived_types = gfc_derived_types;
2371 /* Restore the derived types of this namespace. */
2372 gfc_derived_types = old_dt_list;
2373 /* And openmp state. */
2374 gfc_omp_restore_state (&old_omp_state);
2377 /* Make sure that translation for the gsymbol occurs before
2378 the procedure currently being resolved. */
2379 ns = gfc_global_ns_list;
2380 for (; ns && ns != gsym->ns; ns = ns->sibling)
2382 if (ns->sibling == gsym->ns)
2384 ns->sibling = gsym->ns->sibling;
2385 gsym->ns->sibling = gfc_global_ns_list;
2386 gfc_global_ns_list = gsym->ns;
2391 def_sym = gsym->ns->proc_name;
2392 if (def_sym->attr.entry_master)
2394 gfc_entry_list *entry;
2395 for (entry = gsym->ns->entries; entry; entry = entry->next)
2396 if (strcmp (entry->sym->name, sym->name) == 0)
2398 def_sym = entry->sym;
2403 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2405 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2406 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2407 gfc_typename (&def_sym->ts));
2411 if (sym->attr.if_source == IFSRC_UNKNOWN
2412 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2414 gfc_error ("Explicit interface required for '%s' at %L: %s",
2415 sym->name, &sym->declared_at, reason);
2419 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2420 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2421 gfc_errors_to_warnings (1);
2423 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2424 reason, sizeof(reason), NULL, NULL))
2426 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2427 sym->name, &sym->declared_at, reason);
2432 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2433 && !(gfc_option.warn_std & GFC_STD_GNU)))
2434 gfc_errors_to_warnings (1);
2436 if (sym->attr.if_source != IFSRC_IFBODY)
2437 gfc_procedure_use (def_sym, actual, where);
2441 gfc_errors_to_warnings (0);
2443 if (gsym->type == GSYM_UNKNOWN)
2446 gsym->where = *where;
2453 /************* Function resolution *************/
2455 /* Resolve a function call known to be generic.
2456 Section 14.1.2.4.1. */
2459 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2463 if (sym->attr.generic)
2465 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2468 expr->value.function.name = s->name;
2469 expr->value.function.esym = s;
2471 if (s->ts.type != BT_UNKNOWN)
2473 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2474 expr->ts = s->result->ts;
2477 expr->rank = s->as->rank;
2478 else if (s->result != NULL && s->result->as != NULL)
2479 expr->rank = s->result->as->rank;
2481 gfc_set_sym_referenced (expr->value.function.esym);
2486 /* TODO: Need to search for elemental references in generic
2490 if (sym->attr.intrinsic)
2491 return gfc_intrinsic_func_interface (expr, 0);
2498 resolve_generic_f (gfc_expr *expr)
2502 gfc_interface *intr = NULL;
2504 sym = expr->symtree->n.sym;
2508 m = resolve_generic_f0 (expr, sym);
2511 else if (m == MATCH_ERROR)
2516 for (intr = sym->generic; intr; intr = intr->next)
2517 if (intr->sym->attr.flavor == FL_DERIVED)
2520 if (sym->ns->parent == NULL)
2522 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2526 if (!generic_sym (sym))
2530 /* Last ditch attempt. See if the reference is to an intrinsic
2531 that possesses a matching interface. 14.1.2.4 */
2532 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2534 gfc_error ("There is no specific function for the generic '%s' "
2535 "at %L", expr->symtree->n.sym->name, &expr->where);
2541 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2544 return resolve_structure_cons (expr, 0);
2547 m = gfc_intrinsic_func_interface (expr, 0);
2552 gfc_error ("Generic function '%s' at %L is not consistent with a "
2553 "specific intrinsic interface", expr->symtree->n.sym->name,
2560 /* Resolve a function call known to be specific. */
2563 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2567 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2569 if (sym->attr.dummy)
2571 sym->attr.proc = PROC_DUMMY;
2575 sym->attr.proc = PROC_EXTERNAL;
2579 if (sym->attr.proc == PROC_MODULE
2580 || sym->attr.proc == PROC_ST_FUNCTION
2581 || sym->attr.proc == PROC_INTERNAL)
2584 if (sym->attr.intrinsic)
2586 m = gfc_intrinsic_func_interface (expr, 1);
2590 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2591 "with an intrinsic", sym->name, &expr->where);
2599 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2602 expr->ts = sym->result->ts;
2605 expr->value.function.name = sym->name;
2606 expr->value.function.esym = sym;
2607 if (sym->as != NULL)
2608 expr->rank = sym->as->rank;
2615 resolve_specific_f (gfc_expr *expr)
2620 sym = expr->symtree->n.sym;
2624 m = resolve_specific_f0 (sym, expr);
2627 if (m == MATCH_ERROR)
2630 if (sym->ns->parent == NULL)
2633 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2639 gfc_error ("Unable to resolve the specific function '%s' at %L",
2640 expr->symtree->n.sym->name, &expr->where);
2646 /* Resolve a procedure call not known to be generic nor specific. */
2649 resolve_unknown_f (gfc_expr *expr)
2654 sym = expr->symtree->n.sym;
2656 if (sym->attr.dummy)
2658 sym->attr.proc = PROC_DUMMY;
2659 expr->value.function.name = sym->name;
2663 /* See if we have an intrinsic function reference. */
2665 if (gfc_is_intrinsic (sym, 0, expr->where))
2667 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2672 /* The reference is to an external name. */
2674 sym->attr.proc = PROC_EXTERNAL;
2675 expr->value.function.name = sym->name;
2676 expr->value.function.esym = expr->symtree->n.sym;
2678 if (sym->as != NULL)
2679 expr->rank = sym->as->rank;
2681 /* Type of the expression is either the type of the symbol or the
2682 default type of the symbol. */
2685 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2687 if (sym->ts.type != BT_UNKNOWN)
2691 ts = gfc_get_default_type (sym->name, sym->ns);
2693 if (ts->type == BT_UNKNOWN)
2695 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2696 sym->name, &expr->where);
2707 /* Return true, if the symbol is an external procedure. */
2709 is_external_proc (gfc_symbol *sym)
2711 if (!sym->attr.dummy && !sym->attr.contained
2712 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2713 && sym->attr.proc != PROC_ST_FUNCTION
2714 && !sym->attr.proc_pointer
2715 && !sym->attr.use_assoc
2723 /* Figure out if a function reference is pure or not. Also set the name
2724 of the function for a potential error message. Return nonzero if the
2725 function is PURE, zero if not. */
2727 pure_stmt_function (gfc_expr *, gfc_symbol *);
2730 pure_function (gfc_expr *e, const char **name)
2736 if (e->symtree != NULL
2737 && e->symtree->n.sym != NULL
2738 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2739 return pure_stmt_function (e, e->symtree->n.sym);
2741 if (e->value.function.esym)
2743 pure = gfc_pure (e->value.function.esym);
2744 *name = e->value.function.esym->name;
2746 else if (e->value.function.isym)
2748 pure = e->value.function.isym->pure
2749 || e->value.function.isym->elemental;
2750 *name = e->value.function.isym->name;
2754 /* Implicit functions are not pure. */
2756 *name = e->value.function.name;
2764 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2765 int *f ATTRIBUTE_UNUSED)
2769 /* Don't bother recursing into other statement functions
2770 since they will be checked individually for purity. */
2771 if (e->expr_type != EXPR_FUNCTION
2773 || e->symtree->n.sym == sym
2774 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2777 return pure_function (e, &name) ? false : true;
2782 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2784 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2788 /* Resolve a function call, which means resolving the arguments, then figuring
2789 out which entity the name refers to. */
2792 resolve_function (gfc_expr *expr)
2794 gfc_actual_arglist *arg;
2799 procedure_type p = PROC_INTRINSIC;
2800 bool no_formal_args;
2804 sym = expr->symtree->n.sym;
2806 /* If this is a procedure pointer component, it has already been resolved. */
2807 if (gfc_is_proc_ptr_comp (expr))
2810 if (sym && sym->attr.intrinsic
2811 && !gfc_resolve_intrinsic (sym, &expr->where))
2814 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2816 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2820 /* If this ia a deferred TBP with an abstract interface (which may
2821 of course be referenced), expr->value.function.esym will be set. */
2822 if (sym && sym->attr.abstract && !expr->value.function.esym)
2824 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2825 sym->name, &expr->where);
2829 /* Switch off assumed size checking and do this again for certain kinds
2830 of procedure, once the procedure itself is resolved. */
2831 need_full_assumed_size++;
2833 if (expr->symtree && expr->symtree->n.sym)
2834 p = expr->symtree->n.sym->attr.proc;
2836 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2837 inquiry_argument = true;
2838 no_formal_args = sym && is_external_proc (sym)
2839 && gfc_sym_get_dummy_args (sym) == NULL;
2841 if (!resolve_actual_arglist (expr->value.function.actual,
2844 inquiry_argument = false;
2848 inquiry_argument = false;
2850 /* Resume assumed_size checking. */
2851 need_full_assumed_size--;
2853 /* If the procedure is external, check for usage. */
2854 if (sym && is_external_proc (sym))
2855 resolve_global_procedure (sym, &expr->where,
2856 &expr->value.function.actual, 0);
2858 if (sym && sym->ts.type == BT_CHARACTER
2860 && sym->ts.u.cl->length == NULL
2862 && !sym->ts.deferred
2863 && expr->value.function.esym == NULL
2864 && !sym->attr.contained)
2866 /* Internal procedures are taken care of in resolve_contained_fntype. */
2867 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2868 "be used at %L since it is not a dummy argument",
2869 sym->name, &expr->where);
2873 /* See if function is already resolved. */
2875 if (expr->value.function.name != NULL)
2877 if (expr->ts.type == BT_UNKNOWN)
2883 /* Apply the rules of section 14.1.2. */
2885 switch (procedure_kind (sym))
2888 t = resolve_generic_f (expr);
2891 case PTYPE_SPECIFIC:
2892 t = resolve_specific_f (expr);
2896 t = resolve_unknown_f (expr);
2900 gfc_internal_error ("resolve_function(): bad function type");
2904 /* If the expression is still a function (it might have simplified),
2905 then we check to see if we are calling an elemental function. */
2907 if (expr->expr_type != EXPR_FUNCTION)
2910 temp = need_full_assumed_size;
2911 need_full_assumed_size = 0;
2913 if (!resolve_elemental_actual (expr, NULL))
2916 if (omp_workshare_flag
2917 && expr->value.function.esym
2918 && ! gfc_elemental (expr->value.function.esym))
2920 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2921 "in WORKSHARE construct", expr->value.function.esym->name,
2926 #define GENERIC_ID expr->value.function.isym->id
2927 else if (expr->value.function.actual != NULL
2928 && expr->value.function.isym != NULL
2929 && GENERIC_ID != GFC_ISYM_LBOUND
2930 && GENERIC_ID != GFC_ISYM_LEN
2931 && GENERIC_ID != GFC_ISYM_LOC
2932 && GENERIC_ID != GFC_ISYM_C_LOC
2933 && GENERIC_ID != GFC_ISYM_PRESENT)
2935 /* Array intrinsics must also have the last upper bound of an
2936 assumed size array argument. UBOUND and SIZE have to be
2937 excluded from the check if the second argument is anything
2940 for (arg = expr->value.function.actual; arg; arg = arg->next)
2942 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2943 && arg == expr->value.function.actual
2944 && arg->next != NULL && arg->next->expr)
2946 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2949 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2952 if ((int)mpz_get_si (arg->next->expr->value.integer)
2957 if (arg->expr != NULL
2958 && arg->expr->rank > 0
2959 && resolve_assumed_size_actual (arg->expr))
2965 need_full_assumed_size = temp;
2968 if (!pure_function (expr, &name) && name)
2972 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2973 "FORALL %s", name, &expr->where,
2974 forall_flag == 2 ? "mask" : "block");
2977 else if (do_concurrent_flag)
2979 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2980 "DO CONCURRENT %s", name, &expr->where,
2981 do_concurrent_flag == 2 ? "mask" : "block");
2984 else if (gfc_pure (NULL))
2986 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2987 "procedure within a PURE procedure", name, &expr->where);
2991 if (gfc_implicit_pure (NULL))
2992 gfc_current_ns->proc_name->attr.implicit_pure = 0;
2995 /* Functions without the RECURSIVE attribution are not allowed to
2996 * call themselves. */
2997 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3000 esym = expr->value.function.esym;
3002 if (is_illegal_recursion (esym, gfc_current_ns))
3004 if (esym->attr.entry && esym->ns->entries)
3005 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3006 " function '%s' is not RECURSIVE",
3007 esym->name, &expr->where, esym->ns->entries->sym->name);
3009 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3010 " is not RECURSIVE", esym->name, &expr->where);
3016 /* Character lengths of use associated functions may contains references to
3017 symbols not referenced from the current program unit otherwise. Make sure
3018 those symbols are marked as referenced. */
3020 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3021 && expr->value.function.esym->attr.use_assoc)
3023 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3026 /* Make sure that the expression has a typespec that works. */
3027 if (expr->ts.type == BT_UNKNOWN)
3029 if (expr->symtree->n.sym->result
3030 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3031 && !expr->symtree->n.sym->result->attr.proc_pointer)
3032 expr->ts = expr->symtree->n.sym->result->ts;
3039 /************* Subroutine resolution *************/
3042 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3048 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3049 sym->name, &c->loc);
3050 else if (do_concurrent_flag)
3051 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3052 "PURE", sym->name, &c->loc);
3053 else if (gfc_pure (NULL))
3054 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3057 if (gfc_implicit_pure (NULL))
3058 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3063 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3067 if (sym->attr.generic)
3069 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3072 c->resolved_sym = s;
3073 pure_subroutine (c, s);
3077 /* TODO: Need to search for elemental references in generic interface. */
3080 if (sym->attr.intrinsic)
3081 return gfc_intrinsic_sub_interface (c, 0);
3088 resolve_generic_s (gfc_code *c)
3093 sym = c->symtree->n.sym;
3097 m = resolve_generic_s0 (c, sym);
3100 else if (m == MATCH_ERROR)
3104 if (sym->ns->parent == NULL)
3106 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3110 if (!generic_sym (sym))
3114 /* Last ditch attempt. See if the reference is to an intrinsic
3115 that possesses a matching interface. 14.1.2.4 */
3116 sym = c->symtree->n.sym;
3118 if (!gfc_is_intrinsic (sym, 1, c->loc))
3120 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3121 sym->name, &c->loc);
3125 m = gfc_intrinsic_sub_interface (c, 0);
3129 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3130 "intrinsic subroutine interface", sym->name, &c->loc);
3136 /* Resolve a subroutine call known to be specific. */
3139 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3143 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3145 if (sym->attr.dummy)
3147 sym->attr.proc = PROC_DUMMY;
3151 sym->attr.proc = PROC_EXTERNAL;
3155 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3158 if (sym->attr.intrinsic)
3160 m = gfc_intrinsic_sub_interface (c, 1);
3164 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3165 "with an intrinsic", sym->name, &c->loc);
3173 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3175 c->resolved_sym = sym;
3176 pure_subroutine (c, sym);
3183 resolve_specific_s (gfc_code *c)
3188 sym = c->symtree->n.sym;
3192 m = resolve_specific_s0 (c, sym);
3195 if (m == MATCH_ERROR)
3198 if (sym->ns->parent == NULL)
3201 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3207 sym = c->symtree->n.sym;
3208 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3209 sym->name, &c->loc);
3215 /* Resolve a subroutine call not known to be generic nor specific. */
3218 resolve_unknown_s (gfc_code *c)
3222 sym = c->symtree->n.sym;
3224 if (sym->attr.dummy)
3226 sym->attr.proc = PROC_DUMMY;
3230 /* See if we have an intrinsic function reference. */
3232 if (gfc_is_intrinsic (sym, 1, c->loc))
3234 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3239 /* The reference is to an external name. */
3242 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3244 c->resolved_sym = sym;
3246 pure_subroutine (c, sym);
3252 /* Resolve a subroutine call. Although it was tempting to use the same code
3253 for functions, subroutines and functions are stored differently and this
3254 makes things awkward. */
3257 resolve_call (gfc_code *c)
3260 procedure_type ptype = PROC_INTRINSIC;
3261 gfc_symbol *csym, *sym;
3262 bool no_formal_args;
3264 csym = c->symtree ? c->symtree->n.sym : NULL;
3266 if (csym && csym->ts.type != BT_UNKNOWN)
3268 gfc_error ("'%s' at %L has a type, which is not consistent with "
3269 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3273 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3276 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3277 sym = st ? st->n.sym : NULL;
3278 if (sym && csym != sym
3279 && sym->ns == gfc_current_ns
3280 && sym->attr.flavor == FL_PROCEDURE
3281 && sym->attr.contained)
3284 if (csym->attr.generic)
3285 c->symtree->n.sym = sym;
3288 csym = c->symtree->n.sym;
3292 /* If this ia a deferred TBP, c->expr1 will be set. */
3293 if (!c->expr1 && csym)
3295 if (csym->attr.abstract)
3297 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3298 csym->name, &c->loc);
3302 /* Subroutines without the RECURSIVE attribution are not allowed to
3304 if (is_illegal_recursion (csym, gfc_current_ns))
3306 if (csym->attr.entry && csym->ns->entries)
3307 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3308 "as subroutine '%s' is not RECURSIVE",
3309 csym->name, &c->loc, csym->ns->entries->sym->name);
3311 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3312 "as it is not RECURSIVE", csym->name, &c->loc);
3318 /* Switch off assumed size checking and do this again for certain kinds
3319 of procedure, once the procedure itself is resolved. */
3320 need_full_assumed_size++;
3323 ptype = csym->attr.proc;
3325 no_formal_args = csym && is_external_proc (csym)
3326 && gfc_sym_get_dummy_args (csym) == NULL;
3327 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3330 /* Resume assumed_size checking. */
3331 need_full_assumed_size--;
3333 /* If external, check for usage. */
3334 if (csym && is_external_proc (csym))
3335 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3338 if (c->resolved_sym == NULL)
3340 c->resolved_isym = NULL;
3341 switch (procedure_kind (csym))
3344 t = resolve_generic_s (c);
3347 case PTYPE_SPECIFIC:
3348 t = resolve_specific_s (c);
3352 t = resolve_unknown_s (c);
3356 gfc_internal_error ("resolve_subroutine(): bad function type");
3360 /* Some checks of elemental subroutine actual arguments. */
3361 if (!resolve_elemental_actual (NULL, c))
3368 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3369 op1->shape and op2->shape are non-NULL return true if their shapes
3370 match. If both op1->shape and op2->shape are non-NULL return false
3371 if their shapes do not match. If either op1->shape or op2->shape is
3372 NULL, return true. */
3375 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3382 if (op1->shape != NULL && op2->shape != NULL)
3384 for (i = 0; i < op1->rank; i++)
3386 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3388 gfc_error ("Shapes for operands at %L and %L are not conformable",
3389 &op1->where, &op2->where);
3400 /* Resolve an operator expression node. This can involve replacing the
3401 operation with a user defined function call. */
3404 resolve_operator (gfc_expr *e)
3406 gfc_expr *op1, *op2;
3408 bool dual_locus_error;
3411 /* Resolve all subnodes-- give them types. */
3413 switch (e->value.op.op)
3416 if (!gfc_resolve_expr (e->value.op.op2))
3419 /* Fall through... */
3422 case INTRINSIC_UPLUS:
3423 case INTRINSIC_UMINUS:
3424 case INTRINSIC_PARENTHESES:
3425 if (!gfc_resolve_expr (e->value.op.op1))
3430 /* Typecheck the new node. */
3432 op1 = e->value.op.op1;
3433 op2 = e->value.op.op2;
3434 dual_locus_error = false;
3436 if ((op1 && op1->expr_type == EXPR_NULL)
3437 || (op2 && op2->expr_type == EXPR_NULL))
3439 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3443 switch (e->value.op.op)
3445 case INTRINSIC_UPLUS:
3446 case INTRINSIC_UMINUS:
3447 if (op1->ts.type == BT_INTEGER
3448 || op1->ts.type == BT_REAL
3449 || op1->ts.type == BT_COMPLEX)
3455 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3456 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3459 case INTRINSIC_PLUS:
3460 case INTRINSIC_MINUS:
3461 case INTRINSIC_TIMES:
3462 case INTRINSIC_DIVIDE:
3463 case INTRINSIC_POWER:
3464 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3466 gfc_type_convert_binary (e, 1);
3471 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3472 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3473 gfc_typename (&op2->ts));
3476 case INTRINSIC_CONCAT:
3477 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3478 && op1->ts.kind == op2->ts.kind)
3480 e->ts.type = BT_CHARACTER;
3481 e->ts.kind = op1->ts.kind;
3486 _("Operands of string concatenation operator at %%L are %s/%s"),
3487 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3493 case INTRINSIC_NEQV:
3494 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3496 e->ts.type = BT_LOGICAL;
3497 e->ts.kind = gfc_kind_max (op1, op2);
3498 if (op1->ts.kind < e->ts.kind)
3499 gfc_convert_type (op1, &e->ts, 2);
3500 else if (op2->ts.kind < e->ts.kind)
3501 gfc_convert_type (op2, &e->ts, 2);
3505 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3506 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3507 gfc_typename (&op2->ts));
3512 if (op1->ts.type == BT_LOGICAL)
3514 e->ts.type = BT_LOGICAL;
3515 e->ts.kind = op1->ts.kind;
3519 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3520 gfc_typename (&op1->ts));
3524 case INTRINSIC_GT_OS:
3526 case INTRINSIC_GE_OS:
3528 case INTRINSIC_LT_OS:
3530 case INTRINSIC_LE_OS:
3531 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3533 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3537 /* Fall through... */
3540 case INTRINSIC_EQ_OS:
3542 case INTRINSIC_NE_OS:
3543 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3544 && op1->ts.kind == op2->ts.kind)
3546 e->ts.type = BT_LOGICAL;
3547 e->ts.kind = gfc_default_logical_kind;
3551 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3553 gfc_type_convert_binary (e, 1);
3555 e->ts.type = BT_LOGICAL;
3556 e->ts.kind = gfc_default_logical_kind;
3558 if (gfc_option.warn_compare_reals)
3560 gfc_intrinsic_op op = e->value.op.op;
3562 /* Type conversion has made sure that the types of op1 and op2
3563 agree, so it is only necessary to check the first one. */
3564 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3565 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3566 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3570 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3571 msg = "Equality comparison for %s at %L";
3573 msg = "Inequality comparison for %s at %L";
3575 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3582 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3584 _("Logicals at %%L must be compared with %s instead of %s"),
3585 (e->value.op.op == INTRINSIC_EQ
3586 || e->value.op.op == INTRINSIC_EQ_OS)
3587 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3590 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3591 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3592 gfc_typename (&op2->ts));
3596 case INTRINSIC_USER:
3597 if (e->value.op.uop->op == NULL)
3598 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3599 else if (op2 == NULL)
3600 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3601 e->value.op.uop->name, gfc_typename (&op1->ts));
3604 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3605 e->value.op.uop->name, gfc_typename (&op1->ts),
3606 gfc_typename (&op2->ts));
3607 e->value.op.uop->op->sym->attr.referenced = 1;
3612 case INTRINSIC_PARENTHESES:
3614 if (e->ts.type == BT_CHARACTER)
3615 e->ts.u.cl = op1->ts.u.cl;
3619 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3622 /* Deal with arrayness of an operand through an operator. */
3626 switch (e->value.op.op)
3628 case INTRINSIC_PLUS:
3629 case INTRINSIC_MINUS:
3630 case INTRINSIC_TIMES:
3631 case INTRINSIC_DIVIDE:
3632 case INTRINSIC_POWER:
3633 case INTRINSIC_CONCAT:
3637 case INTRINSIC_NEQV:
3639 case INTRINSIC_EQ_OS:
3641 case INTRINSIC_NE_OS:
3643 case INTRINSIC_GT_OS:
3645 case INTRINSIC_GE_OS:
3647 case INTRINSIC_LT_OS:
3649 case INTRINSIC_LE_OS:
3651 if (op1->rank == 0 && op2->rank == 0)
3654 if (op1->rank == 0 && op2->rank != 0)
3656 e->rank = op2->rank;
3658 if (e->shape == NULL)
3659 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3662 if (op1->rank != 0 && op2->rank == 0)
3664 e->rank = op1->rank;
3666 if (e->shape == NULL)
3667 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3670 if (op1->rank != 0 && op2->rank != 0)
3672 if (op1->rank == op2->rank)
3674 e->rank = op1->rank;
3675 if (e->shape == NULL)
3677 t = compare_shapes (op1, op2);
3681 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3686 /* Allow higher level expressions to work. */
3689 /* Try user-defined operators, and otherwise throw an error. */
3690 dual_locus_error = true;
3692 _("Inconsistent ranks for operator at %%L and %%L"));
3699 case INTRINSIC_PARENTHESES:
3701 case INTRINSIC_UPLUS:
3702 case INTRINSIC_UMINUS:
3703 /* Simply copy arrayness attribute */
3704 e->rank = op1->rank;
3706 if (e->shape == NULL)
3707 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3715 /* Attempt to simplify the expression. */
3718 t = gfc_simplify_expr (e, 0);
3719 /* Some calls do not succeed in simplification and return false
3720 even though there is no error; e.g. variable references to
3721 PARAMETER arrays. */
3722 if (!gfc_is_constant_expr (e))
3730 match m = gfc_extend_expr (e);
3733 if (m == MATCH_ERROR)
3737 if (dual_locus_error)
3738 gfc_error (msg, &op1->where, &op2->where);
3740 gfc_error (msg, &e->where);
3746 /************** Array resolution subroutines **************/
3749 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3752 /* Compare two integer expressions. */
3755 compare_bound (gfc_expr *a, gfc_expr *b)
3759 if (a == NULL || a->expr_type != EXPR_CONSTANT
3760 || b == NULL || b->expr_type != EXPR_CONSTANT)
3763 /* If either of the types isn't INTEGER, we must have
3764 raised an error earlier. */
3766 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3769 i = mpz_cmp (a->value.integer, b->value.integer);
3779 /* Compare an integer expression with an integer. */
3782 compare_bound_int (gfc_expr *a, int b)
3786 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3789 if (a->ts.type != BT_INTEGER)
3790 gfc_internal_error ("compare_bound_int(): Bad expression");
3792 i = mpz_cmp_si (a->value.integer, b);
3802 /* Compare an integer expression with a mpz_t. */
3805 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3809 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3812 if (a->ts.type != BT_INTEGER)
3813 gfc_internal_error ("compare_bound_int(): Bad expression");
3815 i = mpz_cmp (a->value.integer, b);
3825 /* Compute the last value of a sequence given by a triplet.
3826 Return 0 if it wasn't able to compute the last value, or if the
3827 sequence if empty, and 1 otherwise. */
3830 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3831 gfc_expr *stride, mpz_t last)
3835 if (start == NULL || start->expr_type != EXPR_CONSTANT
3836 || end == NULL || end->expr_type != EXPR_CONSTANT
3837 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3840 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3841 || (stride != NULL && stride->ts.type != BT_INTEGER))
3844 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3846 if (compare_bound (start, end) == CMP_GT)
3848 mpz_set (last, end->value.integer);
3852 if (compare_bound_int (stride, 0) == CMP_GT)
3854 /* Stride is positive */
3855 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3860 /* Stride is negative */
3861 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3866 mpz_sub (rem, end->value.integer, start->value.integer);
3867 mpz_tdiv_r (rem, rem, stride->value.integer);
3868 mpz_sub (last, end->value.integer, rem);
3875 /* Compare a single dimension of an array reference to the array
3879 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3883 if (ar->dimen_type[i] == DIMEN_STAR)
3885 gcc_assert (ar->stride[i] == NULL);
3886 /* This implies [*] as [*:] and [*:3] are not possible. */
3887 if (ar->start[i] == NULL)
3889 gcc_assert (ar->end[i] == NULL);
3894 /* Given start, end and stride values, calculate the minimum and
3895 maximum referenced indexes. */
3897 switch (ar->dimen_type[i])
3900 case DIMEN_THIS_IMAGE:
3905 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3908 gfc_warning ("Array reference at %L is out of bounds "
3909 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3910 mpz_get_si (ar->start[i]->value.integer),
3911 mpz_get_si (as->lower[i]->value.integer), i+1);
3913 gfc_warning ("Array reference at %L is out of bounds "
3914 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3915 mpz_get_si (ar->start[i]->value.integer),
3916 mpz_get_si (as->lower[i]->value.integer),
3920 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3923 gfc_warning ("Array reference at %L is out of bounds "
3924 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3925 mpz_get_si (ar->start[i]->value.integer),
3926 mpz_get_si (as->upper[i]->value.integer), i+1);
3928 gfc_warning ("Array reference at %L is out of bounds "
3929 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3930 mpz_get_si (ar->start[i]->value.integer),
3931 mpz_get_si (as->upper[i]->value.integer),
3940 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3941 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3943 comparison comp_start_end = compare_bound (AR_START, AR_END);
3945 /* Check for zero stride, which is not allowed. */
3946 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3948 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3952 /* if start == len || (stride > 0 && start < len)
3953 || (stride < 0 && start > len),
3954 then the array section contains at least one element. In this
3955 case, there is an out-of-bounds access if
3956 (start < lower || start > upper). */
3957 if (compare_bound (AR_START, AR_END) == CMP_EQ
3958 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3959 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3960 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3961 && comp_start_end == CMP_GT))
3963 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3965 gfc_warning ("Lower array reference at %L is out of bounds "
3966 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3967 mpz_get_si (AR_START->value.integer),
3968 mpz_get_si (as->lower[i]->value.integer), i+1);
3971 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3973 gfc_warning ("Lower array reference at %L is out of bounds "
3974 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3975 mpz_get_si (AR_START->value.integer),
3976 mpz_get_si (as->upper[i]->value.integer), i+1);
3981 /* If we can compute the highest index of the array section,
3982 then it also has to be between lower and upper. */
3983 mpz_init (last_value);
3984 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3987 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3989 gfc_warning ("Upper array reference at %L is out of bounds "
3990 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3991 mpz_get_si (last_value),
3992 mpz_get_si (as->lower[i]->value.integer), i+1);
3993 mpz_clear (last_value);
3996 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3998 gfc_warning ("Upper array reference at %L is out of bounds "
3999 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4000 mpz_get_si (last_value),
4001 mpz_get_si (as->upper[i]->value.integer), i+1);
4002 mpz_clear (last_value);
4006 mpz_clear (last_value);
4014 gfc_internal_error ("check_dimension(): Bad array reference");
4021 /* Compare an array reference with an array specification. */
4024 compare_spec_to_ref (gfc_array_ref *ar)
4031 /* TODO: Full array sections are only allowed as actual parameters. */
4032 if (as->type == AS_ASSUMED_SIZE
4033 && (/*ar->type == AR_FULL
4034 ||*/ (ar->type == AR_SECTION
4035 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4037 gfc_error ("Rightmost upper bound of assumed size array section "
4038 "not specified at %L", &ar->where);
4042 if (ar->type == AR_FULL)
4045 if (as->rank != ar->dimen)
4047 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4048 &ar->where, ar->dimen, as->rank);
4052 /* ar->codimen == 0 is a local array. */
4053 if (as->corank != ar->codimen && ar->codimen != 0)
4055 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4056 &ar->where, ar->codimen, as->corank);
4060 for (i = 0; i < as->rank; i++)
4061 if (!check_dimension (i, ar, as))
4064 /* Local access has no coarray spec. */
4065 if (ar->codimen != 0)
4066 for (i = as->rank; i < as->rank + as->corank; i++)
4068 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4069 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4071 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4072 i + 1 - as->rank, &ar->where);
4075 if (!check_dimension (i, ar, as))
4083 /* Resolve one part of an array index. */
4086 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4087 int force_index_integer_kind)
4094 if (!gfc_resolve_expr (index))
4097 if (check_scalar && index->rank != 0)
4099 gfc_error ("Array index at %L must be scalar", &index->where);
4103 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4105 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4106 &index->where, gfc_basic_typename (index->ts.type));
4110 if (index->ts.type == BT_REAL)
4111 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4115 if ((index->ts.kind != gfc_index_integer_kind
4116 && force_index_integer_kind)
4117 || index->ts.type != BT_INTEGER)
4120 ts.type = BT_INTEGER;
4121 ts.kind = gfc_index_integer_kind;
4123 gfc_convert_type_warn (index, &ts, 2, 0);
4129 /* Resolve one part of an array index. */
4132 gfc_resolve_index (gfc_expr *index, int check_scalar)
4134 return gfc_resolve_index_1 (index, check_scalar, 1);
4137 /* Resolve a dim argument to an intrinsic function. */
4140 gfc_resolve_dim_arg (gfc_expr *dim)
4145 if (!gfc_resolve_expr (dim))
4150 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4155 if (dim->ts.type != BT_INTEGER)
4157 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4161 if (dim->ts.kind != gfc_index_integer_kind)
4166 ts.type = BT_INTEGER;
4167 ts.kind = gfc_index_integer_kind;
4169 gfc_convert_type_warn (dim, &ts, 2, 0);
4175 /* Given an expression that contains array references, update those array
4176 references to point to the right array specifications. While this is
4177 filled in during matching, this information is difficult to save and load
4178 in a module, so we take care of it here.
4180 The idea here is that the original array reference comes from the
4181 base symbol. We traverse the list of reference structures, setting
4182 the stored reference to references. Component references can
4183 provide an additional array specification. */
4186 find_array_spec (gfc_expr *e)
4192 if (e->symtree->n.sym->ts.type == BT_CLASS)
4193 as = CLASS_DATA (e->symtree->n.sym)->as;
4195 as = e->symtree->n.sym->as;
4197 for (ref = e->ref; ref; ref = ref->next)
4202 gfc_internal_error ("find_array_spec(): Missing spec");
4209 c = ref->u.c.component;
4210 if (c->attr.dimension)
4213 gfc_internal_error ("find_array_spec(): unused as(1)");
4224 gfc_internal_error ("find_array_spec(): unused as(2)");
4228 /* Resolve an array reference. */
4231 resolve_array_ref (gfc_array_ref *ar)
4233 int i, check_scalar;
4236 for (i = 0; i < ar->dimen + ar->codimen; i++)
4238 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4240 /* Do not force gfc_index_integer_kind for the start. We can
4241 do fine with any integer kind. This avoids temporary arrays
4242 created for indexing with a vector. */
4243 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4245 if (!gfc_resolve_index (ar->end[i], check_scalar))
4247 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4252 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4256 ar->dimen_type[i] = DIMEN_ELEMENT;
4260 ar->dimen_type[i] = DIMEN_VECTOR;
4261 if (e->expr_type == EXPR_VARIABLE
4262 && e->symtree->n.sym->ts.type == BT_DERIVED)
4263 ar->start[i] = gfc_get_parentheses (e);
4267 gfc_error ("Array index at %L is an array of rank %d",
4268 &ar->c_where[i], e->rank);
4272 /* Fill in the upper bound, which may be lower than the
4273 specified one for something like a(2:10:5), which is
4274 identical to a(2:7:5). Only relevant for strides not equal
4275 to one. Don't try a division by zero. */
4276 if (ar->dimen_type[i] == DIMEN_RANGE
4277 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4278 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4279 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4283 if (gfc_ref_dimen_size (ar, i, &size, &end))
4285 if (ar->end[i] == NULL)
4288 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4290 mpz_set (ar->end[i]->value.integer, end);
4292 else if (ar->end[i]->ts.type == BT_INTEGER
4293 && ar->end[i]->expr_type == EXPR_CONSTANT)
4295 mpz_set (ar->end[i]->value.integer, end);
4306 if (ar->type == AR_FULL)
4308 if (ar->as->rank == 0)
4309 ar->type = AR_ELEMENT;
4311 /* Make sure array is the same as array(:,:), this way
4312 we don't need to special case all the time. */
4313 ar->dimen = ar->as->rank;
4314 for (i = 0; i < ar->dimen; i++)
4316 ar->dimen_type[i] = DIMEN_RANGE;
4318 gcc_assert (ar->start[i] == NULL);
4319 gcc_assert (ar->end[i] == NULL);
4320 gcc_assert (ar->stride[i] == NULL);
4324 /* If the reference type is unknown, figure out what kind it is. */
4326 if (ar->type == AR_UNKNOWN)
4328 ar->type = AR_ELEMENT;
4329 for (i = 0; i < ar->dimen; i++)
4330 if (ar->dimen_type[i] == DIMEN_RANGE
4331 || ar->dimen_type[i] == DIMEN_VECTOR)
4333 ar->type = AR_SECTION;
4338 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4341 if (ar->as->corank && ar->codimen == 0)
4344 ar->codimen = ar->as->corank;
4345 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4346 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4354 resolve_substring (gfc_ref *ref)
4356 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4358 if (ref->u.ss.start != NULL)
4360 if (!gfc_resolve_expr (ref->u.ss.start))
4363 if (ref->u.ss.start->ts.type != BT_INTEGER)
4365 gfc_error ("Substring start index at %L must be of type INTEGER",
4366 &ref->u.ss.start->where);
4370 if (ref->u.ss.start->rank != 0)
4372 gfc_error ("Substring start index at %L must be scalar",
4373 &ref->u.ss.start->where);
4377 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4378 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4379 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4381 gfc_error ("Substring start index at %L is less than one",
4382 &ref->u.ss.start->where);
4387 if (ref->u.ss.end != NULL)
4389 if (!gfc_resolve_expr (ref->u.ss.end))
4392 if (ref->u.ss.end->ts.type != BT_INTEGER)
4394 gfc_error ("Substring end index at %L must be of type INTEGER",
4395 &ref->u.ss.end->where);
4399 if (ref->u.ss.end->rank != 0)
4401 gfc_error ("Substring end index at %L must be scalar",
4402 &ref->u.ss.end->where);
4406 if (ref->u.ss.length != NULL
4407 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4408 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4409 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4411 gfc_error ("Substring end index at %L exceeds the string length",
4412 &ref->u.ss.start->where);
4416 if (compare_bound_mpz_t (ref->u.ss.end,
4417 gfc_integer_kinds[k].huge) == CMP_GT
4418 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4419 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4421 gfc_error ("Substring end index at %L is too large",
4422 &ref->u.ss.end->where);
4431 /* This function supplies missing substring charlens. */
4434 gfc_resolve_substring_charlen (gfc_expr *e)
4437 gfc_expr *start, *end;
4439 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4440 if (char_ref->type == REF_SUBSTRING)
4446 gcc_assert (char_ref->next == NULL);
4450 if (e->ts.u.cl->length)
4451 gfc_free_expr (e->ts.u.cl->length);
4452 else if (e->expr_type == EXPR_VARIABLE
4453 && e->symtree->n.sym->attr.dummy)
4457 e->ts.type = BT_CHARACTER;
4458 e->ts.kind = gfc_default_character_kind;
4461 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4463 if (char_ref->u.ss.start)
4464 start = gfc_copy_expr (char_ref->u.ss.start);
4466 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4468 if (char_ref->u.ss.end)
4469 end = gfc_copy_expr (char_ref->u.ss.end);
4470 else if (e->expr_type == EXPR_VARIABLE)
4471 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4477 gfc_free_expr (start);
4478 gfc_free_expr (end);
4482 /* Length = (end - start +1). */
4483 e->ts.u.cl->length = gfc_subtract (end, start);
4484 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4485 gfc_get_int_expr (gfc_default_integer_kind,
4488 e->ts.u.cl->length->ts.type = BT_INTEGER;
4489 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4491 /* Make sure that the length is simplified. */
4492 gfc_simplify_expr (e->ts.u.cl->length, 1);
4493 gfc_resolve_expr (e->ts.u.cl->length);
4497 /* Resolve subtype references. */
4500 resolve_ref (gfc_expr *expr)
4502 int current_part_dimension, n_components, seen_part_dimension;
4505 for (ref = expr->ref; ref; ref = ref->next)
4506 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4508 find_array_spec (expr);
4512 for (ref = expr->ref; ref; ref = ref->next)
4516 if (!resolve_array_ref (&ref->u.ar))
4524 if (!resolve_substring (ref))
4529 /* Check constraints on part references. */
4531 current_part_dimension = 0;
4532 seen_part_dimension = 0;
4535 for (ref = expr->ref; ref; ref = ref->next)
4540 switch (ref->u.ar.type)
4543 /* Coarray scalar. */
4544 if (ref->u.ar.as->rank == 0)
4546 current_part_dimension = 0;
4551 current_part_dimension = 1;
4555 current_part_dimension = 0;
4559 gfc_internal_error ("resolve_ref(): Bad array reference");
4565 if (current_part_dimension || seen_part_dimension)
4568 if (ref->u.c.component->attr.pointer
4569 || ref->u.c.component->attr.proc_pointer
4570 || (ref->u.c.component->ts.type == BT_CLASS
4571 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4573 gfc_error ("Component to the right of a part reference "
4574 "with nonzero rank must not have the POINTER "
4575 "attribute at %L", &expr->where);
4578 else if (ref->u.c.component->attr.allocatable
4579 || (ref->u.c.component->ts.type == BT_CLASS
4580 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4583 gfc_error ("Component to the right of a part reference "
4584 "with nonzero rank must not have the ALLOCATABLE "
4585 "attribute at %L", &expr->where);
4597 if (((ref->type == REF_COMPONENT && n_components > 1)
4598 || ref->next == NULL)
4599 && current_part_dimension
4600 && seen_part_dimension)
4602 gfc_error ("Two or more part references with nonzero rank must "
4603 "not be specified at %L", &expr->where);
4607 if (ref->type == REF_COMPONENT)
4609 if (current_part_dimension)
4610 seen_part_dimension = 1;
4612 /* reset to make sure */
4613 current_part_dimension = 0;
4621 /* Given an expression, determine its shape. This is easier than it sounds.
4622 Leaves the shape array NULL if it is not possible to determine the shape. */
4625 expression_shape (gfc_expr *e)
4627 mpz_t array[GFC_MAX_DIMENSIONS];
4630 if (e->rank <= 0 || e->shape != NULL)
4633 for (i = 0; i < e->rank; i++)
4634 if (!gfc_array_dimen_size (e, i, &array[i]))
4637 e->shape = gfc_get_shape (e->rank);
4639 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4644 for (i--; i >= 0; i--)
4645 mpz_clear (array[i]);
4649 /* Given a variable expression node, compute the rank of the expression by
4650 examining the base symbol and any reference structures it may have. */
4653 expression_rank (gfc_expr *e)
4658 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4659 could lead to serious confusion... */
4660 gcc_assert (e->expr_type != EXPR_COMPCALL);
4664 if (e->expr_type == EXPR_ARRAY)
4666 /* Constructors can have a rank different from one via RESHAPE(). */
4668 if (e->symtree == NULL)
4674 e->rank = (e->symtree->n.sym->as == NULL)
4675 ? 0 : e->symtree->n.sym->as->rank;
4681 for (ref = e->ref; ref; ref = ref->next)
4683 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4684 && ref->u.c.component->attr.function && !ref->next)
4685 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4687 if (ref->type != REF_ARRAY)
4690 if (ref->u.ar.type == AR_FULL)
4692 rank = ref->u.ar.as->rank;
4696 if (ref->u.ar.type == AR_SECTION)
4698 /* Figure out the rank of the section. */
4700 gfc_internal_error ("expression_rank(): Two array specs");
4702 for (i = 0; i < ref->u.ar.dimen; i++)
4703 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4704 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4714 expression_shape (e);
4718 /* Resolve a variable expression. */
4721 resolve_variable (gfc_expr *e)
4728 if (e->symtree == NULL)
4730 sym = e->symtree->n.sym;
4732 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4733 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4734 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4736 if (!actual_arg || inquiry_argument)
4738 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4739 "be used as actual argument", sym->name, &e->where);
4743 /* TS 29113, 407b. */
4744 else if (e->ts.type == BT_ASSUMED)
4748 gfc_error ("Assumed-type variable %s at %L may only be used "
4749 "as actual argument", sym->name, &e->where);
4752 else if (inquiry_argument && !first_actual_arg)
4754 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4755 for all inquiry functions in resolve_function; the reason is
4756 that the function-name resolution happens too late in that
4758 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4759 "an inquiry function shall be the first argument",
4760 sym->name, &e->where);
4764 /* TS 29113, C535b. */
4765 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4766 && CLASS_DATA (sym)->as
4767 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4768 || (sym->ts.type != BT_CLASS && sym->as
4769 && sym->as->type == AS_ASSUMED_RANK))
4773 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4774 "actual argument", sym->name, &e->where);
4777 else if (inquiry_argument && !first_actual_arg)
4779 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4780 for all inquiry functions in resolve_function; the reason is
4781 that the function-name resolution happens too late in that
4783 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4784 "to an inquiry function shall be the first argument",
4785 sym->name, &e->where);
4790 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4791 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4792 && e->ref->next == NULL))
4794 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4795 "a subobject reference", sym->name, &e->ref->u.ar.where);
4798 /* TS 29113, 407b. */
4799 else if (e->ts.type == BT_ASSUMED && e->ref
4800 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4801 && e->ref->next == NULL))
4803 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4804 "reference", sym->name, &e->ref->u.ar.where);
4808 /* TS 29113, C535b. */
4809 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4810 && CLASS_DATA (sym)->as
4811 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4812 || (sym->ts.type != BT_CLASS && sym->as
4813 && sym->as->type == AS_ASSUMED_RANK))
4815 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4816 && e->ref->next == NULL))
4818 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4819 "reference", sym->name, &e->ref->u.ar.where);
4824 /* If this is an associate-name, it may be parsed with an array reference
4825 in error even though the target is scalar. Fail directly in this case.
4826 TODO Understand why class scalar expressions must be excluded. */
4827 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4829 if (sym->ts.type == BT_CLASS)
4830 gfc_fix_class_refs (e);
4831 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4835 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4836 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4838 /* On the other hand, the parser may not have known this is an array;
4839 in this case, we have to add a FULL reference. */
4840 if (sym->assoc && sym->attr.dimension && !e->ref)
4842 e->ref = gfc_get_ref ();
4843 e->ref->type = REF_ARRAY;
4844 e->ref->u.ar.type = AR_FULL;
4845 e->ref->u.ar.dimen = 0;
4848 if (e->ref && !resolve_ref (e))
4851 if (sym->attr.flavor == FL_PROCEDURE
4852 && (!sym->attr.function
4853 || (sym->attr.function && sym->result
4854 && sym->result->attr.proc_pointer
4855 && !sym->result->attr.function)))
4857 e->ts.type = BT_PROCEDURE;
4858 goto resolve_procedure;
4861 if (sym->ts.type != BT_UNKNOWN)
4862 gfc_variable_attr (e, &e->ts);
4865 /* Must be a simple variable reference. */
4866 if (!gfc_set_default_type (sym, 1, sym->ns))
4871 if (check_assumed_size_reference (sym, e))
4874 /* Deal with forward references to entries during resolve_code, to
4875 satisfy, at least partially, 12.5.2.5. */
4876 if (gfc_current_ns->entries
4877 && current_entry_id == sym->entry_id
4880 && cs_base->current->op != EXEC_ENTRY)
4882 gfc_entry_list *entry;
4883 gfc_formal_arglist *formal;
4885 bool seen, saved_specification_expr;
4887 /* If the symbol is a dummy... */
4888 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4890 entry = gfc_current_ns->entries;
4893 /* ...test if the symbol is a parameter of previous entries. */
4894 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4895 for (formal = entry->sym->formal; formal; formal = formal->next)
4897 if (formal->sym && sym->name == formal->sym->name)
4901 /* If it has not been seen as a dummy, this is an error. */
4904 if (specification_expr)
4905 gfc_error ("Variable '%s', used in a specification expression"
4906 ", is referenced at %L before the ENTRY statement "
4907 "in which it is a parameter",
4908 sym->name, &cs_base->current->loc);
4910 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4911 "statement in which it is a parameter",
4912 sym->name, &cs_base->current->loc);
4917 /* Now do the same check on the specification expressions. */
4918 saved_specification_expr = specification_expr;
4919 specification_expr = true;
4920 if (sym->ts.type == BT_CHARACTER
4921 && !gfc_resolve_expr (sym->ts.u.cl->length))
4925 for (n = 0; n < sym->as->rank; n++)
4927 if (!gfc_resolve_expr (sym->as->lower[n]))
4929 if (!gfc_resolve_expr (sym->as->upper[n]))
4932 specification_expr = saved_specification_expr;
4935 /* Update the symbol's entry level. */
4936 sym->entry_id = current_entry_id + 1;
4939 /* If a symbol has been host_associated mark it. This is used latter,
4940 to identify if aliasing is possible via host association. */
4941 if (sym->attr.flavor == FL_VARIABLE
4942 && gfc_current_ns->parent
4943 && (gfc_current_ns->parent == sym->ns
4944 || (gfc_current_ns->parent->parent
4945 && gfc_current_ns->parent->parent == sym->ns)))
4946 sym->attr.host_assoc = 1;
4949 if (t && !resolve_procedure_expression (e))
4952 /* F2008, C617 and C1229. */
4953 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4954 && gfc_is_coindexed (e))
4956 gfc_ref *ref, *ref2 = NULL;
4958 for (ref = e->ref; ref; ref = ref->next)
4960 if (ref->type == REF_COMPONENT)
4962 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4966 for ( ; ref; ref = ref->next)
4967 if (ref->type == REF_COMPONENT)
4970 /* Expression itself is not coindexed object. */
4971 if (ref && e->ts.type == BT_CLASS)
4973 gfc_error ("Polymorphic subobject of coindexed object at %L",
4978 /* Expression itself is coindexed object. */
4982 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4983 for ( ; c; c = c->next)
4984 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4986 gfc_error ("Coindexed object with polymorphic allocatable "
4987 "subcomponent at %L", &e->where);
4998 /* Checks to see that the correct symbol has been host associated.
4999 The only situation where this arises is that in which a twice
5000 contained function is parsed after the host association is made.
5001 Therefore, on detecting this, change the symbol in the expression
5002 and convert the array reference into an actual arglist if the old
5003 symbol is a variable. */
5005 check_host_association (gfc_expr *e)
5007 gfc_symbol *sym, *old_sym;
5011 gfc_actual_arglist *arg, *tail = NULL;
5012 bool retval = e->expr_type == EXPR_FUNCTION;
5014 /* If the expression is the result of substitution in
5015 interface.c(gfc_extend_expr) because there is no way in
5016 which the host association can be wrong. */
5017 if (e->symtree == NULL
5018 || e->symtree->n.sym == NULL
5019 || e->user_operator)
5022 old_sym = e->symtree->n.sym;
5024 if (gfc_current_ns->parent
5025 && old_sym->ns != gfc_current_ns)
5027 /* Use the 'USE' name so that renamed module symbols are
5028 correctly handled. */
5029 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5031 if (sym && old_sym != sym
5032 && sym->ts.type == old_sym->ts.type
5033 && sym->attr.flavor == FL_PROCEDURE
5034 && sym->attr.contained)
5036 /* Clear the shape, since it might not be valid. */
5037 gfc_free_shape (&e->shape, e->rank);
5039 /* Give the expression the right symtree! */
5040 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5041 gcc_assert (st != NULL);
5043 if (old_sym->attr.flavor == FL_PROCEDURE
5044 || e->expr_type == EXPR_FUNCTION)
5046 /* Original was function so point to the new symbol, since
5047 the actual argument list is already attached to the
5049 e->value.function.esym = NULL;
5054 /* Original was variable so convert array references into
5055 an actual arglist. This does not need any checking now
5056 since resolve_function will take care of it. */
5057 e->value.function.actual = NULL;
5058 e->expr_type = EXPR_FUNCTION;
5061 /* Ambiguity will not arise if the array reference is not
5062 the last reference. */
5063 for (ref = e->ref; ref; ref = ref->next)
5064 if (ref->type == REF_ARRAY && ref->next == NULL)
5067 gcc_assert (ref->type == REF_ARRAY);
5069 /* Grab the start expressions from the array ref and
5070 copy them into actual arguments. */
5071 for (n = 0; n < ref->u.ar.dimen; n++)
5073 arg = gfc_get_actual_arglist ();
5074 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5075 if (e->value.function.actual == NULL)
5076 tail = e->value.function.actual = arg;
5084 /* Dump the reference list and set the rank. */
5085 gfc_free_ref_list (e->ref);
5087 e->rank = sym->as ? sym->as->rank : 0;
5090 gfc_resolve_expr (e);
5094 /* This might have changed! */
5095 return e->expr_type == EXPR_FUNCTION;
5100 gfc_resolve_character_operator (gfc_expr *e)
5102 gfc_expr *op1 = e->value.op.op1;
5103 gfc_expr *op2 = e->value.op.op2;
5104 gfc_expr *e1 = NULL;
5105 gfc_expr *e2 = NULL;
5107 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5109 if (op1->ts.u.cl && op1->ts.u.cl->length)
5110 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5111 else if (op1->expr_type == EXPR_CONSTANT)
5112 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5113 op1->value.character.length);
5115 if (op2->ts.u.cl && op2->ts.u.cl->length)
5116 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5117 else if (op2->expr_type == EXPR_CONSTANT)
5118 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5119 op2->value.character.length);
5121 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5131 e->ts.u.cl->length = gfc_add (e1, e2);
5132 e->ts.u.cl->length->ts.type = BT_INTEGER;
5133 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5134 gfc_simplify_expr (e->ts.u.cl->length, 0);
5135 gfc_resolve_expr (e->ts.u.cl->length);
5141 /* Ensure that an character expression has a charlen and, if possible, a
5142 length expression. */
5145 fixup_charlen (gfc_expr *e)
5147 /* The cases fall through so that changes in expression type and the need
5148 for multiple fixes are picked up. In all circumstances, a charlen should
5149 be available for the middle end to hang a backend_decl on. */
5150 switch (e->expr_type)
5153 gfc_resolve_character_operator (e);
5156 if (e->expr_type == EXPR_ARRAY)
5157 gfc_resolve_character_array_constructor (e);
5159 case EXPR_SUBSTRING:
5160 if (!e->ts.u.cl && e->ref)
5161 gfc_resolve_substring_charlen (e);
5165 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5172 /* Update an actual argument to include the passed-object for type-bound
5173 procedures at the right position. */
5175 static gfc_actual_arglist*
5176 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5179 gcc_assert (argpos > 0);
5183 gfc_actual_arglist* result;
5185 result = gfc_get_actual_arglist ();
5189 result->name = name;
5195 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5197 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5202 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5205 extract_compcall_passed_object (gfc_expr* e)
5209 gcc_assert (e->expr_type == EXPR_COMPCALL);
5211 if (e->value.compcall.base_object)
5212 po = gfc_copy_expr (e->value.compcall.base_object);
5215 po = gfc_get_expr ();
5216 po->expr_type = EXPR_VARIABLE;
5217 po->symtree = e->symtree;
5218 po->ref = gfc_copy_ref (e->ref);
5219 po->where = e->where;
5222 if (!gfc_resolve_expr (po))
5229 /* Update the arglist of an EXPR_COMPCALL expression to include the
5233 update_compcall_arglist (gfc_expr* e)
5236 gfc_typebound_proc* tbp;
5238 tbp = e->value.compcall.tbp;
5243 po = extract_compcall_passed_object (e);
5247 if (tbp->nopass || e->value.compcall.ignore_pass)
5253 gcc_assert (tbp->pass_arg_num > 0);
5254 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5262 /* Extract the passed object from a PPC call (a copy of it). */
5265 extract_ppc_passed_object (gfc_expr *e)
5270 po = gfc_get_expr ();
5271 po->expr_type = EXPR_VARIABLE;
5272 po->symtree = e->symtree;
5273 po->ref = gfc_copy_ref (e->ref);
5274 po->where = e->where;
5276 /* Remove PPC reference. */
5278 while ((*ref)->next)
5279 ref = &(*ref)->next;
5280 gfc_free_ref_list (*ref);
5283 if (!gfc_resolve_expr (po))
5290 /* Update the actual arglist of a procedure pointer component to include the
5294 update_ppc_arglist (gfc_expr* e)
5298 gfc_typebound_proc* tb;
5300 ppc = gfc_get_proc_ptr_comp (e);
5308 else if (tb->nopass)
5311 po = extract_ppc_passed_object (e);
5318 gfc_error ("Passed-object at %L must be scalar", &e->where);
5323 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5325 gfc_error ("Base object for procedure-pointer component call at %L is of"
5326 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5330 gcc_assert (tb->pass_arg_num > 0);
5331 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5339 /* Check that the object a TBP is called on is valid, i.e. it must not be
5340 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5343 check_typebound_baseobject (gfc_expr* e)
5346 bool return_value = false;
5348 base = extract_compcall_passed_object (e);
5352 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5354 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5358 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5360 gfc_error ("Base object for type-bound procedure call at %L is of"
5361 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5365 /* F08:C1230. If the procedure called is NOPASS,
5366 the base object must be scalar. */
5367 if (e->value.compcall.tbp->nopass && base->rank != 0)
5369 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5370 " be scalar", &e->where);
5374 return_value = true;
5377 gfc_free_expr (base);
5378 return return_value;
5382 /* Resolve a call to a type-bound procedure, either function or subroutine,
5383 statically from the data in an EXPR_COMPCALL expression. The adapted
5384 arglist and the target-procedure symtree are returned. */
5387 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5388 gfc_actual_arglist** actual)
5390 gcc_assert (e->expr_type == EXPR_COMPCALL);
5391 gcc_assert (!e->value.compcall.tbp->is_generic);
5393 /* Update the actual arglist for PASS. */
5394 if (!update_compcall_arglist (e))
5397 *actual = e->value.compcall.actual;
5398 *target = e->value.compcall.tbp->u.specific;
5400 gfc_free_ref_list (e->ref);
5402 e->value.compcall.actual = NULL;
5404 /* If we find a deferred typebound procedure, check for derived types
5405 that an overriding typebound procedure has not been missed. */
5406 if (e->value.compcall.name
5407 && !e->value.compcall.tbp->non_overridable
5408 && e->value.compcall.base_object
5409 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5412 gfc_symbol *derived;
5414 /* Use the derived type of the base_object. */
5415 derived = e->value.compcall.base_object->ts.u.derived;
5418 /* If necessary, go through the inheritance chain. */
5419 while (!st && derived)
5421 /* Look for the typebound procedure 'name'. */
5422 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5423 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5424 e->value.compcall.name);
5426 derived = gfc_get_derived_super_type (derived);
5429 /* Now find the specific name in the derived type namespace. */
5430 if (st && st->n.tb && st->n.tb->u.specific)
5431 gfc_find_sym_tree (st->n.tb->u.specific->name,
5432 derived->ns, 1, &st);
5440 /* Get the ultimate declared type from an expression. In addition,
5441 return the last class/derived type reference and the copy of the
5442 reference list. If check_types is set true, derived types are
5443 identified as well as class references. */
5445 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5446 gfc_expr *e, bool check_types)
5448 gfc_symbol *declared;
5455 *new_ref = gfc_copy_ref (e->ref);
5457 for (ref = e->ref; ref; ref = ref->next)
5459 if (ref->type != REF_COMPONENT)
5462 if ((ref->u.c.component->ts.type == BT_CLASS
5463 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5464 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5466 declared = ref->u.c.component->ts.u.derived;
5472 if (declared == NULL)
5473 declared = e->symtree->n.sym->ts.u.derived;
5479 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5480 which of the specific bindings (if any) matches the arglist and transform
5481 the expression into a call of that binding. */
5484 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5486 gfc_typebound_proc* genproc;
5487 const char* genname;
5489 gfc_symbol *derived;
5491 gcc_assert (e->expr_type == EXPR_COMPCALL);
5492 genname = e->value.compcall.name;
5493 genproc = e->value.compcall.tbp;
5495 if (!genproc->is_generic)
5498 /* Try the bindings on this type and in the inheritance hierarchy. */
5499 for (; genproc; genproc = genproc->overridden)
5503 gcc_assert (genproc->is_generic);
5504 for (g = genproc->u.generic; g; g = g->next)
5507 gfc_actual_arglist* args;
5510 gcc_assert (g->specific);
5512 if (g->specific->error)
5515 target = g->specific->u.specific->n.sym;
5517 /* Get the right arglist by handling PASS/NOPASS. */
5518 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5519 if (!g->specific->nopass)
5522 po = extract_compcall_passed_object (e);
5525 gfc_free_actual_arglist (args);
5529 gcc_assert (g->specific->pass_arg_num > 0);
5530 gcc_assert (!g->specific->error);
5531 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5532 g->specific->pass_arg);
5534 resolve_actual_arglist (args, target->attr.proc,
5535 is_external_proc (target)
5536 && gfc_sym_get_dummy_args (target) == NULL);
5538 /* Check if this arglist matches the formal. */
5539 matches = gfc_arglist_matches_symbol (&args, target);
5541 /* Clean up and break out of the loop if we've found it. */
5542 gfc_free_actual_arglist (args);
5545 e->value.compcall.tbp = g->specific;
5546 genname = g->specific_st->name;
5547 /* Pass along the name for CLASS methods, where the vtab
5548 procedure pointer component has to be referenced. */
5556 /* Nothing matching found! */
5557 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5558 " '%s' at %L", genname, &e->where);
5562 /* Make sure that we have the right specific instance for the name. */
5563 derived = get_declared_from_expr (NULL, NULL, e, true);
5565 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5567 e->value.compcall.tbp = st->n.tb;
5573 /* Resolve a call to a type-bound subroutine. */
5576 resolve_typebound_call (gfc_code* c, const char **name)
5578 gfc_actual_arglist* newactual;
5579 gfc_symtree* target;
5581 /* Check that's really a SUBROUTINE. */
5582 if (!c->expr1->value.compcall.tbp->subroutine)
5584 gfc_error ("'%s' at %L should be a SUBROUTINE",
5585 c->expr1->value.compcall.name, &c->loc);
5589 if (!check_typebound_baseobject (c->expr1))
5592 /* Pass along the name for CLASS methods, where the vtab
5593 procedure pointer component has to be referenced. */
5595 *name = c->expr1->value.compcall.name;
5597 if (!resolve_typebound_generic_call (c->expr1, name))
5600 /* Transform into an ordinary EXEC_CALL for now. */
5602 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5605 c->ext.actual = newactual;
5606 c->symtree = target;
5607 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5609 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5611 gfc_free_expr (c->expr1);
5612 c->expr1 = gfc_get_expr ();
5613 c->expr1->expr_type = EXPR_FUNCTION;
5614 c->expr1->symtree = target;
5615 c->expr1->where = c->loc;
5617 return resolve_call (c);
5621 /* Resolve a component-call expression. */
5623 resolve_compcall (gfc_expr* e, const char **name)
5625 gfc_actual_arglist* newactual;
5626 gfc_symtree* target;
5628 /* Check that's really a FUNCTION. */
5629 if (!e->value.compcall.tbp->function)
5631 gfc_error ("'%s' at %L should be a FUNCTION",
5632 e->value.compcall.name, &e->where);
5636 /* These must not be assign-calls! */
5637 gcc_assert (!e->value.compcall.assign);
5639 if (!check_typebound_baseobject (e))
5642 /* Pass along the name for CLASS methods, where the vtab
5643 procedure pointer component has to be referenced. */
5645 *name = e->value.compcall.name;
5647 if (!resolve_typebound_generic_call (e, name))
5649 gcc_assert (!e->value.compcall.tbp->is_generic);
5651 /* Take the rank from the function's symbol. */
5652 if (e->value.compcall.tbp->u.specific->n.sym->as)
5653 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5655 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5656 arglist to the TBP's binding target. */
5658 if (!resolve_typebound_static (e, &target, &newactual))
5661 e->value.function.actual = newactual;
5662 e->value.function.name = NULL;
5663 e->value.function.esym = target->n.sym;
5664 e->value.function.isym = NULL;
5665 e->symtree = target;
5666 e->ts = target->n.sym->ts;
5667 e->expr_type = EXPR_FUNCTION;
5669 /* Resolution is not necessary if this is a class subroutine; this
5670 function only has to identify the specific proc. Resolution of
5671 the call will be done next in resolve_typebound_call. */
5672 return gfc_resolve_expr (e);
5677 /* Resolve a typebound function, or 'method'. First separate all
5678 the non-CLASS references by calling resolve_compcall directly. */
5681 resolve_typebound_function (gfc_expr* e)
5683 gfc_symbol *declared;
5695 /* Deal with typebound operators for CLASS objects. */
5696 expr = e->value.compcall.base_object;
5697 overridable = !e->value.compcall.tbp->non_overridable;
5698 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5700 /* If the base_object is not a variable, the corresponding actual
5701 argument expression must be stored in e->base_expression so
5702 that the corresponding tree temporary can be used as the base
5703 object in gfc_conv_procedure_call. */
5704 if (expr->expr_type != EXPR_VARIABLE)
5706 gfc_actual_arglist *args;
5708 for (args= e->value.function.actual; args; args = args->next)
5710 if (expr == args->expr)
5715 /* Since the typebound operators are generic, we have to ensure
5716 that any delays in resolution are corrected and that the vtab
5719 declared = ts.u.derived;
5720 c = gfc_find_component (declared, "_vptr", true, true);
5721 if (c->ts.u.derived == NULL)
5722 c->ts.u.derived = gfc_find_derived_vtab (declared);
5724 if (!resolve_compcall (e, &name))
5727 /* Use the generic name if it is there. */
5728 name = name ? name : e->value.function.esym->name;
5729 e->symtree = expr->symtree;
5730 e->ref = gfc_copy_ref (expr->ref);
5731 get_declared_from_expr (&class_ref, NULL, e, false);
5733 /* Trim away the extraneous references that emerge from nested
5734 use of interface.c (extend_expr). */
5735 if (class_ref && class_ref->next)
5737 gfc_free_ref_list (class_ref->next);
5738 class_ref->next = NULL;
5740 else if (e->ref && !class_ref)
5742 gfc_free_ref_list (e->ref);
5746 gfc_add_vptr_component (e);
5747 gfc_add_component_ref (e, name);
5748 e->value.function.esym = NULL;
5749 if (expr->expr_type != EXPR_VARIABLE)
5750 e->base_expr = expr;
5755 return resolve_compcall (e, NULL);
5757 if (!resolve_ref (e))
5760 /* Get the CLASS declared type. */
5761 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5763 /* Weed out cases of the ultimate component being a derived type. */
5764 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5765 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5767 gfc_free_ref_list (new_ref);
5768 return resolve_compcall (e, NULL);
5771 c = gfc_find_component (declared, "_data", true, true);
5772 declared = c->ts.u.derived;
5774 /* Treat the call as if it is a typebound procedure, in order to roll
5775 out the correct name for the specific function. */
5776 if (!resolve_compcall (e, &name))
5778 gfc_free_ref_list (new_ref);
5785 /* Convert the expression to a procedure pointer component call. */
5786 e->value.function.esym = NULL;
5792 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5793 gfc_add_vptr_component (e);
5794 gfc_add_component_ref (e, name);
5796 /* Recover the typespec for the expression. This is really only
5797 necessary for generic procedures, where the additional call
5798 to gfc_add_component_ref seems to throw the collection of the
5799 correct typespec. */
5803 gfc_free_ref_list (new_ref);
5808 /* Resolve a typebound subroutine, or 'method'. First separate all
5809 the non-CLASS references by calling resolve_typebound_call
5813 resolve_typebound_subroutine (gfc_code *code)
5815 gfc_symbol *declared;
5825 st = code->expr1->symtree;
5827 /* Deal with typebound operators for CLASS objects. */
5828 expr = code->expr1->value.compcall.base_object;
5829 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5830 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5832 /* If the base_object is not a variable, the corresponding actual
5833 argument expression must be stored in e->base_expression so
5834 that the corresponding tree temporary can be used as the base
5835 object in gfc_conv_procedure_call. */
5836 if (expr->expr_type != EXPR_VARIABLE)
5838 gfc_actual_arglist *args;
5840 args= code->expr1->value.function.actual;
5841 for (; args; args = args->next)
5842 if (expr == args->expr)
5846 /* Since the typebound operators are generic, we have to ensure
5847 that any delays in resolution are corrected and that the vtab
5849 declared = expr->ts.u.derived;
5850 c = gfc_find_component (declared, "_vptr", true, true);
5851 if (c->ts.u.derived == NULL)
5852 c->ts.u.derived = gfc_find_derived_vtab (declared);
5854 if (!resolve_typebound_call (code, &name))
5857 /* Use the generic name if it is there. */
5858 name = name ? name : code->expr1->value.function.esym->name;
5859 code->expr1->symtree = expr->symtree;
5860 code->expr1->ref = gfc_copy_ref (expr->ref);
5862 /* Trim away the extraneous references that emerge from nested
5863 use of interface.c (extend_expr). */
5864 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5865 if (class_ref && class_ref->next)
5867 gfc_free_ref_list (class_ref->next);
5868 class_ref->next = NULL;
5870 else if (code->expr1->ref && !class_ref)
5872 gfc_free_ref_list (code->expr1->ref);
5873 code->expr1->ref = NULL;
5876 /* Now use the procedure in the vtable. */
5877 gfc_add_vptr_component (code->expr1);
5878 gfc_add_component_ref (code->expr1, name);
5879 code->expr1->value.function.esym = NULL;
5880 if (expr->expr_type != EXPR_VARIABLE)
5881 code->expr1->base_expr = expr;
5886 return resolve_typebound_call (code, NULL);
5888 if (!resolve_ref (code->expr1))
5891 /* Get the CLASS declared type. */
5892 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5894 /* Weed out cases of the ultimate component being a derived type. */
5895 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5896 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5898 gfc_free_ref_list (new_ref);
5899 return resolve_typebound_call (code, NULL);
5902 if (!resolve_typebound_call (code, &name))
5904 gfc_free_ref_list (new_ref);
5907 ts = code->expr1->ts;
5911 /* Convert the expression to a procedure pointer component call. */
5912 code->expr1->value.function.esym = NULL;
5913 code->expr1->symtree = st;
5916 code->expr1->ref = new_ref;
5918 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5919 gfc_add_vptr_component (code->expr1);
5920 gfc_add_component_ref (code->expr1, name);
5922 /* Recover the typespec for the expression. This is really only
5923 necessary for generic procedures, where the additional call
5924 to gfc_add_component_ref seems to throw the collection of the
5925 correct typespec. */
5926 code->expr1->ts = ts;
5929 gfc_free_ref_list (new_ref);
5935 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5938 resolve_ppc_call (gfc_code* c)
5940 gfc_component *comp;
5942 comp = gfc_get_proc_ptr_comp (c->expr1);
5943 gcc_assert (comp != NULL);
5945 c->resolved_sym = c->expr1->symtree->n.sym;
5946 c->expr1->expr_type = EXPR_VARIABLE;
5948 if (!comp->attr.subroutine)
5949 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5951 if (!resolve_ref (c->expr1))
5954 if (!update_ppc_arglist (c->expr1))
5957 c->ext.actual = c->expr1->value.compcall.actual;
5959 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5960 !(comp->ts.interface
5961 && comp->ts.interface->formal)))
5964 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5970 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5973 resolve_expr_ppc (gfc_expr* e)
5975 gfc_component *comp;
5977 comp = gfc_get_proc_ptr_comp (e);
5978 gcc_assert (comp != NULL);
5980 /* Convert to EXPR_FUNCTION. */
5981 e->expr_type = EXPR_FUNCTION;
5982 e->value.function.isym = NULL;
5983 e->value.function.actual = e->value.compcall.actual;
5985 if (comp->as != NULL)
5986 e->rank = comp->as->rank;
5988 if (!comp->attr.function)
5989 gfc_add_function (&comp->attr, comp->name, &e->where);
5991 if (!resolve_ref (e))
5994 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5995 !(comp->ts.interface
5996 && comp->ts.interface->formal)))
5999 if (!update_ppc_arglist (e))
6002 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6009 gfc_is_expandable_expr (gfc_expr *e)
6011 gfc_constructor *con;
6013 if (e->expr_type == EXPR_ARRAY)
6015 /* Traverse the constructor looking for variables that are flavor
6016 parameter. Parameters must be expanded since they are fully used at
6018 con = gfc_constructor_first (e->value.constructor);
6019 for (; con; con = gfc_constructor_next (con))
6021 if (con->expr->expr_type == EXPR_VARIABLE
6022 && con->expr->symtree
6023 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6024 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6026 if (con->expr->expr_type == EXPR_ARRAY
6027 && gfc_is_expandable_expr (con->expr))
6035 /* Resolve an expression. That is, make sure that types of operands agree
6036 with their operators, intrinsic operators are converted to function calls
6037 for overloaded types and unresolved function references are resolved. */
6040 gfc_resolve_expr (gfc_expr *e)
6043 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6048 /* inquiry_argument only applies to variables. */
6049 inquiry_save = inquiry_argument;
6050 actual_arg_save = actual_arg;
6051 first_actual_arg_save = first_actual_arg;
6053 if (e->expr_type != EXPR_VARIABLE)
6055 inquiry_argument = false;
6057 first_actual_arg = false;
6060 switch (e->expr_type)
6063 t = resolve_operator (e);
6069 if (check_host_association (e))
6070 t = resolve_function (e);
6073 t = resolve_variable (e);
6075 expression_rank (e);
6078 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6079 && e->ref->type != REF_SUBSTRING)
6080 gfc_resolve_substring_charlen (e);
6085 t = resolve_typebound_function (e);
6088 case EXPR_SUBSTRING:
6089 t = resolve_ref (e);
6098 t = resolve_expr_ppc (e);
6103 if (!resolve_ref (e))
6106 t = gfc_resolve_array_constructor (e);
6107 /* Also try to expand a constructor. */
6110 expression_rank (e);
6111 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6112 gfc_expand_constructor (e, false);
6115 /* This provides the opportunity for the length of constructors with
6116 character valued function elements to propagate the string length
6117 to the expression. */
6118 if (t && e->ts.type == BT_CHARACTER)
6120 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6121 here rather then add a duplicate test for it above. */
6122 gfc_expand_constructor (e, false);
6123 t = gfc_resolve_character_array_constructor (e);
6128 case EXPR_STRUCTURE:
6129 t = resolve_ref (e);
6133 t = resolve_structure_cons (e, 0);
6137 t = gfc_simplify_expr (e, 0);
6141 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6144 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6147 inquiry_argument = inquiry_save;
6148 actual_arg = actual_arg_save;
6149 first_actual_arg = first_actual_arg_save;
6155 /* Resolve an expression from an iterator. They must be scalar and have
6156 INTEGER or (optionally) REAL type. */
6159 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6160 const char *name_msgid)
6162 if (!gfc_resolve_expr (expr))
6165 if (expr->rank != 0)
6167 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6171 if (expr->ts.type != BT_INTEGER)
6173 if (expr->ts.type == BT_REAL)
6176 return gfc_notify_std (GFC_STD_F95_DEL,
6177 "%s at %L must be integer",
6178 _(name_msgid), &expr->where);
6181 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6188 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6196 /* Resolve the expressions in an iterator structure. If REAL_OK is
6197 false allow only INTEGER type iterators, otherwise allow REAL types.
6198 Set own_scope to true for ac-implied-do and data-implied-do as those
6199 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6202 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6204 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6207 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6208 _("iterator variable")))
6211 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6212 "Start expression in DO loop"))
6215 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6216 "End expression in DO loop"))
6219 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6220 "Step expression in DO loop"))
6223 if (iter->step->expr_type == EXPR_CONSTANT)
6225 if ((iter->step->ts.type == BT_INTEGER
6226 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6227 || (iter->step->ts.type == BT_REAL
6228 && mpfr_sgn (iter->step->value.real) == 0))
6230 gfc_error ("Step expression in DO loop at %L cannot be zero",
6231 &iter->step->where);
6236 /* Convert start, end, and step to the same type as var. */
6237 if (iter->start->ts.kind != iter->var->ts.kind
6238 || iter->start->ts.type != iter->var->ts.type)
6239 gfc_convert_type (iter->start, &iter->var->ts, 2);
6241 if (iter->end->ts.kind != iter->var->ts.kind
6242 || iter->end->ts.type != iter->var->ts.type)
6243 gfc_convert_type (iter->end, &iter->var->ts, 2);
6245 if (iter->step->ts.kind != iter->var->ts.kind
6246 || iter->step->ts.type != iter->var->ts.type)
6247 gfc_convert_type (iter->step, &iter->var->ts, 2);
6249 if (iter->start->expr_type == EXPR_CONSTANT
6250 && iter->end->expr_type == EXPR_CONSTANT
6251 && iter->step->expr_type == EXPR_CONSTANT)
6254 if (iter->start->ts.type == BT_INTEGER)
6256 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6257 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6261 sgn = mpfr_sgn (iter->step->value.real);
6262 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6264 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6265 gfc_warning ("DO loop at %L will be executed zero times",
6266 &iter->step->where);
6273 /* Traversal function for find_forall_index. f == 2 signals that
6274 that variable itself is not to be checked - only the references. */
6277 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6279 if (expr->expr_type != EXPR_VARIABLE)
6282 /* A scalar assignment */
6283 if (!expr->ref || *f == 1)
6285 if (expr->symtree->n.sym == sym)
6297 /* Check whether the FORALL index appears in the expression or not.
6298 Returns true if SYM is found in EXPR. */
6301 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6303 if (gfc_traverse_expr (expr, sym, forall_index, f))
6310 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6311 to be a scalar INTEGER variable. The subscripts and stride are scalar
6312 INTEGERs, and if stride is a constant it must be nonzero.
6313 Furthermore "A subscript or stride in a forall-triplet-spec shall
6314 not contain a reference to any index-name in the
6315 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6318 resolve_forall_iterators (gfc_forall_iterator *it)
6320 gfc_forall_iterator *iter, *iter2;
6322 for (iter = it; iter; iter = iter->next)
6324 if (gfc_resolve_expr (iter->var)
6325 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6326 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6329 if (gfc_resolve_expr (iter->start)
6330 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6331 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6332 &iter->start->where);
6333 if (iter->var->ts.kind != iter->start->ts.kind)
6334 gfc_convert_type (iter->start, &iter->var->ts, 1);
6336 if (gfc_resolve_expr (iter->end)
6337 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6338 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6340 if (iter->var->ts.kind != iter->end->ts.kind)
6341 gfc_convert_type (iter->end, &iter->var->ts, 1);
6343 if (gfc_resolve_expr (iter->stride))
6345 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6346 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6347 &iter->stride->where, "INTEGER");
6349 if (iter->stride->expr_type == EXPR_CONSTANT
6350 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6351 gfc_error ("FORALL stride expression at %L cannot be zero",
6352 &iter->stride->where);
6354 if (iter->var->ts.kind != iter->stride->ts.kind)
6355 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6358 for (iter = it; iter; iter = iter->next)
6359 for (iter2 = iter; iter2; iter2 = iter2->next)
6361 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6362 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6363 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6364 gfc_error ("FORALL index '%s' may not appear in triplet "
6365 "specification at %L", iter->var->symtree->name,
6366 &iter2->start->where);
6371 /* Given a pointer to a symbol that is a derived type, see if it's
6372 inaccessible, i.e. if it's defined in another module and the components are
6373 PRIVATE. The search is recursive if necessary. Returns zero if no
6374 inaccessible components are found, nonzero otherwise. */
6377 derived_inaccessible (gfc_symbol *sym)
6381 if (sym->attr.use_assoc && sym->attr.private_comp)
6384 for (c = sym->components; c; c = c->next)
6386 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6394 /* Resolve the argument of a deallocate expression. The expression must be
6395 a pointer or a full array. */
6398 resolve_deallocate_expr (gfc_expr *e)
6400 symbol_attribute attr;
6401 int allocatable, pointer;
6407 if (!gfc_resolve_expr (e))
6410 if (e->expr_type != EXPR_VARIABLE)
6413 sym = e->symtree->n.sym;
6414 unlimited = UNLIMITED_POLY(sym);
6416 if (sym->ts.type == BT_CLASS)
6418 allocatable = CLASS_DATA (sym)->attr.allocatable;
6419 pointer = CLASS_DATA (sym)->attr.class_pointer;
6423 allocatable = sym->attr.allocatable;
6424 pointer = sym->attr.pointer;
6426 for (ref = e->ref; ref; ref = ref->next)
6431 if (ref->u.ar.type != AR_FULL
6432 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6433 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6438 c = ref->u.c.component;
6439 if (c->ts.type == BT_CLASS)
6441 allocatable = CLASS_DATA (c)->attr.allocatable;
6442 pointer = CLASS_DATA (c)->attr.class_pointer;
6446 allocatable = c->attr.allocatable;
6447 pointer = c->attr.pointer;
6457 attr = gfc_expr_attr (e);
6459 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6462 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6468 if (gfc_is_coindexed (e))
6470 gfc_error ("Coindexed allocatable object at %L", &e->where);
6475 && !gfc_check_vardef_context (e, true, true, false,
6476 _("DEALLOCATE object")))
6478 if (!gfc_check_vardef_context (e, false, true, false,
6479 _("DEALLOCATE object")))
6486 /* Returns true if the expression e contains a reference to the symbol sym. */
6488 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6490 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6497 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6499 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6503 /* Given the expression node e for an allocatable/pointer of derived type to be
6504 allocated, get the expression node to be initialized afterwards (needed for
6505 derived types with default initializers, and derived types with allocatable
6506 components that need nullification.) */
6509 gfc_expr_to_initialize (gfc_expr *e)
6515 result = gfc_copy_expr (e);
6517 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6518 for (ref = result->ref; ref; ref = ref->next)
6519 if (ref->type == REF_ARRAY && ref->next == NULL)
6521 ref->u.ar.type = AR_FULL;
6523 for (i = 0; i < ref->u.ar.dimen; i++)
6524 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6529 gfc_free_shape (&result->shape, result->rank);
6531 /* Recalculate rank, shape, etc. */
6532 gfc_resolve_expr (result);
6537 /* If the last ref of an expression is an array ref, return a copy of the
6538 expression with that one removed. Otherwise, a copy of the original
6539 expression. This is used for allocate-expressions and pointer assignment
6540 LHS, where there may be an array specification that needs to be stripped
6541 off when using gfc_check_vardef_context. */
6544 remove_last_array_ref (gfc_expr* e)
6549 e2 = gfc_copy_expr (e);
6550 for (r = &e2->ref; *r; r = &(*r)->next)
6551 if ((*r)->type == REF_ARRAY && !(*r)->next)
6553 gfc_free_ref_list (*r);
6562 /* Used in resolve_allocate_expr to check that a allocation-object and
6563 a source-expr are conformable. This does not catch all possible
6564 cases; in particular a runtime checking is needed. */
6567 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6570 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6572 /* First compare rank. */
6573 if (tail && e1->rank != tail->u.ar.as->rank)
6575 gfc_error ("Source-expr at %L must be scalar or have the "
6576 "same rank as the allocate-object at %L",
6577 &e1->where, &e2->where);
6588 for (i = 0; i < e1->rank; i++)
6590 if (tail->u.ar.start[i] == NULL)
6593 if (tail->u.ar.end[i])
6595 mpz_set (s, tail->u.ar.end[i]->value.integer);
6596 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6597 mpz_add_ui (s, s, 1);
6601 mpz_set (s, tail->u.ar.start[i]->value.integer);
6604 if (mpz_cmp (e1->shape[i], s) != 0)
6606 gfc_error ("Source-expr at %L and allocate-object at %L must "
6607 "have the same shape", &e1->where, &e2->where);
6620 /* Resolve the expression in an ALLOCATE statement, doing the additional
6621 checks to see whether the expression is OK or not. The expression must
6622 have a trailing array reference that gives the size of the array. */
6625 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6627 int i, pointer, allocatable, dimension, is_abstract;
6631 symbol_attribute attr;
6632 gfc_ref *ref, *ref2;
6635 gfc_symbol *sym = NULL;
6640 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6641 checking of coarrays. */
6642 for (ref = e->ref; ref; ref = ref->next)
6643 if (ref->next == NULL)
6646 if (ref && ref->type == REF_ARRAY)
6647 ref->u.ar.in_allocate = true;
6649 if (!gfc_resolve_expr (e))
6652 /* Make sure the expression is allocatable or a pointer. If it is
6653 pointer, the next-to-last reference must be a pointer. */
6657 sym = e->symtree->n.sym;
6659 /* Check whether ultimate component is abstract and CLASS. */
6662 /* Is the allocate-object unlimited polymorphic? */
6663 unlimited = UNLIMITED_POLY(e);
6665 if (e->expr_type != EXPR_VARIABLE)
6668 attr = gfc_expr_attr (e);
6669 pointer = attr.pointer;
6670 dimension = attr.dimension;
6671 codimension = attr.codimension;
6675 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6677 allocatable = CLASS_DATA (sym)->attr.allocatable;
6678 pointer = CLASS_DATA (sym)->attr.class_pointer;
6679 dimension = CLASS_DATA (sym)->attr.dimension;
6680 codimension = CLASS_DATA (sym)->attr.codimension;
6681 is_abstract = CLASS_DATA (sym)->attr.abstract;
6685 allocatable = sym->attr.allocatable;
6686 pointer = sym->attr.pointer;
6687 dimension = sym->attr.dimension;
6688 codimension = sym->attr.codimension;
6693 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6698 if (ref->u.ar.codimen > 0)
6701 for (n = ref->u.ar.dimen;
6702 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6703 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6710 if (ref->next != NULL)
6718 gfc_error ("Coindexed allocatable object at %L",
6723 c = ref->u.c.component;
6724 if (c->ts.type == BT_CLASS)
6726 allocatable = CLASS_DATA (c)->attr.allocatable;
6727 pointer = CLASS_DATA (c)->attr.class_pointer;
6728 dimension = CLASS_DATA (c)->attr.dimension;
6729 codimension = CLASS_DATA (c)->attr.codimension;
6730 is_abstract = CLASS_DATA (c)->attr.abstract;
6734 allocatable = c->attr.allocatable;
6735 pointer = c->attr.pointer;
6736 dimension = c->attr.dimension;
6737 codimension = c->attr.codimension;
6738 is_abstract = c->attr.abstract;
6750 /* Check for F08:C628. */
6751 if (allocatable == 0 && pointer == 0 && !unlimited)
6753 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6758 /* Some checks for the SOURCE tag. */
6761 /* Check F03:C631. */
6762 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6764 gfc_error ("Type of entity at %L is type incompatible with "
6765 "source-expr at %L", &e->where, &code->expr3->where);
6769 /* Check F03:C632 and restriction following Note 6.18. */
6770 if (code->expr3->rank > 0 && !unlimited
6771 && !conformable_arrays (code->expr3, e))
6774 /* Check F03:C633. */
6775 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6777 gfc_error ("The allocate-object at %L and the source-expr at %L "
6778 "shall have the same kind type parameter",
6779 &e->where, &code->expr3->where);
6783 /* Check F2008, C642. */
6784 if (code->expr3->ts.type == BT_DERIVED
6785 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6786 || (code->expr3->ts.u.derived->from_intmod
6787 == INTMOD_ISO_FORTRAN_ENV
6788 && code->expr3->ts.u.derived->intmod_sym_id
6789 == ISOFORTRAN_LOCK_TYPE)))
6791 gfc_error ("The source-expr at %L shall neither be of type "
6792 "LOCK_TYPE nor have a LOCK_TYPE component if "
6793 "allocate-object at %L is a coarray",
6794 &code->expr3->where, &e->where);
6799 /* Check F08:C629. */
6800 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6803 gcc_assert (e->ts.type == BT_CLASS);
6804 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6805 "type-spec or source-expr", sym->name, &e->where);
6809 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6811 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6812 code->ext.alloc.ts.u.cl->length);
6813 if (cmp == 1 || cmp == -1 || cmp == -3)
6815 gfc_error ("Allocating %s at %L with type-spec requires the same "
6816 "character-length parameter as in the declaration",
6817 sym->name, &e->where);
6822 /* In the variable definition context checks, gfc_expr_attr is used
6823 on the expression. This is fooled by the array specification
6824 present in e, thus we have to eliminate that one temporarily. */
6825 e2 = remove_last_array_ref (e);
6828 t = gfc_check_vardef_context (e2, true, true, false,
6829 _("ALLOCATE object"));
6831 t = gfc_check_vardef_context (e2, false, true, false,
6832 _("ALLOCATE object"));
6837 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6838 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6840 /* For class arrays, the initialization with SOURCE is done
6841 using _copy and trans_call. It is convenient to exploit that
6842 when the allocated type is different from the declared type but
6843 no SOURCE exists by setting expr3. */
6844 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6846 else if (!code->expr3)
6848 /* Set up default initializer if needed. */
6852 if (code->ext.alloc.ts.type == BT_DERIVED)
6853 ts = code->ext.alloc.ts;
6857 if (ts.type == BT_CLASS)
6858 ts = ts.u.derived->components->ts;
6860 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6862 gfc_code *init_st = gfc_get_code ();
6863 init_st->loc = code->loc;
6864 init_st->op = EXEC_INIT_ASSIGN;
6865 init_st->expr1 = gfc_expr_to_initialize (e);
6866 init_st->expr2 = init_e;
6867 init_st->next = code->next;
6868 code->next = init_st;
6871 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6873 /* Default initialization via MOLD (non-polymorphic). */
6874 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6875 gfc_resolve_expr (rhs);
6876 gfc_free_expr (code->expr3);
6880 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6882 /* Make sure the vtab symbol is present when
6883 the module variables are generated. */
6884 gfc_typespec ts = e->ts;
6886 ts = code->expr3->ts;
6887 else if (code->ext.alloc.ts.type == BT_DERIVED)
6888 ts = code->ext.alloc.ts;
6890 gfc_find_derived_vtab (ts.u.derived);
6893 e = gfc_expr_to_initialize (e);
6895 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6897 /* Again, make sure the vtab symbol is present when
6898 the module variables are generated. */
6899 gfc_typespec *ts = NULL;
6901 ts = &code->expr3->ts;
6903 ts = &code->ext.alloc.ts;
6907 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6908 gfc_find_derived_vtab (ts->u.derived);
6910 gfc_find_intrinsic_vtab (ts);
6913 e = gfc_expr_to_initialize (e);
6916 if (dimension == 0 && codimension == 0)
6919 /* Make sure the last reference node is an array specification. */
6921 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6922 || (dimension && ref2->u.ar.dimen == 0))
6924 gfc_error ("Array specification required in ALLOCATE statement "
6925 "at %L", &e->where);
6929 /* Make sure that the array section reference makes sense in the
6930 context of an ALLOCATE specification. */
6935 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6936 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6938 gfc_error ("Coarray specification required in ALLOCATE statement "
6939 "at %L", &e->where);
6943 for (i = 0; i < ar->dimen; i++)
6945 if (ref2->u.ar.type == AR_ELEMENT)
6948 switch (ar->dimen_type[i])
6954 if (ar->start[i] != NULL
6955 && ar->end[i] != NULL
6956 && ar->stride[i] == NULL)
6959 /* Fall Through... */
6964 case DIMEN_THIS_IMAGE:
6965 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6971 for (a = code->ext.alloc.list; a; a = a->next)
6973 sym = a->expr->symtree->n.sym;
6975 /* TODO - check derived type components. */
6976 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6979 if ((ar->start[i] != NULL
6980 && gfc_find_sym_in_expr (sym, ar->start[i]))
6981 || (ar->end[i] != NULL
6982 && gfc_find_sym_in_expr (sym, ar->end[i])))
6984 gfc_error ("'%s' must not appear in the array specification at "
6985 "%L in the same ALLOCATE statement where it is "
6986 "itself allocated", sym->name, &ar->where);
6992 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6994 if (ar->dimen_type[i] == DIMEN_ELEMENT
6995 || ar->dimen_type[i] == DIMEN_RANGE)
6997 if (i == (ar->dimen + ar->codimen - 1))
6999 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7000 "statement at %L", &e->where);
7006 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7007 && ar->stride[i] == NULL)
7010 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7023 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7025 gfc_expr *stat, *errmsg, *pe, *qe;
7026 gfc_alloc *a, *p, *q;
7029 errmsg = code->expr2;
7031 /* Check the stat variable. */
7034 gfc_check_vardef_context (stat, false, false, false,
7035 _("STAT variable"));
7037 if ((stat->ts.type != BT_INTEGER
7038 && !(stat->ref && (stat->ref->type == REF_ARRAY
7039 || stat->ref->type == REF_COMPONENT)))
7041 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7042 "variable", &stat->where);
7044 for (p = code->ext.alloc.list; p; p = p->next)
7045 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7047 gfc_ref *ref1, *ref2;
7050 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7051 ref1 = ref1->next, ref2 = ref2->next)
7053 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7055 if (ref1->u.c.component->name != ref2->u.c.component->name)
7064 gfc_error ("Stat-variable at %L shall not be %sd within "
7065 "the same %s statement", &stat->where, fcn, fcn);
7071 /* Check the errmsg variable. */
7075 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7078 gfc_check_vardef_context (errmsg, false, false, false,
7079 _("ERRMSG variable"));
7081 if ((errmsg->ts.type != BT_CHARACTER
7083 && (errmsg->ref->type == REF_ARRAY
7084 || errmsg->ref->type == REF_COMPONENT)))
7085 || errmsg->rank > 0 )
7086 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7087 "variable", &errmsg->where);
7089 for (p = code->ext.alloc.list; p; p = p->next)
7090 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7092 gfc_ref *ref1, *ref2;
7095 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7096 ref1 = ref1->next, ref2 = ref2->next)
7098 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7100 if (ref1->u.c.component->name != ref2->u.c.component->name)
7109 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7110 "the same %s statement", &errmsg->where, fcn, fcn);
7116 /* Check that an allocate-object appears only once in the statement. */
7118 for (p = code->ext.alloc.list; p; p = p->next)
7121 for (q = p->next; q; q = q->next)
7124 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7126 /* This is a potential collision. */
7127 gfc_ref *pr = pe->ref;
7128 gfc_ref *qr = qe->ref;
7130 /* Follow the references until
7131 a) They start to differ, in which case there is no error;
7132 you can deallocate a%b and a%c in a single statement
7133 b) Both of them stop, which is an error
7134 c) One of them stops, which is also an error. */
7137 if (pr == NULL && qr == NULL)
7139 gfc_error ("Allocate-object at %L also appears at %L",
7140 &pe->where, &qe->where);
7143 else if (pr != NULL && qr == NULL)
7145 gfc_error ("Allocate-object at %L is subobject of"
7146 " object at %L", &pe->where, &qe->where);
7149 else if (pr == NULL && qr != NULL)
7151 gfc_error ("Allocate-object at %L is subobject of"
7152 " object at %L", &qe->where, &pe->where);
7155 /* Here, pr != NULL && qr != NULL */
7156 gcc_assert(pr->type == qr->type);
7157 if (pr->type == REF_ARRAY)
7159 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7161 gcc_assert (qr->type == REF_ARRAY);
7163 if (pr->next && qr->next)
7166 gfc_array_ref *par = &(pr->u.ar);
7167 gfc_array_ref *qar = &(qr->u.ar);
7169 for (i=0; i<par->dimen; i++)
7171 if ((par->start[i] != NULL
7172 || qar->start[i] != NULL)
7173 && gfc_dep_compare_expr (par->start[i],
7174 qar->start[i]) != 0)
7181 if (pr->u.c.component->name != qr->u.c.component->name)
7194 if (strcmp (fcn, "ALLOCATE") == 0)
7196 for (a = code->ext.alloc.list; a; a = a->next)
7197 resolve_allocate_expr (a->expr, code);
7201 for (a = code->ext.alloc.list; a; a = a->next)
7202 resolve_deallocate_expr (a->expr);
7207 /************ SELECT CASE resolution subroutines ************/
7209 /* Callback function for our mergesort variant. Determines interval
7210 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7211 op1 > op2. Assumes we're not dealing with the default case.
7212 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7213 There are nine situations to check. */
7216 compare_cases (const gfc_case *op1, const gfc_case *op2)
7220 if (op1->low == NULL) /* op1 = (:L) */
7222 /* op2 = (:N), so overlap. */
7224 /* op2 = (M:) or (M:N), L < M */
7225 if (op2->low != NULL
7226 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7229 else if (op1->high == NULL) /* op1 = (K:) */
7231 /* op2 = (M:), so overlap. */
7233 /* op2 = (:N) or (M:N), K > N */
7234 if (op2->high != NULL
7235 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7238 else /* op1 = (K:L) */
7240 if (op2->low == NULL) /* op2 = (:N), K > N */
7241 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7243 else if (op2->high == NULL) /* op2 = (M:), L < M */
7244 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7246 else /* op2 = (M:N) */
7250 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7253 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7262 /* Merge-sort a double linked case list, detecting overlap in the
7263 process. LIST is the head of the double linked case list before it
7264 is sorted. Returns the head of the sorted list if we don't see any
7265 overlap, or NULL otherwise. */
7268 check_case_overlap (gfc_case *list)
7270 gfc_case *p, *q, *e, *tail;
7271 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7273 /* If the passed list was empty, return immediately. */
7280 /* Loop unconditionally. The only exit from this loop is a return
7281 statement, when we've finished sorting the case list. */
7288 /* Count the number of merges we do in this pass. */
7291 /* Loop while there exists a merge to be done. */
7296 /* Count this merge. */
7299 /* Cut the list in two pieces by stepping INSIZE places
7300 forward in the list, starting from P. */
7303 for (i = 0; i < insize; i++)
7312 /* Now we have two lists. Merge them! */
7313 while (psize > 0 || (qsize > 0 && q != NULL))
7315 /* See from which the next case to merge comes from. */
7318 /* P is empty so the next case must come from Q. */
7323 else if (qsize == 0 || q == NULL)
7332 cmp = compare_cases (p, q);
7335 /* The whole case range for P is less than the
7343 /* The whole case range for Q is greater than
7344 the case range for P. */
7351 /* The cases overlap, or they are the same
7352 element in the list. Either way, we must
7353 issue an error and get the next case from P. */
7354 /* FIXME: Sort P and Q by line number. */
7355 gfc_error ("CASE label at %L overlaps with CASE "
7356 "label at %L", &p->where, &q->where);
7364 /* Add the next element to the merged list. */
7373 /* P has now stepped INSIZE places along, and so has Q. So
7374 they're the same. */
7379 /* If we have done only one merge or none at all, we've
7380 finished sorting the cases. */
7389 /* Otherwise repeat, merging lists twice the size. */
7395 /* Check to see if an expression is suitable for use in a CASE statement.
7396 Makes sure that all case expressions are scalar constants of the same
7397 type. Return false if anything is wrong. */
7400 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7402 if (e == NULL) return true;
7404 if (e->ts.type != case_expr->ts.type)
7406 gfc_error ("Expression in CASE statement at %L must be of type %s",
7407 &e->where, gfc_basic_typename (case_expr->ts.type));
7411 /* C805 (R808) For a given case-construct, each case-value shall be of
7412 the same type as case-expr. For character type, length differences
7413 are allowed, but the kind type parameters shall be the same. */
7415 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7417 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7418 &e->where, case_expr->ts.kind);
7422 /* Convert the case value kind to that of case expression kind,
7425 if (e->ts.kind != case_expr->ts.kind)
7426 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7430 gfc_error ("Expression in CASE statement at %L must be scalar",
7439 /* Given a completely parsed select statement, we:
7441 - Validate all expressions and code within the SELECT.
7442 - Make sure that the selection expression is not of the wrong type.
7443 - Make sure that no case ranges overlap.
7444 - Eliminate unreachable cases and unreachable code resulting from
7445 removing case labels.
7447 The standard does allow unreachable cases, e.g. CASE (5:3). But
7448 they are a hassle for code generation, and to prevent that, we just
7449 cut them out here. This is not necessary for overlapping cases
7450 because they are illegal and we never even try to generate code.
7452 We have the additional caveat that a SELECT construct could have
7453 been a computed GOTO in the source code. Fortunately we can fairly
7454 easily work around that here: The case_expr for a "real" SELECT CASE
7455 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7456 we have to do is make sure that the case_expr is a scalar integer
7460 resolve_select (gfc_code *code, bool select_type)
7463 gfc_expr *case_expr;
7464 gfc_case *cp, *default_case, *tail, *head;
7465 int seen_unreachable;
7471 if (code->expr1 == NULL)
7473 /* This was actually a computed GOTO statement. */
7474 case_expr = code->expr2;
7475 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7476 gfc_error ("Selection expression in computed GOTO statement "
7477 "at %L must be a scalar integer expression",
7480 /* Further checking is not necessary because this SELECT was built
7481 by the compiler, so it should always be OK. Just move the
7482 case_expr from expr2 to expr so that we can handle computed
7483 GOTOs as normal SELECTs from here on. */
7484 code->expr1 = code->expr2;
7489 case_expr = code->expr1;
7490 type = case_expr->ts.type;
7493 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7495 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7496 &case_expr->where, gfc_typename (&case_expr->ts));
7498 /* Punt. Going on here just produce more garbage error messages. */
7503 if (!select_type && case_expr->rank != 0)
7505 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7506 "expression", &case_expr->where);
7512 /* Raise a warning if an INTEGER case value exceeds the range of
7513 the case-expr. Later, all expressions will be promoted to the
7514 largest kind of all case-labels. */
7516 if (type == BT_INTEGER)
7517 for (body = code->block; body; body = body->block)
7518 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7521 && gfc_check_integer_range (cp->low->value.integer,
7522 case_expr->ts.kind) != ARITH_OK)
7523 gfc_warning ("Expression in CASE statement at %L is "
7524 "not in the range of %s", &cp->low->where,
7525 gfc_typename (&case_expr->ts));
7528 && cp->low != cp->high
7529 && gfc_check_integer_range (cp->high->value.integer,
7530 case_expr->ts.kind) != ARITH_OK)
7531 gfc_warning ("Expression in CASE statement at %L is "
7532 "not in the range of %s", &cp->high->where,
7533 gfc_typename (&case_expr->ts));
7536 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7537 of the SELECT CASE expression and its CASE values. Walk the lists
7538 of case values, and if we find a mismatch, promote case_expr to
7539 the appropriate kind. */
7541 if (type == BT_LOGICAL || type == BT_INTEGER)
7543 for (body = code->block; body; body = body->block)
7545 /* Walk the case label list. */
7546 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7548 /* Intercept the DEFAULT case. It does not have a kind. */
7549 if (cp->low == NULL && cp->high == NULL)
7552 /* Unreachable case ranges are discarded, so ignore. */
7553 if (cp->low != NULL && cp->high != NULL
7554 && cp->low != cp->high
7555 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7559 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7560 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7562 if (cp->high != NULL
7563 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7564 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7569 /* Assume there is no DEFAULT case. */
7570 default_case = NULL;
7575 for (body = code->block; body; body = body->block)
7577 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7579 seen_unreachable = 0;
7581 /* Walk the case label list, making sure that all case labels
7583 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7585 /* Count the number of cases in the whole construct. */
7588 /* Intercept the DEFAULT case. */
7589 if (cp->low == NULL && cp->high == NULL)
7591 if (default_case != NULL)
7593 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7594 "by a second DEFAULT CASE at %L",
7595 &default_case->where, &cp->where);
7606 /* Deal with single value cases and case ranges. Errors are
7607 issued from the validation function. */
7608 if (!validate_case_label_expr (cp->low, case_expr)
7609 || !validate_case_label_expr (cp->high, case_expr))
7615 if (type == BT_LOGICAL
7616 && ((cp->low == NULL || cp->high == NULL)
7617 || cp->low != cp->high))
7619 gfc_error ("Logical range in CASE statement at %L is not "
7620 "allowed", &cp->low->where);
7625 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7628 value = cp->low->value.logical == 0 ? 2 : 1;
7629 if (value & seen_logical)
7631 gfc_error ("Constant logical value in CASE statement "
7632 "is repeated at %L",
7637 seen_logical |= value;
7640 if (cp->low != NULL && cp->high != NULL
7641 && cp->low != cp->high
7642 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7644 if (gfc_option.warn_surprising)
7645 gfc_warning ("Range specification at %L can never "
7646 "be matched", &cp->where);
7648 cp->unreachable = 1;
7649 seen_unreachable = 1;
7653 /* If the case range can be matched, it can also overlap with
7654 other cases. To make sure it does not, we put it in a
7655 double linked list here. We sort that with a merge sort
7656 later on to detect any overlapping cases. */
7660 head->right = head->left = NULL;
7665 tail->right->left = tail;
7672 /* It there was a failure in the previous case label, give up
7673 for this case label list. Continue with the next block. */
7677 /* See if any case labels that are unreachable have been seen.
7678 If so, we eliminate them. This is a bit of a kludge because
7679 the case lists for a single case statement (label) is a
7680 single forward linked lists. */
7681 if (seen_unreachable)
7683 /* Advance until the first case in the list is reachable. */
7684 while (body->ext.block.case_list != NULL
7685 && body->ext.block.case_list->unreachable)
7687 gfc_case *n = body->ext.block.case_list;
7688 body->ext.block.case_list = body->ext.block.case_list->next;
7690 gfc_free_case_list (n);
7693 /* Strip all other unreachable cases. */
7694 if (body->ext.block.case_list)
7696 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7698 if (cp->next->unreachable)
7700 gfc_case *n = cp->next;
7701 cp->next = cp->next->next;
7703 gfc_free_case_list (n);
7710 /* See if there were overlapping cases. If the check returns NULL,
7711 there was overlap. In that case we don't do anything. If head
7712 is non-NULL, we prepend the DEFAULT case. The sorted list can
7713 then used during code generation for SELECT CASE constructs with
7714 a case expression of a CHARACTER type. */
7717 head = check_case_overlap (head);
7719 /* Prepend the default_case if it is there. */
7720 if (head != NULL && default_case)
7722 default_case->left = NULL;
7723 default_case->right = head;
7724 head->left = default_case;
7728 /* Eliminate dead blocks that may be the result if we've seen
7729 unreachable case labels for a block. */
7730 for (body = code; body && body->block; body = body->block)
7732 if (body->block->ext.block.case_list == NULL)
7734 /* Cut the unreachable block from the code chain. */
7735 gfc_code *c = body->block;
7736 body->block = c->block;
7738 /* Kill the dead block, but not the blocks below it. */
7740 gfc_free_statements (c);
7744 /* More than two cases is legal but insane for logical selects.
7745 Issue a warning for it. */
7746 if (gfc_option.warn_surprising && type == BT_LOGICAL
7748 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7753 /* Check if a derived type is extensible. */
7756 gfc_type_is_extensible (gfc_symbol *sym)
7758 return !(sym->attr.is_bind_c || sym->attr.sequence
7759 || (sym->attr.is_class
7760 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7764 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7765 correct as well as possibly the array-spec. */
7768 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7772 gcc_assert (sym->assoc);
7773 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7775 /* If this is for SELECT TYPE, the target may not yet be set. In that
7776 case, return. Resolution will be called later manually again when
7778 target = sym->assoc->target;
7781 gcc_assert (!sym->assoc->dangling);
7783 if (resolve_target && !gfc_resolve_expr (target))
7786 /* For variable targets, we get some attributes from the target. */
7787 if (target->expr_type == EXPR_VARIABLE)
7791 gcc_assert (target->symtree);
7792 tsym = target->symtree->n.sym;
7794 sym->attr.asynchronous = tsym->attr.asynchronous;
7795 sym->attr.volatile_ = tsym->attr.volatile_;
7797 sym->attr.target = tsym->attr.target
7798 || gfc_expr_attr (target).pointer;
7801 /* Get type if this was not already set. Note that it can be
7802 some other type than the target in case this is a SELECT TYPE
7803 selector! So we must not update when the type is already there. */
7804 if (sym->ts.type == BT_UNKNOWN)
7805 sym->ts = target->ts;
7806 gcc_assert (sym->ts.type != BT_UNKNOWN);
7808 /* See if this is a valid association-to-variable. */
7809 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7810 && !gfc_has_vector_subscript (target));
7812 /* Finally resolve if this is an array or not. */
7813 if (sym->attr.dimension && target->rank == 0)
7815 gfc_error ("Associate-name '%s' at %L is used as array",
7816 sym->name, &sym->declared_at);
7817 sym->attr.dimension = 0;
7821 /* We cannot deal with class selectors that need temporaries. */
7822 if (target->ts.type == BT_CLASS
7823 && gfc_ref_needs_temporary_p (target->ref))
7825 gfc_error ("CLASS selector at %L needs a temporary which is not "
7826 "yet implemented", &target->where);
7830 if (target->ts.type != BT_CLASS && target->rank > 0)
7831 sym->attr.dimension = 1;
7832 else if (target->ts.type == BT_CLASS)
7833 gfc_fix_class_refs (target);
7835 /* The associate-name will have a correct type by now. Make absolutely
7836 sure that it has not picked up a dimension attribute. */
7837 if (sym->ts.type == BT_CLASS)
7838 sym->attr.dimension = 0;
7840 if (sym->attr.dimension)
7842 sym->as = gfc_get_array_spec ();
7843 sym->as->rank = target->rank;
7844 sym->as->type = AS_DEFERRED;
7846 /* Target must not be coindexed, thus the associate-variable
7848 sym->as->corank = 0;
7851 /* Mark this as an associate variable. */
7852 sym->attr.associate_var = 1;
7854 /* If the target is a good class object, so is the associate variable. */
7855 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7856 sym->attr.class_ok = 1;
7860 /* Resolve a SELECT TYPE statement. */
7863 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7865 gfc_symbol *selector_type;
7866 gfc_code *body, *new_st, *if_st, *tail;
7867 gfc_code *class_is = NULL, *default_case = NULL;
7870 char name[GFC_MAX_SYMBOL_LEN];
7875 ns = code->ext.block.ns;
7878 /* Check for F03:C813. */
7879 if (code->expr1->ts.type != BT_CLASS
7880 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7882 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7883 "at %L", &code->loc);
7887 if (!code->expr1->symtree->n.sym->attr.class_ok)
7892 if (code->expr1->symtree->n.sym->attr.untyped)
7893 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7894 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7896 /* F2008: C803 The selector expression must not be coindexed. */
7897 if (gfc_is_coindexed (code->expr2))
7899 gfc_error ("Selector at %L must not be coindexed",
7900 &code->expr2->where);
7907 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7909 if (gfc_is_coindexed (code->expr1))
7911 gfc_error ("Selector at %L must not be coindexed",
7912 &code->expr1->where);
7917 /* Loop over TYPE IS / CLASS IS cases. */
7918 for (body = code->block; body; body = body->block)
7920 c = body->ext.block.case_list;
7922 /* Check F03:C815. */
7923 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7924 && !selector_type->attr.unlimited_polymorphic
7925 && !gfc_type_is_extensible (c->ts.u.derived))
7927 gfc_error ("Derived type '%s' at %L must be extensible",
7928 c->ts.u.derived->name, &c->where);
7933 /* Check F03:C816. */
7934 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7935 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7936 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7938 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7939 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7940 c->ts.u.derived->name, &c->where, selector_type->name);
7942 gfc_error ("Unexpected intrinsic type '%s' at %L",
7943 gfc_basic_typename (c->ts.type), &c->where);
7948 /* Check F03:C814. */
7949 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7951 gfc_error ("The type-spec at %L shall specify that each length "
7952 "type parameter is assumed", &c->where);
7957 /* Intercept the DEFAULT case. */
7958 if (c->ts.type == BT_UNKNOWN)
7960 /* Check F03:C818. */
7963 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7964 "by a second DEFAULT CASE at %L",
7965 &default_case->ext.block.case_list->where, &c->where);
7970 default_case = body;
7977 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7978 target if present. If there are any EXIT statements referring to the
7979 SELECT TYPE construct, this is no problem because the gfc_code
7980 reference stays the same and EXIT is equally possible from the BLOCK
7981 it is changed to. */
7982 code->op = EXEC_BLOCK;
7985 gfc_association_list* assoc;
7987 assoc = gfc_get_association_list ();
7988 assoc->st = code->expr1->symtree;
7989 assoc->target = gfc_copy_expr (code->expr2);
7990 assoc->target->where = code->expr2->where;
7991 /* assoc->variable will be set by resolve_assoc_var. */
7993 code->ext.block.assoc = assoc;
7994 code->expr1->symtree->n.sym->assoc = assoc;
7996 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7999 code->ext.block.assoc = NULL;
8001 /* Add EXEC_SELECT to switch on type. */
8002 new_st = gfc_get_code ();
8003 new_st->op = code->op;
8004 new_st->expr1 = code->expr1;
8005 new_st->expr2 = code->expr2;
8006 new_st->block = code->block;
8007 code->expr1 = code->expr2 = NULL;
8012 ns->code->next = new_st;
8014 code->op = EXEC_SELECT;
8016 gfc_add_vptr_component (code->expr1);
8017 gfc_add_hash_component (code->expr1);
8019 /* Loop over TYPE IS / CLASS IS cases. */
8020 for (body = code->block; body; body = body->block)
8022 c = body->ext.block.case_list;
8024 if (c->ts.type == BT_DERIVED)
8025 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8026 c->ts.u.derived->hash_value);
8027 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8032 ivtab = gfc_find_intrinsic_vtab (&c->ts);
8033 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8034 e = CLASS_DATA (ivtab)->initializer;
8035 c->low = c->high = gfc_copy_expr (e);
8038 else if (c->ts.type == BT_UNKNOWN)
8041 /* Associate temporary to selector. This should only be done
8042 when this case is actually true, so build a new ASSOCIATE
8043 that does precisely this here (instead of using the
8046 if (c->ts.type == BT_CLASS)
8047 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8048 else if (c->ts.type == BT_DERIVED)
8049 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8050 else if (c->ts.type == BT_CHARACTER)
8052 if (c->ts.u.cl && c->ts.u.cl->length
8053 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8054 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8055 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8056 charlen, c->ts.kind);
8059 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8062 st = gfc_find_symtree (ns->sym_root, name);
8063 gcc_assert (st->n.sym->assoc);
8064 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8065 st->n.sym->assoc->target->where = code->expr1->where;
8066 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8067 gfc_add_data_component (st->n.sym->assoc->target);
8069 new_st = gfc_get_code ();
8070 new_st->op = EXEC_BLOCK;
8071 new_st->ext.block.ns = gfc_build_block_ns (ns);
8072 new_st->ext.block.ns->code = body->next;
8073 body->next = new_st;
8075 /* Chain in the new list only if it is marked as dangling. Otherwise
8076 there is a CASE label overlap and this is already used. Just ignore,
8077 the error is diagnosed elsewhere. */
8078 if (st->n.sym->assoc->dangling)
8080 new_st->ext.block.assoc = st->n.sym->assoc;
8081 st->n.sym->assoc->dangling = 0;
8084 resolve_assoc_var (st->n.sym, false);
8087 /* Take out CLASS IS cases for separate treatment. */
8089 while (body && body->block)
8091 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8093 /* Add to class_is list. */
8094 if (class_is == NULL)
8096 class_is = body->block;
8101 for (tail = class_is; tail->block; tail = tail->block) ;
8102 tail->block = body->block;
8105 /* Remove from EXEC_SELECT list. */
8106 body->block = body->block->block;
8119 /* Add a default case to hold the CLASS IS cases. */
8120 for (tail = code; tail->block; tail = tail->block) ;
8121 tail->block = gfc_get_code ();
8123 tail->op = EXEC_SELECT_TYPE;
8124 tail->ext.block.case_list = gfc_get_case ();
8125 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8127 default_case = tail;
8130 /* More than one CLASS IS block? */
8131 if (class_is->block)
8135 /* Sort CLASS IS blocks by extension level. */
8139 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8142 /* F03:C817 (check for doubles). */
8143 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8144 == c2->ext.block.case_list->ts.u.derived->hash_value)
8146 gfc_error ("Double CLASS IS block in SELECT TYPE "
8148 &c2->ext.block.case_list->where);
8151 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8152 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8155 (*c1)->block = c2->block;
8165 /* Generate IF chain. */
8166 if_st = gfc_get_code ();
8167 if_st->op = EXEC_IF;
8169 for (body = class_is; body; body = body->block)
8171 new_st->block = gfc_get_code ();
8172 new_st = new_st->block;
8173 new_st->op = EXEC_IF;
8174 /* Set up IF condition: Call _gfortran_is_extension_of. */
8175 new_st->expr1 = gfc_get_expr ();
8176 new_st->expr1->expr_type = EXPR_FUNCTION;
8177 new_st->expr1->ts.type = BT_LOGICAL;
8178 new_st->expr1->ts.kind = 4;
8179 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8180 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8181 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8182 /* Set up arguments. */
8183 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8184 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8185 new_st->expr1->value.function.actual->expr->where = code->loc;
8186 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8187 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8188 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8189 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8190 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8191 new_st->next = body->next;
8193 if (default_case->next)
8195 new_st->block = gfc_get_code ();
8196 new_st = new_st->block;
8197 new_st->op = EXEC_IF;
8198 new_st->next = default_case->next;
8201 /* Replace CLASS DEFAULT code by the IF chain. */
8202 default_case->next = if_st;
8205 /* Resolve the internal code. This can not be done earlier because
8206 it requires that the sym->assoc of selectors is set already. */
8207 gfc_current_ns = ns;
8208 gfc_resolve_blocks (code->block, gfc_current_ns);
8209 gfc_current_ns = old_ns;
8211 resolve_select (code, true);
8215 /* Resolve a transfer statement. This is making sure that:
8216 -- a derived type being transferred has only non-pointer components
8217 -- a derived type being transferred doesn't have private components, unless
8218 it's being transferred from the module where the type was defined
8219 -- we're not trying to transfer a whole assumed size array. */
8222 resolve_transfer (gfc_code *code)
8231 while (exp != NULL && exp->expr_type == EXPR_OP
8232 && exp->value.op.op == INTRINSIC_PARENTHESES)
8233 exp = exp->value.op.op1;
8235 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8237 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8238 "MOLD=", &exp->where);
8242 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8243 && exp->expr_type != EXPR_FUNCTION))
8246 /* If we are reading, the variable will be changed. Note that
8247 code->ext.dt may be NULL if the TRANSFER is related to
8248 an INQUIRE statement -- but in this case, we are not reading, either. */
8249 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8250 && !gfc_check_vardef_context (exp, false, false, false,
8254 sym = exp->symtree->n.sym;
8257 /* Go to actual component transferred. */
8258 for (ref = exp->ref; ref; ref = ref->next)
8259 if (ref->type == REF_COMPONENT)
8260 ts = &ref->u.c.component->ts;
8262 if (ts->type == BT_CLASS)
8264 /* FIXME: Test for defined input/output. */
8265 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8266 "it is processed by a defined input/output procedure",
8271 if (ts->type == BT_DERIVED)
8273 /* Check that transferred derived type doesn't contain POINTER
8275 if (ts->u.derived->attr.pointer_comp)
8277 gfc_error ("Data transfer element at %L cannot have POINTER "
8278 "components unless it is processed by a defined "
8279 "input/output procedure", &code->loc);
8284 if (ts->u.derived->attr.proc_pointer_comp)
8286 gfc_error ("Data transfer element at %L cannot have "
8287 "procedure pointer components", &code->loc);
8291 if (ts->u.derived->attr.alloc_comp)
8293 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8294 "components unless it is processed by a defined "
8295 "input/output procedure", &code->loc);
8299 /* C_PTR and C_FUNPTR have private components which means they can not
8300 be printed. However, if -std=gnu and not -pedantic, allow
8301 the component to be printed to help debugging. */
8302 if (ts->u.derived->ts.f90_type == BT_VOID)
8304 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8305 "cannot have PRIVATE components", &code->loc))
8308 else if (derived_inaccessible (ts->u.derived))
8310 gfc_error ("Data transfer element at %L cannot have "
8311 "PRIVATE components",&code->loc);
8316 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8317 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8319 gfc_error ("Data transfer element at %L cannot be a full reference to "
8320 "an assumed-size array", &code->loc);
8326 /*********** Toplevel code resolution subroutines ***********/
8328 /* Find the set of labels that are reachable from this block. We also
8329 record the last statement in each block. */
8332 find_reachable_labels (gfc_code *block)
8339 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8341 /* Collect labels in this block. We don't keep those corresponding
8342 to END {IF|SELECT}, these are checked in resolve_branch by going
8343 up through the code_stack. */
8344 for (c = block; c; c = c->next)
8346 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8347 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8350 /* Merge with labels from parent block. */
8353 gcc_assert (cs_base->prev->reachable_labels);
8354 bitmap_ior_into (cs_base->reachable_labels,
8355 cs_base->prev->reachable_labels);
8361 resolve_lock_unlock (gfc_code *code)
8363 if (code->expr1->ts.type != BT_DERIVED
8364 || code->expr1->expr_type != EXPR_VARIABLE
8365 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8366 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8367 || code->expr1->rank != 0
8368 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8369 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8370 &code->expr1->where);
8374 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8375 || code->expr2->expr_type != EXPR_VARIABLE))
8376 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8377 &code->expr2->where);
8380 && !gfc_check_vardef_context (code->expr2, false, false, false,
8381 _("STAT variable")))
8386 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8387 || code->expr3->expr_type != EXPR_VARIABLE))
8388 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8389 &code->expr3->where);
8392 && !gfc_check_vardef_context (code->expr3, false, false, false,
8393 _("ERRMSG variable")))
8396 /* Check ACQUIRED_LOCK. */
8398 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8399 || code->expr4->expr_type != EXPR_VARIABLE))
8400 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8401 "variable", &code->expr4->where);
8404 && !gfc_check_vardef_context (code->expr4, false, false, false,
8405 _("ACQUIRED_LOCK variable")))
8411 resolve_sync (gfc_code *code)
8413 /* Check imageset. The * case matches expr1 == NULL. */
8416 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8417 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8418 "INTEGER expression", &code->expr1->where);
8419 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8420 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8421 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8422 &code->expr1->where);
8423 else if (code->expr1->expr_type == EXPR_ARRAY
8424 && gfc_simplify_expr (code->expr1, 0))
8426 gfc_constructor *cons;
8427 cons = gfc_constructor_first (code->expr1->value.constructor);
8428 for (; cons; cons = gfc_constructor_next (cons))
8429 if (cons->expr->expr_type == EXPR_CONSTANT
8430 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8431 gfc_error ("Imageset argument at %L must between 1 and "
8432 "num_images()", &cons->expr->where);
8438 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8439 || code->expr2->expr_type != EXPR_VARIABLE))
8440 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8441 &code->expr2->where);
8445 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8446 || code->expr3->expr_type != EXPR_VARIABLE))
8447 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8448 &code->expr3->where);
8452 /* Given a branch to a label, see if the branch is conforming.
8453 The code node describes where the branch is located. */
8456 resolve_branch (gfc_st_label *label, gfc_code *code)
8463 /* Step one: is this a valid branching target? */
8465 if (label->defined == ST_LABEL_UNKNOWN)
8467 gfc_error ("Label %d referenced at %L is never defined", label->value,
8472 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8474 gfc_error ("Statement at %L is not a valid branch target statement "
8475 "for the branch statement at %L", &label->where, &code->loc);
8479 /* Step two: make sure this branch is not a branch to itself ;-) */
8481 if (code->here == label)
8483 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8487 /* Step three: See if the label is in the same block as the
8488 branching statement. The hard work has been done by setting up
8489 the bitmap reachable_labels. */
8491 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8493 /* Check now whether there is a CRITICAL construct; if so, check
8494 whether the label is still visible outside of the CRITICAL block,
8495 which is invalid. */
8496 for (stack = cs_base; stack; stack = stack->prev)
8498 if (stack->current->op == EXEC_CRITICAL
8499 && bitmap_bit_p (stack->reachable_labels, label->value))
8500 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8501 "label at %L", &code->loc, &label->where);
8502 else if (stack->current->op == EXEC_DO_CONCURRENT
8503 && bitmap_bit_p (stack->reachable_labels, label->value))
8504 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8505 "for label at %L", &code->loc, &label->where);
8511 /* Step four: If we haven't found the label in the bitmap, it may
8512 still be the label of the END of the enclosing block, in which
8513 case we find it by going up the code_stack. */
8515 for (stack = cs_base; stack; stack = stack->prev)
8517 if (stack->current->next && stack->current->next->here == label)
8519 if (stack->current->op == EXEC_CRITICAL)
8521 /* Note: A label at END CRITICAL does not leave the CRITICAL
8522 construct as END CRITICAL is still part of it. */
8523 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8524 " at %L", &code->loc, &label->where);
8527 else if (stack->current->op == EXEC_DO_CONCURRENT)
8529 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8530 "label at %L", &code->loc, &label->where);
8537 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8541 /* The label is not in an enclosing block, so illegal. This was
8542 allowed in Fortran 66, so we allow it as extension. No
8543 further checks are necessary in this case. */
8544 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8545 "as the GOTO statement at %L", &label->where,
8551 /* Check whether EXPR1 has the same shape as EXPR2. */
8554 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8556 mpz_t shape[GFC_MAX_DIMENSIONS];
8557 mpz_t shape2[GFC_MAX_DIMENSIONS];
8558 bool result = false;
8561 /* Compare the rank. */
8562 if (expr1->rank != expr2->rank)
8565 /* Compare the size of each dimension. */
8566 for (i=0; i<expr1->rank; i++)
8568 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8571 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8574 if (mpz_cmp (shape[i], shape2[i]))
8578 /* When either of the two expression is an assumed size array, we
8579 ignore the comparison of dimension sizes. */
8584 gfc_clear_shape (shape, i);
8585 gfc_clear_shape (shape2, i);
8590 /* Check whether a WHERE assignment target or a WHERE mask expression
8591 has the same shape as the outmost WHERE mask expression. */
8594 resolve_where (gfc_code *code, gfc_expr *mask)
8600 cblock = code->block;
8602 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8603 In case of nested WHERE, only the outmost one is stored. */
8604 if (mask == NULL) /* outmost WHERE */
8606 else /* inner WHERE */
8613 /* Check if the mask-expr has a consistent shape with the
8614 outmost WHERE mask-expr. */
8615 if (!resolve_where_shape (cblock->expr1, e))
8616 gfc_error ("WHERE mask at %L has inconsistent shape",
8617 &cblock->expr1->where);
8620 /* the assignment statement of a WHERE statement, or the first
8621 statement in where-body-construct of a WHERE construct */
8622 cnext = cblock->next;
8627 /* WHERE assignment statement */
8630 /* Check shape consistent for WHERE assignment target. */
8631 if (e && !resolve_where_shape (cnext->expr1, e))
8632 gfc_error ("WHERE assignment target at %L has "
8633 "inconsistent shape", &cnext->expr1->where);
8637 case EXEC_ASSIGN_CALL:
8638 resolve_call (cnext);
8639 if (!cnext->resolved_sym->attr.elemental)
8640 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8641 &cnext->ext.actual->expr->where);
8644 /* WHERE or WHERE construct is part of a where-body-construct */
8646 resolve_where (cnext, e);
8650 gfc_error ("Unsupported statement inside WHERE at %L",
8653 /* the next statement within the same where-body-construct */
8654 cnext = cnext->next;
8656 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8657 cblock = cblock->block;
8662 /* Resolve assignment in FORALL construct.
8663 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8664 FORALL index variables. */
8667 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8671 for (n = 0; n < nvar; n++)
8673 gfc_symbol *forall_index;
8675 forall_index = var_expr[n]->symtree->n.sym;
8677 /* Check whether the assignment target is one of the FORALL index
8679 if ((code->expr1->expr_type == EXPR_VARIABLE)
8680 && (code->expr1->symtree->n.sym == forall_index))
8681 gfc_error ("Assignment to a FORALL index variable at %L",
8682 &code->expr1->where);
8685 /* If one of the FORALL index variables doesn't appear in the
8686 assignment variable, then there could be a many-to-one
8687 assignment. Emit a warning rather than an error because the
8688 mask could be resolving this problem. */
8689 if (!find_forall_index (code->expr1, forall_index, 0))
8690 gfc_warning ("The FORALL with index '%s' is not used on the "
8691 "left side of the assignment at %L and so might "
8692 "cause multiple assignment to this object",
8693 var_expr[n]->symtree->name, &code->expr1->where);
8699 /* Resolve WHERE statement in FORALL construct. */
8702 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8703 gfc_expr **var_expr)
8708 cblock = code->block;
8711 /* the assignment statement of a WHERE statement, or the first
8712 statement in where-body-construct of a WHERE construct */
8713 cnext = cblock->next;
8718 /* WHERE assignment statement */
8720 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8723 /* WHERE operator assignment statement */
8724 case EXEC_ASSIGN_CALL:
8725 resolve_call (cnext);
8726 if (!cnext->resolved_sym->attr.elemental)
8727 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8728 &cnext->ext.actual->expr->where);
8731 /* WHERE or WHERE construct is part of a where-body-construct */
8733 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8737 gfc_error ("Unsupported statement inside WHERE at %L",
8740 /* the next statement within the same where-body-construct */
8741 cnext = cnext->next;
8743 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8744 cblock = cblock->block;
8749 /* Traverse the FORALL body to check whether the following errors exist:
8750 1. For assignment, check if a many-to-one assignment happens.
8751 2. For WHERE statement, check the WHERE body to see if there is any
8752 many-to-one assignment. */
8755 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8759 c = code->block->next;
8765 case EXEC_POINTER_ASSIGN:
8766 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8769 case EXEC_ASSIGN_CALL:
8773 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8774 there is no need to handle it here. */
8778 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8783 /* The next statement in the FORALL body. */
8789 /* Counts the number of iterators needed inside a forall construct, including
8790 nested forall constructs. This is used to allocate the needed memory
8791 in gfc_resolve_forall. */
8794 gfc_count_forall_iterators (gfc_code *code)
8796 int max_iters, sub_iters, current_iters;
8797 gfc_forall_iterator *fa;
8799 gcc_assert(code->op == EXEC_FORALL);
8803 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8806 code = code->block->next;
8810 if (code->op == EXEC_FORALL)
8812 sub_iters = gfc_count_forall_iterators (code);
8813 if (sub_iters > max_iters)
8814 max_iters = sub_iters;
8819 return current_iters + max_iters;
8823 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8824 gfc_resolve_forall_body to resolve the FORALL body. */
8827 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8829 static gfc_expr **var_expr;
8830 static int total_var = 0;
8831 static int nvar = 0;
8833 gfc_forall_iterator *fa;
8838 /* Start to resolve a FORALL construct */
8839 if (forall_save == 0)
8841 /* Count the total number of FORALL index in the nested FORALL
8842 construct in order to allocate the VAR_EXPR with proper size. */
8843 total_var = gfc_count_forall_iterators (code);
8845 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8846 var_expr = XCNEWVEC (gfc_expr *, total_var);
8849 /* The information about FORALL iterator, including FORALL index start, end
8850 and stride. The FORALL index can not appear in start, end or stride. */
8851 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8853 /* Check if any outer FORALL index name is the same as the current
8855 for (i = 0; i < nvar; i++)
8857 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8859 gfc_error ("An outer FORALL construct already has an index "
8860 "with this name %L", &fa->var->where);
8864 /* Record the current FORALL index. */
8865 var_expr[nvar] = gfc_copy_expr (fa->var);
8869 /* No memory leak. */
8870 gcc_assert (nvar <= total_var);
8873 /* Resolve the FORALL body. */
8874 gfc_resolve_forall_body (code, nvar, var_expr);
8876 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8877 gfc_resolve_blocks (code->block, ns);
8881 /* Free only the VAR_EXPRs allocated in this frame. */
8882 for (i = nvar; i < tmp; i++)
8883 gfc_free_expr (var_expr[i]);
8887 /* We are in the outermost FORALL construct. */
8888 gcc_assert (forall_save == 0);
8890 /* VAR_EXPR is not needed any more. */
8897 /* Resolve a BLOCK construct statement. */
8900 resolve_block_construct (gfc_code* code)
8902 /* Resolve the BLOCK's namespace. */
8903 gfc_resolve (code->ext.block.ns);
8905 /* For an ASSOCIATE block, the associations (and their targets) are already
8906 resolved during resolve_symbol. */
8910 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8913 static void resolve_code (gfc_code *, gfc_namespace *);
8916 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8920 for (; b; b = b->block)
8922 t = gfc_resolve_expr (b->expr1);
8923 if (!gfc_resolve_expr (b->expr2))
8929 if (t && b->expr1 != NULL
8930 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8931 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8938 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8939 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8944 resolve_branch (b->label1, b);
8948 resolve_block_construct (b);
8952 case EXEC_SELECT_TYPE:
8956 case EXEC_DO_CONCURRENT:
8964 case EXEC_OMP_ATOMIC:
8965 case EXEC_OMP_CRITICAL:
8967 case EXEC_OMP_MASTER:
8968 case EXEC_OMP_ORDERED:
8969 case EXEC_OMP_PARALLEL:
8970 case EXEC_OMP_PARALLEL_DO:
8971 case EXEC_OMP_PARALLEL_SECTIONS:
8972 case EXEC_OMP_PARALLEL_WORKSHARE:
8973 case EXEC_OMP_SECTIONS:
8974 case EXEC_OMP_SINGLE:
8976 case EXEC_OMP_TASKWAIT:
8977 case EXEC_OMP_TASKYIELD:
8978 case EXEC_OMP_WORKSHARE:
8982 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8985 resolve_code (b->next, ns);
8990 /* Does everything to resolve an ordinary assignment. Returns true
8991 if this is an interface assignment. */
8993 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9003 if (gfc_extend_assign (code, ns))
9007 if (code->op == EXEC_ASSIGN_CALL)
9009 lhs = code->ext.actual->expr;
9010 rhsptr = &code->ext.actual->next->expr;
9014 gfc_actual_arglist* args;
9015 gfc_typebound_proc* tbp;
9017 gcc_assert (code->op == EXEC_COMPCALL);
9019 args = code->expr1->value.compcall.actual;
9021 rhsptr = &args->next->expr;
9023 tbp = code->expr1->value.compcall.tbp;
9024 gcc_assert (!tbp->is_generic);
9027 /* Make a temporary rhs when there is a default initializer
9028 and rhs is the same symbol as the lhs. */
9029 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9030 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9031 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9032 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9033 *rhsptr = gfc_get_parentheses (*rhsptr);
9042 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9043 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9047 /* Handle the case of a BOZ literal on the RHS. */
9048 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9051 if (gfc_option.warn_surprising)
9052 gfc_warning ("BOZ literal at %L is bitwise transferred "
9053 "non-integer symbol '%s'", &code->loc,
9054 lhs->symtree->n.sym->name);
9056 if (!gfc_convert_boz (rhs, &lhs->ts))
9058 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9060 if (rc == ARITH_UNDERFLOW)
9061 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9062 ". This check can be disabled with the option "
9063 "-fno-range-check", &rhs->where);
9064 else if (rc == ARITH_OVERFLOW)
9065 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9066 ". This check can be disabled with the option "
9067 "-fno-range-check", &rhs->where);
9068 else if (rc == ARITH_NAN)
9069 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9070 ". This check can be disabled with the option "
9071 "-fno-range-check", &rhs->where);
9076 if (lhs->ts.type == BT_CHARACTER
9077 && gfc_option.warn_character_truncation)
9079 if (lhs->ts.u.cl != NULL
9080 && lhs->ts.u.cl->length != NULL
9081 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9082 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9084 if (rhs->expr_type == EXPR_CONSTANT)
9085 rlen = rhs->value.character.length;
9087 else if (rhs->ts.u.cl != NULL
9088 && rhs->ts.u.cl->length != NULL
9089 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9090 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9092 if (rlen && llen && rlen > llen)
9093 gfc_warning_now ("CHARACTER expression will be truncated "
9094 "in assignment (%d/%d) at %L",
9095 llen, rlen, &code->loc);
9098 /* Ensure that a vector index expression for the lvalue is evaluated
9099 to a temporary if the lvalue symbol is referenced in it. */
9102 for (ref = lhs->ref; ref; ref= ref->next)
9103 if (ref->type == REF_ARRAY)
9105 for (n = 0; n < ref->u.ar.dimen; n++)
9106 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9107 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9108 ref->u.ar.start[n]))
9110 = gfc_get_parentheses (ref->u.ar.start[n]);
9114 if (gfc_pure (NULL))
9116 if (lhs->ts.type == BT_DERIVED
9117 && lhs->expr_type == EXPR_VARIABLE
9118 && lhs->ts.u.derived->attr.pointer_comp
9119 && rhs->expr_type == EXPR_VARIABLE
9120 && (gfc_impure_variable (rhs->symtree->n.sym)
9121 || gfc_is_coindexed (rhs)))
9124 if (gfc_is_coindexed (rhs))
9125 gfc_error ("Coindexed expression at %L is assigned to "
9126 "a derived type variable with a POINTER "
9127 "component in a PURE procedure",
9130 gfc_error ("The impure variable at %L is assigned to "
9131 "a derived type variable with a POINTER "
9132 "component in a PURE procedure (12.6)",
9137 /* Fortran 2008, C1283. */
9138 if (gfc_is_coindexed (lhs))
9140 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9141 "procedure", &rhs->where);
9146 if (gfc_implicit_pure (NULL))
9148 if (lhs->expr_type == EXPR_VARIABLE
9149 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9150 && lhs->symtree->n.sym->ns != gfc_current_ns)
9151 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9153 if (lhs->ts.type == BT_DERIVED
9154 && lhs->expr_type == EXPR_VARIABLE
9155 && lhs->ts.u.derived->attr.pointer_comp
9156 && rhs->expr_type == EXPR_VARIABLE
9157 && (gfc_impure_variable (rhs->symtree->n.sym)
9158 || gfc_is_coindexed (rhs)))
9159 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9161 /* Fortran 2008, C1283. */
9162 if (gfc_is_coindexed (lhs))
9163 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9167 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9168 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9169 if (lhs->ts.type == BT_CLASS)
9171 gfc_error ("Variable must not be polymorphic in intrinsic assignment at "
9172 "%L - check that there is a matching specific subroutine "
9173 "for '=' operator", &lhs->where);
9177 /* F2008, Section 7.2.1.2. */
9178 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9180 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9181 "component in assignment at %L", &lhs->where);
9185 gfc_check_assign (lhs, rhs, 1);
9190 /* Add a component reference onto an expression. */
9193 add_comp_ref (gfc_expr *e, gfc_component *c)
9198 ref = &((*ref)->next);
9199 *ref = gfc_get_ref ();
9200 (*ref)->type = REF_COMPONENT;
9201 (*ref)->u.c.sym = e->ts.u.derived;
9202 (*ref)->u.c.component = c;
9205 /* Add a full array ref, as necessary. */
9208 gfc_add_full_array_ref (e, c->as);
9209 e->rank = c->as->rank;
9214 /* Build an assignment. Keep the argument 'op' for future use, so that
9215 pointer assignments can be made. */
9218 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9219 gfc_component *comp1, gfc_component *comp2, locus loc)
9221 gfc_code *this_code;
9223 this_code = gfc_get_code ();
9225 this_code->next = NULL;
9226 this_code->expr1 = gfc_copy_expr (expr1);
9227 this_code->expr2 = gfc_copy_expr (expr2);
9228 this_code->loc = loc;
9231 add_comp_ref (this_code->expr1, comp1);
9232 add_comp_ref (this_code->expr2, comp2);
9239 /* Makes a temporary variable expression based on the characteristics of
9240 a given variable expression. */
9243 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9245 static int serial = 0;
9246 char name[GFC_MAX_SYMBOL_LEN];
9249 gfc_array_ref *aref;
9252 sprintf (name, "DA@%d", serial++);
9253 gfc_get_sym_tree (name, ns, &tmp, false);
9254 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9260 /* This function could be expanded to support other expression type
9261 but this is not needed here. */
9262 gcc_assert (e->expr_type == EXPR_VARIABLE);
9264 /* Obtain the arrayspec for the temporary. */
9267 aref = gfc_find_array_ref (e);
9268 if (e->expr_type == EXPR_VARIABLE
9269 && e->symtree->n.sym->as == aref->as)
9273 for (ref = e->ref; ref; ref = ref->next)
9274 if (ref->type == REF_COMPONENT
9275 && ref->u.c.component->as == aref->as)
9283 /* Add the attributes and the arrayspec to the temporary. */
9284 tmp->n.sym->attr = gfc_expr_attr (e);
9287 tmp->n.sym->as = gfc_copy_array_spec (as);
9290 if (as->type == AS_DEFERRED)
9291 tmp->n.sym->attr.allocatable = 1;
9294 tmp->n.sym->attr.dimension = 0;
9296 gfc_set_sym_referenced (tmp->n.sym);
9297 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
9298 e = gfc_lval_expr_from_sym (tmp->n.sym);
9300 /* Should the lhs be a section, use its array ref for the
9301 temporary expression. */
9302 if (aref && aref->type != AR_FULL)
9304 gfc_free_ref_list (e->ref);
9305 e->ref = gfc_copy_ref (ref);
9311 /* Add one line of code to the code chain, making sure that 'head' and
9312 'tail' are appropriately updated. */
9315 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9317 gcc_assert (this_code);
9319 *head = *tail = *this_code;
9321 *tail = gfc_append_code (*tail, *this_code);
9326 /* Counts the potential number of part array references that would
9327 result from resolution of typebound defined assignments. */
9330 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9333 int c_depth = 0, t_depth;
9335 for (c= derived->components; c; c = c->next)
9337 if ((c->ts.type != BT_DERIVED
9339 || c->attr.allocatable
9340 || c->attr.proc_pointer_comp
9341 || c->attr.class_pointer
9342 || c->attr.proc_pointer)
9343 && !c->attr.defined_assign_comp)
9346 if (c->as && c_depth == 0)
9349 if (c->ts.u.derived->attr.defined_assign_comp)
9350 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9355 c_depth = t_depth > c_depth ? t_depth : c_depth;
9357 return depth + c_depth;
9361 /* Implement 7.2.1.3 of the F08 standard:
9362 "An intrinsic assignment where the variable is of derived type is
9363 performed as if each component of the variable were assigned from the
9364 corresponding component of expr using pointer assignment (7.2.2) for
9365 each pointer component, defined assignment for each nonpointer
9366 nonallocatable component of a type that has a type-bound defined
9367 assignment consistent with the component, intrinsic assignment for
9368 each other nonpointer nonallocatable component, ..."
9370 The pointer assignments are taken care of by the intrinsic
9371 assignment of the structure itself. This function recursively adds
9372 defined assignments where required. The recursion is accomplished
9373 by calling resolve_code.
9375 When the lhs in a defined assignment has intent INOUT, we need a
9376 temporary for the lhs. In pseudo-code:
9378 ! Only call function lhs once.
9379 if (lhs is not a constant or an variable)
9382 ! Do the intrinsic assignment
9384 ! Now do the defined assignments
9385 do over components with typebound defined assignment [%cmp]
9386 #if one component's assignment procedure is INOUT
9388 #if expr2 non-variable
9394 t1%cmp {defined=} expr2%cmp
9400 expr1%cmp {defined=} expr2%cmp
9404 /* The temporary assignments have to be put on top of the additional
9405 code to avoid the result being changed by the intrinsic assignment.
9407 static int component_assignment_level = 0;
9408 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9411 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9413 gfc_component *comp1, *comp2;
9414 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9416 int error_count, depth;
9418 gfc_get_errors (NULL, &error_count);
9420 /* Filter out continuing processing after an error. */
9422 || (*code)->expr1->ts.type != BT_DERIVED
9423 || (*code)->expr2->ts.type != BT_DERIVED)
9426 /* TODO: Handle more than one part array reference in assignments. */
9427 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9428 (*code)->expr1->rank ? 1 : 0);
9431 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9432 "done because multiple part array references would "
9433 "occur in intermediate expressions.", &(*code)->loc);
9437 component_assignment_level++;
9439 /* Create a temporary so that functions get called only once. */
9440 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9441 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9445 /* Assign the rhs to the temporary. */
9446 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9447 this_code = build_assignment (EXEC_ASSIGN,
9448 tmp_expr, (*code)->expr2,
9449 NULL, NULL, (*code)->loc);
9450 /* Add the code and substitute the rhs expression. */
9451 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9452 gfc_free_expr ((*code)->expr2);
9453 (*code)->expr2 = tmp_expr;
9456 /* Do the intrinsic assignment. This is not needed if the lhs is one
9457 of the temporaries generated here, since the intrinsic assignment
9458 to the final result already does this. */
9459 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9461 this_code = build_assignment (EXEC_ASSIGN,
9462 (*code)->expr1, (*code)->expr2,
9463 NULL, NULL, (*code)->loc);
9464 add_code_to_chain (&this_code, &head, &tail);
9467 comp1 = (*code)->expr1->ts.u.derived->components;
9468 comp2 = (*code)->expr2->ts.u.derived->components;
9471 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9475 /* The intrinsic assignment does the right thing for pointers
9476 of all kinds and allocatable components. */
9477 if (comp1->ts.type != BT_DERIVED
9478 || comp1->attr.pointer
9479 || comp1->attr.allocatable
9480 || comp1->attr.proc_pointer_comp
9481 || comp1->attr.class_pointer
9482 || comp1->attr.proc_pointer)
9485 /* Make an assigment for this component. */
9486 this_code = build_assignment (EXEC_ASSIGN,
9487 (*code)->expr1, (*code)->expr2,
9488 comp1, comp2, (*code)->loc);
9490 /* Convert the assignment if there is a defined assignment for
9491 this type. Otherwise, using the call from resolve_code,
9492 recurse into its components. */
9493 resolve_code (this_code, ns);
9495 if (this_code->op == EXEC_ASSIGN_CALL)
9497 gfc_formal_arglist *dummy_args;
9499 /* Check that there is a typebound defined assignment. If not,
9500 then this must be a module defined assignment. We cannot
9501 use the defined_assign_comp attribute here because it must
9502 be this derived type that has the defined assignment and not
9504 if (!(comp1->ts.u.derived->f2k_derived
9505 && comp1->ts.u.derived->f2k_derived
9506 ->tb_op[INTRINSIC_ASSIGN]))
9508 gfc_free_statements (this_code);
9513 /* If the first argument of the subroutine has intent INOUT
9514 a temporary must be generated and used instead. */
9515 rsym = this_code->resolved_sym;
9516 dummy_args = gfc_sym_get_dummy_args (rsym);
9518 && dummy_args->sym->attr.intent == INTENT_INOUT)
9520 gfc_code *temp_code;
9523 /* Build the temporary required for the assignment and put
9524 it at the head of the generated code. */
9527 t1 = get_temp_from_expr ((*code)->expr1, ns);
9528 temp_code = build_assignment (EXEC_ASSIGN,
9530 NULL, NULL, (*code)->loc);
9531 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9534 /* Replace the first actual arg with the component of the
9536 gfc_free_expr (this_code->ext.actual->expr);
9537 this_code->ext.actual->expr = gfc_copy_expr (t1);
9538 add_comp_ref (this_code->ext.actual->expr, comp1);
9541 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9543 /* Don't add intrinsic assignments since they are already
9544 effected by the intrinsic assignment of the structure. */
9545 gfc_free_statements (this_code);
9550 add_code_to_chain (&this_code, &head, &tail);
9554 /* Transfer the value to the final result. */
9555 this_code = build_assignment (EXEC_ASSIGN,
9557 comp1, comp2, (*code)->loc);
9558 add_code_to_chain (&this_code, &head, &tail);
9562 /* This is probably not necessary. */
9565 gfc_free_statements (this_code);
9569 /* Put the temporary assignments at the top of the generated code. */
9570 if (tmp_head && component_assignment_level == 1)
9572 gfc_append_code (tmp_head, head);
9574 tmp_head = tmp_tail = NULL;
9577 /* Now attach the remaining code chain to the input code. Step on
9578 to the end of the new code since resolution is complete. */
9579 gcc_assert ((*code)->op == EXEC_ASSIGN);
9580 tail->next = (*code)->next;
9581 /* Overwrite 'code' because this would place the intrinsic assignment
9582 before the temporary for the lhs is created. */
9583 gfc_free_expr ((*code)->expr1);
9584 gfc_free_expr ((*code)->expr2);
9589 component_assignment_level--;
9593 /* Given a block of code, recursively resolve everything pointed to by this
9597 resolve_code (gfc_code *code, gfc_namespace *ns)
9599 int omp_workshare_save;
9600 int forall_save, do_concurrent_save;
9604 frame.prev = cs_base;
9608 find_reachable_labels (code);
9610 for (; code; code = code->next)
9612 frame.current = code;
9613 forall_save = forall_flag;
9614 do_concurrent_save = do_concurrent_flag;
9616 if (code->op == EXEC_FORALL)
9619 gfc_resolve_forall (code, ns, forall_save);
9622 else if (code->block)
9624 omp_workshare_save = -1;
9627 case EXEC_OMP_PARALLEL_WORKSHARE:
9628 omp_workshare_save = omp_workshare_flag;
9629 omp_workshare_flag = 1;
9630 gfc_resolve_omp_parallel_blocks (code, ns);
9632 case EXEC_OMP_PARALLEL:
9633 case EXEC_OMP_PARALLEL_DO:
9634 case EXEC_OMP_PARALLEL_SECTIONS:
9636 omp_workshare_save = omp_workshare_flag;
9637 omp_workshare_flag = 0;
9638 gfc_resolve_omp_parallel_blocks (code, ns);
9641 gfc_resolve_omp_do_blocks (code, ns);
9643 case EXEC_SELECT_TYPE:
9644 /* Blocks are handled in resolve_select_type because we have
9645 to transform the SELECT TYPE into ASSOCIATE first. */
9647 case EXEC_DO_CONCURRENT:
9648 do_concurrent_flag = 1;
9649 gfc_resolve_blocks (code->block, ns);
9650 do_concurrent_flag = 2;
9652 case EXEC_OMP_WORKSHARE:
9653 omp_workshare_save = omp_workshare_flag;
9654 omp_workshare_flag = 1;
9657 gfc_resolve_blocks (code->block, ns);
9661 if (omp_workshare_save != -1)
9662 omp_workshare_flag = omp_workshare_save;
9666 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9667 t = gfc_resolve_expr (code->expr1);
9668 forall_flag = forall_save;
9669 do_concurrent_flag = do_concurrent_save;
9671 if (!gfc_resolve_expr (code->expr2))
9674 if (code->op == EXEC_ALLOCATE
9675 && !gfc_resolve_expr (code->expr3))
9681 case EXEC_END_BLOCK:
9682 case EXEC_END_NESTED_BLOCK:
9686 case EXEC_ERROR_STOP:
9690 case EXEC_ASSIGN_CALL:
9695 case EXEC_SYNC_IMAGES:
9696 case EXEC_SYNC_MEMORY:
9697 resolve_sync (code);
9702 resolve_lock_unlock (code);
9706 /* Keep track of which entry we are up to. */
9707 current_entry_id = code->ext.entry->id;
9711 resolve_where (code, NULL);
9715 if (code->expr1 != NULL)
9717 if (code->expr1->ts.type != BT_INTEGER)
9718 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9719 "INTEGER variable", &code->expr1->where);
9720 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9721 gfc_error ("Variable '%s' has not been assigned a target "
9722 "label at %L", code->expr1->symtree->n.sym->name,
9723 &code->expr1->where);
9726 resolve_branch (code->label1, code);
9730 if (code->expr1 != NULL
9731 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9732 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9733 "INTEGER return specifier", &code->expr1->where);
9736 case EXEC_INIT_ASSIGN:
9737 case EXEC_END_PROCEDURE:
9744 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9748 if (resolve_ordinary_assign (code, ns))
9750 if (code->op == EXEC_COMPCALL)
9756 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9757 if (code->expr1->ts.type == BT_DERIVED
9758 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9759 generate_component_assignments (&code, ns);
9763 case EXEC_LABEL_ASSIGN:
9764 if (code->label1->defined == ST_LABEL_UNKNOWN)
9765 gfc_error ("Label %d referenced at %L is never defined",
9766 code->label1->value, &code->label1->where);
9768 && (code->expr1->expr_type != EXPR_VARIABLE
9769 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9770 || code->expr1->symtree->n.sym->ts.kind
9771 != gfc_default_integer_kind
9772 || code->expr1->symtree->n.sym->as != NULL))
9773 gfc_error ("ASSIGN statement at %L requires a scalar "
9774 "default INTEGER variable", &code->expr1->where);
9777 case EXEC_POINTER_ASSIGN:
9784 /* This is both a variable definition and pointer assignment
9785 context, so check both of them. For rank remapping, a final
9786 array ref may be present on the LHS and fool gfc_expr_attr
9787 used in gfc_check_vardef_context. Remove it. */
9788 e = remove_last_array_ref (code->expr1);
9789 t = gfc_check_vardef_context (e, true, false, false,
9790 _("pointer assignment"));
9792 t = gfc_check_vardef_context (e, false, false, false,
9793 _("pointer assignment"));
9798 gfc_check_pointer_assign (code->expr1, code->expr2);
9802 case EXEC_ARITHMETIC_IF:
9804 && code->expr1->ts.type != BT_INTEGER
9805 && code->expr1->ts.type != BT_REAL)
9806 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9807 "expression", &code->expr1->where);
9809 resolve_branch (code->label1, code);
9810 resolve_branch (code->label2, code);
9811 resolve_branch (code->label3, code);
9815 if (t && code->expr1 != NULL
9816 && (code->expr1->ts.type != BT_LOGICAL
9817 || code->expr1->rank != 0))
9818 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9819 &code->expr1->where);
9824 resolve_call (code);
9829 resolve_typebound_subroutine (code);
9833 resolve_ppc_call (code);
9837 /* Select is complicated. Also, a SELECT construct could be
9838 a transformed computed GOTO. */
9839 resolve_select (code, false);
9842 case EXEC_SELECT_TYPE:
9843 resolve_select_type (code, ns);
9847 resolve_block_construct (code);
9851 if (code->ext.iterator != NULL)
9853 gfc_iterator *iter = code->ext.iterator;
9854 if (gfc_resolve_iterator (iter, true, false))
9855 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9860 if (code->expr1 == NULL)
9861 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9863 && (code->expr1->rank != 0
9864 || code->expr1->ts.type != BT_LOGICAL))
9865 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9866 "a scalar LOGICAL expression", &code->expr1->where);
9871 resolve_allocate_deallocate (code, "ALLOCATE");
9875 case EXEC_DEALLOCATE:
9877 resolve_allocate_deallocate (code, "DEALLOCATE");
9882 if (!gfc_resolve_open (code->ext.open))
9885 resolve_branch (code->ext.open->err, code);
9889 if (!gfc_resolve_close (code->ext.close))
9892 resolve_branch (code->ext.close->err, code);
9895 case EXEC_BACKSPACE:
9899 if (!gfc_resolve_filepos (code->ext.filepos))
9902 resolve_branch (code->ext.filepos->err, code);
9906 if (!gfc_resolve_inquire (code->ext.inquire))
9909 resolve_branch (code->ext.inquire->err, code);
9913 gcc_assert (code->ext.inquire != NULL);
9914 if (!gfc_resolve_inquire (code->ext.inquire))
9917 resolve_branch (code->ext.inquire->err, code);
9921 if (!gfc_resolve_wait (code->ext.wait))
9924 resolve_branch (code->ext.wait->err, code);
9925 resolve_branch (code->ext.wait->end, code);
9926 resolve_branch (code->ext.wait->eor, code);
9931 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
9934 resolve_branch (code->ext.dt->err, code);
9935 resolve_branch (code->ext.dt->end, code);
9936 resolve_branch (code->ext.dt->eor, code);
9940 resolve_transfer (code);
9943 case EXEC_DO_CONCURRENT:
9945 resolve_forall_iterators (code->ext.forall_iterator);
9947 if (code->expr1 != NULL
9948 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9949 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9950 "expression", &code->expr1->where);
9953 case EXEC_OMP_ATOMIC:
9954 case EXEC_OMP_BARRIER:
9955 case EXEC_OMP_CRITICAL:
9956 case EXEC_OMP_FLUSH:
9958 case EXEC_OMP_MASTER:
9959 case EXEC_OMP_ORDERED:
9960 case EXEC_OMP_SECTIONS:
9961 case EXEC_OMP_SINGLE:
9962 case EXEC_OMP_TASKWAIT:
9963 case EXEC_OMP_TASKYIELD:
9964 case EXEC_OMP_WORKSHARE:
9965 gfc_resolve_omp_directive (code, ns);
9968 case EXEC_OMP_PARALLEL:
9969 case EXEC_OMP_PARALLEL_DO:
9970 case EXEC_OMP_PARALLEL_SECTIONS:
9971 case EXEC_OMP_PARALLEL_WORKSHARE:
9973 omp_workshare_save = omp_workshare_flag;
9974 omp_workshare_flag = 0;
9975 gfc_resolve_omp_directive (code, ns);
9976 omp_workshare_flag = omp_workshare_save;
9980 gfc_internal_error ("resolve_code(): Bad statement code");
9984 cs_base = frame.prev;
9988 /* Resolve initial values and make sure they are compatible with
9992 resolve_values (gfc_symbol *sym)
9996 if (sym->value == NULL)
9999 if (sym->value->expr_type == EXPR_STRUCTURE)
10000 t= resolve_structure_cons (sym->value, 1);
10002 t = gfc_resolve_expr (sym->value);
10007 gfc_check_assign_symbol (sym, NULL, sym->value);
10011 /* Verify any BIND(C) derived types in the namespace so we can report errors
10012 for them once, rather than for each variable declared of that type. */
10015 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10017 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10018 && derived_sym->attr.is_bind_c == 1)
10019 verify_bind_c_derived_type (derived_sym);
10025 /* Verify that any binding labels used in a given namespace do not collide
10026 with the names or binding labels of any global symbols. */
10029 gfc_verify_binding_labels (gfc_symbol *sym)
10033 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
10034 && sym->attr.flavor != FL_DERIVED && sym->binding_label)
10036 gfc_gsymbol *bind_c_sym;
10038 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10039 if (bind_c_sym != NULL
10040 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
10042 if (sym->attr.if_source == IFSRC_DECL
10043 && (bind_c_sym->type != GSYM_SUBROUTINE
10044 && bind_c_sym->type != GSYM_FUNCTION)
10045 && ((sym->attr.contained == 1
10046 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
10047 || (sym->attr.use_assoc == 1
10048 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
10050 /* Make sure global procedures don't collide with anything. */
10051 gfc_error ("Binding label '%s' at %L collides with the global "
10052 "entity '%s' at %L", sym->binding_label,
10053 &(sym->declared_at), bind_c_sym->name,
10054 &(bind_c_sym->where));
10057 else if (sym->attr.contained == 0
10058 && (sym->attr.if_source == IFSRC_IFBODY
10059 && sym->attr.flavor == FL_PROCEDURE)
10060 && (bind_c_sym->sym_name != NULL
10061 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
10063 /* Make sure procedures in interface bodies don't collide. */
10064 gfc_error ("Binding label '%s' in interface body at %L collides "
10065 "with the global entity '%s' at %L",
10066 sym->binding_label,
10067 &(sym->declared_at), bind_c_sym->name,
10068 &(bind_c_sym->where));
10071 else if (sym->attr.contained == 0
10072 && sym->attr.if_source == IFSRC_UNKNOWN)
10073 if ((sym->attr.use_assoc && bind_c_sym->mod_name
10074 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
10075 || sym->attr.use_assoc == 0)
10077 gfc_error ("Binding label '%s' at %L collides with global "
10078 "entity '%s' at %L", sym->binding_label,
10079 &(sym->declared_at), bind_c_sym->name,
10080 &(bind_c_sym->where));
10084 if (has_error != 0)
10085 /* Clear the binding label to prevent checking multiple times. */
10086 sym->binding_label = NULL;
10088 else if (bind_c_sym == NULL)
10090 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
10091 bind_c_sym->where = sym->declared_at;
10092 bind_c_sym->sym_name = sym->name;
10094 if (sym->attr.use_assoc == 1)
10095 bind_c_sym->mod_name = sym->module;
10097 if (sym->ns->proc_name != NULL)
10098 bind_c_sym->mod_name = sym->ns->proc_name->name;
10100 if (sym->attr.contained == 0)
10102 if (sym->attr.subroutine)
10103 bind_c_sym->type = GSYM_SUBROUTINE;
10104 else if (sym->attr.function)
10105 bind_c_sym->type = GSYM_FUNCTION;
10113 /* Resolve an index expression. */
10116 resolve_index_expr (gfc_expr *e)
10118 if (!gfc_resolve_expr (e))
10121 if (!gfc_simplify_expr (e, 0))
10124 if (!gfc_specification_expr (e))
10131 /* Resolve a charlen structure. */
10134 resolve_charlen (gfc_charlen *cl)
10137 bool saved_specification_expr;
10143 saved_specification_expr = specification_expr;
10144 specification_expr = true;
10146 if (cl->length_from_typespec)
10148 if (!gfc_resolve_expr (cl->length))
10150 specification_expr = saved_specification_expr;
10154 if (!gfc_simplify_expr (cl->length, 0))
10156 specification_expr = saved_specification_expr;
10163 if (!resolve_index_expr (cl->length))
10165 specification_expr = saved_specification_expr;
10170 /* "If the character length parameter value evaluates to a negative
10171 value, the length of character entities declared is zero." */
10172 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10174 if (gfc_option.warn_surprising)
10175 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10176 " the length has been set to zero",
10177 &cl->length->where, i);
10178 gfc_replace_expr (cl->length,
10179 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10182 /* Check that the character length is not too large. */
10183 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10184 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10185 && cl->length->ts.type == BT_INTEGER
10186 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10188 gfc_error ("String length at %L is too large", &cl->length->where);
10189 specification_expr = saved_specification_expr;
10193 specification_expr = saved_specification_expr;
10198 /* Test for non-constant shape arrays. */
10201 is_non_constant_shape_array (gfc_symbol *sym)
10207 not_constant = false;
10208 if (sym->as != NULL)
10210 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10211 has not been simplified; parameter array references. Do the
10212 simplification now. */
10213 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10215 e = sym->as->lower[i];
10216 if (e && (!resolve_index_expr(e)
10217 || !gfc_is_constant_expr (e)))
10218 not_constant = true;
10219 e = sym->as->upper[i];
10220 if (e && (!resolve_index_expr(e)
10221 || !gfc_is_constant_expr (e)))
10222 not_constant = true;
10225 return not_constant;
10228 /* Given a symbol and an initialization expression, add code to initialize
10229 the symbol to the function entry. */
10231 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10235 gfc_namespace *ns = sym->ns;
10237 /* Search for the function namespace if this is a contained
10238 function without an explicit result. */
10239 if (sym->attr.function && sym == sym->result
10240 && sym->name != sym->ns->proc_name->name)
10242 ns = ns->contained;
10243 for (;ns; ns = ns->sibling)
10244 if (strcmp (ns->proc_name->name, sym->name) == 0)
10250 gfc_free_expr (init);
10254 /* Build an l-value expression for the result. */
10255 lval = gfc_lval_expr_from_sym (sym);
10257 /* Add the code at scope entry. */
10258 init_st = gfc_get_code ();
10259 init_st->next = ns->code;
10260 ns->code = init_st;
10262 /* Assign the default initializer to the l-value. */
10263 init_st->loc = sym->declared_at;
10264 init_st->op = EXEC_INIT_ASSIGN;
10265 init_st->expr1 = lval;
10266 init_st->expr2 = init;
10269 /* Assign the default initializer to a derived type variable or result. */
10272 apply_default_init (gfc_symbol *sym)
10274 gfc_expr *init = NULL;
10276 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10279 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10280 init = gfc_default_initializer (&sym->ts);
10282 if (init == NULL && sym->ts.type != BT_CLASS)
10285 build_init_assign (sym, init);
10286 sym->attr.referenced = 1;
10289 /* Build an initializer for a local integer, real, complex, logical, or
10290 character variable, based on the command line flags finit-local-zero,
10291 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10292 null if the symbol should not have a default initialization. */
10294 build_default_init_expr (gfc_symbol *sym)
10297 gfc_expr *init_expr;
10300 /* These symbols should never have a default initialization. */
10301 if (sym->attr.allocatable
10302 || sym->attr.external
10304 || sym->attr.pointer
10305 || sym->attr.in_equivalence
10306 || sym->attr.in_common
10309 || sym->attr.cray_pointee
10310 || sym->attr.cray_pointer
10314 /* Now we'll try to build an initializer expression. */
10315 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10316 &sym->declared_at);
10318 /* We will only initialize integers, reals, complex, logicals, and
10319 characters, and only if the corresponding command-line flags
10320 were set. Otherwise, we free init_expr and return null. */
10321 switch (sym->ts.type)
10324 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10325 mpz_set_si (init_expr->value.integer,
10326 gfc_option.flag_init_integer_value);
10329 gfc_free_expr (init_expr);
10335 switch (gfc_option.flag_init_real)
10337 case GFC_INIT_REAL_SNAN:
10338 init_expr->is_snan = 1;
10339 /* Fall through. */
10340 case GFC_INIT_REAL_NAN:
10341 mpfr_set_nan (init_expr->value.real);
10344 case GFC_INIT_REAL_INF:
10345 mpfr_set_inf (init_expr->value.real, 1);
10348 case GFC_INIT_REAL_NEG_INF:
10349 mpfr_set_inf (init_expr->value.real, -1);
10352 case GFC_INIT_REAL_ZERO:
10353 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10357 gfc_free_expr (init_expr);
10364 switch (gfc_option.flag_init_real)
10366 case GFC_INIT_REAL_SNAN:
10367 init_expr->is_snan = 1;
10368 /* Fall through. */
10369 case GFC_INIT_REAL_NAN:
10370 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10371 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10374 case GFC_INIT_REAL_INF:
10375 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10376 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10379 case GFC_INIT_REAL_NEG_INF:
10380 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10381 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10384 case GFC_INIT_REAL_ZERO:
10385 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10389 gfc_free_expr (init_expr);
10396 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10397 init_expr->value.logical = 0;
10398 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10399 init_expr->value.logical = 1;
10402 gfc_free_expr (init_expr);
10408 /* For characters, the length must be constant in order to
10409 create a default initializer. */
10410 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10411 && sym->ts.u.cl->length
10412 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10414 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10415 init_expr->value.character.length = char_len;
10416 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10417 for (i = 0; i < char_len; i++)
10418 init_expr->value.character.string[i]
10419 = (unsigned char) gfc_option.flag_init_character_value;
10423 gfc_free_expr (init_expr);
10426 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10427 && sym->ts.u.cl->length)
10429 gfc_actual_arglist *arg;
10430 init_expr = gfc_get_expr ();
10431 init_expr->where = sym->declared_at;
10432 init_expr->ts = sym->ts;
10433 init_expr->expr_type = EXPR_FUNCTION;
10434 init_expr->value.function.isym =
10435 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10436 init_expr->value.function.name = "repeat";
10437 arg = gfc_get_actual_arglist ();
10438 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10440 arg->expr->value.character.string[0]
10441 = gfc_option.flag_init_character_value;
10442 arg->next = gfc_get_actual_arglist ();
10443 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10444 init_expr->value.function.actual = arg;
10449 gfc_free_expr (init_expr);
10455 /* Add an initialization expression to a local variable. */
10457 apply_default_init_local (gfc_symbol *sym)
10459 gfc_expr *init = NULL;
10461 /* The symbol should be a variable or a function return value. */
10462 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10463 || (sym->attr.function && sym->result != sym))
10466 /* Try to build the initializer expression. If we can't initialize
10467 this symbol, then init will be NULL. */
10468 init = build_default_init_expr (sym);
10472 /* For saved variables, we don't want to add an initializer at function
10473 entry, so we just add a static initializer. Note that automatic variables
10474 are stack allocated even with -fno-automatic; we have also to exclude
10475 result variable, which are also nonstatic. */
10476 if (sym->attr.save || sym->ns->save_all
10477 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10478 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10480 /* Don't clobber an existing initializer! */
10481 gcc_assert (sym->value == NULL);
10486 build_init_assign (sym, init);
10490 /* Resolution of common features of flavors variable and procedure. */
10493 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10495 gfc_array_spec *as;
10497 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10498 as = CLASS_DATA (sym)->as;
10502 /* Constraints on deferred shape variable. */
10503 if (as == NULL || as->type != AS_DEFERRED)
10505 bool pointer, allocatable, dimension;
10507 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10509 pointer = CLASS_DATA (sym)->attr.class_pointer;
10510 allocatable = CLASS_DATA (sym)->attr.allocatable;
10511 dimension = CLASS_DATA (sym)->attr.dimension;
10515 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10516 allocatable = sym->attr.allocatable;
10517 dimension = sym->attr.dimension;
10522 if (dimension && as->type != AS_ASSUMED_RANK)
10524 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10525 "shape or assumed rank", sym->name, &sym->declared_at);
10528 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10529 "'%s' at %L may not be ALLOCATABLE",
10530 sym->name, &sym->declared_at))
10534 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10536 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10537 "assumed rank", sym->name, &sym->declared_at);
10543 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10544 && sym->ts.type != BT_CLASS && !sym->assoc)
10546 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10547 sym->name, &sym->declared_at);
10552 /* Constraints on polymorphic variables. */
10553 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10556 if (sym->attr.class_ok
10557 && !sym->attr.select_type_temporary
10558 && !UNLIMITED_POLY (sym)
10559 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10561 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10562 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10563 &sym->declared_at);
10568 /* Assume that use associated symbols were checked in the module ns.
10569 Class-variables that are associate-names are also something special
10570 and excepted from the test. */
10571 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10573 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10574 "or pointer", sym->name, &sym->declared_at);
10583 /* Additional checks for symbols with flavor variable and derived
10584 type. To be called from resolve_fl_variable. */
10587 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10589 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10591 /* Check to see if a derived type is blocked from being host
10592 associated by the presence of another class I symbol in the same
10593 namespace. 14.6.1.3 of the standard and the discussion on
10594 comp.lang.fortran. */
10595 if (sym->ns != sym->ts.u.derived->ns
10596 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10599 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10600 if (s && s->attr.generic)
10601 s = gfc_find_dt_in_generic (s);
10602 if (s && s->attr.flavor != FL_DERIVED)
10604 gfc_error ("The type '%s' cannot be host associated at %L "
10605 "because it is blocked by an incompatible object "
10606 "of the same name declared at %L",
10607 sym->ts.u.derived->name, &sym->declared_at,
10613 /* 4th constraint in section 11.3: "If an object of a type for which
10614 component-initialization is specified (R429) appears in the
10615 specification-part of a module and does not have the ALLOCATABLE
10616 or POINTER attribute, the object shall have the SAVE attribute."
10618 The check for initializers is performed with
10619 gfc_has_default_initializer because gfc_default_initializer generates
10620 a hidden default for allocatable components. */
10621 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10622 && sym->ns->proc_name->attr.flavor == FL_MODULE
10623 && !sym->ns->save_all && !sym->attr.save
10624 && !sym->attr.pointer && !sym->attr.allocatable
10625 && gfc_has_default_initializer (sym->ts.u.derived)
10626 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10627 "'%s' at %L, needed due to the default "
10628 "initialization", sym->name, &sym->declared_at))
10631 /* Assign default initializer. */
10632 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10633 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10635 sym->value = gfc_default_initializer (&sym->ts);
10642 /* Resolve symbols with flavor variable. */
10645 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10647 int no_init_flag, automatic_flag;
10649 const char *auto_save_msg;
10650 bool saved_specification_expr;
10652 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10655 if (!resolve_fl_var_and_proc (sym, mp_flag))
10658 /* Set this flag to check that variables are parameters of all entries.
10659 This check is effected by the call to gfc_resolve_expr through
10660 is_non_constant_shape_array. */
10661 saved_specification_expr = specification_expr;
10662 specification_expr = true;
10664 if (sym->ns->proc_name
10665 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10666 || sym->ns->proc_name->attr.is_main_program)
10667 && !sym->attr.use_assoc
10668 && !sym->attr.allocatable
10669 && !sym->attr.pointer
10670 && is_non_constant_shape_array (sym))
10672 /* The shape of a main program or module array needs to be
10674 gfc_error ("The module or main program array '%s' at %L must "
10675 "have constant shape", sym->name, &sym->declared_at);
10676 specification_expr = saved_specification_expr;
10680 /* Constraints on deferred type parameter. */
10681 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10683 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10684 "requires either the pointer or allocatable attribute",
10685 sym->name, &sym->declared_at);
10686 specification_expr = saved_specification_expr;
10690 if (sym->ts.type == BT_CHARACTER)
10692 /* Make sure that character string variables with assumed length are
10693 dummy arguments. */
10694 e = sym->ts.u.cl->length;
10695 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10696 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10698 gfc_error ("Entity with assumed character length at %L must be a "
10699 "dummy argument or a PARAMETER", &sym->declared_at);
10700 specification_expr = saved_specification_expr;
10704 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10706 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10707 specification_expr = saved_specification_expr;
10711 if (!gfc_is_constant_expr (e)
10712 && !(e->expr_type == EXPR_VARIABLE
10713 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10715 if (!sym->attr.use_assoc && sym->ns->proc_name
10716 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10717 || sym->ns->proc_name->attr.is_main_program))
10719 gfc_error ("'%s' at %L must have constant character length "
10720 "in this context", sym->name, &sym->declared_at);
10721 specification_expr = saved_specification_expr;
10724 if (sym->attr.in_common)
10726 gfc_error ("COMMON variable '%s' at %L must have constant "
10727 "character length", sym->name, &sym->declared_at);
10728 specification_expr = saved_specification_expr;
10734 if (sym->value == NULL && sym->attr.referenced)
10735 apply_default_init_local (sym); /* Try to apply a default initialization. */
10737 /* Determine if the symbol may not have an initializer. */
10738 no_init_flag = automatic_flag = 0;
10739 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10740 || sym->attr.intrinsic || sym->attr.result)
10742 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10743 && is_non_constant_shape_array (sym))
10745 no_init_flag = automatic_flag = 1;
10747 /* Also, they must not have the SAVE attribute.
10748 SAVE_IMPLICIT is checked below. */
10749 if (sym->as && sym->attr.codimension)
10751 int corank = sym->as->corank;
10752 sym->as->corank = 0;
10753 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10754 sym->as->corank = corank;
10756 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10758 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10759 specification_expr = saved_specification_expr;
10764 /* Ensure that any initializer is simplified. */
10766 gfc_simplify_expr (sym->value, 1);
10768 /* Reject illegal initializers. */
10769 if (!sym->mark && sym->value)
10771 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10772 && CLASS_DATA (sym)->attr.allocatable))
10773 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10774 sym->name, &sym->declared_at);
10775 else if (sym->attr.external)
10776 gfc_error ("External '%s' at %L cannot have an initializer",
10777 sym->name, &sym->declared_at);
10778 else if (sym->attr.dummy
10779 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10780 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10781 sym->name, &sym->declared_at);
10782 else if (sym->attr.intrinsic)
10783 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10784 sym->name, &sym->declared_at);
10785 else if (sym->attr.result)
10786 gfc_error ("Function result '%s' at %L cannot have an initializer",
10787 sym->name, &sym->declared_at);
10788 else if (automatic_flag)
10789 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10790 sym->name, &sym->declared_at);
10792 goto no_init_error;
10793 specification_expr = saved_specification_expr;
10798 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10800 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10801 specification_expr = saved_specification_expr;
10805 specification_expr = saved_specification_expr;
10810 /* Resolve a procedure. */
10813 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10815 gfc_formal_arglist *arg;
10817 if (sym->attr.function
10818 && !resolve_fl_var_and_proc (sym, mp_flag))
10821 if (sym->ts.type == BT_CHARACTER)
10823 gfc_charlen *cl = sym->ts.u.cl;
10825 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10826 && !resolve_charlen (cl))
10829 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10830 && sym->attr.proc == PROC_ST_FUNCTION)
10832 gfc_error ("Character-valued statement function '%s' at %L must "
10833 "have constant length", sym->name, &sym->declared_at);
10838 /* Ensure that derived type for are not of a private type. Internal
10839 module procedures are excluded by 2.2.3.3 - i.e., they are not
10840 externally accessible and can access all the objects accessible in
10842 if (!(sym->ns->parent
10843 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10844 && gfc_check_symbol_access (sym))
10846 gfc_interface *iface;
10848 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10851 && arg->sym->ts.type == BT_DERIVED
10852 && !arg->sym->ts.u.derived->attr.use_assoc
10853 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10854 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10855 "and cannot be a dummy argument"
10856 " of '%s', which is PUBLIC at %L",
10857 arg->sym->name, sym->name,
10858 &sym->declared_at))
10860 /* Stop this message from recurring. */
10861 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10866 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10867 PRIVATE to the containing module. */
10868 for (iface = sym->generic; iface; iface = iface->next)
10870 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10873 && arg->sym->ts.type == BT_DERIVED
10874 && !arg->sym->ts.u.derived->attr.use_assoc
10875 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10876 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10877 "PUBLIC interface '%s' at %L "
10878 "takes dummy arguments of '%s' which "
10879 "is PRIVATE", iface->sym->name,
10880 sym->name, &iface->sym->declared_at,
10881 gfc_typename(&arg->sym->ts)))
10883 /* Stop this message from recurring. */
10884 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10890 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10891 PRIVATE to the containing module. */
10892 for (iface = sym->generic; iface; iface = iface->next)
10894 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10897 && arg->sym->ts.type == BT_DERIVED
10898 && !arg->sym->ts.u.derived->attr.use_assoc
10899 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10900 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10901 "PUBLIC interface '%s' at %L takes "
10902 "dummy arguments of '%s' which is "
10903 "PRIVATE", iface->sym->name,
10904 sym->name, &iface->sym->declared_at,
10905 gfc_typename(&arg->sym->ts)))
10907 /* Stop this message from recurring. */
10908 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10915 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10916 && !sym->attr.proc_pointer)
10918 gfc_error ("Function '%s' at %L cannot have an initializer",
10919 sym->name, &sym->declared_at);
10923 /* An external symbol may not have an initializer because it is taken to be
10924 a procedure. Exception: Procedure Pointers. */
10925 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10927 gfc_error ("External object '%s' at %L may not have an initializer",
10928 sym->name, &sym->declared_at);
10932 /* An elemental function is required to return a scalar 12.7.1 */
10933 if (sym->attr.elemental && sym->attr.function && sym->as)
10935 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10936 "result", sym->name, &sym->declared_at);
10937 /* Reset so that the error only occurs once. */
10938 sym->attr.elemental = 0;
10942 if (sym->attr.proc == PROC_ST_FUNCTION
10943 && (sym->attr.allocatable || sym->attr.pointer))
10945 gfc_error ("Statement function '%s' at %L may not have pointer or "
10946 "allocatable attribute", sym->name, &sym->declared_at);
10950 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10951 char-len-param shall not be array-valued, pointer-valued, recursive
10952 or pure. ....snip... A character value of * may only be used in the
10953 following ways: (i) Dummy arg of procedure - dummy associates with
10954 actual length; (ii) To declare a named constant; or (iii) External
10955 function - but length must be declared in calling scoping unit. */
10956 if (sym->attr.function
10957 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
10958 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10960 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10961 || (sym->attr.recursive) || (sym->attr.pure))
10963 if (sym->as && sym->as->rank)
10964 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10965 "array-valued", sym->name, &sym->declared_at);
10967 if (sym->attr.pointer)
10968 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10969 "pointer-valued", sym->name, &sym->declared_at);
10971 if (sym->attr.pure)
10972 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10973 "pure", sym->name, &sym->declared_at);
10975 if (sym->attr.recursive)
10976 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10977 "recursive", sym->name, &sym->declared_at);
10982 /* Appendix B.2 of the standard. Contained functions give an
10983 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10984 character length is an F2003 feature. */
10985 if (!sym->attr.contained
10986 && gfc_current_form != FORM_FIXED
10987 && !sym->ts.deferred)
10988 gfc_notify_std (GFC_STD_F95_OBS,
10989 "CHARACTER(*) function '%s' at %L",
10990 sym->name, &sym->declared_at);
10993 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10995 gfc_formal_arglist *curr_arg;
10996 int has_non_interop_arg = 0;
10998 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10999 sym->common_block))
11001 /* Clear these to prevent looking at them again if there was an
11003 sym->attr.is_bind_c = 0;
11004 sym->attr.is_c_interop = 0;
11005 sym->ts.is_c_interop = 0;
11009 /* So far, no errors have been found. */
11010 sym->attr.is_c_interop = 1;
11011 sym->ts.is_c_interop = 1;
11014 curr_arg = gfc_sym_get_dummy_args (sym);
11015 while (curr_arg != NULL)
11017 /* Skip implicitly typed dummy args here. */
11018 if (curr_arg->sym->attr.implicit_type == 0)
11019 if (!gfc_verify_c_interop_param (curr_arg->sym))
11020 /* If something is found to fail, record the fact so we
11021 can mark the symbol for the procedure as not being
11022 BIND(C) to try and prevent multiple errors being
11024 has_non_interop_arg = 1;
11026 curr_arg = curr_arg->next;
11029 /* See if any of the arguments were not interoperable and if so, clear
11030 the procedure symbol to prevent duplicate error messages. */
11031 if (has_non_interop_arg != 0)
11033 sym->attr.is_c_interop = 0;
11034 sym->ts.is_c_interop = 0;
11035 sym->attr.is_bind_c = 0;
11039 if (!sym->attr.proc_pointer)
11041 if (sym->attr.save == SAVE_EXPLICIT)
11043 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11044 "in '%s' at %L", sym->name, &sym->declared_at);
11047 if (sym->attr.intent)
11049 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11050 "in '%s' at %L", sym->name, &sym->declared_at);
11053 if (sym->attr.subroutine && sym->attr.result)
11055 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11056 "in '%s' at %L", sym->name, &sym->declared_at);
11059 if (sym->attr.external && sym->attr.function
11060 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11061 || sym->attr.contained))
11063 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11064 "in '%s' at %L", sym->name, &sym->declared_at);
11067 if (strcmp ("ppr@", sym->name) == 0)
11069 gfc_error ("Procedure pointer result '%s' at %L "
11070 "is missing the pointer attribute",
11071 sym->ns->proc_name->name, &sym->declared_at);
11080 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11081 been defined and we now know their defined arguments, check that they fulfill
11082 the requirements of the standard for procedures used as finalizers. */
11085 gfc_resolve_finalizers (gfc_symbol* derived)
11087 gfc_finalizer* list;
11088 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11089 bool result = true;
11090 bool seen_scalar = false;
11092 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11095 /* Walk over the list of finalizer-procedures, check them, and if any one
11096 does not fit in with the standard's definition, print an error and remove
11097 it from the list. */
11098 prev_link = &derived->f2k_derived->finalizers;
11099 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11101 gfc_formal_arglist *dummy_args;
11106 /* Skip this finalizer if we already resolved it. */
11107 if (list->proc_tree)
11109 prev_link = &(list->next);
11113 /* Check this exists and is a SUBROUTINE. */
11114 if (!list->proc_sym->attr.subroutine)
11116 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11117 list->proc_sym->name, &list->where);
11121 /* We should have exactly one argument. */
11122 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11123 if (!dummy_args || dummy_args->next)
11125 gfc_error ("FINAL procedure at %L must have exactly one argument",
11129 arg = dummy_args->sym;
11131 /* This argument must be of our type. */
11132 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11134 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11135 &arg->declared_at, derived->name);
11139 /* It must neither be a pointer nor allocatable nor optional. */
11140 if (arg->attr.pointer)
11142 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11143 &arg->declared_at);
11146 if (arg->attr.allocatable)
11148 gfc_error ("Argument of FINAL procedure at %L must not be"
11149 " ALLOCATABLE", &arg->declared_at);
11152 if (arg->attr.optional)
11154 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11155 &arg->declared_at);
11159 /* It must not be INTENT(OUT). */
11160 if (arg->attr.intent == INTENT_OUT)
11162 gfc_error ("Argument of FINAL procedure at %L must not be"
11163 " INTENT(OUT)", &arg->declared_at);
11167 /* Warn if the procedure is non-scalar and not assumed shape. */
11168 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11169 && arg->as->type != AS_ASSUMED_SHAPE)
11170 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11171 " shape argument", &arg->declared_at);
11173 /* Check that it does not match in kind and rank with a FINAL procedure
11174 defined earlier. To really loop over the *earlier* declarations,
11175 we need to walk the tail of the list as new ones were pushed at the
11177 /* TODO: Handle kind parameters once they are implemented. */
11178 my_rank = (arg->as ? arg->as->rank : 0);
11179 for (i = list->next; i; i = i->next)
11181 gfc_formal_arglist *dummy_args;
11183 /* Argument list might be empty; that is an error signalled earlier,
11184 but we nevertheless continued resolving. */
11185 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11188 gfc_symbol* i_arg = dummy_args->sym;
11189 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11190 if (i_rank == my_rank)
11192 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11193 " rank (%d) as '%s'",
11194 list->proc_sym->name, &list->where, my_rank,
11195 i->proc_sym->name);
11201 /* Is this the/a scalar finalizer procedure? */
11202 if (!arg->as || arg->as->rank == 0)
11203 seen_scalar = true;
11205 /* Find the symtree for this procedure. */
11206 gcc_assert (!list->proc_tree);
11207 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11209 prev_link = &list->next;
11212 /* Remove wrong nodes immediately from the list so we don't risk any
11213 troubles in the future when they might fail later expectations. */
11217 *prev_link = list->next;
11218 gfc_free_finalizer (i);
11221 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11222 were nodes in the list, must have been for arrays. It is surely a good
11223 idea to have a scalar version there if there's something to finalize. */
11224 if (gfc_option.warn_surprising && result && !seen_scalar)
11225 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11226 " defined at %L, suggest also scalar one",
11227 derived->name, &derived->declared_at);
11229 /* TODO: Remove this error when finalization is finished. */
11230 gfc_error ("Finalization at %L is not yet implemented",
11231 &derived->declared_at);
11233 gfc_find_derived_vtab (derived);
11238 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11241 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11242 const char* generic_name, locus where)
11244 gfc_symbol *sym1, *sym2;
11245 const char *pass1, *pass2;
11247 gcc_assert (t1->specific && t2->specific);
11248 gcc_assert (!t1->specific->is_generic);
11249 gcc_assert (!t2->specific->is_generic);
11250 gcc_assert (t1->is_operator == t2->is_operator);
11252 sym1 = t1->specific->u.specific->n.sym;
11253 sym2 = t2->specific->u.specific->n.sym;
11258 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11259 if (sym1->attr.subroutine != sym2->attr.subroutine
11260 || sym1->attr.function != sym2->attr.function)
11262 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11263 " GENERIC '%s' at %L",
11264 sym1->name, sym2->name, generic_name, &where);
11268 /* Compare the interfaces. */
11269 if (t1->specific->nopass)
11271 else if (t1->specific->pass_arg)
11272 pass1 = t1->specific->pass_arg;
11274 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
11275 if (t2->specific->nopass)
11277 else if (t2->specific->pass_arg)
11278 pass2 = t2->specific->pass_arg;
11280 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
11281 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11282 NULL, 0, pass1, pass2))
11284 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11285 sym1->name, sym2->name, generic_name, &where);
11293 /* Worker function for resolving a generic procedure binding; this is used to
11294 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11296 The difference between those cases is finding possible inherited bindings
11297 that are overridden, as one has to look for them in tb_sym_root,
11298 tb_uop_root or tb_op, respectively. Thus the caller must already find
11299 the super-type and set p->overridden correctly. */
11302 resolve_tb_generic_targets (gfc_symbol* super_type,
11303 gfc_typebound_proc* p, const char* name)
11305 gfc_tbp_generic* target;
11306 gfc_symtree* first_target;
11307 gfc_symtree* inherited;
11309 gcc_assert (p && p->is_generic);
11311 /* Try to find the specific bindings for the symtrees in our target-list. */
11312 gcc_assert (p->u.generic);
11313 for (target = p->u.generic; target; target = target->next)
11314 if (!target->specific)
11316 gfc_typebound_proc* overridden_tbp;
11317 gfc_tbp_generic* g;
11318 const char* target_name;
11320 target_name = target->specific_st->name;
11322 /* Defined for this type directly. */
11323 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11325 target->specific = target->specific_st->n.tb;
11326 goto specific_found;
11329 /* Look for an inherited specific binding. */
11332 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11337 gcc_assert (inherited->n.tb);
11338 target->specific = inherited->n.tb;
11339 goto specific_found;
11343 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11344 " at %L", target_name, name, &p->where);
11347 /* Once we've found the specific binding, check it is not ambiguous with
11348 other specifics already found or inherited for the same GENERIC. */
11350 gcc_assert (target->specific);
11352 /* This must really be a specific binding! */
11353 if (target->specific->is_generic)
11355 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11356 " '%s' is GENERIC, too", name, &p->where, target_name);
11360 /* Check those already resolved on this type directly. */
11361 for (g = p->u.generic; g; g = g->next)
11362 if (g != target && g->specific
11363 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11366 /* Check for ambiguity with inherited specific targets. */
11367 for (overridden_tbp = p->overridden; overridden_tbp;
11368 overridden_tbp = overridden_tbp->overridden)
11369 if (overridden_tbp->is_generic)
11371 for (g = overridden_tbp->u.generic; g; g = g->next)
11373 gcc_assert (g->specific);
11374 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11380 /* If we attempt to "overwrite" a specific binding, this is an error. */
11381 if (p->overridden && !p->overridden->is_generic)
11383 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11384 " the same name", name, &p->where);
11388 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11389 all must have the same attributes here. */
11390 first_target = p->u.generic->specific->u.specific;
11391 gcc_assert (first_target);
11392 p->subroutine = first_target->n.sym->attr.subroutine;
11393 p->function = first_target->n.sym->attr.function;
11399 /* Resolve a GENERIC procedure binding for a derived type. */
11402 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11404 gfc_symbol* super_type;
11406 /* Find the overridden binding if any. */
11407 st->n.tb->overridden = NULL;
11408 super_type = gfc_get_derived_super_type (derived);
11411 gfc_symtree* overridden;
11412 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11415 if (overridden && overridden->n.tb)
11416 st->n.tb->overridden = overridden->n.tb;
11419 /* Resolve using worker function. */
11420 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11424 /* Retrieve the target-procedure of an operator binding and do some checks in
11425 common for intrinsic and user-defined type-bound operators. */
11428 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11430 gfc_symbol* target_proc;
11432 gcc_assert (target->specific && !target->specific->is_generic);
11433 target_proc = target->specific->u.specific->n.sym;
11434 gcc_assert (target_proc);
11436 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11437 if (target->specific->nopass)
11439 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11443 return target_proc;
11447 /* Resolve a type-bound intrinsic operator. */
11450 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11451 gfc_typebound_proc* p)
11453 gfc_symbol* super_type;
11454 gfc_tbp_generic* target;
11456 /* If there's already an error here, do nothing (but don't fail again). */
11460 /* Operators should always be GENERIC bindings. */
11461 gcc_assert (p->is_generic);
11463 /* Look for an overridden binding. */
11464 super_type = gfc_get_derived_super_type (derived);
11465 if (super_type && super_type->f2k_derived)
11466 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11469 p->overridden = NULL;
11471 /* Resolve general GENERIC properties using worker function. */
11472 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11475 /* Check the targets to be procedures of correct interface. */
11476 for (target = p->u.generic; target; target = target->next)
11478 gfc_symbol* target_proc;
11480 target_proc = get_checked_tb_operator_target (target, p->where);
11484 if (!gfc_check_operator_interface (target_proc, op, p->where))
11487 /* Add target to non-typebound operator list. */
11488 if (!target->specific->deferred && !derived->attr.use_assoc
11489 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11491 gfc_interface *head, *intr;
11492 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11494 head = derived->ns->op[op];
11495 intr = gfc_get_interface ();
11496 intr->sym = target_proc;
11497 intr->where = p->where;
11499 derived->ns->op[op] = intr;
11511 /* Resolve a type-bound user operator (tree-walker callback). */
11513 static gfc_symbol* resolve_bindings_derived;
11514 static bool resolve_bindings_result;
11516 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11519 resolve_typebound_user_op (gfc_symtree* stree)
11521 gfc_symbol* super_type;
11522 gfc_tbp_generic* target;
11524 gcc_assert (stree && stree->n.tb);
11526 if (stree->n.tb->error)
11529 /* Operators should always be GENERIC bindings. */
11530 gcc_assert (stree->n.tb->is_generic);
11532 /* Find overridden procedure, if any. */
11533 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11534 if (super_type && super_type->f2k_derived)
11536 gfc_symtree* overridden;
11537 overridden = gfc_find_typebound_user_op (super_type, NULL,
11538 stree->name, true, NULL);
11540 if (overridden && overridden->n.tb)
11541 stree->n.tb->overridden = overridden->n.tb;
11544 stree->n.tb->overridden = NULL;
11546 /* Resolve basically using worker function. */
11547 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11550 /* Check the targets to be functions of correct interface. */
11551 for (target = stree->n.tb->u.generic; target; target = target->next)
11553 gfc_symbol* target_proc;
11555 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11559 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11566 resolve_bindings_result = false;
11567 stree->n.tb->error = 1;
11571 /* Resolve the type-bound procedures for a derived type. */
11574 resolve_typebound_procedure (gfc_symtree* stree)
11578 gfc_symbol* me_arg;
11579 gfc_symbol* super_type;
11580 gfc_component* comp;
11582 gcc_assert (stree);
11584 /* Undefined specific symbol from GENERIC target definition. */
11588 if (stree->n.tb->error)
11591 /* If this is a GENERIC binding, use that routine. */
11592 if (stree->n.tb->is_generic)
11594 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11599 /* Get the target-procedure to check it. */
11600 gcc_assert (!stree->n.tb->is_generic);
11601 gcc_assert (stree->n.tb->u.specific);
11602 proc = stree->n.tb->u.specific->n.sym;
11603 where = stree->n.tb->where;
11605 /* Default access should already be resolved from the parser. */
11606 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11608 if (stree->n.tb->deferred)
11610 if (!check_proc_interface (proc, &where))
11615 /* Check for F08:C465. */
11616 if ((!proc->attr.subroutine && !proc->attr.function)
11617 || (proc->attr.proc != PROC_MODULE
11618 && proc->attr.if_source != IFSRC_IFBODY)
11619 || proc->attr.abstract)
11621 gfc_error ("'%s' must be a module procedure or an external procedure with"
11622 " an explicit interface at %L", proc->name, &where);
11627 stree->n.tb->subroutine = proc->attr.subroutine;
11628 stree->n.tb->function = proc->attr.function;
11630 /* Find the super-type of the current derived type. We could do this once and
11631 store in a global if speed is needed, but as long as not I believe this is
11632 more readable and clearer. */
11633 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11635 /* If PASS, resolve and check arguments if not already resolved / loaded
11636 from a .mod file. */
11637 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11639 gfc_formal_arglist *dummy_args;
11641 dummy_args = gfc_sym_get_dummy_args (proc);
11642 if (stree->n.tb->pass_arg)
11644 gfc_formal_arglist *i;
11646 /* If an explicit passing argument name is given, walk the arg-list
11647 and look for it. */
11650 stree->n.tb->pass_arg_num = 1;
11651 for (i = dummy_args; i; i = i->next)
11653 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11658 ++stree->n.tb->pass_arg_num;
11663 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11665 proc->name, stree->n.tb->pass_arg, &where,
11666 stree->n.tb->pass_arg);
11672 /* Otherwise, take the first one; there should in fact be at least
11674 stree->n.tb->pass_arg_num = 1;
11677 gfc_error ("Procedure '%s' with PASS at %L must have at"
11678 " least one argument", proc->name, &where);
11681 me_arg = dummy_args->sym;
11684 /* Now check that the argument-type matches and the passed-object
11685 dummy argument is generally fine. */
11687 gcc_assert (me_arg);
11689 if (me_arg->ts.type != BT_CLASS)
11691 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11692 " at %L", proc->name, &where);
11696 if (CLASS_DATA (me_arg)->ts.u.derived
11697 != resolve_bindings_derived)
11699 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11700 " the derived-type '%s'", me_arg->name, proc->name,
11701 me_arg->name, &where, resolve_bindings_derived->name);
11705 gcc_assert (me_arg->ts.type == BT_CLASS);
11706 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11708 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11709 " scalar", proc->name, &where);
11712 if (CLASS_DATA (me_arg)->attr.allocatable)
11714 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11715 " be ALLOCATABLE", proc->name, &where);
11718 if (CLASS_DATA (me_arg)->attr.class_pointer)
11720 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11721 " be POINTER", proc->name, &where);
11726 /* If we are extending some type, check that we don't override a procedure
11727 flagged NON_OVERRIDABLE. */
11728 stree->n.tb->overridden = NULL;
11731 gfc_symtree* overridden;
11732 overridden = gfc_find_typebound_proc (super_type, NULL,
11733 stree->name, true, NULL);
11737 if (overridden->n.tb)
11738 stree->n.tb->overridden = overridden->n.tb;
11740 if (!gfc_check_typebound_override (stree, overridden))
11745 /* See if there's a name collision with a component directly in this type. */
11746 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11747 if (!strcmp (comp->name, stree->name))
11749 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11751 stree->name, &where, resolve_bindings_derived->name);
11755 /* Try to find a name collision with an inherited component. */
11756 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11758 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11759 " component of '%s'",
11760 stree->name, &where, resolve_bindings_derived->name);
11764 stree->n.tb->error = 0;
11768 resolve_bindings_result = false;
11769 stree->n.tb->error = 1;
11774 resolve_typebound_procedures (gfc_symbol* derived)
11777 gfc_symbol* super_type;
11779 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11782 super_type = gfc_get_derived_super_type (derived);
11784 resolve_symbol (super_type);
11786 resolve_bindings_derived = derived;
11787 resolve_bindings_result = true;
11789 /* Make sure the vtab has been generated. */
11790 gfc_find_derived_vtab (derived);
11792 if (derived->f2k_derived->tb_sym_root)
11793 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11794 &resolve_typebound_procedure);
11796 if (derived->f2k_derived->tb_uop_root)
11797 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11798 &resolve_typebound_user_op);
11800 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11802 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11803 if (p && !resolve_typebound_intrinsic_op (derived,
11804 (gfc_intrinsic_op)op, p))
11805 resolve_bindings_result = false;
11808 return resolve_bindings_result;
11812 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11813 to give all identical derived types the same backend_decl. */
11815 add_dt_to_dt_list (gfc_symbol *derived)
11817 gfc_dt_list *dt_list;
11819 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11820 if (derived == dt_list->derived)
11823 dt_list = gfc_get_dt_list ();
11824 dt_list->next = gfc_derived_types;
11825 dt_list->derived = derived;
11826 gfc_derived_types = dt_list;
11830 /* Ensure that a derived-type is really not abstract, meaning that every
11831 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11834 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11839 if (!ensure_not_abstract_walker (sub, st->left))
11841 if (!ensure_not_abstract_walker (sub, st->right))
11844 if (st->n.tb && st->n.tb->deferred)
11846 gfc_symtree* overriding;
11847 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11850 gcc_assert (overriding->n.tb);
11851 if (overriding->n.tb->deferred)
11853 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11854 " '%s' is DEFERRED and not overridden",
11855 sub->name, &sub->declared_at, st->name);
11864 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11866 /* The algorithm used here is to recursively travel up the ancestry of sub
11867 and for each ancestor-type, check all bindings. If any of them is
11868 DEFERRED, look it up starting from sub and see if the found (overriding)
11869 binding is not DEFERRED.
11870 This is not the most efficient way to do this, but it should be ok and is
11871 clearer than something sophisticated. */
11873 gcc_assert (ancestor && !sub->attr.abstract);
11875 if (!ancestor->attr.abstract)
11878 /* Walk bindings of this ancestor. */
11879 if (ancestor->f2k_derived)
11882 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11887 /* Find next ancestor type and recurse on it. */
11888 ancestor = gfc_get_derived_super_type (ancestor);
11890 return ensure_not_abstract (sub, ancestor);
11896 /* This check for typebound defined assignments is done recursively
11897 since the order in which derived types are resolved is not always in
11898 order of the declarations. */
11901 check_defined_assignments (gfc_symbol *derived)
11905 for (c = derived->components; c; c = c->next)
11907 if (c->ts.type != BT_DERIVED
11909 || c->attr.allocatable
11910 || c->attr.proc_pointer_comp
11911 || c->attr.class_pointer
11912 || c->attr.proc_pointer)
11915 if (c->ts.u.derived->attr.defined_assign_comp
11916 || (c->ts.u.derived->f2k_derived
11917 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
11919 derived->attr.defined_assign_comp = 1;
11923 check_defined_assignments (c->ts.u.derived);
11924 if (c->ts.u.derived->attr.defined_assign_comp)
11926 derived->attr.defined_assign_comp = 1;
11933 /* Resolve the components of a derived type. This does not have to wait until
11934 resolution stage, but can be done as soon as the dt declaration has been
11938 resolve_fl_derived0 (gfc_symbol *sym)
11940 gfc_symbol* super_type;
11943 if (sym->attr.unlimited_polymorphic)
11946 super_type = gfc_get_derived_super_type (sym);
11949 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11951 gfc_error ("As extending type '%s' at %L has a coarray component, "
11952 "parent type '%s' shall also have one", sym->name,
11953 &sym->declared_at, super_type->name);
11957 /* Ensure the extended type gets resolved before we do. */
11958 if (super_type && !resolve_fl_derived0 (super_type))
11961 /* An ABSTRACT type must be extensible. */
11962 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11964 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11965 sym->name, &sym->declared_at);
11969 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
11972 for ( ; c != NULL; c = c->next)
11974 if (c->attr.artificial)
11977 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
11978 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
11980 gfc_error ("Deferred-length character component '%s' at %L is not "
11981 "yet supported", c->name, &c->loc);
11986 if ((!sym->attr.is_class || c != sym->components)
11987 && c->attr.codimension
11988 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11990 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11991 "deferred shape", c->name, &c->loc);
11996 if (c->attr.codimension && c->ts.type == BT_DERIVED
11997 && c->ts.u.derived->ts.is_iso_c)
11999 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12000 "shall not be a coarray", c->name, &c->loc);
12005 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12006 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12007 || c->attr.allocatable))
12009 gfc_error ("Component '%s' at %L with coarray component "
12010 "shall be a nonpointer, nonallocatable scalar",
12016 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12018 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12019 "is not an array pointer", c->name, &c->loc);
12023 if (c->attr.proc_pointer && c->ts.interface)
12025 gfc_symbol *ifc = c->ts.interface;
12027 if (!sym->attr.vtype
12028 && !check_proc_interface (ifc, &c->loc))
12031 if (ifc->attr.if_source || ifc->attr.intrinsic)
12033 /* Resolve interface and copy attributes. */
12034 if (ifc->formal && !ifc->formal_ns)
12035 resolve_symbol (ifc);
12036 if (ifc->attr.intrinsic)
12037 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12041 c->ts = ifc->result->ts;
12042 c->attr.allocatable = ifc->result->attr.allocatable;
12043 c->attr.pointer = ifc->result->attr.pointer;
12044 c->attr.dimension = ifc->result->attr.dimension;
12045 c->as = gfc_copy_array_spec (ifc->result->as);
12046 c->attr.class_ok = ifc->result->attr.class_ok;
12051 c->attr.allocatable = ifc->attr.allocatable;
12052 c->attr.pointer = ifc->attr.pointer;
12053 c->attr.dimension = ifc->attr.dimension;
12054 c->as = gfc_copy_array_spec (ifc->as);
12055 c->attr.class_ok = ifc->attr.class_ok;
12057 c->ts.interface = ifc;
12058 c->attr.function = ifc->attr.function;
12059 c->attr.subroutine = ifc->attr.subroutine;
12061 c->attr.pure = ifc->attr.pure;
12062 c->attr.elemental = ifc->attr.elemental;
12063 c->attr.recursive = ifc->attr.recursive;
12064 c->attr.always_explicit = ifc->attr.always_explicit;
12065 c->attr.ext_attr |= ifc->attr.ext_attr;
12066 /* Copy char length. */
12067 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12069 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12070 if (cl->length && !cl->resolved
12071 && !gfc_resolve_expr (cl->length))
12077 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12079 /* Since PPCs are not implicitly typed, a PPC without an explicit
12080 interface must be a subroutine. */
12081 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12084 /* Procedure pointer components: Check PASS arg. */
12085 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12086 && !sym->attr.vtype)
12088 gfc_symbol* me_arg;
12090 if (c->tb->pass_arg)
12092 gfc_formal_arglist* i;
12094 /* If an explicit passing argument name is given, walk the arg-list
12095 and look for it. */
12098 c->tb->pass_arg_num = 1;
12099 for (i = c->ts.interface->formal; i; i = i->next)
12101 if (!strcmp (i->sym->name, c->tb->pass_arg))
12106 c->tb->pass_arg_num++;
12111 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12112 "at %L has no argument '%s'", c->name,
12113 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12120 /* Otherwise, take the first one; there should in fact be at least
12122 c->tb->pass_arg_num = 1;
12123 if (!c->ts.interface->formal)
12125 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12126 "must have at least one argument",
12131 me_arg = c->ts.interface->formal->sym;
12134 /* Now check that the argument-type matches. */
12135 gcc_assert (me_arg);
12136 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12137 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12138 || (me_arg->ts.type == BT_CLASS
12139 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12141 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12142 " the derived type '%s'", me_arg->name, c->name,
12143 me_arg->name, &c->loc, sym->name);
12148 /* Check for C453. */
12149 if (me_arg->attr.dimension)
12151 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12152 "must be scalar", me_arg->name, c->name, me_arg->name,
12158 if (me_arg->attr.pointer)
12160 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12161 "may not have the POINTER attribute", me_arg->name,
12162 c->name, me_arg->name, &c->loc);
12167 if (me_arg->attr.allocatable)
12169 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12170 "may not be ALLOCATABLE", me_arg->name, c->name,
12171 me_arg->name, &c->loc);
12176 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12177 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12178 " at %L", c->name, &c->loc);
12182 /* Check type-spec if this is not the parent-type component. */
12183 if (((sym->attr.is_class
12184 && (!sym->components->ts.u.derived->attr.extension
12185 || c != sym->components->ts.u.derived->components))
12186 || (!sym->attr.is_class
12187 && (!sym->attr.extension || c != sym->components)))
12188 && !sym->attr.vtype
12189 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12192 /* If this type is an extension, set the accessibility of the parent
12195 && ((sym->attr.is_class
12196 && c == sym->components->ts.u.derived->components)
12197 || (!sym->attr.is_class && c == sym->components))
12198 && strcmp (super_type->name, c->name) == 0)
12199 c->attr.access = super_type->attr.access;
12201 /* If this type is an extension, see if this component has the same name
12202 as an inherited type-bound procedure. */
12203 if (super_type && !sym->attr.is_class
12204 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12206 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12207 " inherited type-bound procedure",
12208 c->name, sym->name, &c->loc);
12212 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12213 && !c->ts.deferred)
12215 if (c->ts.u.cl->length == NULL
12216 || (!resolve_charlen(c->ts.u.cl))
12217 || !gfc_is_constant_expr (c->ts.u.cl->length))
12219 gfc_error ("Character length of component '%s' needs to "
12220 "be a constant specification expression at %L",
12222 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12227 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12228 && !c->attr.pointer && !c->attr.allocatable)
12230 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12231 "length must be a POINTER or ALLOCATABLE",
12232 c->name, sym->name, &c->loc);
12236 if (c->ts.type == BT_DERIVED
12237 && sym->component_access != ACCESS_PRIVATE
12238 && gfc_check_symbol_access (sym)
12239 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12240 && !c->ts.u.derived->attr.use_assoc
12241 && !gfc_check_symbol_access (c->ts.u.derived)
12242 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12243 "PRIVATE type and cannot be a component of "
12244 "'%s', which is PUBLIC at %L", c->name,
12245 sym->name, &sym->declared_at))
12248 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12250 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12251 "type %s", c->name, &c->loc, sym->name);
12255 if (sym->attr.sequence)
12257 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12259 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12260 "not have the SEQUENCE attribute",
12261 c->ts.u.derived->name, &sym->declared_at);
12266 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12267 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12268 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12269 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12270 CLASS_DATA (c)->ts.u.derived
12271 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12273 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12274 && c->attr.pointer && c->ts.u.derived->components == NULL
12275 && !c->ts.u.derived->attr.zero_comp)
12277 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12278 "that has not been declared", c->name, sym->name,
12283 if (c->ts.type == BT_CLASS && c->attr.class_ok
12284 && CLASS_DATA (c)->attr.class_pointer
12285 && CLASS_DATA (c)->ts.u.derived->components == NULL
12286 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12287 && !UNLIMITED_POLY (c))
12289 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12290 "that has not been declared", c->name, sym->name,
12296 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12297 && (!c->attr.class_ok
12298 || !(CLASS_DATA (c)->attr.class_pointer
12299 || CLASS_DATA (c)->attr.allocatable)))
12301 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12302 "or pointer", c->name, &c->loc);
12303 /* Prevent a recurrence of the error. */
12304 c->ts.type = BT_UNKNOWN;
12308 /* Ensure that all the derived type components are put on the
12309 derived type list; even in formal namespaces, where derived type
12310 pointer components might not have been declared. */
12311 if (c->ts.type == BT_DERIVED
12313 && c->ts.u.derived->components
12315 && sym != c->ts.u.derived)
12316 add_dt_to_dt_list (c->ts.u.derived);
12318 if (!gfc_resolve_array_spec (c->as,
12319 !(c->attr.pointer || c->attr.proc_pointer
12320 || c->attr.allocatable)))
12323 if (c->initializer && !sym->attr.vtype
12324 && !gfc_check_assign_symbol (sym, c, c->initializer))
12328 check_defined_assignments (sym);
12330 if (!sym->attr.defined_assign_comp && super_type)
12331 sym->attr.defined_assign_comp
12332 = super_type->attr.defined_assign_comp;
12334 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12335 all DEFERRED bindings are overridden. */
12336 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12337 && !sym->attr.is_class
12338 && !ensure_not_abstract (sym, super_type))
12341 /* Add derived type to the derived type list. */
12342 add_dt_to_dt_list (sym);
12344 /* Check if the type is finalizable. This is done in order to ensure that the
12345 finalization wrapper is generated early enough. */
12346 gfc_is_finalizable (sym, NULL);
12352 /* The following procedure does the full resolution of a derived type,
12353 including resolution of all type-bound procedures (if present). In contrast
12354 to 'resolve_fl_derived0' this can only be done after the module has been
12355 parsed completely. */
12358 resolve_fl_derived (gfc_symbol *sym)
12360 gfc_symbol *gen_dt = NULL;
12362 if (sym->attr.unlimited_polymorphic)
12365 if (!sym->attr.is_class)
12366 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12367 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12368 && (!gen_dt->generic->sym->attr.use_assoc
12369 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12370 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12371 "'%s' at %L being the same name as derived "
12372 "type at %L", sym->name,
12373 gen_dt->generic->sym == sym
12374 ? gen_dt->generic->next->sym->name
12375 : gen_dt->generic->sym->name,
12376 gen_dt->generic->sym == sym
12377 ? &gen_dt->generic->next->sym->declared_at
12378 : &gen_dt->generic->sym->declared_at,
12379 &sym->declared_at))
12382 /* Resolve the finalizer procedures. */
12383 if (!gfc_resolve_finalizers (sym))
12386 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12388 /* Fix up incomplete CLASS symbols. */
12389 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12390 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12392 /* Nothing more to do for unlimited polymorphic entities. */
12393 if (data->ts.u.derived->attr.unlimited_polymorphic)
12395 else if (vptr->ts.u.derived == NULL)
12397 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12399 vptr->ts.u.derived = vtab->ts.u.derived;
12403 if (!resolve_fl_derived0 (sym))
12406 /* Resolve the type-bound procedures. */
12407 if (!resolve_typebound_procedures (sym))
12415 resolve_fl_namelist (gfc_symbol *sym)
12420 for (nl = sym->namelist; nl; nl = nl->next)
12422 /* Check again, the check in match only works if NAMELIST comes
12424 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12426 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12427 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12431 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12432 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12433 "with assumed shape in namelist '%s' at %L",
12434 nl->sym->name, sym->name, &sym->declared_at))
12437 if (is_non_constant_shape_array (nl->sym)
12438 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12439 "with nonconstant shape in namelist '%s' at %L",
12440 nl->sym->name, sym->name, &sym->declared_at))
12443 if (nl->sym->ts.type == BT_CHARACTER
12444 && (nl->sym->ts.u.cl->length == NULL
12445 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12446 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12447 "nonconstant character length in "
12448 "namelist '%s' at %L", nl->sym->name,
12449 sym->name, &sym->declared_at))
12452 /* FIXME: Once UDDTIO is implemented, the following can be
12454 if (nl->sym->ts.type == BT_CLASS)
12456 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12457 "polymorphic and requires a defined input/output "
12458 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12462 if (nl->sym->ts.type == BT_DERIVED
12463 && (nl->sym->ts.u.derived->attr.alloc_comp
12464 || nl->sym->ts.u.derived->attr.pointer_comp))
12466 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12467 "namelist '%s' at %L with ALLOCATABLE "
12468 "or POINTER components", nl->sym->name,
12469 sym->name, &sym->declared_at))
12472 /* FIXME: Once UDDTIO is implemented, the following can be
12474 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12475 "ALLOCATABLE or POINTER components and thus requires "
12476 "a defined input/output procedure", nl->sym->name,
12477 sym->name, &sym->declared_at);
12482 /* Reject PRIVATE objects in a PUBLIC namelist. */
12483 if (gfc_check_symbol_access (sym))
12485 for (nl = sym->namelist; nl; nl = nl->next)
12487 if (!nl->sym->attr.use_assoc
12488 && !is_sym_host_assoc (nl->sym, sym->ns)
12489 && !gfc_check_symbol_access (nl->sym))
12491 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12492 "cannot be member of PUBLIC namelist '%s' at %L",
12493 nl->sym->name, sym->name, &sym->declared_at);
12497 /* Types with private components that came here by USE-association. */
12498 if (nl->sym->ts.type == BT_DERIVED
12499 && derived_inaccessible (nl->sym->ts.u.derived))
12501 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12502 "components and cannot be member of namelist '%s' at %L",
12503 nl->sym->name, sym->name, &sym->declared_at);
12507 /* Types with private components that are defined in the same module. */
12508 if (nl->sym->ts.type == BT_DERIVED
12509 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12510 && nl->sym->ts.u.derived->attr.private_comp)
12512 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12513 "cannot be a member of PUBLIC namelist '%s' at %L",
12514 nl->sym->name, sym->name, &sym->declared_at);
12521 /* 14.1.2 A module or internal procedure represent local entities
12522 of the same type as a namelist member and so are not allowed. */
12523 for (nl = sym->namelist; nl; nl = nl->next)
12525 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12528 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12529 if ((nl->sym == sym->ns->proc_name)
12531 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12536 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12537 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12539 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12540 "attribute in '%s' at %L", nlsym->name,
12541 &sym->declared_at);
12551 resolve_fl_parameter (gfc_symbol *sym)
12553 /* A parameter array's shape needs to be constant. */
12554 if (sym->as != NULL
12555 && (sym->as->type == AS_DEFERRED
12556 || is_non_constant_shape_array (sym)))
12558 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12559 "or of deferred shape", sym->name, &sym->declared_at);
12563 /* Make sure a parameter that has been implicitly typed still
12564 matches the implicit type, since PARAMETER statements can precede
12565 IMPLICIT statements. */
12566 if (sym->attr.implicit_type
12567 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12570 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12571 "later IMPLICIT type", sym->name, &sym->declared_at);
12575 /* Make sure the types of derived parameters are consistent. This
12576 type checking is deferred until resolution because the type may
12577 refer to a derived type from the host. */
12578 if (sym->ts.type == BT_DERIVED
12579 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12581 gfc_error ("Incompatible derived type in PARAMETER at %L",
12582 &sym->value->where);
12589 /* Do anything necessary to resolve a symbol. Right now, we just
12590 assume that an otherwise unknown symbol is a variable. This sort
12591 of thing commonly happens for symbols in module. */
12594 resolve_symbol (gfc_symbol *sym)
12596 int check_constant, mp_flag;
12597 gfc_symtree *symtree;
12598 gfc_symtree *this_symtree;
12601 symbol_attribute class_attr;
12602 gfc_array_spec *as;
12603 bool saved_specification_expr;
12609 if (sym->attr.artificial)
12612 if (sym->attr.unlimited_polymorphic)
12615 if (sym->attr.flavor == FL_UNKNOWN
12616 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12617 && !sym->attr.generic && !sym->attr.external
12618 && sym->attr.if_source == IFSRC_UNKNOWN))
12621 /* If we find that a flavorless symbol is an interface in one of the
12622 parent namespaces, find its symtree in this namespace, free the
12623 symbol and set the symtree to point to the interface symbol. */
12624 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12626 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12627 if (symtree && (symtree->n.sym->generic ||
12628 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12629 && sym->ns->construct_entities)))
12631 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12633 gfc_release_symbol (sym);
12634 symtree->n.sym->refs++;
12635 this_symtree->n.sym = symtree->n.sym;
12640 /* Otherwise give it a flavor according to such attributes as
12642 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12643 && sym->attr.intrinsic == 0)
12644 sym->attr.flavor = FL_VARIABLE;
12645 else if (sym->attr.flavor == FL_UNKNOWN)
12647 sym->attr.flavor = FL_PROCEDURE;
12648 if (sym->attr.dimension)
12649 sym->attr.function = 1;
12653 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12654 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12656 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12657 && !resolve_procedure_interface (sym))
12660 if (sym->attr.is_protected && !sym->attr.proc_pointer
12661 && (sym->attr.procedure || sym->attr.external))
12663 if (sym->attr.external)
12664 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12665 "at %L", &sym->declared_at);
12667 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12668 "at %L", &sym->declared_at);
12673 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12676 /* Symbols that are module procedures with results (functions) have
12677 the types and array specification copied for type checking in
12678 procedures that call them, as well as for saving to a module
12679 file. These symbols can't stand the scrutiny that their results
12681 mp_flag = (sym->result != NULL && sym->result != sym);
12683 /* Make sure that the intrinsic is consistent with its internal
12684 representation. This needs to be done before assigning a default
12685 type to avoid spurious warnings. */
12686 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12687 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12690 /* Resolve associate names. */
12692 resolve_assoc_var (sym, true);
12694 /* Assign default type to symbols that need one and don't have one. */
12695 if (sym->ts.type == BT_UNKNOWN)
12697 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12699 gfc_set_default_type (sym, 1, NULL);
12702 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12703 && !sym->attr.function && !sym->attr.subroutine
12704 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12705 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12707 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12709 /* The specific case of an external procedure should emit an error
12710 in the case that there is no implicit type. */
12712 gfc_set_default_type (sym, sym->attr.external, NULL);
12715 /* Result may be in another namespace. */
12716 resolve_symbol (sym->result);
12718 if (!sym->result->attr.proc_pointer)
12720 sym->ts = sym->result->ts;
12721 sym->as = gfc_copy_array_spec (sym->result->as);
12722 sym->attr.dimension = sym->result->attr.dimension;
12723 sym->attr.pointer = sym->result->attr.pointer;
12724 sym->attr.allocatable = sym->result->attr.allocatable;
12725 sym->attr.contiguous = sym->result->attr.contiguous;
12730 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12732 bool saved_specification_expr = specification_expr;
12733 specification_expr = true;
12734 gfc_resolve_array_spec (sym->result->as, false);
12735 specification_expr = saved_specification_expr;
12738 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12740 as = CLASS_DATA (sym)->as;
12741 class_attr = CLASS_DATA (sym)->attr;
12742 class_attr.pointer = class_attr.class_pointer;
12746 class_attr = sym->attr;
12751 if (sym->attr.contiguous
12752 && (!class_attr.dimension
12753 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12754 && !class_attr.pointer)))
12756 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12757 "array pointer or an assumed-shape or assumed-rank array",
12758 sym->name, &sym->declared_at);
12762 /* Assumed size arrays and assumed shape arrays must be dummy
12763 arguments. Array-spec's of implied-shape should have been resolved to
12764 AS_EXPLICIT already. */
12768 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12769 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12770 || as->type == AS_ASSUMED_SHAPE)
12771 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12773 if (as->type == AS_ASSUMED_SIZE)
12774 gfc_error ("Assumed size array at %L must be a dummy argument",
12775 &sym->declared_at);
12777 gfc_error ("Assumed shape array at %L must be a dummy argument",
12778 &sym->declared_at);
12781 /* TS 29113, C535a. */
12782 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12783 && !sym->attr.select_type_temporary)
12785 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12786 &sym->declared_at);
12789 if (as->type == AS_ASSUMED_RANK
12790 && (sym->attr.codimension || sym->attr.value))
12792 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12793 "CODIMENSION attribute", &sym->declared_at);
12798 /* Make sure symbols with known intent or optional are really dummy
12799 variable. Because of ENTRY statement, this has to be deferred
12800 until resolution time. */
12802 if (!sym->attr.dummy
12803 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12805 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12809 if (sym->attr.value && !sym->attr.dummy)
12811 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12812 "it is not a dummy argument", sym->name, &sym->declared_at);
12816 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12818 gfc_charlen *cl = sym->ts.u.cl;
12819 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12821 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12822 "attribute must have constant length",
12823 sym->name, &sym->declared_at);
12827 if (sym->ts.is_c_interop
12828 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12830 gfc_error ("C interoperable character dummy variable '%s' at %L "
12831 "with VALUE attribute must have length one",
12832 sym->name, &sym->declared_at);
12837 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12838 && sym->ts.u.derived->attr.generic)
12840 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12841 if (!sym->ts.u.derived)
12843 gfc_error ("The derived type '%s' at %L is of type '%s', "
12844 "which has not been defined", sym->name,
12845 &sym->declared_at, sym->ts.u.derived->name);
12846 sym->ts.type = BT_UNKNOWN;
12851 /* Use the same constraints as TYPE(*), except for the type check
12852 and that only scalars and assumed-size arrays are permitted. */
12853 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12855 if (!sym->attr.dummy)
12857 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12858 "a dummy argument", sym->name, &sym->declared_at);
12862 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12863 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12864 && sym->ts.type != BT_COMPLEX)
12866 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12867 "of type TYPE(*) or of an numeric intrinsic type",
12868 sym->name, &sym->declared_at);
12872 if (sym->attr.allocatable || sym->attr.codimension
12873 || sym->attr.pointer || sym->attr.value)
12875 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12876 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12877 "attribute", sym->name, &sym->declared_at);
12881 if (sym->attr.intent == INTENT_OUT)
12883 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12884 "have the INTENT(OUT) attribute",
12885 sym->name, &sym->declared_at);
12888 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
12890 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
12891 "either be a scalar or an assumed-size array",
12892 sym->name, &sym->declared_at);
12896 /* Set the type to TYPE(*) and add a dimension(*) to ensure
12897 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
12899 sym->ts.type = BT_ASSUMED;
12900 sym->as = gfc_get_array_spec ();
12901 sym->as->type = AS_ASSUMED_SIZE;
12903 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
12905 else if (sym->ts.type == BT_ASSUMED)
12907 /* TS 29113, C407a. */
12908 if (!sym->attr.dummy)
12910 gfc_error ("Assumed type of variable %s at %L is only permitted "
12911 "for dummy variables", sym->name, &sym->declared_at);
12914 if (sym->attr.allocatable || sym->attr.codimension
12915 || sym->attr.pointer || sym->attr.value)
12917 gfc_error ("Assumed-type variable %s at %L may not have the "
12918 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
12919 sym->name, &sym->declared_at);
12922 if (sym->attr.intent == INTENT_OUT)
12924 gfc_error ("Assumed-type variable %s at %L may not have the "
12925 "INTENT(OUT) attribute",
12926 sym->name, &sym->declared_at);
12929 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
12931 gfc_error ("Assumed-type variable %s at %L shall not be an "
12932 "explicit-shape array", sym->name, &sym->declared_at);
12937 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12938 do this for something that was implicitly typed because that is handled
12939 in gfc_set_default_type. Handle dummy arguments and procedure
12940 definitions separately. Also, anything that is use associated is not
12941 handled here but instead is handled in the module it is declared in.
12942 Finally, derived type definitions are allowed to be BIND(C) since that
12943 only implies that they're interoperable, and they are checked fully for
12944 interoperability when a variable is declared of that type. */
12945 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12946 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12947 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12951 /* First, make sure the variable is declared at the
12952 module-level scope (J3/04-007, Section 15.3). */
12953 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12954 sym->attr.in_common == 0)
12956 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12957 "is neither a COMMON block nor declared at the "
12958 "module level scope", sym->name, &(sym->declared_at));
12961 else if (sym->common_head != NULL)
12963 t = verify_com_block_vars_c_interop (sym->common_head);
12967 /* If type() declaration, we need to verify that the components
12968 of the given type are all C interoperable, etc. */
12969 if (sym->ts.type == BT_DERIVED &&
12970 sym->ts.u.derived->attr.is_c_interop != 1)
12972 /* Make sure the user marked the derived type as BIND(C). If
12973 not, call the verify routine. This could print an error
12974 for the derived type more than once if multiple variables
12975 of that type are declared. */
12976 if (sym->ts.u.derived->attr.is_bind_c != 1)
12977 verify_bind_c_derived_type (sym->ts.u.derived);
12981 /* Verify the variable itself as C interoperable if it
12982 is BIND(C). It is not possible for this to succeed if
12983 the verify_bind_c_derived_type failed, so don't have to handle
12984 any error returned by verify_bind_c_derived_type. */
12985 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12986 sym->common_block);
12991 /* clear the is_bind_c flag to prevent reporting errors more than
12992 once if something failed. */
12993 sym->attr.is_bind_c = 0;
12998 /* If a derived type symbol has reached this point, without its
12999 type being declared, we have an error. Notice that most
13000 conditions that produce undefined derived types have already
13001 been dealt with. However, the likes of:
13002 implicit type(t) (t) ..... call foo (t) will get us here if
13003 the type is not declared in the scope of the implicit
13004 statement. Change the type to BT_UNKNOWN, both because it is so
13005 and to prevent an ICE. */
13006 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13007 && sym->ts.u.derived->components == NULL
13008 && !sym->ts.u.derived->attr.zero_comp)
13010 gfc_error ("The derived type '%s' at %L is of type '%s', "
13011 "which has not been defined", sym->name,
13012 &sym->declared_at, sym->ts.u.derived->name);
13013 sym->ts.type = BT_UNKNOWN;
13017 /* Make sure that the derived type has been resolved and that the
13018 derived type is visible in the symbol's namespace, if it is a
13019 module function and is not PRIVATE. */
13020 if (sym->ts.type == BT_DERIVED
13021 && sym->ts.u.derived->attr.use_assoc
13022 && sym->ns->proc_name
13023 && sym->ns->proc_name->attr.flavor == FL_MODULE
13024 && !resolve_fl_derived (sym->ts.u.derived))
13027 /* Unless the derived-type declaration is use associated, Fortran 95
13028 does not allow public entries of private derived types.
13029 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13030 161 in 95-006r3. */
13031 if (sym->ts.type == BT_DERIVED
13032 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13033 && !sym->ts.u.derived->attr.use_assoc
13034 && gfc_check_symbol_access (sym)
13035 && !gfc_check_symbol_access (sym->ts.u.derived)
13036 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13037 "derived type '%s'",
13038 (sym->attr.flavor == FL_PARAMETER)
13039 ? "parameter" : "variable",
13040 sym->name, &sym->declared_at,
13041 sym->ts.u.derived->name))
13044 /* F2008, C1302. */
13045 if (sym->ts.type == BT_DERIVED
13046 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13047 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13048 || sym->ts.u.derived->attr.lock_comp)
13049 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13051 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13052 "type LOCK_TYPE must be a coarray", sym->name,
13053 &sym->declared_at);
13057 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13058 default initialization is defined (5.1.2.4.4). */
13059 if (sym->ts.type == BT_DERIVED
13061 && sym->attr.intent == INTENT_OUT
13063 && sym->as->type == AS_ASSUMED_SIZE)
13065 for (c = sym->ts.u.derived->components; c; c = c->next)
13067 if (c->initializer)
13069 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13070 "ASSUMED SIZE and so cannot have a default initializer",
13071 sym->name, &sym->declared_at);
13078 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13079 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13081 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13082 "INTENT(OUT)", sym->name, &sym->declared_at);
13087 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13088 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13089 && CLASS_DATA (sym)->attr.coarray_comp))
13090 || class_attr.codimension)
13091 && (sym->attr.result || sym->result == sym))
13093 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13094 "a coarray component", sym->name, &sym->declared_at);
13099 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13100 && sym->ts.u.derived->ts.is_iso_c)
13102 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13103 "shall not be a coarray", sym->name, &sym->declared_at);
13108 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13109 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13110 && CLASS_DATA (sym)->attr.coarray_comp))
13111 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13112 || class_attr.allocatable))
13114 gfc_error ("Variable '%s' at %L with coarray component "
13115 "shall be a nonpointer, nonallocatable scalar",
13116 sym->name, &sym->declared_at);
13120 /* F2008, C526. The function-result case was handled above. */
13121 if (class_attr.codimension
13122 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13123 || sym->attr.select_type_temporary
13124 || sym->ns->save_all
13125 || sym->ns->proc_name->attr.flavor == FL_MODULE
13126 || sym->ns->proc_name->attr.is_main_program
13127 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13129 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13130 "nor a dummy argument", sym->name, &sym->declared_at);
13134 else if (class_attr.codimension && !sym->attr.select_type_temporary
13135 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13137 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13138 "deferred shape", sym->name, &sym->declared_at);
13141 else if (class_attr.codimension && class_attr.allocatable && as
13142 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13144 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13145 "deferred shape", sym->name, &sym->declared_at);
13150 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13151 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13152 && CLASS_DATA (sym)->attr.coarray_comp))
13153 || (class_attr.codimension && class_attr.allocatable))
13154 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13156 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13157 "allocatable coarray or have coarray components",
13158 sym->name, &sym->declared_at);
13162 if (class_attr.codimension && sym->attr.dummy
13163 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13165 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13166 "procedure '%s'", sym->name, &sym->declared_at,
13167 sym->ns->proc_name->name);
13171 if (sym->ts.type == BT_LOGICAL
13172 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13173 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13174 && sym->ns->proc_name->attr.is_bind_c)))
13177 for (i = 0; gfc_logical_kinds[i].kind; i++)
13178 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13180 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13181 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13182 "%L with non-C_Bool kind in BIND(C) procedure "
13183 "'%s'", sym->name, &sym->declared_at,
13184 sym->ns->proc_name->name))
13186 else if (!gfc_logical_kinds[i].c_bool
13187 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13188 "'%s' at %L with non-C_Bool kind in "
13189 "BIND(C) procedure '%s'", sym->name,
13191 sym->attr.function ? sym->name
13192 : sym->ns->proc_name->name))
13196 switch (sym->attr.flavor)
13199 if (!resolve_fl_variable (sym, mp_flag))
13204 if (!resolve_fl_procedure (sym, mp_flag))
13209 if (!resolve_fl_namelist (sym))
13214 if (!resolve_fl_parameter (sym))
13222 /* Resolve array specifier. Check as well some constraints
13223 on COMMON blocks. */
13225 check_constant = sym->attr.in_common && !sym->attr.pointer;
13227 /* Set the formal_arg_flag so that check_conflict will not throw
13228 an error for host associated variables in the specification
13229 expression for an array_valued function. */
13230 if (sym->attr.function && sym->as)
13231 formal_arg_flag = 1;
13233 saved_specification_expr = specification_expr;
13234 specification_expr = true;
13235 gfc_resolve_array_spec (sym->as, check_constant);
13236 specification_expr = saved_specification_expr;
13238 formal_arg_flag = 0;
13240 /* Resolve formal namespaces. */
13241 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13242 && !sym->attr.contained && !sym->attr.intrinsic)
13243 gfc_resolve (sym->formal_ns);
13245 /* Make sure the formal namespace is present. */
13246 if (sym->formal && !sym->formal_ns)
13248 gfc_formal_arglist *formal = sym->formal;
13249 while (formal && !formal->sym)
13250 formal = formal->next;
13254 sym->formal_ns = formal->sym->ns;
13255 if (sym->ns != formal->sym->ns)
13256 sym->formal_ns->refs++;
13260 /* Check threadprivate restrictions. */
13261 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13262 && (!sym->attr.in_common
13263 && sym->module == NULL
13264 && (sym->ns->proc_name == NULL
13265 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13266 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13268 /* If we have come this far we can apply default-initializers, as
13269 described in 14.7.5, to those variables that have not already
13270 been assigned one. */
13271 if (sym->ts.type == BT_DERIVED
13273 && !sym->attr.allocatable
13274 && !sym->attr.alloc_comp)
13276 symbol_attribute *a = &sym->attr;
13278 if ((!a->save && !a->dummy && !a->pointer
13279 && !a->in_common && !a->use_assoc
13280 && (a->referenced || a->result)
13281 && !(a->function && sym != sym->result))
13282 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13283 apply_default_init (sym);
13286 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13287 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13288 && !CLASS_DATA (sym)->attr.class_pointer
13289 && !CLASS_DATA (sym)->attr.allocatable)
13290 apply_default_init (sym);
13292 /* If this symbol has a type-spec, check it. */
13293 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13294 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13295 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13300 /************* Resolve DATA statements *************/
13304 gfc_data_value *vnode;
13310 /* Advance the values structure to point to the next value in the data list. */
13313 next_data_value (void)
13315 while (mpz_cmp_ui (values.left, 0) == 0)
13318 if (values.vnode->next == NULL)
13321 values.vnode = values.vnode->next;
13322 mpz_set (values.left, values.vnode->repeat);
13330 check_data_variable (gfc_data_variable *var, locus *where)
13336 ar_type mark = AR_UNKNOWN;
13338 mpz_t section_index[GFC_MAX_DIMENSIONS];
13344 if (!gfc_resolve_expr (var->expr))
13348 mpz_init_set_si (offset, 0);
13351 if (e->expr_type != EXPR_VARIABLE)
13352 gfc_internal_error ("check_data_variable(): Bad expression");
13354 sym = e->symtree->n.sym;
13356 if (sym->ns->is_block_data && !sym->attr.in_common)
13358 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13359 sym->name, &sym->declared_at);
13362 if (e->ref == NULL && sym->as)
13364 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13365 " declaration", sym->name, where);
13369 has_pointer = sym->attr.pointer;
13371 if (gfc_is_coindexed (e))
13373 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13378 for (ref = e->ref; ref; ref = ref->next)
13380 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13384 && ref->type == REF_ARRAY
13385 && ref->u.ar.type != AR_FULL)
13387 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13388 "be a full array", sym->name, where);
13393 if (e->rank == 0 || has_pointer)
13395 mpz_init_set_ui (size, 1);
13402 /* Find the array section reference. */
13403 for (ref = e->ref; ref; ref = ref->next)
13405 if (ref->type != REF_ARRAY)
13407 if (ref->u.ar.type == AR_ELEMENT)
13413 /* Set marks according to the reference pattern. */
13414 switch (ref->u.ar.type)
13422 /* Get the start position of array section. */
13423 gfc_get_section_index (ar, section_index, &offset);
13428 gcc_unreachable ();
13431 if (!gfc_array_size (e, &size))
13433 gfc_error ("Nonconstant array section at %L in DATA statement",
13435 mpz_clear (offset);
13442 while (mpz_cmp_ui (size, 0) > 0)
13444 if (!next_data_value ())
13446 gfc_error ("DATA statement at %L has more variables than values",
13452 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13456 /* If we have more than one element left in the repeat count,
13457 and we have more than one element left in the target variable,
13458 then create a range assignment. */
13459 /* FIXME: Only done for full arrays for now, since array sections
13461 if (mark == AR_FULL && ref && ref->next == NULL
13462 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13466 if (mpz_cmp (size, values.left) >= 0)
13468 mpz_init_set (range, values.left);
13469 mpz_sub (size, size, values.left);
13470 mpz_set_ui (values.left, 0);
13474 mpz_init_set (range, size);
13475 mpz_sub (values.left, values.left, size);
13476 mpz_set_ui (size, 0);
13479 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13482 mpz_add (offset, offset, range);
13489 /* Assign initial value to symbol. */
13492 mpz_sub_ui (values.left, values.left, 1);
13493 mpz_sub_ui (size, size, 1);
13495 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13500 if (mark == AR_FULL)
13501 mpz_add_ui (offset, offset, 1);
13503 /* Modify the array section indexes and recalculate the offset
13504 for next element. */
13505 else if (mark == AR_SECTION)
13506 gfc_advance_section (section_index, ar, &offset);
13510 if (mark == AR_SECTION)
13512 for (i = 0; i < ar->dimen; i++)
13513 mpz_clear (section_index[i]);
13517 mpz_clear (offset);
13523 static bool traverse_data_var (gfc_data_variable *, locus *);
13525 /* Iterate over a list of elements in a DATA statement. */
13528 traverse_data_list (gfc_data_variable *var, locus *where)
13531 iterator_stack frame;
13532 gfc_expr *e, *start, *end, *step;
13533 bool retval = true;
13535 mpz_init (frame.value);
13538 start = gfc_copy_expr (var->iter.start);
13539 end = gfc_copy_expr (var->iter.end);
13540 step = gfc_copy_expr (var->iter.step);
13542 if (!gfc_simplify_expr (start, 1)
13543 || start->expr_type != EXPR_CONSTANT)
13545 gfc_error ("start of implied-do loop at %L could not be "
13546 "simplified to a constant value", &start->where);
13550 if (!gfc_simplify_expr (end, 1)
13551 || end->expr_type != EXPR_CONSTANT)
13553 gfc_error ("end of implied-do loop at %L could not be "
13554 "simplified to a constant value", &start->where);
13558 if (!gfc_simplify_expr (step, 1)
13559 || step->expr_type != EXPR_CONSTANT)
13561 gfc_error ("step of implied-do loop at %L could not be "
13562 "simplified to a constant value", &start->where);
13567 mpz_set (trip, end->value.integer);
13568 mpz_sub (trip, trip, start->value.integer);
13569 mpz_add (trip, trip, step->value.integer);
13571 mpz_div (trip, trip, step->value.integer);
13573 mpz_set (frame.value, start->value.integer);
13575 frame.prev = iter_stack;
13576 frame.variable = var->iter.var->symtree;
13577 iter_stack = &frame;
13579 while (mpz_cmp_ui (trip, 0) > 0)
13581 if (!traverse_data_var (var->list, where))
13587 e = gfc_copy_expr (var->expr);
13588 if (!gfc_simplify_expr (e, 1))
13595 mpz_add (frame.value, frame.value, step->value.integer);
13597 mpz_sub_ui (trip, trip, 1);
13601 mpz_clear (frame.value);
13604 gfc_free_expr (start);
13605 gfc_free_expr (end);
13606 gfc_free_expr (step);
13608 iter_stack = frame.prev;
13613 /* Type resolve variables in the variable list of a DATA statement. */
13616 traverse_data_var (gfc_data_variable *var, locus *where)
13620 for (; var; var = var->next)
13622 if (var->expr == NULL)
13623 t = traverse_data_list (var, where);
13625 t = check_data_variable (var, where);
13635 /* Resolve the expressions and iterators associated with a data statement.
13636 This is separate from the assignment checking because data lists should
13637 only be resolved once. */
13640 resolve_data_variables (gfc_data_variable *d)
13642 for (; d; d = d->next)
13644 if (d->list == NULL)
13646 if (!gfc_resolve_expr (d->expr))
13651 if (!gfc_resolve_iterator (&d->iter, false, true))
13654 if (!resolve_data_variables (d->list))
13663 /* Resolve a single DATA statement. We implement this by storing a pointer to
13664 the value list into static variables, and then recursively traversing the
13665 variables list, expanding iterators and such. */
13668 resolve_data (gfc_data *d)
13671 if (!resolve_data_variables (d->var))
13674 values.vnode = d->value;
13675 if (d->value == NULL)
13676 mpz_set_ui (values.left, 0);
13678 mpz_set (values.left, d->value->repeat);
13680 if (!traverse_data_var (d->var, &d->where))
13683 /* At this point, we better not have any values left. */
13685 if (next_data_value ())
13686 gfc_error ("DATA statement at %L has more values than variables",
13691 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13692 accessed by host or use association, is a dummy argument to a pure function,
13693 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13694 is storage associated with any such variable, shall not be used in the
13695 following contexts: (clients of this function). */
13697 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13698 procedure. Returns zero if assignment is OK, nonzero if there is a
13701 gfc_impure_variable (gfc_symbol *sym)
13706 if (sym->attr.use_assoc || sym->attr.in_common)
13709 /* Check if the symbol's ns is inside the pure procedure. */
13710 for (ns = gfc_current_ns; ns; ns = ns->parent)
13714 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13718 proc = sym->ns->proc_name;
13719 if (sym->attr.dummy
13720 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13721 || proc->attr.function))
13724 /* TODO: Sort out what can be storage associated, if anything, and include
13725 it here. In principle equivalences should be scanned but it does not
13726 seem to be possible to storage associate an impure variable this way. */
13731 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13732 current namespace is inside a pure procedure. */
13735 gfc_pure (gfc_symbol *sym)
13737 symbol_attribute attr;
13742 /* Check if the current namespace or one of its parents
13743 belongs to a pure procedure. */
13744 for (ns = gfc_current_ns; ns; ns = ns->parent)
13746 sym = ns->proc_name;
13750 if (attr.flavor == FL_PROCEDURE && attr.pure)
13758 return attr.flavor == FL_PROCEDURE && attr.pure;
13762 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13763 checks if the current namespace is implicitly pure. Note that this
13764 function returns false for a PURE procedure. */
13767 gfc_implicit_pure (gfc_symbol *sym)
13773 /* Check if the current procedure is implicit_pure. Walk up
13774 the procedure list until we find a procedure. */
13775 for (ns = gfc_current_ns; ns; ns = ns->parent)
13777 sym = ns->proc_name;
13781 if (sym->attr.flavor == FL_PROCEDURE)
13786 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13787 && !sym->attr.pure;
13791 /* Test whether the current procedure is elemental or not. */
13794 gfc_elemental (gfc_symbol *sym)
13796 symbol_attribute attr;
13799 sym = gfc_current_ns->proc_name;
13804 return attr.flavor == FL_PROCEDURE && attr.elemental;
13808 /* Warn about unused labels. */
13811 warn_unused_fortran_label (gfc_st_label *label)
13816 warn_unused_fortran_label (label->left);
13818 if (label->defined == ST_LABEL_UNKNOWN)
13821 switch (label->referenced)
13823 case ST_LABEL_UNKNOWN:
13824 gfc_warning ("Label %d at %L defined but not used", label->value,
13828 case ST_LABEL_BAD_TARGET:
13829 gfc_warning ("Label %d at %L defined but cannot be used",
13830 label->value, &label->where);
13837 warn_unused_fortran_label (label->right);
13841 /* Returns the sequence type of a symbol or sequence. */
13844 sequence_type (gfc_typespec ts)
13853 if (ts.u.derived->components == NULL)
13854 return SEQ_NONDEFAULT;
13856 result = sequence_type (ts.u.derived->components->ts);
13857 for (c = ts.u.derived->components->next; c; c = c->next)
13858 if (sequence_type (c->ts) != result)
13864 if (ts.kind != gfc_default_character_kind)
13865 return SEQ_NONDEFAULT;
13867 return SEQ_CHARACTER;
13870 if (ts.kind != gfc_default_integer_kind)
13871 return SEQ_NONDEFAULT;
13873 return SEQ_NUMERIC;
13876 if (!(ts.kind == gfc_default_real_kind
13877 || ts.kind == gfc_default_double_kind))
13878 return SEQ_NONDEFAULT;
13880 return SEQ_NUMERIC;
13883 if (ts.kind != gfc_default_complex_kind)
13884 return SEQ_NONDEFAULT;
13886 return SEQ_NUMERIC;
13889 if (ts.kind != gfc_default_logical_kind)
13890 return SEQ_NONDEFAULT;
13892 return SEQ_NUMERIC;
13895 return SEQ_NONDEFAULT;
13900 /* Resolve derived type EQUIVALENCE object. */
13903 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13905 gfc_component *c = derived->components;
13910 /* Shall not be an object of nonsequence derived type. */
13911 if (!derived->attr.sequence)
13913 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13914 "attribute to be an EQUIVALENCE object", sym->name,
13919 /* Shall not have allocatable components. */
13920 if (derived->attr.alloc_comp)
13922 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13923 "components to be an EQUIVALENCE object",sym->name,
13928 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13930 gfc_error ("Derived type variable '%s' at %L with default "
13931 "initialization cannot be in EQUIVALENCE with a variable "
13932 "in COMMON", sym->name, &e->where);
13936 for (; c ; c = c->next)
13938 if (c->ts.type == BT_DERIVED
13939 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
13942 /* Shall not be an object of sequence derived type containing a pointer
13943 in the structure. */
13944 if (c->attr.pointer)
13946 gfc_error ("Derived type variable '%s' at %L with pointer "
13947 "component(s) cannot be an EQUIVALENCE object",
13948 sym->name, &e->where);
13956 /* Resolve equivalence object.
13957 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13958 an allocatable array, an object of nonsequence derived type, an object of
13959 sequence derived type containing a pointer at any level of component
13960 selection, an automatic object, a function name, an entry name, a result
13961 name, a named constant, a structure component, or a subobject of any of
13962 the preceding objects. A substring shall not have length zero. A
13963 derived type shall not have components with default initialization nor
13964 shall two objects of an equivalence group be initialized.
13965 Either all or none of the objects shall have an protected attribute.
13966 The simple constraints are done in symbol.c(check_conflict) and the rest
13967 are implemented here. */
13970 resolve_equivalence (gfc_equiv *eq)
13973 gfc_symbol *first_sym;
13976 locus *last_where = NULL;
13977 seq_type eq_type, last_eq_type;
13978 gfc_typespec *last_ts;
13979 int object, cnt_protected;
13982 last_ts = &eq->expr->symtree->n.sym->ts;
13984 first_sym = eq->expr->symtree->n.sym;
13988 for (object = 1; eq; eq = eq->eq, object++)
13992 e->ts = e->symtree->n.sym->ts;
13993 /* match_varspec might not know yet if it is seeing
13994 array reference or substring reference, as it doesn't
13996 if (e->ref && e->ref->type == REF_ARRAY)
13998 gfc_ref *ref = e->ref;
13999 sym = e->symtree->n.sym;
14001 if (sym->attr.dimension)
14003 ref->u.ar.as = sym->as;
14007 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14008 if (e->ts.type == BT_CHARACTER
14010 && ref->type == REF_ARRAY
14011 && ref->u.ar.dimen == 1
14012 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14013 && ref->u.ar.stride[0] == NULL)
14015 gfc_expr *start = ref->u.ar.start[0];
14016 gfc_expr *end = ref->u.ar.end[0];
14019 /* Optimize away the (:) reference. */
14020 if (start == NULL && end == NULL)
14023 e->ref = ref->next;
14025 e->ref->next = ref->next;
14030 ref->type = REF_SUBSTRING;
14032 start = gfc_get_int_expr (gfc_default_integer_kind,
14034 ref->u.ss.start = start;
14035 if (end == NULL && e->ts.u.cl)
14036 end = gfc_copy_expr (e->ts.u.cl->length);
14037 ref->u.ss.end = end;
14038 ref->u.ss.length = e->ts.u.cl;
14045 /* Any further ref is an error. */
14048 gcc_assert (ref->type == REF_ARRAY);
14049 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14055 if (!gfc_resolve_expr (e))
14058 sym = e->symtree->n.sym;
14060 if (sym->attr.is_protected)
14062 if (cnt_protected > 0 && cnt_protected != object)
14064 gfc_error ("Either all or none of the objects in the "
14065 "EQUIVALENCE set at %L shall have the "
14066 "PROTECTED attribute",
14071 /* Shall not equivalence common block variables in a PURE procedure. */
14072 if (sym->ns->proc_name
14073 && sym->ns->proc_name->attr.pure
14074 && sym->attr.in_common)
14076 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14077 "object in the pure procedure '%s'",
14078 sym->name, &e->where, sym->ns->proc_name->name);
14082 /* Shall not be a named constant. */
14083 if (e->expr_type == EXPR_CONSTANT)
14085 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14086 "object", sym->name, &e->where);
14090 if (e->ts.type == BT_DERIVED
14091 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14094 /* Check that the types correspond correctly:
14096 A numeric sequence structure may be equivalenced to another sequence
14097 structure, an object of default integer type, default real type, double
14098 precision real type, default logical type such that components of the
14099 structure ultimately only become associated to objects of the same
14100 kind. A character sequence structure may be equivalenced to an object
14101 of default character kind or another character sequence structure.
14102 Other objects may be equivalenced only to objects of the same type and
14103 kind parameters. */
14105 /* Identical types are unconditionally OK. */
14106 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14107 goto identical_types;
14109 last_eq_type = sequence_type (*last_ts);
14110 eq_type = sequence_type (sym->ts);
14112 /* Since the pair of objects is not of the same type, mixed or
14113 non-default sequences can be rejected. */
14115 msg = "Sequence %s with mixed components in EQUIVALENCE "
14116 "statement at %L with different type objects";
14118 && last_eq_type == SEQ_MIXED
14119 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14120 || (eq_type == SEQ_MIXED
14121 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14124 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14125 "statement at %L with objects of different type";
14127 && last_eq_type == SEQ_NONDEFAULT
14128 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14129 || (eq_type == SEQ_NONDEFAULT
14130 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14133 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14134 "EQUIVALENCE statement at %L";
14135 if (last_eq_type == SEQ_CHARACTER
14136 && eq_type != SEQ_CHARACTER
14137 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14140 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14141 "EQUIVALENCE statement at %L";
14142 if (last_eq_type == SEQ_NUMERIC
14143 && eq_type != SEQ_NUMERIC
14144 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14149 last_where = &e->where;
14154 /* Shall not be an automatic array. */
14155 if (e->ref->type == REF_ARRAY
14156 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14158 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14159 "an EQUIVALENCE object", sym->name, &e->where);
14166 /* Shall not be a structure component. */
14167 if (r->type == REF_COMPONENT)
14169 gfc_error ("Structure component '%s' at %L cannot be an "
14170 "EQUIVALENCE object",
14171 r->u.c.component->name, &e->where);
14175 /* A substring shall not have length zero. */
14176 if (r->type == REF_SUBSTRING)
14178 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14180 gfc_error ("Substring at %L has length zero",
14181 &r->u.ss.start->where);
14191 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14194 resolve_fntype (gfc_namespace *ns)
14196 gfc_entry_list *el;
14199 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14202 /* If there are any entries, ns->proc_name is the entry master
14203 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14205 sym = ns->entries->sym;
14207 sym = ns->proc_name;
14208 if (sym->result == sym
14209 && sym->ts.type == BT_UNKNOWN
14210 && !gfc_set_default_type (sym, 0, NULL)
14211 && !sym->attr.untyped)
14213 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14214 sym->name, &sym->declared_at);
14215 sym->attr.untyped = 1;
14218 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14219 && !sym->attr.contained
14220 && !gfc_check_symbol_access (sym->ts.u.derived)
14221 && gfc_check_symbol_access (sym))
14223 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14224 "%L of PRIVATE type '%s'", sym->name,
14225 &sym->declared_at, sym->ts.u.derived->name);
14229 for (el = ns->entries->next; el; el = el->next)
14231 if (el->sym->result == el->sym
14232 && el->sym->ts.type == BT_UNKNOWN
14233 && !gfc_set_default_type (el->sym, 0, NULL)
14234 && !el->sym->attr.untyped)
14236 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14237 el->sym->name, &el->sym->declared_at);
14238 el->sym->attr.untyped = 1;
14244 /* 12.3.2.1.1 Defined operators. */
14247 check_uop_procedure (gfc_symbol *sym, locus where)
14249 gfc_formal_arglist *formal;
14251 if (!sym->attr.function)
14253 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14254 sym->name, &where);
14258 if (sym->ts.type == BT_CHARACTER
14259 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14260 && !(sym->result && sym->result->ts.u.cl
14261 && sym->result->ts.u.cl->length))
14263 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14264 "character length", sym->name, &where);
14268 formal = gfc_sym_get_dummy_args (sym);
14269 if (!formal || !formal->sym)
14271 gfc_error ("User operator procedure '%s' at %L must have at least "
14272 "one argument", sym->name, &where);
14276 if (formal->sym->attr.intent != INTENT_IN)
14278 gfc_error ("First argument of operator interface at %L must be "
14279 "INTENT(IN)", &where);
14283 if (formal->sym->attr.optional)
14285 gfc_error ("First argument of operator interface at %L cannot be "
14286 "optional", &where);
14290 formal = formal->next;
14291 if (!formal || !formal->sym)
14294 if (formal->sym->attr.intent != INTENT_IN)
14296 gfc_error ("Second argument of operator interface at %L must be "
14297 "INTENT(IN)", &where);
14301 if (formal->sym->attr.optional)
14303 gfc_error ("Second argument of operator interface at %L cannot be "
14304 "optional", &where);
14310 gfc_error ("Operator interface at %L must have, at most, two "
14311 "arguments", &where);
14319 gfc_resolve_uops (gfc_symtree *symtree)
14321 gfc_interface *itr;
14323 if (symtree == NULL)
14326 gfc_resolve_uops (symtree->left);
14327 gfc_resolve_uops (symtree->right);
14329 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14330 check_uop_procedure (itr->sym, itr->sym->declared_at);
14334 /* Examine all of the expressions associated with a program unit,
14335 assign types to all intermediate expressions, make sure that all
14336 assignments are to compatible types and figure out which names
14337 refer to which functions or subroutines. It doesn't check code
14338 block, which is handled by resolve_code. */
14341 resolve_types (gfc_namespace *ns)
14347 gfc_namespace* old_ns = gfc_current_ns;
14349 /* Check that all IMPLICIT types are ok. */
14350 if (!ns->seen_implicit_none)
14353 for (letter = 0; letter != GFC_LETTERS; ++letter)
14354 if (ns->set_flag[letter]
14355 && !resolve_typespec_used (&ns->default_type[letter],
14356 &ns->implicit_loc[letter], NULL))
14360 gfc_current_ns = ns;
14362 resolve_entries (ns);
14364 resolve_common_vars (ns->blank_common.head, false);
14365 resolve_common_blocks (ns->common_root);
14367 resolve_contained_functions (ns);
14369 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14370 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14371 resolve_formal_arglist (ns->proc_name);
14373 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14375 for (cl = ns->cl_list; cl; cl = cl->next)
14376 resolve_charlen (cl);
14378 gfc_traverse_ns (ns, resolve_symbol);
14380 resolve_fntype (ns);
14382 for (n = ns->contained; n; n = n->sibling)
14384 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14385 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14386 "also be PURE", n->proc_name->name,
14387 &n->proc_name->declared_at);
14393 do_concurrent_flag = 0;
14394 gfc_check_interfaces (ns);
14396 gfc_traverse_ns (ns, resolve_values);
14402 for (d = ns->data; d; d = d->next)
14406 gfc_traverse_ns (ns, gfc_formalize_init_value);
14408 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14410 for (eq = ns->equiv; eq; eq = eq->next)
14411 resolve_equivalence (eq);
14413 /* Warn about unused labels. */
14414 if (warn_unused_label)
14415 warn_unused_fortran_label (ns->st_labels);
14417 gfc_resolve_uops (ns->uop_root);
14419 gfc_current_ns = old_ns;
14423 /* Call resolve_code recursively. */
14426 resolve_codes (gfc_namespace *ns)
14429 bitmap_obstack old_obstack;
14431 if (ns->resolved == 1)
14434 for (n = ns->contained; n; n = n->sibling)
14437 gfc_current_ns = ns;
14439 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14440 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14443 /* Set to an out of range value. */
14444 current_entry_id = -1;
14446 old_obstack = labels_obstack;
14447 bitmap_obstack_initialize (&labels_obstack);
14449 resolve_code (ns->code, ns);
14451 bitmap_obstack_release (&labels_obstack);
14452 labels_obstack = old_obstack;
14456 /* This function is called after a complete program unit has been compiled.
14457 Its purpose is to examine all of the expressions associated with a program
14458 unit, assign types to all intermediate expressions, make sure that all
14459 assignments are to compatible types and figure out which names refer to
14460 which functions or subroutines. */
14463 gfc_resolve (gfc_namespace *ns)
14465 gfc_namespace *old_ns;
14466 code_stack *old_cs_base;
14472 old_ns = gfc_current_ns;
14473 old_cs_base = cs_base;
14475 resolve_types (ns);
14476 component_assignment_level = 0;
14477 resolve_codes (ns);
14479 gfc_current_ns = old_ns;
14480 cs_base = old_cs_base;
14483 gfc_run_passes (ns);