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
)->ts
.u
.derived
;
9247 if (code
->expr2
->rank
&& CLASS_DATA (code
->expr1
)->as
)
9248 CLASS_DATA (code
->expr1
)->as
->rank
= code
->expr2
->rank
;
9250 /* F2008: C803 The selector expression must not be coindexed. */
9251 if (gfc_is_coindexed (code
->expr2
))
9253 gfc_error ("Selector at %L must not be coindexed",
9254 &code
->expr2
->where
);
9261 selector_type
= CLASS_DATA (code
->expr1
)->ts
.u
.derived
;
9263 if (gfc_is_coindexed (code
->expr1
))
9265 gfc_error ("Selector at %L must not be coindexed",
9266 &code
->expr1
->where
);
9271 /* Loop over TYPE IS / CLASS IS cases. */
9272 for (body
= code
->block
; body
; body
= body
->block
)
9274 c
= body
->ext
.block
.case_list
;
9278 /* Check for repeated cases. */
9279 for (tail
= code
->block
; tail
; tail
= tail
->block
)
9281 gfc_case
*d
= tail
->ext
.block
.case_list
;
9285 if (c
->ts
.type
== d
->ts
.type
9286 && ((c
->ts
.type
== BT_DERIVED
9287 && c
->ts
.u
.derived
&& d
->ts
.u
.derived
9288 && !strcmp (c
->ts
.u
.derived
->name
,
9289 d
->ts
.u
.derived
->name
))
9290 || c
->ts
.type
== BT_UNKNOWN
9291 || (!(c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9292 && c
->ts
.kind
== d
->ts
.kind
)))
9294 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9295 &c
->where
, &d
->where
);
9301 /* Check F03:C815. */
9302 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9303 && !selector_type
->attr
.unlimited_polymorphic
9304 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
9306 gfc_error ("Derived type %qs at %L must be extensible",
9307 c
->ts
.u
.derived
->name
, &c
->where
);
9312 /* Check F03:C816. */
9313 if (c
->ts
.type
!= BT_UNKNOWN
&& !selector_type
->attr
.unlimited_polymorphic
9314 && ((c
->ts
.type
!= BT_DERIVED
&& c
->ts
.type
!= BT_CLASS
)
9315 || !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
)))
9317 if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9318 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9319 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
9321 gfc_error ("Unexpected intrinsic type %qs at %L",
9322 gfc_basic_typename (c
->ts
.type
), &c
->where
);
9327 /* Check F03:C814. */
9328 if (c
->ts
.type
== BT_CHARACTER
9329 && (c
->ts
.u
.cl
->length
!= NULL
|| c
->ts
.deferred
))
9331 gfc_error ("The type-spec at %L shall specify that each length "
9332 "type parameter is assumed", &c
->where
);
9337 /* Intercept the DEFAULT case. */
9338 if (c
->ts
.type
== BT_UNKNOWN
)
9340 /* Check F03:C818. */
9343 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9344 "by a second DEFAULT CASE at %L",
9345 &default_case
->ext
.block
.case_list
->where
, &c
->where
);
9350 default_case
= body
;
9357 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9358 target if present. If there are any EXIT statements referring to the
9359 SELECT TYPE construct, this is no problem because the gfc_code
9360 reference stays the same and EXIT is equally possible from the BLOCK
9361 it is changed to. */
9362 code
->op
= EXEC_BLOCK
;
9365 gfc_association_list
* assoc
;
9367 assoc
= gfc_get_association_list ();
9368 assoc
->st
= code
->expr1
->symtree
;
9369 assoc
->target
= gfc_copy_expr (code
->expr2
);
9370 assoc
->target
->where
= code
->expr2
->where
;
9371 /* assoc->variable will be set by resolve_assoc_var. */
9373 code
->ext
.block
.assoc
= assoc
;
9374 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
9376 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
9379 code
->ext
.block
.assoc
= NULL
;
9381 /* Ensure that the selector rank and arrayspec are available to
9382 correct expressions in which they might be missing. */
9383 if (code
->expr2
&& code
->expr2
->rank
)
9385 rank
= code
->expr2
->rank
;
9386 for (ref
= code
->expr2
->ref
; ref
; ref
= ref
->next
)
9387 if (ref
->next
== NULL
)
9389 if (ref
&& ref
->type
== REF_ARRAY
)
9390 ref
= gfc_copy_ref (ref
);
9392 /* Fixup expr1 if necessary. */
9394 fixup_array_ref (&code
->expr1
, code
->expr2
, rank
, ref
);
9396 else if (code
->expr1
->rank
)
9398 rank
= code
->expr1
->rank
;
9399 for (ref
= code
->expr1
->ref
; ref
; ref
= ref
->next
)
9400 if (ref
->next
== NULL
)
9402 if (ref
&& ref
->type
== REF_ARRAY
)
9403 ref
= gfc_copy_ref (ref
);
9406 /* Add EXEC_SELECT to switch on type. */
9407 new_st
= gfc_get_code (code
->op
);
9408 new_st
->expr1
= code
->expr1
;
9409 new_st
->expr2
= code
->expr2
;
9410 new_st
->block
= code
->block
;
9411 code
->expr1
= code
->expr2
= NULL
;
9416 ns
->code
->next
= new_st
;
9418 code
->op
= EXEC_SELECT_TYPE
;
9420 /* Use the intrinsic LOC function to generate an integer expression
9421 for the vtable of the selector. Note that the rank of the selector
9422 expression has to be set to zero. */
9423 gfc_add_vptr_component (code
->expr1
);
9424 code
->expr1
->rank
= 0;
9425 code
->expr1
= build_loc_call (code
->expr1
);
9426 selector_expr
= code
->expr1
->value
.function
.actual
->expr
;
9428 /* Loop over TYPE IS / CLASS IS cases. */
9429 for (body
= code
->block
; body
; body
= body
->block
)
9433 c
= body
->ext
.block
.case_list
;
9435 /* Generate an index integer expression for address of the
9436 TYPE/CLASS vtable and store it in c->low. The hash expression
9437 is stored in c->high and is used to resolve intrinsic cases. */
9438 if (c
->ts
.type
!= BT_UNKNOWN
)
9440 if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
9442 vtab
= gfc_find_derived_vtab (c
->ts
.u
.derived
);
9444 c
->high
= gfc_get_int_expr (gfc_integer_4_kind
, NULL
,
9445 c
->ts
.u
.derived
->hash_value
);
9449 vtab
= gfc_find_vtab (&c
->ts
);
9450 gcc_assert (vtab
&& CLASS_DATA (vtab
)->initializer
);
9451 e
= CLASS_DATA (vtab
)->initializer
;
9452 c
->high
= gfc_copy_expr (e
);
9453 if (c
->high
->ts
.kind
!= gfc_integer_4_kind
)
9456 ts
.kind
= gfc_integer_4_kind
;
9457 ts
.type
= BT_INTEGER
;
9458 gfc_convert_type_warn (c
->high
, &ts
, 2, 0);
9462 e
= gfc_lval_expr_from_sym (vtab
);
9463 c
->low
= build_loc_call (e
);
9468 /* Associate temporary to selector. This should only be done
9469 when this case is actually true, so build a new ASSOCIATE
9470 that does precisely this here (instead of using the
9473 if (c
->ts
.type
== BT_CLASS
)
9474 sprintf (name
, "__tmp_class_%s", c
->ts
.u
.derived
->name
);
9475 else if (c
->ts
.type
== BT_DERIVED
)
9476 sprintf (name
, "__tmp_type_%s", c
->ts
.u
.derived
->name
);
9477 else if (c
->ts
.type
== BT_CHARACTER
)
9479 HOST_WIDE_INT charlen
= 0;
9480 if (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
9481 && c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9482 charlen
= gfc_mpz_get_hwi (c
->ts
.u
.cl
->length
->value
.integer
);
9483 snprintf (name
, sizeof (name
),
9484 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
9485 gfc_basic_typename (c
->ts
.type
), charlen
, c
->ts
.kind
);
9488 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (c
->ts
.type
),
9491 st
= gfc_find_symtree (ns
->sym_root
, name
);
9492 gcc_assert (st
->n
.sym
->assoc
);
9493 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (selector_expr
->symtree
);
9494 st
->n
.sym
->assoc
->target
->where
= selector_expr
->where
;
9495 if (c
->ts
.type
!= BT_CLASS
&& c
->ts
.type
!= BT_UNKNOWN
)
9497 gfc_add_data_component (st
->n
.sym
->assoc
->target
);
9498 /* Fixup the target expression if necessary. */
9500 fixup_array_ref (&st
->n
.sym
->assoc
->target
, NULL
, rank
, ref
);
9503 new_st
= gfc_get_code (EXEC_BLOCK
);
9504 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
9505 new_st
->ext
.block
.ns
->code
= body
->next
;
9506 body
->next
= new_st
;
9508 /* Chain in the new list only if it is marked as dangling. Otherwise
9509 there is a CASE label overlap and this is already used. Just ignore,
9510 the error is diagnosed elsewhere. */
9511 if (st
->n
.sym
->assoc
->dangling
)
9513 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
9514 st
->n
.sym
->assoc
->dangling
= 0;
9517 resolve_assoc_var (st
->n
.sym
, false);
9520 /* Take out CLASS IS cases for separate treatment. */
9522 while (body
&& body
->block
)
9524 if (body
->block
->ext
.block
.case_list
->ts
.type
== BT_CLASS
)
9526 /* Add to class_is list. */
9527 if (class_is
== NULL
)
9529 class_is
= body
->block
;
9534 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
9535 tail
->block
= body
->block
;
9538 /* Remove from EXEC_SELECT list. */
9539 body
->block
= body
->block
->block
;
9552 /* Add a default case to hold the CLASS IS cases. */
9553 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
9554 tail
->block
= gfc_get_code (EXEC_SELECT_TYPE
);
9556 tail
->ext
.block
.case_list
= gfc_get_case ();
9557 tail
->ext
.block
.case_list
->ts
.type
= BT_UNKNOWN
;
9559 default_case
= tail
;
9562 /* More than one CLASS IS block? */
9563 if (class_is
->block
)
9567 /* Sort CLASS IS blocks by extension level. */
9571 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
9574 /* F03:C817 (check for doubles). */
9575 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->hash_value
9576 == c2
->ext
.block
.case_list
->ts
.u
.derived
->hash_value
)
9578 gfc_error ("Double CLASS IS block in SELECT TYPE "
9580 &c2
->ext
.block
.case_list
->where
);
9583 if ((*c1
)->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
9584 < c2
->ext
.block
.case_list
->ts
.u
.derived
->attr
.extension
)
9587 (*c1
)->block
= c2
->block
;
9597 /* Generate IF chain. */
9598 if_st
= gfc_get_code (EXEC_IF
);
9600 for (body
= class_is
; body
; body
= body
->block
)
9602 new_st
->block
= gfc_get_code (EXEC_IF
);
9603 new_st
= new_st
->block
;
9604 /* Set up IF condition: Call _gfortran_is_extension_of. */
9605 new_st
->expr1
= gfc_get_expr ();
9606 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
9607 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
9608 new_st
->expr1
->ts
.kind
= 4;
9609 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
9610 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
9611 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
9612 /* Set up arguments. */
9613 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
9614 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (selector_expr
->symtree
);
9615 new_st
->expr1
->value
.function
.actual
->expr
->where
= code
->loc
;
9616 new_st
->expr1
->where
= code
->loc
;
9617 gfc_add_vptr_component (new_st
->expr1
->value
.function
.actual
->expr
);
9618 vtab
= gfc_find_derived_vtab (body
->ext
.block
.case_list
->ts
.u
.derived
);
9619 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
9620 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
9621 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
9622 new_st
->expr1
->value
.function
.actual
->next
->expr
->where
= code
->loc
;
9623 new_st
->next
= body
->next
;
9625 if (default_case
->next
)
9627 new_st
->block
= gfc_get_code (EXEC_IF
);
9628 new_st
= new_st
->block
;
9629 new_st
->next
= default_case
->next
;
9632 /* Replace CLASS DEFAULT code by the IF chain. */
9633 default_case
->next
= if_st
;
9636 /* Resolve the internal code. This cannot be done earlier because
9637 it requires that the sym->assoc of selectors is set already. */
9638 gfc_current_ns
= ns
;
9639 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
9640 gfc_current_ns
= old_ns
;
9647 /* Resolve a SELECT RANK statement. */
9650 resolve_select_rank (gfc_code
*code
, gfc_namespace
*old_ns
)
9653 gfc_code
*body
, *new_st
, *tail
;
9655 char tname
[GFC_MAX_SYMBOL_LEN
+ 7];
9656 char name
[2 * GFC_MAX_SYMBOL_LEN
];
9658 gfc_expr
*selector_expr
= NULL
;
9660 HOST_WIDE_INT charlen
= 0;
9662 ns
= code
->ext
.block
.ns
;
9665 code
->op
= EXEC_BLOCK
;
9668 gfc_association_list
* assoc
;
9670 assoc
= gfc_get_association_list ();
9671 assoc
->st
= code
->expr1
->symtree
;
9672 assoc
->target
= gfc_copy_expr (code
->expr2
);
9673 assoc
->target
->where
= code
->expr2
->where
;
9674 /* assoc->variable will be set by resolve_assoc_var. */
9676 code
->ext
.block
.assoc
= assoc
;
9677 code
->expr1
->symtree
->n
.sym
->assoc
= assoc
;
9679 resolve_assoc_var (code
->expr1
->symtree
->n
.sym
, false);
9682 code
->ext
.block
.assoc
= NULL
;
9684 /* Loop over RANK cases. Note that returning on the errors causes a
9685 cascade of further errors because the case blocks do not compile
9687 for (body
= code
->block
; body
; body
= body
->block
)
9689 c
= body
->ext
.block
.case_list
;
9691 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
9695 /* Check for repeated cases. */
9696 for (tail
= code
->block
; tail
; tail
= tail
->block
)
9698 gfc_case
*d
= tail
->ext
.block
.case_list
;
9704 /* Check F2018: C1153. */
9705 if (!c
->low
&& !d
->low
)
9706 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9707 &c
->where
, &d
->where
);
9709 if (!c
->low
|| !d
->low
)
9712 /* Check F2018: C1153. */
9713 case_value2
= (int) mpz_get_si (d
->low
->value
.integer
);
9714 if ((case_value
== case_value2
) && case_value
== -1)
9715 gfc_error ("RANK (*) at %L is repeated at %L",
9716 &c
->where
, &d
->where
);
9717 else if (case_value
== case_value2
)
9718 gfc_error ("RANK (%i) at %L is repeated at %L",
9719 case_value
, &c
->where
, &d
->where
);
9725 /* Check F2018: C1155. */
9726 if (case_value
== -1 && (gfc_expr_attr (code
->expr1
).allocatable
9727 || gfc_expr_attr (code
->expr1
).pointer
))
9728 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9729 "allocatable selector at %L", &c
->where
, &code
->expr1
->where
);
9731 if (case_value
== -1 && (gfc_expr_attr (code
->expr1
).allocatable
9732 || gfc_expr_attr (code
->expr1
).pointer
))
9733 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9734 "allocatable selector at %L", &c
->where
, &code
->expr1
->where
);
9737 /* Add EXEC_SELECT to switch on rank. */
9738 new_st
= gfc_get_code (code
->op
);
9739 new_st
->expr1
= code
->expr1
;
9740 new_st
->expr2
= code
->expr2
;
9741 new_st
->block
= code
->block
;
9742 code
->expr1
= code
->expr2
= NULL
;
9747 ns
->code
->next
= new_st
;
9749 code
->op
= EXEC_SELECT_RANK
;
9751 selector_expr
= code
->expr1
;
9753 /* Loop over SELECT RANK cases. */
9754 for (body
= code
->block
; body
; body
= body
->block
)
9756 c
= body
->ext
.block
.case_list
;
9759 /* Pass on the default case. */
9763 /* Associate temporary to selector. This should only be done
9764 when this case is actually true, so build a new ASSOCIATE
9765 that does precisely this here (instead of using the
9767 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
9768 && c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
9769 charlen
= gfc_mpz_get_hwi (c
->ts
.u
.cl
->length
->value
.integer
);
9771 if (c
->ts
.type
== BT_CLASS
)
9772 sprintf (tname
, "class_%s", c
->ts
.u
.derived
->name
);
9773 else if (c
->ts
.type
== BT_DERIVED
)
9774 sprintf (tname
, "type_%s", c
->ts
.u
.derived
->name
);
9775 else if (c
->ts
.type
!= BT_CHARACTER
)
9776 sprintf (tname
, "%s_%d", gfc_basic_typename (c
->ts
.type
), c
->ts
.kind
);
9778 sprintf (tname
, "%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
9779 gfc_basic_typename (c
->ts
.type
), charlen
, c
->ts
.kind
);
9781 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
9782 if (case_value
>= 0)
9783 sprintf (name
, "__tmp_%s_rank_%d", tname
, case_value
);
9785 sprintf (name
, "__tmp_%s_rank_m%d", tname
, -case_value
);
9787 st
= gfc_find_symtree (ns
->sym_root
, name
);
9788 gcc_assert (st
->n
.sym
->assoc
);
9790 st
->n
.sym
->assoc
->target
= gfc_get_variable_expr (selector_expr
->symtree
);
9791 st
->n
.sym
->assoc
->target
->where
= selector_expr
->where
;
9793 new_st
= gfc_get_code (EXEC_BLOCK
);
9794 new_st
->ext
.block
.ns
= gfc_build_block_ns (ns
);
9795 new_st
->ext
.block
.ns
->code
= body
->next
;
9796 body
->next
= new_st
;
9798 /* Chain in the new list only if it is marked as dangling. Otherwise
9799 there is a CASE label overlap and this is already used. Just ignore,
9800 the error is diagnosed elsewhere. */
9801 if (st
->n
.sym
->assoc
->dangling
)
9803 new_st
->ext
.block
.assoc
= st
->n
.sym
->assoc
;
9804 st
->n
.sym
->assoc
->dangling
= 0;
9807 resolve_assoc_var (st
->n
.sym
, false);
9810 gfc_current_ns
= ns
;
9811 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
9812 gfc_current_ns
= old_ns
;
9816 /* Resolve a transfer statement. This is making sure that:
9817 -- a derived type being transferred has only non-pointer components
9818 -- a derived type being transferred doesn't have private components, unless
9819 it's being transferred from the module where the type was defined
9820 -- we're not trying to transfer a whole assumed size array. */
9823 resolve_transfer (gfc_code
*code
)
9825 gfc_symbol
*sym
, *derived
;
9829 bool formatted
= false;
9830 gfc_dt
*dt
= code
->ext
.dt
;
9831 gfc_symbol
*dtio_sub
= NULL
;
9835 while (exp
!= NULL
&& exp
->expr_type
== EXPR_OP
9836 && exp
->value
.op
.op
== INTRINSIC_PARENTHESES
)
9837 exp
= exp
->value
.op
.op1
;
9839 if (exp
&& exp
->expr_type
== EXPR_NULL
9842 gfc_error ("Invalid context for NULL () intrinsic at %L",
9847 if (exp
== NULL
|| (exp
->expr_type
!= EXPR_VARIABLE
9848 && exp
->expr_type
!= EXPR_FUNCTION
9849 && exp
->expr_type
!= EXPR_STRUCTURE
))
9852 /* If we are reading, the variable will be changed. Note that
9853 code->ext.dt may be NULL if the TRANSFER is related to
9854 an INQUIRE statement -- but in this case, we are not reading, either. */
9855 if (dt
&& dt
->dt_io_kind
->value
.iokind
== M_READ
9856 && !gfc_check_vardef_context (exp
, false, false, false,
9860 const gfc_typespec
*ts
= exp
->expr_type
== EXPR_STRUCTURE
9861 || exp
->expr_type
== EXPR_FUNCTION
9862 ? &exp
->ts
: &exp
->symtree
->n
.sym
->ts
;
9864 /* Go to actual component transferred. */
9865 for (ref
= exp
->ref
; ref
; ref
= ref
->next
)
9866 if (ref
->type
== REF_COMPONENT
)
9867 ts
= &ref
->u
.c
.component
->ts
;
9869 if (dt
&& dt
->dt_io_kind
->value
.iokind
!= M_INQUIRE
9870 && (ts
->type
== BT_DERIVED
|| ts
->type
== BT_CLASS
))
9872 derived
= ts
->u
.derived
;
9874 /* Determine when to use the formatted DTIO procedure. */
9875 if (dt
&& (dt
->format_expr
|| dt
->format_label
))
9878 write
= dt
->dt_io_kind
->value
.iokind
== M_WRITE
9879 || dt
->dt_io_kind
->value
.iokind
== M_PRINT
;
9880 dtio_sub
= gfc_find_specific_dtio_proc (derived
, write
, formatted
);
9882 if (dtio_sub
!= NULL
&& exp
->expr_type
== EXPR_VARIABLE
)
9885 sym
= exp
->symtree
->n
.sym
->ns
->proc_name
;
9886 /* Check to see if this is a nested DTIO call, with the
9887 dummy as the io-list object. */
9888 if (sym
&& sym
== dtio_sub
&& sym
->formal
9889 && sym
->formal
->sym
== exp
->symtree
->n
.sym
9890 && exp
->ref
== NULL
)
9892 if (!sym
->attr
.recursive
)
9894 gfc_error ("DTIO %s procedure at %L must be recursive",
9895 sym
->name
, &sym
->declared_at
);
9902 if (ts
->type
== BT_CLASS
&& dtio_sub
== NULL
)
9904 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9905 "it is processed by a defined input/output procedure",
9910 if (ts
->type
== BT_DERIVED
)
9912 /* Check that transferred derived type doesn't contain POINTER
9913 components unless it is processed by a defined input/output
9915 if (ts
->u
.derived
->attr
.pointer_comp
&& dtio_sub
== NULL
)
9917 gfc_error ("Data transfer element at %L cannot have POINTER "
9918 "components unless it is processed by a defined "
9919 "input/output procedure", &code
->loc
);
9924 if (ts
->u
.derived
->attr
.proc_pointer_comp
)
9926 gfc_error ("Data transfer element at %L cannot have "
9927 "procedure pointer components", &code
->loc
);
9931 if (ts
->u
.derived
->attr
.alloc_comp
&& dtio_sub
== NULL
)
9933 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9934 "components unless it is processed by a defined "
9935 "input/output procedure", &code
->loc
);
9939 /* C_PTR and C_FUNPTR have private components which means they cannot
9940 be printed. However, if -std=gnu and not -pedantic, allow
9941 the component to be printed to help debugging. */
9942 if (ts
->u
.derived
->ts
.f90_type
== BT_VOID
)
9944 if (!gfc_notify_std (GFC_STD_GNU
, "Data transfer element at %L "
9945 "cannot have PRIVATE components", &code
->loc
))
9948 else if (derived_inaccessible (ts
->u
.derived
) && dtio_sub
== NULL
)
9950 gfc_error ("Data transfer element at %L cannot have "
9951 "PRIVATE components unless it is processed by "
9952 "a defined input/output procedure", &code
->loc
);
9957 if (exp
->expr_type
== EXPR_STRUCTURE
)
9960 sym
= exp
->symtree
->n
.sym
;
9962 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
&& exp
->ref
9963 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
9965 gfc_error ("Data transfer element at %L cannot be a full reference to "
9966 "an assumed-size array", &code
->loc
);
9972 /*********** Toplevel code resolution subroutines ***********/
9974 /* Find the set of labels that are reachable from this block. We also
9975 record the last statement in each block. */
9978 find_reachable_labels (gfc_code
*block
)
9985 cs_base
->reachable_labels
= bitmap_alloc (&labels_obstack
);
9987 /* Collect labels in this block. We don't keep those corresponding
9988 to END {IF|SELECT}, these are checked in resolve_branch by going
9989 up through the code_stack. */
9990 for (c
= block
; c
; c
= c
->next
)
9992 if (c
->here
&& c
->op
!= EXEC_END_NESTED_BLOCK
)
9993 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
9996 /* Merge with labels from parent block. */
9999 gcc_assert (cs_base
->prev
->reachable_labels
);
10000 bitmap_ior_into (cs_base
->reachable_labels
,
10001 cs_base
->prev
->reachable_labels
);
10007 resolve_lock_unlock_event (gfc_code
*code
)
10009 if (code
->expr1
->expr_type
== EXPR_FUNCTION
10010 && code
->expr1
->value
.function
.isym
10011 && code
->expr1
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
10012 remove_caf_get_intrinsic (code
->expr1
);
10014 if ((code
->op
== EXEC_LOCK
|| code
->op
== EXEC_UNLOCK
)
10015 && (code
->expr1
->ts
.type
!= BT_DERIVED
10016 || code
->expr1
->expr_type
!= EXPR_VARIABLE
10017 || code
->expr1
->ts
.u
.derived
->from_intmod
!= INTMOD_ISO_FORTRAN_ENV
10018 || code
->expr1
->ts
.u
.derived
->intmod_sym_id
!= ISOFORTRAN_LOCK_TYPE
10019 || code
->expr1
->rank
!= 0
10020 || (!gfc_is_coarray (code
->expr1
) &&
10021 !gfc_is_coindexed (code
->expr1
))))
10022 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10023 &code
->expr1
->where
);
10024 else if ((code
->op
== EXEC_EVENT_POST
|| code
->op
== EXEC_EVENT_WAIT
)
10025 && (code
->expr1
->ts
.type
!= BT_DERIVED
10026 || code
->expr1
->expr_type
!= EXPR_VARIABLE
10027 || code
->expr1
->ts
.u
.derived
->from_intmod
10028 != INTMOD_ISO_FORTRAN_ENV
10029 || code
->expr1
->ts
.u
.derived
->intmod_sym_id
10030 != ISOFORTRAN_EVENT_TYPE
10031 || code
->expr1
->rank
!= 0))
10032 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10033 &code
->expr1
->where
);
10034 else if (code
->op
== EXEC_EVENT_POST
&& !gfc_is_coarray (code
->expr1
)
10035 && !gfc_is_coindexed (code
->expr1
))
10036 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10037 &code
->expr1
->where
);
10038 else if (code
->op
== EXEC_EVENT_WAIT
&& !gfc_is_coarray (code
->expr1
))
10039 gfc_error ("Event variable argument at %L must be a coarray but not "
10040 "coindexed", &code
->expr1
->where
);
10044 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
10045 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
10046 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10047 &code
->expr2
->where
);
10050 && !gfc_check_vardef_context (code
->expr2
, false, false, false,
10051 _("STAT variable")))
10054 /* Check ERRMSG. */
10056 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
10057 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
10058 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10059 &code
->expr3
->where
);
10062 && !gfc_check_vardef_context (code
->expr3
, false, false, false,
10063 _("ERRMSG variable")))
10066 /* Check for LOCK the ACQUIRED_LOCK. */
10067 if (code
->op
!= EXEC_EVENT_WAIT
&& code
->expr4
10068 && (code
->expr4
->ts
.type
!= BT_LOGICAL
|| code
->expr4
->rank
!= 0
10069 || code
->expr4
->expr_type
!= EXPR_VARIABLE
))
10070 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10071 "variable", &code
->expr4
->where
);
10073 if (code
->op
!= EXEC_EVENT_WAIT
&& code
->expr4
10074 && !gfc_check_vardef_context (code
->expr4
, false, false, false,
10075 _("ACQUIRED_LOCK variable")))
10078 /* Check for EVENT WAIT the UNTIL_COUNT. */
10079 if (code
->op
== EXEC_EVENT_WAIT
&& code
->expr4
)
10081 if (!gfc_resolve_expr (code
->expr4
) || code
->expr4
->ts
.type
!= BT_INTEGER
10082 || code
->expr4
->rank
!= 0)
10083 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10084 "expression", &code
->expr4
->where
);
10090 resolve_critical (gfc_code
*code
)
10092 gfc_symtree
*symtree
;
10093 gfc_symbol
*lock_type
;
10094 char name
[GFC_MAX_SYMBOL_LEN
];
10095 static int serial
= 0;
10097 if (flag_coarray
!= GFC_FCOARRAY_LIB
)
10100 symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
10101 GFC_PREFIX ("lock_type"));
10103 lock_type
= symtree
->n
.sym
;
10106 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns
, &symtree
,
10108 gcc_unreachable ();
10109 lock_type
= symtree
->n
.sym
;
10110 lock_type
->attr
.flavor
= FL_DERIVED
;
10111 lock_type
->attr
.zero_comp
= 1;
10112 lock_type
->from_intmod
= INTMOD_ISO_FORTRAN_ENV
;
10113 lock_type
->intmod_sym_id
= ISOFORTRAN_LOCK_TYPE
;
10116 sprintf(name
, GFC_PREFIX ("lock_var") "%d",serial
++);
10117 if (gfc_get_sym_tree (name
, gfc_current_ns
, &symtree
, false) != 0)
10118 gcc_unreachable ();
10120 code
->resolved_sym
= symtree
->n
.sym
;
10121 symtree
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
10122 symtree
->n
.sym
->attr
.referenced
= 1;
10123 symtree
->n
.sym
->attr
.artificial
= 1;
10124 symtree
->n
.sym
->attr
.codimension
= 1;
10125 symtree
->n
.sym
->ts
.type
= BT_DERIVED
;
10126 symtree
->n
.sym
->ts
.u
.derived
= lock_type
;
10127 symtree
->n
.sym
->as
= gfc_get_array_spec ();
10128 symtree
->n
.sym
->as
->corank
= 1;
10129 symtree
->n
.sym
->as
->type
= AS_EXPLICIT
;
10130 symtree
->n
.sym
->as
->cotype
= AS_EXPLICIT
;
10131 symtree
->n
.sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
,
10133 gfc_commit_symbols();
10138 resolve_sync (gfc_code
*code
)
10140 /* Check imageset. The * case matches expr1 == NULL. */
10143 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
10144 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10145 "INTEGER expression", &code
->expr1
->where
);
10146 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
10147 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
10148 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10149 &code
->expr1
->where
);
10150 else if (code
->expr1
->expr_type
== EXPR_ARRAY
10151 && gfc_simplify_expr (code
->expr1
, 0))
10153 gfc_constructor
*cons
;
10154 cons
= gfc_constructor_first (code
->expr1
->value
.constructor
);
10155 for (; cons
; cons
= gfc_constructor_next (cons
))
10156 if (cons
->expr
->expr_type
== EXPR_CONSTANT
10157 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
10158 gfc_error ("Imageset argument at %L must between 1 and "
10159 "num_images()", &cons
->expr
->where
);
10164 gfc_resolve_expr (code
->expr2
);
10166 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
10167 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
10168 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10169 &code
->expr2
->where
);
10171 /* Check ERRMSG. */
10172 gfc_resolve_expr (code
->expr3
);
10174 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
10175 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
10176 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10177 &code
->expr3
->where
);
10181 /* Given a branch to a label, see if the branch is conforming.
10182 The code node describes where the branch is located. */
10185 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
10192 /* Step one: is this a valid branching target? */
10194 if (label
->defined
== ST_LABEL_UNKNOWN
)
10196 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
10201 if (label
->defined
!= ST_LABEL_TARGET
&& label
->defined
!= ST_LABEL_DO_TARGET
)
10203 gfc_error ("Statement at %L is not a valid branch target statement "
10204 "for the branch statement at %L", &label
->where
, &code
->loc
);
10208 /* Step two: make sure this branch is not a branch to itself ;-) */
10210 if (code
->here
== label
)
10213 "Branch at %L may result in an infinite loop", &code
->loc
);
10217 /* Step three: See if the label is in the same block as the
10218 branching statement. The hard work has been done by setting up
10219 the bitmap reachable_labels. */
10221 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
10223 /* Check now whether there is a CRITICAL construct; if so, check
10224 whether the label is still visible outside of the CRITICAL block,
10225 which is invalid. */
10226 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
10228 if (stack
->current
->op
== EXEC_CRITICAL
10229 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
10230 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10231 "label at %L", &code
->loc
, &label
->where
);
10232 else if (stack
->current
->op
== EXEC_DO_CONCURRENT
10233 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
10234 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10235 "for label at %L", &code
->loc
, &label
->where
);
10241 /* Step four: If we haven't found the label in the bitmap, it may
10242 still be the label of the END of the enclosing block, in which
10243 case we find it by going up the code_stack. */
10245 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
10247 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
10249 if (stack
->current
->op
== EXEC_CRITICAL
)
10251 /* Note: A label at END CRITICAL does not leave the CRITICAL
10252 construct as END CRITICAL is still part of it. */
10253 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10254 " at %L", &code
->loc
, &label
->where
);
10257 else if (stack
->current
->op
== EXEC_DO_CONCURRENT
)
10259 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10260 "label at %L", &code
->loc
, &label
->where
);
10267 gcc_assert (stack
->current
->next
->op
== EXEC_END_NESTED_BLOCK
);
10271 /* The label is not in an enclosing block, so illegal. This was
10272 allowed in Fortran 66, so we allow it as extension. No
10273 further checks are necessary in this case. */
10274 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
10275 "as the GOTO statement at %L", &label
->where
,
10281 /* Check whether EXPR1 has the same shape as EXPR2. */
10284 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
10286 mpz_t shape
[GFC_MAX_DIMENSIONS
];
10287 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
10288 bool result
= false;
10291 /* Compare the rank. */
10292 if (expr1
->rank
!= expr2
->rank
)
10295 /* Compare the size of each dimension. */
10296 for (i
=0; i
<expr1
->rank
; i
++)
10298 if (!gfc_array_dimen_size (expr1
, i
, &shape
[i
]))
10301 if (!gfc_array_dimen_size (expr2
, i
, &shape2
[i
]))
10304 if (mpz_cmp (shape
[i
], shape2
[i
]))
10308 /* When either of the two expression is an assumed size array, we
10309 ignore the comparison of dimension sizes. */
10314 gfc_clear_shape (shape
, i
);
10315 gfc_clear_shape (shape2
, i
);
10320 /* Check whether a WHERE assignment target or a WHERE mask expression
10321 has the same shape as the outmost WHERE mask expression. */
10324 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
10328 gfc_expr
*e
= NULL
;
10330 cblock
= code
->block
;
10332 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10333 In case of nested WHERE, only the outmost one is stored. */
10334 if (mask
== NULL
) /* outmost WHERE */
10336 else /* inner WHERE */
10343 /* Check if the mask-expr has a consistent shape with the
10344 outmost WHERE mask-expr. */
10345 if (!resolve_where_shape (cblock
->expr1
, e
))
10346 gfc_error ("WHERE mask at %L has inconsistent shape",
10347 &cblock
->expr1
->where
);
10350 /* the assignment statement of a WHERE statement, or the first
10351 statement in where-body-construct of a WHERE construct */
10352 cnext
= cblock
->next
;
10357 /* WHERE assignment statement */
10360 /* Check shape consistent for WHERE assignment target. */
10361 if (e
&& !resolve_where_shape (cnext
->expr1
, e
))
10362 gfc_error ("WHERE assignment target at %L has "
10363 "inconsistent shape", &cnext
->expr1
->where
);
10367 case EXEC_ASSIGN_CALL
:
10368 resolve_call (cnext
);
10369 if (!cnext
->resolved_sym
->attr
.elemental
)
10370 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10371 &cnext
->ext
.actual
->expr
->where
);
10374 /* WHERE or WHERE construct is part of a where-body-construct */
10376 resolve_where (cnext
, e
);
10380 gfc_error ("Unsupported statement inside WHERE at %L",
10383 /* the next statement within the same where-body-construct */
10384 cnext
= cnext
->next
;
10386 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10387 cblock
= cblock
->block
;
10392 /* Resolve assignment in FORALL construct.
10393 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10394 FORALL index variables. */
10397 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
10401 for (n
= 0; n
< nvar
; n
++)
10403 gfc_symbol
*forall_index
;
10405 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
10407 /* Check whether the assignment target is one of the FORALL index
10409 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
10410 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
10411 gfc_error ("Assignment to a FORALL index variable at %L",
10412 &code
->expr1
->where
);
10415 /* If one of the FORALL index variables doesn't appear in the
10416 assignment variable, then there could be a many-to-one
10417 assignment. Emit a warning rather than an error because the
10418 mask could be resolving this problem. */
10419 if (!find_forall_index (code
->expr1
, forall_index
, 0))
10420 gfc_warning (0, "The FORALL with index %qs is not used on the "
10421 "left side of the assignment at %L and so might "
10422 "cause multiple assignment to this object",
10423 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
10429 /* Resolve WHERE statement in FORALL construct. */
10432 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
10433 gfc_expr
**var_expr
)
10438 cblock
= code
->block
;
10441 /* the assignment statement of a WHERE statement, or the first
10442 statement in where-body-construct of a WHERE construct */
10443 cnext
= cblock
->next
;
10448 /* WHERE assignment statement */
10450 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
10453 /* WHERE operator assignment statement */
10454 case EXEC_ASSIGN_CALL
:
10455 resolve_call (cnext
);
10456 if (!cnext
->resolved_sym
->attr
.elemental
)
10457 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10458 &cnext
->ext
.actual
->expr
->where
);
10461 /* WHERE or WHERE construct is part of a where-body-construct */
10463 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
10467 gfc_error ("Unsupported statement inside WHERE at %L",
10470 /* the next statement within the same where-body-construct */
10471 cnext
= cnext
->next
;
10473 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10474 cblock
= cblock
->block
;
10479 /* Traverse the FORALL body to check whether the following errors exist:
10480 1. For assignment, check if a many-to-one assignment happens.
10481 2. For WHERE statement, check the WHERE body to see if there is any
10482 many-to-one assignment. */
10485 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
10489 c
= code
->block
->next
;
10495 case EXEC_POINTER_ASSIGN
:
10496 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
10499 case EXEC_ASSIGN_CALL
:
10503 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10504 there is no need to handle it here. */
10508 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
10513 /* The next statement in the FORALL body. */
10519 /* Counts the number of iterators needed inside a forall construct, including
10520 nested forall constructs. This is used to allocate the needed memory
10521 in gfc_resolve_forall. */
10524 gfc_count_forall_iterators (gfc_code
*code
)
10526 int max_iters
, sub_iters
, current_iters
;
10527 gfc_forall_iterator
*fa
;
10529 gcc_assert(code
->op
== EXEC_FORALL
);
10533 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
10536 code
= code
->block
->next
;
10540 if (code
->op
== EXEC_FORALL
)
10542 sub_iters
= gfc_count_forall_iterators (code
);
10543 if (sub_iters
> max_iters
)
10544 max_iters
= sub_iters
;
10549 return current_iters
+ max_iters
;
10553 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10554 gfc_resolve_forall_body to resolve the FORALL body. */
10557 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
10559 static gfc_expr
**var_expr
;
10560 static int total_var
= 0;
10561 static int nvar
= 0;
10562 int i
, old_nvar
, tmp
;
10563 gfc_forall_iterator
*fa
;
10567 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "FORALL construct at %L", &code
->loc
))
10570 /* Start to resolve a FORALL construct */
10571 if (forall_save
== 0)
10573 /* Count the total number of FORALL indices in the nested FORALL
10574 construct in order to allocate the VAR_EXPR with proper size. */
10575 total_var
= gfc_count_forall_iterators (code
);
10577 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10578 var_expr
= XCNEWVEC (gfc_expr
*, total_var
);
10581 /* The information about FORALL iterator, including FORALL indices start, end
10582 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10583 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
10585 /* Fortran 20008: C738 (R753). */
10586 if (fa
->var
->ref
&& fa
->var
->ref
->type
== REF_ARRAY
)
10588 gfc_error ("FORALL index-name at %L must be a scalar variable "
10589 "of type integer", &fa
->var
->where
);
10593 /* Check if any outer FORALL index name is the same as the current
10595 for (i
= 0; i
< nvar
; i
++)
10597 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
10598 gfc_error ("An outer FORALL construct already has an index "
10599 "with this name %L", &fa
->var
->where
);
10602 /* Record the current FORALL index. */
10603 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
10607 /* No memory leak. */
10608 gcc_assert (nvar
<= total_var
);
10611 /* Resolve the FORALL body. */
10612 gfc_resolve_forall_body (code
, nvar
, var_expr
);
10614 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10615 gfc_resolve_blocks (code
->block
, ns
);
10619 /* Free only the VAR_EXPRs allocated in this frame. */
10620 for (i
= nvar
; i
< tmp
; i
++)
10621 gfc_free_expr (var_expr
[i
]);
10625 /* We are in the outermost FORALL construct. */
10626 gcc_assert (forall_save
== 0);
10628 /* VAR_EXPR is not needed any more. */
10635 /* Resolve a BLOCK construct statement. */
10638 resolve_block_construct (gfc_code
* code
)
10640 /* Resolve the BLOCK's namespace. */
10641 gfc_resolve (code
->ext
.block
.ns
);
10643 /* For an ASSOCIATE block, the associations (and their targets) are already
10644 resolved during resolve_symbol. */
10648 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10652 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
10656 for (; b
; b
= b
->block
)
10658 t
= gfc_resolve_expr (b
->expr1
);
10659 if (!gfc_resolve_expr (b
->expr2
))
10665 if (t
&& b
->expr1
!= NULL
10666 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
10667 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10673 && b
->expr1
!= NULL
10674 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
10675 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10680 resolve_branch (b
->label1
, b
);
10684 resolve_block_construct (b
);
10688 case EXEC_SELECT_TYPE
:
10689 case EXEC_SELECT_RANK
:
10692 case EXEC_DO_WHILE
:
10693 case EXEC_DO_CONCURRENT
:
10694 case EXEC_CRITICAL
:
10697 case EXEC_IOLENGTH
:
10701 case EXEC_OMP_ATOMIC
:
10702 case EXEC_OACC_ATOMIC
:
10704 gfc_omp_atomic_op aop
10705 = (gfc_omp_atomic_op
) (b
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
);
10707 /* Verify this before calling gfc_resolve_code, which might
10709 gcc_assert (b
->next
&& b
->next
->op
== EXEC_ASSIGN
);
10710 gcc_assert (((aop
!= GFC_OMP_ATOMIC_CAPTURE
)
10711 && b
->next
->next
== NULL
)
10712 || ((aop
== GFC_OMP_ATOMIC_CAPTURE
)
10713 && b
->next
->next
!= NULL
10714 && b
->next
->next
->op
== EXEC_ASSIGN
10715 && b
->next
->next
->next
== NULL
));
10719 case EXEC_OACC_PARALLEL_LOOP
:
10720 case EXEC_OACC_PARALLEL
:
10721 case EXEC_OACC_KERNELS_LOOP
:
10722 case EXEC_OACC_KERNELS
:
10723 case EXEC_OACC_SERIAL_LOOP
:
10724 case EXEC_OACC_SERIAL
:
10725 case EXEC_OACC_DATA
:
10726 case EXEC_OACC_HOST_DATA
:
10727 case EXEC_OACC_LOOP
:
10728 case EXEC_OACC_UPDATE
:
10729 case EXEC_OACC_WAIT
:
10730 case EXEC_OACC_CACHE
:
10731 case EXEC_OACC_ENTER_DATA
:
10732 case EXEC_OACC_EXIT_DATA
:
10733 case EXEC_OACC_ROUTINE
:
10734 case EXEC_OMP_CRITICAL
:
10735 case EXEC_OMP_DISTRIBUTE
:
10736 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
10737 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
10738 case EXEC_OMP_DISTRIBUTE_SIMD
:
10740 case EXEC_OMP_DO_SIMD
:
10741 case EXEC_OMP_MASTER
:
10742 case EXEC_OMP_ORDERED
:
10743 case EXEC_OMP_PARALLEL
:
10744 case EXEC_OMP_PARALLEL_DO
:
10745 case EXEC_OMP_PARALLEL_DO_SIMD
:
10746 case EXEC_OMP_PARALLEL_SECTIONS
:
10747 case EXEC_OMP_PARALLEL_WORKSHARE
:
10748 case EXEC_OMP_SECTIONS
:
10749 case EXEC_OMP_SIMD
:
10750 case EXEC_OMP_SINGLE
:
10751 case EXEC_OMP_TARGET
:
10752 case EXEC_OMP_TARGET_DATA
:
10753 case EXEC_OMP_TARGET_ENTER_DATA
:
10754 case EXEC_OMP_TARGET_EXIT_DATA
:
10755 case EXEC_OMP_TARGET_PARALLEL
:
10756 case EXEC_OMP_TARGET_PARALLEL_DO
:
10757 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
10758 case EXEC_OMP_TARGET_SIMD
:
10759 case EXEC_OMP_TARGET_TEAMS
:
10760 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
10761 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
10762 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
10763 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
10764 case EXEC_OMP_TARGET_UPDATE
:
10765 case EXEC_OMP_TASK
:
10766 case EXEC_OMP_TASKGROUP
:
10767 case EXEC_OMP_TASKLOOP
:
10768 case EXEC_OMP_TASKLOOP_SIMD
:
10769 case EXEC_OMP_TASKWAIT
:
10770 case EXEC_OMP_TASKYIELD
:
10771 case EXEC_OMP_TEAMS
:
10772 case EXEC_OMP_TEAMS_DISTRIBUTE
:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
10775 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
10776 case EXEC_OMP_WORKSHARE
:
10780 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10783 gfc_resolve_code (b
->next
, ns
);
10788 /* Does everything to resolve an ordinary assignment. Returns true
10789 if this is an interface assignment. */
10791 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
10798 symbol_attribute attr
;
10800 if (gfc_extend_assign (code
, ns
))
10804 if (code
->op
== EXEC_ASSIGN_CALL
)
10806 lhs
= code
->ext
.actual
->expr
;
10807 rhsptr
= &code
->ext
.actual
->next
->expr
;
10811 gfc_actual_arglist
* args
;
10812 gfc_typebound_proc
* tbp
;
10814 gcc_assert (code
->op
== EXEC_COMPCALL
);
10816 args
= code
->expr1
->value
.compcall
.actual
;
10818 rhsptr
= &args
->next
->expr
;
10820 tbp
= code
->expr1
->value
.compcall
.tbp
;
10821 gcc_assert (!tbp
->is_generic
);
10824 /* Make a temporary rhs when there is a default initializer
10825 and rhs is the same symbol as the lhs. */
10826 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
10827 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
10828 && gfc_has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
10829 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
10830 *rhsptr
= gfc_get_parentheses (*rhsptr
);
10838 if ((gfc_numeric_ts (&lhs
->ts
) || lhs
->ts
.type
== BT_LOGICAL
)
10839 && rhs
->ts
.type
== BT_CHARACTER
10840 && (rhs
->expr_type
!= EXPR_CONSTANT
|| !flag_dec_char_conversions
))
10842 /* Use of -fdec-char-conversions allows assignment of character data
10843 to non-character variables. This not permited for nonconstant
10845 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs
),
10846 gfc_typename (lhs
), &rhs
->where
);
10850 /* Handle the case of a BOZ literal on the RHS. */
10851 if (rhs
->ts
.type
== BT_BOZ
)
10853 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10854 "statement value nor an actual argument of "
10855 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10859 switch (lhs
->ts
.type
)
10862 if (!gfc_boz2int (rhs
, lhs
->ts
.kind
))
10866 if (!gfc_boz2real (rhs
, lhs
->ts
.kind
))
10870 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs
->where
);
10875 if (lhs
->ts
.type
== BT_CHARACTER
&& warn_character_truncation
)
10877 HOST_WIDE_INT llen
= 0, rlen
= 0;
10878 if (lhs
->ts
.u
.cl
!= NULL
10879 && lhs
->ts
.u
.cl
->length
!= NULL
10880 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
10881 llen
= gfc_mpz_get_hwi (lhs
->ts
.u
.cl
->length
->value
.integer
);
10883 if (rhs
->expr_type
== EXPR_CONSTANT
)
10884 rlen
= rhs
->value
.character
.length
;
10886 else if (rhs
->ts
.u
.cl
!= NULL
10887 && rhs
->ts
.u
.cl
->length
!= NULL
10888 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
10889 rlen
= gfc_mpz_get_hwi (rhs
->ts
.u
.cl
->length
->value
.integer
);
10891 if (rlen
&& llen
&& rlen
> llen
)
10892 gfc_warning_now (OPT_Wcharacter_truncation
,
10893 "CHARACTER expression will be truncated "
10894 "in assignment (%ld/%ld) at %L",
10895 (long) llen
, (long) rlen
, &code
->loc
);
10898 /* Ensure that a vector index expression for the lvalue is evaluated
10899 to a temporary if the lvalue symbol is referenced in it. */
10902 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
10903 if (ref
->type
== REF_ARRAY
)
10905 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
10906 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
10907 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
10908 ref
->u
.ar
.start
[n
]))
10910 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
10914 if (gfc_pure (NULL
))
10916 if (lhs
->ts
.type
== BT_DERIVED
10917 && lhs
->expr_type
== EXPR_VARIABLE
10918 && lhs
->ts
.u
.derived
->attr
.pointer_comp
10919 && rhs
->expr_type
== EXPR_VARIABLE
10920 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
10921 || gfc_is_coindexed (rhs
)))
10923 /* F2008, C1283. */
10924 if (gfc_is_coindexed (rhs
))
10925 gfc_error ("Coindexed expression at %L is assigned to "
10926 "a derived type variable with a POINTER "
10927 "component in a PURE procedure",
10930 /* F2008, C1283 (4). */
10931 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10932 "shall not be used as the expr at %L of an intrinsic "
10933 "assignment statement in which the variable is of a "
10934 "derived type if the derived type has a pointer "
10935 "component at any level of component selection.",
10940 /* Fortran 2008, C1283. */
10941 if (gfc_is_coindexed (lhs
))
10943 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10944 "procedure", &rhs
->where
);
10949 if (gfc_implicit_pure (NULL
))
10951 if (lhs
->expr_type
== EXPR_VARIABLE
10952 && lhs
->symtree
->n
.sym
!= gfc_current_ns
->proc_name
10953 && lhs
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
10954 gfc_unset_implicit_pure (NULL
);
10956 if (lhs
->ts
.type
== BT_DERIVED
10957 && lhs
->expr_type
== EXPR_VARIABLE
10958 && lhs
->ts
.u
.derived
->attr
.pointer_comp
10959 && rhs
->expr_type
== EXPR_VARIABLE
10960 && (gfc_impure_variable (rhs
->symtree
->n
.sym
)
10961 || gfc_is_coindexed (rhs
)))
10962 gfc_unset_implicit_pure (NULL
);
10964 /* Fortran 2008, C1283. */
10965 if (gfc_is_coindexed (lhs
))
10966 gfc_unset_implicit_pure (NULL
);
10969 /* F2008, 7.2.1.2. */
10970 attr
= gfc_expr_attr (lhs
);
10971 if (lhs
->ts
.type
== BT_CLASS
&& attr
.allocatable
)
10973 if (attr
.codimension
)
10975 gfc_error ("Assignment to polymorphic coarray at %L is not "
10976 "permitted", &lhs
->where
);
10979 if (!gfc_notify_std (GFC_STD_F2008
, "Assignment to an allocatable "
10980 "polymorphic variable at %L", &lhs
->where
))
10982 if (!flag_realloc_lhs
)
10984 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10985 "requires %<-frealloc-lhs%>", &lhs
->where
);
10989 else if (lhs
->ts
.type
== BT_CLASS
)
10991 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10992 "assignment at %L - check that there is a matching specific "
10993 "subroutine for '=' operator", &lhs
->where
);
10997 bool lhs_coindexed
= gfc_is_coindexed (lhs
);
10999 /* F2008, Section 7.2.1.2. */
11000 if (lhs_coindexed
&& gfc_has_ultimate_allocatable (lhs
))
11002 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11003 "component in assignment at %L", &lhs
->where
);
11007 /* Assign the 'data' of a class object to a derived type. */
11008 if (lhs
->ts
.type
== BT_DERIVED
11009 && rhs
->ts
.type
== BT_CLASS
11010 && rhs
->expr_type
!= EXPR_ARRAY
)
11011 gfc_add_data_component (rhs
);
11013 /* Make sure there is a vtable and, in particular, a _copy for the
11015 if (UNLIMITED_POLY (lhs
) && lhs
->rank
&& rhs
->ts
.type
!= BT_CLASS
)
11016 gfc_find_vtab (&rhs
->ts
);
11018 bool caf_convert_to_send
= flag_coarray
== GFC_FCOARRAY_LIB
11020 || (code
->expr2
->expr_type
== EXPR_FUNCTION
11021 && code
->expr2
->value
.function
.isym
11022 && code
->expr2
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
11023 && (code
->expr1
->rank
== 0 || code
->expr2
->rank
!= 0)
11024 && !gfc_expr_attr (rhs
).allocatable
11025 && !gfc_has_vector_subscript (rhs
)));
11027 gfc_check_assign (lhs
, rhs
, 1, !caf_convert_to_send
);
11029 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11030 Additionally, insert this code when the RHS is a CAF as we then use the
11031 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11032 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11033 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11035 if (caf_convert_to_send
)
11037 if (code
->expr2
->expr_type
== EXPR_FUNCTION
11038 && code
->expr2
->value
.function
.isym
11039 && code
->expr2
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
11040 remove_caf_get_intrinsic (code
->expr2
);
11041 code
->op
= EXEC_CALL
;
11042 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns
, &code
->symtree
, true);
11043 code
->resolved_sym
= code
->symtree
->n
.sym
;
11044 code
->resolved_sym
->attr
.flavor
= FL_PROCEDURE
;
11045 code
->resolved_sym
->attr
.intrinsic
= 1;
11046 code
->resolved_sym
->attr
.subroutine
= 1;
11047 code
->resolved_isym
= gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND
);
11048 gfc_commit_symbol (code
->resolved_sym
);
11049 code
->ext
.actual
= gfc_get_actual_arglist ();
11050 code
->ext
.actual
->expr
= lhs
;
11051 code
->ext
.actual
->next
= gfc_get_actual_arglist ();
11052 code
->ext
.actual
->next
->expr
= rhs
;
11053 code
->expr1
= NULL
;
11054 code
->expr2
= NULL
;
11061 /* Add a component reference onto an expression. */
11064 add_comp_ref (gfc_expr
*e
, gfc_component
*c
)
11069 ref
= &((*ref
)->next
);
11070 *ref
= gfc_get_ref ();
11071 (*ref
)->type
= REF_COMPONENT
;
11072 (*ref
)->u
.c
.sym
= e
->ts
.u
.derived
;
11073 (*ref
)->u
.c
.component
= c
;
11076 /* Add a full array ref, as necessary. */
11079 gfc_add_full_array_ref (e
, c
->as
);
11080 e
->rank
= c
->as
->rank
;
11085 /* Build an assignment. Keep the argument 'op' for future use, so that
11086 pointer assignments can be made. */
11089 build_assignment (gfc_exec_op op
, gfc_expr
*expr1
, gfc_expr
*expr2
,
11090 gfc_component
*comp1
, gfc_component
*comp2
, locus loc
)
11092 gfc_code
*this_code
;
11094 this_code
= gfc_get_code (op
);
11095 this_code
->next
= NULL
;
11096 this_code
->expr1
= gfc_copy_expr (expr1
);
11097 this_code
->expr2
= gfc_copy_expr (expr2
);
11098 this_code
->loc
= loc
;
11099 if (comp1
&& comp2
)
11101 add_comp_ref (this_code
->expr1
, comp1
);
11102 add_comp_ref (this_code
->expr2
, comp2
);
11109 /* Makes a temporary variable expression based on the characteristics of
11110 a given variable expression. */
11113 get_temp_from_expr (gfc_expr
*e
, gfc_namespace
*ns
)
11115 static int serial
= 0;
11116 char name
[GFC_MAX_SYMBOL_LEN
];
11118 gfc_array_spec
*as
;
11119 gfc_array_ref
*aref
;
11122 sprintf (name
, GFC_PREFIX("DA%d"), serial
++);
11123 gfc_get_sym_tree (name
, ns
, &tmp
, false);
11124 gfc_add_type (tmp
->n
.sym
, &e
->ts
, NULL
);
11126 if (e
->expr_type
== EXPR_CONSTANT
&& e
->ts
.type
== BT_CHARACTER
)
11127 tmp
->n
.sym
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_charlen_int_kind
,
11129 e
->value
.character
.length
);
11135 /* Obtain the arrayspec for the temporary. */
11136 if (e
->rank
&& e
->expr_type
!= EXPR_ARRAY
11137 && e
->expr_type
!= EXPR_FUNCTION
11138 && e
->expr_type
!= EXPR_OP
)
11140 aref
= gfc_find_array_ref (e
);
11141 if (e
->expr_type
== EXPR_VARIABLE
11142 && e
->symtree
->n
.sym
->as
== aref
->as
)
11146 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
11147 if (ref
->type
== REF_COMPONENT
11148 && ref
->u
.c
.component
->as
== aref
->as
)
11156 /* Add the attributes and the arrayspec to the temporary. */
11157 tmp
->n
.sym
->attr
= gfc_expr_attr (e
);
11158 tmp
->n
.sym
->attr
.function
= 0;
11159 tmp
->n
.sym
->attr
.result
= 0;
11160 tmp
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
11161 tmp
->n
.sym
->attr
.dummy
= 0;
11162 tmp
->n
.sym
->attr
.intent
= INTENT_UNKNOWN
;
11166 tmp
->n
.sym
->as
= gfc_copy_array_spec (as
);
11169 if (as
->type
== AS_DEFERRED
)
11170 tmp
->n
.sym
->attr
.allocatable
= 1;
11172 else if (e
->rank
&& (e
->expr_type
== EXPR_ARRAY
11173 || e
->expr_type
== EXPR_FUNCTION
11174 || e
->expr_type
== EXPR_OP
))
11176 tmp
->n
.sym
->as
= gfc_get_array_spec ();
11177 tmp
->n
.sym
->as
->type
= AS_DEFERRED
;
11178 tmp
->n
.sym
->as
->rank
= e
->rank
;
11179 tmp
->n
.sym
->attr
.allocatable
= 1;
11180 tmp
->n
.sym
->attr
.dimension
= 1;
11183 tmp
->n
.sym
->attr
.dimension
= 0;
11185 gfc_set_sym_referenced (tmp
->n
.sym
);
11186 gfc_commit_symbol (tmp
->n
.sym
);
11187 e
= gfc_lval_expr_from_sym (tmp
->n
.sym
);
11189 /* Should the lhs be a section, use its array ref for the
11190 temporary expression. */
11191 if (aref
&& aref
->type
!= AR_FULL
)
11193 gfc_free_ref_list (e
->ref
);
11194 e
->ref
= gfc_copy_ref (ref
);
11200 /* Add one line of code to the code chain, making sure that 'head' and
11201 'tail' are appropriately updated. */
11204 add_code_to_chain (gfc_code
**this_code
, gfc_code
**head
, gfc_code
**tail
)
11206 gcc_assert (this_code
);
11208 *head
= *tail
= *this_code
;
11210 *tail
= gfc_append_code (*tail
, *this_code
);
11215 /* Counts the potential number of part array references that would
11216 result from resolution of typebound defined assignments. */
11219 nonscalar_typebound_assign (gfc_symbol
*derived
, int depth
)
11222 int c_depth
= 0, t_depth
;
11224 for (c
= derived
->components
; c
; c
= c
->next
)
11226 if ((!gfc_bt_struct (c
->ts
.type
)
11228 || c
->attr
.allocatable
11229 || c
->attr
.proc_pointer_comp
11230 || c
->attr
.class_pointer
11231 || c
->attr
.proc_pointer
)
11232 && !c
->attr
.defined_assign_comp
)
11235 if (c
->as
&& c_depth
== 0)
11238 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
)
11239 t_depth
= nonscalar_typebound_assign (c
->ts
.u
.derived
,
11244 c_depth
= t_depth
> c_depth
? t_depth
: c_depth
;
11246 return depth
+ c_depth
;
11250 /* Implement 7.2.1.3 of the F08 standard:
11251 "An intrinsic assignment where the variable is of derived type is
11252 performed as if each component of the variable were assigned from the
11253 corresponding component of expr using pointer assignment (7.2.2) for
11254 each pointer component, defined assignment for each nonpointer
11255 nonallocatable component of a type that has a type-bound defined
11256 assignment consistent with the component, intrinsic assignment for
11257 each other nonpointer nonallocatable component, ..."
11259 The pointer assignments are taken care of by the intrinsic
11260 assignment of the structure itself. This function recursively adds
11261 defined assignments where required. The recursion is accomplished
11262 by calling gfc_resolve_code.
11264 When the lhs in a defined assignment has intent INOUT, we need a
11265 temporary for the lhs. In pseudo-code:
11267 ! Only call function lhs once.
11268 if (lhs is not a constant or an variable)
11271 ! Do the intrinsic assignment
11273 ! Now do the defined assignments
11274 do over components with typebound defined assignment [%cmp]
11275 #if one component's assignment procedure is INOUT
11277 #if expr2 non-variable
11283 t1%cmp {defined=} expr2%cmp
11289 expr1%cmp {defined=} expr2%cmp
11293 /* The temporary assignments have to be put on top of the additional
11294 code to avoid the result being changed by the intrinsic assignment.
11296 static int component_assignment_level
= 0;
11297 static gfc_code
*tmp_head
= NULL
, *tmp_tail
= NULL
;
11300 generate_component_assignments (gfc_code
**code
, gfc_namespace
*ns
)
11302 gfc_component
*comp1
, *comp2
;
11303 gfc_code
*this_code
= NULL
, *head
= NULL
, *tail
= NULL
;
11305 int error_count
, depth
;
11307 gfc_get_errors (NULL
, &error_count
);
11309 /* Filter out continuing processing after an error. */
11311 || (*code
)->expr1
->ts
.type
!= BT_DERIVED
11312 || (*code
)->expr2
->ts
.type
!= BT_DERIVED
)
11315 /* TODO: Handle more than one part array reference in assignments. */
11316 depth
= nonscalar_typebound_assign ((*code
)->expr1
->ts
.u
.derived
,
11317 (*code
)->expr1
->rank
? 1 : 0);
11320 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11321 "done because multiple part array references would "
11322 "occur in intermediate expressions.", &(*code
)->loc
);
11326 component_assignment_level
++;
11328 /* Create a temporary so that functions get called only once. */
11329 if ((*code
)->expr2
->expr_type
!= EXPR_VARIABLE
11330 && (*code
)->expr2
->expr_type
!= EXPR_CONSTANT
)
11332 gfc_expr
*tmp_expr
;
11334 /* Assign the rhs to the temporary. */
11335 tmp_expr
= get_temp_from_expr ((*code
)->expr1
, ns
);
11336 this_code
= build_assignment (EXEC_ASSIGN
,
11337 tmp_expr
, (*code
)->expr2
,
11338 NULL
, NULL
, (*code
)->loc
);
11339 /* Add the code and substitute the rhs expression. */
11340 add_code_to_chain (&this_code
, &tmp_head
, &tmp_tail
);
11341 gfc_free_expr ((*code
)->expr2
);
11342 (*code
)->expr2
= tmp_expr
;
11345 /* Do the intrinsic assignment. This is not needed if the lhs is one
11346 of the temporaries generated here, since the intrinsic assignment
11347 to the final result already does this. */
11348 if ((*code
)->expr1
->symtree
->n
.sym
->name
[2] != '@')
11350 this_code
= build_assignment (EXEC_ASSIGN
,
11351 (*code
)->expr1
, (*code
)->expr2
,
11352 NULL
, NULL
, (*code
)->loc
);
11353 add_code_to_chain (&this_code
, &head
, &tail
);
11356 comp1
= (*code
)->expr1
->ts
.u
.derived
->components
;
11357 comp2
= (*code
)->expr2
->ts
.u
.derived
->components
;
11360 for (; comp1
; comp1
= comp1
->next
, comp2
= comp2
->next
)
11362 bool inout
= false;
11364 /* The intrinsic assignment does the right thing for pointers
11365 of all kinds and allocatable components. */
11366 if (!gfc_bt_struct (comp1
->ts
.type
)
11367 || comp1
->attr
.pointer
11368 || comp1
->attr
.allocatable
11369 || comp1
->attr
.proc_pointer_comp
11370 || comp1
->attr
.class_pointer
11371 || comp1
->attr
.proc_pointer
)
11374 /* Make an assigment for this component. */
11375 this_code
= build_assignment (EXEC_ASSIGN
,
11376 (*code
)->expr1
, (*code
)->expr2
,
11377 comp1
, comp2
, (*code
)->loc
);
11379 /* Convert the assignment if there is a defined assignment for
11380 this type. Otherwise, using the call from gfc_resolve_code,
11381 recurse into its components. */
11382 gfc_resolve_code (this_code
, ns
);
11384 if (this_code
->op
== EXEC_ASSIGN_CALL
)
11386 gfc_formal_arglist
*dummy_args
;
11388 /* Check that there is a typebound defined assignment. If not,
11389 then this must be a module defined assignment. We cannot
11390 use the defined_assign_comp attribute here because it must
11391 be this derived type that has the defined assignment and not
11393 if (!(comp1
->ts
.u
.derived
->f2k_derived
11394 && comp1
->ts
.u
.derived
->f2k_derived
11395 ->tb_op
[INTRINSIC_ASSIGN
]))
11397 gfc_free_statements (this_code
);
11402 /* If the first argument of the subroutine has intent INOUT
11403 a temporary must be generated and used instead. */
11404 rsym
= this_code
->resolved_sym
;
11405 dummy_args
= gfc_sym_get_dummy_args (rsym
);
11407 && dummy_args
->sym
->attr
.intent
== INTENT_INOUT
)
11409 gfc_code
*temp_code
;
11412 /* Build the temporary required for the assignment and put
11413 it at the head of the generated code. */
11416 t1
= get_temp_from_expr ((*code
)->expr1
, ns
);
11417 temp_code
= build_assignment (EXEC_ASSIGN
,
11418 t1
, (*code
)->expr1
,
11419 NULL
, NULL
, (*code
)->loc
);
11421 /* For allocatable LHS, check whether it is allocated. Note
11422 that allocatable components with defined assignment are
11423 not yet support. See PR 57696. */
11424 if ((*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
)
11428 gfc_lval_expr_from_sym ((*code
)->expr1
->symtree
->n
.sym
);
11429 block
= gfc_get_code (EXEC_IF
);
11430 block
->block
= gfc_get_code (EXEC_IF
);
11431 block
->block
->expr1
11432 = gfc_build_intrinsic_call (ns
,
11433 GFC_ISYM_ALLOCATED
, "allocated",
11434 (*code
)->loc
, 1, e
);
11435 block
->block
->next
= temp_code
;
11438 add_code_to_chain (&temp_code
, &tmp_head
, &tmp_tail
);
11441 /* Replace the first actual arg with the component of the
11443 gfc_free_expr (this_code
->ext
.actual
->expr
);
11444 this_code
->ext
.actual
->expr
= gfc_copy_expr (t1
);
11445 add_comp_ref (this_code
->ext
.actual
->expr
, comp1
);
11447 /* If the LHS variable is allocatable and wasn't allocated and
11448 the temporary is allocatable, pointer assign the address of
11449 the freshly allocated LHS to the temporary. */
11450 if ((*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
11451 && gfc_expr_attr ((*code
)->expr1
).allocatable
)
11456 cond
= gfc_get_expr ();
11457 cond
->ts
.type
= BT_LOGICAL
;
11458 cond
->ts
.kind
= gfc_default_logical_kind
;
11459 cond
->expr_type
= EXPR_OP
;
11460 cond
->where
= (*code
)->loc
;
11461 cond
->value
.op
.op
= INTRINSIC_NOT
;
11462 cond
->value
.op
.op1
= gfc_build_intrinsic_call (ns
,
11463 GFC_ISYM_ALLOCATED
, "allocated",
11464 (*code
)->loc
, 1, gfc_copy_expr (t1
));
11465 block
= gfc_get_code (EXEC_IF
);
11466 block
->block
= gfc_get_code (EXEC_IF
);
11467 block
->block
->expr1
= cond
;
11468 block
->block
->next
= build_assignment (EXEC_POINTER_ASSIGN
,
11469 t1
, (*code
)->expr1
,
11470 NULL
, NULL
, (*code
)->loc
);
11471 add_code_to_chain (&block
, &head
, &tail
);
11475 else if (this_code
->op
== EXEC_ASSIGN
&& !this_code
->next
)
11477 /* Don't add intrinsic assignments since they are already
11478 effected by the intrinsic assignment of the structure. */
11479 gfc_free_statements (this_code
);
11484 add_code_to_chain (&this_code
, &head
, &tail
);
11488 /* Transfer the value to the final result. */
11489 this_code
= build_assignment (EXEC_ASSIGN
,
11490 (*code
)->expr1
, t1
,
11491 comp1
, comp2
, (*code
)->loc
);
11492 add_code_to_chain (&this_code
, &head
, &tail
);
11496 /* Put the temporary assignments at the top of the generated code. */
11497 if (tmp_head
&& component_assignment_level
== 1)
11499 gfc_append_code (tmp_head
, head
);
11501 tmp_head
= tmp_tail
= NULL
;
11504 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11505 // not accidentally deallocated. Hence, nullify t1.
11506 if (t1
&& (*code
)->expr1
->symtree
->n
.sym
->attr
.allocatable
11507 && gfc_expr_attr ((*code
)->expr1
).allocatable
)
11513 e
= gfc_lval_expr_from_sym ((*code
)->expr1
->symtree
->n
.sym
);
11514 cond
= gfc_build_intrinsic_call (ns
, GFC_ISYM_ASSOCIATED
, "associated",
11515 (*code
)->loc
, 2, gfc_copy_expr (t1
), e
);
11516 block
= gfc_get_code (EXEC_IF
);
11517 block
->block
= gfc_get_code (EXEC_IF
);
11518 block
->block
->expr1
= cond
;
11519 block
->block
->next
= build_assignment (EXEC_POINTER_ASSIGN
,
11520 t1
, gfc_get_null_expr (&(*code
)->loc
),
11521 NULL
, NULL
, (*code
)->loc
);
11522 gfc_append_code (tail
, block
);
11526 /* Now attach the remaining code chain to the input code. Step on
11527 to the end of the new code since resolution is complete. */
11528 gcc_assert ((*code
)->op
== EXEC_ASSIGN
);
11529 tail
->next
= (*code
)->next
;
11530 /* Overwrite 'code' because this would place the intrinsic assignment
11531 before the temporary for the lhs is created. */
11532 gfc_free_expr ((*code
)->expr1
);
11533 gfc_free_expr ((*code
)->expr2
);
11539 component_assignment_level
--;
11543 /* F2008: Pointer function assignments are of the form:
11544 ptr_fcn (args) = expr
11545 This function breaks these assignments into two statements:
11546 temporary_pointer => ptr_fcn(args)
11547 temporary_pointer = expr */
11550 resolve_ptr_fcn_assign (gfc_code
**code
, gfc_namespace
*ns
)
11552 gfc_expr
*tmp_ptr_expr
;
11553 gfc_code
*this_code
;
11554 gfc_component
*comp
;
11557 if ((*code
)->expr1
->expr_type
!= EXPR_FUNCTION
)
11560 /* Even if standard does not support this feature, continue to build
11561 the two statements to avoid upsetting frontend_passes.c. */
11562 gfc_notify_std (GFC_STD_F2008
, "Pointer procedure assignment at "
11563 "%L", &(*code
)->loc
);
11565 comp
= gfc_get_proc_ptr_comp ((*code
)->expr1
);
11568 s
= comp
->ts
.interface
;
11570 s
= (*code
)->expr1
->symtree
->n
.sym
;
11572 if (s
== NULL
|| !s
->result
->attr
.pointer
)
11574 gfc_error ("The function result on the lhs of the assignment at "
11575 "%L must have the pointer attribute.",
11576 &(*code
)->expr1
->where
);
11577 (*code
)->op
= EXEC_NOP
;
11581 tmp_ptr_expr
= get_temp_from_expr ((*code
)->expr2
, ns
);
11583 /* get_temp_from_expression is set up for ordinary assignments. To that
11584 end, where array bounds are not known, arrays are made allocatable.
11585 Change the temporary to a pointer here. */
11586 tmp_ptr_expr
->symtree
->n
.sym
->attr
.pointer
= 1;
11587 tmp_ptr_expr
->symtree
->n
.sym
->attr
.allocatable
= 0;
11588 tmp_ptr_expr
->where
= (*code
)->loc
;
11590 this_code
= build_assignment (EXEC_ASSIGN
,
11591 tmp_ptr_expr
, (*code
)->expr2
,
11592 NULL
, NULL
, (*code
)->loc
);
11593 this_code
->next
= (*code
)->next
;
11594 (*code
)->next
= this_code
;
11595 (*code
)->op
= EXEC_POINTER_ASSIGN
;
11596 (*code
)->expr2
= (*code
)->expr1
;
11597 (*code
)->expr1
= tmp_ptr_expr
;
11603 /* Deferred character length assignments from an operator expression
11604 require a temporary because the character length of the lhs can
11605 change in the course of the assignment. */
11608 deferred_op_assign (gfc_code
**code
, gfc_namespace
*ns
)
11610 gfc_expr
*tmp_expr
;
11611 gfc_code
*this_code
;
11613 if (!((*code
)->expr1
->ts
.type
== BT_CHARACTER
11614 && (*code
)->expr1
->ts
.deferred
&& (*code
)->expr1
->rank
11615 && (*code
)->expr2
->expr_type
== EXPR_OP
))
11618 if (!gfc_check_dependency ((*code
)->expr1
, (*code
)->expr2
, 1))
11621 if (gfc_expr_attr ((*code
)->expr1
).pointer
)
11624 tmp_expr
= get_temp_from_expr ((*code
)->expr1
, ns
);
11625 tmp_expr
->where
= (*code
)->loc
;
11627 /* A new charlen is required to ensure that the variable string
11628 length is different to that of the original lhs. */
11629 tmp_expr
->ts
.u
.cl
= gfc_get_charlen();
11630 tmp_expr
->symtree
->n
.sym
->ts
.u
.cl
= tmp_expr
->ts
.u
.cl
;
11631 tmp_expr
->ts
.u
.cl
->next
= (*code
)->expr2
->ts
.u
.cl
->next
;
11632 (*code
)->expr2
->ts
.u
.cl
->next
= tmp_expr
->ts
.u
.cl
;
11634 tmp_expr
->symtree
->n
.sym
->ts
.deferred
= 1;
11636 this_code
= build_assignment (EXEC_ASSIGN
,
11638 gfc_copy_expr (tmp_expr
),
11639 NULL
, NULL
, (*code
)->loc
);
11641 (*code
)->expr1
= tmp_expr
;
11643 this_code
->next
= (*code
)->next
;
11644 (*code
)->next
= this_code
;
11650 /* Given a block of code, recursively resolve everything pointed to by this
11654 gfc_resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
11656 int omp_workshare_save
;
11657 int forall_save
, do_concurrent_save
;
11661 frame
.prev
= cs_base
;
11665 find_reachable_labels (code
);
11667 for (; code
; code
= code
->next
)
11669 frame
.current
= code
;
11670 forall_save
= forall_flag
;
11671 do_concurrent_save
= gfc_do_concurrent_flag
;
11673 if (code
->op
== EXEC_FORALL
)
11676 gfc_resolve_forall (code
, ns
, forall_save
);
11679 else if (code
->block
)
11681 omp_workshare_save
= -1;
11684 case EXEC_OACC_PARALLEL_LOOP
:
11685 case EXEC_OACC_PARALLEL
:
11686 case EXEC_OACC_KERNELS_LOOP
:
11687 case EXEC_OACC_KERNELS
:
11688 case EXEC_OACC_SERIAL_LOOP
:
11689 case EXEC_OACC_SERIAL
:
11690 case EXEC_OACC_DATA
:
11691 case EXEC_OACC_HOST_DATA
:
11692 case EXEC_OACC_LOOP
:
11693 gfc_resolve_oacc_blocks (code
, ns
);
11695 case EXEC_OMP_PARALLEL_WORKSHARE
:
11696 omp_workshare_save
= omp_workshare_flag
;
11697 omp_workshare_flag
= 1;
11698 gfc_resolve_omp_parallel_blocks (code
, ns
);
11700 case EXEC_OMP_PARALLEL
:
11701 case EXEC_OMP_PARALLEL_DO
:
11702 case EXEC_OMP_PARALLEL_DO_SIMD
:
11703 case EXEC_OMP_PARALLEL_SECTIONS
:
11704 case EXEC_OMP_TARGET_PARALLEL
:
11705 case EXEC_OMP_TARGET_PARALLEL_DO
:
11706 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
11707 case EXEC_OMP_TARGET_TEAMS
:
11708 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
11709 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
11710 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
11711 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
11712 case EXEC_OMP_TASK
:
11713 case EXEC_OMP_TASKLOOP
:
11714 case EXEC_OMP_TASKLOOP_SIMD
:
11715 case EXEC_OMP_TEAMS
:
11716 case EXEC_OMP_TEAMS_DISTRIBUTE
:
11717 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
11718 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
11719 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
11720 omp_workshare_save
= omp_workshare_flag
;
11721 omp_workshare_flag
= 0;
11722 gfc_resolve_omp_parallel_blocks (code
, ns
);
11724 case EXEC_OMP_DISTRIBUTE
:
11725 case EXEC_OMP_DISTRIBUTE_SIMD
:
11727 case EXEC_OMP_DO_SIMD
:
11728 case EXEC_OMP_SIMD
:
11729 case EXEC_OMP_TARGET_SIMD
:
11730 gfc_resolve_omp_do_blocks (code
, ns
);
11732 case EXEC_SELECT_TYPE
:
11733 /* Blocks are handled in resolve_select_type because we have
11734 to transform the SELECT TYPE into ASSOCIATE first. */
11736 case EXEC_DO_CONCURRENT
:
11737 gfc_do_concurrent_flag
= 1;
11738 gfc_resolve_blocks (code
->block
, ns
);
11739 gfc_do_concurrent_flag
= 2;
11741 case EXEC_OMP_WORKSHARE
:
11742 omp_workshare_save
= omp_workshare_flag
;
11743 omp_workshare_flag
= 1;
11746 gfc_resolve_blocks (code
->block
, ns
);
11750 if (omp_workshare_save
!= -1)
11751 omp_workshare_flag
= omp_workshare_save
;
11755 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
11756 t
= gfc_resolve_expr (code
->expr1
);
11757 forall_flag
= forall_save
;
11758 gfc_do_concurrent_flag
= do_concurrent_save
;
11760 if (!gfc_resolve_expr (code
->expr2
))
11763 if (code
->op
== EXEC_ALLOCATE
11764 && !gfc_resolve_expr (code
->expr3
))
11770 case EXEC_END_BLOCK
:
11771 case EXEC_END_NESTED_BLOCK
:
11775 case EXEC_ERROR_STOP
:
11777 case EXEC_CONTINUE
:
11779 case EXEC_ASSIGN_CALL
:
11782 case EXEC_CRITICAL
:
11783 resolve_critical (code
);
11786 case EXEC_SYNC_ALL
:
11787 case EXEC_SYNC_IMAGES
:
11788 case EXEC_SYNC_MEMORY
:
11789 resolve_sync (code
);
11794 case EXEC_EVENT_POST
:
11795 case EXEC_EVENT_WAIT
:
11796 resolve_lock_unlock_event (code
);
11799 case EXEC_FAIL_IMAGE
:
11800 case EXEC_FORM_TEAM
:
11801 case EXEC_CHANGE_TEAM
:
11802 case EXEC_END_TEAM
:
11803 case EXEC_SYNC_TEAM
:
11807 /* Keep track of which entry we are up to. */
11808 current_entry_id
= code
->ext
.entry
->id
;
11812 resolve_where (code
, NULL
);
11816 if (code
->expr1
!= NULL
)
11818 if (code
->expr1
->expr_type
!= EXPR_VARIABLE
11819 || code
->expr1
->ts
.type
!= BT_INTEGER
11820 || (code
->expr1
->ref
11821 && code
->expr1
->ref
->type
== REF_ARRAY
)
11822 || code
->expr1
->symtree
== NULL
11823 || (code
->expr1
->symtree
->n
.sym
11824 && (code
->expr1
->symtree
->n
.sym
->attr
.flavor
11826 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11827 "scalar INTEGER variable", &code
->expr1
->where
);
11828 else if (code
->expr1
->symtree
->n
.sym
11829 && code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
11830 gfc_error ("Variable %qs has not been assigned a target "
11831 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
11832 &code
->expr1
->where
);
11835 resolve_branch (code
->label1
, code
);
11839 if (code
->expr1
!= NULL
11840 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
11841 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11842 "INTEGER return specifier", &code
->expr1
->where
);
11845 case EXEC_INIT_ASSIGN
:
11846 case EXEC_END_PROCEDURE
:
11853 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11855 if (code
->expr1
->expr_type
== EXPR_FUNCTION
11856 && code
->expr1
->value
.function
.isym
11857 && code
->expr1
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
11858 remove_caf_get_intrinsic (code
->expr1
);
11860 /* If this is a pointer function in an lvalue variable context,
11861 the new code will have to be resolved afresh. This is also the
11862 case with an error, where the code is transformed into NOP to
11863 prevent ICEs downstream. */
11864 if (resolve_ptr_fcn_assign (&code
, ns
)
11865 || code
->op
== EXEC_NOP
)
11868 if (!gfc_check_vardef_context (code
->expr1
, false, false, false,
11872 if (resolve_ordinary_assign (code
, ns
))
11874 if (code
->op
== EXEC_COMPCALL
)
11880 /* Check for dependencies in deferred character length array
11881 assignments and generate a temporary, if necessary. */
11882 if (code
->op
== EXEC_ASSIGN
&& deferred_op_assign (&code
, ns
))
11885 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11886 if (code
->op
!= EXEC_CALL
&& code
->expr1
->ts
.type
== BT_DERIVED
11887 && code
->expr1
->ts
.u
.derived
11888 && code
->expr1
->ts
.u
.derived
->attr
.defined_assign_comp
)
11889 generate_component_assignments (&code
, ns
);
11893 case EXEC_LABEL_ASSIGN
:
11894 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
11895 gfc_error ("Label %d referenced at %L is never defined",
11896 code
->label1
->value
, &code
->label1
->where
);
11898 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
11899 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
11900 || code
->expr1
->symtree
->n
.sym
->ts
.kind
11901 != gfc_default_integer_kind
11902 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
11903 gfc_error ("ASSIGN statement at %L requires a scalar "
11904 "default INTEGER variable", &code
->expr1
->where
);
11907 case EXEC_POINTER_ASSIGN
:
11914 /* This is both a variable definition and pointer assignment
11915 context, so check both of them. For rank remapping, a final
11916 array ref may be present on the LHS and fool gfc_expr_attr
11917 used in gfc_check_vardef_context. Remove it. */
11918 e
= remove_last_array_ref (code
->expr1
);
11919 t
= gfc_check_vardef_context (e
, true, false, false,
11920 _("pointer assignment"));
11922 t
= gfc_check_vardef_context (e
, false, false, false,
11923 _("pointer assignment"));
11926 t
= gfc_check_pointer_assign (code
->expr1
, code
->expr2
, !t
) && t
;
11931 /* Assigning a class object always is a regular assign. */
11932 if (code
->expr2
->ts
.type
== BT_CLASS
11933 && code
->expr1
->ts
.type
== BT_CLASS
11934 && !CLASS_DATA (code
->expr2
)->attr
.dimension
11935 && !(gfc_expr_attr (code
->expr1
).proc_pointer
11936 && code
->expr2
->expr_type
== EXPR_VARIABLE
11937 && code
->expr2
->symtree
->n
.sym
->attr
.flavor
11939 code
->op
= EXEC_ASSIGN
;
11943 case EXEC_ARITHMETIC_IF
:
11945 gfc_expr
*e
= code
->expr1
;
11947 gfc_resolve_expr (e
);
11948 if (e
->expr_type
== EXPR_NULL
)
11949 gfc_error ("Invalid NULL at %L", &e
->where
);
11951 if (t
&& (e
->rank
> 0
11952 || !(e
->ts
.type
== BT_REAL
|| e
->ts
.type
== BT_INTEGER
)))
11953 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11954 "REAL or INTEGER expression", &e
->where
);
11956 resolve_branch (code
->label1
, code
);
11957 resolve_branch (code
->label2
, code
);
11958 resolve_branch (code
->label3
, code
);
11963 if (t
&& code
->expr1
!= NULL
11964 && (code
->expr1
->ts
.type
!= BT_LOGICAL
11965 || code
->expr1
->rank
!= 0))
11966 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11967 &code
->expr1
->where
);
11972 resolve_call (code
);
11975 case EXEC_COMPCALL
:
11977 resolve_typebound_subroutine (code
);
11980 case EXEC_CALL_PPC
:
11981 resolve_ppc_call (code
);
11985 /* Select is complicated. Also, a SELECT construct could be
11986 a transformed computed GOTO. */
11987 resolve_select (code
, false);
11990 case EXEC_SELECT_TYPE
:
11991 resolve_select_type (code
, ns
);
11994 case EXEC_SELECT_RANK
:
11995 resolve_select_rank (code
, ns
);
11999 resolve_block_construct (code
);
12003 if (code
->ext
.iterator
!= NULL
)
12005 gfc_iterator
*iter
= code
->ext
.iterator
;
12006 if (gfc_resolve_iterator (iter
, true, false))
12007 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
,
12012 case EXEC_DO_WHILE
:
12013 if (code
->expr1
== NULL
)
12014 gfc_internal_error ("gfc_resolve_code(): No expression on "
12017 && (code
->expr1
->rank
!= 0
12018 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
12019 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12020 "a scalar LOGICAL expression", &code
->expr1
->where
);
12023 case EXEC_ALLOCATE
:
12025 resolve_allocate_deallocate (code
, "ALLOCATE");
12029 case EXEC_DEALLOCATE
:
12031 resolve_allocate_deallocate (code
, "DEALLOCATE");
12036 if (!gfc_resolve_open (code
->ext
.open
, &code
->loc
))
12039 resolve_branch (code
->ext
.open
->err
, code
);
12043 if (!gfc_resolve_close (code
->ext
.close
, &code
->loc
))
12046 resolve_branch (code
->ext
.close
->err
, code
);
12049 case EXEC_BACKSPACE
:
12053 if (!gfc_resolve_filepos (code
->ext
.filepos
, &code
->loc
))
12056 resolve_branch (code
->ext
.filepos
->err
, code
);
12060 if (!gfc_resolve_inquire (code
->ext
.inquire
))
12063 resolve_branch (code
->ext
.inquire
->err
, code
);
12066 case EXEC_IOLENGTH
:
12067 gcc_assert (code
->ext
.inquire
!= NULL
);
12068 if (!gfc_resolve_inquire (code
->ext
.inquire
))
12071 resolve_branch (code
->ext
.inquire
->err
, code
);
12075 if (!gfc_resolve_wait (code
->ext
.wait
))
12078 resolve_branch (code
->ext
.wait
->err
, code
);
12079 resolve_branch (code
->ext
.wait
->end
, code
);
12080 resolve_branch (code
->ext
.wait
->eor
, code
);
12085 if (!gfc_resolve_dt (code
, code
->ext
.dt
, &code
->loc
))
12088 resolve_branch (code
->ext
.dt
->err
, code
);
12089 resolve_branch (code
->ext
.dt
->end
, code
);
12090 resolve_branch (code
->ext
.dt
->eor
, code
);
12093 case EXEC_TRANSFER
:
12094 resolve_transfer (code
);
12097 case EXEC_DO_CONCURRENT
:
12099 resolve_forall_iterators (code
->ext
.forall_iterator
);
12101 if (code
->expr1
!= NULL
12102 && (code
->expr1
->ts
.type
!= BT_LOGICAL
|| code
->expr1
->rank
))
12103 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12104 "expression", &code
->expr1
->where
);
12107 case EXEC_OACC_PARALLEL_LOOP
:
12108 case EXEC_OACC_PARALLEL
:
12109 case EXEC_OACC_KERNELS_LOOP
:
12110 case EXEC_OACC_KERNELS
:
12111 case EXEC_OACC_SERIAL_LOOP
:
12112 case EXEC_OACC_SERIAL
:
12113 case EXEC_OACC_DATA
:
12114 case EXEC_OACC_HOST_DATA
:
12115 case EXEC_OACC_LOOP
:
12116 case EXEC_OACC_UPDATE
:
12117 case EXEC_OACC_WAIT
:
12118 case EXEC_OACC_CACHE
:
12119 case EXEC_OACC_ENTER_DATA
:
12120 case EXEC_OACC_EXIT_DATA
:
12121 case EXEC_OACC_ATOMIC
:
12122 case EXEC_OACC_DECLARE
:
12123 gfc_resolve_oacc_directive (code
, ns
);
12126 case EXEC_OMP_ATOMIC
:
12127 case EXEC_OMP_BARRIER
:
12128 case EXEC_OMP_CANCEL
:
12129 case EXEC_OMP_CANCELLATION_POINT
:
12130 case EXEC_OMP_CRITICAL
:
12131 case EXEC_OMP_FLUSH
:
12132 case EXEC_OMP_DISTRIBUTE
:
12133 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
12134 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
12135 case EXEC_OMP_DISTRIBUTE_SIMD
:
12137 case EXEC_OMP_DO_SIMD
:
12138 case EXEC_OMP_MASTER
:
12139 case EXEC_OMP_ORDERED
:
12140 case EXEC_OMP_SECTIONS
:
12141 case EXEC_OMP_SIMD
:
12142 case EXEC_OMP_SINGLE
:
12143 case EXEC_OMP_TARGET
:
12144 case EXEC_OMP_TARGET_DATA
:
12145 case EXEC_OMP_TARGET_ENTER_DATA
:
12146 case EXEC_OMP_TARGET_EXIT_DATA
:
12147 case EXEC_OMP_TARGET_PARALLEL
:
12148 case EXEC_OMP_TARGET_PARALLEL_DO
:
12149 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
12150 case EXEC_OMP_TARGET_SIMD
:
12151 case EXEC_OMP_TARGET_TEAMS
:
12152 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
12153 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
12154 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
12155 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
12156 case EXEC_OMP_TARGET_UPDATE
:
12157 case EXEC_OMP_TASK
:
12158 case EXEC_OMP_TASKGROUP
:
12159 case EXEC_OMP_TASKLOOP
:
12160 case EXEC_OMP_TASKLOOP_SIMD
:
12161 case EXEC_OMP_TASKWAIT
:
12162 case EXEC_OMP_TASKYIELD
:
12163 case EXEC_OMP_TEAMS
:
12164 case EXEC_OMP_TEAMS_DISTRIBUTE
:
12165 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
12166 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
12167 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
12168 case EXEC_OMP_WORKSHARE
:
12169 gfc_resolve_omp_directive (code
, ns
);
12172 case EXEC_OMP_PARALLEL
:
12173 case EXEC_OMP_PARALLEL_DO
:
12174 case EXEC_OMP_PARALLEL_DO_SIMD
:
12175 case EXEC_OMP_PARALLEL_SECTIONS
:
12176 case EXEC_OMP_PARALLEL_WORKSHARE
:
12177 omp_workshare_save
= omp_workshare_flag
;
12178 omp_workshare_flag
= 0;
12179 gfc_resolve_omp_directive (code
, ns
);
12180 omp_workshare_flag
= omp_workshare_save
;
12184 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12188 cs_base
= frame
.prev
;
12192 /* Resolve initial values and make sure they are compatible with
12196 resolve_values (gfc_symbol
*sym
)
12200 if (sym
->value
== NULL
)
12203 if (sym
->value
->expr_type
== EXPR_STRUCTURE
)
12204 t
= resolve_structure_cons (sym
->value
, 1);
12206 t
= gfc_resolve_expr (sym
->value
);
12211 gfc_check_assign_symbol (sym
, NULL
, sym
->value
);
12215 /* Verify any BIND(C) derived types in the namespace so we can report errors
12216 for them once, rather than for each variable declared of that type. */
12219 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
12221 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
12222 && derived_sym
->attr
.is_bind_c
== 1)
12223 verify_bind_c_derived_type (derived_sym
);
12229 /* Check the interfaces of DTIO procedures associated with derived
12230 type 'sym'. These procedures can either have typebound bindings or
12231 can appear in DTIO generic interfaces. */
12234 gfc_verify_DTIO_procedures (gfc_symbol
*sym
)
12236 if (!sym
|| sym
->attr
.flavor
!= FL_DERIVED
)
12239 gfc_check_dtio_interfaces (sym
);
12244 /* Verify that any binding labels used in a given namespace do not collide
12245 with the names or binding labels of any global symbols. Multiple INTERFACE
12246 for the same procedure are permitted. */
12249 gfc_verify_binding_labels (gfc_symbol
*sym
)
12252 const char *module
;
12254 if (!sym
|| !sym
->attr
.is_bind_c
|| sym
->attr
.is_iso_c
12255 || sym
->attr
.flavor
== FL_DERIVED
|| !sym
->binding_label
)
12258 gsym
= gfc_find_case_gsymbol (gfc_gsym_root
, sym
->binding_label
);
12261 module
= sym
->module
;
12262 else if (sym
->ns
&& sym
->ns
->proc_name
12263 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
12264 module
= sym
->ns
->proc_name
->name
;
12265 else if (sym
->ns
&& sym
->ns
->parent
12266 && sym
->ns
&& sym
->ns
->parent
->proc_name
12267 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
12268 module
= sym
->ns
->parent
->proc_name
->name
;
12274 && (gsym
->type
== GSYM_FUNCTION
|| gsym
->type
== GSYM_SUBROUTINE
)))
12277 gsym
= gfc_get_gsymbol (sym
->binding_label
, true);
12278 gsym
->where
= sym
->declared_at
;
12279 gsym
->sym_name
= sym
->name
;
12280 gsym
->binding_label
= sym
->binding_label
;
12281 gsym
->ns
= sym
->ns
;
12282 gsym
->mod_name
= module
;
12283 if (sym
->attr
.function
)
12284 gsym
->type
= GSYM_FUNCTION
;
12285 else if (sym
->attr
.subroutine
)
12286 gsym
->type
= GSYM_SUBROUTINE
;
12287 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12288 gsym
->defined
= sym
->attr
.if_source
!= IFSRC_IFBODY
;
12292 if (sym
->attr
.flavor
== FL_VARIABLE
&& gsym
->type
!= GSYM_UNKNOWN
)
12294 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12295 "identifier as entity at %L", sym
->name
,
12296 sym
->binding_label
, &sym
->declared_at
, &gsym
->where
);
12297 /* Clear the binding label to prevent checking multiple times. */
12298 sym
->binding_label
= NULL
;
12302 if (sym
->attr
.flavor
== FL_VARIABLE
&& module
12303 && (strcmp (module
, gsym
->mod_name
) != 0
12304 || strcmp (sym
->name
, gsym
->sym_name
) != 0))
12306 /* This can only happen if the variable is defined in a module - if it
12307 isn't the same module, reject it. */
12308 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12309 "uses the same global identifier as entity at %L from module %qs",
12310 sym
->name
, module
, sym
->binding_label
,
12311 &sym
->declared_at
, &gsym
->where
, gsym
->mod_name
);
12312 sym
->binding_label
= NULL
;
12316 if ((sym
->attr
.function
|| sym
->attr
.subroutine
)
12317 && ((gsym
->type
!= GSYM_SUBROUTINE
&& gsym
->type
!= GSYM_FUNCTION
)
12318 || (gsym
->defined
&& sym
->attr
.if_source
!= IFSRC_IFBODY
))
12319 && (sym
!= gsym
->ns
->proc_name
&& sym
->attr
.entry
== 0)
12320 && (module
!= gsym
->mod_name
12321 || strcmp (gsym
->sym_name
, sym
->name
) != 0
12322 || (module
&& strcmp (module
, gsym
->mod_name
) != 0)))
12324 /* Print an error if the procedure is defined multiple times; we have to
12325 exclude references to the same procedure via module association or
12326 multiple checks for the same procedure. */
12327 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12328 "global identifier as entity at %L", sym
->name
,
12329 sym
->binding_label
, &sym
->declared_at
, &gsym
->where
);
12330 sym
->binding_label
= NULL
;
12335 /* Resolve an index expression. */
12338 resolve_index_expr (gfc_expr
*e
)
12340 if (!gfc_resolve_expr (e
))
12343 if (!gfc_simplify_expr (e
, 0))
12346 if (!gfc_specification_expr (e
))
12353 /* Resolve a charlen structure. */
12356 resolve_charlen (gfc_charlen
*cl
)
12359 bool saved_specification_expr
;
12365 saved_specification_expr
= specification_expr
;
12366 specification_expr
= true;
12368 if (cl
->length_from_typespec
)
12370 if (!gfc_resolve_expr (cl
->length
))
12372 specification_expr
= saved_specification_expr
;
12376 if (!gfc_simplify_expr (cl
->length
, 0))
12378 specification_expr
= saved_specification_expr
;
12382 /* cl->length has been resolved. It should have an integer type. */
12383 if (cl
->length
->ts
.type
!= BT_INTEGER
|| cl
->length
->rank
!= 0)
12385 gfc_error ("Scalar INTEGER expression expected at %L",
12386 &cl
->length
->where
);
12392 if (!resolve_index_expr (cl
->length
))
12394 specification_expr
= saved_specification_expr
;
12399 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12400 a negative value, the length of character entities declared is zero. */
12401 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
12402 && mpz_sgn (cl
->length
->value
.integer
) < 0)
12403 gfc_replace_expr (cl
->length
,
12404 gfc_get_int_expr (gfc_charlen_int_kind
, NULL
, 0));
12406 /* Check that the character length is not too large. */
12407 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
12408 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
12409 && cl
->length
->ts
.type
== BT_INTEGER
12410 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
12412 gfc_error ("String length at %L is too large", &cl
->length
->where
);
12413 specification_expr
= saved_specification_expr
;
12417 specification_expr
= saved_specification_expr
;
12422 /* Test for non-constant shape arrays. */
12425 is_non_constant_shape_array (gfc_symbol
*sym
)
12431 not_constant
= false;
12432 if (sym
->as
!= NULL
)
12434 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12435 has not been simplified; parameter array references. Do the
12436 simplification now. */
12437 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
12439 if (i
== GFC_MAX_DIMENSIONS
)
12442 e
= sym
->as
->lower
[i
];
12443 if (e
&& (!resolve_index_expr(e
)
12444 || !gfc_is_constant_expr (e
)))
12445 not_constant
= true;
12446 e
= sym
->as
->upper
[i
];
12447 if (e
&& (!resolve_index_expr(e
)
12448 || !gfc_is_constant_expr (e
)))
12449 not_constant
= true;
12452 return not_constant
;
12455 /* Given a symbol and an initialization expression, add code to initialize
12456 the symbol to the function entry. */
12458 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
12462 gfc_namespace
*ns
= sym
->ns
;
12464 /* Search for the function namespace if this is a contained
12465 function without an explicit result. */
12466 if (sym
->attr
.function
&& sym
== sym
->result
12467 && sym
->name
!= sym
->ns
->proc_name
->name
)
12469 ns
= ns
->contained
;
12470 for (;ns
; ns
= ns
->sibling
)
12471 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
12477 gfc_free_expr (init
);
12481 /* Build an l-value expression for the result. */
12482 lval
= gfc_lval_expr_from_sym (sym
);
12484 /* Add the code at scope entry. */
12485 init_st
= gfc_get_code (EXEC_INIT_ASSIGN
);
12486 init_st
->next
= ns
->code
;
12487 ns
->code
= init_st
;
12489 /* Assign the default initializer to the l-value. */
12490 init_st
->loc
= sym
->declared_at
;
12491 init_st
->expr1
= lval
;
12492 init_st
->expr2
= init
;
12496 /* Whether or not we can generate a default initializer for a symbol. */
12499 can_generate_init (gfc_symbol
*sym
)
12501 symbol_attribute
*a
;
12506 /* These symbols should never have a default initialization. */
12511 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
12512 && (CLASS_DATA (sym
)->attr
.class_pointer
12513 || CLASS_DATA (sym
)->attr
.proc_pointer
))
12514 || a
->in_equivalence
12521 || (!a
->referenced
&& !a
->result
)
12522 || (a
->dummy
&& a
->intent
!= INTENT_OUT
)
12523 || (a
->function
&& sym
!= sym
->result
)
12528 /* Assign the default initializer to a derived type variable or result. */
12531 apply_default_init (gfc_symbol
*sym
)
12533 gfc_expr
*init
= NULL
;
12535 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
12538 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
12539 init
= gfc_generate_initializer (&sym
->ts
, can_generate_init (sym
));
12541 if (init
== NULL
&& sym
->ts
.type
!= BT_CLASS
)
12544 build_init_assign (sym
, init
);
12545 sym
->attr
.referenced
= 1;
12549 /* Build an initializer for a local. Returns null if the symbol should not have
12550 a default initialization. */
12553 build_default_init_expr (gfc_symbol
*sym
)
12555 /* These symbols should never have a default initialization. */
12556 if (sym
->attr
.allocatable
12557 || sym
->attr
.external
12559 || sym
->attr
.pointer
12560 || sym
->attr
.in_equivalence
12561 || sym
->attr
.in_common
12564 || sym
->attr
.cray_pointee
12565 || sym
->attr
.cray_pointer
12569 /* Get the appropriate init expression. */
12570 return gfc_build_default_init_expr (&sym
->ts
, &sym
->declared_at
);
12573 /* Add an initialization expression to a local variable. */
12575 apply_default_init_local (gfc_symbol
*sym
)
12577 gfc_expr
*init
= NULL
;
12579 /* The symbol should be a variable or a function return value. */
12580 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
12581 || (sym
->attr
.function
&& sym
->result
!= sym
))
12584 /* Try to build the initializer expression. If we can't initialize
12585 this symbol, then init will be NULL. */
12586 init
= build_default_init_expr (sym
);
12590 /* For saved variables, we don't want to add an initializer at function
12591 entry, so we just add a static initializer. Note that automatic variables
12592 are stack allocated even with -fno-automatic; we have also to exclude
12593 result variable, which are also nonstatic. */
12594 if (!sym
->attr
.automatic
12595 && (sym
->attr
.save
|| sym
->ns
->save_all
12596 || (flag_max_stack_var_size
== 0 && !sym
->attr
.result
12597 && (sym
->ns
->proc_name
&& !sym
->ns
->proc_name
->attr
.recursive
)
12598 && (!sym
->attr
.dimension
|| !is_non_constant_shape_array (sym
)))))
12600 /* Don't clobber an existing initializer! */
12601 gcc_assert (sym
->value
== NULL
);
12606 build_init_assign (sym
, init
);
12610 /* Resolution of common features of flavors variable and procedure. */
12613 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
12615 gfc_array_spec
*as
;
12617 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
12618 as
= CLASS_DATA (sym
)->as
;
12622 /* Constraints on deferred shape variable. */
12623 if (as
== NULL
|| as
->type
!= AS_DEFERRED
)
12625 bool pointer
, allocatable
, dimension
;
12627 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
12629 pointer
= CLASS_DATA (sym
)->attr
.class_pointer
;
12630 allocatable
= CLASS_DATA (sym
)->attr
.allocatable
;
12631 dimension
= CLASS_DATA (sym
)->attr
.dimension
;
12635 pointer
= sym
->attr
.pointer
&& !sym
->attr
.select_type_temporary
;
12636 allocatable
= sym
->attr
.allocatable
;
12637 dimension
= sym
->attr
.dimension
;
12642 if (dimension
&& as
->type
!= AS_ASSUMED_RANK
)
12644 gfc_error ("Allocatable array %qs at %L must have a deferred "
12645 "shape or assumed rank", sym
->name
, &sym
->declared_at
);
12648 else if (!gfc_notify_std (GFC_STD_F2003
, "Scalar object "
12649 "%qs at %L may not be ALLOCATABLE",
12650 sym
->name
, &sym
->declared_at
))
12654 if (pointer
&& dimension
&& as
->type
!= AS_ASSUMED_RANK
)
12656 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12657 "assumed rank", sym
->name
, &sym
->declared_at
);
12664 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
12665 && sym
->ts
.type
!= BT_CLASS
&& !sym
->assoc
)
12667 gfc_error ("Array %qs at %L cannot have a deferred shape",
12668 sym
->name
, &sym
->declared_at
);
12673 /* Constraints on polymorphic variables. */
12674 if (sym
->ts
.type
== BT_CLASS
&& !(sym
->result
&& sym
->result
!= sym
))
12677 if (sym
->attr
.class_ok
12678 && !sym
->attr
.select_type_temporary
12679 && !UNLIMITED_POLY (sym
)
12680 && !gfc_type_is_extensible (CLASS_DATA (sym
)->ts
.u
.derived
))
12682 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12683 CLASS_DATA (sym
)->ts
.u
.derived
->name
, sym
->name
,
12684 &sym
->declared_at
);
12689 /* Assume that use associated symbols were checked in the module ns.
12690 Class-variables that are associate-names are also something special
12691 and excepted from the test. */
12692 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
&& !sym
->assoc
)
12694 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12695 "or pointer", sym
->name
, &sym
->declared_at
);
12704 /* Additional checks for symbols with flavor variable and derived
12705 type. To be called from resolve_fl_variable. */
12708 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
12710 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
12712 /* Check to see if a derived type is blocked from being host
12713 associated by the presence of another class I symbol in the same
12714 namespace. 14.6.1.3 of the standard and the discussion on
12715 comp.lang.fortran. */
12716 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
12717 && !sym
->ts
.u
.derived
->attr
.use_assoc
12718 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
12721 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
12722 if (s
&& s
->attr
.generic
)
12723 s
= gfc_find_dt_in_generic (s
);
12724 if (s
&& !gfc_fl_struct (s
->attr
.flavor
))
12726 gfc_error ("The type %qs cannot be host associated at %L "
12727 "because it is blocked by an incompatible object "
12728 "of the same name declared at %L",
12729 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
12735 /* 4th constraint in section 11.3: "If an object of a type for which
12736 component-initialization is specified (R429) appears in the
12737 specification-part of a module and does not have the ALLOCATABLE
12738 or POINTER attribute, the object shall have the SAVE attribute."
12740 The check for initializers is performed with
12741 gfc_has_default_initializer because gfc_default_initializer generates
12742 a hidden default for allocatable components. */
12743 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
12744 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12745 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
) && !sym
->attr
.save
12746 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
12747 && gfc_has_default_initializer (sym
->ts
.u
.derived
)
12748 && !gfc_notify_std (GFC_STD_F2008
, "Implied SAVE for module variable "
12749 "%qs at %L, needed due to the default "
12750 "initialization", sym
->name
, &sym
->declared_at
))
12753 /* Assign default initializer. */
12754 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
12755 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
12756 sym
->value
= gfc_generate_initializer (&sym
->ts
, can_generate_init (sym
));
12762 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12763 except in the declaration of an entity or component that has the POINTER
12764 or ALLOCATABLE attribute. */
12767 deferred_requirements (gfc_symbol
*sym
)
12769 if (sym
->ts
.deferred
12770 && !(sym
->attr
.pointer
12771 || sym
->attr
.allocatable
12772 || sym
->attr
.associate_var
12773 || sym
->attr
.omp_udr_artificial_var
))
12775 /* If a function has a result variable, only check the variable. */
12776 if (sym
->result
&& sym
->name
!= sym
->result
->name
)
12779 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12780 "requires either the POINTER or ALLOCATABLE attribute",
12781 sym
->name
, &sym
->declared_at
);
12788 /* Resolve symbols with flavor variable. */
12791 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
12793 const char *auto_save_msg
= "Automatic object %qs at %L cannot have the "
12796 if (!resolve_fl_var_and_proc (sym
, mp_flag
))
12799 /* Set this flag to check that variables are parameters of all entries.
12800 This check is effected by the call to gfc_resolve_expr through
12801 is_non_constant_shape_array. */
12802 bool saved_specification_expr
= specification_expr
;
12803 specification_expr
= true;
12805 if (sym
->ns
->proc_name
12806 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12807 || sym
->ns
->proc_name
->attr
.is_main_program
)
12808 && !sym
->attr
.use_assoc
12809 && !sym
->attr
.allocatable
12810 && !sym
->attr
.pointer
12811 && is_non_constant_shape_array (sym
))
12813 /* F08:C541. The shape of an array defined in a main program or module
12814 * needs to be constant. */
12815 gfc_error ("The module or main program array %qs at %L must "
12816 "have constant shape", sym
->name
, &sym
->declared_at
);
12817 specification_expr
= saved_specification_expr
;
12821 /* Constraints on deferred type parameter. */
12822 if (!deferred_requirements (sym
))
12825 if (sym
->ts
.type
== BT_CHARACTER
&& !sym
->attr
.associate_var
)
12827 /* Make sure that character string variables with assumed length are
12828 dummy arguments. */
12829 gfc_expr
*e
= NULL
;
12832 e
= sym
->ts
.u
.cl
->length
;
12836 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
12837 && !sym
->ts
.deferred
&& !sym
->attr
.select_type_temporary
12838 && !sym
->attr
.omp_udr_artificial_var
)
12840 gfc_error ("Entity with assumed character length at %L must be a "
12841 "dummy argument or a PARAMETER", &sym
->declared_at
);
12842 specification_expr
= saved_specification_expr
;
12846 if (e
&& sym
->attr
.save
== SAVE_EXPLICIT
&& !gfc_is_constant_expr (e
))
12848 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
12849 specification_expr
= saved_specification_expr
;
12853 if (!gfc_is_constant_expr (e
)
12854 && !(e
->expr_type
== EXPR_VARIABLE
12855 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
))
12857 if (!sym
->attr
.use_assoc
&& sym
->ns
->proc_name
12858 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
12859 || sym
->ns
->proc_name
->attr
.is_main_program
))
12861 gfc_error ("%qs at %L must have constant character length "
12862 "in this context", sym
->name
, &sym
->declared_at
);
12863 specification_expr
= saved_specification_expr
;
12866 if (sym
->attr
.in_common
)
12868 gfc_error ("COMMON variable %qs at %L must have constant "
12869 "character length", sym
->name
, &sym
->declared_at
);
12870 specification_expr
= saved_specification_expr
;
12876 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
12877 apply_default_init_local (sym
); /* Try to apply a default initialization. */
12879 /* Determine if the symbol may not have an initializer. */
12880 int no_init_flag
= 0, automatic_flag
= 0;
12881 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
12882 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
12884 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
12885 && is_non_constant_shape_array (sym
))
12887 no_init_flag
= automatic_flag
= 1;
12889 /* Also, they must not have the SAVE attribute.
12890 SAVE_IMPLICIT is checked below. */
12891 if (sym
->as
&& sym
->attr
.codimension
)
12893 int corank
= sym
->as
->corank
;
12894 sym
->as
->corank
= 0;
12895 no_init_flag
= automatic_flag
= is_non_constant_shape_array (sym
);
12896 sym
->as
->corank
= corank
;
12898 if (automatic_flag
&& sym
->attr
.save
== SAVE_EXPLICIT
)
12900 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
12901 specification_expr
= saved_specification_expr
;
12906 /* Ensure that any initializer is simplified. */
12908 gfc_simplify_expr (sym
->value
, 1);
12910 /* Reject illegal initializers. */
12911 if (!sym
->mark
&& sym
->value
)
12913 if (sym
->attr
.allocatable
|| (sym
->ts
.type
== BT_CLASS
12914 && CLASS_DATA (sym
)->attr
.allocatable
))
12915 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12916 sym
->name
, &sym
->declared_at
);
12917 else if (sym
->attr
.external
)
12918 gfc_error ("External %qs at %L cannot have an initializer",
12919 sym
->name
, &sym
->declared_at
);
12920 else if (sym
->attr
.dummy
12921 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
12922 gfc_error ("Dummy %qs at %L cannot have an initializer",
12923 sym
->name
, &sym
->declared_at
);
12924 else if (sym
->attr
.intrinsic
)
12925 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12926 sym
->name
, &sym
->declared_at
);
12927 else if (sym
->attr
.result
)
12928 gfc_error ("Function result %qs at %L cannot have an initializer",
12929 sym
->name
, &sym
->declared_at
);
12930 else if (automatic_flag
)
12931 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12932 sym
->name
, &sym
->declared_at
);
12934 goto no_init_error
;
12935 specification_expr
= saved_specification_expr
;
12940 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
12942 bool res
= resolve_fl_variable_derived (sym
, no_init_flag
);
12943 specification_expr
= saved_specification_expr
;
12947 specification_expr
= saved_specification_expr
;
12952 /* Compare the dummy characteristics of a module procedure interface
12953 declaration with the corresponding declaration in a submodule. */
12954 static gfc_formal_arglist
*new_formal
;
12955 static char errmsg
[200];
12958 compare_fsyms (gfc_symbol
*sym
)
12962 if (sym
== NULL
|| new_formal
== NULL
)
12965 fsym
= new_formal
->sym
;
12970 if (strcmp (sym
->name
, fsym
->name
) == 0)
12972 if (!gfc_check_dummy_characteristics (fsym
, sym
, true, errmsg
, 200))
12973 gfc_error ("%s at %L", errmsg
, &fsym
->declared_at
);
12978 /* Resolve a procedure. */
12981 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
12983 gfc_formal_arglist
*arg
;
12985 if (sym
->attr
.function
12986 && !resolve_fl_var_and_proc (sym
, mp_flag
))
12989 /* Constraints on deferred type parameter. */
12990 if (!deferred_requirements (sym
))
12993 if (sym
->ts
.type
== BT_CHARACTER
)
12995 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
12997 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
12998 && !resolve_charlen (cl
))
13001 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
13002 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
13004 gfc_error ("Character-valued statement function %qs at %L must "
13005 "have constant length", sym
->name
, &sym
->declared_at
);
13010 /* Ensure that derived type for are not of a private type. Internal
13011 module procedures are excluded by 2.2.3.3 - i.e., they are not
13012 externally accessible and can access all the objects accessible in
13014 if (!(sym
->ns
->parent
&& sym
->ns
->parent
->proc_name
13015 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
13016 && gfc_check_symbol_access (sym
))
13018 gfc_interface
*iface
;
13020 for (arg
= gfc_sym_get_dummy_args (sym
); arg
; arg
= arg
->next
)
13023 && arg
->sym
->ts
.type
== BT_DERIVED
13024 && arg
->sym
->ts
.u
.derived
13025 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
13026 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
13027 && !gfc_notify_std (GFC_STD_F2003
, "%qs is of a PRIVATE type "
13028 "and cannot be a dummy argument"
13029 " of %qs, which is PUBLIC at %L",
13030 arg
->sym
->name
, sym
->name
,
13031 &sym
->declared_at
))
13033 /* Stop this message from recurring. */
13034 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
13039 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13040 PRIVATE to the containing module. */
13041 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
13043 for (arg
= gfc_sym_get_dummy_args (iface
->sym
); arg
; arg
= arg
->next
)
13046 && arg
->sym
->ts
.type
== BT_DERIVED
13047 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
13048 && !gfc_check_symbol_access (arg
->sym
->ts
.u
.derived
)
13049 && !gfc_notify_std (GFC_STD_F2003
, "Procedure %qs in "
13050 "PUBLIC interface %qs at %L "
13051 "takes dummy arguments of %qs which "
13052 "is PRIVATE", iface
->sym
->name
,
13053 sym
->name
, &iface
->sym
->declared_at
,
13054 gfc_typename(&arg
->sym
->ts
)))
13056 /* Stop this message from recurring. */
13057 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
13064 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
13065 && !sym
->attr
.proc_pointer
)
13067 gfc_error ("Function %qs at %L cannot have an initializer",
13068 sym
->name
, &sym
->declared_at
);
13070 /* Make sure no second error is issued for this. */
13071 sym
->value
->error
= 1;
13075 /* An external symbol may not have an initializer because it is taken to be
13076 a procedure. Exception: Procedure Pointers. */
13077 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
13079 gfc_error ("External object %qs at %L may not have an initializer",
13080 sym
->name
, &sym
->declared_at
);
13084 /* An elemental function is required to return a scalar 12.7.1 */
13085 if (sym
->attr
.elemental
&& sym
->attr
.function
13086 && (sym
->as
|| (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->as
)))
13088 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13089 "result", sym
->name
, &sym
->declared_at
);
13090 /* Reset so that the error only occurs once. */
13091 sym
->attr
.elemental
= 0;
13095 if (sym
->attr
.proc
== PROC_ST_FUNCTION
13096 && (sym
->attr
.allocatable
|| sym
->attr
.pointer
))
13098 gfc_error ("Statement function %qs at %L may not have pointer or "
13099 "allocatable attribute", sym
->name
, &sym
->declared_at
);
13103 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13104 char-len-param shall not be array-valued, pointer-valued, recursive
13105 or pure. ....snip... A character value of * may only be used in the
13106 following ways: (i) Dummy arg of procedure - dummy associates with
13107 actual length; (ii) To declare a named constant; or (iii) External
13108 function - but length must be declared in calling scoping unit. */
13109 if (sym
->attr
.function
13110 && sym
->ts
.type
== BT_CHARACTER
&& !sym
->ts
.deferred
13111 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
13113 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
13114 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
13116 if (sym
->as
&& sym
->as
->rank
)
13117 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13118 "array-valued", sym
->name
, &sym
->declared_at
);
13120 if (sym
->attr
.pointer
)
13121 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13122 "pointer-valued", sym
->name
, &sym
->declared_at
);
13124 if (sym
->attr
.pure
)
13125 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13126 "pure", sym
->name
, &sym
->declared_at
);
13128 if (sym
->attr
.recursive
)
13129 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13130 "recursive", sym
->name
, &sym
->declared_at
);
13135 /* Appendix B.2 of the standard. Contained functions give an
13136 error anyway. Deferred character length is an F2003 feature.
13137 Don't warn on intrinsic conversion functions, which start
13138 with two underscores. */
13139 if (!sym
->attr
.contained
&& !sym
->ts
.deferred
13140 && (sym
->name
[0] != '_' || sym
->name
[1] != '_'))
13141 gfc_notify_std (GFC_STD_F95_OBS
,
13142 "CHARACTER(*) function %qs at %L",
13143 sym
->name
, &sym
->declared_at
);
13146 /* F2008, C1218. */
13147 if (sym
->attr
.elemental
)
13149 if (sym
->attr
.proc_pointer
)
13151 const char* name
= (sym
->attr
.result
? sym
->ns
->proc_name
->name
13153 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13154 name
, &sym
->declared_at
);
13157 if (sym
->attr
.dummy
)
13159 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13160 sym
->name
, &sym
->declared_at
);
13165 /* F2018, C15100: "The result of an elemental function shall be scalar,
13166 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13167 pointer is tested and caught elsewhere. */
13168 if (sym
->attr
.elemental
&& sym
->result
13169 && (sym
->result
->attr
.allocatable
|| sym
->result
->attr
.pointer
))
13171 gfc_error ("Function result variable %qs at %L of elemental "
13172 "function %qs shall not have an ALLOCATABLE or POINTER "
13173 "attribute", sym
->result
->name
,
13174 &sym
->result
->declared_at
, sym
->name
);
13178 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
13180 gfc_formal_arglist
*curr_arg
;
13181 int has_non_interop_arg
= 0;
13183 if (!verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
13184 sym
->common_block
))
13186 /* Clear these to prevent looking at them again if there was an
13188 sym
->attr
.is_bind_c
= 0;
13189 sym
->attr
.is_c_interop
= 0;
13190 sym
->ts
.is_c_interop
= 0;
13194 /* So far, no errors have been found. */
13195 sym
->attr
.is_c_interop
= 1;
13196 sym
->ts
.is_c_interop
= 1;
13199 curr_arg
= gfc_sym_get_dummy_args (sym
);
13200 while (curr_arg
!= NULL
)
13202 /* Skip implicitly typed dummy args here. */
13203 if (curr_arg
->sym
&& curr_arg
->sym
->attr
.implicit_type
== 0)
13204 if (!gfc_verify_c_interop_param (curr_arg
->sym
))
13205 /* If something is found to fail, record the fact so we
13206 can mark the symbol for the procedure as not being
13207 BIND(C) to try and prevent multiple errors being
13209 has_non_interop_arg
= 1;
13211 curr_arg
= curr_arg
->next
;
13214 /* See if any of the arguments were not interoperable and if so, clear
13215 the procedure symbol to prevent duplicate error messages. */
13216 if (has_non_interop_arg
!= 0)
13218 sym
->attr
.is_c_interop
= 0;
13219 sym
->ts
.is_c_interop
= 0;
13220 sym
->attr
.is_bind_c
= 0;
13224 if (!sym
->attr
.proc_pointer
)
13226 if (sym
->attr
.save
== SAVE_EXPLICIT
)
13228 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13229 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13232 if (sym
->attr
.intent
)
13234 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13235 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13238 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
13240 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13241 "in %qs at %L", sym
->ns
->proc_name
->name
, &sym
->declared_at
);
13244 if (sym
->attr
.external
&& sym
->attr
.function
&& !sym
->attr
.module_procedure
13245 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
13246 || sym
->attr
.contained
))
13248 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13249 "in %qs at %L", sym
->name
, &sym
->declared_at
);
13252 if (strcmp ("ppr@", sym
->name
) == 0)
13254 gfc_error ("Procedure pointer result %qs at %L "
13255 "is missing the pointer attribute",
13256 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
13261 /* Assume that a procedure whose body is not known has references
13262 to external arrays. */
13263 if (sym
->attr
.if_source
!= IFSRC_DECL
)
13264 sym
->attr
.array_outer_dependency
= 1;
13266 /* Compare the characteristics of a module procedure with the
13267 interface declaration. Ideally this would be done with
13268 gfc_compare_interfaces but, at present, the formal interface
13269 cannot be copied to the ts.interface. */
13270 if (sym
->attr
.module_procedure
13271 && sym
->attr
.if_source
== IFSRC_DECL
)
13274 char name
[2*GFC_MAX_SYMBOL_LEN
+ 1];
13276 char *submodule_name
;
13277 strcpy (name
, sym
->ns
->proc_name
->name
);
13278 module_name
= strtok (name
, ".");
13279 submodule_name
= strtok (NULL
, ".");
13281 iface
= sym
->tlink
;
13284 /* Make sure that the result uses the correct charlen for deferred
13286 if (iface
&& sym
->result
13287 && iface
->ts
.type
== BT_CHARACTER
13288 && iface
->ts
.deferred
)
13289 sym
->result
->ts
.u
.cl
= iface
->ts
.u
.cl
;
13294 /* Check the procedure characteristics. */
13295 if (sym
->attr
.elemental
!= iface
->attr
.elemental
)
13297 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13298 "PROCEDURE at %L and its interface in %s",
13299 &sym
->declared_at
, module_name
);
13303 if (sym
->attr
.pure
!= iface
->attr
.pure
)
13305 gfc_error ("Mismatch in PURE attribute between MODULE "
13306 "PROCEDURE at %L and its interface in %s",
13307 &sym
->declared_at
, module_name
);
13311 if (sym
->attr
.recursive
!= iface
->attr
.recursive
)
13313 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13314 "PROCEDURE at %L and its interface in %s",
13315 &sym
->declared_at
, module_name
);
13319 /* Check the result characteristics. */
13320 if (!gfc_check_result_characteristics (sym
, iface
, errmsg
, 200))
13322 gfc_error ("%s between the MODULE PROCEDURE declaration "
13323 "in MODULE %qs and the declaration at %L in "
13325 errmsg
, module_name
, &sym
->declared_at
,
13326 submodule_name
? submodule_name
: module_name
);
13331 /* Check the characteristics of the formal arguments. */
13332 if (sym
->formal
&& sym
->formal_ns
)
13334 for (arg
= sym
->formal
; arg
&& arg
->sym
; arg
= arg
->next
)
13337 gfc_traverse_ns (sym
->formal_ns
, compare_fsyms
);
13345 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13346 been defined and we now know their defined arguments, check that they fulfill
13347 the requirements of the standard for procedures used as finalizers. */
13350 gfc_resolve_finalizers (gfc_symbol
* derived
, bool *finalizable
)
13352 gfc_finalizer
* list
;
13353 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
13354 bool result
= true;
13355 bool seen_scalar
= false;
13358 gfc_symbol
*parent
= gfc_get_derived_super_type (derived
);
13361 gfc_resolve_finalizers (parent
, finalizable
);
13363 /* Ensure that derived-type components have a their finalizers resolved. */
13364 bool has_final
= derived
->f2k_derived
&& derived
->f2k_derived
->finalizers
;
13365 for (c
= derived
->components
; c
; c
= c
->next
)
13366 if (c
->ts
.type
== BT_DERIVED
13367 && !c
->attr
.pointer
&& !c
->attr
.proc_pointer
&& !c
->attr
.allocatable
)
13369 bool has_final2
= false;
13370 if (!gfc_resolve_finalizers (c
->ts
.u
.derived
, &has_final2
))
13371 return false; /* Error. */
13372 has_final
= has_final
|| has_final2
;
13374 /* Return early if not finalizable. */
13378 *finalizable
= false;
13382 /* Walk over the list of finalizer-procedures, check them, and if any one
13383 does not fit in with the standard's definition, print an error and remove
13384 it from the list. */
13385 prev_link
= &derived
->f2k_derived
->finalizers
;
13386 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
13388 gfc_formal_arglist
*dummy_args
;
13393 /* Skip this finalizer if we already resolved it. */
13394 if (list
->proc_tree
)
13396 if (list
->proc_tree
->n
.sym
->formal
->sym
->as
== NULL
13397 || list
->proc_tree
->n
.sym
->formal
->sym
->as
->rank
== 0)
13398 seen_scalar
= true;
13399 prev_link
= &(list
->next
);
13403 /* Check this exists and is a SUBROUTINE. */
13404 if (!list
->proc_sym
->attr
.subroutine
)
13406 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13407 list
->proc_sym
->name
, &list
->where
);
13411 /* We should have exactly one argument. */
13412 dummy_args
= gfc_sym_get_dummy_args (list
->proc_sym
);
13413 if (!dummy_args
|| dummy_args
->next
)
13415 gfc_error ("FINAL procedure at %L must have exactly one argument",
13419 arg
= dummy_args
->sym
;
13421 /* This argument must be of our type. */
13422 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
13424 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13425 &arg
->declared_at
, derived
->name
);
13429 /* It must neither be a pointer nor allocatable nor optional. */
13430 if (arg
->attr
.pointer
)
13432 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13433 &arg
->declared_at
);
13436 if (arg
->attr
.allocatable
)
13438 gfc_error ("Argument of FINAL procedure at %L must not be"
13439 " ALLOCATABLE", &arg
->declared_at
);
13442 if (arg
->attr
.optional
)
13444 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13445 &arg
->declared_at
);
13449 /* It must not be INTENT(OUT). */
13450 if (arg
->attr
.intent
== INTENT_OUT
)
13452 gfc_error ("Argument of FINAL procedure at %L must not be"
13453 " INTENT(OUT)", &arg
->declared_at
);
13457 /* Warn if the procedure is non-scalar and not assumed shape. */
13458 if (warn_surprising
&& arg
->as
&& arg
->as
->rank
!= 0
13459 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
13460 gfc_warning (OPT_Wsurprising
,
13461 "Non-scalar FINAL procedure at %L should have assumed"
13462 " shape argument", &arg
->declared_at
);
13464 /* Check that it does not match in kind and rank with a FINAL procedure
13465 defined earlier. To really loop over the *earlier* declarations,
13466 we need to walk the tail of the list as new ones were pushed at the
13468 /* TODO: Handle kind parameters once they are implemented. */
13469 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
13470 for (i
= list
->next
; i
; i
= i
->next
)
13472 gfc_formal_arglist
*dummy_args
;
13474 /* Argument list might be empty; that is an error signalled earlier,
13475 but we nevertheless continued resolving. */
13476 dummy_args
= gfc_sym_get_dummy_args (i
->proc_sym
);
13479 gfc_symbol
* i_arg
= dummy_args
->sym
;
13480 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
13481 if (i_rank
== my_rank
)
13483 gfc_error ("FINAL procedure %qs declared at %L has the same"
13484 " rank (%d) as %qs",
13485 list
->proc_sym
->name
, &list
->where
, my_rank
,
13486 i
->proc_sym
->name
);
13492 /* Is this the/a scalar finalizer procedure? */
13494 seen_scalar
= true;
13496 /* Find the symtree for this procedure. */
13497 gcc_assert (!list
->proc_tree
);
13498 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
13500 prev_link
= &list
->next
;
13503 /* Remove wrong nodes immediately from the list so we don't risk any
13504 troubles in the future when they might fail later expectations. */
13507 *prev_link
= list
->next
;
13508 gfc_free_finalizer (i
);
13512 if (result
== false)
13515 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13516 were nodes in the list, must have been for arrays. It is surely a good
13517 idea to have a scalar version there if there's something to finalize. */
13518 if (warn_surprising
&& derived
->f2k_derived
->finalizers
&& !seen_scalar
)
13519 gfc_warning (OPT_Wsurprising
,
13520 "Only array FINAL procedures declared for derived type %qs"
13521 " defined at %L, suggest also scalar one",
13522 derived
->name
, &derived
->declared_at
);
13524 vtab
= gfc_find_derived_vtab (derived
);
13525 c
= vtab
->ts
.u
.derived
->components
->next
->next
->next
->next
->next
;
13526 gfc_set_sym_referenced (c
->initializer
->symtree
->n
.sym
);
13529 *finalizable
= true;
13535 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13538 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
13539 const char* generic_name
, locus where
)
13541 gfc_symbol
*sym1
, *sym2
;
13542 const char *pass1
, *pass2
;
13543 gfc_formal_arglist
*dummy_args
;
13545 gcc_assert (t1
->specific
&& t2
->specific
);
13546 gcc_assert (!t1
->specific
->is_generic
);
13547 gcc_assert (!t2
->specific
->is_generic
);
13548 gcc_assert (t1
->is_operator
== t2
->is_operator
);
13550 sym1
= t1
->specific
->u
.specific
->n
.sym
;
13551 sym2
= t2
->specific
->u
.specific
->n
.sym
;
13556 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13557 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
13558 || sym1
->attr
.function
!= sym2
->attr
.function
)
13560 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13561 " GENERIC %qs at %L",
13562 sym1
->name
, sym2
->name
, generic_name
, &where
);
13566 /* Determine PASS arguments. */
13567 if (t1
->specific
->nopass
)
13569 else if (t1
->specific
->pass_arg
)
13570 pass1
= t1
->specific
->pass_arg
;
13573 dummy_args
= gfc_sym_get_dummy_args (t1
->specific
->u
.specific
->n
.sym
);
13575 pass1
= dummy_args
->sym
->name
;
13579 if (t2
->specific
->nopass
)
13581 else if (t2
->specific
->pass_arg
)
13582 pass2
= t2
->specific
->pass_arg
;
13585 dummy_args
= gfc_sym_get_dummy_args (t2
->specific
->u
.specific
->n
.sym
);
13587 pass2
= dummy_args
->sym
->name
;
13592 /* Compare the interfaces. */
13593 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, !t1
->is_operator
, 0,
13594 NULL
, 0, pass1
, pass2
))
13596 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13597 sym1
->name
, sym2
->name
, generic_name
, &where
);
13605 /* Worker function for resolving a generic procedure binding; this is used to
13606 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13608 The difference between those cases is finding possible inherited bindings
13609 that are overridden, as one has to look for them in tb_sym_root,
13610 tb_uop_root or tb_op, respectively. Thus the caller must already find
13611 the super-type and set p->overridden correctly. */
13614 resolve_tb_generic_targets (gfc_symbol
* super_type
,
13615 gfc_typebound_proc
* p
, const char* name
)
13617 gfc_tbp_generic
* target
;
13618 gfc_symtree
* first_target
;
13619 gfc_symtree
* inherited
;
13621 gcc_assert (p
&& p
->is_generic
);
13623 /* Try to find the specific bindings for the symtrees in our target-list. */
13624 gcc_assert (p
->u
.generic
);
13625 for (target
= p
->u
.generic
; target
; target
= target
->next
)
13626 if (!target
->specific
)
13628 gfc_typebound_proc
* overridden_tbp
;
13629 gfc_tbp_generic
* g
;
13630 const char* target_name
;
13632 target_name
= target
->specific_st
->name
;
13634 /* Defined for this type directly. */
13635 if (target
->specific_st
->n
.tb
&& !target
->specific_st
->n
.tb
->error
)
13637 target
->specific
= target
->specific_st
->n
.tb
;
13638 goto specific_found
;
13641 /* Look for an inherited specific binding. */
13644 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
13649 gcc_assert (inherited
->n
.tb
);
13650 target
->specific
= inherited
->n
.tb
;
13651 goto specific_found
;
13655 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13656 " at %L", target_name
, name
, &p
->where
);
13659 /* Once we've found the specific binding, check it is not ambiguous with
13660 other specifics already found or inherited for the same GENERIC. */
13662 gcc_assert (target
->specific
);
13664 /* This must really be a specific binding! */
13665 if (target
->specific
->is_generic
)
13667 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13668 " %qs is GENERIC, too", name
, &p
->where
, target_name
);
13672 /* Check those already resolved on this type directly. */
13673 for (g
= p
->u
.generic
; g
; g
= g
->next
)
13674 if (g
!= target
&& g
->specific
13675 && !check_generic_tbp_ambiguity (target
, g
, name
, p
->where
))
13678 /* Check for ambiguity with inherited specific targets. */
13679 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
13680 overridden_tbp
= overridden_tbp
->overridden
)
13681 if (overridden_tbp
->is_generic
)
13683 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
13685 gcc_assert (g
->specific
);
13686 if (!check_generic_tbp_ambiguity (target
, g
, name
, p
->where
))
13692 /* If we attempt to "overwrite" a specific binding, this is an error. */
13693 if (p
->overridden
&& !p
->overridden
->is_generic
)
13695 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13696 " the same name", name
, &p
->where
);
13700 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13701 all must have the same attributes here. */
13702 first_target
= p
->u
.generic
->specific
->u
.specific
;
13703 gcc_assert (first_target
);
13704 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
13705 p
->function
= first_target
->n
.sym
->attr
.function
;
13711 /* Resolve a GENERIC procedure binding for a derived type. */
13714 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
13716 gfc_symbol
* super_type
;
13718 /* Find the overridden binding if any. */
13719 st
->n
.tb
->overridden
= NULL
;
13720 super_type
= gfc_get_derived_super_type (derived
);
13723 gfc_symtree
* overridden
;
13724 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
13727 if (overridden
&& overridden
->n
.tb
)
13728 st
->n
.tb
->overridden
= overridden
->n
.tb
;
13731 /* Resolve using worker function. */
13732 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
13736 /* Retrieve the target-procedure of an operator binding and do some checks in
13737 common for intrinsic and user-defined type-bound operators. */
13740 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
13742 gfc_symbol
* target_proc
;
13744 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
13745 target_proc
= target
->specific
->u
.specific
->n
.sym
;
13746 gcc_assert (target_proc
);
13748 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13749 if (target
->specific
->nopass
)
13751 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where
);
13755 return target_proc
;
13759 /* Resolve a type-bound intrinsic operator. */
13762 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
13763 gfc_typebound_proc
* p
)
13765 gfc_symbol
* super_type
;
13766 gfc_tbp_generic
* target
;
13768 /* If there's already an error here, do nothing (but don't fail again). */
13772 /* Operators should always be GENERIC bindings. */
13773 gcc_assert (p
->is_generic
);
13775 /* Look for an overridden binding. */
13776 super_type
= gfc_get_derived_super_type (derived
);
13777 if (super_type
&& super_type
->f2k_derived
)
13778 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
13781 p
->overridden
= NULL
;
13783 /* Resolve general GENERIC properties using worker function. */
13784 if (!resolve_tb_generic_targets (super_type
, p
, gfc_op2string(op
)))
13787 /* Check the targets to be procedures of correct interface. */
13788 for (target
= p
->u
.generic
; target
; target
= target
->next
)
13790 gfc_symbol
* target_proc
;
13792 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
13796 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
13799 /* Add target to non-typebound operator list. */
13800 if (!target
->specific
->deferred
&& !derived
->attr
.use_assoc
13801 && p
->access
!= ACCESS_PRIVATE
&& derived
->ns
== gfc_current_ns
)
13803 gfc_interface
*head
, *intr
;
13805 /* Preempt 'gfc_check_new_interface' for submodules, where the
13806 mechanism for handling module procedures winds up resolving
13807 operator interfaces twice and would otherwise cause an error. */
13808 for (intr
= derived
->ns
->op
[op
]; intr
; intr
= intr
->next
)
13809 if (intr
->sym
== target_proc
13810 && target_proc
->attr
.used_in_submodule
)
13813 if (!gfc_check_new_interface (derived
->ns
->op
[op
],
13814 target_proc
, p
->where
))
13816 head
= derived
->ns
->op
[op
];
13817 intr
= gfc_get_interface ();
13818 intr
->sym
= target_proc
;
13819 intr
->where
= p
->where
;
13821 derived
->ns
->op
[op
] = intr
;
13833 /* Resolve a type-bound user operator (tree-walker callback). */
13835 static gfc_symbol
* resolve_bindings_derived
;
13836 static bool resolve_bindings_result
;
13838 static bool check_uop_procedure (gfc_symbol
* sym
, locus where
);
13841 resolve_typebound_user_op (gfc_symtree
* stree
)
13843 gfc_symbol
* super_type
;
13844 gfc_tbp_generic
* target
;
13846 gcc_assert (stree
&& stree
->n
.tb
);
13848 if (stree
->n
.tb
->error
)
13851 /* Operators should always be GENERIC bindings. */
13852 gcc_assert (stree
->n
.tb
->is_generic
);
13854 /* Find overridden procedure, if any. */
13855 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
13856 if (super_type
&& super_type
->f2k_derived
)
13858 gfc_symtree
* overridden
;
13859 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
13860 stree
->name
, true, NULL
);
13862 if (overridden
&& overridden
->n
.tb
)
13863 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
13866 stree
->n
.tb
->overridden
= NULL
;
13868 /* Resolve basically using worker function. */
13869 if (!resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
))
13872 /* Check the targets to be functions of correct interface. */
13873 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
13875 gfc_symbol
* target_proc
;
13877 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
13881 if (!check_uop_procedure (target_proc
, stree
->n
.tb
->where
))
13888 resolve_bindings_result
= false;
13889 stree
->n
.tb
->error
= 1;
13893 /* Resolve the type-bound procedures for a derived type. */
13896 resolve_typebound_procedure (gfc_symtree
* stree
)
13900 gfc_symbol
* me_arg
;
13901 gfc_symbol
* super_type
;
13902 gfc_component
* comp
;
13904 gcc_assert (stree
);
13906 /* Undefined specific symbol from GENERIC target definition. */
13910 if (stree
->n
.tb
->error
)
13913 /* If this is a GENERIC binding, use that routine. */
13914 if (stree
->n
.tb
->is_generic
)
13916 if (!resolve_typebound_generic (resolve_bindings_derived
, stree
))
13921 /* Get the target-procedure to check it. */
13922 gcc_assert (!stree
->n
.tb
->is_generic
);
13923 gcc_assert (stree
->n
.tb
->u
.specific
);
13924 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
13925 where
= stree
->n
.tb
->where
;
13927 /* Default access should already be resolved from the parser. */
13928 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
13930 if (stree
->n
.tb
->deferred
)
13932 if (!check_proc_interface (proc
, &where
))
13937 /* If proc has not been resolved at this point, proc->name may
13938 actually be a USE associated entity. See PR fortran/89647. */
13939 if (!proc
->resolve_symbol_called
13940 && proc
->attr
.function
== 0 && proc
->attr
.subroutine
== 0)
13943 gfc_find_symbol (proc
->name
, gfc_current_ns
->parent
, 1, &tmp
);
13944 if (tmp
&& tmp
->attr
.use_assoc
)
13946 proc
->module
= tmp
->module
;
13947 proc
->attr
.proc
= tmp
->attr
.proc
;
13948 proc
->attr
.function
= tmp
->attr
.function
;
13949 proc
->attr
.subroutine
= tmp
->attr
.subroutine
;
13950 proc
->attr
.use_assoc
= tmp
->attr
.use_assoc
;
13951 proc
->ts
= tmp
->ts
;
13952 proc
->result
= tmp
->result
;
13956 /* Check for F08:C465. */
13957 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
13958 || (proc
->attr
.proc
!= PROC_MODULE
13959 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
13960 || proc
->attr
.abstract
)
13962 gfc_error ("%qs must be a module procedure or an external "
13963 "procedure with an explicit interface at %L",
13964 proc
->name
, &where
);
13969 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
13970 stree
->n
.tb
->function
= proc
->attr
.function
;
13972 /* Find the super-type of the current derived type. We could do this once and
13973 store in a global if speed is needed, but as long as not I believe this is
13974 more readable and clearer. */
13975 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
13977 /* If PASS, resolve and check arguments if not already resolved / loaded
13978 from a .mod file. */
13979 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
13981 gfc_formal_arglist
*dummy_args
;
13983 dummy_args
= gfc_sym_get_dummy_args (proc
);
13984 if (stree
->n
.tb
->pass_arg
)
13986 gfc_formal_arglist
*i
;
13988 /* If an explicit passing argument name is given, walk the arg-list
13989 and look for it. */
13992 stree
->n
.tb
->pass_arg_num
= 1;
13993 for (i
= dummy_args
; i
; i
= i
->next
)
13995 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
14000 ++stree
->n
.tb
->pass_arg_num
;
14005 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14007 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
14008 stree
->n
.tb
->pass_arg
);
14014 /* Otherwise, take the first one; there should in fact be at least
14016 stree
->n
.tb
->pass_arg_num
= 1;
14019 gfc_error ("Procedure %qs with PASS at %L must have at"
14020 " least one argument", proc
->name
, &where
);
14023 me_arg
= dummy_args
->sym
;
14026 /* Now check that the argument-type matches and the passed-object
14027 dummy argument is generally fine. */
14029 gcc_assert (me_arg
);
14031 if (me_arg
->ts
.type
!= BT_CLASS
)
14033 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14034 " at %L", proc
->name
, &where
);
14038 if (CLASS_DATA (me_arg
)->ts
.u
.derived
14039 != resolve_bindings_derived
)
14041 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14042 " the derived-type %qs", me_arg
->name
, proc
->name
,
14043 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
14047 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
14048 if (CLASS_DATA (me_arg
)->as
&& CLASS_DATA (me_arg
)->as
->rank
!= 0)
14050 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14051 " scalar", proc
->name
, &where
);
14054 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
14056 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14057 " be ALLOCATABLE", proc
->name
, &where
);
14060 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
14062 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14063 " be POINTER", proc
->name
, &where
);
14068 /* If we are extending some type, check that we don't override a procedure
14069 flagged NON_OVERRIDABLE. */
14070 stree
->n
.tb
->overridden
= NULL
;
14073 gfc_symtree
* overridden
;
14074 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
14075 stree
->name
, true, NULL
);
14079 if (overridden
->n
.tb
)
14080 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
14082 if (!gfc_check_typebound_override (stree
, overridden
))
14087 /* See if there's a name collision with a component directly in this type. */
14088 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
14089 if (!strcmp (comp
->name
, stree
->name
))
14091 gfc_error ("Procedure %qs at %L has the same name as a component of"
14093 stree
->name
, &where
, resolve_bindings_derived
->name
);
14097 /* Try to find a name collision with an inherited component. */
14098 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true,
14101 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14102 " component of %qs",
14103 stree
->name
, &where
, resolve_bindings_derived
->name
);
14107 stree
->n
.tb
->error
= 0;
14111 resolve_bindings_result
= false;
14112 stree
->n
.tb
->error
= 1;
14117 resolve_typebound_procedures (gfc_symbol
* derived
)
14120 gfc_symbol
* super_type
;
14122 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
14125 super_type
= gfc_get_derived_super_type (derived
);
14127 resolve_symbol (super_type
);
14129 resolve_bindings_derived
= derived
;
14130 resolve_bindings_result
= true;
14132 if (derived
->f2k_derived
->tb_sym_root
)
14133 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
14134 &resolve_typebound_procedure
);
14136 if (derived
->f2k_derived
->tb_uop_root
)
14137 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
14138 &resolve_typebound_user_op
);
14140 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
14142 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
14143 if (p
&& !resolve_typebound_intrinsic_op (derived
,
14144 (gfc_intrinsic_op
)op
, p
))
14145 resolve_bindings_result
= false;
14148 return resolve_bindings_result
;
14152 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14153 to give all identical derived types the same backend_decl. */
14155 add_dt_to_dt_list (gfc_symbol
*derived
)
14157 if (!derived
->dt_next
)
14159 if (gfc_derived_types
)
14161 derived
->dt_next
= gfc_derived_types
->dt_next
;
14162 gfc_derived_types
->dt_next
= derived
;
14166 derived
->dt_next
= derived
;
14168 gfc_derived_types
= derived
;
14173 /* Ensure that a derived-type is really not abstract, meaning that every
14174 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14177 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
14182 if (!ensure_not_abstract_walker (sub
, st
->left
))
14184 if (!ensure_not_abstract_walker (sub
, st
->right
))
14187 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
14189 gfc_symtree
* overriding
;
14190 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
14193 gcc_assert (overriding
->n
.tb
);
14194 if (overriding
->n
.tb
->deferred
)
14196 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14197 " %qs is DEFERRED and not overridden",
14198 sub
->name
, &sub
->declared_at
, st
->name
);
14207 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
14209 /* The algorithm used here is to recursively travel up the ancestry of sub
14210 and for each ancestor-type, check all bindings. If any of them is
14211 DEFERRED, look it up starting from sub and see if the found (overriding)
14212 binding is not DEFERRED.
14213 This is not the most efficient way to do this, but it should be ok and is
14214 clearer than something sophisticated. */
14216 gcc_assert (ancestor
&& !sub
->attr
.abstract
);
14218 if (!ancestor
->attr
.abstract
)
14221 /* Walk bindings of this ancestor. */
14222 if (ancestor
->f2k_derived
)
14225 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
14230 /* Find next ancestor type and recurse on it. */
14231 ancestor
= gfc_get_derived_super_type (ancestor
);
14233 return ensure_not_abstract (sub
, ancestor
);
14239 /* This check for typebound defined assignments is done recursively
14240 since the order in which derived types are resolved is not always in
14241 order of the declarations. */
14244 check_defined_assignments (gfc_symbol
*derived
)
14248 for (c
= derived
->components
; c
; c
= c
->next
)
14250 if (!gfc_bt_struct (c
->ts
.type
)
14252 || c
->attr
.allocatable
14253 || c
->attr
.proc_pointer_comp
14254 || c
->attr
.class_pointer
14255 || c
->attr
.proc_pointer
)
14258 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
14259 || (c
->ts
.u
.derived
->f2k_derived
14260 && c
->ts
.u
.derived
->f2k_derived
->tb_op
[INTRINSIC_ASSIGN
]))
14262 derived
->attr
.defined_assign_comp
= 1;
14266 check_defined_assignments (c
->ts
.u
.derived
);
14267 if (c
->ts
.u
.derived
->attr
.defined_assign_comp
)
14269 derived
->attr
.defined_assign_comp
= 1;
14276 /* Resolve a single component of a derived type or structure. */
14279 resolve_component (gfc_component
*c
, gfc_symbol
*sym
)
14281 gfc_symbol
*super_type
;
14282 symbol_attribute
*attr
;
14284 if (c
->attr
.artificial
)
14287 /* Do not allow vtype components to be resolved in nameless namespaces
14288 such as block data because the procedure pointers will cause ICEs
14289 and vtables are not needed in these contexts. */
14290 if (sym
->attr
.vtype
&& sym
->attr
.use_assoc
14291 && sym
->ns
->proc_name
== NULL
)
14295 if ((!sym
->attr
.is_class
|| c
!= sym
->components
)
14296 && c
->attr
.codimension
14297 && (!c
->attr
.allocatable
|| (c
->as
&& c
->as
->type
!= AS_DEFERRED
)))
14299 gfc_error ("Coarray component %qs at %L must be allocatable with "
14300 "deferred shape", c
->name
, &c
->loc
);
14305 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
14306 && c
->ts
.u
.derived
->ts
.is_iso_c
)
14308 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14309 "shall not be a coarray", c
->name
, &c
->loc
);
14314 if (gfc_bt_struct (c
->ts
.type
) && c
->ts
.u
.derived
->attr
.coarray_comp
14315 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
14316 || c
->attr
.allocatable
))
14318 gfc_error ("Component %qs at %L with coarray component "
14319 "shall be a nonpointer, nonallocatable scalar",
14325 if (c
->ts
.type
== BT_CLASS
)
14327 if (CLASS_DATA (c
))
14329 attr
= &(CLASS_DATA (c
)->attr
);
14331 /* Fix up contiguous attribute. */
14332 if (c
->attr
.contiguous
)
14333 attr
->contiguous
= 1;
14341 if (attr
&& attr
->contiguous
&& (!attr
->dimension
|| !attr
->pointer
))
14343 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14344 "is not an array pointer", c
->name
, &c
->loc
);
14348 /* F2003, 15.2.1 - length has to be one. */
14349 if (sym
->attr
.is_bind_c
&& c
->ts
.type
== BT_CHARACTER
14350 && (c
->ts
.u
.cl
== NULL
|| c
->ts
.u
.cl
->length
== NULL
14351 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
)
14352 || mpz_cmp_si (c
->ts
.u
.cl
->length
->value
.integer
, 1) != 0))
14354 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14359 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
14361 gfc_symbol
*ifc
= c
->ts
.interface
;
14363 if (!sym
->attr
.vtype
&& !check_proc_interface (ifc
, &c
->loc
))
14369 if (ifc
->attr
.if_source
|| ifc
->attr
.intrinsic
)
14371 /* Resolve interface and copy attributes. */
14372 if (ifc
->formal
&& !ifc
->formal_ns
)
14373 resolve_symbol (ifc
);
14374 if (ifc
->attr
.intrinsic
)
14375 gfc_resolve_intrinsic (ifc
, &ifc
->declared_at
);
14379 c
->ts
= ifc
->result
->ts
;
14380 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
14381 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
14382 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
14383 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
14384 c
->attr
.class_ok
= ifc
->result
->attr
.class_ok
;
14389 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
14390 c
->attr
.pointer
= ifc
->attr
.pointer
;
14391 c
->attr
.dimension
= ifc
->attr
.dimension
;
14392 c
->as
= gfc_copy_array_spec (ifc
->as
);
14393 c
->attr
.class_ok
= ifc
->attr
.class_ok
;
14395 c
->ts
.interface
= ifc
;
14396 c
->attr
.function
= ifc
->attr
.function
;
14397 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
14399 c
->attr
.pure
= ifc
->attr
.pure
;
14400 c
->attr
.elemental
= ifc
->attr
.elemental
;
14401 c
->attr
.recursive
= ifc
->attr
.recursive
;
14402 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
14403 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
14404 /* Copy char length. */
14405 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
14407 gfc_charlen
*cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
14408 if (cl
->length
&& !cl
->resolved
14409 && !gfc_resolve_expr (cl
->length
))
14418 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
14420 /* Since PPCs are not implicitly typed, a PPC without an explicit
14421 interface must be a subroutine. */
14422 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
14425 /* Procedure pointer components: Check PASS arg. */
14426 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0
14427 && !sym
->attr
.vtype
)
14429 gfc_symbol
* me_arg
;
14431 if (c
->tb
->pass_arg
)
14433 gfc_formal_arglist
* i
;
14435 /* If an explicit passing argument name is given, walk the arg-list
14436 and look for it. */
14439 c
->tb
->pass_arg_num
= 1;
14440 for (i
= c
->ts
.interface
->formal
; i
; i
= i
->next
)
14442 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
14447 c
->tb
->pass_arg_num
++;
14452 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14453 "at %L has no argument %qs", c
->name
,
14454 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
14461 /* Otherwise, take the first one; there should in fact be at least
14463 c
->tb
->pass_arg_num
= 1;
14464 if (!c
->ts
.interface
->formal
)
14466 gfc_error ("Procedure pointer component %qs with PASS at %L "
14467 "must have at least one argument",
14472 me_arg
= c
->ts
.interface
->formal
->sym
;
14475 /* Now check that the argument-type matches. */
14476 gcc_assert (me_arg
);
14477 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
14478 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
14479 || (me_arg
->ts
.type
== BT_CLASS
14480 && CLASS_DATA (me_arg
)->ts
.u
.derived
!= sym
))
14482 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14483 " the derived type %qs", me_arg
->name
, c
->name
,
14484 me_arg
->name
, &c
->loc
, sym
->name
);
14489 /* Check for F03:C453. */
14490 if (CLASS_DATA (me_arg
)->attr
.dimension
)
14492 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14493 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
14499 if (CLASS_DATA (me_arg
)->attr
.class_pointer
)
14501 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14502 "may not have the POINTER attribute", me_arg
->name
,
14503 c
->name
, me_arg
->name
, &c
->loc
);
14508 if (CLASS_DATA (me_arg
)->attr
.allocatable
)
14510 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14511 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
14512 me_arg
->name
, &c
->loc
);
14517 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
14519 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14520 " at %L", c
->name
, &c
->loc
);
14526 /* Check type-spec if this is not the parent-type component. */
14527 if (((sym
->attr
.is_class
14528 && (!sym
->components
->ts
.u
.derived
->attr
.extension
14529 || c
!= sym
->components
->ts
.u
.derived
->components
))
14530 || (!sym
->attr
.is_class
14531 && (!sym
->attr
.extension
|| c
!= sym
->components
)))
14532 && !sym
->attr
.vtype
14533 && !resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
))
14536 super_type
= gfc_get_derived_super_type (sym
);
14538 /* If this type is an extension, set the accessibility of the parent
14541 && ((sym
->attr
.is_class
14542 && c
== sym
->components
->ts
.u
.derived
->components
)
14543 || (!sym
->attr
.is_class
&& c
== sym
->components
))
14544 && strcmp (super_type
->name
, c
->name
) == 0)
14545 c
->attr
.access
= super_type
->attr
.access
;
14547 /* If this type is an extension, see if this component has the same name
14548 as an inherited type-bound procedure. */
14549 if (super_type
&& !sym
->attr
.is_class
14550 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
14552 gfc_error ("Component %qs of %qs at %L has the same name as an"
14553 " inherited type-bound procedure",
14554 c
->name
, sym
->name
, &c
->loc
);
14558 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
14559 && !c
->ts
.deferred
)
14561 if (c
->ts
.u
.cl
->length
== NULL
14562 || (!resolve_charlen(c
->ts
.u
.cl
))
14563 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
14565 gfc_error ("Character length of component %qs needs to "
14566 "be a constant specification expression at %L",
14568 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
14573 if (c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
14574 && !c
->attr
.pointer
&& !c
->attr
.allocatable
)
14576 gfc_error ("Character component %qs of %qs at %L with deferred "
14577 "length must be a POINTER or ALLOCATABLE",
14578 c
->name
, sym
->name
, &c
->loc
);
14582 /* Add the hidden deferred length field. */
14583 if (c
->ts
.type
== BT_CHARACTER
14584 && (c
->ts
.deferred
|| c
->attr
.pdt_string
)
14585 && !c
->attr
.function
14586 && !sym
->attr
.is_class
)
14588 char name
[GFC_MAX_SYMBOL_LEN
+9];
14589 gfc_component
*strlen
;
14590 sprintf (name
, "_%s_length", c
->name
);
14591 strlen
= gfc_find_component (sym
, name
, true, true, NULL
);
14592 if (strlen
== NULL
)
14594 if (!gfc_add_component (sym
, name
, &strlen
))
14596 strlen
->ts
.type
= BT_INTEGER
;
14597 strlen
->ts
.kind
= gfc_charlen_int_kind
;
14598 strlen
->attr
.access
= ACCESS_PRIVATE
;
14599 strlen
->attr
.artificial
= 1;
14603 if (c
->ts
.type
== BT_DERIVED
14604 && sym
->component_access
!= ACCESS_PRIVATE
14605 && gfc_check_symbol_access (sym
)
14606 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
14607 && !c
->ts
.u
.derived
->attr
.use_assoc
14608 && !gfc_check_symbol_access (c
->ts
.u
.derived
)
14609 && !gfc_notify_std (GFC_STD_F2003
, "the component %qs is a "
14610 "PRIVATE type and cannot be a component of "
14611 "%qs, which is PUBLIC at %L", c
->name
,
14612 sym
->name
, &sym
->declared_at
))
14615 if ((sym
->attr
.sequence
|| sym
->attr
.is_bind_c
) && c
->ts
.type
== BT_CLASS
)
14617 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14618 "type %s", c
->name
, &c
->loc
, sym
->name
);
14622 if (sym
->attr
.sequence
)
14624 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
14626 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14627 "not have the SEQUENCE attribute",
14628 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
14633 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.generic
)
14634 c
->ts
.u
.derived
= gfc_find_dt_in_generic (c
->ts
.u
.derived
);
14635 else if (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
14636 && CLASS_DATA (c
)->ts
.u
.derived
->attr
.generic
)
14637 CLASS_DATA (c
)->ts
.u
.derived
14638 = gfc_find_dt_in_generic (CLASS_DATA (c
)->ts
.u
.derived
);
14640 /* If an allocatable component derived type is of the same type as
14641 the enclosing derived type, we need a vtable generating so that
14642 the __deallocate procedure is created. */
14643 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
14644 && c
->ts
.u
.derived
== sym
&& c
->attr
.allocatable
== 1)
14645 gfc_find_vtab (&c
->ts
);
14647 /* Ensure that all the derived type components are put on the
14648 derived type list; even in formal namespaces, where derived type
14649 pointer components might not have been declared. */
14650 if (c
->ts
.type
== BT_DERIVED
14652 && c
->ts
.u
.derived
->components
14654 && sym
!= c
->ts
.u
.derived
)
14655 add_dt_to_dt_list (c
->ts
.u
.derived
);
14657 if (!gfc_resolve_array_spec (c
->as
,
14658 !(c
->attr
.pointer
|| c
->attr
.proc_pointer
14659 || c
->attr
.allocatable
)))
14662 if (c
->initializer
&& !sym
->attr
.vtype
14663 && !c
->attr
.pdt_kind
&& !c
->attr
.pdt_len
14664 && !gfc_check_assign_symbol (sym
, c
, c
->initializer
))
14671 /* Be nice about the locus for a structure expression - show the locus of the
14672 first non-null sub-expression if we can. */
14675 cons_where (gfc_expr
*struct_expr
)
14677 gfc_constructor
*cons
;
14679 gcc_assert (struct_expr
&& struct_expr
->expr_type
== EXPR_STRUCTURE
);
14681 cons
= gfc_constructor_first (struct_expr
->value
.constructor
);
14682 for (; cons
; cons
= gfc_constructor_next (cons
))
14684 if (cons
->expr
&& cons
->expr
->expr_type
!= EXPR_NULL
)
14685 return &cons
->expr
->where
;
14688 return &struct_expr
->where
;
14691 /* Resolve the components of a structure type. Much less work than derived
14695 resolve_fl_struct (gfc_symbol
*sym
)
14698 gfc_expr
*init
= NULL
;
14701 /* Make sure UNIONs do not have overlapping initializers. */
14702 if (sym
->attr
.flavor
== FL_UNION
)
14704 for (c
= sym
->components
; c
; c
= c
->next
)
14706 if (init
&& c
->initializer
)
14708 gfc_error ("Conflicting initializers in union at %L and %L",
14709 cons_where (init
), cons_where (c
->initializer
));
14710 gfc_free_expr (c
->initializer
);
14711 c
->initializer
= NULL
;
14714 init
= c
->initializer
;
14719 for (c
= sym
->components
; c
; c
= c
->next
)
14720 if (!resolve_component (c
, sym
))
14726 if (sym
->components
)
14727 add_dt_to_dt_list (sym
);
14733 /* Resolve the components of a derived type. This does not have to wait until
14734 resolution stage, but can be done as soon as the dt declaration has been
14738 resolve_fl_derived0 (gfc_symbol
*sym
)
14740 gfc_symbol
* super_type
;
14742 gfc_formal_arglist
*f
;
14745 if (sym
->attr
.unlimited_polymorphic
)
14748 super_type
= gfc_get_derived_super_type (sym
);
14751 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
14753 gfc_error ("As extending type %qs at %L has a coarray component, "
14754 "parent type %qs shall also have one", sym
->name
,
14755 &sym
->declared_at
, super_type
->name
);
14759 /* Ensure the extended type gets resolved before we do. */
14760 if (super_type
&& !resolve_fl_derived0 (super_type
))
14763 /* An ABSTRACT type must be extensible. */
14764 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
14766 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14767 sym
->name
, &sym
->declared_at
);
14771 c
= (sym
->attr
.is_class
) ? sym
->components
->ts
.u
.derived
->components
14775 for ( ; c
!= NULL
; c
= c
->next
)
14776 if (!resolve_component (c
, sym
))
14782 /* Now add the caf token field, where needed. */
14783 if (flag_coarray
!= GFC_FCOARRAY_NONE
14784 && !sym
->attr
.is_class
&& !sym
->attr
.vtype
)
14786 for (c
= sym
->components
; c
; c
= c
->next
)
14787 if (!c
->attr
.dimension
&& !c
->attr
.codimension
14788 && (c
->attr
.allocatable
|| c
->attr
.pointer
))
14790 char name
[GFC_MAX_SYMBOL_LEN
+9];
14791 gfc_component
*token
;
14792 sprintf (name
, "_caf_%s", c
->name
);
14793 token
= gfc_find_component (sym
, name
, true, true, NULL
);
14796 if (!gfc_add_component (sym
, name
, &token
))
14798 token
->ts
.type
= BT_VOID
;
14799 token
->ts
.kind
= gfc_default_integer_kind
;
14800 token
->attr
.access
= ACCESS_PRIVATE
;
14801 token
->attr
.artificial
= 1;
14802 token
->attr
.caf_token
= 1;
14807 check_defined_assignments (sym
);
14809 if (!sym
->attr
.defined_assign_comp
&& super_type
)
14810 sym
->attr
.defined_assign_comp
14811 = super_type
->attr
.defined_assign_comp
;
14813 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14814 all DEFERRED bindings are overridden. */
14815 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
14816 && !sym
->attr
.is_class
14817 && !ensure_not_abstract (sym
, super_type
))
14820 /* Check that there is a component for every PDT parameter. */
14821 if (sym
->attr
.pdt_template
)
14823 for (f
= sym
->formal
; f
; f
= f
->next
)
14827 c
= gfc_find_component (sym
, f
->sym
->name
, true, true, NULL
);
14830 gfc_error ("Parameterized type %qs does not have a component "
14831 "corresponding to parameter %qs at %L", sym
->name
,
14832 f
->sym
->name
, &sym
->declared_at
);
14838 /* Add derived type to the derived type list. */
14839 add_dt_to_dt_list (sym
);
14845 /* The following procedure does the full resolution of a derived type,
14846 including resolution of all type-bound procedures (if present). In contrast
14847 to 'resolve_fl_derived0' this can only be done after the module has been
14848 parsed completely. */
14851 resolve_fl_derived (gfc_symbol
*sym
)
14853 gfc_symbol
*gen_dt
= NULL
;
14855 if (sym
->attr
.unlimited_polymorphic
)
14858 if (!sym
->attr
.is_class
)
14859 gfc_find_symbol (sym
->name
, sym
->ns
, 0, &gen_dt
);
14860 if (gen_dt
&& gen_dt
->generic
&& gen_dt
->generic
->next
14861 && (!gen_dt
->generic
->sym
->attr
.use_assoc
14862 || gen_dt
->generic
->sym
->module
!= gen_dt
->generic
->next
->sym
->module
)
14863 && !gfc_notify_std (GFC_STD_F2003
, "Generic name %qs of function "
14864 "%qs at %L being the same name as derived "
14865 "type at %L", sym
->name
,
14866 gen_dt
->generic
->sym
== sym
14867 ? gen_dt
->generic
->next
->sym
->name
14868 : gen_dt
->generic
->sym
->name
,
14869 gen_dt
->generic
->sym
== sym
14870 ? &gen_dt
->generic
->next
->sym
->declared_at
14871 : &gen_dt
->generic
->sym
->declared_at
,
14872 &sym
->declared_at
))
14875 if (sym
->components
== NULL
&& !sym
->attr
.zero_comp
&& !sym
->attr
.use_assoc
)
14877 gfc_error ("Derived type %qs at %L has not been declared",
14878 sym
->name
, &sym
->declared_at
);
14882 /* Resolve the finalizer procedures. */
14883 if (!gfc_resolve_finalizers (sym
, NULL
))
14886 if (sym
->attr
.is_class
&& sym
->ts
.u
.derived
== NULL
)
14888 /* Fix up incomplete CLASS symbols. */
14889 gfc_component
*data
= gfc_find_component (sym
, "_data", true, true, NULL
);
14890 gfc_component
*vptr
= gfc_find_component (sym
, "_vptr", true, true, NULL
);
14892 /* Nothing more to do for unlimited polymorphic entities. */
14893 if (data
->ts
.u
.derived
->attr
.unlimited_polymorphic
)
14895 else if (vptr
->ts
.u
.derived
== NULL
)
14897 gfc_symbol
*vtab
= gfc_find_derived_vtab (data
->ts
.u
.derived
);
14899 vptr
->ts
.u
.derived
= vtab
->ts
.u
.derived
;
14900 if (!resolve_fl_derived0 (vptr
->ts
.u
.derived
))
14905 if (!resolve_fl_derived0 (sym
))
14908 /* Resolve the type-bound procedures. */
14909 if (!resolve_typebound_procedures (sym
))
14912 /* Generate module vtables subject to their accessibility and their not
14913 being vtables or pdt templates. If this is not done class declarations
14914 in external procedures wind up with their own version and so SELECT TYPE
14915 fails because the vptrs do not have the same address. */
14916 if (gfc_option
.allow_std
& GFC_STD_F2003
14917 && sym
->ns
->proc_name
14918 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
14919 && sym
->attr
.access
!= ACCESS_PRIVATE
14920 && !(sym
->attr
.use_assoc
|| sym
->attr
.vtype
|| sym
->attr
.pdt_template
))
14922 gfc_symbol
*vtab
= gfc_find_derived_vtab (sym
);
14923 gfc_set_sym_referenced (vtab
);
14931 resolve_fl_namelist (gfc_symbol
*sym
)
14936 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
14938 /* Check again, the check in match only works if NAMELIST comes
14940 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SIZE
)
14942 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14943 "allowed", nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
14947 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
14948 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object %qs "
14949 "with assumed shape in namelist %qs at %L",
14950 nl
->sym
->name
, sym
->name
, &sym
->declared_at
))
14953 if (is_non_constant_shape_array (nl
->sym
)
14954 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object %qs "
14955 "with nonconstant shape in namelist %qs at %L",
14956 nl
->sym
->name
, sym
->name
, &sym
->declared_at
))
14959 if (nl
->sym
->ts
.type
== BT_CHARACTER
14960 && (nl
->sym
->ts
.u
.cl
->length
== NULL
14961 || !gfc_is_constant_expr (nl
->sym
->ts
.u
.cl
->length
))
14962 && !gfc_notify_std (GFC_STD_F2003
, "NAMELIST object %qs with "
14963 "nonconstant character length in "
14964 "namelist %qs at %L", nl
->sym
->name
,
14965 sym
->name
, &sym
->declared_at
))
14970 /* Reject PRIVATE objects in a PUBLIC namelist. */
14971 if (gfc_check_symbol_access (sym
))
14973 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
14975 if (!nl
->sym
->attr
.use_assoc
14976 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
14977 && !gfc_check_symbol_access (nl
->sym
))
14979 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14980 "cannot be member of PUBLIC namelist %qs at %L",
14981 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
14985 if (nl
->sym
->ts
.type
== BT_DERIVED
14986 && (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
14987 || nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
))
14989 if (!gfc_notify_std (GFC_STD_F2003
, "NAMELIST object %qs in "
14990 "namelist %qs at %L with ALLOCATABLE "
14991 "or POINTER components", nl
->sym
->name
,
14992 sym
->name
, &sym
->declared_at
))
14997 /* Types with private components that came here by USE-association. */
14998 if (nl
->sym
->ts
.type
== BT_DERIVED
14999 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
15001 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15002 "components and cannot be member of namelist %qs at %L",
15003 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
15007 /* Types with private components that are defined in the same module. */
15008 if (nl
->sym
->ts
.type
== BT_DERIVED
15009 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
15010 && nl
->sym
->ts
.u
.derived
->attr
.private_comp
)
15012 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15013 "cannot be a member of PUBLIC namelist %qs at %L",
15014 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
15021 /* 14.1.2 A module or internal procedure represent local entities
15022 of the same type as a namelist member and so are not allowed. */
15023 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
15025 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
15028 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
15029 if ((nl
->sym
== sym
->ns
->proc_name
)
15031 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
15036 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
15037 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
15039 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15040 "attribute in %qs at %L", nlsym
->name
,
15041 &sym
->declared_at
);
15051 resolve_fl_parameter (gfc_symbol
*sym
)
15053 /* A parameter array's shape needs to be constant. */
15054 if (sym
->as
!= NULL
15055 && (sym
->as
->type
== AS_DEFERRED
15056 || is_non_constant_shape_array (sym
)))
15058 gfc_error ("Parameter array %qs at %L cannot be automatic "
15059 "or of deferred shape", sym
->name
, &sym
->declared_at
);
15063 /* Constraints on deferred type parameter. */
15064 if (!deferred_requirements (sym
))
15067 /* Make sure a parameter that has been implicitly typed still
15068 matches the implicit type, since PARAMETER statements can precede
15069 IMPLICIT statements. */
15070 if (sym
->attr
.implicit_type
15071 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
15074 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15075 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
15079 /* Make sure the types of derived parameters are consistent. This
15080 type checking is deferred until resolution because the type may
15081 refer to a derived type from the host. */
15082 if (sym
->ts
.type
== BT_DERIVED
15083 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
15085 gfc_error ("Incompatible derived type in PARAMETER at %L",
15086 &sym
->value
->where
);
15090 /* F03:C509,C514. */
15091 if (sym
->ts
.type
== BT_CLASS
)
15093 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15094 sym
->name
, &sym
->declared_at
);
15102 /* Called by resolve_symbol to check PDTs. */
15105 resolve_pdt (gfc_symbol
* sym
)
15107 gfc_symbol
*derived
= NULL
;
15108 gfc_actual_arglist
*param
;
15110 bool const_len_exprs
= true;
15111 bool assumed_len_exprs
= false;
15112 symbol_attribute
*attr
;
15114 if (sym
->ts
.type
== BT_DERIVED
)
15116 derived
= sym
->ts
.u
.derived
;
15117 attr
= &(sym
->attr
);
15119 else if (sym
->ts
.type
== BT_CLASS
)
15121 derived
= CLASS_DATA (sym
)->ts
.u
.derived
;
15122 attr
= &(CLASS_DATA (sym
)->attr
);
15125 gcc_unreachable ();
15127 gcc_assert (derived
->attr
.pdt_type
);
15129 for (param
= sym
->param_list
; param
; param
= param
->next
)
15131 c
= gfc_find_component (derived
, param
->name
, false, true, NULL
);
15133 if (c
->attr
.pdt_kind
)
15136 if (param
->expr
&& !gfc_is_constant_expr (param
->expr
)
15137 && c
->attr
.pdt_len
)
15138 const_len_exprs
= false;
15139 else if (param
->spec_type
== SPEC_ASSUMED
)
15140 assumed_len_exprs
= true;
15142 if (param
->spec_type
== SPEC_DEFERRED
15143 && !attr
->allocatable
&& !attr
->pointer
)
15144 gfc_error ("The object %qs at %L has a deferred LEN "
15145 "parameter %qs and is neither allocatable "
15146 "nor a pointer", sym
->name
, &sym
->declared_at
,
15151 if (!const_len_exprs
15152 && (sym
->ns
->proc_name
->attr
.is_main_program
15153 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15154 || sym
->attr
.save
!= SAVE_NONE
))
15155 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15156 "SAVE attribute or be a variable declared in the "
15157 "main program, a module or a submodule(F08/C513)",
15158 sym
->name
, &sym
->declared_at
);
15160 if (assumed_len_exprs
&& !(sym
->attr
.dummy
15161 || sym
->attr
.select_type_temporary
|| sym
->attr
.associate_var
))
15162 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15163 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15164 sym
->name
, &sym
->declared_at
);
15168 /* Do anything necessary to resolve a symbol. Right now, we just
15169 assume that an otherwise unknown symbol is a variable. This sort
15170 of thing commonly happens for symbols in module. */
15173 resolve_symbol (gfc_symbol
*sym
)
15175 int check_constant
, mp_flag
;
15176 gfc_symtree
*symtree
;
15177 gfc_symtree
*this_symtree
;
15180 symbol_attribute class_attr
;
15181 gfc_array_spec
*as
;
15182 bool saved_specification_expr
;
15184 if (sym
->resolve_symbol_called
>= 1)
15186 sym
->resolve_symbol_called
= 1;
15188 /* No symbol will ever have union type; only components can be unions.
15189 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15190 (just like derived type declaration symbols have flavor FL_DERIVED). */
15191 gcc_assert (sym
->ts
.type
!= BT_UNION
);
15193 /* Coarrayed polymorphic objects with allocatable or pointer components are
15194 yet unsupported for -fcoarray=lib. */
15195 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->ts
.type
== BT_CLASS
15196 && sym
->ts
.u
.derived
&& CLASS_DATA (sym
)
15197 && CLASS_DATA (sym
)->attr
.codimension
15198 && CLASS_DATA (sym
)->ts
.u
.derived
15199 && (CLASS_DATA (sym
)->ts
.u
.derived
->attr
.alloc_comp
15200 || CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pointer_comp
))
15202 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15203 "type coarrays at %L are unsupported", &sym
->declared_at
);
15207 if (sym
->attr
.artificial
)
15210 if (sym
->attr
.unlimited_polymorphic
)
15213 if (sym
->attr
.flavor
== FL_UNKNOWN
15214 || (sym
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.intrinsic
15215 && !sym
->attr
.generic
&& !sym
->attr
.external
15216 && sym
->attr
.if_source
== IFSRC_UNKNOWN
15217 && sym
->ts
.type
== BT_UNKNOWN
))
15220 /* If we find that a flavorless symbol is an interface in one of the
15221 parent namespaces, find its symtree in this namespace, free the
15222 symbol and set the symtree to point to the interface symbol. */
15223 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
15225 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
15226 if (symtree
&& (symtree
->n
.sym
->generic
||
15227 (symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
15228 && sym
->ns
->construct_entities
)))
15230 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
15232 if (this_symtree
->n
.sym
== sym
)
15234 symtree
->n
.sym
->refs
++;
15235 gfc_release_symbol (sym
);
15236 this_symtree
->n
.sym
= symtree
->n
.sym
;
15242 /* Otherwise give it a flavor according to such attributes as
15244 if (sym
->attr
.flavor
== FL_UNKNOWN
&& sym
->attr
.external
== 0
15245 && sym
->attr
.intrinsic
== 0)
15246 sym
->attr
.flavor
= FL_VARIABLE
;
15247 else if (sym
->attr
.flavor
== FL_UNKNOWN
)
15249 sym
->attr
.flavor
= FL_PROCEDURE
;
15250 if (sym
->attr
.dimension
)
15251 sym
->attr
.function
= 1;
15255 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
15256 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
15258 if (sym
->attr
.procedure
&& sym
->attr
.if_source
!= IFSRC_DECL
15259 && !resolve_procedure_interface (sym
))
15262 if (sym
->attr
.is_protected
&& !sym
->attr
.proc_pointer
15263 && (sym
->attr
.procedure
|| sym
->attr
.external
))
15265 if (sym
->attr
.external
)
15266 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15267 "at %L", &sym
->declared_at
);
15269 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15270 "at %L", &sym
->declared_at
);
15275 if (sym
->attr
.flavor
== FL_DERIVED
&& !resolve_fl_derived (sym
))
15278 else if ((sym
->attr
.flavor
== FL_STRUCT
|| sym
->attr
.flavor
== FL_UNION
)
15279 && !resolve_fl_struct (sym
))
15282 /* Symbols that are module procedures with results (functions) have
15283 the types and array specification copied for type checking in
15284 procedures that call them, as well as for saving to a module
15285 file. These symbols can't stand the scrutiny that their results
15287 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
15289 /* Make sure that the intrinsic is consistent with its internal
15290 representation. This needs to be done before assigning a default
15291 type to avoid spurious warnings. */
15292 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
15293 && !gfc_resolve_intrinsic (sym
, &sym
->declared_at
))
15296 /* Resolve associate names. */
15298 resolve_assoc_var (sym
, true);
15300 /* Assign default type to symbols that need one and don't have one. */
15301 if (sym
->ts
.type
== BT_UNKNOWN
)
15303 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
15305 gfc_set_default_type (sym
, 1, NULL
);
15308 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
15309 && !sym
->attr
.function
&& !sym
->attr
.subroutine
15310 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
15311 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
15313 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
15315 /* The specific case of an external procedure should emit an error
15316 in the case that there is no implicit type. */
15319 if (!sym
->attr
.mixed_entry_master
)
15320 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
15324 /* Result may be in another namespace. */
15325 resolve_symbol (sym
->result
);
15327 if (!sym
->result
->attr
.proc_pointer
)
15329 sym
->ts
= sym
->result
->ts
;
15330 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
15331 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
15332 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
15333 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
15334 sym
->attr
.contiguous
= sym
->result
->attr
.contiguous
;
15339 else if (mp_flag
&& sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
15341 bool saved_specification_expr
= specification_expr
;
15342 specification_expr
= true;
15343 gfc_resolve_array_spec (sym
->result
->as
, false);
15344 specification_expr
= saved_specification_expr
;
15347 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
15349 as
= CLASS_DATA (sym
)->as
;
15350 class_attr
= CLASS_DATA (sym
)->attr
;
15351 class_attr
.pointer
= class_attr
.class_pointer
;
15355 class_attr
= sym
->attr
;
15360 if (sym
->attr
.contiguous
15361 && (!class_attr
.dimension
15362 || (as
->type
!= AS_ASSUMED_SHAPE
&& as
->type
!= AS_ASSUMED_RANK
15363 && !class_attr
.pointer
)))
15365 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15366 "array pointer or an assumed-shape or assumed-rank array",
15367 sym
->name
, &sym
->declared_at
);
15371 /* Assumed size arrays and assumed shape arrays must be dummy
15372 arguments. Array-spec's of implied-shape should have been resolved to
15373 AS_EXPLICIT already. */
15377 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15378 specification expression. */
15379 if (as
->type
== AS_IMPLIED_SHAPE
)
15382 for (i
=0; i
<as
->rank
; i
++)
15384 if (as
->lower
[i
] != NULL
&& as
->upper
[i
] == NULL
)
15386 gfc_error ("Bad specification for assumed size array at %L",
15387 &as
->lower
[i
]->where
);
15394 if (((as
->type
== AS_ASSUMED_SIZE
&& !as
->cp_was_assumed
)
15395 || as
->type
== AS_ASSUMED_SHAPE
)
15396 && !sym
->attr
.dummy
&& !sym
->attr
.select_type_temporary
)
15398 if (as
->type
== AS_ASSUMED_SIZE
)
15399 gfc_error ("Assumed size array at %L must be a dummy argument",
15400 &sym
->declared_at
);
15402 gfc_error ("Assumed shape array at %L must be a dummy argument",
15403 &sym
->declared_at
);
15406 /* TS 29113, C535a. */
15407 if (as
->type
== AS_ASSUMED_RANK
&& !sym
->attr
.dummy
15408 && !sym
->attr
.select_type_temporary
15409 && !(cs_base
&& cs_base
->current
15410 && cs_base
->current
->op
== EXEC_SELECT_RANK
))
15412 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15413 &sym
->declared_at
);
15416 if (as
->type
== AS_ASSUMED_RANK
15417 && (sym
->attr
.codimension
|| sym
->attr
.value
))
15419 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15420 "CODIMENSION attribute", &sym
->declared_at
);
15425 /* Make sure symbols with known intent or optional are really dummy
15426 variable. Because of ENTRY statement, this has to be deferred
15427 until resolution time. */
15429 if (!sym
->attr
.dummy
15430 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
15432 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
15436 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
15438 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15439 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
15443 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
15445 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
15446 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
15448 gfc_error ("Character dummy variable %qs at %L with VALUE "
15449 "attribute must have constant length",
15450 sym
->name
, &sym
->declared_at
);
15454 if (sym
->ts
.is_c_interop
15455 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
15457 gfc_error ("C interoperable character dummy variable %qs at %L "
15458 "with VALUE attribute must have length one",
15459 sym
->name
, &sym
->declared_at
);
15464 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->attr
.is_iso_c
15465 && sym
->ts
.u
.derived
->attr
.generic
)
15467 sym
->ts
.u
.derived
= gfc_find_dt_in_generic (sym
->ts
.u
.derived
);
15468 if (!sym
->ts
.u
.derived
)
15470 gfc_error ("The derived type %qs at %L is of type %qs, "
15471 "which has not been defined", sym
->name
,
15472 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
15473 sym
->ts
.type
= BT_UNKNOWN
;
15478 /* Use the same constraints as TYPE(*), except for the type check
15479 and that only scalars and assumed-size arrays are permitted. */
15480 if (sym
->attr
.ext_attr
& (1 << EXT_ATTR_NO_ARG_CHECK
))
15482 if (!sym
->attr
.dummy
)
15484 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15485 "a dummy argument", sym
->name
, &sym
->declared_at
);
15489 if (sym
->ts
.type
!= BT_ASSUMED
&& sym
->ts
.type
!= BT_INTEGER
15490 && sym
->ts
.type
!= BT_REAL
&& sym
->ts
.type
!= BT_LOGICAL
15491 && sym
->ts
.type
!= BT_COMPLEX
)
15493 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15494 "of type TYPE(*) or of an numeric intrinsic type",
15495 sym
->name
, &sym
->declared_at
);
15499 if (sym
->attr
.allocatable
|| sym
->attr
.codimension
15500 || sym
->attr
.pointer
|| sym
->attr
.value
)
15502 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15503 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15504 "attribute", sym
->name
, &sym
->declared_at
);
15508 if (sym
->attr
.intent
== INTENT_OUT
)
15510 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15511 "have the INTENT(OUT) attribute",
15512 sym
->name
, &sym
->declared_at
);
15515 if (sym
->attr
.dimension
&& sym
->as
->type
!= AS_ASSUMED_SIZE
)
15517 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15518 "either be a scalar or an assumed-size array",
15519 sym
->name
, &sym
->declared_at
);
15523 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15524 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15526 sym
->ts
.type
= BT_ASSUMED
;
15527 sym
->as
= gfc_get_array_spec ();
15528 sym
->as
->type
= AS_ASSUMED_SIZE
;
15530 sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
15532 else if (sym
->ts
.type
== BT_ASSUMED
)
15534 /* TS 29113, C407a. */
15535 if (!sym
->attr
.dummy
)
15537 gfc_error ("Assumed type of variable %s at %L is only permitted "
15538 "for dummy variables", sym
->name
, &sym
->declared_at
);
15541 if (sym
->attr
.allocatable
|| sym
->attr
.codimension
15542 || sym
->attr
.pointer
|| sym
->attr
.value
)
15544 gfc_error ("Assumed-type variable %s at %L may not have the "
15545 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15546 sym
->name
, &sym
->declared_at
);
15549 if (sym
->attr
.intent
== INTENT_OUT
)
15551 gfc_error ("Assumed-type variable %s at %L may not have the "
15552 "INTENT(OUT) attribute",
15553 sym
->name
, &sym
->declared_at
);
15556 if (sym
->attr
.dimension
&& sym
->as
->type
== AS_EXPLICIT
)
15558 gfc_error ("Assumed-type variable %s at %L shall not be an "
15559 "explicit-shape array", sym
->name
, &sym
->declared_at
);
15564 /* If the symbol is marked as bind(c), that it is declared at module level
15565 scope and verify its type and kind. Do not do the latter for symbols
15566 that are implicitly typed because that is handled in
15567 gfc_set_default_type. Handle dummy arguments and procedure definitions
15568 separately. Also, anything that is use associated is not handled here
15569 but instead is handled in the module it is declared in. Finally, derived
15570 type definitions are allowed to be BIND(C) since that only implies that
15571 they're interoperable, and they are checked fully for interoperability
15572 when a variable is declared of that type. */
15573 if (sym
->attr
.is_bind_c
&& sym
->attr
.use_assoc
== 0
15574 && sym
->attr
.dummy
== 0 && sym
->attr
.flavor
!= FL_PROCEDURE
15575 && sym
->attr
.flavor
!= FL_DERIVED
)
15579 /* First, make sure the variable is declared at the
15580 module-level scope (J3/04-007, Section 15.3). */
15581 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
15582 sym
->attr
.in_common
== 0)
15584 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15585 "is neither a COMMON block nor declared at the "
15586 "module level scope", sym
->name
, &(sym
->declared_at
));
15589 else if (sym
->ts
.type
== BT_CHARACTER
15590 && (sym
->ts
.u
.cl
== NULL
|| sym
->ts
.u
.cl
->length
== NULL
15591 || !gfc_is_constant_expr (sym
->ts
.u
.cl
->length
)
15592 || mpz_cmp_si (sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0))
15594 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15595 sym
->name
, &sym
->declared_at
);
15598 else if (sym
->common_head
!= NULL
&& sym
->attr
.implicit_type
== 0)
15600 t
= verify_com_block_vars_c_interop (sym
->common_head
);
15602 else if (sym
->attr
.implicit_type
== 0)
15604 /* If type() declaration, we need to verify that the components
15605 of the given type are all C interoperable, etc. */
15606 if (sym
->ts
.type
== BT_DERIVED
&&
15607 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
15609 /* Make sure the user marked the derived type as BIND(C). If
15610 not, call the verify routine. This could print an error
15611 for the derived type more than once if multiple variables
15612 of that type are declared. */
15613 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
15614 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
15618 /* Verify the variable itself as C interoperable if it
15619 is BIND(C). It is not possible for this to succeed if
15620 the verify_bind_c_derived_type failed, so don't have to handle
15621 any error returned by verify_bind_c_derived_type. */
15622 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
15623 sym
->common_block
);
15628 /* clear the is_bind_c flag to prevent reporting errors more than
15629 once if something failed. */
15630 sym
->attr
.is_bind_c
= 0;
15635 /* If a derived type symbol has reached this point, without its
15636 type being declared, we have an error. Notice that most
15637 conditions that produce undefined derived types have already
15638 been dealt with. However, the likes of:
15639 implicit type(t) (t) ..... call foo (t) will get us here if
15640 the type is not declared in the scope of the implicit
15641 statement. Change the type to BT_UNKNOWN, both because it is so
15642 and to prevent an ICE. */
15643 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->attr
.is_iso_c
15644 && sym
->ts
.u
.derived
->components
== NULL
15645 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
15647 gfc_error ("The derived type %qs at %L is of type %qs, "
15648 "which has not been defined", sym
->name
,
15649 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
15650 sym
->ts
.type
= BT_UNKNOWN
;
15654 /* Make sure that the derived type has been resolved and that the
15655 derived type is visible in the symbol's namespace, if it is a
15656 module function and is not PRIVATE. */
15657 if (sym
->ts
.type
== BT_DERIVED
15658 && sym
->ts
.u
.derived
->attr
.use_assoc
15659 && sym
->ns
->proc_name
15660 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15661 && !resolve_fl_derived (sym
->ts
.u
.derived
))
15664 /* Unless the derived-type declaration is use associated, Fortran 95
15665 does not allow public entries of private derived types.
15666 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15667 161 in 95-006r3. */
15668 if (sym
->ts
.type
== BT_DERIVED
15669 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15670 && !sym
->ts
.u
.derived
->attr
.use_assoc
15671 && gfc_check_symbol_access (sym
)
15672 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
15673 && !gfc_notify_std (GFC_STD_F2003
, "PUBLIC %s %qs at %L of PRIVATE "
15674 "derived type %qs",
15675 (sym
->attr
.flavor
== FL_PARAMETER
)
15676 ? "parameter" : "variable",
15677 sym
->name
, &sym
->declared_at
,
15678 sym
->ts
.u
.derived
->name
))
15681 /* F2008, C1302. */
15682 if (sym
->ts
.type
== BT_DERIVED
15683 && ((sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
15684 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
15685 || sym
->ts
.u
.derived
->attr
.lock_comp
)
15686 && !sym
->attr
.codimension
&& !sym
->ts
.u
.derived
->attr
.coarray_comp
)
15688 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15689 "type LOCK_TYPE must be a coarray", sym
->name
,
15690 &sym
->declared_at
);
15694 /* TS18508, C702/C703. */
15695 if (sym
->ts
.type
== BT_DERIVED
15696 && ((sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
15697 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
15698 || sym
->ts
.u
.derived
->attr
.event_comp
)
15699 && !sym
->attr
.codimension
&& !sym
->ts
.u
.derived
->attr
.coarray_comp
)
15701 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15702 "type EVENT_TYPE must be a coarray", sym
->name
,
15703 &sym
->declared_at
);
15707 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15708 default initialization is defined (5.1.2.4.4). */
15709 if (sym
->ts
.type
== BT_DERIVED
15711 && sym
->attr
.intent
== INTENT_OUT
15713 && sym
->as
->type
== AS_ASSUMED_SIZE
)
15715 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
15717 if (c
->initializer
)
15719 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15720 "ASSUMED SIZE and so cannot have a default initializer",
15721 sym
->name
, &sym
->declared_at
);
15728 if (sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.dummy
15729 && sym
->attr
.intent
== INTENT_OUT
&& sym
->attr
.lock_comp
)
15731 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15732 "INTENT(OUT)", sym
->name
, &sym
->declared_at
);
15737 if (sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.dummy
15738 && sym
->attr
.intent
== INTENT_OUT
&& sym
->attr
.event_comp
)
15740 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15741 "INTENT(OUT)", sym
->name
, &sym
->declared_at
);
15746 if ((((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15747 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15748 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15749 || class_attr
.codimension
)
15750 && (sym
->attr
.result
|| sym
->result
== sym
))
15752 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15753 "a coarray component", sym
->name
, &sym
->declared_at
);
15758 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
15759 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
15761 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15762 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
15767 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15768 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15769 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15770 && (class_attr
.codimension
|| class_attr
.pointer
|| class_attr
.dimension
15771 || class_attr
.allocatable
))
15773 gfc_error ("Variable %qs at %L with coarray component shall be a "
15774 "nonpointer, nonallocatable scalar, which is not a coarray",
15775 sym
->name
, &sym
->declared_at
);
15779 /* F2008, C526. The function-result case was handled above. */
15780 if (class_attr
.codimension
15781 && !(class_attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
15782 || sym
->attr
.select_type_temporary
15783 || sym
->attr
.associate_var
15784 || (sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15785 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
15786 || sym
->ns
->proc_name
->attr
.is_main_program
15787 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
15789 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15790 "nor a dummy argument", sym
->name
, &sym
->declared_at
);
15794 else if (class_attr
.codimension
&& !sym
->attr
.select_type_temporary
15795 && !class_attr
.allocatable
&& as
&& as
->cotype
== AS_DEFERRED
)
15797 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15798 "deferred shape", sym
->name
, &sym
->declared_at
);
15801 else if (class_attr
.codimension
&& class_attr
.allocatable
&& as
15802 && (as
->cotype
!= AS_DEFERRED
|| as
->type
!= AS_DEFERRED
))
15804 gfc_error ("Allocatable coarray variable %qs at %L must have "
15805 "deferred shape", sym
->name
, &sym
->declared_at
);
15810 if ((((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
15811 || (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
15812 && CLASS_DATA (sym
)->attr
.coarray_comp
))
15813 || (class_attr
.codimension
&& class_attr
.allocatable
))
15814 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
15816 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15817 "allocatable coarray or have coarray components",
15818 sym
->name
, &sym
->declared_at
);
15822 if (class_attr
.codimension
&& sym
->attr
.dummy
15823 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
15825 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15826 "procedure %qs", sym
->name
, &sym
->declared_at
,
15827 sym
->ns
->proc_name
->name
);
15831 if (sym
->ts
.type
== BT_LOGICAL
15832 && ((sym
->attr
.function
&& sym
->attr
.is_bind_c
&& sym
->result
== sym
)
15833 || ((sym
->attr
.dummy
|| sym
->attr
.result
) && sym
->ns
->proc_name
15834 && sym
->ns
->proc_name
->attr
.is_bind_c
)))
15837 for (i
= 0; gfc_logical_kinds
[i
].kind
; i
++)
15838 if (gfc_logical_kinds
[i
].kind
== sym
->ts
.kind
)
15840 if (!gfc_logical_kinds
[i
].c_bool
&& sym
->attr
.dummy
15841 && !gfc_notify_std (GFC_STD_GNU
, "LOGICAL dummy argument %qs at "
15842 "%L with non-C_Bool kind in BIND(C) procedure "
15843 "%qs", sym
->name
, &sym
->declared_at
,
15844 sym
->ns
->proc_name
->name
))
15846 else if (!gfc_logical_kinds
[i
].c_bool
15847 && !gfc_notify_std (GFC_STD_GNU
, "LOGICAL result variable "
15848 "%qs at %L with non-C_Bool kind in "
15849 "BIND(C) procedure %qs", sym
->name
,
15851 sym
->attr
.function
? sym
->name
15852 : sym
->ns
->proc_name
->name
))
15856 switch (sym
->attr
.flavor
)
15859 if (!resolve_fl_variable (sym
, mp_flag
))
15864 if (sym
->formal
&& !sym
->formal_ns
)
15866 /* Check that none of the arguments are a namelist. */
15867 gfc_formal_arglist
*formal
= sym
->formal
;
15869 for (; formal
; formal
= formal
->next
)
15870 if (formal
->sym
&& formal
->sym
->attr
.flavor
== FL_NAMELIST
)
15872 gfc_error ("Namelist %qs cannot be an argument to "
15873 "subroutine or function at %L",
15874 formal
->sym
->name
, &sym
->declared_at
);
15879 if (!resolve_fl_procedure (sym
, mp_flag
))
15884 if (!resolve_fl_namelist (sym
))
15889 if (!resolve_fl_parameter (sym
))
15897 /* Resolve array specifier. Check as well some constraints
15898 on COMMON blocks. */
15900 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
15902 /* Set the formal_arg_flag so that check_conflict will not throw
15903 an error for host associated variables in the specification
15904 expression for an array_valued function. */
15905 if ((sym
->attr
.function
|| sym
->attr
.result
) && sym
->as
)
15906 formal_arg_flag
= true;
15908 saved_specification_expr
= specification_expr
;
15909 specification_expr
= true;
15910 gfc_resolve_array_spec (sym
->as
, check_constant
);
15911 specification_expr
= saved_specification_expr
;
15913 formal_arg_flag
= false;
15915 /* Resolve formal namespaces. */
15916 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
15917 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
15918 gfc_resolve (sym
->formal_ns
);
15920 /* Make sure the formal namespace is present. */
15921 if (sym
->formal
&& !sym
->formal_ns
)
15923 gfc_formal_arglist
*formal
= sym
->formal
;
15924 while (formal
&& !formal
->sym
)
15925 formal
= formal
->next
;
15929 sym
->formal_ns
= formal
->sym
->ns
;
15930 if (sym
->formal_ns
&& sym
->ns
!= formal
->sym
->ns
)
15931 sym
->formal_ns
->refs
++;
15935 /* Check threadprivate restrictions. */
15936 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
15937 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15938 && (!sym
->attr
.in_common
15939 && sym
->module
== NULL
15940 && (sym
->ns
->proc_name
== NULL
15941 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
15942 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
15944 /* Check omp declare target restrictions. */
15945 if (sym
->attr
.omp_declare_target
15946 && sym
->attr
.flavor
== FL_VARIABLE
15948 && !(sym
->ns
->save_all
&& !sym
->attr
.automatic
)
15949 && (!sym
->attr
.in_common
15950 && sym
->module
== NULL
15951 && (sym
->ns
->proc_name
== NULL
15952 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
15953 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15954 sym
->name
, &sym
->declared_at
);
15956 /* If we have come this far we can apply default-initializers, as
15957 described in 14.7.5, to those variables that have not already
15958 been assigned one. */
15959 if (sym
->ts
.type
== BT_DERIVED
15961 && !sym
->attr
.allocatable
15962 && !sym
->attr
.alloc_comp
)
15964 symbol_attribute
*a
= &sym
->attr
;
15966 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
15967 && !a
->in_common
&& !a
->use_assoc
15969 && !((a
->function
|| a
->result
)
15971 || sym
->ts
.u
.derived
->attr
.alloc_comp
15972 || sym
->ts
.u
.derived
->attr
.pointer_comp
))
15973 && !(a
->function
&& sym
!= sym
->result
))
15974 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
15975 apply_default_init (sym
);
15976 else if (a
->function
&& sym
->result
&& a
->access
!= ACCESS_PRIVATE
15977 && (sym
->ts
.u
.derived
->attr
.alloc_comp
15978 || sym
->ts
.u
.derived
->attr
.pointer_comp
))
15979 /* Mark the result symbol to be referenced, when it has allocatable
15981 sym
->result
->attr
.referenced
= 1;
15984 if (sym
->ts
.type
== BT_CLASS
&& sym
->ns
== gfc_current_ns
15985 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
15986 && !CLASS_DATA (sym
)->attr
.class_pointer
15987 && !CLASS_DATA (sym
)->attr
.allocatable
)
15988 apply_default_init (sym
);
15990 /* If this symbol has a type-spec, check it. */
15991 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
15992 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
15993 if (!resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
))
15996 if (sym
->param_list
)
16001 /************* Resolve DATA statements *************/
16005 gfc_data_value
*vnode
;
16011 /* Advance the values structure to point to the next value in the data list. */
16014 next_data_value (void)
16016 while (mpz_cmp_ui (values
.left
, 0) == 0)
16019 if (values
.vnode
->next
== NULL
)
16022 values
.vnode
= values
.vnode
->next
;
16023 mpz_set (values
.left
, values
.vnode
->repeat
);
16031 check_data_variable (gfc_data_variable
*var
, locus
*where
)
16037 ar_type mark
= AR_UNKNOWN
;
16039 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
16045 if (!gfc_resolve_expr (var
->expr
))
16049 mpz_init_set_si (offset
, 0);
16052 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
16053 && e
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
16054 e
= e
->value
.function
.actual
->expr
;
16056 if (e
->expr_type
!= EXPR_VARIABLE
)
16058 gfc_error ("Expecting definable entity near %L", where
);
16062 sym
= e
->symtree
->n
.sym
;
16064 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
16066 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16067 sym
->name
, &sym
->declared_at
);
16071 if (e
->ref
== NULL
&& sym
->as
)
16073 gfc_error ("DATA array %qs at %L must be specified in a previous"
16074 " declaration", sym
->name
, where
);
16078 if (gfc_is_coindexed (e
))
16080 gfc_error ("DATA element %qs at %L cannot have a coindex", sym
->name
,
16085 has_pointer
= sym
->attr
.pointer
;
16087 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
16089 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
16094 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
!= AR_FULL
)
16096 gfc_error ("DATA element %qs at %L is a pointer and so must "
16097 "be a full array", sym
->name
, where
);
16101 if (values
.vnode
->expr
->expr_type
== EXPR_CONSTANT
)
16103 gfc_error ("DATA object near %L has the pointer attribute "
16104 "and the corresponding DATA value is not a valid "
16105 "initial-data-target", where
);
16111 if (e
->rank
== 0 || has_pointer
)
16113 mpz_init_set_ui (size
, 1);
16120 /* Find the array section reference. */
16121 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
16123 if (ref
->type
!= REF_ARRAY
)
16125 if (ref
->u
.ar
.type
== AR_ELEMENT
)
16131 /* Set marks according to the reference pattern. */
16132 switch (ref
->u
.ar
.type
)
16140 /* Get the start position of array section. */
16141 gfc_get_section_index (ar
, section_index
, &offset
);
16146 gcc_unreachable ();
16149 if (!gfc_array_size (e
, &size
))
16151 gfc_error ("Nonconstant array section at %L in DATA statement",
16153 mpz_clear (offset
);
16160 while (mpz_cmp_ui (size
, 0) > 0)
16162 if (!next_data_value ())
16164 gfc_error ("DATA statement at %L has more variables than values",
16170 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
16174 /* If we have more than one element left in the repeat count,
16175 and we have more than one element left in the target variable,
16176 then create a range assignment. */
16177 /* FIXME: Only done for full arrays for now, since array sections
16179 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
16180 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
16184 if (mpz_cmp (size
, values
.left
) >= 0)
16186 mpz_init_set (range
, values
.left
);
16187 mpz_sub (size
, size
, values
.left
);
16188 mpz_set_ui (values
.left
, 0);
16192 mpz_init_set (range
, size
);
16193 mpz_sub (values
.left
, values
.left
, size
);
16194 mpz_set_ui (size
, 0);
16197 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
16200 mpz_add (offset
, offset
, range
);
16207 /* Assign initial value to symbol. */
16210 mpz_sub_ui (values
.left
, values
.left
, 1);
16211 mpz_sub_ui (size
, size
, 1);
16213 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
,
16218 if (mark
== AR_FULL
)
16219 mpz_add_ui (offset
, offset
, 1);
16221 /* Modify the array section indexes and recalculate the offset
16222 for next element. */
16223 else if (mark
== AR_SECTION
)
16224 gfc_advance_section (section_index
, ar
, &offset
);
16228 if (mark
== AR_SECTION
)
16230 for (i
= 0; i
< ar
->dimen
; i
++)
16231 mpz_clear (section_index
[i
]);
16235 mpz_clear (offset
);
16241 static bool traverse_data_var (gfc_data_variable
*, locus
*);
16243 /* Iterate over a list of elements in a DATA statement. */
16246 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
16249 iterator_stack frame
;
16250 gfc_expr
*e
, *start
, *end
, *step
;
16251 bool retval
= true;
16253 mpz_init (frame
.value
);
16256 start
= gfc_copy_expr (var
->iter
.start
);
16257 end
= gfc_copy_expr (var
->iter
.end
);
16258 step
= gfc_copy_expr (var
->iter
.step
);
16260 if (!gfc_simplify_expr (start
, 1)
16261 || start
->expr_type
!= EXPR_CONSTANT
)
16263 gfc_error ("start of implied-do loop at %L could not be "
16264 "simplified to a constant value", &start
->where
);
16268 if (!gfc_simplify_expr (end
, 1)
16269 || end
->expr_type
!= EXPR_CONSTANT
)
16271 gfc_error ("end of implied-do loop at %L could not be "
16272 "simplified to a constant value", &start
->where
);
16276 if (!gfc_simplify_expr (step
, 1)
16277 || step
->expr_type
!= EXPR_CONSTANT
)
16279 gfc_error ("step of implied-do loop at %L could not be "
16280 "simplified to a constant value", &start
->where
);
16285 mpz_set (trip
, end
->value
.integer
);
16286 mpz_sub (trip
, trip
, start
->value
.integer
);
16287 mpz_add (trip
, trip
, step
->value
.integer
);
16289 mpz_div (trip
, trip
, step
->value
.integer
);
16291 mpz_set (frame
.value
, start
->value
.integer
);
16293 frame
.prev
= iter_stack
;
16294 frame
.variable
= var
->iter
.var
->symtree
;
16295 iter_stack
= &frame
;
16297 while (mpz_cmp_ui (trip
, 0) > 0)
16299 if (!traverse_data_var (var
->list
, where
))
16305 e
= gfc_copy_expr (var
->expr
);
16306 if (!gfc_simplify_expr (e
, 1))
16313 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
16315 mpz_sub_ui (trip
, trip
, 1);
16319 mpz_clear (frame
.value
);
16322 gfc_free_expr (start
);
16323 gfc_free_expr (end
);
16324 gfc_free_expr (step
);
16326 iter_stack
= frame
.prev
;
16331 /* Type resolve variables in the variable list of a DATA statement. */
16334 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
16338 for (; var
; var
= var
->next
)
16340 if (var
->expr
== NULL
)
16341 t
= traverse_data_list (var
, where
);
16343 t
= check_data_variable (var
, where
);
16353 /* Resolve the expressions and iterators associated with a data statement.
16354 This is separate from the assignment checking because data lists should
16355 only be resolved once. */
16358 resolve_data_variables (gfc_data_variable
*d
)
16360 for (; d
; d
= d
->next
)
16362 if (d
->list
== NULL
)
16364 if (!gfc_resolve_expr (d
->expr
))
16369 if (!gfc_resolve_iterator (&d
->iter
, false, true))
16372 if (!resolve_data_variables (d
->list
))
16381 /* Resolve a single DATA statement. We implement this by storing a pointer to
16382 the value list into static variables, and then recursively traversing the
16383 variables list, expanding iterators and such. */
16386 resolve_data (gfc_data
*d
)
16389 if (!resolve_data_variables (d
->var
))
16392 values
.vnode
= d
->value
;
16393 if (d
->value
== NULL
)
16394 mpz_set_ui (values
.left
, 0);
16396 mpz_set (values
.left
, d
->value
->repeat
);
16398 if (!traverse_data_var (d
->var
, &d
->where
))
16401 /* At this point, we better not have any values left. */
16403 if (next_data_value ())
16404 gfc_error ("DATA statement at %L has more values than variables",
16409 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16410 accessed by host or use association, is a dummy argument to a pure function,
16411 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16412 is storage associated with any such variable, shall not be used in the
16413 following contexts: (clients of this function). */
16415 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16416 procedure. Returns zero if assignment is OK, nonzero if there is a
16419 gfc_impure_variable (gfc_symbol
*sym
)
16424 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
16427 /* Check if the symbol's ns is inside the pure procedure. */
16428 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16432 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
16436 proc
= sym
->ns
->proc_name
;
16437 if (sym
->attr
.dummy
16438 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
16439 || proc
->attr
.function
))
16442 /* TODO: Sort out what can be storage associated, if anything, and include
16443 it here. In principle equivalences should be scanned but it does not
16444 seem to be possible to storage associate an impure variable this way. */
16449 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16450 current namespace is inside a pure procedure. */
16453 gfc_pure (gfc_symbol
*sym
)
16455 symbol_attribute attr
;
16460 /* Check if the current namespace or one of its parents
16461 belongs to a pure procedure. */
16462 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16464 sym
= ns
->proc_name
;
16468 if (attr
.flavor
== FL_PROCEDURE
&& attr
.pure
)
16476 return attr
.flavor
== FL_PROCEDURE
&& attr
.pure
;
16480 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16481 checks if the current namespace is implicitly pure. Note that this
16482 function returns false for a PURE procedure. */
16485 gfc_implicit_pure (gfc_symbol
*sym
)
16491 /* Check if the current procedure is implicit_pure. Walk up
16492 the procedure list until we find a procedure. */
16493 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16495 sym
= ns
->proc_name
;
16499 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16504 return sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.implicit_pure
16505 && !sym
->attr
.pure
;
16510 gfc_unset_implicit_pure (gfc_symbol
*sym
)
16516 /* Check if the current procedure is implicit_pure. Walk up
16517 the procedure list until we find a procedure. */
16518 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
16520 sym
= ns
->proc_name
;
16524 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16529 if (sym
->attr
.flavor
== FL_PROCEDURE
)
16530 sym
->attr
.implicit_pure
= 0;
16532 sym
->attr
.pure
= 0;
16536 /* Test whether the current procedure is elemental or not. */
16539 gfc_elemental (gfc_symbol
*sym
)
16541 symbol_attribute attr
;
16544 sym
= gfc_current_ns
->proc_name
;
16549 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
16553 /* Warn about unused labels. */
16556 warn_unused_fortran_label (gfc_st_label
*label
)
16561 warn_unused_fortran_label (label
->left
);
16563 if (label
->defined
== ST_LABEL_UNKNOWN
)
16566 switch (label
->referenced
)
16568 case ST_LABEL_UNKNOWN
:
16569 gfc_warning (OPT_Wunused_label
, "Label %d at %L defined but not used",
16570 label
->value
, &label
->where
);
16573 case ST_LABEL_BAD_TARGET
:
16574 gfc_warning (OPT_Wunused_label
,
16575 "Label %d at %L defined but cannot be used",
16576 label
->value
, &label
->where
);
16583 warn_unused_fortran_label (label
->right
);
16587 /* Returns the sequence type of a symbol or sequence. */
16590 sequence_type (gfc_typespec ts
)
16599 if (ts
.u
.derived
->components
== NULL
)
16600 return SEQ_NONDEFAULT
;
16602 result
= sequence_type (ts
.u
.derived
->components
->ts
);
16603 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
16604 if (sequence_type (c
->ts
) != result
)
16610 if (ts
.kind
!= gfc_default_character_kind
)
16611 return SEQ_NONDEFAULT
;
16613 return SEQ_CHARACTER
;
16616 if (ts
.kind
!= gfc_default_integer_kind
)
16617 return SEQ_NONDEFAULT
;
16619 return SEQ_NUMERIC
;
16622 if (!(ts
.kind
== gfc_default_real_kind
16623 || ts
.kind
== gfc_default_double_kind
))
16624 return SEQ_NONDEFAULT
;
16626 return SEQ_NUMERIC
;
16629 if (ts
.kind
!= gfc_default_complex_kind
)
16630 return SEQ_NONDEFAULT
;
16632 return SEQ_NUMERIC
;
16635 if (ts
.kind
!= gfc_default_logical_kind
)
16636 return SEQ_NONDEFAULT
;
16638 return SEQ_NUMERIC
;
16641 return SEQ_NONDEFAULT
;
16646 /* Resolve derived type EQUIVALENCE object. */
16649 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
16651 gfc_component
*c
= derived
->components
;
16656 /* Shall not be an object of nonsequence derived type. */
16657 if (!derived
->attr
.sequence
)
16659 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16660 "attribute to be an EQUIVALENCE object", sym
->name
,
16665 /* Shall not have allocatable components. */
16666 if (derived
->attr
.alloc_comp
)
16668 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16669 "components to be an EQUIVALENCE object",sym
->name
,
16674 if (sym
->attr
.in_common
&& gfc_has_default_initializer (sym
->ts
.u
.derived
))
16676 gfc_error ("Derived type variable %qs at %L with default "
16677 "initialization cannot be in EQUIVALENCE with a variable "
16678 "in COMMON", sym
->name
, &e
->where
);
16682 for (; c
; c
= c
->next
)
16684 if (gfc_bt_struct (c
->ts
.type
)
16685 && (!resolve_equivalence_derived(c
->ts
.u
.derived
, sym
, e
)))
16688 /* Shall not be an object of sequence derived type containing a pointer
16689 in the structure. */
16690 if (c
->attr
.pointer
)
16692 gfc_error ("Derived type variable %qs at %L with pointer "
16693 "component(s) cannot be an EQUIVALENCE object",
16694 sym
->name
, &e
->where
);
16702 /* Resolve equivalence object.
16703 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16704 an allocatable array, an object of nonsequence derived type, an object of
16705 sequence derived type containing a pointer at any level of component
16706 selection, an automatic object, a function name, an entry name, a result
16707 name, a named constant, a structure component, or a subobject of any of
16708 the preceding objects. A substring shall not have length zero. A
16709 derived type shall not have components with default initialization nor
16710 shall two objects of an equivalence group be initialized.
16711 Either all or none of the objects shall have an protected attribute.
16712 The simple constraints are done in symbol.c(check_conflict) and the rest
16713 are implemented here. */
16716 resolve_equivalence (gfc_equiv
*eq
)
16719 gfc_symbol
*first_sym
;
16722 locus
*last_where
= NULL
;
16723 seq_type eq_type
, last_eq_type
;
16724 gfc_typespec
*last_ts
;
16725 int object
, cnt_protected
;
16728 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
16730 first_sym
= eq
->expr
->symtree
->n
.sym
;
16734 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
16738 e
->ts
= e
->symtree
->n
.sym
->ts
;
16739 /* match_varspec might not know yet if it is seeing
16740 array reference or substring reference, as it doesn't
16742 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
16744 gfc_ref
*ref
= e
->ref
;
16745 sym
= e
->symtree
->n
.sym
;
16747 if (sym
->attr
.dimension
)
16749 ref
->u
.ar
.as
= sym
->as
;
16753 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16754 if (e
->ts
.type
== BT_CHARACTER
16756 && ref
->type
== REF_ARRAY
16757 && ref
->u
.ar
.dimen
== 1
16758 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
16759 && ref
->u
.ar
.stride
[0] == NULL
)
16761 gfc_expr
*start
= ref
->u
.ar
.start
[0];
16762 gfc_expr
*end
= ref
->u
.ar
.end
[0];
16765 /* Optimize away the (:) reference. */
16766 if (start
== NULL
&& end
== NULL
)
16769 e
->ref
= ref
->next
;
16771 e
->ref
->next
= ref
->next
;
16776 ref
->type
= REF_SUBSTRING
;
16778 start
= gfc_get_int_expr (gfc_charlen_int_kind
,
16780 ref
->u
.ss
.start
= start
;
16781 if (end
== NULL
&& e
->ts
.u
.cl
)
16782 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
16783 ref
->u
.ss
.end
= end
;
16784 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
16791 /* Any further ref is an error. */
16794 gcc_assert (ref
->type
== REF_ARRAY
);
16795 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16801 if (!gfc_resolve_expr (e
))
16804 sym
= e
->symtree
->n
.sym
;
16806 if (sym
->attr
.is_protected
)
16808 if (cnt_protected
> 0 && cnt_protected
!= object
)
16810 gfc_error ("Either all or none of the objects in the "
16811 "EQUIVALENCE set at %L shall have the "
16812 "PROTECTED attribute",
16817 /* Shall not equivalence common block variables in a PURE procedure. */
16818 if (sym
->ns
->proc_name
16819 && sym
->ns
->proc_name
->attr
.pure
16820 && sym
->attr
.in_common
)
16822 /* Need to check for symbols that may have entered the pure
16823 procedure via a USE statement. */
16824 bool saw_sym
= false;
16825 if (sym
->ns
->use_stmts
)
16828 for (r
= sym
->ns
->use_stmts
->rename
; r
; r
= r
->next
)
16829 if (strcmp(r
->use_name
, sym
->name
) == 0) saw_sym
= true;
16835 gfc_error ("COMMON block member %qs at %L cannot be an "
16836 "EQUIVALENCE object in the pure procedure %qs",
16837 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
16841 /* Shall not be a named constant. */
16842 if (e
->expr_type
== EXPR_CONSTANT
)
16844 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16845 "object", sym
->name
, &e
->where
);
16849 if (e
->ts
.type
== BT_DERIVED
16850 && !resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
))
16853 /* Check that the types correspond correctly:
16855 A numeric sequence structure may be equivalenced to another sequence
16856 structure, an object of default integer type, default real type, double
16857 precision real type, default logical type such that components of the
16858 structure ultimately only become associated to objects of the same
16859 kind. A character sequence structure may be equivalenced to an object
16860 of default character kind or another character sequence structure.
16861 Other objects may be equivalenced only to objects of the same type and
16862 kind parameters. */
16864 /* Identical types are unconditionally OK. */
16865 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
16866 goto identical_types
;
16868 last_eq_type
= sequence_type (*last_ts
);
16869 eq_type
= sequence_type (sym
->ts
);
16871 /* Since the pair of objects is not of the same type, mixed or
16872 non-default sequences can be rejected. */
16874 msg
= "Sequence %s with mixed components in EQUIVALENCE "
16875 "statement at %L with different type objects";
16877 && last_eq_type
== SEQ_MIXED
16878 && !gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
))
16879 || (eq_type
== SEQ_MIXED
16880 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
)))
16883 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
16884 "statement at %L with objects of different type";
16886 && last_eq_type
== SEQ_NONDEFAULT
16887 && !gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
))
16888 || (eq_type
== SEQ_NONDEFAULT
16889 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
)))
16892 msg
="Non-CHARACTER object %qs in default CHARACTER "
16893 "EQUIVALENCE statement at %L";
16894 if (last_eq_type
== SEQ_CHARACTER
16895 && eq_type
!= SEQ_CHARACTER
16896 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
))
16899 msg
="Non-NUMERIC object %qs in default NUMERIC "
16900 "EQUIVALENCE statement at %L";
16901 if (last_eq_type
== SEQ_NUMERIC
16902 && eq_type
!= SEQ_NUMERIC
16903 && !gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, &e
->where
))
16909 last_where
= &e
->where
;
16914 /* Shall not be an automatic array. */
16915 if (e
->ref
->type
== REF_ARRAY
&& is_non_constant_shape_array (sym
))
16917 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16918 "an EQUIVALENCE object", sym
->name
, &e
->where
);
16925 /* Shall not be a structure component. */
16926 if (r
->type
== REF_COMPONENT
)
16928 gfc_error ("Structure component %qs at %L cannot be an "
16929 "EQUIVALENCE object",
16930 r
->u
.c
.component
->name
, &e
->where
);
16934 /* A substring shall not have length zero. */
16935 if (r
->type
== REF_SUBSTRING
)
16937 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
16939 gfc_error ("Substring at %L has length zero",
16940 &r
->u
.ss
.start
->where
);
16950 /* Function called by resolve_fntype to flag other symbols used in the
16951 length type parameter specification of function results. */
16954 flag_fn_result_spec (gfc_expr
*expr
,
16956 int *f ATTRIBUTE_UNUSED
)
16961 if (expr
->expr_type
== EXPR_VARIABLE
)
16963 s
= expr
->symtree
->n
.sym
;
16964 for (ns
= s
->ns
; ns
; ns
= ns
->parent
)
16970 gfc_error ("Self reference in character length expression "
16971 "for %qs at %L", sym
->name
, &expr
->where
);
16975 if (!s
->fn_result_spec
16976 && s
->attr
.flavor
== FL_PARAMETER
)
16978 /* Function contained in a module.... */
16979 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_MODULE
)
16982 s
->fn_result_spec
= 1;
16983 /* Make sure that this symbol is translated as a module
16985 st
= gfc_get_unique_symtree (ns
);
16989 /* ... which is use associated and called. */
16990 else if (s
->attr
.use_assoc
|| s
->attr
.used_in_submodule
16992 /* External function matched with an interface. */
16995 && s
->ns
->proc_name
->attr
.if_source
== IFSRC_DECL
)
16996 || s
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
16997 && s
->ns
->proc_name
->attr
.function
))
16998 s
->fn_result_spec
= 1;
17005 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17008 resolve_fntype (gfc_namespace
*ns
)
17010 gfc_entry_list
*el
;
17013 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
17016 /* If there are any entries, ns->proc_name is the entry master
17017 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17019 sym
= ns
->entries
->sym
;
17021 sym
= ns
->proc_name
;
17022 if (sym
->result
== sym
17023 && sym
->ts
.type
== BT_UNKNOWN
17024 && !gfc_set_default_type (sym
, 0, NULL
)
17025 && !sym
->attr
.untyped
)
17027 gfc_error ("Function %qs at %L has no IMPLICIT type",
17028 sym
->name
, &sym
->declared_at
);
17029 sym
->attr
.untyped
= 1;
17032 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
17033 && !sym
->attr
.contained
17034 && !gfc_check_symbol_access (sym
->ts
.u
.derived
)
17035 && gfc_check_symbol_access (sym
))
17037 gfc_notify_std (GFC_STD_F2003
, "PUBLIC function %qs at "
17038 "%L of PRIVATE type %qs", sym
->name
,
17039 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
17043 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
17045 if (el
->sym
->result
== el
->sym
17046 && el
->sym
->ts
.type
== BT_UNKNOWN
17047 && !gfc_set_default_type (el
->sym
, 0, NULL
)
17048 && !el
->sym
->attr
.untyped
)
17050 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17051 el
->sym
->name
, &el
->sym
->declared_at
);
17052 el
->sym
->attr
.untyped
= 1;
17056 if (sym
->ts
.type
== BT_CHARACTER
)
17057 gfc_traverse_expr (sym
->ts
.u
.cl
->length
, sym
, flag_fn_result_spec
, 0);
17061 /* 12.3.2.1.1 Defined operators. */
17064 check_uop_procedure (gfc_symbol
*sym
, locus where
)
17066 gfc_formal_arglist
*formal
;
17068 if (!sym
->attr
.function
)
17070 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17071 sym
->name
, &where
);
17075 if (sym
->ts
.type
== BT_CHARACTER
17076 && !((sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
) || sym
->ts
.deferred
)
17077 && !(sym
->result
&& ((sym
->result
->ts
.u
.cl
17078 && sym
->result
->ts
.u
.cl
->length
) || sym
->result
->ts
.deferred
)))
17080 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17081 "character length", sym
->name
, &where
);
17085 formal
= gfc_sym_get_dummy_args (sym
);
17086 if (!formal
|| !formal
->sym
)
17088 gfc_error ("User operator procedure %qs at %L must have at least "
17089 "one argument", sym
->name
, &where
);
17093 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
17095 gfc_error ("First argument of operator interface at %L must be "
17096 "INTENT(IN)", &where
);
17100 if (formal
->sym
->attr
.optional
)
17102 gfc_error ("First argument of operator interface at %L cannot be "
17103 "optional", &where
);
17107 formal
= formal
->next
;
17108 if (!formal
|| !formal
->sym
)
17111 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
17113 gfc_error ("Second argument of operator interface at %L must be "
17114 "INTENT(IN)", &where
);
17118 if (formal
->sym
->attr
.optional
)
17120 gfc_error ("Second argument of operator interface at %L cannot be "
17121 "optional", &where
);
17127 gfc_error ("Operator interface at %L must have, at most, two "
17128 "arguments", &where
);
17136 gfc_resolve_uops (gfc_symtree
*symtree
)
17138 gfc_interface
*itr
;
17140 if (symtree
== NULL
)
17143 gfc_resolve_uops (symtree
->left
);
17144 gfc_resolve_uops (symtree
->right
);
17146 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
17147 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
17151 /* Examine all of the expressions associated with a program unit,
17152 assign types to all intermediate expressions, make sure that all
17153 assignments are to compatible types and figure out which names
17154 refer to which functions or subroutines. It doesn't check code
17155 block, which is handled by gfc_resolve_code. */
17158 resolve_types (gfc_namespace
*ns
)
17164 gfc_namespace
* old_ns
= gfc_current_ns
;
17165 bool recursive
= ns
->proc_name
&& ns
->proc_name
->attr
.recursive
;
17167 if (ns
->types_resolved
)
17170 /* Check that all IMPLICIT types are ok. */
17171 if (!ns
->seen_implicit_none
)
17174 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
17175 if (ns
->set_flag
[letter
]
17176 && !resolve_typespec_used (&ns
->default_type
[letter
],
17177 &ns
->implicit_loc
[letter
], NULL
))
17181 gfc_current_ns
= ns
;
17183 resolve_entries (ns
);
17185 resolve_common_vars (&ns
->blank_common
, false);
17186 resolve_common_blocks (ns
->common_root
);
17188 resolve_contained_functions (ns
);
17190 if (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
17191 && ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
17192 gfc_resolve_formal_arglist (ns
->proc_name
);
17194 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
17196 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
17197 resolve_charlen (cl
);
17199 gfc_traverse_ns (ns
, resolve_symbol
);
17201 resolve_fntype (ns
);
17203 for (n
= ns
->contained
; n
; n
= n
->sibling
)
17205 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
17206 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17207 "also be PURE", n
->proc_name
->name
,
17208 &n
->proc_name
->declared_at
);
17214 gfc_do_concurrent_flag
= 0;
17215 gfc_check_interfaces (ns
);
17217 gfc_traverse_ns (ns
, resolve_values
);
17219 if (ns
->save_all
|| (!flag_automatic
&& !recursive
))
17223 for (d
= ns
->data
; d
; d
= d
->next
)
17227 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
17229 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
17231 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
17232 resolve_equivalence (eq
);
17234 /* Warn about unused labels. */
17235 if (warn_unused_label
)
17236 warn_unused_fortran_label (ns
->st_labels
);
17238 gfc_resolve_uops (ns
->uop_root
);
17240 gfc_traverse_ns (ns
, gfc_verify_DTIO_procedures
);
17242 gfc_resolve_omp_declare_simd (ns
);
17244 gfc_resolve_omp_udrs (ns
->omp_udr_root
);
17246 ns
->types_resolved
= 1;
17248 gfc_current_ns
= old_ns
;
17252 /* Call gfc_resolve_code recursively. */
17255 resolve_codes (gfc_namespace
*ns
)
17258 bitmap_obstack old_obstack
;
17260 if (ns
->resolved
== 1)
17263 for (n
= ns
->contained
; n
; n
= n
->sibling
)
17266 gfc_current_ns
= ns
;
17268 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17269 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
17272 /* Set to an out of range value. */
17273 current_entry_id
= -1;
17275 old_obstack
= labels_obstack
;
17276 bitmap_obstack_initialize (&labels_obstack
);
17278 gfc_resolve_oacc_declare (ns
);
17279 gfc_resolve_oacc_routines (ns
);
17280 gfc_resolve_omp_local_vars (ns
);
17281 gfc_resolve_code (ns
->code
, ns
);
17283 bitmap_obstack_release (&labels_obstack
);
17284 labels_obstack
= old_obstack
;
17288 /* This function is called after a complete program unit has been compiled.
17289 Its purpose is to examine all of the expressions associated with a program
17290 unit, assign types to all intermediate expressions, make sure that all
17291 assignments are to compatible types and figure out which names refer to
17292 which functions or subroutines. */
17295 gfc_resolve (gfc_namespace
*ns
)
17297 gfc_namespace
*old_ns
;
17298 code_stack
*old_cs_base
;
17299 struct gfc_omp_saved_state old_omp_state
;
17305 old_ns
= gfc_current_ns
;
17306 old_cs_base
= cs_base
;
17308 /* As gfc_resolve can be called during resolution of an OpenMP construct
17309 body, we should clear any state associated to it, so that say NS's
17310 DO loops are not interpreted as OpenMP loops. */
17311 if (!ns
->construct_entities
)
17312 gfc_omp_save_and_clear_state (&old_omp_state
);
17314 resolve_types (ns
);
17315 component_assignment_level
= 0;
17316 resolve_codes (ns
);
17318 gfc_current_ns
= old_ns
;
17319 cs_base
= old_cs_base
;
17322 gfc_run_passes (ns
);
17324 if (!ns
->construct_entities
)
17325 gfc_omp_restore_state (&old_omp_state
);