1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
37 SEQ_NONDEFAULT
, SEQ_NUMERIC
, SEQ_CHARACTER
, SEQ_MIXED
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code
*head
, *current
;
47 struct code_stack
*prev
;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block except for END {IF|SELECT}s of enclosing
52 bitmap reachable_labels
;
56 static code_stack
*cs_base
= NULL
;
59 /* Nonzero if we're inside a FORALL block. */
61 static int forall_flag
;
63 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
65 static int omp_workshare_flag
;
67 /* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69 static int formal_arg_flag
= 0;
71 /* True if we are resolving a specification expression. */
72 static int specification_expr
= 0;
74 /* The id of the last entry seen. */
75 static int current_entry_id
;
77 /* We use bitmaps to determine if a branch target is valid. */
78 static bitmap_obstack labels_obstack
;
81 gfc_is_formal_arg (void)
83 return formal_arg_flag
;
86 /* Is the symbol host associated? */
88 is_sym_host_assoc (gfc_symbol
*sym
, gfc_namespace
*ns
)
90 for (ns
= ns
->parent
; ns
; ns
= ns
->parent
)
99 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
104 resolve_typespec_used (gfc_typespec
* ts
, locus
* where
, const char* name
)
106 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
->attr
.abstract
)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name
, where
, ts
->u
.derived
->name
);
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts
->u
.derived
->name
, where
);
125 /* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
135 resolve_formal_arglist (gfc_symbol
*proc
)
137 gfc_formal_arglist
*f
;
141 if (proc
->result
!= NULL
)
146 if (gfc_elemental (proc
)
147 || sym
->attr
.pointer
|| sym
->attr
.allocatable
148 || (sym
->as
&& sym
->as
->rank
> 0))
150 proc
->attr
.always_explicit
= 1;
151 sym
->attr
.always_explicit
= 1;
156 for (f
= proc
->formal
; f
; f
= f
->next
)
162 /* Alternate return placeholder. */
163 if (gfc_elemental (proc
))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc
->name
,
167 if (proc
->attr
.function
)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc
->name
,
174 if (sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
175 resolve_formal_arglist (sym
);
177 if (sym
->attr
.subroutine
|| sym
->attr
.external
|| sym
->attr
.intrinsic
)
179 if (gfc_pure (proc
) && !gfc_pure (sym
))
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym
->name
, &sym
->declared_at
);
186 if (gfc_elemental (proc
))
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym
->declared_at
);
193 if (sym
->attr
.function
194 && sym
->ts
.type
== BT_UNKNOWN
195 && sym
->attr
.intrinsic
)
197 gfc_intrinsic_sym
*isym
;
198 isym
= gfc_find_function (sym
->name
);
199 if (isym
== NULL
|| !isym
->specific
)
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym
->name
,
211 if (sym
->ts
.type
== BT_UNKNOWN
)
213 if (!sym
->attr
.function
|| sym
->result
== sym
)
214 gfc_set_default_type (sym
, 1, sym
->ns
);
217 gfc_resolve_array_spec (sym
->as
, 0);
219 /* We can't tell if an array with dimension (:) is assumed or deferred
220 shape until we know if it has the pointer or allocatable attributes.
222 if (sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_DEFERRED
223 && !(sym
->attr
.pointer
|| sym
->attr
.allocatable
))
225 sym
->as
->type
= AS_ASSUMED_SHAPE
;
226 for (i
= 0; i
< sym
->as
->rank
; i
++)
227 sym
->as
->lower
[i
] = gfc_int_expr (1);
230 if ((sym
->as
&& sym
->as
->rank
> 0 && sym
->as
->type
== AS_ASSUMED_SHAPE
)
231 || sym
->attr
.pointer
|| sym
->attr
.allocatable
|| sym
->attr
.target
232 || sym
->attr
.optional
)
234 proc
->attr
.always_explicit
= 1;
236 proc
->result
->attr
.always_explicit
= 1;
239 /* If the flavor is unknown at this point, it has to be a variable.
240 A procedure specification would have already set the type. */
242 if (sym
->attr
.flavor
== FL_UNKNOWN
)
243 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, sym
->name
, &sym
->declared_at
);
245 if (gfc_pure (proc
) && !sym
->attr
.pointer
246 && sym
->attr
.flavor
!= FL_PROCEDURE
)
248 if (proc
->attr
.function
&& sym
->attr
.intent
!= INTENT_IN
)
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym
->name
, proc
->name
,
253 if (proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_UNKNOWN
)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym
->name
, proc
->name
,
259 if (gfc_elemental (proc
))
262 if (sym
->attr
.codimension
)
264 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
265 "procedure", sym
->name
, &sym
->declared_at
);
271 gfc_error ("Argument '%s' of elemental procedure at %L must "
272 "be scalar", sym
->name
, &sym
->declared_at
);
276 if (sym
->attr
.pointer
)
278 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
279 "have the POINTER attribute", sym
->name
,
284 if (sym
->attr
.flavor
== FL_PROCEDURE
)
286 gfc_error ("Dummy procedure '%s' not allowed in elemental "
287 "procedure '%s' at %L", sym
->name
, proc
->name
,
293 /* Each dummy shall be specified to be scalar. */
294 if (proc
->attr
.proc
== PROC_ST_FUNCTION
)
298 gfc_error ("Argument '%s' of statement function at %L must "
299 "be scalar", sym
->name
, &sym
->declared_at
);
303 if (sym
->ts
.type
== BT_CHARACTER
)
305 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
306 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
308 gfc_error ("Character-valued argument '%s' of statement "
309 "function at %L must have constant length",
310 sym
->name
, &sym
->declared_at
);
320 /* Work function called when searching for symbols that have argument lists
321 associated with them. */
324 find_arglists (gfc_symbol
*sym
)
326 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
|| sym
->ns
!= gfc_current_ns
)
329 resolve_formal_arglist (sym
);
333 /* Given a namespace, resolve all formal argument lists within the namespace.
337 resolve_formal_arglists (gfc_namespace
*ns
)
342 gfc_traverse_ns (ns
, find_arglists
);
347 resolve_contained_fntype (gfc_symbol
*sym
, gfc_namespace
*ns
)
351 /* If this namespace is not a function or an entry master function,
353 if (! sym
|| !(sym
->attr
.function
|| sym
->attr
.flavor
== FL_VARIABLE
)
354 || sym
->attr
.entry_master
)
357 /* Try to find out of what the return type is. */
358 if (sym
->result
->ts
.type
== BT_UNKNOWN
&& sym
->result
->ts
.interface
== NULL
)
360 t
= gfc_set_default_type (sym
->result
, 0, ns
);
362 if (t
== FAILURE
&& !sym
->result
->attr
.untyped
)
364 if (sym
->result
== sym
)
365 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
366 sym
->name
, &sym
->declared_at
);
367 else if (!sym
->result
->attr
.proc_pointer
)
368 gfc_error ("Result '%s' of contained function '%s' at %L has "
369 "no IMPLICIT type", sym
->result
->name
, sym
->name
,
370 &sym
->result
->declared_at
);
371 sym
->result
->attr
.untyped
= 1;
375 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
376 type, lists the only ways a character length value of * can be used:
377 dummy arguments of procedures, named constants, and function results
378 in external functions. Internal function results and results of module
379 procedures are not on this list, ergo, not permitted. */
381 if (sym
->result
->ts
.type
== BT_CHARACTER
)
383 gfc_charlen
*cl
= sym
->result
->ts
.u
.cl
;
384 if (!cl
|| !cl
->length
)
386 /* See if this is a module-procedure and adapt error message
389 gcc_assert (ns
->parent
&& ns
->parent
->proc_name
);
390 module_proc
= (ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
);
392 gfc_error ("Character-valued %s '%s' at %L must not be"
394 module_proc
? _("module procedure")
395 : _("internal function"),
396 sym
->name
, &sym
->declared_at
);
402 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
403 introduce duplicates. */
406 merge_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
408 gfc_formal_arglist
*f
, *new_arglist
;
411 for (; new_args
!= NULL
; new_args
= new_args
->next
)
413 new_sym
= new_args
->sym
;
414 /* See if this arg is already in the formal argument list. */
415 for (f
= proc
->formal
; f
; f
= f
->next
)
417 if (new_sym
== f
->sym
)
424 /* Add a new argument. Argument order is not important. */
425 new_arglist
= gfc_get_formal_arglist ();
426 new_arglist
->sym
= new_sym
;
427 new_arglist
->next
= proc
->formal
;
428 proc
->formal
= new_arglist
;
433 /* Flag the arguments that are not present in all entries. */
436 check_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
438 gfc_formal_arglist
*f
, *head
;
441 for (f
= proc
->formal
; f
; f
= f
->next
)
446 for (new_args
= head
; new_args
; new_args
= new_args
->next
)
448 if (new_args
->sym
== f
->sym
)
455 f
->sym
->attr
.not_always_present
= 1;
460 /* Resolve alternate entry points. If a symbol has multiple entry points we
461 create a new master symbol for the main routine, and turn the existing
462 symbol into an entry point. */
465 resolve_entries (gfc_namespace
*ns
)
467 gfc_namespace
*old_ns
;
471 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
472 static int master_count
= 0;
474 if (ns
->proc_name
== NULL
)
477 /* No need to do anything if this procedure doesn't have alternate entry
482 /* We may already have resolved alternate entry points. */
483 if (ns
->proc_name
->attr
.entry_master
)
486 /* If this isn't a procedure something has gone horribly wrong. */
487 gcc_assert (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
);
489 /* Remember the current namespace. */
490 old_ns
= gfc_current_ns
;
494 /* Add the main entry point to the list of entry points. */
495 el
= gfc_get_entry_list ();
496 el
->sym
= ns
->proc_name
;
498 el
->next
= ns
->entries
;
500 ns
->proc_name
->attr
.entry
= 1;
502 /* If it is a module function, it needs to be in the right namespace
503 so that gfc_get_fake_result_decl can gather up the results. The
504 need for this arose in get_proc_name, where these beasts were
505 left in their own namespace, to keep prior references linked to
506 the entry declaration.*/
507 if (ns
->proc_name
->attr
.function
508 && ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
511 /* Do the same for entries where the master is not a module
512 procedure. These are retained in the module namespace because
513 of the module procedure declaration. */
514 for (el
= el
->next
; el
; el
= el
->next
)
515 if (el
->sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
516 && el
->sym
->attr
.mod_proc
)
520 /* Add an entry statement for it. */
527 /* Create a new symbol for the master function. */
528 /* Give the internal function a unique name (within this file).
529 Also include the function name so the user has some hope of figuring
530 out what is going on. */
531 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "master.%d.%s",
532 master_count
++, ns
->proc_name
->name
);
533 gfc_get_ha_symbol (name
, &proc
);
534 gcc_assert (proc
!= NULL
);
536 gfc_add_procedure (&proc
->attr
, PROC_INTERNAL
, proc
->name
, NULL
);
537 if (ns
->proc_name
->attr
.subroutine
)
538 gfc_add_subroutine (&proc
->attr
, proc
->name
, NULL
);
542 gfc_typespec
*ts
, *fts
;
543 gfc_array_spec
*as
, *fas
;
544 gfc_add_function (&proc
->attr
, proc
->name
, NULL
);
546 fas
= ns
->entries
->sym
->as
;
547 fas
= fas
? fas
: ns
->entries
->sym
->result
->as
;
548 fts
= &ns
->entries
->sym
->result
->ts
;
549 if (fts
->type
== BT_UNKNOWN
)
550 fts
= gfc_get_default_type (ns
->entries
->sym
->result
->name
, NULL
);
551 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
553 ts
= &el
->sym
->result
->ts
;
555 as
= as
? as
: el
->sym
->result
->as
;
556 if (ts
->type
== BT_UNKNOWN
)
557 ts
= gfc_get_default_type (el
->sym
->result
->name
, NULL
);
559 if (! gfc_compare_types (ts
, fts
)
560 || (el
->sym
->result
->attr
.dimension
561 != ns
->entries
->sym
->result
->attr
.dimension
)
562 || (el
->sym
->result
->attr
.pointer
563 != ns
->entries
->sym
->result
->attr
.pointer
))
565 else if (as
&& fas
&& ns
->entries
->sym
->result
!= el
->sym
->result
566 && gfc_compare_array_spec (as
, fas
) == 0)
567 gfc_error ("Function %s at %L has entries with mismatched "
568 "array specifications", ns
->entries
->sym
->name
,
569 &ns
->entries
->sym
->declared_at
);
570 /* The characteristics need to match and thus both need to have
571 the same string length, i.e. both len=*, or both len=4.
572 Having both len=<variable> is also possible, but difficult to
573 check at compile time. */
574 else if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& fts
->u
.cl
575 && (((ts
->u
.cl
->length
&& !fts
->u
.cl
->length
)
576 ||(!ts
->u
.cl
->length
&& fts
->u
.cl
->length
))
578 && ts
->u
.cl
->length
->expr_type
579 != fts
->u
.cl
->length
->expr_type
)
581 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
582 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
583 fts
->u
.cl
->length
->value
.integer
) != 0)))
584 gfc_notify_std (GFC_STD_GNU
, "Extension: Function %s at %L with "
585 "entries returning variables of different "
586 "string lengths", ns
->entries
->sym
->name
,
587 &ns
->entries
->sym
->declared_at
);
592 sym
= ns
->entries
->sym
->result
;
593 /* All result types the same. */
595 if (sym
->attr
.dimension
)
596 gfc_set_array_spec (proc
, gfc_copy_array_spec (sym
->as
), NULL
);
597 if (sym
->attr
.pointer
)
598 gfc_add_pointer (&proc
->attr
, NULL
);
602 /* Otherwise the result will be passed through a union by
604 proc
->attr
.mixed_entry_master
= 1;
605 for (el
= ns
->entries
; el
; el
= el
->next
)
607 sym
= el
->sym
->result
;
608 if (sym
->attr
.dimension
)
610 if (el
== ns
->entries
)
611 gfc_error ("FUNCTION result %s can't be an array in "
612 "FUNCTION %s at %L", sym
->name
,
613 ns
->entries
->sym
->name
, &sym
->declared_at
);
615 gfc_error ("ENTRY result %s can't be an array in "
616 "FUNCTION %s at %L", sym
->name
,
617 ns
->entries
->sym
->name
, &sym
->declared_at
);
619 else if (sym
->attr
.pointer
)
621 if (el
== ns
->entries
)
622 gfc_error ("FUNCTION result %s can't be a POINTER in "
623 "FUNCTION %s at %L", sym
->name
,
624 ns
->entries
->sym
->name
, &sym
->declared_at
);
626 gfc_error ("ENTRY result %s can't be a POINTER in "
627 "FUNCTION %s at %L", sym
->name
,
628 ns
->entries
->sym
->name
, &sym
->declared_at
);
633 if (ts
->type
== BT_UNKNOWN
)
634 ts
= gfc_get_default_type (sym
->name
, NULL
);
638 if (ts
->kind
== gfc_default_integer_kind
)
642 if (ts
->kind
== gfc_default_real_kind
643 || ts
->kind
== gfc_default_double_kind
)
647 if (ts
->kind
== gfc_default_complex_kind
)
651 if (ts
->kind
== gfc_default_logical_kind
)
655 /* We will issue error elsewhere. */
663 if (el
== ns
->entries
)
664 gfc_error ("FUNCTION result %s can't be of type %s "
665 "in FUNCTION %s at %L", sym
->name
,
666 gfc_typename (ts
), ns
->entries
->sym
->name
,
669 gfc_error ("ENTRY result %s can't be of type %s "
670 "in FUNCTION %s at %L", sym
->name
,
671 gfc_typename (ts
), ns
->entries
->sym
->name
,
678 proc
->attr
.access
= ACCESS_PRIVATE
;
679 proc
->attr
.entry_master
= 1;
681 /* Merge all the entry point arguments. */
682 for (el
= ns
->entries
; el
; el
= el
->next
)
683 merge_argument_lists (proc
, el
->sym
->formal
);
685 /* Check the master formal arguments for any that are not
686 present in all entry points. */
687 for (el
= ns
->entries
; el
; el
= el
->next
)
688 check_argument_lists (proc
, el
->sym
->formal
);
690 /* Use the master function for the function body. */
691 ns
->proc_name
= proc
;
693 /* Finalize the new symbols. */
694 gfc_commit_symbols ();
696 /* Restore the original namespace. */
697 gfc_current_ns
= old_ns
;
702 has_default_initializer (gfc_symbol
*der
)
706 gcc_assert (der
->attr
.flavor
== FL_DERIVED
);
707 for (c
= der
->components
; c
; c
= c
->next
)
708 if ((c
->ts
.type
!= BT_DERIVED
&& c
->initializer
)
709 || (c
->ts
.type
== BT_DERIVED
710 && (!c
->attr
.pointer
&& has_default_initializer (c
->ts
.u
.derived
))))
716 /* Resolve common variables. */
718 resolve_common_vars (gfc_symbol
*sym
, bool named_common
)
720 gfc_symbol
*csym
= sym
;
722 for (; csym
; csym
= csym
->common_next
)
724 if (csym
->value
|| csym
->attr
.data
)
726 if (!csym
->ns
->is_block_data
)
727 gfc_notify_std (GFC_STD_GNU
, "Variable '%s' at %L is in COMMON "
728 "but only in BLOCK DATA initialization is "
729 "allowed", csym
->name
, &csym
->declared_at
);
730 else if (!named_common
)
731 gfc_notify_std (GFC_STD_GNU
, "Initialized variable '%s' at %L is "
732 "in a blank COMMON but initialization is only "
733 "allowed in named common blocks", csym
->name
,
737 if (csym
->ts
.type
!= BT_DERIVED
)
740 if (!(csym
->ts
.u
.derived
->attr
.sequence
741 || csym
->ts
.u
.derived
->attr
.is_bind_c
))
742 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
743 "has neither the SEQUENCE nor the BIND(C) "
744 "attribute", csym
->name
, &csym
->declared_at
);
745 if (csym
->ts
.u
.derived
->attr
.alloc_comp
)
746 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
747 "has an ultimate component that is "
748 "allocatable", csym
->name
, &csym
->declared_at
);
749 if (has_default_initializer (csym
->ts
.u
.derived
))
750 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
751 "may not have default initializer", csym
->name
,
754 if (csym
->attr
.flavor
== FL_UNKNOWN
&& !csym
->attr
.proc_pointer
)
755 gfc_add_flavor (&csym
->attr
, FL_VARIABLE
, csym
->name
, &csym
->declared_at
);
759 /* Resolve common blocks. */
761 resolve_common_blocks (gfc_symtree
*common_root
)
765 if (common_root
== NULL
)
768 if (common_root
->left
)
769 resolve_common_blocks (common_root
->left
);
770 if (common_root
->right
)
771 resolve_common_blocks (common_root
->right
);
773 resolve_common_vars (common_root
->n
.common
->head
, true);
775 gfc_find_symbol (common_root
->name
, gfc_current_ns
, 0, &sym
);
779 if (sym
->attr
.flavor
== FL_PARAMETER
)
780 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
781 sym
->name
, &common_root
->n
.common
->where
, &sym
->declared_at
);
783 if (sym
->attr
.intrinsic
)
784 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
785 sym
->name
, &common_root
->n
.common
->where
);
786 else if (sym
->attr
.result
787 || gfc_is_function_return_value (sym
, gfc_current_ns
))
788 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
789 "that is also a function result", sym
->name
,
790 &common_root
->n
.common
->where
);
791 else if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_INTERNAL
792 && sym
->attr
.proc
!= PROC_ST_FUNCTION
)
793 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
794 "that is also a global procedure", sym
->name
,
795 &common_root
->n
.common
->where
);
799 /* Resolve contained function types. Because contained functions can call one
800 another, they have to be worked out before any of the contained procedures
803 The good news is that if a function doesn't already have a type, the only
804 way it can get one is through an IMPLICIT type or a RESULT variable, because
805 by definition contained functions are contained namespace they're contained
806 in, not in a sibling or parent namespace. */
809 resolve_contained_functions (gfc_namespace
*ns
)
811 gfc_namespace
*child
;
814 resolve_formal_arglists (ns
);
816 for (child
= ns
->contained
; child
; child
= child
->sibling
)
818 /* Resolve alternate entry points first. */
819 resolve_entries (child
);
821 /* Then check function return types. */
822 resolve_contained_fntype (child
->proc_name
, child
);
823 for (el
= child
->entries
; el
; el
= el
->next
)
824 resolve_contained_fntype (el
->sym
, child
);
829 /* Resolve all of the elements of a structure constructor and make sure that
830 the types are correct. */
833 resolve_structure_cons (gfc_expr
*expr
)
835 gfc_constructor
*cons
;
841 cons
= expr
->value
.constructor
;
842 /* A constructor may have references if it is the result of substituting a
843 parameter variable. In this case we just pull out the component we
846 comp
= expr
->ref
->u
.c
.sym
->components
;
848 comp
= expr
->ts
.u
.derived
->components
;
850 /* See if the user is trying to invoke a structure constructor for one of
851 the iso_c_binding derived types. */
852 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
853 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
854 && (cons
->expr
== NULL
|| cons
->expr
->expr_type
!= EXPR_NULL
))
856 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
857 expr
->ts
.u
.derived
->name
, &(expr
->where
));
861 /* Return if structure constructor is c_null_(fun)prt. */
862 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
863 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
864 && cons
->expr
&& cons
->expr
->expr_type
== EXPR_NULL
)
867 for (; comp
; comp
= comp
->next
, cons
= cons
->next
)
874 if (gfc_resolve_expr (cons
->expr
) == FAILURE
)
880 rank
= comp
->as
? comp
->as
->rank
: 0;
881 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
882 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
884 gfc_error ("The rank of the element in the derived type "
885 "constructor at %L does not match that of the "
886 "component (%d/%d)", &cons
->expr
->where
,
887 cons
->expr
->rank
, rank
);
891 /* If we don't have the right type, try to convert it. */
893 if (!gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
896 if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
897 gfc_error ("The element in the derived type constructor at %L, "
898 "for pointer component '%s', is %s but should be %s",
899 &cons
->expr
->where
, comp
->name
,
900 gfc_basic_typename (cons
->expr
->ts
.type
),
901 gfc_basic_typename (comp
->ts
.type
));
903 t
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
906 if (cons
->expr
->expr_type
== EXPR_NULL
907 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
908 || comp
->attr
.proc_pointer
909 || (comp
->ts
.type
== BT_CLASS
910 && (comp
->ts
.u
.derived
->components
->attr
.pointer
911 || comp
->ts
.u
.derived
->components
->attr
.allocatable
))))
914 gfc_error ("The NULL in the derived type constructor at %L is "
915 "being applied to component '%s', which is neither "
916 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
920 if (!comp
->attr
.pointer
|| cons
->expr
->expr_type
== EXPR_NULL
)
923 a
= gfc_expr_attr (cons
->expr
);
925 if (!a
.pointer
&& !a
.target
)
928 gfc_error ("The element in the derived type constructor at %L, "
929 "for pointer component '%s' should be a POINTER or "
930 "a TARGET", &cons
->expr
->where
, comp
->name
);
933 /* F2003, C1272 (3). */
934 if (gfc_pure (NULL
) && cons
->expr
->expr_type
== EXPR_VARIABLE
935 && gfc_impure_variable (cons
->expr
->symtree
->n
.sym
))
938 gfc_error ("Invalid expression in the derived type constructor for pointer "
939 "component '%s' at %L in PURE procedure", comp
->name
,
948 /****************** Expression name resolution ******************/
950 /* Returns 0 if a symbol was not declared with a type or
951 attribute declaration statement, nonzero otherwise. */
954 was_declared (gfc_symbol
*sym
)
960 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
963 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
964 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
965 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
966 || a
.asynchronous
|| a
.codimension
)
973 /* Determine if a symbol is generic or not. */
976 generic_sym (gfc_symbol
*sym
)
980 if (sym
->attr
.generic
||
981 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
984 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
987 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
994 return generic_sym (s
);
1001 /* Determine if a symbol is specific or not. */
1004 specific_sym (gfc_symbol
*sym
)
1008 if (sym
->attr
.if_source
== IFSRC_IFBODY
1009 || sym
->attr
.proc
== PROC_MODULE
1010 || sym
->attr
.proc
== PROC_INTERNAL
1011 || sym
->attr
.proc
== PROC_ST_FUNCTION
1012 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
1013 || sym
->attr
.external
)
1016 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1019 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
1021 return (s
== NULL
) ? 0 : specific_sym (s
);
1025 /* Figure out if the procedure is specific, generic or unknown. */
1028 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
}
1032 procedure_kind (gfc_symbol
*sym
)
1034 if (generic_sym (sym
))
1035 return PTYPE_GENERIC
;
1037 if (specific_sym (sym
))
1038 return PTYPE_SPECIFIC
;
1040 return PTYPE_UNKNOWN
;
1043 /* Check references to assumed size arrays. The flag need_full_assumed_size
1044 is nonzero when matching actual arguments. */
1046 static int need_full_assumed_size
= 0;
1049 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1051 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1054 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1055 What should it be? */
1056 if ((e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1057 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1058 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1060 gfc_error ("The upper bound in the last dimension must "
1061 "appear in the reference to the assumed size "
1062 "array '%s' at %L", sym
->name
, &e
->where
);
1069 /* Look for bad assumed size array references in argument expressions
1070 of elemental and array valued intrinsic procedures. Since this is
1071 called from procedure resolution functions, it only recurses at
1075 resolve_assumed_size_actual (gfc_expr
*e
)
1080 switch (e
->expr_type
)
1083 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1088 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1089 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1100 /* Check a generic procedure, passed as an actual argument, to see if
1101 there is a matching specific name. If none, it is an error, and if
1102 more than one, the reference is ambiguous. */
1104 count_specific_procs (gfc_expr
*e
)
1111 sym
= e
->symtree
->n
.sym
;
1113 for (p
= sym
->generic
; p
; p
= p
->next
)
1114 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1116 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1122 gfc_error ("'%s' at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1126 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1127 "argument at %L", sym
->name
, &e
->where
);
1133 /* See if a call to sym could possibly be a not allowed RECURSION because of
1134 a missing RECURIVE declaration. This means that either sym is the current
1135 context itself, or sym is the parent of a contained procedure calling its
1136 non-RECURSIVE containing procedure.
1137 This also works if sym is an ENTRY. */
1140 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1142 gfc_symbol
* proc_sym
;
1143 gfc_symbol
* context_proc
;
1144 gfc_namespace
* real_context
;
1146 if (sym
->attr
.flavor
== FL_PROGRAM
)
1149 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
);
1151 /* If we've got an ENTRY, find real procedure. */
1152 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1153 proc_sym
= sym
->ns
->entries
->sym
;
1157 /* If sym is RECURSIVE, all is well of course. */
1158 if (proc_sym
->attr
.recursive
|| gfc_option
.flag_recursive
)
1161 /* Find the context procedure's "real" symbol if it has entries.
1162 We look for a procedure symbol, so recurse on the parents if we don't
1163 find one (like in case of a BLOCK construct). */
1164 for (real_context
= context
; ; real_context
= real_context
->parent
)
1166 /* We should find something, eventually! */
1167 gcc_assert (real_context
);
1169 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1170 : real_context
->proc_name
);
1172 /* In some special cases, there may not be a proc_name, like for this
1174 real(bad_kind()) function foo () ...
1175 when checking the call to bad_kind ().
1176 In these cases, we simply return here and assume that the
1181 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1185 /* A call from sym's body to itself is recursion, of course. */
1186 if (context_proc
== proc_sym
)
1189 /* The same is true if context is a contained procedure and sym the
1191 if (context_proc
->attr
.contained
)
1193 gfc_symbol
* parent_proc
;
1195 gcc_assert (context
->parent
);
1196 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1197 : context
->parent
->proc_name
);
1199 if (parent_proc
== proc_sym
)
1207 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1208 its typespec and formal argument list. */
1211 resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1213 gfc_intrinsic_sym
* isym
;
1219 /* We already know this one is an intrinsic, so we don't call
1220 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1221 gfc_find_subroutine directly to check whether it is a function or
1224 if ((isym
= gfc_find_function (sym
->name
)))
1226 if (sym
->ts
.type
!= BT_UNKNOWN
&& gfc_option
.warn_surprising
1227 && !sym
->attr
.implicit_type
)
1228 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1229 " ignored", sym
->name
, &sym
->declared_at
);
1231 if (!sym
->attr
.function
&&
1232 gfc_add_function (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1237 else if ((isym
= gfc_find_subroutine (sym
->name
)))
1239 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1241 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1242 " specifier", sym
->name
, &sym
->declared_at
);
1246 if (!sym
->attr
.subroutine
&&
1247 gfc_add_subroutine (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1252 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym
->name
,
1257 gfc_copy_formal_args_intr (sym
, isym
);
1259 /* Check it is actually available in the standard settings. */
1260 if (gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
)
1263 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1264 " available in the current standard settings but %s. Use"
1265 " an appropriate -std=* option or enable -fall-intrinsics"
1266 " in order to use it.",
1267 sym
->name
, &sym
->declared_at
, symstd
);
1275 /* Resolve a procedure expression, like passing it to a called procedure or as
1276 RHS for a procedure pointer assignment. */
1279 resolve_procedure_expression (gfc_expr
* expr
)
1283 if (expr
->expr_type
!= EXPR_VARIABLE
)
1285 gcc_assert (expr
->symtree
);
1287 sym
= expr
->symtree
->n
.sym
;
1289 if (sym
->attr
.intrinsic
)
1290 resolve_intrinsic (sym
, &expr
->where
);
1292 if (sym
->attr
.flavor
!= FL_PROCEDURE
1293 || (sym
->attr
.function
&& sym
->result
== sym
))
1296 /* A non-RECURSIVE procedure that is used as procedure expression within its
1297 own body is in danger of being called recursively. */
1298 if (is_illegal_recursion (sym
, gfc_current_ns
))
1299 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1300 " itself recursively. Declare it RECURSIVE or use"
1301 " -frecursive", sym
->name
, &expr
->where
);
1307 /* Resolve an actual argument list. Most of the time, this is just
1308 resolving the expressions in the list.
1309 The exception is that we sometimes have to decide whether arguments
1310 that look like procedure arguments are really simple variable
1314 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1315 bool no_formal_args
)
1318 gfc_symtree
*parent_st
;
1320 int save_need_full_assumed_size
;
1321 gfc_component
*comp
;
1323 for (; arg
; arg
= arg
->next
)
1328 /* Check the label is a valid branching target. */
1331 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1333 gfc_error ("Label %d referenced at %L is never defined",
1334 arg
->label
->value
, &arg
->label
->where
);
1341 if (gfc_is_proc_ptr_comp (e
, &comp
))
1344 if (e
->expr_type
== EXPR_PPC
)
1346 if (comp
->as
!= NULL
)
1347 e
->rank
= comp
->as
->rank
;
1348 e
->expr_type
= EXPR_FUNCTION
;
1350 if (gfc_resolve_expr (e
) == FAILURE
)
1355 if (e
->expr_type
== EXPR_VARIABLE
1356 && e
->symtree
->n
.sym
->attr
.generic
1358 && count_specific_procs (e
) != 1)
1361 if (e
->ts
.type
!= BT_PROCEDURE
)
1363 save_need_full_assumed_size
= need_full_assumed_size
;
1364 if (e
->expr_type
!= EXPR_VARIABLE
)
1365 need_full_assumed_size
= 0;
1366 if (gfc_resolve_expr (e
) != SUCCESS
)
1368 need_full_assumed_size
= save_need_full_assumed_size
;
1372 /* See if the expression node should really be a variable reference. */
1374 sym
= e
->symtree
->n
.sym
;
1376 if (sym
->attr
.flavor
== FL_PROCEDURE
1377 || sym
->attr
.intrinsic
1378 || sym
->attr
.external
)
1382 /* If a procedure is not already determined to be something else
1383 check if it is intrinsic. */
1384 if (!sym
->attr
.intrinsic
1385 && !(sym
->attr
.external
|| sym
->attr
.use_assoc
1386 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1387 && gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1388 sym
->attr
.intrinsic
= 1;
1390 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1392 gfc_error ("Statement function '%s' at %L is not allowed as an "
1393 "actual argument", sym
->name
, &e
->where
);
1396 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1397 sym
->attr
.subroutine
);
1398 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1400 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1401 "actual argument", sym
->name
, &e
->where
);
1404 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1405 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1407 gfc_error ("Internal procedure '%s' is not allowed as an "
1408 "actual argument at %L", sym
->name
, &e
->where
);
1411 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1413 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1414 "allowed as an actual argument at %L", sym
->name
,
1418 /* Check if a generic interface has a specific procedure
1419 with the same name before emitting an error. */
1420 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
1423 /* Just in case a specific was found for the expression. */
1424 sym
= e
->symtree
->n
.sym
;
1426 /* If the symbol is the function that names the current (or
1427 parent) scope, then we really have a variable reference. */
1429 if (gfc_is_function_return_value (sym
, sym
->ns
))
1432 /* If all else fails, see if we have a specific intrinsic. */
1433 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
1435 gfc_intrinsic_sym
*isym
;
1437 isym
= gfc_find_function (sym
->name
);
1438 if (isym
== NULL
|| !isym
->specific
)
1440 gfc_error ("Unable to find a specific INTRINSIC procedure "
1441 "for the reference '%s' at %L", sym
->name
,
1446 sym
->attr
.intrinsic
= 1;
1447 sym
->attr
.function
= 1;
1450 if (gfc_resolve_expr (e
) == FAILURE
)
1455 /* See if the name is a module procedure in a parent unit. */
1457 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1460 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
1462 gfc_error ("Symbol '%s' at %L is ambiguous", sym
->name
, &e
->where
);
1466 if (parent_st
== NULL
)
1469 sym
= parent_st
->n
.sym
;
1470 e
->symtree
= parent_st
; /* Point to the right thing. */
1472 if (sym
->attr
.flavor
== FL_PROCEDURE
1473 || sym
->attr
.intrinsic
1474 || sym
->attr
.external
)
1476 if (gfc_resolve_expr (e
) == FAILURE
)
1482 e
->expr_type
= EXPR_VARIABLE
;
1484 if (sym
->as
!= NULL
)
1486 e
->rank
= sym
->as
->rank
;
1487 e
->ref
= gfc_get_ref ();
1488 e
->ref
->type
= REF_ARRAY
;
1489 e
->ref
->u
.ar
.type
= AR_FULL
;
1490 e
->ref
->u
.ar
.as
= sym
->as
;
1493 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1494 primary.c (match_actual_arg). If above code determines that it
1495 is a variable instead, it needs to be resolved as it was not
1496 done at the beginning of this function. */
1497 save_need_full_assumed_size
= need_full_assumed_size
;
1498 if (e
->expr_type
!= EXPR_VARIABLE
)
1499 need_full_assumed_size
= 0;
1500 if (gfc_resolve_expr (e
) != SUCCESS
)
1502 need_full_assumed_size
= save_need_full_assumed_size
;
1505 /* Check argument list functions %VAL, %LOC and %REF. There is
1506 nothing to do for %REF. */
1507 if (arg
->name
&& arg
->name
[0] == '%')
1509 if (strncmp ("%VAL", arg
->name
, 4) == 0)
1511 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
1513 gfc_error ("By-value argument at %L is not of numeric "
1520 gfc_error ("By-value argument at %L cannot be an array or "
1521 "an array section", &e
->where
);
1525 /* Intrinsics are still PROC_UNKNOWN here. However,
1526 since same file external procedures are not resolvable
1527 in gfortran, it is a good deal easier to leave them to
1529 if (ptype
!= PROC_UNKNOWN
1530 && ptype
!= PROC_DUMMY
1531 && ptype
!= PROC_EXTERNAL
1532 && ptype
!= PROC_MODULE
)
1534 gfc_error ("By-value argument at %L is not allowed "
1535 "in this context", &e
->where
);
1540 /* Statement functions have already been excluded above. */
1541 else if (strncmp ("%LOC", arg
->name
, 4) == 0
1542 && e
->ts
.type
== BT_PROCEDURE
)
1544 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
1546 gfc_error ("Passing internal procedure at %L by location "
1547 "not allowed", &e
->where
);
1558 /* Do the checks of the actual argument list that are specific to elemental
1559 procedures. If called with c == NULL, we have a function, otherwise if
1560 expr == NULL, we have a subroutine. */
1563 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
1565 gfc_actual_arglist
*arg0
;
1566 gfc_actual_arglist
*arg
;
1567 gfc_symbol
*esym
= NULL
;
1568 gfc_intrinsic_sym
*isym
= NULL
;
1570 gfc_intrinsic_arg
*iformal
= NULL
;
1571 gfc_formal_arglist
*eformal
= NULL
;
1572 bool formal_optional
= false;
1573 bool set_by_optional
= false;
1577 /* Is this an elemental procedure? */
1578 if (expr
&& expr
->value
.function
.actual
!= NULL
)
1580 if (expr
->value
.function
.esym
!= NULL
1581 && expr
->value
.function
.esym
->attr
.elemental
)
1583 arg0
= expr
->value
.function
.actual
;
1584 esym
= expr
->value
.function
.esym
;
1586 else if (expr
->value
.function
.isym
!= NULL
1587 && expr
->value
.function
.isym
->elemental
)
1589 arg0
= expr
->value
.function
.actual
;
1590 isym
= expr
->value
.function
.isym
;
1595 else if (c
&& c
->ext
.actual
!= NULL
)
1597 arg0
= c
->ext
.actual
;
1599 if (c
->resolved_sym
)
1600 esym
= c
->resolved_sym
;
1602 esym
= c
->symtree
->n
.sym
;
1605 if (!esym
->attr
.elemental
)
1611 /* The rank of an elemental is the rank of its array argument(s). */
1612 for (arg
= arg0
; arg
; arg
= arg
->next
)
1614 if (arg
->expr
!= NULL
&& arg
->expr
->rank
> 0)
1616 rank
= arg
->expr
->rank
;
1617 if (arg
->expr
->expr_type
== EXPR_VARIABLE
1618 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
1619 set_by_optional
= true;
1621 /* Function specific; set the result rank and shape. */
1625 if (!expr
->shape
&& arg
->expr
->shape
)
1627 expr
->shape
= gfc_get_shape (rank
);
1628 for (i
= 0; i
< rank
; i
++)
1629 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
1636 /* If it is an array, it shall not be supplied as an actual argument
1637 to an elemental procedure unless an array of the same rank is supplied
1638 as an actual argument corresponding to a nonoptional dummy argument of
1639 that elemental procedure(12.4.1.5). */
1640 formal_optional
= false;
1642 iformal
= isym
->formal
;
1644 eformal
= esym
->formal
;
1646 for (arg
= arg0
; arg
; arg
= arg
->next
)
1650 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
1651 formal_optional
= true;
1652 eformal
= eformal
->next
;
1654 else if (isym
&& iformal
)
1656 if (iformal
->optional
)
1657 formal_optional
= true;
1658 iformal
= iformal
->next
;
1661 formal_optional
= true;
1663 if (pedantic
&& arg
->expr
!= NULL
1664 && arg
->expr
->expr_type
== EXPR_VARIABLE
1665 && arg
->expr
->symtree
->n
.sym
->attr
.optional
1668 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
1669 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
1671 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1672 "MISSING, it cannot be the actual argument of an "
1673 "ELEMENTAL procedure unless there is a non-optional "
1674 "argument with the same rank (12.4.1.5)",
1675 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
1680 for (arg
= arg0
; arg
; arg
= arg
->next
)
1682 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
1685 /* Being elemental, the last upper bound of an assumed size array
1686 argument must be present. */
1687 if (resolve_assumed_size_actual (arg
->expr
))
1690 /* Elemental procedure's array actual arguments must conform. */
1693 if (gfc_check_conformance (arg
->expr
, e
,
1694 "elemental procedure") == FAILURE
)
1701 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1702 is an array, the intent inout/out variable needs to be also an array. */
1703 if (rank
> 0 && esym
&& expr
== NULL
)
1704 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
1705 arg
= arg
->next
, eformal
= eformal
->next
)
1706 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
1707 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
1708 && arg
->expr
&& arg
->expr
->rank
== 0)
1710 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1711 "ELEMENTAL subroutine '%s' is a scalar, but another "
1712 "actual argument is an array", &arg
->expr
->where
,
1713 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
1714 : "INOUT", eformal
->sym
->name
, esym
->name
);
1721 /* Go through each actual argument in ACTUAL and see if it can be
1722 implemented as an inlined, non-copying intrinsic. FNSYM is the
1723 function being called, or NULL if not known. */
1726 find_noncopying_intrinsics (gfc_symbol
*fnsym
, gfc_actual_arglist
*actual
)
1728 gfc_actual_arglist
*ap
;
1731 for (ap
= actual
; ap
; ap
= ap
->next
)
1733 && (expr
= gfc_get_noncopying_intrinsic_argument (ap
->expr
))
1734 && !gfc_check_fncall_dependency (expr
, INTENT_IN
, fnsym
, actual
,
1736 ap
->expr
->inline_noncopying_intrinsic
= 1;
1740 /* This function does the checking of references to global procedures
1741 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1742 77 and 95 standards. It checks for a gsymbol for the name, making
1743 one if it does not already exist. If it already exists, then the
1744 reference being resolved must correspond to the type of gsymbol.
1745 Otherwise, the new symbol is equipped with the attributes of the
1746 reference. The corresponding code that is called in creating
1747 global entities is parse.c.
1749 In addition, for all but -std=legacy, the gsymbols are used to
1750 check the interfaces of external procedures from the same file.
1751 The namespace of the gsymbol is resolved and then, once this is
1752 done the interface is checked. */
1756 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1758 if (!gsym_ns
->proc_name
->attr
.recursive
)
1761 if (sym
->ns
== gsym_ns
)
1764 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
1771 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1773 if (gsym_ns
->entries
)
1775 gfc_entry_list
*entry
= gsym_ns
->entries
;
1777 for (; entry
; entry
= entry
->next
)
1779 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
1781 if (strcmp (gsym_ns
->proc_name
->name
,
1782 sym
->ns
->proc_name
->name
) == 0)
1786 && strcmp (gsym_ns
->proc_name
->name
,
1787 sym
->ns
->parent
->proc_name
->name
) == 0)
1796 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
,
1797 gfc_actual_arglist
**actual
, int sub
)
1801 enum gfc_symbol_type type
;
1803 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
1805 gsym
= gfc_get_gsymbol (sym
->name
);
1807 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
1808 gfc_global_used (gsym
, where
);
1810 if (gfc_option
.flag_whole_file
1811 && sym
->attr
.if_source
== IFSRC_UNKNOWN
1812 && gsym
->type
!= GSYM_UNKNOWN
1814 && gsym
->ns
->resolved
!= -1
1815 && gsym
->ns
->proc_name
1816 && not_in_recursive (sym
, gsym
->ns
)
1817 && not_entry_self_reference (sym
, gsym
->ns
))
1819 /* Make sure that translation for the gsymbol occurs before
1820 the procedure currently being resolved. */
1821 ns
= gsym
->ns
->resolved
? NULL
: gfc_global_ns_list
;
1822 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
1824 if (ns
->sibling
== gsym
->ns
)
1826 ns
->sibling
= gsym
->ns
->sibling
;
1827 gsym
->ns
->sibling
= gfc_global_ns_list
;
1828 gfc_global_ns_list
= gsym
->ns
;
1833 if (!gsym
->ns
->resolved
)
1835 gfc_dt_list
*old_dt_list
;
1837 /* Stash away derived types so that the backend_decls do not
1839 old_dt_list
= gfc_derived_types
;
1840 gfc_derived_types
= NULL
;
1842 gfc_resolve (gsym
->ns
);
1844 /* Store the new derived types with the global namespace. */
1845 if (gfc_derived_types
)
1846 gsym
->ns
->derived_types
= gfc_derived_types
;
1848 /* Restore the derived types of this namespace. */
1849 gfc_derived_types
= old_dt_list
;
1852 if (gsym
->ns
->proc_name
->attr
.function
1853 && gsym
->ns
->proc_name
->as
1854 && gsym
->ns
->proc_name
->as
->rank
1855 && (!sym
->as
|| sym
->as
->rank
!= gsym
->ns
->proc_name
->as
->rank
))
1856 gfc_error ("The reference to function '%s' at %L either needs an "
1857 "explicit INTERFACE or the rank is incorrect", sym
->name
,
1860 /* Non-assumed length character functions. */
1861 if (sym
->attr
.function
&& sym
->ts
.type
== BT_CHARACTER
1862 && gsym
->ns
->proc_name
->ts
.u
.cl
!= NULL
1863 && gsym
->ns
->proc_name
->ts
.u
.cl
->length
!= NULL
)
1865 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1867 if (!sym
->attr
.entry_master
&& sym
->attr
.if_source
== IFSRC_UNKNOWN
1868 && cl
&& cl
->length
&& cl
->length
->expr_type
!= EXPR_CONSTANT
)
1870 gfc_error ("Nonconstant character-length function '%s' at %L "
1871 "must have an explicit interface", sym
->name
,
1876 if (gfc_option
.flag_whole_file
== 1
1877 || ((gfc_option
.warn_std
& GFC_STD_LEGACY
)
1879 !(gfc_option
.warn_std
& GFC_STD_GNU
)))
1880 gfc_errors_to_warnings (1);
1882 gfc_procedure_use (gsym
->ns
->proc_name
, actual
, where
);
1884 gfc_errors_to_warnings (0);
1887 if (gsym
->type
== GSYM_UNKNOWN
)
1890 gsym
->where
= *where
;
1897 /************* Function resolution *************/
1899 /* Resolve a function call known to be generic.
1900 Section 14.1.2.4.1. */
1903 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
1907 if (sym
->attr
.generic
)
1909 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
1912 expr
->value
.function
.name
= s
->name
;
1913 expr
->value
.function
.esym
= s
;
1915 if (s
->ts
.type
!= BT_UNKNOWN
)
1917 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
1918 expr
->ts
= s
->result
->ts
;
1921 expr
->rank
= s
->as
->rank
;
1922 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
1923 expr
->rank
= s
->result
->as
->rank
;
1925 gfc_set_sym_referenced (expr
->value
.function
.esym
);
1930 /* TODO: Need to search for elemental references in generic
1934 if (sym
->attr
.intrinsic
)
1935 return gfc_intrinsic_func_interface (expr
, 0);
1942 resolve_generic_f (gfc_expr
*expr
)
1947 sym
= expr
->symtree
->n
.sym
;
1951 m
= resolve_generic_f0 (expr
, sym
);
1954 else if (m
== MATCH_ERROR
)
1958 if (sym
->ns
->parent
== NULL
)
1960 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
1964 if (!generic_sym (sym
))
1968 /* Last ditch attempt. See if the reference is to an intrinsic
1969 that possesses a matching interface. 14.1.2.4 */
1970 if (sym
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
1972 gfc_error ("There is no specific function for the generic '%s' at %L",
1973 expr
->symtree
->n
.sym
->name
, &expr
->where
);
1977 m
= gfc_intrinsic_func_interface (expr
, 0);
1981 gfc_error ("Generic function '%s' at %L is not consistent with a "
1982 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
1989 /* Resolve a function call known to be specific. */
1992 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
1996 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
1998 if (sym
->attr
.dummy
)
2000 sym
->attr
.proc
= PROC_DUMMY
;
2004 sym
->attr
.proc
= PROC_EXTERNAL
;
2008 if (sym
->attr
.proc
== PROC_MODULE
2009 || sym
->attr
.proc
== PROC_ST_FUNCTION
2010 || sym
->attr
.proc
== PROC_INTERNAL
)
2013 if (sym
->attr
.intrinsic
)
2015 m
= gfc_intrinsic_func_interface (expr
, 1);
2019 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2020 "with an intrinsic", sym
->name
, &expr
->where
);
2028 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2031 expr
->ts
= sym
->result
->ts
;
2034 expr
->value
.function
.name
= sym
->name
;
2035 expr
->value
.function
.esym
= sym
;
2036 if (sym
->as
!= NULL
)
2037 expr
->rank
= sym
->as
->rank
;
2044 resolve_specific_f (gfc_expr
*expr
)
2049 sym
= expr
->symtree
->n
.sym
;
2053 m
= resolve_specific_f0 (sym
, expr
);
2056 if (m
== MATCH_ERROR
)
2059 if (sym
->ns
->parent
== NULL
)
2062 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2068 gfc_error ("Unable to resolve the specific function '%s' at %L",
2069 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2075 /* Resolve a procedure call not known to be generic nor specific. */
2078 resolve_unknown_f (gfc_expr
*expr
)
2083 sym
= expr
->symtree
->n
.sym
;
2085 if (sym
->attr
.dummy
)
2087 sym
->attr
.proc
= PROC_DUMMY
;
2088 expr
->value
.function
.name
= sym
->name
;
2092 /* See if we have an intrinsic function reference. */
2094 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2096 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2101 /* The reference is to an external name. */
2103 sym
->attr
.proc
= PROC_EXTERNAL
;
2104 expr
->value
.function
.name
= sym
->name
;
2105 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2107 if (sym
->as
!= NULL
)
2108 expr
->rank
= sym
->as
->rank
;
2110 /* Type of the expression is either the type of the symbol or the
2111 default type of the symbol. */
2114 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2116 if (sym
->ts
.type
!= BT_UNKNOWN
)
2120 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2122 if (ts
->type
== BT_UNKNOWN
)
2124 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2125 sym
->name
, &expr
->where
);
2136 /* Return true, if the symbol is an external procedure. */
2138 is_external_proc (gfc_symbol
*sym
)
2140 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2141 && !(sym
->attr
.intrinsic
2142 || gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
))
2143 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2144 && !sym
->attr
.use_assoc
2152 /* Figure out if a function reference is pure or not. Also set the name
2153 of the function for a potential error message. Return nonzero if the
2154 function is PURE, zero if not. */
2156 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2159 pure_function (gfc_expr
*e
, const char **name
)
2165 if (e
->symtree
!= NULL
2166 && e
->symtree
->n
.sym
!= NULL
2167 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2168 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
2170 if (e
->value
.function
.esym
)
2172 pure
= gfc_pure (e
->value
.function
.esym
);
2173 *name
= e
->value
.function
.esym
->name
;
2175 else if (e
->value
.function
.isym
)
2177 pure
= e
->value
.function
.isym
->pure
2178 || e
->value
.function
.isym
->elemental
;
2179 *name
= e
->value
.function
.isym
->name
;
2183 /* Implicit functions are not pure. */
2185 *name
= e
->value
.function
.name
;
2193 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2194 int *f ATTRIBUTE_UNUSED
)
2198 /* Don't bother recursing into other statement functions
2199 since they will be checked individually for purity. */
2200 if (e
->expr_type
!= EXPR_FUNCTION
2202 || e
->symtree
->n
.sym
== sym
2203 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2206 return pure_function (e
, &name
) ? false : true;
2211 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
2213 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
2218 is_scalar_expr_ptr (gfc_expr
*expr
)
2220 gfc_try retval
= SUCCESS
;
2225 /* See if we have a gfc_ref, which means we have a substring, array
2226 reference, or a component. */
2227 if (expr
->ref
!= NULL
)
2230 while (ref
->next
!= NULL
)
2236 if (ref
->u
.ss
.length
!= NULL
2237 && ref
->u
.ss
.length
->length
!= NULL
2239 && ref
->u
.ss
.start
->expr_type
== EXPR_CONSTANT
2241 && ref
->u
.ss
.end
->expr_type
== EXPR_CONSTANT
)
2243 start
= (int) mpz_get_si (ref
->u
.ss
.start
->value
.integer
);
2244 end
= (int) mpz_get_si (ref
->u
.ss
.end
->value
.integer
);
2245 if (end
- start
+ 1 != 1)
2252 if (ref
->u
.ar
.type
== AR_ELEMENT
)
2254 else if (ref
->u
.ar
.type
== AR_FULL
)
2256 /* The user can give a full array if the array is of size 1. */
2257 if (ref
->u
.ar
.as
!= NULL
2258 && ref
->u
.ar
.as
->rank
== 1
2259 && ref
->u
.ar
.as
->type
== AS_EXPLICIT
2260 && ref
->u
.ar
.as
->lower
[0] != NULL
2261 && ref
->u
.ar
.as
->lower
[0]->expr_type
== EXPR_CONSTANT
2262 && ref
->u
.ar
.as
->upper
[0] != NULL
2263 && ref
->u
.ar
.as
->upper
[0]->expr_type
== EXPR_CONSTANT
)
2265 /* If we have a character string, we need to check if
2266 its length is one. */
2267 if (expr
->ts
.type
== BT_CHARACTER
)
2269 if (expr
->ts
.u
.cl
== NULL
2270 || expr
->ts
.u
.cl
->length
== NULL
2271 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1)
2277 /* We have constant lower and upper bounds. If the
2278 difference between is 1, it can be considered a
2280 start
= (int) mpz_get_si
2281 (ref
->u
.ar
.as
->lower
[0]->value
.integer
);
2282 end
= (int) mpz_get_si
2283 (ref
->u
.ar
.as
->upper
[0]->value
.integer
);
2284 if (end
- start
+ 1 != 1)
2299 else if (expr
->ts
.type
== BT_CHARACTER
&& expr
->rank
== 0)
2301 /* Character string. Make sure it's of length 1. */
2302 if (expr
->ts
.u
.cl
== NULL
2303 || expr
->ts
.u
.cl
->length
== NULL
2304 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
2307 else if (expr
->rank
!= 0)
2314 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2315 and, in the case of c_associated, set the binding label based on
2319 gfc_iso_c_func_interface (gfc_symbol
*sym
, gfc_actual_arglist
*args
,
2320 gfc_symbol
**new_sym
)
2322 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2323 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2324 int optional_arg
= 0, is_pointer
= 0;
2325 gfc_try retval
= SUCCESS
;
2326 gfc_symbol
*args_sym
;
2327 gfc_typespec
*arg_ts
;
2329 if (args
->expr
->expr_type
== EXPR_CONSTANT
2330 || args
->expr
->expr_type
== EXPR_OP
2331 || args
->expr
->expr_type
== EXPR_NULL
)
2333 gfc_error ("Argument to '%s' at %L is not a variable",
2334 sym
->name
, &(args
->expr
->where
));
2338 args_sym
= args
->expr
->symtree
->n
.sym
;
2340 /* The typespec for the actual arg should be that stored in the expr
2341 and not necessarily that of the expr symbol (args_sym), because
2342 the actual expression could be a part-ref of the expr symbol. */
2343 arg_ts
= &(args
->expr
->ts
);
2345 is_pointer
= gfc_is_data_pointer (args
->expr
);
2347 if (sym
->intmod_sym_id
== ISOCBINDING_ASSOCIATED
)
2349 /* If the user gave two args then they are providing something for
2350 the optional arg (the second cptr). Therefore, set the name and
2351 binding label to the c_associated for two cptrs. Otherwise,
2352 set c_associated to expect one cptr. */
2356 sprintf (name
, "%s_2", sym
->name
);
2357 sprintf (binding_label
, "%s_2", sym
->binding_label
);
2363 sprintf (name
, "%s_1", sym
->name
);
2364 sprintf (binding_label
, "%s_1", sym
->binding_label
);
2368 /* Get a new symbol for the version of c_associated that
2370 *new_sym
= get_iso_c_sym (sym
, name
, binding_label
, optional_arg
);
2372 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
2373 || sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2375 sprintf (name
, "%s", sym
->name
);
2376 sprintf (binding_label
, "%s", sym
->binding_label
);
2378 /* Error check the call. */
2379 if (args
->next
!= NULL
)
2381 gfc_error_now ("More actual than formal arguments in '%s' "
2382 "call at %L", name
, &(args
->expr
->where
));
2385 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
)
2387 /* Make sure we have either the target or pointer attribute. */
2388 if (!args_sym
->attr
.target
&& !is_pointer
)
2390 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2391 "a TARGET or an associated pointer",
2393 sym
->name
, &(args
->expr
->where
));
2397 /* See if we have interoperable type and type param. */
2398 if (verify_c_interop (arg_ts
) == SUCCESS
2399 || gfc_check_any_c_kind (arg_ts
) == SUCCESS
)
2401 if (args_sym
->attr
.target
== 1)
2403 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2404 has the target attribute and is interoperable. */
2405 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2406 allocatable variable that has the TARGET attribute and
2407 is not an array of zero size. */
2408 if (args_sym
->attr
.allocatable
== 1)
2410 if (args_sym
->attr
.dimension
!= 0
2411 && (args_sym
->as
&& args_sym
->as
->rank
== 0))
2413 gfc_error_now ("Allocatable variable '%s' used as a "
2414 "parameter to '%s' at %L must not be "
2415 "an array of zero size",
2416 args_sym
->name
, sym
->name
,
2417 &(args
->expr
->where
));
2423 /* A non-allocatable target variable with C
2424 interoperable type and type parameters must be
2426 if (args_sym
&& args_sym
->attr
.dimension
)
2428 if (args_sym
->as
->type
== AS_ASSUMED_SHAPE
)
2430 gfc_error ("Assumed-shape array '%s' at %L "
2431 "cannot be an argument to the "
2432 "procedure '%s' because "
2433 "it is not C interoperable",
2435 &(args
->expr
->where
), sym
->name
);
2438 else if (args_sym
->as
->type
== AS_DEFERRED
)
2440 gfc_error ("Deferred-shape array '%s' at %L "
2441 "cannot be an argument to the "
2442 "procedure '%s' because "
2443 "it is not C interoperable",
2445 &(args
->expr
->where
), sym
->name
);
2450 /* Make sure it's not a character string. Arrays of
2451 any type should be ok if the variable is of a C
2452 interoperable type. */
2453 if (arg_ts
->type
== BT_CHARACTER
)
2454 if (arg_ts
->u
.cl
!= NULL
2455 && (arg_ts
->u
.cl
->length
== NULL
2456 || arg_ts
->u
.cl
->length
->expr_type
2459 (arg_ts
->u
.cl
->length
->value
.integer
, 1)
2461 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2463 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2464 "at %L must have a length of 1",
2465 args_sym
->name
, sym
->name
,
2466 &(args
->expr
->where
));
2472 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2474 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2476 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2477 "associated scalar POINTER", args_sym
->name
,
2478 sym
->name
, &(args
->expr
->where
));
2484 /* The parameter is not required to be C interoperable. If it
2485 is not C interoperable, it must be a nonpolymorphic scalar
2486 with no length type parameters. It still must have either
2487 the pointer or target attribute, and it can be
2488 allocatable (but must be allocated when c_loc is called). */
2489 if (args
->expr
->rank
!= 0
2490 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2492 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2493 "scalar", args_sym
->name
, sym
->name
,
2494 &(args
->expr
->where
));
2497 else if (arg_ts
->type
== BT_CHARACTER
2498 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2500 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2501 "%L must have a length of 1",
2502 args_sym
->name
, sym
->name
,
2503 &(args
->expr
->where
));
2508 else if (sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2510 if (args_sym
->attr
.flavor
!= FL_PROCEDURE
)
2512 /* TODO: Update this error message to allow for procedure
2513 pointers once they are implemented. */
2514 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2516 args_sym
->name
, sym
->name
,
2517 &(args
->expr
->where
));
2520 else if (args_sym
->attr
.is_bind_c
!= 1)
2522 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2524 args_sym
->name
, sym
->name
,
2525 &(args
->expr
->where
));
2530 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2535 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2536 "iso_c_binding function: '%s'!\n", sym
->name
);
2543 /* Resolve a function call, which means resolving the arguments, then figuring
2544 out which entity the name refers to. */
2545 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2546 to INTENT(OUT) or INTENT(INOUT). */
2549 resolve_function (gfc_expr
*expr
)
2551 gfc_actual_arglist
*arg
;
2556 procedure_type p
= PROC_INTRINSIC
;
2557 bool no_formal_args
;
2561 sym
= expr
->symtree
->n
.sym
;
2563 /* If this is a procedure pointer component, it has already been resolved. */
2564 if (gfc_is_proc_ptr_comp (expr
, NULL
))
2567 if (sym
&& sym
->attr
.intrinsic
2568 && resolve_intrinsic (sym
, &expr
->where
) == FAILURE
)
2571 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
2573 gfc_error ("'%s' at %L is not a function", sym
->name
, &expr
->where
);
2577 /* If this ia a deferred TBP with an abstract interface (which may
2578 of course be referenced), expr->value.function.esym will be set. */
2579 if (sym
&& sym
->attr
.abstract
&& !expr
->value
.function
.esym
)
2581 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2582 sym
->name
, &expr
->where
);
2586 /* Switch off assumed size checking and do this again for certain kinds
2587 of procedure, once the procedure itself is resolved. */
2588 need_full_assumed_size
++;
2590 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
2591 p
= expr
->symtree
->n
.sym
->attr
.proc
;
2593 no_formal_args
= sym
&& is_external_proc (sym
) && sym
->formal
== NULL
;
2594 if (resolve_actual_arglist (expr
->value
.function
.actual
,
2595 p
, no_formal_args
) == FAILURE
)
2598 /* Need to setup the call to the correct c_associated, depending on
2599 the number of cptrs to user gives to compare. */
2600 if (sym
&& sym
->attr
.is_iso_c
== 1)
2602 if (gfc_iso_c_func_interface (sym
, expr
->value
.function
.actual
, &sym
)
2606 /* Get the symtree for the new symbol (resolved func).
2607 the old one will be freed later, when it's no longer used. */
2608 gfc_find_sym_tree (sym
->name
, sym
->ns
, 1, &(expr
->symtree
));
2611 /* Resume assumed_size checking. */
2612 need_full_assumed_size
--;
2614 /* If the procedure is external, check for usage. */
2615 if (sym
&& is_external_proc (sym
))
2616 resolve_global_procedure (sym
, &expr
->where
,
2617 &expr
->value
.function
.actual
, 0);
2619 if (sym
&& sym
->ts
.type
== BT_CHARACTER
2621 && sym
->ts
.u
.cl
->length
== NULL
2623 && expr
->value
.function
.esym
== NULL
2624 && !sym
->attr
.contained
)
2626 /* Internal procedures are taken care of in resolve_contained_fntype. */
2627 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2628 "be used at %L since it is not a dummy argument",
2629 sym
->name
, &expr
->where
);
2633 /* See if function is already resolved. */
2635 if (expr
->value
.function
.name
!= NULL
)
2637 if (expr
->ts
.type
== BT_UNKNOWN
)
2643 /* Apply the rules of section 14.1.2. */
2645 switch (procedure_kind (sym
))
2648 t
= resolve_generic_f (expr
);
2651 case PTYPE_SPECIFIC
:
2652 t
= resolve_specific_f (expr
);
2656 t
= resolve_unknown_f (expr
);
2660 gfc_internal_error ("resolve_function(): bad function type");
2664 /* If the expression is still a function (it might have simplified),
2665 then we check to see if we are calling an elemental function. */
2667 if (expr
->expr_type
!= EXPR_FUNCTION
)
2670 temp
= need_full_assumed_size
;
2671 need_full_assumed_size
= 0;
2673 if (resolve_elemental_actual (expr
, NULL
) == FAILURE
)
2676 if (omp_workshare_flag
2677 && expr
->value
.function
.esym
2678 && ! gfc_elemental (expr
->value
.function
.esym
))
2680 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2681 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
2686 #define GENERIC_ID expr->value.function.isym->id
2687 else if (expr
->value
.function
.actual
!= NULL
2688 && expr
->value
.function
.isym
!= NULL
2689 && GENERIC_ID
!= GFC_ISYM_LBOUND
2690 && GENERIC_ID
!= GFC_ISYM_LEN
2691 && GENERIC_ID
!= GFC_ISYM_LOC
2692 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
2694 /* Array intrinsics must also have the last upper bound of an
2695 assumed size array argument. UBOUND and SIZE have to be
2696 excluded from the check if the second argument is anything
2699 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
2701 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
2702 && arg
->next
!= NULL
&& arg
->next
->expr
)
2704 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
2707 if (arg
->next
->name
&& strncmp(arg
->next
->name
, "kind", 4) == 0)
2710 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
2715 if (arg
->expr
!= NULL
2716 && arg
->expr
->rank
> 0
2717 && resolve_assumed_size_actual (arg
->expr
))
2723 need_full_assumed_size
= temp
;
2726 if (!pure_function (expr
, &name
) && name
)
2730 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2731 "FORALL %s", name
, &expr
->where
,
2732 forall_flag
== 2 ? "mask" : "block");
2735 else if (gfc_pure (NULL
))
2737 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2738 "procedure within a PURE procedure", name
, &expr
->where
);
2743 /* Functions without the RECURSIVE attribution are not allowed to
2744 * call themselves. */
2745 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
2748 esym
= expr
->value
.function
.esym
;
2750 if (is_illegal_recursion (esym
, gfc_current_ns
))
2752 if (esym
->attr
.entry
&& esym
->ns
->entries
)
2753 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2754 " function '%s' is not RECURSIVE",
2755 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
2757 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2758 " is not RECURSIVE", esym
->name
, &expr
->where
);
2764 /* Character lengths of use associated functions may contains references to
2765 symbols not referenced from the current program unit otherwise. Make sure
2766 those symbols are marked as referenced. */
2768 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
2769 && expr
->value
.function
.esym
->attr
.use_assoc
)
2771 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
2775 && !((expr
->value
.function
.esym
2776 && expr
->value
.function
.esym
->attr
.elemental
)
2778 (expr
->value
.function
.isym
2779 && expr
->value
.function
.isym
->elemental
)))
2780 find_noncopying_intrinsics (expr
->value
.function
.esym
,
2781 expr
->value
.function
.actual
);
2783 /* Make sure that the expression has a typespec that works. */
2784 if (expr
->ts
.type
== BT_UNKNOWN
)
2786 if (expr
->symtree
->n
.sym
->result
2787 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
2788 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
2789 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
2796 /************* Subroutine resolution *************/
2799 pure_subroutine (gfc_code
*c
, gfc_symbol
*sym
)
2805 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2806 sym
->name
, &c
->loc
);
2807 else if (gfc_pure (NULL
))
2808 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym
->name
,
2814 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
2818 if (sym
->attr
.generic
)
2820 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
2823 c
->resolved_sym
= s
;
2824 pure_subroutine (c
, s
);
2828 /* TODO: Need to search for elemental references in generic interface. */
2831 if (sym
->attr
.intrinsic
)
2832 return gfc_intrinsic_sub_interface (c
, 0);
2839 resolve_generic_s (gfc_code
*c
)
2844 sym
= c
->symtree
->n
.sym
;
2848 m
= resolve_generic_s0 (c
, sym
);
2851 else if (m
== MATCH_ERROR
)
2855 if (sym
->ns
->parent
== NULL
)
2857 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2861 if (!generic_sym (sym
))
2865 /* Last ditch attempt. See if the reference is to an intrinsic
2866 that possesses a matching interface. 14.1.2.4 */
2867 sym
= c
->symtree
->n
.sym
;
2869 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
2871 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2872 sym
->name
, &c
->loc
);
2876 m
= gfc_intrinsic_sub_interface (c
, 0);
2880 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2881 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
2887 /* Set the name and binding label of the subroutine symbol in the call
2888 expression represented by 'c' to include the type and kind of the
2889 second parameter. This function is for resolving the appropriate
2890 version of c_f_pointer() and c_f_procpointer(). For example, a
2891 call to c_f_pointer() for a default integer pointer could have a
2892 name of c_f_pointer_i4. If no second arg exists, which is an error
2893 for these two functions, it defaults to the generic symbol's name
2894 and binding label. */
2897 set_name_and_label (gfc_code
*c
, gfc_symbol
*sym
,
2898 char *name
, char *binding_label
)
2900 gfc_expr
*arg
= NULL
;
2904 /* The second arg of c_f_pointer and c_f_procpointer determines
2905 the type and kind for the procedure name. */
2906 arg
= c
->ext
.actual
->next
->expr
;
2910 /* Set up the name to have the given symbol's name,
2911 plus the type and kind. */
2912 /* a derived type is marked with the type letter 'u' */
2913 if (arg
->ts
.type
== BT_DERIVED
)
2916 kind
= 0; /* set the kind as 0 for now */
2920 type
= gfc_type_letter (arg
->ts
.type
);
2921 kind
= arg
->ts
.kind
;
2924 if (arg
->ts
.type
== BT_CHARACTER
)
2925 /* Kind info for character strings not needed. */
2928 sprintf (name
, "%s_%c%d", sym
->name
, type
, kind
);
2929 /* Set up the binding label as the given symbol's label plus
2930 the type and kind. */
2931 sprintf (binding_label
, "%s_%c%d", sym
->binding_label
, type
, kind
);
2935 /* If the second arg is missing, set the name and label as
2936 was, cause it should at least be found, and the missing
2937 arg error will be caught by compare_parameters(). */
2938 sprintf (name
, "%s", sym
->name
);
2939 sprintf (binding_label
, "%s", sym
->binding_label
);
2946 /* Resolve a generic version of the iso_c_binding procedure given
2947 (sym) to the specific one based on the type and kind of the
2948 argument(s). Currently, this function resolves c_f_pointer() and
2949 c_f_procpointer based on the type and kind of the second argument
2950 (FPTR). Other iso_c_binding procedures aren't specially handled.
2951 Upon successfully exiting, c->resolved_sym will hold the resolved
2952 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2956 gfc_iso_c_sub_interface (gfc_code
*c
, gfc_symbol
*sym
)
2958 gfc_symbol
*new_sym
;
2959 /* this is fine, since we know the names won't use the max */
2960 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2961 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2962 /* default to success; will override if find error */
2963 match m
= MATCH_YES
;
2965 /* Make sure the actual arguments are in the necessary order (based on the
2966 formal args) before resolving. */
2967 gfc_procedure_use (sym
, &c
->ext
.actual
, &(c
->loc
));
2969 if ((sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
) ||
2970 (sym
->intmod_sym_id
== ISOCBINDING_F_PROCPOINTER
))
2972 set_name_and_label (c
, sym
, name
, binding_label
);
2974 if (sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
)
2976 if (c
->ext
.actual
!= NULL
&& c
->ext
.actual
->next
!= NULL
)
2978 /* Make sure we got a third arg if the second arg has non-zero
2979 rank. We must also check that the type and rank are
2980 correct since we short-circuit this check in
2981 gfc_procedure_use() (called above to sort actual args). */
2982 if (c
->ext
.actual
->next
->expr
->rank
!= 0)
2984 if(c
->ext
.actual
->next
->next
== NULL
2985 || c
->ext
.actual
->next
->next
->expr
== NULL
)
2988 gfc_error ("Missing SHAPE parameter for call to %s "
2989 "at %L", sym
->name
, &(c
->loc
));
2991 else if (c
->ext
.actual
->next
->next
->expr
->ts
.type
2993 || c
->ext
.actual
->next
->next
->expr
->rank
!= 1)
2996 gfc_error ("SHAPE parameter for call to %s at %L must "
2997 "be a rank 1 INTEGER array", sym
->name
,
3004 if (m
!= MATCH_ERROR
)
3006 /* the 1 means to add the optional arg to formal list */
3007 new_sym
= get_iso_c_sym (sym
, name
, binding_label
, 1);
3009 /* for error reporting, say it's declared where the original was */
3010 new_sym
->declared_at
= sym
->declared_at
;
3015 /* no differences for c_loc or c_funloc */
3019 /* set the resolved symbol */
3020 if (m
!= MATCH_ERROR
)
3021 c
->resolved_sym
= new_sym
;
3023 c
->resolved_sym
= sym
;
3029 /* Resolve a subroutine call known to be specific. */
3032 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
3036 if(sym
->attr
.is_iso_c
)
3038 m
= gfc_iso_c_sub_interface (c
,sym
);
3042 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
3044 if (sym
->attr
.dummy
)
3046 sym
->attr
.proc
= PROC_DUMMY
;
3050 sym
->attr
.proc
= PROC_EXTERNAL
;
3054 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
3057 if (sym
->attr
.intrinsic
)
3059 m
= gfc_intrinsic_sub_interface (c
, 1);
3063 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3064 "with an intrinsic", sym
->name
, &c
->loc
);
3072 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3074 c
->resolved_sym
= sym
;
3075 pure_subroutine (c
, sym
);
3082 resolve_specific_s (gfc_code
*c
)
3087 sym
= c
->symtree
->n
.sym
;
3091 m
= resolve_specific_s0 (c
, sym
);
3094 if (m
== MATCH_ERROR
)
3097 if (sym
->ns
->parent
== NULL
)
3100 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3106 sym
= c
->symtree
->n
.sym
;
3107 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3108 sym
->name
, &c
->loc
);
3114 /* Resolve a subroutine call not known to be generic nor specific. */
3117 resolve_unknown_s (gfc_code
*c
)
3121 sym
= c
->symtree
->n
.sym
;
3123 if (sym
->attr
.dummy
)
3125 sym
->attr
.proc
= PROC_DUMMY
;
3129 /* See if we have an intrinsic function reference. */
3131 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3133 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3138 /* The reference is to an external name. */
3141 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3143 c
->resolved_sym
= sym
;
3145 pure_subroutine (c
, sym
);
3151 /* Resolve a subroutine call. Although it was tempting to use the same code
3152 for functions, subroutines and functions are stored differently and this
3153 makes things awkward. */
3156 resolve_call (gfc_code
*c
)
3159 procedure_type ptype
= PROC_INTRINSIC
;
3160 gfc_symbol
*csym
, *sym
;
3161 bool no_formal_args
;
3163 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3165 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3167 gfc_error ("'%s' at %L has a type, which is not consistent with "
3168 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3172 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3175 gfc_find_sym_tree (csym
->name
, gfc_current_ns
, 1, &st
);
3176 sym
= st
? st
->n
.sym
: NULL
;
3177 if (sym
&& csym
!= sym
3178 && sym
->ns
== gfc_current_ns
3179 && sym
->attr
.flavor
== FL_PROCEDURE
3180 && sym
->attr
.contained
)
3183 if (csym
->attr
.generic
)
3184 c
->symtree
->n
.sym
= sym
;
3187 csym
= c
->symtree
->n
.sym
;
3191 /* If this ia a deferred TBP with an abstract interface
3192 (which may of course be referenced), c->expr1 will be set. */
3193 if (csym
&& csym
->attr
.abstract
&& !c
->expr1
)
3195 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3196 csym
->name
, &c
->loc
);
3200 /* Subroutines without the RECURSIVE attribution are not allowed to
3201 * call themselves. */
3202 if (csym
&& is_illegal_recursion (csym
, gfc_current_ns
))
3204 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3205 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3206 " subroutine '%s' is not RECURSIVE",
3207 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3209 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3210 " is not RECURSIVE", csym
->name
, &c
->loc
);
3215 /* Switch off assumed size checking and do this again for certain kinds
3216 of procedure, once the procedure itself is resolved. */
3217 need_full_assumed_size
++;
3220 ptype
= csym
->attr
.proc
;
3222 no_formal_args
= csym
&& is_external_proc (csym
) && csym
->formal
== NULL
;
3223 if (resolve_actual_arglist (c
->ext
.actual
, ptype
,
3224 no_formal_args
) == FAILURE
)
3227 /* Resume assumed_size checking. */
3228 need_full_assumed_size
--;
3230 /* If external, check for usage. */
3231 if (csym
&& is_external_proc (csym
))
3232 resolve_global_procedure (csym
, &c
->loc
, &c
->ext
.actual
, 1);
3235 if (c
->resolved_sym
== NULL
)
3237 c
->resolved_isym
= NULL
;
3238 switch (procedure_kind (csym
))
3241 t
= resolve_generic_s (c
);
3244 case PTYPE_SPECIFIC
:
3245 t
= resolve_specific_s (c
);
3249 t
= resolve_unknown_s (c
);
3253 gfc_internal_error ("resolve_subroutine(): bad function type");
3257 /* Some checks of elemental subroutine actual arguments. */
3258 if (resolve_elemental_actual (NULL
, c
) == FAILURE
)
3261 if (t
== SUCCESS
&& !(c
->resolved_sym
&& c
->resolved_sym
->attr
.elemental
))
3262 find_noncopying_intrinsics (c
->resolved_sym
, c
->ext
.actual
);
3267 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3268 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3269 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3270 if their shapes do not match. If either op1->shape or op2->shape is
3271 NULL, return SUCCESS. */
3274 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3281 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3283 for (i
= 0; i
< op1
->rank
; i
++)
3285 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3287 gfc_error ("Shapes for operands at %L and %L are not conformable",
3288 &op1
->where
, &op2
->where
);
3299 /* Resolve an operator expression node. This can involve replacing the
3300 operation with a user defined function call. */
3303 resolve_operator (gfc_expr
*e
)
3305 gfc_expr
*op1
, *op2
;
3307 bool dual_locus_error
;
3310 /* Resolve all subnodes-- give them types. */
3312 switch (e
->value
.op
.op
)
3315 if (gfc_resolve_expr (e
->value
.op
.op2
) == FAILURE
)
3318 /* Fall through... */
3321 case INTRINSIC_UPLUS
:
3322 case INTRINSIC_UMINUS
:
3323 case INTRINSIC_PARENTHESES
:
3324 if (gfc_resolve_expr (e
->value
.op
.op1
) == FAILURE
)
3329 /* Typecheck the new node. */
3331 op1
= e
->value
.op
.op1
;
3332 op2
= e
->value
.op
.op2
;
3333 dual_locus_error
= false;
3335 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
3336 || (op2
&& op2
->expr_type
== EXPR_NULL
))
3338 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
3342 switch (e
->value
.op
.op
)
3344 case INTRINSIC_UPLUS
:
3345 case INTRINSIC_UMINUS
:
3346 if (op1
->ts
.type
== BT_INTEGER
3347 || op1
->ts
.type
== BT_REAL
3348 || op1
->ts
.type
== BT_COMPLEX
)
3354 sprintf (msg
, _("Operand of unary numeric operator '%s' at %%L is %s"),
3355 gfc_op2string (e
->value
.op
.op
), gfc_typename (&e
->ts
));
3358 case INTRINSIC_PLUS
:
3359 case INTRINSIC_MINUS
:
3360 case INTRINSIC_TIMES
:
3361 case INTRINSIC_DIVIDE
:
3362 case INTRINSIC_POWER
:
3363 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3365 gfc_type_convert_binary (e
, 1);
3370 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3371 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3372 gfc_typename (&op2
->ts
));
3375 case INTRINSIC_CONCAT
:
3376 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3377 && op1
->ts
.kind
== op2
->ts
.kind
)
3379 e
->ts
.type
= BT_CHARACTER
;
3380 e
->ts
.kind
= op1
->ts
.kind
;
3385 _("Operands of string concatenation operator at %%L are %s/%s"),
3386 gfc_typename (&op1
->ts
), gfc_typename (&op2
->ts
));
3392 case INTRINSIC_NEQV
:
3393 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3395 e
->ts
.type
= BT_LOGICAL
;
3396 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
3397 if (op1
->ts
.kind
< e
->ts
.kind
)
3398 gfc_convert_type (op1
, &e
->ts
, 2);
3399 else if (op2
->ts
.kind
< e
->ts
.kind
)
3400 gfc_convert_type (op2
, &e
->ts
, 2);
3404 sprintf (msg
, _("Operands of logical operator '%s' at %%L are %s/%s"),
3405 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3406 gfc_typename (&op2
->ts
));
3411 if (op1
->ts
.type
== BT_LOGICAL
)
3413 e
->ts
.type
= BT_LOGICAL
;
3414 e
->ts
.kind
= op1
->ts
.kind
;
3418 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
3419 gfc_typename (&op1
->ts
));
3423 case INTRINSIC_GT_OS
:
3425 case INTRINSIC_GE_OS
:
3427 case INTRINSIC_LT_OS
:
3429 case INTRINSIC_LE_OS
:
3430 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
3432 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
3436 /* Fall through... */
3439 case INTRINSIC_EQ_OS
:
3441 case INTRINSIC_NE_OS
:
3442 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3443 && op1
->ts
.kind
== op2
->ts
.kind
)
3445 e
->ts
.type
= BT_LOGICAL
;
3446 e
->ts
.kind
= gfc_default_logical_kind
;
3450 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3452 gfc_type_convert_binary (e
, 1);
3454 e
->ts
.type
= BT_LOGICAL
;
3455 e
->ts
.kind
= gfc_default_logical_kind
;
3459 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3461 _("Logicals at %%L must be compared with %s instead of %s"),
3462 (e
->value
.op
.op
== INTRINSIC_EQ
3463 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
3464 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
3467 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3468 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3469 gfc_typename (&op2
->ts
));
3473 case INTRINSIC_USER
:
3474 if (e
->value
.op
.uop
->op
== NULL
)
3475 sprintf (msg
, _("Unknown operator '%s' at %%L"), e
->value
.op
.uop
->name
);
3476 else if (op2
== NULL
)
3477 sprintf (msg
, _("Operand of user operator '%s' at %%L is %s"),
3478 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
));
3480 sprintf (msg
, _("Operands of user operator '%s' at %%L are %s/%s"),
3481 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
),
3482 gfc_typename (&op2
->ts
));
3486 case INTRINSIC_PARENTHESES
:
3488 if (e
->ts
.type
== BT_CHARACTER
)
3489 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
3493 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3496 /* Deal with arrayness of an operand through an operator. */
3500 switch (e
->value
.op
.op
)
3502 case INTRINSIC_PLUS
:
3503 case INTRINSIC_MINUS
:
3504 case INTRINSIC_TIMES
:
3505 case INTRINSIC_DIVIDE
:
3506 case INTRINSIC_POWER
:
3507 case INTRINSIC_CONCAT
:
3511 case INTRINSIC_NEQV
:
3513 case INTRINSIC_EQ_OS
:
3515 case INTRINSIC_NE_OS
:
3517 case INTRINSIC_GT_OS
:
3519 case INTRINSIC_GE_OS
:
3521 case INTRINSIC_LT_OS
:
3523 case INTRINSIC_LE_OS
:
3525 if (op1
->rank
== 0 && op2
->rank
== 0)
3528 if (op1
->rank
== 0 && op2
->rank
!= 0)
3530 e
->rank
= op2
->rank
;
3532 if (e
->shape
== NULL
)
3533 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
3536 if (op1
->rank
!= 0 && op2
->rank
== 0)
3538 e
->rank
= op1
->rank
;
3540 if (e
->shape
== NULL
)
3541 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3544 if (op1
->rank
!= 0 && op2
->rank
!= 0)
3546 if (op1
->rank
== op2
->rank
)
3548 e
->rank
= op1
->rank
;
3549 if (e
->shape
== NULL
)
3551 t
= compare_shapes(op1
, op2
);
3555 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3560 /* Allow higher level expressions to work. */
3563 /* Try user-defined operators, and otherwise throw an error. */
3564 dual_locus_error
= true;
3566 _("Inconsistent ranks for operator at %%L and %%L"));
3573 case INTRINSIC_PARENTHESES
:
3575 case INTRINSIC_UPLUS
:
3576 case INTRINSIC_UMINUS
:
3577 /* Simply copy arrayness attribute */
3578 e
->rank
= op1
->rank
;
3580 if (e
->shape
== NULL
)
3581 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3589 /* Attempt to simplify the expression. */
3592 t
= gfc_simplify_expr (e
, 0);
3593 /* Some calls do not succeed in simplification and return FAILURE
3594 even though there is no error; e.g. variable references to
3595 PARAMETER arrays. */
3596 if (!gfc_is_constant_expr (e
))
3605 if (gfc_extend_expr (e
, &real_error
) == SUCCESS
)
3612 if (dual_locus_error
)
3613 gfc_error (msg
, &op1
->where
, &op2
->where
);
3615 gfc_error (msg
, &e
->where
);
3621 /************** Array resolution subroutines **************/
3624 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
}
3627 /* Compare two integer expressions. */
3630 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
3634 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
3635 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
3638 /* If either of the types isn't INTEGER, we must have
3639 raised an error earlier. */
3641 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
3644 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
3654 /* Compare an integer expression with an integer. */
3657 compare_bound_int (gfc_expr
*a
, int b
)
3661 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
3664 if (a
->ts
.type
!= BT_INTEGER
)
3665 gfc_internal_error ("compare_bound_int(): Bad expression");
3667 i
= mpz_cmp_si (a
->value
.integer
, b
);
3677 /* Compare an integer expression with a mpz_t. */
3680 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
3684 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
3687 if (a
->ts
.type
!= BT_INTEGER
)
3688 gfc_internal_error ("compare_bound_int(): Bad expression");
3690 i
= mpz_cmp (a
->value
.integer
, b
);
3700 /* Compute the last value of a sequence given by a triplet.
3701 Return 0 if it wasn't able to compute the last value, or if the
3702 sequence if empty, and 1 otherwise. */
3705 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
3706 gfc_expr
*stride
, mpz_t last
)
3710 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
3711 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
3712 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
3715 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
3716 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
3719 if (stride
== NULL
|| compare_bound_int(stride
, 1) == CMP_EQ
)
3721 if (compare_bound (start
, end
) == CMP_GT
)
3723 mpz_set (last
, end
->value
.integer
);
3727 if (compare_bound_int (stride
, 0) == CMP_GT
)
3729 /* Stride is positive */
3730 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
3735 /* Stride is negative */
3736 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
3741 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
3742 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
3743 mpz_sub (last
, end
->value
.integer
, rem
);
3750 /* Compare a single dimension of an array reference to the array
3754 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
3758 /* Given start, end and stride values, calculate the minimum and
3759 maximum referenced indexes. */
3761 switch (ar
->dimen_type
[i
])
3767 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
3769 gfc_warning ("Array reference at %L is out of bounds "
3770 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3771 mpz_get_si (ar
->start
[i
]->value
.integer
),
3772 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3775 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
3777 gfc_warning ("Array reference at %L is out of bounds "
3778 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3779 mpz_get_si (ar
->start
[i
]->value
.integer
),
3780 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3788 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3789 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3791 comparison comp_start_end
= compare_bound (AR_START
, AR_END
);
3793 /* Check for zero stride, which is not allowed. */
3794 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
3796 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
3800 /* if start == len || (stride > 0 && start < len)
3801 || (stride < 0 && start > len),
3802 then the array section contains at least one element. In this
3803 case, there is an out-of-bounds access if
3804 (start < lower || start > upper). */
3805 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
3806 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
3807 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
3808 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
3809 && comp_start_end
== CMP_GT
))
3811 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
3813 gfc_warning ("Lower array reference at %L is out of bounds "
3814 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3815 mpz_get_si (AR_START
->value
.integer
),
3816 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3819 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
3821 gfc_warning ("Lower array reference at %L is out of bounds "
3822 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3823 mpz_get_si (AR_START
->value
.integer
),
3824 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3829 /* If we can compute the highest index of the array section,
3830 then it also has to be between lower and upper. */
3831 mpz_init (last_value
);
3832 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
3835 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
3837 gfc_warning ("Upper array reference at %L is out of bounds "
3838 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3839 mpz_get_si (last_value
),
3840 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3841 mpz_clear (last_value
);
3844 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
3846 gfc_warning ("Upper array reference at %L is out of bounds "
3847 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3848 mpz_get_si (last_value
),
3849 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3850 mpz_clear (last_value
);
3854 mpz_clear (last_value
);
3862 gfc_internal_error ("check_dimension(): Bad array reference");
3869 /* Compare an array reference with an array specification. */
3872 compare_spec_to_ref (gfc_array_ref
*ar
)
3879 /* TODO: Full array sections are only allowed as actual parameters. */
3880 if (as
->type
== AS_ASSUMED_SIZE
3881 && (/*ar->type == AR_FULL
3882 ||*/ (ar
->type
== AR_SECTION
3883 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
3885 gfc_error ("Rightmost upper bound of assumed size array section "
3886 "not specified at %L", &ar
->where
);
3890 if (ar
->type
== AR_FULL
)
3893 if (as
->rank
!= ar
->dimen
)
3895 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3896 &ar
->where
, ar
->dimen
, as
->rank
);
3900 for (i
= 0; i
< as
->rank
; i
++)
3901 if (check_dimension (i
, ar
, as
) == FAILURE
)
3908 /* Resolve one part of an array index. */
3911 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
3918 if (gfc_resolve_expr (index
) == FAILURE
)
3921 if (check_scalar
&& index
->rank
!= 0)
3923 gfc_error ("Array index at %L must be scalar", &index
->where
);
3927 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
3929 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3930 &index
->where
, gfc_basic_typename (index
->ts
.type
));
3934 if (index
->ts
.type
== BT_REAL
)
3935 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: REAL array index at %L",
3936 &index
->where
) == FAILURE
)
3939 if (index
->ts
.kind
!= gfc_index_integer_kind
3940 || index
->ts
.type
!= BT_INTEGER
)
3943 ts
.type
= BT_INTEGER
;
3944 ts
.kind
= gfc_index_integer_kind
;
3946 gfc_convert_type_warn (index
, &ts
, 2, 0);
3952 /* Resolve a dim argument to an intrinsic function. */
3955 gfc_resolve_dim_arg (gfc_expr
*dim
)
3960 if (gfc_resolve_expr (dim
) == FAILURE
)
3965 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
3970 if (dim
->ts
.type
!= BT_INTEGER
)
3972 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
3976 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
3981 ts
.type
= BT_INTEGER
;
3982 ts
.kind
= gfc_index_integer_kind
;
3984 gfc_convert_type_warn (dim
, &ts
, 2, 0);
3990 /* Given an expression that contains array references, update those array
3991 references to point to the right array specifications. While this is
3992 filled in during matching, this information is difficult to save and load
3993 in a module, so we take care of it here.
3995 The idea here is that the original array reference comes from the
3996 base symbol. We traverse the list of reference structures, setting
3997 the stored reference to references. Component references can
3998 provide an additional array specification. */
4001 find_array_spec (gfc_expr
*e
)
4005 gfc_symbol
*derived
;
4008 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
4009 as
= e
->symtree
->n
.sym
->ts
.u
.derived
->components
->as
;
4011 as
= e
->symtree
->n
.sym
->as
;
4014 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4019 gfc_internal_error ("find_array_spec(): Missing spec");
4026 if (derived
== NULL
)
4027 derived
= e
->symtree
->n
.sym
->ts
.u
.derived
;
4029 if (derived
->attr
.is_class
)
4030 derived
= derived
->components
->ts
.u
.derived
;
4032 c
= derived
->components
;
4034 for (; c
; c
= c
->next
)
4035 if (c
== ref
->u
.c
.component
)
4037 /* Track the sequence of component references. */
4038 if (c
->ts
.type
== BT_DERIVED
)
4039 derived
= c
->ts
.u
.derived
;
4044 gfc_internal_error ("find_array_spec(): Component not found");
4046 if (c
->attr
.dimension
)
4049 gfc_internal_error ("find_array_spec(): unused as(1)");
4060 gfc_internal_error ("find_array_spec(): unused as(2)");
4064 /* Resolve an array reference. */
4067 resolve_array_ref (gfc_array_ref
*ar
)
4069 int i
, check_scalar
;
4072 for (i
= 0; i
< ar
->dimen
; i
++)
4074 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4076 if (gfc_resolve_index (ar
->start
[i
], check_scalar
) == FAILURE
)
4078 if (gfc_resolve_index (ar
->end
[i
], check_scalar
) == FAILURE
)
4080 if (gfc_resolve_index (ar
->stride
[i
], check_scalar
) == FAILURE
)
4085 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4089 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4093 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4094 if (e
->expr_type
== EXPR_VARIABLE
4095 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4096 ar
->start
[i
] = gfc_get_parentheses (e
);
4100 gfc_error ("Array index at %L is an array of rank %d",
4101 &ar
->c_where
[i
], e
->rank
);
4106 /* If the reference type is unknown, figure out what kind it is. */
4108 if (ar
->type
== AR_UNKNOWN
)
4110 ar
->type
= AR_ELEMENT
;
4111 for (i
= 0; i
< ar
->dimen
; i
++)
4112 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4113 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
4115 ar
->type
= AR_SECTION
;
4120 if (!ar
->as
->cray_pointee
&& compare_spec_to_ref (ar
) == FAILURE
)
4128 resolve_substring (gfc_ref
*ref
)
4130 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4132 if (ref
->u
.ss
.start
!= NULL
)
4134 if (gfc_resolve_expr (ref
->u
.ss
.start
) == FAILURE
)
4137 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
4139 gfc_error ("Substring start index at %L must be of type INTEGER",
4140 &ref
->u
.ss
.start
->where
);
4144 if (ref
->u
.ss
.start
->rank
!= 0)
4146 gfc_error ("Substring start index at %L must be scalar",
4147 &ref
->u
.ss
.start
->where
);
4151 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
4152 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4153 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4155 gfc_error ("Substring start index at %L is less than one",
4156 &ref
->u
.ss
.start
->where
);
4161 if (ref
->u
.ss
.end
!= NULL
)
4163 if (gfc_resolve_expr (ref
->u
.ss
.end
) == FAILURE
)
4166 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
4168 gfc_error ("Substring end index at %L must be of type INTEGER",
4169 &ref
->u
.ss
.end
->where
);
4173 if (ref
->u
.ss
.end
->rank
!= 0)
4175 gfc_error ("Substring end index at %L must be scalar",
4176 &ref
->u
.ss
.end
->where
);
4180 if (ref
->u
.ss
.length
!= NULL
4181 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
4182 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4183 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4185 gfc_error ("Substring end index at %L exceeds the string length",
4186 &ref
->u
.ss
.start
->where
);
4190 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
4191 gfc_integer_kinds
[k
].huge
) == CMP_GT
4192 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4193 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4195 gfc_error ("Substring end index at %L is too large",
4196 &ref
->u
.ss
.end
->where
);
4205 /* This function supplies missing substring charlens. */
4208 gfc_resolve_substring_charlen (gfc_expr
*e
)
4211 gfc_expr
*start
, *end
;
4213 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
4214 if (char_ref
->type
== REF_SUBSTRING
)
4220 gcc_assert (char_ref
->next
== NULL
);
4224 if (e
->ts
.u
.cl
->length
)
4225 gfc_free_expr (e
->ts
.u
.cl
->length
);
4226 else if (e
->expr_type
== EXPR_VARIABLE
4227 && e
->symtree
->n
.sym
->attr
.dummy
)
4231 e
->ts
.type
= BT_CHARACTER
;
4232 e
->ts
.kind
= gfc_default_character_kind
;
4235 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4237 if (char_ref
->u
.ss
.start
)
4238 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
4240 start
= gfc_int_expr (1);
4242 if (char_ref
->u
.ss
.end
)
4243 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
4244 else if (e
->expr_type
== EXPR_VARIABLE
)
4245 end
= gfc_copy_expr (e
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4252 /* Length = (end - start +1). */
4253 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
4254 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
, gfc_int_expr (1));
4256 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4257 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4259 /* Make sure that the length is simplified. */
4260 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
4261 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4265 /* Resolve subtype references. */
4268 resolve_ref (gfc_expr
*expr
)
4270 int current_part_dimension
, n_components
, seen_part_dimension
;
4273 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4274 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
4276 find_array_spec (expr
);
4280 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4284 if (resolve_array_ref (&ref
->u
.ar
) == FAILURE
)
4292 resolve_substring (ref
);
4296 /* Check constraints on part references. */
4298 current_part_dimension
= 0;
4299 seen_part_dimension
= 0;
4302 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4307 switch (ref
->u
.ar
.type
)
4311 current_part_dimension
= 1;
4315 current_part_dimension
= 0;
4319 gfc_internal_error ("resolve_ref(): Bad array reference");
4325 if (current_part_dimension
|| seen_part_dimension
)
4328 if (ref
->u
.c
.component
->attr
.pointer
4329 || ref
->u
.c
.component
->attr
.proc_pointer
)
4331 gfc_error ("Component to the right of a part reference "
4332 "with nonzero rank must not have the POINTER "
4333 "attribute at %L", &expr
->where
);
4336 else if (ref
->u
.c
.component
->attr
.allocatable
)
4338 gfc_error ("Component to the right of a part reference "
4339 "with nonzero rank must not have the ALLOCATABLE "
4340 "attribute at %L", &expr
->where
);
4352 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
4353 || ref
->next
== NULL
)
4354 && current_part_dimension
4355 && seen_part_dimension
)
4357 gfc_error ("Two or more part references with nonzero rank must "
4358 "not be specified at %L", &expr
->where
);
4362 if (ref
->type
== REF_COMPONENT
)
4364 if (current_part_dimension
)
4365 seen_part_dimension
= 1;
4367 /* reset to make sure */
4368 current_part_dimension
= 0;
4376 /* Given an expression, determine its shape. This is easier than it sounds.
4377 Leaves the shape array NULL if it is not possible to determine the shape. */
4380 expression_shape (gfc_expr
*e
)
4382 mpz_t array
[GFC_MAX_DIMENSIONS
];
4385 if (e
->rank
== 0 || e
->shape
!= NULL
)
4388 for (i
= 0; i
< e
->rank
; i
++)
4389 if (gfc_array_dimen_size (e
, i
, &array
[i
]) == FAILURE
)
4392 e
->shape
= gfc_get_shape (e
->rank
);
4394 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
4399 for (i
--; i
>= 0; i
--)
4400 mpz_clear (array
[i
]);
4404 /* Given a variable expression node, compute the rank of the expression by
4405 examining the base symbol and any reference structures it may have. */
4408 expression_rank (gfc_expr
*e
)
4413 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4414 could lead to serious confusion... */
4415 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
4419 if (e
->expr_type
== EXPR_ARRAY
)
4421 /* Constructors can have a rank different from one via RESHAPE(). */
4423 if (e
->symtree
== NULL
)
4429 e
->rank
= (e
->symtree
->n
.sym
->as
== NULL
)
4430 ? 0 : e
->symtree
->n
.sym
->as
->rank
;
4436 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4438 if (ref
->type
!= REF_ARRAY
)
4441 if (ref
->u
.ar
.type
== AR_FULL
)
4443 rank
= ref
->u
.ar
.as
->rank
;
4447 if (ref
->u
.ar
.type
== AR_SECTION
)
4449 /* Figure out the rank of the section. */
4451 gfc_internal_error ("expression_rank(): Two array specs");
4453 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
4454 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
4455 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
4465 expression_shape (e
);
4469 /* Resolve a variable expression. */
4472 resolve_variable (gfc_expr
*e
)
4479 if (e
->symtree
== NULL
)
4482 if (e
->ref
&& resolve_ref (e
) == FAILURE
)
4485 sym
= e
->symtree
->n
.sym
;
4486 if (sym
->attr
.flavor
== FL_PROCEDURE
4487 && (!sym
->attr
.function
4488 || (sym
->attr
.function
&& sym
->result
4489 && sym
->result
->attr
.proc_pointer
4490 && !sym
->result
->attr
.function
)))
4492 e
->ts
.type
= BT_PROCEDURE
;
4493 goto resolve_procedure
;
4496 if (sym
->ts
.type
!= BT_UNKNOWN
)
4497 gfc_variable_attr (e
, &e
->ts
);
4500 /* Must be a simple variable reference. */
4501 if (gfc_set_default_type (sym
, 1, sym
->ns
) == FAILURE
)
4506 if (check_assumed_size_reference (sym
, e
))
4509 /* Deal with forward references to entries during resolve_code, to
4510 satisfy, at least partially, 12.5.2.5. */
4511 if (gfc_current_ns
->entries
4512 && current_entry_id
== sym
->entry_id
4515 && cs_base
->current
->op
!= EXEC_ENTRY
)
4517 gfc_entry_list
*entry
;
4518 gfc_formal_arglist
*formal
;
4522 /* If the symbol is a dummy... */
4523 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
4525 entry
= gfc_current_ns
->entries
;
4528 /* ...test if the symbol is a parameter of previous entries. */
4529 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
4530 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
4532 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
4536 /* If it has not been seen as a dummy, this is an error. */
4539 if (specification_expr
)
4540 gfc_error ("Variable '%s', used in a specification expression"
4541 ", is referenced at %L before the ENTRY statement "
4542 "in which it is a parameter",
4543 sym
->name
, &cs_base
->current
->loc
);
4545 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4546 "statement in which it is a parameter",
4547 sym
->name
, &cs_base
->current
->loc
);
4552 /* Now do the same check on the specification expressions. */
4553 specification_expr
= 1;
4554 if (sym
->ts
.type
== BT_CHARACTER
4555 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
4559 for (n
= 0; n
< sym
->as
->rank
; n
++)
4561 specification_expr
= 1;
4562 if (gfc_resolve_expr (sym
->as
->lower
[n
]) == FAILURE
)
4564 specification_expr
= 1;
4565 if (gfc_resolve_expr (sym
->as
->upper
[n
]) == FAILURE
)
4568 specification_expr
= 0;
4571 /* Update the symbol's entry level. */
4572 sym
->entry_id
= current_entry_id
+ 1;
4576 if (t
== SUCCESS
&& resolve_procedure_expression (e
) == FAILURE
)
4583 /* Checks to see that the correct symbol has been host associated.
4584 The only situation where this arises is that in which a twice
4585 contained function is parsed after the host association is made.
4586 Therefore, on detecting this, change the symbol in the expression
4587 and convert the array reference into an actual arglist if the old
4588 symbol is a variable. */
4590 check_host_association (gfc_expr
*e
)
4592 gfc_symbol
*sym
, *old_sym
;
4596 gfc_actual_arglist
*arg
, *tail
= NULL
;
4597 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
4599 /* If the expression is the result of substitution in
4600 interface.c(gfc_extend_expr) because there is no way in
4601 which the host association can be wrong. */
4602 if (e
->symtree
== NULL
4603 || e
->symtree
->n
.sym
== NULL
4604 || e
->user_operator
)
4607 old_sym
= e
->symtree
->n
.sym
;
4609 if (gfc_current_ns
->parent
4610 && old_sym
->ns
!= gfc_current_ns
)
4612 /* Use the 'USE' name so that renamed module symbols are
4613 correctly handled. */
4614 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
4616 if (sym
&& old_sym
!= sym
4617 && sym
->ts
.type
== old_sym
->ts
.type
4618 && sym
->attr
.flavor
== FL_PROCEDURE
4619 && sym
->attr
.contained
)
4621 /* Clear the shape, since it might not be valid. */
4622 if (e
->shape
!= NULL
)
4624 for (n
= 0; n
< e
->rank
; n
++)
4625 mpz_clear (e
->shape
[n
]);
4627 gfc_free (e
->shape
);
4630 /* Give the expression the right symtree! */
4631 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
4632 gcc_assert (st
!= NULL
);
4634 if (old_sym
->attr
.flavor
== FL_PROCEDURE
4635 || e
->expr_type
== EXPR_FUNCTION
)
4637 /* Original was function so point to the new symbol, since
4638 the actual argument list is already attached to the
4640 e
->value
.function
.esym
= NULL
;
4645 /* Original was variable so convert array references into
4646 an actual arglist. This does not need any checking now
4647 since gfc_resolve_function will take care of it. */
4648 e
->value
.function
.actual
= NULL
;
4649 e
->expr_type
= EXPR_FUNCTION
;
4652 /* Ambiguity will not arise if the array reference is not
4653 the last reference. */
4654 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4655 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
4658 gcc_assert (ref
->type
== REF_ARRAY
);
4660 /* Grab the start expressions from the array ref and
4661 copy them into actual arguments. */
4662 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
4664 arg
= gfc_get_actual_arglist ();
4665 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
4666 if (e
->value
.function
.actual
== NULL
)
4667 tail
= e
->value
.function
.actual
= arg
;
4675 /* Dump the reference list and set the rank. */
4676 gfc_free_ref_list (e
->ref
);
4678 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
4681 gfc_resolve_expr (e
);
4685 /* This might have changed! */
4686 return e
->expr_type
== EXPR_FUNCTION
;
4691 gfc_resolve_character_operator (gfc_expr
*e
)
4693 gfc_expr
*op1
= e
->value
.op
.op1
;
4694 gfc_expr
*op2
= e
->value
.op
.op2
;
4695 gfc_expr
*e1
= NULL
;
4696 gfc_expr
*e2
= NULL
;
4698 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
4700 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
4701 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
4702 else if (op1
->expr_type
== EXPR_CONSTANT
)
4703 e1
= gfc_int_expr (op1
->value
.character
.length
);
4705 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
4706 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
4707 else if (op2
->expr_type
== EXPR_CONSTANT
)
4708 e2
= gfc_int_expr (op2
->value
.character
.length
);
4710 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4715 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
4716 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4717 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4718 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
4719 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4725 /* Ensure that an character expression has a charlen and, if possible, a
4726 length expression. */
4729 fixup_charlen (gfc_expr
*e
)
4731 /* The cases fall through so that changes in expression type and the need
4732 for multiple fixes are picked up. In all circumstances, a charlen should
4733 be available for the middle end to hang a backend_decl on. */
4734 switch (e
->expr_type
)
4737 gfc_resolve_character_operator (e
);
4740 if (e
->expr_type
== EXPR_ARRAY
)
4741 gfc_resolve_character_array_constructor (e
);
4743 case EXPR_SUBSTRING
:
4744 if (!e
->ts
.u
.cl
&& e
->ref
)
4745 gfc_resolve_substring_charlen (e
);
4749 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4756 /* Update an actual argument to include the passed-object for type-bound
4757 procedures at the right position. */
4759 static gfc_actual_arglist
*
4760 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
4763 gcc_assert (argpos
> 0);
4767 gfc_actual_arglist
* result
;
4769 result
= gfc_get_actual_arglist ();
4773 result
->name
= name
;
4779 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
4781 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
4786 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4789 extract_compcall_passed_object (gfc_expr
* e
)
4793 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4795 if (e
->value
.compcall
.base_object
)
4796 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
4799 po
= gfc_get_expr ();
4800 po
->expr_type
= EXPR_VARIABLE
;
4801 po
->symtree
= e
->symtree
;
4802 po
->ref
= gfc_copy_ref (e
->ref
);
4803 po
->where
= e
->where
;
4806 if (gfc_resolve_expr (po
) == FAILURE
)
4813 /* Update the arglist of an EXPR_COMPCALL expression to include the
4817 update_compcall_arglist (gfc_expr
* e
)
4820 gfc_typebound_proc
* tbp
;
4822 tbp
= e
->value
.compcall
.tbp
;
4827 po
= extract_compcall_passed_object (e
);
4831 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
4837 gcc_assert (tbp
->pass_arg_num
> 0);
4838 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
4846 /* Extract the passed object from a PPC call (a copy of it). */
4849 extract_ppc_passed_object (gfc_expr
*e
)
4854 po
= gfc_get_expr ();
4855 po
->expr_type
= EXPR_VARIABLE
;
4856 po
->symtree
= e
->symtree
;
4857 po
->ref
= gfc_copy_ref (e
->ref
);
4858 po
->where
= e
->where
;
4860 /* Remove PPC reference. */
4862 while ((*ref
)->next
)
4863 ref
= &(*ref
)->next
;
4864 gfc_free_ref_list (*ref
);
4867 if (gfc_resolve_expr (po
) == FAILURE
)
4874 /* Update the actual arglist of a procedure pointer component to include the
4878 update_ppc_arglist (gfc_expr
* e
)
4882 gfc_typebound_proc
* tb
;
4884 if (!gfc_is_proc_ptr_comp (e
, &ppc
))
4891 else if (tb
->nopass
)
4894 po
= extract_ppc_passed_object (e
);
4900 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
4904 gcc_assert (tb
->pass_arg_num
> 0);
4905 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
4913 /* Check that the object a TBP is called on is valid, i.e. it must not be
4914 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4917 check_typebound_baseobject (gfc_expr
* e
)
4921 base
= extract_compcall_passed_object (e
);
4925 gcc_assert (base
->ts
.type
== BT_DERIVED
|| base
->ts
.type
== BT_CLASS
);
4927 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
4929 gfc_error ("Base object for type-bound procedure call at %L is of"
4930 " ABSTRACT type '%s'", &e
->where
, base
->ts
.u
.derived
->name
);
4934 /* If the procedure called is NOPASS, the base object must be scalar. */
4935 if (e
->value
.compcall
.tbp
->nopass
&& base
->rank
> 0)
4937 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
4938 " be scalar", &e
->where
);
4942 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
4945 gfc_error ("Non-scalar base object at %L currently not implemented",
4954 /* Resolve a call to a type-bound procedure, either function or subroutine,
4955 statically from the data in an EXPR_COMPCALL expression. The adapted
4956 arglist and the target-procedure symtree are returned. */
4959 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
4960 gfc_actual_arglist
** actual
)
4962 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4963 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
4965 /* Update the actual arglist for PASS. */
4966 if (update_compcall_arglist (e
) == FAILURE
)
4969 *actual
= e
->value
.compcall
.actual
;
4970 *target
= e
->value
.compcall
.tbp
->u
.specific
;
4972 gfc_free_ref_list (e
->ref
);
4974 e
->value
.compcall
.actual
= NULL
;
4980 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4981 which of the specific bindings (if any) matches the arglist and transform
4982 the expression into a call of that binding. */
4985 resolve_typebound_generic_call (gfc_expr
* e
)
4987 gfc_typebound_proc
* genproc
;
4988 const char* genname
;
4990 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4991 genname
= e
->value
.compcall
.name
;
4992 genproc
= e
->value
.compcall
.tbp
;
4994 if (!genproc
->is_generic
)
4997 /* Try the bindings on this type and in the inheritance hierarchy. */
4998 for (; genproc
; genproc
= genproc
->overridden
)
5002 gcc_assert (genproc
->is_generic
);
5003 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
5006 gfc_actual_arglist
* args
;
5009 gcc_assert (g
->specific
);
5011 if (g
->specific
->error
)
5014 target
= g
->specific
->u
.specific
->n
.sym
;
5016 /* Get the right arglist by handling PASS/NOPASS. */
5017 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
5018 if (!g
->specific
->nopass
)
5021 po
= extract_compcall_passed_object (e
);
5025 gcc_assert (g
->specific
->pass_arg_num
> 0);
5026 gcc_assert (!g
->specific
->error
);
5027 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
5028 g
->specific
->pass_arg
);
5030 resolve_actual_arglist (args
, target
->attr
.proc
,
5031 is_external_proc (target
) && !target
->formal
);
5033 /* Check if this arglist matches the formal. */
5034 matches
= gfc_arglist_matches_symbol (&args
, target
);
5036 /* Clean up and break out of the loop if we've found it. */
5037 gfc_free_actual_arglist (args
);
5040 e
->value
.compcall
.tbp
= g
->specific
;
5046 /* Nothing matching found! */
5047 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5048 " '%s' at %L", genname
, &e
->where
);
5056 /* Resolve a call to a type-bound subroutine. */
5059 resolve_typebound_call (gfc_code
* c
)
5061 gfc_actual_arglist
* newactual
;
5062 gfc_symtree
* target
;
5064 /* Check that's really a SUBROUTINE. */
5065 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
5067 gfc_error ("'%s' at %L should be a SUBROUTINE",
5068 c
->expr1
->value
.compcall
.name
, &c
->loc
);
5072 if (check_typebound_baseobject (c
->expr1
) == FAILURE
)
5075 if (resolve_typebound_generic_call (c
->expr1
) == FAILURE
)
5078 /* Transform into an ordinary EXEC_CALL for now. */
5080 if (resolve_typebound_static (c
->expr1
, &target
, &newactual
) == FAILURE
)
5083 c
->ext
.actual
= newactual
;
5084 c
->symtree
= target
;
5085 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
5087 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
5089 gfc_free_expr (c
->expr1
);
5090 c
->expr1
= gfc_get_expr ();
5091 c
->expr1
->expr_type
= EXPR_FUNCTION
;
5092 c
->expr1
->symtree
= target
;
5093 c
->expr1
->where
= c
->loc
;
5095 return resolve_call (c
);
5099 /* Resolve a component-call expression. This originally was intended
5100 only to see functions. However, it is convenient to use it in
5101 resolving subroutine class methods, since we do not have to add a
5102 gfc_code each time. */
5104 resolve_compcall (gfc_expr
* e
, bool fcn
, bool class_members
)
5106 gfc_actual_arglist
* newactual
;
5107 gfc_symtree
* target
;
5109 /* Check that's really a FUNCTION. */
5110 if (fcn
&& !e
->value
.compcall
.tbp
->function
)
5112 gfc_error ("'%s' at %L should be a FUNCTION",
5113 e
->value
.compcall
.name
, &e
->where
);
5116 else if (!fcn
&& !e
->value
.compcall
.tbp
->subroutine
)
5118 /* To resolve class member calls, we borrow this bit
5119 of code to select the specific procedures. */
5120 gfc_error ("'%s' at %L should be a SUBROUTINE",
5121 e
->value
.compcall
.name
, &e
->where
);
5125 /* These must not be assign-calls! */
5126 gcc_assert (!e
->value
.compcall
.assign
);
5128 if (check_typebound_baseobject (e
) == FAILURE
)
5131 if (resolve_typebound_generic_call (e
) == FAILURE
)
5133 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5135 /* Take the rank from the function's symbol. */
5136 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
5137 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
5139 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5140 arglist to the TBP's binding target. */
5142 if (resolve_typebound_static (e
, &target
, &newactual
) == FAILURE
)
5145 e
->value
.function
.actual
= newactual
;
5146 e
->value
.function
.name
= NULL
;
5147 e
->value
.function
.esym
= target
->n
.sym
;
5148 e
->value
.function
.class_esym
= NULL
;
5149 e
->value
.function
.isym
= NULL
;
5150 e
->symtree
= target
;
5151 e
->ts
= target
->n
.sym
->ts
;
5152 e
->expr_type
= EXPR_FUNCTION
;
5154 /* Resolution is not necessary when constructing component calls
5155 for class members, since this must only be done for the
5156 declared type, which is done afterwards. */
5157 return !class_members
? gfc_resolve_expr (e
) : SUCCESS
;
5161 /* Resolve a typebound call for the members in a class. This group of
5162 functions implements dynamic dispatch in the provisional version
5163 of f03 OOP. As soon as vtables are in place and contain pointers
5164 to methods, this will no longer be necessary. */
5165 static gfc_expr
*list_e
;
5166 static void check_class_members (gfc_symbol
*);
5167 static gfc_try class_try
;
5168 static bool fcn_flag
;
5172 check_members (gfc_symbol
*derived
)
5174 if (derived
->attr
.flavor
== FL_DERIVED
)
5175 check_class_members (derived
);
5180 check_class_members (gfc_symbol
*derived
)
5184 gfc_class_esym_list
*etmp
;
5186 e
= gfc_copy_expr (list_e
);
5188 tbp
= gfc_find_typebound_proc (derived
, &class_try
,
5189 e
->value
.compcall
.name
,
5194 gfc_error ("no typebound available procedure named '%s' at %L",
5195 e
->value
.compcall
.name
, &e
->where
);
5199 /* If we have to match a passed class member, force the actual
5200 expression to have the correct type. */
5201 if (!tbp
->n
.tb
->nopass
)
5203 if (e
->value
.compcall
.base_object
== NULL
)
5204 e
->value
.compcall
.base_object
= extract_compcall_passed_object (e
);
5206 if (!derived
->attr
.abstract
)
5208 e
->value
.compcall
.base_object
->ts
.type
= BT_DERIVED
;
5209 e
->value
.compcall
.base_object
->ts
.u
.derived
= derived
;
5213 e
->value
.compcall
.tbp
= tbp
->n
.tb
;
5214 e
->value
.compcall
.name
= tbp
->name
;
5216 /* Let the original expresssion catch the assertion in
5217 resolve_compcall, since this flag does not appear to be reset or
5218 copied in some systems. */
5219 e
->value
.compcall
.assign
= 0;
5221 /* Do the renaming, PASSing, generic => specific and other
5222 good things for each class member. */
5223 class_try
= (resolve_compcall (e
, fcn_flag
, true) == SUCCESS
)
5224 ? class_try
: FAILURE
;
5226 /* Now transfer the found symbol to the esym list. */
5227 if (class_try
== SUCCESS
)
5229 etmp
= list_e
->value
.function
.class_esym
;
5230 list_e
->value
.function
.class_esym
5231 = gfc_get_class_esym_list();
5232 list_e
->value
.function
.class_esym
->next
= etmp
;
5233 list_e
->value
.function
.class_esym
->derived
= derived
;
5234 list_e
->value
.function
.class_esym
->esym
5235 = e
->value
.function
.esym
;
5240 /* Burrow down into grandchildren types. */
5241 if (derived
->f2k_derived
)
5242 gfc_traverse_ns (derived
->f2k_derived
, check_members
);
5246 /* Eliminate esym_lists where all the members point to the
5247 typebound procedure of the declared type; ie. one where
5248 type selection has no effect.. */
5250 resolve_class_esym (gfc_expr
*e
)
5252 gfc_class_esym_list
*p
, *q
;
5255 gcc_assert (e
&& e
->expr_type
== EXPR_FUNCTION
);
5257 p
= e
->value
.function
.class_esym
;
5261 for (; p
; p
= p
->next
)
5262 empty
= empty
&& (e
->value
.function
.esym
== p
->esym
);
5266 p
= e
->value
.function
.class_esym
;
5272 e
->value
.function
.class_esym
= NULL
;
5277 /* Generate an expression for the hash value, given the reference to
5278 the class of the final expression (class_ref), the base of the
5279 full reference list (new_ref), the declared type and the class
5282 hash_value_expr (gfc_ref
*class_ref
, gfc_ref
*new_ref
, gfc_symtree
*st
)
5284 gfc_expr
*hash_value
;
5286 /* Build an expression for the correct hash_value; ie. that of the last
5290 class_ref
->next
= NULL
;
5294 gfc_free_ref_list (new_ref
);
5297 hash_value
= gfc_get_expr ();
5298 hash_value
->expr_type
= EXPR_VARIABLE
;
5299 hash_value
->symtree
= st
;
5300 hash_value
->symtree
->n
.sym
->refs
++;
5301 hash_value
->ref
= new_ref
;
5302 gfc_add_component_ref (hash_value
, "$vptr");
5303 gfc_add_component_ref (hash_value
, "$hash");
5309 /* Get the ultimate declared type from an expression. In addition,
5310 return the last class/derived type reference and the copy of the
5313 get_declared_from_expr (gfc_ref
**class_ref
, gfc_ref
**new_ref
,
5316 gfc_symbol
*declared
;
5321 *new_ref
= gfc_copy_ref (e
->ref
);
5322 for (ref
= *new_ref
; ref
; ref
= ref
->next
)
5324 if (ref
->type
!= REF_COMPONENT
)
5327 if (ref
->u
.c
.component
->ts
.type
== BT_CLASS
5328 || ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5330 declared
= ref
->u
.c
.component
->ts
.u
.derived
;
5335 if (declared
== NULL
)
5336 declared
= e
->symtree
->n
.sym
->ts
.u
.derived
;
5342 /* Resolve the argument expressions so that any arguments expressions
5343 that include class methods are resolved before the current call.
5344 This is necessary because of the static variables used in CLASS
5345 method resolution. */
5347 resolve_arg_exprs (gfc_actual_arglist
*arg
)
5349 /* Resolve the actual arglist expressions. */
5350 for (; arg
; arg
= arg
->next
)
5353 gfc_resolve_expr (arg
->expr
);
5358 /* Resolve a typebound function, or 'method'. First separate all
5359 the non-CLASS references by calling resolve_compcall directly.
5360 Then treat the CLASS references by resolving for each of the class
5364 resolve_typebound_function (gfc_expr
* e
)
5366 gfc_symbol
*derived
, *declared
;
5373 return resolve_compcall (e
, true, false);
5375 /* Get the CLASS declared type. */
5376 declared
= get_declared_from_expr (&class_ref
, &new_ref
, e
);
5378 /* Weed out cases of the ultimate component being a derived type. */
5379 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5380 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5382 gfc_free_ref_list (new_ref
);
5383 return resolve_compcall (e
, true, false);
5386 /* Resolve the argument expressions, */
5387 resolve_arg_exprs (e
->value
.function
.actual
);
5389 /* Get the data component, which is of the declared type. */
5390 derived
= declared
->components
->ts
.u
.derived
;
5392 /* Resolve the function call for each member of the class. */
5393 class_try
= SUCCESS
;
5395 list_e
= gfc_copy_expr (e
);
5396 check_class_members (derived
);
5398 class_try
= (resolve_compcall (e
, true, false) == SUCCESS
)
5399 ? class_try
: FAILURE
;
5401 /* Transfer the class list to the original expression. Note that
5402 the class_esym list is cleaned up in trans-expr.c, as the calls
5404 e
->value
.function
.class_esym
= list_e
->value
.function
.class_esym
;
5405 list_e
->value
.function
.class_esym
= NULL
;
5406 gfc_free_expr (list_e
);
5408 resolve_class_esym (e
);
5410 /* More than one typebound procedure so transmit an expression for
5411 the hash_value as the selector. */
5412 if (e
->value
.function
.class_esym
!= NULL
)
5413 e
->value
.function
.class_esym
->hash_value
5414 = hash_value_expr (class_ref
, new_ref
, st
);
5419 /* Resolve a typebound subroutine, or 'method'. First separate all
5420 the non-CLASS references by calling resolve_typebound_call directly.
5421 Then treat the CLASS references by resolving for each of the class
5425 resolve_typebound_subroutine (gfc_code
*code
)
5427 gfc_symbol
*derived
, *declared
;
5432 st
= code
->expr1
->symtree
;
5434 return resolve_typebound_call (code
);
5436 /* Get the CLASS declared type. */
5437 declared
= get_declared_from_expr (&class_ref
, &new_ref
, code
->expr1
);
5439 /* Weed out cases of the ultimate component being a derived type. */
5440 if ((class_ref
&& class_ref
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5441 || (!class_ref
&& st
->n
.sym
->ts
.type
!= BT_CLASS
))
5443 gfc_free_ref_list (new_ref
);
5444 return resolve_typebound_call (code
);
5447 /* Resolve the argument expressions, */
5448 resolve_arg_exprs (code
->expr1
->value
.compcall
.actual
);
5450 /* Get the data component, which is of the declared type. */
5451 derived
= declared
->components
->ts
.u
.derived
;
5453 class_try
= SUCCESS
;
5455 list_e
= gfc_copy_expr (code
->expr1
);
5456 check_class_members (derived
);
5458 class_try
= (resolve_typebound_call (code
) == SUCCESS
)
5459 ? class_try
: FAILURE
;
5461 /* Transfer the class list to the original expression. Note that
5462 the class_esym list is cleaned up in trans-expr.c, as the calls
5464 code
->expr1
->value
.function
.class_esym
5465 = list_e
->value
.function
.class_esym
;
5466 list_e
->value
.function
.class_esym
= NULL
;
5467 gfc_free_expr (list_e
);
5469 resolve_class_esym (code
->expr1
);
5471 /* More than one typebound procedure so transmit an expression for
5472 the hash_value as the selector. */
5473 if (code
->expr1
->value
.function
.class_esym
!= NULL
)
5474 code
->expr1
->value
.function
.class_esym
->hash_value
5475 = hash_value_expr (class_ref
, new_ref
, st
);
5481 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5484 resolve_ppc_call (gfc_code
* c
)
5486 gfc_component
*comp
;
5489 b
= gfc_is_proc_ptr_comp (c
->expr1
, &comp
);
5492 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
5493 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5495 if (!comp
->attr
.subroutine
)
5496 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
5498 if (resolve_ref (c
->expr1
) == FAILURE
)
5501 if (update_ppc_arglist (c
->expr1
) == FAILURE
)
5504 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
5506 if (resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
5507 comp
->formal
== NULL
) == FAILURE
)
5510 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
5516 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5519 resolve_expr_ppc (gfc_expr
* e
)
5521 gfc_component
*comp
;
5524 b
= gfc_is_proc_ptr_comp (e
, &comp
);
5527 /* Convert to EXPR_FUNCTION. */
5528 e
->expr_type
= EXPR_FUNCTION
;
5529 e
->value
.function
.isym
= NULL
;
5530 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
5532 if (comp
->as
!= NULL
)
5533 e
->rank
= comp
->as
->rank
;
5535 if (!comp
->attr
.function
)
5536 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
5538 if (resolve_ref (e
) == FAILURE
)
5541 if (resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
5542 comp
->formal
== NULL
) == FAILURE
)
5545 if (update_ppc_arglist (e
) == FAILURE
)
5548 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
5555 gfc_is_expandable_expr (gfc_expr
*e
)
5557 gfc_constructor
*con
;
5559 if (e
->expr_type
== EXPR_ARRAY
)
5561 /* Traverse the constructor looking for variables that are flavor
5562 parameter. Parameters must be expanded since they are fully used at
5564 for (con
= e
->value
.constructor
; con
; con
= con
->next
)
5566 if (con
->expr
->expr_type
== EXPR_VARIABLE
5567 && con
->expr
->symtree
5568 && (con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
5569 || con
->expr
->symtree
->n
.sym
->attr
.flavor
== FL_VARIABLE
))
5571 if (con
->expr
->expr_type
== EXPR_ARRAY
5572 && gfc_is_expandable_expr (con
->expr
))
5580 /* Resolve an expression. That is, make sure that types of operands agree
5581 with their operators, intrinsic operators are converted to function calls
5582 for overloaded types and unresolved function references are resolved. */
5585 gfc_resolve_expr (gfc_expr
*e
)
5592 switch (e
->expr_type
)
5595 t
= resolve_operator (e
);
5601 if (check_host_association (e
))
5602 t
= resolve_function (e
);
5605 t
= resolve_variable (e
);
5607 expression_rank (e
);
5610 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
5611 && e
->ref
->type
!= REF_SUBSTRING
)
5612 gfc_resolve_substring_charlen (e
);
5617 t
= resolve_typebound_function (e
);
5620 case EXPR_SUBSTRING
:
5621 t
= resolve_ref (e
);
5630 t
= resolve_expr_ppc (e
);
5635 if (resolve_ref (e
) == FAILURE
)
5638 t
= gfc_resolve_array_constructor (e
);
5639 /* Also try to expand a constructor. */
5642 expression_rank (e
);
5643 if (gfc_is_constant_expr (e
) || gfc_is_expandable_expr (e
))
5644 gfc_expand_constructor (e
);
5647 /* This provides the opportunity for the length of constructors with
5648 character valued function elements to propagate the string length
5649 to the expression. */
5650 if (t
== SUCCESS
&& e
->ts
.type
== BT_CHARACTER
)
5652 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5653 here rather then add a duplicate test for it above. */
5654 gfc_expand_constructor (e
);
5655 t
= gfc_resolve_character_array_constructor (e
);
5660 case EXPR_STRUCTURE
:
5661 t
= resolve_ref (e
);
5665 t
= resolve_structure_cons (e
);
5669 t
= gfc_simplify_expr (e
, 0);
5673 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5676 if (e
->ts
.type
== BT_CHARACTER
&& t
== SUCCESS
&& !e
->ts
.u
.cl
)
5683 /* Resolve an expression from an iterator. They must be scalar and have
5684 INTEGER or (optionally) REAL type. */
5687 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
5688 const char *name_msgid
)
5690 if (gfc_resolve_expr (expr
) == FAILURE
)
5693 if (expr
->rank
!= 0)
5695 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
5699 if (expr
->ts
.type
!= BT_INTEGER
)
5701 if (expr
->ts
.type
== BT_REAL
)
5704 return gfc_notify_std (GFC_STD_F95_DEL
,
5705 "Deleted feature: %s at %L must be integer",
5706 _(name_msgid
), &expr
->where
);
5709 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
5716 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
5724 /* Resolve the expressions in an iterator structure. If REAL_OK is
5725 false allow only INTEGER type iterators, otherwise allow REAL types. */
5728 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
)
5730 if (gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable")
5734 if (gfc_pure (NULL
) && gfc_impure_variable (iter
->var
->symtree
->n
.sym
))
5736 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5741 if (gfc_resolve_iterator_expr (iter
->start
, real_ok
,
5742 "Start expression in DO loop") == FAILURE
)
5745 if (gfc_resolve_iterator_expr (iter
->end
, real_ok
,
5746 "End expression in DO loop") == FAILURE
)
5749 if (gfc_resolve_iterator_expr (iter
->step
, real_ok
,
5750 "Step expression in DO loop") == FAILURE
)
5753 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
5755 if ((iter
->step
->ts
.type
== BT_INTEGER
5756 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
5757 || (iter
->step
->ts
.type
== BT_REAL
5758 && mpfr_sgn (iter
->step
->value
.real
) == 0))
5760 gfc_error ("Step expression in DO loop at %L cannot be zero",
5761 &iter
->step
->where
);
5766 /* Convert start, end, and step to the same type as var. */
5767 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
5768 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
5769 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
5771 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
5772 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
5773 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
5775 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
5776 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
5777 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 2);
5779 if (iter
->start
->expr_type
== EXPR_CONSTANT
5780 && iter
->end
->expr_type
== EXPR_CONSTANT
5781 && iter
->step
->expr_type
== EXPR_CONSTANT
)
5784 if (iter
->start
->ts
.type
== BT_INTEGER
)
5786 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
5787 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
5791 sgn
= mpfr_sgn (iter
->step
->value
.real
);
5792 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
5794 if ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0))
5795 gfc_warning ("DO loop at %L will be executed zero times",
5796 &iter
->step
->where
);
5803 /* Traversal function for find_forall_index. f == 2 signals that
5804 that variable itself is not to be checked - only the references. */
5807 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
5809 if (expr
->expr_type
!= EXPR_VARIABLE
)
5812 /* A scalar assignment */
5813 if (!expr
->ref
|| *f
== 1)
5815 if (expr
->symtree
->n
.sym
== sym
)
5827 /* Check whether the FORALL index appears in the expression or not.
5828 Returns SUCCESS if SYM is found in EXPR. */
5831 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
5833 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
5840 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5841 to be a scalar INTEGER variable. The subscripts and stride are scalar
5842 INTEGERs, and if stride is a constant it must be nonzero.
5843 Furthermore "A subscript or stride in a forall-triplet-spec shall
5844 not contain a reference to any index-name in the
5845 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5848 resolve_forall_iterators (gfc_forall_iterator
*it
)
5850 gfc_forall_iterator
*iter
, *iter2
;
5852 for (iter
= it
; iter
; iter
= iter
->next
)
5854 if (gfc_resolve_expr (iter
->var
) == SUCCESS
5855 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
5856 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5859 if (gfc_resolve_expr (iter
->start
) == SUCCESS
5860 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
5861 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5862 &iter
->start
->where
);
5863 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
5864 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
5866 if (gfc_resolve_expr (iter
->end
) == SUCCESS
5867 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
5868 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5870 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
5871 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
5873 if (gfc_resolve_expr (iter
->stride
) == SUCCESS
)
5875 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
5876 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5877 &iter
->stride
->where
, "INTEGER");
5879 if (iter
->stride
->expr_type
== EXPR_CONSTANT
5880 && mpz_cmp_ui(iter
->stride
->value
.integer
, 0) == 0)
5881 gfc_error ("FORALL stride expression at %L cannot be zero",
5882 &iter
->stride
->where
);
5884 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
5885 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 2);
5888 for (iter
= it
; iter
; iter
= iter
->next
)
5889 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
5891 if (find_forall_index (iter2
->start
,
5892 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
5893 || find_forall_index (iter2
->end
,
5894 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
5895 || find_forall_index (iter2
->stride
,
5896 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
)
5897 gfc_error ("FORALL index '%s' may not appear in triplet "
5898 "specification at %L", iter
->var
->symtree
->name
,
5899 &iter2
->start
->where
);
5904 /* Given a pointer to a symbol that is a derived type, see if it's
5905 inaccessible, i.e. if it's defined in another module and the components are
5906 PRIVATE. The search is recursive if necessary. Returns zero if no
5907 inaccessible components are found, nonzero otherwise. */
5910 derived_inaccessible (gfc_symbol
*sym
)
5914 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
5917 for (c
= sym
->components
; c
; c
= c
->next
)
5919 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
5927 /* Resolve the argument of a deallocate expression. The expression must be
5928 a pointer or a full array. */
5931 resolve_deallocate_expr (gfc_expr
*e
)
5933 symbol_attribute attr
;
5934 int allocatable
, pointer
, check_intent_in
;
5939 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5940 check_intent_in
= 1;
5942 if (gfc_resolve_expr (e
) == FAILURE
)
5945 if (e
->expr_type
!= EXPR_VARIABLE
)
5948 sym
= e
->symtree
->n
.sym
;
5950 if (sym
->ts
.type
== BT_CLASS
)
5952 allocatable
= sym
->ts
.u
.derived
->components
->attr
.allocatable
;
5953 pointer
= sym
->ts
.u
.derived
->components
->attr
.pointer
;
5957 allocatable
= sym
->attr
.allocatable
;
5958 pointer
= sym
->attr
.pointer
;
5960 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5963 check_intent_in
= 0;
5968 if (ref
->u
.ar
.type
!= AR_FULL
)
5973 c
= ref
->u
.c
.component
;
5974 if (c
->ts
.type
== BT_CLASS
)
5976 allocatable
= c
->ts
.u
.derived
->components
->attr
.allocatable
;
5977 pointer
= c
->ts
.u
.derived
->components
->attr
.pointer
;
5981 allocatable
= c
->attr
.allocatable
;
5982 pointer
= c
->attr
.pointer
;
5992 attr
= gfc_expr_attr (e
);
5994 if (allocatable
== 0 && attr
.pointer
== 0)
5997 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6001 if (check_intent_in
&& sym
->attr
.intent
== INTENT_IN
)
6003 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
6004 sym
->name
, &e
->where
);
6008 if (e
->ts
.type
== BT_CLASS
)
6010 /* Only deallocate the DATA component. */
6011 gfc_add_component_ref (e
, "$data");
6018 /* Returns true if the expression e contains a reference to the symbol sym. */
6020 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
6022 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
6029 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
6031 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
6035 /* Given the expression node e for an allocatable/pointer of derived type to be
6036 allocated, get the expression node to be initialized afterwards (needed for
6037 derived types with default initializers, and derived types with allocatable
6038 components that need nullification.) */
6041 gfc_expr_to_initialize (gfc_expr
*e
)
6047 result
= gfc_copy_expr (e
);
6049 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6050 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
6051 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
6053 ref
->u
.ar
.type
= AR_FULL
;
6055 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
6056 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
6058 result
->rank
= ref
->u
.ar
.dimen
;
6066 /* Used in resolve_allocate_expr to check that a allocation-object and
6067 a source-expr are conformable. This does not catch all possible
6068 cases; in particular a runtime checking is needed. */
6071 conformable_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
6073 /* First compare rank. */
6074 if (e2
->ref
&& e1
->rank
!= e2
->ref
->u
.ar
.as
->rank
)
6076 gfc_error ("Source-expr at %L must be scalar or have the "
6077 "same rank as the allocate-object at %L",
6078 &e1
->where
, &e2
->where
);
6089 for (i
= 0; i
< e1
->rank
; i
++)
6091 if (e2
->ref
->u
.ar
.end
[i
])
6093 mpz_set (s
, e2
->ref
->u
.ar
.end
[i
]->value
.integer
);
6094 mpz_sub (s
, s
, e2
->ref
->u
.ar
.start
[i
]->value
.integer
);
6095 mpz_add_ui (s
, s
, 1);
6099 mpz_set (s
, e2
->ref
->u
.ar
.start
[i
]->value
.integer
);
6102 if (mpz_cmp (e1
->shape
[i
], s
) != 0)
6104 gfc_error ("Source-expr at %L and allocate-object at %L must "
6105 "have the same shape", &e1
->where
, &e2
->where
);
6118 /* Resolve the expression in an ALLOCATE statement, doing the additional
6119 checks to see whether the expression is OK or not. The expression must
6120 have a trailing array reference that gives the size of the array. */
6123 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
)
6125 int i
, pointer
, allocatable
, dimension
, check_intent_in
, is_abstract
;
6126 symbol_attribute attr
;
6127 gfc_ref
*ref
, *ref2
;
6134 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6135 check_intent_in
= 1;
6137 if (gfc_resolve_expr (e
) == FAILURE
)
6140 /* Make sure the expression is allocatable or a pointer. If it is
6141 pointer, the next-to-last reference must be a pointer. */
6145 sym
= e
->symtree
->n
.sym
;
6147 /* Check whether ultimate component is abstract and CLASS. */
6150 if (e
->expr_type
!= EXPR_VARIABLE
)
6153 attr
= gfc_expr_attr (e
);
6154 pointer
= attr
.pointer
;
6155 dimension
= attr
.dimension
;
6159 if (sym
->ts
.type
== BT_CLASS
)
6161 allocatable
= sym
->ts
.u
.derived
->components
->attr
.allocatable
;
6162 pointer
= sym
->ts
.u
.derived
->components
->attr
.pointer
;
6163 dimension
= sym
->ts
.u
.derived
->components
->attr
.dimension
;
6164 is_abstract
= sym
->ts
.u
.derived
->components
->attr
.abstract
;
6168 allocatable
= sym
->attr
.allocatable
;
6169 pointer
= sym
->attr
.pointer
;
6170 dimension
= sym
->attr
.dimension
;
6173 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
6176 check_intent_in
= 0;
6181 if (ref
->next
!= NULL
)
6186 c
= ref
->u
.c
.component
;
6187 if (c
->ts
.type
== BT_CLASS
)
6189 allocatable
= c
->ts
.u
.derived
->components
->attr
.allocatable
;
6190 pointer
= c
->ts
.u
.derived
->components
->attr
.pointer
;
6191 dimension
= c
->ts
.u
.derived
->components
->attr
.dimension
;
6192 is_abstract
= c
->ts
.u
.derived
->components
->attr
.abstract
;
6196 allocatable
= c
->attr
.allocatable
;
6197 pointer
= c
->attr
.pointer
;
6198 dimension
= c
->attr
.dimension
;
6199 is_abstract
= c
->attr
.abstract
;
6211 if (allocatable
== 0 && pointer
== 0)
6213 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6218 /* Some checks for the SOURCE tag. */
6221 /* Check F03:C631. */
6222 if (!gfc_type_compatible (&e
->ts
, &code
->expr3
->ts
))
6224 gfc_error ("Type of entity at %L is type incompatible with "
6225 "source-expr at %L", &e
->where
, &code
->expr3
->where
);
6229 /* Check F03:C632 and restriction following Note 6.18. */
6230 if (code
->expr3
->rank
> 0
6231 && conformable_arrays (code
->expr3
, e
) == FAILURE
)
6234 /* Check F03:C633. */
6235 if (code
->expr3
->ts
.kind
!= e
->ts
.kind
)
6237 gfc_error ("The allocate-object at %L and the source-expr at %L "
6238 "shall have the same kind type parameter",
6239 &e
->where
, &code
->expr3
->where
);
6243 else if (is_abstract
&& code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
)
6245 gcc_assert (e
->ts
.type
== BT_CLASS
);
6246 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6247 "type-spec or SOURCE=", sym
->name
, &e
->where
);
6251 if (check_intent_in
&& sym
->attr
.intent
== INTENT_IN
)
6253 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6254 sym
->name
, &e
->where
);
6260 /* Add default initializer for those derived types that need them. */
6261 if (e
->ts
.type
== BT_DERIVED
6262 && (init_e
= gfc_default_initializer (&e
->ts
)))
6264 gfc_code
*init_st
= gfc_get_code ();
6265 init_st
->loc
= code
->loc
;
6266 init_st
->op
= EXEC_INIT_ASSIGN
;
6267 init_st
->expr1
= gfc_expr_to_initialize (e
);
6268 init_st
->expr2
= init_e
;
6269 init_st
->next
= code
->next
;
6270 code
->next
= init_st
;
6272 else if (e
->ts
.type
== BT_CLASS
6273 && ((code
->ext
.alloc
.ts
.type
== BT_UNKNOWN
6274 && (init_e
= gfc_default_initializer (&e
->ts
.u
.derived
->components
->ts
)))
6275 || (code
->ext
.alloc
.ts
.type
== BT_DERIVED
6276 && (init_e
= gfc_default_initializer (&code
->ext
.alloc
.ts
)))))
6278 gfc_code
*init_st
= gfc_get_code ();
6279 init_st
->loc
= code
->loc
;
6280 init_st
->op
= EXEC_INIT_ASSIGN
;
6281 init_st
->expr1
= gfc_expr_to_initialize (e
);
6282 init_st
->expr2
= init_e
;
6283 init_st
->next
= code
->next
;
6284 code
->next
= init_st
;
6288 if (pointer
|| dimension
== 0)
6291 /* Make sure the next-to-last reference node is an array specification. */
6293 if (ref2
== NULL
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
)
6295 gfc_error ("Array specification required in ALLOCATE statement "
6296 "at %L", &e
->where
);
6300 /* Make sure that the array section reference makes sense in the
6301 context of an ALLOCATE specification. */
6305 for (i
= 0; i
< ar
->dimen
; i
++)
6307 if (ref2
->u
.ar
.type
== AR_ELEMENT
)
6310 switch (ar
->dimen_type
[i
])
6316 if (ar
->start
[i
] != NULL
6317 && ar
->end
[i
] != NULL
6318 && ar
->stride
[i
] == NULL
)
6321 /* Fall Through... */
6325 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6332 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6334 sym
= a
->expr
->symtree
->n
.sym
;
6336 /* TODO - check derived type components. */
6337 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
6340 if ((ar
->start
[i
] != NULL
6341 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
6342 || (ar
->end
[i
] != NULL
6343 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
6345 gfc_error ("'%s' must not appear in the array specification at "
6346 "%L in the same ALLOCATE statement where it is "
6347 "itself allocated", sym
->name
, &ar
->where
);
6357 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
6359 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
6360 gfc_alloc
*a
, *p
, *q
;
6362 stat
= code
->expr1
? code
->expr1
: NULL
;
6364 errmsg
= code
->expr2
? code
->expr2
: NULL
;
6366 /* Check the stat variable. */
6369 if (stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
6370 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6371 stat
->symtree
->n
.sym
->name
, &stat
->where
);
6373 if (gfc_pure (NULL
) && gfc_impure_variable (stat
->symtree
->n
.sym
))
6374 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6377 if ((stat
->ts
.type
!= BT_INTEGER
6378 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
6379 || stat
->ref
->type
== REF_COMPONENT
)))
6381 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6382 "variable", &stat
->where
);
6384 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6385 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
6386 gfc_error ("Stat-variable at %L shall not be %sd within "
6387 "the same %s statement", &stat
->where
, fcn
, fcn
);
6390 /* Check the errmsg variable. */
6394 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6397 if (errmsg
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
6398 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6399 errmsg
->symtree
->n
.sym
->name
, &errmsg
->where
);
6401 if (gfc_pure (NULL
) && gfc_impure_variable (errmsg
->symtree
->n
.sym
))
6402 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6405 if ((errmsg
->ts
.type
!= BT_CHARACTER
6407 && (errmsg
->ref
->type
== REF_ARRAY
6408 || errmsg
->ref
->type
== REF_COMPONENT
)))
6409 || errmsg
->rank
> 0 )
6410 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6411 "variable", &errmsg
->where
);
6413 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6414 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
6415 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6416 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
6419 /* Check that an allocate-object appears only once in the statement.
6420 FIXME: Checking derived types is disabled. */
6421 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
6424 if ((pe
->ref
&& pe
->ref
->type
!= REF_COMPONENT
)
6425 && (pe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
))
6427 for (q
= p
->next
; q
; q
= q
->next
)
6430 if ((qe
->ref
&& qe
->ref
->type
!= REF_COMPONENT
)
6431 && (qe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
)
6432 && (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
))
6433 gfc_error ("Allocate-object at %L also appears at %L",
6434 &pe
->where
, &qe
->where
);
6439 if (strcmp (fcn
, "ALLOCATE") == 0)
6441 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6442 resolve_allocate_expr (a
->expr
, code
);
6446 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
6447 resolve_deallocate_expr (a
->expr
);
6452 /************ SELECT CASE resolution subroutines ************/
6454 /* Callback function for our mergesort variant. Determines interval
6455 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6456 op1 > op2. Assumes we're not dealing with the default case.
6457 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6458 There are nine situations to check. */
6461 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
6465 if (op1
->low
== NULL
) /* op1 = (:L) */
6467 /* op2 = (:N), so overlap. */
6469 /* op2 = (M:) or (M:N), L < M */
6470 if (op2
->low
!= NULL
6471 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
6474 else if (op1
->high
== NULL
) /* op1 = (K:) */
6476 /* op2 = (M:), so overlap. */
6478 /* op2 = (:N) or (M:N), K > N */
6479 if (op2
->high
!= NULL
6480 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
6483 else /* op1 = (K:L) */
6485 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
6486 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
6488 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
6489 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
6491 else /* op2 = (M:N) */
6495 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
6498 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
6507 /* Merge-sort a double linked case list, detecting overlap in the
6508 process. LIST is the head of the double linked case list before it
6509 is sorted. Returns the head of the sorted list if we don't see any
6510 overlap, or NULL otherwise. */
6513 check_case_overlap (gfc_case
*list
)
6515 gfc_case
*p
, *q
, *e
, *tail
;
6516 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
6518 /* If the passed list was empty, return immediately. */
6525 /* Loop unconditionally. The only exit from this loop is a return
6526 statement, when we've finished sorting the case list. */
6533 /* Count the number of merges we do in this pass. */
6536 /* Loop while there exists a merge to be done. */
6541 /* Count this merge. */
6544 /* Cut the list in two pieces by stepping INSIZE places
6545 forward in the list, starting from P. */
6548 for (i
= 0; i
< insize
; i
++)
6557 /* Now we have two lists. Merge them! */
6558 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
6560 /* See from which the next case to merge comes from. */
6563 /* P is empty so the next case must come from Q. */
6568 else if (qsize
== 0 || q
== NULL
)
6577 cmp
= compare_cases (p
, q
);
6580 /* The whole case range for P is less than the
6588 /* The whole case range for Q is greater than
6589 the case range for P. */
6596 /* The cases overlap, or they are the same
6597 element in the list. Either way, we must
6598 issue an error and get the next case from P. */
6599 /* FIXME: Sort P and Q by line number. */
6600 gfc_error ("CASE label at %L overlaps with CASE "
6601 "label at %L", &p
->where
, &q
->where
);
6609 /* Add the next element to the merged list. */
6618 /* P has now stepped INSIZE places along, and so has Q. So
6619 they're the same. */
6624 /* If we have done only one merge or none at all, we've
6625 finished sorting the cases. */
6634 /* Otherwise repeat, merging lists twice the size. */
6640 /* Check to see if an expression is suitable for use in a CASE statement.
6641 Makes sure that all case expressions are scalar constants of the same
6642 type. Return FAILURE if anything is wrong. */
6645 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
6647 if (e
== NULL
) return SUCCESS
;
6649 if (e
->ts
.type
!= case_expr
->ts
.type
)
6651 gfc_error ("Expression in CASE statement at %L must be of type %s",
6652 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
6656 /* C805 (R808) For a given case-construct, each case-value shall be of
6657 the same type as case-expr. For character type, length differences
6658 are allowed, but the kind type parameters shall be the same. */
6660 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
6662 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6663 &e
->where
, case_expr
->ts
.kind
);
6667 /* Convert the case value kind to that of case expression kind, if needed.
6668 FIXME: Should a warning be issued? */
6669 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
6670 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
6674 gfc_error ("Expression in CASE statement at %L must be scalar",
6683 /* Given a completely parsed select statement, we:
6685 - Validate all expressions and code within the SELECT.
6686 - Make sure that the selection expression is not of the wrong type.
6687 - Make sure that no case ranges overlap.
6688 - Eliminate unreachable cases and unreachable code resulting from
6689 removing case labels.
6691 The standard does allow unreachable cases, e.g. CASE (5:3). But
6692 they are a hassle for code generation, and to prevent that, we just
6693 cut them out here. This is not necessary for overlapping cases
6694 because they are illegal and we never even try to generate code.
6696 We have the additional caveat that a SELECT construct could have
6697 been a computed GOTO in the source code. Fortunately we can fairly
6698 easily work around that here: The case_expr for a "real" SELECT CASE
6699 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6700 we have to do is make sure that the case_expr is a scalar integer
6704 resolve_select (gfc_code
*code
)
6707 gfc_expr
*case_expr
;
6708 gfc_case
*cp
, *default_case
, *tail
, *head
;
6709 int seen_unreachable
;
6715 if (code
->expr1
== NULL
)
6717 /* This was actually a computed GOTO statement. */
6718 case_expr
= code
->expr2
;
6719 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
6720 gfc_error ("Selection expression in computed GOTO statement "
6721 "at %L must be a scalar integer expression",
6724 /* Further checking is not necessary because this SELECT was built
6725 by the compiler, so it should always be OK. Just move the
6726 case_expr from expr2 to expr so that we can handle computed
6727 GOTOs as normal SELECTs from here on. */
6728 code
->expr1
= code
->expr2
;
6733 case_expr
= code
->expr1
;
6735 type
= case_expr
->ts
.type
;
6736 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
6738 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6739 &case_expr
->where
, gfc_typename (&case_expr
->ts
));
6741 /* Punt. Going on here just produce more garbage error messages. */
6745 if (case_expr
->rank
!= 0)
6747 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6748 "expression", &case_expr
->where
);
6754 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6755 of the SELECT CASE expression and its CASE values. Walk the lists
6756 of case values, and if we find a mismatch, promote case_expr to
6757 the appropriate kind. */
6759 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
6761 for (body
= code
->block
; body
; body
= body
->block
)
6763 /* Walk the case label list. */
6764 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
6766 /* Intercept the DEFAULT case. It does not have a kind. */
6767 if (cp
->low
== NULL
&& cp
->high
== NULL
)
6770 /* Unreachable case ranges are discarded, so ignore. */
6771 if (cp
->low
!= NULL
&& cp
->high
!= NULL
6772 && cp
->low
!= cp
->high
6773 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
6776 /* FIXME: Should a warning be issued? */
6778 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
6779 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
6781 if (cp
->high
!= NULL
6782 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
6783 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
6788 /* Assume there is no DEFAULT case. */
6789 default_case
= NULL
;
6794 for (body
= code
->block
; body
; body
= body
->block
)
6796 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6798 seen_unreachable
= 0;
6800 /* Walk the case label list, making sure that all case labels
6802 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
6804 /* Count the number of cases in the whole construct. */
6807 /* Intercept the DEFAULT case. */
6808 if (cp
->low
== NULL
&& cp
->high
== NULL
)
6810 if (default_case
!= NULL
)
6812 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6813 "by a second DEFAULT CASE at %L",
6814 &default_case
->where
, &cp
->where
);
6825 /* Deal with single value cases and case ranges. Errors are
6826 issued from the validation function. */
6827 if(validate_case_label_expr (cp
->low
, case_expr
) != SUCCESS
6828 || validate_case_label_expr (cp
->high
, case_expr
) != SUCCESS
)
6834 if (type
== BT_LOGICAL
6835 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
6836 || cp
->low
!= cp
->high
))
6838 gfc_error ("Logical range in CASE statement at %L is not "
6839 "allowed", &cp
->low
->where
);
6844 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
6847 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
6848 if (value
& seen_logical
)
6850 gfc_error ("constant logical value in CASE statement "
6851 "is repeated at %L",
6856 seen_logical
|= value
;
6859 if (cp
->low
!= NULL
&& cp
->high
!= NULL
6860 && cp
->low
!= cp
->high
6861 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
6863 if (gfc_option
.warn_surprising
)
6864 gfc_warning ("Range specification at %L can never "
6865 "be matched", &cp
->where
);
6867 cp
->unreachable
= 1;
6868 seen_unreachable
= 1;
6872 /* If the case range can be matched, it can also overlap with
6873 other cases. To make sure it does not, we put it in a
6874 double linked list here. We sort that with a merge sort
6875 later on to detect any overlapping cases. */
6879 head
->right
= head
->left
= NULL
;
6884 tail
->right
->left
= tail
;
6891 /* It there was a failure in the previous case label, give up
6892 for this case label list. Continue with the next block. */
6896 /* See if any case labels that are unreachable have been seen.
6897 If so, we eliminate them. This is a bit of a kludge because
6898 the case lists for a single case statement (label) is a
6899 single forward linked lists. */
6900 if (seen_unreachable
)
6902 /* Advance until the first case in the list is reachable. */
6903 while (body
->ext
.case_list
!= NULL
6904 && body
->ext
.case_list
->unreachable
)
6906 gfc_case
*n
= body
->ext
.case_list
;
6907 body
->ext
.case_list
= body
->ext
.case_list
->next
;
6909 gfc_free_case_list (n
);
6912 /* Strip all other unreachable cases. */
6913 if (body
->ext
.case_list
)
6915 for (cp
= body
->ext
.case_list
; cp
->next
; cp
= cp
->next
)
6917 if (cp
->next
->unreachable
)
6919 gfc_case
*n
= cp
->next
;
6920 cp
->next
= cp
->next
->next
;
6922 gfc_free_case_list (n
);
6929 /* See if there were overlapping cases. If the check returns NULL,
6930 there was overlap. In that case we don't do anything. If head
6931 is non-NULL, we prepend the DEFAULT case. The sorted list can
6932 then used during code generation for SELECT CASE constructs with
6933 a case expression of a CHARACTER type. */
6936 head
= check_case_overlap (head
);
6938 /* Prepend the default_case if it is there. */
6939 if (head
!= NULL
&& default_case
)
6941 default_case
->left
= NULL
;
6942 default_case
->right
= head
;
6943 head
->left
= default_case
;
6947 /* Eliminate dead blocks that may be the result if we've seen
6948 unreachable case labels for a block. */
6949 for (body
= code
; body
&& body
->block
; body
= body
->block
)
6951 if (body
->block
->ext
.case_list
== NULL
)
6953 /* Cut the unreachable block from the code chain. */
6954 gfc_code
*c
= body
->block
;
6955 body
->block
= c
->block
;
6957 /* Kill the dead block, but not the blocks below it. */
6959 gfc_free_statements (c
);
6963 /* More than two cases is legal but insane for logical selects.
6964 Issue a warning for it. */
6965 if (gfc_option
.warn_surprising
&& type
== BT_LOGICAL
6967 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6972 /* Check if a derived type is extensible. */
6975 gfc_type_is_extensible (gfc_symbol
*sym
)
6977 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
);
6981 /* Resolve a SELECT TYPE statement. */
6984 resolve_select_type (gfc_code
*code
)
6986 gfc_symbol
*selector_type
;
6987 gfc_code
*body
, *new_st
, *if_st
, *tail
;
6988 gfc_code
*class_is
= NULL
, *default_case
= NULL
;
6991 char name
[GFC_MAX_SYMBOL_LEN
];
6999 selector_type
= code
->expr2
->ts
.u
.derived
->components
->ts
.u
.derived
;
7001 selector_type
= code
->expr1
->ts
.u
.derived
->components
->ts
.u
.derived
;
7003 /* Loop over TYPE IS / CLASS IS cases. */
7004 for (body
= code
->block
; body
; body
= body
->block
)
7006 c
= body
->ext
.case_list
;
7008 /* Check F03:C815. */
7009 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7010 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
7012 gfc_error ("Derived type '%s' at %L must be extensible",
7013 c
->ts
.u
.derived
->name
, &c
->where
);
7018 /* Check F03:C816. */
7019 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
7020 && !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
))
7022 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7023 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
7028 /* Intercept the DEFAULT case. */
7029 if (c
->ts
.type
== BT_UNKNOWN
)
7031 /* Check F03:C818. */
7034 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7035 "by a second DEFAULT CASE at %L",
7036 &default_case
->ext
.case_list
->where
, &c
->where
);
7041 default_case
= body
;
7050 /* Insert assignment for selector variable. */
7051 new_st
= gfc_get_code ();
7052 new_st
->op
= EXEC_ASSIGN
;
7053 new_st
->expr1
= gfc_copy_expr (code
->expr1
);
7054 new_st
->expr2
= gfc_copy_expr (code
->expr2
);
7058 /* Put SELECT TYPE statement inside a BLOCK. */
7059 new_st
= gfc_get_code ();
7060 new_st
->op
= code
->op
;
7061 new_st
->expr1
= code
->expr1
;
7062 new_st
->expr2
= code
->expr2
;
7063 new_st
->block
= code
->block
;
7067 ns
->code
->next
= new_st
;
7068 code
->op
= EXEC_BLOCK
;
7069 code
->expr1
= code
->expr2
= NULL
;
7074 /* Transform to EXEC_SELECT. */
7075 code
->op
= EXEC_SELECT
;
7076 gfc_add_component_ref (code
->expr1
, "$vptr");
7077 gfc_add_component_ref (code
->expr1
, "$hash");
7079 /* Loop over TYPE IS / CLASS IS cases. */
7080 for (body
= code
->block
; body
; body
= body
->block
)
7082 c
= body
->ext
.case_list
;
7084 if (c
->ts
.type
== BT_DERIVED
)
7085 c
->low
= c
->high
= gfc_int_expr (c
->ts
.u
.derived
->hash_value
);
7086 else if (c
->ts
.type
== BT_UNKNOWN
)
7089 /* Assign temporary to selector. */
7090 if (c
->ts
.type
== BT_CLASS
)
7091 sprintf (name
, "tmp$class$%s", c
->ts
.u
.derived
->name
);
7093 sprintf (name
, "tmp$type$%s", c
->ts
.u
.derived
->name
);
7094 st
= gfc_find_symtree (ns
->sym_root
, name
);
7095 new_st
= gfc_get_code ();
7096 new_st
->expr1
= gfc_get_variable_expr (st
);
7097 new_st
->expr2
= gfc_get_variable_expr (code
->expr1
->symtree
);
7098 if (c
->ts
.type
== BT_DERIVED
)
7100 new_st
->op
= EXEC_POINTER_ASSIGN
;
7101 gfc_add_component_ref (new_st
->expr2
, "$data");
7104 new_st
->op
= EXEC_POINTER_ASSIGN
;
7105 new_st
->next
= body
->next
;
7106 body
->next
= new_st
;
7109 /* Take out CLASS IS cases for separate treatment. */
7111 while (body
&& body
->block
)
7113 if (body
->block
->ext
.case_list
->ts
.type
== BT_CLASS
)
7115 /* Add to class_is list. */
7116 if (class_is
== NULL
)
7118 class_is
= body
->block
;
7123 for (tail
= class_is
; tail
->block
; tail
= tail
->block
) ;
7124 tail
->block
= body
->block
;
7127 /* Remove from EXEC_SELECT list. */
7128 body
->block
= body
->block
->block
;
7141 /* Add a default case to hold the CLASS IS cases. */
7142 for (tail
= code
; tail
->block
; tail
= tail
->block
) ;
7143 tail
->block
= gfc_get_code ();
7145 tail
->op
= EXEC_SELECT_TYPE
;
7146 tail
->ext
.case_list
= gfc_get_case ();
7147 tail
->ext
.case_list
->ts
.type
= BT_UNKNOWN
;
7149 default_case
= tail
;
7152 /* More than one CLASS IS block? */
7153 if (class_is
->block
)
7157 /* Sort CLASS IS blocks by extension level. */
7161 for (c1
= &class_is
; (*c1
) && (*c1
)->block
; c1
= &((*c1
)->block
))
7164 /* F03:C817 (check for doubles). */
7165 if ((*c1
)->ext
.case_list
->ts
.u
.derived
->hash_value
7166 == c2
->ext
.case_list
->ts
.u
.derived
->hash_value
)
7168 gfc_error ("Double CLASS IS block in SELECT TYPE "
7169 "statement at %L", &c2
->ext
.case_list
->where
);
7172 if ((*c1
)->ext
.case_list
->ts
.u
.derived
->attr
.extension
7173 < c2
->ext
.case_list
->ts
.u
.derived
->attr
.extension
)
7176 (*c1
)->block
= c2
->block
;
7186 /* Generate IF chain. */
7187 if_st
= gfc_get_code ();
7188 if_st
->op
= EXEC_IF
;
7190 for (body
= class_is
; body
; body
= body
->block
)
7192 new_st
->block
= gfc_get_code ();
7193 new_st
= new_st
->block
;
7194 new_st
->op
= EXEC_IF
;
7195 /* Set up IF condition: Call _gfortran_is_extension_of. */
7196 new_st
->expr1
= gfc_get_expr ();
7197 new_st
->expr1
->expr_type
= EXPR_FUNCTION
;
7198 new_st
->expr1
->ts
.type
= BT_LOGICAL
;
7199 new_st
->expr1
->ts
.kind
= 4;
7200 new_st
->expr1
->value
.function
.name
= gfc_get_string (PREFIX ("is_extension_of"));
7201 new_st
->expr1
->value
.function
.isym
= XCNEW (gfc_intrinsic_sym
);
7202 new_st
->expr1
->value
.function
.isym
->id
= GFC_ISYM_EXTENDS_TYPE_OF
;
7203 /* Set up arguments. */
7204 new_st
->expr1
->value
.function
.actual
= gfc_get_actual_arglist ();
7205 new_st
->expr1
->value
.function
.actual
->expr
= gfc_get_variable_expr (code
->expr1
->symtree
);
7206 gfc_add_component_ref (new_st
->expr1
->value
.function
.actual
->expr
, "$vptr");
7207 vtab
= gfc_find_derived_vtab (body
->ext
.case_list
->ts
.u
.derived
);
7208 st
= gfc_find_symtree (vtab
->ns
->sym_root
, vtab
->name
);
7209 new_st
->expr1
->value
.function
.actual
->next
= gfc_get_actual_arglist ();
7210 new_st
->expr1
->value
.function
.actual
->next
->expr
= gfc_get_variable_expr (st
);
7211 new_st
->next
= body
->next
;
7213 if (default_case
->next
)
7215 new_st
->block
= gfc_get_code ();
7216 new_st
= new_st
->block
;
7217 new_st
->op
= EXEC_IF
;
7218 new_st
->next
= default_case
->next
;
7221 /* Replace CLASS DEFAULT code by the IF chain. */
7222 default_case
->next
= if_st
;
7225 resolve_select (code
);
7230 /* Resolve a transfer statement. This is making sure that:
7231 -- a derived type being transferred has only non-pointer components
7232 -- a derived type being transferred doesn't have private components, unless
7233 it's being transferred from the module where the type was defined
7234 -- we're not trying to transfer a whole assumed size array. */
7237 resolve_transfer (gfc_code
*code
)
7246 if (exp
->expr_type
!= EXPR_VARIABLE
&& exp
->expr_type
!= EXPR_FUNCTION
)
7249 sym
= exp
->symtree
->n
.sym
;
7252 /* Go to actual component transferred. */
7253 for (ref
= code
->expr1
->ref
; ref
; ref
= ref
->next
)
7254 if (ref
->type
== REF_COMPONENT
)
7255 ts
= &ref
->u
.c
.component
->ts
;
7257 if (ts
->type
== BT_DERIVED
)
7259 /* Check that transferred derived type doesn't contain POINTER
7261 if (ts
->u
.derived
->attr
.pointer_comp
)
7263 gfc_error ("Data transfer element at %L cannot have "
7264 "POINTER components", &code
->loc
);
7268 if (ts
->u
.derived
->attr
.alloc_comp
)
7270 gfc_error ("Data transfer element at %L cannot have "
7271 "ALLOCATABLE components", &code
->loc
);
7275 if (derived_inaccessible (ts
->u
.derived
))
7277 gfc_error ("Data transfer element at %L cannot have "
7278 "PRIVATE components",&code
->loc
);
7283 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
7284 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
7286 gfc_error ("Data transfer element at %L cannot be a full reference to "
7287 "an assumed-size array", &code
->loc
);
7293 /*********** Toplevel code resolution subroutines ***********/
7295 /* Find the set of labels that are reachable from this block. We also
7296 record the last statement in each block. */
7299 find_reachable_labels (gfc_code
*block
)
7306 cs_base
->reachable_labels
= bitmap_obstack_alloc (&labels_obstack
);
7308 /* Collect labels in this block. We don't keep those corresponding
7309 to END {IF|SELECT}, these are checked in resolve_branch by going
7310 up through the code_stack. */
7311 for (c
= block
; c
; c
= c
->next
)
7313 if (c
->here
&& c
->op
!= EXEC_END_BLOCK
)
7314 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
7317 /* Merge with labels from parent block. */
7320 gcc_assert (cs_base
->prev
->reachable_labels
);
7321 bitmap_ior_into (cs_base
->reachable_labels
,
7322 cs_base
->prev
->reachable_labels
);
7328 resolve_sync (gfc_code
*code
)
7330 /* Check imageset. The * case matches expr1 == NULL. */
7333 if (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
> 1)
7334 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
7335 "INTEGER expression", &code
->expr1
->where
);
7336 if (code
->expr1
->expr_type
== EXPR_CONSTANT
&& code
->expr1
->rank
== 0
7337 && mpz_cmp_si (code
->expr1
->value
.integer
, 1) < 0)
7338 gfc_error ("Imageset argument at %L must between 1 and num_images()",
7339 &code
->expr1
->where
);
7340 else if (code
->expr1
->expr_type
== EXPR_ARRAY
7341 && gfc_simplify_expr (code
->expr1
, 0) == SUCCESS
)
7343 gfc_constructor
*cons
;
7344 for (cons
= code
->expr1
->value
.constructor
; cons
; cons
= cons
->next
)
7345 if (cons
->expr
->expr_type
== EXPR_CONSTANT
7346 && mpz_cmp_si (cons
->expr
->value
.integer
, 1) < 0)
7347 gfc_error ("Imageset argument at %L must between 1 and "
7348 "num_images()", &cons
->expr
->where
);
7354 && (code
->expr2
->ts
.type
!= BT_INTEGER
|| code
->expr2
->rank
!= 0
7355 || code
->expr2
->expr_type
!= EXPR_VARIABLE
))
7356 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
7357 &code
->expr2
->where
);
7361 && (code
->expr3
->ts
.type
!= BT_CHARACTER
|| code
->expr3
->rank
!= 0
7362 || code
->expr3
->expr_type
!= EXPR_VARIABLE
))
7363 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
7364 &code
->expr3
->where
);
7368 /* Given a branch to a label, see if the branch is conforming.
7369 The code node describes where the branch is located. */
7372 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
7379 /* Step one: is this a valid branching target? */
7381 if (label
->defined
== ST_LABEL_UNKNOWN
)
7383 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
7388 if (label
->defined
!= ST_LABEL_TARGET
)
7390 gfc_error ("Statement at %L is not a valid branch target statement "
7391 "for the branch statement at %L", &label
->where
, &code
->loc
);
7395 /* Step two: make sure this branch is not a branch to itself ;-) */
7397 if (code
->here
== label
)
7399 gfc_warning ("Branch at %L may result in an infinite loop", &code
->loc
);
7403 /* Step three: See if the label is in the same block as the
7404 branching statement. The hard work has been done by setting up
7405 the bitmap reachable_labels. */
7407 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
7409 /* Check now whether there is a CRITICAL construct; if so, check
7410 whether the label is still visible outside of the CRITICAL block,
7411 which is invalid. */
7412 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
7413 if (stack
->current
->op
== EXEC_CRITICAL
7414 && bitmap_bit_p (stack
->reachable_labels
, label
->value
))
7415 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7416 " at %L", &code
->loc
, &label
->where
);
7421 /* Step four: If we haven't found the label in the bitmap, it may
7422 still be the label of the END of the enclosing block, in which
7423 case we find it by going up the code_stack. */
7425 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
7427 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
7429 if (stack
->current
->op
== EXEC_CRITICAL
)
7431 /* Note: A label at END CRITICAL does not leave the CRITICAL
7432 construct as END CRITICAL is still part of it. */
7433 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7434 " at %L", &code
->loc
, &label
->where
);
7441 gcc_assert (stack
->current
->next
->op
== EXEC_END_BLOCK
);
7445 /* The label is not in an enclosing block, so illegal. This was
7446 allowed in Fortran 66, so we allow it as extension. No
7447 further checks are necessary in this case. */
7448 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
7449 "as the GOTO statement at %L", &label
->where
,
7455 /* Check whether EXPR1 has the same shape as EXPR2. */
7458 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
7460 mpz_t shape
[GFC_MAX_DIMENSIONS
];
7461 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
7462 gfc_try result
= FAILURE
;
7465 /* Compare the rank. */
7466 if (expr1
->rank
!= expr2
->rank
)
7469 /* Compare the size of each dimension. */
7470 for (i
=0; i
<expr1
->rank
; i
++)
7472 if (gfc_array_dimen_size (expr1
, i
, &shape
[i
]) == FAILURE
)
7475 if (gfc_array_dimen_size (expr2
, i
, &shape2
[i
]) == FAILURE
)
7478 if (mpz_cmp (shape
[i
], shape2
[i
]))
7482 /* When either of the two expression is an assumed size array, we
7483 ignore the comparison of dimension sizes. */
7488 for (i
--; i
>= 0; i
--)
7490 mpz_clear (shape
[i
]);
7491 mpz_clear (shape2
[i
]);
7497 /* Check whether a WHERE assignment target or a WHERE mask expression
7498 has the same shape as the outmost WHERE mask expression. */
7501 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
7507 cblock
= code
->block
;
7509 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7510 In case of nested WHERE, only the outmost one is stored. */
7511 if (mask
== NULL
) /* outmost WHERE */
7513 else /* inner WHERE */
7520 /* Check if the mask-expr has a consistent shape with the
7521 outmost WHERE mask-expr. */
7522 if (resolve_where_shape (cblock
->expr1
, e
) == FAILURE
)
7523 gfc_error ("WHERE mask at %L has inconsistent shape",
7524 &cblock
->expr1
->where
);
7527 /* the assignment statement of a WHERE statement, or the first
7528 statement in where-body-construct of a WHERE construct */
7529 cnext
= cblock
->next
;
7534 /* WHERE assignment statement */
7537 /* Check shape consistent for WHERE assignment target. */
7538 if (e
&& resolve_where_shape (cnext
->expr1
, e
) == FAILURE
)
7539 gfc_error ("WHERE assignment target at %L has "
7540 "inconsistent shape", &cnext
->expr1
->where
);
7544 case EXEC_ASSIGN_CALL
:
7545 resolve_call (cnext
);
7546 if (!cnext
->resolved_sym
->attr
.elemental
)
7547 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7548 &cnext
->ext
.actual
->expr
->where
);
7551 /* WHERE or WHERE construct is part of a where-body-construct */
7553 resolve_where (cnext
, e
);
7557 gfc_error ("Unsupported statement inside WHERE at %L",
7560 /* the next statement within the same where-body-construct */
7561 cnext
= cnext
->next
;
7563 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7564 cblock
= cblock
->block
;
7569 /* Resolve assignment in FORALL construct.
7570 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7571 FORALL index variables. */
7574 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
7578 for (n
= 0; n
< nvar
; n
++)
7580 gfc_symbol
*forall_index
;
7582 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
7584 /* Check whether the assignment target is one of the FORALL index
7586 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
7587 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
7588 gfc_error ("Assignment to a FORALL index variable at %L",
7589 &code
->expr1
->where
);
7592 /* If one of the FORALL index variables doesn't appear in the
7593 assignment variable, then there could be a many-to-one
7594 assignment. Emit a warning rather than an error because the
7595 mask could be resolving this problem. */
7596 if (find_forall_index (code
->expr1
, forall_index
, 0) == FAILURE
)
7597 gfc_warning ("The FORALL with index '%s' is not used on the "
7598 "left side of the assignment at %L and so might "
7599 "cause multiple assignment to this object",
7600 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
7606 /* Resolve WHERE statement in FORALL construct. */
7609 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
7610 gfc_expr
**var_expr
)
7615 cblock
= code
->block
;
7618 /* the assignment statement of a WHERE statement, or the first
7619 statement in where-body-construct of a WHERE construct */
7620 cnext
= cblock
->next
;
7625 /* WHERE assignment statement */
7627 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
7630 /* WHERE operator assignment statement */
7631 case EXEC_ASSIGN_CALL
:
7632 resolve_call (cnext
);
7633 if (!cnext
->resolved_sym
->attr
.elemental
)
7634 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7635 &cnext
->ext
.actual
->expr
->where
);
7638 /* WHERE or WHERE construct is part of a where-body-construct */
7640 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
7644 gfc_error ("Unsupported statement inside WHERE at %L",
7647 /* the next statement within the same where-body-construct */
7648 cnext
= cnext
->next
;
7650 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7651 cblock
= cblock
->block
;
7656 /* Traverse the FORALL body to check whether the following errors exist:
7657 1. For assignment, check if a many-to-one assignment happens.
7658 2. For WHERE statement, check the WHERE body to see if there is any
7659 many-to-one assignment. */
7662 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
7666 c
= code
->block
->next
;
7672 case EXEC_POINTER_ASSIGN
:
7673 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
7676 case EXEC_ASSIGN_CALL
:
7680 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7681 there is no need to handle it here. */
7685 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
7690 /* The next statement in the FORALL body. */
7696 /* Counts the number of iterators needed inside a forall construct, including
7697 nested forall constructs. This is used to allocate the needed memory
7698 in gfc_resolve_forall. */
7701 gfc_count_forall_iterators (gfc_code
*code
)
7703 int max_iters
, sub_iters
, current_iters
;
7704 gfc_forall_iterator
*fa
;
7706 gcc_assert(code
->op
== EXEC_FORALL
);
7710 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
7713 code
= code
->block
->next
;
7717 if (code
->op
== EXEC_FORALL
)
7719 sub_iters
= gfc_count_forall_iterators (code
);
7720 if (sub_iters
> max_iters
)
7721 max_iters
= sub_iters
;
7726 return current_iters
+ max_iters
;
7730 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7731 gfc_resolve_forall_body to resolve the FORALL body. */
7734 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
7736 static gfc_expr
**var_expr
;
7737 static int total_var
= 0;
7738 static int nvar
= 0;
7740 gfc_forall_iterator
*fa
;
7745 /* Start to resolve a FORALL construct */
7746 if (forall_save
== 0)
7748 /* Count the total number of FORALL index in the nested FORALL
7749 construct in order to allocate the VAR_EXPR with proper size. */
7750 total_var
= gfc_count_forall_iterators (code
);
7752 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7753 var_expr
= (gfc_expr
**) gfc_getmem (total_var
* sizeof (gfc_expr
*));
7756 /* The information about FORALL iterator, including FORALL index start, end
7757 and stride. The FORALL index can not appear in start, end or stride. */
7758 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
7760 /* Check if any outer FORALL index name is the same as the current
7762 for (i
= 0; i
< nvar
; i
++)
7764 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
7766 gfc_error ("An outer FORALL construct already has an index "
7767 "with this name %L", &fa
->var
->where
);
7771 /* Record the current FORALL index. */
7772 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
7776 /* No memory leak. */
7777 gcc_assert (nvar
<= total_var
);
7780 /* Resolve the FORALL body. */
7781 gfc_resolve_forall_body (code
, nvar
, var_expr
);
7783 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7784 gfc_resolve_blocks (code
->block
, ns
);
7788 /* Free only the VAR_EXPRs allocated in this frame. */
7789 for (i
= nvar
; i
< tmp
; i
++)
7790 gfc_free_expr (var_expr
[i
]);
7794 /* We are in the outermost FORALL construct. */
7795 gcc_assert (forall_save
== 0);
7797 /* VAR_EXPR is not needed any more. */
7798 gfc_free (var_expr
);
7804 /* Resolve a BLOCK construct statement. */
7807 resolve_block_construct (gfc_code
* code
)
7809 /* Eventually, we may want to do some checks here or handle special stuff.
7810 But so far the only thing we can do is resolving the local namespace. */
7812 gfc_resolve (code
->ext
.ns
);
7816 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7819 static void resolve_code (gfc_code
*, gfc_namespace
*);
7822 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
7826 for (; b
; b
= b
->block
)
7828 t
= gfc_resolve_expr (b
->expr1
);
7829 if (gfc_resolve_expr (b
->expr2
) == FAILURE
)
7835 if (t
== SUCCESS
&& b
->expr1
!= NULL
7836 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
7837 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7844 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
7845 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7850 resolve_branch (b
->label1
, b
);
7854 resolve_block_construct (b
);
7858 case EXEC_SELECT_TYPE
:
7869 case EXEC_OMP_ATOMIC
:
7870 case EXEC_OMP_CRITICAL
:
7872 case EXEC_OMP_MASTER
:
7873 case EXEC_OMP_ORDERED
:
7874 case EXEC_OMP_PARALLEL
:
7875 case EXEC_OMP_PARALLEL_DO
:
7876 case EXEC_OMP_PARALLEL_SECTIONS
:
7877 case EXEC_OMP_PARALLEL_WORKSHARE
:
7878 case EXEC_OMP_SECTIONS
:
7879 case EXEC_OMP_SINGLE
:
7881 case EXEC_OMP_TASKWAIT
:
7882 case EXEC_OMP_WORKSHARE
:
7886 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7889 resolve_code (b
->next
, ns
);
7894 /* Does everything to resolve an ordinary assignment. Returns true
7895 if this is an interface assignment. */
7897 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
7907 if (gfc_extend_assign (code
, ns
) == SUCCESS
)
7911 if (code
->op
== EXEC_ASSIGN_CALL
)
7913 lhs
= code
->ext
.actual
->expr
;
7914 rhsptr
= &code
->ext
.actual
->next
->expr
;
7918 gfc_actual_arglist
* args
;
7919 gfc_typebound_proc
* tbp
;
7921 gcc_assert (code
->op
== EXEC_COMPCALL
);
7923 args
= code
->expr1
->value
.compcall
.actual
;
7925 rhsptr
= &args
->next
->expr
;
7927 tbp
= code
->expr1
->value
.compcall
.tbp
;
7928 gcc_assert (!tbp
->is_generic
);
7931 /* Make a temporary rhs when there is a default initializer
7932 and rhs is the same symbol as the lhs. */
7933 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
7934 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
7935 && has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
7936 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
7937 *rhsptr
= gfc_get_parentheses (*rhsptr
);
7946 && gfc_notify_std (GFC_STD_GNU
, "Extension: BOZ literal at %L outside "
7947 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7948 &code
->loc
) == FAILURE
)
7951 /* Handle the case of a BOZ literal on the RHS. */
7952 if (rhs
->is_boz
&& lhs
->ts
.type
!= BT_INTEGER
)
7955 if (gfc_option
.warn_surprising
)
7956 gfc_warning ("BOZ literal at %L is bitwise transferred "
7957 "non-integer symbol '%s'", &code
->loc
,
7958 lhs
->symtree
->n
.sym
->name
);
7960 if (!gfc_convert_boz (rhs
, &lhs
->ts
))
7962 if ((rc
= gfc_range_check (rhs
)) != ARITH_OK
)
7964 if (rc
== ARITH_UNDERFLOW
)
7965 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7966 ". This check can be disabled with the option "
7967 "-fno-range-check", &rhs
->where
);
7968 else if (rc
== ARITH_OVERFLOW
)
7969 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7970 ". This check can be disabled with the option "
7971 "-fno-range-check", &rhs
->where
);
7972 else if (rc
== ARITH_NAN
)
7973 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7974 ". This check can be disabled with the option "
7975 "-fno-range-check", &rhs
->where
);
7981 if (lhs
->ts
.type
== BT_CHARACTER
7982 && gfc_option
.warn_character_truncation
)
7984 if (lhs
->ts
.u
.cl
!= NULL
7985 && lhs
->ts
.u
.cl
->length
!= NULL
7986 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
7987 llen
= mpz_get_si (lhs
->ts
.u
.cl
->length
->value
.integer
);
7989 if (rhs
->expr_type
== EXPR_CONSTANT
)
7990 rlen
= rhs
->value
.character
.length
;
7992 else if (rhs
->ts
.u
.cl
!= NULL
7993 && rhs
->ts
.u
.cl
->length
!= NULL
7994 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
7995 rlen
= mpz_get_si (rhs
->ts
.u
.cl
->length
->value
.integer
);
7997 if (rlen
&& llen
&& rlen
> llen
)
7998 gfc_warning_now ("CHARACTER expression will be truncated "
7999 "in assignment (%d/%d) at %L",
8000 llen
, rlen
, &code
->loc
);
8003 /* Ensure that a vector index expression for the lvalue is evaluated
8004 to a temporary if the lvalue symbol is referenced in it. */
8007 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
8008 if (ref
->type
== REF_ARRAY
)
8010 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
8011 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
8012 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
8013 ref
->u
.ar
.start
[n
]))
8015 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
8019 if (gfc_pure (NULL
))
8021 if (gfc_impure_variable (lhs
->symtree
->n
.sym
))
8023 gfc_error ("Cannot assign to variable '%s' in PURE "
8025 lhs
->symtree
->n
.sym
->name
,
8030 if (lhs
->ts
.type
== BT_DERIVED
8031 && lhs
->expr_type
== EXPR_VARIABLE
8032 && lhs
->ts
.u
.derived
->attr
.pointer_comp
8033 && rhs
->expr_type
== EXPR_VARIABLE
8034 && gfc_impure_variable (rhs
->symtree
->n
.sym
))
8036 gfc_error ("The impure variable at %L is assigned to "
8037 "a derived type variable with a POINTER "
8038 "component in a PURE procedure (12.6)",
8045 if (lhs
->ts
.type
== BT_CLASS
)
8047 gfc_error ("Variable must not be polymorphic in assignment at %L",
8052 gfc_check_assign (lhs
, rhs
, 1);
8057 /* Given a block of code, recursively resolve everything pointed to by this
8061 resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
8063 int omp_workshare_save
;
8068 frame
.prev
= cs_base
;
8072 find_reachable_labels (code
);
8074 for (; code
; code
= code
->next
)
8076 frame
.current
= code
;
8077 forall_save
= forall_flag
;
8079 if (code
->op
== EXEC_FORALL
)
8082 gfc_resolve_forall (code
, ns
, forall_save
);
8085 else if (code
->block
)
8087 omp_workshare_save
= -1;
8090 case EXEC_OMP_PARALLEL_WORKSHARE
:
8091 omp_workshare_save
= omp_workshare_flag
;
8092 omp_workshare_flag
= 1;
8093 gfc_resolve_omp_parallel_blocks (code
, ns
);
8095 case EXEC_OMP_PARALLEL
:
8096 case EXEC_OMP_PARALLEL_DO
:
8097 case EXEC_OMP_PARALLEL_SECTIONS
:
8099 omp_workshare_save
= omp_workshare_flag
;
8100 omp_workshare_flag
= 0;
8101 gfc_resolve_omp_parallel_blocks (code
, ns
);
8104 gfc_resolve_omp_do_blocks (code
, ns
);
8106 case EXEC_SELECT_TYPE
:
8107 gfc_current_ns
= code
->ext
.ns
;
8108 gfc_resolve_blocks (code
->block
, gfc_current_ns
);
8109 gfc_current_ns
= ns
;
8111 case EXEC_OMP_WORKSHARE
:
8112 omp_workshare_save
= omp_workshare_flag
;
8113 omp_workshare_flag
= 1;
8116 gfc_resolve_blocks (code
->block
, ns
);
8120 if (omp_workshare_save
!= -1)
8121 omp_workshare_flag
= omp_workshare_save
;
8125 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
8126 t
= gfc_resolve_expr (code
->expr1
);
8127 forall_flag
= forall_save
;
8129 if (gfc_resolve_expr (code
->expr2
) == FAILURE
)
8132 if (code
->op
== EXEC_ALLOCATE
8133 && gfc_resolve_expr (code
->expr3
) == FAILURE
)
8139 case EXEC_END_BLOCK
:
8143 case EXEC_ERROR_STOP
:
8147 case EXEC_ASSIGN_CALL
:
8152 case EXEC_SYNC_IMAGES
:
8153 case EXEC_SYNC_MEMORY
:
8154 resolve_sync (code
);
8158 /* Keep track of which entry we are up to. */
8159 current_entry_id
= code
->ext
.entry
->id
;
8163 resolve_where (code
, NULL
);
8167 if (code
->expr1
!= NULL
)
8169 if (code
->expr1
->ts
.type
!= BT_INTEGER
)
8170 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8171 "INTEGER variable", &code
->expr1
->where
);
8172 else if (code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
8173 gfc_error ("Variable '%s' has not been assigned a target "
8174 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
8175 &code
->expr1
->where
);
8178 resolve_branch (code
->label1
, code
);
8182 if (code
->expr1
!= NULL
8183 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
8184 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8185 "INTEGER return specifier", &code
->expr1
->where
);
8188 case EXEC_INIT_ASSIGN
:
8189 case EXEC_END_PROCEDURE
:
8196 if (resolve_ordinary_assign (code
, ns
))
8198 if (code
->op
== EXEC_COMPCALL
)
8205 case EXEC_LABEL_ASSIGN
:
8206 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
8207 gfc_error ("Label %d referenced at %L is never defined",
8208 code
->label1
->value
, &code
->label1
->where
);
8210 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
8211 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
8212 || code
->expr1
->symtree
->n
.sym
->ts
.kind
8213 != gfc_default_integer_kind
8214 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
8215 gfc_error ("ASSIGN statement at %L requires a scalar "
8216 "default INTEGER variable", &code
->expr1
->where
);
8219 case EXEC_POINTER_ASSIGN
:
8223 gfc_check_pointer_assign (code
->expr1
, code
->expr2
);
8226 case EXEC_ARITHMETIC_IF
:
8228 && code
->expr1
->ts
.type
!= BT_INTEGER
8229 && code
->expr1
->ts
.type
!= BT_REAL
)
8230 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8231 "expression", &code
->expr1
->where
);
8233 resolve_branch (code
->label1
, code
);
8234 resolve_branch (code
->label2
, code
);
8235 resolve_branch (code
->label3
, code
);
8239 if (t
== SUCCESS
&& code
->expr1
!= NULL
8240 && (code
->expr1
->ts
.type
!= BT_LOGICAL
8241 || code
->expr1
->rank
!= 0))
8242 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8243 &code
->expr1
->where
);
8248 resolve_call (code
);
8253 resolve_typebound_subroutine (code
);
8257 resolve_ppc_call (code
);
8261 /* Select is complicated. Also, a SELECT construct could be
8262 a transformed computed GOTO. */
8263 resolve_select (code
);
8266 case EXEC_SELECT_TYPE
:
8267 resolve_select_type (code
);
8271 gfc_resolve (code
->ext
.ns
);
8275 if (code
->ext
.iterator
!= NULL
)
8277 gfc_iterator
*iter
= code
->ext
.iterator
;
8278 if (gfc_resolve_iterator (iter
, true) != FAILURE
)
8279 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
);
8284 if (code
->expr1
== NULL
)
8285 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8287 && (code
->expr1
->rank
!= 0
8288 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
8289 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8290 "a scalar LOGICAL expression", &code
->expr1
->where
);
8295 resolve_allocate_deallocate (code
, "ALLOCATE");
8299 case EXEC_DEALLOCATE
:
8301 resolve_allocate_deallocate (code
, "DEALLOCATE");
8306 if (gfc_resolve_open (code
->ext
.open
) == FAILURE
)
8309 resolve_branch (code
->ext
.open
->err
, code
);
8313 if (gfc_resolve_close (code
->ext
.close
) == FAILURE
)
8316 resolve_branch (code
->ext
.close
->err
, code
);
8319 case EXEC_BACKSPACE
:
8323 if (gfc_resolve_filepos (code
->ext
.filepos
) == FAILURE
)
8326 resolve_branch (code
->ext
.filepos
->err
, code
);
8330 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
8333 resolve_branch (code
->ext
.inquire
->err
, code
);
8337 gcc_assert (code
->ext
.inquire
!= NULL
);
8338 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
8341 resolve_branch (code
->ext
.inquire
->err
, code
);
8345 if (gfc_resolve_wait (code
->ext
.wait
) == FAILURE
)
8348 resolve_branch (code
->ext
.wait
->err
, code
);
8349 resolve_branch (code
->ext
.wait
->end
, code
);
8350 resolve_branch (code
->ext
.wait
->eor
, code
);
8355 if (gfc_resolve_dt (code
->ext
.dt
, &code
->loc
) == FAILURE
)
8358 resolve_branch (code
->ext
.dt
->err
, code
);
8359 resolve_branch (code
->ext
.dt
->end
, code
);
8360 resolve_branch (code
->ext
.dt
->eor
, code
);
8364 resolve_transfer (code
);
8368 resolve_forall_iterators (code
->ext
.forall_iterator
);
8370 if (code
->expr1
!= NULL
&& code
->expr1
->ts
.type
!= BT_LOGICAL
)
8371 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8372 "expression", &code
->expr1
->where
);
8375 case EXEC_OMP_ATOMIC
:
8376 case EXEC_OMP_BARRIER
:
8377 case EXEC_OMP_CRITICAL
:
8378 case EXEC_OMP_FLUSH
:
8380 case EXEC_OMP_MASTER
:
8381 case EXEC_OMP_ORDERED
:
8382 case EXEC_OMP_SECTIONS
:
8383 case EXEC_OMP_SINGLE
:
8384 case EXEC_OMP_TASKWAIT
:
8385 case EXEC_OMP_WORKSHARE
:
8386 gfc_resolve_omp_directive (code
, ns
);
8389 case EXEC_OMP_PARALLEL
:
8390 case EXEC_OMP_PARALLEL_DO
:
8391 case EXEC_OMP_PARALLEL_SECTIONS
:
8392 case EXEC_OMP_PARALLEL_WORKSHARE
:
8394 omp_workshare_save
= omp_workshare_flag
;
8395 omp_workshare_flag
= 0;
8396 gfc_resolve_omp_directive (code
, ns
);
8397 omp_workshare_flag
= omp_workshare_save
;
8401 gfc_internal_error ("resolve_code(): Bad statement code");
8405 cs_base
= frame
.prev
;
8409 /* Resolve initial values and make sure they are compatible with
8413 resolve_values (gfc_symbol
*sym
)
8415 if (sym
->value
== NULL
)
8418 if (gfc_resolve_expr (sym
->value
) == FAILURE
)
8421 gfc_check_assign_symbol (sym
, sym
->value
);
8425 /* Verify the binding labels for common blocks that are BIND(C). The label
8426 for a BIND(C) common block must be identical in all scoping units in which
8427 the common block is declared. Further, the binding label can not collide
8428 with any other global entity in the program. */
8431 resolve_bind_c_comms (gfc_symtree
*comm_block_tree
)
8433 if (comm_block_tree
->n
.common
->is_bind_c
== 1)
8435 gfc_gsymbol
*binding_label_gsym
;
8436 gfc_gsymbol
*comm_name_gsym
;
8438 /* See if a global symbol exists by the common block's name. It may
8439 be NULL if the common block is use-associated. */
8440 comm_name_gsym
= gfc_find_gsymbol (gfc_gsym_root
,
8441 comm_block_tree
->n
.common
->name
);
8442 if (comm_name_gsym
!= NULL
&& comm_name_gsym
->type
!= GSYM_COMMON
)
8443 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8444 "with the global entity '%s' at %L",
8445 comm_block_tree
->n
.common
->binding_label
,
8446 comm_block_tree
->n
.common
->name
,
8447 &(comm_block_tree
->n
.common
->where
),
8448 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
8449 else if (comm_name_gsym
!= NULL
8450 && strcmp (comm_name_gsym
->name
,
8451 comm_block_tree
->n
.common
->name
) == 0)
8453 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8455 if (comm_name_gsym
->binding_label
== NULL
)
8456 /* No binding label for common block stored yet; save this one. */
8457 comm_name_gsym
->binding_label
=
8458 comm_block_tree
->n
.common
->binding_label
;
8460 if (strcmp (comm_name_gsym
->binding_label
,
8461 comm_block_tree
->n
.common
->binding_label
) != 0)
8463 /* Common block names match but binding labels do not. */
8464 gfc_error ("Binding label '%s' for common block '%s' at %L "
8465 "does not match the binding label '%s' for common "
8467 comm_block_tree
->n
.common
->binding_label
,
8468 comm_block_tree
->n
.common
->name
,
8469 &(comm_block_tree
->n
.common
->where
),
8470 comm_name_gsym
->binding_label
,
8471 comm_name_gsym
->name
,
8472 &(comm_name_gsym
->where
));
8477 /* There is no binding label (NAME="") so we have nothing further to
8478 check and nothing to add as a global symbol for the label. */
8479 if (comm_block_tree
->n
.common
->binding_label
[0] == '\0' )
8482 binding_label_gsym
=
8483 gfc_find_gsymbol (gfc_gsym_root
,
8484 comm_block_tree
->n
.common
->binding_label
);
8485 if (binding_label_gsym
== NULL
)
8487 /* Need to make a global symbol for the binding label to prevent
8488 it from colliding with another. */
8489 binding_label_gsym
=
8490 gfc_get_gsymbol (comm_block_tree
->n
.common
->binding_label
);
8491 binding_label_gsym
->sym_name
= comm_block_tree
->n
.common
->name
;
8492 binding_label_gsym
->type
= GSYM_COMMON
;
8496 /* If comm_name_gsym is NULL, the name common block is use
8497 associated and the name could be colliding. */
8498 if (binding_label_gsym
->type
!= GSYM_COMMON
)
8499 gfc_error ("Binding label '%s' for common block '%s' at %L "
8500 "collides with the global entity '%s' at %L",
8501 comm_block_tree
->n
.common
->binding_label
,
8502 comm_block_tree
->n
.common
->name
,
8503 &(comm_block_tree
->n
.common
->where
),
8504 binding_label_gsym
->name
,
8505 &(binding_label_gsym
->where
));
8506 else if (comm_name_gsym
!= NULL
8507 && (strcmp (binding_label_gsym
->name
,
8508 comm_name_gsym
->binding_label
) != 0)
8509 && (strcmp (binding_label_gsym
->sym_name
,
8510 comm_name_gsym
->name
) != 0))
8511 gfc_error ("Binding label '%s' for common block '%s' at %L "
8512 "collides with global entity '%s' at %L",
8513 binding_label_gsym
->name
, binding_label_gsym
->sym_name
,
8514 &(comm_block_tree
->n
.common
->where
),
8515 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
8523 /* Verify any BIND(C) derived types in the namespace so we can report errors
8524 for them once, rather than for each variable declared of that type. */
8527 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
8529 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
8530 && derived_sym
->attr
.is_bind_c
== 1)
8531 verify_bind_c_derived_type (derived_sym
);
8537 /* Verify that any binding labels used in a given namespace do not collide
8538 with the names or binding labels of any global symbols. */
8541 gfc_verify_binding_labels (gfc_symbol
*sym
)
8545 if (sym
!= NULL
&& sym
->attr
.is_bind_c
&& sym
->attr
.is_iso_c
== 0
8546 && sym
->attr
.flavor
!= FL_DERIVED
&& sym
->binding_label
[0] != '\0')
8548 gfc_gsymbol
*bind_c_sym
;
8550 bind_c_sym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
8551 if (bind_c_sym
!= NULL
8552 && strcmp (bind_c_sym
->name
, sym
->binding_label
) == 0)
8554 if (sym
->attr
.if_source
== IFSRC_DECL
8555 && (bind_c_sym
->type
!= GSYM_SUBROUTINE
8556 && bind_c_sym
->type
!= GSYM_FUNCTION
)
8557 && ((sym
->attr
.contained
== 1
8558 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0)
8559 || (sym
->attr
.use_assoc
== 1
8560 && (strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0))))
8562 /* Make sure global procedures don't collide with anything. */
8563 gfc_error ("Binding label '%s' at %L collides with the global "
8564 "entity '%s' at %L", sym
->binding_label
,
8565 &(sym
->declared_at
), bind_c_sym
->name
,
8566 &(bind_c_sym
->where
));
8569 else if (sym
->attr
.contained
== 0
8570 && (sym
->attr
.if_source
== IFSRC_IFBODY
8571 && sym
->attr
.flavor
== FL_PROCEDURE
)
8572 && (bind_c_sym
->sym_name
!= NULL
8573 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0))
8575 /* Make sure procedures in interface bodies don't collide. */
8576 gfc_error ("Binding label '%s' in interface body at %L collides "
8577 "with the global entity '%s' at %L",
8579 &(sym
->declared_at
), bind_c_sym
->name
,
8580 &(bind_c_sym
->where
));
8583 else if (sym
->attr
.contained
== 0
8584 && sym
->attr
.if_source
== IFSRC_UNKNOWN
)
8585 if ((sym
->attr
.use_assoc
&& bind_c_sym
->mod_name
8586 && strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0)
8587 || sym
->attr
.use_assoc
== 0)
8589 gfc_error ("Binding label '%s' at %L collides with global "
8590 "entity '%s' at %L", sym
->binding_label
,
8591 &(sym
->declared_at
), bind_c_sym
->name
,
8592 &(bind_c_sym
->where
));
8597 /* Clear the binding label to prevent checking multiple times. */
8598 sym
->binding_label
[0] = '\0';
8600 else if (bind_c_sym
== NULL
)
8602 bind_c_sym
= gfc_get_gsymbol (sym
->binding_label
);
8603 bind_c_sym
->where
= sym
->declared_at
;
8604 bind_c_sym
->sym_name
= sym
->name
;
8606 if (sym
->attr
.use_assoc
== 1)
8607 bind_c_sym
->mod_name
= sym
->module
;
8609 if (sym
->ns
->proc_name
!= NULL
)
8610 bind_c_sym
->mod_name
= sym
->ns
->proc_name
->name
;
8612 if (sym
->attr
.contained
== 0)
8614 if (sym
->attr
.subroutine
)
8615 bind_c_sym
->type
= GSYM_SUBROUTINE
;
8616 else if (sym
->attr
.function
)
8617 bind_c_sym
->type
= GSYM_FUNCTION
;
8625 /* Resolve an index expression. */
8628 resolve_index_expr (gfc_expr
*e
)
8630 if (gfc_resolve_expr (e
) == FAILURE
)
8633 if (gfc_simplify_expr (e
, 0) == FAILURE
)
8636 if (gfc_specification_expr (e
) == FAILURE
)
8642 /* Resolve a charlen structure. */
8645 resolve_charlen (gfc_charlen
*cl
)
8654 specification_expr
= 1;
8656 if (resolve_index_expr (cl
->length
) == FAILURE
)
8658 specification_expr
= 0;
8662 /* "If the character length parameter value evaluates to a negative
8663 value, the length of character entities declared is zero." */
8664 if (cl
->length
&& !gfc_extract_int (cl
->length
, &i
) && i
< 0)
8666 if (gfc_option
.warn_surprising
)
8667 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8668 " the length has been set to zero",
8669 &cl
->length
->where
, i
);
8670 gfc_replace_expr (cl
->length
, gfc_int_expr (0));
8673 /* Check that the character length is not too large. */
8674 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
8675 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
8676 && cl
->length
->ts
.type
== BT_INTEGER
8677 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
8679 gfc_error ("String length at %L is too large", &cl
->length
->where
);
8687 /* Test for non-constant shape arrays. */
8690 is_non_constant_shape_array (gfc_symbol
*sym
)
8696 not_constant
= false;
8697 if (sym
->as
!= NULL
)
8699 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8700 has not been simplified; parameter array references. Do the
8701 simplification now. */
8702 for (i
= 0; i
< sym
->as
->rank
+ sym
->as
->corank
; i
++)
8704 e
= sym
->as
->lower
[i
];
8705 if (e
&& (resolve_index_expr (e
) == FAILURE
8706 || !gfc_is_constant_expr (e
)))
8707 not_constant
= true;
8708 e
= sym
->as
->upper
[i
];
8709 if (e
&& (resolve_index_expr (e
) == FAILURE
8710 || !gfc_is_constant_expr (e
)))
8711 not_constant
= true;
8714 return not_constant
;
8717 /* Given a symbol and an initialization expression, add code to initialize
8718 the symbol to the function entry. */
8720 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
8724 gfc_namespace
*ns
= sym
->ns
;
8726 /* Search for the function namespace if this is a contained
8727 function without an explicit result. */
8728 if (sym
->attr
.function
&& sym
== sym
->result
8729 && sym
->name
!= sym
->ns
->proc_name
->name
)
8732 for (;ns
; ns
= ns
->sibling
)
8733 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
8739 gfc_free_expr (init
);
8743 /* Build an l-value expression for the result. */
8744 lval
= gfc_lval_expr_from_sym (sym
);
8746 /* Add the code at scope entry. */
8747 init_st
= gfc_get_code ();
8748 init_st
->next
= ns
->code
;
8751 /* Assign the default initializer to the l-value. */
8752 init_st
->loc
= sym
->declared_at
;
8753 init_st
->op
= EXEC_INIT_ASSIGN
;
8754 init_st
->expr1
= lval
;
8755 init_st
->expr2
= init
;
8758 /* Assign the default initializer to a derived type variable or result. */
8761 apply_default_init (gfc_symbol
*sym
)
8763 gfc_expr
*init
= NULL
;
8765 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
8768 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
8769 init
= gfc_default_initializer (&sym
->ts
);
8774 build_init_assign (sym
, init
);
8777 /* Build an initializer for a local integer, real, complex, logical, or
8778 character variable, based on the command line flags finit-local-zero,
8779 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8780 null if the symbol should not have a default initialization. */
8782 build_default_init_expr (gfc_symbol
*sym
)
8785 gfc_expr
*init_expr
;
8788 /* These symbols should never have a default initialization. */
8789 if ((sym
->attr
.dimension
&& !gfc_is_compile_time_shape (sym
->as
))
8790 || sym
->attr
.external
8792 || sym
->attr
.pointer
8793 || sym
->attr
.in_equivalence
8794 || sym
->attr
.in_common
8797 || sym
->attr
.cray_pointee
8798 || sym
->attr
.cray_pointer
)
8801 /* Now we'll try to build an initializer expression. */
8802 init_expr
= gfc_get_expr ();
8803 init_expr
->expr_type
= EXPR_CONSTANT
;
8804 init_expr
->ts
.type
= sym
->ts
.type
;
8805 init_expr
->ts
.kind
= sym
->ts
.kind
;
8806 init_expr
->where
= sym
->declared_at
;
8808 /* We will only initialize integers, reals, complex, logicals, and
8809 characters, and only if the corresponding command-line flags
8810 were set. Otherwise, we free init_expr and return null. */
8811 switch (sym
->ts
.type
)
8814 if (gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
8815 mpz_init_set_si (init_expr
->value
.integer
,
8816 gfc_option
.flag_init_integer_value
);
8819 gfc_free_expr (init_expr
);
8825 mpfr_init (init_expr
->value
.real
);
8826 switch (gfc_option
.flag_init_real
)
8828 case GFC_INIT_REAL_SNAN
:
8829 init_expr
->is_snan
= 1;
8831 case GFC_INIT_REAL_NAN
:
8832 mpfr_set_nan (init_expr
->value
.real
);
8835 case GFC_INIT_REAL_INF
:
8836 mpfr_set_inf (init_expr
->value
.real
, 1);
8839 case GFC_INIT_REAL_NEG_INF
:
8840 mpfr_set_inf (init_expr
->value
.real
, -1);
8843 case GFC_INIT_REAL_ZERO
:
8844 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
8848 gfc_free_expr (init_expr
);
8855 mpc_init2 (init_expr
->value
.complex, mpfr_get_default_prec());
8856 switch (gfc_option
.flag_init_real
)
8858 case GFC_INIT_REAL_SNAN
:
8859 init_expr
->is_snan
= 1;
8861 case GFC_INIT_REAL_NAN
:
8862 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
8863 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
8866 case GFC_INIT_REAL_INF
:
8867 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
8868 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
8871 case GFC_INIT_REAL_NEG_INF
:
8872 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
8873 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
8876 case GFC_INIT_REAL_ZERO
:
8877 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
8881 gfc_free_expr (init_expr
);
8888 if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_FALSE
)
8889 init_expr
->value
.logical
= 0;
8890 else if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_TRUE
)
8891 init_expr
->value
.logical
= 1;
8894 gfc_free_expr (init_expr
);
8900 /* For characters, the length must be constant in order to
8901 create a default initializer. */
8902 if (gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
8903 && sym
->ts
.u
.cl
->length
8904 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8906 char_len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
8907 init_expr
->value
.character
.length
= char_len
;
8908 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
8909 for (i
= 0; i
< char_len
; i
++)
8910 init_expr
->value
.character
.string
[i
]
8911 = (unsigned char) gfc_option
.flag_init_character_value
;
8915 gfc_free_expr (init_expr
);
8921 gfc_free_expr (init_expr
);
8927 /* Add an initialization expression to a local variable. */
8929 apply_default_init_local (gfc_symbol
*sym
)
8931 gfc_expr
*init
= NULL
;
8933 /* The symbol should be a variable or a function return value. */
8934 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
8935 || (sym
->attr
.function
&& sym
->result
!= sym
))
8938 /* Try to build the initializer expression. If we can't initialize
8939 this symbol, then init will be NULL. */
8940 init
= build_default_init_expr (sym
);
8944 /* For saved variables, we don't want to add an initializer at
8945 function entry, so we just add a static initializer. */
8946 if (sym
->attr
.save
|| sym
->ns
->save_all
8947 || gfc_option
.flag_max_stack_var_size
== 0)
8949 /* Don't clobber an existing initializer! */
8950 gcc_assert (sym
->value
== NULL
);
8955 build_init_assign (sym
, init
);
8958 /* Resolution of common features of flavors variable and procedure. */
8961 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
8963 /* Constraints on deferred shape variable. */
8964 if (sym
->as
== NULL
|| sym
->as
->type
!= AS_DEFERRED
)
8966 if (sym
->attr
.allocatable
)
8968 if (sym
->attr
.dimension
)
8970 gfc_error ("Allocatable array '%s' at %L must have "
8971 "a deferred shape", sym
->name
, &sym
->declared_at
);
8974 else if (gfc_notify_std (GFC_STD_F2003
, "Scalar object '%s' at %L "
8975 "may not be ALLOCATABLE", sym
->name
,
8976 &sym
->declared_at
) == FAILURE
)
8980 if (sym
->attr
.pointer
&& sym
->attr
.dimension
)
8982 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8983 sym
->name
, &sym
->declared_at
);
8990 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
8991 && !sym
->attr
.dummy
&& sym
->ts
.type
!= BT_CLASS
)
8993 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8994 sym
->name
, &sym
->declared_at
);
9002 /* Additional checks for symbols with flavor variable and derived
9003 type. To be called from resolve_fl_variable. */
9006 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
9008 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
9010 /* Check to see if a derived type is blocked from being host
9011 associated by the presence of another class I symbol in the same
9012 namespace. 14.6.1.3 of the standard and the discussion on
9013 comp.lang.fortran. */
9014 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
9015 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
9018 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
9019 if (s
&& s
->attr
.flavor
!= FL_DERIVED
)
9021 gfc_error ("The type '%s' cannot be host associated at %L "
9022 "because it is blocked by an incompatible object "
9023 "of the same name declared at %L",
9024 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
9030 /* 4th constraint in section 11.3: "If an object of a type for which
9031 component-initialization is specified (R429) appears in the
9032 specification-part of a module and does not have the ALLOCATABLE
9033 or POINTER attribute, the object shall have the SAVE attribute."
9035 The check for initializers is performed with
9036 has_default_initializer because gfc_default_initializer generates
9037 a hidden default for allocatable components. */
9038 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
9039 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9040 && !sym
->ns
->save_all
&& !sym
->attr
.save
9041 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
9042 && has_default_initializer (sym
->ts
.u
.derived
)
9043 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Implied SAVE for "
9044 "module variable '%s' at %L, needed due to "
9045 "the default initialization", sym
->name
,
9046 &sym
->declared_at
) == FAILURE
)
9049 if (sym
->ts
.type
== BT_CLASS
)
9052 if (!gfc_type_is_extensible (sym
->ts
.u
.derived
->components
->ts
.u
.derived
))
9054 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9055 sym
->ts
.u
.derived
->components
->ts
.u
.derived
->name
,
9056 sym
->name
, &sym
->declared_at
);
9061 /* Assume that use associated symbols were checked in the module ns. */
9062 if (!sym
->attr
.class_ok
&& !sym
->attr
.use_assoc
)
9064 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9065 "or pointer", sym
->name
, &sym
->declared_at
);
9070 /* Assign default initializer. */
9071 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
9072 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
9074 sym
->value
= gfc_default_initializer (&sym
->ts
);
9081 /* Resolve symbols with flavor variable. */
9084 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
9086 int no_init_flag
, automatic_flag
;
9088 const char *auto_save_msg
;
9090 auto_save_msg
= "Automatic object '%s' at %L cannot have the "
9093 if (resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
9096 /* Set this flag to check that variables are parameters of all entries.
9097 This check is effected by the call to gfc_resolve_expr through
9098 is_non_constant_shape_array. */
9099 specification_expr
= 1;
9101 if (sym
->ns
->proc_name
9102 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9103 || sym
->ns
->proc_name
->attr
.is_main_program
)
9104 && !sym
->attr
.use_assoc
9105 && !sym
->attr
.allocatable
9106 && !sym
->attr
.pointer
9107 && is_non_constant_shape_array (sym
))
9109 /* The shape of a main program or module array needs to be
9111 gfc_error ("The module or main program array '%s' at %L must "
9112 "have constant shape", sym
->name
, &sym
->declared_at
);
9113 specification_expr
= 0;
9117 if (sym
->ts
.type
== BT_CHARACTER
)
9119 /* Make sure that character string variables with assumed length are
9121 e
= sym
->ts
.u
.cl
->length
;
9122 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
)
9124 gfc_error ("Entity with assumed character length at %L must be a "
9125 "dummy argument or a PARAMETER", &sym
->declared_at
);
9129 if (e
&& sym
->attr
.save
&& !gfc_is_constant_expr (e
))
9131 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
9135 if (!gfc_is_constant_expr (e
)
9136 && !(e
->expr_type
== EXPR_VARIABLE
9137 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
9138 && sym
->ns
->proc_name
9139 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
9140 || sym
->ns
->proc_name
->attr
.is_main_program
)
9141 && !sym
->attr
.use_assoc
)
9143 gfc_error ("'%s' at %L must have constant character length "
9144 "in this context", sym
->name
, &sym
->declared_at
);
9149 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
9150 apply_default_init_local (sym
); /* Try to apply a default initialization. */
9152 /* Determine if the symbol may not have an initializer. */
9153 no_init_flag
= automatic_flag
= 0;
9154 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
9155 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
9157 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
) && !sym
->attr
.pointer
9158 && is_non_constant_shape_array (sym
))
9160 no_init_flag
= automatic_flag
= 1;
9162 /* Also, they must not have the SAVE attribute.
9163 SAVE_IMPLICIT is checked below. */
9164 if (sym
->attr
.save
== SAVE_EXPLICIT
)
9166 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
9171 /* Ensure that any initializer is simplified. */
9173 gfc_simplify_expr (sym
->value
, 1);
9175 /* Reject illegal initializers. */
9176 if (!sym
->mark
&& sym
->value
)
9178 if (sym
->attr
.allocatable
)
9179 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9180 sym
->name
, &sym
->declared_at
);
9181 else if (sym
->attr
.external
)
9182 gfc_error ("External '%s' at %L cannot have an initializer",
9183 sym
->name
, &sym
->declared_at
);
9184 else if (sym
->attr
.dummy
9185 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
9186 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9187 sym
->name
, &sym
->declared_at
);
9188 else if (sym
->attr
.intrinsic
)
9189 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9190 sym
->name
, &sym
->declared_at
);
9191 else if (sym
->attr
.result
)
9192 gfc_error ("Function result '%s' at %L cannot have an initializer",
9193 sym
->name
, &sym
->declared_at
);
9194 else if (automatic_flag
)
9195 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9196 sym
->name
, &sym
->declared_at
);
9203 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
9204 return resolve_fl_variable_derived (sym
, no_init_flag
);
9210 /* Resolve a procedure. */
9213 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
9215 gfc_formal_arglist
*arg
;
9217 if (sym
->attr
.function
9218 && resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
9221 if (sym
->ts
.type
== BT_CHARACTER
)
9223 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
9225 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
9226 && resolve_charlen (cl
) == FAILURE
)
9229 if ((!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
9230 && sym
->attr
.proc
== PROC_ST_FUNCTION
)
9232 gfc_error ("Character-valued statement function '%s' at %L must "
9233 "have constant length", sym
->name
, &sym
->declared_at
);
9238 /* Ensure that derived type for are not of a private type. Internal
9239 module procedures are excluded by 2.2.3.3 - i.e., they are not
9240 externally accessible and can access all the objects accessible in
9242 if (!(sym
->ns
->parent
9243 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
9244 && gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
9246 gfc_interface
*iface
;
9248 for (arg
= sym
->formal
; arg
; arg
= arg
->next
)
9251 && arg
->sym
->ts
.type
== BT_DERIVED
9252 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
9253 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
9254 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
9255 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: '%s' is of a "
9256 "PRIVATE type and cannot be a dummy argument"
9257 " of '%s', which is PUBLIC at %L",
9258 arg
->sym
->name
, sym
->name
, &sym
->declared_at
)
9261 /* Stop this message from recurring. */
9262 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
9267 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9268 PRIVATE to the containing module. */
9269 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
9271 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
9274 && arg
->sym
->ts
.type
== BT_DERIVED
9275 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
9276 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
9277 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
9278 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
9279 "'%s' in PUBLIC interface '%s' at %L "
9280 "takes dummy arguments of '%s' which is "
9281 "PRIVATE", iface
->sym
->name
, sym
->name
,
9282 &iface
->sym
->declared_at
,
9283 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
9285 /* Stop this message from recurring. */
9286 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
9292 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9293 PRIVATE to the containing module. */
9294 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
9296 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
9299 && arg
->sym
->ts
.type
== BT_DERIVED
9300 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
9301 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
9302 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
9303 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
9304 "'%s' in PUBLIC interface '%s' at %L "
9305 "takes dummy arguments of '%s' which is "
9306 "PRIVATE", iface
->sym
->name
, sym
->name
,
9307 &iface
->sym
->declared_at
,
9308 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
9310 /* Stop this message from recurring. */
9311 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
9318 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
9319 && !sym
->attr
.proc_pointer
)
9321 gfc_error ("Function '%s' at %L cannot have an initializer",
9322 sym
->name
, &sym
->declared_at
);
9326 /* An external symbol may not have an initializer because it is taken to be
9327 a procedure. Exception: Procedure Pointers. */
9328 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
9330 gfc_error ("External object '%s' at %L may not have an initializer",
9331 sym
->name
, &sym
->declared_at
);
9335 /* An elemental function is required to return a scalar 12.7.1 */
9336 if (sym
->attr
.elemental
&& sym
->attr
.function
&& sym
->as
)
9338 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9339 "result", sym
->name
, &sym
->declared_at
);
9340 /* Reset so that the error only occurs once. */
9341 sym
->attr
.elemental
= 0;
9345 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9346 char-len-param shall not be array-valued, pointer-valued, recursive
9347 or pure. ....snip... A character value of * may only be used in the
9348 following ways: (i) Dummy arg of procedure - dummy associates with
9349 actual length; (ii) To declare a named constant; or (iii) External
9350 function - but length must be declared in calling scoping unit. */
9351 if (sym
->attr
.function
9352 && sym
->ts
.type
== BT_CHARACTER
9353 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
9355 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
9356 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
9358 if (sym
->as
&& sym
->as
->rank
)
9359 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9360 "array-valued", sym
->name
, &sym
->declared_at
);
9362 if (sym
->attr
.pointer
)
9363 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9364 "pointer-valued", sym
->name
, &sym
->declared_at
);
9367 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9368 "pure", sym
->name
, &sym
->declared_at
);
9370 if (sym
->attr
.recursive
)
9371 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9372 "recursive", sym
->name
, &sym
->declared_at
);
9377 /* Appendix B.2 of the standard. Contained functions give an
9378 error anyway. Fixed-form is likely to be F77/legacy. */
9379 if (!sym
->attr
.contained
&& gfc_current_form
!= FORM_FIXED
)
9380 gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
9381 "CHARACTER(*) function '%s' at %L",
9382 sym
->name
, &sym
->declared_at
);
9385 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
9387 gfc_formal_arglist
*curr_arg
;
9388 int has_non_interop_arg
= 0;
9390 if (verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
9391 sym
->common_block
) == FAILURE
)
9393 /* Clear these to prevent looking at them again if there was an
9395 sym
->attr
.is_bind_c
= 0;
9396 sym
->attr
.is_c_interop
= 0;
9397 sym
->ts
.is_c_interop
= 0;
9401 /* So far, no errors have been found. */
9402 sym
->attr
.is_c_interop
= 1;
9403 sym
->ts
.is_c_interop
= 1;
9406 curr_arg
= sym
->formal
;
9407 while (curr_arg
!= NULL
)
9409 /* Skip implicitly typed dummy args here. */
9410 if (curr_arg
->sym
->attr
.implicit_type
== 0)
9411 if (verify_c_interop_param (curr_arg
->sym
) == FAILURE
)
9412 /* If something is found to fail, record the fact so we
9413 can mark the symbol for the procedure as not being
9414 BIND(C) to try and prevent multiple errors being
9416 has_non_interop_arg
= 1;
9418 curr_arg
= curr_arg
->next
;
9421 /* See if any of the arguments were not interoperable and if so, clear
9422 the procedure symbol to prevent duplicate error messages. */
9423 if (has_non_interop_arg
!= 0)
9425 sym
->attr
.is_c_interop
= 0;
9426 sym
->ts
.is_c_interop
= 0;
9427 sym
->attr
.is_bind_c
= 0;
9431 if (!sym
->attr
.proc_pointer
)
9433 if (sym
->attr
.save
== SAVE_EXPLICIT
)
9435 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9436 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
9439 if (sym
->attr
.intent
)
9441 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9442 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
9445 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
9447 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9448 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
9451 if (sym
->attr
.external
&& sym
->attr
.function
9452 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
9453 || sym
->attr
.contained
))
9455 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9456 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
9459 if (strcmp ("ppr@", sym
->name
) == 0)
9461 gfc_error ("Procedure pointer result '%s' at %L "
9462 "is missing the pointer attribute",
9463 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
9472 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9473 been defined and we now know their defined arguments, check that they fulfill
9474 the requirements of the standard for procedures used as finalizers. */
9477 gfc_resolve_finalizers (gfc_symbol
* derived
)
9479 gfc_finalizer
* list
;
9480 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
9481 gfc_try result
= SUCCESS
;
9482 bool seen_scalar
= false;
9484 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->finalizers
)
9487 /* Walk over the list of finalizer-procedures, check them, and if any one
9488 does not fit in with the standard's definition, print an error and remove
9489 it from the list. */
9490 prev_link
= &derived
->f2k_derived
->finalizers
;
9491 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
9497 /* Skip this finalizer if we already resolved it. */
9498 if (list
->proc_tree
)
9500 prev_link
= &(list
->next
);
9504 /* Check this exists and is a SUBROUTINE. */
9505 if (!list
->proc_sym
->attr
.subroutine
)
9507 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9508 list
->proc_sym
->name
, &list
->where
);
9512 /* We should have exactly one argument. */
9513 if (!list
->proc_sym
->formal
|| list
->proc_sym
->formal
->next
)
9515 gfc_error ("FINAL procedure at %L must have exactly one argument",
9519 arg
= list
->proc_sym
->formal
->sym
;
9521 /* This argument must be of our type. */
9522 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
9524 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9525 &arg
->declared_at
, derived
->name
);
9529 /* It must neither be a pointer nor allocatable nor optional. */
9530 if (arg
->attr
.pointer
)
9532 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9536 if (arg
->attr
.allocatable
)
9538 gfc_error ("Argument of FINAL procedure at %L must not be"
9539 " ALLOCATABLE", &arg
->declared_at
);
9542 if (arg
->attr
.optional
)
9544 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9549 /* It must not be INTENT(OUT). */
9550 if (arg
->attr
.intent
== INTENT_OUT
)
9552 gfc_error ("Argument of FINAL procedure at %L must not be"
9553 " INTENT(OUT)", &arg
->declared_at
);
9557 /* Warn if the procedure is non-scalar and not assumed shape. */
9558 if (gfc_option
.warn_surprising
&& arg
->as
&& arg
->as
->rank
> 0
9559 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
9560 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9561 " shape argument", &arg
->declared_at
);
9563 /* Check that it does not match in kind and rank with a FINAL procedure
9564 defined earlier. To really loop over the *earlier* declarations,
9565 we need to walk the tail of the list as new ones were pushed at the
9567 /* TODO: Handle kind parameters once they are implemented. */
9568 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
9569 for (i
= list
->next
; i
; i
= i
->next
)
9571 /* Argument list might be empty; that is an error signalled earlier,
9572 but we nevertheless continued resolving. */
9573 if (i
->proc_sym
->formal
)
9575 gfc_symbol
* i_arg
= i
->proc_sym
->formal
->sym
;
9576 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
9577 if (i_rank
== my_rank
)
9579 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9580 " rank (%d) as '%s'",
9581 list
->proc_sym
->name
, &list
->where
, my_rank
,
9588 /* Is this the/a scalar finalizer procedure? */
9589 if (!arg
->as
|| arg
->as
->rank
== 0)
9592 /* Find the symtree for this procedure. */
9593 gcc_assert (!list
->proc_tree
);
9594 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
9596 prev_link
= &list
->next
;
9599 /* Remove wrong nodes immediately from the list so we don't risk any
9600 troubles in the future when they might fail later expectations. */
9604 *prev_link
= list
->next
;
9605 gfc_free_finalizer (i
);
9608 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9609 were nodes in the list, must have been for arrays. It is surely a good
9610 idea to have a scalar version there if there's something to finalize. */
9611 if (gfc_option
.warn_surprising
&& result
== SUCCESS
&& !seen_scalar
)
9612 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9613 " defined at %L, suggest also scalar one",
9614 derived
->name
, &derived
->declared_at
);
9616 /* TODO: Remove this error when finalization is finished. */
9617 gfc_error ("Finalization at %L is not yet implemented",
9618 &derived
->declared_at
);
9624 /* Check that it is ok for the typebound procedure proc to override the
9628 check_typebound_override (gfc_symtree
* proc
, gfc_symtree
* old
)
9631 const gfc_symbol
* proc_target
;
9632 const gfc_symbol
* old_target
;
9633 unsigned proc_pass_arg
, old_pass_arg
, argpos
;
9634 gfc_formal_arglist
* proc_formal
;
9635 gfc_formal_arglist
* old_formal
;
9637 /* This procedure should only be called for non-GENERIC proc. */
9638 gcc_assert (!proc
->n
.tb
->is_generic
);
9640 /* If the overwritten procedure is GENERIC, this is an error. */
9641 if (old
->n
.tb
->is_generic
)
9643 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9644 old
->name
, &proc
->n
.tb
->where
);
9648 where
= proc
->n
.tb
->where
;
9649 proc_target
= proc
->n
.tb
->u
.specific
->n
.sym
;
9650 old_target
= old
->n
.tb
->u
.specific
->n
.sym
;
9652 /* Check that overridden binding is not NON_OVERRIDABLE. */
9653 if (old
->n
.tb
->non_overridable
)
9655 gfc_error ("'%s' at %L overrides a procedure binding declared"
9656 " NON_OVERRIDABLE", proc
->name
, &where
);
9660 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9661 if (!old
->n
.tb
->deferred
&& proc
->n
.tb
->deferred
)
9663 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9664 " non-DEFERRED binding", proc
->name
, &where
);
9668 /* If the overridden binding is PURE, the overriding must be, too. */
9669 if (old_target
->attr
.pure
&& !proc_target
->attr
.pure
)
9671 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9672 proc
->name
, &where
);
9676 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9677 is not, the overriding must not be either. */
9678 if (old_target
->attr
.elemental
&& !proc_target
->attr
.elemental
)
9680 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9681 " ELEMENTAL", proc
->name
, &where
);
9684 if (!old_target
->attr
.elemental
&& proc_target
->attr
.elemental
)
9686 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9687 " be ELEMENTAL, either", proc
->name
, &where
);
9691 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9693 if (old_target
->attr
.subroutine
&& !proc_target
->attr
.subroutine
)
9695 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9696 " SUBROUTINE", proc
->name
, &where
);
9700 /* If the overridden binding is a FUNCTION, the overriding must also be a
9701 FUNCTION and have the same characteristics. */
9702 if (old_target
->attr
.function
)
9704 if (!proc_target
->attr
.function
)
9706 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9707 " FUNCTION", proc
->name
, &where
);
9711 /* FIXME: Do more comprehensive checking (including, for instance, the
9712 rank and array-shape). */
9713 gcc_assert (proc_target
->result
&& old_target
->result
);
9714 if (!gfc_compare_types (&proc_target
->result
->ts
,
9715 &old_target
->result
->ts
))
9717 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9718 " matching result types", proc
->name
, &where
);
9723 /* If the overridden binding is PUBLIC, the overriding one must not be
9725 if (old
->n
.tb
->access
== ACCESS_PUBLIC
9726 && proc
->n
.tb
->access
== ACCESS_PRIVATE
)
9728 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9729 " PRIVATE", proc
->name
, &where
);
9733 /* Compare the formal argument lists of both procedures. This is also abused
9734 to find the position of the passed-object dummy arguments of both
9735 bindings as at least the overridden one might not yet be resolved and we
9736 need those positions in the check below. */
9737 proc_pass_arg
= old_pass_arg
= 0;
9738 if (!proc
->n
.tb
->nopass
&& !proc
->n
.tb
->pass_arg
)
9740 if (!old
->n
.tb
->nopass
&& !old
->n
.tb
->pass_arg
)
9743 for (proc_formal
= proc_target
->formal
, old_formal
= old_target
->formal
;
9744 proc_formal
&& old_formal
;
9745 proc_formal
= proc_formal
->next
, old_formal
= old_formal
->next
)
9747 if (proc
->n
.tb
->pass_arg
9748 && !strcmp (proc
->n
.tb
->pass_arg
, proc_formal
->sym
->name
))
9749 proc_pass_arg
= argpos
;
9750 if (old
->n
.tb
->pass_arg
9751 && !strcmp (old
->n
.tb
->pass_arg
, old_formal
->sym
->name
))
9752 old_pass_arg
= argpos
;
9754 /* Check that the names correspond. */
9755 if (strcmp (proc_formal
->sym
->name
, old_formal
->sym
->name
))
9757 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9758 " to match the corresponding argument of the overridden"
9759 " procedure", proc_formal
->sym
->name
, proc
->name
, &where
,
9760 old_formal
->sym
->name
);
9764 /* Check that the types correspond if neither is the passed-object
9766 /* FIXME: Do more comprehensive testing here. */
9767 if (proc_pass_arg
!= argpos
&& old_pass_arg
!= argpos
9768 && !gfc_compare_types (&proc_formal
->sym
->ts
, &old_formal
->sym
->ts
))
9770 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9771 "in respect to the overridden procedure",
9772 proc_formal
->sym
->name
, proc
->name
, &where
);
9778 if (proc_formal
|| old_formal
)
9780 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9781 " the overridden procedure", proc
->name
, &where
);
9785 /* If the overridden binding is NOPASS, the overriding one must also be
9787 if (old
->n
.tb
->nopass
&& !proc
->n
.tb
->nopass
)
9789 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9790 " NOPASS", proc
->name
, &where
);
9794 /* If the overridden binding is PASS(x), the overriding one must also be
9795 PASS and the passed-object dummy arguments must correspond. */
9796 if (!old
->n
.tb
->nopass
)
9798 if (proc
->n
.tb
->nopass
)
9800 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9801 " PASS", proc
->name
, &where
);
9805 if (proc_pass_arg
!= old_pass_arg
)
9807 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9808 " the same position as the passed-object dummy argument of"
9809 " the overridden procedure", proc
->name
, &where
);
9818 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9821 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
9822 const char* generic_name
, locus where
)
9827 gcc_assert (t1
->specific
&& t2
->specific
);
9828 gcc_assert (!t1
->specific
->is_generic
);
9829 gcc_assert (!t2
->specific
->is_generic
);
9831 sym1
= t1
->specific
->u
.specific
->n
.sym
;
9832 sym2
= t2
->specific
->u
.specific
->n
.sym
;
9837 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9838 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
9839 || sym1
->attr
.function
!= sym2
->attr
.function
)
9841 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9842 " GENERIC '%s' at %L",
9843 sym1
->name
, sym2
->name
, generic_name
, &where
);
9847 /* Compare the interfaces. */
9848 if (gfc_compare_interfaces (sym1
, sym2
, sym2
->name
, 1, 0, NULL
, 0))
9850 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9851 sym1
->name
, sym2
->name
, generic_name
, &where
);
9859 /* Worker function for resolving a generic procedure binding; this is used to
9860 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9862 The difference between those cases is finding possible inherited bindings
9863 that are overridden, as one has to look for them in tb_sym_root,
9864 tb_uop_root or tb_op, respectively. Thus the caller must already find
9865 the super-type and set p->overridden correctly. */
9868 resolve_tb_generic_targets (gfc_symbol
* super_type
,
9869 gfc_typebound_proc
* p
, const char* name
)
9871 gfc_tbp_generic
* target
;
9872 gfc_symtree
* first_target
;
9873 gfc_symtree
* inherited
;
9875 gcc_assert (p
&& p
->is_generic
);
9877 /* Try to find the specific bindings for the symtrees in our target-list. */
9878 gcc_assert (p
->u
.generic
);
9879 for (target
= p
->u
.generic
; target
; target
= target
->next
)
9880 if (!target
->specific
)
9882 gfc_typebound_proc
* overridden_tbp
;
9884 const char* target_name
;
9886 target_name
= target
->specific_st
->name
;
9888 /* Defined for this type directly. */
9889 if (target
->specific_st
->n
.tb
)
9891 target
->specific
= target
->specific_st
->n
.tb
;
9892 goto specific_found
;
9895 /* Look for an inherited specific binding. */
9898 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
9903 gcc_assert (inherited
->n
.tb
);
9904 target
->specific
= inherited
->n
.tb
;
9905 goto specific_found
;
9909 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9910 " at %L", target_name
, name
, &p
->where
);
9913 /* Once we've found the specific binding, check it is not ambiguous with
9914 other specifics already found or inherited for the same GENERIC. */
9916 gcc_assert (target
->specific
);
9918 /* This must really be a specific binding! */
9919 if (target
->specific
->is_generic
)
9921 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9922 " '%s' is GENERIC, too", name
, &p
->where
, target_name
);
9926 /* Check those already resolved on this type directly. */
9927 for (g
= p
->u
.generic
; g
; g
= g
->next
)
9928 if (g
!= target
&& g
->specific
9929 && check_generic_tbp_ambiguity (target
, g
, name
, p
->where
)
9933 /* Check for ambiguity with inherited specific targets. */
9934 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
9935 overridden_tbp
= overridden_tbp
->overridden
)
9936 if (overridden_tbp
->is_generic
)
9938 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
9940 gcc_assert (g
->specific
);
9941 if (check_generic_tbp_ambiguity (target
, g
,
9942 name
, p
->where
) == FAILURE
)
9948 /* If we attempt to "overwrite" a specific binding, this is an error. */
9949 if (p
->overridden
&& !p
->overridden
->is_generic
)
9951 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9952 " the same name", name
, &p
->where
);
9956 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9957 all must have the same attributes here. */
9958 first_target
= p
->u
.generic
->specific
->u
.specific
;
9959 gcc_assert (first_target
);
9960 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
9961 p
->function
= first_target
->n
.sym
->attr
.function
;
9967 /* Resolve a GENERIC procedure binding for a derived type. */
9970 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
9972 gfc_symbol
* super_type
;
9974 /* Find the overridden binding if any. */
9975 st
->n
.tb
->overridden
= NULL
;
9976 super_type
= gfc_get_derived_super_type (derived
);
9979 gfc_symtree
* overridden
;
9980 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
9983 if (overridden
&& overridden
->n
.tb
)
9984 st
->n
.tb
->overridden
= overridden
->n
.tb
;
9987 /* Resolve using worker function. */
9988 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
9992 /* Retrieve the target-procedure of an operator binding and do some checks in
9993 common for intrinsic and user-defined type-bound operators. */
9996 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
9998 gfc_symbol
* target_proc
;
10000 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
10001 target_proc
= target
->specific
->u
.specific
->n
.sym
;
10002 gcc_assert (target_proc
);
10004 /* All operator bindings must have a passed-object dummy argument. */
10005 if (target
->specific
->nopass
)
10007 gfc_error ("Type-bound operator at %L can't be NOPASS", &where
);
10011 return target_proc
;
10015 /* Resolve a type-bound intrinsic operator. */
10018 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
10019 gfc_typebound_proc
* p
)
10021 gfc_symbol
* super_type
;
10022 gfc_tbp_generic
* target
;
10024 /* If there's already an error here, do nothing (but don't fail again). */
10028 /* Operators should always be GENERIC bindings. */
10029 gcc_assert (p
->is_generic
);
10031 /* Look for an overridden binding. */
10032 super_type
= gfc_get_derived_super_type (derived
);
10033 if (super_type
&& super_type
->f2k_derived
)
10034 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
10037 p
->overridden
= NULL
;
10039 /* Resolve general GENERIC properties using worker function. */
10040 if (resolve_tb_generic_targets (super_type
, p
, gfc_op2string (op
)) == FAILURE
)
10043 /* Check the targets to be procedures of correct interface. */
10044 for (target
= p
->u
.generic
; target
; target
= target
->next
)
10046 gfc_symbol
* target_proc
;
10048 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
10052 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
10064 /* Resolve a type-bound user operator (tree-walker callback). */
10066 static gfc_symbol
* resolve_bindings_derived
;
10067 static gfc_try resolve_bindings_result
;
10069 static gfc_try
check_uop_procedure (gfc_symbol
* sym
, locus where
);
10072 resolve_typebound_user_op (gfc_symtree
* stree
)
10074 gfc_symbol
* super_type
;
10075 gfc_tbp_generic
* target
;
10077 gcc_assert (stree
&& stree
->n
.tb
);
10079 if (stree
->n
.tb
->error
)
10082 /* Operators should always be GENERIC bindings. */
10083 gcc_assert (stree
->n
.tb
->is_generic
);
10085 /* Find overridden procedure, if any. */
10086 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
10087 if (super_type
&& super_type
->f2k_derived
)
10089 gfc_symtree
* overridden
;
10090 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
10091 stree
->name
, true, NULL
);
10093 if (overridden
&& overridden
->n
.tb
)
10094 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
10097 stree
->n
.tb
->overridden
= NULL
;
10099 /* Resolve basically using worker function. */
10100 if (resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
)
10104 /* Check the targets to be functions of correct interface. */
10105 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
10107 gfc_symbol
* target_proc
;
10109 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
10113 if (check_uop_procedure (target_proc
, stree
->n
.tb
->where
) == FAILURE
)
10120 resolve_bindings_result
= FAILURE
;
10121 stree
->n
.tb
->error
= 1;
10125 /* Resolve the type-bound procedures for a derived type. */
10128 resolve_typebound_procedure (gfc_symtree
* stree
)
10132 gfc_symbol
* me_arg
;
10133 gfc_symbol
* super_type
;
10134 gfc_component
* comp
;
10136 gcc_assert (stree
);
10138 /* Undefined specific symbol from GENERIC target definition. */
10142 if (stree
->n
.tb
->error
)
10145 /* If this is a GENERIC binding, use that routine. */
10146 if (stree
->n
.tb
->is_generic
)
10148 if (resolve_typebound_generic (resolve_bindings_derived
, stree
)
10154 /* Get the target-procedure to check it. */
10155 gcc_assert (!stree
->n
.tb
->is_generic
);
10156 gcc_assert (stree
->n
.tb
->u
.specific
);
10157 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
10158 where
= stree
->n
.tb
->where
;
10160 /* Default access should already be resolved from the parser. */
10161 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
10163 /* It should be a module procedure or an external procedure with explicit
10164 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10165 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
10166 || (proc
->attr
.proc
!= PROC_MODULE
10167 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
10168 || (proc
->attr
.abstract
&& !stree
->n
.tb
->deferred
))
10170 gfc_error ("'%s' must be a module procedure or an external procedure with"
10171 " an explicit interface at %L", proc
->name
, &where
);
10174 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
10175 stree
->n
.tb
->function
= proc
->attr
.function
;
10177 /* Find the super-type of the current derived type. We could do this once and
10178 store in a global if speed is needed, but as long as not I believe this is
10179 more readable and clearer. */
10180 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
10182 /* If PASS, resolve and check arguments if not already resolved / loaded
10183 from a .mod file. */
10184 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
10186 if (stree
->n
.tb
->pass_arg
)
10188 gfc_formal_arglist
* i
;
10190 /* If an explicit passing argument name is given, walk the arg-list
10191 and look for it. */
10194 stree
->n
.tb
->pass_arg_num
= 1;
10195 for (i
= proc
->formal
; i
; i
= i
->next
)
10197 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
10202 ++stree
->n
.tb
->pass_arg_num
;
10207 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10209 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
10210 stree
->n
.tb
->pass_arg
);
10216 /* Otherwise, take the first one; there should in fact be at least
10218 stree
->n
.tb
->pass_arg_num
= 1;
10221 gfc_error ("Procedure '%s' with PASS at %L must have at"
10222 " least one argument", proc
->name
, &where
);
10225 me_arg
= proc
->formal
->sym
;
10228 /* Now check that the argument-type matches and the passed-object
10229 dummy argument is generally fine. */
10231 gcc_assert (me_arg
);
10233 if (me_arg
->ts
.type
!= BT_CLASS
)
10235 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10236 " at %L", proc
->name
, &where
);
10240 if (me_arg
->ts
.u
.derived
->components
->ts
.u
.derived
10241 != resolve_bindings_derived
)
10243 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10244 " the derived-type '%s'", me_arg
->name
, proc
->name
,
10245 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
10249 gcc_assert (me_arg
->ts
.type
== BT_CLASS
);
10250 if (me_arg
->ts
.u
.derived
->components
->as
10251 && me_arg
->ts
.u
.derived
->components
->as
->rank
> 0)
10253 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10254 " scalar", proc
->name
, &where
);
10257 if (me_arg
->ts
.u
.derived
->components
->attr
.allocatable
)
10259 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10260 " be ALLOCATABLE", proc
->name
, &where
);
10263 if (me_arg
->ts
.u
.derived
->components
->attr
.class_pointer
)
10265 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10266 " be POINTER", proc
->name
, &where
);
10271 /* If we are extending some type, check that we don't override a procedure
10272 flagged NON_OVERRIDABLE. */
10273 stree
->n
.tb
->overridden
= NULL
;
10276 gfc_symtree
* overridden
;
10277 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
10278 stree
->name
, true, NULL
);
10280 if (overridden
&& overridden
->n
.tb
)
10281 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
10283 if (overridden
&& check_typebound_override (stree
, overridden
) == FAILURE
)
10287 /* See if there's a name collision with a component directly in this type. */
10288 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
10289 if (!strcmp (comp
->name
, stree
->name
))
10291 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10293 stree
->name
, &where
, resolve_bindings_derived
->name
);
10297 /* Try to find a name collision with an inherited component. */
10298 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true))
10300 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10301 " component of '%s'",
10302 stree
->name
, &where
, resolve_bindings_derived
->name
);
10306 stree
->n
.tb
->error
= 0;
10310 resolve_bindings_result
= FAILURE
;
10311 stree
->n
.tb
->error
= 1;
10315 resolve_typebound_procedures (gfc_symbol
* derived
)
10319 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
10322 resolve_bindings_derived
= derived
;
10323 resolve_bindings_result
= SUCCESS
;
10325 if (derived
->f2k_derived
->tb_sym_root
)
10326 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
10327 &resolve_typebound_procedure
);
10329 if (derived
->f2k_derived
->tb_uop_root
)
10330 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
10331 &resolve_typebound_user_op
);
10333 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
10335 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
10336 if (p
&& resolve_typebound_intrinsic_op (derived
, (gfc_intrinsic_op
) op
,
10338 resolve_bindings_result
= FAILURE
;
10341 return resolve_bindings_result
;
10345 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10346 to give all identical derived types the same backend_decl. */
10348 add_dt_to_dt_list (gfc_symbol
*derived
)
10350 gfc_dt_list
*dt_list
;
10352 for (dt_list
= gfc_derived_types
; dt_list
; dt_list
= dt_list
->next
)
10353 if (derived
== dt_list
->derived
)
10356 if (dt_list
== NULL
)
10358 dt_list
= gfc_get_dt_list ();
10359 dt_list
->next
= gfc_derived_types
;
10360 dt_list
->derived
= derived
;
10361 gfc_derived_types
= dt_list
;
10366 /* Ensure that a derived-type is really not abstract, meaning that every
10367 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10370 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
10375 if (ensure_not_abstract_walker (sub
, st
->left
) == FAILURE
)
10377 if (ensure_not_abstract_walker (sub
, st
->right
) == FAILURE
)
10380 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
10382 gfc_symtree
* overriding
;
10383 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
10384 gcc_assert (overriding
&& overriding
->n
.tb
);
10385 if (overriding
->n
.tb
->deferred
)
10387 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10388 " '%s' is DEFERRED and not overridden",
10389 sub
->name
, &sub
->declared_at
, st
->name
);
10398 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
10400 /* The algorithm used here is to recursively travel up the ancestry of sub
10401 and for each ancestor-type, check all bindings. If any of them is
10402 DEFERRED, look it up starting from sub and see if the found (overriding)
10403 binding is not DEFERRED.
10404 This is not the most efficient way to do this, but it should be ok and is
10405 clearer than something sophisticated. */
10407 gcc_assert (ancestor
&& ancestor
->attr
.abstract
&& !sub
->attr
.abstract
);
10409 /* Walk bindings of this ancestor. */
10410 if (ancestor
->f2k_derived
)
10413 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
10418 /* Find next ancestor type and recurse on it. */
10419 ancestor
= gfc_get_derived_super_type (ancestor
);
10421 return ensure_not_abstract (sub
, ancestor
);
10427 static void resolve_symbol (gfc_symbol
*sym
);
10430 /* Resolve the components of a derived type. */
10433 resolve_fl_derived (gfc_symbol
*sym
)
10435 gfc_symbol
* super_type
;
10439 super_type
= gfc_get_derived_super_type (sym
);
10442 if (super_type
&& sym
->attr
.coarray_comp
&& !super_type
->attr
.coarray_comp
)
10444 gfc_error ("As extending type '%s' at %L has a coarray component, "
10445 "parent type '%s' shall also have one", sym
->name
,
10446 &sym
->declared_at
, super_type
->name
);
10450 /* Ensure the extended type gets resolved before we do. */
10451 if (super_type
&& resolve_fl_derived (super_type
) == FAILURE
)
10454 /* An ABSTRACT type must be extensible. */
10455 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
10457 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10458 sym
->name
, &sym
->declared_at
);
10462 for (c
= sym
->components
; c
!= NULL
; c
= c
->next
)
10465 if (c
->attr
.codimension
10466 && (!c
->attr
.allocatable
|| c
->as
->type
!= AS_DEFERRED
))
10468 gfc_error ("Coarray component '%s' at %L must be allocatable with "
10469 "deferred shape", c
->name
, &c
->loc
);
10474 if (c
->attr
.codimension
&& c
->ts
.type
== BT_DERIVED
10475 && c
->ts
.u
.derived
->ts
.is_iso_c
)
10477 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
10478 "shall not be a coarray", c
->name
, &c
->loc
);
10483 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
10484 && (c
->attr
.codimension
|| c
->attr
.pointer
|| c
->attr
.dimension
10485 || c
->attr
.allocatable
))
10487 gfc_error ("Component '%s' at %L with coarray component "
10488 "shall be a nonpointer, nonallocatable scalar",
10493 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
10495 if (c
->ts
.interface
->attr
.procedure
)
10496 gfc_error ("Interface '%s', used by procedure pointer component "
10497 "'%s' at %L, is declared in a later PROCEDURE statement",
10498 c
->ts
.interface
->name
, c
->name
, &c
->loc
);
10500 /* Get the attributes from the interface (now resolved). */
10501 if (c
->ts
.interface
->attr
.if_source
10502 || c
->ts
.interface
->attr
.intrinsic
)
10504 gfc_symbol
*ifc
= c
->ts
.interface
;
10506 if (ifc
->formal
&& !ifc
->formal_ns
)
10507 resolve_symbol (ifc
);
10509 if (ifc
->attr
.intrinsic
)
10510 resolve_intrinsic (ifc
, &ifc
->declared_at
);
10514 c
->ts
= ifc
->result
->ts
;
10515 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
10516 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
10517 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
10518 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
10523 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
10524 c
->attr
.pointer
= ifc
->attr
.pointer
;
10525 c
->attr
.dimension
= ifc
->attr
.dimension
;
10526 c
->as
= gfc_copy_array_spec (ifc
->as
);
10528 c
->ts
.interface
= ifc
;
10529 c
->attr
.function
= ifc
->attr
.function
;
10530 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
10531 gfc_copy_formal_args_ppc (c
, ifc
);
10533 c
->attr
.pure
= ifc
->attr
.pure
;
10534 c
->attr
.elemental
= ifc
->attr
.elemental
;
10535 c
->attr
.recursive
= ifc
->attr
.recursive
;
10536 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
10537 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
10538 /* Replace symbols in array spec. */
10542 for (i
= 0; i
< c
->as
->rank
; i
++)
10544 gfc_expr_replace_comp (c
->as
->lower
[i
], c
);
10545 gfc_expr_replace_comp (c
->as
->upper
[i
], c
);
10548 /* Copy char length. */
10549 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
10551 c
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
10552 gfc_expr_replace_comp (c
->ts
.u
.cl
->length
, c
);
10555 else if (c
->ts
.interface
->name
[0] != '\0')
10557 gfc_error ("Interface '%s' of procedure pointer component "
10558 "'%s' at %L must be explicit", c
->ts
.interface
->name
,
10563 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
10565 /* Since PPCs are not implicitly typed, a PPC without an explicit
10566 interface must be a subroutine. */
10567 gfc_add_subroutine (&c
->attr
, c
->name
, &c
->loc
);
10570 /* Procedure pointer components: Check PASS arg. */
10571 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0)
10573 gfc_symbol
* me_arg
;
10575 if (c
->tb
->pass_arg
)
10577 gfc_formal_arglist
* i
;
10579 /* If an explicit passing argument name is given, walk the arg-list
10580 and look for it. */
10583 c
->tb
->pass_arg_num
= 1;
10584 for (i
= c
->formal
; i
; i
= i
->next
)
10586 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
10591 c
->tb
->pass_arg_num
++;
10596 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10597 "at %L has no argument '%s'", c
->name
,
10598 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
10605 /* Otherwise, take the first one; there should in fact be at least
10607 c
->tb
->pass_arg_num
= 1;
10610 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10611 "must have at least one argument",
10616 me_arg
= c
->formal
->sym
;
10619 /* Now check that the argument-type matches. */
10620 gcc_assert (me_arg
);
10621 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
10622 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
10623 || (me_arg
->ts
.type
== BT_CLASS
10624 && me_arg
->ts
.u
.derived
->components
->ts
.u
.derived
!= sym
))
10626 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10627 " the derived type '%s'", me_arg
->name
, c
->name
,
10628 me_arg
->name
, &c
->loc
, sym
->name
);
10633 /* Check for C453. */
10634 if (me_arg
->attr
.dimension
)
10636 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10637 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
10643 if (me_arg
->attr
.pointer
)
10645 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10646 "may not have the POINTER attribute", me_arg
->name
,
10647 c
->name
, me_arg
->name
, &c
->loc
);
10652 if (me_arg
->attr
.allocatable
)
10654 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10655 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
10656 me_arg
->name
, &c
->loc
);
10661 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
10662 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10663 " at %L", c
->name
, &c
->loc
);
10667 /* Check type-spec if this is not the parent-type component. */
10668 if ((!sym
->attr
.extension
|| c
!= sym
->components
)
10669 && resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
) == FAILURE
)
10672 /* If this type is an extension, set the accessibility of the parent
10674 if (super_type
&& c
== sym
->components
10675 && strcmp (super_type
->name
, c
->name
) == 0)
10676 c
->attr
.access
= super_type
->attr
.access
;
10678 /* If this type is an extension, see if this component has the same name
10679 as an inherited type-bound procedure. */
10681 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
10683 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10684 " inherited type-bound procedure",
10685 c
->name
, sym
->name
, &c
->loc
);
10689 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
)
10691 if (c
->ts
.u
.cl
->length
== NULL
10692 || (resolve_charlen (c
->ts
.u
.cl
) == FAILURE
)
10693 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
10695 gfc_error ("Character length of component '%s' needs to "
10696 "be a constant specification expression at %L",
10698 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
10703 if (c
->ts
.type
== BT_DERIVED
10704 && sym
->component_access
!= ACCESS_PRIVATE
10705 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
10706 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
10707 && !c
->ts
.u
.derived
->attr
.use_assoc
10708 && !gfc_check_access (c
->ts
.u
.derived
->attr
.access
,
10709 c
->ts
.u
.derived
->ns
->default_access
)
10710 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: the component '%s' "
10711 "is a PRIVATE type and cannot be a component of "
10712 "'%s', which is PUBLIC at %L", c
->name
,
10713 sym
->name
, &sym
->declared_at
) == FAILURE
)
10716 if (sym
->attr
.sequence
)
10718 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
10720 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10721 "not have the SEQUENCE attribute",
10722 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
10727 if (c
->ts
.type
== BT_DERIVED
&& c
->attr
.pointer
10728 && c
->ts
.u
.derived
->components
== NULL
10729 && !c
->ts
.u
.derived
->attr
.zero_comp
)
10731 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10732 "that has not been declared", c
->name
, sym
->name
,
10738 if (c
->ts
.type
== BT_CLASS
10739 && !(c
->ts
.u
.derived
->components
->attr
.pointer
10740 || c
->ts
.u
.derived
->components
->attr
.allocatable
))
10742 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10743 "or pointer", c
->name
, &c
->loc
);
10747 /* Ensure that all the derived type components are put on the
10748 derived type list; even in formal namespaces, where derived type
10749 pointer components might not have been declared. */
10750 if (c
->ts
.type
== BT_DERIVED
10752 && c
->ts
.u
.derived
->components
10754 && sym
!= c
->ts
.u
.derived
)
10755 add_dt_to_dt_list (c
->ts
.u
.derived
);
10757 if (c
->attr
.pointer
|| c
->attr
.proc_pointer
|| c
->attr
.allocatable
10761 for (i
= 0; i
< c
->as
->rank
; i
++)
10763 if (c
->as
->lower
[i
] == NULL
10764 || (resolve_index_expr (c
->as
->lower
[i
]) == FAILURE
)
10765 || !gfc_is_constant_expr (c
->as
->lower
[i
])
10766 || c
->as
->upper
[i
] == NULL
10767 || (resolve_index_expr (c
->as
->upper
[i
]) == FAILURE
)
10768 || !gfc_is_constant_expr (c
->as
->upper
[i
]))
10770 gfc_error ("Component '%s' of '%s' at %L must have "
10771 "constant array bounds",
10772 c
->name
, sym
->name
, &c
->loc
);
10778 /* Resolve the type-bound procedures. */
10779 if (resolve_typebound_procedures (sym
) == FAILURE
)
10782 /* Resolve the finalizer procedures. */
10783 if (gfc_resolve_finalizers (sym
) == FAILURE
)
10786 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10787 all DEFERRED bindings are overridden. */
10788 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
10789 && ensure_not_abstract (sym
, super_type
) == FAILURE
)
10792 /* Add derived type to the derived type list. */
10793 add_dt_to_dt_list (sym
);
10800 resolve_fl_namelist (gfc_symbol
*sym
)
10805 /* Reject PRIVATE objects in a PUBLIC namelist. */
10806 if (gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
10808 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10810 if (!nl
->sym
->attr
.use_assoc
10811 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
10812 && !gfc_check_access(nl
->sym
->attr
.access
,
10813 nl
->sym
->ns
->default_access
))
10815 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10816 "cannot be member of PUBLIC namelist '%s' at %L",
10817 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10821 /* Types with private components that came here by USE-association. */
10822 if (nl
->sym
->ts
.type
== BT_DERIVED
10823 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
10825 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10826 "components and cannot be member of namelist '%s' at %L",
10827 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10831 /* Types with private components that are defined in the same module. */
10832 if (nl
->sym
->ts
.type
== BT_DERIVED
10833 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
10834 && !gfc_check_access (nl
->sym
->ts
.u
.derived
->attr
.private_comp
10835 ? ACCESS_PRIVATE
: ACCESS_UNKNOWN
,
10836 nl
->sym
->ns
->default_access
))
10838 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10839 "cannot be a member of PUBLIC namelist '%s' at %L",
10840 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10846 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10848 /* Reject namelist arrays of assumed shape. */
10849 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
10850 && gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object '%s' "
10851 "must not have assumed shape in namelist "
10852 "'%s' at %L", nl
->sym
->name
, sym
->name
,
10853 &sym
->declared_at
) == FAILURE
)
10856 /* Reject namelist arrays that are not constant shape. */
10857 if (is_non_constant_shape_array (nl
->sym
))
10859 gfc_error ("NAMELIST array object '%s' must have constant "
10860 "shape in namelist '%s' at %L", nl
->sym
->name
,
10861 sym
->name
, &sym
->declared_at
);
10865 /* Namelist objects cannot have allocatable or pointer components. */
10866 if (nl
->sym
->ts
.type
!= BT_DERIVED
)
10869 if (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
)
10871 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10872 "have ALLOCATABLE components",
10873 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10877 if (nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
)
10879 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10880 "have POINTER components",
10881 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10887 /* 14.1.2 A module or internal procedure represent local entities
10888 of the same type as a namelist member and so are not allowed. */
10889 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10891 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
10894 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
10895 if ((nl
->sym
== sym
->ns
->proc_name
)
10897 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
10901 if (nl
->sym
&& nl
->sym
->name
)
10902 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
10903 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
10905 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10906 "attribute in '%s' at %L", nlsym
->name
,
10907 &sym
->declared_at
);
10917 resolve_fl_parameter (gfc_symbol
*sym
)
10919 /* A parameter array's shape needs to be constant. */
10920 if (sym
->as
!= NULL
10921 && (sym
->as
->type
== AS_DEFERRED
10922 || is_non_constant_shape_array (sym
)))
10924 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10925 "or of deferred shape", sym
->name
, &sym
->declared_at
);
10929 /* Make sure a parameter that has been implicitly typed still
10930 matches the implicit type, since PARAMETER statements can precede
10931 IMPLICIT statements. */
10932 if (sym
->attr
.implicit_type
10933 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
10936 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10937 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
10941 /* Make sure the types of derived parameters are consistent. This
10942 type checking is deferred until resolution because the type may
10943 refer to a derived type from the host. */
10944 if (sym
->ts
.type
== BT_DERIVED
10945 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
10947 gfc_error ("Incompatible derived type in PARAMETER at %L",
10948 &sym
->value
->where
);
10955 /* Do anything necessary to resolve a symbol. Right now, we just
10956 assume that an otherwise unknown symbol is a variable. This sort
10957 of thing commonly happens for symbols in module. */
10960 resolve_symbol (gfc_symbol
*sym
)
10962 int check_constant
, mp_flag
;
10963 gfc_symtree
*symtree
;
10964 gfc_symtree
*this_symtree
;
10968 if (sym
->attr
.flavor
== FL_UNKNOWN
)
10971 /* If we find that a flavorless symbol is an interface in one of the
10972 parent namespaces, find its symtree in this namespace, free the
10973 symbol and set the symtree to point to the interface symbol. */
10974 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
10976 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
10977 if (symtree
&& symtree
->n
.sym
->generic
)
10979 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
10983 gfc_free_symbol (sym
);
10984 symtree
->n
.sym
->refs
++;
10985 this_symtree
->n
.sym
= symtree
->n
.sym
;
10990 /* Otherwise give it a flavor according to such attributes as
10992 if (sym
->attr
.external
== 0 && sym
->attr
.intrinsic
== 0)
10993 sym
->attr
.flavor
= FL_VARIABLE
;
10996 sym
->attr
.flavor
= FL_PROCEDURE
;
10997 if (sym
->attr
.dimension
)
10998 sym
->attr
.function
= 1;
11002 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
11003 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
11005 if (sym
->attr
.procedure
&& sym
->ts
.interface
11006 && sym
->attr
.if_source
!= IFSRC_DECL
)
11008 if (sym
->ts
.interface
== sym
)
11010 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
11011 "interface", sym
->name
, &sym
->declared_at
);
11014 if (sym
->ts
.interface
->attr
.procedure
)
11016 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
11017 " in a later PROCEDURE statement", sym
->ts
.interface
->name
,
11018 sym
->name
,&sym
->declared_at
);
11022 /* Get the attributes from the interface (now resolved). */
11023 if (sym
->ts
.interface
->attr
.if_source
11024 || sym
->ts
.interface
->attr
.intrinsic
)
11026 gfc_symbol
*ifc
= sym
->ts
.interface
;
11027 resolve_symbol (ifc
);
11029 if (ifc
->attr
.intrinsic
)
11030 resolve_intrinsic (ifc
, &ifc
->declared_at
);
11033 sym
->ts
= ifc
->result
->ts
;
11036 sym
->ts
.interface
= ifc
;
11037 sym
->attr
.function
= ifc
->attr
.function
;
11038 sym
->attr
.subroutine
= ifc
->attr
.subroutine
;
11039 gfc_copy_formal_args (sym
, ifc
);
11041 sym
->attr
.allocatable
= ifc
->attr
.allocatable
;
11042 sym
->attr
.pointer
= ifc
->attr
.pointer
;
11043 sym
->attr
.pure
= ifc
->attr
.pure
;
11044 sym
->attr
.elemental
= ifc
->attr
.elemental
;
11045 sym
->attr
.dimension
= ifc
->attr
.dimension
;
11046 sym
->attr
.recursive
= ifc
->attr
.recursive
;
11047 sym
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
11048 sym
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
11049 /* Copy array spec. */
11050 sym
->as
= gfc_copy_array_spec (ifc
->as
);
11054 for (i
= 0; i
< sym
->as
->rank
; i
++)
11056 gfc_expr_replace_symbols (sym
->as
->lower
[i
], sym
);
11057 gfc_expr_replace_symbols (sym
->as
->upper
[i
], sym
);
11060 /* Copy char length. */
11061 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
11063 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
11064 gfc_expr_replace_symbols (sym
->ts
.u
.cl
->length
, sym
);
11067 else if (sym
->ts
.interface
->name
[0] != '\0')
11069 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11070 sym
->ts
.interface
->name
, sym
->name
, &sym
->declared_at
);
11075 if (sym
->attr
.flavor
== FL_DERIVED
&& resolve_fl_derived (sym
) == FAILURE
)
11078 /* Symbols that are module procedures with results (functions) have
11079 the types and array specification copied for type checking in
11080 procedures that call them, as well as for saving to a module
11081 file. These symbols can't stand the scrutiny that their results
11083 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
11086 /* Make sure that the intrinsic is consistent with its internal
11087 representation. This needs to be done before assigning a default
11088 type to avoid spurious warnings. */
11089 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
11090 && resolve_intrinsic (sym
, &sym
->declared_at
) == FAILURE
)
11093 /* Assign default type to symbols that need one and don't have one. */
11094 if (sym
->ts
.type
== BT_UNKNOWN
)
11096 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
11097 gfc_set_default_type (sym
, 1, NULL
);
11099 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
11100 && !sym
->attr
.function
&& !sym
->attr
.subroutine
11101 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
11102 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
11104 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
11106 /* The specific case of an external procedure should emit an error
11107 in the case that there is no implicit type. */
11109 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
11112 /* Result may be in another namespace. */
11113 resolve_symbol (sym
->result
);
11115 if (!sym
->result
->attr
.proc_pointer
)
11117 sym
->ts
= sym
->result
->ts
;
11118 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
11119 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
11120 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
11121 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
11127 /* Assumed size arrays and assumed shape arrays must be dummy
11130 if (sym
->as
!= NULL
11131 && ((sym
->as
->type
== AS_ASSUMED_SIZE
&& !sym
->as
->cp_was_assumed
)
11132 || sym
->as
->type
== AS_ASSUMED_SHAPE
)
11133 && sym
->attr
.dummy
== 0)
11135 if (sym
->as
->type
== AS_ASSUMED_SIZE
)
11136 gfc_error ("Assumed size array at %L must be a dummy argument",
11137 &sym
->declared_at
);
11139 gfc_error ("Assumed shape array at %L must be a dummy argument",
11140 &sym
->declared_at
);
11144 /* Make sure symbols with known intent or optional are really dummy
11145 variable. Because of ENTRY statement, this has to be deferred
11146 until resolution time. */
11148 if (!sym
->attr
.dummy
11149 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
11151 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
11155 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
11157 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11158 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
11162 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
11164 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
11165 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
11167 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11168 "attribute must have constant length",
11169 sym
->name
, &sym
->declared_at
);
11173 if (sym
->ts
.is_c_interop
11174 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
11176 gfc_error ("C interoperable character dummy variable '%s' at %L "
11177 "with VALUE attribute must have length one",
11178 sym
->name
, &sym
->declared_at
);
11183 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11184 do this for something that was implicitly typed because that is handled
11185 in gfc_set_default_type. Handle dummy arguments and procedure
11186 definitions separately. Also, anything that is use associated is not
11187 handled here but instead is handled in the module it is declared in.
11188 Finally, derived type definitions are allowed to be BIND(C) since that
11189 only implies that they're interoperable, and they are checked fully for
11190 interoperability when a variable is declared of that type. */
11191 if (sym
->attr
.is_bind_c
&& sym
->attr
.implicit_type
== 0 &&
11192 sym
->attr
.use_assoc
== 0 && sym
->attr
.dummy
== 0 &&
11193 sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.flavor
!= FL_DERIVED
)
11195 gfc_try t
= SUCCESS
;
11197 /* First, make sure the variable is declared at the
11198 module-level scope (J3/04-007, Section 15.3). */
11199 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
11200 sym
->attr
.in_common
== 0)
11202 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11203 "is neither a COMMON block nor declared at the "
11204 "module level scope", sym
->name
, &(sym
->declared_at
));
11207 else if (sym
->common_head
!= NULL
)
11209 t
= verify_com_block_vars_c_interop (sym
->common_head
);
11213 /* If type() declaration, we need to verify that the components
11214 of the given type are all C interoperable, etc. */
11215 if (sym
->ts
.type
== BT_DERIVED
&&
11216 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
11218 /* Make sure the user marked the derived type as BIND(C). If
11219 not, call the verify routine. This could print an error
11220 for the derived type more than once if multiple variables
11221 of that type are declared. */
11222 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
11223 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
11227 /* Verify the variable itself as C interoperable if it
11228 is BIND(C). It is not possible for this to succeed if
11229 the verify_bind_c_derived_type failed, so don't have to handle
11230 any error returned by verify_bind_c_derived_type. */
11231 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
11232 sym
->common_block
);
11237 /* clear the is_bind_c flag to prevent reporting errors more than
11238 once if something failed. */
11239 sym
->attr
.is_bind_c
= 0;
11244 /* If a derived type symbol has reached this point, without its
11245 type being declared, we have an error. Notice that most
11246 conditions that produce undefined derived types have already
11247 been dealt with. However, the likes of:
11248 implicit type(t) (t) ..... call foo (t) will get us here if
11249 the type is not declared in the scope of the implicit
11250 statement. Change the type to BT_UNKNOWN, both because it is so
11251 and to prevent an ICE. */
11252 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->components
== NULL
11253 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
11255 gfc_error ("The derived type '%s' at %L is of type '%s', "
11256 "which has not been defined", sym
->name
,
11257 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
11258 sym
->ts
.type
= BT_UNKNOWN
;
11262 /* Make sure that the derived type has been resolved and that the
11263 derived type is visible in the symbol's namespace, if it is a
11264 module function and is not PRIVATE. */
11265 if (sym
->ts
.type
== BT_DERIVED
11266 && sym
->ts
.u
.derived
->attr
.use_assoc
11267 && sym
->ns
->proc_name
11268 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
11272 if (resolve_fl_derived (sym
->ts
.u
.derived
) == FAILURE
)
11275 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 1, &ds
);
11276 if (!ds
&& sym
->attr
.function
11277 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
11279 symtree
= gfc_new_symtree (&sym
->ns
->sym_root
,
11280 sym
->ts
.u
.derived
->name
);
11281 symtree
->n
.sym
= sym
->ts
.u
.derived
;
11282 sym
->ts
.u
.derived
->refs
++;
11286 /* Unless the derived-type declaration is use associated, Fortran 95
11287 does not allow public entries of private derived types.
11288 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11289 161 in 95-006r3. */
11290 if (sym
->ts
.type
== BT_DERIVED
11291 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
11292 && !sym
->ts
.u
.derived
->attr
.use_assoc
11293 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
11294 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
11295 sym
->ts
.u
.derived
->ns
->default_access
)
11296 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC %s '%s' at %L "
11297 "of PRIVATE derived type '%s'",
11298 (sym
->attr
.flavor
== FL_PARAMETER
) ? "parameter"
11299 : "variable", sym
->name
, &sym
->declared_at
,
11300 sym
->ts
.u
.derived
->name
) == FAILURE
)
11303 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11304 default initialization is defined (5.1.2.4.4). */
11305 if (sym
->ts
.type
== BT_DERIVED
11307 && sym
->attr
.intent
== INTENT_OUT
11309 && sym
->as
->type
== AS_ASSUMED_SIZE
)
11311 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
11313 if (c
->initializer
)
11315 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11316 "ASSUMED SIZE and so cannot have a default initializer",
11317 sym
->name
, &sym
->declared_at
);
11324 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
11325 || sym
->attr
.codimension
)
11326 && sym
->attr
.result
)
11327 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
11328 "a coarray component", sym
->name
, &sym
->declared_at
);
11331 if (sym
->attr
.codimension
&& sym
->ts
.type
== BT_DERIVED
11332 && sym
->ts
.u
.derived
->ts
.is_iso_c
)
11333 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11334 "shall not be a coarray", sym
->name
, &sym
->declared_at
);
11337 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
11338 && (sym
->attr
.codimension
|| sym
->attr
.pointer
|| sym
->attr
.dimension
11339 || sym
->attr
.allocatable
))
11340 gfc_error ("Variable '%s' at %L with coarray component "
11341 "shall be a nonpointer, nonallocatable scalar",
11342 sym
->name
, &sym
->declared_at
);
11344 /* F2008, C526. The function-result case was handled above. */
11345 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
11346 || sym
->attr
.codimension
)
11347 && !(sym
->attr
.allocatable
|| sym
->attr
.dummy
|| sym
->attr
.save
11348 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
11349 || sym
->ns
->proc_name
->attr
.is_main_program
11350 || sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.use_assoc
))
11351 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
11352 "component and is not ALLOCATABLE, SAVE nor a "
11353 "dummy argument", sym
->name
, &sym
->declared_at
);
11355 else if (sym
->attr
.codimension
&& !sym
->attr
.allocatable
11356 && sym
->as
->cotype
== AS_DEFERRED
)
11357 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
11358 "deferred shape", sym
->name
, &sym
->declared_at
);
11359 else if (sym
->attr
.codimension
&& sym
->attr
.allocatable
11360 && (sym
->as
->type
!= AS_DEFERRED
|| sym
->as
->cotype
!= AS_DEFERRED
))
11361 gfc_error ("Allocatable coarray variable '%s' at %L must have "
11362 "deferred shape", sym
->name
, &sym
->declared_at
);
11366 if (((sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.coarray_comp
)
11367 || (sym
->attr
.codimension
&& sym
->attr
.allocatable
))
11368 && sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
11369 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
11370 "allocatable coarray or have coarray components",
11371 sym
->name
, &sym
->declared_at
);
11373 if (sym
->attr
.codimension
&& sym
->attr
.dummy
11374 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
11375 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
11376 "procedure '%s'", sym
->name
, &sym
->declared_at
,
11377 sym
->ns
->proc_name
->name
);
11379 switch (sym
->attr
.flavor
)
11382 if (resolve_fl_variable (sym
, mp_flag
) == FAILURE
)
11387 if (resolve_fl_procedure (sym
, mp_flag
) == FAILURE
)
11392 if (resolve_fl_namelist (sym
) == FAILURE
)
11397 if (resolve_fl_parameter (sym
) == FAILURE
)
11405 /* Resolve array specifier. Check as well some constraints
11406 on COMMON blocks. */
11408 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
11410 /* Set the formal_arg_flag so that check_conflict will not throw
11411 an error for host associated variables in the specification
11412 expression for an array_valued function. */
11413 if (sym
->attr
.function
&& sym
->as
)
11414 formal_arg_flag
= 1;
11416 gfc_resolve_array_spec (sym
->as
, check_constant
);
11418 formal_arg_flag
= 0;
11420 /* Resolve formal namespaces. */
11421 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
11422 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
11423 gfc_resolve (sym
->formal_ns
);
11425 /* Make sure the formal namespace is present. */
11426 if (sym
->formal
&& !sym
->formal_ns
)
11428 gfc_formal_arglist
*formal
= sym
->formal
;
11429 while (formal
&& !formal
->sym
)
11430 formal
= formal
->next
;
11434 sym
->formal_ns
= formal
->sym
->ns
;
11435 sym
->formal_ns
->refs
++;
11439 /* Check threadprivate restrictions. */
11440 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
&& !sym
->ns
->save_all
11441 && (!sym
->attr
.in_common
11442 && sym
->module
== NULL
11443 && (sym
->ns
->proc_name
== NULL
11444 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
11445 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
11447 /* If we have come this far we can apply default-initializers, as
11448 described in 14.7.5, to those variables that have not already
11449 been assigned one. */
11450 if (sym
->ts
.type
== BT_DERIVED
11451 && sym
->attr
.referenced
11452 && sym
->ns
== gfc_current_ns
11454 && !sym
->attr
.allocatable
11455 && !sym
->attr
.alloc_comp
)
11457 symbol_attribute
*a
= &sym
->attr
;
11459 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
11460 && !a
->in_common
&& !a
->use_assoc
11461 && !(a
->function
&& sym
!= sym
->result
))
11462 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
11463 apply_default_init (sym
);
11466 /* If this symbol has a type-spec, check it. */
11467 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
11468 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
11469 if (resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
)
11475 /************* Resolve DATA statements *************/
11479 gfc_data_value
*vnode
;
11485 /* Advance the values structure to point to the next value in the data list. */
11488 next_data_value (void)
11490 while (mpz_cmp_ui (values
.left
, 0) == 0)
11493 if (values
.vnode
->next
== NULL
)
11496 values
.vnode
= values
.vnode
->next
;
11497 mpz_set (values
.left
, values
.vnode
->repeat
);
11505 check_data_variable (gfc_data_variable
*var
, locus
*where
)
11511 ar_type mark
= AR_UNKNOWN
;
11513 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
11519 if (gfc_resolve_expr (var
->expr
) == FAILURE
)
11523 mpz_init_set_si (offset
, 0);
11526 if (e
->expr_type
!= EXPR_VARIABLE
)
11527 gfc_internal_error ("check_data_variable(): Bad expression");
11529 sym
= e
->symtree
->n
.sym
;
11531 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
11533 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11534 sym
->name
, &sym
->declared_at
);
11537 if (e
->ref
== NULL
&& sym
->as
)
11539 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11540 " declaration", sym
->name
, where
);
11544 has_pointer
= sym
->attr
.pointer
;
11546 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
11548 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
11552 && ref
->type
== REF_ARRAY
11553 && ref
->u
.ar
.type
!= AR_FULL
)
11555 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11556 "be a full array", sym
->name
, where
);
11561 if (e
->rank
== 0 || has_pointer
)
11563 mpz_init_set_ui (size
, 1);
11570 /* Find the array section reference. */
11571 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
11573 if (ref
->type
!= REF_ARRAY
)
11575 if (ref
->u
.ar
.type
== AR_ELEMENT
)
11581 /* Set marks according to the reference pattern. */
11582 switch (ref
->u
.ar
.type
)
11590 /* Get the start position of array section. */
11591 gfc_get_section_index (ar
, section_index
, &offset
);
11596 gcc_unreachable ();
11599 if (gfc_array_size (e
, &size
) == FAILURE
)
11601 gfc_error ("Nonconstant array section at %L in DATA statement",
11603 mpz_clear (offset
);
11610 while (mpz_cmp_ui (size
, 0) > 0)
11612 if (next_data_value () == FAILURE
)
11614 gfc_error ("DATA statement at %L has more variables than values",
11620 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
11624 /* If we have more than one element left in the repeat count,
11625 and we have more than one element left in the target variable,
11626 then create a range assignment. */
11627 /* FIXME: Only done for full arrays for now, since array sections
11629 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
11630 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
11634 if (mpz_cmp (size
, values
.left
) >= 0)
11636 mpz_init_set (range
, values
.left
);
11637 mpz_sub (size
, size
, values
.left
);
11638 mpz_set_ui (values
.left
, 0);
11642 mpz_init_set (range
, size
);
11643 mpz_sub (values
.left
, values
.left
, size
);
11644 mpz_set_ui (size
, 0);
11647 gfc_assign_data_value_range (var
->expr
, values
.vnode
->expr
,
11650 mpz_add (offset
, offset
, range
);
11654 /* Assign initial value to symbol. */
11657 mpz_sub_ui (values
.left
, values
.left
, 1);
11658 mpz_sub_ui (size
, size
, 1);
11660 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
, offset
);
11664 if (mark
== AR_FULL
)
11665 mpz_add_ui (offset
, offset
, 1);
11667 /* Modify the array section indexes and recalculate the offset
11668 for next element. */
11669 else if (mark
== AR_SECTION
)
11670 gfc_advance_section (section_index
, ar
, &offset
);
11674 if (mark
== AR_SECTION
)
11676 for (i
= 0; i
< ar
->dimen
; i
++)
11677 mpz_clear (section_index
[i
]);
11681 mpz_clear (offset
);
11687 static gfc_try
traverse_data_var (gfc_data_variable
*, locus
*);
11689 /* Iterate over a list of elements in a DATA statement. */
11692 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
11695 iterator_stack frame
;
11696 gfc_expr
*e
, *start
, *end
, *step
;
11697 gfc_try retval
= SUCCESS
;
11699 mpz_init (frame
.value
);
11701 start
= gfc_copy_expr (var
->iter
.start
);
11702 end
= gfc_copy_expr (var
->iter
.end
);
11703 step
= gfc_copy_expr (var
->iter
.step
);
11705 if (gfc_simplify_expr (start
, 1) == FAILURE
11706 || start
->expr_type
!= EXPR_CONSTANT
)
11708 gfc_error ("iterator start at %L does not simplify", &start
->where
);
11712 if (gfc_simplify_expr (end
, 1) == FAILURE
11713 || end
->expr_type
!= EXPR_CONSTANT
)
11715 gfc_error ("iterator end at %L does not simplify", &end
->where
);
11719 if (gfc_simplify_expr (step
, 1) == FAILURE
11720 || step
->expr_type
!= EXPR_CONSTANT
)
11722 gfc_error ("iterator step at %L does not simplify", &step
->where
);
11727 mpz_init_set (trip
, end
->value
.integer
);
11728 mpz_sub (trip
, trip
, start
->value
.integer
);
11729 mpz_add (trip
, trip
, step
->value
.integer
);
11731 mpz_div (trip
, trip
, step
->value
.integer
);
11733 mpz_set (frame
.value
, start
->value
.integer
);
11735 frame
.prev
= iter_stack
;
11736 frame
.variable
= var
->iter
.var
->symtree
;
11737 iter_stack
= &frame
;
11739 while (mpz_cmp_ui (trip
, 0) > 0)
11741 if (traverse_data_var (var
->list
, where
) == FAILURE
)
11748 e
= gfc_copy_expr (var
->expr
);
11749 if (gfc_simplify_expr (e
, 1) == FAILURE
)
11757 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
11759 mpz_sub_ui (trip
, trip
, 1);
11764 mpz_clear (frame
.value
);
11766 gfc_free_expr (start
);
11767 gfc_free_expr (end
);
11768 gfc_free_expr (step
);
11770 iter_stack
= frame
.prev
;
11775 /* Type resolve variables in the variable list of a DATA statement. */
11778 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
11782 for (; var
; var
= var
->next
)
11784 if (var
->expr
== NULL
)
11785 t
= traverse_data_list (var
, where
);
11787 t
= check_data_variable (var
, where
);
11797 /* Resolve the expressions and iterators associated with a data statement.
11798 This is separate from the assignment checking because data lists should
11799 only be resolved once. */
11802 resolve_data_variables (gfc_data_variable
*d
)
11804 for (; d
; d
= d
->next
)
11806 if (d
->list
== NULL
)
11808 if (gfc_resolve_expr (d
->expr
) == FAILURE
)
11813 if (gfc_resolve_iterator (&d
->iter
, false) == FAILURE
)
11816 if (resolve_data_variables (d
->list
) == FAILURE
)
11825 /* Resolve a single DATA statement. We implement this by storing a pointer to
11826 the value list into static variables, and then recursively traversing the
11827 variables list, expanding iterators and such. */
11830 resolve_data (gfc_data
*d
)
11833 if (resolve_data_variables (d
->var
) == FAILURE
)
11836 values
.vnode
= d
->value
;
11837 if (d
->value
== NULL
)
11838 mpz_set_ui (values
.left
, 0);
11840 mpz_set (values
.left
, d
->value
->repeat
);
11842 if (traverse_data_var (d
->var
, &d
->where
) == FAILURE
)
11845 /* At this point, we better not have any values left. */
11847 if (next_data_value () == SUCCESS
)
11848 gfc_error ("DATA statement at %L has more values than variables",
11853 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11854 accessed by host or use association, is a dummy argument to a pure function,
11855 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11856 is storage associated with any such variable, shall not be used in the
11857 following contexts: (clients of this function). */
11859 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11860 procedure. Returns zero if assignment is OK, nonzero if there is a
11863 gfc_impure_variable (gfc_symbol
*sym
)
11868 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
11871 /* Check if the symbol's ns is inside the pure procedure. */
11872 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
11876 if (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
11880 proc
= sym
->ns
->proc_name
;
11881 if (sym
->attr
.dummy
&& gfc_pure (proc
)
11882 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
11884 proc
->attr
.function
))
11887 /* TODO: Sort out what can be storage associated, if anything, and include
11888 it here. In principle equivalences should be scanned but it does not
11889 seem to be possible to storage associate an impure variable this way. */
11894 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
11895 current namespace is inside a pure procedure. */
11898 gfc_pure (gfc_symbol
*sym
)
11900 symbol_attribute attr
;
11905 /* Check if the current namespace or one of its parents
11906 belongs to a pure procedure. */
11907 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
11909 sym
= ns
->proc_name
;
11913 if (attr
.flavor
== FL_PROCEDURE
&& (attr
.pure
|| attr
.elemental
))
11921 return attr
.flavor
== FL_PROCEDURE
&& (attr
.pure
|| attr
.elemental
);
11925 /* Test whether the current procedure is elemental or not. */
11928 gfc_elemental (gfc_symbol
*sym
)
11930 symbol_attribute attr
;
11933 sym
= gfc_current_ns
->proc_name
;
11938 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
11942 /* Warn about unused labels. */
11945 warn_unused_fortran_label (gfc_st_label
*label
)
11950 warn_unused_fortran_label (label
->left
);
11952 if (label
->defined
== ST_LABEL_UNKNOWN
)
11955 switch (label
->referenced
)
11957 case ST_LABEL_UNKNOWN
:
11958 gfc_warning ("Label %d at %L defined but not used", label
->value
,
11962 case ST_LABEL_BAD_TARGET
:
11963 gfc_warning ("Label %d at %L defined but cannot be used",
11964 label
->value
, &label
->where
);
11971 warn_unused_fortran_label (label
->right
);
11975 /* Returns the sequence type of a symbol or sequence. */
11978 sequence_type (gfc_typespec ts
)
11987 if (ts
.u
.derived
->components
== NULL
)
11988 return SEQ_NONDEFAULT
;
11990 result
= sequence_type (ts
.u
.derived
->components
->ts
);
11991 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
11992 if (sequence_type (c
->ts
) != result
)
11998 if (ts
.kind
!= gfc_default_character_kind
)
11999 return SEQ_NONDEFAULT
;
12001 return SEQ_CHARACTER
;
12004 if (ts
.kind
!= gfc_default_integer_kind
)
12005 return SEQ_NONDEFAULT
;
12007 return SEQ_NUMERIC
;
12010 if (!(ts
.kind
== gfc_default_real_kind
12011 || ts
.kind
== gfc_default_double_kind
))
12012 return SEQ_NONDEFAULT
;
12014 return SEQ_NUMERIC
;
12017 if (ts
.kind
!= gfc_default_complex_kind
)
12018 return SEQ_NONDEFAULT
;
12020 return SEQ_NUMERIC
;
12023 if (ts
.kind
!= gfc_default_logical_kind
)
12024 return SEQ_NONDEFAULT
;
12026 return SEQ_NUMERIC
;
12029 return SEQ_NONDEFAULT
;
12034 /* Resolve derived type EQUIVALENCE object. */
12037 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
12039 gfc_component
*c
= derived
->components
;
12044 /* Shall not be an object of nonsequence derived type. */
12045 if (!derived
->attr
.sequence
)
12047 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12048 "attribute to be an EQUIVALENCE object", sym
->name
,
12053 /* Shall not have allocatable components. */
12054 if (derived
->attr
.alloc_comp
)
12056 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12057 "components to be an EQUIVALENCE object",sym
->name
,
12062 if (sym
->attr
.in_common
&& has_default_initializer (sym
->ts
.u
.derived
))
12064 gfc_error ("Derived type variable '%s' at %L with default "
12065 "initialization cannot be in EQUIVALENCE with a variable "
12066 "in COMMON", sym
->name
, &e
->where
);
12070 for (; c
; c
= c
->next
)
12072 if (c
->ts
.type
== BT_DERIVED
12073 && (resolve_equivalence_derived (c
->ts
.u
.derived
, sym
, e
) == FAILURE
))
12076 /* Shall not be an object of sequence derived type containing a pointer
12077 in the structure. */
12078 if (c
->attr
.pointer
)
12080 gfc_error ("Derived type variable '%s' at %L with pointer "
12081 "component(s) cannot be an EQUIVALENCE object",
12082 sym
->name
, &e
->where
);
12090 /* Resolve equivalence object.
12091 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12092 an allocatable array, an object of nonsequence derived type, an object of
12093 sequence derived type containing a pointer at any level of component
12094 selection, an automatic object, a function name, an entry name, a result
12095 name, a named constant, a structure component, or a subobject of any of
12096 the preceding objects. A substring shall not have length zero. A
12097 derived type shall not have components with default initialization nor
12098 shall two objects of an equivalence group be initialized.
12099 Either all or none of the objects shall have an protected attribute.
12100 The simple constraints are done in symbol.c(check_conflict) and the rest
12101 are implemented here. */
12104 resolve_equivalence (gfc_equiv
*eq
)
12107 gfc_symbol
*first_sym
;
12110 locus
*last_where
= NULL
;
12111 seq_type eq_type
, last_eq_type
;
12112 gfc_typespec
*last_ts
;
12113 int object
, cnt_protected
;
12116 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
12118 first_sym
= eq
->expr
->symtree
->n
.sym
;
12122 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
12126 e
->ts
= e
->symtree
->n
.sym
->ts
;
12127 /* match_varspec might not know yet if it is seeing
12128 array reference or substring reference, as it doesn't
12130 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
12132 gfc_ref
*ref
= e
->ref
;
12133 sym
= e
->symtree
->n
.sym
;
12135 if (sym
->attr
.dimension
)
12137 ref
->u
.ar
.as
= sym
->as
;
12141 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12142 if (e
->ts
.type
== BT_CHARACTER
12144 && ref
->type
== REF_ARRAY
12145 && ref
->u
.ar
.dimen
== 1
12146 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
12147 && ref
->u
.ar
.stride
[0] == NULL
)
12149 gfc_expr
*start
= ref
->u
.ar
.start
[0];
12150 gfc_expr
*end
= ref
->u
.ar
.end
[0];
12153 /* Optimize away the (:) reference. */
12154 if (start
== NULL
&& end
== NULL
)
12157 e
->ref
= ref
->next
;
12159 e
->ref
->next
= ref
->next
;
12164 ref
->type
= REF_SUBSTRING
;
12166 start
= gfc_int_expr (1);
12167 ref
->u
.ss
.start
= start
;
12168 if (end
== NULL
&& e
->ts
.u
.cl
)
12169 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
12170 ref
->u
.ss
.end
= end
;
12171 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
12178 /* Any further ref is an error. */
12181 gcc_assert (ref
->type
== REF_ARRAY
);
12182 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12188 if (gfc_resolve_expr (e
) == FAILURE
)
12191 sym
= e
->symtree
->n
.sym
;
12193 if (sym
->attr
.is_protected
)
12195 if (cnt_protected
> 0 && cnt_protected
!= object
)
12197 gfc_error ("Either all or none of the objects in the "
12198 "EQUIVALENCE set at %L shall have the "
12199 "PROTECTED attribute",
12204 /* Shall not equivalence common block variables in a PURE procedure. */
12205 if (sym
->ns
->proc_name
12206 && sym
->ns
->proc_name
->attr
.pure
12207 && sym
->attr
.in_common
)
12209 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12210 "object in the pure procedure '%s'",
12211 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
12215 /* Shall not be a named constant. */
12216 if (e
->expr_type
== EXPR_CONSTANT
)
12218 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12219 "object", sym
->name
, &e
->where
);
12223 if (e
->ts
.type
== BT_DERIVED
12224 && resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
) == FAILURE
)
12227 /* Check that the types correspond correctly:
12229 A numeric sequence structure may be equivalenced to another sequence
12230 structure, an object of default integer type, default real type, double
12231 precision real type, default logical type such that components of the
12232 structure ultimately only become associated to objects of the same
12233 kind. A character sequence structure may be equivalenced to an object
12234 of default character kind or another character sequence structure.
12235 Other objects may be equivalenced only to objects of the same type and
12236 kind parameters. */
12238 /* Identical types are unconditionally OK. */
12239 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
12240 goto identical_types
;
12242 last_eq_type
= sequence_type (*last_ts
);
12243 eq_type
= sequence_type (sym
->ts
);
12245 /* Since the pair of objects is not of the same type, mixed or
12246 non-default sequences can be rejected. */
12248 msg
= "Sequence %s with mixed components in EQUIVALENCE "
12249 "statement at %L with different type objects";
12251 && last_eq_type
== SEQ_MIXED
12252 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
)
12254 || (eq_type
== SEQ_MIXED
12255 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
12256 &e
->where
) == FAILURE
))
12259 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
12260 "statement at %L with objects of different type";
12262 && last_eq_type
== SEQ_NONDEFAULT
12263 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
,
12264 last_where
) == FAILURE
)
12265 || (eq_type
== SEQ_NONDEFAULT
12266 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
12267 &e
->where
) == FAILURE
))
12270 msg
="Non-CHARACTER object '%s' in default CHARACTER "
12271 "EQUIVALENCE statement at %L";
12272 if (last_eq_type
== SEQ_CHARACTER
12273 && eq_type
!= SEQ_CHARACTER
12274 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
12275 &e
->where
) == FAILURE
)
12278 msg
="Non-NUMERIC object '%s' in default NUMERIC "
12279 "EQUIVALENCE statement at %L";
12280 if (last_eq_type
== SEQ_NUMERIC
12281 && eq_type
!= SEQ_NUMERIC
12282 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
12283 &e
->where
) == FAILURE
)
12288 last_where
= &e
->where
;
12293 /* Shall not be an automatic array. */
12294 if (e
->ref
->type
== REF_ARRAY
12295 && gfc_resolve_array_spec (e
->ref
->u
.ar
.as
, 1) == FAILURE
)
12297 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12298 "an EQUIVALENCE object", sym
->name
, &e
->where
);
12305 /* Shall not be a structure component. */
12306 if (r
->type
== REF_COMPONENT
)
12308 gfc_error ("Structure component '%s' at %L cannot be an "
12309 "EQUIVALENCE object",
12310 r
->u
.c
.component
->name
, &e
->where
);
12314 /* A substring shall not have length zero. */
12315 if (r
->type
== REF_SUBSTRING
)
12317 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
12319 gfc_error ("Substring at %L has length zero",
12320 &r
->u
.ss
.start
->where
);
12330 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12333 resolve_fntype (gfc_namespace
*ns
)
12335 gfc_entry_list
*el
;
12338 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
12341 /* If there are any entries, ns->proc_name is the entry master
12342 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12344 sym
= ns
->entries
->sym
;
12346 sym
= ns
->proc_name
;
12347 if (sym
->result
== sym
12348 && sym
->ts
.type
== BT_UNKNOWN
12349 && gfc_set_default_type (sym
, 0, NULL
) == FAILURE
12350 && !sym
->attr
.untyped
)
12352 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12353 sym
->name
, &sym
->declared_at
);
12354 sym
->attr
.untyped
= 1;
12357 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
12358 && !sym
->attr
.contained
12359 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
12360 sym
->ts
.u
.derived
->ns
->default_access
)
12361 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
12363 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC function '%s' at "
12364 "%L of PRIVATE type '%s'", sym
->name
,
12365 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
12369 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
12371 if (el
->sym
->result
== el
->sym
12372 && el
->sym
->ts
.type
== BT_UNKNOWN
12373 && gfc_set_default_type (el
->sym
, 0, NULL
) == FAILURE
12374 && !el
->sym
->attr
.untyped
)
12376 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12377 el
->sym
->name
, &el
->sym
->declared_at
);
12378 el
->sym
->attr
.untyped
= 1;
12384 /* 12.3.2.1.1 Defined operators. */
12387 check_uop_procedure (gfc_symbol
*sym
, locus where
)
12389 gfc_formal_arglist
*formal
;
12391 if (!sym
->attr
.function
)
12393 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12394 sym
->name
, &where
);
12398 if (sym
->ts
.type
== BT_CHARACTER
12399 && !(sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
)
12400 && !(sym
->result
&& sym
->result
->ts
.u
.cl
12401 && sym
->result
->ts
.u
.cl
->length
))
12403 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12404 "character length", sym
->name
, &where
);
12408 formal
= sym
->formal
;
12409 if (!formal
|| !formal
->sym
)
12411 gfc_error ("User operator procedure '%s' at %L must have at least "
12412 "one argument", sym
->name
, &where
);
12416 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
12418 gfc_error ("First argument of operator interface at %L must be "
12419 "INTENT(IN)", &where
);
12423 if (formal
->sym
->attr
.optional
)
12425 gfc_error ("First argument of operator interface at %L cannot be "
12426 "optional", &where
);
12430 formal
= formal
->next
;
12431 if (!formal
|| !formal
->sym
)
12434 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
12436 gfc_error ("Second argument of operator interface at %L must be "
12437 "INTENT(IN)", &where
);
12441 if (formal
->sym
->attr
.optional
)
12443 gfc_error ("Second argument of operator interface at %L cannot be "
12444 "optional", &where
);
12450 gfc_error ("Operator interface at %L must have, at most, two "
12451 "arguments", &where
);
12459 gfc_resolve_uops (gfc_symtree
*symtree
)
12461 gfc_interface
*itr
;
12463 if (symtree
== NULL
)
12466 gfc_resolve_uops (symtree
->left
);
12467 gfc_resolve_uops (symtree
->right
);
12469 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
12470 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
12474 /* Examine all of the expressions associated with a program unit,
12475 assign types to all intermediate expressions, make sure that all
12476 assignments are to compatible types and figure out which names
12477 refer to which functions or subroutines. It doesn't check code
12478 block, which is handled by resolve_code. */
12481 resolve_types (gfc_namespace
*ns
)
12487 gfc_namespace
* old_ns
= gfc_current_ns
;
12489 /* Check that all IMPLICIT types are ok. */
12490 if (!ns
->seen_implicit_none
)
12493 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
12494 if (ns
->set_flag
[letter
]
12495 && resolve_typespec_used (&ns
->default_type
[letter
],
12496 &ns
->implicit_loc
[letter
],
12501 gfc_current_ns
= ns
;
12503 resolve_entries (ns
);
12505 resolve_common_vars (ns
->blank_common
.head
, false);
12506 resolve_common_blocks (ns
->common_root
);
12508 resolve_contained_functions (ns
);
12510 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
12512 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
12513 resolve_charlen (cl
);
12515 gfc_traverse_ns (ns
, resolve_symbol
);
12517 resolve_fntype (ns
);
12519 for (n
= ns
->contained
; n
; n
= n
->sibling
)
12521 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
12522 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12523 "also be PURE", n
->proc_name
->name
,
12524 &n
->proc_name
->declared_at
);
12530 gfc_check_interfaces (ns
);
12532 gfc_traverse_ns (ns
, resolve_values
);
12538 for (d
= ns
->data
; d
; d
= d
->next
)
12542 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
12544 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
12546 if (ns
->common_root
!= NULL
)
12547 gfc_traverse_symtree (ns
->common_root
, resolve_bind_c_comms
);
12549 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
12550 resolve_equivalence (eq
);
12552 /* Warn about unused labels. */
12553 if (warn_unused_label
)
12554 warn_unused_fortran_label (ns
->st_labels
);
12556 gfc_resolve_uops (ns
->uop_root
);
12558 gfc_current_ns
= old_ns
;
12562 /* Call resolve_code recursively. */
12565 resolve_codes (gfc_namespace
*ns
)
12568 bitmap_obstack old_obstack
;
12570 for (n
= ns
->contained
; n
; n
= n
->sibling
)
12573 gfc_current_ns
= ns
;
12575 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12576 if (!(ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
))
12579 /* Set to an out of range value. */
12580 current_entry_id
= -1;
12582 old_obstack
= labels_obstack
;
12583 bitmap_obstack_initialize (&labels_obstack
);
12585 resolve_code (ns
->code
, ns
);
12587 bitmap_obstack_release (&labels_obstack
);
12588 labels_obstack
= old_obstack
;
12592 /* This function is called after a complete program unit has been compiled.
12593 Its purpose is to examine all of the expressions associated with a program
12594 unit, assign types to all intermediate expressions, make sure that all
12595 assignments are to compatible types and figure out which names refer to
12596 which functions or subroutines. */
12599 gfc_resolve (gfc_namespace
*ns
)
12601 gfc_namespace
*old_ns
;
12602 code_stack
*old_cs_base
;
12608 old_ns
= gfc_current_ns
;
12609 old_cs_base
= cs_base
;
12611 resolve_types (ns
);
12612 resolve_codes (ns
);
12614 gfc_current_ns
= old_ns
;
12615 cs_base
= old_cs_base
;