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.allocatable)
283 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
284 "have the ALLOCATABLE attribute", sym->name,
289 if (sym->attr.pointer)
291 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
292 "have the POINTER attribute", sym->name,
297 if (sym->attr.flavor == FL_PROCEDURE)
299 gfc_error ("Dummy procedure '%s' not allowed in elemental "
300 "procedure '%s' at %L", sym->name, proc->name,
305 if (sym->attr.intent == INTENT_UNKNOWN)
307 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
308 "have its INTENT specified", sym->name, proc->name,
314 /* Each dummy shall be specified to be scalar. */
315 if (proc->attr.proc == PROC_ST_FUNCTION)
319 gfc_error ("Argument '%s' of statement function at %L must "
320 "be scalar", sym->name, &sym->declared_at);
324 if (sym->ts.type == BT_CHARACTER)
326 gfc_charlen *cl = sym->ts.u.cl;
327 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
329 gfc_error ("Character-valued argument '%s' of statement "
330 "function at %L must have constant length",
331 sym->name, &sym->declared_at);
341 /* Work function called when searching for symbols that have argument lists
342 associated with them. */
345 find_arglists (gfc_symbol *sym)
347 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
350 resolve_formal_arglist (sym);
354 /* Given a namespace, resolve all formal argument lists within the namespace.
358 resolve_formal_arglists (gfc_namespace *ns)
363 gfc_traverse_ns (ns, find_arglists);
368 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
372 /* If this namespace is not a function or an entry master function,
374 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
375 || sym->attr.entry_master)
378 /* Try to find out of what the return type is. */
379 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
381 t = gfc_set_default_type (sym->result, 0, ns);
383 if (t == FAILURE && !sym->result->attr.untyped)
385 if (sym->result == sym)
386 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
387 sym->name, &sym->declared_at);
388 else if (!sym->result->attr.proc_pointer)
389 gfc_error ("Result '%s' of contained function '%s' at %L has "
390 "no IMPLICIT type", sym->result->name, sym->name,
391 &sym->result->declared_at);
392 sym->result->attr.untyped = 1;
396 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
397 type, lists the only ways a character length value of * can be used:
398 dummy arguments of procedures, named constants, and function results
399 in external functions. Internal function results and results of module
400 procedures are not on this list, ergo, not permitted. */
402 if (sym->result->ts.type == BT_CHARACTER)
404 gfc_charlen *cl = sym->result->ts.u.cl;
405 if (!cl || !cl->length)
407 /* See if this is a module-procedure and adapt error message
410 gcc_assert (ns->parent && ns->parent->proc_name);
411 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
413 gfc_error ("Character-valued %s '%s' at %L must not be"
415 module_proc ? _("module procedure")
416 : _("internal function"),
417 sym->name, &sym->declared_at);
423 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
424 introduce duplicates. */
427 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
429 gfc_formal_arglist *f, *new_arglist;
432 for (; new_args != NULL; new_args = new_args->next)
434 new_sym = new_args->sym;
435 /* See if this arg is already in the formal argument list. */
436 for (f = proc->formal; f; f = f->next)
438 if (new_sym == f->sym)
445 /* Add a new argument. Argument order is not important. */
446 new_arglist = gfc_get_formal_arglist ();
447 new_arglist->sym = new_sym;
448 new_arglist->next = proc->formal;
449 proc->formal = new_arglist;
454 /* Flag the arguments that are not present in all entries. */
457 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
459 gfc_formal_arglist *f, *head;
462 for (f = proc->formal; f; f = f->next)
467 for (new_args = head; new_args; new_args = new_args->next)
469 if (new_args->sym == f->sym)
476 f->sym->attr.not_always_present = 1;
481 /* Resolve alternate entry points. If a symbol has multiple entry points we
482 create a new master symbol for the main routine, and turn the existing
483 symbol into an entry point. */
486 resolve_entries (gfc_namespace *ns)
488 gfc_namespace *old_ns;
492 char name[GFC_MAX_SYMBOL_LEN + 1];
493 static int master_count = 0;
495 if (ns->proc_name == NULL)
498 /* No need to do anything if this procedure doesn't have alternate entry
503 /* We may already have resolved alternate entry points. */
504 if (ns->proc_name->attr.entry_master)
507 /* If this isn't a procedure something has gone horribly wrong. */
508 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
510 /* Remember the current namespace. */
511 old_ns = gfc_current_ns;
515 /* Add the main entry point to the list of entry points. */
516 el = gfc_get_entry_list ();
517 el->sym = ns->proc_name;
519 el->next = ns->entries;
521 ns->proc_name->attr.entry = 1;
523 /* If it is a module function, it needs to be in the right namespace
524 so that gfc_get_fake_result_decl can gather up the results. The
525 need for this arose in get_proc_name, where these beasts were
526 left in their own namespace, to keep prior references linked to
527 the entry declaration.*/
528 if (ns->proc_name->attr.function
529 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
532 /* Do the same for entries where the master is not a module
533 procedure. These are retained in the module namespace because
534 of the module procedure declaration. */
535 for (el = el->next; el; el = el->next)
536 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
537 && el->sym->attr.mod_proc)
541 /* Add an entry statement for it. */
548 /* Create a new symbol for the master function. */
549 /* Give the internal function a unique name (within this file).
550 Also include the function name so the user has some hope of figuring
551 out what is going on. */
552 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
553 master_count++, ns->proc_name->name);
554 gfc_get_ha_symbol (name, &proc);
555 gcc_assert (proc != NULL);
557 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
558 if (ns->proc_name->attr.subroutine)
559 gfc_add_subroutine (&proc->attr, proc->name, NULL);
563 gfc_typespec *ts, *fts;
564 gfc_array_spec *as, *fas;
565 gfc_add_function (&proc->attr, proc->name, NULL);
567 fas = ns->entries->sym->as;
568 fas = fas ? fas : ns->entries->sym->result->as;
569 fts = &ns->entries->sym->result->ts;
570 if (fts->type == BT_UNKNOWN)
571 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
572 for (el = ns->entries->next; el; el = el->next)
574 ts = &el->sym->result->ts;
576 as = as ? as : el->sym->result->as;
577 if (ts->type == BT_UNKNOWN)
578 ts = gfc_get_default_type (el->sym->result->name, NULL);
580 if (! gfc_compare_types (ts, fts)
581 || (el->sym->result->attr.dimension
582 != ns->entries->sym->result->attr.dimension)
583 || (el->sym->result->attr.pointer
584 != ns->entries->sym->result->attr.pointer))
586 else if (as && fas && ns->entries->sym->result != el->sym->result
587 && gfc_compare_array_spec (as, fas) == 0)
588 gfc_error ("Function %s at %L has entries with mismatched "
589 "array specifications", ns->entries->sym->name,
590 &ns->entries->sym->declared_at);
591 /* The characteristics need to match and thus both need to have
592 the same string length, i.e. both len=*, or both len=4.
593 Having both len=<variable> is also possible, but difficult to
594 check at compile time. */
595 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
596 && (((ts->u.cl->length && !fts->u.cl->length)
597 ||(!ts->u.cl->length && fts->u.cl->length))
599 && ts->u.cl->length->expr_type
600 != fts->u.cl->length->expr_type)
602 && ts->u.cl->length->expr_type == EXPR_CONSTANT
603 && mpz_cmp (ts->u.cl->length->value.integer,
604 fts->u.cl->length->value.integer) != 0)))
605 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
606 "entries returning variables of different "
607 "string lengths", ns->entries->sym->name,
608 &ns->entries->sym->declared_at);
613 sym = ns->entries->sym->result;
614 /* All result types the same. */
616 if (sym->attr.dimension)
617 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
618 if (sym->attr.pointer)
619 gfc_add_pointer (&proc->attr, NULL);
623 /* Otherwise the result will be passed through a union by
625 proc->attr.mixed_entry_master = 1;
626 for (el = ns->entries; el; el = el->next)
628 sym = el->sym->result;
629 if (sym->attr.dimension)
631 if (el == ns->entries)
632 gfc_error ("FUNCTION result %s can't be an array in "
633 "FUNCTION %s at %L", sym->name,
634 ns->entries->sym->name, &sym->declared_at);
636 gfc_error ("ENTRY result %s can't be an array in "
637 "FUNCTION %s at %L", sym->name,
638 ns->entries->sym->name, &sym->declared_at);
640 else if (sym->attr.pointer)
642 if (el == ns->entries)
643 gfc_error ("FUNCTION result %s can't be a POINTER in "
644 "FUNCTION %s at %L", sym->name,
645 ns->entries->sym->name, &sym->declared_at);
647 gfc_error ("ENTRY result %s can't be a POINTER in "
648 "FUNCTION %s at %L", sym->name,
649 ns->entries->sym->name, &sym->declared_at);
654 if (ts->type == BT_UNKNOWN)
655 ts = gfc_get_default_type (sym->name, NULL);
659 if (ts->kind == gfc_default_integer_kind)
663 if (ts->kind == gfc_default_real_kind
664 || ts->kind == gfc_default_double_kind)
668 if (ts->kind == gfc_default_complex_kind)
672 if (ts->kind == gfc_default_logical_kind)
676 /* We will issue error elsewhere. */
684 if (el == ns->entries)
685 gfc_error ("FUNCTION result %s can't be of type %s "
686 "in FUNCTION %s at %L", sym->name,
687 gfc_typename (ts), ns->entries->sym->name,
690 gfc_error ("ENTRY result %s can't be of type %s "
691 "in FUNCTION %s at %L", sym->name,
692 gfc_typename (ts), ns->entries->sym->name,
699 proc->attr.access = ACCESS_PRIVATE;
700 proc->attr.entry_master = 1;
702 /* Merge all the entry point arguments. */
703 for (el = ns->entries; el; el = el->next)
704 merge_argument_lists (proc, el->sym->formal);
706 /* Check the master formal arguments for any that are not
707 present in all entry points. */
708 for (el = ns->entries; el; el = el->next)
709 check_argument_lists (proc, el->sym->formal);
711 /* Use the master function for the function body. */
712 ns->proc_name = proc;
714 /* Finalize the new symbols. */
715 gfc_commit_symbols ();
717 /* Restore the original namespace. */
718 gfc_current_ns = old_ns;
722 /* Resolve common variables. */
724 resolve_common_vars (gfc_symbol *sym, bool named_common)
726 gfc_symbol *csym = sym;
728 for (; csym; csym = csym->common_next)
730 if (csym->value || csym->attr.data)
732 if (!csym->ns->is_block_data)
733 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
734 "but only in BLOCK DATA initialization is "
735 "allowed", csym->name, &csym->declared_at);
736 else if (!named_common)
737 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
738 "in a blank COMMON but initialization is only "
739 "allowed in named common blocks", csym->name,
743 if (csym->ts.type != BT_DERIVED)
746 if (!(csym->ts.u.derived->attr.sequence
747 || csym->ts.u.derived->attr.is_bind_c))
748 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
749 "has neither the SEQUENCE nor the BIND(C) "
750 "attribute", csym->name, &csym->declared_at);
751 if (csym->ts.u.derived->attr.alloc_comp)
752 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
753 "has an ultimate component that is "
754 "allocatable", csym->name, &csym->declared_at);
755 if (gfc_has_default_initializer (csym->ts.u.derived))
756 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
757 "may not have default initializer", csym->name,
760 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
761 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
765 /* Resolve common blocks. */
767 resolve_common_blocks (gfc_symtree *common_root)
771 if (common_root == NULL)
774 if (common_root->left)
775 resolve_common_blocks (common_root->left);
776 if (common_root->right)
777 resolve_common_blocks (common_root->right);
779 resolve_common_vars (common_root->n.common->head, true);
781 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
785 if (sym->attr.flavor == FL_PARAMETER)
786 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
787 sym->name, &common_root->n.common->where, &sym->declared_at);
789 if (sym->attr.intrinsic)
790 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
791 sym->name, &common_root->n.common->where);
792 else if (sym->attr.result
793 || gfc_is_function_return_value (sym, gfc_current_ns))
794 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
795 "that is also a function result", sym->name,
796 &common_root->n.common->where);
797 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
798 && sym->attr.proc != PROC_ST_FUNCTION)
799 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
800 "that is also a global procedure", sym->name,
801 &common_root->n.common->where);
805 /* Resolve contained function types. Because contained functions can call one
806 another, they have to be worked out before any of the contained procedures
809 The good news is that if a function doesn't already have a type, the only
810 way it can get one is through an IMPLICIT type or a RESULT variable, because
811 by definition contained functions are contained namespace they're contained
812 in, not in a sibling or parent namespace. */
815 resolve_contained_functions (gfc_namespace *ns)
817 gfc_namespace *child;
820 resolve_formal_arglists (ns);
822 for (child = ns->contained; child; child = child->sibling)
824 /* Resolve alternate entry points first. */
825 resolve_entries (child);
827 /* Then check function return types. */
828 resolve_contained_fntype (child->proc_name, child);
829 for (el = child->entries; el; el = el->next)
830 resolve_contained_fntype (el->sym, child);
835 /* Resolve all of the elements of a structure constructor and make sure that
836 the types are correct. The 'init' flag indicates that the given
837 constructor is an initializer. */
840 resolve_structure_cons (gfc_expr *expr, int init)
842 gfc_constructor *cons;
848 cons = gfc_constructor_first (expr->value.constructor);
849 /* A constructor may have references if it is the result of substituting a
850 parameter variable. In this case we just pull out the component we
853 comp = expr->ref->u.c.sym->components;
855 comp = expr->ts.u.derived->components;
857 /* See if the user is trying to invoke a structure constructor for one of
858 the iso_c_binding derived types. */
859 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
860 && expr->ts.u.derived->ts.is_iso_c && cons
861 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
863 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
864 expr->ts.u.derived->name, &(expr->where));
868 /* Return if structure constructor is c_null_(fun)prt. */
869 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
870 && expr->ts.u.derived->ts.is_iso_c && cons
871 && cons->expr && cons->expr->expr_type == EXPR_NULL)
874 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
881 if (gfc_resolve_expr (cons->expr) == FAILURE)
887 rank = comp->as ? comp->as->rank : 0;
888 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
889 && (comp->attr.allocatable || cons->expr->rank))
891 gfc_error ("The rank of the element in the derived type "
892 "constructor at %L does not match that of the "
893 "component (%d/%d)", &cons->expr->where,
894 cons->expr->rank, rank);
898 /* If we don't have the right type, try to convert it. */
900 if (!comp->attr.proc_pointer &&
901 !gfc_compare_types (&cons->expr->ts, &comp->ts))
904 if (strcmp (comp->name, "$extends") == 0)
906 /* Can afford to be brutal with the $extends initializer.
907 The derived type can get lost because it is PRIVATE
908 but it is not usage constrained by the standard. */
909 cons->expr->ts = comp->ts;
912 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
913 gfc_error ("The element in the derived type constructor at %L, "
914 "for pointer component '%s', is %s but should be %s",
915 &cons->expr->where, comp->name,
916 gfc_basic_typename (cons->expr->ts.type),
917 gfc_basic_typename (comp->ts.type));
919 t = gfc_convert_type (cons->expr, &comp->ts, 1);
922 /* For strings, the length of the constructor should be the same as
923 the one of the structure, ensure this if the lengths are known at
924 compile time and when we are dealing with PARAMETER or structure
926 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
927 && comp->ts.u.cl->length
928 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
929 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
930 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
931 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
932 comp->ts.u.cl->length->value.integer) != 0)
934 if (cons->expr->expr_type == EXPR_VARIABLE
935 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
937 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
938 to make use of the gfc_resolve_character_array_constructor
939 machinery. The expression is later simplified away to
940 an array of string literals. */
941 gfc_expr *para = cons->expr;
942 cons->expr = gfc_get_expr ();
943 cons->expr->ts = para->ts;
944 cons->expr->where = para->where;
945 cons->expr->expr_type = EXPR_ARRAY;
946 cons->expr->rank = para->rank;
947 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
948 gfc_constructor_append_expr (&cons->expr->value.constructor,
949 para, &cons->expr->where);
951 if (cons->expr->expr_type == EXPR_ARRAY)
954 p = gfc_constructor_first (cons->expr->value.constructor);
955 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
957 gfc_charlen *cl, *cl2;
960 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
962 if (cl == cons->expr->ts.u.cl)
970 cl2->next = cl->next;
972 gfc_free_expr (cl->length);
976 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
977 cons->expr->ts.u.cl->length_from_typespec = true;
978 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
979 gfc_resolve_character_array_constructor (cons->expr);
983 if (cons->expr->expr_type == EXPR_NULL
984 && !(comp->attr.pointer || comp->attr.allocatable
985 || comp->attr.proc_pointer
986 || (comp->ts.type == BT_CLASS
987 && (CLASS_DATA (comp)->attr.class_pointer
988 || CLASS_DATA (comp)->attr.allocatable))))
991 gfc_error ("The NULL in the derived type constructor at %L is "
992 "being applied to component '%s', which is neither "
993 "a POINTER nor ALLOCATABLE", &cons->expr->where,
997 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
1000 a = gfc_expr_attr (cons->expr);
1002 if (!a.pointer && !a.target)
1005 gfc_error ("The element in the derived type constructor at %L, "
1006 "for pointer component '%s' should be a POINTER or "
1007 "a TARGET", &cons->expr->where, comp->name);
1012 /* F08:C461. Additional checks for pointer initialization. */
1016 gfc_error ("Pointer initialization target at %L "
1017 "must not be ALLOCATABLE ", &cons->expr->where);
1022 gfc_error ("Pointer initialization target at %L "
1023 "must have the SAVE attribute", &cons->expr->where);
1027 /* F2003, C1272 (3). */
1028 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1029 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1030 || gfc_is_coindexed (cons->expr)))
1033 gfc_error ("Invalid expression in the derived type constructor for "
1034 "pointer component '%s' at %L in PURE procedure",
1035 comp->name, &cons->expr->where);
1044 /****************** Expression name resolution ******************/
1046 /* Returns 0 if a symbol was not declared with a type or
1047 attribute declaration statement, nonzero otherwise. */
1050 was_declared (gfc_symbol *sym)
1056 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1059 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1060 || a.optional || a.pointer || a.save || a.target || a.volatile_
1061 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1062 || a.asynchronous || a.codimension)
1069 /* Determine if a symbol is generic or not. */
1072 generic_sym (gfc_symbol *sym)
1076 if (sym->attr.generic ||
1077 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1080 if (was_declared (sym) || sym->ns->parent == NULL)
1083 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1090 return generic_sym (s);
1097 /* Determine if a symbol is specific or not. */
1100 specific_sym (gfc_symbol *sym)
1104 if (sym->attr.if_source == IFSRC_IFBODY
1105 || sym->attr.proc == PROC_MODULE
1106 || sym->attr.proc == PROC_INTERNAL
1107 || sym->attr.proc == PROC_ST_FUNCTION
1108 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1109 || sym->attr.external)
1112 if (was_declared (sym) || sym->ns->parent == NULL)
1115 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1117 return (s == NULL) ? 0 : specific_sym (s);
1121 /* Figure out if the procedure is specific, generic or unknown. */
1124 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1128 procedure_kind (gfc_symbol *sym)
1130 if (generic_sym (sym))
1131 return PTYPE_GENERIC;
1133 if (specific_sym (sym))
1134 return PTYPE_SPECIFIC;
1136 return PTYPE_UNKNOWN;
1139 /* Check references to assumed size arrays. The flag need_full_assumed_size
1140 is nonzero when matching actual arguments. */
1142 static int need_full_assumed_size = 0;
1145 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1147 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1150 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1151 What should it be? */
1152 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1153 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1154 && (e->ref->u.ar.type == AR_FULL))
1156 gfc_error ("The upper bound in the last dimension must "
1157 "appear in the reference to the assumed size "
1158 "array '%s' at %L", sym->name, &e->where);
1165 /* Look for bad assumed size array references in argument expressions
1166 of elemental and array valued intrinsic procedures. Since this is
1167 called from procedure resolution functions, it only recurses at
1171 resolve_assumed_size_actual (gfc_expr *e)
1176 switch (e->expr_type)
1179 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1184 if (resolve_assumed_size_actual (e->value.op.op1)
1185 || resolve_assumed_size_actual (e->value.op.op2))
1196 /* Check a generic procedure, passed as an actual argument, to see if
1197 there is a matching specific name. If none, it is an error, and if
1198 more than one, the reference is ambiguous. */
1200 count_specific_procs (gfc_expr *e)
1207 sym = e->symtree->n.sym;
1209 for (p = sym->generic; p; p = p->next)
1210 if (strcmp (sym->name, p->sym->name) == 0)
1212 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1218 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1222 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1223 "argument at %L", sym->name, &e->where);
1229 /* See if a call to sym could possibly be a not allowed RECURSION because of
1230 a missing RECURIVE declaration. This means that either sym is the current
1231 context itself, or sym is the parent of a contained procedure calling its
1232 non-RECURSIVE containing procedure.
1233 This also works if sym is an ENTRY. */
1236 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1238 gfc_symbol* proc_sym;
1239 gfc_symbol* context_proc;
1240 gfc_namespace* real_context;
1242 if (sym->attr.flavor == FL_PROGRAM)
1245 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1247 /* If we've got an ENTRY, find real procedure. */
1248 if (sym->attr.entry && sym->ns->entries)
1249 proc_sym = sym->ns->entries->sym;
1253 /* If sym is RECURSIVE, all is well of course. */
1254 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1257 /* Find the context procedure's "real" symbol if it has entries.
1258 We look for a procedure symbol, so recurse on the parents if we don't
1259 find one (like in case of a BLOCK construct). */
1260 for (real_context = context; ; real_context = real_context->parent)
1262 /* We should find something, eventually! */
1263 gcc_assert (real_context);
1265 context_proc = (real_context->entries ? real_context->entries->sym
1266 : real_context->proc_name);
1268 /* In some special cases, there may not be a proc_name, like for this
1270 real(bad_kind()) function foo () ...
1271 when checking the call to bad_kind ().
1272 In these cases, we simply return here and assume that the
1277 if (context_proc->attr.flavor != FL_LABEL)
1281 /* A call from sym's body to itself is recursion, of course. */
1282 if (context_proc == proc_sym)
1285 /* The same is true if context is a contained procedure and sym the
1287 if (context_proc->attr.contained)
1289 gfc_symbol* parent_proc;
1291 gcc_assert (context->parent);
1292 parent_proc = (context->parent->entries ? context->parent->entries->sym
1293 : context->parent->proc_name);
1295 if (parent_proc == proc_sym)
1303 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1304 its typespec and formal argument list. */
1307 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1309 gfc_intrinsic_sym* isym;
1315 /* We already know this one is an intrinsic, so we don't call
1316 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1317 gfc_find_subroutine directly to check whether it is a function or
1320 if ((isym = gfc_find_function (sym->name)))
1322 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1323 && !sym->attr.implicit_type)
1324 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1325 " ignored", sym->name, &sym->declared_at);
1327 if (!sym->attr.function &&
1328 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1333 else if ((isym = gfc_find_subroutine (sym->name)))
1335 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1337 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1338 " specifier", sym->name, &sym->declared_at);
1342 if (!sym->attr.subroutine &&
1343 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1348 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1353 gfc_copy_formal_args_intr (sym, isym);
1355 /* Check it is actually available in the standard settings. */
1356 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1359 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1360 " available in the current standard settings but %s. Use"
1361 " an appropriate -std=* option or enable -fall-intrinsics"
1362 " in order to use it.",
1363 sym->name, &sym->declared_at, symstd);
1371 /* Resolve a procedure expression, like passing it to a called procedure or as
1372 RHS for a procedure pointer assignment. */
1375 resolve_procedure_expression (gfc_expr* expr)
1379 if (expr->expr_type != EXPR_VARIABLE)
1381 gcc_assert (expr->symtree);
1383 sym = expr->symtree->n.sym;
1385 if (sym->attr.intrinsic)
1386 resolve_intrinsic (sym, &expr->where);
1388 if (sym->attr.flavor != FL_PROCEDURE
1389 || (sym->attr.function && sym->result == sym))
1392 /* A non-RECURSIVE procedure that is used as procedure expression within its
1393 own body is in danger of being called recursively. */
1394 if (is_illegal_recursion (sym, gfc_current_ns))
1395 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1396 " itself recursively. Declare it RECURSIVE or use"
1397 " -frecursive", sym->name, &expr->where);
1403 /* Resolve an actual argument list. Most of the time, this is just
1404 resolving the expressions in the list.
1405 The exception is that we sometimes have to decide whether arguments
1406 that look like procedure arguments are really simple variable
1410 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1411 bool no_formal_args)
1414 gfc_symtree *parent_st;
1416 int save_need_full_assumed_size;
1417 gfc_component *comp;
1419 for (; arg; arg = arg->next)
1424 /* Check the label is a valid branching target. */
1427 if (arg->label->defined == ST_LABEL_UNKNOWN)
1429 gfc_error ("Label %d referenced at %L is never defined",
1430 arg->label->value, &arg->label->where);
1437 if (gfc_is_proc_ptr_comp (e, &comp))
1440 if (e->expr_type == EXPR_PPC)
1442 if (comp->as != NULL)
1443 e->rank = comp->as->rank;
1444 e->expr_type = EXPR_FUNCTION;
1446 if (gfc_resolve_expr (e) == FAILURE)
1451 if (e->expr_type == EXPR_VARIABLE
1452 && e->symtree->n.sym->attr.generic
1454 && count_specific_procs (e) != 1)
1457 if (e->ts.type != BT_PROCEDURE)
1459 save_need_full_assumed_size = need_full_assumed_size;
1460 if (e->expr_type != EXPR_VARIABLE)
1461 need_full_assumed_size = 0;
1462 if (gfc_resolve_expr (e) != SUCCESS)
1464 need_full_assumed_size = save_need_full_assumed_size;
1468 /* See if the expression node should really be a variable reference. */
1470 sym = e->symtree->n.sym;
1472 if (sym->attr.flavor == FL_PROCEDURE
1473 || sym->attr.intrinsic
1474 || sym->attr.external)
1478 /* If a procedure is not already determined to be something else
1479 check if it is intrinsic. */
1480 if (!sym->attr.intrinsic
1481 && !(sym->attr.external || sym->attr.use_assoc
1482 || sym->attr.if_source == IFSRC_IFBODY)
1483 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1484 sym->attr.intrinsic = 1;
1486 if (sym->attr.proc == PROC_ST_FUNCTION)
1488 gfc_error ("Statement function '%s' at %L is not allowed as an "
1489 "actual argument", sym->name, &e->where);
1492 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1493 sym->attr.subroutine);
1494 if (sym->attr.intrinsic && actual_ok == 0)
1496 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1497 "actual argument", sym->name, &e->where);
1500 if (sym->attr.contained && !sym->attr.use_assoc
1501 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1503 gfc_error ("Internal procedure '%s' is not allowed as an "
1504 "actual argument at %L", sym->name, &e->where);
1507 if (sym->attr.elemental && !sym->attr.intrinsic)
1509 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1510 "allowed as an actual argument at %L", sym->name,
1514 /* Check if a generic interface has a specific procedure
1515 with the same name before emitting an error. */
1516 if (sym->attr.generic && count_specific_procs (e) != 1)
1519 /* Just in case a specific was found for the expression. */
1520 sym = e->symtree->n.sym;
1522 /* If the symbol is the function that names the current (or
1523 parent) scope, then we really have a variable reference. */
1525 if (gfc_is_function_return_value (sym, sym->ns))
1528 /* If all else fails, see if we have a specific intrinsic. */
1529 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1531 gfc_intrinsic_sym *isym;
1533 isym = gfc_find_function (sym->name);
1534 if (isym == NULL || !isym->specific)
1536 gfc_error ("Unable to find a specific INTRINSIC procedure "
1537 "for the reference '%s' at %L", sym->name,
1542 sym->attr.intrinsic = 1;
1543 sym->attr.function = 1;
1546 if (gfc_resolve_expr (e) == FAILURE)
1551 /* See if the name is a module procedure in a parent unit. */
1553 if (was_declared (sym) || sym->ns->parent == NULL)
1556 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1558 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1562 if (parent_st == NULL)
1565 sym = parent_st->n.sym;
1566 e->symtree = parent_st; /* Point to the right thing. */
1568 if (sym->attr.flavor == FL_PROCEDURE
1569 || sym->attr.intrinsic
1570 || sym->attr.external)
1572 if (gfc_resolve_expr (e) == FAILURE)
1578 e->expr_type = EXPR_VARIABLE;
1580 if (sym->as != NULL)
1582 e->rank = sym->as->rank;
1583 e->ref = gfc_get_ref ();
1584 e->ref->type = REF_ARRAY;
1585 e->ref->u.ar.type = AR_FULL;
1586 e->ref->u.ar.as = sym->as;
1589 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1590 primary.c (match_actual_arg). If above code determines that it
1591 is a variable instead, it needs to be resolved as it was not
1592 done at the beginning of this function. */
1593 save_need_full_assumed_size = need_full_assumed_size;
1594 if (e->expr_type != EXPR_VARIABLE)
1595 need_full_assumed_size = 0;
1596 if (gfc_resolve_expr (e) != SUCCESS)
1598 need_full_assumed_size = save_need_full_assumed_size;
1601 /* Check argument list functions %VAL, %LOC and %REF. There is
1602 nothing to do for %REF. */
1603 if (arg->name && arg->name[0] == '%')
1605 if (strncmp ("%VAL", arg->name, 4) == 0)
1607 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1609 gfc_error ("By-value argument at %L is not of numeric "
1616 gfc_error ("By-value argument at %L cannot be an array or "
1617 "an array section", &e->where);
1621 /* Intrinsics are still PROC_UNKNOWN here. However,
1622 since same file external procedures are not resolvable
1623 in gfortran, it is a good deal easier to leave them to
1625 if (ptype != PROC_UNKNOWN
1626 && ptype != PROC_DUMMY
1627 && ptype != PROC_EXTERNAL
1628 && ptype != PROC_MODULE)
1630 gfc_error ("By-value argument at %L is not allowed "
1631 "in this context", &e->where);
1636 /* Statement functions have already been excluded above. */
1637 else if (strncmp ("%LOC", arg->name, 4) == 0
1638 && e->ts.type == BT_PROCEDURE)
1640 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1642 gfc_error ("Passing internal procedure at %L by location "
1643 "not allowed", &e->where);
1649 /* Fortran 2008, C1237. */
1650 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1651 && gfc_has_ultimate_pointer (e))
1653 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1654 "component", &e->where);
1663 /* Do the checks of the actual argument list that are specific to elemental
1664 procedures. If called with c == NULL, we have a function, otherwise if
1665 expr == NULL, we have a subroutine. */
1668 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1670 gfc_actual_arglist *arg0;
1671 gfc_actual_arglist *arg;
1672 gfc_symbol *esym = NULL;
1673 gfc_intrinsic_sym *isym = NULL;
1675 gfc_intrinsic_arg *iformal = NULL;
1676 gfc_formal_arglist *eformal = NULL;
1677 bool formal_optional = false;
1678 bool set_by_optional = false;
1682 /* Is this an elemental procedure? */
1683 if (expr && expr->value.function.actual != NULL)
1685 if (expr->value.function.esym != NULL
1686 && expr->value.function.esym->attr.elemental)
1688 arg0 = expr->value.function.actual;
1689 esym = expr->value.function.esym;
1691 else if (expr->value.function.isym != NULL
1692 && expr->value.function.isym->elemental)
1694 arg0 = expr->value.function.actual;
1695 isym = expr->value.function.isym;
1700 else if (c && c->ext.actual != NULL)
1702 arg0 = c->ext.actual;
1704 if (c->resolved_sym)
1705 esym = c->resolved_sym;
1707 esym = c->symtree->n.sym;
1710 if (!esym->attr.elemental)
1716 /* The rank of an elemental is the rank of its array argument(s). */
1717 for (arg = arg0; arg; arg = arg->next)
1719 if (arg->expr != NULL && arg->expr->rank > 0)
1721 rank = arg->expr->rank;
1722 if (arg->expr->expr_type == EXPR_VARIABLE
1723 && arg->expr->symtree->n.sym->attr.optional)
1724 set_by_optional = true;
1726 /* Function specific; set the result rank and shape. */
1730 if (!expr->shape && arg->expr->shape)
1732 expr->shape = gfc_get_shape (rank);
1733 for (i = 0; i < rank; i++)
1734 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1741 /* If it is an array, it shall not be supplied as an actual argument
1742 to an elemental procedure unless an array of the same rank is supplied
1743 as an actual argument corresponding to a nonoptional dummy argument of
1744 that elemental procedure(12.4.1.5). */
1745 formal_optional = false;
1747 iformal = isym->formal;
1749 eformal = esym->formal;
1751 for (arg = arg0; arg; arg = arg->next)
1755 if (eformal->sym && eformal->sym->attr.optional)
1756 formal_optional = true;
1757 eformal = eformal->next;
1759 else if (isym && iformal)
1761 if (iformal->optional)
1762 formal_optional = true;
1763 iformal = iformal->next;
1766 formal_optional = true;
1768 if (pedantic && arg->expr != NULL
1769 && arg->expr->expr_type == EXPR_VARIABLE
1770 && arg->expr->symtree->n.sym->attr.optional
1773 && (set_by_optional || arg->expr->rank != rank)
1774 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1776 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1777 "MISSING, it cannot be the actual argument of an "
1778 "ELEMENTAL procedure unless there is a non-optional "
1779 "argument with the same rank (12.4.1.5)",
1780 arg->expr->symtree->n.sym->name, &arg->expr->where);
1785 for (arg = arg0; arg; arg = arg->next)
1787 if (arg->expr == NULL || arg->expr->rank == 0)
1790 /* Being elemental, the last upper bound of an assumed size array
1791 argument must be present. */
1792 if (resolve_assumed_size_actual (arg->expr))
1795 /* Elemental procedure's array actual arguments must conform. */
1798 if (gfc_check_conformance (arg->expr, e,
1799 "elemental procedure") == FAILURE)
1806 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1807 is an array, the intent inout/out variable needs to be also an array. */
1808 if (rank > 0 && esym && expr == NULL)
1809 for (eformal = esym->formal, arg = arg0; arg && eformal;
1810 arg = arg->next, eformal = eformal->next)
1811 if ((eformal->sym->attr.intent == INTENT_OUT
1812 || eformal->sym->attr.intent == INTENT_INOUT)
1813 && arg->expr && arg->expr->rank == 0)
1815 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1816 "ELEMENTAL subroutine '%s' is a scalar, but another "
1817 "actual argument is an array", &arg->expr->where,
1818 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1819 : "INOUT", eformal->sym->name, esym->name);
1826 /* Go through each actual argument in ACTUAL and see if it can be
1827 implemented as an inlined, non-copying intrinsic. FNSYM is the
1828 function being called, or NULL if not known. */
1831 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1833 gfc_actual_arglist *ap;
1836 for (ap = actual; ap; ap = ap->next)
1838 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1839 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1841 ap->expr->inline_noncopying_intrinsic = 1;
1845 /* This function does the checking of references to global procedures
1846 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1847 77 and 95 standards. It checks for a gsymbol for the name, making
1848 one if it does not already exist. If it already exists, then the
1849 reference being resolved must correspond to the type of gsymbol.
1850 Otherwise, the new symbol is equipped with the attributes of the
1851 reference. The corresponding code that is called in creating
1852 global entities is parse.c.
1854 In addition, for all but -std=legacy, the gsymbols are used to
1855 check the interfaces of external procedures from the same file.
1856 The namespace of the gsymbol is resolved and then, once this is
1857 done the interface is checked. */
1861 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1863 if (!gsym_ns->proc_name->attr.recursive)
1866 if (sym->ns == gsym_ns)
1869 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1876 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1878 if (gsym_ns->entries)
1880 gfc_entry_list *entry = gsym_ns->entries;
1882 for (; entry; entry = entry->next)
1884 if (strcmp (sym->name, entry->sym->name) == 0)
1886 if (strcmp (gsym_ns->proc_name->name,
1887 sym->ns->proc_name->name) == 0)
1891 && strcmp (gsym_ns->proc_name->name,
1892 sym->ns->parent->proc_name->name) == 0)
1901 resolve_global_procedure (gfc_symbol *sym, locus *where,
1902 gfc_actual_arglist **actual, int sub)
1906 enum gfc_symbol_type type;
1908 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1910 gsym = gfc_get_gsymbol (sym->name);
1912 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1913 gfc_global_used (gsym, where);
1915 if (gfc_option.flag_whole_file
1916 && (sym->attr.if_source == IFSRC_UNKNOWN
1917 || sym->attr.if_source == IFSRC_IFBODY)
1918 && gsym->type != GSYM_UNKNOWN
1920 && gsym->ns->resolved != -1
1921 && gsym->ns->proc_name
1922 && not_in_recursive (sym, gsym->ns)
1923 && not_entry_self_reference (sym, gsym->ns))
1925 gfc_symbol *def_sym;
1927 /* Resolve the gsymbol namespace if needed. */
1928 if (!gsym->ns->resolved)
1930 gfc_dt_list *old_dt_list;
1932 /* Stash away derived types so that the backend_decls do not
1934 old_dt_list = gfc_derived_types;
1935 gfc_derived_types = NULL;
1937 gfc_resolve (gsym->ns);
1939 /* Store the new derived types with the global namespace. */
1940 if (gfc_derived_types)
1941 gsym->ns->derived_types = gfc_derived_types;
1943 /* Restore the derived types of this namespace. */
1944 gfc_derived_types = old_dt_list;
1947 /* Make sure that translation for the gsymbol occurs before
1948 the procedure currently being resolved. */
1949 ns = gfc_global_ns_list;
1950 for (; ns && ns != gsym->ns; ns = ns->sibling)
1952 if (ns->sibling == gsym->ns)
1954 ns->sibling = gsym->ns->sibling;
1955 gsym->ns->sibling = gfc_global_ns_list;
1956 gfc_global_ns_list = gsym->ns;
1961 def_sym = gsym->ns->proc_name;
1962 if (def_sym->attr.entry_master)
1964 gfc_entry_list *entry;
1965 for (entry = gsym->ns->entries; entry; entry = entry->next)
1966 if (strcmp (entry->sym->name, sym->name) == 0)
1968 def_sym = entry->sym;
1973 /* Differences in constant character lengths. */
1974 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
1976 long int l1 = 0, l2 = 0;
1977 gfc_charlen *cl1 = sym->ts.u.cl;
1978 gfc_charlen *cl2 = def_sym->ts.u.cl;
1981 && cl1->length != NULL
1982 && cl1->length->expr_type == EXPR_CONSTANT)
1983 l1 = mpz_get_si (cl1->length->value.integer);
1986 && cl2->length != NULL
1987 && cl2->length->expr_type == EXPR_CONSTANT)
1988 l2 = mpz_get_si (cl2->length->value.integer);
1990 if (l1 && l2 && l1 != l2)
1991 gfc_error ("Character length mismatch in return type of "
1992 "function '%s' at %L (%ld/%ld)", sym->name,
1993 &sym->declared_at, l1, l2);
1996 /* Type mismatch of function return type and expected type. */
1997 if (sym->attr.function
1998 && !gfc_compare_types (&sym->ts, &def_sym->ts))
1999 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2000 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2001 gfc_typename (&def_sym->ts));
2003 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2005 gfc_formal_arglist *arg = def_sym->formal;
2006 for ( ; arg; arg = arg->next)
2009 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2010 else if (arg->sym->attr.allocatable
2011 || arg->sym->attr.asynchronous
2012 || arg->sym->attr.optional
2013 || arg->sym->attr.pointer
2014 || arg->sym->attr.target
2015 || arg->sym->attr.value
2016 || arg->sym->attr.volatile_)
2018 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2019 "has an attribute that requires an explicit "
2020 "interface for this procedure", arg->sym->name,
2021 sym->name, &sym->declared_at);
2024 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2025 else if (arg->sym && arg->sym->as
2026 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2028 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2029 "argument '%s' must have an explicit interface",
2030 sym->name, &sym->declared_at, arg->sym->name);
2033 /* F2008, 12.4.2.2 (2c) */
2034 else if (arg->sym->attr.codimension)
2036 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2037 "'%s' must have an explicit interface",
2038 sym->name, &sym->declared_at, arg->sym->name);
2041 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2042 else if (false) /* TODO: is a parametrized derived type */
2044 gfc_error ("Procedure '%s' at %L with parametrized derived "
2045 "type argument '%s' must have an explicit "
2046 "interface", sym->name, &sym->declared_at,
2050 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2051 else if (arg->sym->ts.type == BT_CLASS)
2053 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2054 "argument '%s' must have an explicit interface",
2055 sym->name, &sym->declared_at, arg->sym->name);
2060 if (def_sym->attr.function)
2062 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2063 if (def_sym->as && def_sym->as->rank
2064 && (!sym->as || sym->as->rank != def_sym->as->rank))
2065 gfc_error ("The reference to function '%s' at %L either needs an "
2066 "explicit INTERFACE or the rank is incorrect", sym->name,
2069 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2070 if ((def_sym->result->attr.pointer
2071 || def_sym->result->attr.allocatable)
2072 && (sym->attr.if_source != IFSRC_IFBODY
2073 || def_sym->result->attr.pointer
2074 != sym->result->attr.pointer
2075 || def_sym->result->attr.allocatable
2076 != sym->result->attr.allocatable))
2077 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2078 "result must have an explicit interface", sym->name,
2081 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2082 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2083 && def_sym->ts.u.cl->length != NULL)
2085 gfc_charlen *cl = sym->ts.u.cl;
2087 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2088 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2090 gfc_error ("Nonconstant character-length function '%s' at %L "
2091 "must have an explicit interface", sym->name,
2097 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2098 if (def_sym->attr.elemental && !sym->attr.elemental)
2100 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2101 "interface", sym->name, &sym->declared_at);
2104 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2105 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2107 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2108 "an explicit interface", sym->name, &sym->declared_at);
2111 if (gfc_option.flag_whole_file == 1
2112 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2113 && !(gfc_option.warn_std & GFC_STD_GNU)))
2114 gfc_errors_to_warnings (1);
2116 if (sym->attr.if_source != IFSRC_IFBODY)
2117 gfc_procedure_use (def_sym, actual, where);
2119 gfc_errors_to_warnings (0);
2122 if (gsym->type == GSYM_UNKNOWN)
2125 gsym->where = *where;
2132 /************* Function resolution *************/
2134 /* Resolve a function call known to be generic.
2135 Section 14.1.2.4.1. */
2138 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2142 if (sym->attr.generic)
2144 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2147 expr->value.function.name = s->name;
2148 expr->value.function.esym = s;
2150 if (s->ts.type != BT_UNKNOWN)
2152 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2153 expr->ts = s->result->ts;
2156 expr->rank = s->as->rank;
2157 else if (s->result != NULL && s->result->as != NULL)
2158 expr->rank = s->result->as->rank;
2160 gfc_set_sym_referenced (expr->value.function.esym);
2165 /* TODO: Need to search for elemental references in generic
2169 if (sym->attr.intrinsic)
2170 return gfc_intrinsic_func_interface (expr, 0);
2177 resolve_generic_f (gfc_expr *expr)
2182 sym = expr->symtree->n.sym;
2186 m = resolve_generic_f0 (expr, sym);
2189 else if (m == MATCH_ERROR)
2193 if (sym->ns->parent == NULL)
2195 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2199 if (!generic_sym (sym))
2203 /* Last ditch attempt. See if the reference is to an intrinsic
2204 that possesses a matching interface. 14.1.2.4 */
2205 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2207 gfc_error ("There is no specific function for the generic '%s' at %L",
2208 expr->symtree->n.sym->name, &expr->where);
2212 m = gfc_intrinsic_func_interface (expr, 0);
2216 gfc_error ("Generic function '%s' at %L is not consistent with a "
2217 "specific intrinsic interface", expr->symtree->n.sym->name,
2224 /* Resolve a function call known to be specific. */
2227 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2231 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2233 if (sym->attr.dummy)
2235 sym->attr.proc = PROC_DUMMY;
2239 sym->attr.proc = PROC_EXTERNAL;
2243 if (sym->attr.proc == PROC_MODULE
2244 || sym->attr.proc == PROC_ST_FUNCTION
2245 || sym->attr.proc == PROC_INTERNAL)
2248 if (sym->attr.intrinsic)
2250 m = gfc_intrinsic_func_interface (expr, 1);
2254 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2255 "with an intrinsic", sym->name, &expr->where);
2263 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2266 expr->ts = sym->result->ts;
2269 expr->value.function.name = sym->name;
2270 expr->value.function.esym = sym;
2271 if (sym->as != NULL)
2272 expr->rank = sym->as->rank;
2279 resolve_specific_f (gfc_expr *expr)
2284 sym = expr->symtree->n.sym;
2288 m = resolve_specific_f0 (sym, expr);
2291 if (m == MATCH_ERROR)
2294 if (sym->ns->parent == NULL)
2297 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2303 gfc_error ("Unable to resolve the specific function '%s' at %L",
2304 expr->symtree->n.sym->name, &expr->where);
2310 /* Resolve a procedure call not known to be generic nor specific. */
2313 resolve_unknown_f (gfc_expr *expr)
2318 sym = expr->symtree->n.sym;
2320 if (sym->attr.dummy)
2322 sym->attr.proc = PROC_DUMMY;
2323 expr->value.function.name = sym->name;
2327 /* See if we have an intrinsic function reference. */
2329 if (gfc_is_intrinsic (sym, 0, expr->where))
2331 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2336 /* The reference is to an external name. */
2338 sym->attr.proc = PROC_EXTERNAL;
2339 expr->value.function.name = sym->name;
2340 expr->value.function.esym = expr->symtree->n.sym;
2342 if (sym->as != NULL)
2343 expr->rank = sym->as->rank;
2345 /* Type of the expression is either the type of the symbol or the
2346 default type of the symbol. */
2349 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2351 if (sym->ts.type != BT_UNKNOWN)
2355 ts = gfc_get_default_type (sym->name, sym->ns);
2357 if (ts->type == BT_UNKNOWN)
2359 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2360 sym->name, &expr->where);
2371 /* Return true, if the symbol is an external procedure. */
2373 is_external_proc (gfc_symbol *sym)
2375 if (!sym->attr.dummy && !sym->attr.contained
2376 && !(sym->attr.intrinsic
2377 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2378 && sym->attr.proc != PROC_ST_FUNCTION
2379 && !sym->attr.proc_pointer
2380 && !sym->attr.use_assoc
2388 /* Figure out if a function reference is pure or not. Also set the name
2389 of the function for a potential error message. Return nonzero if the
2390 function is PURE, zero if not. */
2392 pure_stmt_function (gfc_expr *, gfc_symbol *);
2395 pure_function (gfc_expr *e, const char **name)
2401 if (e->symtree != NULL
2402 && e->symtree->n.sym != NULL
2403 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2404 return pure_stmt_function (e, e->symtree->n.sym);
2406 if (e->value.function.esym)
2408 pure = gfc_pure (e->value.function.esym);
2409 *name = e->value.function.esym->name;
2411 else if (e->value.function.isym)
2413 pure = e->value.function.isym->pure
2414 || e->value.function.isym->elemental;
2415 *name = e->value.function.isym->name;
2419 /* Implicit functions are not pure. */
2421 *name = e->value.function.name;
2429 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2430 int *f ATTRIBUTE_UNUSED)
2434 /* Don't bother recursing into other statement functions
2435 since they will be checked individually for purity. */
2436 if (e->expr_type != EXPR_FUNCTION
2438 || e->symtree->n.sym == sym
2439 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2442 return pure_function (e, &name) ? false : true;
2447 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2449 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2454 is_scalar_expr_ptr (gfc_expr *expr)
2456 gfc_try retval = SUCCESS;
2461 /* See if we have a gfc_ref, which means we have a substring, array
2462 reference, or a component. */
2463 if (expr->ref != NULL)
2466 while (ref->next != NULL)
2472 if (ref->u.ss.length != NULL
2473 && ref->u.ss.length->length != NULL
2475 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2477 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2479 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2480 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2481 if (end - start + 1 != 1)
2488 if (ref->u.ar.type == AR_ELEMENT)
2490 else if (ref->u.ar.type == AR_FULL)
2492 /* The user can give a full array if the array is of size 1. */
2493 if (ref->u.ar.as != NULL
2494 && ref->u.ar.as->rank == 1
2495 && ref->u.ar.as->type == AS_EXPLICIT
2496 && ref->u.ar.as->lower[0] != NULL
2497 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2498 && ref->u.ar.as->upper[0] != NULL
2499 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2501 /* If we have a character string, we need to check if
2502 its length is one. */
2503 if (expr->ts.type == BT_CHARACTER)
2505 if (expr->ts.u.cl == NULL
2506 || expr->ts.u.cl->length == NULL
2507 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2513 /* We have constant lower and upper bounds. If the
2514 difference between is 1, it can be considered a
2516 start = (int) mpz_get_si
2517 (ref->u.ar.as->lower[0]->value.integer);
2518 end = (int) mpz_get_si
2519 (ref->u.ar.as->upper[0]->value.integer);
2520 if (end - start + 1 != 1)
2535 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2537 /* Character string. Make sure it's of length 1. */
2538 if (expr->ts.u.cl == NULL
2539 || expr->ts.u.cl->length == NULL
2540 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2543 else if (expr->rank != 0)
2550 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2551 and, in the case of c_associated, set the binding label based on
2555 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2556 gfc_symbol **new_sym)
2558 char name[GFC_MAX_SYMBOL_LEN + 1];
2559 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2560 int optional_arg = 0;
2561 gfc_try retval = SUCCESS;
2562 gfc_symbol *args_sym;
2563 gfc_typespec *arg_ts;
2564 symbol_attribute arg_attr;
2566 if (args->expr->expr_type == EXPR_CONSTANT
2567 || args->expr->expr_type == EXPR_OP
2568 || args->expr->expr_type == EXPR_NULL)
2570 gfc_error ("Argument to '%s' at %L is not a variable",
2571 sym->name, &(args->expr->where));
2575 args_sym = args->expr->symtree->n.sym;
2577 /* The typespec for the actual arg should be that stored in the expr
2578 and not necessarily that of the expr symbol (args_sym), because
2579 the actual expression could be a part-ref of the expr symbol. */
2580 arg_ts = &(args->expr->ts);
2581 arg_attr = gfc_expr_attr (args->expr);
2583 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2585 /* If the user gave two args then they are providing something for
2586 the optional arg (the second cptr). Therefore, set the name and
2587 binding label to the c_associated for two cptrs. Otherwise,
2588 set c_associated to expect one cptr. */
2592 sprintf (name, "%s_2", sym->name);
2593 sprintf (binding_label, "%s_2", sym->binding_label);
2599 sprintf (name, "%s_1", sym->name);
2600 sprintf (binding_label, "%s_1", sym->binding_label);
2604 /* Get a new symbol for the version of c_associated that
2606 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2608 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2609 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2611 sprintf (name, "%s", sym->name);
2612 sprintf (binding_label, "%s", sym->binding_label);
2614 /* Error check the call. */
2615 if (args->next != NULL)
2617 gfc_error_now ("More actual than formal arguments in '%s' "
2618 "call at %L", name, &(args->expr->where));
2621 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2623 /* Make sure we have either the target or pointer attribute. */
2624 if (!arg_attr.target && !arg_attr.pointer)
2626 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2627 "a TARGET or an associated pointer",
2629 sym->name, &(args->expr->where));
2633 /* See if we have interoperable type and type param. */
2634 if (verify_c_interop (arg_ts) == SUCCESS
2635 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2637 if (args_sym->attr.target == 1)
2639 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2640 has the target attribute and is interoperable. */
2641 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2642 allocatable variable that has the TARGET attribute and
2643 is not an array of zero size. */
2644 if (args_sym->attr.allocatable == 1)
2646 if (args_sym->attr.dimension != 0
2647 && (args_sym->as && args_sym->as->rank == 0))
2649 gfc_error_now ("Allocatable variable '%s' used as a "
2650 "parameter to '%s' at %L must not be "
2651 "an array of zero size",
2652 args_sym->name, sym->name,
2653 &(args->expr->where));
2659 /* A non-allocatable target variable with C
2660 interoperable type and type parameters must be
2662 if (args_sym && args_sym->attr.dimension)
2664 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2666 gfc_error ("Assumed-shape array '%s' at %L "
2667 "cannot be an argument to the "
2668 "procedure '%s' because "
2669 "it is not C interoperable",
2671 &(args->expr->where), sym->name);
2674 else if (args_sym->as->type == AS_DEFERRED)
2676 gfc_error ("Deferred-shape array '%s' at %L "
2677 "cannot be an argument to the "
2678 "procedure '%s' because "
2679 "it is not C interoperable",
2681 &(args->expr->where), sym->name);
2686 /* Make sure it's not a character string. Arrays of
2687 any type should be ok if the variable is of a C
2688 interoperable type. */
2689 if (arg_ts->type == BT_CHARACTER)
2690 if (arg_ts->u.cl != NULL
2691 && (arg_ts->u.cl->length == NULL
2692 || arg_ts->u.cl->length->expr_type
2695 (arg_ts->u.cl->length->value.integer, 1)
2697 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2699 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2700 "at %L must have a length of 1",
2701 args_sym->name, sym->name,
2702 &(args->expr->where));
2707 else if (arg_attr.pointer
2708 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2710 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2712 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2713 "associated scalar POINTER", args_sym->name,
2714 sym->name, &(args->expr->where));
2720 /* The parameter is not required to be C interoperable. If it
2721 is not C interoperable, it must be a nonpolymorphic scalar
2722 with no length type parameters. It still must have either
2723 the pointer or target attribute, and it can be
2724 allocatable (but must be allocated when c_loc is called). */
2725 if (args->expr->rank != 0
2726 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2728 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2729 "scalar", args_sym->name, sym->name,
2730 &(args->expr->where));
2733 else if (arg_ts->type == BT_CHARACTER
2734 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2736 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2737 "%L must have a length of 1",
2738 args_sym->name, sym->name,
2739 &(args->expr->where));
2742 else if (arg_ts->type == BT_CLASS)
2744 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2745 "polymorphic", args_sym->name, sym->name,
2746 &(args->expr->where));
2751 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2753 if (args_sym->attr.flavor != FL_PROCEDURE)
2755 /* TODO: Update this error message to allow for procedure
2756 pointers once they are implemented. */
2757 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2759 args_sym->name, sym->name,
2760 &(args->expr->where));
2763 else if (args_sym->attr.is_bind_c != 1)
2765 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2767 args_sym->name, sym->name,
2768 &(args->expr->where));
2773 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2778 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2779 "iso_c_binding function: '%s'!\n", sym->name);
2786 /* Resolve a function call, which means resolving the arguments, then figuring
2787 out which entity the name refers to. */
2788 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2789 to INTENT(OUT) or INTENT(INOUT). */
2792 resolve_function (gfc_expr *expr)
2794 gfc_actual_arglist *arg;
2799 procedure_type p = PROC_INTRINSIC;
2800 bool no_formal_args;
2804 sym = expr->symtree->n.sym;
2806 /* If this is a procedure pointer component, it has already been resolved. */
2807 if (gfc_is_proc_ptr_comp (expr, NULL))
2810 if (sym && sym->attr.intrinsic
2811 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2814 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2816 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2820 /* If this ia a deferred TBP with an abstract interface (which may
2821 of course be referenced), expr->value.function.esym will be set. */
2822 if (sym && sym->attr.abstract && !expr->value.function.esym)
2824 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2825 sym->name, &expr->where);
2829 /* Switch off assumed size checking and do this again for certain kinds
2830 of procedure, once the procedure itself is resolved. */
2831 need_full_assumed_size++;
2833 if (expr->symtree && expr->symtree->n.sym)
2834 p = expr->symtree->n.sym->attr.proc;
2836 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2837 inquiry_argument = true;
2838 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2840 if (resolve_actual_arglist (expr->value.function.actual,
2841 p, no_formal_args) == FAILURE)
2843 inquiry_argument = false;
2847 inquiry_argument = false;
2849 /* Need to setup the call to the correct c_associated, depending on
2850 the number of cptrs to user gives to compare. */
2851 if (sym && sym->attr.is_iso_c == 1)
2853 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2857 /* Get the symtree for the new symbol (resolved func).
2858 the old one will be freed later, when it's no longer used. */
2859 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2862 /* Resume assumed_size checking. */
2863 need_full_assumed_size--;
2865 /* If the procedure is external, check for usage. */
2866 if (sym && is_external_proc (sym))
2867 resolve_global_procedure (sym, &expr->where,
2868 &expr->value.function.actual, 0);
2870 if (sym && sym->ts.type == BT_CHARACTER
2872 && sym->ts.u.cl->length == NULL
2874 && expr->value.function.esym == NULL
2875 && !sym->attr.contained)
2877 /* Internal procedures are taken care of in resolve_contained_fntype. */
2878 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2879 "be used at %L since it is not a dummy argument",
2880 sym->name, &expr->where);
2884 /* See if function is already resolved. */
2886 if (expr->value.function.name != NULL)
2888 if (expr->ts.type == BT_UNKNOWN)
2894 /* Apply the rules of section 14.1.2. */
2896 switch (procedure_kind (sym))
2899 t = resolve_generic_f (expr);
2902 case PTYPE_SPECIFIC:
2903 t = resolve_specific_f (expr);
2907 t = resolve_unknown_f (expr);
2911 gfc_internal_error ("resolve_function(): bad function type");
2915 /* If the expression is still a function (it might have simplified),
2916 then we check to see if we are calling an elemental function. */
2918 if (expr->expr_type != EXPR_FUNCTION)
2921 temp = need_full_assumed_size;
2922 need_full_assumed_size = 0;
2924 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2927 if (omp_workshare_flag
2928 && expr->value.function.esym
2929 && ! gfc_elemental (expr->value.function.esym))
2931 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2932 "in WORKSHARE construct", expr->value.function.esym->name,
2937 #define GENERIC_ID expr->value.function.isym->id
2938 else if (expr->value.function.actual != NULL
2939 && expr->value.function.isym != NULL
2940 && GENERIC_ID != GFC_ISYM_LBOUND
2941 && GENERIC_ID != GFC_ISYM_LEN
2942 && GENERIC_ID != GFC_ISYM_LOC
2943 && GENERIC_ID != GFC_ISYM_PRESENT)
2945 /* Array intrinsics must also have the last upper bound of an
2946 assumed size array argument. UBOUND and SIZE have to be
2947 excluded from the check if the second argument is anything
2950 for (arg = expr->value.function.actual; arg; arg = arg->next)
2952 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2953 && arg->next != NULL && arg->next->expr)
2955 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2958 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2961 if ((int)mpz_get_si (arg->next->expr->value.integer)
2966 if (arg->expr != NULL
2967 && arg->expr->rank > 0
2968 && resolve_assumed_size_actual (arg->expr))
2974 need_full_assumed_size = temp;
2977 if (!pure_function (expr, &name) && name)
2981 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2982 "FORALL %s", name, &expr->where,
2983 forall_flag == 2 ? "mask" : "block");
2986 else if (gfc_pure (NULL))
2988 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2989 "procedure within a PURE procedure", name, &expr->where);
2994 /* Functions without the RECURSIVE attribution are not allowed to
2995 * call themselves. */
2996 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2999 esym = expr->value.function.esym;
3001 if (is_illegal_recursion (esym, gfc_current_ns))
3003 if (esym->attr.entry && esym->ns->entries)
3004 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3005 " function '%s' is not RECURSIVE",
3006 esym->name, &expr->where, esym->ns->entries->sym->name);
3008 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3009 " is not RECURSIVE", esym->name, &expr->where);
3015 /* Character lengths of use associated functions may contains references to
3016 symbols not referenced from the current program unit otherwise. Make sure
3017 those symbols are marked as referenced. */
3019 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3020 && expr->value.function.esym->attr.use_assoc)
3022 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3026 && !((expr->value.function.esym
3027 && expr->value.function.esym->attr.elemental)
3029 (expr->value.function.isym
3030 && expr->value.function.isym->elemental)))
3031 find_noncopying_intrinsics (expr->value.function.esym,
3032 expr->value.function.actual);
3034 /* Make sure that the expression has a typespec that works. */
3035 if (expr->ts.type == BT_UNKNOWN)
3037 if (expr->symtree->n.sym->result
3038 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3039 && !expr->symtree->n.sym->result->attr.proc_pointer)
3040 expr->ts = expr->symtree->n.sym->result->ts;
3047 /************* Subroutine resolution *************/
3050 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3056 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3057 sym->name, &c->loc);
3058 else if (gfc_pure (NULL))
3059 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3065 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3069 if (sym->attr.generic)
3071 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3074 c->resolved_sym = s;
3075 pure_subroutine (c, s);
3079 /* TODO: Need to search for elemental references in generic interface. */
3082 if (sym->attr.intrinsic)
3083 return gfc_intrinsic_sub_interface (c, 0);
3090 resolve_generic_s (gfc_code *c)
3095 sym = c->symtree->n.sym;
3099 m = resolve_generic_s0 (c, sym);
3102 else if (m == MATCH_ERROR)
3106 if (sym->ns->parent == NULL)
3108 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3112 if (!generic_sym (sym))
3116 /* Last ditch attempt. See if the reference is to an intrinsic
3117 that possesses a matching interface. 14.1.2.4 */
3118 sym = c->symtree->n.sym;
3120 if (!gfc_is_intrinsic (sym, 1, c->loc))
3122 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3123 sym->name, &c->loc);
3127 m = gfc_intrinsic_sub_interface (c, 0);
3131 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3132 "intrinsic subroutine interface", sym->name, &c->loc);
3138 /* Set the name and binding label of the subroutine symbol in the call
3139 expression represented by 'c' to include the type and kind of the
3140 second parameter. This function is for resolving the appropriate
3141 version of c_f_pointer() and c_f_procpointer(). For example, a
3142 call to c_f_pointer() for a default integer pointer could have a
3143 name of c_f_pointer_i4. If no second arg exists, which is an error
3144 for these two functions, it defaults to the generic symbol's name
3145 and binding label. */
3148 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3149 char *name, char *binding_label)
3151 gfc_expr *arg = NULL;
3155 /* The second arg of c_f_pointer and c_f_procpointer determines
3156 the type and kind for the procedure name. */
3157 arg = c->ext.actual->next->expr;
3161 /* Set up the name to have the given symbol's name,
3162 plus the type and kind. */
3163 /* a derived type is marked with the type letter 'u' */
3164 if (arg->ts.type == BT_DERIVED)
3167 kind = 0; /* set the kind as 0 for now */
3171 type = gfc_type_letter (arg->ts.type);
3172 kind = arg->ts.kind;
3175 if (arg->ts.type == BT_CHARACTER)
3176 /* Kind info for character strings not needed. */
3179 sprintf (name, "%s_%c%d", sym->name, type, kind);
3180 /* Set up the binding label as the given symbol's label plus
3181 the type and kind. */
3182 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3186 /* If the second arg is missing, set the name and label as
3187 was, cause it should at least be found, and the missing
3188 arg error will be caught by compare_parameters(). */
3189 sprintf (name, "%s", sym->name);
3190 sprintf (binding_label, "%s", sym->binding_label);
3197 /* Resolve a generic version of the iso_c_binding procedure given
3198 (sym) to the specific one based on the type and kind of the
3199 argument(s). Currently, this function resolves c_f_pointer() and
3200 c_f_procpointer based on the type and kind of the second argument
3201 (FPTR). Other iso_c_binding procedures aren't specially handled.
3202 Upon successfully exiting, c->resolved_sym will hold the resolved
3203 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3207 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3209 gfc_symbol *new_sym;
3210 /* this is fine, since we know the names won't use the max */
3211 char name[GFC_MAX_SYMBOL_LEN + 1];
3212 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3213 /* default to success; will override if find error */
3214 match m = MATCH_YES;
3216 /* Make sure the actual arguments are in the necessary order (based on the
3217 formal args) before resolving. */
3218 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3220 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3221 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3223 set_name_and_label (c, sym, name, binding_label);
3225 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3227 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3229 /* Make sure we got a third arg if the second arg has non-zero
3230 rank. We must also check that the type and rank are
3231 correct since we short-circuit this check in
3232 gfc_procedure_use() (called above to sort actual args). */
3233 if (c->ext.actual->next->expr->rank != 0)
3235 if(c->ext.actual->next->next == NULL
3236 || c->ext.actual->next->next->expr == NULL)
3239 gfc_error ("Missing SHAPE parameter for call to %s "
3240 "at %L", sym->name, &(c->loc));
3242 else if (c->ext.actual->next->next->expr->ts.type
3244 || c->ext.actual->next->next->expr->rank != 1)
3247 gfc_error ("SHAPE parameter for call to %s at %L must "
3248 "be a rank 1 INTEGER array", sym->name,
3255 if (m != MATCH_ERROR)
3257 /* the 1 means to add the optional arg to formal list */
3258 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3260 /* for error reporting, say it's declared where the original was */
3261 new_sym->declared_at = sym->declared_at;
3266 /* no differences for c_loc or c_funloc */
3270 /* set the resolved symbol */
3271 if (m != MATCH_ERROR)
3272 c->resolved_sym = new_sym;
3274 c->resolved_sym = sym;
3280 /* Resolve a subroutine call known to be specific. */
3283 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3287 if(sym->attr.is_iso_c)
3289 m = gfc_iso_c_sub_interface (c,sym);
3293 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3295 if (sym->attr.dummy)
3297 sym->attr.proc = PROC_DUMMY;
3301 sym->attr.proc = PROC_EXTERNAL;
3305 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3308 if (sym->attr.intrinsic)
3310 m = gfc_intrinsic_sub_interface (c, 1);
3314 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3315 "with an intrinsic", sym->name, &c->loc);
3323 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3325 c->resolved_sym = sym;
3326 pure_subroutine (c, sym);
3333 resolve_specific_s (gfc_code *c)
3338 sym = c->symtree->n.sym;