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 static gfc_try
resolve_fl_derived0 (gfc_symbol
*sym
);
956 /* Resolve all of the elements of a structure constructor and make sure that
957 the types are correct. The 'init' flag indicates that the given
958 constructor is an initializer. */
961 resolve_structure_cons (gfc_expr
*expr
, int init
)
963 gfc_constructor
*cons
;
970 if (expr
->ts
.type
== BT_DERIVED
)
971 resolve_fl_derived0 (expr
->ts
.u
.derived
);
973 cons
= gfc_constructor_first (expr
->value
.constructor
);
974 /* A constructor may have references if it is the result of substituting a
975 parameter variable. In this case we just pull out the component we
978 comp
= expr
->ref
->u
.c
.sym
->components
;
980 comp
= expr
->ts
.u
.derived
->components
;
982 /* See if the user is trying to invoke a structure constructor for one of
983 the iso_c_binding derived types. */
984 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
985 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
986 && (cons
->expr
== NULL
|| cons
->expr
->expr_type
!= EXPR_NULL
))
988 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
989 expr
->ts
.u
.derived
->name
, &(expr
->where
));
993 /* Return if structure constructor is c_null_(fun)prt. */
994 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
995 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
996 && cons
->expr
&& cons
->expr
->expr_type
== EXPR_NULL
)
999 for (; comp
&& cons
; comp
= comp
->next
, cons
= gfc_constructor_next (cons
))
1006 if (gfc_resolve_expr (cons
->expr
) == FAILURE
)
1012 rank
= comp
->as
? comp
->as
->rank
: 0;
1013 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
1014 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
1016 gfc_error ("The rank of the element in the derived type "
1017 "constructor at %L does not match that of the "
1018 "component (%d/%d)", &cons
->expr
->where
,
1019 cons
->expr
->rank
, rank
);
1023 /* If we don't have the right type, try to convert it. */
1025 if (!comp
->attr
.proc_pointer
&&
1026 !gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
1029 if (strcmp (comp
->name
, "_extends") == 0)
1031 /* Can afford to be brutal with the _extends initializer.
1032 The derived type can get lost because it is PRIVATE
1033 but it is not usage constrained by the standard. */
1034 cons
->expr
->ts
= comp
->ts
;
1037 else if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
1038 gfc_error ("The element in the derived type constructor at %L, "
1039 "for pointer component '%s', is %s but should be %s",
1040 &cons
->expr
->where
, comp
->name
,
1041 gfc_basic_typename (cons
->expr
->ts
.type
),
1042 gfc_basic_typename (comp
->ts
.type
));
1044 t
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
1047 /* For strings, the length of the constructor should be the same as
1048 the one of the structure, ensure this if the lengths are known at
1049 compile time and when we are dealing with PARAMETER or structure
1051 if (cons
->expr
->ts
.type
== BT_CHARACTER
&& comp
->ts
.u
.cl
1052 && comp
->ts
.u
.cl
->length
1053 && comp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1054 && cons
->expr
->ts
.u
.cl
&& cons
->expr
->ts
.u
.cl
->length
1055 && cons
->expr
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1056 && mpz_cmp (cons
->expr
->ts
.u
.cl
->length
->value
.integer
,
1057 comp
->ts
.u
.cl
->length
->value
.integer
) != 0)
1059 if (cons
->expr
->expr_type
== EXPR_VARIABLE
1060 && cons
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
1062 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1063 to make use of the gfc_resolve_character_array_constructor
1064 machinery. The expression is later simplified away to
1065 an array of string literals. */
1066 gfc_expr
*para
= cons
->expr
;
1067 cons
->expr
= gfc_get_expr ();
1068 cons
->expr
->ts
= para
->ts
;
1069 cons
->expr
->where
= para
->where
;
1070 cons
->expr
->expr_type
= EXPR_ARRAY
;
1071 cons
->expr
->rank
= para
->rank
;
1072 cons
->expr
->shape
= gfc_copy_shape (para
->shape
, para
->rank
);
1073 gfc_constructor_append_expr (&cons
->expr
->value
.constructor
,
1074 para
, &cons
->expr
->where
);
1076 if (cons
->expr
->expr_type
== EXPR_ARRAY
)
1079 p
= gfc_constructor_first (cons
->expr
->value
.constructor
);
1080 if (cons
->expr
->ts
.u
.cl
!= p
->expr
->ts
.u
.cl
)
1082 gfc_charlen
*cl
, *cl2
;
1085 for (cl
= gfc_current_ns
->cl_list
; cl
; cl
= cl
->next
)
1087 if (cl
== cons
->expr
->ts
.u
.cl
)
1095 cl2
->next
= cl
->next
;
1097 gfc_free_expr (cl
->length
);
1101 cons
->expr
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1102 cons
->expr
->ts
.u
.cl
->length_from_typespec
= true;
1103 cons
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (comp
->ts
.u
.cl
->length
);
1104 gfc_resolve_character_array_constructor (cons
->expr
);
1108 if (cons
->expr
->expr_type
== EXPR_NULL
1109 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
1110 || comp
->attr
.proc_pointer
1111 || (comp
->ts
.type
== BT_CLASS
1112 && (CLASS_DATA (comp
)->attr
.class_pointer
1113 || CLASS_DATA (comp
)->attr
.allocatable
))))
1116 gfc_error ("The NULL in the derived type constructor at %L is "
1117 "being applied to component '%s', which is neither "
1118 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
1122 if (!comp
->attr
.pointer
|| comp
->attr
.proc_pointer
1123 || cons
->expr
->expr_type
== EXPR_NULL
)
1126 a
= gfc_expr_attr (cons
->expr
);
1128 if (!a
.pointer
&& !a
.target
)
1131 gfc_error ("The element in the derived type constructor at %L, "
1132 "for pointer component '%s' should be a POINTER or "
1133 "a TARGET", &cons
->expr
->where
, comp
->name
);
1138 /* F08:C461. Additional checks for pointer initialization. */
1142 gfc_error ("Pointer initialization target at %L "
1143 "must not be ALLOCATABLE ", &cons
->expr
->where
);
1148 gfc_error ("Pointer initialization target at %L "
1149 "must have the SAVE attribute", &cons
->expr
->where
);
1153 /* F2003, C1272 (3). */
1154 if (gfc_pure (NULL
) && cons
->expr
->expr_type
== EXPR_VARIABLE
1155 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1156 || gfc_is_coindexed (cons
->expr
)))
1159 gfc_error ("Invalid expression in the derived type constructor for "
1160 "pointer component '%s' at %L in PURE procedure",
1161 comp
->name
, &cons
->expr
->where
);
1164 if (gfc_implicit_pure (NULL
)
1165 && cons
->expr
->expr_type
== EXPR_VARIABLE
1166 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1167 || gfc_is_coindexed (cons
->expr
)))
1168 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
1176 /****************** Expression name resolution ******************/
1178 /* Returns 0 if a symbol was not declared with a type or
1179 attribute declaration statement, nonzero otherwise. */
1182 was_declared (gfc_symbol
*sym
)
1188 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
1191 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
1192 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
1193 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
1194 || a
.asynchronous
|| a
.codimension
)
1201 /* Determine if a symbol is generic or not. */
1204 generic_sym (gfc_symbol
*sym
)
1208 if (sym
->attr
.generic
||
1209 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
1212 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1215 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1222 return generic_sym (s
);
1229 /* Determine if a symbol is specific or not. */
1232 specific_sym (gfc_symbol
*sym
)
1236 if (sym
->attr
.if_source
== IFSRC_IFBODY
1237 || sym
->attr
.proc
== PROC_MODULE
1238 || sym
->attr
.proc
== PROC_INTERNAL
1239 || sym
->attr
.proc
== PROC_ST_FUNCTION
1240 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
1241 || sym
->attr
.external
)
1244 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1247 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1249 return (s
== NULL
) ? 0 : specific_sym (s
);
1253 /* Figure out if the procedure is specific, generic or unknown. */
1256 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
}
1260 procedure_kind (gfc_symbol
*sym
)
1262 if (generic_sym (sym
))
1263 return PTYPE_GENERIC
;
1265 if (specific_sym (sym
))
1266 return PTYPE_SPECIFIC
;
1268 return PTYPE_UNKNOWN
;
1271 /* Check references to assumed size arrays. The flag need_full_assumed_size
1272 is nonzero when matching actual arguments. */
1274 static int need_full_assumed_size
= 0;
1277 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1279 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1282 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1283 What should it be? */
1284 if ((e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1285 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1286 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1288 gfc_error ("The upper bound in the last dimension must "
1289 "appear in the reference to the assumed size "
1290 "array '%s' at %L", sym
->name
, &e
->where
);
1297 /* Look for bad assumed size array references in argument expressions
1298 of elemental and array valued intrinsic procedures. Since this is
1299 called from procedure resolution functions, it only recurses at
1303 resolve_assumed_size_actual (gfc_expr
*e
)
1308 switch (e
->expr_type
)
1311 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1316 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1317 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1328 /* Check a generic procedure, passed as an actual argument, to see if
1329 there is a matching specific name. If none, it is an error, and if
1330 more than one, the reference is ambiguous. */
1332 count_specific_procs (gfc_expr
*e
)
1339 sym
= e
->symtree
->n
.sym
;
1341 for (p
= sym
->generic
; p
; p
= p
->next
)
1342 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1344 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1350 gfc_error ("'%s' at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1354 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1355 "argument at %L", sym
->name
, &e
->where
);
1361 /* See if a call to sym could possibly be a not allowed RECURSION because of
1362 a missing RECURIVE declaration. This means that either sym is the current
1363 context itself, or sym is the parent of a contained procedure calling its
1364 non-RECURSIVE containing procedure.
1365 This also works if sym is an ENTRY. */
1368 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1370 gfc_symbol
* proc_sym
;
1371 gfc_symbol
* context_proc
;
1372 gfc_namespace
* real_context
;
1374 if (sym
->attr
.flavor
== FL_PROGRAM
)
1377 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
);
1379 /* If we've got an ENTRY, find real procedure. */
1380 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1381 proc_sym
= sym
->ns
->entries
->sym
;
1385 /* If sym is RECURSIVE, all is well of course. */
1386 if (proc_sym
->attr
.recursive
|| gfc_option
.flag_recursive
)
1389 /* Find the context procedure's "real" symbol if it has entries.
1390 We look for a procedure symbol, so recurse on the parents if we don't
1391 find one (like in case of a BLOCK construct). */
1392 for (real_context
= context
; ; real_context
= real_context
->parent
)
1394 /* We should find something, eventually! */
1395 gcc_assert (real_context
);
1397 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1398 : real_context
->proc_name
);
1400 /* In some special cases, there may not be a proc_name, like for this
1402 real(bad_kind()) function foo () ...
1403 when checking the call to bad_kind ().
1404 In these cases, we simply return here and assume that the
1409 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1413 /* A call from sym's body to itself is recursion, of course. */
1414 if (context_proc
== proc_sym
)
1417 /* The same is true if context is a contained procedure and sym the
1419 if (context_proc
->attr
.contained
)
1421 gfc_symbol
* parent_proc
;
1423 gcc_assert (context
->parent
);
1424 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1425 : context
->parent
->proc_name
);
1427 if (parent_proc
== proc_sym
)
1435 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1436 its typespec and formal argument list. */
1439 resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1441 gfc_intrinsic_sym
* isym
= NULL
;
1447 /* Already resolved. */
1448 if (sym
->from_intmod
&& sym
->ts
.type
!= BT_UNKNOWN
)
1451 /* We already know this one is an intrinsic, so we don't call
1452 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1453 gfc_find_subroutine directly to check whether it is a function or
1456 if (sym
->intmod_sym_id
)
1457 isym
= gfc_intrinsic_function_by_id ((gfc_isym_id
) sym
->intmod_sym_id
);
1459 isym
= gfc_find_function (sym
->name
);
1463 if (sym
->ts
.type
!= BT_UNKNOWN
&& gfc_option
.warn_surprising
1464 && !sym
->attr
.implicit_type
)
1465 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1466 " ignored", sym
->name
, &sym
->declared_at
);
1468 if (!sym
->attr
.function
&&
1469 gfc_add_function (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1474 else if ((isym
= gfc_find_subroutine (sym
->name
)))
1476 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1478 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1479 " specifier", sym
->name
, &sym
->declared_at
);
1483 if (!sym
->attr
.subroutine
&&
1484 gfc_add_subroutine (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1489 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym
->name
,
1494 gfc_copy_formal_args_intr (sym
, isym
);
1496 /* Check it is actually available in the standard settings. */
1497 if (gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
)
1500 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1501 " available in the current standard settings but %s. Use"
1502 " an appropriate -std=* option or enable -fall-intrinsics"
1503 " in order to use it.",
1504 sym
->name
, &sym
->declared_at
, symstd
);
1512 /* Resolve a procedure expression, like passing it to a called procedure or as
1513 RHS for a procedure pointer assignment. */
1516 resolve_procedure_expression (gfc_expr
* expr
)
1520 if (expr
->expr_type
!= EXPR_VARIABLE
)
1522 gcc_assert (expr
->symtree
);
1524 sym
= expr
->symtree
->n
.sym
;
1526 if (sym
->attr
.intrinsic
)
1527 resolve_intrinsic (sym
, &expr
->where
);
1529 if (sym
->attr
.flavor
!= FL_PROCEDURE
1530 || (sym
->attr
.function
&& sym
->result
== sym
))
1533 /* A non-RECURSIVE procedure that is used as procedure expression within its
1534 own body is in danger of being called recursively. */
1535 if (is_illegal_recursion (sym
, gfc_current_ns
))
1536 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1537 " itself recursively. Declare it RECURSIVE or use"
1538 " -frecursive", sym
->name
, &expr
->where
);
1544 /* Resolve an actual argument list. Most of the time, this is just
1545 resolving the expressions in the list.
1546 The exception is that we sometimes have to decide whether arguments
1547 that look like procedure arguments are really simple variable
1551 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1552 bool no_formal_args
)
1555 gfc_symtree
*parent_st
;
1557 int save_need_full_assumed_size
;
1559 for (; arg
; arg
= arg
->next
)
1564 /* Check the label is a valid branching target. */
1567 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1569 gfc_error ("Label %d referenced at %L is never defined",
1570 arg
->label
->value
, &arg
->label
->where
);
1577 if (e
->expr_type
== EXPR_VARIABLE
1578 && e
->symtree
->n
.sym
->attr
.generic
1580 && count_specific_procs (e
) != 1)
1583 if (e
->ts
.type
!= BT_PROCEDURE
)
1585 save_need_full_assumed_size
= need_full_assumed_size
;
1586 if (e
->expr_type
!= EXPR_VARIABLE
)
1587 need_full_assumed_size
= 0;
1588 if (gfc_resolve_expr (e
) != SUCCESS
)
1590 need_full_assumed_size
= save_need_full_assumed_size
;
1594 /* See if the expression node should really be a variable reference. */
1596 sym
= e
->symtree
->n
.sym
;
1598 if (sym
->attr
.flavor
== FL_PROCEDURE
1599 || sym
->attr
.intrinsic
1600 || sym
->attr
.external
)
1604 /* If a procedure is not already determined to be something else
1605 check if it is intrinsic. */
1606 if (!sym
->attr
.intrinsic
1607 && !(sym
->attr
.external
|| sym
->attr
.use_assoc
1608 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1609 && gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1610 sym
->attr
.intrinsic
= 1;
1612 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1614 gfc_error ("Statement function '%s' at %L is not allowed as an "
1615 "actual argument", sym
->name
, &e
->where
);
1618 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1619 sym
->attr
.subroutine
);
1620 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1622 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1623 "actual argument", sym
->name
, &e
->where
);
1626 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1627 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1629 if (gfc_notify_std (GFC_STD_F2008
,
1630 "Fortran 2008: Internal procedure '%s' is"
1631 " used as actual argument at %L",
1632 sym
->name
, &e
->where
) == FAILURE
)
1636 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1638 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1639 "allowed as an actual argument at %L", sym
->name
,
1643 /* Check if a generic interface has a specific procedure
1644 with the same name before emitting an error. */
1645 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
1648 /* Just in case a specific was found for the expression. */
1649 sym
= e
->symtree
->n
.sym
;
1651 /* If the symbol is the function that names the current (or
1652 parent) scope, then we really have a variable reference. */
1654 if (gfc_is_function_return_value (sym
, sym
->ns
))
1657 /* If all else fails, see if we have a specific intrinsic. */
1658 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
1660 gfc_intrinsic_sym
*isym
;
1662 isym
= gfc_find_function (sym
->name
);
1663 if (isym
== NULL
|| !isym
->specific
)
1665 gfc_error ("Unable to find a specific INTRINSIC procedure "
1666 "for the reference '%s' at %L", sym
->name
,
1671 sym
->attr
.intrinsic
= 1;
1672 sym
->attr
.function
= 1;
1675 if (gfc_resolve_expr (e
) == FAILURE
)
1680 /* See if the name is a module procedure in a parent unit. */
1682 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1685 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
1687 gfc_error ("Symbol '%s' at %L is ambiguous", sym
->name
, &e
->where
);
1691 if (parent_st
== NULL
)
1694 sym
= parent_st
->n
.sym
;
1695 e
->symtree
= parent_st
; /* Point to the right thing. */
1697 if (sym
->attr
.flavor
== FL_PROCEDURE
1698 || sym
->attr
.intrinsic
1699 || sym
->attr
.external
)
1701 if (gfc_resolve_expr (e
) == FAILURE
)
1707 e
->expr_type
= EXPR_VARIABLE
;
1709 if (sym
->as
!= NULL
)
1711 e
->rank
= sym
->as
->rank
;
1712 e
->ref
= gfc_get_ref ();
1713 e
->ref
->type
= REF_ARRAY
;
1714 e
->ref
->u
.ar
.type
= AR_FULL
;
1715 e
->ref
->u
.ar
.as
= sym
->as
;
1718 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1719 primary.c (match_actual_arg). If above code determines that it
1720 is a variable instead, it needs to be resolved as it was not
1721 done at the beginning of this function. */
1722 save_need_full_assumed_size
= need_full_assumed_size
;
1723 if (e
->expr_type
!= EXPR_VARIABLE
)
1724 need_full_assumed_size
= 0;
1725 if (gfc_resolve_expr (e
) != SUCCESS
)
1727 need_full_assumed_size
= save_need_full_assumed_size
;
1730 /* Check argument list functions %VAL, %LOC and %REF. There is
1731 nothing to do for %REF. */
1732 if (arg
->name
&& arg
->name
[0] == '%')
1734 if (strncmp ("%VAL", arg
->name
, 4) == 0)
1736 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
1738 gfc_error ("By-value argument at %L is not of numeric "
1745 gfc_error ("By-value argument at %L cannot be an array or "
1746 "an array section", &e
->where
);
1750 /* Intrinsics are still PROC_UNKNOWN here. However,
1751 since same file external procedures are not resolvable
1752 in gfortran, it is a good deal easier to leave them to
1754 if (ptype
!= PROC_UNKNOWN
1755 && ptype
!= PROC_DUMMY
1756 && ptype
!= PROC_EXTERNAL
1757 && ptype
!= PROC_MODULE
)
1759 gfc_error ("By-value argument at %L is not allowed "
1760 "in this context", &e
->where
);
1765 /* Statement functions have already been excluded above. */
1766 else if (strncmp ("%LOC", arg
->name
, 4) == 0
1767 && e
->ts
.type
== BT_PROCEDURE
)
1769 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
1771 gfc_error ("Passing internal procedure at %L by location "
1772 "not allowed", &e
->where
);
1778 /* Fortran 2008, C1237. */
1779 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_is_coindexed (e
)
1780 && gfc_has_ultimate_pointer (e
))
1782 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1783 "component", &e
->where
);
1792 /* Do the checks of the actual argument list that are specific to elemental
1793 procedures. If called with c == NULL, we have a function, otherwise if
1794 expr == NULL, we have a subroutine. */
1797 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
1799 gfc_actual_arglist
*arg0
;
1800 gfc_actual_arglist
*arg
;
1801 gfc_symbol
*esym
= NULL
;
1802 gfc_intrinsic_sym
*isym
= NULL
;
1804 gfc_intrinsic_arg
*iformal
= NULL
;
1805 gfc_formal_arglist
*eformal
= NULL
;
1806 bool formal_optional
= false;
1807 bool set_by_optional
= false;
1811 /* Is this an elemental procedure? */
1812 if (expr
&& expr
->value
.function
.actual
!= NULL
)
1814 if (expr
->value
.function
.esym
!= NULL
1815 && expr
->value
.function
.esym
->attr
.elemental
)
1817 arg0
= expr
->value
.function
.actual
;
1818 esym
= expr
->value
.function
.esym
;
1820 else if (expr
->value
.function
.isym
!= NULL
1821 && expr
->value
.function
.isym
->elemental
)
1823 arg0
= expr
->value
.function
.actual
;
1824 isym
= expr
->value
.function
.isym
;
1829 else if (c
&& c
->ext
.actual
!= NULL
)
1831 arg0
= c
->ext
.actual
;
1833 if (c
->resolved_sym
)
1834 esym
= c
->resolved_sym
;
1836 esym
= c
->symtree
->n
.sym
;
1839 if (!esym
->attr
.elemental
)
1845 /* The rank of an elemental is the rank of its array argument(s). */
1846 for (arg
= arg0
; arg
; arg
= arg
->next
)
1848 if (arg
->expr
!= NULL
&& arg
->expr
->rank
> 0)
1850 rank
= arg
->expr
->rank
;
1851 if (arg
->expr
->expr_type
== EXPR_VARIABLE
1852 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
1853 set_by_optional
= true;
1855 /* Function specific; set the result rank and shape. */
1859 if (!expr
->shape
&& arg
->expr
->shape
)
1861 expr
->shape
= gfc_get_shape (rank
);
1862 for (i
= 0; i
< rank
; i
++)
1863 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
1870 /* If it is an array, it shall not be supplied as an actual argument
1871 to an elemental procedure unless an array of the same rank is supplied
1872 as an actual argument corresponding to a nonoptional dummy argument of
1873 that elemental procedure(12.4.1.5). */
1874 formal_optional
= false;
1876 iformal
= isym
->formal
;
1878 eformal
= esym
->formal
;
1880 for (arg
= arg0
; arg
; arg
= arg
->next
)
1884 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
1885 formal_optional
= true;
1886 eformal
= eformal
->next
;
1888 else if (isym
&& iformal
)
1890 if (iformal
->optional
)
1891 formal_optional
= true;
1892 iformal
= iformal
->next
;
1895 formal_optional
= true;
1897 if (pedantic
&& arg
->expr
!= NULL
1898 && arg
->expr
->expr_type
== EXPR_VARIABLE
1899 && arg
->expr
->symtree
->n
.sym
->attr
.optional
1902 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
1903 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
1905 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1906 "MISSING, it cannot be the actual argument of an "
1907 "ELEMENTAL procedure unless there is a non-optional "
1908 "argument with the same rank (12.4.1.5)",
1909 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
1914 for (arg
= arg0
; arg
; arg
= arg
->next
)
1916 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
1919 /* Being elemental, the last upper bound of an assumed size array
1920 argument must be present. */
1921 if (resolve_assumed_size_actual (arg
->expr
))
1924 /* Elemental procedure's array actual arguments must conform. */
1927 if (gfc_check_conformance (arg
->expr
, e
,
1928 "elemental procedure") == FAILURE
)
1935 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1936 is an array, the intent inout/out variable needs to be also an array. */
1937 if (rank
> 0 && esym
&& expr
== NULL
)
1938 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
1939 arg
= arg
->next
, eformal
= eformal
->next
)
1940 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
1941 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
1942 && arg
->expr
&& arg
->expr
->rank
== 0)
1944 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1945 "ELEMENTAL subroutine '%s' is a scalar, but another "
1946 "actual argument is an array", &arg
->expr
->where
,
1947 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
1948 : "INOUT", eformal
->sym
->name
, esym
->name
);
1955 /* This function does the checking of references to global procedures
1956 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1957 77 and 95 standards. It checks for a gsymbol for the name, making
1958 one if it does not already exist. If it already exists, then the
1959 reference being resolved must correspond to the type of gsymbol.
1960 Otherwise, the new symbol is equipped with the attributes of the
1961 reference. The corresponding code that is called in creating
1962 global entities is parse.c.
1964 In addition, for all but -std=legacy, the gsymbols are used to
1965 check the interfaces of external procedures from the same file.
1966 The namespace of the gsymbol is resolved and then, once this is
1967 done the interface is checked. */
1971 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1973 if (!gsym_ns
->proc_name
->attr
.recursive
)
1976 if (sym
->ns
== gsym_ns
)
1979 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
1986 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1988 if (gsym_ns
->entries
)
1990 gfc_entry_list
*entry
= gsym_ns
->entries
;
1992 for (; entry
; entry
= entry
->next
)
1994 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
1996 if (strcmp (gsym_ns
->proc_name
->name
,
1997 sym
->ns
->proc_name
->name
) == 0)
2001 && strcmp (gsym_ns
->proc_name
->name
,
2002 sym
->ns
->parent
->proc_name
->name
) == 0)
2011 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
,
2012 gfc_actual_arglist
**actual
, int sub
)
2016 enum gfc_symbol_type type
;
2018 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
2020 gsym
= gfc_get_gsymbol (sym
->name
);
2022 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
2023 gfc_global_used (gsym
, where
);
2025 if (gfc_option
.flag_whole_file
2026 && (sym
->attr
.if_source
== IFSRC_UNKNOWN
2027 || sym
->attr
.if_source
== IFSRC_IFBODY
)
2028 && gsym
->type
!= GSYM_UNKNOWN
2030 && gsym
->ns
->resolved
!= -1
2031 && gsym
->ns
->proc_name
2032 && not_in_recursive (sym
, gsym
->ns
)
2033 && not_entry_self_reference (sym
, gsym
->ns
))
2035 gfc_symbol
*def_sym
;
2037 /* Resolve the gsymbol namespace if needed. */
2038 if (!gsym
->ns
->resolved
)
2040 gfc_dt_list
*old_dt_list
;
2041 struct gfc_omp_saved_state old_omp_state
;
2043 /* Stash away derived types so that the backend_decls do not
2045 old_dt_list
= gfc_derived_types
;
2046 gfc_derived_types
= NULL
;
2047 /* And stash away openmp state. */
2048 gfc_omp_save_and_clear_state (&old_omp_state
);
2050 gfc_resolve (gsym
->ns
);
2052 /* Store the new derived types with the global namespace. */
2053 if (gfc_derived_types
)
2054 gsym
->ns
->derived_types
= gfc_derived_types
;
2056 /* Restore the derived types of this namespace. */
2057 gfc_derived_types
= old_dt_list
;
2058 /* And openmp state. */
2059 gfc_omp_restore_state (&old_omp_state
);
2062 /* Make sure that translation for the gsymbol occurs before
2063 the procedure currently being resolved. */
2064 ns
= gfc_global_ns_list
;
2065 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
2067 if (ns
->sibling
== gsym
->ns
)
2069 ns
->sibling
= gsym
->ns
->sibling
;
2070 gsym
->ns
->sibling
= gfc_global_ns_list
;
2071 gfc_global_ns_list
= gsym
->ns
;
2076 def_sym
= gsym
->ns
->proc_name
;
2077 if (def_sym
->attr
.entry_master
)
2079 gfc_entry_list
*entry
;
2080 for (entry
= gsym
->ns
->entries
; entry
; entry
= entry
->next
)
2081 if (strcmp (entry
->sym
->name
, sym
->name
) == 0)
2083 def_sym
= entry
->sym
;
2088 /* Differences in constant character lengths. */
2089 if (sym
->attr
.function
&& sym
->ts
.type
== BT_CHARACTER
)
2091 long int l1
= 0, l2
= 0;
2092 gfc_charlen
*cl1
= sym
->ts
.u
.cl
;
2093 gfc_charlen
*cl2
= def_sym
->ts
.u
.cl
;
2096 && cl1
->length
!= NULL
2097 && cl1
->length
->expr_type
== EXPR_CONSTANT
)
2098 l1
= mpz_get_si (cl1
->length
->value
.integer
);
2101 && cl2
->length
!= NULL
2102 && cl2
->length
->expr_type
== EXPR_CONSTANT
)
2103 l2
= mpz_get_si (cl2
->length
->value
.integer
);
2105 if (l1
&& l2
&& l1
!= l2
)
2106 gfc_error ("Character length mismatch in return type of "
2107 "function '%s' at %L (%ld/%ld)", sym
->name
,
2108 &sym
->declared_at
, l1
, l2
);
2111 /* Type mismatch of function return type and expected type. */
2112 if (sym
->attr
.function
2113 && !gfc_compare_types (&sym
->ts
, &def_sym
->ts
))
2114 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2115 sym
->name
, &sym
->declared_at
, gfc_typename (&sym
->ts
),
2116 gfc_typename (&def_sym
->ts
));
2118 if (def_sym
->formal
&& sym
->attr
.if_source
!= IFSRC_IFBODY
)
2120 gfc_formal_arglist
*arg
= def_sym
->formal
;
2121 for ( ; arg
; arg
= arg
->next
)
2124 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2125 else if (arg
->sym
->attr
.allocatable
2126 || arg
->sym
->attr
.asynchronous
2127 || arg
->sym
->attr
.optional
2128 || arg
->sym
->attr
.pointer
2129 || arg
->sym
->attr
.target
2130 || arg
->sym
->attr
.value
2131 || arg
->sym
->attr
.volatile_
)
2133 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2134 "has an attribute that requires an explicit "
2135 "interface for this procedure", arg
->sym
->name
,
2136 sym
->name
, &sym
->declared_at
);
2139 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2140 else if (arg
->sym
&& arg
->sym
->as
2141 && arg
->sym
->as
->type
== AS_ASSUMED_SHAPE
)
2143 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2144 "argument '%s' must have an explicit interface",
2145 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2148 /* F2008, 12.4.2.2 (2c) */
2149 else if (arg
->sym
->attr
.codimension
)
2151 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2152 "'%s' must have an explicit interface",
2153 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2156 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2157 else if (false) /* TODO: is a parametrized derived type */
2159 gfc_error ("Procedure '%s' at %L with parametrized derived "
2160 "type argument '%s' must have an explicit "
2161 "interface", sym
->name
, &sym
->declared_at
,
2165 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2166 else if (arg
->sym
->ts
.type
== BT_CLASS
)
2168 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2169 "argument '%s' must have an explicit interface",
2170 sym
->name
, &sym
->declared_at
, arg
->sym
->name
);
2175 if (def_sym
->attr
.function
)
2177 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2178 if (def_sym
->as
&& def_sym
->as
->rank
2179 && (!sym
->as
|| sym
->as
->rank
!= def_sym
->as
->rank
))
2180 gfc_error ("The reference to function '%s' at %L either needs an "
2181 "explicit INTERFACE or the rank is incorrect", sym
->name
,
2184 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2185 if ((def_sym
->result
->attr
.pointer
2186 || def_sym
->result
->attr
.allocatable
)
2187 && (sym
->attr
.if_source
!= IFSRC_IFBODY
2188 || def_sym
->result
->attr
.pointer
2189 != sym
->result
->attr
.pointer
2190 || def_sym
->result
->attr
.allocatable
2191 != sym
->result
->attr
.allocatable
))
2192 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2193 "result must have an explicit interface", sym
->name
,
2196 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2197 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->attr
.if_source
!= IFSRC_IFBODY
2198 && def_sym
->ts
.type
== BT_CHARACTER
&& def_sym
->ts
.u
.cl
->length
!= NULL
)
2200 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
2202 if (!sym
->attr
.entry_master
&& sym
->attr
.if_source
== IFSRC_UNKNOWN
2203 && cl
&& cl
->length
&& cl
->length
->expr_type
!= EXPR_CONSTANT
)
2205 gfc_error ("Nonconstant character-length function '%s' at %L "
2206 "must have an explicit interface", sym
->name
,
2212 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2213 if (def_sym
->attr
.elemental
&& !sym
->attr
.elemental
)
2215 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2216 "interface", sym
->name
, &sym
->declared_at
);
2219 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2220 if (def_sym
->attr
.is_bind_c
&& !sym
->attr
.is_bind_c
)
2222 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2223 "an explicit interface", sym
->name
, &sym
->declared_at
);
2226 if (gfc_option
.flag_whole_file
== 1
2227 || ((gfc_option
.warn_std
& GFC_STD_LEGACY
)
2228 && !(gfc_option
.warn_std
& GFC_STD_GNU
)))
2229 gfc_errors_to_warnings (1);
2231 if (sym
->attr
.if_source
!= IFSRC_IFBODY
)
2232 gfc_procedure_use (def_sym
, actual
, where
);
2234 gfc_errors_to_warnings (0);
2237 if (gsym
->type
== GSYM_UNKNOWN
)
2240 gsym
->where
= *where
;
2247 /************* Function resolution *************/
2249 /* Resolve a function call known to be generic.
2250 Section 14.1.2.4.1. */
2253 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
2257 if (sym
->attr
.generic
)
2259 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
2262 expr
->value
.function
.name
= s
->name
;
2263 expr
->value
.function
.esym
= s
;
2265 if (s
->ts
.type
!= BT_UNKNOWN
)
2267 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
2268 expr
->ts
= s
->result
->ts
;
2271 expr
->rank
= s
->as
->rank
;
2272 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
2273 expr
->rank
= s
->result
->as
->rank
;
2275 gfc_set_sym_referenced (expr
->value
.function
.esym
);
2280 /* TODO: Need to search for elemental references in generic
2284 if (sym
->attr
.intrinsic
)
2285 return gfc_intrinsic_func_interface (expr
, 0);
2292 resolve_generic_f (gfc_expr
*expr
)
2297 sym
= expr
->symtree
->n
.sym
;
2301 m
= resolve_generic_f0 (expr
, sym
);
2304 else if (m
== MATCH_ERROR
)
2308 if (sym
->ns
->parent
== NULL
)
2310 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2314 if (!generic_sym (sym
))
2318 /* Last ditch attempt. See if the reference is to an intrinsic
2319 that possesses a matching interface. 14.1.2.4 */
2320 if (sym
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
2322 gfc_error ("There is no specific function for the generic '%s' at %L",
2323 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2327 m
= gfc_intrinsic_func_interface (expr
, 0);
2331 gfc_error ("Generic function '%s' at %L is not consistent with a "
2332 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
2339 /* Resolve a function call known to be specific. */
2342 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
2346 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
2348 if (sym
->attr
.dummy
)
2350 sym
->attr
.proc
= PROC_DUMMY
;
2354 sym
->attr
.proc
= PROC_EXTERNAL
;
2358 if (sym
->attr
.proc
== PROC_MODULE
2359 || sym
->attr
.proc
== PROC_ST_FUNCTION
2360 || sym
->attr
.proc
== PROC_INTERNAL
)
2363 if (sym
->attr
.intrinsic
)
2365 m
= gfc_intrinsic_func_interface (expr
, 1);
2369 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2370 "with an intrinsic", sym
->name
, &expr
->where
);
2378 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2381 expr
->ts
= sym
->result
->ts
;
2384 expr
->value
.function
.name
= sym
->name
;
2385 expr
->value
.function
.esym
= sym
;
2386 if (sym
->as
!= NULL
)
2387 expr
->rank
= sym
->as
->rank
;
2394 resolve_specific_f (gfc_expr
*expr
)
2399 sym
= expr
->symtree
->n
.sym
;
2403 m
= resolve_specific_f0 (sym
, expr
);
2406 if (m
== MATCH_ERROR
)
2409 if (sym
->ns
->parent
== NULL
)
2412 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2418 gfc_error ("Unable to resolve the specific function '%s' at %L",
2419 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2425 /* Resolve a procedure call not known to be generic nor specific. */
2428 resolve_unknown_f (gfc_expr
*expr
)
2433 sym
= expr
->symtree
->n
.sym
;
2435 if (sym
->attr
.dummy
)
2437 sym
->attr
.proc
= PROC_DUMMY
;
2438 expr
->value
.function
.name
= sym
->name
;
2442 /* See if we have an intrinsic function reference. */
2444 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2446 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2451 /* The reference is to an external name. */
2453 sym
->attr
.proc
= PROC_EXTERNAL
;
2454 expr
->value
.function
.name
= sym
->name
;
2455 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2457 if (sym
->as
!= NULL
)
2458 expr
->rank
= sym
->as
->rank
;
2460 /* Type of the expression is either the type of the symbol or the
2461 default type of the symbol. */
2464 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2466 if (sym
->ts
.type
!= BT_UNKNOWN
)
2470 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2472 if (ts
->type
== BT_UNKNOWN
)
2474 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2475 sym
->name
, &expr
->where
);
2486 /* Return true, if the symbol is an external procedure. */
2488 is_external_proc (gfc_symbol
*sym
)
2490 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2491 && !(sym
->attr
.intrinsic
2492 || gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
))
2493 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2494 && !sym
->attr
.proc_pointer
2495 && !sym
->attr
.use_assoc
2503 /* Figure out if a function reference is pure or not. Also set the name
2504 of the function for a potential error message. Return nonzero if the
2505 function is PURE, zero if not. */
2507 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2510 pure_function (gfc_expr
*e
, const char **name
)
2516 if (e
->symtree
!= NULL
2517 && e
->symtree
->n
.sym
!= NULL
2518 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2519 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
2521 if (e
->value
.function
.esym
)
2523 pure
= gfc_pure (e
->value
.function
.esym
);
2524 *name
= e
->value
.function
.esym
->name
;
2526 else if (e
->value
.function
.isym
)
2528 pure
= e
->value
.function
.isym
->pure
2529 || e
->value
.function
.isym
->elemental
;
2530 *name
= e
->value
.function
.isym
->name
;
2534 /* Implicit functions are not pure. */
2536 *name
= e
->value
.function
.name
;
2544 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2545 int *f ATTRIBUTE_UNUSED
)
2549 /* Don't bother recursing into other statement functions
2550 since they will be checked individually for purity. */
2551 if (e
->expr_type
!= EXPR_FUNCTION
2553 || e
->symtree
->n
.sym
== sym
2554 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2557 return pure_function (e
, &name
) ? false : true;
2562 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
2564 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
2569 is_scalar_expr_ptr (gfc_expr
*expr
)
2571 gfc_try retval
= SUCCESS
;
2576 /* See if we have a gfc_ref, which means we have a substring, array
2577 reference, or a component. */
2578 if (expr
->ref
!= NULL
)
2581 while (ref
->next
!= NULL
)
2587 if (ref
->u
.ss
.start
== NULL
|| ref
->u
.ss
.end
== NULL
2588 || gfc_dep_compare_expr (ref
->u
.ss
.start
, ref
->u
.ss
.end
) != 0)
2593 if (ref
->u
.ar
.type
== AR_ELEMENT
)
2595 else if (ref
->u
.ar
.type
== AR_FULL
)
2597 /* The user can give a full array if the array is of size 1. */
2598 if (ref
->u
.ar
.as
!= NULL
2599 && ref
->u
.ar
.as
->rank
== 1
2600 && ref
->u
.ar
.as
->type
== AS_EXPLICIT
2601 && ref
->u
.ar
.as
->lower
[0] != NULL
2602 && ref
->u
.ar
.as
->lower
[0]->expr_type
== EXPR_CONSTANT
2603 && ref
->u
.ar
.as
->upper
[0] != NULL
2604 && ref
->u
.ar
.as
->upper
[0]->expr_type
== EXPR_CONSTANT
)
2606 /* If we have a character string, we need to check if
2607 its length is one. */
2608 if (expr
->ts
.type
== BT_CHARACTER
)
2610 if (expr
->ts
.u
.cl
== NULL
2611 || expr
->ts
.u
.cl
->length
== NULL
2612 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1)
2618 /* We have constant lower and upper bounds. If the
2619 difference between is 1, it can be considered a
2621 FIXME: Use gfc_dep_compare_expr instead. */
2622 start
= (int) mpz_get_si
2623 (ref
->u
.ar
.as
->lower
[0]->value
.integer
);
2624 end
= (int) mpz_get_si
2625 (ref
->u
.ar
.as
->upper
[0]->value
.integer
);
2626 if (end
- start
+ 1 != 1)
2641 else if (expr
->ts
.type
== BT_CHARACTER
&& expr
->rank
== 0)
2643 /* Character string. Make sure it's of length 1. */
2644 if (expr
->ts
.u
.cl
== NULL
2645 || expr
->ts
.u
.cl
->length
== NULL
2646 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
2649 else if (expr
->rank
!= 0)
2656 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2657 and, in the case of c_associated, set the binding label based on
2661 gfc_iso_c_func_interface (gfc_symbol
*sym
, gfc_actual_arglist
*args
,
2662 gfc_symbol
**new_sym
)
2664 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2665 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2666 int optional_arg
= 0;
2667 gfc_try retval
= SUCCESS
;
2668 gfc_symbol
*args_sym
;
2669 gfc_typespec
*arg_ts
;
2670 symbol_attribute arg_attr
;
2672 if (args
->expr
->expr_type
== EXPR_CONSTANT
2673 || args
->expr
->expr_type
== EXPR_OP
2674 || args
->expr
->expr_type
== EXPR_NULL
)
2676 gfc_error ("Argument to '%s' at %L is not a variable",
2677 sym
->name
, &(args
->expr
->where
));
2681 args_sym
= args
->expr
->symtree
->n
.sym
;
2683 /* The typespec for the actual arg should be that stored in the expr
2684 and not necessarily that of the expr symbol (args_sym), because
2685 the actual expression could be a part-ref of the expr symbol. */
2686 arg_ts
= &(args
->expr
->ts
);
2687 arg_attr
= gfc_expr_attr (args
->expr
);
2689 if (sym
->intmod_sym_id
== ISOCBINDING_ASSOCIATED
)
2691 /* If the user gave two args then they are providing something for
2692 the optional arg (the second cptr). Therefore, set the name and
2693 binding label to the c_associated for two cptrs. Otherwise,
2694 set c_associated to expect one cptr. */
2698 sprintf (name
, "%s_2", sym
->name
);
2699 sprintf (binding_label
, "%s_2", sym
->binding_label
);
2705 sprintf (name
, "%s_1", sym
->name
);
2706 sprintf (binding_label
, "%s_1", sym
->binding_label
);
2710 /* Get a new symbol for the version of c_associated that
2712 *new_sym
= get_iso_c_sym (sym
, name
, binding_label
, optional_arg
);
2714 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
2715 || sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2717 sprintf (name
, "%s", sym
->name
);
2718 sprintf (binding_label
, "%s", sym
->binding_label
);
2720 /* Error check the call. */
2721 if (args
->next
!= NULL
)
2723 gfc_error_now ("More actual than formal arguments in '%s' "
2724 "call at %L", name
, &(args
->expr
->where
));
2727 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
)
2732 /* Make sure we have either the target or pointer attribute. */
2733 if (!arg_attr
.target
&& !arg_attr
.pointer
)
2735 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2736 "a TARGET or an associated pointer",
2738 sym
->name
, &(args
->expr
->where
));
2742 if (gfc_is_coindexed (args
->expr
))
2744 gfc_error_now ("Coindexed argument not permitted"
2745 " in '%s' call at %L", name
,
2746 &(args
->expr
->where
));
2750 /* Follow references to make sure there are no array
2752 seen_section
= false;
2754 for (ref
=args
->expr
->ref
; ref
; ref
= ref
->next
)
2756 if (ref
->type
== REF_ARRAY
)
2758 if (ref
->u
.ar
.type
== AR_SECTION
)
2759 seen_section
= true;
2761 if (ref
->u
.ar
.type
!= AR_ELEMENT
)
2764 for (r
= ref
->next
; r
; r
=r
->next
)
2765 if (r
->type
== REF_COMPONENT
)
2767 gfc_error_now ("Array section not permitted"
2768 " in '%s' call at %L", name
,
2769 &(args
->expr
->where
));
2777 if (seen_section
&& retval
== SUCCESS
)
2778 gfc_warning ("Array section in '%s' call at %L", name
,
2779 &(args
->expr
->where
));
2781 /* See if we have interoperable type and type param. */
2782 if (verify_c_interop (arg_ts
) == SUCCESS
2783 || gfc_check_any_c_kind (arg_ts
) == SUCCESS
)
2785 if (args_sym
->attr
.target
== 1)
2787 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2788 has the target attribute and is interoperable. */
2789 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2790 allocatable variable that has the TARGET attribute and
2791 is not an array of zero size. */
2792 if (args_sym
->attr
.allocatable
== 1)
2794 if (args_sym
->attr
.dimension
!= 0
2795 && (args_sym
->as
&& args_sym
->as
->rank
== 0))
2797 gfc_error_now ("Allocatable variable '%s' used as a "
2798 "parameter to '%s' at %L must not be "
2799 "an array of zero size",
2800 args_sym
->name
, sym
->name
,
2801 &(args
->expr
->where
));
2807 /* A non-allocatable target variable with C
2808 interoperable type and type parameters must be
2810 if (args_sym
&& args_sym
->attr
.dimension
)
2812 if (args_sym
->as
->type
== AS_ASSUMED_SHAPE
)
2814 gfc_error ("Assumed-shape array '%s' at %L "
2815 "cannot be an argument to the "
2816 "procedure '%s' because "
2817 "it is not C interoperable",
2819 &(args
->expr
->where
), sym
->name
);
2822 else if (args_sym
->as
->type
== AS_DEFERRED
)
2824 gfc_error ("Deferred-shape array '%s' at %L "
2825 "cannot be an argument to the "
2826 "procedure '%s' because "
2827 "it is not C interoperable",
2829 &(args
->expr
->where
), sym
->name
);
2834 /* Make sure it's not a character string. Arrays of
2835 any type should be ok if the variable is of a C
2836 interoperable type. */
2837 if (arg_ts
->type
== BT_CHARACTER
)
2838 if (arg_ts
->u
.cl
!= NULL
2839 && (arg_ts
->u
.cl
->length
== NULL
2840 || arg_ts
->u
.cl
->length
->expr_type
2843 (arg_ts
->u
.cl
->length
->value
.integer
, 1)
2845 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2847 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2848 "at %L must have a length of 1",
2849 args_sym
->name
, sym
->name
,
2850 &(args
->expr
->where
));
2855 else if (arg_attr
.pointer
2856 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2858 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2860 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2861 "associated scalar POINTER", args_sym
->name
,
2862 sym
->name
, &(args
->expr
->where
));
2868 /* The parameter is not required to be C interoperable. If it
2869 is not C interoperable, it must be a nonpolymorphic scalar
2870 with no length type parameters. It still must have either
2871 the pointer or target attribute, and it can be
2872 allocatable (but must be allocated when c_loc is called). */
2873 if (args
->expr
->rank
!= 0
2874 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2876 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2877 "scalar", args_sym
->name
, sym
->name
,
2878 &(args
->expr
->where
));
2881 else if (arg_ts
->type
== BT_CHARACTER
2882 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2884 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2885 "%L must have a length of 1",
2886 args_sym
->name
, sym
->name
,
2887 &(args
->expr
->where
));
2890 else if (arg_ts
->type
== BT_CLASS
)
2892 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2893 "polymorphic", args_sym
->name
, sym
->name
,
2894 &(args
->expr
->where
));
2899 else if (sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2901 if (args_sym
->attr
.flavor
!= FL_PROCEDURE
)
2903 /* TODO: Update this error message to allow for procedure
2904 pointers once they are implemented. */
2905 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2907 args_sym
->name
, sym
->name
,
2908 &(args
->expr
->where
));
2911 else if (args_sym
->attr
.is_bind_c
!= 1)
2913 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2915 args_sym
->name
, sym
->name
,
2916 &(args
->expr
->where
));
2921 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2926 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2927 "iso_c_binding function: '%s'!\n", sym
->name
);
2934 /* Resolve a function call, which means resolving the arguments, then figuring
2935 out which entity the name refers to. */
2938 resolve_function (gfc_expr
*expr
)
2940 gfc_actual_arglist
*arg
;
2945 procedure_type p
= PROC_INTRINSIC
;
2946 bool no_formal_args
;
2950 sym
= expr
->symtree
->n
.sym
;
2952 /* If this is a procedure pointer component, it has already been resolved. */
2953 if (gfc_is_proc_ptr_comp (expr
, NULL
))
2956 if (sym
&& sym
->attr
.intrinsic
2957 && resolve_intrinsic (sym
, &expr
->where
) == FAILURE
)
2960 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
2962 gfc_error ("'%s' at %L is not a function", sym
->name
, &expr
->where
);
2966 /* If this ia a deferred TBP with an abstract interface (which may
2967 of course be referenced), expr->value.function.esym will be set. */
2968 if (sym
&& sym
->attr
.abstract
&& !expr
->value
.function
.esym
)
2970 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2971 sym
->name
, &expr
->where
);
2975 /* Switch off assumed size checking and do this again for certain kinds
2976 of procedure, once the procedure itself is resolved. */
2977 need_full_assumed_size
++;
2979 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
2980 p
= expr
->symtree
->n
.sym
->attr
.proc
;
2982 if (expr
->value
.function
.isym
&& expr
->value
.function
.isym
->inquiry
)
2983 inquiry_argument
= true;
2984 no_formal_args
= sym
&& is_external_proc (sym
) && sym
->formal
== NULL
;
2986 if (resolve_actual_arglist (expr
->value
.function
.actual
,
2987 p
, no_formal_args
) == FAILURE
)
2989 inquiry_argument
= false;
2993 inquiry_argument
= false;
2995 /* Need to setup the call to the correct c_associated, depending on
2996 the number of cptrs to user gives to compare. */
2997 if (sym
&& sym
->attr
.is_iso_c
== 1)
2999 if (gfc_iso_c_func_interface (sym
, expr
->value
.function
.actual
, &sym
)
3003 /* Get the symtree for the new symbol (resolved func).
3004 the old one will be freed later, when it's no longer used. */
3005 gfc_find_sym_tree (sym
->name
, sym
->ns
, 1, &(expr
->symtree
));
3008 /* Resume assumed_size checking. */
3009 need_full_assumed_size
--;
3011 /* If the procedure is external, check for usage. */
3012 if (sym
&& is_external_proc (sym
))
3013 resolve_global_procedure (sym
, &expr
->where
,
3014 &expr
->value
.function
.actual
, 0);
3016 if (sym
&& sym
->ts
.type
== BT_CHARACTER
3018 && sym
->ts
.u
.cl
->length
== NULL
3020 && !sym
->ts
.deferred
3021 && expr
->value
.function
.esym
== NULL
3022 && !sym
->attr
.contained
)
3024 /* Internal procedures are taken care of in resolve_contained_fntype. */
3025 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3026 "be used at %L since it is not a dummy argument",
3027 sym
->name
, &expr
->where
);
3031 /* See if function is already resolved. */
3033 if (expr
->value
.function
.name
!= NULL
)
3035 if (expr
->ts
.type
== BT_UNKNOWN
)
3041 /* Apply the rules of section 14.1.2. */
3043 switch (procedure_kind (sym
))
3046 t
= resolve_generic_f (expr
);
3049 case PTYPE_SPECIFIC
:
3050 t
= resolve_specific_f (expr
);
3054 t
= resolve_unknown_f (expr
);
3058 gfc_internal_error ("resolve_function(): bad function type");
3062 /* If the expression is still a function (it might have simplified),
3063 then we check to see if we are calling an elemental function. */
3065 if (expr
->expr_type
!= EXPR_FUNCTION
)
3068 temp
= need_full_assumed_size
;
3069 need_full_assumed_size
= 0;
3071 if (resolve_elemental_actual (expr
, NULL
) == FAILURE
)
3074 if (omp_workshare_flag
3075 && expr
->value
.function
.esym
3076 && ! gfc_elemental (expr
->value
.function
.esym
))
3078 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3079 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
3084 #define GENERIC_ID expr->value.function.isym->id
3085 else if (expr
->value
.function
.actual
!= NULL
3086 && expr
->value
.function
.isym
!= NULL
3087 && GENERIC_ID
!= GFC_ISYM_LBOUND
3088 && GENERIC_ID
!= GFC_ISYM_LEN
3089 && GENERIC_ID
!= GFC_ISYM_LOC
3090 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
3092 /* Array intrinsics must also have the last upper bound of an
3093 assumed size array argument. UBOUND and SIZE have to be
3094 excluded from the check if the second argument is anything
3097 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
3099 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
3100 && arg
->next
!= NULL
&& arg
->next
->expr
)
3102 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
3105 if (arg
->next
->name
&& strncmp(arg
->next
->name
, "kind", 4) == 0)
3108 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
3113 if (arg
->expr
!= NULL
3114 && arg
->expr
->rank
> 0
3115 && resolve_assumed_size_actual (arg
->expr
))
3121 need_full_assumed_size
= temp
;
3124 if (!pure_function (expr
, &name
) && name
)
3128 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3129 "FORALL %s", name
, &expr
->where
,
3130 forall_flag
== 2 ? "mask" : "block");
3133 else if (gfc_pure (NULL
))
3135 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3136 "procedure within a PURE procedure", name
, &expr
->where
);
3141 if (!pure_function (expr
, &name
) && name
&& gfc_implicit_pure (NULL
))
3142 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3144 /* Functions without the RECURSIVE attribution are not allowed to
3145 * call themselves. */
3146 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
3149 esym
= expr
->value
.function
.esym
;
3151 if (is_illegal_recursion (esym
, gfc_current_ns
))
3153 if (esym
->attr
.entry
&& esym
->ns
->entries
)
3154 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3155 " function '%s' is not RECURSIVE",
3156 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
3158 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3159 " is not RECURSIVE", esym
->name
, &expr
->where
);
3165 /* Character lengths of use associated functions may contains references to
3166 symbols not referenced from the current program unit otherwise. Make sure
3167 those symbols are marked as referenced. */
3169 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
3170 && expr
->value
.function
.esym
->attr
.use_assoc
)
3172 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
3175 /* Make sure that the expression has a typespec that works. */
3176 if (expr
->ts
.type
== BT_UNKNOWN
)
3178 if (expr
->symtree
->n
.sym
->result
3179 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
3180 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
3181 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
3188 /************* Subroutine resolution *************/
3191 pure_subroutine (gfc_code
*c
, gfc_symbol
*sym
)
3197 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3198 sym
->name
, &c
->loc
);
3199 else if (gfc_pure (NULL
))
3200 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym
->name
,
3206 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3210 if (sym
->attr
.generic
)
3212 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
3215 c
->resolved_sym
= s
;
3216 pure_subroutine (c
, s
);
3220 /* TODO: Need to search for elemental references in generic interface. */
3223 if (sym
->attr
.intrinsic
)
3224 return gfc_intrinsic_sub_interface (c
, 0);
3231 resolve_generic_s (gfc_code
*c
)
3236 sym
= c
->symtree
->n
.sym
;
3240 m
= resolve_generic_s0 (c
, sym
);
3243 else if (m
== MATCH_ERROR
)
3247 if (sym
->ns
->parent
== NULL
)
3249 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3253 if (!generic_sym (sym
))
3257 /* Last ditch attempt. See if the reference is to an intrinsic
3258 that possesses a matching interface. 14.1.2.4 */
3259 sym
= c
->symtree
->n
.sym
;
3261 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
3263 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3264 sym
->name
, &c
->loc
);
3268 m
= gfc_intrinsic_sub_interface (c
, 0);
3272 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3273 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
3279 /* Set the name and binding label of the subroutine symbol in the call
3280 expression represented by 'c' to include the type and kind of the
3281 second parameter. This function is for resolving the appropriate
3282 version of c_f_pointer() and c_f_procpointer(). For example, a
3283 call to c_f_pointer() for a default integer pointer could have a
3284 name of c_f_pointer_i4. If no second arg exists, which is an error
3285 for these two functions, it defaults to the generic symbol's name
3286 and binding label. */
3289 set_name_and_label (gfc_code
*c
, gfc_symbol
*sym
,
3290 char *name
, char *binding_label
)
3292 gfc_expr
*arg
= NULL
;
3296 /* The second arg of c_f_pointer and c_f_procpointer determines
3297 the type and kind for the procedure name. */
3298 arg
= c
->ext
.actual
->next
->expr
;
3302 /* Set up the name to have the given symbol's name,
3303 plus the type and kind. */
3304 /* a derived type is marked with the type letter 'u' */
3305 if (arg
->ts
.type
== BT_DERIVED
)
3308 kind
= 0; /* set the kind as 0 for now */
3312 type
= gfc_type_letter (arg
->ts
.type
);
3313 kind
= arg
->ts
.kind
;
3316 if (arg
->ts
.type
== BT_CHARACTER
)
3317 /* Kind info for character strings not needed. */
3320 sprintf (name
, "%s_%c%d", sym
->name
, type
, kind
);
3321 /* Set up the binding label as the given symbol's label plus
3322 the type and kind. */
3323 sprintf (binding_label
, "%s_%c%d", sym
->binding_label
, type
, kind
);
3327 /* If the second arg is missing, set the name and label as
3328 was, cause it should at least be found, and the missing
3329 arg error will be caught by compare_parameters(). */
3330 sprintf (name
, "%s", sym
->name
);
3331 sprintf (binding_label
, "%s", sym
->binding_label
);
3338 /* Resolve a generic version of the iso_c_binding procedure given
3339 (sym) to the specific one based on the type and kind of the
3340 argument(s). Currently, this function resolves c_f_pointer() and
3341 c_f_procpointer based on the type and kind of the second argument
3342 (FPTR). Other iso_c_binding procedures aren't specially handled.
3343 Upon successfully exiting, c->resolved_sym will hold the resolved
3344 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3348 gfc_iso_c_sub_interface (gfc_code
*c
, gfc_symbol
*sym
)
3350 gfc_symbol
*new_sym
;
3351 /* this is fine, since we know the names won't use the max */
3352 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3353 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
3354 /* default to success; will override if find error */
3355 match m
= MATCH_YES
;
3357 /* Make sure the actual arguments are in the necessary order (based on the
3358 formal args) before resolving. */
3359 gfc_procedure_use (sym
, &c
->ext
.actual
, &(c
->loc
));
3361 if ((sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
) ||
3362 (sym
->intmod_sym_id
== ISOCBINDING_F_PROCPOINTER
))
3364 set_name_and_label (c
, sym
, name
, binding_label
);
3366 if (sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
)
3368 if (c
->ext
.actual
!= NULL
&& c
->ext
.actual
->next
!= NULL
)
3370 /* Make sure we got a third arg if the second arg has non-zero
3371 rank. We must also check that the type and rank are
3372 correct since we short-circuit this check in
3373 gfc_procedure_use() (called above to sort actual args). */
3374 if (c
->ext
.actual
->next
->expr
->rank
!= 0)
3376 if(c
->ext
.actual
->next
->next
== NULL
3377 || c
->ext
.actual
->next
->next
->expr
== NULL
)
3380 gfc_error ("Missing SHAPE parameter for call to %s "
3381 "at %L", sym
->name
, &(c
->loc
));
3383 else if (c
->ext
.actual
->next
->next
->expr
->ts
.type
3385 || c
->ext
.actual
->next
->next
->expr
->rank
!= 1)
3388 gfc_error ("SHAPE parameter for call to %s at %L must "
3389 "be a rank 1 INTEGER array", sym
->name
,
3396 if (m
!= MATCH_ERROR
)
3398 /* the 1 means to add the optional arg to formal list */
3399 new_sym
= get_iso_c_sym (sym
, name
, binding_label
, 1);
3401 /* for error reporting, say it's declared where the original was */
3402 new_sym
->declared_at
= sym
->declared_at
;
3407 /* no differences for c_loc or c_funloc */
3411 /* set the resolved symbol */
3412 if (m
!= MATCH_ERROR
)
3413 c
->resolved_sym
= new_sym
;
3415 c
->resolved_sym
= sym
;
3421 /* Resolve a subroutine call known to be specific. */
3424 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3428 if(sym
->attr
.is_iso_c
)
3430 m
= gfc_iso_c_sub_interface (c
,sym
);
3434 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
3436 if (sym
->attr
.dummy
)
3438 sym
->attr
.proc
= PROC_DUMMY
;
3442 sym
->attr
.proc
= PROC_EXTERNAL
;
3446 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
3449 if (sym
->attr
.intrinsic
)
3451 m
= gfc_intrinsic_sub_interface (c
, 1);
3455 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3456 "with an intrinsic", sym
->name
, &c
->loc
);
3464 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3466 c
->resolved_sym
= sym
;
3467 pure_subroutine (c
, sym
);
3474 resolve_specific_s (gfc_code
*c
)
3479 sym
= c
->symtree
->n
.sym
;
3483 m
= resolve_specific_s0 (c
, sym
);
3486 if (m
== MATCH_ERROR
)
3489 if (sym
->ns
->parent
== NULL
)
3492 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3498 sym
= c
->symtree
->n
.sym
;
3499 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3500 sym
->name
, &c
->loc
);
3506 /* Resolve a subroutine call not known to be generic nor specific. */
3509 resolve_unknown_s (gfc_code
*c
)
3513 sym
= c
->symtree
->n
.sym
;
3515 if (sym
->attr
.dummy
)
3517 sym
->attr
.proc
= PROC_DUMMY
;
3521 /* See if we have an intrinsic function reference. */
3523 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3525 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3530 /* The reference is to an external name. */
3533 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3535 c
->resolved_sym
= sym
;
3537 pure_subroutine (c
, sym
);
3543 /* Resolve a subroutine call. Although it was tempting to use the same code
3544 for functions, subroutines and functions are stored differently and this
3545 makes things awkward. */
3548 resolve_call (gfc_code
*c
)
3551 procedure_type ptype
= PROC_INTRINSIC
;
3552 gfc_symbol
*csym
, *sym
;
3553 bool no_formal_args
;
3555 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3557 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3559 gfc_error ("'%s' at %L has a type, which is not consistent with "
3560 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3564 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3567 gfc_find_sym_tree (csym
->name
, gfc_current_ns
, 1, &st
);
3568 sym
= st
? st
->n
.sym
: NULL
;
3569 if (sym
&& csym
!= sym
3570 && sym
->ns
== gfc_current_ns
3571 && sym
->attr
.flavor
== FL_PROCEDURE
3572 && sym
->attr
.contained
)
3575 if (csym
->attr
.generic
)
3576 c
->symtree
->n
.sym
= sym
;
3579 csym
= c
->symtree
->n
.sym
;
3583 /* If this ia a deferred TBP with an abstract interface
3584 (which may of course be referenced), c->expr1 will be set. */
3585 if (csym
&& csym
->attr
.abstract
&& !c
->expr1
)
3587 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3588 csym
->name
, &c
->loc
);
3592 /* Subroutines without the RECURSIVE attribution are not allowed to
3593 * call themselves. */
3594 if (csym
&& is_illegal_recursion (csym
, gfc_current_ns
))
3596 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3597 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3598 " subroutine '%s' is not RECURSIVE",
3599 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3601 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3602 " is not RECURSIVE", csym
->name
, &c
->loc
);
3607 /* Switch off assumed size checking and do this again for certain kinds
3608 of procedure, once the procedure itself is resolved. */
3609 need_full_assumed_size
++;
3612 ptype
= csym
->attr
.proc
;
3614 no_formal_args
= csym
&& is_external_proc (csym
) && csym
->formal
== NULL
;
3615 if (resolve_actual_arglist (c
->ext
.actual
, ptype
,
3616 no_formal_args
) == FAILURE
)
3619 /* Resume assumed_size checking. */
3620 need_full_assumed_size
--;
3622 /* If external, check for usage. */
3623 if (csym
&& is_external_proc (csym
))
3624 resolve_global_procedure (csym
, &c
->loc
, &c
->ext
.actual
, 1);
3627 if (c
->resolved_sym
== NULL
)
3629 c
->resolved_isym
= NULL
;
3630 switch (procedure_kind (csym
))
3633 t
= resolve_generic_s (c
);
3636 case PTYPE_SPECIFIC
:
3637 t
= resolve_specific_s (c
);
3641 t
= resolve_unknown_s (c
);
3645 gfc_internal_error ("resolve_subroutine(): bad function type");
3649 /* Some checks of elemental subroutine actual arguments. */
3650 if (resolve_elemental_actual (NULL
, c
) == FAILURE
)
3657 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3658 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3659 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3660 if their shapes do not match. If either op1->shape or op2->shape is
3661 NULL, return SUCCESS. */
3664 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3671 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3673 for (i
= 0; i
< op1
->rank
; i
++)
3675 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3677 gfc_error ("Shapes for operands at %L and %L are not conformable",
3678 &op1
->where
, &op2
->where
);
3689 /* Resolve an operator expression node. This can involve replacing the
3690 operation with a user defined function call. */
3693 resolve_operator (gfc_expr
*e
)
3695 gfc_expr
*op1
, *op2
;
3697 bool dual_locus_error
;
3700 /* Resolve all subnodes-- give them types. */
3702 switch (e
->value
.op
.op
)
3705 if (gfc_resolve_expr (e
->value
.op
.op2
) == FAILURE
)
3708 /* Fall through... */
3711 case INTRINSIC_UPLUS
:
3712 case INTRINSIC_UMINUS
:
3713 case INTRINSIC_PARENTHESES
:
3714 if (gfc_resolve_expr (e
->value
.op
.op1
) == FAILURE
)
3719 /* Typecheck the new node. */
3721 op1
= e
->value
.op
.op1
;
3722 op2
= e
->value
.op
.op2
;
3723 dual_locus_error
= false;
3725 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
3726 || (op2
&& op2
->expr_type
== EXPR_NULL
))
3728 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
3732 switch (e
->value
.op
.op
)
3734 case INTRINSIC_UPLUS
:
3735 case INTRINSIC_UMINUS
:
3736 if (op1
->ts
.type
== BT_INTEGER
3737 || op1
->ts
.type
== BT_REAL
3738 || op1
->ts
.type
== BT_COMPLEX
)
3744 sprintf (msg
, _("Operand of unary numeric operator '%s' at %%L is %s"),
3745 gfc_op2string (e
->value
.op
.op
), gfc_typename (&e
->ts
));
3748 case INTRINSIC_PLUS
:
3749 case INTRINSIC_MINUS
:
3750 case INTRINSIC_TIMES
:
3751 case INTRINSIC_DIVIDE
:
3752 case INTRINSIC_POWER
:
3753 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3755 gfc_type_convert_binary (e
, 1);
3760 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3761 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3762 gfc_typename (&op2
->ts
));
3765 case INTRINSIC_CONCAT
:
3766 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3767 && op1
->ts
.kind
== op2
->ts
.kind
)
3769 e
->ts
.type
= BT_CHARACTER
;
3770 e
->ts
.kind
= op1
->ts
.kind
;
3775 _("Operands of string concatenation operator at %%L are %s/%s"),
3776 gfc_typename (&op1
->ts
), gfc_typename (&op2
->ts
));
3782 case INTRINSIC_NEQV
:
3783 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3785 e
->ts
.type
= BT_LOGICAL
;
3786 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
3787 if (op1
->ts
.kind
< e
->ts
.kind
)
3788 gfc_convert_type (op1
, &e
->ts
, 2);
3789 else if (op2
->ts
.kind
< e
->ts
.kind
)
3790 gfc_convert_type (op2
, &e
->ts
, 2);
3794 sprintf (msg
, _("Operands of logical operator '%s' at %%L are %s/%s"),
3795 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3796 gfc_typename (&op2
->ts
));
3801 if (op1
->ts
.type
== BT_LOGICAL
)
3803 e
->ts
.type
= BT_LOGICAL
;
3804 e
->ts
.kind
= op1
->ts
.kind
;
3808 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
3809 gfc_typename (&op1
->ts
));
3813 case INTRINSIC_GT_OS
:
3815 case INTRINSIC_GE_OS
:
3817 case INTRINSIC_LT_OS
:
3819 case INTRINSIC_LE_OS
:
3820 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
3822 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
3826 /* Fall through... */
3829 case INTRINSIC_EQ_OS
:
3831 case INTRINSIC_NE_OS
:
3832 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3833 && op1
->ts
.kind
== op2
->ts
.kind
)
3835 e
->ts
.type
= BT_LOGICAL
;
3836 e
->ts
.kind
= gfc_default_logical_kind
;
3840 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3842 gfc_type_convert_binary (e
, 1);
3844 e
->ts
.type
= BT_LOGICAL
;
3845 e
->ts
.kind
= gfc_default_logical_kind
;
3849 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3851 _("Logicals at %%L must be compared with %s instead of %s"),
3852 (e
->value
.op
.op
== INTRINSIC_EQ
3853 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
3854 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
3857 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3858 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3859 gfc_typename (&op2
->ts
));
3863 case INTRINSIC_USER
:
3864 if (e
->value
.op
.uop
->op
== NULL
)
3865 sprintf (msg
, _("Unknown operator '%s' at %%L"), e
->value
.op
.uop
->name
);
3866 else if (op2
== NULL
)
3867 sprintf (msg
, _("Operand of user operator '%s' at %%L is %s"),
3868 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
));
3871 sprintf (msg
, _("Operands of user operator '%s' at %%L are %s/%s"),
3872 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
),
3873 gfc_typename (&op2
->ts
));
3874 e
->value
.op
.uop
->op
->sym
->attr
.referenced
= 1;
3879 case INTRINSIC_PARENTHESES
:
3881 if (e
->ts
.type
== BT_CHARACTER
)
3882 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
3886 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3889 /* Deal with arrayness of an operand through an operator. */
3893 switch (e
->value
.op
.op
)
3895 case INTRINSIC_PLUS
:
3896 case INTRINSIC_MINUS
:
3897 case INTRINSIC_TIMES
:
3898 case INTRINSIC_DIVIDE
:
3899 case INTRINSIC_POWER
:
3900 case INTRINSIC_CONCAT
:
3904 case INTRINSIC_NEQV
:
3906 case INTRINSIC_EQ_OS
:
3908 case INTRINSIC_NE_OS
:
3910 case INTRINSIC_GT_OS
:
3912 case INTRINSIC_GE_OS
:
3914 case INTRINSIC_LT_OS
:
3916 case INTRINSIC_LE_OS
:
3918 if (op1
->rank
== 0 && op2
->rank
== 0)
3921 if (op1
->rank
== 0 && op2
->rank
!= 0)
3923 e
->rank
= op2
->rank
;
3925 if (e
->shape
== NULL
)
3926 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
3929 if (op1
->rank
!= 0 && op2
->rank
== 0)
3931 e
->rank
= op1
->rank
;
3933 if (e
->shape
== NULL
)
3934 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3937 if (op1
->rank
!= 0 && op2
->rank
!= 0)
3939 if (op1
->rank
== op2
->rank
)
3941 e
->rank
= op1
->rank
;
3942 if (e
->shape
== NULL
)
3944 t
= compare_shapes (op1
, op2
);
3948 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3953 /* Allow higher level expressions to work. */
3956 /* Try user-defined operators, and otherwise throw an error. */
3957 dual_locus_error
= true;
3959 _("Inconsistent ranks for operator at %%L and %%L"));
3966 case INTRINSIC_PARENTHESES
:
3968 case INTRINSIC_UPLUS
:
3969 case INTRINSIC_UMINUS
:
3970 /* Simply copy arrayness attribute */
3971 e
->rank
= op1
->rank
;
3973 if (e
->shape
== NULL
)
3974 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3982 /* Attempt to simplify the expression. */
3985 t
= gfc_simplify_expr (e
, 0);
3986 /* Some calls do not succeed in simplification and return FAILURE
3987 even though there is no error; e.g. variable references to
3988 PARAMETER arrays. */
3989 if (!gfc_is_constant_expr (e
))
3998 if (gfc_extend_expr (e
, &real_error
) == SUCCESS
)
4005 if (dual_locus_error
)
4006 gfc_error (msg
, &op1
->where
, &op2
->where
);
4008 gfc_error (msg
, &e
->where
);
4014 /************** Array resolution subroutines **************/
4017 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
}
4020 /* Compare two integer expressions. */
4023 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
4027 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
4028 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
4031 /* If either of the types isn't INTEGER, we must have
4032 raised an error earlier. */
4034 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
4037 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
4047 /* Compare an integer expression with an integer. */
4050 compare_bound_int (gfc_expr
*a
, int b
)
4054 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4057 if (a
->ts
.type
!= BT_INTEGER
)
4058 gfc_internal_error ("compare_bound_int(): Bad expression");
4060 i
= mpz_cmp_si (a
->value
.integer
, b
);
4070 /* Compare an integer expression with a mpz_t. */
4073 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
4077 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4080 if (a
->ts
.type
!= BT_INTEGER
)
4081 gfc_internal_error ("compare_bound_int(): Bad expression");
4083 i
= mpz_cmp (a
->value
.integer
, b
);
4093 /* Compute the last value of a sequence given by a triplet.
4094 Return 0 if it wasn't able to compute the last value, or if the
4095 sequence if empty, and 1 otherwise. */
4098 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
4099 gfc_expr
*stride
, mpz_t last
)
4103 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
4104 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
4105 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
4108 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
4109 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
4112 if (stride
== NULL
|| compare_bound_int(stride
, 1) == CMP_EQ
)
4114 if (compare_bound (start
, end
) == CMP_GT
)
4116 mpz_set (last
, end
->value
.integer
);
4120 if (compare_bound_int (stride
, 0) == CMP_GT
)
4122 /* Stride is positive */
4123 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
4128 /* Stride is negative */
4129 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
4134 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
4135 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
4136 mpz_sub (last
, end
->value
.integer
, rem
);
4143 /* Compare a single dimension of an array reference to the array
4147 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
4151 if (ar
->dimen_type
[i
] == DIMEN_STAR
)
4153 gcc_assert (ar
->stride
[i
] == NULL
);
4154 /* This implies [*] as [*:] and [*:3] are not possible. */
4155 if (ar
->start
[i
] == NULL
)
4157 gcc_assert (ar
->end
[i
] == NULL
);
4162 /* Given start, end and stride values, calculate the minimum and
4163 maximum referenced indexes. */
4165 switch (ar
->dimen_type
[i
])
4168 case DIMEN_THIS_IMAGE
:
4173 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
4176 gfc_warning ("Array reference at %L is out of bounds "
4177 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4178 mpz_get_si (ar
->start
[i
]->value
.integer
),
4179 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4181 gfc_warning ("Array reference at %L is out of bounds "
4182 "(%ld < %ld) in codimension %d", &ar
->c_where
[i
],
4183 mpz_get_si (ar
->start
[i
]->value
.integer
),
4184 mpz_get_si (as
->lower
[i
]->value
.integer
),
4188 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
4191 gfc_warning ("Array reference at %L is out of bounds "
4192 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4193 mpz_get_si (ar
->start
[i
]->value
.integer
),
4194 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4196 gfc_warning ("Array reference at %L is out of bounds "
4197 "(%ld > %ld) in codimension %d", &ar
->c_where
[i
],
4198 mpz_get_si (ar
->start
[i
]->value
.integer
),
4199 mpz_get_si (as
->upper
[i
]->value
.integer
),
4208 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4209 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4211 comparison comp_start_end
= compare_bound (AR_START
, AR_END
);
4213 /* Check for zero stride, which is not allowed. */
4214 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
4216 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
4220 /* if start == len || (stride > 0 && start < len)
4221 || (stride < 0 && start > len),
4222 then the array section contains at least one element. In this
4223 case, there is an out-of-bounds access if
4224 (start < lower || start > upper). */
4225 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
4226 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
4227 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
4228 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
4229 && comp_start_end
== CMP_GT
))
4231 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
4233 gfc_warning ("Lower array reference at %L is out of bounds "
4234 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4235 mpz_get_si (AR_START
->value
.integer
),
4236 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4239 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
4241 gfc_warning ("Lower array reference at %L is out of bounds "
4242 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4243 mpz_get_si (AR_START
->value
.integer
),
4244 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4249 /* If we can compute the highest index of the array section,
4250 then it also has to be between lower and upper. */
4251 mpz_init (last_value
);
4252 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
4255 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
4257 gfc_warning ("Upper array reference at %L is out of bounds "
4258 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4259 mpz_get_si (last_value
),
4260 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4261 mpz_clear (last_value
);
4264 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
4266 gfc_warning ("Upper array reference at %L is out of bounds "
4267 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4268 mpz_get_si (last_value
),
4269 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4270 mpz_clear (last_value
);
4274 mpz_clear (last_value
);
4282 gfc_internal_error ("check_dimension(): Bad array reference");
4289 /* Compare an array reference with an array specification. */
4292 compare_spec_to_ref (gfc_array_ref
*ar
)
4299 /* TODO: Full array sections are only allowed as actual parameters. */
4300 if (as
->type
== AS_ASSUMED_SIZE
4301 && (/*ar->type == AR_FULL
4302 ||*/ (ar
->type
== AR_SECTION
4303 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
4305 gfc_error ("Rightmost upper bound of assumed size array section "
4306 "not specified at %L", &ar
->where
);
4310 if (ar
->type
== AR_FULL
)
4313 if (as
->rank
!= ar
->dimen
)
4315 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4316 &ar
->where
, ar
->dimen
, as
->rank
);
4320 /* ar->codimen == 0 is a local array. */
4321 if (as
->corank
!= ar
->codimen
&& ar
->codimen
!= 0)
4323 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4324 &ar
->where
, ar
->codimen
, as
->corank
);
4328 for (i
= 0; i
< as
->rank
; i
++)
4329 if (check_dimension (i
, ar
, as
) == FAILURE
)
4332 /* Local access has no coarray spec. */
4333 if (ar
->codimen
!= 0)
4334 for (i
= as
->rank
; i
< as
->rank
+ as
->corank
; i
++)
4336 if (ar
->dimen_type
[i
] != DIMEN_ELEMENT
&& !ar
->in_allocate
4337 && ar
->dimen_type
[i
] != DIMEN_THIS_IMAGE
)
4339 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4340 i
+ 1 - as
->rank
, &ar
->where
);
4343 if (check_dimension (i
, ar
, as
) == FAILURE
)
4347 if (as
->corank
&& ar
->codimen
== 0)
4350 ar
->codimen
= as
->corank
;
4351 for (n
= ar
->dimen
; n
< ar
->dimen
+ ar
->codimen
; n
++)
4352 ar
->dimen_type
[n
] = DIMEN_THIS_IMAGE
;
4359 /* Resolve one part of an array index. */
4362 gfc_resolve_index_1 (gfc_expr
*index
, int check_scalar
,
4363 int force_index_integer_kind
)
4370 if (gfc_resolve_expr (index
) == FAILURE
)
4373 if (check_scalar
&& index
->rank
!= 0)
4375 gfc_error ("Array index at %L must be scalar", &index
->where
);
4379 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
4381 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4382 &index
->where
, gfc_basic_typename (index
->ts
.type
));
4386 if (index
->ts
.type
== BT_REAL
)
4387 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: REAL array index at %L",
4388 &index
->where
) == FAILURE
)
4391 if ((index
->ts
.kind
!= gfc_index_integer_kind
4392 && force_index_integer_kind
)
4393 || index
->ts
.type
!= BT_INTEGER
)
4396 ts
.type
= BT_INTEGER
;
4397 ts
.kind
= gfc_index_integer_kind
;
4399 gfc_convert_type_warn (index
, &ts
, 2, 0);
4405 /* Resolve one part of an array index. */
4408 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
4410 return gfc_resolve_index_1 (index
, check_scalar
, 1);
4413 /* Resolve a dim argument to an intrinsic function. */
4416 gfc_resolve_dim_arg (gfc_expr
*dim
)
4421 if (gfc_resolve_expr (dim
) == FAILURE
)
4426 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
4431 if (dim
->ts
.type
!= BT_INTEGER
)
4433 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
4437 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
4442 ts
.type
= BT_INTEGER
;
4443 ts
.kind
= gfc_index_integer_kind
;
4445 gfc_convert_type_warn (dim
, &ts
, 2, 0);
4451 /* Given an expression that contains array references, update those array
4452 references to point to the right array specifications. While this is
4453 filled in during matching, this information is difficult to save and load
4454 in a module, so we take care of it here.
4456 The idea here is that the original array reference comes from the
4457 base symbol. We traverse the list of reference structures, setting
4458 the stored reference to references. Component references can
4459 provide an additional array specification. */
4462 find_array_spec (gfc_expr
*e
)
4466 gfc_symbol
*derived
;
4469 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
4470 as
= CLASS_DATA (e
->symtree
->n
.sym
)->as
;
4472 as
= e
->symtree
->n
.sym
->as
;
4475 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4480 gfc_internal_error ("find_array_spec(): Missing spec");
4487 if (derived
== NULL
)
4488 derived
= e
->symtree
->n
.sym
->ts
.u
.derived
;
4490 if (derived
->attr
.is_class
)
4491 derived
= derived
->components
->ts
.u
.derived
;
4493 c
= derived
->components
;
4495 for (; c
; c
= c
->next
)
4496 if (c
== ref
->u
.c
.component
)
4498 /* Track the sequence of component references. */
4499 if (c
->ts
.type
== BT_DERIVED
)
4500 derived
= c
->ts
.u
.derived
;
4505 gfc_internal_error ("find_array_spec(): Component not found");
4507 if (c
->attr
.dimension
)
4510 gfc_internal_error ("find_array_spec(): unused as(1)");
4521 gfc_internal_error ("find_array_spec(): unused as(2)");
4525 /* Resolve an array reference. */
4528 resolve_array_ref (gfc_array_ref
*ar
)
4530 int i
, check_scalar
;
4533 for (i
= 0; i
< ar
->dimen
+ ar
->codimen
; i
++)
4535 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4537 /* Do not force gfc_index_integer_kind for the start. We can
4538 do fine with any integer kind. This avoids temporary arrays
4539 created for indexing with a vector. */
4540 if (gfc_resolve_index_1 (ar
->start
[i
], check_scalar
, 0) == FAILURE
)
4542 if (gfc_resolve_index (ar
->end
[i
], check_scalar
) == FAILURE
)
4544 if (gfc_resolve_index (ar
->stride
[i
], check_scalar
) == FAILURE
)
4549 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4553 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4557 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4558 if (e
->expr_type
== EXPR_VARIABLE
4559 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4560 ar
->start
[i
] = gfc_get_parentheses (e
);
4564 gfc_error ("Array index at %L is an array of rank %d",
4565 &ar
->c_where
[i
], e
->rank
);
4569 /* Fill in the upper bound, which may be lower than the
4570 specified one for something like a(2:10:5), which is
4571 identical to a(2:7:5). Only relevant for strides not equal
4572 to one. Don't try a division by zero. */
4573 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4574 && ar
->stride
[i
] != NULL
&& ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
4575 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1L) != 0
4576 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 0L) != 0)
4580 if (gfc_ref_dimen_size (ar
, i
, &size
, &end
) == SUCCESS
)
4582 if (ar
->end
[i
] == NULL
)
4585 gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
4587 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4589 else if (ar
->end
[i
]->ts
.type
== BT_INTEGER
4590 && ar
->end
[i
]->expr_type
== EXPR_CONSTANT
)
4592 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4603 if (ar
->type
== AR_FULL
&& ar
->as
->rank
== 0)
4604 ar
->type
= AR_ELEMENT
;
4606 /* If the reference type is unknown, figure out what kind it is. */
4608 if (ar
->type
== AR_UNKNOWN
)
4610 ar
->type
= AR_ELEMENT
;
4611 for (i
= 0; i
< ar
->dimen
; i
++)
4612 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4613 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
4615 ar
->type
= AR_SECTION
;
4620 if (!ar
->as
->cray_pointee
&& compare_spec_to_ref (ar
) == FAILURE
)
4628 resolve_substring (gfc_ref
*ref
)
4630 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4632 if (ref
->u
.ss
.start
!= NULL
)
4634 if (gfc_resolve_expr (ref
->u
.ss
.start
) == FAILURE
)
4637 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
4639 gfc_error ("Substring start index at %L must be of type INTEGER",
4640 &ref
->u
.ss
.start
->where
);
4644 if (ref
->u
.ss
.start
->rank
!= 0)
4646 gfc_error ("Substring start index at %L must be scalar",
4647 &ref
->u
.ss
.start
->where
);
4651 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
4652 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4653 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4655 gfc_error ("Substring start index at %L is less than one",
4656 &ref
->u
.ss
.start
->where
);
4661 if (ref
->u
.ss
.end
!= NULL
)
4663 if (gfc_resolve_expr (ref
->u
.ss
.end
) == FAILURE
)
4666 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
4668 gfc_error ("Substring end index at %L must be of type INTEGER",
4669 &ref
->u
.ss
.end
->where
);
4673 if (ref
->u
.ss
.end
->rank
!= 0)
4675 gfc_error ("Substring end index at %L must be scalar",
4676 &ref
->u
.ss
.end
->where
);
4680 if (ref
->u
.ss
.length
!= NULL
4681 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
4682 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4683 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4685 gfc_error ("Substring end index at %L exceeds the string length",
4686 &ref
->u
.ss
.start
->where
);
4690 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
4691 gfc_integer_kinds
[k
].huge
) == CMP_GT
4692 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4693 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4695 gfc_error ("Substring end index at %L is too large",
4696 &ref
->u
.ss
.end
->where
);
4705 /* This function supplies missing substring charlens. */
4708 gfc_resolve_substring_charlen (gfc_expr
*e
)
4711 gfc_expr
*start
, *end
;
4713 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
4714 if (char_ref
->type
== REF_SUBSTRING
)
4720 gcc_assert (char_ref
->next
== NULL
);
4724 if (e
->ts
.u
.cl
->length
)
4725 gfc_free_expr (e
->ts
.u
.cl
->length
);
4726 else if (e
->expr_type
== EXPR_VARIABLE
4727 && e
->symtree
->n
.sym
->attr
.dummy
)
4731 e
->ts
.type
= BT_CHARACTER
;
4732 e
->ts
.kind
= gfc_default_character_kind
;
4735 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4737 if (char_ref
->u
.ss
.start
)
4738 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
4740 start
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
4742 if (char_ref
->u
.ss
.end
)
4743 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
4744 else if (e
->expr_type
== EXPR_VARIABLE
)
4745 end
= gfc_copy_expr (e
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4752 /* Length = (end - start +1). */
4753 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
4754 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
,
4755 gfc_get_int_expr (gfc_default_integer_kind
,
4758 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4759 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4761 /* Make sure that the length is simplified. */
4762 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
4763 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4767 /* Resolve subtype references. */
4770 resolve_ref (gfc_expr
*expr
)
4772 int current_part_dimension
, n_components
, seen_part_dimension
;
4775 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4776 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
4778 find_array_spec (expr
);
4782 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4786 if (resolve_array_ref (&ref
->u
.ar
) == FAILURE
)
4794 resolve_substring (ref
);
4798 /* Check constraints on part references. */
4800 current_part_dimension
= 0;
4801 seen_part_dimension
= 0;
4804 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4809 switch (ref
->u
.ar
.type
)
4812 /* Coarray scalar. */
4813 if (ref
->u
.ar
.as
->rank
== 0)
4815 current_part_dimension
= 0;
4820 current_part_dimension
= 1;
4824 current_part_dimension
= 0;
4828 gfc_internal_error ("resolve_ref(): Bad array reference");
4834 if (current_part_dimension
|| seen_part_dimension
)
4837 if (ref
->u
.c
.component
->attr
.pointer
4838 || ref
->u
.c
.component
->attr
.proc_pointer
)
4840 gfc_error ("Component to the right of a part reference "
4841 "with nonzero rank must not have the POINTER "
4842 "attribute at %L", &expr
->where
);
4845 else if (ref
->u
.c
.component
->attr
.allocatable
)
4847 gfc_error ("Component to the right of a part reference "
4848 "with nonzero rank must not have the ALLOCATABLE "
4849 "attribute at %L", &expr
->where
);
4861 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
4862 || ref
->next
== NULL
)
4863 && current_part_dimension
4864 && seen_part_dimension
)
4866 gfc_error ("Two or more part references with nonzero rank must "
4867 "not be specified at %L", &expr
->where
);
4871 if (ref
->type
== REF_COMPONENT
)
4873 if (current_part_dimension
)
4874 seen_part_dimension
= 1;
4876 /* reset to make sure */
4877 current_part_dimension
= 0;
4885 /* Given an expression, determine its shape. This is easier than it sounds.
4886 Leaves the shape array NULL if it is not possible to determine the shape. */
4889 expression_shape (gfc_expr
*e
)
4891 mpz_t array
[GFC_MAX_DIMENSIONS
];
4894 if (e
->rank
== 0 || e
->shape
!= NULL
)
4897 for (i
= 0; i
< e
->rank
; i
++)
4898 if (gfc_array_dimen_size (e
, i
, &array
[i
]) == FAILURE
)
4901 e
->shape
= gfc_get_shape (e
->rank
);
4903 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
4908 for (i
--; i
>= 0; i
--)
4909 mpz_clear (array
[i
]);
4913 /* Given a variable expression node, compute the rank of the expression by
4914 examining the base symbol and any reference structures it may have. */
4917 expression_rank (gfc_expr
*e
)
4922 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4923 could lead to serious confusion... */
4924 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
4928 if (e
->expr_type
== EXPR_ARRAY
)
4930 /* Constructors can have a rank different from one via RESHAPE(). */
4932 if (e
->symtree
== NULL
)
4938 e
->rank
= (e
->symtree
->n
.sym
->as
== NULL
)
4939 ? 0 : e
->symtree
->n
.sym
->as
->rank
;
4945 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4947 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.proc_pointer
4948 && ref
->u
.c
.component
->attr
.function
&& !ref
->next
)
4949 rank
= ref
->u
.c
.component
->as
? ref
->u
.c
.component
->as
->rank
: 0;
4951 if (ref
->type
!= REF_ARRAY
)
4954 if (ref
->u
.ar
.type
== AR_FULL
)
4956 rank
= ref
->u
.ar
.as
->rank
;
4960 if (ref
->u
.ar
.type
== AR_SECTION
)
4962 /* Figure out the rank of the section. */
4964 gfc_internal_error ("expression_rank(): Two array specs");
4966 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
4967 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
4968 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
4978 expression_shape (e
);
4982 /* Resolve a variable expression. */
4985 resolve_variable (gfc_expr
*e
)
4992 if (e
->symtree
== NULL
)
4994 sym
= e
->symtree
->n
.sym
;
4996 /* If this is an associate-name, it may be parsed with an array reference
4997 in error even though the target is scalar. Fail directly in this case. */
4998 if (sym
->assoc
&& !sym
->attr
.dimension
&& e
->ref
&& e
->ref
->type
== REF_ARRAY
)
5001 /* On the other hand, the parser may not have known this is an array;
5002 in this case, we have to add a FULL reference. */
5003 if (sym
->assoc
&& sym
->attr
.dimension
&& !e
->ref
)
5005 e
->ref
= gfc_get_ref ();
5006 e
->ref
->type
= REF_ARRAY
;
5007 e
->ref
->u
.ar
.type
= AR_FULL
;
5008 e
->ref
->u
.ar
.dimen
= 0;
5011 if (e
->ref
&& resolve_ref (e
) == FAILURE
)
5014 if (sym
->attr
.flavor
== FL_PROCEDURE
5015 && (!sym
->attr
.function
5016 || (sym
->attr
.function
&& sym
->result
5017 && sym
->result
->attr
.proc_pointer
5018 && !sym
->result
->attr
.function
)))
5020 e
->ts
.type
= BT_PROCEDURE
;
5021 goto resolve_procedure
;
5024 if (sym
->ts
.type
!= BT_UNKNOWN
)
5025 gfc_variable_attr (e
, &e
->ts
);
5028 /* Must be a simple variable reference. */
5029 if (gfc_set_default_type (sym
, 1, sym
->ns
) == FAILURE
)
5034 if (check_assumed_size_reference (sym
, e
))
5037 /* Deal with forward references to entries during resolve_code, to
5038 satisfy, at least partially, 12.5.2.5. */
5039 if (gfc_current_ns
->entries
5040 && current_entry_id
== sym
->entry_id
5043 && cs_base
->current
->op
!= EXEC_ENTRY
)
5045 gfc_entry_list
*entry
;
5046 gfc_formal_arglist
*formal
;
5050 /* If the symbol is a dummy... */
5051 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
5053 entry
= gfc_current_ns
->entries
;
5056 /* ...test if the symbol is a parameter of previous entries. */
5057 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
5058 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
5060 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
5064 /* If it has not been seen as a dummy, this is an error. */
5067 if (specification_expr
)
5068 gfc_error ("Variable '%s', used in a specification expression"
5069 ", is referenced at %L before the ENTRY statement "
5070 "in which it is a parameter",
5071 sym
->name
, &cs_base
->current
->loc
);
5073 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5074 "statement in which it is a parameter",
5075 sym
->name
, &cs_base
->current
->loc
);
5080 /* Now do the same check on the specification expressions. */
5081 specification_expr
= 1;
5082 if (sym
->ts
.type
== BT_CHARACTER
5083 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
5087 for (n
= 0; n
< sym
->as
->rank
; n
++)
5089 specification_expr
= 1;
5090 if (gfc_resolve_expr (sym
->as
->lower
[n
]) == FAILURE
)
5092 specification_expr
= 1;
5093 if (gfc_resolve_expr (sym
->as
->upper
[n
]) == FAILURE
)
5096 specification_expr
= 0;
5099 /* Update the symbol's entry level. */
5100 sym
->entry_id
= current_entry_id
+ 1;
5103 /* If a symbol has been host_associated mark it. This is used latter,
5104 to identify if aliasing is possible via host association. */
5105 if (sym
->attr
.flavor
== FL_VARIABLE
5106 && gfc_current_ns
->parent
5107 && (gfc_current_ns
->parent
== sym
->ns
5108 || (gfc_current_ns
->parent
->parent
5109 && gfc_current_ns
->parent
->parent
== sym
->ns
)))
5110 sym
->attr
.host_assoc
= 1;
5113 if (t
== SUCCESS
&& resolve_procedure_expression (e
) == FAILURE
)
5116 /* F2008, C617 and C1229. */
5117 if (!inquiry_argument
&& (e
->ts
.type
== BT_CLASS
|| e
->ts
.type
== BT_DERIVED
)
5118 && gfc_is_coindexed (e
))
5120 gfc_ref
*ref
, *ref2
= NULL
;
5122 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5124 if (ref
->type
== REF_COMPONENT
)
5126 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5130 for ( ; ref
; ref
= ref
->next
)
5131 if (ref
->type
== REF_COMPONENT
)
5134 /* Expression itself is not coindexed object. */
5135 if (ref
&& e
->ts
.type
== BT_CLASS
)
5137 gfc_error ("Polymorphic subobject of coindexed object at %L",
5142 /* Expression itself is coindexed object. */
5146 c
= ref2
? ref2
->u
.c
.component
: e
->symtree
->n
.sym
->components
;
5147 for ( ; c
; c
= c
->next
)
5148 if (c
->attr
.allocatable
&& c
->ts
.type
== BT_CLASS
)
5150 gfc_error ("Coindexed object with polymorphic allocatable "
5151 "subcomponent at %L", &e
->where
);
5162 /* Checks to see that the correct symbol has been host associated.
5163 The only situation where this arises is that in which a twice
5164 contained function is parsed after the host association is made.
5165 Therefore, on detecting this, change the symbol in the expression
5166 and convert the array reference into an actual arglist if the old
5167 symbol is a variable. */
5169 check_host_association (gfc_expr
*e
)
5171 gfc_symbol
*sym
, *old_sym
;
5175 gfc_actual_arglist
*arg
, *tail
= NULL
;
5176 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
5178 /* If the expression is the result of substitution in
5179 interface.c(gfc_extend_expr) because there is no way in
5180 which the host association can be wrong. */
5181 if (e
->symtree
== NULL
5182 || e
->symtree
->n
.sym
== NULL
5183 || e
->user_operator
)
5186 old_sym
= e
->symtree
->n
.sym
;
5188 if (gfc_current_ns
->parent
5189 && old_sym
->ns
!= gfc_current_ns
)
5191 /* Use the 'USE' name so that renamed module symbols are
5192 correctly handled. */
5193 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
5195 if (sym
&& old_sym
!= sym
5196 && sym
->ts
.type
== old_sym
->ts
.type
5197 && sym
->attr
.flavor
== FL_PROCEDURE
5198 && sym
->attr
.contained
)
5200 /* Clear the shape, since it might not be valid. */
5201 if (e
->shape
!= NULL
)
5203 for (n
= 0; n
< e
->rank
; n
++)
5204 mpz_clear (e
->shape
[n
]);
5209 /* Give the expression the right symtree! */
5210 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
5211 gcc_assert (st
!= NULL
);
5213 if (old_sym
->attr
.flavor
== FL_PROCEDURE
5214 || e
->expr_type
== EXPR_FUNCTION
)
5216 /* Original was function so point to the new symbol, since
5217 the actual argument list is already attached to the
5219 e
->value
.function
.esym
= NULL
;
5224 /* Original was variable so convert array references into
5225 an actual arglist. This does not need any checking now
5226 since resolve_function will take care of it. */
5227 e
->value
.function
.actual
= NULL
;
5228 e
->expr_type
= EXPR_FUNCTION
;
5231 /* Ambiguity will not arise if the array reference is not
5232 the last reference. */
5233 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5234 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
5237 gcc_assert (ref
->type
== REF_ARRAY
);
5239 /* Grab the start expressions from the array ref and
5240 copy them into actual arguments. */
5241 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
5243 arg
= gfc_get_actual_arglist ();
5244 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
5245 if (e
->value
.function
.actual
== NULL
)
5246 tail
= e
->value
.function
.actual
= arg
;
5254 /* Dump the reference list and set the rank. */
5255 gfc_free_ref_list (e
->ref
);
5257 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
5260 gfc_resolve_expr (e
);
5264 /* This might have changed! */
5265 return e
->expr_type
== EXPR_FUNCTION
;
5270 gfc_resolve_character_operator (gfc_expr
*e
)
5272 gfc_expr
*op1
= e
->value
.op
.op1
;
5273 gfc_expr
*op2
= e
->value
.op
.op2
;
5274 gfc_expr
*e1
= NULL
;
5275 gfc_expr
*e2
= NULL
;
5277 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
5279 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
5280 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
5281 else if (op1
->expr_type
== EXPR_CONSTANT
)
5282 e1
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5283 op1
->value
.character
.length
);
5285 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
5286 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
5287 else if (op2
->expr_type
== EXPR_CONSTANT
)
5288 e2
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
5289 op2
->value
.character
.length
);
5291 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5296 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
5297 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
5298 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
5299 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
5300 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
5306 /* Ensure that an character expression has a charlen and, if possible, a
5307 length expression. */
5310 fixup_charlen (gfc_expr
*e
)
5312 /* The cases fall through so that changes in expression type and the need
5313 for multiple fixes are picked up. In all circumstances, a charlen should
5314 be available for the middle end to hang a backend_decl on. */
5315 switch (e
->expr_type
)
5318 gfc_resolve_character_operator (e
);
5321 if (e
->expr_type
== EXPR_ARRAY
)
5322 gfc_resolve_character_array_constructor (e
);
5324 case EXPR_SUBSTRING
:
5325 if (!e
->ts
.u
.cl
&& e
->ref
)
5326 gfc_resolve_substring_charlen (e
);
5330 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5337 /* Update an actual argument to include the passed-object for type-bound
5338 procedures at the right position. */
5340 static gfc_actual_arglist
*
5341 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
5344 gcc_assert (argpos
> 0);
5348 gfc_actual_arglist
* result
;
5350 result
= gfc_get_actual_arglist ();
5354 result
->name
= name
;
5360 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
5362 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
5367 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5370 extract_compcall_passed_object (gfc_expr
* e
)
5374 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5376 if (e
->value
.compcall
.base_object
)
5377 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
5380 po
= gfc_get_expr ();
5381 po
->expr_type
= EXPR_VARIABLE
;
5382 po
->symtree
= e
->symtree
;
5383 po
->ref
= gfc_copy_ref (e
->ref
);
5384 po
->where
= e
->where
;
5387 if (gfc_resolve_expr (po
) == FAILURE
)
5394 /* Update the arglist of an EXPR_COMPCALL expression to include the
5398 update_compcall_arglist (gfc_expr
* e
)
5401 gfc_typebound_proc
* tbp
;
5403 tbp
= e
->value
.compcall
.tbp
;
5408 po
= extract_compcall_passed_object (e
);
5412 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
5418 gcc_assert (tbp
->pass_arg_num
> 0);
5419 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5427 /* Extract the passed object from a PPC call (a copy of it). */
5430 extract_ppc_passed_object (gfc_expr
*e
)
5435 po
= gfc_get_expr ();
5436 po
->expr_type
= EXPR_VARIABLE
;
5437 po
->symtree
= e
->symtree
;
5438 po
->ref
= gfc_copy_ref (e
->ref
);
5439 po
->where
= e
->where
;
5441 /* Remove PPC reference. */
5443 while ((*ref
)->next
)
5444 ref
= &(*ref
)->next
;
5445 gfc_free_ref_list (*ref
);
5448 if (gfc_resolve_expr (po
) == FAILURE
)
5455 /* Update the actual arglist of a procedure pointer component to include the
5459 update_ppc_arglist (gfc_expr
* e
)
5463 gfc_typebound_proc
* tb
;
5465 if (!gfc_is_proc_ptr_comp (e
, &ppc
))
5472 else if (tb
->nopass
)
5475 po
= extract_ppc_passed_object (e
);
5482 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
5487 if (po
->ts
.type
== BT_DERIVED
&& po
->ts
.u
.derived
->attr
.abstract
)
5489 gfc_error ("Base object for procedure-pointer component call at %L is of"
5490 " ABSTRACT type '%s'", &e
->where
, po
->ts
.u
.derived
->name
);
5494 gcc_assert (tb
->pass_arg_num
> 0);
5495 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
5503 /* Check that the object a TBP is called on is valid, i.e. it must not be
5504 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5507 check_typebound_baseobject (gfc_expr
* e
)
5510 gfc_try return_value
= FAILURE
;
5512 base
= extract_compcall_passed_object (e
);
5516 gcc_assert (base
->ts
.type
== BT_DERIVED
|| base
->ts
.type
== BT_CLASS
);
5519 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
5521 gfc_error ("Base object for type-bound procedure call at %L is of"
5522 " ABSTRACT type '%s'", &e
->where
, base
->ts
.u
.derived
->name
);
5526 /* F08:C1230. If the procedure called is NOPASS,
5527 the base object must be scalar. */
5528 if (e
->value
.compcall
.tbp
->nopass
&& base
->rank
> 0)
5530 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5531 " be scalar", &e
->where
);
5535 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5538 gfc_error ("Non-scalar base object at %L currently not implemented",
5543 return_value
= SUCCESS
;
5546 gfc_free_expr (base
);
5547 return return_value
;
5551 /* Resolve a call to a type-bound procedure, either function or subroutine,
5552 statically from the data in an EXPR_COMPCALL expression. The adapted
5553 arglist and the target-procedure symtree are returned. */
5556 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
5557 gfc_actual_arglist
** actual
)
5559 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5560 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5562 /* Update the actual arglist for PASS. */
5563 if (update_compcall_arglist (e
) == FAILURE
)
5566 *actual
= e
->value
.compcall
.actual
;
5567 *target
= e
->value
.compcall
.tbp
->u
.specific
;
5569 gfc_free_ref_list (e
->ref
);
5571 e
->value
.compcall
.actual
= NULL
;
5577 /* Get the ultimate declared type from an expression. In addition,
5578 return the last class/derived type reference and the copy of the
5581 get_declared_from_expr (gfc_ref
**class_ref
, gfc_ref
**new_ref
,
5584 gfc_symbol
*declared
;
5591 *new_ref
= gfc_copy_ref (e
->ref
);
5593 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5595 if (ref
->type
!= REF_COMPONENT
)
5598 if (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5599 || ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5601 declared
= ref
->u
.c
.component
->ts
.u
.derived
;
5607 if (declared
== NULL
)
5608 declared
= e
->symtree
->n
.sym
->ts
.u
.derived
;
5614 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5615 which of the specific bindings (if any) matches the arglist and transform
5616 the expression into a call of that binding. */
5619 resolve_typebound_generic_call (gfc_expr
* e
, const char **name
)
5621 gfc_typebound_proc
* genproc
;
5622 const char* genname
;
5624 gfc_symbol
*derived
;
5626 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
5627 genname
= e
->value
.compcall
.name
;
5628 genproc
= e
->value
.compcall
.tbp
;
5630 if (!genproc
->is_generic
)
5633 /* Try the bindings on this type and in the inheritance hierarchy. */
5634 for (; genproc
; genproc
= genproc
->overridden
)
5638 gcc_assert (genproc
->is_generic
);
5639 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
5642 gfc_actual_arglist
* args
;
5645 gcc_assert (g
->specific
);
5647 if (g
->specific
->error
)
5650 target
= g
->specific
->u
.specific
->n
.sym
;
5652 /* Get the right arglist by handling PASS/NOPASS. */
5653 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
5654 if (!g
->specific
->nopass
)
5657 po
= extract_compcall_passed_object (e
);
5661 gcc_assert (g
->specific
->pass_arg_num
> 0);
5662 gcc_assert (!g
->specific
->error
);
5663 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
5664 g
->specific
->pass_arg
);
5666 resolve_actual_arglist (args
, target
->attr
.proc
,
5667 is_external_proc (target
) && !target
->formal
);
5669 /* Check if this arglist matches the formal. */
5670 matches
= gfc_arglist_matches_symbol (&args
, target
);
5672 /* Clean up and break out of the loop if we've found it. */
5673 gfc_free_actual_arglist (args
);
5676 e
->value
.compcall
.tbp
= g
->specific
;
5677 genname
= g
->specific_st
->name
;
5678 /* Pass along the name for CLASS methods, where the vtab
5679 procedure pointer component has to be referenced. */
5687 /* Nothing matching found! */
5688 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5689 " '%s' at %L", genname
, &e
->where
);
5693 /* Make sure that we have the right specific instance for the name. */
5694 derived
= get_declared_from_expr (NULL
, NULL
, e
);
5696 st
= gfc_find_typebound_proc (derived
, NULL
, genname
, true, &e
->where
);
5698 e
->value
.compcall
.tbp
= st
->n
.tb
;
5704 /* Resolve a call to a type-bound subroutine. */
5707 resolve_typebound_call (gfc_code
* c
, const char **name
)
5709 gfc_actual_arglist
* newactual
;
5710 gfc_symtree
* target
;
5712 /* Check that's really a SUBROUTINE. */
5713 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
5715 gfc_error ("'%s' at %L should be a SUBROUTINE",
5716 c
->expr1
->value
.compcall
.name
, &c
->loc
);
5720 if (check_typebound_baseobject (c
->expr1
) == FAILURE
)
5723 /* Pass along the name for CLASS methods, where the vtab
5724 procedure pointer component has to be referenced. */
5726 *name
= c
->expr1
->value
.compcall
.name
;
5728 if (resolve_typebound_generic_call (c
->expr1
, name
) == FAILURE
)
5731 /* Transform into an ordinary EXEC_CALL for now. */
5733 if (resolve_typebound_static (c
->expr1
, &target
, &newactual
) == FAILURE
)
5736 c
->ext
.actual
= newactual
;
5737 c
->symtree
= target
;
5738 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
5740 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
5742 gfc_free_expr (c
->expr1
);
5743 c
->expr1
= gfc_get_expr ();
5744 c
->expr1
->expr_type
= EXPR_FUNCTION
;
5745 c
->expr1
->symtree
= target
;
5746 c
->expr1
->where
= c
->loc
;
5748 return resolve_call (c
);
5752 /* Resolve a component-call expression. */
5754 resolve_compcall (gfc_expr
* e
, const char **name
)
5756 gfc_actual_arglist
* newactual
;
5757 gfc_symtree
* target
;
5759 /* Check that's really a FUNCTION. */
5760 if (!e
->value
.compcall
.tbp
->function
)
5762 gfc_error ("'%s' at %L should be a FUNCTION",
5763 e
->value
.compcall
.name
, &e
->where
);
5767 /* These must not be assign-calls! */
5768 gcc_assert (!e
->value
.compcall
.assign
);
5770 if (check_typebound_baseobject (e
) == FAILURE
)
5773 /* Pass along the name for CLASS methods, where the vtab
5774 procedure pointer component has to be referenced. */
5776 *name
= e
->value
.compcall
.name
;
5778 if (resolve_typebound_generic_call (e
, name
) == FAILURE
)
5780 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5782 /* Take the rank from the function's symbol. */
5783 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
5784 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
5786 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5787 arglist to the TBP's binding target. */
5789 if (resolve_typebound_static (e
, &target
, &newactual
) == FAILURE
)
5792 e
->value
.function
.actual
= newactual
;
5793 e
->value
.function
.name
= NULL
;
5794 e
->value
.function
.esym
= target
->n
.sym
;
5795 e
->value
.function
.isym
= NULL
;
5796 e
->symtree
= target
;
5797 e
->ts
= target
->n
.sym
->ts
;
5798 e
->expr_type
= EXPR_FUNCTION
;
5800 /* Resolution is not necessary if this is a class subroutine; this
5801 function only has to identify the specific proc. Resolution of
5802 the call will be done next in resolve_typebound_call. */
5803 return gfc_resolve_expr (e
);
5808 /* Resolve a typebound function, or 'method'. First separate all
5809 the non-CLASS references by calling resolve_compcall directly. */
5812 resolve_typebound_function (gfc_expr
* e
)
5814 gfc_symbol
*declared
;
5825 /* Deal with typebound operators for CLASS objects. */
5826 expr
= e
->value
.compcall
.base_object
;
5827 if (expr
&& expr
->ts
.type
== BT_CLASS
&& e
->value
.compcall
.name
)
5829 /* Since the typebound operators are generic, we have to ensure
5830 that any delays in resolution are corrected and that the vtab
5833 declared
= ts
.u
.derived
;
5834 c
= gfc_find_component (declared
, "_vptr", true, true);
5835 if (c
->ts
.u
.derived
== NULL
)
5836 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5838 if (resolve_compcall (e
, &name
) == FAILURE
)
5841 /* Use the generic name if it is there. */
5842 name
= name
? name
: e
->value
.function
.esym
->name
;
5843 e
->symtree
= expr
->symtree
;
5844 e
->ref
= gfc_copy_ref (expr
->ref
);
5845 gfc_add_vptr_component (e
);
5846 gfc_add_component_ref (e
, name
);
5847 e
->value
.function
.esym
= NULL
;
5852 return resolve_compcall (e
, NULL
);
5854 if (resolve_ref (e
) == FAILURE
)
5857 /* Get the CLASS declared type. */
5858 declared
= get_declared_from_expr (&class_ref
, &new_ref
, e
);
5860 /* Weed out cases of the ultimate component being a derived type. */
5861 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5862 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5864 gfc_free_ref_list (new_ref
);
5865 return resolve_compcall (e
, NULL
);
5868 c
= gfc_find_component (declared
, "_data", true, true);
5869 declared
= c
->ts
.u
.derived
;
5871 /* Treat the call as if it is a typebound procedure, in order to roll
5872 out the correct name for the specific function. */
5873 if (resolve_compcall (e
, &name
) == FAILURE
)
5877 /* Then convert the expression to a procedure pointer component call. */
5878 e
->value
.function
.esym
= NULL
;
5884 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5885 gfc_add_vptr_component (e
);
5886 gfc_add_component_ref (e
, name
);
5888 /* Recover the typespec for the expression. This is really only
5889 necessary for generic procedures, where the additional call
5890 to gfc_add_component_ref seems to throw the collection of the
5891 correct typespec. */
5896 /* Resolve a typebound subroutine, or 'method'. First separate all
5897 the non-CLASS references by calling resolve_typebound_call
5901 resolve_typebound_subroutine (gfc_code
*code
)
5903 gfc_symbol
*declared
;
5912 st
= code
->expr1
->symtree
;
5914 /* Deal with typebound operators for CLASS objects. */
5915 expr
= code
->expr1
->value
.compcall
.base_object
;
5916 if (expr
&& expr
->ts
.type
== BT_CLASS
&& code
->expr1
->value
.compcall
.name
)
5918 /* Since the typebound operators are generic, we have to ensure
5919 that any delays in resolution are corrected and that the vtab
5921 declared
= expr
->ts
.u
.derived
;
5922 c
= gfc_find_component (declared
, "_vptr", true, true);
5923 if (c
->ts
.u
.derived
== NULL
)
5924 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
5926 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5929 /* Use the generic name if it is there. */
5930 name
= name
? name
: code
->expr1
->value
.function
.esym
->name
;
5931 code
->expr1
->symtree
= expr
->symtree
;
5932 code
->expr1
->ref
= gfc_copy_ref (expr
->ref
);
5933 gfc_add_vptr_component (code
->expr1
);
5934 gfc_add_component_ref (code
->expr1
, name
);
5935 code
->expr1
->value
.function
.esym
= NULL
;
5940 return resolve_typebound_call (code
, NULL
);
5942 if (resolve_ref (code
->expr1
) == FAILURE
)
5945 /* Get the CLASS declared type. */
5946 get_declared_from_expr (&class_ref
, &new_ref
, code
->expr1
);
5948 /* Weed out cases of the ultimate component being a derived type. */
5949 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5950 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5952 gfc_free_ref_list (new_ref
);
5953 return resolve_typebound_call (code
, NULL
);
5956 if (resolve_typebound_call (code
, &name
) == FAILURE
)
5958 ts
= code
->expr1
->ts
;
5960 /* Then convert the expression to a procedure pointer component call. */
5961 code
->expr1
->value
.function
.esym
= NULL
;
5962 code
->expr1
->symtree
= st
;
5965 code
->expr1
->ref
= new_ref
;
5967 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5968 gfc_add_vptr_component (code
->expr1
);
5969 gfc_add_component_ref (code
->expr1
, name
);
5971 /* Recover the typespec for the expression. This is really only
5972 necessary for generic procedures, where the additional call
5973 to gfc_add_component_ref seems to throw the collection of the
5974 correct typespec. */
5975 code
->expr1
->ts
= ts
;
5980 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5983 resolve_ppc_call (gfc_code
* c
)
5985 gfc_component
*comp
;
5988 b
= gfc_is_proc_ptr_comp (c
->expr1
, &comp
);
5991 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
5992 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5994 if (!comp
->attr
.subroutine
)
5995 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
5997 if (resolve_ref (c
->expr1
) == FAILURE
)
6000 if (update_ppc_arglist (c
->expr1
) == FAILURE
)
6003 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
6005 if (resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
6006 comp
->formal
== NULL
) == FAILURE
)
6009 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
6015 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6018 resolve_expr_ppc (gfc_expr
* e
)
6020 gfc_component
*comp
;
6023 b
= gfc_is_proc_ptr_comp (e
, &comp
);
6026 /* Convert to EXPR_FUNCTION. */
6027 e
->expr_type
= EXPR_FUNCTION
;
6028 e
->value
.function
.isym
= NULL
;
6029 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
6031 if (comp
->as
!= NULL
)
6032 e
->rank
= comp
->as
->rank
;
6034 if (!comp
->attr
.function
)
6035 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
6037 if (resolve_ref (e
) == FAILURE
)
6040 if (resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
6041 comp
->formal
== NULL
) == FAILURE
)
6044 if (update_ppc_arglist (e
) == FAILURE
)
6047 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
6054 gfc_is_expandable_expr (gfc_expr
*e
)
6056 gfc_constructor
*con
;
6058 if (e
->expr_type
== EXPR_ARRAY
)
6060 /* Traverse the constructor looking for variables that are flavor
6061 parameter. Parameters must be expanded since they are fully used at
6063 con
= gfc_constructor_first (e
->value
.constructor
);
6064 for (; con
; con
= gfc_constructor_next (con
))
6066 if (con
->expr
->expr_type
== EXPR_VARIABLE
6067 && con
->expr
->symtree
6068 && (con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
6069 || con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_VARIABLE
))
6071 if (con
->expr
->expr_type
== EXPR_ARRAY
6072 && gfc_is_expandable_expr (con
->expr
))
6080 /* Resolve an expression. That is, make sure that types of operands agree
6081 with their operators, intrinsic operators are converted to function calls
6082 for overloaded types and unresolved function references are resolved. */
6085 gfc_resolve_expr (gfc_expr
*e
)
6093 /* inquiry_argument only applies to variables. */
6094 inquiry_save
= inquiry_argument
;
6095 if (e
->expr_type
!= EXPR_VARIABLE
)
6096 inquiry_argument
= false;
6098 switch (e
->expr_type
)
6101 t
= resolve_operator (e
);
6107 if (check_host_association (e
))
6108 t
= resolve_function (e
);
6111 t
= resolve_variable (e
);
6113 expression_rank (e
);
6116 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
6117 && e
->ref
->type
!= REF_SUBSTRING
)
6118 gfc_resolve_substring_charlen (e
);
6123 t
= resolve_typebound_function (e
);
6126 case EXPR_SUBSTRING
:
6127 t
= resolve_ref (e
);
6136 t
= resolve_expr_ppc (e
);
6141 if (resolve_ref (e
) == FAILURE
)
6144 t
= gfc_resolve_array_constructor (e
);
6145 /* Also try to expand a constructor. */
6148 expression_rank (e
);
6149 if (gfc_is_constant_expr (e
) || gfc_is_expandable_expr (e
))
6150 gfc_expand_constructor (e
, false);
6153 /* This provides the opportunity for the length of constructors with
6154 character valued function elements to propagate the string length
6155 to the expression. */
6156 if (t
== SUCCESS
&& e
->ts
.type
== BT_CHARACTER
)
6158 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6159 here rather then add a duplicate test for it above. */
6160 gfc_expand_constructor (e
, false);
6161 t
= gfc_resolve_character_array_constructor (e
);
6166 case EXPR_STRUCTURE
:
6167 t
= resolve_ref (e
);
6171 t
= resolve_structure_cons (e
, 0);
6175 t
= gfc_simplify_expr (e
, 0);
6179 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6182 if (e
->ts
.type
== BT_CHARACTER
&& t
== SUCCESS
&& !e
->ts
.u
.cl
)
6185 inquiry_argument
= inquiry_save
;
6191 /* Resolve an expression from an iterator. They must be scalar and have
6192 INTEGER or (optionally) REAL type. */
6195 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
6196 const char *name_msgid
)
6198 if (gfc_resolve_expr (expr
) == FAILURE
)
6201 if (expr
->rank
!= 0)
6203 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
6207 if (expr
->ts
.type
!= BT_INTEGER
)
6209 if (expr
->ts
.type
== BT_REAL
)
6212 return gfc_notify_std (GFC_STD_F95_DEL
,
6213 "Deleted feature: %s at %L must be integer",
6214 _(name_msgid
), &expr
->where
);
6217 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
6224 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
6232 /* Resolve the expressions in an iterator structure. If REAL_OK is
6233 false allow only INTEGER type iterators, otherwise allow REAL types. */
6236 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
)
6238 if (gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable")
6242 if (gfc_check_vardef_context (iter
->var
, false, false, _("iterator variable"))
6246 if (gfc_resolve_iterator_expr (iter
->start
, real_ok
,
6247 "Start expression in DO loop") == FAILURE
)
6250 if (gfc_resolve_iterator_expr (iter
->end
, real_ok
,
6251 "End expression in DO loop") == FAILURE
)
6254 if (gfc_resolve_iterator_expr (iter
->step
, real_ok
,
6255 "Step expression in DO loop") == FAILURE
)
6258 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
6260 if ((iter
->step
->ts
.type
== BT_INTEGER
6261 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
6262 || (iter
->step
->ts
.type
== BT_REAL
6263 && mpfr_sgn (iter
->step
->value
.real
) == 0))
6265 gfc_error ("Step expression in DO loop at %L cannot be zero",
6266 &iter
->step
->where
);
6271 /* Convert start, end, and step to the same type as var. */
6272 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
6273 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
6274 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6276 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
6277 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
6278 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6280 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
6281 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
6282 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 2);
6284 if (iter
->start
->expr_type
== EXPR_CONSTANT
6285 && iter
->end
->expr_type
== EXPR_CONSTANT
6286 && iter
->step
->expr_type
== EXPR_CONSTANT
)
6289 if (iter
->start
->ts
.type
== BT_INTEGER
)
6291 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
6292 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
6296 sgn
= mpfr_sgn (iter
->step
->value
.real
);
6297 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
6299 if ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0))
6300 gfc_warning ("DO loop at %L will be executed zero times",
6301 &iter
->step
->where
);
6308 /* Traversal function for find_forall_index. f == 2 signals that
6309 that variable itself is not to be checked - only the references. */
6312 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
6314 if (expr
->expr_type
!= EXPR_VARIABLE
)
6317 /* A scalar assignment */
6318 if (!expr
->ref
|| *f
== 1)
6320 if (expr
->symtree
->n
.sym
== sym
)
6332 /* Check whether the FORALL index appears in the expression or not.
6333 Returns SUCCESS if SYM is found in EXPR. */
6336 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
6338 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
6345 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6346 to be a scalar INTEGER variable. The subscripts and stride are scalar
6347 INTEGERs, and if stride is a constant it must be nonzero.
6348 Furthermore "A subscript or stride in a forall-triplet-spec shall
6349 not contain a reference to any index-name in the
6350 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6353 resolve_forall_iterators (gfc_forall_iterator
*it
)
6355 gfc_forall_iterator
*iter
, *iter2
;
6357 for (iter
= it
; iter
; iter
= iter
->next
)
6359 if (gfc_resolve_expr (iter
->var
) == SUCCESS
6360 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
6361 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6364 if (gfc_resolve_expr (iter
->start
) == SUCCESS
6365 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
6366 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6367 &iter
->start
->where
);
6368 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
6369 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
6371 if (gfc_resolve_expr (iter
->end
) == SUCCESS
6372 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
6373 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6375 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
6376 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
6378 if (gfc_resolve_expr (iter
->stride
) == SUCCESS
)
6380 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
6381 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6382 &iter
->stride
->where
, "INTEGER");
6384 if (iter
->stride
->expr_type
== EXPR_CONSTANT
6385 && mpz_cmp_ui(iter
->stride
->value
.integer
, 0) == 0)
6386 gfc_error ("FORALL stride expression at %L cannot be zero",
6387 &iter
->stride
->where
);
6389 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
6390 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 2);
6393 for (iter
= it
; iter
; iter
= iter
->next
)
6394 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
6396 if (find_forall_index (iter2
->start
,
6397 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6398 || find_forall_index (iter2
->end
,
6399 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
6400 || find_forall_index (iter2
->stride
,
6401 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
)
6402 gfc_error ("FORALL index '%s' may not appear in triplet "
6403 "specification at %L", iter
->var
->symtree
->name
,
6404 &iter2
->start
->where
);
6409 /* Given a pointer to a symbol that is a derived type, see if it's
6410 inaccessible, i.e. if it's defined in another module and the components are
6411 PRIVATE. The search is recursive if necessary. Returns zero if no
6412 inaccessible components are found, nonzero otherwise. */
6415 derived_inaccessible (gfc_symbol
*sym
)
6419 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
6422 for (c
= sym
->components
; c
; c
= c
->next
)
6424 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
6432 /* Resolve the argument of a deallocate expression. The expression must be
6433 a pointer or a full array. */
6436 resolve_deallocate_expr (gfc_expr
*e
)
6438 symbol_attribute attr
;
6439 int allocatable
, pointer
;
6444 if (gfc_resolve_expr (e
) == FAILURE
)
6447 if (e
->expr_type
!= EXPR_VARIABLE
)
6450 sym
= e
->symtree
->n
.sym
;
6452 if (sym
->ts
.type
== BT_CLASS
)
6454 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6455 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6459 allocatable
= sym
->attr
.allocatable
;
6460 pointer
= sym
->attr
.pointer
;
6462 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6467 if (ref
->u
.ar
.type
!= AR_FULL
6468 && !(ref
->u
.ar
.type
== AR_ELEMENT
&& ref
->u
.ar
.as
->rank
== 0
6469 && ref
->u
.ar
.codimen
&& gfc_ref_this_image (ref
)))
6474 c
= ref
->u
.c
.component
;
6475 if (c
->ts
.type
== BT_CLASS
)
6477 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6478 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6482 allocatable
= c
->attr
.allocatable
;
6483 pointer
= c
->attr
.pointer
;
6493 attr
= gfc_expr_attr (e
);
6495 if (allocatable
== 0 && attr
.pointer
== 0)
6498 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6504 if (gfc_is_coindexed (e
))
6506 gfc_error ("Coindexed allocatable object at %L", &e
->where
);
6511 && gfc_check_vardef_context (e
, true, true, _("DEALLOCATE object"))
6514 if (gfc_check_vardef_context (e
, false, true, _("DEALLOCATE object"))
6522 /* Returns true if the expression e contains a reference to the symbol sym. */
6524 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
6526 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
6533 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
6535 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
6539 /* Given the expression node e for an allocatable/pointer of derived type to be
6540 allocated, get the expression node to be initialized afterwards (needed for
6541 derived types with default initializers, and derived types with allocatable
6542 components that need nullification.) */
6545 gfc_expr_to_initialize (gfc_expr
*e
)
6551 result
= gfc_copy_expr (e
);
6553 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6554 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
6555 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
6557 ref
->u
.ar
.type
= AR_FULL
;
6559 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
6560 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
6562 result
->rank
= ref
->u
.ar
.dimen
;
6570 /* If the last ref of an expression is an array ref, return a copy of the
6571 expression with that one removed. Otherwise, a copy of the original
6572 expression. This is used for allocate-expressions and pointer assignment
6573 LHS, where there may be an array specification that needs to be stripped
6574 off when using gfc_check_vardef_context. */
6577 remove_last_array_ref (gfc_expr
* e
)
6582 e2
= gfc_copy_expr (e
);
6583 for (r
= &e2
->ref
; *r
; r
= &(*r
)->next
)
6584 if ((*r
)->type
== REF_ARRAY
&& !(*r
)->next
)
6586 gfc_free_ref_list (*r
);
6595 /* Used in resolve_allocate_expr to check that a allocation-object and
6596 a source-expr are conformable. This does not catch all possible
6597 cases; in particular a runtime checking is needed. */
6600 conformable_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
6603 for (tail
= e2
->ref
; tail
&& tail
->next
; tail
= tail
->next
);
6605 /* First compare rank. */
6606 if (tail
&& e1
->rank
!= tail
->u
.ar
.as
->rank
)
6608 gfc_error ("Source-expr at %L must be scalar or have the "
6609 "same rank as the allocate-object at %L",
6610 &e1
->where
, &e2
->where
);
6621 for (i
= 0; i
< e1
->rank
; i
++)
6623 if (tail
->u
.ar
.end
[i
])
6625 mpz_set (s
, tail
->u
.ar
.end
[i
]->value
.integer
);
6626 mpz_sub (s
, s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6627 mpz_add_ui (s
, s
, 1);
6631 mpz_set (s
, tail
->u
.ar
.start
[i
]->value
.integer
);
6634 if (mpz_cmp (e1
->shape
[i
], s
) != 0)
6636 gfc_error ("Source-expr at %L and allocate-object at %L must "
6637 "have the same shape", &e1
->where
, &e2
->where
);
6650 /* Resolve the expression in an ALLOCATE statement, doing the additional
6651 checks to see whether the expression is OK or not. The expression must
6652 have a trailing array reference that gives the size of the array. */
6655 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
)
6657 int i
, pointer
, allocatable
, dimension
, is_abstract
;
6660 symbol_attribute attr
;
6661 gfc_ref
*ref
, *ref2
;
6664 gfc_symbol
*sym
= NULL
;
6669 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6670 checking of coarrays. */
6671 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6672 if (ref
->next
== NULL
)
6675 if (ref
&& ref
->type
== REF_ARRAY
)
6676 ref
->u
.ar
.in_allocate
= true;
6678 if (gfc_resolve_expr (e
) == FAILURE
)
6681 /* Make sure the expression is allocatable or a pointer. If it is
6682 pointer, the next-to-last reference must be a pointer. */
6686 sym
= e
->symtree
->n
.sym
;
6688 /* Check whether ultimate component is abstract and CLASS. */
6691 if (e
->expr_type
!= EXPR_VARIABLE
)
6694 attr
= gfc_expr_attr (e
);
6695 pointer
= attr
.pointer
;
6696 dimension
= attr
.dimension
;
6697 codimension
= attr
.codimension
;
6701 if (sym
->ts
.type
== BT_CLASS
)
6703 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
6704 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
6705 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
6706 codimension
= CLASS_DATA (sym
)->attr
.codimension
;
6707 is_abstract
= CLASS_DATA (sym
)->attr
.abstract
;
6711 allocatable
= sym
->attr
.allocatable
;
6712 pointer
= sym
->attr
.pointer
;
6713 dimension
= sym
->attr
.dimension
;
6714 codimension
= sym
->attr
.codimension
;
6719 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
6724 if (ref
->u
.ar
.codimen
> 0)
6727 for (n
= ref
->u
.ar
.dimen
;
6728 n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
6729 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_THIS_IMAGE
)
6736 if (ref
->next
!= NULL
)
6744 gfc_error ("Coindexed allocatable object at %L",
6749 c
= ref
->u
.c
.component
;
6750 if (c
->ts
.type
== BT_CLASS
)
6752 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
6753 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
6754 dimension
= CLASS_DATA (c
)->attr
.dimension
;
6755 codimension
= CLASS_DATA (c
)->attr
.codimension
;
6756 is_abstract
= CLASS_DATA (c
)->attr
.abstract
;
6760 allocatable
= c
->attr
.allocatable
;
6761 pointer
= c
->attr
.pointer
;
6762 dimension
= c
->attr
.dimension
;
6763 codimension
= c
->attr
.codimension
;
6764 is_abstract
= c
->attr
.abstract
;
6776 if (allocatable
== 0 && pointer
== 0)
6778 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6783 /* Some checks for the SOURCE tag. */
6786 /* Check F03:C631. */
6787 if (!gfc_type_compatible (&e
->ts
, &code
->expr3
->ts
))
6789 gfc_error ("Type of entity at %L is type incompatible with "
6790 "source-expr at %L", &e
->where
, &code
->expr3
->where
);
6794 /* Check F03:C632 and restriction following Note 6.18. */
6795 if (code
->expr3
->rank
> 0
6796 && conformable_arrays (code
->expr3
, e
) == FAILURE
)
6799 /* Check F03:C633. */
6800 if (code
->expr3
->ts
.kind
!= e
->ts
.kind
)
6802 gfc_error ("The allocate-object at %L and the source-expr at %L "
6803 "shall have the same kind type parameter",
6804 &e
->where
, &code
->expr3
->where
);
6808 /* Check F2008, C642. */
6809 if (code
->expr3
->ts
.type
== BT_DERIVED
6810 && ((codimension
&& gfc_expr_attr (code
->expr3
).lock_comp
)
6811 || (code
->expr3
->ts
.u
.derived
->from_intmod
6812 == INTMOD_ISO_FORTRAN_ENV
6813 && code
->expr3
->ts
.u
.derived
->intmod_sym_id
6814 == ISOFORTRAN_LOCK_TYPE
)))
6816 gfc_error ("The source-expr at %L shall neither be of type "
6817 "LOCK_TYPE nor have a LOCK_TYPE component if "
6818 "allocate-object at %L is a coarray",
6819 &code
->expr3
->where
, &e
->where
);
6824 /* Check F08:C629. */
6825 if (is_abstract
&& code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
6828 gcc_assert (e
->ts
.type
== BT_CLASS
);
6829 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6830 "type-spec or source-expr", sym
->name
, &e
->where
);
6834 /* In the variable definition context checks, gfc_expr_attr is used
6835 on the expression. This is fooled by the array specification
6836 present in e, thus we have to eliminate that one temporarily. */
6837 e2
= remove_last_array_ref (e
);
6839 if (t
== SUCCESS
&& pointer
)
6840 t
= gfc_check_vardef_context (e2
, true, true, _("ALLOCATE object"));
6842 t
= gfc_check_vardef_context (e2
, false, true, _("ALLOCATE object"));
6849 /* Set up default initializer if needed. */
6853 if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6854 ts
= code
->ext
.alloc
.ts
;
6858 if (ts
.type
== BT_CLASS
)
6859 ts
= ts
.u
.derived
->components
->ts
;
6861 if (ts
.type
== BT_DERIVED
&& (init_e
= gfc_default_initializer (&ts
)))
6863 gfc_code
*init_st
= gfc_get_code ();
6864 init_st
->loc
= code
->loc
;
6865 init_st
->op
= EXEC_INIT_ASSIGN
;
6866 init_st
->expr1
= gfc_expr_to_initialize (e
);
6867 init_st
->expr2
= init_e
;
6868 init_st
->next
= code
->next
;
6869 code
->next
= init_st
;
6872 else if (code
->expr3
->mold
&& code
->expr3
->ts
.type
== BT_DERIVED
)
6874 /* Default initialization via MOLD (non-polymorphic). */
6875 gfc_expr
*rhs
= gfc_default_initializer (&code
->expr3
->ts
);
6876 gfc_resolve_expr (rhs
);
6877 gfc_free_expr (code
->expr3
);
6881 if (e
->ts
.type
== BT_CLASS
)
6883 /* Make sure the vtab symbol is present when
6884 the module variables are generated. */
6885 gfc_typespec ts
= e
->ts
;
6887 ts
= code
->expr3
->ts
;
6888 else if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
6889 ts
= code
->ext
.alloc
.ts
;
6890 gfc_find_derived_vtab (ts
.u
.derived
);
6893 if (dimension
== 0 && codimension
== 0)
6896 /* Make sure the last reference node is an array specifiction. */
6898 if (!ref2
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
6899 || (dimension
&& ref2
->u
.ar
.dimen
== 0))
6901 gfc_error ("Array specification required in ALLOCATE statement "
6902 "at %L", &e
->where
);
6906 /* Make sure that the array section reference makes sense in the
6907 context of an ALLOCATE specification. */
6912 for (i
= ar
->dimen
; i
< ar
->dimen
+ ar
->codimen
; i
++)
6913 if (ar
->dimen_type
[i
] == DIMEN_THIS_IMAGE
)
6915 gfc_error ("Coarray specification required in ALLOCATE statement "
6916 "at %L", &e
->where
);
6920 for (i
= 0; i
< ar
->dimen
; i
++)
6922 if (ref2
->u
.ar
.type
== AR_ELEMENT
)
6925 switch (ar
->dimen_type
[i
])
6931 if (ar
->start
[i
] != NULL
6932 && ar
->end
[i
] != NULL
6933 && ar
->stride
[i
] == NULL
)
6936 /* Fall Through... */
6941 case DIMEN_THIS_IMAGE
:
6942 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6948 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6950 sym
= a
->expr
->symtree
->n
.sym
;
6952 /* TODO - check derived type components. */
6953 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
6956 if ((ar
->start
[i
] != NULL
6957 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
6958 || (ar
->end
[i
] != NULL
6959 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
6961 gfc_error ("'%s' must not appear in the array specification at "
6962 "%L in the same ALLOCATE statement where it is "
6963 "itself allocated", sym
->name
, &ar
->where
);
6969 for (i
= ar
->dimen
; i
< ar
->codimen
+ ar
->dimen
; i
++)
6971 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
6972 || ar
->dimen_type
[i
] == DIMEN_RANGE
)
6974 if (i
== (ar
->dimen
+ ar
->codimen
- 1))
6976 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6977 "statement at %L", &e
->where
);
6983 if (ar
->dimen_type
[i
] == DIMEN_STAR
&& i
== (ar
->dimen
+ ar
->codimen
- 1)
6984 && ar
->stride
[i
] == NULL
)
6987 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7000 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
7002 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
7003 gfc_alloc
*a
, *p
, *q
;
7006 errmsg
= code
->expr2
;
7008 /* Check the stat variable. */
7011 gfc_check_vardef_context (stat
, false, false, _("STAT variable"));
7013 if ((stat
->ts
.type
!= BT_INTEGER
7014 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
7015 || stat
->ref
->type
== REF_COMPONENT
)))
7017 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7018 "variable", &stat
->where
);
7020 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
7021 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
7023 gfc_ref
*ref1
, *ref2
;
7026 for (ref1
= p
->expr
->ref
, ref2
= stat
->ref
; ref1
&& ref2
;
7027 ref1
= ref1
->next
, ref2
= ref2
->next
)
7029 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
7031 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
7040 gfc_error ("Stat-variable at %L shall not be %sd within "
7041 "the same %s statement", &stat
->where
, fcn
, fcn
);
7047 /* Check the errmsg variable. */
7051 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7054 gfc_check_vardef_context (errmsg
, false, false, _("ERRMSG variable"));
7056 if ((errmsg
->ts
.type
!= BT_CHARACTER
7058 && (errmsg
->ref
->type
== REF_ARRAY
7059 || errmsg
->ref
->type
== REF_COMPONENT
)))
7060 || errmsg
->rank
> 0 )
7061 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7062 "variable", &errmsg
->where
);
7064 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
7065 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
7067 gfc_ref
*ref1
, *ref2
;
7070 for (ref1
= p
->expr
->ref
, ref2
= errmsg
->ref
; ref1
&& ref2
;
7071 ref1
= ref1
->next
, ref2
= ref2
->next
)
7073 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
7075 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
7084 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7085 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
7091 /* Check that an allocate-object appears only once in the statement.
7092 FIXME: Checking derived types is disabled. */
7093 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
7096 for (q
= p
->next
; q
; q
= q
->next
)
7099 if (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
)
7101 /* This is a potential collision. */
7102 gfc_ref
*pr
= pe
->ref
;
7103 gfc_ref
*qr
= qe
->ref
;
7105 /* Follow the references until
7106 a) They start to differ, in which case there is no error;
7107 you can deallocate a%b and a%c in a single statement
7108 b) Both of them stop, which is an error
7109 c) One of them stops, which is also an error. */
7112 if (pr
== NULL
&& qr
== NULL
)
7114 gfc_error ("Allocate-object at %L also appears at %L",
7115 &pe
->where
, &qe
->where
);
7118 else if (pr
!= NULL
&& qr
== NULL
)
7120 gfc_error ("Allocate-object at %L is subobject of"
7121 " object at %L", &pe
->where
, &qe
->where
);
7124 else if (pr
== NULL
&& qr
!= NULL
)
7126 gfc_error ("Allocate-object at %L is subobject of"
7127 " object at %L", &qe
->where
, &pe
->where
);
7130 /* Here, pr != NULL && qr != NULL */
7131 gcc_assert(pr
->type
== qr
->type
);
7132 if (pr
->type
== REF_ARRAY
)
7134 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7136 gcc_assert (qr
->type
== REF_ARRAY
);
7138 if (pr
->next
&& qr
->next
)
7140 gfc_array_ref
*par
= &(pr
->u
.ar
);
7141 gfc_array_ref
*qar
= &(qr
->u
.ar
);
7142 if (gfc_dep_compare_expr (par
->start
[0],
7143 qar
->start
[0]) != 0)
7149 if (pr
->u
.c
.component
->name
!= qr
->u
.c
.component
->name
)
7160 if (strcmp (fcn
, "ALLOCATE") == 0)
7162 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
7163 resolve_allocate_expr (a
->expr
, code
);
7167 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
7168 resolve_deallocate_expr (a
->expr
);
7173 /************ SELECT CASE resolution subroutines ************/
7175 /* Callback function for our mergesort variant. Determines interval
7176 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7177 op1 > op2. Assumes we're not dealing with the default case.
7178 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7179 There are nine situations to check. */
7182 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
7186 if (op1
->low
== NULL
) /* op1 = (:L) */
7188 /* op2 = (:N), so overlap. */
7190 /* op2 = (M:) or (M:N), L < M */
7191 if (op2
->low
!= NULL
7192 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7195 else if (op1
->high
== NULL
) /* op1 = (K:) */
7197 /* op2 = (M:), so overlap. */
7199 /* op2 = (:N) or (M:N), K > N */
7200 if (op2
->high
!= NULL
7201 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7204 else /* op1 = (K:L) */
7206 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
7207 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7209 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
7210 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7212 else /* op2 = (M:N) */
7216 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
7219 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
7228 /* Merge-sort a double linked case list, detecting overlap in the
7229 process. LIST is the head of the double linked case list before it
7230 is sorted. Returns the head of the sorted list if we don't see any
7231 overlap, or NULL otherwise. */
7234 check_case_overlap (gfc_case
*list
)
7236 gfc_case
*p
, *q
, *e
, *tail
;
7237 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
7239 /* If the passed list was empty, return immediately. */
7246 /* Loop unconditionally. The only exit from this loop is a return
7247 statement, when we've finished sorting the case list. */
7254 /* Count the number of merges we do in this pass. */
7257 /* Loop while there exists a merge to be done. */
7262 /* Count this merge. */
7265 /* Cut the list in two pieces by stepping INSIZE places
7266 forward in the list, starting from P. */
7269 for (i
= 0; i
< insize
; i
++)
7278 /* Now we have two lists. Merge them! */
7279 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
7281 /* See from which the next case to merge comes from. */
7284 /* P is empty so the next case must come from Q. */
7289 else if (qsize
== 0 || q
== NULL
)
7298 cmp
= compare_cases (p
, q
);
7301 /* The whole case range for P is less than the
7309 /* The whole case range for Q is greater than
7310 the case range for P. */
7317 /* The cases overlap, or they are the same
7318 element in the list. Either way, we must
7319 issue an error and get the next case from P. */
7320 /* FIXME: Sort P and Q by line number. */
7321 gfc_error ("CASE label at %L overlaps with CASE "
7322 "label at %L", &p
->where
, &q
->where
);
7330 /* Add the next element to the merged list. */
7339 /* P has now stepped INSIZE places along, and so has Q. So
7340 they're the same. */
7345 /* If we have done only one merge or none at all, we've
7346 finished sorting the cases. */
7355 /* Otherwise repeat, merging lists twice the size. */
7361 /* Check to see if an expression is suitable for use in a CASE statement.
7362 Makes sure that all case expressions are scalar constants of the same
7363 type. Return FAILURE if anything is wrong. */
7366 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
7368 if (e
== NULL
) return SUCCESS
;
7370 if (e
->ts
.type
!= case_expr
->ts
.type
)
7372 gfc_error ("Expression in CASE statement at %L must be of type %s",
7373 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
7377 /* C805 (R808) For a given case-construct, each case-value shall be of
7378 the same type as case-expr. For character type, length differences
7379 are allowed, but the kind type parameters shall be the same. */
7381 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
7383 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7384 &e
->where
, case_expr
->ts
.kind
);
7388 /* Convert the case value kind to that of case expression kind,
7391 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
7392 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
7396 gfc_error ("Expression in CASE statement at %L must be scalar",
7405 /* Given a completely parsed select statement, we:
7407 - Validate all expressions and code within the SELECT.
7408 - Make sure that the selection expression is not of the wrong type.
7409 - Make sure that no case ranges overlap.
7410 - Eliminate unreachable cases and unreachable code resulting from
7411 removing case labels.
7413 The standard does allow unreachable cases, e.g. CASE (5:3). But
7414 they are a hassle for code generation, and to prevent that, we just
7415 cut them out here. This is not necessary for overlapping cases
7416 because they are illegal and we never even try to generate code.
7418 We have the additional caveat that a SELECT construct could have
7419 been a computed GOTO in the source code. Fortunately we can fairly
7420 easily work around that here: The case_expr for a "real" SELECT CASE
7421 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7422 we have to do is make sure that the case_expr is a scalar integer
7426 resolve_select (gfc_code
*code
)
7429 gfc_expr
*case_expr
;
7430 gfc_case
*cp
, *default_case
, *tail
, *head
;
7431 int seen_unreachable
;
7437 if (code
->expr1
== NULL
)
7439 /* This was actually a computed GOTO statement. */
7440 case_expr
= code
->expr2
;
7441 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
7442 gfc_error ("Selection expression in computed GOTO statement "
7443 "at %L must be a scalar integer expression",
7446 /* Further checking is not necessary because this SELECT was built
7447 by the compiler, so it should always be OK. Just move the
7448 case_expr from expr2 to expr so that we can handle computed
7449 GOTOs as normal SELECTs from here on. */
7450 code
->expr1
= code
->expr2
;
7455 case_expr
= code
->expr1
;
7457 type
= case_expr
->ts
.type
;
7458 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
7460 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7461 &case_expr
->where
, gfc_typename (&case_expr
->ts
));
7463 /* Punt. Going on here just produce more garbage error messages. */
7467 if (case_expr
->rank
!= 0)
7469 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7470 "expression", &case_expr
->where
);
7477 /* Raise a warning if an INTEGER case value exceeds the range of
7478 the case-expr. Later, all expressions will be promoted to the
7479 largest kind of all case-labels. */
7481 if (type
== BT_INTEGER
)
7482 for (body
= code
->block
; body
; body
= body
->block
)
7483 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7486 && gfc_check_integer_range (cp
->low
->value
.integer
,
7487 case_expr
->ts
.kind
) != ARITH_OK
)
7488 gfc_warning ("Expression in CASE statement at %L is "
7489 "not in the range of %s", &cp
->low
->where
,
7490 gfc_typename (&case_expr
->ts
));
7493 && cp
->low
!= cp
->high
7494 && gfc_check_integer_range (cp
->high
->value
.integer
,
7495 case_expr
->ts
.kind
) != ARITH_OK
)
7496 gfc_warning ("Expression in CASE statement at %L is "
7497 "not in the range of %s", &cp
->high
->where
,
7498 gfc_typename (&case_expr
->ts
));
7501 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7502 of the SELECT CASE expression and its CASE values. Walk the lists
7503 of case values, and if we find a mismatch, promote case_expr to
7504 the appropriate kind. */
7506 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
7508 for (body
= code
->block
; body
; body
= body
->block
)
7510 /* Walk the case label list. */
7511 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7513 /* Intercept the DEFAULT case. It does not have a kind. */
7514 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7517 /* Unreachable case ranges are discarded, so ignore. */
7518 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7519 && cp
->low
!= cp
->high
7520 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7524 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
7525 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
7527 if (cp
->high
!= NULL
7528 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
7529 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
7534 /* Assume there is no DEFAULT case. */
7535 default_case
= NULL
;
7540 for (body
= code
->block
; body
; body
= body
->block
)
7542 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7544 seen_unreachable
= 0;
7546 /* Walk the case label list, making sure that all case labels
7548 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
7550 /* Count the number of cases in the whole construct. */
7553 /* Intercept the DEFAULT case. */
7554 if (cp
->low
== NULL
&& cp
->high
== NULL
)
7556 if (default_case
!= NULL
)
7558 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7559 "by a second DEFAULT CASE at %L",
7560 &default_case
->where
, &cp
->where
);
7571 /* Deal with single value cases and case ranges. Errors are
7572 issued from the validation function. */
7573 if (validate_case_label_expr (cp
->low
, case_expr
) != SUCCESS
7574 || validate_case_label_expr (cp
->high
, case_expr
) != SUCCESS
)
7580 if (type
== BT_LOGICAL
7581 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
7582 || cp
->low
!= cp
->high
))
7584 gfc_error ("Logical range in CASE statement at %L is not "
7585 "allowed", &cp
->low
->where
);
7590 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
7593 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
7594 if (value
& seen_logical
)
7596 gfc_error ("Constant logical value in CASE statement "
7597 "is repeated at %L",
7602 seen_logical
|= value
;
7605 if (cp
->low
!= NULL
&& cp
->high
!= NULL
7606 && cp
->low
!= cp
->high
7607 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
7609 if (gfc_option
.warn_surprising
)
7610 gfc_warning ("Range specification at %L can never "
7611 "be matched", &cp
->where
);
7613 cp
->unreachable
= 1;
7614 seen_unreachable
= 1;
7618 /* If the case range can be matched, it can also overlap with
7619 other cases. To make sure it does not, we put it in a
7620 double linked list here. We sort that with a merge sort
7621 later on to detect any overlapping cases. */
7625 head
->right
= head
->left
= NULL
;
7630 tail
->right
->left
= tail
;
7637 /* It there was a failure in the previous case label, give up
7638 for this case label list. Continue with the next block. */
7642 /* See if any case labels that are unreachable have been seen.
7643 If so, we eliminate them. This is a bit of a kludge because
7644 the case lists for a single case statement (label) is a
7645 single forward linked lists. */
7646 if (seen_unreachable
)
7648 /* Advance until the first case in the list is reachable. */
7649 while (body
->ext
.block
.case_list
!= NULL
7650 && body
->ext
.block
.case_list
->unreachable
)
7652 gfc_case
*n
= body
->ext
.block
.case_list
;
7653 body
->ext
.block
.case_list
= body
->ext
.block
.case_list
->next
;
7655 gfc_free_case_list (n
);
7658 /* Strip all other unreachable cases. */
7659 if (body
->ext
.block
.case_list
)
7661 for (cp
= body
->ext
.block
.case_list
; cp
->next
; cp
= cp
->next
)
7663 if (cp
->next
->unreachable
)
7665 gfc_case
*n
= cp
->next
;
7666 cp
->next
= cp
->next
->next
;
7668 gfc_free_case_list (n
);
7675 /* See if there were overlapping cases. If the check returns NULL,
7676 there was overlap. In that case we don't do anything. If head
7677 is non-NULL, we prepend the DEFAULT case. The sorted list can
7678 then used during code generation for SELECT CASE constructs with
7679 a case expression of a CHARACTER type. */
7682 head
= check_case_overlap (head
);
7684 /* Prepend the default_case if it is there. */
7685 if (head
!= NULL
&& default_case
)
7687 default_case
->left
= NULL
;
7688 default_case
->right
= head
;
7689 head
->left
= default_case
;
7693 /* Eliminate dead blocks that may be the result if we've seen
7694 unreachable case labels for a block. */
7695 for (body
= code
; body
&& body
->block
; body
= body
->block
)
7697 if (body
->block
->ext
.block
.case_list
== NULL
)
7699 /* Cut the unreachable block from the code chain. */
7700 gfc_code
*c
= body
->block
;
7701 body
->block
= c
->block
;
7703 /* Kill the dead block, but not the blocks below it. */
7705 gfc_free_statements (c
);
7709 /* More than two cases is legal but insane for logical selects.
7710 Issue a warning for it. */
7711 if (gfc_option
.warn_surprising
&& type
== BT_LOGICAL
7713 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7718 /* Check if a derived type is extensible. */
7721 gfc_type_is_extensible (gfc_symbol
*sym
)
7723 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
);
7727 /* Resolve an associate name: Resolve target and ensure the type-spec is
7728 correct as well as possibly the array-spec. */
7731 resolve_assoc_var (gfc_symbol
* sym
, bool resolve_target
)
7735 gcc_assert (sym
->assoc
);
7736 gcc_assert (sym
->attr
.flavor
== FL_VARIABLE
);
7738 /* If this is for SELECT TYPE, the target may not yet be set. In that
7739 case, return. Resolution will be called later manually again when
7741 target
= sym
->assoc
->target
;
7744 gcc_assert (!sym
->assoc
->dangling
);
7746 if (resolve_target
&& gfc_resolve_expr (target
) != SUCCESS
)
7749 /* For variable targets, we get some attributes from the target. */
7750 if (target
->expr_type
== EXPR_VARIABLE
)
7754 gcc_assert (target
->symtree
);
7755 tsym
= target
->symtree
->n
.sym
;
7757 sym
->attr
.asynchronous
= tsym
->attr
.asynchronous
;
7758 sym
->attr
.volatile_
= tsym
->attr
.volatile_
;
7760 sym
->attr
.target
= (tsym
->attr
.target
|| tsym
->attr
.pointer
);
7763 /* Get type if this was not already set. Note that it can be
7764 some other type than the target in case this is a SELECT TYPE
7765 selector! So we must not update when the type is already there. */
7766 if (sym
->ts
.type
== BT_UNKNOWN
)
7767 sym
->ts
= target
->ts
;
7768 gcc_assert (sym
->ts
.type
!= BT_UNKNOWN
);
7770 /* See if this is a valid association-to-variable. */
7771 sym
->assoc
->variable
= (target
->expr_type
== EXPR_VARIABLE
7772 && !gfc_has_vector_subscript (target
));
7774 /* Finally resolve if this is an array or not. */
7775 if (sym
->attr
.dimension
&& target
->rank
== 0)
7777 gfc_error ("Associate-name '%s' at %L is used as array",
7778 sym
->name
, &sym
->declared_at
);
7779 sym
->attr
.dimension
= 0;
7782 if (target
->rank
> 0)
7783 sym
->attr
.dimension
= 1;
7785 if (sym
->attr
.dimension
)
7787 sym
->as
= gfc_get_array_spec ();
7788 sym
->as
->rank
= target
->rank
;
7789 sym
->as
->type
= AS_DEFERRED
;
7791 /* Target must not be coindexed, thus the associate-variable
7793 sym
->as
->corank
= 0;
7798 /* Resolve a SELECT TYPE statement. */
7801 resolve_select_type (gfc_code
*code
, gfc_namespace
*old_ns
)
7803 gfc_symbol
*selector_type
;
7804 gfc_code
*body
, *new_st
, *if_st
, *tail
;
7805 gfc_code
*class_is
= NULL
, *default_case
= NULL
;
7808 char name
[GFC_MAX_SYMBOL_LEN
];
7812 ns
= code
->ext
.block
.ns
;
7815 /* Check for F03:C813. */
7816 if (code
->expr1
->ts
.type
!= BT_CLASS
7817 && !(code
->expr2
&& code
->expr2
->ts
.type
== BT_CLASS
))
7819 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7820 "at %L", &code
->loc
);
7826 if (code
->expr1
->symtree
->n
.sym
->attr
.untyped
)
7827 code
->expr1
->symtree
->n
.sym
->ts
= code
->expr2
->ts
;
7828 selector_type
= CLASS_DATA (code
->expr2
)->ts
.u
.derived
;
7831 selector_type
= CLASS_DATA (code
->expr1
)->ts
.u
.derived
;
7833 /* Loop over TYPE IS / CLASS IS cases. */
7834 for (body
= code
->block
; body
; body
= body
->block
)
7836 c
= body
->ext
.block
.case_list
;
7838 /* Check F03:C815. */
7839 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7840 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
7842 gfc_error ("Derived type '%s' at %L must be extensible",
7843 c
->ts
.u
.derived
->name
, &c
->where
);
7848 /* Check F03:C816. */
7849 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7850 && !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
))
7852 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7853 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
7858 /* Intercept the DEFAULT case. */
7859 if (c
->ts
.type
== BT_UNKNOWN
)
7861 /* Check F03:C818. */
7864 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7865 "by a second DEFAULT CASE at %L",
7866 &default_case
->ext
.block
.case_list
->where
, &c
->where
);
7871 default_case
= body
;
7878 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7879 target if present. If there are any EXIT statements referring to the
7880 SELECT TYPE construct, this is no problem because the gfc_code
7881 reference stays the same and EXIT is equally possible from the BLOCK
7882 it is changed to. */
7883 code
->op
= EXEC_BLOCK
;
7886 gfc_association_list
* assoc
;
7888 assoc
= gfc_get_association_list ();
7889 assoc
->st
= code
->expr1
->symtree
;
7890 assoc
->target
= gfc_copy_expr (code
->expr2
);
7891 /* assoc->variable will be set by resolve_assoc_var. */
7893 code
->ext
.block
.assoc
= assoc
;
7894 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
7896 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
7899 code
->ext
.block
.assoc
= NULL
;
7901 /* Add EXEC_SELECT to switch on type. */
7902 new_st
= gfc_get_code ();
7903 new_st
->op
= code
->op
;
7904 new_st
->expr1
= code
->expr1
;
7905 new_st
->expr2
= code
->expr2
;
7906 new_st
->block
= code
->block
;
7907 code
->expr1
= code
->expr2
= NULL
;
7912 ns
->code
->next
= new_st
;
7914 code
->op
= EXEC_SELECT
;
7915 gfc_add_vptr_component (code
->expr1
);
7916 gfc_add_hash_component (code
->expr1
);
7918 /* Loop over TYPE IS / CLASS IS cases. */
7919 for (body
= code
->block
; body
; body
= body
->block
)
7921 c
= body
->ext
.block
.case_list
;
7923 if (c
->ts
.type
== BT_DERIVED
)
7924 c
->low
= c
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
7925 c
->ts
.u
.derived
->hash_value
);
7927 else if (c
->ts
.type
== BT_UNKNOWN
)
7930 /* Associate temporary to selector. This should only be done
7931 when this case is actually true, so build a new ASSOCIATE
7932 that does precisely this here (instead of using the
7935 if (c
->ts
.type
== BT_CLASS
)
7936 sprintf (name
, "__tmp_class_%s", c
->ts
.u
.derived
->name
);
7938 sprintf (name
, "__tmp_type_%s", c
->ts
.u
.derived
->name
);
7939 st
= gfc_find_symtree (ns
->sym_root
, name
);
7940 gcc_assert (st
->n
.sym
->assoc
);
7941 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (code
->expr1
->symtree
);
7942 if (c
->ts
.type
== BT_DERIVED
)
7943 gfc_add_data_component (st
->n
.sym
->assoc
->target
);
7945 new_st
= gfc_get_code ();
7946 new_st
->op
= EXEC_BLOCK
;
7947 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
7948 new_st
->ext
.block
.ns
->code
= body
->next
;
7949 body
->next
= new_st
;
7951 /* Chain in the new list only if it is marked as dangling. Otherwise
7952 there is a CASE label overlap and this is already used. Just ignore,
7953 the error is diagonsed elsewhere. */
7954 if (st
->n
.sym
->assoc
->dangling
)
7956 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
7957 st
->n
.sym
->assoc
->dangling
= 0;
7960 resolve_assoc_var (st
->n
.sym
, false);
7963 /* Take out CLASS IS cases for separate treatment. */
7965 while (body
&& body
->block
)
7967 if (body
->block
->ext
.block
.case_list
->ts
.type
== BT_CLASS
)
7969 /* Add to class_is list. */
7970 if (class_is
== NULL
)
7972 class_is
= body
->block
;
7977 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
7978 tail
->block
= body
->block
;
7981 /* Remove from EXEC_SELECT list. */
7982 body
->block
= body
->block
->block
;
7995 /* Add a default case to hold the CLASS IS cases. */
7996 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
7997 tail
->block
= gfc_get_code ();
7999 tail
->op
= EXEC_SELECT_TYPE
;
8000 tail
->ext
.block
.case_list
= gfc_get_case ();
8001 tail
->ext
.block
.case_list
->ts
.type
= BT_UNKNOWN
;
8003 default_case
= tail
;
8006 /* More than one CLASS IS block? */
8007 if (class_is
->block
)
8011 /* Sort CLASS IS blocks by extension level. */
8015 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
8018 /* F03:C817 (check for doubles). */
8019 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->hash_value
8020 == c2
->ext
.block
.case_list
->ts
.u
.derived
->hash_value
)
8022 gfc_error ("Double CLASS IS block in SELECT TYPE "
8024 &c2
->ext
.block
.case_list
->where
);
8027 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
8028 < c2
->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
)
8031 (*c1
)->block
= c2
->block
;
8041 /* Generate IF chain. */
8042 if_st
= gfc_get_code ();
8043 if_st
->op
= EXEC_IF
;
8045 for (body
= class_is
; body
; body
= body
->block
)
8047 new_st
->block
= gfc_get_code ();
8048 new_st
= new_st
->block
;
8049 new_st
->op
= EXEC_IF
;
8050 /* Set up IF condition: Call _gfortran_is_extension_of. */
8051 new_st
->expr1
= gfc_get_expr ();
8052 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
8053 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
8054 new_st
->expr1
->ts
.kind
= 4;
8055 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
8056 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
8057 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
8058 /* Set up arguments. */
8059 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
8060 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (code
->expr1
->symtree
);
8061 new_st
->expr1
->value
.function
.actual
->expr
->where
= code
->loc
;
8062 gfc_add_vptr_component (new_st
->expr1
->value
.function
.actual
->expr
);
8063 vtab
= gfc_find_derived_vtab (body
->ext
.block
.case_list
->ts
.u
.derived
);
8064 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
8065 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
8066 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
8067 new_st
->next
= body
->next
;
8069 if (default_case
->next
)
8071 new_st
->block
= gfc_get_code ();
8072 new_st
= new_st
->block
;
8073 new_st
->op
= EXEC_IF
;
8074 new_st
->next
= default_case
->next
;
8077 /* Replace CLASS DEFAULT code by the IF chain. */
8078 default_case
->next
= if_st
;
8081 /* Resolve the internal code. This can not be done earlier because
8082 it requires that the sym->assoc of selectors is set already. */
8083 gfc_current_ns
= ns
;
8084 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
8085 gfc_current_ns
= old_ns
;
8087 resolve_select (code
);
8091 /* Resolve a transfer statement. This is making sure that:
8092 -- a derived type being transferred has only non-pointer components
8093 -- a derived type being transferred doesn't have private components, unless
8094 it's being transferred from the module where the type was defined
8095 -- we're not trying to transfer a whole assumed size array. */
8098 resolve_transfer (gfc_code
*code
)
8107 while (exp
!= NULL
&& exp
->expr_type
== EXPR_OP
8108 && exp
->value
.op
.op
== INTRINSIC_PARENTHESES
)
8109 exp
= exp
->value
.op
.op1
;
8111 if (exp
== NULL
|| (exp
->expr_type
!= EXPR_VARIABLE
8112 && exp
->expr_type
!= EXPR_FUNCTION
))
8115 /* If we are reading, the variable will be changed. Note that
8116 code->ext.dt may be NULL if the TRANSFER is related to
8117 an INQUIRE statement -- but in this case, we are not reading, either. */
8118 if (code
->ext
.dt
&& code
->ext
.dt
->dt_io_kind
->value
.iokind
== M_READ
8119 && gfc_check_vardef_context (exp
, false, false, _("item in READ"))
8123 sym
= exp
->symtree
->n
.sym
;
8126 /* Go to actual component transferred. */
8127 for (ref
= exp
->ref
; ref
; ref
= ref
->next
)
8128 if (ref
->type
== REF_COMPONENT
)
8129 ts
= &ref
->u
.c
.component
->ts
;
8131 if (ts
->type
== BT_CLASS
)
8133 /* FIXME: Test for defined input/output. */
8134 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8135 "it is processed by a defined input/output procedure",
8140 if (ts
->type
== BT_DERIVED
)
8142 /* Check that transferred derived type doesn't contain POINTER
8144 if (ts
->u
.derived
->attr
.pointer_comp
)
8146 gfc_error ("Data transfer element at %L cannot have POINTER "
8147 "components unless it is processed by a defined "
8148 "input/output procedure", &code
->loc
);
8153 if (ts
->u
.derived
->attr
.proc_pointer_comp
)
8155 gfc_error ("Data transfer element at %L cannot have "
8156 "procedure pointer components", &code
->loc
);
8160 if (ts
->u
.derived
->attr
.alloc_comp
)
8162 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8163 "components unless it is processed by a defined "
8164 "input/output procedure", &code
->loc
);
8168 if (derived_inaccessible (ts
->u
.derived
))
8170 gfc_error ("Data transfer element at %L cannot have "
8171 "PRIVATE components",&code
->loc
);
8176 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
8177 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
8179 gfc_error ("Data transfer element at %L cannot be a full reference to "
8180 "an assumed-size array", &code
->loc
);
8186 /*********** Toplevel code resolution subroutines ***********/
8188 /* Find the set of labels that are reachable from this block. We also
8189 record the last statement in each block. */
8192 find_reachable_labels (gfc_code
*block
)
8199 cs_base
->reachable_labels
= bitmap_obstack_alloc (&labels_obstack
);
8201 /* Collect labels in this block. We don't keep those corresponding
8202 to END {IF|SELECT}, these are checked in resolve_branch by going
8203 up through the code_stack. */
8204 for (c
= block
; c
; c
= c
->next
)
8206 if (c
->here
&& c
->op
!= EXEC_END_NESTED_BLOCK
)
8207 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
8210 /* Merge with labels from parent block. */
8213 gcc_assert (cs_base
->prev
->reachable_labels
);
8214 bitmap_ior_into (cs_base
->reachable_labels
,
8215 cs_base
->prev
->reachable_labels
);
8221 resolve_lock_unlock (gfc_code
*code
)
8223 if (code
->expr1
->ts
.type
!= BT_DERIVED
8224 || code
->expr1
->expr_type
!= EXPR_VARIABLE
8225 || code
->expr1
->ts
.u
.derived
->from_intmod
!= INTMOD_ISO_FORTRAN_ENV
8226 || code
->expr1
->ts
.u
.derived
->intmod_sym_id
!= ISOFORTRAN_LOCK_TYPE
8227 || code
->expr1
->rank
!= 0
8228 || (!gfc_is_coarray (code
->expr1
) && !gfc_is_coindexed (code
->expr1
)))
8229 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8230 &code
->expr1
->where
);
8234 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
8235 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
8236 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8237 &code
->expr2
->where
);
8240 && gfc_check_vardef_context (code
->expr2
, false, false,
8241 _("STAT variable")) == FAILURE
)
8246 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
8247 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
8248 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8249 &code
->expr3
->where
);
8252 && gfc_check_vardef_context (code
->expr3
, false, false,
8253 _("ERRMSG variable")) == FAILURE
)
8256 /* Check ACQUIRED_LOCK. */
8258 && (code
->expr4
->ts
.type
!= BT_LOGICAL
|| code
->expr4
->rank
!= 0
8259 || code
->expr4
->expr_type
!= EXPR_VARIABLE
))
8260 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8261 "variable", &code
->expr4
->where
);
8264 && gfc_check_vardef_context (code
->expr4
, false, false,
8265 _("ACQUIRED_LOCK variable")) == FAILURE
)
8271 resolve_sync (gfc_code
*code
)
8273 /* Check imageset. The * case matches expr1 == NULL. */
8276 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
8277 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8278 "INTEGER expression", &code
->expr1
->where
);
8279 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
8280 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
8281 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8282 &code
->expr1
->where
);
8283 else if (code
->expr1
->expr_type
== EXPR_ARRAY
8284 && gfc_simplify_expr (code
->expr1
, 0) == SUCCESS
)
8286 gfc_constructor
*cons
;
8287 cons
= gfc_constructor_first (code
->expr1
->value
.constructor
);
8288 for (; cons
; cons
= gfc_constructor_next (cons
))
8289 if (cons
->expr
->expr_type
== EXPR_CONSTANT
8290 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
8291 gfc_error ("Imageset argument at %L must between 1 and "
8292 "num_images()", &cons
->expr
->where
);
8298 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
8299 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
8300 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8301 &code
->expr2
->where
);
8305 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
8306 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
8307 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8308 &code
->expr3
->where
);
8312 /* Given a branch to a label, see if the branch is conforming.
8313 The code node describes where the branch is located. */
8316 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
8323 /* Step one: is this a valid branching target? */
8325 if (label
->defined
== ST_LABEL_UNKNOWN
)
8327 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
8332 if (label
->defined
!= ST_LABEL_TARGET
)
8334 gfc_error ("Statement at %L is not a valid branch target statement "
8335 "for the branch statement at %L", &label
->where
, &code
->loc
);
8339 /* Step two: make sure this branch is not a branch to itself ;-) */
8341 if (code
->here
== label
)
8343 gfc_warning ("Branch at %L may result in an infinite loop", &code
->loc
);
8347 /* Step three: See if the label is in the same block as the
8348 branching statement. The hard work has been done by setting up
8349 the bitmap reachable_labels. */
8351 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
8353 /* Check now whether there is a CRITICAL construct; if so, check
8354 whether the label is still visible outside of the CRITICAL block,
8355 which is invalid. */
8356 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8357 if (stack
->current
->op
== EXEC_CRITICAL
8358 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
8359 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8360 " at %L", &code
->loc
, &label
->where
);
8365 /* Step four: If we haven't found the label in the bitmap, it may
8366 still be the label of the END of the enclosing block, in which
8367 case we find it by going up the code_stack. */
8369 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
8371 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
8373 if (stack
->current
->op
== EXEC_CRITICAL
)
8375 /* Note: A label at END CRITICAL does not leave the CRITICAL
8376 construct as END CRITICAL is still part of it. */
8377 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8378 " at %L", &code
->loc
, &label
->where
);
8385 gcc_assert (stack
->current
->next
->op
== EXEC_END_NESTED_BLOCK
);
8389 /* The label is not in an enclosing block, so illegal. This was
8390 allowed in Fortran 66, so we allow it as extension. No
8391 further checks are necessary in this case. */
8392 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
8393 "as the GOTO statement at %L", &label
->where
,
8399 /* Check whether EXPR1 has the same shape as EXPR2. */
8402 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
8404 mpz_t shape
[GFC_MAX_DIMENSIONS
];
8405 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
8406 gfc_try result
= FAILURE
;
8409 /* Compare the rank. */
8410 if (expr1
->rank
!= expr2
->rank
)
8413 /* Compare the size of each dimension. */
8414 for (i
=0; i
<expr1
->rank
; i
++)
8416 if (gfc_array_dimen_size (expr1
, i
, &shape
[i
]) == FAILURE
)
8419 if (gfc_array_dimen_size (expr2
, i
, &shape2
[i
]) == FAILURE
)
8422 if (mpz_cmp (shape
[i
], shape2
[i
]))
8426 /* When either of the two expression is an assumed size array, we
8427 ignore the comparison of dimension sizes. */
8432 for (i
--; i
>= 0; i
--)
8434 mpz_clear (shape
[i
]);
8435 mpz_clear (shape2
[i
]);
8441 /* Check whether a WHERE assignment target or a WHERE mask expression
8442 has the same shape as the outmost WHERE mask expression. */
8445 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
8451 cblock
= code
->block
;
8453 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8454 In case of nested WHERE, only the outmost one is stored. */
8455 if (mask
== NULL
) /* outmost WHERE */
8457 else /* inner WHERE */
8464 /* Check if the mask-expr has a consistent shape with the
8465 outmost WHERE mask-expr. */
8466 if (resolve_where_shape (cblock
->expr1
, e
) == FAILURE
)
8467 gfc_error ("WHERE mask at %L has inconsistent shape",
8468 &cblock
->expr1
->where
);
8471 /* the assignment statement of a WHERE statement, or the first
8472 statement in where-body-construct of a WHERE construct */
8473 cnext
= cblock
->next
;
8478 /* WHERE assignment statement */
8481 /* Check shape consistent for WHERE assignment target. */
8482 if (e
&& resolve_where_shape (cnext
->expr1
, e
) == FAILURE
)
8483 gfc_error ("WHERE assignment target at %L has "
8484 "inconsistent shape", &cnext
->expr1
->where
);
8488 case EXEC_ASSIGN_CALL
:
8489 resolve_call (cnext
);
8490 if (!cnext
->resolved_sym
->attr
.elemental
)
8491 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8492 &cnext
->ext
.actual
->expr
->where
);
8495 /* WHERE or WHERE construct is part of a where-body-construct */
8497 resolve_where (cnext
, e
);
8501 gfc_error ("Unsupported statement inside WHERE at %L",
8504 /* the next statement within the same where-body-construct */
8505 cnext
= cnext
->next
;
8507 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8508 cblock
= cblock
->block
;
8513 /* Resolve assignment in FORALL construct.
8514 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8515 FORALL index variables. */
8518 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8522 for (n
= 0; n
< nvar
; n
++)
8524 gfc_symbol
*forall_index
;
8526 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
8528 /* Check whether the assignment target is one of the FORALL index
8530 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
8531 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
8532 gfc_error ("Assignment to a FORALL index variable at %L",
8533 &code
->expr1
->where
);
8536 /* If one of the FORALL index variables doesn't appear in the
8537 assignment variable, then there could be a many-to-one
8538 assignment. Emit a warning rather than an error because the
8539 mask could be resolving this problem. */
8540 if (find_forall_index (code
->expr1
, forall_index
, 0) == FAILURE
)
8541 gfc_warning ("The FORALL with index '%s' is not used on the "
8542 "left side of the assignment at %L and so might "
8543 "cause multiple assignment to this object",
8544 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
8550 /* Resolve WHERE statement in FORALL construct. */
8553 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
8554 gfc_expr
**var_expr
)
8559 cblock
= code
->block
;
8562 /* the assignment statement of a WHERE statement, or the first
8563 statement in where-body-construct of a WHERE construct */
8564 cnext
= cblock
->next
;
8569 /* WHERE assignment statement */
8571 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
8574 /* WHERE operator assignment statement */
8575 case EXEC_ASSIGN_CALL
:
8576 resolve_call (cnext
);
8577 if (!cnext
->resolved_sym
->attr
.elemental
)
8578 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8579 &cnext
->ext
.actual
->expr
->where
);
8582 /* WHERE or WHERE construct is part of a where-body-construct */
8584 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
8588 gfc_error ("Unsupported statement inside WHERE at %L",
8591 /* the next statement within the same where-body-construct */
8592 cnext
= cnext
->next
;
8594 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8595 cblock
= cblock
->block
;
8600 /* Traverse the FORALL body to check whether the following errors exist:
8601 1. For assignment, check if a many-to-one assignment happens.
8602 2. For WHERE statement, check the WHERE body to see if there is any
8603 many-to-one assignment. */
8606 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
8610 c
= code
->block
->next
;
8616 case EXEC_POINTER_ASSIGN
:
8617 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
8620 case EXEC_ASSIGN_CALL
:
8624 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8625 there is no need to handle it here. */
8629 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
8634 /* The next statement in the FORALL body. */
8640 /* Counts the number of iterators needed inside a forall construct, including
8641 nested forall constructs. This is used to allocate the needed memory
8642 in gfc_resolve_forall. */
8645 gfc_count_forall_iterators (gfc_code
*code
)
8647 int max_iters
, sub_iters
, current_iters
;
8648 gfc_forall_iterator
*fa
;
8650 gcc_assert(code
->op
== EXEC_FORALL
);
8654 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8657 code
= code
->block
->next
;
8661 if (code
->op
== EXEC_FORALL
)
8663 sub_iters
= gfc_count_forall_iterators (code
);
8664 if (sub_iters
> max_iters
)
8665 max_iters
= sub_iters
;
8670 return current_iters
+ max_iters
;
8674 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8675 gfc_resolve_forall_body to resolve the FORALL body. */
8678 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
8680 static gfc_expr
**var_expr
;
8681 static int total_var
= 0;
8682 static int nvar
= 0;
8684 gfc_forall_iterator
*fa
;
8689 /* Start to resolve a FORALL construct */
8690 if (forall_save
== 0)
8692 /* Count the total number of FORALL index in the nested FORALL
8693 construct in order to allocate the VAR_EXPR with proper size. */
8694 total_var
= gfc_count_forall_iterators (code
);
8696 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8697 var_expr
= XCNEWVEC (gfc_expr
*, total_var
);
8700 /* The information about FORALL iterator, including FORALL index start, end
8701 and stride. The FORALL index can not appear in start, end or stride. */
8702 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
8704 /* Check if any outer FORALL index name is the same as the current
8706 for (i
= 0; i
< nvar
; i
++)
8708 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
8710 gfc_error ("An outer FORALL construct already has an index "
8711 "with this name %L", &fa
->var
->where
);
8715 /* Record the current FORALL index. */
8716 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
8720 /* No memory leak. */
8721 gcc_assert (nvar
<= total_var
);
8724 /* Resolve the FORALL body. */
8725 gfc_resolve_forall_body (code
, nvar
, var_expr
);
8727 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8728 gfc_resolve_blocks (code
->block
, ns
);
8732 /* Free only the VAR_EXPRs allocated in this frame. */
8733 for (i
= nvar
; i
< tmp
; i
++)
8734 gfc_free_expr (var_expr
[i
]);
8738 /* We are in the outermost FORALL construct. */
8739 gcc_assert (forall_save
== 0);
8741 /* VAR_EXPR is not needed any more. */
8748 /* Resolve a BLOCK construct statement. */
8751 resolve_block_construct (gfc_code
* code
)
8753 /* Resolve the BLOCK's namespace. */
8754 gfc_resolve (code
->ext
.block
.ns
);
8756 /* For an ASSOCIATE block, the associations (and their targets) are already
8757 resolved during resolve_symbol. */
8761 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8764 static void resolve_code (gfc_code
*, gfc_namespace
*);
8767 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
8771 for (; b
; b
= b
->block
)
8773 t
= gfc_resolve_expr (b
->expr1
);
8774 if (gfc_resolve_expr (b
->expr2
) == FAILURE
)
8780 if (t
== SUCCESS
&& b
->expr1
!= NULL
8781 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
8782 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8789 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
8790 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8795 resolve_branch (b
->label1
, b
);
8799 resolve_block_construct (b
);
8803 case EXEC_SELECT_TYPE
:
8814 case EXEC_OMP_ATOMIC
:
8815 case EXEC_OMP_CRITICAL
:
8817 case EXEC_OMP_MASTER
:
8818 case EXEC_OMP_ORDERED
:
8819 case EXEC_OMP_PARALLEL
:
8820 case EXEC_OMP_PARALLEL_DO
:
8821 case EXEC_OMP_PARALLEL_SECTIONS
:
8822 case EXEC_OMP_PARALLEL_WORKSHARE
:
8823 case EXEC_OMP_SECTIONS
:
8824 case EXEC_OMP_SINGLE
:
8826 case EXEC_OMP_TASKWAIT
:
8827 case EXEC_OMP_TASKYIELD
:
8828 case EXEC_OMP_WORKSHARE
:
8832 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8835 resolve_code (b
->next
, ns
);
8840 /* Does everything to resolve an ordinary assignment. Returns true
8841 if this is an interface assignment. */
8843 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
8853 if (gfc_extend_assign (code
, ns
) == SUCCESS
)
8857 if (code
->op
== EXEC_ASSIGN_CALL
)
8859 lhs
= code
->ext
.actual
->expr
;
8860 rhsptr
= &code
->ext
.actual
->next
->expr
;
8864 gfc_actual_arglist
* args
;
8865 gfc_typebound_proc
* tbp
;
8867 gcc_assert (code
->op
== EXEC_COMPCALL
);
8869 args
= code
->expr1
->value
.compcall
.actual
;
8871 rhsptr
= &args
->next
->expr
;
8873 tbp
= code
->expr1
->value
.compcall
.tbp
;
8874 gcc_assert (!tbp
->is_generic
);
8877 /* Make a temporary rhs when there is a default initializer
8878 and rhs is the same symbol as the lhs. */
8879 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
8880 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
8881 && gfc_has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
8882 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
8883 *rhsptr
= gfc_get_parentheses (*rhsptr
);
8892 && gfc_notify_std (GFC_STD_GNU
, "Extension: BOZ literal at %L outside "
8893 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8894 &code
->loc
) == FAILURE
)
8897 /* Handle the case of a BOZ literal on the RHS. */
8898 if (rhs
->is_boz
&& lhs
->ts
.type
!= BT_INTEGER
)
8901 if (gfc_option
.warn_surprising
)
8902 gfc_warning ("BOZ literal at %L is bitwise transferred "
8903 "non-integer symbol '%s'", &code
->loc
,
8904 lhs
->symtree
->n
.sym
->name
);
8906 if (!gfc_convert_boz (rhs
, &lhs
->ts
))
8908 if ((rc
= gfc_range_check (rhs
)) != ARITH_OK
)
8910 if (rc
== ARITH_UNDERFLOW
)
8911 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8912 ". This check can be disabled with the option "
8913 "-fno-range-check", &rhs
->where
);
8914 else if (rc
== ARITH_OVERFLOW
)
8915 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8916 ". This check can be disabled with the option "
8917 "-fno-range-check", &rhs
->where
);
8918 else if (rc
== ARITH_NAN
)
8919 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8920 ". This check can be disabled with the option "
8921 "-fno-range-check", &rhs
->where
);
8926 if (lhs
->ts
.type
== BT_CHARACTER
8927 && gfc_option
.warn_character_truncation
)
8929 if (lhs
->ts
.u
.cl
!= NULL
8930 && lhs
->ts
.u
.cl
->length
!= NULL
8931 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8932 llen
= mpz_get_si (lhs
->ts
.u
.cl
->length
->value
.integer
);
8934 if (rhs
->expr_type
== EXPR_CONSTANT
)
8935 rlen
= rhs
->value
.character
.length
;
8937 else if (rhs
->ts
.u
.cl
!= NULL
8938 && rhs
->ts
.u
.cl
->length
!= NULL
8939 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8940 rlen
= mpz_get_si (rhs
->ts
.u
.cl
->length
->value
.integer
);
8942 if (rlen
&& llen
&& rlen
> llen
)
8943 gfc_warning_now ("CHARACTER expression will be truncated "
8944 "in assignment (%d/%d) at %L",
8945 llen
, rlen
, &code
->loc
);
8948 /* Ensure that a vector index expression for the lvalue is evaluated
8949 to a temporary if the lvalue symbol is referenced in it. */
8952 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
8953 if (ref
->type
== REF_ARRAY
)
8955 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
8956 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
8957 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
8958 ref
->u
.ar
.start
[n
]))
8960 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
8964 if (gfc_pure (NULL
))
8966 if (lhs
->ts
.type
== BT_DERIVED
8967 && lhs
->expr_type
== EXPR_VARIABLE
8968 && lhs
->ts
.u
.derived
->attr
.pointer_comp
8969 && rhs
->expr_type
== EXPR_VARIABLE
8970 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
8971 || gfc_is_coindexed (rhs
)))
8974 if (gfc_is_coindexed (rhs
))
8975 gfc_error ("Coindexed expression at %L is assigned to "
8976 "a derived type variable with a POINTER "
8977 "component in a PURE procedure",
8980 gfc_error ("The impure variable at %L is assigned to "
8981 "a derived type variable with a POINTER "
8982 "component in a PURE procedure (12.6)",
8987 /* Fortran 2008, C1283. */
8988 if (gfc_is_coindexed (lhs
))
8990 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8991 "procedure", &rhs
->where
);
8996 if (gfc_implicit_pure (NULL
))
8998 if (lhs
->expr_type
== EXPR_VARIABLE
8999 && lhs
->symtree
->n
.sym
!= gfc_current_ns
->proc_name
9000 && lhs
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
9001 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
9003 if (lhs
->ts
.type
== BT_DERIVED
9004 && lhs
->expr_type
== EXPR_VARIABLE
9005 && lhs
->ts
.u
.derived
->attr
.pointer_comp
9006 && rhs
->expr_type
== EXPR_VARIABLE
9007 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
9008 || gfc_is_coindexed (rhs
)))
9009 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
9011 /* Fortran 2008, C1283. */
9012 if (gfc_is_coindexed (lhs
))
9013 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
9017 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9018 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9019 if (lhs
->ts
.type
== BT_CLASS
)
9021 gfc_error ("Variable must not be polymorphic in assignment at %L",
9026 /* F2008, Section 7.2.1.2. */
9027 if (gfc_is_coindexed (lhs
) && gfc_has_ultimate_allocatable (lhs
))
9029 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9030 "component in assignment at %L", &lhs
->where
);
9034 gfc_check_assign (lhs
, rhs
, 1);
9039 /* Given a block of code, recursively resolve everything pointed to by this
9043 resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
9045 int omp_workshare_save
;
9050 frame
.prev
= cs_base
;
9054 find_reachable_labels (code
);
9056 for (; code
; code
= code
->next
)
9058 frame
.current
= code
;
9059 forall_save
= forall_flag
;
9061 if (code
->op
== EXEC_FORALL
)
9064 gfc_resolve_forall (code
, ns
, forall_save
);
9067 else if (code
->block
)
9069 omp_workshare_save
= -1;
9072 case EXEC_OMP_PARALLEL_WORKSHARE
:
9073 omp_workshare_save
= omp_workshare_flag
;
9074 omp_workshare_flag
= 1;
9075 gfc_resolve_omp_parallel_blocks (code
, ns
);
9077 case EXEC_OMP_PARALLEL
:
9078 case EXEC_OMP_PARALLEL_DO
:
9079 case EXEC_OMP_PARALLEL_SECTIONS
:
9081 omp_workshare_save
= omp_workshare_flag
;
9082 omp_workshare_flag
= 0;
9083 gfc_resolve_omp_parallel_blocks (code
, ns
);
9086 gfc_resolve_omp_do_blocks (code
, ns
);
9088 case EXEC_SELECT_TYPE
:
9089 /* Blocks are handled in resolve_select_type because we have
9090 to transform the SELECT TYPE into ASSOCIATE first. */
9092 case EXEC_OMP_WORKSHARE
:
9093 omp_workshare_save
= omp_workshare_flag
;
9094 omp_workshare_flag
= 1;
9097 gfc_resolve_blocks (code
->block
, ns
);
9101 if (omp_workshare_save
!= -1)
9102 omp_workshare_flag
= omp_workshare_save
;
9106 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
9107 t
= gfc_resolve_expr (code
->expr1
);
9108 forall_flag
= forall_save
;
9110 if (gfc_resolve_expr (code
->expr2
) == FAILURE
)
9113 if (code
->op
== EXEC_ALLOCATE
9114 && gfc_resolve_expr (code
->expr3
) == FAILURE
)
9120 case EXEC_END_BLOCK
:
9121 case EXEC_END_NESTED_BLOCK
:
9125 case EXEC_ERROR_STOP
:
9129 case EXEC_ASSIGN_CALL
:
9134 case EXEC_SYNC_IMAGES
:
9135 case EXEC_SYNC_MEMORY
:
9136 resolve_sync (code
);
9141 resolve_lock_unlock (code
);
9145 /* Keep track of which entry we are up to. */
9146 current_entry_id
= code
->ext
.entry
->id
;
9150 resolve_where (code
, NULL
);
9154 if (code
->expr1
!= NULL
)
9156 if (code
->expr1
->ts
.type
!= BT_INTEGER
)
9157 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9158 "INTEGER variable", &code
->expr1
->where
);
9159 else if (code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
9160 gfc_error ("Variable '%s' has not been assigned a target "
9161 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
9162 &code
->expr1
->where
);
9165 resolve_branch (code
->label1
, code
);
9169 if (code
->expr1
!= NULL
9170 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
9171 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9172 "INTEGER return specifier", &code
->expr1
->where
);
9175 case EXEC_INIT_ASSIGN
:
9176 case EXEC_END_PROCEDURE
:
9183 if (gfc_check_vardef_context (code
->expr1
, false, false,
9184 _("assignment")) == FAILURE
)
9187 if (resolve_ordinary_assign (code
, ns
))
9189 if (code
->op
== EXEC_COMPCALL
)
9196 case EXEC_LABEL_ASSIGN
:
9197 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
9198 gfc_error ("Label %d referenced at %L is never defined",
9199 code
->label1
->value
, &code
->label1
->where
);
9201 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
9202 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
9203 || code
->expr1
->symtree
->n
.sym
->ts
.kind
9204 != gfc_default_integer_kind
9205 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
9206 gfc_error ("ASSIGN statement at %L requires a scalar "
9207 "default INTEGER variable", &code
->expr1
->where
);
9210 case EXEC_POINTER_ASSIGN
:
9217 /* This is both a variable definition and pointer assignment
9218 context, so check both of them. For rank remapping, a final
9219 array ref may be present on the LHS and fool gfc_expr_attr
9220 used in gfc_check_vardef_context. Remove it. */
9221 e
= remove_last_array_ref (code
->expr1
);
9222 t
= gfc_check_vardef_context (e
, true, false,
9223 _("pointer assignment"));
9225 t
= gfc_check_vardef_context (e
, false, false,
9226 _("pointer assignment"));
9231 gfc_check_pointer_assign (code
->expr1
, code
->expr2
);
9235 case EXEC_ARITHMETIC_IF
:
9237 && code
->expr1
->ts
.type
!= BT_INTEGER
9238 && code
->expr1
->ts
.type
!= BT_REAL
)
9239 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9240 "expression", &code
->expr1
->where
);
9242 resolve_branch (code
->label1
, code
);
9243 resolve_branch (code
->label2
, code
);
9244 resolve_branch (code
->label3
, code
);
9248 if (t
== SUCCESS
&& code
->expr1
!= NULL
9249 && (code
->expr1
->ts
.type
!= BT_LOGICAL
9250 || code
->expr1
->rank
!= 0))
9251 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9252 &code
->expr1
->where
);
9257 resolve_call (code
);
9262 resolve_typebound_subroutine (code
);
9266 resolve_ppc_call (code
);
9270 /* Select is complicated. Also, a SELECT construct could be
9271 a transformed computed GOTO. */
9272 resolve_select (code
);
9275 case EXEC_SELECT_TYPE
:
9276 resolve_select_type (code
, ns
);
9280 resolve_block_construct (code
);
9284 if (code
->ext
.iterator
!= NULL
)
9286 gfc_iterator
*iter
= code
->ext
.iterator
;
9287 if (gfc_resolve_iterator (iter
, true) != FAILURE
)
9288 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
);
9293 if (code
->expr1
== NULL
)
9294 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9296 && (code
->expr1
->rank
!= 0
9297 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
9298 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9299 "a scalar LOGICAL expression", &code
->expr1
->where
);
9304 resolve_allocate_deallocate (code
, "ALLOCATE");
9308 case EXEC_DEALLOCATE
:
9310 resolve_allocate_deallocate (code
, "DEALLOCATE");
9315 if (gfc_resolve_open (code
->ext
.open
) == FAILURE
)
9318 resolve_branch (code
->ext
.open
->err
, code
);
9322 if (gfc_resolve_close (code
->ext
.close
) == FAILURE
)
9325 resolve_branch (code
->ext
.close
->err
, code
);
9328 case EXEC_BACKSPACE
:
9332 if (gfc_resolve_filepos (code
->ext
.filepos
) == FAILURE
)
9335 resolve_branch (code
->ext
.filepos
->err
, code
);
9339 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9342 resolve_branch (code
->ext
.inquire
->err
, code
);
9346 gcc_assert (code
->ext
.inquire
!= NULL
);
9347 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
9350 resolve_branch (code
->ext
.inquire
->err
, code
);
9354 if (gfc_resolve_wait (code
->ext
.wait
) == FAILURE
)
9357 resolve_branch (code
->ext
.wait
->err
, code
);
9358 resolve_branch (code
->ext
.wait
->end
, code
);
9359 resolve_branch (code
->ext
.wait
->eor
, code
);
9364 if (gfc_resolve_dt (code
->ext
.dt
, &code
->loc
) == FAILURE
)
9367 resolve_branch (code
->ext
.dt
->err
, code
);
9368 resolve_branch (code
->ext
.dt
->end
, code
);
9369 resolve_branch (code
->ext
.dt
->eor
, code
);
9373 resolve_transfer (code
);
9377 resolve_forall_iterators (code
->ext
.forall_iterator
);
9379 if (code
->expr1
!= NULL
9380 && (code
->expr1
->ts
.type
!= BT_LOGICAL
|| code
->expr1
->rank
))
9381 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9382 "expression", &code
->expr1
->where
);
9385 case EXEC_OMP_ATOMIC
:
9386 case EXEC_OMP_BARRIER
:
9387 case EXEC_OMP_CRITICAL
:
9388 case EXEC_OMP_FLUSH
:
9390 case EXEC_OMP_MASTER
:
9391 case EXEC_OMP_ORDERED
:
9392 case EXEC_OMP_SECTIONS
:
9393 case EXEC_OMP_SINGLE
:
9394 case EXEC_OMP_TASKWAIT
:
9395 case EXEC_OMP_TASKYIELD
:
9396 case EXEC_OMP_WORKSHARE
:
9397 gfc_resolve_omp_directive (code
, ns
);
9400 case EXEC_OMP_PARALLEL
:
9401 case EXEC_OMP_PARALLEL_DO
:
9402 case EXEC_OMP_PARALLEL_SECTIONS
:
9403 case EXEC_OMP_PARALLEL_WORKSHARE
:
9405 omp_workshare_save
= omp_workshare_flag
;
9406 omp_workshare_flag
= 0;
9407 gfc_resolve_omp_directive (code
, ns
);
9408 omp_workshare_flag
= omp_workshare_save
;
9412 gfc_internal_error ("resolve_code(): Bad statement code");
9416 cs_base
= frame
.prev
;
9420 /* Resolve initial values and make sure they are compatible with
9424 resolve_values (gfc_symbol
*sym
)
9428 if (sym
->value
== NULL
)
9431 if (sym
->value
->expr_type
== EXPR_STRUCTURE
)
9432 t
= resolve_structure_cons (sym
->value
, 1);
9434 t
= gfc_resolve_expr (sym
->value
);
9439 gfc_check_assign_symbol (sym
, sym
->value
);
9443 /* Verify the binding labels for common blocks that are BIND(C). The label
9444 for a BIND(C) common block must be identical in all scoping units in which
9445 the common block is declared. Further, the binding label can not collide
9446 with any other global entity in the program. */
9449 resolve_bind_c_comms (gfc_symtree
*comm_block_tree
)
9451 if (comm_block_tree
->n
.common
->is_bind_c
== 1)
9453 gfc_gsymbol
*binding_label_gsym
;
9454 gfc_gsymbol
*comm_name_gsym
;
9456 /* See if a global symbol exists by the common block's name. It may
9457 be NULL if the common block is use-associated. */
9458 comm_name_gsym
= gfc_find_gsymbol (gfc_gsym_root
,
9459 comm_block_tree
->n
.common
->name
);
9460 if (comm_name_gsym
!= NULL
&& comm_name_gsym
->type
!= GSYM_COMMON
)
9461 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9462 "with the global entity '%s' at %L",
9463 comm_block_tree
->n
.common
->binding_label
,
9464 comm_block_tree
->n
.common
->name
,
9465 &(comm_block_tree
->n
.common
->where
),
9466 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9467 else if (comm_name_gsym
!= NULL
9468 && strcmp (comm_name_gsym
->name
,
9469 comm_block_tree
->n
.common
->name
) == 0)
9471 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9473 if (comm_name_gsym
->binding_label
== NULL
)
9474 /* No binding label for common block stored yet; save this one. */
9475 comm_name_gsym
->binding_label
=
9476 comm_block_tree
->n
.common
->binding_label
;
9478 if (strcmp (comm_name_gsym
->binding_label
,
9479 comm_block_tree
->n
.common
->binding_label
) != 0)
9481 /* Common block names match but binding labels do not. */
9482 gfc_error ("Binding label '%s' for common block '%s' at %L "
9483 "does not match the binding label '%s' for common "
9485 comm_block_tree
->n
.common
->binding_label
,
9486 comm_block_tree
->n
.common
->name
,
9487 &(comm_block_tree
->n
.common
->where
),
9488 comm_name_gsym
->binding_label
,
9489 comm_name_gsym
->name
,
9490 &(comm_name_gsym
->where
));
9495 /* There is no binding label (NAME="") so we have nothing further to
9496 check and nothing to add as a global symbol for the label. */
9497 if (comm_block_tree
->n
.common
->binding_label
[0] == '\0' )
9500 binding_label_gsym
=
9501 gfc_find_gsymbol (gfc_gsym_root
,
9502 comm_block_tree
->n
.common
->binding_label
);
9503 if (binding_label_gsym
== NULL
)
9505 /* Need to make a global symbol for the binding label to prevent
9506 it from colliding with another. */
9507 binding_label_gsym
=
9508 gfc_get_gsymbol (comm_block_tree
->n
.common
->binding_label
);
9509 binding_label_gsym
->sym_name
= comm_block_tree
->n
.common
->name
;
9510 binding_label_gsym
->type
= GSYM_COMMON
;
9514 /* If comm_name_gsym is NULL, the name common block is use
9515 associated and the name could be colliding. */
9516 if (binding_label_gsym
->type
!= GSYM_COMMON
)
9517 gfc_error ("Binding label '%s' for common block '%s' at %L "
9518 "collides with the global entity '%s' at %L",
9519 comm_block_tree
->n
.common
->binding_label
,
9520 comm_block_tree
->n
.common
->name
,
9521 &(comm_block_tree
->n
.common
->where
),
9522 binding_label_gsym
->name
,
9523 &(binding_label_gsym
->where
));
9524 else if (comm_name_gsym
!= NULL
9525 && (strcmp (binding_label_gsym
->name
,
9526 comm_name_gsym
->binding_label
) != 0)
9527 && (strcmp (binding_label_gsym
->sym_name
,
9528 comm_name_gsym
->name
) != 0))
9529 gfc_error ("Binding label '%s' for common block '%s' at %L "
9530 "collides with global entity '%s' at %L",
9531 binding_label_gsym
->name
, binding_label_gsym
->sym_name
,
9532 &(comm_block_tree
->n
.common
->where
),
9533 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
9541 /* Verify any BIND(C) derived types in the namespace so we can report errors
9542 for them once, rather than for each variable declared of that type. */
9545 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
9547 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
9548 && derived_sym
->attr
.is_bind_c
== 1)
9549 verify_bind_c_derived_type (derived_sym
);
9555 /* Verify that any binding labels used in a given namespace do not collide
9556 with the names or binding labels of any global symbols. */
9559 gfc_verify_binding_labels (gfc_symbol
*sym
)
9563 if (sym
!= NULL
&& sym
->attr
.is_bind_c
&& sym
->attr
.is_iso_c
== 0
9564 && sym
->attr
.flavor
!= FL_DERIVED
&& sym
->binding_label
[0] != '\0')
9566 gfc_gsymbol
*bind_c_sym
;
9568 bind_c_sym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
9569 if (bind_c_sym
!= NULL
9570 && strcmp (bind_c_sym
->name
, sym
->binding_label
) == 0)
9572 if (sym
->attr
.if_source
== IFSRC_DECL
9573 && (bind_c_sym
->type
!= GSYM_SUBROUTINE
9574 && bind_c_sym
->type
!= GSYM_FUNCTION
)
9575 && ((sym
->attr
.contained
== 1
9576 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0)
9577 || (sym
->attr
.use_assoc
== 1
9578 && (strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0))))
9580 /* Make sure global procedures don't collide with anything. */
9581 gfc_error ("Binding label '%s' at %L collides with the global "
9582 "entity '%s' at %L", sym
->binding_label
,
9583 &(sym
->declared_at
), bind_c_sym
->name
,
9584 &(bind_c_sym
->where
));
9587 else if (sym
->attr
.contained
== 0
9588 && (sym
->attr
.if_source
== IFSRC_IFBODY
9589 && sym
->attr
.flavor
== FL_PROCEDURE
)
9590 && (bind_c_sym
->sym_name
!= NULL
9591 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0))
9593 /* Make sure procedures in interface bodies don't collide. */
9594 gfc_error ("Binding label '%s' in interface body at %L collides "
9595 "with the global entity '%s' at %L",
9597 &(sym
->declared_at
), bind_c_sym
->name
,
9598 &(bind_c_sym
->where
));
9601 else if (sym
->attr
.contained
== 0
9602 && sym
->attr
.if_source
== IFSRC_UNKNOWN
)
9603 if ((sym
->attr
.use_assoc
&& bind_c_sym
->mod_name
9604 && strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0)
9605 || sym
->attr
.use_assoc
== 0)
9607 gfc_error ("Binding label '%s' at %L collides with global "
9608 "entity '%s' at %L", sym
->binding_label
,
9609 &(sym
->declared_at
), bind_c_sym
->name
,
9610 &(bind_c_sym
->where
));
9615 /* Clear the binding label to prevent checking multiple times. */
9616 sym
->binding_label
[0] = '\0';
9618 else if (bind_c_sym
== NULL
)
9620 bind_c_sym
= gfc_get_gsymbol (sym
->binding_label
);
9621 bind_c_sym
->where
= sym
->declared_at
;
9622 bind_c_sym
->sym_name
= sym
->name
;
9624 if (sym
->attr
.use_assoc
== 1)
9625 bind_c_sym
->mod_name
= sym
->module
;
9627 if (sym
->ns
->proc_name
!= NULL
)
9628 bind_c_sym
->mod_name
= sym
->ns
->proc_name
->name
;
9630 if (sym
->attr
.contained
== 0)
9632 if (sym
->attr
.subroutine
)
9633 bind_c_sym
->type
= GSYM_SUBROUTINE
;
9634 else if (sym
->attr
.function
)
9635 bind_c_sym
->type
= GSYM_FUNCTION
;
9643 /* Resolve an index expression. */
9646 resolve_index_expr (gfc_expr
*e
)
9648 if (gfc_resolve_expr (e
) == FAILURE
)
9651 if (gfc_simplify_expr (e
, 0) == FAILURE
)
9654 if (gfc_specification_expr (e
) == FAILURE
)
9661 /* Resolve a charlen structure. */
9664 resolve_charlen (gfc_charlen
*cl
)
9673 specification_expr
= 1;
9675 if (resolve_index_expr (cl
->length
) == FAILURE
)
9677 specification_expr
= 0;
9681 /* "If the character length parameter value evaluates to a negative
9682 value, the length of character entities declared is zero." */
9683 if (cl
->length
&& !gfc_extract_int (cl
->length
, &i
) && i
< 0)
9685 if (gfc_option
.warn_surprising
)
9686 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9687 " the length has been set to zero",
9688 &cl
->length
->where
, i
);
9689 gfc_replace_expr (cl
->length
,
9690 gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0));
9693 /* Check that the character length is not too large. */
9694 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
9695 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
9696 && cl
->length
->ts
.type
== BT_INTEGER
9697 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
9699 gfc_error ("String length at %L is too large", &cl
->length
->where
);
9707 /* Test for non-constant shape arrays. */
9710 is_non_constant_shape_array (gfc_symbol
*sym
)
9716 not_constant
= false;
9717 if (sym
->as
!= NULL
)
9719 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9720 has not been simplified; parameter array references. Do the
9721 simplification now. */
9722 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
9724 e
= sym
->as
->lower
[i
];
9725 if (e
&& (resolve_index_expr (e
) == FAILURE
9726 || !gfc_is_constant_expr (e
)))
9727 not_constant
= true;
9728 e
= sym
->as
->upper
[i
];
9729 if (e
&& (resolve_index_expr (e
) == FAILURE
9730 || !gfc_is_constant_expr (e
)))
9731 not_constant
= true;
9734 return not_constant
;
9737 /* Given a symbol and an initialization expression, add code to initialize
9738 the symbol to the function entry. */
9740 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
9744 gfc_namespace
*ns
= sym
->ns
;
9746 /* Search for the function namespace if this is a contained
9747 function without an explicit result. */
9748 if (sym
->attr
.function
&& sym
== sym
->result
9749 && sym
->name
!= sym
->ns
->proc_name
->name
)
9752 for (;ns
; ns
= ns
->sibling
)
9753 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
9759 gfc_free_expr (init
);
9763 /* Build an l-value expression for the result. */
9764 lval
= gfc_lval_expr_from_sym (sym
);
9766 /* Add the code at scope entry. */
9767 init_st
= gfc_get_code ();
9768 init_st
->next
= ns
->code
;
9771 /* Assign the default initializer to the l-value. */
9772 init_st
->loc
= sym
->declared_at
;
9773 init_st
->op
= EXEC_INIT_ASSIGN
;
9774 init_st
->expr1
= lval
;
9775 init_st
->expr2
= init
;
9778 /* Assign the default initializer to a derived type variable or result. */
9781 apply_default_init (gfc_symbol
*sym
)
9783 gfc_expr
*init
= NULL
;
9785 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9788 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
9789 init
= gfc_default_initializer (&sym
->ts
);
9791 if (init
== NULL
&& sym
->ts
.type
!= BT_CLASS
)
9794 build_init_assign (sym
, init
);
9795 sym
->attr
.referenced
= 1;
9798 /* Build an initializer for a local integer, real, complex, logical, or
9799 character variable, based on the command line flags finit-local-zero,
9800 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9801 null if the symbol should not have a default initialization. */
9803 build_default_init_expr (gfc_symbol
*sym
)
9806 gfc_expr
*init_expr
;
9809 /* These symbols should never have a default initialization. */
9810 if ((sym
->attr
.dimension
&& !gfc_is_compile_time_shape (sym
->as
))
9811 || sym
->attr
.external
9813 || sym
->attr
.pointer
9814 || sym
->attr
.in_equivalence
9815 || sym
->attr
.in_common
9818 || sym
->attr
.cray_pointee
9819 || sym
->attr
.cray_pointer
)
9822 /* Now we'll try to build an initializer expression. */
9823 init_expr
= gfc_get_constant_expr (sym
->ts
.type
, sym
->ts
.kind
,
9826 /* We will only initialize integers, reals, complex, logicals, and
9827 characters, and only if the corresponding command-line flags
9828 were set. Otherwise, we free init_expr and return null. */
9829 switch (sym
->ts
.type
)
9832 if (gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
9833 mpz_set_si (init_expr
->value
.integer
,
9834 gfc_option
.flag_init_integer_value
);
9837 gfc_free_expr (init_expr
);
9843 switch (gfc_option
.flag_init_real
)
9845 case GFC_INIT_REAL_SNAN
:
9846 init_expr
->is_snan
= 1;
9848 case GFC_INIT_REAL_NAN
:
9849 mpfr_set_nan (init_expr
->value
.real
);
9852 case GFC_INIT_REAL_INF
:
9853 mpfr_set_inf (init_expr
->value
.real
, 1);
9856 case GFC_INIT_REAL_NEG_INF
:
9857 mpfr_set_inf (init_expr
->value
.real
, -1);
9860 case GFC_INIT_REAL_ZERO
:
9861 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
9865 gfc_free_expr (init_expr
);
9872 switch (gfc_option
.flag_init_real
)
9874 case GFC_INIT_REAL_SNAN
:
9875 init_expr
->is_snan
= 1;
9877 case GFC_INIT_REAL_NAN
:
9878 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
9879 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
9882 case GFC_INIT_REAL_INF
:
9883 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
9884 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
9887 case GFC_INIT_REAL_NEG_INF
:
9888 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
9889 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
9892 case GFC_INIT_REAL_ZERO
:
9893 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
9897 gfc_free_expr (init_expr
);
9904 if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_FALSE
)
9905 init_expr
->value
.logical
= 0;
9906 else if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_TRUE
)
9907 init_expr
->value
.logical
= 1;
9910 gfc_free_expr (init_expr
);
9916 /* For characters, the length must be constant in order to
9917 create a default initializer. */
9918 if (gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
9919 && sym
->ts
.u
.cl
->length
9920 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9922 char_len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
9923 init_expr
->value
.character
.length
= char_len
;
9924 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
9925 for (i
= 0; i
< char_len
; i
++)
9926 init_expr
->value
.character
.string
[i
]
9927 = (unsigned char) gfc_option
.flag_init_character_value
;
9931 gfc_free_expr (init_expr
);
9937 gfc_free_expr (init_expr
);
9943 /* Add an initialization expression to a local variable. */
9945 apply_default_init_local (gfc_symbol
*sym
)
9947 gfc_expr
*init
= NULL
;
9949 /* The symbol should be a variable or a function return value. */
9950 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
9951 || (sym
->attr
.function
&& sym
->result
!= sym
))
9954 /* Try to build the initializer expression. If we can't initialize
9955 this symbol, then init will be NULL. */
9956 init
= build_default_init_expr (sym
);
9960 /* For saved variables, we don't want to add an initializer at
9961 function entry, so we just add a static initializer. */
9962 if (sym
->attr
.save
|| sym
->ns
->save_all
9963 || gfc_option
.flag_max_stack_var_size
== 0)
9965 /* Don't clobber an existing initializer! */
9966 gcc_assert (sym
->value
== NULL
);
9971 build_init_assign (sym
, init
);
9975 /* Resolution of common features of flavors variable and procedure. */
9978 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
9980 /* Avoid double diagnostics for function result symbols. */
9981 if ((sym
->result
|| sym
->attr
.result
) && !sym
->attr
.dummy
9982 && (sym
->ns
!= gfc_current_ns
))
9985 /* Constraints on deferred shape variable. */
9986 if (sym
->as
== NULL
|| sym
->as
->type
!= AS_DEFERRED
)
9988 if (sym
->attr
.allocatable
)
9990 if (sym
->attr
.dimension
)
9992 gfc_error ("Allocatable array '%s' at %L must have "
9993 "a deferred shape", sym
->name
, &sym
->declared_at
);
9996 else if (gfc_notify_std (GFC_STD_F2003
, "Scalar object '%s' at %L "
9997 "may not be ALLOCATABLE", sym
->name
,
9998 &sym
->declared_at
) == FAILURE
)
10002 if (sym
->attr
.pointer
&& sym
->attr
.dimension
)
10004 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
10005 sym
->name
, &sym
->declared_at
);
10011 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
10012 && sym
->ts
.type
!= BT_CLASS
&& !sym
->assoc
)
10014 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10015 sym
->name
, &sym
->declared_at
);
10020 /* Constraints on polymorphic variables. */
10021 if (sym
->ts
.type
== BT_CLASS
&& !(sym
->result
&& sym
->result
!= sym
))
10024 if (sym
->attr
.class_ok
10025 && !gfc_type_is_extensible (CLASS_DATA (sym
)->ts
.u
.derived
))
10027 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10028 CLASS_DATA (sym
)->ts
.u
.derived
->name
, sym
->name
,
10029 &sym
->declared_at
);
10034 /* Assume that use associated symbols were checked in the module ns.
10035 Class-variables that are associate-names are also something special
10036 and excepted from the test. */
10037 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
&& !sym
->assoc
)
10039 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10040 "or pointer", sym
->name
, &sym
->declared_at
);
10049 /* Additional checks for symbols with flavor variable and derived
10050 type. To be called from resolve_fl_variable. */
10053 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
10055 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
10057 /* Check to see if a derived type is blocked from being host
10058 associated by the presence of another class I symbol in the same
10059 namespace. 14.6.1.3 of the standard and the discussion on
10060 comp.lang.fortran. */
10061 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
10062 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
10065 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
10066 if (s
&& s
->attr
.flavor
!= FL_DERIVED
)
10068 gfc_error ("The type '%s' cannot be host associated at %L "
10069 "because it is blocked by an incompatible object "
10070 "of the same name declared at %L",
10071 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
10077 /* 4th constraint in section 11.3: "If an object of a type for which
10078 component-initialization is specified (R429) appears in the
10079 specification-part of a module and does not have the ALLOCATABLE
10080 or POINTER attribute, the object shall have the SAVE attribute."
10082 The check for initializers is performed with
10083 gfc_has_default_initializer because gfc_default_initializer generates
10084 a hidden default for allocatable components. */
10085 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
10086 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10087 && !sym
->ns
->save_all
&& !sym
->attr
.save
10088 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
10089 && gfc_has_default_initializer (sym
->ts
.u
.derived
)
10090 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Implied SAVE for "
10091 "module variable '%s' at %L, needed due to "
10092 "the default initialization", sym
->name
,
10093 &sym
->declared_at
) == FAILURE
)
10096 /* Assign default initializer. */
10097 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
10098 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
10100 sym
->value
= gfc_default_initializer (&sym
->ts
);
10107 /* Resolve symbols with flavor variable. */
10110 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
10112 int no_init_flag
, automatic_flag
;
10114 const char *auto_save_msg
;
10116 auto_save_msg
= "Automatic object '%s' at %L cannot have the "
10119 if (resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
10122 /* Set this flag to check that variables are parameters of all entries.
10123 This check is effected by the call to gfc_resolve_expr through
10124 is_non_constant_shape_array. */
10125 specification_expr
= 1;
10127 if (sym
->ns
->proc_name
10128 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10129 || sym
->ns
->proc_name
->attr
.is_main_program
)
10130 && !sym
->attr
.use_assoc
10131 && !sym
->attr
.allocatable
10132 && !sym
->attr
.pointer
10133 && is_non_constant_shape_array (sym
))
10135 /* The shape of a main program or module array needs to be
10137 gfc_error ("The module or main program array '%s' at %L must "
10138 "have constant shape", sym
->name
, &sym
->declared_at
);
10139 specification_expr
= 0;
10143 /* Constraints on deferred type parameter. */
10144 if (sym
->ts
.deferred
&& !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
10146 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10147 "requires either the pointer or allocatable attribute",
10148 sym
->name
, &sym
->declared_at
);
10152 if (sym
->ts
.type
== BT_CHARACTER
)
10154 /* Make sure that character string variables with assumed length are
10155 dummy arguments. */
10156 e
= sym
->ts
.u
.cl
->length
;
10157 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
10158 && !sym
->ts
.deferred
)
10160 gfc_error ("Entity with assumed character length at %L must be a "
10161 "dummy argument or a PARAMETER", &sym
->declared_at
);
10165 if (e
&& sym
->attr
.save
== SAVE_EXPLICIT
&& !gfc_is_constant_expr (e
))
10167 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
10171 if (!gfc_is_constant_expr (e
)
10172 && !(e
->expr_type
== EXPR_VARIABLE
10173 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
))
10175 if (!sym
->attr
.use_assoc
&& sym
->ns
->proc_name
10176 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10177 || sym
->ns
->proc_name
->attr
.is_main_program
))
10179 gfc_error ("'%s' at %L must have constant character length "
10180 "in this context", sym
->name
, &sym
->declared_at
);
10183 if (sym
->attr
.in_common
)
10185 gfc_error ("COMMON variable '%s' at %L must have constant "
10186 "character length", sym
->name
, &sym
->declared_at
);
10192 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
10193 apply_default_init_local (sym
); /* Try to apply a default initialization. */
10195 /* Determine if the symbol may not have an initializer. */
10196 no_init_flag
= automatic_flag
= 0;
10197 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
10198 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
10200 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
10201 && is_non_constant_shape_array (sym
))
10203 no_init_flag
= automatic_flag
= 1;
10205 /* Also, they must not have the SAVE attribute.
10206 SAVE_IMPLICIT is checked below. */
10207 if (sym
->as
&& sym
->attr
.codimension
)
10209 int corank
= sym
->as
->corank
;
10210 sym
->as
->corank
= 0;
10211 no_init_flag
= automatic_flag
= is_non_constant_shape_array (sym
);
10212 sym
->as
->corank
= corank
;
10214 if (automatic_flag
&& sym
->attr
.save
== SAVE_EXPLICIT
)
10216 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
10221 /* Ensure that any initializer is simplified. */
10223 gfc_simplify_expr (sym
->value
, 1);
10225 /* Reject illegal initializers. */
10226 if (!sym
->mark
&& sym
->value
)
10228 if (sym
->attr
.allocatable
|| (sym
->ts
.type
== BT_CLASS
10229 && CLASS_DATA (sym
)->attr
.allocatable
))
10230 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10231 sym
->name
, &sym
->declared_at
);
10232 else if (sym
->attr
.external
)
10233 gfc_error ("External '%s' at %L cannot have an initializer",
10234 sym
->name
, &sym
->declared_at
);
10235 else if (sym
->attr
.dummy
10236 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
10237 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10238 sym
->name
, &sym
->declared_at
);
10239 else if (sym
->attr
.intrinsic
)
10240 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10241 sym
->name
, &sym
->declared_at
);
10242 else if (sym
->attr
.result
)
10243 gfc_error ("Function result '%s' at %L cannot have an initializer",
10244 sym
->name
, &sym
->declared_at
);
10245 else if (automatic_flag
)
10246 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10247 sym
->name
, &sym
->declared_at
);
10249 goto no_init_error
;
10254 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
10255 return resolve_fl_variable_derived (sym
, no_init_flag
);
10261 /* Resolve a procedure. */
10264 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
10266 gfc_formal_arglist
*arg
;
10268 if (sym
->attr
.function
10269 && resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
10272 if (sym
->ts
.type
== BT_CHARACTER
)
10274 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
10276 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
10277 && resolve_charlen (cl
) == FAILURE
)
10280 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
10281 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
10283 gfc_error ("Character-valued statement function '%s' at %L must "
10284 "have constant length", sym
->name
, &sym
->declared_at
);
10289 /* Ensure that derived type for are not of a private type. Internal
10290 module procedures are excluded by 2.2.3.3 - i.e., they are not
10291 externally accessible and can access all the objects accessible in
10293 if (!(sym
->ns
->parent
10294 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
10295 && gfc_check_symbol_access (sym
))
10297 gfc_interface
*iface
;
10299 for (arg
= sym
->formal
; arg
; arg
= arg
->next
)
10302 && arg
->sym
->ts
.type
== BT_DERIVED
10303 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10304 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10305 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: '%s' is of a "
10306 "PRIVATE type and cannot be a dummy argument"
10307 " of '%s', which is PUBLIC at %L",
10308 arg
->sym
->name
, sym
->name
, &sym
->declared_at
)
10311 /* Stop this message from recurring. */
10312 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10317 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10318 PRIVATE to the containing module. */
10319 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10321 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10324 && arg
->sym
->ts
.type
== BT_DERIVED
10325 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10326 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10327 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10328 "'%s' in PUBLIC interface '%s' at %L "
10329 "takes dummy arguments of '%s' which is "
10330 "PRIVATE", iface
->sym
->name
, sym
->name
,
10331 &iface
->sym
->declared_at
,
10332 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10334 /* Stop this message from recurring. */
10335 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10341 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10342 PRIVATE to the containing module. */
10343 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
10345 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
10348 && arg
->sym
->ts
.type
== BT_DERIVED
10349 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
10350 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
10351 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
10352 "'%s' in PUBLIC interface '%s' at %L "
10353 "takes dummy arguments of '%s' which is "
10354 "PRIVATE", iface
->sym
->name
, sym
->name
,
10355 &iface
->sym
->declared_at
,
10356 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
10358 /* Stop this message from recurring. */
10359 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
10366 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
10367 && !sym
->attr
.proc_pointer
)
10369 gfc_error ("Function '%s' at %L cannot have an initializer",
10370 sym
->name
, &sym
->declared_at
);
10374 /* An external symbol may not have an initializer because it is taken to be
10375 a procedure. Exception: Procedure Pointers. */
10376 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
10378 gfc_error ("External object '%s' at %L may not have an initializer",
10379 sym
->name
, &sym
->declared_at
);
10383 /* An elemental function is required to return a scalar 12.7.1 */
10384 if (sym
->attr
.elemental
&& sym
->attr
.function
&& sym
->as
)
10386 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10387 "result", sym
->name
, &sym
->declared_at
);
10388 /* Reset so that the error only occurs once. */
10389 sym
->attr
.elemental
= 0;
10393 if (sym
->attr
.proc
== PROC_ST_FUNCTION
10394 && (sym
->attr
.allocatable
|| sym
->attr
.pointer
))
10396 gfc_error ("Statement function '%s' at %L may not have pointer or "
10397 "allocatable attribute", sym
->name
, &sym
->declared_at
);
10401 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10402 char-len-param shall not be array-valued, pointer-valued, recursive
10403 or pure. ....snip... A character value of * may only be used in the
10404 following ways: (i) Dummy arg of procedure - dummy associates with
10405 actual length; (ii) To declare a named constant; or (iii) External
10406 function - but length must be declared in calling scoping unit. */
10407 if (sym
->attr
.function
10408 && sym
->ts
.type
== BT_CHARACTER
10409 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
10411 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
10412 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
10414 if (sym
->as
&& sym
->as
->rank
)
10415 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10416 "array-valued", sym
->name
, &sym
->declared_at
);
10418 if (sym
->attr
.pointer
)
10419 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10420 "pointer-valued", sym
->name
, &sym
->declared_at
);
10422 if (sym
->attr
.pure
)
10423 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10424 "pure", sym
->name
, &sym
->declared_at
);
10426 if (sym
->attr
.recursive
)
10427 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10428 "recursive", sym
->name
, &sym
->declared_at
);
10433 /* Appendix B.2 of the standard. Contained functions give an
10434 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10435 character length is an F2003 feature. */
10436 if (!sym
->attr
.contained
10437 && gfc_current_form
!= FORM_FIXED
10438 && !sym
->ts
.deferred
)
10439 gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
10440 "CHARACTER(*) function '%s' at %L",
10441 sym
->name
, &sym
->declared_at
);
10444 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
10446 gfc_formal_arglist
*curr_arg
;
10447 int has_non_interop_arg
= 0;
10449 if (verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
10450 sym
->common_block
) == FAILURE
)
10452 /* Clear these to prevent looking at them again if there was an
10454 sym
->attr
.is_bind_c
= 0;
10455 sym
->attr
.is_c_interop
= 0;
10456 sym
->ts
.is_c_interop
= 0;
10460 /* So far, no errors have been found. */
10461 sym
->attr
.is_c_interop
= 1;
10462 sym
->ts
.is_c_interop
= 1;
10465 curr_arg
= sym
->formal
;
10466 while (curr_arg
!= NULL
)
10468 /* Skip implicitly typed dummy args here. */
10469 if (curr_arg
->sym
->attr
.implicit_type
== 0)
10470 if (verify_c_interop_param (curr_arg
->sym
) == FAILURE
)
10471 /* If something is found to fail, record the fact so we
10472 can mark the symbol for the procedure as not being
10473 BIND(C) to try and prevent multiple errors being
10475 has_non_interop_arg
= 1;
10477 curr_arg
= curr_arg
->next
;
10480 /* See if any of the arguments were not interoperable and if so, clear
10481 the procedure symbol to prevent duplicate error messages. */
10482 if (has_non_interop_arg
!= 0)
10484 sym
->attr
.is_c_interop
= 0;
10485 sym
->ts
.is_c_interop
= 0;
10486 sym
->attr
.is_bind_c
= 0;
10490 if (!sym
->attr
.proc_pointer
)
10492 if (sym
->attr
.save
== SAVE_EXPLICIT
)
10494 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10495 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10498 if (sym
->attr
.intent
)
10500 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10501 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10504 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
10506 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10507 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10510 if (sym
->attr
.external
&& sym
->attr
.function
10511 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
10512 || sym
->attr
.contained
))
10514 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10515 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
10518 if (strcmp ("ppr@", sym
->name
) == 0)
10520 gfc_error ("Procedure pointer result '%s' at %L "
10521 "is missing the pointer attribute",
10522 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
10531 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10532 been defined and we now know their defined arguments, check that they fulfill
10533 the requirements of the standard for procedures used as finalizers. */
10536 gfc_resolve_finalizers (gfc_symbol
* derived
)
10538 gfc_finalizer
* list
;
10539 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
10540 gfc_try result
= SUCCESS
;
10541 bool seen_scalar
= false;
10543 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->finalizers
)
10546 /* Walk over the list of finalizer-procedures, check them, and if any one
10547 does not fit in with the standard's definition, print an error and remove
10548 it from the list. */
10549 prev_link
= &derived
->f2k_derived
->finalizers
;
10550 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
10556 /* Skip this finalizer if we already resolved it. */
10557 if (list
->proc_tree
)
10559 prev_link
= &(list
->next
);
10563 /* Check this exists and is a SUBROUTINE. */
10564 if (!list
->proc_sym
->attr
.subroutine
)
10566 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10567 list
->proc_sym
->name
, &list
->where
);
10571 /* We should have exactly one argument. */
10572 if (!list
->proc_sym
->formal
|| list
->proc_sym
->formal
->next
)
10574 gfc_error ("FINAL procedure at %L must have exactly one argument",
10578 arg
= list
->proc_sym
->formal
->sym
;
10580 /* This argument must be of our type. */
10581 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
10583 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10584 &arg
->declared_at
, derived
->name
);
10588 /* It must neither be a pointer nor allocatable nor optional. */
10589 if (arg
->attr
.pointer
)
10591 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10592 &arg
->declared_at
);
10595 if (arg
->attr
.allocatable
)
10597 gfc_error ("Argument of FINAL procedure at %L must not be"
10598 " ALLOCATABLE", &arg
->declared_at
);
10601 if (arg
->attr
.optional
)
10603 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10604 &arg
->declared_at
);
10608 /* It must not be INTENT(OUT). */
10609 if (arg
->attr
.intent
== INTENT_OUT
)
10611 gfc_error ("Argument of FINAL procedure at %L must not be"
10612 " INTENT(OUT)", &arg
->declared_at
);
10616 /* Warn if the procedure is non-scalar and not assumed shape. */
10617 if (gfc_option
.warn_surprising
&& arg
->as
&& arg
->as
->rank
> 0
10618 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
10619 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10620 " shape argument", &arg
->declared_at
);
10622 /* Check that it does not match in kind and rank with a FINAL procedure
10623 defined earlier. To really loop over the *earlier* declarations,
10624 we need to walk the tail of the list as new ones were pushed at the
10626 /* TODO: Handle kind parameters once they are implemented. */
10627 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
10628 for (i
= list
->next
; i
; i
= i
->next
)
10630 /* Argument list might be empty; that is an error signalled earlier,
10631 but we nevertheless continued resolving. */
10632 if (i
->proc_sym
->formal
)
10634 gfc_symbol
* i_arg
= i
->proc_sym
->formal
->sym
;
10635 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
10636 if (i_rank
== my_rank
)
10638 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10639 " rank (%d) as '%s'",
10640 list
->proc_sym
->name
, &list
->where
, my_rank
,
10641 i
->proc_sym
->name
);
10647 /* Is this the/a scalar finalizer procedure? */
10648 if (!arg
->as
|| arg
->as
->rank
== 0)
10649 seen_scalar
= true;
10651 /* Find the symtree for this procedure. */
10652 gcc_assert (!list
->proc_tree
);
10653 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
10655 prev_link
= &list
->next
;
10658 /* Remove wrong nodes immediately from the list so we don't risk any
10659 troubles in the future when they might fail later expectations. */
10663 *prev_link
= list
->next
;
10664 gfc_free_finalizer (i
);
10667 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10668 were nodes in the list, must have been for arrays. It is surely a good
10669 idea to have a scalar version there if there's something to finalize. */
10670 if (gfc_option
.warn_surprising
&& result
== SUCCESS
&& !seen_scalar
)
10671 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10672 " defined at %L, suggest also scalar one",
10673 derived
->name
, &derived
->declared_at
);
10675 /* TODO: Remove this error when finalization is finished. */
10676 gfc_error ("Finalization at %L is not yet implemented",
10677 &derived
->declared_at
);
10683 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10686 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
10687 const char* generic_name
, locus where
)
10692 gcc_assert (t1
->specific
&& t2
->specific
);
10693 gcc_assert (!t1
->specific
->is_generic
);
10694 gcc_assert (!t2
->specific
->is_generic
);
10696 sym1
= t1
->specific
->u
.specific
->n
.sym
;
10697 sym2
= t2
->specific
->u
.specific
->n
.sym
;
10702 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10703 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
10704 || sym1
->attr
.function
!= sym2
->attr
.function
)
10706 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10707 " GENERIC '%s' at %L",
10708 sym1
->name
, sym2
->name
, generic_name
, &where
);
10712 /* Compare the interfaces. */
10713 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, 1, 0, NULL
, 0))
10715 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10716 sym1
->name
, sym2
->name
, generic_name
, &where
);
10724 /* Worker function for resolving a generic procedure binding; this is used to
10725 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10727 The difference between those cases is finding possible inherited bindings
10728 that are overridden, as one has to look for them in tb_sym_root,
10729 tb_uop_root or tb_op, respectively. Thus the caller must already find
10730 the super-type and set p->overridden correctly. */
10733 resolve_tb_generic_targets (gfc_symbol
* super_type
,
10734 gfc_typebound_proc
* p
, const char* name
)
10736 gfc_tbp_generic
* target
;
10737 gfc_symtree
* first_target
;
10738 gfc_symtree
* inherited
;
10740 gcc_assert (p
&& p
->is_generic
);
10742 /* Try to find the specific bindings for the symtrees in our target-list. */
10743 gcc_assert (p
->u
.generic
);
10744 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10745 if (!target
->specific
)
10747 gfc_typebound_proc
* overridden_tbp
;
10748 gfc_tbp_generic
* g
;
10749 const char* target_name
;
10751 target_name
= target
->specific_st
->name
;
10753 /* Defined for this type directly. */
10754 if (target
->specific_st
->n
.tb
&& !target
->specific_st
->n
.tb
->error
)
10756 target
->specific
= target
->specific_st
->n
.tb
;
10757 goto specific_found
;
10760 /* Look for an inherited specific binding. */
10763 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
10768 gcc_assert (inherited
->n
.tb
);
10769 target
->specific
= inherited
->n
.tb
;
10770 goto specific_found
;
10774 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10775 " at %L", target_name
, name
, &p
->where
);
10778 /* Once we've found the specific binding, check it is not ambiguous with
10779 other specifics already found or inherited for the same GENERIC. */
10781 gcc_assert (target
->specific
);
10783 /* This must really be a specific binding! */
10784 if (target
->specific
->is_generic
)
10786 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10787 " '%s' is GENERIC, too", name
, &p
->where
, target_name
);
10791 /* Check those already resolved on this type directly. */
10792 for (g
= p
->u
.generic
; g
; g
= g
->next
)
10793 if (g
!= target
&& g
->specific
10794 && check_generic_tbp_ambiguity (target
, g
, name
, p
->where
)
10798 /* Check for ambiguity with inherited specific targets. */
10799 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
10800 overridden_tbp
= overridden_tbp
->overridden
)
10801 if (overridden_tbp
->is_generic
)
10803 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
10805 gcc_assert (g
->specific
);
10806 if (check_generic_tbp_ambiguity (target
, g
,
10807 name
, p
->where
) == FAILURE
)
10813 /* If we attempt to "overwrite" a specific binding, this is an error. */
10814 if (p
->overridden
&& !p
->overridden
->is_generic
)
10816 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10817 " the same name", name
, &p
->where
);
10821 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10822 all must have the same attributes here. */
10823 first_target
= p
->u
.generic
->specific
->u
.specific
;
10824 gcc_assert (first_target
);
10825 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
10826 p
->function
= first_target
->n
.sym
->attr
.function
;
10832 /* Resolve a GENERIC procedure binding for a derived type. */
10835 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
10837 gfc_symbol
* super_type
;
10839 /* Find the overridden binding if any. */
10840 st
->n
.tb
->overridden
= NULL
;
10841 super_type
= gfc_get_derived_super_type (derived
);
10844 gfc_symtree
* overridden
;
10845 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
10848 if (overridden
&& overridden
->n
.tb
)
10849 st
->n
.tb
->overridden
= overridden
->n
.tb
;
10852 /* Resolve using worker function. */
10853 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
10857 /* Retrieve the target-procedure of an operator binding and do some checks in
10858 common for intrinsic and user-defined type-bound operators. */
10861 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
10863 gfc_symbol
* target_proc
;
10865 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
10866 target_proc
= target
->specific
->u
.specific
->n
.sym
;
10867 gcc_assert (target_proc
);
10869 /* All operator bindings must have a passed-object dummy argument. */
10870 if (target
->specific
->nopass
)
10872 gfc_error ("Type-bound operator at %L can't be NOPASS", &where
);
10876 return target_proc
;
10880 /* Resolve a type-bound intrinsic operator. */
10883 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
10884 gfc_typebound_proc
* p
)
10886 gfc_symbol
* super_type
;
10887 gfc_tbp_generic
* target
;
10889 /* If there's already an error here, do nothing (but don't fail again). */
10893 /* Operators should always be GENERIC bindings. */
10894 gcc_assert (p
->is_generic
);
10896 /* Look for an overridden binding. */
10897 super_type
= gfc_get_derived_super_type (derived
);
10898 if (super_type
&& super_type
->f2k_derived
)
10899 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
10902 p
->overridden
= NULL
;
10904 /* Resolve general GENERIC properties using worker function. */
10905 if (resolve_tb_generic_targets (super_type
, p
, gfc_op2string (op
)) == FAILURE
)
10908 /* Check the targets to be procedures of correct interface. */
10909 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10911 gfc_symbol
* target_proc
;
10913 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
10917 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
10929 /* Resolve a type-bound user operator (tree-walker callback). */
10931 static gfc_symbol
* resolve_bindings_derived
;
10932 static gfc_try resolve_bindings_result
;
10934 static gfc_try
check_uop_procedure (gfc_symbol
* sym
, locus where
);
10937 resolve_typebound_user_op (gfc_symtree
* stree
)
10939 gfc_symbol
* super_type
;
10940 gfc_tbp_generic
* target
;
10942 gcc_assert (stree
&& stree
->n
.tb
);
10944 if (stree
->n
.tb
->error
)
10947 /* Operators should always be GENERIC bindings. */
10948 gcc_assert (stree
->n
.tb
->is_generic
);
10950 /* Find overridden procedure, if any. */
10951 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
10952 if (super_type
&& super_type
->f2k_derived
)
10954 gfc_symtree
* overridden
;
10955 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
10956 stree
->name
, true, NULL
);
10958 if (overridden
&& overridden
->n
.tb
)
10959 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
10962 stree
->n
.tb
->overridden
= NULL
;
10964 /* Resolve basically using worker function. */
10965 if (resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
)
10969 /* Check the targets to be functions of correct interface. */
10970 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
10972 gfc_symbol
* target_proc
;
10974 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
10978 if (check_uop_procedure (target_proc
, stree
->n
.tb
->where
) == FAILURE
)
10985 resolve_bindings_result
= FAILURE
;
10986 stree
->n
.tb
->error
= 1;
10990 /* Resolve the type-bound procedures for a derived type. */
10993 resolve_typebound_procedure (gfc_symtree
* stree
)
10997 gfc_symbol
* me_arg
;
10998 gfc_symbol
* super_type
;
10999 gfc_component
* comp
;
11001 gcc_assert (stree
);
11003 /* Undefined specific symbol from GENERIC target definition. */
11007 if (stree
->n
.tb
->error
)
11010 /* If this is a GENERIC binding, use that routine. */
11011 if (stree
->n
.tb
->is_generic
)
11013 if (resolve_typebound_generic (resolve_bindings_derived
, stree
)
11019 /* Get the target-procedure to check it. */
11020 gcc_assert (!stree
->n
.tb
->is_generic
);
11021 gcc_assert (stree
->n
.tb
->u
.specific
);
11022 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
11023 where
= stree
->n
.tb
->where
;
11025 /* Default access should already be resolved from the parser. */
11026 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
11028 /* It should be a module procedure or an external procedure with explicit
11029 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11030 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
11031 || (proc
->attr
.proc
!= PROC_MODULE
11032 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
11033 || (proc
->attr
.abstract
&& !stree
->n
.tb
->deferred
))
11035 gfc_error ("'%s' must be a module procedure or an external procedure with"
11036 " an explicit interface at %L", proc
->name
, &where
);
11039 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
11040 stree
->n
.tb
->function
= proc
->attr
.function
;
11042 /* Find the super-type of the current derived type. We could do this once and
11043 store in a global if speed is needed, but as long as not I believe this is
11044 more readable and clearer. */
11045 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
11047 /* If PASS, resolve and check arguments if not already resolved / loaded
11048 from a .mod file. */
11049 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
11051 if (stree
->n
.tb
->pass_arg
)
11053 gfc_formal_arglist
* i
;
11055 /* If an explicit passing argument name is given, walk the arg-list
11056 and look for it. */
11059 stree
->n
.tb
->pass_arg_num
= 1;
11060 for (i
= proc
->formal
; i
; i
= i
->next
)
11062 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
11067 ++stree
->n
.tb
->pass_arg_num
;
11072 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11074 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
11075 stree
->n
.tb
->pass_arg
);
11081 /* Otherwise, take the first one; there should in fact be at least
11083 stree
->n
.tb
->pass_arg_num
= 1;
11086 gfc_error ("Procedure '%s' with PASS at %L must have at"
11087 " least one argument", proc
->name
, &where
);
11090 me_arg
= proc
->formal
->sym
;
11093 /* Now check that the argument-type matches and the passed-object
11094 dummy argument is generally fine. */
11096 gcc_assert (me_arg
);
11098 if (me_arg
->ts
.type
!= BT_CLASS
)
11100 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11101 " at %L", proc
->name
, &where
);
11105 if (CLASS_DATA (me_arg
)->ts
.u
.derived
11106 != resolve_bindings_derived
)
11108 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11109 " the derived-type '%s'", me_arg
->name
, proc
->name
,
11110 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
11114 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
11115 if (CLASS_DATA (me_arg
)->as
&& CLASS_DATA (me_arg
)->as
->rank
> 0)
11117 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11118 " scalar", proc
->name
, &where
);
11121 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
11123 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11124 " be ALLOCATABLE", proc
->name
, &where
);
11127 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
11129 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11130 " be POINTER", proc
->name
, &where
);
11135 /* If we are extending some type, check that we don't override a procedure
11136 flagged NON_OVERRIDABLE. */
11137 stree
->n
.tb
->overridden
= NULL
;
11140 gfc_symtree
* overridden
;
11141 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
11142 stree
->name
, true, NULL
);
11146 if (overridden
->n
.tb
)
11147 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
11149 if (gfc_check_typebound_override (stree
, overridden
) == FAILURE
)
11154 /* See if there's a name collision with a component directly in this type. */
11155 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
11156 if (!strcmp (comp
->name
, stree
->name
))
11158 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11160 stree
->name
, &where
, resolve_bindings_derived
->name
);
11164 /* Try to find a name collision with an inherited component. */
11165 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true))
11167 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11168 " component of '%s'",
11169 stree
->name
, &where
, resolve_bindings_derived
->name
);
11173 stree
->n
.tb
->error
= 0;
11177 resolve_bindings_result
= FAILURE
;
11178 stree
->n
.tb
->error
= 1;
11183 resolve_typebound_procedures (gfc_symbol
* derived
)
11186 gfc_symbol
* super_type
;
11188 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
11191 super_type
= gfc_get_derived_super_type (derived
);
11193 resolve_typebound_procedures (super_type
);
11195 resolve_bindings_derived
= derived
;
11196 resolve_bindings_result
= SUCCESS
;
11198 /* Make sure the vtab has been generated. */
11199 gfc_find_derived_vtab (derived
);
11201 if (derived
->f2k_derived
->tb_sym_root
)
11202 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
11203 &resolve_typebound_procedure
);
11205 if (derived
->f2k_derived
->tb_uop_root
)
11206 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
11207 &resolve_typebound_user_op
);
11209 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
11211 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
11212 if (p
&& resolve_typebound_intrinsic_op (derived
, (gfc_intrinsic_op
) op
,
11214 resolve_bindings_result
= FAILURE
;
11217 return resolve_bindings_result
;
11221 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11222 to give all identical derived types the same backend_decl. */
11224 add_dt_to_dt_list (gfc_symbol
*derived
)
11226 gfc_dt_list
*dt_list
;
11228 for (dt_list
= gfc_derived_types
; dt_list
; dt_list
= dt_list
->next
)
11229 if (derived
== dt_list
->derived
)
11232 dt_list
= gfc_get_dt_list ();
11233 dt_list
->next
= gfc_derived_types
;
11234 dt_list
->derived
= derived
;
11235 gfc_derived_types
= dt_list
;
11239 /* Ensure that a derived-type is really not abstract, meaning that every
11240 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11243 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
11248 if (ensure_not_abstract_walker (sub
, st
->left
) == FAILURE
)
11250 if (ensure_not_abstract_walker (sub
, st
->right
) == FAILURE
)
11253 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
11255 gfc_symtree
* overriding
;
11256 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
11259 gcc_assert (overriding
->n
.tb
);
11260 if (overriding
->n
.tb
->deferred
)
11262 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11263 " '%s' is DEFERRED and not overridden",
11264 sub
->name
, &sub
->declared_at
, st
->name
);
11273 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
11275 /* The algorithm used here is to recursively travel up the ancestry of sub
11276 and for each ancestor-type, check all bindings. If any of them is
11277 DEFERRED, look it up starting from sub and see if the found (overriding)
11278 binding is not DEFERRED.
11279 This is not the most efficient way to do this, but it should be ok and is
11280 clearer than something sophisticated. */
11282 gcc_assert (ancestor
&& !sub
->attr
.abstract
);
11284 if (!ancestor
->attr
.abstract
)
11287 /* Walk bindings of this ancestor. */
11288 if (ancestor
->f2k_derived
)
11291 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
11296 /* Find next ancestor type and recurse on it. */
11297 ancestor
= gfc_get_derived_super_type (ancestor
);
11299 return ensure_not_abstract (sub
, ancestor
);
11305 /* Resolve the components of a derived type. This does not have to wait until
11306 resolution stage, but can be done as soon as the dt declaration has been
11310 resolve_fl_derived0 (gfc_symbol
*sym
)
11312 gfc_symbol
* super_type
;
11315 super_type
= gfc_get_derived_super_type (sym
);
11318 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
11320 gfc_error ("As extending type '%s' at %L has a coarray component, "
11321 "parent type '%s' shall also have one", sym
->name
,
11322 &sym
->declared_at
, super_type
->name
);
11326 /* Ensure the extended type gets resolved before we do. */
11327 if (super_type
&& resolve_fl_derived0 (super_type
) == FAILURE
)
11330 /* An ABSTRACT type must be extensible. */
11331 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
11333 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11334 sym
->name
, &sym
->declared_at
);
11338 for (c
= sym
->components
; c
!= NULL
; c
= c
->next
)
11341 if (c
->attr
.codimension
/* FIXME: c->as check due to PR 43412. */
11342 && (!c
->attr
.allocatable
|| (c
->as
&& c
->as
->type
!= AS_DEFERRED
)))
11344 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11345 "deferred shape", c
->name
, &c
->loc
);
11350 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
11351 && c
->ts
.u
.derived
->ts
.is_iso_c
)
11353 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11354 "shall not be a coarray", c
->name
, &c
->loc
);
11359 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
11360 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
11361 || c
->attr
.allocatable
))
11363 gfc_error ("Component '%s' at %L with coarray component "
11364 "shall be a nonpointer, nonallocatable scalar",
11370 if (c
->attr
.contiguous
&& (!c
->attr
.dimension
|| !c
->attr
.pointer
))
11372 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11373 "is not an array pointer", c
->name
, &c
->loc
);
11377 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
11379 if (c
->ts
.interface
->attr
.procedure
&& !sym
->attr
.vtype
)
11380 gfc_error ("Interface '%s', used by procedure pointer component "
11381 "'%s' at %L, is declared in a later PROCEDURE statement",
11382 c
->ts
.interface
->name
, c
->name
, &c
->loc
);
11384 /* Get the attributes from the interface (now resolved). */
11385 if (c
->ts
.interface
->attr
.if_source
11386 || c
->ts
.interface
->attr
.intrinsic
)
11388 gfc_symbol
*ifc
= c
->ts
.interface
;
11390 if (ifc
->formal
&& !ifc
->formal_ns
)
11391 resolve_symbol (ifc
);
11393 if (ifc
->attr
.intrinsic
)
11394 resolve_intrinsic (ifc
, &ifc
->declared_at
);
11398 c
->ts
= ifc
->result
->ts
;
11399 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
11400 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
11401 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
11402 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
11407 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
11408 c
->attr
.pointer
= ifc
->attr
.pointer
;
11409 c
->attr
.dimension
= ifc
->attr
.dimension
;
11410 c
->as
= gfc_copy_array_spec (ifc
->as
);
11412 c
->ts
.interface
= ifc
;
11413 c
->attr
.function
= ifc
->attr
.function
;
11414 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
11415 gfc_copy_formal_args_ppc (c
, ifc
);
11417 c
->attr
.pure
= ifc
->attr
.pure
;
11418 c
->attr
.elemental
= ifc
->attr
.elemental
;
11419 c
->attr
.recursive
= ifc
->attr
.recursive
;
11420 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
11421 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
11422 /* Replace symbols in array spec. */
11426 for (i
= 0; i
< c
->as
->rank
; i
++)
11428 gfc_expr_replace_comp (c
->as
->lower
[i
], c
);
11429 gfc_expr_replace_comp (c
->as
->upper
[i
], c
);
11432 /* Copy char length. */
11433 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
11435 gfc_charlen
*cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
11436 gfc_expr_replace_comp (cl
->length
, c
);
11437 if (cl
->length
&& !cl
->resolved
11438 && gfc_resolve_expr (cl
->length
) == FAILURE
)
11443 else if (!sym
->attr
.vtype
&& c
->ts
.interface
->name
[0] != '\0')
11445 gfc_error ("Interface '%s' of procedure pointer component "
11446 "'%s' at %L must be explicit", c
->ts
.interface
->name
,
11451 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
11453 /* Since PPCs are not implicitly typed, a PPC without an explicit
11454 interface must be a subroutine. */
11455 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
11458 /* Procedure pointer components: Check PASS arg. */
11459 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0
11460 && !sym
->attr
.vtype
)
11462 gfc_symbol
* me_arg
;
11464 if (c
->tb
->pass_arg
)
11466 gfc_formal_arglist
* i
;
11468 /* If an explicit passing argument name is given, walk the arg-list
11469 and look for it. */
11472 c
->tb
->pass_arg_num
= 1;
11473 for (i
= c
->formal
; i
; i
= i
->next
)
11475 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
11480 c
->tb
->pass_arg_num
++;
11485 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11486 "at %L has no argument '%s'", c
->name
,
11487 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
11494 /* Otherwise, take the first one; there should in fact be at least
11496 c
->tb
->pass_arg_num
= 1;
11499 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11500 "must have at least one argument",
11505 me_arg
= c
->formal
->sym
;
11508 /* Now check that the argument-type matches. */
11509 gcc_assert (me_arg
);
11510 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
11511 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
11512 || (me_arg
->ts
.type
== BT_CLASS
11513 && CLASS_DATA (me_arg
)->ts
.u
.derived
!= sym
))
11515 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11516 " the derived type '%s'", me_arg
->name
, c
->name
,
11517 me_arg
->name
, &c
->loc
, sym
->name
);
11522 /* Check for C453. */
11523 if (me_arg
->attr
.dimension
)
11525 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11526 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
11532 if (me_arg
->attr
.pointer
)
11534 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11535 "may not have the POINTER attribute", me_arg
->name
,
11536 c
->name
, me_arg
->name
, &c
->loc
);
11541 if (me_arg
->attr
.allocatable
)
11543 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11544 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
11545 me_arg
->name
, &c
->loc
);
11550 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
11551 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11552 " at %L", c
->name
, &c
->loc
);
11556 /* Check type-spec if this is not the parent-type component. */
11557 if ((!sym
->attr
.extension
|| c
!= sym
->components
) && !sym
->attr
.vtype
11558 && resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
) == FAILURE
)
11561 /* If this type is an extension, set the accessibility of the parent
11563 if (super_type
&& c
== sym
->components
11564 && strcmp (super_type
->name
, c
->name
) == 0)
11565 c
->attr
.access
= super_type
->attr
.access
;
11567 /* If this type is an extension, see if this component has the same name
11568 as an inherited type-bound procedure. */
11569 if (super_type
&& !sym
->attr
.is_class
11570 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
11572 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11573 " inherited type-bound procedure",
11574 c
->name
, sym
->name
, &c
->loc
);
11578 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
11579 && !c
->ts
.deferred
)
11581 if (c
->ts
.u
.cl
->length
== NULL
11582 || (resolve_charlen (c
->ts
.u
.cl
) == FAILURE
)
11583 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
11585 gfc_error ("Character length of component '%s' needs to "
11586 "be a constant specification expression at %L",
11588 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
11593 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
11594 && !c
->attr
.pointer
&& !c
->attr
.allocatable
)
11596 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11597 "length must be a POINTER or ALLOCATABLE",
11598 c
->name
, sym
->name
, &c
->loc
);
11602 if (c
->ts
.type
== BT_DERIVED
11603 && sym
->component_access
!= ACCESS_PRIVATE
11604 && gfc_check_symbol_access (sym
)
11605 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
11606 && !c
->ts
.u
.derived
->attr
.use_assoc
11607 && !gfc_check_symbol_access (c
->ts
.u
.derived
)
11608 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: the component '%s' "
11609 "is a PRIVATE type and cannot be a component of "
11610 "'%s', which is PUBLIC at %L", c
->name
,
11611 sym
->name
, &sym
->declared_at
) == FAILURE
)
11614 if ((sym
->attr
.sequence
|| sym
->attr
.is_bind_c
) && c
->ts
.type
== BT_CLASS
)
11616 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11617 "type %s", c
->name
, &c
->loc
, sym
->name
);
11621 if (sym
->attr
.sequence
)
11623 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
11625 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11626 "not have the SEQUENCE attribute",
11627 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
11632 if (!sym
->attr
.is_class
&& c
->ts
.type
== BT_DERIVED
&& !sym
->attr
.vtype
11633 && c
->attr
.pointer
&& c
->ts
.u
.derived
->components
== NULL
11634 && !c
->ts
.u
.derived
->attr
.zero_comp
)
11636 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11637 "that has not been declared", c
->name
, sym
->name
,
11642 if (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
11643 && CLASS_DATA (c
)->attr
.class_pointer
11644 && CLASS_DATA (c
)->ts
.u
.derived
->components
== NULL
11645 && !CLASS_DATA (c
)->ts
.u
.derived
->attr
.zero_comp
)
11647 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11648 "that has not been declared", c
->name
, sym
->name
,
11654 if (c
->ts
.type
== BT_CLASS
&& c
->attr
.flavor
!= FL_PROCEDURE
11655 && (!c
->attr
.class_ok
11656 || !(CLASS_DATA (c
)->attr
.class_pointer
11657 || CLASS_DATA (c
)->attr
.allocatable
)))
11659 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11660 "or pointer", c
->name
, &c
->loc
);
11664 /* Ensure that all the derived type components are put on the
11665 derived type list; even in formal namespaces, where derived type
11666 pointer components might not have been declared. */
11667 if (c
->ts
.type
== BT_DERIVED
11669 && c
->ts
.u
.derived
->components
11671 && sym
!= c
->ts
.u
.derived
)
11672 add_dt_to_dt_list (c
->ts
.u
.derived
);
11674 if (gfc_resolve_array_spec (c
->as
, !(c
->attr
.pointer
11675 || c
->attr
.proc_pointer
11676 || c
->attr
.allocatable
)) == FAILURE
)
11680 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11681 all DEFERRED bindings are overridden. */
11682 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
11683 && !sym
->attr
.is_class
11684 && ensure_not_abstract (sym
, super_type
) == FAILURE
)
11687 /* Add derived type to the derived type list. */
11688 add_dt_to_dt_list (sym
);
11694 /* The following procedure does the full resolution of a derived type,
11695 including resolution of all type-bound procedures (if present). In contrast
11696 to 'resolve_fl_derived0' this can only be done after the module has been
11697 parsed completely. */
11700 resolve_fl_derived (gfc_symbol
*sym
)
11702 if (sym
->attr
.is_class
&& sym
->ts
.u
.derived
== NULL
)
11704 /* Fix up incomplete CLASS symbols. */
11705 gfc_component
*data
= gfc_find_component (sym
, "_data", true, true);
11706 gfc_component
*vptr
= gfc_find_component (sym
, "_vptr", true, true);
11707 if (vptr
->ts
.u
.derived
== NULL
)
11709 gfc_symbol
*vtab
= gfc_find_derived_vtab (data
->ts
.u
.derived
);
11711 vptr
->ts
.u
.derived
= vtab
->ts
.u
.derived
;
11715 if (resolve_fl_derived0 (sym
) == FAILURE
)
11718 /* Resolve the type-bound procedures. */
11719 if (resolve_typebound_procedures (sym
) == FAILURE
)
11722 /* Resolve the finalizer procedures. */
11723 if (gfc_resolve_finalizers (sym
) == FAILURE
)
11731 resolve_fl_namelist (gfc_symbol
*sym
)
11736 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11738 /* Check again, the check in match only works if NAMELIST comes
11740 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SIZE
)
11742 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
11743 "allowed", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11747 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
11748 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST array "
11749 "object '%s' with assumed shape in namelist "
11750 "'%s' at %L", nl
->sym
->name
, sym
->name
,
11751 &sym
->declared_at
) == FAILURE
)
11754 if (is_non_constant_shape_array (nl
->sym
)
11755 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST array "
11756 "object '%s' with nonconstant shape in namelist "
11757 "'%s' at %L", nl
->sym
->name
, sym
->name
,
11758 &sym
->declared_at
) == FAILURE
)
11761 if (nl
->sym
->ts
.type
== BT_CHARACTER
11762 && (nl
->sym
->ts
.u
.cl
->length
== NULL
11763 || !gfc_is_constant_expr (nl
->sym
->ts
.u
.cl
->length
))
11764 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST object "
11765 "'%s' with nonconstant character length in "
11766 "namelist '%s' at %L", nl
->sym
->name
, sym
->name
,
11767 &sym
->declared_at
) == FAILURE
)
11770 /* FIXME: Once UDDTIO is implemented, the following can be
11772 if (nl
->sym
->ts
.type
== BT_CLASS
)
11774 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
11775 "polymorphic and requires a defined input/output "
11776 "procedure", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11780 if (nl
->sym
->ts
.type
== BT_DERIVED
11781 && (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
11782 || nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
))
11784 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: NAMELIST object "
11785 "'%s' in namelist '%s' at %L with ALLOCATABLE "
11786 "or POINTER components", nl
->sym
->name
,
11787 sym
->name
, &sym
->declared_at
) == FAILURE
)
11790 /* FIXME: Once UDDTIO is implemented, the following can be
11792 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
11793 "ALLOCATABLE or POINTER components and thus requires "
11794 "a defined input/output procedure", nl
->sym
->name
,
11795 sym
->name
, &sym
->declared_at
);
11800 /* Reject PRIVATE objects in a PUBLIC namelist. */
11801 if (gfc_check_symbol_access (sym
))
11803 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11805 if (!nl
->sym
->attr
.use_assoc
11806 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
11807 && !gfc_check_symbol_access (nl
->sym
))
11809 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11810 "cannot be member of PUBLIC namelist '%s' at %L",
11811 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11815 /* Types with private components that came here by USE-association. */
11816 if (nl
->sym
->ts
.type
== BT_DERIVED
11817 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
11819 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11820 "components and cannot be member of namelist '%s' at %L",
11821 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11825 /* Types with private components that are defined in the same module. */
11826 if (nl
->sym
->ts
.type
== BT_DERIVED
11827 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
11828 && nl
->sym
->ts
.u
.derived
->attr
.private_comp
)
11830 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11831 "cannot be a member of PUBLIC namelist '%s' at %L",
11832 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
11839 /* 14.1.2 A module or internal procedure represent local entities
11840 of the same type as a namelist member and so are not allowed. */
11841 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
11843 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
11846 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
11847 if ((nl
->sym
== sym
->ns
->proc_name
)
11849 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
11853 if (nl
->sym
&& nl
->sym
->name
)
11854 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
11855 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
11857 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11858 "attribute in '%s' at %L", nlsym
->name
,
11859 &sym
->declared_at
);
11869 resolve_fl_parameter (gfc_symbol
*sym
)
11871 /* A parameter array's shape needs to be constant. */
11872 if (sym
->as
!= NULL
11873 && (sym
->as
->type
== AS_DEFERRED
11874 || is_non_constant_shape_array (sym
)))
11876 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11877 "or of deferred shape", sym
->name
, &sym
->declared_at
);
11881 /* Make sure a parameter that has been implicitly typed still
11882 matches the implicit type, since PARAMETER statements can precede
11883 IMPLICIT statements. */
11884 if (sym
->attr
.implicit_type
11885 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
11888 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11889 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
11893 /* Make sure the types of derived parameters are consistent. This
11894 type checking is deferred until resolution because the type may
11895 refer to a derived type from the host. */
11896 if (sym
->ts
.type
== BT_DERIVED
11897 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
11899 gfc_error ("Incompatible derived type in PARAMETER at %L",
11900 &sym
->value
->where
);
11907 /* Do anything necessary to resolve a symbol. Right now, we just
11908 assume that an otherwise unknown symbol is a variable. This sort
11909 of thing commonly happens for symbols in module. */
11912 resolve_symbol (gfc_symbol
*sym
)
11914 int check_constant
, mp_flag
;
11915 gfc_symtree
*symtree
;
11916 gfc_symtree
*this_symtree
;
11920 if (sym
->attr
.flavor
== FL_UNKNOWN
)
11923 /* If we find that a flavorless symbol is an interface in one of the
11924 parent namespaces, find its symtree in this namespace, free the
11925 symbol and set the symtree to point to the interface symbol. */
11926 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
11928 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
11929 if (symtree
&& (symtree
->n
.sym
->generic
||
11930 (symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
11931 && sym
->ns
->construct_entities
)))
11933 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
11935 gfc_release_symbol (sym
);
11936 symtree
->n
.sym
->refs
++;
11937 this_symtree
->n
.sym
= symtree
->n
.sym
;
11942 /* Otherwise give it a flavor according to such attributes as
11944 if (sym
->attr
.external
== 0 && sym
->attr
.intrinsic
== 0)
11945 sym
->attr
.flavor
= FL_VARIABLE
;
11948 sym
->attr
.flavor
= FL_PROCEDURE
;
11949 if (sym
->attr
.dimension
)
11950 sym
->attr
.function
= 1;
11954 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
11955 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
11957 if (sym
->attr
.procedure
&& sym
->ts
.interface
11958 && sym
->attr
.if_source
!= IFSRC_DECL
11959 && resolve_procedure_interface (sym
) == FAILURE
)
11962 if (sym
->attr
.is_protected
&& !sym
->attr
.proc_pointer
11963 && (sym
->attr
.procedure
|| sym
->attr
.external
))
11965 if (sym
->attr
.external
)
11966 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
11967 "at %L", &sym
->declared_at
);
11969 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
11970 "at %L", &sym
->declared_at
);
11977 if (sym
->attr
.contiguous
11978 && (!sym
->attr
.dimension
|| (sym
->as
->type
!= AS_ASSUMED_SHAPE
11979 && !sym
->attr
.pointer
)))
11981 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
11982 "array pointer or an assumed-shape array", sym
->name
,
11983 &sym
->declared_at
);
11987 if (sym
->attr
.flavor
== FL_DERIVED
&& resolve_fl_derived (sym
) == FAILURE
)
11990 /* Symbols that are module procedures with results (functions) have
11991 the types and array specification copied for type checking in
11992 procedures that call them, as well as for saving to a module
11993 file. These symbols can't stand the scrutiny that their results
11995 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
11997 /* Make sure that the intrinsic is consistent with its internal
11998 representation. This needs to be done before assigning a default
11999 type to avoid spurious warnings. */
12000 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
12001 && resolve_intrinsic (sym
, &sym
->declared_at
) == FAILURE
)
12004 /* Resolve associate names. */
12006 resolve_assoc_var (sym
, true);
12008 /* Assign default type to symbols that need one and don't have one. */
12009 if (sym
->ts
.type
== BT_UNKNOWN
)
12011 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
12012 gfc_set_default_type (sym
, 1, NULL
);
12014 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
12015 && !sym
->attr
.function
&& !sym
->attr
.subroutine
12016 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
12017 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
12019 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
12021 /* The specific case of an external procedure should emit an error
12022 in the case that there is no implicit type. */
12024 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
12027 /* Result may be in another namespace. */
12028 resolve_symbol (sym
->result
);
12030 if (!sym
->result
->attr
.proc_pointer
)
12032 sym
->ts
= sym
->result
->ts
;
12033 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
12034 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
12035 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
12036 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
12037 sym
->attr
.contiguous
= sym
->result
->attr
.contiguous
;
12042 else if (mp_flag
&& sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
12043 gfc_resolve_array_spec (sym
->result
->as
, false);
12045 /* Assumed size arrays and assumed shape arrays must be dummy
12046 arguments. Array-spec's of implied-shape should have been resolved to
12047 AS_EXPLICIT already. */
12051 gcc_assert (sym
->as
->type
!= AS_IMPLIED_SHAPE
);
12052 if (((sym
->as
->type
== AS_ASSUMED_SIZE
&& !sym
->as
->cp_was_assumed
)
12053 || sym
->as
->type
== AS_ASSUMED_SHAPE
)
12054 && sym
->attr
.dummy
== 0)
12056 if (sym
->as
->type
== AS_ASSUMED_SIZE
)
12057 gfc_error ("Assumed size array at %L must be a dummy argument",
12058 &sym
->declared_at
);
12060 gfc_error ("Assumed shape array at %L must be a dummy argument",
12061 &sym
->declared_at
);
12066 /* Make sure symbols with known intent or optional are really dummy
12067 variable. Because of ENTRY statement, this has to be deferred
12068 until resolution time. */
12070 if (!sym
->attr
.dummy
12071 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
12073 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
12077 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
12079 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12080 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
12084 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
12086 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
12087 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
12089 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12090 "attribute must have constant length",
12091 sym
->name
, &sym
->declared_at
);
12095 if (sym
->ts
.is_c_interop
12096 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
12098 gfc_error ("C interoperable character dummy variable '%s' at %L "
12099 "with VALUE attribute must have length one",
12100 sym
->name
, &sym
->declared_at
);
12105 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12106 do this for something that was implicitly typed because that is handled
12107 in gfc_set_default_type. Handle dummy arguments and procedure
12108 definitions separately. Also, anything that is use associated is not
12109 handled here but instead is handled in the module it is declared in.
12110 Finally, derived type definitions are allowed to be BIND(C) since that
12111 only implies that they're interoperable, and they are checked fully for
12112 interoperability when a variable is declared of that type. */
12113 if (sym
->attr
.is_bind_c
&& sym
->attr
.implicit_type
== 0 &&
12114 sym
->attr
.use_assoc
== 0 && sym
->attr
.dummy
== 0 &&
12115 sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.flavor
!= FL_DERIVED
)
12117 gfc_try t
= SUCCESS
;
12119 /* First, make sure the variable is declared at the
12120 module-level scope (J3/04-007, Section 15.3). */
12121 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
12122 sym
->attr
.in_common
== 0)
12124 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12125 "is neither a COMMON block nor declared at the "
12126 "module level scope", sym
->name
, &(sym
->declared_at
));
12129 else if (sym
->common_head
!= NULL
)
12131 t
= verify_com_block_vars_c_interop (sym
->common_head
);
12135 /* If type() declaration, we need to verify that the components
12136 of the given type are all C interoperable, etc. */
12137 if (sym
->ts
.type
== BT_DERIVED
&&
12138 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
12140 /* Make sure the user marked the derived type as BIND(C). If
12141 not, call the verify routine. This could print an error
12142 for the derived type more than once if multiple variables
12143 of that type are declared. */
12144 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
12145 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
12149 /* Verify the variable itself as C interoperable if it
12150 is BIND(C). It is not possible for this to succeed if
12151 the verify_bind_c_derived_type failed, so don't have to handle
12152 any error returned by verify_bind_c_derived_type. */
12153 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
12154 sym
->common_block
);
12159 /* clear the is_bind_c flag to prevent reporting errors more than
12160 once if something failed. */
12161 sym
->attr
.is_bind_c
= 0;
12166 /* If a derived type symbol has reached this point, without its
12167 type being declared, we have an error. Notice that most
12168 conditions that produce undefined derived types have already
12169 been dealt with. However, the likes of:
12170 implicit type(t) (t) ..... call foo (t) will get us here if
12171 the type is not declared in the scope of the implicit
12172 statement. Change the type to BT_UNKNOWN, both because it is so
12173 and to prevent an ICE. */
12174 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->components
== NULL
12175 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
12177 gfc_error ("The derived type '%s' at %L is of type '%s', "
12178 "which has not been defined", sym
->name
,
12179 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
12180 sym
->ts
.type
= BT_UNKNOWN
;
12184 /* Make sure that the derived type has been resolved and that the
12185 derived type is visible in the symbol's namespace, if it is a
12186 module function and is not PRIVATE. */
12187 if (sym
->ts
.type
== BT_DERIVED
12188 && sym
->ts
.u
.derived
->attr
.use_assoc
12189 && sym
->ns
->proc_name
12190 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
12194 if (resolve_fl_derived (sym
->ts
.u
.derived
) == FAILURE
)
12197 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 1, &ds
);
12198 if (!ds
&& sym
->attr
.function
&& gfc_check_symbol_access (sym
))
12200 symtree
= gfc_new_symtree (&sym
->ns
->sym_root
,
12201 sym
->ts
.u
.derived
->name
);
12202 symtree
->n
.sym
= sym
->ts
.u
.derived
;
12203 sym
->ts
.u
.derived
->refs
++;
12207 /* Unless the derived-type declaration is use associated, Fortran 95
12208 does not allow public entries of private derived types.
12209 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12210 161 in 95-006r3. */
12211 if (sym
->ts
.type
== BT_DERIVED
12212 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12213 && !sym
->ts
.u
.derived
->attr
.use_assoc
12214 && gfc_check_symbol_access (sym
)
12215 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
12216 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC %s '%s' at %L "
12217 "of PRIVATE derived type '%s'",
12218 (sym
->attr
.flavor
== FL_PARAMETER
) ? "parameter"
12219 : "variable", sym
->name
, &sym
->declared_at
,
12220 sym
->ts
.u
.derived
->name
) == FAILURE
)
12223 /* F2008, C1302. */
12224 if (sym
->ts
.type
== BT_DERIVED
12225 && ((sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
12226 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
12227 || sym
->ts
.u
.derived
->attr
.lock_comp
)
12228 && !sym
->attr
.codimension
&& !sym
->ts
.u
.derived
->attr
.coarray_comp
)
12230 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
12231 "type LOCK_TYPE must be a coarray", sym
->name
,
12232 &sym
->declared_at
);
12236 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12237 default initialization is defined (5.1.2.4.4). */
12238 if (sym
->ts
.type
== BT_DERIVED
12240 && sym
->attr
.intent
== INTENT_OUT
12242 && sym
->as
->type
== AS_ASSUMED_SIZE
)
12244 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
12246 if (c
->initializer
)
12248 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12249 "ASSUMED SIZE and so cannot have a default initializer",
12250 sym
->name
, &sym
->declared_at
);
12257 if (sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.dummy
12258 && sym
->attr
.intent
== INTENT_OUT
&& sym
->attr
.lock_comp
)
12260 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
12261 "INTENT(OUT)", sym
->name
, &sym
->declared_at
);
12266 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12267 || sym
->attr
.codimension
)
12268 && (sym
->attr
.result
|| sym
->result
== sym
))
12270 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12271 "a coarray component", sym
->name
, &sym
->declared_at
);
12276 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
12277 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
12279 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12280 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
12285 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
12286 && (sym
->attr
.codimension
|| sym
->attr
.pointer
|| sym
->attr
.dimension
12287 || sym
->attr
.allocatable
))
12289 gfc_error ("Variable '%s' at %L with coarray component "
12290 "shall be a nonpointer, nonallocatable scalar",
12291 sym
->name
, &sym
->declared_at
);
12295 /* F2008, C526. The function-result case was handled above. */
12296 if (sym
->attr
.codimension
12297 && !(sym
->attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
12298 || sym
->ns
->save_all
12299 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12300 || sym
->ns
->proc_name
->attr
.is_main_program
12301 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
12303 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
12304 "nor a dummy argument", sym
->name
, &sym
->declared_at
);
12307 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12308 else if (sym
->attr
.codimension
&& !sym
->attr
.allocatable
12309 && sym
->as
&& sym
->as
->cotype
== AS_DEFERRED
)
12311 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12312 "deferred shape", sym
->name
, &sym
->declared_at
);
12315 else if (sym
->attr
.codimension
&& sym
->attr
.allocatable
12316 && (sym
->as
->type
!= AS_DEFERRED
|| sym
->as
->cotype
!= AS_DEFERRED
))
12318 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12319 "deferred shape", sym
->name
, &sym
->declared_at
);
12324 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
12325 || (sym
->attr
.codimension
&& sym
->attr
.allocatable
))
12326 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
12328 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12329 "allocatable coarray or have coarray components",
12330 sym
->name
, &sym
->declared_at
);
12334 if (sym
->attr
.codimension
&& sym
->attr
.dummy
12335 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
12337 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12338 "procedure '%s'", sym
->name
, &sym
->declared_at
,
12339 sym
->ns
->proc_name
->name
);
12343 switch (sym
->attr
.flavor
)
12346 if (resolve_fl_variable (sym
, mp_flag
) == FAILURE
)
12351 if (resolve_fl_procedure (sym
, mp_flag
) == FAILURE
)
12356 if (resolve_fl_namelist (sym
) == FAILURE
)
12361 if (resolve_fl_parameter (sym
) == FAILURE
)
12369 /* Resolve array specifier. Check as well some constraints
12370 on COMMON blocks. */
12372 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
12374 /* Set the formal_arg_flag so that check_conflict will not throw
12375 an error for host associated variables in the specification
12376 expression for an array_valued function. */
12377 if (sym
->attr
.function
&& sym
->as
)
12378 formal_arg_flag
= 1;
12380 gfc_resolve_array_spec (sym
->as
, check_constant
);
12382 formal_arg_flag
= 0;
12384 /* Resolve formal namespaces. */
12385 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
12386 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
12387 gfc_resolve (sym
->formal_ns
);
12389 /* Make sure the formal namespace is present. */
12390 if (sym
->formal
&& !sym
->formal_ns
)
12392 gfc_formal_arglist
*formal
= sym
->formal
;
12393 while (formal
&& !formal
->sym
)
12394 formal
= formal
->next
;
12398 sym
->formal_ns
= formal
->sym
->ns
;
12399 sym
->formal_ns
->refs
++;
12403 /* Check threadprivate restrictions. */
12404 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
&& !sym
->ns
->save_all
12405 && (!sym
->attr
.in_common
12406 && sym
->module
== NULL
12407 && (sym
->ns
->proc_name
== NULL
12408 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
12409 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
12411 /* If we have come this far we can apply default-initializers, as
12412 described in 14.7.5, to those variables that have not already
12413 been assigned one. */
12414 if (sym
->ts
.type
== BT_DERIVED
12415 && sym
->ns
== gfc_current_ns
12417 && !sym
->attr
.allocatable
12418 && !sym
->attr
.alloc_comp
)
12420 symbol_attribute
*a
= &sym
->attr
;
12422 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
12423 && !a
->in_common
&& !a
->use_assoc
12424 && (a
->referenced
|| a
->result
)
12425 && !(a
->function
&& sym
!= sym
->result
))
12426 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
12427 apply_default_init (sym
);
12430 if (sym
->ts
.type
== BT_CLASS
&& sym
->ns
== gfc_current_ns
12431 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
12432 && !CLASS_DATA (sym
)->attr
.class_pointer
12433 && !CLASS_DATA (sym
)->attr
.allocatable
)
12434 apply_default_init (sym
);
12436 /* If this symbol has a type-spec, check it. */
12437 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
12438 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
12439 if (resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
)
12445 /************* Resolve DATA statements *************/
12449 gfc_data_value
*vnode
;
12455 /* Advance the values structure to point to the next value in the data list. */
12458 next_data_value (void)
12460 while (mpz_cmp_ui (values
.left
, 0) == 0)
12463 if (values
.vnode
->next
== NULL
)
12466 values
.vnode
= values
.vnode
->next
;
12467 mpz_set (values
.left
, values
.vnode
->repeat
);
12475 check_data_variable (gfc_data_variable
*var
, locus
*where
)
12481 ar_type mark
= AR_UNKNOWN
;
12483 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
12489 if (gfc_resolve_expr (var
->expr
) == FAILURE
)
12493 mpz_init_set_si (offset
, 0);
12496 if (e
->expr_type
!= EXPR_VARIABLE
)
12497 gfc_internal_error ("check_data_variable(): Bad expression");
12499 sym
= e
->symtree
->n
.sym
;
12501 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
12503 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12504 sym
->name
, &sym
->declared_at
);
12507 if (e
->ref
== NULL
&& sym
->as
)
12509 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12510 " declaration", sym
->name
, where
);
12514 has_pointer
= sym
->attr
.pointer
;
12516 if (gfc_is_coindexed (e
))
12518 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym
->name
,
12523 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12525 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
12529 && ref
->type
== REF_ARRAY
12530 && ref
->u
.ar
.type
!= AR_FULL
)
12532 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12533 "be a full array", sym
->name
, where
);
12538 if (e
->rank
== 0 || has_pointer
)
12540 mpz_init_set_ui (size
, 1);
12547 /* Find the array section reference. */
12548 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
12550 if (ref
->type
!= REF_ARRAY
)
12552 if (ref
->u
.ar
.type
== AR_ELEMENT
)
12558 /* Set marks according to the reference pattern. */
12559 switch (ref
->u
.ar
.type
)
12567 /* Get the start position of array section. */
12568 gfc_get_section_index (ar
, section_index
, &offset
);
12573 gcc_unreachable ();
12576 if (gfc_array_size (e
, &size
) == FAILURE
)
12578 gfc_error ("Nonconstant array section at %L in DATA statement",
12580 mpz_clear (offset
);
12587 while (mpz_cmp_ui (size
, 0) > 0)
12589 if (next_data_value () == FAILURE
)
12591 gfc_error ("DATA statement at %L has more variables than values",
12597 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
12601 /* If we have more than one element left in the repeat count,
12602 and we have more than one element left in the target variable,
12603 then create a range assignment. */
12604 /* FIXME: Only done for full arrays for now, since array sections
12606 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
12607 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
12611 if (mpz_cmp (size
, values
.left
) >= 0)
12613 mpz_init_set (range
, values
.left
);
12614 mpz_sub (size
, size
, values
.left
);
12615 mpz_set_ui (values
.left
, 0);
12619 mpz_init_set (range
, size
);
12620 mpz_sub (values
.left
, values
.left
, size
);
12621 mpz_set_ui (size
, 0);
12624 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
12627 mpz_add (offset
, offset
, range
);
12634 /* Assign initial value to symbol. */
12637 mpz_sub_ui (values
.left
, values
.left
, 1);
12638 mpz_sub_ui (size
, size
, 1);
12640 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
12645 if (mark
== AR_FULL
)
12646 mpz_add_ui (offset
, offset
, 1);
12648 /* Modify the array section indexes and recalculate the offset
12649 for next element. */
12650 else if (mark
== AR_SECTION
)
12651 gfc_advance_section (section_index
, ar
, &offset
);
12655 if (mark
== AR_SECTION
)
12657 for (i
= 0; i
< ar
->dimen
; i
++)
12658 mpz_clear (section_index
[i
]);
12662 mpz_clear (offset
);
12668 static gfc_try
traverse_data_var (gfc_data_variable
*, locus
*);
12670 /* Iterate over a list of elements in a DATA statement. */
12673 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
12676 iterator_stack frame
;
12677 gfc_expr
*e
, *start
, *end
, *step
;
12678 gfc_try retval
= SUCCESS
;
12680 mpz_init (frame
.value
);
12683 start
= gfc_copy_expr (var
->iter
.start
);
12684 end
= gfc_copy_expr (var
->iter
.end
);
12685 step
= gfc_copy_expr (var
->iter
.step
);
12687 if (gfc_simplify_expr (start
, 1) == FAILURE
12688 || start
->expr_type
!= EXPR_CONSTANT
)
12690 gfc_error ("start of implied-do loop at %L could not be "
12691 "simplified to a constant value", &start
->where
);
12695 if (gfc_simplify_expr (end
, 1) == FAILURE
12696 || end
->expr_type
!= EXPR_CONSTANT
)
12698 gfc_error ("end of implied-do loop at %L could not be "
12699 "simplified to a constant value", &start
->where
);
12703 if (gfc_simplify_expr (step
, 1) == FAILURE
12704 || step
->expr_type
!= EXPR_CONSTANT
)
12706 gfc_error ("step of implied-do loop at %L could not be "
12707 "simplified to a constant value", &start
->where
);
12712 mpz_set (trip
, end
->value
.integer
);
12713 mpz_sub (trip
, trip
, start
->value
.integer
);
12714 mpz_add (trip
, trip
, step
->value
.integer
);
12716 mpz_div (trip
, trip
, step
->value
.integer
);
12718 mpz_set (frame
.value
, start
->value
.integer
);
12720 frame
.prev
= iter_stack
;
12721 frame
.variable
= var
->iter
.var
->symtree
;
12722 iter_stack
= &frame
;
12724 while (mpz_cmp_ui (trip
, 0) > 0)
12726 if (traverse_data_var (var
->list
, where
) == FAILURE
)
12732 e
= gfc_copy_expr (var
->expr
);
12733 if (gfc_simplify_expr (e
, 1) == FAILURE
)
12740 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
12742 mpz_sub_ui (trip
, trip
, 1);
12746 mpz_clear (frame
.value
);
12749 gfc_free_expr (start
);
12750 gfc_free_expr (end
);
12751 gfc_free_expr (step
);
12753 iter_stack
= frame
.prev
;
12758 /* Type resolve variables in the variable list of a DATA statement. */
12761 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
12765 for (; var
; var
= var
->next
)
12767 if (var
->expr
== NULL
)
12768 t
= traverse_data_list (var
, where
);
12770 t
= check_data_variable (var
, where
);
12780 /* Resolve the expressions and iterators associated with a data statement.
12781 This is separate from the assignment checking because data lists should
12782 only be resolved once. */
12785 resolve_data_variables (gfc_data_variable
*d
)
12787 for (; d
; d
= d
->next
)
12789 if (d
->list
== NULL
)
12791 if (gfc_resolve_expr (d
->expr
) == FAILURE
)
12796 if (gfc_resolve_iterator (&d
->iter
, false) == FAILURE
)
12799 if (resolve_data_variables (d
->list
) == FAILURE
)
12808 /* Resolve a single DATA statement. We implement this by storing a pointer to
12809 the value list into static variables, and then recursively traversing the
12810 variables list, expanding iterators and such. */
12813 resolve_data (gfc_data
*d
)
12816 if (resolve_data_variables (d
->var
) == FAILURE
)
12819 values
.vnode
= d
->value
;
12820 if (d
->value
== NULL
)
12821 mpz_set_ui (values
.left
, 0);
12823 mpz_set (values
.left
, d
->value
->repeat
);
12825 if (traverse_data_var (d
->var
, &d
->where
) == FAILURE
)
12828 /* At this point, we better not have any values left. */
12830 if (next_data_value () == SUCCESS
)
12831 gfc_error ("DATA statement at %L has more values than variables",
12836 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12837 accessed by host or use association, is a dummy argument to a pure function,
12838 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12839 is storage associated with any such variable, shall not be used in the
12840 following contexts: (clients of this function). */
12842 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12843 procedure. Returns zero if assignment is OK, nonzero if there is a
12846 gfc_impure_variable (gfc_symbol
*sym
)
12851 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
12854 /* Check if the symbol's ns is inside the pure procedure. */
12855 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12859 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
12863 proc
= sym
->ns
->proc_name
;
12864 if (sym
->attr
.dummy
&& gfc_pure (proc
)
12865 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
12867 proc
->attr
.function
))
12870 /* TODO: Sort out what can be storage associated, if anything, and include
12871 it here. In principle equivalences should be scanned but it does not
12872 seem to be possible to storage associate an impure variable this way. */
12877 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12878 current namespace is inside a pure procedure. */
12881 gfc_pure (gfc_symbol
*sym
)
12883 symbol_attribute attr
;
12888 /* Check if the current namespace or one of its parents
12889 belongs to a pure procedure. */
12890 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
12892 sym
= ns
->proc_name
;
12896 if (attr
.flavor
== FL_PROCEDURE
&& attr
.pure
)
12904 return attr
.flavor
== FL_PROCEDURE
&& attr
.pure
;
12908 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
12909 checks if the current namespace is implicitly pure. Note that this
12910 function returns false for a PURE procedure. */
12913 gfc_implicit_pure (gfc_symbol
*sym
)
12915 symbol_attribute attr
;
12919 /* Check if the current namespace is implicit_pure. */
12920 sym
= gfc_current_ns
->proc_name
;
12924 if (attr
.flavor
== FL_PROCEDURE
12925 && attr
.implicit_pure
&& !attr
.pure
)
12932 return attr
.flavor
== FL_PROCEDURE
&& attr
.implicit_pure
&& !attr
.pure
;
12936 /* Test whether the current procedure is elemental or not. */
12939 gfc_elemental (gfc_symbol
*sym
)
12941 symbol_attribute attr
;
12944 sym
= gfc_current_ns
->proc_name
;
12949 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
12953 /* Warn about unused labels. */
12956 warn_unused_fortran_label (gfc_st_label
*label
)
12961 warn_unused_fortran_label (label
->left
);
12963 if (label
->defined
== ST_LABEL_UNKNOWN
)
12966 switch (label
->referenced
)
12968 case ST_LABEL_UNKNOWN
:
12969 gfc_warning ("Label %d at %L defined but not used", label
->value
,
12973 case ST_LABEL_BAD_TARGET
:
12974 gfc_warning ("Label %d at %L defined but cannot be used",
12975 label
->value
, &label
->where
);
12982 warn_unused_fortran_label (label
->right
);
12986 /* Returns the sequence type of a symbol or sequence. */
12989 sequence_type (gfc_typespec ts
)
12998 if (ts
.u
.derived
->components
== NULL
)
12999 return SEQ_NONDEFAULT
;
13001 result
= sequence_type (ts
.u
.derived
->components
->ts
);
13002 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
13003 if (sequence_type (c
->ts
) != result
)
13009 if (ts
.kind
!= gfc_default_character_kind
)
13010 return SEQ_NONDEFAULT
;
13012 return SEQ_CHARACTER
;
13015 if (ts
.kind
!= gfc_default_integer_kind
)
13016 return SEQ_NONDEFAULT
;
13018 return SEQ_NUMERIC
;
13021 if (!(ts
.kind
== gfc_default_real_kind
13022 || ts
.kind
== gfc_default_double_kind
))
13023 return SEQ_NONDEFAULT
;
13025 return SEQ_NUMERIC
;
13028 if (ts
.kind
!= gfc_default_complex_kind
)
13029 return SEQ_NONDEFAULT
;
13031 return SEQ_NUMERIC
;
13034 if (ts
.kind
!= gfc_default_logical_kind
)
13035 return SEQ_NONDEFAULT
;
13037 return SEQ_NUMERIC
;
13040 return SEQ_NONDEFAULT
;
13045 /* Resolve derived type EQUIVALENCE object. */
13048 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
13050 gfc_component
*c
= derived
->components
;
13055 /* Shall not be an object of nonsequence derived type. */
13056 if (!derived
->attr
.sequence
)
13058 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13059 "attribute to be an EQUIVALENCE object", sym
->name
,
13064 /* Shall not have allocatable components. */
13065 if (derived
->attr
.alloc_comp
)
13067 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13068 "components to be an EQUIVALENCE object",sym
->name
,
13073 if (sym
->attr
.in_common
&& gfc_has_default_initializer (sym
->ts
.u
.derived
))
13075 gfc_error ("Derived type variable '%s' at %L with default "
13076 "initialization cannot be in EQUIVALENCE with a variable "
13077 "in COMMON", sym
->name
, &e
->where
);
13081 for (; c
; c
= c
->next
)
13083 if (c
->ts
.type
== BT_DERIVED
13084 && (resolve_equivalence_derived (c
->ts
.u
.derived
, sym
, e
) == FAILURE
))
13087 /* Shall not be an object of sequence derived type containing a pointer
13088 in the structure. */
13089 if (c
->attr
.pointer
)
13091 gfc_error ("Derived type variable '%s' at %L with pointer "
13092 "component(s) cannot be an EQUIVALENCE object",
13093 sym
->name
, &e
->where
);
13101 /* Resolve equivalence object.
13102 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13103 an allocatable array, an object of nonsequence derived type, an object of
13104 sequence derived type containing a pointer at any level of component
13105 selection, an automatic object, a function name, an entry name, a result
13106 name, a named constant, a structure component, or a subobject of any of
13107 the preceding objects. A substring shall not have length zero. A
13108 derived type shall not have components with default initialization nor
13109 shall two objects of an equivalence group be initialized.
13110 Either all or none of the objects shall have an protected attribute.
13111 The simple constraints are done in symbol.c(check_conflict) and the rest
13112 are implemented here. */
13115 resolve_equivalence (gfc_equiv
*eq
)
13118 gfc_symbol
*first_sym
;
13121 locus
*last_where
= NULL
;
13122 seq_type eq_type
, last_eq_type
;
13123 gfc_typespec
*last_ts
;
13124 int object
, cnt_protected
;
13127 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
13129 first_sym
= eq
->expr
->symtree
->n
.sym
;
13133 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
13137 e
->ts
= e
->symtree
->n
.sym
->ts
;
13138 /* match_varspec might not know yet if it is seeing
13139 array reference or substring reference, as it doesn't
13141 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
13143 gfc_ref
*ref
= e
->ref
;
13144 sym
= e
->symtree
->n
.sym
;
13146 if (sym
->attr
.dimension
)
13148 ref
->u
.ar
.as
= sym
->as
;
13152 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13153 if (e
->ts
.type
== BT_CHARACTER
13155 && ref
->type
== REF_ARRAY
13156 && ref
->u
.ar
.dimen
== 1
13157 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
13158 && ref
->u
.ar
.stride
[0] == NULL
)
13160 gfc_expr
*start
= ref
->u
.ar
.start
[0];
13161 gfc_expr
*end
= ref
->u
.ar
.end
[0];
13164 /* Optimize away the (:) reference. */
13165 if (start
== NULL
&& end
== NULL
)
13168 e
->ref
= ref
->next
;
13170 e
->ref
->next
= ref
->next
;
13175 ref
->type
= REF_SUBSTRING
;
13177 start
= gfc_get_int_expr (gfc_default_integer_kind
,
13179 ref
->u
.ss
.start
= start
;
13180 if (end
== NULL
&& e
->ts
.u
.cl
)
13181 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
13182 ref
->u
.ss
.end
= end
;
13183 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
13190 /* Any further ref is an error. */
13193 gcc_assert (ref
->type
== REF_ARRAY
);
13194 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13200 if (gfc_resolve_expr (e
) == FAILURE
)
13203 sym
= e
->symtree
->n
.sym
;
13205 if (sym
->attr
.is_protected
)
13207 if (cnt_protected
> 0 && cnt_protected
!= object
)
13209 gfc_error ("Either all or none of the objects in the "
13210 "EQUIVALENCE set at %L shall have the "
13211 "PROTECTED attribute",
13216 /* Shall not equivalence common block variables in a PURE procedure. */
13217 if (sym
->ns
->proc_name
13218 && sym
->ns
->proc_name
->attr
.pure
13219 && sym
->attr
.in_common
)
13221 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13222 "object in the pure procedure '%s'",
13223 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
13227 /* Shall not be a named constant. */
13228 if (e
->expr_type
== EXPR_CONSTANT
)
13230 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13231 "object", sym
->name
, &e
->where
);
13235 if (e
->ts
.type
== BT_DERIVED
13236 && resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
) == FAILURE
)
13239 /* Check that the types correspond correctly:
13241 A numeric sequence structure may be equivalenced to another sequence
13242 structure, an object of default integer type, default real type, double
13243 precision real type, default logical type such that components of the
13244 structure ultimately only become associated to objects of the same
13245 kind. A character sequence structure may be equivalenced to an object
13246 of default character kind or another character sequence structure.
13247 Other objects may be equivalenced only to objects of the same type and
13248 kind parameters. */
13250 /* Identical types are unconditionally OK. */
13251 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
13252 goto identical_types
;
13254 last_eq_type
= sequence_type (*last_ts
);
13255 eq_type
= sequence_type (sym
->ts
);
13257 /* Since the pair of objects is not of the same type, mixed or
13258 non-default sequences can be rejected. */
13260 msg
= "Sequence %s with mixed components in EQUIVALENCE "
13261 "statement at %L with different type objects";
13263 && last_eq_type
== SEQ_MIXED
13264 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
)
13266 || (eq_type
== SEQ_MIXED
13267 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13268 &e
->where
) == FAILURE
))
13271 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
13272 "statement at %L with objects of different type";
13274 && last_eq_type
== SEQ_NONDEFAULT
13275 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
,
13276 last_where
) == FAILURE
)
13277 || (eq_type
== SEQ_NONDEFAULT
13278 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13279 &e
->where
) == FAILURE
))
13282 msg
="Non-CHARACTER object '%s' in default CHARACTER "
13283 "EQUIVALENCE statement at %L";
13284 if (last_eq_type
== SEQ_CHARACTER
13285 && eq_type
!= SEQ_CHARACTER
13286 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13287 &e
->where
) == FAILURE
)
13290 msg
="Non-NUMERIC object '%s' in default NUMERIC "
13291 "EQUIVALENCE statement at %L";
13292 if (last_eq_type
== SEQ_NUMERIC
13293 && eq_type
!= SEQ_NUMERIC
13294 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
13295 &e
->where
) == FAILURE
)
13300 last_where
= &e
->where
;
13305 /* Shall not be an automatic array. */
13306 if (e
->ref
->type
== REF_ARRAY
13307 && gfc_resolve_array_spec (e
->ref
->u
.ar
.as
, 1) == FAILURE
)
13309 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13310 "an EQUIVALENCE object", sym
->name
, &e
->where
);
13317 /* Shall not be a structure component. */
13318 if (r
->type
== REF_COMPONENT
)
13320 gfc_error ("Structure component '%s' at %L cannot be an "
13321 "EQUIVALENCE object",
13322 r
->u
.c
.component
->name
, &e
->where
);
13326 /* A substring shall not have length zero. */
13327 if (r
->type
== REF_SUBSTRING
)
13329 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
13331 gfc_error ("Substring at %L has length zero",
13332 &r
->u
.ss
.start
->where
);
13342 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13345 resolve_fntype (gfc_namespace
*ns
)
13347 gfc_entry_list
*el
;
13350 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
13353 /* If there are any entries, ns->proc_name is the entry master
13354 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13356 sym
= ns
->entries
->sym
;
13358 sym
= ns
->proc_name
;
13359 if (sym
->result
== sym
13360 && sym
->ts
.type
== BT_UNKNOWN
13361 && gfc_set_default_type (sym
, 0, NULL
) == FAILURE
13362 && !sym
->attr
.untyped
)
13364 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13365 sym
->name
, &sym
->declared_at
);
13366 sym
->attr
.untyped
= 1;
13369 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
13370 && !sym
->attr
.contained
13371 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
13372 && gfc_check_symbol_access (sym
))
13374 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC function '%s' at "
13375 "%L of PRIVATE type '%s'", sym
->name
,
13376 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
13380 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
13382 if (el
->sym
->result
== el
->sym
13383 && el
->sym
->ts
.type
== BT_UNKNOWN
13384 && gfc_set_default_type (el
->sym
, 0, NULL
) == FAILURE
13385 && !el
->sym
->attr
.untyped
)
13387 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13388 el
->sym
->name
, &el
->sym
->declared_at
);
13389 el
->sym
->attr
.untyped
= 1;
13395 /* 12.3.2.1.1 Defined operators. */
13398 check_uop_procedure (gfc_symbol
*sym
, locus where
)
13400 gfc_formal_arglist
*formal
;
13402 if (!sym
->attr
.function
)
13404 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13405 sym
->name
, &where
);
13409 if (sym
->ts
.type
== BT_CHARACTER
13410 && !(sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
)
13411 && !(sym
->result
&& sym
->result
->ts
.u
.cl
13412 && sym
->result
->ts
.u
.cl
->length
))
13414 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13415 "character length", sym
->name
, &where
);
13419 formal
= sym
->formal
;
13420 if (!formal
|| !formal
->sym
)
13422 gfc_error ("User operator procedure '%s' at %L must have at least "
13423 "one argument", sym
->name
, &where
);
13427 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13429 gfc_error ("First argument of operator interface at %L must be "
13430 "INTENT(IN)", &where
);
13434 if (formal
->sym
->attr
.optional
)
13436 gfc_error ("First argument of operator interface at %L cannot be "
13437 "optional", &where
);
13441 formal
= formal
->next
;
13442 if (!formal
|| !formal
->sym
)
13445 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
13447 gfc_error ("Second argument of operator interface at %L must be "
13448 "INTENT(IN)", &where
);
13452 if (formal
->sym
->attr
.optional
)
13454 gfc_error ("Second argument of operator interface at %L cannot be "
13455 "optional", &where
);
13461 gfc_error ("Operator interface at %L must have, at most, two "
13462 "arguments", &where
);
13470 gfc_resolve_uops (gfc_symtree
*symtree
)
13472 gfc_interface
*itr
;
13474 if (symtree
== NULL
)
13477 gfc_resolve_uops (symtree
->left
);
13478 gfc_resolve_uops (symtree
->right
);
13480 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
13481 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
13485 /* Examine all of the expressions associated with a program unit,
13486 assign types to all intermediate expressions, make sure that all
13487 assignments are to compatible types and figure out which names
13488 refer to which functions or subroutines. It doesn't check code
13489 block, which is handled by resolve_code. */
13492 resolve_types (gfc_namespace
*ns
)
13498 gfc_namespace
* old_ns
= gfc_current_ns
;
13500 /* Check that all IMPLICIT types are ok. */
13501 if (!ns
->seen_implicit_none
)
13504 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
13505 if (ns
->set_flag
[letter
]
13506 && resolve_typespec_used (&ns
->default_type
[letter
],
13507 &ns
->implicit_loc
[letter
],
13512 gfc_current_ns
= ns
;
13514 resolve_entries (ns
);
13516 resolve_common_vars (ns
->blank_common
.head
, false);
13517 resolve_common_blocks (ns
->common_root
);
13519 resolve_contained_functions (ns
);
13521 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
13522 && ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
13523 resolve_formal_arglist (ns
->proc_name
);
13525 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
13527 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
13528 resolve_charlen (cl
);
13530 gfc_traverse_ns (ns
, resolve_symbol
);
13532 resolve_fntype (ns
);
13534 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13536 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
13537 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13538 "also be PURE", n
->proc_name
->name
,
13539 &n
->proc_name
->declared_at
);
13545 gfc_check_interfaces (ns
);
13547 gfc_traverse_ns (ns
, resolve_values
);
13553 for (d
= ns
->data
; d
; d
= d
->next
)
13557 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
13559 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
13561 if (ns
->common_root
!= NULL
)
13562 gfc_traverse_symtree (ns
->common_root
, resolve_bind_c_comms
);
13564 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
13565 resolve_equivalence (eq
);
13567 /* Warn about unused labels. */
13568 if (warn_unused_label
)
13569 warn_unused_fortran_label (ns
->st_labels
);
13571 gfc_resolve_uops (ns
->uop_root
);
13573 gfc_current_ns
= old_ns
;
13577 /* Call resolve_code recursively. */
13580 resolve_codes (gfc_namespace
*ns
)
13583 bitmap_obstack old_obstack
;
13585 if (ns
->resolved
== 1)
13588 for (n
= ns
->contained
; n
; n
= n
->sibling
)
13591 gfc_current_ns
= ns
;
13593 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13594 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
13597 /* Set to an out of range value. */
13598 current_entry_id
= -1;
13600 old_obstack
= labels_obstack
;
13601 bitmap_obstack_initialize (&labels_obstack
);
13603 resolve_code (ns
->code
, ns
);
13605 bitmap_obstack_release (&labels_obstack
);
13606 labels_obstack
= old_obstack
;
13610 /* This function is called after a complete program unit has been compiled.
13611 Its purpose is to examine all of the expressions associated with a program
13612 unit, assign types to all intermediate expressions, make sure that all
13613 assignments are to compatible types and figure out which names refer to
13614 which functions or subroutines. */
13617 gfc_resolve (gfc_namespace
*ns
)
13619 gfc_namespace
*old_ns
;
13620 code_stack
*old_cs_base
;
13626 old_ns
= gfc_current_ns
;
13627 old_cs_base
= cs_base
;
13629 resolve_types (ns
);
13630 resolve_codes (ns
);
13632 gfc_current_ns
= old_ns
;
13633 cs_base
= old_cs_base
;
13636 gfc_run_passes (ns
);