1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
53 bitmap reachable_labels;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL block. */
62 static int forall_flag;
64 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
66 static int omp_workshare_flag;
68 /* Nonzero if we are processing a formal arglist. The corresponding function
69 resets the flag each time that it is read. */
70 static int formal_arg_flag = 0;
72 /* True if we are resolving a specification expression. */
73 static int specification_expr = 0;
75 /* The id of the last entry seen. */
76 static int current_entry_id;
78 /* We use bitmaps to determine if a branch target is valid. */
79 static bitmap_obstack labels_obstack;
81 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
82 static bool inquiry_argument = false;
85 gfc_is_formal_arg (void)
87 return formal_arg_flag;
90 /* Is the symbol host associated? */
92 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
94 for (ns = ns->parent; ns; ns = ns->parent)
103 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
104 an ABSTRACT derived-type. If where is not NULL, an error message with that
105 locus is printed, optionally using name. */
108 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
110 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
115 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
116 name, where, ts->u.derived->name);
118 gfc_error ("ABSTRACT type '%s' used at %L",
119 ts->u.derived->name, where);
129 /* Resolve types of formal argument lists. These have to be done early so that
130 the formal argument lists of module procedures can be copied to the
131 containing module before the individual procedures are resolved
132 individually. We also resolve argument lists of procedures in interface
133 blocks because they are self-contained scoping units.
135 Since a dummy argument cannot be a non-dummy procedure, the only
136 resort left for untyped names are the IMPLICIT types. */
139 resolve_formal_arglist (gfc_symbol *proc)
141 gfc_formal_arglist *f;
145 if (proc->result != NULL)
150 if (gfc_elemental (proc)
151 || sym->attr.pointer || sym->attr.allocatable
152 || (sym->as && sym->as->rank > 0))
154 proc->attr.always_explicit = 1;
155 sym->attr.always_explicit = 1;
160 for (f = proc->formal; f; f = f->next)
166 /* Alternate return placeholder. */
167 if (gfc_elemental (proc))
168 gfc_error ("Alternate return specifier in elemental subroutine "
169 "'%s' at %L is not allowed", proc->name,
171 if (proc->attr.function)
172 gfc_error ("Alternate return specifier in function "
173 "'%s' at %L is not allowed", proc->name,
178 if (sym->attr.if_source != IFSRC_UNKNOWN)
179 resolve_formal_arglist (sym);
181 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
183 if (gfc_pure (proc) && !gfc_pure (sym))
185 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
186 "also be PURE", sym->name, &sym->declared_at);
190 if (gfc_elemental (proc))
192 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
193 "procedure", &sym->declared_at);
197 if (sym->attr.function
198 && sym->ts.type == BT_UNKNOWN
199 && sym->attr.intrinsic)
201 gfc_intrinsic_sym *isym;
202 isym = gfc_find_function (sym->name);
203 if (isym == NULL || !isym->specific)
205 gfc_error ("Unable to find a specific INTRINSIC procedure "
206 "for the reference '%s' at %L", sym->name,
215 if (sym->ts.type == BT_UNKNOWN)
217 if (!sym->attr.function || sym->result == sym)
218 gfc_set_default_type (sym, 1, sym->ns);
221 gfc_resolve_array_spec (sym->as, 0);
223 /* We can't tell if an array with dimension (:) is assumed or deferred
224 shape until we know if it has the pointer or allocatable attributes.
226 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
227 && !(sym->attr.pointer || sym->attr.allocatable))
229 sym->as->type = AS_ASSUMED_SHAPE;
230 for (i = 0; i < sym->as->rank; i++)
231 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
235 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
236 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
237 || sym->attr.optional)
239 proc->attr.always_explicit = 1;
241 proc->result->attr.always_explicit = 1;
244 /* If the flavor is unknown at this point, it has to be a variable.
245 A procedure specification would have already set the type. */
247 if (sym->attr.flavor == FL_UNKNOWN)
248 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
250 if (gfc_pure (proc) && !sym->attr.pointer
251 && sym->attr.flavor != FL_PROCEDURE)
253 if (proc->attr.function && sym->attr.intent != INTENT_IN)
254 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
255 "INTENT(IN)", sym->name, proc->name,
258 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
259 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
260 "have its INTENT specified", sym->name, proc->name,
264 if (gfc_elemental (proc))
267 if (sym->attr.codimension)
269 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
270 "procedure", sym->name, &sym->declared_at);
276 gfc_error ("Argument '%s' of elemental procedure at %L must "
277 "be scalar", sym->name, &sym->declared_at);
281 if (sym->attr.pointer)
283 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
284 "have the POINTER attribute", sym->name,
289 if (sym->attr.flavor == FL_PROCEDURE)
291 gfc_error ("Dummy procedure '%s' not allowed in elemental "
292 "procedure '%s' at %L", sym->name, proc->name,
298 /* Each dummy shall be specified to be scalar. */
299 if (proc->attr.proc == PROC_ST_FUNCTION)
303 gfc_error ("Argument '%s' of statement function at %L must "
304 "be scalar", sym->name, &sym->declared_at);
308 if (sym->ts.type == BT_CHARACTER)
310 gfc_charlen *cl = sym->ts.u.cl;
311 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
313 gfc_error ("Character-valued argument '%s' of statement "
314 "function at %L must have constant length",
315 sym->name, &sym->declared_at);
325 /* Work function called when searching for symbols that have argument lists
326 associated with them. */
329 find_arglists (gfc_symbol *sym)
331 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
334 resolve_formal_arglist (sym);
338 /* Given a namespace, resolve all formal argument lists within the namespace.
342 resolve_formal_arglists (gfc_namespace *ns)
347 gfc_traverse_ns (ns, find_arglists);
352 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
356 /* If this namespace is not a function or an entry master function,
358 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
359 || sym->attr.entry_master)
362 /* Try to find out of what the return type is. */
363 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
365 t = gfc_set_default_type (sym->result, 0, ns);
367 if (t == FAILURE && !sym->result->attr.untyped)
369 if (sym->result == sym)
370 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
371 sym->name, &sym->declared_at);
372 else if (!sym->result->attr.proc_pointer)
373 gfc_error ("Result '%s' of contained function '%s' at %L has "
374 "no IMPLICIT type", sym->result->name, sym->name,
375 &sym->result->declared_at);
376 sym->result->attr.untyped = 1;
380 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
381 type, lists the only ways a character length value of * can be used:
382 dummy arguments of procedures, named constants, and function results
383 in external functions. Internal function results and results of module
384 procedures are not on this list, ergo, not permitted. */
386 if (sym->result->ts.type == BT_CHARACTER)
388 gfc_charlen *cl = sym->result->ts.u.cl;
389 if (!cl || !cl->length)
391 /* See if this is a module-procedure and adapt error message
394 gcc_assert (ns->parent && ns->parent->proc_name);
395 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
397 gfc_error ("Character-valued %s '%s' at %L must not be"
399 module_proc ? _("module procedure")
400 : _("internal function"),
401 sym->name, &sym->declared_at);
407 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
408 introduce duplicates. */
411 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
413 gfc_formal_arglist *f, *new_arglist;
416 for (; new_args != NULL; new_args = new_args->next)
418 new_sym = new_args->sym;
419 /* See if this arg is already in the formal argument list. */
420 for (f = proc->formal; f; f = f->next)
422 if (new_sym == f->sym)
429 /* Add a new argument. Argument order is not important. */
430 new_arglist = gfc_get_formal_arglist ();
431 new_arglist->sym = new_sym;
432 new_arglist->next = proc->formal;
433 proc->formal = new_arglist;
438 /* Flag the arguments that are not present in all entries. */
441 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
443 gfc_formal_arglist *f, *head;
446 for (f = proc->formal; f; f = f->next)
451 for (new_args = head; new_args; new_args = new_args->next)
453 if (new_args->sym == f->sym)
460 f->sym->attr.not_always_present = 1;
465 /* Resolve alternate entry points. If a symbol has multiple entry points we
466 create a new master symbol for the main routine, and turn the existing
467 symbol into an entry point. */
470 resolve_entries (gfc_namespace *ns)
472 gfc_namespace *old_ns;
476 char name[GFC_MAX_SYMBOL_LEN + 1];
477 static int master_count = 0;
479 if (ns->proc_name == NULL)
482 /* No need to do anything if this procedure doesn't have alternate entry
487 /* We may already have resolved alternate entry points. */
488 if (ns->proc_name->attr.entry_master)
491 /* If this isn't a procedure something has gone horribly wrong. */
492 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
494 /* Remember the current namespace. */
495 old_ns = gfc_current_ns;
499 /* Add the main entry point to the list of entry points. */
500 el = gfc_get_entry_list ();
501 el->sym = ns->proc_name;
503 el->next = ns->entries;
505 ns->proc_name->attr.entry = 1;
507 /* If it is a module function, it needs to be in the right namespace
508 so that gfc_get_fake_result_decl can gather up the results. The
509 need for this arose in get_proc_name, where these beasts were
510 left in their own namespace, to keep prior references linked to
511 the entry declaration.*/
512 if (ns->proc_name->attr.function
513 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
516 /* Do the same for entries where the master is not a module
517 procedure. These are retained in the module namespace because
518 of the module procedure declaration. */
519 for (el = el->next; el; el = el->next)
520 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
521 && el->sym->attr.mod_proc)
525 /* Add an entry statement for it. */
532 /* Create a new symbol for the master function. */
533 /* Give the internal function a unique name (within this file).
534 Also include the function name so the user has some hope of figuring
535 out what is going on. */
536 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
537 master_count++, ns->proc_name->name);
538 gfc_get_ha_symbol (name, &proc);
539 gcc_assert (proc != NULL);
541 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
542 if (ns->proc_name->attr.subroutine)
543 gfc_add_subroutine (&proc->attr, proc->name, NULL);
547 gfc_typespec *ts, *fts;
548 gfc_array_spec *as, *fas;
549 gfc_add_function (&proc->attr, proc->name, NULL);
551 fas = ns->entries->sym->as;
552 fas = fas ? fas : ns->entries->sym->result->as;
553 fts = &ns->entries->sym->result->ts;
554 if (fts->type == BT_UNKNOWN)
555 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
556 for (el = ns->entries->next; el; el = el->next)
558 ts = &el->sym->result->ts;
560 as = as ? as : el->sym->result->as;
561 if (ts->type == BT_UNKNOWN)
562 ts = gfc_get_default_type (el->sym->result->name, NULL);
564 if (! gfc_compare_types (ts, fts)
565 || (el->sym->result->attr.dimension
566 != ns->entries->sym->result->attr.dimension)
567 || (el->sym->result->attr.pointer
568 != ns->entries->sym->result->attr.pointer))
570 else if (as && fas && ns->entries->sym->result != el->sym->result
571 && gfc_compare_array_spec (as, fas) == 0)
572 gfc_error ("Function %s at %L has entries with mismatched "
573 "array specifications", ns->entries->sym->name,
574 &ns->entries->sym->declared_at);
575 /* The characteristics need to match and thus both need to have
576 the same string length, i.e. both len=*, or both len=4.
577 Having both len=<variable> is also possible, but difficult to
578 check at compile time. */
579 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
580 && (((ts->u.cl->length && !fts->u.cl->length)
581 ||(!ts->u.cl->length && fts->u.cl->length))
583 && ts->u.cl->length->expr_type
584 != fts->u.cl->length->expr_type)
586 && ts->u.cl->length->expr_type == EXPR_CONSTANT
587 && mpz_cmp (ts->u.cl->length->value.integer,
588 fts->u.cl->length->value.integer) != 0)))
589 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
590 "entries returning variables of different "
591 "string lengths", ns->entries->sym->name,
592 &ns->entries->sym->declared_at);
597 sym = ns->entries->sym->result;
598 /* All result types the same. */
600 if (sym->attr.dimension)
601 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
602 if (sym->attr.pointer)
603 gfc_add_pointer (&proc->attr, NULL);
607 /* Otherwise the result will be passed through a union by
609 proc->attr.mixed_entry_master = 1;
610 for (el = ns->entries; el; el = el->next)
612 sym = el->sym->result;
613 if (sym->attr.dimension)
615 if (el == ns->entries)
616 gfc_error ("FUNCTION result %s can't be an array in "
617 "FUNCTION %s at %L", sym->name,
618 ns->entries->sym->name, &sym->declared_at);
620 gfc_error ("ENTRY result %s can't be an array in "
621 "FUNCTION %s at %L", sym->name,
622 ns->entries->sym->name, &sym->declared_at);
624 else if (sym->attr.pointer)
626 if (el == ns->entries)
627 gfc_error ("FUNCTION result %s can't be a POINTER in "
628 "FUNCTION %s at %L", sym->name,
629 ns->entries->sym->name, &sym->declared_at);
631 gfc_error ("ENTRY result %s can't be a POINTER in "
632 "FUNCTION %s at %L", sym->name,
633 ns->entries->sym->name, &sym->declared_at);
638 if (ts->type == BT_UNKNOWN)
639 ts = gfc_get_default_type (sym->name, NULL);
643 if (ts->kind == gfc_default_integer_kind)
647 if (ts->kind == gfc_default_real_kind
648 || ts->kind == gfc_default_double_kind)
652 if (ts->kind == gfc_default_complex_kind)
656 if (ts->kind == gfc_default_logical_kind)
660 /* We will issue error elsewhere. */
668 if (el == ns->entries)
669 gfc_error ("FUNCTION result %s can't be of type %s "
670 "in FUNCTION %s at %L", sym->name,
671 gfc_typename (ts), ns->entries->sym->name,
674 gfc_error ("ENTRY result %s can't be of type %s "
675 "in FUNCTION %s at %L", sym->name,
676 gfc_typename (ts), ns->entries->sym->name,
683 proc->attr.access = ACCESS_PRIVATE;
684 proc->attr.entry_master = 1;
686 /* Merge all the entry point arguments. */
687 for (el = ns->entries; el; el = el->next)
688 merge_argument_lists (proc, el->sym->formal);
690 /* Check the master formal arguments for any that are not
691 present in all entry points. */
692 for (el = ns->entries; el; el = el->next)
693 check_argument_lists (proc, el->sym->formal);
695 /* Use the master function for the function body. */
696 ns->proc_name = proc;
698 /* Finalize the new symbols. */
699 gfc_commit_symbols ();
701 /* Restore the original namespace. */
702 gfc_current_ns = old_ns;
706 /* Resolve common variables. */
708 resolve_common_vars (gfc_symbol *sym, bool named_common)
710 gfc_symbol *csym = sym;
712 for (; csym; csym = csym->common_next)
714 if (csym->value || csym->attr.data)
716 if (!csym->ns->is_block_data)
717 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
718 "but only in BLOCK DATA initialization is "
719 "allowed", csym->name, &csym->declared_at);
720 else if (!named_common)
721 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
722 "in a blank COMMON but initialization is only "
723 "allowed in named common blocks", csym->name,
727 if (csym->ts.type != BT_DERIVED)
730 if (!(csym->ts.u.derived->attr.sequence
731 || csym->ts.u.derived->attr.is_bind_c))
732 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
733 "has neither the SEQUENCE nor the BIND(C) "
734 "attribute", csym->name, &csym->declared_at);
735 if (csym->ts.u.derived->attr.alloc_comp)
736 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
737 "has an ultimate component that is "
738 "allocatable", csym->name, &csym->declared_at);
739 if (gfc_has_default_initializer (csym->ts.u.derived))
740 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
741 "may not have default initializer", csym->name,
744 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
745 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
749 /* Resolve common blocks. */
751 resolve_common_blocks (gfc_symtree *common_root)
755 if (common_root == NULL)
758 if (common_root->left)
759 resolve_common_blocks (common_root->left);
760 if (common_root->right)
761 resolve_common_blocks (common_root->right);
763 resolve_common_vars (common_root->n.common->head, true);
765 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
769 if (sym->attr.flavor == FL_PARAMETER)
770 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
771 sym->name, &common_root->n.common->where, &sym->declared_at);
773 if (sym->attr.intrinsic)
774 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
775 sym->name, &common_root->n.common->where);
776 else if (sym->attr.result
777 || gfc_is_function_return_value (sym, gfc_current_ns))
778 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
779 "that is also a function result", sym->name,
780 &common_root->n.common->where);
781 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
782 && sym->attr.proc != PROC_ST_FUNCTION)
783 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
784 "that is also a global procedure", sym->name,
785 &common_root->n.common->where);
789 /* Resolve contained function types. Because contained functions can call one
790 another, they have to be worked out before any of the contained procedures
793 The good news is that if a function doesn't already have a type, the only
794 way it can get one is through an IMPLICIT type or a RESULT variable, because
795 by definition contained functions are contained namespace they're contained
796 in, not in a sibling or parent namespace. */
799 resolve_contained_functions (gfc_namespace *ns)
801 gfc_namespace *child;
804 resolve_formal_arglists (ns);
806 for (child = ns->contained; child; child = child->sibling)
808 /* Resolve alternate entry points first. */
809 resolve_entries (child);
811 /* Then check function return types. */
812 resolve_contained_fntype (child->proc_name, child);
813 for (el = child->entries; el; el = el->next)
814 resolve_contained_fntype (el->sym, child);
819 /* Resolve all of the elements of a structure constructor and make sure that
820 the types are correct. */
823 resolve_structure_cons (gfc_expr *expr)
825 gfc_constructor *cons;
831 cons = gfc_constructor_first (expr->value.constructor);
832 /* A constructor may have references if it is the result of substituting a
833 parameter variable. In this case we just pull out the component we
836 comp = expr->ref->u.c.sym->components;
838 comp = expr->ts.u.derived->components;
840 /* See if the user is trying to invoke a structure constructor for one of
841 the iso_c_binding derived types. */
842 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
843 && expr->ts.u.derived->ts.is_iso_c && cons
844 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
846 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
847 expr->ts.u.derived->name, &(expr->where));
851 /* Return if structure constructor is c_null_(fun)prt. */
852 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
853 && expr->ts.u.derived->ts.is_iso_c && cons
854 && cons->expr && cons->expr->expr_type == EXPR_NULL)
857 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
864 if (gfc_resolve_expr (cons->expr) == FAILURE)
870 rank = comp->as ? comp->as->rank : 0;
871 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
872 && (comp->attr.allocatable || cons->expr->rank))
874 gfc_error ("The rank of the element in the derived type "
875 "constructor at %L does not match that of the "
876 "component (%d/%d)", &cons->expr->where,
877 cons->expr->rank, rank);
881 /* If we don't have the right type, try to convert it. */
883 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
886 if (strcmp (comp->name, "$extends") == 0)
888 /* Can afford to be brutal with the $extends initializer.
889 The derived type can get lost because it is PRIVATE
890 but it is not usage constrained by the standard. */
891 cons->expr->ts = comp->ts;
894 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
895 gfc_error ("The element in the derived type constructor at %L, "
896 "for pointer component '%s', is %s but should be %s",
897 &cons->expr->where, comp->name,
898 gfc_basic_typename (cons->expr->ts.type),
899 gfc_basic_typename (comp->ts.type));
901 t = gfc_convert_type (cons->expr, &comp->ts, 1);
904 /* For strings, the length of the constructor should be the same as
905 the one of the structure, ensure this if the lengths are known at
906 compile time and when we are dealing with PARAMETER or structure
908 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
909 && comp->ts.u.cl->length
910 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
911 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
912 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
913 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
914 comp->ts.u.cl->length->value.integer) != 0)
916 if (cons->expr->expr_type == EXPR_VARIABLE
917 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
919 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
920 to make use of the gfc_resolve_character_array_constructor
921 machinery. The expression is later simplified away to
922 an array of string literals. */
923 gfc_expr *para = cons->expr;
924 cons->expr = gfc_get_expr ();
925 cons->expr->ts = para->ts;
926 cons->expr->where = para->where;
927 cons->expr->expr_type = EXPR_ARRAY;
928 cons->expr->rank = para->rank;
929 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
930 gfc_constructor_append_expr (&cons->expr->value.constructor,
931 para, &cons->expr->where);
933 if (cons->expr->expr_type == EXPR_ARRAY)
936 p = gfc_constructor_first (cons->expr->value.constructor);
937 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
939 gfc_charlen *cl, *cl2;
942 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
944 if (cl == cons->expr->ts.u.cl)
952 cl2->next = cl->next;
954 gfc_free_expr (cl->length);
958 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
959 cons->expr->ts.u.cl->length_from_typespec = true;
960 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
961 gfc_resolve_character_array_constructor (cons->expr);
965 if (cons->expr->expr_type == EXPR_NULL
966 && !(comp->attr.pointer || comp->attr.allocatable
967 || comp->attr.proc_pointer
968 || (comp->ts.type == BT_CLASS
969 && (CLASS_DATA (comp)->attr.class_pointer
970 || CLASS_DATA (comp)->attr.allocatable))))
973 gfc_error ("The NULL in the derived type constructor at %L is "
974 "being applied to component '%s', which is neither "
975 "a POINTER nor ALLOCATABLE", &cons->expr->where,
979 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
982 a = gfc_expr_attr (cons->expr);
984 if (!a.pointer && !a.target)
987 gfc_error ("The element in the derived type constructor at %L, "
988 "for pointer component '%s' should be a POINTER or "
989 "a TARGET", &cons->expr->where, comp->name);
992 /* F2003, C1272 (3). */
993 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
994 && (gfc_impure_variable (cons->expr->symtree->n.sym)
995 || gfc_is_coindexed (cons->expr)))
998 gfc_error ("Invalid expression in the derived type constructor for "
999 "pointer component '%s' at %L in PURE procedure",
1000 comp->name, &cons->expr->where);
1008 /****************** Expression name resolution ******************/
1010 /* Returns 0 if a symbol was not declared with a type or
1011 attribute declaration statement, nonzero otherwise. */
1014 was_declared (gfc_symbol *sym)
1020 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1023 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1024 || a.optional || a.pointer || a.save || a.target || a.volatile_
1025 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1026 || a.asynchronous || a.codimension)
1033 /* Determine if a symbol is generic or not. */
1036 generic_sym (gfc_symbol *sym)
1040 if (sym->attr.generic ||
1041 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1044 if (was_declared (sym) || sym->ns->parent == NULL)
1047 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1054 return generic_sym (s);
1061 /* Determine if a symbol is specific or not. */
1064 specific_sym (gfc_symbol *sym)
1068 if (sym->attr.if_source == IFSRC_IFBODY
1069 || sym->attr.proc == PROC_MODULE
1070 || sym->attr.proc == PROC_INTERNAL
1071 || sym->attr.proc == PROC_ST_FUNCTION
1072 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1073 || sym->attr.external)
1076 if (was_declared (sym) || sym->ns->parent == NULL)
1079 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1081 return (s == NULL) ? 0 : specific_sym (s);
1085 /* Figure out if the procedure is specific, generic or unknown. */
1088 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1092 procedure_kind (gfc_symbol *sym)
1094 if (generic_sym (sym))
1095 return PTYPE_GENERIC;
1097 if (specific_sym (sym))
1098 return PTYPE_SPECIFIC;
1100 return PTYPE_UNKNOWN;
1103 /* Check references to assumed size arrays. The flag need_full_assumed_size
1104 is nonzero when matching actual arguments. */
1106 static int need_full_assumed_size = 0;
1109 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1111 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1114 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1115 What should it be? */
1116 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1117 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1118 && (e->ref->u.ar.type == AR_FULL))
1120 gfc_error ("The upper bound in the last dimension must "
1121 "appear in the reference to the assumed size "
1122 "array '%s' at %L", sym->name, &e->where);
1129 /* Look for bad assumed size array references in argument expressions
1130 of elemental and array valued intrinsic procedures. Since this is
1131 called from procedure resolution functions, it only recurses at
1135 resolve_assumed_size_actual (gfc_expr *e)
1140 switch (e->expr_type)
1143 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1148 if (resolve_assumed_size_actual (e->value.op.op1)
1149 || resolve_assumed_size_actual (e->value.op.op2))
1160 /* Check a generic procedure, passed as an actual argument, to see if
1161 there is a matching specific name. If none, it is an error, and if
1162 more than one, the reference is ambiguous. */
1164 count_specific_procs (gfc_expr *e)
1171 sym = e->symtree->n.sym;
1173 for (p = sym->generic; p; p = p->next)
1174 if (strcmp (sym->name, p->sym->name) == 0)
1176 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1182 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1186 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1187 "argument at %L", sym->name, &e->where);
1193 /* See if a call to sym could possibly be a not allowed RECURSION because of
1194 a missing RECURIVE declaration. This means that either sym is the current
1195 context itself, or sym is the parent of a contained procedure calling its
1196 non-RECURSIVE containing procedure.
1197 This also works if sym is an ENTRY. */
1200 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1202 gfc_symbol* proc_sym;
1203 gfc_symbol* context_proc;
1204 gfc_namespace* real_context;
1206 if (sym->attr.flavor == FL_PROGRAM)
1209 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1211 /* If we've got an ENTRY, find real procedure. */
1212 if (sym->attr.entry && sym->ns->entries)
1213 proc_sym = sym->ns->entries->sym;
1217 /* If sym is RECURSIVE, all is well of course. */
1218 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1221 /* Find the context procedure's "real" symbol if it has entries.
1222 We look for a procedure symbol, so recurse on the parents if we don't
1223 find one (like in case of a BLOCK construct). */
1224 for (real_context = context; ; real_context = real_context->parent)
1226 /* We should find something, eventually! */
1227 gcc_assert (real_context);
1229 context_proc = (real_context->entries ? real_context->entries->sym
1230 : real_context->proc_name);
1232 /* In some special cases, there may not be a proc_name, like for this
1234 real(bad_kind()) function foo () ...
1235 when checking the call to bad_kind ().
1236 In these cases, we simply return here and assume that the
1241 if (context_proc->attr.flavor != FL_LABEL)
1245 /* A call from sym's body to itself is recursion, of course. */
1246 if (context_proc == proc_sym)
1249 /* The same is true if context is a contained procedure and sym the
1251 if (context_proc->attr.contained)
1253 gfc_symbol* parent_proc;
1255 gcc_assert (context->parent);
1256 parent_proc = (context->parent->entries ? context->parent->entries->sym
1257 : context->parent->proc_name);
1259 if (parent_proc == proc_sym)
1267 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1268 its typespec and formal argument list. */
1271 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1273 gfc_intrinsic_sym* isym;
1279 /* We already know this one is an intrinsic, so we don't call
1280 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1281 gfc_find_subroutine directly to check whether it is a function or
1284 if ((isym = gfc_find_function (sym->name)))
1286 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1287 && !sym->attr.implicit_type)
1288 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1289 " ignored", sym->name, &sym->declared_at);
1291 if (!sym->attr.function &&
1292 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1297 else if ((isym = gfc_find_subroutine (sym->name)))
1299 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1301 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1302 " specifier", sym->name, &sym->declared_at);
1306 if (!sym->attr.subroutine &&
1307 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1312 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1317 gfc_copy_formal_args_intr (sym, isym);
1319 /* Check it is actually available in the standard settings. */
1320 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1323 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1324 " available in the current standard settings but %s. Use"
1325 " an appropriate -std=* option or enable -fall-intrinsics"
1326 " in order to use it.",
1327 sym->name, &sym->declared_at, symstd);
1335 /* Resolve a procedure expression, like passing it to a called procedure or as
1336 RHS for a procedure pointer assignment. */
1339 resolve_procedure_expression (gfc_expr* expr)
1343 if (expr->expr_type != EXPR_VARIABLE)
1345 gcc_assert (expr->symtree);
1347 sym = expr->symtree->n.sym;
1349 if (sym->attr.intrinsic)
1350 resolve_intrinsic (sym, &expr->where);
1352 if (sym->attr.flavor != FL_PROCEDURE
1353 || (sym->attr.function && sym->result == sym))
1356 /* A non-RECURSIVE procedure that is used as procedure expression within its
1357 own body is in danger of being called recursively. */
1358 if (is_illegal_recursion (sym, gfc_current_ns))
1359 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1360 " itself recursively. Declare it RECURSIVE or use"
1361 " -frecursive", sym->name, &expr->where);
1367 /* Resolve an actual argument list. Most of the time, this is just
1368 resolving the expressions in the list.
1369 The exception is that we sometimes have to decide whether arguments
1370 that look like procedure arguments are really simple variable
1374 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1375 bool no_formal_args)
1378 gfc_symtree *parent_st;
1380 int save_need_full_assumed_size;
1381 gfc_component *comp;
1383 for (; arg; arg = arg->next)
1388 /* Check the label is a valid branching target. */
1391 if (arg->label->defined == ST_LABEL_UNKNOWN)
1393 gfc_error ("Label %d referenced at %L is never defined",
1394 arg->label->value, &arg->label->where);
1401 if (gfc_is_proc_ptr_comp (e, &comp))
1404 if (e->expr_type == EXPR_PPC)
1406 if (comp->as != NULL)
1407 e->rank = comp->as->rank;
1408 e->expr_type = EXPR_FUNCTION;
1410 if (gfc_resolve_expr (e) == FAILURE)
1415 if (e->expr_type == EXPR_VARIABLE
1416 && e->symtree->n.sym->attr.generic
1418 && count_specific_procs (e) != 1)
1421 if (e->ts.type != BT_PROCEDURE)
1423 save_need_full_assumed_size = need_full_assumed_size;
1424 if (e->expr_type != EXPR_VARIABLE)
1425 need_full_assumed_size = 0;
1426 if (gfc_resolve_expr (e) != SUCCESS)
1428 need_full_assumed_size = save_need_full_assumed_size;
1432 /* See if the expression node should really be a variable reference. */
1434 sym = e->symtree->n.sym;
1436 if (sym->attr.flavor == FL_PROCEDURE
1437 || sym->attr.intrinsic
1438 || sym->attr.external)
1442 /* If a procedure is not already determined to be something else
1443 check if it is intrinsic. */
1444 if (!sym->attr.intrinsic
1445 && !(sym->attr.external || sym->attr.use_assoc
1446 || sym->attr.if_source == IFSRC_IFBODY)
1447 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1448 sym->attr.intrinsic = 1;
1450 if (sym->attr.proc == PROC_ST_FUNCTION)
1452 gfc_error ("Statement function '%s' at %L is not allowed as an "
1453 "actual argument", sym->name, &e->where);
1456 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1457 sym->attr.subroutine);
1458 if (sym->attr.intrinsic && actual_ok == 0)
1460 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1461 "actual argument", sym->name, &e->where);
1464 if (sym->attr.contained && !sym->attr.use_assoc
1465 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1467 gfc_error ("Internal procedure '%s' is not allowed as an "
1468 "actual argument at %L", sym->name, &e->where);
1471 if (sym->attr.elemental && !sym->attr.intrinsic)
1473 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1474 "allowed as an actual argument at %L", sym->name,
1478 /* Check if a generic interface has a specific procedure
1479 with the same name before emitting an error. */
1480 if (sym->attr.generic && count_specific_procs (e) != 1)
1483 /* Just in case a specific was found for the expression. */
1484 sym = e->symtree->n.sym;
1486 /* If the symbol is the function that names the current (or
1487 parent) scope, then we really have a variable reference. */
1489 if (gfc_is_function_return_value (sym, sym->ns))
1492 /* If all else fails, see if we have a specific intrinsic. */
1493 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1495 gfc_intrinsic_sym *isym;
1497 isym = gfc_find_function (sym->name);
1498 if (isym == NULL || !isym->specific)
1500 gfc_error ("Unable to find a specific INTRINSIC procedure "
1501 "for the reference '%s' at %L", sym->name,
1506 sym->attr.intrinsic = 1;
1507 sym->attr.function = 1;
1510 if (gfc_resolve_expr (e) == FAILURE)
1515 /* See if the name is a module procedure in a parent unit. */
1517 if (was_declared (sym) || sym->ns->parent == NULL)
1520 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1522 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1526 if (parent_st == NULL)
1529 sym = parent_st->n.sym;
1530 e->symtree = parent_st; /* Point to the right thing. */
1532 if (sym->attr.flavor == FL_PROCEDURE
1533 || sym->attr.intrinsic
1534 || sym->attr.external)
1536 if (gfc_resolve_expr (e) == FAILURE)
1542 e->expr_type = EXPR_VARIABLE;
1544 if (sym->as != NULL)
1546 e->rank = sym->as->rank;
1547 e->ref = gfc_get_ref ();
1548 e->ref->type = REF_ARRAY;
1549 e->ref->u.ar.type = AR_FULL;
1550 e->ref->u.ar.as = sym->as;
1553 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1554 primary.c (match_actual_arg). If above code determines that it
1555 is a variable instead, it needs to be resolved as it was not
1556 done at the beginning of this function. */
1557 save_need_full_assumed_size = need_full_assumed_size;
1558 if (e->expr_type != EXPR_VARIABLE)
1559 need_full_assumed_size = 0;
1560 if (gfc_resolve_expr (e) != SUCCESS)
1562 need_full_assumed_size = save_need_full_assumed_size;
1565 /* Check argument list functions %VAL, %LOC and %REF. There is
1566 nothing to do for %REF. */
1567 if (arg->name && arg->name[0] == '%')
1569 if (strncmp ("%VAL", arg->name, 4) == 0)
1571 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1573 gfc_error ("By-value argument at %L is not of numeric "
1580 gfc_error ("By-value argument at %L cannot be an array or "
1581 "an array section", &e->where);
1585 /* Intrinsics are still PROC_UNKNOWN here. However,
1586 since same file external procedures are not resolvable
1587 in gfortran, it is a good deal easier to leave them to
1589 if (ptype != PROC_UNKNOWN
1590 && ptype != PROC_DUMMY
1591 && ptype != PROC_EXTERNAL
1592 && ptype != PROC_MODULE)
1594 gfc_error ("By-value argument at %L is not allowed "
1595 "in this context", &e->where);
1600 /* Statement functions have already been excluded above. */
1601 else if (strncmp ("%LOC", arg->name, 4) == 0
1602 && e->ts.type == BT_PROCEDURE)
1604 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1606 gfc_error ("Passing internal procedure at %L by location "
1607 "not allowed", &e->where);
1613 /* Fortran 2008, C1237. */
1614 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1615 && gfc_has_ultimate_pointer (e))
1617 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1618 "component", &e->where);
1627 /* Do the checks of the actual argument list that are specific to elemental
1628 procedures. If called with c == NULL, we have a function, otherwise if
1629 expr == NULL, we have a subroutine. */
1632 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1634 gfc_actual_arglist *arg0;
1635 gfc_actual_arglist *arg;
1636 gfc_symbol *esym = NULL;
1637 gfc_intrinsic_sym *isym = NULL;
1639 gfc_intrinsic_arg *iformal = NULL;
1640 gfc_formal_arglist *eformal = NULL;
1641 bool formal_optional = false;
1642 bool set_by_optional = false;
1646 /* Is this an elemental procedure? */
1647 if (expr && expr->value.function.actual != NULL)
1649 if (expr->value.function.esym != NULL
1650 && expr->value.function.esym->attr.elemental)
1652 arg0 = expr->value.function.actual;
1653 esym = expr->value.function.esym;
1655 else if (expr->value.function.isym != NULL
1656 && expr->value.function.isym->elemental)
1658 arg0 = expr->value.function.actual;
1659 isym = expr->value.function.isym;
1664 else if (c && c->ext.actual != NULL)
1666 arg0 = c->ext.actual;
1668 if (c->resolved_sym)
1669 esym = c->resolved_sym;
1671 esym = c->symtree->n.sym;
1674 if (!esym->attr.elemental)
1680 /* The rank of an elemental is the rank of its array argument(s). */
1681 for (arg = arg0; arg; arg = arg->next)
1683 if (arg->expr != NULL && arg->expr->rank > 0)
1685 rank = arg->expr->rank;
1686 if (arg->expr->expr_type == EXPR_VARIABLE
1687 && arg->expr->symtree->n.sym->attr.optional)
1688 set_by_optional = true;
1690 /* Function specific; set the result rank and shape. */
1694 if (!expr->shape && arg->expr->shape)
1696 expr->shape = gfc_get_shape (rank);
1697 for (i = 0; i < rank; i++)
1698 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1705 /* If it is an array, it shall not be supplied as an actual argument
1706 to an elemental procedure unless an array of the same rank is supplied
1707 as an actual argument corresponding to a nonoptional dummy argument of
1708 that elemental procedure(12.4.1.5). */
1709 formal_optional = false;
1711 iformal = isym->formal;
1713 eformal = esym->formal;
1715 for (arg = arg0; arg; arg = arg->next)
1719 if (eformal->sym && eformal->sym->attr.optional)
1720 formal_optional = true;
1721 eformal = eformal->next;
1723 else if (isym && iformal)
1725 if (iformal->optional)
1726 formal_optional = true;
1727 iformal = iformal->next;
1730 formal_optional = true;
1732 if (pedantic && arg->expr != NULL
1733 && arg->expr->expr_type == EXPR_VARIABLE
1734 && arg->expr->symtree->n.sym->attr.optional
1737 && (set_by_optional || arg->expr->rank != rank)
1738 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1740 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1741 "MISSING, it cannot be the actual argument of an "
1742 "ELEMENTAL procedure unless there is a non-optional "
1743 "argument with the same rank (12.4.1.5)",
1744 arg->expr->symtree->n.sym->name, &arg->expr->where);
1749 for (arg = arg0; arg; arg = arg->next)
1751 if (arg->expr == NULL || arg->expr->rank == 0)
1754 /* Being elemental, the last upper bound of an assumed size array
1755 argument must be present. */
1756 if (resolve_assumed_size_actual (arg->expr))
1759 /* Elemental procedure's array actual arguments must conform. */
1762 if (gfc_check_conformance (arg->expr, e,
1763 "elemental procedure") == FAILURE)
1770 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1771 is an array, the intent inout/out variable needs to be also an array. */
1772 if (rank > 0 && esym && expr == NULL)
1773 for (eformal = esym->formal, arg = arg0; arg && eformal;
1774 arg = arg->next, eformal = eformal->next)
1775 if ((eformal->sym->attr.intent == INTENT_OUT
1776 || eformal->sym->attr.intent == INTENT_INOUT)
1777 && arg->expr && arg->expr->rank == 0)
1779 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1780 "ELEMENTAL subroutine '%s' is a scalar, but another "
1781 "actual argument is an array", &arg->expr->where,
1782 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1783 : "INOUT", eformal->sym->name, esym->name);
1790 /* Go through each actual argument in ACTUAL and see if it can be
1791 implemented as an inlined, non-copying intrinsic. FNSYM is the
1792 function being called, or NULL if not known. */
1795 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1797 gfc_actual_arglist *ap;
1800 for (ap = actual; ap; ap = ap->next)
1802 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1803 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1805 ap->expr->inline_noncopying_intrinsic = 1;
1809 /* This function does the checking of references to global procedures
1810 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1811 77 and 95 standards. It checks for a gsymbol for the name, making
1812 one if it does not already exist. If it already exists, then the
1813 reference being resolved must correspond to the type of gsymbol.
1814 Otherwise, the new symbol is equipped with the attributes of the
1815 reference. The corresponding code that is called in creating
1816 global entities is parse.c.
1818 In addition, for all but -std=legacy, the gsymbols are used to
1819 check the interfaces of external procedures from the same file.
1820 The namespace of the gsymbol is resolved and then, once this is
1821 done the interface is checked. */
1825 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1827 if (!gsym_ns->proc_name->attr.recursive)
1830 if (sym->ns == gsym_ns)
1833 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1840 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1842 if (gsym_ns->entries)
1844 gfc_entry_list *entry = gsym_ns->entries;
1846 for (; entry; entry = entry->next)
1848 if (strcmp (sym->name, entry->sym->name) == 0)
1850 if (strcmp (gsym_ns->proc_name->name,
1851 sym->ns->proc_name->name) == 0)
1855 && strcmp (gsym_ns->proc_name->name,
1856 sym->ns->parent->proc_name->name) == 0)
1865 resolve_global_procedure (gfc_symbol *sym, locus *where,
1866 gfc_actual_arglist **actual, int sub)
1870 enum gfc_symbol_type type;
1872 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1874 gsym = gfc_get_gsymbol (sym->name);
1876 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1877 gfc_global_used (gsym, where);
1879 if (gfc_option.flag_whole_file
1880 && (sym->attr.if_source == IFSRC_UNKNOWN
1881 || sym->attr.if_source == IFSRC_IFBODY)
1882 && gsym->type != GSYM_UNKNOWN
1884 && gsym->ns->resolved != -1
1885 && gsym->ns->proc_name
1886 && not_in_recursive (sym, gsym->ns)
1887 && not_entry_self_reference (sym, gsym->ns))
1889 gfc_symbol *def_sym;
1891 /* Resolve the gsymbol namespace if needed. */
1892 if (!gsym->ns->resolved)
1894 gfc_dt_list *old_dt_list;
1896 /* Stash away derived types so that the backend_decls do not
1898 old_dt_list = gfc_derived_types;
1899 gfc_derived_types = NULL;
1901 gfc_resolve (gsym->ns);
1903 /* Store the new derived types with the global namespace. */
1904 if (gfc_derived_types)
1905 gsym->ns->derived_types = gfc_derived_types;
1907 /* Restore the derived types of this namespace. */
1908 gfc_derived_types = old_dt_list;
1911 /* Make sure that translation for the gsymbol occurs before
1912 the procedure currently being resolved. */
1913 ns = gfc_global_ns_list;
1914 for (; ns && ns != gsym->ns; ns = ns->sibling)
1916 if (ns->sibling == gsym->ns)
1918 ns->sibling = gsym->ns->sibling;
1919 gsym->ns->sibling = gfc_global_ns_list;
1920 gfc_global_ns_list = gsym->ns;
1925 def_sym = gsym->ns->proc_name;
1926 if (def_sym->attr.entry_master)
1928 gfc_entry_list *entry;
1929 for (entry = gsym->ns->entries; entry; entry = entry->next)
1930 if (strcmp (entry->sym->name, sym->name) == 0)
1932 def_sym = entry->sym;
1937 /* Differences in constant character lengths. */
1938 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
1940 long int l1 = 0, l2 = 0;
1941 gfc_charlen *cl1 = sym->ts.u.cl;
1942 gfc_charlen *cl2 = def_sym->ts.u.cl;
1945 && cl1->length != NULL
1946 && cl1->length->expr_type == EXPR_CONSTANT)
1947 l1 = mpz_get_si (cl1->length->value.integer);
1950 && cl2->length != NULL
1951 && cl2->length->expr_type == EXPR_CONSTANT)
1952 l2 = mpz_get_si (cl2->length->value.integer);
1954 if (l1 && l2 && l1 != l2)
1955 gfc_error ("Character length mismatch in return type of "
1956 "function '%s' at %L (%ld/%ld)", sym->name,
1957 &sym->declared_at, l1, l2);
1960 /* Type mismatch of function return type and expected type. */
1961 if (sym->attr.function
1962 && !gfc_compare_types (&sym->ts, &def_sym->ts))
1963 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
1964 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
1965 gfc_typename (&def_sym->ts));
1967 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
1969 gfc_formal_arglist *arg = def_sym->formal;
1970 for ( ; arg; arg = arg->next)
1973 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
1974 else if (arg->sym->attr.allocatable
1975 || arg->sym->attr.asynchronous
1976 || arg->sym->attr.optional
1977 || arg->sym->attr.pointer
1978 || arg->sym->attr.target
1979 || arg->sym->attr.value
1980 || arg->sym->attr.volatile_)
1982 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
1983 "has an attribute that requires an explicit "
1984 "interface for this procedure", arg->sym->name,
1985 sym->name, &sym->declared_at);
1988 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
1989 else if (arg->sym && arg->sym->as
1990 && arg->sym->as->type == AS_ASSUMED_SHAPE)
1992 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
1993 "argument '%s' must have an explicit interface",
1994 sym->name, &sym->declared_at, arg->sym->name);
1997 /* F2008, 12.4.2.2 (2c) */
1998 else if (arg->sym->attr.codimension)
2000 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2001 "'%s' must have an explicit interface",
2002 sym->name, &sym->declared_at, arg->sym->name);
2005 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2006 else if (false) /* TODO: is a parametrized derived type */
2008 gfc_error ("Procedure '%s' at %L with parametrized derived "
2009 "type argument '%s' must have an explicit "
2010 "interface", sym->name, &sym->declared_at,
2014 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2015 else if (arg->sym->ts.type == BT_CLASS)
2017 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2018 "argument '%s' must have an explicit interface",
2019 sym->name, &sym->declared_at, arg->sym->name);
2024 if (def_sym->attr.function)
2026 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2027 if (def_sym->as && def_sym->as->rank
2028 && (!sym->as || sym->as->rank != def_sym->as->rank))
2029 gfc_error ("The reference to function '%s' at %L either needs an "
2030 "explicit INTERFACE or the rank is incorrect", sym->name,
2033 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2034 if ((def_sym->result->attr.pointer
2035 || def_sym->result->attr.allocatable)
2036 && (sym->attr.if_source != IFSRC_IFBODY
2037 || def_sym->result->attr.pointer
2038 != sym->result->attr.pointer
2039 || def_sym->result->attr.allocatable
2040 != sym->result->attr.allocatable))
2041 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2042 "result must have an explicit interface", sym->name,
2045 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2046 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2047 && def_sym->ts.u.cl->length != NULL)
2049 gfc_charlen *cl = sym->ts.u.cl;
2051 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2052 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2054 gfc_error ("Nonconstant character-length function '%s' at %L "
2055 "must have an explicit interface", sym->name,
2061 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2062 if (def_sym->attr.elemental && !sym->attr.elemental)
2064 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2065 "interface", sym->name, &sym->declared_at);
2068 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2069 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2071 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2072 "an explicit interface", sym->name, &sym->declared_at);
2075 if (gfc_option.flag_whole_file == 1
2076 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2077 && !(gfc_option.warn_std & GFC_STD_GNU)))
2078 gfc_errors_to_warnings (1);
2080 if (sym->attr.if_source != IFSRC_IFBODY)
2081 gfc_procedure_use (def_sym, actual, where);
2083 gfc_errors_to_warnings (0);
2086 if (gsym->type == GSYM_UNKNOWN)
2089 gsym->where = *where;
2096 /************* Function resolution *************/
2098 /* Resolve a function call known to be generic.
2099 Section 14.1.2.4.1. */
2102 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2106 if (sym->attr.generic)
2108 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2111 expr->value.function.name = s->name;
2112 expr->value.function.esym = s;
2114 if (s->ts.type != BT_UNKNOWN)
2116 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2117 expr->ts = s->result->ts;
2120 expr->rank = s->as->rank;
2121 else if (s->result != NULL && s->result->as != NULL)
2122 expr->rank = s->result->as->rank;
2124 gfc_set_sym_referenced (expr->value.function.esym);
2129 /* TODO: Need to search for elemental references in generic
2133 if (sym->attr.intrinsic)
2134 return gfc_intrinsic_func_interface (expr, 0);
2141 resolve_generic_f (gfc_expr *expr)
2146 sym = expr->symtree->n.sym;
2150 m = resolve_generic_f0 (expr, sym);
2153 else if (m == MATCH_ERROR)
2157 if (sym->ns->parent == NULL)
2159 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2163 if (!generic_sym (sym))
2167 /* Last ditch attempt. See if the reference is to an intrinsic
2168 that possesses a matching interface. 14.1.2.4 */
2169 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2171 gfc_error ("There is no specific function for the generic '%s' at %L",
2172 expr->symtree->n.sym->name, &expr->where);
2176 m = gfc_intrinsic_func_interface (expr, 0);
2180 gfc_error ("Generic function '%s' at %L is not consistent with a "
2181 "specific intrinsic interface", expr->symtree->n.sym->name,
2188 /* Resolve a function call known to be specific. */
2191 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2195 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2197 if (sym->attr.dummy)
2199 sym->attr.proc = PROC_DUMMY;
2203 sym->attr.proc = PROC_EXTERNAL;
2207 if (sym->attr.proc == PROC_MODULE
2208 || sym->attr.proc == PROC_ST_FUNCTION
2209 || sym->attr.proc == PROC_INTERNAL)
2212 if (sym->attr.intrinsic)
2214 m = gfc_intrinsic_func_interface (expr, 1);
2218 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2219 "with an intrinsic", sym->name, &expr->where);
2227 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2230 expr->ts = sym->result->ts;
2233 expr->value.function.name = sym->name;
2234 expr->value.function.esym = sym;
2235 if (sym->as != NULL)
2236 expr->rank = sym->as->rank;
2243 resolve_specific_f (gfc_expr *expr)
2248 sym = expr->symtree->n.sym;
2252 m = resolve_specific_f0 (sym, expr);
2255 if (m == MATCH_ERROR)
2258 if (sym->ns->parent == NULL)
2261 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2267 gfc_error ("Unable to resolve the specific function '%s' at %L",
2268 expr->symtree->n.sym->name, &expr->where);
2274 /* Resolve a procedure call not known to be generic nor specific. */
2277 resolve_unknown_f (gfc_expr *expr)
2282 sym = expr->symtree->n.sym;
2284 if (sym->attr.dummy)
2286 sym->attr.proc = PROC_DUMMY;
2287 expr->value.function.name = sym->name;
2291 /* See if we have an intrinsic function reference. */
2293 if (gfc_is_intrinsic (sym, 0, expr->where))
2295 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2300 /* The reference is to an external name. */
2302 sym->attr.proc = PROC_EXTERNAL;
2303 expr->value.function.name = sym->name;
2304 expr->value.function.esym = expr->symtree->n.sym;
2306 if (sym->as != NULL)
2307 expr->rank = sym->as->rank;
2309 /* Type of the expression is either the type of the symbol or the
2310 default type of the symbol. */
2313 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2315 if (sym->ts.type != BT_UNKNOWN)
2319 ts = gfc_get_default_type (sym->name, sym->ns);
2321 if (ts->type == BT_UNKNOWN)
2323 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2324 sym->name, &expr->where);
2335 /* Return true, if the symbol is an external procedure. */
2337 is_external_proc (gfc_symbol *sym)
2339 if (!sym->attr.dummy && !sym->attr.contained
2340 && !(sym->attr.intrinsic
2341 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2342 && sym->attr.proc != PROC_ST_FUNCTION
2343 && !sym->attr.proc_pointer
2344 && !sym->attr.use_assoc
2352 /* Figure out if a function reference is pure or not. Also set the name
2353 of the function for a potential error message. Return nonzero if the
2354 function is PURE, zero if not. */
2356 pure_stmt_function (gfc_expr *, gfc_symbol *);
2359 pure_function (gfc_expr *e, const char **name)
2365 if (e->symtree != NULL
2366 && e->symtree->n.sym != NULL
2367 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2368 return pure_stmt_function (e, e->symtree->n.sym);
2370 if (e->value.function.esym)
2372 pure = gfc_pure (e->value.function.esym);
2373 *name = e->value.function.esym->name;
2375 else if (e->value.function.isym)
2377 pure = e->value.function.isym->pure
2378 || e->value.function.isym->elemental;
2379 *name = e->value.function.isym->name;
2383 /* Implicit functions are not pure. */
2385 *name = e->value.function.name;
2393 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2394 int *f ATTRIBUTE_UNUSED)
2398 /* Don't bother recursing into other statement functions
2399 since they will be checked individually for purity. */
2400 if (e->expr_type != EXPR_FUNCTION
2402 || e->symtree->n.sym == sym
2403 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2406 return pure_function (e, &name) ? false : true;
2411 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2413 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2418 is_scalar_expr_ptr (gfc_expr *expr)
2420 gfc_try retval = SUCCESS;
2425 /* See if we have a gfc_ref, which means we have a substring, array
2426 reference, or a component. */
2427 if (expr->ref != NULL)
2430 while (ref->next != NULL)
2436 if (ref->u.ss.length != NULL
2437 && ref->u.ss.length->length != NULL
2439 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2441 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2443 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2444 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2445 if (end - start + 1 != 1)
2452 if (ref->u.ar.type == AR_ELEMENT)
2454 else if (ref->u.ar.type == AR_FULL)
2456 /* The user can give a full array if the array is of size 1. */
2457 if (ref->u.ar.as != NULL
2458 && ref->u.ar.as->rank == 1
2459 && ref->u.ar.as->type == AS_EXPLICIT
2460 && ref->u.ar.as->lower[0] != NULL
2461 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2462 && ref->u.ar.as->upper[0] != NULL
2463 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2465 /* If we have a character string, we need to check if
2466 its length is one. */
2467 if (expr->ts.type == BT_CHARACTER)
2469 if (expr->ts.u.cl == NULL
2470 || expr->ts.u.cl->length == NULL
2471 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2477 /* We have constant lower and upper bounds. If the
2478 difference between is 1, it can be considered a
2480 start = (int) mpz_get_si
2481 (ref->u.ar.as->lower[0]->value.integer);
2482 end = (int) mpz_get_si
2483 (ref->u.ar.as->upper[0]->value.integer);
2484 if (end - start + 1 != 1)
2499 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2501 /* Character string. Make sure it's of length 1. */
2502 if (expr->ts.u.cl == NULL
2503 || expr->ts.u.cl->length == NULL
2504 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2507 else if (expr->rank != 0)
2514 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2515 and, in the case of c_associated, set the binding label based on
2519 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2520 gfc_symbol **new_sym)
2522 char name[GFC_MAX_SYMBOL_LEN + 1];
2523 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2524 int optional_arg = 0;
2525 gfc_try retval = SUCCESS;
2526 gfc_symbol *args_sym;
2527 gfc_typespec *arg_ts;
2528 symbol_attribute arg_attr;
2530 if (args->expr->expr_type == EXPR_CONSTANT
2531 || args->expr->expr_type == EXPR_OP
2532 || args->expr->expr_type == EXPR_NULL)
2534 gfc_error ("Argument to '%s' at %L is not a variable",
2535 sym->name, &(args->expr->where));
2539 args_sym = args->expr->symtree->n.sym;
2541 /* The typespec for the actual arg should be that stored in the expr
2542 and not necessarily that of the expr symbol (args_sym), because
2543 the actual expression could be a part-ref of the expr symbol. */
2544 arg_ts = &(args->expr->ts);
2545 arg_attr = gfc_expr_attr (args->expr);
2547 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2549 /* If the user gave two args then they are providing something for
2550 the optional arg (the second cptr). Therefore, set the name and
2551 binding label to the c_associated for two cptrs. Otherwise,
2552 set c_associated to expect one cptr. */
2556 sprintf (name, "%s_2", sym->name);
2557 sprintf (binding_label, "%s_2", sym->binding_label);
2563 sprintf (name, "%s_1", sym->name);
2564 sprintf (binding_label, "%s_1", sym->binding_label);
2568 /* Get a new symbol for the version of c_associated that
2570 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2572 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2573 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2575 sprintf (name, "%s", sym->name);
2576 sprintf (binding_label, "%s", sym->binding_label);
2578 /* Error check the call. */
2579 if (args->next != NULL)
2581 gfc_error_now ("More actual than formal arguments in '%s' "
2582 "call at %L", name, &(args->expr->where));
2585 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2587 /* Make sure we have either the target or pointer attribute. */
2588 if (!arg_attr.target && !arg_attr.pointer)
2590 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2591 "a TARGET or an associated pointer",
2593 sym->name, &(args->expr->where));
2597 /* See if we have interoperable type and type param. */
2598 if (verify_c_interop (arg_ts) == SUCCESS
2599 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2601 if (args_sym->attr.target == 1)
2603 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2604 has the target attribute and is interoperable. */
2605 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2606 allocatable variable that has the TARGET attribute and
2607 is not an array of zero size. */
2608 if (args_sym->attr.allocatable == 1)
2610 if (args_sym->attr.dimension != 0
2611 && (args_sym->as && args_sym->as->rank == 0))
2613 gfc_error_now ("Allocatable variable '%s' used as a "
2614 "parameter to '%s' at %L must not be "
2615 "an array of zero size",
2616 args_sym->name, sym->name,
2617 &(args->expr->where));
2623 /* A non-allocatable target variable with C
2624 interoperable type and type parameters must be
2626 if (args_sym && args_sym->attr.dimension)
2628 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2630 gfc_error ("Assumed-shape array '%s' at %L "
2631 "cannot be an argument to the "
2632 "procedure '%s' because "
2633 "it is not C interoperable",
2635 &(args->expr->where), sym->name);
2638 else if (args_sym->as->type == AS_DEFERRED)
2640 gfc_error ("Deferred-shape array '%s' at %L "
2641 "cannot be an argument to the "
2642 "procedure '%s' because "
2643 "it is not C interoperable",
2645 &(args->expr->where), sym->name);
2650 /* Make sure it's not a character string. Arrays of
2651 any type should be ok if the variable is of a C
2652 interoperable type. */
2653 if (arg_ts->type == BT_CHARACTER)
2654 if (arg_ts->u.cl != NULL
2655 && (arg_ts->u.cl->length == NULL
2656 || arg_ts->u.cl->length->expr_type
2659 (arg_ts->u.cl->length->value.integer, 1)
2661 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2663 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2664 "at %L must have a length of 1",
2665 args_sym->name, sym->name,
2666 &(args->expr->where));
2671 else if (arg_attr.pointer
2672 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2674 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2676 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2677 "associated scalar POINTER", args_sym->name,
2678 sym->name, &(args->expr->where));
2684 /* The parameter is not required to be C interoperable. If it
2685 is not C interoperable, it must be a nonpolymorphic scalar
2686 with no length type parameters. It still must have either
2687 the pointer or target attribute, and it can be
2688 allocatable (but must be allocated when c_loc is called). */
2689 if (args->expr->rank != 0
2690 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2692 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2693 "scalar", args_sym->name, sym->name,
2694 &(args->expr->where));
2697 else if (arg_ts->type == BT_CHARACTER
2698 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2700 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2701 "%L must have a length of 1",
2702 args_sym->name, sym->name,
2703 &(args->expr->where));
2706 else if (arg_ts->type == BT_CLASS)
2708 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2709 "polymorphic", args_sym->name, sym->name,
2710 &(args->expr->where));
2715 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2717 if (args_sym->attr.flavor != FL_PROCEDURE)
2719 /* TODO: Update this error message to allow for procedure
2720 pointers once they are implemented. */
2721 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2723 args_sym->name, sym->name,
2724 &(args->expr->where));
2727 else if (args_sym->attr.is_bind_c != 1)
2729 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2731 args_sym->name, sym->name,
2732 &(args->expr->where));
2737 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2742 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2743 "iso_c_binding function: '%s'!\n", sym->name);
2750 /* Resolve a function call, which means resolving the arguments, then figuring
2751 out which entity the name refers to. */
2752 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2753 to INTENT(OUT) or INTENT(INOUT). */
2756 resolve_function (gfc_expr *expr)
2758 gfc_actual_arglist *arg;
2763 procedure_type p = PROC_INTRINSIC;
2764 bool no_formal_args;
2768 sym = expr->symtree->n.sym;
2770 /* If this is a procedure pointer component, it has already been resolved. */
2771 if (gfc_is_proc_ptr_comp (expr, NULL))
2774 if (sym && sym->attr.intrinsic
2775 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2778 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2780 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2784 /* If this ia a deferred TBP with an abstract interface (which may
2785 of course be referenced), expr->value.function.esym will be set. */
2786 if (sym && sym->attr.abstract && !expr->value.function.esym)
2788 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2789 sym->name, &expr->where);
2793 /* Switch off assumed size checking and do this again for certain kinds
2794 of procedure, once the procedure itself is resolved. */
2795 need_full_assumed_size++;
2797 if (expr->symtree && expr->symtree->n.sym)
2798 p = expr->symtree->n.sym->attr.proc;
2800 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2801 inquiry_argument = true;
2802 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2804 if (resolve_actual_arglist (expr->value.function.actual,
2805 p, no_formal_args) == FAILURE)
2807 inquiry_argument = false;
2811 inquiry_argument = false;
2813 /* Need to setup the call to the correct c_associated, depending on
2814 the number of cptrs to user gives to compare. */
2815 if (sym && sym->attr.is_iso_c == 1)
2817 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2821 /* Get the symtree for the new symbol (resolved func).
2822 the old one will be freed later, when it's no longer used. */
2823 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2826 /* Resume assumed_size checking. */
2827 need_full_assumed_size--;
2829 /* If the procedure is external, check for usage. */
2830 if (sym && is_external_proc (sym))
2831 resolve_global_procedure (sym, &expr->where,
2832 &expr->value.function.actual, 0);
2834 if (sym && sym->ts.type == BT_CHARACTER
2836 && sym->ts.u.cl->length == NULL
2838 && expr->value.function.esym == NULL
2839 && !sym->attr.contained)
2841 /* Internal procedures are taken care of in resolve_contained_fntype. */
2842 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2843 "be used at %L since it is not a dummy argument",
2844 sym->name, &expr->where);
2848 /* See if function is already resolved. */
2850 if (expr->value.function.name != NULL)
2852 if (expr->ts.type == BT_UNKNOWN)
2858 /* Apply the rules of section 14.1.2. */
2860 switch (procedure_kind (sym))
2863 t = resolve_generic_f (expr);
2866 case PTYPE_SPECIFIC:
2867 t = resolve_specific_f (expr);
2871 t = resolve_unknown_f (expr);
2875 gfc_internal_error ("resolve_function(): bad function type");
2879 /* If the expression is still a function (it might have simplified),
2880 then we check to see if we are calling an elemental function. */
2882 if (expr->expr_type != EXPR_FUNCTION)
2885 temp = need_full_assumed_size;
2886 need_full_assumed_size = 0;
2888 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2891 if (omp_workshare_flag
2892 && expr->value.function.esym
2893 && ! gfc_elemental (expr->value.function.esym))
2895 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2896 "in WORKSHARE construct", expr->value.function.esym->name,
2901 #define GENERIC_ID expr->value.function.isym->id
2902 else if (expr->value.function.actual != NULL
2903 && expr->value.function.isym != NULL
2904 && GENERIC_ID != GFC_ISYM_LBOUND
2905 && GENERIC_ID != GFC_ISYM_LEN
2906 && GENERIC_ID != GFC_ISYM_LOC
2907 && GENERIC_ID != GFC_ISYM_PRESENT)
2909 /* Array intrinsics must also have the last upper bound of an
2910 assumed size array argument. UBOUND and SIZE have to be
2911 excluded from the check if the second argument is anything
2914 for (arg = expr->value.function.actual; arg; arg = arg->next)
2916 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2917 && arg->next != NULL && arg->next->expr)
2919 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2922 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2925 if ((int)mpz_get_si (arg->next->expr->value.integer)
2930 if (arg->expr != NULL
2931 && arg->expr->rank > 0
2932 && resolve_assumed_size_actual (arg->expr))
2938 need_full_assumed_size = temp;
2941 if (!pure_function (expr, &name) && name)
2945 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2946 "FORALL %s", name, &expr->where,
2947 forall_flag == 2 ? "mask" : "block");
2950 else if (gfc_pure (NULL))
2952 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2953 "procedure within a PURE procedure", name, &expr->where);
2958 /* Functions without the RECURSIVE attribution are not allowed to
2959 * call themselves. */
2960 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2963 esym = expr->value.function.esym;
2965 if (is_illegal_recursion (esym, gfc_current_ns))
2967 if (esym->attr.entry && esym->ns->entries)
2968 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2969 " function '%s' is not RECURSIVE",
2970 esym->name, &expr->where, esym->ns->entries->sym->name);
2972 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2973 " is not RECURSIVE", esym->name, &expr->where);
2979 /* Character lengths of use associated functions may contains references to
2980 symbols not referenced from the current program unit otherwise. Make sure
2981 those symbols are marked as referenced. */
2983 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2984 && expr->value.function.esym->attr.use_assoc)
2986 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2990 && !((expr->value.function.esym
2991 && expr->value.function.esym->attr.elemental)
2993 (expr->value.function.isym
2994 && expr->value.function.isym->elemental)))
2995 find_noncopying_intrinsics (expr->value.function.esym,
2996 expr->value.function.actual);
2998 /* Make sure that the expression has a typespec that works. */
2999 if (expr->ts.type == BT_UNKNOWN)
3001 if (expr->symtree->n.sym->result
3002 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3003 && !expr->symtree->n.sym->result->attr.proc_pointer)
3004 expr->ts = expr->symtree->n.sym->result->ts;
3011 /************* Subroutine resolution *************/
3014 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3020 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3021 sym->name, &c->loc);
3022 else if (gfc_pure (NULL))
3023 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3029 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3033 if (sym->attr.generic)
3035 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3038 c->resolved_sym = s;
3039 pure_subroutine (c, s);
3043 /* TODO: Need to search for elemental references in generic interface. */
3046 if (sym->attr.intrinsic)
3047 return gfc_intrinsic_sub_interface (c, 0);
3054 resolve_generic_s (gfc_code *c)
3059 sym = c->symtree->n.sym;
3063 m = resolve_generic_s0 (c, sym);
3066 else if (m == MATCH_ERROR)
3070 if (sym->ns->parent == NULL)
3072 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3076 if (!generic_sym (sym))
3080 /* Last ditch attempt. See if the reference is to an intrinsic
3081 that possesses a matching interface. 14.1.2.4 */
3082 sym = c->symtree->n.sym;
3084 if (!gfc_is_intrinsic (sym, 1, c->loc))
3086 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3087 sym->name, &c->loc);
3091 m = gfc_intrinsic_sub_interface (c, 0);
3095 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3096 "intrinsic subroutine interface", sym->name, &c->loc);
3102 /* Set the name and binding label of the subroutine symbol in the call
3103 expression represented by 'c' to include the type and kind of the
3104 second parameter. This function is for resolving the appropriate
3105 version of c_f_pointer() and c_f_procpointer(). For example, a
3106 call to c_f_pointer() for a default integer pointer could have a
3107 name of c_f_pointer_i4. If no second arg exists, which is an error
3108 for these two functions, it defaults to the generic symbol's name
3109 and binding label. */
3112 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3113 char *name, char *binding_label)
3115 gfc_expr *arg = NULL;
3119 /* The second arg of c_f_pointer and c_f_procpointer determines
3120 the type and kind for the procedure name. */
3121 arg = c->ext.actual->next->expr;
3125 /* Set up the name to have the given symbol's name,
3126 plus the type and kind. */
3127 /* a derived type is marked with the type letter 'u' */
3128 if (arg->ts.type == BT_DERIVED)
3131 kind = 0; /* set the kind as 0 for now */
3135 type = gfc_type_letter (arg->ts.type);
3136 kind = arg->ts.kind;
3139 if (arg->ts.type == BT_CHARACTER)
3140 /* Kind info for character strings not needed. */
3143 sprintf (name, "%s_%c%d", sym->name, type, kind);
3144 /* Set up the binding label as the given symbol's label plus
3145 the type and kind. */
3146 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3150 /* If the second arg is missing, set the name and label as
3151 was, cause it should at least be found, and the missing
3152 arg error will be caught by compare_parameters(). */
3153 sprintf (name, "%s", sym->name);
3154 sprintf (binding_label, "%s", sym->binding_label);
3161 /* Resolve a generic version of the iso_c_binding procedure given
3162 (sym) to the specific one based on the type and kind of the
3163 argument(s). Currently, this function resolves c_f_pointer() and
3164 c_f_procpointer based on the type and kind of the second argument
3165 (FPTR). Other iso_c_binding procedures aren't specially handled.
3166 Upon successfully exiting, c->resolved_sym will hold the resolved
3167 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3171 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3173 gfc_symbol *new_sym;
3174 /* this is fine, since we know the names won't use the max */
3175 char name[GFC_MAX_SYMBOL_LEN + 1];
3176 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3177 /* default to success; will override if find error */
3178 match m = MATCH_YES;
3180 /* Make sure the actual arguments are in the necessary order (based on the
3181 formal args) before resolving. */
3182 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3184 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3185 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3187 set_name_and_label (c, sym, name, binding_label);
3189 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3191 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3193 /* Make sure we got a third arg if the second arg has non-zero
3194 rank. We must also check that the type and rank are
3195 correct since we short-circuit this check in
3196 gfc_procedure_use() (called above to sort actual args). */
3197 if (c->ext.actual->next->expr->rank != 0)
3199 if(c->ext.actual->next->next == NULL
3200 || c->ext.actual->next->next->expr == NULL)
3203 gfc_error ("Missing SHAPE parameter for call to %s "
3204 "at %L", sym->name, &(c->loc));
3206 else if (c->ext.actual->next->next->expr->ts.type
3208 || c->ext.actual->next->next->expr->rank != 1)
3211 gfc_error ("SHAPE parameter for call to %s at %L must "
3212 "be a rank 1 INTEGER array", sym->name,
3219 if (m != MATCH_ERROR)
3221 /* the 1 means to add the optional arg to formal list */
3222 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3224 /* for error reporting, say it's declared where the original was */
3225 new_sym->declared_at = sym->declared_at;
3230 /* no differences for c_loc or c_funloc */
3234 /* set the resolved symbol */
3235 if (m != MATCH_ERROR)
3236 c->resolved_sym = new_sym;
3238 c->resolved_sym = sym;
3244 /* Resolve a subroutine call known to be specific. */
3247 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3251 if(sym->attr.is_iso_c)
3253 m = gfc_iso_c_sub_interface (c,sym);
3257 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3259 if (sym->attr.dummy)
3261 sym->attr.proc = PROC_DUMMY;
3265 sym->attr.proc = PROC_EXTERNAL;
3269 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3272 if (sym->attr.intrinsic)
3274 m = gfc_intrinsic_sub_interface (c, 1);
3278 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3279 "with an intrinsic", sym->name, &c->loc);
3287 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3289 c->resolved_sym = sym;
3290 pure_subroutine (c, sym);
3297 resolve_specific_s (gfc_code *c)
3302 sym = c->symtree->n.sym;
3306 m = resolve_specific_s0 (c, sym);
3309 if (m == MATCH_ERROR)
3312 if (sym->ns->parent == NULL)
3315 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3321 sym = c->symtree->n.sym;
3322 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3323 sym->name, &c->loc);
3329 /* Resolve a subroutine call not known to be generic nor specific. */
3332 resolve_unknown_s (gfc_code *c)
3336 sym = c->symtree->n.sym;
3338 if (sym->attr.dummy)
3340 sym->attr.proc = PROC_DUMMY;
3344 /* See if we have an intrinsic function reference. */
3346 if (gfc_is_intrinsic (sym, 1, c->loc))
3348 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)