Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / fortran / resolve.c
blob5a3f782934102c3b93417c4663af252d93951e26
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 3, or (at your option) any later
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
35 typedef enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39 seq_type;
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code *head, *current, *tail;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block. */
51 bitmap reachable_labels;
53 code_stack;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL block. */
60 static int forall_flag;
62 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
64 static int omp_workshare_flag;
66 /* Nonzero if we are processing a formal arglist. The corresponding function
67 resets the flag each time that it is read. */
68 static int formal_arg_flag = 0;
70 /* True if we are resolving a specification expression. */
71 static int specification_expr = 0;
73 /* The id of the last entry seen. */
74 static int current_entry_id;
76 /* We use bitmaps to determine if a branch target is valid. */
77 static bitmap_obstack labels_obstack;
79 int
80 gfc_is_formal_arg (void)
82 return formal_arg_flag;
85 /* Resolve types of formal argument lists. These have to be done early so that
86 the formal argument lists of module procedures can be copied to the
87 containing module before the individual procedures are resolved
88 individually. We also resolve argument lists of procedures in interface
89 blocks because they are self-contained scoping units.
91 Since a dummy argument cannot be a non-dummy procedure, the only
92 resort left for untyped names are the IMPLICIT types. */
94 static void
95 resolve_formal_arglist (gfc_symbol *proc)
97 gfc_formal_arglist *f;
98 gfc_symbol *sym;
99 int i;
101 if (proc->result != NULL)
102 sym = proc->result;
103 else
104 sym = proc;
106 if (gfc_elemental (proc)
107 || sym->attr.pointer || sym->attr.allocatable
108 || (sym->as && sym->as->rank > 0))
109 proc->attr.always_explicit = 1;
111 formal_arg_flag = 1;
113 for (f = proc->formal; f; f = f->next)
115 sym = f->sym;
117 if (sym == NULL)
119 /* Alternate return placeholder. */
120 if (gfc_elemental (proc))
121 gfc_error ("Alternate return specifier in elemental subroutine "
122 "'%s' at %L is not allowed", proc->name,
123 &proc->declared_at);
124 if (proc->attr.function)
125 gfc_error ("Alternate return specifier in function "
126 "'%s' at %L is not allowed", proc->name,
127 &proc->declared_at);
128 continue;
131 if (sym->attr.if_source != IFSRC_UNKNOWN)
132 resolve_formal_arglist (sym);
134 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
136 if (gfc_pure (proc) && !gfc_pure (sym))
138 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
139 "also be PURE", sym->name, &sym->declared_at);
140 continue;
143 if (gfc_elemental (proc))
145 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
146 "procedure", &sym->declared_at);
147 continue;
150 if (sym->attr.function
151 && sym->ts.type == BT_UNKNOWN
152 && sym->attr.intrinsic)
154 gfc_intrinsic_sym *isym;
155 isym = gfc_find_function (sym->name);
156 if (isym == NULL || !isym->specific)
158 gfc_error ("Unable to find a specific INTRINSIC procedure "
159 "for the reference '%s' at %L", sym->name,
160 &sym->declared_at);
162 sym->ts = isym->ts;
165 continue;
168 if (sym->ts.type == BT_UNKNOWN)
170 if (!sym->attr.function || sym->result == sym)
171 gfc_set_default_type (sym, 1, sym->ns);
174 gfc_resolve_array_spec (sym->as, 0);
176 /* We can't tell if an array with dimension (:) is assumed or deferred
177 shape until we know if it has the pointer or allocatable attributes.
179 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
180 && !(sym->attr.pointer || sym->attr.allocatable))
182 sym->as->type = AS_ASSUMED_SHAPE;
183 for (i = 0; i < sym->as->rank; i++)
184 sym->as->lower[i] = gfc_int_expr (1);
187 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
188 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
189 || sym->attr.optional)
190 proc->attr.always_explicit = 1;
192 /* If the flavor is unknown at this point, it has to be a variable.
193 A procedure specification would have already set the type. */
195 if (sym->attr.flavor == FL_UNKNOWN)
196 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
198 if (gfc_pure (proc) && !sym->attr.pointer
199 && sym->attr.flavor != FL_PROCEDURE)
201 if (proc->attr.function && sym->attr.intent != INTENT_IN)
202 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
203 "INTENT(IN)", sym->name, proc->name,
204 &sym->declared_at);
206 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
207 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
208 "have its INTENT specified", sym->name, proc->name,
209 &sym->declared_at);
212 if (gfc_elemental (proc))
214 if (sym->as != NULL)
216 gfc_error ("Argument '%s' of elemental procedure at %L must "
217 "be scalar", sym->name, &sym->declared_at);
218 continue;
221 if (sym->attr.pointer)
223 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
224 "have the POINTER attribute", sym->name,
225 &sym->declared_at);
226 continue;
229 if (sym->attr.flavor == FL_PROCEDURE)
231 gfc_error ("Dummy procedure '%s' not allowed in elemental "
232 "procedure '%s' at %L", sym->name, proc->name,
233 &sym->declared_at);
234 continue;
238 /* Each dummy shall be specified to be scalar. */
239 if (proc->attr.proc == PROC_ST_FUNCTION)
241 if (sym->as != NULL)
243 gfc_error ("Argument '%s' of statement function at %L must "
244 "be scalar", sym->name, &sym->declared_at);
245 continue;
248 if (sym->ts.type == BT_CHARACTER)
250 gfc_charlen *cl = sym->ts.cl;
251 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
253 gfc_error ("Character-valued argument '%s' of statement "
254 "function at %L must have constant length",
255 sym->name, &sym->declared_at);
256 continue;
261 formal_arg_flag = 0;
265 /* Work function called when searching for symbols that have argument lists
266 associated with them. */
268 static void
269 find_arglists (gfc_symbol *sym)
271 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
272 return;
274 resolve_formal_arglist (sym);
278 /* Given a namespace, resolve all formal argument lists within the namespace.
281 static void
282 resolve_formal_arglists (gfc_namespace *ns)
284 if (ns == NULL)
285 return;
287 gfc_traverse_ns (ns, find_arglists);
291 static void
292 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
294 try t;
296 /* If this namespace is not a function or an entry master function,
297 ignore it. */
298 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
299 || sym->attr.entry_master)
300 return;
302 /* Try to find out of what the return type is. */
303 if (sym->result->ts.type == BT_UNKNOWN)
305 t = gfc_set_default_type (sym->result, 0, ns);
307 if (t == FAILURE && !sym->result->attr.untyped)
309 if (sym->result == sym)
310 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
311 sym->name, &sym->declared_at);
312 else
313 gfc_error ("Result '%s' of contained function '%s' at %L has "
314 "no IMPLICIT type", sym->result->name, sym->name,
315 &sym->result->declared_at);
316 sym->result->attr.untyped = 1;
320 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
321 type, lists the only ways a character length value of * can be used:
322 dummy arguments of procedures, named constants, and function results
323 in external functions. Internal function results are not on that list;
324 ergo, not permitted. */
326 if (sym->result->ts.type == BT_CHARACTER)
328 gfc_charlen *cl = sym->result->ts.cl;
329 if (!cl || !cl->length)
330 gfc_error ("Character-valued internal function '%s' at %L must "
331 "not be assumed length", sym->name, &sym->declared_at);
336 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
337 introduce duplicates. */
339 static void
340 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
342 gfc_formal_arglist *f, *new_arglist;
343 gfc_symbol *new_sym;
345 for (; new_args != NULL; new_args = new_args->next)
347 new_sym = new_args->sym;
348 /* See if this arg is already in the formal argument list. */
349 for (f = proc->formal; f; f = f->next)
351 if (new_sym == f->sym)
352 break;
355 if (f)
356 continue;
358 /* Add a new argument. Argument order is not important. */
359 new_arglist = gfc_get_formal_arglist ();
360 new_arglist->sym = new_sym;
361 new_arglist->next = proc->formal;
362 proc->formal = new_arglist;
367 /* Flag the arguments that are not present in all entries. */
369 static void
370 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
372 gfc_formal_arglist *f, *head;
373 head = new_args;
375 for (f = proc->formal; f; f = f->next)
377 if (f->sym == NULL)
378 continue;
380 for (new_args = head; new_args; new_args = new_args->next)
382 if (new_args->sym == f->sym)
383 break;
386 if (new_args)
387 continue;
389 f->sym->attr.not_always_present = 1;
394 /* Resolve alternate entry points. If a symbol has multiple entry points we
395 create a new master symbol for the main routine, and turn the existing
396 symbol into an entry point. */
398 static void
399 resolve_entries (gfc_namespace *ns)
401 gfc_namespace *old_ns;
402 gfc_code *c;
403 gfc_symbol *proc;
404 gfc_entry_list *el;
405 char name[GFC_MAX_SYMBOL_LEN + 1];
406 static int master_count = 0;
408 if (ns->proc_name == NULL)
409 return;
411 /* No need to do anything if this procedure doesn't have alternate entry
412 points. */
413 if (!ns->entries)
414 return;
416 /* We may already have resolved alternate entry points. */
417 if (ns->proc_name->attr.entry_master)
418 return;
420 /* If this isn't a procedure something has gone horribly wrong. */
421 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
423 /* Remember the current namespace. */
424 old_ns = gfc_current_ns;
426 gfc_current_ns = ns;
428 /* Add the main entry point to the list of entry points. */
429 el = gfc_get_entry_list ();
430 el->sym = ns->proc_name;
431 el->id = 0;
432 el->next = ns->entries;
433 ns->entries = el;
434 ns->proc_name->attr.entry = 1;
436 /* If it is a module function, it needs to be in the right namespace
437 so that gfc_get_fake_result_decl can gather up the results. The
438 need for this arose in get_proc_name, where these beasts were
439 left in their own namespace, to keep prior references linked to
440 the entry declaration.*/
441 if (ns->proc_name->attr.function
442 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
443 el->sym->ns = ns;
445 /* Do the same for entries where the master is not a module
446 procedure. These are retained in the module namespace because
447 of the module procedure declaration. */
448 for (el = el->next; el; el = el->next)
449 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
450 && el->sym->attr.mod_proc)
451 el->sym->ns = ns;
452 el = ns->entries;
454 /* Add an entry statement for it. */
455 c = gfc_get_code ();
456 c->op = EXEC_ENTRY;
457 c->ext.entry = el;
458 c->next = ns->code;
459 ns->code = c;
461 /* Create a new symbol for the master function. */
462 /* Give the internal function a unique name (within this file).
463 Also include the function name so the user has some hope of figuring
464 out what is going on. */
465 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
466 master_count++, ns->proc_name->name);
467 gfc_get_ha_symbol (name, &proc);
468 gcc_assert (proc != NULL);
470 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
471 if (ns->proc_name->attr.subroutine)
472 gfc_add_subroutine (&proc->attr, proc->name, NULL);
473 else
475 gfc_symbol *sym;
476 gfc_typespec *ts, *fts;
477 gfc_array_spec *as, *fas;
478 gfc_add_function (&proc->attr, proc->name, NULL);
479 proc->result = proc;
480 fas = ns->entries->sym->as;
481 fas = fas ? fas : ns->entries->sym->result->as;
482 fts = &ns->entries->sym->result->ts;
483 if (fts->type == BT_UNKNOWN)
484 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
485 for (el = ns->entries->next; el; el = el->next)
487 ts = &el->sym->result->ts;
488 as = el->sym->as;
489 as = as ? as : el->sym->result->as;
490 if (ts->type == BT_UNKNOWN)
491 ts = gfc_get_default_type (el->sym->result, NULL);
493 if (! gfc_compare_types (ts, fts)
494 || (el->sym->result->attr.dimension
495 != ns->entries->sym->result->attr.dimension)
496 || (el->sym->result->attr.pointer
497 != ns->entries->sym->result->attr.pointer))
498 break;
499 else if (as && fas && ns->entries->sym->result != el->sym->result
500 && gfc_compare_array_spec (as, fas) == 0)
501 gfc_error ("Function %s at %L has entries with mismatched "
502 "array specifications", ns->entries->sym->name,
503 &ns->entries->sym->declared_at);
504 /* The characteristics need to match and thus both need to have
505 the same string length, i.e. both len=*, or both len=4.
506 Having both len=<variable> is also possible, but difficult to
507 check at compile time. */
508 else if (ts->type == BT_CHARACTER && ts->cl && fts->cl
509 && (((ts->cl->length && !fts->cl->length)
510 ||(!ts->cl->length && fts->cl->length))
511 || (ts->cl->length
512 && ts->cl->length->expr_type
513 != fts->cl->length->expr_type)
514 || (ts->cl->length
515 && ts->cl->length->expr_type == EXPR_CONSTANT
516 && mpz_cmp (ts->cl->length->value.integer,
517 fts->cl->length->value.integer) != 0)))
518 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
519 "entries returning variables of different "
520 "string lengths", ns->entries->sym->name,
521 &ns->entries->sym->declared_at);
524 if (el == NULL)
526 sym = ns->entries->sym->result;
527 /* All result types the same. */
528 proc->ts = *fts;
529 if (sym->attr.dimension)
530 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
531 if (sym->attr.pointer)
532 gfc_add_pointer (&proc->attr, NULL);
534 else
536 /* Otherwise the result will be passed through a union by
537 reference. */
538 proc->attr.mixed_entry_master = 1;
539 for (el = ns->entries; el; el = el->next)
541 sym = el->sym->result;
542 if (sym->attr.dimension)
544 if (el == ns->entries)
545 gfc_error ("FUNCTION result %s can't be an array in "
546 "FUNCTION %s at %L", sym->name,
547 ns->entries->sym->name, &sym->declared_at);
548 else
549 gfc_error ("ENTRY result %s can't be an array in "
550 "FUNCTION %s at %L", sym->name,
551 ns->entries->sym->name, &sym->declared_at);
553 else if (sym->attr.pointer)
555 if (el == ns->entries)
556 gfc_error ("FUNCTION result %s can't be a POINTER in "
557 "FUNCTION %s at %L", sym->name,
558 ns->entries->sym->name, &sym->declared_at);
559 else
560 gfc_error ("ENTRY result %s can't be a POINTER in "
561 "FUNCTION %s at %L", sym->name,
562 ns->entries->sym->name, &sym->declared_at);
564 else
566 ts = &sym->ts;
567 if (ts->type == BT_UNKNOWN)
568 ts = gfc_get_default_type (sym, NULL);
569 switch (ts->type)
571 case BT_INTEGER:
572 if (ts->kind == gfc_default_integer_kind)
573 sym = NULL;
574 break;
575 case BT_REAL:
576 if (ts->kind == gfc_default_real_kind
577 || ts->kind == gfc_default_double_kind)
578 sym = NULL;
579 break;
580 case BT_COMPLEX:
581 if (ts->kind == gfc_default_complex_kind)
582 sym = NULL;
583 break;
584 case BT_LOGICAL:
585 if (ts->kind == gfc_default_logical_kind)
586 sym = NULL;
587 break;
588 case BT_UNKNOWN:
589 /* We will issue error elsewhere. */
590 sym = NULL;
591 break;
592 default:
593 break;
595 if (sym)
597 if (el == ns->entries)
598 gfc_error ("FUNCTION result %s can't be of type %s "
599 "in FUNCTION %s at %L", sym->name,
600 gfc_typename (ts), ns->entries->sym->name,
601 &sym->declared_at);
602 else
603 gfc_error ("ENTRY result %s can't be of type %s "
604 "in FUNCTION %s at %L", sym->name,
605 gfc_typename (ts), ns->entries->sym->name,
606 &sym->declared_at);
612 proc->attr.access = ACCESS_PRIVATE;
613 proc->attr.entry_master = 1;
615 /* Merge all the entry point arguments. */
616 for (el = ns->entries; el; el = el->next)
617 merge_argument_lists (proc, el->sym->formal);
619 /* Check the master formal arguments for any that are not
620 present in all entry points. */
621 for (el = ns->entries; el; el = el->next)
622 check_argument_lists (proc, el->sym->formal);
624 /* Use the master function for the function body. */
625 ns->proc_name = proc;
627 /* Finalize the new symbols. */
628 gfc_commit_symbols ();
630 /* Restore the original namespace. */
631 gfc_current_ns = old_ns;
635 static bool
636 has_default_initializer (gfc_symbol *der)
638 gfc_component *c;
640 gcc_assert (der->attr.flavor == FL_DERIVED);
641 for (c = der->components; c; c = c->next)
642 if ((c->ts.type != BT_DERIVED && c->initializer)
643 || (c->ts.type == BT_DERIVED
644 && (!c->pointer && has_default_initializer (c->ts.derived))))
645 break;
647 return c != NULL;
650 /* Resolve common variables. */
651 static void
652 resolve_common_vars (gfc_symbol *sym, bool named_common)
654 gfc_symbol *csym = sym;
656 for (; csym; csym = csym->common_next)
658 if (csym->value || csym->attr.data)
660 if (!csym->ns->is_block_data)
661 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
662 "but only in BLOCK DATA initialization is "
663 "allowed", csym->name, &csym->declared_at);
664 else if (!named_common)
665 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
666 "in a blank COMMON but initialization is only "
667 "allowed in named common blocks", csym->name,
668 &csym->declared_at);
671 if (csym->ts.type != BT_DERIVED)
672 continue;
674 if (!(csym->ts.derived->attr.sequence
675 || csym->ts.derived->attr.is_bind_c))
676 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
677 "has neither the SEQUENCE nor the BIND(C) "
678 "attribute", csym->name, &csym->declared_at);
679 if (csym->ts.derived->attr.alloc_comp)
680 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
681 "has an ultimate component that is "
682 "allocatable", csym->name, &csym->declared_at);
683 if (has_default_initializer (csym->ts.derived))
684 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
685 "may not have default initializer", csym->name,
686 &csym->declared_at);
690 /* Resolve common blocks. */
691 static void
692 resolve_common_blocks (gfc_symtree *common_root)
694 gfc_symbol *sym;
696 if (common_root == NULL)
697 return;
699 if (common_root->left)
700 resolve_common_blocks (common_root->left);
701 if (common_root->right)
702 resolve_common_blocks (common_root->right);
704 resolve_common_vars (common_root->n.common->head, true);
706 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
707 if (sym == NULL)
708 return;
710 if (sym->attr.flavor == FL_PARAMETER)
711 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
712 sym->name, &common_root->n.common->where, &sym->declared_at);
714 if (sym->attr.intrinsic)
715 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
716 sym->name, &common_root->n.common->where);
717 else if (sym->attr.result
718 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
719 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
720 "that is also a function result", sym->name,
721 &common_root->n.common->where);
722 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
723 && sym->attr.proc != PROC_ST_FUNCTION)
724 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
725 "that is also a global procedure", sym->name,
726 &common_root->n.common->where);
730 /* Resolve contained function types. Because contained functions can call one
731 another, they have to be worked out before any of the contained procedures
732 can be resolved.
734 The good news is that if a function doesn't already have a type, the only
735 way it can get one is through an IMPLICIT type or a RESULT variable, because
736 by definition contained functions are contained namespace they're contained
737 in, not in a sibling or parent namespace. */
739 static void
740 resolve_contained_functions (gfc_namespace *ns)
742 gfc_namespace *child;
743 gfc_entry_list *el;
745 resolve_formal_arglists (ns);
747 for (child = ns->contained; child; child = child->sibling)
749 /* Resolve alternate entry points first. */
750 resolve_entries (child);
752 /* Then check function return types. */
753 resolve_contained_fntype (child->proc_name, child);
754 for (el = child->entries; el; el = el->next)
755 resolve_contained_fntype (el->sym, child);
760 /* Resolve all of the elements of a structure constructor and make sure that
761 the types are correct. */
763 static try
764 resolve_structure_cons (gfc_expr *expr)
766 gfc_constructor *cons;
767 gfc_component *comp;
768 try t;
769 symbol_attribute a;
771 t = SUCCESS;
772 cons = expr->value.constructor;
773 /* A constructor may have references if it is the result of substituting a
774 parameter variable. In this case we just pull out the component we
775 want. */
776 if (expr->ref)
777 comp = expr->ref->u.c.sym->components;
778 else
779 comp = expr->ts.derived->components;
781 /* See if the user is trying to invoke a structure constructor for one of
782 the iso_c_binding derived types. */
783 if (expr->ts.derived && expr->ts.derived->ts.is_iso_c && cons
784 && cons->expr != NULL)
786 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
787 expr->ts.derived->name, &(expr->where));
788 return FAILURE;
791 for (; comp; comp = comp->next, cons = cons->next)
793 int rank;
795 if (!cons->expr)
796 continue;
798 if (gfc_resolve_expr (cons->expr) == FAILURE)
800 t = FAILURE;
801 continue;
804 rank = comp->as ? comp->as->rank : 0;
805 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
806 && (comp->allocatable || cons->expr->rank))
808 gfc_error ("The rank of the element in the derived type "
809 "constructor at %L does not match that of the "
810 "component (%d/%d)", &cons->expr->where,
811 cons->expr->rank, rank);
812 t = FAILURE;
815 /* If we don't have the right type, try to convert it. */
817 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
819 t = FAILURE;
820 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
821 gfc_error ("The element in the derived type constructor at %L, "
822 "for pointer component '%s', is %s but should be %s",
823 &cons->expr->where, comp->name,
824 gfc_basic_typename (cons->expr->ts.type),
825 gfc_basic_typename (comp->ts.type));
826 else
827 t = gfc_convert_type (cons->expr, &comp->ts, 1);
830 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
831 continue;
833 a = gfc_expr_attr (cons->expr);
835 if (!a.pointer && !a.target)
837 t = FAILURE;
838 gfc_error ("The element in the derived type constructor at %L, "
839 "for pointer component '%s' should be a POINTER or "
840 "a TARGET", &cons->expr->where, comp->name);
844 return t;
848 /****************** Expression name resolution ******************/
850 /* Returns 0 if a symbol was not declared with a type or
851 attribute declaration statement, nonzero otherwise. */
853 static int
854 was_declared (gfc_symbol *sym)
856 symbol_attribute a;
858 a = sym->attr;
860 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
861 return 1;
863 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
864 || a.optional || a.pointer || a.save || a.target || a.volatile_
865 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
866 return 1;
868 return 0;
872 /* Determine if a symbol is generic or not. */
874 static int
875 generic_sym (gfc_symbol *sym)
877 gfc_symbol *s;
879 if (sym->attr.generic ||
880 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
881 return 1;
883 if (was_declared (sym) || sym->ns->parent == NULL)
884 return 0;
886 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
888 if (s != NULL)
890 if (s == sym)
891 return 0;
892 else
893 return generic_sym (s);
896 return 0;
900 /* Determine if a symbol is specific or not. */
902 static int
903 specific_sym (gfc_symbol *sym)
905 gfc_symbol *s;
907 if (sym->attr.if_source == IFSRC_IFBODY
908 || sym->attr.proc == PROC_MODULE
909 || sym->attr.proc == PROC_INTERNAL
910 || sym->attr.proc == PROC_ST_FUNCTION
911 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
912 || sym->attr.external)
913 return 1;
915 if (was_declared (sym) || sym->ns->parent == NULL)
916 return 0;
918 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
920 return (s == NULL) ? 0 : specific_sym (s);
924 /* Figure out if the procedure is specific, generic or unknown. */
926 typedef enum
927 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
928 proc_type;
930 static proc_type
931 procedure_kind (gfc_symbol *sym)
933 if (generic_sym (sym))
934 return PTYPE_GENERIC;
936 if (specific_sym (sym))
937 return PTYPE_SPECIFIC;
939 return PTYPE_UNKNOWN;
942 /* Check references to assumed size arrays. The flag need_full_assumed_size
943 is nonzero when matching actual arguments. */
945 static int need_full_assumed_size = 0;
947 static bool
948 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
950 gfc_ref *ref;
951 int dim;
952 int last = 1;
954 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
955 return false;
957 for (ref = e->ref; ref; ref = ref->next)
958 if (ref->type == REF_ARRAY)
959 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
960 last = (ref->u.ar.end[dim] == NULL)
961 && (ref->u.ar.type == DIMEN_ELEMENT);
963 if (last)
965 gfc_error ("The upper bound in the last dimension must "
966 "appear in the reference to the assumed size "
967 "array '%s' at %L", sym->name, &e->where);
968 return true;
970 return false;
974 /* Look for bad assumed size array references in argument expressions
975 of elemental and array valued intrinsic procedures. Since this is
976 called from procedure resolution functions, it only recurses at
977 operators. */
979 static bool
980 resolve_assumed_size_actual (gfc_expr *e)
982 if (e == NULL)
983 return false;
985 switch (e->expr_type)
987 case EXPR_VARIABLE:
988 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
989 return true;
990 break;
992 case EXPR_OP:
993 if (resolve_assumed_size_actual (e->value.op.op1)
994 || resolve_assumed_size_actual (e->value.op.op2))
995 return true;
996 break;
998 default:
999 break;
1001 return false;
1005 /* Resolve an actual argument list. Most of the time, this is just
1006 resolving the expressions in the list.
1007 The exception is that we sometimes have to decide whether arguments
1008 that look like procedure arguments are really simple variable
1009 references. */
1011 static try
1012 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype)
1014 gfc_symbol *sym;
1015 gfc_symtree *parent_st;
1016 gfc_expr *e;
1017 int save_need_full_assumed_size;
1019 for (; arg; arg = arg->next)
1021 e = arg->expr;
1022 if (e == NULL)
1024 /* Check the label is a valid branching target. */
1025 if (arg->label)
1027 if (arg->label->defined == ST_LABEL_UNKNOWN)
1029 gfc_error ("Label %d referenced at %L is never defined",
1030 arg->label->value, &arg->label->where);
1031 return FAILURE;
1034 continue;
1037 if (e->expr_type == FL_VARIABLE && e->symtree->ambiguous)
1039 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1040 &e->where);
1041 return FAILURE;
1044 if (e->ts.type != BT_PROCEDURE)
1046 save_need_full_assumed_size = need_full_assumed_size;
1047 if (e->expr_type != FL_VARIABLE)
1048 need_full_assumed_size = 0;
1049 if (gfc_resolve_expr (e) != SUCCESS)
1050 return FAILURE;
1051 need_full_assumed_size = save_need_full_assumed_size;
1052 goto argument_list;
1055 /* See if the expression node should really be a variable reference. */
1057 sym = e->symtree->n.sym;
1059 if (sym->attr.flavor == FL_PROCEDURE
1060 || sym->attr.intrinsic
1061 || sym->attr.external)
1063 int actual_ok;
1065 /* If a procedure is not already determined to be something else
1066 check if it is intrinsic. */
1067 if (!sym->attr.intrinsic
1068 && !(sym->attr.external || sym->attr.use_assoc
1069 || sym->attr.if_source == IFSRC_IFBODY)
1070 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1071 sym->attr.intrinsic = 1;
1073 if (sym->attr.proc == PROC_ST_FUNCTION)
1075 gfc_error ("Statement function '%s' at %L is not allowed as an "
1076 "actual argument", sym->name, &e->where);
1079 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1080 sym->attr.subroutine);
1081 if (sym->attr.intrinsic && actual_ok == 0)
1083 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1084 "actual argument", sym->name, &e->where);
1087 if (sym->attr.contained && !sym->attr.use_assoc
1088 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1090 gfc_error ("Internal procedure '%s' is not allowed as an "
1091 "actual argument at %L", sym->name, &e->where);
1094 if (sym->attr.elemental && !sym->attr.intrinsic)
1096 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1097 "allowed as an actual argument at %L", sym->name,
1098 &e->where);
1101 /* Check if a generic interface has a specific procedure
1102 with the same name before emitting an error. */
1103 if (sym->attr.generic)
1105 gfc_interface *p;
1106 for (p = sym->generic; p; p = p->next)
1107 if (strcmp (sym->name, p->sym->name) == 0)
1109 e->symtree = gfc_find_symtree
1110 (p->sym->ns->sym_root, sym->name);
1111 sym = p->sym;
1112 break;
1115 if (p == NULL || e->symtree == NULL)
1116 gfc_error ("GENERIC procedure '%s' is not "
1117 "allowed as an actual argument at %L", sym->name,
1118 &e->where);
1121 /* If the symbol is the function that names the current (or
1122 parent) scope, then we really have a variable reference. */
1124 if (sym->attr.function && sym->result == sym
1125 && (sym->ns->proc_name == sym
1126 || (sym->ns->parent != NULL
1127 && sym->ns->parent->proc_name == sym)))
1128 goto got_variable;
1130 /* If all else fails, see if we have a specific intrinsic. */
1131 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1133 gfc_intrinsic_sym *isym;
1135 isym = gfc_find_function (sym->name);
1136 if (isym == NULL || !isym->specific)
1138 gfc_error ("Unable to find a specific INTRINSIC procedure "
1139 "for the reference '%s' at %L", sym->name,
1140 &e->where);
1141 return FAILURE;
1143 sym->ts = isym->ts;
1144 sym->attr.intrinsic = 1;
1145 sym->attr.function = 1;
1147 goto argument_list;
1150 /* See if the name is a module procedure in a parent unit. */
1152 if (was_declared (sym) || sym->ns->parent == NULL)
1153 goto got_variable;
1155 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1157 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1158 return FAILURE;
1161 if (parent_st == NULL)
1162 goto got_variable;
1164 sym = parent_st->n.sym;
1165 e->symtree = parent_st; /* Point to the right thing. */
1167 if (sym->attr.flavor == FL_PROCEDURE
1168 || sym->attr.intrinsic
1169 || sym->attr.external)
1171 goto argument_list;
1174 got_variable:
1175 e->expr_type = EXPR_VARIABLE;
1176 e->ts = sym->ts;
1177 if (sym->as != NULL)
1179 e->rank = sym->as->rank;
1180 e->ref = gfc_get_ref ();
1181 e->ref->type = REF_ARRAY;
1182 e->ref->u.ar.type = AR_FULL;
1183 e->ref->u.ar.as = sym->as;
1186 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1187 primary.c (match_actual_arg). If above code determines that it
1188 is a variable instead, it needs to be resolved as it was not
1189 done at the beginning of this function. */
1190 save_need_full_assumed_size = need_full_assumed_size;
1191 if (e->expr_type != FL_VARIABLE)
1192 need_full_assumed_size = 0;
1193 if (gfc_resolve_expr (e) != SUCCESS)
1194 return FAILURE;
1195 need_full_assumed_size = save_need_full_assumed_size;
1197 argument_list:
1198 /* Check argument list functions %VAL, %LOC and %REF. There is
1199 nothing to do for %REF. */
1200 if (arg->name && arg->name[0] == '%')
1202 if (strncmp ("%VAL", arg->name, 4) == 0)
1204 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1206 gfc_error ("By-value argument at %L is not of numeric "
1207 "type", &e->where);
1208 return FAILURE;
1211 if (e->rank)
1213 gfc_error ("By-value argument at %L cannot be an array or "
1214 "an array section", &e->where);
1215 return FAILURE;
1218 /* Intrinsics are still PROC_UNKNOWN here. However,
1219 since same file external procedures are not resolvable
1220 in gfortran, it is a good deal easier to leave them to
1221 intrinsic.c. */
1222 if (ptype != PROC_UNKNOWN
1223 && ptype != PROC_DUMMY
1224 && ptype != PROC_EXTERNAL
1225 && ptype != PROC_MODULE)
1227 gfc_error ("By-value argument at %L is not allowed "
1228 "in this context", &e->where);
1229 return FAILURE;
1233 /* Statement functions have already been excluded above. */
1234 else if (strncmp ("%LOC", arg->name, 4) == 0
1235 && e->ts.type == BT_PROCEDURE)
1237 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1239 gfc_error ("Passing internal procedure at %L by location "
1240 "not allowed", &e->where);
1241 return FAILURE;
1247 return SUCCESS;
1251 /* Do the checks of the actual argument list that are specific to elemental
1252 procedures. If called with c == NULL, we have a function, otherwise if
1253 expr == NULL, we have a subroutine. */
1255 static try
1256 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1258 gfc_actual_arglist *arg0;
1259 gfc_actual_arglist *arg;
1260 gfc_symbol *esym = NULL;
1261 gfc_intrinsic_sym *isym = NULL;
1262 gfc_expr *e = NULL;
1263 gfc_intrinsic_arg *iformal = NULL;
1264 gfc_formal_arglist *eformal = NULL;
1265 bool formal_optional = false;
1266 bool set_by_optional = false;
1267 int i;
1268 int rank = 0;
1270 /* Is this an elemental procedure? */
1271 if (expr && expr->value.function.actual != NULL)
1273 if (expr->value.function.esym != NULL
1274 && expr->value.function.esym->attr.elemental)
1276 arg0 = expr->value.function.actual;
1277 esym = expr->value.function.esym;
1279 else if (expr->value.function.isym != NULL
1280 && expr->value.function.isym->elemental)
1282 arg0 = expr->value.function.actual;
1283 isym = expr->value.function.isym;
1285 else
1286 return SUCCESS;
1288 else if (c && c->ext.actual != NULL && c->symtree->n.sym->attr.elemental)
1290 arg0 = c->ext.actual;
1291 esym = c->symtree->n.sym;
1293 else
1294 return SUCCESS;
1296 /* The rank of an elemental is the rank of its array argument(s). */
1297 for (arg = arg0; arg; arg = arg->next)
1299 if (arg->expr != NULL && arg->expr->rank > 0)
1301 rank = arg->expr->rank;
1302 if (arg->expr->expr_type == EXPR_VARIABLE
1303 && arg->expr->symtree->n.sym->attr.optional)
1304 set_by_optional = true;
1306 /* Function specific; set the result rank and shape. */
1307 if (expr)
1309 expr->rank = rank;
1310 if (!expr->shape && arg->expr->shape)
1312 expr->shape = gfc_get_shape (rank);
1313 for (i = 0; i < rank; i++)
1314 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1317 break;
1321 /* If it is an array, it shall not be supplied as an actual argument
1322 to an elemental procedure unless an array of the same rank is supplied
1323 as an actual argument corresponding to a nonoptional dummy argument of
1324 that elemental procedure(12.4.1.5). */
1325 formal_optional = false;
1326 if (isym)
1327 iformal = isym->formal;
1328 else
1329 eformal = esym->formal;
1331 for (arg = arg0; arg; arg = arg->next)
1333 if (eformal)
1335 if (eformal->sym && eformal->sym->attr.optional)
1336 formal_optional = true;
1337 eformal = eformal->next;
1339 else if (isym && iformal)
1341 if (iformal->optional)
1342 formal_optional = true;
1343 iformal = iformal->next;
1345 else if (isym)
1346 formal_optional = true;
1348 if (pedantic && arg->expr != NULL
1349 && arg->expr->expr_type == EXPR_VARIABLE
1350 && arg->expr->symtree->n.sym->attr.optional
1351 && formal_optional
1352 && arg->expr->rank
1353 && (set_by_optional || arg->expr->rank != rank)
1354 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1356 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1357 "MISSING, it cannot be the actual argument of an "
1358 "ELEMENTAL procedure unless there is a non-optional "
1359 "argument with the same rank (12.4.1.5)",
1360 arg->expr->symtree->n.sym->name, &arg->expr->where);
1361 return FAILURE;
1365 for (arg = arg0; arg; arg = arg->next)
1367 if (arg->expr == NULL || arg->expr->rank == 0)
1368 continue;
1370 /* Being elemental, the last upper bound of an assumed size array
1371 argument must be present. */
1372 if (resolve_assumed_size_actual (arg->expr))
1373 return FAILURE;
1375 /* Elemental procedure's array actual arguments must conform. */
1376 if (e != NULL)
1378 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1379 == FAILURE)
1380 return FAILURE;
1382 else
1383 e = arg->expr;
1386 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1387 is an array, the intent inout/out variable needs to be also an array. */
1388 if (rank > 0 && esym && expr == NULL)
1389 for (eformal = esym->formal, arg = arg0; arg && eformal;
1390 arg = arg->next, eformal = eformal->next)
1391 if ((eformal->sym->attr.intent == INTENT_OUT
1392 || eformal->sym->attr.intent == INTENT_INOUT)
1393 && arg->expr && arg->expr->rank == 0)
1395 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1396 "ELEMENTAL subroutine '%s' is a scalar, but another "
1397 "actual argument is an array", &arg->expr->where,
1398 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1399 : "INOUT", eformal->sym->name, esym->name);
1400 return FAILURE;
1402 return SUCCESS;
1406 /* Go through each actual argument in ACTUAL and see if it can be
1407 implemented as an inlined, non-copying intrinsic. FNSYM is the
1408 function being called, or NULL if not known. */
1410 static void
1411 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1413 gfc_actual_arglist *ap;
1414 gfc_expr *expr;
1416 for (ap = actual; ap; ap = ap->next)
1417 if (ap->expr
1418 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1419 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1420 ap->expr->inline_noncopying_intrinsic = 1;
1424 /* This function does the checking of references to global procedures
1425 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1426 77 and 95 standards. It checks for a gsymbol for the name, making
1427 one if it does not already exist. If it already exists, then the
1428 reference being resolved must correspond to the type of gsymbol.
1429 Otherwise, the new symbol is equipped with the attributes of the
1430 reference. The corresponding code that is called in creating
1431 global entities is parse.c. */
1433 static void
1434 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1436 gfc_gsymbol * gsym;
1437 unsigned int type;
1439 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1441 gsym = gfc_get_gsymbol (sym->name);
1443 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1444 gfc_global_used (gsym, where);
1446 if (gsym->type == GSYM_UNKNOWN)
1448 gsym->type = type;
1449 gsym->where = *where;
1452 gsym->used = 1;
1456 /************* Function resolution *************/
1458 /* Resolve a function call known to be generic.
1459 Section 14.1.2.4.1. */
1461 static match
1462 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1464 gfc_symbol *s;
1466 if (sym->attr.generic)
1468 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1469 if (s != NULL)
1471 expr->value.function.name = s->name;
1472 expr->value.function.esym = s;
1474 if (s->ts.type != BT_UNKNOWN)
1475 expr->ts = s->ts;
1476 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1477 expr->ts = s->result->ts;
1479 if (s->as != NULL)
1480 expr->rank = s->as->rank;
1481 else if (s->result != NULL && s->result->as != NULL)
1482 expr->rank = s->result->as->rank;
1484 gfc_set_sym_referenced (expr->value.function.esym);
1486 return MATCH_YES;
1489 /* TODO: Need to search for elemental references in generic
1490 interface. */
1493 if (sym->attr.intrinsic)
1494 return gfc_intrinsic_func_interface (expr, 0);
1496 return MATCH_NO;
1500 static try
1501 resolve_generic_f (gfc_expr *expr)
1503 gfc_symbol *sym;
1504 match m;
1506 sym = expr->symtree->n.sym;
1508 for (;;)
1510 m = resolve_generic_f0 (expr, sym);
1511 if (m == MATCH_YES)
1512 return SUCCESS;
1513 else if (m == MATCH_ERROR)
1514 return FAILURE;
1516 generic:
1517 if (sym->ns->parent == NULL)
1518 break;
1519 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1521 if (sym == NULL)
1522 break;
1523 if (!generic_sym (sym))
1524 goto generic;
1527 /* Last ditch attempt. See if the reference is to an intrinsic
1528 that possesses a matching interface. 14.1.2.4 */
1529 if (sym && !gfc_intrinsic_name (sym->name, 0))
1531 gfc_error ("There is no specific function for the generic '%s' at %L",
1532 expr->symtree->n.sym->name, &expr->where);
1533 return FAILURE;
1536 m = gfc_intrinsic_func_interface (expr, 0);
1537 if (m == MATCH_YES)
1538 return SUCCESS;
1539 if (m == MATCH_NO)
1540 gfc_error ("Generic function '%s' at %L is not consistent with a "
1541 "specific intrinsic interface", expr->symtree->n.sym->name,
1542 &expr->where);
1544 return FAILURE;
1548 /* Resolve a function call known to be specific. */
1550 static match
1551 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1553 match m;
1555 /* See if we have an intrinsic interface. */
1557 if (sym->interface != NULL && sym->interface->attr.intrinsic)
1559 gfc_intrinsic_sym *isym;
1560 isym = gfc_find_function (sym->interface->name);
1562 /* Existance of isym should be checked already. */
1563 gcc_assert (isym);
1565 sym->ts = isym->ts;
1566 sym->attr.function = 1;
1567 sym->attr.proc = PROC_EXTERNAL;
1568 goto found;
1571 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1573 if (sym->attr.dummy)
1575 sym->attr.proc = PROC_DUMMY;
1576 goto found;
1579 sym->attr.proc = PROC_EXTERNAL;
1580 goto found;
1583 if (sym->attr.proc == PROC_MODULE
1584 || sym->attr.proc == PROC_ST_FUNCTION
1585 || sym->attr.proc == PROC_INTERNAL)
1586 goto found;
1588 if (sym->attr.intrinsic)
1590 m = gfc_intrinsic_func_interface (expr, 1);
1591 if (m == MATCH_YES)
1592 return MATCH_YES;
1593 if (m == MATCH_NO)
1594 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1595 "with an intrinsic", sym->name, &expr->where);
1597 return MATCH_ERROR;
1600 return MATCH_NO;
1602 found:
1603 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1605 expr->ts = sym->ts;
1606 expr->value.function.name = sym->name;
1607 expr->value.function.esym = sym;
1608 if (sym->as != NULL)
1609 expr->rank = sym->as->rank;
1611 return MATCH_YES;
1615 static try
1616 resolve_specific_f (gfc_expr *expr)
1618 gfc_symbol *sym;
1619 match m;
1621 sym = expr->symtree->n.sym;
1623 for (;;)
1625 m = resolve_specific_f0 (sym, expr);
1626 if (m == MATCH_YES)
1627 return SUCCESS;
1628 if (m == MATCH_ERROR)
1629 return FAILURE;
1631 if (sym->ns->parent == NULL)
1632 break;
1634 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1636 if (sym == NULL)
1637 break;
1640 gfc_error ("Unable to resolve the specific function '%s' at %L",
1641 expr->symtree->n.sym->name, &expr->where);
1643 return SUCCESS;
1647 /* Resolve a procedure call not known to be generic nor specific. */
1649 static try
1650 resolve_unknown_f (gfc_expr *expr)
1652 gfc_symbol *sym;
1653 gfc_typespec *ts;
1655 sym = expr->symtree->n.sym;
1657 if (sym->attr.dummy)
1659 sym->attr.proc = PROC_DUMMY;
1660 expr->value.function.name = sym->name;
1661 goto set_type;
1664 /* See if we have an intrinsic function reference. */
1666 if (gfc_intrinsic_name (sym->name, 0))
1668 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1669 return SUCCESS;
1670 return FAILURE;
1673 /* The reference is to an external name. */
1675 sym->attr.proc = PROC_EXTERNAL;
1676 expr->value.function.name = sym->name;
1677 expr->value.function.esym = expr->symtree->n.sym;
1679 if (sym->as != NULL)
1680 expr->rank = sym->as->rank;
1682 /* Type of the expression is either the type of the symbol or the
1683 default type of the symbol. */
1685 set_type:
1686 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1688 if (sym->ts.type != BT_UNKNOWN)
1689 expr->ts = sym->ts;
1690 else
1692 ts = gfc_get_default_type (sym, sym->ns);
1694 if (ts->type == BT_UNKNOWN)
1696 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1697 sym->name, &expr->where);
1698 return FAILURE;
1700 else
1701 expr->ts = *ts;
1704 return SUCCESS;
1708 /* Return true, if the symbol is an external procedure. */
1709 static bool
1710 is_external_proc (gfc_symbol *sym)
1712 if (!sym->attr.dummy && !sym->attr.contained
1713 && !(sym->attr.intrinsic
1714 || gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1715 && sym->attr.proc != PROC_ST_FUNCTION
1716 && !sym->attr.use_assoc
1717 && sym->name)
1718 return true;
1719 else
1720 return false;
1724 /* Figure out if a function reference is pure or not. Also set the name
1725 of the function for a potential error message. Return nonzero if the
1726 function is PURE, zero if not. */
1727 static int
1728 pure_stmt_function (gfc_expr *, gfc_symbol *);
1730 static int
1731 pure_function (gfc_expr *e, const char **name)
1733 int pure;
1735 *name = NULL;
1737 if (e->symtree != NULL
1738 && e->symtree->n.sym != NULL
1739 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1740 return pure_stmt_function (e, e->symtree->n.sym);
1742 if (e->value.function.esym)
1744 pure = gfc_pure (e->value.function.esym);
1745 *name = e->value.function.esym->name;
1747 else if (e->value.function.isym)
1749 pure = e->value.function.isym->pure
1750 || e->value.function.isym->elemental;
1751 *name = e->value.function.isym->name;
1753 else
1755 /* Implicit functions are not pure. */
1756 pure = 0;
1757 *name = e->value.function.name;
1760 return pure;
1764 static bool
1765 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1766 int *f ATTRIBUTE_UNUSED)
1768 const char *name;
1770 /* Don't bother recursing into other statement functions
1771 since they will be checked individually for purity. */
1772 if (e->expr_type != EXPR_FUNCTION
1773 || !e->symtree
1774 || e->symtree->n.sym == sym
1775 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1776 return false;
1778 return pure_function (e, &name) ? false : true;
1782 static int
1783 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
1785 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
1789 static try
1790 is_scalar_expr_ptr (gfc_expr *expr)
1792 try retval = SUCCESS;
1793 gfc_ref *ref;
1794 int start;
1795 int end;
1797 /* See if we have a gfc_ref, which means we have a substring, array
1798 reference, or a component. */
1799 if (expr->ref != NULL)
1801 ref = expr->ref;
1802 while (ref->next != NULL)
1803 ref = ref->next;
1805 switch (ref->type)
1807 case REF_SUBSTRING:
1808 if (ref->u.ss.length != NULL
1809 && ref->u.ss.length->length != NULL
1810 && ref->u.ss.start
1811 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1812 && ref->u.ss.end
1813 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1815 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
1816 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
1817 if (end - start + 1 != 1)
1818 retval = FAILURE;
1820 else
1821 retval = FAILURE;
1822 break;
1823 case REF_ARRAY:
1824 if (ref->u.ar.type == AR_ELEMENT)
1825 retval = SUCCESS;
1826 else if (ref->u.ar.type == AR_FULL)
1828 /* The user can give a full array if the array is of size 1. */
1829 if (ref->u.ar.as != NULL
1830 && ref->u.ar.as->rank == 1
1831 && ref->u.ar.as->type == AS_EXPLICIT
1832 && ref->u.ar.as->lower[0] != NULL
1833 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
1834 && ref->u.ar.as->upper[0] != NULL
1835 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
1837 /* If we have a character string, we need to check if
1838 its length is one. */
1839 if (expr->ts.type == BT_CHARACTER)
1841 if (expr->ts.cl == NULL
1842 || expr->ts.cl->length == NULL
1843 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
1844 != 0)
1845 retval = FAILURE;
1847 else
1849 /* We have constant lower and upper bounds. If the
1850 difference between is 1, it can be considered a
1851 scalar. */
1852 start = (int) mpz_get_si
1853 (ref->u.ar.as->lower[0]->value.integer);
1854 end = (int) mpz_get_si
1855 (ref->u.ar.as->upper[0]->value.integer);
1856 if (end - start + 1 != 1)
1857 retval = FAILURE;
1860 else
1861 retval = FAILURE;
1863 else
1864 retval = FAILURE;
1865 break;
1866 default:
1867 retval = SUCCESS;
1868 break;
1871 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
1873 /* Character string. Make sure it's of length 1. */
1874 if (expr->ts.cl == NULL
1875 || expr->ts.cl->length == NULL
1876 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
1877 retval = FAILURE;
1879 else if (expr->rank != 0)
1880 retval = FAILURE;
1882 return retval;
1886 /* Match one of the iso_c_binding functions (c_associated or c_loc)
1887 and, in the case of c_associated, set the binding label based on
1888 the arguments. */
1890 static try
1891 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
1892 gfc_symbol **new_sym)
1894 char name[GFC_MAX_SYMBOL_LEN + 1];
1895 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
1896 int optional_arg = 0;
1897 try retval = SUCCESS;
1898 gfc_symbol *args_sym;
1899 gfc_typespec *arg_ts;
1900 gfc_ref *parent_ref;
1901 gfc_ref *curr_ref;
1903 if (args->expr->expr_type == EXPR_CONSTANT
1904 || args->expr->expr_type == EXPR_OP
1905 || args->expr->expr_type == EXPR_NULL)
1907 gfc_error ("Argument to '%s' at %L is not a variable",
1908 sym->name, &(args->expr->where));
1909 return FAILURE;
1912 args_sym = args->expr->symtree->n.sym;
1914 /* The typespec for the actual arg should be that stored in the expr
1915 and not necessarily that of the expr symbol (args_sym), because
1916 the actual expression could be a part-ref of the expr symbol. */
1917 arg_ts = &(args->expr->ts);
1919 /* Get the parent reference (if any) for the expression. This happens for
1920 cases such as a%b%c. */
1921 parent_ref = args->expr->ref;
1922 curr_ref = NULL;
1923 if (parent_ref != NULL)
1925 curr_ref = parent_ref->next;
1926 while (curr_ref != NULL && curr_ref->next != NULL)
1928 parent_ref = curr_ref;
1929 curr_ref = curr_ref->next;
1933 /* If curr_ref is non-NULL, we had a part-ref expression. If the curr_ref
1934 is for a REF_COMPONENT, then we need to use it as the parent_ref for
1935 the name, etc. Otherwise, the current parent_ref should be correct. */
1936 if (curr_ref != NULL && curr_ref->type == REF_COMPONENT)
1937 parent_ref = curr_ref;
1939 if (parent_ref == args->expr->ref)
1940 parent_ref = NULL;
1941 else if (parent_ref != NULL && parent_ref->type != REF_COMPONENT)
1942 gfc_internal_error ("Unexpected expression reference type in "
1943 "gfc_iso_c_func_interface");
1945 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
1947 /* If the user gave two args then they are providing something for
1948 the optional arg (the second cptr). Therefore, set the name and
1949 binding label to the c_associated for two cptrs. Otherwise,
1950 set c_associated to expect one cptr. */
1951 if (args->next)
1953 /* two args. */
1954 sprintf (name, "%s_2", sym->name);
1955 sprintf (binding_label, "%s_2", sym->binding_label);
1956 optional_arg = 1;
1958 else
1960 /* one arg. */
1961 sprintf (name, "%s_1", sym->name);
1962 sprintf (binding_label, "%s_1", sym->binding_label);
1963 optional_arg = 0;
1966 /* Get a new symbol for the version of c_associated that
1967 will get called. */
1968 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
1970 else if (sym->intmod_sym_id == ISOCBINDING_LOC
1971 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1973 sprintf (name, "%s", sym->name);
1974 sprintf (binding_label, "%s", sym->binding_label);
1976 /* Error check the call. */
1977 if (args->next != NULL)
1979 gfc_error_now ("More actual than formal arguments in '%s' "
1980 "call at %L", name, &(args->expr->where));
1981 retval = FAILURE;
1983 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
1985 /* Make sure we have either the target or pointer attribute. */
1986 if (!(args_sym->attr.target)
1987 && !(args_sym->attr.pointer)
1988 && (parent_ref == NULL ||
1989 !parent_ref->u.c.component->pointer))
1991 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
1992 "a TARGET or an associated pointer",
1993 args_sym->name,
1994 sym->name, &(args->expr->where));
1995 retval = FAILURE;
1998 /* See if we have interoperable type and type param. */
1999 if (verify_c_interop (arg_ts,
2000 (parent_ref ? parent_ref->u.c.component->name
2001 : args_sym->name),
2002 &(args->expr->where)) == SUCCESS
2003 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2005 if (args_sym->attr.target == 1)
2007 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2008 has the target attribute and is interoperable. */
2009 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2010 allocatable variable that has the TARGET attribute and
2011 is not an array of zero size. */
2012 if (args_sym->attr.allocatable == 1)
2014 if (args_sym->attr.dimension != 0
2015 && (args_sym->as && args_sym->as->rank == 0))
2017 gfc_error_now ("Allocatable variable '%s' used as a "
2018 "parameter to '%s' at %L must not be "
2019 "an array of zero size",
2020 args_sym->name, sym->name,
2021 &(args->expr->where));
2022 retval = FAILURE;
2025 else
2027 /* A non-allocatable target variable with C
2028 interoperable type and type parameters must be
2029 interoperable. */
2030 if (args_sym && args_sym->attr.dimension)
2032 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2034 gfc_error ("Assumed-shape array '%s' at %L "
2035 "cannot be an argument to the "
2036 "procedure '%s' because "
2037 "it is not C interoperable",
2038 args_sym->name,
2039 &(args->expr->where), sym->name);
2040 retval = FAILURE;
2042 else if (args_sym->as->type == AS_DEFERRED)
2044 gfc_error ("Deferred-shape array '%s' at %L "
2045 "cannot be an argument to the "
2046 "procedure '%s' because "
2047 "it is not C interoperable",
2048 args_sym->name,
2049 &(args->expr->where), sym->name);
2050 retval = FAILURE;
2054 /* Make sure it's not a character string. Arrays of
2055 any type should be ok if the variable is of a C
2056 interoperable type. */
2057 if (arg_ts->type == BT_CHARACTER)
2058 if (arg_ts->cl != NULL
2059 && (arg_ts->cl->length == NULL
2060 || arg_ts->cl->length->expr_type
2061 != EXPR_CONSTANT
2062 || mpz_cmp_si
2063 (arg_ts->cl->length->value.integer, 1)
2064 != 0)
2065 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2067 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2068 "at %L must have a length of 1",
2069 args_sym->name, sym->name,
2070 &(args->expr->where));
2071 retval = FAILURE;
2075 else if ((args_sym->attr.pointer == 1 ||
2076 (parent_ref != NULL
2077 && parent_ref->u.c.component->pointer))
2078 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2080 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2081 scalar pointer. */
2082 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2083 "associated scalar POINTER", args_sym->name,
2084 sym->name, &(args->expr->where));
2085 retval = FAILURE;
2088 else
2090 /* The parameter is not required to be C interoperable. If it
2091 is not C interoperable, it must be a nonpolymorphic scalar
2092 with no length type parameters. It still must have either
2093 the pointer or target attribute, and it can be
2094 allocatable (but must be allocated when c_loc is called). */
2095 if (args->expr->rank != 0
2096 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2098 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2099 "scalar", args_sym->name, sym->name,
2100 &(args->expr->where));
2101 retval = FAILURE;
2103 else if (arg_ts->type == BT_CHARACTER
2104 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2106 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2107 "%L must have a length of 1",
2108 args_sym->name, sym->name,
2109 &(args->expr->where));
2110 retval = FAILURE;
2114 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2116 if (args_sym->attr.flavor != FL_PROCEDURE)
2118 /* TODO: Update this error message to allow for procedure
2119 pointers once they are implemented. */
2120 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2121 "procedure",
2122 args_sym->name, sym->name,
2123 &(args->expr->where));
2124 retval = FAILURE;
2126 else if (args_sym->attr.is_bind_c != 1)
2128 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2129 "BIND(C)",
2130 args_sym->name, sym->name,
2131 &(args->expr->where));
2132 retval = FAILURE;
2136 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2137 *new_sym = sym;
2139 else
2141 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2142 "iso_c_binding function: '%s'!\n", sym->name);
2145 return retval;
2149 /* Resolve a function call, which means resolving the arguments, then figuring
2150 out which entity the name refers to. */
2151 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2152 to INTENT(OUT) or INTENT(INOUT). */
2154 static try
2155 resolve_function (gfc_expr *expr)
2157 gfc_actual_arglist *arg;
2158 gfc_symbol *sym;
2159 const char *name;
2160 try t;
2161 int temp;
2162 procedure_type p = PROC_INTRINSIC;
2164 sym = NULL;
2165 if (expr->symtree)
2166 sym = expr->symtree->n.sym;
2168 if (sym && sym->attr.flavor == FL_VARIABLE)
2170 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2171 return FAILURE;
2174 if (sym && sym->attr.abstract)
2176 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2177 sym->name, &expr->where);
2178 return FAILURE;
2181 /* If the procedure is external, check for usage. */
2182 if (sym && is_external_proc (sym))
2183 resolve_global_procedure (sym, &expr->where, 0);
2185 /* Switch off assumed size checking and do this again for certain kinds
2186 of procedure, once the procedure itself is resolved. */
2187 need_full_assumed_size++;
2189 if (expr->symtree && expr->symtree->n.sym)
2190 p = expr->symtree->n.sym->attr.proc;
2192 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
2193 return FAILURE;
2195 /* Need to setup the call to the correct c_associated, depending on
2196 the number of cptrs to user gives to compare. */
2197 if (sym && sym->attr.is_iso_c == 1)
2199 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2200 == FAILURE)
2201 return FAILURE;
2203 /* Get the symtree for the new symbol (resolved func).
2204 the old one will be freed later, when it's no longer used. */
2205 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2208 /* Resume assumed_size checking. */
2209 need_full_assumed_size--;
2211 if (sym && sym->ts.type == BT_CHARACTER
2212 && sym->ts.cl
2213 && sym->ts.cl->length == NULL
2214 && !sym->attr.dummy
2215 && expr->value.function.esym == NULL
2216 && !sym->attr.contained)
2218 /* Internal procedures are taken care of in resolve_contained_fntype. */
2219 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2220 "be used at %L since it is not a dummy argument",
2221 sym->name, &expr->where);
2222 return FAILURE;
2225 /* See if function is already resolved. */
2227 if (expr->value.function.name != NULL)
2229 if (expr->ts.type == BT_UNKNOWN)
2230 expr->ts = sym->ts;
2231 t = SUCCESS;
2233 else
2235 /* Apply the rules of section 14.1.2. */
2237 switch (procedure_kind (sym))
2239 case PTYPE_GENERIC:
2240 t = resolve_generic_f (expr);
2241 break;
2243 case PTYPE_SPECIFIC:
2244 t = resolve_specific_f (expr);
2245 break;
2247 case PTYPE_UNKNOWN:
2248 t = resolve_unknown_f (expr);
2249 break;
2251 default:
2252 gfc_internal_error ("resolve_function(): bad function type");
2256 /* If the expression is still a function (it might have simplified),
2257 then we check to see if we are calling an elemental function. */
2259 if (expr->expr_type != EXPR_FUNCTION)
2260 return t;
2262 temp = need_full_assumed_size;
2263 need_full_assumed_size = 0;
2265 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2266 return FAILURE;
2268 if (omp_workshare_flag
2269 && expr->value.function.esym
2270 && ! gfc_elemental (expr->value.function.esym))
2272 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2273 "in WORKSHARE construct", expr->value.function.esym->name,
2274 &expr->where);
2275 t = FAILURE;
2278 #define GENERIC_ID expr->value.function.isym->id
2279 else if (expr->value.function.actual != NULL
2280 && expr->value.function.isym != NULL
2281 && GENERIC_ID != GFC_ISYM_LBOUND
2282 && GENERIC_ID != GFC_ISYM_LEN
2283 && GENERIC_ID != GFC_ISYM_LOC
2284 && GENERIC_ID != GFC_ISYM_PRESENT)
2286 /* Array intrinsics must also have the last upper bound of an
2287 assumed size array argument. UBOUND and SIZE have to be
2288 excluded from the check if the second argument is anything
2289 than a constant. */
2290 int inquiry;
2291 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
2292 || GENERIC_ID == GFC_ISYM_SIZE;
2294 for (arg = expr->value.function.actual; arg; arg = arg->next)
2296 if (inquiry && arg->next != NULL && arg->next->expr)
2298 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2299 break;
2301 if ((int)mpz_get_si (arg->next->expr->value.integer)
2302 < arg->expr->rank)
2303 break;
2306 if (arg->expr != NULL
2307 && arg->expr->rank > 0
2308 && resolve_assumed_size_actual (arg->expr))
2309 return FAILURE;
2312 #undef GENERIC_ID
2314 need_full_assumed_size = temp;
2315 name = NULL;
2317 if (!pure_function (expr, &name) && name)
2319 if (forall_flag)
2321 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2322 "FORALL %s", name, &expr->where,
2323 forall_flag == 2 ? "mask" : "block");
2324 t = FAILURE;
2326 else if (gfc_pure (NULL))
2328 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2329 "procedure within a PURE procedure", name, &expr->where);
2330 t = FAILURE;
2334 /* Functions without the RECURSIVE attribution are not allowed to
2335 * call themselves. */
2336 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2338 gfc_symbol *esym, *proc;
2339 esym = expr->value.function.esym;
2340 proc = gfc_current_ns->proc_name;
2341 if (esym == proc)
2343 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
2344 "RECURSIVE", name, &expr->where);
2345 t = FAILURE;
2348 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
2349 && esym->ns->entries->sym == proc->ns->entries->sym)
2351 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
2352 "'%s' is not declared as RECURSIVE",
2353 esym->name, &expr->where, esym->ns->entries->sym->name);
2354 t = FAILURE;
2358 /* Character lengths of use associated functions may contains references to
2359 symbols not referenced from the current program unit otherwise. Make sure
2360 those symbols are marked as referenced. */
2362 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2363 && expr->value.function.esym->attr.use_assoc)
2365 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2368 if (t == SUCCESS)
2369 find_noncopying_intrinsics (expr->value.function.esym,
2370 expr->value.function.actual);
2372 /* Make sure that the expression has a typespec that works. */
2373 if (expr->ts.type == BT_UNKNOWN)
2375 if (expr->symtree->n.sym->result
2376 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2377 expr->ts = expr->symtree->n.sym->result->ts;
2380 return t;
2384 /************* Subroutine resolution *************/
2386 static void
2387 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2389 if (gfc_pure (sym))
2390 return;
2392 if (forall_flag)
2393 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2394 sym->name, &c->loc);
2395 else if (gfc_pure (NULL))
2396 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2397 &c->loc);
2401 static match
2402 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2404 gfc_symbol *s;
2406 if (sym->attr.generic)
2408 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2409 if (s != NULL)
2411 c->resolved_sym = s;
2412 pure_subroutine (c, s);
2413 return MATCH_YES;
2416 /* TODO: Need to search for elemental references in generic interface. */
2419 if (sym->attr.intrinsic)
2420 return gfc_intrinsic_sub_interface (c, 0);
2422 return MATCH_NO;
2426 static try
2427 resolve_generic_s (gfc_code *c)
2429 gfc_symbol *sym;
2430 match m;
2432 sym = c->symtree->n.sym;
2434 for (;;)
2436 m = resolve_generic_s0 (c, sym);
2437 if (m == MATCH_YES)
2438 return SUCCESS;
2439 else if (m == MATCH_ERROR)
2440 return FAILURE;
2442 generic:
2443 if (sym->ns->parent == NULL)
2444 break;
2445 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2447 if (sym == NULL)
2448 break;
2449 if (!generic_sym (sym))
2450 goto generic;
2453 /* Last ditch attempt. See if the reference is to an intrinsic
2454 that possesses a matching interface. 14.1.2.4 */
2455 sym = c->symtree->n.sym;
2457 if (!gfc_intrinsic_name (sym->name, 1))
2459 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2460 sym->name, &c->loc);
2461 return FAILURE;
2464 m = gfc_intrinsic_sub_interface (c, 0);
2465 if (m == MATCH_YES)
2466 return SUCCESS;
2467 if (m == MATCH_NO)
2468 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2469 "intrinsic subroutine interface", sym->name, &c->loc);
2471 return FAILURE;
2475 /* Set the name and binding label of the subroutine symbol in the call
2476 expression represented by 'c' to include the type and kind of the
2477 second parameter. This function is for resolving the appropriate
2478 version of c_f_pointer() and c_f_procpointer(). For example, a
2479 call to c_f_pointer() for a default integer pointer could have a
2480 name of c_f_pointer_i4. If no second arg exists, which is an error
2481 for these two functions, it defaults to the generic symbol's name
2482 and binding label. */
2484 static void
2485 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2486 char *name, char *binding_label)
2488 gfc_expr *arg = NULL;
2489 char type;
2490 int kind;
2492 /* The second arg of c_f_pointer and c_f_procpointer determines
2493 the type and kind for the procedure name. */
2494 arg = c->ext.actual->next->expr;
2496 if (arg != NULL)
2498 /* Set up the name to have the given symbol's name,
2499 plus the type and kind. */
2500 /* a derived type is marked with the type letter 'u' */
2501 if (arg->ts.type == BT_DERIVED)
2503 type = 'd';
2504 kind = 0; /* set the kind as 0 for now */
2506 else
2508 type = gfc_type_letter (arg->ts.type);
2509 kind = arg->ts.kind;
2512 if (arg->ts.type == BT_CHARACTER)
2513 /* Kind info for character strings not needed. */
2514 kind = 0;
2516 sprintf (name, "%s_%c%d", sym->name, type, kind);
2517 /* Set up the binding label as the given symbol's label plus
2518 the type and kind. */
2519 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2521 else
2523 /* If the second arg is missing, set the name and label as
2524 was, cause it should at least be found, and the missing
2525 arg error will be caught by compare_parameters(). */
2526 sprintf (name, "%s", sym->name);
2527 sprintf (binding_label, "%s", sym->binding_label);
2530 return;
2534 /* Resolve a generic version of the iso_c_binding procedure given
2535 (sym) to the specific one based on the type and kind of the
2536 argument(s). Currently, this function resolves c_f_pointer() and
2537 c_f_procpointer based on the type and kind of the second argument
2538 (FPTR). Other iso_c_binding procedures aren't specially handled.
2539 Upon successfully exiting, c->resolved_sym will hold the resolved
2540 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2541 otherwise. */
2543 match
2544 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2546 gfc_symbol *new_sym;
2547 /* this is fine, since we know the names won't use the max */
2548 char name[GFC_MAX_SYMBOL_LEN + 1];
2549 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2550 /* default to success; will override if find error */
2551 match m = MATCH_YES;
2553 /* Make sure the actual arguments are in the necessary order (based on the
2554 formal args) before resolving. */
2555 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2557 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2558 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2560 set_name_and_label (c, sym, name, binding_label);
2562 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2564 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2566 /* Make sure we got a third arg if the second arg has non-zero
2567 rank. We must also check that the type and rank are
2568 correct since we short-circuit this check in
2569 gfc_procedure_use() (called above to sort actual args). */
2570 if (c->ext.actual->next->expr->rank != 0)
2572 if(c->ext.actual->next->next == NULL
2573 || c->ext.actual->next->next->expr == NULL)
2575 m = MATCH_ERROR;
2576 gfc_error ("Missing SHAPE parameter for call to %s "
2577 "at %L", sym->name, &(c->loc));
2579 else if (c->ext.actual->next->next->expr->ts.type
2580 != BT_INTEGER
2581 || c->ext.actual->next->next->expr->rank != 1)
2583 m = MATCH_ERROR;
2584 gfc_error ("SHAPE parameter for call to %s at %L must "
2585 "be a rank 1 INTEGER array", sym->name,
2586 &(c->loc));
2592 if (m != MATCH_ERROR)
2594 /* the 1 means to add the optional arg to formal list */
2595 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2597 /* for error reporting, say it's declared where the original was */
2598 new_sym->declared_at = sym->declared_at;
2601 else
2603 /* no differences for c_loc or c_funloc */
2604 new_sym = sym;
2607 /* set the resolved symbol */
2608 if (m != MATCH_ERROR)
2609 c->resolved_sym = new_sym;
2610 else
2611 c->resolved_sym = sym;
2613 return m;
2617 /* Resolve a subroutine call known to be specific. */
2619 static match
2620 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2622 match m;
2624 /* See if we have an intrinsic interface. */
2625 if (sym->interface != NULL && !sym->interface->attr.abstract
2626 && !sym->interface->attr.subroutine)
2628 gfc_intrinsic_sym *isym;
2630 isym = gfc_find_function (sym->interface->name);
2632 /* Existance of isym should be checked already. */
2633 gcc_assert (isym);
2635 sym->ts = isym->ts;
2636 sym->attr.function = 1;
2637 goto found;
2640 if(sym->attr.is_iso_c)
2642 m = gfc_iso_c_sub_interface (c,sym);
2643 return m;
2646 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2648 if (sym->attr.dummy)
2650 sym->attr.proc = PROC_DUMMY;
2651 goto found;
2654 sym->attr.proc = PROC_EXTERNAL;
2655 goto found;
2658 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2659 goto found;
2661 if (sym->attr.intrinsic)
2663 m = gfc_intrinsic_sub_interface (c, 1);
2664 if (m == MATCH_YES)
2665 return MATCH_YES;
2666 if (m == MATCH_NO)
2667 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2668 "with an intrinsic", sym->name, &c->loc);
2670 return MATCH_ERROR;
2673 return MATCH_NO;
2675 found:
2676 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2678 c->resolved_sym = sym;
2679 pure_subroutine (c, sym);
2681 return MATCH_YES;
2685 static try
2686 resolve_specific_s (gfc_code *c)
2688 gfc_symbol *sym;
2689 match m;
2691 sym = c->symtree->n.sym;
2693 for (;;)
2695 m = resolve_specific_s0 (c, sym);
2696 if (m == MATCH_YES)
2697 return SUCCESS;
2698 if (m == MATCH_ERROR)
2699 return FAILURE;
2701 if (sym->ns->parent == NULL)
2702 break;
2704 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2706 if (sym == NULL)
2707 break;
2710 sym = c->symtree->n.sym;
2711 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2712 sym->name, &c->loc);
2714 return FAILURE;
2718 /* Resolve a subroutine call not known to be generic nor specific. */
2720 static try
2721 resolve_unknown_s (gfc_code *c)
2723 gfc_symbol *sym;
2725 sym = c->symtree->n.sym;
2727 if (sym->attr.dummy)
2729 sym->attr.proc = PROC_DUMMY;
2730 goto found;
2733 /* See if we have an intrinsic function reference. */
2735 if (gfc_intrinsic_name (sym->name, 1))
2737 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2738 return SUCCESS;
2739 return FAILURE;
2742 /* The reference is to an external name. */
2744 found:
2745 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2747 c->resolved_sym = sym;
2749 pure_subroutine (c, sym);
2751 return SUCCESS;
2755 /* Resolve a subroutine call. Although it was tempting to use the same code
2756 for functions, subroutines and functions are stored differently and this
2757 makes things awkward. */
2759 static try
2760 resolve_call (gfc_code *c)
2762 try t;
2763 procedure_type ptype = PROC_INTRINSIC;
2765 if (c->symtree && c->symtree->n.sym
2766 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
2768 gfc_error ("'%s' at %L has a type, which is not consistent with "
2769 "the CALL at %L", c->symtree->n.sym->name,
2770 &c->symtree->n.sym->declared_at, &c->loc);
2771 return FAILURE;
2774 /* If external, check for usage. */
2775 if (c->symtree && is_external_proc (c->symtree->n.sym))
2776 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
2778 /* Subroutines without the RECURSIVE attribution are not allowed to
2779 * call themselves. */
2780 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
2782 gfc_symbol *csym, *proc;
2783 csym = c->symtree->n.sym;
2784 proc = gfc_current_ns->proc_name;
2785 if (csym == proc)
2787 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
2788 "RECURSIVE", csym->name, &c->loc);
2789 t = FAILURE;
2792 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
2793 && csym->ns->entries->sym == proc->ns->entries->sym)
2795 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
2796 "'%s' is not declared as RECURSIVE",
2797 csym->name, &c->loc, csym->ns->entries->sym->name);
2798 t = FAILURE;
2802 /* Switch off assumed size checking and do this again for certain kinds
2803 of procedure, once the procedure itself is resolved. */
2804 need_full_assumed_size++;
2806 if (c->symtree && c->symtree->n.sym)
2807 ptype = c->symtree->n.sym->attr.proc;
2809 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
2810 return FAILURE;
2812 /* Resume assumed_size checking. */
2813 need_full_assumed_size--;
2815 t = SUCCESS;
2816 if (c->resolved_sym == NULL)
2817 switch (procedure_kind (c->symtree->n.sym))
2819 case PTYPE_GENERIC:
2820 t = resolve_generic_s (c);
2821 break;
2823 case PTYPE_SPECIFIC:
2824 t = resolve_specific_s (c);
2825 break;
2827 case PTYPE_UNKNOWN:
2828 t = resolve_unknown_s (c);
2829 break;
2831 default:
2832 gfc_internal_error ("resolve_subroutine(): bad function type");
2835 /* Some checks of elemental subroutine actual arguments. */
2836 if (resolve_elemental_actual (NULL, c) == FAILURE)
2837 return FAILURE;
2839 if (t == SUCCESS)
2840 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2841 return t;
2845 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2846 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2847 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2848 if their shapes do not match. If either op1->shape or op2->shape is
2849 NULL, return SUCCESS. */
2851 static try
2852 compare_shapes (gfc_expr *op1, gfc_expr *op2)
2854 try t;
2855 int i;
2857 t = SUCCESS;
2859 if (op1->shape != NULL && op2->shape != NULL)
2861 for (i = 0; i < op1->rank; i++)
2863 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2865 gfc_error ("Shapes for operands at %L and %L are not conformable",
2866 &op1->where, &op2->where);
2867 t = FAILURE;
2868 break;
2873 return t;
2877 /* Resolve an operator expression node. This can involve replacing the
2878 operation with a user defined function call. */
2880 static try
2881 resolve_operator (gfc_expr *e)
2883 gfc_expr *op1, *op2;
2884 char msg[200];
2885 bool dual_locus_error;
2886 try t;
2888 /* Resolve all subnodes-- give them types. */
2890 switch (e->value.op.operator)
2892 default:
2893 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2894 return FAILURE;
2896 /* Fall through... */
2898 case INTRINSIC_NOT:
2899 case INTRINSIC_UPLUS:
2900 case INTRINSIC_UMINUS:
2901 case INTRINSIC_PARENTHESES:
2902 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2903 return FAILURE;
2904 break;
2907 /* Typecheck the new node. */
2909 op1 = e->value.op.op1;
2910 op2 = e->value.op.op2;
2911 dual_locus_error = false;
2913 if ((op1 && op1->expr_type == EXPR_NULL)
2914 || (op2 && op2->expr_type == EXPR_NULL))
2916 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
2917 goto bad_op;
2920 switch (e->value.op.operator)
2922 case INTRINSIC_UPLUS:
2923 case INTRINSIC_UMINUS:
2924 if (op1->ts.type == BT_INTEGER
2925 || op1->ts.type == BT_REAL
2926 || op1->ts.type == BT_COMPLEX)
2928 e->ts = op1->ts;
2929 break;
2932 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2933 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2934 goto bad_op;
2936 case INTRINSIC_PLUS:
2937 case INTRINSIC_MINUS:
2938 case INTRINSIC_TIMES:
2939 case INTRINSIC_DIVIDE:
2940 case INTRINSIC_POWER:
2941 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2943 gfc_type_convert_binary (e);
2944 break;
2947 sprintf (msg,
2948 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2949 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2950 gfc_typename (&op2->ts));
2951 goto bad_op;
2953 case INTRINSIC_CONCAT:
2954 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2956 e->ts.type = BT_CHARACTER;
2957 e->ts.kind = op1->ts.kind;
2958 break;
2961 sprintf (msg,
2962 _("Operands of string concatenation operator at %%L are %s/%s"),
2963 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2964 goto bad_op;
2966 case INTRINSIC_AND:
2967 case INTRINSIC_OR:
2968 case INTRINSIC_EQV:
2969 case INTRINSIC_NEQV:
2970 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2972 e->ts.type = BT_LOGICAL;
2973 e->ts.kind = gfc_kind_max (op1, op2);
2974 if (op1->ts.kind < e->ts.kind)
2975 gfc_convert_type (op1, &e->ts, 2);
2976 else if (op2->ts.kind < e->ts.kind)
2977 gfc_convert_type (op2, &e->ts, 2);
2978 break;
2981 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2982 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2983 gfc_typename (&op2->ts));
2985 goto bad_op;
2987 case INTRINSIC_NOT:
2988 if (op1->ts.type == BT_LOGICAL)
2990 e->ts.type = BT_LOGICAL;
2991 e->ts.kind = op1->ts.kind;
2992 break;
2995 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
2996 gfc_typename (&op1->ts));
2997 goto bad_op;
2999 case INTRINSIC_GT:
3000 case INTRINSIC_GT_OS:
3001 case INTRINSIC_GE:
3002 case INTRINSIC_GE_OS:
3003 case INTRINSIC_LT:
3004 case INTRINSIC_LT_OS:
3005 case INTRINSIC_LE:
3006 case INTRINSIC_LE_OS:
3007 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3009 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3010 goto bad_op;
3013 /* Fall through... */
3015 case INTRINSIC_EQ:
3016 case INTRINSIC_EQ_OS:
3017 case INTRINSIC_NE:
3018 case INTRINSIC_NE_OS:
3019 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
3021 e->ts.type = BT_LOGICAL;
3022 e->ts.kind = gfc_default_logical_kind;
3023 break;
3026 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3028 gfc_type_convert_binary (e);
3030 e->ts.type = BT_LOGICAL;
3031 e->ts.kind = gfc_default_logical_kind;
3032 break;
3035 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3036 sprintf (msg,
3037 _("Logicals at %%L must be compared with %s instead of %s"),
3038 (e->value.op.operator == INTRINSIC_EQ
3039 || e->value.op.operator == INTRINSIC_EQ_OS)
3040 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.operator));
3041 else
3042 sprintf (msg,
3043 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3044 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
3045 gfc_typename (&op2->ts));
3047 goto bad_op;
3049 case INTRINSIC_USER:
3050 if (e->value.op.uop->operator == NULL)
3051 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3052 else if (op2 == NULL)
3053 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3054 e->value.op.uop->name, gfc_typename (&op1->ts));
3055 else
3056 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3057 e->value.op.uop->name, gfc_typename (&op1->ts),
3058 gfc_typename (&op2->ts));
3060 goto bad_op;
3062 case INTRINSIC_PARENTHESES:
3063 e->ts = op1->ts;
3064 if (e->ts.type == BT_CHARACTER)
3065 e->ts.cl = op1->ts.cl;
3066 break;
3068 default:
3069 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3072 /* Deal with arrayness of an operand through an operator. */
3074 t = SUCCESS;
3076 switch (e->value.op.operator)
3078 case INTRINSIC_PLUS:
3079 case INTRINSIC_MINUS:
3080 case INTRINSIC_TIMES:
3081 case INTRINSIC_DIVIDE:
3082 case INTRINSIC_POWER:
3083 case INTRINSIC_CONCAT:
3084 case INTRINSIC_AND:
3085 case INTRINSIC_OR:
3086 case INTRINSIC_EQV:
3087 case INTRINSIC_NEQV:
3088 case INTRINSIC_EQ:
3089 case INTRINSIC_EQ_OS:
3090 case INTRINSIC_NE:
3091 case INTRINSIC_NE_OS:
3092 case INTRINSIC_GT:
3093 case INTRINSIC_GT_OS:
3094 case INTRINSIC_GE:
3095 case INTRINSIC_GE_OS:
3096 case INTRINSIC_LT:
3097 case INTRINSIC_LT_OS:
3098 case INTRINSIC_LE:
3099 case INTRINSIC_LE_OS:
3101 if (op1->rank == 0 && op2->rank == 0)
3102 e->rank = 0;
3104 if (op1->rank == 0 && op2->rank != 0)
3106 e->rank = op2->rank;
3108 if (e->shape == NULL)
3109 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3112 if (op1->rank != 0 && op2->rank == 0)
3114 e->rank = op1->rank;
3116 if (e->shape == NULL)
3117 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3120 if (op1->rank != 0 && op2->rank != 0)
3122 if (op1->rank == op2->rank)
3124 e->rank = op1->rank;
3125 if (e->shape == NULL)
3127 t = compare_shapes(op1, op2);
3128 if (t == FAILURE)
3129 e->shape = NULL;
3130 else
3131 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3134 else
3136 /* Allow higher level expressions to work. */
3137 e->rank = 0;
3139 /* Try user-defined operators, and otherwise throw an error. */
3140 dual_locus_error = true;
3141 sprintf (msg,
3142 _("Inconsistent ranks for operator at %%L and %%L"));
3143 goto bad_op;
3147 break;
3149 case INTRINSIC_PARENTHESES:
3150 case INTRINSIC_NOT:
3151 case INTRINSIC_UPLUS:
3152 case INTRINSIC_UMINUS:
3153 /* Simply copy arrayness attribute */
3154 e->rank = op1->rank;
3156 if (e->shape == NULL)
3157 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3159 break;
3161 default:
3162 break;
3165 /* Attempt to simplify the expression. */
3166 if (t == SUCCESS)
3168 t = gfc_simplify_expr (e, 0);
3169 /* Some calls do not succeed in simplification and return FAILURE
3170 even though there is no error; eg. variable references to
3171 PARAMETER arrays. */
3172 if (!gfc_is_constant_expr (e))
3173 t = SUCCESS;
3175 return t;
3177 bad_op:
3179 if (gfc_extend_expr (e) == SUCCESS)
3180 return SUCCESS;
3182 if (dual_locus_error)
3183 gfc_error (msg, &op1->where, &op2->where);
3184 else
3185 gfc_error (msg, &e->where);
3187 return FAILURE;
3191 /************** Array resolution subroutines **************/
3193 typedef enum
3194 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3195 comparison;
3197 /* Compare two integer expressions. */
3199 static comparison
3200 compare_bound (gfc_expr *a, gfc_expr *b)
3202 int i;
3204 if (a == NULL || a->expr_type != EXPR_CONSTANT
3205 || b == NULL || b->expr_type != EXPR_CONSTANT)
3206 return CMP_UNKNOWN;
3208 /* If either of the types isn't INTEGER, we must have
3209 raised an error earlier. */
3211 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3212 return CMP_UNKNOWN;
3214 i = mpz_cmp (a->value.integer, b->value.integer);
3216 if (i < 0)
3217 return CMP_LT;
3218 if (i > 0)
3219 return CMP_GT;
3220 return CMP_EQ;
3224 /* Compare an integer expression with an integer. */
3226 static comparison
3227 compare_bound_int (gfc_expr *a, int b)
3229 int i;
3231 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3232 return CMP_UNKNOWN;
3234 if (a->ts.type != BT_INTEGER)
3235 gfc_internal_error ("compare_bound_int(): Bad expression");
3237 i = mpz_cmp_si (a->value.integer, b);
3239 if (i < 0)
3240 return CMP_LT;
3241 if (i > 0)
3242 return CMP_GT;
3243 return CMP_EQ;
3247 /* Compare an integer expression with a mpz_t. */
3249 static comparison
3250 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3252 int i;
3254 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3255 return CMP_UNKNOWN;
3257 if (a->ts.type != BT_INTEGER)
3258 gfc_internal_error ("compare_bound_int(): Bad expression");
3260 i = mpz_cmp (a->value.integer, b);
3262 if (i < 0)
3263 return CMP_LT;
3264 if (i > 0)
3265 return CMP_GT;
3266 return CMP_EQ;
3270 /* Compute the last value of a sequence given by a triplet.
3271 Return 0 if it wasn't able to compute the last value, or if the
3272 sequence if empty, and 1 otherwise. */
3274 static int
3275 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3276 gfc_expr *stride, mpz_t last)
3278 mpz_t rem;
3280 if (start == NULL || start->expr_type != EXPR_CONSTANT
3281 || end == NULL || end->expr_type != EXPR_CONSTANT
3282 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3283 return 0;
3285 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3286 || (stride != NULL && stride->ts.type != BT_INTEGER))
3287 return 0;
3289 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3291 if (compare_bound (start, end) == CMP_GT)
3292 return 0;
3293 mpz_set (last, end->value.integer);
3294 return 1;
3297 if (compare_bound_int (stride, 0) == CMP_GT)
3299 /* Stride is positive */
3300 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3301 return 0;
3303 else
3305 /* Stride is negative */
3306 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3307 return 0;
3310 mpz_init (rem);
3311 mpz_sub (rem, end->value.integer, start->value.integer);
3312 mpz_tdiv_r (rem, rem, stride->value.integer);
3313 mpz_sub (last, end->value.integer, rem);
3314 mpz_clear (rem);
3316 return 1;
3320 /* Compare a single dimension of an array reference to the array
3321 specification. */
3323 static try
3324 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3326 mpz_t last_value;
3328 /* Given start, end and stride values, calculate the minimum and
3329 maximum referenced indexes. */
3331 switch (ar->dimen_type[i])
3333 case DIMEN_VECTOR:
3334 break;
3336 case DIMEN_ELEMENT:
3337 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3339 gfc_warning ("Array reference at %L is out of bounds "
3340 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3341 mpz_get_si (ar->start[i]->value.integer),
3342 mpz_get_si (as->lower[i]->value.integer), i+1);
3343 return SUCCESS;
3345 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3347 gfc_warning ("Array reference at %L is out of bounds "
3348 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3349 mpz_get_si (ar->start[i]->value.integer),
3350 mpz_get_si (as->upper[i]->value.integer), i+1);
3351 return SUCCESS;
3354 break;
3356 case DIMEN_RANGE:
3358 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3359 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3361 comparison comp_start_end = compare_bound (AR_START, AR_END);
3363 /* Check for zero stride, which is not allowed. */
3364 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3366 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3367 return FAILURE;
3370 /* if start == len || (stride > 0 && start < len)
3371 || (stride < 0 && start > len),
3372 then the array section contains at least one element. In this
3373 case, there is an out-of-bounds access if
3374 (start < lower || start > upper). */
3375 if (compare_bound (AR_START, AR_END) == CMP_EQ
3376 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3377 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3378 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3379 && comp_start_end == CMP_GT))
3381 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3383 gfc_warning ("Lower array reference at %L is out of bounds "
3384 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3385 mpz_get_si (AR_START->value.integer),
3386 mpz_get_si (as->lower[i]->value.integer), i+1);
3387 return SUCCESS;
3389 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3391 gfc_warning ("Lower array reference at %L is out of bounds "
3392 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3393 mpz_get_si (AR_START->value.integer),
3394 mpz_get_si (as->upper[i]->value.integer), i+1);
3395 return SUCCESS;
3399 /* If we can compute the highest index of the array section,
3400 then it also has to be between lower and upper. */
3401 mpz_init (last_value);
3402 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3403 last_value))
3405 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3407 gfc_warning ("Upper array reference at %L is out of bounds "
3408 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3409 mpz_get_si (last_value),
3410 mpz_get_si (as->lower[i]->value.integer), i+1);
3411 mpz_clear (last_value);
3412 return SUCCESS;
3414 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3416 gfc_warning ("Upper array reference at %L is out of bounds "
3417 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3418 mpz_get_si (last_value),
3419 mpz_get_si (as->upper[i]->value.integer), i+1);
3420 mpz_clear (last_value);
3421 return SUCCESS;
3424 mpz_clear (last_value);
3426 #undef AR_START
3427 #undef AR_END
3429 break;
3431 default:
3432 gfc_internal_error ("check_dimension(): Bad array reference");
3435 return SUCCESS;
3439 /* Compare an array reference with an array specification. */
3441 static try
3442 compare_spec_to_ref (gfc_array_ref *ar)
3444 gfc_array_spec *as;
3445 int i;
3447 as = ar->as;
3448 i = as->rank - 1;
3449 /* TODO: Full array sections are only allowed as actual parameters. */
3450 if (as->type == AS_ASSUMED_SIZE
3451 && (/*ar->type == AR_FULL
3452 ||*/ (ar->type == AR_SECTION
3453 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3455 gfc_error ("Rightmost upper bound of assumed size array section "
3456 "not specified at %L", &ar->where);
3457 return FAILURE;
3460 if (ar->type == AR_FULL)
3461 return SUCCESS;
3463 if (as->rank != ar->dimen)
3465 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3466 &ar->where, ar->dimen, as->rank);
3467 return FAILURE;
3470 for (i = 0; i < as->rank; i++)
3471 if (check_dimension (i, ar, as) == FAILURE)
3472 return FAILURE;
3474 return SUCCESS;
3478 /* Resolve one part of an array index. */
3481 gfc_resolve_index (gfc_expr *index, int check_scalar)
3483 gfc_typespec ts;
3485 if (index == NULL)
3486 return SUCCESS;
3488 if (gfc_resolve_expr (index) == FAILURE)
3489 return FAILURE;
3491 if (check_scalar && index->rank != 0)
3493 gfc_error ("Array index at %L must be scalar", &index->where);
3494 return FAILURE;
3497 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3499 gfc_error ("Array index at %L must be of INTEGER type",
3500 &index->where);
3501 return FAILURE;
3504 if (index->ts.type == BT_REAL)
3505 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3506 &index->where) == FAILURE)
3507 return FAILURE;
3509 if (index->ts.kind != gfc_index_integer_kind
3510 || index->ts.type != BT_INTEGER)
3512 gfc_clear_ts (&ts);
3513 ts.type = BT_INTEGER;
3514 ts.kind = gfc_index_integer_kind;
3516 gfc_convert_type_warn (index, &ts, 2, 0);
3519 return SUCCESS;
3522 /* Resolve a dim argument to an intrinsic function. */
3525 gfc_resolve_dim_arg (gfc_expr *dim)
3527 if (dim == NULL)
3528 return SUCCESS;
3530 if (gfc_resolve_expr (dim) == FAILURE)
3531 return FAILURE;
3533 if (dim->rank != 0)
3535 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3536 return FAILURE;
3540 if (dim->ts.type != BT_INTEGER)
3542 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3543 return FAILURE;
3546 if (dim->ts.kind != gfc_index_integer_kind)
3548 gfc_typespec ts;
3550 ts.type = BT_INTEGER;
3551 ts.kind = gfc_index_integer_kind;
3553 gfc_convert_type_warn (dim, &ts, 2, 0);
3556 return SUCCESS;
3559 /* Given an expression that contains array references, update those array
3560 references to point to the right array specifications. While this is
3561 filled in during matching, this information is difficult to save and load
3562 in a module, so we take care of it here.
3564 The idea here is that the original array reference comes from the
3565 base symbol. We traverse the list of reference structures, setting
3566 the stored reference to references. Component references can
3567 provide an additional array specification. */
3569 static void
3570 find_array_spec (gfc_expr *e)
3572 gfc_array_spec *as;
3573 gfc_component *c;
3574 gfc_symbol *derived;
3575 gfc_ref *ref;
3577 as = e->symtree->n.sym->as;
3578 derived = NULL;
3580 for (ref = e->ref; ref; ref = ref->next)
3581 switch (ref->type)
3583 case REF_ARRAY:
3584 if (as == NULL)
3585 gfc_internal_error ("find_array_spec(): Missing spec");
3587 ref->u.ar.as = as;
3588 as = NULL;
3589 break;
3591 case REF_COMPONENT:
3592 if (derived == NULL)
3593 derived = e->symtree->n.sym->ts.derived;
3595 c = derived->components;
3597 for (; c; c = c->next)
3598 if (c == ref->u.c.component)
3600 /* Track the sequence of component references. */
3601 if (c->ts.type == BT_DERIVED)
3602 derived = c->ts.derived;
3603 break;
3606 if (c == NULL)
3607 gfc_internal_error ("find_array_spec(): Component not found");
3609 if (c->dimension)
3611 if (as != NULL)
3612 gfc_internal_error ("find_array_spec(): unused as(1)");
3613 as = c->as;
3616 break;
3618 case REF_SUBSTRING:
3619 break;
3622 if (as != NULL)
3623 gfc_internal_error ("find_array_spec(): unused as(2)");
3627 /* Resolve an array reference. */
3629 static try
3630 resolve_array_ref (gfc_array_ref *ar)
3632 int i, check_scalar;
3633 gfc_expr *e;
3635 for (i = 0; i < ar->dimen; i++)
3637 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3639 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3640 return FAILURE;
3641 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3642 return FAILURE;
3643 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3644 return FAILURE;
3646 e = ar->start[i];
3648 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3649 switch (e->rank)
3651 case 0:
3652 ar->dimen_type[i] = DIMEN_ELEMENT;
3653 break;
3655 case 1:
3656 ar->dimen_type[i] = DIMEN_VECTOR;
3657 if (e->expr_type == EXPR_VARIABLE
3658 && e->symtree->n.sym->ts.type == BT_DERIVED)
3659 ar->start[i] = gfc_get_parentheses (e);
3660 break;
3662 default:
3663 gfc_error ("Array index at %L is an array of rank %d",
3664 &ar->c_where[i], e->rank);
3665 return FAILURE;
3669 /* If the reference type is unknown, figure out what kind it is. */
3671 if (ar->type == AR_UNKNOWN)
3673 ar->type = AR_ELEMENT;
3674 for (i = 0; i < ar->dimen; i++)
3675 if (ar->dimen_type[i] == DIMEN_RANGE
3676 || ar->dimen_type[i] == DIMEN_VECTOR)
3678 ar->type = AR_SECTION;
3679 break;
3683 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3684 return FAILURE;
3686 return SUCCESS;
3690 static try
3691 resolve_substring (gfc_ref *ref)
3693 if (ref->u.ss.start != NULL)
3695 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3696 return FAILURE;
3698 if (ref->u.ss.start->ts.type != BT_INTEGER)
3700 gfc_error ("Substring start index at %L must be of type INTEGER",
3701 &ref->u.ss.start->where);
3702 return FAILURE;
3705 if (ref->u.ss.start->rank != 0)
3707 gfc_error ("Substring start index at %L must be scalar",
3708 &ref->u.ss.start->where);
3709 return FAILURE;
3712 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3713 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3714 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3716 gfc_error ("Substring start index at %L is less than one",
3717 &ref->u.ss.start->where);
3718 return FAILURE;
3722 if (ref->u.ss.end != NULL)
3724 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3725 return FAILURE;
3727 if (ref->u.ss.end->ts.type != BT_INTEGER)
3729 gfc_error ("Substring end index at %L must be of type INTEGER",
3730 &ref->u.ss.end->where);
3731 return FAILURE;
3734 if (ref->u.ss.end->rank != 0)
3736 gfc_error ("Substring end index at %L must be scalar",
3737 &ref->u.ss.end->where);
3738 return FAILURE;
3741 if (ref->u.ss.length != NULL
3742 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3743 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3744 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3746 gfc_error ("Substring end index at %L exceeds the string length",
3747 &ref->u.ss.start->where);
3748 return FAILURE;
3752 return SUCCESS;
3756 /* This function supplies missing substring charlens. */
3758 void
3759 gfc_resolve_substring_charlen (gfc_expr *e)
3761 gfc_ref *char_ref;
3762 gfc_expr *start, *end;
3764 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3765 if (char_ref->type == REF_SUBSTRING)
3766 break;
3768 if (!char_ref)
3769 return;
3771 gcc_assert (char_ref->next == NULL);
3773 if (e->ts.cl)
3775 if (e->ts.cl->length)
3776 gfc_free_expr (e->ts.cl->length);
3777 else if (e->expr_type == EXPR_VARIABLE
3778 && e->symtree->n.sym->attr.dummy)
3779 return;
3782 e->ts.type = BT_CHARACTER;
3783 e->ts.kind = gfc_default_character_kind;
3785 if (!e->ts.cl)
3787 e->ts.cl = gfc_get_charlen ();
3788 e->ts.cl->next = gfc_current_ns->cl_list;
3789 gfc_current_ns->cl_list = e->ts.cl;
3792 if (char_ref->u.ss.start)
3793 start = gfc_copy_expr (char_ref->u.ss.start);
3794 else
3795 start = gfc_int_expr (1);
3797 if (char_ref->u.ss.end)
3798 end = gfc_copy_expr (char_ref->u.ss.end);
3799 else if (e->expr_type == EXPR_VARIABLE)
3800 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
3801 else
3802 end = NULL;
3804 if (!start || !end)
3805 return;
3807 /* Length = (end - start +1). */
3808 e->ts.cl->length = gfc_subtract (end, start);
3809 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
3811 e->ts.cl->length->ts.type = BT_INTEGER;
3812 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
3814 /* Make sure that the length is simplified. */
3815 gfc_simplify_expr (e->ts.cl->length, 1);
3816 gfc_resolve_expr (e->ts.cl->length);
3820 /* Resolve subtype references. */
3822 static try
3823 resolve_ref (gfc_expr *expr)
3825 int current_part_dimension, n_components, seen_part_dimension;
3826 gfc_ref *ref;
3828 for (ref = expr->ref; ref; ref = ref->next)
3829 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
3831 find_array_spec (expr);
3832 break;
3835 for (ref = expr->ref; ref; ref = ref->next)
3836 switch (ref->type)
3838 case REF_ARRAY:
3839 if (resolve_array_ref (&ref->u.ar) == FAILURE)
3840 return FAILURE;
3841 break;
3843 case REF_COMPONENT:
3844 break;
3846 case REF_SUBSTRING:
3847 resolve_substring (ref);
3848 break;
3851 /* Check constraints on part references. */
3853 current_part_dimension = 0;
3854 seen_part_dimension = 0;
3855 n_components = 0;
3857 for (ref = expr->ref; ref; ref = ref->next)
3859 switch (ref->type)
3861 case REF_ARRAY:
3862 switch (ref->u.ar.type)
3864 case AR_FULL:
3865 case AR_SECTION:
3866 current_part_dimension = 1;
3867 break;
3869 case AR_ELEMENT:
3870 current_part_dimension = 0;
3871 break;
3873 case AR_UNKNOWN:
3874 gfc_internal_error ("resolve_ref(): Bad array reference");
3877 break;
3879 case REF_COMPONENT:
3880 if (current_part_dimension || seen_part_dimension)
3882 if (ref->u.c.component->pointer)
3884 gfc_error ("Component to the right of a part reference "
3885 "with nonzero rank must not have the POINTER "
3886 "attribute at %L", &expr->where);
3887 return FAILURE;
3889 else if (ref->u.c.component->allocatable)
3891 gfc_error ("Component to the right of a part reference "
3892 "with nonzero rank must not have the ALLOCATABLE "
3893 "attribute at %L", &expr->where);
3894 return FAILURE;
3898 n_components++;
3899 break;
3901 case REF_SUBSTRING:
3902 break;
3905 if (((ref->type == REF_COMPONENT && n_components > 1)
3906 || ref->next == NULL)
3907 && current_part_dimension
3908 && seen_part_dimension)
3910 gfc_error ("Two or more part references with nonzero rank must "
3911 "not be specified at %L", &expr->where);
3912 return FAILURE;
3915 if (ref->type == REF_COMPONENT)
3917 if (current_part_dimension)
3918 seen_part_dimension = 1;
3920 /* reset to make sure */
3921 current_part_dimension = 0;
3925 return SUCCESS;
3929 /* Given an expression, determine its shape. This is easier than it sounds.
3930 Leaves the shape array NULL if it is not possible to determine the shape. */
3932 static void
3933 expression_shape (gfc_expr *e)
3935 mpz_t array[GFC_MAX_DIMENSIONS];
3936 int i;
3938 if (e->rank == 0 || e->shape != NULL)
3939 return;
3941 for (i = 0; i < e->rank; i++)
3942 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
3943 goto fail;
3945 e->shape = gfc_get_shape (e->rank);
3947 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
3949 return;
3951 fail:
3952 for (i--; i >= 0; i--)
3953 mpz_clear (array[i]);
3957 /* Given a variable expression node, compute the rank of the expression by
3958 examining the base symbol and any reference structures it may have. */
3960 static void
3961 expression_rank (gfc_expr *e)
3963 gfc_ref *ref;
3964 int i, rank;
3966 if (e->ref == NULL)
3968 if (e->expr_type == EXPR_ARRAY)
3969 goto done;
3970 /* Constructors can have a rank different from one via RESHAPE(). */
3972 if (e->symtree == NULL)
3974 e->rank = 0;
3975 goto done;
3978 e->rank = (e->symtree->n.sym->as == NULL)
3979 ? 0 : e->symtree->n.sym->as->rank;
3980 goto done;
3983 rank = 0;
3985 for (ref = e->ref; ref; ref = ref->next)
3987 if (ref->type != REF_ARRAY)
3988 continue;
3990 if (ref->u.ar.type == AR_FULL)
3992 rank = ref->u.ar.as->rank;
3993 break;
3996 if (ref->u.ar.type == AR_SECTION)
3998 /* Figure out the rank of the section. */
3999 if (rank != 0)
4000 gfc_internal_error ("expression_rank(): Two array specs");
4002 for (i = 0; i < ref->u.ar.dimen; i++)
4003 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4004 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4005 rank++;
4007 break;
4011 e->rank = rank;
4013 done:
4014 expression_shape (e);
4018 /* Resolve a variable expression. */
4020 static try
4021 resolve_variable (gfc_expr *e)
4023 gfc_symbol *sym;
4024 try t;
4026 t = SUCCESS;
4028 if (e->symtree == NULL)
4029 return FAILURE;
4031 if (e->ref && resolve_ref (e) == FAILURE)
4032 return FAILURE;
4034 sym = e->symtree->n.sym;
4035 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
4037 e->ts.type = BT_PROCEDURE;
4038 return SUCCESS;
4041 if (sym->ts.type != BT_UNKNOWN)
4042 gfc_variable_attr (e, &e->ts);
4043 else
4045 /* Must be a simple variable reference. */
4046 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4047 return FAILURE;
4048 e->ts = sym->ts;
4051 if (check_assumed_size_reference (sym, e))
4052 return FAILURE;
4054 /* Deal with forward references to entries during resolve_code, to
4055 satisfy, at least partially, 12.5.2.5. */
4056 if (gfc_current_ns->entries
4057 && current_entry_id == sym->entry_id
4058 && cs_base
4059 && cs_base->current
4060 && cs_base->current->op != EXEC_ENTRY)
4062 gfc_entry_list *entry;
4063 gfc_formal_arglist *formal;
4064 int n;
4065 bool seen;
4067 /* If the symbol is a dummy... */
4068 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4070 entry = gfc_current_ns->entries;
4071 seen = false;
4073 /* ...test if the symbol is a parameter of previous entries. */
4074 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4075 for (formal = entry->sym->formal; formal; formal = formal->next)
4077 if (formal->sym && sym->name == formal->sym->name)
4078 seen = true;
4081 /* If it has not been seen as a dummy, this is an error. */
4082 if (!seen)
4084 if (specification_expr)
4085 gfc_error ("Variable '%s', used in a specification expression"
4086 ", is referenced at %L before the ENTRY statement "
4087 "in which it is a parameter",
4088 sym->name, &cs_base->current->loc);
4089 else
4090 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4091 "statement in which it is a parameter",
4092 sym->name, &cs_base->current->loc);
4093 t = FAILURE;
4097 /* Now do the same check on the specification expressions. */
4098 specification_expr = 1;
4099 if (sym->ts.type == BT_CHARACTER
4100 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4101 t = FAILURE;
4103 if (sym->as)
4104 for (n = 0; n < sym->as->rank; n++)
4106 specification_expr = 1;
4107 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4108 t = FAILURE;
4109 specification_expr = 1;
4110 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4111 t = FAILURE;
4113 specification_expr = 0;
4115 if (t == SUCCESS)
4116 /* Update the symbol's entry level. */
4117 sym->entry_id = current_entry_id + 1;
4120 return t;
4124 /* Checks to see that the correct symbol has been host associated.
4125 The only situation where this arises is that in which a twice
4126 contained function is parsed after the host association is made.
4127 Therefore, on detecting this, the line is rematched, having got
4128 rid of the existing references and actual_arg_list. */
4129 static bool
4130 check_host_association (gfc_expr *e)
4132 gfc_symbol *sym, *old_sym;
4133 locus temp_locus;
4134 gfc_expr *expr;
4135 int n;
4136 bool retval = e->expr_type == EXPR_FUNCTION;
4138 if (e->symtree == NULL || e->symtree->n.sym == NULL)
4139 return retval;
4141 old_sym = e->symtree->n.sym;
4143 if (old_sym->attr.use_assoc)
4144 return retval;
4146 if (gfc_current_ns->parent
4147 && old_sym->ns != gfc_current_ns)
4149 gfc_find_symbol (old_sym->name, gfc_current_ns, 1, &sym);
4150 if (sym && old_sym != sym
4151 && sym->attr.flavor == FL_PROCEDURE
4152 && sym->attr.contained)
4154 temp_locus = gfc_current_locus;
4155 gfc_current_locus = e->where;
4157 gfc_buffer_error (1);
4159 gfc_free_ref_list (e->ref);
4160 e->ref = NULL;
4162 if (retval)
4164 gfc_free_actual_arglist (e->value.function.actual);
4165 e->value.function.actual = NULL;
4168 if (e->shape != NULL)
4170 for (n = 0; n < e->rank; n++)
4171 mpz_clear (e->shape[n]);
4173 gfc_free (e->shape);
4176 gfc_match_rvalue (&expr);
4177 gfc_clear_error ();
4178 gfc_buffer_error (0);
4180 gcc_assert (expr && sym == expr->symtree->n.sym);
4182 *e = *expr;
4183 gfc_free (expr);
4184 sym->refs++;
4186 gfc_current_locus = temp_locus;
4189 /* This might have changed! */
4190 return e->expr_type == EXPR_FUNCTION;
4194 static void
4195 gfc_resolve_character_operator (gfc_expr *e)
4197 gfc_expr *op1 = e->value.op.op1;
4198 gfc_expr *op2 = e->value.op.op2;
4199 gfc_expr *e1 = NULL;
4200 gfc_expr *e2 = NULL;
4202 gcc_assert (e->value.op.operator == INTRINSIC_CONCAT);
4204 if (op1->ts.cl && op1->ts.cl->length)
4205 e1 = gfc_copy_expr (op1->ts.cl->length);
4206 else if (op1->expr_type == EXPR_CONSTANT)
4207 e1 = gfc_int_expr (op1->value.character.length);
4209 if (op2->ts.cl && op2->ts.cl->length)
4210 e2 = gfc_copy_expr (op2->ts.cl->length);
4211 else if (op2->expr_type == EXPR_CONSTANT)
4212 e2 = gfc_int_expr (op2->value.character.length);
4214 e->ts.cl = gfc_get_charlen ();
4215 e->ts.cl->next = gfc_current_ns->cl_list;
4216 gfc_current_ns->cl_list = e->ts.cl;
4218 if (!e1 || !e2)
4219 return;
4221 e->ts.cl->length = gfc_add (e1, e2);
4222 e->ts.cl->length->ts.type = BT_INTEGER;
4223 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4224 gfc_simplify_expr (e->ts.cl->length, 0);
4225 gfc_resolve_expr (e->ts.cl->length);
4227 return;
4231 /* Ensure that an character expression has a charlen and, if possible, a
4232 length expression. */
4234 static void
4235 fixup_charlen (gfc_expr *e)
4237 /* The cases fall through so that changes in expression type and the need
4238 for multiple fixes are picked up. In all circumstances, a charlen should
4239 be available for the middle end to hang a backend_decl on. */
4240 switch (e->expr_type)
4242 case EXPR_OP:
4243 gfc_resolve_character_operator (e);
4245 case EXPR_ARRAY:
4246 if (e->expr_type == EXPR_ARRAY)
4247 gfc_resolve_character_array_constructor (e);
4249 case EXPR_SUBSTRING:
4250 if (!e->ts.cl && e->ref)
4251 gfc_resolve_substring_charlen (e);
4253 default:
4254 if (!e->ts.cl)
4256 e->ts.cl = gfc_get_charlen ();
4257 e->ts.cl->next = gfc_current_ns->cl_list;
4258 gfc_current_ns->cl_list = e->ts.cl;
4261 break;
4266 /* Resolve an expression. That is, make sure that types of operands agree
4267 with their operators, intrinsic operators are converted to function calls
4268 for overloaded types and unresolved function references are resolved. */
4271 gfc_resolve_expr (gfc_expr *e)
4273 try t;
4275 if (e == NULL)
4276 return SUCCESS;
4278 switch (e->expr_type)
4280 case EXPR_OP:
4281 t = resolve_operator (e);
4282 break;
4284 case EXPR_FUNCTION:
4285 case EXPR_VARIABLE:
4287 if (check_host_association (e))
4288 t = resolve_function (e);
4289 else
4291 t = resolve_variable (e);
4292 if (t == SUCCESS)
4293 expression_rank (e);
4296 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4297 && e->ref->type != REF_SUBSTRING)
4298 gfc_resolve_substring_charlen (e);
4300 break;
4302 case EXPR_SUBSTRING:
4303 t = resolve_ref (e);
4304 break;
4306 case EXPR_CONSTANT:
4307 case EXPR_NULL:
4308 t = SUCCESS;
4309 break;
4311 case EXPR_ARRAY:
4312 t = FAILURE;
4313 if (resolve_ref (e) == FAILURE)
4314 break;
4316 t = gfc_resolve_array_constructor (e);
4317 /* Also try to expand a constructor. */
4318 if (t == SUCCESS)
4320 expression_rank (e);
4321 gfc_expand_constructor (e);
4324 /* This provides the opportunity for the length of constructors with
4325 character valued function elements to propagate the string length
4326 to the expression. */
4327 if (e->ts.type == BT_CHARACTER)
4328 gfc_resolve_character_array_constructor (e);
4330 break;
4332 case EXPR_STRUCTURE:
4333 t = resolve_ref (e);
4334 if (t == FAILURE)
4335 break;
4337 t = resolve_structure_cons (e);
4338 if (t == FAILURE)
4339 break;
4341 t = gfc_simplify_expr (e, 0);
4342 break;
4344 default:
4345 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4348 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4349 fixup_charlen (e);
4351 return t;
4355 /* Resolve an expression from an iterator. They must be scalar and have
4356 INTEGER or (optionally) REAL type. */
4358 static try
4359 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4360 const char *name_msgid)
4362 if (gfc_resolve_expr (expr) == FAILURE)
4363 return FAILURE;
4365 if (expr->rank != 0)
4367 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4368 return FAILURE;
4371 if (expr->ts.type != BT_INTEGER)
4373 if (expr->ts.type == BT_REAL)
4375 if (real_ok)
4376 return gfc_notify_std (GFC_STD_F95_DEL,
4377 "Deleted feature: %s at %L must be integer",
4378 _(name_msgid), &expr->where);
4379 else
4381 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
4382 &expr->where);
4383 return FAILURE;
4386 else
4388 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
4389 return FAILURE;
4392 return SUCCESS;
4396 /* Resolve the expressions in an iterator structure. If REAL_OK is
4397 false allow only INTEGER type iterators, otherwise allow REAL types. */
4400 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
4402 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
4403 == FAILURE)
4404 return FAILURE;
4406 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
4408 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
4409 &iter->var->where);
4410 return FAILURE;
4413 if (gfc_resolve_iterator_expr (iter->start, real_ok,
4414 "Start expression in DO loop") == FAILURE)
4415 return FAILURE;
4417 if (gfc_resolve_iterator_expr (iter->end, real_ok,
4418 "End expression in DO loop") == FAILURE)
4419 return FAILURE;
4421 if (gfc_resolve_iterator_expr (iter->step, real_ok,
4422 "Step expression in DO loop") == FAILURE)
4423 return FAILURE;
4425 if (iter->step->expr_type == EXPR_CONSTANT)
4427 if ((iter->step->ts.type == BT_INTEGER
4428 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
4429 || (iter->step->ts.type == BT_REAL
4430 && mpfr_sgn (iter->step->value.real) == 0))
4432 gfc_error ("Step expression in DO loop at %L cannot be zero",
4433 &iter->step->where);
4434 return FAILURE;
4438 /* Convert start, end, and step to the same type as var. */
4439 if (iter->start->ts.kind != iter->var->ts.kind
4440 || iter->start->ts.type != iter->var->ts.type)
4441 gfc_convert_type (iter->start, &iter->var->ts, 2);
4443 if (iter->end->ts.kind != iter->var->ts.kind
4444 || iter->end->ts.type != iter->var->ts.type)
4445 gfc_convert_type (iter->end, &iter->var->ts, 2);
4447 if (iter->step->ts.kind != iter->var->ts.kind
4448 || iter->step->ts.type != iter->var->ts.type)
4449 gfc_convert_type (iter->step, &iter->var->ts, 2);
4451 return SUCCESS;
4455 /* Traversal function for find_forall_index. f == 2 signals that
4456 that variable itself is not to be checked - only the references. */
4458 static bool
4459 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
4461 if (expr->expr_type != EXPR_VARIABLE)
4462 return false;
4464 /* A scalar assignment */
4465 if (!expr->ref || *f == 1)
4467 if (expr->symtree->n.sym == sym)
4468 return true;
4469 else
4470 return false;
4473 if (*f == 2)
4474 *f = 1;
4475 return false;
4479 /* Check whether the FORALL index appears in the expression or not.
4480 Returns SUCCESS if SYM is found in EXPR. */
4483 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
4485 if (gfc_traverse_expr (expr, sym, forall_index, f))
4486 return SUCCESS;
4487 else
4488 return FAILURE;
4492 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
4493 to be a scalar INTEGER variable. The subscripts and stride are scalar
4494 INTEGERs, and if stride is a constant it must be nonzero.
4495 Furthermore "A subscript or stride in a forall-triplet-spec shall
4496 not contain a reference to any index-name in the
4497 forall-triplet-spec-list in which it appears." (7.5.4.1) */
4499 static void
4500 resolve_forall_iterators (gfc_forall_iterator *it)
4502 gfc_forall_iterator *iter, *iter2;
4504 for (iter = it; iter; iter = iter->next)
4506 if (gfc_resolve_expr (iter->var) == SUCCESS
4507 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
4508 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
4509 &iter->var->where);
4511 if (gfc_resolve_expr (iter->start) == SUCCESS
4512 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
4513 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
4514 &iter->start->where);
4515 if (iter->var->ts.kind != iter->start->ts.kind)
4516 gfc_convert_type (iter->start, &iter->var->ts, 2);
4518 if (gfc_resolve_expr (iter->end) == SUCCESS
4519 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
4520 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
4521 &iter->end->where);
4522 if (iter->var->ts.kind != iter->end->ts.kind)
4523 gfc_convert_type (iter->end, &iter->var->ts, 2);
4525 if (gfc_resolve_expr (iter->stride) == SUCCESS)
4527 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
4528 gfc_error ("FORALL stride expression at %L must be a scalar %s",
4529 &iter->stride->where, "INTEGER");
4531 if (iter->stride->expr_type == EXPR_CONSTANT
4532 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
4533 gfc_error ("FORALL stride expression at %L cannot be zero",
4534 &iter->stride->where);
4536 if (iter->var->ts.kind != iter->stride->ts.kind)
4537 gfc_convert_type (iter->stride, &iter->var->ts, 2);
4540 for (iter = it; iter; iter = iter->next)
4541 for (iter2 = iter; iter2; iter2 = iter2->next)
4543 if (find_forall_index (iter2->start,
4544 iter->var->symtree->n.sym, 0) == SUCCESS
4545 || find_forall_index (iter2->end,
4546 iter->var->symtree->n.sym, 0) == SUCCESS
4547 || find_forall_index (iter2->stride,
4548 iter->var->symtree->n.sym, 0) == SUCCESS)
4549 gfc_error ("FORALL index '%s' may not appear in triplet "
4550 "specification at %L", iter->var->symtree->name,
4551 &iter2->start->where);
4556 /* Given a pointer to a symbol that is a derived type, see if it's
4557 inaccessible, i.e. if it's defined in another module and the components are
4558 PRIVATE. The search is recursive if necessary. Returns zero if no
4559 inaccessible components are found, nonzero otherwise. */
4561 static int
4562 derived_inaccessible (gfc_symbol *sym)
4564 gfc_component *c;
4566 if (sym->attr.use_assoc && sym->attr.private_comp)
4567 return 1;
4569 for (c = sym->components; c; c = c->next)
4571 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
4572 return 1;
4575 return 0;
4579 /* Resolve the argument of a deallocate expression. The expression must be
4580 a pointer or a full array. */
4582 static try
4583 resolve_deallocate_expr (gfc_expr *e)
4585 symbol_attribute attr;
4586 int allocatable, pointer, check_intent_in;
4587 gfc_ref *ref;
4589 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4590 check_intent_in = 1;
4592 if (gfc_resolve_expr (e) == FAILURE)
4593 return FAILURE;
4595 if (e->expr_type != EXPR_VARIABLE)
4596 goto bad;
4598 allocatable = e->symtree->n.sym->attr.allocatable;
4599 pointer = e->symtree->n.sym->attr.pointer;
4600 for (ref = e->ref; ref; ref = ref->next)
4602 if (pointer)
4603 check_intent_in = 0;
4605 switch (ref->type)
4607 case REF_ARRAY:
4608 if (ref->u.ar.type != AR_FULL)
4609 allocatable = 0;
4610 break;
4612 case REF_COMPONENT:
4613 allocatable = (ref->u.c.component->as != NULL
4614 && ref->u.c.component->as->type == AS_DEFERRED);
4615 pointer = ref->u.c.component->pointer;
4616 break;
4618 case REF_SUBSTRING:
4619 allocatable = 0;
4620 break;
4624 attr = gfc_expr_attr (e);
4626 if (allocatable == 0 && attr.pointer == 0)
4628 bad:
4629 gfc_error ("Expression in DEALLOCATE statement at %L must be "
4630 "ALLOCATABLE or a POINTER", &e->where);
4633 if (check_intent_in
4634 && e->symtree->n.sym->attr.intent == INTENT_IN)
4636 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
4637 e->symtree->n.sym->name, &e->where);
4638 return FAILURE;
4641 return SUCCESS;
4645 /* Returns true if the expression e contains a reference to the symbol sym. */
4646 static bool
4647 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4649 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
4650 return true;
4652 return false;
4655 static bool
4656 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
4658 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
4662 /* Given the expression node e for an allocatable/pointer of derived type to be
4663 allocated, get the expression node to be initialized afterwards (needed for
4664 derived types with default initializers, and derived types with allocatable
4665 components that need nullification.) */
4667 static gfc_expr *
4668 expr_to_initialize (gfc_expr *e)
4670 gfc_expr *result;
4671 gfc_ref *ref;
4672 int i;
4674 result = gfc_copy_expr (e);
4676 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
4677 for (ref = result->ref; ref; ref = ref->next)
4678 if (ref->type == REF_ARRAY && ref->next == NULL)
4680 ref->u.ar.type = AR_FULL;
4682 for (i = 0; i < ref->u.ar.dimen; i++)
4683 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
4685 result->rank = ref->u.ar.dimen;
4686 break;
4689 return result;
4693 /* Resolve the expression in an ALLOCATE statement, doing the additional
4694 checks to see whether the expression is OK or not. The expression must
4695 have a trailing array reference that gives the size of the array. */
4697 static try
4698 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
4700 int i, pointer, allocatable, dimension, check_intent_in;
4701 symbol_attribute attr;
4702 gfc_ref *ref, *ref2;
4703 gfc_array_ref *ar;
4704 gfc_code *init_st;
4705 gfc_expr *init_e;
4706 gfc_symbol *sym;
4707 gfc_alloc *a;
4709 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4710 check_intent_in = 1;
4712 if (gfc_resolve_expr (e) == FAILURE)
4713 return FAILURE;
4715 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
4716 sym = code->expr->symtree->n.sym;
4717 else
4718 sym = NULL;
4720 /* Make sure the expression is allocatable or a pointer. If it is
4721 pointer, the next-to-last reference must be a pointer. */
4723 ref2 = NULL;
4725 if (e->expr_type != EXPR_VARIABLE)
4727 allocatable = 0;
4728 attr = gfc_expr_attr (e);
4729 pointer = attr.pointer;
4730 dimension = attr.dimension;
4732 else
4734 allocatable = e->symtree->n.sym->attr.allocatable;
4735 pointer = e->symtree->n.sym->attr.pointer;
4736 dimension = e->symtree->n.sym->attr.dimension;
4738 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
4740 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
4741 "not be allocated in the same statement at %L",
4742 sym->name, &e->where);
4743 return FAILURE;
4746 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
4748 if (pointer)
4749 check_intent_in = 0;
4751 switch (ref->type)
4753 case REF_ARRAY:
4754 if (ref->next != NULL)
4755 pointer = 0;
4756 break;
4758 case REF_COMPONENT:
4759 allocatable = (ref->u.c.component->as != NULL
4760 && ref->u.c.component->as->type == AS_DEFERRED);
4762 pointer = ref->u.c.component->pointer;
4763 dimension = ref->u.c.component->dimension;
4764 break;
4766 case REF_SUBSTRING:
4767 allocatable = 0;
4768 pointer = 0;
4769 break;
4774 if (allocatable == 0 && pointer == 0)
4776 gfc_error ("Expression in ALLOCATE statement at %L must be "
4777 "ALLOCATABLE or a POINTER", &e->where);
4778 return FAILURE;
4781 if (check_intent_in
4782 && e->symtree->n.sym->attr.intent == INTENT_IN)
4784 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
4785 e->symtree->n.sym->name, &e->where);
4786 return FAILURE;
4789 /* Add default initializer for those derived types that need them. */
4790 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
4792 init_st = gfc_get_code ();
4793 init_st->loc = code->loc;
4794 init_st->op = EXEC_INIT_ASSIGN;
4795 init_st->expr = expr_to_initialize (e);
4796 init_st->expr2 = init_e;
4797 init_st->next = code->next;
4798 code->next = init_st;
4801 if (pointer && dimension == 0)
4802 return SUCCESS;
4804 /* Make sure the next-to-last reference node is an array specification. */
4806 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
4808 gfc_error ("Array specification required in ALLOCATE statement "
4809 "at %L", &e->where);
4810 return FAILURE;
4813 /* Make sure that the array section reference makes sense in the
4814 context of an ALLOCATE specification. */
4816 ar = &ref2->u.ar;
4818 for (i = 0; i < ar->dimen; i++)
4820 if (ref2->u.ar.type == AR_ELEMENT)
4821 goto check_symbols;
4823 switch (ar->dimen_type[i])
4825 case DIMEN_ELEMENT:
4826 break;
4828 case DIMEN_RANGE:
4829 if (ar->start[i] != NULL
4830 && ar->end[i] != NULL
4831 && ar->stride[i] == NULL)
4832 break;
4834 /* Fall Through... */
4836 case DIMEN_UNKNOWN:
4837 case DIMEN_VECTOR:
4838 gfc_error ("Bad array specification in ALLOCATE statement at %L",
4839 &e->where);
4840 return FAILURE;
4843 check_symbols:
4845 for (a = code->ext.alloc_list; a; a = a->next)
4847 sym = a->expr->symtree->n.sym;
4849 /* TODO - check derived type components. */
4850 if (sym->ts.type == BT_DERIVED)
4851 continue;
4853 if ((ar->start[i] != NULL && find_sym_in_expr (sym, ar->start[i]))
4854 || (ar->end[i] != NULL && find_sym_in_expr (sym, ar->end[i])))
4856 gfc_error ("'%s' must not appear an the array specification at "
4857 "%L in the same ALLOCATE statement where it is "
4858 "itself allocated", sym->name, &ar->where);
4859 return FAILURE;
4864 return SUCCESS;
4867 static void
4868 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
4870 gfc_symbol *s = NULL;
4871 gfc_alloc *a;
4872 bool is_variable;
4874 if (code->expr)
4875 s = code->expr->symtree->n.sym;
4877 if (s)
4879 if (s->attr.intent == INTENT_IN)
4880 gfc_error ("STAT variable '%s' of %s statement at %C cannot "
4881 "be INTENT(IN)", s->name, fcn);
4883 if (gfc_pure (NULL) && gfc_impure_variable (s))
4884 gfc_error ("Illegal STAT variable in %s statement at %C "
4885 "for a PURE procedure", fcn);
4887 is_variable = false;
4888 if (s->attr.flavor == FL_VARIABLE)
4889 is_variable = true;
4890 else if (s->attr.function && s->result == s
4891 && (gfc_current_ns->proc_name == s
4893 (gfc_current_ns->parent
4894 && gfc_current_ns->parent->proc_name == s)))
4895 is_variable = true;
4896 else if (gfc_current_ns->entries && s->result == s)
4898 gfc_entry_list *el;
4899 for (el = gfc_current_ns->entries; el; el = el->next)
4900 if (el->sym == s)
4902 is_variable = true;
4905 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
4906 && s->result == s)
4908 gfc_entry_list *el;
4909 for (el = gfc_current_ns->parent->entries; el; el = el->next)
4910 if (el->sym == s)
4912 is_variable = true;
4916 if (s->attr.flavor == FL_UNKNOWN
4917 && gfc_add_flavor (&s->attr, FL_VARIABLE,
4918 s->name, NULL) == SUCCESS)
4919 is_variable = true;
4921 if (!is_variable)
4922 gfc_error ("STAT tag in %s statement at %L must be "
4923 "a variable", fcn, &code->expr->where);
4927 if (s && code->expr->ts.type != BT_INTEGER)
4928 gfc_error ("STAT tag in %s statement at %L must be "
4929 "of type INTEGER", fcn, &code->expr->where);
4931 if (strcmp (fcn, "ALLOCATE") == 0)
4933 for (a = code->ext.alloc_list; a; a = a->next)
4934 resolve_allocate_expr (a->expr, code);
4936 else
4938 for (a = code->ext.alloc_list; a; a = a->next)
4939 resolve_deallocate_expr (a->expr);
4943 /************ SELECT CASE resolution subroutines ************/
4945 /* Callback function for our mergesort variant. Determines interval
4946 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4947 op1 > op2. Assumes we're not dealing with the default case.
4948 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
4949 There are nine situations to check. */
4951 static int
4952 compare_cases (const gfc_case *op1, const gfc_case *op2)
4954 int retval;
4956 if (op1->low == NULL) /* op1 = (:L) */
4958 /* op2 = (:N), so overlap. */
4959 retval = 0;
4960 /* op2 = (M:) or (M:N), L < M */
4961 if (op2->low != NULL
4962 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4963 retval = -1;
4965 else if (op1->high == NULL) /* op1 = (K:) */
4967 /* op2 = (M:), so overlap. */
4968 retval = 0;
4969 /* op2 = (:N) or (M:N), K > N */
4970 if (op2->high != NULL
4971 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4972 retval = 1;
4974 else /* op1 = (K:L) */
4976 if (op2->low == NULL) /* op2 = (:N), K > N */
4977 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4978 ? 1 : 0;
4979 else if (op2->high == NULL) /* op2 = (M:), L < M */
4980 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4981 ? -1 : 0;
4982 else /* op2 = (M:N) */
4984 retval = 0;
4985 /* L < M */
4986 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4987 retval = -1;
4988 /* K > N */
4989 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4990 retval = 1;
4994 return retval;
4998 /* Merge-sort a double linked case list, detecting overlap in the
4999 process. LIST is the head of the double linked case list before it
5000 is sorted. Returns the head of the sorted list if we don't see any
5001 overlap, or NULL otherwise. */
5003 static gfc_case *
5004 check_case_overlap (gfc_case *list)
5006 gfc_case *p, *q, *e, *tail;
5007 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5009 /* If the passed list was empty, return immediately. */
5010 if (!list)
5011 return NULL;
5013 overlap_seen = 0;
5014 insize = 1;
5016 /* Loop unconditionally. The only exit from this loop is a return
5017 statement, when we've finished sorting the case list. */
5018 for (;;)
5020 p = list;
5021 list = NULL;
5022 tail = NULL;
5024 /* Count the number of merges we do in this pass. */
5025 nmerges = 0;
5027 /* Loop while there exists a merge to be done. */
5028 while (p)
5030 int i;
5032 /* Count this merge. */
5033 nmerges++;
5035 /* Cut the list in two pieces by stepping INSIZE places
5036 forward in the list, starting from P. */
5037 psize = 0;
5038 q = p;
5039 for (i = 0; i < insize; i++)
5041 psize++;
5042 q = q->right;
5043 if (!q)
5044 break;
5046 qsize = insize;
5048 /* Now we have two lists. Merge them! */
5049 while (psize > 0 || (qsize > 0 && q != NULL))
5051 /* See from which the next case to merge comes from. */
5052 if (psize == 0)
5054 /* P is empty so the next case must come from Q. */
5055 e = q;
5056 q = q->right;
5057 qsize--;
5059 else if (qsize == 0 || q == NULL)
5061 /* Q is empty. */
5062 e = p;
5063 p = p->right;
5064 psize--;
5066 else
5068 cmp = compare_cases (p, q);
5069 if (cmp < 0)
5071 /* The whole case range for P is less than the
5072 one for Q. */
5073 e = p;
5074 p = p->right;
5075 psize--;
5077 else if (cmp > 0)
5079 /* The whole case range for Q is greater than
5080 the case range for P. */
5081 e = q;
5082 q = q->right;
5083 qsize--;
5085 else
5087 /* The cases overlap, or they are the same
5088 element in the list. Either way, we must
5089 issue an error and get the next case from P. */
5090 /* FIXME: Sort P and Q by line number. */
5091 gfc_error ("CASE label at %L overlaps with CASE "
5092 "label at %L", &p->where, &q->where);
5093 overlap_seen = 1;
5094 e = p;
5095 p = p->right;
5096 psize--;
5100 /* Add the next element to the merged list. */
5101 if (tail)
5102 tail->right = e;
5103 else
5104 list = e;
5105 e->left = tail;
5106 tail = e;
5109 /* P has now stepped INSIZE places along, and so has Q. So
5110 they're the same. */
5111 p = q;
5113 tail->right = NULL;
5115 /* If we have done only one merge or none at all, we've
5116 finished sorting the cases. */
5117 if (nmerges <= 1)
5119 if (!overlap_seen)
5120 return list;
5121 else
5122 return NULL;
5125 /* Otherwise repeat, merging lists twice the size. */
5126 insize *= 2;
5131 /* Check to see if an expression is suitable for use in a CASE statement.
5132 Makes sure that all case expressions are scalar constants of the same
5133 type. Return FAILURE if anything is wrong. */
5135 static try
5136 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5138 if (e == NULL) return SUCCESS;
5140 if (e->ts.type != case_expr->ts.type)
5142 gfc_error ("Expression in CASE statement at %L must be of type %s",
5143 &e->where, gfc_basic_typename (case_expr->ts.type));
5144 return FAILURE;
5147 /* C805 (R808) For a given case-construct, each case-value shall be of
5148 the same type as case-expr. For character type, length differences
5149 are allowed, but the kind type parameters shall be the same. */
5151 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5153 gfc_error("Expression in CASE statement at %L must be kind %d",
5154 &e->where, case_expr->ts.kind);
5155 return FAILURE;
5158 /* Convert the case value kind to that of case expression kind, if needed.
5159 FIXME: Should a warning be issued? */
5160 if (e->ts.kind != case_expr->ts.kind)
5161 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5163 if (e->rank != 0)
5165 gfc_error ("Expression in CASE statement at %L must be scalar",
5166 &e->where);
5167 return FAILURE;
5170 return SUCCESS;
5174 /* Given a completely parsed select statement, we:
5176 - Validate all expressions and code within the SELECT.
5177 - Make sure that the selection expression is not of the wrong type.
5178 - Make sure that no case ranges overlap.
5179 - Eliminate unreachable cases and unreachable code resulting from
5180 removing case labels.
5182 The standard does allow unreachable cases, e.g. CASE (5:3). But
5183 they are a hassle for code generation, and to prevent that, we just
5184 cut them out here. This is not necessary for overlapping cases
5185 because they are illegal and we never even try to generate code.
5187 We have the additional caveat that a SELECT construct could have
5188 been a computed GOTO in the source code. Fortunately we can fairly
5189 easily work around that here: The case_expr for a "real" SELECT CASE
5190 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5191 we have to do is make sure that the case_expr is a scalar integer
5192 expression. */
5194 static void
5195 resolve_select (gfc_code *code)
5197 gfc_code *body;
5198 gfc_expr *case_expr;
5199 gfc_case *cp, *default_case, *tail, *head;
5200 int seen_unreachable;
5201 int seen_logical;
5202 int ncases;
5203 bt type;
5204 try t;
5206 if (code->expr == NULL)
5208 /* This was actually a computed GOTO statement. */
5209 case_expr = code->expr2;
5210 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5211 gfc_error ("Selection expression in computed GOTO statement "
5212 "at %L must be a scalar integer expression",
5213 &case_expr->where);
5215 /* Further checking is not necessary because this SELECT was built
5216 by the compiler, so it should always be OK. Just move the
5217 case_expr from expr2 to expr so that we can handle computed
5218 GOTOs as normal SELECTs from here on. */
5219 code->expr = code->expr2;
5220 code->expr2 = NULL;
5221 return;
5224 case_expr = code->expr;
5226 type = case_expr->ts.type;
5227 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5229 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5230 &case_expr->where, gfc_typename (&case_expr->ts));
5232 /* Punt. Going on here just produce more garbage error messages. */
5233 return;
5236 if (case_expr->rank != 0)
5238 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5239 "expression", &case_expr->where);
5241 /* Punt. */
5242 return;
5245 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5246 of the SELECT CASE expression and its CASE values. Walk the lists
5247 of case values, and if we find a mismatch, promote case_expr to
5248 the appropriate kind. */
5250 if (type == BT_LOGICAL || type == BT_INTEGER)
5252 for (body = code->block; body; body = body->block)
5254 /* Walk the case label list. */
5255 for (cp = body->ext.case_list; cp; cp = cp->next)
5257 /* Intercept the DEFAULT case. It does not have a kind. */
5258 if (cp->low == NULL && cp->high == NULL)
5259 continue;
5261 /* Unreachable case ranges are discarded, so ignore. */
5262 if (cp->low != NULL && cp->high != NULL
5263 && cp->low != cp->high
5264 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5265 continue;
5267 /* FIXME: Should a warning be issued? */
5268 if (cp->low != NULL
5269 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5270 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5272 if (cp->high != NULL
5273 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5274 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5279 /* Assume there is no DEFAULT case. */
5280 default_case = NULL;
5281 head = tail = NULL;
5282 ncases = 0;
5283 seen_logical = 0;
5285 for (body = code->block; body; body = body->block)
5287 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5288 t = SUCCESS;
5289 seen_unreachable = 0;
5291 /* Walk the case label list, making sure that all case labels
5292 are legal. */
5293 for (cp = body->ext.case_list; cp; cp = cp->next)
5295 /* Count the number of cases in the whole construct. */
5296 ncases++;
5298 /* Intercept the DEFAULT case. */
5299 if (cp->low == NULL && cp->high == NULL)
5301 if (default_case != NULL)
5303 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5304 "by a second DEFAULT CASE at %L",
5305 &default_case->where, &cp->where);
5306 t = FAILURE;
5307 break;
5309 else
5311 default_case = cp;
5312 continue;
5316 /* Deal with single value cases and case ranges. Errors are
5317 issued from the validation function. */
5318 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5319 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5321 t = FAILURE;
5322 break;
5325 if (type == BT_LOGICAL
5326 && ((cp->low == NULL || cp->high == NULL)
5327 || cp->low != cp->high))
5329 gfc_error ("Logical range in CASE statement at %L is not "
5330 "allowed", &cp->low->where);
5331 t = FAILURE;
5332 break;
5335 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5337 int value;
5338 value = cp->low->value.logical == 0 ? 2 : 1;
5339 if (value & seen_logical)
5341 gfc_error ("constant logical value in CASE statement "
5342 "is repeated at %L",
5343 &cp->low->where);
5344 t = FAILURE;
5345 break;
5347 seen_logical |= value;
5350 if (cp->low != NULL && cp->high != NULL
5351 && cp->low != cp->high
5352 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5354 if (gfc_option.warn_surprising)
5355 gfc_warning ("Range specification at %L can never "
5356 "be matched", &cp->where);
5358 cp->unreachable = 1;
5359 seen_unreachable = 1;
5361 else
5363 /* If the case range can be matched, it can also overlap with
5364 other cases. To make sure it does not, we put it in a
5365 double linked list here. We sort that with a merge sort
5366 later on to detect any overlapping cases. */
5367 if (!head)
5369 head = tail = cp;
5370 head->right = head->left = NULL;
5372 else
5374 tail->right = cp;
5375 tail->right->left = tail;
5376 tail = tail->right;
5377 tail->right = NULL;
5382 /* It there was a failure in the previous case label, give up
5383 for this case label list. Continue with the next block. */
5384 if (t == FAILURE)
5385 continue;
5387 /* See if any case labels that are unreachable have been seen.
5388 If so, we eliminate them. This is a bit of a kludge because
5389 the case lists for a single case statement (label) is a
5390 single forward linked lists. */
5391 if (seen_unreachable)
5393 /* Advance until the first case in the list is reachable. */
5394 while (body->ext.case_list != NULL
5395 && body->ext.case_list->unreachable)
5397 gfc_case *n = body->ext.case_list;
5398 body->ext.case_list = body->ext.case_list->next;
5399 n->next = NULL;
5400 gfc_free_case_list (n);
5403 /* Strip all other unreachable cases. */
5404 if (body->ext.case_list)
5406 for (cp = body->ext.case_list; cp->next; cp = cp->next)
5408 if (cp->next->unreachable)
5410 gfc_case *n = cp->next;
5411 cp->next = cp->next->next;
5412 n->next = NULL;
5413 gfc_free_case_list (n);
5420 /* See if there were overlapping cases. If the check returns NULL,
5421 there was overlap. In that case we don't do anything. If head
5422 is non-NULL, we prepend the DEFAULT case. The sorted list can
5423 then used during code generation for SELECT CASE constructs with
5424 a case expression of a CHARACTER type. */
5425 if (head)
5427 head = check_case_overlap (head);
5429 /* Prepend the default_case if it is there. */
5430 if (head != NULL && default_case)
5432 default_case->left = NULL;
5433 default_case->right = head;
5434 head->left = default_case;
5438 /* Eliminate dead blocks that may be the result if we've seen
5439 unreachable case labels for a block. */
5440 for (body = code; body && body->block; body = body->block)
5442 if (body->block->ext.case_list == NULL)
5444 /* Cut the unreachable block from the code chain. */
5445 gfc_code *c = body->block;
5446 body->block = c->block;
5448 /* Kill the dead block, but not the blocks below it. */
5449 c->block = NULL;
5450 gfc_free_statements (c);
5454 /* More than two cases is legal but insane for logical selects.
5455 Issue a warning for it. */
5456 if (gfc_option.warn_surprising && type == BT_LOGICAL
5457 && ncases > 2)
5458 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
5459 &code->loc);
5463 /* Resolve a transfer statement. This is making sure that:
5464 -- a derived type being transferred has only non-pointer components
5465 -- a derived type being transferred doesn't have private components, unless
5466 it's being transferred from the module where the type was defined
5467 -- we're not trying to transfer a whole assumed size array. */
5469 static void
5470 resolve_transfer (gfc_code *code)
5472 gfc_typespec *ts;
5473 gfc_symbol *sym;
5474 gfc_ref *ref;
5475 gfc_expr *exp;
5477 exp = code->expr;
5479 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
5480 return;
5482 sym = exp->symtree->n.sym;
5483 ts = &sym->ts;
5485 /* Go to actual component transferred. */
5486 for (ref = code->expr->ref; ref; ref = ref->next)
5487 if (ref->type == REF_COMPONENT)
5488 ts = &ref->u.c.component->ts;
5490 if (ts->type == BT_DERIVED)
5492 /* Check that transferred derived type doesn't contain POINTER
5493 components. */
5494 if (ts->derived->attr.pointer_comp)
5496 gfc_error ("Data transfer element at %L cannot have "
5497 "POINTER components", &code->loc);
5498 return;
5501 if (ts->derived->attr.alloc_comp)
5503 gfc_error ("Data transfer element at %L cannot have "
5504 "ALLOCATABLE components", &code->loc);
5505 return;
5508 if (derived_inaccessible (ts->derived))
5510 gfc_error ("Data transfer element at %L cannot have "
5511 "PRIVATE components",&code->loc);
5512 return;
5516 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
5517 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
5519 gfc_error ("Data transfer element at %L cannot be a full reference to "
5520 "an assumed-size array", &code->loc);
5521 return;
5526 /*********** Toplevel code resolution subroutines ***********/
5528 /* Find the set of labels that are reachable from this block. We also
5529 record the last statement in each block so that we don't have to do
5530 a linear search to find the END DO statements of the blocks. */
5532 static void
5533 reachable_labels (gfc_code *block)
5535 gfc_code *c;
5537 if (!block)
5538 return;
5540 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
5542 /* Collect labels in this block. */
5543 for (c = block; c; c = c->next)
5545 if (c->here)
5546 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
5548 if (!c->next && cs_base->prev)
5549 cs_base->prev->tail = c;
5552 /* Merge with labels from parent block. */
5553 if (cs_base->prev)
5555 gcc_assert (cs_base->prev->reachable_labels);
5556 bitmap_ior_into (cs_base->reachable_labels,
5557 cs_base->prev->reachable_labels);
5561 /* Given a branch to a label and a namespace, if the branch is conforming.
5562 The code node describes where the branch is located. */
5564 static void
5565 resolve_branch (gfc_st_label *label, gfc_code *code)
5567 code_stack *stack;
5569 if (label == NULL)
5570 return;
5572 /* Step one: is this a valid branching target? */
5574 if (label->defined == ST_LABEL_UNKNOWN)
5576 gfc_error ("Label %d referenced at %L is never defined", label->value,
5577 &label->where);
5578 return;
5581 if (label->defined != ST_LABEL_TARGET)
5583 gfc_error ("Statement at %L is not a valid branch target statement "
5584 "for the branch statement at %L", &label->where, &code->loc);
5585 return;
5588 /* Step two: make sure this branch is not a branch to itself ;-) */
5590 if (code->here == label)
5592 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
5593 return;
5596 /* Step three: See if the label is in the same block as the
5597 branching statement. The hard work has been done by setting up
5598 the bitmap reachable_labels. */
5600 if (!bitmap_bit_p (cs_base->reachable_labels, label->value))
5602 /* The label is not in an enclosing block, so illegal. This was
5603 allowed in Fortran 66, so we allow it as extension. No
5604 further checks are necessary in this case. */
5605 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
5606 "as the GOTO statement at %L", &label->where,
5607 &code->loc);
5608 return;
5611 /* Step four: Make sure that the branching target is legal if
5612 the statement is an END {SELECT,IF}. */
5614 for (stack = cs_base; stack; stack = stack->prev)
5615 if (stack->current->next && stack->current->next->here == label)
5616 break;
5618 if (stack && stack->current->next->op == EXEC_NOP)
5620 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps to "
5621 "END of construct at %L", &code->loc,
5622 &stack->current->next->loc);
5623 return; /* We know this is not an END DO. */
5626 /* Step five: Make sure that we're not jumping to the end of a DO
5627 loop from within the loop. */
5629 for (stack = cs_base; stack; stack = stack->prev)
5630 if ((stack->current->op == EXEC_DO
5631 || stack->current->op == EXEC_DO_WHILE)
5632 && stack->tail->here == label && stack->tail->op == EXEC_NOP)
5634 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps "
5635 "to END of construct at %L", &code->loc,
5636 &stack->tail->loc);
5637 return;
5643 /* Check whether EXPR1 has the same shape as EXPR2. */
5645 static try
5646 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
5648 mpz_t shape[GFC_MAX_DIMENSIONS];
5649 mpz_t shape2[GFC_MAX_DIMENSIONS];
5650 try result = FAILURE;
5651 int i;
5653 /* Compare the rank. */
5654 if (expr1->rank != expr2->rank)
5655 return result;
5657 /* Compare the size of each dimension. */
5658 for (i=0; i<expr1->rank; i++)
5660 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
5661 goto ignore;
5663 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
5664 goto ignore;
5666 if (mpz_cmp (shape[i], shape2[i]))
5667 goto over;
5670 /* When either of the two expression is an assumed size array, we
5671 ignore the comparison of dimension sizes. */
5672 ignore:
5673 result = SUCCESS;
5675 over:
5676 for (i--; i >= 0; i--)
5678 mpz_clear (shape[i]);
5679 mpz_clear (shape2[i]);
5681 return result;
5685 /* Check whether a WHERE assignment target or a WHERE mask expression
5686 has the same shape as the outmost WHERE mask expression. */
5688 static void
5689 resolve_where (gfc_code *code, gfc_expr *mask)
5691 gfc_code *cblock;
5692 gfc_code *cnext;
5693 gfc_expr *e = NULL;
5695 cblock = code->block;
5697 /* Store the first WHERE mask-expr of the WHERE statement or construct.
5698 In case of nested WHERE, only the outmost one is stored. */
5699 if (mask == NULL) /* outmost WHERE */
5700 e = cblock->expr;
5701 else /* inner WHERE */
5702 e = mask;
5704 while (cblock)
5706 if (cblock->expr)
5708 /* Check if the mask-expr has a consistent shape with the
5709 outmost WHERE mask-expr. */
5710 if (resolve_where_shape (cblock->expr, e) == FAILURE)
5711 gfc_error ("WHERE mask at %L has inconsistent shape",
5712 &cblock->expr->where);
5715 /* the assignment statement of a WHERE statement, or the first
5716 statement in where-body-construct of a WHERE construct */
5717 cnext = cblock->next;
5718 while (cnext)
5720 switch (cnext->op)
5722 /* WHERE assignment statement */
5723 case EXEC_ASSIGN:
5725 /* Check shape consistent for WHERE assignment target. */
5726 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
5727 gfc_error ("WHERE assignment target at %L has "
5728 "inconsistent shape", &cnext->expr->where);
5729 break;
5732 case EXEC_ASSIGN_CALL:
5733 resolve_call (cnext);
5734 if (!cnext->resolved_sym->attr.elemental)
5735 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5736 &cnext->ext.actual->expr->where);
5737 break;
5739 /* WHERE or WHERE construct is part of a where-body-construct */
5740 case EXEC_WHERE:
5741 resolve_where (cnext, e);
5742 break;
5744 default:
5745 gfc_error ("Unsupported statement inside WHERE at %L",
5746 &cnext->loc);
5748 /* the next statement within the same where-body-construct */
5749 cnext = cnext->next;
5751 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5752 cblock = cblock->block;
5757 /* Resolve assignment in FORALL construct.
5758 NVAR is the number of FORALL index variables, and VAR_EXPR records the
5759 FORALL index variables. */
5761 static void
5762 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
5764 int n;
5766 for (n = 0; n < nvar; n++)
5768 gfc_symbol *forall_index;
5770 forall_index = var_expr[n]->symtree->n.sym;
5772 /* Check whether the assignment target is one of the FORALL index
5773 variable. */
5774 if ((code->expr->expr_type == EXPR_VARIABLE)
5775 && (code->expr->symtree->n.sym == forall_index))
5776 gfc_error ("Assignment to a FORALL index variable at %L",
5777 &code->expr->where);
5778 else
5780 /* If one of the FORALL index variables doesn't appear in the
5781 assignment target, then there will be a many-to-one
5782 assignment. */
5783 if (find_forall_index (code->expr, forall_index, 0) == FAILURE)
5784 gfc_error ("The FORALL with index '%s' cause more than one "
5785 "assignment to this object at %L",
5786 var_expr[n]->symtree->name, &code->expr->where);
5792 /* Resolve WHERE statement in FORALL construct. */
5794 static void
5795 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
5796 gfc_expr **var_expr)
5798 gfc_code *cblock;
5799 gfc_code *cnext;
5801 cblock = code->block;
5802 while (cblock)
5804 /* the assignment statement of a WHERE statement, or the first
5805 statement in where-body-construct of a WHERE construct */
5806 cnext = cblock->next;
5807 while (cnext)
5809 switch (cnext->op)
5811 /* WHERE assignment statement */
5812 case EXEC_ASSIGN:
5813 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
5814 break;
5816 /* WHERE operator assignment statement */
5817 case EXEC_ASSIGN_CALL:
5818 resolve_call (cnext);
5819 if (!cnext->resolved_sym->attr.elemental)
5820 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5821 &cnext->ext.actual->expr->where);
5822 break;
5824 /* WHERE or WHERE construct is part of a where-body-construct */
5825 case EXEC_WHERE:
5826 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
5827 break;
5829 default:
5830 gfc_error ("Unsupported statement inside WHERE at %L",
5831 &cnext->loc);
5833 /* the next statement within the same where-body-construct */
5834 cnext = cnext->next;
5836 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5837 cblock = cblock->block;
5842 /* Traverse the FORALL body to check whether the following errors exist:
5843 1. For assignment, check if a many-to-one assignment happens.
5844 2. For WHERE statement, check the WHERE body to see if there is any
5845 many-to-one assignment. */
5847 static void
5848 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
5850 gfc_code *c;
5852 c = code->block->next;
5853 while (c)
5855 switch (c->op)
5857 case EXEC_ASSIGN:
5858 case EXEC_POINTER_ASSIGN:
5859 gfc_resolve_assign_in_forall (c, nvar, var_expr);
5860 break;
5862 case EXEC_ASSIGN_CALL:
5863 resolve_call (c);
5864 break;
5866 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
5867 there is no need to handle it here. */
5868 case EXEC_FORALL:
5869 break;
5870 case EXEC_WHERE:
5871 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
5872 break;
5873 default:
5874 break;
5876 /* The next statement in the FORALL body. */
5877 c = c->next;
5882 /* Given a FORALL construct, first resolve the FORALL iterator, then call
5883 gfc_resolve_forall_body to resolve the FORALL body. */
5885 static void
5886 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
5888 static gfc_expr **var_expr;
5889 static int total_var = 0;
5890 static int nvar = 0;
5891 gfc_forall_iterator *fa;
5892 gfc_code *next;
5893 int i;
5895 /* Start to resolve a FORALL construct */
5896 if (forall_save == 0)
5898 /* Count the total number of FORALL index in the nested FORALL
5899 construct in order to allocate the VAR_EXPR with proper size. */
5900 next = code;
5901 while ((next != NULL) && (next->op == EXEC_FORALL))
5903 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
5904 total_var ++;
5905 next = next->block->next;
5908 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
5909 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
5912 /* The information about FORALL iterator, including FORALL index start, end
5913 and stride. The FORALL index can not appear in start, end or stride. */
5914 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
5916 /* Check if any outer FORALL index name is the same as the current
5917 one. */
5918 for (i = 0; i < nvar; i++)
5920 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
5922 gfc_error ("An outer FORALL construct already has an index "
5923 "with this name %L", &fa->var->where);
5927 /* Record the current FORALL index. */
5928 var_expr[nvar] = gfc_copy_expr (fa->var);
5930 nvar++;
5933 /* Resolve the FORALL body. */
5934 gfc_resolve_forall_body (code, nvar, var_expr);
5936 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
5937 gfc_resolve_blocks (code->block, ns);
5939 /* Free VAR_EXPR after the whole FORALL construct resolved. */
5940 for (i = 0; i < total_var; i++)
5941 gfc_free_expr (var_expr[i]);
5943 /* Reset the counters. */
5944 total_var = 0;
5945 nvar = 0;
5949 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
5950 DO code nodes. */
5952 static void resolve_code (gfc_code *, gfc_namespace *);
5954 void
5955 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
5957 try t;
5959 for (; b; b = b->block)
5961 t = gfc_resolve_expr (b->expr);
5962 if (gfc_resolve_expr (b->expr2) == FAILURE)
5963 t = FAILURE;
5965 switch (b->op)
5967 case EXEC_IF:
5968 if (t == SUCCESS && b->expr != NULL
5969 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
5970 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
5971 &b->expr->where);
5972 break;
5974 case EXEC_WHERE:
5975 if (t == SUCCESS
5976 && b->expr != NULL
5977 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank == 0))
5978 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
5979 &b->expr->where);
5980 break;
5982 case EXEC_GOTO:
5983 resolve_branch (b->label, b);
5984 break;
5986 case EXEC_SELECT:
5987 case EXEC_FORALL:
5988 case EXEC_DO:
5989 case EXEC_DO_WHILE:
5990 case EXEC_READ:
5991 case EXEC_WRITE:
5992 case EXEC_IOLENGTH:
5993 break;
5995 case EXEC_OMP_ATOMIC:
5996 case EXEC_OMP_CRITICAL:
5997 case EXEC_OMP_DO:
5998 case EXEC_OMP_MASTER:
5999 case EXEC_OMP_ORDERED:
6000 case EXEC_OMP_PARALLEL:
6001 case EXEC_OMP_PARALLEL_DO:
6002 case EXEC_OMP_PARALLEL_SECTIONS:
6003 case EXEC_OMP_PARALLEL_WORKSHARE:
6004 case EXEC_OMP_SECTIONS:
6005 case EXEC_OMP_SINGLE:
6006 case EXEC_OMP_WORKSHARE:
6007 break;
6009 default:
6010 gfc_internal_error ("resolve_block(): Bad block type");
6013 resolve_code (b->next, ns);
6018 /* Does everything to resolve an ordinary assignment. Returns true
6019 if this is an interface asignment. */
6020 static bool
6021 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6023 bool rval = false;
6024 gfc_expr *lhs;
6025 gfc_expr *rhs;
6026 int llen = 0;
6027 int rlen = 0;
6028 int n;
6029 gfc_ref *ref;
6031 if (gfc_extend_assign (code, ns) == SUCCESS)
6033 lhs = code->ext.actual->expr;
6034 rhs = code->ext.actual->next->expr;
6035 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
6037 gfc_error ("Subroutine '%s' called instead of assignment at "
6038 "%L must be PURE", code->symtree->n.sym->name,
6039 &code->loc);
6040 return rval;
6043 /* Make a temporary rhs when there is a default initializer
6044 and rhs is the same symbol as the lhs. */
6045 if (rhs->expr_type == EXPR_VARIABLE
6046 && rhs->symtree->n.sym->ts.type == BT_DERIVED
6047 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
6048 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
6049 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
6051 return true;
6054 lhs = code->expr;
6055 rhs = code->expr2;
6057 if (rhs->is_boz
6058 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6059 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6060 &code->loc) == FAILURE)
6061 return false;
6063 /* Handle the case of a BOZ literal on the RHS. */
6064 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6066 int rc;
6067 if (gfc_option.warn_surprising)
6068 gfc_warning ("BOZ literal at %L is bitwise transferred "
6069 "non-integer symbol '%s'", &code->loc,
6070 lhs->symtree->n.sym->name);
6072 if (!gfc_convert_boz (rhs, &lhs->ts))
6073 return false;
6074 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6076 if (rc == ARITH_UNDERFLOW)
6077 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6078 ". This check can be disabled with the option "
6079 "-fno-range-check", &rhs->where);
6080 else if (rc == ARITH_OVERFLOW)
6081 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6082 ". This check can be disabled with the option "
6083 "-fno-range-check", &rhs->where);
6084 else if (rc == ARITH_NAN)
6085 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6086 ". This check can be disabled with the option "
6087 "-fno-range-check", &rhs->where);
6088 return false;
6093 if (lhs->ts.type == BT_CHARACTER
6094 && gfc_option.warn_character_truncation)
6096 if (lhs->ts.cl != NULL
6097 && lhs->ts.cl->length != NULL
6098 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6099 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
6101 if (rhs->expr_type == EXPR_CONSTANT)
6102 rlen = rhs->value.character.length;
6104 else if (rhs->ts.cl != NULL
6105 && rhs->ts.cl->length != NULL
6106 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6107 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
6109 if (rlen && llen && rlen > llen)
6110 gfc_warning_now ("CHARACTER expression will be truncated "
6111 "in assignment (%d/%d) at %L",
6112 llen, rlen, &code->loc);
6115 /* Ensure that a vector index expression for the lvalue is evaluated
6116 to a temporary if the lvalue symbol is referenced in it. */
6117 if (lhs->rank)
6119 for (ref = lhs->ref; ref; ref= ref->next)
6120 if (ref->type == REF_ARRAY)
6122 for (n = 0; n < ref->u.ar.dimen; n++)
6123 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
6124 && find_sym_in_expr (lhs->symtree->n.sym,
6125 ref->u.ar.start[n]))
6126 ref->u.ar.start[n]
6127 = gfc_get_parentheses (ref->u.ar.start[n]);
6131 if (gfc_pure (NULL))
6133 if (gfc_impure_variable (lhs->symtree->n.sym))
6135 gfc_error ("Cannot assign to variable '%s' in PURE "
6136 "procedure at %L",
6137 lhs->symtree->n.sym->name,
6138 &lhs->where);
6139 return rval;
6142 if (lhs->ts.type == BT_DERIVED
6143 && lhs->expr_type == EXPR_VARIABLE
6144 && lhs->ts.derived->attr.pointer_comp
6145 && gfc_impure_variable (rhs->symtree->n.sym))
6147 gfc_error ("The impure variable at %L is assigned to "
6148 "a derived type variable with a POINTER "
6149 "component in a PURE procedure (12.6)",
6150 &rhs->where);
6151 return rval;
6155 gfc_check_assign (lhs, rhs, 1);
6156 return false;
6159 /* Given a block of code, recursively resolve everything pointed to by this
6160 code block. */
6162 static void
6163 resolve_code (gfc_code *code, gfc_namespace *ns)
6165 int omp_workshare_save;
6166 int forall_save;
6167 code_stack frame;
6168 try t;
6170 frame.prev = cs_base;
6171 frame.head = code;
6172 cs_base = &frame;
6174 reachable_labels (code);
6176 for (; code; code = code->next)
6178 frame.current = code;
6179 forall_save = forall_flag;
6181 if (code->op == EXEC_FORALL)
6183 forall_flag = 1;
6184 gfc_resolve_forall (code, ns, forall_save);
6185 forall_flag = 2;
6187 else if (code->block)
6189 omp_workshare_save = -1;
6190 switch (code->op)
6192 case EXEC_OMP_PARALLEL_WORKSHARE:
6193 omp_workshare_save = omp_workshare_flag;
6194 omp_workshare_flag = 1;
6195 gfc_resolve_omp_parallel_blocks (code, ns);
6196 break;
6197 case EXEC_OMP_PARALLEL:
6198 case EXEC_OMP_PARALLEL_DO:
6199 case EXEC_OMP_PARALLEL_SECTIONS:
6200 omp_workshare_save = omp_workshare_flag;
6201 omp_workshare_flag = 0;
6202 gfc_resolve_omp_parallel_blocks (code, ns);
6203 break;
6204 case EXEC_OMP_DO:
6205 gfc_resolve_omp_do_blocks (code, ns);
6206 break;
6207 case EXEC_OMP_WORKSHARE:
6208 omp_workshare_save = omp_workshare_flag;
6209 omp_workshare_flag = 1;
6210 /* FALLTHROUGH */
6211 default:
6212 gfc_resolve_blocks (code->block, ns);
6213 break;
6216 if (omp_workshare_save != -1)
6217 omp_workshare_flag = omp_workshare_save;
6220 t = gfc_resolve_expr (code->expr);
6221 forall_flag = forall_save;
6223 if (gfc_resolve_expr (code->expr2) == FAILURE)
6224 t = FAILURE;
6226 switch (code->op)
6228 case EXEC_NOP:
6229 case EXEC_CYCLE:
6230 case EXEC_PAUSE:
6231 case EXEC_STOP:
6232 case EXEC_EXIT:
6233 case EXEC_CONTINUE:
6234 case EXEC_DT_END:
6235 break;
6237 case EXEC_ENTRY:
6238 /* Keep track of which entry we are up to. */
6239 current_entry_id = code->ext.entry->id;
6240 break;
6242 case EXEC_WHERE:
6243 resolve_where (code, NULL);
6244 break;
6246 case EXEC_GOTO:
6247 if (code->expr != NULL)
6249 if (code->expr->ts.type != BT_INTEGER)
6250 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6251 "INTEGER variable", &code->expr->where);
6252 else if (code->expr->symtree->n.sym->attr.assign != 1)
6253 gfc_error ("Variable '%s' has not been assigned a target "
6254 "label at %L", code->expr->symtree->n.sym->name,
6255 &code->expr->where);
6257 else
6258 resolve_branch (code->label, code);
6259 break;
6261 case EXEC_RETURN:
6262 if (code->expr != NULL
6263 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
6264 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6265 "INTEGER return specifier", &code->expr->where);
6266 break;
6268 case EXEC_INIT_ASSIGN:
6269 break;
6271 case EXEC_ASSIGN:
6272 if (t == FAILURE)
6273 break;
6275 if (resolve_ordinary_assign (code, ns))
6276 goto call;
6278 break;
6280 case EXEC_LABEL_ASSIGN:
6281 if (code->label->defined == ST_LABEL_UNKNOWN)
6282 gfc_error ("Label %d referenced at %L is never defined",
6283 code->label->value, &code->label->where);
6284 if (t == SUCCESS
6285 && (code->expr->expr_type != EXPR_VARIABLE
6286 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
6287 || code->expr->symtree->n.sym->ts.kind
6288 != gfc_default_integer_kind
6289 || code->expr->symtree->n.sym->as != NULL))
6290 gfc_error ("ASSIGN statement at %L requires a scalar "
6291 "default INTEGER variable", &code->expr->where);
6292 break;
6294 case EXEC_POINTER_ASSIGN:
6295 if (t == FAILURE)
6296 break;
6298 gfc_check_pointer_assign (code->expr, code->expr2);
6299 break;
6301 case EXEC_ARITHMETIC_IF:
6302 if (t == SUCCESS
6303 && code->expr->ts.type != BT_INTEGER
6304 && code->expr->ts.type != BT_REAL)
6305 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6306 "expression", &code->expr->where);
6308 resolve_branch (code->label, code);
6309 resolve_branch (code->label2, code);
6310 resolve_branch (code->label3, code);
6311 break;
6313 case EXEC_IF:
6314 if (t == SUCCESS && code->expr != NULL
6315 && (code->expr->ts.type != BT_LOGICAL
6316 || code->expr->rank != 0))
6317 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6318 &code->expr->where);
6319 break;
6321 case EXEC_CALL:
6322 call:
6323 resolve_call (code);
6324 break;
6326 case EXEC_SELECT:
6327 /* Select is complicated. Also, a SELECT construct could be
6328 a transformed computed GOTO. */
6329 resolve_select (code);
6330 break;
6332 case EXEC_DO:
6333 if (code->ext.iterator != NULL)
6335 gfc_iterator *iter = code->ext.iterator;
6336 if (gfc_resolve_iterator (iter, true) != FAILURE)
6337 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
6339 break;
6341 case EXEC_DO_WHILE:
6342 if (code->expr == NULL)
6343 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
6344 if (t == SUCCESS
6345 && (code->expr->rank != 0
6346 || code->expr->ts.type != BT_LOGICAL))
6347 gfc_error ("Exit condition of DO WHILE loop at %L must be "
6348 "a scalar LOGICAL expression", &code->expr->where);
6349 break;
6351 case EXEC_ALLOCATE:
6352 if (t == SUCCESS)
6353 resolve_allocate_deallocate (code, "ALLOCATE");
6355 break;
6357 case EXEC_DEALLOCATE:
6358 if (t == SUCCESS)
6359 resolve_allocate_deallocate (code, "DEALLOCATE");
6361 break;
6363 case EXEC_OPEN:
6364 if (gfc_resolve_open (code->ext.open) == FAILURE)
6365 break;
6367 resolve_branch (code->ext.open->err, code);
6368 break;
6370 case EXEC_CLOSE:
6371 if (gfc_resolve_close (code->ext.close) == FAILURE)
6372 break;
6374 resolve_branch (code->ext.close->err, code);
6375 break;
6377 case EXEC_BACKSPACE:
6378 case EXEC_ENDFILE:
6379 case EXEC_REWIND:
6380 case EXEC_FLUSH:
6381 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
6382 break;
6384 resolve_branch (code->ext.filepos->err, code);
6385 break;
6387 case EXEC_INQUIRE:
6388 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6389 break;
6391 resolve_branch (code->ext.inquire->err, code);
6392 break;
6394 case EXEC_IOLENGTH:
6395 gcc_assert (code->ext.inquire != NULL);
6396 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6397 break;
6399 resolve_branch (code->ext.inquire->err, code);
6400 break;
6402 case EXEC_READ:
6403 case EXEC_WRITE:
6404 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
6405 break;
6407 resolve_branch (code->ext.dt->err, code);
6408 resolve_branch (code->ext.dt->end, code);
6409 resolve_branch (code->ext.dt->eor, code);
6410 break;
6412 case EXEC_TRANSFER:
6413 resolve_transfer (code);
6414 break;
6416 case EXEC_FORALL:
6417 resolve_forall_iterators (code->ext.forall_iterator);
6419 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
6420 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
6421 "expression", &code->expr->where);
6422 break;
6424 case EXEC_OMP_ATOMIC:
6425 case EXEC_OMP_BARRIER:
6426 case EXEC_OMP_CRITICAL:
6427 case EXEC_OMP_FLUSH:
6428 case EXEC_OMP_DO:
6429 case EXEC_OMP_MASTER:
6430 case EXEC_OMP_ORDERED:
6431 case EXEC_OMP_SECTIONS:
6432 case EXEC_OMP_SINGLE:
6433 case EXEC_OMP_WORKSHARE:
6434 gfc_resolve_omp_directive (code, ns);
6435 break;
6437 case EXEC_OMP_PARALLEL:
6438 case EXEC_OMP_PARALLEL_DO:
6439 case EXEC_OMP_PARALLEL_SECTIONS:
6440 case EXEC_OMP_PARALLEL_WORKSHARE:
6441 omp_workshare_save = omp_workshare_flag;
6442 omp_workshare_flag = 0;
6443 gfc_resolve_omp_directive (code, ns);
6444 omp_workshare_flag = omp_workshare_save;
6445 break;
6447 default:
6448 gfc_internal_error ("resolve_code(): Bad statement code");
6452 cs_base = frame.prev;
6456 /* Resolve initial values and make sure they are compatible with
6457 the variable. */
6459 static void
6460 resolve_values (gfc_symbol *sym)
6462 if (sym->value == NULL)
6463 return;
6465 if (gfc_resolve_expr (sym->value) == FAILURE)
6466 return;
6468 gfc_check_assign_symbol (sym, sym->value);
6472 /* Verify the binding labels for common blocks that are BIND(C). The label
6473 for a BIND(C) common block must be identical in all scoping units in which
6474 the common block is declared. Further, the binding label can not collide
6475 with any other global entity in the program. */
6477 static void
6478 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
6480 if (comm_block_tree->n.common->is_bind_c == 1)
6482 gfc_gsymbol *binding_label_gsym;
6483 gfc_gsymbol *comm_name_gsym;
6485 /* See if a global symbol exists by the common block's name. It may
6486 be NULL if the common block is use-associated. */
6487 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
6488 comm_block_tree->n.common->name);
6489 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
6490 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
6491 "with the global entity '%s' at %L",
6492 comm_block_tree->n.common->binding_label,
6493 comm_block_tree->n.common->name,
6494 &(comm_block_tree->n.common->where),
6495 comm_name_gsym->name, &(comm_name_gsym->where));
6496 else if (comm_name_gsym != NULL
6497 && strcmp (comm_name_gsym->name,
6498 comm_block_tree->n.common->name) == 0)
6500 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
6501 as expected. */
6502 if (comm_name_gsym->binding_label == NULL)
6503 /* No binding label for common block stored yet; save this one. */
6504 comm_name_gsym->binding_label =
6505 comm_block_tree->n.common->binding_label;
6506 else
6507 if (strcmp (comm_name_gsym->binding_label,
6508 comm_block_tree->n.common->binding_label) != 0)
6510 /* Common block names match but binding labels do not. */
6511 gfc_error ("Binding label '%s' for common block '%s' at %L "
6512 "does not match the binding label '%s' for common "
6513 "block '%s' at %L",
6514 comm_block_tree->n.common->binding_label,
6515 comm_block_tree->n.common->name,
6516 &(comm_block_tree->n.common->where),
6517 comm_name_gsym->binding_label,
6518 comm_name_gsym->name,
6519 &(comm_name_gsym->where));
6520 return;
6524 /* There is no binding label (NAME="") so we have nothing further to
6525 check and nothing to add as a global symbol for the label. */
6526 if (comm_block_tree->n.common->binding_label[0] == '\0' )
6527 return;
6529 binding_label_gsym =
6530 gfc_find_gsymbol (gfc_gsym_root,
6531 comm_block_tree->n.common->binding_label);
6532 if (binding_label_gsym == NULL)
6534 /* Need to make a global symbol for the binding label to prevent
6535 it from colliding with another. */
6536 binding_label_gsym =
6537 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
6538 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
6539 binding_label_gsym->type = GSYM_COMMON;
6541 else
6543 /* If comm_name_gsym is NULL, the name common block is use
6544 associated and the name could be colliding. */
6545 if (binding_label_gsym->type != GSYM_COMMON)
6546 gfc_error ("Binding label '%s' for common block '%s' at %L "
6547 "collides with the global entity '%s' at %L",
6548 comm_block_tree->n.common->binding_label,
6549 comm_block_tree->n.common->name,
6550 &(comm_block_tree->n.common->where),
6551 binding_label_gsym->name,
6552 &(binding_label_gsym->where));
6553 else if (comm_name_gsym != NULL
6554 && (strcmp (binding_label_gsym->name,
6555 comm_name_gsym->binding_label) != 0)
6556 && (strcmp (binding_label_gsym->sym_name,
6557 comm_name_gsym->name) != 0))
6558 gfc_error ("Binding label '%s' for common block '%s' at %L "
6559 "collides with global entity '%s' at %L",
6560 binding_label_gsym->name, binding_label_gsym->sym_name,
6561 &(comm_block_tree->n.common->where),
6562 comm_name_gsym->name, &(comm_name_gsym->where));
6566 return;
6570 /* Verify any BIND(C) derived types in the namespace so we can report errors
6571 for them once, rather than for each variable declared of that type. */
6573 static void
6574 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
6576 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
6577 && derived_sym->attr.is_bind_c == 1)
6578 verify_bind_c_derived_type (derived_sym);
6580 return;
6584 /* Verify that any binding labels used in a given namespace do not collide
6585 with the names or binding labels of any global symbols. */
6587 static void
6588 gfc_verify_binding_labels (gfc_symbol *sym)
6590 int has_error = 0;
6592 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
6593 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
6595 gfc_gsymbol *bind_c_sym;
6597 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
6598 if (bind_c_sym != NULL
6599 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
6601 if (sym->attr.if_source == IFSRC_DECL
6602 && (bind_c_sym->type != GSYM_SUBROUTINE
6603 && bind_c_sym->type != GSYM_FUNCTION)
6604 && ((sym->attr.contained == 1
6605 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
6606 || (sym->attr.use_assoc == 1
6607 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
6609 /* Make sure global procedures don't collide with anything. */
6610 gfc_error ("Binding label '%s' at %L collides with the global "
6611 "entity '%s' at %L", sym->binding_label,
6612 &(sym->declared_at), bind_c_sym->name,
6613 &(bind_c_sym->where));
6614 has_error = 1;
6616 else if (sym->attr.contained == 0
6617 && (sym->attr.if_source == IFSRC_IFBODY
6618 && sym->attr.flavor == FL_PROCEDURE)
6619 && (bind_c_sym->sym_name != NULL
6620 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
6622 /* Make sure procedures in interface bodies don't collide. */
6623 gfc_error ("Binding label '%s' in interface body at %L collides "
6624 "with the global entity '%s' at %L",
6625 sym->binding_label,
6626 &(sym->declared_at), bind_c_sym->name,
6627 &(bind_c_sym->where));
6628 has_error = 1;
6630 else if (sym->attr.contained == 0
6631 && (sym->attr.if_source == IFSRC_UNKNOWN))
6632 if ((sym->attr.use_assoc
6633 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))
6634 || sym->attr.use_assoc == 0)
6636 gfc_error ("Binding label '%s' at %L collides with global "
6637 "entity '%s' at %L", sym->binding_label,
6638 &(sym->declared_at), bind_c_sym->name,
6639 &(bind_c_sym->where));
6640 has_error = 1;
6643 if (has_error != 0)
6644 /* Clear the binding label to prevent checking multiple times. */
6645 sym->binding_label[0] = '\0';
6647 else if (bind_c_sym == NULL)
6649 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
6650 bind_c_sym->where = sym->declared_at;
6651 bind_c_sym->sym_name = sym->name;
6653 if (sym->attr.use_assoc == 1)
6654 bind_c_sym->mod_name = sym->module;
6655 else
6656 if (sym->ns->proc_name != NULL)
6657 bind_c_sym->mod_name = sym->ns->proc_name->name;
6659 if (sym->attr.contained == 0)
6661 if (sym->attr.subroutine)
6662 bind_c_sym->type = GSYM_SUBROUTINE;
6663 else if (sym->attr.function)
6664 bind_c_sym->type = GSYM_FUNCTION;
6668 return;
6672 /* Resolve an index expression. */
6674 static try
6675 resolve_index_expr (gfc_expr *e)
6677 if (gfc_resolve_expr (e) == FAILURE)
6678 return FAILURE;
6680 if (gfc_simplify_expr (e, 0) == FAILURE)
6681 return FAILURE;
6683 if (gfc_specification_expr (e) == FAILURE)
6684 return FAILURE;
6686 return SUCCESS;
6689 /* Resolve a charlen structure. */
6691 static try
6692 resolve_charlen (gfc_charlen *cl)
6694 int i;
6696 if (cl->resolved)
6697 return SUCCESS;
6699 cl->resolved = 1;
6701 specification_expr = 1;
6703 if (resolve_index_expr (cl->length) == FAILURE)
6705 specification_expr = 0;
6706 return FAILURE;
6709 /* "If the character length parameter value evaluates to a negative
6710 value, the length of character entities declared is zero." */
6711 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
6713 gfc_warning_now ("CHARACTER variable has zero length at %L",
6714 &cl->length->where);
6715 gfc_replace_expr (cl->length, gfc_int_expr (0));
6718 return SUCCESS;
6722 /* Test for non-constant shape arrays. */
6724 static bool
6725 is_non_constant_shape_array (gfc_symbol *sym)
6727 gfc_expr *e;
6728 int i;
6729 bool not_constant;
6731 not_constant = false;
6732 if (sym->as != NULL)
6734 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
6735 has not been simplified; parameter array references. Do the
6736 simplification now. */
6737 for (i = 0; i < sym->as->rank; i++)
6739 e = sym->as->lower[i];
6740 if (e && (resolve_index_expr (e) == FAILURE
6741 || !gfc_is_constant_expr (e)))
6742 not_constant = true;
6744 e = sym->as->upper[i];
6745 if (e && (resolve_index_expr (e) == FAILURE
6746 || !gfc_is_constant_expr (e)))
6747 not_constant = true;
6750 return not_constant;
6753 /* Given a symbol and an initialization expression, add code to initialize
6754 the symbol to the function entry. */
6755 static void
6756 build_init_assign (gfc_symbol *sym, gfc_expr *init)
6758 gfc_expr *lval;
6759 gfc_code *init_st;
6760 gfc_namespace *ns = sym->ns;
6762 /* Search for the function namespace if this is a contained
6763 function without an explicit result. */
6764 if (sym->attr.function && sym == sym->result
6765 && sym->name != sym->ns->proc_name->name)
6767 ns = ns->contained;
6768 for (;ns; ns = ns->sibling)
6769 if (strcmp (ns->proc_name->name, sym->name) == 0)
6770 break;
6773 if (ns == NULL)
6775 gfc_free_expr (init);
6776 return;
6779 /* Build an l-value expression for the result. */
6780 lval = gfc_lval_expr_from_sym (sym);
6782 /* Add the code at scope entry. */
6783 init_st = gfc_get_code ();
6784 init_st->next = ns->code;
6785 ns->code = init_st;
6787 /* Assign the default initializer to the l-value. */
6788 init_st->loc = sym->declared_at;
6789 init_st->op = EXEC_INIT_ASSIGN;
6790 init_st->expr = lval;
6791 init_st->expr2 = init;
6794 /* Assign the default initializer to a derived type variable or result. */
6796 static void
6797 apply_default_init (gfc_symbol *sym)
6799 gfc_expr *init = NULL;
6801 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6802 return;
6804 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
6805 init = gfc_default_initializer (&sym->ts);
6807 if (init == NULL)
6808 return;
6810 build_init_assign (sym, init);
6813 /* Build an initializer for a local integer, real, complex, logical, or
6814 character variable, based on the command line flags finit-local-zero,
6815 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
6816 null if the symbol should not have a default initialization. */
6817 static gfc_expr *
6818 build_default_init_expr (gfc_symbol *sym)
6820 int char_len;
6821 gfc_expr *init_expr;
6822 int i;
6823 char *ch;
6825 /* These symbols should never have a default initialization. */
6826 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
6827 || sym->attr.external
6828 || sym->attr.dummy
6829 || sym->attr.pointer
6830 || sym->attr.in_equivalence
6831 || sym->attr.in_common
6832 || sym->attr.data
6833 || sym->module
6834 || sym->attr.cray_pointee
6835 || sym->attr.cray_pointer)
6836 return NULL;
6838 /* Now we'll try to build an initializer expression. */
6839 init_expr = gfc_get_expr ();
6840 init_expr->expr_type = EXPR_CONSTANT;
6841 init_expr->ts.type = sym->ts.type;
6842 init_expr->ts.kind = sym->ts.kind;
6843 init_expr->where = sym->declared_at;
6845 /* We will only initialize integers, reals, complex, logicals, and
6846 characters, and only if the corresponding command-line flags
6847 were set. Otherwise, we free init_expr and return null. */
6848 switch (sym->ts.type)
6850 case BT_INTEGER:
6851 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
6852 mpz_init_set_si (init_expr->value.integer,
6853 gfc_option.flag_init_integer_value);
6854 else
6856 gfc_free_expr (init_expr);
6857 init_expr = NULL;
6859 break;
6861 case BT_REAL:
6862 mpfr_init (init_expr->value.real);
6863 switch (gfc_option.flag_init_real)
6865 case GFC_INIT_REAL_NAN:
6866 mpfr_set_nan (init_expr->value.real);
6867 break;
6869 case GFC_INIT_REAL_INF:
6870 mpfr_set_inf (init_expr->value.real, 1);
6871 break;
6873 case GFC_INIT_REAL_NEG_INF:
6874 mpfr_set_inf (init_expr->value.real, -1);
6875 break;
6877 case GFC_INIT_REAL_ZERO:
6878 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
6879 break;
6881 default:
6882 gfc_free_expr (init_expr);
6883 init_expr = NULL;
6884 break;
6886 break;
6888 case BT_COMPLEX:
6889 mpfr_init (init_expr->value.complex.r);
6890 mpfr_init (init_expr->value.complex.i);
6891 switch (gfc_option.flag_init_real)
6893 case GFC_INIT_REAL_NAN:
6894 mpfr_set_nan (init_expr->value.complex.r);
6895 mpfr_set_nan (init_expr->value.complex.i);
6896 break;
6898 case GFC_INIT_REAL_INF:
6899 mpfr_set_inf (init_expr->value.complex.r, 1);
6900 mpfr_set_inf (init_expr->value.complex.i, 1);
6901 break;
6903 case GFC_INIT_REAL_NEG_INF:
6904 mpfr_set_inf (init_expr->value.complex.r, -1);
6905 mpfr_set_inf (init_expr->value.complex.i, -1);
6906 break;
6908 case GFC_INIT_REAL_ZERO:
6909 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
6910 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
6911 break;
6913 default:
6914 gfc_free_expr (init_expr);
6915 init_expr = NULL;
6916 break;
6918 break;
6920 case BT_LOGICAL:
6921 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
6922 init_expr->value.logical = 0;
6923 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
6924 init_expr->value.logical = 1;
6925 else
6927 gfc_free_expr (init_expr);
6928 init_expr = NULL;
6930 break;
6932 case BT_CHARACTER:
6933 /* For characters, the length must be constant in order to
6934 create a default initializer. */
6935 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
6936 && sym->ts.cl->length
6937 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
6939 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
6940 init_expr->value.character.length = char_len;
6941 init_expr->value.character.string = gfc_getmem (char_len+1);
6942 ch = init_expr->value.character.string;
6943 for (i = 0; i < char_len; i++)
6944 *(ch++) = gfc_option.flag_init_character_value;
6946 else
6948 gfc_free_expr (init_expr);
6949 init_expr = NULL;
6951 break;
6953 default:
6954 gfc_free_expr (init_expr);
6955 init_expr = NULL;
6957 return init_expr;
6960 /* Add an initialization expression to a local variable. */
6961 static void
6962 apply_default_init_local (gfc_symbol *sym)
6964 gfc_expr *init = NULL;
6966 /* The symbol should be a variable or a function return value. */
6967 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6968 || (sym->attr.function && sym->result != sym))
6969 return;
6971 /* Try to build the initializer expression. If we can't initialize
6972 this symbol, then init will be NULL. */
6973 init = build_default_init_expr (sym);
6974 if (init == NULL)
6975 return;
6977 /* For saved variables, we don't want to add an initializer at
6978 function entry, so we just add a static initializer. */
6979 if (sym->attr.save || sym->ns->save_all)
6981 /* Don't clobber an existing initializer! */
6982 gcc_assert (sym->value == NULL);
6983 sym->value = init;
6984 return;
6987 build_init_assign (sym, init);
6990 /* Resolution of common features of flavors variable and procedure. */
6992 static try
6993 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
6995 /* Constraints on deferred shape variable. */
6996 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
6998 if (sym->attr.allocatable)
7000 if (sym->attr.dimension)
7001 gfc_error ("Allocatable array '%s' at %L must have "
7002 "a deferred shape", sym->name, &sym->declared_at);
7003 else
7004 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
7005 sym->name, &sym->declared_at);
7006 return FAILURE;
7009 if (sym->attr.pointer && sym->attr.dimension)
7011 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
7012 sym->name, &sym->declared_at);
7013 return FAILURE;
7017 else
7019 if (!mp_flag && !sym->attr.allocatable
7020 && !sym->attr.pointer && !sym->attr.dummy)
7022 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7023 sym->name, &sym->declared_at);
7024 return FAILURE;
7027 return SUCCESS;
7031 /* Additional checks for symbols with flavor variable and derived
7032 type. To be called from resolve_fl_variable. */
7034 static try
7035 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
7037 gcc_assert (sym->ts.type == BT_DERIVED);
7039 /* Check to see if a derived type is blocked from being host
7040 associated by the presence of another class I symbol in the same
7041 namespace. 14.6.1.3 of the standard and the discussion on
7042 comp.lang.fortran. */
7043 if (sym->ns != sym->ts.derived->ns
7044 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
7046 gfc_symbol *s;
7047 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
7048 if (s && (s->attr.flavor != FL_DERIVED
7049 || !gfc_compare_derived_types (s, sym->ts.derived)))
7051 gfc_error ("The type '%s' cannot be host associated at %L "
7052 "because it is blocked by an incompatible object "
7053 "of the same name declared at %L",
7054 sym->ts.derived->name, &sym->declared_at,
7055 &s->declared_at);
7056 return FAILURE;
7060 /* 4th constraint in section 11.3: "If an object of a type for which
7061 component-initialization is specified (R429) appears in the
7062 specification-part of a module and does not have the ALLOCATABLE
7063 or POINTER attribute, the object shall have the SAVE attribute."
7065 The check for initializers is performed with
7066 has_default_initializer because gfc_default_initializer generates
7067 a hidden default for allocatable components. */
7068 if (!(sym->value || no_init_flag) && sym->ns->proc_name
7069 && sym->ns->proc_name->attr.flavor == FL_MODULE
7070 && !sym->ns->save_all && !sym->attr.save
7071 && !sym->attr.pointer && !sym->attr.allocatable
7072 && has_default_initializer (sym->ts.derived))
7074 gfc_error("Object '%s' at %L must have the SAVE attribute for "
7075 "default initialization of a component",
7076 sym->name, &sym->declared_at);
7077 return FAILURE;
7080 /* Assign default initializer. */
7081 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
7082 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
7084 sym->value = gfc_default_initializer (&sym->ts);
7087 return SUCCESS;
7091 /* Resolve symbols with flavor variable. */
7093 static try
7094 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
7096 int no_init_flag, automatic_flag;
7097 gfc_expr *e;
7098 const char *auto_save_msg;
7100 auto_save_msg = "Automatic object '%s' at %L cannot have the "
7101 "SAVE attribute";
7103 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7104 return FAILURE;
7106 /* Set this flag to check that variables are parameters of all entries.
7107 This check is effected by the call to gfc_resolve_expr through
7108 is_non_constant_shape_array. */
7109 specification_expr = 1;
7111 if (sym->ns->proc_name
7112 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7113 || sym->ns->proc_name->attr.is_main_program)
7114 && !sym->attr.use_assoc
7115 && !sym->attr.allocatable
7116 && !sym->attr.pointer
7117 && is_non_constant_shape_array (sym))
7119 /* The shape of a main program or module array needs to be
7120 constant. */
7121 gfc_error ("The module or main program array '%s' at %L must "
7122 "have constant shape", sym->name, &sym->declared_at);
7123 specification_expr = 0;
7124 return FAILURE;
7127 if (sym->ts.type == BT_CHARACTER)
7129 /* Make sure that character string variables with assumed length are
7130 dummy arguments. */
7131 e = sym->ts.cl->length;
7132 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
7134 gfc_error ("Entity with assumed character length at %L must be a "
7135 "dummy argument or a PARAMETER", &sym->declared_at);
7136 return FAILURE;
7139 if (e && sym->attr.save && !gfc_is_constant_expr (e))
7141 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7142 return FAILURE;
7145 if (!gfc_is_constant_expr (e)
7146 && !(e->expr_type == EXPR_VARIABLE
7147 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
7148 && sym->ns->proc_name
7149 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7150 || sym->ns->proc_name->attr.is_main_program)
7151 && !sym->attr.use_assoc)
7153 gfc_error ("'%s' at %L must have constant character length "
7154 "in this context", sym->name, &sym->declared_at);
7155 return FAILURE;
7159 if (sym->value == NULL && sym->attr.referenced)
7160 apply_default_init_local (sym); /* Try to apply a default initialization. */
7162 /* Determine if the symbol may not have an initializer. */
7163 no_init_flag = automatic_flag = 0;
7164 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
7165 || sym->attr.intrinsic || sym->attr.result)
7166 no_init_flag = 1;
7167 else if (sym->attr.dimension && !sym->attr.pointer
7168 && is_non_constant_shape_array (sym))
7170 no_init_flag = automatic_flag = 1;
7172 /* Also, they must not have the SAVE attribute.
7173 SAVE_IMPLICIT is checked below. */
7174 if (sym->attr.save == SAVE_EXPLICIT)
7176 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7177 return FAILURE;
7181 /* Reject illegal initializers. */
7182 if (!sym->mark && sym->value)
7184 if (sym->attr.allocatable)
7185 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7186 sym->name, &sym->declared_at);
7187 else if (sym->attr.external)
7188 gfc_error ("External '%s' at %L cannot have an initializer",
7189 sym->name, &sym->declared_at);
7190 else if (sym->attr.dummy
7191 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7192 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7193 sym->name, &sym->declared_at);
7194 else if (sym->attr.intrinsic)
7195 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7196 sym->name, &sym->declared_at);
7197 else if (sym->attr.result)
7198 gfc_error ("Function result '%s' at %L cannot have an initializer",
7199 sym->name, &sym->declared_at);
7200 else if (automatic_flag)
7201 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7202 sym->name, &sym->declared_at);
7203 else
7204 goto no_init_error;
7205 return FAILURE;
7208 no_init_error:
7209 if (sym->ts.type == BT_DERIVED)
7210 return resolve_fl_variable_derived (sym, no_init_flag);
7212 return SUCCESS;
7216 /* Resolve a procedure. */
7218 static try
7219 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7221 gfc_formal_arglist *arg;
7223 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7224 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7225 "interfaces", sym->name, &sym->declared_at);
7227 if (sym->attr.function
7228 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7229 return FAILURE;
7231 if (sym->ts.type == BT_CHARACTER)
7233 gfc_charlen *cl = sym->ts.cl;
7235 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7236 && resolve_charlen (cl) == FAILURE)
7237 return FAILURE;
7239 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7241 if (sym->attr.proc == PROC_ST_FUNCTION)
7243 gfc_error ("Character-valued statement function '%s' at %L must "
7244 "have constant length", sym->name, &sym->declared_at);
7245 return FAILURE;
7248 if (sym->attr.external && sym->formal == NULL
7249 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7251 gfc_error ("Automatic character length function '%s' at %L must "
7252 "have an explicit interface", sym->name,
7253 &sym->declared_at);
7254 return FAILURE;
7259 /* Ensure that derived type for are not of a private type. Internal
7260 module procedures are excluded by 2.2.3.3 - ie. they are not
7261 externally accessible and can access all the objects accessible in
7262 the host. */
7263 if (!(sym->ns->parent
7264 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7265 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7267 gfc_interface *iface;
7269 for (arg = sym->formal; arg; arg = arg->next)
7271 if (arg->sym
7272 && arg->sym->ts.type == BT_DERIVED
7273 && !arg->sym->ts.derived->attr.use_assoc
7274 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7275 arg->sym->ts.derived->ns->default_access)
7276 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7277 "PRIVATE type and cannot be a dummy argument"
7278 " of '%s', which is PUBLIC at %L",
7279 arg->sym->name, sym->name, &sym->declared_at)
7280 == FAILURE)
7282 /* Stop this message from recurring. */
7283 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7284 return FAILURE;
7288 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7289 PRIVATE to the containing module. */
7290 for (iface = sym->generic; iface; iface = iface->next)
7292 for (arg = iface->sym->formal; arg; arg = arg->next)
7294 if (arg->sym
7295 && arg->sym->ts.type == BT_DERIVED
7296 && !arg->sym->ts.derived->attr.use_assoc
7297 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7298 arg->sym->ts.derived->ns->default_access)
7299 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7300 "'%s' in PUBLIC interface '%s' at %L "
7301 "takes dummy arguments of '%s' which is "
7302 "PRIVATE", iface->sym->name, sym->name,
7303 &iface->sym->declared_at,
7304 gfc_typename (&arg->sym->ts)) == FAILURE)
7306 /* Stop this message from recurring. */
7307 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7308 return FAILURE;
7313 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7314 PRIVATE to the containing module. */
7315 for (iface = sym->generic; iface; iface = iface->next)
7317 for (arg = iface->sym->formal; arg; arg = arg->next)
7319 if (arg->sym
7320 && arg->sym->ts.type == BT_DERIVED
7321 && !arg->sym->ts.derived->attr.use_assoc
7322 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7323 arg->sym->ts.derived->ns->default_access)
7324 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7325 "'%s' in PUBLIC interface '%s' at %L "
7326 "takes dummy arguments of '%s' which is "
7327 "PRIVATE", iface->sym->name, sym->name,
7328 &iface->sym->declared_at,
7329 gfc_typename (&arg->sym->ts)) == FAILURE)
7331 /* Stop this message from recurring. */
7332 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7333 return FAILURE;
7339 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION)
7341 gfc_error ("Function '%s' at %L cannot have an initializer",
7342 sym->name, &sym->declared_at);
7343 return FAILURE;
7346 /* An external symbol may not have an initializer because it is taken to be
7347 a procedure. */
7348 if (sym->attr.external && sym->value)
7350 gfc_error ("External object '%s' at %L may not have an initializer",
7351 sym->name, &sym->declared_at);
7352 return FAILURE;
7355 /* An elemental function is required to return a scalar 12.7.1 */
7356 if (sym->attr.elemental && sym->attr.function && sym->as)
7358 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
7359 "result", sym->name, &sym->declared_at);
7360 /* Reset so that the error only occurs once. */
7361 sym->attr.elemental = 0;
7362 return FAILURE;
7365 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
7366 char-len-param shall not be array-valued, pointer-valued, recursive
7367 or pure. ....snip... A character value of * may only be used in the
7368 following ways: (i) Dummy arg of procedure - dummy associates with
7369 actual length; (ii) To declare a named constant; or (iii) External
7370 function - but length must be declared in calling scoping unit. */
7371 if (sym->attr.function
7372 && sym->ts.type == BT_CHARACTER
7373 && sym->ts.cl && sym->ts.cl->length == NULL)
7375 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
7376 || (sym->attr.recursive) || (sym->attr.pure))
7378 if (sym->as && sym->as->rank)
7379 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7380 "array-valued", sym->name, &sym->declared_at);
7382 if (sym->attr.pointer)
7383 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7384 "pointer-valued", sym->name, &sym->declared_at);
7386 if (sym->attr.pure)
7387 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7388 "pure", sym->name, &sym->declared_at);
7390 if (sym->attr.recursive)
7391 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7392 "recursive", sym->name, &sym->declared_at);
7394 return FAILURE;
7397 /* Appendix B.2 of the standard. Contained functions give an
7398 error anyway. Fixed-form is likely to be F77/legacy. */
7399 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
7400 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
7401 "'%s' at %L is obsolescent in fortran 95",
7402 sym->name, &sym->declared_at);
7405 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
7407 gfc_formal_arglist *curr_arg;
7408 int has_non_interop_arg = 0;
7410 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7411 sym->common_block) == FAILURE)
7413 /* Clear these to prevent looking at them again if there was an
7414 error. */
7415 sym->attr.is_bind_c = 0;
7416 sym->attr.is_c_interop = 0;
7417 sym->ts.is_c_interop = 0;
7419 else
7421 /* So far, no errors have been found. */
7422 sym->attr.is_c_interop = 1;
7423 sym->ts.is_c_interop = 1;
7426 curr_arg = sym->formal;
7427 while (curr_arg != NULL)
7429 /* Skip implicitly typed dummy args here. */
7430 if (curr_arg->sym->attr.implicit_type == 0)
7431 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
7432 /* If something is found to fail, record the fact so we
7433 can mark the symbol for the procedure as not being
7434 BIND(C) to try and prevent multiple errors being
7435 reported. */
7436 has_non_interop_arg = 1;
7438 curr_arg = curr_arg->next;
7441 /* See if any of the arguments were not interoperable and if so, clear
7442 the procedure symbol to prevent duplicate error messages. */
7443 if (has_non_interop_arg != 0)
7445 sym->attr.is_c_interop = 0;
7446 sym->ts.is_c_interop = 0;
7447 sym->attr.is_bind_c = 0;
7451 return SUCCESS;
7455 /* Resolve the components of a derived type. */
7457 static try
7458 resolve_fl_derived (gfc_symbol *sym)
7460 gfc_component *c;
7461 gfc_dt_list * dt_list;
7462 int i;
7464 for (c = sym->components; c != NULL; c = c->next)
7466 if (c->ts.type == BT_CHARACTER)
7468 if (c->ts.cl->length == NULL
7469 || (resolve_charlen (c->ts.cl) == FAILURE)
7470 || !gfc_is_constant_expr (c->ts.cl->length))
7472 gfc_error ("Character length of component '%s' needs to "
7473 "be a constant specification expression at %L",
7474 c->name,
7475 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
7476 return FAILURE;
7480 if (c->ts.type == BT_DERIVED
7481 && sym->component_access != ACCESS_PRIVATE
7482 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7483 && !c->ts.derived->attr.use_assoc
7484 && !gfc_check_access (c->ts.derived->attr.access,
7485 c->ts.derived->ns->default_access))
7487 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
7488 "a component of '%s', which is PUBLIC at %L",
7489 c->name, sym->name, &sym->declared_at);
7490 return FAILURE;
7493 if (sym->attr.sequence)
7495 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
7497 gfc_error ("Component %s of SEQUENCE type declared at %L does "
7498 "not have the SEQUENCE attribute",
7499 c->ts.derived->name, &sym->declared_at);
7500 return FAILURE;
7504 if (c->ts.type == BT_DERIVED && c->pointer
7505 && c->ts.derived->components == NULL)
7507 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
7508 "that has not been declared", c->name, sym->name,
7509 &c->loc);
7510 return FAILURE;
7513 if (c->pointer || c->allocatable || c->as == NULL)
7514 continue;
7516 for (i = 0; i < c->as->rank; i++)
7518 if (c->as->lower[i] == NULL
7519 || !gfc_is_constant_expr (c->as->lower[i])
7520 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
7521 || c->as->upper[i] == NULL
7522 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
7523 || !gfc_is_constant_expr (c->as->upper[i]))
7525 gfc_error ("Component '%s' of '%s' at %L must have "
7526 "constant array bounds",
7527 c->name, sym->name, &c->loc);
7528 return FAILURE;
7533 /* Add derived type to the derived type list. */
7534 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
7535 if (sym == dt_list->derived)
7536 break;
7538 if (dt_list == NULL)
7540 dt_list = gfc_get_dt_list ();
7541 dt_list->next = gfc_derived_types;
7542 dt_list->derived = sym;
7543 gfc_derived_types = dt_list;
7546 return SUCCESS;
7550 static try
7551 resolve_fl_namelist (gfc_symbol *sym)
7553 gfc_namelist *nl;
7554 gfc_symbol *nlsym;
7556 /* Reject PRIVATE objects in a PUBLIC namelist. */
7557 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
7559 for (nl = sym->namelist; nl; nl = nl->next)
7561 if (!nl->sym->attr.use_assoc
7562 && !(sym->ns->parent == nl->sym->ns)
7563 && !(sym->ns->parent
7564 && sym->ns->parent->parent == nl->sym->ns)
7565 && !gfc_check_access(nl->sym->attr.access,
7566 nl->sym->ns->default_access))
7568 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
7569 "cannot be member of PUBLIC namelist '%s' at %L",
7570 nl->sym->name, sym->name, &sym->declared_at);
7571 return FAILURE;
7574 /* Types with private components that came here by USE-association. */
7575 if (nl->sym->ts.type == BT_DERIVED
7576 && derived_inaccessible (nl->sym->ts.derived))
7578 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
7579 "components and cannot be member of namelist '%s' at %L",
7580 nl->sym->name, sym->name, &sym->declared_at);
7581 return FAILURE;
7584 /* Types with private components that are defined in the same module. */
7585 if (nl->sym->ts.type == BT_DERIVED
7586 && !(sym->ns->parent == nl->sym->ts.derived->ns)
7587 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
7588 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
7589 nl->sym->ns->default_access))
7591 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
7592 "cannot be a member of PUBLIC namelist '%s' at %L",
7593 nl->sym->name, sym->name, &sym->declared_at);
7594 return FAILURE;
7599 for (nl = sym->namelist; nl; nl = nl->next)
7601 /* Reject namelist arrays of assumed shape. */
7602 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
7603 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
7604 "must not have assumed shape in namelist "
7605 "'%s' at %L", nl->sym->name, sym->name,
7606 &sym->declared_at) == FAILURE)
7607 return FAILURE;
7609 /* Reject namelist arrays that are not constant shape. */
7610 if (is_non_constant_shape_array (nl->sym))
7612 gfc_error ("NAMELIST array object '%s' must have constant "
7613 "shape in namelist '%s' at %L", nl->sym->name,
7614 sym->name, &sym->declared_at);
7615 return FAILURE;
7618 /* Namelist objects cannot have allocatable or pointer components. */
7619 if (nl->sym->ts.type != BT_DERIVED)
7620 continue;
7622 if (nl->sym->ts.derived->attr.alloc_comp)
7624 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7625 "have ALLOCATABLE components",
7626 nl->sym->name, sym->name, &sym->declared_at);
7627 return FAILURE;
7630 if (nl->sym->ts.derived->attr.pointer_comp)
7632 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7633 "have POINTER components",
7634 nl->sym->name, sym->name, &sym->declared_at);
7635 return FAILURE;
7640 /* 14.1.2 A module or internal procedure represent local entities
7641 of the same type as a namelist member and so are not allowed. */
7642 for (nl = sym->namelist; nl; nl = nl->next)
7644 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
7645 continue;
7647 if (nl->sym->attr.function && nl->sym == nl->sym->result)
7648 if ((nl->sym == sym->ns->proc_name)
7650 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
7651 continue;
7653 nlsym = NULL;
7654 if (nl->sym && nl->sym->name)
7655 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
7656 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
7658 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
7659 "attribute in '%s' at %L", nlsym->name,
7660 &sym->declared_at);
7661 return FAILURE;
7665 return SUCCESS;
7669 static try
7670 resolve_fl_parameter (gfc_symbol *sym)
7672 /* A parameter array's shape needs to be constant. */
7673 if (sym->as != NULL
7674 && (sym->as->type == AS_DEFERRED
7675 || is_non_constant_shape_array (sym)))
7677 gfc_error ("Parameter array '%s' at %L cannot be automatic "
7678 "or of deferred shape", sym->name, &sym->declared_at);
7679 return FAILURE;
7682 /* Make sure a parameter that has been implicitly typed still
7683 matches the implicit type, since PARAMETER statements can precede
7684 IMPLICIT statements. */
7685 if (sym->attr.implicit_type
7686 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
7688 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
7689 "later IMPLICIT type", sym->name, &sym->declared_at);
7690 return FAILURE;
7693 /* Make sure the types of derived parameters are consistent. This
7694 type checking is deferred until resolution because the type may
7695 refer to a derived type from the host. */
7696 if (sym->ts.type == BT_DERIVED
7697 && !gfc_compare_types (&sym->ts, &sym->value->ts))
7699 gfc_error ("Incompatible derived type in PARAMETER at %L",
7700 &sym->value->where);
7701 return FAILURE;
7703 return SUCCESS;
7707 /* Do anything necessary to resolve a symbol. Right now, we just
7708 assume that an otherwise unknown symbol is a variable. This sort
7709 of thing commonly happens for symbols in module. */
7711 static void
7712 resolve_symbol (gfc_symbol *sym)
7714 int check_constant, mp_flag;
7715 gfc_symtree *symtree;
7716 gfc_symtree *this_symtree;
7717 gfc_namespace *ns;
7718 gfc_component *c;
7720 if (sym->attr.flavor == FL_UNKNOWN)
7723 /* If we find that a flavorless symbol is an interface in one of the
7724 parent namespaces, find its symtree in this namespace, free the
7725 symbol and set the symtree to point to the interface symbol. */
7726 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
7728 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7729 if (symtree && symtree->n.sym->generic)
7731 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
7732 sym->name);
7733 sym->refs--;
7734 if (!sym->refs)
7735 gfc_free_symbol (sym);
7736 symtree->n.sym->refs++;
7737 this_symtree->n.sym = symtree->n.sym;
7738 return;
7742 /* Otherwise give it a flavor according to such attributes as
7743 it has. */
7744 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
7745 sym->attr.flavor = FL_VARIABLE;
7746 else
7748 sym->attr.flavor = FL_PROCEDURE;
7749 if (sym->attr.dimension)
7750 sym->attr.function = 1;
7754 if (sym->attr.procedure && sym->interface
7755 && sym->attr.if_source != IFSRC_DECL)
7757 if (sym->interface->attr.procedure)
7758 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
7759 "in a later PROCEDURE statement", sym->interface->name,
7760 sym->name,&sym->declared_at);
7762 /* Get the attributes from the interface (now resolved). */
7763 if (sym->interface->attr.if_source || sym->interface->attr.intrinsic)
7765 sym->ts = sym->interface->ts;
7766 sym->attr.function = sym->interface->attr.function;
7767 sym->attr.subroutine = sym->interface->attr.subroutine;
7768 copy_formal_args (sym, sym->interface);
7770 else if (sym->interface->name[0] != '\0')
7772 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
7773 sym->interface->name, sym->name, &sym->declared_at);
7774 return;
7778 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
7779 return;
7781 /* Symbols that are module procedures with results (functions) have
7782 the types and array specification copied for type checking in
7783 procedures that call them, as well as for saving to a module
7784 file. These symbols can't stand the scrutiny that their results
7785 can. */
7786 mp_flag = (sym->result != NULL && sym->result != sym);
7789 /* Make sure that the intrinsic is consistent with its internal
7790 representation. This needs to be done before assigning a default
7791 type to avoid spurious warnings. */
7792 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
7794 if (gfc_intrinsic_name (sym->name, 0))
7796 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising)
7797 gfc_warning ("Type specified for intrinsic function '%s' at %L is ignored",
7798 sym->name, &sym->declared_at);
7800 else if (gfc_intrinsic_name (sym->name, 1))
7802 if (sym->ts.type != BT_UNKNOWN)
7804 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type specifier",
7805 sym->name, &sym->declared_at);
7806 return;
7809 else
7811 gfc_error ("Intrinsic '%s' at %L does not exist", sym->name, &sym->declared_at);
7812 return;
7816 /* Assign default type to symbols that need one and don't have one. */
7817 if (sym->ts.type == BT_UNKNOWN)
7819 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
7820 gfc_set_default_type (sym, 1, NULL);
7822 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
7824 /* The specific case of an external procedure should emit an error
7825 in the case that there is no implicit type. */
7826 if (!mp_flag)
7827 gfc_set_default_type (sym, sym->attr.external, NULL);
7828 else
7830 /* Result may be in another namespace. */
7831 resolve_symbol (sym->result);
7833 sym->ts = sym->result->ts;
7834 sym->as = gfc_copy_array_spec (sym->result->as);
7835 sym->attr.dimension = sym->result->attr.dimension;
7836 sym->attr.pointer = sym->result->attr.pointer;
7837 sym->attr.allocatable = sym->result->attr.allocatable;
7842 /* Assumed size arrays and assumed shape arrays must be dummy
7843 arguments. */
7845 if (sym->as != NULL
7846 && (sym->as->type == AS_ASSUMED_SIZE
7847 || sym->as->type == AS_ASSUMED_SHAPE)
7848 && sym->attr.dummy == 0)
7850 if (sym->as->type == AS_ASSUMED_SIZE)
7851 gfc_error ("Assumed size array at %L must be a dummy argument",
7852 &sym->declared_at);
7853 else
7854 gfc_error ("Assumed shape array at %L must be a dummy argument",
7855 &sym->declared_at);
7856 return;
7859 /* Make sure symbols with known intent or optional are really dummy
7860 variable. Because of ENTRY statement, this has to be deferred
7861 until resolution time. */
7863 if (!sym->attr.dummy
7864 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
7866 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
7867 return;
7870 if (sym->attr.value && !sym->attr.dummy)
7872 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
7873 "it is not a dummy argument", sym->name, &sym->declared_at);
7874 return;
7877 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
7879 gfc_charlen *cl = sym->ts.cl;
7880 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7882 gfc_error ("Character dummy variable '%s' at %L with VALUE "
7883 "attribute must have constant length",
7884 sym->name, &sym->declared_at);
7885 return;
7888 if (sym->ts.is_c_interop
7889 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
7891 gfc_error ("C interoperable character dummy variable '%s' at %L "
7892 "with VALUE attribute must have length one",
7893 sym->name, &sym->declared_at);
7894 return;
7898 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
7899 do this for something that was implicitly typed because that is handled
7900 in gfc_set_default_type. Handle dummy arguments and procedure
7901 definitions separately. Also, anything that is use associated is not
7902 handled here but instead is handled in the module it is declared in.
7903 Finally, derived type definitions are allowed to be BIND(C) since that
7904 only implies that they're interoperable, and they are checked fully for
7905 interoperability when a variable is declared of that type. */
7906 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
7907 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
7908 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
7910 try t = SUCCESS;
7912 /* First, make sure the variable is declared at the
7913 module-level scope (J3/04-007, Section 15.3). */
7914 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
7915 sym->attr.in_common == 0)
7917 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
7918 "is neither a COMMON block nor declared at the "
7919 "module level scope", sym->name, &(sym->declared_at));
7920 t = FAILURE;
7922 else if (sym->common_head != NULL)
7924 t = verify_com_block_vars_c_interop (sym->common_head);
7926 else
7928 /* If type() declaration, we need to verify that the components
7929 of the given type are all C interoperable, etc. */
7930 if (sym->ts.type == BT_DERIVED &&
7931 sym->ts.derived->attr.is_c_interop != 1)
7933 /* Make sure the user marked the derived type as BIND(C). If
7934 not, call the verify routine. This could print an error
7935 for the derived type more than once if multiple variables
7936 of that type are declared. */
7937 if (sym->ts.derived->attr.is_bind_c != 1)
7938 verify_bind_c_derived_type (sym->ts.derived);
7939 t = FAILURE;
7942 /* Verify the variable itself as C interoperable if it
7943 is BIND(C). It is not possible for this to succeed if
7944 the verify_bind_c_derived_type failed, so don't have to handle
7945 any error returned by verify_bind_c_derived_type. */
7946 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7947 sym->common_block);
7950 if (t == FAILURE)
7952 /* clear the is_bind_c flag to prevent reporting errors more than
7953 once if something failed. */
7954 sym->attr.is_bind_c = 0;
7955 return;
7959 /* If a derived type symbol has reached this point, without its
7960 type being declared, we have an error. Notice that most
7961 conditions that produce undefined derived types have already
7962 been dealt with. However, the likes of:
7963 implicit type(t) (t) ..... call foo (t) will get us here if
7964 the type is not declared in the scope of the implicit
7965 statement. Change the type to BT_UNKNOWN, both because it is so
7966 and to prevent an ICE. */
7967 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
7968 && !sym->ts.derived->attr.zero_comp)
7970 gfc_error ("The derived type '%s' at %L is of type '%s', "
7971 "which has not been defined", sym->name,
7972 &sym->declared_at, sym->ts.derived->name);
7973 sym->ts.type = BT_UNKNOWN;
7974 return;
7977 /* Unless the derived-type declaration is use associated, Fortran 95
7978 does not allow public entries of private derived types.
7979 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
7980 161 in 95-006r3. */
7981 if (sym->ts.type == BT_DERIVED
7982 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
7983 && !sym->ts.derived->attr.use_assoc
7984 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7985 && !gfc_check_access (sym->ts.derived->attr.access,
7986 sym->ts.derived->ns->default_access)
7987 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
7988 "of PRIVATE derived type '%s'",
7989 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
7990 : "variable", sym->name, &sym->declared_at,
7991 sym->ts.derived->name) == FAILURE)
7992 return;
7994 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
7995 default initialization is defined (5.1.2.4.4). */
7996 if (sym->ts.type == BT_DERIVED
7997 && sym->attr.dummy
7998 && sym->attr.intent == INTENT_OUT
7999 && sym->as
8000 && sym->as->type == AS_ASSUMED_SIZE)
8002 for (c = sym->ts.derived->components; c; c = c->next)
8004 if (c->initializer)
8006 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
8007 "ASSUMED SIZE and so cannot have a default initializer",
8008 sym->name, &sym->declared_at);
8009 return;
8014 switch (sym->attr.flavor)
8016 case FL_VARIABLE:
8017 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
8018 return;
8019 break;
8021 case FL_PROCEDURE:
8022 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
8023 return;
8024 break;
8026 case FL_NAMELIST:
8027 if (resolve_fl_namelist (sym) == FAILURE)
8028 return;
8029 break;
8031 case FL_PARAMETER:
8032 if (resolve_fl_parameter (sym) == FAILURE)
8033 return;
8034 break;
8036 default:
8037 break;
8040 /* Resolve array specifier. Check as well some constraints
8041 on COMMON blocks. */
8043 check_constant = sym->attr.in_common && !sym->attr.pointer;
8045 /* Set the formal_arg_flag so that check_conflict will not throw
8046 an error for host associated variables in the specification
8047 expression for an array_valued function. */
8048 if (sym->attr.function && sym->as)
8049 formal_arg_flag = 1;
8051 gfc_resolve_array_spec (sym->as, check_constant);
8053 formal_arg_flag = 0;
8055 /* Resolve formal namespaces. */
8056 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
8057 gfc_resolve (sym->formal_ns);
8059 /* Check threadprivate restrictions. */
8060 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
8061 && (!sym->attr.in_common
8062 && sym->module == NULL
8063 && (sym->ns->proc_name == NULL
8064 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
8065 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
8067 /* If we have come this far we can apply default-initializers, as
8068 described in 14.7.5, to those variables that have not already
8069 been assigned one. */
8070 if (sym->ts.type == BT_DERIVED
8071 && sym->attr.referenced
8072 && sym->ns == gfc_current_ns
8073 && !sym->value
8074 && !sym->attr.allocatable
8075 && !sym->attr.alloc_comp)
8077 symbol_attribute *a = &sym->attr;
8079 if ((!a->save && !a->dummy && !a->pointer
8080 && !a->in_common && !a->use_assoc
8081 && !(a->function && sym != sym->result))
8082 || (a->dummy && a->intent == INTENT_OUT))
8083 apply_default_init (sym);
8088 /************* Resolve DATA statements *************/
8090 static struct
8092 gfc_data_value *vnode;
8093 mpz_t left;
8095 values;
8098 /* Advance the values structure to point to the next value in the data list. */
8100 static try
8101 next_data_value (void)
8104 while (mpz_cmp_ui (values.left, 0) == 0)
8106 if (values.vnode->next == NULL)
8107 return FAILURE;
8109 values.vnode = values.vnode->next;
8110 mpz_set (values.left, values.vnode->repeat);
8113 return SUCCESS;
8117 static try
8118 check_data_variable (gfc_data_variable *var, locus *where)
8120 gfc_expr *e;
8121 mpz_t size;
8122 mpz_t offset;
8123 try t;
8124 ar_type mark = AR_UNKNOWN;
8125 int i;
8126 mpz_t section_index[GFC_MAX_DIMENSIONS];
8127 gfc_ref *ref;
8128 gfc_array_ref *ar;
8130 if (gfc_resolve_expr (var->expr) == FAILURE)
8131 return FAILURE;
8133 ar = NULL;
8134 mpz_init_set_si (offset, 0);
8135 e = var->expr;
8137 if (e->expr_type != EXPR_VARIABLE)
8138 gfc_internal_error ("check_data_variable(): Bad expression");
8140 if (e->symtree->n.sym->ns->is_block_data
8141 && !e->symtree->n.sym->attr.in_common)
8143 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
8144 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
8147 if (e->ref == NULL && e->symtree->n.sym->as)
8149 gfc_error ("DATA array '%s' at %L must be specified in a previous"
8150 " declaration", e->symtree->n.sym->name, where);
8151 return FAILURE;
8154 if (e->rank == 0)
8156 mpz_init_set_ui (size, 1);
8157 ref = NULL;
8159 else
8161 ref = e->ref;
8163 /* Find the array section reference. */
8164 for (ref = e->ref; ref; ref = ref->next)
8166 if (ref->type != REF_ARRAY)
8167 continue;
8168 if (ref->u.ar.type == AR_ELEMENT)
8169 continue;
8170 break;
8172 gcc_assert (ref);
8174 /* Set marks according to the reference pattern. */
8175 switch (ref->u.ar.type)
8177 case AR_FULL:
8178 mark = AR_FULL;
8179 break;
8181 case AR_SECTION:
8182 ar = &ref->u.ar;
8183 /* Get the start position of array section. */
8184 gfc_get_section_index (ar, section_index, &offset);
8185 mark = AR_SECTION;
8186 break;
8188 default:
8189 gcc_unreachable ();
8192 if (gfc_array_size (e, &size) == FAILURE)
8194 gfc_error ("Nonconstant array section at %L in DATA statement",
8195 &e->where);
8196 mpz_clear (offset);
8197 return FAILURE;
8201 t = SUCCESS;
8203 while (mpz_cmp_ui (size, 0) > 0)
8205 if (next_data_value () == FAILURE)
8207 gfc_error ("DATA statement at %L has more variables than values",
8208 where);
8209 t = FAILURE;
8210 break;
8213 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
8214 if (t == FAILURE)
8215 break;
8217 /* If we have more than one element left in the repeat count,
8218 and we have more than one element left in the target variable,
8219 then create a range assignment. */
8220 /* FIXME: Only done for full arrays for now, since array sections
8221 seem tricky. */
8222 if (mark == AR_FULL && ref && ref->next == NULL
8223 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
8225 mpz_t range;
8227 if (mpz_cmp (size, values.left) >= 0)
8229 mpz_init_set (range, values.left);
8230 mpz_sub (size, size, values.left);
8231 mpz_set_ui (values.left, 0);
8233 else
8235 mpz_init_set (range, size);
8236 mpz_sub (values.left, values.left, size);
8237 mpz_set_ui (size, 0);
8240 gfc_assign_data_value_range (var->expr, values.vnode->expr,
8241 offset, range);
8243 mpz_add (offset, offset, range);
8244 mpz_clear (range);
8247 /* Assign initial value to symbol. */
8248 else
8250 mpz_sub_ui (values.left, values.left, 1);
8251 mpz_sub_ui (size, size, 1);
8253 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
8254 if (t == FAILURE)
8255 break;
8257 if (mark == AR_FULL)
8258 mpz_add_ui (offset, offset, 1);
8260 /* Modify the array section indexes and recalculate the offset
8261 for next element. */
8262 else if (mark == AR_SECTION)
8263 gfc_advance_section (section_index, ar, &offset);
8267 if (mark == AR_SECTION)
8269 for (i = 0; i < ar->dimen; i++)
8270 mpz_clear (section_index[i]);
8273 mpz_clear (size);
8274 mpz_clear (offset);
8276 return t;
8280 static try traverse_data_var (gfc_data_variable *, locus *);
8282 /* Iterate over a list of elements in a DATA statement. */
8284 static try
8285 traverse_data_list (gfc_data_variable *var, locus *where)
8287 mpz_t trip;
8288 iterator_stack frame;
8289 gfc_expr *e, *start, *end, *step;
8290 try retval = SUCCESS;
8292 mpz_init (frame.value);
8294 start = gfc_copy_expr (var->iter.start);
8295 end = gfc_copy_expr (var->iter.end);
8296 step = gfc_copy_expr (var->iter.step);
8298 if (gfc_simplify_expr (start, 1) == FAILURE
8299 || start->expr_type != EXPR_CONSTANT)
8301 gfc_error ("iterator start at %L does not simplify", &start->where);
8302 retval = FAILURE;
8303 goto cleanup;
8305 if (gfc_simplify_expr (end, 1) == FAILURE
8306 || end->expr_type != EXPR_CONSTANT)
8308 gfc_error ("iterator end at %L does not simplify", &end->where);
8309 retval = FAILURE;
8310 goto cleanup;
8312 if (gfc_simplify_expr (step, 1) == FAILURE
8313 || step->expr_type != EXPR_CONSTANT)
8315 gfc_error ("iterator step at %L does not simplify", &step->where);
8316 retval = FAILURE;
8317 goto cleanup;
8320 mpz_init_set (trip, end->value.integer);
8321 mpz_sub (trip, trip, start->value.integer);
8322 mpz_add (trip, trip, step->value.integer);
8324 mpz_div (trip, trip, step->value.integer);
8326 mpz_set (frame.value, start->value.integer);
8328 frame.prev = iter_stack;
8329 frame.variable = var->iter.var->symtree;
8330 iter_stack = &frame;
8332 while (mpz_cmp_ui (trip, 0) > 0)
8334 if (traverse_data_var (var->list, where) == FAILURE)
8336 mpz_clear (trip);
8337 retval = FAILURE;
8338 goto cleanup;
8341 e = gfc_copy_expr (var->expr);
8342 if (gfc_simplify_expr (e, 1) == FAILURE)
8344 gfc_free_expr (e);
8345 mpz_clear (trip);
8346 retval = FAILURE;
8347 goto cleanup;
8350 mpz_add (frame.value, frame.value, step->value.integer);
8352 mpz_sub_ui (trip, trip, 1);
8355 mpz_clear (trip);
8356 cleanup:
8357 mpz_clear (frame.value);
8359 gfc_free_expr (start);
8360 gfc_free_expr (end);
8361 gfc_free_expr (step);
8363 iter_stack = frame.prev;
8364 return retval;
8368 /* Type resolve variables in the variable list of a DATA statement. */
8370 static try
8371 traverse_data_var (gfc_data_variable *var, locus *where)
8373 try t;
8375 for (; var; var = var->next)
8377 if (var->expr == NULL)
8378 t = traverse_data_list (var, where);
8379 else
8380 t = check_data_variable (var, where);
8382 if (t == FAILURE)
8383 return FAILURE;
8386 return SUCCESS;
8390 /* Resolve the expressions and iterators associated with a data statement.
8391 This is separate from the assignment checking because data lists should
8392 only be resolved once. */
8394 static try
8395 resolve_data_variables (gfc_data_variable *d)
8397 for (; d; d = d->next)
8399 if (d->list == NULL)
8401 if (gfc_resolve_expr (d->expr) == FAILURE)
8402 return FAILURE;
8404 else
8406 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
8407 return FAILURE;
8409 if (resolve_data_variables (d->list) == FAILURE)
8410 return FAILURE;
8414 return SUCCESS;
8418 /* Resolve a single DATA statement. We implement this by storing a pointer to
8419 the value list into static variables, and then recursively traversing the
8420 variables list, expanding iterators and such. */
8422 static void
8423 resolve_data (gfc_data *d)
8426 if (resolve_data_variables (d->var) == FAILURE)
8427 return;
8429 values.vnode = d->value;
8430 if (d->value == NULL)
8431 mpz_set_ui (values.left, 0);
8432 else
8433 mpz_set (values.left, d->value->repeat);
8435 if (traverse_data_var (d->var, &d->where) == FAILURE)
8436 return;
8438 /* At this point, we better not have any values left. */
8440 if (next_data_value () == SUCCESS)
8441 gfc_error ("DATA statement at %L has more values than variables",
8442 &d->where);
8446 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
8447 accessed by host or use association, is a dummy argument to a pure function,
8448 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
8449 is storage associated with any such variable, shall not be used in the
8450 following contexts: (clients of this function). */
8452 /* Determines if a variable is not 'pure', ie not assignable within a pure
8453 procedure. Returns zero if assignment is OK, nonzero if there is a
8454 problem. */
8456 gfc_impure_variable (gfc_symbol *sym)
8458 gfc_symbol *proc;
8460 if (sym->attr.use_assoc || sym->attr.in_common)
8461 return 1;
8463 if (sym->ns != gfc_current_ns)
8464 return !sym->attr.function;
8466 proc = sym->ns->proc_name;
8467 if (sym->attr.dummy && gfc_pure (proc)
8468 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
8470 proc->attr.function))
8471 return 1;
8473 /* TODO: Sort out what can be storage associated, if anything, and include
8474 it here. In principle equivalences should be scanned but it does not
8475 seem to be possible to storage associate an impure variable this way. */
8476 return 0;
8480 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
8481 symbol of the current procedure. */
8484 gfc_pure (gfc_symbol *sym)
8486 symbol_attribute attr;
8488 if (sym == NULL)
8489 sym = gfc_current_ns->proc_name;
8490 if (sym == NULL)
8491 return 0;
8493 attr = sym->attr;
8495 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
8499 /* Test whether the current procedure is elemental or not. */
8502 gfc_elemental (gfc_symbol *sym)
8504 symbol_attribute attr;
8506 if (sym == NULL)
8507 sym = gfc_current_ns->proc_name;
8508 if (sym == NULL)
8509 return 0;
8510 attr = sym->attr;
8512 return attr.flavor == FL_PROCEDURE && attr.elemental;
8516 /* Warn about unused labels. */
8518 static void
8519 warn_unused_fortran_label (gfc_st_label *label)
8521 if (label == NULL)
8522 return;
8524 warn_unused_fortran_label (label->left);
8526 if (label->defined == ST_LABEL_UNKNOWN)
8527 return;
8529 switch (label->referenced)
8531 case ST_LABEL_UNKNOWN:
8532 gfc_warning ("Label %d at %L defined but not used", label->value,
8533 &label->where);
8534 break;
8536 case ST_LABEL_BAD_TARGET:
8537 gfc_warning ("Label %d at %L defined but cannot be used",
8538 label->value, &label->where);
8539 break;
8541 default:
8542 break;
8545 warn_unused_fortran_label (label->right);
8549 /* Returns the sequence type of a symbol or sequence. */
8551 static seq_type
8552 sequence_type (gfc_typespec ts)
8554 seq_type result;
8555 gfc_component *c;
8557 switch (ts.type)
8559 case BT_DERIVED:
8561 if (ts.derived->components == NULL)
8562 return SEQ_NONDEFAULT;
8564 result = sequence_type (ts.derived->components->ts);
8565 for (c = ts.derived->components->next; c; c = c->next)
8566 if (sequence_type (c->ts) != result)
8567 return SEQ_MIXED;
8569 return result;
8571 case BT_CHARACTER:
8572 if (ts.kind != gfc_default_character_kind)
8573 return SEQ_NONDEFAULT;
8575 return SEQ_CHARACTER;
8577 case BT_INTEGER:
8578 if (ts.kind != gfc_default_integer_kind)
8579 return SEQ_NONDEFAULT;
8581 return SEQ_NUMERIC;
8583 case BT_REAL:
8584 if (!(ts.kind == gfc_default_real_kind
8585 || ts.kind == gfc_default_double_kind))
8586 return SEQ_NONDEFAULT;
8588 return SEQ_NUMERIC;
8590 case BT_COMPLEX:
8591 if (ts.kind != gfc_default_complex_kind)
8592 return SEQ_NONDEFAULT;
8594 return SEQ_NUMERIC;
8596 case BT_LOGICAL:
8597 if (ts.kind != gfc_default_logical_kind)
8598 return SEQ_NONDEFAULT;
8600 return SEQ_NUMERIC;
8602 default:
8603 return SEQ_NONDEFAULT;
8608 /* Resolve derived type EQUIVALENCE object. */
8610 static try
8611 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
8613 gfc_symbol *d;
8614 gfc_component *c = derived->components;
8616 if (!derived)
8617 return SUCCESS;
8619 /* Shall not be an object of nonsequence derived type. */
8620 if (!derived->attr.sequence)
8622 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
8623 "attribute to be an EQUIVALENCE object", sym->name,
8624 &e->where);
8625 return FAILURE;
8628 /* Shall not have allocatable components. */
8629 if (derived->attr.alloc_comp)
8631 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
8632 "components to be an EQUIVALENCE object",sym->name,
8633 &e->where);
8634 return FAILURE;
8637 if (sym->attr.in_common && has_default_initializer (sym->ts.derived))
8639 gfc_error ("Derived type variable '%s' at %L with default "
8640 "initialization cannot be in EQUIVALENCE with a variable "
8641 "in COMMON", sym->name, &e->where);
8642 return FAILURE;
8645 for (; c ; c = c->next)
8647 d = c->ts.derived;
8648 if (d
8649 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
8650 return FAILURE;
8652 /* Shall not be an object of sequence derived type containing a pointer
8653 in the structure. */
8654 if (c->pointer)
8656 gfc_error ("Derived type variable '%s' at %L with pointer "
8657 "component(s) cannot be an EQUIVALENCE object",
8658 sym->name, &e->where);
8659 return FAILURE;
8662 return SUCCESS;
8666 /* Resolve equivalence object.
8667 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
8668 an allocatable array, an object of nonsequence derived type, an object of
8669 sequence derived type containing a pointer at any level of component
8670 selection, an automatic object, a function name, an entry name, a result
8671 name, a named constant, a structure component, or a subobject of any of
8672 the preceding objects. A substring shall not have length zero. A
8673 derived type shall not have components with default initialization nor
8674 shall two objects of an equivalence group be initialized.
8675 Either all or none of the objects shall have an protected attribute.
8676 The simple constraints are done in symbol.c(check_conflict) and the rest
8677 are implemented here. */
8679 static void
8680 resolve_equivalence (gfc_equiv *eq)
8682 gfc_symbol *sym;
8683 gfc_symbol *derived;
8684 gfc_symbol *first_sym;
8685 gfc_expr *e;
8686 gfc_ref *r;
8687 locus *last_where = NULL;
8688 seq_type eq_type, last_eq_type;
8689 gfc_typespec *last_ts;
8690 int object, cnt_protected;
8691 const char *value_name;
8692 const char *msg;
8694 value_name = NULL;
8695 last_ts = &eq->expr->symtree->n.sym->ts;
8697 first_sym = eq->expr->symtree->n.sym;
8699 cnt_protected = 0;
8701 for (object = 1; eq; eq = eq->eq, object++)
8703 e = eq->expr;
8705 e->ts = e->symtree->n.sym->ts;
8706 /* match_varspec might not know yet if it is seeing
8707 array reference or substring reference, as it doesn't
8708 know the types. */
8709 if (e->ref && e->ref->type == REF_ARRAY)
8711 gfc_ref *ref = e->ref;
8712 sym = e->symtree->n.sym;
8714 if (sym->attr.dimension)
8716 ref->u.ar.as = sym->as;
8717 ref = ref->next;
8720 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
8721 if (e->ts.type == BT_CHARACTER
8722 && ref
8723 && ref->type == REF_ARRAY
8724 && ref->u.ar.dimen == 1
8725 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
8726 && ref->u.ar.stride[0] == NULL)
8728 gfc_expr *start = ref->u.ar.start[0];
8729 gfc_expr *end = ref->u.ar.end[0];
8730 void *mem = NULL;
8732 /* Optimize away the (:) reference. */
8733 if (start == NULL && end == NULL)
8735 if (e->ref == ref)
8736 e->ref = ref->next;
8737 else
8738 e->ref->next = ref->next;
8739 mem = ref;
8741 else
8743 ref->type = REF_SUBSTRING;
8744 if (start == NULL)
8745 start = gfc_int_expr (1);
8746 ref->u.ss.start = start;
8747 if (end == NULL && e->ts.cl)
8748 end = gfc_copy_expr (e->ts.cl->length);
8749 ref->u.ss.end = end;
8750 ref->u.ss.length = e->ts.cl;
8751 e->ts.cl = NULL;
8753 ref = ref->next;
8754 gfc_free (mem);
8757 /* Any further ref is an error. */
8758 if (ref)
8760 gcc_assert (ref->type == REF_ARRAY);
8761 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
8762 &ref->u.ar.where);
8763 continue;
8767 if (gfc_resolve_expr (e) == FAILURE)
8768 continue;
8770 sym = e->symtree->n.sym;
8772 if (sym->attr.protected)
8773 cnt_protected++;
8774 if (cnt_protected > 0 && cnt_protected != object)
8776 gfc_error ("Either all or none of the objects in the "
8777 "EQUIVALENCE set at %L shall have the "
8778 "PROTECTED attribute",
8779 &e->where);
8780 break;
8783 /* Shall not equivalence common block variables in a PURE procedure. */
8784 if (sym->ns->proc_name
8785 && sym->ns->proc_name->attr.pure
8786 && sym->attr.in_common)
8788 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
8789 "object in the pure procedure '%s'",
8790 sym->name, &e->where, sym->ns->proc_name->name);
8791 break;
8794 /* Shall not be a named constant. */
8795 if (e->expr_type == EXPR_CONSTANT)
8797 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
8798 "object", sym->name, &e->where);
8799 continue;
8802 derived = e->ts.derived;
8803 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
8804 continue;
8806 /* Check that the types correspond correctly:
8807 Note 5.28:
8808 A numeric sequence structure may be equivalenced to another sequence
8809 structure, an object of default integer type, default real type, double
8810 precision real type, default logical type such that components of the
8811 structure ultimately only become associated to objects of the same
8812 kind. A character sequence structure may be equivalenced to an object
8813 of default character kind or another character sequence structure.
8814 Other objects may be equivalenced only to objects of the same type and
8815 kind parameters. */
8817 /* Identical types are unconditionally OK. */
8818 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
8819 goto identical_types;
8821 last_eq_type = sequence_type (*last_ts);
8822 eq_type = sequence_type (sym->ts);
8824 /* Since the pair of objects is not of the same type, mixed or
8825 non-default sequences can be rejected. */
8827 msg = "Sequence %s with mixed components in EQUIVALENCE "
8828 "statement at %L with different type objects";
8829 if ((object ==2
8830 && last_eq_type == SEQ_MIXED
8831 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
8832 == FAILURE)
8833 || (eq_type == SEQ_MIXED
8834 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8835 &e->where) == FAILURE))
8836 continue;
8838 msg = "Non-default type object or sequence %s in EQUIVALENCE "
8839 "statement at %L with objects of different type";
8840 if ((object ==2
8841 && last_eq_type == SEQ_NONDEFAULT
8842 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
8843 last_where) == FAILURE)
8844 || (eq_type == SEQ_NONDEFAULT
8845 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8846 &e->where) == FAILURE))
8847 continue;
8849 msg ="Non-CHARACTER object '%s' in default CHARACTER "
8850 "EQUIVALENCE statement at %L";
8851 if (last_eq_type == SEQ_CHARACTER
8852 && eq_type != SEQ_CHARACTER
8853 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8854 &e->where) == FAILURE)
8855 continue;
8857 msg ="Non-NUMERIC object '%s' in default NUMERIC "
8858 "EQUIVALENCE statement at %L";
8859 if (last_eq_type == SEQ_NUMERIC
8860 && eq_type != SEQ_NUMERIC
8861 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8862 &e->where) == FAILURE)
8863 continue;
8865 identical_types:
8866 last_ts =&sym->ts;
8867 last_where = &e->where;
8869 if (!e->ref)
8870 continue;
8872 /* Shall not be an automatic array. */
8873 if (e->ref->type == REF_ARRAY
8874 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
8876 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
8877 "an EQUIVALENCE object", sym->name, &e->where);
8878 continue;
8881 r = e->ref;
8882 while (r)
8884 /* Shall not be a structure component. */
8885 if (r->type == REF_COMPONENT)
8887 gfc_error ("Structure component '%s' at %L cannot be an "
8888 "EQUIVALENCE object",
8889 r->u.c.component->name, &e->where);
8890 break;
8893 /* A substring shall not have length zero. */
8894 if (r->type == REF_SUBSTRING)
8896 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
8898 gfc_error ("Substring at %L has length zero",
8899 &r->u.ss.start->where);
8900 break;
8903 r = r->next;
8909 /* Resolve function and ENTRY types, issue diagnostics if needed. */
8911 static void
8912 resolve_fntype (gfc_namespace *ns)
8914 gfc_entry_list *el;
8915 gfc_symbol *sym;
8917 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
8918 return;
8920 /* If there are any entries, ns->proc_name is the entry master
8921 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
8922 if (ns->entries)
8923 sym = ns->entries->sym;
8924 else
8925 sym = ns->proc_name;
8926 if (sym->result == sym
8927 && sym->ts.type == BT_UNKNOWN
8928 && gfc_set_default_type (sym, 0, NULL) == FAILURE
8929 && !sym->attr.untyped)
8931 gfc_error ("Function '%s' at %L has no IMPLICIT type",
8932 sym->name, &sym->declared_at);
8933 sym->attr.untyped = 1;
8936 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
8937 && !gfc_check_access (sym->ts.derived->attr.access,
8938 sym->ts.derived->ns->default_access)
8939 && gfc_check_access (sym->attr.access, sym->ns->default_access))
8941 gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
8942 sym->name, &sym->declared_at, sym->ts.derived->name);
8945 if (ns->entries)
8946 for (el = ns->entries->next; el; el = el->next)
8948 if (el->sym->result == el->sym
8949 && el->sym->ts.type == BT_UNKNOWN
8950 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
8951 && !el->sym->attr.untyped)
8953 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
8954 el->sym->name, &el->sym->declared_at);
8955 el->sym->attr.untyped = 1;
8960 /* 12.3.2.1.1 Defined operators. */
8962 static void
8963 gfc_resolve_uops (gfc_symtree *symtree)
8965 gfc_interface *itr;
8966 gfc_symbol *sym;
8967 gfc_formal_arglist *formal;
8969 if (symtree == NULL)
8970 return;
8972 gfc_resolve_uops (symtree->left);
8973 gfc_resolve_uops (symtree->right);
8975 for (itr = symtree->n.uop->operator; itr; itr = itr->next)
8977 sym = itr->sym;
8978 if (!sym->attr.function)
8979 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
8980 sym->name, &sym->declared_at);
8982 if (sym->ts.type == BT_CHARACTER
8983 && !(sym->ts.cl && sym->ts.cl->length)
8984 && !(sym->result && sym->result->ts.cl
8985 && sym->result->ts.cl->length))
8986 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
8987 "character length", sym->name, &sym->declared_at);
8989 formal = sym->formal;
8990 if (!formal || !formal->sym)
8992 gfc_error ("User operator procedure '%s' at %L must have at least "
8993 "one argument", sym->name, &sym->declared_at);
8994 continue;
8997 if (formal->sym->attr.intent != INTENT_IN)
8998 gfc_error ("First argument of operator interface at %L must be "
8999 "INTENT(IN)", &sym->declared_at);
9001 if (formal->sym->attr.optional)
9002 gfc_error ("First argument of operator interface at %L cannot be "
9003 "optional", &sym->declared_at);
9005 formal = formal->next;
9006 if (!formal || !formal->sym)
9007 continue;
9009 if (formal->sym->attr.intent != INTENT_IN)
9010 gfc_error ("Second argument of operator interface at %L must be "
9011 "INTENT(IN)", &sym->declared_at);
9013 if (formal->sym->attr.optional)
9014 gfc_error ("Second argument of operator interface at %L cannot be "
9015 "optional", &sym->declared_at);
9017 if (formal->next)
9018 gfc_error ("Operator interface at %L must have, at most, two "
9019 "arguments", &sym->declared_at);
9024 /* Examine all of the expressions associated with a program unit,
9025 assign types to all intermediate expressions, make sure that all
9026 assignments are to compatible types and figure out which names
9027 refer to which functions or subroutines. It doesn't check code
9028 block, which is handled by resolve_code. */
9030 static void
9031 resolve_types (gfc_namespace *ns)
9033 gfc_namespace *n;
9034 gfc_charlen *cl;
9035 gfc_data *d;
9036 gfc_equiv *eq;
9038 gfc_current_ns = ns;
9040 resolve_entries (ns);
9042 resolve_common_vars (ns->blank_common.head, false);
9043 resolve_common_blocks (ns->common_root);
9045 resolve_contained_functions (ns);
9047 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
9049 for (cl = ns->cl_list; cl; cl = cl->next)
9050 resolve_charlen (cl);
9052 gfc_traverse_ns (ns, resolve_symbol);
9054 resolve_fntype (ns);
9056 for (n = ns->contained; n; n = n->sibling)
9058 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
9059 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
9060 "also be PURE", n->proc_name->name,
9061 &n->proc_name->declared_at);
9063 resolve_types (n);
9066 forall_flag = 0;
9067 gfc_check_interfaces (ns);
9069 gfc_traverse_ns (ns, resolve_values);
9071 if (ns->save_all)
9072 gfc_save_all (ns);
9074 iter_stack = NULL;
9075 for (d = ns->data; d; d = d->next)
9076 resolve_data (d);
9078 iter_stack = NULL;
9079 gfc_traverse_ns (ns, gfc_formalize_init_value);
9081 gfc_traverse_ns (ns, gfc_verify_binding_labels);
9083 if (ns->common_root != NULL)
9084 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
9086 for (eq = ns->equiv; eq; eq = eq->next)
9087 resolve_equivalence (eq);
9089 /* Warn about unused labels. */
9090 if (warn_unused_label)
9091 warn_unused_fortran_label (ns->st_labels);
9093 gfc_resolve_uops (ns->uop_root);
9097 /* Call resolve_code recursively. */
9099 static void
9100 resolve_codes (gfc_namespace *ns)
9102 gfc_namespace *n;
9104 for (n = ns->contained; n; n = n->sibling)
9105 resolve_codes (n);
9107 gfc_current_ns = ns;
9108 cs_base = NULL;
9109 /* Set to an out of range value. */
9110 current_entry_id = -1;
9112 bitmap_obstack_initialize (&labels_obstack);
9113 resolve_code (ns->code, ns);
9114 bitmap_obstack_release (&labels_obstack);
9118 /* This function is called after a complete program unit has been compiled.
9119 Its purpose is to examine all of the expressions associated with a program
9120 unit, assign types to all intermediate expressions, make sure that all
9121 assignments are to compatible types and figure out which names refer to
9122 which functions or subroutines. */
9124 void
9125 gfc_resolve (gfc_namespace *ns)
9127 gfc_namespace *old_ns;
9129 old_ns = gfc_current_ns;
9131 resolve_types (ns);
9132 resolve_codes (ns);
9134 gfc_current_ns = old_ns;