1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
))
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym
->name
, &sym
->declared_at
);
268 if (sym
->attr
.pointer
)
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym
->name
,
276 if (sym
->attr
.flavor
== FL_PROCEDURE
)
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym
->name
, proc
->name
,
285 /* Each dummy shall be specified to be scalar. */
286 if (proc
->attr
.proc
== PROC_ST_FUNCTION
)
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym
->name
, &sym
->declared_at
);
295 if (sym
->ts
.type
== BT_CHARACTER
)
297 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
298 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym
->name
, &sym
->declared_at
);
312 /* Work function called when searching for symbols that have argument lists
313 associated with them. */
316 find_arglists (gfc_symbol
*sym
)
318 if (sym
->attr
.if_source
== IFSRC_UNKNOWN
|| sym
->ns
!= gfc_current_ns
)
321 resolve_formal_arglist (sym
);
325 /* Given a namespace, resolve all formal argument lists within the namespace.
329 resolve_formal_arglists (gfc_namespace
*ns
)
334 gfc_traverse_ns (ns
, find_arglists
);
339 resolve_contained_fntype (gfc_symbol
*sym
, gfc_namespace
*ns
)
343 /* If this namespace is not a function or an entry master function,
345 if (! sym
|| !(sym
->attr
.function
|| sym
->attr
.flavor
== FL_VARIABLE
)
346 || sym
->attr
.entry_master
)
349 /* Try to find out of what the return type is. */
350 if (sym
->result
->ts
.type
== BT_UNKNOWN
&& sym
->result
->ts
.interface
== NULL
)
352 t
= gfc_set_default_type (sym
->result
, 0, ns
);
354 if (t
== FAILURE
&& !sym
->result
->attr
.untyped
)
356 if (sym
->result
== sym
)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym
->name
, &sym
->declared_at
);
359 else if (!sym
->result
->attr
.proc_pointer
)
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym
->result
->name
, sym
->name
,
362 &sym
->result
->declared_at
);
363 sym
->result
->attr
.untyped
= 1;
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
370 in external functions. Internal function results are not on that list;
371 ergo, not permitted. */
373 if (sym
->result
->ts
.type
== BT_CHARACTER
)
375 gfc_charlen
*cl
= sym
->result
->ts
.u
.cl
;
376 if (!cl
|| !cl
->length
)
377 gfc_error ("Character-valued internal function '%s' at %L must "
378 "not be assumed length", sym
->name
, &sym
->declared_at
);
383 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
384 introduce duplicates. */
387 merge_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
389 gfc_formal_arglist
*f
, *new_arglist
;
392 for (; new_args
!= NULL
; new_args
= new_args
->next
)
394 new_sym
= new_args
->sym
;
395 /* See if this arg is already in the formal argument list. */
396 for (f
= proc
->formal
; f
; f
= f
->next
)
398 if (new_sym
== f
->sym
)
405 /* Add a new argument. Argument order is not important. */
406 new_arglist
= gfc_get_formal_arglist ();
407 new_arglist
->sym
= new_sym
;
408 new_arglist
->next
= proc
->formal
;
409 proc
->formal
= new_arglist
;
414 /* Flag the arguments that are not present in all entries. */
417 check_argument_lists (gfc_symbol
*proc
, gfc_formal_arglist
*new_args
)
419 gfc_formal_arglist
*f
, *head
;
422 for (f
= proc
->formal
; f
; f
= f
->next
)
427 for (new_args
= head
; new_args
; new_args
= new_args
->next
)
429 if (new_args
->sym
== f
->sym
)
436 f
->sym
->attr
.not_always_present
= 1;
441 /* Resolve alternate entry points. If a symbol has multiple entry points we
442 create a new master symbol for the main routine, and turn the existing
443 symbol into an entry point. */
446 resolve_entries (gfc_namespace
*ns
)
448 gfc_namespace
*old_ns
;
452 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
453 static int master_count
= 0;
455 if (ns
->proc_name
== NULL
)
458 /* No need to do anything if this procedure doesn't have alternate entry
463 /* We may already have resolved alternate entry points. */
464 if (ns
->proc_name
->attr
.entry_master
)
467 /* If this isn't a procedure something has gone horribly wrong. */
468 gcc_assert (ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
);
470 /* Remember the current namespace. */
471 old_ns
= gfc_current_ns
;
475 /* Add the main entry point to the list of entry points. */
476 el
= gfc_get_entry_list ();
477 el
->sym
= ns
->proc_name
;
479 el
->next
= ns
->entries
;
481 ns
->proc_name
->attr
.entry
= 1;
483 /* If it is a module function, it needs to be in the right namespace
484 so that gfc_get_fake_result_decl can gather up the results. The
485 need for this arose in get_proc_name, where these beasts were
486 left in their own namespace, to keep prior references linked to
487 the entry declaration.*/
488 if (ns
->proc_name
->attr
.function
489 && ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
492 /* Do the same for entries where the master is not a module
493 procedure. These are retained in the module namespace because
494 of the module procedure declaration. */
495 for (el
= el
->next
; el
; el
= el
->next
)
496 if (el
->sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
497 && el
->sym
->attr
.mod_proc
)
501 /* Add an entry statement for it. */
508 /* Create a new symbol for the master function. */
509 /* Give the internal function a unique name (within this file).
510 Also include the function name so the user has some hope of figuring
511 out what is going on. */
512 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "master.%d.%s",
513 master_count
++, ns
->proc_name
->name
);
514 gfc_get_ha_symbol (name
, &proc
);
515 gcc_assert (proc
!= NULL
);
517 gfc_add_procedure (&proc
->attr
, PROC_INTERNAL
, proc
->name
, NULL
);
518 if (ns
->proc_name
->attr
.subroutine
)
519 gfc_add_subroutine (&proc
->attr
, proc
->name
, NULL
);
523 gfc_typespec
*ts
, *fts
;
524 gfc_array_spec
*as
, *fas
;
525 gfc_add_function (&proc
->attr
, proc
->name
, NULL
);
527 fas
= ns
->entries
->sym
->as
;
528 fas
= fas
? fas
: ns
->entries
->sym
->result
->as
;
529 fts
= &ns
->entries
->sym
->result
->ts
;
530 if (fts
->type
== BT_UNKNOWN
)
531 fts
= gfc_get_default_type (ns
->entries
->sym
->result
->name
, NULL
);
532 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
534 ts
= &el
->sym
->result
->ts
;
536 as
= as
? as
: el
->sym
->result
->as
;
537 if (ts
->type
== BT_UNKNOWN
)
538 ts
= gfc_get_default_type (el
->sym
->result
->name
, NULL
);
540 if (! gfc_compare_types (ts
, fts
)
541 || (el
->sym
->result
->attr
.dimension
542 != ns
->entries
->sym
->result
->attr
.dimension
)
543 || (el
->sym
->result
->attr
.pointer
544 != ns
->entries
->sym
->result
->attr
.pointer
))
546 else if (as
&& fas
&& ns
->entries
->sym
->result
!= el
->sym
->result
547 && gfc_compare_array_spec (as
, fas
) == 0)
548 gfc_error ("Function %s at %L has entries with mismatched "
549 "array specifications", ns
->entries
->sym
->name
,
550 &ns
->entries
->sym
->declared_at
);
551 /* The characteristics need to match and thus both need to have
552 the same string length, i.e. both len=*, or both len=4.
553 Having both len=<variable> is also possible, but difficult to
554 check at compile time. */
555 else if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& fts
->u
.cl
556 && (((ts
->u
.cl
->length
&& !fts
->u
.cl
->length
)
557 ||(!ts
->u
.cl
->length
&& fts
->u
.cl
->length
))
559 && ts
->u
.cl
->length
->expr_type
560 != fts
->u
.cl
->length
->expr_type
)
562 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
563 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
564 fts
->u
.cl
->length
->value
.integer
) != 0)))
565 gfc_notify_std (GFC_STD_GNU
, "Extension: Function %s at %L with "
566 "entries returning variables of different "
567 "string lengths", ns
->entries
->sym
->name
,
568 &ns
->entries
->sym
->declared_at
);
573 sym
= ns
->entries
->sym
->result
;
574 /* All result types the same. */
576 if (sym
->attr
.dimension
)
577 gfc_set_array_spec (proc
, gfc_copy_array_spec (sym
->as
), NULL
);
578 if (sym
->attr
.pointer
)
579 gfc_add_pointer (&proc
->attr
, NULL
);
583 /* Otherwise the result will be passed through a union by
585 proc
->attr
.mixed_entry_master
= 1;
586 for (el
= ns
->entries
; el
; el
= el
->next
)
588 sym
= el
->sym
->result
;
589 if (sym
->attr
.dimension
)
591 if (el
== ns
->entries
)
592 gfc_error ("FUNCTION result %s can't be an array in "
593 "FUNCTION %s at %L", sym
->name
,
594 ns
->entries
->sym
->name
, &sym
->declared_at
);
596 gfc_error ("ENTRY result %s can't be an array in "
597 "FUNCTION %s at %L", sym
->name
,
598 ns
->entries
->sym
->name
, &sym
->declared_at
);
600 else if (sym
->attr
.pointer
)
602 if (el
== ns
->entries
)
603 gfc_error ("FUNCTION result %s can't be a POINTER in "
604 "FUNCTION %s at %L", sym
->name
,
605 ns
->entries
->sym
->name
, &sym
->declared_at
);
607 gfc_error ("ENTRY result %s can't be a POINTER in "
608 "FUNCTION %s at %L", sym
->name
,
609 ns
->entries
->sym
->name
, &sym
->declared_at
);
614 if (ts
->type
== BT_UNKNOWN
)
615 ts
= gfc_get_default_type (sym
->name
, NULL
);
619 if (ts
->kind
== gfc_default_integer_kind
)
623 if (ts
->kind
== gfc_default_real_kind
624 || ts
->kind
== gfc_default_double_kind
)
628 if (ts
->kind
== gfc_default_complex_kind
)
632 if (ts
->kind
== gfc_default_logical_kind
)
636 /* We will issue error elsewhere. */
644 if (el
== ns
->entries
)
645 gfc_error ("FUNCTION result %s can't be of type %s "
646 "in FUNCTION %s at %L", sym
->name
,
647 gfc_typename (ts
), ns
->entries
->sym
->name
,
650 gfc_error ("ENTRY result %s can't be of type %s "
651 "in FUNCTION %s at %L", sym
->name
,
652 gfc_typename (ts
), ns
->entries
->sym
->name
,
659 proc
->attr
.access
= ACCESS_PRIVATE
;
660 proc
->attr
.entry_master
= 1;
662 /* Merge all the entry point arguments. */
663 for (el
= ns
->entries
; el
; el
= el
->next
)
664 merge_argument_lists (proc
, el
->sym
->formal
);
666 /* Check the master formal arguments for any that are not
667 present in all entry points. */
668 for (el
= ns
->entries
; el
; el
= el
->next
)
669 check_argument_lists (proc
, el
->sym
->formal
);
671 /* Use the master function for the function body. */
672 ns
->proc_name
= proc
;
674 /* Finalize the new symbols. */
675 gfc_commit_symbols ();
677 /* Restore the original namespace. */
678 gfc_current_ns
= old_ns
;
683 has_default_initializer (gfc_symbol
*der
)
687 gcc_assert (der
->attr
.flavor
== FL_DERIVED
);
688 for (c
= der
->components
; c
; c
= c
->next
)
689 if ((c
->ts
.type
!= BT_DERIVED
&& c
->initializer
)
690 || (c
->ts
.type
== BT_DERIVED
691 && (!c
->attr
.pointer
&& has_default_initializer (c
->ts
.u
.derived
))))
697 /* Resolve common variables. */
699 resolve_common_vars (gfc_symbol
*sym
, bool named_common
)
701 gfc_symbol
*csym
= sym
;
703 for (; csym
; csym
= csym
->common_next
)
705 if (csym
->value
|| csym
->attr
.data
)
707 if (!csym
->ns
->is_block_data
)
708 gfc_notify_std (GFC_STD_GNU
, "Variable '%s' at %L is in COMMON "
709 "but only in BLOCK DATA initialization is "
710 "allowed", csym
->name
, &csym
->declared_at
);
711 else if (!named_common
)
712 gfc_notify_std (GFC_STD_GNU
, "Initialized variable '%s' at %L is "
713 "in a blank COMMON but initialization is only "
714 "allowed in named common blocks", csym
->name
,
718 if (csym
->ts
.type
!= BT_DERIVED
)
721 if (!(csym
->ts
.u
.derived
->attr
.sequence
722 || csym
->ts
.u
.derived
->attr
.is_bind_c
))
723 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
724 "has neither the SEQUENCE nor the BIND(C) "
725 "attribute", csym
->name
, &csym
->declared_at
);
726 if (csym
->ts
.u
.derived
->attr
.alloc_comp
)
727 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
728 "has an ultimate component that is "
729 "allocatable", csym
->name
, &csym
->declared_at
);
730 if (has_default_initializer (csym
->ts
.u
.derived
))
731 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
732 "may not have default initializer", csym
->name
,
735 if (csym
->attr
.flavor
== FL_UNKNOWN
&& !csym
->attr
.proc_pointer
)
736 gfc_add_flavor (&csym
->attr
, FL_VARIABLE
, csym
->name
, &csym
->declared_at
);
740 /* Resolve common blocks. */
742 resolve_common_blocks (gfc_symtree
*common_root
)
746 if (common_root
== NULL
)
749 if (common_root
->left
)
750 resolve_common_blocks (common_root
->left
);
751 if (common_root
->right
)
752 resolve_common_blocks (common_root
->right
);
754 resolve_common_vars (common_root
->n
.common
->head
, true);
756 gfc_find_symbol (common_root
->name
, gfc_current_ns
, 0, &sym
);
760 if (sym
->attr
.flavor
== FL_PARAMETER
)
761 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
762 sym
->name
, &common_root
->n
.common
->where
, &sym
->declared_at
);
764 if (sym
->attr
.intrinsic
)
765 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
766 sym
->name
, &common_root
->n
.common
->where
);
767 else if (sym
->attr
.result
768 ||(sym
->attr
.function
&& gfc_current_ns
->proc_name
== sym
))
769 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
770 "that is also a function result", sym
->name
,
771 &common_root
->n
.common
->where
);
772 else if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_INTERNAL
773 && sym
->attr
.proc
!= PROC_ST_FUNCTION
)
774 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: COMMON block '%s' at %L "
775 "that is also a global procedure", sym
->name
,
776 &common_root
->n
.common
->where
);
780 /* Resolve contained function types. Because contained functions can call one
781 another, they have to be worked out before any of the contained procedures
784 The good news is that if a function doesn't already have a type, the only
785 way it can get one is through an IMPLICIT type or a RESULT variable, because
786 by definition contained functions are contained namespace they're contained
787 in, not in a sibling or parent namespace. */
790 resolve_contained_functions (gfc_namespace
*ns
)
792 gfc_namespace
*child
;
795 resolve_formal_arglists (ns
);
797 for (child
= ns
->contained
; child
; child
= child
->sibling
)
799 /* Resolve alternate entry points first. */
800 resolve_entries (child
);
802 /* Then check function return types. */
803 resolve_contained_fntype (child
->proc_name
, child
);
804 for (el
= child
->entries
; el
; el
= el
->next
)
805 resolve_contained_fntype (el
->sym
, child
);
810 /* Resolve all of the elements of a structure constructor and make sure that
811 the types are correct. */
814 resolve_structure_cons (gfc_expr
*expr
)
816 gfc_constructor
*cons
;
822 cons
= expr
->value
.constructor
;
823 /* A constructor may have references if it is the result of substituting a
824 parameter variable. In this case we just pull out the component we
827 comp
= expr
->ref
->u
.c
.sym
->components
;
829 comp
= expr
->ts
.u
.derived
->components
;
831 /* See if the user is trying to invoke a structure constructor for one of
832 the iso_c_binding derived types. */
833 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
834 && expr
->ts
.u
.derived
->ts
.is_iso_c
&& cons
&& cons
->expr
!= NULL
)
836 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
837 expr
->ts
.u
.derived
->name
, &(expr
->where
));
841 for (; comp
; comp
= comp
->next
, cons
= cons
->next
)
848 if (gfc_resolve_expr (cons
->expr
) == FAILURE
)
854 rank
= comp
->as
? comp
->as
->rank
: 0;
855 if (cons
->expr
->expr_type
!= EXPR_NULL
&& rank
!= cons
->expr
->rank
856 && (comp
->attr
.allocatable
|| cons
->expr
->rank
))
858 gfc_error ("The rank of the element in the derived type "
859 "constructor at %L does not match that of the "
860 "component (%d/%d)", &cons
->expr
->where
,
861 cons
->expr
->rank
, rank
);
865 /* If we don't have the right type, try to convert it. */
867 if (!gfc_compare_types (&cons
->expr
->ts
, &comp
->ts
))
870 if (comp
->attr
.pointer
&& cons
->expr
->ts
.type
!= BT_UNKNOWN
)
871 gfc_error ("The element in the derived type constructor at %L, "
872 "for pointer component '%s', is %s but should be %s",
873 &cons
->expr
->where
, comp
->name
,
874 gfc_basic_typename (cons
->expr
->ts
.type
),
875 gfc_basic_typename (comp
->ts
.type
));
877 t
= gfc_convert_type (cons
->expr
, &comp
->ts
, 1);
880 if (cons
->expr
->expr_type
== EXPR_NULL
881 && !(comp
->attr
.pointer
|| comp
->attr
.allocatable
882 || comp
->attr
.proc_pointer
883 || (comp
->ts
.type
== BT_CLASS
884 && (comp
->ts
.u
.derived
->components
->attr
.pointer
885 || comp
->ts
.u
.derived
->components
->attr
.allocatable
))))
888 gfc_error ("The NULL in the derived type constructor at %L is "
889 "being applied to component '%s', which is neither "
890 "a POINTER nor ALLOCATABLE", &cons
->expr
->where
,
894 if (!comp
->attr
.pointer
|| cons
->expr
->expr_type
== EXPR_NULL
)
897 a
= gfc_expr_attr (cons
->expr
);
899 if (!a
.pointer
&& !a
.target
)
902 gfc_error ("The element in the derived type constructor at %L, "
903 "for pointer component '%s' should be a POINTER or "
904 "a TARGET", &cons
->expr
->where
, comp
->name
);
912 /****************** Expression name resolution ******************/
914 /* Returns 0 if a symbol was not declared with a type or
915 attribute declaration statement, nonzero otherwise. */
918 was_declared (gfc_symbol
*sym
)
924 if (!a
.implicit_type
&& sym
->ts
.type
!= BT_UNKNOWN
)
927 if (a
.allocatable
|| a
.dimension
|| a
.dummy
|| a
.external
|| a
.intrinsic
928 || a
.optional
|| a
.pointer
|| a
.save
|| a
.target
|| a
.volatile_
929 || a
.value
|| a
.access
!= ACCESS_UNKNOWN
|| a
.intent
!= INTENT_UNKNOWN
)
936 /* Determine if a symbol is generic or not. */
939 generic_sym (gfc_symbol
*sym
)
943 if (sym
->attr
.generic
||
944 (sym
->attr
.intrinsic
&& gfc_generic_intrinsic (sym
->name
)))
947 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
950 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
957 return generic_sym (s
);
964 /* Determine if a symbol is specific or not. */
967 specific_sym (gfc_symbol
*sym
)
971 if (sym
->attr
.if_source
== IFSRC_IFBODY
972 || sym
->attr
.proc
== PROC_MODULE
973 || sym
->attr
.proc
== PROC_INTERNAL
974 || sym
->attr
.proc
== PROC_ST_FUNCTION
975 || (sym
->attr
.intrinsic
&& gfc_specific_intrinsic (sym
->name
))
976 || sym
->attr
.external
)
979 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
982 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
);
984 return (s
== NULL
) ? 0 : specific_sym (s
);
988 /* Figure out if the procedure is specific, generic or unknown. */
991 { PTYPE_GENERIC
= 1, PTYPE_SPECIFIC
, PTYPE_UNKNOWN
}
995 procedure_kind (gfc_symbol
*sym
)
997 if (generic_sym (sym
))
998 return PTYPE_GENERIC
;
1000 if (specific_sym (sym
))
1001 return PTYPE_SPECIFIC
;
1003 return PTYPE_UNKNOWN
;
1006 /* Check references to assumed size arrays. The flag need_full_assumed_size
1007 is nonzero when matching actual arguments. */
1009 static int need_full_assumed_size
= 0;
1012 check_assumed_size_reference (gfc_symbol
*sym
, gfc_expr
*e
)
1014 if (need_full_assumed_size
|| !(sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
))
1017 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1018 What should it be? */
1019 if ((e
->ref
->u
.ar
.end
[e
->ref
->u
.ar
.as
->rank
- 1] == NULL
)
1020 && (e
->ref
->u
.ar
.as
->type
== AS_ASSUMED_SIZE
)
1021 && (e
->ref
->u
.ar
.type
== AR_FULL
))
1023 gfc_error ("The upper bound in the last dimension must "
1024 "appear in the reference to the assumed size "
1025 "array '%s' at %L", sym
->name
, &e
->where
);
1032 /* Look for bad assumed size array references in argument expressions
1033 of elemental and array valued intrinsic procedures. Since this is
1034 called from procedure resolution functions, it only recurses at
1038 resolve_assumed_size_actual (gfc_expr
*e
)
1043 switch (e
->expr_type
)
1046 if (e
->symtree
&& check_assumed_size_reference (e
->symtree
->n
.sym
, e
))
1051 if (resolve_assumed_size_actual (e
->value
.op
.op1
)
1052 || resolve_assumed_size_actual (e
->value
.op
.op2
))
1063 /* Check a generic procedure, passed as an actual argument, to see if
1064 there is a matching specific name. If none, it is an error, and if
1065 more than one, the reference is ambiguous. */
1067 count_specific_procs (gfc_expr
*e
)
1074 sym
= e
->symtree
->n
.sym
;
1076 for (p
= sym
->generic
; p
; p
= p
->next
)
1077 if (strcmp (sym
->name
, p
->sym
->name
) == 0)
1079 e
->symtree
= gfc_find_symtree (p
->sym
->ns
->sym_root
,
1085 gfc_error ("'%s' at %L is ambiguous", e
->symtree
->n
.sym
->name
,
1089 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1090 "argument at %L", sym
->name
, &e
->where
);
1096 /* See if a call to sym could possibly be a not allowed RECURSION because of
1097 a missing RECURIVE declaration. This means that either sym is the current
1098 context itself, or sym is the parent of a contained procedure calling its
1099 non-RECURSIVE containing procedure.
1100 This also works if sym is an ENTRY. */
1103 is_illegal_recursion (gfc_symbol
* sym
, gfc_namespace
* context
)
1105 gfc_symbol
* proc_sym
;
1106 gfc_symbol
* context_proc
;
1107 gfc_namespace
* real_context
;
1109 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
);
1111 /* If we've got an ENTRY, find real procedure. */
1112 if (sym
->attr
.entry
&& sym
->ns
->entries
)
1113 proc_sym
= sym
->ns
->entries
->sym
;
1117 /* If sym is RECURSIVE, all is well of course. */
1118 if (proc_sym
->attr
.recursive
|| gfc_option
.flag_recursive
)
1121 /* Find the context procedure's "real" symbol if it has entries.
1122 We look for a procedure symbol, so recurse on the parents if we don't
1123 find one (like in case of a BLOCK construct). */
1124 for (real_context
= context
; ; real_context
= real_context
->parent
)
1126 /* We should find something, eventually! */
1127 gcc_assert (real_context
);
1129 context_proc
= (real_context
->entries
? real_context
->entries
->sym
1130 : real_context
->proc_name
);
1132 /* In some special cases, there may not be a proc_name, like for this
1134 real(bad_kind()) function foo () ...
1135 when checking the call to bad_kind ().
1136 In these cases, we simply return here and assume that the
1141 if (context_proc
->attr
.flavor
!= FL_LABEL
)
1145 /* A call from sym's body to itself is recursion, of course. */
1146 if (context_proc
== proc_sym
)
1149 /* The same is true if context is a contained procedure and sym the
1151 if (context_proc
->attr
.contained
)
1153 gfc_symbol
* parent_proc
;
1155 gcc_assert (context
->parent
);
1156 parent_proc
= (context
->parent
->entries
? context
->parent
->entries
->sym
1157 : context
->parent
->proc_name
);
1159 if (parent_proc
== proc_sym
)
1167 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1168 its typespec and formal argument list. */
1171 resolve_intrinsic (gfc_symbol
*sym
, locus
*loc
)
1173 gfc_intrinsic_sym
* isym
;
1179 /* We already know this one is an intrinsic, so we don't call
1180 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1181 gfc_find_subroutine directly to check whether it is a function or
1184 if ((isym
= gfc_find_function (sym
->name
)))
1186 if (sym
->ts
.type
!= BT_UNKNOWN
&& gfc_option
.warn_surprising
1187 && !sym
->attr
.implicit_type
)
1188 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1189 " ignored", sym
->name
, &sym
->declared_at
);
1191 if (!sym
->attr
.function
&&
1192 gfc_add_function (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1197 else if ((isym
= gfc_find_subroutine (sym
->name
)))
1199 if (sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.implicit_type
)
1201 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1202 " specifier", sym
->name
, &sym
->declared_at
);
1206 if (!sym
->attr
.subroutine
&&
1207 gfc_add_subroutine (&sym
->attr
, sym
->name
, loc
) == FAILURE
)
1212 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym
->name
,
1217 gfc_copy_formal_args_intr (sym
, isym
);
1219 /* Check it is actually available in the standard settings. */
1220 if (gfc_check_intrinsic_standard (isym
, &symstd
, false, sym
->declared_at
)
1223 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1224 " available in the current standard settings but %s. Use"
1225 " an appropriate -std=* option or enable -fall-intrinsics"
1226 " in order to use it.",
1227 sym
->name
, &sym
->declared_at
, symstd
);
1235 /* Resolve a procedure expression, like passing it to a called procedure or as
1236 RHS for a procedure pointer assignment. */
1239 resolve_procedure_expression (gfc_expr
* expr
)
1243 if (expr
->expr_type
!= EXPR_VARIABLE
)
1245 gcc_assert (expr
->symtree
);
1247 sym
= expr
->symtree
->n
.sym
;
1249 if (sym
->attr
.intrinsic
)
1250 resolve_intrinsic (sym
, &expr
->where
);
1252 if (sym
->attr
.flavor
!= FL_PROCEDURE
1253 || (sym
->attr
.function
&& sym
->result
== sym
))
1256 /* A non-RECURSIVE procedure that is used as procedure expression within its
1257 own body is in danger of being called recursively. */
1258 if (is_illegal_recursion (sym
, gfc_current_ns
))
1259 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1260 " itself recursively. Declare it RECURSIVE or use"
1261 " -frecursive", sym
->name
, &expr
->where
);
1267 /* Resolve an actual argument list. Most of the time, this is just
1268 resolving the expressions in the list.
1269 The exception is that we sometimes have to decide whether arguments
1270 that look like procedure arguments are really simple variable
1274 resolve_actual_arglist (gfc_actual_arglist
*arg
, procedure_type ptype
,
1275 bool no_formal_args
)
1278 gfc_symtree
*parent_st
;
1280 int save_need_full_assumed_size
;
1281 gfc_component
*comp
;
1283 for (; arg
; arg
= arg
->next
)
1288 /* Check the label is a valid branching target. */
1291 if (arg
->label
->defined
== ST_LABEL_UNKNOWN
)
1293 gfc_error ("Label %d referenced at %L is never defined",
1294 arg
->label
->value
, &arg
->label
->where
);
1301 if (gfc_is_proc_ptr_comp (e
, &comp
))
1304 if (e
->expr_type
== EXPR_PPC
)
1306 if (comp
->as
!= NULL
)
1307 e
->rank
= comp
->as
->rank
;
1308 e
->expr_type
= EXPR_FUNCTION
;
1313 if (e
->expr_type
== EXPR_VARIABLE
1314 && e
->symtree
->n
.sym
->attr
.generic
1316 && count_specific_procs (e
) != 1)
1319 if (e
->ts
.type
!= BT_PROCEDURE
)
1321 save_need_full_assumed_size
= need_full_assumed_size
;
1322 if (e
->expr_type
!= EXPR_VARIABLE
)
1323 need_full_assumed_size
= 0;
1324 if (gfc_resolve_expr (e
) != SUCCESS
)
1326 need_full_assumed_size
= save_need_full_assumed_size
;
1330 /* See if the expression node should really be a variable reference. */
1332 sym
= e
->symtree
->n
.sym
;
1334 if (sym
->attr
.flavor
== FL_PROCEDURE
1335 || sym
->attr
.intrinsic
1336 || sym
->attr
.external
)
1340 /* If a procedure is not already determined to be something else
1341 check if it is intrinsic. */
1342 if (!sym
->attr
.intrinsic
1343 && !(sym
->attr
.external
|| sym
->attr
.use_assoc
1344 || sym
->attr
.if_source
== IFSRC_IFBODY
)
1345 && gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, e
->where
))
1346 sym
->attr
.intrinsic
= 1;
1348 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1350 gfc_error ("Statement function '%s' at %L is not allowed as an "
1351 "actual argument", sym
->name
, &e
->where
);
1354 actual_ok
= gfc_intrinsic_actual_ok (sym
->name
,
1355 sym
->attr
.subroutine
);
1356 if (sym
->attr
.intrinsic
&& actual_ok
== 0)
1358 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1359 "actual argument", sym
->name
, &e
->where
);
1362 if (sym
->attr
.contained
&& !sym
->attr
.use_assoc
1363 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1365 gfc_error ("Internal procedure '%s' is not allowed as an "
1366 "actual argument at %L", sym
->name
, &e
->where
);
1369 if (sym
->attr
.elemental
&& !sym
->attr
.intrinsic
)
1371 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1372 "allowed as an actual argument at %L", sym
->name
,
1376 /* Check if a generic interface has a specific procedure
1377 with the same name before emitting an error. */
1378 if (sym
->attr
.generic
&& count_specific_procs (e
) != 1)
1381 /* Just in case a specific was found for the expression. */
1382 sym
= e
->symtree
->n
.sym
;
1384 /* If the symbol is the function that names the current (or
1385 parent) scope, then we really have a variable reference. */
1387 if (sym
->attr
.function
&& sym
->result
== sym
1388 && (sym
->ns
->proc_name
== sym
1389 || (sym
->ns
->parent
!= NULL
1390 && sym
->ns
->parent
->proc_name
== sym
)))
1393 /* If all else fails, see if we have a specific intrinsic. */
1394 if (sym
->ts
.type
== BT_UNKNOWN
&& sym
->attr
.intrinsic
)
1396 gfc_intrinsic_sym
*isym
;
1398 isym
= gfc_find_function (sym
->name
);
1399 if (isym
== NULL
|| !isym
->specific
)
1401 gfc_error ("Unable to find a specific INTRINSIC procedure "
1402 "for the reference '%s' at %L", sym
->name
,
1407 sym
->attr
.intrinsic
= 1;
1408 sym
->attr
.function
= 1;
1411 if (gfc_resolve_expr (e
) == FAILURE
)
1416 /* See if the name is a module procedure in a parent unit. */
1418 if (was_declared (sym
) || sym
->ns
->parent
== NULL
)
1421 if (gfc_find_sym_tree (sym
->name
, sym
->ns
->parent
, 1, &parent_st
))
1423 gfc_error ("Symbol '%s' at %L is ambiguous", sym
->name
, &e
->where
);
1427 if (parent_st
== NULL
)
1430 sym
= parent_st
->n
.sym
;
1431 e
->symtree
= parent_st
; /* Point to the right thing. */
1433 if (sym
->attr
.flavor
== FL_PROCEDURE
1434 || sym
->attr
.intrinsic
1435 || sym
->attr
.external
)
1437 if (gfc_resolve_expr (e
) == FAILURE
)
1443 e
->expr_type
= EXPR_VARIABLE
;
1445 if (sym
->as
!= NULL
)
1447 e
->rank
= sym
->as
->rank
;
1448 e
->ref
= gfc_get_ref ();
1449 e
->ref
->type
= REF_ARRAY
;
1450 e
->ref
->u
.ar
.type
= AR_FULL
;
1451 e
->ref
->u
.ar
.as
= sym
->as
;
1454 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1455 primary.c (match_actual_arg). If above code determines that it
1456 is a variable instead, it needs to be resolved as it was not
1457 done at the beginning of this function. */
1458 save_need_full_assumed_size
= need_full_assumed_size
;
1459 if (e
->expr_type
!= EXPR_VARIABLE
)
1460 need_full_assumed_size
= 0;
1461 if (gfc_resolve_expr (e
) != SUCCESS
)
1463 need_full_assumed_size
= save_need_full_assumed_size
;
1466 /* Check argument list functions %VAL, %LOC and %REF. There is
1467 nothing to do for %REF. */
1468 if (arg
->name
&& arg
->name
[0] == '%')
1470 if (strncmp ("%VAL", arg
->name
, 4) == 0)
1472 if (e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_DERIVED
)
1474 gfc_error ("By-value argument at %L is not of numeric "
1481 gfc_error ("By-value argument at %L cannot be an array or "
1482 "an array section", &e
->where
);
1486 /* Intrinsics are still PROC_UNKNOWN here. However,
1487 since same file external procedures are not resolvable
1488 in gfortran, it is a good deal easier to leave them to
1490 if (ptype
!= PROC_UNKNOWN
1491 && ptype
!= PROC_DUMMY
1492 && ptype
!= PROC_EXTERNAL
1493 && ptype
!= PROC_MODULE
)
1495 gfc_error ("By-value argument at %L is not allowed "
1496 "in this context", &e
->where
);
1501 /* Statement functions have already been excluded above. */
1502 else if (strncmp ("%LOC", arg
->name
, 4) == 0
1503 && e
->ts
.type
== BT_PROCEDURE
)
1505 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_INTERNAL
)
1507 gfc_error ("Passing internal procedure at %L by location "
1508 "not allowed", &e
->where
);
1519 /* Do the checks of the actual argument list that are specific to elemental
1520 procedures. If called with c == NULL, we have a function, otherwise if
1521 expr == NULL, we have a subroutine. */
1524 resolve_elemental_actual (gfc_expr
*expr
, gfc_code
*c
)
1526 gfc_actual_arglist
*arg0
;
1527 gfc_actual_arglist
*arg
;
1528 gfc_symbol
*esym
= NULL
;
1529 gfc_intrinsic_sym
*isym
= NULL
;
1531 gfc_intrinsic_arg
*iformal
= NULL
;
1532 gfc_formal_arglist
*eformal
= NULL
;
1533 bool formal_optional
= false;
1534 bool set_by_optional
= false;
1538 /* Is this an elemental procedure? */
1539 if (expr
&& expr
->value
.function
.actual
!= NULL
)
1541 if (expr
->value
.function
.esym
!= NULL
1542 && expr
->value
.function
.esym
->attr
.elemental
)
1544 arg0
= expr
->value
.function
.actual
;
1545 esym
= expr
->value
.function
.esym
;
1547 else if (expr
->value
.function
.isym
!= NULL
1548 && expr
->value
.function
.isym
->elemental
)
1550 arg0
= expr
->value
.function
.actual
;
1551 isym
= expr
->value
.function
.isym
;
1556 else if (c
&& c
->ext
.actual
!= NULL
)
1558 arg0
= c
->ext
.actual
;
1560 if (c
->resolved_sym
)
1561 esym
= c
->resolved_sym
;
1563 esym
= c
->symtree
->n
.sym
;
1566 if (!esym
->attr
.elemental
)
1572 /* The rank of an elemental is the rank of its array argument(s). */
1573 for (arg
= arg0
; arg
; arg
= arg
->next
)
1575 if (arg
->expr
!= NULL
&& arg
->expr
->rank
> 0)
1577 rank
= arg
->expr
->rank
;
1578 if (arg
->expr
->expr_type
== EXPR_VARIABLE
1579 && arg
->expr
->symtree
->n
.sym
->attr
.optional
)
1580 set_by_optional
= true;
1582 /* Function specific; set the result rank and shape. */
1586 if (!expr
->shape
&& arg
->expr
->shape
)
1588 expr
->shape
= gfc_get_shape (rank
);
1589 for (i
= 0; i
< rank
; i
++)
1590 mpz_init_set (expr
->shape
[i
], arg
->expr
->shape
[i
]);
1597 /* If it is an array, it shall not be supplied as an actual argument
1598 to an elemental procedure unless an array of the same rank is supplied
1599 as an actual argument corresponding to a nonoptional dummy argument of
1600 that elemental procedure(12.4.1.5). */
1601 formal_optional
= false;
1603 iformal
= isym
->formal
;
1605 eformal
= esym
->formal
;
1607 for (arg
= arg0
; arg
; arg
= arg
->next
)
1611 if (eformal
->sym
&& eformal
->sym
->attr
.optional
)
1612 formal_optional
= true;
1613 eformal
= eformal
->next
;
1615 else if (isym
&& iformal
)
1617 if (iformal
->optional
)
1618 formal_optional
= true;
1619 iformal
= iformal
->next
;
1622 formal_optional
= true;
1624 if (pedantic
&& arg
->expr
!= NULL
1625 && arg
->expr
->expr_type
== EXPR_VARIABLE
1626 && arg
->expr
->symtree
->n
.sym
->attr
.optional
1629 && (set_by_optional
|| arg
->expr
->rank
!= rank
)
1630 && !(isym
&& isym
->id
== GFC_ISYM_CONVERSION
))
1632 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1633 "MISSING, it cannot be the actual argument of an "
1634 "ELEMENTAL procedure unless there is a non-optional "
1635 "argument with the same rank (12.4.1.5)",
1636 arg
->expr
->symtree
->n
.sym
->name
, &arg
->expr
->where
);
1641 for (arg
= arg0
; arg
; arg
= arg
->next
)
1643 if (arg
->expr
== NULL
|| arg
->expr
->rank
== 0)
1646 /* Being elemental, the last upper bound of an assumed size array
1647 argument must be present. */
1648 if (resolve_assumed_size_actual (arg
->expr
))
1651 /* Elemental procedure's array actual arguments must conform. */
1654 if (gfc_check_conformance (arg
->expr
, e
,
1655 "elemental procedure") == FAILURE
)
1662 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1663 is an array, the intent inout/out variable needs to be also an array. */
1664 if (rank
> 0 && esym
&& expr
== NULL
)
1665 for (eformal
= esym
->formal
, arg
= arg0
; arg
&& eformal
;
1666 arg
= arg
->next
, eformal
= eformal
->next
)
1667 if ((eformal
->sym
->attr
.intent
== INTENT_OUT
1668 || eformal
->sym
->attr
.intent
== INTENT_INOUT
)
1669 && arg
->expr
&& arg
->expr
->rank
== 0)
1671 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1672 "ELEMENTAL subroutine '%s' is a scalar, but another "
1673 "actual argument is an array", &arg
->expr
->where
,
1674 (eformal
->sym
->attr
.intent
== INTENT_OUT
) ? "OUT"
1675 : "INOUT", eformal
->sym
->name
, esym
->name
);
1682 /* Go through each actual argument in ACTUAL and see if it can be
1683 implemented as an inlined, non-copying intrinsic. FNSYM is the
1684 function being called, or NULL if not known. */
1687 find_noncopying_intrinsics (gfc_symbol
*fnsym
, gfc_actual_arglist
*actual
)
1689 gfc_actual_arglist
*ap
;
1692 for (ap
= actual
; ap
; ap
= ap
->next
)
1694 && (expr
= gfc_get_noncopying_intrinsic_argument (ap
->expr
))
1695 && !gfc_check_fncall_dependency (expr
, INTENT_IN
, fnsym
, actual
,
1697 ap
->expr
->inline_noncopying_intrinsic
= 1;
1701 /* This function does the checking of references to global procedures
1702 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1703 77 and 95 standards. It checks for a gsymbol for the name, making
1704 one if it does not already exist. If it already exists, then the
1705 reference being resolved must correspond to the type of gsymbol.
1706 Otherwise, the new symbol is equipped with the attributes of the
1707 reference. The corresponding code that is called in creating
1708 global entities is parse.c.
1710 In addition, for all but -std=legacy, the gsymbols are used to
1711 check the interfaces of external procedures from the same file.
1712 The namespace of the gsymbol is resolved and then, once this is
1713 done the interface is checked. */
1717 not_in_recursive (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1719 if (!gsym_ns
->proc_name
->attr
.recursive
)
1722 if (sym
->ns
== gsym_ns
)
1725 if (sym
->ns
->parent
&& sym
->ns
->parent
== gsym_ns
)
1732 not_entry_self_reference (gfc_symbol
*sym
, gfc_namespace
*gsym_ns
)
1734 if (gsym_ns
->entries
)
1736 gfc_entry_list
*entry
= gsym_ns
->entries
;
1738 for (; entry
; entry
= entry
->next
)
1740 if (strcmp (sym
->name
, entry
->sym
->name
) == 0)
1742 if (strcmp (gsym_ns
->proc_name
->name
,
1743 sym
->ns
->proc_name
->name
) == 0)
1747 && strcmp (gsym_ns
->proc_name
->name
,
1748 sym
->ns
->parent
->proc_name
->name
) == 0)
1757 resolve_global_procedure (gfc_symbol
*sym
, locus
*where
,
1758 gfc_actual_arglist
**actual
, int sub
)
1762 enum gfc_symbol_type type
;
1764 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
1766 gsym
= gfc_get_gsymbol (sym
->name
);
1768 if ((gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= type
))
1769 gfc_global_used (gsym
, where
);
1771 if (gfc_option
.flag_whole_file
1772 && sym
->attr
.if_source
== IFSRC_UNKNOWN
1773 && gsym
->type
!= GSYM_UNKNOWN
1775 && gsym
->ns
->resolved
!= -1
1776 && gsym
->ns
->proc_name
1777 && not_in_recursive (sym
, gsym
->ns
)
1778 && not_entry_self_reference (sym
, gsym
->ns
))
1780 /* Make sure that translation for the gsymbol occurs before
1781 the procedure currently being resolved. */
1782 ns
= gsym
->ns
->resolved
? NULL
: gfc_global_ns_list
;
1783 for (; ns
&& ns
!= gsym
->ns
; ns
= ns
->sibling
)
1785 if (ns
->sibling
== gsym
->ns
)
1787 ns
->sibling
= gsym
->ns
->sibling
;
1788 gsym
->ns
->sibling
= gfc_global_ns_list
;
1789 gfc_global_ns_list
= gsym
->ns
;
1794 if (!gsym
->ns
->resolved
)
1796 gfc_dt_list
*old_dt_list
;
1798 /* Stash away derived types so that the backend_decls do not
1800 old_dt_list
= gfc_derived_types
;
1801 gfc_derived_types
= NULL
;
1803 gfc_resolve (gsym
->ns
);
1805 /* Store the new derived types with the global namespace. */
1806 if (gfc_derived_types
)
1807 gsym
->ns
->derived_types
= gfc_derived_types
;
1809 /* Restore the derived types of this namespace. */
1810 gfc_derived_types
= old_dt_list
;
1813 if (gsym
->ns
->proc_name
->attr
.function
1814 && gsym
->ns
->proc_name
->as
1815 && gsym
->ns
->proc_name
->as
->rank
1816 && (!sym
->as
|| sym
->as
->rank
!= gsym
->ns
->proc_name
->as
->rank
))
1817 gfc_error ("The reference to function '%s' at %L either needs an "
1818 "explicit INTERFACE or the rank is incorrect", sym
->name
,
1821 if (gfc_option
.flag_whole_file
== 1
1822 || ((gfc_option
.warn_std
& GFC_STD_LEGACY
)
1824 !(gfc_option
.warn_std
& GFC_STD_GNU
)))
1825 gfc_errors_to_warnings (1);
1827 gfc_procedure_use (gsym
->ns
->proc_name
, actual
, where
);
1829 gfc_errors_to_warnings (0);
1832 if (gsym
->type
== GSYM_UNKNOWN
)
1835 gsym
->where
= *where
;
1842 /************* Function resolution *************/
1844 /* Resolve a function call known to be generic.
1845 Section 14.1.2.4.1. */
1848 resolve_generic_f0 (gfc_expr
*expr
, gfc_symbol
*sym
)
1852 if (sym
->attr
.generic
)
1854 s
= gfc_search_interface (sym
->generic
, 0, &expr
->value
.function
.actual
);
1857 expr
->value
.function
.name
= s
->name
;
1858 expr
->value
.function
.esym
= s
;
1860 if (s
->ts
.type
!= BT_UNKNOWN
)
1862 else if (s
->result
!= NULL
&& s
->result
->ts
.type
!= BT_UNKNOWN
)
1863 expr
->ts
= s
->result
->ts
;
1866 expr
->rank
= s
->as
->rank
;
1867 else if (s
->result
!= NULL
&& s
->result
->as
!= NULL
)
1868 expr
->rank
= s
->result
->as
->rank
;
1870 gfc_set_sym_referenced (expr
->value
.function
.esym
);
1875 /* TODO: Need to search for elemental references in generic
1879 if (sym
->attr
.intrinsic
)
1880 return gfc_intrinsic_func_interface (expr
, 0);
1887 resolve_generic_f (gfc_expr
*expr
)
1892 sym
= expr
->symtree
->n
.sym
;
1896 m
= resolve_generic_f0 (expr
, sym
);
1899 else if (m
== MATCH_ERROR
)
1903 if (sym
->ns
->parent
== NULL
)
1905 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
1909 if (!generic_sym (sym
))
1913 /* Last ditch attempt. See if the reference is to an intrinsic
1914 that possesses a matching interface. 14.1.2.4 */
1915 if (sym
&& !gfc_is_intrinsic (sym
, 0, expr
->where
))
1917 gfc_error ("There is no specific function for the generic '%s' at %L",
1918 expr
->symtree
->n
.sym
->name
, &expr
->where
);
1922 m
= gfc_intrinsic_func_interface (expr
, 0);
1926 gfc_error ("Generic function '%s' at %L is not consistent with a "
1927 "specific intrinsic interface", expr
->symtree
->n
.sym
->name
,
1934 /* Resolve a function call known to be specific. */
1937 resolve_specific_f0 (gfc_symbol
*sym
, gfc_expr
*expr
)
1941 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
1943 if (sym
->attr
.dummy
)
1945 sym
->attr
.proc
= PROC_DUMMY
;
1949 sym
->attr
.proc
= PROC_EXTERNAL
;
1953 if (sym
->attr
.proc
== PROC_MODULE
1954 || sym
->attr
.proc
== PROC_ST_FUNCTION
1955 || sym
->attr
.proc
== PROC_INTERNAL
)
1958 if (sym
->attr
.intrinsic
)
1960 m
= gfc_intrinsic_func_interface (expr
, 1);
1964 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1965 "with an intrinsic", sym
->name
, &expr
->where
);
1973 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
1976 expr
->ts
= sym
->result
->ts
;
1979 expr
->value
.function
.name
= sym
->name
;
1980 expr
->value
.function
.esym
= sym
;
1981 if (sym
->as
!= NULL
)
1982 expr
->rank
= sym
->as
->rank
;
1989 resolve_specific_f (gfc_expr
*expr
)
1994 sym
= expr
->symtree
->n
.sym
;
1998 m
= resolve_specific_f0 (sym
, expr
);
2001 if (m
== MATCH_ERROR
)
2004 if (sym
->ns
->parent
== NULL
)
2007 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2013 gfc_error ("Unable to resolve the specific function '%s' at %L",
2014 expr
->symtree
->n
.sym
->name
, &expr
->where
);
2020 /* Resolve a procedure call not known to be generic nor specific. */
2023 resolve_unknown_f (gfc_expr
*expr
)
2028 sym
= expr
->symtree
->n
.sym
;
2030 if (sym
->attr
.dummy
)
2032 sym
->attr
.proc
= PROC_DUMMY
;
2033 expr
->value
.function
.name
= sym
->name
;
2037 /* See if we have an intrinsic function reference. */
2039 if (gfc_is_intrinsic (sym
, 0, expr
->where
))
2041 if (gfc_intrinsic_func_interface (expr
, 1) == MATCH_YES
)
2046 /* The reference is to an external name. */
2048 sym
->attr
.proc
= PROC_EXTERNAL
;
2049 expr
->value
.function
.name
= sym
->name
;
2050 expr
->value
.function
.esym
= expr
->symtree
->n
.sym
;
2052 if (sym
->as
!= NULL
)
2053 expr
->rank
= sym
->as
->rank
;
2055 /* Type of the expression is either the type of the symbol or the
2056 default type of the symbol. */
2059 gfc_procedure_use (sym
, &expr
->value
.function
.actual
, &expr
->where
);
2061 if (sym
->ts
.type
!= BT_UNKNOWN
)
2065 ts
= gfc_get_default_type (sym
->name
, sym
->ns
);
2067 if (ts
->type
== BT_UNKNOWN
)
2069 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2070 sym
->name
, &expr
->where
);
2081 /* Return true, if the symbol is an external procedure. */
2083 is_external_proc (gfc_symbol
*sym
)
2085 if (!sym
->attr
.dummy
&& !sym
->attr
.contained
2086 && !(sym
->attr
.intrinsic
2087 || gfc_is_intrinsic (sym
, sym
->attr
.subroutine
, sym
->declared_at
))
2088 && sym
->attr
.proc
!= PROC_ST_FUNCTION
2089 && !sym
->attr
.use_assoc
2097 /* Figure out if a function reference is pure or not. Also set the name
2098 of the function for a potential error message. Return nonzero if the
2099 function is PURE, zero if not. */
2101 pure_stmt_function (gfc_expr
*, gfc_symbol
*);
2104 pure_function (gfc_expr
*e
, const char **name
)
2110 if (e
->symtree
!= NULL
2111 && e
->symtree
->n
.sym
!= NULL
2112 && e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2113 return pure_stmt_function (e
, e
->symtree
->n
.sym
);
2115 if (e
->value
.function
.esym
)
2117 pure
= gfc_pure (e
->value
.function
.esym
);
2118 *name
= e
->value
.function
.esym
->name
;
2120 else if (e
->value
.function
.isym
)
2122 pure
= e
->value
.function
.isym
->pure
2123 || e
->value
.function
.isym
->elemental
;
2124 *name
= e
->value
.function
.isym
->name
;
2128 /* Implicit functions are not pure. */
2130 *name
= e
->value
.function
.name
;
2138 impure_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2139 int *f ATTRIBUTE_UNUSED
)
2143 /* Don't bother recursing into other statement functions
2144 since they will be checked individually for purity. */
2145 if (e
->expr_type
!= EXPR_FUNCTION
2147 || e
->symtree
->n
.sym
== sym
2148 || e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
2151 return pure_function (e
, &name
) ? false : true;
2156 pure_stmt_function (gfc_expr
*e
, gfc_symbol
*sym
)
2158 return gfc_traverse_expr (e
, sym
, impure_stmt_fcn
, 0) ? 0 : 1;
2163 is_scalar_expr_ptr (gfc_expr
*expr
)
2165 gfc_try retval
= SUCCESS
;
2170 /* See if we have a gfc_ref, which means we have a substring, array
2171 reference, or a component. */
2172 if (expr
->ref
!= NULL
)
2175 while (ref
->next
!= NULL
)
2181 if (ref
->u
.ss
.length
!= NULL
2182 && ref
->u
.ss
.length
->length
!= NULL
2184 && ref
->u
.ss
.start
->expr_type
== EXPR_CONSTANT
2186 && ref
->u
.ss
.end
->expr_type
== EXPR_CONSTANT
)
2188 start
= (int) mpz_get_si (ref
->u
.ss
.start
->value
.integer
);
2189 end
= (int) mpz_get_si (ref
->u
.ss
.end
->value
.integer
);
2190 if (end
- start
+ 1 != 1)
2197 if (ref
->u
.ar
.type
== AR_ELEMENT
)
2199 else if (ref
->u
.ar
.type
== AR_FULL
)
2201 /* The user can give a full array if the array is of size 1. */
2202 if (ref
->u
.ar
.as
!= NULL
2203 && ref
->u
.ar
.as
->rank
== 1
2204 && ref
->u
.ar
.as
->type
== AS_EXPLICIT
2205 && ref
->u
.ar
.as
->lower
[0] != NULL
2206 && ref
->u
.ar
.as
->lower
[0]->expr_type
== EXPR_CONSTANT
2207 && ref
->u
.ar
.as
->upper
[0] != NULL
2208 && ref
->u
.ar
.as
->upper
[0]->expr_type
== EXPR_CONSTANT
)
2210 /* If we have a character string, we need to check if
2211 its length is one. */
2212 if (expr
->ts
.type
== BT_CHARACTER
)
2214 if (expr
->ts
.u
.cl
== NULL
2215 || expr
->ts
.u
.cl
->length
== NULL
2216 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1)
2222 /* We have constant lower and upper bounds. If the
2223 difference between is 1, it can be considered a
2225 start
= (int) mpz_get_si
2226 (ref
->u
.ar
.as
->lower
[0]->value
.integer
);
2227 end
= (int) mpz_get_si
2228 (ref
->u
.ar
.as
->upper
[0]->value
.integer
);
2229 if (end
- start
+ 1 != 1)
2244 else if (expr
->ts
.type
== BT_CHARACTER
&& expr
->rank
== 0)
2246 /* Character string. Make sure it's of length 1. */
2247 if (expr
->ts
.u
.cl
== NULL
2248 || expr
->ts
.u
.cl
->length
== NULL
2249 || mpz_cmp_si (expr
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
2252 else if (expr
->rank
!= 0)
2259 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2260 and, in the case of c_associated, set the binding label based on
2264 gfc_iso_c_func_interface (gfc_symbol
*sym
, gfc_actual_arglist
*args
,
2265 gfc_symbol
**new_sym
)
2267 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2268 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2269 int optional_arg
= 0, is_pointer
= 0;
2270 gfc_try retval
= SUCCESS
;
2271 gfc_symbol
*args_sym
;
2272 gfc_typespec
*arg_ts
;
2274 if (args
->expr
->expr_type
== EXPR_CONSTANT
2275 || args
->expr
->expr_type
== EXPR_OP
2276 || args
->expr
->expr_type
== EXPR_NULL
)
2278 gfc_error ("Argument to '%s' at %L is not a variable",
2279 sym
->name
, &(args
->expr
->where
));
2283 args_sym
= args
->expr
->symtree
->n
.sym
;
2285 /* The typespec for the actual arg should be that stored in the expr
2286 and not necessarily that of the expr symbol (args_sym), because
2287 the actual expression could be a part-ref of the expr symbol. */
2288 arg_ts
= &(args
->expr
->ts
);
2290 is_pointer
= gfc_is_data_pointer (args
->expr
);
2292 if (sym
->intmod_sym_id
== ISOCBINDING_ASSOCIATED
)
2294 /* If the user gave two args then they are providing something for
2295 the optional arg (the second cptr). Therefore, set the name and
2296 binding label to the c_associated for two cptrs. Otherwise,
2297 set c_associated to expect one cptr. */
2301 sprintf (name
, "%s_2", sym
->name
);
2302 sprintf (binding_label
, "%s_2", sym
->binding_label
);
2308 sprintf (name
, "%s_1", sym
->name
);
2309 sprintf (binding_label
, "%s_1", sym
->binding_label
);
2313 /* Get a new symbol for the version of c_associated that
2315 *new_sym
= get_iso_c_sym (sym
, name
, binding_label
, optional_arg
);
2317 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
2318 || sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2320 sprintf (name
, "%s", sym
->name
);
2321 sprintf (binding_label
, "%s", sym
->binding_label
);
2323 /* Error check the call. */
2324 if (args
->next
!= NULL
)
2326 gfc_error_now ("More actual than formal arguments in '%s' "
2327 "call at %L", name
, &(args
->expr
->where
));
2330 else if (sym
->intmod_sym_id
== ISOCBINDING_LOC
)
2332 /* Make sure we have either the target or pointer attribute. */
2333 if (!args_sym
->attr
.target
&& !is_pointer
)
2335 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2336 "a TARGET or an associated pointer",
2338 sym
->name
, &(args
->expr
->where
));
2342 /* See if we have interoperable type and type param. */
2343 if (verify_c_interop (arg_ts
) == SUCCESS
2344 || gfc_check_any_c_kind (arg_ts
) == SUCCESS
)
2346 if (args_sym
->attr
.target
== 1)
2348 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2349 has the target attribute and is interoperable. */
2350 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2351 allocatable variable that has the TARGET attribute and
2352 is not an array of zero size. */
2353 if (args_sym
->attr
.allocatable
== 1)
2355 if (args_sym
->attr
.dimension
!= 0
2356 && (args_sym
->as
&& args_sym
->as
->rank
== 0))
2358 gfc_error_now ("Allocatable variable '%s' used as a "
2359 "parameter to '%s' at %L must not be "
2360 "an array of zero size",
2361 args_sym
->name
, sym
->name
,
2362 &(args
->expr
->where
));
2368 /* A non-allocatable target variable with C
2369 interoperable type and type parameters must be
2371 if (args_sym
&& args_sym
->attr
.dimension
)
2373 if (args_sym
->as
->type
== AS_ASSUMED_SHAPE
)
2375 gfc_error ("Assumed-shape array '%s' at %L "
2376 "cannot be an argument to the "
2377 "procedure '%s' because "
2378 "it is not C interoperable",
2380 &(args
->expr
->where
), sym
->name
);
2383 else if (args_sym
->as
->type
== AS_DEFERRED
)
2385 gfc_error ("Deferred-shape array '%s' at %L "
2386 "cannot be an argument to the "
2387 "procedure '%s' because "
2388 "it is not C interoperable",
2390 &(args
->expr
->where
), sym
->name
);
2395 /* Make sure it's not a character string. Arrays of
2396 any type should be ok if the variable is of a C
2397 interoperable type. */
2398 if (arg_ts
->type
== BT_CHARACTER
)
2399 if (arg_ts
->u
.cl
!= NULL
2400 && (arg_ts
->u
.cl
->length
== NULL
2401 || arg_ts
->u
.cl
->length
->expr_type
2404 (arg_ts
->u
.cl
->length
->value
.integer
, 1)
2406 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2408 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2409 "at %L must have a length of 1",
2410 args_sym
->name
, sym
->name
,
2411 &(args
->expr
->where
));
2417 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2419 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2421 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2422 "associated scalar POINTER", args_sym
->name
,
2423 sym
->name
, &(args
->expr
->where
));
2429 /* The parameter is not required to be C interoperable. If it
2430 is not C interoperable, it must be a nonpolymorphic scalar
2431 with no length type parameters. It still must have either
2432 the pointer or target attribute, and it can be
2433 allocatable (but must be allocated when c_loc is called). */
2434 if (args
->expr
->rank
!= 0
2435 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2437 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2438 "scalar", args_sym
->name
, sym
->name
,
2439 &(args
->expr
->where
));
2442 else if (arg_ts
->type
== BT_CHARACTER
2443 && is_scalar_expr_ptr (args
->expr
) != SUCCESS
)
2445 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2446 "%L must have a length of 1",
2447 args_sym
->name
, sym
->name
,
2448 &(args
->expr
->where
));
2453 else if (sym
->intmod_sym_id
== ISOCBINDING_FUNLOC
)
2455 if (args_sym
->attr
.flavor
!= FL_PROCEDURE
)
2457 /* TODO: Update this error message to allow for procedure
2458 pointers once they are implemented. */
2459 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2461 args_sym
->name
, sym
->name
,
2462 &(args
->expr
->where
));
2465 else if (args_sym
->attr
.is_bind_c
!= 1)
2467 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2469 args_sym
->name
, sym
->name
,
2470 &(args
->expr
->where
));
2475 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2480 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2481 "iso_c_binding function: '%s'!\n", sym
->name
);
2488 /* Resolve a function call, which means resolving the arguments, then figuring
2489 out which entity the name refers to. */
2490 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2491 to INTENT(OUT) or INTENT(INOUT). */
2494 resolve_function (gfc_expr
*expr
)
2496 gfc_actual_arglist
*arg
;
2501 procedure_type p
= PROC_INTRINSIC
;
2502 bool no_formal_args
;
2506 sym
= expr
->symtree
->n
.sym
;
2508 if (sym
&& sym
->attr
.intrinsic
2509 && resolve_intrinsic (sym
, &expr
->where
) == FAILURE
)
2512 if (sym
&& (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.subroutine
))
2514 gfc_error ("'%s' at %L is not a function", sym
->name
, &expr
->where
);
2518 if (sym
&& sym
->attr
.abstract
)
2520 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2521 sym
->name
, &expr
->where
);
2525 /* Switch off assumed size checking and do this again for certain kinds
2526 of procedure, once the procedure itself is resolved. */
2527 need_full_assumed_size
++;
2529 if (expr
->symtree
&& expr
->symtree
->n
.sym
)
2530 p
= expr
->symtree
->n
.sym
->attr
.proc
;
2532 no_formal_args
= sym
&& is_external_proc (sym
) && sym
->formal
== NULL
;
2533 if (resolve_actual_arglist (expr
->value
.function
.actual
,
2534 p
, no_formal_args
) == FAILURE
)
2537 /* Need to setup the call to the correct c_associated, depending on
2538 the number of cptrs to user gives to compare. */
2539 if (sym
&& sym
->attr
.is_iso_c
== 1)
2541 if (gfc_iso_c_func_interface (sym
, expr
->value
.function
.actual
, &sym
)
2545 /* Get the symtree for the new symbol (resolved func).
2546 the old one will be freed later, when it's no longer used. */
2547 gfc_find_sym_tree (sym
->name
, sym
->ns
, 1, &(expr
->symtree
));
2550 /* Resume assumed_size checking. */
2551 need_full_assumed_size
--;
2553 /* If the procedure is external, check for usage. */
2554 if (sym
&& is_external_proc (sym
))
2555 resolve_global_procedure (sym
, &expr
->where
,
2556 &expr
->value
.function
.actual
, 0);
2558 if (sym
&& sym
->ts
.type
== BT_CHARACTER
2560 && sym
->ts
.u
.cl
->length
== NULL
2562 && expr
->value
.function
.esym
== NULL
2563 && !sym
->attr
.contained
)
2565 /* Internal procedures are taken care of in resolve_contained_fntype. */
2566 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2567 "be used at %L since it is not a dummy argument",
2568 sym
->name
, &expr
->where
);
2572 /* See if function is already resolved. */
2574 if (expr
->value
.function
.name
!= NULL
)
2576 if (expr
->ts
.type
== BT_UNKNOWN
)
2582 /* Apply the rules of section 14.1.2. */
2584 switch (procedure_kind (sym
))
2587 t
= resolve_generic_f (expr
);
2590 case PTYPE_SPECIFIC
:
2591 t
= resolve_specific_f (expr
);
2595 t
= resolve_unknown_f (expr
);
2599 gfc_internal_error ("resolve_function(): bad function type");
2603 /* If the expression is still a function (it might have simplified),
2604 then we check to see if we are calling an elemental function. */
2606 if (expr
->expr_type
!= EXPR_FUNCTION
)
2609 temp
= need_full_assumed_size
;
2610 need_full_assumed_size
= 0;
2612 if (resolve_elemental_actual (expr
, NULL
) == FAILURE
)
2615 if (omp_workshare_flag
2616 && expr
->value
.function
.esym
2617 && ! gfc_elemental (expr
->value
.function
.esym
))
2619 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2620 "in WORKSHARE construct", expr
->value
.function
.esym
->name
,
2625 #define GENERIC_ID expr->value.function.isym->id
2626 else if (expr
->value
.function
.actual
!= NULL
2627 && expr
->value
.function
.isym
!= NULL
2628 && GENERIC_ID
!= GFC_ISYM_LBOUND
2629 && GENERIC_ID
!= GFC_ISYM_LEN
2630 && GENERIC_ID
!= GFC_ISYM_LOC
2631 && GENERIC_ID
!= GFC_ISYM_PRESENT
)
2633 /* Array intrinsics must also have the last upper bound of an
2634 assumed size array argument. UBOUND and SIZE have to be
2635 excluded from the check if the second argument is anything
2638 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
2640 if ((GENERIC_ID
== GFC_ISYM_UBOUND
|| GENERIC_ID
== GFC_ISYM_SIZE
)
2641 && arg
->next
!= NULL
&& arg
->next
->expr
)
2643 if (arg
->next
->expr
->expr_type
!= EXPR_CONSTANT
)
2646 if (arg
->next
->name
&& strncmp(arg
->next
->name
, "kind", 4) == 0)
2649 if ((int)mpz_get_si (arg
->next
->expr
->value
.integer
)
2654 if (arg
->expr
!= NULL
2655 && arg
->expr
->rank
> 0
2656 && resolve_assumed_size_actual (arg
->expr
))
2662 need_full_assumed_size
= temp
;
2665 if (!pure_function (expr
, &name
) && name
)
2669 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2670 "FORALL %s", name
, &expr
->where
,
2671 forall_flag
== 2 ? "mask" : "block");
2674 else if (gfc_pure (NULL
))
2676 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2677 "procedure within a PURE procedure", name
, &expr
->where
);
2682 /* Functions without the RECURSIVE attribution are not allowed to
2683 * call themselves. */
2684 if (expr
->value
.function
.esym
&& !expr
->value
.function
.esym
->attr
.recursive
)
2687 esym
= expr
->value
.function
.esym
;
2689 if (is_illegal_recursion (esym
, gfc_current_ns
))
2691 if (esym
->attr
.entry
&& esym
->ns
->entries
)
2692 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2693 " function '%s' is not RECURSIVE",
2694 esym
->name
, &expr
->where
, esym
->ns
->entries
->sym
->name
);
2696 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2697 " is not RECURSIVE", esym
->name
, &expr
->where
);
2703 /* Character lengths of use associated functions may contains references to
2704 symbols not referenced from the current program unit otherwise. Make sure
2705 those symbols are marked as referenced. */
2707 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->value
.function
.esym
2708 && expr
->value
.function
.esym
->attr
.use_assoc
)
2710 gfc_expr_set_symbols_referenced (expr
->ts
.u
.cl
->length
);
2714 && !((expr
->value
.function
.esym
2715 && expr
->value
.function
.esym
->attr
.elemental
)
2717 (expr
->value
.function
.isym
2718 && expr
->value
.function
.isym
->elemental
)))
2719 find_noncopying_intrinsics (expr
->value
.function
.esym
,
2720 expr
->value
.function
.actual
);
2722 /* Make sure that the expression has a typespec that works. */
2723 if (expr
->ts
.type
== BT_UNKNOWN
)
2725 if (expr
->symtree
->n
.sym
->result
2726 && expr
->symtree
->n
.sym
->result
->ts
.type
!= BT_UNKNOWN
2727 && !expr
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
2728 expr
->ts
= expr
->symtree
->n
.sym
->result
->ts
;
2735 /************* Subroutine resolution *************/
2738 pure_subroutine (gfc_code
*c
, gfc_symbol
*sym
)
2744 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2745 sym
->name
, &c
->loc
);
2746 else if (gfc_pure (NULL
))
2747 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym
->name
,
2753 resolve_generic_s0 (gfc_code
*c
, gfc_symbol
*sym
)
2757 if (sym
->attr
.generic
)
2759 s
= gfc_search_interface (sym
->generic
, 1, &c
->ext
.actual
);
2762 c
->resolved_sym
= s
;
2763 pure_subroutine (c
, s
);
2767 /* TODO: Need to search for elemental references in generic interface. */
2770 if (sym
->attr
.intrinsic
)
2771 return gfc_intrinsic_sub_interface (c
, 0);
2778 resolve_generic_s (gfc_code
*c
)
2783 sym
= c
->symtree
->n
.sym
;
2787 m
= resolve_generic_s0 (c
, sym
);
2790 else if (m
== MATCH_ERROR
)
2794 if (sym
->ns
->parent
== NULL
)
2796 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
2800 if (!generic_sym (sym
))
2804 /* Last ditch attempt. See if the reference is to an intrinsic
2805 that possesses a matching interface. 14.1.2.4 */
2806 sym
= c
->symtree
->n
.sym
;
2808 if (!gfc_is_intrinsic (sym
, 1, c
->loc
))
2810 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2811 sym
->name
, &c
->loc
);
2815 m
= gfc_intrinsic_sub_interface (c
, 0);
2819 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2820 "intrinsic subroutine interface", sym
->name
, &c
->loc
);
2826 /* Set the name and binding label of the subroutine symbol in the call
2827 expression represented by 'c' to include the type and kind of the
2828 second parameter. This function is for resolving the appropriate
2829 version of c_f_pointer() and c_f_procpointer(). For example, a
2830 call to c_f_pointer() for a default integer pointer could have a
2831 name of c_f_pointer_i4. If no second arg exists, which is an error
2832 for these two functions, it defaults to the generic symbol's name
2833 and binding label. */
2836 set_name_and_label (gfc_code
*c
, gfc_symbol
*sym
,
2837 char *name
, char *binding_label
)
2839 gfc_expr
*arg
= NULL
;
2843 /* The second arg of c_f_pointer and c_f_procpointer determines
2844 the type and kind for the procedure name. */
2845 arg
= c
->ext
.actual
->next
->expr
;
2849 /* Set up the name to have the given symbol's name,
2850 plus the type and kind. */
2851 /* a derived type is marked with the type letter 'u' */
2852 if (arg
->ts
.type
== BT_DERIVED
)
2855 kind
= 0; /* set the kind as 0 for now */
2859 type
= gfc_type_letter (arg
->ts
.type
);
2860 kind
= arg
->ts
.kind
;
2863 if (arg
->ts
.type
== BT_CHARACTER
)
2864 /* Kind info for character strings not needed. */
2867 sprintf (name
, "%s_%c%d", sym
->name
, type
, kind
);
2868 /* Set up the binding label as the given symbol's label plus
2869 the type and kind. */
2870 sprintf (binding_label
, "%s_%c%d", sym
->binding_label
, type
, kind
);
2874 /* If the second arg is missing, set the name and label as
2875 was, cause it should at least be found, and the missing
2876 arg error will be caught by compare_parameters(). */
2877 sprintf (name
, "%s", sym
->name
);
2878 sprintf (binding_label
, "%s", sym
->binding_label
);
2885 /* Resolve a generic version of the iso_c_binding procedure given
2886 (sym) to the specific one based on the type and kind of the
2887 argument(s). Currently, this function resolves c_f_pointer() and
2888 c_f_procpointer based on the type and kind of the second argument
2889 (FPTR). Other iso_c_binding procedures aren't specially handled.
2890 Upon successfully exiting, c->resolved_sym will hold the resolved
2891 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2895 gfc_iso_c_sub_interface (gfc_code
*c
, gfc_symbol
*sym
)
2897 gfc_symbol
*new_sym
;
2898 /* this is fine, since we know the names won't use the max */
2899 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2900 char binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
2901 /* default to success; will override if find error */
2902 match m
= MATCH_YES
;
2904 /* Make sure the actual arguments are in the necessary order (based on the
2905 formal args) before resolving. */
2906 gfc_procedure_use (sym
, &c
->ext
.actual
, &(c
->loc
));
2908 if ((sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
) ||
2909 (sym
->intmod_sym_id
== ISOCBINDING_F_PROCPOINTER
))
2911 set_name_and_label (c
, sym
, name
, binding_label
);
2913 if (sym
->intmod_sym_id
== ISOCBINDING_F_POINTER
)
2915 if (c
->ext
.actual
!= NULL
&& c
->ext
.actual
->next
!= NULL
)
2917 /* Make sure we got a third arg if the second arg has non-zero
2918 rank. We must also check that the type and rank are
2919 correct since we short-circuit this check in
2920 gfc_procedure_use() (called above to sort actual args). */
2921 if (c
->ext
.actual
->next
->expr
->rank
!= 0)
2923 if(c
->ext
.actual
->next
->next
== NULL
2924 || c
->ext
.actual
->next
->next
->expr
== NULL
)
2927 gfc_error ("Missing SHAPE parameter for call to %s "
2928 "at %L", sym
->name
, &(c
->loc
));
2930 else if (c
->ext
.actual
->next
->next
->expr
->ts
.type
2932 || c
->ext
.actual
->next
->next
->expr
->rank
!= 1)
2935 gfc_error ("SHAPE parameter for call to %s at %L must "
2936 "be a rank 1 INTEGER array", sym
->name
,
2943 if (m
!= MATCH_ERROR
)
2945 /* the 1 means to add the optional arg to formal list */
2946 new_sym
= get_iso_c_sym (sym
, name
, binding_label
, 1);
2948 /* for error reporting, say it's declared where the original was */
2949 new_sym
->declared_at
= sym
->declared_at
;
2954 /* no differences for c_loc or c_funloc */
2958 /* set the resolved symbol */
2959 if (m
!= MATCH_ERROR
)
2960 c
->resolved_sym
= new_sym
;
2962 c
->resolved_sym
= sym
;
2968 /* Resolve a subroutine call known to be specific. */
2971 resolve_specific_s0 (gfc_code
*c
, gfc_symbol
*sym
)
2975 if(sym
->attr
.is_iso_c
)
2977 m
= gfc_iso_c_sub_interface (c
,sym
);
2981 if (sym
->attr
.external
|| sym
->attr
.if_source
== IFSRC_IFBODY
)
2983 if (sym
->attr
.dummy
)
2985 sym
->attr
.proc
= PROC_DUMMY
;
2989 sym
->attr
.proc
= PROC_EXTERNAL
;
2993 if (sym
->attr
.proc
== PROC_MODULE
|| sym
->attr
.proc
== PROC_INTERNAL
)
2996 if (sym
->attr
.intrinsic
)
2998 m
= gfc_intrinsic_sub_interface (c
, 1);
3002 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3003 "with an intrinsic", sym
->name
, &c
->loc
);
3011 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3013 c
->resolved_sym
= sym
;
3014 pure_subroutine (c
, sym
);
3021 resolve_specific_s (gfc_code
*c
)
3026 sym
= c
->symtree
->n
.sym
;
3030 m
= resolve_specific_s0 (c
, sym
);
3033 if (m
== MATCH_ERROR
)
3036 if (sym
->ns
->parent
== NULL
)
3039 gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &sym
);
3045 sym
= c
->symtree
->n
.sym
;
3046 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3047 sym
->name
, &c
->loc
);
3053 /* Resolve a subroutine call not known to be generic nor specific. */
3056 resolve_unknown_s (gfc_code
*c
)
3060 sym
= c
->symtree
->n
.sym
;
3062 if (sym
->attr
.dummy
)
3064 sym
->attr
.proc
= PROC_DUMMY
;
3068 /* See if we have an intrinsic function reference. */
3070 if (gfc_is_intrinsic (sym
, 1, c
->loc
))
3072 if (gfc_intrinsic_sub_interface (c
, 1) == MATCH_YES
)
3077 /* The reference is to an external name. */
3080 gfc_procedure_use (sym
, &c
->ext
.actual
, &c
->loc
);
3082 c
->resolved_sym
= sym
;
3084 pure_subroutine (c
, sym
);
3090 /* Resolve a subroutine call. Although it was tempting to use the same code
3091 for functions, subroutines and functions are stored differently and this
3092 makes things awkward. */
3095 resolve_call (gfc_code
*c
)
3098 procedure_type ptype
= PROC_INTRINSIC
;
3099 gfc_symbol
*csym
, *sym
;
3100 bool no_formal_args
;
3102 csym
= c
->symtree
? c
->symtree
->n
.sym
: NULL
;
3104 if (csym
&& csym
->ts
.type
!= BT_UNKNOWN
)
3106 gfc_error ("'%s' at %L has a type, which is not consistent with "
3107 "the CALL at %L", csym
->name
, &csym
->declared_at
, &c
->loc
);
3111 if (csym
&& gfc_current_ns
->parent
&& csym
->ns
!= gfc_current_ns
)
3114 gfc_find_sym_tree (csym
->name
, gfc_current_ns
, 1, &st
);
3115 sym
= st
? st
->n
.sym
: NULL
;
3116 if (sym
&& csym
!= sym
3117 && sym
->ns
== gfc_current_ns
3118 && sym
->attr
.flavor
== FL_PROCEDURE
3119 && sym
->attr
.contained
)
3122 if (csym
->attr
.generic
)
3123 c
->symtree
->n
.sym
= sym
;
3126 csym
= c
->symtree
->n
.sym
;
3130 /* Subroutines without the RECURSIVE attribution are not allowed to
3131 * call themselves. */
3132 if (csym
&& is_illegal_recursion (csym
, gfc_current_ns
))
3134 if (csym
->attr
.entry
&& csym
->ns
->entries
)
3135 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3136 " subroutine '%s' is not RECURSIVE",
3137 csym
->name
, &c
->loc
, csym
->ns
->entries
->sym
->name
);
3139 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3140 " is not RECURSIVE", csym
->name
, &c
->loc
);
3145 /* Switch off assumed size checking and do this again for certain kinds
3146 of procedure, once the procedure itself is resolved. */
3147 need_full_assumed_size
++;
3150 ptype
= csym
->attr
.proc
;
3152 no_formal_args
= csym
&& is_external_proc (csym
) && csym
->formal
== NULL
;
3153 if (resolve_actual_arglist (c
->ext
.actual
, ptype
,
3154 no_formal_args
) == FAILURE
)
3157 /* Resume assumed_size checking. */
3158 need_full_assumed_size
--;
3160 /* If external, check for usage. */
3161 if (csym
&& is_external_proc (csym
))
3162 resolve_global_procedure (csym
, &c
->loc
, &c
->ext
.actual
, 1);
3165 if (c
->resolved_sym
== NULL
)
3167 c
->resolved_isym
= NULL
;
3168 switch (procedure_kind (csym
))
3171 t
= resolve_generic_s (c
);
3174 case PTYPE_SPECIFIC
:
3175 t
= resolve_specific_s (c
);
3179 t
= resolve_unknown_s (c
);
3183 gfc_internal_error ("resolve_subroutine(): bad function type");
3187 /* Some checks of elemental subroutine actual arguments. */
3188 if (resolve_elemental_actual (NULL
, c
) == FAILURE
)
3191 if (t
== SUCCESS
&& !(c
->resolved_sym
&& c
->resolved_sym
->attr
.elemental
))
3192 find_noncopying_intrinsics (c
->resolved_sym
, c
->ext
.actual
);
3197 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3198 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3199 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3200 if their shapes do not match. If either op1->shape or op2->shape is
3201 NULL, return SUCCESS. */
3204 compare_shapes (gfc_expr
*op1
, gfc_expr
*op2
)
3211 if (op1
->shape
!= NULL
&& op2
->shape
!= NULL
)
3213 for (i
= 0; i
< op1
->rank
; i
++)
3215 if (mpz_cmp (op1
->shape
[i
], op2
->shape
[i
]) != 0)
3217 gfc_error ("Shapes for operands at %L and %L are not conformable",
3218 &op1
->where
, &op2
->where
);
3229 /* Resolve an operator expression node. This can involve replacing the
3230 operation with a user defined function call. */
3233 resolve_operator (gfc_expr
*e
)
3235 gfc_expr
*op1
, *op2
;
3237 bool dual_locus_error
;
3240 /* Resolve all subnodes-- give them types. */
3242 switch (e
->value
.op
.op
)
3245 if (gfc_resolve_expr (e
->value
.op
.op2
) == FAILURE
)
3248 /* Fall through... */
3251 case INTRINSIC_UPLUS
:
3252 case INTRINSIC_UMINUS
:
3253 case INTRINSIC_PARENTHESES
:
3254 if (gfc_resolve_expr (e
->value
.op
.op1
) == FAILURE
)
3259 /* Typecheck the new node. */
3261 op1
= e
->value
.op
.op1
;
3262 op2
= e
->value
.op
.op2
;
3263 dual_locus_error
= false;
3265 if ((op1
&& op1
->expr_type
== EXPR_NULL
)
3266 || (op2
&& op2
->expr_type
== EXPR_NULL
))
3268 sprintf (msg
, _("Invalid context for NULL() pointer at %%L"));
3272 switch (e
->value
.op
.op
)
3274 case INTRINSIC_UPLUS
:
3275 case INTRINSIC_UMINUS
:
3276 if (op1
->ts
.type
== BT_INTEGER
3277 || op1
->ts
.type
== BT_REAL
3278 || op1
->ts
.type
== BT_COMPLEX
)
3284 sprintf (msg
, _("Operand of unary numeric operator '%s' at %%L is %s"),
3285 gfc_op2string (e
->value
.op
.op
), gfc_typename (&e
->ts
));
3288 case INTRINSIC_PLUS
:
3289 case INTRINSIC_MINUS
:
3290 case INTRINSIC_TIMES
:
3291 case INTRINSIC_DIVIDE
:
3292 case INTRINSIC_POWER
:
3293 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3295 gfc_type_convert_binary (e
);
3300 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3301 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3302 gfc_typename (&op2
->ts
));
3305 case INTRINSIC_CONCAT
:
3306 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3307 && op1
->ts
.kind
== op2
->ts
.kind
)
3309 e
->ts
.type
= BT_CHARACTER
;
3310 e
->ts
.kind
= op1
->ts
.kind
;
3315 _("Operands of string concatenation operator at %%L are %s/%s"),
3316 gfc_typename (&op1
->ts
), gfc_typename (&op2
->ts
));
3322 case INTRINSIC_NEQV
:
3323 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3325 e
->ts
.type
= BT_LOGICAL
;
3326 e
->ts
.kind
= gfc_kind_max (op1
, op2
);
3327 if (op1
->ts
.kind
< e
->ts
.kind
)
3328 gfc_convert_type (op1
, &e
->ts
, 2);
3329 else if (op2
->ts
.kind
< e
->ts
.kind
)
3330 gfc_convert_type (op2
, &e
->ts
, 2);
3334 sprintf (msg
, _("Operands of logical operator '%s' at %%L are %s/%s"),
3335 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3336 gfc_typename (&op2
->ts
));
3341 if (op1
->ts
.type
== BT_LOGICAL
)
3343 e
->ts
.type
= BT_LOGICAL
;
3344 e
->ts
.kind
= op1
->ts
.kind
;
3348 sprintf (msg
, _("Operand of .not. operator at %%L is %s"),
3349 gfc_typename (&op1
->ts
));
3353 case INTRINSIC_GT_OS
:
3355 case INTRINSIC_GE_OS
:
3357 case INTRINSIC_LT_OS
:
3359 case INTRINSIC_LE_OS
:
3360 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
3362 strcpy (msg
, _("COMPLEX quantities cannot be compared at %L"));
3366 /* Fall through... */
3369 case INTRINSIC_EQ_OS
:
3371 case INTRINSIC_NE_OS
:
3372 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
3373 && op1
->ts
.kind
== op2
->ts
.kind
)
3375 e
->ts
.type
= BT_LOGICAL
;
3376 e
->ts
.kind
= gfc_default_logical_kind
;
3380 if (gfc_numeric_ts (&op1
->ts
) && gfc_numeric_ts (&op2
->ts
))
3382 gfc_type_convert_binary (e
);
3384 e
->ts
.type
= BT_LOGICAL
;
3385 e
->ts
.kind
= gfc_default_logical_kind
;
3389 if (op1
->ts
.type
== BT_LOGICAL
&& op2
->ts
.type
== BT_LOGICAL
)
3391 _("Logicals at %%L must be compared with %s instead of %s"),
3392 (e
->value
.op
.op
== INTRINSIC_EQ
3393 || e
->value
.op
.op
== INTRINSIC_EQ_OS
)
3394 ? ".eqv." : ".neqv.", gfc_op2string (e
->value
.op
.op
));
3397 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3398 gfc_op2string (e
->value
.op
.op
), gfc_typename (&op1
->ts
),
3399 gfc_typename (&op2
->ts
));
3403 case INTRINSIC_USER
:
3404 if (e
->value
.op
.uop
->op
== NULL
)
3405 sprintf (msg
, _("Unknown operator '%s' at %%L"), e
->value
.op
.uop
->name
);
3406 else if (op2
== NULL
)
3407 sprintf (msg
, _("Operand of user operator '%s' at %%L is %s"),
3408 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
));
3410 sprintf (msg
, _("Operands of user operator '%s' at %%L are %s/%s"),
3411 e
->value
.op
.uop
->name
, gfc_typename (&op1
->ts
),
3412 gfc_typename (&op2
->ts
));
3416 case INTRINSIC_PARENTHESES
:
3418 if (e
->ts
.type
== BT_CHARACTER
)
3419 e
->ts
.u
.cl
= op1
->ts
.u
.cl
;
3423 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3426 /* Deal with arrayness of an operand through an operator. */
3430 switch (e
->value
.op
.op
)
3432 case INTRINSIC_PLUS
:
3433 case INTRINSIC_MINUS
:
3434 case INTRINSIC_TIMES
:
3435 case INTRINSIC_DIVIDE
:
3436 case INTRINSIC_POWER
:
3437 case INTRINSIC_CONCAT
:
3441 case INTRINSIC_NEQV
:
3443 case INTRINSIC_EQ_OS
:
3445 case INTRINSIC_NE_OS
:
3447 case INTRINSIC_GT_OS
:
3449 case INTRINSIC_GE_OS
:
3451 case INTRINSIC_LT_OS
:
3453 case INTRINSIC_LE_OS
:
3455 if (op1
->rank
== 0 && op2
->rank
== 0)
3458 if (op1
->rank
== 0 && op2
->rank
!= 0)
3460 e
->rank
= op2
->rank
;
3462 if (e
->shape
== NULL
)
3463 e
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
3466 if (op1
->rank
!= 0 && op2
->rank
== 0)
3468 e
->rank
= op1
->rank
;
3470 if (e
->shape
== NULL
)
3471 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3474 if (op1
->rank
!= 0 && op2
->rank
!= 0)
3476 if (op1
->rank
== op2
->rank
)
3478 e
->rank
= op1
->rank
;
3479 if (e
->shape
== NULL
)
3481 t
= compare_shapes(op1
, op2
);
3485 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3490 /* Allow higher level expressions to work. */
3493 /* Try user-defined operators, and otherwise throw an error. */
3494 dual_locus_error
= true;
3496 _("Inconsistent ranks for operator at %%L and %%L"));
3503 case INTRINSIC_PARENTHESES
:
3505 case INTRINSIC_UPLUS
:
3506 case INTRINSIC_UMINUS
:
3507 /* Simply copy arrayness attribute */
3508 e
->rank
= op1
->rank
;
3510 if (e
->shape
== NULL
)
3511 e
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
3519 /* Attempt to simplify the expression. */
3522 t
= gfc_simplify_expr (e
, 0);
3523 /* Some calls do not succeed in simplification and return FAILURE
3524 even though there is no error; e.g. variable references to
3525 PARAMETER arrays. */
3526 if (!gfc_is_constant_expr (e
))
3535 if (gfc_extend_expr (e
, &real_error
) == SUCCESS
)
3542 if (dual_locus_error
)
3543 gfc_error (msg
, &op1
->where
, &op2
->where
);
3545 gfc_error (msg
, &e
->where
);
3551 /************** Array resolution subroutines **************/
3554 { CMP_LT
, CMP_EQ
, CMP_GT
, CMP_UNKNOWN
}
3557 /* Compare two integer expressions. */
3560 compare_bound (gfc_expr
*a
, gfc_expr
*b
)
3564 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
3565 || b
== NULL
|| b
->expr_type
!= EXPR_CONSTANT
)
3568 /* If either of the types isn't INTEGER, we must have
3569 raised an error earlier. */
3571 if (a
->ts
.type
!= BT_INTEGER
|| b
->ts
.type
!= BT_INTEGER
)
3574 i
= mpz_cmp (a
->value
.integer
, b
->value
.integer
);
3584 /* Compare an integer expression with an integer. */
3587 compare_bound_int (gfc_expr
*a
, int b
)
3591 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
3594 if (a
->ts
.type
!= BT_INTEGER
)
3595 gfc_internal_error ("compare_bound_int(): Bad expression");
3597 i
= mpz_cmp_si (a
->value
.integer
, b
);
3607 /* Compare an integer expression with a mpz_t. */
3610 compare_bound_mpz_t (gfc_expr
*a
, mpz_t b
)
3614 if (a
== NULL
|| a
->expr_type
!= EXPR_CONSTANT
)
3617 if (a
->ts
.type
!= BT_INTEGER
)
3618 gfc_internal_error ("compare_bound_int(): Bad expression");
3620 i
= mpz_cmp (a
->value
.integer
, b
);
3630 /* Compute the last value of a sequence given by a triplet.
3631 Return 0 if it wasn't able to compute the last value, or if the
3632 sequence if empty, and 1 otherwise. */
3635 compute_last_value_for_triplet (gfc_expr
*start
, gfc_expr
*end
,
3636 gfc_expr
*stride
, mpz_t last
)
3640 if (start
== NULL
|| start
->expr_type
!= EXPR_CONSTANT
3641 || end
== NULL
|| end
->expr_type
!= EXPR_CONSTANT
3642 || (stride
!= NULL
&& stride
->expr_type
!= EXPR_CONSTANT
))
3645 if (start
->ts
.type
!= BT_INTEGER
|| end
->ts
.type
!= BT_INTEGER
3646 || (stride
!= NULL
&& stride
->ts
.type
!= BT_INTEGER
))
3649 if (stride
== NULL
|| compare_bound_int(stride
, 1) == CMP_EQ
)
3651 if (compare_bound (start
, end
) == CMP_GT
)
3653 mpz_set (last
, end
->value
.integer
);
3657 if (compare_bound_int (stride
, 0) == CMP_GT
)
3659 /* Stride is positive */
3660 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) > 0)
3665 /* Stride is negative */
3666 if (mpz_cmp (start
->value
.integer
, end
->value
.integer
) < 0)
3671 mpz_sub (rem
, end
->value
.integer
, start
->value
.integer
);
3672 mpz_tdiv_r (rem
, rem
, stride
->value
.integer
);
3673 mpz_sub (last
, end
->value
.integer
, rem
);
3680 /* Compare a single dimension of an array reference to the array
3684 check_dimension (int i
, gfc_array_ref
*ar
, gfc_array_spec
*as
)
3688 /* Given start, end and stride values, calculate the minimum and
3689 maximum referenced indexes. */
3691 switch (ar
->dimen_type
[i
])
3697 if (compare_bound (ar
->start
[i
], as
->lower
[i
]) == CMP_LT
)
3699 gfc_warning ("Array reference at %L is out of bounds "
3700 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3701 mpz_get_si (ar
->start
[i
]->value
.integer
),
3702 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3705 if (compare_bound (ar
->start
[i
], as
->upper
[i
]) == CMP_GT
)
3707 gfc_warning ("Array reference at %L is out of bounds "
3708 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3709 mpz_get_si (ar
->start
[i
]->value
.integer
),
3710 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3718 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3719 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3721 comparison comp_start_end
= compare_bound (AR_START
, AR_END
);
3723 /* Check for zero stride, which is not allowed. */
3724 if (compare_bound_int (ar
->stride
[i
], 0) == CMP_EQ
)
3726 gfc_error ("Illegal stride of zero at %L", &ar
->c_where
[i
]);
3730 /* if start == len || (stride > 0 && start < len)
3731 || (stride < 0 && start > len),
3732 then the array section contains at least one element. In this
3733 case, there is an out-of-bounds access if
3734 (start < lower || start > upper). */
3735 if (compare_bound (AR_START
, AR_END
) == CMP_EQ
3736 || ((compare_bound_int (ar
->stride
[i
], 0) == CMP_GT
3737 || ar
->stride
[i
] == NULL
) && comp_start_end
== CMP_LT
)
3738 || (compare_bound_int (ar
->stride
[i
], 0) == CMP_LT
3739 && comp_start_end
== CMP_GT
))
3741 if (compare_bound (AR_START
, as
->lower
[i
]) == CMP_LT
)
3743 gfc_warning ("Lower array reference at %L is out of bounds "
3744 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3745 mpz_get_si (AR_START
->value
.integer
),
3746 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3749 if (compare_bound (AR_START
, as
->upper
[i
]) == CMP_GT
)
3751 gfc_warning ("Lower array reference at %L is out of bounds "
3752 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3753 mpz_get_si (AR_START
->value
.integer
),
3754 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3759 /* If we can compute the highest index of the array section,
3760 then it also has to be between lower and upper. */
3761 mpz_init (last_value
);
3762 if (compute_last_value_for_triplet (AR_START
, AR_END
, ar
->stride
[i
],
3765 if (compare_bound_mpz_t (as
->lower
[i
], last_value
) == CMP_GT
)
3767 gfc_warning ("Upper array reference at %L is out of bounds "
3768 "(%ld < %ld) in dimension %d", &ar
->c_where
[i
],
3769 mpz_get_si (last_value
),
3770 mpz_get_si (as
->lower
[i
]->value
.integer
), i
+1);
3771 mpz_clear (last_value
);
3774 if (compare_bound_mpz_t (as
->upper
[i
], last_value
) == CMP_LT
)
3776 gfc_warning ("Upper array reference at %L is out of bounds "
3777 "(%ld > %ld) in dimension %d", &ar
->c_where
[i
],
3778 mpz_get_si (last_value
),
3779 mpz_get_si (as
->upper
[i
]->value
.integer
), i
+1);
3780 mpz_clear (last_value
);
3784 mpz_clear (last_value
);
3792 gfc_internal_error ("check_dimension(): Bad array reference");
3799 /* Compare an array reference with an array specification. */
3802 compare_spec_to_ref (gfc_array_ref
*ar
)
3809 /* TODO: Full array sections are only allowed as actual parameters. */
3810 if (as
->type
== AS_ASSUMED_SIZE
3811 && (/*ar->type == AR_FULL
3812 ||*/ (ar
->type
== AR_SECTION
3813 && ar
->dimen_type
[i
] == DIMEN_RANGE
&& ar
->end
[i
] == NULL
)))
3815 gfc_error ("Rightmost upper bound of assumed size array section "
3816 "not specified at %L", &ar
->where
);
3820 if (ar
->type
== AR_FULL
)
3823 if (as
->rank
!= ar
->dimen
)
3825 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3826 &ar
->where
, ar
->dimen
, as
->rank
);
3830 for (i
= 0; i
< as
->rank
; i
++)
3831 if (check_dimension (i
, ar
, as
) == FAILURE
)
3838 /* Resolve one part of an array index. */
3841 gfc_resolve_index (gfc_expr
*index
, int check_scalar
)
3848 if (gfc_resolve_expr (index
) == FAILURE
)
3851 if (check_scalar
&& index
->rank
!= 0)
3853 gfc_error ("Array index at %L must be scalar", &index
->where
);
3857 if (index
->ts
.type
!= BT_INTEGER
&& index
->ts
.type
!= BT_REAL
)
3859 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3860 &index
->where
, gfc_basic_typename (index
->ts
.type
));
3864 if (index
->ts
.type
== BT_REAL
)
3865 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: REAL array index at %L",
3866 &index
->where
) == FAILURE
)
3869 if (index
->ts
.kind
!= gfc_index_integer_kind
3870 || index
->ts
.type
!= BT_INTEGER
)
3873 ts
.type
= BT_INTEGER
;
3874 ts
.kind
= gfc_index_integer_kind
;
3876 gfc_convert_type_warn (index
, &ts
, 2, 0);
3882 /* Resolve a dim argument to an intrinsic function. */
3885 gfc_resolve_dim_arg (gfc_expr
*dim
)
3890 if (gfc_resolve_expr (dim
) == FAILURE
)
3895 gfc_error ("Argument dim at %L must be scalar", &dim
->where
);
3900 if (dim
->ts
.type
!= BT_INTEGER
)
3902 gfc_error ("Argument dim at %L must be of INTEGER type", &dim
->where
);
3906 if (dim
->ts
.kind
!= gfc_index_integer_kind
)
3910 ts
.type
= BT_INTEGER
;
3911 ts
.kind
= gfc_index_integer_kind
;
3913 gfc_convert_type_warn (dim
, &ts
, 2, 0);
3919 /* Given an expression that contains array references, update those array
3920 references to point to the right array specifications. While this is
3921 filled in during matching, this information is difficult to save and load
3922 in a module, so we take care of it here.
3924 The idea here is that the original array reference comes from the
3925 base symbol. We traverse the list of reference structures, setting
3926 the stored reference to references. Component references can
3927 provide an additional array specification. */
3930 find_array_spec (gfc_expr
*e
)
3934 gfc_symbol
*derived
;
3937 if (e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
3938 as
= e
->symtree
->n
.sym
->ts
.u
.derived
->components
->as
;
3940 as
= e
->symtree
->n
.sym
->as
;
3943 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
3948 gfc_internal_error ("find_array_spec(): Missing spec");
3955 if (derived
== NULL
)
3956 derived
= e
->symtree
->n
.sym
->ts
.u
.derived
;
3958 c
= derived
->components
;
3960 for (; c
; c
= c
->next
)
3961 if (c
== ref
->u
.c
.component
)
3963 /* Track the sequence of component references. */
3964 if (c
->ts
.type
== BT_DERIVED
)
3965 derived
= c
->ts
.u
.derived
;
3970 gfc_internal_error ("find_array_spec(): Component not found");
3972 if (c
->attr
.dimension
)
3975 gfc_internal_error ("find_array_spec(): unused as(1)");
3986 gfc_internal_error ("find_array_spec(): unused as(2)");
3990 /* Resolve an array reference. */
3993 resolve_array_ref (gfc_array_ref
*ar
)
3995 int i
, check_scalar
;
3998 for (i
= 0; i
< ar
->dimen
; i
++)
4000 check_scalar
= ar
->dimen_type
[i
] == DIMEN_RANGE
;
4002 if (gfc_resolve_index (ar
->start
[i
], check_scalar
) == FAILURE
)
4004 if (gfc_resolve_index (ar
->end
[i
], check_scalar
) == FAILURE
)
4006 if (gfc_resolve_index (ar
->stride
[i
], check_scalar
) == FAILURE
)
4011 if (ar
->dimen_type
[i
] == DIMEN_UNKNOWN
)
4015 ar
->dimen_type
[i
] = DIMEN_ELEMENT
;
4019 ar
->dimen_type
[i
] = DIMEN_VECTOR
;
4020 if (e
->expr_type
== EXPR_VARIABLE
4021 && e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
)
4022 ar
->start
[i
] = gfc_get_parentheses (e
);
4026 gfc_error ("Array index at %L is an array of rank %d",
4027 &ar
->c_where
[i
], e
->rank
);
4032 /* If the reference type is unknown, figure out what kind it is. */
4034 if (ar
->type
== AR_UNKNOWN
)
4036 ar
->type
= AR_ELEMENT
;
4037 for (i
= 0; i
< ar
->dimen
; i
++)
4038 if (ar
->dimen_type
[i
] == DIMEN_RANGE
4039 || ar
->dimen_type
[i
] == DIMEN_VECTOR
)
4041 ar
->type
= AR_SECTION
;
4046 if (!ar
->as
->cray_pointee
&& compare_spec_to_ref (ar
) == FAILURE
)
4054 resolve_substring (gfc_ref
*ref
)
4056 int k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4058 if (ref
->u
.ss
.start
!= NULL
)
4060 if (gfc_resolve_expr (ref
->u
.ss
.start
) == FAILURE
)
4063 if (ref
->u
.ss
.start
->ts
.type
!= BT_INTEGER
)
4065 gfc_error ("Substring start index at %L must be of type INTEGER",
4066 &ref
->u
.ss
.start
->where
);
4070 if (ref
->u
.ss
.start
->rank
!= 0)
4072 gfc_error ("Substring start index at %L must be scalar",
4073 &ref
->u
.ss
.start
->where
);
4077 if (compare_bound_int (ref
->u
.ss
.start
, 1) == CMP_LT
4078 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4079 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4081 gfc_error ("Substring start index at %L is less than one",
4082 &ref
->u
.ss
.start
->where
);
4087 if (ref
->u
.ss
.end
!= NULL
)
4089 if (gfc_resolve_expr (ref
->u
.ss
.end
) == FAILURE
)
4092 if (ref
->u
.ss
.end
->ts
.type
!= BT_INTEGER
)
4094 gfc_error ("Substring end index at %L must be of type INTEGER",
4095 &ref
->u
.ss
.end
->where
);
4099 if (ref
->u
.ss
.end
->rank
!= 0)
4101 gfc_error ("Substring end index at %L must be scalar",
4102 &ref
->u
.ss
.end
->where
);
4106 if (ref
->u
.ss
.length
!= NULL
4107 && compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.length
->length
) == CMP_GT
4108 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4109 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4111 gfc_error ("Substring end index at %L exceeds the string length",
4112 &ref
->u
.ss
.start
->where
);
4116 if (compare_bound_mpz_t (ref
->u
.ss
.end
,
4117 gfc_integer_kinds
[k
].huge
) == CMP_GT
4118 && (compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_EQ
4119 || compare_bound (ref
->u
.ss
.end
, ref
->u
.ss
.start
) == CMP_GT
))
4121 gfc_error ("Substring end index at %L is too large",
4122 &ref
->u
.ss
.end
->where
);
4131 /* This function supplies missing substring charlens. */
4134 gfc_resolve_substring_charlen (gfc_expr
*e
)
4137 gfc_expr
*start
, *end
;
4139 for (char_ref
= e
->ref
; char_ref
; char_ref
= char_ref
->next
)
4140 if (char_ref
->type
== REF_SUBSTRING
)
4146 gcc_assert (char_ref
->next
== NULL
);
4150 if (e
->ts
.u
.cl
->length
)
4151 gfc_free_expr (e
->ts
.u
.cl
->length
);
4152 else if (e
->expr_type
== EXPR_VARIABLE
4153 && e
->symtree
->n
.sym
->attr
.dummy
)
4157 e
->ts
.type
= BT_CHARACTER
;
4158 e
->ts
.kind
= gfc_default_character_kind
;
4161 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4163 if (char_ref
->u
.ss
.start
)
4164 start
= gfc_copy_expr (char_ref
->u
.ss
.start
);
4166 start
= gfc_int_expr (1);
4168 if (char_ref
->u
.ss
.end
)
4169 end
= gfc_copy_expr (char_ref
->u
.ss
.end
);
4170 else if (e
->expr_type
== EXPR_VARIABLE
)
4171 end
= gfc_copy_expr (e
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4178 /* Length = (end - start +1). */
4179 e
->ts
.u
.cl
->length
= gfc_subtract (end
, start
);
4180 e
->ts
.u
.cl
->length
= gfc_add (e
->ts
.u
.cl
->length
, gfc_int_expr (1));
4182 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4183 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4185 /* Make sure that the length is simplified. */
4186 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 1);
4187 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4191 /* Resolve subtype references. */
4194 resolve_ref (gfc_expr
*expr
)
4196 int current_part_dimension
, n_components
, seen_part_dimension
;
4199 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4200 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
== NULL
)
4202 find_array_spec (expr
);
4206 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4210 if (resolve_array_ref (&ref
->u
.ar
) == FAILURE
)
4218 resolve_substring (ref
);
4222 /* Check constraints on part references. */
4224 current_part_dimension
= 0;
4225 seen_part_dimension
= 0;
4228 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
4233 switch (ref
->u
.ar
.type
)
4237 current_part_dimension
= 1;
4241 current_part_dimension
= 0;
4245 gfc_internal_error ("resolve_ref(): Bad array reference");
4251 if (current_part_dimension
|| seen_part_dimension
)
4253 if (ref
->u
.c
.component
->attr
.pointer
)
4255 gfc_error ("Component to the right of a part reference "
4256 "with nonzero rank must not have the POINTER "
4257 "attribute at %L", &expr
->where
);
4260 else if (ref
->u
.c
.component
->attr
.allocatable
)
4262 gfc_error ("Component to the right of a part reference "
4263 "with nonzero rank must not have the ALLOCATABLE "
4264 "attribute at %L", &expr
->where
);
4276 if (((ref
->type
== REF_COMPONENT
&& n_components
> 1)
4277 || ref
->next
== NULL
)
4278 && current_part_dimension
4279 && seen_part_dimension
)
4281 gfc_error ("Two or more part references with nonzero rank must "
4282 "not be specified at %L", &expr
->where
);
4286 if (ref
->type
== REF_COMPONENT
)
4288 if (current_part_dimension
)
4289 seen_part_dimension
= 1;
4291 /* reset to make sure */
4292 current_part_dimension
= 0;
4300 /* Given an expression, determine its shape. This is easier than it sounds.
4301 Leaves the shape array NULL if it is not possible to determine the shape. */
4304 expression_shape (gfc_expr
*e
)
4306 mpz_t array
[GFC_MAX_DIMENSIONS
];
4309 if (e
->rank
== 0 || e
->shape
!= NULL
)
4312 for (i
= 0; i
< e
->rank
; i
++)
4313 if (gfc_array_dimen_size (e
, i
, &array
[i
]) == FAILURE
)
4316 e
->shape
= gfc_get_shape (e
->rank
);
4318 memcpy (e
->shape
, array
, e
->rank
* sizeof (mpz_t
));
4323 for (i
--; i
>= 0; i
--)
4324 mpz_clear (array
[i
]);
4328 /* Given a variable expression node, compute the rank of the expression by
4329 examining the base symbol and any reference structures it may have. */
4332 expression_rank (gfc_expr
*e
)
4337 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4338 could lead to serious confusion... */
4339 gcc_assert (e
->expr_type
!= EXPR_COMPCALL
);
4343 if (e
->expr_type
== EXPR_ARRAY
)
4345 /* Constructors can have a rank different from one via RESHAPE(). */
4347 if (e
->symtree
== NULL
)
4353 e
->rank
= (e
->symtree
->n
.sym
->as
== NULL
)
4354 ? 0 : e
->symtree
->n
.sym
->as
->rank
;
4360 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4362 if (ref
->type
!= REF_ARRAY
)
4365 if (ref
->u
.ar
.type
== AR_FULL
)
4367 rank
= ref
->u
.ar
.as
->rank
;
4371 if (ref
->u
.ar
.type
== AR_SECTION
)
4373 /* Figure out the rank of the section. */
4375 gfc_internal_error ("expression_rank(): Two array specs");
4377 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
4378 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
4379 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
4389 expression_shape (e
);
4393 /* Resolve a variable expression. */
4396 resolve_variable (gfc_expr
*e
)
4403 if (e
->symtree
== NULL
)
4406 if (e
->ref
&& resolve_ref (e
) == FAILURE
)
4409 sym
= e
->symtree
->n
.sym
;
4410 if (sym
->attr
.flavor
== FL_PROCEDURE
4411 && (!sym
->attr
.function
4412 || (sym
->attr
.function
&& sym
->result
4413 && sym
->result
->attr
.proc_pointer
4414 && !sym
->result
->attr
.function
)))
4416 e
->ts
.type
= BT_PROCEDURE
;
4417 goto resolve_procedure
;
4420 if (sym
->ts
.type
!= BT_UNKNOWN
)
4421 gfc_variable_attr (e
, &e
->ts
);
4424 /* Must be a simple variable reference. */
4425 if (gfc_set_default_type (sym
, 1, sym
->ns
) == FAILURE
)
4430 if (check_assumed_size_reference (sym
, e
))
4433 /* Deal with forward references to entries during resolve_code, to
4434 satisfy, at least partially, 12.5.2.5. */
4435 if (gfc_current_ns
->entries
4436 && current_entry_id
== sym
->entry_id
4439 && cs_base
->current
->op
!= EXEC_ENTRY
)
4441 gfc_entry_list
*entry
;
4442 gfc_formal_arglist
*formal
;
4446 /* If the symbol is a dummy... */
4447 if (sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
)
4449 entry
= gfc_current_ns
->entries
;
4452 /* ...test if the symbol is a parameter of previous entries. */
4453 for (; entry
&& entry
->id
<= current_entry_id
; entry
= entry
->next
)
4454 for (formal
= entry
->sym
->formal
; formal
; formal
= formal
->next
)
4456 if (formal
->sym
&& sym
->name
== formal
->sym
->name
)
4460 /* If it has not been seen as a dummy, this is an error. */
4463 if (specification_expr
)
4464 gfc_error ("Variable '%s', used in a specification expression"
4465 ", is referenced at %L before the ENTRY statement "
4466 "in which it is a parameter",
4467 sym
->name
, &cs_base
->current
->loc
);
4469 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4470 "statement in which it is a parameter",
4471 sym
->name
, &cs_base
->current
->loc
);
4476 /* Now do the same check on the specification expressions. */
4477 specification_expr
= 1;
4478 if (sym
->ts
.type
== BT_CHARACTER
4479 && gfc_resolve_expr (sym
->ts
.u
.cl
->length
) == FAILURE
)
4483 for (n
= 0; n
< sym
->as
->rank
; n
++)
4485 specification_expr
= 1;
4486 if (gfc_resolve_expr (sym
->as
->lower
[n
]) == FAILURE
)
4488 specification_expr
= 1;
4489 if (gfc_resolve_expr (sym
->as
->upper
[n
]) == FAILURE
)
4492 specification_expr
= 0;
4495 /* Update the symbol's entry level. */
4496 sym
->entry_id
= current_entry_id
+ 1;
4500 if (t
== SUCCESS
&& resolve_procedure_expression (e
) == FAILURE
)
4507 /* Checks to see that the correct symbol has been host associated.
4508 The only situation where this arises is that in which a twice
4509 contained function is parsed after the host association is made.
4510 Therefore, on detecting this, change the symbol in the expression
4511 and convert the array reference into an actual arglist if the old
4512 symbol is a variable. */
4514 check_host_association (gfc_expr
*e
)
4516 gfc_symbol
*sym
, *old_sym
;
4520 gfc_actual_arglist
*arg
, *tail
= NULL
;
4521 bool retval
= e
->expr_type
== EXPR_FUNCTION
;
4523 /* If the expression is the result of substitution in
4524 interface.c(gfc_extend_expr) because there is no way in
4525 which the host association can be wrong. */
4526 if (e
->symtree
== NULL
4527 || e
->symtree
->n
.sym
== NULL
4528 || e
->user_operator
)
4531 old_sym
= e
->symtree
->n
.sym
;
4533 if (gfc_current_ns
->parent
4534 && old_sym
->ns
!= gfc_current_ns
)
4536 /* Use the 'USE' name so that renamed module symbols are
4537 correctly handled. */
4538 gfc_find_symbol (e
->symtree
->name
, gfc_current_ns
, 1, &sym
);
4540 if (sym
&& old_sym
!= sym
4541 && sym
->ts
.type
== old_sym
->ts
.type
4542 && sym
->attr
.flavor
== FL_PROCEDURE
4543 && sym
->attr
.contained
)
4545 /* Clear the shape, since it might not be valid. */
4546 if (e
->shape
!= NULL
)
4548 for (n
= 0; n
< e
->rank
; n
++)
4549 mpz_clear (e
->shape
[n
]);
4551 gfc_free (e
->shape
);
4554 /* Give the expression the right symtree! */
4555 gfc_find_sym_tree (e
->symtree
->name
, NULL
, 1, &st
);
4556 gcc_assert (st
!= NULL
);
4558 if (old_sym
->attr
.flavor
== FL_PROCEDURE
4559 || e
->expr_type
== EXPR_FUNCTION
)
4561 /* Original was function so point to the new symbol, since
4562 the actual argument list is already attached to the
4564 e
->value
.function
.esym
= NULL
;
4569 /* Original was variable so convert array references into
4570 an actual arglist. This does not need any checking now
4571 since gfc_resolve_function will take care of it. */
4572 e
->value
.function
.actual
= NULL
;
4573 e
->expr_type
= EXPR_FUNCTION
;
4576 /* Ambiguity will not arise if the array reference is not
4577 the last reference. */
4578 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4579 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
4582 gcc_assert (ref
->type
== REF_ARRAY
);
4584 /* Grab the start expressions from the array ref and
4585 copy them into actual arguments. */
4586 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
4588 arg
= gfc_get_actual_arglist ();
4589 arg
->expr
= gfc_copy_expr (ref
->u
.ar
.start
[n
]);
4590 if (e
->value
.function
.actual
== NULL
)
4591 tail
= e
->value
.function
.actual
= arg
;
4599 /* Dump the reference list and set the rank. */
4600 gfc_free_ref_list (e
->ref
);
4602 e
->rank
= sym
->as
? sym
->as
->rank
: 0;
4605 gfc_resolve_expr (e
);
4609 /* This might have changed! */
4610 return e
->expr_type
== EXPR_FUNCTION
;
4615 gfc_resolve_character_operator (gfc_expr
*e
)
4617 gfc_expr
*op1
= e
->value
.op
.op1
;
4618 gfc_expr
*op2
= e
->value
.op
.op2
;
4619 gfc_expr
*e1
= NULL
;
4620 gfc_expr
*e2
= NULL
;
4622 gcc_assert (e
->value
.op
.op
== INTRINSIC_CONCAT
);
4624 if (op1
->ts
.u
.cl
&& op1
->ts
.u
.cl
->length
)
4625 e1
= gfc_copy_expr (op1
->ts
.u
.cl
->length
);
4626 else if (op1
->expr_type
== EXPR_CONSTANT
)
4627 e1
= gfc_int_expr (op1
->value
.character
.length
);
4629 if (op2
->ts
.u
.cl
&& op2
->ts
.u
.cl
->length
)
4630 e2
= gfc_copy_expr (op2
->ts
.u
.cl
->length
);
4631 else if (op2
->expr_type
== EXPR_CONSTANT
)
4632 e2
= gfc_int_expr (op2
->value
.character
.length
);
4634 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4639 e
->ts
.u
.cl
->length
= gfc_add (e1
, e2
);
4640 e
->ts
.u
.cl
->length
->ts
.type
= BT_INTEGER
;
4641 e
->ts
.u
.cl
->length
->ts
.kind
= gfc_charlen_int_kind
;
4642 gfc_simplify_expr (e
->ts
.u
.cl
->length
, 0);
4643 gfc_resolve_expr (e
->ts
.u
.cl
->length
);
4649 /* Ensure that an character expression has a charlen and, if possible, a
4650 length expression. */
4653 fixup_charlen (gfc_expr
*e
)
4655 /* The cases fall through so that changes in expression type and the need
4656 for multiple fixes are picked up. In all circumstances, a charlen should
4657 be available for the middle end to hang a backend_decl on. */
4658 switch (e
->expr_type
)
4661 gfc_resolve_character_operator (e
);
4664 if (e
->expr_type
== EXPR_ARRAY
)
4665 gfc_resolve_character_array_constructor (e
);
4667 case EXPR_SUBSTRING
:
4668 if (!e
->ts
.u
.cl
&& e
->ref
)
4669 gfc_resolve_substring_charlen (e
);
4673 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4680 /* Update an actual argument to include the passed-object for type-bound
4681 procedures at the right position. */
4683 static gfc_actual_arglist
*
4684 update_arglist_pass (gfc_actual_arglist
* lst
, gfc_expr
* po
, unsigned argpos
,
4687 gcc_assert (argpos
> 0);
4691 gfc_actual_arglist
* result
;
4693 result
= gfc_get_actual_arglist ();
4697 result
->name
= name
;
4703 lst
->next
= update_arglist_pass (lst
->next
, po
, argpos
- 1, name
);
4705 lst
= update_arglist_pass (NULL
, po
, argpos
- 1, name
);
4710 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4713 extract_compcall_passed_object (gfc_expr
* e
)
4717 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4719 if (e
->value
.compcall
.base_object
)
4720 po
= gfc_copy_expr (e
->value
.compcall
.base_object
);
4723 po
= gfc_get_expr ();
4724 po
->expr_type
= EXPR_VARIABLE
;
4725 po
->symtree
= e
->symtree
;
4726 po
->ref
= gfc_copy_ref (e
->ref
);
4729 if (gfc_resolve_expr (po
) == FAILURE
)
4736 /* Update the arglist of an EXPR_COMPCALL expression to include the
4740 update_compcall_arglist (gfc_expr
* e
)
4743 gfc_typebound_proc
* tbp
;
4745 tbp
= e
->value
.compcall
.tbp
;
4750 po
= extract_compcall_passed_object (e
);
4756 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
4760 if (tbp
->nopass
|| e
->value
.compcall
.ignore_pass
)
4766 gcc_assert (tbp
->pass_arg_num
> 0);
4767 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
4775 /* Extract the passed object from a PPC call (a copy of it). */
4778 extract_ppc_passed_object (gfc_expr
*e
)
4783 po
= gfc_get_expr ();
4784 po
->expr_type
= EXPR_VARIABLE
;
4785 po
->symtree
= e
->symtree
;
4786 po
->ref
= gfc_copy_ref (e
->ref
);
4788 /* Remove PPC reference. */
4790 while ((*ref
)->next
)
4791 (*ref
) = (*ref
)->next
;
4792 gfc_free_ref_list (*ref
);
4795 if (gfc_resolve_expr (po
) == FAILURE
)
4802 /* Update the actual arglist of a procedure pointer component to include the
4806 update_ppc_arglist (gfc_expr
* e
)
4810 gfc_typebound_proc
* tb
;
4812 if (!gfc_is_proc_ptr_comp (e
, &ppc
))
4819 else if (tb
->nopass
)
4822 po
= extract_ppc_passed_object (e
);
4828 gfc_error ("Passed-object at %L must be scalar", &e
->where
);
4832 gcc_assert (tb
->pass_arg_num
> 0);
4833 e
->value
.compcall
.actual
= update_arglist_pass (e
->value
.compcall
.actual
, po
,
4841 /* Check that the object a TBP is called on is valid, i.e. it must not be
4842 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4845 check_typebound_baseobject (gfc_expr
* e
)
4849 base
= extract_compcall_passed_object (e
);
4853 gcc_assert (base
->ts
.type
== BT_DERIVED
|| base
->ts
.type
== BT_CLASS
);
4855 if (base
->ts
.type
== BT_DERIVED
&& base
->ts
.u
.derived
->attr
.abstract
)
4857 gfc_error ("Base object for type-bound procedure call at %L is of"
4858 " ABSTRACT type '%s'", &e
->where
, base
->ts
.u
.derived
->name
);
4866 /* Resolve a call to a type-bound procedure, either function or subroutine,
4867 statically from the data in an EXPR_COMPCALL expression. The adapted
4868 arglist and the target-procedure symtree are returned. */
4871 resolve_typebound_static (gfc_expr
* e
, gfc_symtree
** target
,
4872 gfc_actual_arglist
** actual
)
4874 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4875 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
4877 /* Update the actual arglist for PASS. */
4878 if (update_compcall_arglist (e
) == FAILURE
)
4881 *actual
= e
->value
.compcall
.actual
;
4882 *target
= e
->value
.compcall
.tbp
->u
.specific
;
4884 gfc_free_ref_list (e
->ref
);
4886 e
->value
.compcall
.actual
= NULL
;
4892 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4893 which of the specific bindings (if any) matches the arglist and transform
4894 the expression into a call of that binding. */
4897 resolve_typebound_generic_call (gfc_expr
* e
)
4899 gfc_typebound_proc
* genproc
;
4900 const char* genname
;
4902 gcc_assert (e
->expr_type
== EXPR_COMPCALL
);
4903 genname
= e
->value
.compcall
.name
;
4904 genproc
= e
->value
.compcall
.tbp
;
4906 if (!genproc
->is_generic
)
4909 /* Try the bindings on this type and in the inheritance hierarchy. */
4910 for (; genproc
; genproc
= genproc
->overridden
)
4914 gcc_assert (genproc
->is_generic
);
4915 for (g
= genproc
->u
.generic
; g
; g
= g
->next
)
4918 gfc_actual_arglist
* args
;
4921 gcc_assert (g
->specific
);
4923 if (g
->specific
->error
)
4926 target
= g
->specific
->u
.specific
->n
.sym
;
4928 /* Get the right arglist by handling PASS/NOPASS. */
4929 args
= gfc_copy_actual_arglist (e
->value
.compcall
.actual
);
4930 if (!g
->specific
->nopass
)
4933 po
= extract_compcall_passed_object (e
);
4937 gcc_assert (g
->specific
->pass_arg_num
> 0);
4938 gcc_assert (!g
->specific
->error
);
4939 args
= update_arglist_pass (args
, po
, g
->specific
->pass_arg_num
,
4940 g
->specific
->pass_arg
);
4942 resolve_actual_arglist (args
, target
->attr
.proc
,
4943 is_external_proc (target
) && !target
->formal
);
4945 /* Check if this arglist matches the formal. */
4946 matches
= gfc_arglist_matches_symbol (&args
, target
);
4948 /* Clean up and break out of the loop if we've found it. */
4949 gfc_free_actual_arglist (args
);
4952 e
->value
.compcall
.tbp
= g
->specific
;
4958 /* Nothing matching found! */
4959 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4960 " '%s' at %L", genname
, &e
->where
);
4968 /* Resolve a call to a type-bound subroutine. */
4971 resolve_typebound_call (gfc_code
* c
)
4973 gfc_actual_arglist
* newactual
;
4974 gfc_symtree
* target
;
4976 /* Check that's really a SUBROUTINE. */
4977 if (!c
->expr1
->value
.compcall
.tbp
->subroutine
)
4979 gfc_error ("'%s' at %L should be a SUBROUTINE",
4980 c
->expr1
->value
.compcall
.name
, &c
->loc
);
4984 if (check_typebound_baseobject (c
->expr1
) == FAILURE
)
4987 if (resolve_typebound_generic_call (c
->expr1
) == FAILURE
)
4990 /* Transform into an ordinary EXEC_CALL for now. */
4992 if (resolve_typebound_static (c
->expr1
, &target
, &newactual
) == FAILURE
)
4995 c
->ext
.actual
= newactual
;
4996 c
->symtree
= target
;
4997 c
->op
= (c
->expr1
->value
.compcall
.assign
? EXEC_ASSIGN_CALL
: EXEC_CALL
);
4999 gcc_assert (!c
->expr1
->ref
&& !c
->expr1
->value
.compcall
.actual
);
5000 gfc_free_expr (c
->expr1
);
5003 return resolve_call (c
);
5007 /* Resolve a component-call expression. */
5010 resolve_compcall (gfc_expr
* e
)
5012 gfc_actual_arglist
* newactual
;
5013 gfc_symtree
* target
;
5015 /* Check that's really a FUNCTION. */
5016 if (!e
->value
.compcall
.tbp
->function
)
5018 gfc_error ("'%s' at %L should be a FUNCTION",
5019 e
->value
.compcall
.name
, &e
->where
);
5023 /* These must not be assign-calls! */
5024 gcc_assert (!e
->value
.compcall
.assign
);
5026 if (check_typebound_baseobject (e
) == FAILURE
)
5029 if (resolve_typebound_generic_call (e
) == FAILURE
)
5031 gcc_assert (!e
->value
.compcall
.tbp
->is_generic
);
5033 /* Take the rank from the function's symbol. */
5034 if (e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
)
5035 e
->rank
= e
->value
.compcall
.tbp
->u
.specific
->n
.sym
->as
->rank
;
5037 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5038 arglist to the TBP's binding target. */
5040 if (resolve_typebound_static (e
, &target
, &newactual
) == FAILURE
)
5043 e
->value
.function
.actual
= newactual
;
5044 e
->value
.function
.name
= e
->value
.compcall
.name
;
5045 e
->value
.function
.esym
= target
->n
.sym
;
5046 e
->value
.function
.isym
= NULL
;
5047 e
->symtree
= target
;
5048 e
->ts
= target
->n
.sym
->ts
;
5049 e
->expr_type
= EXPR_FUNCTION
;
5051 return gfc_resolve_expr (e
);
5055 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5058 resolve_ppc_call (gfc_code
* c
)
5060 gfc_component
*comp
;
5063 b
= gfc_is_proc_ptr_comp (c
->expr1
, &comp
);
5066 c
->resolved_sym
= c
->expr1
->symtree
->n
.sym
;
5067 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5069 if (!comp
->attr
.subroutine
)
5070 gfc_add_subroutine (&comp
->attr
, comp
->name
, &c
->expr1
->where
);
5072 if (resolve_ref (c
->expr1
) == FAILURE
)
5075 if (update_ppc_arglist (c
->expr1
) == FAILURE
)
5078 c
->ext
.actual
= c
->expr1
->value
.compcall
.actual
;
5080 if (resolve_actual_arglist (c
->ext
.actual
, comp
->attr
.proc
,
5081 comp
->formal
== NULL
) == FAILURE
)
5084 gfc_ppc_use (comp
, &c
->expr1
->value
.compcall
.actual
, &c
->expr1
->where
);
5090 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5093 resolve_expr_ppc (gfc_expr
* e
)
5095 gfc_component
*comp
;
5098 b
= gfc_is_proc_ptr_comp (e
, &comp
);
5101 /* Convert to EXPR_FUNCTION. */
5102 e
->expr_type
= EXPR_FUNCTION
;
5103 e
->value
.function
.isym
= NULL
;
5104 e
->value
.function
.actual
= e
->value
.compcall
.actual
;
5106 if (comp
->as
!= NULL
)
5107 e
->rank
= comp
->as
->rank
;
5109 if (!comp
->attr
.function
)
5110 gfc_add_function (&comp
->attr
, comp
->name
, &e
->where
);
5112 if (resolve_ref (e
) == FAILURE
)
5115 if (resolve_actual_arglist (e
->value
.function
.actual
, comp
->attr
.proc
,
5116 comp
->formal
== NULL
) == FAILURE
)
5119 if (update_ppc_arglist (e
) == FAILURE
)
5122 gfc_ppc_use (comp
, &e
->value
.compcall
.actual
, &e
->where
);
5128 /* Resolve an expression. That is, make sure that types of operands agree
5129 with their operators, intrinsic operators are converted to function calls
5130 for overloaded types and unresolved function references are resolved. */
5133 gfc_resolve_expr (gfc_expr
*e
)
5140 switch (e
->expr_type
)
5143 t
= resolve_operator (e
);
5149 if (check_host_association (e
))
5150 t
= resolve_function (e
);
5153 t
= resolve_variable (e
);
5155 expression_rank (e
);
5158 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
&& e
->ref
5159 && e
->ref
->type
!= REF_SUBSTRING
)
5160 gfc_resolve_substring_charlen (e
);
5165 t
= resolve_compcall (e
);
5168 case EXPR_SUBSTRING
:
5169 t
= resolve_ref (e
);
5178 t
= resolve_expr_ppc (e
);
5183 if (resolve_ref (e
) == FAILURE
)
5186 t
= gfc_resolve_array_constructor (e
);
5187 /* Also try to expand a constructor. */
5190 expression_rank (e
);
5191 gfc_expand_constructor (e
);
5194 /* This provides the opportunity for the length of constructors with
5195 character valued function elements to propagate the string length
5196 to the expression. */
5197 if (t
== SUCCESS
&& e
->ts
.type
== BT_CHARACTER
)
5198 t
= gfc_resolve_character_array_constructor (e
);
5202 case EXPR_STRUCTURE
:
5203 t
= resolve_ref (e
);
5207 t
= resolve_structure_cons (e
);
5211 t
= gfc_simplify_expr (e
, 0);
5215 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5218 if (e
->ts
.type
== BT_CHARACTER
&& t
== SUCCESS
&& !e
->ts
.u
.cl
)
5225 /* Resolve an expression from an iterator. They must be scalar and have
5226 INTEGER or (optionally) REAL type. */
5229 gfc_resolve_iterator_expr (gfc_expr
*expr
, bool real_ok
,
5230 const char *name_msgid
)
5232 if (gfc_resolve_expr (expr
) == FAILURE
)
5235 if (expr
->rank
!= 0)
5237 gfc_error ("%s at %L must be a scalar", _(name_msgid
), &expr
->where
);
5241 if (expr
->ts
.type
!= BT_INTEGER
)
5243 if (expr
->ts
.type
== BT_REAL
)
5246 return gfc_notify_std (GFC_STD_F95_DEL
,
5247 "Deleted feature: %s at %L must be integer",
5248 _(name_msgid
), &expr
->where
);
5251 gfc_error ("%s at %L must be INTEGER", _(name_msgid
),
5258 gfc_error ("%s at %L must be INTEGER", _(name_msgid
), &expr
->where
);
5266 /* Resolve the expressions in an iterator structure. If REAL_OK is
5267 false allow only INTEGER type iterators, otherwise allow REAL types. */
5270 gfc_resolve_iterator (gfc_iterator
*iter
, bool real_ok
)
5272 if (gfc_resolve_iterator_expr (iter
->var
, real_ok
, "Loop variable")
5276 if (gfc_pure (NULL
) && gfc_impure_variable (iter
->var
->symtree
->n
.sym
))
5278 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5283 if (gfc_resolve_iterator_expr (iter
->start
, real_ok
,
5284 "Start expression in DO loop") == FAILURE
)
5287 if (gfc_resolve_iterator_expr (iter
->end
, real_ok
,
5288 "End expression in DO loop") == FAILURE
)
5291 if (gfc_resolve_iterator_expr (iter
->step
, real_ok
,
5292 "Step expression in DO loop") == FAILURE
)
5295 if (iter
->step
->expr_type
== EXPR_CONSTANT
)
5297 if ((iter
->step
->ts
.type
== BT_INTEGER
5298 && mpz_cmp_ui (iter
->step
->value
.integer
, 0) == 0)
5299 || (iter
->step
->ts
.type
== BT_REAL
5300 && mpfr_sgn (iter
->step
->value
.real
) == 0))
5302 gfc_error ("Step expression in DO loop at %L cannot be zero",
5303 &iter
->step
->where
);
5308 /* Convert start, end, and step to the same type as var. */
5309 if (iter
->start
->ts
.kind
!= iter
->var
->ts
.kind
5310 || iter
->start
->ts
.type
!= iter
->var
->ts
.type
)
5311 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
5313 if (iter
->end
->ts
.kind
!= iter
->var
->ts
.kind
5314 || iter
->end
->ts
.type
!= iter
->var
->ts
.type
)
5315 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
5317 if (iter
->step
->ts
.kind
!= iter
->var
->ts
.kind
5318 || iter
->step
->ts
.type
!= iter
->var
->ts
.type
)
5319 gfc_convert_type (iter
->step
, &iter
->var
->ts
, 2);
5321 if (iter
->start
->expr_type
== EXPR_CONSTANT
5322 && iter
->end
->expr_type
== EXPR_CONSTANT
5323 && iter
->step
->expr_type
== EXPR_CONSTANT
)
5326 if (iter
->start
->ts
.type
== BT_INTEGER
)
5328 sgn
= mpz_cmp_ui (iter
->step
->value
.integer
, 0);
5329 cmp
= mpz_cmp (iter
->end
->value
.integer
, iter
->start
->value
.integer
);
5333 sgn
= mpfr_sgn (iter
->step
->value
.real
);
5334 cmp
= mpfr_cmp (iter
->end
->value
.real
, iter
->start
->value
.real
);
5336 if ((sgn
> 0 && cmp
< 0) || (sgn
< 0 && cmp
> 0))
5337 gfc_warning ("DO loop at %L will be executed zero times",
5338 &iter
->step
->where
);
5345 /* Traversal function for find_forall_index. f == 2 signals that
5346 that variable itself is not to be checked - only the references. */
5349 forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int *f
)
5351 if (expr
->expr_type
!= EXPR_VARIABLE
)
5354 /* A scalar assignment */
5355 if (!expr
->ref
|| *f
== 1)
5357 if (expr
->symtree
->n
.sym
== sym
)
5369 /* Check whether the FORALL index appears in the expression or not.
5370 Returns SUCCESS if SYM is found in EXPR. */
5373 find_forall_index (gfc_expr
*expr
, gfc_symbol
*sym
, int f
)
5375 if (gfc_traverse_expr (expr
, sym
, forall_index
, f
))
5382 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5383 to be a scalar INTEGER variable. The subscripts and stride are scalar
5384 INTEGERs, and if stride is a constant it must be nonzero.
5385 Furthermore "A subscript or stride in a forall-triplet-spec shall
5386 not contain a reference to any index-name in the
5387 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5390 resolve_forall_iterators (gfc_forall_iterator
*it
)
5392 gfc_forall_iterator
*iter
, *iter2
;
5394 for (iter
= it
; iter
; iter
= iter
->next
)
5396 if (gfc_resolve_expr (iter
->var
) == SUCCESS
5397 && (iter
->var
->ts
.type
!= BT_INTEGER
|| iter
->var
->rank
!= 0))
5398 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5401 if (gfc_resolve_expr (iter
->start
) == SUCCESS
5402 && (iter
->start
->ts
.type
!= BT_INTEGER
|| iter
->start
->rank
!= 0))
5403 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5404 &iter
->start
->where
);
5405 if (iter
->var
->ts
.kind
!= iter
->start
->ts
.kind
)
5406 gfc_convert_type (iter
->start
, &iter
->var
->ts
, 2);
5408 if (gfc_resolve_expr (iter
->end
) == SUCCESS
5409 && (iter
->end
->ts
.type
!= BT_INTEGER
|| iter
->end
->rank
!= 0))
5410 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5412 if (iter
->var
->ts
.kind
!= iter
->end
->ts
.kind
)
5413 gfc_convert_type (iter
->end
, &iter
->var
->ts
, 2);
5415 if (gfc_resolve_expr (iter
->stride
) == SUCCESS
)
5417 if (iter
->stride
->ts
.type
!= BT_INTEGER
|| iter
->stride
->rank
!= 0)
5418 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5419 &iter
->stride
->where
, "INTEGER");
5421 if (iter
->stride
->expr_type
== EXPR_CONSTANT
5422 && mpz_cmp_ui(iter
->stride
->value
.integer
, 0) == 0)
5423 gfc_error ("FORALL stride expression at %L cannot be zero",
5424 &iter
->stride
->where
);
5426 if (iter
->var
->ts
.kind
!= iter
->stride
->ts
.kind
)
5427 gfc_convert_type (iter
->stride
, &iter
->var
->ts
, 2);
5430 for (iter
= it
; iter
; iter
= iter
->next
)
5431 for (iter2
= iter
; iter2
; iter2
= iter2
->next
)
5433 if (find_forall_index (iter2
->start
,
5434 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
5435 || find_forall_index (iter2
->end
,
5436 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
5437 || find_forall_index (iter2
->stride
,
5438 iter
->var
->symtree
->n
.sym
, 0) == SUCCESS
)
5439 gfc_error ("FORALL index '%s' may not appear in triplet "
5440 "specification at %L", iter
->var
->symtree
->name
,
5441 &iter2
->start
->where
);
5446 /* Given a pointer to a symbol that is a derived type, see if it's
5447 inaccessible, i.e. if it's defined in another module and the components are
5448 PRIVATE. The search is recursive if necessary. Returns zero if no
5449 inaccessible components are found, nonzero otherwise. */
5452 derived_inaccessible (gfc_symbol
*sym
)
5456 if (sym
->attr
.use_assoc
&& sym
->attr
.private_comp
)
5459 for (c
= sym
->components
; c
; c
= c
->next
)
5461 if (c
->ts
.type
== BT_DERIVED
&& derived_inaccessible (c
->ts
.u
.derived
))
5469 /* Resolve the argument of a deallocate expression. The expression must be
5470 a pointer or a full array. */
5473 resolve_deallocate_expr (gfc_expr
*e
)
5475 symbol_attribute attr
;
5476 int allocatable
, pointer
, check_intent_in
;
5481 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5482 check_intent_in
= 1;
5484 if (gfc_resolve_expr (e
) == FAILURE
)
5487 if (e
->expr_type
!= EXPR_VARIABLE
)
5490 sym
= e
->symtree
->n
.sym
;
5492 if (sym
->ts
.type
== BT_CLASS
)
5494 allocatable
= sym
->ts
.u
.derived
->components
->attr
.allocatable
;
5495 pointer
= sym
->ts
.u
.derived
->components
->attr
.pointer
;
5499 allocatable
= sym
->attr
.allocatable
;
5500 pointer
= sym
->attr
.pointer
;
5502 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5505 check_intent_in
= 0;
5510 if (ref
->u
.ar
.type
!= AR_FULL
)
5515 c
= ref
->u
.c
.component
;
5516 if (c
->ts
.type
== BT_CLASS
)
5518 allocatable
= c
->ts
.u
.derived
->components
->attr
.allocatable
;
5519 pointer
= c
->ts
.u
.derived
->components
->attr
.pointer
;
5523 allocatable
= c
->attr
.allocatable
;
5524 pointer
= c
->attr
.pointer
;
5534 attr
= gfc_expr_attr (e
);
5536 if (allocatable
== 0 && attr
.pointer
== 0)
5539 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5543 if (check_intent_in
&& sym
->attr
.intent
== INTENT_IN
)
5545 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5546 sym
->name
, &e
->where
);
5550 if (e
->ts
.type
== BT_CLASS
)
5552 /* Only deallocate the DATA component. */
5553 gfc_add_component_ref (e
, "$data");
5560 /* Returns true if the expression e contains a reference to the symbol sym. */
5562 sym_in_expr (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5564 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
->n
.sym
== sym
)
5571 gfc_find_sym_in_expr (gfc_symbol
*sym
, gfc_expr
*e
)
5573 return gfc_traverse_expr (e
, sym
, sym_in_expr
, 0);
5577 /* Given the expression node e for an allocatable/pointer of derived type to be
5578 allocated, get the expression node to be initialized afterwards (needed for
5579 derived types with default initializers, and derived types with allocatable
5580 components that need nullification.) */
5583 gfc_expr_to_initialize (gfc_expr
*e
)
5589 result
= gfc_copy_expr (e
);
5591 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5592 for (ref
= result
->ref
; ref
; ref
= ref
->next
)
5593 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
5595 ref
->u
.ar
.type
= AR_FULL
;
5597 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
5598 ref
->u
.ar
.start
[i
] = ref
->u
.ar
.end
[i
] = ref
->u
.ar
.stride
[i
] = NULL
;
5600 result
->rank
= ref
->u
.ar
.dimen
;
5608 /* Resolve the expression in an ALLOCATE statement, doing the additional
5609 checks to see whether the expression is OK or not. The expression must
5610 have a trailing array reference that gives the size of the array. */
5613 resolve_allocate_expr (gfc_expr
*e
, gfc_code
*code
)
5615 int i
, pointer
, allocatable
, dimension
, check_intent_in
;
5616 symbol_attribute attr
;
5617 gfc_ref
*ref
, *ref2
;
5624 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5625 check_intent_in
= 1;
5627 if (gfc_resolve_expr (e
) == FAILURE
)
5630 /* Make sure the expression is allocatable or a pointer. If it is
5631 pointer, the next-to-last reference must be a pointer. */
5635 sym
= e
->symtree
->n
.sym
;
5637 if (e
->expr_type
!= EXPR_VARIABLE
)
5640 attr
= gfc_expr_attr (e
);
5641 pointer
= attr
.pointer
;
5642 dimension
= attr
.dimension
;
5646 if (sym
->ts
.type
== BT_CLASS
)
5648 allocatable
= sym
->ts
.u
.derived
->components
->attr
.allocatable
;
5649 pointer
= sym
->ts
.u
.derived
->components
->attr
.pointer
;
5650 dimension
= sym
->ts
.u
.derived
->components
->attr
.dimension
;
5654 allocatable
= sym
->attr
.allocatable
;
5655 pointer
= sym
->attr
.pointer
;
5656 dimension
= sym
->attr
.dimension
;
5659 for (ref
= e
->ref
; ref
; ref2
= ref
, ref
= ref
->next
)
5662 check_intent_in
= 0;
5667 if (ref
->next
!= NULL
)
5672 c
= ref
->u
.c
.component
;
5673 if (c
->ts
.type
== BT_CLASS
)
5675 allocatable
= c
->ts
.u
.derived
->components
->attr
.allocatable
;
5676 pointer
= c
->ts
.u
.derived
->components
->attr
.pointer
;
5677 dimension
= c
->ts
.u
.derived
->components
->attr
.dimension
;
5681 allocatable
= c
->attr
.allocatable
;
5682 pointer
= c
->attr
.pointer
;
5683 dimension
= c
->attr
.dimension
;
5695 if (allocatable
== 0 && pointer
== 0)
5697 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5702 if (check_intent_in
&& sym
->attr
.intent
== INTENT_IN
)
5704 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
5705 sym
->name
, &e
->where
);
5709 if (e
->ts
.type
== BT_CLASS
)
5711 /* Initialize VINDEX for CLASS objects. */
5712 init_st
= gfc_get_code ();
5713 init_st
->loc
= code
->loc
;
5714 init_st
->expr1
= gfc_expr_to_initialize (e
);
5715 init_st
->op
= EXEC_ASSIGN
;
5716 gfc_add_component_ref (init_st
->expr1
, "$vindex");
5717 if (code
->expr3
&& code
->expr3
->ts
.type
== BT_CLASS
)
5719 /* vindex must be determined at run time. */
5720 init_st
->expr2
= gfc_copy_expr (code
->expr3
);
5721 gfc_add_component_ref (init_st
->expr2
, "$vindex");
5725 /* vindex is fixed at compile time. */
5728 vindex
= code
->expr3
->ts
.u
.derived
->vindex
;
5729 else if (code
->ext
.alloc
.ts
.type
== BT_DERIVED
)
5730 vindex
= code
->ext
.alloc
.ts
.u
.derived
->vindex
;
5731 else if (e
->ts
.type
== BT_CLASS
)
5732 vindex
= e
->ts
.u
.derived
->components
->ts
.u
.derived
->vindex
;
5734 vindex
= e
->ts
.u
.derived
->vindex
;
5735 init_st
->expr2
= gfc_int_expr (vindex
);
5737 init_st
->expr2
->where
= init_st
->expr1
->where
= init_st
->loc
;
5738 init_st
->next
= code
->next
;
5739 code
->next
= init_st
;
5740 /* Only allocate the DATA component. */
5741 gfc_add_component_ref (e
, "$data");
5744 if (pointer
|| dimension
== 0)
5747 /* Make sure the next-to-last reference node is an array specification. */
5749 if (ref2
== NULL
|| ref2
->type
!= REF_ARRAY
|| ref2
->u
.ar
.type
== AR_FULL
)
5751 gfc_error ("Array specification required in ALLOCATE statement "
5752 "at %L", &e
->where
);
5756 /* Make sure that the array section reference makes sense in the
5757 context of an ALLOCATE specification. */
5761 for (i
= 0; i
< ar
->dimen
; i
++)
5763 if (ref2
->u
.ar
.type
== AR_ELEMENT
)
5766 switch (ar
->dimen_type
[i
])
5772 if (ar
->start
[i
] != NULL
5773 && ar
->end
[i
] != NULL
5774 && ar
->stride
[i
] == NULL
)
5777 /* Fall Through... */
5781 gfc_error ("Bad array specification in ALLOCATE statement at %L",
5788 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
5790 sym
= a
->expr
->symtree
->n
.sym
;
5792 /* TODO - check derived type components. */
5793 if (sym
->ts
.type
== BT_DERIVED
)
5796 if ((ar
->start
[i
] != NULL
5797 && gfc_find_sym_in_expr (sym
, ar
->start
[i
]))
5798 || (ar
->end
[i
] != NULL
5799 && gfc_find_sym_in_expr (sym
, ar
->end
[i
])))
5801 gfc_error ("'%s' must not appear in the array specification at "
5802 "%L in the same ALLOCATE statement where it is "
5803 "itself allocated", sym
->name
, &ar
->where
);
5813 resolve_allocate_deallocate (gfc_code
*code
, const char *fcn
)
5815 gfc_expr
*stat
, *errmsg
, *pe
, *qe
;
5816 gfc_alloc
*a
, *p
, *q
;
5818 stat
= code
->expr1
? code
->expr1
: NULL
;
5820 errmsg
= code
->expr2
? code
->expr2
: NULL
;
5822 /* Check the stat variable. */
5825 if (stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
5826 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
5827 stat
->symtree
->n
.sym
->name
, &stat
->where
);
5829 if (gfc_pure (NULL
) && gfc_impure_variable (stat
->symtree
->n
.sym
))
5830 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
5833 if ((stat
->ts
.type
!= BT_INTEGER
5834 && !(stat
->ref
&& (stat
->ref
->type
== REF_ARRAY
5835 || stat
->ref
->type
== REF_COMPONENT
)))
5837 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
5838 "variable", &stat
->where
);
5840 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
5841 if (p
->expr
->symtree
->n
.sym
->name
== stat
->symtree
->n
.sym
->name
)
5842 gfc_error ("Stat-variable at %L shall not be %sd within "
5843 "the same %s statement", &stat
->where
, fcn
, fcn
);
5846 /* Check the errmsg variable. */
5850 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
5853 if (errmsg
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
5854 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
5855 errmsg
->symtree
->n
.sym
->name
, &errmsg
->where
);
5857 if (gfc_pure (NULL
) && gfc_impure_variable (errmsg
->symtree
->n
.sym
))
5858 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
5861 if ((errmsg
->ts
.type
!= BT_CHARACTER
5863 && (errmsg
->ref
->type
== REF_ARRAY
5864 || errmsg
->ref
->type
== REF_COMPONENT
)))
5865 || errmsg
->rank
> 0 )
5866 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
5867 "variable", &errmsg
->where
);
5869 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
5870 if (p
->expr
->symtree
->n
.sym
->name
== errmsg
->symtree
->n
.sym
->name
)
5871 gfc_error ("Errmsg-variable at %L shall not be %sd within "
5872 "the same %s statement", &errmsg
->where
, fcn
, fcn
);
5875 /* Check that an allocate-object appears only once in the statement.
5876 FIXME: Checking derived types is disabled. */
5877 for (p
= code
->ext
.alloc
.list
; p
; p
= p
->next
)
5880 if ((pe
->ref
&& pe
->ref
->type
!= REF_COMPONENT
)
5881 && (pe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
))
5883 for (q
= p
->next
; q
; q
= q
->next
)
5886 if ((qe
->ref
&& qe
->ref
->type
!= REF_COMPONENT
)
5887 && (qe
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
)
5888 && (pe
->symtree
->n
.sym
->name
== qe
->symtree
->n
.sym
->name
))
5889 gfc_error ("Allocate-object at %L also appears at %L",
5890 &pe
->where
, &qe
->where
);
5895 if (strcmp (fcn
, "ALLOCATE") == 0)
5897 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
5898 resolve_allocate_expr (a
->expr
, code
);
5902 for (a
= code
->ext
.alloc
.list
; a
; a
= a
->next
)
5903 resolve_deallocate_expr (a
->expr
);
5908 /************ SELECT CASE resolution subroutines ************/
5910 /* Callback function for our mergesort variant. Determines interval
5911 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
5912 op1 > op2. Assumes we're not dealing with the default case.
5913 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
5914 There are nine situations to check. */
5917 compare_cases (const gfc_case
*op1
, const gfc_case
*op2
)
5921 if (op1
->low
== NULL
) /* op1 = (:L) */
5923 /* op2 = (:N), so overlap. */
5925 /* op2 = (M:) or (M:N), L < M */
5926 if (op2
->low
!= NULL
5927 && gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
5930 else if (op1
->high
== NULL
) /* op1 = (K:) */
5932 /* op2 = (M:), so overlap. */
5934 /* op2 = (:N) or (M:N), K > N */
5935 if (op2
->high
!= NULL
5936 && gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
5939 else /* op1 = (K:L) */
5941 if (op2
->low
== NULL
) /* op2 = (:N), K > N */
5942 retval
= (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
5944 else if (op2
->high
== NULL
) /* op2 = (M:), L < M */
5945 retval
= (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
5947 else /* op2 = (M:N) */
5951 if (gfc_compare_expr (op1
->high
, op2
->low
, INTRINSIC_LT
) < 0)
5954 else if (gfc_compare_expr (op1
->low
, op2
->high
, INTRINSIC_GT
) > 0)
5963 /* Merge-sort a double linked case list, detecting overlap in the
5964 process. LIST is the head of the double linked case list before it
5965 is sorted. Returns the head of the sorted list if we don't see any
5966 overlap, or NULL otherwise. */
5969 check_case_overlap (gfc_case
*list
)
5971 gfc_case
*p
, *q
, *e
, *tail
;
5972 int insize
, nmerges
, psize
, qsize
, cmp
, overlap_seen
;
5974 /* If the passed list was empty, return immediately. */
5981 /* Loop unconditionally. The only exit from this loop is a return
5982 statement, when we've finished sorting the case list. */
5989 /* Count the number of merges we do in this pass. */
5992 /* Loop while there exists a merge to be done. */
5997 /* Count this merge. */
6000 /* Cut the list in two pieces by stepping INSIZE places
6001 forward in the list, starting from P. */
6004 for (i
= 0; i
< insize
; i
++)
6013 /* Now we have two lists. Merge them! */
6014 while (psize
> 0 || (qsize
> 0 && q
!= NULL
))
6016 /* See from which the next case to merge comes from. */
6019 /* P is empty so the next case must come from Q. */
6024 else if (qsize
== 0 || q
== NULL
)
6033 cmp
= compare_cases (p
, q
);
6036 /* The whole case range for P is less than the
6044 /* The whole case range for Q is greater than
6045 the case range for P. */
6052 /* The cases overlap, or they are the same
6053 element in the list. Either way, we must
6054 issue an error and get the next case from P. */
6055 /* FIXME: Sort P and Q by line number. */
6056 gfc_error ("CASE label at %L overlaps with CASE "
6057 "label at %L", &p
->where
, &q
->where
);
6065 /* Add the next element to the merged list. */
6074 /* P has now stepped INSIZE places along, and so has Q. So
6075 they're the same. */
6080 /* If we have done only one merge or none at all, we've
6081 finished sorting the cases. */
6090 /* Otherwise repeat, merging lists twice the size. */
6096 /* Check to see if an expression is suitable for use in a CASE statement.
6097 Makes sure that all case expressions are scalar constants of the same
6098 type. Return FAILURE if anything is wrong. */
6101 validate_case_label_expr (gfc_expr
*e
, gfc_expr
*case_expr
)
6103 if (e
== NULL
) return SUCCESS
;
6105 if (e
->ts
.type
!= case_expr
->ts
.type
)
6107 gfc_error ("Expression in CASE statement at %L must be of type %s",
6108 &e
->where
, gfc_basic_typename (case_expr
->ts
.type
));
6112 /* C805 (R808) For a given case-construct, each case-value shall be of
6113 the same type as case-expr. For character type, length differences
6114 are allowed, but the kind type parameters shall be the same. */
6116 if (case_expr
->ts
.type
== BT_CHARACTER
&& e
->ts
.kind
!= case_expr
->ts
.kind
)
6118 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6119 &e
->where
, case_expr
->ts
.kind
);
6123 /* Convert the case value kind to that of case expression kind, if needed.
6124 FIXME: Should a warning be issued? */
6125 if (e
->ts
.kind
!= case_expr
->ts
.kind
)
6126 gfc_convert_type_warn (e
, &case_expr
->ts
, 2, 0);
6130 gfc_error ("Expression in CASE statement at %L must be scalar",
6139 /* Given a completely parsed select statement, we:
6141 - Validate all expressions and code within the SELECT.
6142 - Make sure that the selection expression is not of the wrong type.
6143 - Make sure that no case ranges overlap.
6144 - Eliminate unreachable cases and unreachable code resulting from
6145 removing case labels.
6147 The standard does allow unreachable cases, e.g. CASE (5:3). But
6148 they are a hassle for code generation, and to prevent that, we just
6149 cut them out here. This is not necessary for overlapping cases
6150 because they are illegal and we never even try to generate code.
6152 We have the additional caveat that a SELECT construct could have
6153 been a computed GOTO in the source code. Fortunately we can fairly
6154 easily work around that here: The case_expr for a "real" SELECT CASE
6155 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6156 we have to do is make sure that the case_expr is a scalar integer
6160 resolve_select (gfc_code
*code
)
6163 gfc_expr
*case_expr
;
6164 gfc_case
*cp
, *default_case
, *tail
, *head
;
6165 int seen_unreachable
;
6171 if (code
->expr1
== NULL
)
6173 /* This was actually a computed GOTO statement. */
6174 case_expr
= code
->expr2
;
6175 if (case_expr
->ts
.type
!= BT_INTEGER
|| case_expr
->rank
!= 0)
6176 gfc_error ("Selection expression in computed GOTO statement "
6177 "at %L must be a scalar integer expression",
6180 /* Further checking is not necessary because this SELECT was built
6181 by the compiler, so it should always be OK. Just move the
6182 case_expr from expr2 to expr so that we can handle computed
6183 GOTOs as normal SELECTs from here on. */
6184 code
->expr1
= code
->expr2
;
6189 case_expr
= code
->expr1
;
6191 type
= case_expr
->ts
.type
;
6192 if (type
!= BT_LOGICAL
&& type
!= BT_INTEGER
&& type
!= BT_CHARACTER
)
6194 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6195 &case_expr
->where
, gfc_typename (&case_expr
->ts
));
6197 /* Punt. Going on here just produce more garbage error messages. */
6201 if (case_expr
->rank
!= 0)
6203 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6204 "expression", &case_expr
->where
);
6210 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6211 of the SELECT CASE expression and its CASE values. Walk the lists
6212 of case values, and if we find a mismatch, promote case_expr to
6213 the appropriate kind. */
6215 if (type
== BT_LOGICAL
|| type
== BT_INTEGER
)
6217 for (body
= code
->block
; body
; body
= body
->block
)
6219 /* Walk the case label list. */
6220 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
6222 /* Intercept the DEFAULT case. It does not have a kind. */
6223 if (cp
->low
== NULL
&& cp
->high
== NULL
)
6226 /* Unreachable case ranges are discarded, so ignore. */
6227 if (cp
->low
!= NULL
&& cp
->high
!= NULL
6228 && cp
->low
!= cp
->high
6229 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
6232 /* FIXME: Should a warning be issued? */
6234 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->low
))
6235 gfc_convert_type_warn (case_expr
, &cp
->low
->ts
, 2, 0);
6237 if (cp
->high
!= NULL
6238 && case_expr
->ts
.kind
!= gfc_kind_max(case_expr
, cp
->high
))
6239 gfc_convert_type_warn (case_expr
, &cp
->high
->ts
, 2, 0);
6244 /* Assume there is no DEFAULT case. */
6245 default_case
= NULL
;
6250 for (body
= code
->block
; body
; body
= body
->block
)
6252 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6254 seen_unreachable
= 0;
6256 /* Walk the case label list, making sure that all case labels
6258 for (cp
= body
->ext
.case_list
; cp
; cp
= cp
->next
)
6260 /* Count the number of cases in the whole construct. */
6263 /* Intercept the DEFAULT case. */
6264 if (cp
->low
== NULL
&& cp
->high
== NULL
)
6266 if (default_case
!= NULL
)
6268 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6269 "by a second DEFAULT CASE at %L",
6270 &default_case
->where
, &cp
->where
);
6281 /* Deal with single value cases and case ranges. Errors are
6282 issued from the validation function. */
6283 if(validate_case_label_expr (cp
->low
, case_expr
) != SUCCESS
6284 || validate_case_label_expr (cp
->high
, case_expr
) != SUCCESS
)
6290 if (type
== BT_LOGICAL
6291 && ((cp
->low
== NULL
|| cp
->high
== NULL
)
6292 || cp
->low
!= cp
->high
))
6294 gfc_error ("Logical range in CASE statement at %L is not "
6295 "allowed", &cp
->low
->where
);
6300 if (type
== BT_LOGICAL
&& cp
->low
->expr_type
== EXPR_CONSTANT
)
6303 value
= cp
->low
->value
.logical
== 0 ? 2 : 1;
6304 if (value
& seen_logical
)
6306 gfc_error ("constant logical value in CASE statement "
6307 "is repeated at %L",
6312 seen_logical
|= value
;
6315 if (cp
->low
!= NULL
&& cp
->high
!= NULL
6316 && cp
->low
!= cp
->high
6317 && gfc_compare_expr (cp
->low
, cp
->high
, INTRINSIC_GT
) > 0)
6319 if (gfc_option
.warn_surprising
)
6320 gfc_warning ("Range specification at %L can never "
6321 "be matched", &cp
->where
);
6323 cp
->unreachable
= 1;
6324 seen_unreachable
= 1;
6328 /* If the case range can be matched, it can also overlap with
6329 other cases. To make sure it does not, we put it in a
6330 double linked list here. We sort that with a merge sort
6331 later on to detect any overlapping cases. */
6335 head
->right
= head
->left
= NULL
;
6340 tail
->right
->left
= tail
;
6347 /* It there was a failure in the previous case label, give up
6348 for this case label list. Continue with the next block. */
6352 /* See if any case labels that are unreachable have been seen.
6353 If so, we eliminate them. This is a bit of a kludge because
6354 the case lists for a single case statement (label) is a
6355 single forward linked lists. */
6356 if (seen_unreachable
)
6358 /* Advance until the first case in the list is reachable. */
6359 while (body
->ext
.case_list
!= NULL
6360 && body
->ext
.case_list
->unreachable
)
6362 gfc_case
*n
= body
->ext
.case_list
;
6363 body
->ext
.case_list
= body
->ext
.case_list
->next
;
6365 gfc_free_case_list (n
);
6368 /* Strip all other unreachable cases. */
6369 if (body
->ext
.case_list
)
6371 for (cp
= body
->ext
.case_list
; cp
->next
; cp
= cp
->next
)
6373 if (cp
->next
->unreachable
)
6375 gfc_case
*n
= cp
->next
;
6376 cp
->next
= cp
->next
->next
;
6378 gfc_free_case_list (n
);
6385 /* See if there were overlapping cases. If the check returns NULL,
6386 there was overlap. In that case we don't do anything. If head
6387 is non-NULL, we prepend the DEFAULT case. The sorted list can
6388 then used during code generation for SELECT CASE constructs with
6389 a case expression of a CHARACTER type. */
6392 head
= check_case_overlap (head
);
6394 /* Prepend the default_case if it is there. */
6395 if (head
!= NULL
&& default_case
)
6397 default_case
->left
= NULL
;
6398 default_case
->right
= head
;
6399 head
->left
= default_case
;
6403 /* Eliminate dead blocks that may be the result if we've seen
6404 unreachable case labels for a block. */
6405 for (body
= code
; body
&& body
->block
; body
= body
->block
)
6407 if (body
->block
->ext
.case_list
== NULL
)
6409 /* Cut the unreachable block from the code chain. */
6410 gfc_code
*c
= body
->block
;
6411 body
->block
= c
->block
;
6413 /* Kill the dead block, but not the blocks below it. */
6415 gfc_free_statements (c
);
6419 /* More than two cases is legal but insane for logical selects.
6420 Issue a warning for it. */
6421 if (gfc_option
.warn_surprising
&& type
== BT_LOGICAL
6423 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6428 /* Check if a derived type is extensible. */
6431 gfc_type_is_extensible (gfc_symbol
*sym
)
6433 return !(sym
->attr
.is_bind_c
|| sym
->attr
.sequence
);
6437 /* Resolve a SELECT TYPE statement. */
6440 resolve_select_type (gfc_code
*code
)
6442 gfc_symbol
*selector_type
;
6443 gfc_code
*body
, *new_st
;
6444 gfc_case
*c
, *default_case
;
6446 char name
[GFC_MAX_SYMBOL_LEN
];
6448 selector_type
= code
->expr1
->ts
.u
.derived
->components
->ts
.u
.derived
;
6450 /* Assume there is no DEFAULT case. */
6451 default_case
= NULL
;
6453 /* Loop over TYPE IS / CLASS IS cases. */
6454 for (body
= code
->block
; body
; body
= body
->block
)
6456 c
= body
->ext
.case_list
;
6458 /* Check F03:C815. */
6459 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
6460 && !gfc_type_is_extensible (c
->ts
.u
.derived
))
6462 gfc_error ("Derived type '%s' at %L must be extensible",
6463 c
->ts
.u
.derived
->name
, &c
->where
);
6467 /* Check F03:C816. */
6468 if ((c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
6469 && !gfc_type_is_extension_of (selector_type
, c
->ts
.u
.derived
))
6471 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6472 c
->ts
.u
.derived
->name
, &c
->where
, selector_type
->name
);
6476 /* Intercept the DEFAULT case. */
6477 if (c
->ts
.type
== BT_UNKNOWN
)
6479 /* Check F03:C818. */
6480 if (default_case
!= NULL
)
6481 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6482 "by a second DEFAULT CASE at %L",
6483 &default_case
->where
, &c
->where
);
6490 /* Transform to EXEC_SELECT. */
6491 code
->op
= EXEC_SELECT
;
6492 gfc_add_component_ref (code
->expr1
, "$vindex");
6494 /* Loop over TYPE IS / CLASS IS cases. */
6495 for (body
= code
->block
; body
; body
= body
->block
)
6497 c
= body
->ext
.case_list
;
6498 if (c
->ts
.type
== BT_DERIVED
)
6499 c
->low
= c
->high
= gfc_int_expr (c
->ts
.u
.derived
->vindex
);
6500 else if (c
->ts
.type
== BT_CLASS
)
6501 /* Currently IS CLASS blocks are simply ignored.
6502 TODO: Implement IS CLASS. */
6505 if (c
->ts
.type
!= BT_DERIVED
)
6507 /* Assign temporary to selector. */
6508 sprintf (name
, "tmp$%s", c
->ts
.u
.derived
->name
);
6509 st
= gfc_find_symtree (code
->expr1
->symtree
->n
.sym
->ns
->sym_root
, name
);
6510 new_st
= gfc_get_code ();
6511 new_st
->op
= EXEC_POINTER_ASSIGN
;
6512 new_st
->expr1
= gfc_get_variable_expr (st
);
6513 new_st
->expr2
= gfc_get_variable_expr (code
->expr1
->symtree
);
6514 gfc_add_component_ref (new_st
->expr2
, "$data");
6515 new_st
->next
= body
->next
;
6516 body
->next
= new_st
;
6519 /* Eliminate dead blocks. */
6520 for (body
= code
; body
&& body
->block
; body
= body
->block
)
6522 if (body
->block
->ext
.case_list
->unreachable
)
6524 /* Cut the unreachable block from the code chain. */
6525 gfc_code
*cd
= body
->block
;
6526 body
->block
= cd
->block
;
6527 /* Kill the dead block, but not the blocks below it. */
6529 gfc_free_statements (cd
);
6533 resolve_select (code
);
6538 /* Resolve a transfer statement. This is making sure that:
6539 -- a derived type being transferred has only non-pointer components
6540 -- a derived type being transferred doesn't have private components, unless
6541 it's being transferred from the module where the type was defined
6542 -- we're not trying to transfer a whole assumed size array. */
6545 resolve_transfer (gfc_code
*code
)
6554 if (exp
->expr_type
!= EXPR_VARIABLE
&& exp
->expr_type
!= EXPR_FUNCTION
)
6557 sym
= exp
->symtree
->n
.sym
;
6560 /* Go to actual component transferred. */
6561 for (ref
= code
->expr1
->ref
; ref
; ref
= ref
->next
)
6562 if (ref
->type
== REF_COMPONENT
)
6563 ts
= &ref
->u
.c
.component
->ts
;
6565 if (ts
->type
== BT_DERIVED
)
6567 /* Check that transferred derived type doesn't contain POINTER
6569 if (ts
->u
.derived
->attr
.pointer_comp
)
6571 gfc_error ("Data transfer element at %L cannot have "
6572 "POINTER components", &code
->loc
);
6576 if (ts
->u
.derived
->attr
.alloc_comp
)
6578 gfc_error ("Data transfer element at %L cannot have "
6579 "ALLOCATABLE components", &code
->loc
);
6583 if (derived_inaccessible (ts
->u
.derived
))
6585 gfc_error ("Data transfer element at %L cannot have "
6586 "PRIVATE components",&code
->loc
);
6591 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SIZE
6592 && exp
->ref
->type
== REF_ARRAY
&& exp
->ref
->u
.ar
.type
== AR_FULL
)
6594 gfc_error ("Data transfer element at %L cannot be a full reference to "
6595 "an assumed-size array", &code
->loc
);
6601 /*********** Toplevel code resolution subroutines ***********/
6603 /* Find the set of labels that are reachable from this block. We also
6604 record the last statement in each block. */
6607 find_reachable_labels (gfc_code
*block
)
6614 cs_base
->reachable_labels
= bitmap_obstack_alloc (&labels_obstack
);
6616 /* Collect labels in this block. We don't keep those corresponding
6617 to END {IF|SELECT}, these are checked in resolve_branch by going
6618 up through the code_stack. */
6619 for (c
= block
; c
; c
= c
->next
)
6621 if (c
->here
&& c
->op
!= EXEC_END_BLOCK
)
6622 bitmap_set_bit (cs_base
->reachable_labels
, c
->here
->value
);
6625 /* Merge with labels from parent block. */
6628 gcc_assert (cs_base
->prev
->reachable_labels
);
6629 bitmap_ior_into (cs_base
->reachable_labels
,
6630 cs_base
->prev
->reachable_labels
);
6634 /* Given a branch to a label, see if the branch is conforming.
6635 The code node describes where the branch is located. */
6638 resolve_branch (gfc_st_label
*label
, gfc_code
*code
)
6645 /* Step one: is this a valid branching target? */
6647 if (label
->defined
== ST_LABEL_UNKNOWN
)
6649 gfc_error ("Label %d referenced at %L is never defined", label
->value
,
6654 if (label
->defined
!= ST_LABEL_TARGET
)
6656 gfc_error ("Statement at %L is not a valid branch target statement "
6657 "for the branch statement at %L", &label
->where
, &code
->loc
);
6661 /* Step two: make sure this branch is not a branch to itself ;-) */
6663 if (code
->here
== label
)
6665 gfc_warning ("Branch at %L may result in an infinite loop", &code
->loc
);
6669 /* Step three: See if the label is in the same block as the
6670 branching statement. The hard work has been done by setting up
6671 the bitmap reachable_labels. */
6673 if (bitmap_bit_p (cs_base
->reachable_labels
, label
->value
))
6676 /* Step four: If we haven't found the label in the bitmap, it may
6677 still be the label of the END of the enclosing block, in which
6678 case we find it by going up the code_stack. */
6680 for (stack
= cs_base
; stack
; stack
= stack
->prev
)
6681 if (stack
->current
->next
&& stack
->current
->next
->here
== label
)
6686 gcc_assert (stack
->current
->next
->op
== EXEC_END_BLOCK
);
6690 /* The label is not in an enclosing block, so illegal. This was
6691 allowed in Fortran 66, so we allow it as extension. No
6692 further checks are necessary in this case. */
6693 gfc_notify_std (GFC_STD_LEGACY
, "Label at %L is not in the same block "
6694 "as the GOTO statement at %L", &label
->where
,
6700 /* Check whether EXPR1 has the same shape as EXPR2. */
6703 resolve_where_shape (gfc_expr
*expr1
, gfc_expr
*expr2
)
6705 mpz_t shape
[GFC_MAX_DIMENSIONS
];
6706 mpz_t shape2
[GFC_MAX_DIMENSIONS
];
6707 gfc_try result
= FAILURE
;
6710 /* Compare the rank. */
6711 if (expr1
->rank
!= expr2
->rank
)
6714 /* Compare the size of each dimension. */
6715 for (i
=0; i
<expr1
->rank
; i
++)
6717 if (gfc_array_dimen_size (expr1
, i
, &shape
[i
]) == FAILURE
)
6720 if (gfc_array_dimen_size (expr2
, i
, &shape2
[i
]) == FAILURE
)
6723 if (mpz_cmp (shape
[i
], shape2
[i
]))
6727 /* When either of the two expression is an assumed size array, we
6728 ignore the comparison of dimension sizes. */
6733 for (i
--; i
>= 0; i
--)
6735 mpz_clear (shape
[i
]);
6736 mpz_clear (shape2
[i
]);
6742 /* Check whether a WHERE assignment target or a WHERE mask expression
6743 has the same shape as the outmost WHERE mask expression. */
6746 resolve_where (gfc_code
*code
, gfc_expr
*mask
)
6752 cblock
= code
->block
;
6754 /* Store the first WHERE mask-expr of the WHERE statement or construct.
6755 In case of nested WHERE, only the outmost one is stored. */
6756 if (mask
== NULL
) /* outmost WHERE */
6758 else /* inner WHERE */
6765 /* Check if the mask-expr has a consistent shape with the
6766 outmost WHERE mask-expr. */
6767 if (resolve_where_shape (cblock
->expr1
, e
) == FAILURE
)
6768 gfc_error ("WHERE mask at %L has inconsistent shape",
6769 &cblock
->expr1
->where
);
6772 /* the assignment statement of a WHERE statement, or the first
6773 statement in where-body-construct of a WHERE construct */
6774 cnext
= cblock
->next
;
6779 /* WHERE assignment statement */
6782 /* Check shape consistent for WHERE assignment target. */
6783 if (e
&& resolve_where_shape (cnext
->expr1
, e
) == FAILURE
)
6784 gfc_error ("WHERE assignment target at %L has "
6785 "inconsistent shape", &cnext
->expr1
->where
);
6789 case EXEC_ASSIGN_CALL
:
6790 resolve_call (cnext
);
6791 if (!cnext
->resolved_sym
->attr
.elemental
)
6792 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6793 &cnext
->ext
.actual
->expr
->where
);
6796 /* WHERE or WHERE construct is part of a where-body-construct */
6798 resolve_where (cnext
, e
);
6802 gfc_error ("Unsupported statement inside WHERE at %L",
6805 /* the next statement within the same where-body-construct */
6806 cnext
= cnext
->next
;
6808 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6809 cblock
= cblock
->block
;
6814 /* Resolve assignment in FORALL construct.
6815 NVAR is the number of FORALL index variables, and VAR_EXPR records the
6816 FORALL index variables. */
6819 gfc_resolve_assign_in_forall (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
6823 for (n
= 0; n
< nvar
; n
++)
6825 gfc_symbol
*forall_index
;
6827 forall_index
= var_expr
[n
]->symtree
->n
.sym
;
6829 /* Check whether the assignment target is one of the FORALL index
6831 if ((code
->expr1
->expr_type
== EXPR_VARIABLE
)
6832 && (code
->expr1
->symtree
->n
.sym
== forall_index
))
6833 gfc_error ("Assignment to a FORALL index variable at %L",
6834 &code
->expr1
->where
);
6837 /* If one of the FORALL index variables doesn't appear in the
6838 assignment variable, then there could be a many-to-one
6839 assignment. Emit a warning rather than an error because the
6840 mask could be resolving this problem. */
6841 if (find_forall_index (code
->expr1
, forall_index
, 0) == FAILURE
)
6842 gfc_warning ("The FORALL with index '%s' is not used on the "
6843 "left side of the assignment at %L and so might "
6844 "cause multiple assignment to this object",
6845 var_expr
[n
]->symtree
->name
, &code
->expr1
->where
);
6851 /* Resolve WHERE statement in FORALL construct. */
6854 gfc_resolve_where_code_in_forall (gfc_code
*code
, int nvar
,
6855 gfc_expr
**var_expr
)
6860 cblock
= code
->block
;
6863 /* the assignment statement of a WHERE statement, or the first
6864 statement in where-body-construct of a WHERE construct */
6865 cnext
= cblock
->next
;
6870 /* WHERE assignment statement */
6872 gfc_resolve_assign_in_forall (cnext
, nvar
, var_expr
);
6875 /* WHERE operator assignment statement */
6876 case EXEC_ASSIGN_CALL
:
6877 resolve_call (cnext
);
6878 if (!cnext
->resolved_sym
->attr
.elemental
)
6879 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6880 &cnext
->ext
.actual
->expr
->where
);
6883 /* WHERE or WHERE construct is part of a where-body-construct */
6885 gfc_resolve_where_code_in_forall (cnext
, nvar
, var_expr
);
6889 gfc_error ("Unsupported statement inside WHERE at %L",
6892 /* the next statement within the same where-body-construct */
6893 cnext
= cnext
->next
;
6895 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6896 cblock
= cblock
->block
;
6901 /* Traverse the FORALL body to check whether the following errors exist:
6902 1. For assignment, check if a many-to-one assignment happens.
6903 2. For WHERE statement, check the WHERE body to see if there is any
6904 many-to-one assignment. */
6907 gfc_resolve_forall_body (gfc_code
*code
, int nvar
, gfc_expr
**var_expr
)
6911 c
= code
->block
->next
;
6917 case EXEC_POINTER_ASSIGN
:
6918 gfc_resolve_assign_in_forall (c
, nvar
, var_expr
);
6921 case EXEC_ASSIGN_CALL
:
6925 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
6926 there is no need to handle it here. */
6930 gfc_resolve_where_code_in_forall(c
, nvar
, var_expr
);
6935 /* The next statement in the FORALL body. */
6941 /* Counts the number of iterators needed inside a forall construct, including
6942 nested forall constructs. This is used to allocate the needed memory
6943 in gfc_resolve_forall. */
6946 gfc_count_forall_iterators (gfc_code
*code
)
6948 int max_iters
, sub_iters
, current_iters
;
6949 gfc_forall_iterator
*fa
;
6951 gcc_assert(code
->op
== EXEC_FORALL
);
6955 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
6958 code
= code
->block
->next
;
6962 if (code
->op
== EXEC_FORALL
)
6964 sub_iters
= gfc_count_forall_iterators (code
);
6965 if (sub_iters
> max_iters
)
6966 max_iters
= sub_iters
;
6971 return current_iters
+ max_iters
;
6975 /* Given a FORALL construct, first resolve the FORALL iterator, then call
6976 gfc_resolve_forall_body to resolve the FORALL body. */
6979 gfc_resolve_forall (gfc_code
*code
, gfc_namespace
*ns
, int forall_save
)
6981 static gfc_expr
**var_expr
;
6982 static int total_var
= 0;
6983 static int nvar
= 0;
6985 gfc_forall_iterator
*fa
;
6990 /* Start to resolve a FORALL construct */
6991 if (forall_save
== 0)
6993 /* Count the total number of FORALL index in the nested FORALL
6994 construct in order to allocate the VAR_EXPR with proper size. */
6995 total_var
= gfc_count_forall_iterators (code
);
6997 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6998 var_expr
= (gfc_expr
**) gfc_getmem (total_var
* sizeof (gfc_expr
*));
7001 /* The information about FORALL iterator, including FORALL index start, end
7002 and stride. The FORALL index can not appear in start, end or stride. */
7003 for (fa
= code
->ext
.forall_iterator
; fa
; fa
= fa
->next
)
7005 /* Check if any outer FORALL index name is the same as the current
7007 for (i
= 0; i
< nvar
; i
++)
7009 if (fa
->var
->symtree
->n
.sym
== var_expr
[i
]->symtree
->n
.sym
)
7011 gfc_error ("An outer FORALL construct already has an index "
7012 "with this name %L", &fa
->var
->where
);
7016 /* Record the current FORALL index. */
7017 var_expr
[nvar
] = gfc_copy_expr (fa
->var
);
7021 /* No memory leak. */
7022 gcc_assert (nvar
<= total_var
);
7025 /* Resolve the FORALL body. */
7026 gfc_resolve_forall_body (code
, nvar
, var_expr
);
7028 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7029 gfc_resolve_blocks (code
->block
, ns
);
7033 /* Free only the VAR_EXPRs allocated in this frame. */
7034 for (i
= nvar
; i
< tmp
; i
++)
7035 gfc_free_expr (var_expr
[i
]);
7039 /* We are in the outermost FORALL construct. */
7040 gcc_assert (forall_save
== 0);
7042 /* VAR_EXPR is not needed any more. */
7043 gfc_free (var_expr
);
7049 /* Resolve a BLOCK construct statement. */
7052 resolve_block_construct (gfc_code
* code
)
7054 /* Eventually, we may want to do some checks here or handle special stuff.
7055 But so far the only thing we can do is resolving the local namespace. */
7057 gfc_resolve (code
->ext
.ns
);
7061 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7064 static void resolve_code (gfc_code
*, gfc_namespace
*);
7067 gfc_resolve_blocks (gfc_code
*b
, gfc_namespace
*ns
)
7071 for (; b
; b
= b
->block
)
7073 t
= gfc_resolve_expr (b
->expr1
);
7074 if (gfc_resolve_expr (b
->expr2
) == FAILURE
)
7080 if (t
== SUCCESS
&& b
->expr1
!= NULL
7081 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
!= 0))
7082 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7089 && (b
->expr1
->ts
.type
!= BT_LOGICAL
|| b
->expr1
->rank
== 0))
7090 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7095 resolve_branch (b
->label1
, b
);
7099 resolve_block_construct (b
);
7103 case EXEC_SELECT_TYPE
:
7113 case EXEC_OMP_ATOMIC
:
7114 case EXEC_OMP_CRITICAL
:
7116 case EXEC_OMP_MASTER
:
7117 case EXEC_OMP_ORDERED
:
7118 case EXEC_OMP_PARALLEL
:
7119 case EXEC_OMP_PARALLEL_DO
:
7120 case EXEC_OMP_PARALLEL_SECTIONS
:
7121 case EXEC_OMP_PARALLEL_WORKSHARE
:
7122 case EXEC_OMP_SECTIONS
:
7123 case EXEC_OMP_SINGLE
:
7125 case EXEC_OMP_TASKWAIT
:
7126 case EXEC_OMP_WORKSHARE
:
7130 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7133 resolve_code (b
->next
, ns
);
7138 /* Does everything to resolve an ordinary assignment. Returns true
7139 if this is an interface assignment. */
7141 resolve_ordinary_assign (gfc_code
*code
, gfc_namespace
*ns
)
7151 if (gfc_extend_assign (code
, ns
) == SUCCESS
)
7153 gfc_symbol
* assign_proc
;
7156 if (code
->op
== EXEC_ASSIGN_CALL
)
7158 lhs
= code
->ext
.actual
->expr
;
7159 rhsptr
= &code
->ext
.actual
->next
->expr
;
7160 assign_proc
= code
->symtree
->n
.sym
;
7164 gfc_actual_arglist
* args
;
7165 gfc_typebound_proc
* tbp
;
7167 gcc_assert (code
->op
== EXEC_COMPCALL
);
7169 args
= code
->expr1
->value
.compcall
.actual
;
7171 rhsptr
= &args
->next
->expr
;
7173 tbp
= code
->expr1
->value
.compcall
.tbp
;
7174 gcc_assert (!tbp
->is_generic
);
7175 assign_proc
= tbp
->u
.specific
->n
.sym
;
7178 /* Make a temporary rhs when there is a default initializer
7179 and rhs is the same symbol as the lhs. */
7180 if ((*rhsptr
)->expr_type
== EXPR_VARIABLE
7181 && (*rhsptr
)->symtree
->n
.sym
->ts
.type
== BT_DERIVED
7182 && has_default_initializer ((*rhsptr
)->symtree
->n
.sym
->ts
.u
.derived
)
7183 && (lhs
->symtree
->n
.sym
== (*rhsptr
)->symtree
->n
.sym
))
7184 *rhsptr
= gfc_get_parentheses (*rhsptr
);
7193 && gfc_notify_std (GFC_STD_GNU
, "Extension: BOZ literal at %L outside "
7194 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7195 &code
->loc
) == FAILURE
)
7198 /* Handle the case of a BOZ literal on the RHS. */
7199 if (rhs
->is_boz
&& lhs
->ts
.type
!= BT_INTEGER
)
7202 if (gfc_option
.warn_surprising
)
7203 gfc_warning ("BOZ literal at %L is bitwise transferred "
7204 "non-integer symbol '%s'", &code
->loc
,
7205 lhs
->symtree
->n
.sym
->name
);
7207 if (!gfc_convert_boz (rhs
, &lhs
->ts
))
7209 if ((rc
= gfc_range_check (rhs
)) != ARITH_OK
)
7211 if (rc
== ARITH_UNDERFLOW
)
7212 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7213 ". This check can be disabled with the option "
7214 "-fno-range-check", &rhs
->where
);
7215 else if (rc
== ARITH_OVERFLOW
)
7216 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7217 ". This check can be disabled with the option "
7218 "-fno-range-check", &rhs
->where
);
7219 else if (rc
== ARITH_NAN
)
7220 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7221 ". This check can be disabled with the option "
7222 "-fno-range-check", &rhs
->where
);
7228 if (lhs
->ts
.type
== BT_CHARACTER
7229 && gfc_option
.warn_character_truncation
)
7231 if (lhs
->ts
.u
.cl
!= NULL
7232 && lhs
->ts
.u
.cl
->length
!= NULL
7233 && lhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
7234 llen
= mpz_get_si (lhs
->ts
.u
.cl
->length
->value
.integer
);
7236 if (rhs
->expr_type
== EXPR_CONSTANT
)
7237 rlen
= rhs
->value
.character
.length
;
7239 else if (rhs
->ts
.u
.cl
!= NULL
7240 && rhs
->ts
.u
.cl
->length
!= NULL
7241 && rhs
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
7242 rlen
= mpz_get_si (rhs
->ts
.u
.cl
->length
->value
.integer
);
7244 if (rlen
&& llen
&& rlen
> llen
)
7245 gfc_warning_now ("CHARACTER expression will be truncated "
7246 "in assignment (%d/%d) at %L",
7247 llen
, rlen
, &code
->loc
);
7250 /* Ensure that a vector index expression for the lvalue is evaluated
7251 to a temporary if the lvalue symbol is referenced in it. */
7254 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
7255 if (ref
->type
== REF_ARRAY
)
7257 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
7258 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
7259 && gfc_find_sym_in_expr (lhs
->symtree
->n
.sym
,
7260 ref
->u
.ar
.start
[n
]))
7262 = gfc_get_parentheses (ref
->u
.ar
.start
[n
]);
7266 if (gfc_pure (NULL
))
7268 if (gfc_impure_variable (lhs
->symtree
->n
.sym
))
7270 gfc_error ("Cannot assign to variable '%s' in PURE "
7272 lhs
->symtree
->n
.sym
->name
,
7277 if (lhs
->ts
.type
== BT_DERIVED
7278 && lhs
->expr_type
== EXPR_VARIABLE
7279 && lhs
->ts
.u
.derived
->attr
.pointer_comp
7280 && gfc_impure_variable (rhs
->symtree
->n
.sym
))
7282 gfc_error ("The impure variable at %L is assigned to "
7283 "a derived type variable with a POINTER "
7284 "component in a PURE procedure (12.6)",
7290 gfc_check_assign (lhs
, rhs
, 1);
7295 /* Check an assignment to a CLASS object (pointer or ordinary assignment). */
7298 resolve_class_assign (gfc_code
*code
)
7300 gfc_code
*assign_code
= gfc_get_code ();
7302 if (code
->expr2
->ts
.type
!= BT_CLASS
)
7304 /* Insert an additional assignment which sets the vindex. */
7305 assign_code
->next
= code
->next
;
7306 code
->next
= assign_code
;
7307 assign_code
->op
= EXEC_ASSIGN
;
7308 assign_code
->expr1
= gfc_copy_expr (code
->expr1
);
7309 gfc_add_component_ref (assign_code
->expr1
, "$vindex");
7310 if (code
->expr2
->ts
.type
== BT_DERIVED
)
7311 /* vindex is constant, determined at compile time. */
7312 assign_code
->expr2
= gfc_int_expr (code
->expr2
->ts
.u
.derived
->vindex
);
7313 else if (code
->expr2
->ts
.type
== BT_CLASS
)
7315 /* vindex must be determined at run time. */
7316 assign_code
->expr2
= gfc_copy_expr (code
->expr2
);
7317 gfc_add_component_ref (assign_code
->expr2
, "$vindex");
7319 else if (code
->expr2
->expr_type
== EXPR_NULL
)
7320 assign_code
->expr2
= gfc_int_expr (0);
7325 /* Modify the actual pointer assignment. */
7326 if (code
->expr2
->ts
.type
== BT_CLASS
)
7327 code
->op
= EXEC_ASSIGN
;
7329 gfc_add_component_ref (code
->expr1
, "$data");
7333 /* Given a block of code, recursively resolve everything pointed to by this
7337 resolve_code (gfc_code
*code
, gfc_namespace
*ns
)
7339 int omp_workshare_save
;
7344 frame
.prev
= cs_base
;
7348 find_reachable_labels (code
);
7350 for (; code
; code
= code
->next
)
7352 frame
.current
= code
;
7353 forall_save
= forall_flag
;
7355 if (code
->op
== EXEC_FORALL
)
7358 gfc_resolve_forall (code
, ns
, forall_save
);
7361 else if (code
->block
)
7363 omp_workshare_save
= -1;
7366 case EXEC_OMP_PARALLEL_WORKSHARE
:
7367 omp_workshare_save
= omp_workshare_flag
;
7368 omp_workshare_flag
= 1;
7369 gfc_resolve_omp_parallel_blocks (code
, ns
);
7371 case EXEC_OMP_PARALLEL
:
7372 case EXEC_OMP_PARALLEL_DO
:
7373 case EXEC_OMP_PARALLEL_SECTIONS
:
7375 omp_workshare_save
= omp_workshare_flag
;
7376 omp_workshare_flag
= 0;
7377 gfc_resolve_omp_parallel_blocks (code
, ns
);
7380 gfc_resolve_omp_do_blocks (code
, ns
);
7382 case EXEC_OMP_WORKSHARE
:
7383 omp_workshare_save
= omp_workshare_flag
;
7384 omp_workshare_flag
= 1;
7387 gfc_resolve_blocks (code
->block
, ns
);
7391 if (omp_workshare_save
!= -1)
7392 omp_workshare_flag
= omp_workshare_save
;
7396 if (code
->op
!= EXEC_COMPCALL
&& code
->op
!= EXEC_CALL_PPC
)
7397 t
= gfc_resolve_expr (code
->expr1
);
7398 forall_flag
= forall_save
;
7400 if (gfc_resolve_expr (code
->expr2
) == FAILURE
)
7406 case EXEC_END_BLOCK
:
7413 case EXEC_ASSIGN_CALL
:
7417 /* Keep track of which entry we are up to. */
7418 current_entry_id
= code
->ext
.entry
->id
;
7422 resolve_where (code
, NULL
);
7426 if (code
->expr1
!= NULL
)
7428 if (code
->expr1
->ts
.type
!= BT_INTEGER
)
7429 gfc_error ("ASSIGNED GOTO statement at %L requires an "
7430 "INTEGER variable", &code
->expr1
->where
);
7431 else if (code
->expr1
->symtree
->n
.sym
->attr
.assign
!= 1)
7432 gfc_error ("Variable '%s' has not been assigned a target "
7433 "label at %L", code
->expr1
->symtree
->n
.sym
->name
,
7434 &code
->expr1
->where
);
7437 resolve_branch (code
->label1
, code
);
7441 if (code
->expr1
!= NULL
7442 && (code
->expr1
->ts
.type
!= BT_INTEGER
|| code
->expr1
->rank
))
7443 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
7444 "INTEGER return specifier", &code
->expr1
->where
);
7447 case EXEC_INIT_ASSIGN
:
7448 case EXEC_END_PROCEDURE
:
7455 if (code
->expr1
->ts
.type
== BT_CLASS
)
7456 resolve_class_assign (code
);
7458 if (resolve_ordinary_assign (code
, ns
))
7460 if (code
->op
== EXEC_COMPCALL
)
7468 case EXEC_LABEL_ASSIGN
:
7469 if (code
->label1
->defined
== ST_LABEL_UNKNOWN
)
7470 gfc_error ("Label %d referenced at %L is never defined",
7471 code
->label1
->value
, &code
->label1
->where
);
7473 && (code
->expr1
->expr_type
!= EXPR_VARIABLE
7474 || code
->expr1
->symtree
->n
.sym
->ts
.type
!= BT_INTEGER
7475 || code
->expr1
->symtree
->n
.sym
->ts
.kind
7476 != gfc_default_integer_kind
7477 || code
->expr1
->symtree
->n
.sym
->as
!= NULL
))
7478 gfc_error ("ASSIGN statement at %L requires a scalar "
7479 "default INTEGER variable", &code
->expr1
->where
);
7482 case EXEC_POINTER_ASSIGN
:
7486 if (code
->expr1
->ts
.type
== BT_CLASS
)
7487 resolve_class_assign (code
);
7489 gfc_check_pointer_assign (code
->expr1
, code
->expr2
);
7493 case EXEC_ARITHMETIC_IF
:
7495 && code
->expr1
->ts
.type
!= BT_INTEGER
7496 && code
->expr1
->ts
.type
!= BT_REAL
)
7497 gfc_error ("Arithmetic IF statement at %L requires a numeric "
7498 "expression", &code
->expr1
->where
);
7500 resolve_branch (code
->label1
, code
);
7501 resolve_branch (code
->label2
, code
);
7502 resolve_branch (code
->label3
, code
);
7506 if (t
== SUCCESS
&& code
->expr1
!= NULL
7507 && (code
->expr1
->ts
.type
!= BT_LOGICAL
7508 || code
->expr1
->rank
!= 0))
7509 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7510 &code
->expr1
->where
);
7515 resolve_call (code
);
7520 resolve_typebound_call (code
);
7524 resolve_ppc_call (code
);
7528 /* Select is complicated. Also, a SELECT construct could be
7529 a transformed computed GOTO. */
7530 resolve_select (code
);
7533 case EXEC_SELECT_TYPE
:
7534 resolve_select_type (code
);
7538 gfc_resolve (code
->ext
.ns
);
7542 if (code
->ext
.iterator
!= NULL
)
7544 gfc_iterator
*iter
= code
->ext
.iterator
;
7545 if (gfc_resolve_iterator (iter
, true) != FAILURE
)
7546 gfc_resolve_do_iterator (code
, iter
->var
->symtree
->n
.sym
);
7551 if (code
->expr1
== NULL
)
7552 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7554 && (code
->expr1
->rank
!= 0
7555 || code
->expr1
->ts
.type
!= BT_LOGICAL
))
7556 gfc_error ("Exit condition of DO WHILE loop at %L must be "
7557 "a scalar LOGICAL expression", &code
->expr1
->where
);
7562 resolve_allocate_deallocate (code
, "ALLOCATE");
7566 case EXEC_DEALLOCATE
:
7568 resolve_allocate_deallocate (code
, "DEALLOCATE");
7573 if (gfc_resolve_open (code
->ext
.open
) == FAILURE
)
7576 resolve_branch (code
->ext
.open
->err
, code
);
7580 if (gfc_resolve_close (code
->ext
.close
) == FAILURE
)
7583 resolve_branch (code
->ext
.close
->err
, code
);
7586 case EXEC_BACKSPACE
:
7590 if (gfc_resolve_filepos (code
->ext
.filepos
) == FAILURE
)
7593 resolve_branch (code
->ext
.filepos
->err
, code
);
7597 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
7600 resolve_branch (code
->ext
.inquire
->err
, code
);
7604 gcc_assert (code
->ext
.inquire
!= NULL
);
7605 if (gfc_resolve_inquire (code
->ext
.inquire
) == FAILURE
)
7608 resolve_branch (code
->ext
.inquire
->err
, code
);
7612 if (gfc_resolve_wait (code
->ext
.wait
) == FAILURE
)
7615 resolve_branch (code
->ext
.wait
->err
, code
);
7616 resolve_branch (code
->ext
.wait
->end
, code
);
7617 resolve_branch (code
->ext
.wait
->eor
, code
);
7622 if (gfc_resolve_dt (code
->ext
.dt
, &code
->loc
) == FAILURE
)
7625 resolve_branch (code
->ext
.dt
->err
, code
);
7626 resolve_branch (code
->ext
.dt
->end
, code
);
7627 resolve_branch (code
->ext
.dt
->eor
, code
);
7631 resolve_transfer (code
);
7635 resolve_forall_iterators (code
->ext
.forall_iterator
);
7637 if (code
->expr1
!= NULL
&& code
->expr1
->ts
.type
!= BT_LOGICAL
)
7638 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7639 "expression", &code
->expr1
->where
);
7642 case EXEC_OMP_ATOMIC
:
7643 case EXEC_OMP_BARRIER
:
7644 case EXEC_OMP_CRITICAL
:
7645 case EXEC_OMP_FLUSH
:
7647 case EXEC_OMP_MASTER
:
7648 case EXEC_OMP_ORDERED
:
7649 case EXEC_OMP_SECTIONS
:
7650 case EXEC_OMP_SINGLE
:
7651 case EXEC_OMP_TASKWAIT
:
7652 case EXEC_OMP_WORKSHARE
:
7653 gfc_resolve_omp_directive (code
, ns
);
7656 case EXEC_OMP_PARALLEL
:
7657 case EXEC_OMP_PARALLEL_DO
:
7658 case EXEC_OMP_PARALLEL_SECTIONS
:
7659 case EXEC_OMP_PARALLEL_WORKSHARE
:
7661 omp_workshare_save
= omp_workshare_flag
;
7662 omp_workshare_flag
= 0;
7663 gfc_resolve_omp_directive (code
, ns
);
7664 omp_workshare_flag
= omp_workshare_save
;
7668 gfc_internal_error ("resolve_code(): Bad statement code");
7672 cs_base
= frame
.prev
;
7676 /* Resolve initial values and make sure they are compatible with
7680 resolve_values (gfc_symbol
*sym
)
7682 if (sym
->value
== NULL
)
7685 if (gfc_resolve_expr (sym
->value
) == FAILURE
)
7688 gfc_check_assign_symbol (sym
, sym
->value
);
7692 /* Verify the binding labels for common blocks that are BIND(C). The label
7693 for a BIND(C) common block must be identical in all scoping units in which
7694 the common block is declared. Further, the binding label can not collide
7695 with any other global entity in the program. */
7698 resolve_bind_c_comms (gfc_symtree
*comm_block_tree
)
7700 if (comm_block_tree
->n
.common
->is_bind_c
== 1)
7702 gfc_gsymbol
*binding_label_gsym
;
7703 gfc_gsymbol
*comm_name_gsym
;
7705 /* See if a global symbol exists by the common block's name. It may
7706 be NULL if the common block is use-associated. */
7707 comm_name_gsym
= gfc_find_gsymbol (gfc_gsym_root
,
7708 comm_block_tree
->n
.common
->name
);
7709 if (comm_name_gsym
!= NULL
&& comm_name_gsym
->type
!= GSYM_COMMON
)
7710 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
7711 "with the global entity '%s' at %L",
7712 comm_block_tree
->n
.common
->binding_label
,
7713 comm_block_tree
->n
.common
->name
,
7714 &(comm_block_tree
->n
.common
->where
),
7715 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
7716 else if (comm_name_gsym
!= NULL
7717 && strcmp (comm_name_gsym
->name
,
7718 comm_block_tree
->n
.common
->name
) == 0)
7720 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
7722 if (comm_name_gsym
->binding_label
== NULL
)
7723 /* No binding label for common block stored yet; save this one. */
7724 comm_name_gsym
->binding_label
=
7725 comm_block_tree
->n
.common
->binding_label
;
7727 if (strcmp (comm_name_gsym
->binding_label
,
7728 comm_block_tree
->n
.common
->binding_label
) != 0)
7730 /* Common block names match but binding labels do not. */
7731 gfc_error ("Binding label '%s' for common block '%s' at %L "
7732 "does not match the binding label '%s' for common "
7734 comm_block_tree
->n
.common
->binding_label
,
7735 comm_block_tree
->n
.common
->name
,
7736 &(comm_block_tree
->n
.common
->where
),
7737 comm_name_gsym
->binding_label
,
7738 comm_name_gsym
->name
,
7739 &(comm_name_gsym
->where
));
7744 /* There is no binding label (NAME="") so we have nothing further to
7745 check and nothing to add as a global symbol for the label. */
7746 if (comm_block_tree
->n
.common
->binding_label
[0] == '\0' )
7749 binding_label_gsym
=
7750 gfc_find_gsymbol (gfc_gsym_root
,
7751 comm_block_tree
->n
.common
->binding_label
);
7752 if (binding_label_gsym
== NULL
)
7754 /* Need to make a global symbol for the binding label to prevent
7755 it from colliding with another. */
7756 binding_label_gsym
=
7757 gfc_get_gsymbol (comm_block_tree
->n
.common
->binding_label
);
7758 binding_label_gsym
->sym_name
= comm_block_tree
->n
.common
->name
;
7759 binding_label_gsym
->type
= GSYM_COMMON
;
7763 /* If comm_name_gsym is NULL, the name common block is use
7764 associated and the name could be colliding. */
7765 if (binding_label_gsym
->type
!= GSYM_COMMON
)
7766 gfc_error ("Binding label '%s' for common block '%s' at %L "
7767 "collides with the global entity '%s' at %L",
7768 comm_block_tree
->n
.common
->binding_label
,
7769 comm_block_tree
->n
.common
->name
,
7770 &(comm_block_tree
->n
.common
->where
),
7771 binding_label_gsym
->name
,
7772 &(binding_label_gsym
->where
));
7773 else if (comm_name_gsym
!= NULL
7774 && (strcmp (binding_label_gsym
->name
,
7775 comm_name_gsym
->binding_label
) != 0)
7776 && (strcmp (binding_label_gsym
->sym_name
,
7777 comm_name_gsym
->name
) != 0))
7778 gfc_error ("Binding label '%s' for common block '%s' at %L "
7779 "collides with global entity '%s' at %L",
7780 binding_label_gsym
->name
, binding_label_gsym
->sym_name
,
7781 &(comm_block_tree
->n
.common
->where
),
7782 comm_name_gsym
->name
, &(comm_name_gsym
->where
));
7790 /* Verify any BIND(C) derived types in the namespace so we can report errors
7791 for them once, rather than for each variable declared of that type. */
7794 resolve_bind_c_derived_types (gfc_symbol
*derived_sym
)
7796 if (derived_sym
!= NULL
&& derived_sym
->attr
.flavor
== FL_DERIVED
7797 && derived_sym
->attr
.is_bind_c
== 1)
7798 verify_bind_c_derived_type (derived_sym
);
7804 /* Verify that any binding labels used in a given namespace do not collide
7805 with the names or binding labels of any global symbols. */
7808 gfc_verify_binding_labels (gfc_symbol
*sym
)
7812 if (sym
!= NULL
&& sym
->attr
.is_bind_c
&& sym
->attr
.is_iso_c
== 0
7813 && sym
->attr
.flavor
!= FL_DERIVED
&& sym
->binding_label
[0] != '\0')
7815 gfc_gsymbol
*bind_c_sym
;
7817 bind_c_sym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
7818 if (bind_c_sym
!= NULL
7819 && strcmp (bind_c_sym
->name
, sym
->binding_label
) == 0)
7821 if (sym
->attr
.if_source
== IFSRC_DECL
7822 && (bind_c_sym
->type
!= GSYM_SUBROUTINE
7823 && bind_c_sym
->type
!= GSYM_FUNCTION
)
7824 && ((sym
->attr
.contained
== 1
7825 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0)
7826 || (sym
->attr
.use_assoc
== 1
7827 && (strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0))))
7829 /* Make sure global procedures don't collide with anything. */
7830 gfc_error ("Binding label '%s' at %L collides with the global "
7831 "entity '%s' at %L", sym
->binding_label
,
7832 &(sym
->declared_at
), bind_c_sym
->name
,
7833 &(bind_c_sym
->where
));
7836 else if (sym
->attr
.contained
== 0
7837 && (sym
->attr
.if_source
== IFSRC_IFBODY
7838 && sym
->attr
.flavor
== FL_PROCEDURE
)
7839 && (bind_c_sym
->sym_name
!= NULL
7840 && strcmp (bind_c_sym
->sym_name
, sym
->name
) != 0))
7842 /* Make sure procedures in interface bodies don't collide. */
7843 gfc_error ("Binding label '%s' in interface body at %L collides "
7844 "with the global entity '%s' at %L",
7846 &(sym
->declared_at
), bind_c_sym
->name
,
7847 &(bind_c_sym
->where
));
7850 else if (sym
->attr
.contained
== 0
7851 && sym
->attr
.if_source
== IFSRC_UNKNOWN
)
7852 if ((sym
->attr
.use_assoc
&& bind_c_sym
->mod_name
7853 && strcmp (bind_c_sym
->mod_name
, sym
->module
) != 0)
7854 || sym
->attr
.use_assoc
== 0)
7856 gfc_error ("Binding label '%s' at %L collides with global "
7857 "entity '%s' at %L", sym
->binding_label
,
7858 &(sym
->declared_at
), bind_c_sym
->name
,
7859 &(bind_c_sym
->where
));
7864 /* Clear the binding label to prevent checking multiple times. */
7865 sym
->binding_label
[0] = '\0';
7867 else if (bind_c_sym
== NULL
)
7869 bind_c_sym
= gfc_get_gsymbol (sym
->binding_label
);
7870 bind_c_sym
->where
= sym
->declared_at
;
7871 bind_c_sym
->sym_name
= sym
->name
;
7873 if (sym
->attr
.use_assoc
== 1)
7874 bind_c_sym
->mod_name
= sym
->module
;
7876 if (sym
->ns
->proc_name
!= NULL
)
7877 bind_c_sym
->mod_name
= sym
->ns
->proc_name
->name
;
7879 if (sym
->attr
.contained
== 0)
7881 if (sym
->attr
.subroutine
)
7882 bind_c_sym
->type
= GSYM_SUBROUTINE
;
7883 else if (sym
->attr
.function
)
7884 bind_c_sym
->type
= GSYM_FUNCTION
;
7892 /* Resolve an index expression. */
7895 resolve_index_expr (gfc_expr
*e
)
7897 if (gfc_resolve_expr (e
) == FAILURE
)
7900 if (gfc_simplify_expr (e
, 0) == FAILURE
)
7903 if (gfc_specification_expr (e
) == FAILURE
)
7909 /* Resolve a charlen structure. */
7912 resolve_charlen (gfc_charlen
*cl
)
7921 specification_expr
= 1;
7923 if (resolve_index_expr (cl
->length
) == FAILURE
)
7925 specification_expr
= 0;
7929 /* "If the character length parameter value evaluates to a negative
7930 value, the length of character entities declared is zero." */
7931 if (cl
->length
&& !gfc_extract_int (cl
->length
, &i
) && i
< 0)
7933 gfc_warning_now ("CHARACTER variable has zero length at %L",
7934 &cl
->length
->where
);
7935 gfc_replace_expr (cl
->length
, gfc_int_expr (0));
7938 /* Check that the character length is not too large. */
7939 k
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
7940 if (cl
->length
&& cl
->length
->expr_type
== EXPR_CONSTANT
7941 && cl
->length
->ts
.type
== BT_INTEGER
7942 && mpz_cmp (cl
->length
->value
.integer
, gfc_integer_kinds
[k
].huge
) > 0)
7944 gfc_error ("String length at %L is too large", &cl
->length
->where
);
7952 /* Test for non-constant shape arrays. */
7955 is_non_constant_shape_array (gfc_symbol
*sym
)
7961 not_constant
= false;
7962 if (sym
->as
!= NULL
)
7964 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
7965 has not been simplified; parameter array references. Do the
7966 simplification now. */
7967 for (i
= 0; i
< sym
->as
->rank
; i
++)
7969 e
= sym
->as
->lower
[i
];
7970 if (e
&& (resolve_index_expr (e
) == FAILURE
7971 || !gfc_is_constant_expr (e
)))
7972 not_constant
= true;
7974 e
= sym
->as
->upper
[i
];
7975 if (e
&& (resolve_index_expr (e
) == FAILURE
7976 || !gfc_is_constant_expr (e
)))
7977 not_constant
= true;
7980 return not_constant
;
7983 /* Given a symbol and an initialization expression, add code to initialize
7984 the symbol to the function entry. */
7986 build_init_assign (gfc_symbol
*sym
, gfc_expr
*init
)
7990 gfc_namespace
*ns
= sym
->ns
;
7992 /* Search for the function namespace if this is a contained
7993 function without an explicit result. */
7994 if (sym
->attr
.function
&& sym
== sym
->result
7995 && sym
->name
!= sym
->ns
->proc_name
->name
)
7998 for (;ns
; ns
= ns
->sibling
)
7999 if (strcmp (ns
->proc_name
->name
, sym
->name
) == 0)
8005 gfc_free_expr (init
);
8009 /* Build an l-value expression for the result. */
8010 lval
= gfc_lval_expr_from_sym (sym
);
8012 /* Add the code at scope entry. */
8013 init_st
= gfc_get_code ();
8014 init_st
->next
= ns
->code
;
8017 /* Assign the default initializer to the l-value. */
8018 init_st
->loc
= sym
->declared_at
;
8019 init_st
->op
= EXEC_INIT_ASSIGN
;
8020 init_st
->expr1
= lval
;
8021 init_st
->expr2
= init
;
8024 /* Assign the default initializer to a derived type variable or result. */
8027 apply_default_init (gfc_symbol
*sym
)
8029 gfc_expr
*init
= NULL
;
8031 if (sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
8034 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
)
8035 init
= gfc_default_initializer (&sym
->ts
);
8040 build_init_assign (sym
, init
);
8043 /* Build an initializer for a local integer, real, complex, logical, or
8044 character variable, based on the command line flags finit-local-zero,
8045 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8046 null if the symbol should not have a default initialization. */
8048 build_default_init_expr (gfc_symbol
*sym
)
8051 gfc_expr
*init_expr
;
8054 /* These symbols should never have a default initialization. */
8055 if ((sym
->attr
.dimension
&& !gfc_is_compile_time_shape (sym
->as
))
8056 || sym
->attr
.external
8058 || sym
->attr
.pointer
8059 || sym
->attr
.in_equivalence
8060 || sym
->attr
.in_common
8063 || sym
->attr
.cray_pointee
8064 || sym
->attr
.cray_pointer
)
8067 /* Now we'll try to build an initializer expression. */
8068 init_expr
= gfc_get_expr ();
8069 init_expr
->expr_type
= EXPR_CONSTANT
;
8070 init_expr
->ts
.type
= sym
->ts
.type
;
8071 init_expr
->ts
.kind
= sym
->ts
.kind
;
8072 init_expr
->where
= sym
->declared_at
;
8074 /* We will only initialize integers, reals, complex, logicals, and
8075 characters, and only if the corresponding command-line flags
8076 were set. Otherwise, we free init_expr and return null. */
8077 switch (sym
->ts
.type
)
8080 if (gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
8081 mpz_init_set_si (init_expr
->value
.integer
,
8082 gfc_option
.flag_init_integer_value
);
8085 gfc_free_expr (init_expr
);
8091 mpfr_init (init_expr
->value
.real
);
8092 switch (gfc_option
.flag_init_real
)
8094 case GFC_INIT_REAL_SNAN
:
8095 init_expr
->is_snan
= 1;
8097 case GFC_INIT_REAL_NAN
:
8098 mpfr_set_nan (init_expr
->value
.real
);
8101 case GFC_INIT_REAL_INF
:
8102 mpfr_set_inf (init_expr
->value
.real
, 1);
8105 case GFC_INIT_REAL_NEG_INF
:
8106 mpfr_set_inf (init_expr
->value
.real
, -1);
8109 case GFC_INIT_REAL_ZERO
:
8110 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
8114 gfc_free_expr (init_expr
);
8122 mpc_init2 (init_expr
->value
.complex, mpfr_get_default_prec());
8124 mpfr_init (init_expr
->value
.complex.r
);
8125 mpfr_init (init_expr
->value
.complex.i
);
8127 switch (gfc_option
.flag_init_real
)
8129 case GFC_INIT_REAL_SNAN
:
8130 init_expr
->is_snan
= 1;
8132 case GFC_INIT_REAL_NAN
:
8133 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
8134 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
8137 case GFC_INIT_REAL_INF
:
8138 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
8139 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
8142 case GFC_INIT_REAL_NEG_INF
:
8143 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
8144 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
8147 case GFC_INIT_REAL_ZERO
:
8149 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
8151 mpfr_set_ui (init_expr
->value
.complex.r
, 0.0, GFC_RND_MODE
);
8152 mpfr_set_ui (init_expr
->value
.complex.i
, 0.0, GFC_RND_MODE
);
8157 gfc_free_expr (init_expr
);
8164 if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_FALSE
)
8165 init_expr
->value
.logical
= 0;
8166 else if (gfc_option
.flag_init_logical
== GFC_INIT_LOGICAL_TRUE
)
8167 init_expr
->value
.logical
= 1;
8170 gfc_free_expr (init_expr
);
8176 /* For characters, the length must be constant in order to
8177 create a default initializer. */
8178 if (gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
8179 && sym
->ts
.u
.cl
->length
8180 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
8182 char_len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
8183 init_expr
->value
.character
.length
= char_len
;
8184 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
8185 for (i
= 0; i
< char_len
; i
++)
8186 init_expr
->value
.character
.string
[i
]
8187 = (unsigned char) gfc_option
.flag_init_character_value
;
8191 gfc_free_expr (init_expr
);
8197 gfc_free_expr (init_expr
);
8203 /* Add an initialization expression to a local variable. */
8205 apply_default_init_local (gfc_symbol
*sym
)
8207 gfc_expr
*init
= NULL
;
8209 /* The symbol should be a variable or a function return value. */
8210 if ((sym
->attr
.flavor
!= FL_VARIABLE
&& !sym
->attr
.function
)
8211 || (sym
->attr
.function
&& sym
->result
!= sym
))
8214 /* Try to build the initializer expression. If we can't initialize
8215 this symbol, then init will be NULL. */
8216 init
= build_default_init_expr (sym
);
8220 /* For saved variables, we don't want to add an initializer at
8221 function entry, so we just add a static initializer. */
8222 if (sym
->attr
.save
|| sym
->ns
->save_all
)
8224 /* Don't clobber an existing initializer! */
8225 gcc_assert (sym
->value
== NULL
);
8230 build_init_assign (sym
, init
);
8233 /* Resolution of common features of flavors variable and procedure. */
8236 resolve_fl_var_and_proc (gfc_symbol
*sym
, int mp_flag
)
8238 /* Constraints on deferred shape variable. */
8239 if (sym
->as
== NULL
|| sym
->as
->type
!= AS_DEFERRED
)
8241 if (sym
->attr
.allocatable
)
8243 if (sym
->attr
.dimension
)
8245 gfc_error ("Allocatable array '%s' at %L must have "
8246 "a deferred shape", sym
->name
, &sym
->declared_at
);
8249 else if (gfc_notify_std (GFC_STD_F2003
, "Scalar object '%s' at %L "
8250 "may not be ALLOCATABLE", sym
->name
,
8251 &sym
->declared_at
) == FAILURE
)
8255 if (sym
->attr
.pointer
&& sym
->attr
.dimension
)
8257 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8258 sym
->name
, &sym
->declared_at
);
8265 if (!mp_flag
&& !sym
->attr
.allocatable
&& !sym
->attr
.pointer
8266 && !sym
->attr
.dummy
&& sym
->ts
.type
!= BT_CLASS
)
8268 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8269 sym
->name
, &sym
->declared_at
);
8277 /* Additional checks for symbols with flavor variable and derived
8278 type. To be called from resolve_fl_variable. */
8281 resolve_fl_variable_derived (gfc_symbol
*sym
, int no_init_flag
)
8283 gcc_assert (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
);
8285 /* Check to see if a derived type is blocked from being host
8286 associated by the presence of another class I symbol in the same
8287 namespace. 14.6.1.3 of the standard and the discussion on
8288 comp.lang.fortran. */
8289 if (sym
->ns
!= sym
->ts
.u
.derived
->ns
8290 && sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
8293 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 0, &s
);
8294 if (s
&& s
->attr
.flavor
!= FL_DERIVED
)
8296 gfc_error ("The type '%s' cannot be host associated at %L "
8297 "because it is blocked by an incompatible object "
8298 "of the same name declared at %L",
8299 sym
->ts
.u
.derived
->name
, &sym
->declared_at
,
8305 /* 4th constraint in section 11.3: "If an object of a type for which
8306 component-initialization is specified (R429) appears in the
8307 specification-part of a module and does not have the ALLOCATABLE
8308 or POINTER attribute, the object shall have the SAVE attribute."
8310 The check for initializers is performed with
8311 has_default_initializer because gfc_default_initializer generates
8312 a hidden default for allocatable components. */
8313 if (!(sym
->value
|| no_init_flag
) && sym
->ns
->proc_name
8314 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
8315 && !sym
->ns
->save_all
&& !sym
->attr
.save
8316 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
8317 && has_default_initializer (sym
->ts
.u
.derived
))
8319 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8320 "default initialization of a component",
8321 sym
->name
, &sym
->declared_at
);
8325 if (sym
->ts
.type
== BT_CLASS
)
8328 if (!gfc_type_is_extensible (sym
->ts
.u
.derived
->components
->ts
.u
.derived
))
8330 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8331 sym
->ts
.u
.derived
->name
, sym
->name
, &sym
->declared_at
);
8336 if (!(sym
->attr
.dummy
|| sym
->attr
.allocatable
|| sym
->attr
.pointer
8337 || sym
->ts
.u
.derived
->components
->attr
.allocatable
8338 || sym
->ts
.u
.derived
->components
->attr
.pointer
))
8340 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8341 "or pointer", sym
->name
, &sym
->declared_at
);
8346 /* Assign default initializer. */
8347 if (!(sym
->value
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
8348 && (!no_init_flag
|| sym
->attr
.intent
== INTENT_OUT
))
8350 sym
->value
= gfc_default_initializer (&sym
->ts
);
8357 /* Resolve symbols with flavor variable. */
8360 resolve_fl_variable (gfc_symbol
*sym
, int mp_flag
)
8362 int no_init_flag
, automatic_flag
;
8364 const char *auto_save_msg
;
8366 auto_save_msg
= "Automatic object '%s' at %L cannot have the "
8369 if (resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
8372 /* Set this flag to check that variables are parameters of all entries.
8373 This check is effected by the call to gfc_resolve_expr through
8374 is_non_constant_shape_array. */
8375 specification_expr
= 1;
8377 if (sym
->ns
->proc_name
8378 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
8379 || sym
->ns
->proc_name
->attr
.is_main_program
)
8380 && !sym
->attr
.use_assoc
8381 && !sym
->attr
.allocatable
8382 && !sym
->attr
.pointer
8383 && is_non_constant_shape_array (sym
))
8385 /* The shape of a main program or module array needs to be
8387 gfc_error ("The module or main program array '%s' at %L must "
8388 "have constant shape", sym
->name
, &sym
->declared_at
);
8389 specification_expr
= 0;
8393 if (sym
->ts
.type
== BT_CHARACTER
)
8395 /* Make sure that character string variables with assumed length are
8397 e
= sym
->ts
.u
.cl
->length
;
8398 if (e
== NULL
&& !sym
->attr
.dummy
&& !sym
->attr
.result
)
8400 gfc_error ("Entity with assumed character length at %L must be a "
8401 "dummy argument or a PARAMETER", &sym
->declared_at
);
8405 if (e
&& sym
->attr
.save
&& !gfc_is_constant_expr (e
))
8407 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
8411 if (!gfc_is_constant_expr (e
)
8412 && !(e
->expr_type
== EXPR_VARIABLE
8413 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
8414 && sym
->ns
->proc_name
8415 && (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
8416 || sym
->ns
->proc_name
->attr
.is_main_program
)
8417 && !sym
->attr
.use_assoc
)
8419 gfc_error ("'%s' at %L must have constant character length "
8420 "in this context", sym
->name
, &sym
->declared_at
);
8425 if (sym
->value
== NULL
&& sym
->attr
.referenced
)
8426 apply_default_init_local (sym
); /* Try to apply a default initialization. */
8428 /* Determine if the symbol may not have an initializer. */
8429 no_init_flag
= automatic_flag
= 0;
8430 if (sym
->attr
.allocatable
|| sym
->attr
.external
|| sym
->attr
.dummy
8431 || sym
->attr
.intrinsic
|| sym
->attr
.result
)
8433 else if (sym
->attr
.dimension
&& !sym
->attr
.pointer
8434 && is_non_constant_shape_array (sym
))
8436 no_init_flag
= automatic_flag
= 1;
8438 /* Also, they must not have the SAVE attribute.
8439 SAVE_IMPLICIT is checked below. */
8440 if (sym
->attr
.save
== SAVE_EXPLICIT
)
8442 gfc_error (auto_save_msg
, sym
->name
, &sym
->declared_at
);
8447 /* Ensure that any initializer is simplified. */
8449 gfc_simplify_expr (sym
->value
, 1);
8451 /* Reject illegal initializers. */
8452 if (!sym
->mark
&& sym
->value
)
8454 if (sym
->attr
.allocatable
)
8455 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
8456 sym
->name
, &sym
->declared_at
);
8457 else if (sym
->attr
.external
)
8458 gfc_error ("External '%s' at %L cannot have an initializer",
8459 sym
->name
, &sym
->declared_at
);
8460 else if (sym
->attr
.dummy
8461 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->attr
.intent
== INTENT_OUT
))
8462 gfc_error ("Dummy '%s' at %L cannot have an initializer",
8463 sym
->name
, &sym
->declared_at
);
8464 else if (sym
->attr
.intrinsic
)
8465 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
8466 sym
->name
, &sym
->declared_at
);
8467 else if (sym
->attr
.result
)
8468 gfc_error ("Function result '%s' at %L cannot have an initializer",
8469 sym
->name
, &sym
->declared_at
);
8470 else if (automatic_flag
)
8471 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
8472 sym
->name
, &sym
->declared_at
);
8479 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
8480 return resolve_fl_variable_derived (sym
, no_init_flag
);
8486 /* Resolve a procedure. */
8489 resolve_fl_procedure (gfc_symbol
*sym
, int mp_flag
)
8491 gfc_formal_arglist
*arg
;
8493 if (sym
->attr
.ambiguous_interfaces
&& !sym
->attr
.referenced
)
8494 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
8495 "interfaces", sym
->name
, &sym
->declared_at
);
8497 if (sym
->attr
.function
8498 && resolve_fl_var_and_proc (sym
, mp_flag
) == FAILURE
)
8501 if (sym
->ts
.type
== BT_CHARACTER
)
8503 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
8505 if (cl
&& cl
->length
&& gfc_is_constant_expr (cl
->length
)
8506 && resolve_charlen (cl
) == FAILURE
)
8509 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
8511 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
8513 gfc_error ("Character-valued statement function '%s' at %L must "
8514 "have constant length", sym
->name
, &sym
->declared_at
);
8518 if (sym
->attr
.external
&& sym
->formal
== NULL
8519 && cl
&& cl
->length
&& cl
->length
->expr_type
!= EXPR_CONSTANT
)
8521 gfc_error ("Automatic character length function '%s' at %L must "
8522 "have an explicit interface", sym
->name
,
8529 /* Ensure that derived type for are not of a private type. Internal
8530 module procedures are excluded by 2.2.3.3 - i.e., they are not
8531 externally accessible and can access all the objects accessible in
8533 if (!(sym
->ns
->parent
8534 && sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
8535 && gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
8537 gfc_interface
*iface
;
8539 for (arg
= sym
->formal
; arg
; arg
= arg
->next
)
8542 && arg
->sym
->ts
.type
== BT_DERIVED
8543 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
8544 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
8545 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
8546 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: '%s' is of a "
8547 "PRIVATE type and cannot be a dummy argument"
8548 " of '%s', which is PUBLIC at %L",
8549 arg
->sym
->name
, sym
->name
, &sym
->declared_at
)
8552 /* Stop this message from recurring. */
8553 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
8558 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8559 PRIVATE to the containing module. */
8560 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
8562 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
8565 && arg
->sym
->ts
.type
== BT_DERIVED
8566 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
8567 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
8568 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
8569 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
8570 "'%s' in PUBLIC interface '%s' at %L "
8571 "takes dummy arguments of '%s' which is "
8572 "PRIVATE", iface
->sym
->name
, sym
->name
,
8573 &iface
->sym
->declared_at
,
8574 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
8576 /* Stop this message from recurring. */
8577 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
8583 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8584 PRIVATE to the containing module. */
8585 for (iface
= sym
->generic
; iface
; iface
= iface
->next
)
8587 for (arg
= iface
->sym
->formal
; arg
; arg
= arg
->next
)
8590 && arg
->sym
->ts
.type
== BT_DERIVED
8591 && !arg
->sym
->ts
.u
.derived
->attr
.use_assoc
8592 && !gfc_check_access (arg
->sym
->ts
.u
.derived
->attr
.access
,
8593 arg
->sym
->ts
.u
.derived
->ns
->default_access
)
8594 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure "
8595 "'%s' in PUBLIC interface '%s' at %L "
8596 "takes dummy arguments of '%s' which is "
8597 "PRIVATE", iface
->sym
->name
, sym
->name
,
8598 &iface
->sym
->declared_at
,
8599 gfc_typename (&arg
->sym
->ts
)) == FAILURE
)
8601 /* Stop this message from recurring. */
8602 arg
->sym
->ts
.u
.derived
->attr
.access
= ACCESS_PUBLIC
;
8609 if (sym
->attr
.function
&& sym
->value
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
8610 && !sym
->attr
.proc_pointer
)
8612 gfc_error ("Function '%s' at %L cannot have an initializer",
8613 sym
->name
, &sym
->declared_at
);
8617 /* An external symbol may not have an initializer because it is taken to be
8618 a procedure. Exception: Procedure Pointers. */
8619 if (sym
->attr
.external
&& sym
->value
&& !sym
->attr
.proc_pointer
)
8621 gfc_error ("External object '%s' at %L may not have an initializer",
8622 sym
->name
, &sym
->declared_at
);
8626 /* An elemental function is required to return a scalar 12.7.1 */
8627 if (sym
->attr
.elemental
&& sym
->attr
.function
&& sym
->as
)
8629 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
8630 "result", sym
->name
, &sym
->declared_at
);
8631 /* Reset so that the error only occurs once. */
8632 sym
->attr
.elemental
= 0;
8636 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
8637 char-len-param shall not be array-valued, pointer-valued, recursive
8638 or pure. ....snip... A character value of * may only be used in the
8639 following ways: (i) Dummy arg of procedure - dummy associates with
8640 actual length; (ii) To declare a named constant; or (iii) External
8641 function - but length must be declared in calling scoping unit. */
8642 if (sym
->attr
.function
8643 && sym
->ts
.type
== BT_CHARACTER
8644 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
== NULL
)
8646 if ((sym
->as
&& sym
->as
->rank
) || (sym
->attr
.pointer
)
8647 || (sym
->attr
.recursive
) || (sym
->attr
.pure
))
8649 if (sym
->as
&& sym
->as
->rank
)
8650 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8651 "array-valued", sym
->name
, &sym
->declared_at
);
8653 if (sym
->attr
.pointer
)
8654 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8655 "pointer-valued", sym
->name
, &sym
->declared_at
);
8658 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8659 "pure", sym
->name
, &sym
->declared_at
);
8661 if (sym
->attr
.recursive
)
8662 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8663 "recursive", sym
->name
, &sym
->declared_at
);
8668 /* Appendix B.2 of the standard. Contained functions give an
8669 error anyway. Fixed-form is likely to be F77/legacy. */
8670 if (!sym
->attr
.contained
&& gfc_current_form
!= FORM_FIXED
)
8671 gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
8672 "CHARACTER(*) function '%s' at %L",
8673 sym
->name
, &sym
->declared_at
);
8676 if (sym
->attr
.is_bind_c
&& sym
->attr
.is_c_interop
!= 1)
8678 gfc_formal_arglist
*curr_arg
;
8679 int has_non_interop_arg
= 0;
8681 if (verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
8682 sym
->common_block
) == FAILURE
)
8684 /* Clear these to prevent looking at them again if there was an
8686 sym
->attr
.is_bind_c
= 0;
8687 sym
->attr
.is_c_interop
= 0;
8688 sym
->ts
.is_c_interop
= 0;
8692 /* So far, no errors have been found. */
8693 sym
->attr
.is_c_interop
= 1;
8694 sym
->ts
.is_c_interop
= 1;
8697 curr_arg
= sym
->formal
;
8698 while (curr_arg
!= NULL
)
8700 /* Skip implicitly typed dummy args here. */
8701 if (curr_arg
->sym
->attr
.implicit_type
== 0)
8702 if (verify_c_interop_param (curr_arg
->sym
) == FAILURE
)
8703 /* If something is found to fail, record the fact so we
8704 can mark the symbol for the procedure as not being
8705 BIND(C) to try and prevent multiple errors being
8707 has_non_interop_arg
= 1;
8709 curr_arg
= curr_arg
->next
;
8712 /* See if any of the arguments were not interoperable and if so, clear
8713 the procedure symbol to prevent duplicate error messages. */
8714 if (has_non_interop_arg
!= 0)
8716 sym
->attr
.is_c_interop
= 0;
8717 sym
->ts
.is_c_interop
= 0;
8718 sym
->attr
.is_bind_c
= 0;
8722 if (!sym
->attr
.proc_pointer
)
8724 if (sym
->attr
.save
== SAVE_EXPLICIT
)
8726 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
8727 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
8730 if (sym
->attr
.intent
)
8732 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
8733 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
8736 if (sym
->attr
.subroutine
&& sym
->attr
.result
)
8738 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
8739 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
8742 if (sym
->attr
.external
&& sym
->attr
.function
8743 && ((sym
->attr
.if_source
== IFSRC_DECL
&& !sym
->attr
.procedure
)
8744 || sym
->attr
.contained
))
8746 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
8747 "in '%s' at %L", sym
->name
, &sym
->declared_at
);
8750 if (strcmp ("ppr@", sym
->name
) == 0)
8752 gfc_error ("Procedure pointer result '%s' at %L "
8753 "is missing the pointer attribute",
8754 sym
->ns
->proc_name
->name
, &sym
->declared_at
);
8763 /* Resolve a list of finalizer procedures. That is, after they have hopefully
8764 been defined and we now know their defined arguments, check that they fulfill
8765 the requirements of the standard for procedures used as finalizers. */
8768 gfc_resolve_finalizers (gfc_symbol
* derived
)
8770 gfc_finalizer
* list
;
8771 gfc_finalizer
** prev_link
; /* For removing wrong entries from the list. */
8772 gfc_try result
= SUCCESS
;
8773 bool seen_scalar
= false;
8775 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->finalizers
)
8778 /* Walk over the list of finalizer-procedures, check them, and if any one
8779 does not fit in with the standard's definition, print an error and remove
8780 it from the list. */
8781 prev_link
= &derived
->f2k_derived
->finalizers
;
8782 for (list
= derived
->f2k_derived
->finalizers
; list
; list
= *prev_link
)
8788 /* Skip this finalizer if we already resolved it. */
8789 if (list
->proc_tree
)
8791 prev_link
= &(list
->next
);
8795 /* Check this exists and is a SUBROUTINE. */
8796 if (!list
->proc_sym
->attr
.subroutine
)
8798 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
8799 list
->proc_sym
->name
, &list
->where
);
8803 /* We should have exactly one argument. */
8804 if (!list
->proc_sym
->formal
|| list
->proc_sym
->formal
->next
)
8806 gfc_error ("FINAL procedure at %L must have exactly one argument",
8810 arg
= list
->proc_sym
->formal
->sym
;
8812 /* This argument must be of our type. */
8813 if (arg
->ts
.type
!= BT_DERIVED
|| arg
->ts
.u
.derived
!= derived
)
8815 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
8816 &arg
->declared_at
, derived
->name
);
8820 /* It must neither be a pointer nor allocatable nor optional. */
8821 if (arg
->attr
.pointer
)
8823 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
8827 if (arg
->attr
.allocatable
)
8829 gfc_error ("Argument of FINAL procedure at %L must not be"
8830 " ALLOCATABLE", &arg
->declared_at
);
8833 if (arg
->attr
.optional
)
8835 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
8840 /* It must not be INTENT(OUT). */
8841 if (arg
->attr
.intent
== INTENT_OUT
)
8843 gfc_error ("Argument of FINAL procedure at %L must not be"
8844 " INTENT(OUT)", &arg
->declared_at
);
8848 /* Warn if the procedure is non-scalar and not assumed shape. */
8849 if (gfc_option
.warn_surprising
&& arg
->as
&& arg
->as
->rank
> 0
8850 && arg
->as
->type
!= AS_ASSUMED_SHAPE
)
8851 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
8852 " shape argument", &arg
->declared_at
);
8854 /* Check that it does not match in kind and rank with a FINAL procedure
8855 defined earlier. To really loop over the *earlier* declarations,
8856 we need to walk the tail of the list as new ones were pushed at the
8858 /* TODO: Handle kind parameters once they are implemented. */
8859 my_rank
= (arg
->as
? arg
->as
->rank
: 0);
8860 for (i
= list
->next
; i
; i
= i
->next
)
8862 /* Argument list might be empty; that is an error signalled earlier,
8863 but we nevertheless continued resolving. */
8864 if (i
->proc_sym
->formal
)
8866 gfc_symbol
* i_arg
= i
->proc_sym
->formal
->sym
;
8867 const int i_rank
= (i_arg
->as
? i_arg
->as
->rank
: 0);
8868 if (i_rank
== my_rank
)
8870 gfc_error ("FINAL procedure '%s' declared at %L has the same"
8871 " rank (%d) as '%s'",
8872 list
->proc_sym
->name
, &list
->where
, my_rank
,
8879 /* Is this the/a scalar finalizer procedure? */
8880 if (!arg
->as
|| arg
->as
->rank
== 0)
8883 /* Find the symtree for this procedure. */
8884 gcc_assert (!list
->proc_tree
);
8885 list
->proc_tree
= gfc_find_sym_in_symtree (list
->proc_sym
);
8887 prev_link
= &list
->next
;
8890 /* Remove wrong nodes immediately from the list so we don't risk any
8891 troubles in the future when they might fail later expectations. */
8895 *prev_link
= list
->next
;
8896 gfc_free_finalizer (i
);
8899 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
8900 were nodes in the list, must have been for arrays. It is surely a good
8901 idea to have a scalar version there if there's something to finalize. */
8902 if (gfc_option
.warn_surprising
&& result
== SUCCESS
&& !seen_scalar
)
8903 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
8904 " defined at %L, suggest also scalar one",
8905 derived
->name
, &derived
->declared_at
);
8907 /* TODO: Remove this error when finalization is finished. */
8908 gfc_error ("Finalization at %L is not yet implemented",
8909 &derived
->declared_at
);
8915 /* Check that it is ok for the typebound procedure proc to override the
8919 check_typebound_override (gfc_symtree
* proc
, gfc_symtree
* old
)
8922 const gfc_symbol
* proc_target
;
8923 const gfc_symbol
* old_target
;
8924 unsigned proc_pass_arg
, old_pass_arg
, argpos
;
8925 gfc_formal_arglist
* proc_formal
;
8926 gfc_formal_arglist
* old_formal
;
8928 /* This procedure should only be called for non-GENERIC proc. */
8929 gcc_assert (!proc
->n
.tb
->is_generic
);
8931 /* If the overwritten procedure is GENERIC, this is an error. */
8932 if (old
->n
.tb
->is_generic
)
8934 gfc_error ("Can't overwrite GENERIC '%s' at %L",
8935 old
->name
, &proc
->n
.tb
->where
);
8939 where
= proc
->n
.tb
->where
;
8940 proc_target
= proc
->n
.tb
->u
.specific
->n
.sym
;
8941 old_target
= old
->n
.tb
->u
.specific
->n
.sym
;
8943 /* Check that overridden binding is not NON_OVERRIDABLE. */
8944 if (old
->n
.tb
->non_overridable
)
8946 gfc_error ("'%s' at %L overrides a procedure binding declared"
8947 " NON_OVERRIDABLE", proc
->name
, &where
);
8951 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
8952 if (!old
->n
.tb
->deferred
&& proc
->n
.tb
->deferred
)
8954 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
8955 " non-DEFERRED binding", proc
->name
, &where
);
8959 /* If the overridden binding is PURE, the overriding must be, too. */
8960 if (old_target
->attr
.pure
&& !proc_target
->attr
.pure
)
8962 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
8963 proc
->name
, &where
);
8967 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
8968 is not, the overriding must not be either. */
8969 if (old_target
->attr
.elemental
&& !proc_target
->attr
.elemental
)
8971 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
8972 " ELEMENTAL", proc
->name
, &where
);
8975 if (!old_target
->attr
.elemental
&& proc_target
->attr
.elemental
)
8977 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
8978 " be ELEMENTAL, either", proc
->name
, &where
);
8982 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
8984 if (old_target
->attr
.subroutine
&& !proc_target
->attr
.subroutine
)
8986 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
8987 " SUBROUTINE", proc
->name
, &where
);
8991 /* If the overridden binding is a FUNCTION, the overriding must also be a
8992 FUNCTION and have the same characteristics. */
8993 if (old_target
->attr
.function
)
8995 if (!proc_target
->attr
.function
)
8997 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
8998 " FUNCTION", proc
->name
, &where
);
9002 /* FIXME: Do more comprehensive checking (including, for instance, the
9003 rank and array-shape). */
9004 gcc_assert (proc_target
->result
&& old_target
->result
);
9005 if (!gfc_compare_types (&proc_target
->result
->ts
,
9006 &old_target
->result
->ts
))
9008 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9009 " matching result types", proc
->name
, &where
);
9014 /* If the overridden binding is PUBLIC, the overriding one must not be
9016 if (old
->n
.tb
->access
== ACCESS_PUBLIC
9017 && proc
->n
.tb
->access
== ACCESS_PRIVATE
)
9019 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9020 " PRIVATE", proc
->name
, &where
);
9024 /* Compare the formal argument lists of both procedures. This is also abused
9025 to find the position of the passed-object dummy arguments of both
9026 bindings as at least the overridden one might not yet be resolved and we
9027 need those positions in the check below. */
9028 proc_pass_arg
= old_pass_arg
= 0;
9029 if (!proc
->n
.tb
->nopass
&& !proc
->n
.tb
->pass_arg
)
9031 if (!old
->n
.tb
->nopass
&& !old
->n
.tb
->pass_arg
)
9034 for (proc_formal
= proc_target
->formal
, old_formal
= old_target
->formal
;
9035 proc_formal
&& old_formal
;
9036 proc_formal
= proc_formal
->next
, old_formal
= old_formal
->next
)
9038 if (proc
->n
.tb
->pass_arg
9039 && !strcmp (proc
->n
.tb
->pass_arg
, proc_formal
->sym
->name
))
9040 proc_pass_arg
= argpos
;
9041 if (old
->n
.tb
->pass_arg
9042 && !strcmp (old
->n
.tb
->pass_arg
, old_formal
->sym
->name
))
9043 old_pass_arg
= argpos
;
9045 /* Check that the names correspond. */
9046 if (strcmp (proc_formal
->sym
->name
, old_formal
->sym
->name
))
9048 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9049 " to match the corresponding argument of the overridden"
9050 " procedure", proc_formal
->sym
->name
, proc
->name
, &where
,
9051 old_formal
->sym
->name
);
9055 /* Check that the types correspond if neither is the passed-object
9057 /* FIXME: Do more comprehensive testing here. */
9058 if (proc_pass_arg
!= argpos
&& old_pass_arg
!= argpos
9059 && !gfc_compare_types (&proc_formal
->sym
->ts
, &old_formal
->sym
->ts
))
9061 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
9062 " in respect to the overridden procedure",
9063 proc_formal
->sym
->name
, proc
->name
, &where
);
9069 if (proc_formal
|| old_formal
)
9071 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9072 " the overridden procedure", proc
->name
, &where
);
9076 /* If the overridden binding is NOPASS, the overriding one must also be
9078 if (old
->n
.tb
->nopass
&& !proc
->n
.tb
->nopass
)
9080 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9081 " NOPASS", proc
->name
, &where
);
9085 /* If the overridden binding is PASS(x), the overriding one must also be
9086 PASS and the passed-object dummy arguments must correspond. */
9087 if (!old
->n
.tb
->nopass
)
9089 if (proc
->n
.tb
->nopass
)
9091 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9092 " PASS", proc
->name
, &where
);
9096 if (proc_pass_arg
!= old_pass_arg
)
9098 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9099 " the same position as the passed-object dummy argument of"
9100 " the overridden procedure", proc
->name
, &where
);
9109 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9112 check_generic_tbp_ambiguity (gfc_tbp_generic
* t1
, gfc_tbp_generic
* t2
,
9113 const char* generic_name
, locus where
)
9118 gcc_assert (t1
->specific
&& t2
->specific
);
9119 gcc_assert (!t1
->specific
->is_generic
);
9120 gcc_assert (!t2
->specific
->is_generic
);
9122 sym1
= t1
->specific
->u
.specific
->n
.sym
;
9123 sym2
= t2
->specific
->u
.specific
->n
.sym
;
9128 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9129 if (sym1
->attr
.subroutine
!= sym2
->attr
.subroutine
9130 || sym1
->attr
.function
!= sym2
->attr
.function
)
9132 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9133 " GENERIC '%s' at %L",
9134 sym1
->name
, sym2
->name
, generic_name
, &where
);
9138 /* Compare the interfaces. */
9139 if (gfc_compare_interfaces (sym1
, sym2
, NULL
, 1, 0, NULL
, 0))
9141 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9142 sym1
->name
, sym2
->name
, generic_name
, &where
);
9150 /* Worker function for resolving a generic procedure binding; this is used to
9151 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9153 The difference between those cases is finding possible inherited bindings
9154 that are overridden, as one has to look for them in tb_sym_root,
9155 tb_uop_root or tb_op, respectively. Thus the caller must already find
9156 the super-type and set p->overridden correctly. */
9159 resolve_tb_generic_targets (gfc_symbol
* super_type
,
9160 gfc_typebound_proc
* p
, const char* name
)
9162 gfc_tbp_generic
* target
;
9163 gfc_symtree
* first_target
;
9164 gfc_symtree
* inherited
;
9166 gcc_assert (p
&& p
->is_generic
);
9168 /* Try to find the specific bindings for the symtrees in our target-list. */
9169 gcc_assert (p
->u
.generic
);
9170 for (target
= p
->u
.generic
; target
; target
= target
->next
)
9171 if (!target
->specific
)
9173 gfc_typebound_proc
* overridden_tbp
;
9175 const char* target_name
;
9177 target_name
= target
->specific_st
->name
;
9179 /* Defined for this type directly. */
9180 if (target
->specific_st
->n
.tb
)
9182 target
->specific
= target
->specific_st
->n
.tb
;
9183 goto specific_found
;
9186 /* Look for an inherited specific binding. */
9189 inherited
= gfc_find_typebound_proc (super_type
, NULL
, target_name
,
9194 gcc_assert (inherited
->n
.tb
);
9195 target
->specific
= inherited
->n
.tb
;
9196 goto specific_found
;
9200 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9201 " at %L", target_name
, name
, &p
->where
);
9204 /* Once we've found the specific binding, check it is not ambiguous with
9205 other specifics already found or inherited for the same GENERIC. */
9207 gcc_assert (target
->specific
);
9209 /* This must really be a specific binding! */
9210 if (target
->specific
->is_generic
)
9212 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9213 " '%s' is GENERIC, too", name
, &p
->where
, target_name
);
9217 /* Check those already resolved on this type directly. */
9218 for (g
= p
->u
.generic
; g
; g
= g
->next
)
9219 if (g
!= target
&& g
->specific
9220 && check_generic_tbp_ambiguity (target
, g
, name
, p
->where
)
9224 /* Check for ambiguity with inherited specific targets. */
9225 for (overridden_tbp
= p
->overridden
; overridden_tbp
;
9226 overridden_tbp
= overridden_tbp
->overridden
)
9227 if (overridden_tbp
->is_generic
)
9229 for (g
= overridden_tbp
->u
.generic
; g
; g
= g
->next
)
9231 gcc_assert (g
->specific
);
9232 if (check_generic_tbp_ambiguity (target
, g
,
9233 name
, p
->where
) == FAILURE
)
9239 /* If we attempt to "overwrite" a specific binding, this is an error. */
9240 if (p
->overridden
&& !p
->overridden
->is_generic
)
9242 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9243 " the same name", name
, &p
->where
);
9247 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9248 all must have the same attributes here. */
9249 first_target
= p
->u
.generic
->specific
->u
.specific
;
9250 gcc_assert (first_target
);
9251 p
->subroutine
= first_target
->n
.sym
->attr
.subroutine
;
9252 p
->function
= first_target
->n
.sym
->attr
.function
;
9258 /* Resolve a GENERIC procedure binding for a derived type. */
9261 resolve_typebound_generic (gfc_symbol
* derived
, gfc_symtree
* st
)
9263 gfc_symbol
* super_type
;
9265 /* Find the overridden binding if any. */
9266 st
->n
.tb
->overridden
= NULL
;
9267 super_type
= gfc_get_derived_super_type (derived
);
9270 gfc_symtree
* overridden
;
9271 overridden
= gfc_find_typebound_proc (super_type
, NULL
, st
->name
,
9274 if (overridden
&& overridden
->n
.tb
)
9275 st
->n
.tb
->overridden
= overridden
->n
.tb
;
9278 /* Resolve using worker function. */
9279 return resolve_tb_generic_targets (super_type
, st
->n
.tb
, st
->name
);
9283 /* Retrieve the target-procedure of an operator binding and do some checks in
9284 common for intrinsic and user-defined type-bound operators. */
9287 get_checked_tb_operator_target (gfc_tbp_generic
* target
, locus where
)
9289 gfc_symbol
* target_proc
;
9291 gcc_assert (target
->specific
&& !target
->specific
->is_generic
);
9292 target_proc
= target
->specific
->u
.specific
->n
.sym
;
9293 gcc_assert (target_proc
);
9295 /* All operator bindings must have a passed-object dummy argument. */
9296 if (target
->specific
->nopass
)
9298 gfc_error ("Type-bound operator at %L can't be NOPASS", &where
);
9306 /* Resolve a type-bound intrinsic operator. */
9309 resolve_typebound_intrinsic_op (gfc_symbol
* derived
, gfc_intrinsic_op op
,
9310 gfc_typebound_proc
* p
)
9312 gfc_symbol
* super_type
;
9313 gfc_tbp_generic
* target
;
9315 /* If there's already an error here, do nothing (but don't fail again). */
9319 /* Operators should always be GENERIC bindings. */
9320 gcc_assert (p
->is_generic
);
9322 /* Look for an overridden binding. */
9323 super_type
= gfc_get_derived_super_type (derived
);
9324 if (super_type
&& super_type
->f2k_derived
)
9325 p
->overridden
= gfc_find_typebound_intrinsic_op (super_type
, NULL
,
9328 p
->overridden
= NULL
;
9330 /* Resolve general GENERIC properties using worker function. */
9331 if (resolve_tb_generic_targets (super_type
, p
, gfc_op2string (op
)) == FAILURE
)
9334 /* Check the targets to be procedures of correct interface. */
9335 for (target
= p
->u
.generic
; target
; target
= target
->next
)
9337 gfc_symbol
* target_proc
;
9339 target_proc
= get_checked_tb_operator_target (target
, p
->where
);
9343 if (!gfc_check_operator_interface (target_proc
, op
, p
->where
))
9355 /* Resolve a type-bound user operator (tree-walker callback). */
9357 static gfc_symbol
* resolve_bindings_derived
;
9358 static gfc_try resolve_bindings_result
;
9360 static gfc_try
check_uop_procedure (gfc_symbol
* sym
, locus where
);
9363 resolve_typebound_user_op (gfc_symtree
* stree
)
9365 gfc_symbol
* super_type
;
9366 gfc_tbp_generic
* target
;
9368 gcc_assert (stree
&& stree
->n
.tb
);
9370 if (stree
->n
.tb
->error
)
9373 /* Operators should always be GENERIC bindings. */
9374 gcc_assert (stree
->n
.tb
->is_generic
);
9376 /* Find overridden procedure, if any. */
9377 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
9378 if (super_type
&& super_type
->f2k_derived
)
9380 gfc_symtree
* overridden
;
9381 overridden
= gfc_find_typebound_user_op (super_type
, NULL
,
9382 stree
->name
, true, NULL
);
9384 if (overridden
&& overridden
->n
.tb
)
9385 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
9388 stree
->n
.tb
->overridden
= NULL
;
9390 /* Resolve basically using worker function. */
9391 if (resolve_tb_generic_targets (super_type
, stree
->n
.tb
, stree
->name
)
9395 /* Check the targets to be functions of correct interface. */
9396 for (target
= stree
->n
.tb
->u
.generic
; target
; target
= target
->next
)
9398 gfc_symbol
* target_proc
;
9400 target_proc
= get_checked_tb_operator_target (target
, stree
->n
.tb
->where
);
9404 if (check_uop_procedure (target_proc
, stree
->n
.tb
->where
) == FAILURE
)
9411 resolve_bindings_result
= FAILURE
;
9412 stree
->n
.tb
->error
= 1;
9416 /* Resolve the type-bound procedures for a derived type. */
9419 resolve_typebound_procedure (gfc_symtree
* stree
)
9424 gfc_symbol
* super_type
;
9425 gfc_component
* comp
;
9429 /* Undefined specific symbol from GENERIC target definition. */
9433 if (stree
->n
.tb
->error
)
9436 /* If this is a GENERIC binding, use that routine. */
9437 if (stree
->n
.tb
->is_generic
)
9439 if (resolve_typebound_generic (resolve_bindings_derived
, stree
)
9445 /* Get the target-procedure to check it. */
9446 gcc_assert (!stree
->n
.tb
->is_generic
);
9447 gcc_assert (stree
->n
.tb
->u
.specific
);
9448 proc
= stree
->n
.tb
->u
.specific
->n
.sym
;
9449 where
= stree
->n
.tb
->where
;
9451 /* Default access should already be resolved from the parser. */
9452 gcc_assert (stree
->n
.tb
->access
!= ACCESS_UNKNOWN
);
9454 /* It should be a module procedure or an external procedure with explicit
9455 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
9456 if ((!proc
->attr
.subroutine
&& !proc
->attr
.function
)
9457 || (proc
->attr
.proc
!= PROC_MODULE
9458 && proc
->attr
.if_source
!= IFSRC_IFBODY
)
9459 || (proc
->attr
.abstract
&& !stree
->n
.tb
->deferred
))
9461 gfc_error ("'%s' must be a module procedure or an external procedure with"
9462 " an explicit interface at %L", proc
->name
, &where
);
9465 stree
->n
.tb
->subroutine
= proc
->attr
.subroutine
;
9466 stree
->n
.tb
->function
= proc
->attr
.function
;
9468 /* Find the super-type of the current derived type. We could do this once and
9469 store in a global if speed is needed, but as long as not I believe this is
9470 more readable and clearer. */
9471 super_type
= gfc_get_derived_super_type (resolve_bindings_derived
);
9473 /* If PASS, resolve and check arguments if not already resolved / loaded
9474 from a .mod file. */
9475 if (!stree
->n
.tb
->nopass
&& stree
->n
.tb
->pass_arg_num
== 0)
9477 if (stree
->n
.tb
->pass_arg
)
9479 gfc_formal_arglist
* i
;
9481 /* If an explicit passing argument name is given, walk the arg-list
9485 stree
->n
.tb
->pass_arg_num
= 1;
9486 for (i
= proc
->formal
; i
; i
= i
->next
)
9488 if (!strcmp (i
->sym
->name
, stree
->n
.tb
->pass_arg
))
9493 ++stree
->n
.tb
->pass_arg_num
;
9498 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
9500 proc
->name
, stree
->n
.tb
->pass_arg
, &where
,
9501 stree
->n
.tb
->pass_arg
);
9507 /* Otherwise, take the first one; there should in fact be at least
9509 stree
->n
.tb
->pass_arg_num
= 1;
9512 gfc_error ("Procedure '%s' with PASS at %L must have at"
9513 " least one argument", proc
->name
, &where
);
9516 me_arg
= proc
->formal
->sym
;
9519 /* Now check that the argument-type matches. */
9520 gcc_assert (me_arg
);
9521 if (me_arg
->ts
.type
!= BT_CLASS
)
9523 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9524 " at %L", proc
->name
, &where
);
9528 if (me_arg
->ts
.u
.derived
->components
->ts
.u
.derived
9529 != resolve_bindings_derived
)
9531 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9532 " the derived-type '%s'", me_arg
->name
, proc
->name
,
9533 me_arg
->name
, &where
, resolve_bindings_derived
->name
);
9539 /* If we are extending some type, check that we don't override a procedure
9540 flagged NON_OVERRIDABLE. */
9541 stree
->n
.tb
->overridden
= NULL
;
9544 gfc_symtree
* overridden
;
9545 overridden
= gfc_find_typebound_proc (super_type
, NULL
,
9546 stree
->name
, true, NULL
);
9548 if (overridden
&& overridden
->n
.tb
)
9549 stree
->n
.tb
->overridden
= overridden
->n
.tb
;
9551 if (overridden
&& check_typebound_override (stree
, overridden
) == FAILURE
)
9555 /* See if there's a name collision with a component directly in this type. */
9556 for (comp
= resolve_bindings_derived
->components
; comp
; comp
= comp
->next
)
9557 if (!strcmp (comp
->name
, stree
->name
))
9559 gfc_error ("Procedure '%s' at %L has the same name as a component of"
9561 stree
->name
, &where
, resolve_bindings_derived
->name
);
9565 /* Try to find a name collision with an inherited component. */
9566 if (super_type
&& gfc_find_component (super_type
, stree
->name
, true, true))
9568 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
9569 " component of '%s'",
9570 stree
->name
, &where
, resolve_bindings_derived
->name
);
9574 stree
->n
.tb
->error
= 0;
9578 resolve_bindings_result
= FAILURE
;
9579 stree
->n
.tb
->error
= 1;
9583 resolve_typebound_procedures (gfc_symbol
* derived
)
9587 if (!derived
->f2k_derived
|| !derived
->f2k_derived
->tb_sym_root
)
9590 resolve_bindings_derived
= derived
;
9591 resolve_bindings_result
= SUCCESS
;
9593 if (derived
->f2k_derived
->tb_sym_root
)
9594 gfc_traverse_symtree (derived
->f2k_derived
->tb_sym_root
,
9595 &resolve_typebound_procedure
);
9597 if (derived
->f2k_derived
->tb_uop_root
)
9598 gfc_traverse_symtree (derived
->f2k_derived
->tb_uop_root
,
9599 &resolve_typebound_user_op
);
9601 for (op
= 0; op
!= GFC_INTRINSIC_OPS
; ++op
)
9603 gfc_typebound_proc
* p
= derived
->f2k_derived
->tb_op
[op
];
9604 if (p
&& resolve_typebound_intrinsic_op (derived
, (gfc_intrinsic_op
) op
,
9606 resolve_bindings_result
= FAILURE
;
9609 return resolve_bindings_result
;
9613 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
9614 to give all identical derived types the same backend_decl. */
9616 add_dt_to_dt_list (gfc_symbol
*derived
)
9618 gfc_dt_list
*dt_list
;
9620 for (dt_list
= gfc_derived_types
; dt_list
; dt_list
= dt_list
->next
)
9621 if (derived
== dt_list
->derived
)
9624 if (dt_list
== NULL
)
9626 dt_list
= gfc_get_dt_list ();
9627 dt_list
->next
= gfc_derived_types
;
9628 dt_list
->derived
= derived
;
9629 gfc_derived_types
= dt_list
;
9634 /* Ensure that a derived-type is really not abstract, meaning that every
9635 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
9638 ensure_not_abstract_walker (gfc_symbol
* sub
, gfc_symtree
* st
)
9643 if (ensure_not_abstract_walker (sub
, st
->left
) == FAILURE
)
9645 if (ensure_not_abstract_walker (sub
, st
->right
) == FAILURE
)
9648 if (st
->n
.tb
&& st
->n
.tb
->deferred
)
9650 gfc_symtree
* overriding
;
9651 overriding
= gfc_find_typebound_proc (sub
, NULL
, st
->name
, true, NULL
);
9652 gcc_assert (overriding
&& overriding
->n
.tb
);
9653 if (overriding
->n
.tb
->deferred
)
9655 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
9656 " '%s' is DEFERRED and not overridden",
9657 sub
->name
, &sub
->declared_at
, st
->name
);
9666 ensure_not_abstract (gfc_symbol
* sub
, gfc_symbol
* ancestor
)
9668 /* The algorithm used here is to recursively travel up the ancestry of sub
9669 and for each ancestor-type, check all bindings. If any of them is
9670 DEFERRED, look it up starting from sub and see if the found (overriding)
9671 binding is not DEFERRED.
9672 This is not the most efficient way to do this, but it should be ok and is
9673 clearer than something sophisticated. */
9675 gcc_assert (ancestor
&& ancestor
->attr
.abstract
&& !sub
->attr
.abstract
);
9677 /* Walk bindings of this ancestor. */
9678 if (ancestor
->f2k_derived
)
9681 t
= ensure_not_abstract_walker (sub
, ancestor
->f2k_derived
->tb_sym_root
);
9686 /* Find next ancestor type and recurse on it. */
9687 ancestor
= gfc_get_derived_super_type (ancestor
);
9689 return ensure_not_abstract (sub
, ancestor
);
9695 static void resolve_symbol (gfc_symbol
*sym
);
9698 /* Resolve the components of a derived type. */
9701 resolve_fl_derived (gfc_symbol
*sym
)
9703 gfc_symbol
* super_type
;
9707 super_type
= gfc_get_derived_super_type (sym
);
9709 /* Ensure the extended type gets resolved before we do. */
9710 if (super_type
&& resolve_fl_derived (super_type
) == FAILURE
)
9713 /* An ABSTRACT type must be extensible. */
9714 if (sym
->attr
.abstract
&& !gfc_type_is_extensible (sym
))
9716 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
9717 sym
->name
, &sym
->declared_at
);
9721 for (c
= sym
->components
; c
!= NULL
; c
= c
->next
)
9723 if (c
->attr
.proc_pointer
&& c
->ts
.interface
)
9725 if (c
->ts
.interface
->attr
.procedure
)
9726 gfc_error ("Interface '%s', used by procedure pointer component "
9727 "'%s' at %L, is declared in a later PROCEDURE statement",
9728 c
->ts
.interface
->name
, c
->name
, &c
->loc
);
9730 /* Get the attributes from the interface (now resolved). */
9731 if (c
->ts
.interface
->attr
.if_source
9732 || c
->ts
.interface
->attr
.intrinsic
)
9734 gfc_symbol
*ifc
= c
->ts
.interface
;
9736 if (ifc
->formal
&& !ifc
->formal_ns
)
9737 resolve_symbol (ifc
);
9739 if (ifc
->attr
.intrinsic
)
9740 resolve_intrinsic (ifc
, &ifc
->declared_at
);
9744 c
->ts
= ifc
->result
->ts
;
9745 c
->attr
.allocatable
= ifc
->result
->attr
.allocatable
;
9746 c
->attr
.pointer
= ifc
->result
->attr
.pointer
;
9747 c
->attr
.dimension
= ifc
->result
->attr
.dimension
;
9748 c
->as
= gfc_copy_array_spec (ifc
->result
->as
);
9753 c
->attr
.allocatable
= ifc
->attr
.allocatable
;
9754 c
->attr
.pointer
= ifc
->attr
.pointer
;
9755 c
->attr
.dimension
= ifc
->attr
.dimension
;
9756 c
->as
= gfc_copy_array_spec (ifc
->as
);
9758 c
->ts
.interface
= ifc
;
9759 c
->attr
.function
= ifc
->attr
.function
;
9760 c
->attr
.subroutine
= ifc
->attr
.subroutine
;
9761 gfc_copy_formal_args_ppc (c
, ifc
);
9763 c
->attr
.pure
= ifc
->attr
.pure
;
9764 c
->attr
.elemental
= ifc
->attr
.elemental
;
9765 c
->attr
.recursive
= ifc
->attr
.recursive
;
9766 c
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
9767 c
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
9768 /* Replace symbols in array spec. */
9772 for (i
= 0; i
< c
->as
->rank
; i
++)
9774 gfc_expr_replace_comp (c
->as
->lower
[i
], c
);
9775 gfc_expr_replace_comp (c
->as
->upper
[i
], c
);
9778 /* Copy char length. */
9779 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
9781 c
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
9782 gfc_expr_replace_comp (c
->ts
.u
.cl
->length
, c
);
9785 else if (c
->ts
.interface
->name
[0] != '\0')
9787 gfc_error ("Interface '%s' of procedure pointer component "
9788 "'%s' at %L must be explicit", c
->ts
.interface
->name
,
9793 else if (c
->attr
.proc_pointer
&& c
->ts
.type
== BT_UNKNOWN
)
9795 c
->ts
= *gfc_get_default_type (c
->name
, NULL
);
9796 c
->attr
.implicit_type
= 1;
9799 /* Procedure pointer components: Check PASS arg. */
9800 if (c
->attr
.proc_pointer
&& !c
->tb
->nopass
&& c
->tb
->pass_arg_num
== 0)
9804 if (c
->tb
->pass_arg
)
9806 gfc_formal_arglist
* i
;
9808 /* If an explicit passing argument name is given, walk the arg-list
9812 c
->tb
->pass_arg_num
= 1;
9813 for (i
= c
->formal
; i
; i
= i
->next
)
9815 if (!strcmp (i
->sym
->name
, c
->tb
->pass_arg
))
9820 c
->tb
->pass_arg_num
++;
9825 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
9826 "at %L has no argument '%s'", c
->name
,
9827 c
->tb
->pass_arg
, &c
->loc
, c
->tb
->pass_arg
);
9834 /* Otherwise, take the first one; there should in fact be at least
9836 c
->tb
->pass_arg_num
= 1;
9839 gfc_error ("Procedure pointer component '%s' with PASS at %L "
9840 "must have at least one argument",
9845 me_arg
= c
->formal
->sym
;
9848 /* Now check that the argument-type matches. */
9849 gcc_assert (me_arg
);
9850 if ((me_arg
->ts
.type
!= BT_DERIVED
&& me_arg
->ts
.type
!= BT_CLASS
)
9851 || (me_arg
->ts
.type
== BT_DERIVED
&& me_arg
->ts
.u
.derived
!= sym
)
9852 || (me_arg
->ts
.type
== BT_CLASS
9853 && me_arg
->ts
.u
.derived
->components
->ts
.u
.derived
!= sym
))
9855 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9856 " the derived type '%s'", me_arg
->name
, c
->name
,
9857 me_arg
->name
, &c
->loc
, sym
->name
);
9862 /* Check for C453. */
9863 if (me_arg
->attr
.dimension
)
9865 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9866 "must be scalar", me_arg
->name
, c
->name
, me_arg
->name
,
9872 if (me_arg
->attr
.pointer
)
9874 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9875 "may not have the POINTER attribute", me_arg
->name
,
9876 c
->name
, me_arg
->name
, &c
->loc
);
9881 if (me_arg
->attr
.allocatable
)
9883 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9884 "may not be ALLOCATABLE", me_arg
->name
, c
->name
,
9885 me_arg
->name
, &c
->loc
);
9890 if (gfc_type_is_extensible (sym
) && me_arg
->ts
.type
!= BT_CLASS
)
9891 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9892 " at %L", c
->name
, &c
->loc
);
9896 /* Check type-spec if this is not the parent-type component. */
9897 if ((!sym
->attr
.extension
|| c
!= sym
->components
)
9898 && resolve_typespec_used (&c
->ts
, &c
->loc
, c
->name
) == FAILURE
)
9901 /* If this type is an extension, see if this component has the same name
9902 as an inherited type-bound procedure. */
9904 && gfc_find_typebound_proc (super_type
, NULL
, c
->name
, true, NULL
))
9906 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
9907 " inherited type-bound procedure",
9908 c
->name
, sym
->name
, &c
->loc
);
9912 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
)
9914 if (c
->ts
.u
.cl
->length
== NULL
9915 || (resolve_charlen (c
->ts
.u
.cl
) == FAILURE
)
9916 || !gfc_is_constant_expr (c
->ts
.u
.cl
->length
))
9918 gfc_error ("Character length of component '%s' needs to "
9919 "be a constant specification expression at %L",
9921 c
->ts
.u
.cl
->length
? &c
->ts
.u
.cl
->length
->where
: &c
->loc
);
9926 if (c
->ts
.type
== BT_DERIVED
9927 && sym
->component_access
!= ACCESS_PRIVATE
9928 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
9929 && !is_sym_host_assoc (c
->ts
.u
.derived
, sym
->ns
)
9930 && !c
->ts
.u
.derived
->attr
.use_assoc
9931 && !gfc_check_access (c
->ts
.u
.derived
->attr
.access
,
9932 c
->ts
.u
.derived
->ns
->default_access
)
9933 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: the component '%s' "
9934 "is a PRIVATE type and cannot be a component of "
9935 "'%s', which is PUBLIC at %L", c
->name
,
9936 sym
->name
, &sym
->declared_at
) == FAILURE
)
9939 if (sym
->attr
.sequence
)
9941 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.sequence
== 0)
9943 gfc_error ("Component %s of SEQUENCE type declared at %L does "
9944 "not have the SEQUENCE attribute",
9945 c
->ts
.u
.derived
->name
, &sym
->declared_at
);
9950 if (c
->ts
.type
== BT_DERIVED
&& c
->attr
.pointer
9951 && c
->ts
.u
.derived
->components
== NULL
9952 && !c
->ts
.u
.derived
->attr
.zero_comp
)
9954 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
9955 "that has not been declared", c
->name
, sym
->name
,
9961 if (c
->ts
.type
== BT_CLASS
9962 && !(c
->ts
.u
.derived
->components
->attr
.pointer
9963 || c
->ts
.u
.derived
->components
->attr
.allocatable
))
9965 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
9966 "or pointer", c
->name
, &c
->loc
);
9970 /* Ensure that all the derived type components are put on the
9971 derived type list; even in formal namespaces, where derived type
9972 pointer components might not have been declared. */
9973 if (c
->ts
.type
== BT_DERIVED
9975 && c
->ts
.u
.derived
->components
9977 && sym
!= c
->ts
.u
.derived
)
9978 add_dt_to_dt_list (c
->ts
.u
.derived
);
9980 if (c
->attr
.pointer
|| c
->attr
.proc_pointer
|| c
->attr
.allocatable
9984 for (i
= 0; i
< c
->as
->rank
; i
++)
9986 if (c
->as
->lower
[i
] == NULL
9987 || (resolve_index_expr (c
->as
->lower
[i
]) == FAILURE
)
9988 || !gfc_is_constant_expr (c
->as
->lower
[i
])
9989 || c
->as
->upper
[i
] == NULL
9990 || (resolve_index_expr (c
->as
->upper
[i
]) == FAILURE
)
9991 || !gfc_is_constant_expr (c
->as
->upper
[i
]))
9993 gfc_error ("Component '%s' of '%s' at %L must have "
9994 "constant array bounds",
9995 c
->name
, sym
->name
, &c
->loc
);
10001 /* Resolve the type-bound procedures. */
10002 if (resolve_typebound_procedures (sym
) == FAILURE
)
10005 /* Resolve the finalizer procedures. */
10006 if (gfc_resolve_finalizers (sym
) == FAILURE
)
10009 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10010 all DEFERRED bindings are overridden. */
10011 if (super_type
&& super_type
->attr
.abstract
&& !sym
->attr
.abstract
10012 && ensure_not_abstract (sym
, super_type
) == FAILURE
)
10015 /* Add derived type to the derived type list. */
10016 add_dt_to_dt_list (sym
);
10023 resolve_fl_namelist (gfc_symbol
*sym
)
10028 /* Reject PRIVATE objects in a PUBLIC namelist. */
10029 if (gfc_check_access(sym
->attr
.access
, sym
->ns
->default_access
))
10031 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10033 if (!nl
->sym
->attr
.use_assoc
10034 && !is_sym_host_assoc (nl
->sym
, sym
->ns
)
10035 && !gfc_check_access(nl
->sym
->attr
.access
,
10036 nl
->sym
->ns
->default_access
))
10038 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10039 "cannot be member of PUBLIC namelist '%s' at %L",
10040 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10044 /* Types with private components that came here by USE-association. */
10045 if (nl
->sym
->ts
.type
== BT_DERIVED
10046 && derived_inaccessible (nl
->sym
->ts
.u
.derived
))
10048 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10049 "components and cannot be member of namelist '%s' at %L",
10050 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10054 /* Types with private components that are defined in the same module. */
10055 if (nl
->sym
->ts
.type
== BT_DERIVED
10056 && !is_sym_host_assoc (nl
->sym
->ts
.u
.derived
, sym
->ns
)
10057 && !gfc_check_access (nl
->sym
->ts
.u
.derived
->attr
.private_comp
10058 ? ACCESS_PRIVATE
: ACCESS_UNKNOWN
,
10059 nl
->sym
->ns
->default_access
))
10061 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10062 "cannot be a member of PUBLIC namelist '%s' at %L",
10063 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10069 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10071 /* Reject namelist arrays of assumed shape. */
10072 if (nl
->sym
->as
&& nl
->sym
->as
->type
== AS_ASSUMED_SHAPE
10073 && gfc_notify_std (GFC_STD_F2003
, "NAMELIST array object '%s' "
10074 "must not have assumed shape in namelist "
10075 "'%s' at %L", nl
->sym
->name
, sym
->name
,
10076 &sym
->declared_at
) == FAILURE
)
10079 /* Reject namelist arrays that are not constant shape. */
10080 if (is_non_constant_shape_array (nl
->sym
))
10082 gfc_error ("NAMELIST array object '%s' must have constant "
10083 "shape in namelist '%s' at %L", nl
->sym
->name
,
10084 sym
->name
, &sym
->declared_at
);
10088 /* Namelist objects cannot have allocatable or pointer components. */
10089 if (nl
->sym
->ts
.type
!= BT_DERIVED
)
10092 if (nl
->sym
->ts
.u
.derived
->attr
.alloc_comp
)
10094 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10095 "have ALLOCATABLE components",
10096 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10100 if (nl
->sym
->ts
.u
.derived
->attr
.pointer_comp
)
10102 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10103 "have POINTER components",
10104 nl
->sym
->name
, sym
->name
, &sym
->declared_at
);
10110 /* 14.1.2 A module or internal procedure represent local entities
10111 of the same type as a namelist member and so are not allowed. */
10112 for (nl
= sym
->namelist
; nl
; nl
= nl
->next
)
10114 if (nl
->sym
->ts
.kind
!= 0 && nl
->sym
->attr
.flavor
== FL_VARIABLE
)
10117 if (nl
->sym
->attr
.function
&& nl
->sym
== nl
->sym
->result
)
10118 if ((nl
->sym
== sym
->ns
->proc_name
)
10120 (sym
->ns
->parent
&& nl
->sym
== sym
->ns
->parent
->proc_name
))
10124 if (nl
->sym
&& nl
->sym
->name
)
10125 gfc_find_symbol (nl
->sym
->name
, sym
->ns
, 1, &nlsym
);
10126 if (nlsym
&& nlsym
->attr
.flavor
== FL_PROCEDURE
)
10128 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10129 "attribute in '%s' at %L", nlsym
->name
,
10130 &sym
->declared_at
);
10140 resolve_fl_parameter (gfc_symbol
*sym
)
10142 /* A parameter array's shape needs to be constant. */
10143 if (sym
->as
!= NULL
10144 && (sym
->as
->type
== AS_DEFERRED
10145 || is_non_constant_shape_array (sym
)))
10147 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10148 "or of deferred shape", sym
->name
, &sym
->declared_at
);
10152 /* Make sure a parameter that has been implicitly typed still
10153 matches the implicit type, since PARAMETER statements can precede
10154 IMPLICIT statements. */
10155 if (sym
->attr
.implicit_type
10156 && !gfc_compare_types (&sym
->ts
, gfc_get_default_type (sym
->name
,
10159 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10160 "later IMPLICIT type", sym
->name
, &sym
->declared_at
);
10164 /* Make sure the types of derived parameters are consistent. This
10165 type checking is deferred until resolution because the type may
10166 refer to a derived type from the host. */
10167 if (sym
->ts
.type
== BT_DERIVED
10168 && !gfc_compare_types (&sym
->ts
, &sym
->value
->ts
))
10170 gfc_error ("Incompatible derived type in PARAMETER at %L",
10171 &sym
->value
->where
);
10178 /* Do anything necessary to resolve a symbol. Right now, we just
10179 assume that an otherwise unknown symbol is a variable. This sort
10180 of thing commonly happens for symbols in module. */
10183 resolve_symbol (gfc_symbol
*sym
)
10185 int check_constant
, mp_flag
;
10186 gfc_symtree
*symtree
;
10187 gfc_symtree
*this_symtree
;
10191 if (sym
->attr
.flavor
== FL_UNKNOWN
)
10194 /* If we find that a flavorless symbol is an interface in one of the
10195 parent namespaces, find its symtree in this namespace, free the
10196 symbol and set the symtree to point to the interface symbol. */
10197 for (ns
= gfc_current_ns
->parent
; ns
; ns
= ns
->parent
)
10199 symtree
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
10200 if (symtree
&& symtree
->n
.sym
->generic
)
10202 this_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
10206 gfc_free_symbol (sym
);
10207 symtree
->n
.sym
->refs
++;
10208 this_symtree
->n
.sym
= symtree
->n
.sym
;
10213 /* Otherwise give it a flavor according to such attributes as
10215 if (sym
->attr
.external
== 0 && sym
->attr
.intrinsic
== 0)
10216 sym
->attr
.flavor
= FL_VARIABLE
;
10219 sym
->attr
.flavor
= FL_PROCEDURE
;
10220 if (sym
->attr
.dimension
)
10221 sym
->attr
.function
= 1;
10225 if (sym
->attr
.external
&& sym
->ts
.type
!= BT_UNKNOWN
&& !sym
->attr
.function
)
10226 gfc_add_function (&sym
->attr
, sym
->name
, &sym
->declared_at
);
10228 if (sym
->attr
.procedure
&& sym
->ts
.interface
10229 && sym
->attr
.if_source
!= IFSRC_DECL
)
10231 if (sym
->ts
.interface
== sym
)
10233 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10234 "interface", sym
->name
, &sym
->declared_at
);
10237 if (sym
->ts
.interface
->attr
.procedure
)
10239 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10240 " in a later PROCEDURE statement", sym
->ts
.interface
->name
,
10241 sym
->name
,&sym
->declared_at
);
10245 /* Get the attributes from the interface (now resolved). */
10246 if (sym
->ts
.interface
->attr
.if_source
10247 || sym
->ts
.interface
->attr
.intrinsic
)
10249 gfc_symbol
*ifc
= sym
->ts
.interface
;
10250 resolve_symbol (ifc
);
10252 if (ifc
->attr
.intrinsic
)
10253 resolve_intrinsic (ifc
, &ifc
->declared_at
);
10256 sym
->ts
= ifc
->result
->ts
;
10259 sym
->ts
.interface
= ifc
;
10260 sym
->attr
.function
= ifc
->attr
.function
;
10261 sym
->attr
.subroutine
= ifc
->attr
.subroutine
;
10262 gfc_copy_formal_args (sym
, ifc
);
10264 sym
->attr
.allocatable
= ifc
->attr
.allocatable
;
10265 sym
->attr
.pointer
= ifc
->attr
.pointer
;
10266 sym
->attr
.pure
= ifc
->attr
.pure
;
10267 sym
->attr
.elemental
= ifc
->attr
.elemental
;
10268 sym
->attr
.dimension
= ifc
->attr
.dimension
;
10269 sym
->attr
.recursive
= ifc
->attr
.recursive
;
10270 sym
->attr
.always_explicit
= ifc
->attr
.always_explicit
;
10271 sym
->attr
.ext_attr
|= ifc
->attr
.ext_attr
;
10272 /* Copy array spec. */
10273 sym
->as
= gfc_copy_array_spec (ifc
->as
);
10277 for (i
= 0; i
< sym
->as
->rank
; i
++)
10279 gfc_expr_replace_symbols (sym
->as
->lower
[i
], sym
);
10280 gfc_expr_replace_symbols (sym
->as
->upper
[i
], sym
);
10283 /* Copy char length. */
10284 if (ifc
->ts
.type
== BT_CHARACTER
&& ifc
->ts
.u
.cl
)
10286 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ifc
->ts
.u
.cl
);
10287 gfc_expr_replace_symbols (sym
->ts
.u
.cl
->length
, sym
);
10290 else if (sym
->ts
.interface
->name
[0] != '\0')
10292 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10293 sym
->ts
.interface
->name
, sym
->name
, &sym
->declared_at
);
10298 if (sym
->attr
.flavor
== FL_DERIVED
&& resolve_fl_derived (sym
) == FAILURE
)
10301 /* Symbols that are module procedures with results (functions) have
10302 the types and array specification copied for type checking in
10303 procedures that call them, as well as for saving to a module
10304 file. These symbols can't stand the scrutiny that their results
10306 mp_flag
= (sym
->result
!= NULL
&& sym
->result
!= sym
);
10309 /* Make sure that the intrinsic is consistent with its internal
10310 representation. This needs to be done before assigning a default
10311 type to avoid spurious warnings. */
10312 if (sym
->attr
.flavor
!= FL_MODULE
&& sym
->attr
.intrinsic
10313 && resolve_intrinsic (sym
, &sym
->declared_at
) == FAILURE
)
10316 /* Assign default type to symbols that need one and don't have one. */
10317 if (sym
->ts
.type
== BT_UNKNOWN
)
10319 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
)
10320 gfc_set_default_type (sym
, 1, NULL
);
10322 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
10323 && !sym
->attr
.function
&& !sym
->attr
.subroutine
10324 && gfc_get_default_type (sym
->name
, sym
->ns
)->type
== BT_UNKNOWN
)
10325 gfc_add_subroutine (&sym
->attr
, sym
->name
, &sym
->declared_at
);
10327 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
)
10329 /* The specific case of an external procedure should emit an error
10330 in the case that there is no implicit type. */
10332 gfc_set_default_type (sym
, sym
->attr
.external
, NULL
);
10335 /* Result may be in another namespace. */
10336 resolve_symbol (sym
->result
);
10338 if (!sym
->result
->attr
.proc_pointer
)
10340 sym
->ts
= sym
->result
->ts
;
10341 sym
->as
= gfc_copy_array_spec (sym
->result
->as
);
10342 sym
->attr
.dimension
= sym
->result
->attr
.dimension
;
10343 sym
->attr
.pointer
= sym
->result
->attr
.pointer
;
10344 sym
->attr
.allocatable
= sym
->result
->attr
.allocatable
;
10350 /* Assumed size arrays and assumed shape arrays must be dummy
10353 if (sym
->as
!= NULL
10354 && (sym
->as
->type
== AS_ASSUMED_SIZE
10355 || sym
->as
->type
== AS_ASSUMED_SHAPE
)
10356 && sym
->attr
.dummy
== 0)
10358 if (sym
->as
->type
== AS_ASSUMED_SIZE
)
10359 gfc_error ("Assumed size array at %L must be a dummy argument",
10360 &sym
->declared_at
);
10362 gfc_error ("Assumed shape array at %L must be a dummy argument",
10363 &sym
->declared_at
);
10367 /* Make sure symbols with known intent or optional are really dummy
10368 variable. Because of ENTRY statement, this has to be deferred
10369 until resolution time. */
10371 if (!sym
->attr
.dummy
10372 && (sym
->attr
.optional
|| sym
->attr
.intent
!= INTENT_UNKNOWN
))
10374 gfc_error ("Symbol at %L is not a DUMMY variable", &sym
->declared_at
);
10378 if (sym
->attr
.value
&& !sym
->attr
.dummy
)
10380 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
10381 "it is not a dummy argument", sym
->name
, &sym
->declared_at
);
10385 if (sym
->attr
.value
&& sym
->ts
.type
== BT_CHARACTER
)
10387 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
10388 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
)
10390 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10391 "attribute must have constant length",
10392 sym
->name
, &sym
->declared_at
);
10396 if (sym
->ts
.is_c_interop
10397 && mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
10399 gfc_error ("C interoperable character dummy variable '%s' at %L "
10400 "with VALUE attribute must have length one",
10401 sym
->name
, &sym
->declared_at
);
10406 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
10407 do this for something that was implicitly typed because that is handled
10408 in gfc_set_default_type. Handle dummy arguments and procedure
10409 definitions separately. Also, anything that is use associated is not
10410 handled here but instead is handled in the module it is declared in.
10411 Finally, derived type definitions are allowed to be BIND(C) since that
10412 only implies that they're interoperable, and they are checked fully for
10413 interoperability when a variable is declared of that type. */
10414 if (sym
->attr
.is_bind_c
&& sym
->attr
.implicit_type
== 0 &&
10415 sym
->attr
.use_assoc
== 0 && sym
->attr
.dummy
== 0 &&
10416 sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.flavor
!= FL_DERIVED
)
10418 gfc_try t
= SUCCESS
;
10420 /* First, make sure the variable is declared at the
10421 module-level scope (J3/04-007, Section 15.3). */
10422 if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
&&
10423 sym
->attr
.in_common
== 0)
10425 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
10426 "is neither a COMMON block nor declared at the "
10427 "module level scope", sym
->name
, &(sym
->declared_at
));
10430 else if (sym
->common_head
!= NULL
)
10432 t
= verify_com_block_vars_c_interop (sym
->common_head
);
10436 /* If type() declaration, we need to verify that the components
10437 of the given type are all C interoperable, etc. */
10438 if (sym
->ts
.type
== BT_DERIVED
&&
10439 sym
->ts
.u
.derived
->attr
.is_c_interop
!= 1)
10441 /* Make sure the user marked the derived type as BIND(C). If
10442 not, call the verify routine. This could print an error
10443 for the derived type more than once if multiple variables
10444 of that type are declared. */
10445 if (sym
->ts
.u
.derived
->attr
.is_bind_c
!= 1)
10446 verify_bind_c_derived_type (sym
->ts
.u
.derived
);
10450 /* Verify the variable itself as C interoperable if it
10451 is BIND(C). It is not possible for this to succeed if
10452 the verify_bind_c_derived_type failed, so don't have to handle
10453 any error returned by verify_bind_c_derived_type. */
10454 t
= verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
10455 sym
->common_block
);
10460 /* clear the is_bind_c flag to prevent reporting errors more than
10461 once if something failed. */
10462 sym
->attr
.is_bind_c
= 0;
10467 /* If a derived type symbol has reached this point, without its
10468 type being declared, we have an error. Notice that most
10469 conditions that produce undefined derived types have already
10470 been dealt with. However, the likes of:
10471 implicit type(t) (t) ..... call foo (t) will get us here if
10472 the type is not declared in the scope of the implicit
10473 statement. Change the type to BT_UNKNOWN, both because it is so
10474 and to prevent an ICE. */
10475 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->components
== NULL
10476 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
10478 gfc_error ("The derived type '%s' at %L is of type '%s', "
10479 "which has not been defined", sym
->name
,
10480 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
10481 sym
->ts
.type
= BT_UNKNOWN
;
10485 /* Make sure that the derived type has been resolved and that the
10486 derived type is visible in the symbol's namespace, if it is a
10487 module function and is not PRIVATE. */
10488 if (sym
->ts
.type
== BT_DERIVED
10489 && sym
->ts
.u
.derived
->attr
.use_assoc
10490 && sym
->ns
->proc_name
10491 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
10495 if (resolve_fl_derived (sym
->ts
.u
.derived
) == FAILURE
)
10498 gfc_find_symbol (sym
->ts
.u
.derived
->name
, sym
->ns
, 1, &ds
);
10499 if (!ds
&& sym
->attr
.function
10500 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
10502 symtree
= gfc_new_symtree (&sym
->ns
->sym_root
,
10503 sym
->ts
.u
.derived
->name
);
10504 symtree
->n
.sym
= sym
->ts
.u
.derived
;
10505 sym
->ts
.u
.derived
->refs
++;
10509 /* Unless the derived-type declaration is use associated, Fortran 95
10510 does not allow public entries of private derived types.
10511 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
10512 161 in 95-006r3. */
10513 if (sym
->ts
.type
== BT_DERIVED
10514 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
10515 && !sym
->ts
.u
.derived
->attr
.use_assoc
10516 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
)
10517 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
10518 sym
->ts
.u
.derived
->ns
->default_access
)
10519 && gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC %s '%s' at %L "
10520 "of PRIVATE derived type '%s'",
10521 (sym
->attr
.flavor
== FL_PARAMETER
) ? "parameter"
10522 : "variable", sym
->name
, &sym
->declared_at
,
10523 sym
->ts
.u
.derived
->name
) == FAILURE
)
10526 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
10527 default initialization is defined (5.1.2.4.4). */
10528 if (sym
->ts
.type
== BT_DERIVED
10530 && sym
->attr
.intent
== INTENT_OUT
10532 && sym
->as
->type
== AS_ASSUMED_SIZE
)
10534 for (c
= sym
->ts
.u
.derived
->components
; c
; c
= c
->next
)
10536 if (c
->initializer
)
10538 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
10539 "ASSUMED SIZE and so cannot have a default initializer",
10540 sym
->name
, &sym
->declared_at
);
10546 switch (sym
->attr
.flavor
)
10549 if (resolve_fl_variable (sym
, mp_flag
) == FAILURE
)
10554 if (resolve_fl_procedure (sym
, mp_flag
) == FAILURE
)
10559 if (resolve_fl_namelist (sym
) == FAILURE
)
10564 if (resolve_fl_parameter (sym
) == FAILURE
)
10572 /* Resolve array specifier. Check as well some constraints
10573 on COMMON blocks. */
10575 check_constant
= sym
->attr
.in_common
&& !sym
->attr
.pointer
;
10577 /* Set the formal_arg_flag so that check_conflict will not throw
10578 an error for host associated variables in the specification
10579 expression for an array_valued function. */
10580 if (sym
->attr
.function
&& sym
->as
)
10581 formal_arg_flag
= 1;
10583 gfc_resolve_array_spec (sym
->as
, check_constant
);
10585 formal_arg_flag
= 0;
10587 /* Resolve formal namespaces. */
10588 if (sym
->formal_ns
&& sym
->formal_ns
!= gfc_current_ns
10589 && !sym
->attr
.contained
&& !sym
->attr
.intrinsic
)
10590 gfc_resolve (sym
->formal_ns
);
10592 /* Make sure the formal namespace is present. */
10593 if (sym
->formal
&& !sym
->formal_ns
)
10595 gfc_formal_arglist
*formal
= sym
->formal
;
10596 while (formal
&& !formal
->sym
)
10597 formal
= formal
->next
;
10601 sym
->formal_ns
= formal
->sym
->ns
;
10602 sym
->formal_ns
->refs
++;
10606 /* Check threadprivate restrictions. */
10607 if (sym
->attr
.threadprivate
&& !sym
->attr
.save
&& !sym
->ns
->save_all
10608 && (!sym
->attr
.in_common
10609 && sym
->module
== NULL
10610 && (sym
->ns
->proc_name
== NULL
10611 || sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)))
10612 gfc_error ("Threadprivate at %L isn't SAVEd", &sym
->declared_at
);
10614 /* If we have come this far we can apply default-initializers, as
10615 described in 14.7.5, to those variables that have not already
10616 been assigned one. */
10617 if (sym
->ts
.type
== BT_DERIVED
10618 && sym
->attr
.referenced
10619 && sym
->ns
== gfc_current_ns
10621 && !sym
->attr
.allocatable
10622 && !sym
->attr
.alloc_comp
)
10624 symbol_attribute
*a
= &sym
->attr
;
10626 if ((!a
->save
&& !a
->dummy
&& !a
->pointer
10627 && !a
->in_common
&& !a
->use_assoc
10628 && !(a
->function
&& sym
!= sym
->result
))
10629 || (a
->dummy
&& a
->intent
== INTENT_OUT
&& !a
->pointer
))
10630 apply_default_init (sym
);
10633 /* If this symbol has a type-spec, check it. */
10634 if (sym
->attr
.flavor
== FL_VARIABLE
|| sym
->attr
.flavor
== FL_PARAMETER
10635 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.function
))
10636 if (resolve_typespec_used (&sym
->ts
, &sym
->declared_at
, sym
->name
)
10642 /************* Resolve DATA statements *************/
10646 gfc_data_value
*vnode
;
10652 /* Advance the values structure to point to the next value in the data list. */
10655 next_data_value (void)
10657 while (mpz_cmp_ui (values
.left
, 0) == 0)
10659 if (!gfc_is_constant_expr (values
.vnode
->expr
))
10660 gfc_error ("non-constant DATA value at %L",
10661 &values
.vnode
->expr
->where
);
10663 if (values
.vnode
->next
== NULL
)
10666 values
.vnode
= values
.vnode
->next
;
10667 mpz_set (values
.left
, values
.vnode
->repeat
);
10675 check_data_variable (gfc_data_variable
*var
, locus
*where
)
10681 ar_type mark
= AR_UNKNOWN
;
10683 mpz_t section_index
[GFC_MAX_DIMENSIONS
];
10689 if (gfc_resolve_expr (var
->expr
) == FAILURE
)
10693 mpz_init_set_si (offset
, 0);
10696 if (e
->expr_type
!= EXPR_VARIABLE
)
10697 gfc_internal_error ("check_data_variable(): Bad expression");
10699 sym
= e
->symtree
->n
.sym
;
10701 if (sym
->ns
->is_block_data
&& !sym
->attr
.in_common
)
10703 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
10704 sym
->name
, &sym
->declared_at
);
10707 if (e
->ref
== NULL
&& sym
->as
)
10709 gfc_error ("DATA array '%s' at %L must be specified in a previous"
10710 " declaration", sym
->name
, where
);
10714 has_pointer
= sym
->attr
.pointer
;
10716 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
10718 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
10722 && ref
->type
== REF_ARRAY
10723 && ref
->u
.ar
.type
!= AR_FULL
)
10725 gfc_error ("DATA element '%s' at %L is a pointer and so must "
10726 "be a full array", sym
->name
, where
);
10731 if (e
->rank
== 0 || has_pointer
)
10733 mpz_init_set_ui (size
, 1);
10740 /* Find the array section reference. */
10741 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
10743 if (ref
->type
!= REF_ARRAY
)
10745 if (ref
->u
.ar
.type
== AR_ELEMENT
)
10751 /* Set marks according to the reference pattern. */
10752 switch (ref
->u
.ar
.type
)
10760 /* Get the start position of array section. */
10761 gfc_get_section_index (ar
, section_index
, &offset
);
10766 gcc_unreachable ();
10769 if (gfc_array_size (e
, &size
) == FAILURE
)
10771 gfc_error ("Nonconstant array section at %L in DATA statement",
10773 mpz_clear (offset
);
10780 while (mpz_cmp_ui (size
, 0) > 0)
10782 if (next_data_value () == FAILURE
)
10784 gfc_error ("DATA statement at %L has more variables than values",
10790 t
= gfc_check_assign (var
->expr
, values
.vnode
->expr
, 0);
10794 /* If we have more than one element left in the repeat count,
10795 and we have more than one element left in the target variable,
10796 then create a range assignment. */
10797 /* FIXME: Only done for full arrays for now, since array sections
10799 if (mark
== AR_FULL
&& ref
&& ref
->next
== NULL
10800 && mpz_cmp_ui (values
.left
, 1) > 0 && mpz_cmp_ui (size
, 1) > 0)
10804 if (mpz_cmp (size
, values
.left
) >= 0)
10806 mpz_init_set (range
, values
.left
);
10807 mpz_sub (size
, size
, values
.left
);
10808 mpz_set_ui (values
.left
, 0);
10812 mpz_init_set (range
, size
);
10813 mpz_sub (values
.left
, values
.left
, size
);
10814 mpz_set_ui (size
, 0);
10817 gfc_assign_data_value_range (var
->expr
, values
.vnode
->expr
,
10820 mpz_add (offset
, offset
, range
);
10824 /* Assign initial value to symbol. */
10827 mpz_sub_ui (values
.left
, values
.left
, 1);
10828 mpz_sub_ui (size
, size
, 1);
10830 t
= gfc_assign_data_value (var
->expr
, values
.vnode
->expr
, offset
);
10834 if (mark
== AR_FULL
)
10835 mpz_add_ui (offset
, offset
, 1);
10837 /* Modify the array section indexes and recalculate the offset
10838 for next element. */
10839 else if (mark
== AR_SECTION
)
10840 gfc_advance_section (section_index
, ar
, &offset
);
10844 if (mark
== AR_SECTION
)
10846 for (i
= 0; i
< ar
->dimen
; i
++)
10847 mpz_clear (section_index
[i
]);
10851 mpz_clear (offset
);
10857 static gfc_try
traverse_data_var (gfc_data_variable
*, locus
*);
10859 /* Iterate over a list of elements in a DATA statement. */
10862 traverse_data_list (gfc_data_variable
*var
, locus
*where
)
10865 iterator_stack frame
;
10866 gfc_expr
*e
, *start
, *end
, *step
;
10867 gfc_try retval
= SUCCESS
;
10869 mpz_init (frame
.value
);
10871 start
= gfc_copy_expr (var
->iter
.start
);
10872 end
= gfc_copy_expr (var
->iter
.end
);
10873 step
= gfc_copy_expr (var
->iter
.step
);
10875 if (gfc_simplify_expr (start
, 1) == FAILURE
10876 || start
->expr_type
!= EXPR_CONSTANT
)
10878 gfc_error ("iterator start at %L does not simplify", &start
->where
);
10882 if (gfc_simplify_expr (end
, 1) == FAILURE
10883 || end
->expr_type
!= EXPR_CONSTANT
)
10885 gfc_error ("iterator end at %L does not simplify", &end
->where
);
10889 if (gfc_simplify_expr (step
, 1) == FAILURE
10890 || step
->expr_type
!= EXPR_CONSTANT
)
10892 gfc_error ("iterator step at %L does not simplify", &step
->where
);
10897 mpz_init_set (trip
, end
->value
.integer
);
10898 mpz_sub (trip
, trip
, start
->value
.integer
);
10899 mpz_add (trip
, trip
, step
->value
.integer
);
10901 mpz_div (trip
, trip
, step
->value
.integer
);
10903 mpz_set (frame
.value
, start
->value
.integer
);
10905 frame
.prev
= iter_stack
;
10906 frame
.variable
= var
->iter
.var
->symtree
;
10907 iter_stack
= &frame
;
10909 while (mpz_cmp_ui (trip
, 0) > 0)
10911 if (traverse_data_var (var
->list
, where
) == FAILURE
)
10918 e
= gfc_copy_expr (var
->expr
);
10919 if (gfc_simplify_expr (e
, 1) == FAILURE
)
10927 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
10929 mpz_sub_ui (trip
, trip
, 1);
10934 mpz_clear (frame
.value
);
10936 gfc_free_expr (start
);
10937 gfc_free_expr (end
);
10938 gfc_free_expr (step
);
10940 iter_stack
= frame
.prev
;
10945 /* Type resolve variables in the variable list of a DATA statement. */
10948 traverse_data_var (gfc_data_variable
*var
, locus
*where
)
10952 for (; var
; var
= var
->next
)
10954 if (var
->expr
== NULL
)
10955 t
= traverse_data_list (var
, where
);
10957 t
= check_data_variable (var
, where
);
10967 /* Resolve the expressions and iterators associated with a data statement.
10968 This is separate from the assignment checking because data lists should
10969 only be resolved once. */
10972 resolve_data_variables (gfc_data_variable
*d
)
10974 for (; d
; d
= d
->next
)
10976 if (d
->list
== NULL
)
10978 if (gfc_resolve_expr (d
->expr
) == FAILURE
)
10983 if (gfc_resolve_iterator (&d
->iter
, false) == FAILURE
)
10986 if (resolve_data_variables (d
->list
) == FAILURE
)
10995 /* Resolve a single DATA statement. We implement this by storing a pointer to
10996 the value list into static variables, and then recursively traversing the
10997 variables list, expanding iterators and such. */
11000 resolve_data (gfc_data
*d
)
11003 if (resolve_data_variables (d
->var
) == FAILURE
)
11006 values
.vnode
= d
->value
;
11007 if (d
->value
== NULL
)
11008 mpz_set_ui (values
.left
, 0);
11010 mpz_set (values
.left
, d
->value
->repeat
);
11012 if (traverse_data_var (d
->var
, &d
->where
) == FAILURE
)
11015 /* At this point, we better not have any values left. */
11017 if (next_data_value () == SUCCESS
)
11018 gfc_error ("DATA statement at %L has more values than variables",
11023 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11024 accessed by host or use association, is a dummy argument to a pure function,
11025 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11026 is storage associated with any such variable, shall not be used in the
11027 following contexts: (clients of this function). */
11029 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11030 procedure. Returns zero if assignment is OK, nonzero if there is a
11033 gfc_impure_variable (gfc_symbol
*sym
)
11037 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
11040 if (sym
->ns
!= gfc_current_ns
)
11041 return !sym
->attr
.function
;
11043 proc
= sym
->ns
->proc_name
;
11044 if (sym
->attr
.dummy
&& gfc_pure (proc
)
11045 && ((proc
->attr
.subroutine
&& sym
->attr
.intent
== INTENT_IN
)
11047 proc
->attr
.function
))
11050 /* TODO: Sort out what can be storage associated, if anything, and include
11051 it here. In principle equivalences should be scanned but it does not
11052 seem to be possible to storage associate an impure variable this way. */
11057 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
11058 symbol of the current procedure. */
11061 gfc_pure (gfc_symbol
*sym
)
11063 symbol_attribute attr
;
11066 sym
= gfc_current_ns
->proc_name
;
11072 return attr
.flavor
== FL_PROCEDURE
&& (attr
.pure
|| attr
.elemental
);
11076 /* Test whether the current procedure is elemental or not. */
11079 gfc_elemental (gfc_symbol
*sym
)
11081 symbol_attribute attr
;
11084 sym
= gfc_current_ns
->proc_name
;
11089 return attr
.flavor
== FL_PROCEDURE
&& attr
.elemental
;
11093 /* Warn about unused labels. */
11096 warn_unused_fortran_label (gfc_st_label
*label
)
11101 warn_unused_fortran_label (label
->left
);
11103 if (label
->defined
== ST_LABEL_UNKNOWN
)
11106 switch (label
->referenced
)
11108 case ST_LABEL_UNKNOWN
:
11109 gfc_warning ("Label %d at %L defined but not used", label
->value
,
11113 case ST_LABEL_BAD_TARGET
:
11114 gfc_warning ("Label %d at %L defined but cannot be used",
11115 label
->value
, &label
->where
);
11122 warn_unused_fortran_label (label
->right
);
11126 /* Returns the sequence type of a symbol or sequence. */
11129 sequence_type (gfc_typespec ts
)
11138 if (ts
.u
.derived
->components
== NULL
)
11139 return SEQ_NONDEFAULT
;
11141 result
= sequence_type (ts
.u
.derived
->components
->ts
);
11142 for (c
= ts
.u
.derived
->components
->next
; c
; c
= c
->next
)
11143 if (sequence_type (c
->ts
) != result
)
11149 if (ts
.kind
!= gfc_default_character_kind
)
11150 return SEQ_NONDEFAULT
;
11152 return SEQ_CHARACTER
;
11155 if (ts
.kind
!= gfc_default_integer_kind
)
11156 return SEQ_NONDEFAULT
;
11158 return SEQ_NUMERIC
;
11161 if (!(ts
.kind
== gfc_default_real_kind
11162 || ts
.kind
== gfc_default_double_kind
))
11163 return SEQ_NONDEFAULT
;
11165 return SEQ_NUMERIC
;
11168 if (ts
.kind
!= gfc_default_complex_kind
)
11169 return SEQ_NONDEFAULT
;
11171 return SEQ_NUMERIC
;
11174 if (ts
.kind
!= gfc_default_logical_kind
)
11175 return SEQ_NONDEFAULT
;
11177 return SEQ_NUMERIC
;
11180 return SEQ_NONDEFAULT
;
11185 /* Resolve derived type EQUIVALENCE object. */
11188 resolve_equivalence_derived (gfc_symbol
*derived
, gfc_symbol
*sym
, gfc_expr
*e
)
11190 gfc_component
*c
= derived
->components
;
11195 /* Shall not be an object of nonsequence derived type. */
11196 if (!derived
->attr
.sequence
)
11198 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
11199 "attribute to be an EQUIVALENCE object", sym
->name
,
11204 /* Shall not have allocatable components. */
11205 if (derived
->attr
.alloc_comp
)
11207 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
11208 "components to be an EQUIVALENCE object",sym
->name
,
11213 if (sym
->attr
.in_common
&& has_default_initializer (sym
->ts
.u
.derived
))
11215 gfc_error ("Derived type variable '%s' at %L with default "
11216 "initialization cannot be in EQUIVALENCE with a variable "
11217 "in COMMON", sym
->name
, &e
->where
);
11221 for (; c
; c
= c
->next
)
11223 if (c
->ts
.type
== BT_DERIVED
11224 && (resolve_equivalence_derived (c
->ts
.u
.derived
, sym
, e
) == FAILURE
))
11227 /* Shall not be an object of sequence derived type containing a pointer
11228 in the structure. */
11229 if (c
->attr
.pointer
)
11231 gfc_error ("Derived type variable '%s' at %L with pointer "
11232 "component(s) cannot be an EQUIVALENCE object",
11233 sym
->name
, &e
->where
);
11241 /* Resolve equivalence object.
11242 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11243 an allocatable array, an object of nonsequence derived type, an object of
11244 sequence derived type containing a pointer at any level of component
11245 selection, an automatic object, a function name, an entry name, a result
11246 name, a named constant, a structure component, or a subobject of any of
11247 the preceding objects. A substring shall not have length zero. A
11248 derived type shall not have components with default initialization nor
11249 shall two objects of an equivalence group be initialized.
11250 Either all or none of the objects shall have an protected attribute.
11251 The simple constraints are done in symbol.c(check_conflict) and the rest
11252 are implemented here. */
11255 resolve_equivalence (gfc_equiv
*eq
)
11258 gfc_symbol
*first_sym
;
11261 locus
*last_where
= NULL
;
11262 seq_type eq_type
, last_eq_type
;
11263 gfc_typespec
*last_ts
;
11264 int object
, cnt_protected
;
11265 const char *value_name
;
11269 last_ts
= &eq
->expr
->symtree
->n
.sym
->ts
;
11271 first_sym
= eq
->expr
->symtree
->n
.sym
;
11275 for (object
= 1; eq
; eq
= eq
->eq
, object
++)
11279 e
->ts
= e
->symtree
->n
.sym
->ts
;
11280 /* match_varspec might not know yet if it is seeing
11281 array reference or substring reference, as it doesn't
11283 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
)
11285 gfc_ref
*ref
= e
->ref
;
11286 sym
= e
->symtree
->n
.sym
;
11288 if (sym
->attr
.dimension
)
11290 ref
->u
.ar
.as
= sym
->as
;
11294 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11295 if (e
->ts
.type
== BT_CHARACTER
11297 && ref
->type
== REF_ARRAY
11298 && ref
->u
.ar
.dimen
== 1
11299 && ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
11300 && ref
->u
.ar
.stride
[0] == NULL
)
11302 gfc_expr
*start
= ref
->u
.ar
.start
[0];
11303 gfc_expr
*end
= ref
->u
.ar
.end
[0];
11306 /* Optimize away the (:) reference. */
11307 if (start
== NULL
&& end
== NULL
)
11310 e
->ref
= ref
->next
;
11312 e
->ref
->next
= ref
->next
;
11317 ref
->type
= REF_SUBSTRING
;
11319 start
= gfc_int_expr (1);
11320 ref
->u
.ss
.start
= start
;
11321 if (end
== NULL
&& e
->ts
.u
.cl
)
11322 end
= gfc_copy_expr (e
->ts
.u
.cl
->length
);
11323 ref
->u
.ss
.end
= end
;
11324 ref
->u
.ss
.length
= e
->ts
.u
.cl
;
11331 /* Any further ref is an error. */
11334 gcc_assert (ref
->type
== REF_ARRAY
);
11335 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11341 if (gfc_resolve_expr (e
) == FAILURE
)
11344 sym
= e
->symtree
->n
.sym
;
11346 if (sym
->attr
.is_protected
)
11348 if (cnt_protected
> 0 && cnt_protected
!= object
)
11350 gfc_error ("Either all or none of the objects in the "
11351 "EQUIVALENCE set at %L shall have the "
11352 "PROTECTED attribute",
11357 /* Shall not equivalence common block variables in a PURE procedure. */
11358 if (sym
->ns
->proc_name
11359 && sym
->ns
->proc_name
->attr
.pure
11360 && sym
->attr
.in_common
)
11362 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11363 "object in the pure procedure '%s'",
11364 sym
->name
, &e
->where
, sym
->ns
->proc_name
->name
);
11368 /* Shall not be a named constant. */
11369 if (e
->expr_type
== EXPR_CONSTANT
)
11371 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11372 "object", sym
->name
, &e
->where
);
11376 if (e
->ts
.type
== BT_DERIVED
11377 && resolve_equivalence_derived (e
->ts
.u
.derived
, sym
, e
) == FAILURE
)
11380 /* Check that the types correspond correctly:
11382 A numeric sequence structure may be equivalenced to another sequence
11383 structure, an object of default integer type, default real type, double
11384 precision real type, default logical type such that components of the
11385 structure ultimately only become associated to objects of the same
11386 kind. A character sequence structure may be equivalenced to an object
11387 of default character kind or another character sequence structure.
11388 Other objects may be equivalenced only to objects of the same type and
11389 kind parameters. */
11391 /* Identical types are unconditionally OK. */
11392 if (object
== 1 || gfc_compare_types (last_ts
, &sym
->ts
))
11393 goto identical_types
;
11395 last_eq_type
= sequence_type (*last_ts
);
11396 eq_type
= sequence_type (sym
->ts
);
11398 /* Since the pair of objects is not of the same type, mixed or
11399 non-default sequences can be rejected. */
11401 msg
= "Sequence %s with mixed components in EQUIVALENCE "
11402 "statement at %L with different type objects";
11404 && last_eq_type
== SEQ_MIXED
11405 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
, last_where
)
11407 || (eq_type
== SEQ_MIXED
11408 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
11409 &e
->where
) == FAILURE
))
11412 msg
= "Non-default type object or sequence %s in EQUIVALENCE "
11413 "statement at %L with objects of different type";
11415 && last_eq_type
== SEQ_NONDEFAULT
11416 && gfc_notify_std (GFC_STD_GNU
, msg
, first_sym
->name
,
11417 last_where
) == FAILURE
)
11418 || (eq_type
== SEQ_NONDEFAULT
11419 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
11420 &e
->where
) == FAILURE
))
11423 msg
="Non-CHARACTER object '%s' in default CHARACTER "
11424 "EQUIVALENCE statement at %L";
11425 if (last_eq_type
== SEQ_CHARACTER
11426 && eq_type
!= SEQ_CHARACTER
11427 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
11428 &e
->where
) == FAILURE
)
11431 msg
="Non-NUMERIC object '%s' in default NUMERIC "
11432 "EQUIVALENCE statement at %L";
11433 if (last_eq_type
== SEQ_NUMERIC
11434 && eq_type
!= SEQ_NUMERIC
11435 && gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
,
11436 &e
->where
) == FAILURE
)
11441 last_where
= &e
->where
;
11446 /* Shall not be an automatic array. */
11447 if (e
->ref
->type
== REF_ARRAY
11448 && gfc_resolve_array_spec (e
->ref
->u
.ar
.as
, 1) == FAILURE
)
11450 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
11451 "an EQUIVALENCE object", sym
->name
, &e
->where
);
11458 /* Shall not be a structure component. */
11459 if (r
->type
== REF_COMPONENT
)
11461 gfc_error ("Structure component '%s' at %L cannot be an "
11462 "EQUIVALENCE object",
11463 r
->u
.c
.component
->name
, &e
->where
);
11467 /* A substring shall not have length zero. */
11468 if (r
->type
== REF_SUBSTRING
)
11470 if (compare_bound (r
->u
.ss
.start
, r
->u
.ss
.end
) == CMP_GT
)
11472 gfc_error ("Substring at %L has length zero",
11473 &r
->u
.ss
.start
->where
);
11483 /* Resolve function and ENTRY types, issue diagnostics if needed. */
11486 resolve_fntype (gfc_namespace
*ns
)
11488 gfc_entry_list
*el
;
11491 if (ns
->proc_name
== NULL
|| !ns
->proc_name
->attr
.function
)
11494 /* If there are any entries, ns->proc_name is the entry master
11495 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
11497 sym
= ns
->entries
->sym
;
11499 sym
= ns
->proc_name
;
11500 if (sym
->result
== sym
11501 && sym
->ts
.type
== BT_UNKNOWN
11502 && gfc_set_default_type (sym
, 0, NULL
) == FAILURE
11503 && !sym
->attr
.untyped
)
11505 gfc_error ("Function '%s' at %L has no IMPLICIT type",
11506 sym
->name
, &sym
->declared_at
);
11507 sym
->attr
.untyped
= 1;
11510 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.u
.derived
->attr
.use_assoc
11511 && !sym
->attr
.contained
11512 && !gfc_check_access (sym
->ts
.u
.derived
->attr
.access
,
11513 sym
->ts
.u
.derived
->ns
->default_access
)
11514 && gfc_check_access (sym
->attr
.access
, sym
->ns
->default_access
))
11516 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PUBLIC function '%s' at "
11517 "%L of PRIVATE type '%s'", sym
->name
,
11518 &sym
->declared_at
, sym
->ts
.u
.derived
->name
);
11522 for (el
= ns
->entries
->next
; el
; el
= el
->next
)
11524 if (el
->sym
->result
== el
->sym
11525 && el
->sym
->ts
.type
== BT_UNKNOWN
11526 && gfc_set_default_type (el
->sym
, 0, NULL
) == FAILURE
11527 && !el
->sym
->attr
.untyped
)
11529 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
11530 el
->sym
->name
, &el
->sym
->declared_at
);
11531 el
->sym
->attr
.untyped
= 1;
11537 /* 12.3.2.1.1 Defined operators. */
11540 check_uop_procedure (gfc_symbol
*sym
, locus where
)
11542 gfc_formal_arglist
*formal
;
11544 if (!sym
->attr
.function
)
11546 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
11547 sym
->name
, &where
);
11551 if (sym
->ts
.type
== BT_CHARACTER
11552 && !(sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
)
11553 && !(sym
->result
&& sym
->result
->ts
.u
.cl
11554 && sym
->result
->ts
.u
.cl
->length
))
11556 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
11557 "character length", sym
->name
, &where
);
11561 formal
= sym
->formal
;
11562 if (!formal
|| !formal
->sym
)
11564 gfc_error ("User operator procedure '%s' at %L must have at least "
11565 "one argument", sym
->name
, &where
);
11569 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
11571 gfc_error ("First argument of operator interface at %L must be "
11572 "INTENT(IN)", &where
);
11576 if (formal
->sym
->attr
.optional
)
11578 gfc_error ("First argument of operator interface at %L cannot be "
11579 "optional", &where
);
11583 formal
= formal
->next
;
11584 if (!formal
|| !formal
->sym
)
11587 if (formal
->sym
->attr
.intent
!= INTENT_IN
)
11589 gfc_error ("Second argument of operator interface at %L must be "
11590 "INTENT(IN)", &where
);
11594 if (formal
->sym
->attr
.optional
)
11596 gfc_error ("Second argument of operator interface at %L cannot be "
11597 "optional", &where
);
11603 gfc_error ("Operator interface at %L must have, at most, two "
11604 "arguments", &where
);
11612 gfc_resolve_uops (gfc_symtree
*symtree
)
11614 gfc_interface
*itr
;
11616 if (symtree
== NULL
)
11619 gfc_resolve_uops (symtree
->left
);
11620 gfc_resolve_uops (symtree
->right
);
11622 for (itr
= symtree
->n
.uop
->op
; itr
; itr
= itr
->next
)
11623 check_uop_procedure (itr
->sym
, itr
->sym
->declared_at
);
11627 /* Examine all of the expressions associated with a program unit,
11628 assign types to all intermediate expressions, make sure that all
11629 assignments are to compatible types and figure out which names
11630 refer to which functions or subroutines. It doesn't check code
11631 block, which is handled by resolve_code. */
11634 resolve_types (gfc_namespace
*ns
)
11640 gfc_namespace
* old_ns
= gfc_current_ns
;
11642 /* Check that all IMPLICIT types are ok. */
11643 if (!ns
->seen_implicit_none
)
11646 for (letter
= 0; letter
!= GFC_LETTERS
; ++letter
)
11647 if (ns
->set_flag
[letter
]
11648 && resolve_typespec_used (&ns
->default_type
[letter
],
11649 &ns
->implicit_loc
[letter
],
11654 gfc_current_ns
= ns
;
11656 resolve_entries (ns
);
11658 resolve_common_vars (ns
->blank_common
.head
, false);
11659 resolve_common_blocks (ns
->common_root
);
11661 resolve_contained_functions (ns
);
11663 gfc_traverse_ns (ns
, resolve_bind_c_derived_types
);
11665 for (cl
= ns
->cl_list
; cl
; cl
= cl
->next
)
11666 resolve_charlen (cl
);
11668 gfc_traverse_ns (ns
, resolve_symbol
);
11670 resolve_fntype (ns
);
11672 for (n
= ns
->contained
; n
; n
= n
->sibling
)
11674 if (gfc_pure (ns
->proc_name
) && !gfc_pure (n
->proc_name
))
11675 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
11676 "also be PURE", n
->proc_name
->name
,
11677 &n
->proc_name
->declared_at
);
11683 gfc_check_interfaces (ns
);
11685 gfc_traverse_ns (ns
, resolve_values
);
11691 for (d
= ns
->data
; d
; d
= d
->next
)
11695 gfc_traverse_ns (ns
, gfc_formalize_init_value
);
11697 gfc_traverse_ns (ns
, gfc_verify_binding_labels
);
11699 if (ns
->common_root
!= NULL
)
11700 gfc_traverse_symtree (ns
->common_root
, resolve_bind_c_comms
);
11702 for (eq
= ns
->equiv
; eq
; eq
= eq
->next
)
11703 resolve_equivalence (eq
);
11705 /* Warn about unused labels. */
11706 if (warn_unused_label
)
11707 warn_unused_fortran_label (ns
->st_labels
);
11709 gfc_resolve_uops (ns
->uop_root
);
11711 gfc_current_ns
= old_ns
;
11715 /* Call resolve_code recursively. */
11718 resolve_codes (gfc_namespace
*ns
)
11721 bitmap_obstack old_obstack
;
11723 for (n
= ns
->contained
; n
; n
= n
->sibling
)
11726 gfc_current_ns
= ns
;
11728 /* Set to an out of range value. */
11729 current_entry_id
= -1;
11731 old_obstack
= labels_obstack
;
11732 bitmap_obstack_initialize (&labels_obstack
);
11734 resolve_code (ns
->code
, ns
);
11736 bitmap_obstack_release (&labels_obstack
);
11737 labels_obstack
= old_obstack
;
11741 /* This function is called after a complete program unit has been compiled.
11742 Its purpose is to examine all of the expressions associated with a program
11743 unit, assign types to all intermediate expressions, make sure that all
11744 assignments are to compatible types and figure out which names refer to
11745 which functions or subroutines. */
11748 gfc_resolve (gfc_namespace
*ns
)
11750 gfc_namespace
*old_ns
;
11751 code_stack
*old_cs_base
;
11757 old_ns
= gfc_current_ns
;
11758 old_cs_base
= cs_base
;
11760 resolve_types (ns
);
11761 resolve_codes (ns
);
11763 gfc_current_ns
= old_ns
;
11764 cs_base
= old_cs_base
;