1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
29 #include "arith.h" /* For gfc_compare_expr(). */
30 #include "dependency.h"
32 /* Types used in equivalence statements. */
36 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current, *tail;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
50 bitmap reachable_labels;
54 static code_stack *cs_base = NULL;
57 /* Nonzero if we're inside a FORALL block. */
59 static int forall_flag;
61 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
63 static int omp_workshare_flag;
65 /* Nonzero if we are processing a formal arglist. The corresponding function
66 resets the flag each time that it is read. */
67 static int formal_arg_flag = 0;
69 /* True if we are resolving a specification expression. */
70 static int specification_expr = 0;
72 /* The id of the last entry seen. */
73 static int current_entry_id;
75 /* We use bitmaps to determine if a branch target is valid. */
76 static bitmap_obstack labels_obstack;
79 gfc_is_formal_arg (void)
81 return formal_arg_flag;
84 /* Resolve types of formal argument lists. These have to be done early so that
85 the formal argument lists of module procedures can be copied to the
86 containing module before the individual procedures are resolved
87 individually. We also resolve argument lists of procedures in interface
88 blocks because they are self-contained scoping units.
90 Since a dummy argument cannot be a non-dummy procedure, the only
91 resort left for untyped names are the IMPLICIT types. */
94 resolve_formal_arglist (gfc_symbol *proc)
96 gfc_formal_arglist *f;
100 if (proc->result != NULL)
105 if (gfc_elemental (proc)
106 || sym->attr.pointer || sym->attr.allocatable
107 || (sym->as && sym->as->rank > 0))
108 proc->attr.always_explicit = 1;
112 for (f = proc->formal; f; f = f->next)
118 /* Alternate return placeholder. */
119 if (gfc_elemental (proc))
120 gfc_error ("Alternate return specifier in elemental subroutine "
121 "'%s' at %L is not allowed", proc->name,
123 if (proc->attr.function)
124 gfc_error ("Alternate return specifier in function "
125 "'%s' at %L is not allowed", proc->name,
130 if (sym->attr.if_source != IFSRC_UNKNOWN)
131 resolve_formal_arglist (sym);
133 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
135 if (gfc_pure (proc) && !gfc_pure (sym))
137 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
138 "also be PURE", sym->name, &sym->declared_at);
142 if (gfc_elemental (proc))
144 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
145 "procedure", &sym->declared_at);
149 if (sym->attr.function
150 && sym->ts.type == BT_UNKNOWN
151 && sym->attr.intrinsic)
153 gfc_intrinsic_sym *isym;
154 isym = gfc_find_function (sym->name);
155 if (isym == NULL || !isym->specific)
157 gfc_error ("Unable to find a specific INTRINSIC procedure "
158 "for the reference '%s' at %L", sym->name,
167 if (sym->ts.type == BT_UNKNOWN)
169 if (!sym->attr.function || sym->result == sym)
170 gfc_set_default_type (sym, 1, sym->ns);
173 gfc_resolve_array_spec (sym->as, 0);
175 /* We can't tell if an array with dimension (:) is assumed or deferred
176 shape until we know if it has the pointer or allocatable attributes.
178 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
179 && !(sym->attr.pointer || sym->attr.allocatable))
181 sym->as->type = AS_ASSUMED_SHAPE;
182 for (i = 0; i < sym->as->rank; i++)
183 sym->as->lower[i] = gfc_int_expr (1);
186 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
187 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
188 || sym->attr.optional)
189 proc->attr.always_explicit = 1;
191 /* If the flavor is unknown at this point, it has to be a variable.
192 A procedure specification would have already set the type. */
194 if (sym->attr.flavor == FL_UNKNOWN)
195 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
197 if (gfc_pure (proc) && !sym->attr.pointer
198 && sym->attr.flavor != FL_PROCEDURE)
200 if (proc->attr.function && sym->attr.intent != INTENT_IN)
201 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
202 "INTENT(IN)", sym->name, proc->name,
205 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
206 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
207 "have its INTENT specified", sym->name, proc->name,
211 if (gfc_elemental (proc))
215 gfc_error ("Argument '%s' of elemental procedure at %L must "
216 "be scalar", sym->name, &sym->declared_at);
220 if (sym->attr.pointer)
222 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
223 "have the POINTER attribute", sym->name,
229 /* Each dummy shall be specified to be scalar. */
230 if (proc->attr.proc == PROC_ST_FUNCTION)
234 gfc_error ("Argument '%s' of statement function at %L must "
235 "be scalar", sym->name, &sym->declared_at);
239 if (sym->ts.type == BT_CHARACTER)
241 gfc_charlen *cl = sym->ts.cl;
242 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
244 gfc_error ("Character-valued argument '%s' of statement "
245 "function at %L must have constant length",
246 sym->name, &sym->declared_at);
256 /* Work function called when searching for symbols that have argument lists
257 associated with them. */
260 find_arglists (gfc_symbol *sym)
262 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
265 resolve_formal_arglist (sym);
269 /* Given a namespace, resolve all formal argument lists within the namespace.
273 resolve_formal_arglists (gfc_namespace *ns)
278 gfc_traverse_ns (ns, find_arglists);
283 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
287 /* If this namespace is not a function, ignore it. */
288 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE))
291 /* Try to find out of what the return type is. */
292 if (sym->result->ts.type == BT_UNKNOWN)
294 t = gfc_set_default_type (sym->result, 0, ns);
296 if (t == FAILURE && !sym->result->attr.untyped)
298 if (sym->result == sym)
299 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
300 sym->name, &sym->declared_at);
302 gfc_error ("Result '%s' of contained function '%s' at %L has "
303 "no IMPLICIT type", sym->result->name, sym->name,
304 &sym->result->declared_at);
305 sym->result->attr.untyped = 1;
309 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
310 type, lists the only ways a character length value of * can be used:
311 dummy arguments of procedures, named constants, and function results
312 in external functions. Internal function results are not on that list;
313 ergo, not permitted. */
315 if (sym->result->ts.type == BT_CHARACTER)
317 gfc_charlen *cl = sym->result->ts.cl;
318 if (!cl || !cl->length)
319 gfc_error ("Character-valued internal function '%s' at %L must "
320 "not be assumed length", sym->name, &sym->declared_at);
325 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
326 introduce duplicates. */
329 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
331 gfc_formal_arglist *f, *new_arglist;
334 for (; new_args != NULL; new_args = new_args->next)
336 new_sym = new_args->sym;
337 /* See if this arg is already in the formal argument list. */
338 for (f = proc->formal; f; f = f->next)
340 if (new_sym == f->sym)
347 /* Add a new argument. Argument order is not important. */
348 new_arglist = gfc_get_formal_arglist ();
349 new_arglist->sym = new_sym;
350 new_arglist->next = proc->formal;
351 proc->formal = new_arglist;
356 /* Flag the arguments that are not present in all entries. */
359 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
361 gfc_formal_arglist *f, *head;
364 for (f = proc->formal; f; f = f->next)
369 for (new_args = head; new_args; new_args = new_args->next)
371 if (new_args->sym == f->sym)
378 f->sym->attr.not_always_present = 1;
383 /* Resolve alternate entry points. If a symbol has multiple entry points we
384 create a new master symbol for the main routine, and turn the existing
385 symbol into an entry point. */
388 resolve_entries (gfc_namespace *ns)
390 gfc_namespace *old_ns;
394 char name[GFC_MAX_SYMBOL_LEN + 1];
395 static int master_count = 0;
397 if (ns->proc_name == NULL)
400 /* No need to do anything if this procedure doesn't have alternate entry
405 /* We may already have resolved alternate entry points. */
406 if (ns->proc_name->attr.entry_master)
409 /* If this isn't a procedure something has gone horribly wrong. */
410 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
412 /* Remember the current namespace. */
413 old_ns = gfc_current_ns;
417 /* Add the main entry point to the list of entry points. */
418 el = gfc_get_entry_list ();
419 el->sym = ns->proc_name;
421 el->next = ns->entries;
423 ns->proc_name->attr.entry = 1;
425 /* If it is a module function, it needs to be in the right namespace
426 so that gfc_get_fake_result_decl can gather up the results. The
427 need for this arose in get_proc_name, where these beasts were
428 left in their own namespace, to keep prior references linked to
429 the entry declaration.*/
430 if (ns->proc_name->attr.function
431 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
434 /* Add an entry statement for it. */
441 /* Create a new symbol for the master function. */
442 /* Give the internal function a unique name (within this file).
443 Also include the function name so the user has some hope of figuring
444 out what is going on. */
445 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
446 master_count++, ns->proc_name->name);
447 gfc_get_ha_symbol (name, &proc);
448 gcc_assert (proc != NULL);
450 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
451 if (ns->proc_name->attr.subroutine)
452 gfc_add_subroutine (&proc->attr, proc->name, NULL);
456 gfc_typespec *ts, *fts;
457 gfc_array_spec *as, *fas;
458 gfc_add_function (&proc->attr, proc->name, NULL);
460 fas = ns->entries->sym->as;
461 fas = fas ? fas : ns->entries->sym->result->as;
462 fts = &ns->entries->sym->result->ts;
463 if (fts->type == BT_UNKNOWN)
464 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
465 for (el = ns->entries->next; el; el = el->next)
467 ts = &el->sym->result->ts;
469 as = as ? as : el->sym->result->as;
470 if (ts->type == BT_UNKNOWN)
471 ts = gfc_get_default_type (el->sym->result, NULL);
473 if (! gfc_compare_types (ts, fts)
474 || (el->sym->result->attr.dimension
475 != ns->entries->sym->result->attr.dimension)
476 || (el->sym->result->attr.pointer
477 != ns->entries->sym->result->attr.pointer))
480 else if (as && fas && gfc_compare_array_spec (as, fas) == 0)
481 gfc_error ("Procedure %s at %L has entries with mismatched "
482 "array specifications", ns->entries->sym->name,
483 &ns->entries->sym->declared_at);
488 sym = ns->entries->sym->result;
489 /* All result types the same. */
491 if (sym->attr.dimension)
492 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
493 if (sym->attr.pointer)
494 gfc_add_pointer (&proc->attr, NULL);
498 /* Otherwise the result will be passed through a union by
500 proc->attr.mixed_entry_master = 1;
501 for (el = ns->entries; el; el = el->next)
503 sym = el->sym->result;
504 if (sym->attr.dimension)
506 if (el == ns->entries)
507 gfc_error ("FUNCTION result %s can't be an array in "
508 "FUNCTION %s at %L", sym->name,
509 ns->entries->sym->name, &sym->declared_at);
511 gfc_error ("ENTRY result %s can't be an array in "
512 "FUNCTION %s at %L", sym->name,
513 ns->entries->sym->name, &sym->declared_at);
515 else if (sym->attr.pointer)
517 if (el == ns->entries)
518 gfc_error ("FUNCTION result %s can't be a POINTER in "
519 "FUNCTION %s at %L", sym->name,
520 ns->entries->sym->name, &sym->declared_at);
522 gfc_error ("ENTRY result %s can't be a POINTER in "
523 "FUNCTION %s at %L", sym->name,
524 ns->entries->sym->name, &sym->declared_at);
529 if (ts->type == BT_UNKNOWN)
530 ts = gfc_get_default_type (sym, NULL);
534 if (ts->kind == gfc_default_integer_kind)
538 if (ts->kind == gfc_default_real_kind
539 || ts->kind == gfc_default_double_kind)
543 if (ts->kind == gfc_default_complex_kind)
547 if (ts->kind == gfc_default_logical_kind)
551 /* We will issue error elsewhere. */
559 if (el == ns->entries)
560 gfc_error ("FUNCTION result %s can't be of type %s "
561 "in FUNCTION %s at %L", sym->name,
562 gfc_typename (ts), ns->entries->sym->name,
565 gfc_error ("ENTRY result %s can't be of type %s "
566 "in FUNCTION %s at %L", sym->name,
567 gfc_typename (ts), ns->entries->sym->name,
574 proc->attr.access = ACCESS_PRIVATE;
575 proc->attr.entry_master = 1;
577 /* Merge all the entry point arguments. */
578 for (el = ns->entries; el; el = el->next)
579 merge_argument_lists (proc, el->sym->formal);
581 /* Check the master formal arguments for any that are not
582 present in all entry points. */
583 for (el = ns->entries; el; el = el->next)
584 check_argument_lists (proc, el->sym->formal);
586 /* Use the master function for the function body. */
587 ns->proc_name = proc;
589 /* Finalize the new symbols. */
590 gfc_commit_symbols ();
592 /* Restore the original namespace. */
593 gfc_current_ns = old_ns;
597 /* Resolve common blocks. */
599 resolve_common_blocks (gfc_symtree *common_root)
601 gfc_symtree *symtree;
604 if (common_root == NULL)
607 for (symtree = common_root; symtree->left; symtree = symtree->left);
609 for (; symtree; symtree = symtree->right)
611 gfc_find_symbol (symtree->name, gfc_current_ns, 0, &sym);
615 if (sym->attr.flavor == FL_PARAMETER)
617 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
618 sym->name, &symtree->n.common->where,
622 if (sym->attr.intrinsic)
624 gfc_error ("COMMON block '%s' at %L is also an intrinsic "
625 "procedure", sym->name,
626 &symtree->n.common->where);
628 else if (sym->attr.result
629 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
631 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' "
632 "at %L that is also a function result", sym->name,
633 &symtree->n.common->where);
635 else if (sym->attr.flavor == FL_PROCEDURE
636 && sym->attr.proc != PROC_INTERNAL
637 && sym->attr.proc != PROC_ST_FUNCTION)
639 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' "
640 "at %L that is also a global procedure", sym->name,
641 &symtree->n.common->where);
647 /* Resolve contained function types. Because contained functions can call one
648 another, they have to be worked out before any of the contained procedures
651 The good news is that if a function doesn't already have a type, the only
652 way it can get one is through an IMPLICIT type or a RESULT variable, because
653 by definition contained functions are contained namespace they're contained
654 in, not in a sibling or parent namespace. */
657 resolve_contained_functions (gfc_namespace *ns)
659 gfc_namespace *child;
662 resolve_formal_arglists (ns);
664 for (child = ns->contained; child; child = child->sibling)
666 /* Resolve alternate entry points first. */
667 resolve_entries (child);
669 /* Then check function return types. */
670 resolve_contained_fntype (child->proc_name, child);
671 for (el = child->entries; el; el = el->next)
672 resolve_contained_fntype (el->sym, child);
677 /* Resolve all of the elements of a structure constructor and make sure that
678 the types are correct. */
681 resolve_structure_cons (gfc_expr *expr)
683 gfc_constructor *cons;
689 cons = expr->value.constructor;
690 /* A constructor may have references if it is the result of substituting a
691 parameter variable. In this case we just pull out the component we
694 comp = expr->ref->u.c.sym->components;
696 comp = expr->ts.derived->components;
698 for (; comp; comp = comp->next, cons = cons->next)
703 if (gfc_resolve_expr (cons->expr) == FAILURE)
709 if (cons->expr->expr_type != EXPR_NULL
710 && comp->as && comp->as->rank != cons->expr->rank
711 && (comp->allocatable || cons->expr->rank))
713 gfc_error ("The rank of the element in the derived type "
714 "constructor at %L does not match that of the "
715 "component (%d/%d)", &cons->expr->where,
716 cons->expr->rank, comp->as ? comp->as->rank : 0);
720 /* If we don't have the right type, try to convert it. */
722 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
725 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
726 gfc_error ("The element in the derived type constructor at %L, "
727 "for pointer component '%s', is %s but should be %s",
728 &cons->expr->where, comp->name,
729 gfc_basic_typename (cons->expr->ts.type),
730 gfc_basic_typename (comp->ts.type));
732 t = gfc_convert_type (cons->expr, &comp->ts, 1);
735 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
738 a = gfc_expr_attr (cons->expr);
740 if (!a.pointer && !a.target)
743 gfc_error ("The element in the derived type constructor at %L, "
744 "for pointer component '%s' should be a POINTER or "
745 "a TARGET", &cons->expr->where, comp->name);
753 /****************** Expression name resolution ******************/
755 /* Returns 0 if a symbol was not declared with a type or
756 attribute declaration statement, nonzero otherwise. */
759 was_declared (gfc_symbol *sym)
765 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
768 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
769 || a.optional || a.pointer || a.save || a.target || a.volatile_
770 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
777 /* Determine if a symbol is generic or not. */
780 generic_sym (gfc_symbol *sym)
784 if (sym->attr.generic ||
785 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
788 if (was_declared (sym) || sym->ns->parent == NULL)
791 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
793 return (s == NULL) ? 0 : generic_sym (s);
797 /* Determine if a symbol is specific or not. */
800 specific_sym (gfc_symbol *sym)
804 if (sym->attr.if_source == IFSRC_IFBODY
805 || sym->attr.proc == PROC_MODULE
806 || sym->attr.proc == PROC_INTERNAL
807 || sym->attr.proc == PROC_ST_FUNCTION
808 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
809 || sym->attr.external)
812 if (was_declared (sym) || sym->ns->parent == NULL)
815 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
817 return (s == NULL) ? 0 : specific_sym (s);
821 /* Figure out if the procedure is specific, generic or unknown. */
824 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
828 procedure_kind (gfc_symbol *sym)
830 if (generic_sym (sym))
831 return PTYPE_GENERIC;
833 if (specific_sym (sym))
834 return PTYPE_SPECIFIC;
836 return PTYPE_UNKNOWN;
839 /* Check references to assumed size arrays. The flag need_full_assumed_size
840 is nonzero when matching actual arguments. */
842 static int need_full_assumed_size = 0;
845 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
851 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
854 for (ref = e->ref; ref; ref = ref->next)
855 if (ref->type == REF_ARRAY)
856 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
857 last = (ref->u.ar.end[dim] == NULL)
858 && (ref->u.ar.type == DIMEN_ELEMENT);
862 gfc_error ("The upper bound in the last dimension must "
863 "appear in the reference to the assumed size "
864 "array '%s' at %L", sym->name, &e->where);
871 /* Look for bad assumed size array references in argument expressions
872 of elemental and array valued intrinsic procedures. Since this is
873 called from procedure resolution functions, it only recurses at
877 resolve_assumed_size_actual (gfc_expr *e)
882 switch (e->expr_type)
885 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
890 if (resolve_assumed_size_actual (e->value.op.op1)
891 || resolve_assumed_size_actual (e->value.op.op2))
902 /* Resolve an actual argument list. Most of the time, this is just
903 resolving the expressions in the list.
904 The exception is that we sometimes have to decide whether arguments
905 that look like procedure arguments are really simple variable
909 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype)
912 gfc_symtree *parent_st;
915 for (; arg; arg = arg->next)
920 /* Check the label is a valid branching target. */
923 if (arg->label->defined == ST_LABEL_UNKNOWN)
925 gfc_error ("Label %d referenced at %L is never defined",
926 arg->label->value, &arg->label->where);
933 if (e->ts.type != BT_PROCEDURE)
935 if (gfc_resolve_expr (e) != SUCCESS)
940 /* See if the expression node should really be a variable reference. */
942 sym = e->symtree->n.sym;
944 if (sym->attr.flavor == FL_PROCEDURE
945 || sym->attr.intrinsic
946 || sym->attr.external)
950 /* If a procedure is not already determined to be something else
951 check if it is intrinsic. */
952 if (!sym->attr.intrinsic
953 && !(sym->attr.external || sym->attr.use_assoc
954 || sym->attr.if_source == IFSRC_IFBODY)
955 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
956 sym->attr.intrinsic = 1;
958 if (sym->attr.proc == PROC_ST_FUNCTION)
960 gfc_error ("Statement function '%s' at %L is not allowed as an "
961 "actual argument", sym->name, &e->where);
964 actual_ok = gfc_intrinsic_actual_ok (sym->name,
965 sym->attr.subroutine);
966 if (sym->attr.intrinsic && actual_ok == 0)
968 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
969 "actual argument", sym->name, &e->where);
972 if (sym->attr.contained && !sym->attr.use_assoc
973 && sym->ns->proc_name->attr.flavor != FL_MODULE)
975 gfc_error ("Internal procedure '%s' is not allowed as an "
976 "actual argument at %L", sym->name, &e->where);
979 if (sym->attr.elemental && !sym->attr.intrinsic)
981 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
982 "allowed as an actual argument at %L", sym->name,
986 /* Check if a generic interface has a specific procedure
987 with the same name before emitting an error. */
988 if (sym->attr.generic)
991 for (p = sym->generic; p; p = p->next)
992 if (strcmp (sym->name, p->sym->name) == 0)
994 e->symtree = gfc_find_symtree
995 (p->sym->ns->sym_root, sym->name);
1000 if (p == NULL || e->symtree == NULL)
1001 gfc_error ("GENERIC non-INTRINSIC procedure '%s' is not "
1002 "allowed as an actual argument at %L", sym->name,
1006 /* If the symbol is the function that names the current (or
1007 parent) scope, then we really have a variable reference. */
1009 if (sym->attr.function && sym->result == sym
1010 && (sym->ns->proc_name == sym
1011 || (sym->ns->parent != NULL
1012 && sym->ns->parent->proc_name == sym)))
1015 /* If all else fails, see if we have a specific intrinsic. */
1016 if (sym->attr.function
1017 && sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1019 gfc_intrinsic_sym *isym;
1020 isym = gfc_find_function (sym->name);
1021 if (isym == NULL || !isym->specific)
1023 gfc_error ("Unable to find a specific INTRINSIC procedure "
1024 "for the reference '%s' at %L", sym->name,
1032 /* See if the name is a module procedure in a parent unit. */
1034 if (was_declared (sym) || sym->ns->parent == NULL)
1037 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1039 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1043 if (parent_st == NULL)
1046 sym = parent_st->n.sym;
1047 e->symtree = parent_st; /* Point to the right thing. */
1049 if (sym->attr.flavor == FL_PROCEDURE
1050 || sym->attr.intrinsic
1051 || sym->attr.external)
1057 e->expr_type = EXPR_VARIABLE;
1059 if (sym->as != NULL)
1061 e->rank = sym->as->rank;
1062 e->ref = gfc_get_ref ();
1063 e->ref->type = REF_ARRAY;
1064 e->ref->u.ar.type = AR_FULL;
1065 e->ref->u.ar.as = sym->as;
1068 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1069 primary.c (match_actual_arg). If above code determines that it
1070 is a variable instead, it needs to be resolved as it was not
1071 done at the beginning of this function. */
1072 if (gfc_resolve_expr (e) != SUCCESS)
1076 /* Check argument list functions %VAL, %LOC and %REF. There is
1077 nothing to do for %REF. */
1078 if (arg->name && arg->name[0] == '%')
1080 if (strncmp ("%VAL", arg->name, 4) == 0)
1082 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1084 gfc_error ("By-value argument at %L is not of numeric "
1091 gfc_error ("By-value argument at %L cannot be an array or "
1092 "an array section", &e->where);
1096 /* Intrinsics are still PROC_UNKNOWN here. However,
1097 since same file external procedures are not resolvable
1098 in gfortran, it is a good deal easier to leave them to
1100 if (ptype != PROC_UNKNOWN
1101 && ptype != PROC_DUMMY
1102 && ptype != PROC_EXTERNAL
1103 && ptype != PROC_MODULE)
1105 gfc_error ("By-value argument at %L is not allowed "
1106 "in this context", &e->where);
1111 /* Statement functions have already been excluded above. */
1112 else if (strncmp ("%LOC", arg->name, 4) == 0
1113 && e->ts.type == BT_PROCEDURE)
1115 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1117 gfc_error ("Passing internal procedure at %L by location "
1118 "not allowed", &e->where);
1129 /* Do the checks of the actual argument list that are specific to elemental
1130 procedures. If called with c == NULL, we have a function, otherwise if
1131 expr == NULL, we have a subroutine. */
1134 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1136 gfc_actual_arglist *arg0;
1137 gfc_actual_arglist *arg;
1138 gfc_symbol *esym = NULL;
1139 gfc_intrinsic_sym *isym = NULL;
1141 gfc_intrinsic_arg *iformal = NULL;
1142 gfc_formal_arglist *eformal = NULL;
1143 bool formal_optional = false;
1144 bool set_by_optional = false;
1148 /* Is this an elemental procedure? */
1149 if (expr && expr->value.function.actual != NULL)
1151 if (expr->value.function.esym != NULL
1152 && expr->value.function.esym->attr.elemental)
1154 arg0 = expr->value.function.actual;
1155 esym = expr->value.function.esym;
1157 else if (expr->value.function.isym != NULL
1158 && expr->value.function.isym->elemental)
1160 arg0 = expr->value.function.actual;
1161 isym = expr->value.function.isym;
1166 else if (c && c->ext.actual != NULL && c->symtree->n.sym->attr.elemental)
1168 arg0 = c->ext.actual;
1169 esym = c->symtree->n.sym;
1174 /* The rank of an elemental is the rank of its array argument(s). */
1175 for (arg = arg0; arg; arg = arg->next)
1177 if (arg->expr != NULL && arg->expr->rank > 0)
1179 rank = arg->expr->rank;
1180 if (arg->expr->expr_type == EXPR_VARIABLE
1181 && arg->expr->symtree->n.sym->attr.optional)
1182 set_by_optional = true;
1184 /* Function specific; set the result rank and shape. */
1188 if (!expr->shape && arg->expr->shape)
1190 expr->shape = gfc_get_shape (rank);
1191 for (i = 0; i < rank; i++)
1192 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1199 /* If it is an array, it shall not be supplied as an actual argument
1200 to an elemental procedure unless an array of the same rank is supplied
1201 as an actual argument corresponding to a nonoptional dummy argument of
1202 that elemental procedure(12.4.1.5). */
1203 formal_optional = false;
1205 iformal = isym->formal;
1207 eformal = esym->formal;
1209 for (arg = arg0; arg; arg = arg->next)
1213 if (eformal->sym && eformal->sym->attr.optional)
1214 formal_optional = true;
1215 eformal = eformal->next;
1217 else if (isym && iformal)
1219 if (iformal->optional)
1220 formal_optional = true;
1221 iformal = iformal->next;
1224 formal_optional = true;
1226 if (pedantic && arg->expr != NULL
1227 && arg->expr->expr_type == EXPR_VARIABLE
1228 && arg->expr->symtree->n.sym->attr.optional
1231 && (set_by_optional || arg->expr->rank != rank)
1232 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1234 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1235 "MISSING, it cannot be the actual argument of an "
1236 "ELEMENTAL procedure unless there is a non-optional "
1237 "argument with the same rank (12.4.1.5)",
1238 arg->expr->symtree->n.sym->name, &arg->expr->where);
1243 for (arg = arg0; arg; arg = arg->next)
1245 if (arg->expr == NULL || arg->expr->rank == 0)
1248 /* Being elemental, the last upper bound of an assumed size array
1249 argument must be present. */
1250 if (resolve_assumed_size_actual (arg->expr))
1256 /* Elemental subroutine array actual arguments must conform. */
1259 if (gfc_check_conformance ("elemental subroutine", arg->expr, e)
1271 /* Go through each actual argument in ACTUAL and see if it can be
1272 implemented as an inlined, non-copying intrinsic. FNSYM is the
1273 function being called, or NULL if not known. */
1276 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1278 gfc_actual_arglist *ap;
1281 for (ap = actual; ap; ap = ap->next)
1283 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1284 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1285 ap->expr->inline_noncopying_intrinsic = 1;
1289 /* This function does the checking of references to global procedures
1290 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1291 77 and 95 standards. It checks for a gsymbol for the name, making
1292 one if it does not already exist. If it already exists, then the
1293 reference being resolved must correspond to the type of gsymbol.
1294 Otherwise, the new symbol is equipped with the attributes of the
1295 reference. The corresponding code that is called in creating
1296 global entities is parse.c. */
1299 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1304 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1306 gsym = gfc_get_gsymbol (sym->name);
1308 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1309 global_used (gsym, where);
1311 if (gsym->type == GSYM_UNKNOWN)
1314 gsym->where = *where;
1321 /************* Function resolution *************/
1323 /* Resolve a function call known to be generic.
1324 Section 14.1.2.4.1. */
1327 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1331 if (sym->attr.generic)
1333 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1336 expr->value.function.name = s->name;
1337 expr->value.function.esym = s;
1339 if (s->ts.type != BT_UNKNOWN)
1341 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1342 expr->ts = s->result->ts;
1345 expr->rank = s->as->rank;
1346 else if (s->result != NULL && s->result->as != NULL)
1347 expr->rank = s->result->as->rank;
1352 /* TODO: Need to search for elemental references in generic
1356 if (sym->attr.intrinsic)
1357 return gfc_intrinsic_func_interface (expr, 0);
1364 resolve_generic_f (gfc_expr *expr)
1369 sym = expr->symtree->n.sym;
1373 m = resolve_generic_f0 (expr, sym);
1376 else if (m == MATCH_ERROR)
1380 if (sym->ns->parent == NULL)
1382 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1386 if (!generic_sym (sym))
1390 /* Last ditch attempt. See if the reference is to an intrinsic
1391 that possesses a matching interface. 14.1.2.4 */
1392 if (sym && !gfc_intrinsic_name (sym->name, 0))
1394 gfc_error ("There is no specific function for the generic '%s' at %L",
1395 expr->symtree->n.sym->name, &expr->where);
1399 m = gfc_intrinsic_func_interface (expr, 0);
1403 gfc_error ("Generic function '%s' at %L is not consistent with a "
1404 "specific intrinsic interface", expr->symtree->n.sym->name,
1411 /* Resolve a function call known to be specific. */
1414 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1418 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1420 if (sym->attr.dummy)
1422 sym->attr.proc = PROC_DUMMY;
1426 sym->attr.proc = PROC_EXTERNAL;
1430 if (sym->attr.proc == PROC_MODULE
1431 || sym->attr.proc == PROC_ST_FUNCTION
1432 || sym->attr.proc == PROC_INTERNAL)
1435 if (sym->attr.intrinsic)
1437 m = gfc_intrinsic_func_interface (expr, 1);
1441 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1442 "with an intrinsic", sym->name, &expr->where);
1450 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1453 expr->value.function.name = sym->name;
1454 expr->value.function.esym = sym;
1455 if (sym->as != NULL)
1456 expr->rank = sym->as->rank;
1463 resolve_specific_f (gfc_expr *expr)
1468 sym = expr->symtree->n.sym;
1472 m = resolve_specific_f0 (sym, expr);
1475 if (m == MATCH_ERROR)
1478 if (sym->ns->parent == NULL)
1481 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1487 gfc_error ("Unable to resolve the specific function '%s' at %L",
1488 expr->symtree->n.sym->name, &expr->where);
1494 /* Resolve a procedure call not known to be generic nor specific. */
1497 resolve_unknown_f (gfc_expr *expr)
1502 sym = expr->symtree->n.sym;
1504 if (sym->attr.dummy)
1506 sym->attr.proc = PROC_DUMMY;
1507 expr->value.function.name = sym->name;
1511 /* See if we have an intrinsic function reference. */
1513 if (gfc_intrinsic_name (sym->name, 0))
1515 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1520 /* The reference is to an external name. */
1522 sym->attr.proc = PROC_EXTERNAL;
1523 expr->value.function.name = sym->name;
1524 expr->value.function.esym = expr->symtree->n.sym;
1526 if (sym->as != NULL)
1527 expr->rank = sym->as->rank;
1529 /* Type of the expression is either the type of the symbol or the
1530 default type of the symbol. */
1533 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1535 if (sym->ts.type != BT_UNKNOWN)
1539 ts = gfc_get_default_type (sym, sym->ns);
1541 if (ts->type == BT_UNKNOWN)
1543 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1544 sym->name, &expr->where);
1555 /* Return true, if the symbol is an external procedure. */
1557 is_external_proc (gfc_symbol *sym)
1559 if (!sym->attr.dummy && !sym->attr.contained
1560 && !(sym->attr.intrinsic
1561 || gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1562 && sym->attr.proc != PROC_ST_FUNCTION
1563 && !sym->attr.use_assoc
1571 /* Figure out if a function reference is pure or not. Also set the name
1572 of the function for a potential error message. Return nonzero if the
1573 function is PURE, zero if not. */
1576 pure_function (gfc_expr *e, const char **name)
1582 if (e->symtree != NULL
1583 && e->symtree->n.sym != NULL
1584 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1587 if (e->value.function.esym)
1589 pure = gfc_pure (e->value.function.esym);
1590 *name = e->value.function.esym->name;
1592 else if (e->value.function.isym)
1594 pure = e->value.function.isym->pure
1595 || e->value.function.isym->elemental;
1596 *name = e->value.function.isym->name;
1600 /* Implicit functions are not pure. */
1602 *name = e->value.function.name;
1610 is_scalar_expr_ptr (gfc_expr *expr)
1612 try retval = SUCCESS;
1617 /* See if we have a gfc_ref, which means we have a substring, array
1618 reference, or a component. */
1619 if (expr->ref != NULL)
1622 while (ref->next != NULL)
1628 if (ref->u.ss.length != NULL
1629 && ref->u.ss.length->length != NULL
1631 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1633 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1635 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
1636 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
1637 if (end - start + 1 != 1)
1644 if (ref->u.ar.type == AR_ELEMENT)
1646 else if (ref->u.ar.type == AR_FULL)
1648 /* The user can give a full array if the array is of size 1. */
1649 if (ref->u.ar.as != NULL
1650 && ref->u.ar.as->rank == 1
1651 && ref->u.ar.as->type == AS_EXPLICIT
1652 && ref->u.ar.as->lower[0] != NULL
1653 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
1654 && ref->u.ar.as->upper[0] != NULL
1655 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
1657 /* If we have a character string, we need to check if
1658 its length is one. */
1659 if (expr->ts.type == BT_CHARACTER)
1661 if (expr->ts.cl == NULL
1662 || expr->ts.cl->length == NULL
1663 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
1669 /* We have constant lower and upper bounds. If the
1670 difference between is 1, it can be considered a
1672 start = (int) mpz_get_si
1673 (ref->u.ar.as->lower[0]->value.integer);
1674 end = (int) mpz_get_si
1675 (ref->u.ar.as->upper[0]->value.integer);
1676 if (end - start + 1 != 1)
1691 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
1693 /* Character string. Make sure it's of length 1. */
1694 if (expr->ts.cl == NULL
1695 || expr->ts.cl->length == NULL
1696 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
1699 else if (expr->rank != 0)
1706 /* Match one of the iso_c_binding functions (c_associated or c_loc)
1707 and, in the case of c_associated, set the binding label based on
1711 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
1712 gfc_symbol **new_sym)
1714 char name[GFC_MAX_SYMBOL_LEN + 1];
1715 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
1716 int optional_arg = 0;
1717 try retval = SUCCESS;
1718 gfc_symbol *args_sym;
1720 if (args->expr->expr_type == EXPR_CONSTANT
1721 || args->expr->expr_type == EXPR_OP
1722 || args->expr->expr_type == EXPR_NULL)
1724 gfc_error ("Argument to '%s' at %L is not a variable",
1725 sym->name, &(args->expr->where));
1729 args_sym = args->expr->symtree->n.sym;
1731 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
1733 /* If the user gave two args then they are providing something for
1734 the optional arg (the second cptr). Therefore, set the name and
1735 binding label to the c_associated for two cptrs. Otherwise,
1736 set c_associated to expect one cptr. */
1740 sprintf (name, "%s_2", sym->name);
1741 sprintf (binding_label, "%s_2", sym->binding_label);
1747 sprintf (name, "%s_1", sym->name);
1748 sprintf (binding_label, "%s_1", sym->binding_label);
1752 /* Get a new symbol for the version of c_associated that
1754 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
1756 else if (sym->intmod_sym_id == ISOCBINDING_LOC
1757 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1759 sprintf (name, "%s", sym->name);
1760 sprintf (binding_label, "%s", sym->binding_label);
1762 /* Error check the call. */
1763 if (args->next != NULL)
1765 gfc_error_now ("More actual than formal arguments in '%s' "
1766 "call at %L", name, &(args->expr->where));
1769 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
1771 /* Make sure we have either the target or pointer attribute. */
1772 if (!(args->expr->symtree->n.sym->attr.target)
1773 && !(args->expr->symtree->n.sym->attr.pointer))
1775 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
1776 "a TARGET or an associated pointer",
1777 args->expr->symtree->n.sym->name,
1778 sym->name, &(args->expr->where));
1782 /* See if we have interoperable type and type param. */
1783 if (verify_c_interop (&(args->expr->symtree->n.sym->ts),
1784 args->expr->symtree->n.sym->name,
1785 &(args->expr->where)) == SUCCESS
1786 || gfc_check_any_c_kind (&(args_sym->ts)) == SUCCESS)
1788 if (args_sym->attr.target == 1)
1790 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
1791 has the target attribute and is interoperable. */
1792 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
1793 allocatable variable that has the TARGET attribute and
1794 is not an array of zero size. */
1795 if (args_sym->attr.allocatable == 1)
1797 if (args_sym->attr.dimension != 0
1798 && (args_sym->as && args_sym->as->rank == 0))
1800 gfc_error_now ("Allocatable variable '%s' used as a "
1801 "parameter to '%s' at %L must not be "
1802 "an array of zero size",
1803 args_sym->name, sym->name,
1804 &(args->expr->where));
1810 /* Make sure it's not a character string. Arrays of
1811 any type should be ok if the variable is of a C
1812 interoperable type. */
1813 if (args_sym->ts.type == BT_CHARACTER
1814 && is_scalar_expr_ptr (args->expr) != SUCCESS)
1816 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
1817 "%L must have a length of 1",
1818 args_sym->name, sym->name,
1819 &(args->expr->where));
1824 else if (args_sym->attr.pointer == 1
1825 && is_scalar_expr_ptr (args->expr) != SUCCESS)
1827 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
1829 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
1830 "associated scalar POINTER", args_sym->name,
1831 sym->name, &(args->expr->where));
1837 /* The parameter is not required to be C interoperable. If it
1838 is not C interoperable, it must be a nonpolymorphic scalar
1839 with no length type parameters. It still must have either
1840 the pointer or target attribute, and it can be
1841 allocatable (but must be allocated when c_loc is called). */
1842 if (args_sym->attr.dimension != 0
1843 && is_scalar_expr_ptr (args->expr) != SUCCESS)
1845 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
1846 "scalar", args_sym->name, sym->name,
1847 &(args->expr->where));
1850 else if (args_sym->ts.type == BT_CHARACTER
1851 && args_sym->ts.cl != NULL)
1853 gfc_error_now ("CHARACTER parameter '%s' to '%s' at %L "
1854 "cannot have a length type parameter",
1855 args_sym->name, sym->name,
1856 &(args->expr->where));
1861 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1863 if (args->expr->symtree->n.sym->attr.flavor != FL_PROCEDURE)
1865 /* TODO: Update this error message to allow for procedure
1866 pointers once they are implemented. */
1867 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
1869 args->expr->symtree->n.sym->name, sym->name,
1870 &(args->expr->where));
1873 else if (args->expr->symtree->n.sym->attr.is_c_interop != 1)
1875 gfc_error_now ("Parameter '%s' to '%s' at %L must be C "
1877 args->expr->symtree->n.sym->name, sym->name,
1878 &(args->expr->where));
1883 /* for c_loc/c_funloc, the new symbol is the same as the old one */
1888 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
1889 "iso_c_binding function: '%s'!\n", sym->name);
1896 /* Resolve a function call, which means resolving the arguments, then figuring
1897 out which entity the name refers to. */
1898 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1899 to INTENT(OUT) or INTENT(INOUT). */
1902 resolve_function (gfc_expr *expr)
1904 gfc_actual_arglist *arg;
1909 procedure_type p = PROC_INTRINSIC;
1913 sym = expr->symtree->n.sym;
1915 if (sym && sym->attr.flavor == FL_VARIABLE)
1917 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
1921 /* If the procedure is external, check for usage. */
1922 if (sym && is_external_proc (sym))
1923 resolve_global_procedure (sym, &expr->where, 0);
1925 /* Switch off assumed size checking and do this again for certain kinds
1926 of procedure, once the procedure itself is resolved. */
1927 need_full_assumed_size++;
1929 if (expr->symtree && expr->symtree->n.sym)
1930 p = expr->symtree->n.sym->attr.proc;
1932 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
1935 /* Need to setup the call to the correct c_associated, depending on
1936 the number of cptrs to user gives to compare. */
1937 if (sym && sym->attr.is_iso_c == 1)
1939 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
1943 /* Get the symtree for the new symbol (resolved func).
1944 the old one will be freed later, when it's no longer used. */
1945 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
1948 /* Resume assumed_size checking. */
1949 need_full_assumed_size--;
1951 if (sym && sym->ts.type == BT_CHARACTER
1953 && sym->ts.cl->length == NULL
1955 && expr->value.function.esym == NULL
1956 && !sym->attr.contained)
1958 /* Internal procedures are taken care of in resolve_contained_fntype. */
1959 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1960 "be used at %L since it is not a dummy argument",
1961 sym->name, &expr->where);
1965 /* See if function is already resolved. */
1967 if (expr->value.function.name != NULL)
1969 if (expr->ts.type == BT_UNKNOWN)
1975 /* Apply the rules of section 14.1.2. */
1977 switch (procedure_kind (sym))
1980 t = resolve_generic_f (expr);
1983 case PTYPE_SPECIFIC:
1984 t = resolve_specific_f (expr);
1988 t = resolve_unknown_f (expr);
1992 gfc_internal_error ("resolve_function(): bad function type");
1996 /* If the expression is still a function (it might have simplified),
1997 then we check to see if we are calling an elemental function. */
1999 if (expr->expr_type != EXPR_FUNCTION)
2002 temp = need_full_assumed_size;
2003 need_full_assumed_size = 0;
2005 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2008 if (omp_workshare_flag
2009 && expr->value.function.esym
2010 && ! gfc_elemental (expr->value.function.esym))
2012 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2013 "in WORKSHARE construct", expr->value.function.esym->name,
2018 #define GENERIC_ID expr->value.function.isym->id
2019 else if (expr->value.function.actual != NULL
2020 && expr->value.function.isym != NULL
2021 && GENERIC_ID != GFC_ISYM_LBOUND
2022 && GENERIC_ID != GFC_ISYM_LEN
2023 && GENERIC_ID != GFC_ISYM_LOC
2024 && GENERIC_ID != GFC_ISYM_PRESENT)
2026 /* Array intrinsics must also have the last upper bound of an
2027 assumed size array argument. UBOUND and SIZE have to be
2028 excluded from the check if the second argument is anything
2031 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
2032 || GENERIC_ID == GFC_ISYM_SIZE;
2034 for (arg = expr->value.function.actual; arg; arg = arg->next)
2036 if (inquiry && arg->next != NULL && arg->next->expr)
2038 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2041 if ((int)mpz_get_si (arg->next->expr->value.integer)
2046 if (arg->expr != NULL
2047 && arg->expr->rank > 0
2048 && resolve_assumed_size_actual (arg->expr))
2054 need_full_assumed_size = temp;
2057 if (!pure_function (expr, &name) && name)
2061 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2062 "FORALL %s", name, &expr->where,
2063 forall_flag == 2 ? "mask" : "block");
2066 else if (gfc_pure (NULL))
2068 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2069 "procedure within a PURE procedure", name, &expr->where);
2074 /* Functions without the RECURSIVE attribution are not allowed to
2075 * call themselves. */
2076 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2078 gfc_symbol *esym, *proc;
2079 esym = expr->value.function.esym;
2080 proc = gfc_current_ns->proc_name;
2083 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
2084 "RECURSIVE", name, &expr->where);
2088 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
2089 && esym->ns->entries->sym == proc->ns->entries->sym)
2091 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
2092 "'%s' is not declared as RECURSIVE",
2093 esym->name, &expr->where, esym->ns->entries->sym->name);
2098 /* Character lengths of use associated functions may contains references to
2099 symbols not referenced from the current program unit otherwise. Make sure
2100 those symbols are marked as referenced. */
2102 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2103 && expr->value.function.esym->attr.use_assoc)
2105 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2109 find_noncopying_intrinsics (expr->value.function.esym,
2110 expr->value.function.actual);
2112 /* Make sure that the expression has a typespec that works. */
2113 if (expr->ts.type == BT_UNKNOWN)
2115 if (expr->symtree->n.sym->result
2116 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2117 expr->ts = expr->symtree->n.sym->result->ts;
2124 /************* Subroutine resolution *************/
2127 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2133 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2134 sym->name, &c->loc);
2135 else if (gfc_pure (NULL))
2136 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2142 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2146 if (sym->attr.generic)
2148 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2151 c->resolved_sym = s;
2152 pure_subroutine (c, s);
2156 /* TODO: Need to search for elemental references in generic interface. */
2159 if (sym->attr.intrinsic)
2160 return gfc_intrinsic_sub_interface (c, 0);
2167 resolve_generic_s (gfc_code *c)
2172 sym = c->symtree->n.sym;
2176 m = resolve_generic_s0 (c, sym);
2179 else if (m == MATCH_ERROR)
2183 if (sym->ns->parent == NULL)
2185 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2189 if (!generic_sym (sym))
2193 /* Last ditch attempt. See if the reference is to an intrinsic
2194 that possesses a matching interface. 14.1.2.4 */
2195 sym = c->symtree->n.sym;
2197 if (!gfc_intrinsic_name (sym->name, 1))
2199 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2200 sym->name, &c->loc);
2204 m = gfc_intrinsic_sub_interface (c, 0);
2208 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2209 "intrinsic subroutine interface", sym->name, &c->loc);
2215 /* Set the name and binding label of the subroutine symbol in the call
2216 expression represented by 'c' to include the type and kind of the
2217 second parameter. This function is for resolving the appropriate
2218 version of c_f_pointer() and c_f_procpointer(). For example, a
2219 call to c_f_pointer() for a default integer pointer could have a
2220 name of c_f_pointer_i4. If no second arg exists, which is an error
2221 for these two functions, it defaults to the generic symbol's name
2222 and binding label. */
2225 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2226 char *name, char *binding_label)
2228 gfc_expr *arg = NULL;
2232 /* The second arg of c_f_pointer and c_f_procpointer determines
2233 the type and kind for the procedure name. */
2234 arg = c->ext.actual->next->expr;
2238 /* Set up the name to have the given symbol's name,
2239 plus the type and kind. */
2240 /* a derived type is marked with the type letter 'u' */
2241 if (arg->ts.type == BT_DERIVED)
2244 kind = 0; /* set the kind as 0 for now */
2248 type = gfc_type_letter (arg->ts.type);
2249 kind = arg->ts.kind;
2251 sprintf (name, "%s_%c%d", sym->name, type, kind);
2252 /* Set up the binding label as the given symbol's label plus
2253 the type and kind. */
2254 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2258 /* If the second arg is missing, set the name and label as
2259 was, cause it should at least be found, and the missing
2260 arg error will be caught by compare_parameters(). */
2261 sprintf (name, "%s", sym->name);
2262 sprintf (binding_label, "%s", sym->binding_label);
2269 /* Resolve a generic version of the iso_c_binding procedure given
2270 (sym) to the specific one based on the type and kind of the
2271 argument(s). Currently, this function resolves c_f_pointer() and
2272 c_f_procpointer based on the type and kind of the second argument
2273 (FPTR). Other iso_c_binding procedures aren't specially handled.
2274 Upon successfully exiting, c->resolved_sym will hold the resolved
2275 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2279 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2281 gfc_symbol *new_sym;
2282 /* this is fine, since we know the names won't use the max */
2283 char name[GFC_MAX_SYMBOL_LEN + 1];
2284 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2285 /* default to success; will override if find error */
2286 match m = MATCH_YES;
2287 gfc_symbol *tmp_sym;
2289 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2290 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2292 set_name_and_label (c, sym, name, binding_label);
2294 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2296 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2298 /* Make sure we got a third arg. The type/rank of it will
2299 be checked later if it's there (gfc_procedure_use()). */
2300 if (c->ext.actual->next->expr->rank != 0 &&
2301 c->ext.actual->next->next == NULL)
2304 gfc_error ("Missing SHAPE parameter for call to %s "
2305 "at %L", sym->name, &(c->loc));
2307 /* Make sure the param is a POINTER. No need to make sure
2308 it does not have INTENT(IN) since it is a POINTER. */
2309 tmp_sym = c->ext.actual->next->expr->symtree->n.sym;
2310 if (tmp_sym != NULL && tmp_sym->attr.pointer != 1)
2312 gfc_error ("Argument '%s' to '%s' at %L "
2313 "must have the POINTER attribute",
2314 tmp_sym->name, sym->name, &(c->loc));
2320 if (m != MATCH_ERROR)
2322 /* the 1 means to add the optional arg to formal list */
2323 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2325 /* for error reporting, say it's declared where the original was */
2326 new_sym->declared_at = sym->declared_at;
2329 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2331 /* TODO: Figure out if this is even reacable; this part of the
2332 conditional may not be necessary. */
2334 if (c->ext.actual->next == NULL)
2336 /* The user did not give two args, so resolve to the version
2337 of c_associated expecting one arg. */
2339 /* get rid of the second arg */
2340 /* TODO!! Should free up the memory here! */
2341 sym->formal->next = NULL;
2349 sprintf (name, "%s_%d", sym->name, num_args);
2350 sprintf (binding_label, "%s_%d", sym->binding_label, num_args);
2351 sym->name = gfc_get_string (name);
2352 strcpy (sym->binding_label, binding_label);
2356 /* no differences for c_loc or c_funloc */
2360 /* set the resolved symbol */
2361 if (m != MATCH_ERROR)
2363 gfc_procedure_use (new_sym, &c->ext.actual, &c->loc);
2364 c->resolved_sym = new_sym;
2367 c->resolved_sym = sym;
2373 /* Resolve a subroutine call known to be specific. */
2376 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2380 if(sym->attr.is_iso_c)
2382 m = gfc_iso_c_sub_interface (c,sym);
2386 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2388 if (sym->attr.dummy)
2390 sym->attr.proc = PROC_DUMMY;
2394 sym->attr.proc = PROC_EXTERNAL;
2398 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2401 if (sym->attr.intrinsic)
2403 m = gfc_intrinsic_sub_interface (c, 1);
2407 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2408 "with an intrinsic", sym->name, &c->loc);
2416 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2418 c->resolved_sym = sym;
2419 pure_subroutine (c, sym);
2426 resolve_specific_s (gfc_code *c)
2431 sym = c->symtree->n.sym;
2435 m = resolve_specific_s0 (c, sym);
2438 if (m == MATCH_ERROR)
2441 if (sym->ns->parent == NULL)
2444 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2450 sym = c->symtree->n.sym;
2451 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2452 sym->name, &c->loc);
2458 /* Resolve a subroutine call not known to be generic nor specific. */
2461 resolve_unknown_s (gfc_code *c)
2465 sym = c->symtree->n.sym;
2467 if (sym->attr.dummy)
2469 sym->attr.proc = PROC_DUMMY;
2473 /* See if we have an intrinsic function reference. */
2475 if (gfc_intrinsic_name (sym->name, 1))
2477 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2482 /* The reference is to an external name. */
2485 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2487 c->resolved_sym = sym;
2489 pure_subroutine (c, sym);
2495 /* Resolve a subroutine call. Although it was tempting to use the same code
2496 for functions, subroutines and functions are stored differently and this
2497 makes things awkward. */
2500 resolve_call (gfc_code *c)
2503 procedure_type ptype = PROC_INTRINSIC;
2505 if (c->symtree && c->symtree->n.sym
2506 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
2508 gfc_error ("'%s' at %L has a type, which is not consistent with "
2509 "the CALL at %L", c->symtree->n.sym->name,
2510 &c->symtree->n.sym->declared_at, &c->loc);
2514 /* If external, check for usage. */
2515 if (c->symtree && is_external_proc (c->symtree->n.sym))
2516 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
2518 /* Subroutines without the RECURSIVE attribution are not allowed to
2519 * call themselves. */
2520 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
2522 gfc_symbol *csym, *proc;
2523 csym = c->symtree->n.sym;
2524 proc = gfc_current_ns->proc_name;
2527 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
2528 "RECURSIVE", csym->name, &c->loc);
2532 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
2533 && csym->ns->entries->sym == proc->ns->entries->sym)
2535 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
2536 "'%s' is not declared as RECURSIVE",
2537 csym->name, &c->loc, csym->ns->entries->sym->name);
2542 /* Switch off assumed size checking and do this again for certain kinds
2543 of procedure, once the procedure itself is resolved. */
2544 need_full_assumed_size++;
2546 if (c->symtree && c->symtree->n.sym)
2547 ptype = c->symtree->n.sym->attr.proc;
2549 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
2552 /* Resume assumed_size checking. */
2553 need_full_assumed_size--;
2556 if (c->resolved_sym == NULL)
2557 switch (procedure_kind (c->symtree->n.sym))
2560 t = resolve_generic_s (c);
2563 case PTYPE_SPECIFIC:
2564 t = resolve_specific_s (c);
2568 t = resolve_unknown_s (c);
2572 gfc_internal_error ("resolve_subroutine(): bad function type");
2575 /* Some checks of elemental subroutine actual arguments. */
2576 if (resolve_elemental_actual (NULL, c) == FAILURE)
2580 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2585 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2586 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2587 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2588 if their shapes do not match. If either op1->shape or op2->shape is
2589 NULL, return SUCCESS. */
2592 compare_shapes (gfc_expr *op1, gfc_expr *op2)
2599 if (op1->shape != NULL && op2->shape != NULL)
2601 for (i = 0; i < op1->rank; i++)
2603 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2605 gfc_error ("Shapes for operands at %L and %L are not conformable",
2606 &op1->where, &op2->where);
2617 /* Resolve an operator expression node. This can involve replacing the
2618 operation with a user defined function call. */
2621 resolve_operator (gfc_expr *e)
2623 gfc_expr *op1, *op2;
2625 bool dual_locus_error;
2628 /* Resolve all subnodes-- give them types. */
2630 switch (e->value.op.operator)
2633 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2636 /* Fall through... */
2639 case INTRINSIC_UPLUS:
2640 case INTRINSIC_UMINUS:
2641 case INTRINSIC_PARENTHESES:
2642 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2647 /* Typecheck the new node. */
2649 op1 = e->value.op.op1;
2650 op2 = e->value.op.op2;
2651 dual_locus_error = false;
2653 if ((op1 && op1->expr_type == EXPR_NULL)
2654 || (op2 && op2->expr_type == EXPR_NULL))
2656 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
2660 switch (e->value.op.operator)
2662 case INTRINSIC_UPLUS:
2663 case INTRINSIC_UMINUS:
2664 if (op1->ts.type == BT_INTEGER
2665 || op1->ts.type == BT_REAL
2666 || op1->ts.type == BT_COMPLEX)
2672 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2673 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2676 case INTRINSIC_PLUS:
2677 case INTRINSIC_MINUS:
2678 case INTRINSIC_TIMES:
2679 case INTRINSIC_DIVIDE:
2680 case INTRINSIC_POWER:
2681 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2683 gfc_type_convert_binary (e);
2688 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2689 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2690 gfc_typename (&op2->ts));
2693 case INTRINSIC_CONCAT:
2694 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2696 e->ts.type = BT_CHARACTER;
2697 e->ts.kind = op1->ts.kind;
2702 _("Operands of string concatenation operator at %%L are %s/%s"),
2703 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2709 case INTRINSIC_NEQV:
2710 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2712 e->ts.type = BT_LOGICAL;
2713 e->ts.kind = gfc_kind_max (op1, op2);
2714 if (op1->ts.kind < e->ts.kind)
2715 gfc_convert_type (op1, &e->ts, 2);
2716 else if (op2->ts.kind < e->ts.kind)
2717 gfc_convert_type (op2, &e->ts, 2);
2721 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2722 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2723 gfc_typename (&op2->ts));
2728 if (op1->ts.type == BT_LOGICAL)
2730 e->ts.type = BT_LOGICAL;
2731 e->ts.kind = op1->ts.kind;
2735 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
2736 gfc_typename (&op1->ts));
2740 case INTRINSIC_GT_OS:
2742 case INTRINSIC_GE_OS:
2744 case INTRINSIC_LT_OS:
2746 case INTRINSIC_LE_OS:
2747 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2749 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
2753 /* Fall through... */
2756 case INTRINSIC_EQ_OS:
2758 case INTRINSIC_NE_OS:
2759 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2761 e->ts.type = BT_LOGICAL;
2762 e->ts.kind = gfc_default_logical_kind;
2766 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2768 gfc_type_convert_binary (e);
2770 e->ts.type = BT_LOGICAL;
2771 e->ts.kind = gfc_default_logical_kind;
2775 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2777 _("Logicals at %%L must be compared with %s instead of %s"),
2778 e->value.op.operator == INTRINSIC_EQ ? ".eqv." : ".neqv.",
2779 gfc_op2string (e->value.op.operator));
2782 _("Operands of comparison operator '%s' at %%L are %s/%s"),
2783 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2784 gfc_typename (&op2->ts));
2788 case INTRINSIC_USER:
2789 if (e->value.op.uop->operator == NULL)
2790 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
2791 else if (op2 == NULL)
2792 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
2793 e->value.op.uop->name, gfc_typename (&op1->ts));
2795 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
2796 e->value.op.uop->name, gfc_typename (&op1->ts),
2797 gfc_typename (&op2->ts));
2801 case INTRINSIC_PARENTHESES:
2805 gfc_internal_error ("resolve_operator(): Bad intrinsic");
2808 /* Deal with arrayness of an operand through an operator. */
2812 switch (e->value.op.operator)
2814 case INTRINSIC_PLUS:
2815 case INTRINSIC_MINUS:
2816 case INTRINSIC_TIMES:
2817 case INTRINSIC_DIVIDE:
2818 case INTRINSIC_POWER:
2819 case INTRINSIC_CONCAT:
2823 case INTRINSIC_NEQV:
2825 case INTRINSIC_EQ_OS:
2827 case INTRINSIC_NE_OS:
2829 case INTRINSIC_GT_OS:
2831 case INTRINSIC_GE_OS:
2833 case INTRINSIC_LT_OS:
2835 case INTRINSIC_LE_OS:
2837 if (op1->rank == 0 && op2->rank == 0)
2840 if (op1->rank == 0 && op2->rank != 0)
2842 e->rank = op2->rank;
2844 if (e->shape == NULL)
2845 e->shape = gfc_copy_shape (op2->shape, op2->rank);
2848 if (op1->rank != 0 && op2->rank == 0)
2850 e->rank = op1->rank;
2852 if (e->shape == NULL)
2853 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2856 if (op1->rank != 0 && op2->rank != 0)
2858 if (op1->rank == op2->rank)
2860 e->rank = op1->rank;
2861 if (e->shape == NULL)
2863 t = compare_shapes(op1, op2);
2867 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2872 /* Allow higher level expressions to work. */
2875 /* Try user-defined operators, and otherwise throw an error. */
2876 dual_locus_error = true;
2878 _("Inconsistent ranks for operator at %%L and %%L"));
2886 case INTRINSIC_UPLUS:
2887 case INTRINSIC_UMINUS:
2888 case INTRINSIC_PARENTHESES:
2889 e->rank = op1->rank;
2891 if (e->shape == NULL)
2892 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2894 /* Simply copy arrayness attribute */
2901 /* Attempt to simplify the expression. */
2904 t = gfc_simplify_expr (e, 0);
2905 /* Some calls do not succeed in simplification and return FAILURE
2906 even though there is no error; eg. variable references to
2907 PARAMETER arrays. */
2908 if (!gfc_is_constant_expr (e))
2915 if (gfc_extend_expr (e) == SUCCESS)
2918 if (dual_locus_error)
2919 gfc_error (msg, &op1->where, &op2->where);
2921 gfc_error (msg, &e->where);
2927 /************** Array resolution subroutines **************/
2930 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
2933 /* Compare two integer expressions. */
2936 compare_bound (gfc_expr *a, gfc_expr *b)
2940 if (a == NULL || a->expr_type != EXPR_CONSTANT
2941 || b == NULL || b->expr_type != EXPR_CONSTANT)
2944 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
2945 gfc_internal_error ("compare_bound(): Bad expression");
2947 i = mpz_cmp (a->value.integer, b->value.integer);
2957 /* Compare an integer expression with an integer. */
2960 compare_bound_int (gfc_expr *a, int b)
2964 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2967 if (a->ts.type != BT_INTEGER)
2968 gfc_internal_error ("compare_bound_int(): Bad expression");
2970 i = mpz_cmp_si (a->value.integer, b);
2980 /* Compare an integer expression with a mpz_t. */
2983 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
2987 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2990 if (a->ts.type != BT_INTEGER)
2991 gfc_internal_error ("compare_bound_int(): Bad expression");
2993 i = mpz_cmp (a->value.integer, b);
3003 /* Compute the last value of a sequence given by a triplet.
3004 Return 0 if it wasn't able to compute the last value, or if the
3005 sequence if empty, and 1 otherwise. */
3008 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3009 gfc_expr *stride, mpz_t last)
3013 if (start == NULL || start->expr_type != EXPR_CONSTANT
3014 || end == NULL || end->expr_type != EXPR_CONSTANT
3015 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3018 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3019 || (stride != NULL && stride->ts.type != BT_INTEGER))
3022 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3024 if (compare_bound (start, end) == CMP_GT)
3026 mpz_set (last, end->value.integer);
3030 if (compare_bound_int (stride, 0) == CMP_GT)
3032 /* Stride is positive */
3033 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3038 /* Stride is negative */
3039 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3044 mpz_sub (rem, end->value.integer, start->value.integer);
3045 mpz_tdiv_r (rem, rem, stride->value.integer);
3046 mpz_sub (last, end->value.integer, rem);
3053 /* Compare a single dimension of an array reference to the array
3057 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3061 /* Given start, end and stride values, calculate the minimum and
3062 maximum referenced indexes. */
3070 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3072 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3079 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3080 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3082 comparison comp_start_end = compare_bound (AR_START, AR_END);
3084 /* Check for zero stride, which is not allowed. */
3085 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3087 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3091 /* if start == len || (stride > 0 && start < len)
3092 || (stride < 0 && start > len),
3093 then the array section contains at least one element. In this
3094 case, there is an out-of-bounds access if
3095 (start < lower || start > upper). */
3096 if (compare_bound (AR_START, AR_END) == CMP_EQ
3097 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3098 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3099 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3100 && comp_start_end == CMP_GT))
3102 if (compare_bound (AR_START, as->lower[i]) == CMP_LT
3103 || compare_bound (AR_START, as->upper[i]) == CMP_GT)
3107 /* If we can compute the highest index of the array section,
3108 then it also has to be between lower and upper. */
3109 mpz_init (last_value);
3110 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3113 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT
3114 || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3116 mpz_clear (last_value);
3120 mpz_clear (last_value);
3128 gfc_internal_error ("check_dimension(): Bad array reference");
3134 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
3139 /* Compare an array reference with an array specification. */
3142 compare_spec_to_ref (gfc_array_ref *ar)
3149 /* TODO: Full array sections are only allowed as actual parameters. */
3150 if (as->type == AS_ASSUMED_SIZE
3151 && (/*ar->type == AR_FULL
3152 ||*/ (ar->type == AR_SECTION
3153 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3155 gfc_error ("Rightmost upper bound of assumed size array section "
3156 "not specified at %L", &ar->where);
3160 if (ar->type == AR_FULL)
3163 if (as->rank != ar->dimen)
3165 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3166 &ar->where, ar->dimen, as->rank);
3170 for (i = 0; i < as->rank; i++)
3171 if (check_dimension (i, ar, as) == FAILURE)
3178 /* Resolve one part of an array index. */
3181 gfc_resolve_index (gfc_expr *index, int check_scalar)
3188 if (gfc_resolve_expr (index) == FAILURE)
3191 if (check_scalar && index->rank != 0)
3193 gfc_error ("Array index at %L must be scalar", &index->where);
3197 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3199 gfc_error ("Array index at %L must be of INTEGER type",
3204 if (index->ts.type == BT_REAL)
3205 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3206 &index->where) == FAILURE)
3209 if (index->ts.kind != gfc_index_integer_kind
3210 || index->ts.type != BT_INTEGER)
3213 ts.type = BT_INTEGER;
3214 ts.kind = gfc_index_integer_kind;
3216 gfc_convert_type_warn (index, &ts, 2, 0);
3222 /* Resolve a dim argument to an intrinsic function. */
3225 gfc_resolve_dim_arg (gfc_expr *dim)
3230 if (gfc_resolve_expr (dim) == FAILURE)
3235 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3239 if (dim->ts.type != BT_INTEGER)
3241 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3244 if (dim->ts.kind != gfc_index_integer_kind)
3248 ts.type = BT_INTEGER;
3249 ts.kind = gfc_index_integer_kind;
3251 gfc_convert_type_warn (dim, &ts, 2, 0);
3257 /* Given an expression that contains array references, update those array
3258 references to point to the right array specifications. While this is
3259 filled in during matching, this information is difficult to save and load
3260 in a module, so we take care of it here.
3262 The idea here is that the original array reference comes from the
3263 base symbol. We traverse the list of reference structures, setting
3264 the stored reference to references. Component references can
3265 provide an additional array specification. */
3268 find_array_spec (gfc_expr *e)
3272 gfc_symbol *derived;
3275 as = e->symtree->n.sym->as;
3278 for (ref = e->ref; ref; ref = ref->next)
3283 gfc_internal_error ("find_array_spec(): Missing spec");
3290 if (derived == NULL)
3291 derived = e->symtree->n.sym->ts.derived;
3293 c = derived->components;
3295 for (; c; c = c->next)
3296 if (c == ref->u.c.component)
3298 /* Track the sequence of component references. */
3299 if (c->ts.type == BT_DERIVED)
3300 derived = c->ts.derived;
3305 gfc_internal_error ("find_array_spec(): Component not found");
3310 gfc_internal_error ("find_array_spec(): unused as(1)");
3321 gfc_internal_error ("find_array_spec(): unused as(2)");
3325 /* Resolve an array reference. */
3328 resolve_array_ref (gfc_array_ref *ar)
3330 int i, check_scalar;
3333 for (i = 0; i < ar->dimen; i++)
3335 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3337 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3339 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3341 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3346 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3350 ar->dimen_type[i] = DIMEN_ELEMENT;
3354 ar->dimen_type[i] = DIMEN_VECTOR;
3355 if (e->expr_type == EXPR_VARIABLE
3356 && e->symtree->n.sym->ts.type == BT_DERIVED)
3357 ar->start[i] = gfc_get_parentheses (e);
3361 gfc_error ("Array index at %L is an array of rank %d",
3362 &ar->c_where[i], e->rank);
3367 /* If the reference type is unknown, figure out what kind it is. */
3369 if (ar->type == AR_UNKNOWN)
3371 ar->type = AR_ELEMENT;
3372 for (i = 0; i < ar->dimen; i++)
3373 if (ar->dimen_type[i] == DIMEN_RANGE
3374 || ar->dimen_type[i] == DIMEN_VECTOR)
3376 ar->type = AR_SECTION;
3381 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3389 resolve_substring (gfc_ref *ref)
3391 if (ref->u.ss.start != NULL)
3393 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3396 if (ref->u.ss.start->ts.type != BT_INTEGER)
3398 gfc_error ("Substring start index at %L must be of type INTEGER",
3399 &ref->u.ss.start->where);
3403 if (ref->u.ss.start->rank != 0)
3405 gfc_error ("Substring start index at %L must be scalar",
3406 &ref->u.ss.start->where);
3410 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3411 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3412 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3414 gfc_error ("Substring start index at %L is less than one",
3415 &ref->u.ss.start->where);
3420 if (ref->u.ss.end != NULL)
3422 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3425 if (ref->u.ss.end->ts.type != BT_INTEGER)
3427 gfc_error ("Substring end index at %L must be of type INTEGER",
3428 &ref->u.ss.end->where);
3432 if (ref->u.ss.end->rank != 0)
3434 gfc_error ("Substring end index at %L must be scalar",
3435 &ref->u.ss.end->where);
3439 if (ref->u.ss.length != NULL
3440 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3441 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3442 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3444 gfc_error ("Substring end index at %L exceeds the string length",
3445 &ref->u.ss.start->where);
3454 /* Resolve subtype references. */
3457 resolve_ref (gfc_expr *expr)
3459 int current_part_dimension, n_components, seen_part_dimension;
3462 for (ref = expr->ref; ref; ref = ref->next)
3463 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
3465 find_array_spec (expr);