1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
29 #include "arith.h" /* For gfc_compare_expr(). */
30 #include "dependency.h"
32 #include "target-memory.h" /* for gfc_simplify_transfer */
33 #include "constructor.h"
35 /* Types used in equivalence statements. */
39 SEQ_NONDEFAULT
, SEQ_NUMERIC
, SEQ_CHARACTER
, SEQ_MIXED
43 /* Stack to keep track of the nesting of blocks as we move through the
44 code. See resolve_branch() and resolve_code(). */
46 typedef struct code_stack
48 struct gfc_code
*head
, *current
;
49 struct code_stack
*prev
;
51 /* This bitmap keeps track of the targets valid for a branch from
52 inside this block except for END {IF|SELECT}s of enclosing
54 bitmap reachable_labels
;
58 static code_stack
*cs_base
= NULL
;
61 /* Nonzero if we're inside a FORALL block. */
63 static int forall_flag
;
65 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
67 static int omp_workshare_flag
;
69 /* Nonzero if we are processing a formal arglist. The corresponding function
70 resets the flag each time that it is read. */
71 static int formal_arg_flag
= 0;
73 /* True if we are resolving a specification expression. */
74 static int specification_expr
= 0;
76 /* The id of the last entry seen. */
77 static int current_entry_id
;
79 /* We use bitmaps to determine if a branch target is valid. */
80 static bitmap_obstack labels_obstack
;
82 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
83 static bool inquiry_argument
= false;
86 gfc_is_formal_arg (void)
88 return formal_arg_flag
;
91 /* Is the symbol host associated? */
93 is_sym_host_assoc (gfc_symbol
*sym
, gfc_namespace
*ns
)
95 for (ns
= ns
->parent
; ns
; ns
= ns
->parent
)
104 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
105 an ABSTRACT derived-type. If where is not NULL, an error message with that
106 locus is printed, optionally using name. */
109 resolve_typespec_used (gfc_typespec
* ts
, locus
* where
, const char* name
)
111 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
->attr
.abstract
)
116 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
117 name
, where
, ts
->u
.derived
->name
);
119 gfc_error ("ABSTRACT type '%s' used at %L",
120 ts
->u
.derived
->name
, where
);
130 static void resolve_symbol (gfc_symbol
*sym
);
131 static gfc_try
resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
);
134 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
137 resolve_procedure_interface (gfc_symbol
*sym
)
139 if (sym
->ts
.interface
== sym
)
141 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
142 sym
->name
, &sym
->declared_at
);
145 if (sym
->ts
.interface
->attr
.procedure
)
147 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
148 "in a later PROCEDURE statement", sym
->ts
.interface
->name
,
149 sym
->name
, &sym
->declared_at
);
153 /* Get the attributes from the interface (now resolved). */
154 if (sym
->ts
.interface
->attr
.if_source
|| sym
->ts
.interface
->attr
.intrinsic
)
156 gfc_symbol
*ifc
= sym
->ts
.interface
;
157 resolve_symbol (ifc
);
159 if (ifc
->attr
.intrinsic
)
160 resolve_intrinsic (ifc
, &ifc
->declared_at
);
164 sym
->ts
= ifc
->result
->ts
;
169 sym
->ts
.interface
= ifc
;
170 sym
->attr
.function
= ifc
->attr
.function
;
171 sym
->attr
.subroutine
= ifc
->attr
.subroutine
;
172 gfc_copy_formal_args (sym
, ifc
);
174 sym
->attr
.allocatable
= ifc
->attr
.allocatable
;
175 sym
->attr
.pointer
= ifc
->attr
.pointer
;
176 sym
->attr
.pure
= ifc
->attr
.pure
;
177 sym
->attr
.elemental
= ifc
->attr
.elemental
;
178 sym
->attr
.dimension
= ifc
->attr
.dimension
;
179 sym
->attr
.contiguous
= ifc
->attr
.contiguous
;
180 sym
->attr
.recursive
= ifc
->attr
.recursive
;
181 sym
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
182 sym
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
183 sym
->attr
.is_bind_c
= ifc
->attr
.is_bind_c
;
184 /* Copy array spec. */
185 sym
->as
= gfc_copy_array_spec (ifc
->as
);
189 for (i
= 0; i
< sym
->as
->rank
; i
++)
191 gfc_expr_replace_symbols (sym
->as
->lower
[i
], sym
);
192 gfc_expr_replace_symbols (sym
->as
->upper
[i
], sym
);
195 /* Copy char length. */
196 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
198 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
199 gfc_expr_replace_symbols (sym
->ts
.u
.cl
->length
, sym
);
200 if (sym
->ts
.u
.cl
->length
&& !sym
->ts
.u
.cl
->resolved
201 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
205 else if (sym
->ts
.interface
->name
[0] != '\0')
207 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
208 sym
->ts
.interface
->name
, sym
->name
, &sym
->declared_at
);
216 /* Resolve types of formal argument lists. These have to be done early so that
217 the formal argument lists of module procedures can be copied to the
218 containing module before the individual procedures are resolved
219 individually. We also resolve argument lists of procedures in interface
220 blocks because they are self-contained scoping units.
222 Since a dummy argument cannot be a non-dummy procedure, the only
223 resort left for untyped names are the IMPLICIT types. */
226 resolve_formal_arglist (gfc_symbol
*proc
)
228 gfc_formal_arglist
*f
;
232 if (proc
->result
!= NULL
)
237 if (gfc_elemental (proc
)
238 || sym
->attr
.pointer
|| sym
->attr
.allocatable
239 || (sym
->as
&& sym
->as
->rank
> 0))
241 proc
->attr
.always_explicit
= 1;
242 sym
->attr
.always_explicit
= 1;
247 for (f
= proc
->formal
; f
; f
= f
->next
)
253 /* Alternate return placeholder. */
254 if (gfc_elemental (proc
))
255 gfc_error ("Alternate return specifier in elemental subroutine "
256 "'%s' at %L is not allowed", proc
->name
,
258 if (proc
->attr
.function
)
259 gfc_error ("Alternate return specifier in function "
260 "'%s' at %L is not allowed", proc
->name
,
264 else if (sym
->attr
.procedure
&& sym
->ts
.interface
265 && sym
->attr
.if_source
!= IFSRC_DECL
)
266 resolve_procedure_interface (sym
);
268 if (sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
269 resolve_formal_arglist (sym
);
271 if (sym
->attr
.subroutine
|| sym
->attr
.external
|| sym
->attr
.intrinsic
)
273 if (gfc_pure (proc
) && !gfc_pure (sym
))
275 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
276 "also be PURE", sym
->name
, &sym
->declared_at
);
280 if (proc
->attr
.implicit_pure
&& !gfc_pure(sym
))
281 proc
->attr
.implicit_pure
= 0;
283 if (gfc_elemental (proc
))
285 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
286 "procedure", &sym
->declared_at
);
290 if (sym
->attr
.function
291 && sym
->ts
.type
== BT_UNKNOWN
292 && sym
->attr
.intrinsic
)
294 gfc_intrinsic_sym
*isym
;
295 isym
= gfc_find_function (sym
->name
);
296 if (isym
== NULL
|| !isym
->specific
)
298 gfc_error ("Unable to find a specific INTRINSIC procedure "
299 "for the reference '%s' at %L", sym
->name
,
308 if (sym
->ts
.type
== BT_UNKNOWN
&& !proc
->attr
.intrinsic
309 && (!sym
->attr
.function
|| sym
->result
== sym
))
310 gfc_set_default_type (sym
, 1, sym
->ns
);
312 gfc_resolve_array_spec (sym
->as
, 0);
314 /* We can't tell if an array with dimension (:) is assumed or deferred
315 shape until we know if it has the pointer or allocatable attributes.
317 if (sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_DEFERRED
318 && !(sym
->attr
.pointer
|| sym
->attr
.allocatable
)
319 && sym
->attr
.flavor
!= FL_PROCEDURE
)
321 sym
->as
->type
= AS_ASSUMED_SHAPE
;
322 for (i
= 0; i
< sym
->as
->rank
; i
++)
323 sym
->as
->lower
[i
] = gfc_get_int_expr (gfc_default_integer_kind
,
327 if ((sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_ASSUMED_SHAPE
)
328 || sym
->attr
.pointer
|| sym
->attr
.allocatable
|| sym
->attr
.target
329 || sym
->attr
.optional
)
331 proc
->attr
.always_explicit
= 1;
333 proc
->result
->attr
.always_explicit
= 1;
336 /* If the flavor is unknown at this point, it has to be a variable.
337 A procedure specification would have already set the type. */
339 if (sym
->attr
.flavor
== FL_UNKNOWN
)
340 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, sym
->name
, &sym
->declared_at
);
342 if (gfc_pure (proc
) && !sym
->attr
.pointer
343 && sym
->attr
.flavor
!= FL_PROCEDURE
)
345 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
)
348 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Argument '%s' "
349 "of pure function '%s' at %L with VALUE "
350 "attribute but without INTENT(IN)", sym
->name
,
351 proc
->name
, &sym
->declared_at
);
353 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
354 "INTENT(IN) or VALUE", sym
->name
, proc
->name
,
358 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
)
361 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Argument '%s' "
362 "of pure subroutine '%s' at %L with VALUE "
363 "attribute but without INTENT", sym
->name
,
364 proc
->name
, &sym
->declared_at
);
366 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
367 "have its INTENT specified or have the VALUE "
368 "attribute", sym
->name
, proc
->name
, &sym
->declared_at
);
372 if (proc
->attr
.implicit_pure
&& !sym
->attr
.pointer
373 && sym
->attr
.flavor
!= FL_PROCEDURE
)
375 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
)
376 proc
->attr
.implicit_pure
= 0;
378 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
)
379 proc
->attr
.implicit_pure
= 0;
382 if (gfc_elemental (proc
))
385 if (sym
->attr
.codimension
)
387 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
388 "procedure", sym
->name
, &sym
->declared_at
);
394 gfc_error ("Argument '%s' of elemental procedure at %L must "
395 "be scalar", sym
->name
, &sym
->declared_at
);
399 if (sym
->attr
.allocatable
)
401 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
402 "have the ALLOCATABLE attribute", sym
->name
,
407 if (sym
->attr
.pointer
)
409 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
410 "have the POINTER attribute", sym
->name
,
415 if (sym
->attr
.flavor
== FL_PROCEDURE
)
417 gfc_error ("Dummy procedure '%s' not allowed in elemental "
418 "procedure '%s' at %L", sym
->name
, proc
->name
,
423 if (sym
->attr
.intent
== INTENT_UNKNOWN
)
425 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
426 "have its INTENT specified", sym
->name
, proc
->name
,
432 /* Each dummy shall be specified to be scalar. */
433 if (proc
->attr
.proc
== PROC_ST_FUNCTION
)
437 gfc_error ("Argument '%s' of statement function at %L must "
438 "be scalar", sym
->name
, &sym
->declared_at
);
442 if (sym
->ts
.type
== BT_CHARACTER
)
444 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
445 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
447 gfc_error ("Character-valued argument '%s' of statement "
448 "function at %L must have constant length",
449 sym
->name
, &sym
->declared_at
);
459 /* Work function called when searching for symbols that have argument lists
460 associated with them. */
463 find_arglists (gfc_symbol
*sym
)
465 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
|| sym
->ns
!= gfc_current_ns
)
468 resolve_formal_arglist (sym
);
472 /* Given a namespace, resolve all formal argument lists within the namespace.
476 resolve_formal_arglists (gfc_namespace
*ns
)
481 gfc_traverse_ns (ns
, find_arglists
);
486 resolve_contained_fntype (gfc_symbol
*sym
, gfc_namespace
*ns
)
490 /* If this namespace is not a function or an entry master function,
492 if (! sym
|| !(sym
->attr
.function
|| sym
->attr
.flavor
== FL_VARIABLE
)
493 || sym
->attr
.entry_master
)
496 /* Try to find out of what the return type is. */
497 if (sym
->result
->ts
.type
== BT_UNKNOWN
&& sym
->result
->ts
.interface
== NULL
)
499 t
= gfc_set_default_type (sym
->result
, 0, ns
);
501 if (t
== FAILURE
&& !sym
->result
->attr
.untyped
)
503 if (sym
->result
== sym
)
504 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
505 sym
->name
, &sym
->declared_at
);
506 else if (!sym
->result
->attr
.proc_pointer
)
507 gfc_error ("Result '%s' of contained function '%s' at %L has "
508 "no IMPLICIT type", sym
->result
->name
, sym
->name
,
509 &sym
->result
->declared_at
);
510 sym
->result
->attr
.untyped
= 1;
514 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
515 type, lists the only ways a character length value of * can be used:
516 dummy arguments of procedures, named constants, and function results
517 in external functions. Internal function results and results of module
518 procedures are not on this list, ergo, not permitted. */
520 if (sym
->result
->ts
.type
== BT_CHARACTER
)
522 gfc_charlen
*cl
= sym
->result
->ts
.u
.cl
;
523 if ((!cl
|| !cl
->length
) && !sym
->result
->ts
.deferred
)
525 /* See if this is a module-procedure and adapt error message
528 gcc_assert (ns
->parent
&& ns
->parent
->proc_name
);
529 module_proc
= (ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
);
531 gfc_error ("Character-valued %s '%s' at %L must not be"
533 module_proc
? _("module procedure")
534 : _("internal function"),
535 sym
->name
, &sym
->declared_at
);
541 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
542 introduce duplicates. */
545 merge_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
547 gfc_formal_arglist
*f
, *new_arglist
;
550 for (; new_args
!= NULL
; new_args
= new_args
->next
)
552 new_sym
= new_args
->sym
;
553 /* See if this arg is already in the formal argument list. */
554 for (f
= proc
->formal
; f
; f
= f
->next
)
556 if (new_sym
== f
->sym
)
563 /* Add a new argument. Argument order is not important. */
564 new_arglist
= gfc_get_formal_arglist ();
565 new_arglist
->sym
= new_sym
;
566 new_arglist
->next
= proc
->formal
;
567 proc
->formal
= new_arglist
;
572 /* Flag the arguments that are not present in all entries. */
575 check_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
577 gfc_formal_arglist
*f
, *head
;
580 for (f
= proc
->formal
; f
; f
= f
->next
)
585 for (new_args
= head
; new_args
; new_args
= new_args
->next
)
587 if (new_args
->sym
== f
->sym
)
594 f
->sym
->attr
.not_always_present
= 1;
599 /* Resolve alternate entry points. If a symbol has multiple entry points we
600 create a new master symbol for the main routine, and turn the existing
601 symbol into an entry point. */
604 resolve_entries (gfc_namespace
*ns
)
606 gfc_namespace
*old_ns
;
610 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
611 static int master_count
= 0;
613 if (ns
->proc_name
== NULL
)
616 /* No need to do anything if this procedure doesn't have alternate entry
621 /* We may already have resolved alternate entry points. */
622 if (ns
->proc_name
->attr
.entry_master
)
625 /* If this isn't a procedure something has gone horribly wrong. */
626 gcc_assert (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
);
628 /* Remember the current namespace. */
629 old_ns
= gfc_current_ns
;
633 /* Add the main entry point to the list of entry points. */
634 el
= gfc_get_entry_list ();
635 el
->sym
= ns
->proc_name
;
637 el
->next
= ns
->entries
;
639 ns
->proc_name
->attr
.entry
= 1;
641 /* If it is a module function, it needs to be in the right namespace
642 so that gfc_get_fake_result_decl can gather up the results. The
643 need for this arose in get_proc_name, where these beasts were
644 left in their own namespace, to keep prior references linked to
645 the entry declaration.*/
646 if (ns
->proc_name
->attr
.function
647 && ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
650 /* Do the same for entries where the master is not a module
651 procedure. These are retained in the module namespace because
652 of the module procedure declaration. */
653 for (el
= el
->next
; el
; el
= el
->next
)
654 if (el
->sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
655 && el
->sym
->attr
.mod_proc
)
659 /* Add an entry statement for it. */
666 /* Create a new symbol for the master function. */
667 /* Give the internal function a unique name (within this file).
668 Also include the function name so the user has some hope of figuring
669 out what is going on. */
670 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "master.%d.%s",
671 master_count
++, ns
->proc_name
->name
);
672 gfc_get_ha_symbol (name
, &proc
);
673 gcc_assert (proc
!= NULL
);
675 gfc_add_procedure (&proc
->attr
, PROC_INTERNAL
, proc
->name
, NULL
);
676 if (ns
->proc_name
->attr
.subroutine
)
677 gfc_add_subroutine (&proc
->attr
, proc
->name
, NULL
);
681 gfc_typespec
*ts
, *fts
;
682 gfc_array_spec
*as
, *fas
;
683 gfc_add_function (&proc
->attr
, proc
->name
, NULL
);
685 fas
= ns
->entries
->sym
->as
;
686 fas
= fas
? fas
: ns
->entries
->sym
->result
->as
;
687 fts
= &ns
->entries
->sym
->result
->ts
;
688 if (fts
->type
== BT_UNKNOWN
)
689 fts
= gfc_get_default_type (ns
->entries
->sym
->result
->name
, NULL
);
690 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
692 ts
= &el
->sym
->result
->ts
;
694 as
= as
? as
: el
->sym
->result
->as
;
695 if (ts
->type
== BT_UNKNOWN
)
696 ts
= gfc_get_default_type (el
->sym
->result
->name
, NULL
);
698 if (! gfc_compare_types (ts
, fts
)
699 || (el
->sym
->result
->attr
.dimension
700 != ns
->entries
->sym
->result
->attr
.dimension
)
701 || (el
->sym
->result
->attr
.pointer
702 != ns
->entries
->sym
->result
->attr
.pointer
))
704 else if (as
&& fas
&& ns
->entries
->sym
->result
!= el
->sym
->result
705 && gfc_compare_array_spec (as
, fas
) == 0)
706 gfc_error ("Function %s at %L has entries with mismatched "
707 "array specifications", ns
->entries
->sym
->name
,
708 &ns
->entries
->sym
->declared_at
);
709 /* The characteristics need to match and thus both need to have
710 the same string length, i.e. both len=*, or both len=4.
711 Having both len=<variable> is also possible, but difficult to
712 check at compile time. */
713 else if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& fts
->u
.cl
714 && (((ts
->u
.cl
->length
&& !fts
->u
.cl
->length
)
715 ||(!ts
->u
.cl
->length
&& fts
->u
.cl
->length
))
717 && ts
->u
.cl
->length
->expr_type
718 != fts
->u
.cl
->length
->expr_type
)
720 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
721 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
722 fts
->u
.cl
->length
->value
.integer
) != 0)))
723 gfc_notify_std (GFC_STD_GNU
, "Extension: Function %s at %L with "
724 "entries returning variables of different "
725 "string lengths", ns
->entries
->sym
->name
,
726 &ns
->entries
->sym
->declared_at
);
731 sym
= ns
->entries
->sym
->result
;
732 /* All result types the same. */
734 if (sym
->attr
.dimension
)
735 gfc_set_array_spec (proc
, gfc_copy_array_spec (sym
->as
), NULL
);
736 if (sym
->attr
.pointer
)
737 gfc_add_pointer (&proc
->attr
, NULL
);
741 /* Otherwise the result will be passed through a union by
743 proc
->attr
.mixed_entry_master
= 1;
744 for (el
= ns
->entries
; el
; el
= el
->next
)
746 sym
= el
->sym
->result
;
747 if (sym
->attr
.dimension
)
749 if (el
== ns
->entries
)
750 gfc_error ("FUNCTION result %s can't be an array in "
751 "FUNCTION %s at %L", sym
->name
,
752 ns
->entries
->sym
->name
, &sym
->declared_at
);
754 gfc_error ("ENTRY result %s can't be an array in "
755 "FUNCTION %s at %L", sym
->name
,
756 ns
->entries
->sym
->name
, &sym
->declared_at
);
758 else if (sym
->attr
.pointer
)
760 if (el
== ns
->entries
)
761 gfc_error ("FUNCTION result %s can't be a POINTER in "
762 "FUNCTION %s at %L", sym
->name
,
763 ns
->entries
->sym
->name
, &sym
->declared_at
);
765 gfc_error ("ENTRY result %s can't be a POINTER in "
766 "FUNCTION %s at %L", sym
->name
,
767 ns
->entries
->sym
->name
, &sym
->declared_at
);
772 if (ts
->type
== BT_UNKNOWN
)
773 ts
= gfc_get_default_type (sym
->name
, NULL
);
777 if (ts
->kind
== gfc_default_integer_kind
)
781 if (ts
->kind
== gfc_default_real_kind
782 || ts
->kind
== gfc_default_double_kind
)
786 if (ts
->kind
== gfc_default_complex_kind
)
790 if (ts
->kind
== gfc_default_logical_kind
)
794 /* We will issue error elsewhere. */
802 if (el
== ns
->entries
)
803 gfc_error ("FUNCTION result %s can't be of type %s "
804 "in FUNCTION %s at %L", sym
->name
,
805 gfc_typename (ts
), ns
->entries
->sym
->name
,
808 gfc_error ("ENTRY result %s can't be of type %s "
809 "in FUNCTION %s at %L", sym
->name
,
810 gfc_typename (ts
), ns
->entries
->sym
->name
,
817 proc
->attr
.access
= ACCESS_PRIVATE
;
818 proc
->attr
.entry_master
= 1;
820 /* Merge all the entry point arguments. */
821 for (el
= ns
->entries
; el
; el
= el
->next
)
822 merge_argument_lists (proc
, el
->sym
->formal
);
824 /* Check the master formal arguments for any that are not
825 present in all entry points. */
826 for (el
= ns
->entries
; el
; el
= el
->next
)
827 check_argument_lists (proc
, el
->sym
->formal
);
829 /* Use the master function for the function body. */
830 ns
->proc_name
= proc
;
832 /* Finalize the new symbols. */
833 gfc_commit_symbols ();
835 /* Restore the original namespace. */
836 gfc_current_ns
= old_ns
;
840 /* Resolve common variables. */
842 resolve_common_vars (gfc_symbol
*sym
, bool named_common
)
844 gfc_symbol
*csym
= sym
;
846 for (; csym
; csym
= csym
->common_next
)
848 if (csym
->value
|| csym
->attr
.data
)
850 if (!csym
->ns
->is_block_data
)
851 gfc_notify_std (GFC_STD_GNU
, "Variable '%s' at %L is in COMMON "
852 "but only in BLOCK DATA initialization is "
853 "allowed", csym
->name
, &csym
->declared_at
);
854 else if (!named_common
)
855 gfc_notify_std (GFC_STD_GNU
, "Initialized variable '%s' at %L is "
856 "in a blank COMMON but initialization is only "
857 "allowed in named common blocks", csym
->name
,
861 if (csym
->ts
.type
!= BT_DERIVED
)
864 if (!(csym
->ts
.u
.derived
->attr
.sequence
865 || csym
->ts
.u
.derived
->attr
.is_bind_c
))
866 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
867 "has neither the SEQUENCE nor the BIND(C) "
868 "attribute", csym
->name
, &csym
->declared_at
);
869 if (csym
->ts
.u
.derived
->attr
.alloc_comp
)
870 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
871 "has an ultimate component that is "
872 "allocatable", csym
->name
, &csym
->declared_at
);
873 if (gfc_has_default_initializer (csym
->ts
.u
.derived
))
874 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
875 "may not have default initializer", csym
->name
,
878 if (csym
->attr
.flavor
== FL_UNKNOWN
&& !csym
->attr
.proc_pointer
)
879 gfc_add_flavor (&csym
->attr
, FL_VARIABLE
, csym
->name
, &csym
->declared_at
);
883 /* Resolve common blocks. */
885 resolve_common_blocks (gfc_symtree
*common_root
)
889 if (common_root
== NULL
)
892 if (common_root
->left
)
893 resolve_common_blocks (common_root
->left
);
894 if (common_root
->right
)
895 resolve_common_blocks (common_root
->right
);
897 resolve_common_vars (common_root
->n
.common
->head
, true);
899 gfc_find_symbol (common_root
->name
, gfc_current_ns
, 0, &sym
);
903 if (sym
->attr
.flavor
== FL_PARAMETER
)
904 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
905 sym
->name
, &common_root
->n
.common
->where
, &sym
->declared_at
);
907 if (sym
->attr
.intrinsic
)
908 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
909 sym
->name
, &common_root
->n
.common
->where
);
910 else if (sym
->attr
.result
911 || gfc_is_function_return_value (sym
, gfc_current_ns
))
912 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
913 "that is also a function result", sym
->name
,
914 &common_root
->n
.common
->where
);
915 else if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_INTERNAL
916 && sym
->attr
.proc
!= PROC_ST_FUNCTION
)
917 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
918 "that is also a global procedure", sym
->name
,
919 &common_root
->n
.common
->where
);
923 /* Resolve contained function types. Because contained functions can call one
924 another, they have to be worked out before any of the contained procedures
927 The good news is that if a function doesn't already have a type, the only
928 way it can get one is through an IMPLICIT type or a RESULT variable, because
929 by definition contained functions are contained namespace they're contained
930 in, not in a sibling or parent namespace. */
933 resolve_contained_functions (gfc_namespace
*ns
)
935 gfc_namespace
*child
;
938 resolve_formal_arglists (ns
);
940 for (child
= ns
->contained
; child
; child
= child
->sibling
)
942 /* Resolve alternate entry points first. */
943 resolve_entries (child
);
945 /* Then check function return types. */
946 resolve_contained_fntype (child
->proc_name
, child
);
947 for (el
= child
->entries
; el
; el
= el
->next
)
948 resolve_contained_fntype (el
->sym
, child
);
953 /* Resolve all of the elements of a structure constructor and make sure that
954 the types are correct. The 'init' flag indicates that the given
955 constructor is an initializer. */
958 resolve_structure_cons (gfc_expr
*expr
, int init
)
960 gfc_constructor
*cons
;
967 if (expr
->ts
.type
== BT_DERIVED
)
968 resolve_symbol (expr
->ts
.u
.derived
);
970 cons
= gfc_constructor_first (expr
->value
.constructor
);
971 /* A constructor may have references if it is the result of substituting a
972 parameter variable. In this case we just pull out the component we
975 comp
= expr
->ref
->u
.c
.sym
->components
;
977 comp
= expr
->ts
.u
.derived
->components
;
979 /* See if the user is trying to invoke a structure constructor for one of
980 the iso_c_binding derived types. */
981 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
982 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
983 && (cons
->expr
== NULL
|| cons
->expr
->expr_type
!= EXPR_NULL
))
985 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
986 expr
->ts
.u
.derived
->name
, &(expr
->where
));
990 /* Return if structure constructor is c_null_(fun)prt. */
991 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
992 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
993 && cons
->expr
&& cons
->expr
->expr_type
== EXPR_NULL
)
996 for (; comp
&& cons
; comp
= comp
->next
, cons
= gfc_constructor_next (cons
))
1003 if (gfc_resolve_expr (cons
->expr
) == FAILURE
)
1009 rank
= comp
->as
? comp
->as
->rank
: 0;
1010 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
1011 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
1013 gfc_error ("The rank of the element in the derived type "
1014 "constructor at %L does not match that of the "
1015 "component (%d/%d)", &cons
->expr
->where
,
1016 cons
->expr
->rank
, rank
);
1020 /* If we don't have the right type, try to convert it. */
1022 if (!comp
->attr
.proc_pointer
&&
1023 !gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
1026 if (strcmp (comp
->name
, "_extends") == 0)
1028 /* Can afford to be brutal with the _extends initializer.
1029 The derived type can get lost because it is PRIVATE
1030 but it is not usage constrained by the standard. */
1031 cons
->expr
->ts
= comp
->ts
;
1034 else if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
1035 gfc_error ("The element in the derived type constructor at %L, "
1036 "for pointer component '%s', is %s but should be %s",
1037 &cons
->expr
->where
, comp
->name
,
1038 gfc_basic_typename (cons
->expr
->ts
.type
),
1039 gfc_basic_typename (comp
->ts
.type
));
1041 t
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
1044 /* For strings, the length of the constructor should be the same as
1045 the one of the structure, ensure this if the lengths are known at
1046 compile time and when we are dealing with PARAMETER or structure
1048 if (cons
->expr
->ts
.type
== BT_CHARACTER
&& comp
->ts
.u
.cl
1049 && comp
->ts
.u
.cl
->length
1050 && comp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1051 && cons
->expr
->ts
.u
.cl
&& cons
->expr
->ts
.u
.cl
->length
1052 && cons
->expr
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1053 && mpz_cmp (cons
->expr
->ts
.u
.cl
->length
->value
.integer
,
1054 comp
->ts
.u
.cl
->length
->value
.integer
) != 0)
1056 if (cons
->expr
->expr_type
== EXPR_VARIABLE
1057 && cons
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
1059 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1060 to make use of the gfc_resolve_character_array_constructor
1061 machinery. The expression is later simplified away to
1062 an array of string literals. */
1063 gfc_expr
*para
= cons
->expr
;
1064 cons
->expr
= gfc_get_expr ();
1065 cons
->expr
->ts
= para
->ts
;
1066 cons
->expr
->where
= para
->where
;
1067 cons
->expr
->expr_type
= EXPR_ARRAY
;
1068 cons
->expr
->rank
= para
->rank
;
1069 cons
->expr
->shape
= gfc_copy_shape (para
->shape
, para
->rank
);
1070 gfc_constructor_append_expr (&cons
->expr
->value
.constructor
,
1071 para
, &cons
->expr
->where
);
1073 if (cons
->expr
->expr_type
== EXPR_ARRAY
)
1076 p
= gfc_constructor_first (cons
->expr
->value
.constructor
);
1077 if (cons
->expr
->ts
.u
.cl
!= p
->expr
->ts
.u
.cl
)
1079 gfc_charlen
*cl
, *cl2
;
1082 for (cl
= gfc_current_ns
->cl_list
; cl
; cl
= cl
->next
)
1084 if (cl
== cons
->expr
->ts
.u
.cl
)
1092 cl2
->next
= cl
->next
;
1094 gfc_free_expr (cl
->length
);
1098 cons
->expr
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1099 cons
->expr
->ts
.u
.cl
->length_from_typespec
= true;
1100 cons
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (comp
->ts
.u
.cl
->length
);
1101 gfc_resolve_character_array_constructor (cons
->expr
);
1105 if (cons
->expr
->expr_type
== EXPR_NULL
1106 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
1107 || comp
->attr
.proc_pointer
1108 || (comp
->ts
.type
== BT_CLASS
1109 && (CLASS_DATA (comp
)->attr
.class_pointer
1110 || CLASS_DATA (comp
)->attr
.allocatable
))))
1113 gfc_error ("The NULL in the derived type constructor at %L is "
1114 "being applied to component '%s', which is neither "
1115 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
1119 if (!comp
->attr
.pointer
|| comp
->attr
.proc_pointer
1120 || cons
->expr
->expr_type
== EXPR_NULL
)
1123 a
= gfc_expr_attr (cons
->expr
);
1125 if (!a
.pointer
&& !a
.target
)
1128 gfc_error ("The element in the derived type constructor at %L, "
1129 "for pointer component '%s' should be a POINTER or "
1130 "a TARGET", &cons
->expr
->where
, comp
->name
);
1135 /* F08:C461. Additional checks for pointer initialization. */
1139 gfc_error ("Pointer initialization target at %L "
1140 "must not be ALLOCATABLE ", &cons
->expr
->where
);
1145 gfc_error ("Pointer initialization target at %L "
1146 "must have the SAVE attribute", &cons
->expr
->where
);
1150 /* F2003, C1272 (3). */
1151 if (gfc_pure (NULL
) && cons
->expr
->expr_type
== EXPR_VARIABLE
1152 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1153 || gfc_is_coindexed (cons
->expr
)))
1156 gfc_error ("Invalid expression in the derived type constructor for "
1157 "pointer component '%s' at %L in PURE procedure",
1158 comp
->name
, &cons
->expr
->where
);
1161 if (gfc_implicit_pure (NULL
)
1162 && cons
->expr
->expr_type
== EXPR_VARIABLE
1163 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1164 || gfc_is_coindexed (cons
->expr
)))
1165 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
1173 /****************** Expression name resolution ******************/
1175 /* Returns 0 if a symbol was not declared with a type or
1176 attribute declaration statement, nonzero otherwise. */
1179 was_declared (gfc_symbol
*sym
)
1185 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
1188 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
1189 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
1190 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
1191 || a
.asynchronous
|| a
.codimension
)
1198 /* Determine if a symbol is generic or not. */
1201 generic_sym (gfc_symbol
*sym
)
1205 if (sym
->attr
.generic
||
1206 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
1209 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1212 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1219 return generic_sym (s
);
1226 /* Determine if a symbol is specific or not. */
1229 specific_sym (gfc_symbol
*sym
)
1233 if (sym
->attr
.if_source
== IFSRC_IFBODY
1234 || sym
->attr
.proc
== PROC_MODULE
1235 || sym
->attr
.proc
== PROC_INTERNAL
1236 || sym
->attr
.proc
== PROC_ST_FUNCTION
1237 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
1238 || sym
->attr
.external
)
1241 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1244 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1246 return (s
== NULL
) ? 0 : specific_sym (s
);
1250 /* Figure out if the procedure is specific, generic or unknown. */
1253 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
}
1257 procedure_kind (gfc_symbol
*sym
)
1259 if (generic_sym (sym
))
1260 return PTYPE_GENERIC
;
1262 if (specific_sym (sym
))
1263 return PTYPE_SPECIFIC
;
1265 return PTYPE_UNKNOWN
;
1268 /* Check references to assumed size arrays. The flag need_full_assumed_size
1269 is nonzero when matching actual arguments. */
1271 static int need_full_assumed_size
= 0;
1274 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1276 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1279 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1280 What should it be? */
1281 if ((e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1282 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1283 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1285 gfc_error ("The upper bound in the last dimension must "
1286 "appear in the reference to the assumed size "
1287 "array '%s' at %L", sym
->name
, &e
->where
);
1294 /* Look for bad assumed size array references in argument expressions
1295 of elemental and array valued intrinsic procedures. Since this is
1296 called from procedure resolution functions, it only recurses at
1300 resolve_assumed_size_actual (gfc_expr
*e
)
1305 switch (e
->expr_type
)
1308 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1313 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1314 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1325 /* Check a generic procedure, passed as an actual argument, to see if
1326 there is a matching specific name. If none, it is an error, and if
1327 more than one, the reference is ambiguous. */
1329 count_specific_procs (gfc_expr
*e
)
1336 sym
= e
->symtree
->n
.sym
;
1338 for (p
= sym
->generic
; p
; p
= p
->next
)
1339 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1341 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1347 gfc_error ("'%s' at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1351 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1352 "argument at %L", sym
->name
, &e
->where
);
1358 /* See if a call to sym could possibly be a not allowed RECURSION because of
1359 a missing RECURIVE declaration. This means that either sym is the current
1360 context itself, or sym is the parent of a contained procedure calling its
1361 non-RECURSIVE containing procedure.
1362 This also works if sym is an ENTRY. */
1365 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1367 gfc_symbol
* proc_sym
;
1368 gfc_symbol
* context_proc
;
1369 gfc_namespace
* real_context
;
1371 if (sym
->attr
.flavor
== FL_PROGRAM
)
1374 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
);
1376 /* If we've got an ENTRY, find real procedure. */
1377 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1378 proc_sym
= sym
->ns
->entries
->sym
;
1382 /* If sym is RECURSIVE, all is well of course. */
1383 if (proc_sym
->attr
.recursive
|| gfc_option
.flag_recursive
)
1386 /* Find the context procedure's "real" symbol if it has entries.
1387 We look for a procedure symbol, so recurse on the parents if we don't
1388 find one (like in case of a BLOCK construct). */
1389 for (real_context
= context
; ; real_context
= real_context
->parent
)
1391 /* We should find something, eventually! */
1392 gcc_assert (real_context
);
1394 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1395 : real_context
->proc_name
);
1397 /* In some special cases, there may not be a proc_name, like for this
1399 real(bad_kind()) function foo () ...
1400 when checking the call to bad_kind ().
1401 In these cases, we simply return here and assume that the
1406 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1410 /* A call from sym's body to itself is recursion, of course. */
1411 if (context_proc
== proc_sym
)
1414 /* The same is true if context is a contained procedure and sym the
1416 if (context_proc
->attr
.contained
)
1418 gfc_symbol
* parent_proc
;
1420 gcc_assert (context
->parent
);
1421 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1422 : context
->parent
->proc_name
);
1424 if (parent_proc
== proc_sym
)
1432 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1433 its typespec and formal argument list. */
1436 resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1438 gfc_intrinsic_sym
* isym
= NULL
;
1444 /* Already resolved. */
1445 if (sym
->from_intmod
&& sym
->ts
.type
!= BT_UNKNOWN
)
1448 /* We already know this one is an intrinsic, so we don't call
1449 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1450 gfc_find_subroutine directly to check whether it is a function or
1453 if (sym
->intmod_sym_id
)
1454 isym
= gfc_intrinsic_function_by_id ((gfc_isym_id
) sym
->intmod_sym_id
);
1456 isym
= gfc_find_function (sym
->name
);
1460 if (sym
->ts
.type
!= BT_UNKNOWN
&& gfc_option
.warn_surprising
1461 && !sym
->attr
.implicit_type
)
1462 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1463 " ignored", sym
->name
, &sym
->declared_at
);
1465 if (!sym
->attr
.function
&&
1466 gfc_add_function (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1471 else if ((isym
= gfc_find_subroutine (sym
->name
)))
1473 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1475 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1476 " specifier", sym
->name
, &sym
->declared_at
);
1480 if (!sym
->attr
.subroutine
&&
1481 gfc_add_subroutine (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1486 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym
->name
,
1491 gfc_copy_formal_args_intr (sym
, isym
);
1493 /* Check it is actually available in the standard settings. */
1494 if (gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
)
1497 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1498 " available in the current standard settings but %s. Use"
1499 " an appropriate -std=* option or enable -fall-intrinsics"
1500 " in order to use it.",
1501 sym
->name
, &sym
->declared_at
, symstd
);
1509 /* Resolve a procedure expression, like passing it to a called procedure or as
1510 RHS for a procedure pointer assignment. */
1513 resolve_procedure_expression (gfc_expr
* expr
)
1517 if (expr
->expr_type
!= EXPR_VARIABLE
)
1519 gcc_assert (expr
->symtree
);
1521 sym
= expr
->symtree
->n
.sym
;
1523 if (sym
->attr
.intrinsic
)
1524 resolve_intrinsic (sym
, &expr
->where
);
1526 if (sym
->attr
.flavor
!= FL_PROCEDURE
1527 || (sym
->attr
.function
&& sym
->result
== sym
))
1530 /* A non-RECURSIVE procedure that is used as procedure expression within its
1531 own body is in danger of being called recursively. */
1532 if (is_illegal_recursion (sym
, gfc_current_ns
))
1533 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1534 " itself recursively. Declare it RECURSIVE or use"
1535 " -frecursive", sym
->name
, &expr
->where
);
1541 /* Resolve an actual argument list. Most of the time, this is just
1542 resolving the expressions in the list.
1543 The exception is that we sometimes have to decide whether arguments
1544 that look like procedure arguments are really simple variable
1548 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1549 bool no_formal_args
)
1552 gfc_symtree
*parent_st
;
1554 int save_need_full_assumed_size
;
1556 for (; arg
; arg
= arg
->next
)
1561 /* Check the label is a valid branching target. */
1564 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1566 gfc_error ("Label %d referenced at %L is never defined",
1567 arg
->label
->value
, &arg
->label
->where
);
1574 if (e
->expr_type
== EXPR_VARIABLE
1575 && e
->symtree
->n
.sym
->attr
.generic
1577 && count_specific_procs (e
) != 1)
1580 if (e
->ts
.type
!= BT_PROCEDURE
)
1582 save_need_full_assumed_size
= need_full_assumed_size
;
1583 if (e
->expr_type
!= EXPR_VARIABLE
)
1584 need_full_assumed_size
= 0;
1585 if (gfc_resolve_expr (e
) != SUCCESS
)
1587 need_full_assumed_size
= save_need_full_assumed_size
;
1591 /* See if the expression node should really be a variable reference. */
1593 sym
= e
->symtree
->n
.sym
;
1595 if (sym
->attr
.flavor
== FL_PROCEDURE
1596 || sym
->attr
.intrinsic
1597 || sym
->attr
.external
)
1601 /* If a procedure is not already determined to be something else
1602 check if it is intrinsic. */
1603 if (!sym
->attr
.intrinsic
1604 && !(sym
->attr
.external
|| sym
->attr
.use_assoc
1605 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1606 && gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1607 sym
->attr
.intrinsic
= 1;
1609 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1611 gfc_error ("Statement function '%s' at %L is not allowed as an "
1612 "actual argument", sym
->name
, &e
->where
);
1615 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1616 sym
->attr
.subroutine
);
1617 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1619 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1620 "actual argument", sym
->name
, &e
->where
);
1623 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1624 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1626 if (gfc_notify_std (GFC_STD_F2008
,
1627 "Fortran 2008: Internal procedure '%s' is"
1628 " used as actual argument at %L",
1629 sym
->name
, &e
->where
) == FAILURE
)
1633 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1635 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1636 "allowed as an actual argument at %L", sym
->name
,
1640 /* Check if a generic interface has a specific procedure
1641 with the same name before emitting an error. */
1642 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
1645 /* Just in case a specific was found for the expression. */
1646 sym
= e
->symtree
->n
.sym
;
1648 /* If the symbol is the function that names the current (or
1649 parent) scope, then we really have a variable reference. */
1651 if (gfc_is_function_return_value (sym
, sym
->ns
))
1654 /* If all else fails, see if we have a specific intrinsic. */
1655 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
1657 gfc_intrinsic_sym
*isym
;
1659 isym
= gfc_find_function (sym
->name
);
1660 if (isym
== NULL
|| !isym
->specific
)
1662 gfc_error ("Unable to find a specific INTRINSIC procedure "
1663 "for the reference '%s' at %L", sym
->name
,
1668 sym
->attr
.intrinsic
= 1;
1669 sym
->attr
.function
= 1;
1672 if (gfc_resolve_expr (e
) == FAILURE
)
1677 /* See if the name is a module procedure in a parent unit. */
1679 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1682 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
1684 gfc_error ("Symbol '%s' at %L is ambiguous", sym
->name
, &e
->where
);
1688 if (parent_st
== NULL
)
1691 sym
= parent_st
->n
.sym
;
1692 e
->symtree
= parent_st
; /* Point to the right thing. */
1694 if (sym
->attr
.flavor
== FL_PROCEDURE
1695 || sym
->attr
.intrinsic
1696 || sym
->attr
.external
)
1698 if (gfc_resolve_expr (e
) == FAILURE
)
1704 e
->expr_type
= EXPR_VARIABLE
;
1706 if (sym
->as
!= NULL
)
1708 e
->rank
= sym
->as
->rank
;
1709 e
->ref
= gfc_get_ref ();
1710 e
->ref
->type
= REF_ARRAY
;
1711 e
->ref
->u
.ar
.type
= AR_FULL
;
1712 e
->ref
->u
.ar
.as
= sym
->as
;
1715 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1716 primary.c (match_actual_arg). If above code determines that it
1717 is a variable instead, it needs to be resolved as it was not
1718 done at the beginning of this function. */
1719 save_need_full_assumed_size
= need_full_assumed_size
;
1720 if (e
->expr_type
!= EXPR_VARIABLE
)
1721 need_full_assumed_size
= 0;
1722 if (gfc_resolve_expr (e
) != SUCCESS
)
1724 need_full_assumed_size
= save_need_full_assumed_size
;
1727 /* Check argument list functions %VAL, %LOC and %REF. There is
1728 nothing to do for %REF. */
1729 if (arg
->name
&& arg
->name
[0] == '%')
1731 if (strncmp ("%VAL", arg
->name
, 4) == 0)
1733 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
1735 gfc_error ("By-value argument at %L is not of numeric "
1742 gfc_error ("By-value argument at %L cannot be an array or "
1743 "an array section", &e
->where
);
1747 /* Intrinsics are still PROC_UNKNOWN here. However,
1748 since same file external procedures are not resolvable
1749 in gfortran, it is a good deal easier to leave them to
1751 if (ptype
!= PROC_UNKNOWN
1752 && ptype
!= PROC_DUMMY
1753 && ptype
!= PROC_EXTERNAL
1754 && ptype
!= PROC_MODULE
)
1756 gfc_error ("By-value argument at %L is not allowed "
1757 "in this context", &e
->where
);
1762 /* Statement functions have already been excluded above. */
1763 else if (strncmp ("%LOC", arg
->name
, 4) == 0
1764 && e
->ts
.type
== BT_PROCEDURE
)
1766 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
1768 gfc_error ("Passing internal procedure at %L by location "
1769 "not allowed", &e
->where
);
1775 /* Fortran 2008, C1237. */
1776 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_is_coindexed (e
)
1777 && gfc_has_ultimate_pointer (e
))
1779 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1780 "component", &e
->where
);
1789 /* Do the checks of the actual argument list that are specific to elemental
1790 procedures. If called with c == NULL, we have a function, otherwise if
1791 expr == NULL, we have a subroutine. */
1794 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
1796 gfc_actual_arglist
*arg0
;
1797 gfc_actual_arglist
*arg
;
1798 gfc_symbol
*esym
= NULL
;
1799 gfc_intrinsic_sym
*isym
= NULL
;
1801 gfc_intrinsic_arg
*iformal
= NULL
;
1802 gfc_formal_arglist
*eformal
= NULL
;
1803 bool formal_optional
= false;
1804 bool set_by_optional
= false;
1808 /* Is this an elemental procedure? */
1809 if (expr
&& expr
->value
.function
.actual
!= NULL
)
1811 if (expr
->value
.function
.esym
!= NULL
1812 && expr
->value
.function
.esym
->attr
.elemental
)
1814 arg0
= expr
->value
.function
.actual
;
1815 esym
= expr
->value
.function
.esym
;
1817 else if (expr
->value
.function
.isym
!= NULL
1818 && expr
->value
.function
.isym
->elemental
)
1820 arg0
= expr
->value
.function
.actual
;
1821 isym
= expr
->value
.function
.isym
;
1826 else if (c
&& c
->ext
.actual
!= NULL
)
1828 arg0
= c
->ext
.actual
;
1830 if (c
->resolved_sym
)
1831 esym
= c
->resolved_sym
;
1833 esym
= c
->symtree
->n
.sym
;
1836 if (!esym
->attr
.elemental
)
1842 /* The rank of an elemental is the rank of its array argument(s). */
1843 for (arg
= arg0
; arg
; arg
= arg
->next
)
1845 if (arg
->expr
!= NULL
&& arg
->expr
->rank
> 0)
1847 rank
= arg
->expr
->rank
;
1848 if (arg
->expr
->expr_type
== EXPR_VARIABLE
1849 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
1850 set_by_optional
= true;
1852 /* Function specific; set the result rank and shape. */
1856 if (!expr
->shape
&& arg
->expr
->shape
)
1858 expr
->shape
= gfc_get_shape (rank
);
1859 for (i
= 0; i
< rank
; i
++)
1860 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
1867 /* If it is an array, it shall not be supplied as an actual argument
1868 to an elemental procedure unless an array of the same rank is supplied
1869 as an actual argument corresponding to a nonoptional dummy argument of
1870 that elemental procedure(12.4.1.5). */
1871 formal_optional
= false;
1873 iformal
= isym
->formal
;
1875 eformal
= esym
->formal
;
1877 for (arg
= arg0
; arg
; arg
= arg
->next
)
1881 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
1882 formal_optional
= true;
1883 eformal
= eformal
->next
;
1885 else if (isym
&& iformal
)
1887 if (iformal
->optional
)
1888 formal_optional
= true;
1889 iformal
= iformal
->next
;
1892 formal_optional
= true;
1894 if (pedantic
&& arg
->expr
!= NULL
1895 && arg
->expr
->expr_type
== EXPR_VARIABLE
1896 && arg
->expr
->symtree
->n
.sym
->attr
.optional
1899 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
1900 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
1902 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1903 "MISSING, it cannot be the actual argument of an "
1904 "ELEMENTAL procedure unless there is a non-optional "
1905 "argument with the same rank (12.4.1.5)",
1906 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
1911 for (arg
= arg0
; arg
; arg
= arg
->next
)
1913 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
1916 /* Being elemental, the last upper bound of an assumed size array
1917 argument must be present. */
1918 if (resolve_assumed_size_actual (arg
->expr
))
1921 /* Elemental procedure's array actual arguments must conform. */
1924 if (gfc_check_conformance (arg
->expr
, e
,
1925 "elemental procedure") == FAILURE
)
1932 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1933 is an array, the intent inout/out variable needs to be also an array. */
1934 if (rank
> 0 && esym
&& expr
== NULL
)
1935 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
1936 arg
= arg
->next
, eformal
= eformal
->next
)
1937 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
1938 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
1939 && arg
->expr
&& arg
->expr
->rank
== 0)
1941 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1942 "ELEMENTAL subroutine '%s' is a scalar, but another "
1943 "actual argument is an array", &arg
->expr
->where
,
1944 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
1945 : "INOUT", eformal
->sym
->name
, esym
->name
);
1952 /* This function does the checking of references to global procedures
1953 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1954 77 and 95 standards. It checks for a gsymbol for the name, making
1955 one if it does not already exist. If it already exists, then the
1956 reference being resolved must correspond to the type of gsymbol.
1957 Otherwise, the new symbol is equipped with the attributes of the
1958 reference. The corresponding code that is called in creating
1959 global entities is parse.c.
1961 In addition, for all but -std=legacy, the gsymbols are used to
1962 check the interfaces of external procedures from the same file.
1963 The namespace of the gsymbol is resolved and then, once this is
1964 done the interface is checked. */
1968 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1970 if (!gsym_ns
->proc_name
->attr
.recursive
)
1973 if (sym
->ns
== gsym_ns
)
1976 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
1983 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1985 if (gsym_ns
->entries
)
1987 gfc_entry_list
*entry
= gsym_ns
->entries
;
1989 for (; entry
; entry
= entry
->next
)
1991 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
1993 if (strcmp (gsym_ns
->proc_name
->name
,
1994 sym
->ns
->proc_name
->name
) == 0)
1998 && strcmp (gsym_ns
->proc_name
->name
,
1999 sym
->ns
->parent
->proc_name
->name
) == 0)
2008 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
,
2009 gfc_actual_arglist
**actual
, int sub
)
2013 enum gfc_symbol_type type
;
2015 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
2017 gsym
= gfc_get_gsymbol (sym
->name
);
2019 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
2020 gfc_global_used (gsym
, where
);
2022 if (gfc_option
.flag_whole_file
2023 && (sym
->attr
.if_source
== IFSRC_UNKNOWN
2024 || sym
->attr
.if_source
== IFSRC_IFBODY
)
2025 && gsym
->type
!= GSYM_UNKNOWN
2027 && gsym
->ns
->resolved
!= -1
2028 && gsym
->ns
->proc_name
2029 && not_in_recursive (sym
, gsym
->ns
)
2030 && not_entry_self_reference (sym
, gsym
->ns
))
2032 gfc_symbol
*def_sym
;
2034 /* Resolve the gsymbol namespace if needed. */
2035 if (!gsym
->ns
->resolved
)
2037 gfc_dt_list
*old_dt_list
;
2038 struct gfc_omp_saved_state old_omp_state
;
2040 /* Stash away derived types so that the backend_decls do not
2042 old_dt_list
= gfc_derived_types
;
2043 gfc_derived_types
= NULL
;
2044 /* And stash away openmp state. */
2045 gfc_omp_save_and_clear_state (&old_omp_state
);
2047 gfc_resolve (gsym
->ns
);
2049 /* Store the new derived types with the global namespace. */
2050 if (gfc_derived_types
)
2051 gsym
->ns
->derived_types
= gfc_derived_types
;
2053 /* Restore the derived types of this namespace. */
2054 gfc_derived_types
= old_dt_list
;
2055 /* And openmp state. */
2056 gfc_omp_restore_state (&old_omp_state
);
2059 /* Make sure that translation for the gsymbol occurs before
2060 the procedure currently being resolved. */
2061 ns
= gfc_global_ns_list
;
2062 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
2064 if (ns
->sibling
== gsym
->ns
)
2066 ns
->sibling
= gsym
->ns
->sibling
;
2067 gsym
->ns
->sibling
= gfc_global_ns_list
;
2068 gfc_global_ns_list
= gsym
->ns
;
2073 def_sym
= gsym
->ns
->proc_name
;
2074 if (def_sym
->attr
.entry_master
)
2076 gfc_entry_list
*entry
;
2077 for (entry
= gsym
->ns
->entries
; entry
; entry
= entry
->next
)
2078 if (strcmp (entry
->sym
->name
, sym
->name
) == 0)
2080 def_sym
= entry
->sym
;
2085 /* Differences in constant character lengths. */
2086 if (sym
->attr
.function
&& sym
->ts
.type
== BT_CHARACTER
)
2088 long int l1
= 0, l2
= 0;
2089 gfc_charlen
*cl1
= sym
->ts
.u
.cl
;
2090 gfc_charlen
*cl2
= def_sym
->ts
.u
.cl
;
2093 && cl1
->length
!= NULL
2094 && cl1
->length
->expr_type
== EXPR_CONSTANT
)
2095 l1
= mpz_get_si (cl1
->length
->value
.integer
);
2098 && cl2
->length
!= NULL
2099 && cl2
->length
->expr_type
== EXPR_CONSTANT
)
2100 l2
= mpz_get_si (cl2
->length
->value
.integer
);
2102 if (l1
&& l2
&& l1
!= l2
)
2103 gfc_error ("Character length mismatch in return type of "
2104 "function '%s' at %L (%ld/%ld)", sym
->name
,
2105 &sym
->declared_at
, l1
, l2
);
2108 /* Type mismatch of function return type and expected type. */
2109 if (sym
->attr
.function
2110 && !gfc_compare_types (&sym
->ts
, &def_sym
->ts
))
2111 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2112 sym
->name
, &sym
->declared_at
, gfc_typename (&sym
->ts
),
2113 gfc_typename (&def_sym
->ts
));
2115 if (def_sym
->formal
&& sym
->attr
.if_source
!= IFSRC_IFBODY
)
2117 gfc_formal_arglist
*arg
= def_sym
->formal
;
2118 for ( ; arg
; arg
= arg
->next
)
2121 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2122 else if (arg
->sym
->attr
.allocatable
2123 || arg
->sym
->attr
.asynchronous
2124 || arg
->sym
->attr
.optional
2125 || arg
->sym
->attr
.pointer
2126 || arg
->sym
->attr
.target
2127 || arg
->sym
->attr
.value
2128 || arg
->sym
->attr
.volatile_
)
2130 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2131 "has an attribute that requires an explicit "
2132 "interface for this procedure", arg
->sym
->name
,
2133 sym
->name
, &sym
->declared_at
);
2136 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2137 else if (arg
->sym
&& arg
->sym
->as
2138 && arg
->sym
->as
->type
== AS_ASSUMED_SHAPE
)
2140 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2141 "argument '%s' must have an explicit interface",
2142 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2145 /* F2008, 12.4.2.2 (2c) */
2146 else if (arg
->sym
->attr
.codimension
)
2148 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2149 "'%s' must have an explicit interface",
2150 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2153 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2154 else if (false) /* TODO: is a parametrized derived type */
2156 gfc_error ("Procedure '%s' at %L with parametrized derived "
2157 "type argument '%s' must have an explicit "
2158 "interface", sym
->name
, &sym
->declared_at
,
2162 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2163 else if (arg
->sym
->ts
.type
== BT_CLASS
)
2165 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2166 "argument '%s' must have an explicit interface",
2167 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2172 if (def_sym
->attr
.function
)
2174 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2175 if (def_sym
->as
&& def_sym
->as
->rank
2176 && (!sym
->as
|| sym
->as
->rank
!= def_sym
->as
->rank
))
2177 gfc_error ("The reference to function '%s' at %L either needs an "
2178 "explicit INTERFACE or the rank is incorrect", sym
->name
,
2181 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2182 if ((def_sym
->result
->attr
.pointer
2183 || def_sym
->result
->attr
.allocatable
)
2184 && (sym
->attr
.if_source
!= IFSRC_IFBODY
2185 || def_sym
->result
->attr
.pointer
2186 != sym
->result
->attr
.pointer
2187 || def_sym
->result
->attr
.allocatable
2188 != sym
->result
->attr
.allocatable
))
2189 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2190 "result must have an explicit interface", sym
->name
,
2193 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2194 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->attr
.if_source
!= IFSRC_IFBODY
2195 && def_sym
->ts
.type
== BT_CHARACTER
&& def_sym
->ts
.u
.cl
->length
!= NULL
)
2197 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
2199 if (!sym
->attr
.entry_master
&& sym
->attr
.if_source
== IFSRC_UNKNOWN
2200 && cl
&& cl
->length
&& cl
->length
->expr_type
!= EXPR_CONSTANT
)
2202 gfc_error ("Nonconstant character-length function '%s' at %L "
2203 "must have an explicit interface", sym
->name
,
2209 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2210 if (def_sym
->attr
.elemental
&& !sym
->attr
.elemental
)
2212 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2213 "interface", sym
->name
, &sym
->declared_at
);
2216 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2217 if (def_sym
->attr
.is_bind_c
&& !sym
->attr
.is_bind_c
)
2219 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2220 "an explicit interface", sym
->name
, &sym
->declared_at
);
2223 if (gfc_option
.flag_whole_file
== 1
2224 || ((gfc_option
.warn_std
& GFC_STD_LEGACY
)
2225 && !(gfc_option
.warn_std
& GFC_STD_GNU
)))
2226 gfc_errors_to_warnings (1);
2228 if (sym
->attr
.if_source
!= IFSRC_IFBODY
)
2229 gfc_procedure_use (def_sym
, actual
, where
);
2231 gfc_errors_to_warnings (0);
2234 if (gsym
->type
== GSYM_UNKNOWN
)
2237 gsym
->where
= *where
;
2244 /************* Function resolution *************/
2246 /* Resolve a function call known to be generic.
2247 Section 14.1.2.4.1. */
2250 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
2254 if (sym
->attr
.generic
)
2256 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
2259 expr
->value
.function
.name
= s
->name
;
2260 expr
->value
.function
.esym
= s
;
2262 if (s
->ts
.type
!= BT_UNKNOWN
)
2264 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
2265 expr
->ts
= s
->result
->ts
;
2268 expr
->rank
= s
->as
->rank
;
2269 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
2270 expr
->rank
= s
->result
->as
->rank
;
2272 gfc_set_sym_referenced (expr
->value
.function
.esym
);
2277 /* TODO: Need to search for elemental references in generic
2281 if (sym
->attr
.intrinsic
)
2282 return gfc_intrinsic_func_interface (expr
, 0);
2289 resolve_generic_f (gfc_expr
*expr
)
2294 sym
= expr
->symtree
->n
.sym
;
2298 m
= resolve_generic_f0 (expr
, sym
);
2301 else if (m
== MATCH_ERROR
)
2305 if (sym
->ns
->parent
== NULL
)
2307 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2311 if (!generic_sym (sym
))
2315 /* Last ditch attempt. See if the reference is to an intrinsic
2316 that possesses a matching interface. 14.1.2.4 */
2317 if (sym
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
2319 gfc_error ("There is no specific function for the generic '%s' at %L",
2320 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2324 m
= gfc_intrinsic_func_interface (expr
, 0);
2328 gfc_error ("Generic function '%s' at %L is not consistent with a "
2329 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
2336 /* Resolve a function call known to be specific. */
2339 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
2343 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
2345 if (sym
->attr
.dummy
)
2347 sym
->attr
.proc
= PROC_DUMMY
;
2351 sym
->attr
.proc
= PROC_EXTERNAL
;
2355 if (sym
->attr
.proc
== PROC_MODULE
2356 || sym
->attr
.proc
== PROC_ST_FUNCTION
2357 || sym
->attr
.proc
== PROC_INTERNAL
)
2360 if (sym
->attr
.intrinsic
)
2362 m
= gfc_intrinsic_func_interface (expr
, 1);
2366 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2367 "with an intrinsic", sym
->name
, &expr
->where
);
2375 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2378 expr
->ts
= sym
->result
->ts
;
2381 expr
->value
.function
.name
= sym
->name
;
2382 expr
->value
.function
.esym
= sym
;
2383 if (sym
->as
!= NULL
)
2384 expr
->rank
= sym
->as
->rank
;
2391 resolve_specific_f (gfc_expr
*expr
)
2396 sym
= expr
->symtree
->n
.sym
;
2400 m
= resolve_specific_f0 (sym
, expr
);
2403 if (m
== MATCH_ERROR
)
2406 if (sym
->ns
->parent
== NULL
)
2409 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2415 gfc_error ("Unable to resolve the specific function '%s' at %L",
2416 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2422 /* Resolve a procedure call not known to be generic nor specific. */
2425 resolve_unknown_f (gfc_expr
*expr
)
2430 sym
= expr
->symtree
->n
.sym
;
2432 if (sym
->attr
.dummy
)
2434 sym
->attr
.proc
= PROC_DUMMY
;
2435 expr
->value
.function
.name
= sym
->name
;
2439 /* See if we have an intrinsic function reference. */
2441 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2443 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2448 /* The reference is to an external name. */
2450 sym
->attr
.proc
= PROC_EXTERNAL
;
2451 expr
->value
.function
.name
= sym
->name
;
2452 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2454 if (sym
->as
!= NULL
)
2455 expr
->rank
= sym
->as
->rank
;
2457 /* Type of the expression is either the type of the symbol or the
2458 default type of the symbol. */
2461 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2463 if (sym
->ts
.type
!= BT_UNKNOWN
)
2467 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2469 if (ts
->type
== BT_UNKNOWN
)
2471 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2472 sym
->name
, &expr
->where
);
2483 /* Return true, if the symbol is an external procedure. */
2485 is_external_proc (gfc_symbol
*sym
)
2487 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2488 && !(sym
->attr
.intrinsic
2489 || gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
))
2490 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2491 && !sym
->attr
.proc_pointer
2492 && !sym
->attr
.use_assoc
2500 /* Figure out if a function reference is pure or not. Also set the name
2501 of the function for a potential error message. Return nonzero if the
2502 function is PURE, zero if not. */
2504 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2507 pure_function (gfc_expr
*e
, const char **name
)
2513 if (e
->symtree
!= NULL
2514 && e
->symtree
->n
.sym
!= NULL
2515 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2516 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
2518 if (e
->value
.function
.esym
)
2520 pure
= gfc_pure (e
->value
.function
.esym
);
2521 *name
= e
->value
.function
.esym
->name
;
2523 else if (e
->value
.function
.isym
)
2525 pure
= e
->value
.function
.isym
->pure
2526 || e
->value
.function
.isym
->elemental
;
2527 *name
= e
->value
.function
.isym
->name
;
2531 /* Implicit functions are not pure. */
2533 *name
= e
->value
.function
.name
;
2541 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2542 int *f ATTRIBUTE_UNUSED
)
2546 /* Don't bother recursing into other statement functions
2547 since they will be checked individually for purity. */
2548 if (e
->expr_type
!= EXPR_FUNCTION
2550 || e
->symtree
->n
.sym
== sym
2551 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2554 return pure_function (e
, &name
) ? false : true;
2559 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
2561 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
2566 is_scalar_expr_ptr (gfc_expr
*expr
)
2568 gfc_try retval
= SUCCESS
;
2573 /* See if we have a gfc_ref, which means we have a substring, array
2574 reference, or a component. */
2575 if (expr
->ref
!= NULL
)
2578 while (ref
->next
!= NULL
)
2584 if (ref
->u
.ss
.start
== NULL
|| ref
->u
.ss
.end
== NULL
2585 || gfc_dep_compare_expr (ref
->u
.ss
.start
, ref
->u
.ss
.end
) != 0)
2590 if (ref
->u
.ar
.type
== AR_ELEMENT
)
2592 else if (ref
->u
.ar
.type
== AR_FULL
)
2594 /* The user can give a full array if the array is of size 1. */
2595 if (ref
->u
.ar
.as
!= NULL
2596 && ref
->u
.ar
.as
->rank
== 1
2597 && ref
->u
.ar
.as
->type
== AS_EXPLICIT
2598 && ref
->u
.ar
.as
->lower
[0] != NULL
2599 && ref
->u
.ar
.as
->lower
[0]->expr_type
== EXPR_CONSTANT
2600 && ref
->u
.ar
.as
->upper
[0] != NULL
2601 && ref
->u
.ar
.as
->upper
[0]->expr_type
== EXPR_CONSTANT
)
2603 /* If we have a character string, we need to check if
2604 its length is one. */
2605 if (expr
->ts
.type
== BT_CHARACTER
)
2607 if (expr
->ts
.u
.cl
== NULL
2608 || expr
->ts
.u
.cl
->length
== NULL
2609 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1)
2615 /* We have constant lower and upper bounds. If the
2616 difference between is 1, it can be considered a
2618 FIXME: Use gfc_dep_compare_expr instead. */
2619 start
= (int) mpz_get_si
2620 (ref
->u
.ar
.as
->lower
[0]->value
.integer
);
2621 end
= (int) mpz_get_si
2622 (ref
->u
.ar
.as
->upper
[0]->value
.integer
);
2623 if (end
- start
+ 1 != 1)
2638 else if (expr
->ts
.type
== BT_CHARACTER
&& expr
->rank
== 0)
2640 /* Character string. Make sure it's of length 1. */
2641 if (expr
->ts
.u
.cl
== NULL
2642 || expr
->ts
.u
.cl
->length
== NULL
2643 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
2646 else if (expr
->rank
!= 0)
2653 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2654 and, in the case of c_associated, set the binding label based on
2658 gfc_iso_c_func_interface (gfc_symbol
*sym
, gfc_actual_arglist
*args
,
2659 gfc_symbol
**new_sym
)
2661 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2662 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2663 int optional_arg
= 0;
2664 gfc_try retval
= SUCCESS
;
2665 gfc_symbol
*args_sym
;
2666 gfc_typespec
*arg_ts
;
2667 symbol_attribute arg_attr
;
2669 if (args
->expr
->expr_type
== EXPR_CONSTANT
2670 || args
->expr
->expr_type
== EXPR_OP
2671 || args
->expr
->expr_type
== EXPR_NULL
)
2673 gfc_error ("Argument to '%s' at %L is not a variable",
2674 sym
->name
, &(args
->expr
->where
));
2678 args_sym
= args
->expr
->symtree
->n
.sym
;
2680 /* The typespec for the actual arg should be that stored in the expr
2681 and not necessarily that of the expr symbol (args_sym), because
2682 the actual expression could be a part-ref of the expr symbol. */
2683 arg_ts
= &(args
->expr
->ts
);
2684 arg_attr
= gfc_expr_attr (args
->expr
);
2686 if (sym
->intmod_sym_id
== ISOCBINDING_ASSOCIATED
)
2688 /* If the user gave two args then they are providing something for
2689 the optional arg (the second cptr). Therefore, set the name and
2690 binding label to the c_associated for two cptrs. Otherwise,
2691 set c_associated to expect one cptr. */
2695 sprintf (name
, "%s_2", sym
->name
);
2696 sprintf (binding_label
, "%s_2", sym
->binding_label
);
2702 sprintf (name
, "%s_1", sym
->name
);
2703 sprintf (binding_label
, "%s_1", sym
->binding_label
);
2707 /* Get a new symbol for the version of c_associated that
2709 *new_sym
= get_iso_c_sym (sym
, name
, binding_label
, optional_arg
);
2711 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
2712 || sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2714 sprintf (name
, "%s", sym
->name
);
2715 sprintf (binding_label
, "%s", sym
->binding_label
);
2717 /* Error check the call. */
2718 if (args
->next
!= NULL
)
2720 gfc_error_now ("More actual than formal arguments in '%s' "
2721 "call at %L", name
, &(args
->expr
->where
));
2724 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
)
2729 /* Make sure we have either the target or pointer attribute. */
2730 if (!arg_attr
.target
&& !arg_attr
.pointer
)
2732 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2733 "a TARGET or an associated pointer",
2735 sym
->name
, &(args
->expr
->where
));
2739 if (gfc_is_coindexed (args
->expr
))
2741 gfc_error_now ("Coindexed argument not permitted"
2742 " in '%s' call at %L", name
,
2743 &(args
->expr
->where
));
2747 /* Follow references to make sure there are no array
2749 seen_section
= false;
2751 for (ref
=args
->expr
->ref
; ref
; ref
= ref
->next
)
2753 if (ref
->type
== REF_ARRAY
)
2755 if (ref
->u
.ar
.type
== AR_SECTION
)
2756 seen_section
= true;
2758 if (ref
->u
.ar
.type
!= AR_ELEMENT
)
2761 for (r
= ref
->next
; r
; r
=r
->next
)
2762 if (r
->type
== REF_COMPONENT
)
2764 gfc_error_now ("Array section not permitted"
2765 " in '%s' call at %L", name
,
2766 &(args
->expr
->where
));
2774 if (seen_section
&& retval
== SUCCESS
)
2775 gfc_warning ("Array section in '%s' call at %L", name
,
2776 &(args
->expr
->where
));
2778 /* See if we have interoperable type and type param. */
2779 if (verify_c_interop (arg_ts
) == SUCCESS
2780 || gfc_check_any_c_kind (arg_ts
) == SUCCESS
)
2782 if (args_sym
->attr
.target
== 1)
2784 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2785 has the target attribute and is interoperable. */
2786 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2787 allocatable variable that has the TARGET attribute and
2788 is not an array of zero size. */
2789 if (args_sym
->attr
.allocatable
== 1)
2791 if (args_sym
->attr
.dimension
!= 0
2792 && (args_sym
->as
&& args_sym
->as
->rank
== 0))
2794 gfc_error_now ("Allocatable variable '%s' used as a "
2795 "parameter to '%s' at %L must not be "
2796 "an array of zero size",
2797 args_sym
->name
, sym
->name
,
2798 &(args
->expr
->where
));
2804 /* A non-allocatable target variable with C
2805 interoperable type and type parameters must be
2807 if (args_sym
&& args_sym
->attr
.dimension
)
2809 if (args_sym
->as
->type
== AS_ASSUMED_SHAPE
)
2811 gfc_error ("Assumed-shape array '%s' at %L "
2812 "cannot be an argument to the "
2813 "procedure '%s' because "
2814 "it is not C interoperable",
2816 &(args
->expr
->where
), sym
->name
);
2819 else if (args_sym
->as
->type
== AS_DEFERRED
)
2821 gfc_error ("Deferred-shape array '%s' at %L "
2822 "cannot be an argument to the "
2823 "procedure '%s' because "
2824 "it is not C interoperable",
2826 &(args
->expr
->where
), sym
->name
);
2831 /* Make sure it's not a character string. Arrays of
2832 any type should be ok if the variable is of a C
2833 interoperable type. */
2834 if (arg_ts
->type
== BT_CHARACTER
)
2835 if (arg_ts
->u
.cl
!= NULL
2836 && (arg_ts
->u
.cl
->length
== NULL
2837 || arg_ts
->u
.cl
->length
->expr_type
2840 (arg_ts
->u
.cl
->length
->value
.integer
, 1)
2842 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2844 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2845 "at %L must have a length of 1",
2846 args_sym
->name
, sym
->name
,
2847 &(args
->expr
->where
));
2852 else if (arg_attr
.pointer
2853 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2855 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2857 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2858 "associated scalar POINTER", args_sym
->name
,
2859 sym
->name
, &(args
->expr
->where
));
2865 /* The parameter is not required to be C interoperable. If it
2866 is not C interoperable, it must be a nonpolymorphic scalar
2867 with no length type parameters. It still must have either
2868 the pointer or target attribute, and it can be
2869 allocatable (but must be allocated when c_loc is called). */
2870 if (args
->expr
->rank
!= 0
2871 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2873 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2874 "scalar", args_sym
->name
, sym
->name
,
2875 &(args
->expr
->where
));
2878 else if (arg_ts
->type
== BT_CHARACTER
2879 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2881 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2882 "%L must have a length of 1",
2883 args_sym
->name
, sym
->name
,
2884 &(args
->expr
->where
));
2887 else if (arg_ts
->type
== BT_CLASS
)
2889 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2890 "polymorphic", args_sym
->name
, sym
->name
,
2891 &(args
->expr
->where
));
2896 else if (sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2898 if (args_sym
->attr
.flavor
!= FL_PROCEDURE
)
2900 /* TODO: Update this error message to allow for procedure
2901 pointers once they are implemented. */
2902 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2904 args_sym
->name
, sym
->name
,
2905 &(args
->expr
->where
));
2908 else if (args_sym
->attr
.is_bind_c
!= 1)
2910 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2912 args_sym
->name
, sym
->name
,
2913 &(args
->expr
->where
));
2918 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2923 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2924 "iso_c_binding function: '%s'!\n", sym
->name
);
2931 /* Resolve a function call, which means resolving the arguments, then figuring
2932 out which entity the name refers to. */
2935 resolve_function (gfc_expr
*expr
)
2937 gfc_actual_arglist
*arg
;
2942 procedure_type p
= PROC_INTRINSIC
;
2943 bool no_formal_args
;
2947 sym
= expr
->symtree
->n
.sym
;
2949 /* If this is a procedure pointer component, it has already been resolved. */
2950 if (gfc_is_proc_ptr_comp (expr
, NULL
))
2953 if (sym
&& sym
->attr
.intrinsic
2954 && resolve_intrinsic (sym
, &expr
->where
) == FAILURE
)
2957 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
2959 gfc_error ("'%s' at %L is not a function", sym
->name
, &expr
->where
);
2963 /* If this ia a deferred TBP with an abstract interface (which may
2964 of course be referenced), expr->value.function.esym will be set. */
2965 if (sym
&& sym
->attr
.abstract
&& !expr
->value
.function
.esym
)
2967 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2968 sym
->name
, &expr
->where
);
2972 /* Switch off assumed size checking and do this again for certain kinds
2973 of procedure, once the procedure itself is resolved. */
2974 need_full_assumed_size
++;
2976 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
2977 p
= expr
->symtree
->n
.sym
->attr
.proc
;
2979 if (expr
->value
.function
.isym
&& expr
->value
.function
.isym
->inquiry
)
2980 inquiry_argument
= true;
2981 no_formal_args
= sym
&& is_external_proc (sym
) && sym
->formal
== NULL
;
2983 if (resolve_actual_arglist (expr
->value
.function
.actual
,
2984 p
, no_formal_args
) == FAILURE
)
2986 inquiry_argument
= false;
2990 inquiry_argument
= false;
2992 /* Need to setup the call to the correct c_associated, depending on
2993 the number of cptrs to user gives to compare. */
2994 if (sym
&& sym
->attr
.is_iso_c
== 1)
2996 if (gfc_iso_c_func_interface (sym
, expr
->value
.function
.actual
, &sym
)
3000 /* Get the symtree for the new symbol (resolved func).
3001 the old one will be freed later, when it's no longer used. */
3002 gfc_find_sym_tree (sym
->name
, sym
->ns
, 1, &(expr
->symtree
));
3005 /* Resume assumed_size checking. */
3006 need_full_assumed_size
--;
3008 /* If the procedure is external, check for usage. */
3009 if (sym
&& is_external_proc (sym
))
3010 resolve_global_procedure (sym
, &expr
->where
,
3011 &expr
->value
.function
.actual
, 0);
3013 if (sym
&& sym
->ts
.type
== BT_CHARACTER
3015 && sym
->ts
.u
.cl
->length
== NULL
3017 && !sym
->ts
.deferred
3018 && expr
->value
.function
.esym
== NULL
3019 && !sym
->attr
.contained
)
3021 /* Internal procedures are taken care of in resolve_contained_fntype. */
3022 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3023 "be used at %L since it is not a dummy argument",
3024 sym
->name
, &expr
->where
);
3028 /* See if function is already resolved. */
3030 if (expr
->value
.function
.name
!= NULL
)
3032 if (expr
->ts
.type
== BT_UNKNOWN
)
3038 /* Apply the rules of section 14.1.2. */
3040 switch (procedure_kind (sym
))
3043 t
= resolve_generic_f (expr
);
3046 case PTYPE_SPECIFIC
:
3047 t
= resolve_specific_f (expr
);
3051 t
= resolve_unknown_f (expr
);
3055 gfc_internal_error ("resolve_function(): bad function type");
3059 /* If the expression is still a function (it might have simplified),
3060 then we check to see if we are calling an elemental function. */
3062 if (expr
->expr_type
!= EXPR_FUNCTION
)
3065 temp
= need_full_assumed_size
;
3066 need_full_assumed_size
= 0;
3068 if (resolve_elemental_actual (expr
, NULL
) == FAILURE
)
3071 if (omp_workshare_flag
3072 && expr
->value
.function
.esym
3073 && ! gfc_elemental (expr
->value
.function
.esym
))
3075 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3076 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
3081 #define GENERIC_ID expr->value.function.isym->id
3082 else if (expr
->value
.function
.actual
!= NULL
3083 && expr
->value
.function
.isym
!= NULL
3084 && GENERIC_ID
!= GFC_ISYM_LBOUND
3085 && GENERIC_ID
!= GFC_ISYM_LEN
3086 && GENERIC_ID
!= GFC_ISYM_LOC
3087 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
3089 /* Array intrinsics must also have the last upper bound of an
3090 assumed size array argument. UBOUND and SIZE have to be
3091 excluded from the check if the second argument is anything
3094 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
3096 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
3097 && arg
->next
!= NULL
&& arg
->next
->expr
)
3099 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
3102 if (arg
->next
->name
&& strncmp(arg
->next
->name
, "kind", 4) == 0)
3105 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
3110 if (arg
->expr
!= NULL
3111 && arg
->expr
->rank
> 0
3112 && resolve_assumed_size_actual (arg
->expr
))
3118 need_full_assumed_size
= temp
;
3121 if (!pure_function (expr
, &name
) && name
)
3125 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3126 "FORALL %s", name
, &expr
->where
,
3127 forall_flag
== 2 ? "mask" : "block");
3130 else if (gfc_pure (NULL
))
3132 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3133 "procedure within a PURE procedure", name
, &expr
->where
);
3138 if (!pure_function (expr
, &name
) && name
&& gfc_implicit_pure (NULL
))
3139 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3141 /* Functions without the RECURSIVE attribution are not allowed to
3142 * call themselves. */
3143 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
3146 esym
= expr
->value
.function
.esym
;
3148 if (is_illegal_recursion (esym
, gfc_current_ns
))
3150 if (esym
->attr
.entry
&& esym
->ns
->entries
)
3151 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3152 " function '%s' is not RECURSIVE",
3153 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
3155 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3156 " is not RECURSIVE", esym
->name
, &expr
->where
);
3162 /* Character lengths of use associated functions may contains references to
3163 symbols not referenced from the current program unit otherwise. Make sure
3164 those symbols are marked as referenced. */
3166 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
3167 && expr
->value
.function
.esym
->attr
.use_assoc
)
3169 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
3172 /* Make sure that the expression has a typespec that works. */
3173 if (expr
->ts
.type
== BT_UNKNOWN
)
3175 if (expr
->symtree
->n
.sym
->result
3176 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
3177 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
3178 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
3185 /************* Subroutine resolution *************/
3188 pure_subroutine (gfc_code
*c
, gfc_symbol
*sym
)
3194 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3195 sym
->name
, &c
->loc
);
3196 else if (gfc_pure (NULL
))
3197 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym
->name
,
3203 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3207 if (sym
->attr
.generic
)
3209 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
3212 c
->resolved_sym
= s
;
3213 pure_subroutine (c
, s
);
3217 /* TODO: Need to search for elemental references in generic interface. */
3220 if (sym
->attr
.intrinsic
)
3221 return gfc_intrinsic_sub_interface (c
, 0);
3228 resolve_generic_s (gfc_code
*c
)
3233 sym
= c
->symtree
->n
.sym
;
3237 m
= resolve_generic_s0 (c
, sym
);
3240 else if (m
== MATCH_ERROR
)
3244 if (sym
->ns
->parent
== NULL
)
3246 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3250 if (!generic_sym (sym
))
3254 /* Last ditch attempt. See if the reference is to an intrinsic
3255 that possesses a matching interface. 14.1.2.4 */
3256 sym
= c
->symtree
->n
.sym
;
3258 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
3260 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3261 sym
->name
, &c
->loc
);
3265 m
= gfc_intrinsic_sub_interface (c
, 0);
3269 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3270 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
3276 /* Set the name and binding label of the subroutine symbol in the call
3277 expression represented by 'c' to include the type and kind of the
3278 second parameter. This function is for resolving the appropriate
3279 version of c_f_pointer() and c_f_procpointer(). For example, a
3280 call to c_f_pointer() for a default integer pointer could have a
3281 name of c_f_pointer_i4. If no second arg exists, which is an error
3282 for these two functions, it defaults to the generic symbol's name
3283 and binding label. */
3286 set_name_and_label (gfc_code
*c
, gfc_symbol
*sym
,
3287 char *name
, char *binding_label
)
3289 gfc_expr
*arg
= NULL
;
3293 /* The second arg of c_f_pointer and c_f_procpointer determines
3294 the type and kind for the procedure name. */
3295 arg
= c
->ext
.actual
->next
->expr
;
3299 /* Set up the name to have the given symbol's name,
3300 plus the type and kind. */
3301 /* a derived type is marked with the type letter 'u' */
3302 if (arg
->ts
.type
== BT_DERIVED
)
3305 kind
= 0; /* set the kind as 0 for now */
3309 type
= gfc_type_letter (arg
->ts
.type
);
3310 kind
= arg
->ts
.kind
;
3313 if (arg
->ts
.type
== BT_CHARACTER
)
3314 /* Kind info for character strings not needed. */
3317 sprintf (name
, "%s_%c%d", sym
->name
, type
, kind
);
3318 /* Set up the binding label as the given symbol's label plus
3319 the type and kind. */
3320 sprintf (binding_label
, "%s_%c%d", sym
->binding_label
, type
, kind
);
3324 /* If the second arg is missing, set the name and label as
3325 was, cause it should at least be found, and the missing
3326 arg error will be caught by compare_parameters(). */
3327 sprintf (name
, "%s", sym
->name
);
3328 sprintf (binding_label
, "%s", sym
->binding_label
);
3335 /* Resolve a generic version of the iso_c_binding procedure given
3336 (sym) to the specific one based on the type and kind of the
3337 argument(s). Currently, this function resolves c_f_pointer() and
3338 c_f_procpointer based on the type and kind of the second argument
3339 (FPTR). Other iso_c_binding procedures aren't specially handled.
3340 Upon successfully exiting, c->resolved_sym will hold the resolved
3341 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3345 gfc_iso_c_sub_interface (gfc_code
*c
, gfc_symbol
*sym
)
3347 gfc_symbol
*new_sym
;
3348 /* this is fine, since we know the names won't use the max */
3349 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3350 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
3351 /* default to success; will override if find error */
3352 match m
= MATCH_YES
;
3354 /* Make sure the actual arguments are in the necessary order (based on the
3355 formal args) before resolving. */
3356 gfc_procedure_use (sym
, &c
->ext
.actual
, &(c
->loc
));
3358 if ((sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
) ||
3359 (sym
->intmod_sym_id
== ISOCBINDING_F_PROCPOINTER
))
3361 set_name_and_label (c
, sym
, name
, binding_label
);
3363 if (sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
)
3365 if (c
->ext
.actual
!= NULL
&& c
->ext
.actual
->next
!= NULL
)
3367 /* Make sure we got a third arg if the second arg has non-zero
3368 rank. We must also check that the type and rank are
3369 correct since we short-circuit this check in
3370 gfc_procedure_use() (called above to sort actual args). */
3371 if (c
->ext
.actual
->next
->expr
->rank
!= 0)
3373 if(c
->ext
.actual
->next
->next
== NULL
3374 || c
->ext
.actual
->next
->next
->expr
== NULL
)
3377 gfc_error ("Missing SHAPE parameter for call to %s "
3378 "at %L", sym
->name
, &(c
->loc
));
3380 else if (c
->ext
.actual
->next
->next
->expr
->ts
.type
3382 || c
->ext
.actual
->next
->next
->expr
->rank
!= 1)
3385 gfc_error ("SHAPE parameter for call to %s at %L must "
3386 "be a rank 1 INTEGER array", sym
->name
,
3393 if (m
!= MATCH_ERROR
)
3395 /* the 1 means to add the optional arg to formal list */
3396 new_sym
= get_iso_c_sym (sym
, name
, binding_label
, 1);
3398 /* for error reporting, say it's declared where the original was */
3399 new_sym
->declared_at
= sym
->declared_at
;
3404 /* no differences for c_loc or c_funloc */
3408 /* set the resolved symbol */
3409 if (m
!= MATCH_ERROR
)
3410 c
->resolved_sym
= new_sym
;
3412 c
->resolved_sym
= sym
;
3418 /* Resolve a subroutine call known to be specific. */
3421 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3425 if(sym
->attr
.is_iso_c
)
3427 m
= gfc_iso_c_sub_interface (c
,sym
);
3431 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
3433 if (sym
->attr
.dummy
)
3435 sym
->attr
.proc
= PROC_DUMMY
;
3439 sym
->attr
.proc
= PROC_EXTERNAL
;
3443 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
3446 if (sym
->attr
.intrinsic
)
3448 m
= gfc_intrinsic_sub_interface (c
, 1);
3452 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3453 "with an intrinsic", sym
->name
, &c
->loc
);
3461 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3463 c
->resolved_sym
= sym
;
3464 pure_subroutine (c
, sym
);
3471 resolve_specific_s (gfc_code
*c
)
3476 sym
= c
->symtree
->n
.sym
;
3480 m
= resolve_specific_s0 (c
, sym
);
3483 if (m
== MATCH_ERROR
)
3486 if (sym
->ns
->parent
== NULL
)
3489 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3495 sym
= c
->symtree
->n
.sym
;
3496 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3497 sym
->name
, &c
->loc
);
3503 /* Resolve a subroutine call not known to be generic nor specific. */
3506 resolve_unknown_s (gfc_code
*c
)
3510 sym
= c
->symtree
->n
.sym
;
3512 if (sym
->attr
.dummy
)
3514 sym
->attr
.proc
= PROC_DUMMY
;
3518 /* See if we have an intrinsic function reference. */
3520 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3522 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3527 /* The reference is to an external name. */
3530 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3532 c
->resolved_sym
= sym
;
3534 pure_subroutine (c
, sym
);
3540 /* Resolve a subroutine call. Although it was tempting to use the same code
3541 for functions, subroutines and functions are stored differently and this
3542 makes things awkward. */
3545 resolve_call (gfc_code
*c
)
3548 procedure_type ptype
= PROC_INTRINSIC
;
3549 gfc_symbol
*csym
, *sym
;
3550 bool no_formal_args
;
3552 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3554 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3556 gfc_error ("'%s' at %L has a type, which is not consistent with "
3557 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3561 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3564 gfc_find_sym_tree (csym
->name
, gfc_current_ns
, 1, &st
);
3565 sym
= st
? st
->n
.sym
: NULL
;
3566 if (sym
&& csym
!= sym
3567 && sym
->ns
== gfc_current_ns
3568 && sym
->attr
.flavor
== FL_PROCEDURE
3569 && sym
->attr
.contained
)
3572 if (csym
->attr
.generic
)
3573 c
->symtree
->n
.sym
= sym
;
3576 csym
= c
->symtree
->n
.sym
;
3580 /* If this ia a deferred TBP with an abstract interface
3581 (which may of course be referenced), c->expr1 will be set. */
3582 if (csym
&& csym
->attr
.abstract
&& !c
->expr1
)
3584 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3585 csym
->name
, &c
->loc
);
3589 /* Subroutines without the RECURSIVE attribution are not allowed to
3590 * call themselves. */
3591 if (csym
&& is_illegal_recursion (csym
, gfc_current_ns
))
3593 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3594 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3595 " subroutine '%s' is not RECURSIVE",
3596 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3598 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3599 " is not RECURSIVE", csym
->name
, &c
->loc
);
3604 /* Switch off assumed size checking and do this again for certain kinds
3605 of procedure, once the procedure itself is resolved. */
3606 need_full_assumed_size
++;
3609 ptype
= csym
->attr
.proc
;
3611 no_formal_args
= csym
&& is_external_proc (csym
) && csym
->formal
== NULL
;
3612 if (resolve_actual_arglist (c
->ext
.actual
, ptype
,
3613 no_formal_args
) == FAILURE
)
3616 /* Resume assumed_size checking. */
3617 need_full_assumed_size
--;
3619 /* If external, check for usage. */
3620 if (csym
&& is_external_proc (csym
))
3621 resolve_global_procedure (csym
, &c
->loc
, &c
->ext
.actual
, 1);
3624 if (c
->resolved_sym
== NULL
)
3626 c
->resolved_isym
= NULL
;
3627 switch (procedure_kind (csym
))
3630 t
= resolve_generic_s (c
);
3633 case PTYPE_SPECIFIC
:
3634 t
= resolve_specific_s (c
);
3638 t
= resolve_unknown_s (c
);
3642 gfc_internal_error ("resolve_subroutine(): bad function type");
3646 /* Some checks of elemental subroutine actual arguments. */
3647 if (resolve_elemental_actual (NULL
, c
) == FAILURE
)
3654 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3655 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3656 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3657 if their shapes do not match. If either op1->shape or op2->shape is
3658 NULL, return SUCCESS. */
3661 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3668 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3670 for (i
= 0; i
< op1
->rank
; i
++)
3672 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3674 gfc_error ("Shapes for operands at %L and %L are not conformable",
3675 &op1
->where
, &op2
->where
);
3686 /* Resolve an operator expression node. This can involve replacing the
3687 operation with a user defined function call. */
3690 resolve_operator (gfc_expr
*e
)
3692 gfc_expr
*op1
, *op2
;
3694 bool dual_locus_error
;
3697 /* Resolve all subnodes-- give them types. */
3699 switch (e
->value
.op
.op
)
3702 if (gfc_resolve_expr (e
->value
.op
.op2
) == FAILURE
)
3705 /* Fall through... */
3708 case INTRINSIC_UPLUS
:
3709 case INTRINSIC_UMINUS
:
3710 case INTRINSIC_PARENTHESES
:
3711 if (gfc_resolve_expr (e
->value
.op
.op1
) == FAILURE
)
3716 /* Typecheck the new node. */
3718 op1
= e
->value
.op
.op1
;
3719 op2
= e
->value
.op
.op2
;
3720 dual_locus_error
= false;
3722 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
3723 || (op2
&& op2
->expr_type
== EXPR_NULL
))
3725 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
3729 switch (e
->value
.op
.op
)
3731 case INTRINSIC_UPLUS
:
3732 case INTRINSIC_UMINUS
:
3733 if (op1
->ts
.type
== BT_INTEGER
3734 || op1
->ts
.type
== BT_REAL
3735 || op1
->ts
.type
== BT_COMPLEX
)
3741 sprintf (msg
, _("Operand of unary numeric operator '%s' at %%L is %s"),
3742 gfc_op2string (e
->value
.op
.op
), gfc_typename (&e
->ts
));
3745 case INTRINSIC_PLUS
:
3746 case INTRINSIC_MINUS
:
3747 case INTRINSIC_TIMES
:
3748 case INTRINSIC_DIVIDE
:
3749 case INTRINSIC_POWER
:
3750 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3752 gfc_type_convert_binary (e
, 1);
3757 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3758 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3759 gfc_typename (&op2
->ts
));
3762 case INTRINSIC_CONCAT
:
3763 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3764 && op1
->ts
.kind
== op2
->ts
.kind
)
3766 e
->ts
.type
= BT_CHARACTER
;
3767 e
->ts
.kind
= op1
->ts
.kind
;
3772 _("Operands of string concatenation operator at %%L are %s/%s"),
3773 gfc_typename (&op1
->ts
), gfc_typename (&op2
->ts
));
3779 case INTRINSIC_NEQV
:
3780 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3782 e
->ts
.type
= BT_LOGICAL
;
3783 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
3784 if (op1
->ts
.kind
< e
->ts
.kind
)
3785 gfc_convert_type (op1
, &e
->ts
, 2);
3786 else if (op2
->ts
.kind
< e
->ts
.kind
)
3787 gfc_convert_type (op2
, &e
->ts
, 2);
3791 sprintf (msg
, _("Operands of logical operator '%s' at %%L are %s/%s"),
3792 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3793 gfc_typename (&op2
->ts
));
3798 if (op1
->ts
.type
== BT_LOGICAL
)
3800 e
->ts
.type
= BT_LOGICAL
;
3801 e
->ts
.kind
= op1
->ts
.kind
;
3805 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
3806 gfc_typename (&op1
->ts
));
3810 case INTRINSIC_GT_OS
:
3812 case INTRINSIC_GE_OS
:
3814 case INTRINSIC_LT_OS
:
3816 case INTRINSIC_LE_OS
:
3817 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
3819 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
3823 /* Fall through... */
3826 case INTRINSIC_EQ_OS
:
3828 case INTRINSIC_NE_OS
:
3829 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3830 && op1
->ts
.kind
== op2
->ts
.kind
)
3832 e
->ts
.type
= BT_LOGICAL
;
3833 e
->ts
.kind
= gfc_default_logical_kind
;
3837 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3839 gfc_type_convert_binary (e
, 1);
3841 e
->ts
.type
= BT_LOGICAL
;
3842 e
->ts
.kind
= gfc_default_logical_kind
;
3846 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3848 _("Logicals at %%L must be compared with %s instead of %s"),
3849 (e
->value
.op
.op
== INTRINSIC_EQ
3850 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
3851 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
3854 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3855 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3856 gfc_typename (&op2
->ts
));
3860 case INTRINSIC_USER
:
3861 if (e
->value
.op
.uop
->op
== NULL
)
3862 sprintf (msg
, _("Unknown operator '%s' at %%L"), e
->value
.op
.uop
->name
);
3863 else if (op2
== NULL
)
3864 sprintf (msg
, _("Operand of user operator '%s' at %%L is %s"),
3865 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
));
3868 sprintf (msg
, _("Operands of user operator '%s' at %%L are %s/%s"),
3869 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
),
3870 gfc_typename (&op2
->ts
));
3871 e
->value
.op
.uop
->op
->sym
->attr
.referenced
= 1;
3876 case INTRINSIC_PARENTHESES
:
3878 if (e
->ts
.type
== BT_CHARACTER
)
3879 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
3883 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3886 /* Deal with arrayness of an operand through an operator. */
3890 switch (e
->value
.op
.op
)
3892 case INTRINSIC_PLUS
:
3893 case INTRINSIC_MINUS
:
3894 case INTRINSIC_TIMES
:
3895 case INTRINSIC_DIVIDE
:
3896 case INTRINSIC_POWER
:
3897 case INTRINSIC_CONCAT
:
3901 case INTRINSIC_NEQV
:
3903 case INTRINSIC_EQ_OS
:
3905 case INTRINSIC_NE_OS
:
3907 case INTRINSIC_GT_OS
:
3909 case INTRINSIC_GE_OS
:
3911 case INTRINSIC_LT_OS
:
3913 case INTRINSIC_LE_OS
:
3915 if (op1
->rank
== 0 && op2
->rank
== 0)
3918 if (op1
->rank
== 0 && op2
->rank
!= 0)
3920 e
->rank
= op2
->rank
;
3922 if (e
->shape
== NULL
)
3923 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
3926 if (op1
->rank
!= 0 && op2
->rank
== 0)
3928 e
->rank
= op1
->rank
;
3930 if (e
->shape
== NULL
)
3931 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3934 if (op1
->rank
!= 0 && op2
->rank
!= 0)
3936 if (op1
->rank
== op2
->rank
)
3938 e
->rank
= op1
->rank
;
3939 if (e
->shape
== NULL
)
3941 t
= compare_shapes (op1
, op2
);
3945 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3950 /* Allow higher level expressions to work. */
3953 /* Try user-defined operators, and otherwise throw an error. */
3954 dual_locus_error
= true;
3956 _("Inconsistent ranks for operator at %%L and %%L"));
3963 case INTRINSIC_PARENTHESES
:
3965 case INTRINSIC_UPLUS
:
3966 case INTRINSIC_UMINUS
:
3967 /* Simply copy arrayness attribute */
3968 e
->rank
= op1
->rank
;
3970 if (e
->shape
== NULL
)
3971 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3979 /* Attempt to simplify the expression. */
3982 t
= gfc_simplify_expr (e
, 0);
3983 /* Some calls do not succeed in simplification and return FAILURE
3984 even though there is no error; e.g. variable references to
3985 PARAMETER arrays. */
3986 if (!gfc_is_constant_expr (e
))
3995 if (gfc_extend_expr (e
, &real_error
) == SUCCESS
)
4002 if (dual_locus_error
)
4003 gfc_error (msg
, &op1
->where
, &op2
->where
);
4005 gfc_error (msg
, &e
->where
);
4011 /************** Array resolution subroutines **************/
4014 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
}
4017 /* Compare two integer expressions. */
4020 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
4024 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
4025 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
4028 /* If either of the types isn't INTEGER, we must have
4029 raised an error earlier. */
4031 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
4034 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
4044 /* Compare an integer expression with an integer. */
4047 compare_bound_int (gfc_expr
*a
, int b
)
4051 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4054 if (a
->ts
.type
!= BT_INTEGER
)
4055 gfc_internal_error ("compare_bound_int(): Bad expression");
4057 i
= mpz_cmp_si (a
->value
.integer
, b
);
4067 /* Compare an integer expression with a mpz_t. */
4070 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
4074 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4077 if (a
->ts
.type
!= BT_INTEGER
)
4078 gfc_internal_error ("compare_bound_int(): Bad expression");
4080 i
= mpz_cmp (a
->value
.integer
, b
);
4090 /* Compute the last value of a sequence given by a triplet.
4091 Return 0 if it wasn't able to compute the last value, or if the
4092 sequence if empty, and 1 otherwise. */
4095 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
4096 gfc_expr
*stride
, mpz_t last
)
4100 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
4101 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
4102 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
4105 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
4106 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
4109 if (stride
== NULL
|| compare_bound_int(stride
, 1) == CMP_EQ
)
4111 if (compare_bound (start
, end
) == CMP_GT
)
4113 mpz_set (last
, end
->value
.integer
);
4117 if (compare_bound_int (stride
, 0) == CMP_GT
)
4119 /* Stride is positive */
4120 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
4125 /* Stride is negative */
4126 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
4131 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
4132 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
4133 mpz_sub (last
, end
->value
.integer
, rem
);
4140 /* Compare a single dimension of an array reference to the array
4144 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
4148 if (ar
->dimen_type
[i
] == DIMEN_STAR
)
4150 gcc_assert (ar
->stride
[i
] == NULL
);
4151 /* This implies [*] as [*:] and [*:3] are not possible. */
4152 if (ar
->start
[i
] == NULL
)
4154 gcc_assert (ar
->end
[i
] == NULL
);
4159 /* Given start, end and stride values, calculate the minimum and
4160 maximum referenced indexes. */
4162 switch (ar
->dimen_type
[i
])
4165 case DIMEN_THIS_IMAGE
:
4170 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
4173 gfc_warning ("Array reference at %L is out of bounds "
4174 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4175 mpz_get_si (ar
->start
[i
]->value
.integer
),
4176 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4178 gfc_warning ("Array reference at %L is out of bounds "
4179 "(%ld < %ld) in codimension %d", &ar
->c_where
[i
],
4180 mpz_get_si (ar
->start
[i
]->value
.integer
),
4181 mpz_get_si (as
->lower
[i
]->value
.integer
),
4185 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
4188 gfc_warning ("Array reference at %L is out of bounds "
4189 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4190 mpz_get_si (ar
->start
[i
]->value
.integer
),
4191 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4193 gfc_warning ("Array reference at %L is out of bounds "
4194 "(%ld > %ld) in codimension %d", &ar
->c_where
[i
],
4195 mpz_get_si (ar
->start
[i
]->value
.integer
),
4196 mpz_get_si (as
->upper
[i
]->value
.integer
),
4205 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4206 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4208 comparison comp_start_end
= compare_bound (AR_START
, AR_END
);
4210 /* Check for zero stride, which is not allowed. */
4211 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
4213 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
4217 /* if start == len || (stride > 0 && start < len)
4218 || (stride < 0 && start > len),
4219 then the array section contains at least one element. In this
4220 case, there is an out-of-bounds access if
4221 (start < lower || start > upper). */
4222 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
4223 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
4224 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
4225 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
4226 && comp_start_end
== CMP_GT
))
4228 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
4230 gfc_warning ("Lower array reference at %L is out of bounds "
4231 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4232 mpz_get_si (AR_START
->value
.integer
),
4233 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4236 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
4238 gfc_warning ("Lower array reference at %L is out of bounds "
4239 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4240 mpz_get_si (AR_START
->value
.integer
),
4241 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4246 /* If we can compute the highest index of the array section,
4247 then it also has to be between lower and upper. */
4248 mpz_init (last_value
);
4249 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
4252 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
4254 gfc_warning ("Upper array reference at %L is out of bounds "
4255 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4256 mpz_get_si (last_value
),
4257 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4258 mpz_clear (last_value
);
4261 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
4263 gfc_warning ("Upper array reference at %L is out of bounds "
4264 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4265 mpz_get_si (last_value
),
4266 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4267 mpz_clear (last_value
);
4271 mpz_clear (last_value
);
4279 gfc_internal_error ("check_dimension(): Bad array reference");
4286 /* Compare an array reference with an array specification. */
4289 compare_spec_to_ref (gfc_array_ref
*ar
)
4296 /* TODO: Full array sections are only allowed as actual parameters. */
4297 if (as
->type
== AS_ASSUMED_SIZE
4298 && (/*ar->type == AR_FULL
4299 ||*/ (ar
->type
== AR_SECTION
4300 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
4302 gfc_error ("Rightmost upper bound of assumed size array section "
4303 "not specified at %L", &ar
->where
);
4307 if (ar
->type
== AR_FULL
)
4310 if (as
->rank
!= ar
->dimen
)
4312 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4313 &ar
->where
, ar
->dimen
, as
->rank
);
4317 /* ar->codimen == 0 is a local array. */
4318 if (as
->corank
!= ar
->codimen
&& ar
->codimen
!= 0)
4320 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4321 &ar
->where
, ar
->codimen
, as
->corank
);
4325 for (i
= 0; i
< as
->rank
; i
++)
4326 if (check_dimension (i
, ar
, as
) == FAILURE
)
4329 /* Local access has no coarray spec. */
4330 if (ar
->codimen
!= 0)
4331 for (i
= as
->rank
; i
< as
->rank
+ as
->corank
; i
++)
4333 if (ar
->dimen_type
[i
] != DIMEN_ELEMENT
&& !ar
->in_allocate
4334 && ar
->dimen_type
[i
] != DIMEN_THIS_IMAGE
)
4336 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4337 i
+ 1 - as
->rank
, &ar
->where
);
4340 if (check_dimension (i
, ar
, as
) == FAILURE
)
4344 if (as
->corank
&& ar
->codimen
== 0)
4347 ar
->codimen
= as
->corank
;
4348 for (n
= ar
->dimen
; n
< ar
->dimen
+ ar
->codimen
; n
++)
4349 ar
->dimen_type
[n
] = DIMEN_THIS_IMAGE
;
4356 /* Resolve one part of an array index. */
4359 gfc_resolve_index_1 (gfc_expr
*index
, int check_scalar
,
4360 int force_index_integer_kind
)
4367 if (gfc_resolve_expr (index
) == FAILURE
)
4370 if (check_scalar
&& index
->rank
!= 0)
4372 gfc_error ("Array index at %L must be scalar", &index
->where
);
4376 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
4378 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4379 &index
->where
, gfc_basic_typename (index
->ts
.type
));
4383 if (index
->ts
.type
== BT_REAL
)
4384 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: REAL array index at %L",
4385 &index
->where
) == FAILURE
)
4388 if ((index
->ts
.kind
!= gfc_index_integer_kind
4389 && force_index_integer_kind
)
4390 || index
->ts
.type
!= BT_INTEGER
)
4393 ts
.type
= BT_INTEGER
;
4394 ts
.kind
= gfc_index_integer_kind
;
4396 gfc_convert_type_warn (index
, &ts
, 2, 0);
4402 /* Resolve one part of an array index. */
4405 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
4407 return gfc_resolve_index_1 (index
, check_scalar
, 1);
4410 /* Resolve a dim argument to an intrinsic function. */
4413 gfc_resolve_dim_arg (gfc_expr
*dim
)
4418 if (gfc_resolve_expr (dim
) == FAILURE
)
4423 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
4428 if (dim
->ts
.type
!= BT_INTEGER
)
4430 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
4434 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
4439 ts
.type
= BT_INTEGER
;
4440 ts
.kind
= gfc_index_integer_kind
;
4442 gfc_convert_type_warn (dim
, &ts
, 2, 0);
4448 /* Given an expression that contains array references, update those array
4449 references to point to the right array specifications. While this is
4450 filled in during matching, this information is difficult to save and load
4451 in a module, so we take care of it here.
4453 The idea here is that the original array reference comes from the
4454 base symbol. We traverse the list of reference structures, setting
4455 the stored reference to references. Component references can
4456 provide an additional array specification. */
4459 find_array_spec (gfc_expr
*e
)
4463 gfc_symbol
*derived
;
4466 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
4467 as
= CLASS_DATA (e
->symtree
->n
.sym
)->as
;
4469 as
= e
->symtree
->n
.sym
->as
;
4472 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4477 gfc_internal_error ("find_array_spec(): Missing spec");
4484 if (derived
== NULL
)
4485 derived
= e
->symtree
->n
.sym
->ts
.u
.derived
;
4487 if (derived
->attr
.is_class
)
4488 derived
= derived
->components
->ts
.u
.derived
;
4490 c
= derived
->components
;
4492 for (; c
; c
= c
->next
)
4493 if (c
== ref
->u
.c
.component
)
4495 /* Track the sequence of component references. */
4496 if (c
->ts
.type
== BT_DERIVED
)
4497 derived
= c
->ts
.u
.derived
;
4502 gfc_internal_error ("find_array_spec(): Component not found");
4504 if (c
->attr
.dimension
)
4507 gfc_internal_error ("find_array_spec(): unused as(1)");
4518 gfc_internal_error ("find_array_spec(): unused as(2)");
4522 /* Resolve an array reference. */
4525 resolve_array_ref (gfc_array_ref
*ar
)
4527 int i
, check_scalar
;
4530 for (i
= 0; i
< ar
->dimen
+ ar
->codimen
; i
++)
4532 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4534 /* Do not force gfc_index_integer_kind for the start. We can
4535 do fine with any integer kind. This avoids temporary arrays
4536 created for indexing with a vector. */
4537 if (gfc_resolve_index_1 (ar
->start
[i
], check_scalar
, 0) == FAILURE
)
4539 if (gfc_resolve_index (ar
->end
[i
], check_scalar
) == FAILURE
)
4541 if (gfc_resolve_index (ar
->stride
[i
], check_scalar
) == FAILURE
)
4546 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4550 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4554 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4555 if (e
->expr_type
== EXPR_VARIABLE
4556 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4557 ar
->start
[i
] = gfc_get_parentheses (e
);
4561 gfc_error ("Array index at %L is an array of rank %d",
4562 &ar
->c_where
[i
], e
->rank
);
4566 /* Fill in the upper bound, which may be lower than the
4567 specified one for something like a(2:10:5), which is
4568 identical to a(2:7:5). Only relevant for strides not equal
4570 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4571 && ar
->stride
[i
] != NULL
&& ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
4572 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1L) != 0)
4576 if (gfc_ref_dimen_size (ar
, i
, &size
, &end
) == SUCCESS
)
4578 if (ar
->end
[i
] == NULL
)
4581 gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
4583 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4585 else if (ar
->end
[i
]->ts
.type
== BT_INTEGER
4586 && ar
->end
[i
]->expr_type
== EXPR_CONSTANT
)
4588 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4599 if (ar
->type
== AR_FULL
&& ar
->as
->rank
== 0)
4600 ar
->type
= AR_ELEMENT
;
4602 /* If the reference type is unknown, figure out what kind it is. */
4604 if (ar
->type
== AR_UNKNOWN
)
4606 ar
->type
= AR_ELEMENT
;
4607 for (i
= 0; i
< ar
->dimen
; i
++)
4608 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4609 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
4611 ar
->type
= AR_SECTION
;
4616 if (!ar
->as
->cray_pointee
&& compare_spec_to_ref (ar
) == FAILURE
)
4624 resolve_substring (gfc_ref
*ref
)
4626 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4628 if (ref
->u
.ss
.start
!= NULL
)
4630 if (gfc_resolve_expr (ref
->u
.ss
.start
) == FAILURE
)
4633 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
4635 gfc_error ("Substring start index at %L must be of type INTEGER",
4636 &ref
->u
.ss
.start
->where
);
4640 if (ref
->u
.ss
.start
->rank
!= 0)
4642 gfc_error ("Substring start index at %L must be scalar",
4643 &ref
->u
.ss
.start
->where
);
4647 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
4648 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4649 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4651 gfc_error ("Substring start index at %L is less than one",
4652 &ref
->u
.ss
.start
->where
);
4657 if (ref
->u
.ss
.end
!= NULL
)
4659 if (gfc_resolve_expr (ref
->u
.ss
.end
) == FAILURE
)
4662 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
4664 gfc_error ("Substring end index at %L must be of type INTEGER",
4665 &ref
->u
.ss
.end
->where
);
4669 if (ref
->u
.ss
.end
->rank
!= 0)
4671 gfc_error ("Substring end index at %L must be scalar",
4672 &ref
->u
.ss
.end
->where
);
4676 if (ref
->u
.ss
.length
!= NULL
4677 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
4678 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4679 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4681 gfc_error ("Substring end index at %L exceeds the string length",
4682 &ref
->u
.ss
.start
->where
);
4686 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
4687 gfc_integer_kinds
[k
].huge
) == CMP_GT
4688 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4689 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4691 gfc_error ("Substring end index at %L is too large",
4692 &ref
->u
.ss
.end
->where
);
4701 /* This function supplies missing substring charlens. */
4704 gfc_resolve_substring_charlen (gfc_expr
*e
)
4707 gfc_expr
*start
, *end
;
4709 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
4710 if (char_ref
->type
== REF_SUBSTRING
)
4716 gcc_assert (char_ref
->next
== NULL
);
4720 if (e
->ts
.u
.cl
->length
)
4721 gfc_free_expr (e
->ts
.u
.cl
->length
);
4722 else if (e
->expr_type
== EXPR_VARIABLE
4723 && e
->symtree
->n
.sym
->attr
.dummy
)
4727 e
->ts
.type
= BT_CHARACTER
;
4728 e
->ts
.kind
= gfc_default_character_kind
;
4731 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4733 if (char_ref
->u
.ss
.start
)
4734 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
4736 start
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
4738 if (char_ref
->u
.ss
.end
)
4739 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
4740 else if (e
->expr_type
== EXPR_VARIABLE
)
4741 end
= gfc_copy_expr (e
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4748 /* Length = (end - start +1). */
4749 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
4750 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
,
4751 gfc_get_int_expr (gfc_default_integer_kind
,
4754 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4755 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4757 /* Make sure that the length is simplified. */
4758 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
4759 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4763 /* Resolve subtype references. */
4766 resolve_ref (gfc_expr
*expr
)
4768 int current_part_dimension
, n_components
, seen_part_dimension
;
4771 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4772 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
4774 find_array_spec (expr
);
4778 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4782 if (resolve_array_ref (&ref
->u
.ar
) == FAILURE
)
4790 resolve_substring (ref
);
4794 /* Check constraints on part references. */
4796 current_part_dimension
= 0;
4797 seen_part_dimension
= 0;
4800 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4805 switch (ref
->u
.ar
.type
)
4808 /* Coarray scalar. */
4809 if (ref
->u
.ar
.as
->rank
== 0)
4811 current_part_dimension
= 0;
4816 current_part_dimension
= 1;
4820 current_part_dimension
= 0;
4824 gfc_internal_error ("resolve_ref(): Bad array reference");
4830 if (current_part_dimension
|| seen_part_dimension
)
4833 if (ref
->u
.c
.component
->attr
.pointer
4834 || ref
->u
.c
.component
->attr
.proc_pointer
)
4836 gfc_error ("Component to the right of a part reference "
4837 "with nonzero rank must not have the POINTER "
4838 "attribute at %L", &expr
->where
);
4841 else if (ref
->u
.c
.component
->attr
.allocatable
)
4843 gfc_error ("Component to the right of a part reference "
4844 "with nonzero rank must not have the ALLOCATABLE "
4845 "attribute at %L", &expr
->where
);
4857 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
4858 || ref
->next
== NULL
)
4859 && current_part_dimension
4860 && seen_part_dimension
)
4862 gfc_error ("Two or more part references with nonzero rank must "
4863 "not be specified at %L", &expr
->where
);
4867 if (ref
->type
== REF_COMPONENT
)
4869 if (current_part_dimension
)
4870 seen_part_dimension
= 1;
4872 /* reset to make sure */
4873 current_part_dimension
= 0;
4881 /* Given an expression, determine its shape. This is easier than it sounds.
4882 Leaves the shape array NULL if it is not possible to determine the shape. */
4885 expression_shape (gfc_expr
*e
)
4887 mpz_t array
[GFC_MAX_DIMENSIONS
];
4890 if (e
->rank
== 0 || e
->shape
!= NULL
)
4893 for (i
= 0; i
< e
->rank
; i
++)
4894 if (gfc_array_dimen_size (e
, i
, &array
[i
]) == FAILURE
)
4897 e
->shape
= gfc_get_shape (e
->rank
);
4899 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
4904 for (i
--; i
>= 0; i
--)
4905 mpz_clear (array
[i
]);
4909 /* Given a variable expression node, compute the rank of the expression by
4910 examining the base symbol and any reference structures it may have. */
4913 expression_rank (gfc_expr
*e
)
4918 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4919 could lead to serious confusion... */
4920 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
4924 if (e
->expr_type
== EXPR_ARRAY
)
4926 /* Constructors can have a rank different from one via RESHAPE(). */
4928 if (e
->symtree
== NULL
)
4934 e
->rank
= (e
->symtree
->n
.sym
->as
== NULL
)
4935 ? 0 : e
->symtree
->n
.sym
->as
->rank
;
4941 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4943 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.proc_pointer
4944 && ref
->u
.c
.component
->attr
.function
&& !ref
->next
)
4945 rank
= ref
->u
.c
.component
->as
? ref
->u
.c
.component
->as
->rank
: 0;
4947 if (ref
->type
!= REF_ARRAY
)
4950 if (ref
->u
.ar
.type
== AR_FULL
)
4952 rank
= ref
->u
.ar
.as
->rank
;
4956 if (ref
->u
.ar
.type
== AR_SECTION
)
4958 /* Figure out the rank of the section. */
4960 gfc_internal_error ("expression_rank(): Two array specs");
4962 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
4963 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
4964 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
4974 expression_shape (e
);
4978 /* Resolve a variable expression. */
4981 resolve_variable (gfc_expr
*e
)
4988 if (e
->symtree
== NULL
)
4990 sym
= e
->symtree
->n
.sym
;
4992 /* If this is an associate-name, it may be parsed with an array reference
4993 in error even though the target is scalar. Fail directly in this case. */
4994 if (sym
->assoc
&& !sym
->attr
.dimension
&& e
->ref
&& e
->ref
->type
== REF_ARRAY
)
4997 /* On the other hand, the parser may not have known this is an array;
4998 in this case, we have to add a FULL reference. */
4999 if (sym
->assoc
&& sym
->attr
.dimension
&& !e
->ref
)
5001 e
->ref
= gfc_get_ref ();
5002 e
->ref
->type
= REF_ARRAY
;
5003 e
->ref
->u
.ar
.type
= AR_FULL
;
5004 e
->ref
->u
.ar
.dimen
= 0;
5007 if (e
->ref
&& resolve_ref (e
) == FAILURE
)
5010 if (sym
->attr
.flavor
== FL_PROCEDURE
5011 && (!sym
->attr
.function
5012 || (sym
->attr
.function
&& sym
->result
5013 && sym
->result
->attr
.proc_pointer
5014 && !sym
->result
->attr
.function
)))
5016 e
->ts
.type
= BT_PROCEDURE
;
5017 goto resolve_procedure
;
5020 if (sym
->ts
.type
!= BT_UNKNOWN
)
5021 gfc_variable_attr (e
, &e
->ts
);
5024 /* Must be a simple variable reference. */
5025 if (gfc_set_default_type (sym
, 1, sym
->ns
) == FAILURE
)
5030 if (check_assumed_size_reference (sym
, e
))
5033 /* Deal with forward references to entries during resolve_code, to
5034 satisfy, at least partially, 12.5.2.5. */
5035 if (gfc_current_ns
->entries
5036 && current_entry_id
== sym
->entry_id
5039 && cs_base
->current
->op
!= EXEC_ENTRY
)
5041 gfc_entry_list
*entry
;
5042 gfc_formal_arglist
*formal
;
5046 /* If the symbol is a dummy... */
5047 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
5049 entry
= gfc_current_ns
->entries
;
5052 /* ...test if the symbol is a parameter of previous entries. */
5053 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
5054 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
5056 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
5060 /* If it has not been seen as a dummy, this is an error. */
5063 if (specification_expr
)
5064 gfc_error ("Variable '%s', used in a specification expression"
5065 ", is referenced at %L before the ENTRY statement "
5066 "in which it is a parameter",
5067 sym
->name
, &cs_base
->current
->loc
);
5069 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5070 "statement in which it is a parameter",
5071 sym
->name
, &cs_base
->current
->loc
);
5076 /* Now do the same check on the specification expressions. */
5077 specification_expr
= 1;
5078 if (sym
->ts
.type
== BT_CHARACTER
5079 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
5083 for (n
= 0; n
< sym
->as
->rank
; n
++)
5085 specification_expr
= 1;
5086 if (gfc_resolve_expr (sym
->as
->lower
[n
]) == FAILURE
)
5088 specification_expr
= 1;
5089 if (gfc_resolve_expr (sym
->as
->upper
[n
]) == FAILURE
)
5092 specification_expr
= 0;
5095 /* Update the symbol's entry level. */
5096 sym
->entry_id
= current_entry_id
+ 1;
5099 /* If a symbol has been host_associated mark it. This is used latter,
5100 to identify if aliasing is possible via host association. */
5101 if (sym
->attr
.flavor
== FL_VARIABLE
5102 && gfc_current_ns
->parent
5103 && (gfc_current_ns
->parent
== sym
->ns
5104 || (gfc_current_ns
->parent
->parent
5105 && gfc_current_ns
->parent
->parent
== sym
->ns
)))
5106 sym
->attr
.host_assoc
= 1;
5109 if (t
== SUCCESS
&& resolve_procedure_expression (e
) == FAILURE
)
5112 /* F2008, C617 and C1229. */
5113 if (!inquiry_argument
&& (e
->ts
.type
== BT_CLASS
|| e
->ts
.type
== BT_DERIVED
)
5114 && gfc_is_coindexed (e
))
5116 gfc_ref
*ref
, *ref2
= NULL
;
5118 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5120 if (ref
->type
== REF_COMPONENT
)
5122 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5126 for ( ; ref
; ref
= ref
->next
)
5127 if (ref
->type
== REF_COMPONENT
)
5130 /* Expression itself is not coindexed object. */
5131 if (ref
&& e
->ts
.type
== BT_CLASS
)
5133 gfc_error ("Polymorphic subobject of coindexed object at %L",
5138 /* Expression itself is coindexed object. */
5142 c
= ref2
? ref2
->u
.c
.component
: e
->symtree
->n
.sym
->components
;
5143 for ( ; c
; c
= c
->next
)
5144 if (c
->attr
.allocatable
&& c
->ts
.type
== BT_CLASS
)
5146 gfc_error ("Coindexed object with polymorphic allocatable "
5147 "subcomponent at %L", &e
->where
);
5158 /* Checks to see that the correct symbol has been host associated.
5159 The only situation where this arises is that in which a twice
5160 contained function is parsed after the host association is made.
5161 Therefore, on detecting this, change the symbol in the expression
5162 and convert the array reference into an actual arglist if the old
5163 symbol is a variable. */
5165 check_host_association (gfc_expr
*e
)
5167 gfc_symbol
*sym
, *old_sym
;
5171 gfc_actual_arglist
*arg
, *tail
= NULL
;
5172 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
5174 /* If the expression is the result of substitution in
5175 interface.c(gfc_extend_expr) because there is no way in
5176 which the host association can be wrong. */
5177 if (e
->symtree
== NULL
5178 || e
->symtree
->n
.sym
== NULL
5179 || e
->user_operator
)
5182 old_sym
= e
->symtree
->n
.sym
;
5184 if (gfc_current_ns
->parent
5185 && old_sym
->ns
!= gfc_current_ns
)
5187 /* Use the 'USE' name so that renamed module symbols are
5188 correctly handled. */
5189 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
5191 if (sym
&& old_sym
!= sym
5192 && sym
->ts
.type
== old_sym
->ts
.type
5193 && sym
->attr
.flavor
== FL_PROCEDURE
5194 && sym
->attr
.contained
)
5196 /* Clear the shape, since it might not be valid. */
5197 if (e
->shape
!= NULL
)
5199 for (n
= 0; n
< e
->rank
; n
++)
5200 mpz_clear (e
->shape
[n
]);
5205 /* Give the expression the right symtree! */
5206 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
5207 gcc_assert (st
!= NULL
);
5209 if (old_sym
->attr
.flavor
== FL_PROCEDURE
5210 || e
->expr_type
== EXPR_FUNCTION
)
5212 /* Original was function so point to the new symbol, since
5213 the actual argument list is already attached to the
5215 e
->value
.function
.esym
= NULL
;
5220 /* Original was variable so convert array references into
5221 an actual arglist. This does not need any checking now
5222 since gfc_resolve_function will take care of it. */
5223 e
->value
.function
.actual
= NULL
;
5224 e
->expr_type
= EXPR_FUNCTION
;
5227 /* Ambiguity will not arise if the array reference is not
5228 the last reference. */
5229 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5230 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
5233 gcc_assert (ref
->type
== REF_ARRAY
);
5235 /* Grab the start expressions from the array ref and
5236 copy them into actual arguments. */
5237 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
5239 arg
= gfc_get_actual_arglist ();
5240 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
5241 if (e
->value
.function
.actual
== NULL
)
5242 tail
= e
->value
.function
.actual
= arg
;
5250 /* Dump the reference list and set the rank. */
5251 gfc_free_ref_list (e
->ref
);
5253 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
5256 gfc_resolve_expr (e
);
5260 /* This might have changed! */
5261 return e
->expr_type
== EXPR_FUNCTION
;
5266 gfc_resolve_character_operator (gfc_expr
*e
)
5268 gfc_expr
*op1
= e
->value
.op
.op1
;
5269 gfc_expr
*op2
= e
->value
.op
.op2
;
5270 gfc_expr
*e1
= NULL
;
5271 gfc_expr
*e2
= NULL
;
5273 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
5275 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
5276 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
5277 else if (op1
->expr_type
== EXPR_CONSTANT
)
5278 e1
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5279 op1
->value
.character
.length
);
5281 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
5282 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
5283 else if (op2
->expr_type
== EXPR_CONSTANT
)
5284 e2
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5285 op2
->value
.character
.length
);
5287 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5292 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
5293 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
5294 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
5295 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
5296 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
5302 /* Ensure that an character expression has a charlen and, if possible, a
5303 length expression. */
5306 fixup_charlen (gfc_expr
*e
)
5308 /* The cases fall through so that changes in expression type and the need
5309 for multiple fixes are picked up. In all circumstances, a charlen should
5310 be available for the middle end to hang a backend_decl on. */
5311 switch (e
->expr_type
)
5314 gfc_resolve_character_operator (e
);
5317 if (e
->expr_type
== EXPR_ARRAY
)
5318 gfc_resolve_character_array_constructor (e
);
5320 case EXPR_SUBSTRING
:
5321 if (!e
->ts
.u
.cl
&& e
->ref
)
5322 gfc_resolve_substring_charlen (e
);
5326 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5333 /* Update an actual argument to include the passed-object for type-bound
5334 procedures at the right position. */
5336 static gfc_actual_arglist
*
5337 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
5340 gcc_assert (argpos
> 0);
5344 gfc_actual_arglist
* result
;
5346 result
= gfc_get_actual_arglist ();
5350 result
->name
= name
;
5356 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
5358 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
5363 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5366 extract_compcall_passed_object (gfc_expr
* e
)
5370 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5372 if (e
->value
.compcall
.base_object
)
5373 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
5376 po
= gfc_get_expr ();
5377 po
->expr_type
= EXPR_VARIABLE
;
5378 po
->symtree
= e
->symtree
;
5379 po
->ref
= gfc_copy_ref (e
->ref
);
5380 po
->where
= e
->where
;
5383 if (gfc_resolve_expr (po
) == FAILURE
)
5390 /* Update the arglist of an EXPR_COMPCALL expression to include the
5394 update_compcall_arglist (gfc_expr
* e
)
5397 gfc_typebound_proc
* tbp
;
5399 tbp
= e
->value
.compcall
.tbp
;
5404 po
= extract_compcall_passed_object (e
);
5408 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
5414 gcc_assert (tbp
->pass_arg_num
> 0);
5415 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5423 /* Extract the passed object from a PPC call (a copy of it). */
5426 extract_ppc_passed_object (gfc_expr
*e
)
5431 po
= gfc_get_expr ();
5432 po
->expr_type
= EXPR_VARIABLE
;
5433 po
->symtree
= e
->symtree
;
5434 po
->ref
= gfc_copy_ref (e
->ref
);
5435 po
->where
= e
->where
;
5437 /* Remove PPC reference. */
5439 while ((*ref
)->next
)
5440 ref
= &(*ref
)->next
;
5441 gfc_free_ref_list (*ref
);
5444 if (gfc_resolve_expr (po
) == FAILURE
)
5451 /* Update the actual arglist of a procedure pointer component to include the
5455 update_ppc_arglist (gfc_expr
* e
)
5459 gfc_typebound_proc
* tb
;
5461 if (!gfc_is_proc_ptr_comp (e
, &ppc
))
5468 else if (tb
->nopass
)
5471 po
= extract_ppc_passed_object (e
);
5478 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
5483 if (po
->ts
.type
== BT_DERIVED
&& po
->ts
.u
.derived
->attr
.abstract
)
5485 gfc_error ("Base object for procedure-pointer component call at %L is of"
5486 " ABSTRACT type '%s'", &e
->where
, po
->ts
.u
.derived
->name
);
5490 gcc_assert (tb
->pass_arg_num
> 0);
5491 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5499 /* Check that the object a TBP is called on is valid, i.e. it must not be
5500 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5503 check_typebound_baseobject (gfc_expr
* e
)
5506 gfc_try return_value
= FAILURE
;
5508 base
= extract_compcall_passed_object (e
);
5512 gcc_assert (base
->ts
.type
== BT_DERIVED
|| base
->ts
.type
== BT_CLASS
);
5515 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
5517 gfc_error ("Base object for type-bound procedure call at %L is of"
5518 " ABSTRACT type '%s'", &e
->where
, base
->ts
.u
.derived
->name
);
5522 /* F08:C1230. If the procedure called is NOPASS,
5523 the base object must be scalar. */
5524 if (e
->value
.compcall
.tbp
->nopass
&& base
->rank
> 0)
5526 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5527 " be scalar", &e
->where
);
5531 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5534 gfc_error ("Non-scalar base object at %L currently not implemented",
5539 return_value
= SUCCESS
;
5542 gfc_free_expr (base
);
5543 return return_value
;
5547 /* Resolve a call to a type-bound procedure, either function or subroutine,
5548 statically from the data in an EXPR_COMPCALL expression. The adapted
5549 arglist and the target-procedure symtree are returned. */
5552 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
5553 gfc_actual_arglist
** actual
)
5555 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5556 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5558 /* Update the actual arglist for PASS. */
5559 if (update_compcall_arglist (e
) == FAILURE
)
5562 *actual
= e
->value
.compcall
.actual
;
5563 *target
= e
->value
.compcall
.tbp
->u
.specific
;
5565 gfc_free_ref_list (e
->ref
);
5567 e
->value
.compcall
.actual
= NULL
;
5573 /* Get the ultimate declared type from an expression. In addition,
5574 return the last class/derived type reference and the copy of the
5577 get_declared_from_expr (gfc_ref
**class_ref
, gfc_ref
**new_ref
,
5580 gfc_symbol
*declared
;
5587 *new_ref
= gfc_copy_ref (e
->ref
);
5589 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5591 if (ref
->type
!= REF_COMPONENT
)
5594 if (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5595 || ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5597 declared
= ref
->u
.c
.component
->ts
.u
.derived
;
5603 if (declared
== NULL
)
5604 declared
= e
->symtree
->n
.sym
->ts
.u
.derived
;
5610 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5611 which of the specific bindings (if any) matches the arglist and transform
5612 the expression into a call of that binding. */
5615 resolve_typebound_generic_call (gfc_expr
* e
, const char **name
)
5617 gfc_typebound_proc
* genproc
;
5618 const char* genname
;
5620 gfc_symbol
*derived
;
5622 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5623 genname
= e
->value
.compcall
.name
;
5624 genproc
= e
->value
.compcall
.tbp
;
5626 if (!genproc
->is_generic
)
5629 /* Try the bindings on this type and in the inheritance hierarchy. */
5630 for (; genproc
; genproc
= genproc
->overridden
)
5634 gcc_assert (genproc
->is_generic
);
5635 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
5638 gfc_actual_arglist
* args
;
5641 gcc_assert (g
->specific
);
5643 if (g
->specific
->error
)
5646 target
= g
->specific
->u
.specific
->n
.sym
;
5648 /* Get the right arglist by handling PASS/NOPASS. */
5649 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
5650 if (!g
->specific
->nopass
)
5653 po
= extract_compcall_passed_object (e
);
5657 gcc_assert (g
->specific
->pass_arg_num
> 0);
5658 gcc_assert (!g
->specific
->error
);
5659 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
5660 g
->specific
->pass_arg
);
5662 resolve_actual_arglist (args
, target
->attr
.proc
,
5663 is_external_proc (target
) && !target
->formal
);
5665 /* Check if this arglist matches the formal. */
5666 matches
= gfc_arglist_matches_symbol (&args
, target
);
5668 /* Clean up and break out of the loop if we've found it. */
5669 gfc_free_actual_arglist (args
);
5672 e
->value
.compcall
.tbp
= g
->specific
;
5673 genname
= g
->specific_st
->name
;
5674 /* Pass along the name for CLASS methods, where the vtab
5675 procedure pointer component has to be referenced. */
5683 /* Nothing matching found! */
5684 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5685 " '%s' at %L", genname
, &e
->where
);
5689 /* Make sure that we have the right specific instance for the name. */
5690 derived
= get_declared_from_expr (NULL
, NULL
, e
);
5692 st
= gfc_find_typebound_proc (derived
, NULL
, genname
, true, &e
->where
);
5694 e
->value
.compcall
.tbp
= st
->n
.tb
;
5700 /* Resolve a call to a type-bound subroutine. */
5703 resolve_typebound_call (gfc_code
* c
, const char **name
)
5705 gfc_actual_arglist
* newactual
;
5706 gfc_symtree
* target
;
5708 /* Check that's really a SUBROUTINE. */
5709 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
5711 gfc_error ("'%s' at %L should be a SUBROUTINE",
5712 c
->expr1
->value
.compcall
.name
, &c
->loc
);
5716 if (check_typebound_baseobject (c
->expr1
) == FAILURE
)
5719 /* Pass along the name for CLASS methods, where the vtab
5720 procedure pointer component has to be referenced. */
5722 *name
= c
->expr1
->value
.compcall
.name
;
5724 if (resolve_typebound_generic_call (c
->expr1
, name
) == FAILURE
)
5727 /* Transform into an ordinary EXEC_CALL for now. */
5729 if (resolve_typebound_static (c
->expr1
, &target
, &newactual
) == FAILURE
)
5732 c
->ext
.actual
= newactual
;
5733 c
->symtree
= target
;
5734 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
5736 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
5738 gfc_free_expr (c
->expr1
);
5739 c
->expr1
= gfc_get_expr ();
5740 c
->expr1
->expr_type
= EXPR_FUNCTION
;
5741 c
->expr1
->symtree
= target
;
5742 c
->expr1
->where
= c
->loc
;
5744 return resolve_call (c
);
5748 /* Resolve a component-call expression. */
5750 resolve_compcall (gfc_expr
* e
, const char **name
)
5752 gfc_actual_arglist
* newactual
;
5753 gfc_symtree
* target
;
5755 /* Check that's really a FUNCTION. */
5756 if (!e
->value
.compcall
.tbp
->function
)
5758 gfc_error ("'%s' at %L should be a FUNCTION",
5759 e
->value
.compcall
.name
, &e
->where
);
5763 /* These must not be assign-calls! */
5764 gcc_assert (!e
->value
.compcall
.assign
);
5766 if (check_typebound_baseobject (e
) == FAILURE
)
5769 /* Pass along the name for CLASS methods, where the vtab
5770 procedure pointer component has to be referenced. */
5772 *name
= e
->value
.compcall
.name
;
5774 if (resolve_typebound_generic_call (e
, name
) == FAILURE
)
5776 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5778 /* Take the rank from the function's symbol. */
5779 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
5780 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
5782 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5783 arglist to the TBP's binding target. */
5785 if (resolve_typebound_static (e
, &target
, &newactual
) == FAILURE
)
5788 e
->value
.function
.actual
= newactual
;
5789 e
->value
.function
.name
= NULL
;
5790 e
->value
.function
.esym
= target
->n
.sym
;
5791 e
->value
.function
.isym
= NULL
;
5792 e
->symtree
= target
;
5793 e
->ts
= target
->n
.sym
->ts
;
5794 e
->expr_type
= EXPR_FUNCTION
;
5796 /* Resolution is not necessary if this is a class subroutine; this
5797 function only has to identify the specific proc. Resolution of
5798 the call will be done next in resolve_typebound_call. */
5799 return gfc_resolve_expr (e
);
5804 /* Resolve a typebound function, or 'method'. First separate all
5805 the non-CLASS references by calling resolve_compcall directly. */
5808 resolve_typebound_function (gfc_expr
* e
)
5810 gfc_symbol
*declared
;
5821 /* Deal with typebound operators for CLASS objects. */
5822 expr
= e
->value
.compcall
.base_object
;
5823 if (expr
&& expr
->ts
.type
== BT_CLASS
&& e
->value
.compcall
.name
)
5825 /* Since the typebound operators are generic, we have to ensure
5826 that any delays in resolution are corrected and that the vtab
5829 declared
= ts
.u
.derived
;
5830 c
= gfc_find_component (declared
, "_vptr", true, true);
5831 if (c
->ts
.u
.derived
== NULL
)
5832 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5834 if (resolve_compcall (e
, &name
) == FAILURE
)
5837 /* Use the generic name if it is there. */
5838 name
= name
? name
: e
->value
.function
.esym
->name
;
5839 e
->symtree
= expr
->symtree
;
5840 e
->ref
= gfc_copy_ref (expr
->ref
);
5841 gfc_add_vptr_component (e
);
5842 gfc_add_component_ref (e
, name
);
5843 e
->value
.function
.esym
= NULL
;
5848 return resolve_compcall (e
, NULL
);
5850 if (resolve_ref (e
) == FAILURE
)
5853 /* Get the CLASS declared type. */
5854 declared
= get_declared_from_expr (&class_ref
, &new_ref
, e
);
5856 /* Weed out cases of the ultimate component being a derived type. */
5857 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5858 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5860 gfc_free_ref_list (new_ref
);
5861 return resolve_compcall (e
, NULL
);
5864 c
= gfc_find_component (declared
, "_data", true, true);
5865 declared
= c
->ts
.u
.derived
;
5867 /* Treat the call as if it is a typebound procedure, in order to roll
5868 out the correct name for the specific function. */
5869 if (resolve_compcall (e
, &name
) == FAILURE
)
5873 /* Then convert the expression to a procedure pointer component call. */
5874 e
->value
.function
.esym
= NULL
;
5880 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5881 gfc_add_vptr_component (e
);
5882 gfc_add_component_ref (e
, name
);
5884 /* Recover the typespec for the expression. This is really only
5885 necessary for generic procedures, where the additional call
5886 to gfc_add_component_ref seems to throw the collection of the
5887 correct typespec. */
5892 /* Resolve a typebound subroutine, or 'method'. First separate all
5893 the non-CLASS references by calling resolve_typebound_call
5897 resolve_typebound_subroutine (gfc_code
*code
)
5899 gfc_symbol
*declared
;
5908 st
= code
->expr1
->symtree
;
5910 /* Deal with typebound operators for CLASS objects. */
5911 expr
= code
->expr1
->value
.compcall
.base_object
;
5912 if (expr
&& expr
->ts
.type
== BT_CLASS
&& code
->expr1
->value
.compcall
.name
)
5914 /* Since the typebound operators are generic, we have to ensure
5915 that any delays in resolution are corrected and that the vtab
5917 declared
= expr
->ts
.u
.derived
;
5918 c
= gfc_find_component (declared
, "_vptr", true, true);
5919 if (c
->ts
.u
.derived
== NULL
)
5920 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5922 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5925 /* Use the generic name if it is there. */
5926 name
= name
? name
: code
->expr1
->value
.function
.esym
->name
;
5927 code
->expr1
->symtree
= expr
->symtree
;
5928 code
->expr1
->ref
= gfc_copy_ref (expr
->ref
);
5929 gfc_add_vptr_component (code
->expr1
);
5930 gfc_add_component_ref (code
->expr1
, name
);
5931 code
->expr1
->value
.function
.esym
= NULL
;
5936 return resolve_typebound_call (code
, NULL
);
5938 if (resolve_ref (code
->expr1
) == FAILURE
)
5941 /* Get the CLASS declared type. */
5942 get_declared_from_expr (&class_ref
, &new_ref
, code
->expr1
);
5944 /* Weed out cases of the ultimate component being a derived type. */
5945 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5946 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5948 gfc_free_ref_list (new_ref
);
5949 return resolve_typebound_call (code
, NULL
);
5952 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5954 ts
= code
->expr1
->ts
;
5956 /* Then convert the expression to a procedure pointer component call. */
5957 code
->expr1
->value
.function
.esym
= NULL
;
5958 code
->expr1
->symtree
= st
;
5961 code
->expr1
->ref
= new_ref
;
5963 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5964 gfc_add_vptr_component (code
->expr1
);
5965 gfc_add_component_ref (code
->expr1
, name
);
5967 /* Recover the typespec for the expression. This is really only
5968 necessary for generic procedures, where the additional call
5969 to gfc_add_component_ref seems to throw the collection of the
5970 correct typespec. */
5971 code
->expr1
->ts
= ts
;
5976 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5979 resolve_ppc_call (gfc_code
* c
)
5981 gfc_component
*comp
;
5984 b
= gfc_is_proc_ptr_comp (c
->expr1
, &comp
);
5987 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
5988 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5990 if (!comp
->attr
.subroutine
)
5991 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
5993 if (resolve_ref (c
->expr1
) == FAILURE
)
5996 if (update_ppc_arglist (c
->expr1
) == FAILURE
)
5999 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
6001 if (resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
6002 comp
->formal
== NULL
) == FAILURE
)
6005 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
6011 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6014 resolve_expr_ppc (gfc_expr
* e
)
6016 gfc_component
*comp
;
6019 b
= gfc_is_proc_ptr_comp (e
, &comp
);
6022 /* Convert to EXPR_FUNCTION. */
6023 e
->expr_type
= EXPR_FUNCTION
;
6024 e
->value
.function
.isym
= NULL
;
6025 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
6027 if (comp
->as
!= NULL
)
6028 e
->rank
= comp
->as
->rank
;
6030 if (!comp
->attr
.function
)
6031 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
6033 if (resolve_ref (e
) == FAILURE
)
6036 if (resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
6037 comp
->formal
== NULL
) == FAILURE
)
6040 if (update_ppc_arglist (e
) == FAILURE
)
6043 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
6050 gfc_is_expandable_expr (gfc_expr
*e
)
6052 gfc_constructor
*con
;
6054 if (e
->expr_type
== EXPR_ARRAY
)
6056 /* Traverse the constructor looking for variables that are flavor
6057 parameter. Parameters must be expanded since they are fully used at
6059 con
= gfc_constructor_first (e
->value
.constructor
);
6060 for (; con
; con
= gfc_constructor_next (con
))
6062 if (con
->expr
->expr_type
== EXPR_VARIABLE
6063 && con
->expr
->symtree
6064 && (con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
6065 || con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_VARIABLE
))
6067 if (con
->expr
->expr_type
== EXPR_ARRAY
6068 && gfc_is_expandable_expr (con
->expr
))
6076 /* Resolve an expression. That is, make sure that types of operands agree
6077 with their operators, intrinsic operators are converted to function calls
6078 for overloaded types and unresolved function references are resolved. */
6081 gfc_resolve_expr (gfc_expr
*e
)
6089 /* inquiry_argument only applies to variables. */
6090 inquiry_save
= inquiry_argument
;
6091 if (e
->expr_type
!= EXPR_VARIABLE
)
6092 inquiry_argument
= false;
6094 switch (e
->expr_type
)
6097 t
= resolve_operator (e
);
6103 if (check_host_association (e
))
6104 t
= resolve_function (e
);
6107 t
= resolve_variable (e
);
6109 expression_rank (e
);
6112 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
6113 && e
->ref
->type
!= REF_SUBSTRING
)
6114 gfc_resolve_substring_charlen (e
);
6119 t
= resolve_typebound_function (e
);
6122 case EXPR_SUBSTRING
:
6123 t
= resolve_ref (e
);
6132 t
= resolve_expr_ppc (e
);
6137 if (resolve_ref (e
) == FAILURE
)
6140 t
= gfc_resolve_array_constructor (e
);
6141 /* Also try to expand a constructor. */
6144 expression_rank (e
);
6145 if (gfc_is_constant_expr (e
) || gfc_is_expandable_expr (e
))
6146 gfc_expand_constructor (e
, false);
6149 /* This provides the opportunity for the length of constructors with
6150 character valued function elements to propagate the string length
6151 to the expression. */
6152 if (t
== SUCCESS
&& e
->ts
.type
== BT_CHARACTER
)
6154 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6155 here rather then add a duplicate test for it above. */
6156 gfc_expand_constructor (e
, false);
6157 t
= gfc_resolve_character_array_constructor (e
);
6162 case EXPR_STRUCTURE
:
6163 t
= resolve_ref (e
);
6167 t
= resolve_structure_cons (e
, 0);
6171 t
= gfc_simplify_expr (e
, 0);
6175 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6178 if (e
->ts
.type
== BT_CHARACTER
&& t
== SUCCESS
&& !e
->ts
.u
.cl
)
6181 inquiry_argument
= inquiry_save
;
6187 /* Resolve an expression from an iterator. They must be scalar and have
6188 INTEGER or (optionally) REAL type. */
6191 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
6192 const char *name_msgid
)
6194 if (gfc_resolve_expr (expr
) == FAILURE
)
6197 if (expr
->rank
!= 0)
6199 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
6203 if (expr
->ts
.type
!= BT_INTEGER
)
6205 if (expr
->ts
.type
== BT_REAL
)
6208 return gfc_notify_std (GFC_STD_F95_DEL
,
6209 "Deleted feature: %s at %L must be integer",
6210 _(name_msgid
), &expr
->where
);
6213 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
6220 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
6228 /* Resolve the expressions in an iterator structure. If REAL_OK is
6229 false allow only INTEGER type iterators, otherwise allow REAL types. */
6232 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
)
6234 if (gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable")
6238 if (gfc_check_vardef_context (iter
->var
, false, _("iterator variable"))
6242 if (gfc_resolve_iterator_expr (iter
->start
, real_ok
,
6243 "Start expression in DO loop") == FAILURE
)
6246 if (gfc_resolve_iterator_expr (iter
->end
, real_ok
,
6247 "End expression in DO loop") == FAILURE
)
6250 if (gfc_resolve_iterator_expr (iter
->step
, real_ok
,
6251 "Step expression in DO loop") == FAILURE
)
6254 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
6256 if ((iter
->step
->ts
.type
== BT_INTEGER
6257 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
6258 || (iter
->step
->ts
.type
== BT_REAL
6259 && mpfr_sgn (iter
->step
->value
.real
) == 0))
6261 gfc_error ("Step expression in DO loop at %L cannot be zero",
6262 &iter
->step
->where
);
6267 /* Convert start, end, and step to the same type as var. */
6268 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
6269 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
6270 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6272 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
6273 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
6274 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6276 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
6277 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
6278 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 2);
6280 if (iter
->start
->expr_type
== EXPR_CONSTANT
6281 && iter
->end
->expr_type
== EXPR_CONSTANT
6282 && iter
->step
->expr_type
== EXPR_CONSTANT
)
6285 if (iter
->start
->ts
.type
== BT_INTEGER
)
6287 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
6288 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
6292 sgn
= mpfr_sgn (iter
->step
->value
.real
);
6293 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
6295 if ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0))
6296 gfc_warning ("DO loop at %L will be executed zero times",
6297 &iter
->step
->where
);
6304 /* Traversal function for find_forall_index. f == 2 signals that
6305 that variable itself is not to be checked - only the references. */
6308 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
6310 if (expr
->expr_type
!= EXPR_VARIABLE
)
6313 /* A scalar assignment */
6314 if (!expr
->ref
|| *f
== 1)
6316 if (expr
->symtree
->n
.sym
== sym
)
6328 /* Check whether the FORALL index appears in the expression or not.
6329 Returns SUCCESS if SYM is found in EXPR. */
6332 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
6334 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
6341 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6342 to be a scalar INTEGER variable. The subscripts and stride are scalar
6343 INTEGERs, and if stride is a constant it must be nonzero.
6344 Furthermore "A subscript or stride in a forall-triplet-spec shall
6345 not contain a reference to any index-name in the
6346 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6349 resolve_forall_iterators (gfc_forall_iterator
*it
)
6351 gfc_forall_iterator
*iter
, *iter2
;
6353 for (iter
= it
; iter
; iter
= iter
->next
)
6355 if (gfc_resolve_expr (iter
->var
) == SUCCESS
6356 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
6357 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6360 if (gfc_resolve_expr (iter
->start
) == SUCCESS
6361 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
6362 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6363 &iter
->start
->where
);
6364 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
6365 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6367 if (gfc_resolve_expr (iter
->end
) == SUCCESS
6368 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
6369 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6371 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
6372 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6374 if (gfc_resolve_expr (iter
->stride
) == SUCCESS
)
6376 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
6377 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6378 &iter
->stride
->where
, "INTEGER");
6380 if (iter
->stride
->expr_type
== EXPR_CONSTANT
6381 && mpz_cmp_ui(iter
->stride
->value
.integer
, 0) == 0)
6382 gfc_error ("FORALL stride expression at %L cannot be zero",
6383 &iter
->stride
->where
);
6385 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
6386 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 2);
6389 for (iter
= it
; iter
; iter
= iter
->next
)
6390 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
6392 if (find_forall_index (iter2
->start
,
6393 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6394 || find_forall_index (iter2
->end
,
6395 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6396 || find_forall_index (iter2
->stride
,
6397 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
)
6398 gfc_error ("FORALL index '%s' may not appear in triplet "
6399 "specification at %L", iter
->var
->symtree
->name
,
6400 &iter2
->start
->where
);
6405 /* Given a pointer to a symbol that is a derived type, see if it's
6406 inaccessible, i.e. if it's defined in another module and the components are
6407 PRIVATE. The search is recursive if necessary. Returns zero if no
6408 inaccessible components are found, nonzero otherwise. */
6411 derived_inaccessible (gfc_symbol
*sym
)
6415 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
6418 for (c
= sym
->components
; c
; c
= c
->next
)
6420 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
6428 /* Resolve the argument of a deallocate expression. The expression must be
6429 a pointer or a full array. */
6432 resolve_deallocate_expr (gfc_expr
*e
)
6434 symbol_attribute attr
;
6435 int allocatable
, pointer
;
6440 if (gfc_resolve_expr (e
) == FAILURE
)
6443 if (e
->expr_type
!= EXPR_VARIABLE
)
6446 sym
= e
->symtree
->n
.sym
;
6448 if (sym
->ts
.type
== BT_CLASS
)
6450 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6451 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6455 allocatable
= sym
->attr
.allocatable
;
6456 pointer
= sym
->attr
.pointer
;
6458 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6463 if (ref
->u
.ar
.type
!= AR_FULL
)
6468 c
= ref
->u
.c
.component
;
6469 if (c
->ts
.type
== BT_CLASS
)
6471 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6472 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6476 allocatable
= c
->attr
.allocatable
;
6477 pointer
= c
->attr
.pointer
;
6487 attr
= gfc_expr_attr (e
);
6489 if (allocatable
== 0 && attr
.pointer
== 0)
6492 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6498 && gfc_check_vardef_context (e
, true, _("DEALLOCATE object")) == FAILURE
)
6500 if (gfc_check_vardef_context (e
, false, _("DEALLOCATE object")) == FAILURE
)
6507 /* Returns true if the expression e contains a reference to the symbol sym. */
6509 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
6511 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
6518 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
6520 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
6524 /* Given the expression node e for an allocatable/pointer of derived type to be
6525 allocated, get the expression node to be initialized afterwards (needed for
6526 derived types with default initializers, and derived types with allocatable
6527 components that need nullification.) */
6530 gfc_expr_to_initialize (gfc_expr
*e
)
6536 result
= gfc_copy_expr (e
);
6538 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6539 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
6540 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
6542 ref
->u
.ar
.type
= AR_FULL
;
6544 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
6545 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
6547 result
->rank
= ref
->u
.ar
.dimen
;
6555 /* If the last ref of an expression is an array ref, return a copy of the
6556 expression with that one removed. Otherwise, a copy of the original
6557 expression. This is used for allocate-expressions and pointer assignment
6558 LHS, where there may be an array specification that needs to be stripped
6559 off when using gfc_check_vardef_context. */
6562 remove_last_array_ref (gfc_expr
* e
)
6567 e2
= gfc_copy_expr (e
);
6568 for (r
= &e2
->ref
; *r
; r
= &(*r
)->next
)
6569 if ((*r
)->type
== REF_ARRAY
&& !(*r
)->next
)
6571 gfc_free_ref_list (*r
);
6580 /* Used in resolve_allocate_expr to check that a allocation-object and
6581 a source-expr are conformable. This does not catch all possible
6582 cases; in particular a runtime checking is needed. */
6585 conformable_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
6588 for (tail
= e2
->ref
; tail
&& tail
->next
; tail
= tail
->next
);
6590 /* First compare rank. */
6591 if (tail
&& e1
->rank
!= tail
->u
.ar
.as
->rank
)
6593 gfc_error ("Source-expr at %L must be scalar or have the "
6594 "same rank as the allocate-object at %L",
6595 &e1
->where
, &e2
->where
);
6606 for (i
= 0; i
< e1
->rank
; i
++)
6608 if (tail
->u
.ar
.end
[i
])
6610 mpz_set (s
, tail
->u
.ar
.end
[i
]->value
.integer
);
6611 mpz_sub (s
, s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6612 mpz_add_ui (s
, s
, 1);
6616 mpz_set (s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6619 if (mpz_cmp (e1
->shape
[i
], s
) != 0)
6621 gfc_error ("Source-expr at %L and allocate-object at %L must "
6622 "have the same shape", &e1
->where
, &e2
->where
);
6635 /* Resolve the expression in an ALLOCATE statement, doing the additional
6636 checks to see whether the expression is OK or not. The expression must
6637 have a trailing array reference that gives the size of the array. */
6640 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
)
6642 int i
, pointer
, allocatable
, dimension
, is_abstract
;
6645 symbol_attribute attr
;
6646 gfc_ref
*ref
, *ref2
;
6649 gfc_symbol
*sym
= NULL
;
6654 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6655 checking of coarrays. */
6656 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6657 if (ref
->next
== NULL
)
6660 if (ref
&& ref
->type
== REF_ARRAY
)
6661 ref
->u
.ar
.in_allocate
= true;
6663 if (gfc_resolve_expr (e
) == FAILURE
)
6666 /* Make sure the expression is allocatable or a pointer. If it is
6667 pointer, the next-to-last reference must be a pointer. */
6671 sym
= e
->symtree
->n
.sym
;
6673 /* Check whether ultimate component is abstract and CLASS. */
6676 if (e
->expr_type
!= EXPR_VARIABLE
)
6679 attr
= gfc_expr_attr (e
);
6680 pointer
= attr
.pointer
;
6681 dimension
= attr
.dimension
;
6682 codimension
= attr
.codimension
;
6686 if (sym
->ts
.type
== BT_CLASS
)
6688 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6689 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6690 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
6691 codimension
= CLASS_DATA (sym
)->attr
.codimension
;
6692 is_abstract
= CLASS_DATA (sym
)->attr
.abstract
;
6696 allocatable
= sym
->attr
.allocatable
;
6697 pointer
= sym
->attr
.pointer
;
6698 dimension
= sym
->attr
.dimension
;
6699 codimension
= sym
->attr
.codimension
;
6704 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
6709 if (ref
->u
.ar
.codimen
> 0)
6712 for (n
= ref
->u
.ar
.dimen
;
6713 n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
6714 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_THIS_IMAGE
)
6721 if (ref
->next
!= NULL
)
6729 gfc_error ("Coindexed allocatable object at %L",
6734 c
= ref
->u
.c
.component
;
6735 if (c
->ts
.type
== BT_CLASS
)
6737 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6738 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6739 dimension
= CLASS_DATA (c
)->attr
.dimension
;
6740 codimension
= CLASS_DATA (c
)->attr
.codimension
;
6741 is_abstract
= CLASS_DATA (c
)->attr
.abstract
;
6745 allocatable
= c
->attr
.allocatable
;
6746 pointer
= c
->attr
.pointer
;
6747 dimension
= c
->attr
.dimension
;
6748 codimension
= c
->attr
.codimension
;
6749 is_abstract
= c
->attr
.abstract
;
6761 if (allocatable
== 0 && pointer
== 0)
6763 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6768 /* Some checks for the SOURCE tag. */
6771 /* Check F03:C631. */
6772 if (!gfc_type_compatible (&e
->ts
, &code
->expr3
->ts
))
6774 gfc_error ("Type of entity at %L is type incompatible with "
6775 "source-expr at %L", &e
->where
, &code
->expr3
->where
);
6779 /* Check F03:C632 and restriction following Note 6.18. */
6780 if (code
->expr3
->rank
> 0
6781 && conformable_arrays (code
->expr3
, e
) == FAILURE
)
6784 /* Check F03:C633. */
6785 if (code
->expr3
->ts
.kind
!= e
->ts
.kind
)
6787 gfc_error ("The allocate-object at %L and the source-expr at %L "
6788 "shall have the same kind type parameter",
6789 &e
->where
, &code
->expr3
->where
);
6794 /* Check F08:C629. */
6795 if (is_abstract
&& code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
6798 gcc_assert (e
->ts
.type
== BT_CLASS
);
6799 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6800 "type-spec or source-expr", sym
->name
, &e
->where
);
6804 /* In the variable definition context checks, gfc_expr_attr is used
6805 on the expression. This is fooled by the array specification
6806 present in e, thus we have to eliminate that one temporarily. */
6807 e2
= remove_last_array_ref (e
);
6809 if (t
== SUCCESS
&& pointer
)
6810 t
= gfc_check_vardef_context (e2
, true, _("ALLOCATE object"));
6812 t
= gfc_check_vardef_context (e2
, false, _("ALLOCATE object"));
6819 /* Set up default initializer if needed. */
6823 if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6824 ts
= code
->ext
.alloc
.ts
;
6828 if (ts
.type
== BT_CLASS
)
6829 ts
= ts
.u
.derived
->components
->ts
;
6831 if (ts
.type
== BT_DERIVED
&& (init_e
= gfc_default_initializer (&ts
)))
6833 gfc_code
*init_st
= gfc_get_code ();
6834 init_st
->loc
= code
->loc
;
6835 init_st
->op
= EXEC_INIT_ASSIGN
;
6836 init_st
->expr1
= gfc_expr_to_initialize (e
);
6837 init_st
->expr2
= init_e
;
6838 init_st
->next
= code
->next
;
6839 code
->next
= init_st
;
6842 else if (code
->expr3
->mold
&& code
->expr3
->ts
.type
== BT_DERIVED
)
6844 /* Default initialization via MOLD (non-polymorphic). */
6845 gfc_expr
*rhs
= gfc_default_initializer (&code
->expr3
->ts
);
6846 gfc_resolve_expr (rhs
);
6847 gfc_free_expr (code
->expr3
);
6851 if (e
->ts
.type
== BT_CLASS
)
6853 /* Make sure the vtab symbol is present when
6854 the module variables are generated. */
6855 gfc_typespec ts
= e
->ts
;
6857 ts
= code
->expr3
->ts
;
6858 else if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6859 ts
= code
->ext
.alloc
.ts
;
6860 gfc_find_derived_vtab (ts
.u
.derived
);
6863 if (pointer
|| (dimension
== 0 && codimension
== 0))
6866 /* Make sure the last reference node is an array specifiction. */
6868 if (!ref2
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
6869 || (dimension
&& ref2
->u
.ar
.dimen
== 0))
6871 gfc_error ("Array specification required in ALLOCATE statement "
6872 "at %L", &e
->where
);
6876 /* Make sure that the array section reference makes sense in the
6877 context of an ALLOCATE specification. */
6882 for (i
= ar
->dimen
; i
< ar
->dimen
+ ar
->codimen
; i
++)
6883 if (ar
->dimen_type
[i
] == DIMEN_THIS_IMAGE
)
6885 gfc_error ("Coarray specification required in ALLOCATE statement "
6886 "at %L", &e
->where
);
6890 for (i
= 0; i
< ar
->dimen
; i
++)
6892 if (ref2
->u
.ar
.type
== AR_ELEMENT
)
6895 switch (ar
->dimen_type
[i
])
6901 if (ar
->start
[i
] != NULL
6902 && ar
->end
[i
] != NULL
6903 && ar
->stride
[i
] == NULL
)
6906 /* Fall Through... */
6911 case DIMEN_THIS_IMAGE
:
6912 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6918 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6920 sym
= a
->expr
->symtree
->n
.sym
;
6922 /* TODO - check derived type components. */
6923 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
6926 if ((ar
->start
[i
] != NULL
6927 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
6928 || (ar
->end
[i
] != NULL
6929 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
6931 gfc_error ("'%s' must not appear in the array specification at "
6932 "%L in the same ALLOCATE statement where it is "
6933 "itself allocated", sym
->name
, &ar
->where
);
6939 for (i
= ar
->dimen
; i
< ar
->codimen
+ ar
->dimen
; i
++)
6941 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
6942 || ar
->dimen_type
[i
] == DIMEN_RANGE
)
6944 if (i
== (ar
->dimen
+ ar
->codimen
- 1))
6946 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6947 "statement at %L", &e
->where
);
6953 if (ar
->dimen_type
[i
] == DIMEN_STAR
&& i
== (ar
->dimen
+ ar
->codimen
- 1)
6954 && ar
->stride
[i
] == NULL
)
6957 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6962 if (codimension
&& ar
->as
->rank
== 0)
6964 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6965 "at %L", &e
->where
);
6977 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
6979 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
6980 gfc_alloc
*a
, *p
, *q
;
6983 errmsg
= code
->expr2
;
6985 /* Check the stat variable. */
6988 gfc_check_vardef_context (stat
, false, _("STAT variable"));
6990 if ((stat
->ts
.type
!= BT_INTEGER
6991 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
6992 || stat
->ref
->type
== REF_COMPONENT
)))
6994 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6995 "variable", &stat
->where
);
6997 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6998 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
7000 gfc_ref
*ref1
, *ref2
;
7003 for (ref1
= p
->expr
->ref
, ref2
= stat
->ref
; ref1
&& ref2
;
7004 ref1
= ref1
->next
, ref2
= ref2
->next
)
7006 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
7008 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
7017 gfc_error ("Stat-variable at %L shall not be %sd within "
7018 "the same %s statement", &stat
->where
, fcn
, fcn
);
7024 /* Check the errmsg variable. */
7028 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7031 gfc_check_vardef_context (errmsg
, false, _("ERRMSG variable"));
7033 if ((errmsg
->ts
.type
!= BT_CHARACTER
7035 && (errmsg
->ref
->type
== REF_ARRAY
7036 || errmsg
->ref
->type
== REF_COMPONENT
)))
7037 || errmsg
->rank
> 0 )
7038 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7039 "variable", &errmsg
->where
);
7041 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
7042 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
7044 gfc_ref
*ref1
, *ref2
;
7047 for (ref1
= p
->expr
->ref
, ref2
= errmsg
->ref
; ref1
&& ref2
;
7048 ref1
= ref1
->next
, ref2
= ref2
->next
)
7050 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
7052 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
7061 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7062 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
7068 /* Check that an allocate-object appears only once in the statement.
7069 FIXME: Checking derived types is disabled. */
7070 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
7073 for (q
= p
->next
; q
; q
= q
->next
)
7076 if (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
)
7078 /* This is a potential collision. */
7079 gfc_ref
*pr
= pe
->ref
;
7080 gfc_ref
*qr
= qe
->ref
;
7082 /* Follow the references until
7083 a) They start to differ, in which case there is no error;
7084 you can deallocate a%b and a%c in a single statement
7085 b) Both of them stop, which is an error
7086 c) One of them stops, which is also an error. */
7089 if (pr
== NULL
&& qr
== NULL
)
7091 gfc_error ("Allocate-object at %L also appears at %L",
7092 &pe
->where
, &qe
->where
);
7095 else if (pr
!= NULL
&& qr
== NULL
)
7097 gfc_error ("Allocate-object at %L is subobject of"
7098 " object at %L", &pe
->where
, &qe
->where
);
7101 else if (pr
== NULL
&& qr
!= NULL
)
7103 gfc_error ("Allocate-object at %L is subobject of"
7104 " object at %L", &qe
->where
, &pe
->where
);
7107 /* Here, pr != NULL && qr != NULL */
7108 gcc_assert(pr
->type
== qr
->type
);
7109 if (pr
->type
== REF_ARRAY
)
7111 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7113 gcc_assert (qr
->type
== REF_ARRAY
);
7115 if (pr
->next
&& qr
->next
)
7117 gfc_array_ref
*par
= &(pr
->u
.ar
);
7118 gfc_array_ref
*qar
= &(qr
->u
.ar
);
7119 if (gfc_dep_compare_expr (par
->start
[0],
7120 qar
->start
[0]) != 0)
7126 if (pr
->u
.c
.component
->name
!= qr
->u
.c
.component
->name
)
7137 if (strcmp (fcn
, "ALLOCATE") == 0)
7139 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
7140 resolve_allocate_expr (a
->expr
, code
);
7144 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
7145 resolve_deallocate_expr (a
->expr
);
7150 /************ SELECT CASE resolution subroutines ************/
7152 /* Callback function for our mergesort variant. Determines interval
7153 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7154 op1 > op2. Assumes we're not dealing with the default case.
7155 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7156 There are nine situations to check. */
7159 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
7163 if (op1
->low
== NULL
) /* op1 = (:L) */
7165 /* op2 = (:N), so overlap. */
7167 /* op2 = (M:) or (M:N), L < M */
7168 if (op2
->low
!= NULL
7169 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7172 else if (op1
->high
== NULL
) /* op1 = (K:) */
7174 /* op2 = (M:), so overlap. */
7176 /* op2 = (:N) or (M:N), K > N */
7177 if (op2
->high
!= NULL
7178 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7181 else /* op1 = (K:L) */
7183 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
7184 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7186 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
7187 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7189 else /* op2 = (M:N) */
7193 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7196 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7205 /* Merge-sort a double linked case list, detecting overlap in the
7206 process. LIST is the head of the double linked case list before it
7207 is sorted. Returns the head of the sorted list if we don't see any
7208 overlap, or NULL otherwise. */
7211 check_case_overlap (gfc_case
*list
)
7213 gfc_case
*p
, *q
, *e
, *tail
;
7214 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
7216 /* If the passed list was empty, return immediately. */
7223 /* Loop unconditionally. The only exit from this loop is a return
7224 statement, when we've finished sorting the case list. */
7231 /* Count the number of merges we do in this pass. */
7234 /* Loop while there exists a merge to be done. */
7239 /* Count this merge. */
7242 /* Cut the list in two pieces by stepping INSIZE places
7243 forward in the list, starting from P. */
7246 for (i
= 0; i
< insize
; i
++)
7255 /* Now we have two lists. Merge them! */
7256 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
7258 /* See from which the next case to merge comes from. */
7261 /* P is empty so the next case must come from Q. */
7266 else if (qsize
== 0 || q
== NULL
)
7275 cmp
= compare_cases (p
, q
);
7278 /* The whole case range for P is less than the
7286 /* The whole case range for Q is greater than
7287 the case range for P. */
7294 /* The cases overlap, or they are the same
7295 element in the list. Either way, we must
7296 issue an error and get the next case from P. */
7297 /* FIXME: Sort P and Q by line number. */
7298 gfc_error ("CASE label at %L overlaps with CASE "
7299 "label at %L", &p
->where
, &q
->where
);
7307 /* Add the next element to the merged list. */
7316 /* P has now stepped INSIZE places along, and so has Q. So
7317 they're the same. */
7322 /* If we have done only one merge or none at all, we've
7323 finished sorting the cases. */
7332 /* Otherwise repeat, merging lists twice the size. */
7338 /* Check to see if an expression is suitable for use in a CASE statement.
7339 Makes sure that all case expressions are scalar constants of the same
7340 type. Return FAILURE if anything is wrong. */
7343 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
7345 if (e
== NULL
) return SUCCESS
;
7347 if (e
->ts
.type
!= case_expr
->ts
.type
)
7349 gfc_error ("Expression in CASE statement at %L must be of type %s",
7350 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
7354 /* C805 (R808) For a given case-construct, each case-value shall be of
7355 the same type as case-expr. For character type, length differences
7356 are allowed, but the kind type parameters shall be the same. */
7358 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
7360 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7361 &e
->where
, case_expr
->ts
.kind
);
7365 /* Convert the case value kind to that of case expression kind,
7368 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
7369 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
7373 gfc_error ("Expression in CASE statement at %L must be scalar",
7382 /* Given a completely parsed select statement, we:
7384 - Validate all expressions and code within the SELECT.
7385 - Make sure that the selection expression is not of the wrong type.
7386 - Make sure that no case ranges overlap.
7387 - Eliminate unreachable cases and unreachable code resulting from
7388 removing case labels.
7390 The standard does allow unreachable cases, e.g. CASE (5:3). But
7391 they are a hassle for code generation, and to prevent that, we just
7392 cut them out here. This is not necessary for overlapping cases
7393 because they are illegal and we never even try to generate code.
7395 We have the additional caveat that a SELECT construct could have
7396 been a computed GOTO in the source code. Fortunately we can fairly
7397 easily work around that here: The case_expr for a "real" SELECT CASE
7398 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7399 we have to do is make sure that the case_expr is a scalar integer
7403 resolve_select (gfc_code
*code
)
7406 gfc_expr
*case_expr
;
7407 gfc_case
*cp
, *default_case
, *tail
, *head
;
7408 int seen_unreachable
;
7414 if (code
->expr1
== NULL
)
7416 /* This was actually a computed GOTO statement. */
7417 case_expr
= code
->expr2
;
7418 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
7419 gfc_error ("Selection expression in computed GOTO statement "
7420 "at %L must be a scalar integer expression",
7423 /* Further checking is not necessary because this SELECT was built
7424 by the compiler, so it should always be OK. Just move the
7425 case_expr from expr2 to expr so that we can handle computed
7426 GOTOs as normal SELECTs from here on. */
7427 code
->expr1
= code
->expr2
;
7432 case_expr
= code
->expr1
;
7434 type
= case_expr
->ts
.type
;
7435 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
7437 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7438 &case_expr
->where
, gfc_typename (&case_expr
->ts
));
7440 /* Punt. Going on here just produce more garbage error messages. */
7444 if (case_expr
->rank
!= 0)
7446 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7447 "expression", &case_expr
->where
);
7454 /* Raise a warning if an INTEGER case value exceeds the range of
7455 the case-expr. Later, all expressions will be promoted to the
7456 largest kind of all case-labels. */
7458 if (type
== BT_INTEGER
)
7459 for (body
= code
->block
; body
; body
= body
->block
)
7460 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7463 && gfc_check_integer_range (cp
->low
->value
.integer
,
7464 case_expr
->ts
.kind
) != ARITH_OK
)
7465 gfc_warning ("Expression in CASE statement at %L is "
7466 "not in the range of %s", &cp
->low
->where
,
7467 gfc_typename (&case_expr
->ts
));
7470 && cp
->low
!= cp
->high
7471 && gfc_check_integer_range (cp
->high
->value
.integer
,
7472 case_expr
->ts
.kind
) != ARITH_OK
)
7473 gfc_warning ("Expression in CASE statement at %L is "
7474 "not in the range of %s", &cp
->high
->where
,
7475 gfc_typename (&case_expr
->ts
));
7478 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7479 of the SELECT CASE expression and its CASE values. Walk the lists
7480 of case values, and if we find a mismatch, promote case_expr to
7481 the appropriate kind. */
7483 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
7485 for (body
= code
->block
; body
; body
= body
->block
)
7487 /* Walk the case label list. */
7488 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7490 /* Intercept the DEFAULT case. It does not have a kind. */
7491 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7494 /* Unreachable case ranges are discarded, so ignore. */
7495 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7496 && cp
->low
!= cp
->high
7497 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7501 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
7502 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
7504 if (cp
->high
!= NULL
7505 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
7506 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
7511 /* Assume there is no DEFAULT case. */
7512 default_case
= NULL
;
7517 for (body
= code
->block
; body
; body
= body
->block
)
7519 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7521 seen_unreachable
= 0;
7523 /* Walk the case label list, making sure that all case labels
7525 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7527 /* Count the number of cases in the whole construct. */
7530 /* Intercept the DEFAULT case. */
7531 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7533 if (default_case
!= NULL
)
7535 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7536 "by a second DEFAULT CASE at %L",
7537 &default_case
->where
, &cp
->where
);
7548 /* Deal with single value cases and case ranges. Errors are
7549 issued from the validation function. */
7550 if (validate_case_label_expr (cp
->low
, case_expr
) != SUCCESS
7551 || validate_case_label_expr (cp
->high
, case_expr
) != SUCCESS
)
7557 if (type
== BT_LOGICAL
7558 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
7559 || cp
->low
!= cp
->high
))
7561 gfc_error ("Logical range in CASE statement at %L is not "
7562 "allowed", &cp
->low
->where
);
7567 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
7570 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
7571 if (value
& seen_logical
)
7573 gfc_error ("Constant logical value in CASE statement "
7574 "is repeated at %L",
7579 seen_logical
|= value
;
7582 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7583 && cp
->low
!= cp
->high
7584 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7586 if (gfc_option
.warn_surprising
)
7587 gfc_warning ("Range specification at %L can never "
7588 "be matched", &cp
->where
);
7590 cp
->unreachable
= 1;
7591 seen_unreachable
= 1;
7595 /* If the case range can be matched, it can also overlap with
7596 other cases. To make sure it does not, we put it in a
7597 double linked list here. We sort that with a merge sort
7598 later on to detect any overlapping cases. */
7602 head
->right
= head
->left
= NULL
;
7607 tail
->right
->left
= tail
;
7614 /* It there was a failure in the previous case label, give up
7615 for this case label list. Continue with the next block. */
7619 /* See if any case labels that are unreachable have been seen.
7620 If so, we eliminate them. This is a bit of a kludge because
7621 the case lists for a single case statement (label) is a
7622 single forward linked lists. */
7623 if (seen_unreachable
)
7625 /* Advance until the first case in the list is reachable. */
7626 while (body
->ext
.block
.case_list
!= NULL
7627 && body
->ext
.block
.case_list
->unreachable
)
7629 gfc_case
*n
= body
->ext
.block
.case_list
;
7630 body
->ext
.block
.case_list
= body
->ext
.block
.case_list
->next
;
7632 gfc_free_case_list (n
);
7635 /* Strip all other unreachable cases. */
7636 if (body
->ext
.block
.case_list
)
7638 for (cp
= body
->ext
.block
.case_list
; cp
->next
; cp
= cp
->next
)
7640 if (cp
->next
->unreachable
)
7642 gfc_case
*n
= cp
->next
;
7643 cp
->next
= cp
->next
->next
;
7645 gfc_free_case_list (n
);
7652 /* See if there were overlapping cases. If the check returns NULL,
7653 there was overlap. In that case we don't do anything. If head
7654 is non-NULL, we prepend the DEFAULT case. The sorted list can
7655 then used during code generation for SELECT CASE constructs with
7656 a case expression of a CHARACTER type. */
7659 head
= check_case_overlap (head
);
7661 /* Prepend the default_case if it is there. */
7662 if (head
!= NULL
&& default_case
)
7664 default_case
->left
= NULL
;
7665 default_case
->right
= head
;
7666 head
->left
= default_case
;
7670 /* Eliminate dead blocks that may be the result if we've seen
7671 unreachable case labels for a block. */
7672 for (body
= code
; body
&& body
->block
; body
= body
->block
)
7674 if (body
->block
->ext
.block
.case_list
== NULL
)
7676 /* Cut the unreachable block from the code chain. */
7677 gfc_code
*c
= body
->block
;
7678 body
->block
= c
->block
;
7680 /* Kill the dead block, but not the blocks below it. */
7682 gfc_free_statements (c
);
7686 /* More than two cases is legal but insane for logical selects.
7687 Issue a warning for it. */
7688 if (gfc_option
.warn_surprising
&& type
== BT_LOGICAL
7690 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7695 /* Check if a derived type is extensible. */
7698 gfc_type_is_extensible (gfc_symbol
*sym
)
7700 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
);
7704 /* Resolve an associate name: Resolve target and ensure the type-spec is
7705 correct as well as possibly the array-spec. */
7708 resolve_assoc_var (gfc_symbol
* sym
, bool resolve_target
)
7712 gcc_assert (sym
->assoc
);
7713 gcc_assert (sym
->attr
.flavor
== FL_VARIABLE
);
7715 /* If this is for SELECT TYPE, the target may not yet be set. In that
7716 case, return. Resolution will be called later manually again when
7718 target
= sym
->assoc
->target
;
7721 gcc_assert (!sym
->assoc
->dangling
);
7723 if (resolve_target
&& gfc_resolve_expr (target
) != SUCCESS
)
7726 /* For variable targets, we get some attributes from the target. */
7727 if (target
->expr_type
== EXPR_VARIABLE
)
7731 gcc_assert (target
->symtree
);
7732 tsym
= target
->symtree
->n
.sym
;
7734 sym
->attr
.asynchronous
= tsym
->attr
.asynchronous
;
7735 sym
->attr
.volatile_
= tsym
->attr
.volatile_
;
7737 sym
->attr
.target
= (tsym
->attr
.target
|| tsym
->attr
.pointer
);
7740 /* Get type if this was not already set. Note that it can be
7741 some other type than the target in case this is a SELECT TYPE
7742 selector! So we must not update when the type is already there. */
7743 if (sym
->ts
.type
== BT_UNKNOWN
)
7744 sym
->ts
= target
->ts
;
7745 gcc_assert (sym
->ts
.type
!= BT_UNKNOWN
);
7747 /* See if this is a valid association-to-variable. */
7748 sym
->assoc
->variable
= (target
->expr_type
== EXPR_VARIABLE
7749 && !gfc_has_vector_subscript (target
));
7751 /* Finally resolve if this is an array or not. */
7752 if (sym
->attr
.dimension
&& target
->rank
== 0)
7754 gfc_error ("Associate-name '%s' at %L is used as array",
7755 sym
->name
, &sym
->declared_at
);
7756 sym
->attr
.dimension
= 0;
7759 if (target
->rank
> 0)
7760 sym
->attr
.dimension
= 1;
7762 if (sym
->attr
.dimension
)
7764 sym
->as
= gfc_get_array_spec ();
7765 sym
->as
->rank
= target
->rank
;
7766 sym
->as
->type
= AS_DEFERRED
;
7768 /* Target must not be coindexed, thus the associate-variable
7770 sym
->as
->corank
= 0;
7775 /* Resolve a SELECT TYPE statement. */
7778 resolve_select_type (gfc_code
*code
, gfc_namespace
*old_ns
)
7780 gfc_symbol
*selector_type
;
7781 gfc_code
*body
, *new_st
, *if_st
, *tail
;
7782 gfc_code
*class_is
= NULL
, *default_case
= NULL
;
7785 char name
[GFC_MAX_SYMBOL_LEN
];
7789 ns
= code
->ext
.block
.ns
;
7792 /* Check for F03:C813. */
7793 if (code
->expr1
->ts
.type
!= BT_CLASS
7794 && !(code
->expr2
&& code
->expr2
->ts
.type
== BT_CLASS
))
7796 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7797 "at %L", &code
->loc
);
7803 if (code
->expr1
->symtree
->n
.sym
->attr
.untyped
)
7804 code
->expr1
->symtree
->n
.sym
->ts
= code
->expr2
->ts
;
7805 selector_type
= CLASS_DATA (code
->expr2
)->ts
.u
.derived
;
7808 selector_type
= CLASS_DATA (code
->expr1
)->ts
.u
.derived
;
7810 /* Loop over TYPE IS / CLASS IS cases. */
7811 for (body
= code
->block
; body
; body
= body
->block
)
7813 c
= body
->ext
.block
.case_list
;
7815 /* Check F03:C815. */
7816 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7817 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
7819 gfc_error ("Derived type '%s' at %L must be extensible",
7820 c
->ts
.u
.derived
->name
, &c
->where
);
7825 /* Check F03:C816. */
7826 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7827 && !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
))
7829 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7830 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
7835 /* Intercept the DEFAULT case. */
7836 if (c
->ts
.type
== BT_UNKNOWN
)
7838 /* Check F03:C818. */
7841 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7842 "by a second DEFAULT CASE at %L",
7843 &default_case
->ext
.block
.case_list
->where
, &c
->where
);
7848 default_case
= body
;
7855 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7856 target if present. If there are any EXIT statements referring to the
7857 SELECT TYPE construct, this is no problem because the gfc_code
7858 reference stays the same and EXIT is equally possible from the BLOCK
7859 it is changed to. */
7860 code
->op
= EXEC_BLOCK
;
7863 gfc_association_list
* assoc
;
7865 assoc
= gfc_get_association_list ();
7866 assoc
->st
= code
->expr1
->symtree
;
7867 assoc
->target
= gfc_copy_expr (code
->expr2
);
7868 /* assoc->variable will be set by resolve_assoc_var. */
7870 code
->ext
.block
.assoc
= assoc
;
7871 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
7873 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
7876 code
->ext
.block
.assoc
= NULL
;
7878 /* Add EXEC_SELECT to switch on type. */
7879 new_st
= gfc_get_code ();
7880 new_st
->op
= code
->op
;
7881 new_st
->expr1
= code
->expr1
;
7882 new_st
->expr2
= code
->expr2
;
7883 new_st
->block
= code
->block
;
7884 code
->expr1
= code
->expr2
= NULL
;
7889 ns
->code
->next
= new_st
;
7891 code
->op
= EXEC_SELECT
;
7892 gfc_add_vptr_component (code
->expr1
);
7893 gfc_add_hash_component (code
->expr1
);
7895 /* Loop over TYPE IS / CLASS IS cases. */
7896 for (body
= code
->block
; body
; body
= body
->block
)
7898 c
= body
->ext
.block
.case_list
;
7900 if (c
->ts
.type
== BT_DERIVED
)
7901 c
->low
= c
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
7902 c
->ts
.u
.derived
->hash_value
);
7904 else if (c
->ts
.type
== BT_UNKNOWN
)
7907 /* Associate temporary to selector. This should only be done
7908 when this case is actually true, so build a new ASSOCIATE
7909 that does precisely this here (instead of using the
7912 if (c
->ts
.type
== BT_CLASS
)
7913 sprintf (name
, "__tmp_class_%s", c
->ts
.u
.derived
->name
);
7915 sprintf (name
, "__tmp_type_%s", c
->ts
.u
.derived
->name
);
7916 st
= gfc_find_symtree (ns
->sym_root
, name
);
7917 gcc_assert (st
->n
.sym
->assoc
);
7918 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (code
->expr1
->symtree
);
7919 if (c
->ts
.type
== BT_DERIVED
)
7920 gfc_add_data_component (st
->n
.sym
->assoc
->target
);
7922 new_st
= gfc_get_code ();
7923 new_st
->op
= EXEC_BLOCK
;
7924 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
7925 new_st
->ext
.block
.ns
->code
= body
->next
;
7926 body
->next
= new_st
;
7928 /* Chain in the new list only if it is marked as dangling. Otherwise
7929 there is a CASE label overlap and this is already used. Just ignore,
7930 the error is diagonsed elsewhere. */
7931 if (st
->n
.sym
->assoc
->dangling
)
7933 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
7934 st
->n
.sym
->assoc
->dangling
= 0;
7937 resolve_assoc_var (st
->n
.sym
, false);
7940 /* Take out CLASS IS cases for separate treatment. */
7942 while (body
&& body
->block
)
7944 if (body
->block
->ext
.block
.case_list
->ts
.type
== BT_CLASS
)
7946 /* Add to class_is list. */
7947 if (class_is
== NULL
)
7949 class_is
= body
->block
;
7954 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
7955 tail
->block
= body
->block
;
7958 /* Remove from EXEC_SELECT list. */
7959 body
->block
= body
->block
->block
;
7972 /* Add a default case to hold the CLASS IS cases. */
7973 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
7974 tail
->block
= gfc_get_code ();
7976 tail
->op
= EXEC_SELECT_TYPE
;
7977 tail
->ext
.block
.case_list
= gfc_get_case ();
7978 tail
->ext
.block
.case_list
->ts
.type
= BT_UNKNOWN
;
7980 default_case
= tail
;
7983 /* More than one CLASS IS block? */
7984 if (class_is
->block
)
7988 /* Sort CLASS IS blocks by extension level. */
7992 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
7995 /* F03:C817 (check for doubles). */
7996 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->hash_value
7997 == c2
->ext
.block
.case_list
->ts
.u
.derived
->hash_value
)
7999 gfc_error ("Double CLASS IS block in SELECT TYPE "
8001 &c2
->ext
.block
.case_list
->where
);
8004 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
8005 < c2
->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
)
8008 (*c1
)->block
= c2
->block
;
8018 /* Generate IF chain. */
8019 if_st
= gfc_get_code ();
8020 if_st
->op
= EXEC_IF
;
8022 for (body
= class_is
; body
; body
= body
->block
)
8024 new_st
->block
= gfc_get_code ();
8025 new_st
= new_st
->block
;
8026 new_st
->op
= EXEC_IF
;
8027 /* Set up IF condition: Call _gfortran_is_extension_of. */
8028 new_st
->expr1
= gfc_get_expr ();
8029 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
8030 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
8031 new_st
->expr1
->ts
.kind
= 4;
8032 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
8033 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
8034 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
8035 /* Set up arguments. */
8036 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
8037 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (code
->expr1
->symtree
);
8038 new_st
->expr1
->value
.function
.actual
->expr
->where
= code
->loc
;
8039 gfc_add_vptr_component (new_st
->expr1
->value
.function
.actual
->expr
);
8040 vtab
= gfc_find_derived_vtab (body
->ext
.block
.case_list
->ts
.u
.derived
);
8041 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
8042 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
8043 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
8044 new_st
->next
= body
->next
;
8046 if (default_case
->next
)
8048 new_st
->block
= gfc_get_code ();
8049 new_st
= new_st
->block
;
8050 new_st
->op
= EXEC_IF
;
8051 new_st
->next
= default_case
->next
;
8054 /* Replace CLASS DEFAULT code by the IF chain. */
8055 default_case
->next
= if_st
;
8058 /* Resolve the internal code. This can not be done earlier because
8059 it requires that the sym->assoc of selectors is set already. */
8060 gfc_current_ns
= ns
;
8061 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
8062 gfc_current_ns
= old_ns
;
8064 resolve_select (code
);
8068 /* Resolve a transfer statement. This is making sure that:
8069 -- a derived type being transferred has only non-pointer components
8070 -- a derived type being transferred doesn't have private components, unless
8071 it's being transferred from the module where the type was defined
8072 -- we're not trying to transfer a whole assumed size array. */
8075 resolve_transfer (gfc_code
*code
)
8084 while (exp
!= NULL
&& exp
->expr_type
== EXPR_OP
8085 && exp
->value
.op
.op
== INTRINSIC_PARENTHESES
)
8086 exp
= exp
->value
.op
.op1
;
8088 if (exp
== NULL
|| (exp
->expr_type
!= EXPR_VARIABLE
8089 && exp
->expr_type
!= EXPR_FUNCTION
))
8092 /* If we are reading, the variable will be changed. Note that
8093 code->ext.dt may be NULL if the TRANSFER is related to
8094 an INQUIRE statement -- but in this case, we are not reading, either. */
8095 if (code
->ext
.dt
&& code
->ext
.dt
->dt_io_kind
->value
.iokind
== M_READ
8096 && gfc_check_vardef_context (exp
, false, _("item in READ")) == FAILURE
)
8099 sym
= exp
->symtree
->n
.sym
;
8102 /* Go to actual component transferred. */
8103 for (ref
= exp
->ref
; ref
; ref
= ref
->next
)
8104 if (ref
->type
== REF_COMPONENT
)
8105 ts
= &ref
->u
.c
.component
->ts
;
8107 if (ts
->type
== BT_CLASS
)
8109 /* FIXME: Test for defined input/output. */
8110 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8111 "it is processed by a defined input/output procedure",
8116 if (ts
->type
== BT_DERIVED
)
8118 /* Check that transferred derived type doesn't contain POINTER
8120 if (ts
->u
.derived
->attr
.pointer_comp
)
8122 gfc_error ("Data transfer element at %L cannot have "
8123 "POINTER components", &code
->loc
);
8128 if (ts
->u
.derived
->attr
.proc_pointer_comp
)
8130 gfc_error ("Data transfer element at %L cannot have "
8131 "procedure pointer components", &code
->loc
);
8135 if (ts
->u
.derived
->attr
.alloc_comp
)
8137 gfc_error ("Data transfer element at %L cannot have "
8138 "ALLOCATABLE components", &code
->loc
);
8142 if (derived_inaccessible (ts
->u
.derived
))
8144 gfc_error ("Data transfer element at %L cannot have "
8145 "PRIVATE components",&code
->loc
);
8150 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
8151 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
8153 gfc_error ("Data transfer element at %L cannot be a full reference to "
8154 "an assumed-size array", &code
->loc
);
8160 /*********** Toplevel code resolution subroutines ***********/
8162 /* Find the set of labels that are reachable from this block. We also
8163 record the last statement in each block. */
8166 find_reachable_labels (gfc_code
*block
)
8173 cs_base
->reachable_labels
= bitmap_obstack_alloc (&labels_obstack
);
8175 /* Collect labels in this block. We don't keep those corresponding
8176 to END {IF|SELECT}, these are checked in resolve_branch by going
8177 up through the code_stack. */
8178 for (c
= block
; c
; c
= c
->next
)
8180 if (c
->here
&& c
->op
!= EXEC_END_BLOCK
)
8181 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
8184 /* Merge with labels from parent block. */
8187 gcc_assert (cs_base
->prev
->reachable_labels
);
8188 bitmap_ior_into (cs_base
->reachable_labels
,
8189 cs_base
->prev
->reachable_labels
);
8195 resolve_sync (gfc_code
*code
)
8197 /* Check imageset. The * case matches expr1 == NULL. */
8200 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
8201 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8202 "INTEGER expression", &code
->expr1
->where
);
8203 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
8204 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
8205 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8206 &code
->expr1
->where
);
8207 else if (code
->expr1
->expr_type
== EXPR_ARRAY
8208 && gfc_simplify_expr (code
->expr1
, 0) == SUCCESS
)
8210 gfc_constructor
*cons
;
8211 cons
= gfc_constructor_first (code
->expr1
->value
.constructor
);
8212 for (; cons
; cons
= gfc_constructor_next (cons
))
8213 if (cons
->expr
->expr_type
== EXPR_CONSTANT
8214 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
8215 gfc_error ("Imageset argument at %L must between 1 and "
8216 "num_images()", &cons
->expr
->where
);
8222 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
8223 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
8224 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8225 &code
->expr2
->where
);
8229 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
8230 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
8231 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8232 &code
->expr3
->where
);
8236 /* Given a branch to a label, see if the branch is conforming.
8237 The code node describes where the branch is located. */
8240 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
8247 /* Step one: is this a valid branching target? */
8249 if (label
->defined
== ST_LABEL_UNKNOWN
)
8251 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
8256 if (label
->defined
!= ST_LABEL_TARGET
)
8258 gfc_error ("Statement at %L is not a valid branch target statement "
8259 "for the branch statement at %L", &label
->where
, &code
->loc
);
8263 /* Step two: make sure this branch is not a branch to itself ;-) */
8265 if (code
->here
== label
)
8267 gfc_warning ("Branch at %L may result in an infinite loop", &code
->loc
);
8271 /* Step three: See if the label is in the same block as the
8272 branching statement. The hard work has been done by setting up
8273 the bitmap reachable_labels. */
8275 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
8277 /* Check now whether there is a CRITICAL construct; if so, check
8278 whether the label is still visible outside of the CRITICAL block,
8279 which is invalid. */
8280 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8281 if (stack
->current
->op
== EXEC_CRITICAL
8282 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
8283 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8284 " at %L", &code
->loc
, &label
->where
);
8289 /* Step four: If we haven't found the label in the bitmap, it may
8290 still be the label of the END of the enclosing block, in which
8291 case we find it by going up the code_stack. */
8293 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8295 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
8297 if (stack
->current
->op
== EXEC_CRITICAL
)
8299 /* Note: A label at END CRITICAL does not leave the CRITICAL
8300 construct as END CRITICAL is still part of it. */
8301 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8302 " at %L", &code
->loc
, &label
->where
);
8309 gcc_assert (stack
->current
->next
->op
== EXEC_END_BLOCK
);
8313 /* The label is not in an enclosing block, so illegal. This was
8314 allowed in Fortran 66, so we allow it as extension. No
8315 further checks are necessary in this case. */
8316 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
8317 "as the GOTO statement at %L", &label
->where
,
8323 /* Check whether EXPR1 has the same shape as EXPR2. */
8326 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
8328 mpz_t shape
[GFC_MAX_DIMENSIONS
];
8329 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
8330 gfc_try result
= FAILURE
;
8333 /* Compare the rank. */
8334 if (expr1
->rank
!= expr2
->rank
)
8337 /* Compare the size of each dimension. */
8338 for (i
=0; i
<expr1
->rank
; i
++)
8340 if (gfc_array_dimen_size (expr1
, i
, &shape
[i
]) == FAILURE
)
8343 if (gfc_array_dimen_size (expr2
, i
, &shape2
[i
]) == FAILURE
)
8346 if (mpz_cmp (shape
[i
], shape2
[i
]))
8350 /* When either of the two expression is an assumed size array, we
8351 ignore the comparison of dimension sizes. */
8356 for (i
--; i
>= 0; i
--)
8358 mpz_clear (shape
[i
]);
8359 mpz_clear (shape2
[i
]);
8365 /* Check whether a WHERE assignment target or a WHERE mask expression
8366 has the same shape as the outmost WHERE mask expression. */
8369 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
8375 cblock
= code
->block
;
8377 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8378 In case of nested WHERE, only the outmost one is stored. */
8379 if (mask
== NULL
) /* outmost WHERE */
8381 else /* inner WHERE */
8388 /* Check if the mask-expr has a consistent shape with the
8389 outmost WHERE mask-expr. */
8390 if (resolve_where_shape (cblock
->expr1
, e
) == FAILURE
)
8391 gfc_error ("WHERE mask at %L has inconsistent shape",
8392 &cblock
->expr1
->where
);
8395 /* the assignment statement of a WHERE statement, or the first
8396 statement in where-body-construct of a WHERE construct */
8397 cnext
= cblock
->next
;
8402 /* WHERE assignment statement */
8405 /* Check shape consistent for WHERE assignment target. */
8406 if (e
&& resolve_where_shape (cnext
->expr1
, e
) == FAILURE
)
8407 gfc_error ("WHERE assignment target at %L has "
8408 "inconsistent shape", &cnext
->expr1
->where
);
8412 case EXEC_ASSIGN_CALL
:
8413 resolve_call (cnext
);
8414 if (!cnext
->resolved_sym
->attr
.elemental
)
8415 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8416 &cnext
->ext
.actual
->expr
->where
);
8419 /* WHERE or WHERE construct is part of a where-body-construct */
8421 resolve_where (cnext
, e
);
8425 gfc_error ("Unsupported statement inside WHERE at %L",
8428 /* the next statement within the same where-body-construct */
8429 cnext
= cnext
->next
;
8431 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8432 cblock
= cblock
->block
;
8437 /* Resolve assignment in FORALL construct.
8438 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8439 FORALL index variables. */
8442 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8446 for (n
= 0; n
< nvar
; n
++)
8448 gfc_symbol
*forall_index
;
8450 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
8452 /* Check whether the assignment target is one of the FORALL index
8454 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
8455 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
8456 gfc_error ("Assignment to a FORALL index variable at %L",
8457 &code
->expr1
->where
);
8460 /* If one of the FORALL index variables doesn't appear in the
8461 assignment variable, then there could be a many-to-one
8462 assignment. Emit a warning rather than an error because the
8463 mask could be resolving this problem. */
8464 if (find_forall_index (code
->expr1
, forall_index
, 0) == FAILURE
)
8465 gfc_warning ("The FORALL with index '%s' is not used on the "
8466 "left side of the assignment at %L and so might "
8467 "cause multiple assignment to this object",
8468 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
8474 /* Resolve WHERE statement in FORALL construct. */
8477 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
8478 gfc_expr
**var_expr
)
8483 cblock
= code
->block
;
8486 /* the assignment statement of a WHERE statement, or the first
8487 statement in where-body-construct of a WHERE construct */
8488 cnext
= cblock
->next
;
8493 /* WHERE assignment statement */
8495 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
8498 /* WHERE operator assignment statement */
8499 case EXEC_ASSIGN_CALL
:
8500 resolve_call (cnext
);
8501 if (!cnext
->resolved_sym
->attr
.elemental
)
8502 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8503 &cnext
->ext
.actual
->expr
->where
);
8506 /* WHERE or WHERE construct is part of a where-body-construct */
8508 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
8512 gfc_error ("Unsupported statement inside WHERE at %L",
8515 /* the next statement within the same where-body-construct */
8516 cnext
= cnext
->next
;
8518 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8519 cblock
= cblock
->block
;
8524 /* Traverse the FORALL body to check whether the following errors exist:
8525 1. For assignment, check if a many-to-one assignment happens.
8526 2. For WHERE statement, check the WHERE body to see if there is any
8527 many-to-one assignment. */
8530 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8534 c
= code
->block
->next
;
8540 case EXEC_POINTER_ASSIGN
:
8541 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
8544 case EXEC_ASSIGN_CALL
:
8548 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8549 there is no need to handle it here. */
8553 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
8558 /* The next statement in the FORALL body. */
8564 /* Counts the number of iterators needed inside a forall construct, including
8565 nested forall constructs. This is used to allocate the needed memory
8566 in gfc_resolve_forall. */
8569 gfc_count_forall_iterators (gfc_code
*code
)
8571 int max_iters
, sub_iters
, current_iters
;
8572 gfc_forall_iterator
*fa
;
8574 gcc_assert(code
->op
== EXEC_FORALL
);
8578 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8581 code
= code
->block
->next
;
8585 if (code
->op
== EXEC_FORALL
)
8587 sub_iters
= gfc_count_forall_iterators (code
);
8588 if (sub_iters
> max_iters
)
8589 max_iters
= sub_iters
;
8594 return current_iters
+ max_iters
;
8598 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8599 gfc_resolve_forall_body to resolve the FORALL body. */
8602 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
8604 static gfc_expr
**var_expr
;
8605 static int total_var
= 0;
8606 static int nvar
= 0;
8608 gfc_forall_iterator
*fa
;
8613 /* Start to resolve a FORALL construct */
8614 if (forall_save
== 0)
8616 /* Count the total number of FORALL index in the nested FORALL
8617 construct in order to allocate the VAR_EXPR with proper size. */
8618 total_var
= gfc_count_forall_iterators (code
);
8620 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8621 var_expr
= XCNEWVEC (gfc_expr
*, total_var
);
8624 /* The information about FORALL iterator, including FORALL index start, end
8625 and stride. The FORALL index can not appear in start, end or stride. */
8626 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8628 /* Check if any outer FORALL index name is the same as the current
8630 for (i
= 0; i
< nvar
; i
++)
8632 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
8634 gfc_error ("An outer FORALL construct already has an index "
8635 "with this name %L", &fa
->var
->where
);
8639 /* Record the current FORALL index. */
8640 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
8644 /* No memory leak. */
8645 gcc_assert (nvar
<= total_var
);
8648 /* Resolve the FORALL body. */
8649 gfc_resolve_forall_body (code
, nvar
, var_expr
);
8651 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8652 gfc_resolve_blocks (code
->block
, ns
);
8656 /* Free only the VAR_EXPRs allocated in this frame. */
8657 for (i
= nvar
; i
< tmp
; i
++)
8658 gfc_free_expr (var_expr
[i
]);
8662 /* We are in the outermost FORALL construct. */
8663 gcc_assert (forall_save
== 0);
8665 /* VAR_EXPR is not needed any more. */
8672 /* Resolve a BLOCK construct statement. */
8675 resolve_block_construct (gfc_code
* code
)
8677 /* Resolve the BLOCK's namespace. */
8678 gfc_resolve (code
->ext
.block
.ns
);
8680 /* For an ASSOCIATE block, the associations (and their targets) are already
8681 resolved during resolve_symbol. */
8685 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8688 static void resolve_code (gfc_code
*, gfc_namespace
*);
8691 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
8695 for (; b
; b
= b
->block
)
8697 t
= gfc_resolve_expr (b
->expr1
);
8698 if (gfc_resolve_expr (b
->expr2
) == FAILURE
)
8704 if (t
== SUCCESS
&& b
->expr1
!= NULL
8705 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
8706 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8713 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
8714 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8719 resolve_branch (b
->label1
, b
);
8723 resolve_block_construct (b
);
8727 case EXEC_SELECT_TYPE
:
8738 case EXEC_OMP_ATOMIC
:
8739 case EXEC_OMP_CRITICAL
:
8741 case EXEC_OMP_MASTER
:
8742 case EXEC_OMP_ORDERED
:
8743 case EXEC_OMP_PARALLEL
:
8744 case EXEC_OMP_PARALLEL_DO
:
8745 case EXEC_OMP_PARALLEL_SECTIONS
:
8746 case EXEC_OMP_PARALLEL_WORKSHARE
:
8747 case EXEC_OMP_SECTIONS
:
8748 case EXEC_OMP_SINGLE
:
8750 case EXEC_OMP_TASKWAIT
:
8751 case EXEC_OMP_WORKSHARE
:
8755 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8758 resolve_code (b
->next
, ns
);
8763 /* Does everything to resolve an ordinary assignment. Returns true
8764 if this is an interface assignment. */
8766 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
8776 if (gfc_extend_assign (code
, ns
) == SUCCESS
)
8780 if (code
->op
== EXEC_ASSIGN_CALL
)
8782 lhs
= code
->ext
.actual
->expr
;
8783 rhsptr
= &code
->ext
.actual
->next
->expr
;
8787 gfc_actual_arglist
* args
;
8788 gfc_typebound_proc
* tbp
;
8790 gcc_assert (code
->op
== EXEC_COMPCALL
);
8792 args
= code
->expr1
->value
.compcall
.actual
;
8794 rhsptr
= &args
->next
->expr
;
8796 tbp
= code
->expr1
->value
.compcall
.tbp
;
8797 gcc_assert (!tbp
->is_generic
);
8800 /* Make a temporary rhs when there is a default initializer
8801 and rhs is the same symbol as the lhs. */
8802 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
8803 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
8804 && gfc_has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
8805 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
8806 *rhsptr
= gfc_get_parentheses (*rhsptr
);
8815 && gfc_notify_std (GFC_STD_GNU
, "Extension: BOZ literal at %L outside "
8816 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8817 &code
->loc
) == FAILURE
)
8820 /* Handle the case of a BOZ literal on the RHS. */
8821 if (rhs
->is_boz
&& lhs
->ts
.type
!= BT_INTEGER
)
8824 if (gfc_option
.warn_surprising
)
8825 gfc_warning ("BOZ literal at %L is bitwise transferred "
8826 "non-integer symbol '%s'", &code
->loc
,
8827 lhs
->symtree
->n
.sym
->name
);
8829 if (!gfc_convert_boz (rhs
, &lhs
->ts
))
8831 if ((rc
= gfc_range_check (rhs
)) != ARITH_OK
)
8833 if (rc
== ARITH_UNDERFLOW
)
8834 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8835 ". This check can be disabled with the option "
8836 "-fno-range-check", &rhs
->where
);
8837 else if (rc
== ARITH_OVERFLOW
)
8838 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8839 ". This check can be disabled with the option "
8840 "-fno-range-check", &rhs
->where
);
8841 else if (rc
== ARITH_NAN
)
8842 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8843 ". This check can be disabled with the option "
8844 "-fno-range-check", &rhs
->where
);
8849 if (lhs
->ts
.type
== BT_CHARACTER
8850 && gfc_option
.warn_character_truncation
)
8852 if (lhs
->ts
.u
.cl
!= NULL
8853 && lhs
->ts
.u
.cl
->length
!= NULL
8854 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8855 llen
= mpz_get_si (lhs
->ts
.u
.cl
->length
->value
.integer
);
8857 if (rhs
->expr_type
== EXPR_CONSTANT
)
8858 rlen
= rhs
->value
.character
.length
;
8860 else if (rhs
->ts
.u
.cl
!= NULL
8861 && rhs
->ts
.u
.cl
->length
!= NULL
8862 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8863 rlen
= mpz_get_si (rhs
->ts
.u
.cl
->length
->value
.integer
);
8865 if (rlen
&& llen
&& rlen
> llen
)
8866 gfc_warning_now ("CHARACTER expression will be truncated "
8867 "in assignment (%d/%d) at %L",
8868 llen
, rlen
, &code
->loc
);
8871 /* Ensure that a vector index expression for the lvalue is evaluated
8872 to a temporary if the lvalue symbol is referenced in it. */
8875 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
8876 if (ref
->type
== REF_ARRAY
)
8878 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
8879 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
8880 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
8881 ref
->u
.ar
.start
[n
]))
8883 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
8887 if (gfc_pure (NULL
))
8889 if (lhs
->ts
.type
== BT_DERIVED
8890 && lhs
->expr_type
== EXPR_VARIABLE
8891 && lhs
->ts
.u
.derived
->attr
.pointer_comp
8892 && rhs
->expr_type
== EXPR_VARIABLE
8893 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
8894 || gfc_is_coindexed (rhs
)))
8897 if (gfc_is_coindexed (rhs
))
8898 gfc_error ("Coindexed expression at %L is assigned to "
8899 "a derived type variable with a POINTER "
8900 "component in a PURE procedure",
8903 gfc_error ("The impure variable at %L is assigned to "
8904 "a derived type variable with a POINTER "
8905 "component in a PURE procedure (12.6)",
8910 /* Fortran 2008, C1283. */
8911 if (gfc_is_coindexed (lhs
))
8913 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8914 "procedure", &rhs
->where
);
8919 if (gfc_implicit_pure (NULL
))
8921 if (lhs
->expr_type
== EXPR_VARIABLE
8922 && lhs
->symtree
->n
.sym
!= gfc_current_ns
->proc_name
8923 && lhs
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
8924 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
8926 if (lhs
->ts
.type
== BT_DERIVED
8927 && lhs
->expr_type
== EXPR_VARIABLE
8928 && lhs
->ts
.u
.derived
->attr
.pointer_comp
8929 && rhs
->expr_type
== EXPR_VARIABLE
8930 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
8931 || gfc_is_coindexed (rhs
)))
8932 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
8934 /* Fortran 2008, C1283. */
8935 if (gfc_is_coindexed (lhs
))
8936 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
8940 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8941 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8942 if (lhs
->ts
.type
== BT_CLASS
)
8944 gfc_error ("Variable must not be polymorphic in assignment at %L",
8949 /* F2008, Section 7.2.1.2. */
8950 if (gfc_is_coindexed (lhs
) && gfc_has_ultimate_allocatable (lhs
))
8952 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8953 "component in assignment at %L", &lhs
->where
);
8957 gfc_check_assign (lhs
, rhs
, 1);
8962 /* Given a block of code, recursively resolve everything pointed to by this
8966 resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
8968 int omp_workshare_save
;
8973 frame
.prev
= cs_base
;
8977 find_reachable_labels (code
);
8979 for (; code
; code
= code
->next
)
8981 frame
.current
= code
;
8982 forall_save
= forall_flag
;
8984 if (code
->op
== EXEC_FORALL
)
8987 gfc_resolve_forall (code
, ns
, forall_save
);
8990 else if (code
->block
)
8992 omp_workshare_save
= -1;
8995 case EXEC_OMP_PARALLEL_WORKSHARE
:
8996 omp_workshare_save
= omp_workshare_flag
;
8997 omp_workshare_flag
= 1;
8998 gfc_resolve_omp_parallel_blocks (code
, ns
);
9000 case EXEC_OMP_PARALLEL
:
9001 case EXEC_OMP_PARALLEL_DO
:
9002 case EXEC_OMP_PARALLEL_SECTIONS
:
9004 omp_workshare_save
= omp_workshare_flag
;
9005 omp_workshare_flag
= 0;
9006 gfc_resolve_omp_parallel_blocks (code
, ns
);
9009 gfc_resolve_omp_do_blocks (code
, ns
);
9011 case EXEC_SELECT_TYPE
:
9012 /* Blocks are handled in resolve_select_type because we have
9013 to transform the SELECT TYPE into ASSOCIATE first. */
9015 case EXEC_OMP_WORKSHARE
:
9016 omp_workshare_save
= omp_workshare_flag
;
9017 omp_workshare_flag
= 1;
9020 gfc_resolve_blocks (code
->block
, ns
);
9024 if (omp_workshare_save
!= -1)
9025 omp_workshare_flag
= omp_workshare_save
;
9029 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
9030 t
= gfc_resolve_expr (code
->expr1
);
9031 forall_flag
= forall_save
;
9033 if (gfc_resolve_expr (code
->expr2
) == FAILURE
)
9036 if (code
->op
== EXEC_ALLOCATE
9037 && gfc_resolve_expr (code
->expr3
) == FAILURE
)
9043 case EXEC_END_BLOCK
:
9047 case EXEC_ERROR_STOP
:
9051 case EXEC_ASSIGN_CALL
:
9056 case EXEC_SYNC_IMAGES
:
9057 case EXEC_SYNC_MEMORY
:
9058 resolve_sync (code
);
9062 /* Keep track of which entry we are up to. */
9063 current_entry_id
= code
->ext
.entry
->id
;
9067 resolve_where (code
, NULL
);
9071 if (code
->expr1
!= NULL
)
9073 if (code
->expr1
->ts
.type
!= BT_INTEGER
)
9074 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9075 "INTEGER variable", &code
->expr1
->where
);
9076 else if (code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
9077 gfc_error ("Variable '%s' has not been assigned a target "
9078 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
9079 &code
->expr1
->where
);
9082 resolve_branch (code
->label1
, code
);
9086 if (code
->expr1
!= NULL
9087 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
9088 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9089 "INTEGER return specifier", &code
->expr1
->where
);
9092 case EXEC_INIT_ASSIGN
:
9093 case EXEC_END_PROCEDURE
:
9100 if (gfc_check_vardef_context (code
->expr1
, false, _("assignment"))
9104 if (resolve_ordinary_assign (code
, ns
))
9106 if (code
->op
== EXEC_COMPCALL
)
9113 case EXEC_LABEL_ASSIGN
:
9114 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
9115 gfc_error ("Label %d referenced at %L is never defined",
9116 code
->label1
->value
, &code
->label1
->where
);
9118 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
9119 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
9120 || code
->expr1
->symtree
->n
.sym
->ts
.kind
9121 != gfc_default_integer_kind
9122 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
9123 gfc_error ("ASSIGN statement at %L requires a scalar "
9124 "default INTEGER variable", &code
->expr1
->where
);
9127 case EXEC_POINTER_ASSIGN
:
9134 /* This is both a variable definition and pointer assignment
9135 context, so check both of them. For rank remapping, a final
9136 array ref may be present on the LHS and fool gfc_expr_attr
9137 used in gfc_check_vardef_context. Remove it. */
9138 e
= remove_last_array_ref (code
->expr1
);
9139 t
= gfc_check_vardef_context (e
, true, _("pointer assignment"));
9141 t
= gfc_check_vardef_context (e
, false, _("pointer assignment"));
9146 gfc_check_pointer_assign (code
->expr1
, code
->expr2
);
9150 case EXEC_ARITHMETIC_IF
:
9152 && code
->expr1
->ts
.type
!= BT_INTEGER
9153 && code
->expr1
->ts
.type
!= BT_REAL
)
9154 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9155 "expression", &code
->expr1
->where
);
9157 resolve_branch (code
->label1
, code
);
9158 resolve_branch (code
->label2
, code
);
9159 resolve_branch (code
->label3
, code
);
9163 if (t
== SUCCESS
&& code
->expr1
!= NULL
9164 && (code
->expr1
->ts
.type
!= BT_LOGICAL
9165 || code
->expr1
->rank
!= 0))
9166 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9167 &code
->expr1
->where
);
9172 resolve_call (code
);
9177 resolve_typebound_subroutine (code
);
9181 resolve_ppc_call (code
);
9185 /* Select is complicated. Also, a SELECT construct could be
9186 a transformed computed GOTO. */
9187 resolve_select (code
);
9190 case EXEC_SELECT_TYPE
:
9191 resolve_select_type (code
, ns
);
9195 resolve_block_construct (code
);
9199 if (code
->ext
.iterator
!= NULL
)
9201 gfc_iterator
*iter
= code
->ext
.iterator
;
9202 if (gfc_resolve_iterator (iter
, true) != FAILURE
)
9203 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
);
9208 if (code
->expr1
== NULL
)
9209 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9211 && (code
->expr1
->rank
!= 0
9212 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
9213 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9214 "a scalar LOGICAL expression", &code
->expr1
->where
);
9219 resolve_allocate_deallocate (code
, "ALLOCATE");
9223 case EXEC_DEALLOCATE
:
9225 resolve_allocate_deallocate (code
, "DEALLOCATE");
9230 if (gfc_resolve_open (code
->ext
.open
) == FAILURE
)
9233 resolve_branch (code
->ext
.open
->err
, code
);
9237 if (gfc_resolve_close (code
->ext
.close
) == FAILURE
)
9240 resolve_branch (code
->ext
.close
->err
, code
);
9243 case EXEC_BACKSPACE
:
9247 if (gfc_resolve_filepos (code
->ext
.filepos
) == FAILURE
)
9250 resolve_branch (code
->ext
.filepos
->err
, code
);
9254 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9257 resolve_branch (code
->ext
.inquire
->err
, code
);
9261 gcc_assert (code
->ext
.inquire
!= NULL
);
9262 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9265 resolve_branch (code
->ext
.inquire
->err
, code
);
9269 if (gfc_resolve_wait (code
->ext
.wait
) == FAILURE
)
9272 resolve_branch (code
->ext
.wait
->err
, code
);
9273 resolve_branch (code
->ext
.wait
->end
, code
);
9274 resolve_branch (code
->ext
.wait
->eor
, code
);
9279 if (gfc_resolve_dt (code
->ext
.dt
, &code
->loc
) == FAILURE
)
9282 resolve_branch (code
->ext
.dt
->err
, code
);
9283 resolve_branch (code
->ext
.dt
->end
, code
);
9284 resolve_branch (code
->ext
.dt
->eor
, code
);
9288 resolve_transfer (code
);
9292 resolve_forall_iterators (code
->ext
.forall_iterator
);
9294 if (code
->expr1
!= NULL
9295 && (code
->expr1
->ts
.type
!= BT_LOGICAL
|| code
->expr1
->rank
))
9296 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9297 "expression", &code
->expr1
->where
);
9300 case EXEC_OMP_ATOMIC
:
9301 case EXEC_OMP_BARRIER
:
9302 case EXEC_OMP_CRITICAL
:
9303 case EXEC_OMP_FLUSH
:
9305 case EXEC_OMP_MASTER
:
9306 case EXEC_OMP_ORDERED
:
9307 case EXEC_OMP_SECTIONS
:
9308 case EXEC_OMP_SINGLE
:
9309 case EXEC_OMP_TASKWAIT
:
9310 case EXEC_OMP_WORKSHARE
:
9311 gfc_resolve_omp_directive (code
, ns
);
9314 case EXEC_OMP_PARALLEL
:
9315 case EXEC_OMP_PARALLEL_DO
:
9316 case EXEC_OMP_PARALLEL_SECTIONS
:
9317 case EXEC_OMP_PARALLEL_WORKSHARE
:
9319 omp_workshare_save
= omp_workshare_flag
;
9320 omp_workshare_flag
= 0;
9321 gfc_resolve_omp_directive (code
, ns
);
9322 omp_workshare_flag
= omp_workshare_save
;
9326 gfc_internal_error ("resolve_code(): Bad statement code");
9330 cs_base
= frame
.prev
;
9334 /* Resolve initial values and make sure they are compatible with
9338 resolve_values (gfc_symbol
*sym
)
9342 if (sym
->value
== NULL
)
9345 if (sym
->value
->expr_type
== EXPR_STRUCTURE
)
9346 t
= resolve_structure_cons (sym
->value
, 1);
9348 t
= gfc_resolve_expr (sym
->value
);
9353 gfc_check_assign_symbol (sym
, sym
->value
);
9357 /* Verify the binding labels for common blocks that are BIND(C). The label
9358 for a BIND(C) common block must be identical in all scoping units in which
9359 the common block is declared. Further, the binding label can not collide
9360 with any other global entity in the program. */
9363 resolve_bind_c_comms (gfc_symtree
*comm_block_tree
)
9365 if (comm_block_tree
->n
.common
->is_bind_c
== 1)
9367 gfc_gsymbol
*binding_label_gsym
;
9368 gfc_gsymbol
*comm_name_gsym
;
9370 /* See if a global symbol exists by the common block's name. It may
9371 be NULL if the common block is use-associated. */
9372 comm_name_gsym
= gfc_find_gsymbol (gfc_gsym_root
,
9373 comm_block_tree
->n
.common
->name
);
9374 if (comm_name_gsym
!= NULL
&& comm_name_gsym
->type
!= GSYM_COMMON
)
9375 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9376 "with the global entity '%s' at %L",
9377 comm_block_tree
->n
.common
->binding_label
,
9378 comm_block_tree
->n
.common
->name
,
9379 &(comm_block_tree
->n
.common
->where
),
9380 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9381 else if (comm_name_gsym
!= NULL
9382 && strcmp (comm_name_gsym
->name
,
9383 comm_block_tree
->n
.common
->name
) == 0)
9385 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9387 if (comm_name_gsym
->binding_label
== NULL
)
9388 /* No binding label for common block stored yet; save this one. */
9389 comm_name_gsym
->binding_label
=
9390 comm_block_tree
->n
.common
->binding_label
;
9392 if (strcmp (comm_name_gsym
->binding_label
,
9393 comm_block_tree
->n
.common
->binding_label
) != 0)
9395 /* Common block names match but binding labels do not. */
9396 gfc_error ("Binding label '%s' for common block '%s' at %L "
9397 "does not match the binding label '%s' for common "
9399 comm_block_tree
->n
.common
->binding_label
,
9400 comm_block_tree
->n
.common
->name
,
9401 &(comm_block_tree
->n
.common
->where
),
9402 comm_name_gsym
->binding_label
,
9403 comm_name_gsym
->name
,
9404 &(comm_name_gsym
->where
));
9409 /* There is no binding label (NAME="") so we have nothing further to
9410 check and nothing to add as a global symbol for the label. */
9411 if (comm_block_tree
->n
.common
->binding_label
[0] == '\0' )
9414 binding_label_gsym
=
9415 gfc_find_gsymbol (gfc_gsym_root
,
9416 comm_block_tree
->n
.common
->binding_label
);
9417 if (binding_label_gsym
== NULL
)
9419 /* Need to make a global symbol for the binding label to prevent
9420 it from colliding with another. */
9421 binding_label_gsym
=
9422 gfc_get_gsymbol (comm_block_tree
->n
.common
->binding_label
);
9423 binding_label_gsym
->sym_name
= comm_block_tree
->n
.common
->name
;
9424 binding_label_gsym
->type
= GSYM_COMMON
;
9428 /* If comm_name_gsym is NULL, the name common block is use
9429 associated and the name could be colliding. */
9430 if (binding_label_gsym
->type
!= GSYM_COMMON
)
9431 gfc_error ("Binding label '%s' for common block '%s' at %L "
9432 "collides with the global entity '%s' at %L",
9433 comm_block_tree
->n
.common
->binding_label
,
9434 comm_block_tree
->n
.common
->name
,
9435 &(comm_block_tree
->n
.common
->where
),
9436 binding_label_gsym
->name
,
9437 &(binding_label_gsym
->where
));
9438 else if (comm_name_gsym
!= NULL
9439 && (strcmp (binding_label_gsym
->name
,
9440 comm_name_gsym
->binding_label
) != 0)
9441 && (strcmp (binding_label_gsym
->sym_name
,
9442 comm_name_gsym
->name
) != 0))
9443 gfc_error ("Binding label '%s' for common block '%s' at %L "
9444 "collides with global entity '%s' at %L",
9445 binding_label_gsym
->name
, binding_label_gsym
->sym_name
,
9446 &(comm_block_tree
->n
.common
->where
),
9447 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9455 /* Verify any BIND(C) derived types in the namespace so we can report errors
9456 for them once, rather than for each variable declared of that type. */
9459 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
9461 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
9462 && derived_sym
->attr
.is_bind_c
== 1)
9463 verify_bind_c_derived_type (derived_sym
);
9469 /* Verify that any binding labels used in a given namespace do not collide
9470 with the names or binding labels of any global symbols. */
9473 gfc_verify_binding_labels (gfc_symbol
*sym
)
9477 if (sym
!= NULL
&& sym
->attr
.is_bind_c
&& sym
->attr
.is_iso_c
== 0
9478 && sym
->attr
.flavor
!= FL_DERIVED
&& sym
->binding_label
[0] != '\0')
9480 gfc_gsymbol
*bind_c_sym
;
9482 bind_c_sym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
9483 if (bind_c_sym
!= NULL
9484 && strcmp (bind_c_sym
->name
, sym
->binding_label
) == 0)
9486 if (sym
->attr
.if_source
== IFSRC_DECL
9487 && (bind_c_sym
->type
!= GSYM_SUBROUTINE
9488 && bind_c_sym
->type
!= GSYM_FUNCTION
)
9489 && ((sym
->attr
.contained
== 1
9490 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0)
9491 || (sym
->attr
.use_assoc
== 1
9492 && (strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0))))
9494 /* Make sure global procedures don't collide with anything. */
9495 gfc_error ("Binding label '%s' at %L collides with the global "
9496 "entity '%s' at %L", sym
->binding_label
,
9497 &(sym
->declared_at
), bind_c_sym
->name
,
9498 &(bind_c_sym
->where
));
9501 else if (sym
->attr
.contained
== 0
9502 && (sym
->attr
.if_source
== IFSRC_IFBODY
9503 && sym
->attr
.flavor
== FL_PROCEDURE
)
9504 && (bind_c_sym
->sym_name
!= NULL
9505 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0))
9507 /* Make sure procedures in interface bodies don't collide. */
9508 gfc_error ("Binding label '%s' in interface body at %L collides "
9509 "with the global entity '%s' at %L",
9511 &(sym
->declared_at
), bind_c_sym
->name
,
9512 &(bind_c_sym
->where
));
9515 else if (sym
->attr
.contained
== 0
9516 && sym
->attr
.if_source
== IFSRC_UNKNOWN
)
9517 if ((sym
->attr
.use_assoc
&& bind_c_sym
->mod_name
9518 && strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0)
9519 || sym
->attr
.use_assoc
== 0)
9521 gfc_error ("Binding label '%s' at %L collides with global "
9522 "entity '%s' at %L", sym
->binding_label
,
9523 &(sym
->declared_at
), bind_c_sym
->name
,
9524 &(bind_c_sym
->where
));
9529 /* Clear the binding label to prevent checking multiple times. */
9530 sym
->binding_label
[0] = '\0';
9532 else if (bind_c_sym
== NULL
)
9534 bind_c_sym
= gfc_get_gsymbol (sym
->binding_label
);
9535 bind_c_sym
->where
= sym
->declared_at
;
9536 bind_c_sym
->sym_name
= sym
->name
;
9538 if (sym
->attr
.use_assoc
== 1)
9539 bind_c_sym
->mod_name
= sym
->module
;
9541 if (sym
->ns
->proc_name
!= NULL
)
9542 bind_c_sym
->mod_name
= sym
->ns
->proc_name
->name
;
9544 if (sym
->attr
.contained
== 0)
9546 if (sym
->attr
.subroutine
)
9547 bind_c_sym
->type
= GSYM_SUBROUTINE
;
9548 else if (sym
->attr
.function
)
9549 bind_c_sym
->type
= GSYM_FUNCTION
;
9557 /* Resolve an index expression. */
9560 resolve_index_expr (gfc_expr
*e
)
9562 if (gfc_resolve_expr (e
) == FAILURE
)
9565 if (gfc_simplify_expr (e
, 0) == FAILURE
)
9568 if (gfc_specification_expr (e
) == FAILURE
)
9575 /* Resolve a charlen structure. */
9578 resolve_charlen (gfc_charlen
*cl
)
9587 specification_expr
= 1;
9589 if (resolve_index_expr (cl
->length
) == FAILURE
)
9591 specification_expr
= 0;
9595 /* "If the character length parameter value evaluates to a negative
9596 value, the length of character entities declared is zero." */
9597 if (cl
->length
&& !gfc_extract_int (cl
->length
, &i
) && i
< 0)
9599 if (gfc_option
.warn_surprising
)
9600 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9601 " the length has been set to zero",
9602 &cl
->length
->where
, i
);
9603 gfc_replace_expr (cl
->length
,
9604 gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0));
9607 /* Check that the character length is not too large. */
9608 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
9609 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
9610 && cl
->length
->ts
.type
== BT_INTEGER
9611 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
9613 gfc_error ("String length at %L is too large", &cl
->length
->where
);
9621 /* Test for non-constant shape arrays. */
9624 is_non_constant_shape_array (gfc_symbol
*sym
)
9630 not_constant
= false;
9631 if (sym
->as
!= NULL
)
9633 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9634 has not been simplified; parameter array references. Do the
9635 simplification now. */
9636 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
9638 e
= sym
->as
->lower
[i
];
9639 if (e
&& (resolve_index_expr (e
) == FAILURE
9640 || !gfc_is_constant_expr (e
)))
9641 not_constant
= true;
9642 e
= sym
->as
->upper
[i
];
9643 if (e
&& (resolve_index_expr (e
) == FAILURE
9644 || !gfc_is_constant_expr (e
)))
9645 not_constant
= true;
9648 return not_constant
;
9651 /* Given a symbol and an initialization expression, add code to initialize
9652 the symbol to the function entry. */
9654 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
9658 gfc_namespace
*ns
= sym
->ns
;
9660 /* Search for the function namespace if this is a contained
9661 function without an explicit result. */
9662 if (sym
->attr
.function
&& sym
== sym
->result
9663 && sym
->name
!= sym
->ns
->proc_name
->name
)
9666 for (;ns
; ns
= ns
->sibling
)
9667 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
9673 gfc_free_expr (init
);
9677 /* Build an l-value expression for the result. */
9678 lval
= gfc_lval_expr_from_sym (sym
);
9680 /* Add the code at scope entry. */
9681 init_st
= gfc_get_code ();
9682 init_st
->next
= ns
->code
;
9685 /* Assign the default initializer to the l-value. */
9686 init_st
->loc
= sym
->declared_at
;
9687 init_st
->op
= EXEC_INIT_ASSIGN
;
9688 init_st
->expr1
= lval
;
9689 init_st
->expr2
= init
;
9692 /* Assign the default initializer to a derived type variable or result. */
9695 apply_default_init (gfc_symbol
*sym
)
9697 gfc_expr
*init
= NULL
;
9699 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9702 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
9703 init
= gfc_default_initializer (&sym
->ts
);
9705 if (init
== NULL
&& sym
->ts
.type
!= BT_CLASS
)
9708 build_init_assign (sym
, init
);
9709 sym
->attr
.referenced
= 1;
9712 /* Build an initializer for a local integer, real, complex, logical, or
9713 character variable, based on the command line flags finit-local-zero,
9714 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9715 null if the symbol should not have a default initialization. */
9717 build_default_init_expr (gfc_symbol
*sym
)
9720 gfc_expr
*init_expr
;
9723 /* These symbols should never have a default initialization. */
9724 if ((sym
->attr
.dimension
&& !gfc_is_compile_time_shape (sym
->as
))
9725 || sym
->attr
.external
9727 || sym
->attr
.pointer
9728 || sym
->attr
.in_equivalence
9729 || sym
->attr
.in_common
9732 || sym
->attr
.cray_pointee
9733 || sym
->attr
.cray_pointer
)
9736 /* Now we'll try to build an initializer expression. */
9737 init_expr
= gfc_get_constant_expr (sym
->ts
.type
, sym
->ts
.kind
,
9740 /* We will only initialize integers, reals, complex, logicals, and
9741 characters, and only if the corresponding command-line flags
9742 were set. Otherwise, we free init_expr and return null. */
9743 switch (sym
->ts
.type
)
9746 if (gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
9747 mpz_set_si (init_expr
->value
.integer
,
9748 gfc_option
.flag_init_integer_value
);
9751 gfc_free_expr (init_expr
);
9757 switch (gfc_option
.flag_init_real
)
9759 case GFC_INIT_REAL_SNAN
:
9760 init_expr
->is_snan
= 1;
9762 case GFC_INIT_REAL_NAN
:
9763 mpfr_set_nan (init_expr
->value
.real
);
9766 case GFC_INIT_REAL_INF
:
9767 mpfr_set_inf (init_expr
->value
.real
, 1);
9770 case GFC_INIT_REAL_NEG_INF
:
9771 mpfr_set_inf (init_expr
->value
.real
, -1);
9774 case GFC_INIT_REAL_ZERO
:
9775 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
9779 gfc_free_expr (init_expr
);
9786 switch (gfc_option
.flag_init_real
)
9788 case GFC_INIT_REAL_SNAN
:
9789 init_expr
->is_snan
= 1;
9791 case GFC_INIT_REAL_NAN
:
9792 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
9793 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
9796 case GFC_INIT_REAL_INF
:
9797 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
9798 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
9801 case GFC_INIT_REAL_NEG_INF
:
9802 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
9803 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
9806 case GFC_INIT_REAL_ZERO
:
9807 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
9811 gfc_free_expr (init_expr
);
9818 if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_FALSE
)
9819 init_expr
->value
.logical
= 0;
9820 else if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_TRUE
)
9821 init_expr
->value
.logical
= 1;
9824 gfc_free_expr (init_expr
);
9830 /* For characters, the length must be constant in order to
9831 create a default initializer. */
9832 if (gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
9833 && sym
->ts
.u
.cl
->length
9834 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9836 char_len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
9837 init_expr
->value
.character
.length
= char_len
;
9838 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
9839 for (i
= 0; i
< char_len
; i
++)
9840 init_expr
->value
.character
.string
[i
]
9841 = (unsigned char) gfc_option
.flag_init_character_value
;
9845 gfc_free_expr (init_expr
);
9851 gfc_free_expr (init_expr
);
9857 /* Add an initialization expression to a local variable. */
9859 apply_default_init_local (gfc_symbol
*sym
)
9861 gfc_expr
*init
= NULL
;
9863 /* The symbol should be a variable or a function return value. */
9864 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9865 || (sym
->attr
.function
&& sym
->result
!= sym
))
9868 /* Try to build the initializer expression. If we can't initialize
9869 this symbol, then init will be NULL. */
9870 init
= build_default_init_expr (sym
);
9874 /* For saved variables, we don't want to add an initializer at
9875 function entry, so we just add a static initializer. */
9876 if (sym
->attr
.save
|| sym
->ns
->save_all
9877 || gfc_option
.flag_max_stack_var_size
== 0)
9879 /* Don't clobber an existing initializer! */
9880 gcc_assert (sym
->value
== NULL
);
9885 build_init_assign (sym
, init
);
9889 /* Resolution of common features of flavors variable and procedure. */
9892 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
9894 /* Avoid double diagnostics for function result symbols. */
9895 if ((sym
->result
|| sym
->attr
.result
) && !sym
->attr
.dummy
9896 && (sym
->ns
!= gfc_current_ns
))
9899 /* Constraints on deferred shape variable. */
9900 if (sym
->as
== NULL
|| sym
->as
->type
!= AS_DEFERRED
)
9902 if (sym
->attr
.allocatable
)
9904 if (sym
->attr
.dimension
)
9906 gfc_error ("Allocatable array '%s' at %L must have "
9907 "a deferred shape", sym
->name
, &sym
->declared_at
);
9910 else if (gfc_notify_std (GFC_STD_F2003
, "Scalar object '%s' at %L "
9911 "may not be ALLOCATABLE", sym
->name
,
9912 &sym
->declared_at
) == FAILURE
)
9916 if (sym
->attr
.pointer
&& sym
->attr
.dimension
)
9918 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9919 sym
->name
, &sym
->declared_at
);
9925 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
9926 && sym
->ts
.type
!= BT_CLASS
&& !sym
->assoc
)
9928 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9929 sym
->name
, &sym
->declared_at
);
9934 /* Constraints on polymorphic variables. */
9935 if (sym
->ts
.type
== BT_CLASS
&& !(sym
->result
&& sym
->result
!= sym
))
9938 if (sym
->attr
.class_ok
9939 && !gfc_type_is_extensible (CLASS_DATA (sym
)->ts
.u
.derived
))
9941 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9942 CLASS_DATA (sym
)->ts
.u
.derived
->name
, sym
->name
,
9948 /* Assume that use associated symbols were checked in the module ns.
9949 Class-variables that are associate-names are also something special
9950 and excepted from the test. */
9951 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
&& !sym
->assoc
)
9953 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9954 "or pointer", sym
->name
, &sym
->declared_at
);
9963 /* Additional checks for symbols with flavor variable and derived
9964 type. To be called from resolve_fl_variable. */
9967 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
9969 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
9971 /* Check to see if a derived type is blocked from being host
9972 associated by the presence of another class I symbol in the same
9973 namespace. 14.6.1.3 of the standard and the discussion on
9974 comp.lang.fortran. */
9975 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
9976 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
9979 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
9980 if (s
&& s
->attr
.flavor
!= FL_DERIVED
)
9982 gfc_error ("The type '%s' cannot be host associated at %L "
9983 "because it is blocked by an incompatible object "
9984 "of the same name declared at %L",
9985 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
9991 /* 4th constraint in section 11.3: "If an object of a type for which
9992 component-initialization is specified (R429) appears in the
9993 specification-part of a module and does not have the ALLOCATABLE
9994 or POINTER attribute, the object shall have the SAVE attribute."
9996 The check for initializers is performed with
9997 gfc_has_default_initializer because gfc_default_initializer generates
9998 a hidden default for allocatable components. */
9999 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
10000 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10001 && !sym
->ns
->save_all
&& !sym
->attr
.save
10002 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
10003 && gfc_has_default_initializer (sym
->ts
.u
.derived
)
10004 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Implied SAVE for "
10005 "module variable '%s' at %L, needed due to "
10006 "the default initialization", sym
->name
,
10007 &sym
->declared_at
) == FAILURE
)
10010 /* Assign default initializer. */
10011 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
10012 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
10014 sym
->value
= gfc_default_initializer (&sym
->ts
);
10021 /* Resolve symbols with flavor variable. */
10024 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
10026 int no_init_flag
, automatic_flag
;
10028 const char *auto_save_msg
;
10030 auto_save_msg
= "Automatic object '%s' at %L cannot have the "
10033 if (resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
10036 /* Set this flag to check that variables are parameters of all entries.
10037 This check is effected by the call to gfc_resolve_expr through
10038 is_non_constant_shape_array. */
10039 specification_expr
= 1;
10041 if (sym
->ns
->proc_name
10042 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10043 || sym
->ns
->proc_name
->attr
.is_main_program
)
10044 && !sym
->attr
.use_assoc
10045 && !sym
->attr
.allocatable
10046 && !sym
->attr
.pointer
10047 && is_non_constant_shape_array (sym
))
10049 /* The shape of a main program or module array needs to be
10051 gfc_error ("The module or main program array '%s' at %L must "
10052 "have constant shape", sym
->name
, &sym
->declared_at
);
10053 specification_expr
= 0;
10057 /* Constraints on deferred type parameter. */
10058 if (sym
->ts
.deferred
&& !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
10060 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10061 "requires either the pointer or allocatable attribute",
10062 sym
->name
, &sym
->declared_at
);
10066 if (sym
->ts
.type
== BT_CHARACTER
)
10068 /* Make sure that character string variables with assumed length are
10069 dummy arguments. */
10070 e
= sym
->ts
.u
.cl
->length
;
10071 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
10072 && !sym
->ts
.deferred
)
10074 gfc_error ("Entity with assumed character length at %L must be a "
10075 "dummy argument or a PARAMETER", &sym
->declared_at
);
10079 if (e
&& sym
->attr
.save
== SAVE_EXPLICIT
&& !gfc_is_constant_expr (e
))
10081 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
10085 if (!gfc_is_constant_expr (e
)
10086 && !(e
->expr_type
== EXPR_VARIABLE
10087 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
10088 && sym
->ns
->proc_name
10089 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10090 || sym
->ns
->proc_name
->attr
.is_main_program
)
10091 && !sym
->attr
.use_assoc
)
10093 gfc_error ("'%s' at %L must have constant character length "
10094 "in this context", sym
->name
, &sym
->declared_at
);
10099 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
10100 apply_default_init_local (sym
); /* Try to apply a default initialization. */
10102 /* Determine if the symbol may not have an initializer. */
10103 no_init_flag
= automatic_flag
= 0;
10104 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
10105 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
10107 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
10108 && is_non_constant_shape_array (sym
))
10110 no_init_flag
= automatic_flag
= 1;
10112 /* Also, they must not have the SAVE attribute.
10113 SAVE_IMPLICIT is checked below. */
10114 if (sym
->attr
.save
== SAVE_EXPLICIT
)
10116 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
10121 /* Ensure that any initializer is simplified. */
10123 gfc_simplify_expr (sym
->value
, 1);
10125 /* Reject illegal initializers. */
10126 if (!sym
->mark
&& sym
->value
)
10128 if (sym
->attr
.allocatable
|| (sym
->ts
.type
== BT_CLASS
10129 && CLASS_DATA (sym
)->attr
.allocatable
))
10130 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10131 sym
->name
, &sym
->declared_at
);
10132 else if (sym
->attr
.external
)
10133 gfc_error ("External '%s' at %L cannot have an initializer",
10134 sym
->name
, &sym
->declared_at
);
10135 else if (sym
->attr
.dummy
10136 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
10137 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10138 sym
->name
, &sym
->declared_at
);
10139 else if (sym
->attr
.intrinsic
)
10140 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10141 sym
->name
, &sym
->declared_at
);
10142 else if (sym
->attr
.result
)
10143 gfc_error ("Function result '%s' at %L cannot have an initializer",
10144 sym
->name
, &sym
->declared_at
);
10145 else if (automatic_flag
)
10146 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10147 sym
->name
, &sym
->declared_at
);
10149 goto no_init_error
;
10154 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
10155 return resolve_fl_variable_derived (sym
, no_init_flag
);
10161 /* Resolve a procedure. */
10164 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
10166 gfc_formal_arglist
*arg
;
10168 if (sym
->attr
.function
10169 && resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
10172 if (sym
->ts
.type
== BT_CHARACTER
)
10174 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
10176 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
10177 && resolve_charlen (cl
) == FAILURE
)
10180 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
10181 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
10183 gfc_error ("Character-valued statement function '%s' at %L must "
10184 "have constant length", sym
->name
, &sym
->declared_at
);
10189 /* Ensure that derived type for are not of a private type. Internal
10190 module procedures are excluded by 2.2.3.3 - i.e., they are not
10191 externally accessible and can access all the objects accessible in
10193 if (!(sym
->ns
->parent
10194 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
10195 && gfc_check_symbol_access (sym
))
10197 gfc_interface
*iface
;
10199 for (arg
= sym
->formal
; arg
; arg
= arg
->next
)
10202 && arg
->sym
->ts
.type
== BT_DERIVED
10203 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10204 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10205 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: '%s' is of a "
10206 "PRIVATE type and cannot be a dummy argument"
10207 " of '%s', which is PUBLIC at %L",
10208 arg
->sym
->name
, sym
->name
, &sym
->declared_at
)
10211 /* Stop this message from recurring. */
10212 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10217 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10218 PRIVATE to the containing module. */
10219 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10221 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10224 && arg
->sym
->ts
.type
== BT_DERIVED
10225 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10226 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10227 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10228 "'%s' in PUBLIC interface '%s' at %L "
10229 "takes dummy arguments of '%s' which is "
10230 "PRIVATE", iface
->sym
->name
, sym
->name
,
10231 &iface
->sym
->declared_at
,
10232 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10234 /* Stop this message from recurring. */
10235 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10241 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10242 PRIVATE to the containing module. */
10243 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10245 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10248 && arg
->sym
->ts
.type
== BT_DERIVED
10249 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10250 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10251 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10252 "'%s' in PUBLIC interface '%s' at %L "
10253 "takes dummy arguments of '%s' which is "
10254 "PRIVATE", iface
->sym
->name
, sym
->name
,
10255 &iface
->sym
->declared_at
,
10256 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10258 /* Stop this message from recurring. */
10259 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10266 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
10267 && !sym
->attr
.proc_pointer
)
10269 gfc_error ("Function '%s' at %L cannot have an initializer",
10270 sym
->name
, &sym
->declared_at
);
10274 /* An external symbol may not have an initializer because it is taken to be
10275 a procedure. Exception: Procedure Pointers. */
10276 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
10278 gfc_error ("External object '%s' at %L may not have an initializer",
10279 sym
->name
, &sym
->declared_at
);
10283 /* An elemental function is required to return a scalar 12.7.1 */
10284 if (sym
->attr
.elemental
&& sym
->attr
.function
&& sym
->as
)
10286 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10287 "result", sym
->name
, &sym
->declared_at
);
10288 /* Reset so that the error only occurs once. */
10289 sym
->attr
.elemental
= 0;
10293 if (sym
->attr
.proc
== PROC_ST_FUNCTION
10294 && (sym
->attr
.allocatable
|| sym
->attr
.pointer
))
10296 gfc_error ("Statement function '%s' at %L may not have pointer or "
10297 "allocatable attribute", sym
->name
, &sym
->declared_at
);
10301 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10302 char-len-param shall not be array-valued, pointer-valued, recursive
10303 or pure. ....snip... A character value of * may only be used in the
10304 following ways: (i) Dummy arg of procedure - dummy associates with
10305 actual length; (ii) To declare a named constant; or (iii) External
10306 function - but length must be declared in calling scoping unit. */
10307 if (sym
->attr
.function
10308 && sym
->ts
.type
== BT_CHARACTER
10309 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
10311 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
10312 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
10314 if (sym
->as
&& sym
->as
->rank
)
10315 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10316 "array-valued", sym
->name
, &sym
->declared_at
);
10318 if (sym
->attr
.pointer
)
10319 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10320 "pointer-valued", sym
->name
, &sym
->declared_at
);
10322 if (sym
->attr
.pure
)
10323 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10324 "pure", sym
->name
, &sym
->declared_at
);
10326 if (sym
->attr
.recursive
)
10327 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10328 "recursive", sym
->name
, &sym
->declared_at
);
10333 /* Appendix B.2 of the standard. Contained functions give an
10334 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10335 character length is an F2003 feature. */
10336 if (!sym
->attr
.contained
10337 && gfc_current_form
!= FORM_FIXED
10338 && !sym
->ts
.deferred
)
10339 gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
10340 "CHARACTER(*) function '%s' at %L",
10341 sym
->name
, &sym
->declared_at
);
10344 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
10346 gfc_formal_arglist
*curr_arg
;
10347 int has_non_interop_arg
= 0;
10349 if (verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
10350 sym
->common_block
) == FAILURE
)
10352 /* Clear these to prevent looking at them again if there was an
10354 sym
->attr
.is_bind_c
= 0;
10355 sym
->attr
.is_c_interop
= 0;
10356 sym
->ts
.is_c_interop
= 0;
10360 /* So far, no errors have been found. */
10361 sym
->attr
.is_c_interop
= 1;
10362 sym
->ts
.is_c_interop
= 1;
10365 curr_arg
= sym
->formal
;
10366 while (curr_arg
!= NULL
)
10368 /* Skip implicitly typed dummy args here. */
10369 if (curr_arg
->sym
->attr
.implicit_type
== 0)
10370 if (verify_c_interop_param (curr_arg
->sym
) == FAILURE
)
10371 /* If something is found to fail, record the fact so we
10372 can mark the symbol for the procedure as not being
10373 BIND(C) to try and prevent multiple errors being
10375 has_non_interop_arg
= 1;
10377 curr_arg
= curr_arg
->next
;
10380 /* See if any of the arguments were not interoperable and if so, clear
10381 the procedure symbol to prevent duplicate error messages. */
10382 if (has_non_interop_arg
!= 0)
10384 sym
->attr
.is_c_interop
= 0;
10385 sym
->ts
.is_c_interop
= 0;
10386 sym
->attr
.is_bind_c
= 0;
10390 if (!sym
->attr
.proc_pointer
)
10392 if (sym
->attr
.save
== SAVE_EXPLICIT
)
10394 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10395 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10398 if (sym
->attr
.intent
)
10400 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10401 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10404 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
10406 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10407 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10410 if (sym
->attr
.external
&& sym
->attr
.function
10411 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
10412 || sym
->attr
.contained
))
10414 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10415 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10418 if (strcmp ("ppr@", sym
->name
) == 0)
10420 gfc_error ("Procedure pointer result '%s' at %L "
10421 "is missing the pointer attribute",
10422 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
10431 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10432 been defined and we now know their defined arguments, check that they fulfill
10433 the requirements of the standard for procedures used as finalizers. */
10436 gfc_resolve_finalizers (gfc_symbol
* derived
)
10438 gfc_finalizer
* list
;
10439 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
10440 gfc_try result
= SUCCESS
;
10441 bool seen_scalar
= false;
10443 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->finalizers
)
10446 /* Walk over the list of finalizer-procedures, check them, and if any one
10447 does not fit in with the standard's definition, print an error and remove
10448 it from the list. */
10449 prev_link
= &derived
->f2k_derived
->finalizers
;
10450 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
10456 /* Skip this finalizer if we already resolved it. */
10457 if (list
->proc_tree
)
10459 prev_link
= &(list
->next
);
10463 /* Check this exists and is a SUBROUTINE. */
10464 if (!list
->proc_sym
->attr
.subroutine
)
10466 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10467 list
->proc_sym
->name
, &list
->where
);
10471 /* We should have exactly one argument. */
10472 if (!list
->proc_sym
->formal
|| list
->proc_sym
->formal
->next
)
10474 gfc_error ("FINAL procedure at %L must have exactly one argument",
10478 arg
= list
->proc_sym
->formal
->sym
;
10480 /* This argument must be of our type. */
10481 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
10483 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10484 &arg
->declared_at
, derived
->name
);
10488 /* It must neither be a pointer nor allocatable nor optional. */
10489 if (arg
->attr
.pointer
)
10491 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10492 &arg
->declared_at
);
10495 if (arg
->attr
.allocatable
)
10497 gfc_error ("Argument of FINAL procedure at %L must not be"
10498 " ALLOCATABLE", &arg
->declared_at
);
10501 if (arg
->attr
.optional
)
10503 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10504 &arg
->declared_at
);
10508 /* It must not be INTENT(OUT). */
10509 if (arg
->attr
.intent
== INTENT_OUT
)
10511 gfc_error ("Argument of FINAL procedure at %L must not be"
10512 " INTENT(OUT)", &arg
->declared_at
);
10516 /* Warn if the procedure is non-scalar and not assumed shape. */
10517 if (gfc_option
.warn_surprising
&& arg
->as
&& arg
->as
->rank
> 0
10518 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
10519 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10520 " shape argument", &arg
->declared_at
);
10522 /* Check that it does not match in kind and rank with a FINAL procedure
10523 defined earlier. To really loop over the *earlier* declarations,
10524 we need to walk the tail of the list as new ones were pushed at the
10526 /* TODO: Handle kind parameters once they are implemented. */
10527 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
10528 for (i
= list
->next
; i
; i
= i
->next
)
10530 /* Argument list might be empty; that is an error signalled earlier,
10531 but we nevertheless continued resolving. */
10532 if (i
->proc_sym
->formal
)
10534 gfc_symbol
* i_arg
= i
->proc_sym
->formal
->sym
;
10535 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
10536 if (i_rank
== my_rank
)
10538 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10539 " rank (%d) as '%s'",
10540 list
->proc_sym
->name
, &list
->where
, my_rank
,
10541 i
->proc_sym
->name
);
10547 /* Is this the/a scalar finalizer procedure? */
10548 if (!arg
->as
|| arg
->as
->rank
== 0)
10549 seen_scalar
= true;
10551 /* Find the symtree for this procedure. */
10552 gcc_assert (!list
->proc_tree
);
10553 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
10555 prev_link
= &list
->next
;
10558 /* Remove wrong nodes immediately from the list so we don't risk any
10559 troubles in the future when they might fail later expectations. */
10563 *prev_link
= list
->next
;
10564 gfc_free_finalizer (i
);
10567 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10568 were nodes in the list, must have been for arrays. It is surely a good
10569 idea to have a scalar version there if there's something to finalize. */
10570 if (gfc_option
.warn_surprising
&& result
== SUCCESS
&& !seen_scalar
)
10571 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10572 " defined at %L, suggest also scalar one",
10573 derived
->name
, &derived
->declared_at
);
10575 /* TODO: Remove this error when finalization is finished. */
10576 gfc_error ("Finalization at %L is not yet implemented",
10577 &derived
->declared_at
);
10583 /* Check that it is ok for the typebound procedure proc to override the
10587 check_typebound_override (gfc_symtree
* proc
, gfc_symtree
* old
)
10590 const gfc_symbol
* proc_target
;
10591 const gfc_symbol
* old_target
;
10592 unsigned proc_pass_arg
, old_pass_arg
, argpos
;
10593 gfc_formal_arglist
* proc_formal
;
10594 gfc_formal_arglist
* old_formal
;
10596 /* This procedure should only be called for non-GENERIC proc. */
10597 gcc_assert (!proc
->n
.tb
->is_generic
);
10599 /* If the overwritten procedure is GENERIC, this is an error. */
10600 if (old
->n
.tb
->is_generic
)
10602 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10603 old
->name
, &proc
->n
.tb
->where
);
10607 where
= proc
->n
.tb
->where
;
10608 proc_target
= proc
->n
.tb
->u
.specific
->n
.sym
;
10609 old_target
= old
->n
.tb
->u
.specific
->n
.sym
;
10611 /* Check that overridden binding is not NON_OVERRIDABLE. */
10612 if (old
->n
.tb
->non_overridable
)
10614 gfc_error ("'%s' at %L overrides a procedure binding declared"
10615 " NON_OVERRIDABLE", proc
->name
, &where
);
10619 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10620 if (!old
->n
.tb
->deferred
&& proc
->n
.tb
->deferred
)
10622 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10623 " non-DEFERRED binding", proc
->name
, &where
);
10627 /* If the overridden binding is PURE, the overriding must be, too. */
10628 if (old_target
->attr
.pure
&& !proc_target
->attr
.pure
)
10630 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10631 proc
->name
, &where
);
10635 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10636 is not, the overriding must not be either. */
10637 if (old_target
->attr
.elemental
&& !proc_target
->attr
.elemental
)
10639 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10640 " ELEMENTAL", proc
->name
, &where
);
10643 if (!old_target
->attr
.elemental
&& proc_target
->attr
.elemental
)
10645 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10646 " be ELEMENTAL, either", proc
->name
, &where
);
10650 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10652 if (old_target
->attr
.subroutine
&& !proc_target
->attr
.subroutine
)
10654 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10655 " SUBROUTINE", proc
->name
, &where
);
10659 /* If the overridden binding is a FUNCTION, the overriding must also be a
10660 FUNCTION and have the same characteristics. */
10661 if (old_target
->attr
.function
)
10663 if (!proc_target
->attr
.function
)
10665 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10666 " FUNCTION", proc
->name
, &where
);
10670 /* FIXME: Do more comprehensive checking (including, for instance, the
10671 rank and array-shape). */
10672 gcc_assert (proc_target
->result
&& old_target
->result
);
10673 if (!gfc_compare_types (&proc_target
->result
->ts
,
10674 &old_target
->result
->ts
))
10676 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10677 " matching result types", proc
->name
, &where
);
10682 /* If the overridden binding is PUBLIC, the overriding one must not be
10684 if (old
->n
.tb
->access
== ACCESS_PUBLIC
10685 && proc
->n
.tb
->access
== ACCESS_PRIVATE
)
10687 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10688 " PRIVATE", proc
->name
, &where
);
10692 /* Compare the formal argument lists of both procedures. This is also abused
10693 to find the position of the passed-object dummy arguments of both
10694 bindings as at least the overridden one might not yet be resolved and we
10695 need those positions in the check below. */
10696 proc_pass_arg
= old_pass_arg
= 0;
10697 if (!proc
->n
.tb
->nopass
&& !proc
->n
.tb
->pass_arg
)
10699 if (!old
->n
.tb
->nopass
&& !old
->n
.tb
->pass_arg
)
10702 for (proc_formal
= proc_target
->formal
, old_formal
= old_target
->formal
;
10703 proc_formal
&& old_formal
;
10704 proc_formal
= proc_formal
->next
, old_formal
= old_formal
->next
)
10706 if (proc
->n
.tb
->pass_arg
10707 && !strcmp (proc
->n
.tb
->pass_arg
, proc_formal
->sym
->name
))
10708 proc_pass_arg
= argpos
;
10709 if (old
->n
.tb
->pass_arg
10710 && !strcmp (old
->n
.tb
->pass_arg
, old_formal
->sym
->name
))
10711 old_pass_arg
= argpos
;
10713 /* Check that the names correspond. */
10714 if (strcmp (proc_formal
->sym
->name
, old_formal
->sym
->name
))
10716 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10717 " to match the corresponding argument of the overridden"
10718 " procedure", proc_formal
->sym
->name
, proc
->name
, &where
,
10719 old_formal
->sym
->name
);
10723 /* Check that the types correspond if neither is the passed-object
10725 /* FIXME: Do more comprehensive testing here. */
10726 if (proc_pass_arg
!= argpos
&& old_pass_arg
!= argpos
10727 && !gfc_compare_types (&proc_formal
->sym
->ts
, &old_formal
->sym
->ts
))
10729 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10730 "in respect to the overridden procedure",
10731 proc_formal
->sym
->name
, proc
->name
, &where
);
10737 if (proc_formal
|| old_formal
)
10739 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10740 " the overridden procedure", proc
->name
, &where
);
10744 /* If the overridden binding is NOPASS, the overriding one must also be
10746 if (old
->n
.tb
->nopass
&& !proc
->n
.tb
->nopass
)
10748 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10749 " NOPASS", proc
->name
, &where
);
10753 /* If the overridden binding is PASS(x), the overriding one must also be
10754 PASS and the passed-object dummy arguments must correspond. */
10755 if (!old
->n
.tb
->nopass
)
10757 if (proc
->n
.tb
->nopass
)
10759 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10760 " PASS", proc
->name
, &where
);
10764 if (proc_pass_arg
!= old_pass_arg
)
10766 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10767 " the same position as the passed-object dummy argument of"
10768 " the overridden procedure", proc
->name
, &where
);
10777 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10780 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
10781 const char* generic_name
, locus where
)
10786 gcc_assert (t1
->specific
&& t2
->specific
);
10787 gcc_assert (!t1
->specific
->is_generic
);
10788 gcc_assert (!t2
->specific
->is_generic
);
10790 sym1
= t1
->specific
->u
.specific
->n
.sym
;
10791 sym2
= t2
->specific
->u
.specific
->n
.sym
;
10796 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10797 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
10798 || sym1
->attr
.function
!= sym2
->attr
.function
)
10800 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10801 " GENERIC '%s' at %L",
10802 sym1
->name
, sym2
->name
, generic_name
, &where
);
10806 /* Compare the interfaces. */
10807 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, 1, 0, NULL
, 0))
10809 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10810 sym1
->name
, sym2
->name
, generic_name
, &where
);
10818 /* Worker function for resolving a generic procedure binding; this is used to
10819 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10821 The difference between those cases is finding possible inherited bindings
10822 that are overridden, as one has to look for them in tb_sym_root,
10823 tb_uop_root or tb_op, respectively. Thus the caller must already find
10824 the super-type and set p->overridden correctly. */
10827 resolve_tb_generic_targets (gfc_symbol
* super_type
,
10828 gfc_typebound_proc
* p
, const char* name
)
10830 gfc_tbp_generic
* target
;
10831 gfc_symtree
* first_target
;
10832 gfc_symtree
* inherited
;
10834 gcc_assert (p
&& p
->is_generic
);
10836 /* Try to find the specific bindings for the symtrees in our target-list. */
10837 gcc_assert (p
->u
.generic
);
10838 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10839 if (!target
->specific
)
10841 gfc_typebound_proc
* overridden_tbp
;
10842 gfc_tbp_generic
* g
;
10843 const char* target_name
;
10845 target_name
= target
->specific_st
->name
;
10847 /* Defined for this type directly. */
10848 if (target
->specific_st
->n
.tb
&& !target
->specific_st
->n
.tb
->error
)
10850 target
->specific
= target
->specific_st
->n
.tb
;
10851 goto specific_found
;
10854 /* Look for an inherited specific binding. */
10857 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
10862 gcc_assert (inherited
->n
.tb
);
10863 target
->specific
= inherited
->n
.tb
;
10864 goto specific_found
;
10868 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10869 " at %L", target_name
, name
, &p
->where
);
10872 /* Once we've found the specific binding, check it is not ambiguous with
10873 other specifics already found or inherited for the same GENERIC. */
10875 gcc_assert (target
->specific
);
10877 /* This must really be a specific binding! */
10878 if (target
->specific
->is_generic
)
10880 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10881 " '%s' is GENERIC, too", name
, &p
->where
, target_name
);
10885 /* Check those already resolved on this type directly. */
10886 for (g
= p
->u
.generic
; g
; g
= g
->next
)
10887 if (g
!= target
&& g
->specific
10888 && check_generic_tbp_ambiguity (target
, g
, name
, p
->where
)
10892 /* Check for ambiguity with inherited specific targets. */
10893 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
10894 overridden_tbp
= overridden_tbp
->overridden
)
10895 if (overridden_tbp
->is_generic
)
10897 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
10899 gcc_assert (g
->specific
);
10900 if (check_generic_tbp_ambiguity (target
, g
,
10901 name
, p
->where
) == FAILURE
)
10907 /* If we attempt to "overwrite" a specific binding, this is an error. */
10908 if (p
->overridden
&& !p
->overridden
->is_generic
)
10910 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10911 " the same name", name
, &p
->where
);
10915 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10916 all must have the same attributes here. */
10917 first_target
= p
->u
.generic
->specific
->u
.specific
;
10918 gcc_assert (first_target
);
10919 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
10920 p
->function
= first_target
->n
.sym
->attr
.function
;
10926 /* Resolve a GENERIC procedure binding for a derived type. */
10929 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
10931 gfc_symbol
* super_type
;
10933 /* Find the overridden binding if any. */
10934 st
->n
.tb
->overridden
= NULL
;
10935 super_type
= gfc_get_derived_super_type (derived
);
10938 gfc_symtree
* overridden
;
10939 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
10942 if (overridden
&& overridden
->n
.tb
)
10943 st
->n
.tb
->overridden
= overridden
->n
.tb
;
10946 /* Resolve using worker function. */
10947 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
10951 /* Retrieve the target-procedure of an operator binding and do some checks in
10952 common for intrinsic and user-defined type-bound operators. */
10955 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
10957 gfc_symbol
* target_proc
;
10959 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
10960 target_proc
= target
->specific
->u
.specific
->n
.sym
;
10961 gcc_assert (target_proc
);
10963 /* All operator bindings must have a passed-object dummy argument. */
10964 if (target
->specific
->nopass
)
10966 gfc_error ("Type-bound operator at %L can't be NOPASS", &where
);
10970 return target_proc
;
10974 /* Resolve a type-bound intrinsic operator. */
10977 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
10978 gfc_typebound_proc
* p
)
10980 gfc_symbol
* super_type
;
10981 gfc_tbp_generic
* target
;
10983 /* If there's already an error here, do nothing (but don't fail again). */
10987 /* Operators should always be GENERIC bindings. */
10988 gcc_assert (p
->is_generic
);
10990 /* Look for an overridden binding. */
10991 super_type
= gfc_get_derived_super_type (derived
);
10992 if (super_type
&& super_type
->f2k_derived
)
10993 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
10996 p
->overridden
= NULL
;
10998 /* Resolve general GENERIC properties using worker function. */
10999 if (resolve_tb_generic_targets (super_type
, p
, gfc_op2string (op
)) == FAILURE
)
11002 /* Check the targets to be procedures of correct interface. */
11003 for (target
= p
->u
.generic
; target
; target
= target
->next
)
11005 gfc_symbol
* target_proc
;
11007 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
11011 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
11023 /* Resolve a type-bound user operator (tree-walker callback). */
11025 static gfc_symbol
* resolve_bindings_derived
;
11026 static gfc_try resolve_bindings_result
;
11028 static gfc_try
check_uop_procedure (gfc_symbol
* sym
, locus where
);
11031 resolve_typebound_user_op (gfc_symtree
* stree
)
11033 gfc_symbol
* super_type
;
11034 gfc_tbp_generic
* target
;
11036 gcc_assert (stree
&& stree
->n
.tb
);
11038 if (stree
->n
.tb
->error
)
11041 /* Operators should always be GENERIC bindings. */
11042 gcc_assert (stree
->n
.tb
->is_generic
);
11044 /* Find overridden procedure, if any. */
11045 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
11046 if (super_type
&& super_type
->f2k_derived
)
11048 gfc_symtree
* overridden
;
11049 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
11050 stree
->name
, true, NULL
);
11052 if (overridden
&& overridden
->n
.tb
)
11053 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
11056 stree
->n
.tb
->overridden
= NULL
;
11058 /* Resolve basically using worker function. */
11059 if (resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
)
11063 /* Check the targets to be functions of correct interface. */
11064 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
11066 gfc_symbol
* target_proc
;
11068 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
11072 if (check_uop_procedure (target_proc
, stree
->n
.tb
->where
) == FAILURE
)
11079 resolve_bindings_result
= FAILURE
;
11080 stree
->n
.tb
->error
= 1;
11084 /* Resolve the type-bound procedures for a derived type. */
11087 resolve_typebound_procedure (gfc_symtree
* stree
)
11091 gfc_symbol
* me_arg
;
11092 gfc_symbol
* super_type
;
11093 gfc_component
* comp
;
11095 gcc_assert (stree
);
11097 /* Undefined specific symbol from GENERIC target definition. */
11101 if (stree
->n
.tb
->error
)
11104 /* If this is a GENERIC binding, use that routine. */
11105 if (stree
->n
.tb
->is_generic
)
11107 if (resolve_typebound_generic (resolve_bindings_derived
, stree
)
11113 /* Get the target-procedure to check it. */
11114 gcc_assert (!stree
->n
.tb
->is_generic
);
11115 gcc_assert (stree
->n
.tb
->u
.specific
);
11116 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
11117 where
= stree
->n
.tb
->where
;
11119 /* Default access should already be resolved from the parser. */
11120 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
11122 /* It should be a module procedure or an external procedure with explicit
11123 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11124 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
11125 || (proc
->attr
.proc
!= PROC_MODULE
11126 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
11127 || (proc
->attr
.abstract
&& !stree
->n
.tb
->deferred
))
11129 gfc_error ("'%s' must be a module procedure or an external procedure with"
11130 " an explicit interface at %L", proc
->name
, &where
);
11133 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
11134 stree
->n
.tb
->function
= proc
->attr
.function
;
11136 /* Find the super-type of the current derived type. We could do this once and
11137 store in a global if speed is needed, but as long as not I believe this is
11138 more readable and clearer. */
11139 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
11141 /* If PASS, resolve and check arguments if not already resolved / loaded
11142 from a .mod file. */
11143 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
11145 if (stree
->n
.tb
->pass_arg
)
11147 gfc_formal_arglist
* i
;
11149 /* If an explicit passing argument name is given, walk the arg-list
11150 and look for it. */
11153 stree
->n
.tb
->pass_arg_num
= 1;
11154 for (i
= proc
->formal
; i
; i
= i
->next
)
11156 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
11161 ++stree
->n
.tb
->pass_arg_num
;
11166 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11168 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
11169 stree
->n
.tb
->pass_arg
);
11175 /* Otherwise, take the first one; there should in fact be at least
11177 stree
->n
.tb
->pass_arg_num
= 1;
11180 gfc_error ("Procedure '%s' with PASS at %L must have at"
11181 " least one argument", proc
->name
, &where
);
11184 me_arg
= proc
->formal
->sym
;
11187 /* Now check that the argument-type matches and the passed-object
11188 dummy argument is generally fine. */
11190 gcc_assert (me_arg
);
11192 if (me_arg
->ts
.type
!= BT_CLASS
)
11194 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11195 " at %L", proc
->name
, &where
);
11199 if (CLASS_DATA (me_arg
)->ts
.u
.derived
11200 != resolve_bindings_derived
)
11202 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11203 " the derived-type '%s'", me_arg
->name
, proc
->name
,
11204 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
11208 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
11209 if (CLASS_DATA (me_arg
)->as
&& CLASS_DATA (me_arg
)->as
->rank
> 0)
11211 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11212 " scalar", proc
->name
, &where
);
11215 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
11217 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11218 " be ALLOCATABLE", proc
->name
, &where
);
11221 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
11223 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11224 " be POINTER", proc
->name
, &where
);
11229 /* If we are extending some type, check that we don't override a procedure
11230 flagged NON_OVERRIDABLE. */
11231 stree
->n
.tb
->overridden
= NULL
;
11234 gfc_symtree
* overridden
;
11235 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
11236 stree
->name
, true, NULL
);
11238 if (overridden
&& overridden
->n
.tb
)
11239 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
11241 if (overridden
&& check_typebound_override (stree
, overridden
) == FAILURE
)
11245 /* See if there's a name collision with a component directly in this type. */
11246 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
11247 if (!strcmp (comp
->name
, stree
->name
))
11249 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11251 stree
->name
, &where
, resolve_bindings_derived
->name
);
11255 /* Try to find a name collision with an inherited component. */
11256 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true))
11258 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11259 " component of '%s'",
11260 stree
->name
, &where
, resolve_bindings_derived
->name
);
11264 stree
->n
.tb
->error
= 0;
11268 resolve_bindings_result
= FAILURE
;
11269 stree
->n
.tb
->error
= 1;
11274 resolve_typebound_procedures (gfc_symbol
* derived
)
11278 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
11281 resolve_bindings_derived
= derived
;
11282 resolve_bindings_result
= SUCCESS
;
11284 /* Make sure the vtab has been generated. */
11285 gfc_find_derived_vtab (derived
);
11287 if (derived
->f2k_derived
->tb_sym_root
)
11288 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
11289 &resolve_typebound_procedure
);
11291 if (derived
->f2k_derived
->tb_uop_root
)
11292 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
11293 &resolve_typebound_user_op
);
11295 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
11297 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
11298 if (p
&& resolve_typebound_intrinsic_op (derived
, (gfc_intrinsic_op
) op
,
11300 resolve_bindings_result
= FAILURE
;
11303 return resolve_bindings_result
;
11307 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11308 to give all identical derived types the same backend_decl. */
11310 add_dt_to_dt_list (gfc_symbol
*derived
)
11312 gfc_dt_list
*dt_list
;
11314 for (dt_list
= gfc_derived_types
; dt_list
; dt_list
= dt_list
->next
)
11315 if (derived
== dt_list
->derived
)
11318 dt_list
= gfc_get_dt_list ();
11319 dt_list
->next
= gfc_derived_types
;
11320 dt_list
->derived
= derived
;
11321 gfc_derived_types
= dt_list
;
11325 /* Ensure that a derived-type is really not abstract, meaning that every
11326 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11329 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
11334 if (ensure_not_abstract_walker (sub
, st
->left
) == FAILURE
)
11336 if (ensure_not_abstract_walker (sub
, st
->right
) == FAILURE
)
11339 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
11341 gfc_symtree
* overriding
;
11342 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
11345 gcc_assert (overriding
->n
.tb
);
11346 if (overriding
->n
.tb
->deferred
)
11348 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11349 " '%s' is DEFERRED and not overridden",
11350 sub
->name
, &sub
->declared_at
, st
->name
);
11359 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
11361 /* The algorithm used here is to recursively travel up the ancestry of sub
11362 and for each ancestor-type, check all bindings. If any of them is
11363 DEFERRED, look it up starting from sub and see if the found (overriding)
11364 binding is not DEFERRED.
11365 This is not the most efficient way to do this, but it should be ok and is
11366 clearer than something sophisticated. */
11368 gcc_assert (ancestor
&& !sub
->attr
.abstract
);
11370 if (!ancestor
->attr
.abstract
)
11373 /* Walk bindings of this ancestor. */
11374 if (ancestor
->f2k_derived
)
11377 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
11382 /* Find next ancestor type and recurse on it. */
11383 ancestor
= gfc_get_derived_super_type (ancestor
);
11385 return ensure_not_abstract (sub
, ancestor
);
11391 /* Resolve the components of a derived type. */
11394 resolve_fl_derived (gfc_symbol
*sym
)
11396 gfc_symbol
* super_type
;
11399 super_type
= gfc_get_derived_super_type (sym
);
11401 if (sym
->attr
.is_class
&& sym
->ts
.u
.derived
== NULL
)
11403 /* Fix up incomplete CLASS symbols. */
11404 gfc_component
*data
= gfc_find_component (sym
, "_data", true, true);
11405 gfc_component
*vptr
= gfc_find_component (sym
, "_vptr", true, true);
11406 if (vptr
->ts
.u
.derived
== NULL
)
11408 gfc_symbol
*vtab
= gfc_find_derived_vtab (data
->ts
.u
.derived
);
11410 vptr
->ts
.u
.derived
= vtab
->ts
.u
.derived
;
11415 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
11417 gfc_error ("As extending type '%s' at %L has a coarray component, "
11418 "parent type '%s' shall also have one", sym
->name
,
11419 &sym
->declared_at
, super_type
->name
);
11423 /* Ensure the extended type gets resolved before we do. */
11424 if (super_type
&& resolve_fl_derived (super_type
) == FAILURE
)
11427 /* An ABSTRACT type must be extensible. */
11428 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
11430 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11431 sym
->name
, &sym
->declared_at
);
11435 for (c
= sym
->components
; c
!= NULL
; c
= c
->next
)
11438 if (c
->attr
.codimension
/* FIXME: c->as check due to PR 43412. */
11439 && (!c
->attr
.allocatable
|| (c
->as
&& c
->as
->type
!= AS_DEFERRED
)))
11441 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11442 "deferred shape", c
->name
, &c
->loc
);
11447 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
11448 && c
->ts
.u
.derived
->ts
.is_iso_c
)
11450 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11451 "shall not be a coarray", c
->name
, &c
->loc
);
11456 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
11457 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
11458 || c
->attr
.allocatable
))
11460 gfc_error ("Component '%s' at %L with coarray component "
11461 "shall be a nonpointer, nonallocatable scalar",
11467 if (c
->attr
.contiguous
&& (!c
->attr
.dimension
|| !c
->attr
.pointer
))
11469 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11470 "is not an array pointer", c
->name
, &c
->loc
);
11474 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
11476 if (c
->ts
.interface
->attr
.procedure
&& !sym
->attr
.vtype
)
11477 gfc_error ("Interface '%s', used by procedure pointer component "
11478 "'%s' at %L, is declared in a later PROCEDURE statement",
11479 c
->ts
.interface
->name
, c
->name
, &c
->loc
);
11481 /* Get the attributes from the interface (now resolved). */
11482 if (c
->ts
.interface
->attr
.if_source
11483 || c
->ts
.interface
->attr
.intrinsic
)
11485 gfc_symbol
*ifc
= c
->ts
.interface
;
11487 if (ifc
->formal
&& !ifc
->formal_ns
)
11488 resolve_symbol (ifc
);
11490 if (ifc
->attr
.intrinsic
)
11491 resolve_intrinsic (ifc
, &ifc
->declared_at
);
11495 c
->ts
= ifc
->result
->ts
;
11496 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
11497 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
11498 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
11499 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
11504 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
11505 c
->attr
.pointer
= ifc
->attr
.pointer
;
11506 c
->attr
.dimension
= ifc
->attr
.dimension
;
11507 c
->as
= gfc_copy_array_spec (ifc
->as
);
11509 c
->ts
.interface
= ifc
;
11510 c
->attr
.function
= ifc
->attr
.function
;
11511 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
11512 gfc_copy_formal_args_ppc (c
, ifc
);
11514 c
->attr
.pure
= ifc
->attr
.pure
;
11515 c
->attr
.elemental
= ifc
->attr
.elemental
;
11516 c
->attr
.recursive
= ifc
->attr
.recursive
;
11517 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
11518 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
11519 /* Replace symbols in array spec. */
11523 for (i
= 0; i
< c
->as
->rank
; i
++)
11525 gfc_expr_replace_comp (c
->as
->lower
[i
], c
);
11526 gfc_expr_replace_comp (c
->as
->upper
[i
], c
);
11529 /* Copy char length. */
11530 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
11532 gfc_charlen
*cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
11533 gfc_expr_replace_comp (cl
->length
, c
);
11534 if (cl
->length
&& !cl
->resolved
11535 && gfc_resolve_expr (cl
->length
) == FAILURE
)
11540 else if (!sym
->attr
.vtype
&& c
->ts
.interface
->name
[0] != '\0')
11542 gfc_error ("Interface '%s' of procedure pointer component "
11543 "'%s' at %L must be explicit", c
->ts
.interface
->name
,
11548 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
11550 /* Since PPCs are not implicitly typed, a PPC without an explicit
11551 interface must be a subroutine. */
11552 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
11555 /* Procedure pointer components: Check PASS arg. */
11556 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0
11557 && !sym
->attr
.vtype
)
11559 gfc_symbol
* me_arg
;
11561 if (c
->tb
->pass_arg
)
11563 gfc_formal_arglist
* i
;
11565 /* If an explicit passing argument name is given, walk the arg-list
11566 and look for it. */
11569 c
->tb
->pass_arg_num
= 1;
11570 for (i
= c
->formal
; i
; i
= i
->next
)
11572 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
11577 c
->tb
->pass_arg_num
++;
11582 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11583 "at %L has no argument '%s'", c
->name
,
11584 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
11591 /* Otherwise, take the first one; there should in fact be at least
11593 c
->tb
->pass_arg_num
= 1;
11596 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11597 "must have at least one argument",
11602 me_arg
= c
->formal
->sym
;
11605 /* Now check that the argument-type matches. */
11606 gcc_assert (me_arg
);
11607 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
11608 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
11609 || (me_arg
->ts
.type
== BT_CLASS
11610 && CLASS_DATA (me_arg
)->ts
.u
.derived
!= sym
))
11612 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11613 " the derived type '%s'", me_arg
->name
, c
->name
,
11614 me_arg
->name
, &c
->loc
, sym
->name
);
11619 /* Check for C453. */
11620 if (me_arg
->attr
.dimension
)
11622 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11623 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
11629 if (me_arg
->attr
.pointer
)
11631 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11632 "may not have the POINTER attribute", me_arg
->name
,
11633 c
->name
, me_arg
->name
, &c
->loc
);
11638 if (me_arg
->attr
.allocatable
)
11640 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11641 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
11642 me_arg
->name
, &c
->loc
);
11647 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
11648 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11649 " at %L", c
->name
, &c
->loc
);
11653 /* Check type-spec if this is not the parent-type component. */
11654 if ((!sym
->attr
.extension
|| c
!= sym
->components
) && !sym
->attr
.vtype
11655 && resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
) == FAILURE
)
11658 /* If this type is an extension, set the accessibility of the parent
11660 if (super_type
&& c
== sym
->components
11661 && strcmp (super_type
->name
, c
->name
) == 0)
11662 c
->attr
.access
= super_type
->attr
.access
;
11664 /* If this type is an extension, see if this component has the same name
11665 as an inherited type-bound procedure. */
11666 if (super_type
&& !sym
->attr
.is_class
11667 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
11669 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11670 " inherited type-bound procedure",
11671 c
->name
, sym
->name
, &c
->loc
);
11675 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
11676 && !c
->ts
.deferred
)
11678 if (c
->ts
.u
.cl
->length
== NULL
11679 || (resolve_charlen (c
->ts
.u
.cl
) == FAILURE
)
11680 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
11682 gfc_error ("Character length of component '%s' needs to "
11683 "be a constant specification expression at %L",
11685 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
11690 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
11691 && !c
->attr
.pointer
&& !c
->attr
.allocatable
)
11693 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11694 "length must be a POINTER or ALLOCATABLE",
11695 c
->name
, sym
->name
, &c
->loc
);
11699 if (c
->ts
.type
== BT_DERIVED
11700 && sym
->component_access
!= ACCESS_PRIVATE
11701 && gfc_check_symbol_access (sym
)
11702 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
11703 && !c
->ts
.u
.derived
->attr
.use_assoc
11704 && !gfc_check_symbol_access (c
->ts
.u
.derived
)
11705 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: the component '%s' "
11706 "is a PRIVATE type and cannot be a component of "
11707 "'%s', which is PUBLIC at %L", c
->name
,
11708 sym
->name
, &sym
->declared_at
) == FAILURE
)
11711 if ((sym
->attr
.sequence
|| sym
->attr
.is_bind_c
) && c
->ts
.type
== BT_CLASS
)
11713 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11714 "type %s", c
->name
, &c
->loc
, sym
->name
);
11718 if (sym
->attr
.sequence
)
11720 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
11722 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11723 "not have the SEQUENCE attribute",
11724 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
11729 if (!sym
->attr
.is_class
&& c
->ts
.type
== BT_DERIVED
&& !sym
->attr
.vtype
11730 && c
->attr
.pointer
&& c
->ts
.u
.derived
->components
== NULL
11731 && !c
->ts
.u
.derived
->attr
.zero_comp
)
11733 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11734 "that has not been declared", c
->name
, sym
->name
,
11739 if (c
->ts
.type
== BT_CLASS
&& CLASS_DATA (c
)->attr
.class_pointer
11740 && CLASS_DATA (c
)->ts
.u
.derived
->components
== NULL
11741 && !CLASS_DATA (c
)->ts
.u
.derived
->attr
.zero_comp
)
11743 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11744 "that has not been declared", c
->name
, sym
->name
,
11750 if (c
->ts
.type
== BT_CLASS
11751 && !(CLASS_DATA (c
)->attr
.class_pointer
11752 || CLASS_DATA (c
)->attr
.allocatable
))
11754 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11755 "or pointer", c
->name
, &c
->loc
);
11759 /* Ensure that all the derived type components are put on the
11760 derived type list; even in formal namespaces, where derived type
11761 pointer components might not have been declared. */
11762 if (c
->ts
.type
== BT_DERIVED
11764 && c
->ts
.u
.derived
->components
11766 && sym
!= c
->ts
.u
.derived
)
11767 add_dt_to_dt_list (c
->ts
.u
.derived
);
11769 if (gfc_resolve_array_spec (c
->as
, !(c
->attr
.pointer
11770 || c
->attr
.proc_pointer
11771 || c
->attr
.allocatable
)) == FAILURE
)
11775 /* Resolve the type-bound procedures. */
11776 if (resolve_typebound_procedures (sym
) == FAILURE
)
11779 /* Resolve the finalizer procedures. */
11780 if (gfc_resolve_finalizers (sym
) == FAILURE
)
11783 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11784 all DEFERRED bindings are overridden. */
11785 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
11786 && !sym
->attr
.is_class
11787 && ensure_not_abstract (sym
, super_type
) == FAILURE
)
11790 /* Add derived type to the derived type list. */
11791 add_dt_to_dt_list (sym
);
11798 resolve_fl_namelist (gfc_symbol
*sym
)
11803 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11805 /* Check again, the check in match only works if NAMELIST comes
11807 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SIZE
)
11809 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
11810 "allowed", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11814 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
11815 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST array "
11816 "object '%s' with assumed shape in namelist "
11817 "'%s' at %L", nl
->sym
->name
, sym
->name
,
11818 &sym
->declared_at
) == FAILURE
)
11821 if (is_non_constant_shape_array (nl
->sym
)
11822 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST array "
11823 "object '%s' with nonconstant shape in namelist "
11824 "'%s' at %L", nl
->sym
->name
, sym
->name
,
11825 &sym
->declared_at
) == FAILURE
)
11828 if (nl
->sym
->ts
.type
== BT_CHARACTER
11829 && (nl
->sym
->ts
.u
.cl
->length
== NULL
11830 || !gfc_is_constant_expr (nl
->sym
->ts
.u
.cl
->length
))
11831 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST object "
11832 "'%s' with nonconstant character length in "
11833 "namelist '%s' at %L", nl
->sym
->name
, sym
->name
,
11834 &sym
->declared_at
) == FAILURE
)
11837 /* FIXME: Once UDDTIO is implemented, the following can be
11839 if (nl
->sym
->ts
.type
== BT_CLASS
)
11841 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
11842 "polymorphic and requires a defined input/output "
11843 "procedure", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11847 if (nl
->sym
->ts
.type
== BT_DERIVED
11848 && (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
11849 || nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
))
11851 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST object "
11852 "'%s' in namelist '%s' at %L with ALLOCATABLE "
11853 "or POINTER components", nl
->sym
->name
,
11854 sym
->name
, &sym
->declared_at
) == FAILURE
)
11857 /* FIXME: Once UDDTIO is implemented, the following can be
11859 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
11860 "ALLOCATABLE or POINTER components and thus requires "
11861 "a defined input/output procedure", nl
->sym
->name
,
11862 sym
->name
, &sym
->declared_at
);
11867 /* Reject PRIVATE objects in a PUBLIC namelist. */
11868 if (gfc_check_symbol_access (sym
))
11870 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11872 if (!nl
->sym
->attr
.use_assoc
11873 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
11874 && !gfc_check_symbol_access (nl
->sym
))
11876 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11877 "cannot be member of PUBLIC namelist '%s' at %L",
11878 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11882 /* Types with private components that came here by USE-association. */
11883 if (nl
->sym
->ts
.type
== BT_DERIVED
11884 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
11886 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11887 "components and cannot be member of namelist '%s' at %L",
11888 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11892 /* Types with private components that are defined in the same module. */
11893 if (nl
->sym
->ts
.type
== BT_DERIVED
11894 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
11895 && nl
->sym
->ts
.u
.derived
->attr
.private_comp
)
11897 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11898 "cannot be a member of PUBLIC namelist '%s' at %L",
11899 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11906 /* 14.1.2 A module or internal procedure represent local entities
11907 of the same type as a namelist member and so are not allowed. */
11908 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11910 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
11913 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
11914 if ((nl
->sym
== sym
->ns
->proc_name
)
11916 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
11920 if (nl
->sym
&& nl
->sym
->name
)
11921 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
11922 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
11924 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11925 "attribute in '%s' at %L", nlsym
->name
,
11926 &sym
->declared_at
);
11936 resolve_fl_parameter (gfc_symbol
*sym
)
11938 /* A parameter array's shape needs to be constant. */
11939 if (sym
->as
!= NULL
11940 && (sym
->as
->type
== AS_DEFERRED
11941 || is_non_constant_shape_array (sym
)))
11943 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11944 "or of deferred shape", sym
->name
, &sym
->declared_at
);
11948 /* Make sure a parameter that has been implicitly typed still
11949 matches the implicit type, since PARAMETER statements can precede
11950 IMPLICIT statements. */
11951 if (sym
->attr
.implicit_type
11952 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
11955 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11956 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
11960 /* Make sure the types of derived parameters are consistent. This
11961 type checking is deferred until resolution because the type may
11962 refer to a derived type from the host. */
11963 if (sym
->ts
.type
== BT_DERIVED
11964 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
11966 gfc_error ("Incompatible derived type in PARAMETER at %L",
11967 &sym
->value
->where
);
11974 /* Do anything necessary to resolve a symbol. Right now, we just
11975 assume that an otherwise unknown symbol is a variable. This sort
11976 of thing commonly happens for symbols in module. */
11979 resolve_symbol (gfc_symbol
*sym
)
11981 int check_constant
, mp_flag
;
11982 gfc_symtree
*symtree
;
11983 gfc_symtree
*this_symtree
;
11987 if (sym
->attr
.flavor
== FL_UNKNOWN
)
11990 /* If we find that a flavorless symbol is an interface in one of the
11991 parent namespaces, find its symtree in this namespace, free the
11992 symbol and set the symtree to point to the interface symbol. */
11993 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
11995 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
11996 if (symtree
&& (symtree
->n
.sym
->generic
||
11997 (symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
11998 && sym
->ns
->construct_entities
)))
12000 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
12002 gfc_release_symbol (sym
);
12003 symtree
->n
.sym
->refs
++;
12004 this_symtree
->n
.sym
= symtree
->n
.sym
;
12009 /* Otherwise give it a flavor according to such attributes as
12011 if (sym
->attr
.external
== 0 && sym
->attr
.intrinsic
== 0)
12012 sym
->attr
.flavor
= FL_VARIABLE
;
12015 sym
->attr
.flavor
= FL_PROCEDURE
;
12016 if (sym
->attr
.dimension
)
12017 sym
->attr
.function
= 1;
12021 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
12022 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
12024 if (sym
->attr
.procedure
&& sym
->ts
.interface
12025 && sym
->attr
.if_source
!= IFSRC_DECL
12026 && resolve_procedure_interface (sym
) == FAILURE
)
12029 if (sym
->attr
.is_protected
&& !sym
->attr
.proc_pointer
12030 && (sym
->attr
.procedure
|| sym
->attr
.external
))
12032 if (sym
->attr
.external
)
12033 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12034 "at %L", &sym
->declared_at
);
12036 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12037 "at %L", &sym
->declared_at
);
12044 if (sym
->attr
.contiguous
12045 && (!sym
->attr
.dimension
|| (sym
->as
->type
!= AS_ASSUMED_SHAPE
12046 && !sym
->attr
.pointer
)))
12048 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12049 "array pointer or an assumed-shape array", sym
->name
,
12050 &sym
->declared_at
);
12054 if (sym
->attr
.flavor
== FL_DERIVED
&& resolve_fl_derived (sym
) == FAILURE
)
12057 /* Symbols that are module procedures with results (functions) have
12058 the types and array specification copied for type checking in
12059 procedures that call them, as well as for saving to a module
12060 file. These symbols can't stand the scrutiny that their results
12062 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
12064 /* Make sure that the intrinsic is consistent with its internal
12065 representation. This needs to be done before assigning a default
12066 type to avoid spurious warnings. */
12067 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
12068 && resolve_intrinsic (sym
, &sym
->declared_at
) == FAILURE
)
12071 /* Resolve associate names. */
12073 resolve_assoc_var (sym
, true);
12075 /* Assign default type to symbols that need one and don't have one. */
12076 if (sym
->ts
.type
== BT_UNKNOWN
)
12078 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
12079 gfc_set_default_type (sym
, 1, NULL
);
12081 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
12082 && !sym
->attr
.function
&& !sym
->attr
.subroutine
12083 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
12084 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
12086 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
12088 /* The specific case of an external procedure should emit an error
12089 in the case that there is no implicit type. */
12091 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
12094 /* Result may be in another namespace. */
12095 resolve_symbol (sym
->result
);
12097 if (!sym
->result
->attr
.proc_pointer
)
12099 sym
->ts
= sym
->result
->ts
;
12100 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
12101 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
12102 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
12103 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
12104 sym
->attr
.contiguous
= sym
->result
->attr
.contiguous
;
12110 /* Assumed size arrays and assumed shape arrays must be dummy
12111 arguments. Array-spec's of implied-shape should have been resolved to
12112 AS_EXPLICIT already. */
12116 gcc_assert (sym
->as
->type
!= AS_IMPLIED_SHAPE
);
12117 if (((sym
->as
->type
== AS_ASSUMED_SIZE
&& !sym
->as
->cp_was_assumed
)
12118 || sym
->as
->type
== AS_ASSUMED_SHAPE
)
12119 && sym
->attr
.dummy
== 0)
12121 if (sym
->as
->type
== AS_ASSUMED_SIZE
)
12122 gfc_error ("Assumed size array at %L must be a dummy argument",
12123 &sym
->declared_at
);
12125 gfc_error ("Assumed shape array at %L must be a dummy argument",
12126 &sym
->declared_at
);
12131 /* Make sure symbols with known intent or optional are really dummy
12132 variable. Because of ENTRY statement, this has to be deferred
12133 until resolution time. */
12135 if (!sym
->attr
.dummy
12136 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
12138 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
12142 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
12144 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12145 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
12149 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
12151 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
12152 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
12154 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12155 "attribute must have constant length",
12156 sym
->name
, &sym
->declared_at
);
12160 if (sym
->ts
.is_c_interop
12161 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
12163 gfc_error ("C interoperable character dummy variable '%s' at %L "
12164 "with VALUE attribute must have length one",
12165 sym
->name
, &sym
->declared_at
);
12170 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12171 do this for something that was implicitly typed because that is handled
12172 in gfc_set_default_type. Handle dummy arguments and procedure
12173 definitions separately. Also, anything that is use associated is not
12174 handled here but instead is handled in the module it is declared in.
12175 Finally, derived type definitions are allowed to be BIND(C) since that
12176 only implies that they're interoperable, and they are checked fully for
12177 interoperability when a variable is declared of that type. */
12178 if (sym
->attr
.is_bind_c
&& sym
->attr
.implicit_type
== 0 &&
12179 sym
->attr
.use_assoc
== 0 && sym
->attr
.dummy
== 0 &&
12180 sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.flavor
!= FL_DERIVED
)
12182 gfc_try t
= SUCCESS
;
12184 /* First, make sure the variable is declared at the
12185 module-level scope (J3/04-007, Section 15.3). */
12186 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
12187 sym
->attr
.in_common
== 0)
12189 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12190 "is neither a COMMON block nor declared at the "
12191 "module level scope", sym
->name
, &(sym
->declared_at
));
12194 else if (sym
->common_head
!= NULL
)
12196 t
= verify_com_block_vars_c_interop (sym
->common_head
);
12200 /* If type() declaration, we need to verify that the components
12201 of the given type are all C interoperable, etc. */
12202 if (sym
->ts
.type
== BT_DERIVED
&&
12203 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
12205 /* Make sure the user marked the derived type as BIND(C). If
12206 not, call the verify routine. This could print an error
12207 for the derived type more than once if multiple variables
12208 of that type are declared. */
12209 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
12210 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
12214 /* Verify the variable itself as C interoperable if it
12215 is BIND(C). It is not possible for this to succeed if
12216 the verify_bind_c_derived_type failed, so don't have to handle
12217 any error returned by verify_bind_c_derived_type. */
12218 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
12219 sym
->common_block
);
12224 /* clear the is_bind_c flag to prevent reporting errors more than
12225 once if something failed. */
12226 sym
->attr
.is_bind_c
= 0;
12231 /* If a derived type symbol has reached this point, without its
12232 type being declared, we have an error. Notice that most
12233 conditions that produce undefined derived types have already
12234 been dealt with. However, the likes of:
12235 implicit type(t) (t) ..... call foo (t) will get us here if
12236 the type is not declared in the scope of the implicit
12237 statement. Change the type to BT_UNKNOWN, both because it is so
12238 and to prevent an ICE. */
12239 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->components
== NULL
12240 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
12242 gfc_error ("The derived type '%s' at %L is of type '%s', "
12243 "which has not been defined", sym
->name
,
12244 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
12245 sym
->ts
.type
= BT_UNKNOWN
;
12249 /* Make sure that the derived type has been resolved and that the
12250 derived type is visible in the symbol's namespace, if it is a
12251 module function and is not PRIVATE. */
12252 if (sym
->ts
.type
== BT_DERIVED
12253 && sym
->ts
.u
.derived
->attr
.use_assoc
12254 && sym
->ns
->proc_name
12255 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
12259 if (resolve_fl_derived (sym
->ts
.u
.derived
) == FAILURE
)
12262 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 1, &ds
);
12263 if (!ds
&& sym
->attr
.function
&& gfc_check_symbol_access (sym
))
12265 symtree
= gfc_new_symtree (&sym
->ns
->sym_root
,
12266 sym
->ts
.u
.derived
->name
);
12267 symtree
->n
.sym
= sym
->ts
.u
.derived
;
12268 sym
->ts
.u
.derived
->refs
++;
12272 /* Unless the derived-type declaration is use associated, Fortran 95
12273 does not allow public entries of private derived types.
12274 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12275 161 in 95-006r3. */
12276 if (sym
->ts
.type
== BT_DERIVED
12277 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12278 && !sym
->ts
.u
.derived
->attr
.use_assoc
12279 && gfc_check_symbol_access (sym
)
12280 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
12281 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC %s '%s' at %L "
12282 "of PRIVATE derived type '%s'",
12283 (sym
->attr
.flavor
== FL_PARAMETER
) ? "parameter"
12284 : "variable", sym
->name
, &sym
->declared_at
,
12285 sym
->ts
.u
.derived
->name
) == FAILURE
)
12288 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12289 default initialization is defined (5.1.2.4.4). */
12290 if (sym
->ts
.type
== BT_DERIVED
12292 && sym
->attr
.intent
== INTENT_OUT
12294 && sym
->as
->type
== AS_ASSUMED_SIZE
)
12296 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
12298 if (c
->initializer
)
12300 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12301 "ASSUMED SIZE and so cannot have a default initializer",
12302 sym
->name
, &sym
->declared_at
);
12309 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12310 || sym
->attr
.codimension
)
12311 && sym
->attr
.result
)
12312 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12313 "a coarray component", sym
->name
, &sym
->declared_at
);
12316 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
12317 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
12318 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12319 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
12322 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
12323 && (sym
->attr
.codimension
|| sym
->attr
.pointer
|| sym
->attr
.dimension
12324 || sym
->attr
.allocatable
))
12325 gfc_error ("Variable '%s' at %L with coarray component "
12326 "shall be a nonpointer, nonallocatable scalar",
12327 sym
->name
, &sym
->declared_at
);
12329 /* F2008, C526. The function-result case was handled above. */
12330 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12331 || sym
->attr
.codimension
)
12332 && !(sym
->attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
12333 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12334 || sym
->ns
->proc_name
->attr
.is_main_program
12335 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
12336 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
12337 "component and is not ALLOCATABLE, SAVE nor a "
12338 "dummy argument", sym
->name
, &sym
->declared_at
);
12339 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12340 else if (sym
->attr
.codimension
&& !sym
->attr
.allocatable
12341 && sym
->as
&& sym
->as
->cotype
== AS_DEFERRED
)
12342 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12343 "deferred shape", sym
->name
, &sym
->declared_at
);
12344 else if (sym
->attr
.codimension
&& sym
->attr
.allocatable
12345 && (sym
->as
->type
!= AS_DEFERRED
|| sym
->as
->cotype
!= AS_DEFERRED
))
12346 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12347 "deferred shape", sym
->name
, &sym
->declared_at
);
12351 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12352 || (sym
->attr
.codimension
&& sym
->attr
.allocatable
))
12353 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
12354 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12355 "allocatable coarray or have coarray components",
12356 sym
->name
, &sym
->declared_at
);
12358 if (sym
->attr
.codimension
&& sym
->attr
.dummy
12359 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
12360 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12361 "procedure '%s'", sym
->name
, &sym
->declared_at
,
12362 sym
->ns
->proc_name
->name
);
12364 switch (sym
->attr
.flavor
)
12367 if (resolve_fl_variable (sym
, mp_flag
) == FAILURE
)
12372 if (resolve_fl_procedure (sym
, mp_flag
) == FAILURE
)
12377 if (resolve_fl_namelist (sym
) == FAILURE
)
12382 if (resolve_fl_parameter (sym
) == FAILURE
)
12390 /* Resolve array specifier. Check as well some constraints
12391 on COMMON blocks. */
12393 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
12395 /* Set the formal_arg_flag so that check_conflict will not throw
12396 an error for host associated variables in the specification
12397 expression for an array_valued function. */
12398 if (sym
->attr
.function
&& sym
->as
)
12399 formal_arg_flag
= 1;
12401 gfc_resolve_array_spec (sym
->as
, check_constant
);
12403 formal_arg_flag
= 0;
12405 /* Resolve formal namespaces. */
12406 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
12407 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
12408 gfc_resolve (sym
->formal_ns
);
12410 /* Make sure the formal namespace is present. */
12411 if (sym
->formal
&& !sym
->formal_ns
)
12413 gfc_formal_arglist
*formal
= sym
->formal
;
12414 while (formal
&& !formal
->sym
)
12415 formal
= formal
->next
;
12419 sym
->formal_ns
= formal
->sym
->ns
;
12420 sym
->formal_ns
->refs
++;
12424 /* Check threadprivate restrictions. */
12425 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
&& !sym
->ns
->save_all
12426 && (!sym
->attr
.in_common
12427 && sym
->module
== NULL
12428 && (sym
->ns
->proc_name
== NULL
12429 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
12430 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
12432 /* If we have come this far we can apply default-initializers, as
12433 described in 14.7.5, to those variables that have not already
12434 been assigned one. */
12435 if (sym
->ts
.type
== BT_DERIVED
12436 && sym
->ns
== gfc_current_ns
12438 && !sym
->attr
.allocatable
12439 && !sym
->attr
.alloc_comp
)
12441 symbol_attribute
*a
= &sym
->attr
;
12443 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
12444 && !a
->in_common
&& !a
->use_assoc
12445 && (a
->referenced
|| a
->result
)
12446 && !(a
->function
&& sym
!= sym
->result
))
12447 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
12448 apply_default_init (sym
);
12451 if (sym
->ts
.type
== BT_CLASS
&& sym
->ns
== gfc_current_ns
12452 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
12453 && !CLASS_DATA (sym
)->attr
.class_pointer
12454 && !CLASS_DATA (sym
)->attr
.allocatable
)
12455 apply_default_init (sym
);
12457 /* If this symbol has a type-spec, check it. */
12458 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
12459 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
12460 if (resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
)
12466 /************* Resolve DATA statements *************/
12470 gfc_data_value
*vnode
;
12476 /* Advance the values structure to point to the next value in the data list. */
12479 next_data_value (void)
12481 while (mpz_cmp_ui (values
.left
, 0) == 0)
12484 if (values
.vnode
->next
== NULL
)
12487 values
.vnode
= values
.vnode
->next
;
12488 mpz_set (values
.left
, values
.vnode
->repeat
);
12496 check_data_variable (gfc_data_variable
*var
, locus
*where
)
12502 ar_type mark
= AR_UNKNOWN
;
12504 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
12510 if (gfc_resolve_expr (var
->expr
) == FAILURE
)
12514 mpz_init_set_si (offset
, 0);
12517 if (e
->expr_type
!= EXPR_VARIABLE
)
12518 gfc_internal_error ("check_data_variable(): Bad expression");
12520 sym
= e
->symtree
->n
.sym
;
12522 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
12524 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12525 sym
->name
, &sym
->declared_at
);
12528 if (e
->ref
== NULL
&& sym
->as
)
12530 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12531 " declaration", sym
->name
, where
);
12535 has_pointer
= sym
->attr
.pointer
;
12537 if (gfc_is_coindexed (e
))
12539 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym
->name
,
12544 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12546 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
12550 && ref
->type
== REF_ARRAY
12551 && ref
->u
.ar
.type
!= AR_FULL
)
12553 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12554 "be a full array", sym
->name
, where
);
12559 if (e
->rank
== 0 || has_pointer
)
12561 mpz_init_set_ui (size
, 1);
12568 /* Find the array section reference. */
12569 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12571 if (ref
->type
!= REF_ARRAY
)
12573 if (ref
->u
.ar
.type
== AR_ELEMENT
)
12579 /* Set marks according to the reference pattern. */
12580 switch (ref
->u
.ar
.type
)
12588 /* Get the start position of array section. */
12589 gfc_get_section_index (ar
, section_index
, &offset
);
12594 gcc_unreachable ();
12597 if (gfc_array_size (e
, &size
) == FAILURE
)
12599 gfc_error ("Nonconstant array section at %L in DATA statement",
12601 mpz_clear (offset
);
12608 while (mpz_cmp_ui (size
, 0) > 0)
12610 if (next_data_value () == FAILURE
)
12612 gfc_error ("DATA statement at %L has more variables than values",
12618 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
12622 /* If we have more than one element left in the repeat count,
12623 and we have more than one element left in the target variable,
12624 then create a range assignment. */
12625 /* FIXME: Only done for full arrays for now, since array sections
12627 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
12628 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
12632 if (mpz_cmp (size
, values
.left
) >= 0)
12634 mpz_init_set (range
, values
.left
);
12635 mpz_sub (size
, size
, values
.left
);
12636 mpz_set_ui (values
.left
, 0);
12640 mpz_init_set (range
, size
);
12641 mpz_sub (values
.left
, values
.left
, size
);
12642 mpz_set_ui (size
, 0);
12645 t
= gfc_assign_data_value_range (var
->expr
, values
.vnode
->expr
,
12648 mpz_add (offset
, offset
, range
);
12655 /* Assign initial value to symbol. */
12658 mpz_sub_ui (values
.left
, values
.left
, 1);
12659 mpz_sub_ui (size
, size
, 1);
12661 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
, offset
);
12665 if (mark
== AR_FULL
)
12666 mpz_add_ui (offset
, offset
, 1);
12668 /* Modify the array section indexes and recalculate the offset
12669 for next element. */
12670 else if (mark
== AR_SECTION
)
12671 gfc_advance_section (section_index
, ar
, &offset
);
12675 if (mark
== AR_SECTION
)
12677 for (i
= 0; i
< ar
->dimen
; i
++)
12678 mpz_clear (section_index
[i
]);
12682 mpz_clear (offset
);
12688 static gfc_try
traverse_data_var (gfc_data_variable
*, locus
*);
12690 /* Iterate over a list of elements in a DATA statement. */
12693 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
12696 iterator_stack frame
;
12697 gfc_expr
*e
, *start
, *end
, *step
;
12698 gfc_try retval
= SUCCESS
;
12700 mpz_init (frame
.value
);
12703 start
= gfc_copy_expr (var
->iter
.start
);
12704 end
= gfc_copy_expr (var
->iter
.end
);
12705 step
= gfc_copy_expr (var
->iter
.step
);
12707 if (gfc_simplify_expr (start
, 1) == FAILURE
12708 || start
->expr_type
!= EXPR_CONSTANT
)
12710 gfc_error ("start of implied-do loop at %L could not be "
12711 "simplified to a constant value", &start
->where
);
12715 if (gfc_simplify_expr (end
, 1) == FAILURE
12716 || end
->expr_type
!= EXPR_CONSTANT
)
12718 gfc_error ("end of implied-do loop at %L could not be "
12719 "simplified to a constant value", &start
->where
);
12723 if (gfc_simplify_expr (step
, 1) == FAILURE
12724 || step
->expr_type
!= EXPR_CONSTANT
)
12726 gfc_error ("step of implied-do loop at %L could not be "
12727 "simplified to a constant value", &start
->where
);
12732 mpz_set (trip
, end
->value
.integer
);
12733 mpz_sub (trip
, trip
, start
->value
.integer
);
12734 mpz_add (trip
, trip
, step
->value
.integer
);
12736 mpz_div (trip
, trip
, step
->value
.integer
);
12738 mpz_set (frame
.value
, start
->value
.integer
);
12740 frame
.prev
= iter_stack
;
12741 frame
.variable
= var
->iter
.var
->symtree
;
12742 iter_stack
= &frame
;
12744 while (mpz_cmp_ui (trip
, 0) > 0)
12746 if (traverse_data_var (var
->list
, where
) == FAILURE
)
12752 e
= gfc_copy_expr (var
->expr
);
12753 if (gfc_simplify_expr (e
, 1) == FAILURE
)
12760 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
12762 mpz_sub_ui (trip
, trip
, 1);
12766 mpz_clear (frame
.value
);
12769 gfc_free_expr (start
);
12770 gfc_free_expr (end
);
12771 gfc_free_expr (step
);
12773 iter_stack
= frame
.prev
;
12778 /* Type resolve variables in the variable list of a DATA statement. */
12781 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
12785 for (; var
; var
= var
->next
)
12787 if (var
->expr
== NULL
)
12788 t
= traverse_data_list (var
, where
);
12790 t
= check_data_variable (var
, where
);
12800 /* Resolve the expressions and iterators associated with a data statement.
12801 This is separate from the assignment checking because data lists should
12802 only be resolved once. */
12805 resolve_data_variables (gfc_data_variable
*d
)
12807 for (; d
; d
= d
->next
)
12809 if (d
->list
== NULL
)
12811 if (gfc_resolve_expr (d
->expr
) == FAILURE
)
12816 if (gfc_resolve_iterator (&d
->iter
, false) == FAILURE
)
12819 if (resolve_data_variables (d
->list
) == FAILURE
)
12828 /* Resolve a single DATA statement. We implement this by storing a pointer to
12829 the value list into static variables, and then recursively traversing the
12830 variables list, expanding iterators and such. */
12833 resolve_data (gfc_data
*d
)
12836 if (resolve_data_variables (d
->var
) == FAILURE
)
12839 values
.vnode
= d
->value
;
12840 if (d
->value
== NULL
)
12841 mpz_set_ui (values
.left
, 0);
12843 mpz_set (values
.left
, d
->value
->repeat
);
12845 if (traverse_data_var (d
->var
, &d
->where
) == FAILURE
)
12848 /* At this point, we better not have any values left. */
12850 if (next_data_value () == SUCCESS
)
12851 gfc_error ("DATA statement at %L has more values than variables",
12856 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12857 accessed by host or use association, is a dummy argument to a pure function,
12858 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12859 is storage associated with any such variable, shall not be used in the
12860 following contexts: (clients of this function). */
12862 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12863 procedure. Returns zero if assignment is OK, nonzero if there is a
12866 gfc_impure_variable (gfc_symbol
*sym
)
12871 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
12874 /* Check if the symbol's ns is inside the pure procedure. */
12875 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12879 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
12883 proc
= sym
->ns
->proc_name
;
12884 if (sym
->attr
.dummy
&& gfc_pure (proc
)
12885 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
12887 proc
->attr
.function
))
12890 /* TODO: Sort out what can be storage associated, if anything, and include
12891 it here. In principle equivalences should be scanned but it does not
12892 seem to be possible to storage associate an impure variable this way. */
12897 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12898 current namespace is inside a pure procedure. */
12901 gfc_pure (gfc_symbol
*sym
)
12903 symbol_attribute attr
;
12908 /* Check if the current namespace or one of its parents
12909 belongs to a pure procedure. */
12910 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12912 sym
= ns
->proc_name
;
12916 if (attr
.flavor
== FL_PROCEDURE
&& attr
.pure
)
12924 return attr
.flavor
== FL_PROCEDURE
&& attr
.pure
;
12928 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
12929 checks if the current namespace is implicitly pure. Note that this
12930 function returns false for a PURE procedure. */
12933 gfc_implicit_pure (gfc_symbol
*sym
)
12935 symbol_attribute attr
;
12939 /* Check if the current namespace is implicit_pure. */
12940 sym
= gfc_current_ns
->proc_name
;
12944 if (attr
.flavor
== FL_PROCEDURE
12945 && attr
.implicit_pure
&& !attr
.pure
)
12952 return attr
.flavor
== FL_PROCEDURE
&& attr
.implicit_pure
&& !attr
.pure
;
12956 /* Test whether the current procedure is elemental or not. */
12959 gfc_elemental (gfc_symbol
*sym
)
12961 symbol_attribute attr
;
12964 sym
= gfc_current_ns
->proc_name
;
12969 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
12973 /* Warn about unused labels. */
12976 warn_unused_fortran_label (gfc_st_label
*label
)
12981 warn_unused_fortran_label (label
->left
);
12983 if (label
->defined
== ST_LABEL_UNKNOWN
)
12986 switch (label
->referenced
)
12988 case ST_LABEL_UNKNOWN
:
12989 gfc_warning ("Label %d at %L defined but not used", label
->value
,
12993 case ST_LABEL_BAD_TARGET
:
12994 gfc_warning ("Label %d at %L defined but cannot be used",
12995 label
->value
, &label
->where
);
13002 warn_unused_fortran_label (label
->right
);
13006 /* Returns the sequence type of a symbol or sequence. */
13009 sequence_type (gfc_typespec ts
)
13018 if (ts
.u
.derived
->components
== NULL
)
13019 return SEQ_NONDEFAULT
;
13021 result
= sequence_type (ts
.u
.derived
->components
->ts
);
13022 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
13023 if (sequence_type (c
->ts
) != result
)
13029 if (ts
.kind
!= gfc_default_character_kind
)
13030 return SEQ_NONDEFAULT
;
13032 return SEQ_CHARACTER
;
13035 if (ts
.kind
!= gfc_default_integer_kind
)
13036 return SEQ_NONDEFAULT
;
13038 return SEQ_NUMERIC
;
13041 if (!(ts
.kind
== gfc_default_real_kind
13042 || ts
.kind
== gfc_default_double_kind
))
13043 return SEQ_NONDEFAULT
;
13045 return SEQ_NUMERIC
;
13048 if (ts
.kind
!= gfc_default_complex_kind
)
13049 return SEQ_NONDEFAULT
;
13051 return SEQ_NUMERIC
;
13054 if (ts
.kind
!= gfc_default_logical_kind
)
13055 return SEQ_NONDEFAULT
;
13057 return SEQ_NUMERIC
;
13060 return SEQ_NONDEFAULT
;
13065 /* Resolve derived type EQUIVALENCE object. */
13068 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
13070 gfc_component
*c
= derived
->components
;
13075 /* Shall not be an object of nonsequence derived type. */
13076 if (!derived
->attr
.sequence
)
13078 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13079 "attribute to be an EQUIVALENCE object", sym
->name
,
13084 /* Shall not have allocatable components. */
13085 if (derived
->attr
.alloc_comp
)
13087 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13088 "components to be an EQUIVALENCE object",sym
->name
,
13093 if (sym
->attr
.in_common
&& gfc_has_default_initializer (sym
->ts
.u
.derived
))
13095 gfc_error ("Derived type variable '%s' at %L with default "
13096 "initialization cannot be in EQUIVALENCE with a variable "
13097 "in COMMON", sym
->name
, &e
->where
);
13101 for (; c
; c
= c
->next
)
13103 if (c
->ts
.type
== BT_DERIVED
13104 && (resolve_equivalence_derived (c
->ts
.u
.derived
, sym
, e
) == FAILURE
))
13107 /* Shall not be an object of sequence derived type containing a pointer
13108 in the structure. */
13109 if (c
->attr
.pointer
)
13111 gfc_error ("Derived type variable '%s' at %L with pointer "
13112 "component(s) cannot be an EQUIVALENCE object",
13113 sym
->name
, &e
->where
);
13121 /* Resolve equivalence object.
13122 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13123 an allocatable array, an object of nonsequence derived type, an object of
13124 sequence derived type containing a pointer at any level of component
13125 selection, an automatic object, a function name, an entry name, a result
13126 name, a named constant, a structure component, or a subobject of any of
13127 the preceding objects. A substring shall not have length zero. A
13128 derived type shall not have components with default initialization nor
13129 shall two objects of an equivalence group be initialized.
13130 Either all or none of the objects shall have an protected attribute.
13131 The simple constraints are done in symbol.c(check_conflict) and the rest
13132 are implemented here. */
13135 resolve_equivalence (gfc_equiv
*eq
)
13138 gfc_symbol
*first_sym
;
13141 locus
*last_where
= NULL
;
13142 seq_type eq_type
, last_eq_type
;
13143 gfc_typespec
*last_ts
;
13144 int object
, cnt_protected
;
13147 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
13149 first_sym
= eq
->expr
->symtree
->n
.sym
;
13153 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
13157 e
->ts
= e
->symtree
->n
.sym
->ts
;
13158 /* match_varspec might not know yet if it is seeing
13159 array reference or substring reference, as it doesn't
13161 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
13163 gfc_ref
*ref
= e
->ref
;
13164 sym
= e
->symtree
->n
.sym
;
13166 if (sym
->attr
.dimension
)
13168 ref
->u
.ar
.as
= sym
->as
;
13172 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13173 if (e
->ts
.type
== BT_CHARACTER
13175 && ref
->type
== REF_ARRAY
13176 && ref
->u
.ar
.dimen
== 1
13177 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
13178 && ref
->u
.ar
.stride
[0] == NULL
)
13180 gfc_expr
*start
= ref
->u
.ar
.start
[0];
13181 gfc_expr
*end
= ref
->u
.ar
.end
[0];
13184 /* Optimize away the (:) reference. */
13185 if (start
== NULL
&& end
== NULL
)
13188 e
->ref
= ref
->next
;
13190 e
->ref
->next
= ref
->next
;
13195 ref
->type
= REF_SUBSTRING
;
13197 start
= gfc_get_int_expr (gfc_default_integer_kind
,
13199 ref
->u
.ss
.start
= start
;
13200 if (end
== NULL
&& e
->ts
.u
.cl
)
13201 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
13202 ref
->u
.ss
.end
= end
;
13203 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
13210 /* Any further ref is an error. */
13213 gcc_assert (ref
->type
== REF_ARRAY
);
13214 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13220 if (gfc_resolve_expr (e
) == FAILURE
)
13223 sym
= e
->symtree
->n
.sym
;
13225 if (sym
->attr
.is_protected
)
13227 if (cnt_protected
> 0 && cnt_protected
!= object
)
13229 gfc_error ("Either all or none of the objects in the "
13230 "EQUIVALENCE set at %L shall have the "
13231 "PROTECTED attribute",
13236 /* Shall not equivalence common block variables in a PURE procedure. */
13237 if (sym
->ns
->proc_name
13238 && sym
->ns
->proc_name
->attr
.pure
13239 && sym
->attr
.in_common
)
13241 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13242 "object in the pure procedure '%s'",
13243 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
13247 /* Shall not be a named constant. */
13248 if (e
->expr_type
== EXPR_CONSTANT
)
13250 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13251 "object", sym
->name
, &e
->where
);
13255 if (e
->ts
.type
== BT_DERIVED
13256 && resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
) == FAILURE
)
13259 /* Check that the types correspond correctly:
13261 A numeric sequence structure may be equivalenced to another sequence
13262 structure, an object of default integer type, default real type, double
13263 precision real type, default logical type such that components of the
13264 structure ultimately only become associated to objects of the same
13265 kind. A character sequence structure may be equivalenced to an object
13266 of default character kind or another character sequence structure.
13267 Other objects may be equivalenced only to objects of the same type and
13268 kind parameters. */
13270 /* Identical types are unconditionally OK. */
13271 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
13272 goto identical_types
;
13274 last_eq_type
= sequence_type (*last_ts
);
13275 eq_type
= sequence_type (sym
->ts
);
13277 /* Since the pair of objects is not of the same type, mixed or
13278 non-default sequences can be rejected. */
13280 msg
= "Sequence %s with mixed components in EQUIVALENCE "
13281 "statement at %L with different type objects";
13283 && last_eq_type
== SEQ_MIXED
13284 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
)
13286 || (eq_type
== SEQ_MIXED
13287 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13288 &e
->where
) == FAILURE
))
13291 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
13292 "statement at %L with objects of different type";
13294 && last_eq_type
== SEQ_NONDEFAULT
13295 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
,
13296 last_where
) == FAILURE
)
13297 || (eq_type
== SEQ_NONDEFAULT
13298 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13299 &e
->where
) == FAILURE
))
13302 msg
="Non-CHARACTER object '%s' in default CHARACTER "
13303 "EQUIVALENCE statement at %L";
13304 if (last_eq_type
== SEQ_CHARACTER
13305 && eq_type
!= SEQ_CHARACTER
13306 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13307 &e
->where
) == FAILURE
)
13310 msg
="Non-NUMERIC object '%s' in default NUMERIC "
13311 "EQUIVALENCE statement at %L";
13312 if (last_eq_type
== SEQ_NUMERIC
13313 && eq_type
!= SEQ_NUMERIC
13314 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13315 &e
->where
) == FAILURE
)
13320 last_where
= &e
->where
;
13325 /* Shall not be an automatic array. */
13326 if (e
->ref
->type
== REF_ARRAY
13327 && gfc_resolve_array_spec (e
->ref
->u
.ar
.as
, 1) == FAILURE
)
13329 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13330 "an EQUIVALENCE object", sym
->name
, &e
->where
);
13337 /* Shall not be a structure component. */
13338 if (r
->type
== REF_COMPONENT
)
13340 gfc_error ("Structure component '%s' at %L cannot be an "
13341 "EQUIVALENCE object",
13342 r
->u
.c
.component
->name
, &e
->where
);
13346 /* A substring shall not have length zero. */
13347 if (r
->type
== REF_SUBSTRING
)
13349 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
13351 gfc_error ("Substring at %L has length zero",
13352 &r
->u
.ss
.start
->where
);
13362 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13365 resolve_fntype (gfc_namespace
*ns
)
13367 gfc_entry_list
*el
;
13370 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
13373 /* If there are any entries, ns->proc_name is the entry master
13374 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13376 sym
= ns
->entries
->sym
;
13378 sym
= ns
->proc_name
;
13379 if (sym
->result
== sym
13380 && sym
->ts
.type
== BT_UNKNOWN
13381 && gfc_set_default_type (sym
, 0, NULL
) == FAILURE
13382 && !sym
->attr
.untyped
)
13384 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13385 sym
->name
, &sym
->declared_at
);
13386 sym
->attr
.untyped
= 1;
13389 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
13390 && !sym
->attr
.contained
13391 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
13392 && gfc_check_symbol_access (sym
))
13394 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC function '%s' at "
13395 "%L of PRIVATE type '%s'", sym
->name
,
13396 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
13400 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
13402 if (el
->sym
->result
== el
->sym
13403 && el
->sym
->ts
.type
== BT_UNKNOWN
13404 && gfc_set_default_type (el
->sym
, 0, NULL
) == FAILURE
13405 && !el
->sym
->attr
.untyped
)
13407 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13408 el
->sym
->name
, &el
->sym
->declared_at
);
13409 el
->sym
->attr
.untyped
= 1;
13415 /* 12.3.2.1.1 Defined operators. */
13418 check_uop_procedure (gfc_symbol
*sym
, locus where
)
13420 gfc_formal_arglist
*formal
;
13422 if (!sym
->attr
.function
)
13424 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13425 sym
->name
, &where
);
13429 if (sym
->ts
.type
== BT_CHARACTER
13430 && !(sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
)
13431 && !(sym
->result
&& sym
->result
->ts
.u
.cl
13432 && sym
->result
->ts
.u
.cl
->length
))
13434 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13435 "character length", sym
->name
, &where
);
13439 formal
= sym
->formal
;
13440 if (!formal
|| !formal
->sym
)
13442 gfc_error ("User operator procedure '%s' at %L must have at least "
13443 "one argument", sym
->name
, &where
);
13447 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13449 gfc_error ("First argument of operator interface at %L must be "
13450 "INTENT(IN)", &where
);
13454 if (formal
->sym
->attr
.optional
)
13456 gfc_error ("First argument of operator interface at %L cannot be "
13457 "optional", &where
);
13461 formal
= formal
->next
;
13462 if (!formal
|| !formal
->sym
)
13465 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13467 gfc_error ("Second argument of operator interface at %L must be "
13468 "INTENT(IN)", &where
);
13472 if (formal
->sym
->attr
.optional
)
13474 gfc_error ("Second argument of operator interface at %L cannot be "
13475 "optional", &where
);
13481 gfc_error ("Operator interface at %L must have, at most, two "
13482 "arguments", &where
);
13490 gfc_resolve_uops (gfc_symtree
*symtree
)
13492 gfc_interface
*itr
;
13494 if (symtree
== NULL
)
13497 gfc_resolve_uops (symtree
->left
);
13498 gfc_resolve_uops (symtree
->right
);
13500 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
13501 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
13505 /* Examine all of the expressions associated with a program unit,
13506 assign types to all intermediate expressions, make sure that all
13507 assignments are to compatible types and figure out which names
13508 refer to which functions or subroutines. It doesn't check code
13509 block, which is handled by resolve_code. */
13512 resolve_types (gfc_namespace
*ns
)
13518 gfc_namespace
* old_ns
= gfc_current_ns
;
13520 /* Check that all IMPLICIT types are ok. */
13521 if (!ns
->seen_implicit_none
)
13524 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
13525 if (ns
->set_flag
[letter
]
13526 && resolve_typespec_used (&ns
->default_type
[letter
],
13527 &ns
->implicit_loc
[letter
],
13532 gfc_current_ns
= ns
;
13534 resolve_entries (ns
);
13536 resolve_common_vars (ns
->blank_common
.head
, false);
13537 resolve_common_blocks (ns
->common_root
);
13539 resolve_contained_functions (ns
);
13541 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
13542 && ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
13543 resolve_formal_arglist (ns
->proc_name
);
13545 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
13547 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
13548 resolve_charlen (cl
);
13550 gfc_traverse_ns (ns
, resolve_symbol
);
13552 resolve_fntype (ns
);
13554 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13556 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
13557 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13558 "also be PURE", n
->proc_name
->name
,
13559 &n
->proc_name
->declared_at
);
13565 gfc_check_interfaces (ns
);
13567 gfc_traverse_ns (ns
, resolve_values
);
13573 for (d
= ns
->data
; d
; d
= d
->next
)
13577 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
13579 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
13581 if (ns
->common_root
!= NULL
)
13582 gfc_traverse_symtree (ns
->common_root
, resolve_bind_c_comms
);
13584 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
13585 resolve_equivalence (eq
);
13587 /* Warn about unused labels. */
13588 if (warn_unused_label
)
13589 warn_unused_fortran_label (ns
->st_labels
);
13591 gfc_resolve_uops (ns
->uop_root
);
13593 gfc_current_ns
= old_ns
;
13597 /* Call resolve_code recursively. */
13600 resolve_codes (gfc_namespace
*ns
)
13603 bitmap_obstack old_obstack
;
13605 if (ns
->resolved
== 1)
13608 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13611 gfc_current_ns
= ns
;
13613 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13614 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
13617 /* Set to an out of range value. */
13618 current_entry_id
= -1;
13620 old_obstack
= labels_obstack
;
13621 bitmap_obstack_initialize (&labels_obstack
);
13623 resolve_code (ns
->code
, ns
);
13625 bitmap_obstack_release (&labels_obstack
);
13626 labels_obstack
= old_obstack
;
13630 /* This function is called after a complete program unit has been compiled.
13631 Its purpose is to examine all of the expressions associated with a program
13632 unit, assign types to all intermediate expressions, make sure that all
13633 assignments are to compatible types and figure out which names refer to
13634 which functions or subroutines. */
13637 gfc_resolve (gfc_namespace
*ns
)
13639 gfc_namespace
*old_ns
;
13640 code_stack
*old_cs_base
;
13646 old_ns
= gfc_current_ns
;
13647 old_cs_base
= cs_base
;
13649 resolve_types (ns
);
13650 resolve_codes (ns
);
13652 gfc_current_ns
= old_ns
;
13653 cs_base
= old_cs_base
;
13656 gfc_run_passes (ns
);