1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
33 /* Types used in equivalence statements. */
37 SEQ_NONDEFAULT
, SEQ_NUMERIC
, SEQ_CHARACTER
, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code
*head
, *current
;
46 struct code_stack
*prev
;
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
51 bitmap reachable_labels
;
55 static code_stack
*cs_base
= NULL
;
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
60 static int forall_flag
;
61 int gfc_do_concurrent_flag
;
63 /* True when we are resolving an expression that is an actual argument to
65 static bool actual_arg
= false;
66 /* True when we are resolving an expression that is the first actual argument
68 static bool first_actual_arg
= false;
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
73 static int omp_workshare_flag
;
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag
= false;
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr
= false;
82 /* The id of the last entry seen. */
83 static int current_entry_id
;
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack
;
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument
= false;
93 gfc_is_formal_arg (void)
95 return formal_arg_flag
;
98 /* Is the symbol host associated? */
100 is_sym_host_assoc (gfc_symbol
*sym
, gfc_namespace
*ns
)
102 for (ns
= ns
->parent
; ns
; ns
= ns
->parent
)
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
116 resolve_typespec_used (gfc_typespec
* ts
, locus
* where
, const char* name
)
118 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
->attr
.abstract
)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name
, where
, ts
->u
.derived
->name
);
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts
->u
.derived
->name
, where
);
138 check_proc_interface (gfc_symbol
*ifc
, locus
*where
)
140 /* Several checks for F08:C1216. */
141 if (ifc
->attr
.procedure
)
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc
->name
, where
);
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface
*gen
= ifc
->generic
;
152 while (gen
&& strcmp (gen
->sym
->name
, ifc
->name
) != 0)
156 gfc_error ("Interface %qs at %L may not be generic",
161 if (ifc
->attr
.proc
== PROC_ST_FUNCTION
)
163 gfc_error ("Interface %qs at %L may not be a statement function",
167 if (gfc_is_intrinsic (ifc
, 0, ifc
->declared_at
)
168 || gfc_is_intrinsic (ifc
, 1, ifc
->declared_at
))
169 ifc
->attr
.intrinsic
= 1;
170 if (ifc
->attr
.intrinsic
&& !gfc_intrinsic_actual_ok (ifc
->name
, 0))
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc
->name
, where
);
176 if (!ifc
->attr
.if_source
&& !ifc
->attr
.intrinsic
&& ifc
->name
[0] != '\0')
178 gfc_error ("Interface %qs at %L must be explicit", ifc
->name
, where
);
185 static void resolve_symbol (gfc_symbol
*sym
);
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
191 resolve_procedure_interface (gfc_symbol
*sym
)
193 gfc_symbol
*ifc
= sym
->ts
.interface
;
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym
->name
, &sym
->declared_at
);
204 if (!check_proc_interface (ifc
, &sym
->declared_at
))
207 if (ifc
->attr
.if_source
|| ifc
->attr
.intrinsic
)
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc
);
211 if (ifc
->attr
.intrinsic
)
212 gfc_resolve_intrinsic (ifc
, &ifc
->declared_at
);
216 sym
->ts
= ifc
->result
->ts
;
217 sym
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
218 sym
->attr
.pointer
= ifc
->result
->attr
.pointer
;
219 sym
->attr
.dimension
= ifc
->result
->attr
.dimension
;
220 sym
->attr
.class_ok
= ifc
->result
->attr
.class_ok
;
221 sym
->as
= gfc_copy_array_spec (ifc
->result
->as
);
227 sym
->attr
.allocatable
= ifc
->attr
.allocatable
;
228 sym
->attr
.pointer
= ifc
->attr
.pointer
;
229 sym
->attr
.dimension
= ifc
->attr
.dimension
;
230 sym
->attr
.class_ok
= ifc
->attr
.class_ok
;
231 sym
->as
= gfc_copy_array_spec (ifc
->as
);
233 sym
->ts
.interface
= ifc
;
234 sym
->attr
.function
= ifc
->attr
.function
;
235 sym
->attr
.subroutine
= ifc
->attr
.subroutine
;
237 sym
->attr
.pure
= ifc
->attr
.pure
;
238 sym
->attr
.elemental
= ifc
->attr
.elemental
;
239 sym
->attr
.contiguous
= ifc
->attr
.contiguous
;
240 sym
->attr
.recursive
= ifc
->attr
.recursive
;
241 sym
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
242 sym
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
243 sym
->attr
.is_bind_c
= ifc
->attr
.is_bind_c
;
244 /* Copy char length. */
245 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
247 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
248 if (sym
->ts
.u
.cl
->length
&& !sym
->ts
.u
.cl
->resolved
249 && !gfc_resolve_expr (sym
->ts
.u
.cl
->length
))
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
268 gfc_resolve_formal_arglist (gfc_symbol
*proc
)
270 gfc_formal_arglist
*f
;
272 bool saved_specification_expr
;
275 if (proc
->result
!= NULL
)
280 if (gfc_elemental (proc
)
281 || sym
->attr
.pointer
|| sym
->attr
.allocatable
282 || (sym
->as
&& sym
->as
->rank
!= 0))
284 proc
->attr
.always_explicit
= 1;
285 sym
->attr
.always_explicit
= 1;
288 formal_arg_flag
= true;
290 for (f
= proc
->formal
; f
; f
= f
->next
)
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc
))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc
->name
,
303 if (proc
->attr
.function
)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc
->name
,
309 else if (sym
->attr
.procedure
&& sym
->attr
.if_source
!= IFSRC_DECL
310 && !resolve_procedure_interface (sym
))
313 if (strcmp (proc
->name
, sym
->name
) == 0)
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym
->name
,
321 if (sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
322 gfc_resolve_formal_arglist (sym
);
324 if (sym
->attr
.subroutine
|| sym
->attr
.external
)
326 if (sym
->attr
.flavor
== FL_UNKNOWN
)
327 gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, &sym
->declared_at
);
331 if (sym
->ts
.type
== BT_UNKNOWN
&& !proc
->attr
.intrinsic
332 && (!sym
->attr
.function
|| sym
->result
== sym
))
333 gfc_set_default_type (sym
, 1, sym
->ns
);
336 as
= sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
337 ? CLASS_DATA (sym
)->as
: sym
->as
;
339 saved_specification_expr
= specification_expr
;
340 specification_expr
= true;
341 gfc_resolve_array_spec (as
, 0);
342 specification_expr
= saved_specification_expr
;
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
347 if (as
&& as
->rank
> 0 && as
->type
== AS_DEFERRED
348 && ((sym
->ts
.type
!= BT_CLASS
349 && !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
350 || (sym
->ts
.type
== BT_CLASS
351 && !(CLASS_DATA (sym
)->attr
.class_pointer
352 || CLASS_DATA (sym
)->attr
.allocatable
)))
353 && sym
->attr
.flavor
!= FL_PROCEDURE
)
355 as
->type
= AS_ASSUMED_SHAPE
;
356 for (i
= 0; i
< as
->rank
; i
++)
357 as
->lower
[i
] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
360 if ((as
&& as
->rank
> 0 && as
->type
== AS_ASSUMED_SHAPE
)
361 || (as
&& as
->type
== AS_ASSUMED_RANK
)
362 || sym
->attr
.pointer
|| sym
->attr
.allocatable
|| sym
->attr
.target
363 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
364 && (CLASS_DATA (sym
)->attr
.class_pointer
365 || CLASS_DATA (sym
)->attr
.allocatable
366 || CLASS_DATA (sym
)->attr
.target
))
367 || sym
->attr
.optional
)
369 proc
->attr
.always_explicit
= 1;
371 proc
->result
->attr
.always_explicit
= 1;
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
377 if (sym
->attr
.flavor
== FL_UNKNOWN
)
378 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, sym
->name
, &sym
->declared_at
);
382 if (sym
->attr
.flavor
== FL_PROCEDURE
)
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym
->name
, &sym
->declared_at
);
392 else if (!sym
->attr
.pointer
)
394 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
)
397 gfc_notify_std (GFC_STD_F2008
, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym
->name
, proc
->name
, &sym
->declared_at
);
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym
->name
, proc
->name
,
407 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
)
410 gfc_notify_std (GFC_STD_F2008
, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym
->name
,
413 proc
->name
, &sym
->declared_at
);
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym
->name
, proc
->name
,
423 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.intent
== INTENT_OUT
)
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym
->name
, proc
->name
,
432 if (proc
->attr
.implicit_pure
)
434 if (sym
->attr
.flavor
== FL_PROCEDURE
)
437 proc
->attr
.implicit_pure
= 0;
439 else if (!sym
->attr
.pointer
)
441 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
443 proc
->attr
.implicit_pure
= 0;
445 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
447 proc
->attr
.implicit_pure
= 0;
451 if (gfc_elemental (proc
))
454 if (sym
->attr
.codimension
455 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
456 && CLASS_DATA (sym
)->attr
.codimension
))
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym
->name
, &sym
->declared_at
);
463 if (sym
->as
|| (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
464 && CLASS_DATA (sym
)->as
))
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym
->name
, &sym
->declared_at
);
471 if (sym
->attr
.allocatable
472 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
473 && CLASS_DATA (sym
)->attr
.allocatable
))
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym
->name
,
481 if (sym
->attr
.pointer
482 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
483 && CLASS_DATA (sym
)->attr
.class_pointer
))
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym
->name
,
491 if (sym
->attr
.flavor
== FL_PROCEDURE
)
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym
->name
, proc
->name
,
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym
->attr
.intent
== INTENT_UNKNOWN
&& !sym
->attr
.value
)
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym
->name
, proc
->name
,
510 /* Each dummy shall be specified to be scalar. */
511 if (proc
->attr
.proc
== PROC_ST_FUNCTION
)
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym
->name
, proc
->name
,
523 if (sym
->ts
.type
== BT_CHARACTER
)
525 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
526 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym
->name
, &sym
->declared_at
);
536 formal_arg_flag
= false;
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
544 find_arglists (gfc_symbol
*sym
)
546 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
|| sym
->ns
!= gfc_current_ns
547 || gfc_fl_struct (sym
->attr
.flavor
) || sym
->attr
.intrinsic
)
550 gfc_resolve_formal_arglist (sym
);
554 /* Given a namespace, resolve all formal argument lists within the namespace.
558 resolve_formal_arglists (gfc_namespace
*ns
)
563 gfc_traverse_ns (ns
, find_arglists
);
568 resolve_contained_fntype (gfc_symbol
*sym
, gfc_namespace
*ns
)
572 if (sym
&& sym
->attr
.flavor
== FL_PROCEDURE
574 && sym
->ns
->parent
->proc_name
575 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_PROCEDURE
576 && !strcmp (sym
->name
, sym
->ns
->parent
->proc_name
->name
))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym
->name
, &sym
->declared_at
);
580 /* If this namespace is not a function or an entry master function,
582 if (! sym
|| !(sym
->attr
.function
|| sym
->attr
.flavor
== FL_VARIABLE
)
583 || sym
->attr
.entry_master
)
589 /* Try to find out of what the return type is. */
590 if (sym
->result
->ts
.type
== BT_UNKNOWN
&& sym
->result
->ts
.interface
== NULL
)
592 t
= gfc_set_default_type (sym
->result
, 0, ns
);
594 if (!t
&& !sym
->result
->attr
.untyped
)
596 if (sym
->result
== sym
)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym
->name
, &sym
->declared_at
);
599 else if (!sym
->result
->attr
.proc_pointer
)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym
->result
->name
, sym
->name
,
602 &sym
->result
->declared_at
);
603 sym
->result
->attr
.untyped
= 1;
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
614 if (sym
->result
->ts
.type
== BT_CHARACTER
)
616 gfc_charlen
*cl
= sym
->result
->ts
.u
.cl
;
617 if ((!cl
|| !cl
->length
) && !sym
->result
->ts
.deferred
)
619 /* See if this is a module-procedure and adapt error message
622 gcc_assert (ns
->parent
&& ns
->parent
->proc_name
);
623 module_proc
= (ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
);
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym
->name
, &sym
->declared_at
);
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
640 merge_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
642 gfc_formal_arglist
*f
, *new_arglist
;
645 for (; new_args
!= NULL
; new_args
= new_args
->next
)
647 new_sym
= new_args
->sym
;
648 /* See if this arg is already in the formal argument list. */
649 for (f
= proc
->formal
; f
; f
= f
->next
)
651 if (new_sym
== f
->sym
)
658 /* Add a new argument. Argument order is not important. */
659 new_arglist
= gfc_get_formal_arglist ();
660 new_arglist
->sym
= new_sym
;
661 new_arglist
->next
= proc
->formal
;
662 proc
->formal
= new_arglist
;
667 /* Flag the arguments that are not present in all entries. */
670 check_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
672 gfc_formal_arglist
*f
, *head
;
675 for (f
= proc
->formal
; f
; f
= f
->next
)
680 for (new_args
= head
; new_args
; new_args
= new_args
->next
)
682 if (new_args
->sym
== f
->sym
)
689 f
->sym
->attr
.not_always_present
= 1;
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
699 resolve_entries (gfc_namespace
*ns
)
701 gfc_namespace
*old_ns
;
705 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
706 static int master_count
= 0;
708 if (ns
->proc_name
== NULL
)
711 /* No need to do anything if this procedure doesn't have alternate entry
716 /* We may already have resolved alternate entry points. */
717 if (ns
->proc_name
->attr
.entry_master
)
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
);
723 /* Remember the current namespace. */
724 old_ns
= gfc_current_ns
;
728 /* Add the main entry point to the list of entry points. */
729 el
= gfc_get_entry_list ();
730 el
->sym
= ns
->proc_name
;
732 el
->next
= ns
->entries
;
734 ns
->proc_name
->attr
.entry
= 1;
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns
->proc_name
->attr
.function
742 && ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el
= el
->next
; el
; el
= el
->next
)
749 if (el
->sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
750 && el
->sym
->attr
.mod_proc
)
754 /* Add an entry statement for it. */
755 c
= gfc_get_code (EXEC_ENTRY
);
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "master.%d.%s",
765 master_count
++, ns
->proc_name
->name
);
766 gfc_get_ha_symbol (name
, &proc
);
767 gcc_assert (proc
!= NULL
);
769 gfc_add_procedure (&proc
->attr
, PROC_INTERNAL
, proc
->name
, NULL
);
770 if (ns
->proc_name
->attr
.subroutine
)
771 gfc_add_subroutine (&proc
->attr
, proc
->name
, NULL
);
775 gfc_typespec
*ts
, *fts
;
776 gfc_array_spec
*as
, *fas
;
777 gfc_add_function (&proc
->attr
, proc
->name
, NULL
);
779 fas
= ns
->entries
->sym
->as
;
780 fas
= fas
? fas
: ns
->entries
->sym
->result
->as
;
781 fts
= &ns
->entries
->sym
->result
->ts
;
782 if (fts
->type
== BT_UNKNOWN
)
783 fts
= gfc_get_default_type (ns
->entries
->sym
->result
->name
, NULL
);
784 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
786 ts
= &el
->sym
->result
->ts
;
788 as
= as
? as
: el
->sym
->result
->as
;
789 if (ts
->type
== BT_UNKNOWN
)
790 ts
= gfc_get_default_type (el
->sym
->result
->name
, NULL
);
792 if (! gfc_compare_types (ts
, fts
)
793 || (el
->sym
->result
->attr
.dimension
794 != ns
->entries
->sym
->result
->attr
.dimension
)
795 || (el
->sym
->result
->attr
.pointer
796 != ns
->entries
->sym
->result
->attr
.pointer
))
798 else if (as
&& fas
&& ns
->entries
->sym
->result
!= el
->sym
->result
799 && gfc_compare_array_spec (as
, fas
) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns
->entries
->sym
->name
,
802 &ns
->entries
->sym
->declared_at
);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& fts
->u
.cl
808 && (((ts
->u
.cl
->length
&& !fts
->u
.cl
->length
)
809 ||(!ts
->u
.cl
->length
&& fts
->u
.cl
->length
))
811 && ts
->u
.cl
->length
->expr_type
812 != fts
->u
.cl
->length
->expr_type
)
814 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
815 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
816 fts
->u
.cl
->length
->value
.integer
) != 0)))
817 gfc_notify_std (GFC_STD_GNU
, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns
->entries
->sym
->name
,
820 &ns
->entries
->sym
->declared_at
);
825 sym
= ns
->entries
->sym
->result
;
826 /* All result types the same. */
828 if (sym
->attr
.dimension
)
829 gfc_set_array_spec (proc
, gfc_copy_array_spec (sym
->as
), NULL
);
830 if (sym
->attr
.pointer
)
831 gfc_add_pointer (&proc
->attr
, NULL
);
835 /* Otherwise the result will be passed through a union by
837 proc
->attr
.mixed_entry_master
= 1;
838 for (el
= ns
->entries
; el
; el
= el
->next
)
840 sym
= el
->sym
->result
;
841 if (sym
->attr
.dimension
)
843 if (el
== ns
->entries
)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym
->name
,
846 ns
->entries
->sym
->name
, &sym
->declared_at
);
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym
->name
,
850 ns
->entries
->sym
->name
, &sym
->declared_at
);
852 else if (sym
->attr
.pointer
)
854 if (el
== ns
->entries
)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym
->name
,
857 ns
->entries
->sym
->name
, &sym
->declared_at
);
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym
->name
,
861 ns
->entries
->sym
->name
, &sym
->declared_at
);
866 if (ts
->type
== BT_UNKNOWN
)
867 ts
= gfc_get_default_type (sym
->name
, NULL
);
871 if (ts
->kind
== gfc_default_integer_kind
)
875 if (ts
->kind
== gfc_default_real_kind
876 || ts
->kind
== gfc_default_double_kind
)
880 if (ts
->kind
== gfc_default_complex_kind
)
884 if (ts
->kind
== gfc_default_logical_kind
)
888 /* We will issue error elsewhere. */
896 if (el
== ns
->entries
)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym
->name
,
899 gfc_typename (ts
), ns
->entries
->sym
->name
,
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym
->name
,
904 gfc_typename (ts
), ns
->entries
->sym
->name
,
911 proc
->attr
.access
= ACCESS_PRIVATE
;
912 proc
->attr
.entry_master
= 1;
914 /* Merge all the entry point arguments. */
915 for (el
= ns
->entries
; el
; el
= el
->next
)
916 merge_argument_lists (proc
, el
->sym
->formal
);
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el
= ns
->entries
; el
; el
= el
->next
)
921 check_argument_lists (proc
, el
->sym
->formal
);
923 /* Use the master function for the function body. */
924 ns
->proc_name
= proc
;
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
929 /* Restore the original namespace. */
930 gfc_current_ns
= old_ns
;
934 /* Resolve common variables. */
936 resolve_common_vars (gfc_common_head
*common_block
, bool named_common
)
938 gfc_symbol
*csym
= common_block
->head
;
940 for (; csym
; csym
= csym
->common_next
)
942 /* gfc_add_in_common may have been called before, but the reported errors
943 have been ignored to continue parsing.
944 We do the checks again here. */
945 if (!csym
->attr
.use_assoc
)
947 gfc_add_in_common (&csym
->attr
, csym
->name
, &common_block
->where
);
948 gfc_notify_std (GFC_STD_F2018_OBS
, "COMMON block at %L",
949 &common_block
->where
);
952 if (csym
->value
|| csym
->attr
.data
)
954 if (!csym
->ns
->is_block_data
)
955 gfc_notify_std (GFC_STD_GNU
, "Variable %qs at %L is in COMMON "
956 "but only in BLOCK DATA initialization is "
957 "allowed", csym
->name
, &csym
->declared_at
);
958 else if (!named_common
)
959 gfc_notify_std (GFC_STD_GNU
, "Initialized variable %qs at %L is "
960 "in a blank COMMON but initialization is only "
961 "allowed in named common blocks", csym
->name
,
965 if (UNLIMITED_POLY (csym
))
966 gfc_error_now ("%qs in cannot appear in COMMON at %L "
967 "[F2008:C5100]", csym
->name
, &csym
->declared_at
);
969 if (csym
->ts
.type
!= BT_DERIVED
)
972 if (!(csym
->ts
.u
.derived
->attr
.sequence
973 || csym
->ts
.u
.derived
->attr
.is_bind_c
))
974 gfc_error_now ("Derived type variable %qs in COMMON at %L "
975 "has neither the SEQUENCE nor the BIND(C) "
976 "attribute", csym
->name
, &csym
->declared_at
);
977 if (csym
->ts
.u
.derived
->attr
.alloc_comp
)
978 gfc_error_now ("Derived type variable %qs in COMMON at %L "
979 "has an ultimate component that is "
980 "allocatable", csym
->name
, &csym
->declared_at
);
981 if (gfc_has_default_initializer (csym
->ts
.u
.derived
))
982 gfc_error_now ("Derived type variable %qs in COMMON at %L "
983 "may not have default initializer", csym
->name
,
986 if (csym
->attr
.flavor
== FL_UNKNOWN
&& !csym
->attr
.proc_pointer
)
987 gfc_add_flavor (&csym
->attr
, FL_VARIABLE
, csym
->name
, &csym
->declared_at
);
991 /* Resolve common blocks. */
993 resolve_common_blocks (gfc_symtree
*common_root
)
998 if (common_root
== NULL
)
1001 if (common_root
->left
)
1002 resolve_common_blocks (common_root
->left
);
1003 if (common_root
->right
)
1004 resolve_common_blocks (common_root
->right
);
1006 resolve_common_vars (common_root
->n
.common
, true);
1008 /* The common name is a global name - in Fortran 2003 also if it has a
1009 C binding name, since Fortran 2008 only the C binding name is a global
1011 if (!common_root
->n
.common
->binding_label
1012 || gfc_notification_std (GFC_STD_F2008
))
1014 gsym
= gfc_find_gsymbol (gfc_gsym_root
,
1015 common_root
->n
.common
->name
);
1017 if (gsym
&& gfc_notification_std (GFC_STD_F2008
)
1018 && gsym
->type
== GSYM_COMMON
1019 && ((common_root
->n
.common
->binding_label
1020 && (!gsym
->binding_label
1021 || strcmp (common_root
->n
.common
->binding_label
,
1022 gsym
->binding_label
) != 0))
1023 || (!common_root
->n
.common
->binding_label
1024 && gsym
->binding_label
)))
1026 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1027 "identifier and must thus have the same binding name "
1028 "as the same-named COMMON block at %L: %s vs %s",
1029 common_root
->n
.common
->name
, &common_root
->n
.common
->where
,
1031 common_root
->n
.common
->binding_label
1032 ? common_root
->n
.common
->binding_label
: "(blank)",
1033 gsym
->binding_label
? gsym
->binding_label
: "(blank)");
1037 if (gsym
&& gsym
->type
!= GSYM_COMMON
1038 && !common_root
->n
.common
->binding_label
)
1040 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1042 common_root
->n
.common
->name
, &common_root
->n
.common
->where
,
1046 if (gsym
&& gsym
->type
!= GSYM_COMMON
)
1048 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1049 "%L sharing the identifier with global non-COMMON-block "
1050 "entity at %L", common_root
->n
.common
->name
,
1051 &common_root
->n
.common
->where
, &gsym
->where
);
1056 gsym
= gfc_get_gsymbol (common_root
->n
.common
->name
, false);
1057 gsym
->type
= GSYM_COMMON
;
1058 gsym
->where
= common_root
->n
.common
->where
;
1064 if (common_root
->n
.common
->binding_label
)
1066 gsym
= gfc_find_gsymbol (gfc_gsym_root
,
1067 common_root
->n
.common
->binding_label
);
1068 if (gsym
&& gsym
->type
!= GSYM_COMMON
)
1070 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1071 "global identifier as entity at %L",
1072 &common_root
->n
.common
->where
,
1073 common_root
->n
.common
->binding_label
, &gsym
->where
);
1078 gsym
= gfc_get_gsymbol (common_root
->n
.common
->binding_label
, true);
1079 gsym
->type
= GSYM_COMMON
;
1080 gsym
->where
= common_root
->n
.common
->where
;
1086 gfc_find_symbol (common_root
->name
, gfc_current_ns
, 0, &sym
);
1090 if (sym
->attr
.flavor
== FL_PARAMETER
)
1091 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1092 sym
->name
, &common_root
->n
.common
->where
, &sym
->declared_at
);
1094 if (sym
->attr
.external
)
1095 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1096 sym
->name
, &common_root
->n
.common
->where
);
1098 if (sym
->attr
.intrinsic
)
1099 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1100 sym
->name
, &common_root
->n
.common
->where
);
1101 else if (sym
->attr
.result
1102 || gfc_is_function_return_value (sym
, gfc_current_ns
))
1103 gfc_notify_std (GFC_STD_F2003
, "COMMON block %qs at %L "
1104 "that is also a function result", sym
->name
,
1105 &common_root
->n
.common
->where
);
1106 else if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_INTERNAL
1107 && sym
->attr
.proc
!= PROC_ST_FUNCTION
)
1108 gfc_notify_std (GFC_STD_F2003
, "COMMON block %qs at %L "
1109 "that is also a global procedure", sym
->name
,
1110 &common_root
->n
.common
->where
);
1114 /* Resolve contained function types. Because contained functions can call one
1115 another, they have to be worked out before any of the contained procedures
1118 The good news is that if a function doesn't already have a type, the only
1119 way it can get one is through an IMPLICIT type or a RESULT variable, because
1120 by definition contained functions are contained namespace they're contained
1121 in, not in a sibling or parent namespace. */
1124 resolve_contained_functions (gfc_namespace
*ns
)
1126 gfc_namespace
*child
;
1129 resolve_formal_arglists (ns
);
1131 for (child
= ns
->contained
; child
; child
= child
->sibling
)
1133 /* Resolve alternate entry points first. */
1134 resolve_entries (child
);
1136 /* Then check function return types. */
1137 resolve_contained_fntype (child
->proc_name
, child
);
1138 for (el
= child
->entries
; el
; el
= el
->next
)
1139 resolve_contained_fntype (el
->sym
, child
);
1145 /* A Parameterized Derived Type constructor must contain values for
1146 the PDT KIND parameters or they must have a default initializer.
1147 Go through the constructor picking out the KIND expressions,
1148 storing them in 'param_list' and then call gfc_get_pdt_instance
1149 to obtain the PDT instance. */
1151 static gfc_actual_arglist
*param_list
, *param_tail
, *param
;
1154 get_pdt_spec_expr (gfc_component
*c
, gfc_expr
*expr
)
1156 param
= gfc_get_actual_arglist ();
1158 param_list
= param_tail
= param
;
1161 param_tail
->next
= param
;
1162 param_tail
= param_tail
->next
;
1165 param_tail
->name
= c
->name
;
1167 param_tail
->expr
= gfc_copy_expr (expr
);
1168 else if (c
->initializer
)
1169 param_tail
->expr
= gfc_copy_expr (c
->initializer
);
1172 param_tail
->spec_type
= SPEC_ASSUMED
;
1173 if (c
->attr
.pdt_kind
)
1175 gfc_error ("The KIND parameter %qs in the PDT constructor "
1176 "at %C has no value", param
->name
);
1185 get_pdt_constructor (gfc_expr
*expr
, gfc_constructor
**constr
,
1186 gfc_symbol
*derived
)
1188 gfc_constructor
*cons
= NULL
;
1189 gfc_component
*comp
;
1192 if (expr
&& expr
->expr_type
== EXPR_STRUCTURE
)
1193 cons
= gfc_constructor_first (expr
->value
.constructor
);
1198 comp
= derived
->components
;
1200 for (; comp
&& cons
; comp
= comp
->next
, cons
= gfc_constructor_next (cons
))
1203 && cons
->expr
->expr_type
== EXPR_STRUCTURE
1204 && comp
->ts
.type
== BT_DERIVED
)
1206 t
= get_pdt_constructor (cons
->expr
, NULL
, comp
->ts
.u
.derived
);
1210 else if (comp
->ts
.type
== BT_DERIVED
)
1212 t
= get_pdt_constructor (NULL
, &cons
, comp
->ts
.u
.derived
);
1216 else if ((comp
->attr
.pdt_kind
|| comp
->attr
.pdt_len
)
1217 && derived
->attr
.pdt_template
)
1219 t
= get_pdt_spec_expr (comp
, cons
->expr
);
1228 static bool resolve_fl_derived0 (gfc_symbol
*sym
);
1229 static bool resolve_fl_struct (gfc_symbol
*sym
);
1232 /* Resolve all of the elements of a structure constructor and make sure that
1233 the types are correct. The 'init' flag indicates that the given
1234 constructor is an initializer. */
1237 resolve_structure_cons (gfc_expr
*expr
, int init
)
1239 gfc_constructor
*cons
;
1240 gfc_component
*comp
;
1246 if (expr
->ts
.type
== BT_DERIVED
|| expr
->ts
.type
== BT_UNION
)
1248 if (expr
->ts
.u
.derived
->attr
.flavor
== FL_DERIVED
)
1249 resolve_fl_derived0 (expr
->ts
.u
.derived
);
1251 resolve_fl_struct (expr
->ts
.u
.derived
);
1253 /* If this is a Parameterized Derived Type template, find the
1254 instance corresponding to the PDT kind parameters. */
1255 if (expr
->ts
.u
.derived
->attr
.pdt_template
)
1258 t
= get_pdt_constructor (expr
, NULL
, expr
->ts
.u
.derived
);
1261 gfc_get_pdt_instance (param_list
, &expr
->ts
.u
.derived
, NULL
);
1263 expr
->param_list
= gfc_copy_actual_arglist (param_list
);
1266 gfc_free_actual_arglist (param_list
);
1268 if (!expr
->ts
.u
.derived
->attr
.pdt_type
)
1273 cons
= gfc_constructor_first (expr
->value
.constructor
);
1275 /* A constructor may have references if it is the result of substituting a
1276 parameter variable. In this case we just pull out the component we
1279 comp
= expr
->ref
->u
.c
.sym
->components
;
1281 comp
= expr
->ts
.u
.derived
->components
;
1283 for (; comp
&& cons
; comp
= comp
->next
, cons
= gfc_constructor_next (cons
))
1290 /* Unions use an EXPR_NULL contrived expression to tell the translation
1291 phase to generate an initializer of the appropriate length.
1293 if (cons
->expr
->ts
.type
== BT_UNION
&& cons
->expr
->expr_type
== EXPR_NULL
)
1296 if (!gfc_resolve_expr (cons
->expr
))
1302 rank
= comp
->as
? comp
->as
->rank
: 0;
1303 if (comp
->ts
.type
== BT_CLASS
1304 && !comp
->ts
.u
.derived
->attr
.unlimited_polymorphic
1305 && CLASS_DATA (comp
)->as
)
1306 rank
= CLASS_DATA (comp
)->as
->rank
;
1308 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
1309 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
1311 gfc_error ("The rank of the element in the structure "
1312 "constructor at %L does not match that of the "
1313 "component (%d/%d)", &cons
->expr
->where
,
1314 cons
->expr
->rank
, rank
);
1318 /* If we don't have the right type, try to convert it. */
1320 if (!comp
->attr
.proc_pointer
&&
1321 !gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
1323 if (strcmp (comp
->name
, "_extends") == 0)
1325 /* Can afford to be brutal with the _extends initializer.
1326 The derived type can get lost because it is PRIVATE
1327 but it is not usage constrained by the standard. */
1328 cons
->expr
->ts
= comp
->ts
;
1330 else if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
1332 gfc_error ("The element in the structure constructor at %L, "
1333 "for pointer component %qs, is %s but should be %s",
1334 &cons
->expr
->where
, comp
->name
,
1335 gfc_basic_typename (cons
->expr
->ts
.type
),
1336 gfc_basic_typename (comp
->ts
.type
));
1341 bool t2
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
1347 /* For strings, the length of the constructor should be the same as
1348 the one of the structure, ensure this if the lengths are known at
1349 compile time and when we are dealing with PARAMETER or structure
1351 if (cons
->expr
->ts
.type
== BT_CHARACTER
&& comp
->ts
.u
.cl
1352 && comp
->ts
.u
.cl
->length
1353 && comp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1354 && cons
->expr
->ts
.u
.cl
&& cons
->expr
->ts
.u
.cl
->length
1355 && cons
->expr
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
1356 && cons
->expr
->rank
!= 0
1357 && mpz_cmp (cons
->expr
->ts
.u
.cl
->length
->value
.integer
,
1358 comp
->ts
.u
.cl
->length
->value
.integer
) != 0)
1360 if (cons
->expr
->expr_type
== EXPR_VARIABLE
1361 && cons
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
1363 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1364 to make use of the gfc_resolve_character_array_constructor
1365 machinery. The expression is later simplified away to
1366 an array of string literals. */
1367 gfc_expr
*para
= cons
->expr
;
1368 cons
->expr
= gfc_get_expr ();
1369 cons
->expr
->ts
= para
->ts
;
1370 cons
->expr
->where
= para
->where
;
1371 cons
->expr
->expr_type
= EXPR_ARRAY
;
1372 cons
->expr
->rank
= para
->rank
;
1373 cons
->expr
->shape
= gfc_copy_shape (para
->shape
, para
->rank
);
1374 gfc_constructor_append_expr (&cons
->expr
->value
.constructor
,
1375 para
, &cons
->expr
->where
);
1378 if (cons
->expr
->expr_type
== EXPR_ARRAY
)
1380 /* Rely on the cleanup of the namespace to deal correctly with
1381 the old charlen. (There was a block here that attempted to
1382 remove the charlen but broke the chain in so doing.) */
1383 cons
->expr
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1384 cons
->expr
->ts
.u
.cl
->length_from_typespec
= true;
1385 cons
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (comp
->ts
.u
.cl
->length
);
1386 gfc_resolve_character_array_constructor (cons
->expr
);
1390 if (cons
->expr
->expr_type
== EXPR_NULL
1391 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
1392 || comp
->attr
.proc_pointer
|| comp
->ts
.f90_type
== BT_VOID
1393 || (comp
->ts
.type
== BT_CLASS
1394 && (CLASS_DATA (comp
)->attr
.class_pointer
1395 || CLASS_DATA (comp
)->attr
.allocatable
))))
1398 gfc_error ("The NULL in the structure constructor at %L is "
1399 "being applied to component %qs, which is neither "
1400 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
1404 if (comp
->attr
.proc_pointer
&& comp
->ts
.interface
)
1406 /* Check procedure pointer interface. */
1407 gfc_symbol
*s2
= NULL
;
1412 c2
= gfc_get_proc_ptr_comp (cons
->expr
);
1415 s2
= c2
->ts
.interface
;
1418 else if (cons
->expr
->expr_type
== EXPR_FUNCTION
)
1420 s2
= cons
->expr
->symtree
->n
.sym
->result
;
1421 name
= cons
->expr
->symtree
->n
.sym
->result
->name
;
1423 else if (cons
->expr
->expr_type
!= EXPR_NULL
)
1425 s2
= cons
->expr
->symtree
->n
.sym
;
1426 name
= cons
->expr
->symtree
->n
.sym
->name
;
1429 if (s2
&& !gfc_compare_interfaces (comp
->ts
.interface
, s2
, name
, 0, 1,
1430 err
, sizeof (err
), NULL
, NULL
))
1432 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1433 "component %qs in structure constructor at %L:"
1434 " %s", comp
->name
, &cons
->expr
->where
, err
);
1439 if (!comp
->attr
.pointer
|| comp
->attr
.proc_pointer
1440 || cons
->expr
->expr_type
== EXPR_NULL
)
1443 a
= gfc_expr_attr (cons
->expr
);
1445 if (!a
.pointer
&& !a
.target
)
1448 gfc_error ("The element in the structure constructor at %L, "
1449 "for pointer component %qs should be a POINTER or "
1450 "a TARGET", &cons
->expr
->where
, comp
->name
);
1455 /* F08:C461. Additional checks for pointer initialization. */
1459 gfc_error ("Pointer initialization target at %L "
1460 "must not be ALLOCATABLE", &cons
->expr
->where
);
1465 gfc_error ("Pointer initialization target at %L "
1466 "must have the SAVE attribute", &cons
->expr
->where
);
1470 /* F2003, C1272 (3). */
1471 bool impure
= cons
->expr
->expr_type
== EXPR_VARIABLE
1472 && (gfc_impure_variable (cons
->expr
->symtree
->n
.sym
)
1473 || gfc_is_coindexed (cons
->expr
));
1474 if (impure
&& gfc_pure (NULL
))
1477 gfc_error ("Invalid expression in the structure constructor for "
1478 "pointer component %qs at %L in PURE procedure",
1479 comp
->name
, &cons
->expr
->where
);
1483 gfc_unset_implicit_pure (NULL
);
1490 /****************** Expression name resolution ******************/
1492 /* Returns 0 if a symbol was not declared with a type or
1493 attribute declaration statement, nonzero otherwise. */
1496 was_declared (gfc_symbol
*sym
)
1502 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
1505 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
1506 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
1507 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
1508 || a
.asynchronous
|| a
.codimension
)
1515 /* Determine if a symbol is generic or not. */
1518 generic_sym (gfc_symbol
*sym
)
1522 if (sym
->attr
.generic
||
1523 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
1526 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1529 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1536 return generic_sym (s
);
1543 /* Determine if a symbol is specific or not. */
1546 specific_sym (gfc_symbol
*sym
)
1550 if (sym
->attr
.if_source
== IFSRC_IFBODY
1551 || sym
->attr
.proc
== PROC_MODULE
1552 || sym
->attr
.proc
== PROC_INTERNAL
1553 || sym
->attr
.proc
== PROC_ST_FUNCTION
1554 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
1555 || sym
->attr
.external
)
1558 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1561 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1563 return (s
== NULL
) ? 0 : specific_sym (s
);
1567 /* Figure out if the procedure is specific, generic or unknown. */
1570 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
};
1573 procedure_kind (gfc_symbol
*sym
)
1575 if (generic_sym (sym
))
1576 return PTYPE_GENERIC
;
1578 if (specific_sym (sym
))
1579 return PTYPE_SPECIFIC
;
1581 return PTYPE_UNKNOWN
;
1584 /* Check references to assumed size arrays. The flag need_full_assumed_size
1585 is nonzero when matching actual arguments. */
1587 static int need_full_assumed_size
= 0;
1590 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1592 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1595 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1596 What should it be? */
1597 if (e
->ref
&& (e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1598 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1599 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1601 gfc_error ("The upper bound in the last dimension must "
1602 "appear in the reference to the assumed size "
1603 "array %qs at %L", sym
->name
, &e
->where
);
1610 /* Look for bad assumed size array references in argument expressions
1611 of elemental and array valued intrinsic procedures. Since this is
1612 called from procedure resolution functions, it only recurses at
1616 resolve_assumed_size_actual (gfc_expr
*e
)
1621 switch (e
->expr_type
)
1624 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1629 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1630 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1641 /* Check a generic procedure, passed as an actual argument, to see if
1642 there is a matching specific name. If none, it is an error, and if
1643 more than one, the reference is ambiguous. */
1645 count_specific_procs (gfc_expr
*e
)
1652 sym
= e
->symtree
->n
.sym
;
1654 for (p
= sym
->generic
; p
; p
= p
->next
)
1655 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1657 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1663 gfc_error ("%qs at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1667 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1668 "argument at %L", sym
->name
, &e
->where
);
1674 /* See if a call to sym could possibly be a not allowed RECURSION because of
1675 a missing RECURSIVE declaration. This means that either sym is the current
1676 context itself, or sym is the parent of a contained procedure calling its
1677 non-RECURSIVE containing procedure.
1678 This also works if sym is an ENTRY. */
1681 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1683 gfc_symbol
* proc_sym
;
1684 gfc_symbol
* context_proc
;
1685 gfc_namespace
* real_context
;
1687 if (sym
->attr
.flavor
== FL_PROGRAM
1688 || gfc_fl_struct (sym
->attr
.flavor
))
1691 /* If we've got an ENTRY, find real procedure. */
1692 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1693 proc_sym
= sym
->ns
->entries
->sym
;
1697 /* If sym is RECURSIVE, all is well of course. */
1698 if (proc_sym
->attr
.recursive
|| flag_recursive
)
1701 /* Find the context procedure's "real" symbol if it has entries.
1702 We look for a procedure symbol, so recurse on the parents if we don't
1703 find one (like in case of a BLOCK construct). */
1704 for (real_context
= context
; ; real_context
= real_context
->parent
)
1706 /* We should find something, eventually! */
1707 gcc_assert (real_context
);
1709 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1710 : real_context
->proc_name
);
1712 /* In some special cases, there may not be a proc_name, like for this
1714 real(bad_kind()) function foo () ...
1715 when checking the call to bad_kind ().
1716 In these cases, we simply return here and assume that the
1721 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1725 /* A call from sym's body to itself is recursion, of course. */
1726 if (context_proc
== proc_sym
)
1729 /* The same is true if context is a contained procedure and sym the
1731 if (context_proc
->attr
.contained
)
1733 gfc_symbol
* parent_proc
;
1735 gcc_assert (context
->parent
);
1736 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1737 : context
->parent
->proc_name
);
1739 if (parent_proc
== proc_sym
)
1747 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1748 its typespec and formal argument list. */
1751 gfc_resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1753 gfc_intrinsic_sym
* isym
= NULL
;
1756 if (sym
->resolve_symbol_called
>= 2)
1759 sym
->resolve_symbol_called
= 2;
1761 /* Already resolved. */
1762 if (sym
->from_intmod
&& sym
->ts
.type
!= BT_UNKNOWN
)
1765 /* We already know this one is an intrinsic, so we don't call
1766 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1767 gfc_find_subroutine directly to check whether it is a function or
1770 if (sym
->intmod_sym_id
&& sym
->attr
.subroutine
)
1772 gfc_isym_id id
= gfc_isym_id_by_intmod_sym (sym
);
1773 isym
= gfc_intrinsic_subroutine_by_id (id
);
1775 else if (sym
->intmod_sym_id
)
1777 gfc_isym_id id
= gfc_isym_id_by_intmod_sym (sym
);
1778 isym
= gfc_intrinsic_function_by_id (id
);
1780 else if (!sym
->attr
.subroutine
)
1781 isym
= gfc_find_function (sym
->name
);
1783 if (isym
&& !sym
->attr
.subroutine
)
1785 if (sym
->ts
.type
!= BT_UNKNOWN
&& warn_surprising
1786 && !sym
->attr
.implicit_type
)
1787 gfc_warning (OPT_Wsurprising
,
1788 "Type specified for intrinsic function %qs at %L is"
1789 " ignored", sym
->name
, &sym
->declared_at
);
1791 if (!sym
->attr
.function
&&
1792 !gfc_add_function(&sym
->attr
, sym
->name
, loc
))
1797 else if (isym
|| (isym
= gfc_find_subroutine (sym
->name
)))
1799 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1801 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1802 " specifier", sym
->name
, &sym
->declared_at
);
1806 if (!sym
->attr
.subroutine
&&
1807 !gfc_add_subroutine(&sym
->attr
, sym
->name
, loc
))
1812 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym
->name
,
1817 gfc_copy_formal_args_intr (sym
, isym
, NULL
);
1819 sym
->attr
.pure
= isym
->pure
;
1820 sym
->attr
.elemental
= isym
->elemental
;
1822 /* Check it is actually available in the standard settings. */
1823 if (!gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
))
1825 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1826 "available in the current standard settings but %s. Use "
1827 "an appropriate %<-std=*%> option or enable "
1828 "%<-fall-intrinsics%> in order to use it.",
1829 sym
->name
, &sym
->declared_at
, symstd
);
1837 /* Resolve a procedure expression, like passing it to a called procedure or as
1838 RHS for a procedure pointer assignment. */
1841 resolve_procedure_expression (gfc_expr
* expr
)
1845 if (expr
->expr_type
!= EXPR_VARIABLE
)
1847 gcc_assert (expr
->symtree
);
1849 sym
= expr
->symtree
->n
.sym
;
1851 if (sym
->attr
.intrinsic
)
1852 gfc_resolve_intrinsic (sym
, &expr
->where
);
1854 if (sym
->attr
.flavor
!= FL_PROCEDURE
1855 || (sym
->attr
.function
&& sym
->result
== sym
))
1858 /* A non-RECURSIVE procedure that is used as procedure expression within its
1859 own body is in danger of being called recursively. */
1860 if (is_illegal_recursion (sym
, gfc_current_ns
))
1861 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1862 " itself recursively. Declare it RECURSIVE or use"
1863 " %<-frecursive%>", sym
->name
, &expr
->where
);
1869 /* Check that name is not a derived type. */
1872 is_dt_name (const char *name
)
1874 gfc_symbol
*dt_list
, *dt_first
;
1876 dt_list
= dt_first
= gfc_derived_types
;
1877 for (; dt_list
; dt_list
= dt_list
->dt_next
)
1879 if (strcmp(dt_list
->name
, name
) == 0)
1881 if (dt_first
== dt_list
->dt_next
)
1888 /* Resolve an actual argument list. Most of the time, this is just
1889 resolving the expressions in the list.
1890 The exception is that we sometimes have to decide whether arguments
1891 that look like procedure arguments are really simple variable
1895 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1896 bool no_formal_args
)
1899 gfc_symtree
*parent_st
;
1901 gfc_component
*comp
;
1902 int save_need_full_assumed_size
;
1903 bool return_value
= false;
1904 bool actual_arg_sav
= actual_arg
, first_actual_arg_sav
= first_actual_arg
;
1907 first_actual_arg
= true;
1909 for (; arg
; arg
= arg
->next
)
1914 /* Check the label is a valid branching target. */
1917 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1919 gfc_error ("Label %d referenced at %L is never defined",
1920 arg
->label
->value
, &arg
->label
->where
);
1924 first_actual_arg
= false;
1928 if (e
->expr_type
== EXPR_VARIABLE
1929 && e
->symtree
->n
.sym
->attr
.generic
1931 && count_specific_procs (e
) != 1)
1934 if (e
->ts
.type
!= BT_PROCEDURE
)
1936 save_need_full_assumed_size
= need_full_assumed_size
;
1937 if (e
->expr_type
!= EXPR_VARIABLE
)
1938 need_full_assumed_size
= 0;
1939 if (!gfc_resolve_expr (e
))
1941 need_full_assumed_size
= save_need_full_assumed_size
;
1945 /* See if the expression node should really be a variable reference. */
1947 sym
= e
->symtree
->n
.sym
;
1949 if (sym
->attr
.flavor
== FL_PROCEDURE
&& is_dt_name (sym
->name
))
1951 gfc_error ("Derived type %qs is used as an actual "
1952 "argument at %L", sym
->name
, &e
->where
);
1956 if (sym
->attr
.flavor
== FL_PROCEDURE
1957 || sym
->attr
.intrinsic
1958 || sym
->attr
.external
)
1962 /* If a procedure is not already determined to be something else
1963 check if it is intrinsic. */
1964 if (gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1965 sym
->attr
.intrinsic
= 1;
1967 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1969 gfc_error ("Statement function %qs at %L is not allowed as an "
1970 "actual argument", sym
->name
, &e
->where
);
1973 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1974 sym
->attr
.subroutine
);
1975 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1977 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1978 "actual argument", sym
->name
, &e
->where
);
1981 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1982 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1984 if (!gfc_notify_std (GFC_STD_F2008
, "Internal procedure %qs is"
1985 " used as actual argument at %L",
1986 sym
->name
, &e
->where
))
1990 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1992 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1993 "allowed as an actual argument at %L", sym
->name
,
1997 /* Check if a generic interface has a specific procedure
1998 with the same name before emitting an error. */
1999 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
2002 /* Just in case a specific was found for the expression. */
2003 sym
= e
->symtree
->n
.sym
;
2005 /* If the symbol is the function that names the current (or
2006 parent) scope, then we really have a variable reference. */
2008 if (gfc_is_function_return_value (sym
, sym
->ns
))
2011 /* If all else fails, see if we have a specific intrinsic. */
2012 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
2014 gfc_intrinsic_sym
*isym
;
2016 isym
= gfc_find_function (sym
->name
);
2017 if (isym
== NULL
|| !isym
->specific
)
2019 gfc_error ("Unable to find a specific INTRINSIC procedure "
2020 "for the reference %qs at %L", sym
->name
,
2025 sym
->attr
.intrinsic
= 1;
2026 sym
->attr
.function
= 1;
2029 if (!gfc_resolve_expr (e
))
2034 /* See if the name is a module procedure in a parent unit. */
2036 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
2039 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
2041 gfc_error ("Symbol %qs at %L is ambiguous", sym
->name
, &e
->where
);
2045 if (parent_st
== NULL
)
2048 sym
= parent_st
->n
.sym
;
2049 e
->symtree
= parent_st
; /* Point to the right thing. */
2051 if (sym
->attr
.flavor
== FL_PROCEDURE
2052 || sym
->attr
.intrinsic
2053 || sym
->attr
.external
)
2055 if (!gfc_resolve_expr (e
))
2061 e
->expr_type
= EXPR_VARIABLE
;
2063 if ((sym
->as
!= NULL
&& sym
->ts
.type
!= BT_CLASS
)
2064 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
2065 && CLASS_DATA (sym
)->as
))
2067 e
->rank
= sym
->ts
.type
== BT_CLASS
2068 ? CLASS_DATA (sym
)->as
->rank
: sym
->as
->rank
;
2069 e
->ref
= gfc_get_ref ();
2070 e
->ref
->type
= REF_ARRAY
;
2071 e
->ref
->u
.ar
.type
= AR_FULL
;
2072 e
->ref
->u
.ar
.as
= sym
->ts
.type
== BT_CLASS
2073 ? CLASS_DATA (sym
)->as
: sym
->as
;
2076 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2077 primary.c (match_actual_arg). If above code determines that it
2078 is a variable instead, it needs to be resolved as it was not
2079 done at the beginning of this function. */
2080 save_need_full_assumed_size
= need_full_assumed_size
;
2081 if (e
->expr_type
!= EXPR_VARIABLE
)
2082 need_full_assumed_size
= 0;
2083 if (!gfc_resolve_expr (e
))
2085 need_full_assumed_size
= save_need_full_assumed_size
;
2088 /* Check argument list functions %VAL, %LOC and %REF. There is
2089 nothing to do for %REF. */
2090 if (arg
->name
&& arg
->name
[0] == '%')
2092 if (strcmp ("%VAL", arg
->name
) == 0)
2094 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
2096 gfc_error ("By-value argument at %L is not of numeric "
2103 gfc_error ("By-value argument at %L cannot be an array or "
2104 "an array section", &e
->where
);
2108 /* Intrinsics are still PROC_UNKNOWN here. However,
2109 since same file external procedures are not resolvable
2110 in gfortran, it is a good deal easier to leave them to
2112 if (ptype
!= PROC_UNKNOWN
2113 && ptype
!= PROC_DUMMY
2114 && ptype
!= PROC_EXTERNAL
2115 && ptype
!= PROC_MODULE
)
2117 gfc_error ("By-value argument at %L is not allowed "
2118 "in this context", &e
->where
);
2123 /* Statement functions have already been excluded above. */
2124 else if (strcmp ("%LOC", arg
->name
) == 0
2125 && e
->ts
.type
== BT_PROCEDURE
)
2127 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
2129 gfc_error ("Passing internal procedure at %L by location "
2130 "not allowed", &e
->where
);
2136 comp
= gfc_get_proc_ptr_comp(e
);
2137 if (e
->expr_type
== EXPR_VARIABLE
2138 && comp
&& comp
->attr
.elemental
)
2140 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2141 "allowed as an actual argument at %L", comp
->name
,
2145 /* Fortran 2008, C1237. */
2146 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_is_coindexed (e
)
2147 && gfc_has_ultimate_pointer (e
))
2149 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2150 "component", &e
->where
);
2154 first_actual_arg
= false;
2157 return_value
= true;
2160 actual_arg
= actual_arg_sav
;
2161 first_actual_arg
= first_actual_arg_sav
;
2163 return return_value
;
2167 /* Do the checks of the actual argument list that are specific to elemental
2168 procedures. If called with c == NULL, we have a function, otherwise if
2169 expr == NULL, we have a subroutine. */
2172 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
2174 gfc_actual_arglist
*arg0
;
2175 gfc_actual_arglist
*arg
;
2176 gfc_symbol
*esym
= NULL
;
2177 gfc_intrinsic_sym
*isym
= NULL
;
2179 gfc_intrinsic_arg
*iformal
= NULL
;
2180 gfc_formal_arglist
*eformal
= NULL
;
2181 bool formal_optional
= false;
2182 bool set_by_optional
= false;
2186 /* Is this an elemental procedure? */
2187 if (expr
&& expr
->value
.function
.actual
!= NULL
)
2189 if (expr
->value
.function
.esym
!= NULL
2190 && expr
->value
.function
.esym
->attr
.elemental
)
2192 arg0
= expr
->value
.function
.actual
;
2193 esym
= expr
->value
.function
.esym
;
2195 else if (expr
->value
.function
.isym
!= NULL
2196 && expr
->value
.function
.isym
->elemental
)
2198 arg0
= expr
->value
.function
.actual
;
2199 isym
= expr
->value
.function
.isym
;
2204 else if (c
&& c
->ext
.actual
!= NULL
)
2206 arg0
= c
->ext
.actual
;
2208 if (c
->resolved_sym
)
2209 esym
= c
->resolved_sym
;
2211 esym
= c
->symtree
->n
.sym
;
2214 if (!esym
->attr
.elemental
)
2220 /* The rank of an elemental is the rank of its array argument(s). */
2221 for (arg
= arg0
; arg
; arg
= arg
->next
)
2223 if (arg
->expr
!= NULL
&& arg
->expr
->rank
!= 0)
2225 rank
= arg
->expr
->rank
;
2226 if (arg
->expr
->expr_type
== EXPR_VARIABLE
2227 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
2228 set_by_optional
= true;
2230 /* Function specific; set the result rank and shape. */
2234 if (!expr
->shape
&& arg
->expr
->shape
)
2236 expr
->shape
= gfc_get_shape (rank
);
2237 for (i
= 0; i
< rank
; i
++)
2238 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
2245 /* If it is an array, it shall not be supplied as an actual argument
2246 to an elemental procedure unless an array of the same rank is supplied
2247 as an actual argument corresponding to a nonoptional dummy argument of
2248 that elemental procedure(12.4.1.5). */
2249 formal_optional
= false;
2251 iformal
= isym
->formal
;
2253 eformal
= esym
->formal
;
2255 for (arg
= arg0
; arg
; arg
= arg
->next
)
2259 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
2260 formal_optional
= true;
2261 eformal
= eformal
->next
;
2263 else if (isym
&& iformal
)
2265 if (iformal
->optional
)
2266 formal_optional
= true;
2267 iformal
= iformal
->next
;
2270 formal_optional
= true;
2272 if (pedantic
&& arg
->expr
!= NULL
2273 && arg
->expr
->expr_type
== EXPR_VARIABLE
2274 && arg
->expr
->symtree
->n
.sym
->attr
.optional
2277 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
2278 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
2281 gfc_actual_arglist
*a
;
2283 /* Scan the argument list for a non-optional argument with the
2284 same rank as arg. */
2285 for (a
= arg0
; a
; a
= a
->next
)
2287 && a
->expr
->rank
== arg
->expr
->rank
2288 && !a
->expr
->symtree
->n
.sym
->attr
.optional
)
2295 gfc_warning (OPT_Wpedantic
,
2296 "%qs at %L is an array and OPTIONAL; If it is not "
2297 "present, then it cannot be the actual argument of "
2298 "an ELEMENTAL procedure unless there is a non-optional"
2299 " argument with the same rank "
2300 "(Fortran 2018, 15.5.2.12)",
2301 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
2305 for (arg
= arg0
; arg
; arg
= arg
->next
)
2307 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
2310 /* Being elemental, the last upper bound of an assumed size array
2311 argument must be present. */
2312 if (resolve_assumed_size_actual (arg
->expr
))
2315 /* Elemental procedure's array actual arguments must conform. */
2318 if (!gfc_check_conformance (arg
->expr
, e
, _("elemental procedure")))
2325 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2326 is an array, the intent inout/out variable needs to be also an array. */
2327 if (rank
> 0 && esym
&& expr
== NULL
)
2328 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
2329 arg
= arg
->next
, eformal
= eformal
->next
)
2330 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
2331 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
2332 && arg
->expr
&& arg
->expr
->rank
== 0)
2334 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2335 "ELEMENTAL subroutine %qs is a scalar, but another "
2336 "actual argument is an array", &arg
->expr
->where
,
2337 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
2338 : "INOUT", eformal
->sym
->name
, esym
->name
);
2345 /* This function does the checking of references to global procedures
2346 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2347 77 and 95 standards. It checks for a gsymbol for the name, making
2348 one if it does not already exist. If it already exists, then the
2349 reference being resolved must correspond to the type of gsymbol.
2350 Otherwise, the new symbol is equipped with the attributes of the
2351 reference. The corresponding code that is called in creating
2352 global entities is parse.c.
2354 In addition, for all but -std=legacy, the gsymbols are used to
2355 check the interfaces of external procedures from the same file.
2356 The namespace of the gsymbol is resolved and then, once this is
2357 done the interface is checked. */
2361 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
2363 if (!gsym_ns
->proc_name
->attr
.recursive
)
2366 if (sym
->ns
== gsym_ns
)
2369 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
2376 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
2378 if (gsym_ns
->entries
)
2380 gfc_entry_list
*entry
= gsym_ns
->entries
;
2382 for (; entry
; entry
= entry
->next
)
2384 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
2386 if (strcmp (gsym_ns
->proc_name
->name
,
2387 sym
->ns
->proc_name
->name
) == 0)
2391 && strcmp (gsym_ns
->proc_name
->name
,
2392 sym
->ns
->parent
->proc_name
->name
) == 0)
2401 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2404 gfc_explicit_interface_required (gfc_symbol
*sym
, char *errmsg
, int err_len
)
2406 gfc_formal_arglist
*arg
= gfc_sym_get_dummy_args (sym
);
2408 for ( ; arg
; arg
= arg
->next
)
2413 if (arg
->sym
->attr
.allocatable
) /* (2a) */
2415 strncpy (errmsg
, _("allocatable argument"), err_len
);
2418 else if (arg
->sym
->attr
.asynchronous
)
2420 strncpy (errmsg
, _("asynchronous argument"), err_len
);
2423 else if (arg
->sym
->attr
.optional
)
2425 strncpy (errmsg
, _("optional argument"), err_len
);
2428 else if (arg
->sym
->attr
.pointer
)
2430 strncpy (errmsg
, _("pointer argument"), err_len
);
2433 else if (arg
->sym
->attr
.target
)
2435 strncpy (errmsg
, _("target argument"), err_len
);
2438 else if (arg
->sym
->attr
.value
)
2440 strncpy (errmsg
, _("value argument"), err_len
);
2443 else if (arg
->sym
->attr
.volatile_
)
2445 strncpy (errmsg
, _("volatile argument"), err_len
);
2448 else if (arg
->sym
->as
&& arg
->sym
->as
->type
== AS_ASSUMED_SHAPE
) /* (2b) */
2450 strncpy (errmsg
, _("assumed-shape argument"), err_len
);
2453 else if (arg
->sym
->as
&& arg
->sym
->as
->type
== AS_ASSUMED_RANK
) /* TS 29113, 6.2. */
2455 strncpy (errmsg
, _("assumed-rank argument"), err_len
);
2458 else if (arg
->sym
->attr
.codimension
) /* (2c) */
2460 strncpy (errmsg
, _("coarray argument"), err_len
);
2463 else if (false) /* (2d) TODO: parametrized derived type */
2465 strncpy (errmsg
, _("parametrized derived type argument"), err_len
);
2468 else if (arg
->sym
->ts
.type
== BT_CLASS
) /* (2e) */
2470 strncpy (errmsg
, _("polymorphic argument"), err_len
);
2473 else if (arg
->sym
->attr
.ext_attr
& (1 << EXT_ATTR_NO_ARG_CHECK
))
2475 strncpy (errmsg
, _("NO_ARG_CHECK attribute"), err_len
);
2478 else if (arg
->sym
->ts
.type
== BT_ASSUMED
)
2480 /* As assumed-type is unlimited polymorphic (cf. above).
2481 See also TS 29113, Note 6.1. */
2482 strncpy (errmsg
, _("assumed-type argument"), err_len
);
2487 if (sym
->attr
.function
)
2489 gfc_symbol
*res
= sym
->result
? sym
->result
: sym
;
2491 if (res
->attr
.dimension
) /* (3a) */
2493 strncpy (errmsg
, _("array result"), err_len
);
2496 else if (res
->attr
.pointer
|| res
->attr
.allocatable
) /* (3b) */
2498 strncpy (errmsg
, _("pointer or allocatable result"), err_len
);
2501 else if (res
->ts
.type
== BT_CHARACTER
&& res
->ts
.u
.cl
2502 && res
->ts
.u
.cl
->length
2503 && res
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
) /* (3c) */
2505 strncpy (errmsg
, _("result with non-constant character length"), err_len
);
2510 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
) /* (4) */
2512 strncpy (errmsg
, _("elemental procedure"), err_len
);
2515 else if (sym
->attr
.is_bind_c
) /* (5) */
2517 strncpy (errmsg
, _("bind(c) procedure"), err_len
);
2526 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
, int sub
)
2530 enum gfc_symbol_type type
;
2533 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
2535 gsym
= gfc_get_gsymbol (sym
->binding_label
? sym
->binding_label
: sym
->name
,
2536 sym
->binding_label
!= NULL
);
2538 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
2539 gfc_global_used (gsym
, where
);
2541 if ((sym
->attr
.if_source
== IFSRC_UNKNOWN
2542 || sym
->attr
.if_source
== IFSRC_IFBODY
)
2543 && gsym
->type
!= GSYM_UNKNOWN
2544 && !gsym
->binding_label
2546 && gsym
->ns
->proc_name
2547 && not_in_recursive (sym
, gsym
->ns
)
2548 && not_entry_self_reference (sym
, gsym
->ns
))
2550 gfc_symbol
*def_sym
;
2551 def_sym
= gsym
->ns
->proc_name
;
2553 if (gsym
->ns
->resolved
!= -1)
2556 /* Resolve the gsymbol namespace if needed. */
2557 if (!gsym
->ns
->resolved
)
2559 gfc_symbol
*old_dt_list
;
2561 /* Stash away derived types so that the backend_decls
2562 do not get mixed up. */
2563 old_dt_list
= gfc_derived_types
;
2564 gfc_derived_types
= NULL
;
2566 gfc_resolve (gsym
->ns
);
2568 /* Store the new derived types with the global namespace. */
2569 if (gfc_derived_types
)
2570 gsym
->ns
->derived_types
= gfc_derived_types
;
2572 /* Restore the derived types of this namespace. */
2573 gfc_derived_types
= old_dt_list
;
2576 /* Make sure that translation for the gsymbol occurs before
2577 the procedure currently being resolved. */
2578 ns
= gfc_global_ns_list
;
2579 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
2581 if (ns
->sibling
== gsym
->ns
)
2583 ns
->sibling
= gsym
->ns
->sibling
;
2584 gsym
->ns
->sibling
= gfc_global_ns_list
;
2585 gfc_global_ns_list
= gsym
->ns
;
2590 /* This can happen if a binding name has been specified. */
2591 if (gsym
->binding_label
&& gsym
->sym_name
!= def_sym
->name
)
2592 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &def_sym
);
2594 if (def_sym
->attr
.entry_master
|| def_sym
->attr
.entry
)
2596 gfc_entry_list
*entry
;
2597 for (entry
= gsym
->ns
->entries
; entry
; entry
= entry
->next
)
2598 if (strcmp (entry
->sym
->name
, sym
->name
) == 0)
2600 def_sym
= entry
->sym
;
2606 if (sym
->attr
.function
&& !gfc_compare_types (&sym
->ts
, &def_sym
->ts
))
2608 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2609 sym
->name
, &sym
->declared_at
, gfc_typename (&sym
->ts
),
2610 gfc_typename (&def_sym
->ts
));
2614 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
2615 && gfc_explicit_interface_required (def_sym
, reason
, sizeof(reason
)))
2617 gfc_error ("Explicit interface required for %qs at %L: %s",
2618 sym
->name
, &sym
->declared_at
, reason
);
2622 bool bad_result_characteristics
;
2623 if (!gfc_compare_interfaces (sym
, def_sym
, sym
->name
, 0, 1,
2624 reason
, sizeof(reason
), NULL
, NULL
,
2625 &bad_result_characteristics
))
2627 /* Turn erros into warnings with -std=gnu and -std=legacy,
2628 unless a function returns a wrong type, which can lead
2629 to all kinds of ICEs and wrong code. */
2631 if (!pedantic
&& (gfc_option
.allow_std
& GFC_STD_GNU
)
2632 && !bad_result_characteristics
)
2633 gfc_errors_to_warnings (true);
2635 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2636 sym
->name
, &sym
->declared_at
, reason
);
2638 gfc_errors_to_warnings (false);
2645 if (gsym
->type
== GSYM_UNKNOWN
)
2648 gsym
->where
= *where
;
2655 /************* Function resolution *************/
2657 /* Resolve a function call known to be generic.
2658 Section 14.1.2.4.1. */
2661 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
2665 if (sym
->attr
.generic
)
2667 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
2670 expr
->value
.function
.name
= s
->name
;
2671 expr
->value
.function
.esym
= s
;
2673 if (s
->ts
.type
!= BT_UNKNOWN
)
2675 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
2676 expr
->ts
= s
->result
->ts
;
2679 expr
->rank
= s
->as
->rank
;
2680 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
2681 expr
->rank
= s
->result
->as
->rank
;
2683 gfc_set_sym_referenced (expr
->value
.function
.esym
);
2688 /* TODO: Need to search for elemental references in generic
2692 if (sym
->attr
.intrinsic
)
2693 return gfc_intrinsic_func_interface (expr
, 0);
2700 resolve_generic_f (gfc_expr
*expr
)
2704 gfc_interface
*intr
= NULL
;
2706 sym
= expr
->symtree
->n
.sym
;
2710 m
= resolve_generic_f0 (expr
, sym
);
2713 else if (m
== MATCH_ERROR
)
2718 for (intr
= sym
->generic
; intr
; intr
= intr
->next
)
2719 if (gfc_fl_struct (intr
->sym
->attr
.flavor
))
2722 if (sym
->ns
->parent
== NULL
)
2724 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2728 if (!generic_sym (sym
))
2732 /* Last ditch attempt. See if the reference is to an intrinsic
2733 that possesses a matching interface. 14.1.2.4 */
2734 if (sym
&& !intr
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
2736 if (gfc_init_expr_flag
)
2737 gfc_error ("Function %qs in initialization expression at %L "
2738 "must be an intrinsic function",
2739 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2741 gfc_error ("There is no specific function for the generic %qs "
2742 "at %L", expr
->symtree
->n
.sym
->name
, &expr
->where
);
2748 if (!gfc_convert_to_structure_constructor (expr
, intr
->sym
, NULL
,
2751 if (!gfc_use_derived (expr
->ts
.u
.derived
))
2753 return resolve_structure_cons (expr
, 0);
2756 m
= gfc_intrinsic_func_interface (expr
, 0);
2761 gfc_error ("Generic function %qs at %L is not consistent with a "
2762 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
2769 /* Resolve a function call known to be specific. */
2772 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
2776 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
2778 if (sym
->attr
.dummy
)
2780 sym
->attr
.proc
= PROC_DUMMY
;
2784 sym
->attr
.proc
= PROC_EXTERNAL
;
2788 if (sym
->attr
.proc
== PROC_MODULE
2789 || sym
->attr
.proc
== PROC_ST_FUNCTION
2790 || sym
->attr
.proc
== PROC_INTERNAL
)
2793 if (sym
->attr
.intrinsic
)
2795 m
= gfc_intrinsic_func_interface (expr
, 1);
2799 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2800 "with an intrinsic", sym
->name
, &expr
->where
);
2808 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2811 expr
->ts
= sym
->result
->ts
;
2814 expr
->value
.function
.name
= sym
->name
;
2815 expr
->value
.function
.esym
= sym
;
2816 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2818 if (sym
->ts
.type
== BT_CLASS
&& !CLASS_DATA (sym
))
2820 if (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->as
)
2821 expr
->rank
= CLASS_DATA (sym
)->as
->rank
;
2822 else if (sym
->as
!= NULL
)
2823 expr
->rank
= sym
->as
->rank
;
2830 resolve_specific_f (gfc_expr
*expr
)
2835 sym
= expr
->symtree
->n
.sym
;
2839 m
= resolve_specific_f0 (sym
, expr
);
2842 if (m
== MATCH_ERROR
)
2845 if (sym
->ns
->parent
== NULL
)
2848 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2854 gfc_error ("Unable to resolve the specific function %qs at %L",
2855 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2860 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2861 candidates in CANDIDATES_LEN. */
2864 lookup_function_fuzzy_find_candidates (gfc_symtree
*sym
,
2866 size_t &candidates_len
)
2872 if ((sym
->n
.sym
->ts
.type
!= BT_UNKNOWN
|| sym
->n
.sym
->attr
.external
)
2873 && sym
->n
.sym
->attr
.flavor
== FL_PROCEDURE
)
2874 vec_push (candidates
, candidates_len
, sym
->name
);
2878 lookup_function_fuzzy_find_candidates (p
, candidates
, candidates_len
);
2882 lookup_function_fuzzy_find_candidates (p
, candidates
, candidates_len
);
2886 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2889 gfc_lookup_function_fuzzy (const char *fn
, gfc_symtree
*symroot
)
2891 char **candidates
= NULL
;
2892 size_t candidates_len
= 0;
2893 lookup_function_fuzzy_find_candidates (symroot
, candidates
, candidates_len
);
2894 return gfc_closest_fuzzy_match (fn
, candidates
);
2898 /* Resolve a procedure call not known to be generic nor specific. */
2901 resolve_unknown_f (gfc_expr
*expr
)
2906 sym
= expr
->symtree
->n
.sym
;
2908 if (sym
->attr
.dummy
)
2910 sym
->attr
.proc
= PROC_DUMMY
;
2911 expr
->value
.function
.name
= sym
->name
;
2915 /* See if we have an intrinsic function reference. */
2917 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2919 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2924 /* The reference is to an external name. */
2926 sym
->attr
.proc
= PROC_EXTERNAL
;
2927 expr
->value
.function
.name
= sym
->name
;
2928 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2930 if (sym
->as
!= NULL
)
2931 expr
->rank
= sym
->as
->rank
;
2933 /* Type of the expression is either the type of the symbol or the
2934 default type of the symbol. */
2937 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2939 if (sym
->ts
.type
!= BT_UNKNOWN
)
2943 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2945 if (ts
->type
== BT_UNKNOWN
)
2948 = gfc_lookup_function_fuzzy (sym
->name
, sym
->ns
->sym_root
);
2950 gfc_error ("Function %qs at %L has no IMPLICIT type"
2951 "; did you mean %qs?",
2952 sym
->name
, &expr
->where
, guessed
);
2954 gfc_error ("Function %qs at %L has no IMPLICIT type",
2955 sym
->name
, &expr
->where
);
2966 /* Return true, if the symbol is an external procedure. */
2968 is_external_proc (gfc_symbol
*sym
)
2970 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2971 && !gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
)
2972 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2973 && !sym
->attr
.proc_pointer
2974 && !sym
->attr
.use_assoc
2982 /* Figure out if a function reference is pure or not. Also set the name
2983 of the function for a potential error message. Return nonzero if the
2984 function is PURE, zero if not. */
2986 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2989 gfc_pure_function (gfc_expr
*e
, const char **name
)
2992 gfc_component
*comp
;
2996 if (e
->symtree
!= NULL
2997 && e
->symtree
->n
.sym
!= NULL
2998 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2999 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
3001 comp
= gfc_get_proc_ptr_comp (e
);
3004 pure
= gfc_pure (comp
->ts
.interface
);
3007 else if (e
->value
.function
.esym
)
3009 pure
= gfc_pure (e
->value
.function
.esym
);
3010 *name
= e
->value
.function
.esym
->name
;
3012 else if (e
->value
.function
.isym
)
3014 pure
= e
->value
.function
.isym
->pure
3015 || e
->value
.function
.isym
->elemental
;
3016 *name
= e
->value
.function
.isym
->name
;
3020 /* Implicit functions are not pure. */
3022 *name
= e
->value
.function
.name
;
3029 /* Check if the expression is a reference to an implicitly pure function. */
3032 gfc_implicit_pure_function (gfc_expr
*e
)
3034 gfc_component
*comp
= gfc_get_proc_ptr_comp (e
);
3036 return gfc_implicit_pure (comp
->ts
.interface
);
3037 else if (e
->value
.function
.esym
)
3038 return gfc_implicit_pure (e
->value
.function
.esym
);
3045 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
3046 int *f ATTRIBUTE_UNUSED
)
3050 /* Don't bother recursing into other statement functions
3051 since they will be checked individually for purity. */
3052 if (e
->expr_type
!= EXPR_FUNCTION
3054 || e
->symtree
->n
.sym
== sym
3055 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
3058 return gfc_pure_function (e
, &name
) ? false : true;
3063 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
3065 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
3069 /* Check if an impure function is allowed in the current context. */
3071 static bool check_pure_function (gfc_expr
*e
)
3073 const char *name
= NULL
;
3074 if (!gfc_pure_function (e
, &name
) && name
)
3078 gfc_error ("Reference to impure function %qs at %L inside a "
3079 "FORALL %s", name
, &e
->where
,
3080 forall_flag
== 2 ? "mask" : "block");
3083 else if (gfc_do_concurrent_flag
)
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "DO CONCURRENT %s", name
, &e
->where
,
3087 gfc_do_concurrent_flag
== 2 ? "mask" : "block");
3090 else if (gfc_pure (NULL
))
3092 gfc_error ("Reference to impure function %qs at %L "
3093 "within a PURE procedure", name
, &e
->where
);
3096 if (!gfc_implicit_pure_function (e
))
3097 gfc_unset_implicit_pure (NULL
);
3103 /* Update current procedure's array_outer_dependency flag, considering
3104 a call to procedure SYM. */
3107 update_current_proc_array_outer_dependency (gfc_symbol
*sym
)
3109 /* Check to see if this is a sibling function that has not yet
3111 gfc_namespace
*sibling
= gfc_current_ns
->sibling
;
3112 for (; sibling
; sibling
= sibling
->sibling
)
3114 if (sibling
->proc_name
== sym
)
3116 gfc_resolve (sibling
);
3121 /* If SYM has references to outer arrays, so has the procedure calling
3122 SYM. If SYM is a procedure pointer, we can assume the worst. */
3123 if ((sym
->attr
.array_outer_dependency
|| sym
->attr
.proc_pointer
)
3124 && gfc_current_ns
->proc_name
)
3125 gfc_current_ns
->proc_name
->attr
.array_outer_dependency
= 1;
3129 /* Resolve a function call, which means resolving the arguments, then figuring
3130 out which entity the name refers to. */
3133 resolve_function (gfc_expr
*expr
)
3135 gfc_actual_arglist
*arg
;
3139 procedure_type p
= PROC_INTRINSIC
;
3140 bool no_formal_args
;
3144 sym
= expr
->symtree
->n
.sym
;
3146 /* If this is a procedure pointer component, it has already been resolved. */
3147 if (gfc_is_proc_ptr_comp (expr
))
3150 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3152 if (sym
&& sym
->attr
.intrinsic
3153 && (sym
->intmod_sym_id
== GFC_ISYM_CAF_GET
3154 || sym
->intmod_sym_id
== GFC_ISYM_CAF_SEND
))
3159 gfc_error ("Unexpected junk after %qs at %L", expr
->symtree
->n
.sym
->name
,
3164 if (sym
&& sym
->attr
.intrinsic
3165 && !gfc_resolve_intrinsic (sym
, &expr
->where
))
3168 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
3170 gfc_error ("%qs at %L is not a function", sym
->name
, &expr
->where
);
3174 /* If this is a deferred TBP with an abstract interface (which may
3175 of course be referenced), expr->value.function.esym will be set. */
3176 if (sym
&& sym
->attr
.abstract
&& !expr
->value
.function
.esym
)
3178 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3179 sym
->name
, &expr
->where
);
3183 /* If this is a deferred TBP with an abstract interface, its result
3184 cannot be an assumed length character (F2003: C418). */
3185 if (sym
&& sym
->attr
.abstract
&& sym
->attr
.function
3186 && sym
->result
->ts
.u
.cl
3187 && sym
->result
->ts
.u
.cl
->length
== NULL
3188 && !sym
->result
->ts
.deferred
)
3190 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3191 "character length result (F2008: C418)", sym
->name
,
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size
++;
3200 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
3201 p
= expr
->symtree
->n
.sym
->attr
.proc
;
3203 if (expr
->value
.function
.isym
&& expr
->value
.function
.isym
->inquiry
)
3204 inquiry_argument
= true;
3205 no_formal_args
= sym
&& is_external_proc (sym
)
3206 && gfc_sym_get_dummy_args (sym
) == NULL
;
3208 if (!resolve_actual_arglist (expr
->value
.function
.actual
,
3211 inquiry_argument
= false;
3215 inquiry_argument
= false;
3217 /* Resume assumed_size checking. */
3218 need_full_assumed_size
--;
3220 /* If the procedure is external, check for usage. */
3221 if (sym
&& is_external_proc (sym
))
3222 resolve_global_procedure (sym
, &expr
->where
, 0);
3224 if (sym
&& sym
->ts
.type
== BT_CHARACTER
3226 && sym
->ts
.u
.cl
->length
== NULL
3228 && !sym
->ts
.deferred
3229 && expr
->value
.function
.esym
== NULL
3230 && !sym
->attr
.contained
)
3232 /* Internal procedures are taken care of in resolve_contained_fntype. */
3233 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3234 "be used at %L since it is not a dummy argument",
3235 sym
->name
, &expr
->where
);
3239 /* See if function is already resolved. */
3241 if (expr
->value
.function
.name
!= NULL
3242 || expr
->value
.function
.isym
!= NULL
)
3244 if (expr
->ts
.type
== BT_UNKNOWN
)
3250 /* Apply the rules of section 14.1.2. */
3252 switch (procedure_kind (sym
))
3255 t
= resolve_generic_f (expr
);
3258 case PTYPE_SPECIFIC
:
3259 t
= resolve_specific_f (expr
);
3263 t
= resolve_unknown_f (expr
);
3267 gfc_internal_error ("resolve_function(): bad function type");
3271 /* If the expression is still a function (it might have simplified),
3272 then we check to see if we are calling an elemental function. */
3274 if (expr
->expr_type
!= EXPR_FUNCTION
)
3277 /* Walk the argument list looking for invalid BOZ. */
3278 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
3279 if (arg
->expr
&& arg
->expr
->ts
.type
== BT_BOZ
)
3281 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3282 "actual argument in a function reference",
3287 temp
= need_full_assumed_size
;
3288 need_full_assumed_size
= 0;
3290 if (!resolve_elemental_actual (expr
, NULL
))
3293 if (omp_workshare_flag
3294 && expr
->value
.function
.esym
3295 && ! gfc_elemental (expr
->value
.function
.esym
))
3297 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3298 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
3303 #define GENERIC_ID expr->value.function.isym->id
3304 else if (expr
->value
.function
.actual
!= NULL
3305 && expr
->value
.function
.isym
!= NULL
3306 && GENERIC_ID
!= GFC_ISYM_LBOUND
3307 && GENERIC_ID
!= GFC_ISYM_LCOBOUND
3308 && GENERIC_ID
!= GFC_ISYM_UCOBOUND
3309 && GENERIC_ID
!= GFC_ISYM_LEN
3310 && GENERIC_ID
!= GFC_ISYM_LOC
3311 && GENERIC_ID
!= GFC_ISYM_C_LOC
3312 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
3314 /* Array intrinsics must also have the last upper bound of an
3315 assumed size array argument. UBOUND and SIZE have to be
3316 excluded from the check if the second argument is anything
3319 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
3321 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
3322 && arg
== expr
->value
.function
.actual
3323 && arg
->next
!= NULL
&& arg
->next
->expr
)
3325 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
3328 if (arg
->next
->name
&& strcmp (arg
->next
->name
, "kind") == 0)
3331 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
3336 if (arg
->expr
!= NULL
3337 && arg
->expr
->rank
> 0
3338 && resolve_assumed_size_actual (arg
->expr
))
3344 need_full_assumed_size
= temp
;
3346 if (!check_pure_function(expr
))
3349 /* Functions without the RECURSIVE attribution are not allowed to
3350 * call themselves. */
3351 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
3354 esym
= expr
->value
.function
.esym
;
3356 if (is_illegal_recursion (esym
, gfc_current_ns
))
3358 if (esym
->attr
.entry
&& esym
->ns
->entries
)
3359 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3360 " function %qs is not RECURSIVE",
3361 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
3363 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3364 " is not RECURSIVE", esym
->name
, &expr
->where
);
3370 /* Character lengths of use associated functions may contains references to
3371 symbols not referenced from the current program unit otherwise. Make sure
3372 those symbols are marked as referenced. */
3374 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
3375 && expr
->value
.function
.esym
->attr
.use_assoc
)
3377 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
3380 /* Make sure that the expression has a typespec that works. */
3381 if (expr
->ts
.type
== BT_UNKNOWN
)
3383 if (expr
->symtree
->n
.sym
->result
3384 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
3385 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
3386 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
3389 if (!expr
->ref
&& !expr
->value
.function
.isym
)
3391 if (expr
->value
.function
.esym
)
3392 update_current_proc_array_outer_dependency (expr
->value
.function
.esym
);
3394 update_current_proc_array_outer_dependency (sym
);
3397 /* typebound procedure: Assume the worst. */
3398 gfc_current_ns
->proc_name
->attr
.array_outer_dependency
= 1;
3404 /************* Subroutine resolution *************/
3407 pure_subroutine (gfc_symbol
*sym
, const char *name
, locus
*loc
)
3414 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3418 else if (gfc_do_concurrent_flag
)
3420 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3424 else if (gfc_pure (NULL
))
3426 gfc_error ("Subroutine call to %qs at %L is not PURE", name
, loc
);
3430 gfc_unset_implicit_pure (NULL
);
3436 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3440 if (sym
->attr
.generic
)
3442 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
3445 c
->resolved_sym
= s
;
3446 if (!pure_subroutine (s
, s
->name
, &c
->loc
))
3451 /* TODO: Need to search for elemental references in generic interface. */
3454 if (sym
->attr
.intrinsic
)
3455 return gfc_intrinsic_sub_interface (c
, 0);
3462 resolve_generic_s (gfc_code
*c
)
3467 sym
= c
->symtree
->n
.sym
;
3471 m
= resolve_generic_s0 (c
, sym
);
3474 else if (m
== MATCH_ERROR
)
3478 if (sym
->ns
->parent
== NULL
)
3480 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3484 if (!generic_sym (sym
))
3488 /* Last ditch attempt. See if the reference is to an intrinsic
3489 that possesses a matching interface. 14.1.2.4 */
3490 sym
= c
->symtree
->n
.sym
;
3492 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
3494 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3495 sym
->name
, &c
->loc
);
3499 m
= gfc_intrinsic_sub_interface (c
, 0);
3503 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3504 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
3510 /* Resolve a subroutine call known to be specific. */
3513 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3517 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
3519 if (sym
->attr
.dummy
)
3521 sym
->attr
.proc
= PROC_DUMMY
;
3525 sym
->attr
.proc
= PROC_EXTERNAL
;
3529 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
3532 if (sym
->attr
.intrinsic
)
3534 m
= gfc_intrinsic_sub_interface (c
, 1);
3538 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3539 "with an intrinsic", sym
->name
, &c
->loc
);
3547 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3549 c
->resolved_sym
= sym
;
3550 if (!pure_subroutine (sym
, sym
->name
, &c
->loc
))
3558 resolve_specific_s (gfc_code
*c
)
3563 sym
= c
->symtree
->n
.sym
;
3567 m
= resolve_specific_s0 (c
, sym
);
3570 if (m
== MATCH_ERROR
)
3573 if (sym
->ns
->parent
== NULL
)
3576 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3582 sym
= c
->symtree
->n
.sym
;
3583 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3584 sym
->name
, &c
->loc
);
3590 /* Resolve a subroutine call not known to be generic nor specific. */
3593 resolve_unknown_s (gfc_code
*c
)
3597 sym
= c
->symtree
->n
.sym
;
3599 if (sym
->attr
.dummy
)
3601 sym
->attr
.proc
= PROC_DUMMY
;
3605 /* See if we have an intrinsic function reference. */
3607 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3609 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3614 /* The reference is to an external name. */
3617 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3619 c
->resolved_sym
= sym
;
3621 return pure_subroutine (sym
, sym
->name
, &c
->loc
);
3625 /* Resolve a subroutine call. Although it was tempting to use the same code
3626 for functions, subroutines and functions are stored differently and this
3627 makes things awkward. */
3630 resolve_call (gfc_code
*c
)
3633 procedure_type ptype
= PROC_INTRINSIC
;
3634 gfc_symbol
*csym
, *sym
;
3635 bool no_formal_args
;
3637 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3639 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3641 gfc_error ("%qs at %L has a type, which is not consistent with "
3642 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3646 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3649 gfc_find_sym_tree (c
->symtree
->name
, gfc_current_ns
, 1, &st
);
3650 sym
= st
? st
->n
.sym
: NULL
;
3651 if (sym
&& csym
!= sym
3652 && sym
->ns
== gfc_current_ns
3653 && sym
->attr
.flavor
== FL_PROCEDURE
3654 && sym
->attr
.contained
)
3657 if (csym
->attr
.generic
)
3658 c
->symtree
->n
.sym
= sym
;
3661 csym
= c
->symtree
->n
.sym
;
3665 /* If this ia a deferred TBP, c->expr1 will be set. */
3666 if (!c
->expr1
&& csym
)
3668 if (csym
->attr
.abstract
)
3670 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3671 csym
->name
, &c
->loc
);
3675 /* Subroutines without the RECURSIVE attribution are not allowed to
3677 if (is_illegal_recursion (csym
, gfc_current_ns
))
3679 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3680 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3681 "as subroutine %qs is not RECURSIVE",
3682 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3684 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3685 "as it is not RECURSIVE", csym
->name
, &c
->loc
);
3691 /* Switch off assumed size checking and do this again for certain kinds
3692 of procedure, once the procedure itself is resolved. */
3693 need_full_assumed_size
++;
3696 ptype
= csym
->attr
.proc
;
3698 no_formal_args
= csym
&& is_external_proc (csym
)
3699 && gfc_sym_get_dummy_args (csym
) == NULL
;
3700 if (!resolve_actual_arglist (c
->ext
.actual
, ptype
, no_formal_args
))
3703 /* Resume assumed_size checking. */
3704 need_full_assumed_size
--;
3706 /* If external, check for usage. */
3707 if (csym
&& is_external_proc (csym
))
3708 resolve_global_procedure (csym
, &c
->loc
, 1);
3711 if (c
->resolved_sym
== NULL
)
3713 c
->resolved_isym
= NULL
;
3714 switch (procedure_kind (csym
))
3717 t
= resolve_generic_s (c
);
3720 case PTYPE_SPECIFIC
:
3721 t
= resolve_specific_s (c
);
3725 t
= resolve_unknown_s (c
);
3729 gfc_internal_error ("resolve_subroutine(): bad function type");
3733 /* Some checks of elemental subroutine actual arguments. */
3734 if (!resolve_elemental_actual (NULL
, c
))
3738 update_current_proc_array_outer_dependency (csym
);
3740 /* Typebound procedure: Assume the worst. */
3741 gfc_current_ns
->proc_name
->attr
.array_outer_dependency
= 1;
3747 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3748 op1->shape and op2->shape are non-NULL return true if their shapes
3749 match. If both op1->shape and op2->shape are non-NULL return false
3750 if their shapes do not match. If either op1->shape or op2->shape is
3751 NULL, return true. */
3754 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3761 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3763 for (i
= 0; i
< op1
->rank
; i
++)
3765 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3767 gfc_error ("Shapes for operands at %L and %L are not conformable",
3768 &op1
->where
, &op2
->where
);
3778 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3779 For example A .AND. B becomes IAND(A, B). */
3781 logical_to_bitwise (gfc_expr
*e
)
3783 gfc_expr
*tmp
, *op1
, *op2
;
3785 gfc_actual_arglist
*args
= NULL
;
3787 gcc_assert (e
->expr_type
== EXPR_OP
);
3789 isym
= GFC_ISYM_NONE
;
3790 op1
= e
->value
.op
.op1
;
3791 op2
= e
->value
.op
.op2
;
3793 switch (e
->value
.op
.op
)
3796 isym
= GFC_ISYM_NOT
;
3799 isym
= GFC_ISYM_IAND
;
3802 isym
= GFC_ISYM_IOR
;
3804 case INTRINSIC_NEQV
:
3805 isym
= GFC_ISYM_IEOR
;
3808 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3809 Change the old expression to NEQV, which will get replaced by IEOR,
3810 and wrap it in NOT. */
3811 tmp
= gfc_copy_expr (e
);
3812 tmp
->value
.op
.op
= INTRINSIC_NEQV
;
3813 tmp
= logical_to_bitwise (tmp
);
3814 isym
= GFC_ISYM_NOT
;
3819 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3822 /* Inherit the original operation's operands as arguments. */
3823 args
= gfc_get_actual_arglist ();
3827 args
->next
= gfc_get_actual_arglist ();
3828 args
->next
->expr
= op2
;
3831 /* Convert the expression to a function call. */
3832 e
->expr_type
= EXPR_FUNCTION
;
3833 e
->value
.function
.actual
= args
;
3834 e
->value
.function
.isym
= gfc_intrinsic_function_by_id (isym
);
3835 e
->value
.function
.name
= e
->value
.function
.isym
->name
;
3836 e
->value
.function
.esym
= NULL
;
3838 /* Make up a pre-resolved function call symtree if we need to. */
3839 if (!e
->symtree
|| !e
->symtree
->n
.sym
)
3842 gfc_get_ha_sym_tree (e
->value
.function
.isym
->name
, &e
->symtree
);
3843 sym
= e
->symtree
->n
.sym
;
3845 sym
->attr
.flavor
= FL_PROCEDURE
;
3846 sym
->attr
.function
= 1;
3847 sym
->attr
.elemental
= 1;
3849 sym
->attr
.referenced
= 1;
3850 gfc_intrinsic_symbol (sym
);
3851 gfc_commit_symbol (sym
);
3854 args
->name
= e
->value
.function
.isym
->formal
->name
;
3855 if (e
->value
.function
.isym
->formal
->next
)
3856 args
->next
->name
= e
->value
.function
.isym
->formal
->next
->name
;
3861 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3862 candidates in CANDIDATES_LEN. */
3864 lookup_uop_fuzzy_find_candidates (gfc_symtree
*uop
,
3866 size_t &candidates_len
)
3873 /* Not sure how to properly filter here. Use all for a start.
3874 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3875 these as i suppose they don't make terribly sense. */
3877 if (uop
->n
.uop
->op
!= NULL
)
3878 vec_push (candidates
, candidates_len
, uop
->name
);
3882 lookup_uop_fuzzy_find_candidates (p
, candidates
, candidates_len
);
3886 lookup_uop_fuzzy_find_candidates (p
, candidates
, candidates_len
);
3889 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3892 lookup_uop_fuzzy (const char *op
, gfc_symtree
*uop
)
3894 char **candidates
= NULL
;
3895 size_t candidates_len
= 0;
3896 lookup_uop_fuzzy_find_candidates (uop
, candidates
, candidates_len
);
3897 return gfc_closest_fuzzy_match (op
, candidates
);
3901 /* Callback finding an impure function as an operand to an .and. or
3902 .or. expression. Remember the last function warned about to
3903 avoid double warnings when recursing. */
3906 impure_function_callback (gfc_expr
**e
, int *walk_subtrees ATTRIBUTE_UNUSED
,
3911 static gfc_expr
*last
= NULL
;
3912 bool *found
= (bool *) data
;
3914 if (f
->expr_type
== EXPR_FUNCTION
)
3917 if (f
!= last
&& !gfc_pure_function (f
, &name
)
3918 && !gfc_implicit_pure_function (f
))
3921 gfc_warning (OPT_Wfunction_elimination
,
3922 "Impure function %qs at %L might not be evaluated",
3925 gfc_warning (OPT_Wfunction_elimination
,
3926 "Impure function at %L might not be evaluated",
3935 /* Return true if TYPE is character based, false otherwise. */
3938 is_character_based (bt type
)
3940 return type
== BT_CHARACTER
|| type
== BT_HOLLERITH
;
3944 /* If expression is a hollerith, convert it to character and issue a warning
3945 for the conversion. */
3948 convert_hollerith_to_character (gfc_expr
*e
)
3950 if (e
->ts
.type
== BT_HOLLERITH
)
3954 t
.type
= BT_CHARACTER
;
3955 t
.kind
= e
->ts
.kind
;
3956 gfc_convert_type_warn (e
, &t
, 2, 1);
3960 /* Convert to numeric and issue a warning for the conversion. */
3963 convert_to_numeric (gfc_expr
*a
, gfc_expr
*b
)
3967 t
.type
= b
->ts
.type
;
3968 t
.kind
= b
->ts
.kind
;
3969 gfc_convert_type_warn (a
, &t
, 2, 1);
3972 /* Resolve an operator expression node. This can involve replacing the
3973 operation with a user defined function call. */
3976 resolve_operator (gfc_expr
*e
)
3978 gfc_expr
*op1
, *op2
;
3980 bool dual_locus_error
;
3983 /* Resolve all subnodes-- give them types. */
3985 switch (e
->value
.op
.op
)
3988 if (!gfc_resolve_expr (e
->value
.op
.op2
))
3994 case INTRINSIC_UPLUS
:
3995 case INTRINSIC_UMINUS
:
3996 case INTRINSIC_PARENTHESES
:
3997 if (!gfc_resolve_expr (e
->value
.op
.op1
))
4000 && e
->value
.op
.op1
->ts
.type
== BT_BOZ
&& !e
->value
.op
.op2
)
4002 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4003 "unary operator %qs", &e
->value
.op
.op1
->where
,
4004 gfc_op2string (e
->value
.op
.op
));
4010 /* Typecheck the new node. */
4012 op1
= e
->value
.op
.op1
;
4013 op2
= e
->value
.op
.op2
;
4014 if (op1
== NULL
&& op2
== NULL
)
4017 dual_locus_error
= false;
4019 /* op1 and op2 cannot both be BOZ. */
4020 if (op1
&& op1
->ts
.type
== BT_BOZ
4021 && op2
&& op2
->ts
.type
== BT_BOZ
)
4023 gfc_error ("Operands at %L and %L cannot appear as operands of "
4024 "binary operator %qs", &op1
->where
, &op2
->where
,
4025 gfc_op2string (e
->value
.op
.op
));
4029 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
4030 || (op2
&& op2
->expr_type
== EXPR_NULL
))
4032 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
4036 switch (e
->value
.op
.op
)
4038 case INTRINSIC_UPLUS
:
4039 case INTRINSIC_UMINUS
:
4040 if (op1
->ts
.type
== BT_INTEGER
4041 || op1
->ts
.type
== BT_REAL
4042 || op1
->ts
.type
== BT_COMPLEX
)
4048 sprintf (msg
, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4049 gfc_op2string (e
->value
.op
.op
), gfc_typename (e
));
4052 case INTRINSIC_PLUS
:
4053 case INTRINSIC_MINUS
:
4054 case INTRINSIC_TIMES
:
4055 case INTRINSIC_DIVIDE
:
4056 case INTRINSIC_POWER
:
4057 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
4059 gfc_type_convert_binary (e
, 1);
4063 if (op1
->ts
.type
== BT_DERIVED
|| op2
->ts
.type
== BT_DERIVED
)
4065 _("Unexpected derived-type entities in binary intrinsic "
4066 "numeric operator %%<%s%%> at %%L"),
4067 gfc_op2string (e
->value
.op
.op
));
4070 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4071 gfc_op2string (e
->value
.op
.op
), gfc_typename (op1
),
4072 gfc_typename (op2
));
4075 case INTRINSIC_CONCAT
:
4076 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
4077 && op1
->ts
.kind
== op2
->ts
.kind
)
4079 e
->ts
.type
= BT_CHARACTER
;
4080 e
->ts
.kind
= op1
->ts
.kind
;
4085 _("Operands of string concatenation operator at %%L are %s/%s"),
4086 gfc_typename (op1
), gfc_typename (op2
));
4092 case INTRINSIC_NEQV
:
4093 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
4095 e
->ts
.type
= BT_LOGICAL
;
4096 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
4097 if (op1
->ts
.kind
< e
->ts
.kind
)
4098 gfc_convert_type (op1
, &e
->ts
, 2);
4099 else if (op2
->ts
.kind
< e
->ts
.kind
)
4100 gfc_convert_type (op2
, &e
->ts
, 2);
4102 if (flag_frontend_optimize
&&
4103 (e
->value
.op
.op
== INTRINSIC_AND
|| e
->value
.op
.op
== INTRINSIC_OR
))
4105 /* Warn about short-circuiting
4106 with impure function as second operand. */
4108 gfc_expr_walker (&op2
, impure_function_callback
, &op2_f
);
4113 /* Logical ops on integers become bitwise ops with -fdec. */
4115 && (op1
->ts
.type
== BT_INTEGER
|| op2
->ts
.type
== BT_INTEGER
))
4117 e
->ts
.type
= BT_INTEGER
;
4118 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
4119 if (op1
->ts
.type
!= e
->ts
.type
|| op1
->ts
.kind
!= e
->ts
.kind
)
4120 gfc_convert_type (op1
, &e
->ts
, 1);
4121 if (op2
->ts
.type
!= e
->ts
.type
|| op2
->ts
.kind
!= e
->ts
.kind
)
4122 gfc_convert_type (op2
, &e
->ts
, 1);
4123 e
= logical_to_bitwise (e
);
4127 sprintf (msg
, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4128 gfc_op2string (e
->value
.op
.op
), gfc_typename (op1
),
4129 gfc_typename (op2
));
4134 /* Logical ops on integers become bitwise ops with -fdec. */
4135 if (flag_dec
&& op1
->ts
.type
== BT_INTEGER
)
4137 e
->ts
.type
= BT_INTEGER
;
4138 e
->ts
.kind
= op1
->ts
.kind
;
4139 e
= logical_to_bitwise (e
);
4143 if (op1
->ts
.type
== BT_LOGICAL
)
4145 e
->ts
.type
= BT_LOGICAL
;
4146 e
->ts
.kind
= op1
->ts
.kind
;
4150 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
4151 gfc_typename (op1
));
4155 case INTRINSIC_GT_OS
:
4157 case INTRINSIC_GE_OS
:
4159 case INTRINSIC_LT_OS
:
4161 case INTRINSIC_LE_OS
:
4162 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
4164 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
4171 case INTRINSIC_EQ_OS
:
4173 case INTRINSIC_NE_OS
:
4176 && is_character_based (op1
->ts
.type
)
4177 && is_character_based (op2
->ts
.type
))
4179 convert_hollerith_to_character (op1
);
4180 convert_hollerith_to_character (op2
);
4183 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
4184 && op1
->ts
.kind
== op2
->ts
.kind
)
4186 e
->ts
.type
= BT_LOGICAL
;
4187 e
->ts
.kind
= gfc_default_logical_kind
;
4191 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4192 if (op1
->ts
.type
== BT_BOZ
)
4194 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4195 "as an operand of a relational operator"),
4199 if (op2
->ts
.type
== BT_INTEGER
&& !gfc_boz2int (op1
, op2
->ts
.kind
))
4202 if (op2
->ts
.type
== BT_REAL
&& !gfc_boz2real (op1
, op2
->ts
.kind
))
4206 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4207 if (op2
->ts
.type
== BT_BOZ
)
4209 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4210 " as an operand of a relational operator"),
4214 if (op1
->ts
.type
== BT_INTEGER
&& !gfc_boz2int (op2
, op1
->ts
.kind
))
4217 if (op1
->ts
.type
== BT_REAL
&& !gfc_boz2real (op2
, op1
->ts
.kind
))
4221 && op1
->ts
.type
== BT_HOLLERITH
&& gfc_numeric_ts (&op2
->ts
))
4222 convert_to_numeric (op1
, op2
);
4225 && gfc_numeric_ts (&op1
->ts
) && op2
->ts
.type
== BT_HOLLERITH
)
4226 convert_to_numeric (op2
, op1
);
4228 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
4230 gfc_type_convert_binary (e
, 1);
4232 e
->ts
.type
= BT_LOGICAL
;
4233 e
->ts
.kind
= gfc_default_logical_kind
;
4235 if (warn_compare_reals
)
4237 gfc_intrinsic_op op
= e
->value
.op
.op
;
4239 /* Type conversion has made sure that the types of op1 and op2
4240 agree, so it is only necessary to check the first one. */
4241 if ((op1
->ts
.type
== BT_REAL
|| op1
->ts
.type
== BT_COMPLEX
)
4242 && (op
== INTRINSIC_EQ
|| op
== INTRINSIC_EQ_OS
4243 || op
== INTRINSIC_NE
|| op
== INTRINSIC_NE_OS
))
4247 if (op
== INTRINSIC_EQ
|| op
== INTRINSIC_EQ_OS
)
4248 msg
= G_("Equality comparison for %s at %L");
4250 msg
= G_("Inequality comparison for %s at %L");
4252 gfc_warning (OPT_Wcompare_reals
, msg
,
4253 gfc_typename (op1
), &op1
->where
);
4260 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
4262 _("Logicals at %%L must be compared with %s instead of %s"),
4263 (e
->value
.op
.op
== INTRINSIC_EQ
4264 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
4265 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
4268 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4269 gfc_op2string (e
->value
.op
.op
), gfc_typename (op1
),
4270 gfc_typename (op2
));
4274 case INTRINSIC_USER
:
4275 if (e
->value
.op
.uop
->op
== NULL
)
4277 const char *name
= e
->value
.op
.uop
->name
;
4278 const char *guessed
;
4279 guessed
= lookup_uop_fuzzy (name
, e
->value
.op
.uop
->ns
->uop_root
);
4281 sprintf (msg
, _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4284 sprintf (msg
, _("Unknown operator %%<%s%%> at %%L"), name
);
4286 else if (op2
== NULL
)
4287 sprintf (msg
, _("Operand of user operator %%<%s%%> at %%L is %s"),
4288 e
->value
.op
.uop
->name
, gfc_typename (op1
));
4291 sprintf (msg
, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4292 e
->value
.op
.uop
->name
, gfc_typename (op1
),
4293 gfc_typename (op2
));
4294 e
->value
.op
.uop
->op
->sym
->attr
.referenced
= 1;
4299 case INTRINSIC_PARENTHESES
:
4301 if (e
->ts
.type
== BT_CHARACTER
)
4302 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
4306 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4309 /* Deal with arrayness of an operand through an operator. */
4311 switch (e
->value
.op
.op
)
4313 case INTRINSIC_PLUS
:
4314 case INTRINSIC_MINUS
:
4315 case INTRINSIC_TIMES
:
4316 case INTRINSIC_DIVIDE
:
4317 case INTRINSIC_POWER
:
4318 case INTRINSIC_CONCAT
:
4322 case INTRINSIC_NEQV
:
4324 case INTRINSIC_EQ_OS
:
4326 case INTRINSIC_NE_OS
:
4328 case INTRINSIC_GT_OS
:
4330 case INTRINSIC_GE_OS
:
4332 case INTRINSIC_LT_OS
:
4334 case INTRINSIC_LE_OS
:
4336 if (op1
->rank
== 0 && op2
->rank
== 0)
4339 if (op1
->rank
== 0 && op2
->rank
!= 0)
4341 e
->rank
= op2
->rank
;
4343 if (e
->shape
== NULL
)
4344 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
4347 if (op1
->rank
!= 0 && op2
->rank
== 0)
4349 e
->rank
= op1
->rank
;
4351 if (e
->shape
== NULL
)
4352 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
4355 if (op1
->rank
!= 0 && op2
->rank
!= 0)
4357 if (op1
->rank
== op2
->rank
)
4359 e
->rank
= op1
->rank
;
4360 if (e
->shape
== NULL
)
4362 t
= compare_shapes (op1
, op2
);
4366 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
4371 /* Allow higher level expressions to work. */
4374 /* Try user-defined operators, and otherwise throw an error. */
4375 dual_locus_error
= true;
4377 _("Inconsistent ranks for operator at %%L and %%L"));
4384 case INTRINSIC_PARENTHESES
:
4386 case INTRINSIC_UPLUS
:
4387 case INTRINSIC_UMINUS
:
4388 /* Simply copy arrayness attribute */
4389 e
->rank
= op1
->rank
;
4391 if (e
->shape
== NULL
)
4392 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
4402 /* Attempt to simplify the expression. */
4405 t
= gfc_simplify_expr (e
, 0);
4406 /* Some calls do not succeed in simplification and return false
4407 even though there is no error; e.g. variable references to
4408 PARAMETER arrays. */
4409 if (!gfc_is_constant_expr (e
))
4417 match m
= gfc_extend_expr (e
);
4420 if (m
== MATCH_ERROR
)
4424 if (dual_locus_error
)
4425 gfc_error (msg
, &op1
->where
, &op2
->where
);
4427 gfc_error (msg
, &e
->where
);
4433 /************** Array resolution subroutines **************/
4436 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
};
4438 /* Compare two integer expressions. */
4440 static compare_result
4441 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
4445 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
4446 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
4449 /* If either of the types isn't INTEGER, we must have
4450 raised an error earlier. */
4452 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
4455 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
4465 /* Compare an integer expression with an integer. */
4467 static compare_result
4468 compare_bound_int (gfc_expr
*a
, int b
)
4472 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4475 if (a
->ts
.type
!= BT_INTEGER
)
4476 gfc_internal_error ("compare_bound_int(): Bad expression");
4478 i
= mpz_cmp_si (a
->value
.integer
, b
);
4488 /* Compare an integer expression with a mpz_t. */
4490 static compare_result
4491 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
4495 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
4498 if (a
->ts
.type
!= BT_INTEGER
)
4499 gfc_internal_error ("compare_bound_int(): Bad expression");
4501 i
= mpz_cmp (a
->value
.integer
, b
);
4511 /* Compute the last value of a sequence given by a triplet.
4512 Return 0 if it wasn't able to compute the last value, or if the
4513 sequence if empty, and 1 otherwise. */
4516 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
4517 gfc_expr
*stride
, mpz_t last
)
4521 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
4522 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
4523 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
4526 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
4527 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
4530 if (stride
== NULL
|| compare_bound_int (stride
, 1) == CMP_EQ
)
4532 if (compare_bound (start
, end
) == CMP_GT
)
4534 mpz_set (last
, end
->value
.integer
);
4538 if (compare_bound_int (stride
, 0) == CMP_GT
)
4540 /* Stride is positive */
4541 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
4546 /* Stride is negative */
4547 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
4552 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
4553 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
4554 mpz_sub (last
, end
->value
.integer
, rem
);
4561 /* Compare a single dimension of an array reference to the array
4565 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
4569 if (ar
->dimen_type
[i
] == DIMEN_STAR
)
4571 gcc_assert (ar
->stride
[i
] == NULL
);
4572 /* This implies [*] as [*:] and [*:3] are not possible. */
4573 if (ar
->start
[i
] == NULL
)
4575 gcc_assert (ar
->end
[i
] == NULL
);
4580 /* Given start, end and stride values, calculate the minimum and
4581 maximum referenced indexes. */
4583 switch (ar
->dimen_type
[i
])
4586 case DIMEN_THIS_IMAGE
:
4591 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
4594 gfc_warning (0, "Array reference at %L is out of bounds "
4595 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4596 mpz_get_si (ar
->start
[i
]->value
.integer
),
4597 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4599 gfc_warning (0, "Array reference at %L is out of bounds "
4600 "(%ld < %ld) in codimension %d", &ar
->c_where
[i
],
4601 mpz_get_si (ar
->start
[i
]->value
.integer
),
4602 mpz_get_si (as
->lower
[i
]->value
.integer
),
4606 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
4609 gfc_warning (0, "Array reference at %L is out of bounds "
4610 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4611 mpz_get_si (ar
->start
[i
]->value
.integer
),
4612 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4614 gfc_warning (0, "Array reference at %L is out of bounds "
4615 "(%ld > %ld) in codimension %d", &ar
->c_where
[i
],
4616 mpz_get_si (ar
->start
[i
]->value
.integer
),
4617 mpz_get_si (as
->upper
[i
]->value
.integer
),
4626 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4627 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4629 compare_result comp_start_end
= compare_bound (AR_START
, AR_END
);
4631 /* Check for zero stride, which is not allowed. */
4632 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
4634 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
4638 /* if start == len || (stride > 0 && start < len)
4639 || (stride < 0 && start > len),
4640 then the array section contains at least one element. In this
4641 case, there is an out-of-bounds access if
4642 (start < lower || start > upper). */
4643 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
4644 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
4645 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
4646 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
4647 && comp_start_end
== CMP_GT
))
4649 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
4651 gfc_warning (0, "Lower array reference at %L is out of bounds "
4652 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4653 mpz_get_si (AR_START
->value
.integer
),
4654 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4657 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
4659 gfc_warning (0, "Lower array reference at %L is out of bounds "
4660 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4661 mpz_get_si (AR_START
->value
.integer
),
4662 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4667 /* If we can compute the highest index of the array section,
4668 then it also has to be between lower and upper. */
4669 mpz_init (last_value
);
4670 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
4673 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
4675 gfc_warning (0, "Upper array reference at %L is out of bounds "
4676 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
4677 mpz_get_si (last_value
),
4678 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
4679 mpz_clear (last_value
);
4682 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
4684 gfc_warning (0, "Upper array reference at %L is out of bounds "
4685 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
4686 mpz_get_si (last_value
),
4687 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
4688 mpz_clear (last_value
);
4692 mpz_clear (last_value
);
4700 gfc_internal_error ("check_dimension(): Bad array reference");
4707 /* Compare an array reference with an array specification. */
4710 compare_spec_to_ref (gfc_array_ref
*ar
)
4717 /* TODO: Full array sections are only allowed as actual parameters. */
4718 if (as
->type
== AS_ASSUMED_SIZE
4719 && (/*ar->type == AR_FULL
4720 ||*/ (ar
->type
== AR_SECTION
4721 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
4723 gfc_error ("Rightmost upper bound of assumed size array section "
4724 "not specified at %L", &ar
->where
);
4728 if (ar
->type
== AR_FULL
)
4731 if (as
->rank
!= ar
->dimen
)
4733 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4734 &ar
->where
, ar
->dimen
, as
->rank
);
4738 /* ar->codimen == 0 is a local array. */
4739 if (as
->corank
!= ar
->codimen
&& ar
->codimen
!= 0)
4741 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4742 &ar
->where
, ar
->codimen
, as
->corank
);
4746 for (i
= 0; i
< as
->rank
; i
++)
4747 if (!check_dimension (i
, ar
, as
))
4750 /* Local access has no coarray spec. */
4751 if (ar
->codimen
!= 0)
4752 for (i
= as
->rank
; i
< as
->rank
+ as
->corank
; i
++)
4754 if (ar
->dimen_type
[i
] != DIMEN_ELEMENT
&& !ar
->in_allocate
4755 && ar
->dimen_type
[i
] != DIMEN_THIS_IMAGE
)
4757 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4758 i
+ 1 - as
->rank
, &ar
->where
);
4761 if (!check_dimension (i
, ar
, as
))
4769 /* Resolve one part of an array index. */
4772 gfc_resolve_index_1 (gfc_expr
*index
, int check_scalar
,
4773 int force_index_integer_kind
)
4780 if (!gfc_resolve_expr (index
))
4783 if (check_scalar
&& index
->rank
!= 0)
4785 gfc_error ("Array index at %L must be scalar", &index
->where
);
4789 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
4791 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4792 &index
->where
, gfc_basic_typename (index
->ts
.type
));
4796 if (index
->ts
.type
== BT_REAL
)
4797 if (!gfc_notify_std (GFC_STD_LEGACY
, "REAL array index at %L",
4801 if ((index
->ts
.kind
!= gfc_index_integer_kind
4802 && force_index_integer_kind
)
4803 || index
->ts
.type
!= BT_INTEGER
)
4806 ts
.type
= BT_INTEGER
;
4807 ts
.kind
= gfc_index_integer_kind
;
4809 gfc_convert_type_warn (index
, &ts
, 2, 0);
4815 /* Resolve one part of an array index. */
4818 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
4820 return gfc_resolve_index_1 (index
, check_scalar
, 1);
4823 /* Resolve a dim argument to an intrinsic function. */
4826 gfc_resolve_dim_arg (gfc_expr
*dim
)
4831 if (!gfc_resolve_expr (dim
))
4836 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
4841 if (dim
->ts
.type
!= BT_INTEGER
)
4843 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
4847 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
4852 ts
.type
= BT_INTEGER
;
4853 ts
.kind
= gfc_index_integer_kind
;
4855 gfc_convert_type_warn (dim
, &ts
, 2, 0);
4861 /* Given an expression that contains array references, update those array
4862 references to point to the right array specifications. While this is
4863 filled in during matching, this information is difficult to save and load
4864 in a module, so we take care of it here.
4866 The idea here is that the original array reference comes from the
4867 base symbol. We traverse the list of reference structures, setting
4868 the stored reference to references. Component references can
4869 provide an additional array specification. */
4872 find_array_spec (gfc_expr
*e
)
4877 bool class_as
= false;
4879 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
4881 as
= CLASS_DATA (e
->symtree
->n
.sym
)->as
;
4885 as
= e
->symtree
->n
.sym
->as
;
4887 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4892 gfc_internal_error ("find_array_spec(): Missing spec");
4899 c
= ref
->u
.c
.component
;
4900 if (c
->attr
.dimension
)
4902 if (as
!= NULL
&& !(class_as
&& as
== c
->as
))
4903 gfc_internal_error ("find_array_spec(): unused as(1)");
4915 gfc_internal_error ("find_array_spec(): unused as(2)");
4919 /* Resolve an array reference. */
4922 resolve_array_ref (gfc_array_ref
*ar
)
4924 int i
, check_scalar
;
4927 for (i
= 0; i
< ar
->dimen
+ ar
->codimen
; i
++)
4929 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4931 /* Do not force gfc_index_integer_kind for the start. We can
4932 do fine with any integer kind. This avoids temporary arrays
4933 created for indexing with a vector. */
4934 if (!gfc_resolve_index_1 (ar
->start
[i
], check_scalar
, 0))
4936 if (!gfc_resolve_index (ar
->end
[i
], check_scalar
))
4938 if (!gfc_resolve_index (ar
->stride
[i
], check_scalar
))
4943 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4947 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4951 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4952 if (e
->expr_type
== EXPR_VARIABLE
4953 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4954 ar
->start
[i
] = gfc_get_parentheses (e
);
4958 gfc_error ("Array index at %L is an array of rank %d",
4959 &ar
->c_where
[i
], e
->rank
);
4963 /* Fill in the upper bound, which may be lower than the
4964 specified one for something like a(2:10:5), which is
4965 identical to a(2:7:5). Only relevant for strides not equal
4966 to one. Don't try a division by zero. */
4967 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4968 && ar
->stride
[i
] != NULL
&& ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
4969 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1L) != 0
4970 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 0L) != 0)
4974 if (gfc_ref_dimen_size (ar
, i
, &size
, &end
))
4976 if (ar
->end
[i
] == NULL
)
4979 gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
4981 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4983 else if (ar
->end
[i
]->ts
.type
== BT_INTEGER
4984 && ar
->end
[i
]->expr_type
== EXPR_CONSTANT
)
4986 mpz_set (ar
->end
[i
]->value
.integer
, end
);
4997 if (ar
->type
== AR_FULL
)
4999 if (ar
->as
->rank
== 0)
5000 ar
->type
= AR_ELEMENT
;
5002 /* Make sure array is the same as array(:,:), this way
5003 we don't need to special case all the time. */
5004 ar
->dimen
= ar
->as
->rank
;
5005 for (i
= 0; i
< ar
->dimen
; i
++)
5007 ar
->dimen_type
[i
] = DIMEN_RANGE
;
5009 gcc_assert (ar
->start
[i
] == NULL
);
5010 gcc_assert (ar
->end
[i
] == NULL
);
5011 gcc_assert (ar
->stride
[i
] == NULL
);
5015 /* If the reference type is unknown, figure out what kind it is. */
5017 if (ar
->type
== AR_UNKNOWN
)
5019 ar
->type
= AR_ELEMENT
;
5020 for (i
= 0; i
< ar
->dimen
; i
++)
5021 if (ar
->dimen_type
[i
] == DIMEN_RANGE
5022 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
5024 ar
->type
= AR_SECTION
;
5029 if (!ar
->as
->cray_pointee
&& !compare_spec_to_ref (ar
))
5032 if (ar
->as
->corank
&& ar
->codimen
== 0)
5035 ar
->codimen
= ar
->as
->corank
;
5036 for (n
= ar
->dimen
; n
< ar
->dimen
+ ar
->codimen
; n
++)
5037 ar
->dimen_type
[n
] = DIMEN_THIS_IMAGE
;
5045 resolve_substring (gfc_ref
*ref
, bool *equal_length
)
5047 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
5049 if (ref
->u
.ss
.start
!= NULL
)
5051 if (!gfc_resolve_expr (ref
->u
.ss
.start
))
5054 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
5056 gfc_error ("Substring start index at %L must be of type INTEGER",
5057 &ref
->u
.ss
.start
->where
);
5061 if (ref
->u
.ss
.start
->rank
!= 0)
5063 gfc_error ("Substring start index at %L must be scalar",
5064 &ref
->u
.ss
.start
->where
);
5068 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
5069 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
5070 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
5072 gfc_error ("Substring start index at %L is less than one",
5073 &ref
->u
.ss
.start
->where
);
5078 if (ref
->u
.ss
.end
!= NULL
)
5080 if (!gfc_resolve_expr (ref
->u
.ss
.end
))
5083 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
5085 gfc_error ("Substring end index at %L must be of type INTEGER",
5086 &ref
->u
.ss
.end
->where
);
5090 if (ref
->u
.ss
.end
->rank
!= 0)
5092 gfc_error ("Substring end index at %L must be scalar",
5093 &ref
->u
.ss
.end
->where
);
5097 if (ref
->u
.ss
.length
!= NULL
5098 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
5099 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
5100 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
5102 gfc_error ("Substring end index at %L exceeds the string length",
5103 &ref
->u
.ss
.start
->where
);
5107 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
5108 gfc_integer_kinds
[k
].huge
) == CMP_GT
5109 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
5110 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
5112 gfc_error ("Substring end index at %L is too large",
5113 &ref
->u
.ss
.end
->where
);
5116 /* If the substring has the same length as the original
5117 variable, the reference itself can be deleted. */
5119 if (ref
->u
.ss
.length
!= NULL
5120 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_EQ
5121 && compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_EQ
)
5122 *equal_length
= true;
5129 /* This function supplies missing substring charlens. */
5132 gfc_resolve_substring_charlen (gfc_expr
*e
)
5135 gfc_expr
*start
, *end
;
5136 gfc_typespec
*ts
= NULL
;
5139 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
5141 if (char_ref
->type
== REF_SUBSTRING
|| char_ref
->type
== REF_INQUIRY
)
5143 if (char_ref
->type
== REF_COMPONENT
)
5144 ts
= &char_ref
->u
.c
.component
->ts
;
5147 if (!char_ref
|| char_ref
->type
== REF_INQUIRY
)
5150 gcc_assert (char_ref
->next
== NULL
);
5154 if (e
->ts
.u
.cl
->length
)
5155 gfc_free_expr (e
->ts
.u
.cl
->length
);
5156 else if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
->attr
.dummy
)
5161 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
5163 if (char_ref
->u
.ss
.start
)
5164 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
5166 start
= gfc_get_int_expr (gfc_charlen_int_kind
, NULL
, 1);
5168 if (char_ref
->u
.ss
.end
)
5169 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
5170 else if (e
->expr_type
== EXPR_VARIABLE
)
5173 ts
= &e
->symtree
->n
.sym
->ts
;
5174 end
= gfc_copy_expr (ts
->u
.cl
->length
);
5181 gfc_free_expr (start
);
5182 gfc_free_expr (end
);
5186 /* Length = (end - start + 1).
5187 Check first whether it has a constant length. */
5188 if (gfc_dep_difference (end
, start
, &diff
))
5190 gfc_expr
*len
= gfc_get_constant_expr (BT_INTEGER
, gfc_charlen_int_kind
,
5193 mpz_add_ui (len
->value
.integer
, diff
, 1);
5195 e
->ts
.u
.cl
->length
= len
;
5196 /* The check for length < 0 is handled below */
5200 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
5201 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
,
5202 gfc_get_int_expr (gfc_charlen_int_kind
,
5206 /* F2008, 6.4.1: Both the starting point and the ending point shall
5207 be within the range 1, 2, ..., n unless the starting point exceeds
5208 the ending point, in which case the substring has length zero. */
5210 if (mpz_cmp_si (e
->ts
.u
.cl
->length
->value
.integer
, 0) < 0)
5211 mpz_set_si (e
->ts
.u
.cl
->length
->value
.integer
, 0);
5213 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
5214 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
5216 /* Make sure that the length is simplified. */
5217 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
5218 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
5222 /* Resolve subtype references. */
5225 gfc_resolve_ref (gfc_expr
*expr
)
5227 int current_part_dimension
, n_components
, seen_part_dimension
, dim
;
5228 gfc_ref
*ref
, **prev
, *array_ref
;
5231 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
5232 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
5234 find_array_spec (expr
);
5238 for (prev
= &expr
->ref
; *prev
!= NULL
;
5239 prev
= *prev
== NULL
? prev
: &(*prev
)->next
)
5240 switch ((*prev
)->type
)
5243 if (!resolve_array_ref (&(*prev
)->u
.ar
))
5252 equal_length
= false;
5253 if (!resolve_substring (*prev
, &equal_length
))
5256 if (expr
->expr_type
!= EXPR_SUBSTRING
&& equal_length
)
5258 /* Remove the reference and move the charlen, if any. */
5262 expr
->ts
.u
.cl
= ref
->u
.ss
.length
;
5263 ref
->u
.ss
.length
= NULL
;
5264 gfc_free_ref_list (ref
);
5269 /* Check constraints on part references. */
5271 current_part_dimension
= 0;
5272 seen_part_dimension
= 0;
5276 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
5282 switch (ref
->u
.ar
.type
)
5285 /* Coarray scalar. */
5286 if (ref
->u
.ar
.as
->rank
== 0)
5288 current_part_dimension
= 0;
5293 current_part_dimension
= 1;
5298 current_part_dimension
= 0;
5302 gfc_internal_error ("resolve_ref(): Bad array reference");
5308 if (current_part_dimension
|| seen_part_dimension
)
5311 if (ref
->u
.c
.component
->attr
.pointer
5312 || ref
->u
.c
.component
->attr
.proc_pointer
5313 || (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5314 && CLASS_DATA (ref
->u
.c
.component
)->attr
.pointer
))
5316 gfc_error ("Component to the right of a part reference "
5317 "with nonzero rank must not have the POINTER "
5318 "attribute at %L", &expr
->where
);
5321 else if (ref
->u
.c
.component
->attr
.allocatable
5322 || (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5323 && CLASS_DATA (ref
->u
.c
.component
)->attr
.allocatable
))
5326 gfc_error ("Component to the right of a part reference "
5327 "with nonzero rank must not have the ALLOCATABLE "
5328 "attribute at %L", &expr
->where
);
5340 /* Implement requirement in note 9.7 of F2018 that the result of the
5341 LEN inquiry be a scalar. */
5342 if (ref
->u
.i
== INQUIRY_LEN
&& array_ref
&& expr
->ts
.deferred
)
5344 array_ref
->u
.ar
.type
= AR_ELEMENT
;
5346 /* INQUIRY_LEN is not evaluated from the rest of the expr
5347 but directly from the string length. This means that setting
5348 the array indices to one does not matter but might trigger
5349 a runtime bounds error. Suppress the check. */
5350 expr
->no_bounds_check
= 1;
5351 for (dim
= 0; dim
< array_ref
->u
.ar
.dimen
; dim
++)
5353 array_ref
->u
.ar
.dimen_type
[dim
] = DIMEN_ELEMENT
;
5354 if (array_ref
->u
.ar
.start
[dim
])
5355 gfc_free_expr (array_ref
->u
.ar
.start
[dim
]);
5356 array_ref
->u
.ar
.start
[dim
]
5357 = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
5358 if (array_ref
->u
.ar
.end
[dim
])
5359 gfc_free_expr (array_ref
->u
.ar
.end
[dim
]);
5360 if (array_ref
->u
.ar
.stride
[dim
])
5361 gfc_free_expr (array_ref
->u
.ar
.stride
[dim
]);
5367 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
5368 || ref
->next
== NULL
)
5369 && current_part_dimension
5370 && seen_part_dimension
)
5372 gfc_error ("Two or more part references with nonzero rank must "
5373 "not be specified at %L", &expr
->where
);
5377 if (ref
->type
== REF_COMPONENT
)
5379 if (current_part_dimension
)
5380 seen_part_dimension
= 1;
5382 /* reset to make sure */
5383 current_part_dimension
= 0;
5391 /* Given an expression, determine its shape. This is easier than it sounds.
5392 Leaves the shape array NULL if it is not possible to determine the shape. */
5395 expression_shape (gfc_expr
*e
)
5397 mpz_t array
[GFC_MAX_DIMENSIONS
];
5400 if (e
->rank
<= 0 || e
->shape
!= NULL
)
5403 for (i
= 0; i
< e
->rank
; i
++)
5404 if (!gfc_array_dimen_size (e
, i
, &array
[i
]))
5407 e
->shape
= gfc_get_shape (e
->rank
);
5409 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
5414 for (i
--; i
>= 0; i
--)
5415 mpz_clear (array
[i
]);
5419 /* Given a variable expression node, compute the rank of the expression by
5420 examining the base symbol and any reference structures it may have. */
5423 gfc_expression_rank (gfc_expr
*e
)
5428 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5429 could lead to serious confusion... */
5430 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
5434 if (e
->expr_type
== EXPR_ARRAY
)
5436 /* Constructors can have a rank different from one via RESHAPE(). */
5438 e
->rank
= ((e
->symtree
== NULL
|| e
->symtree
->n
.sym
->as
== NULL
)
5439 ? 0 : e
->symtree
->n
.sym
->as
->rank
);
5445 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5447 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.proc_pointer
5448 && ref
->u
.c
.component
->attr
.function
&& !ref
->next
)
5449 rank
= ref
->u
.c
.component
->as
? ref
->u
.c
.component
->as
->rank
: 0;
5451 if (ref
->type
!= REF_ARRAY
)
5454 if (ref
->u
.ar
.type
== AR_FULL
)
5456 rank
= ref
->u
.ar
.as
->rank
;
5460 if (ref
->u
.ar
.type
== AR_SECTION
)
5462 /* Figure out the rank of the section. */
5464 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5466 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
5467 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
5468 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
5478 expression_shape (e
);
5483 add_caf_get_intrinsic (gfc_expr
*e
)
5485 gfc_expr
*wrapper
, *tmp_expr
;
5489 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5490 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5495 for (n
= ref
->u
.ar
.dimen
; n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
5496 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
5499 tmp_expr
= XCNEW (gfc_expr
);
5501 wrapper
= gfc_build_intrinsic_call (gfc_current_ns
, GFC_ISYM_CAF_GET
,
5502 "caf_get", tmp_expr
->where
, 1, tmp_expr
);
5503 wrapper
->ts
= e
->ts
;
5504 wrapper
->rank
= e
->rank
;
5506 wrapper
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
5513 remove_caf_get_intrinsic (gfc_expr
*e
)
5515 gcc_assert (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
5516 && e
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
);
5517 gfc_expr
*e2
= e
->value
.function
.actual
->expr
;
5518 e
->value
.function
.actual
->expr
= NULL
;
5519 gfc_free_actual_arglist (e
->value
.function
.actual
);
5520 gfc_free_shape (&e
->shape
, e
->rank
);
5526 /* Resolve a variable expression. */
5529 resolve_variable (gfc_expr
*e
)
5536 if (e
->symtree
== NULL
)
5538 sym
= e
->symtree
->n
.sym
;
5540 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5541 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5542 if (sym
->attr
.ext_attr
& (1 << EXT_ATTR_NO_ARG_CHECK
))
5544 if (!actual_arg
|| inquiry_argument
)
5546 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5547 "be used as actual argument", sym
->name
, &e
->where
);
5551 /* TS 29113, 407b. */
5552 else if (e
->ts
.type
== BT_ASSUMED
)
5556 gfc_error ("Assumed-type variable %s at %L may only be used "
5557 "as actual argument", sym
->name
, &e
->where
);
5560 else if (inquiry_argument
&& !first_actual_arg
)
5562 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5563 for all inquiry functions in resolve_function; the reason is
5564 that the function-name resolution happens too late in that
5566 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5567 "an inquiry function shall be the first argument",
5568 sym
->name
, &e
->where
);
5572 /* TS 29113, C535b. */
5573 else if (((sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
5574 && CLASS_DATA (sym
)->as
5575 && CLASS_DATA (sym
)->as
->type
== AS_ASSUMED_RANK
)
5576 || (sym
->ts
.type
!= BT_CLASS
&& sym
->as
5577 && sym
->as
->type
== AS_ASSUMED_RANK
))
5578 && !sym
->attr
.select_rank_temporary
)
5581 && !(cs_base
&& cs_base
->current
5582 && cs_base
->current
->op
== EXEC_SELECT_RANK
))
5584 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5585 "actual argument", sym
->name
, &e
->where
);
5588 else if (inquiry_argument
&& !first_actual_arg
)
5590 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5591 for all inquiry functions in resolve_function; the reason is
5592 that the function-name resolution happens too late in that
5594 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5595 "to an inquiry function shall be the first argument",
5596 sym
->name
, &e
->where
);
5601 if ((sym
->attr
.ext_attr
& (1 << EXT_ATTR_NO_ARG_CHECK
)) && e
->ref
5602 && !(e
->ref
->type
== REF_ARRAY
&& e
->ref
->u
.ar
.type
== AR_FULL
5603 && e
->ref
->next
== NULL
))
5605 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5606 "a subobject reference", sym
->name
, &e
->ref
->u
.ar
.where
);
5609 /* TS 29113, 407b. */
5610 else if (e
->ts
.type
== BT_ASSUMED
&& e
->ref
5611 && !(e
->ref
->type
== REF_ARRAY
&& e
->ref
->u
.ar
.type
== AR_FULL
5612 && e
->ref
->next
== NULL
))
5614 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5615 "reference", sym
->name
, &e
->ref
->u
.ar
.where
);
5619 /* TS 29113, C535b. */
5620 if (((sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
5621 && CLASS_DATA (sym
)->as
5622 && CLASS_DATA (sym
)->as
->type
== AS_ASSUMED_RANK
)
5623 || (sym
->ts
.type
!= BT_CLASS
&& sym
->as
5624 && sym
->as
->type
== AS_ASSUMED_RANK
))
5626 && !(e
->ref
->type
== REF_ARRAY
&& e
->ref
->u
.ar
.type
== AR_FULL
5627 && e
->ref
->next
== NULL
))
5629 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5630 "reference", sym
->name
, &e
->ref
->u
.ar
.where
);
5634 /* For variables that are used in an associate (target => object) where
5635 the object's basetype is array valued while the target is scalar,
5636 the ts' type of the component refs is still array valued, which
5637 can't be translated that way. */
5638 if (sym
->assoc
&& e
->rank
== 0 && e
->ref
&& sym
->ts
.type
== BT_CLASS
5639 && sym
->assoc
->target
&& sym
->assoc
->target
->ts
.type
== BT_CLASS
5640 && CLASS_DATA (sym
->assoc
->target
)->as
)
5642 gfc_ref
*ref
= e
->ref
;
5648 ref
->u
.c
.sym
= sym
->ts
.u
.derived
;
5649 /* Stop the loop. */
5659 /* If this is an associate-name, it may be parsed with an array reference
5660 in error even though the target is scalar. Fail directly in this case.
5661 TODO Understand why class scalar expressions must be excluded. */
5662 if (sym
->assoc
&& !(sym
->ts
.type
== BT_CLASS
&& e
->rank
== 0))
5664 if (sym
->ts
.type
== BT_CLASS
)
5665 gfc_fix_class_refs (e
);
5666 if (!sym
->attr
.dimension
&& e
->ref
&& e
->ref
->type
== REF_ARRAY
)
5668 else if (sym
->attr
.dimension
&& (!e
->ref
|| e
->ref
->type
!= REF_ARRAY
))
5670 /* This can happen because the parser did not detect that the
5671 associate name is an array and the expression had no array
5673 gfc_ref
*ref
= gfc_get_ref ();
5674 ref
->type
= REF_ARRAY
;
5675 ref
->u
.ar
= *gfc_get_array_ref();
5676 ref
->u
.ar
.type
= AR_FULL
;
5679 ref
->u
.ar
.as
= sym
->as
;
5680 ref
->u
.ar
.dimen
= sym
->as
->rank
;
5688 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.generic
)
5689 sym
->ts
.u
.derived
= gfc_find_dt_in_generic (sym
->ts
.u
.derived
);
5691 /* On the other hand, the parser may not have known this is an array;
5692 in this case, we have to add a FULL reference. */
5693 if (sym
->assoc
&& sym
->attr
.dimension
&& !e
->ref
)
5695 e
->ref
= gfc_get_ref ();
5696 e
->ref
->type
= REF_ARRAY
;
5697 e
->ref
->u
.ar
.type
= AR_FULL
;
5698 e
->ref
->u
.ar
.dimen
= 0;
5701 /* Like above, but for class types, where the checking whether an array
5702 ref is present is more complicated. Furthermore make sure not to add
5703 the full array ref to _vptr or _len refs. */
5704 if (sym
->assoc
&& sym
->ts
.type
== BT_CLASS
5705 && CLASS_DATA (sym
)->attr
.dimension
5706 && (e
->ts
.type
!= BT_DERIVED
|| !e
->ts
.u
.derived
->attr
.vtype
))
5708 gfc_ref
*ref
, *newref
;
5710 newref
= gfc_get_ref ();
5711 newref
->type
= REF_ARRAY
;
5712 newref
->u
.ar
.type
= AR_FULL
;
5713 newref
->u
.ar
.dimen
= 0;
5714 /* Because this is an associate var and the first ref either is a ref to
5715 the _data component or not, no traversal of the ref chain is
5716 needed. The array ref needs to be inserted after the _data ref,
5717 or when that is not present, which may happend for polymorphic
5718 types, then at the first position. */
5722 else if (ref
->type
== REF_COMPONENT
5723 && strcmp ("_data", ref
->u
.c
.component
->name
) == 0)
5725 if (!ref
->next
|| ref
->next
->type
!= REF_ARRAY
)
5727 newref
->next
= ref
->next
;
5731 /* Array ref present already. */
5732 gfc_free_ref_list (newref
);
5734 else if (ref
->type
== REF_ARRAY
)
5735 /* Array ref present already. */
5736 gfc_free_ref_list (newref
);
5744 if (e
->ref
&& !gfc_resolve_ref (e
))
5747 if (sym
->attr
.flavor
== FL_PROCEDURE
5748 && (!sym
->attr
.function
5749 || (sym
->attr
.function
&& sym
->result
5750 && sym
->result
->attr
.proc_pointer
5751 && !sym
->result
->attr
.function
)))
5753 e
->ts
.type
= BT_PROCEDURE
;
5754 goto resolve_procedure
;
5757 if (sym
->ts
.type
!= BT_UNKNOWN
)
5758 gfc_variable_attr (e
, &e
->ts
);
5759 else if (sym
->attr
.flavor
== FL_PROCEDURE
5760 && sym
->attr
.function
&& sym
->result
5761 && sym
->result
->ts
.type
!= BT_UNKNOWN
5762 && sym
->result
->attr
.proc_pointer
)
5763 e
->ts
= sym
->result
->ts
;
5766 /* Must be a simple variable reference. */
5767 if (!gfc_set_default_type (sym
, 1, sym
->ns
))
5772 if (check_assumed_size_reference (sym
, e
))
5775 /* Deal with forward references to entries during gfc_resolve_code, to
5776 satisfy, at least partially, 12.5.2.5. */
5777 if (gfc_current_ns
->entries
5778 && current_entry_id
== sym
->entry_id
5781 && cs_base
->current
->op
!= EXEC_ENTRY
)
5783 gfc_entry_list
*entry
;
5784 gfc_formal_arglist
*formal
;
5786 bool seen
, saved_specification_expr
;
5788 /* If the symbol is a dummy... */
5789 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
5791 entry
= gfc_current_ns
->entries
;
5794 /* ...test if the symbol is a parameter of previous entries. */
5795 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
5796 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
5798 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
5805 /* If it has not been seen as a dummy, this is an error. */
5808 if (specification_expr
)
5809 gfc_error ("Variable %qs, used in a specification expression"
5810 ", is referenced at %L before the ENTRY statement "
5811 "in which it is a parameter",
5812 sym
->name
, &cs_base
->current
->loc
);
5814 gfc_error ("Variable %qs is used at %L before the ENTRY "
5815 "statement in which it is a parameter",
5816 sym
->name
, &cs_base
->current
->loc
);
5821 /* Now do the same check on the specification expressions. */
5822 saved_specification_expr
= specification_expr
;
5823 specification_expr
= true;
5824 if (sym
->ts
.type
== BT_CHARACTER
5825 && !gfc_resolve_expr (sym
->ts
.u
.cl
->length
))
5829 for (n
= 0; n
< sym
->as
->rank
; n
++)
5831 if (!gfc_resolve_expr (sym
->as
->lower
[n
]))
5833 if (!gfc_resolve_expr (sym
->as
->upper
[n
]))
5836 specification_expr
= saved_specification_expr
;
5839 /* Update the symbol's entry level. */
5840 sym
->entry_id
= current_entry_id
+ 1;
5843 /* If a symbol has been host_associated mark it. This is used latter,
5844 to identify if aliasing is possible via host association. */
5845 if (sym
->attr
.flavor
== FL_VARIABLE
5846 && gfc_current_ns
->parent
5847 && (gfc_current_ns
->parent
== sym
->ns
5848 || (gfc_current_ns
->parent
->parent
5849 && gfc_current_ns
->parent
->parent
== sym
->ns
)))
5850 sym
->attr
.host_assoc
= 1;
5852 if (gfc_current_ns
->proc_name
5853 && sym
->attr
.dimension
5854 && (sym
->ns
!= gfc_current_ns
5855 || sym
->attr
.use_assoc
5856 || sym
->attr
.in_common
))
5857 gfc_current_ns
->proc_name
->attr
.array_outer_dependency
= 1;
5860 if (t
&& !resolve_procedure_expression (e
))
5863 /* F2008, C617 and C1229. */
5864 if (!inquiry_argument
&& (e
->ts
.type
== BT_CLASS
|| e
->ts
.type
== BT_DERIVED
)
5865 && gfc_is_coindexed (e
))
5867 gfc_ref
*ref
, *ref2
= NULL
;
5869 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5871 if (ref
->type
== REF_COMPONENT
)
5873 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5877 for ( ; ref
; ref
= ref
->next
)
5878 if (ref
->type
== REF_COMPONENT
)
5881 /* Expression itself is not coindexed object. */
5882 if (ref
&& e
->ts
.type
== BT_CLASS
)
5884 gfc_error ("Polymorphic subobject of coindexed object at %L",
5889 /* Expression itself is coindexed object. */
5893 c
= ref2
? ref2
->u
.c
.component
: e
->symtree
->n
.sym
->components
;
5894 for ( ; c
; c
= c
->next
)
5895 if (c
->attr
.allocatable
&& c
->ts
.type
== BT_CLASS
)
5897 gfc_error ("Coindexed object with polymorphic allocatable "
5898 "subcomponent at %L", &e
->where
);
5906 gfc_expression_rank (e
);
5908 if (t
&& flag_coarray
== GFC_FCOARRAY_LIB
&& gfc_is_coindexed (e
))
5909 add_caf_get_intrinsic (e
);
5911 /* Simplify cases where access to a parameter array results in a
5912 single constant. Suppress errors since those will have been
5913 issued before, as warnings. */
5914 if (e
->rank
== 0 && sym
->as
&& sym
->attr
.flavor
== FL_PARAMETER
)
5916 gfc_push_suppress_errors ();
5917 gfc_simplify_expr (e
, 1);
5918 gfc_pop_suppress_errors ();
5925 /* Checks to see that the correct symbol has been host associated.
5926 The only situation where this arises is that in which a twice
5927 contained function is parsed after the host association is made.
5928 Therefore, on detecting this, change the symbol in the expression
5929 and convert the array reference into an actual arglist if the old
5930 symbol is a variable. */
5932 check_host_association (gfc_expr
*e
)
5934 gfc_symbol
*sym
, *old_sym
;
5938 gfc_actual_arglist
*arg
, *tail
= NULL
;
5939 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
5941 /* If the expression is the result of substitution in
5942 interface.c(gfc_extend_expr) because there is no way in
5943 which the host association can be wrong. */
5944 if (e
->symtree
== NULL
5945 || e
->symtree
->n
.sym
== NULL
5946 || e
->user_operator
)
5949 old_sym
= e
->symtree
->n
.sym
;
5951 if (gfc_current_ns
->parent
5952 && old_sym
->ns
!= gfc_current_ns
)
5954 /* Use the 'USE' name so that renamed module symbols are
5955 correctly handled. */
5956 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
5958 if (sym
&& old_sym
!= sym
5959 && sym
->ts
.type
== old_sym
->ts
.type
5960 && sym
->attr
.flavor
== FL_PROCEDURE
5961 && sym
->attr
.contained
)
5963 /* Clear the shape, since it might not be valid. */
5964 gfc_free_shape (&e
->shape
, e
->rank
);
5966 /* Give the expression the right symtree! */
5967 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
5968 gcc_assert (st
!= NULL
);
5970 if (old_sym
->attr
.flavor
== FL_PROCEDURE
5971 || e
->expr_type
== EXPR_FUNCTION
)
5973 /* Original was function so point to the new symbol, since
5974 the actual argument list is already attached to the
5976 e
->value
.function
.esym
= NULL
;
5981 /* Original was variable so convert array references into
5982 an actual arglist. This does not need any checking now
5983 since resolve_function will take care of it. */
5984 e
->value
.function
.actual
= NULL
;
5985 e
->expr_type
= EXPR_FUNCTION
;
5988 /* Ambiguity will not arise if the array reference is not
5989 the last reference. */
5990 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5991 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
5994 gcc_assert (ref
->type
== REF_ARRAY
);
5996 /* Grab the start expressions from the array ref and
5997 copy them into actual arguments. */
5998 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
6000 arg
= gfc_get_actual_arglist ();
6001 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
6002 if (e
->value
.function
.actual
== NULL
)
6003 tail
= e
->value
.function
.actual
= arg
;
6011 /* Dump the reference list and set the rank. */
6012 gfc_free_ref_list (e
->ref
);
6014 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
6017 gfc_resolve_expr (e
);
6021 /* This might have changed! */
6022 return e
->expr_type
== EXPR_FUNCTION
;
6027 gfc_resolve_character_operator (gfc_expr
*e
)
6029 gfc_expr
*op1
= e
->value
.op
.op1
;
6030 gfc_expr
*op2
= e
->value
.op
.op2
;
6031 gfc_expr
*e1
= NULL
;
6032 gfc_expr
*e2
= NULL
;
6034 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
6036 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
6037 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
6038 else if (op1
->expr_type
== EXPR_CONSTANT
)
6039 e1
= gfc_get_int_expr (gfc_charlen_int_kind
, NULL
,
6040 op1
->value
.character
.length
);
6042 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
6043 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
6044 else if (op2
->expr_type
== EXPR_CONSTANT
)
6045 e2
= gfc_get_int_expr (gfc_charlen_int_kind
, NULL
,
6046 op2
->value
.character
.length
);
6048 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
6058 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
6059 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
6060 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
6061 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
6062 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
6068 /* Ensure that an character expression has a charlen and, if possible, a
6069 length expression. */
6072 fixup_charlen (gfc_expr
*e
)
6074 /* The cases fall through so that changes in expression type and the need
6075 for multiple fixes are picked up. In all circumstances, a charlen should
6076 be available for the middle end to hang a backend_decl on. */
6077 switch (e
->expr_type
)
6080 gfc_resolve_character_operator (e
);
6084 if (e
->expr_type
== EXPR_ARRAY
)
6085 gfc_resolve_character_array_constructor (e
);
6088 case EXPR_SUBSTRING
:
6089 if (!e
->ts
.u
.cl
&& e
->ref
)
6090 gfc_resolve_substring_charlen (e
);
6095 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
6102 /* Update an actual argument to include the passed-object for type-bound
6103 procedures at the right position. */
6105 static gfc_actual_arglist
*
6106 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
6109 gcc_assert (argpos
> 0);
6113 gfc_actual_arglist
* result
;
6115 result
= gfc_get_actual_arglist ();
6119 result
->name
= name
;
6125 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
6127 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
6132 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6135 extract_compcall_passed_object (gfc_expr
* e
)
6139 if (e
->expr_type
== EXPR_UNKNOWN
)
6141 gfc_error ("Error in typebound call at %L",
6146 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
6148 if (e
->value
.compcall
.base_object
)
6149 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
6152 po
= gfc_get_expr ();
6153 po
->expr_type
= EXPR_VARIABLE
;
6154 po
->symtree
= e
->symtree
;
6155 po
->ref
= gfc_copy_ref (e
->ref
);
6156 po
->where
= e
->where
;
6159 if (!gfc_resolve_expr (po
))
6166 /* Update the arglist of an EXPR_COMPCALL expression to include the
6170 update_compcall_arglist (gfc_expr
* e
)
6173 gfc_typebound_proc
* tbp
;
6175 tbp
= e
->value
.compcall
.tbp
;
6180 po
= extract_compcall_passed_object (e
);
6184 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
6190 if (tbp
->pass_arg_num
<= 0)
6193 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
6201 /* Extract the passed object from a PPC call (a copy of it). */
6204 extract_ppc_passed_object (gfc_expr
*e
)
6209 po
= gfc_get_expr ();
6210 po
->expr_type
= EXPR_VARIABLE
;
6211 po
->symtree
= e
->symtree
;
6212 po
->ref
= gfc_copy_ref (e
->ref
);
6213 po
->where
= e
->where
;
6215 /* Remove PPC reference. */
6217 while ((*ref
)->next
)
6218 ref
= &(*ref
)->next
;
6219 gfc_free_ref_list (*ref
);
6222 if (!gfc_resolve_expr (po
))
6229 /* Update the actual arglist of a procedure pointer component to include the
6233 update_ppc_arglist (gfc_expr
* e
)
6237 gfc_typebound_proc
* tb
;
6239 ppc
= gfc_get_proc_ptr_comp (e
);
6247 else if (tb
->nopass
)
6250 po
= extract_ppc_passed_object (e
);
6257 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
6262 if (po
->ts
.type
== BT_DERIVED
&& po
->ts
.u
.derived
->attr
.abstract
)
6264 gfc_error ("Base object for procedure-pointer component call at %L is of"
6265 " ABSTRACT type %qs", &e
->where
, po
->ts
.u
.derived
->name
);
6269 gcc_assert (tb
->pass_arg_num
> 0);
6270 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
6278 /* Check that the object a TBP is called on is valid, i.e. it must not be
6279 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6282 check_typebound_baseobject (gfc_expr
* e
)
6285 bool return_value
= false;
6287 base
= extract_compcall_passed_object (e
);
6291 if (base
->ts
.type
!= BT_DERIVED
&& base
->ts
.type
!= BT_CLASS
)
6293 gfc_error ("Error in typebound call at %L", &e
->where
);
6297 if (base
->ts
.type
== BT_CLASS
&& !gfc_expr_attr (base
).class_ok
)
6301 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
6303 gfc_error ("Base object for type-bound procedure call at %L is of"
6304 " ABSTRACT type %qs", &e
->where
, base
->ts
.u
.derived
->name
);
6308 /* F08:C1230. If the procedure called is NOPASS,
6309 the base object must be scalar. */
6310 if (e
->value
.compcall
.tbp
->nopass
&& base
->rank
!= 0)
6312 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6313 " be scalar", &e
->where
);
6317 return_value
= true;
6320 gfc_free_expr (base
);
6321 return return_value
;
6325 /* Resolve a call to a type-bound procedure, either function or subroutine,
6326 statically from the data in an EXPR_COMPCALL expression. The adapted
6327 arglist and the target-procedure symtree are returned. */
6330 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
6331 gfc_actual_arglist
** actual
)
6333 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
6334 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
6336 /* Update the actual arglist for PASS. */
6337 if (!update_compcall_arglist (e
))
6340 *actual
= e
->value
.compcall
.actual
;
6341 *target
= e
->value
.compcall
.tbp
->u
.specific
;
6343 gfc_free_ref_list (e
->ref
);
6345 e
->value
.compcall
.actual
= NULL
;
6347 /* If we find a deferred typebound procedure, check for derived types
6348 that an overriding typebound procedure has not been missed. */
6349 if (e
->value
.compcall
.name
6350 && !e
->value
.compcall
.tbp
->non_overridable
6351 && e
->value
.compcall
.base_object
6352 && e
->value
.compcall
.base_object
->ts
.type
== BT_DERIVED
)
6355 gfc_symbol
*derived
;
6357 /* Use the derived type of the base_object. */
6358 derived
= e
->value
.compcall
.base_object
->ts
.u
.derived
;
6361 /* If necessary, go through the inheritance chain. */
6362 while (!st
&& derived
)
6364 /* Look for the typebound procedure 'name'. */
6365 if (derived
->f2k_derived
&& derived
->f2k_derived
->tb_sym_root
)
6366 st
= gfc_find_symtree (derived
->f2k_derived
->tb_sym_root
,
6367 e
->value
.compcall
.name
);
6369 derived
= gfc_get_derived_super_type (derived
);
6372 /* Now find the specific name in the derived type namespace. */
6373 if (st
&& st
->n
.tb
&& st
->n
.tb
->u
.specific
)
6374 gfc_find_sym_tree (st
->n
.tb
->u
.specific
->name
,
6375 derived
->ns
, 1, &st
);
6383 /* Get the ultimate declared type from an expression. In addition,
6384 return the last class/derived type reference and the copy of the
6385 reference list. If check_types is set true, derived types are
6386 identified as well as class references. */
6388 get_declared_from_expr (gfc_ref
**class_ref
, gfc_ref
**new_ref
,
6389 gfc_expr
*e
, bool check_types
)
6391 gfc_symbol
*declared
;
6398 *new_ref
= gfc_copy_ref (e
->ref
);
6400 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
6402 if (ref
->type
!= REF_COMPONENT
)
6405 if ((ref
->u
.c
.component
->ts
.type
== BT_CLASS
6406 || (check_types
&& gfc_bt_struct (ref
->u
.c
.component
->ts
.type
)))
6407 && ref
->u
.c
.component
->attr
.flavor
!= FL_PROCEDURE
)
6409 declared
= ref
->u
.c
.component
->ts
.u
.derived
;
6415 if (declared
== NULL
)
6416 declared
= e
->symtree
->n
.sym
->ts
.u
.derived
;
6422 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6423 which of the specific bindings (if any) matches the arglist and transform
6424 the expression into a call of that binding. */
6427 resolve_typebound_generic_call (gfc_expr
* e
, const char **name
)
6429 gfc_typebound_proc
* genproc
;
6430 const char* genname
;
6432 gfc_symbol
*derived
;
6434 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
6435 genname
= e
->value
.compcall
.name
;
6436 genproc
= e
->value
.compcall
.tbp
;
6438 if (!genproc
->is_generic
)
6441 /* Try the bindings on this type and in the inheritance hierarchy. */
6442 for (; genproc
; genproc
= genproc
->overridden
)
6446 gcc_assert (genproc
->is_generic
);
6447 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
6450 gfc_actual_arglist
* args
;
6453 gcc_assert (g
->specific
);
6455 if (g
->specific
->error
)
6458 target
= g
->specific
->u
.specific
->n
.sym
;
6460 /* Get the right arglist by handling PASS/NOPASS. */
6461 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
6462 if (!g
->specific
->nopass
)
6465 po
= extract_compcall_passed_object (e
);
6468 gfc_free_actual_arglist (args
);
6472 gcc_assert (g
->specific
->pass_arg_num
> 0);
6473 gcc_assert (!g
->specific
->error
);
6474 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
6475 g
->specific
->pass_arg
);
6477 resolve_actual_arglist (args
, target
->attr
.proc
,
6478 is_external_proc (target
)
6479 && gfc_sym_get_dummy_args (target
) == NULL
);
6481 /* Check if this arglist matches the formal. */
6482 matches
= gfc_arglist_matches_symbol (&args
, target
);
6484 /* Clean up and break out of the loop if we've found it. */
6485 gfc_free_actual_arglist (args
);
6488 e
->value
.compcall
.tbp
= g
->specific
;
6489 genname
= g
->specific_st
->name
;
6490 /* Pass along the name for CLASS methods, where the vtab
6491 procedure pointer component has to be referenced. */
6499 /* Nothing matching found! */
6500 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6501 " %qs at %L", genname
, &e
->where
);
6505 /* Make sure that we have the right specific instance for the name. */
6506 derived
= get_declared_from_expr (NULL
, NULL
, e
, true);
6508 st
= gfc_find_typebound_proc (derived
, NULL
, genname
, true, &e
->where
);
6510 e
->value
.compcall
.tbp
= st
->n
.tb
;
6516 /* Resolve a call to a type-bound subroutine. */
6519 resolve_typebound_call (gfc_code
* c
, const char **name
, bool *overridable
)
6521 gfc_actual_arglist
* newactual
;
6522 gfc_symtree
* target
;
6524 /* Check that's really a SUBROUTINE. */
6525 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
6527 if (!c
->expr1
->value
.compcall
.tbp
->is_generic
6528 && c
->expr1
->value
.compcall
.tbp
->u
.specific
6529 && c
->expr1
->value
.compcall
.tbp
->u
.specific
->n
.sym
6530 && c
->expr1
->value
.compcall
.tbp
->u
.specific
->n
.sym
->attr
.subroutine
)
6531 c
->expr1
->value
.compcall
.tbp
->subroutine
= 1;
6534 gfc_error ("%qs at %L should be a SUBROUTINE",
6535 c
->expr1
->value
.compcall
.name
, &c
->loc
);
6540 if (!check_typebound_baseobject (c
->expr1
))
6543 /* Pass along the name for CLASS methods, where the vtab
6544 procedure pointer component has to be referenced. */
6546 *name
= c
->expr1
->value
.compcall
.name
;
6548 if (!resolve_typebound_generic_call (c
->expr1
, name
))
6551 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6553 *overridable
= !c
->expr1
->value
.compcall
.tbp
->non_overridable
;
6555 /* Transform into an ordinary EXEC_CALL for now. */
6557 if (!resolve_typebound_static (c
->expr1
, &target
, &newactual
))
6560 c
->ext
.actual
= newactual
;
6561 c
->symtree
= target
;
6562 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
6564 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
6566 gfc_free_expr (c
->expr1
);
6567 c
->expr1
= gfc_get_expr ();
6568 c
->expr1
->expr_type
= EXPR_FUNCTION
;
6569 c
->expr1
->symtree
= target
;
6570 c
->expr1
->where
= c
->loc
;
6572 return resolve_call (c
);
6576 /* Resolve a component-call expression. */
6578 resolve_compcall (gfc_expr
* e
, const char **name
)
6580 gfc_actual_arglist
* newactual
;
6581 gfc_symtree
* target
;
6583 /* Check that's really a FUNCTION. */
6584 if (!e
->value
.compcall
.tbp
->function
)
6586 gfc_error ("%qs at %L should be a FUNCTION",
6587 e
->value
.compcall
.name
, &e
->where
);
6592 /* These must not be assign-calls! */
6593 gcc_assert (!e
->value
.compcall
.assign
);
6595 if (!check_typebound_baseobject (e
))
6598 /* Pass along the name for CLASS methods, where the vtab
6599 procedure pointer component has to be referenced. */
6601 *name
= e
->value
.compcall
.name
;
6603 if (!resolve_typebound_generic_call (e
, name
))
6605 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
6607 /* Take the rank from the function's symbol. */
6608 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
6609 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
6611 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6612 arglist to the TBP's binding target. */
6614 if (!resolve_typebound_static (e
, &target
, &newactual
))
6617 e
->value
.function
.actual
= newactual
;
6618 e
->value
.function
.name
= NULL
;
6619 e
->value
.function
.esym
= target
->n
.sym
;
6620 e
->value
.function
.isym
= NULL
;
6621 e
->symtree
= target
;
6622 e
->ts
= target
->n
.sym
->ts
;
6623 e
->expr_type
= EXPR_FUNCTION
;
6625 /* Resolution is not necessary if this is a class subroutine; this
6626 function only has to identify the specific proc. Resolution of
6627 the call will be done next in resolve_typebound_call. */
6628 return gfc_resolve_expr (e
);
6632 static bool resolve_fl_derived (gfc_symbol
*sym
);
6635 /* Resolve a typebound function, or 'method'. First separate all
6636 the non-CLASS references by calling resolve_compcall directly. */
6639 resolve_typebound_function (gfc_expr
* e
)
6641 gfc_symbol
*declared
;
6653 /* Deal with typebound operators for CLASS objects. */
6654 expr
= e
->value
.compcall
.base_object
;
6655 overridable
= !e
->value
.compcall
.tbp
->non_overridable
;
6656 if (expr
&& expr
->ts
.type
== BT_CLASS
&& e
->value
.compcall
.name
)
6658 /* Since the typebound operators are generic, we have to ensure
6659 that any delays in resolution are corrected and that the vtab
6662 declared
= ts
.u
.derived
;
6663 c
= gfc_find_component (declared
, "_vptr", true, true, NULL
);
6664 if (c
->ts
.u
.derived
== NULL
)
6665 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
6667 if (!resolve_compcall (e
, &name
))
6670 /* Use the generic name if it is there. */
6671 name
= name
? name
: e
->value
.function
.esym
->name
;
6672 e
->symtree
= expr
->symtree
;
6673 e
->ref
= gfc_copy_ref (expr
->ref
);
6674 get_declared_from_expr (&class_ref
, NULL
, e
, false);
6676 /* Trim away the extraneous references that emerge from nested
6677 use of interface.c (extend_expr). */
6678 if (class_ref
&& class_ref
->next
)
6680 gfc_free_ref_list (class_ref
->next
);
6681 class_ref
->next
= NULL
;
6683 else if (e
->ref
&& !class_ref
&& expr
->ts
.type
!= BT_CLASS
)
6685 gfc_free_ref_list (e
->ref
);
6689 gfc_add_vptr_component (e
);
6690 gfc_add_component_ref (e
, name
);
6691 e
->value
.function
.esym
= NULL
;
6692 if (expr
->expr_type
!= EXPR_VARIABLE
)
6693 e
->base_expr
= expr
;
6698 return resolve_compcall (e
, NULL
);
6700 if (!gfc_resolve_ref (e
))
6703 /* Get the CLASS declared type. */
6704 declared
= get_declared_from_expr (&class_ref
, &new_ref
, e
, true);
6706 if (!resolve_fl_derived (declared
))
6709 /* Weed out cases of the ultimate component being a derived type. */
6710 if ((class_ref
&& gfc_bt_struct (class_ref
->u
.c
.component
->ts
.type
))
6711 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
6713 gfc_free_ref_list (new_ref
);
6714 return resolve_compcall (e
, NULL
);
6717 c
= gfc_find_component (declared
, "_data", true, true, NULL
);
6719 /* Treat the call as if it is a typebound procedure, in order to roll
6720 out the correct name for the specific function. */
6721 if (!resolve_compcall (e
, &name
))
6723 gfc_free_ref_list (new_ref
);
6730 /* Convert the expression to a procedure pointer component call. */
6731 e
->value
.function
.esym
= NULL
;
6737 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6738 gfc_add_vptr_component (e
);
6739 gfc_add_component_ref (e
, name
);
6741 /* Recover the typespec for the expression. This is really only
6742 necessary for generic procedures, where the additional call
6743 to gfc_add_component_ref seems to throw the collection of the
6744 correct typespec. */
6748 gfc_free_ref_list (new_ref
);
6753 /* Resolve a typebound subroutine, or 'method'. First separate all
6754 the non-CLASS references by calling resolve_typebound_call
6758 resolve_typebound_subroutine (gfc_code
*code
)
6760 gfc_symbol
*declared
;
6770 st
= code
->expr1
->symtree
;
6772 /* Deal with typebound operators for CLASS objects. */
6773 expr
= code
->expr1
->value
.compcall
.base_object
;
6774 overridable
= !code
->expr1
->value
.compcall
.tbp
->non_overridable
;
6775 if (expr
&& expr
->ts
.type
== BT_CLASS
&& code
->expr1
->value
.compcall
.name
)
6777 /* If the base_object is not a variable, the corresponding actual
6778 argument expression must be stored in e->base_expression so
6779 that the corresponding tree temporary can be used as the base
6780 object in gfc_conv_procedure_call. */
6781 if (expr
->expr_type
!= EXPR_VARIABLE
)
6783 gfc_actual_arglist
*args
;
6785 args
= code
->expr1
->value
.function
.actual
;
6786 for (; args
; args
= args
->next
)
6787 if (expr
== args
->expr
)
6791 /* Since the typebound operators are generic, we have to ensure
6792 that any delays in resolution are corrected and that the vtab
6794 declared
= expr
->ts
.u
.derived
;
6795 c
= gfc_find_component (declared
, "_vptr", true, true, NULL
);
6796 if (c
->ts
.u
.derived
== NULL
)
6797 c
->ts
.u
.derived
= gfc_find_derived_vtab (declared
);
6799 if (!resolve_typebound_call (code
, &name
, NULL
))
6802 /* Use the generic name if it is there. */
6803 name
= name
? name
: code
->expr1
->value
.function
.esym
->name
;
6804 code
->expr1
->symtree
= expr
->symtree
;
6805 code
->expr1
->ref
= gfc_copy_ref (expr
->ref
);
6807 /* Trim away the extraneous references that emerge from nested
6808 use of interface.c (extend_expr). */
6809 get_declared_from_expr (&class_ref
, NULL
, code
->expr1
, false);
6810 if (class_ref
&& class_ref
->next
)
6812 gfc_free_ref_list (class_ref
->next
);
6813 class_ref
->next
= NULL
;
6815 else if (code
->expr1
->ref
&& !class_ref
)
6817 gfc_free_ref_list (code
->expr1
->ref
);
6818 code
->expr1
->ref
= NULL
;
6821 /* Now use the procedure in the vtable. */
6822 gfc_add_vptr_component (code
->expr1
);
6823 gfc_add_component_ref (code
->expr1
, name
);
6824 code
->expr1
->value
.function
.esym
= NULL
;
6825 if (expr
->expr_type
!= EXPR_VARIABLE
)
6826 code
->expr1
->base_expr
= expr
;
6831 return resolve_typebound_call (code
, NULL
, NULL
);
6833 if (!gfc_resolve_ref (code
->expr1
))
6836 /* Get the CLASS declared type. */
6837 get_declared_from_expr (&class_ref
, &new_ref
, code
->expr1
, true);
6839 /* Weed out cases of the ultimate component being a derived type. */
6840 if ((class_ref
&& gfc_bt_struct (class_ref
->u
.c
.component
->ts
.type
))
6841 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
6843 gfc_free_ref_list (new_ref
);
6844 return resolve_typebound_call (code
, NULL
, NULL
);
6847 if (!resolve_typebound_call (code
, &name
, &overridable
))
6849 gfc_free_ref_list (new_ref
);
6852 ts
= code
->expr1
->ts
;
6856 /* Convert the expression to a procedure pointer component call. */
6857 code
->expr1
->value
.function
.esym
= NULL
;
6858 code
->expr1
->symtree
= st
;
6861 code
->expr1
->ref
= new_ref
;
6863 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6864 gfc_add_vptr_component (code
->expr1
);
6865 gfc_add_component_ref (code
->expr1
, name
);
6867 /* Recover the typespec for the expression. This is really only
6868 necessary for generic procedures, where the additional call
6869 to gfc_add_component_ref seems to throw the collection of the
6870 correct typespec. */
6871 code
->expr1
->ts
= ts
;
6874 gfc_free_ref_list (new_ref
);
6880 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6883 resolve_ppc_call (gfc_code
* c
)
6885 gfc_component
*comp
;
6887 comp
= gfc_get_proc_ptr_comp (c
->expr1
);
6888 gcc_assert (comp
!= NULL
);
6890 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
6891 c
->expr1
->expr_type
= EXPR_VARIABLE
;
6893 if (!comp
->attr
.subroutine
)
6894 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
6896 if (!gfc_resolve_ref (c
->expr1
))
6899 if (!update_ppc_arglist (c
->expr1
))
6902 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
6904 if (!resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
6905 !(comp
->ts
.interface
6906 && comp
->ts
.interface
->formal
)))
6909 if (!pure_subroutine (comp
->ts
.interface
, comp
->name
, &c
->expr1
->where
))
6912 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
6918 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6921 resolve_expr_ppc (gfc_expr
* e
)
6923 gfc_component
*comp
;
6925 comp
= gfc_get_proc_ptr_comp (e
);
6926 gcc_assert (comp
!= NULL
);
6928 /* Convert to EXPR_FUNCTION. */
6929 e
->expr_type
= EXPR_FUNCTION
;
6930 e
->value
.function
.isym
= NULL
;
6931 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
6933 if (comp
->as
!= NULL
)
6934 e
->rank
= comp
->as
->rank
;
6936 if (!comp
->attr
.function
)
6937 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
6939 if (!gfc_resolve_ref (e
))
6942 if (!resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
6943 !(comp
->ts
.interface
6944 && comp
->ts
.interface
->formal
)))
6947 if (!update_ppc_arglist (e
))
6950 if (!check_pure_function(e
))
6953 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
6960 gfc_is_expandable_expr (gfc_expr
*e
)
6962 gfc_constructor
*con
;
6964 if (e
->expr_type
== EXPR_ARRAY
)
6966 /* Traverse the constructor looking for variables that are flavor
6967 parameter. Parameters must be expanded since they are fully used at
6969 con
= gfc_constructor_first (e
->value
.constructor
);
6970 for (; con
; con
= gfc_constructor_next (con
))
6972 if (con
->expr
->expr_type
== EXPR_VARIABLE
6973 && con
->expr
->symtree
6974 && (con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
6975 || con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_VARIABLE
))
6977 if (con
->expr
->expr_type
== EXPR_ARRAY
6978 && gfc_is_expandable_expr (con
->expr
))
6987 /* Sometimes variables in specification expressions of the result
6988 of module procedures in submodules wind up not being the 'real'
6989 dummy. Find this, if possible, in the namespace of the first
6993 fixup_unique_dummy (gfc_expr
*e
)
6995 gfc_symtree
*st
= NULL
;
6996 gfc_symbol
*s
= NULL
;
6998 if (e
->symtree
->n
.sym
->ns
->proc_name
6999 && e
->symtree
->n
.sym
->ns
->proc_name
->formal
)
7000 s
= e
->symtree
->n
.sym
->ns
->proc_name
->formal
->sym
;
7003 st
= gfc_find_symtree (s
->ns
->sym_root
, e
->symtree
->n
.sym
->name
);
7006 && st
->n
.sym
!= NULL
7007 && st
->n
.sym
->attr
.dummy
)
7011 /* Resolve an expression. That is, make sure that types of operands agree
7012 with their operators, intrinsic operators are converted to function calls
7013 for overloaded types and unresolved function references are resolved. */
7016 gfc_resolve_expr (gfc_expr
*e
)
7019 bool inquiry_save
, actual_arg_save
, first_actual_arg_save
;
7021 if (e
== NULL
|| e
->do_not_resolve_again
)
7024 /* inquiry_argument only applies to variables. */
7025 inquiry_save
= inquiry_argument
;
7026 actual_arg_save
= actual_arg
;
7027 first_actual_arg_save
= first_actual_arg
;
7029 if (e
->expr_type
!= EXPR_VARIABLE
)
7031 inquiry_argument
= false;
7033 first_actual_arg
= false;
7035 else if (e
->symtree
!= NULL
7036 && *e
->symtree
->name
== '@'
7037 && e
->symtree
->n
.sym
->attr
.dummy
)
7039 /* Deal with submodule specification expressions that are not
7040 found to be referenced in module.c(read_cleanup). */
7041 fixup_unique_dummy (e
);
7044 switch (e
->expr_type
)
7047 t
= resolve_operator (e
);
7053 if (check_host_association (e
))
7054 t
= resolve_function (e
);
7056 t
= resolve_variable (e
);
7058 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
7059 && e
->ref
->type
!= REF_SUBSTRING
)
7060 gfc_resolve_substring_charlen (e
);
7065 t
= resolve_typebound_function (e
);
7068 case EXPR_SUBSTRING
:
7069 t
= gfc_resolve_ref (e
);
7078 t
= resolve_expr_ppc (e
);
7083 if (!gfc_resolve_ref (e
))
7086 t
= gfc_resolve_array_constructor (e
);
7087 /* Also try to expand a constructor. */
7090 gfc_expression_rank (e
);
7091 if (gfc_is_constant_expr (e
) || gfc_is_expandable_expr (e
))
7092 gfc_expand_constructor (e
, false);
7095 /* This provides the opportunity for the length of constructors with
7096 character valued function elements to propagate the string length
7097 to the expression. */
7098 if (t
&& e
->ts
.type
== BT_CHARACTER
)
7100 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7101 here rather then add a duplicate test for it above. */
7102 gfc_expand_constructor (e
, false);
7103 t
= gfc_resolve_character_array_constructor (e
);
7108 case EXPR_STRUCTURE
:
7109 t
= gfc_resolve_ref (e
);
7113 t
= resolve_structure_cons (e
, 0);
7117 t
= gfc_simplify_expr (e
, 0);
7121 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7124 if (e
->ts
.type
== BT_CHARACTER
&& t
&& !e
->ts
.u
.cl
)
7127 inquiry_argument
= inquiry_save
;
7128 actual_arg
= actual_arg_save
;
7129 first_actual_arg
= first_actual_arg_save
;
7131 /* For some reason, resolving these expressions a second time mangles
7132 the typespec of the expression itself. */
7133 if (t
&& e
->expr_type
== EXPR_VARIABLE
7134 && e
->symtree
->n
.sym
->attr
.select_rank_temporary
7135 && UNLIMITED_POLY (e
->symtree
->n
.sym
))
7136 e
->do_not_resolve_again
= 1;
7142 /* Resolve an expression from an iterator. They must be scalar and have
7143 INTEGER or (optionally) REAL type. */
7146 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
7147 const char *name_msgid
)
7149 if (!gfc_resolve_expr (expr
))
7152 if (expr
->rank
!= 0)
7154 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
7158 if (expr
->ts
.type
!= BT_INTEGER
)
7160 if (expr
->ts
.type
== BT_REAL
)
7163 return gfc_notify_std (GFC_STD_F95_DEL
,
7164 "%s at %L must be integer",
7165 _(name_msgid
), &expr
->where
);
7168 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
7175 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
7183 /* Resolve the expressions in an iterator structure. If REAL_OK is
7184 false allow only INTEGER type iterators, otherwise allow REAL types.
7185 Set own_scope to true for ac-implied-do and data-implied-do as those
7186 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7189 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
, bool own_scope
)
7191 if (!gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable"))
7194 if (!gfc_check_vardef_context (iter
->var
, false, false, own_scope
,
7195 _("iterator variable")))
7198 if (!gfc_resolve_iterator_expr (iter
->start
, real_ok
,
7199 "Start expression in DO loop"))
7202 if (!gfc_resolve_iterator_expr (iter
->end
, real_ok
,
7203 "End expression in DO loop"))
7206 if (!gfc_resolve_iterator_expr (iter
->step
, real_ok
,
7207 "Step expression in DO loop"))
7210 /* Convert start, end, and step to the same type as var. */
7211 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
7212 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
7213 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 1);
7215 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
7216 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
7217 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 1);
7219 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
7220 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
7221 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 1);
7223 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
7225 if ((iter
->step
->ts
.type
== BT_INTEGER
7226 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
7227 || (iter
->step
->ts
.type
== BT_REAL
7228 && mpfr_sgn (iter
->step
->value
.real
) == 0))
7230 gfc_error ("Step expression in DO loop at %L cannot be zero",
7231 &iter
->step
->where
);
7236 if (iter
->start
->expr_type
== EXPR_CONSTANT
7237 && iter
->end
->expr_type
== EXPR_CONSTANT
7238 && iter
->step
->expr_type
== EXPR_CONSTANT
)
7241 if (iter
->start
->ts
.type
== BT_INTEGER
)
7243 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
7244 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
7248 sgn
= mpfr_sgn (iter
->step
->value
.real
);
7249 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
7251 if (warn_zerotrip
&& ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0)))
7252 gfc_warning (OPT_Wzerotrip
,
7253 "DO loop at %L will be executed zero times",
7254 &iter
->step
->where
);
7257 if (iter
->end
->expr_type
== EXPR_CONSTANT
7258 && iter
->end
->ts
.type
== BT_INTEGER
7259 && iter
->step
->expr_type
== EXPR_CONSTANT
7260 && iter
->step
->ts
.type
== BT_INTEGER
7261 && (mpz_cmp_si (iter
->step
->value
.integer
, -1L) == 0
7262 || mpz_cmp_si (iter
->step
->value
.integer
, 1L) == 0))
7264 bool is_step_positive
= mpz_cmp_ui (iter
->step
->value
.integer
, 1) == 0;
7265 int k
= gfc_validate_kind (BT_INTEGER
, iter
->end
->ts
.kind
, false);
7267 if (is_step_positive
7268 && mpz_cmp (iter
->end
->value
.integer
, gfc_integer_kinds
[k
].huge
) == 0)
7269 gfc_warning (OPT_Wundefined_do_loop
,
7270 "DO loop at %L is undefined as it overflows",
7271 &iter
->step
->where
);
7272 else if (!is_step_positive
7273 && mpz_cmp (iter
->end
->value
.integer
,
7274 gfc_integer_kinds
[k
].min_int
) == 0)
7275 gfc_warning (OPT_Wundefined_do_loop
,
7276 "DO loop at %L is undefined as it underflows",
7277 &iter
->step
->where
);
7284 /* Traversal function for find_forall_index. f == 2 signals that
7285 that variable itself is not to be checked - only the references. */
7288 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
7290 if (expr
->expr_type
!= EXPR_VARIABLE
)
7293 /* A scalar assignment */
7294 if (!expr
->ref
|| *f
== 1)
7296 if (expr
->symtree
->n
.sym
== sym
)
7308 /* Check whether the FORALL index appears in the expression or not.
7309 Returns true if SYM is found in EXPR. */
7312 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
7314 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
7321 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7322 to be a scalar INTEGER variable. The subscripts and stride are scalar
7323 INTEGERs, and if stride is a constant it must be nonzero.
7324 Furthermore "A subscript or stride in a forall-triplet-spec shall
7325 not contain a reference to any index-name in the
7326 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7329 resolve_forall_iterators (gfc_forall_iterator
*it
)
7331 gfc_forall_iterator
*iter
, *iter2
;
7333 for (iter
= it
; iter
; iter
= iter
->next
)
7335 if (gfc_resolve_expr (iter
->var
)
7336 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
7337 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7340 if (gfc_resolve_expr (iter
->start
)
7341 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
7342 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7343 &iter
->start
->where
);
7344 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
7345 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 1);
7347 if (gfc_resolve_expr (iter
->end
)
7348 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
7349 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7351 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
7352 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 1);
7354 if (gfc_resolve_expr (iter
->stride
))
7356 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
7357 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7358 &iter
->stride
->where
, "INTEGER");
7360 if (iter
->stride
->expr_type
== EXPR_CONSTANT
7361 && mpz_cmp_ui (iter
->stride
->value
.integer
, 0) == 0)
7362 gfc_error ("FORALL stride expression at %L cannot be zero",
7363 &iter
->stride
->where
);
7365 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
7366 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 1);
7369 for (iter
= it
; iter
; iter
= iter
->next
)
7370 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
7372 if (find_forall_index (iter2
->start
, iter
->var
->symtree
->n
.sym
, 0)
7373 || find_forall_index (iter2
->end
, iter
->var
->symtree
->n
.sym
, 0)
7374 || find_forall_index (iter2
->stride
, iter
->var
->symtree
->n
.sym
, 0))
7375 gfc_error ("FORALL index %qs may not appear in triplet "
7376 "specification at %L", iter
->var
->symtree
->name
,
7377 &iter2
->start
->where
);
7382 /* Given a pointer to a symbol that is a derived type, see if it's
7383 inaccessible, i.e. if it's defined in another module and the components are
7384 PRIVATE. The search is recursive if necessary. Returns zero if no
7385 inaccessible components are found, nonzero otherwise. */
7388 derived_inaccessible (gfc_symbol
*sym
)
7392 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
7395 for (c
= sym
->components
; c
; c
= c
->next
)
7397 /* Prevent an infinite loop through this function. */
7398 if (c
->ts
.type
== BT_DERIVED
&& c
->attr
.pointer
7399 && sym
== c
->ts
.u
.derived
)
7402 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
7410 /* Resolve the argument of a deallocate expression. The expression must be
7411 a pointer or a full array. */
7414 resolve_deallocate_expr (gfc_expr
*e
)
7416 symbol_attribute attr
;
7417 int allocatable
, pointer
;
7423 if (!gfc_resolve_expr (e
))
7426 if (e
->expr_type
!= EXPR_VARIABLE
)
7429 sym
= e
->symtree
->n
.sym
;
7430 unlimited
= UNLIMITED_POLY(sym
);
7432 if (sym
->ts
.type
== BT_CLASS
)
7434 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
7435 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
7439 allocatable
= sym
->attr
.allocatable
;
7440 pointer
= sym
->attr
.pointer
;
7442 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
7447 if (ref
->u
.ar
.type
!= AR_FULL
7448 && !(ref
->u
.ar
.type
== AR_ELEMENT
&& ref
->u
.ar
.as
->rank
== 0
7449 && ref
->u
.ar
.codimen
&& gfc_ref_this_image (ref
)))
7454 c
= ref
->u
.c
.component
;
7455 if (c
->ts
.type
== BT_CLASS
)
7457 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
7458 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
7462 allocatable
= c
->attr
.allocatable
;
7463 pointer
= c
->attr
.pointer
;
7474 attr
= gfc_expr_attr (e
);
7476 if (allocatable
== 0 && attr
.pointer
== 0 && !unlimited
)
7479 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7485 if (gfc_is_coindexed (e
))
7487 gfc_error ("Coindexed allocatable object at %L", &e
->where
);
7492 && !gfc_check_vardef_context (e
, true, true, false,
7493 _("DEALLOCATE object")))
7495 if (!gfc_check_vardef_context (e
, false, true, false,
7496 _("DEALLOCATE object")))
7503 /* Returns true if the expression e contains a reference to the symbol sym. */
7505 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
7507 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
7514 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
7516 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
7520 /* Given the expression node e for an allocatable/pointer of derived type to be
7521 allocated, get the expression node to be initialized afterwards (needed for
7522 derived types with default initializers, and derived types with allocatable
7523 components that need nullification.) */
7526 gfc_expr_to_initialize (gfc_expr
*e
)
7532 result
= gfc_copy_expr (e
);
7534 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7535 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
7536 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
7538 if (ref
->u
.ar
.dimen
== 0
7539 && ref
->u
.ar
.as
&& ref
->u
.ar
.as
->corank
)
7542 ref
->u
.ar
.type
= AR_FULL
;
7544 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
7545 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
7550 gfc_free_shape (&result
->shape
, result
->rank
);
7552 /* Recalculate rank, shape, etc. */
7553 gfc_resolve_expr (result
);
7558 /* If the last ref of an expression is an array ref, return a copy of the
7559 expression with that one removed. Otherwise, a copy of the original
7560 expression. This is used for allocate-expressions and pointer assignment
7561 LHS, where there may be an array specification that needs to be stripped
7562 off when using gfc_check_vardef_context. */
7565 remove_last_array_ref (gfc_expr
* e
)
7570 e2
= gfc_copy_expr (e
);
7571 for (r
= &e2
->ref
; *r
; r
= &(*r
)->next
)
7572 if ((*r
)->type
== REF_ARRAY
&& !(*r
)->next
)
7574 gfc_free_ref_list (*r
);
7583 /* Used in resolve_allocate_expr to check that a allocation-object and
7584 a source-expr are conformable. This does not catch all possible
7585 cases; in particular a runtime checking is needed. */
7588 conformable_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
7591 for (tail
= e2
->ref
; tail
&& tail
->next
; tail
= tail
->next
);
7593 /* First compare rank. */
7594 if ((tail
&& (!tail
->u
.ar
.as
|| e1
->rank
!= tail
->u
.ar
.as
->rank
))
7595 || (!tail
&& e1
->rank
!= e2
->rank
))
7597 gfc_error ("Source-expr at %L must be scalar or have the "
7598 "same rank as the allocate-object at %L",
7599 &e1
->where
, &e2
->where
);
7610 for (i
= 0; i
< e1
->rank
; i
++)
7612 if (tail
->u
.ar
.start
[i
] == NULL
)
7615 if (tail
->u
.ar
.end
[i
])
7617 mpz_set (s
, tail
->u
.ar
.end
[i
]->value
.integer
);
7618 mpz_sub (s
, s
, tail
->u
.ar
.start
[i
]->value
.integer
);
7619 mpz_add_ui (s
, s
, 1);
7623 mpz_set (s
, tail
->u
.ar
.start
[i
]->value
.integer
);
7626 if (mpz_cmp (e1
->shape
[i
], s
) != 0)
7628 gfc_error ("Source-expr at %L and allocate-object at %L must "
7629 "have the same shape", &e1
->where
, &e2
->where
);
7642 /* Resolve the expression in an ALLOCATE statement, doing the additional
7643 checks to see whether the expression is OK or not. The expression must
7644 have a trailing array reference that gives the size of the array. */
7647 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
, bool *array_alloc_wo_spec
)
7649 int i
, pointer
, allocatable
, dimension
, is_abstract
;
7653 symbol_attribute attr
;
7654 gfc_ref
*ref
, *ref2
;
7657 gfc_symbol
*sym
= NULL
;
7662 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7663 checking of coarrays. */
7664 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
7665 if (ref
->next
== NULL
)
7668 if (ref
&& ref
->type
== REF_ARRAY
)
7669 ref
->u
.ar
.in_allocate
= true;
7671 if (!gfc_resolve_expr (e
))
7674 /* Make sure the expression is allocatable or a pointer. If it is
7675 pointer, the next-to-last reference must be a pointer. */
7679 sym
= e
->symtree
->n
.sym
;
7681 /* Check whether ultimate component is abstract and CLASS. */
7684 /* Is the allocate-object unlimited polymorphic? */
7685 unlimited
= UNLIMITED_POLY(e
);
7687 if (e
->expr_type
!= EXPR_VARIABLE
)
7690 attr
= gfc_expr_attr (e
);
7691 pointer
= attr
.pointer
;
7692 dimension
= attr
.dimension
;
7693 codimension
= attr
.codimension
;
7697 if (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
))
7699 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
7700 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
7701 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
7702 codimension
= CLASS_DATA (sym
)->attr
.codimension
;
7703 is_abstract
= CLASS_DATA (sym
)->attr
.abstract
;
7707 allocatable
= sym
->attr
.allocatable
;
7708 pointer
= sym
->attr
.pointer
;
7709 dimension
= sym
->attr
.dimension
;
7710 codimension
= sym
->attr
.codimension
;
7715 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
7720 if (ref
->u
.ar
.codimen
> 0)
7723 for (n
= ref
->u
.ar
.dimen
;
7724 n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
7725 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_THIS_IMAGE
)
7732 if (ref
->next
!= NULL
)
7740 gfc_error ("Coindexed allocatable object at %L",
7745 c
= ref
->u
.c
.component
;
7746 if (c
->ts
.type
== BT_CLASS
)
7748 allocatable
= CLASS_DATA (c
)->attr
.allocatable
;
7749 pointer
= CLASS_DATA (c
)->attr
.class_pointer
;
7750 dimension
= CLASS_DATA (c
)->attr
.dimension
;
7751 codimension
= CLASS_DATA (c
)->attr
.codimension
;
7752 is_abstract
= CLASS_DATA (c
)->attr
.abstract
;
7756 allocatable
= c
->attr
.allocatable
;
7757 pointer
= c
->attr
.pointer
;
7758 dimension
= c
->attr
.dimension
;
7759 codimension
= c
->attr
.codimension
;
7760 is_abstract
= c
->attr
.abstract
;
7773 /* Check for F08:C628. */
7774 if (allocatable
== 0 && pointer
== 0 && !unlimited
)
7776 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7781 /* Some checks for the SOURCE tag. */
7784 /* Check F03:C631. */
7785 if (!gfc_type_compatible (&e
->ts
, &code
->expr3
->ts
))
7787 gfc_error ("Type of entity at %L is type incompatible with "
7788 "source-expr at %L", &e
->where
, &code
->expr3
->where
);
7792 /* Check F03:C632 and restriction following Note 6.18. */
7793 if (code
->expr3
->rank
> 0 && !conformable_arrays (code
->expr3
, e
))
7796 /* Check F03:C633. */
7797 if (code
->expr3
->ts
.kind
!= e
->ts
.kind
&& !unlimited
)
7799 gfc_error ("The allocate-object at %L and the source-expr at %L "
7800 "shall have the same kind type parameter",
7801 &e
->where
, &code
->expr3
->where
);
7805 /* Check F2008, C642. */
7806 if (code
->expr3
->ts
.type
== BT_DERIVED
7807 && ((codimension
&& gfc_expr_attr (code
->expr3
).lock_comp
)
7808 || (code
->expr3
->ts
.u
.derived
->from_intmod
7809 == INTMOD_ISO_FORTRAN_ENV
7810 && code
->expr3
->ts
.u
.derived
->intmod_sym_id
7811 == ISOFORTRAN_LOCK_TYPE
)))
7813 gfc_error ("The source-expr at %L shall neither be of type "
7814 "LOCK_TYPE nor have a LOCK_TYPE component if "
7815 "allocate-object at %L is a coarray",
7816 &code
->expr3
->where
, &e
->where
);
7820 /* Check TS18508, C702/C703. */
7821 if (code
->expr3
->ts
.type
== BT_DERIVED
7822 && ((codimension
&& gfc_expr_attr (code
->expr3
).event_comp
)
7823 || (code
->expr3
->ts
.u
.derived
->from_intmod
7824 == INTMOD_ISO_FORTRAN_ENV
7825 && code
->expr3
->ts
.u
.derived
->intmod_sym_id
7826 == ISOFORTRAN_EVENT_TYPE
)))
7828 gfc_error ("The source-expr at %L shall neither be of type "
7829 "EVENT_TYPE nor have a EVENT_TYPE component if "
7830 "allocate-object at %L is a coarray",
7831 &code
->expr3
->where
, &e
->where
);
7836 /* Check F08:C629. */
7837 if (is_abstract
&& code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
7840 gcc_assert (e
->ts
.type
== BT_CLASS
);
7841 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7842 "type-spec or source-expr", sym
->name
, &e
->where
);
7846 /* Check F08:C632. */
7847 if (code
->ext
.alloc
.ts
.type
== BT_CHARACTER
&& !e
->ts
.deferred
7848 && !UNLIMITED_POLY (e
))
7852 if (!e
->ts
.u
.cl
->length
)
7855 cmp
= gfc_dep_compare_expr (e
->ts
.u
.cl
->length
,
7856 code
->ext
.alloc
.ts
.u
.cl
->length
);
7857 if (cmp
== 1 || cmp
== -1 || cmp
== -3)
7859 gfc_error ("Allocating %s at %L with type-spec requires the same "
7860 "character-length parameter as in the declaration",
7861 sym
->name
, &e
->where
);
7866 /* In the variable definition context checks, gfc_expr_attr is used
7867 on the expression. This is fooled by the array specification
7868 present in e, thus we have to eliminate that one temporarily. */
7869 e2
= remove_last_array_ref (e
);
7872 t
= gfc_check_vardef_context (e2
, true, true, false,
7873 _("ALLOCATE object"));
7875 t
= gfc_check_vardef_context (e2
, false, true, false,
7876 _("ALLOCATE object"));
7881 if (e
->ts
.type
== BT_CLASS
&& CLASS_DATA (e
)->attr
.dimension
7882 && !code
->expr3
&& code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
7884 /* For class arrays, the initialization with SOURCE is done
7885 using _copy and trans_call. It is convenient to exploit that
7886 when the allocated type is different from the declared type but
7887 no SOURCE exists by setting expr3. */
7888 code
->expr3
= gfc_default_initializer (&code
->ext
.alloc
.ts
);
7890 else if (flag_coarray
!= GFC_FCOARRAY_LIB
&& e
->ts
.type
== BT_DERIVED
7891 && e
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
7892 && e
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
7894 /* We have to zero initialize the integer variable. */
7895 code
->expr3
= gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, 0);
7898 if (e
->ts
.type
== BT_CLASS
&& !unlimited
&& !UNLIMITED_POLY (code
->expr3
))
7900 /* Make sure the vtab symbol is present when
7901 the module variables are generated. */
7902 gfc_typespec ts
= e
->ts
;
7904 ts
= code
->expr3
->ts
;
7905 else if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
7906 ts
= code
->ext
.alloc
.ts
;
7908 /* Finding the vtab also publishes the type's symbol. Therefore this
7909 statement is necessary. */
7910 gfc_find_derived_vtab (ts
.u
.derived
);
7912 else if (unlimited
&& !UNLIMITED_POLY (code
->expr3
))
7914 /* Again, make sure the vtab symbol is present when
7915 the module variables are generated. */
7916 gfc_typespec
*ts
= NULL
;
7918 ts
= &code
->expr3
->ts
;
7920 ts
= &code
->ext
.alloc
.ts
;
7924 /* Finding the vtab also publishes the type's symbol. Therefore this
7925 statement is necessary. */
7929 if (dimension
== 0 && codimension
== 0)
7932 /* Make sure the last reference node is an array specification. */
7934 if (!ref2
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
7935 || (dimension
&& ref2
->u
.ar
.dimen
== 0))
7940 if (!gfc_notify_std (GFC_STD_F2008
, "Array specification required "
7941 "in ALLOCATE statement at %L", &e
->where
))
7943 if (code
->expr3
->rank
!= 0)
7944 *array_alloc_wo_spec
= true;
7947 gfc_error ("Array specification or array-valued SOURCE= "
7948 "expression required in ALLOCATE statement at %L",
7955 gfc_error ("Array specification required in ALLOCATE statement "
7956 "at %L", &e
->where
);
7961 /* Make sure that the array section reference makes sense in the
7962 context of an ALLOCATE specification. */
7967 for (i
= ar
->dimen
; i
< ar
->dimen
+ ar
->codimen
; i
++)
7969 switch (ar
->dimen_type
[i
])
7971 case DIMEN_THIS_IMAGE
:
7972 gfc_error ("Coarray specification required in ALLOCATE statement "
7973 "at %L", &e
->where
);
7977 if (ar
->start
[i
] == 0 || ar
->end
[i
] == 0)
7979 /* If ar->stride[i] is NULL, we issued a previous error. */
7980 if (ar
->stride
[i
] == NULL
)
7981 gfc_error ("Bad array specification in ALLOCATE statement "
7982 "at %L", &e
->where
);
7985 else if (gfc_dep_compare_expr (ar
->start
[i
], ar
->end
[i
]) == 1)
7987 gfc_error ("Upper cobound is less than lower cobound at %L",
7988 &ar
->start
[i
]->where
);
7994 if (ar
->start
[i
]->expr_type
== EXPR_CONSTANT
)
7996 gcc_assert (ar
->start
[i
]->ts
.type
== BT_INTEGER
);
7997 if (mpz_cmp_si (ar
->start
[i
]->value
.integer
, 1) < 0)
7999 gfc_error ("Upper cobound is less than lower cobound "
8000 "of 1 at %L", &ar
->start
[i
]->where
);
8010 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8016 for (i
= 0; i
< ar
->dimen
; i
++)
8018 if (ar
->type
== AR_ELEMENT
|| ar
->type
== AR_FULL
)
8021 switch (ar
->dimen_type
[i
])
8027 if (ar
->start
[i
] != NULL
8028 && ar
->end
[i
] != NULL
8029 && ar
->stride
[i
] == NULL
)
8037 case DIMEN_THIS_IMAGE
:
8038 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8044 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
8046 sym
= a
->expr
->symtree
->n
.sym
;
8048 /* TODO - check derived type components. */
8049 if (gfc_bt_struct (sym
->ts
.type
) || sym
->ts
.type
== BT_CLASS
)
8052 if ((ar
->start
[i
] != NULL
8053 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
8054 || (ar
->end
[i
] != NULL
8055 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
8057 gfc_error ("%qs must not appear in the array specification at "
8058 "%L in the same ALLOCATE statement where it is "
8059 "itself allocated", sym
->name
, &ar
->where
);
8065 for (i
= ar
->dimen
; i
< ar
->codimen
+ ar
->dimen
; i
++)
8067 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
8068 || ar
->dimen_type
[i
] == DIMEN_RANGE
)
8070 if (i
== (ar
->dimen
+ ar
->codimen
- 1))
8072 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8073 "statement at %L", &e
->where
);
8079 if (ar
->dimen_type
[i
] == DIMEN_STAR
&& i
== (ar
->dimen
+ ar
->codimen
- 1)
8080 && ar
->stride
[i
] == NULL
)
8083 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8097 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
8099 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
8100 gfc_alloc
*a
, *p
, *q
;
8103 errmsg
= code
->expr2
;
8105 /* Check the stat variable. */
8108 gfc_check_vardef_context (stat
, false, false, false,
8109 _("STAT variable"));
8111 if ((stat
->ts
.type
!= BT_INTEGER
8112 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
8113 || stat
->ref
->type
== REF_COMPONENT
)))
8115 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8116 "variable", &stat
->where
);
8118 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
8119 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
8121 gfc_ref
*ref1
, *ref2
;
8124 for (ref1
= p
->expr
->ref
, ref2
= stat
->ref
; ref1
&& ref2
;
8125 ref1
= ref1
->next
, ref2
= ref2
->next
)
8127 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
8129 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
8138 gfc_error ("Stat-variable at %L shall not be %sd within "
8139 "the same %s statement", &stat
->where
, fcn
, fcn
);
8145 /* Check the errmsg variable. */
8149 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8152 gfc_check_vardef_context (errmsg
, false, false, false,
8153 _("ERRMSG variable"));
8155 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8156 F18:R930 errmsg-variable is scalar-default-char-variable
8157 F18:R906 default-char-variable is variable
8158 F18:C906 default-char-variable shall be default character. */
8159 if ((errmsg
->ts
.type
!= BT_CHARACTER
8161 && (errmsg
->ref
->type
== REF_ARRAY
8162 || errmsg
->ref
->type
== REF_COMPONENT
)))
8164 || errmsg
->ts
.kind
!= gfc_default_character_kind
)
8165 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8166 "variable", &errmsg
->where
);
8168 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
8169 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
8171 gfc_ref
*ref1
, *ref2
;
8174 for (ref1
= p
->expr
->ref
, ref2
= errmsg
->ref
; ref1
&& ref2
;
8175 ref1
= ref1
->next
, ref2
= ref2
->next
)
8177 if (ref1
->type
!= REF_COMPONENT
|| ref2
->type
!= REF_COMPONENT
)
8179 if (ref1
->u
.c
.component
->name
!= ref2
->u
.c
.component
->name
)
8188 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8189 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
8195 /* Check that an allocate-object appears only once in the statement. */
8197 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
8200 for (q
= p
->next
; q
; q
= q
->next
)
8203 if (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
)
8205 /* This is a potential collision. */
8206 gfc_ref
*pr
= pe
->ref
;
8207 gfc_ref
*qr
= qe
->ref
;
8209 /* Follow the references until
8210 a) They start to differ, in which case there is no error;
8211 you can deallocate a%b and a%c in a single statement
8212 b) Both of them stop, which is an error
8213 c) One of them stops, which is also an error. */
8216 if (pr
== NULL
&& qr
== NULL
)
8218 gfc_error ("Allocate-object at %L also appears at %L",
8219 &pe
->where
, &qe
->where
);
8222 else if (pr
!= NULL
&& qr
== NULL
)
8224 gfc_error ("Allocate-object at %L is subobject of"
8225 " object at %L", &pe
->where
, &qe
->where
);
8228 else if (pr
== NULL
&& qr
!= NULL
)
8230 gfc_error ("Allocate-object at %L is subobject of"
8231 " object at %L", &qe
->where
, &pe
->where
);
8234 /* Here, pr != NULL && qr != NULL */
8235 gcc_assert(pr
->type
== qr
->type
);
8236 if (pr
->type
== REF_ARRAY
)
8238 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8240 gcc_assert (qr
->type
== REF_ARRAY
);
8242 if (pr
->next
&& qr
->next
)
8245 gfc_array_ref
*par
= &(pr
->u
.ar
);
8246 gfc_array_ref
*qar
= &(qr
->u
.ar
);
8248 for (i
=0; i
<par
->dimen
; i
++)
8250 if ((par
->start
[i
] != NULL
8251 || qar
->start
[i
] != NULL
)
8252 && gfc_dep_compare_expr (par
->start
[i
],
8253 qar
->start
[i
]) != 0)
8260 if (pr
->u
.c
.component
->name
!= qr
->u
.c
.component
->name
)
8273 if (strcmp (fcn
, "ALLOCATE") == 0)
8275 bool arr_alloc_wo_spec
= false;
8277 /* Resolving the expr3 in the loop over all objects to allocate would
8278 execute loop invariant code for each loop item. Therefore do it just
8280 if (code
->expr3
&& code
->expr3
->mold
8281 && code
->expr3
->ts
.type
== BT_DERIVED
)
8283 /* Default initialization via MOLD (non-polymorphic). */
8284 gfc_expr
*rhs
= gfc_default_initializer (&code
->expr3
->ts
);
8287 gfc_resolve_expr (rhs
);
8288 gfc_free_expr (code
->expr3
);
8292 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
8293 resolve_allocate_expr (a
->expr
, code
, &arr_alloc_wo_spec
);
8295 if (arr_alloc_wo_spec
&& code
->expr3
)
8297 /* Mark the allocate to have to take the array specification
8299 code
->ext
.alloc
.arr_spec_from_expr3
= 1;
8304 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
8305 resolve_deallocate_expr (a
->expr
);
8310 /************ SELECT CASE resolution subroutines ************/
8312 /* Callback function for our mergesort variant. Determines interval
8313 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8314 op1 > op2. Assumes we're not dealing with the default case.
8315 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8316 There are nine situations to check. */
8319 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
8323 if (op1
->low
== NULL
) /* op1 = (:L) */
8325 /* op2 = (:N), so overlap. */
8327 /* op2 = (M:) or (M:N), L < M */
8328 if (op2
->low
!= NULL
8329 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
8332 else if (op1
->high
== NULL
) /* op1 = (K:) */
8334 /* op2 = (M:), so overlap. */
8336 /* op2 = (:N) or (M:N), K > N */
8337 if (op2
->high
!= NULL
8338 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
8341 else /* op1 = (K:L) */
8343 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
8344 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
8346 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
8347 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
8349 else /* op2 = (M:N) */
8353 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
8356 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
8365 /* Merge-sort a double linked case list, detecting overlap in the
8366 process. LIST is the head of the double linked case list before it
8367 is sorted. Returns the head of the sorted list if we don't see any
8368 overlap, or NULL otherwise. */
8371 check_case_overlap (gfc_case
*list
)
8373 gfc_case
*p
, *q
, *e
, *tail
;
8374 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
8376 /* If the passed list was empty, return immediately. */
8383 /* Loop unconditionally. The only exit from this loop is a return
8384 statement, when we've finished sorting the case list. */
8391 /* Count the number of merges we do in this pass. */
8394 /* Loop while there exists a merge to be done. */
8399 /* Count this merge. */
8402 /* Cut the list in two pieces by stepping INSIZE places
8403 forward in the list, starting from P. */
8406 for (i
= 0; i
< insize
; i
++)
8415 /* Now we have two lists. Merge them! */
8416 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
8418 /* See from which the next case to merge comes from. */
8421 /* P is empty so the next case must come from Q. */
8426 else if (qsize
== 0 || q
== NULL
)
8435 cmp
= compare_cases (p
, q
);
8438 /* The whole case range for P is less than the
8446 /* The whole case range for Q is greater than
8447 the case range for P. */
8454 /* The cases overlap, or they are the same
8455 element in the list. Either way, we must
8456 issue an error and get the next case from P. */
8457 /* FIXME: Sort P and Q by line number. */
8458 gfc_error ("CASE label at %L overlaps with CASE "
8459 "label at %L", &p
->where
, &q
->where
);
8467 /* Add the next element to the merged list. */
8476 /* P has now stepped INSIZE places along, and so has Q. So
8477 they're the same. */
8482 /* If we have done only one merge or none at all, we've
8483 finished sorting the cases. */
8492 /* Otherwise repeat, merging lists twice the size. */
8498 /* Check to see if an expression is suitable for use in a CASE statement.
8499 Makes sure that all case expressions are scalar constants of the same
8500 type. Return false if anything is wrong. */
8503 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
8505 if (e
== NULL
) return true;
8507 if (e
->ts
.type
!= case_expr
->ts
.type
)
8509 gfc_error ("Expression in CASE statement at %L must be of type %s",
8510 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
8514 /* C805 (R808) For a given case-construct, each case-value shall be of
8515 the same type as case-expr. For character type, length differences
8516 are allowed, but the kind type parameters shall be the same. */
8518 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
8520 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8521 &e
->where
, case_expr
->ts
.kind
);
8525 /* Convert the case value kind to that of case expression kind,
8528 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
8529 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
8533 gfc_error ("Expression in CASE statement at %L must be scalar",
8542 /* Given a completely parsed select statement, we:
8544 - Validate all expressions and code within the SELECT.
8545 - Make sure that the selection expression is not of the wrong type.
8546 - Make sure that no case ranges overlap.
8547 - Eliminate unreachable cases and unreachable code resulting from
8548 removing case labels.
8550 The standard does allow unreachable cases, e.g. CASE (5:3). But
8551 they are a hassle for code generation, and to prevent that, we just
8552 cut them out here. This is not necessary for overlapping cases
8553 because they are illegal and we never even try to generate code.
8555 We have the additional caveat that a SELECT construct could have
8556 been a computed GOTO in the source code. Fortunately we can fairly
8557 easily work around that here: The case_expr for a "real" SELECT CASE
8558 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8559 we have to do is make sure that the case_expr is a scalar integer
8563 resolve_select (gfc_code
*code
, bool select_type
)
8566 gfc_expr
*case_expr
;
8567 gfc_case
*cp
, *default_case
, *tail
, *head
;
8568 int seen_unreachable
;
8574 if (code
->expr1
== NULL
)
8576 /* This was actually a computed GOTO statement. */
8577 case_expr
= code
->expr2
;
8578 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
8579 gfc_error ("Selection expression in computed GOTO statement "
8580 "at %L must be a scalar integer expression",
8583 /* Further checking is not necessary because this SELECT was built
8584 by the compiler, so it should always be OK. Just move the
8585 case_expr from expr2 to expr so that we can handle computed
8586 GOTOs as normal SELECTs from here on. */
8587 code
->expr1
= code
->expr2
;
8592 case_expr
= code
->expr1
;
8593 type
= case_expr
->ts
.type
;
8596 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
8598 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8599 &case_expr
->where
, gfc_typename (case_expr
));
8601 /* Punt. Going on here just produce more garbage error messages. */
8606 if (!select_type
&& case_expr
->rank
!= 0)
8608 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8609 "expression", &case_expr
->where
);
8615 /* Raise a warning if an INTEGER case value exceeds the range of
8616 the case-expr. Later, all expressions will be promoted to the
8617 largest kind of all case-labels. */
8619 if (type
== BT_INTEGER
)
8620 for (body
= code
->block
; body
; body
= body
->block
)
8621 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
8624 && gfc_check_integer_range (cp
->low
->value
.integer
,
8625 case_expr
->ts
.kind
) != ARITH_OK
)
8626 gfc_warning (0, "Expression in CASE statement at %L is "
8627 "not in the range of %s", &cp
->low
->where
,
8628 gfc_typename (case_expr
));
8631 && cp
->low
!= cp
->high
8632 && gfc_check_integer_range (cp
->high
->value
.integer
,
8633 case_expr
->ts
.kind
) != ARITH_OK
)
8634 gfc_warning (0, "Expression in CASE statement at %L is "
8635 "not in the range of %s", &cp
->high
->where
,
8636 gfc_typename (case_expr
));
8639 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8640 of the SELECT CASE expression and its CASE values. Walk the lists
8641 of case values, and if we find a mismatch, promote case_expr to
8642 the appropriate kind. */
8644 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
8646 for (body
= code
->block
; body
; body
= body
->block
)
8648 /* Walk the case label list. */
8649 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
8651 /* Intercept the DEFAULT case. It does not have a kind. */
8652 if (cp
->low
== NULL
&& cp
->high
== NULL
)
8655 /* Unreachable case ranges are discarded, so ignore. */
8656 if (cp
->low
!= NULL
&& cp
->high
!= NULL
8657 && cp
->low
!= cp
->high
8658 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
8662 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
8663 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
8665 if (cp
->high
!= NULL
8666 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
8667 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
8672 /* Assume there is no DEFAULT case. */
8673 default_case
= NULL
;
8678 for (body
= code
->block
; body
; body
= body
->block
)
8680 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8682 seen_unreachable
= 0;
8684 /* Walk the case label list, making sure that all case labels
8686 for (cp
= body
->ext
.block
.case_list
; cp
; cp
= cp
->next
)
8688 /* Count the number of cases in the whole construct. */
8691 /* Intercept the DEFAULT case. */
8692 if (cp
->low
== NULL
&& cp
->high
== NULL
)
8694 if (default_case
!= NULL
)
8696 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8697 "by a second DEFAULT CASE at %L",
8698 &default_case
->where
, &cp
->where
);
8709 /* Deal with single value cases and case ranges. Errors are
8710 issued from the validation function. */
8711 if (!validate_case_label_expr (cp
->low
, case_expr
)
8712 || !validate_case_label_expr (cp
->high
, case_expr
))
8718 if (type
== BT_LOGICAL
8719 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
8720 || cp
->low
!= cp
->high
))
8722 gfc_error ("Logical range in CASE statement at %L is not "
8723 "allowed", &cp
->low
->where
);
8728 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
8731 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
8732 if (value
& seen_logical
)
8734 gfc_error ("Constant logical value in CASE statement "
8735 "is repeated at %L",
8740 seen_logical
|= value
;
8743 if (cp
->low
!= NULL
&& cp
->high
!= NULL
8744 && cp
->low
!= cp
->high
8745 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
8747 if (warn_surprising
)
8748 gfc_warning (OPT_Wsurprising
,
8749 "Range specification at %L can never be matched",
8752 cp
->unreachable
= 1;
8753 seen_unreachable
= 1;
8757 /* If the case range can be matched, it can also overlap with
8758 other cases. To make sure it does not, we put it in a
8759 double linked list here. We sort that with a merge sort
8760 later on to detect any overlapping cases. */
8764 head
->right
= head
->left
= NULL
;
8769 tail
->right
->left
= tail
;
8776 /* It there was a failure in the previous case label, give up
8777 for this case label list. Continue with the next block. */
8781 /* See if any case labels that are unreachable have been seen.
8782 If so, we eliminate them. This is a bit of a kludge because
8783 the case lists for a single case statement (label) is a
8784 single forward linked lists. */
8785 if (seen_unreachable
)
8787 /* Advance until the first case in the list is reachable. */
8788 while (body
->ext
.block
.case_list
!= NULL
8789 && body
->ext
.block
.case_list
->unreachable
)
8791 gfc_case
*n
= body
->ext
.block
.case_list
;
8792 body
->ext
.block
.case_list
= body
->ext
.block
.case_list
->next
;
8794 gfc_free_case_list (n
);
8797 /* Strip all other unreachable cases. */
8798 if (body
->ext
.block
.case_list
)
8800 for (cp
= body
->ext
.block
.case_list
; cp
&& cp
->next
; cp
= cp
->next
)
8802 if (cp
->next
->unreachable
)
8804 gfc_case
*n
= cp
->next
;
8805 cp
->next
= cp
->next
->next
;
8807 gfc_free_case_list (n
);
8814 /* See if there were overlapping cases. If the check returns NULL,
8815 there was overlap. In that case we don't do anything. If head
8816 is non-NULL, we prepend the DEFAULT case. The sorted list can
8817 then used during code generation for SELECT CASE constructs with
8818 a case expression of a CHARACTER type. */
8821 head
= check_case_overlap (head
);
8823 /* Prepend the default_case if it is there. */
8824 if (head
!= NULL
&& default_case
)
8826 default_case
->left
= NULL
;
8827 default_case
->right
= head
;
8828 head
->left
= default_case
;
8832 /* Eliminate dead blocks that may be the result if we've seen
8833 unreachable case labels for a block. */
8834 for (body
= code
; body
&& body
->block
; body
= body
->block
)
8836 if (body
->block
->ext
.block
.case_list
== NULL
)
8838 /* Cut the unreachable block from the code chain. */
8839 gfc_code
*c
= body
->block
;
8840 body
->block
= c
->block
;
8842 /* Kill the dead block, but not the blocks below it. */
8844 gfc_free_statements (c
);
8848 /* More than two cases is legal but insane for logical selects.
8849 Issue a warning for it. */
8850 if (warn_surprising
&& type
== BT_LOGICAL
&& ncases
> 2)
8851 gfc_warning (OPT_Wsurprising
,
8852 "Logical SELECT CASE block at %L has more that two cases",
8857 /* Check if a derived type is extensible. */
8860 gfc_type_is_extensible (gfc_symbol
*sym
)
8862 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
8863 || (sym
->attr
.is_class
8864 && sym
->components
->ts
.u
.derived
->attr
.unlimited_polymorphic
));
8869 resolve_types (gfc_namespace
*ns
);
8871 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8872 correct as well as possibly the array-spec. */
8875 resolve_assoc_var (gfc_symbol
* sym
, bool resolve_target
)
8879 gcc_assert (sym
->assoc
);
8880 gcc_assert (sym
->attr
.flavor
== FL_VARIABLE
);
8882 /* If this is for SELECT TYPE, the target may not yet be set. In that
8883 case, return. Resolution will be called later manually again when
8885 target
= sym
->assoc
->target
;
8888 gcc_assert (!sym
->assoc
->dangling
);
8890 if (resolve_target
&& !gfc_resolve_expr (target
))
8893 /* For variable targets, we get some attributes from the target. */
8894 if (target
->expr_type
== EXPR_VARIABLE
)
8896 gfc_symbol
*tsym
, *dsym
;
8898 gcc_assert (target
->symtree
);
8899 tsym
= target
->symtree
->n
.sym
;
8901 if (gfc_expr_attr (target
).proc_pointer
)
8903 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8904 tsym
->name
, &target
->where
);
8908 if (tsym
->attr
.flavor
== FL_PROCEDURE
&& tsym
->generic
8909 && (dsym
= gfc_find_dt_in_generic (tsym
)) != NULL
8910 && dsym
->attr
.flavor
== FL_DERIVED
)
8912 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8913 tsym
->name
, &target
->where
);
8917 if (tsym
->attr
.flavor
== FL_PROCEDURE
)
8919 bool is_error
= true;
8920 if (tsym
->attr
.function
&& tsym
->result
== tsym
)
8921 for (gfc_namespace
*ns
= sym
->ns
; ns
; ns
= ns
->parent
)
8922 if (tsym
== ns
->proc_name
)
8929 gfc_error ("Associating entity %qs at %L is a procedure name",
8930 tsym
->name
, &target
->where
);
8935 sym
->attr
.asynchronous
= tsym
->attr
.asynchronous
;
8936 sym
->attr
.volatile_
= tsym
->attr
.volatile_
;
8938 sym
->attr
.target
= tsym
->attr
.target
8939 || gfc_expr_attr (target
).pointer
;
8940 if (is_subref_array (target
))
8941 sym
->attr
.subref_array_pointer
= 1;
8943 else if (target
->ts
.type
== BT_PROCEDURE
)
8945 gfc_error ("Associating selector-expression at %L yields a procedure",
8950 if (target
->expr_type
== EXPR_NULL
)
8952 gfc_error ("Selector at %L cannot be NULL()", &target
->where
);
8955 else if (target
->ts
.type
== BT_UNKNOWN
)
8957 gfc_error ("Selector at %L has no type", &target
->where
);
8961 /* Get type if this was not already set. Note that it can be
8962 some other type than the target in case this is a SELECT TYPE
8963 selector! So we must not update when the type is already there. */
8964 if (sym
->ts
.type
== BT_UNKNOWN
)
8965 sym
->ts
= target
->ts
;
8967 gcc_assert (sym
->ts
.type
!= BT_UNKNOWN
);
8969 /* See if this is a valid association-to-variable. */
8970 sym
->assoc
->variable
= (target
->expr_type
== EXPR_VARIABLE
8971 && !gfc_has_vector_subscript (target
));
8973 /* Finally resolve if this is an array or not. */
8974 if (sym
->attr
.dimension
&& target
->rank
== 0)
8976 /* primary.c makes the assumption that a reference to an associate
8977 name followed by a left parenthesis is an array reference. */
8978 if (sym
->ts
.type
!= BT_CHARACTER
)
8979 gfc_error ("Associate-name %qs at %L is used as array",
8980 sym
->name
, &sym
->declared_at
);
8981 sym
->attr
.dimension
= 0;
8986 /* We cannot deal with class selectors that need temporaries. */
8987 if (target
->ts
.type
== BT_CLASS
8988 && gfc_ref_needs_temporary_p (target
->ref
))
8990 gfc_error ("CLASS selector at %L needs a temporary which is not "
8991 "yet implemented", &target
->where
);
8995 if (target
->ts
.type
== BT_CLASS
)
8996 gfc_fix_class_refs (target
);
8998 if (target
->rank
!= 0 && !sym
->attr
.select_rank_temporary
)
9001 /* The rank may be incorrectly guessed at parsing, therefore make sure
9002 it is corrected now. */
9003 if (sym
->ts
.type
!= BT_CLASS
&& (!sym
->as
|| sym
->assoc
->rankguessed
))
9006 sym
->as
= gfc_get_array_spec ();
9008 as
->rank
= target
->rank
;
9009 as
->type
= AS_DEFERRED
;
9010 as
->corank
= gfc_get_corank (target
);
9011 sym
->attr
.dimension
= 1;
9012 if (as
->corank
!= 0)
9013 sym
->attr
.codimension
= 1;
9015 else if (sym
->ts
.type
== BT_CLASS
&& (!CLASS_DATA (sym
)->as
|| sym
->assoc
->rankguessed
))
9017 if (!CLASS_DATA (sym
)->as
)
9018 CLASS_DATA (sym
)->as
= gfc_get_array_spec ();
9019 as
= CLASS_DATA (sym
)->as
;
9020 as
->rank
= target
->rank
;
9021 as
->type
= AS_DEFERRED
;
9022 as
->corank
= gfc_get_corank (target
);
9023 CLASS_DATA (sym
)->attr
.dimension
= 1;
9024 if (as
->corank
!= 0)
9025 CLASS_DATA (sym
)->attr
.codimension
= 1;
9028 else if (!sym
->attr
.select_rank_temporary
)
9030 /* target's rank is 0, but the type of the sym is still array valued,
9031 which has to be corrected. */
9032 if (sym
->ts
.type
== BT_CLASS
9033 && CLASS_DATA (sym
) && CLASS_DATA (sym
)->as
)
9036 symbol_attribute attr
;
9037 /* The associated variable's type is still the array type
9038 correct this now. */
9039 gfc_typespec
*ts
= &target
->ts
;
9042 for (ref
= target
->ref
; ref
!= NULL
; ref
= ref
->next
)
9047 ts
= &ref
->u
.c
.component
->ts
;
9050 if (ts
->type
== BT_CLASS
)
9051 ts
= &ts
->u
.derived
->components
->ts
;
9057 /* Create a scalar instance of the current class type. Because the
9058 rank of a class array goes into its name, the type has to be
9059 rebuild. The alternative of (re-)setting just the attributes
9060 and as in the current type, destroys the type also in other
9064 sym
->ts
.type
= BT_CLASS
;
9065 attr
= CLASS_DATA (sym
) ? CLASS_DATA (sym
)->attr
: sym
->attr
;
9067 attr
.associate_var
= 1;
9068 attr
.dimension
= attr
.codimension
= 0;
9069 attr
.class_pointer
= 1;
9070 if (!gfc_build_class_symbol (&sym
->ts
, &attr
, &as
))
9072 /* Make sure the _vptr is set. */
9073 c
= gfc_find_component (sym
->ts
.u
.derived
, "_vptr", true, true, NULL
);
9074 if (c
->ts
.u
.derived
== NULL
)
9075 c
->ts
.u
.derived
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
9076 CLASS_DATA (sym
)->attr
.pointer
= 1;
9077 CLASS_DATA (sym
)->attr
.class_pointer
= 1;
9078 gfc_set_sym_referenced (sym
->ts
.u
.derived
);
9079 gfc_commit_symbol (sym
->ts
.u
.derived
);
9080 /* _vptr now has the _vtab in it, change it to the _vtype. */
9081 if (c
->ts
.u
.derived
->attr
.vtab
)
9082 c
->ts
.u
.derived
= c
->ts
.u
.derived
->ts
.u
.derived
;
9083 c
->ts
.u
.derived
->ns
->types_resolved
= 0;
9084 resolve_types (c
->ts
.u
.derived
->ns
);
9088 /* Mark this as an associate variable. */
9089 sym
->attr
.associate_var
= 1;
9091 /* Fix up the type-spec for CHARACTER types. */
9092 if (sym
->ts
.type
== BT_CHARACTER
&& !sym
->attr
.select_type_temporary
)
9095 sym
->ts
.u
.cl
= target
->ts
.u
.cl
;
9097 if (sym
->ts
.deferred
&& target
->expr_type
== EXPR_VARIABLE
9098 && target
->symtree
->n
.sym
->attr
.dummy
9099 && sym
->ts
.u
.cl
== target
->ts
.u
.cl
)
9101 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, NULL
);
9102 sym
->ts
.deferred
= 1;
9105 if (!sym
->ts
.u
.cl
->length
9106 && !sym
->ts
.deferred
9107 && target
->expr_type
== EXPR_CONSTANT
)
9109 sym
->ts
.u
.cl
->length
=
9110 gfc_get_int_expr (gfc_charlen_int_kind
, NULL
,
9111 target
->value
.character
.length
);
9113 else if ((!sym
->ts
.u
.cl
->length
9114 || sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
9115 && target
->expr_type
!= EXPR_VARIABLE
)
9117 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, NULL
);
9118 sym
->ts
.deferred
= 1;
9120 /* This is reset in trans-stmt.c after the assignment
9121 of the target expression to the associate name. */
9122 sym
->attr
.allocatable
= 1;
9126 /* If the target is a good class object, so is the associate variable. */
9127 if (sym
->ts
.type
== BT_CLASS
&& gfc_expr_attr (target
).class_ok
)
9128 sym
->attr
.class_ok
= 1;
9132 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9133 array reference, where necessary. The symbols are artificial and so
9134 the dimension attribute and arrayspec can also be set. In addition,
9135 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9136 This is corrected here as well.*/
9139 fixup_array_ref (gfc_expr
**expr1
, gfc_expr
*expr2
,
9140 int rank
, gfc_ref
*ref
)
9142 gfc_ref
*nref
= (*expr1
)->ref
;
9143 gfc_symbol
*sym1
= (*expr1
)->symtree
->n
.sym
;
9144 gfc_symbol
*sym2
= expr2
? expr2
->symtree
->n
.sym
: NULL
;
9145 (*expr1
)->rank
= rank
;
9146 if (sym1
->ts
.type
== BT_CLASS
)
9148 if ((*expr1
)->ts
.type
!= BT_CLASS
)
9149 (*expr1
)->ts
= sym1
->ts
;
9151 CLASS_DATA (sym1
)->attr
.dimension
= 1;
9152 if (CLASS_DATA (sym1
)->as
== NULL
&& sym2
)
9153 CLASS_DATA (sym1
)->as
9154 = gfc_copy_array_spec (CLASS_DATA (sym2
)->as
);
9158 sym1
->attr
.dimension
= 1;
9159 if (sym1
->as
== NULL
&& sym2
)
9160 sym1
->as
= gfc_copy_array_spec (sym2
->as
);
9163 for (; nref
; nref
= nref
->next
)
9164 if (nref
->next
== NULL
)
9167 if (ref
&& nref
&& nref
->type
!= REF_ARRAY
)
9168 nref
->next
= gfc_copy_ref (ref
);
9169 else if (ref
&& !nref
)
9170 (*expr1
)->ref
= gfc_copy_ref (ref
);
9175 build_loc_call (gfc_expr
*sym_expr
)
9178 loc_call
= gfc_get_expr ();
9179 loc_call
->expr_type
= EXPR_FUNCTION
;
9180 gfc_get_sym_tree ("_loc", gfc_current_ns
, &loc_call
->symtree
, false);
9181 loc_call
->symtree
->n
.sym
->attr
.flavor
= FL_PROCEDURE
;
9182 loc_call
->symtree
->n
.sym
->attr
.intrinsic
= 1;
9183 loc_call
->symtree
->n
.sym
->result
= loc_call
->symtree
->n
.sym
;
9184 gfc_commit_symbol (loc_call
->symtree
->n
.sym
);
9185 loc_call
->ts
.type
= BT_INTEGER
;
9186 loc_call
->ts
.kind
= gfc_index_integer_kind
;
9187 loc_call
->value
.function
.isym
= gfc_intrinsic_function_by_id (GFC_ISYM_LOC
);
9188 loc_call
->value
.function
.actual
= gfc_get_actual_arglist ();
9189 loc_call
->value
.function
.actual
->expr
= sym_expr
;
9190 loc_call
->where
= sym_expr
->where
;
9194 /* Resolve a SELECT TYPE statement. */
9197 resolve_select_type (gfc_code
*code
, gfc_namespace
*old_ns
)
9199 gfc_symbol
*selector_type
;
9200 gfc_code
*body
, *new_st
, *if_st
, *tail
;
9201 gfc_code
*class_is
= NULL
, *default_case
= NULL
;
9204 char name
[GFC_MAX_SYMBOL_LEN
];
9208 gfc_ref
* ref
= NULL
;
9209 gfc_expr
*selector_expr
= NULL
;
9211 ns
= code
->ext
.block
.ns
;
9214 /* Check for F03:C813. */
9215 if (code
->expr1
->ts
.type
!= BT_CLASS
9216 && !(code
->expr2
&& code
->expr2
->ts
.type
== BT_CLASS
))
9218 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9219 "at %L", &code
->loc
);
9223 if (!code
->expr1
->symtree
->n
.sym
->attr
.class_ok
)
9228 gfc_ref
*ref2
= NULL
;
9229 for (ref
= code
->expr2
->ref
; ref
!= NULL
; ref
= ref
->next
)
9230 if (ref
->type
== REF_COMPONENT
9231 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
9236 if (code
->expr1
->symtree
->n
.sym
->attr
.untyped
)
9237 code
->expr1
->symtree
->n
.sym
->ts
= ref2
->u
.c
.component
->ts
;
9238 selector_type
= CLASS_DATA (ref2
->u
.c
.component
)->ts
.u
.derived
;
9242 if (code
->expr1
->symtree
->n
.sym
->attr
.untyped
)
9243 code
->expr1
->symtree
->n
.sym
->ts
= code
->expr2
->ts
;
9244 selector_type
= CLASS_DATA (code
->expr2
)
9245 ? CLASS_DATA (code
->expr2
)->ts
.u
.derived
: code
->expr2
->ts
.u
.derived
;
9248 if (code
->expr2
->rank
&& CLASS_DATA (code
->expr1
)->as
)
9249 CLASS_DATA (code
->expr1
)->as
->rank
= code
->expr2
->rank
;
9251 /* F2008: C803 The selector expression must not be coindexed. */
9252 if (gfc_is_coindexed (code
->expr2
))
9254 gfc_error ("Selector at %L must not be coindexed",
9255 &code
->expr2
->where
);
9262 selector_type
= CLASS_DATA (code
->expr1
)->ts
.u
.derived
;
9264 if (gfc_is_coindexed (code
->expr1
))
9266 gfc_error ("Selector at %L must not be coindexed",
9267 &code
->expr1
->where
);
9272 /* Loop over TYPE IS / CLASS IS cases. */
9273 for (body
= code
->block
; body
; body
= body
->block
)
9275 c
= body
->ext
.block
.case_list
;
9279 /* Check for repeated cases. */
9280 for (tail
= code
->block
; tail
; tail
= tail
->block
)
9282 gfc_case
*d
= tail
->ext
.block
.case_list
;
9286 if (c
->ts
.type
== d
->ts
.type
9287 && ((c
->ts
.type
== BT_DERIVED
9288 && c
->ts
.u
.derived
&& d
->ts
.u
.derived
9289 && !strcmp (c
->ts
.u
.derived
->name
,
9290 d
->ts
.u
.derived
->name
))
9291 || c
->ts
.type
== BT_UNKNOWN
9292 || (!(c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9293 && c
->ts
.kind
== d
->ts
.kind
)))
9295 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9296 &c
->where
, &d
->where
);
9302 /* Check F03:C815. */
9303 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9304 && !selector_type
->attr
.unlimited_polymorphic
9305 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
9307 gfc_error ("Derived type %qs at %L must be extensible",
9308 c
->ts
.u
.derived
->name
, &c
->where
);
9313 /* Check F03:C816. */
9314 if (c
->ts
.type
!= BT_UNKNOWN
&& !selector_type
->attr
.unlimited_polymorphic
9315 && ((c
->ts
.type
!= BT_DERIVED
&& c
->ts
.type
!= BT_CLASS
)
9316 || !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
)))
9318 if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9319 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9320 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
9322 gfc_error ("Unexpected intrinsic type %qs at %L",
9323 gfc_basic_typename (c
->ts
.type
), &c
->where
);
9328 /* Check F03:C814. */
9329 if (c
->ts
.type
== BT_CHARACTER
9330 && (c
->ts
.u
.cl
->length
!= NULL
|| c
->ts
.deferred
))
9332 gfc_error ("The type-spec at %L shall specify that each length "
9333 "type parameter is assumed", &c
->where
);
9338 /* Intercept the DEFAULT case. */
9339 if (c
->ts
.type
== BT_UNKNOWN
)
9341 /* Check F03:C818. */
9344 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9345 "by a second DEFAULT CASE at %L",
9346 &default_case
->ext
.block
.case_list
->where
, &c
->where
);
9351 default_case
= body
;
9358 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9359 target if present. If there are any EXIT statements referring to the
9360 SELECT TYPE construct, this is no problem because the gfc_code
9361 reference stays the same and EXIT is equally possible from the BLOCK
9362 it is changed to. */
9363 code
->op
= EXEC_BLOCK
;
9366 gfc_association_list
* assoc
;
9368 assoc
= gfc_get_association_list ();
9369 assoc
->st
= code
->expr1
->symtree
;
9370 assoc
->target
= gfc_copy_expr (code
->expr2
);
9371 assoc
->target
->where
= code
->expr2
->where
;
9372 /* assoc->variable will be set by resolve_assoc_var. */
9374 code
->ext
.block
.assoc
= assoc
;
9375 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
9377 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
9380 code
->ext
.block
.assoc
= NULL
;
9382 /* Ensure that the selector rank and arrayspec are available to
9383 correct expressions in which they might be missing. */
9384 if (code
->expr2
&& code
->expr2
->rank
)
9386 rank
= code
->expr2
->rank
;
9387 for (ref
= code
->expr2
->ref
; ref
; ref
= ref
->next
)
9388 if (ref
->next
== NULL
)
9390 if (ref
&& ref
->type
== REF_ARRAY
)
9391 ref
= gfc_copy_ref (ref
);
9393 /* Fixup expr1 if necessary. */
9395 fixup_array_ref (&code
->expr1
, code
->expr2
, rank
, ref
);
9397 else if (code
->expr1
->rank
)
9399 rank
= code
->expr1
->rank
;
9400 for (ref
= code
->expr1
->ref
; ref
; ref
= ref
->next
)
9401 if (ref
->next
== NULL
)
9403 if (ref
&& ref
->type
== REF_ARRAY
)
9404 ref
= gfc_copy_ref (ref
);
9407 /* Add EXEC_SELECT to switch on type. */
9408 new_st
= gfc_get_code (code
->op
);
9409 new_st
->expr1
= code
->expr1
;
9410 new_st
->expr2
= code
->expr2
;
9411 new_st
->block
= code
->block
;
9412 code
->expr1
= code
->expr2
= NULL
;
9417 ns
->code
->next
= new_st
;
9419 code
->op
= EXEC_SELECT_TYPE
;
9421 /* Use the intrinsic LOC function to generate an integer expression
9422 for the vtable of the selector. Note that the rank of the selector
9423 expression has to be set to zero. */
9424 gfc_add_vptr_component (code
->expr1
);
9425 code
->expr1
->rank
= 0;
9426 code
->expr1
= build_loc_call (code
->expr1
);
9427 selector_expr
= code
->expr1
->value
.function
.actual
->expr
;
9429 /* Loop over TYPE IS / CLASS IS cases. */
9430 for (body
= code
->block
; body
; body
= body
->block
)
9434 c
= body
->ext
.block
.case_list
;
9436 /* Generate an index integer expression for address of the
9437 TYPE/CLASS vtable and store it in c->low. The hash expression
9438 is stored in c->high and is used to resolve intrinsic cases. */
9439 if (c
->ts
.type
!= BT_UNKNOWN
)
9441 if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9443 vtab
= gfc_find_derived_vtab (c
->ts
.u
.derived
);
9445 c
->high
= gfc_get_int_expr (gfc_integer_4_kind
, NULL
,
9446 c
->ts
.u
.derived
->hash_value
);
9450 vtab
= gfc_find_vtab (&c
->ts
);
9451 gcc_assert (vtab
&& CLASS_DATA (vtab
)->initializer
);
9452 e
= CLASS_DATA (vtab
)->initializer
;
9453 c
->high
= gfc_copy_expr (e
);
9454 if (c
->high
->ts
.kind
!= gfc_integer_4_kind
)
9457 ts
.kind
= gfc_integer_4_kind
;
9458 ts
.type
= BT_INTEGER
;
9459 gfc_convert_type_warn (c
->high
, &ts
, 2, 0);
9463 e
= gfc_lval_expr_from_sym (vtab
);
9464 c
->low
= build_loc_call (e
);
9469 /* Associate temporary to selector. This should only be done
9470 when this case is actually true, so build a new ASSOCIATE
9471 that does precisely this here (instead of using the
9474 if (c
->ts
.type
== BT_CLASS
)
9475 sprintf (name
, "__tmp_class_%s", c
->ts
.u
.derived
->name
);
9476 else if (c
->ts
.type
== BT_DERIVED
)
9477 sprintf (name
, "__tmp_type_%s", c
->ts
.u
.derived
->name
);
9478 else if (c
->ts
.type
== BT_CHARACTER
)
9480 HOST_WIDE_INT charlen
= 0;
9481 if (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
9482 && c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9483 charlen
= gfc_mpz_get_hwi (c
->ts
.u
.cl
->length
->value
.integer
);
9484 snprintf (name
, sizeof (name
),
9485 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
9486 gfc_basic_typename (c
->ts
.type
), charlen
, c
->ts
.kind
);
9489 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (c
->ts
.type
),
9492 st
= gfc_find_symtree (ns
->sym_root
, name
);
9493 gcc_assert (st
->n
.sym
->assoc
);
9494 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (selector_expr
->symtree
);
9495 st
->n
.sym
->assoc
->target
->where
= selector_expr
->where
;
9496 if (c
->ts
.type
!= BT_CLASS
&& c
->ts
.type
!= BT_UNKNOWN
)
9498 gfc_add_data_component (st
->n
.sym
->assoc
->target
);
9499 /* Fixup the target expression if necessary. */
9501 fixup_array_ref (&st
->n
.sym
->assoc
->target
, NULL
, rank
, ref
);
9504 new_st
= gfc_get_code (EXEC_BLOCK
);
9505 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
9506 new_st
->ext
.block
.ns
->code
= body
->next
;
9507 body
->next
= new_st
;
9509 /* Chain in the new list only if it is marked as dangling. Otherwise
9510 there is a CASE label overlap and this is already used. Just ignore,
9511 the error is diagnosed elsewhere. */
9512 if (st
->n
.sym
->assoc
->dangling
)
9514 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
9515 st
->n
.sym
->assoc
->dangling
= 0;
9518 resolve_assoc_var (st
->n
.sym
, false);
9521 /* Take out CLASS IS cases for separate treatment. */
9523 while (body
&& body
->block
)
9525 if (body
->block
->ext
.block
.case_list
->ts
.type
== BT_CLASS
)
9527 /* Add to class_is list. */
9528 if (class_is
== NULL
)
9530 class_is
= body
->block
;
9535 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
9536 tail
->block
= body
->block
;
9539 /* Remove from EXEC_SELECT list. */
9540 body
->block
= body
->block
->block
;
9553 /* Add a default case to hold the CLASS IS cases. */
9554 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
9555 tail
->block
= gfc_get_code (EXEC_SELECT_TYPE
);
9557 tail
->ext
.block
.case_list
= gfc_get_case ();
9558 tail
->ext
.block
.case_list
->ts
.type
= BT_UNKNOWN
;
9560 default_case
= tail
;
9563 /* More than one CLASS IS block? */
9564 if (class_is
->block
)
9568 /* Sort CLASS IS blocks by extension level. */
9572 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
9575 /* F03:C817 (check for doubles). */
9576 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->hash_value
9577 == c2
->ext
.block
.case_list
->ts
.u
.derived
->hash_value
)
9579 gfc_error ("Double CLASS IS block in SELECT TYPE "
9581 &c2
->ext
.block
.case_list
->where
);
9584 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
9585 < c2
->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
)
9588 (*c1
)->block
= c2
->block
;
9598 /* Generate IF chain. */
9599 if_st
= gfc_get_code (EXEC_IF
);
9601 for (body
= class_is
; body
; body
= body
->block
)
9603 new_st
->block
= gfc_get_code (EXEC_IF
);
9604 new_st
= new_st
->block
;
9605 /* Set up IF condition: Call _gfortran_is_extension_of. */
9606 new_st
->expr1
= gfc_get_expr ();
9607 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
9608 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
9609 new_st
->expr1
->ts
.kind
= 4;
9610 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
9611 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
9612 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
9613 /* Set up arguments. */
9614 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
9615 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (selector_expr
->symtree
);
9616 new_st
->expr1
->value
.function
.actual
->expr
->where
= code
->loc
;
9617 new_st
->expr1
->where
= code
->loc
;
9618 gfc_add_vptr_component (new_st
->expr1
->value
.function
.actual
->expr
);
9619 vtab
= gfc_find_derived_vtab (body
->ext
.block
.case_list
->ts
.u
.derived
);
9620 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
9621 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
9622 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
9623 new_st
->expr1
->value
.function
.actual
->next
->expr
->where
= code
->loc
;
9624 new_st
->next
= body
->next
;
9626 if (default_case
->next
)
9628 new_st
->block
= gfc_get_code (EXEC_IF
);
9629 new_st
= new_st
->block
;
9630 new_st
->next
= default_case
->next
;
9633 /* Replace CLASS DEFAULT code by the IF chain. */
9634 default_case
->next
= if_st
;
9637 /* Resolve the internal code. This cannot be done earlier because
9638 it requires that the sym->assoc of selectors is set already. */
9639 gfc_current_ns
= ns
;
9640 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
9641 gfc_current_ns
= old_ns
;
9648 /* Resolve a SELECT RANK statement. */
9651 resolve_select_rank (gfc_code
*code
, gfc_namespace
*old_ns
)
9654 gfc_code
*body
, *new_st
, *tail
;
9656 char tname
[GFC_MAX_SYMBOL_LEN
+ 7];
9657 char name
[2 * GFC_MAX_SYMBOL_LEN
];
9659 gfc_expr
*selector_expr
= NULL
;
9661 HOST_WIDE_INT charlen
= 0;
9663 ns
= code
->ext
.block
.ns
;
9666 code
->op
= EXEC_BLOCK
;
9669 gfc_association_list
* assoc
;
9671 assoc
= gfc_get_association_list ();
9672 assoc
->st
= code
->expr1
->symtree
;
9673 assoc
->target
= gfc_copy_expr (code
->expr2
);
9674 assoc
->target
->where
= code
->expr2
->where
;
9675 /* assoc->variable will be set by resolve_assoc_var. */
9677 code
->ext
.block
.assoc
= assoc
;
9678 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
9680 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
9683 code
->ext
.block
.assoc
= NULL
;
9685 /* Loop over RANK cases. Note that returning on the errors causes a
9686 cascade of further errors because the case blocks do not compile
9688 for (body
= code
->block
; body
; body
= body
->block
)
9690 c
= body
->ext
.block
.case_list
;
9692 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
9696 /* Check for repeated cases. */
9697 for (tail
= code
->block
; tail
; tail
= tail
->block
)
9699 gfc_case
*d
= tail
->ext
.block
.case_list
;
9705 /* Check F2018: C1153. */
9706 if (!c
->low
&& !d
->low
)
9707 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9708 &c
->where
, &d
->where
);
9710 if (!c
->low
|| !d
->low
)
9713 /* Check F2018: C1153. */
9714 case_value2
= (int) mpz_get_si (d
->low
->value
.integer
);
9715 if ((case_value
== case_value2
) && case_value
== -1)
9716 gfc_error ("RANK (*) at %L is repeated at %L",
9717 &c
->where
, &d
->where
);
9718 else if (case_value
== case_value2
)
9719 gfc_error ("RANK (%i) at %L is repeated at %L",
9720 case_value
, &c
->where
, &d
->where
);
9726 /* Check F2018: C1155. */
9727 if (case_value
== -1 && (gfc_expr_attr (code
->expr1
).allocatable
9728 || gfc_expr_attr (code
->expr1
).pointer
))
9729 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9730 "allocatable selector at %L", &c
->where
, &code
->expr1
->where
);
9732 if (case_value
== -1 && (gfc_expr_attr (code
->expr1
).allocatable
9733 || gfc_expr_attr (code
->expr1
).pointer
))
9734 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9735 "allocatable selector at %L", &c
->where
, &code
->expr1
->where
);
9738 /* Add EXEC_SELECT to switch on rank. */
9739 new_st
= gfc_get_code (code
->op
);
9740 new_st
->expr1
= code
->expr1
;
9741 new_st
->expr2
= code
->expr2
;
9742 new_st
->block
= code
->block
;
9743 code
->expr1
= code
->expr2
= NULL
;
9748 ns
->code
->next
= new_st
;
9750 code
->op
= EXEC_SELECT_RANK
;
9752 selector_expr
= code
->expr1
;
9754 /* Loop over SELECT RANK cases. */
9755 for (body
= code
->block
; body
; body
= body
->block
)
9757 c
= body
->ext
.block
.case_list
;
9760 /* Pass on the default case. */
9764 /* Associate temporary to selector. This should only be done
9765 when this case is actually true, so build a new ASSOCIATE
9766 that does precisely this here (instead of using the
9768 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
9769 && c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9770 charlen
= gfc_mpz_get_hwi (c
->ts
.u
.cl
->length
->value
.integer
);
9772 if (c
->ts
.type
== BT_CLASS
)
9773 sprintf (tname
, "class_%s", c
->ts
.u
.derived
->name
);
9774 else if (c
->ts
.type
== BT_DERIVED
)
9775 sprintf (tname
, "type_%s", c
->ts
.u
.derived
->name
);
9776 else if (c
->ts
.type
!= BT_CHARACTER
)
9777 sprintf (tname
, "%s_%d", gfc_basic_typename (c
->ts
.type
), c
->ts
.kind
);
9779 sprintf (tname
, "%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
9780 gfc_basic_typename (c
->ts
.type
), charlen
, c
->ts
.kind
);
9782 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
9783 if (case_value
>= 0)
9784 sprintf (name
, "__tmp_%s_rank_%d", tname
, case_value
);
9786 sprintf (name
, "__tmp_%s_rank_m%d", tname
, -case_value
);
9788 st
= gfc_find_symtree (ns
->sym_root
, name
);
9789 gcc_assert (st
->n
.sym
->assoc
);
9791 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (selector_expr
->symtree
);
9792 st
->n
.sym
->assoc
->target
->where
= selector_expr
->where
;
9794 new_st
= gfc_get_code (EXEC_BLOCK
);
9795 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
9796 new_st
->ext
.block
.ns
->code
= body
->next
;
9797 body
->next
= new_st
;
9799 /* Chain in the new list only if it is marked as dangling. Otherwise
9800 there is a CASE label overlap and this is already used. Just ignore,
9801 the error is diagnosed elsewhere. */
9802 if (st
->n
.sym
->assoc
->dangling
)
9804 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
9805 st
->n
.sym
->assoc
->dangling
= 0;
9808 resolve_assoc_var (st
->n
.sym
, false);
9811 gfc_current_ns
= ns
;
9812 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
9813 gfc_current_ns
= old_ns
;
9817 /* Resolve a transfer statement. This is making sure that:
9818 -- a derived type being transferred has only non-pointer components
9819 -- a derived type being transferred doesn't have private components, unless
9820 it's being transferred from the module where the type was defined
9821 -- we're not trying to transfer a whole assumed size array. */
9824 resolve_transfer (gfc_code
*code
)
9826 gfc_symbol
*sym
, *derived
;
9830 bool formatted
= false;
9831 gfc_dt
*dt
= code
->ext
.dt
;
9832 gfc_symbol
*dtio_sub
= NULL
;
9836 while (exp
!= NULL
&& exp
->expr_type
== EXPR_OP
9837 && exp
->value
.op
.op
== INTRINSIC_PARENTHESES
)
9838 exp
= exp
->value
.op
.op1
;
9840 if (exp
&& exp
->expr_type
== EXPR_NULL
9843 gfc_error ("Invalid context for NULL () intrinsic at %L",
9848 if (exp
== NULL
|| (exp
->expr_type
!= EXPR_VARIABLE
9849 && exp
->expr_type
!= EXPR_FUNCTION
9850 && exp
->expr_type
!= EXPR_STRUCTURE
))
9853 /* If we are reading, the variable will be changed. Note that
9854 code->ext.dt may be NULL if the TRANSFER is related to
9855 an INQUIRE statement -- but in this case, we are not reading, either. */
9856 if (dt
&& dt
->dt_io_kind
->value
.iokind
== M_READ
9857 && !gfc_check_vardef_context (exp
, false, false, false,
9861 const gfc_typespec
*ts
= exp
->expr_type
== EXPR_STRUCTURE
9862 || exp
->expr_type
== EXPR_FUNCTION
9863 ? &exp
->ts
: &exp
->symtree
->n
.sym
->ts
;
9865 /* Go to actual component transferred. */
9866 for (ref
= exp
->ref
; ref
; ref
= ref
->next
)
9867 if (ref
->type
== REF_COMPONENT
)
9868 ts
= &ref
->u
.c
.component
->ts
;
9870 if (dt
&& dt
->dt_io_kind
->value
.iokind
!= M_INQUIRE
9871 && (ts
->type
== BT_DERIVED
|| ts
->type
== BT_CLASS
))
9873 derived
= ts
->u
.derived
;
9875 /* Determine when to use the formatted DTIO procedure. */
9876 if (dt
&& (dt
->format_expr
|| dt
->format_label
))
9879 write
= dt
->dt_io_kind
->value
.iokind
== M_WRITE
9880 || dt
->dt_io_kind
->value
.iokind
== M_PRINT
;
9881 dtio_sub
= gfc_find_specific_dtio_proc (derived
, write
, formatted
);
9883 if (dtio_sub
!= NULL
&& exp
->expr_type
== EXPR_VARIABLE
)
9886 sym
= exp
->symtree
->n
.sym
->ns
->proc_name
;
9887 /* Check to see if this is a nested DTIO call, with the
9888 dummy as the io-list object. */
9889 if (sym
&& sym
== dtio_sub
&& sym
->formal
9890 && sym
->formal
->sym
== exp
->symtree
->n
.sym
9891 && exp
->ref
== NULL
)
9893 if (!sym
->attr
.recursive
)
9895 gfc_error ("DTIO %s procedure at %L must be recursive",
9896 sym
->name
, &sym
->declared_at
);
9903 if (ts
->type
== BT_CLASS
&& dtio_sub
== NULL
)
9905 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9906 "it is processed by a defined input/output procedure",
9911 if (ts
->type
== BT_DERIVED
)
9913 /* Check that transferred derived type doesn't contain POINTER
9914 components unless it is processed by a defined input/output
9916 if (ts
->u
.derived
->attr
.pointer_comp
&& dtio_sub
== NULL
)
9918 gfc_error ("Data transfer element at %L cannot have POINTER "
9919 "components unless it is processed by a defined "
9920 "input/output procedure", &code
->loc
);
9925 if (ts
->u
.derived
->attr
.proc_pointer_comp
)
9927 gfc_error ("Data transfer element at %L cannot have "
9928 "procedure pointer components", &code
->loc
);
9932 if (ts
->u
.derived
->attr
.alloc_comp
&& dtio_sub
== NULL
)
9934 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9935 "components unless it is processed by a defined "
9936 "input/output procedure", &code
->loc
);
9940 /* C_PTR and C_FUNPTR have private components which means they cannot
9941 be printed. However, if -std=gnu and not -pedantic, allow
9942 the component to be printed to help debugging. */
9943 if (ts
->u
.derived
->ts
.f90_type
== BT_VOID
)
9945 if (!gfc_notify_std (GFC_STD_GNU
, "Data transfer element at %L "
9946 "cannot have PRIVATE components", &code
->loc
))
9949 else if (derived_inaccessible (ts
->u
.derived
) && dtio_sub
== NULL
)
9951 gfc_error ("Data transfer element at %L cannot have "
9952 "PRIVATE components unless it is processed by "
9953 "a defined input/output procedure", &code
->loc
);
9958 if (exp
->expr_type
== EXPR_STRUCTURE
)
9961 sym
= exp
->symtree
->n
.sym
;
9963 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
&& exp
->ref
9964 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
9966 gfc_error ("Data transfer element at %L cannot be a full reference to "
9967 "an assumed-size array", &code
->loc
);
9973 /*********** Toplevel code resolution subroutines ***********/
9975 /* Find the set of labels that are reachable from this block. We also
9976 record the last statement in each block. */
9979 find_reachable_labels (gfc_code
*block
)
9986 cs_base
->reachable_labels
= bitmap_alloc (&labels_obstack
);
9988 /* Collect labels in this block. We don't keep those corresponding
9989 to END {IF|SELECT}, these are checked in resolve_branch by going
9990 up through the code_stack. */
9991 for (c
= block
; c
; c
= c
->next
)
9993 if (c
->here
&& c
->op
!= EXEC_END_NESTED_BLOCK
)
9994 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
9997 /* Merge with labels from parent block. */
10000 gcc_assert (cs_base
->prev
->reachable_labels
);
10001 bitmap_ior_into (cs_base
->reachable_labels
,
10002 cs_base
->prev
->reachable_labels
);
10008 resolve_lock_unlock_event (gfc_code
*code
)
10010 if (code
->expr1
->expr_type
== EXPR_FUNCTION
10011 && code
->expr1
->value
.function
.isym
10012 && code
->expr1
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
10013 remove_caf_get_intrinsic (code
->expr1
);
10015 if ((code
->op
== EXEC_LOCK
|| code
->op
== EXEC_UNLOCK
)
10016 && (code
->expr1
->ts
.type
!= BT_DERIVED
10017 || code
->expr1
->expr_type
!= EXPR_VARIABLE
10018 || code
->expr1
->ts
.u
.derived
->from_intmod
!= INTMOD_ISO_FORTRAN_ENV
10019 || code
->expr1
->ts
.u
.derived
->intmod_sym_id
!= ISOFORTRAN_LOCK_TYPE
10020 || code
->expr1
->rank
!= 0
10021 || (!gfc_is_coarray (code
->expr1
) &&
10022 !gfc_is_coindexed (code
->expr1
))))
10023 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10024 &code
->expr1
->where
);
10025 else if ((code
->op
== EXEC_EVENT_POST
|| code
->op
== EXEC_EVENT_WAIT
)
10026 && (code
->expr1
->ts
.type
!= BT_DERIVED
10027 || code
->expr1
->expr_type
!= EXPR_VARIABLE
10028 || code
->expr1
->ts
.u
.derived
->from_intmod
10029 != INTMOD_ISO_FORTRAN_ENV
10030 || code
->expr1
->ts
.u
.derived
->intmod_sym_id
10031 != ISOFORTRAN_EVENT_TYPE
10032 || code
->expr1
->rank
!= 0))
10033 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10034 &code
->expr1
->where
);
10035 else if (code
->op
== EXEC_EVENT_POST
&& !gfc_is_coarray (code
->expr1
)
10036 && !gfc_is_coindexed (code
->expr1
))
10037 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10038 &code
->expr1
->where
);
10039 else if (code
->op
== EXEC_EVENT_WAIT
&& !gfc_is_coarray (code
->expr1
))
10040 gfc_error ("Event variable argument at %L must be a coarray but not "
10041 "coindexed", &code
->expr1
->where
);
10045 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
10046 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
10047 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10048 &code
->expr2
->where
);
10051 && !gfc_check_vardef_context (code
->expr2
, false, false, false,
10052 _("STAT variable")))
10055 /* Check ERRMSG. */
10057 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
10058 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
10059 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10060 &code
->expr3
->where
);
10063 && !gfc_check_vardef_context (code
->expr3
, false, false, false,
10064 _("ERRMSG variable")))
10067 /* Check for LOCK the ACQUIRED_LOCK. */
10068 if (code
->op
!= EXEC_EVENT_WAIT
&& code
->expr4
10069 && (code
->expr4
->ts
.type
!= BT_LOGICAL
|| code
->expr4
->rank
!= 0
10070 || code
->expr4
->expr_type
!= EXPR_VARIABLE
))
10071 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10072 "variable", &code
->expr4
->where
);
10074 if (code
->op
!= EXEC_EVENT_WAIT
&& code
->expr4
10075 && !gfc_check_vardef_context (code
->expr4
, false, false, false,
10076 _("ACQUIRED_LOCK variable")))
10079 /* Check for EVENT WAIT the UNTIL_COUNT. */
10080 if (code
->op
== EXEC_EVENT_WAIT
&& code
->expr4
)
10082 if (!gfc_resolve_expr (code
->expr4
) || code
->expr4
->ts
.type
!= BT_INTEGER
10083 || code
->expr4
->rank
!= 0)
10084 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10085 "expression", &code
->expr4
->where
);
10091 resolve_critical (gfc_code
*code
)
10093 gfc_symtree
*symtree
;
10094 gfc_symbol
*lock_type
;
10095 char name
[GFC_MAX_SYMBOL_LEN
];
10096 static int serial
= 0;
10098 if (flag_coarray
!= GFC_FCOARRAY_LIB
)
10101 symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
10102 GFC_PREFIX ("lock_type"));
10104 lock_type
= symtree
->n
.sym
;
10107 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns
, &symtree
,
10109 gcc_unreachable ();
10110 lock_type
= symtree
->n
.sym
;
10111 lock_type
->attr
.flavor
= FL_DERIVED
;
10112 lock_type
->attr
.zero_comp
= 1;
10113 lock_type
->from_intmod
= INTMOD_ISO_FORTRAN_ENV
;
10114 lock_type
->intmod_sym_id
= ISOFORTRAN_LOCK_TYPE
;
10117 sprintf(name
, GFC_PREFIX ("lock_var") "%d",serial
++);
10118 if (gfc_get_sym_tree (name
, gfc_current_ns
, &symtree
, false) != 0)
10119 gcc_unreachable ();
10121 code
->resolved_sym
= symtree
->n
.sym
;
10122 symtree
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
10123 symtree
->n
.sym
->attr
.referenced
= 1;
10124 symtree
->n
.sym
->attr
.artificial
= 1;
10125 symtree
->n
.sym
->attr
.codimension
= 1;
10126 symtree
->n
.sym
->ts
.type
= BT_DERIVED
;
10127 symtree
->n
.sym
->ts
.u
.derived
= lock_type
;
10128 symtree
->n
.sym
->as
= gfc_get_array_spec ();
10129 symtree
->n
.sym
->as
->corank
= 1;
10130 symtree
->n
.sym
->as
->type
= AS_EXPLICIT
;
10131 symtree
->n
.sym
->as
->cotype
= AS_EXPLICIT
;
10132 symtree
->n
.sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
,
10134 gfc_commit_symbols();
10139 resolve_sync (gfc_code
*code
)
10141 /* Check imageset. The * case matches expr1 == NULL. */
10144 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
10145 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10146 "INTEGER expression", &code
->expr1
->where
);
10147 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
10148 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
10149 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10150 &code
->expr1
->where
);
10151 else if (code
->expr1
->expr_type
== EXPR_ARRAY
10152 && gfc_simplify_expr (code
->expr1
, 0))
10154 gfc_constructor
*cons
;
10155 cons
= gfc_constructor_first (code
->expr1
->value
.constructor
);
10156 for (; cons
; cons
= gfc_constructor_next (cons
))
10157 if (cons
->expr
->expr_type
== EXPR_CONSTANT
10158 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
10159 gfc_error ("Imageset argument at %L must between 1 and "
10160 "num_images()", &cons
->expr
->where
);
10165 gfc_resolve_expr (code
->expr2
);
10167 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
10168 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
10169 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10170 &code
->expr2
->where
);
10172 /* Check ERRMSG. */
10173 gfc_resolve_expr (code
->expr3
);
10175 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
10176 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
10177 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10178 &code
->expr3
->where
);
10182 /* Given a branch to a label, see if the branch is conforming.
10183 The code node describes where the branch is located. */
10186 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
10193 /* Step one: is this a valid branching target? */
10195 if (label
->defined
== ST_LABEL_UNKNOWN
)
10197 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
10202 if (label
->defined
!= ST_LABEL_TARGET
&& label
->defined
!= ST_LABEL_DO_TARGET
)
10204 gfc_error ("Statement at %L is not a valid branch target statement "
10205 "for the branch statement at %L", &label
->where
, &code
->loc
);
10209 /* Step two: make sure this branch is not a branch to itself ;-) */
10211 if (code
->here
== label
)
10214 "Branch at %L may result in an infinite loop", &code
->loc
);
10218 /* Step three: See if the label is in the same block as the
10219 branching statement. The hard work has been done by setting up
10220 the bitmap reachable_labels. */
10222 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
10224 /* Check now whether there is a CRITICAL construct; if so, check
10225 whether the label is still visible outside of the CRITICAL block,
10226 which is invalid. */
10227 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
10229 if (stack
->current
->op
== EXEC_CRITICAL
10230 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
10231 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10232 "label at %L", &code
->loc
, &label
->where
);
10233 else if (stack
->current
->op
== EXEC_DO_CONCURRENT
10234 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
10235 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10236 "for label at %L", &code
->loc
, &label
->where
);
10242 /* Step four: If we haven't found the label in the bitmap, it may
10243 still be the label of the END of the enclosing block, in which
10244 case we find it by going up the code_stack. */
10246 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
10248 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
10250 if (stack
->current
->op
== EXEC_CRITICAL
)
10252 /* Note: A label at END CRITICAL does not leave the CRITICAL
10253 construct as END CRITICAL is still part of it. */
10254 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10255 " at %L", &code
->loc
, &label
->where
);
10258 else if (stack
->current
->op
== EXEC_DO_CONCURRENT
)
10260 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10261 "label at %L", &code
->loc
, &label
->where
);
10268 gcc_assert (stack
->current
->next
->op
== EXEC_END_NESTED_BLOCK
);
10272 /* The label is not in an enclosing block, so illegal. This was
10273 allowed in Fortran 66, so we allow it as extension. No
10274 further checks are necessary in this case. */
10275 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
10276 "as the GOTO statement at %L", &label
->where
,
10282 /* Check whether EXPR1 has the same shape as EXPR2. */
10285 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
10287 mpz_t shape
[GFC_MAX_DIMENSIONS
];
10288 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
10289 bool result
= false;
10292 /* Compare the rank. */
10293 if (expr1
->rank
!= expr2
->rank
)
10296 /* Compare the size of each dimension. */
10297 for (i
=0; i
<expr1
->rank
; i
++)
10299 if (!gfc_array_dimen_size (expr1
, i
, &shape
[i
]))
10302 if (!gfc_array_dimen_size (expr2
, i
, &shape2
[i
]))
10305 if (mpz_cmp (shape
[i
], shape2
[i
]))
10309 /* When either of the two expression is an assumed size array, we
10310 ignore the comparison of dimension sizes. */
10315 gfc_clear_shape (shape
, i
);
10316 gfc_clear_shape (shape2
, i
);
10321 /* Check whether a WHERE assignment target or a WHERE mask expression
10322 has the same shape as the outmost WHERE mask expression. */
10325 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
10329 gfc_expr
*e
= NULL
;
10331 cblock
= code
->block
;
10333 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10334 In case of nested WHERE, only the outmost one is stored. */
10335 if (mask
== NULL
) /* outmost WHERE */
10337 else /* inner WHERE */
10344 /* Check if the mask-expr has a consistent shape with the
10345 outmost WHERE mask-expr. */
10346 if (!resolve_where_shape (cblock
->expr1
, e
))
10347 gfc_error ("WHERE mask at %L has inconsistent shape",
10348 &cblock
->expr1
->where
);
10351 /* the assignment statement of a WHERE statement, or the first
10352 statement in where-body-construct of a WHERE construct */
10353 cnext
= cblock
->next
;
10358 /* WHERE assignment statement */
10361 /* Check shape consistent for WHERE assignment target. */
10362 if (e
&& !resolve_where_shape (cnext
->expr1
, e
))
10363 gfc_error ("WHERE assignment target at %L has "
10364 "inconsistent shape", &cnext
->expr1
->where
);
10368 case EXEC_ASSIGN_CALL
:
10369 resolve_call (cnext
);
10370 if (!cnext
->resolved_sym
->attr
.elemental
)
10371 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10372 &cnext
->ext
.actual
->expr
->where
);
10375 /* WHERE or WHERE construct is part of a where-body-construct */
10377 resolve_where (cnext
, e
);
10381 gfc_error ("Unsupported statement inside WHERE at %L",
10384 /* the next statement within the same where-body-construct */
10385 cnext
= cnext
->next
;
10387 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10388 cblock
= cblock
->block
;
10393 /* Resolve assignment in FORALL construct.
10394 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10395 FORALL index variables. */
10398 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
10402 for (n
= 0; n
< nvar
; n
++)
10404 gfc_symbol
*forall_index
;
10406 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
10408 /* Check whether the assignment target is one of the FORALL index
10410 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
10411 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
10412 gfc_error ("Assignment to a FORALL index variable at %L",
10413 &code
->expr1
->where
);
10416 /* If one of the FORALL index variables doesn't appear in the
10417 assignment variable, then there could be a many-to-one
10418 assignment. Emit a warning rather than an error because the
10419 mask could be resolving this problem. */
10420 if (!find_forall_index (code
->expr1
, forall_index
, 0))
10421 gfc_warning (0, "The FORALL with index %qs is not used on the "
10422 "left side of the assignment at %L and so might "
10423 "cause multiple assignment to this object",
10424 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
10430 /* Resolve WHERE statement in FORALL construct. */
10433 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
10434 gfc_expr
**var_expr
)
10439 cblock
= code
->block
;
10442 /* the assignment statement of a WHERE statement, or the first
10443 statement in where-body-construct of a WHERE construct */
10444 cnext
= cblock
->next
;
10449 /* WHERE assignment statement */
10451 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
10454 /* WHERE operator assignment statement */
10455 case EXEC_ASSIGN_CALL
:
10456 resolve_call (cnext
);
10457 if (!cnext
->resolved_sym
->attr
.elemental
)
10458 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10459 &cnext
->ext
.actual
->expr
->where
);
10462 /* WHERE or WHERE construct is part of a where-body-construct */
10464 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
10468 gfc_error ("Unsupported statement inside WHERE at %L",
10471 /* the next statement within the same where-body-construct */
10472 cnext
= cnext
->next
;
10474 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10475 cblock
= cblock
->block
;
10480 /* Traverse the FORALL body to check whether the following errors exist:
10481 1. For assignment, check if a many-to-one assignment happens.
10482 2. For WHERE statement, check the WHERE body to see if there is any
10483 many-to-one assignment. */
10486 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
10490 c
= code
->block
->next
;
10496 case EXEC_POINTER_ASSIGN
:
10497 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
10500 case EXEC_ASSIGN_CALL
:
10504 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10505 there is no need to handle it here. */
10509 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
10514 /* The next statement in the FORALL body. */
10520 /* Counts the number of iterators needed inside a forall construct, including
10521 nested forall constructs. This is used to allocate the needed memory
10522 in gfc_resolve_forall. */
10525 gfc_count_forall_iterators (gfc_code
*code
)
10527 int max_iters
, sub_iters
, current_iters
;
10528 gfc_forall_iterator
*fa
;
10530 gcc_assert(code
->op
== EXEC_FORALL
);
10534 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
10537 code
= code
->block
->next
;
10541 if (code
->op
== EXEC_FORALL
)
10543 sub_iters
= gfc_count_forall_iterators (code
);
10544 if (sub_iters
> max_iters
)
10545 max_iters
= sub_iters
;
10550 return current_iters
+ max_iters
;
10554 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10555 gfc_resolve_forall_body to resolve the FORALL body. */
10558 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
10560 static gfc_expr
**var_expr
;
10561 static int total_var
= 0;
10562 static int nvar
= 0;
10563 int i
, old_nvar
, tmp
;
10564 gfc_forall_iterator
*fa
;
10568 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "FORALL construct at %L", &code
->loc
))
10571 /* Start to resolve a FORALL construct */
10572 if (forall_save
== 0)
10574 /* Count the total number of FORALL indices in the nested FORALL
10575 construct in order to allocate the VAR_EXPR with proper size. */
10576 total_var
= gfc_count_forall_iterators (code
);
10578 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10579 var_expr
= XCNEWVEC (gfc_expr
*, total_var
);
10582 /* The information about FORALL iterator, including FORALL indices start, end
10583 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10584 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
10586 /* Fortran 20008: C738 (R753). */
10587 if (fa
->var
->ref
&& fa
->var
->ref
->type
== REF_ARRAY
)
10589 gfc_error ("FORALL index-name at %L must be a scalar variable "
10590 "of type integer", &fa
->var
->where
);
10594 /* Check if any outer FORALL index name is the same as the current
10596 for (i
= 0; i
< nvar
; i
++)
10598 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
10599 gfc_error ("An outer FORALL construct already has an index "
10600 "with this name %L", &fa
->var
->where
);
10603 /* Record the current FORALL index. */
10604 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
10608 /* No memory leak. */
10609 gcc_assert (nvar
<= total_var
);
10612 /* Resolve the FORALL body. */
10613 gfc_resolve_forall_body (code
, nvar
, var_expr
);
10615 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10616 gfc_resolve_blocks (code
->block
, ns
);
10620 /* Free only the VAR_EXPRs allocated in this frame. */
10621 for (i
= nvar
; i
< tmp
; i
++)
10622 gfc_free_expr (var_expr
[i
]);
10626 /* We are in the outermost FORALL construct. */
10627 gcc_assert (forall_save
== 0);
10629 /* VAR_EXPR is not needed any more. */
10636 /* Resolve a BLOCK construct statement. */
10639 resolve_block_construct (gfc_code
* code
)
10641 /* Resolve the BLOCK's namespace. */
10642 gfc_resolve (code
->ext
.block
.ns
);
10644 /* For an ASSOCIATE block, the associations (and their targets) are already
10645 resolved during resolve_symbol. */
10649 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10653 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
10657 for (; b
; b
= b
->block
)
10659 t
= gfc_resolve_expr (b
->expr1
);
10660 if (!gfc_resolve_expr (b
->expr2
))
10666 if (t
&& b
->expr1
!= NULL
10667 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
10668 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10674 && b
->expr1
!= NULL
10675 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
10676 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10681 resolve_branch (b
->label1
, b
);
10685 resolve_block_construct (b
);
10689 case EXEC_SELECT_TYPE
:
10690 case EXEC_SELECT_RANK
:
10693 case EXEC_DO_WHILE
:
10694 case EXEC_DO_CONCURRENT
:
10695 case EXEC_CRITICAL
:
10698 case EXEC_IOLENGTH
:
10702 case EXEC_OMP_ATOMIC
:
10703 case EXEC_OACC_ATOMIC
:
10705 gfc_omp_atomic_op aop
10706 = (gfc_omp_atomic_op
) (b
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
);
10708 /* Verify this before calling gfc_resolve_code, which might
10710 gcc_assert (b
->next
&& b
->next
->op
== EXEC_ASSIGN
);
10711 gcc_assert (((aop
!= GFC_OMP_ATOMIC_CAPTURE
)
10712 && b
->next
->next
== NULL
)
10713 || ((aop
== GFC_OMP_ATOMIC_CAPTURE
)
10714 && b
->next
->next
!= NULL
10715 && b
->next
->next
->op
== EXEC_ASSIGN
10716 && b
->next
->next
->next
== NULL
));
10720 case EXEC_OACC_PARALLEL_LOOP
:
10721 case EXEC_OACC_PARALLEL
:
10722 case EXEC_OACC_KERNELS_LOOP
:
10723 case EXEC_OACC_KERNELS
:
10724 case EXEC_OACC_SERIAL_LOOP
:
10725 case EXEC_OACC_SERIAL
:
10726 case EXEC_OACC_DATA
:
10727 case EXEC_OACC_HOST_DATA
:
10728 case EXEC_OACC_LOOP
:
10729 case EXEC_OACC_UPDATE
:
10730 case EXEC_OACC_WAIT
:
10731 case EXEC_OACC_CACHE
:
10732 case EXEC_OACC_ENTER_DATA
:
10733 case EXEC_OACC_EXIT_DATA
:
10734 case EXEC_OACC_ROUTINE
:
10735 case EXEC_OMP_CRITICAL
:
10736 case EXEC_OMP_DISTRIBUTE
:
10737 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
10738 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
10739 case EXEC_OMP_DISTRIBUTE_SIMD
:
10741 case EXEC_OMP_DO_SIMD
:
10742 case EXEC_OMP_MASTER
:
10743 case EXEC_OMP_ORDERED
:
10744 case EXEC_OMP_PARALLEL
:
10745 case EXEC_OMP_PARALLEL_DO
:
10746 case EXEC_OMP_PARALLEL_DO_SIMD
:
10747 case EXEC_OMP_PARALLEL_SECTIONS
:
10748 case EXEC_OMP_PARALLEL_WORKSHARE
:
10749 case EXEC_OMP_SECTIONS
:
10750 case EXEC_OMP_SIMD
:
10751 case EXEC_OMP_SINGLE
:
10752 case EXEC_OMP_TARGET
:
10753 case EXEC_OMP_TARGET_DATA
:
10754 case EXEC_OMP_TARGET_ENTER_DATA
:
10755 case EXEC_OMP_TARGET_EXIT_DATA
:
10756 case EXEC_OMP_TARGET_PARALLEL
:
10757 case EXEC_OMP_TARGET_PARALLEL_DO
:
10758 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
10759 case EXEC_OMP_TARGET_SIMD
:
10760 case EXEC_OMP_TARGET_TEAMS
:
10761 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
10762 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
10763 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
10764 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
10765 case EXEC_OMP_TARGET_UPDATE
:
10766 case EXEC_OMP_TASK
:
10767 case EXEC_OMP_TASKGROUP
:
10768 case EXEC_OMP_TASKLOOP
:
10769 case EXEC_OMP_TASKLOOP_SIMD
:
10770 case EXEC_OMP_TASKWAIT
:
10771 case EXEC_OMP_TASKYIELD
:
10772 case EXEC_OMP_TEAMS
:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE
:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
10775 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
10776 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
10777 case EXEC_OMP_WORKSHARE
:
10781 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10784 gfc_resolve_code (b
->next
, ns
);
10789 /* Does everything to resolve an ordinary assignment. Returns true
10790 if this is an interface assignment. */
10792 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
10799 symbol_attribute attr
;
10801 if (gfc_extend_assign (code
, ns
))
10805 if (code
->op
== EXEC_ASSIGN_CALL
)
10807 lhs
= code
->ext
.actual
->expr
;
10808 rhsptr
= &code
->ext
.actual
->next
->expr
;
10812 gfc_actual_arglist
* args
;
10813 gfc_typebound_proc
* tbp
;
10815 gcc_assert (code
->op
== EXEC_COMPCALL
);
10817 args
= code
->expr1
->value
.compcall
.actual
;
10819 rhsptr
= &args
->next
->expr
;
10821 tbp
= code
->expr1
->value
.compcall
.tbp
;
10822 gcc_assert (!tbp
->is_generic
);
10825 /* Make a temporary rhs when there is a default initializer
10826 and rhs is the same symbol as the lhs. */
10827 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
10828 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
10829 && gfc_has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
10830 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
10831 *rhsptr
= gfc_get_parentheses (*rhsptr
);
10839 if ((gfc_numeric_ts (&lhs
->ts
) || lhs
->ts
.type
== BT_LOGICAL
)
10840 && rhs
->ts
.type
== BT_CHARACTER
10841 && (rhs
->expr_type
!= EXPR_CONSTANT
|| !flag_dec_char_conversions
))
10843 /* Use of -fdec-char-conversions allows assignment of character data
10844 to non-character variables. This not permited for nonconstant
10846 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs
),
10847 gfc_typename (lhs
), &rhs
->where
);
10851 /* Handle the case of a BOZ literal on the RHS. */
10852 if (rhs
->ts
.type
== BT_BOZ
)
10854 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10855 "statement value nor an actual argument of "
10856 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10860 switch (lhs
->ts
.type
)
10863 if (!gfc_boz2int (rhs
, lhs
->ts
.kind
))
10867 if (!gfc_boz2real (rhs
, lhs
->ts
.kind
))
10871 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs
->where
);
10876 if (lhs
->ts
.type
== BT_CHARACTER
&& warn_character_truncation
)
10878 HOST_WIDE_INT llen
= 0, rlen
= 0;
10879 if (lhs
->ts
.u
.cl
!= NULL
10880 && lhs
->ts
.u
.cl
->length
!= NULL
10881 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
10882 llen
= gfc_mpz_get_hwi (lhs
->ts
.u
.cl
->length
->value
.integer
);
10884 if (rhs
->expr_type
== EXPR_CONSTANT
)
10885 rlen
= rhs
->value
.character
.length
;
10887 else if (rhs
->ts
.u
.cl
!= NULL
10888 && rhs
->ts
.u
.cl
->length
!= NULL
10889 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
10890 rlen
= gfc_mpz_get_hwi (rhs
->ts
.u
.cl
->length
->value
.integer
);
10892 if (rlen
&& llen
&& rlen
> llen
)
10893 gfc_warning_now (OPT_Wcharacter_truncation
,
10894 "CHARACTER expression will be truncated "
10895 "in assignment (%ld/%ld) at %L",
10896 (long) llen
, (long) rlen
, &code
->loc
);
10899 /* Ensure that a vector index expression for the lvalue is evaluated
10900 to a temporary if the lvalue symbol is referenced in it. */
10903 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
10904 if (ref
->type
== REF_ARRAY
)
10906 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
10907 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
10908 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
10909 ref
->u
.ar
.start
[n
]))
10911 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
10915 if (gfc_pure (NULL
))
10917 if (lhs
->ts
.type
== BT_DERIVED
10918 && lhs
->expr_type
== EXPR_VARIABLE
10919 && lhs
->ts
.u
.derived
->attr
.pointer_comp
10920 && rhs
->expr_type
== EXPR_VARIABLE
10921 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
10922 || gfc_is_coindexed (rhs
)))
10924 /* F2008, C1283. */
10925 if (gfc_is_coindexed (rhs
))
10926 gfc_error ("Coindexed expression at %L is assigned to "
10927 "a derived type variable with a POINTER "
10928 "component in a PURE procedure",
10931 /* F2008, C1283 (4). */
10932 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10933 "shall not be used as the expr at %L of an intrinsic "
10934 "assignment statement in which the variable is of a "
10935 "derived type if the derived type has a pointer "
10936 "component at any level of component selection.",
10941 /* Fortran 2008, C1283. */
10942 if (gfc_is_coindexed (lhs
))
10944 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10945 "procedure", &rhs
->where
);
10950 if (gfc_implicit_pure (NULL
))
10952 if (lhs
->expr_type
== EXPR_VARIABLE
10953 && lhs
->symtree
->n
.sym
!= gfc_current_ns
->proc_name
10954 && lhs
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
10955 gfc_unset_implicit_pure (NULL
);
10957 if (lhs
->ts
.type
== BT_DERIVED
10958 && lhs
->expr_type
== EXPR_VARIABLE
10959 && lhs
->ts
.u
.derived
->attr
.pointer_comp
10960 && rhs
->expr_type
== EXPR_VARIABLE
10961 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
10962 || gfc_is_coindexed (rhs
)))
10963 gfc_unset_implicit_pure (NULL
);
10965 /* Fortran 2008, C1283. */
10966 if (gfc_is_coindexed (lhs
))
10967 gfc_unset_implicit_pure (NULL
);
10970 /* F2008, 7.2.1.2. */
10971 attr
= gfc_expr_attr (lhs
);
10972 if (lhs
->ts
.type
== BT_CLASS
&& attr
.allocatable
)
10974 if (attr
.codimension
)
10976 gfc_error ("Assignment to polymorphic coarray at %L is not "
10977 "permitted", &lhs
->where
);
10980 if (!gfc_notify_std (GFC_STD_F2008
, "Assignment to an allocatable "
10981 "polymorphic variable at %L", &lhs
->where
))
10983 if (!flag_realloc_lhs
)
10985 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10986 "requires %<-frealloc-lhs%>", &lhs
->where
);
10990 else if (lhs
->ts
.type
== BT_CLASS
)
10992 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10993 "assignment at %L - check that there is a matching specific "
10994 "subroutine for '=' operator", &lhs
->where
);
10998 bool lhs_coindexed
= gfc_is_coindexed (lhs
);
11000 /* F2008, Section 7.2.1.2. */
11001 if (lhs_coindexed
&& gfc_has_ultimate_allocatable (lhs
))
11003 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11004 "component in assignment at %L", &lhs
->where
);
11008 /* Assign the 'data' of a class object to a derived type. */
11009 if (lhs
->ts
.type
== BT_DERIVED
11010 && rhs
->ts
.type
== BT_CLASS
11011 && rhs
->expr_type
!= EXPR_ARRAY
)
11012 gfc_add_data_component (rhs
);
11014 /* Make sure there is a vtable and, in particular, a _copy for the
11016 if (UNLIMITED_POLY (lhs
) && lhs
->rank
&& rhs
->ts
.type
!= BT_CLASS
)
11017 gfc_find_vtab (&rhs
->ts
);
11019 bool caf_convert_to_send
= flag_coarray
== GFC_FCOARRAY_LIB
11021 || (code
->expr2
->expr_type
== EXPR_FUNCTION
11022 && code
->expr2
->value
.function
.isym
11023 && code
->expr2
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
11024 && (code
->expr1
->rank
== 0 || code
->expr2
->rank
!= 0)
11025 && !gfc_expr_attr (rhs
).allocatable
11026 && !gfc_has_vector_subscript (rhs
)));
11028 gfc_check_assign (lhs
, rhs
, 1, !caf_convert_to_send
);
11030 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11031 Additionally, insert this code when the RHS is a CAF as we then use the
11032 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11033 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11034 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11036 if (caf_convert_to_send
)
11038 if (code
->expr2
->expr_type
== EXPR_FUNCTION
11039 && code
->expr2
->value
.function
.isym
11040 && code
->expr2
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
11041 remove_caf_get_intrinsic (code
->expr2
);
11042 code
->op
= EXEC_CALL
;
11043 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns
, &code
->symtree
, true);
11044 code
->resolved_sym
= code
->symtree
->n
.sym
;
11045 code
->resolved_sym
->attr
.flavor
= FL_PROCEDURE
;
11046 code
->resolved_sym
->attr
.intrinsic
= 1;
11047 code
->resolved_sym
->attr
.subroutine
= 1;
11048 code
->resolved_isym
= gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND
);
11049 gfc_commit_symbol (code
->resolved_sym
);
11050 code
->ext
.actual
= gfc_get_actual_arglist ();
11051 code
->ext
.actual
->expr
= lhs
;
11052 code
->ext
.actual
->next
= gfc_get_actual_arglist ();
11053 code
->ext
.actual
->next
->expr
= rhs
;
11054 code
->expr1
= NULL
;
11055 code
->expr2
= NULL
;
11062 /* Add a component reference onto an expression. */
11065 add_comp_ref (gfc_expr
*e
, gfc_component
*c
)
11070 ref
= &((*ref
)->next
);
11071 *ref
= gfc_get_ref ();
11072 (*ref
)->type
= REF_COMPONENT
;
11073 (*ref
)->u
.c
.sym
= e
->ts
.u
.derived
;
11074 (*ref
)->u
.c
.component
= c
;
11077 /* Add a full array ref, as necessary. */
11080 gfc_add_full_array_ref (e
, c
->as
);
11081 e
->rank
= c
->as
->rank
;
11086 /* Build an assignment. Keep the argument 'op' for future use, so that
11087 pointer assignments can be made. */
11090 build_assignment (gfc_exec_op op
, gfc_expr
*expr1
, gfc_expr
*expr2
,
11091 gfc_component
*comp1
, gfc_component
*comp2
, locus loc
)
11093 gfc_code
*this_code
;
11095 this_code
= gfc_get_code (op
);
11096 this_code
->next
= NULL
;
11097 this_code
->expr1
= gfc_copy_expr (expr1
);
11098 this_code
->expr2
= gfc_copy_expr (expr2
);
11099 this_code
->loc
= loc
;
11100 if (comp1
&& comp2
)
11102 add_comp_ref (this_code
->expr1
, comp1
);
11103 add_comp_ref (this_code
->expr2
, comp2
);
11110 /* Makes a temporary variable expression based on the characteristics of
11111 a given variable expression. */
11114 get_temp_from_expr (gfc_expr
*e
, gfc_namespace
*ns
)
11116 static int serial
= 0;
11117 char name
[GFC_MAX_SYMBOL_LEN
];
11119 gfc_array_spec
*as
;
11120 gfc_array_ref
*aref
;
11123 sprintf (name
, GFC_PREFIX("DA%d"), serial
++);
11124 gfc_get_sym_tree (name
, ns
, &tmp
, false);
11125 gfc_add_type (tmp
->n
.sym
, &e
->ts
, NULL
);
11127 if (e
->expr_type
== EXPR_CONSTANT
&& e
->ts
.type
== BT_CHARACTER
)
11128 tmp
->n
.sym
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_charlen_int_kind
,
11130 e
->value
.character
.length
);
11136 /* Obtain the arrayspec for the temporary. */
11137 if (e
->rank
&& e
->expr_type
!= EXPR_ARRAY
11138 && e
->expr_type
!= EXPR_FUNCTION
11139 && e
->expr_type
!= EXPR_OP
)
11141 aref
= gfc_find_array_ref (e
);
11142 if (e
->expr_type
== EXPR_VARIABLE
11143 && e
->symtree
->n
.sym
->as
== aref
->as
)
11147 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
11148 if (ref
->type
== REF_COMPONENT
11149 && ref
->u
.c
.component
->as
== aref
->as
)
11157 /* Add the attributes and the arrayspec to the temporary. */
11158 tmp
->n
.sym
->attr
= gfc_expr_attr (e
);
11159 tmp
->n
.sym
->attr
.function
= 0;
11160 tmp
->n
.sym
->attr
.result
= 0;
11161 tmp
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
11162 tmp
->n
.sym
->attr
.dummy
= 0;
11163 tmp
->n
.sym
->attr
.intent
= INTENT_UNKNOWN
;
11167 tmp
->n
.sym
->as
= gfc_copy_array_spec (as
);
11170 if (as
->type
== AS_DEFERRED
)
11171 tmp
->n
.sym
->attr
.allocatable
= 1;
11173 else if (e
->rank
&& (e
->expr_type
== EXPR_ARRAY
11174 || e
->expr_type
== EXPR_FUNCTION
11175 || e
->expr_type
== EXPR_OP
))
11177 tmp
->n
.sym
->as
= gfc_get_array_spec ();
11178 tmp
->n
.sym
->as
->type
= AS_DEFERRED
;
11179 tmp
->n
.sym
->as
->rank
= e
->rank
;
11180 tmp
->n
.sym
->attr
.allocatable
= 1;
11181 tmp
->n
.sym
->attr
.dimension
= 1;
11184 tmp
->n
.sym
->attr
.dimension
= 0;
11186 gfc_set_sym_referenced (tmp
->n
.sym
);
11187 gfc_commit_symbol (tmp
->n
.sym
);
11188 e
= gfc_lval_expr_from_sym (tmp
->n
.sym
);
11190 /* Should the lhs be a section, use its array ref for the
11191 temporary expression. */
11192 if (aref
&& aref
->type
!= AR_FULL
)
11194 gfc_free_ref_list (e
->ref
);
11195 e
->ref
= gfc_copy_ref (ref
);
11201 /* Add one line of code to the code chain, making sure that 'head' and
11202 'tail' are appropriately updated. */
11205 add_code_to_chain (gfc_code
**this_code
, gfc_code
**head
, gfc_code
**tail
)
11207 gcc_assert (this_code
);
11209 *head
= *tail
= *this_code
;
11211 *tail
= gfc_append_code (*tail
, *this_code
);
11216 /* Counts the potential number of part array references that would
11217 result from resolution of typebound defined assignments. */
11220 nonscalar_typebound_assign (gfc_symbol
*derived
, int depth
)
11223 int c_depth
= 0, t_depth
;
11225 for (c
= derived
->components
; c
; c
= c
->next
)
11227 if ((!gfc_bt_struct (c
->ts
.type
)
11229 || c
->attr
.allocatable
11230 || c
->attr
.proc_pointer_comp
11231 || c
->attr
.class_pointer
11232 || c
->attr
.proc_pointer
)
11233 && !c
->attr
.defined_assign_comp
)
11236 if (c
->as
&& c_depth
== 0)
11239 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
)
11240 t_depth
= nonscalar_typebound_assign (c
->ts
.u
.derived
,
11245 c_depth
= t_depth
> c_depth
? t_depth
: c_depth
;
11247 return depth
+ c_depth
;
11251 /* Implement 7.2.1.3 of the F08 standard:
11252 "An intrinsic assignment where the variable is of derived type is
11253 performed as if each component of the variable were assigned from the
11254 corresponding component of expr using pointer assignment (7.2.2) for
11255 each pointer component, defined assignment for each nonpointer
11256 nonallocatable component of a type that has a type-bound defined
11257 assignment consistent with the component, intrinsic assignment for
11258 each other nonpointer nonallocatable component, ..."
11260 The pointer assignments are taken care of by the intrinsic
11261 assignment of the structure itself. This function recursively adds
11262 defined assignments where required. The recursion is accomplished
11263 by calling gfc_resolve_code.
11265 When the lhs in a defined assignment has intent INOUT, we need a
11266 temporary for the lhs. In pseudo-code:
11268 ! Only call function lhs once.
11269 if (lhs is not a constant or an variable)
11272 ! Do the intrinsic assignment
11274 ! Now do the defined assignments
11275 do over components with typebound defined assignment [%cmp]
11276 #if one component's assignment procedure is INOUT
11278 #if expr2 non-variable
11284 t1%cmp {defined=} expr2%cmp
11290 expr1%cmp {defined=} expr2%cmp
11294 /* The temporary assignments have to be put on top of the additional
11295 code to avoid the result being changed by the intrinsic assignment.
11297 static int component_assignment_level
= 0;
11298 static gfc_code
*tmp_head
= NULL
, *tmp_tail
= NULL
;
11301 generate_component_assignments (gfc_code
**code
, gfc_namespace
*ns
)
11303 gfc_component
*comp1
, *comp2
;
11304 gfc_code
*this_code
= NULL
, *head
= NULL
, *tail
= NULL
;
11306 int error_count
, depth
;
11308 gfc_get_errors (NULL
, &error_count
);
11310 /* Filter out continuing processing after an error. */
11312 || (*code
)->expr1
->ts
.type
!= BT_DERIVED
11313 || (*code
)->expr2
->ts
.type
!= BT_DERIVED
)
11316 /* TODO: Handle more than one part array reference in assignments. */
11317 depth
= nonscalar_typebound_assign ((*code
)->expr1
->ts
.u
.derived
,
11318 (*code
)->expr1
->rank
? 1 : 0);
11321 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11322 "done because multiple part array references would "
11323 "occur in intermediate expressions.", &(*code
)->loc
);
11327 component_assignment_level
++;
11329 /* Create a temporary so that functions get called only once. */
11330 if ((*code
)->expr2
->expr_type
!= EXPR_VARIABLE
11331 && (*code
)->expr2
->expr_type
!= EXPR_CONSTANT
)
11333 gfc_expr
*tmp_expr
;
11335 /* Assign the rhs to the temporary. */
11336 tmp_expr
= get_temp_from_expr ((*code
)->expr1
, ns
);
11337 this_code
= build_assignment (EXEC_ASSIGN
,
11338 tmp_expr
, (*code
)->expr2
,
11339 NULL
, NULL
, (*code
)->loc
);
11340 /* Add the code and substitute the rhs expression. */
11341 add_code_to_chain (&this_code
, &tmp_head
, &tmp_tail
);
11342 gfc_free_expr ((*code
)->expr2
);
11343 (*code
)->expr2
= tmp_expr
;
11346 /* Do the intrinsic assignment. This is not needed if the lhs is one
11347 of the temporaries generated here, since the intrinsic assignment
11348 to the final result already does this. */
11349 if ((*code
)->expr1
->symtree
->n
.sym
->name
[2] != '@')
11351 this_code
= build_assignment (EXEC_ASSIGN
,
11352 (*code
)->expr1
, (*code
)->expr2
,
11353 NULL
, NULL
, (*code
)->loc
);
11354 add_code_to_chain (&this_code
, &head
, &tail
);
11357 comp1
= (*code
)->expr1
->ts
.u
.derived
->components
;
11358 comp2
= (*code
)->expr2
->ts
.u
.derived
->components
;
11361 for (; comp1
; comp1
= comp1
->next
, comp2
= comp2
->next
)
11363 bool inout
= false;
11365 /* The intrinsic assignment does the right thing for pointers
11366 of all kinds and allocatable components. */
11367 if (!gfc_bt_struct (comp1
->ts
.type
)
11368 || comp1
->attr
.pointer
11369 || comp1
->attr
.allocatable
11370 || comp1
->attr
.proc_pointer_comp
11371 || comp1
->attr
.class_pointer
11372 || comp1
->attr
.proc_pointer
)
11375 /* Make an assigment for this component. */
11376 this_code
= build_assignment (EXEC_ASSIGN
,
11377 (*code
)->expr1
, (*code
)->expr2
,
11378 comp1
, comp2
, (*code
)->loc
);
11380 /* Convert the assignment if there is a defined assignment for
11381 this type. Otherwise, using the call from gfc_resolve_code,
11382 recurse into its components. */
11383 gfc_resolve_code (this_code
, ns
);
11385 if (this_code
->op
== EXEC_ASSIGN_CALL
)
11387 gfc_formal_arglist
*dummy_args
;
11389 /* Check that there is a typebound defined assignment. If not,
11390 then this must be a module defined assignment. We cannot
11391 use the defined_assign_comp attribute here because it must
11392 be this derived type that has the defined assignment and not
11394 if (!(comp1
->ts
.u
.derived
->f2k_derived
11395 && comp1
->ts
.u
.derived
->f2k_derived
11396 ->tb_op
[INTRINSIC_ASSIGN
]))
11398 gfc_free_statements (this_code
);
11403 /* If the first argument of the subroutine has intent INOUT
11404 a temporary must be generated and used instead. */
11405 rsym
= this_code
->resolved_sym
;
11406 dummy_args
= gfc_sym_get_dummy_args (rsym
);
11408 && dummy_args
->sym
->attr
.intent
== INTENT_INOUT
)
11410 gfc_code
*temp_code
;
11413 /* Build the temporary required for the assignment and put
11414 it at the head of the generated code. */
11417 t1
= get_temp_from_expr ((*code
)->expr1
, ns
);
11418 temp_code
= build_assignment (EXEC_ASSIGN
,
11419 t1
, (*code
)->expr1
,
11420 NULL
, NULL
, (*code
)->loc
);
11422 /* For allocatable LHS, check whether it is allocated. Note
11423 that allocatable components with defined assignment are
11424 not yet support. See PR 57696. */
11425 if ((*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
)
11429 gfc_lval_expr_from_sym ((*code
)->expr1
->symtree
->n
.sym
);
11430 block
= gfc_get_code (EXEC_IF
);
11431 block
->block
= gfc_get_code (EXEC_IF
);
11432 block
->block
->expr1
11433 = gfc_build_intrinsic_call (ns
,
11434 GFC_ISYM_ALLOCATED
, "allocated",
11435 (*code
)->loc
, 1, e
);
11436 block
->block
->next
= temp_code
;
11439 add_code_to_chain (&temp_code
, &tmp_head
, &tmp_tail
);
11442 /* Replace the first actual arg with the component of the
11444 gfc_free_expr (this_code
->ext
.actual
->expr
);
11445 this_code
->ext
.actual
->expr
= gfc_copy_expr (t1
);
11446 add_comp_ref (this_code
->ext
.actual
->expr
, comp1
);
11448 /* If the LHS variable is allocatable and wasn't allocated and
11449 the temporary is allocatable, pointer assign the address of
11450 the freshly allocated LHS to the temporary. */
11451 if ((*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
11452 && gfc_expr_attr ((*code
)->expr1
).allocatable
)
11457 cond
= gfc_get_expr ();
11458 cond
->ts
.type
= BT_LOGICAL
;
11459 cond
->ts
.kind
= gfc_default_logical_kind
;
11460 cond
->expr_type
= EXPR_OP
;
11461 cond
->where
= (*code
)->loc
;
11462 cond
->value
.op
.op
= INTRINSIC_NOT
;
11463 cond
->value
.op
.op1
= gfc_build_intrinsic_call (ns
,
11464 GFC_ISYM_ALLOCATED
, "allocated",
11465 (*code
)->loc
, 1, gfc_copy_expr (t1
));
11466 block
= gfc_get_code (EXEC_IF
);
11467 block
->block
= gfc_get_code (EXEC_IF
);
11468 block
->block
->expr1
= cond
;
11469 block
->block
->next
= build_assignment (EXEC_POINTER_ASSIGN
,
11470 t1
, (*code
)->expr1
,
11471 NULL
, NULL
, (*code
)->loc
);
11472 add_code_to_chain (&block
, &head
, &tail
);
11476 else if (this_code
->op
== EXEC_ASSIGN
&& !this_code
->next
)
11478 /* Don't add intrinsic assignments since they are already
11479 effected by the intrinsic assignment of the structure. */
11480 gfc_free_statements (this_code
);
11485 add_code_to_chain (&this_code
, &head
, &tail
);
11489 /* Transfer the value to the final result. */
11490 this_code
= build_assignment (EXEC_ASSIGN
,
11491 (*code
)->expr1
, t1
,
11492 comp1
, comp2
, (*code
)->loc
);
11493 add_code_to_chain (&this_code
, &head
, &tail
);
11497 /* Put the temporary assignments at the top of the generated code. */
11498 if (tmp_head
&& component_assignment_level
== 1)
11500 gfc_append_code (tmp_head
, head
);
11502 tmp_head
= tmp_tail
= NULL
;
11505 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11506 // not accidentally deallocated. Hence, nullify t1.
11507 if (t1
&& (*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
11508 && gfc_expr_attr ((*code
)->expr1
).allocatable
)
11514 e
= gfc_lval_expr_from_sym ((*code
)->expr1
->symtree
->n
.sym
);
11515 cond
= gfc_build_intrinsic_call (ns
, GFC_ISYM_ASSOCIATED
, "associated",
11516 (*code
)->loc
, 2, gfc_copy_expr (t1
), e
);
11517 block
= gfc_get_code (EXEC_IF
);
11518 block
->block
= gfc_get_code (EXEC_IF
);
11519 block
->block
->expr1
= cond
;
11520 block
->block
->next
= build_assignment (EXEC_POINTER_ASSIGN
,
11521 t1
, gfc_get_null_expr (&(*code
)->loc
),
11522 NULL
, NULL
, (*code
)->loc
);
11523 gfc_append_code (tail
, block
);
11527 /* Now attach the remaining code chain to the input code. Step on
11528 to the end of the new code since resolution is complete. */
11529 gcc_assert ((*code
)->op
== EXEC_ASSIGN
);
11530 tail
->next
= (*code
)->next
;
11531 /* Overwrite 'code' because this would place the intrinsic assignment
11532 before the temporary for the lhs is created. */
11533 gfc_free_expr ((*code
)->expr1
);
11534 gfc_free_expr ((*code
)->expr2
);
11540 component_assignment_level
--;
11544 /* F2008: Pointer function assignments are of the form:
11545 ptr_fcn (args) = expr
11546 This function breaks these assignments into two statements:
11547 temporary_pointer => ptr_fcn(args)
11548 temporary_pointer = expr */
11551 resolve_ptr_fcn_assign (gfc_code
**code
, gfc_namespace
*ns
)
11553 gfc_expr
*tmp_ptr_expr
;
11554 gfc_code
*this_code
;
11555 gfc_component
*comp
;
11558 if ((*code
)->expr1
->expr_type
!= EXPR_FUNCTION
)
11561 /* Even if standard does not support this feature, continue to build
11562 the two statements to avoid upsetting frontend_passes.c. */
11563 gfc_notify_std (GFC_STD_F2008
, "Pointer procedure assignment at "
11564 "%L", &(*code
)->loc
);
11566 comp
= gfc_get_proc_ptr_comp ((*code
)->expr1
);
11569 s
= comp
->ts
.interface
;
11571 s
= (*code
)->expr1
->symtree
->n
.sym
;
11573 if (s
== NULL
|| !s
->result
->attr
.pointer
)
11575 gfc_error ("The function result on the lhs of the assignment at "
11576 "%L must have the pointer attribute.",
11577 &(*code
)->expr1
->where
);
11578 (*code
)->op
= EXEC_NOP
;
11582 tmp_ptr_expr
= get_temp_from_expr ((*code
)->expr2
, ns
);
11584 /* get_temp_from_expression is set up for ordinary assignments. To that
11585 end, where array bounds are not known, arrays are made allocatable.
11586 Change the temporary to a pointer here. */
11587 tmp_ptr_expr
->symtree
->n
.sym
->attr
.pointer
= 1;
11588 tmp_ptr_expr
->symtree
->n
.sym
->attr
.allocatable
= 0;
11589 tmp_ptr_expr
->where
= (*code
)->loc
;
11591 this_code
= build_assignment (EXEC_ASSIGN
,
11592 tmp_ptr_expr
, (*code
)->expr2
,
11593 NULL
, NULL
, (*code
)->loc
);
11594 this_code
->next
= (*code
)->next
;
11595 (*code
)->next
= this_code
;
11596 (*code
)->op
= EXEC_POINTER_ASSIGN
;
11597 (*code
)->expr2
= (*code
)->expr1
;
11598 (*code
)->expr1
= tmp_ptr_expr
;
11604 /* Deferred character length assignments from an operator expression
11605 require a temporary because the character length of the lhs can
11606 change in the course of the assignment. */
11609 deferred_op_assign (gfc_code
**code
, gfc_namespace
*ns
)
11611 gfc_expr
*tmp_expr
;
11612 gfc_code
*this_code
;
11614 if (!((*code
)->expr1
->ts
.type
== BT_CHARACTER
11615 && (*code
)->expr1
->ts
.deferred
&& (*code
)->expr1
->rank
11616 && (*code
)->expr2
->expr_type
== EXPR_OP
))
11619 if (!gfc_check_dependency ((*code
)->expr1
, (*code
)->expr2
, 1))
11622 if (gfc_expr_attr ((*code
)->expr1
).pointer
)
11625 tmp_expr
= get_temp_from_expr ((*code
)->expr1
, ns
);
11626 tmp_expr
->where
= (*code
)->loc
;
11628 /* A new charlen is required to ensure that the variable string
11629 length is different to that of the original lhs. */
11630 tmp_expr
->ts
.u
.cl
= gfc_get_charlen();
11631 tmp_expr
->symtree
->n
.sym
->ts
.u
.cl
= tmp_expr
->ts
.u
.cl
;
11632 tmp_expr
->ts
.u
.cl
->next
= (*code
)->expr2
->ts
.u
.cl
->next
;
11633 (*code
)->expr2
->ts
.u
.cl
->next
= tmp_expr
->ts
.u
.cl
;
11635 tmp_expr
->symtree
->n
.sym
->ts
.deferred
= 1;
11637 this_code
= build_assignment (EXEC_ASSIGN
,
11639 gfc_copy_expr (tmp_expr
),
11640 NULL
, NULL
, (*code
)->loc
);
11642 (*code
)->expr1
= tmp_expr
;
11644 this_code
->next
= (*code
)->next
;
11645 (*code
)->next
= this_code
;
11651 /* Given a block of code, recursively resolve everything pointed to by this
11655 gfc_resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
11657 int omp_workshare_save
;
11658 int forall_save
, do_concurrent_save
;
11662 frame
.prev
= cs_base
;
11666 find_reachable_labels (code
);
11668 for (; code
; code
= code
->next
)
11670 frame
.current
= code
;
11671 forall_save
= forall_flag
;
11672 do_concurrent_save
= gfc_do_concurrent_flag
;
11674 if (code
->op
== EXEC_FORALL
)
11677 gfc_resolve_forall (code
, ns
, forall_save
);
11680 else if (code
->block
)
11682 omp_workshare_save
= -1;
11685 case EXEC_OACC_PARALLEL_LOOP
:
11686 case EXEC_OACC_PARALLEL
:
11687 case EXEC_OACC_KERNELS_LOOP
:
11688 case EXEC_OACC_KERNELS
:
11689 case EXEC_OACC_SERIAL_LOOP
:
11690 case EXEC_OACC_SERIAL
:
11691 case EXEC_OACC_DATA
:
11692 case EXEC_OACC_HOST_DATA
:
11693 case EXEC_OACC_LOOP
:
11694 gfc_resolve_oacc_blocks (code
, ns
);
11696 case EXEC_OMP_PARALLEL_WORKSHARE
:
11697 omp_workshare_save
= omp_workshare_flag
;
11698 omp_workshare_flag
= 1;
11699 gfc_resolve_omp_parallel_blocks (code
, ns
);
11701 case EXEC_OMP_PARALLEL
:
11702 case EXEC_OMP_PARALLEL_DO
:
11703 case EXEC_OMP_PARALLEL_DO_SIMD
:
11704 case EXEC_OMP_PARALLEL_SECTIONS
:
11705 case EXEC_OMP_TARGET_PARALLEL
:
11706 case EXEC_OMP_TARGET_PARALLEL_DO
:
11707 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
11708 case EXEC_OMP_TARGET_TEAMS
:
11709 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
11710 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
11711 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
11712 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
11713 case EXEC_OMP_TASK
:
11714 case EXEC_OMP_TASKLOOP
:
11715 case EXEC_OMP_TASKLOOP_SIMD
:
11716 case EXEC_OMP_TEAMS
:
11717 case EXEC_OMP_TEAMS_DISTRIBUTE
:
11718 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
11719 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
11720 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
11721 omp_workshare_save
= omp_workshare_flag
;
11722 omp_workshare_flag
= 0;
11723 gfc_resolve_omp_parallel_blocks (code
, ns
);
11725 case EXEC_OMP_DISTRIBUTE
:
11726 case EXEC_OMP_DISTRIBUTE_SIMD
:
11728 case EXEC_OMP_DO_SIMD
:
11729 case EXEC_OMP_SIMD
:
11730 case EXEC_OMP_TARGET_SIMD
:
11731 gfc_resolve_omp_do_blocks (code
, ns
);
11733 case EXEC_SELECT_TYPE
:
11734 /* Blocks are handled in resolve_select_type because we have
11735 to transform the SELECT TYPE into ASSOCIATE first. */
11737 case EXEC_DO_CONCURRENT
:
11738 gfc_do_concurrent_flag
= 1;
11739 gfc_resolve_blocks (code
->block
, ns
);
11740 gfc_do_concurrent_flag
= 2;
11742 case EXEC_OMP_WORKSHARE
:
11743 omp_workshare_save
= omp_workshare_flag
;
11744 omp_workshare_flag
= 1;
11747 gfc_resolve_blocks (code
->block
, ns
);
11751 if (omp_workshare_save
!= -1)
11752 omp_workshare_flag
= omp_workshare_save
;
11756 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
11757 t
= gfc_resolve_expr (code
->expr1
);
11758 forall_flag
= forall_save
;
11759 gfc_do_concurrent_flag
= do_concurrent_save
;
11761 if (!gfc_resolve_expr (code
->expr2
))
11764 if (code
->op
== EXEC_ALLOCATE
11765 && !gfc_resolve_expr (code
->expr3
))
11771 case EXEC_END_BLOCK
:
11772 case EXEC_END_NESTED_BLOCK
:
11776 case EXEC_ERROR_STOP
:
11778 case EXEC_CONTINUE
:
11780 case EXEC_ASSIGN_CALL
:
11783 case EXEC_CRITICAL
:
11784 resolve_critical (code
);
11787 case EXEC_SYNC_ALL
:
11788 case EXEC_SYNC_IMAGES
:
11789 case EXEC_SYNC_MEMORY
:
11790 resolve_sync (code
);
11795 case EXEC_EVENT_POST
:
11796 case EXEC_EVENT_WAIT
:
11797 resolve_lock_unlock_event (code
);
11800 case EXEC_FAIL_IMAGE
:
11801 case EXEC_FORM_TEAM
:
11802 case EXEC_CHANGE_TEAM
:
11803 case EXEC_END_TEAM
:
11804 case EXEC_SYNC_TEAM
:
11808 /* Keep track of which entry we are up to. */
11809 current_entry_id
= code
->ext
.entry
->id
;
11813 resolve_where (code
, NULL
);
11817 if (code
->expr1
!= NULL
)
11819 if (code
->expr1
->expr_type
!= EXPR_VARIABLE
11820 || code
->expr1
->ts
.type
!= BT_INTEGER
11821 || (code
->expr1
->ref
11822 && code
->expr1
->ref
->type
== REF_ARRAY
)
11823 || code
->expr1
->symtree
== NULL
11824 || (code
->expr1
->symtree
->n
.sym
11825 && (code
->expr1
->symtree
->n
.sym
->attr
.flavor
11827 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11828 "scalar INTEGER variable", &code
->expr1
->where
);
11829 else if (code
->expr1
->symtree
->n
.sym
11830 && code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
11831 gfc_error ("Variable %qs has not been assigned a target "
11832 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
11833 &code
->expr1
->where
);
11836 resolve_branch (code
->label1
, code
);
11840 if (code
->expr1
!= NULL
11841 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
11842 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11843 "INTEGER return specifier", &code
->expr1
->where
);
11846 case EXEC_INIT_ASSIGN
:
11847 case EXEC_END_PROCEDURE
:
11854 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11856 if (code
->expr1
->expr_type
== EXPR_FUNCTION
11857 && code
->expr1
->value
.function
.isym
11858 && code
->expr1
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
11859 remove_caf_get_intrinsic (code
->expr1
);
11861 /* If this is a pointer function in an lvalue variable context,
11862 the new code will have to be resolved afresh. This is also the
11863 case with an error, where the code is transformed into NOP to
11864 prevent ICEs downstream. */
11865 if (resolve_ptr_fcn_assign (&code
, ns
)
11866 || code
->op
== EXEC_NOP
)
11869 if (!gfc_check_vardef_context (code
->expr1
, false, false, false,
11873 if (resolve_ordinary_assign (code
, ns
))
11875 if (code
->op
== EXEC_COMPCALL
)
11881 /* Check for dependencies in deferred character length array
11882 assignments and generate a temporary, if necessary. */
11883 if (code
->op
== EXEC_ASSIGN
&& deferred_op_assign (&code
, ns
))
11886 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11887 if (code
->op
!= EXEC_CALL
&& code
->expr1
->ts
.type
== BT_DERIVED
11888 && code
->expr1
->ts
.u
.derived
11889 && code
->expr1
->ts
.u
.derived
->attr
.defined_assign_comp
)
11890 generate_component_assignments (&code
, ns
);
11894 case EXEC_LABEL_ASSIGN
:
11895 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
11896 gfc_error ("Label %d referenced at %L is never defined",
11897 code
->label1
->value
, &code
->label1
->where
);
11899 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
11900 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
11901 || code
->expr1
->symtree
->n
.sym
->ts
.kind
11902 != gfc_default_integer_kind
11903 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
11904 gfc_error ("ASSIGN statement at %L requires a scalar "
11905 "default INTEGER variable", &code
->expr1
->where
);
11908 case EXEC_POINTER_ASSIGN
:
11915 /* This is both a variable definition and pointer assignment
11916 context, so check both of them. For rank remapping, a final
11917 array ref may be present on the LHS and fool gfc_expr_attr
11918 used in gfc_check_vardef_context. Remove it. */
11919 e
= remove_last_array_ref (code
->expr1
);
11920 t
= gfc_check_vardef_context (e
, true, false, false,
11921 _("pointer assignment"));
11923 t
= gfc_check_vardef_context (e
, false, false, false,
11924 _("pointer assignment"));
11927 t
= gfc_check_pointer_assign (code
->expr1
, code
->expr2
, !t
) && t
;
11932 /* Assigning a class object always is a regular assign. */
11933 if (code
->expr2
->ts
.type
== BT_CLASS
11934 && code
->expr1
->ts
.type
== BT_CLASS
11935 && !CLASS_DATA (code
->expr2
)->attr
.dimension
11936 && !(gfc_expr_attr (code
->expr1
).proc_pointer
11937 && code
->expr2
->expr_type
== EXPR_VARIABLE
11938 && code
->expr2
->symtree
->n
.sym
->attr
.flavor
11940 code
->op
= EXEC_ASSIGN
;
11944 case EXEC_ARITHMETIC_IF
:
11946 gfc_expr
*e
= code
->expr1
;
11948 gfc_resolve_expr (e
);
11949 if (e
->expr_type
== EXPR_NULL
)
11950 gfc_error ("Invalid NULL at %L", &e
->where
);
11952 if (t
&& (e
->rank
> 0
11953 || !(e
->ts
.type
== BT_REAL
|| e
->ts
.type
== BT_INTEGER
)))
11954 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11955 "REAL or INTEGER expression", &e
->where
);
11957 resolve_branch (code
->label1
, code
);
11958 resolve_branch (code
->label2
, code
);
11959 resolve_branch (code
->label3
, code
);
11964 if (t
&& code
->expr1
!= NULL
11965 && (code
->expr1
->ts
.type
!= BT_LOGICAL
11966 || code
->expr1
->rank
!= 0))
11967 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11968 &code
->expr1
->where
);
11973 resolve_call (code
);
11976 case EXEC_COMPCALL
:
11978 resolve_typebound_subroutine (code
);
11981 case EXEC_CALL_PPC
:
11982 resolve_ppc_call (code
);
11986 /* Select is complicated. Also, a SELECT construct could be
11987 a transformed computed GOTO. */
11988 resolve_select (code
, false);
11991 case EXEC_SELECT_TYPE
:
11992 resolve_select_type (code
, ns
);
11995 case EXEC_SELECT_RANK
:
11996 resolve_select_rank (code
, ns
);
12000 resolve_block_construct (code
);
12004 if (code
->ext
.iterator
!= NULL
)
12006 gfc_iterator
*iter
= code
->ext
.iterator
;
12007 if (gfc_resolve_iterator (iter
, true, false))
12008 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
,
12013 case EXEC_DO_WHILE
:
12014 if (code
->expr1
== NULL
)
12015 gfc_internal_error ("gfc_resolve_code(): No expression on "
12018 && (code
->expr1
->rank
!= 0
12019 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
12020 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12021 "a scalar LOGICAL expression", &code
->expr1
->where
);
12024 case EXEC_ALLOCATE
:
12026 resolve_allocate_deallocate (code
, "ALLOCATE");
12030 case EXEC_DEALLOCATE
:
12032 resolve_allocate_deallocate (code
, "DEALLOCATE");
12037 if (!gfc_resolve_open (code
->ext
.open
, &code
->loc
))
12040 resolve_branch (code
->ext
.open
->err
, code
);
12044 if (!gfc_resolve_close (code
->ext
.close
, &code
->loc
))
12047 resolve_branch (code
->ext
.close
->err
, code
);
12050 case EXEC_BACKSPACE
:
12054 if (!gfc_resolve_filepos (code
->ext
.filepos
, &code
->loc
))
12057 resolve_branch (code
->ext
.filepos
->err
, code
);
12061 if (!gfc_resolve_inquire (code
->ext
.inquire
))
12064 resolve_branch (code
->ext
.inquire
->err
, code
);
12067 case EXEC_IOLENGTH
:
12068 gcc_assert (code
->ext
.inquire
!= NULL
);
12069 if (!gfc_resolve_inquire (code
->ext
.inquire
))
12072 resolve_branch (code
->ext
.inquire
->err
, code
);
12076 if (!gfc_resolve_wait (code
->ext
.wait
))
12079 resolve_branch (code
->ext
.wait
->err
, code
);
12080 resolve_branch (code
->ext
.wait
->end
, code
);
12081 resolve_branch (code
->ext
.wait
->eor
, code
);
12086 if (!gfc_resolve_dt (code
, code
->ext
.dt
, &code
->loc
))
12089 resolve_branch (code
->ext
.dt
->err
, code
);
12090 resolve_branch (code
->ext
.dt
->end
, code
);
12091 resolve_branch (code
->ext
.dt
->eor
, code
);
12094 case EXEC_TRANSFER
:
12095 resolve_transfer (code
);
12098 case EXEC_DO_CONCURRENT
:
12100 resolve_forall_iterators (code
->ext
.forall_iterator
);
12102 if (code
->expr1
!= NULL
12103 && (code
->expr1
->ts
.type
!= BT_LOGICAL
|| code
->expr1
->rank
))
12104 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12105 "expression", &code
->expr1
->where
);
12108 case EXEC_OACC_PARALLEL_LOOP
:
12109 case EXEC_OACC_PARALLEL
:
12110 case EXEC_OACC_KERNELS_LOOP
:
12111 case EXEC_OACC_KERNELS
:
12112 case EXEC_OACC_SERIAL_LOOP
:
12113 case EXEC_OACC_SERIAL
:
12114 case EXEC_OACC_DATA
:
12115 case EXEC_OACC_HOST_DATA
:
12116 case EXEC_OACC_LOOP
:
12117 case EXEC_OACC_UPDATE
:
12118 case EXEC_OACC_WAIT
:
12119 case EXEC_OACC_CACHE
:
12120 case EXEC_OACC_ENTER_DATA
:
12121 case EXEC_OACC_EXIT_DATA
:
12122 case EXEC_OACC_ATOMIC
:
12123 case EXEC_OACC_DECLARE
:
12124 gfc_resolve_oacc_directive (code
, ns
);
12127 case EXEC_OMP_ATOMIC
:
12128 case EXEC_OMP_BARRIER
:
12129 case EXEC_OMP_CANCEL
:
12130 case EXEC_OMP_CANCELLATION_POINT
:
12131 case EXEC_OMP_CRITICAL
:
12132 case EXEC_OMP_FLUSH
:
12133 case EXEC_OMP_DISTRIBUTE
:
12134 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
12135 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
12136 case EXEC_OMP_DISTRIBUTE_SIMD
:
12138 case EXEC_OMP_DO_SIMD
:
12139 case EXEC_OMP_MASTER
:
12140 case EXEC_OMP_ORDERED
:
12141 case EXEC_OMP_SECTIONS
:
12142 case EXEC_OMP_SIMD
:
12143 case EXEC_OMP_SINGLE
:
12144 case EXEC_OMP_TARGET
:
12145 case EXEC_OMP_TARGET_DATA
:
12146 case EXEC_OMP_TARGET_ENTER_DATA
:
12147 case EXEC_OMP_TARGET_EXIT_DATA
:
12148 case EXEC_OMP_TARGET_PARALLEL
:
12149 case EXEC_OMP_TARGET_PARALLEL_DO
:
12150 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
12151 case EXEC_OMP_TARGET_SIMD
:
12152 case EXEC_OMP_TARGET_TEAMS
:
12153 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
12154 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
12155 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
12156 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
12157 case EXEC_OMP_TARGET_UPDATE
:
12158 case EXEC_OMP_TASK
:
12159 case EXEC_OMP_TASKGROUP
:
12160 case EXEC_OMP_TASKLOOP
:
12161 case EXEC_OMP_TASKLOOP_SIMD
:
12162 case EXEC_OMP_TASKWAIT
:
12163 case EXEC_OMP_TASKYIELD
:
12164 case EXEC_OMP_TEAMS
:
12165 case EXEC_OMP_TEAMS_DISTRIBUTE
:
12166 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
12167 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
12168 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
12169 case EXEC_OMP_WORKSHARE
:
12170 gfc_resolve_omp_directive (code
, ns
);
12173 case EXEC_OMP_PARALLEL
:
12174 case EXEC_OMP_PARALLEL_DO
:
12175 case EXEC_OMP_PARALLEL_DO_SIMD
:
12176 case EXEC_OMP_PARALLEL_SECTIONS
:
12177 case EXEC_OMP_PARALLEL_WORKSHARE
:
12178 omp_workshare_save
= omp_workshare_flag
;
12179 omp_workshare_flag
= 0;
12180 gfc_resolve_omp_directive (code
, ns
);
12181 omp_workshare_flag
= omp_workshare_save
;
12185 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12189 cs_base
= frame
.prev
;
12193 /* Resolve initial values and make sure they are compatible with
12197 resolve_values (gfc_symbol
*sym
)
12201 if (sym
->value
== NULL
)
12204 if (sym
->value
->expr_type
== EXPR_STRUCTURE
)
12205 t
= resolve_structure_cons (sym
->value
, 1);
12207 t
= gfc_resolve_expr (sym
->value
);
12212 gfc_check_assign_symbol (sym
, NULL
, sym
->value
);
12216 /* Verify any BIND(C) derived types in the namespace so we can report errors
12217 for them once, rather than for each variable declared of that type. */
12220 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
12222 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
12223 && derived_sym
->attr
.is_bind_c
== 1)
12224 verify_bind_c_derived_type (derived_sym
);
12230 /* Check the interfaces of DTIO procedures associated with derived
12231 type 'sym'. These procedures can either have typebound bindings or
12232 can appear in DTIO generic interfaces. */
12235 gfc_verify_DTIO_procedures (gfc_symbol
*sym
)
12237 if (!sym
|| sym
->attr
.flavor
!= FL_DERIVED
)
12240 gfc_check_dtio_interfaces (sym
);
12245 /* Verify that any binding labels used in a given namespace do not collide
12246 with the names or binding labels of any global symbols. Multiple INTERFACE
12247 for the same procedure are permitted. */
12250 gfc_verify_binding_labels (gfc_symbol
*sym
)
12253 const char *module
;
12255 if (!sym
|| !sym
->attr
.is_bind_c
|| sym
->attr
.is_iso_c
12256 || sym
->attr
.flavor
== FL_DERIVED
|| !sym
->binding_label
)
12259 gsym
= gfc_find_case_gsymbol (gfc_gsym_root
, sym
->binding_label
);
12262 module
= sym
->module
;
12263 else if (sym
->ns
&& sym
->ns
->proc_name
12264 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
12265 module
= sym
->ns
->proc_name
->name
;
12266 else if (sym
->ns
&& sym
->ns
->parent
12267 && sym
->ns
&& sym
->ns
->parent
->proc_name
12268 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
12269 module
= sym
->ns
->parent
->proc_name
->name
;
12275 && (gsym
->type
== GSYM_FUNCTION
|| gsym
->type
== GSYM_SUBROUTINE
)))
12278 gsym
= gfc_get_gsymbol (sym
->binding_label
, true);
12279 gsym
->where
= sym
->declared_at
;
12280 gsym
->sym_name
= sym
->name
;
12281 gsym
->binding_label
= sym
->binding_label
;
12282 gsym
->ns
= sym
->ns
;
12283 gsym
->mod_name
= module
;
12284 if (sym
->attr
.function
)
12285 gsym
->type
= GSYM_FUNCTION
;
12286 else if (sym
->attr
.subroutine
)
12287 gsym
->type
= GSYM_SUBROUTINE
;
12288 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12289 gsym
->defined
= sym
->attr
.if_source
!= IFSRC_IFBODY
;
12293 if (sym
->attr
.flavor
== FL_VARIABLE
&& gsym
->type
!= GSYM_UNKNOWN
)
12295 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12296 "identifier as entity at %L", sym
->name
,
12297 sym
->binding_label
, &sym
->declared_at
, &gsym
->where
);
12298 /* Clear the binding label to prevent checking multiple times. */
12299 sym
->binding_label
= NULL
;
12303 if (sym
->attr
.flavor
== FL_VARIABLE
&& module
12304 && (strcmp (module
, gsym
->mod_name
) != 0
12305 || strcmp (sym
->name
, gsym
->sym_name
) != 0))
12307 /* This can only happen if the variable is defined in a module - if it
12308 isn't the same module, reject it. */
12309 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12310 "uses the same global identifier as entity at %L from module %qs",
12311 sym
->name
, module
, sym
->binding_label
,
12312 &sym
->declared_at
, &gsym
->where
, gsym
->mod_name
);
12313 sym
->binding_label
= NULL
;
12317 if ((sym
->attr
.function
|| sym
->attr
.subroutine
)
12318 && ((gsym
->type
!= GSYM_SUBROUTINE
&& gsym
->type
!= GSYM_FUNCTION
)
12319 || (gsym
->defined
&& sym
->attr
.if_source
!= IFSRC_IFBODY
))
12320 && (sym
!= gsym
->ns
->proc_name
&& sym
->attr
.entry
== 0)
12321 && (module
!= gsym
->mod_name
12322 || strcmp (gsym
->sym_name
, sym
->name
) != 0
12323 || (module
&& strcmp (module
, gsym
->mod_name
) != 0)))
12325 /* Print an error if the procedure is defined multiple times; we have to
12326 exclude references to the same procedure via module association or
12327 multiple checks for the same procedure. */
12328 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12329 "global identifier as entity at %L", sym
->name
,
12330 sym
->binding_label
, &sym
->declared_at
, &gsym
->where
);
12331 sym
->binding_label
= NULL
;
12336 /* Resolve an index expression. */
12339 resolve_index_expr (gfc_expr
*e
)
12341 if (!gfc_resolve_expr (e
))
12344 if (!gfc_simplify_expr (e
, 0))
12347 if (!gfc_specification_expr (e
))
12354 /* Resolve a charlen structure. */
12357 resolve_charlen (gfc_charlen
*cl
)
12360 bool saved_specification_expr
;
12366 saved_specification_expr
= specification_expr
;
12367 specification_expr
= true;
12369 if (cl
->length_from_typespec
)
12371 if (!gfc_resolve_expr (cl
->length
))
12373 specification_expr
= saved_specification_expr
;
12377 if (!gfc_simplify_expr (cl
->length
, 0))
12379 specification_expr
= saved_specification_expr
;
12383 /* cl->length has been resolved. It should have an integer type. */
12384 if (cl
->length
->ts
.type
!= BT_INTEGER
|| cl
->length
->rank
!= 0)
12386 gfc_error ("Scalar INTEGER expression expected at %L",
12387 &cl
->length
->where
);
12393 if (!resolve_index_expr (cl
->length
))
12395 specification_expr
= saved_specification_expr
;
12400 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12401 a negative value, the length of character entities declared is zero. */
12402 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
12403 && mpz_sgn (cl
->length
->value
.integer
) < 0)
12404 gfc_replace_expr (cl
->length
,
12405 gfc_get_int_expr (gfc_charlen_int_kind
, NULL
, 0));
12407 /* Check that the character length is not too large. */
12408 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
12409 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
12410 && cl
->length
->ts
.type
== BT_INTEGER
12411 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
12413 gfc_error ("String length at %L is too large", &cl
->length
->where
);
12414 specification_expr
= saved_specification_expr
;
12418 specification_expr
= saved_specification_expr
;
12423 /* Test for non-constant shape arrays. */
12426 is_non_constant_shape_array (gfc_symbol
*sym
)
12432 not_constant
= false;
12433 if (sym
->as
!= NULL
)
12435 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12436 has not been simplified; parameter array references. Do the
12437 simplification now. */
12438 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
12440 if (i
== GFC_MAX_DIMENSIONS
)
12443 e
= sym
->as
->lower
[i
];
12444 if (e
&& (!resolve_index_expr(e
)
12445 || !gfc_is_constant_expr (e
)))
12446 not_constant
= true;
12447 e
= sym
->as
->upper
[i
];
12448 if (e
&& (!resolve_index_expr(e
)
12449 || !gfc_is_constant_expr (e
)))
12450 not_constant
= true;
12453 return not_constant
;
12456 /* Given a symbol and an initialization expression, add code to initialize
12457 the symbol to the function entry. */
12459 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
12463 gfc_namespace
*ns
= sym
->ns
;
12465 /* Search for the function namespace if this is a contained
12466 function without an explicit result. */
12467 if (sym
->attr
.function
&& sym
== sym
->result
12468 && sym
->name
!= sym
->ns
->proc_name
->name
)
12470 ns
= ns
->contained
;
12471 for (;ns
; ns
= ns
->sibling
)
12472 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
12478 gfc_free_expr (init
);
12482 /* Build an l-value expression for the result. */
12483 lval
= gfc_lval_expr_from_sym (sym
);
12485 /* Add the code at scope entry. */
12486 init_st
= gfc_get_code (EXEC_INIT_ASSIGN
);
12487 init_st
->next
= ns
->code
;
12488 ns
->code
= init_st
;
12490 /* Assign the default initializer to the l-value. */
12491 init_st
->loc
= sym
->declared_at
;
12492 init_st
->expr1
= lval
;
12493 init_st
->expr2
= init
;
12497 /* Whether or not we can generate a default initializer for a symbol. */
12500 can_generate_init (gfc_symbol
*sym
)
12502 symbol_attribute
*a
;
12507 /* These symbols should never have a default initialization. */
12512 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
12513 && (CLASS_DATA (sym
)->attr
.class_pointer
12514 || CLASS_DATA (sym
)->attr
.proc_pointer
))
12515 || a
->in_equivalence
12522 || (!a
->referenced
&& !a
->result
)
12523 || (a
->dummy
&& a
->intent
!= INTENT_OUT
)
12524 || (a
->function
&& sym
!= sym
->result
)
12529 /* Assign the default initializer to a derived type variable or result. */
12532 apply_default_init (gfc_symbol
*sym
)
12534 gfc_expr
*init
= NULL
;
12536 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
12539 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
12540 init
= gfc_generate_initializer (&sym
->ts
, can_generate_init (sym
));
12542 if (init
== NULL
&& sym
->ts
.type
!= BT_CLASS
)
12545 build_init_assign (sym
, init
);
12546 sym
->attr
.referenced
= 1;
12550 /* Build an initializer for a local. Returns null if the symbol should not have
12551 a default initialization. */
12554 build_default_init_expr (gfc_symbol
*sym
)
12556 /* These symbols should never have a default initialization. */
12557 if (sym
->attr
.allocatable
12558 || sym
->attr
.external
12560 || sym
->attr
.pointer
12561 || sym
->attr
.in_equivalence
12562 || sym
->attr
.in_common
12565 || sym
->attr
.cray_pointee
12566 || sym
->attr
.cray_pointer
12570 /* Get the appropriate init expression. */
12571 return gfc_build_default_init_expr (&sym
->ts
, &sym
->declared_at
);
12574 /* Add an initialization expression to a local variable. */
12576 apply_default_init_local (gfc_symbol
*sym
)
12578 gfc_expr
*init
= NULL
;
12580 /* The symbol should be a variable or a function return value. */
12581 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
12582 || (sym
->attr
.function
&& sym
->result
!= sym
))
12585 /* Try to build the initializer expression. If we can't initialize
12586 this symbol, then init will be NULL. */
12587 init
= build_default_init_expr (sym
);
12591 /* For saved variables, we don't want to add an initializer at function
12592 entry, so we just add a static initializer. Note that automatic variables
12593 are stack allocated even with -fno-automatic; we have also to exclude
12594 result variable, which are also nonstatic. */
12595 if (!sym
->attr
.automatic
12596 && (sym
->attr
.save
|| sym
->ns
->save_all
12597 || (flag_max_stack_var_size
== 0 && !sym
->attr
.result
12598 && (sym
->ns
->proc_name
&& !sym
->ns
->proc_name
->attr
.recursive
)
12599 && (!sym
->attr
.dimension
|| !is_non_constant_shape_array (sym
)))))
12601 /* Don't clobber an existing initializer! */
12602 gcc_assert (sym
->value
== NULL
);
12607 build_init_assign (sym
, init
);
12611 /* Resolution of common features of flavors variable and procedure. */
12614 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
12616 gfc_array_spec
*as
;
12618 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
12619 as
= CLASS_DATA (sym
)->as
;
12623 /* Constraints on deferred shape variable. */
12624 if (as
== NULL
|| as
->type
!= AS_DEFERRED
)
12626 bool pointer
, allocatable
, dimension
;
12628 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
12630 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
12631 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
12632 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
12636 pointer
= sym
->attr
.pointer
&& !sym
->attr
.select_type_temporary
;
12637 allocatable
= sym
->attr
.allocatable
;
12638 dimension
= sym
->attr
.dimension
;
12643 if (dimension
&& as
->type
!= AS_ASSUMED_RANK
)
12645 gfc_error ("Allocatable array %qs at %L must have a deferred "
12646 "shape or assumed rank", sym
->name
, &sym
->declared_at
);
12649 else if (!gfc_notify_std (GFC_STD_F2003
, "Scalar object "
12650 "%qs at %L may not be ALLOCATABLE",
12651 sym
->name
, &sym
->declared_at
))
12655 if (pointer
&& dimension
&& as
->type
!= AS_ASSUMED_RANK
)
12657 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12658 "assumed rank", sym
->name
, &sym
->declared_at
);
12665 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
12666 && sym
->ts
.type
!= BT_CLASS
&& !sym
->assoc
)
12668 gfc_error ("Array %qs at %L cannot have a deferred shape",
12669 sym
->name
, &sym
->declared_at
);
12674 /* Constraints on polymorphic variables. */
12675 if (sym
->ts
.type
== BT_CLASS
&& !(sym
->result
&& sym
->result
!= sym
))
12678 if (sym
->attr
.class_ok
12679 && !sym
->attr
.select_type_temporary
12680 && !UNLIMITED_POLY (sym
)
12681 && !gfc_type_is_extensible (CLASS_DATA (sym
)->ts
.u
.derived
))
12683 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12684 CLASS_DATA (sym
)->ts
.u
.derived
->name
, sym
->name
,
12685 &sym
->declared_at
);
12690 /* Assume that use associated symbols were checked in the module ns.
12691 Class-variables that are associate-names are also something special
12692 and excepted from the test. */
12693 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
&& !sym
->assoc
)
12695 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12696 "or pointer", sym
->name
, &sym
->declared_at
);
12705 /* Additional checks for symbols with flavor variable and derived
12706 type. To be called from resolve_fl_variable. */
12709 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
12711 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
12713 /* Check to see if a derived type is blocked from being host
12714 associated by the presence of another class I symbol in the same
12715 namespace. 14.6.1.3 of the standard and the discussion on
12716 comp.lang.fortran. */
12717 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
12718 && !sym
->ts
.u
.derived
->attr
.use_assoc
12719 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
12722 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
12723 if (s
&& s
->attr
.generic
)
12724 s
= gfc_find_dt_in_generic (s
);
12725 if (s
&& !gfc_fl_struct (s
->attr
.flavor
))
12727 gfc_error ("The type %qs cannot be host associated at %L "
12728 "because it is blocked by an incompatible object "
12729 "of the same name declared at %L",
12730 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
12736 /* 4th constraint in section 11.3: "If an object of a type for which
12737 component-initialization is specified (R429) appears in the
12738 specification-part of a module and does not have the ALLOCATABLE
12739 or POINTER attribute, the object shall have the SAVE attribute."
12741 The check for initializers is performed with
12742 gfc_has_default_initializer because gfc_default_initializer generates
12743 a hidden default for allocatable components. */
12744 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
12745 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12746 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
) && !sym
->attr
.save
12747 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
12748 && gfc_has_default_initializer (sym
->ts
.u
.derived
)
12749 && !gfc_notify_std (GFC_STD_F2008
, "Implied SAVE for module variable "
12750 "%qs at %L, needed due to the default "
12751 "initialization", sym
->name
, &sym
->declared_at
))
12754 /* Assign default initializer. */
12755 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
12756 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
12757 sym
->value
= gfc_generate_initializer (&sym
->ts
, can_generate_init (sym
));
12763 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12764 except in the declaration of an entity or component that has the POINTER
12765 or ALLOCATABLE attribute. */
12768 deferred_requirements (gfc_symbol
*sym
)
12770 if (sym
->ts
.deferred
12771 && !(sym
->attr
.pointer
12772 || sym
->attr
.allocatable
12773 || sym
->attr
.associate_var
12774 || sym
->attr
.omp_udr_artificial_var
))
12776 /* If a function has a result variable, only check the variable. */
12777 if (sym
->result
&& sym
->name
!= sym
->result
->name
)
12780 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12781 "requires either the POINTER or ALLOCATABLE attribute",
12782 sym
->name
, &sym
->declared_at
);
12789 /* Resolve symbols with flavor variable. */
12792 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
12794 const char *auto_save_msg
= "Automatic object %qs at %L cannot have the "
12797 if (!resolve_fl_var_and_proc (sym
, mp_flag
))
12800 /* Set this flag to check that variables are parameters of all entries.
12801 This check is effected by the call to gfc_resolve_expr through
12802 is_non_constant_shape_array. */
12803 bool saved_specification_expr
= specification_expr
;
12804 specification_expr
= true;
12806 if (sym
->ns
->proc_name
12807 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12808 || sym
->ns
->proc_name
->attr
.is_main_program
)
12809 && !sym
->attr
.use_assoc
12810 && !sym
->attr
.allocatable
12811 && !sym
->attr
.pointer
12812 && is_non_constant_shape_array (sym
))
12814 /* F08:C541. The shape of an array defined in a main program or module
12815 * needs to be constant. */
12816 gfc_error ("The module or main program array %qs at %L must "
12817 "have constant shape", sym
->name
, &sym
->declared_at
);
12818 specification_expr
= saved_specification_expr
;
12822 /* Constraints on deferred type parameter. */
12823 if (!deferred_requirements (sym
))
12826 if (sym
->ts
.type
== BT_CHARACTER
&& !sym
->attr
.associate_var
)
12828 /* Make sure that character string variables with assumed length are
12829 dummy arguments. */
12830 gfc_expr
*e
= NULL
;
12833 e
= sym
->ts
.u
.cl
->length
;
12837 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
12838 && !sym
->ts
.deferred
&& !sym
->attr
.select_type_temporary
12839 && !sym
->attr
.omp_udr_artificial_var
)
12841 gfc_error ("Entity with assumed character length at %L must be a "
12842 "dummy argument or a PARAMETER", &sym
->declared_at
);
12843 specification_expr
= saved_specification_expr
;
12847 if (e
&& sym
->attr
.save
== SAVE_EXPLICIT
&& !gfc_is_constant_expr (e
))
12849 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
12850 specification_expr
= saved_specification_expr
;
12854 if (!gfc_is_constant_expr (e
)
12855 && !(e
->expr_type
== EXPR_VARIABLE
12856 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
))
12858 if (!sym
->attr
.use_assoc
&& sym
->ns
->proc_name
12859 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12860 || sym
->ns
->proc_name
->attr
.is_main_program
))
12862 gfc_error ("%qs at %L must have constant character length "
12863 "in this context", sym
->name
, &sym
->declared_at
);
12864 specification_expr
= saved_specification_expr
;
12867 if (sym
->attr
.in_common
)
12869 gfc_error ("COMMON variable %qs at %L must have constant "
12870 "character length", sym
->name
, &sym
->declared_at
);
12871 specification_expr
= saved_specification_expr
;
12877 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
12878 apply_default_init_local (sym
); /* Try to apply a default initialization. */
12880 /* Determine if the symbol may not have an initializer. */
12881 int no_init_flag
= 0, automatic_flag
= 0;
12882 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
12883 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
12885 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
12886 && is_non_constant_shape_array (sym
))
12888 no_init_flag
= automatic_flag
= 1;
12890 /* Also, they must not have the SAVE attribute.
12891 SAVE_IMPLICIT is checked below. */
12892 if (sym
->as
&& sym
->attr
.codimension
)
12894 int corank
= sym
->as
->corank
;
12895 sym
->as
->corank
= 0;
12896 no_init_flag
= automatic_flag
= is_non_constant_shape_array (sym
);
12897 sym
->as
->corank
= corank
;
12899 if (automatic_flag
&& sym
->attr
.save
== SAVE_EXPLICIT
)
12901 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
12902 specification_expr
= saved_specification_expr
;
12907 /* Ensure that any initializer is simplified. */
12909 gfc_simplify_expr (sym
->value
, 1);
12911 /* Reject illegal initializers. */
12912 if (!sym
->mark
&& sym
->value
)
12914 if (sym
->attr
.allocatable
|| (sym
->ts
.type
== BT_CLASS
12915 && CLASS_DATA (sym
)->attr
.allocatable
))
12916 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12917 sym
->name
, &sym
->declared_at
);
12918 else if (sym
->attr
.external
)
12919 gfc_error ("External %qs at %L cannot have an initializer",
12920 sym
->name
, &sym
->declared_at
);
12921 else if (sym
->attr
.dummy
12922 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
12923 gfc_error ("Dummy %qs at %L cannot have an initializer",
12924 sym
->name
, &sym
->declared_at
);
12925 else if (sym
->attr
.intrinsic
)
12926 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12927 sym
->name
, &sym
->declared_at
);
12928 else if (sym
->attr
.result
)
12929 gfc_error ("Function result %qs at %L cannot have an initializer",
12930 sym
->name
, &sym
->declared_at
);
12931 else if (automatic_flag
)
12932 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12933 sym
->name
, &sym
->declared_at
);
12935 goto no_init_error
;
12936 specification_expr
= saved_specification_expr
;
12941 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
12943 bool res
= resolve_fl_variable_derived (sym
, no_init_flag
);
12944 specification_expr
= saved_specification_expr
;
12948 specification_expr
= saved_specification_expr
;
12953 /* Compare the dummy characteristics of a module procedure interface
12954 declaration with the corresponding declaration in a submodule. */
12955 static gfc_formal_arglist
*new_formal
;
12956 static char errmsg
[200];
12959 compare_fsyms (gfc_symbol
*sym
)
12963 if (sym
== NULL
|| new_formal
== NULL
)
12966 fsym
= new_formal
->sym
;
12971 if (strcmp (sym
->name
, fsym
->name
) == 0)
12973 if (!gfc_check_dummy_characteristics (fsym
, sym
, true, errmsg
, 200))
12974 gfc_error ("%s at %L", errmsg
, &fsym
->declared_at
);
12979 /* Resolve a procedure. */
12982 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
12984 gfc_formal_arglist
*arg
;
12986 if (sym
->attr
.function
12987 && !resolve_fl_var_and_proc (sym
, mp_flag
))
12990 /* Constraints on deferred type parameter. */
12991 if (!deferred_requirements (sym
))
12994 if (sym
->ts
.type
== BT_CHARACTER
)
12996 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
12998 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
12999 && !resolve_charlen (cl
))
13002 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
13003 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
13005 gfc_error ("Character-valued statement function %qs at %L must "
13006 "have constant length", sym
->name
, &sym
->declared_at
);
13011 /* Ensure that derived type for are not of a private type. Internal
13012 module procedures are excluded by 2.2.3.3 - i.e., they are not
13013 externally accessible and can access all the objects accessible in
13015 if (!(sym
->ns
->parent
&& sym
->ns
->parent
->proc_name
13016 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
13017 && gfc_check_symbol_access (sym
))
13019 gfc_interface
*iface
;
13021 for (arg
= gfc_sym_get_dummy_args (sym
); arg
; arg
= arg
->next
)
13024 && arg
->sym
->ts
.type
== BT_DERIVED
13025 && arg
->sym
->ts
.u
.derived
13026 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
13027 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
13028 && !gfc_notify_std (GFC_STD_F2003
, "%qs is of a PRIVATE type "
13029 "and cannot be a dummy argument"
13030 " of %qs, which is PUBLIC at %L",
13031 arg
->sym
->name
, sym
->name
,
13032 &sym
->declared_at
))
13034 /* Stop this message from recurring. */
13035 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
13040 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13041 PRIVATE to the containing module. */
13042 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
13044 for (arg
= gfc_sym_get_dummy_args (iface
->sym
); arg
; arg
= arg
->next
)
13047 && arg
->sym
->ts
.type
== BT_DERIVED
13048 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
13049 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
13050 && !gfc_notify_std (GFC_STD_F2003
, "Procedure %qs in "
13051 "PUBLIC interface %qs at %L "
13052 "takes dummy arguments of %qs which "
13053 "is PRIVATE", iface
->sym
->name
,
13054 sym
->name
, &iface
->sym
->declared_at
,
13055 gfc_typename(&arg
->sym
->ts
)))
13057 /* Stop this message from recurring. */
13058 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
13065 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
13066 && !sym
->attr
.proc_pointer
)
13068 gfc_error ("Function %qs at %L cannot have an initializer",
13069 sym
->name
, &sym
->declared_at
);
13071 /* Make sure no second error is issued for this. */
13072 sym
->value
->error
= 1;
13076 /* An external symbol may not have an initializer because it is taken to be
13077 a procedure. Exception: Procedure Pointers. */
13078 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
13080 gfc_error ("External object %qs at %L may not have an initializer",
13081 sym
->name
, &sym
->declared_at
);
13085 /* An elemental function is required to return a scalar 12.7.1 */
13086 if (sym
->attr
.elemental
&& sym
->attr
.function
13087 && (sym
->as
|| (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->as
)))
13089 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13090 "result", sym
->name
, &sym
->declared_at
);
13091 /* Reset so that the error only occurs once. */
13092 sym
->attr
.elemental
= 0;
13096 if (sym
->attr
.proc
== PROC_ST_FUNCTION
13097 && (sym
->attr
.allocatable
|| sym
->attr
.pointer
))
13099 gfc_error ("Statement function %qs at %L may not have pointer or "
13100 "allocatable attribute", sym
->name
, &sym
->declared_at
);
13104 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13105 char-len-param shall not be array-valued, pointer-valued, recursive
13106 or pure. ....snip... A character value of * may only be used in the
13107 following ways: (i) Dummy arg of procedure - dummy associates with
13108 actual length; (ii) To declare a named constant; or (iii) External
13109 function - but length must be declared in calling scoping unit. */
13110 if (sym
->attr
.function
13111 && sym
->ts
.type
== BT_CHARACTER
&& !sym
->ts
.deferred
13112 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
13114 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
13115 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
13117 if (sym
->as
&& sym
->as
->rank
)
13118 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13119 "array-valued", sym
->name
, &sym
->declared_at
);
13121 if (sym
->attr
.pointer
)
13122 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13123 "pointer-valued", sym
->name
, &sym
->declared_at
);
13125 if (sym
->attr
.pure
)
13126 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13127 "pure", sym
->name
, &sym
->declared_at
);
13129 if (sym
->attr
.recursive
)
13130 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13131 "recursive", sym
->name
, &sym
->declared_at
);
13136 /* Appendix B.2 of the standard. Contained functions give an
13137 error anyway. Deferred character length is an F2003 feature.
13138 Don't warn on intrinsic conversion functions, which start
13139 with two underscores. */
13140 if (!sym
->attr
.contained
&& !sym
->ts
.deferred
13141 && (sym
->name
[0] != '_' || sym
->name
[1] != '_'))
13142 gfc_notify_std (GFC_STD_F95_OBS
,
13143 "CHARACTER(*) function %qs at %L",
13144 sym
->name
, &sym
->declared_at
);
13147 /* F2008, C1218. */
13148 if (sym
->attr
.elemental
)
13150 if (sym
->attr
.proc_pointer
)
13152 const char* name
= (sym
->attr
.result
? sym
->ns
->proc_name
->name
13154 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13155 name
, &sym
->declared_at
);
13158 if (sym
->attr
.dummy
)
13160 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13161 sym
->name
, &sym
->declared_at
);
13166 /* F2018, C15100: "The result of an elemental function shall be scalar,
13167 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13168 pointer is tested and caught elsewhere. */
13169 if (sym
->attr
.elemental
&& sym
->result
13170 && (sym
->result
->attr
.allocatable
|| sym
->result
->attr
.pointer
))
13172 gfc_error ("Function result variable %qs at %L of elemental "
13173 "function %qs shall not have an ALLOCATABLE or POINTER "
13174 "attribute", sym
->result
->name
,
13175 &sym
->result
->declared_at
, sym
->name
);
13179 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
13181 gfc_formal_arglist
*curr_arg
;
13182 int has_non_interop_arg
= 0;
13184 if (!verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
13185 sym
->common_block
))
13187 /* Clear these to prevent looking at them again if there was an
13189 sym
->attr
.is_bind_c
= 0;
13190 sym
->attr
.is_c_interop
= 0;
13191 sym
->ts
.is_c_interop
= 0;
13195 /* So far, no errors have been found. */
13196 sym
->attr
.is_c_interop
= 1;
13197 sym
->ts
.is_c_interop
= 1;
13200 curr_arg
= gfc_sym_get_dummy_args (sym
);
13201 while (curr_arg
!= NULL
)
13203 /* Skip implicitly typed dummy args here. */
13204 if (curr_arg
->sym
&& curr_arg
->sym
->attr
.implicit_type
== 0)
13205 if (!gfc_verify_c_interop_param (curr_arg
->sym
))
13206 /* If something is found to fail, record the fact so we
13207 can mark the symbol for the procedure as not being
13208 BIND(C) to try and prevent multiple errors being
13210 has_non_interop_arg
= 1;
13212 curr_arg
= curr_arg
->next
;
13215 /* See if any of the arguments were not interoperable and if so, clear
13216 the procedure symbol to prevent duplicate error messages. */
13217 if (has_non_interop_arg
!= 0)
13219 sym
->attr
.is_c_interop
= 0;
13220 sym
->ts
.is_c_interop
= 0;
13221 sym
->attr
.is_bind_c
= 0;
13225 if (!sym
->attr
.proc_pointer
)
13227 if (sym
->attr
.save
== SAVE_EXPLICIT
)
13229 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13230 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13233 if (sym
->attr
.intent
)
13235 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13236 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13239 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
13241 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13242 "in %qs at %L", sym
->ns
->proc_name
->name
, &sym
->declared_at
);
13245 if (sym
->attr
.external
&& sym
->attr
.function
&& !sym
->attr
.module_procedure
13246 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
13247 || sym
->attr
.contained
))
13249 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13250 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13253 if (strcmp ("ppr@", sym
->name
) == 0)
13255 gfc_error ("Procedure pointer result %qs at %L "
13256 "is missing the pointer attribute",
13257 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
13262 /* Assume that a procedure whose body is not known has references
13263 to external arrays. */
13264 if (sym
->attr
.if_source
!= IFSRC_DECL
)
13265 sym
->attr
.array_outer_dependency
= 1;
13267 /* Compare the characteristics of a module procedure with the
13268 interface declaration. Ideally this would be done with
13269 gfc_compare_interfaces but, at present, the formal interface
13270 cannot be copied to the ts.interface. */
13271 if (sym
->attr
.module_procedure
13272 && sym
->attr
.if_source
== IFSRC_DECL
)
13275 char name
[2*GFC_MAX_SYMBOL_LEN
+ 1];
13277 char *submodule_name
;
13278 strcpy (name
, sym
->ns
->proc_name
->name
);
13279 module_name
= strtok (name
, ".");
13280 submodule_name
= strtok (NULL
, ".");
13282 iface
= sym
->tlink
;
13285 /* Make sure that the result uses the correct charlen for deferred
13287 if (iface
&& sym
->result
13288 && iface
->ts
.type
== BT_CHARACTER
13289 && iface
->ts
.deferred
)
13290 sym
->result
->ts
.u
.cl
= iface
->ts
.u
.cl
;
13295 /* Check the procedure characteristics. */
13296 if (sym
->attr
.elemental
!= iface
->attr
.elemental
)
13298 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13299 "PROCEDURE at %L and its interface in %s",
13300 &sym
->declared_at
, module_name
);
13304 if (sym
->attr
.pure
!= iface
->attr
.pure
)
13306 gfc_error ("Mismatch in PURE attribute between MODULE "
13307 "PROCEDURE at %L and its interface in %s",
13308 &sym
->declared_at
, module_name
);
13312 if (sym
->attr
.recursive
!= iface
->attr
.recursive
)
13314 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13315 "PROCEDURE at %L and its interface in %s",
13316 &sym
->declared_at
, module_name
);
13320 /* Check the result characteristics. */
13321 if (!gfc_check_result_characteristics (sym
, iface
, errmsg
, 200))
13323 gfc_error ("%s between the MODULE PROCEDURE declaration "
13324 "in MODULE %qs and the declaration at %L in "
13326 errmsg
, module_name
, &sym
->declared_at
,
13327 submodule_name
? submodule_name
: module_name
);
13332 /* Check the characteristics of the formal arguments. */
13333 if (sym
->formal
&& sym
->formal_ns
)
13335 for (arg
= sym
->formal
; arg
&& arg
->sym
; arg
= arg
->next
)
13338 gfc_traverse_ns (sym
->formal_ns
, compare_fsyms
);
13346 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13347 been defined and we now know their defined arguments, check that they fulfill
13348 the requirements of the standard for procedures used as finalizers. */
13351 gfc_resolve_finalizers (gfc_symbol
* derived
, bool *finalizable
)
13353 gfc_finalizer
* list
;
13354 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
13355 bool result
= true;
13356 bool seen_scalar
= false;
13359 gfc_symbol
*parent
= gfc_get_derived_super_type (derived
);
13362 gfc_resolve_finalizers (parent
, finalizable
);
13364 /* Ensure that derived-type components have a their finalizers resolved. */
13365 bool has_final
= derived
->f2k_derived
&& derived
->f2k_derived
->finalizers
;
13366 for (c
= derived
->components
; c
; c
= c
->next
)
13367 if (c
->ts
.type
== BT_DERIVED
13368 && !c
->attr
.pointer
&& !c
->attr
.proc_pointer
&& !c
->attr
.allocatable
)
13370 bool has_final2
= false;
13371 if (!gfc_resolve_finalizers (c
->ts
.u
.derived
, &has_final2
))
13372 return false; /* Error. */
13373 has_final
= has_final
|| has_final2
;
13375 /* Return early if not finalizable. */
13379 *finalizable
= false;
13383 /* Walk over the list of finalizer-procedures, check them, and if any one
13384 does not fit in with the standard's definition, print an error and remove
13385 it from the list. */
13386 prev_link
= &derived
->f2k_derived
->finalizers
;
13387 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
13389 gfc_formal_arglist
*dummy_args
;
13394 /* Skip this finalizer if we already resolved it. */
13395 if (list
->proc_tree
)
13397 if (list
->proc_tree
->n
.sym
->formal
->sym
->as
== NULL
13398 || list
->proc_tree
->n
.sym
->formal
->sym
->as
->rank
== 0)
13399 seen_scalar
= true;
13400 prev_link
= &(list
->next
);
13404 /* Check this exists and is a SUBROUTINE. */
13405 if (!list
->proc_sym
->attr
.subroutine
)
13407 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13408 list
->proc_sym
->name
, &list
->where
);
13412 /* We should have exactly one argument. */
13413 dummy_args
= gfc_sym_get_dummy_args (list
->proc_sym
);
13414 if (!dummy_args
|| dummy_args
->next
)
13416 gfc_error ("FINAL procedure at %L must have exactly one argument",
13420 arg
= dummy_args
->sym
;
13422 /* This argument must be of our type. */
13423 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
13425 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13426 &arg
->declared_at
, derived
->name
);
13430 /* It must neither be a pointer nor allocatable nor optional. */
13431 if (arg
->attr
.pointer
)
13433 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13434 &arg
->declared_at
);
13437 if (arg
->attr
.allocatable
)
13439 gfc_error ("Argument of FINAL procedure at %L must not be"
13440 " ALLOCATABLE", &arg
->declared_at
);
13443 if (arg
->attr
.optional
)
13445 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13446 &arg
->declared_at
);
13450 /* It must not be INTENT(OUT). */
13451 if (arg
->attr
.intent
== INTENT_OUT
)
13453 gfc_error ("Argument of FINAL procedure at %L must not be"
13454 " INTENT(OUT)", &arg
->declared_at
);
13458 /* Warn if the procedure is non-scalar and not assumed shape. */
13459 if (warn_surprising
&& arg
->as
&& arg
->as
->rank
!= 0
13460 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
13461 gfc_warning (OPT_Wsurprising
,
13462 "Non-scalar FINAL procedure at %L should have assumed"
13463 " shape argument", &arg
->declared_at
);
13465 /* Check that it does not match in kind and rank with a FINAL procedure
13466 defined earlier. To really loop over the *earlier* declarations,
13467 we need to walk the tail of the list as new ones were pushed at the
13469 /* TODO: Handle kind parameters once they are implemented. */
13470 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
13471 for (i
= list
->next
; i
; i
= i
->next
)
13473 gfc_formal_arglist
*dummy_args
;
13475 /* Argument list might be empty; that is an error signalled earlier,
13476 but we nevertheless continued resolving. */
13477 dummy_args
= gfc_sym_get_dummy_args (i
->proc_sym
);
13480 gfc_symbol
* i_arg
= dummy_args
->sym
;
13481 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
13482 if (i_rank
== my_rank
)
13484 gfc_error ("FINAL procedure %qs declared at %L has the same"
13485 " rank (%d) as %qs",
13486 list
->proc_sym
->name
, &list
->where
, my_rank
,
13487 i
->proc_sym
->name
);
13493 /* Is this the/a scalar finalizer procedure? */
13495 seen_scalar
= true;
13497 /* Find the symtree for this procedure. */
13498 gcc_assert (!list
->proc_tree
);
13499 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
13501 prev_link
= &list
->next
;
13504 /* Remove wrong nodes immediately from the list so we don't risk any
13505 troubles in the future when they might fail later expectations. */
13508 *prev_link
= list
->next
;
13509 gfc_free_finalizer (i
);
13513 if (result
== false)
13516 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13517 were nodes in the list, must have been for arrays. It is surely a good
13518 idea to have a scalar version there if there's something to finalize. */
13519 if (warn_surprising
&& derived
->f2k_derived
->finalizers
&& !seen_scalar
)
13520 gfc_warning (OPT_Wsurprising
,
13521 "Only array FINAL procedures declared for derived type %qs"
13522 " defined at %L, suggest also scalar one",
13523 derived
->name
, &derived
->declared_at
);
13525 vtab
= gfc_find_derived_vtab (derived
);
13526 c
= vtab
->ts
.u
.derived
->components
->next
->next
->next
->next
->next
;
13527 gfc_set_sym_referenced (c
->initializer
->symtree
->n
.sym
);
13530 *finalizable
= true;
13536 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13539 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
13540 const char* generic_name
, locus where
)
13542 gfc_symbol
*sym1
, *sym2
;
13543 const char *pass1
, *pass2
;
13544 gfc_formal_arglist
*dummy_args
;
13546 gcc_assert (t1
->specific
&& t2
->specific
);
13547 gcc_assert (!t1
->specific
->is_generic
);
13548 gcc_assert (!t2
->specific
->is_generic
);
13549 gcc_assert (t1
->is_operator
== t2
->is_operator
);
13551 sym1
= t1
->specific
->u
.specific
->n
.sym
;
13552 sym2
= t2
->specific
->u
.specific
->n
.sym
;
13557 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13558 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
13559 || sym1
->attr
.function
!= sym2
->attr
.function
)
13561 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13562 " GENERIC %qs at %L",
13563 sym1
->name
, sym2
->name
, generic_name
, &where
);
13567 /* Determine PASS arguments. */
13568 if (t1
->specific
->nopass
)
13570 else if (t1
->specific
->pass_arg
)
13571 pass1
= t1
->specific
->pass_arg
;
13574 dummy_args
= gfc_sym_get_dummy_args (t1
->specific
->u
.specific
->n
.sym
);
13576 pass1
= dummy_args
->sym
->name
;
13580 if (t2
->specific
->nopass
)
13582 else if (t2
->specific
->pass_arg
)
13583 pass2
= t2
->specific
->pass_arg
;
13586 dummy_args
= gfc_sym_get_dummy_args (t2
->specific
->u
.specific
->n
.sym
);
13588 pass2
= dummy_args
->sym
->name
;
13593 /* Compare the interfaces. */
13594 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, !t1
->is_operator
, 0,
13595 NULL
, 0, pass1
, pass2
))
13597 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13598 sym1
->name
, sym2
->name
, generic_name
, &where
);
13606 /* Worker function for resolving a generic procedure binding; this is used to
13607 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13609 The difference between those cases is finding possible inherited bindings
13610 that are overridden, as one has to look for them in tb_sym_root,
13611 tb_uop_root or tb_op, respectively. Thus the caller must already find
13612 the super-type and set p->overridden correctly. */
13615 resolve_tb_generic_targets (gfc_symbol
* super_type
,
13616 gfc_typebound_proc
* p
, const char* name
)
13618 gfc_tbp_generic
* target
;
13619 gfc_symtree
* first_target
;
13620 gfc_symtree
* inherited
;
13622 gcc_assert (p
&& p
->is_generic
);
13624 /* Try to find the specific bindings for the symtrees in our target-list. */
13625 gcc_assert (p
->u
.generic
);
13626 for (target
= p
->u
.generic
; target
; target
= target
->next
)
13627 if (!target
->specific
)
13629 gfc_typebound_proc
* overridden_tbp
;
13630 gfc_tbp_generic
* g
;
13631 const char* target_name
;
13633 target_name
= target
->specific_st
->name
;
13635 /* Defined for this type directly. */
13636 if (target
->specific_st
->n
.tb
&& !target
->specific_st
->n
.tb
->error
)
13638 target
->specific
= target
->specific_st
->n
.tb
;
13639 goto specific_found
;
13642 /* Look for an inherited specific binding. */
13645 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
13650 gcc_assert (inherited
->n
.tb
);
13651 target
->specific
= inherited
->n
.tb
;
13652 goto specific_found
;
13656 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13657 " at %L", target_name
, name
, &p
->where
);
13660 /* Once we've found the specific binding, check it is not ambiguous with
13661 other specifics already found or inherited for the same GENERIC. */
13663 gcc_assert (target
->specific
);
13665 /* This must really be a specific binding! */
13666 if (target
->specific
->is_generic
)
13668 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13669 " %qs is GENERIC, too", name
, &p
->where
, target_name
);
13673 /* Check those already resolved on this type directly. */
13674 for (g
= p
->u
.generic
; g
; g
= g
->next
)
13675 if (g
!= target
&& g
->specific
13676 && !check_generic_tbp_ambiguity (target
, g
, name
, p
->where
))
13679 /* Check for ambiguity with inherited specific targets. */
13680 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
13681 overridden_tbp
= overridden_tbp
->overridden
)
13682 if (overridden_tbp
->is_generic
)
13684 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
13686 gcc_assert (g
->specific
);
13687 if (!check_generic_tbp_ambiguity (target
, g
, name
, p
->where
))
13693 /* If we attempt to "overwrite" a specific binding, this is an error. */
13694 if (p
->overridden
&& !p
->overridden
->is_generic
)
13696 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13697 " the same name", name
, &p
->where
);
13701 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13702 all must have the same attributes here. */
13703 first_target
= p
->u
.generic
->specific
->u
.specific
;
13704 gcc_assert (first_target
);
13705 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
13706 p
->function
= first_target
->n
.sym
->attr
.function
;
13712 /* Resolve a GENERIC procedure binding for a derived type. */
13715 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
13717 gfc_symbol
* super_type
;
13719 /* Find the overridden binding if any. */
13720 st
->n
.tb
->overridden
= NULL
;
13721 super_type
= gfc_get_derived_super_type (derived
);
13724 gfc_symtree
* overridden
;
13725 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
13728 if (overridden
&& overridden
->n
.tb
)
13729 st
->n
.tb
->overridden
= overridden
->n
.tb
;
13732 /* Resolve using worker function. */
13733 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
13737 /* Retrieve the target-procedure of an operator binding and do some checks in
13738 common for intrinsic and user-defined type-bound operators. */
13741 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
13743 gfc_symbol
* target_proc
;
13745 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
13746 target_proc
= target
->specific
->u
.specific
->n
.sym
;
13747 gcc_assert (target_proc
);
13749 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13750 if (target
->specific
->nopass
)
13752 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where
);
13756 return target_proc
;
13760 /* Resolve a type-bound intrinsic operator. */
13763 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
13764 gfc_typebound_proc
* p
)
13766 gfc_symbol
* super_type
;
13767 gfc_tbp_generic
* target
;
13769 /* If there's already an error here, do nothing (but don't fail again). */
13773 /* Operators should always be GENERIC bindings. */
13774 gcc_assert (p
->is_generic
);
13776 /* Look for an overridden binding. */
13777 super_type
= gfc_get_derived_super_type (derived
);
13778 if (super_type
&& super_type
->f2k_derived
)
13779 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
13782 p
->overridden
= NULL
;
13784 /* Resolve general GENERIC properties using worker function. */
13785 if (!resolve_tb_generic_targets (super_type
, p
, gfc_op2string(op
)))
13788 /* Check the targets to be procedures of correct interface. */
13789 for (target
= p
->u
.generic
; target
; target
= target
->next
)
13791 gfc_symbol
* target_proc
;
13793 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
13797 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
13800 /* Add target to non-typebound operator list. */
13801 if (!target
->specific
->deferred
&& !derived
->attr
.use_assoc
13802 && p
->access
!= ACCESS_PRIVATE
&& derived
->ns
== gfc_current_ns
)
13804 gfc_interface
*head
, *intr
;
13806 /* Preempt 'gfc_check_new_interface' for submodules, where the
13807 mechanism for handling module procedures winds up resolving
13808 operator interfaces twice and would otherwise cause an error. */
13809 for (intr
= derived
->ns
->op
[op
]; intr
; intr
= intr
->next
)
13810 if (intr
->sym
== target_proc
13811 && target_proc
->attr
.used_in_submodule
)
13814 if (!gfc_check_new_interface (derived
->ns
->op
[op
],
13815 target_proc
, p
->where
))
13817 head
= derived
->ns
->op
[op
];
13818 intr
= gfc_get_interface ();
13819 intr
->sym
= target_proc
;
13820 intr
->where
= p
->where
;
13822 derived
->ns
->op
[op
] = intr
;
13834 /* Resolve a type-bound user operator (tree-walker callback). */
13836 static gfc_symbol
* resolve_bindings_derived
;
13837 static bool resolve_bindings_result
;
13839 static bool check_uop_procedure (gfc_symbol
* sym
, locus where
);
13842 resolve_typebound_user_op (gfc_symtree
* stree
)
13844 gfc_symbol
* super_type
;
13845 gfc_tbp_generic
* target
;
13847 gcc_assert (stree
&& stree
->n
.tb
);
13849 if (stree
->n
.tb
->error
)
13852 /* Operators should always be GENERIC bindings. */
13853 gcc_assert (stree
->n
.tb
->is_generic
);
13855 /* Find overridden procedure, if any. */
13856 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
13857 if (super_type
&& super_type
->f2k_derived
)
13859 gfc_symtree
* overridden
;
13860 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
13861 stree
->name
, true, NULL
);
13863 if (overridden
&& overridden
->n
.tb
)
13864 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
13867 stree
->n
.tb
->overridden
= NULL
;
13869 /* Resolve basically using worker function. */
13870 if (!resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
))
13873 /* Check the targets to be functions of correct interface. */
13874 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
13876 gfc_symbol
* target_proc
;
13878 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
13882 if (!check_uop_procedure (target_proc
, stree
->n
.tb
->where
))
13889 resolve_bindings_result
= false;
13890 stree
->n
.tb
->error
= 1;
13894 /* Resolve the type-bound procedures for a derived type. */
13897 resolve_typebound_procedure (gfc_symtree
* stree
)
13901 gfc_symbol
* me_arg
;
13902 gfc_symbol
* super_type
;
13903 gfc_component
* comp
;
13905 gcc_assert (stree
);
13907 /* Undefined specific symbol from GENERIC target definition. */
13911 if (stree
->n
.tb
->error
)
13914 /* If this is a GENERIC binding, use that routine. */
13915 if (stree
->n
.tb
->is_generic
)
13917 if (!resolve_typebound_generic (resolve_bindings_derived
, stree
))
13922 /* Get the target-procedure to check it. */
13923 gcc_assert (!stree
->n
.tb
->is_generic
);
13924 gcc_assert (stree
->n
.tb
->u
.specific
);
13925 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
13926 where
= stree
->n
.tb
->where
;
13928 /* Default access should already be resolved from the parser. */
13929 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
13931 if (stree
->n
.tb
->deferred
)
13933 if (!check_proc_interface (proc
, &where
))
13938 /* If proc has not been resolved at this point, proc->name may
13939 actually be a USE associated entity. See PR fortran/89647. */
13940 if (!proc
->resolve_symbol_called
13941 && proc
->attr
.function
== 0 && proc
->attr
.subroutine
== 0)
13944 gfc_find_symbol (proc
->name
, gfc_current_ns
->parent
, 1, &tmp
);
13945 if (tmp
&& tmp
->attr
.use_assoc
)
13947 proc
->module
= tmp
->module
;
13948 proc
->attr
.proc
= tmp
->attr
.proc
;
13949 proc
->attr
.function
= tmp
->attr
.function
;
13950 proc
->attr
.subroutine
= tmp
->attr
.subroutine
;
13951 proc
->attr
.use_assoc
= tmp
->attr
.use_assoc
;
13952 proc
->ts
= tmp
->ts
;
13953 proc
->result
= tmp
->result
;
13957 /* Check for F08:C465. */
13958 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
13959 || (proc
->attr
.proc
!= PROC_MODULE
13960 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
13961 || proc
->attr
.abstract
)
13963 gfc_error ("%qs must be a module procedure or an external "
13964 "procedure with an explicit interface at %L",
13965 proc
->name
, &where
);
13970 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
13971 stree
->n
.tb
->function
= proc
->attr
.function
;
13973 /* Find the super-type of the current derived type. We could do this once and
13974 store in a global if speed is needed, but as long as not I believe this is
13975 more readable and clearer. */
13976 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
13978 /* If PASS, resolve and check arguments if not already resolved / loaded
13979 from a .mod file. */
13980 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
13982 gfc_formal_arglist
*dummy_args
;
13984 dummy_args
= gfc_sym_get_dummy_args (proc
);
13985 if (stree
->n
.tb
->pass_arg
)
13987 gfc_formal_arglist
*i
;
13989 /* If an explicit passing argument name is given, walk the arg-list
13990 and look for it. */
13993 stree
->n
.tb
->pass_arg_num
= 1;
13994 for (i
= dummy_args
; i
; i
= i
->next
)
13996 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
14001 ++stree
->n
.tb
->pass_arg_num
;
14006 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14008 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
14009 stree
->n
.tb
->pass_arg
);
14015 /* Otherwise, take the first one; there should in fact be at least
14017 stree
->n
.tb
->pass_arg_num
= 1;
14020 gfc_error ("Procedure %qs with PASS at %L must have at"
14021 " least one argument", proc
->name
, &where
);
14024 me_arg
= dummy_args
->sym
;
14027 /* Now check that the argument-type matches and the passed-object
14028 dummy argument is generally fine. */
14030 gcc_assert (me_arg
);
14032 if (me_arg
->ts
.type
!= BT_CLASS
)
14034 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14035 " at %L", proc
->name
, &where
);
14039 if (CLASS_DATA (me_arg
)->ts
.u
.derived
14040 != resolve_bindings_derived
)
14042 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14043 " the derived-type %qs", me_arg
->name
, proc
->name
,
14044 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
14048 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
14049 if (CLASS_DATA (me_arg
)->as
&& CLASS_DATA (me_arg
)->as
->rank
!= 0)
14051 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14052 " scalar", proc
->name
, &where
);
14055 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
14057 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14058 " be ALLOCATABLE", proc
->name
, &where
);
14061 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
14063 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14064 " be POINTER", proc
->name
, &where
);
14069 /* If we are extending some type, check that we don't override a procedure
14070 flagged NON_OVERRIDABLE. */
14071 stree
->n
.tb
->overridden
= NULL
;
14074 gfc_symtree
* overridden
;
14075 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
14076 stree
->name
, true, NULL
);
14080 if (overridden
->n
.tb
)
14081 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
14083 if (!gfc_check_typebound_override (stree
, overridden
))
14088 /* See if there's a name collision with a component directly in this type. */
14089 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
14090 if (!strcmp (comp
->name
, stree
->name
))
14092 gfc_error ("Procedure %qs at %L has the same name as a component of"
14094 stree
->name
, &where
, resolve_bindings_derived
->name
);
14098 /* Try to find a name collision with an inherited component. */
14099 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true,
14102 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14103 " component of %qs",
14104 stree
->name
, &where
, resolve_bindings_derived
->name
);
14108 stree
->n
.tb
->error
= 0;
14112 resolve_bindings_result
= false;
14113 stree
->n
.tb
->error
= 1;
14118 resolve_typebound_procedures (gfc_symbol
* derived
)
14121 gfc_symbol
* super_type
;
14123 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
14126 super_type
= gfc_get_derived_super_type (derived
);
14128 resolve_symbol (super_type
);
14130 resolve_bindings_derived
= derived
;
14131 resolve_bindings_result
= true;
14133 if (derived
->f2k_derived
->tb_sym_root
)
14134 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
14135 &resolve_typebound_procedure
);
14137 if (derived
->f2k_derived
->tb_uop_root
)
14138 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
14139 &resolve_typebound_user_op
);
14141 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
14143 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
14144 if (p
&& !resolve_typebound_intrinsic_op (derived
,
14145 (gfc_intrinsic_op
)op
, p
))
14146 resolve_bindings_result
= false;
14149 return resolve_bindings_result
;
14153 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14154 to give all identical derived types the same backend_decl. */
14156 add_dt_to_dt_list (gfc_symbol
*derived
)
14158 if (!derived
->dt_next
)
14160 if (gfc_derived_types
)
14162 derived
->dt_next
= gfc_derived_types
->dt_next
;
14163 gfc_derived_types
->dt_next
= derived
;
14167 derived
->dt_next
= derived
;
14169 gfc_derived_types
= derived
;
14174 /* Ensure that a derived-type is really not abstract, meaning that every
14175 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14178 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
14183 if (!ensure_not_abstract_walker (sub
, st
->left
))
14185 if (!ensure_not_abstract_walker (sub
, st
->right
))
14188 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
14190 gfc_symtree
* overriding
;
14191 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
14194 gcc_assert (overriding
->n
.tb
);
14195 if (overriding
->n
.tb
->deferred
)
14197 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14198 " %qs is DEFERRED and not overridden",
14199 sub
->name
, &sub
->declared_at
, st
->name
);
14208 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
14210 /* The algorithm used here is to recursively travel up the ancestry of sub
14211 and for each ancestor-type, check all bindings. If any of them is
14212 DEFERRED, look it up starting from sub and see if the found (overriding)
14213 binding is not DEFERRED.
14214 This is not the most efficient way to do this, but it should be ok and is
14215 clearer than something sophisticated. */
14217 gcc_assert (ancestor
&& !sub
->attr
.abstract
);
14219 if (!ancestor
->attr
.abstract
)
14222 /* Walk bindings of this ancestor. */
14223 if (ancestor
->f2k_derived
)
14226 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
14231 /* Find next ancestor type and recurse on it. */
14232 ancestor
= gfc_get_derived_super_type (ancestor
);
14234 return ensure_not_abstract (sub
, ancestor
);
14240 /* This check for typebound defined assignments is done recursively
14241 since the order in which derived types are resolved is not always in
14242 order of the declarations. */
14245 check_defined_assignments (gfc_symbol
*derived
)
14249 for (c
= derived
->components
; c
; c
= c
->next
)
14251 if (!gfc_bt_struct (c
->ts
.type
)
14253 || c
->attr
.allocatable
14254 || c
->attr
.proc_pointer_comp
14255 || c
->attr
.class_pointer
14256 || c
->attr
.proc_pointer
)
14259 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
14260 || (c
->ts
.u
.derived
->f2k_derived
14261 && c
->ts
.u
.derived
->f2k_derived
->tb_op
[INTRINSIC_ASSIGN
]))
14263 derived
->attr
.defined_assign_comp
= 1;
14267 check_defined_assignments (c
->ts
.u
.derived
);
14268 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
)
14270 derived
->attr
.defined_assign_comp
= 1;
14277 /* Resolve a single component of a derived type or structure. */
14280 resolve_component (gfc_component
*c
, gfc_symbol
*sym
)
14282 gfc_symbol
*super_type
;
14283 symbol_attribute
*attr
;
14285 if (c
->attr
.artificial
)
14288 /* Do not allow vtype components to be resolved in nameless namespaces
14289 such as block data because the procedure pointers will cause ICEs
14290 and vtables are not needed in these contexts. */
14291 if (sym
->attr
.vtype
&& sym
->attr
.use_assoc
14292 && sym
->ns
->proc_name
== NULL
)
14296 if ((!sym
->attr
.is_class
|| c
!= sym
->components
)
14297 && c
->attr
.codimension
14298 && (!c
->attr
.allocatable
|| (c
->as
&& c
->as
->type
!= AS_DEFERRED
)))
14300 gfc_error ("Coarray component %qs at %L must be allocatable with "
14301 "deferred shape", c
->name
, &c
->loc
);
14306 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
14307 && c
->ts
.u
.derived
->ts
.is_iso_c
)
14309 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14310 "shall not be a coarray", c
->name
, &c
->loc
);
14315 if (gfc_bt_struct (c
->ts
.type
) && c
->ts
.u
.derived
->attr
.coarray_comp
14316 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
14317 || c
->attr
.allocatable
))
14319 gfc_error ("Component %qs at %L with coarray component "
14320 "shall be a nonpointer, nonallocatable scalar",
14326 if (c
->ts
.type
== BT_CLASS
)
14328 if (CLASS_DATA (c
))
14330 attr
= &(CLASS_DATA (c
)->attr
);
14332 /* Fix up contiguous attribute. */
14333 if (c
->attr
.contiguous
)
14334 attr
->contiguous
= 1;
14342 if (attr
&& attr
->contiguous
&& (!attr
->dimension
|| !attr
->pointer
))
14344 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14345 "is not an array pointer", c
->name
, &c
->loc
);
14349 /* F2003, 15.2.1 - length has to be one. */
14350 if (sym
->attr
.is_bind_c
&& c
->ts
.type
== BT_CHARACTER
14351 && (c
->ts
.u
.cl
== NULL
|| c
->ts
.u
.cl
->length
== NULL
14352 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
)
14353 || mpz_cmp_si (c
->ts
.u
.cl
->length
->value
.integer
, 1) != 0))
14355 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14360 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
14362 gfc_symbol
*ifc
= c
->ts
.interface
;
14364 if (!sym
->attr
.vtype
&& !check_proc_interface (ifc
, &c
->loc
))
14370 if (ifc
->attr
.if_source
|| ifc
->attr
.intrinsic
)
14372 /* Resolve interface and copy attributes. */
14373 if (ifc
->formal
&& !ifc
->formal_ns
)
14374 resolve_symbol (ifc
);
14375 if (ifc
->attr
.intrinsic
)
14376 gfc_resolve_intrinsic (ifc
, &ifc
->declared_at
);
14380 c
->ts
= ifc
->result
->ts
;
14381 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
14382 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
14383 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
14384 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
14385 c
->attr
.class_ok
= ifc
->result
->attr
.class_ok
;
14390 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
14391 c
->attr
.pointer
= ifc
->attr
.pointer
;
14392 c
->attr
.dimension
= ifc
->attr
.dimension
;
14393 c
->as
= gfc_copy_array_spec (ifc
->as
);
14394 c
->attr
.class_ok
= ifc
->attr
.class_ok
;
14396 c
->ts
.interface
= ifc
;
14397 c
->attr
.function
= ifc
->attr
.function
;
14398 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
14400 c
->attr
.pure
= ifc
->attr
.pure
;
14401 c
->attr
.elemental
= ifc
->attr
.elemental
;
14402 c
->attr
.recursive
= ifc
->attr
.recursive
;
14403 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
14404 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
14405 /* Copy char length. */
14406 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
14408 gfc_charlen
*cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
14409 if (cl
->length
&& !cl
->resolved
14410 && !gfc_resolve_expr (cl
->length
))
14419 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
14421 /* Since PPCs are not implicitly typed, a PPC without an explicit
14422 interface must be a subroutine. */
14423 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
14426 /* Procedure pointer components: Check PASS arg. */
14427 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0
14428 && !sym
->attr
.vtype
)
14430 gfc_symbol
* me_arg
;
14432 if (c
->tb
->pass_arg
)
14434 gfc_formal_arglist
* i
;
14436 /* If an explicit passing argument name is given, walk the arg-list
14437 and look for it. */
14440 c
->tb
->pass_arg_num
= 1;
14441 for (i
= c
->ts
.interface
->formal
; i
; i
= i
->next
)
14443 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
14448 c
->tb
->pass_arg_num
++;
14453 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14454 "at %L has no argument %qs", c
->name
,
14455 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
14462 /* Otherwise, take the first one; there should in fact be at least
14464 c
->tb
->pass_arg_num
= 1;
14465 if (!c
->ts
.interface
->formal
)
14467 gfc_error ("Procedure pointer component %qs with PASS at %L "
14468 "must have at least one argument",
14473 me_arg
= c
->ts
.interface
->formal
->sym
;
14476 /* Now check that the argument-type matches. */
14477 gcc_assert (me_arg
);
14478 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
14479 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
14480 || (me_arg
->ts
.type
== BT_CLASS
14481 && CLASS_DATA (me_arg
)->ts
.u
.derived
!= sym
))
14483 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14484 " the derived type %qs", me_arg
->name
, c
->name
,
14485 me_arg
->name
, &c
->loc
, sym
->name
);
14490 /* Check for F03:C453. */
14491 if (CLASS_DATA (me_arg
)->attr
.dimension
)
14493 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14494 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
14500 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
14502 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14503 "may not have the POINTER attribute", me_arg
->name
,
14504 c
->name
, me_arg
->name
, &c
->loc
);
14509 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
14511 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14512 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
14513 me_arg
->name
, &c
->loc
);
14518 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
14520 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14521 " at %L", c
->name
, &c
->loc
);
14527 /* Check type-spec if this is not the parent-type component. */
14528 if (((sym
->attr
.is_class
14529 && (!sym
->components
->ts
.u
.derived
->attr
.extension
14530 || c
!= sym
->components
->ts
.u
.derived
->components
))
14531 || (!sym
->attr
.is_class
14532 && (!sym
->attr
.extension
|| c
!= sym
->components
)))
14533 && !sym
->attr
.vtype
14534 && !resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
))
14537 super_type
= gfc_get_derived_super_type (sym
);
14539 /* If this type is an extension, set the accessibility of the parent
14542 && ((sym
->attr
.is_class
14543 && c
== sym
->components
->ts
.u
.derived
->components
)
14544 || (!sym
->attr
.is_class
&& c
== sym
->components
))
14545 && strcmp (super_type
->name
, c
->name
) == 0)
14546 c
->attr
.access
= super_type
->attr
.access
;
14548 /* If this type is an extension, see if this component has the same name
14549 as an inherited type-bound procedure. */
14550 if (super_type
&& !sym
->attr
.is_class
14551 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
14553 gfc_error ("Component %qs of %qs at %L has the same name as an"
14554 " inherited type-bound procedure",
14555 c
->name
, sym
->name
, &c
->loc
);
14559 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
14560 && !c
->ts
.deferred
)
14562 if (c
->ts
.u
.cl
->length
== NULL
14563 || (!resolve_charlen(c
->ts
.u
.cl
))
14564 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
14566 gfc_error ("Character length of component %qs needs to "
14567 "be a constant specification expression at %L",
14569 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
14574 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
14575 && !c
->attr
.pointer
&& !c
->attr
.allocatable
)
14577 gfc_error ("Character component %qs of %qs at %L with deferred "
14578 "length must be a POINTER or ALLOCATABLE",
14579 c
->name
, sym
->name
, &c
->loc
);
14583 /* Add the hidden deferred length field. */
14584 if (c
->ts
.type
== BT_CHARACTER
14585 && (c
->ts
.deferred
|| c
->attr
.pdt_string
)
14586 && !c
->attr
.function
14587 && !sym
->attr
.is_class
)
14589 char name
[GFC_MAX_SYMBOL_LEN
+9];
14590 gfc_component
*strlen
;
14591 sprintf (name
, "_%s_length", c
->name
);
14592 strlen
= gfc_find_component (sym
, name
, true, true, NULL
);
14593 if (strlen
== NULL
)
14595 if (!gfc_add_component (sym
, name
, &strlen
))
14597 strlen
->ts
.type
= BT_INTEGER
;
14598 strlen
->ts
.kind
= gfc_charlen_int_kind
;
14599 strlen
->attr
.access
= ACCESS_PRIVATE
;
14600 strlen
->attr
.artificial
= 1;
14604 if (c
->ts
.type
== BT_DERIVED
14605 && sym
->component_access
!= ACCESS_PRIVATE
14606 && gfc_check_symbol_access (sym
)
14607 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
14608 && !c
->ts
.u
.derived
->attr
.use_assoc
14609 && !gfc_check_symbol_access (c
->ts
.u
.derived
)
14610 && !gfc_notify_std (GFC_STD_F2003
, "the component %qs is a "
14611 "PRIVATE type and cannot be a component of "
14612 "%qs, which is PUBLIC at %L", c
->name
,
14613 sym
->name
, &sym
->declared_at
))
14616 if ((sym
->attr
.sequence
|| sym
->attr
.is_bind_c
) && c
->ts
.type
== BT_CLASS
)
14618 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14619 "type %s", c
->name
, &c
->loc
, sym
->name
);
14623 if (sym
->attr
.sequence
)
14625 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
14627 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14628 "not have the SEQUENCE attribute",
14629 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
14634 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.generic
)
14635 c
->ts
.u
.derived
= gfc_find_dt_in_generic (c
->ts
.u
.derived
);
14636 else if (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
14637 && CLASS_DATA (c
)->ts
.u
.derived
->attr
.generic
)
14638 CLASS_DATA (c
)->ts
.u
.derived
14639 = gfc_find_dt_in_generic (CLASS_DATA (c
)->ts
.u
.derived
);
14641 /* If an allocatable component derived type is of the same type as
14642 the enclosing derived type, we need a vtable generating so that
14643 the __deallocate procedure is created. */
14644 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
14645 && c
->ts
.u
.derived
== sym
&& c
->attr
.allocatable
== 1)
14646 gfc_find_vtab (&c
->ts
);
14648 /* Ensure that all the derived type components are put on the
14649 derived type list; even in formal namespaces, where derived type
14650 pointer components might not have been declared. */
14651 if (c
->ts
.type
== BT_DERIVED
14653 && c
->ts
.u
.derived
->components
14655 && sym
!= c
->ts
.u
.derived
)
14656 add_dt_to_dt_list (c
->ts
.u
.derived
);
14658 if (!gfc_resolve_array_spec (c
->as
,
14659 !(c
->attr
.pointer
|| c
->attr
.proc_pointer
14660 || c
->attr
.allocatable
)))
14663 if (c
->initializer
&& !sym
->attr
.vtype
14664 && !c
->attr
.pdt_kind
&& !c
->attr
.pdt_len
14665 && !gfc_check_assign_symbol (sym
, c
, c
->initializer
))
14672 /* Be nice about the locus for a structure expression - show the locus of the
14673 first non-null sub-expression if we can. */
14676 cons_where (gfc_expr
*struct_expr
)
14678 gfc_constructor
*cons
;
14680 gcc_assert (struct_expr
&& struct_expr
->expr_type
== EXPR_STRUCTURE
);
14682 cons
= gfc_constructor_first (struct_expr
->value
.constructor
);
14683 for (; cons
; cons
= gfc_constructor_next (cons
))
14685 if (cons
->expr
&& cons
->expr
->expr_type
!= EXPR_NULL
)
14686 return &cons
->expr
->where
;
14689 return &struct_expr
->where
;
14692 /* Resolve the components of a structure type. Much less work than derived
14696 resolve_fl_struct (gfc_symbol
*sym
)
14699 gfc_expr
*init
= NULL
;
14702 /* Make sure UNIONs do not have overlapping initializers. */
14703 if (sym
->attr
.flavor
== FL_UNION
)
14705 for (c
= sym
->components
; c
; c
= c
->next
)
14707 if (init
&& c
->initializer
)
14709 gfc_error ("Conflicting initializers in union at %L and %L",
14710 cons_where (init
), cons_where (c
->initializer
));
14711 gfc_free_expr (c
->initializer
);
14712 c
->initializer
= NULL
;
14715 init
= c
->initializer
;
14720 for (c
= sym
->components
; c
; c
= c
->next
)
14721 if (!resolve_component (c
, sym
))
14727 if (sym
->components
)
14728 add_dt_to_dt_list (sym
);
14734 /* Resolve the components of a derived type. This does not have to wait until
14735 resolution stage, but can be done as soon as the dt declaration has been
14739 resolve_fl_derived0 (gfc_symbol
*sym
)
14741 gfc_symbol
* super_type
;
14743 gfc_formal_arglist
*f
;
14746 if (sym
->attr
.unlimited_polymorphic
)
14749 super_type
= gfc_get_derived_super_type (sym
);
14752 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
14754 gfc_error ("As extending type %qs at %L has a coarray component, "
14755 "parent type %qs shall also have one", sym
->name
,
14756 &sym
->declared_at
, super_type
->name
);
14760 /* Ensure the extended type gets resolved before we do. */
14761 if (super_type
&& !resolve_fl_derived0 (super_type
))
14764 /* An ABSTRACT type must be extensible. */
14765 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
14767 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14768 sym
->name
, &sym
->declared_at
);
14772 c
= (sym
->attr
.is_class
) ? sym
->components
->ts
.u
.derived
->components
14776 for ( ; c
!= NULL
; c
= c
->next
)
14777 if (!resolve_component (c
, sym
))
14783 /* Now add the caf token field, where needed. */
14784 if (flag_coarray
!= GFC_FCOARRAY_NONE
14785 && !sym
->attr
.is_class
&& !sym
->attr
.vtype
)
14787 for (c
= sym
->components
; c
; c
= c
->next
)
14788 if (!c
->attr
.dimension
&& !c
->attr
.codimension
14789 && (c
->attr
.allocatable
|| c
->attr
.pointer
))
14791 char name
[GFC_MAX_SYMBOL_LEN
+9];
14792 gfc_component
*token
;
14793 sprintf (name
, "_caf_%s", c
->name
);
14794 token
= gfc_find_component (sym
, name
, true, true, NULL
);
14797 if (!gfc_add_component (sym
, name
, &token
))
14799 token
->ts
.type
= BT_VOID
;
14800 token
->ts
.kind
= gfc_default_integer_kind
;
14801 token
->attr
.access
= ACCESS_PRIVATE
;
14802 token
->attr
.artificial
= 1;
14803 token
->attr
.caf_token
= 1;
14808 check_defined_assignments (sym
);
14810 if (!sym
->attr
.defined_assign_comp
&& super_type
)
14811 sym
->attr
.defined_assign_comp
14812 = super_type
->attr
.defined_assign_comp
;
14814 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14815 all DEFERRED bindings are overridden. */
14816 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
14817 && !sym
->attr
.is_class
14818 && !ensure_not_abstract (sym
, super_type
))
14821 /* Check that there is a component for every PDT parameter. */
14822 if (sym
->attr
.pdt_template
)
14824 for (f
= sym
->formal
; f
; f
= f
->next
)
14828 c
= gfc_find_component (sym
, f
->sym
->name
, true, true, NULL
);
14831 gfc_error ("Parameterized type %qs does not have a component "
14832 "corresponding to parameter %qs at %L", sym
->name
,
14833 f
->sym
->name
, &sym
->declared_at
);
14839 /* Add derived type to the derived type list. */
14840 add_dt_to_dt_list (sym
);
14846 /* The following procedure does the full resolution of a derived type,
14847 including resolution of all type-bound procedures (if present). In contrast
14848 to 'resolve_fl_derived0' this can only be done after the module has been
14849 parsed completely. */
14852 resolve_fl_derived (gfc_symbol
*sym
)
14854 gfc_symbol
*gen_dt
= NULL
;
14856 if (sym
->attr
.unlimited_polymorphic
)
14859 if (!sym
->attr
.is_class
)
14860 gfc_find_symbol (sym
->name
, sym
->ns
, 0, &gen_dt
);
14861 if (gen_dt
&& gen_dt
->generic
&& gen_dt
->generic
->next
14862 && (!gen_dt
->generic
->sym
->attr
.use_assoc
14863 || gen_dt
->generic
->sym
->module
!= gen_dt
->generic
->next
->sym
->module
)
14864 && !gfc_notify_std (GFC_STD_F2003
, "Generic name %qs of function "
14865 "%qs at %L being the same name as derived "
14866 "type at %L", sym
->name
,
14867 gen_dt
->generic
->sym
== sym
14868 ? gen_dt
->generic
->next
->sym
->name
14869 : gen_dt
->generic
->sym
->name
,
14870 gen_dt
->generic
->sym
== sym
14871 ? &gen_dt
->generic
->next
->sym
->declared_at
14872 : &gen_dt
->generic
->sym
->declared_at
,
14873 &sym
->declared_at
))
14876 if (sym
->components
== NULL
&& !sym
->attr
.zero_comp
&& !sym
->attr
.use_assoc
)
14878 gfc_error ("Derived type %qs at %L has not been declared",
14879 sym
->name
, &sym
->declared_at
);
14883 /* Resolve the finalizer procedures. */
14884 if (!gfc_resolve_finalizers (sym
, NULL
))
14887 if (sym
->attr
.is_class
&& sym
->ts
.u
.derived
== NULL
)
14889 /* Fix up incomplete CLASS symbols. */
14890 gfc_component
*data
= gfc_find_component (sym
, "_data", true, true, NULL
);
14891 gfc_component
*vptr
= gfc_find_component (sym
, "_vptr", true, true, NULL
);
14893 /* Nothing more to do for unlimited polymorphic entities. */
14894 if (data
->ts
.u
.derived
->attr
.unlimited_polymorphic
)
14896 else if (vptr
->ts
.u
.derived
== NULL
)
14898 gfc_symbol
*vtab
= gfc_find_derived_vtab (data
->ts
.u
.derived
);
14900 vptr
->ts
.u
.derived
= vtab
->ts
.u
.derived
;
14901 if (!resolve_fl_derived0 (vptr
->ts
.u
.derived
))
14906 if (!resolve_fl_derived0 (sym
))
14909 /* Resolve the type-bound procedures. */
14910 if (!resolve_typebound_procedures (sym
))
14913 /* Generate module vtables subject to their accessibility and their not
14914 being vtables or pdt templates. If this is not done class declarations
14915 in external procedures wind up with their own version and so SELECT TYPE
14916 fails because the vptrs do not have the same address. */
14917 if (gfc_option
.allow_std
& GFC_STD_F2003
14918 && sym
->ns
->proc_name
14919 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
14920 && sym
->attr
.access
!= ACCESS_PRIVATE
14921 && !(sym
->attr
.use_assoc
|| sym
->attr
.vtype
|| sym
->attr
.pdt_template
))
14923 gfc_symbol
*vtab
= gfc_find_derived_vtab (sym
);
14924 gfc_set_sym_referenced (vtab
);
14932 resolve_fl_namelist (gfc_symbol
*sym
)
14937 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
14939 /* Check again, the check in match only works if NAMELIST comes
14941 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SIZE
)
14943 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14944 "allowed", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
14948 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
14949 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object %qs "
14950 "with assumed shape in namelist %qs at %L",
14951 nl
->sym
->name
, sym
->name
, &sym
->declared_at
))
14954 if (is_non_constant_shape_array (nl
->sym
)
14955 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object %qs "
14956 "with nonconstant shape in namelist %qs at %L",
14957 nl
->sym
->name
, sym
->name
, &sym
->declared_at
))
14960 if (nl
->sym
->ts
.type
== BT_CHARACTER
14961 && (nl
->sym
->ts
.u
.cl
->length
== NULL
14962 || !gfc_is_constant_expr (nl
->sym
->ts
.u
.cl
->length
))
14963 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST object %qs with "
14964 "nonconstant character length in "
14965 "namelist %qs at %L", nl
->sym
->name
,
14966 sym
->name
, &sym
->declared_at
))
14971 /* Reject PRIVATE objects in a PUBLIC namelist. */
14972 if (gfc_check_symbol_access (sym
))
14974 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
14976 if (!nl
->sym
->attr
.use_assoc
14977 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
14978 && !gfc_check_symbol_access (nl
->sym
))
14980 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14981 "cannot be member of PUBLIC namelist %qs at %L",
14982 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
14986 if (nl
->sym
->ts
.type
== BT_DERIVED
14987 && (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
14988 || nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
))
14990 if (!gfc_notify_std (GFC_STD_F2003
, "NAMELIST object %qs in "
14991 "namelist %qs at %L with ALLOCATABLE "
14992 "or POINTER components", nl
->sym
->name
,
14993 sym
->name
, &sym
->declared_at
))
14998 /* Types with private components that came here by USE-association. */
14999 if (nl
->sym
->ts
.type
== BT_DERIVED
15000 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
15002 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15003 "components and cannot be member of namelist %qs at %L",
15004 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
15008 /* Types with private components that are defined in the same module. */
15009 if (nl
->sym
->ts
.type
== BT_DERIVED
15010 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
15011 && nl
->sym
->ts
.u
.derived
->attr
.private_comp
)
15013 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15014 "cannot be a member of PUBLIC namelist %qs at %L",
15015 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
15022 /* 14.1.2 A module or internal procedure represent local entities
15023 of the same type as a namelist member and so are not allowed. */
15024 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
15026 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
15029 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
15030 if ((nl
->sym
== sym
->ns
->proc_name
)
15032 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
15037 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
15038 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
15040 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15041 "attribute in %qs at %L", nlsym
->name
,
15042 &sym
->declared_at
);
15052 resolve_fl_parameter (gfc_symbol
*sym
)
15054 /* A parameter array's shape needs to be constant. */
15055 if (sym
->as
!= NULL
15056 && (sym
->as
->type
== AS_DEFERRED
15057 || is_non_constant_shape_array (sym
)))
15059 gfc_error ("Parameter array %qs at %L cannot be automatic "
15060 "or of deferred shape", sym
->name
, &sym
->declared_at
);
15064 /* Constraints on deferred type parameter. */
15065 if (!deferred_requirements (sym
))
15068 /* Make sure a parameter that has been implicitly typed still
15069 matches the implicit type, since PARAMETER statements can precede
15070 IMPLICIT statements. */
15071 if (sym
->attr
.implicit_type
15072 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
15075 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15076 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
15080 /* Make sure the types of derived parameters are consistent. This
15081 type checking is deferred until resolution because the type may
15082 refer to a derived type from the host. */
15083 if (sym
->ts
.type
== BT_DERIVED
15084 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
15086 gfc_error ("Incompatible derived type in PARAMETER at %L",
15087 &sym
->value
->where
);
15091 /* F03:C509,C514. */
15092 if (sym
->ts
.type
== BT_CLASS
)
15094 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15095 sym
->name
, &sym
->declared_at
);
15103 /* Called by resolve_symbol to check PDTs. */
15106 resolve_pdt (gfc_symbol
* sym
)
15108 gfc_symbol
*derived
= NULL
;
15109 gfc_actual_arglist
*param
;
15111 bool const_len_exprs
= true;
15112 bool assumed_len_exprs
= false;
15113 symbol_attribute
*attr
;
15115 if (sym
->ts
.type
== BT_DERIVED
)
15117 derived
= sym
->ts
.u
.derived
;
15118 attr
= &(sym
->attr
);
15120 else if (sym
->ts
.type
== BT_CLASS
)
15122 derived
= CLASS_DATA (sym
)->ts
.u
.derived
;
15123 attr
= &(CLASS_DATA (sym
)->attr
);
15126 gcc_unreachable ();
15128 gcc_assert (derived
->attr
.pdt_type
);
15130 for (param
= sym
->param_list
; param
; param
= param
->next
)
15132 c
= gfc_find_component (derived
, param
->name
, false, true, NULL
);
15134 if (c
->attr
.pdt_kind
)
15137 if (param
->expr
&& !gfc_is_constant_expr (param
->expr
)
15138 && c
->attr
.pdt_len
)
15139 const_len_exprs
= false;
15140 else if (param
->spec_type
== SPEC_ASSUMED
)
15141 assumed_len_exprs
= true;
15143 if (param
->spec_type
== SPEC_DEFERRED
15144 && !attr
->allocatable
&& !attr
->pointer
)
15145 gfc_error ("The object %qs at %L has a deferred LEN "
15146 "parameter %qs and is neither allocatable "
15147 "nor a pointer", sym
->name
, &sym
->declared_at
,
15152 if (!const_len_exprs
15153 && (sym
->ns
->proc_name
->attr
.is_main_program
15154 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15155 || sym
->attr
.save
!= SAVE_NONE
))
15156 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15157 "SAVE attribute or be a variable declared in the "
15158 "main program, a module or a submodule(F08/C513)",
15159 sym
->name
, &sym
->declared_at
);
15161 if (assumed_len_exprs
&& !(sym
->attr
.dummy
15162 || sym
->attr
.select_type_temporary
|| sym
->attr
.associate_var
))
15163 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15164 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15165 sym
->name
, &sym
->declared_at
);
15169 /* Do anything necessary to resolve a symbol. Right now, we just
15170 assume that an otherwise unknown symbol is a variable. This sort
15171 of thing commonly happens for symbols in module. */
15174 resolve_symbol (gfc_symbol
*sym
)
15176 int check_constant
, mp_flag
;
15177 gfc_symtree
*symtree
;
15178 gfc_symtree
*this_symtree
;
15181 symbol_attribute class_attr
;
15182 gfc_array_spec
*as
;
15183 bool saved_specification_expr
;
15185 if (sym
->resolve_symbol_called
>= 1)
15187 sym
->resolve_symbol_called
= 1;
15189 /* No symbol will ever have union type; only components can be unions.
15190 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15191 (just like derived type declaration symbols have flavor FL_DERIVED). */
15192 gcc_assert (sym
->ts
.type
!= BT_UNION
);
15194 /* Coarrayed polymorphic objects with allocatable or pointer components are
15195 yet unsupported for -fcoarray=lib. */
15196 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->ts
.type
== BT_CLASS
15197 && sym
->ts
.u
.derived
&& CLASS_DATA (sym
)
15198 && CLASS_DATA (sym
)->attr
.codimension
15199 && CLASS_DATA (sym
)->ts
.u
.derived
15200 && (CLASS_DATA (sym
)->ts
.u
.derived
->attr
.alloc_comp
15201 || CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pointer_comp
))
15203 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15204 "type coarrays at %L are unsupported", &sym
->declared_at
);
15208 if (sym
->attr
.artificial
)
15211 if (sym
->attr
.unlimited_polymorphic
)
15214 if (sym
->attr
.flavor
== FL_UNKNOWN
15215 || (sym
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.intrinsic
15216 && !sym
->attr
.generic
&& !sym
->attr
.external
15217 && sym
->attr
.if_source
== IFSRC_UNKNOWN
15218 && sym
->ts
.type
== BT_UNKNOWN
))
15221 /* If we find that a flavorless symbol is an interface in one of the
15222 parent namespaces, find its symtree in this namespace, free the
15223 symbol and set the symtree to point to the interface symbol. */
15224 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
15226 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
15227 if (symtree
&& (symtree
->n
.sym
->generic
||
15228 (symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
15229 && sym
->ns
->construct_entities
)))
15231 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
15233 if (this_symtree
->n
.sym
== sym
)
15235 symtree
->n
.sym
->refs
++;
15236 gfc_release_symbol (sym
);
15237 this_symtree
->n
.sym
= symtree
->n
.sym
;
15243 /* Otherwise give it a flavor according to such attributes as
15245 if (sym
->attr
.flavor
== FL_UNKNOWN
&& sym
->attr
.external
== 0
15246 && sym
->attr
.intrinsic
== 0)
15247 sym
->attr
.flavor
= FL_VARIABLE
;
15248 else if (sym
->attr
.flavor
== FL_UNKNOWN
)
15250 sym
->attr
.flavor
= FL_PROCEDURE
;
15251 if (sym
->attr
.dimension
)
15252 sym
->attr
.function
= 1;
15256 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
15257 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
15259 if (sym
->attr
.procedure
&& sym
->attr
.if_source
!= IFSRC_DECL
15260 && !resolve_procedure_interface (sym
))
15263 if (sym
->attr
.is_protected
&& !sym
->attr
.proc_pointer
15264 && (sym
->attr
.procedure
|| sym
->attr
.external
))
15266 if (sym
->attr
.external
)
15267 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15268 "at %L", &sym
->declared_at
);
15270 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15271 "at %L", &sym
->declared_at
);
15276 if (sym
->attr
.flavor
== FL_DERIVED
&& !resolve_fl_derived (sym
))
15279 else if ((sym
->attr
.flavor
== FL_STRUCT
|| sym
->attr
.flavor
== FL_UNION
)
15280 && !resolve_fl_struct (sym
))
15283 /* Symbols that are module procedures with results (functions) have
15284 the types and array specification copied for type checking in
15285 procedures that call them, as well as for saving to a module
15286 file. These symbols can't stand the scrutiny that their results
15288 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
15290 /* Make sure that the intrinsic is consistent with its internal
15291 representation. This needs to be done before assigning a default
15292 type to avoid spurious warnings. */
15293 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
15294 && !gfc_resolve_intrinsic (sym
, &sym
->declared_at
))
15297 /* Resolve associate names. */
15299 resolve_assoc_var (sym
, true);
15301 /* Assign default type to symbols that need one and don't have one. */
15302 if (sym
->ts
.type
== BT_UNKNOWN
)
15304 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
15306 gfc_set_default_type (sym
, 1, NULL
);
15309 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
15310 && !sym
->attr
.function
&& !sym
->attr
.subroutine
15311 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
15312 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
15314 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
15316 /* The specific case of an external procedure should emit an error
15317 in the case that there is no implicit type. */
15320 if (!sym
->attr
.mixed_entry_master
)
15321 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
15325 /* Result may be in another namespace. */
15326 resolve_symbol (sym
->result
);
15328 if (!sym
->result
->attr
.proc_pointer
)
15330 sym
->ts
= sym
->result
->ts
;
15331 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
15332 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
15333 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
15334 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
15335 sym
->attr
.contiguous
= sym
->result
->attr
.contiguous
;
15340 else if (mp_flag
&& sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
15342 bool saved_specification_expr
= specification_expr
;
15343 specification_expr
= true;
15344 gfc_resolve_array_spec (sym
->result
->as
, false);
15345 specification_expr
= saved_specification_expr
;
15348 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
15350 as
= CLASS_DATA (sym
)->as
;
15351 class_attr
= CLASS_DATA (sym
)->attr
;
15352 class_attr
.pointer
= class_attr
.class_pointer
;
15356 class_attr
= sym
->attr
;
15361 if (sym
->attr
.contiguous
15362 && (!class_attr
.dimension
15363 || (as
->type
!= AS_ASSUMED_SHAPE
&& as
->type
!= AS_ASSUMED_RANK
15364 && !class_attr
.pointer
)))
15366 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15367 "array pointer or an assumed-shape or assumed-rank array",
15368 sym
->name
, &sym
->declared_at
);
15372 /* Assumed size arrays and assumed shape arrays must be dummy
15373 arguments. Array-spec's of implied-shape should have been resolved to
15374 AS_EXPLICIT already. */
15378 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15379 specification expression. */
15380 if (as
->type
== AS_IMPLIED_SHAPE
)
15383 for (i
=0; i
<as
->rank
; i
++)
15385 if (as
->lower
[i
] != NULL
&& as
->upper
[i
] == NULL
)
15387 gfc_error ("Bad specification for assumed size array at %L",
15388 &as
->lower
[i
]->where
);
15395 if (((as
->type
== AS_ASSUMED_SIZE
&& !as
->cp_was_assumed
)
15396 || as
->type
== AS_ASSUMED_SHAPE
)
15397 && !sym
->attr
.dummy
&& !sym
->attr
.select_type_temporary
)
15399 if (as
->type
== AS_ASSUMED_SIZE
)
15400 gfc_error ("Assumed size array at %L must be a dummy argument",
15401 &sym
->declared_at
);
15403 gfc_error ("Assumed shape array at %L must be a dummy argument",
15404 &sym
->declared_at
);
15407 /* TS 29113, C535a. */
15408 if (as
->type
== AS_ASSUMED_RANK
&& !sym
->attr
.dummy
15409 && !sym
->attr
.select_type_temporary
15410 && !(cs_base
&& cs_base
->current
15411 && cs_base
->current
->op
== EXEC_SELECT_RANK
))
15413 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15414 &sym
->declared_at
);
15417 if (as
->type
== AS_ASSUMED_RANK
15418 && (sym
->attr
.codimension
|| sym
->attr
.value
))
15420 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15421 "CODIMENSION attribute", &sym
->declared_at
);
15426 /* Make sure symbols with known intent or optional are really dummy
15427 variable. Because of ENTRY statement, this has to be deferred
15428 until resolution time. */
15430 if (!sym
->attr
.dummy
15431 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
15433 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
15437 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
15439 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15440 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
15444 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
15446 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
15447 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
15449 gfc_error ("Character dummy variable %qs at %L with VALUE "
15450 "attribute must have constant length",
15451 sym
->name
, &sym
->declared_at
);
15455 if (sym
->ts
.is_c_interop
15456 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
15458 gfc_error ("C interoperable character dummy variable %qs at %L "
15459 "with VALUE attribute must have length one",
15460 sym
->name
, &sym
->declared_at
);
15465 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->attr
.is_iso_c
15466 && sym
->ts
.u
.derived
->attr
.generic
)
15468 sym
->ts
.u
.derived
= gfc_find_dt_in_generic (sym
->ts
.u
.derived
);
15469 if (!sym
->ts
.u
.derived
)
15471 gfc_error ("The derived type %qs at %L is of type %qs, "
15472 "which has not been defined", sym
->name
,
15473 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
15474 sym
->ts
.type
= BT_UNKNOWN
;
15479 /* Use the same constraints as TYPE(*), except for the type check
15480 and that only scalars and assumed-size arrays are permitted. */
15481 if (sym
->attr
.ext_attr
& (1 << EXT_ATTR_NO_ARG_CHECK
))
15483 if (!sym
->attr
.dummy
)
15485 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15486 "a dummy argument", sym
->name
, &sym
->declared_at
);
15490 if (sym
->ts
.type
!= BT_ASSUMED
&& sym
->ts
.type
!= BT_INTEGER
15491 && sym
->ts
.type
!= BT_REAL
&& sym
->ts
.type
!= BT_LOGICAL
15492 && sym
->ts
.type
!= BT_COMPLEX
)
15494 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15495 "of type TYPE(*) or of an numeric intrinsic type",
15496 sym
->name
, &sym
->declared_at
);
15500 if (sym
->attr
.allocatable
|| sym
->attr
.codimension
15501 || sym
->attr
.pointer
|| sym
->attr
.value
)
15503 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15504 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15505 "attribute", sym
->name
, &sym
->declared_at
);
15509 if (sym
->attr
.intent
== INTENT_OUT
)
15511 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15512 "have the INTENT(OUT) attribute",
15513 sym
->name
, &sym
->declared_at
);
15516 if (sym
->attr
.dimension
&& sym
->as
->type
!= AS_ASSUMED_SIZE
)
15518 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15519 "either be a scalar or an assumed-size array",
15520 sym
->name
, &sym
->declared_at
);
15524 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15525 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15527 sym
->ts
.type
= BT_ASSUMED
;
15528 sym
->as
= gfc_get_array_spec ();
15529 sym
->as
->type
= AS_ASSUMED_SIZE
;
15531 sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
15533 else if (sym
->ts
.type
== BT_ASSUMED
)
15535 /* TS 29113, C407a. */
15536 if (!sym
->attr
.dummy
)
15538 gfc_error ("Assumed type of variable %s at %L is only permitted "
15539 "for dummy variables", sym
->name
, &sym
->declared_at
);
15542 if (sym
->attr
.allocatable
|| sym
->attr
.codimension
15543 || sym
->attr
.pointer
|| sym
->attr
.value
)
15545 gfc_error ("Assumed-type variable %s at %L may not have the "
15546 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15547 sym
->name
, &sym
->declared_at
);
15550 if (sym
->attr
.intent
== INTENT_OUT
)
15552 gfc_error ("Assumed-type variable %s at %L may not have the "
15553 "INTENT(OUT) attribute",
15554 sym
->name
, &sym
->declared_at
);
15557 if (sym
->attr
.dimension
&& sym
->as
->type
== AS_EXPLICIT
)
15559 gfc_error ("Assumed-type variable %s at %L shall not be an "
15560 "explicit-shape array", sym
->name
, &sym
->declared_at
);
15565 /* If the symbol is marked as bind(c), that it is declared at module level
15566 scope and verify its type and kind. Do not do the latter for symbols
15567 that are implicitly typed because that is handled in
15568 gfc_set_default_type. Handle dummy arguments and procedure definitions
15569 separately. Also, anything that is use associated is not handled here
15570 but instead is handled in the module it is declared in. Finally, derived
15571 type definitions are allowed to be BIND(C) since that only implies that
15572 they're interoperable, and they are checked fully for interoperability
15573 when a variable is declared of that type. */
15574 if (sym
->attr
.is_bind_c
&& sym
->attr
.use_assoc
== 0
15575 && sym
->attr
.dummy
== 0 && sym
->attr
.flavor
!= FL_PROCEDURE
15576 && sym
->attr
.flavor
!= FL_DERIVED
)
15580 /* First, make sure the variable is declared at the
15581 module-level scope (J3/04-007, Section 15.3). */
15582 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
15583 sym
->attr
.in_common
== 0)
15585 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15586 "is neither a COMMON block nor declared at the "
15587 "module level scope", sym
->name
, &(sym
->declared_at
));
15590 else if (sym
->ts
.type
== BT_CHARACTER
15591 && (sym
->ts
.u
.cl
== NULL
|| sym
->ts
.u
.cl
->length
== NULL
15592 || !gfc_is_constant_expr (sym
->ts
.u
.cl
->length
)
15593 || mpz_cmp_si (sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0))
15595 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15596 sym
->name
, &sym
->declared_at
);
15599 else if (sym
->common_head
!= NULL
&& sym
->attr
.implicit_type
== 0)
15601 t
= verify_com_block_vars_c_interop (sym
->common_head
);
15603 else if (sym
->attr
.implicit_type
== 0)
15605 /* If type() declaration, we need to verify that the components
15606 of the given type are all C interoperable, etc. */
15607 if (sym
->ts
.type
== BT_DERIVED
&&
15608 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
15610 /* Make sure the user marked the derived type as BIND(C). If
15611 not, call the verify routine. This could print an error
15612 for the derived type more than once if multiple variables
15613 of that type are declared. */
15614 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
15615 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
15619 /* Verify the variable itself as C interoperable if it
15620 is BIND(C). It is not possible for this to succeed if
15621 the verify_bind_c_derived_type failed, so don't have to handle
15622 any error returned by verify_bind_c_derived_type. */
15623 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
15624 sym
->common_block
);
15629 /* clear the is_bind_c flag to prevent reporting errors more than
15630 once if something failed. */
15631 sym
->attr
.is_bind_c
= 0;
15636 /* If a derived type symbol has reached this point, without its
15637 type being declared, we have an error. Notice that most
15638 conditions that produce undefined derived types have already
15639 been dealt with. However, the likes of:
15640 implicit type(t) (t) ..... call foo (t) will get us here if
15641 the type is not declared in the scope of the implicit
15642 statement. Change the type to BT_UNKNOWN, both because it is so
15643 and to prevent an ICE. */
15644 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->attr
.is_iso_c
15645 && sym
->ts
.u
.derived
->components
== NULL
15646 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
15648 gfc_error ("The derived type %qs at %L is of type %qs, "
15649 "which has not been defined", sym
->name
,
15650 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
15651 sym
->ts
.type
= BT_UNKNOWN
;
15655 /* Make sure that the derived type has been resolved and that the
15656 derived type is visible in the symbol's namespace, if it is a
15657 module function and is not PRIVATE. */
15658 if (sym
->ts
.type
== BT_DERIVED
15659 && sym
->ts
.u
.derived
->attr
.use_assoc
15660 && sym
->ns
->proc_name
15661 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15662 && !resolve_fl_derived (sym
->ts
.u
.derived
))
15665 /* Unless the derived-type declaration is use associated, Fortran 95
15666 does not allow public entries of private derived types.
15667 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15668 161 in 95-006r3. */
15669 if (sym
->ts
.type
== BT_DERIVED
15670 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15671 && !sym
->ts
.u
.derived
->attr
.use_assoc
15672 && gfc_check_symbol_access (sym
)
15673 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
15674 && !gfc_notify_std (GFC_STD_F2003
, "PUBLIC %s %qs at %L of PRIVATE "
15675 "derived type %qs",
15676 (sym
->attr
.flavor
== FL_PARAMETER
)
15677 ? "parameter" : "variable",
15678 sym
->name
, &sym
->declared_at
,
15679 sym
->ts
.u
.derived
->name
))
15682 /* F2008, C1302. */
15683 if (sym
->ts
.type
== BT_DERIVED
15684 && ((sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
15685 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
15686 || sym
->ts
.u
.derived
->attr
.lock_comp
)
15687 && !sym
->attr
.codimension
&& !sym
->ts
.u
.derived
->attr
.coarray_comp
)
15689 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15690 "type LOCK_TYPE must be a coarray", sym
->name
,
15691 &sym
->declared_at
);
15695 /* TS18508, C702/C703. */
15696 if (sym
->ts
.type
== BT_DERIVED
15697 && ((sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
15698 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
15699 || sym
->ts
.u
.derived
->attr
.event_comp
)
15700 && !sym
->attr
.codimension
&& !sym
->ts
.u
.derived
->attr
.coarray_comp
)
15702 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15703 "type EVENT_TYPE must be a coarray", sym
->name
,
15704 &sym
->declared_at
);
15708 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15709 default initialization is defined (5.1.2.4.4). */
15710 if (sym
->ts
.type
== BT_DERIVED
15712 && sym
->attr
.intent
== INTENT_OUT
15714 && sym
->as
->type
== AS_ASSUMED_SIZE
)
15716 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
15718 if (c
->initializer
)
15720 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15721 "ASSUMED SIZE and so cannot have a default initializer",
15722 sym
->name
, &sym
->declared_at
);
15729 if (sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.dummy
15730 && sym
->attr
.intent
== INTENT_OUT
&& sym
->attr
.lock_comp
)
15732 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15733 "INTENT(OUT)", sym
->name
, &sym
->declared_at
);
15738 if (sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.dummy
15739 && sym
->attr
.intent
== INTENT_OUT
&& sym
->attr
.event_comp
)
15741 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15742 "INTENT(OUT)", sym
->name
, &sym
->declared_at
);
15747 if ((((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15748 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15749 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15750 || class_attr
.codimension
)
15751 && (sym
->attr
.result
|| sym
->result
== sym
))
15753 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15754 "a coarray component", sym
->name
, &sym
->declared_at
);
15759 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
15760 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
15762 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15763 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
15768 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15769 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15770 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15771 && (class_attr
.codimension
|| class_attr
.pointer
|| class_attr
.dimension
15772 || class_attr
.allocatable
))
15774 gfc_error ("Variable %qs at %L with coarray component shall be a "
15775 "nonpointer, nonallocatable scalar, which is not a coarray",
15776 sym
->name
, &sym
->declared_at
);
15780 /* F2008, C526. The function-result case was handled above. */
15781 if (class_attr
.codimension
15782 && !(class_attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
15783 || sym
->attr
.select_type_temporary
15784 || sym
->attr
.associate_var
15785 || (sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15786 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15787 || sym
->ns
->proc_name
->attr
.is_main_program
15788 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
15790 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15791 "nor a dummy argument", sym
->name
, &sym
->declared_at
);
15795 else if (class_attr
.codimension
&& !sym
->attr
.select_type_temporary
15796 && !class_attr
.allocatable
&& as
&& as
->cotype
== AS_DEFERRED
)
15798 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15799 "deferred shape", sym
->name
, &sym
->declared_at
);
15802 else if (class_attr
.codimension
&& class_attr
.allocatable
&& as
15803 && (as
->cotype
!= AS_DEFERRED
|| as
->type
!= AS_DEFERRED
))
15805 gfc_error ("Allocatable coarray variable %qs at %L must have "
15806 "deferred shape", sym
->name
, &sym
->declared_at
);
15811 if ((((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15812 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15813 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15814 || (class_attr
.codimension
&& class_attr
.allocatable
))
15815 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
15817 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15818 "allocatable coarray or have coarray components",
15819 sym
->name
, &sym
->declared_at
);
15823 if (class_attr
.codimension
&& sym
->attr
.dummy
15824 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
15826 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15827 "procedure %qs", sym
->name
, &sym
->declared_at
,
15828 sym
->ns
->proc_name
->name
);
15832 if (sym
->ts
.type
== BT_LOGICAL
15833 && ((sym
->attr
.function
&& sym
->attr
.is_bind_c
&& sym
->result
== sym
)
15834 || ((sym
->attr
.dummy
|| sym
->attr
.result
) && sym
->ns
->proc_name
15835 && sym
->ns
->proc_name
->attr
.is_bind_c
)))
15838 for (i
= 0; gfc_logical_kinds
[i
].kind
; i
++)
15839 if (gfc_logical_kinds
[i
].kind
== sym
->ts
.kind
)
15841 if (!gfc_logical_kinds
[i
].c_bool
&& sym
->attr
.dummy
15842 && !gfc_notify_std (GFC_STD_GNU
, "LOGICAL dummy argument %qs at "
15843 "%L with non-C_Bool kind in BIND(C) procedure "
15844 "%qs", sym
->name
, &sym
->declared_at
,
15845 sym
->ns
->proc_name
->name
))
15847 else if (!gfc_logical_kinds
[i
].c_bool
15848 && !gfc_notify_std (GFC_STD_GNU
, "LOGICAL result variable "
15849 "%qs at %L with non-C_Bool kind in "
15850 "BIND(C) procedure %qs", sym
->name
,
15852 sym
->attr
.function
? sym
->name
15853 : sym
->ns
->proc_name
->name
))
15857 switch (sym
->attr
.flavor
)
15860 if (!resolve_fl_variable (sym
, mp_flag
))
15865 if (sym
->formal
&& !sym
->formal_ns
)
15867 /* Check that none of the arguments are a namelist. */
15868 gfc_formal_arglist
*formal
= sym
->formal
;
15870 for (; formal
; formal
= formal
->next
)
15871 if (formal
->sym
&& formal
->sym
->attr
.flavor
== FL_NAMELIST
)
15873 gfc_error ("Namelist %qs cannot be an argument to "
15874 "subroutine or function at %L",
15875 formal
->sym
->name
, &sym
->declared_at
);
15880 if (!resolve_fl_procedure (sym
, mp_flag
))
15885 if (!resolve_fl_namelist (sym
))
15890 if (!resolve_fl_parameter (sym
))
15898 /* Resolve array specifier. Check as well some constraints
15899 on COMMON blocks. */
15901 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
15903 /* Set the formal_arg_flag so that check_conflict will not throw
15904 an error for host associated variables in the specification
15905 expression for an array_valued function. */
15906 if ((sym
->attr
.function
|| sym
->attr
.result
) && sym
->as
)
15907 formal_arg_flag
= true;
15909 saved_specification_expr
= specification_expr
;
15910 specification_expr
= true;
15911 gfc_resolve_array_spec (sym
->as
, check_constant
);
15912 specification_expr
= saved_specification_expr
;
15914 formal_arg_flag
= false;
15916 /* Resolve formal namespaces. */
15917 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
15918 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
15919 gfc_resolve (sym
->formal_ns
);
15921 /* Make sure the formal namespace is present. */
15922 if (sym
->formal
&& !sym
->formal_ns
)
15924 gfc_formal_arglist
*formal
= sym
->formal
;
15925 while (formal
&& !formal
->sym
)
15926 formal
= formal
->next
;
15930 sym
->formal_ns
= formal
->sym
->ns
;
15931 if (sym
->formal_ns
&& sym
->ns
!= formal
->sym
->ns
)
15932 sym
->formal_ns
->refs
++;
15936 /* Check threadprivate restrictions. */
15937 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
15938 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15939 && (!sym
->attr
.in_common
15940 && sym
->module
== NULL
15941 && (sym
->ns
->proc_name
== NULL
15942 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
15943 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
15945 /* Check omp declare target restrictions. */
15946 if (sym
->attr
.omp_declare_target
15947 && sym
->attr
.flavor
== FL_VARIABLE
15949 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15950 && (!sym
->attr
.in_common
15951 && sym
->module
== NULL
15952 && (sym
->ns
->proc_name
== NULL
15953 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
15954 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15955 sym
->name
, &sym
->declared_at
);
15957 /* If we have come this far we can apply default-initializers, as
15958 described in 14.7.5, to those variables that have not already
15959 been assigned one. */
15960 if (sym
->ts
.type
== BT_DERIVED
15962 && !sym
->attr
.allocatable
15963 && !sym
->attr
.alloc_comp
)
15965 symbol_attribute
*a
= &sym
->attr
;
15967 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
15968 && !a
->in_common
&& !a
->use_assoc
15970 && !((a
->function
|| a
->result
)
15972 || sym
->ts
.u
.derived
->attr
.alloc_comp
15973 || sym
->ts
.u
.derived
->attr
.pointer_comp
))
15974 && !(a
->function
&& sym
!= sym
->result
))
15975 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
15976 apply_default_init (sym
);
15977 else if (a
->function
&& sym
->result
&& a
->access
!= ACCESS_PRIVATE
15978 && (sym
->ts
.u
.derived
->attr
.alloc_comp
15979 || sym
->ts
.u
.derived
->attr
.pointer_comp
))
15980 /* Mark the result symbol to be referenced, when it has allocatable
15982 sym
->result
->attr
.referenced
= 1;
15985 if (sym
->ts
.type
== BT_CLASS
&& sym
->ns
== gfc_current_ns
15986 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
15987 && !CLASS_DATA (sym
)->attr
.class_pointer
15988 && !CLASS_DATA (sym
)->attr
.allocatable
)
15989 apply_default_init (sym
);
15991 /* If this symbol has a type-spec, check it. */
15992 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
15993 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
15994 if (!resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
))
15997 if (sym
->param_list
)
16002 /************* Resolve DATA statements *************/
16006 gfc_data_value
*vnode
;
16012 /* Advance the values structure to point to the next value in the data list. */
16015 next_data_value (void)
16017 while (mpz_cmp_ui (values
.left
, 0) == 0)
16020 if (values
.vnode
->next
== NULL
)
16023 values
.vnode
= values
.vnode
->next
;
16024 mpz_set (values
.left
, values
.vnode
->repeat
);
16032 check_data_variable (gfc_data_variable
*var
, locus
*where
)
16038 ar_type mark
= AR_UNKNOWN
;
16040 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
16046 if (!gfc_resolve_expr (var
->expr
))
16050 mpz_init_set_si (offset
, 0);
16053 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
16054 && e
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
16055 e
= e
->value
.function
.actual
->expr
;
16057 if (e
->expr_type
!= EXPR_VARIABLE
)
16059 gfc_error ("Expecting definable entity near %L", where
);
16063 sym
= e
->symtree
->n
.sym
;
16065 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
16067 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16068 sym
->name
, &sym
->declared_at
);
16072 if (e
->ref
== NULL
&& sym
->as
)
16074 gfc_error ("DATA array %qs at %L must be specified in a previous"
16075 " declaration", sym
->name
, where
);
16079 if (gfc_is_coindexed (e
))
16081 gfc_error ("DATA element %qs at %L cannot have a coindex", sym
->name
,
16086 has_pointer
= sym
->attr
.pointer
;
16088 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
16090 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
16095 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
!= AR_FULL
)
16097 gfc_error ("DATA element %qs at %L is a pointer and so must "
16098 "be a full array", sym
->name
, where
);
16102 if (values
.vnode
->expr
->expr_type
== EXPR_CONSTANT
)
16104 gfc_error ("DATA object near %L has the pointer attribute "
16105 "and the corresponding DATA value is not a valid "
16106 "initial-data-target", where
);
16112 if (e
->rank
== 0 || has_pointer
)
16114 mpz_init_set_ui (size
, 1);
16121 /* Find the array section reference. */
16122 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
16124 if (ref
->type
!= REF_ARRAY
)
16126 if (ref
->u
.ar
.type
== AR_ELEMENT
)
16132 /* Set marks according to the reference pattern. */
16133 switch (ref
->u
.ar
.type
)
16141 /* Get the start position of array section. */
16142 gfc_get_section_index (ar
, section_index
, &offset
);
16147 gcc_unreachable ();
16150 if (!gfc_array_size (e
, &size
))
16152 gfc_error ("Nonconstant array section at %L in DATA statement",
16154 mpz_clear (offset
);
16161 while (mpz_cmp_ui (size
, 0) > 0)
16163 if (!next_data_value ())
16165 gfc_error ("DATA statement at %L has more variables than values",
16171 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
16175 /* If we have more than one element left in the repeat count,
16176 and we have more than one element left in the target variable,
16177 then create a range assignment. */
16178 /* FIXME: Only done for full arrays for now, since array sections
16180 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
16181 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
16185 if (mpz_cmp (size
, values
.left
) >= 0)
16187 mpz_init_set (range
, values
.left
);
16188 mpz_sub (size
, size
, values
.left
);
16189 mpz_set_ui (values
.left
, 0);
16193 mpz_init_set (range
, size
);
16194 mpz_sub (values
.left
, values
.left
, size
);
16195 mpz_set_ui (size
, 0);
16198 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
16201 mpz_add (offset
, offset
, range
);
16208 /* Assign initial value to symbol. */
16211 mpz_sub_ui (values
.left
, values
.left
, 1);
16212 mpz_sub_ui (size
, size
, 1);
16214 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
16219 if (mark
== AR_FULL
)
16220 mpz_add_ui (offset
, offset
, 1);
16222 /* Modify the array section indexes and recalculate the offset
16223 for next element. */
16224 else if (mark
== AR_SECTION
)
16225 gfc_advance_section (section_index
, ar
, &offset
);
16229 if (mark
== AR_SECTION
)
16231 for (i
= 0; i
< ar
->dimen
; i
++)
16232 mpz_clear (section_index
[i
]);
16236 mpz_clear (offset
);
16242 static bool traverse_data_var (gfc_data_variable
*, locus
*);
16244 /* Iterate over a list of elements in a DATA statement. */
16247 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
16250 iterator_stack frame
;
16251 gfc_expr
*e
, *start
, *end
, *step
;
16252 bool retval
= true;
16254 mpz_init (frame
.value
);
16257 start
= gfc_copy_expr (var
->iter
.start
);
16258 end
= gfc_copy_expr (var
->iter
.end
);
16259 step
= gfc_copy_expr (var
->iter
.step
);
16261 if (!gfc_simplify_expr (start
, 1)
16262 || start
->expr_type
!= EXPR_CONSTANT
)
16264 gfc_error ("start of implied-do loop at %L could not be "
16265 "simplified to a constant value", &start
->where
);
16269 if (!gfc_simplify_expr (end
, 1)
16270 || end
->expr_type
!= EXPR_CONSTANT
)
16272 gfc_error ("end of implied-do loop at %L could not be "
16273 "simplified to a constant value", &start
->where
);
16277 if (!gfc_simplify_expr (step
, 1)
16278 || step
->expr_type
!= EXPR_CONSTANT
)
16280 gfc_error ("step of implied-do loop at %L could not be "
16281 "simplified to a constant value", &start
->where
);
16286 mpz_set (trip
, end
->value
.integer
);
16287 mpz_sub (trip
, trip
, start
->value
.integer
);
16288 mpz_add (trip
, trip
, step
->value
.integer
);
16290 mpz_div (trip
, trip
, step
->value
.integer
);
16292 mpz_set (frame
.value
, start
->value
.integer
);
16294 frame
.prev
= iter_stack
;
16295 frame
.variable
= var
->iter
.var
->symtree
;
16296 iter_stack
= &frame
;
16298 while (mpz_cmp_ui (trip
, 0) > 0)
16300 if (!traverse_data_var (var
->list
, where
))
16306 e
= gfc_copy_expr (var
->expr
);
16307 if (!gfc_simplify_expr (e
, 1))
16314 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
16316 mpz_sub_ui (trip
, trip
, 1);
16320 mpz_clear (frame
.value
);
16323 gfc_free_expr (start
);
16324 gfc_free_expr (end
);
16325 gfc_free_expr (step
);
16327 iter_stack
= frame
.prev
;
16332 /* Type resolve variables in the variable list of a DATA statement. */
16335 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
16339 for (; var
; var
= var
->next
)
16341 if (var
->expr
== NULL
)
16342 t
= traverse_data_list (var
, where
);
16344 t
= check_data_variable (var
, where
);
16354 /* Resolve the expressions and iterators associated with a data statement.
16355 This is separate from the assignment checking because data lists should
16356 only be resolved once. */
16359 resolve_data_variables (gfc_data_variable
*d
)
16361 for (; d
; d
= d
->next
)
16363 if (d
->list
== NULL
)
16365 if (!gfc_resolve_expr (d
->expr
))
16370 if (!gfc_resolve_iterator (&d
->iter
, false, true))
16373 if (!resolve_data_variables (d
->list
))
16382 /* Resolve a single DATA statement. We implement this by storing a pointer to
16383 the value list into static variables, and then recursively traversing the
16384 variables list, expanding iterators and such. */
16387 resolve_data (gfc_data
*d
)
16390 if (!resolve_data_variables (d
->var
))
16393 values
.vnode
= d
->value
;
16394 if (d
->value
== NULL
)
16395 mpz_set_ui (values
.left
, 0);
16397 mpz_set (values
.left
, d
->value
->repeat
);
16399 if (!traverse_data_var (d
->var
, &d
->where
))
16402 /* At this point, we better not have any values left. */
16404 if (next_data_value ())
16405 gfc_error ("DATA statement at %L has more values than variables",
16410 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16411 accessed by host or use association, is a dummy argument to a pure function,
16412 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16413 is storage associated with any such variable, shall not be used in the
16414 following contexts: (clients of this function). */
16416 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16417 procedure. Returns zero if assignment is OK, nonzero if there is a
16420 gfc_impure_variable (gfc_symbol
*sym
)
16425 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
16428 /* Check if the symbol's ns is inside the pure procedure. */
16429 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16433 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
16437 proc
= sym
->ns
->proc_name
;
16438 if (sym
->attr
.dummy
16439 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
16440 || proc
->attr
.function
))
16443 /* TODO: Sort out what can be storage associated, if anything, and include
16444 it here. In principle equivalences should be scanned but it does not
16445 seem to be possible to storage associate an impure variable this way. */
16450 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16451 current namespace is inside a pure procedure. */
16454 gfc_pure (gfc_symbol
*sym
)
16456 symbol_attribute attr
;
16461 /* Check if the current namespace or one of its parents
16462 belongs to a pure procedure. */
16463 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16465 sym
= ns
->proc_name
;
16469 if (attr
.flavor
== FL_PROCEDURE
&& attr
.pure
)
16477 return attr
.flavor
== FL_PROCEDURE
&& attr
.pure
;
16481 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16482 checks if the current namespace is implicitly pure. Note that this
16483 function returns false for a PURE procedure. */
16486 gfc_implicit_pure (gfc_symbol
*sym
)
16492 /* Check if the current procedure is implicit_pure. Walk up
16493 the procedure list until we find a procedure. */
16494 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16496 sym
= ns
->proc_name
;
16500 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16505 return sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.implicit_pure
16506 && !sym
->attr
.pure
;
16511 gfc_unset_implicit_pure (gfc_symbol
*sym
)
16517 /* Check if the current procedure is implicit_pure. Walk up
16518 the procedure list until we find a procedure. */
16519 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16521 sym
= ns
->proc_name
;
16525 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16530 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16531 sym
->attr
.implicit_pure
= 0;
16533 sym
->attr
.pure
= 0;
16537 /* Test whether the current procedure is elemental or not. */
16540 gfc_elemental (gfc_symbol
*sym
)
16542 symbol_attribute attr
;
16545 sym
= gfc_current_ns
->proc_name
;
16550 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
16554 /* Warn about unused labels. */
16557 warn_unused_fortran_label (gfc_st_label
*label
)
16562 warn_unused_fortran_label (label
->left
);
16564 if (label
->defined
== ST_LABEL_UNKNOWN
)
16567 switch (label
->referenced
)
16569 case ST_LABEL_UNKNOWN
:
16570 gfc_warning (OPT_Wunused_label
, "Label %d at %L defined but not used",
16571 label
->value
, &label
->where
);
16574 case ST_LABEL_BAD_TARGET
:
16575 gfc_warning (OPT_Wunused_label
,
16576 "Label %d at %L defined but cannot be used",
16577 label
->value
, &label
->where
);
16584 warn_unused_fortran_label (label
->right
);
16588 /* Returns the sequence type of a symbol or sequence. */
16591 sequence_type (gfc_typespec ts
)
16600 if (ts
.u
.derived
->components
== NULL
)
16601 return SEQ_NONDEFAULT
;
16603 result
= sequence_type (ts
.u
.derived
->components
->ts
);
16604 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
16605 if (sequence_type (c
->ts
) != result
)
16611 if (ts
.kind
!= gfc_default_character_kind
)
16612 return SEQ_NONDEFAULT
;
16614 return SEQ_CHARACTER
;
16617 if (ts
.kind
!= gfc_default_integer_kind
)
16618 return SEQ_NONDEFAULT
;
16620 return SEQ_NUMERIC
;
16623 if (!(ts
.kind
== gfc_default_real_kind
16624 || ts
.kind
== gfc_default_double_kind
))
16625 return SEQ_NONDEFAULT
;
16627 return SEQ_NUMERIC
;
16630 if (ts
.kind
!= gfc_default_complex_kind
)
16631 return SEQ_NONDEFAULT
;
16633 return SEQ_NUMERIC
;
16636 if (ts
.kind
!= gfc_default_logical_kind
)
16637 return SEQ_NONDEFAULT
;
16639 return SEQ_NUMERIC
;
16642 return SEQ_NONDEFAULT
;
16647 /* Resolve derived type EQUIVALENCE object. */
16650 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
16652 gfc_component
*c
= derived
->components
;
16657 /* Shall not be an object of nonsequence derived type. */
16658 if (!derived
->attr
.sequence
)
16660 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16661 "attribute to be an EQUIVALENCE object", sym
->name
,
16666 /* Shall not have allocatable components. */
16667 if (derived
->attr
.alloc_comp
)
16669 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16670 "components to be an EQUIVALENCE object",sym
->name
,
16675 if (sym
->attr
.in_common
&& gfc_has_default_initializer (sym
->ts
.u
.derived
))
16677 gfc_error ("Derived type variable %qs at %L with default "
16678 "initialization cannot be in EQUIVALENCE with a variable "
16679 "in COMMON", sym
->name
, &e
->where
);
16683 for (; c
; c
= c
->next
)
16685 if (gfc_bt_struct (c
->ts
.type
)
16686 && (!resolve_equivalence_derived(c
->ts
.u
.derived
, sym
, e
)))
16689 /* Shall not be an object of sequence derived type containing a pointer
16690 in the structure. */
16691 if (c
->attr
.pointer
)
16693 gfc_error ("Derived type variable %qs at %L with pointer "
16694 "component(s) cannot be an EQUIVALENCE object",
16695 sym
->name
, &e
->where
);
16703 /* Resolve equivalence object.
16704 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16705 an allocatable array, an object of nonsequence derived type, an object of
16706 sequence derived type containing a pointer at any level of component
16707 selection, an automatic object, a function name, an entry name, a result
16708 name, a named constant, a structure component, or a subobject of any of
16709 the preceding objects. A substring shall not have length zero. A
16710 derived type shall not have components with default initialization nor
16711 shall two objects of an equivalence group be initialized.
16712 Either all or none of the objects shall have an protected attribute.
16713 The simple constraints are done in symbol.c(check_conflict) and the rest
16714 are implemented here. */
16717 resolve_equivalence (gfc_equiv
*eq
)
16720 gfc_symbol
*first_sym
;
16723 locus
*last_where
= NULL
;
16724 seq_type eq_type
, last_eq_type
;
16725 gfc_typespec
*last_ts
;
16726 int object
, cnt_protected
;
16729 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
16731 first_sym
= eq
->expr
->symtree
->n
.sym
;
16735 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
16739 e
->ts
= e
->symtree
->n
.sym
->ts
;
16740 /* match_varspec might not know yet if it is seeing
16741 array reference or substring reference, as it doesn't
16743 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
16745 gfc_ref
*ref
= e
->ref
;
16746 sym
= e
->symtree
->n
.sym
;
16748 if (sym
->attr
.dimension
)
16750 ref
->u
.ar
.as
= sym
->as
;
16754 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16755 if (e
->ts
.type
== BT_CHARACTER
16757 && ref
->type
== REF_ARRAY
16758 && ref
->u
.ar
.dimen
== 1
16759 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
16760 && ref
->u
.ar
.stride
[0] == NULL
)
16762 gfc_expr
*start
= ref
->u
.ar
.start
[0];
16763 gfc_expr
*end
= ref
->u
.ar
.end
[0];
16766 /* Optimize away the (:) reference. */
16767 if (start
== NULL
&& end
== NULL
)
16770 e
->ref
= ref
->next
;
16772 e
->ref
->next
= ref
->next
;
16777 ref
->type
= REF_SUBSTRING
;
16779 start
= gfc_get_int_expr (gfc_charlen_int_kind
,
16781 ref
->u
.ss
.start
= start
;
16782 if (end
== NULL
&& e
->ts
.u
.cl
)
16783 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
16784 ref
->u
.ss
.end
= end
;
16785 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
16792 /* Any further ref is an error. */
16795 gcc_assert (ref
->type
== REF_ARRAY
);
16796 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16802 if (!gfc_resolve_expr (e
))
16805 sym
= e
->symtree
->n
.sym
;
16807 if (sym
->attr
.is_protected
)
16809 if (cnt_protected
> 0 && cnt_protected
!= object
)
16811 gfc_error ("Either all or none of the objects in the "
16812 "EQUIVALENCE set at %L shall have the "
16813 "PROTECTED attribute",
16818 /* Shall not equivalence common block variables in a PURE procedure. */
16819 if (sym
->ns
->proc_name
16820 && sym
->ns
->proc_name
->attr
.pure
16821 && sym
->attr
.in_common
)
16823 /* Need to check for symbols that may have entered the pure
16824 procedure via a USE statement. */
16825 bool saw_sym
= false;
16826 if (sym
->ns
->use_stmts
)
16829 for (r
= sym
->ns
->use_stmts
->rename
; r
; r
= r
->next
)
16830 if (strcmp(r
->use_name
, sym
->name
) == 0) saw_sym
= true;
16836 gfc_error ("COMMON block member %qs at %L cannot be an "
16837 "EQUIVALENCE object in the pure procedure %qs",
16838 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
16842 /* Shall not be a named constant. */
16843 if (e
->expr_type
== EXPR_CONSTANT
)
16845 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16846 "object", sym
->name
, &e
->where
);
16850 if (e
->ts
.type
== BT_DERIVED
16851 && !resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
))
16854 /* Check that the types correspond correctly:
16856 A numeric sequence structure may be equivalenced to another sequence
16857 structure, an object of default integer type, default real type, double
16858 precision real type, default logical type such that components of the
16859 structure ultimately only become associated to objects of the same
16860 kind. A character sequence structure may be equivalenced to an object
16861 of default character kind or another character sequence structure.
16862 Other objects may be equivalenced only to objects of the same type and
16863 kind parameters. */
16865 /* Identical types are unconditionally OK. */
16866 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
16867 goto identical_types
;
16869 last_eq_type
= sequence_type (*last_ts
);
16870 eq_type
= sequence_type (sym
->ts
);
16872 /* Since the pair of objects is not of the same type, mixed or
16873 non-default sequences can be rejected. */
16875 msg
= "Sequence %s with mixed components in EQUIVALENCE "
16876 "statement at %L with different type objects";
16878 && last_eq_type
== SEQ_MIXED
16879 && !gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
))
16880 || (eq_type
== SEQ_MIXED
16881 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
)))
16884 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
16885 "statement at %L with objects of different type";
16887 && last_eq_type
== SEQ_NONDEFAULT
16888 && !gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
))
16889 || (eq_type
== SEQ_NONDEFAULT
16890 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
)))
16893 msg
="Non-CHARACTER object %qs in default CHARACTER "
16894 "EQUIVALENCE statement at %L";
16895 if (last_eq_type
== SEQ_CHARACTER
16896 && eq_type
!= SEQ_CHARACTER
16897 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
))
16900 msg
="Non-NUMERIC object %qs in default NUMERIC "
16901 "EQUIVALENCE statement at %L";
16902 if (last_eq_type
== SEQ_NUMERIC
16903 && eq_type
!= SEQ_NUMERIC
16904 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
))
16910 last_where
= &e
->where
;
16915 /* Shall not be an automatic array. */
16916 if (e
->ref
->type
== REF_ARRAY
&& is_non_constant_shape_array (sym
))
16918 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16919 "an EQUIVALENCE object", sym
->name
, &e
->where
);
16926 /* Shall not be a structure component. */
16927 if (r
->type
== REF_COMPONENT
)
16929 gfc_error ("Structure component %qs at %L cannot be an "
16930 "EQUIVALENCE object",
16931 r
->u
.c
.component
->name
, &e
->where
);
16935 /* A substring shall not have length zero. */
16936 if (r
->type
== REF_SUBSTRING
)
16938 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
16940 gfc_error ("Substring at %L has length zero",
16941 &r
->u
.ss
.start
->where
);
16951 /* Function called by resolve_fntype to flag other symbols used in the
16952 length type parameter specification of function results. */
16955 flag_fn_result_spec (gfc_expr
*expr
,
16957 int *f ATTRIBUTE_UNUSED
)
16962 if (expr
->expr_type
== EXPR_VARIABLE
)
16964 s
= expr
->symtree
->n
.sym
;
16965 for (ns
= s
->ns
; ns
; ns
= ns
->parent
)
16971 gfc_error ("Self reference in character length expression "
16972 "for %qs at %L", sym
->name
, &expr
->where
);
16976 if (!s
->fn_result_spec
16977 && s
->attr
.flavor
== FL_PARAMETER
)
16979 /* Function contained in a module.... */
16980 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_MODULE
)
16983 s
->fn_result_spec
= 1;
16984 /* Make sure that this symbol is translated as a module
16986 st
= gfc_get_unique_symtree (ns
);
16990 /* ... which is use associated and called. */
16991 else if (s
->attr
.use_assoc
|| s
->attr
.used_in_submodule
16993 /* External function matched with an interface. */
16996 && s
->ns
->proc_name
->attr
.if_source
== IFSRC_DECL
)
16997 || s
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
16998 && s
->ns
->proc_name
->attr
.function
))
16999 s
->fn_result_spec
= 1;
17006 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17009 resolve_fntype (gfc_namespace
*ns
)
17011 gfc_entry_list
*el
;
17014 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
17017 /* If there are any entries, ns->proc_name is the entry master
17018 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17020 sym
= ns
->entries
->sym
;
17022 sym
= ns
->proc_name
;
17023 if (sym
->result
== sym
17024 && sym
->ts
.type
== BT_UNKNOWN
17025 && !gfc_set_default_type (sym
, 0, NULL
)
17026 && !sym
->attr
.untyped
)
17028 gfc_error ("Function %qs at %L has no IMPLICIT type",
17029 sym
->name
, &sym
->declared_at
);
17030 sym
->attr
.untyped
= 1;
17033 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
17034 && !sym
->attr
.contained
17035 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
17036 && gfc_check_symbol_access (sym
))
17038 gfc_notify_std (GFC_STD_F2003
, "PUBLIC function %qs at "
17039 "%L of PRIVATE type %qs", sym
->name
,
17040 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
17044 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
17046 if (el
->sym
->result
== el
->sym
17047 && el
->sym
->ts
.type
== BT_UNKNOWN
17048 && !gfc_set_default_type (el
->sym
, 0, NULL
)
17049 && !el
->sym
->attr
.untyped
)
17051 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17052 el
->sym
->name
, &el
->sym
->declared_at
);
17053 el
->sym
->attr
.untyped
= 1;
17057 if (sym
->ts
.type
== BT_CHARACTER
)
17058 gfc_traverse_expr (sym
->ts
.u
.cl
->length
, sym
, flag_fn_result_spec
, 0);
17062 /* 12.3.2.1.1 Defined operators. */
17065 check_uop_procedure (gfc_symbol
*sym
, locus where
)
17067 gfc_formal_arglist
*formal
;
17069 if (!sym
->attr
.function
)
17071 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17072 sym
->name
, &where
);
17076 if (sym
->ts
.type
== BT_CHARACTER
17077 && !((sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
) || sym
->ts
.deferred
)
17078 && !(sym
->result
&& ((sym
->result
->ts
.u
.cl
17079 && sym
->result
->ts
.u
.cl
->length
) || sym
->result
->ts
.deferred
)))
17081 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17082 "character length", sym
->name
, &where
);
17086 formal
= gfc_sym_get_dummy_args (sym
);
17087 if (!formal
|| !formal
->sym
)
17089 gfc_error ("User operator procedure %qs at %L must have at least "
17090 "one argument", sym
->name
, &where
);
17094 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
17096 gfc_error ("First argument of operator interface at %L must be "
17097 "INTENT(IN)", &where
);
17101 if (formal
->sym
->attr
.optional
)
17103 gfc_error ("First argument of operator interface at %L cannot be "
17104 "optional", &where
);
17108 formal
= formal
->next
;
17109 if (!formal
|| !formal
->sym
)
17112 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
17114 gfc_error ("Second argument of operator interface at %L must be "
17115 "INTENT(IN)", &where
);
17119 if (formal
->sym
->attr
.optional
)
17121 gfc_error ("Second argument of operator interface at %L cannot be "
17122 "optional", &where
);
17128 gfc_error ("Operator interface at %L must have, at most, two "
17129 "arguments", &where
);
17137 gfc_resolve_uops (gfc_symtree
*symtree
)
17139 gfc_interface
*itr
;
17141 if (symtree
== NULL
)
17144 gfc_resolve_uops (symtree
->left
);
17145 gfc_resolve_uops (symtree
->right
);
17147 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
17148 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
17152 /* Examine all of the expressions associated with a program unit,
17153 assign types to all intermediate expressions, make sure that all
17154 assignments are to compatible types and figure out which names
17155 refer to which functions or subroutines. It doesn't check code
17156 block, which is handled by gfc_resolve_code. */
17159 resolve_types (gfc_namespace
*ns
)
17165 gfc_namespace
* old_ns
= gfc_current_ns
;
17166 bool recursive
= ns
->proc_name
&& ns
->proc_name
->attr
.recursive
;
17168 if (ns
->types_resolved
)
17171 /* Check that all IMPLICIT types are ok. */
17172 if (!ns
->seen_implicit_none
)
17175 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
17176 if (ns
->set_flag
[letter
]
17177 && !resolve_typespec_used (&ns
->default_type
[letter
],
17178 &ns
->implicit_loc
[letter
], NULL
))
17182 gfc_current_ns
= ns
;
17184 resolve_entries (ns
);
17186 resolve_common_vars (&ns
->blank_common
, false);
17187 resolve_common_blocks (ns
->common_root
);
17189 resolve_contained_functions (ns
);
17191 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
17192 && ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
17193 gfc_resolve_formal_arglist (ns
->proc_name
);
17195 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
17197 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
17198 resolve_charlen (cl
);
17200 gfc_traverse_ns (ns
, resolve_symbol
);
17202 resolve_fntype (ns
);
17204 for (n
= ns
->contained
; n
; n
= n
->sibling
)
17206 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
17207 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17208 "also be PURE", n
->proc_name
->name
,
17209 &n
->proc_name
->declared_at
);
17215 gfc_do_concurrent_flag
= 0;
17216 gfc_check_interfaces (ns
);
17218 gfc_traverse_ns (ns
, resolve_values
);
17220 if (ns
->save_all
|| (!flag_automatic
&& !recursive
))
17224 for (d
= ns
->data
; d
; d
= d
->next
)
17228 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
17230 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
17232 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
17233 resolve_equivalence (eq
);
17235 /* Warn about unused labels. */
17236 if (warn_unused_label
)
17237 warn_unused_fortran_label (ns
->st_labels
);
17239 gfc_resolve_uops (ns
->uop_root
);
17241 gfc_traverse_ns (ns
, gfc_verify_DTIO_procedures
);
17243 gfc_resolve_omp_declare_simd (ns
);
17245 gfc_resolve_omp_udrs (ns
->omp_udr_root
);
17247 ns
->types_resolved
= 1;
17249 gfc_current_ns
= old_ns
;
17253 /* Call gfc_resolve_code recursively. */
17256 resolve_codes (gfc_namespace
*ns
)
17259 bitmap_obstack old_obstack
;
17261 if (ns
->resolved
== 1)
17264 for (n
= ns
->contained
; n
; n
= n
->sibling
)
17267 gfc_current_ns
= ns
;
17269 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17270 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
17273 /* Set to an out of range value. */
17274 current_entry_id
= -1;
17276 old_obstack
= labels_obstack
;
17277 bitmap_obstack_initialize (&labels_obstack
);
17279 gfc_resolve_oacc_declare (ns
);
17280 gfc_resolve_oacc_routines (ns
);
17281 gfc_resolve_omp_local_vars (ns
);
17282 gfc_resolve_code (ns
->code
, ns
);
17284 bitmap_obstack_release (&labels_obstack
);
17285 labels_obstack
= old_obstack
;
17289 /* This function is called after a complete program unit has been compiled.
17290 Its purpose is to examine all of the expressions associated with a program
17291 unit, assign types to all intermediate expressions, make sure that all
17292 assignments are to compatible types and figure out which names refer to
17293 which functions or subroutines. */
17296 gfc_resolve (gfc_namespace
*ns
)
17298 gfc_namespace
*old_ns
;
17299 code_stack
*old_cs_base
;
17300 struct gfc_omp_saved_state old_omp_state
;
17306 old_ns
= gfc_current_ns
;
17307 old_cs_base
= cs_base
;
17309 /* As gfc_resolve can be called during resolution of an OpenMP construct
17310 body, we should clear any state associated to it, so that say NS's
17311 DO loops are not interpreted as OpenMP loops. */
17312 if (!ns
->construct_entities
)
17313 gfc_omp_save_and_clear_state (&old_omp_state
);
17315 resolve_types (ns
);
17316 component_assignment_level
= 0;
17317 resolve_codes (ns
);
17319 gfc_current_ns
= old_ns
;
17320 cs_base
= old_cs_base
;
17323 gfc_run_passes (ns
);
17325 if (!ns
->construct_entities
)
17326 gfc_omp_restore_state (&old_omp_state
);