1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 block. */
62 static int forall_flag
;
64 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
66 static int omp_workshare_flag
;
68 /* Nonzero if we are processing a formal arglist. The corresponding function
69 resets the flag each time that it is read. */
70 static int formal_arg_flag
= 0;
72 /* True if we are resolving a specification expression. */
73 static int specification_expr
= 0;
75 /* The id of the last entry seen. */
76 static int current_entry_id
;
78 /* We use bitmaps to determine if a branch target is valid. */
79 static bitmap_obstack labels_obstack
;
81 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
82 static bool inquiry_argument
= false;
85 gfc_is_formal_arg (void)
87 return formal_arg_flag
;
90 /* Is the symbol host associated? */
92 is_sym_host_assoc (gfc_symbol
*sym
, gfc_namespace
*ns
)
94 for (ns
= ns
->parent
; ns
; ns
= ns
->parent
)
103 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
104 an ABSTRACT derived-type. If where is not NULL, an error message with that
105 locus is printed, optionally using name. */
108 resolve_typespec_used (gfc_typespec
* ts
, locus
* where
, const char* name
)
110 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
->attr
.abstract
)
115 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
116 name
, where
, ts
->u
.derived
->name
);
118 gfc_error ("ABSTRACT type '%s' used at %L",
119 ts
->u
.derived
->name
, where
);
129 static void resolve_symbol (gfc_symbol
*sym
);
130 static gfc_try
resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
);
133 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
136 resolve_procedure_interface (gfc_symbol
*sym
)
138 if (sym
->ts
.interface
== sym
)
140 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
141 sym
->name
, &sym
->declared_at
);
144 if (sym
->ts
.interface
->attr
.procedure
)
146 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
147 "in a later PROCEDURE statement", sym
->ts
.interface
->name
,
148 sym
->name
, &sym
->declared_at
);
152 /* Get the attributes from the interface (now resolved). */
153 if (sym
->ts
.interface
->attr
.if_source
|| sym
->ts
.interface
->attr
.intrinsic
)
155 gfc_symbol
*ifc
= sym
->ts
.interface
;
156 resolve_symbol (ifc
);
158 if (ifc
->attr
.intrinsic
)
159 resolve_intrinsic (ifc
, &ifc
->declared_at
);
162 sym
->ts
= ifc
->result
->ts
;
165 sym
->ts
.interface
= ifc
;
166 sym
->attr
.function
= ifc
->attr
.function
;
167 sym
->attr
.subroutine
= ifc
->attr
.subroutine
;
168 gfc_copy_formal_args (sym
, ifc
);
170 sym
->attr
.allocatable
= ifc
->attr
.allocatable
;
171 sym
->attr
.pointer
= ifc
->attr
.pointer
;
172 sym
->attr
.pure
= ifc
->attr
.pure
;
173 sym
->attr
.elemental
= ifc
->attr
.elemental
;
174 sym
->attr
.dimension
= ifc
->attr
.dimension
;
175 sym
->attr
.contiguous
= ifc
->attr
.contiguous
;
176 sym
->attr
.recursive
= ifc
->attr
.recursive
;
177 sym
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
178 sym
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
179 sym
->attr
.is_bind_c
= ifc
->attr
.is_bind_c
;
180 /* Copy array spec. */
181 sym
->as
= gfc_copy_array_spec (ifc
->as
);
185 for (i
= 0; i
< sym
->as
->rank
; i
++)
187 gfc_expr_replace_symbols (sym
->as
->lower
[i
], sym
);
188 gfc_expr_replace_symbols (sym
->as
->upper
[i
], sym
);
191 /* Copy char length. */
192 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
194 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
195 gfc_expr_replace_symbols (sym
->ts
.u
.cl
->length
, sym
);
196 if (sym
->ts
.u
.cl
->length
&& !sym
->ts
.u
.cl
->resolved
197 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
201 else if (sym
->ts
.interface
->name
[0] != '\0')
203 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
204 sym
->ts
.interface
->name
, sym
->name
, &sym
->declared_at
);
212 /* Resolve types of formal argument lists. These have to be done early so that
213 the formal argument lists of module procedures can be copied to the
214 containing module before the individual procedures are resolved
215 individually. We also resolve argument lists of procedures in interface
216 blocks because they are self-contained scoping units.
218 Since a dummy argument cannot be a non-dummy procedure, the only
219 resort left for untyped names are the IMPLICIT types. */
222 resolve_formal_arglist (gfc_symbol
*proc
)
224 gfc_formal_arglist
*f
;
228 if (proc
->result
!= NULL
)
233 if (gfc_elemental (proc
)
234 || sym
->attr
.pointer
|| sym
->attr
.allocatable
235 || (sym
->as
&& sym
->as
->rank
> 0))
237 proc
->attr
.always_explicit
= 1;
238 sym
->attr
.always_explicit
= 1;
243 for (f
= proc
->formal
; f
; f
= f
->next
)
249 /* Alternate return placeholder. */
250 if (gfc_elemental (proc
))
251 gfc_error ("Alternate return specifier in elemental subroutine "
252 "'%s' at %L is not allowed", proc
->name
,
254 if (proc
->attr
.function
)
255 gfc_error ("Alternate return specifier in function "
256 "'%s' at %L is not allowed", proc
->name
,
260 else if (sym
->attr
.procedure
&& sym
->ts
.interface
261 && sym
->attr
.if_source
!= IFSRC_DECL
)
262 resolve_procedure_interface (sym
);
264 if (sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
265 resolve_formal_arglist (sym
);
267 if (sym
->attr
.subroutine
|| sym
->attr
.external
|| sym
->attr
.intrinsic
)
269 if (gfc_pure (proc
) && !gfc_pure (sym
))
271 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
272 "also be PURE", sym
->name
, &sym
->declared_at
);
276 if (gfc_elemental (proc
))
278 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
279 "procedure", &sym
->declared_at
);
283 if (sym
->attr
.function
284 && sym
->ts
.type
== BT_UNKNOWN
285 && sym
->attr
.intrinsic
)
287 gfc_intrinsic_sym
*isym
;
288 isym
= gfc_find_function (sym
->name
);
289 if (isym
== NULL
|| !isym
->specific
)
291 gfc_error ("Unable to find a specific INTRINSIC procedure "
292 "for the reference '%s' at %L", sym
->name
,
301 if (sym
->ts
.type
== BT_UNKNOWN
&& !proc
->attr
.intrinsic
302 && (!sym
->attr
.function
|| sym
->result
== sym
))
303 gfc_set_default_type (sym
, 1, sym
->ns
);
305 gfc_resolve_array_spec (sym
->as
, 0);
307 /* We can't tell if an array with dimension (:) is assumed or deferred
308 shape until we know if it has the pointer or allocatable attributes.
310 if (sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_DEFERRED
311 && !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
313 sym
->as
->type
= AS_ASSUMED_SHAPE
;
314 for (i
= 0; i
< sym
->as
->rank
; i
++)
315 sym
->as
->lower
[i
] = gfc_get_int_expr (gfc_default_integer_kind
,
319 if ((sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_ASSUMED_SHAPE
)
320 || sym
->attr
.pointer
|| sym
->attr
.allocatable
|| sym
->attr
.target
321 || sym
->attr
.optional
)
323 proc
->attr
.always_explicit
= 1;
325 proc
->result
->attr
.always_explicit
= 1;
328 /* If the flavor is unknown at this point, it has to be a variable.
329 A procedure specification would have already set the type. */
331 if (sym
->attr
.flavor
== FL_UNKNOWN
)
332 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, sym
->name
, &sym
->declared_at
);
334 if (gfc_pure (proc
) && !sym
->attr
.pointer
335 && sym
->attr
.flavor
!= FL_PROCEDURE
)
337 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
)
338 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
339 "INTENT(IN)", sym
->name
, proc
->name
,
342 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
)
343 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
344 "have its INTENT specified", sym
->name
, proc
->name
,
348 if (gfc_elemental (proc
))
351 if (sym
->attr
.codimension
)
353 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
354 "procedure", sym
->name
, &sym
->declared_at
);
360 gfc_error ("Argument '%s' of elemental procedure at %L must "
361 "be scalar", sym
->name
, &sym
->declared_at
);
365 if (sym
->attr
.allocatable
)
367 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
368 "have the ALLOCATABLE attribute", sym
->name
,
373 if (sym
->attr
.pointer
)
375 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
376 "have the POINTER attribute", sym
->name
,
381 if (sym
->attr
.flavor
== FL_PROCEDURE
)
383 gfc_error ("Dummy procedure '%s' not allowed in elemental "
384 "procedure '%s' at %L", sym
->name
, proc
->name
,
389 if (sym
->attr
.intent
== INTENT_UNKNOWN
)
391 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
392 "have its INTENT specified", sym
->name
, proc
->name
,
398 /* Each dummy shall be specified to be scalar. */
399 if (proc
->attr
.proc
== PROC_ST_FUNCTION
)
403 gfc_error ("Argument '%s' of statement function at %L must "
404 "be scalar", sym
->name
, &sym
->declared_at
);
408 if (sym
->ts
.type
== BT_CHARACTER
)
410 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
411 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
413 gfc_error ("Character-valued argument '%s' of statement "
414 "function at %L must have constant length",
415 sym
->name
, &sym
->declared_at
);
425 /* Work function called when searching for symbols that have argument lists
426 associated with them. */
429 find_arglists (gfc_symbol
*sym
)
431 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
|| sym
->ns
!= gfc_current_ns
)
434 resolve_formal_arglist (sym
);
438 /* Given a namespace, resolve all formal argument lists within the namespace.
442 resolve_formal_arglists (gfc_namespace
*ns
)
447 gfc_traverse_ns (ns
, find_arglists
);
452 resolve_contained_fntype (gfc_symbol
*sym
, gfc_namespace
*ns
)
456 /* If this namespace is not a function or an entry master function,
458 if (! sym
|| !(sym
->attr
.function
|| sym
->attr
.flavor
== FL_VARIABLE
)
459 || sym
->attr
.entry_master
)
462 /* Try to find out of what the return type is. */
463 if (sym
->result
->ts
.type
== BT_UNKNOWN
&& sym
->result
->ts
.interface
== NULL
)
465 t
= gfc_set_default_type (sym
->result
, 0, ns
);
467 if (t
== FAILURE
&& !sym
->result
->attr
.untyped
)
469 if (sym
->result
== sym
)
470 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
471 sym
->name
, &sym
->declared_at
);
472 else if (!sym
->result
->attr
.proc_pointer
)
473 gfc_error ("Result '%s' of contained function '%s' at %L has "
474 "no IMPLICIT type", sym
->result
->name
, sym
->name
,
475 &sym
->result
->declared_at
);
476 sym
->result
->attr
.untyped
= 1;
480 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
481 type, lists the only ways a character length value of * can be used:
482 dummy arguments of procedures, named constants, and function results
483 in external functions. Internal function results and results of module
484 procedures are not on this list, ergo, not permitted. */
486 if (sym
->result
->ts
.type
== BT_CHARACTER
)
488 gfc_charlen
*cl
= sym
->result
->ts
.u
.cl
;
489 if (!cl
|| !cl
->length
)
491 /* See if this is a module-procedure and adapt error message
494 gcc_assert (ns
->parent
&& ns
->parent
->proc_name
);
495 module_proc
= (ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
);
497 gfc_error ("Character-valued %s '%s' at %L must not be"
499 module_proc
? _("module procedure")
500 : _("internal function"),
501 sym
->name
, &sym
->declared_at
);
507 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
508 introduce duplicates. */
511 merge_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
513 gfc_formal_arglist
*f
, *new_arglist
;
516 for (; new_args
!= NULL
; new_args
= new_args
->next
)
518 new_sym
= new_args
->sym
;
519 /* See if this arg is already in the formal argument list. */
520 for (f
= proc
->formal
; f
; f
= f
->next
)
522 if (new_sym
== f
->sym
)
529 /* Add a new argument. Argument order is not important. */
530 new_arglist
= gfc_get_formal_arglist ();
531 new_arglist
->sym
= new_sym
;
532 new_arglist
->next
= proc
->formal
;
533 proc
->formal
= new_arglist
;
538 /* Flag the arguments that are not present in all entries. */
541 check_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
543 gfc_formal_arglist
*f
, *head
;
546 for (f
= proc
->formal
; f
; f
= f
->next
)
551 for (new_args
= head
; new_args
; new_args
= new_args
->next
)
553 if (new_args
->sym
== f
->sym
)
560 f
->sym
->attr
.not_always_present
= 1;
565 /* Resolve alternate entry points. If a symbol has multiple entry points we
566 create a new master symbol for the main routine, and turn the existing
567 symbol into an entry point. */
570 resolve_entries (gfc_namespace
*ns
)
572 gfc_namespace
*old_ns
;
576 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
577 static int master_count
= 0;
579 if (ns
->proc_name
== NULL
)
582 /* No need to do anything if this procedure doesn't have alternate entry
587 /* We may already have resolved alternate entry points. */
588 if (ns
->proc_name
->attr
.entry_master
)
591 /* If this isn't a procedure something has gone horribly wrong. */
592 gcc_assert (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
);
594 /* Remember the current namespace. */
595 old_ns
= gfc_current_ns
;
599 /* Add the main entry point to the list of entry points. */
600 el
= gfc_get_entry_list ();
601 el
->sym
= ns
->proc_name
;
603 el
->next
= ns
->entries
;
605 ns
->proc_name
->attr
.entry
= 1;
607 /* If it is a module function, it needs to be in the right namespace
608 so that gfc_get_fake_result_decl can gather up the results. The
609 need for this arose in get_proc_name, where these beasts were
610 left in their own namespace, to keep prior references linked to
611 the entry declaration.*/
612 if (ns
->proc_name
->attr
.function
613 && ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
616 /* Do the same for entries where the master is not a module
617 procedure. These are retained in the module namespace because
618 of the module procedure declaration. */
619 for (el
= el
->next
; el
; el
= el
->next
)
620 if (el
->sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
621 && el
->sym
->attr
.mod_proc
)
625 /* Add an entry statement for it. */
632 /* Create a new symbol for the master function. */
633 /* Give the internal function a unique name (within this file).
634 Also include the function name so the user has some hope of figuring
635 out what is going on. */
636 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "master.%d.%s",
637 master_count
++, ns
->proc_name
->name
);
638 gfc_get_ha_symbol (name
, &proc
);
639 gcc_assert (proc
!= NULL
);
641 gfc_add_procedure (&proc
->attr
, PROC_INTERNAL
, proc
->name
, NULL
);
642 if (ns
->proc_name
->attr
.subroutine
)
643 gfc_add_subroutine (&proc
->attr
, proc
->name
, NULL
);
647 gfc_typespec
*ts
, *fts
;
648 gfc_array_spec
*as
, *fas
;
649 gfc_add_function (&proc
->attr
, proc
->name
, NULL
);
651 fas
= ns
->entries
->sym
->as
;
652 fas
= fas
? fas
: ns
->entries
->sym
->result
->as
;
653 fts
= &ns
->entries
->sym
->result
->ts
;
654 if (fts
->type
== BT_UNKNOWN
)
655 fts
= gfc_get_default_type (ns
->entries
->sym
->result
->name
, NULL
);
656 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
658 ts
= &el
->sym
->result
->ts
;
660 as
= as
? as
: el
->sym
->result
->as
;
661 if (ts
->type
== BT_UNKNOWN
)
662 ts
= gfc_get_default_type (el
->sym
->result
->name
, NULL
);
664 if (! gfc_compare_types (ts
, fts
)
665 || (el
->sym
->result
->attr
.dimension
666 != ns
->entries
->sym
->result
->attr
.dimension
)
667 || (el
->sym
->result
->attr
.pointer
668 != ns
->entries
->sym
->result
->attr
.pointer
))
670 else if (as
&& fas
&& ns
->entries
->sym
->result
!= el
->sym
->result
671 && gfc_compare_array_spec (as
, fas
) == 0)
672 gfc_error ("Function %s at %L has entries with mismatched "
673 "array specifications", ns
->entries
->sym
->name
,
674 &ns
->entries
->sym
->declared_at
);
675 /* The characteristics need to match and thus both need to have
676 the same string length, i.e. both len=*, or both len=4.
677 Having both len=<variable> is also possible, but difficult to
678 check at compile time. */
679 else if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& fts
->u
.cl
680 && (((ts
->u
.cl
->length
&& !fts
->u
.cl
->length
)
681 ||(!ts
->u
.cl
->length
&& fts
->u
.cl
->length
))
683 && ts
->u
.cl
->length
->expr_type
684 != fts
->u
.cl
->length
->expr_type
)
686 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
687 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
688 fts
->u
.cl
->length
->value
.integer
) != 0)))
689 gfc_notify_std (GFC_STD_GNU
, "Extension: Function %s at %L with "
690 "entries returning variables of different "
691 "string lengths", ns
->entries
->sym
->name
,
692 &ns
->entries
->sym
->declared_at
);
697 sym
= ns
->entries
->sym
->result
;
698 /* All result types the same. */
700 if (sym
->attr
.dimension
)
701 gfc_set_array_spec (proc
, gfc_copy_array_spec (sym
->as
), NULL
);
702 if (sym
->attr
.pointer
)
703 gfc_add_pointer (&proc
->attr
, NULL
);
707 /* Otherwise the result will be passed through a union by
709 proc
->attr
.mixed_entry_master
= 1;
710 for (el
= ns
->entries
; el
; el
= el
->next
)
712 sym
= el
->sym
->result
;
713 if (sym
->attr
.dimension
)
715 if (el
== ns
->entries
)
716 gfc_error ("FUNCTION result %s can't be an array in "
717 "FUNCTION %s at %L", sym
->name
,
718 ns
->entries
->sym
->name
, &sym
->declared_at
);
720 gfc_error ("ENTRY result %s can't be an array in "
721 "FUNCTION %s at %L", sym
->name
,
722 ns
->entries
->sym
->name
, &sym
->declared_at
);
724 else if (sym
->attr
.pointer
)
726 if (el
== ns
->entries
)
727 gfc_error ("FUNCTION result %s can't be a POINTER in "
728 "FUNCTION %s at %L", sym
->name
,
729 ns
->entries
->sym
->name
, &sym
->declared_at
);
731 gfc_error ("ENTRY result %s can't be a POINTER in "
732 "FUNCTION %s at %L", sym
->name
,
733 ns
->entries
->sym
->name
, &sym
->declared_at
);
738 if (ts
->type
== BT_UNKNOWN
)
739 ts
= gfc_get_default_type (sym
->name
, NULL
);
743 if (ts
->kind
== gfc_default_integer_kind
)
747 if (ts
->kind
== gfc_default_real_kind
748 || ts
->kind
== gfc_default_double_kind
)
752 if (ts
->kind
== gfc_default_complex_kind
)
756 if (ts
->kind
== gfc_default_logical_kind
)
760 /* We will issue error elsewhere. */
768 if (el
== ns
->entries
)
769 gfc_error ("FUNCTION result %s can't be of type %s "
770 "in FUNCTION %s at %L", sym
->name
,
771 gfc_typename (ts
), ns
->entries
->sym
->name
,
774 gfc_error ("ENTRY result %s can't be of type %s "
775 "in FUNCTION %s at %L", sym
->name
,
776 gfc_typename (ts
), ns
->entries
->sym
->name
,
783 proc
->attr
.access
= ACCESS_PRIVATE
;
784 proc
->attr
.entry_master
= 1;
786 /* Merge all the entry point arguments. */
787 for (el
= ns
->entries
; el
; el
= el
->next
)
788 merge_argument_lists (proc
, el
->sym
->formal
);
790 /* Check the master formal arguments for any that are not
791 present in all entry points. */
792 for (el
= ns
->entries
; el
; el
= el
->next
)
793 check_argument_lists (proc
, el
->sym
->formal
);
795 /* Use the master function for the function body. */
796 ns
->proc_name
= proc
;
798 /* Finalize the new symbols. */
799 gfc_commit_symbols ();
801 /* Restore the original namespace. */
802 gfc_current_ns
= old_ns
;
806 /* Resolve common variables. */
808 resolve_common_vars (gfc_symbol
*sym
, bool named_common
)
810 gfc_symbol
*csym
= sym
;
812 for (; csym
; csym
= csym
->common_next
)
814 if (csym
->value
|| csym
->attr
.data
)
816 if (!csym
->ns
->is_block_data
)
817 gfc_notify_std (GFC_STD_GNU
, "Variable '%s' at %L is in COMMON "
818 "but only in BLOCK DATA initialization is "
819 "allowed", csym
->name
, &csym
->declared_at
);
820 else if (!named_common
)
821 gfc_notify_std (GFC_STD_GNU
, "Initialized variable '%s' at %L is "
822 "in a blank COMMON but initialization is only "
823 "allowed in named common blocks", csym
->name
,
827 if (csym
->ts
.type
!= BT_DERIVED
)
830 if (!(csym
->ts
.u
.derived
->attr
.sequence
831 || csym
->ts
.u
.derived
->attr
.is_bind_c
))
832 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
833 "has neither the SEQUENCE nor the BIND(C) "
834 "attribute", csym
->name
, &csym
->declared_at
);
835 if (csym
->ts
.u
.derived
->attr
.alloc_comp
)
836 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
837 "has an ultimate component that is "
838 "allocatable", csym
->name
, &csym
->declared_at
);
839 if (gfc_has_default_initializer (csym
->ts
.u
.derived
))
840 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
841 "may not have default initializer", csym
->name
,
844 if (csym
->attr
.flavor
== FL_UNKNOWN
&& !csym
->attr
.proc_pointer
)
845 gfc_add_flavor (&csym
->attr
, FL_VARIABLE
, csym
->name
, &csym
->declared_at
);
849 /* Resolve common blocks. */
851 resolve_common_blocks (gfc_symtree
*common_root
)
855 if (common_root
== NULL
)
858 if (common_root
->left
)
859 resolve_common_blocks (common_root
->left
);
860 if (common_root
->right
)
861 resolve_common_blocks (common_root
->right
);
863 resolve_common_vars (common_root
->n
.common
->head
, true);
865 gfc_find_symbol (common_root
->name
, gfc_current_ns
, 0, &sym
);
869 if (sym
->attr
.flavor
== FL_PARAMETER
)
870 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
871 sym
->name
, &common_root
->n
.common
->where
, &sym
->declared_at
);
873 if (sym
->attr
.intrinsic
)
874 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
875 sym
->name
, &common_root
->n
.common
->where
);
876 else if (sym
->attr
.result
877 || gfc_is_function_return_value (sym
, gfc_current_ns
))
878 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
879 "that is also a function result", sym
->name
,
880 &common_root
->n
.common
->where
);
881 else if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_INTERNAL
882 && sym
->attr
.proc
!= PROC_ST_FUNCTION
)
883 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
884 "that is also a global procedure", sym
->name
,
885 &common_root
->n
.common
->where
);
889 /* Resolve contained function types. Because contained functions can call one
890 another, they have to be worked out before any of the contained procedures
893 The good news is that if a function doesn't already have a type, the only
894 way it can get one is through an IMPLICIT type or a RESULT variable, because
895 by definition contained functions are contained namespace they're contained
896 in, not in a sibling or parent namespace. */
899 resolve_contained_functions (gfc_namespace
*ns
)
901 gfc_namespace
*child
;
904 resolve_formal_arglists (ns
);
906 for (child
= ns
->contained
; child
; child
= child
->sibling
)
908 /* Resolve alternate entry points first. */
909 resolve_entries (child
);
911 /* Then check function return types. */
912 resolve_contained_fntype (child
->proc_name
, child
);
913 for (el
= child
->entries
; el
; el
= el
->next
)
914 resolve_contained_fntype (el
->sym
, child
);
919 /* Resolve all of the elements of a structure constructor and make sure that
920 the types are correct. The 'init' flag indicates that the given
921 constructor is an initializer. */
924 resolve_structure_cons (gfc_expr
*expr
, int init
)
926 gfc_constructor
*cons
;
933 if (expr
->ts
.type
== BT_DERIVED
)
934 resolve_symbol (expr
->ts
.u
.derived
);
936 cons
= gfc_constructor_first (expr
->value
.constructor
);
937 /* A constructor may have references if it is the result of substituting a
938 parameter variable. In this case we just pull out the component we
941 comp
= expr
->ref
->u
.c
.sym
->components
;
943 comp
= expr
->ts
.u
.derived
->components
;
945 /* See if the user is trying to invoke a structure constructor for one of
946 the iso_c_binding derived types. */
947 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
948 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
949 && (cons
->expr
== NULL
|| cons
->expr
->expr_type
!= EXPR_NULL
))
951 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
952 expr
->ts
.u
.derived
->name
, &(expr
->where
));
956 /* Return if structure constructor is c_null_(fun)prt. */
957 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
958 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
959 && cons
->expr
&& cons
->expr
->expr_type
== EXPR_NULL
)
962 for (; comp
&& cons
; comp
= comp
->next
, cons
= gfc_constructor_next (cons
))
969 if (gfc_resolve_expr (cons
->expr
) == FAILURE
)
975 rank
= comp
->as
? comp
->as
->rank
: 0;
976 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
977 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
979 gfc_error ("The rank of the element in the derived type "
980 "constructor at %L does not match that of the "
981 "component (%d/%d)", &cons
->expr
->where
,
982 cons
->expr
->rank
, rank
);
986 /* If we don't have the right type, try to convert it. */
988 if (!comp
->attr
.proc_pointer
&&
989 !gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
992 if (strcmp (comp
->name
, "_extends") == 0)
994 /* Can afford to be brutal with the _extends initializer.
995 The derived type can get lost because it is PRIVATE
996 but it is not usage constrained by the standard. */
997 cons
->expr
->ts
= comp
->ts
;
1000 else if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
1001 gfc_error ("The element in the derived type constructor at %L, "
1002 "for pointer component '%s', is %s but should be %s",
1003 &cons
->expr
->where
, comp
->name
,
1004 gfc_basic_typename (cons
->expr
->ts
.type
),
1005 gfc_basic_typename (comp
->ts
.type
));
1007 t
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
1010 /* For strings, the length of the constructor should be the same as
1011 the one of the structure, ensure this if the lengths are known at
1012 compile time and when we are dealing with PARAMETER or structure
1014 if (cons
->expr
->ts
.type
== BT_CHARACTER
&& comp
->ts
.u
.cl
1015 && comp
->ts
.u
.cl
->length
1016 && comp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1017 && cons
->expr
->ts
.u
.cl
&& cons
->expr
->ts
.u
.cl
->length
1018 && cons
->expr
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1019 && mpz_cmp (cons
->expr
->ts
.u
.cl
->length
->value
.integer
,
1020 comp
->ts
.u
.cl
->length
->value
.integer
) != 0)
1022 if (cons
->expr
->expr_type
== EXPR_VARIABLE
1023 && cons
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
1025 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1026 to make use of the gfc_resolve_character_array_constructor
1027 machinery. The expression is later simplified away to
1028 an array of string literals. */
1029 gfc_expr
*para
= cons
->expr
;
1030 cons
->expr
= gfc_get_expr ();
1031 cons
->expr
->ts
= para
->ts
;
1032 cons
->expr
->where
= para
->where
;
1033 cons
->expr
->expr_type
= EXPR_ARRAY
;
1034 cons
->expr
->rank
= para
->rank
;
1035 cons
->expr
->shape
= gfc_copy_shape (para
->shape
, para
->rank
);
1036 gfc_constructor_append_expr (&cons
->expr
->value
.constructor
,
1037 para
, &cons
->expr
->where
);
1039 if (cons
->expr
->expr_type
== EXPR_ARRAY
)
1042 p
= gfc_constructor_first (cons
->expr
->value
.constructor
);
1043 if (cons
->expr
->ts
.u
.cl
!= p
->expr
->ts
.u
.cl
)
1045 gfc_charlen
*cl
, *cl2
;
1048 for (cl
= gfc_current_ns
->cl_list
; cl
; cl
= cl
->next
)
1050 if (cl
== cons
->expr
->ts
.u
.cl
)
1058 cl2
->next
= cl
->next
;
1060 gfc_free_expr (cl
->length
);
1064 cons
->expr
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1065 cons
->expr
->ts
.u
.cl
->length_from_typespec
= true;
1066 cons
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (comp
->ts
.u
.cl
->length
);
1067 gfc_resolve_character_array_constructor (cons
->expr
);
1071 if (cons
->expr
->expr_type
== EXPR_NULL
1072 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
1073 || comp
->attr
.proc_pointer
1074 || (comp
->ts
.type
== BT_CLASS
1075 && (CLASS_DATA (comp
)->attr
.class_pointer
1076 || CLASS_DATA (comp
)->attr
.allocatable
))))
1079 gfc_error ("The NULL in the derived type constructor at %L is "
1080 "being applied to component '%s', which is neither "
1081 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
1085 if (!comp
->attr
.pointer
|| comp
->attr
.proc_pointer
1086 || cons
->expr
->expr_type
== EXPR_NULL
)
1089 a
= gfc_expr_attr (cons
->expr
);
1091 if (!a
.pointer
&& !a
.target
)
1094 gfc_error ("The element in the derived type constructor at %L, "
1095 "for pointer component '%s' should be a POINTER or "
1096 "a TARGET", &cons
->expr
->where
, comp
->name
);
1101 /* F08:C461. Additional checks for pointer initialization. */
1105 gfc_error ("Pointer initialization target at %L "
1106 "must not be ALLOCATABLE ", &cons
->expr
->where
);
1111 gfc_error ("Pointer initialization target at %L "
1112 "must have the SAVE attribute", &cons
->expr
->where
);
1116 /* F2003, C1272 (3). */
1117 if (gfc_pure (NULL
) && cons
->expr
->expr_type
== EXPR_VARIABLE
1118 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1119 || gfc_is_coindexed (cons
->expr
)))
1122 gfc_error ("Invalid expression in the derived type constructor for "
1123 "pointer component '%s' at %L in PURE procedure",
1124 comp
->name
, &cons
->expr
->where
);
1133 /****************** Expression name resolution ******************/
1135 /* Returns 0 if a symbol was not declared with a type or
1136 attribute declaration statement, nonzero otherwise. */
1139 was_declared (gfc_symbol
*sym
)
1145 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
1148 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
1149 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
1150 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
1151 || a
.asynchronous
|| a
.codimension
)
1158 /* Determine if a symbol is generic or not. */
1161 generic_sym (gfc_symbol
*sym
)
1165 if (sym
->attr
.generic
||
1166 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
1169 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1172 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1179 return generic_sym (s
);
1186 /* Determine if a symbol is specific or not. */
1189 specific_sym (gfc_symbol
*sym
)
1193 if (sym
->attr
.if_source
== IFSRC_IFBODY
1194 || sym
->attr
.proc
== PROC_MODULE
1195 || sym
->attr
.proc
== PROC_INTERNAL
1196 || sym
->attr
.proc
== PROC_ST_FUNCTION
1197 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
1198 || sym
->attr
.external
)
1201 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1204 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1206 return (s
== NULL
) ? 0 : specific_sym (s
);
1210 /* Figure out if the procedure is specific, generic or unknown. */
1213 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
}
1217 procedure_kind (gfc_symbol
*sym
)
1219 if (generic_sym (sym
))
1220 return PTYPE_GENERIC
;
1222 if (specific_sym (sym
))
1223 return PTYPE_SPECIFIC
;
1225 return PTYPE_UNKNOWN
;
1228 /* Check references to assumed size arrays. The flag need_full_assumed_size
1229 is nonzero when matching actual arguments. */
1231 static int need_full_assumed_size
= 0;
1234 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1236 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1239 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1240 What should it be? */
1241 if ((e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1242 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1243 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1245 gfc_error ("The upper bound in the last dimension must "
1246 "appear in the reference to the assumed size "
1247 "array '%s' at %L", sym
->name
, &e
->where
);
1254 /* Look for bad assumed size array references in argument expressions
1255 of elemental and array valued intrinsic procedures. Since this is
1256 called from procedure resolution functions, it only recurses at
1260 resolve_assumed_size_actual (gfc_expr
*e
)
1265 switch (e
->expr_type
)
1268 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1273 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1274 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1285 /* Check a generic procedure, passed as an actual argument, to see if
1286 there is a matching specific name. If none, it is an error, and if
1287 more than one, the reference is ambiguous. */
1289 count_specific_procs (gfc_expr
*e
)
1296 sym
= e
->symtree
->n
.sym
;
1298 for (p
= sym
->generic
; p
; p
= p
->next
)
1299 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1301 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1307 gfc_error ("'%s' at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1311 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1312 "argument at %L", sym
->name
, &e
->where
);
1318 /* See if a call to sym could possibly be a not allowed RECURSION because of
1319 a missing RECURIVE declaration. This means that either sym is the current
1320 context itself, or sym is the parent of a contained procedure calling its
1321 non-RECURSIVE containing procedure.
1322 This also works if sym is an ENTRY. */
1325 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1327 gfc_symbol
* proc_sym
;
1328 gfc_symbol
* context_proc
;
1329 gfc_namespace
* real_context
;
1331 if (sym
->attr
.flavor
== FL_PROGRAM
)
1334 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
);
1336 /* If we've got an ENTRY, find real procedure. */
1337 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1338 proc_sym
= sym
->ns
->entries
->sym
;
1342 /* If sym is RECURSIVE, all is well of course. */
1343 if (proc_sym
->attr
.recursive
|| gfc_option
.flag_recursive
)
1346 /* Find the context procedure's "real" symbol if it has entries.
1347 We look for a procedure symbol, so recurse on the parents if we don't
1348 find one (like in case of a BLOCK construct). */
1349 for (real_context
= context
; ; real_context
= real_context
->parent
)
1351 /* We should find something, eventually! */
1352 gcc_assert (real_context
);
1354 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1355 : real_context
->proc_name
);
1357 /* In some special cases, there may not be a proc_name, like for this
1359 real(bad_kind()) function foo () ...
1360 when checking the call to bad_kind ().
1361 In these cases, we simply return here and assume that the
1366 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1370 /* A call from sym's body to itself is recursion, of course. */
1371 if (context_proc
== proc_sym
)
1374 /* The same is true if context is a contained procedure and sym the
1376 if (context_proc
->attr
.contained
)
1378 gfc_symbol
* parent_proc
;
1380 gcc_assert (context
->parent
);
1381 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1382 : context
->parent
->proc_name
);
1384 if (parent_proc
== proc_sym
)
1392 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1393 its typespec and formal argument list. */
1396 resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1398 gfc_intrinsic_sym
* isym
= NULL
;
1404 /* We already know this one is an intrinsic, so we don't call
1405 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1406 gfc_find_subroutine directly to check whether it is a function or
1409 if (sym
->intmod_sym_id
)
1410 isym
= gfc_intrinsic_function_by_id ((gfc_isym_id
) sym
->intmod_sym_id
);
1412 isym
= gfc_find_function (sym
->name
);
1416 if (sym
->ts
.type
!= BT_UNKNOWN
&& gfc_option
.warn_surprising
1417 && !sym
->attr
.implicit_type
)
1418 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1419 " ignored", sym
->name
, &sym
->declared_at
);
1421 if (!sym
->attr
.function
&&
1422 gfc_add_function (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1427 else if ((isym
= gfc_find_subroutine (sym
->name
)))
1429 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1431 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1432 " specifier", sym
->name
, &sym
->declared_at
);
1436 if (!sym
->attr
.subroutine
&&
1437 gfc_add_subroutine (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1442 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym
->name
,
1447 gfc_copy_formal_args_intr (sym
, isym
);
1449 /* Check it is actually available in the standard settings. */
1450 if (gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
)
1453 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1454 " available in the current standard settings but %s. Use"
1455 " an appropriate -std=* option or enable -fall-intrinsics"
1456 " in order to use it.",
1457 sym
->name
, &sym
->declared_at
, symstd
);
1465 /* Resolve a procedure expression, like passing it to a called procedure or as
1466 RHS for a procedure pointer assignment. */
1469 resolve_procedure_expression (gfc_expr
* expr
)
1473 if (expr
->expr_type
!= EXPR_VARIABLE
)
1475 gcc_assert (expr
->symtree
);
1477 sym
= expr
->symtree
->n
.sym
;
1479 if (sym
->attr
.intrinsic
)
1480 resolve_intrinsic (sym
, &expr
->where
);
1482 if (sym
->attr
.flavor
!= FL_PROCEDURE
1483 || (sym
->attr
.function
&& sym
->result
== sym
))
1486 /* A non-RECURSIVE procedure that is used as procedure expression within its
1487 own body is in danger of being called recursively. */
1488 if (is_illegal_recursion (sym
, gfc_current_ns
))
1489 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1490 " itself recursively. Declare it RECURSIVE or use"
1491 " -frecursive", sym
->name
, &expr
->where
);
1497 /* Resolve an actual argument list. Most of the time, this is just
1498 resolving the expressions in the list.
1499 The exception is that we sometimes have to decide whether arguments
1500 that look like procedure arguments are really simple variable
1504 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1505 bool no_formal_args
)
1508 gfc_symtree
*parent_st
;
1510 int save_need_full_assumed_size
;
1511 gfc_component
*comp
;
1513 for (; arg
; arg
= arg
->next
)
1518 /* Check the label is a valid branching target. */
1521 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1523 gfc_error ("Label %d referenced at %L is never defined",
1524 arg
->label
->value
, &arg
->label
->where
);
1531 if (gfc_is_proc_ptr_comp (e
, &comp
))
1534 if (e
->expr_type
== EXPR_PPC
)
1536 if (comp
->as
!= NULL
)
1537 e
->rank
= comp
->as
->rank
;
1538 e
->expr_type
= EXPR_FUNCTION
;
1540 if (gfc_resolve_expr (e
) == FAILURE
)
1545 if (e
->expr_type
== EXPR_VARIABLE
1546 && e
->symtree
->n
.sym
->attr
.generic
1548 && count_specific_procs (e
) != 1)
1551 if (e
->ts
.type
!= BT_PROCEDURE
)
1553 save_need_full_assumed_size
= need_full_assumed_size
;
1554 if (e
->expr_type
!= EXPR_VARIABLE
)
1555 need_full_assumed_size
= 0;
1556 if (gfc_resolve_expr (e
) != SUCCESS
)
1558 need_full_assumed_size
= save_need_full_assumed_size
;
1562 /* See if the expression node should really be a variable reference. */
1564 sym
= e
->symtree
->n
.sym
;
1566 if (sym
->attr
.flavor
== FL_PROCEDURE
1567 || sym
->attr
.intrinsic
1568 || sym
->attr
.external
)
1572 /* If a procedure is not already determined to be something else
1573 check if it is intrinsic. */
1574 if (!sym
->attr
.intrinsic
1575 && !(sym
->attr
.external
|| sym
->attr
.use_assoc
1576 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1577 && gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1578 sym
->attr
.intrinsic
= 1;
1580 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1582 gfc_error ("Statement function '%s' at %L is not allowed as an "
1583 "actual argument", sym
->name
, &e
->where
);
1586 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1587 sym
->attr
.subroutine
);
1588 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1590 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1591 "actual argument", sym
->name
, &e
->where
);
1594 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1595 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1597 if (gfc_notify_std (GFC_STD_F2008
,
1598 "Fortran 2008: Internal procedure '%s' is"
1599 " used as actual argument at %L",
1600 sym
->name
, &e
->where
) == FAILURE
)
1604 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1606 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1607 "allowed as an actual argument at %L", sym
->name
,
1611 /* Check if a generic interface has a specific procedure
1612 with the same name before emitting an error. */
1613 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
1616 /* Just in case a specific was found for the expression. */
1617 sym
= e
->symtree
->n
.sym
;
1619 /* If the symbol is the function that names the current (or
1620 parent) scope, then we really have a variable reference. */
1622 if (gfc_is_function_return_value (sym
, sym
->ns
))
1625 /* If all else fails, see if we have a specific intrinsic. */
1626 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
1628 gfc_intrinsic_sym
*isym
;
1630 isym
= gfc_find_function (sym
->name
);
1631 if (isym
== NULL
|| !isym
->specific
)
1633 gfc_error ("Unable to find a specific INTRINSIC procedure "
1634 "for the reference '%s' at %L", sym
->name
,
1639 sym
->attr
.intrinsic
= 1;
1640 sym
->attr
.function
= 1;
1643 if (gfc_resolve_expr (e
) == FAILURE
)
1648 /* See if the name is a module procedure in a parent unit. */
1650 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1653 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
1655 gfc_error ("Symbol '%s' at %L is ambiguous", sym
->name
, &e
->where
);
1659 if (parent_st
== NULL
)
1662 sym
= parent_st
->n
.sym
;
1663 e
->symtree
= parent_st
; /* Point to the right thing. */
1665 if (sym
->attr
.flavor
== FL_PROCEDURE
1666 || sym
->attr
.intrinsic
1667 || sym
->attr
.external
)
1669 if (gfc_resolve_expr (e
) == FAILURE
)
1675 e
->expr_type
= EXPR_VARIABLE
;
1677 if (sym
->as
!= NULL
)
1679 e
->rank
= sym
->as
->rank
;
1680 e
->ref
= gfc_get_ref ();
1681 e
->ref
->type
= REF_ARRAY
;
1682 e
->ref
->u
.ar
.type
= AR_FULL
;
1683 e
->ref
->u
.ar
.as
= sym
->as
;
1686 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1687 primary.c (match_actual_arg). If above code determines that it
1688 is a variable instead, it needs to be resolved as it was not
1689 done at the beginning of this function. */
1690 save_need_full_assumed_size
= need_full_assumed_size
;
1691 if (e
->expr_type
!= EXPR_VARIABLE
)
1692 need_full_assumed_size
= 0;
1693 if (gfc_resolve_expr (e
) != SUCCESS
)
1695 need_full_assumed_size
= save_need_full_assumed_size
;
1698 /* Check argument list functions %VAL, %LOC and %REF. There is
1699 nothing to do for %REF. */
1700 if (arg
->name
&& arg
->name
[0] == '%')
1702 if (strncmp ("%VAL", arg
->name
, 4) == 0)
1704 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
1706 gfc_error ("By-value argument at %L is not of numeric "
1713 gfc_error ("By-value argument at %L cannot be an array or "
1714 "an array section", &e
->where
);
1718 /* Intrinsics are still PROC_UNKNOWN here. However,
1719 since same file external procedures are not resolvable
1720 in gfortran, it is a good deal easier to leave them to
1722 if (ptype
!= PROC_UNKNOWN
1723 && ptype
!= PROC_DUMMY
1724 && ptype
!= PROC_EXTERNAL
1725 && ptype
!= PROC_MODULE
)
1727 gfc_error ("By-value argument at %L is not allowed "
1728 "in this context", &e
->where
);
1733 /* Statement functions have already been excluded above. */
1734 else if (strncmp ("%LOC", arg
->name
, 4) == 0
1735 && e
->ts
.type
== BT_PROCEDURE
)
1737 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
1739 gfc_error ("Passing internal procedure at %L by location "
1740 "not allowed", &e
->where
);
1746 /* Fortran 2008, C1237. */
1747 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_is_coindexed (e
)
1748 && gfc_has_ultimate_pointer (e
))
1750 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1751 "component", &e
->where
);
1760 /* Do the checks of the actual argument list that are specific to elemental
1761 procedures. If called with c == NULL, we have a function, otherwise if
1762 expr == NULL, we have a subroutine. */
1765 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
1767 gfc_actual_arglist
*arg0
;
1768 gfc_actual_arglist
*arg
;
1769 gfc_symbol
*esym
= NULL
;
1770 gfc_intrinsic_sym
*isym
= NULL
;
1772 gfc_intrinsic_arg
*iformal
= NULL
;
1773 gfc_formal_arglist
*eformal
= NULL
;
1774 bool formal_optional
= false;
1775 bool set_by_optional
= false;
1779 /* Is this an elemental procedure? */
1780 if (expr
&& expr
->value
.function
.actual
!= NULL
)
1782 if (expr
->value
.function
.esym
!= NULL
1783 && expr
->value
.function
.esym
->attr
.elemental
)
1785 arg0
= expr
->value
.function
.actual
;
1786 esym
= expr
->value
.function
.esym
;
1788 else if (expr
->value
.function
.isym
!= NULL
1789 && expr
->value
.function
.isym
->elemental
)
1791 arg0
= expr
->value
.function
.actual
;
1792 isym
= expr
->value
.function
.isym
;
1797 else if (c
&& c
->ext
.actual
!= NULL
)
1799 arg0
= c
->ext
.actual
;
1801 if (c
->resolved_sym
)
1802 esym
= c
->resolved_sym
;
1804 esym
= c
->symtree
->n
.sym
;
1807 if (!esym
->attr
.elemental
)
1813 /* The rank of an elemental is the rank of its array argument(s). */
1814 for (arg
= arg0
; arg
; arg
= arg
->next
)
1816 if (arg
->expr
!= NULL
&& arg
->expr
->rank
> 0)
1818 rank
= arg
->expr
->rank
;
1819 if (arg
->expr
->expr_type
== EXPR_VARIABLE
1820 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
1821 set_by_optional
= true;
1823 /* Function specific; set the result rank and shape. */
1827 if (!expr
->shape
&& arg
->expr
->shape
)
1829 expr
->shape
= gfc_get_shape (rank
);
1830 for (i
= 0; i
< rank
; i
++)
1831 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
1838 /* If it is an array, it shall not be supplied as an actual argument
1839 to an elemental procedure unless an array of the same rank is supplied
1840 as an actual argument corresponding to a nonoptional dummy argument of
1841 that elemental procedure(12.4.1.5). */
1842 formal_optional
= false;
1844 iformal
= isym
->formal
;
1846 eformal
= esym
->formal
;
1848 for (arg
= arg0
; arg
; arg
= arg
->next
)
1852 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
1853 formal_optional
= true;
1854 eformal
= eformal
->next
;
1856 else if (isym
&& iformal
)
1858 if (iformal
->optional
)
1859 formal_optional
= true;
1860 iformal
= iformal
->next
;
1863 formal_optional
= true;
1865 if (pedantic
&& arg
->expr
!= NULL
1866 && arg
->expr
->expr_type
== EXPR_VARIABLE
1867 && arg
->expr
->symtree
->n
.sym
->attr
.optional
1870 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
1871 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
1873 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1874 "MISSING, it cannot be the actual argument of an "
1875 "ELEMENTAL procedure unless there is a non-optional "
1876 "argument with the same rank (12.4.1.5)",
1877 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
1882 for (arg
= arg0
; arg
; arg
= arg
->next
)
1884 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
1887 /* Being elemental, the last upper bound of an assumed size array
1888 argument must be present. */
1889 if (resolve_assumed_size_actual (arg
->expr
))
1892 /* Elemental procedure's array actual arguments must conform. */
1895 if (gfc_check_conformance (arg
->expr
, e
,
1896 "elemental procedure") == FAILURE
)
1903 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1904 is an array, the intent inout/out variable needs to be also an array. */
1905 if (rank
> 0 && esym
&& expr
== NULL
)
1906 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
1907 arg
= arg
->next
, eformal
= eformal
->next
)
1908 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
1909 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
1910 && arg
->expr
&& arg
->expr
->rank
== 0)
1912 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1913 "ELEMENTAL subroutine '%s' is a scalar, but another "
1914 "actual argument is an array", &arg
->expr
->where
,
1915 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
1916 : "INOUT", eformal
->sym
->name
, esym
->name
);
1923 /* This function does the checking of references to global procedures
1924 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1925 77 and 95 standards. It checks for a gsymbol for the name, making
1926 one if it does not already exist. If it already exists, then the
1927 reference being resolved must correspond to the type of gsymbol.
1928 Otherwise, the new symbol is equipped with the attributes of the
1929 reference. The corresponding code that is called in creating
1930 global entities is parse.c.
1932 In addition, for all but -std=legacy, the gsymbols are used to
1933 check the interfaces of external procedures from the same file.
1934 The namespace of the gsymbol is resolved and then, once this is
1935 done the interface is checked. */
1939 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1941 if (!gsym_ns
->proc_name
->attr
.recursive
)
1944 if (sym
->ns
== gsym_ns
)
1947 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
1954 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1956 if (gsym_ns
->entries
)
1958 gfc_entry_list
*entry
= gsym_ns
->entries
;
1960 for (; entry
; entry
= entry
->next
)
1962 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
1964 if (strcmp (gsym_ns
->proc_name
->name
,
1965 sym
->ns
->proc_name
->name
) == 0)
1969 && strcmp (gsym_ns
->proc_name
->name
,
1970 sym
->ns
->parent
->proc_name
->name
) == 0)
1979 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
,
1980 gfc_actual_arglist
**actual
, int sub
)
1984 enum gfc_symbol_type type
;
1986 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
1988 gsym
= gfc_get_gsymbol (sym
->name
);
1990 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
1991 gfc_global_used (gsym
, where
);
1993 if (gfc_option
.flag_whole_file
1994 && (sym
->attr
.if_source
== IFSRC_UNKNOWN
1995 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1996 && gsym
->type
!= GSYM_UNKNOWN
1998 && gsym
->ns
->resolved
!= -1
1999 && gsym
->ns
->proc_name
2000 && not_in_recursive (sym
, gsym
->ns
)
2001 && not_entry_self_reference (sym
, gsym
->ns
))
2003 gfc_symbol
*def_sym
;
2005 /* Resolve the gsymbol namespace if needed. */
2006 if (!gsym
->ns
->resolved
)
2008 gfc_dt_list
*old_dt_list
;
2010 /* Stash away derived types so that the backend_decls do not
2012 old_dt_list
= gfc_derived_types
;
2013 gfc_derived_types
= NULL
;
2015 gfc_resolve (gsym
->ns
);
2017 /* Store the new derived types with the global namespace. */
2018 if (gfc_derived_types
)
2019 gsym
->ns
->derived_types
= gfc_derived_types
;
2021 /* Restore the derived types of this namespace. */
2022 gfc_derived_types
= old_dt_list
;
2025 /* Make sure that translation for the gsymbol occurs before
2026 the procedure currently being resolved. */
2027 ns
= gfc_global_ns_list
;
2028 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
2030 if (ns
->sibling
== gsym
->ns
)
2032 ns
->sibling
= gsym
->ns
->sibling
;
2033 gsym
->ns
->sibling
= gfc_global_ns_list
;
2034 gfc_global_ns_list
= gsym
->ns
;
2039 def_sym
= gsym
->ns
->proc_name
;
2040 if (def_sym
->attr
.entry_master
)
2042 gfc_entry_list
*entry
;
2043 for (entry
= gsym
->ns
->entries
; entry
; entry
= entry
->next
)
2044 if (strcmp (entry
->sym
->name
, sym
->name
) == 0)
2046 def_sym
= entry
->sym
;
2051 /* Differences in constant character lengths. */
2052 if (sym
->attr
.function
&& sym
->ts
.type
== BT_CHARACTER
)
2054 long int l1
= 0, l2
= 0;
2055 gfc_charlen
*cl1
= sym
->ts
.u
.cl
;
2056 gfc_charlen
*cl2
= def_sym
->ts
.u
.cl
;
2059 && cl1
->length
!= NULL
2060 && cl1
->length
->expr_type
== EXPR_CONSTANT
)
2061 l1
= mpz_get_si (cl1
->length
->value
.integer
);
2064 && cl2
->length
!= NULL
2065 && cl2
->length
->expr_type
== EXPR_CONSTANT
)
2066 l2
= mpz_get_si (cl2
->length
->value
.integer
);
2068 if (l1
&& l2
&& l1
!= l2
)
2069 gfc_error ("Character length mismatch in return type of "
2070 "function '%s' at %L (%ld/%ld)", sym
->name
,
2071 &sym
->declared_at
, l1
, l2
);
2074 /* Type mismatch of function return type and expected type. */
2075 if (sym
->attr
.function
2076 && !gfc_compare_types (&sym
->ts
, &def_sym
->ts
))
2077 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2078 sym
->name
, &sym
->declared_at
, gfc_typename (&sym
->ts
),
2079 gfc_typename (&def_sym
->ts
));
2081 if (def_sym
->formal
&& sym
->attr
.if_source
!= IFSRC_IFBODY
)
2083 gfc_formal_arglist
*arg
= def_sym
->formal
;
2084 for ( ; arg
; arg
= arg
->next
)
2087 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2088 else if (arg
->sym
->attr
.allocatable
2089 || arg
->sym
->attr
.asynchronous
2090 || arg
->sym
->attr
.optional
2091 || arg
->sym
->attr
.pointer
2092 || arg
->sym
->attr
.target
2093 || arg
->sym
->attr
.value
2094 || arg
->sym
->attr
.volatile_
)
2096 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2097 "has an attribute that requires an explicit "
2098 "interface for this procedure", arg
->sym
->name
,
2099 sym
->name
, &sym
->declared_at
);
2102 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2103 else if (arg
->sym
&& arg
->sym
->as
2104 && arg
->sym
->as
->type
== AS_ASSUMED_SHAPE
)
2106 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2107 "argument '%s' must have an explicit interface",
2108 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2111 /* F2008, 12.4.2.2 (2c) */
2112 else if (arg
->sym
->attr
.codimension
)
2114 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2115 "'%s' must have an explicit interface",
2116 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2119 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2120 else if (false) /* TODO: is a parametrized derived type */
2122 gfc_error ("Procedure '%s' at %L with parametrized derived "
2123 "type argument '%s' must have an explicit "
2124 "interface", sym
->name
, &sym
->declared_at
,
2128 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2129 else if (arg
->sym
->ts
.type
== BT_CLASS
)
2131 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2132 "argument '%s' must have an explicit interface",
2133 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2138 if (def_sym
->attr
.function
)
2140 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2141 if (def_sym
->as
&& def_sym
->as
->rank
2142 && (!sym
->as
|| sym
->as
->rank
!= def_sym
->as
->rank
))
2143 gfc_error ("The reference to function '%s' at %L either needs an "
2144 "explicit INTERFACE or the rank is incorrect", sym
->name
,
2147 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2148 if ((def_sym
->result
->attr
.pointer
2149 || def_sym
->result
->attr
.allocatable
)
2150 && (sym
->attr
.if_source
!= IFSRC_IFBODY
2151 || def_sym
->result
->attr
.pointer
2152 != sym
->result
->attr
.pointer
2153 || def_sym
->result
->attr
.allocatable
2154 != sym
->result
->attr
.allocatable
))
2155 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2156 "result must have an explicit interface", sym
->name
,
2159 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2160 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->attr
.if_source
!= IFSRC_IFBODY
2161 && def_sym
->ts
.u
.cl
->length
!= NULL
)
2163 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
2165 if (!sym
->attr
.entry_master
&& sym
->attr
.if_source
== IFSRC_UNKNOWN
2166 && cl
&& cl
->length
&& cl
->length
->expr_type
!= EXPR_CONSTANT
)
2168 gfc_error ("Nonconstant character-length function '%s' at %L "
2169 "must have an explicit interface", sym
->name
,
2175 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2176 if (def_sym
->attr
.elemental
&& !sym
->attr
.elemental
)
2178 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2179 "interface", sym
->name
, &sym
->declared_at
);
2182 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2183 if (def_sym
->attr
.is_bind_c
&& !sym
->attr
.is_bind_c
)
2185 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2186 "an explicit interface", sym
->name
, &sym
->declared_at
);
2189 if (gfc_option
.flag_whole_file
== 1
2190 || ((gfc_option
.warn_std
& GFC_STD_LEGACY
)
2191 && !(gfc_option
.warn_std
& GFC_STD_GNU
)))
2192 gfc_errors_to_warnings (1);
2194 if (sym
->attr
.if_source
!= IFSRC_IFBODY
)
2195 gfc_procedure_use (def_sym
, actual
, where
);
2197 gfc_errors_to_warnings (0);
2200 if (gsym
->type
== GSYM_UNKNOWN
)
2203 gsym
->where
= *where
;
2210 /************* Function resolution *************/
2212 /* Resolve a function call known to be generic.
2213 Section 14.1.2.4.1. */
2216 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
2220 if (sym
->attr
.generic
)
2222 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
2225 expr
->value
.function
.name
= s
->name
;
2226 expr
->value
.function
.esym
= s
;
2228 if (s
->ts
.type
!= BT_UNKNOWN
)
2230 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
2231 expr
->ts
= s
->result
->ts
;
2234 expr
->rank
= s
->as
->rank
;
2235 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
2236 expr
->rank
= s
->result
->as
->rank
;
2238 gfc_set_sym_referenced (expr
->value
.function
.esym
);
2243 /* TODO: Need to search for elemental references in generic
2247 if (sym
->attr
.intrinsic
)
2248 return gfc_intrinsic_func_interface (expr
, 0);
2255 resolve_generic_f (gfc_expr
*expr
)
2260 sym
= expr
->symtree
->n
.sym
;
2264 m
= resolve_generic_f0 (expr
, sym
);
2267 else if (m
== MATCH_ERROR
)
2271 if (sym
->ns
->parent
== NULL
)
2273 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2277 if (!generic_sym (sym
))
2281 /* Last ditch attempt. See if the reference is to an intrinsic
2282 that possesses a matching interface. 14.1.2.4 */
2283 if (sym
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
2285 gfc_error ("There is no specific function for the generic '%s' at %L",
2286 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2290 m
= gfc_intrinsic_func_interface (expr
, 0);
2294 gfc_error ("Generic function '%s' at %L is not consistent with a "
2295 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
2302 /* Resolve a function call known to be specific. */
2305 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
2309 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
2311 if (sym
->attr
.dummy
)
2313 sym
->attr
.proc
= PROC_DUMMY
;
2317 sym
->attr
.proc
= PROC_EXTERNAL
;
2321 if (sym
->attr
.proc
== PROC_MODULE
2322 || sym
->attr
.proc
== PROC_ST_FUNCTION
2323 || sym
->attr
.proc
== PROC_INTERNAL
)
2326 if (sym
->attr
.intrinsic
)
2328 m
= gfc_intrinsic_func_interface (expr
, 1);
2332 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2333 "with an intrinsic", sym
->name
, &expr
->where
);
2341 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2344 expr
->ts
= sym
->result
->ts
;
2347 expr
->value
.function
.name
= sym
->name
;
2348 expr
->value
.function
.esym
= sym
;
2349 if (sym
->as
!= NULL
)
2350 expr
->rank
= sym
->as
->rank
;
2357 resolve_specific_f (gfc_expr
*expr
)
2362 sym
= expr
->symtree
->n
.sym
;
2366 m
= resolve_specific_f0 (sym
, expr
);
2369 if (m
== MATCH_ERROR
)
2372 if (sym
->ns
->parent
== NULL
)
2375 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2381 gfc_error ("Unable to resolve the specific function '%s' at %L",
2382 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2388 /* Resolve a procedure call not known to be generic nor specific. */
2391 resolve_unknown_f (gfc_expr
*expr
)
2396 sym
= expr
->symtree
->n
.sym
;
2398 if (sym
->attr
.dummy
)
2400 sym
->attr
.proc
= PROC_DUMMY
;
2401 expr
->value
.function
.name
= sym
->name
;
2405 /* See if we have an intrinsic function reference. */
2407 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2409 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2414 /* The reference is to an external name. */
2416 sym
->attr
.proc
= PROC_EXTERNAL
;
2417 expr
->value
.function
.name
= sym
->name
;
2418 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2420 if (sym
->as
!= NULL
)
2421 expr
->rank
= sym
->as
->rank
;
2423 /* Type of the expression is either the type of the symbol or the
2424 default type of the symbol. */
2427 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2429 if (sym
->ts
.type
!= BT_UNKNOWN
)
2433 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2435 if (ts
->type
== BT_UNKNOWN
)
2437 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2438 sym
->name
, &expr
->where
);
2449 /* Return true, if the symbol is an external procedure. */
2451 is_external_proc (gfc_symbol
*sym
)
2453 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2454 && !(sym
->attr
.intrinsic
2455 || gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
))
2456 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2457 && !sym
->attr
.proc_pointer
2458 && !sym
->attr
.use_assoc
2466 /* Figure out if a function reference is pure or not. Also set the name
2467 of the function for a potential error message. Return nonzero if the
2468 function is PURE, zero if not. */
2470 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2473 pure_function (gfc_expr
*e
, const char **name
)
2479 if (e
->symtree
!= NULL
2480 && e
->symtree
->n
.sym
!= NULL
2481 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2482 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
2484 if (e
->value
.function
.esym
)
2486 pure
= gfc_pure (e
->value
.function
.esym
);
2487 *name
= e
->value
.function
.esym
->name
;
2489 else if (e
->value
.function
.isym
)
2491 pure
= e
->value
.function
.isym
->pure
2492 || e
->value
.function
.isym
->elemental
;
2493 *name
= e
->value
.function
.isym
->name
;
2497 /* Implicit functions are not pure. */
2499 *name
= e
->value
.function
.name
;
2507 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2508 int *f ATTRIBUTE_UNUSED
)
2512 /* Don't bother recursing into other statement functions
2513 since they will be checked individually for purity. */
2514 if (e
->expr_type
!= EXPR_FUNCTION
2516 || e
->symtree
->n
.sym
== sym
2517 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2520 return pure_function (e
, &name
) ? false : true;
2525 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
2527 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
2532 is_scalar_expr_ptr (gfc_expr
*expr
)
2534 gfc_try retval
= SUCCESS
;
2539 /* See if we have a gfc_ref, which means we have a substring, array
2540 reference, or a component. */
2541 if (expr
->ref
!= NULL
)
2544 while (ref
->next
!= NULL
)
2550 if (ref
->u
.ss
.length
!= NULL
2551 && ref
->u
.ss
.length
->length
!= NULL
2553 && ref
->u
.ss
.start
->expr_type
== EXPR_CONSTANT
2555 && ref
->u
.ss
.end
->expr_type
== EXPR_CONSTANT
)
2557 start
= (int) mpz_get_si (ref
->u
.ss
.start
->value
.integer
);
2558 end
= (int) mpz_get_si (ref
->u
.ss
.end
->value
.integer
);
2559 if (end
- start
+ 1 != 1)
2566 if (ref
->u
.ar
.type
== AR_ELEMENT
)
2568 else if (ref
->u
.ar
.type
== AR_FULL
)
2570 /* The user can give a full array if the array is of size 1. */
2571 if (ref
->u
.ar
.as
!= NULL
2572 && ref
->u
.ar
.as
->rank
== 1
2573 && ref
->u
.ar
.as
->type
== AS_EXPLICIT
2574 && ref
->u
.ar
.as
->lower
[0] != NULL
2575 && ref
->u
.ar
.as
->lower
[0]->expr_type
== EXPR_CONSTANT
2576 && ref
->u
.ar
.as
->upper
[0] != NULL
2577 && ref
->u
.ar
.as
->upper
[0]->expr_type
== EXPR_CONSTANT
)
2579 /* If we have a character string, we need to check if
2580 its length is one. */
2581 if (expr
->ts
.type
== BT_CHARACTER
)
2583 if (expr
->ts
.u
.cl
== NULL
2584 || expr
->ts
.u
.cl
->length
== NULL
2585 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1)
2591 /* We have constant lower and upper bounds. If the
2592 difference between is 1, it can be considered a
2594 start
= (int) mpz_get_si
2595 (ref
->u
.ar
.as
->lower
[0]->value
.integer
);
2596 end
= (int) mpz_get_si
2597 (ref
->u
.ar
.as
->upper
[0]->value
.integer
);
2598 if (end
- start
+ 1 != 1)
2613 else if (expr
->ts
.type
== BT_CHARACTER
&& expr
->rank
== 0)
2615 /* Character string. Make sure it's of length 1. */
2616 if (expr
->ts
.u
.cl
== NULL
2617 || expr
->ts
.u
.cl
->length
== NULL
2618 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
2621 else if (expr
->rank
!= 0)
2628 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2629 and, in the case of c_associated, set the binding label based on
2633 gfc_iso_c_func_interface (gfc_symbol
*sym
, gfc_actual_arglist
*args
,
2634 gfc_symbol
**new_sym
)
2636 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2637 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2638 int optional_arg
= 0;
2639 gfc_try retval
= SUCCESS
;
2640 gfc_symbol
*args_sym
;
2641 gfc_typespec
*arg_ts
;
2642 symbol_attribute arg_attr
;
2644 if (args
->expr
->expr_type
== EXPR_CONSTANT
2645 || args
->expr
->expr_type
== EXPR_OP
2646 || args
->expr
->expr_type
== EXPR_NULL
)
2648 gfc_error ("Argument to '%s' at %L is not a variable",
2649 sym
->name
, &(args
->expr
->where
));
2653 args_sym
= args
->expr
->symtree
->n
.sym
;
2655 /* The typespec for the actual arg should be that stored in the expr
2656 and not necessarily that of the expr symbol (args_sym), because
2657 the actual expression could be a part-ref of the expr symbol. */
2658 arg_ts
= &(args
->expr
->ts
);
2659 arg_attr
= gfc_expr_attr (args
->expr
);
2661 if (sym
->intmod_sym_id
== ISOCBINDING_ASSOCIATED
)
2663 /* If the user gave two args then they are providing something for
2664 the optional arg (the second cptr). Therefore, set the name and
2665 binding label to the c_associated for two cptrs. Otherwise,
2666 set c_associated to expect one cptr. */
2670 sprintf (name
, "%s_2", sym
->name
);
2671 sprintf (binding_label
, "%s_2", sym
->binding_label
);
2677 sprintf (name
, "%s_1", sym
->name
);
2678 sprintf (binding_label
, "%s_1", sym
->binding_label
);
2682 /* Get a new symbol for the version of c_associated that
2684 *new_sym
= get_iso_c_sym (sym
, name
, binding_label
, optional_arg
);
2686 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
2687 || sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2689 sprintf (name
, "%s", sym
->name
);
2690 sprintf (binding_label
, "%s", sym
->binding_label
);
2692 /* Error check the call. */
2693 if (args
->next
!= NULL
)
2695 gfc_error_now ("More actual than formal arguments in '%s' "
2696 "call at %L", name
, &(args
->expr
->where
));
2699 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
)
2701 /* Make sure we have either the target or pointer attribute. */
2702 if (!arg_attr
.target
&& !arg_attr
.pointer
)
2704 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2705 "a TARGET or an associated pointer",
2707 sym
->name
, &(args
->expr
->where
));
2711 /* See if we have interoperable type and type param. */
2712 if (verify_c_interop (arg_ts
) == SUCCESS
2713 || gfc_check_any_c_kind (arg_ts
) == SUCCESS
)
2715 if (args_sym
->attr
.target
== 1)
2717 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2718 has the target attribute and is interoperable. */
2719 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2720 allocatable variable that has the TARGET attribute and
2721 is not an array of zero size. */
2722 if (args_sym
->attr
.allocatable
== 1)
2724 if (args_sym
->attr
.dimension
!= 0
2725 && (args_sym
->as
&& args_sym
->as
->rank
== 0))
2727 gfc_error_now ("Allocatable variable '%s' used as a "
2728 "parameter to '%s' at %L must not be "
2729 "an array of zero size",
2730 args_sym
->name
, sym
->name
,
2731 &(args
->expr
->where
));
2737 /* A non-allocatable target variable with C
2738 interoperable type and type parameters must be
2740 if (args_sym
&& args_sym
->attr
.dimension
)
2742 if (args_sym
->as
->type
== AS_ASSUMED_SHAPE
)
2744 gfc_error ("Assumed-shape array '%s' at %L "
2745 "cannot be an argument to the "
2746 "procedure '%s' because "
2747 "it is not C interoperable",
2749 &(args
->expr
->where
), sym
->name
);
2752 else if (args_sym
->as
->type
== AS_DEFERRED
)
2754 gfc_error ("Deferred-shape array '%s' at %L "
2755 "cannot be an argument to the "
2756 "procedure '%s' because "
2757 "it is not C interoperable",
2759 &(args
->expr
->where
), sym
->name
);
2764 /* Make sure it's not a character string. Arrays of
2765 any type should be ok if the variable is of a C
2766 interoperable type. */
2767 if (arg_ts
->type
== BT_CHARACTER
)
2768 if (arg_ts
->u
.cl
!= NULL
2769 && (arg_ts
->u
.cl
->length
== NULL
2770 || arg_ts
->u
.cl
->length
->expr_type
2773 (arg_ts
->u
.cl
->length
->value
.integer
, 1)
2775 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2777 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2778 "at %L must have a length of 1",
2779 args_sym
->name
, sym
->name
,
2780 &(args
->expr
->where
));
2785 else if (arg_attr
.pointer
2786 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2788 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2790 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2791 "associated scalar POINTER", args_sym
->name
,
2792 sym
->name
, &(args
->expr
->where
));
2798 /* The parameter is not required to be C interoperable. If it
2799 is not C interoperable, it must be a nonpolymorphic scalar
2800 with no length type parameters. It still must have either
2801 the pointer or target attribute, and it can be
2802 allocatable (but must be allocated when c_loc is called). */
2803 if (args
->expr
->rank
!= 0
2804 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2806 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2807 "scalar", args_sym
->name
, sym
->name
,
2808 &(args
->expr
->where
));
2811 else if (arg_ts
->type
== BT_CHARACTER
2812 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2814 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2815 "%L must have a length of 1",
2816 args_sym
->name
, sym
->name
,
2817 &(args
->expr
->where
));
2820 else if (arg_ts
->type
== BT_CLASS
)
2822 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2823 "polymorphic", args_sym
->name
, sym
->name
,
2824 &(args
->expr
->where
));
2829 else if (sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2831 if (args_sym
->attr
.flavor
!= FL_PROCEDURE
)
2833 /* TODO: Update this error message to allow for procedure
2834 pointers once they are implemented. */
2835 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2837 args_sym
->name
, sym
->name
,
2838 &(args
->expr
->where
));
2841 else if (args_sym
->attr
.is_bind_c
!= 1)
2843 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2845 args_sym
->name
, sym
->name
,
2846 &(args
->expr
->where
));
2851 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2856 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2857 "iso_c_binding function: '%s'!\n", sym
->name
);
2864 /* Resolve a function call, which means resolving the arguments, then figuring
2865 out which entity the name refers to. */
2868 resolve_function (gfc_expr
*expr
)
2870 gfc_actual_arglist
*arg
;
2875 procedure_type p
= PROC_INTRINSIC
;
2876 bool no_formal_args
;
2880 sym
= expr
->symtree
->n
.sym
;
2882 /* If this is a procedure pointer component, it has already been resolved. */
2883 if (gfc_is_proc_ptr_comp (expr
, NULL
))
2886 if (sym
&& sym
->attr
.intrinsic
2887 && resolve_intrinsic (sym
, &expr
->where
) == FAILURE
)
2890 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
2892 gfc_error ("'%s' at %L is not a function", sym
->name
, &expr
->where
);
2896 /* If this ia a deferred TBP with an abstract interface (which may
2897 of course be referenced), expr->value.function.esym will be set. */
2898 if (sym
&& sym
->attr
.abstract
&& !expr
->value
.function
.esym
)
2900 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2901 sym
->name
, &expr
->where
);
2905 /* Switch off assumed size checking and do this again for certain kinds
2906 of procedure, once the procedure itself is resolved. */
2907 need_full_assumed_size
++;
2909 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
2910 p
= expr
->symtree
->n
.sym
->attr
.proc
;
2912 if (expr
->value
.function
.isym
&& expr
->value
.function
.isym
->inquiry
)
2913 inquiry_argument
= true;
2914 no_formal_args
= sym
&& is_external_proc (sym
) && sym
->formal
== NULL
;
2916 if (resolve_actual_arglist (expr
->value
.function
.actual
,
2917 p
, no_formal_args
) == FAILURE
)
2919 inquiry_argument
= false;
2923 inquiry_argument
= false;
2925 /* Need to setup the call to the correct c_associated, depending on
2926 the number of cptrs to user gives to compare. */
2927 if (sym
&& sym
->attr
.is_iso_c
== 1)
2929 if (gfc_iso_c_func_interface (sym
, expr
->value
.function
.actual
, &sym
)
2933 /* Get the symtree for the new symbol (resolved func).
2934 the old one will be freed later, when it's no longer used. */
2935 gfc_find_sym_tree (sym
->name
, sym
->ns
, 1, &(expr
->symtree
));
2938 /* Resume assumed_size checking. */
2939 need_full_assumed_size
--;
2941 /* If the procedure is external, check for usage. */
2942 if (sym
&& is_external_proc (sym
))
2943 resolve_global_procedure (sym
, &expr
->where
,
2944 &expr
->value
.function
.actual
, 0);
2946 if (sym
&& sym
->ts
.type
== BT_CHARACTER
2948 && sym
->ts
.u
.cl
->length
== NULL
2950 && expr
->value
.function
.esym
== NULL
2951 && !sym
->attr
.contained
)
2953 /* Internal procedures are taken care of in resolve_contained_fntype. */
2954 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2955 "be used at %L since it is not a dummy argument",
2956 sym
->name
, &expr
->where
);
2960 /* See if function is already resolved. */
2962 if (expr
->value
.function
.name
!= NULL
)
2964 if (expr
->ts
.type
== BT_UNKNOWN
)
2970 /* Apply the rules of section 14.1.2. */
2972 switch (procedure_kind (sym
))
2975 t
= resolve_generic_f (expr
);
2978 case PTYPE_SPECIFIC
:
2979 t
= resolve_specific_f (expr
);
2983 t
= resolve_unknown_f (expr
);
2987 gfc_internal_error ("resolve_function(): bad function type");
2991 /* If the expression is still a function (it might have simplified),
2992 then we check to see if we are calling an elemental function. */
2994 if (expr
->expr_type
!= EXPR_FUNCTION
)
2997 temp
= need_full_assumed_size
;
2998 need_full_assumed_size
= 0;
3000 if (resolve_elemental_actual (expr
, NULL
) == FAILURE
)
3003 if (omp_workshare_flag
3004 && expr
->value
.function
.esym
3005 && ! gfc_elemental (expr
->value
.function
.esym
))
3007 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3008 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
3013 #define GENERIC_ID expr->value.function.isym->id
3014 else if (expr
->value
.function
.actual
!= NULL
3015 && expr
->value
.function
.isym
!= NULL
3016 && GENERIC_ID
!= GFC_ISYM_LBOUND
3017 && GENERIC_ID
!= GFC_ISYM_LEN
3018 && GENERIC_ID
!= GFC_ISYM_LOC
3019 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
3021 /* Array intrinsics must also have the last upper bound of an
3022 assumed size array argument. UBOUND and SIZE have to be
3023 excluded from the check if the second argument is anything
3026 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
3028 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
3029 && arg
->next
!= NULL
&& arg
->next
->expr
)
3031 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
3034 if (arg
->next
->name
&& strncmp(arg
->next
->name
, "kind", 4) == 0)
3037 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
3042 if (arg
->expr
!= NULL
3043 && arg
->expr
->rank
> 0
3044 && resolve_assumed_size_actual (arg
->expr
))
3050 need_full_assumed_size
= temp
;
3053 if (!pure_function (expr
, &name
) && name
)
3057 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3058 "FORALL %s", name
, &expr
->where
,
3059 forall_flag
== 2 ? "mask" : "block");
3062 else if (gfc_pure (NULL
))
3064 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3065 "procedure within a PURE procedure", name
, &expr
->where
);
3070 /* Functions without the RECURSIVE attribution are not allowed to
3071 * call themselves. */
3072 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
3075 esym
= expr
->value
.function
.esym
;
3077 if (is_illegal_recursion (esym
, gfc_current_ns
))
3079 if (esym
->attr
.entry
&& esym
->ns
->entries
)
3080 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3081 " function '%s' is not RECURSIVE",
3082 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
3084 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3085 " is not RECURSIVE", esym
->name
, &expr
->where
);
3091 /* Character lengths of use associated functions may contains references to
3092 symbols not referenced from the current program unit otherwise. Make sure
3093 those symbols are marked as referenced. */
3095 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
3096 && expr
->value
.function
.esym
->attr
.use_assoc
)
3098 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
3101 /* Make sure that the expression has a typespec that works. */
3102 if (expr
->ts
.type
== BT_UNKNOWN
)
3104 if (expr
->symtree
->n
.sym
->result
3105 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
3106 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
3107 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
3114 /************* Subroutine resolution *************/
3117 pure_subroutine (gfc_code
*c
, gfc_symbol
*sym
)
3123 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3124 sym
->name
, &c
->loc
);
3125 else if (gfc_pure (NULL
))
3126 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym
->name
,
3132 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3136 if (sym
->attr
.generic
)
3138 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
3141 c
->resolved_sym
= s
;
3142 pure_subroutine (c
, s
);
3146 /* TODO: Need to search for elemental references in generic interface. */
3149 if (sym
->attr
.intrinsic
)
3150 return gfc_intrinsic_sub_interface (c
, 0);
3157 resolve_generic_s (gfc_code
*c
)
3162 sym
= c
->symtree
->n
.sym
;
3166 m
= resolve_generic_s0 (c
, sym
);
3169 else if (m
== MATCH_ERROR
)
3173 if (sym
->ns
->parent
== NULL
)
3175 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3179 if (!generic_sym (sym
))
3183 /* Last ditch attempt. See if the reference is to an intrinsic
3184 that possesses a matching interface. 14.1.2.4 */
3185 sym
= c
->symtree
->n
.sym
;
3187 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
3189 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3190 sym
->name
, &c
->loc
);
3194 m
= gfc_intrinsic_sub_interface (c
, 0);
3198 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3199 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
3205 /* Set the name and binding label of the subroutine symbol in the call
3206 expression represented by 'c' to include the type and kind of the
3207 second parameter. This function is for resolving the appropriate
3208 version of c_f_pointer() and c_f_procpointer(). For example, a
3209 call to c_f_pointer() for a default integer pointer could have a
3210 name of c_f_pointer_i4. If no second arg exists, which is an error
3211 for these two functions, it defaults to the generic symbol's name
3212 and binding label. */
3215 set_name_and_label (gfc_code
*c
, gfc_symbol
*sym
,
3216 char *name
, char *binding_label
)
3218 gfc_expr
*arg
= NULL
;
3222 /* The second arg of c_f_pointer and c_f_procpointer determines
3223 the type and kind for the procedure name. */
3224 arg
= c
->ext
.actual
->next
->expr
;
3228 /* Set up the name to have the given symbol's name,
3229 plus the type and kind. */
3230 /* a derived type is marked with the type letter 'u' */
3231 if (arg
->ts
.type
== BT_DERIVED
)
3234 kind
= 0; /* set the kind as 0 for now */
3238 type
= gfc_type_letter (arg
->ts
.type
);
3239 kind
= arg
->ts
.kind
;
3242 if (arg
->ts
.type
== BT_CHARACTER
)
3243 /* Kind info for character strings not needed. */
3246 sprintf (name
, "%s_%c%d", sym
->name
, type
, kind
);
3247 /* Set up the binding label as the given symbol's label plus
3248 the type and kind. */
3249 sprintf (binding_label
, "%s_%c%d", sym
->binding_label
, type
, kind
);
3253 /* If the second arg is missing, set the name and label as
3254 was, cause it should at least be found, and the missing
3255 arg error will be caught by compare_parameters(). */
3256 sprintf (name
, "%s", sym
->name
);
3257 sprintf (binding_label
, "%s", sym
->binding_label
);
3264 /* Resolve a generic version of the iso_c_binding procedure given
3265 (sym) to the specific one based on the type and kind of the
3266 argument(s). Currently, this function resolves c_f_pointer() and
3267 c_f_procpointer based on the type and kind of the second argument
3268 (FPTR). Other iso_c_binding procedures aren't specially handled.
3269 Upon successfully exiting, c->resolved_sym will hold the resolved
3270 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3274 gfc_iso_c_sub_interface (gfc_code
*c
, gfc_symbol
*sym
)
3276 gfc_symbol
*new_sym
;
3277 /* this is fine, since we know the names won't use the max */
3278 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3279 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
3280 /* default to success; will override if find error */
3281 match m
= MATCH_YES
;
3283 /* Make sure the actual arguments are in the necessary order (based on the
3284 formal args) before resolving. */
3285 gfc_procedure_use (sym
, &c
->ext
.actual
, &(c
->loc
));
3287 if ((sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
) ||
3288 (sym
->intmod_sym_id
== ISOCBINDING_F_PROCPOINTER
))
3290 set_name_and_label (c
, sym
, name
, binding_label
);
3292 if (sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
)
3294 if (c
->ext
.actual
!= NULL
&& c
->ext
.actual
->next
!= NULL
)
3296 /* Make sure we got a third arg if the second arg has non-zero
3297 rank. We must also check that the type and rank are
3298 correct since we short-circuit this check in
3299 gfc_procedure_use() (called above to sort actual args). */
3300 if (c
->ext
.actual
->next
->expr
->rank
!= 0)
3302 if(c
->ext
.actual
->next
->next
== NULL
3303 || c
->ext
.actual
->next
->next
->expr
== NULL
)
3306 gfc_error ("Missing SHAPE parameter for call to %s "
3307 "at %L", sym
->name
, &(c
->loc
));
3309 else if (c
->ext
.actual
->next
->next
->expr
->ts
.type
3311 || c
->ext
.actual
->next
->next
->expr
->rank
!= 1)
3314 gfc_error ("SHAPE parameter for call to %s at %L must "
3315 "be a rank 1 INTEGER array", sym
->name
,
3322 if (m
!= MATCH_ERROR
)
3324 /* the 1 means to add the optional arg to formal list */
3325 new_sym
= get_iso_c_sym (sym
, name
, binding_label
, 1);
3327 /* for error reporting, say it's declared where the original was */
3328 new_sym
->declared_at
= sym
->declared_at
;
3333 /* no differences for c_loc or c_funloc */
3337 /* set the resolved symbol */
3338 if (m
!= MATCH_ERROR
)
3339 c
->resolved_sym
= new_sym
;
3341 c
->resolved_sym
= sym
;
3347 /* Resolve a subroutine call known to be specific. */
3350 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3354 if(sym
->attr
.is_iso_c
)
3356 m
= gfc_iso_c_sub_interface (c
,sym
);
3360 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
3362 if (sym
->attr
.dummy
)
3364 sym
->attr
.proc
= PROC_DUMMY
;
3368 sym
->attr
.proc
= PROC_EXTERNAL
;
3372 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
3375 if (sym
->attr
.intrinsic
)
3377 m
= gfc_intrinsic_sub_interface (c
, 1);
3381 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3382 "with an intrinsic", sym
->name
, &c
->loc
);
3390 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3392 c
->resolved_sym
= sym
;
3393 pure_subroutine (c
, sym
);
3400 resolve_specific_s (gfc_code
*c
)
3405 sym
= c
->symtree
->n
.sym
;
3409 m
= resolve_specific_s0 (c
, sym
);
3412 if (m
== MATCH_ERROR
)
3415 if (sym
->ns
->parent
== NULL
)
3418 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3424 sym
= c
->symtree
->n
.sym
;
3425 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3426 sym
->name
, &c
->loc
);
3432 /* Resolve a subroutine call not known to be generic nor specific. */
3435 resolve_unknown_s (gfc_code
*c
)
3439 sym
= c
->symtree
->n
.sym
;
3441 if (sym
->attr
.dummy
)
3443 sym
->attr
.proc
= PROC_DUMMY
;
3447 /* See if we have an intrinsic function reference. */
3449 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3451 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3456 /* The reference is to an external name. */
3459 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3461 c
->resolved_sym
= sym
;
3463 pure_subroutine (c
, sym
);
3469 /* Resolve a subroutine call. Although it was tempting to use the same code
3470 for functions, subroutines and functions are stored differently and this
3471 makes things awkward. */
3474 resolve_call (gfc_code
*c
)
3477 procedure_type ptype
= PROC_INTRINSIC
;
3478 gfc_symbol
*csym
, *sym
;
3479 bool no_formal_args
;
3481 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3483 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3485 gfc_error ("'%s' at %L has a type, which is not consistent with "
3486 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3490 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3493 gfc_find_sym_tree (csym
->name
, gfc_current_ns
, 1, &st
);
3494 sym
= st
? st
->n
.sym
: NULL
;
3495 if (sym
&& csym
!= sym
3496 && sym
->ns
== gfc_current_ns
3497 && sym
->attr
.flavor
== FL_PROCEDURE
3498 && sym
->attr
.contained
)
3501 if (csym
->attr
.generic
)
3502 c
->symtree
->n
.sym
= sym
;
3505 csym
= c
->symtree
->n
.sym
;
3509 /* If this ia a deferred TBP with an abstract interface
3510 (which may of course be referenced), c->expr1 will be set. */
3511 if (csym
&& csym
->attr
.abstract
&& !c
->expr1
)
3513 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3514 csym
->name
, &c
->loc
);
3518 /* Subroutines without the RECURSIVE attribution are not allowed to
3519 * call themselves. */
3520 if (csym
&& is_illegal_recursion (csym
, gfc_current_ns
))
3522 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3523 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3524 " subroutine '%s' is not RECURSIVE",
3525 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3527 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3528 " is not RECURSIVE", csym
->name
, &c
->loc
);
3533 /* Switch off assumed size checking and do this again for certain kinds
3534 of procedure, once the procedure itself is resolved. */
3535 need_full_assumed_size
++;
3538 ptype
= csym
->attr
.proc
;
3540 no_formal_args
= csym
&& is_external_proc (csym
) && csym
->formal
== NULL
;
3541 if (resolve_actual_arglist (c
->ext
.actual
, ptype
,
3542 no_formal_args
) == FAILURE
)
3545 /* Resume assumed_size checking. */
3546 need_full_assumed_size
--;
3548 /* If external, check for usage. */
3549 if (csym
&& is_external_proc (csym
))
3550 resolve_global_procedure (csym
, &c
->loc
, &c
->ext
.actual
, 1);
3553 if (c
->resolved_sym
== NULL
)
3555 c
->resolved_isym
= NULL
;
3556 switch (procedure_kind (csym
))
3559 t
= resolve_generic_s (c
);
3562 case PTYPE_SPECIFIC
:
3563 t
= resolve_specific_s (c
);
3567 t
= resolve_unknown_s (c
);
3571 gfc_internal_error ("resolve_subroutine(): bad function type");
3575 /* Some checks of elemental subroutine actual arguments. */
3576 if (resolve_elemental_actual (NULL
, c
) == FAILURE
)
3583 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3584 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3585 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3586 if their shapes do not match. If either op1->shape or op2->shape is
3587 NULL, return SUCCESS. */
3590 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3597 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3599 for (i
= 0; i
< op1
->rank
; i
++)
3601 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3603 gfc_error ("Shapes for operands at %L and %L are not conformable",
3604 &op1
->where
, &op2
->where
);
3615 /* Resolve an operator expression node. This can involve replacing the
3616 operation with a user defined function call. */
3619 resolve_operator (gfc_expr
*e
)
3621 gfc_expr
*op1
, *op2
;
3623 bool dual_locus_error
;
3626 /* Resolve all subnodes-- give them types. */
3628 switch (e
->value
.op
.op
)
3631 if (gfc_resolve_expr (e
->value
.op
.op2
) == FAILURE
)
3634 /* Fall through... */
3637 case INTRINSIC_UPLUS
:
3638 case INTRINSIC_UMINUS
:
3639 case INTRINSIC_PARENTHESES
:
3640 if (gfc_resolve_expr (e
->value
.op
.op1
) == FAILURE
)
3645 /* Typecheck the new node. */
3647 op1
= e
->value
.op
.op1
;
3648 op2
= e
->value
.op
.op2
;
3649 dual_locus_error
= false;
3651 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
3652 || (op2
&& op2
->expr_type
== EXPR_NULL
))
3654 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
3658 switch (e
->value
.op
.op
)
3660 case INTRINSIC_UPLUS
:
3661 case INTRINSIC_UMINUS
:
3662 if (op1
->ts
.type
== BT_INTEGER
3663 || op1
->ts
.type
== BT_REAL
3664 || op1
->ts
.type
== BT_COMPLEX
)
3670 sprintf (msg
, _("Operand of unary numeric operator '%s' at %%L is %s"),
3671 gfc_op2string (e
->value
.op
.op
), gfc_typename (&e
->ts
));
3674 case INTRINSIC_PLUS
:
3675 case INTRINSIC_MINUS
:
3676 case INTRINSIC_TIMES
:
3677 case INTRINSIC_DIVIDE
:
3678 case INTRINSIC_POWER
:
3679 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3681 gfc_type_convert_binary (e
, 1);
3686 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3687 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3688 gfc_typename (&op2
->ts
));
3691 case INTRINSIC_CONCAT
:
3692 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3693 && op1
->ts
.kind
== op2
->ts
.kind
)
3695 e
->ts
.type
= BT_CHARACTER
;
3696 e
->ts
.kind
= op1
->ts
.kind
;
3701 _("Operands of string concatenation operator at %%L are %s/%s"),
3702 gfc_typename (&op1
->ts
), gfc_typename (&op2
->ts
));
3708 case INTRINSIC_NEQV
:
3709 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3711 e
->ts
.type
= BT_LOGICAL
;
3712 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
3713 if (op1
->ts
.kind
< e
->ts
.kind
)
3714 gfc_convert_type (op1
, &e
->ts
, 2);
3715 else if (op2
->ts
.kind
< e
->ts
.kind
)
3716 gfc_convert_type (op2
, &e
->ts
, 2);
3720 sprintf (msg
, _("Operands of logical operator '%s' at %%L are %s/%s"),
3721 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3722 gfc_typename (&op2
->ts
));
3727 if (op1
->ts
.type
== BT_LOGICAL
)
3729 e
->ts
.type
= BT_LOGICAL
;
3730 e
->ts
.kind
= op1
->ts
.kind
;
3734 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
3735 gfc_typename (&op1
->ts
));
3739 case INTRINSIC_GT_OS
:
3741 case INTRINSIC_GE_OS
:
3743 case INTRINSIC_LT_OS
:
3745 case INTRINSIC_LE_OS
:
3746 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
3748 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
3752 /* Fall through... */
3755 case INTRINSIC_EQ_OS
:
3757 case INTRINSIC_NE_OS
:
3758 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3759 && op1
->ts
.kind
== op2
->ts
.kind
)
3761 e
->ts
.type
= BT_LOGICAL
;
3762 e
->ts
.kind
= gfc_default_logical_kind
;
3766 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3768 gfc_type_convert_binary (e
, 1);
3770 e
->ts
.type
= BT_LOGICAL
;
3771 e
->ts
.kind
= gfc_default_logical_kind
;
3775 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3777 _("Logicals at %%L must be compared with %s instead of %s"),
3778 (e
->value
.op
.op
== INTRINSIC_EQ
3779 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
3780 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
3783 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3784 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3785 gfc_typename (&op2
->ts
));
3789 case INTRINSIC_USER
:
3790 if (e
->value
.op
.uop
->op
== NULL
)
3791 sprintf (msg
, _("Unknown operator '%s' at %%L"), e
->value
.op
.uop
->name
);
3792 else if (op2
== NULL
)
3793 sprintf (msg
, _("Operand of user operator '%s' at %%L is %s"),
3794 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
));
3796 sprintf (msg
, _("Operands of user operator '%s' at %%L are %s/%s"),
3797 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
),
3798 gfc_typename (&op2
->ts
));
3802 case INTRINSIC_PARENTHESES
:
3804 if (e
->ts
.type
== BT_CHARACTER
)
3805 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
3809 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3812 /* Deal with arrayness of an operand through an operator. */
3816 switch (e
->value
.op
.op
)
3818 case INTRINSIC_PLUS
:
3819 case INTRINSIC_MINUS
:
3820 case INTRINSIC_TIMES
:
3821 case INTRINSIC_DIVIDE
:
3822 case INTRINSIC_POWER
:
3823 case INTRINSIC_CONCAT
:
3827 case INTRINSIC_NEQV
:
3829 case INTRINSIC_EQ_OS
:
3831 case INTRINSIC_NE_OS
:
3833 case INTRINSIC_GT_OS
:
3835 case INTRINSIC_GE_OS
:
3837 case INTRINSIC_LT_OS
:
3839 case INTRINSIC_LE_OS
:
3841 if (op1
->rank
== 0 && op2
->rank
== 0)
3844 if (op1
->rank
== 0 && op2
->rank
!= 0)
3846 e
->rank
= op2
->rank
;
3848 if (e
->shape
== NULL
)
3849 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
3852 if (op1
->rank
!= 0 && op2
->rank
== 0)
3854 e
->rank
= op1
->rank
;
3856 if (e
->shape
== NULL
)
3857 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3860 if (op1
->rank
!= 0 && op2
->rank
!= 0)
3862 if (op1
->rank
== op2
->rank
)
3864 e
->rank
= op1
->rank
;
3865 if (e
->shape
== NULL
)
3867 t
= compare_shapes (op1
, op2
);
3871 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3876 /* Allow higher level expressions to work. */
3879 /* Try user-defined operators, and otherwise throw an error. */
3880 dual_locus_error
= true;
3882 _("Inconsistent ranks for operator at %%L and %%L"));
3889 case INTRINSIC_PARENTHESES
:
3891 case INTRINSIC_UPLUS
:
3892 case INTRINSIC_UMINUS
:
3893 /* Simply copy arrayness attribute */
3894 e
->rank
= op1
->rank
;
3896 if (e
->shape
== NULL
)
3897 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3905 /* Attempt to simplify the expression. */
3908 t
= gfc_simplify_expr (e
, 0);
3909 /* Some calls do not succeed in simplification and return FAILURE
3910 even though there is no error; e.g. variable references to
3911 PARAMETER arrays. */
3912 if (!gfc_is_constant_expr (e
))
3921 if (gfc_extend_expr (e
, &real_error
) == SUCCESS
)
3928 if (dual_locus_error
)
3929 gfc_error (msg
, &op1
->where
, &op2
->where
);
3931 gfc_error (msg
, &e
->where
);
3937 /************** Array resolution subroutines **************/
3940 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
}
3943 /* Compare two integer expressions. */
3946 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
3950 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
3951 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
3954 /* If either of the types isn't INTEGER, we must have
3955 raised an error earlier. */
3957 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
3960 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
3970 /* Compare an integer expression with an integer. */
3973 compare_bound_int (gfc_expr
*a
, int b
)
3977 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
3980 if (a
->ts
.type
!= BT_INTEGER
)
3981 gfc_internal_error ("compare_bound_int(): Bad expression");
3983 i
= mpz_cmp_si (a
->value
.integer
, b
);
3993 /* Compare an integer expression with a mpz_t. */
3996 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
4000 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4003 if (a
->ts
.type
!= BT_INTEGER
)
4004 gfc_internal_error ("compare_bound_int(): Bad expression");
4006 i
= mpz_cmp (a
->value
.integer
, b
);
4016 /* Compute the last value of a sequence given by a triplet.
4017 Return 0 if it wasn't able to compute the last value, or if the
4018 sequence if empty, and 1 otherwise. */
4021 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
4022 gfc_expr
*stride
, mpz_t last
)
4026 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
4027 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
4028 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
4031 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
4032 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
4035 if (stride
== NULL
|| compare_bound_int(stride
, 1) == CMP_EQ
)
4037 if (compare_bound (start
, end
) == CMP_GT
)
4039 mpz_set (last
, end
->value
.integer
);
4043 if (compare_bound_int (stride
, 0) == CMP_GT
)
4045 /* Stride is positive */
4046 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
4051 /* Stride is negative */
4052 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
4057 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
4058 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
4059 mpz_sub (last
, end
->value
.integer
, rem
);
4066 /* Compare a single dimension of an array reference to the array
4070 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
4074 if (ar
->dimen_type
[i
] == DIMEN_STAR
)
4076 gcc_assert (ar
->stride
[i
] == NULL
);
4077 /* This implies [*] as [*:] and [*:3] are not possible. */
4078 if (ar
->start
[i
] == NULL
)
4080 gcc_assert (ar
->end
[i
] == NULL
);
4085 /* Given start, end and stride values, calculate the minimum and
4086 maximum referenced indexes. */
4088 switch (ar
->dimen_type
[i
])
4095 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
4098 gfc_warning ("Array reference at %L is out of bounds "
4099 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4100 mpz_get_si (ar
->start
[i
]->value
.integer
),
4101 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4103 gfc_warning ("Array reference at %L is out of bounds "
4104 "(%ld < %ld) in codimension %d", &ar
->c_where
[i
],
4105 mpz_get_si (ar
->start
[i
]->value
.integer
),
4106 mpz_get_si (as
->lower
[i
]->value
.integer
),
4110 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
4113 gfc_warning ("Array reference at %L is out of bounds "
4114 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4115 mpz_get_si (ar
->start
[i
]->value
.integer
),
4116 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4118 gfc_warning ("Array reference at %L is out of bounds "
4119 "(%ld > %ld) in codimension %d", &ar
->c_where
[i
],
4120 mpz_get_si (ar
->start
[i
]->value
.integer
),
4121 mpz_get_si (as
->upper
[i
]->value
.integer
),
4130 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4131 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4133 comparison comp_start_end
= compare_bound (AR_START
, AR_END
);
4135 /* Check for zero stride, which is not allowed. */
4136 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
4138 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
4142 /* if start == len || (stride > 0 && start < len)
4143 || (stride < 0 && start > len),
4144 then the array section contains at least one element. In this
4145 case, there is an out-of-bounds access if
4146 (start < lower || start > upper). */
4147 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
4148 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
4149 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
4150 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
4151 && comp_start_end
== CMP_GT
))
4153 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
4155 gfc_warning ("Lower array reference at %L is out of bounds "
4156 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4157 mpz_get_si (AR_START
->value
.integer
),
4158 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4161 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
4163 gfc_warning ("Lower array reference at %L is out of bounds "
4164 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4165 mpz_get_si (AR_START
->value
.integer
),
4166 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4171 /* If we can compute the highest index of the array section,
4172 then it also has to be between lower and upper. */
4173 mpz_init (last_value
);
4174 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
4177 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
4179 gfc_warning ("Upper array reference at %L is out of bounds "
4180 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4181 mpz_get_si (last_value
),
4182 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4183 mpz_clear (last_value
);
4186 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
4188 gfc_warning ("Upper array reference at %L is out of bounds "
4189 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4190 mpz_get_si (last_value
),
4191 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4192 mpz_clear (last_value
);
4196 mpz_clear (last_value
);
4204 gfc_internal_error ("check_dimension(): Bad array reference");
4211 /* Compare an array reference with an array specification. */
4214 compare_spec_to_ref (gfc_array_ref
*ar
)
4221 /* TODO: Full array sections are only allowed as actual parameters. */
4222 if (as
->type
== AS_ASSUMED_SIZE
4223 && (/*ar->type == AR_FULL
4224 ||*/ (ar
->type
== AR_SECTION
4225 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
4227 gfc_error ("Rightmost upper bound of assumed size array section "
4228 "not specified at %L", &ar
->where
);
4232 if (ar
->type
== AR_FULL
)
4235 if (as
->rank
!= ar
->dimen
)
4237 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4238 &ar
->where
, ar
->dimen
, as
->rank
);
4242 /* ar->codimen == 0 is a local array. */
4243 if (as
->corank
!= ar
->codimen
&& ar
->codimen
!= 0)
4245 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4246 &ar
->where
, ar
->codimen
, as
->corank
);
4250 for (i
= 0; i
< as
->rank
; i
++)
4251 if (check_dimension (i
, ar
, as
) == FAILURE
)
4254 /* Local access has no coarray spec. */
4255 if (ar
->codimen
!= 0)
4256 for (i
= as
->rank
; i
< as
->rank
+ as
->corank
; i
++)
4258 if (ar
->dimen_type
[i
] != DIMEN_ELEMENT
&& !ar
->in_allocate
)
4260 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4261 i
+ 1 - as
->rank
, &ar
->where
);
4264 if (check_dimension (i
, ar
, as
) == FAILURE
)
4272 /* Resolve one part of an array index. */
4275 gfc_resolve_index_1 (gfc_expr
*index
, int check_scalar
,
4276 int force_index_integer_kind
)
4283 if (gfc_resolve_expr (index
) == FAILURE
)
4286 if (check_scalar
&& index
->rank
!= 0)
4288 gfc_error ("Array index at %L must be scalar", &index
->where
);
4292 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
4294 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4295 &index
->where
, gfc_basic_typename (index
->ts
.type
));
4299 if (index
->ts
.type
== BT_REAL
)
4300 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: REAL array index at %L",
4301 &index
->where
) == FAILURE
)
4304 if ((index
->ts
.kind
!= gfc_index_integer_kind
4305 && force_index_integer_kind
)
4306 || index
->ts
.type
!= BT_INTEGER
)
4309 ts
.type
= BT_INTEGER
;
4310 ts
.kind
= gfc_index_integer_kind
;
4312 gfc_convert_type_warn (index
, &ts
, 2, 0);
4318 /* Resolve one part of an array index. */
4321 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
4323 return gfc_resolve_index_1 (index
, check_scalar
, 1);
4326 /* Resolve a dim argument to an intrinsic function. */
4329 gfc_resolve_dim_arg (gfc_expr
*dim
)
4334 if (gfc_resolve_expr (dim
) == FAILURE
)
4339 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
4344 if (dim
->ts
.type
!= BT_INTEGER
)
4346 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
4350 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
4355 ts
.type
= BT_INTEGER
;
4356 ts
.kind
= gfc_index_integer_kind
;
4358 gfc_convert_type_warn (dim
, &ts
, 2, 0);
4364 /* Given an expression that contains array references, update those array
4365 references to point to the right array specifications. While this is
4366 filled in during matching, this information is difficult to save and load
4367 in a module, so we take care of it here.
4369 The idea here is that the original array reference comes from the
4370 base symbol. We traverse the list of reference structures, setting
4371 the stored reference to references. Component references can
4372 provide an additional array specification. */
4375 find_array_spec (gfc_expr
*e
)
4379 gfc_symbol
*derived
;
4382 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
4383 as
= CLASS_DATA (e
->symtree
->n
.sym
)->as
;
4385 as
= e
->symtree
->n
.sym
->as
;
4388 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4393 gfc_internal_error ("find_array_spec(): Missing spec");
4400 if (derived
== NULL
)
4401 derived
= e
->symtree
->n
.sym
->ts
.u
.derived
;
4403 if (derived
->attr
.is_class
)
4404 derived
= derived
->components
->ts
.u
.derived
;
4406 c
= derived
->components
;
4408 for (; c
; c
= c
->next
)
4409 if (c
== ref
->u
.c
.component
)
4411 /* Track the sequence of component references. */
4412 if (c
->ts
.type
== BT_DERIVED
)
4413 derived
= c
->ts
.u
.derived
;
4418 gfc_internal_error ("find_array_spec(): Component not found");
4420 if (c
->attr
.dimension
)
4423 gfc_internal_error ("find_array_spec(): unused as(1)");
4434 gfc_internal_error ("find_array_spec(): unused as(2)");
4438 /* Resolve an array reference. */
4441 resolve_array_ref (gfc_array_ref
*ar
)
4443 int i
, check_scalar
;
4446 for (i
= 0; i
< ar
->dimen
+ ar
->codimen
; i
++)
4448 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4450 /* Do not force gfc_index_integer_kind for the start. We can
4451 do fine with any integer kind. This avoids temporary arrays
4452 created for indexing with a vector. */
4453 if (gfc_resolve_index_1 (ar
->start
[i
], check_scalar
, 0) == FAILURE
)
4455 if (gfc_resolve_index (ar
->end
[i
], check_scalar
) == FAILURE
)
4457 if (gfc_resolve_index (ar
->stride
[i
], check_scalar
) == FAILURE
)
4462 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4466 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4470 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4471 if (e
->expr_type
== EXPR_VARIABLE
4472 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4473 ar
->start
[i
] = gfc_get_parentheses (e
);
4477 gfc_error ("Array index at %L is an array of rank %d",
4478 &ar
->c_where
[i
], e
->rank
);
4482 /* Fill in the upper bound, which may be lower than the
4483 specified one for something like a(2:10:5), which is
4484 identical to a(2:7:5). Only relevant for strides not equal
4486 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4487 && ar
->stride
[i
] != NULL
&& ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
4488 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1L) != 0)
4492 if (gfc_ref_dimen_size (ar
, i
, &size
, &end
) == SUCCESS
)
4494 if (ar
->end
[i
] == NULL
)
4497 gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
4499 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4501 else if (ar
->end
[i
]->ts
.type
== BT_INTEGER
4502 && ar
->end
[i
]->expr_type
== EXPR_CONSTANT
)
4504 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4515 if (ar
->type
== AR_FULL
&& ar
->as
->rank
== 0)
4516 ar
->type
= AR_ELEMENT
;
4518 /* If the reference type is unknown, figure out what kind it is. */
4520 if (ar
->type
== AR_UNKNOWN
)
4522 ar
->type
= AR_ELEMENT
;
4523 for (i
= 0; i
< ar
->dimen
; i
++)
4524 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4525 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
4527 ar
->type
= AR_SECTION
;
4532 if (!ar
->as
->cray_pointee
&& compare_spec_to_ref (ar
) == FAILURE
)
4540 resolve_substring (gfc_ref
*ref
)
4542 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4544 if (ref
->u
.ss
.start
!= NULL
)
4546 if (gfc_resolve_expr (ref
->u
.ss
.start
) == FAILURE
)
4549 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
4551 gfc_error ("Substring start index at %L must be of type INTEGER",
4552 &ref
->u
.ss
.start
->where
);
4556 if (ref
->u
.ss
.start
->rank
!= 0)
4558 gfc_error ("Substring start index at %L must be scalar",
4559 &ref
->u
.ss
.start
->where
);
4563 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
4564 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4565 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4567 gfc_error ("Substring start index at %L is less than one",
4568 &ref
->u
.ss
.start
->where
);
4573 if (ref
->u
.ss
.end
!= NULL
)
4575 if (gfc_resolve_expr (ref
->u
.ss
.end
) == FAILURE
)
4578 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
4580 gfc_error ("Substring end index at %L must be of type INTEGER",
4581 &ref
->u
.ss
.end
->where
);
4585 if (ref
->u
.ss
.end
->rank
!= 0)
4587 gfc_error ("Substring end index at %L must be scalar",
4588 &ref
->u
.ss
.end
->where
);
4592 if (ref
->u
.ss
.length
!= NULL
4593 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
4594 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4595 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4597 gfc_error ("Substring end index at %L exceeds the string length",
4598 &ref
->u
.ss
.start
->where
);
4602 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
4603 gfc_integer_kinds
[k
].huge
) == CMP_GT
4604 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4605 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4607 gfc_error ("Substring end index at %L is too large",
4608 &ref
->u
.ss
.end
->where
);
4617 /* This function supplies missing substring charlens. */
4620 gfc_resolve_substring_charlen (gfc_expr
*e
)
4623 gfc_expr
*start
, *end
;
4625 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
4626 if (char_ref
->type
== REF_SUBSTRING
)
4632 gcc_assert (char_ref
->next
== NULL
);
4636 if (e
->ts
.u
.cl
->length
)
4637 gfc_free_expr (e
->ts
.u
.cl
->length
);
4638 else if (e
->expr_type
== EXPR_VARIABLE
4639 && e
->symtree
->n
.sym
->attr
.dummy
)
4643 e
->ts
.type
= BT_CHARACTER
;
4644 e
->ts
.kind
= gfc_default_character_kind
;
4647 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4649 if (char_ref
->u
.ss
.start
)
4650 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
4652 start
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
4654 if (char_ref
->u
.ss
.end
)
4655 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
4656 else if (e
->expr_type
== EXPR_VARIABLE
)
4657 end
= gfc_copy_expr (e
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4664 /* Length = (end - start +1). */
4665 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
4666 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
,
4667 gfc_get_int_expr (gfc_default_integer_kind
,
4670 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4671 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4673 /* Make sure that the length is simplified. */
4674 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
4675 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4679 /* Resolve subtype references. */
4682 resolve_ref (gfc_expr
*expr
)
4684 int current_part_dimension
, n_components
, seen_part_dimension
;
4687 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4688 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
4690 find_array_spec (expr
);
4694 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4698 if (resolve_array_ref (&ref
->u
.ar
) == FAILURE
)
4706 resolve_substring (ref
);
4710 /* Check constraints on part references. */
4712 current_part_dimension
= 0;
4713 seen_part_dimension
= 0;
4716 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4721 switch (ref
->u
.ar
.type
)
4724 /* Coarray scalar. */
4725 if (ref
->u
.ar
.as
->rank
== 0)
4727 current_part_dimension
= 0;
4732 current_part_dimension
= 1;
4736 current_part_dimension
= 0;
4740 gfc_internal_error ("resolve_ref(): Bad array reference");
4746 if (current_part_dimension
|| seen_part_dimension
)
4749 if (ref
->u
.c
.component
->attr
.pointer
4750 || ref
->u
.c
.component
->attr
.proc_pointer
)
4752 gfc_error ("Component to the right of a part reference "
4753 "with nonzero rank must not have the POINTER "
4754 "attribute at %L", &expr
->where
);
4757 else if (ref
->u
.c
.component
->attr
.allocatable
)
4759 gfc_error ("Component to the right of a part reference "
4760 "with nonzero rank must not have the ALLOCATABLE "
4761 "attribute at %L", &expr
->where
);
4773 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
4774 || ref
->next
== NULL
)
4775 && current_part_dimension
4776 && seen_part_dimension
)
4778 gfc_error ("Two or more part references with nonzero rank must "
4779 "not be specified at %L", &expr
->where
);
4783 if (ref
->type
== REF_COMPONENT
)
4785 if (current_part_dimension
)
4786 seen_part_dimension
= 1;
4788 /* reset to make sure */
4789 current_part_dimension
= 0;
4797 /* Given an expression, determine its shape. This is easier than it sounds.
4798 Leaves the shape array NULL if it is not possible to determine the shape. */
4801 expression_shape (gfc_expr
*e
)
4803 mpz_t array
[GFC_MAX_DIMENSIONS
];
4806 if (e
->rank
== 0 || e
->shape
!= NULL
)
4809 for (i
= 0; i
< e
->rank
; i
++)
4810 if (gfc_array_dimen_size (e
, i
, &array
[i
]) == FAILURE
)
4813 e
->shape
= gfc_get_shape (e
->rank
);
4815 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
4820 for (i
--; i
>= 0; i
--)
4821 mpz_clear (array
[i
]);
4825 /* Given a variable expression node, compute the rank of the expression by
4826 examining the base symbol and any reference structures it may have. */
4829 expression_rank (gfc_expr
*e
)
4834 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4835 could lead to serious confusion... */
4836 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
4840 if (e
->expr_type
== EXPR_ARRAY
)
4842 /* Constructors can have a rank different from one via RESHAPE(). */
4844 if (e
->symtree
== NULL
)
4850 e
->rank
= (e
->symtree
->n
.sym
->as
== NULL
)
4851 ? 0 : e
->symtree
->n
.sym
->as
->rank
;
4857 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4859 if (ref
->type
!= REF_ARRAY
)
4862 if (ref
->u
.ar
.type
== AR_FULL
)
4864 rank
= ref
->u
.ar
.as
->rank
;
4868 if (ref
->u
.ar
.type
== AR_SECTION
)
4870 /* Figure out the rank of the section. */
4872 gfc_internal_error ("expression_rank(): Two array specs");
4874 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
4875 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
4876 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
4886 expression_shape (e
);
4890 /* Resolve a variable expression. */
4893 resolve_variable (gfc_expr
*e
)
4900 if (e
->symtree
== NULL
)
4902 sym
= e
->symtree
->n
.sym
;
4904 /* If this is an associate-name, it may be parsed with an array reference
4905 in error even though the target is scalar. Fail directly in this case. */
4906 if (sym
->assoc
&& !sym
->attr
.dimension
&& e
->ref
&& e
->ref
->type
== REF_ARRAY
)
4909 /* On the other hand, the parser may not have known this is an array;
4910 in this case, we have to add a FULL reference. */
4911 if (sym
->assoc
&& sym
->attr
.dimension
&& !e
->ref
)
4913 e
->ref
= gfc_get_ref ();
4914 e
->ref
->type
= REF_ARRAY
;
4915 e
->ref
->u
.ar
.type
= AR_FULL
;
4916 e
->ref
->u
.ar
.dimen
= 0;
4919 if (e
->ref
&& resolve_ref (e
) == FAILURE
)
4922 if (sym
->attr
.flavor
== FL_PROCEDURE
4923 && (!sym
->attr
.function
4924 || (sym
->attr
.function
&& sym
->result
4925 && sym
->result
->attr
.proc_pointer
4926 && !sym
->result
->attr
.function
)))
4928 e
->ts
.type
= BT_PROCEDURE
;
4929 goto resolve_procedure
;
4932 if (sym
->ts
.type
!= BT_UNKNOWN
)
4933 gfc_variable_attr (e
, &e
->ts
);
4936 /* Must be a simple variable reference. */
4937 if (gfc_set_default_type (sym
, 1, sym
->ns
) == FAILURE
)
4942 if (check_assumed_size_reference (sym
, e
))
4945 /* Deal with forward references to entries during resolve_code, to
4946 satisfy, at least partially, 12.5.2.5. */
4947 if (gfc_current_ns
->entries
4948 && current_entry_id
== sym
->entry_id
4951 && cs_base
->current
->op
!= EXEC_ENTRY
)
4953 gfc_entry_list
*entry
;
4954 gfc_formal_arglist
*formal
;
4958 /* If the symbol is a dummy... */
4959 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
4961 entry
= gfc_current_ns
->entries
;
4964 /* ...test if the symbol is a parameter of previous entries. */
4965 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
4966 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
4968 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
4972 /* If it has not been seen as a dummy, this is an error. */
4975 if (specification_expr
)
4976 gfc_error ("Variable '%s', used in a specification expression"
4977 ", is referenced at %L before the ENTRY statement "
4978 "in which it is a parameter",
4979 sym
->name
, &cs_base
->current
->loc
);
4981 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4982 "statement in which it is a parameter",
4983 sym
->name
, &cs_base
->current
->loc
);
4988 /* Now do the same check on the specification expressions. */
4989 specification_expr
= 1;
4990 if (sym
->ts
.type
== BT_CHARACTER
4991 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
4995 for (n
= 0; n
< sym
->as
->rank
; n
++)
4997 specification_expr
= 1;
4998 if (gfc_resolve_expr (sym
->as
->lower
[n
]) == FAILURE
)
5000 specification_expr
= 1;
5001 if (gfc_resolve_expr (sym
->as
->upper
[n
]) == FAILURE
)
5004 specification_expr
= 0;
5007 /* Update the symbol's entry level. */
5008 sym
->entry_id
= current_entry_id
+ 1;
5011 /* If a symbol has been host_associated mark it. This is used latter,
5012 to identify if aliasing is possible via host association. */
5013 if (sym
->attr
.flavor
== FL_VARIABLE
5014 && gfc_current_ns
->parent
5015 && (gfc_current_ns
->parent
== sym
->ns
5016 || (gfc_current_ns
->parent
->parent
5017 && gfc_current_ns
->parent
->parent
== sym
->ns
)))
5018 sym
->attr
.host_assoc
= 1;
5021 if (t
== SUCCESS
&& resolve_procedure_expression (e
) == FAILURE
)
5024 /* F2008, C617 and C1229. */
5025 if (!inquiry_argument
&& (e
->ts
.type
== BT_CLASS
|| e
->ts
.type
== BT_DERIVED
)
5026 && gfc_is_coindexed (e
))
5028 gfc_ref
*ref
, *ref2
= NULL
;
5030 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5032 if (ref
->type
== REF_COMPONENT
)
5034 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5038 for ( ; ref
; ref
= ref
->next
)
5039 if (ref
->type
== REF_COMPONENT
)
5042 /* Expression itself is not coindexed object. */
5043 if (ref
&& e
->ts
.type
== BT_CLASS
)
5045 gfc_error ("Polymorphic subobject of coindexed object at %L",
5050 /* Expression itself is coindexed object. */
5054 c
= ref2
? ref2
->u
.c
.component
: e
->symtree
->n
.sym
->components
;
5055 for ( ; c
; c
= c
->next
)
5056 if (c
->attr
.allocatable
&& c
->ts
.type
== BT_CLASS
)
5058 gfc_error ("Coindexed object with polymorphic allocatable "
5059 "subcomponent at %L", &e
->where
);
5070 /* Checks to see that the correct symbol has been host associated.
5071 The only situation where this arises is that in which a twice
5072 contained function is parsed after the host association is made.
5073 Therefore, on detecting this, change the symbol in the expression
5074 and convert the array reference into an actual arglist if the old
5075 symbol is a variable. */
5077 check_host_association (gfc_expr
*e
)
5079 gfc_symbol
*sym
, *old_sym
;
5083 gfc_actual_arglist
*arg
, *tail
= NULL
;
5084 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
5086 /* If the expression is the result of substitution in
5087 interface.c(gfc_extend_expr) because there is no way in
5088 which the host association can be wrong. */
5089 if (e
->symtree
== NULL
5090 || e
->symtree
->n
.sym
== NULL
5091 || e
->user_operator
)
5094 old_sym
= e
->symtree
->n
.sym
;
5096 if (gfc_current_ns
->parent
5097 && old_sym
->ns
!= gfc_current_ns
)
5099 /* Use the 'USE' name so that renamed module symbols are
5100 correctly handled. */
5101 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
5103 if (sym
&& old_sym
!= sym
5104 && sym
->ts
.type
== old_sym
->ts
.type
5105 && sym
->attr
.flavor
== FL_PROCEDURE
5106 && sym
->attr
.contained
)
5108 /* Clear the shape, since it might not be valid. */
5109 if (e
->shape
!= NULL
)
5111 for (n
= 0; n
< e
->rank
; n
++)
5112 mpz_clear (e
->shape
[n
]);
5114 gfc_free (e
->shape
);
5117 /* Give the expression the right symtree! */
5118 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
5119 gcc_assert (st
!= NULL
);
5121 if (old_sym
->attr
.flavor
== FL_PROCEDURE
5122 || e
->expr_type
== EXPR_FUNCTION
)
5124 /* Original was function so point to the new symbol, since
5125 the actual argument list is already attached to the
5127 e
->value
.function
.esym
= NULL
;
5132 /* Original was variable so convert array references into
5133 an actual arglist. This does not need any checking now
5134 since gfc_resolve_function will take care of it. */
5135 e
->value
.function
.actual
= NULL
;
5136 e
->expr_type
= EXPR_FUNCTION
;
5139 /* Ambiguity will not arise if the array reference is not
5140 the last reference. */
5141 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5142 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
5145 gcc_assert (ref
->type
== REF_ARRAY
);
5147 /* Grab the start expressions from the array ref and
5148 copy them into actual arguments. */
5149 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
5151 arg
= gfc_get_actual_arglist ();
5152 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
5153 if (e
->value
.function
.actual
== NULL
)
5154 tail
= e
->value
.function
.actual
= arg
;
5162 /* Dump the reference list and set the rank. */
5163 gfc_free_ref_list (e
->ref
);
5165 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
5168 gfc_resolve_expr (e
);
5172 /* This might have changed! */
5173 return e
->expr_type
== EXPR_FUNCTION
;
5178 gfc_resolve_character_operator (gfc_expr
*e
)
5180 gfc_expr
*op1
= e
->value
.op
.op1
;
5181 gfc_expr
*op2
= e
->value
.op
.op2
;
5182 gfc_expr
*e1
= NULL
;
5183 gfc_expr
*e2
= NULL
;
5185 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
5187 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
5188 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
5189 else if (op1
->expr_type
== EXPR_CONSTANT
)
5190 e1
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5191 op1
->value
.character
.length
);
5193 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
5194 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
5195 else if (op2
->expr_type
== EXPR_CONSTANT
)
5196 e2
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5197 op2
->value
.character
.length
);
5199 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5204 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
5205 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
5206 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
5207 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
5208 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
5214 /* Ensure that an character expression has a charlen and, if possible, a
5215 length expression. */
5218 fixup_charlen (gfc_expr
*e
)
5220 /* The cases fall through so that changes in expression type and the need
5221 for multiple fixes are picked up. In all circumstances, a charlen should
5222 be available for the middle end to hang a backend_decl on. */
5223 switch (e
->expr_type
)
5226 gfc_resolve_character_operator (e
);
5229 if (e
->expr_type
== EXPR_ARRAY
)
5230 gfc_resolve_character_array_constructor (e
);
5232 case EXPR_SUBSTRING
:
5233 if (!e
->ts
.u
.cl
&& e
->ref
)
5234 gfc_resolve_substring_charlen (e
);
5238 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5245 /* Update an actual argument to include the passed-object for type-bound
5246 procedures at the right position. */
5248 static gfc_actual_arglist
*
5249 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
5252 gcc_assert (argpos
> 0);
5256 gfc_actual_arglist
* result
;
5258 result
= gfc_get_actual_arglist ();
5262 result
->name
= name
;
5268 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
5270 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
5275 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5278 extract_compcall_passed_object (gfc_expr
* e
)
5282 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5284 if (e
->value
.compcall
.base_object
)
5285 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
5288 po
= gfc_get_expr ();
5289 po
->expr_type
= EXPR_VARIABLE
;
5290 po
->symtree
= e
->symtree
;
5291 po
->ref
= gfc_copy_ref (e
->ref
);
5292 po
->where
= e
->where
;
5295 if (gfc_resolve_expr (po
) == FAILURE
)
5302 /* Update the arglist of an EXPR_COMPCALL expression to include the
5306 update_compcall_arglist (gfc_expr
* e
)
5309 gfc_typebound_proc
* tbp
;
5311 tbp
= e
->value
.compcall
.tbp
;
5316 po
= extract_compcall_passed_object (e
);
5320 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
5326 gcc_assert (tbp
->pass_arg_num
> 0);
5327 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5335 /* Extract the passed object from a PPC call (a copy of it). */
5338 extract_ppc_passed_object (gfc_expr
*e
)
5343 po
= gfc_get_expr ();
5344 po
->expr_type
= EXPR_VARIABLE
;
5345 po
->symtree
= e
->symtree
;
5346 po
->ref
= gfc_copy_ref (e
->ref
);
5347 po
->where
= e
->where
;
5349 /* Remove PPC reference. */
5351 while ((*ref
)->next
)
5352 ref
= &(*ref
)->next
;
5353 gfc_free_ref_list (*ref
);
5356 if (gfc_resolve_expr (po
) == FAILURE
)
5363 /* Update the actual arglist of a procedure pointer component to include the
5367 update_ppc_arglist (gfc_expr
* e
)
5371 gfc_typebound_proc
* tb
;
5373 if (!gfc_is_proc_ptr_comp (e
, &ppc
))
5380 else if (tb
->nopass
)
5383 po
= extract_ppc_passed_object (e
);
5390 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
5395 if (po
->ts
.type
== BT_DERIVED
&& po
->ts
.u
.derived
->attr
.abstract
)
5397 gfc_error ("Base object for procedure-pointer component call at %L is of"
5398 " ABSTRACT type '%s'", &e
->where
, po
->ts
.u
.derived
->name
);
5402 gcc_assert (tb
->pass_arg_num
> 0);
5403 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5411 /* Check that the object a TBP is called on is valid, i.e. it must not be
5412 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5415 check_typebound_baseobject (gfc_expr
* e
)
5418 gfc_try return_value
= FAILURE
;
5420 base
= extract_compcall_passed_object (e
);
5424 gcc_assert (base
->ts
.type
== BT_DERIVED
|| base
->ts
.type
== BT_CLASS
);
5427 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
5429 gfc_error ("Base object for type-bound procedure call at %L is of"
5430 " ABSTRACT type '%s'", &e
->where
, base
->ts
.u
.derived
->name
);
5434 /* F08:C1230. If the procedure called is NOPASS,
5435 the base object must be scalar. */
5436 if (e
->value
.compcall
.tbp
->nopass
&& base
->rank
> 0)
5438 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5439 " be scalar", &e
->where
);
5443 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5446 gfc_error ("Non-scalar base object at %L currently not implemented",
5451 return_value
= SUCCESS
;
5454 gfc_free_expr (base
);
5455 return return_value
;
5459 /* Resolve a call to a type-bound procedure, either function or subroutine,
5460 statically from the data in an EXPR_COMPCALL expression. The adapted
5461 arglist and the target-procedure symtree are returned. */
5464 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
5465 gfc_actual_arglist
** actual
)
5467 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5468 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5470 /* Update the actual arglist for PASS. */
5471 if (update_compcall_arglist (e
) == FAILURE
)
5474 *actual
= e
->value
.compcall
.actual
;
5475 *target
= e
->value
.compcall
.tbp
->u
.specific
;
5477 gfc_free_ref_list (e
->ref
);
5479 e
->value
.compcall
.actual
= NULL
;
5485 /* Get the ultimate declared type from an expression. In addition,
5486 return the last class/derived type reference and the copy of the
5489 get_declared_from_expr (gfc_ref
**class_ref
, gfc_ref
**new_ref
,
5492 gfc_symbol
*declared
;
5499 *new_ref
= gfc_copy_ref (e
->ref
);
5501 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5503 if (ref
->type
!= REF_COMPONENT
)
5506 if (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5507 || ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5509 declared
= ref
->u
.c
.component
->ts
.u
.derived
;
5515 if (declared
== NULL
)
5516 declared
= e
->symtree
->n
.sym
->ts
.u
.derived
;
5522 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5523 which of the specific bindings (if any) matches the arglist and transform
5524 the expression into a call of that binding. */
5527 resolve_typebound_generic_call (gfc_expr
* e
, const char **name
)
5529 gfc_typebound_proc
* genproc
;
5530 const char* genname
;
5532 gfc_symbol
*derived
;
5534 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5535 genname
= e
->value
.compcall
.name
;
5536 genproc
= e
->value
.compcall
.tbp
;
5538 if (!genproc
->is_generic
)
5541 /* Try the bindings on this type and in the inheritance hierarchy. */
5542 for (; genproc
; genproc
= genproc
->overridden
)
5546 gcc_assert (genproc
->is_generic
);
5547 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
5550 gfc_actual_arglist
* args
;
5553 gcc_assert (g
->specific
);
5555 if (g
->specific
->error
)
5558 target
= g
->specific
->u
.specific
->n
.sym
;
5560 /* Get the right arglist by handling PASS/NOPASS. */
5561 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
5562 if (!g
->specific
->nopass
)
5565 po
= extract_compcall_passed_object (e
);
5569 gcc_assert (g
->specific
->pass_arg_num
> 0);
5570 gcc_assert (!g
->specific
->error
);
5571 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
5572 g
->specific
->pass_arg
);
5574 resolve_actual_arglist (args
, target
->attr
.proc
,
5575 is_external_proc (target
) && !target
->formal
);
5577 /* Check if this arglist matches the formal. */
5578 matches
= gfc_arglist_matches_symbol (&args
, target
);
5580 /* Clean up and break out of the loop if we've found it. */
5581 gfc_free_actual_arglist (args
);
5584 e
->value
.compcall
.tbp
= g
->specific
;
5585 genname
= g
->specific_st
->name
;
5586 /* Pass along the name for CLASS methods, where the vtab
5587 procedure pointer component has to be referenced. */
5595 /* Nothing matching found! */
5596 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5597 " '%s' at %L", genname
, &e
->where
);
5601 /* Make sure that we have the right specific instance for the name. */
5602 derived
= get_declared_from_expr (NULL
, NULL
, e
);
5604 st
= gfc_find_typebound_proc (derived
, NULL
, genname
, false, &e
->where
);
5606 e
->value
.compcall
.tbp
= st
->n
.tb
;
5612 /* Resolve a call to a type-bound subroutine. */
5615 resolve_typebound_call (gfc_code
* c
, const char **name
)
5617 gfc_actual_arglist
* newactual
;
5618 gfc_symtree
* target
;
5620 /* Check that's really a SUBROUTINE. */
5621 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
5623 gfc_error ("'%s' at %L should be a SUBROUTINE",
5624 c
->expr1
->value
.compcall
.name
, &c
->loc
);
5628 if (check_typebound_baseobject (c
->expr1
) == FAILURE
)
5631 /* Pass along the name for CLASS methods, where the vtab
5632 procedure pointer component has to be referenced. */
5634 *name
= c
->expr1
->value
.compcall
.name
;
5636 if (resolve_typebound_generic_call (c
->expr1
, name
) == FAILURE
)
5639 /* Transform into an ordinary EXEC_CALL for now. */
5641 if (resolve_typebound_static (c
->expr1
, &target
, &newactual
) == FAILURE
)
5644 c
->ext
.actual
= newactual
;
5645 c
->symtree
= target
;
5646 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
5648 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
5650 gfc_free_expr (c
->expr1
);
5651 c
->expr1
= gfc_get_expr ();
5652 c
->expr1
->expr_type
= EXPR_FUNCTION
;
5653 c
->expr1
->symtree
= target
;
5654 c
->expr1
->where
= c
->loc
;
5656 return resolve_call (c
);
5660 /* Resolve a component-call expression. */
5662 resolve_compcall (gfc_expr
* e
, const char **name
)
5664 gfc_actual_arglist
* newactual
;
5665 gfc_symtree
* target
;
5667 /* Check that's really a FUNCTION. */
5668 if (!e
->value
.compcall
.tbp
->function
)
5670 gfc_error ("'%s' at %L should be a FUNCTION",
5671 e
->value
.compcall
.name
, &e
->where
);
5675 /* These must not be assign-calls! */
5676 gcc_assert (!e
->value
.compcall
.assign
);
5678 if (check_typebound_baseobject (e
) == FAILURE
)
5681 /* Pass along the name for CLASS methods, where the vtab
5682 procedure pointer component has to be referenced. */
5684 *name
= e
->value
.compcall
.name
;
5686 if (resolve_typebound_generic_call (e
, name
) == FAILURE
)
5688 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5690 /* Take the rank from the function's symbol. */
5691 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
5692 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
5694 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5695 arglist to the TBP's binding target. */
5697 if (resolve_typebound_static (e
, &target
, &newactual
) == FAILURE
)
5700 e
->value
.function
.actual
= newactual
;
5701 e
->value
.function
.name
= NULL
;
5702 e
->value
.function
.esym
= target
->n
.sym
;
5703 e
->value
.function
.isym
= NULL
;
5704 e
->symtree
= target
;
5705 e
->ts
= target
->n
.sym
->ts
;
5706 e
->expr_type
= EXPR_FUNCTION
;
5708 /* Resolution is not necessary if this is a class subroutine; this
5709 function only has to identify the specific proc. Resolution of
5710 the call will be done next in resolve_typebound_call. */
5711 return gfc_resolve_expr (e
);
5716 /* Resolve a typebound function, or 'method'. First separate all
5717 the non-CLASS references by calling resolve_compcall directly. */
5720 resolve_typebound_function (gfc_expr
* e
)
5722 gfc_symbol
*declared
;
5733 /* Deal with typebound operators for CLASS objects. */
5734 expr
= e
->value
.compcall
.base_object
;
5735 if (expr
&& expr
->ts
.type
== BT_CLASS
&& e
->value
.compcall
.name
)
5737 /* Since the typebound operators are generic, we have to ensure
5738 that any delays in resolution are corrected and that the vtab
5741 declared
= ts
.u
.derived
;
5742 c
= gfc_find_component (declared
, "_vptr", true, true);
5743 if (c
->ts
.u
.derived
== NULL
)
5744 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5746 if (resolve_compcall (e
, &name
) == FAILURE
)
5749 /* Use the generic name if it is there. */
5750 name
= name
? name
: e
->value
.function
.esym
->name
;
5751 e
->symtree
= expr
->symtree
;
5752 e
->ref
= gfc_copy_ref (expr
->ref
);
5753 gfc_add_vptr_component (e
);
5754 gfc_add_component_ref (e
, name
);
5755 e
->value
.function
.esym
= NULL
;
5760 return resolve_compcall (e
, NULL
);
5762 if (resolve_ref (e
) == FAILURE
)
5765 /* Get the CLASS declared type. */
5766 declared
= get_declared_from_expr (&class_ref
, &new_ref
, e
);
5768 /* Weed out cases of the ultimate component being a derived type. */
5769 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5770 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5772 gfc_free_ref_list (new_ref
);
5773 return resolve_compcall (e
, NULL
);
5776 c
= gfc_find_component (declared
, "_data", true, true);
5777 declared
= c
->ts
.u
.derived
;
5779 /* Treat the call as if it is a typebound procedure, in order to roll
5780 out the correct name for the specific function. */
5781 if (resolve_compcall (e
, &name
) == FAILURE
)
5785 /* Then 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. */
5804 /* Resolve a typebound subroutine, or 'method'. First separate all
5805 the non-CLASS references by calling resolve_typebound_call
5809 resolve_typebound_subroutine (gfc_code
*code
)
5811 gfc_symbol
*declared
;
5820 st
= code
->expr1
->symtree
;
5822 /* Deal with typebound operators for CLASS objects. */
5823 expr
= code
->expr1
->value
.compcall
.base_object
;
5824 if (expr
&& expr
->symtree
->n
.sym
->ts
.type
== BT_CLASS
5825 && code
->expr1
->value
.compcall
.name
)
5827 /* Since the typebound operators are generic, we have to ensure
5828 that any delays in resolution are corrected and that the vtab
5830 ts
= expr
->symtree
->n
.sym
->ts
;
5831 declared
= ts
.u
.derived
;
5832 c
= gfc_find_component (declared
, "_vptr", true, true);
5833 if (c
->ts
.u
.derived
== NULL
)
5834 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5836 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5839 /* Use the generic name if it is there. */
5840 name
= name
? name
: code
->expr1
->value
.function
.esym
->name
;
5841 code
->expr1
->symtree
= expr
->symtree
;
5842 expr
->symtree
->n
.sym
->ts
.u
.derived
= declared
;
5843 gfc_add_vptr_component (code
->expr1
);
5844 gfc_add_component_ref (code
->expr1
, name
);
5845 code
->expr1
->value
.function
.esym
= NULL
;
5850 return resolve_typebound_call (code
, NULL
);
5852 if (resolve_ref (code
->expr1
) == FAILURE
)
5855 /* Get the CLASS declared type. */
5856 get_declared_from_expr (&class_ref
, &new_ref
, code
->expr1
);
5858 /* Weed out cases of the ultimate component being a derived type. */
5859 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5860 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5862 gfc_free_ref_list (new_ref
);
5863 return resolve_typebound_call (code
, NULL
);
5866 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5868 ts
= code
->expr1
->ts
;
5870 /* Then convert the expression to a procedure pointer component call. */
5871 code
->expr1
->value
.function
.esym
= NULL
;
5872 code
->expr1
->symtree
= st
;
5875 code
->expr1
->ref
= new_ref
;
5877 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5878 gfc_add_vptr_component (code
->expr1
);
5879 gfc_add_component_ref (code
->expr1
, name
);
5881 /* Recover the typespec for the expression. This is really only
5882 necessary for generic procedures, where the additional call
5883 to gfc_add_component_ref seems to throw the collection of the
5884 correct typespec. */
5885 code
->expr1
->ts
= ts
;
5890 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5893 resolve_ppc_call (gfc_code
* c
)
5895 gfc_component
*comp
;
5898 b
= gfc_is_proc_ptr_comp (c
->expr1
, &comp
);
5901 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
5902 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5904 if (!comp
->attr
.subroutine
)
5905 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
5907 if (resolve_ref (c
->expr1
) == FAILURE
)
5910 if (update_ppc_arglist (c
->expr1
) == FAILURE
)
5913 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
5915 if (resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
5916 comp
->formal
== NULL
) == FAILURE
)
5919 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
5925 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5928 resolve_expr_ppc (gfc_expr
* e
)
5930 gfc_component
*comp
;
5933 b
= gfc_is_proc_ptr_comp (e
, &comp
);
5936 /* Convert to EXPR_FUNCTION. */
5937 e
->expr_type
= EXPR_FUNCTION
;
5938 e
->value
.function
.isym
= NULL
;
5939 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
5941 if (comp
->as
!= NULL
)
5942 e
->rank
= comp
->as
->rank
;
5944 if (!comp
->attr
.function
)
5945 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
5947 if (resolve_ref (e
) == FAILURE
)
5950 if (resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
5951 comp
->formal
== NULL
) == FAILURE
)
5954 if (update_ppc_arglist (e
) == FAILURE
)
5957 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
5964 gfc_is_expandable_expr (gfc_expr
*e
)
5966 gfc_constructor
*con
;
5968 if (e
->expr_type
== EXPR_ARRAY
)
5970 /* Traverse the constructor looking for variables that are flavor
5971 parameter. Parameters must be expanded since they are fully used at
5973 con
= gfc_constructor_first (e
->value
.constructor
);
5974 for (; con
; con
= gfc_constructor_next (con
))
5976 if (con
->expr
->expr_type
== EXPR_VARIABLE
5977 && con
->expr
->symtree
5978 && (con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
5979 || con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_VARIABLE
))
5981 if (con
->expr
->expr_type
== EXPR_ARRAY
5982 && gfc_is_expandable_expr (con
->expr
))
5990 /* Resolve an expression. That is, make sure that types of operands agree
5991 with their operators, intrinsic operators are converted to function calls
5992 for overloaded types and unresolved function references are resolved. */
5995 gfc_resolve_expr (gfc_expr
*e
)
6003 /* inquiry_argument only applies to variables. */
6004 inquiry_save
= inquiry_argument
;
6005 if (e
->expr_type
!= EXPR_VARIABLE
)
6006 inquiry_argument
= false;
6008 switch (e
->expr_type
)
6011 t
= resolve_operator (e
);
6017 if (check_host_association (e
))
6018 t
= resolve_function (e
);
6021 t
= resolve_variable (e
);
6023 expression_rank (e
);
6026 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
6027 && e
->ref
->type
!= REF_SUBSTRING
)
6028 gfc_resolve_substring_charlen (e
);
6033 t
= resolve_typebound_function (e
);
6036 case EXPR_SUBSTRING
:
6037 t
= resolve_ref (e
);
6046 t
= resolve_expr_ppc (e
);
6051 if (resolve_ref (e
) == FAILURE
)
6054 t
= gfc_resolve_array_constructor (e
);
6055 /* Also try to expand a constructor. */
6058 expression_rank (e
);
6059 if (gfc_is_constant_expr (e
) || gfc_is_expandable_expr (e
))
6060 gfc_expand_constructor (e
, false);
6063 /* This provides the opportunity for the length of constructors with
6064 character valued function elements to propagate the string length
6065 to the expression. */
6066 if (t
== SUCCESS
&& e
->ts
.type
== BT_CHARACTER
)
6068 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6069 here rather then add a duplicate test for it above. */
6070 gfc_expand_constructor (e
, false);
6071 t
= gfc_resolve_character_array_constructor (e
);
6076 case EXPR_STRUCTURE
:
6077 t
= resolve_ref (e
);
6081 t
= resolve_structure_cons (e
, 0);
6085 t
= gfc_simplify_expr (e
, 0);
6089 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6092 if (e
->ts
.type
== BT_CHARACTER
&& t
== SUCCESS
&& !e
->ts
.u
.cl
)
6095 inquiry_argument
= inquiry_save
;
6101 /* Resolve an expression from an iterator. They must be scalar and have
6102 INTEGER or (optionally) REAL type. */
6105 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
6106 const char *name_msgid
)
6108 if (gfc_resolve_expr (expr
) == FAILURE
)
6111 if (expr
->rank
!= 0)
6113 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
6117 if (expr
->ts
.type
!= BT_INTEGER
)
6119 if (expr
->ts
.type
== BT_REAL
)
6122 return gfc_notify_std (GFC_STD_F95_DEL
,
6123 "Deleted feature: %s at %L must be integer",
6124 _(name_msgid
), &expr
->where
);
6127 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
6134 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
6142 /* Resolve the expressions in an iterator structure. If REAL_OK is
6143 false allow only INTEGER type iterators, otherwise allow REAL types. */
6146 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
)
6148 if (gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable")
6152 if (gfc_check_vardef_context (iter
->var
, false, _("iterator variable"))
6156 if (gfc_resolve_iterator_expr (iter
->start
, real_ok
,
6157 "Start expression in DO loop") == FAILURE
)
6160 if (gfc_resolve_iterator_expr (iter
->end
, real_ok
,
6161 "End expression in DO loop") == FAILURE
)
6164 if (gfc_resolve_iterator_expr (iter
->step
, real_ok
,
6165 "Step expression in DO loop") == FAILURE
)
6168 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
6170 if ((iter
->step
->ts
.type
== BT_INTEGER
6171 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
6172 || (iter
->step
->ts
.type
== BT_REAL
6173 && mpfr_sgn (iter
->step
->value
.real
) == 0))
6175 gfc_error ("Step expression in DO loop at %L cannot be zero",
6176 &iter
->step
->where
);
6181 /* Convert start, end, and step to the same type as var. */
6182 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
6183 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
6184 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6186 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
6187 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
6188 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6190 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
6191 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
6192 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 2);
6194 if (iter
->start
->expr_type
== EXPR_CONSTANT
6195 && iter
->end
->expr_type
== EXPR_CONSTANT
6196 && iter
->step
->expr_type
== EXPR_CONSTANT
)
6199 if (iter
->start
->ts
.type
== BT_INTEGER
)
6201 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
6202 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
6206 sgn
= mpfr_sgn (iter
->step
->value
.real
);
6207 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
6209 if ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0))
6210 gfc_warning ("DO loop at %L will be executed zero times",
6211 &iter
->step
->where
);
6218 /* Traversal function for find_forall_index. f == 2 signals that
6219 that variable itself is not to be checked - only the references. */
6222 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
6224 if (expr
->expr_type
!= EXPR_VARIABLE
)
6227 /* A scalar assignment */
6228 if (!expr
->ref
|| *f
== 1)
6230 if (expr
->symtree
->n
.sym
== sym
)
6242 /* Check whether the FORALL index appears in the expression or not.
6243 Returns SUCCESS if SYM is found in EXPR. */
6246 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
6248 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
6255 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6256 to be a scalar INTEGER variable. The subscripts and stride are scalar
6257 INTEGERs, and if stride is a constant it must be nonzero.
6258 Furthermore "A subscript or stride in a forall-triplet-spec shall
6259 not contain a reference to any index-name in the
6260 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6263 resolve_forall_iterators (gfc_forall_iterator
*it
)
6265 gfc_forall_iterator
*iter
, *iter2
;
6267 for (iter
= it
; iter
; iter
= iter
->next
)
6269 if (gfc_resolve_expr (iter
->var
) == SUCCESS
6270 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
6271 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6274 if (gfc_resolve_expr (iter
->start
) == SUCCESS
6275 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
6276 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6277 &iter
->start
->where
);
6278 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
6279 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6281 if (gfc_resolve_expr (iter
->end
) == SUCCESS
6282 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
6283 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6285 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
6286 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6288 if (gfc_resolve_expr (iter
->stride
) == SUCCESS
)
6290 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
6291 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6292 &iter
->stride
->where
, "INTEGER");
6294 if (iter
->stride
->expr_type
== EXPR_CONSTANT
6295 && mpz_cmp_ui(iter
->stride
->value
.integer
, 0) == 0)
6296 gfc_error ("FORALL stride expression at %L cannot be zero",
6297 &iter
->stride
->where
);
6299 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
6300 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 2);
6303 for (iter
= it
; iter
; iter
= iter
->next
)
6304 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
6306 if (find_forall_index (iter2
->start
,
6307 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6308 || find_forall_index (iter2
->end
,
6309 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6310 || find_forall_index (iter2
->stride
,
6311 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
)
6312 gfc_error ("FORALL index '%s' may not appear in triplet "
6313 "specification at %L", iter
->var
->symtree
->name
,
6314 &iter2
->start
->where
);
6319 /* Given a pointer to a symbol that is a derived type, see if it's
6320 inaccessible, i.e. if it's defined in another module and the components are
6321 PRIVATE. The search is recursive if necessary. Returns zero if no
6322 inaccessible components are found, nonzero otherwise. */
6325 derived_inaccessible (gfc_symbol
*sym
)
6329 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
6332 for (c
= sym
->components
; c
; c
= c
->next
)
6334 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
6342 /* Resolve the argument of a deallocate expression. The expression must be
6343 a pointer or a full array. */
6346 resolve_deallocate_expr (gfc_expr
*e
)
6348 symbol_attribute attr
;
6349 int allocatable
, pointer
;
6354 if (gfc_resolve_expr (e
) == FAILURE
)
6357 if (e
->expr_type
!= EXPR_VARIABLE
)
6360 sym
= e
->symtree
->n
.sym
;
6362 if (sym
->ts
.type
== BT_CLASS
)
6364 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6365 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6369 allocatable
= sym
->attr
.allocatable
;
6370 pointer
= sym
->attr
.pointer
;
6372 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6377 if (ref
->u
.ar
.type
!= AR_FULL
)
6382 c
= ref
->u
.c
.component
;
6383 if (c
->ts
.type
== BT_CLASS
)
6385 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6386 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6390 allocatable
= c
->attr
.allocatable
;
6391 pointer
= c
->attr
.pointer
;
6401 attr
= gfc_expr_attr (e
);
6403 if (allocatable
== 0 && attr
.pointer
== 0)
6406 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6412 && gfc_check_vardef_context (e
, true, _("DEALLOCATE object")) == FAILURE
)
6414 if (gfc_check_vardef_context (e
, false, _("DEALLOCATE object")) == FAILURE
)
6417 if (e
->ts
.type
== BT_CLASS
)
6419 /* Only deallocate the DATA component. */
6420 gfc_add_data_component (e
);
6427 /* Returns true if the expression e contains a reference to the symbol sym. */
6429 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
6431 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
6438 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
6440 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
6444 /* Given the expression node e for an allocatable/pointer of derived type to be
6445 allocated, get the expression node to be initialized afterwards (needed for
6446 derived types with default initializers, and derived types with allocatable
6447 components that need nullification.) */
6450 gfc_expr_to_initialize (gfc_expr
*e
)
6456 result
= gfc_copy_expr (e
);
6458 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6459 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
6460 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
6462 ref
->u
.ar
.type
= AR_FULL
;
6464 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
6465 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
6467 result
->rank
= ref
->u
.ar
.dimen
;
6475 /* If the last ref of an expression is an array ref, return a copy of the
6476 expression with that one removed. Otherwise, a copy of the original
6477 expression. This is used for allocate-expressions and pointer assignment
6478 LHS, where there may be an array specification that needs to be stripped
6479 off when using gfc_check_vardef_context. */
6482 remove_last_array_ref (gfc_expr
* e
)
6487 e2
= gfc_copy_expr (e
);
6488 for (r
= &e2
->ref
; *r
; r
= &(*r
)->next
)
6489 if ((*r
)->type
== REF_ARRAY
&& !(*r
)->next
)
6491 gfc_free_ref_list (*r
);
6500 /* Used in resolve_allocate_expr to check that a allocation-object and
6501 a source-expr are conformable. This does not catch all possible
6502 cases; in particular a runtime checking is needed. */
6505 conformable_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
6508 for (tail
= e2
->ref
; tail
&& tail
->next
; tail
= tail
->next
);
6510 /* First compare rank. */
6511 if (tail
&& e1
->rank
!= tail
->u
.ar
.as
->rank
)
6513 gfc_error ("Source-expr at %L must be scalar or have the "
6514 "same rank as the allocate-object at %L",
6515 &e1
->where
, &e2
->where
);
6526 for (i
= 0; i
< e1
->rank
; i
++)
6528 if (tail
->u
.ar
.end
[i
])
6530 mpz_set (s
, tail
->u
.ar
.end
[i
]->value
.integer
);
6531 mpz_sub (s
, s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6532 mpz_add_ui (s
, s
, 1);
6536 mpz_set (s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6539 if (mpz_cmp (e1
->shape
[i
], s
) != 0)
6541 gfc_error ("Source-expr at %L and allocate-object at %L must "
6542 "have the same shape", &e1
->where
, &e2
->where
);
6555 /* Resolve the expression in an ALLOCATE statement, doing the additional
6556 checks to see whether the expression is OK or not. The expression must
6557 have a trailing array reference that gives the size of the array. */
6560 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
)
6562 int i
, pointer
, allocatable
, dimension
, is_abstract
;
6564 symbol_attribute attr
;
6565 gfc_ref
*ref
, *ref2
;
6568 gfc_symbol
*sym
= NULL
;
6573 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6574 checking of coarrays. */
6575 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6576 if (ref
->next
== NULL
)
6579 if (ref
&& ref
->type
== REF_ARRAY
)
6580 ref
->u
.ar
.in_allocate
= true;
6582 if (gfc_resolve_expr (e
) == FAILURE
)
6585 /* Make sure the expression is allocatable or a pointer. If it is
6586 pointer, the next-to-last reference must be a pointer. */
6590 sym
= e
->symtree
->n
.sym
;
6592 /* Check whether ultimate component is abstract and CLASS. */
6595 if (e
->expr_type
!= EXPR_VARIABLE
)
6598 attr
= gfc_expr_attr (e
);
6599 pointer
= attr
.pointer
;
6600 dimension
= attr
.dimension
;
6601 codimension
= attr
.codimension
;
6605 if (sym
->ts
.type
== BT_CLASS
)
6607 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6608 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6609 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
6610 codimension
= CLASS_DATA (sym
)->attr
.codimension
;
6611 is_abstract
= CLASS_DATA (sym
)->attr
.abstract
;
6615 allocatable
= sym
->attr
.allocatable
;
6616 pointer
= sym
->attr
.pointer
;
6617 dimension
= sym
->attr
.dimension
;
6618 codimension
= sym
->attr
.codimension
;
6621 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
6626 if (ref
->next
!= NULL
)
6632 if (gfc_is_coindexed (e
))
6634 gfc_error ("Coindexed allocatable object at %L",
6639 c
= ref
->u
.c
.component
;
6640 if (c
->ts
.type
== BT_CLASS
)
6642 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6643 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6644 dimension
= CLASS_DATA (c
)->attr
.dimension
;
6645 codimension
= CLASS_DATA (c
)->attr
.codimension
;
6646 is_abstract
= CLASS_DATA (c
)->attr
.abstract
;
6650 allocatable
= c
->attr
.allocatable
;
6651 pointer
= c
->attr
.pointer
;
6652 dimension
= c
->attr
.dimension
;
6653 codimension
= c
->attr
.codimension
;
6654 is_abstract
= c
->attr
.abstract
;
6666 if (allocatable
== 0 && pointer
== 0)
6668 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6673 /* Some checks for the SOURCE tag. */
6676 /* Check F03:C631. */
6677 if (!gfc_type_compatible (&e
->ts
, &code
->expr3
->ts
))
6679 gfc_error ("Type of entity at %L is type incompatible with "
6680 "source-expr at %L", &e
->where
, &code
->expr3
->where
);
6684 /* Check F03:C632 and restriction following Note 6.18. */
6685 if (code
->expr3
->rank
> 0
6686 && conformable_arrays (code
->expr3
, e
) == FAILURE
)
6689 /* Check F03:C633. */
6690 if (code
->expr3
->ts
.kind
!= e
->ts
.kind
)
6692 gfc_error ("The allocate-object at %L and the source-expr at %L "
6693 "shall have the same kind type parameter",
6694 &e
->where
, &code
->expr3
->where
);
6699 /* Check F08:C629. */
6700 if (is_abstract
&& code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
6703 gcc_assert (e
->ts
.type
== BT_CLASS
);
6704 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6705 "type-spec or source-expr", sym
->name
, &e
->where
);
6709 /* In the variable definition context checks, gfc_expr_attr is used
6710 on the expression. This is fooled by the array specification
6711 present in e, thus we have to eliminate that one temporarily. */
6712 e2
= remove_last_array_ref (e
);
6714 if (t
== SUCCESS
&& pointer
)
6715 t
= gfc_check_vardef_context (e2
, true, _("ALLOCATE object"));
6717 t
= gfc_check_vardef_context (e2
, false, _("ALLOCATE object"));
6724 /* Set up default initializer if needed. */
6728 if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6729 ts
= code
->ext
.alloc
.ts
;
6733 if (ts
.type
== BT_CLASS
)
6734 ts
= ts
.u
.derived
->components
->ts
;
6736 if (ts
.type
== BT_DERIVED
&& (init_e
= gfc_default_initializer (&ts
)))
6738 gfc_code
*init_st
= gfc_get_code ();
6739 init_st
->loc
= code
->loc
;
6740 init_st
->op
= EXEC_INIT_ASSIGN
;
6741 init_st
->expr1
= gfc_expr_to_initialize (e
);
6742 init_st
->expr2
= init_e
;
6743 init_st
->next
= code
->next
;
6744 code
->next
= init_st
;
6747 else if (code
->expr3
->mold
&& code
->expr3
->ts
.type
== BT_DERIVED
)
6749 /* Default initialization via MOLD (non-polymorphic). */
6750 gfc_expr
*rhs
= gfc_default_initializer (&code
->expr3
->ts
);
6751 gfc_resolve_expr (rhs
);
6752 gfc_free_expr (code
->expr3
);
6756 if (e
->ts
.type
== BT_CLASS
)
6758 /* Make sure the vtab symbol is present when
6759 the module variables are generated. */
6760 gfc_typespec ts
= e
->ts
;
6762 ts
= code
->expr3
->ts
;
6763 else if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6764 ts
= code
->ext
.alloc
.ts
;
6765 gfc_find_derived_vtab (ts
.u
.derived
);
6768 if (pointer
|| (dimension
== 0 && codimension
== 0))
6771 /* Make sure the last reference node is an array specifiction. */
6773 if (!ref2
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
6774 || (dimension
&& ref2
->u
.ar
.dimen
== 0))
6776 gfc_error ("Array specification required in ALLOCATE statement "
6777 "at %L", &e
->where
);
6781 /* Make sure that the array section reference makes sense in the
6782 context of an ALLOCATE specification. */
6786 if (codimension
&& ar
->codimen
== 0)
6788 gfc_error ("Coarray specification required in ALLOCATE statement "
6789 "at %L", &e
->where
);
6793 for (i
= 0; i
< ar
->dimen
; i
++)
6795 if (ref2
->u
.ar
.type
== AR_ELEMENT
)
6798 switch (ar
->dimen_type
[i
])
6804 if (ar
->start
[i
] != NULL
6805 && ar
->end
[i
] != NULL
6806 && ar
->stride
[i
] == NULL
)
6809 /* Fall Through... */
6814 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6820 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6822 sym
= a
->expr
->symtree
->n
.sym
;
6824 /* TODO - check derived type components. */
6825 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
6828 if ((ar
->start
[i
] != NULL
6829 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
6830 || (ar
->end
[i
] != NULL
6831 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
6833 gfc_error ("'%s' must not appear in the array specification at "
6834 "%L in the same ALLOCATE statement where it is "
6835 "itself allocated", sym
->name
, &ar
->where
);
6841 for (i
= ar
->dimen
; i
< ar
->codimen
+ ar
->dimen
; i
++)
6843 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
6844 || ar
->dimen_type
[i
] == DIMEN_RANGE
)
6846 if (i
== (ar
->dimen
+ ar
->codimen
- 1))
6848 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6849 "statement at %L", &e
->where
);
6855 if (ar
->dimen_type
[i
] == DIMEN_STAR
&& i
== (ar
->dimen
+ ar
->codimen
- 1)
6856 && ar
->stride
[i
] == NULL
)
6859 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6864 if (codimension
&& ar
->as
->rank
== 0)
6866 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6867 "at %L", &e
->where
);
6874 gfc_error ("Support for entity at %L with deferred type parameter "
6875 "not yet implemented", &e
->where
);
6885 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
6887 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
6888 gfc_alloc
*a
, *p
, *q
;
6891 errmsg
= code
->expr2
;
6893 /* Check the stat variable. */
6896 gfc_check_vardef_context (stat
, false, _("STAT variable"));
6898 if ((stat
->ts
.type
!= BT_INTEGER
6899 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
6900 || stat
->ref
->type
== REF_COMPONENT
)))
6902 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6903 "variable", &stat
->where
);
6905 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6906 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
6908 gfc_ref
*ref1
, *ref2
;
6911 for (ref1
= p
->expr
->ref
, ref2
= stat
->ref
; ref1
&& ref2
;
6912 ref1
= ref1
->next
, ref2
= ref2
->next
)
6914 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
6916 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
6925 gfc_error ("Stat-variable at %L shall not be %sd within "
6926 "the same %s statement", &stat
->where
, fcn
, fcn
);
6932 /* Check the errmsg variable. */
6936 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6939 gfc_check_vardef_context (errmsg
, false, _("ERRMSG variable"));
6941 if ((errmsg
->ts
.type
!= BT_CHARACTER
6943 && (errmsg
->ref
->type
== REF_ARRAY
6944 || errmsg
->ref
->type
== REF_COMPONENT
)))
6945 || errmsg
->rank
> 0 )
6946 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6947 "variable", &errmsg
->where
);
6949 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6950 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
6952 gfc_ref
*ref1
, *ref2
;
6955 for (ref1
= p
->expr
->ref
, ref2
= errmsg
->ref
; ref1
&& ref2
;
6956 ref1
= ref1
->next
, ref2
= ref2
->next
)
6958 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
6960 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
6969 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6970 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
6976 /* Check that an allocate-object appears only once in the statement.
6977 FIXME: Checking derived types is disabled. */
6978 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6981 if ((pe
->ref
&& pe
->ref
->type
!= REF_COMPONENT
)
6982 && (pe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
))
6984 for (q
= p
->next
; q
; q
= q
->next
)
6987 if ((qe
->ref
&& qe
->ref
->type
!= REF_COMPONENT
)
6988 && (qe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
)
6989 && (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
))
6990 gfc_error ("Allocate-object at %L also appears at %L",
6991 &pe
->where
, &qe
->where
);
6996 if (strcmp (fcn
, "ALLOCATE") == 0)
6998 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6999 resolve_allocate_expr (a
->expr
, code
);
7003 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
7004 resolve_deallocate_expr (a
->expr
);
7009 /************ SELECT CASE resolution subroutines ************/
7011 /* Callback function for our mergesort variant. Determines interval
7012 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7013 op1 > op2. Assumes we're not dealing with the default case.
7014 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7015 There are nine situations to check. */
7018 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
7022 if (op1
->low
== NULL
) /* op1 = (:L) */
7024 /* op2 = (:N), so overlap. */
7026 /* op2 = (M:) or (M:N), L < M */
7027 if (op2
->low
!= NULL
7028 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7031 else if (op1
->high
== NULL
) /* op1 = (K:) */
7033 /* op2 = (M:), so overlap. */
7035 /* op2 = (:N) or (M:N), K > N */
7036 if (op2
->high
!= NULL
7037 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7040 else /* op1 = (K:L) */
7042 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
7043 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7045 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
7046 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7048 else /* op2 = (M:N) */
7052 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7055 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7064 /* Merge-sort a double linked case list, detecting overlap in the
7065 process. LIST is the head of the double linked case list before it
7066 is sorted. Returns the head of the sorted list if we don't see any
7067 overlap, or NULL otherwise. */
7070 check_case_overlap (gfc_case
*list
)
7072 gfc_case
*p
, *q
, *e
, *tail
;
7073 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
7075 /* If the passed list was empty, return immediately. */
7082 /* Loop unconditionally. The only exit from this loop is a return
7083 statement, when we've finished sorting the case list. */
7090 /* Count the number of merges we do in this pass. */
7093 /* Loop while there exists a merge to be done. */
7098 /* Count this merge. */
7101 /* Cut the list in two pieces by stepping INSIZE places
7102 forward in the list, starting from P. */
7105 for (i
= 0; i
< insize
; i
++)
7114 /* Now we have two lists. Merge them! */
7115 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
7117 /* See from which the next case to merge comes from. */
7120 /* P is empty so the next case must come from Q. */
7125 else if (qsize
== 0 || q
== NULL
)
7134 cmp
= compare_cases (p
, q
);
7137 /* The whole case range for P is less than the
7145 /* The whole case range for Q is greater than
7146 the case range for P. */
7153 /* The cases overlap, or they are the same
7154 element in the list. Either way, we must
7155 issue an error and get the next case from P. */
7156 /* FIXME: Sort P and Q by line number. */
7157 gfc_error ("CASE label at %L overlaps with CASE "
7158 "label at %L", &p
->where
, &q
->where
);
7166 /* Add the next element to the merged list. */
7175 /* P has now stepped INSIZE places along, and so has Q. So
7176 they're the same. */
7181 /* If we have done only one merge or none at all, we've
7182 finished sorting the cases. */
7191 /* Otherwise repeat, merging lists twice the size. */
7197 /* Check to see if an expression is suitable for use in a CASE statement.
7198 Makes sure that all case expressions are scalar constants of the same
7199 type. Return FAILURE if anything is wrong. */
7202 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
7204 if (e
== NULL
) return SUCCESS
;
7206 if (e
->ts
.type
!= case_expr
->ts
.type
)
7208 gfc_error ("Expression in CASE statement at %L must be of type %s",
7209 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
7213 /* C805 (R808) For a given case-construct, each case-value shall be of
7214 the same type as case-expr. For character type, length differences
7215 are allowed, but the kind type parameters shall be the same. */
7217 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
7219 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7220 &e
->where
, case_expr
->ts
.kind
);
7224 /* Convert the case value kind to that of case expression kind,
7227 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
7228 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
7232 gfc_error ("Expression in CASE statement at %L must be scalar",
7241 /* Given a completely parsed select statement, we:
7243 - Validate all expressions and code within the SELECT.
7244 - Make sure that the selection expression is not of the wrong type.
7245 - Make sure that no case ranges overlap.
7246 - Eliminate unreachable cases and unreachable code resulting from
7247 removing case labels.
7249 The standard does allow unreachable cases, e.g. CASE (5:3). But
7250 they are a hassle for code generation, and to prevent that, we just
7251 cut them out here. This is not necessary for overlapping cases
7252 because they are illegal and we never even try to generate code.
7254 We have the additional caveat that a SELECT construct could have
7255 been a computed GOTO in the source code. Fortunately we can fairly
7256 easily work around that here: The case_expr for a "real" SELECT CASE
7257 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7258 we have to do is make sure that the case_expr is a scalar integer
7262 resolve_select (gfc_code
*code
)
7265 gfc_expr
*case_expr
;
7266 gfc_case
*cp
, *default_case
, *tail
, *head
;
7267 int seen_unreachable
;
7273 if (code
->expr1
== NULL
)
7275 /* This was actually a computed GOTO statement. */
7276 case_expr
= code
->expr2
;
7277 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
7278 gfc_error ("Selection expression in computed GOTO statement "
7279 "at %L must be a scalar integer expression",
7282 /* Further checking is not necessary because this SELECT was built
7283 by the compiler, so it should always be OK. Just move the
7284 case_expr from expr2 to expr so that we can handle computed
7285 GOTOs as normal SELECTs from here on. */
7286 code
->expr1
= code
->expr2
;
7291 case_expr
= code
->expr1
;
7293 type
= case_expr
->ts
.type
;
7294 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
7296 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7297 &case_expr
->where
, gfc_typename (&case_expr
->ts
));
7299 /* Punt. Going on here just produce more garbage error messages. */
7303 if (case_expr
->rank
!= 0)
7305 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7306 "expression", &case_expr
->where
);
7313 /* Raise a warning if an INTEGER case value exceeds the range of
7314 the case-expr. Later, all expressions will be promoted to the
7315 largest kind of all case-labels. */
7317 if (type
== BT_INTEGER
)
7318 for (body
= code
->block
; body
; body
= body
->block
)
7319 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
7322 && gfc_check_integer_range (cp
->low
->value
.integer
,
7323 case_expr
->ts
.kind
) != ARITH_OK
)
7324 gfc_warning ("Expression in CASE statement at %L is "
7325 "not in the range of %s", &cp
->low
->where
,
7326 gfc_typename (&case_expr
->ts
));
7329 && cp
->low
!= cp
->high
7330 && gfc_check_integer_range (cp
->high
->value
.integer
,
7331 case_expr
->ts
.kind
) != ARITH_OK
)
7332 gfc_warning ("Expression in CASE statement at %L is "
7333 "not in the range of %s", &cp
->high
->where
,
7334 gfc_typename (&case_expr
->ts
));
7337 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7338 of the SELECT CASE expression and its CASE values. Walk the lists
7339 of case values, and if we find a mismatch, promote case_expr to
7340 the appropriate kind. */
7342 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
7344 for (body
= code
->block
; body
; body
= body
->block
)
7346 /* Walk the case label list. */
7347 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
7349 /* Intercept the DEFAULT case. It does not have a kind. */
7350 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7353 /* Unreachable case ranges are discarded, so ignore. */
7354 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7355 && cp
->low
!= cp
->high
7356 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7360 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
7361 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
7363 if (cp
->high
!= NULL
7364 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
7365 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
7370 /* Assume there is no DEFAULT case. */
7371 default_case
= NULL
;
7376 for (body
= code
->block
; body
; body
= body
->block
)
7378 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7380 seen_unreachable
= 0;
7382 /* Walk the case label list, making sure that all case labels
7384 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
7386 /* Count the number of cases in the whole construct. */
7389 /* Intercept the DEFAULT case. */
7390 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7392 if (default_case
!= NULL
)
7394 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7395 "by a second DEFAULT CASE at %L",
7396 &default_case
->where
, &cp
->where
);
7407 /* Deal with single value cases and case ranges. Errors are
7408 issued from the validation function. */
7409 if (validate_case_label_expr (cp
->low
, case_expr
) != SUCCESS
7410 || validate_case_label_expr (cp
->high
, case_expr
) != SUCCESS
)
7416 if (type
== BT_LOGICAL
7417 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
7418 || cp
->low
!= cp
->high
))
7420 gfc_error ("Logical range in CASE statement at %L is not "
7421 "allowed", &cp
->low
->where
);
7426 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
7429 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
7430 if (value
& seen_logical
)
7432 gfc_error ("Constant logical value in CASE statement "
7433 "is repeated at %L",
7438 seen_logical
|= value
;
7441 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7442 && cp
->low
!= cp
->high
7443 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7445 if (gfc_option
.warn_surprising
)
7446 gfc_warning ("Range specification at %L can never "
7447 "be matched", &cp
->where
);
7449 cp
->unreachable
= 1;
7450 seen_unreachable
= 1;
7454 /* If the case range can be matched, it can also overlap with
7455 other cases. To make sure it does not, we put it in a
7456 double linked list here. We sort that with a merge sort
7457 later on to detect any overlapping cases. */
7461 head
->right
= head
->left
= NULL
;
7466 tail
->right
->left
= tail
;
7473 /* It there was a failure in the previous case label, give up
7474 for this case label list. Continue with the next block. */
7478 /* See if any case labels that are unreachable have been seen.
7479 If so, we eliminate them. This is a bit of a kludge because
7480 the case lists for a single case statement (label) is a
7481 single forward linked lists. */
7482 if (seen_unreachable
)
7484 /* Advance until the first case in the list is reachable. */
7485 while (body
->ext
.case_list
!= NULL
7486 && body
->ext
.case_list
->unreachable
)
7488 gfc_case
*n
= body
->ext
.case_list
;
7489 body
->ext
.case_list
= body
->ext
.case_list
->next
;
7491 gfc_free_case_list (n
);
7494 /* Strip all other unreachable cases. */
7495 if (body
->ext
.case_list
)
7497 for (cp
= body
->ext
.case_list
; cp
->next
; cp
= cp
->next
)
7499 if (cp
->next
->unreachable
)
7501 gfc_case
*n
= cp
->next
;
7502 cp
->next
= cp
->next
->next
;
7504 gfc_free_case_list (n
);
7511 /* See if there were overlapping cases. If the check returns NULL,
7512 there was overlap. In that case we don't do anything. If head
7513 is non-NULL, we prepend the DEFAULT case. The sorted list can
7514 then used during code generation for SELECT CASE constructs with
7515 a case expression of a CHARACTER type. */
7518 head
= check_case_overlap (head
);
7520 /* Prepend the default_case if it is there. */
7521 if (head
!= NULL
&& default_case
)
7523 default_case
->left
= NULL
;
7524 default_case
->right
= head
;
7525 head
->left
= default_case
;
7529 /* Eliminate dead blocks that may be the result if we've seen
7530 unreachable case labels for a block. */
7531 for (body
= code
; body
&& body
->block
; body
= body
->block
)
7533 if (body
->block
->ext
.case_list
== NULL
)
7535 /* Cut the unreachable block from the code chain. */
7536 gfc_code
*c
= body
->block
;
7537 body
->block
= c
->block
;
7539 /* Kill the dead block, but not the blocks below it. */
7541 gfc_free_statements (c
);
7545 /* More than two cases is legal but insane for logical selects.
7546 Issue a warning for it. */
7547 if (gfc_option
.warn_surprising
&& type
== BT_LOGICAL
7549 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7554 /* Check if a derived type is extensible. */
7557 gfc_type_is_extensible (gfc_symbol
*sym
)
7559 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
);
7563 /* Resolve an associate name: Resolve target and ensure the type-spec is
7564 correct as well as possibly the array-spec. */
7567 resolve_assoc_var (gfc_symbol
* sym
, bool resolve_target
)
7571 gcc_assert (sym
->assoc
);
7572 gcc_assert (sym
->attr
.flavor
== FL_VARIABLE
);
7574 /* If this is for SELECT TYPE, the target may not yet be set. In that
7575 case, return. Resolution will be called later manually again when
7577 target
= sym
->assoc
->target
;
7580 gcc_assert (!sym
->assoc
->dangling
);
7582 if (resolve_target
&& gfc_resolve_expr (target
) != SUCCESS
)
7585 /* For variable targets, we get some attributes from the target. */
7586 if (target
->expr_type
== EXPR_VARIABLE
)
7590 gcc_assert (target
->symtree
);
7591 tsym
= target
->symtree
->n
.sym
;
7593 sym
->attr
.asynchronous
= tsym
->attr
.asynchronous
;
7594 sym
->attr
.volatile_
= tsym
->attr
.volatile_
;
7596 sym
->attr
.target
= (tsym
->attr
.target
|| tsym
->attr
.pointer
);
7599 /* Get type if this was not already set. Note that it can be
7600 some other type than the target in case this is a SELECT TYPE
7601 selector! So we must not update when the type is already there. */
7602 if (sym
->ts
.type
== BT_UNKNOWN
)
7603 sym
->ts
= target
->ts
;
7604 gcc_assert (sym
->ts
.type
!= BT_UNKNOWN
);
7606 /* See if this is a valid association-to-variable. */
7607 sym
->assoc
->variable
= (target
->expr_type
== EXPR_VARIABLE
7608 && !gfc_has_vector_subscript (target
));
7610 /* Finally resolve if this is an array or not. */
7611 if (sym
->attr
.dimension
&& target
->rank
== 0)
7613 gfc_error ("Associate-name '%s' at %L is used as array",
7614 sym
->name
, &sym
->declared_at
);
7615 sym
->attr
.dimension
= 0;
7618 if (target
->rank
> 0)
7619 sym
->attr
.dimension
= 1;
7621 if (sym
->attr
.dimension
)
7623 sym
->as
= gfc_get_array_spec ();
7624 sym
->as
->rank
= target
->rank
;
7625 sym
->as
->type
= AS_DEFERRED
;
7627 /* Target must not be coindexed, thus the associate-variable
7629 sym
->as
->corank
= 0;
7634 /* Resolve a SELECT TYPE statement. */
7637 resolve_select_type (gfc_code
*code
, gfc_namespace
*old_ns
)
7639 gfc_symbol
*selector_type
;
7640 gfc_code
*body
, *new_st
, *if_st
, *tail
;
7641 gfc_code
*class_is
= NULL
, *default_case
= NULL
;
7644 char name
[GFC_MAX_SYMBOL_LEN
];
7648 ns
= code
->ext
.block
.ns
;
7651 /* Check for F03:C813. */
7652 if (code
->expr1
->ts
.type
!= BT_CLASS
7653 && !(code
->expr2
&& code
->expr2
->ts
.type
== BT_CLASS
))
7655 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7656 "at %L", &code
->loc
);
7662 if (code
->expr1
->symtree
->n
.sym
->attr
.untyped
)
7663 code
->expr1
->symtree
->n
.sym
->ts
= code
->expr2
->ts
;
7664 selector_type
= CLASS_DATA (code
->expr2
)->ts
.u
.derived
;
7667 selector_type
= CLASS_DATA (code
->expr1
)->ts
.u
.derived
;
7669 /* Loop over TYPE IS / CLASS IS cases. */
7670 for (body
= code
->block
; body
; body
= body
->block
)
7672 c
= body
->ext
.case_list
;
7674 /* Check F03:C815. */
7675 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7676 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
7678 gfc_error ("Derived type '%s' at %L must be extensible",
7679 c
->ts
.u
.derived
->name
, &c
->where
);
7684 /* Check F03:C816. */
7685 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7686 && !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
))
7688 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7689 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
7694 /* Intercept the DEFAULT case. */
7695 if (c
->ts
.type
== BT_UNKNOWN
)
7697 /* Check F03:C818. */
7700 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7701 "by a second DEFAULT CASE at %L",
7702 &default_case
->ext
.case_list
->where
, &c
->where
);
7707 default_case
= body
;
7714 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7715 target if present. If there are any EXIT statements referring to the
7716 SELECT TYPE construct, this is no problem because the gfc_code
7717 reference stays the same and EXIT is equally possible from the BLOCK
7718 it is changed to. */
7719 code
->op
= EXEC_BLOCK
;
7722 gfc_association_list
* assoc
;
7724 assoc
= gfc_get_association_list ();
7725 assoc
->st
= code
->expr1
->symtree
;
7726 assoc
->target
= gfc_copy_expr (code
->expr2
);
7727 /* assoc->variable will be set by resolve_assoc_var. */
7729 code
->ext
.block
.assoc
= assoc
;
7730 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
7732 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
7735 code
->ext
.block
.assoc
= NULL
;
7737 /* Add EXEC_SELECT to switch on type. */
7738 new_st
= gfc_get_code ();
7739 new_st
->op
= code
->op
;
7740 new_st
->expr1
= code
->expr1
;
7741 new_st
->expr2
= code
->expr2
;
7742 new_st
->block
= code
->block
;
7743 code
->expr1
= code
->expr2
= NULL
;
7748 ns
->code
->next
= new_st
;
7750 code
->op
= EXEC_SELECT
;
7751 gfc_add_vptr_component (code
->expr1
);
7752 gfc_add_hash_component (code
->expr1
);
7754 /* Loop over TYPE IS / CLASS IS cases. */
7755 for (body
= code
->block
; body
; body
= body
->block
)
7757 c
= body
->ext
.case_list
;
7759 if (c
->ts
.type
== BT_DERIVED
)
7760 c
->low
= c
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
7761 c
->ts
.u
.derived
->hash_value
);
7763 else if (c
->ts
.type
== BT_UNKNOWN
)
7766 /* Associate temporary to selector. This should only be done
7767 when this case is actually true, so build a new ASSOCIATE
7768 that does precisely this here (instead of using the
7771 if (c
->ts
.type
== BT_CLASS
)
7772 sprintf (name
, "__tmp_class_%s", c
->ts
.u
.derived
->name
);
7774 sprintf (name
, "__tmp_type_%s", c
->ts
.u
.derived
->name
);
7775 st
= gfc_find_symtree (ns
->sym_root
, name
);
7776 gcc_assert (st
->n
.sym
->assoc
);
7777 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (code
->expr1
->symtree
);
7778 if (c
->ts
.type
== BT_DERIVED
)
7779 gfc_add_data_component (st
->n
.sym
->assoc
->target
);
7781 new_st
= gfc_get_code ();
7782 new_st
->op
= EXEC_BLOCK
;
7783 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
7784 new_st
->ext
.block
.ns
->code
= body
->next
;
7785 body
->next
= new_st
;
7787 /* Chain in the new list only if it is marked as dangling. Otherwise
7788 there is a CASE label overlap and this is already used. Just ignore,
7789 the error is diagonsed elsewhere. */
7790 if (st
->n
.sym
->assoc
->dangling
)
7792 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
7793 st
->n
.sym
->assoc
->dangling
= 0;
7796 resolve_assoc_var (st
->n
.sym
, false);
7799 /* Take out CLASS IS cases for separate treatment. */
7801 while (body
&& body
->block
)
7803 if (body
->block
->ext
.case_list
->ts
.type
== BT_CLASS
)
7805 /* Add to class_is list. */
7806 if (class_is
== NULL
)
7808 class_is
= body
->block
;
7813 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
7814 tail
->block
= body
->block
;
7817 /* Remove from EXEC_SELECT list. */
7818 body
->block
= body
->block
->block
;
7831 /* Add a default case to hold the CLASS IS cases. */
7832 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
7833 tail
->block
= gfc_get_code ();
7835 tail
->op
= EXEC_SELECT_TYPE
;
7836 tail
->ext
.case_list
= gfc_get_case ();
7837 tail
->ext
.case_list
->ts
.type
= BT_UNKNOWN
;
7839 default_case
= tail
;
7842 /* More than one CLASS IS block? */
7843 if (class_is
->block
)
7847 /* Sort CLASS IS blocks by extension level. */
7851 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
7854 /* F03:C817 (check for doubles). */
7855 if ((*c1
)->ext
.case_list
->ts
.u
.derived
->hash_value
7856 == c2
->ext
.case_list
->ts
.u
.derived
->hash_value
)
7858 gfc_error ("Double CLASS IS block in SELECT TYPE "
7859 "statement at %L", &c2
->ext
.case_list
->where
);
7862 if ((*c1
)->ext
.case_list
->ts
.u
.derived
->attr
.extension
7863 < c2
->ext
.case_list
->ts
.u
.derived
->attr
.extension
)
7866 (*c1
)->block
= c2
->block
;
7876 /* Generate IF chain. */
7877 if_st
= gfc_get_code ();
7878 if_st
->op
= EXEC_IF
;
7880 for (body
= class_is
; body
; body
= body
->block
)
7882 new_st
->block
= gfc_get_code ();
7883 new_st
= new_st
->block
;
7884 new_st
->op
= EXEC_IF
;
7885 /* Set up IF condition: Call _gfortran_is_extension_of. */
7886 new_st
->expr1
= gfc_get_expr ();
7887 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
7888 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
7889 new_st
->expr1
->ts
.kind
= 4;
7890 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
7891 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
7892 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
7893 /* Set up arguments. */
7894 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
7895 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (code
->expr1
->symtree
);
7896 new_st
->expr1
->value
.function
.actual
->expr
->where
= code
->loc
;
7897 gfc_add_vptr_component (new_st
->expr1
->value
.function
.actual
->expr
);
7898 vtab
= gfc_find_derived_vtab (body
->ext
.case_list
->ts
.u
.derived
);
7899 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
7900 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
7901 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
7902 new_st
->next
= body
->next
;
7904 if (default_case
->next
)
7906 new_st
->block
= gfc_get_code ();
7907 new_st
= new_st
->block
;
7908 new_st
->op
= EXEC_IF
;
7909 new_st
->next
= default_case
->next
;
7912 /* Replace CLASS DEFAULT code by the IF chain. */
7913 default_case
->next
= if_st
;
7916 /* Resolve the internal code. This can not be done earlier because
7917 it requires that the sym->assoc of selectors is set already. */
7918 gfc_current_ns
= ns
;
7919 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
7920 gfc_current_ns
= old_ns
;
7922 resolve_select (code
);
7926 /* Resolve a transfer statement. This is making sure that:
7927 -- a derived type being transferred has only non-pointer components
7928 -- a derived type being transferred doesn't have private components, unless
7929 it's being transferred from the module where the type was defined
7930 -- we're not trying to transfer a whole assumed size array. */
7933 resolve_transfer (gfc_code
*code
)
7942 while (exp
!= NULL
&& exp
->expr_type
== EXPR_OP
7943 && exp
->value
.op
.op
== INTRINSIC_PARENTHESES
)
7944 exp
= exp
->value
.op
.op1
;
7946 if (exp
== NULL
|| (exp
->expr_type
!= EXPR_VARIABLE
7947 && exp
->expr_type
!= EXPR_FUNCTION
))
7950 /* If we are reading, the variable will be changed. Note that
7951 code->ext.dt may be NULL if the TRANSFER is related to
7952 an INQUIRE statement -- but in this case, we are not reading, either. */
7953 if (code
->ext
.dt
&& code
->ext
.dt
->dt_io_kind
->value
.iokind
== M_READ
7954 && gfc_check_vardef_context (exp
, false, _("item in READ")) == FAILURE
)
7957 sym
= exp
->symtree
->n
.sym
;
7960 /* Go to actual component transferred. */
7961 for (ref
= exp
->ref
; ref
; ref
= ref
->next
)
7962 if (ref
->type
== REF_COMPONENT
)
7963 ts
= &ref
->u
.c
.component
->ts
;
7965 if (ts
->type
== BT_CLASS
)
7967 /* FIXME: Test for defined input/output. */
7968 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
7969 "it is processed by a defined input/output procedure",
7974 if (ts
->type
== BT_DERIVED
)
7976 /* Check that transferred derived type doesn't contain POINTER
7978 if (ts
->u
.derived
->attr
.pointer_comp
)
7980 gfc_error ("Data transfer element at %L cannot have "
7981 "POINTER components", &code
->loc
);
7985 if (ts
->u
.derived
->attr
.alloc_comp
)
7987 gfc_error ("Data transfer element at %L cannot have "
7988 "ALLOCATABLE components", &code
->loc
);
7992 if (derived_inaccessible (ts
->u
.derived
))
7994 gfc_error ("Data transfer element at %L cannot have "
7995 "PRIVATE components",&code
->loc
);
8000 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
8001 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
8003 gfc_error ("Data transfer element at %L cannot be a full reference to "
8004 "an assumed-size array", &code
->loc
);
8010 /*********** Toplevel code resolution subroutines ***********/
8012 /* Find the set of labels that are reachable from this block. We also
8013 record the last statement in each block. */
8016 find_reachable_labels (gfc_code
*block
)
8023 cs_base
->reachable_labels
= bitmap_obstack_alloc (&labels_obstack
);
8025 /* Collect labels in this block. We don't keep those corresponding
8026 to END {IF|SELECT}, these are checked in resolve_branch by going
8027 up through the code_stack. */
8028 for (c
= block
; c
; c
= c
->next
)
8030 if (c
->here
&& c
->op
!= EXEC_END_BLOCK
)
8031 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
8034 /* Merge with labels from parent block. */
8037 gcc_assert (cs_base
->prev
->reachable_labels
);
8038 bitmap_ior_into (cs_base
->reachable_labels
,
8039 cs_base
->prev
->reachable_labels
);
8045 resolve_sync (gfc_code
*code
)
8047 /* Check imageset. The * case matches expr1 == NULL. */
8050 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
8051 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8052 "INTEGER expression", &code
->expr1
->where
);
8053 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
8054 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
8055 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8056 &code
->expr1
->where
);
8057 else if (code
->expr1
->expr_type
== EXPR_ARRAY
8058 && gfc_simplify_expr (code
->expr1
, 0) == SUCCESS
)
8060 gfc_constructor
*cons
;
8061 cons
= gfc_constructor_first (code
->expr1
->value
.constructor
);
8062 for (; cons
; cons
= gfc_constructor_next (cons
))
8063 if (cons
->expr
->expr_type
== EXPR_CONSTANT
8064 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
8065 gfc_error ("Imageset argument at %L must between 1 and "
8066 "num_images()", &cons
->expr
->where
);
8072 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
8073 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
8074 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8075 &code
->expr2
->where
);
8079 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
8080 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
8081 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8082 &code
->expr3
->where
);
8086 /* Given a branch to a label, see if the branch is conforming.
8087 The code node describes where the branch is located. */
8090 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
8097 /* Step one: is this a valid branching target? */
8099 if (label
->defined
== ST_LABEL_UNKNOWN
)
8101 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
8106 if (label
->defined
!= ST_LABEL_TARGET
)
8108 gfc_error ("Statement at %L is not a valid branch target statement "
8109 "for the branch statement at %L", &label
->where
, &code
->loc
);
8113 /* Step two: make sure this branch is not a branch to itself ;-) */
8115 if (code
->here
== label
)
8117 gfc_warning ("Branch at %L may result in an infinite loop", &code
->loc
);
8121 /* Step three: See if the label is in the same block as the
8122 branching statement. The hard work has been done by setting up
8123 the bitmap reachable_labels. */
8125 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
8127 /* Check now whether there is a CRITICAL construct; if so, check
8128 whether the label is still visible outside of the CRITICAL block,
8129 which is invalid. */
8130 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8131 if (stack
->current
->op
== EXEC_CRITICAL
8132 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
8133 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8134 " at %L", &code
->loc
, &label
->where
);
8139 /* Step four: If we haven't found the label in the bitmap, it may
8140 still be the label of the END of the enclosing block, in which
8141 case we find it by going up the code_stack. */
8143 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8145 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
8147 if (stack
->current
->op
== EXEC_CRITICAL
)
8149 /* Note: A label at END CRITICAL does not leave the CRITICAL
8150 construct as END CRITICAL is still part of it. */
8151 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8152 " at %L", &code
->loc
, &label
->where
);
8159 gcc_assert (stack
->current
->next
->op
== EXEC_END_BLOCK
);
8163 /* The label is not in an enclosing block, so illegal. This was
8164 allowed in Fortran 66, so we allow it as extension. No
8165 further checks are necessary in this case. */
8166 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
8167 "as the GOTO statement at %L", &label
->where
,
8173 /* Check whether EXPR1 has the same shape as EXPR2. */
8176 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
8178 mpz_t shape
[GFC_MAX_DIMENSIONS
];
8179 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
8180 gfc_try result
= FAILURE
;
8183 /* Compare the rank. */
8184 if (expr1
->rank
!= expr2
->rank
)
8187 /* Compare the size of each dimension. */
8188 for (i
=0; i
<expr1
->rank
; i
++)
8190 if (gfc_array_dimen_size (expr1
, i
, &shape
[i
]) == FAILURE
)
8193 if (gfc_array_dimen_size (expr2
, i
, &shape2
[i
]) == FAILURE
)
8196 if (mpz_cmp (shape
[i
], shape2
[i
]))
8200 /* When either of the two expression is an assumed size array, we
8201 ignore the comparison of dimension sizes. */
8206 for (i
--; i
>= 0; i
--)
8208 mpz_clear (shape
[i
]);
8209 mpz_clear (shape2
[i
]);
8215 /* Check whether a WHERE assignment target or a WHERE mask expression
8216 has the same shape as the outmost WHERE mask expression. */
8219 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
8225 cblock
= code
->block
;
8227 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8228 In case of nested WHERE, only the outmost one is stored. */
8229 if (mask
== NULL
) /* outmost WHERE */
8231 else /* inner WHERE */
8238 /* Check if the mask-expr has a consistent shape with the
8239 outmost WHERE mask-expr. */
8240 if (resolve_where_shape (cblock
->expr1
, e
) == FAILURE
)
8241 gfc_error ("WHERE mask at %L has inconsistent shape",
8242 &cblock
->expr1
->where
);
8245 /* the assignment statement of a WHERE statement, or the first
8246 statement in where-body-construct of a WHERE construct */
8247 cnext
= cblock
->next
;
8252 /* WHERE assignment statement */
8255 /* Check shape consistent for WHERE assignment target. */
8256 if (e
&& resolve_where_shape (cnext
->expr1
, e
) == FAILURE
)
8257 gfc_error ("WHERE assignment target at %L has "
8258 "inconsistent shape", &cnext
->expr1
->where
);
8262 case EXEC_ASSIGN_CALL
:
8263 resolve_call (cnext
);
8264 if (!cnext
->resolved_sym
->attr
.elemental
)
8265 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8266 &cnext
->ext
.actual
->expr
->where
);
8269 /* WHERE or WHERE construct is part of a where-body-construct */
8271 resolve_where (cnext
, e
);
8275 gfc_error ("Unsupported statement inside WHERE at %L",
8278 /* the next statement within the same where-body-construct */
8279 cnext
= cnext
->next
;
8281 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8282 cblock
= cblock
->block
;
8287 /* Resolve assignment in FORALL construct.
8288 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8289 FORALL index variables. */
8292 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8296 for (n
= 0; n
< nvar
; n
++)
8298 gfc_symbol
*forall_index
;
8300 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
8302 /* Check whether the assignment target is one of the FORALL index
8304 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
8305 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
8306 gfc_error ("Assignment to a FORALL index variable at %L",
8307 &code
->expr1
->where
);
8310 /* If one of the FORALL index variables doesn't appear in the
8311 assignment variable, then there could be a many-to-one
8312 assignment. Emit a warning rather than an error because the
8313 mask could be resolving this problem. */
8314 if (find_forall_index (code
->expr1
, forall_index
, 0) == FAILURE
)
8315 gfc_warning ("The FORALL with index '%s' is not used on the "
8316 "left side of the assignment at %L and so might "
8317 "cause multiple assignment to this object",
8318 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
8324 /* Resolve WHERE statement in FORALL construct. */
8327 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
8328 gfc_expr
**var_expr
)
8333 cblock
= code
->block
;
8336 /* the assignment statement of a WHERE statement, or the first
8337 statement in where-body-construct of a WHERE construct */
8338 cnext
= cblock
->next
;
8343 /* WHERE assignment statement */
8345 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
8348 /* WHERE operator assignment statement */
8349 case EXEC_ASSIGN_CALL
:
8350 resolve_call (cnext
);
8351 if (!cnext
->resolved_sym
->attr
.elemental
)
8352 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8353 &cnext
->ext
.actual
->expr
->where
);
8356 /* WHERE or WHERE construct is part of a where-body-construct */
8358 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
8362 gfc_error ("Unsupported statement inside WHERE at %L",
8365 /* the next statement within the same where-body-construct */
8366 cnext
= cnext
->next
;
8368 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8369 cblock
= cblock
->block
;
8374 /* Traverse the FORALL body to check whether the following errors exist:
8375 1. For assignment, check if a many-to-one assignment happens.
8376 2. For WHERE statement, check the WHERE body to see if there is any
8377 many-to-one assignment. */
8380 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8384 c
= code
->block
->next
;
8390 case EXEC_POINTER_ASSIGN
:
8391 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
8394 case EXEC_ASSIGN_CALL
:
8398 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8399 there is no need to handle it here. */
8403 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
8408 /* The next statement in the FORALL body. */
8414 /* Counts the number of iterators needed inside a forall construct, including
8415 nested forall constructs. This is used to allocate the needed memory
8416 in gfc_resolve_forall. */
8419 gfc_count_forall_iterators (gfc_code
*code
)
8421 int max_iters
, sub_iters
, current_iters
;
8422 gfc_forall_iterator
*fa
;
8424 gcc_assert(code
->op
== EXEC_FORALL
);
8428 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8431 code
= code
->block
->next
;
8435 if (code
->op
== EXEC_FORALL
)
8437 sub_iters
= gfc_count_forall_iterators (code
);
8438 if (sub_iters
> max_iters
)
8439 max_iters
= sub_iters
;
8444 return current_iters
+ max_iters
;
8448 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8449 gfc_resolve_forall_body to resolve the FORALL body. */
8452 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
8454 static gfc_expr
**var_expr
;
8455 static int total_var
= 0;
8456 static int nvar
= 0;
8458 gfc_forall_iterator
*fa
;
8463 /* Start to resolve a FORALL construct */
8464 if (forall_save
== 0)
8466 /* Count the total number of FORALL index in the nested FORALL
8467 construct in order to allocate the VAR_EXPR with proper size. */
8468 total_var
= gfc_count_forall_iterators (code
);
8470 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8471 var_expr
= (gfc_expr
**) gfc_getmem (total_var
* sizeof (gfc_expr
*));
8474 /* The information about FORALL iterator, including FORALL index start, end
8475 and stride. The FORALL index can not appear in start, end or stride. */
8476 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8478 /* Check if any outer FORALL index name is the same as the current
8480 for (i
= 0; i
< nvar
; i
++)
8482 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
8484 gfc_error ("An outer FORALL construct already has an index "
8485 "with this name %L", &fa
->var
->where
);
8489 /* Record the current FORALL index. */
8490 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
8494 /* No memory leak. */
8495 gcc_assert (nvar
<= total_var
);
8498 /* Resolve the FORALL body. */
8499 gfc_resolve_forall_body (code
, nvar
, var_expr
);
8501 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8502 gfc_resolve_blocks (code
->block
, ns
);
8506 /* Free only the VAR_EXPRs allocated in this frame. */
8507 for (i
= nvar
; i
< tmp
; i
++)
8508 gfc_free_expr (var_expr
[i
]);
8512 /* We are in the outermost FORALL construct. */
8513 gcc_assert (forall_save
== 0);
8515 /* VAR_EXPR is not needed any more. */
8516 gfc_free (var_expr
);
8522 /* Resolve a BLOCK construct statement. */
8525 resolve_block_construct (gfc_code
* code
)
8527 /* Resolve the BLOCK's namespace. */
8528 gfc_resolve (code
->ext
.block
.ns
);
8530 /* For an ASSOCIATE block, the associations (and their targets) are already
8531 resolved during resolve_symbol. */
8535 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8538 static void resolve_code (gfc_code
*, gfc_namespace
*);
8541 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
8545 for (; b
; b
= b
->block
)
8547 t
= gfc_resolve_expr (b
->expr1
);
8548 if (gfc_resolve_expr (b
->expr2
) == FAILURE
)
8554 if (t
== SUCCESS
&& b
->expr1
!= NULL
8555 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
8556 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8563 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
8564 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8569 resolve_branch (b
->label1
, b
);
8573 resolve_block_construct (b
);
8577 case EXEC_SELECT_TYPE
:
8588 case EXEC_OMP_ATOMIC
:
8589 case EXEC_OMP_CRITICAL
:
8591 case EXEC_OMP_MASTER
:
8592 case EXEC_OMP_ORDERED
:
8593 case EXEC_OMP_PARALLEL
:
8594 case EXEC_OMP_PARALLEL_DO
:
8595 case EXEC_OMP_PARALLEL_SECTIONS
:
8596 case EXEC_OMP_PARALLEL_WORKSHARE
:
8597 case EXEC_OMP_SECTIONS
:
8598 case EXEC_OMP_SINGLE
:
8600 case EXEC_OMP_TASKWAIT
:
8601 case EXEC_OMP_WORKSHARE
:
8605 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8608 resolve_code (b
->next
, ns
);
8613 /* Does everything to resolve an ordinary assignment. Returns true
8614 if this is an interface assignment. */
8616 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
8626 if (gfc_extend_assign (code
, ns
) == SUCCESS
)
8630 if (code
->op
== EXEC_ASSIGN_CALL
)
8632 lhs
= code
->ext
.actual
->expr
;
8633 rhsptr
= &code
->ext
.actual
->next
->expr
;
8637 gfc_actual_arglist
* args
;
8638 gfc_typebound_proc
* tbp
;
8640 gcc_assert (code
->op
== EXEC_COMPCALL
);
8642 args
= code
->expr1
->value
.compcall
.actual
;
8644 rhsptr
= &args
->next
->expr
;
8646 tbp
= code
->expr1
->value
.compcall
.tbp
;
8647 gcc_assert (!tbp
->is_generic
);
8650 /* Make a temporary rhs when there is a default initializer
8651 and rhs is the same symbol as the lhs. */
8652 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
8653 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
8654 && gfc_has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
8655 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
8656 *rhsptr
= gfc_get_parentheses (*rhsptr
);
8665 && gfc_notify_std (GFC_STD_GNU
, "Extension: BOZ literal at %L outside "
8666 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8667 &code
->loc
) == FAILURE
)
8670 /* Handle the case of a BOZ literal on the RHS. */
8671 if (rhs
->is_boz
&& lhs
->ts
.type
!= BT_INTEGER
)
8674 if (gfc_option
.warn_surprising
)
8675 gfc_warning ("BOZ literal at %L is bitwise transferred "
8676 "non-integer symbol '%s'", &code
->loc
,
8677 lhs
->symtree
->n
.sym
->name
);
8679 if (!gfc_convert_boz (rhs
, &lhs
->ts
))
8681 if ((rc
= gfc_range_check (rhs
)) != ARITH_OK
)
8683 if (rc
== ARITH_UNDERFLOW
)
8684 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8685 ". This check can be disabled with the option "
8686 "-fno-range-check", &rhs
->where
);
8687 else if (rc
== ARITH_OVERFLOW
)
8688 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8689 ". This check can be disabled with the option "
8690 "-fno-range-check", &rhs
->where
);
8691 else if (rc
== ARITH_NAN
)
8692 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8693 ". This check can be disabled with the option "
8694 "-fno-range-check", &rhs
->where
);
8699 if (lhs
->ts
.type
== BT_CHARACTER
8700 && gfc_option
.warn_character_truncation
)
8702 if (lhs
->ts
.u
.cl
!= NULL
8703 && lhs
->ts
.u
.cl
->length
!= NULL
8704 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8705 llen
= mpz_get_si (lhs
->ts
.u
.cl
->length
->value
.integer
);
8707 if (rhs
->expr_type
== EXPR_CONSTANT
)
8708 rlen
= rhs
->value
.character
.length
;
8710 else if (rhs
->ts
.u
.cl
!= NULL
8711 && rhs
->ts
.u
.cl
->length
!= NULL
8712 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8713 rlen
= mpz_get_si (rhs
->ts
.u
.cl
->length
->value
.integer
);
8715 if (rlen
&& llen
&& rlen
> llen
)
8716 gfc_warning_now ("CHARACTER expression will be truncated "
8717 "in assignment (%d/%d) at %L",
8718 llen
, rlen
, &code
->loc
);
8721 /* Ensure that a vector index expression for the lvalue is evaluated
8722 to a temporary if the lvalue symbol is referenced in it. */
8725 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
8726 if (ref
->type
== REF_ARRAY
)
8728 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
8729 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
8730 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
8731 ref
->u
.ar
.start
[n
]))
8733 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
8737 if (gfc_pure (NULL
))
8739 if (lhs
->ts
.type
== BT_DERIVED
8740 && lhs
->expr_type
== EXPR_VARIABLE
8741 && lhs
->ts
.u
.derived
->attr
.pointer_comp
8742 && rhs
->expr_type
== EXPR_VARIABLE
8743 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
8744 || gfc_is_coindexed (rhs
)))
8747 if (gfc_is_coindexed (rhs
))
8748 gfc_error ("Coindexed expression at %L is assigned to "
8749 "a derived type variable with a POINTER "
8750 "component in a PURE procedure",
8753 gfc_error ("The impure variable at %L is assigned to "
8754 "a derived type variable with a POINTER "
8755 "component in a PURE procedure (12.6)",
8760 /* Fortran 2008, C1283. */
8761 if (gfc_is_coindexed (lhs
))
8763 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8764 "procedure", &rhs
->where
);
8770 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8771 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8772 if (lhs
->ts
.type
== BT_CLASS
)
8774 gfc_error ("Variable must not be polymorphic in assignment at %L",
8779 /* F2008, Section 7.2.1.2. */
8780 if (gfc_is_coindexed (lhs
) && gfc_has_ultimate_allocatable (lhs
))
8782 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8783 "component in assignment at %L", &lhs
->where
);
8787 gfc_check_assign (lhs
, rhs
, 1);
8792 /* Given a block of code, recursively resolve everything pointed to by this
8796 resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
8798 int omp_workshare_save
;
8803 frame
.prev
= cs_base
;
8807 find_reachable_labels (code
);
8809 for (; code
; code
= code
->next
)
8811 frame
.current
= code
;
8812 forall_save
= forall_flag
;
8814 if (code
->op
== EXEC_FORALL
)
8817 gfc_resolve_forall (code
, ns
, forall_save
);
8820 else if (code
->block
)
8822 omp_workshare_save
= -1;
8825 case EXEC_OMP_PARALLEL_WORKSHARE
:
8826 omp_workshare_save
= omp_workshare_flag
;
8827 omp_workshare_flag
= 1;
8828 gfc_resolve_omp_parallel_blocks (code
, ns
);
8830 case EXEC_OMP_PARALLEL
:
8831 case EXEC_OMP_PARALLEL_DO
:
8832 case EXEC_OMP_PARALLEL_SECTIONS
:
8834 omp_workshare_save
= omp_workshare_flag
;
8835 omp_workshare_flag
= 0;
8836 gfc_resolve_omp_parallel_blocks (code
, ns
);
8839 gfc_resolve_omp_do_blocks (code
, ns
);
8841 case EXEC_SELECT_TYPE
:
8842 /* Blocks are handled in resolve_select_type because we have
8843 to transform the SELECT TYPE into ASSOCIATE first. */
8845 case EXEC_OMP_WORKSHARE
:
8846 omp_workshare_save
= omp_workshare_flag
;
8847 omp_workshare_flag
= 1;
8850 gfc_resolve_blocks (code
->block
, ns
);
8854 if (omp_workshare_save
!= -1)
8855 omp_workshare_flag
= omp_workshare_save
;
8859 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
8860 t
= gfc_resolve_expr (code
->expr1
);
8861 forall_flag
= forall_save
;
8863 if (gfc_resolve_expr (code
->expr2
) == FAILURE
)
8866 if (code
->op
== EXEC_ALLOCATE
8867 && gfc_resolve_expr (code
->expr3
) == FAILURE
)
8873 case EXEC_END_BLOCK
:
8877 case EXEC_ERROR_STOP
:
8881 case EXEC_ASSIGN_CALL
:
8886 case EXEC_SYNC_IMAGES
:
8887 case EXEC_SYNC_MEMORY
:
8888 resolve_sync (code
);
8892 /* Keep track of which entry we are up to. */
8893 current_entry_id
= code
->ext
.entry
->id
;
8897 resolve_where (code
, NULL
);
8901 if (code
->expr1
!= NULL
)
8903 if (code
->expr1
->ts
.type
!= BT_INTEGER
)
8904 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8905 "INTEGER variable", &code
->expr1
->where
);
8906 else if (code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
8907 gfc_error ("Variable '%s' has not been assigned a target "
8908 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
8909 &code
->expr1
->where
);
8912 resolve_branch (code
->label1
, code
);
8916 if (code
->expr1
!= NULL
8917 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
8918 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8919 "INTEGER return specifier", &code
->expr1
->where
);
8922 case EXEC_INIT_ASSIGN
:
8923 case EXEC_END_PROCEDURE
:
8930 if (gfc_check_vardef_context (code
->expr1
, false, _("assignment"))
8934 if (resolve_ordinary_assign (code
, ns
))
8936 if (code
->op
== EXEC_COMPCALL
)
8943 case EXEC_LABEL_ASSIGN
:
8944 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
8945 gfc_error ("Label %d referenced at %L is never defined",
8946 code
->label1
->value
, &code
->label1
->where
);
8948 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
8949 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
8950 || code
->expr1
->symtree
->n
.sym
->ts
.kind
8951 != gfc_default_integer_kind
8952 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
8953 gfc_error ("ASSIGN statement at %L requires a scalar "
8954 "default INTEGER variable", &code
->expr1
->where
);
8957 case EXEC_POINTER_ASSIGN
:
8964 /* This is both a variable definition and pointer assignment
8965 context, so check both of them. For rank remapping, a final
8966 array ref may be present on the LHS and fool gfc_expr_attr
8967 used in gfc_check_vardef_context. Remove it. */
8968 e
= remove_last_array_ref (code
->expr1
);
8969 t
= gfc_check_vardef_context (e
, true, _("pointer assignment"));
8971 t
= gfc_check_vardef_context (e
, false, _("pointer assignment"));
8976 gfc_check_pointer_assign (code
->expr1
, code
->expr2
);
8980 case EXEC_ARITHMETIC_IF
:
8982 && code
->expr1
->ts
.type
!= BT_INTEGER
8983 && code
->expr1
->ts
.type
!= BT_REAL
)
8984 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8985 "expression", &code
->expr1
->where
);
8987 resolve_branch (code
->label1
, code
);
8988 resolve_branch (code
->label2
, code
);
8989 resolve_branch (code
->label3
, code
);
8993 if (t
== SUCCESS
&& code
->expr1
!= NULL
8994 && (code
->expr1
->ts
.type
!= BT_LOGICAL
8995 || code
->expr1
->rank
!= 0))
8996 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8997 &code
->expr1
->where
);
9002 resolve_call (code
);
9007 resolve_typebound_subroutine (code
);
9011 resolve_ppc_call (code
);
9015 /* Select is complicated. Also, a SELECT construct could be
9016 a transformed computed GOTO. */
9017 resolve_select (code
);
9020 case EXEC_SELECT_TYPE
:
9021 resolve_select_type (code
, ns
);
9025 resolve_block_construct (code
);
9029 if (code
->ext
.iterator
!= NULL
)
9031 gfc_iterator
*iter
= code
->ext
.iterator
;
9032 if (gfc_resolve_iterator (iter
, true) != FAILURE
)
9033 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
);
9038 if (code
->expr1
== NULL
)
9039 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9041 && (code
->expr1
->rank
!= 0
9042 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
9043 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9044 "a scalar LOGICAL expression", &code
->expr1
->where
);
9049 resolve_allocate_deallocate (code
, "ALLOCATE");
9053 case EXEC_DEALLOCATE
:
9055 resolve_allocate_deallocate (code
, "DEALLOCATE");
9060 if (gfc_resolve_open (code
->ext
.open
) == FAILURE
)
9063 resolve_branch (code
->ext
.open
->err
, code
);
9067 if (gfc_resolve_close (code
->ext
.close
) == FAILURE
)
9070 resolve_branch (code
->ext
.close
->err
, code
);
9073 case EXEC_BACKSPACE
:
9077 if (gfc_resolve_filepos (code
->ext
.filepos
) == FAILURE
)
9080 resolve_branch (code
->ext
.filepos
->err
, code
);
9084 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9087 resolve_branch (code
->ext
.inquire
->err
, code
);
9091 gcc_assert (code
->ext
.inquire
!= NULL
);
9092 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9095 resolve_branch (code
->ext
.inquire
->err
, code
);
9099 if (gfc_resolve_wait (code
->ext
.wait
) == FAILURE
)
9102 resolve_branch (code
->ext
.wait
->err
, code
);
9103 resolve_branch (code
->ext
.wait
->end
, code
);
9104 resolve_branch (code
->ext
.wait
->eor
, code
);
9109 if (gfc_resolve_dt (code
->ext
.dt
, &code
->loc
) == FAILURE
)
9112 resolve_branch (code
->ext
.dt
->err
, code
);
9113 resolve_branch (code
->ext
.dt
->end
, code
);
9114 resolve_branch (code
->ext
.dt
->eor
, code
);
9118 resolve_transfer (code
);
9122 resolve_forall_iterators (code
->ext
.forall_iterator
);
9124 if (code
->expr1
!= NULL
9125 && (code
->expr1
->ts
.type
!= BT_LOGICAL
|| code
->expr1
->rank
))
9126 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9127 "expression", &code
->expr1
->where
);
9130 case EXEC_OMP_ATOMIC
:
9131 case EXEC_OMP_BARRIER
:
9132 case EXEC_OMP_CRITICAL
:
9133 case EXEC_OMP_FLUSH
:
9135 case EXEC_OMP_MASTER
:
9136 case EXEC_OMP_ORDERED
:
9137 case EXEC_OMP_SECTIONS
:
9138 case EXEC_OMP_SINGLE
:
9139 case EXEC_OMP_TASKWAIT
:
9140 case EXEC_OMP_WORKSHARE
:
9141 gfc_resolve_omp_directive (code
, ns
);
9144 case EXEC_OMP_PARALLEL
:
9145 case EXEC_OMP_PARALLEL_DO
:
9146 case EXEC_OMP_PARALLEL_SECTIONS
:
9147 case EXEC_OMP_PARALLEL_WORKSHARE
:
9149 omp_workshare_save
= omp_workshare_flag
;
9150 omp_workshare_flag
= 0;
9151 gfc_resolve_omp_directive (code
, ns
);
9152 omp_workshare_flag
= omp_workshare_save
;
9156 gfc_internal_error ("resolve_code(): Bad statement code");
9160 cs_base
= frame
.prev
;
9164 /* Resolve initial values and make sure they are compatible with
9168 resolve_values (gfc_symbol
*sym
)
9172 if (sym
->value
== NULL
)
9175 if (sym
->value
->expr_type
== EXPR_STRUCTURE
)
9176 t
= resolve_structure_cons (sym
->value
, 1);
9178 t
= gfc_resolve_expr (sym
->value
);
9183 gfc_check_assign_symbol (sym
, sym
->value
);
9187 /* Verify the binding labels for common blocks that are BIND(C). The label
9188 for a BIND(C) common block must be identical in all scoping units in which
9189 the common block is declared. Further, the binding label can not collide
9190 with any other global entity in the program. */
9193 resolve_bind_c_comms (gfc_symtree
*comm_block_tree
)
9195 if (comm_block_tree
->n
.common
->is_bind_c
== 1)
9197 gfc_gsymbol
*binding_label_gsym
;
9198 gfc_gsymbol
*comm_name_gsym
;
9200 /* See if a global symbol exists by the common block's name. It may
9201 be NULL if the common block is use-associated. */
9202 comm_name_gsym
= gfc_find_gsymbol (gfc_gsym_root
,
9203 comm_block_tree
->n
.common
->name
);
9204 if (comm_name_gsym
!= NULL
&& comm_name_gsym
->type
!= GSYM_COMMON
)
9205 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9206 "with the global entity '%s' at %L",
9207 comm_block_tree
->n
.common
->binding_label
,
9208 comm_block_tree
->n
.common
->name
,
9209 &(comm_block_tree
->n
.common
->where
),
9210 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9211 else if (comm_name_gsym
!= NULL
9212 && strcmp (comm_name_gsym
->name
,
9213 comm_block_tree
->n
.common
->name
) == 0)
9215 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9217 if (comm_name_gsym
->binding_label
== NULL
)
9218 /* No binding label for common block stored yet; save this one. */
9219 comm_name_gsym
->binding_label
=
9220 comm_block_tree
->n
.common
->binding_label
;
9222 if (strcmp (comm_name_gsym
->binding_label
,
9223 comm_block_tree
->n
.common
->binding_label
) != 0)
9225 /* Common block names match but binding labels do not. */
9226 gfc_error ("Binding label '%s' for common block '%s' at %L "
9227 "does not match the binding label '%s' for common "
9229 comm_block_tree
->n
.common
->binding_label
,
9230 comm_block_tree
->n
.common
->name
,
9231 &(comm_block_tree
->n
.common
->where
),
9232 comm_name_gsym
->binding_label
,
9233 comm_name_gsym
->name
,
9234 &(comm_name_gsym
->where
));
9239 /* There is no binding label (NAME="") so we have nothing further to
9240 check and nothing to add as a global symbol for the label. */
9241 if (comm_block_tree
->n
.common
->binding_label
[0] == '\0' )
9244 binding_label_gsym
=
9245 gfc_find_gsymbol (gfc_gsym_root
,
9246 comm_block_tree
->n
.common
->binding_label
);
9247 if (binding_label_gsym
== NULL
)
9249 /* Need to make a global symbol for the binding label to prevent
9250 it from colliding with another. */
9251 binding_label_gsym
=
9252 gfc_get_gsymbol (comm_block_tree
->n
.common
->binding_label
);
9253 binding_label_gsym
->sym_name
= comm_block_tree
->n
.common
->name
;
9254 binding_label_gsym
->type
= GSYM_COMMON
;
9258 /* If comm_name_gsym is NULL, the name common block is use
9259 associated and the name could be colliding. */
9260 if (binding_label_gsym
->type
!= GSYM_COMMON
)
9261 gfc_error ("Binding label '%s' for common block '%s' at %L "
9262 "collides with the global entity '%s' at %L",
9263 comm_block_tree
->n
.common
->binding_label
,
9264 comm_block_tree
->n
.common
->name
,
9265 &(comm_block_tree
->n
.common
->where
),
9266 binding_label_gsym
->name
,
9267 &(binding_label_gsym
->where
));
9268 else if (comm_name_gsym
!= NULL
9269 && (strcmp (binding_label_gsym
->name
,
9270 comm_name_gsym
->binding_label
) != 0)
9271 && (strcmp (binding_label_gsym
->sym_name
,
9272 comm_name_gsym
->name
) != 0))
9273 gfc_error ("Binding label '%s' for common block '%s' at %L "
9274 "collides with global entity '%s' at %L",
9275 binding_label_gsym
->name
, binding_label_gsym
->sym_name
,
9276 &(comm_block_tree
->n
.common
->where
),
9277 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9285 /* Verify any BIND(C) derived types in the namespace so we can report errors
9286 for them once, rather than for each variable declared of that type. */
9289 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
9291 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
9292 && derived_sym
->attr
.is_bind_c
== 1)
9293 verify_bind_c_derived_type (derived_sym
);
9299 /* Verify that any binding labels used in a given namespace do not collide
9300 with the names or binding labels of any global symbols. */
9303 gfc_verify_binding_labels (gfc_symbol
*sym
)
9307 if (sym
!= NULL
&& sym
->attr
.is_bind_c
&& sym
->attr
.is_iso_c
== 0
9308 && sym
->attr
.flavor
!= FL_DERIVED
&& sym
->binding_label
[0] != '\0')
9310 gfc_gsymbol
*bind_c_sym
;
9312 bind_c_sym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
9313 if (bind_c_sym
!= NULL
9314 && strcmp (bind_c_sym
->name
, sym
->binding_label
) == 0)
9316 if (sym
->attr
.if_source
== IFSRC_DECL
9317 && (bind_c_sym
->type
!= GSYM_SUBROUTINE
9318 && bind_c_sym
->type
!= GSYM_FUNCTION
)
9319 && ((sym
->attr
.contained
== 1
9320 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0)
9321 || (sym
->attr
.use_assoc
== 1
9322 && (strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0))))
9324 /* Make sure global procedures don't collide with anything. */
9325 gfc_error ("Binding label '%s' at %L collides with the global "
9326 "entity '%s' at %L", sym
->binding_label
,
9327 &(sym
->declared_at
), bind_c_sym
->name
,
9328 &(bind_c_sym
->where
));
9331 else if (sym
->attr
.contained
== 0
9332 && (sym
->attr
.if_source
== IFSRC_IFBODY
9333 && sym
->attr
.flavor
== FL_PROCEDURE
)
9334 && (bind_c_sym
->sym_name
!= NULL
9335 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0))
9337 /* Make sure procedures in interface bodies don't collide. */
9338 gfc_error ("Binding label '%s' in interface body at %L collides "
9339 "with the global entity '%s' at %L",
9341 &(sym
->declared_at
), bind_c_sym
->name
,
9342 &(bind_c_sym
->where
));
9345 else if (sym
->attr
.contained
== 0
9346 && sym
->attr
.if_source
== IFSRC_UNKNOWN
)
9347 if ((sym
->attr
.use_assoc
&& bind_c_sym
->mod_name
9348 && strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0)
9349 || sym
->attr
.use_assoc
== 0)
9351 gfc_error ("Binding label '%s' at %L collides with global "
9352 "entity '%s' at %L", sym
->binding_label
,
9353 &(sym
->declared_at
), bind_c_sym
->name
,
9354 &(bind_c_sym
->where
));
9359 /* Clear the binding label to prevent checking multiple times. */
9360 sym
->binding_label
[0] = '\0';
9362 else if (bind_c_sym
== NULL
)
9364 bind_c_sym
= gfc_get_gsymbol (sym
->binding_label
);
9365 bind_c_sym
->where
= sym
->declared_at
;
9366 bind_c_sym
->sym_name
= sym
->name
;
9368 if (sym
->attr
.use_assoc
== 1)
9369 bind_c_sym
->mod_name
= sym
->module
;
9371 if (sym
->ns
->proc_name
!= NULL
)
9372 bind_c_sym
->mod_name
= sym
->ns
->proc_name
->name
;
9374 if (sym
->attr
.contained
== 0)
9376 if (sym
->attr
.subroutine
)
9377 bind_c_sym
->type
= GSYM_SUBROUTINE
;
9378 else if (sym
->attr
.function
)
9379 bind_c_sym
->type
= GSYM_FUNCTION
;
9387 /* Resolve an index expression. */
9390 resolve_index_expr (gfc_expr
*e
)
9392 if (gfc_resolve_expr (e
) == FAILURE
)
9395 if (gfc_simplify_expr (e
, 0) == FAILURE
)
9398 if (gfc_specification_expr (e
) == FAILURE
)
9405 /* Resolve a charlen structure. */
9408 resolve_charlen (gfc_charlen
*cl
)
9417 specification_expr
= 1;
9419 if (resolve_index_expr (cl
->length
) == FAILURE
)
9421 specification_expr
= 0;
9425 /* "If the character length parameter value evaluates to a negative
9426 value, the length of character entities declared is zero." */
9427 if (cl
->length
&& !gfc_extract_int (cl
->length
, &i
) && i
< 0)
9429 if (gfc_option
.warn_surprising
)
9430 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9431 " the length has been set to zero",
9432 &cl
->length
->where
, i
);
9433 gfc_replace_expr (cl
->length
,
9434 gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0));
9437 /* Check that the character length is not too large. */
9438 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
9439 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
9440 && cl
->length
->ts
.type
== BT_INTEGER
9441 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
9443 gfc_error ("String length at %L is too large", &cl
->length
->where
);
9451 /* Test for non-constant shape arrays. */
9454 is_non_constant_shape_array (gfc_symbol
*sym
)
9460 not_constant
= false;
9461 if (sym
->as
!= NULL
)
9463 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9464 has not been simplified; parameter array references. Do the
9465 simplification now. */
9466 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
9468 e
= sym
->as
->lower
[i
];
9469 if (e
&& (resolve_index_expr (e
) == FAILURE
9470 || !gfc_is_constant_expr (e
)))
9471 not_constant
= true;
9472 e
= sym
->as
->upper
[i
];
9473 if (e
&& (resolve_index_expr (e
) == FAILURE
9474 || !gfc_is_constant_expr (e
)))
9475 not_constant
= true;
9478 return not_constant
;
9481 /* Given a symbol and an initialization expression, add code to initialize
9482 the symbol to the function entry. */
9484 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
9488 gfc_namespace
*ns
= sym
->ns
;
9490 /* Search for the function namespace if this is a contained
9491 function without an explicit result. */
9492 if (sym
->attr
.function
&& sym
== sym
->result
9493 && sym
->name
!= sym
->ns
->proc_name
->name
)
9496 for (;ns
; ns
= ns
->sibling
)
9497 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
9503 gfc_free_expr (init
);
9507 /* Build an l-value expression for the result. */
9508 lval
= gfc_lval_expr_from_sym (sym
);
9510 /* Add the code at scope entry. */
9511 init_st
= gfc_get_code ();
9512 init_st
->next
= ns
->code
;
9515 /* Assign the default initializer to the l-value. */
9516 init_st
->loc
= sym
->declared_at
;
9517 init_st
->op
= EXEC_INIT_ASSIGN
;
9518 init_st
->expr1
= lval
;
9519 init_st
->expr2
= init
;
9522 /* Assign the default initializer to a derived type variable or result. */
9525 apply_default_init (gfc_symbol
*sym
)
9527 gfc_expr
*init
= NULL
;
9529 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9532 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
9533 init
= gfc_default_initializer (&sym
->ts
);
9535 if (init
== NULL
&& sym
->ts
.type
!= BT_CLASS
)
9538 build_init_assign (sym
, init
);
9539 sym
->attr
.referenced
= 1;
9542 /* Build an initializer for a local integer, real, complex, logical, or
9543 character variable, based on the command line flags finit-local-zero,
9544 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9545 null if the symbol should not have a default initialization. */
9547 build_default_init_expr (gfc_symbol
*sym
)
9550 gfc_expr
*init_expr
;
9553 /* These symbols should never have a default initialization. */
9554 if ((sym
->attr
.dimension
&& !gfc_is_compile_time_shape (sym
->as
))
9555 || sym
->attr
.external
9557 || sym
->attr
.pointer
9558 || sym
->attr
.in_equivalence
9559 || sym
->attr
.in_common
9562 || sym
->attr
.cray_pointee
9563 || sym
->attr
.cray_pointer
)
9566 /* Now we'll try to build an initializer expression. */
9567 init_expr
= gfc_get_constant_expr (sym
->ts
.type
, sym
->ts
.kind
,
9570 /* We will only initialize integers, reals, complex, logicals, and
9571 characters, and only if the corresponding command-line flags
9572 were set. Otherwise, we free init_expr and return null. */
9573 switch (sym
->ts
.type
)
9576 if (gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
9577 mpz_set_si (init_expr
->value
.integer
,
9578 gfc_option
.flag_init_integer_value
);
9581 gfc_free_expr (init_expr
);
9587 switch (gfc_option
.flag_init_real
)
9589 case GFC_INIT_REAL_SNAN
:
9590 init_expr
->is_snan
= 1;
9592 case GFC_INIT_REAL_NAN
:
9593 mpfr_set_nan (init_expr
->value
.real
);
9596 case GFC_INIT_REAL_INF
:
9597 mpfr_set_inf (init_expr
->value
.real
, 1);
9600 case GFC_INIT_REAL_NEG_INF
:
9601 mpfr_set_inf (init_expr
->value
.real
, -1);
9604 case GFC_INIT_REAL_ZERO
:
9605 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
9609 gfc_free_expr (init_expr
);
9616 switch (gfc_option
.flag_init_real
)
9618 case GFC_INIT_REAL_SNAN
:
9619 init_expr
->is_snan
= 1;
9621 case GFC_INIT_REAL_NAN
:
9622 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
9623 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
9626 case GFC_INIT_REAL_INF
:
9627 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
9628 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
9631 case GFC_INIT_REAL_NEG_INF
:
9632 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
9633 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
9636 case GFC_INIT_REAL_ZERO
:
9637 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
9641 gfc_free_expr (init_expr
);
9648 if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_FALSE
)
9649 init_expr
->value
.logical
= 0;
9650 else if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_TRUE
)
9651 init_expr
->value
.logical
= 1;
9654 gfc_free_expr (init_expr
);
9660 /* For characters, the length must be constant in order to
9661 create a default initializer. */
9662 if (gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
9663 && sym
->ts
.u
.cl
->length
9664 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9666 char_len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
9667 init_expr
->value
.character
.length
= char_len
;
9668 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
9669 for (i
= 0; i
< char_len
; i
++)
9670 init_expr
->value
.character
.string
[i
]
9671 = (unsigned char) gfc_option
.flag_init_character_value
;
9675 gfc_free_expr (init_expr
);
9681 gfc_free_expr (init_expr
);
9687 /* Add an initialization expression to a local variable. */
9689 apply_default_init_local (gfc_symbol
*sym
)
9691 gfc_expr
*init
= NULL
;
9693 /* The symbol should be a variable or a function return value. */
9694 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9695 || (sym
->attr
.function
&& sym
->result
!= sym
))
9698 /* Try to build the initializer expression. If we can't initialize
9699 this symbol, then init will be NULL. */
9700 init
= build_default_init_expr (sym
);
9704 /* For saved variables, we don't want to add an initializer at
9705 function entry, so we just add a static initializer. */
9706 if (sym
->attr
.save
|| sym
->ns
->save_all
9707 || gfc_option
.flag_max_stack_var_size
== 0)
9709 /* Don't clobber an existing initializer! */
9710 gcc_assert (sym
->value
== NULL
);
9715 build_init_assign (sym
, init
);
9719 /* Resolution of common features of flavors variable and procedure. */
9722 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
9724 /* Constraints on deferred shape variable. */
9725 if (sym
->as
== NULL
|| sym
->as
->type
!= AS_DEFERRED
)
9727 if (sym
->attr
.allocatable
)
9729 if (sym
->attr
.dimension
)
9731 gfc_error ("Allocatable array '%s' at %L must have "
9732 "a deferred shape", sym
->name
, &sym
->declared_at
);
9735 else if (gfc_notify_std (GFC_STD_F2003
, "Scalar object '%s' at %L "
9736 "may not be ALLOCATABLE", sym
->name
,
9737 &sym
->declared_at
) == FAILURE
)
9741 if (sym
->attr
.pointer
&& sym
->attr
.dimension
)
9743 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9744 sym
->name
, &sym
->declared_at
);
9750 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
9751 && !sym
->attr
.dummy
&& sym
->ts
.type
!= BT_CLASS
&& !sym
->assoc
)
9753 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9754 sym
->name
, &sym
->declared_at
);
9759 /* Constraints on polymorphic variables. */
9760 if (sym
->ts
.type
== BT_CLASS
&& !(sym
->result
&& sym
->result
!= sym
))
9763 if (sym
->attr
.class_ok
9764 && !gfc_type_is_extensible (CLASS_DATA (sym
)->ts
.u
.derived
))
9766 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9767 CLASS_DATA (sym
)->ts
.u
.derived
->name
, sym
->name
,
9773 /* Assume that use associated symbols were checked in the module ns.
9774 Class-variables that are associate-names are also something special
9775 and excepted from the test. */
9776 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
&& !sym
->assoc
)
9778 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9779 "or pointer", sym
->name
, &sym
->declared_at
);
9788 /* Additional checks for symbols with flavor variable and derived
9789 type. To be called from resolve_fl_variable. */
9792 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
9794 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
9796 /* Check to see if a derived type is blocked from being host
9797 associated by the presence of another class I symbol in the same
9798 namespace. 14.6.1.3 of the standard and the discussion on
9799 comp.lang.fortran. */
9800 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
9801 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
9804 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
9805 if (s
&& s
->attr
.flavor
!= FL_DERIVED
)
9807 gfc_error ("The type '%s' cannot be host associated at %L "
9808 "because it is blocked by an incompatible object "
9809 "of the same name declared at %L",
9810 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
9816 /* 4th constraint in section 11.3: "If an object of a type for which
9817 component-initialization is specified (R429) appears in the
9818 specification-part of a module and does not have the ALLOCATABLE
9819 or POINTER attribute, the object shall have the SAVE attribute."
9821 The check for initializers is performed with
9822 gfc_has_default_initializer because gfc_default_initializer generates
9823 a hidden default for allocatable components. */
9824 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
9825 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9826 && !sym
->ns
->save_all
&& !sym
->attr
.save
9827 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
9828 && gfc_has_default_initializer (sym
->ts
.u
.derived
)
9829 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Implied SAVE for "
9830 "module variable '%s' at %L, needed due to "
9831 "the default initialization", sym
->name
,
9832 &sym
->declared_at
) == FAILURE
)
9835 /* Assign default initializer. */
9836 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
9837 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
9839 sym
->value
= gfc_default_initializer (&sym
->ts
);
9846 /* Resolve symbols with flavor variable. */
9849 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
9851 int no_init_flag
, automatic_flag
;
9853 const char *auto_save_msg
;
9855 auto_save_msg
= "Automatic object '%s' at %L cannot have the "
9858 if (resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
9861 /* Set this flag to check that variables are parameters of all entries.
9862 This check is effected by the call to gfc_resolve_expr through
9863 is_non_constant_shape_array. */
9864 specification_expr
= 1;
9866 if (sym
->ns
->proc_name
9867 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9868 || sym
->ns
->proc_name
->attr
.is_main_program
)
9869 && !sym
->attr
.use_assoc
9870 && !sym
->attr
.allocatable
9871 && !sym
->attr
.pointer
9872 && is_non_constant_shape_array (sym
))
9874 /* The shape of a main program or module array needs to be
9876 gfc_error ("The module or main program array '%s' at %L must "
9877 "have constant shape", sym
->name
, &sym
->declared_at
);
9878 specification_expr
= 0;
9882 /* Constraints on deferred type parameter. */
9883 if (sym
->ts
.deferred
&& !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
9885 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
9886 "requires either the pointer or allocatable attribute",
9887 sym
->name
, &sym
->declared_at
);
9891 if (sym
->ts
.type
== BT_CHARACTER
)
9893 /* Make sure that character string variables with assumed length are
9895 e
= sym
->ts
.u
.cl
->length
;
9896 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
9897 && !sym
->ts
.deferred
)
9899 gfc_error ("Entity with assumed character length at %L must be a "
9900 "dummy argument or a PARAMETER", &sym
->declared_at
);
9904 if (e
&& sym
->attr
.save
== SAVE_EXPLICIT
&& !gfc_is_constant_expr (e
))
9906 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
9910 if (!gfc_is_constant_expr (e
)
9911 && !(e
->expr_type
== EXPR_VARIABLE
9912 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
9913 && sym
->ns
->proc_name
9914 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9915 || sym
->ns
->proc_name
->attr
.is_main_program
)
9916 && !sym
->attr
.use_assoc
)
9918 gfc_error ("'%s' at %L must have constant character length "
9919 "in this context", sym
->name
, &sym
->declared_at
);
9924 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
9925 apply_default_init_local (sym
); /* Try to apply a default initialization. */
9927 /* Determine if the symbol may not have an initializer. */
9928 no_init_flag
= automatic_flag
= 0;
9929 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
9930 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
9932 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
9933 && is_non_constant_shape_array (sym
))
9935 no_init_flag
= automatic_flag
= 1;
9937 /* Also, they must not have the SAVE attribute.
9938 SAVE_IMPLICIT is checked below. */
9939 if (sym
->attr
.save
== SAVE_EXPLICIT
)
9941 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
9946 /* Ensure that any initializer is simplified. */
9948 gfc_simplify_expr (sym
->value
, 1);
9950 /* Reject illegal initializers. */
9951 if (!sym
->mark
&& sym
->value
)
9953 if (sym
->attr
.allocatable
)
9954 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9955 sym
->name
, &sym
->declared_at
);
9956 else if (sym
->attr
.external
)
9957 gfc_error ("External '%s' at %L cannot have an initializer",
9958 sym
->name
, &sym
->declared_at
);
9959 else if (sym
->attr
.dummy
9960 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
9961 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9962 sym
->name
, &sym
->declared_at
);
9963 else if (sym
->attr
.intrinsic
)
9964 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9965 sym
->name
, &sym
->declared_at
);
9966 else if (sym
->attr
.result
)
9967 gfc_error ("Function result '%s' at %L cannot have an initializer",
9968 sym
->name
, &sym
->declared_at
);
9969 else if (automatic_flag
)
9970 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9971 sym
->name
, &sym
->declared_at
);
9978 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
9979 return resolve_fl_variable_derived (sym
, no_init_flag
);
9985 /* Resolve a procedure. */
9988 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
9990 gfc_formal_arglist
*arg
;
9992 if (sym
->attr
.function
9993 && resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
9996 if (sym
->ts
.type
== BT_CHARACTER
)
9998 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
10000 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
10001 && resolve_charlen (cl
) == FAILURE
)
10004 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
10005 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
10007 gfc_error ("Character-valued statement function '%s' at %L must "
10008 "have constant length", sym
->name
, &sym
->declared_at
);
10013 /* Ensure that derived type for are not of a private type. Internal
10014 module procedures are excluded by 2.2.3.3 - i.e., they are not
10015 externally accessible and can access all the objects accessible in
10017 if (!(sym
->ns
->parent
10018 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
10019 && gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
10021 gfc_interface
*iface
;
10023 for (arg
= sym
->formal
; arg
; arg
= arg
->next
)
10026 && arg
->sym
->ts
.type
== BT_DERIVED
10027 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10028 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
10029 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
10030 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: '%s' is of a "
10031 "PRIVATE type and cannot be a dummy argument"
10032 " of '%s', which is PUBLIC at %L",
10033 arg
->sym
->name
, sym
->name
, &sym
->declared_at
)
10036 /* Stop this message from recurring. */
10037 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10042 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10043 PRIVATE to the containing module. */
10044 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10046 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10049 && arg
->sym
->ts
.type
== BT_DERIVED
10050 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10051 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
10052 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
10053 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10054 "'%s' in PUBLIC interface '%s' at %L "
10055 "takes dummy arguments of '%s' which is "
10056 "PRIVATE", iface
->sym
->name
, sym
->name
,
10057 &iface
->sym
->declared_at
,
10058 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10060 /* Stop this message from recurring. */
10061 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10067 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10068 PRIVATE to the containing module. */
10069 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10071 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10074 && arg
->sym
->ts
.type
== BT_DERIVED
10075 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10076 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
10077 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
10078 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10079 "'%s' in PUBLIC interface '%s' at %L "
10080 "takes dummy arguments of '%s' which is "
10081 "PRIVATE", iface
->sym
->name
, sym
->name
,
10082 &iface
->sym
->declared_at
,
10083 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10085 /* Stop this message from recurring. */
10086 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10093 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
10094 && !sym
->attr
.proc_pointer
)
10096 gfc_error ("Function '%s' at %L cannot have an initializer",
10097 sym
->name
, &sym
->declared_at
);
10101 /* An external symbol may not have an initializer because it is taken to be
10102 a procedure. Exception: Procedure Pointers. */
10103 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
10105 gfc_error ("External object '%s' at %L may not have an initializer",
10106 sym
->name
, &sym
->declared_at
);
10110 /* An elemental function is required to return a scalar 12.7.1 */
10111 if (sym
->attr
.elemental
&& sym
->attr
.function
&& sym
->as
)
10113 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10114 "result", sym
->name
, &sym
->declared_at
);
10115 /* Reset so that the error only occurs once. */
10116 sym
->attr
.elemental
= 0;
10120 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10121 char-len-param shall not be array-valued, pointer-valued, recursive
10122 or pure. ....snip... A character value of * may only be used in the
10123 following ways: (i) Dummy arg of procedure - dummy associates with
10124 actual length; (ii) To declare a named constant; or (iii) External
10125 function - but length must be declared in calling scoping unit. */
10126 if (sym
->attr
.function
10127 && sym
->ts
.type
== BT_CHARACTER
10128 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
10130 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
10131 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
10133 if (sym
->as
&& sym
->as
->rank
)
10134 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10135 "array-valued", sym
->name
, &sym
->declared_at
);
10137 if (sym
->attr
.pointer
)
10138 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10139 "pointer-valued", sym
->name
, &sym
->declared_at
);
10141 if (sym
->attr
.pure
)
10142 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10143 "pure", sym
->name
, &sym
->declared_at
);
10145 if (sym
->attr
.recursive
)
10146 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10147 "recursive", sym
->name
, &sym
->declared_at
);
10152 /* Appendix B.2 of the standard. Contained functions give an
10153 error anyway. Fixed-form is likely to be F77/legacy. */
10154 if (!sym
->attr
.contained
&& gfc_current_form
!= FORM_FIXED
)
10155 gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
10156 "CHARACTER(*) function '%s' at %L",
10157 sym
->name
, &sym
->declared_at
);
10160 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
10162 gfc_formal_arglist
*curr_arg
;
10163 int has_non_interop_arg
= 0;
10165 if (verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
10166 sym
->common_block
) == FAILURE
)
10168 /* Clear these to prevent looking at them again if there was an
10170 sym
->attr
.is_bind_c
= 0;
10171 sym
->attr
.is_c_interop
= 0;
10172 sym
->ts
.is_c_interop
= 0;
10176 /* So far, no errors have been found. */
10177 sym
->attr
.is_c_interop
= 1;
10178 sym
->ts
.is_c_interop
= 1;
10181 curr_arg
= sym
->formal
;
10182 while (curr_arg
!= NULL
)
10184 /* Skip implicitly typed dummy args here. */
10185 if (curr_arg
->sym
->attr
.implicit_type
== 0)
10186 if (verify_c_interop_param (curr_arg
->sym
) == FAILURE
)
10187 /* If something is found to fail, record the fact so we
10188 can mark the symbol for the procedure as not being
10189 BIND(C) to try and prevent multiple errors being
10191 has_non_interop_arg
= 1;
10193 curr_arg
= curr_arg
->next
;
10196 /* See if any of the arguments were not interoperable and if so, clear
10197 the procedure symbol to prevent duplicate error messages. */
10198 if (has_non_interop_arg
!= 0)
10200 sym
->attr
.is_c_interop
= 0;
10201 sym
->ts
.is_c_interop
= 0;
10202 sym
->attr
.is_bind_c
= 0;
10206 if (!sym
->attr
.proc_pointer
)
10208 if (sym
->attr
.save
== SAVE_EXPLICIT
)
10210 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10211 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10214 if (sym
->attr
.intent
)
10216 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10217 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10220 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
10222 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10223 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10226 if (sym
->attr
.external
&& sym
->attr
.function
10227 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
10228 || sym
->attr
.contained
))
10230 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10231 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10234 if (strcmp ("ppr@", sym
->name
) == 0)
10236 gfc_error ("Procedure pointer result '%s' at %L "
10237 "is missing the pointer attribute",
10238 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
10247 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10248 been defined and we now know their defined arguments, check that they fulfill
10249 the requirements of the standard for procedures used as finalizers. */
10252 gfc_resolve_finalizers (gfc_symbol
* derived
)
10254 gfc_finalizer
* list
;
10255 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
10256 gfc_try result
= SUCCESS
;
10257 bool seen_scalar
= false;
10259 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->finalizers
)
10262 /* Walk over the list of finalizer-procedures, check them, and if any one
10263 does not fit in with the standard's definition, print an error and remove
10264 it from the list. */
10265 prev_link
= &derived
->f2k_derived
->finalizers
;
10266 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
10272 /* Skip this finalizer if we already resolved it. */
10273 if (list
->proc_tree
)
10275 prev_link
= &(list
->next
);
10279 /* Check this exists and is a SUBROUTINE. */
10280 if (!list
->proc_sym
->attr
.subroutine
)
10282 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10283 list
->proc_sym
->name
, &list
->where
);
10287 /* We should have exactly one argument. */
10288 if (!list
->proc_sym
->formal
|| list
->proc_sym
->formal
->next
)
10290 gfc_error ("FINAL procedure at %L must have exactly one argument",
10294 arg
= list
->proc_sym
->formal
->sym
;
10296 /* This argument must be of our type. */
10297 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
10299 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10300 &arg
->declared_at
, derived
->name
);
10304 /* It must neither be a pointer nor allocatable nor optional. */
10305 if (arg
->attr
.pointer
)
10307 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10308 &arg
->declared_at
);
10311 if (arg
->attr
.allocatable
)
10313 gfc_error ("Argument of FINAL procedure at %L must not be"
10314 " ALLOCATABLE", &arg
->declared_at
);
10317 if (arg
->attr
.optional
)
10319 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10320 &arg
->declared_at
);
10324 /* It must not be INTENT(OUT). */
10325 if (arg
->attr
.intent
== INTENT_OUT
)
10327 gfc_error ("Argument of FINAL procedure at %L must not be"
10328 " INTENT(OUT)", &arg
->declared_at
);
10332 /* Warn if the procedure is non-scalar and not assumed shape. */
10333 if (gfc_option
.warn_surprising
&& arg
->as
&& arg
->as
->rank
> 0
10334 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
10335 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10336 " shape argument", &arg
->declared_at
);
10338 /* Check that it does not match in kind and rank with a FINAL procedure
10339 defined earlier. To really loop over the *earlier* declarations,
10340 we need to walk the tail of the list as new ones were pushed at the
10342 /* TODO: Handle kind parameters once they are implemented. */
10343 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
10344 for (i
= list
->next
; i
; i
= i
->next
)
10346 /* Argument list might be empty; that is an error signalled earlier,
10347 but we nevertheless continued resolving. */
10348 if (i
->proc_sym
->formal
)
10350 gfc_symbol
* i_arg
= i
->proc_sym
->formal
->sym
;
10351 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
10352 if (i_rank
== my_rank
)
10354 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10355 " rank (%d) as '%s'",
10356 list
->proc_sym
->name
, &list
->where
, my_rank
,
10357 i
->proc_sym
->name
);
10363 /* Is this the/a scalar finalizer procedure? */
10364 if (!arg
->as
|| arg
->as
->rank
== 0)
10365 seen_scalar
= true;
10367 /* Find the symtree for this procedure. */
10368 gcc_assert (!list
->proc_tree
);
10369 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
10371 prev_link
= &list
->next
;
10374 /* Remove wrong nodes immediately from the list so we don't risk any
10375 troubles in the future when they might fail later expectations. */
10379 *prev_link
= list
->next
;
10380 gfc_free_finalizer (i
);
10383 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10384 were nodes in the list, must have been for arrays. It is surely a good
10385 idea to have a scalar version there if there's something to finalize. */
10386 if (gfc_option
.warn_surprising
&& result
== SUCCESS
&& !seen_scalar
)
10387 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10388 " defined at %L, suggest also scalar one",
10389 derived
->name
, &derived
->declared_at
);
10391 /* TODO: Remove this error when finalization is finished. */
10392 gfc_error ("Finalization at %L is not yet implemented",
10393 &derived
->declared_at
);
10399 /* Check that it is ok for the typebound procedure proc to override the
10403 check_typebound_override (gfc_symtree
* proc
, gfc_symtree
* old
)
10406 const gfc_symbol
* proc_target
;
10407 const gfc_symbol
* old_target
;
10408 unsigned proc_pass_arg
, old_pass_arg
, argpos
;
10409 gfc_formal_arglist
* proc_formal
;
10410 gfc_formal_arglist
* old_formal
;
10412 /* This procedure should only be called for non-GENERIC proc. */
10413 gcc_assert (!proc
->n
.tb
->is_generic
);
10415 /* If the overwritten procedure is GENERIC, this is an error. */
10416 if (old
->n
.tb
->is_generic
)
10418 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10419 old
->name
, &proc
->n
.tb
->where
);
10423 where
= proc
->n
.tb
->where
;
10424 proc_target
= proc
->n
.tb
->u
.specific
->n
.sym
;
10425 old_target
= old
->n
.tb
->u
.specific
->n
.sym
;
10427 /* Check that overridden binding is not NON_OVERRIDABLE. */
10428 if (old
->n
.tb
->non_overridable
)
10430 gfc_error ("'%s' at %L overrides a procedure binding declared"
10431 " NON_OVERRIDABLE", proc
->name
, &where
);
10435 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10436 if (!old
->n
.tb
->deferred
&& proc
->n
.tb
->deferred
)
10438 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10439 " non-DEFERRED binding", proc
->name
, &where
);
10443 /* If the overridden binding is PURE, the overriding must be, too. */
10444 if (old_target
->attr
.pure
&& !proc_target
->attr
.pure
)
10446 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10447 proc
->name
, &where
);
10451 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10452 is not, the overriding must not be either. */
10453 if (old_target
->attr
.elemental
&& !proc_target
->attr
.elemental
)
10455 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10456 " ELEMENTAL", proc
->name
, &where
);
10459 if (!old_target
->attr
.elemental
&& proc_target
->attr
.elemental
)
10461 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10462 " be ELEMENTAL, either", proc
->name
, &where
);
10466 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10468 if (old_target
->attr
.subroutine
&& !proc_target
->attr
.subroutine
)
10470 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10471 " SUBROUTINE", proc
->name
, &where
);
10475 /* If the overridden binding is a FUNCTION, the overriding must also be a
10476 FUNCTION and have the same characteristics. */
10477 if (old_target
->attr
.function
)
10479 if (!proc_target
->attr
.function
)
10481 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10482 " FUNCTION", proc
->name
, &where
);
10486 /* FIXME: Do more comprehensive checking (including, for instance, the
10487 rank and array-shape). */
10488 gcc_assert (proc_target
->result
&& old_target
->result
);
10489 if (!gfc_compare_types (&proc_target
->result
->ts
,
10490 &old_target
->result
->ts
))
10492 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10493 " matching result types", proc
->name
, &where
);
10498 /* If the overridden binding is PUBLIC, the overriding one must not be
10500 if (old
->n
.tb
->access
== ACCESS_PUBLIC
10501 && proc
->n
.tb
->access
== ACCESS_PRIVATE
)
10503 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10504 " PRIVATE", proc
->name
, &where
);
10508 /* Compare the formal argument lists of both procedures. This is also abused
10509 to find the position of the passed-object dummy arguments of both
10510 bindings as at least the overridden one might not yet be resolved and we
10511 need those positions in the check below. */
10512 proc_pass_arg
= old_pass_arg
= 0;
10513 if (!proc
->n
.tb
->nopass
&& !proc
->n
.tb
->pass_arg
)
10515 if (!old
->n
.tb
->nopass
&& !old
->n
.tb
->pass_arg
)
10518 for (proc_formal
= proc_target
->formal
, old_formal
= old_target
->formal
;
10519 proc_formal
&& old_formal
;
10520 proc_formal
= proc_formal
->next
, old_formal
= old_formal
->next
)
10522 if (proc
->n
.tb
->pass_arg
10523 && !strcmp (proc
->n
.tb
->pass_arg
, proc_formal
->sym
->name
))
10524 proc_pass_arg
= argpos
;
10525 if (old
->n
.tb
->pass_arg
10526 && !strcmp (old
->n
.tb
->pass_arg
, old_formal
->sym
->name
))
10527 old_pass_arg
= argpos
;
10529 /* Check that the names correspond. */
10530 if (strcmp (proc_formal
->sym
->name
, old_formal
->sym
->name
))
10532 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10533 " to match the corresponding argument of the overridden"
10534 " procedure", proc_formal
->sym
->name
, proc
->name
, &where
,
10535 old_formal
->sym
->name
);
10539 /* Check that the types correspond if neither is the passed-object
10541 /* FIXME: Do more comprehensive testing here. */
10542 if (proc_pass_arg
!= argpos
&& old_pass_arg
!= argpos
10543 && !gfc_compare_types (&proc_formal
->sym
->ts
, &old_formal
->sym
->ts
))
10545 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10546 "in respect to the overridden procedure",
10547 proc_formal
->sym
->name
, proc
->name
, &where
);
10553 if (proc_formal
|| old_formal
)
10555 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10556 " the overridden procedure", proc
->name
, &where
);
10560 /* If the overridden binding is NOPASS, the overriding one must also be
10562 if (old
->n
.tb
->nopass
&& !proc
->n
.tb
->nopass
)
10564 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10565 " NOPASS", proc
->name
, &where
);
10569 /* If the overridden binding is PASS(x), the overriding one must also be
10570 PASS and the passed-object dummy arguments must correspond. */
10571 if (!old
->n
.tb
->nopass
)
10573 if (proc
->n
.tb
->nopass
)
10575 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10576 " PASS", proc
->name
, &where
);
10580 if (proc_pass_arg
!= old_pass_arg
)
10582 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10583 " the same position as the passed-object dummy argument of"
10584 " the overridden procedure", proc
->name
, &where
);
10593 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10596 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
10597 const char* generic_name
, locus where
)
10602 gcc_assert (t1
->specific
&& t2
->specific
);
10603 gcc_assert (!t1
->specific
->is_generic
);
10604 gcc_assert (!t2
->specific
->is_generic
);
10606 sym1
= t1
->specific
->u
.specific
->n
.sym
;
10607 sym2
= t2
->specific
->u
.specific
->n
.sym
;
10612 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10613 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
10614 || sym1
->attr
.function
!= sym2
->attr
.function
)
10616 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10617 " GENERIC '%s' at %L",
10618 sym1
->name
, sym2
->name
, generic_name
, &where
);
10622 /* Compare the interfaces. */
10623 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, 1, 0, NULL
, 0))
10625 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10626 sym1
->name
, sym2
->name
, generic_name
, &where
);
10634 /* Worker function for resolving a generic procedure binding; this is used to
10635 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10637 The difference between those cases is finding possible inherited bindings
10638 that are overridden, as one has to look for them in tb_sym_root,
10639 tb_uop_root or tb_op, respectively. Thus the caller must already find
10640 the super-type and set p->overridden correctly. */
10643 resolve_tb_generic_targets (gfc_symbol
* super_type
,
10644 gfc_typebound_proc
* p
, const char* name
)
10646 gfc_tbp_generic
* target
;
10647 gfc_symtree
* first_target
;
10648 gfc_symtree
* inherited
;
10650 gcc_assert (p
&& p
->is_generic
);
10652 /* Try to find the specific bindings for the symtrees in our target-list. */
10653 gcc_assert (p
->u
.generic
);
10654 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10655 if (!target
->specific
)
10657 gfc_typebound_proc
* overridden_tbp
;
10658 gfc_tbp_generic
* g
;
10659 const char* target_name
;
10661 target_name
= target
->specific_st
->name
;
10663 /* Defined for this type directly. */
10664 if (target
->specific_st
->n
.tb
&& !target
->specific_st
->n
.tb
->error
)
10666 target
->specific
= target
->specific_st
->n
.tb
;
10667 goto specific_found
;
10670 /* Look for an inherited specific binding. */
10673 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
10678 gcc_assert (inherited
->n
.tb
);
10679 target
->specific
= inherited
->n
.tb
;
10680 goto specific_found
;
10684 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10685 " at %L", target_name
, name
, &p
->where
);
10688 /* Once we've found the specific binding, check it is not ambiguous with
10689 other specifics already found or inherited for the same GENERIC. */
10691 gcc_assert (target
->specific
);
10693 /* This must really be a specific binding! */
10694 if (target
->specific
->is_generic
)
10696 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10697 " '%s' is GENERIC, too", name
, &p
->where
, target_name
);
10701 /* Check those already resolved on this type directly. */
10702 for (g
= p
->u
.generic
; g
; g
= g
->next
)
10703 if (g
!= target
&& g
->specific
10704 && check_generic_tbp_ambiguity (target
, g
, name
, p
->where
)
10708 /* Check for ambiguity with inherited specific targets. */
10709 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
10710 overridden_tbp
= overridden_tbp
->overridden
)
10711 if (overridden_tbp
->is_generic
)
10713 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
10715 gcc_assert (g
->specific
);
10716 if (check_generic_tbp_ambiguity (target
, g
,
10717 name
, p
->where
) == FAILURE
)
10723 /* If we attempt to "overwrite" a specific binding, this is an error. */
10724 if (p
->overridden
&& !p
->overridden
->is_generic
)
10726 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10727 " the same name", name
, &p
->where
);
10731 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10732 all must have the same attributes here. */
10733 first_target
= p
->u
.generic
->specific
->u
.specific
;
10734 gcc_assert (first_target
);
10735 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
10736 p
->function
= first_target
->n
.sym
->attr
.function
;
10742 /* Resolve a GENERIC procedure binding for a derived type. */
10745 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
10747 gfc_symbol
* super_type
;
10749 /* Find the overridden binding if any. */
10750 st
->n
.tb
->overridden
= NULL
;
10751 super_type
= gfc_get_derived_super_type (derived
);
10754 gfc_symtree
* overridden
;
10755 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
10758 if (overridden
&& overridden
->n
.tb
)
10759 st
->n
.tb
->overridden
= overridden
->n
.tb
;
10762 /* Resolve using worker function. */
10763 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
10767 /* Retrieve the target-procedure of an operator binding and do some checks in
10768 common for intrinsic and user-defined type-bound operators. */
10771 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
10773 gfc_symbol
* target_proc
;
10775 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
10776 target_proc
= target
->specific
->u
.specific
->n
.sym
;
10777 gcc_assert (target_proc
);
10779 /* All operator bindings must have a passed-object dummy argument. */
10780 if (target
->specific
->nopass
)
10782 gfc_error ("Type-bound operator at %L can't be NOPASS", &where
);
10786 return target_proc
;
10790 /* Resolve a type-bound intrinsic operator. */
10793 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
10794 gfc_typebound_proc
* p
)
10796 gfc_symbol
* super_type
;
10797 gfc_tbp_generic
* target
;
10799 /* If there's already an error here, do nothing (but don't fail again). */
10803 /* Operators should always be GENERIC bindings. */
10804 gcc_assert (p
->is_generic
);
10806 /* Look for an overridden binding. */
10807 super_type
= gfc_get_derived_super_type (derived
);
10808 if (super_type
&& super_type
->f2k_derived
)
10809 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
10812 p
->overridden
= NULL
;
10814 /* Resolve general GENERIC properties using worker function. */
10815 if (resolve_tb_generic_targets (super_type
, p
, gfc_op2string (op
)) == FAILURE
)
10818 /* Check the targets to be procedures of correct interface. */
10819 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10821 gfc_symbol
* target_proc
;
10823 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
10827 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
10839 /* Resolve a type-bound user operator (tree-walker callback). */
10841 static gfc_symbol
* resolve_bindings_derived
;
10842 static gfc_try resolve_bindings_result
;
10844 static gfc_try
check_uop_procedure (gfc_symbol
* sym
, locus where
);
10847 resolve_typebound_user_op (gfc_symtree
* stree
)
10849 gfc_symbol
* super_type
;
10850 gfc_tbp_generic
* target
;
10852 gcc_assert (stree
&& stree
->n
.tb
);
10854 if (stree
->n
.tb
->error
)
10857 /* Operators should always be GENERIC bindings. */
10858 gcc_assert (stree
->n
.tb
->is_generic
);
10860 /* Find overridden procedure, if any. */
10861 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
10862 if (super_type
&& super_type
->f2k_derived
)
10864 gfc_symtree
* overridden
;
10865 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
10866 stree
->name
, true, NULL
);
10868 if (overridden
&& overridden
->n
.tb
)
10869 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
10872 stree
->n
.tb
->overridden
= NULL
;
10874 /* Resolve basically using worker function. */
10875 if (resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
)
10879 /* Check the targets to be functions of correct interface. */
10880 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
10882 gfc_symbol
* target_proc
;
10884 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
10888 if (check_uop_procedure (target_proc
, stree
->n
.tb
->where
) == FAILURE
)
10895 resolve_bindings_result
= FAILURE
;
10896 stree
->n
.tb
->error
= 1;
10900 /* Resolve the type-bound procedures for a derived type. */
10903 resolve_typebound_procedure (gfc_symtree
* stree
)
10907 gfc_symbol
* me_arg
;
10908 gfc_symbol
* super_type
;
10909 gfc_component
* comp
;
10911 gcc_assert (stree
);
10913 /* Undefined specific symbol from GENERIC target definition. */
10917 if (stree
->n
.tb
->error
)
10920 /* If this is a GENERIC binding, use that routine. */
10921 if (stree
->n
.tb
->is_generic
)
10923 if (resolve_typebound_generic (resolve_bindings_derived
, stree
)
10929 /* Get the target-procedure to check it. */
10930 gcc_assert (!stree
->n
.tb
->is_generic
);
10931 gcc_assert (stree
->n
.tb
->u
.specific
);
10932 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
10933 where
= stree
->n
.tb
->where
;
10935 /* Default access should already be resolved from the parser. */
10936 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
10938 /* It should be a module procedure or an external procedure with explicit
10939 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10940 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
10941 || (proc
->attr
.proc
!= PROC_MODULE
10942 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
10943 || (proc
->attr
.abstract
&& !stree
->n
.tb
->deferred
))
10945 gfc_error ("'%s' must be a module procedure or an external procedure with"
10946 " an explicit interface at %L", proc
->name
, &where
);
10949 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
10950 stree
->n
.tb
->function
= proc
->attr
.function
;
10952 /* Find the super-type of the current derived type. We could do this once and
10953 store in a global if speed is needed, but as long as not I believe this is
10954 more readable and clearer. */
10955 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
10957 /* If PASS, resolve and check arguments if not already resolved / loaded
10958 from a .mod file. */
10959 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
10961 if (stree
->n
.tb
->pass_arg
)
10963 gfc_formal_arglist
* i
;
10965 /* If an explicit passing argument name is given, walk the arg-list
10966 and look for it. */
10969 stree
->n
.tb
->pass_arg_num
= 1;
10970 for (i
= proc
->formal
; i
; i
= i
->next
)
10972 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
10977 ++stree
->n
.tb
->pass_arg_num
;
10982 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10984 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
10985 stree
->n
.tb
->pass_arg
);
10991 /* Otherwise, take the first one; there should in fact be at least
10993 stree
->n
.tb
->pass_arg_num
= 1;
10996 gfc_error ("Procedure '%s' with PASS at %L must have at"
10997 " least one argument", proc
->name
, &where
);
11000 me_arg
= proc
->formal
->sym
;
11003 /* Now check that the argument-type matches and the passed-object
11004 dummy argument is generally fine. */
11006 gcc_assert (me_arg
);
11008 if (me_arg
->ts
.type
!= BT_CLASS
)
11010 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11011 " at %L", proc
->name
, &where
);
11015 if (CLASS_DATA (me_arg
)->ts
.u
.derived
11016 != resolve_bindings_derived
)
11018 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11019 " the derived-type '%s'", me_arg
->name
, proc
->name
,
11020 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
11024 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
11025 if (CLASS_DATA (me_arg
)->as
&& CLASS_DATA (me_arg
)->as
->rank
> 0)
11027 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11028 " scalar", proc
->name
, &where
);
11031 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
11033 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11034 " be ALLOCATABLE", proc
->name
, &where
);
11037 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
11039 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11040 " be POINTER", proc
->name
, &where
);
11045 /* If we are extending some type, check that we don't override a procedure
11046 flagged NON_OVERRIDABLE. */
11047 stree
->n
.tb
->overridden
= NULL
;
11050 gfc_symtree
* overridden
;
11051 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
11052 stree
->name
, true, NULL
);
11054 if (overridden
&& overridden
->n
.tb
)
11055 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
11057 if (overridden
&& check_typebound_override (stree
, overridden
) == FAILURE
)
11061 /* See if there's a name collision with a component directly in this type. */
11062 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
11063 if (!strcmp (comp
->name
, stree
->name
))
11065 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11067 stree
->name
, &where
, resolve_bindings_derived
->name
);
11071 /* Try to find a name collision with an inherited component. */
11072 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true))
11074 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11075 " component of '%s'",
11076 stree
->name
, &where
, resolve_bindings_derived
->name
);
11080 stree
->n
.tb
->error
= 0;
11084 resolve_bindings_result
= FAILURE
;
11085 stree
->n
.tb
->error
= 1;
11090 resolve_typebound_procedures (gfc_symbol
* derived
)
11094 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
11097 resolve_bindings_derived
= derived
;
11098 resolve_bindings_result
= SUCCESS
;
11100 /* Make sure the vtab has been generated. */
11101 gfc_find_derived_vtab (derived
);
11103 if (derived
->f2k_derived
->tb_sym_root
)
11104 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
11105 &resolve_typebound_procedure
);
11107 if (derived
->f2k_derived
->tb_uop_root
)
11108 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
11109 &resolve_typebound_user_op
);
11111 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
11113 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
11114 if (p
&& resolve_typebound_intrinsic_op (derived
, (gfc_intrinsic_op
) op
,
11116 resolve_bindings_result
= FAILURE
;
11119 return resolve_bindings_result
;
11123 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11124 to give all identical derived types the same backend_decl. */
11126 add_dt_to_dt_list (gfc_symbol
*derived
)
11128 gfc_dt_list
*dt_list
;
11130 for (dt_list
= gfc_derived_types
; dt_list
; dt_list
= dt_list
->next
)
11131 if (derived
== dt_list
->derived
)
11134 dt_list
= gfc_get_dt_list ();
11135 dt_list
->next
= gfc_derived_types
;
11136 dt_list
->derived
= derived
;
11137 gfc_derived_types
= dt_list
;
11141 /* Ensure that a derived-type is really not abstract, meaning that every
11142 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11145 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
11150 if (ensure_not_abstract_walker (sub
, st
->left
) == FAILURE
)
11152 if (ensure_not_abstract_walker (sub
, st
->right
) == FAILURE
)
11155 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
11157 gfc_symtree
* overriding
;
11158 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
11161 gcc_assert (overriding
->n
.tb
);
11162 if (overriding
->n
.tb
->deferred
)
11164 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11165 " '%s' is DEFERRED and not overridden",
11166 sub
->name
, &sub
->declared_at
, st
->name
);
11175 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
11177 /* The algorithm used here is to recursively travel up the ancestry of sub
11178 and for each ancestor-type, check all bindings. If any of them is
11179 DEFERRED, look it up starting from sub and see if the found (overriding)
11180 binding is not DEFERRED.
11181 This is not the most efficient way to do this, but it should be ok and is
11182 clearer than something sophisticated. */
11184 gcc_assert (ancestor
&& !sub
->attr
.abstract
);
11186 if (!ancestor
->attr
.abstract
)
11189 /* Walk bindings of this ancestor. */
11190 if (ancestor
->f2k_derived
)
11193 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
11198 /* Find next ancestor type and recurse on it. */
11199 ancestor
= gfc_get_derived_super_type (ancestor
);
11201 return ensure_not_abstract (sub
, ancestor
);
11207 /* Resolve the components of a derived type. */
11210 resolve_fl_derived (gfc_symbol
*sym
)
11212 gfc_symbol
* super_type
;
11215 super_type
= gfc_get_derived_super_type (sym
);
11217 if (sym
->attr
.is_class
&& sym
->ts
.u
.derived
== NULL
)
11219 /* Fix up incomplete CLASS symbols. */
11220 gfc_component
*data
= gfc_find_component (sym
, "_data", true, true);
11221 gfc_component
*vptr
= gfc_find_component (sym
, "_vptr", true, true);
11222 if (vptr
->ts
.u
.derived
== NULL
)
11224 gfc_symbol
*vtab
= gfc_find_derived_vtab (data
->ts
.u
.derived
);
11226 vptr
->ts
.u
.derived
= vtab
->ts
.u
.derived
;
11231 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
11233 gfc_error ("As extending type '%s' at %L has a coarray component, "
11234 "parent type '%s' shall also have one", sym
->name
,
11235 &sym
->declared_at
, super_type
->name
);
11239 /* Ensure the extended type gets resolved before we do. */
11240 if (super_type
&& resolve_fl_derived (super_type
) == FAILURE
)
11243 /* An ABSTRACT type must be extensible. */
11244 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
11246 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11247 sym
->name
, &sym
->declared_at
);
11251 for (c
= sym
->components
; c
!= NULL
; c
= c
->next
)
11254 if (c
->attr
.codimension
/* FIXME: c->as check due to PR 43412. */
11255 && (!c
->attr
.allocatable
|| (c
->as
&& c
->as
->type
!= AS_DEFERRED
)))
11257 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11258 "deferred shape", c
->name
, &c
->loc
);
11263 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
11264 && c
->ts
.u
.derived
->ts
.is_iso_c
)
11266 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11267 "shall not be a coarray", c
->name
, &c
->loc
);
11272 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
11273 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
11274 || c
->attr
.allocatable
))
11276 gfc_error ("Component '%s' at %L with coarray component "
11277 "shall be a nonpointer, nonallocatable scalar",
11283 if (c
->attr
.contiguous
&& (!c
->attr
.dimension
|| !c
->attr
.pointer
))
11285 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11286 "is not an array pointer", c
->name
, &c
->loc
);
11290 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
11292 if (c
->ts
.interface
->attr
.procedure
&& !sym
->attr
.vtype
)
11293 gfc_error ("Interface '%s', used by procedure pointer component "
11294 "'%s' at %L, is declared in a later PROCEDURE statement",
11295 c
->ts
.interface
->name
, c
->name
, &c
->loc
);
11297 /* Get the attributes from the interface (now resolved). */
11298 if (c
->ts
.interface
->attr
.if_source
11299 || c
->ts
.interface
->attr
.intrinsic
)
11301 gfc_symbol
*ifc
= c
->ts
.interface
;
11303 if (ifc
->formal
&& !ifc
->formal_ns
)
11304 resolve_symbol (ifc
);
11306 if (ifc
->attr
.intrinsic
)
11307 resolve_intrinsic (ifc
, &ifc
->declared_at
);
11311 c
->ts
= ifc
->result
->ts
;
11312 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
11313 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
11314 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
11315 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
11320 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
11321 c
->attr
.pointer
= ifc
->attr
.pointer
;
11322 c
->attr
.dimension
= ifc
->attr
.dimension
;
11323 c
->as
= gfc_copy_array_spec (ifc
->as
);
11325 c
->ts
.interface
= ifc
;
11326 c
->attr
.function
= ifc
->attr
.function
;
11327 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
11328 gfc_copy_formal_args_ppc (c
, ifc
);
11330 c
->attr
.pure
= ifc
->attr
.pure
;
11331 c
->attr
.elemental
= ifc
->attr
.elemental
;
11332 c
->attr
.recursive
= ifc
->attr
.recursive
;
11333 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
11334 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
11335 /* Replace symbols in array spec. */
11339 for (i
= 0; i
< c
->as
->rank
; i
++)
11341 gfc_expr_replace_comp (c
->as
->lower
[i
], c
);
11342 gfc_expr_replace_comp (c
->as
->upper
[i
], c
);
11345 /* Copy char length. */
11346 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
11348 gfc_charlen
*cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
11349 gfc_expr_replace_comp (cl
->length
, c
);
11350 if (cl
->length
&& !cl
->resolved
11351 && gfc_resolve_expr (cl
->length
) == FAILURE
)
11356 else if (!sym
->attr
.vtype
&& c
->ts
.interface
->name
[0] != '\0')
11358 gfc_error ("Interface '%s' of procedure pointer component "
11359 "'%s' at %L must be explicit", c
->ts
.interface
->name
,
11364 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
11366 /* Since PPCs are not implicitly typed, a PPC without an explicit
11367 interface must be a subroutine. */
11368 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
11371 /* Procedure pointer components: Check PASS arg. */
11372 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0
11373 && !sym
->attr
.vtype
)
11375 gfc_symbol
* me_arg
;
11377 if (c
->tb
->pass_arg
)
11379 gfc_formal_arglist
* i
;
11381 /* If an explicit passing argument name is given, walk the arg-list
11382 and look for it. */
11385 c
->tb
->pass_arg_num
= 1;
11386 for (i
= c
->formal
; i
; i
= i
->next
)
11388 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
11393 c
->tb
->pass_arg_num
++;
11398 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11399 "at %L has no argument '%s'", c
->name
,
11400 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
11407 /* Otherwise, take the first one; there should in fact be at least
11409 c
->tb
->pass_arg_num
= 1;
11412 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11413 "must have at least one argument",
11418 me_arg
= c
->formal
->sym
;
11421 /* Now check that the argument-type matches. */
11422 gcc_assert (me_arg
);
11423 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
11424 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
11425 || (me_arg
->ts
.type
== BT_CLASS
11426 && CLASS_DATA (me_arg
)->ts
.u
.derived
!= sym
))
11428 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11429 " the derived type '%s'", me_arg
->name
, c
->name
,
11430 me_arg
->name
, &c
->loc
, sym
->name
);
11435 /* Check for C453. */
11436 if (me_arg
->attr
.dimension
)
11438 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11439 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
11445 if (me_arg
->attr
.pointer
)
11447 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11448 "may not have the POINTER attribute", me_arg
->name
,
11449 c
->name
, me_arg
->name
, &c
->loc
);
11454 if (me_arg
->attr
.allocatable
)
11456 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11457 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
11458 me_arg
->name
, &c
->loc
);
11463 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
11464 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11465 " at %L", c
->name
, &c
->loc
);
11469 /* Check type-spec if this is not the parent-type component. */
11470 if ((!sym
->attr
.extension
|| c
!= sym
->components
) && !sym
->attr
.vtype
11471 && resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
) == FAILURE
)
11474 /* If this type is an extension, set the accessibility of the parent
11476 if (super_type
&& c
== sym
->components
11477 && strcmp (super_type
->name
, c
->name
) == 0)
11478 c
->attr
.access
= super_type
->attr
.access
;
11480 /* If this type is an extension, see if this component has the same name
11481 as an inherited type-bound procedure. */
11482 if (super_type
&& !sym
->attr
.is_class
11483 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
11485 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11486 " inherited type-bound procedure",
11487 c
->name
, sym
->name
, &c
->loc
);
11491 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
)
11493 if (c
->ts
.u
.cl
->length
== NULL
11494 || (resolve_charlen (c
->ts
.u
.cl
) == FAILURE
)
11495 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
11497 gfc_error ("Character length of component '%s' needs to "
11498 "be a constant specification expression at %L",
11500 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
11505 if (c
->ts
.type
== BT_DERIVED
11506 && sym
->component_access
!= ACCESS_PRIVATE
11507 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
11508 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
11509 && !c
->ts
.u
.derived
->attr
.use_assoc
11510 && !gfc_check_access (c
->ts
.u
.derived
->attr
.access
,
11511 c
->ts
.u
.derived
->ns
->default_access
)
11512 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: the component '%s' "
11513 "is a PRIVATE type and cannot be a component of "
11514 "'%s', which is PUBLIC at %L", c
->name
,
11515 sym
->name
, &sym
->declared_at
) == FAILURE
)
11518 if ((sym
->attr
.sequence
|| sym
->attr
.is_bind_c
) && c
->ts
.type
== BT_CLASS
)
11520 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11521 "type %s", c
->name
, &c
->loc
, sym
->name
);
11525 if (sym
->attr
.sequence
)
11527 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
11529 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11530 "not have the SEQUENCE attribute",
11531 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
11536 if (!sym
->attr
.is_class
&& c
->ts
.type
== BT_DERIVED
&& !sym
->attr
.vtype
11537 && c
->attr
.pointer
&& c
->ts
.u
.derived
->components
== NULL
11538 && !c
->ts
.u
.derived
->attr
.zero_comp
)
11540 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11541 "that has not been declared", c
->name
, sym
->name
,
11546 if (c
->ts
.type
== BT_CLASS
&& CLASS_DATA (c
)->attr
.class_pointer
11547 && CLASS_DATA (c
)->ts
.u
.derived
->components
== NULL
11548 && !CLASS_DATA (c
)->ts
.u
.derived
->attr
.zero_comp
)
11550 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11551 "that has not been declared", c
->name
, sym
->name
,
11557 if (c
->ts
.type
== BT_CLASS
11558 && !(CLASS_DATA (c
)->attr
.class_pointer
11559 || CLASS_DATA (c
)->attr
.allocatable
))
11561 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11562 "or pointer", c
->name
, &c
->loc
);
11566 /* Ensure that all the derived type components are put on the
11567 derived type list; even in formal namespaces, where derived type
11568 pointer components might not have been declared. */
11569 if (c
->ts
.type
== BT_DERIVED
11571 && c
->ts
.u
.derived
->components
11573 && sym
!= c
->ts
.u
.derived
)
11574 add_dt_to_dt_list (c
->ts
.u
.derived
);
11576 if (gfc_resolve_array_spec (c
->as
, !(c
->attr
.pointer
11577 || c
->attr
.proc_pointer
11578 || c
->attr
.allocatable
)) == FAILURE
)
11582 /* Resolve the type-bound procedures. */
11583 if (resolve_typebound_procedures (sym
) == FAILURE
)
11586 /* Resolve the finalizer procedures. */
11587 if (gfc_resolve_finalizers (sym
) == FAILURE
)
11590 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11591 all DEFERRED bindings are overridden. */
11592 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
11593 && !sym
->attr
.is_class
11594 && ensure_not_abstract (sym
, super_type
) == FAILURE
)
11597 /* Add derived type to the derived type list. */
11598 add_dt_to_dt_list (sym
);
11605 resolve_fl_namelist (gfc_symbol
*sym
)
11610 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11612 /* Reject namelist arrays of assumed shape. */
11613 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
11614 && gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object '%s' "
11615 "must not have assumed shape in namelist "
11616 "'%s' at %L", nl
->sym
->name
, sym
->name
,
11617 &sym
->declared_at
) == FAILURE
)
11620 /* Reject namelist arrays that are not constant shape. */
11621 if (is_non_constant_shape_array (nl
->sym
))
11623 gfc_error ("NAMELIST array object '%s' must have constant "
11624 "shape in namelist '%s' at %L", nl
->sym
->name
,
11625 sym
->name
, &sym
->declared_at
);
11629 /* Namelist objects cannot have allocatable or pointer components. */
11630 if (nl
->sym
->ts
.type
!= BT_DERIVED
)
11633 if (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
)
11635 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11636 "have ALLOCATABLE components",
11637 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11641 if (nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
)
11643 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11644 "have POINTER components",
11645 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11650 /* Reject PRIVATE objects in a PUBLIC namelist. */
11651 if (gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
11653 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11655 if (!nl
->sym
->attr
.use_assoc
11656 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
11657 && !gfc_check_access(nl
->sym
->attr
.access
,
11658 nl
->sym
->ns
->default_access
))
11660 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11661 "cannot be member of PUBLIC namelist '%s' at %L",
11662 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11666 /* Types with private components that came here by USE-association. */
11667 if (nl
->sym
->ts
.type
== BT_DERIVED
11668 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
11670 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11671 "components and cannot be member of namelist '%s' at %L",
11672 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11676 /* Types with private components that are defined in the same module. */
11677 if (nl
->sym
->ts
.type
== BT_DERIVED
11678 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
11679 && !gfc_check_access (nl
->sym
->ts
.u
.derived
->attr
.private_comp
11680 ? ACCESS_PRIVATE
: ACCESS_UNKNOWN
,
11681 nl
->sym
->ns
->default_access
))
11683 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11684 "cannot be a member of PUBLIC namelist '%s' at %L",
11685 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11692 /* 14.1.2 A module or internal procedure represent local entities
11693 of the same type as a namelist member and so are not allowed. */
11694 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11696 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
11699 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
11700 if ((nl
->sym
== sym
->ns
->proc_name
)
11702 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
11706 if (nl
->sym
&& nl
->sym
->name
)
11707 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
11708 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
11710 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11711 "attribute in '%s' at %L", nlsym
->name
,
11712 &sym
->declared_at
);
11722 resolve_fl_parameter (gfc_symbol
*sym
)
11724 /* A parameter array's shape needs to be constant. */
11725 if (sym
->as
!= NULL
11726 && (sym
->as
->type
== AS_DEFERRED
11727 || is_non_constant_shape_array (sym
)))
11729 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11730 "or of deferred shape", sym
->name
, &sym
->declared_at
);
11734 /* Make sure a parameter that has been implicitly typed still
11735 matches the implicit type, since PARAMETER statements can precede
11736 IMPLICIT statements. */
11737 if (sym
->attr
.implicit_type
11738 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
11741 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11742 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
11746 /* Make sure the types of derived parameters are consistent. This
11747 type checking is deferred until resolution because the type may
11748 refer to a derived type from the host. */
11749 if (sym
->ts
.type
== BT_DERIVED
11750 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
11752 gfc_error ("Incompatible derived type in PARAMETER at %L",
11753 &sym
->value
->where
);
11760 /* Do anything necessary to resolve a symbol. Right now, we just
11761 assume that an otherwise unknown symbol is a variable. This sort
11762 of thing commonly happens for symbols in module. */
11765 resolve_symbol (gfc_symbol
*sym
)
11767 int check_constant
, mp_flag
;
11768 gfc_symtree
*symtree
;
11769 gfc_symtree
*this_symtree
;
11773 /* Avoid double resolution of function result symbols. */
11774 if ((sym
->result
|| sym
->attr
.result
) && !sym
->attr
.dummy
11775 && (sym
->ns
!= gfc_current_ns
))
11778 if (sym
->attr
.flavor
== FL_UNKNOWN
)
11781 /* If we find that a flavorless symbol is an interface in one of the
11782 parent namespaces, find its symtree in this namespace, free the
11783 symbol and set the symtree to point to the interface symbol. */
11784 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
11786 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
11787 if (symtree
&& (symtree
->n
.sym
->generic
||
11788 (symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
11789 && sym
->ns
->construct_entities
)))
11791 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
11793 gfc_release_symbol (sym
);
11794 symtree
->n
.sym
->refs
++;
11795 this_symtree
->n
.sym
= symtree
->n
.sym
;
11800 /* Otherwise give it a flavor according to such attributes as
11802 if (sym
->attr
.external
== 0 && sym
->attr
.intrinsic
== 0)
11803 sym
->attr
.flavor
= FL_VARIABLE
;
11806 sym
->attr
.flavor
= FL_PROCEDURE
;
11807 if (sym
->attr
.dimension
)
11808 sym
->attr
.function
= 1;
11812 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
11813 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
11815 if (sym
->attr
.procedure
&& sym
->ts
.interface
11816 && sym
->attr
.if_source
!= IFSRC_DECL
11817 && resolve_procedure_interface (sym
) == FAILURE
)
11820 if (sym
->attr
.is_protected
&& !sym
->attr
.proc_pointer
11821 && (sym
->attr
.procedure
|| sym
->attr
.external
))
11823 if (sym
->attr
.external
)
11824 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
11825 "at %L", &sym
->declared_at
);
11827 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
11828 "at %L", &sym
->declared_at
);
11835 if (sym
->attr
.contiguous
11836 && (!sym
->attr
.dimension
|| (sym
->as
->type
!= AS_ASSUMED_SHAPE
11837 && !sym
->attr
.pointer
)))
11839 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
11840 "array pointer or an assumed-shape array", sym
->name
,
11841 &sym
->declared_at
);
11845 if (sym
->attr
.flavor
== FL_DERIVED
&& resolve_fl_derived (sym
) == FAILURE
)
11848 /* Symbols that are module procedures with results (functions) have
11849 the types and array specification copied for type checking in
11850 procedures that call them, as well as for saving to a module
11851 file. These symbols can't stand the scrutiny that their results
11853 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
11855 /* Make sure that the intrinsic is consistent with its internal
11856 representation. This needs to be done before assigning a default
11857 type to avoid spurious warnings. */
11858 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
11859 && resolve_intrinsic (sym
, &sym
->declared_at
) == FAILURE
)
11862 /* Resolve associate names. */
11864 resolve_assoc_var (sym
, true);
11866 /* Assign default type to symbols that need one and don't have one. */
11867 if (sym
->ts
.type
== BT_UNKNOWN
)
11869 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
11870 gfc_set_default_type (sym
, 1, NULL
);
11872 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
11873 && !sym
->attr
.function
&& !sym
->attr
.subroutine
11874 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
11875 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
11877 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
11879 /* The specific case of an external procedure should emit an error
11880 in the case that there is no implicit type. */
11882 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
11885 /* Result may be in another namespace. */
11886 resolve_symbol (sym
->result
);
11888 if (!sym
->result
->attr
.proc_pointer
)
11890 sym
->ts
= sym
->result
->ts
;
11891 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
11892 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
11893 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
11894 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
11895 sym
->attr
.contiguous
= sym
->result
->attr
.contiguous
;
11901 /* Assumed size arrays and assumed shape arrays must be dummy
11902 arguments. Array-spec's of implied-shape should have been resolved to
11903 AS_EXPLICIT already. */
11907 gcc_assert (sym
->as
->type
!= AS_IMPLIED_SHAPE
);
11908 if (((sym
->as
->type
== AS_ASSUMED_SIZE
&& !sym
->as
->cp_was_assumed
)
11909 || sym
->as
->type
== AS_ASSUMED_SHAPE
)
11910 && sym
->attr
.dummy
== 0)
11912 if (sym
->as
->type
== AS_ASSUMED_SIZE
)
11913 gfc_error ("Assumed size array at %L must be a dummy argument",
11914 &sym
->declared_at
);
11916 gfc_error ("Assumed shape array at %L must be a dummy argument",
11917 &sym
->declared_at
);
11922 /* Make sure symbols with known intent or optional are really dummy
11923 variable. Because of ENTRY statement, this has to be deferred
11924 until resolution time. */
11926 if (!sym
->attr
.dummy
11927 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
11929 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
11933 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
11935 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11936 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
11940 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
11942 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
11943 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
11945 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11946 "attribute must have constant length",
11947 sym
->name
, &sym
->declared_at
);
11951 if (sym
->ts
.is_c_interop
11952 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
11954 gfc_error ("C interoperable character dummy variable '%s' at %L "
11955 "with VALUE attribute must have length one",
11956 sym
->name
, &sym
->declared_at
);
11961 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11962 do this for something that was implicitly typed because that is handled
11963 in gfc_set_default_type. Handle dummy arguments and procedure
11964 definitions separately. Also, anything that is use associated is not
11965 handled here but instead is handled in the module it is declared in.
11966 Finally, derived type definitions are allowed to be BIND(C) since that
11967 only implies that they're interoperable, and they are checked fully for
11968 interoperability when a variable is declared of that type. */
11969 if (sym
->attr
.is_bind_c
&& sym
->attr
.implicit_type
== 0 &&
11970 sym
->attr
.use_assoc
== 0 && sym
->attr
.dummy
== 0 &&
11971 sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.flavor
!= FL_DERIVED
)
11973 gfc_try t
= SUCCESS
;
11975 /* First, make sure the variable is declared at the
11976 module-level scope (J3/04-007, Section 15.3). */
11977 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
11978 sym
->attr
.in_common
== 0)
11980 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11981 "is neither a COMMON block nor declared at the "
11982 "module level scope", sym
->name
, &(sym
->declared_at
));
11985 else if (sym
->common_head
!= NULL
)
11987 t
= verify_com_block_vars_c_interop (sym
->common_head
);
11991 /* If type() declaration, we need to verify that the components
11992 of the given type are all C interoperable, etc. */
11993 if (sym
->ts
.type
== BT_DERIVED
&&
11994 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
11996 /* Make sure the user marked the derived type as BIND(C). If
11997 not, call the verify routine. This could print an error
11998 for the derived type more than once if multiple variables
11999 of that type are declared. */
12000 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
12001 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
12005 /* Verify the variable itself as C interoperable if it
12006 is BIND(C). It is not possible for this to succeed if
12007 the verify_bind_c_derived_type failed, so don't have to handle
12008 any error returned by verify_bind_c_derived_type. */
12009 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
12010 sym
->common_block
);
12015 /* clear the is_bind_c flag to prevent reporting errors more than
12016 once if something failed. */
12017 sym
->attr
.is_bind_c
= 0;
12022 /* If a derived type symbol has reached this point, without its
12023 type being declared, we have an error. Notice that most
12024 conditions that produce undefined derived types have already
12025 been dealt with. However, the likes of:
12026 implicit type(t) (t) ..... call foo (t) will get us here if
12027 the type is not declared in the scope of the implicit
12028 statement. Change the type to BT_UNKNOWN, both because it is so
12029 and to prevent an ICE. */
12030 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->components
== NULL
12031 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
12033 gfc_error ("The derived type '%s' at %L is of type '%s', "
12034 "which has not been defined", sym
->name
,
12035 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
12036 sym
->ts
.type
= BT_UNKNOWN
;
12040 /* Make sure that the derived type has been resolved and that the
12041 derived type is visible in the symbol's namespace, if it is a
12042 module function and is not PRIVATE. */
12043 if (sym
->ts
.type
== BT_DERIVED
12044 && sym
->ts
.u
.derived
->attr
.use_assoc
12045 && sym
->ns
->proc_name
12046 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
12050 if (resolve_fl_derived (sym
->ts
.u
.derived
) == FAILURE
)
12053 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 1, &ds
);
12054 if (!ds
&& sym
->attr
.function
12055 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
12057 symtree
= gfc_new_symtree (&sym
->ns
->sym_root
,
12058 sym
->ts
.u
.derived
->name
);
12059 symtree
->n
.sym
= sym
->ts
.u
.derived
;
12060 sym
->ts
.u
.derived
->refs
++;
12064 /* Unless the derived-type declaration is use associated, Fortran 95
12065 does not allow public entries of private derived types.
12066 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12067 161 in 95-006r3. */
12068 if (sym
->ts
.type
== BT_DERIVED
12069 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12070 && !sym
->ts
.u
.derived
->attr
.use_assoc
12071 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
12072 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
12073 sym
->ts
.u
.derived
->ns
->default_access
)
12074 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC %s '%s' at %L "
12075 "of PRIVATE derived type '%s'",
12076 (sym
->attr
.flavor
== FL_PARAMETER
) ? "parameter"
12077 : "variable", sym
->name
, &sym
->declared_at
,
12078 sym
->ts
.u
.derived
->name
) == FAILURE
)
12081 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12082 default initialization is defined (5.1.2.4.4). */
12083 if (sym
->ts
.type
== BT_DERIVED
12085 && sym
->attr
.intent
== INTENT_OUT
12087 && sym
->as
->type
== AS_ASSUMED_SIZE
)
12089 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
12091 if (c
->initializer
)
12093 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12094 "ASSUMED SIZE and so cannot have a default initializer",
12095 sym
->name
, &sym
->declared_at
);
12102 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12103 || sym
->attr
.codimension
)
12104 && sym
->attr
.result
)
12105 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12106 "a coarray component", sym
->name
, &sym
->declared_at
);
12109 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
12110 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
12111 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12112 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
12115 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
12116 && (sym
->attr
.codimension
|| sym
->attr
.pointer
|| sym
->attr
.dimension
12117 || sym
->attr
.allocatable
))
12118 gfc_error ("Variable '%s' at %L with coarray component "
12119 "shall be a nonpointer, nonallocatable scalar",
12120 sym
->name
, &sym
->declared_at
);
12122 /* F2008, C526. The function-result case was handled above. */
12123 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12124 || sym
->attr
.codimension
)
12125 && !(sym
->attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
12126 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12127 || sym
->ns
->proc_name
->attr
.is_main_program
12128 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
12129 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
12130 "component and is not ALLOCATABLE, SAVE nor a "
12131 "dummy argument", sym
->name
, &sym
->declared_at
);
12132 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12133 else if (sym
->attr
.codimension
&& !sym
->attr
.allocatable
12134 && sym
->as
&& sym
->as
->cotype
== AS_DEFERRED
)
12135 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12136 "deferred shape", sym
->name
, &sym
->declared_at
);
12137 else if (sym
->attr
.codimension
&& sym
->attr
.allocatable
12138 && (sym
->as
->type
!= AS_DEFERRED
|| sym
->as
->cotype
!= AS_DEFERRED
))
12139 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12140 "deferred shape", sym
->name
, &sym
->declared_at
);
12144 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12145 || (sym
->attr
.codimension
&& sym
->attr
.allocatable
))
12146 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
12147 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12148 "allocatable coarray or have coarray components",
12149 sym
->name
, &sym
->declared_at
);
12151 if (sym
->attr
.codimension
&& sym
->attr
.dummy
12152 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
12153 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12154 "procedure '%s'", sym
->name
, &sym
->declared_at
,
12155 sym
->ns
->proc_name
->name
);
12157 switch (sym
->attr
.flavor
)
12160 if (resolve_fl_variable (sym
, mp_flag
) == FAILURE
)
12165 if (resolve_fl_procedure (sym
, mp_flag
) == FAILURE
)
12170 if (resolve_fl_namelist (sym
) == FAILURE
)
12175 if (resolve_fl_parameter (sym
) == FAILURE
)
12183 /* Resolve array specifier. Check as well some constraints
12184 on COMMON blocks. */
12186 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
12188 /* Set the formal_arg_flag so that check_conflict will not throw
12189 an error for host associated variables in the specification
12190 expression for an array_valued function. */
12191 if (sym
->attr
.function
&& sym
->as
)
12192 formal_arg_flag
= 1;
12194 gfc_resolve_array_spec (sym
->as
, check_constant
);
12196 formal_arg_flag
= 0;
12198 /* Resolve formal namespaces. */
12199 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
12200 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
12201 gfc_resolve (sym
->formal_ns
);
12203 /* Make sure the formal namespace is present. */
12204 if (sym
->formal
&& !sym
->formal_ns
)
12206 gfc_formal_arglist
*formal
= sym
->formal
;
12207 while (formal
&& !formal
->sym
)
12208 formal
= formal
->next
;
12212 sym
->formal_ns
= formal
->sym
->ns
;
12213 sym
->formal_ns
->refs
++;
12217 /* Check threadprivate restrictions. */
12218 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
&& !sym
->ns
->save_all
12219 && (!sym
->attr
.in_common
12220 && sym
->module
== NULL
12221 && (sym
->ns
->proc_name
== NULL
12222 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
12223 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
12225 /* If we have come this far we can apply default-initializers, as
12226 described in 14.7.5, to those variables that have not already
12227 been assigned one. */
12228 if (sym
->ts
.type
== BT_DERIVED
12229 && sym
->ns
== gfc_current_ns
12231 && !sym
->attr
.allocatable
12232 && !sym
->attr
.alloc_comp
)
12234 symbol_attribute
*a
= &sym
->attr
;
12236 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
12237 && !a
->in_common
&& !a
->use_assoc
12238 && (a
->referenced
|| a
->result
)
12239 && !(a
->function
&& sym
!= sym
->result
))
12240 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
12241 apply_default_init (sym
);
12244 if (sym
->ts
.type
== BT_CLASS
&& sym
->ns
== gfc_current_ns
12245 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
12246 && !CLASS_DATA (sym
)->attr
.class_pointer
12247 && !CLASS_DATA (sym
)->attr
.allocatable
)
12248 apply_default_init (sym
);
12250 /* If this symbol has a type-spec, check it. */
12251 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
12252 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
12253 if (resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
)
12259 /************* Resolve DATA statements *************/
12263 gfc_data_value
*vnode
;
12269 /* Advance the values structure to point to the next value in the data list. */
12272 next_data_value (void)
12274 while (mpz_cmp_ui (values
.left
, 0) == 0)
12277 if (values
.vnode
->next
== NULL
)
12280 values
.vnode
= values
.vnode
->next
;
12281 mpz_set (values
.left
, values
.vnode
->repeat
);
12289 check_data_variable (gfc_data_variable
*var
, locus
*where
)
12295 ar_type mark
= AR_UNKNOWN
;
12297 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
12303 if (gfc_resolve_expr (var
->expr
) == FAILURE
)
12307 mpz_init_set_si (offset
, 0);
12310 if (e
->expr_type
!= EXPR_VARIABLE
)
12311 gfc_internal_error ("check_data_variable(): Bad expression");
12313 sym
= e
->symtree
->n
.sym
;
12315 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
12317 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12318 sym
->name
, &sym
->declared_at
);
12321 if (e
->ref
== NULL
&& sym
->as
)
12323 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12324 " declaration", sym
->name
, where
);
12328 has_pointer
= sym
->attr
.pointer
;
12330 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12332 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
12335 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
)
12337 gfc_error ("DATA element '%s' at %L cannot have a coindex",
12343 && ref
->type
== REF_ARRAY
12344 && ref
->u
.ar
.type
!= AR_FULL
)
12346 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12347 "be a full array", sym
->name
, where
);
12352 if (e
->rank
== 0 || has_pointer
)
12354 mpz_init_set_ui (size
, 1);
12361 /* Find the array section reference. */
12362 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12364 if (ref
->type
!= REF_ARRAY
)
12366 if (ref
->u
.ar
.type
== AR_ELEMENT
)
12372 /* Set marks according to the reference pattern. */
12373 switch (ref
->u
.ar
.type
)
12381 /* Get the start position of array section. */
12382 gfc_get_section_index (ar
, section_index
, &offset
);
12387 gcc_unreachable ();
12390 if (gfc_array_size (e
, &size
) == FAILURE
)
12392 gfc_error ("Nonconstant array section at %L in DATA statement",
12394 mpz_clear (offset
);
12401 while (mpz_cmp_ui (size
, 0) > 0)
12403 if (next_data_value () == FAILURE
)
12405 gfc_error ("DATA statement at %L has more variables than values",
12411 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
12415 /* If we have more than one element left in the repeat count,
12416 and we have more than one element left in the target variable,
12417 then create a range assignment. */
12418 /* FIXME: Only done for full arrays for now, since array sections
12420 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
12421 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
12425 if (mpz_cmp (size
, values
.left
) >= 0)
12427 mpz_init_set (range
, values
.left
);
12428 mpz_sub (size
, size
, values
.left
);
12429 mpz_set_ui (values
.left
, 0);
12433 mpz_init_set (range
, size
);
12434 mpz_sub (values
.left
, values
.left
, size
);
12435 mpz_set_ui (size
, 0);
12438 t
= gfc_assign_data_value_range (var
->expr
, values
.vnode
->expr
,
12441 mpz_add (offset
, offset
, range
);
12448 /* Assign initial value to symbol. */
12451 mpz_sub_ui (values
.left
, values
.left
, 1);
12452 mpz_sub_ui (size
, size
, 1);
12454 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
, offset
);
12458 if (mark
== AR_FULL
)
12459 mpz_add_ui (offset
, offset
, 1);
12461 /* Modify the array section indexes and recalculate the offset
12462 for next element. */
12463 else if (mark
== AR_SECTION
)
12464 gfc_advance_section (section_index
, ar
, &offset
);
12468 if (mark
== AR_SECTION
)
12470 for (i
= 0; i
< ar
->dimen
; i
++)
12471 mpz_clear (section_index
[i
]);
12475 mpz_clear (offset
);
12481 static gfc_try
traverse_data_var (gfc_data_variable
*, locus
*);
12483 /* Iterate over a list of elements in a DATA statement. */
12486 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
12489 iterator_stack frame
;
12490 gfc_expr
*e
, *start
, *end
, *step
;
12491 gfc_try retval
= SUCCESS
;
12493 mpz_init (frame
.value
);
12496 start
= gfc_copy_expr (var
->iter
.start
);
12497 end
= gfc_copy_expr (var
->iter
.end
);
12498 step
= gfc_copy_expr (var
->iter
.step
);
12500 if (gfc_simplify_expr (start
, 1) == FAILURE
12501 || start
->expr_type
!= EXPR_CONSTANT
)
12503 gfc_error ("start of implied-do loop at %L could not be "
12504 "simplified to a constant value", &start
->where
);
12508 if (gfc_simplify_expr (end
, 1) == FAILURE
12509 || end
->expr_type
!= EXPR_CONSTANT
)
12511 gfc_error ("end of implied-do loop at %L could not be "
12512 "simplified to a constant value", &start
->where
);
12516 if (gfc_simplify_expr (step
, 1) == FAILURE
12517 || step
->expr_type
!= EXPR_CONSTANT
)
12519 gfc_error ("step of implied-do loop at %L could not be "
12520 "simplified to a constant value", &start
->where
);
12525 mpz_set (trip
, end
->value
.integer
);
12526 mpz_sub (trip
, trip
, start
->value
.integer
);
12527 mpz_add (trip
, trip
, step
->value
.integer
);
12529 mpz_div (trip
, trip
, step
->value
.integer
);
12531 mpz_set (frame
.value
, start
->value
.integer
);
12533 frame
.prev
= iter_stack
;
12534 frame
.variable
= var
->iter
.var
->symtree
;
12535 iter_stack
= &frame
;
12537 while (mpz_cmp_ui (trip
, 0) > 0)
12539 if (traverse_data_var (var
->list
, where
) == FAILURE
)
12545 e
= gfc_copy_expr (var
->expr
);
12546 if (gfc_simplify_expr (e
, 1) == FAILURE
)
12553 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
12555 mpz_sub_ui (trip
, trip
, 1);
12559 mpz_clear (frame
.value
);
12562 gfc_free_expr (start
);
12563 gfc_free_expr (end
);
12564 gfc_free_expr (step
);
12566 iter_stack
= frame
.prev
;
12571 /* Type resolve variables in the variable list of a DATA statement. */
12574 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
12578 for (; var
; var
= var
->next
)
12580 if (var
->expr
== NULL
)
12581 t
= traverse_data_list (var
, where
);
12583 t
= check_data_variable (var
, where
);
12593 /* Resolve the expressions and iterators associated with a data statement.
12594 This is separate from the assignment checking because data lists should
12595 only be resolved once. */
12598 resolve_data_variables (gfc_data_variable
*d
)
12600 for (; d
; d
= d
->next
)
12602 if (d
->list
== NULL
)
12604 if (gfc_resolve_expr (d
->expr
) == FAILURE
)
12609 if (gfc_resolve_iterator (&d
->iter
, false) == FAILURE
)
12612 if (resolve_data_variables (d
->list
) == FAILURE
)
12621 /* Resolve a single DATA statement. We implement this by storing a pointer to
12622 the value list into static variables, and then recursively traversing the
12623 variables list, expanding iterators and such. */
12626 resolve_data (gfc_data
*d
)
12629 if (resolve_data_variables (d
->var
) == FAILURE
)
12632 values
.vnode
= d
->value
;
12633 if (d
->value
== NULL
)
12634 mpz_set_ui (values
.left
, 0);
12636 mpz_set (values
.left
, d
->value
->repeat
);
12638 if (traverse_data_var (d
->var
, &d
->where
) == FAILURE
)
12641 /* At this point, we better not have any values left. */
12643 if (next_data_value () == SUCCESS
)
12644 gfc_error ("DATA statement at %L has more values than variables",
12649 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12650 accessed by host or use association, is a dummy argument to a pure function,
12651 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12652 is storage associated with any such variable, shall not be used in the
12653 following contexts: (clients of this function). */
12655 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12656 procedure. Returns zero if assignment is OK, nonzero if there is a
12659 gfc_impure_variable (gfc_symbol
*sym
)
12664 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
12667 /* Check if the symbol's ns is inside the pure procedure. */
12668 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12672 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
12676 proc
= sym
->ns
->proc_name
;
12677 if (sym
->attr
.dummy
&& gfc_pure (proc
)
12678 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
12680 proc
->attr
.function
))
12683 /* TODO: Sort out what can be storage associated, if anything, and include
12684 it here. In principle equivalences should be scanned but it does not
12685 seem to be possible to storage associate an impure variable this way. */
12690 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12691 current namespace is inside a pure procedure. */
12694 gfc_pure (gfc_symbol
*sym
)
12696 symbol_attribute attr
;
12701 /* Check if the current namespace or one of its parents
12702 belongs to a pure procedure. */
12703 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12705 sym
= ns
->proc_name
;
12709 if (attr
.flavor
== FL_PROCEDURE
&& attr
.pure
)
12717 return attr
.flavor
== FL_PROCEDURE
&& attr
.pure
;
12721 /* Test whether the current procedure is elemental or not. */
12724 gfc_elemental (gfc_symbol
*sym
)
12726 symbol_attribute attr
;
12729 sym
= gfc_current_ns
->proc_name
;
12734 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
12738 /* Warn about unused labels. */
12741 warn_unused_fortran_label (gfc_st_label
*label
)
12746 warn_unused_fortran_label (label
->left
);
12748 if (label
->defined
== ST_LABEL_UNKNOWN
)
12751 switch (label
->referenced
)
12753 case ST_LABEL_UNKNOWN
:
12754 gfc_warning ("Label %d at %L defined but not used", label
->value
,
12758 case ST_LABEL_BAD_TARGET
:
12759 gfc_warning ("Label %d at %L defined but cannot be used",
12760 label
->value
, &label
->where
);
12767 warn_unused_fortran_label (label
->right
);
12771 /* Returns the sequence type of a symbol or sequence. */
12774 sequence_type (gfc_typespec ts
)
12783 if (ts
.u
.derived
->components
== NULL
)
12784 return SEQ_NONDEFAULT
;
12786 result
= sequence_type (ts
.u
.derived
->components
->ts
);
12787 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
12788 if (sequence_type (c
->ts
) != result
)
12794 if (ts
.kind
!= gfc_default_character_kind
)
12795 return SEQ_NONDEFAULT
;
12797 return SEQ_CHARACTER
;
12800 if (ts
.kind
!= gfc_default_integer_kind
)
12801 return SEQ_NONDEFAULT
;
12803 return SEQ_NUMERIC
;
12806 if (!(ts
.kind
== gfc_default_real_kind
12807 || ts
.kind
== gfc_default_double_kind
))
12808 return SEQ_NONDEFAULT
;
12810 return SEQ_NUMERIC
;
12813 if (ts
.kind
!= gfc_default_complex_kind
)
12814 return SEQ_NONDEFAULT
;
12816 return SEQ_NUMERIC
;
12819 if (ts
.kind
!= gfc_default_logical_kind
)
12820 return SEQ_NONDEFAULT
;
12822 return SEQ_NUMERIC
;
12825 return SEQ_NONDEFAULT
;
12830 /* Resolve derived type EQUIVALENCE object. */
12833 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
12835 gfc_component
*c
= derived
->components
;
12840 /* Shall not be an object of nonsequence derived type. */
12841 if (!derived
->attr
.sequence
)
12843 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12844 "attribute to be an EQUIVALENCE object", sym
->name
,
12849 /* Shall not have allocatable components. */
12850 if (derived
->attr
.alloc_comp
)
12852 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12853 "components to be an EQUIVALENCE object",sym
->name
,
12858 if (sym
->attr
.in_common
&& gfc_has_default_initializer (sym
->ts
.u
.derived
))
12860 gfc_error ("Derived type variable '%s' at %L with default "
12861 "initialization cannot be in EQUIVALENCE with a variable "
12862 "in COMMON", sym
->name
, &e
->where
);
12866 for (; c
; c
= c
->next
)
12868 if (c
->ts
.type
== BT_DERIVED
12869 && (resolve_equivalence_derived (c
->ts
.u
.derived
, sym
, e
) == FAILURE
))
12872 /* Shall not be an object of sequence derived type containing a pointer
12873 in the structure. */
12874 if (c
->attr
.pointer
)
12876 gfc_error ("Derived type variable '%s' at %L with pointer "
12877 "component(s) cannot be an EQUIVALENCE object",
12878 sym
->name
, &e
->where
);
12886 /* Resolve equivalence object.
12887 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12888 an allocatable array, an object of nonsequence derived type, an object of
12889 sequence derived type containing a pointer at any level of component
12890 selection, an automatic object, a function name, an entry name, a result
12891 name, a named constant, a structure component, or a subobject of any of
12892 the preceding objects. A substring shall not have length zero. A
12893 derived type shall not have components with default initialization nor
12894 shall two objects of an equivalence group be initialized.
12895 Either all or none of the objects shall have an protected attribute.
12896 The simple constraints are done in symbol.c(check_conflict) and the rest
12897 are implemented here. */
12900 resolve_equivalence (gfc_equiv
*eq
)
12903 gfc_symbol
*first_sym
;
12906 locus
*last_where
= NULL
;
12907 seq_type eq_type
, last_eq_type
;
12908 gfc_typespec
*last_ts
;
12909 int object
, cnt_protected
;
12912 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
12914 first_sym
= eq
->expr
->symtree
->n
.sym
;
12918 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
12922 e
->ts
= e
->symtree
->n
.sym
->ts
;
12923 /* match_varspec might not know yet if it is seeing
12924 array reference or substring reference, as it doesn't
12926 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
12928 gfc_ref
*ref
= e
->ref
;
12929 sym
= e
->symtree
->n
.sym
;
12931 if (sym
->attr
.dimension
)
12933 ref
->u
.ar
.as
= sym
->as
;
12937 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12938 if (e
->ts
.type
== BT_CHARACTER
12940 && ref
->type
== REF_ARRAY
12941 && ref
->u
.ar
.dimen
== 1
12942 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
12943 && ref
->u
.ar
.stride
[0] == NULL
)
12945 gfc_expr
*start
= ref
->u
.ar
.start
[0];
12946 gfc_expr
*end
= ref
->u
.ar
.end
[0];
12949 /* Optimize away the (:) reference. */
12950 if (start
== NULL
&& end
== NULL
)
12953 e
->ref
= ref
->next
;
12955 e
->ref
->next
= ref
->next
;
12960 ref
->type
= REF_SUBSTRING
;
12962 start
= gfc_get_int_expr (gfc_default_integer_kind
,
12964 ref
->u
.ss
.start
= start
;
12965 if (end
== NULL
&& e
->ts
.u
.cl
)
12966 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
12967 ref
->u
.ss
.end
= end
;
12968 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
12975 /* Any further ref is an error. */
12978 gcc_assert (ref
->type
== REF_ARRAY
);
12979 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12985 if (gfc_resolve_expr (e
) == FAILURE
)
12988 sym
= e
->symtree
->n
.sym
;
12990 if (sym
->attr
.is_protected
)
12992 if (cnt_protected
> 0 && cnt_protected
!= object
)
12994 gfc_error ("Either all or none of the objects in the "
12995 "EQUIVALENCE set at %L shall have the "
12996 "PROTECTED attribute",
13001 /* Shall not equivalence common block variables in a PURE procedure. */
13002 if (sym
->ns
->proc_name
13003 && sym
->ns
->proc_name
->attr
.pure
13004 && sym
->attr
.in_common
)
13006 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13007 "object in the pure procedure '%s'",
13008 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
13012 /* Shall not be a named constant. */
13013 if (e
->expr_type
== EXPR_CONSTANT
)
13015 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13016 "object", sym
->name
, &e
->where
);
13020 if (e
->ts
.type
== BT_DERIVED
13021 && resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
) == FAILURE
)
13024 /* Check that the types correspond correctly:
13026 A numeric sequence structure may be equivalenced to another sequence
13027 structure, an object of default integer type, default real type, double
13028 precision real type, default logical type such that components of the
13029 structure ultimately only become associated to objects of the same
13030 kind. A character sequence structure may be equivalenced to an object
13031 of default character kind or another character sequence structure.
13032 Other objects may be equivalenced only to objects of the same type and
13033 kind parameters. */
13035 /* Identical types are unconditionally OK. */
13036 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
13037 goto identical_types
;
13039 last_eq_type
= sequence_type (*last_ts
);
13040 eq_type
= sequence_type (sym
->ts
);
13042 /* Since the pair of objects is not of the same type, mixed or
13043 non-default sequences can be rejected. */
13045 msg
= "Sequence %s with mixed components in EQUIVALENCE "
13046 "statement at %L with different type objects";
13048 && last_eq_type
== SEQ_MIXED
13049 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
)
13051 || (eq_type
== SEQ_MIXED
13052 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13053 &e
->where
) == FAILURE
))
13056 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
13057 "statement at %L with objects of different type";
13059 && last_eq_type
== SEQ_NONDEFAULT
13060 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
,
13061 last_where
) == FAILURE
)
13062 || (eq_type
== SEQ_NONDEFAULT
13063 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13064 &e
->where
) == FAILURE
))
13067 msg
="Non-CHARACTER object '%s' in default CHARACTER "
13068 "EQUIVALENCE statement at %L";
13069 if (last_eq_type
== SEQ_CHARACTER
13070 && eq_type
!= SEQ_CHARACTER
13071 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13072 &e
->where
) == FAILURE
)
13075 msg
="Non-NUMERIC object '%s' in default NUMERIC "
13076 "EQUIVALENCE statement at %L";
13077 if (last_eq_type
== SEQ_NUMERIC
13078 && eq_type
!= SEQ_NUMERIC
13079 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13080 &e
->where
) == FAILURE
)
13085 last_where
= &e
->where
;
13090 /* Shall not be an automatic array. */
13091 if (e
->ref
->type
== REF_ARRAY
13092 && gfc_resolve_array_spec (e
->ref
->u
.ar
.as
, 1) == FAILURE
)
13094 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13095 "an EQUIVALENCE object", sym
->name
, &e
->where
);
13102 /* Shall not be a structure component. */
13103 if (r
->type
== REF_COMPONENT
)
13105 gfc_error ("Structure component '%s' at %L cannot be an "
13106 "EQUIVALENCE object",
13107 r
->u
.c
.component
->name
, &e
->where
);
13111 /* A substring shall not have length zero. */
13112 if (r
->type
== REF_SUBSTRING
)
13114 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
13116 gfc_error ("Substring at %L has length zero",
13117 &r
->u
.ss
.start
->where
);
13127 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13130 resolve_fntype (gfc_namespace
*ns
)
13132 gfc_entry_list
*el
;
13135 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
13138 /* If there are any entries, ns->proc_name is the entry master
13139 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13141 sym
= ns
->entries
->sym
;
13143 sym
= ns
->proc_name
;
13144 if (sym
->result
== sym
13145 && sym
->ts
.type
== BT_UNKNOWN
13146 && gfc_set_default_type (sym
, 0, NULL
) == FAILURE
13147 && !sym
->attr
.untyped
)
13149 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13150 sym
->name
, &sym
->declared_at
);
13151 sym
->attr
.untyped
= 1;
13154 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
13155 && !sym
->attr
.contained
13156 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
13157 sym
->ts
.u
.derived
->ns
->default_access
)
13158 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
13160 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC function '%s' at "
13161 "%L of PRIVATE type '%s'", sym
->name
,
13162 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
13166 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
13168 if (el
->sym
->result
== el
->sym
13169 && el
->sym
->ts
.type
== BT_UNKNOWN
13170 && gfc_set_default_type (el
->sym
, 0, NULL
) == FAILURE
13171 && !el
->sym
->attr
.untyped
)
13173 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13174 el
->sym
->name
, &el
->sym
->declared_at
);
13175 el
->sym
->attr
.untyped
= 1;
13181 /* 12.3.2.1.1 Defined operators. */
13184 check_uop_procedure (gfc_symbol
*sym
, locus where
)
13186 gfc_formal_arglist
*formal
;
13188 if (!sym
->attr
.function
)
13190 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13191 sym
->name
, &where
);
13195 if (sym
->ts
.type
== BT_CHARACTER
13196 && !(sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
)
13197 && !(sym
->result
&& sym
->result
->ts
.u
.cl
13198 && sym
->result
->ts
.u
.cl
->length
))
13200 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13201 "character length", sym
->name
, &where
);
13205 formal
= sym
->formal
;
13206 if (!formal
|| !formal
->sym
)
13208 gfc_error ("User operator procedure '%s' at %L must have at least "
13209 "one argument", sym
->name
, &where
);
13213 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13215 gfc_error ("First argument of operator interface at %L must be "
13216 "INTENT(IN)", &where
);
13220 if (formal
->sym
->attr
.optional
)
13222 gfc_error ("First argument of operator interface at %L cannot be "
13223 "optional", &where
);
13227 formal
= formal
->next
;
13228 if (!formal
|| !formal
->sym
)
13231 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13233 gfc_error ("Second argument of operator interface at %L must be "
13234 "INTENT(IN)", &where
);
13238 if (formal
->sym
->attr
.optional
)
13240 gfc_error ("Second argument of operator interface at %L cannot be "
13241 "optional", &where
);
13247 gfc_error ("Operator interface at %L must have, at most, two "
13248 "arguments", &where
);
13256 gfc_resolve_uops (gfc_symtree
*symtree
)
13258 gfc_interface
*itr
;
13260 if (symtree
== NULL
)
13263 gfc_resolve_uops (symtree
->left
);
13264 gfc_resolve_uops (symtree
->right
);
13266 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
13267 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
13271 /* Examine all of the expressions associated with a program unit,
13272 assign types to all intermediate expressions, make sure that all
13273 assignments are to compatible types and figure out which names
13274 refer to which functions or subroutines. It doesn't check code
13275 block, which is handled by resolve_code. */
13278 resolve_types (gfc_namespace
*ns
)
13284 gfc_namespace
* old_ns
= gfc_current_ns
;
13286 /* Check that all IMPLICIT types are ok. */
13287 if (!ns
->seen_implicit_none
)
13290 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
13291 if (ns
->set_flag
[letter
]
13292 && resolve_typespec_used (&ns
->default_type
[letter
],
13293 &ns
->implicit_loc
[letter
],
13298 gfc_current_ns
= ns
;
13300 resolve_entries (ns
);
13302 resolve_common_vars (ns
->blank_common
.head
, false);
13303 resolve_common_blocks (ns
->common_root
);
13305 resolve_contained_functions (ns
);
13307 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
13309 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
13310 resolve_charlen (cl
);
13312 gfc_traverse_ns (ns
, resolve_symbol
);
13314 resolve_fntype (ns
);
13316 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13318 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
13319 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13320 "also be PURE", n
->proc_name
->name
,
13321 &n
->proc_name
->declared_at
);
13327 gfc_check_interfaces (ns
);
13329 gfc_traverse_ns (ns
, resolve_values
);
13335 for (d
= ns
->data
; d
; d
= d
->next
)
13339 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
13341 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
13343 if (ns
->common_root
!= NULL
)
13344 gfc_traverse_symtree (ns
->common_root
, resolve_bind_c_comms
);
13346 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
13347 resolve_equivalence (eq
);
13349 /* Warn about unused labels. */
13350 if (warn_unused_label
)
13351 warn_unused_fortran_label (ns
->st_labels
);
13353 gfc_resolve_uops (ns
->uop_root
);
13355 gfc_current_ns
= old_ns
;
13359 /* Call resolve_code recursively. */
13362 resolve_codes (gfc_namespace
*ns
)
13365 bitmap_obstack old_obstack
;
13367 if (ns
->resolved
== 1)
13370 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13373 gfc_current_ns
= ns
;
13375 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13376 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
13379 /* Set to an out of range value. */
13380 current_entry_id
= -1;
13382 old_obstack
= labels_obstack
;
13383 bitmap_obstack_initialize (&labels_obstack
);
13385 resolve_code (ns
->code
, ns
);
13387 bitmap_obstack_release (&labels_obstack
);
13388 labels_obstack
= old_obstack
;
13392 /* This function is called after a complete program unit has been compiled.
13393 Its purpose is to examine all of the expressions associated with a program
13394 unit, assign types to all intermediate expressions, make sure that all
13395 assignments are to compatible types and figure out which names refer to
13396 which functions or subroutines. */
13399 gfc_resolve (gfc_namespace
*ns
)
13401 gfc_namespace
*old_ns
;
13402 code_stack
*old_cs_base
;
13408 old_ns
= gfc_current_ns
;
13409 old_cs_base
= cs_base
;
13411 resolve_types (ns
);
13412 resolve_codes (ns
);
13414 gfc_current_ns
= old_ns
;
13415 cs_base
= old_cs_base
;
13418 gfc_run_passes (ns
);