]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/resolve.c
05b4dc145c3090b5f460ade0305fabfc3d8dd4b3
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
3 Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
21 02110-1301, USA. */
22
23
24 #include "config.h"
25 #include "system.h"
26 #include "flags.h"
27 #include "gfortran.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30
31 /* Types used in equivalence statements. */
32
33 typedef enum seq_type
34 {
35 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
36 }
37 seq_type;
38
39 /* Stack to push the current if we descend into a block during
40 resolution. See resolve_branch() and resolve_code(). */
41
42 typedef struct code_stack
43 {
44 struct gfc_code *head, *current;
45 struct code_stack *prev;
46 }
47 code_stack;
48
49 static code_stack *cs_base = NULL;
50
51
52 /* Nonzero if we're inside a FORALL block. */
53
54 static int forall_flag;
55
56 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
57
58 static int omp_workshare_flag;
59
60 /* Nonzero if we are processing a formal arglist. The corresponding function
61 resets the flag each time that it is read. */
62 static int formal_arg_flag = 0;
63
64 /* True if we are resolving a specification expression. */
65 static int specification_expr = 0;
66
67 /* The id of the last entry seen. */
68 static int current_entry_id;
69
70 int
71 gfc_is_formal_arg (void)
72 {
73 return formal_arg_flag;
74 }
75
76 /* Resolve types of formal argument lists. These have to be done early so that
77 the formal argument lists of module procedures can be copied to the
78 containing module before the individual procedures are resolved
79 individually. We also resolve argument lists of procedures in interface
80 blocks because they are self-contained scoping units.
81
82 Since a dummy argument cannot be a non-dummy procedure, the only
83 resort left for untyped names are the IMPLICIT types. */
84
85 static void
86 resolve_formal_arglist (gfc_symbol * proc)
87 {
88 gfc_formal_arglist *f;
89 gfc_symbol *sym;
90 int i;
91
92 if (proc->result != NULL)
93 sym = proc->result;
94 else
95 sym = proc;
96
97 if (gfc_elemental (proc)
98 || sym->attr.pointer || sym->attr.allocatable
99 || (sym->as && sym->as->rank > 0))
100 proc->attr.always_explicit = 1;
101
102 formal_arg_flag = 1;
103
104 for (f = proc->formal; f; f = f->next)
105 {
106 sym = f->sym;
107
108 if (sym == NULL)
109 {
110 /* Alternate return placeholder. */
111 if (gfc_elemental (proc))
112 gfc_error ("Alternate return specifier in elemental subroutine "
113 "'%s' at %L is not allowed", proc->name,
114 &proc->declared_at);
115 if (proc->attr.function)
116 gfc_error ("Alternate return specifier in function "
117 "'%s' at %L is not allowed", proc->name,
118 &proc->declared_at);
119 continue;
120 }
121
122 if (sym->attr.if_source != IFSRC_UNKNOWN)
123 resolve_formal_arglist (sym);
124
125 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
126 {
127 if (gfc_pure (proc) && !gfc_pure (sym))
128 {
129 gfc_error
130 ("Dummy procedure '%s' of PURE procedure at %L must also "
131 "be PURE", sym->name, &sym->declared_at);
132 continue;
133 }
134
135 if (gfc_elemental (proc))
136 {
137 gfc_error
138 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
139 &sym->declared_at);
140 continue;
141 }
142
143 if (sym->attr.function
144 && sym->ts.type == BT_UNKNOWN
145 && sym->attr.intrinsic)
146 {
147 gfc_intrinsic_sym *isym;
148 isym = gfc_find_function (sym->name);
149 if (isym == NULL || !isym->specific)
150 {
151 gfc_error ("Unable to find a specific INTRINSIC procedure "
152 "for the reference '%s' at %L", sym->name,
153 &sym->declared_at);
154 }
155 sym->ts = isym->ts;
156 }
157
158 continue;
159 }
160
161 if (sym->ts.type == BT_UNKNOWN)
162 {
163 if (!sym->attr.function || sym->result == sym)
164 gfc_set_default_type (sym, 1, sym->ns);
165 }
166
167 gfc_resolve_array_spec (sym->as, 0);
168
169 /* We can't tell if an array with dimension (:) is assumed or deferred
170 shape until we know if it has the pointer or allocatable attributes.
171 */
172 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
173 && !(sym->attr.pointer || sym->attr.allocatable))
174 {
175 sym->as->type = AS_ASSUMED_SHAPE;
176 for (i = 0; i < sym->as->rank; i++)
177 sym->as->lower[i] = gfc_int_expr (1);
178 }
179
180 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
181 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
182 || sym->attr.optional)
183 proc->attr.always_explicit = 1;
184
185 /* If the flavor is unknown at this point, it has to be a variable.
186 A procedure specification would have already set the type. */
187
188 if (sym->attr.flavor == FL_UNKNOWN)
189 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
190
191 if (gfc_pure (proc) && !sym->attr.pointer
192 && sym->attr.flavor != FL_PROCEDURE)
193 {
194 if (proc->attr.function && sym->attr.intent != INTENT_IN)
195 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
196 "INTENT(IN)", sym->name, proc->name,
197 &sym->declared_at);
198
199 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
200 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
201 "have its INTENT specified", sym->name, proc->name,
202 &sym->declared_at);
203 }
204
205 if (gfc_elemental (proc))
206 {
207 if (sym->as != NULL)
208 {
209 gfc_error
210 ("Argument '%s' of elemental procedure at %L must be scalar",
211 sym->name, &sym->declared_at);
212 continue;
213 }
214
215 if (sym->attr.pointer)
216 {
217 gfc_error
218 ("Argument '%s' of elemental procedure at %L cannot have "
219 "the POINTER attribute", sym->name, &sym->declared_at);
220 continue;
221 }
222 }
223
224 /* Each dummy shall be specified to be scalar. */
225 if (proc->attr.proc == PROC_ST_FUNCTION)
226 {
227 if (sym->as != NULL)
228 {
229 gfc_error
230 ("Argument '%s' of statement function at %L must be scalar",
231 sym->name, &sym->declared_at);
232 continue;
233 }
234
235 if (sym->ts.type == BT_CHARACTER)
236 {
237 gfc_charlen *cl = sym->ts.cl;
238 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
239 {
240 gfc_error
241 ("Character-valued argument '%s' of statement function at "
242 "%L must have constant length",
243 sym->name, &sym->declared_at);
244 continue;
245 }
246 }
247 }
248 }
249 formal_arg_flag = 0;
250 }
251
252
253 /* Work function called when searching for symbols that have argument lists
254 associated with them. */
255
256 static void
257 find_arglists (gfc_symbol * sym)
258 {
259
260 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
261 return;
262
263 resolve_formal_arglist (sym);
264 }
265
266
267 /* Given a namespace, resolve all formal argument lists within the namespace.
268 */
269
270 static void
271 resolve_formal_arglists (gfc_namespace * ns)
272 {
273
274 if (ns == NULL)
275 return;
276
277 gfc_traverse_ns (ns, find_arglists);
278 }
279
280
281 static void
282 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
283 {
284 try t;
285
286 /* If this namespace is not a function, ignore it. */
287 if (! sym
288 || !(sym->attr.function
289 || sym->attr.flavor == FL_VARIABLE))
290 return;
291
292 /* Try to find out of what the return type is. */
293 if (sym->result != NULL)
294 sym = sym->result;
295
296 if (sym->ts.type == BT_UNKNOWN)
297 {
298 t = gfc_set_default_type (sym, 0, ns);
299
300 if (t == FAILURE && !sym->attr.untyped)
301 {
302 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
303 sym->name, &sym->declared_at); /* FIXME */
304 sym->attr.untyped = 1;
305 }
306 }
307
308 /*Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character type,
309 lists the only ways a character length value of * can be used: dummy arguments
310 of procedures, named constants, and function results in external functions.
311 Internal function results are not on that list; ergo, not permitted. */
312
313 if (sym->ts.type == BT_CHARACTER)
314 {
315 gfc_charlen *cl = sym->ts.cl;
316 if (!cl || !cl->length)
317 gfc_error ("Character-valued internal function '%s' at %L must "
318 "not be assumed length", sym->name, &sym->declared_at);
319 }
320 }
321
322
323 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
324 introduce duplicates. */
325
326 static void
327 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
328 {
329 gfc_formal_arglist *f, *new_arglist;
330 gfc_symbol *new_sym;
331
332 for (; new_args != NULL; new_args = new_args->next)
333 {
334 new_sym = new_args->sym;
335 /* See if this arg is already in the formal argument list. */
336 for (f = proc->formal; f; f = f->next)
337 {
338 if (new_sym == f->sym)
339 break;
340 }
341
342 if (f)
343 continue;
344
345 /* Add a new argument. Argument order is not important. */
346 new_arglist = gfc_get_formal_arglist ();
347 new_arglist->sym = new_sym;
348 new_arglist->next = proc->formal;
349 proc->formal = new_arglist;
350 }
351 }
352
353
354 /* Flag the arguments that are not present in all entries. */
355
356 static void
357 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
358 {
359 gfc_formal_arglist *f, *head;
360 head = new_args;
361
362 for (f = proc->formal; f; f = f->next)
363 {
364 if (f->sym == NULL)
365 continue;
366
367 for (new_args = head; new_args; new_args = new_args->next)
368 {
369 if (new_args->sym == f->sym)
370 break;
371 }
372
373 if (new_args)
374 continue;
375
376 f->sym->attr.not_always_present = 1;
377 }
378 }
379
380
381 /* Resolve alternate entry points. If a symbol has multiple entry points we
382 create a new master symbol for the main routine, and turn the existing
383 symbol into an entry point. */
384
385 static void
386 resolve_entries (gfc_namespace * ns)
387 {
388 gfc_namespace *old_ns;
389 gfc_code *c;
390 gfc_symbol *proc;
391 gfc_entry_list *el;
392 char name[GFC_MAX_SYMBOL_LEN + 1];
393 static int master_count = 0;
394
395 if (ns->proc_name == NULL)
396 return;
397
398 /* No need to do anything if this procedure doesn't have alternate entry
399 points. */
400 if (!ns->entries)
401 return;
402
403 /* We may already have resolved alternate entry points. */
404 if (ns->proc_name->attr.entry_master)
405 return;
406
407 /* If this isn't a procedure something has gone horribly wrong. */
408 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
409
410 /* Remember the current namespace. */
411 old_ns = gfc_current_ns;
412
413 gfc_current_ns = ns;
414
415 /* Add the main entry point to the list of entry points. */
416 el = gfc_get_entry_list ();
417 el->sym = ns->proc_name;
418 el->id = 0;
419 el->next = ns->entries;
420 ns->entries = el;
421 ns->proc_name->attr.entry = 1;
422
423 /* If it is a module function, it needs to be in the right namespace
424 so that gfc_get_fake_result_decl can gather up the results. The
425 need for this arose in get_proc_name, where these beasts were
426 left in their own namespace, to keep prior references linked to
427 the entry declaration.*/
428 if (ns->proc_name->attr.function
429 && ns->parent
430 && ns->parent->proc_name->attr.flavor == FL_MODULE)
431 el->sym->ns = ns;
432
433 /* Add an entry statement for it. */
434 c = gfc_get_code ();
435 c->op = EXEC_ENTRY;
436 c->ext.entry = el;
437 c->next = ns->code;
438 ns->code = c;
439
440 /* Create a new symbol for the master function. */
441 /* Give the internal function a unique name (within this file).
442 Also include the function name so the user has some hope of figuring
443 out what is going on. */
444 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
445 master_count++, ns->proc_name->name);
446 gfc_get_ha_symbol (name, &proc);
447 gcc_assert (proc != NULL);
448
449 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
450 if (ns->proc_name->attr.subroutine)
451 gfc_add_subroutine (&proc->attr, proc->name, NULL);
452 else
453 {
454 gfc_symbol *sym;
455 gfc_typespec *ts, *fts;
456 gfc_array_spec *as, *fas;
457 gfc_add_function (&proc->attr, proc->name, NULL);
458 proc->result = proc;
459 fas = ns->entries->sym->as;
460 fas = fas ? fas : ns->entries->sym->result->as;
461 fts = &ns->entries->sym->result->ts;
462 if (fts->type == BT_UNKNOWN)
463 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
464 for (el = ns->entries->next; el; el = el->next)
465 {
466 ts = &el->sym->result->ts;
467 as = el->sym->as;
468 as = as ? as : el->sym->result->as;
469 if (ts->type == BT_UNKNOWN)
470 ts = gfc_get_default_type (el->sym->result, NULL);
471
472 if (! gfc_compare_types (ts, fts)
473 || (el->sym->result->attr.dimension
474 != ns->entries->sym->result->attr.dimension)
475 || (el->sym->result->attr.pointer
476 != ns->entries->sym->result->attr.pointer))
477 break;
478
479 else if (as && fas && gfc_compare_array_spec (as, fas) == 0)
480 gfc_error ("Procedure %s at %L has entries with mismatched "
481 "array specifications", ns->entries->sym->name,
482 &ns->entries->sym->declared_at);
483 }
484
485 if (el == NULL)
486 {
487 sym = ns->entries->sym->result;
488 /* All result types the same. */
489 proc->ts = *fts;
490 if (sym->attr.dimension)
491 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
492 if (sym->attr.pointer)
493 gfc_add_pointer (&proc->attr, NULL);
494 }
495 else
496 {
497 /* Otherwise the result will be passed through a union by
498 reference. */
499 proc->attr.mixed_entry_master = 1;
500 for (el = ns->entries; el; el = el->next)
501 {
502 sym = el->sym->result;
503 if (sym->attr.dimension)
504 {
505 if (el == ns->entries)
506 gfc_error
507 ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
508 sym->name, ns->entries->sym->name, &sym->declared_at);
509 else
510 gfc_error
511 ("ENTRY result %s can't be an array in FUNCTION %s at %L",
512 sym->name, ns->entries->sym->name, &sym->declared_at);
513 }
514 else if (sym->attr.pointer)
515 {
516 if (el == ns->entries)
517 gfc_error
518 ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
519 sym->name, ns->entries->sym->name, &sym->declared_at);
520 else
521 gfc_error
522 ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
523 sym->name, ns->entries->sym->name, &sym->declared_at);
524 }
525 else
526 {
527 ts = &sym->ts;
528 if (ts->type == BT_UNKNOWN)
529 ts = gfc_get_default_type (sym, NULL);
530 switch (ts->type)
531 {
532 case BT_INTEGER:
533 if (ts->kind == gfc_default_integer_kind)
534 sym = NULL;
535 break;
536 case BT_REAL:
537 if (ts->kind == gfc_default_real_kind
538 || ts->kind == gfc_default_double_kind)
539 sym = NULL;
540 break;
541 case BT_COMPLEX:
542 if (ts->kind == gfc_default_complex_kind)
543 sym = NULL;
544 break;
545 case BT_LOGICAL:
546 if (ts->kind == gfc_default_logical_kind)
547 sym = NULL;
548 break;
549 case BT_UNKNOWN:
550 /* We will issue error elsewhere. */
551 sym = NULL;
552 break;
553 default:
554 break;
555 }
556 if (sym)
557 {
558 if (el == ns->entries)
559 gfc_error
560 ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
561 sym->name, gfc_typename (ts), ns->entries->sym->name,
562 &sym->declared_at);
563 else
564 gfc_error
565 ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
566 sym->name, gfc_typename (ts), ns->entries->sym->name,
567 &sym->declared_at);
568 }
569 }
570 }
571 }
572 }
573 proc->attr.access = ACCESS_PRIVATE;
574 proc->attr.entry_master = 1;
575
576 /* Merge all the entry point arguments. */
577 for (el = ns->entries; el; el = el->next)
578 merge_argument_lists (proc, el->sym->formal);
579
580 /* Check the master formal arguments for any that are not
581 present in all entry points. */
582 for (el = ns->entries; el; el = el->next)
583 check_argument_lists (proc, el->sym->formal);
584
585 /* Use the master function for the function body. */
586 ns->proc_name = proc;
587
588 /* Finalize the new symbols. */
589 gfc_commit_symbols ();
590
591 /* Restore the original namespace. */
592 gfc_current_ns = old_ns;
593 }
594
595
596 /* Resolve contained function types. Because contained functions can call one
597 another, they have to be worked out before any of the contained procedures
598 can be resolved.
599
600 The good news is that if a function doesn't already have a type, the only
601 way it can get one is through an IMPLICIT type or a RESULT variable, because
602 by definition contained functions are contained namespace they're contained
603 in, not in a sibling or parent namespace. */
604
605 static void
606 resolve_contained_functions (gfc_namespace * ns)
607 {
608 gfc_namespace *child;
609 gfc_entry_list *el;
610
611 resolve_formal_arglists (ns);
612
613 for (child = ns->contained; child; child = child->sibling)
614 {
615 /* Resolve alternate entry points first. */
616 resolve_entries (child);
617
618 /* Then check function return types. */
619 resolve_contained_fntype (child->proc_name, child);
620 for (el = child->entries; el; el = el->next)
621 resolve_contained_fntype (el->sym, child);
622 }
623 }
624
625
626 /* Resolve all of the elements of a structure constructor and make sure that
627 the types are correct. */
628
629 static try
630 resolve_structure_cons (gfc_expr * expr)
631 {
632 gfc_constructor *cons;
633 gfc_component *comp;
634 try t;
635 symbol_attribute a;
636
637 t = SUCCESS;
638 cons = expr->value.constructor;
639 /* A constructor may have references if it is the result of substituting a
640 parameter variable. In this case we just pull out the component we
641 want. */
642 if (expr->ref)
643 comp = expr->ref->u.c.sym->components;
644 else
645 comp = expr->ts.derived->components;
646
647 for (; comp; comp = comp->next, cons = cons->next)
648 {
649 if (! cons->expr)
650 continue;
651
652 if (gfc_resolve_expr (cons->expr) == FAILURE)
653 {
654 t = FAILURE;
655 continue;
656 }
657
658 if (cons->expr->expr_type != EXPR_NULL
659 && comp->as && comp->as->rank != cons->expr->rank
660 && (comp->allocatable || cons->expr->rank))
661 {
662 gfc_error ("The rank of the element in the derived type "
663 "constructor at %L does not match that of the "
664 "component (%d/%d)", &cons->expr->where,
665 cons->expr->rank, comp->as ? comp->as->rank : 0);
666 t = FAILURE;
667 }
668
669 /* If we don't have the right type, try to convert it. */
670
671 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
672 {
673 t = FAILURE;
674 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
675 gfc_error ("The element in the derived type constructor at %L, "
676 "for pointer component '%s', is %s but should be %s",
677 &cons->expr->where, comp->name,
678 gfc_basic_typename (cons->expr->ts.type),
679 gfc_basic_typename (comp->ts.type));
680 else
681 t = gfc_convert_type (cons->expr, &comp->ts, 1);
682 }
683
684 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
685 continue;
686
687 a = gfc_expr_attr (cons->expr);
688
689 if (!a.pointer && !a.target)
690 {
691 t = FAILURE;
692 gfc_error ("The element in the derived type constructor at %L, "
693 "for pointer component '%s' should be a POINTER or "
694 "a TARGET", &cons->expr->where, comp->name);
695 }
696 }
697
698 return t;
699 }
700
701
702
703 /****************** Expression name resolution ******************/
704
705 /* Returns 0 if a symbol was not declared with a type or
706 attribute declaration statement, nonzero otherwise. */
707
708 static int
709 was_declared (gfc_symbol * sym)
710 {
711 symbol_attribute a;
712
713 a = sym->attr;
714
715 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
716 return 1;
717
718 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
719 || a.optional || a.pointer || a.save || a.target || a.volatile_ || a.value
720 || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
721 return 1;
722
723 return 0;
724 }
725
726
727 /* Determine if a symbol is generic or not. */
728
729 static int
730 generic_sym (gfc_symbol * sym)
731 {
732 gfc_symbol *s;
733
734 if (sym->attr.generic ||
735 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
736 return 1;
737
738 if (was_declared (sym) || sym->ns->parent == NULL)
739 return 0;
740
741 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
742
743 return (s == NULL) ? 0 : generic_sym (s);
744 }
745
746
747 /* Determine if a symbol is specific or not. */
748
749 static int
750 specific_sym (gfc_symbol * sym)
751 {
752 gfc_symbol *s;
753
754 if (sym->attr.if_source == IFSRC_IFBODY
755 || sym->attr.proc == PROC_MODULE
756 || sym->attr.proc == PROC_INTERNAL
757 || sym->attr.proc == PROC_ST_FUNCTION
758 || (sym->attr.intrinsic &&
759 gfc_specific_intrinsic (sym->name))
760 || sym->attr.external)
761 return 1;
762
763 if (was_declared (sym) || sym->ns->parent == NULL)
764 return 0;
765
766 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
767
768 return (s == NULL) ? 0 : specific_sym (s);
769 }
770
771
772 /* Figure out if the procedure is specific, generic or unknown. */
773
774 typedef enum
775 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
776 proc_type;
777
778 static proc_type
779 procedure_kind (gfc_symbol * sym)
780 {
781
782 if (generic_sym (sym))
783 return PTYPE_GENERIC;
784
785 if (specific_sym (sym))
786 return PTYPE_SPECIFIC;
787
788 return PTYPE_UNKNOWN;
789 }
790
791 /* Check references to assumed size arrays. The flag need_full_assumed_size
792 is nonzero when matching actual arguments. */
793
794 static int need_full_assumed_size = 0;
795
796 static bool
797 check_assumed_size_reference (gfc_symbol * sym, gfc_expr * e)
798 {
799 gfc_ref * ref;
800 int dim;
801 int last = 1;
802
803 if (need_full_assumed_size
804 || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
805 return false;
806
807 for (ref = e->ref; ref; ref = ref->next)
808 if (ref->type == REF_ARRAY)
809 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
810 last = (ref->u.ar.end[dim] == NULL) && (ref->u.ar.type == DIMEN_ELEMENT);
811
812 if (last)
813 {
814 gfc_error ("The upper bound in the last dimension must "
815 "appear in the reference to the assumed size "
816 "array '%s' at %L", sym->name, &e->where);
817 return true;
818 }
819 return false;
820 }
821
822
823 /* Look for bad assumed size array references in argument expressions
824 of elemental and array valued intrinsic procedures. Since this is
825 called from procedure resolution functions, it only recurses at
826 operators. */
827
828 static bool
829 resolve_assumed_size_actual (gfc_expr *e)
830 {
831 if (e == NULL)
832 return false;
833
834 switch (e->expr_type)
835 {
836 case EXPR_VARIABLE:
837 if (e->symtree
838 && check_assumed_size_reference (e->symtree->n.sym, e))
839 return true;
840 break;
841
842 case EXPR_OP:
843 if (resolve_assumed_size_actual (e->value.op.op1)
844 || resolve_assumed_size_actual (e->value.op.op2))
845 return true;
846 break;
847
848 default:
849 break;
850 }
851 return false;
852 }
853
854
855 /* Resolve an actual argument list. Most of the time, this is just
856 resolving the expressions in the list.
857 The exception is that we sometimes have to decide whether arguments
858 that look like procedure arguments are really simple variable
859 references. */
860
861 static try
862 resolve_actual_arglist (gfc_actual_arglist * arg, procedure_type ptype)
863 {
864 gfc_symbol *sym;
865 gfc_symtree *parent_st;
866 gfc_expr *e;
867
868 for (; arg; arg = arg->next)
869 {
870 e = arg->expr;
871 if (e == NULL)
872 {
873 /* Check the label is a valid branching target. */
874 if (arg->label)
875 {
876 if (arg->label->defined == ST_LABEL_UNKNOWN)
877 {
878 gfc_error ("Label %d referenced at %L is never defined",
879 arg->label->value, &arg->label->where);
880 return FAILURE;
881 }
882 }
883 continue;
884 }
885
886 if (e->ts.type != BT_PROCEDURE)
887 {
888 if (gfc_resolve_expr (e) != SUCCESS)
889 return FAILURE;
890 goto argument_list;
891 }
892
893 /* See if the expression node should really be a variable
894 reference. */
895
896 sym = e->symtree->n.sym;
897
898 if (sym->attr.flavor == FL_PROCEDURE
899 || sym->attr.intrinsic
900 || sym->attr.external)
901 {
902 int actual_ok;
903
904 /* If a procedure is not already determined to be something else
905 check if it is intrinsic. */
906 if (!sym->attr.intrinsic
907 && !(sym->attr.external || sym->attr.use_assoc
908 || sym->attr.if_source == IFSRC_IFBODY)
909 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
910 sym->attr.intrinsic = 1;
911
912 if (sym->attr.proc == PROC_ST_FUNCTION)
913 {
914 gfc_error ("Statement function '%s' at %L is not allowed as an "
915 "actual argument", sym->name, &e->where);
916 }
917
918 actual_ok = gfc_intrinsic_actual_ok (sym->name, sym->attr.subroutine);
919 if (sym->attr.intrinsic && actual_ok == 0)
920 {
921 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
922 "actual argument", sym->name, &e->where);
923 }
924
925 if (sym->attr.contained && !sym->attr.use_assoc
926 && sym->ns->proc_name->attr.flavor != FL_MODULE)
927 {
928 gfc_error ("Internal procedure '%s' is not allowed as an "
929 "actual argument at %L", sym->name, &e->where);
930 }
931
932 if (sym->attr.elemental && !sym->attr.intrinsic)
933 {
934 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
935 "allowed as an actual argument at %L", sym->name,
936 &e->where);
937 }
938
939 if (sym->attr.generic)
940 {
941 gfc_error ("GENERIC non-INTRINSIC procedure '%s' is not "
942 "allowed as an actual argument at %L", sym->name,
943 &e->where);
944 }
945
946 /* If the symbol is the function that names the current (or
947 parent) scope, then we really have a variable reference. */
948
949 if (sym->attr.function && sym->result == sym
950 && (sym->ns->proc_name == sym
951 || (sym->ns->parent != NULL
952 && sym->ns->parent->proc_name == sym)))
953 goto got_variable;
954
955 /* If all else fails, see if we have a specific intrinsic. */
956 if (sym->attr.function
957 && sym->ts.type == BT_UNKNOWN
958 && sym->attr.intrinsic)
959 {
960 gfc_intrinsic_sym *isym;
961 isym = gfc_find_function (sym->name);
962 if (isym == NULL || !isym->specific)
963 {
964 gfc_error ("Unable to find a specific INTRINSIC procedure "
965 "for the reference '%s' at %L", sym->name,
966 &e->where);
967 }
968 sym->ts = isym->ts;
969 }
970 goto argument_list;
971 }
972
973 /* See if the name is a module procedure in a parent unit. */
974
975 if (was_declared (sym) || sym->ns->parent == NULL)
976 goto got_variable;
977
978 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
979 {
980 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
981 return FAILURE;
982 }
983
984 if (parent_st == NULL)
985 goto got_variable;
986
987 sym = parent_st->n.sym;
988 e->symtree = parent_st; /* Point to the right thing. */
989
990 if (sym->attr.flavor == FL_PROCEDURE
991 || sym->attr.intrinsic
992 || sym->attr.external)
993 {
994 goto argument_list;
995 }
996
997 got_variable:
998 e->expr_type = EXPR_VARIABLE;
999 e->ts = sym->ts;
1000 if (sym->as != NULL)
1001 {
1002 e->rank = sym->as->rank;
1003 e->ref = gfc_get_ref ();
1004 e->ref->type = REF_ARRAY;
1005 e->ref->u.ar.type = AR_FULL;
1006 e->ref->u.ar.as = sym->as;
1007 }
1008
1009 argument_list:
1010 /* Check argument list functions %VAL, %LOC and %REF. There is
1011 nothing to do for %REF. */
1012 if (arg->name && arg->name[0] == '%')
1013 {
1014 if (strncmp ("%VAL", arg->name, 4) == 0)
1015 {
1016 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1017 {
1018 gfc_error ("By-value argument at %L is not of numeric "
1019 "type", &e->where);
1020 return FAILURE;
1021 }
1022
1023 if (e->rank)
1024 {
1025 gfc_error ("By-value argument at %L cannot be an array or "
1026 "an array section", &e->where);
1027 return FAILURE;
1028 }
1029
1030 /* Intrinsics are still PROC_UNKNOWN here. However,
1031 since same file external procedures are not resolvable
1032 in gfortran, it is a good deal easier to leave them to
1033 intrinsic.c. */
1034 if (ptype != PROC_UNKNOWN && ptype != PROC_EXTERNAL)
1035 {
1036 gfc_error ("By-value argument at %L is not allowed "
1037 "in this context", &e->where);
1038 return FAILURE;
1039 }
1040
1041 if (((e->ts.type == BT_REAL || e->ts.type == BT_COMPLEX)
1042 && e->ts.kind > gfc_default_real_kind)
1043 || (e->ts.kind > gfc_default_integer_kind))
1044 {
1045 gfc_error ("Kind of by-value argument at %L is larger "
1046 "than default kind", &e->where);
1047 return FAILURE;
1048 }
1049
1050 }
1051
1052 /* Statement functions have already been excluded above. */
1053 else if (strncmp ("%LOC", arg->name, 4) == 0
1054 && e->ts.type == BT_PROCEDURE)
1055 {
1056 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1057 {
1058 gfc_error ("Passing internal procedure at %L by location "
1059 "not allowed", &e->where);
1060 return FAILURE;
1061 }
1062 }
1063 }
1064 }
1065
1066 return SUCCESS;
1067 }
1068
1069
1070 /* Do the checks of the actual argument list that are specific to elemental
1071 procedures. If called with c == NULL, we have a function, otherwise if
1072 expr == NULL, we have a subroutine. */
1073 static try
1074 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1075 {
1076 gfc_actual_arglist *arg0;
1077 gfc_actual_arglist *arg;
1078 gfc_symbol *esym = NULL;
1079 gfc_intrinsic_sym *isym = NULL;
1080 gfc_expr *e = NULL;
1081 gfc_intrinsic_arg *iformal = NULL;
1082 gfc_formal_arglist *eformal = NULL;
1083 bool formal_optional = false;
1084 bool set_by_optional = false;
1085 int i;
1086 int rank = 0;
1087
1088 /* Is this an elemental procedure? */
1089 if (expr && expr->value.function.actual != NULL)
1090 {
1091 if (expr->value.function.esym != NULL
1092 && expr->value.function.esym->attr.elemental)
1093 {
1094 arg0 = expr->value.function.actual;
1095 esym = expr->value.function.esym;
1096 }
1097 else if (expr->value.function.isym != NULL
1098 && expr->value.function.isym->elemental)
1099 {
1100 arg0 = expr->value.function.actual;
1101 isym = expr->value.function.isym;
1102 }
1103 else
1104 return SUCCESS;
1105 }
1106 else if (c && c->ext.actual != NULL
1107 && c->symtree->n.sym->attr.elemental)
1108 {
1109 arg0 = c->ext.actual;
1110 esym = c->symtree->n.sym;
1111 }
1112 else
1113 return SUCCESS;
1114
1115 /* The rank of an elemental is the rank of its array argument(s). */
1116 for (arg = arg0; arg; arg = arg->next)
1117 {
1118 if (arg->expr != NULL && arg->expr->rank > 0)
1119 {
1120 rank = arg->expr->rank;
1121 if (arg->expr->expr_type == EXPR_VARIABLE
1122 && arg->expr->symtree->n.sym->attr.optional)
1123 set_by_optional = true;
1124
1125 /* Function specific; set the result rank and shape. */
1126 if (expr)
1127 {
1128 expr->rank = rank;
1129 if (!expr->shape && arg->expr->shape)
1130 {
1131 expr->shape = gfc_get_shape (rank);
1132 for (i = 0; i < rank; i++)
1133 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1134 }
1135 }
1136 break;
1137 }
1138 }
1139
1140 /* If it is an array, it shall not be supplied as an actual argument
1141 to an elemental procedure unless an array of the same rank is supplied
1142 as an actual argument corresponding to a nonoptional dummy argument of
1143 that elemental procedure(12.4.1.5). */
1144 formal_optional = false;
1145 if (isym)
1146 iformal = isym->formal;
1147 else
1148 eformal = esym->formal;
1149
1150 for (arg = arg0; arg; arg = arg->next)
1151 {
1152 if (eformal)
1153 {
1154 if (eformal->sym && eformal->sym->attr.optional)
1155 formal_optional = true;
1156 eformal = eformal->next;
1157 }
1158 else if (isym && iformal)
1159 {
1160 if (iformal->optional)
1161 formal_optional = true;
1162 iformal = iformal->next;
1163 }
1164 else if (isym)
1165 formal_optional = true;
1166
1167 if (pedantic && arg->expr != NULL
1168 && arg->expr->expr_type == EXPR_VARIABLE
1169 && arg->expr->symtree->n.sym->attr.optional
1170 && formal_optional
1171 && arg->expr->rank
1172 && (set_by_optional || arg->expr->rank != rank)
1173 && !(isym && isym->generic_id == GFC_ISYM_CONVERSION))
1174 {
1175 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1176 "MISSING, it cannot be the actual argument of an "
1177 "ELEMENTAL procedure unless there is a non-optional"
1178 "argument with the same rank (12.4.1.5)",
1179 arg->expr->symtree->n.sym->name, &arg->expr->where);
1180 return FAILURE;
1181 }
1182 }
1183
1184 for (arg = arg0; arg; arg = arg->next)
1185 {
1186 if (arg->expr == NULL || arg->expr->rank == 0)
1187 continue;
1188
1189 /* Being elemental, the last upper bound of an assumed size array
1190 argument must be present. */
1191 if (resolve_assumed_size_actual (arg->expr))
1192 return FAILURE;
1193
1194 if (expr)
1195 continue;
1196
1197 /* Elemental subroutine array actual arguments must conform. */
1198 if (e != NULL)
1199 {
1200 if (gfc_check_conformance ("elemental subroutine", arg->expr, e)
1201 == FAILURE)
1202 return FAILURE;
1203 }
1204 else
1205 e = arg->expr;
1206 }
1207
1208 return SUCCESS;
1209 }
1210
1211
1212 /* Go through each actual argument in ACTUAL and see if it can be
1213 implemented as an inlined, non-copying intrinsic. FNSYM is the
1214 function being called, or NULL if not known. */
1215
1216 static void
1217 find_noncopying_intrinsics (gfc_symbol * fnsym, gfc_actual_arglist * actual)
1218 {
1219 gfc_actual_arglist *ap;
1220 gfc_expr *expr;
1221
1222 for (ap = actual; ap; ap = ap->next)
1223 if (ap->expr
1224 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1225 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1226 ap->expr->inline_noncopying_intrinsic = 1;
1227 }
1228
1229 /* This function does the checking of references to global procedures
1230 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1231 77 and 95 standards. It checks for a gsymbol for the name, making
1232 one if it does not already exist. If it already exists, then the
1233 reference being resolved must correspond to the type of gsymbol.
1234 Otherwise, the new symbol is equipped with the attributes of the
1235 reference. The corresponding code that is called in creating
1236 global entities is parse.c. */
1237
1238 static void
1239 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1240 {
1241 gfc_gsymbol * gsym;
1242 unsigned int type;
1243
1244 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1245
1246 gsym = gfc_get_gsymbol (sym->name);
1247
1248 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1249 global_used (gsym, where);
1250
1251 if (gsym->type == GSYM_UNKNOWN)
1252 {
1253 gsym->type = type;
1254 gsym->where = *where;
1255 }
1256
1257 gsym->used = 1;
1258 }
1259
1260 /************* Function resolution *************/
1261
1262 /* Resolve a function call known to be generic.
1263 Section 14.1.2.4.1. */
1264
1265 static match
1266 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
1267 {
1268 gfc_symbol *s;
1269
1270 if (sym->attr.generic)
1271 {
1272 s =
1273 gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1274 if (s != NULL)
1275 {
1276 expr->value.function.name = s->name;
1277 expr->value.function.esym = s;
1278
1279 if (s->ts.type != BT_UNKNOWN)
1280 expr->ts = s->ts;
1281 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1282 expr->ts = s->result->ts;
1283
1284 if (s->as != NULL)
1285 expr->rank = s->as->rank;
1286 else if (s->result != NULL && s->result->as != NULL)
1287 expr->rank = s->result->as->rank;
1288
1289 return MATCH_YES;
1290 }
1291
1292 /* TODO: Need to search for elemental references in generic interface */
1293 }
1294
1295 if (sym->attr.intrinsic)
1296 return gfc_intrinsic_func_interface (expr, 0);
1297
1298 return MATCH_NO;
1299 }
1300
1301
1302 static try
1303 resolve_generic_f (gfc_expr * expr)
1304 {
1305 gfc_symbol *sym;
1306 match m;
1307
1308 sym = expr->symtree->n.sym;
1309
1310 for (;;)
1311 {
1312 m = resolve_generic_f0 (expr, sym);
1313 if (m == MATCH_YES)
1314 return SUCCESS;
1315 else if (m == MATCH_ERROR)
1316 return FAILURE;
1317
1318 generic:
1319 if (sym->ns->parent == NULL)
1320 break;
1321 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1322
1323 if (sym == NULL)
1324 break;
1325 if (!generic_sym (sym))
1326 goto generic;
1327 }
1328
1329 /* Last ditch attempt. See if the reference is to an intrinsic
1330 that possesses a matching interface. 14.1.2.4 */
1331 if (!gfc_intrinsic_name (sym->name, 0))
1332 {
1333 gfc_error ("There is no specific function for the generic '%s' at %L",
1334 expr->symtree->n.sym->name, &expr->where);
1335 return FAILURE;
1336 }
1337
1338 m = gfc_intrinsic_func_interface (expr, 0);
1339 if (m == MATCH_YES)
1340 return SUCCESS;
1341 if (m == MATCH_NO)
1342 gfc_error
1343 ("Generic function '%s' at %L is not consistent with a specific "
1344 "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
1345
1346 return FAILURE;
1347 }
1348
1349
1350 /* Resolve a function call known to be specific. */
1351
1352 static match
1353 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
1354 {
1355 match m;
1356
1357 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1358 {
1359 if (sym->attr.dummy)
1360 {
1361 sym->attr.proc = PROC_DUMMY;
1362 goto found;
1363 }
1364
1365 sym->attr.proc = PROC_EXTERNAL;
1366 goto found;
1367 }
1368
1369 if (sym->attr.proc == PROC_MODULE
1370 || sym->attr.proc == PROC_ST_FUNCTION
1371 || sym->attr.proc == PROC_INTERNAL)
1372 goto found;
1373
1374 if (sym->attr.intrinsic)
1375 {
1376 m = gfc_intrinsic_func_interface (expr, 1);
1377 if (m == MATCH_YES)
1378 return MATCH_YES;
1379 if (m == MATCH_NO)
1380 gfc_error
1381 ("Function '%s' at %L is INTRINSIC but is not compatible with "
1382 "an intrinsic", sym->name, &expr->where);
1383
1384 return MATCH_ERROR;
1385 }
1386
1387 return MATCH_NO;
1388
1389 found:
1390 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1391
1392 expr->ts = sym->ts;
1393 expr->value.function.name = sym->name;
1394 expr->value.function.esym = sym;
1395 if (sym->as != NULL)
1396 expr->rank = sym->as->rank;
1397
1398 return MATCH_YES;
1399 }
1400
1401
1402 static try
1403 resolve_specific_f (gfc_expr * expr)
1404 {
1405 gfc_symbol *sym;
1406 match m;
1407
1408 sym = expr->symtree->n.sym;
1409
1410 for (;;)
1411 {
1412 m = resolve_specific_f0 (sym, expr);
1413 if (m == MATCH_YES)
1414 return SUCCESS;
1415 if (m == MATCH_ERROR)
1416 return FAILURE;
1417
1418 if (sym->ns->parent == NULL)
1419 break;
1420
1421 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1422
1423 if (sym == NULL)
1424 break;
1425 }
1426
1427 gfc_error ("Unable to resolve the specific function '%s' at %L",
1428 expr->symtree->n.sym->name, &expr->where);
1429
1430 return SUCCESS;
1431 }
1432
1433
1434 /* Resolve a procedure call not known to be generic nor specific. */
1435
1436 static try
1437 resolve_unknown_f (gfc_expr * expr)
1438 {
1439 gfc_symbol *sym;
1440 gfc_typespec *ts;
1441
1442 sym = expr->symtree->n.sym;
1443
1444 if (sym->attr.dummy)
1445 {
1446 sym->attr.proc = PROC_DUMMY;
1447 expr->value.function.name = sym->name;
1448 goto set_type;
1449 }
1450
1451 /* See if we have an intrinsic function reference. */
1452
1453 if (gfc_intrinsic_name (sym->name, 0))
1454 {
1455 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1456 return SUCCESS;
1457 return FAILURE;
1458 }
1459
1460 /* The reference is to an external name. */
1461
1462 sym->attr.proc = PROC_EXTERNAL;
1463 expr->value.function.name = sym->name;
1464 expr->value.function.esym = expr->symtree->n.sym;
1465
1466 if (sym->as != NULL)
1467 expr->rank = sym->as->rank;
1468
1469 /* Type of the expression is either the type of the symbol or the
1470 default type of the symbol. */
1471
1472 set_type:
1473 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1474
1475 if (sym->ts.type != BT_UNKNOWN)
1476 expr->ts = sym->ts;
1477 else
1478 {
1479 ts = gfc_get_default_type (sym, sym->ns);
1480
1481 if (ts->type == BT_UNKNOWN)
1482 {
1483 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1484 sym->name, &expr->where);
1485 return FAILURE;
1486 }
1487 else
1488 expr->ts = *ts;
1489 }
1490
1491 return SUCCESS;
1492 }
1493
1494
1495 /* Figure out if a function reference is pure or not. Also set the name
1496 of the function for a potential error message. Return nonzero if the
1497 function is PURE, zero if not. */
1498
1499 static int
1500 pure_function (gfc_expr * e, const char **name)
1501 {
1502 int pure;
1503
1504 if (e->value.function.esym)
1505 {
1506 pure = gfc_pure (e->value.function.esym);
1507 *name = e->value.function.esym->name;
1508 }
1509 else if (e->value.function.isym)
1510 {
1511 pure = e->value.function.isym->pure
1512 || e->value.function.isym->elemental;
1513 *name = e->value.function.isym->name;
1514 }
1515 else
1516 {
1517 /* Implicit functions are not pure. */
1518 pure = 0;
1519 *name = e->value.function.name;
1520 }
1521
1522 return pure;
1523 }
1524
1525
1526 /* Resolve a function call, which means resolving the arguments, then figuring
1527 out which entity the name refers to. */
1528 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1529 to INTENT(OUT) or INTENT(INOUT). */
1530
1531 static try
1532 resolve_function (gfc_expr * expr)
1533 {
1534 gfc_actual_arglist *arg;
1535 gfc_symbol * sym;
1536 const char *name;
1537 try t;
1538 int temp;
1539 procedure_type p = PROC_INTRINSIC;
1540
1541 sym = NULL;
1542 if (expr->symtree)
1543 sym = expr->symtree->n.sym;
1544
1545 if (sym && sym->attr.flavor == FL_VARIABLE)
1546 {
1547 gfc_error ("'%s' at %L is not a function",
1548 sym->name, &expr->where);
1549 return FAILURE;
1550 }
1551
1552 /* If the procedure is not internal, a statement function or a module
1553 procedure,it must be external and should be checked for usage. */
1554 if (sym && !sym->attr.dummy && !sym->attr.contained
1555 && sym->attr.proc != PROC_ST_FUNCTION
1556 && !sym->attr.use_assoc)
1557 resolve_global_procedure (sym, &expr->where, 0);
1558
1559 /* Switch off assumed size checking and do this again for certain kinds
1560 of procedure, once the procedure itself is resolved. */
1561 need_full_assumed_size++;
1562
1563 if (expr->symtree && expr->symtree->n.sym)
1564 p = expr->symtree->n.sym->attr.proc;
1565
1566 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
1567 return FAILURE;
1568
1569 /* Resume assumed_size checking. */
1570 need_full_assumed_size--;
1571
1572 if (sym && sym->ts.type == BT_CHARACTER
1573 && sym->ts.cl
1574 && sym->ts.cl->length == NULL
1575 && !sym->attr.dummy
1576 && expr->value.function.esym == NULL
1577 && !sym->attr.contained)
1578 {
1579 /* Internal procedures are taken care of in resolve_contained_fntype. */
1580 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1581 "be used at %L since it is not a dummy argument",
1582 sym->name, &expr->where);
1583 return FAILURE;
1584 }
1585
1586 /* See if function is already resolved. */
1587
1588 if (expr->value.function.name != NULL)
1589 {
1590 if (expr->ts.type == BT_UNKNOWN)
1591 expr->ts = sym->ts;
1592 t = SUCCESS;
1593 }
1594 else
1595 {
1596 /* Apply the rules of section 14.1.2. */
1597
1598 switch (procedure_kind (sym))
1599 {
1600 case PTYPE_GENERIC:
1601 t = resolve_generic_f (expr);
1602 break;
1603
1604 case PTYPE_SPECIFIC:
1605 t = resolve_specific_f (expr);
1606 break;
1607
1608 case PTYPE_UNKNOWN:
1609 t = resolve_unknown_f (expr);
1610 break;
1611
1612 default:
1613 gfc_internal_error ("resolve_function(): bad function type");
1614 }
1615 }
1616
1617 /* If the expression is still a function (it might have simplified),
1618 then we check to see if we are calling an elemental function. */
1619
1620 if (expr->expr_type != EXPR_FUNCTION)
1621 return t;
1622
1623 temp = need_full_assumed_size;
1624 need_full_assumed_size = 0;
1625
1626 if (resolve_elemental_actual (expr, NULL) == FAILURE)
1627 return FAILURE;
1628
1629 if (omp_workshare_flag
1630 && expr->value.function.esym
1631 && ! gfc_elemental (expr->value.function.esym))
1632 {
1633 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed"
1634 " in WORKSHARE construct", expr->value.function.esym->name,
1635 &expr->where);
1636 t = FAILURE;
1637 }
1638
1639 #define GENERIC_ID expr->value.function.isym->generic_id
1640 else if (expr->value.function.actual != NULL
1641 && expr->value.function.isym != NULL
1642 && GENERIC_ID != GFC_ISYM_LBOUND
1643 && GENERIC_ID != GFC_ISYM_LEN
1644 && GENERIC_ID != GFC_ISYM_LOC
1645 && GENERIC_ID != GFC_ISYM_PRESENT)
1646 {
1647 /* Array intrinsics must also have the last upper bound of an
1648 assumed size array argument. UBOUND and SIZE have to be
1649 excluded from the check if the second argument is anything
1650 than a constant. */
1651 int inquiry;
1652 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
1653 || GENERIC_ID == GFC_ISYM_SIZE;
1654
1655 for (arg = expr->value.function.actual; arg; arg = arg->next)
1656 {
1657 if (inquiry && arg->next != NULL && arg->next->expr
1658 && arg->next->expr->expr_type != EXPR_CONSTANT)
1659 break;
1660
1661 if (arg->expr != NULL
1662 && arg->expr->rank > 0
1663 && resolve_assumed_size_actual (arg->expr))
1664 return FAILURE;
1665 }
1666 }
1667 #undef GENERIC_ID
1668
1669 need_full_assumed_size = temp;
1670
1671 if (!pure_function (expr, &name) && name)
1672 {
1673 if (forall_flag)
1674 {
1675 gfc_error
1676 ("reference to non-PURE function '%s' at %L inside a "
1677 "FORALL %s", name, &expr->where, forall_flag == 2 ?
1678 "mask" : "block");
1679 t = FAILURE;
1680 }
1681 else if (gfc_pure (NULL))
1682 {
1683 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1684 "procedure within a PURE procedure", name, &expr->where);
1685 t = FAILURE;
1686 }
1687 }
1688
1689 /* Functions without the RECURSIVE attribution are not allowed to
1690 * call themselves. */
1691 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
1692 {
1693 gfc_symbol *esym, *proc;
1694 esym = expr->value.function.esym;
1695 proc = gfc_current_ns->proc_name;
1696 if (esym == proc)
1697 {
1698 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
1699 "RECURSIVE", name, &expr->where);
1700 t = FAILURE;
1701 }
1702
1703 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
1704 && esym->ns->entries->sym == proc->ns->entries->sym)
1705 {
1706 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
1707 "'%s' is not declared as RECURSIVE",
1708 esym->name, &expr->where, esym->ns->entries->sym->name);
1709 t = FAILURE;
1710 }
1711 }
1712
1713 /* Character lengths of use associated functions may contains references to
1714 symbols not referenced from the current program unit otherwise. Make sure
1715 those symbols are marked as referenced. */
1716
1717 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
1718 && expr->value.function.esym->attr.use_assoc)
1719 {
1720 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
1721 }
1722
1723 if (t == SUCCESS)
1724 find_noncopying_intrinsics (expr->value.function.esym,
1725 expr->value.function.actual);
1726 return t;
1727 }
1728
1729
1730 /************* Subroutine resolution *************/
1731
1732 static void
1733 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1734 {
1735
1736 if (gfc_pure (sym))
1737 return;
1738
1739 if (forall_flag)
1740 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1741 sym->name, &c->loc);
1742 else if (gfc_pure (NULL))
1743 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1744 &c->loc);
1745 }
1746
1747
1748 static match
1749 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1750 {
1751 gfc_symbol *s;
1752
1753 if (sym->attr.generic)
1754 {
1755 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1756 if (s != NULL)
1757 {
1758 c->resolved_sym = s;
1759 pure_subroutine (c, s);
1760 return MATCH_YES;
1761 }
1762
1763 /* TODO: Need to search for elemental references in generic interface. */
1764 }
1765
1766 if (sym->attr.intrinsic)
1767 return gfc_intrinsic_sub_interface (c, 0);
1768
1769 return MATCH_NO;
1770 }
1771
1772
1773 static try
1774 resolve_generic_s (gfc_code * c)
1775 {
1776 gfc_symbol *sym;
1777 match m;
1778
1779 sym = c->symtree->n.sym;
1780
1781 for (;;)
1782 {
1783 m = resolve_generic_s0 (c, sym);
1784 if (m == MATCH_YES)
1785 return SUCCESS;
1786 else if (m == MATCH_ERROR)
1787 return FAILURE;
1788
1789 generic:
1790 if (sym->ns->parent == NULL)
1791 break;
1792 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1793
1794 if (sym == NULL)
1795 break;
1796 if (!generic_sym (sym))
1797 goto generic;
1798 }
1799
1800 /* Last ditch attempt. See if the reference is to an intrinsic
1801 that possesses a matching interface. 14.1.2.4 */
1802 sym = c->symtree->n.sym;
1803
1804 if (!gfc_intrinsic_name (sym->name, 1))
1805 {
1806 gfc_error
1807 ("There is no specific subroutine for the generic '%s' at %L",
1808 sym->name, &c->loc);
1809 return FAILURE;
1810 }
1811
1812 m = gfc_intrinsic_sub_interface (c, 0);
1813 if (m == MATCH_YES)
1814 return SUCCESS;
1815 if (m == MATCH_NO)
1816 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1817 "intrinsic subroutine interface", sym->name, &c->loc);
1818
1819 return FAILURE;
1820 }
1821
1822
1823 /* Resolve a subroutine call known to be specific. */
1824
1825 static match
1826 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1827 {
1828 match m;
1829
1830 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1831 {
1832 if (sym->attr.dummy)
1833 {
1834 sym->attr.proc = PROC_DUMMY;
1835 goto found;
1836 }
1837
1838 sym->attr.proc = PROC_EXTERNAL;
1839 goto found;
1840 }
1841
1842 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1843 goto found;
1844
1845 if (sym->attr.intrinsic)
1846 {
1847 m = gfc_intrinsic_sub_interface (c, 1);
1848 if (m == MATCH_YES)
1849 return MATCH_YES;
1850 if (m == MATCH_NO)
1851 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1852 "with an intrinsic", sym->name, &c->loc);
1853
1854 return MATCH_ERROR;
1855 }
1856
1857 return MATCH_NO;
1858
1859 found:
1860 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1861
1862 c->resolved_sym = sym;
1863 pure_subroutine (c, sym);
1864
1865 return MATCH_YES;
1866 }
1867
1868
1869 static try
1870 resolve_specific_s (gfc_code * c)
1871 {
1872 gfc_symbol *sym;
1873 match m;
1874
1875 sym = c->symtree->n.sym;
1876
1877 for (;;)
1878 {
1879 m = resolve_specific_s0 (c, sym);
1880 if (m == MATCH_YES)
1881 return SUCCESS;
1882 if (m == MATCH_ERROR)
1883 return FAILURE;
1884
1885 if (sym->ns->parent == NULL)
1886 break;
1887
1888 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1889
1890 if (sym == NULL)
1891 break;
1892 }
1893
1894 sym = c->symtree->n.sym;
1895 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1896 sym->name, &c->loc);
1897
1898 return FAILURE;
1899 }
1900
1901
1902 /* Resolve a subroutine call not known to be generic nor specific. */
1903
1904 static try
1905 resolve_unknown_s (gfc_code * c)
1906 {
1907 gfc_symbol *sym;
1908
1909 sym = c->symtree->n.sym;
1910
1911 if (sym->attr.dummy)
1912 {
1913 sym->attr.proc = PROC_DUMMY;
1914 goto found;
1915 }
1916
1917 /* See if we have an intrinsic function reference. */
1918
1919 if (gfc_intrinsic_name (sym->name, 1))
1920 {
1921 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1922 return SUCCESS;
1923 return FAILURE;
1924 }
1925
1926 /* The reference is to an external name. */
1927
1928 found:
1929 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1930
1931 c->resolved_sym = sym;
1932
1933 pure_subroutine (c, sym);
1934
1935 return SUCCESS;
1936 }
1937
1938
1939 /* Resolve a subroutine call. Although it was tempting to use the same code
1940 for functions, subroutines and functions are stored differently and this
1941 makes things awkward. */
1942
1943 static try
1944 resolve_call (gfc_code * c)
1945 {
1946 try t;
1947 procedure_type ptype = PROC_INTRINSIC;
1948
1949 if (c->symtree && c->symtree->n.sym
1950 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
1951 {
1952 gfc_error ("'%s' at %L has a type, which is not consistent with "
1953 "the CALL at %L", c->symtree->n.sym->name,
1954 &c->symtree->n.sym->declared_at, &c->loc);
1955 return FAILURE;
1956 }
1957
1958 /* If the procedure is not internal or module, it must be external and
1959 should be checked for usage. */
1960 if (c->symtree && c->symtree->n.sym
1961 && !c->symtree->n.sym->attr.dummy
1962 && !c->symtree->n.sym->attr.contained
1963 && !c->symtree->n.sym->attr.use_assoc)
1964 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
1965
1966 /* Subroutines without the RECURSIVE attribution are not allowed to
1967 * call themselves. */
1968 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
1969 {
1970 gfc_symbol *csym, *proc;
1971 csym = c->symtree->n.sym;
1972 proc = gfc_current_ns->proc_name;
1973 if (csym == proc)
1974 {
1975 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
1976 "RECURSIVE", csym->name, &c->loc);
1977 t = FAILURE;
1978 }
1979
1980 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
1981 && csym->ns->entries->sym == proc->ns->entries->sym)
1982 {
1983 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
1984 "'%s' is not declared as RECURSIVE",
1985 csym->name, &c->loc, csym->ns->entries->sym->name);
1986 t = FAILURE;
1987 }
1988 }
1989
1990 /* Switch off assumed size checking and do this again for certain kinds
1991 of procedure, once the procedure itself is resolved. */
1992 need_full_assumed_size++;
1993
1994 if (c->symtree && c->symtree->n.sym)
1995 ptype = c->symtree->n.sym->attr.proc;
1996
1997 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
1998 return FAILURE;
1999
2000 /* Resume assumed_size checking. */
2001 need_full_assumed_size--;
2002
2003
2004 t = SUCCESS;
2005 if (c->resolved_sym == NULL)
2006 switch (procedure_kind (c->symtree->n.sym))
2007 {
2008 case PTYPE_GENERIC:
2009 t = resolve_generic_s (c);
2010 break;
2011
2012 case PTYPE_SPECIFIC:
2013 t = resolve_specific_s (c);
2014 break;
2015
2016 case PTYPE_UNKNOWN:
2017 t = resolve_unknown_s (c);
2018 break;
2019
2020 default:
2021 gfc_internal_error ("resolve_subroutine(): bad function type");
2022 }
2023
2024 /* Some checks of elemental subroutine actual arguments. */
2025 if (resolve_elemental_actual (NULL, c) == FAILURE)
2026 return FAILURE;
2027
2028 if (t == SUCCESS)
2029 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2030 return t;
2031 }
2032
2033 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2034 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2035 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2036 if their shapes do not match. If either op1->shape or op2->shape is
2037 NULL, return SUCCESS. */
2038
2039 static try
2040 compare_shapes (gfc_expr * op1, gfc_expr * op2)
2041 {
2042 try t;
2043 int i;
2044
2045 t = SUCCESS;
2046
2047 if (op1->shape != NULL && op2->shape != NULL)
2048 {
2049 for (i = 0; i < op1->rank; i++)
2050 {
2051 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2052 {
2053 gfc_error ("Shapes for operands at %L and %L are not conformable",
2054 &op1->where, &op2->where);
2055 t = FAILURE;
2056 break;
2057 }
2058 }
2059 }
2060
2061 return t;
2062 }
2063
2064 /* Resolve an operator expression node. This can involve replacing the
2065 operation with a user defined function call. */
2066
2067 static try
2068 resolve_operator (gfc_expr * e)
2069 {
2070 gfc_expr *op1, *op2;
2071 char msg[200];
2072 try t;
2073
2074 /* Resolve all subnodes-- give them types. */
2075
2076 switch (e->value.op.operator)
2077 {
2078 default:
2079 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2080 return FAILURE;
2081
2082 /* Fall through... */
2083
2084 case INTRINSIC_NOT:
2085 case INTRINSIC_UPLUS:
2086 case INTRINSIC_UMINUS:
2087 case INTRINSIC_PARENTHESES:
2088 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2089 return FAILURE;
2090 break;
2091 }
2092
2093 /* Typecheck the new node. */
2094
2095 op1 = e->value.op.op1;
2096 op2 = e->value.op.op2;
2097
2098 switch (e->value.op.operator)
2099 {
2100 case INTRINSIC_UPLUS:
2101 case INTRINSIC_UMINUS:
2102 if (op1->ts.type == BT_INTEGER
2103 || op1->ts.type == BT_REAL
2104 || op1->ts.type == BT_COMPLEX)
2105 {
2106 e->ts = op1->ts;
2107 break;
2108 }
2109
2110 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2111 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2112 goto bad_op;
2113
2114 case INTRINSIC_PLUS:
2115 case INTRINSIC_MINUS:
2116 case INTRINSIC_TIMES:
2117 case INTRINSIC_DIVIDE:
2118 case INTRINSIC_POWER:
2119 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2120 {
2121 gfc_type_convert_binary (e);
2122 break;
2123 }
2124
2125 sprintf (msg,
2126 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2127 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2128 gfc_typename (&op2->ts));
2129 goto bad_op;
2130
2131 case INTRINSIC_CONCAT:
2132 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2133 {
2134 e->ts.type = BT_CHARACTER;
2135 e->ts.kind = op1->ts.kind;
2136 break;
2137 }
2138
2139 sprintf (msg,
2140 _("Operands of string concatenation operator at %%L are %s/%s"),
2141 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2142 goto bad_op;
2143
2144 case INTRINSIC_AND:
2145 case INTRINSIC_OR:
2146 case INTRINSIC_EQV:
2147 case INTRINSIC_NEQV:
2148 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2149 {
2150 e->ts.type = BT_LOGICAL;
2151 e->ts.kind = gfc_kind_max (op1, op2);
2152 if (op1->ts.kind < e->ts.kind)
2153 gfc_convert_type (op1, &e->ts, 2);
2154 else if (op2->ts.kind < e->ts.kind)
2155 gfc_convert_type (op2, &e->ts, 2);
2156 break;
2157 }
2158
2159 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2160 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2161 gfc_typename (&op2->ts));
2162
2163 goto bad_op;
2164
2165 case INTRINSIC_NOT:
2166 if (op1->ts.type == BT_LOGICAL)
2167 {
2168 e->ts.type = BT_LOGICAL;
2169 e->ts.kind = op1->ts.kind;
2170 break;
2171 }
2172
2173 sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
2174 gfc_typename (&op1->ts));
2175 goto bad_op;
2176
2177 case INTRINSIC_GT:
2178 case INTRINSIC_GE:
2179 case INTRINSIC_LT:
2180 case INTRINSIC_LE:
2181 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2182 {
2183 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
2184 goto bad_op;
2185 }
2186
2187 /* Fall through... */
2188
2189 case INTRINSIC_EQ:
2190 case INTRINSIC_NE:
2191 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2192 {
2193 e->ts.type = BT_LOGICAL;
2194 e->ts.kind = gfc_default_logical_kind;
2195 break;
2196 }
2197
2198 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2199 {
2200 gfc_type_convert_binary (e);
2201
2202 e->ts.type = BT_LOGICAL;
2203 e->ts.kind = gfc_default_logical_kind;
2204 break;
2205 }
2206
2207 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2208 sprintf (msg,
2209 _("Logicals at %%L must be compared with %s instead of %s"),
2210 e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
2211 gfc_op2string (e->value.op.operator));
2212 else
2213 sprintf (msg,
2214 _("Operands of comparison operator '%s' at %%L are %s/%s"),
2215 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2216 gfc_typename (&op2->ts));
2217
2218 goto bad_op;
2219
2220 case INTRINSIC_USER:
2221 if (op2 == NULL)
2222 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
2223 e->value.op.uop->name, gfc_typename (&op1->ts));
2224 else
2225 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
2226 e->value.op.uop->name, gfc_typename (&op1->ts),
2227 gfc_typename (&op2->ts));
2228
2229 goto bad_op;
2230
2231 case INTRINSIC_PARENTHESES:
2232 break;
2233
2234 default:
2235 gfc_internal_error ("resolve_operator(): Bad intrinsic");
2236 }
2237
2238 /* Deal with arrayness of an operand through an operator. */
2239
2240 t = SUCCESS;
2241
2242 switch (e->value.op.operator)
2243 {
2244 case INTRINSIC_PLUS:
2245 case INTRINSIC_MINUS:
2246 case INTRINSIC_TIMES:
2247 case INTRINSIC_DIVIDE:
2248 case INTRINSIC_POWER:
2249 case INTRINSIC_CONCAT:
2250 case INTRINSIC_AND:
2251 case INTRINSIC_OR:
2252 case INTRINSIC_EQV:
2253 case INTRINSIC_NEQV:
2254 case INTRINSIC_EQ:
2255 case INTRINSIC_NE:
2256 case INTRINSIC_GT:
2257 case INTRINSIC_GE:
2258 case INTRINSIC_LT:
2259 case INTRINSIC_LE:
2260
2261 if (op1->rank == 0 && op2->rank == 0)
2262 e->rank = 0;
2263
2264 if (op1->rank == 0 && op2->rank != 0)
2265 {
2266 e->rank = op2->rank;
2267
2268 if (e->shape == NULL)
2269 e->shape = gfc_copy_shape (op2->shape, op2->rank);
2270 }
2271
2272 if (op1->rank != 0 && op2->rank == 0)
2273 {
2274 e->rank = op1->rank;
2275
2276 if (e->shape == NULL)
2277 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2278 }
2279
2280 if (op1->rank != 0 && op2->rank != 0)
2281 {
2282 if (op1->rank == op2->rank)
2283 {
2284 e->rank = op1->rank;
2285 if (e->shape == NULL)
2286 {
2287 t = compare_shapes(op1, op2);
2288 if (t == FAILURE)
2289 e->shape = NULL;
2290 else
2291 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2292 }
2293 }
2294 else
2295 {
2296 gfc_error ("Inconsistent ranks for operator at %L and %L",
2297 &op1->where, &op2->where);
2298 t = FAILURE;
2299
2300 /* Allow higher level expressions to work. */
2301 e->rank = 0;
2302 }
2303 }
2304
2305 break;
2306
2307 case INTRINSIC_NOT:
2308 case INTRINSIC_UPLUS:
2309 case INTRINSIC_UMINUS:
2310 case INTRINSIC_PARENTHESES:
2311 e->rank = op1->rank;
2312
2313 if (e->shape == NULL)
2314 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2315
2316 /* Simply copy arrayness attribute */
2317 break;
2318
2319 default:
2320 break;
2321 }
2322
2323 /* Attempt to simplify the expression. */
2324 if (t == SUCCESS)
2325 {
2326 t = gfc_simplify_expr (e, 0);
2327 /* Some calls do not succeed in simplification and return FAILURE
2328 even though there is no error; eg. variable references to
2329 PARAMETER arrays. */
2330 if (!gfc_is_constant_expr (e))
2331 t = SUCCESS;
2332 }
2333 return t;
2334
2335 bad_op:
2336
2337 if (gfc_extend_expr (e) == SUCCESS)
2338 return SUCCESS;
2339
2340 gfc_error (msg, &e->where);
2341
2342 return FAILURE;
2343 }
2344
2345
2346 /************** Array resolution subroutines **************/
2347
2348
2349 typedef enum
2350 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
2351 comparison;
2352
2353 /* Compare two integer expressions. */
2354
2355 static comparison
2356 compare_bound (gfc_expr * a, gfc_expr * b)
2357 {
2358 int i;
2359
2360 if (a == NULL || a->expr_type != EXPR_CONSTANT
2361 || b == NULL || b->expr_type != EXPR_CONSTANT)
2362 return CMP_UNKNOWN;
2363
2364 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
2365 gfc_internal_error ("compare_bound(): Bad expression");
2366
2367 i = mpz_cmp (a->value.integer, b->value.integer);
2368
2369 if (i < 0)
2370 return CMP_LT;
2371 if (i > 0)
2372 return CMP_GT;
2373 return CMP_EQ;
2374 }
2375
2376
2377 /* Compare an integer expression with an integer. */
2378
2379 static comparison
2380 compare_bound_int (gfc_expr * a, int b)
2381 {
2382 int i;
2383
2384 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2385 return CMP_UNKNOWN;
2386
2387 if (a->ts.type != BT_INTEGER)
2388 gfc_internal_error ("compare_bound_int(): Bad expression");
2389
2390 i = mpz_cmp_si (a->value.integer, b);
2391
2392 if (i < 0)
2393 return CMP_LT;
2394 if (i > 0)
2395 return CMP_GT;
2396 return CMP_EQ;
2397 }
2398
2399
2400 /* Compare an integer expression with a mpz_t. */
2401
2402 static comparison
2403 compare_bound_mpz_t (gfc_expr * a, mpz_t b)
2404 {
2405 int i;
2406
2407 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2408 return CMP_UNKNOWN;
2409
2410 if (a->ts.type != BT_INTEGER)
2411 gfc_internal_error ("compare_bound_int(): Bad expression");
2412
2413 i = mpz_cmp (a->value.integer, b);
2414
2415 if (i < 0)
2416 return CMP_LT;
2417 if (i > 0)
2418 return CMP_GT;
2419 return CMP_EQ;
2420 }
2421
2422
2423 /* Compute the last value of a sequence given by a triplet.
2424 Return 0 if it wasn't able to compute the last value, or if the
2425 sequence if empty, and 1 otherwise. */
2426
2427 static int
2428 compute_last_value_for_triplet (gfc_expr * start, gfc_expr * end,
2429 gfc_expr * stride, mpz_t last)
2430 {
2431 mpz_t rem;
2432
2433 if (start == NULL || start->expr_type != EXPR_CONSTANT
2434 || end == NULL || end->expr_type != EXPR_CONSTANT
2435 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
2436 return 0;
2437
2438 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
2439 || (stride != NULL && stride->ts.type != BT_INTEGER))
2440 return 0;
2441
2442 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
2443 {
2444 if (compare_bound (start, end) == CMP_GT)
2445 return 0;
2446 mpz_set (last, end->value.integer);
2447 return 1;
2448 }
2449
2450 if (compare_bound_int (stride, 0) == CMP_GT)
2451 {
2452 /* Stride is positive */
2453 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
2454 return 0;
2455 }
2456 else
2457 {
2458 /* Stride is negative */
2459 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
2460 return 0;
2461 }
2462
2463 mpz_init (rem);
2464 mpz_sub (rem, end->value.integer, start->value.integer);
2465 mpz_tdiv_r (rem, rem, stride->value.integer);
2466 mpz_sub (last, end->value.integer, rem);
2467 mpz_clear (rem);
2468
2469 return 1;
2470 }
2471
2472
2473 /* Compare a single dimension of an array reference to the array
2474 specification. */
2475
2476 static try
2477 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
2478 {
2479 mpz_t last_value;
2480
2481 /* Given start, end and stride values, calculate the minimum and
2482 maximum referenced indexes. */
2483
2484 switch (ar->type)
2485 {
2486 case AR_FULL:
2487 break;
2488
2489 case AR_ELEMENT:
2490 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2491 goto bound;
2492 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2493 goto bound;
2494
2495 break;
2496
2497 case AR_SECTION:
2498 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
2499 {
2500 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
2501 return FAILURE;
2502 }
2503
2504 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
2505 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
2506
2507 if (compare_bound (AR_START, AR_END) == CMP_EQ
2508 && (compare_bound (AR_START, as->lower[i]) == CMP_LT
2509 || compare_bound (AR_START, as->upper[i]) == CMP_GT))
2510 goto bound;
2511
2512 if (((compare_bound_int (ar->stride[i], 0) == CMP_GT
2513 || ar->stride[i] == NULL)
2514 && compare_bound (AR_START, AR_END) != CMP_GT)
2515 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
2516 && compare_bound (AR_START, AR_END) != CMP_LT))
2517 {
2518 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
2519 goto bound;
2520 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
2521 goto bound;
2522 }
2523
2524 mpz_init (last_value);
2525 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
2526 last_value))
2527 {
2528 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT
2529 || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
2530 {
2531 mpz_clear (last_value);
2532 goto bound;
2533 }
2534 }
2535 mpz_clear (last_value);
2536
2537 #undef AR_START
2538 #undef AR_END
2539
2540 break;
2541
2542 default:
2543 gfc_internal_error ("check_dimension(): Bad array reference");
2544 }
2545
2546 return SUCCESS;
2547
2548 bound:
2549 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
2550 return SUCCESS;
2551 }
2552
2553
2554 /* Compare an array reference with an array specification. */
2555
2556 static try
2557 compare_spec_to_ref (gfc_array_ref * ar)
2558 {
2559 gfc_array_spec *as;
2560 int i;
2561
2562 as = ar->as;
2563 i = as->rank - 1;
2564 /* TODO: Full array sections are only allowed as actual parameters. */
2565 if (as->type == AS_ASSUMED_SIZE
2566 && (/*ar->type == AR_FULL
2567 ||*/ (ar->type == AR_SECTION
2568 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
2569 {
2570 gfc_error ("Rightmost upper bound of assumed size array section"
2571 " not specified at %L", &ar->where);
2572 return FAILURE;
2573 }
2574
2575 if (ar->type == AR_FULL)
2576 return SUCCESS;
2577
2578 if (as->rank != ar->dimen)
2579 {
2580 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2581 &ar->where, ar->dimen, as->rank);
2582 return FAILURE;
2583 }
2584
2585 for (i = 0; i < as->rank; i++)
2586 if (check_dimension (i, ar, as) == FAILURE)
2587 return FAILURE;
2588
2589 return SUCCESS;
2590 }
2591
2592
2593 /* Resolve one part of an array index. */
2594
2595 try
2596 gfc_resolve_index (gfc_expr * index, int check_scalar)
2597 {
2598 gfc_typespec ts;
2599
2600 if (index == NULL)
2601 return SUCCESS;
2602
2603 if (gfc_resolve_expr (index) == FAILURE)
2604 return FAILURE;
2605
2606 if (check_scalar && index->rank != 0)
2607 {
2608 gfc_error ("Array index at %L must be scalar", &index->where);
2609 return FAILURE;
2610 }
2611
2612 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
2613 {
2614 gfc_error ("Array index at %L must be of INTEGER type",
2615 &index->where);
2616 return FAILURE;
2617 }
2618
2619 if (index->ts.type == BT_REAL)
2620 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
2621 &index->where) == FAILURE)
2622 return FAILURE;
2623
2624 if (index->ts.kind != gfc_index_integer_kind
2625 || index->ts.type != BT_INTEGER)
2626 {
2627 gfc_clear_ts (&ts);
2628 ts.type = BT_INTEGER;
2629 ts.kind = gfc_index_integer_kind;
2630
2631 gfc_convert_type_warn (index, &ts, 2, 0);
2632 }
2633
2634 return SUCCESS;
2635 }
2636
2637 /* Resolve a dim argument to an intrinsic function. */
2638
2639 try
2640 gfc_resolve_dim_arg (gfc_expr *dim)
2641 {
2642 if (dim == NULL)
2643 return SUCCESS;
2644
2645 if (gfc_resolve_expr (dim) == FAILURE)
2646 return FAILURE;
2647
2648 if (dim->rank != 0)
2649 {
2650 gfc_error ("Argument dim at %L must be scalar", &dim->where);
2651 return FAILURE;
2652
2653 }
2654 if (dim->ts.type != BT_INTEGER)
2655 {
2656 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
2657 return FAILURE;
2658 }
2659 if (dim->ts.kind != gfc_index_integer_kind)
2660 {
2661 gfc_typespec ts;
2662
2663 ts.type = BT_INTEGER;
2664 ts.kind = gfc_index_integer_kind;
2665
2666 gfc_convert_type_warn (dim, &ts, 2, 0);
2667 }
2668
2669 return SUCCESS;
2670 }
2671
2672 /* Given an expression that contains array references, update those array
2673 references to point to the right array specifications. While this is
2674 filled in during matching, this information is difficult to save and load
2675 in a module, so we take care of it here.
2676
2677 The idea here is that the original array reference comes from the
2678 base symbol. We traverse the list of reference structures, setting
2679 the stored reference to references. Component references can
2680 provide an additional array specification. */
2681
2682 static void
2683 find_array_spec (gfc_expr * e)
2684 {
2685 gfc_array_spec *as;
2686 gfc_component *c;
2687 gfc_symbol *derived;
2688 gfc_ref *ref;
2689
2690 as = e->symtree->n.sym->as;
2691 derived = NULL;
2692
2693 for (ref = e->ref; ref; ref = ref->next)
2694 switch (ref->type)
2695 {
2696 case REF_ARRAY:
2697 if (as == NULL)
2698 gfc_internal_error ("find_array_spec(): Missing spec");
2699
2700 ref->u.ar.as = as;
2701 as = NULL;
2702 break;
2703
2704 case REF_COMPONENT:
2705 if (derived == NULL)
2706 derived = e->symtree->n.sym->ts.derived;
2707
2708 c = derived->components;
2709
2710 for (; c; c = c->next)
2711 if (c == ref->u.c.component)
2712 {
2713 /* Track the sequence of component references. */
2714 if (c->ts.type == BT_DERIVED)
2715 derived = c->ts.derived;
2716 break;
2717 }
2718
2719 if (c == NULL)
2720 gfc_internal_error ("find_array_spec(): Component not found");
2721
2722 if (c->dimension)
2723 {
2724 if (as != NULL)
2725 gfc_internal_error ("find_array_spec(): unused as(1)");
2726 as = c->as;
2727 }
2728
2729 break;
2730
2731 case REF_SUBSTRING:
2732 break;
2733 }
2734
2735 if (as != NULL)
2736 gfc_internal_error ("find_array_spec(): unused as(2)");
2737 }
2738
2739
2740 /* Resolve an array reference. */
2741
2742 static try
2743 resolve_array_ref (gfc_array_ref * ar)
2744 {
2745 int i, check_scalar;
2746 gfc_expr *e;
2747
2748 for (i = 0; i < ar->dimen; i++)
2749 {
2750 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2751
2752 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2753 return FAILURE;
2754 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2755 return FAILURE;
2756 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2757 return FAILURE;
2758
2759 e = ar->start[i];
2760
2761 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2762 switch (e->rank)
2763 {
2764 case 0:
2765 ar->dimen_type[i] = DIMEN_ELEMENT;
2766 break;
2767
2768 case 1:
2769 ar->dimen_type[i] = DIMEN_VECTOR;
2770 if (e->expr_type == EXPR_VARIABLE
2771 && e->symtree->n.sym->ts.type == BT_DERIVED)
2772 ar->start[i] = gfc_get_parentheses (e);
2773 break;
2774
2775 default:
2776 gfc_error ("Array index at %L is an array of rank %d",
2777 &ar->c_where[i], e->rank);
2778 return FAILURE;
2779 }
2780 }
2781
2782 /* If the reference type is unknown, figure out what kind it is. */
2783
2784 if (ar->type == AR_UNKNOWN)
2785 {
2786 ar->type = AR_ELEMENT;
2787 for (i = 0; i < ar->dimen; i++)
2788 if (ar->dimen_type[i] == DIMEN_RANGE
2789 || ar->dimen_type[i] == DIMEN_VECTOR)
2790 {
2791 ar->type = AR_SECTION;
2792 break;
2793 }
2794 }
2795
2796 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2797 return FAILURE;
2798
2799 return SUCCESS;
2800 }
2801
2802
2803 static try
2804 resolve_substring (gfc_ref * ref)
2805 {
2806
2807 if (ref->u.ss.start != NULL)
2808 {
2809 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2810 return FAILURE;
2811
2812 if (ref->u.ss.start->ts.type != BT_INTEGER)
2813 {
2814 gfc_error ("Substring start index at %L must be of type INTEGER",
2815 &ref->u.ss.start->where);
2816 return FAILURE;
2817 }
2818
2819 if (ref->u.ss.start->rank != 0)
2820 {
2821 gfc_error ("Substring start index at %L must be scalar",
2822 &ref->u.ss.start->where);
2823 return FAILURE;
2824 }
2825
2826 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
2827 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2828 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2829 {
2830 gfc_error ("Substring start index at %L is less than one",
2831 &ref->u.ss.start->where);
2832 return FAILURE;
2833 }
2834 }
2835
2836 if (ref->u.ss.end != NULL)
2837 {
2838 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2839 return FAILURE;
2840
2841 if (ref->u.ss.end->ts.type != BT_INTEGER)
2842 {
2843 gfc_error ("Substring end index at %L must be of type INTEGER",
2844 &ref->u.ss.end->where);
2845 return FAILURE;
2846 }
2847
2848 if (ref->u.ss.end->rank != 0)
2849 {
2850 gfc_error ("Substring end index at %L must be scalar",
2851 &ref->u.ss.end->where);
2852 return FAILURE;
2853 }
2854
2855 if (ref->u.ss.length != NULL
2856 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
2857 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2858 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2859 {
2860 gfc_error ("Substring end index at %L exceeds the string length",
2861 &ref->u.ss.start->where);
2862 return FAILURE;
2863 }
2864 }
2865
2866 return SUCCESS;
2867 }
2868
2869
2870 /* Resolve subtype references. */
2871
2872 static try
2873 resolve_ref (gfc_expr * expr)
2874 {
2875 int current_part_dimension, n_components, seen_part_dimension;
2876 gfc_ref *ref;
2877
2878 for (ref = expr->ref; ref; ref = ref->next)
2879 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2880 {
2881 find_array_spec (expr);
2882 break;
2883 }
2884
2885 for (ref = expr->ref; ref; ref = ref->next)
2886 switch (ref->type)
2887 {
2888 case REF_ARRAY:
2889 if (resolve_array_ref (&ref->u.ar) == FAILURE)
2890 return FAILURE;
2891 break;
2892
2893 case REF_COMPONENT:
2894 break;
2895
2896 case REF_SUBSTRING:
2897 resolve_substring (ref);
2898 break;
2899 }
2900
2901 /* Check constraints on part references. */
2902
2903 current_part_dimension = 0;
2904 seen_part_dimension = 0;
2905 n_components = 0;
2906
2907 for (ref = expr->ref; ref; ref = ref->next)
2908 {
2909 switch (ref->type)
2910 {
2911 case REF_ARRAY:
2912 switch (ref->u.ar.type)
2913 {
2914 case AR_FULL:
2915 case AR_SECTION:
2916 current_part_dimension = 1;
2917 break;
2918
2919 case AR_ELEMENT:
2920 current_part_dimension = 0;
2921 break;
2922
2923 case AR_UNKNOWN:
2924 gfc_internal_error ("resolve_ref(): Bad array reference");
2925 }
2926
2927 break;
2928
2929 case REF_COMPONENT:
2930 if (current_part_dimension || seen_part_dimension)
2931 {
2932 if (ref->u.c.component->pointer)
2933 {
2934 gfc_error
2935 ("Component to the right of a part reference with nonzero "
2936 "rank must not have the POINTER attribute at %L",
2937 &expr->where);
2938 return FAILURE;
2939 }
2940 else if (ref->u.c.component->allocatable)
2941 {
2942 gfc_error
2943 ("Component to the right of a part reference with nonzero "
2944 "rank must not have the ALLOCATABLE attribute at %L",
2945 &expr->where);
2946 return FAILURE;
2947 }
2948 }
2949
2950 n_components++;
2951 break;
2952
2953 case REF_SUBSTRING:
2954 break;
2955 }
2956
2957 if (((ref->type == REF_COMPONENT && n_components > 1)
2958 || ref->next == NULL)
2959 && current_part_dimension
2960 && seen_part_dimension)
2961 {
2962
2963 gfc_error ("Two or more part references with nonzero rank must "
2964 "not be specified at %L", &expr->where);
2965 return FAILURE;
2966 }
2967
2968 if (ref->type == REF_COMPONENT)
2969 {
2970 if (current_part_dimension)
2971 seen_part_dimension = 1;
2972
2973 /* reset to make sure */
2974 current_part_dimension = 0;
2975 }
2976 }
2977
2978 return SUCCESS;
2979 }
2980
2981
2982 /* Given an expression, determine its shape. This is easier than it sounds.
2983 Leaves the shape array NULL if it is not possible to determine the shape. */
2984
2985 static void
2986 expression_shape (gfc_expr * e)
2987 {
2988 mpz_t array[GFC_MAX_DIMENSIONS];
2989 int i;
2990
2991 if (e->rank == 0 || e->shape != NULL)
2992 return;
2993
2994 for (i = 0; i < e->rank; i++)
2995 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2996 goto fail;
2997
2998 e->shape = gfc_get_shape (e->rank);
2999
3000 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
3001
3002 return;
3003
3004 fail:
3005 for (i--; i >= 0; i--)
3006 mpz_clear (array[i]);
3007 }
3008
3009
3010 /* Given a variable expression node, compute the rank of the expression by
3011 examining the base symbol and any reference structures it may have. */
3012
3013 static void
3014 expression_rank (gfc_expr * e)
3015 {
3016 gfc_ref *ref;
3017 int i, rank;
3018
3019 if (e->ref == NULL)
3020 {
3021 if (e->expr_type == EXPR_ARRAY)
3022 goto done;
3023 /* Constructors can have a rank different from one via RESHAPE(). */
3024
3025 if (e->symtree == NULL)
3026 {
3027 e->rank = 0;
3028 goto done;
3029 }
3030
3031 e->rank = (e->symtree->n.sym->as == NULL)
3032 ? 0 : e->symtree->n.sym->as->rank;
3033 goto done;
3034 }
3035
3036 rank = 0;
3037
3038 for (ref = e->ref; ref; ref = ref->next)
3039 {
3040 if (ref->type != REF_ARRAY)
3041 continue;
3042
3043 if (ref->u.ar.type == AR_FULL)
3044 {
3045 rank = ref->u.ar.as->rank;
3046 break;
3047 }
3048
3049 if (ref->u.ar.type == AR_SECTION)
3050 {
3051 /* Figure out the rank of the section. */
3052 if (rank != 0)
3053 gfc_internal_error ("expression_rank(): Two array specs");
3054
3055 for (i = 0; i < ref->u.ar.dimen; i++)
3056 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
3057 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
3058 rank++;
3059
3060 break;
3061 }
3062 }
3063
3064 e->rank = rank;
3065
3066 done:
3067 expression_shape (e);
3068 }
3069
3070
3071 /* Resolve a variable expression. */
3072
3073 static try
3074 resolve_variable (gfc_expr * e)
3075 {
3076 gfc_symbol *sym;
3077 try t;
3078
3079 t = SUCCESS;
3080
3081 if (e->symtree == NULL)
3082 return FAILURE;
3083
3084 if (e->ref && resolve_ref (e) == FAILURE)
3085 return FAILURE;
3086
3087 sym = e->symtree->n.sym;
3088 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
3089 {
3090 e->ts.type = BT_PROCEDURE;
3091 return SUCCESS;
3092 }
3093
3094 if (sym->ts.type != BT_UNKNOWN)
3095 gfc_variable_attr (e, &e->ts);
3096 else
3097 {
3098 /* Must be a simple variable reference. */
3099 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
3100 return FAILURE;
3101 e->ts = sym->ts;
3102 }
3103
3104 if (check_assumed_size_reference (sym, e))
3105 return FAILURE;
3106
3107 /* Deal with forward references to entries during resolve_code, to
3108 satisfy, at least partially, 12.5.2.5. */
3109 if (gfc_current_ns->entries
3110 && current_entry_id == sym->entry_id
3111 && cs_base
3112 && cs_base->current
3113 && cs_base->current->op != EXEC_ENTRY)
3114 {
3115 gfc_entry_list *entry;
3116 gfc_formal_arglist *formal;
3117 int n;
3118 bool seen;
3119
3120 /* If the symbol is a dummy... */
3121 if (sym->attr.dummy)
3122 {
3123 entry = gfc_current_ns->entries;
3124 seen = false;
3125
3126 /* ...test if the symbol is a parameter of previous entries. */
3127 for (; entry && entry->id <= current_entry_id; entry = entry->next)
3128 for (formal = entry->sym->formal; formal; formal = formal->next)
3129 {
3130 if (formal->sym && sym->name == formal->sym->name)
3131 seen = true;
3132 }
3133
3134 /* If it has not been seen as a dummy, this is an error. */
3135 if (!seen)
3136 {
3137 if (specification_expr)
3138 gfc_error ("Variable '%s',used in a specification expression, "
3139 "is referenced at %L before the ENTRY statement "
3140 "in which it is a parameter",
3141 sym->name, &cs_base->current->loc);
3142 else
3143 gfc_error ("Variable '%s' is used at %L before the ENTRY "
3144 "statement in which it is a parameter",
3145 sym->name, &cs_base->current->loc);
3146 t = FAILURE;
3147 }
3148 }
3149
3150 /* Now do the same check on the specification expressions. */
3151 specification_expr = 1;
3152 if (sym->ts.type == BT_CHARACTER
3153 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
3154 t = FAILURE;
3155
3156 if (sym->as)
3157 for (n = 0; n < sym->as->rank; n++)
3158 {
3159 specification_expr = 1;
3160 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
3161 t = FAILURE;
3162 specification_expr = 1;
3163 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
3164 t = FAILURE;
3165 }
3166 specification_expr = 0;
3167
3168 if (t == SUCCESS)
3169 /* Update the symbol's entry level. */
3170 sym->entry_id = current_entry_id + 1;
3171 }
3172
3173 return t;
3174 }
3175
3176
3177 /* Resolve an expression. That is, make sure that types of operands agree
3178 with their operators, intrinsic operators are converted to function calls
3179 for overloaded types and unresolved function references are resolved. */
3180
3181 try
3182 gfc_resolve_expr (gfc_expr * e)
3183 {
3184 try t;
3185
3186 if (e == NULL)
3187 return SUCCESS;
3188
3189 switch (e->expr_type)
3190 {
3191 case EXPR_OP:
3192 t = resolve_operator (e);
3193 break;
3194
3195 case EXPR_FUNCTION:
3196 t = resolve_function (e);
3197 break;
3198
3199 case EXPR_VARIABLE:
3200 t = resolve_variable (e);
3201 if (t == SUCCESS)
3202 expression_rank (e);
3203 break;
3204
3205 case EXPR_SUBSTRING:
3206 t = resolve_ref (e);
3207 break;
3208
3209 case EXPR_CONSTANT:
3210 case EXPR_NULL:
3211 t = SUCCESS;
3212 break;
3213
3214 case EXPR_ARRAY:
3215 t = FAILURE;
3216 if (resolve_ref (e) == FAILURE)
3217 break;
3218
3219 t = gfc_resolve_array_constructor (e);
3220 /* Also try to expand a constructor. */
3221 if (t == SUCCESS)
3222 {
3223 expression_rank (e);
3224 gfc_expand_constructor (e);
3225 }
3226
3227 /* This provides the opportunity for the length of constructors with character
3228 valued function elements to propogate the string length to the expression. */
3229 if (e->ts.type == BT_CHARACTER)
3230 gfc_resolve_character_array_constructor (e);
3231
3232 break;
3233
3234 case EXPR_STRUCTURE:
3235 t = resolve_ref (e);
3236 if (t == FAILURE)
3237 break;
3238
3239 t = resolve_structure_cons (e);
3240 if (t == FAILURE)
3241 break;
3242
3243 t = gfc_simplify_expr (e, 0);
3244 break;
3245
3246 default:
3247 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
3248 }
3249
3250 return t;
3251 }
3252
3253
3254 /* Resolve an expression from an iterator. They must be scalar and have
3255 INTEGER or (optionally) REAL type. */
3256
3257 static try
3258 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
3259 const char * name_msgid)
3260 {
3261 if (gfc_resolve_expr (expr) == FAILURE)
3262 return FAILURE;
3263
3264 if (expr->rank != 0)
3265 {
3266 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
3267 return FAILURE;
3268 }
3269
3270 if (!(expr->ts.type == BT_INTEGER
3271 || (expr->ts.type == BT_REAL && real_ok)))
3272 {
3273 if (real_ok)
3274 gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
3275 &expr->where);
3276 else
3277 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
3278 return FAILURE;
3279 }
3280 return SUCCESS;
3281 }
3282
3283
3284 /* Resolve the expressions in an iterator structure. If REAL_OK is
3285 false allow only INTEGER type iterators, otherwise allow REAL types. */
3286
3287 try
3288 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
3289 {
3290
3291 if (iter->var->ts.type == BT_REAL)
3292 gfc_notify_std (GFC_STD_F95_DEL,
3293 "Obsolete: REAL DO loop iterator at %L",
3294 &iter->var->where);
3295
3296 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
3297 == FAILURE)
3298 return FAILURE;
3299
3300 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
3301 {
3302 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
3303 &iter->var->where);
3304 return FAILURE;
3305 }
3306
3307 if (gfc_resolve_iterator_expr (iter->start, real_ok,
3308 "Start expression in DO loop") == FAILURE)
3309 return FAILURE;
3310
3311 if (gfc_resolve_iterator_expr (iter->end, real_ok,
3312 "End expression in DO loop") == FAILURE)
3313 return FAILURE;
3314
3315 if (gfc_resolve_iterator_expr (iter->step, real_ok,
3316 "Step expression in DO loop") == FAILURE)
3317 return FAILURE;
3318
3319 if (iter->step->expr_type == EXPR_CONSTANT)
3320 {
3321 if ((iter->step->ts.type == BT_INTEGER
3322 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
3323 || (iter->step->ts.type == BT_REAL
3324 && mpfr_sgn (iter->step->value.real) == 0))
3325 {
3326 gfc_error ("Step expression in DO loop at %L cannot be zero",
3327 &iter->step->where);
3328 return FAILURE;
3329 }
3330 }
3331
3332 /* Convert start, end, and step to the same type as var. */
3333 if (iter->start->ts.kind != iter->var->ts.kind
3334 || iter->start->ts.type != iter->var->ts.type)
3335 gfc_convert_type (iter->start, &iter->var->ts, 2);
3336
3337 if (iter->end->ts.kind != iter->var->ts.kind
3338 || iter->end->ts.type != iter->var->ts.type)
3339 gfc_convert_type (iter->end, &iter->var->ts, 2);
3340
3341 if (iter->step->ts.kind != iter->var->ts.kind
3342 || iter->step->ts.type != iter->var->ts.type)
3343 gfc_convert_type (iter->step, &iter->var->ts, 2);
3344
3345 return SUCCESS;
3346 }
3347
3348
3349 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
3350 to be a scalar INTEGER variable. The subscripts and stride are scalar
3351 INTEGERs, and if stride is a constant it must be nonzero. */
3352
3353 static void
3354 resolve_forall_iterators (gfc_forall_iterator * iter)
3355 {
3356
3357 while (iter)
3358 {
3359 if (gfc_resolve_expr (iter->var) == SUCCESS
3360 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
3361 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
3362 &iter->var->where);
3363
3364 if (gfc_resolve_expr (iter->start) == SUCCESS
3365 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
3366 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
3367 &iter->start->where);
3368 if (iter->var->ts.kind != iter->start->ts.kind)
3369 gfc_convert_type (iter->start, &iter->var->ts, 2);
3370
3371 if (gfc_resolve_expr (iter->end) == SUCCESS
3372 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
3373 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
3374 &iter->end->where);
3375 if (iter->var->ts.kind != iter->end->ts.kind)
3376 gfc_convert_type (iter->end, &iter->var->ts, 2);
3377
3378 if (gfc_resolve_expr (iter->stride) == SUCCESS)
3379 {
3380 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
3381 gfc_error ("FORALL stride expression at %L must be a scalar %s",
3382 &iter->stride->where, "INTEGER");
3383
3384 if (iter->stride->expr_type == EXPR_CONSTANT
3385 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
3386 gfc_error ("FORALL stride expression at %L cannot be zero",
3387 &iter->stride->where);
3388 }
3389 if (iter->var->ts.kind != iter->stride->ts.kind)
3390 gfc_convert_type (iter->stride, &iter->var->ts, 2);
3391
3392 iter = iter->next;
3393 }
3394 }
3395
3396
3397 /* Given a pointer to a symbol that is a derived type, see if any components
3398 have the POINTER attribute. The search is recursive if necessary.
3399 Returns zero if no pointer components are found, nonzero otherwise. */
3400
3401 static int
3402 derived_pointer (gfc_symbol * sym)
3403 {
3404 gfc_component *c;
3405
3406 for (c = sym->components; c; c = c->next)
3407 {
3408 if (c->pointer)
3409 return 1;
3410
3411 if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
3412 return 1;
3413 }
3414
3415 return 0;
3416 }
3417
3418
3419 /* Given a pointer to a symbol that is a derived type, see if it's
3420 inaccessible, i.e. if it's defined in another module and the components are
3421 PRIVATE. The search is recursive if necessary. Returns zero if no
3422 inaccessible components are found, nonzero otherwise. */
3423
3424 static int
3425 derived_inaccessible (gfc_symbol *sym)
3426 {
3427 gfc_component *c;
3428
3429 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
3430 return 1;
3431
3432 for (c = sym->components; c; c = c->next)
3433 {
3434 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
3435 return 1;
3436 }
3437
3438 return 0;
3439 }
3440
3441
3442 /* Resolve the argument of a deallocate expression. The expression must be
3443 a pointer or a full array. */
3444
3445 static try
3446 resolve_deallocate_expr (gfc_expr * e)
3447 {
3448 symbol_attribute attr;
3449 int allocatable, pointer, check_intent_in;
3450 gfc_ref *ref;
3451
3452 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
3453 check_intent_in = 1;
3454
3455 if (gfc_resolve_expr (e) == FAILURE)
3456 return FAILURE;
3457
3458 if (e->expr_type != EXPR_VARIABLE)
3459 goto bad;
3460
3461 allocatable = e->symtree->n.sym->attr.allocatable;
3462 pointer = e->symtree->n.sym->attr.pointer;
3463 for (ref = e->ref; ref; ref = ref->next)
3464 {
3465 if (pointer)
3466 check_intent_in = 0;
3467
3468 switch (ref->type)
3469 {
3470 case REF_ARRAY:
3471 if (ref->u.ar.type != AR_FULL)
3472 allocatable = 0;
3473 break;
3474
3475 case REF_COMPONENT:
3476 allocatable = (ref->u.c.component->as != NULL
3477 && ref->u.c.component->as->type == AS_DEFERRED);
3478 pointer = ref->u.c.component->pointer;
3479 break;
3480
3481 case REF_SUBSTRING:
3482 allocatable = 0;
3483 break;
3484 }
3485 }
3486
3487 attr = gfc_expr_attr (e);
3488
3489 if (allocatable == 0 && attr.pointer == 0)
3490 {
3491 bad:
3492 gfc_error ("Expression in DEALLOCATE statement at %L must be "
3493 "ALLOCATABLE or a POINTER", &e->where);
3494 }
3495
3496 if (check_intent_in
3497 && e->symtree->n.sym->attr.intent == INTENT_IN)
3498 {
3499 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
3500 e->symtree->n.sym->name, &e->where);
3501 return FAILURE;
3502 }
3503
3504 return SUCCESS;
3505 }
3506
3507 /* Returns true if the expression e contains a reference the symbol sym. */
3508 static bool
3509 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
3510 {
3511 gfc_actual_arglist *arg;
3512 gfc_ref *ref;
3513 int i;
3514 bool rv = false;
3515
3516 if (e == NULL)
3517 return rv;
3518
3519 switch (e->expr_type)
3520 {
3521 case EXPR_FUNCTION:
3522 for (arg = e->value.function.actual; arg; arg = arg->next)
3523 rv = rv || find_sym_in_expr (sym, arg->expr);
3524 break;
3525
3526 /* If the variable is not the same as the dependent, 'sym', and
3527 it is not marked as being declared and it is in the same
3528 namespace as 'sym', add it to the local declarations. */
3529 case EXPR_VARIABLE:
3530 if (sym == e->symtree->n.sym)
3531 return true;
3532 break;
3533
3534 case EXPR_OP:
3535 rv = rv || find_sym_in_expr (sym, e->value.op.op1);
3536 rv = rv || find_sym_in_expr (sym, e->value.op.op2);
3537 break;
3538
3539 default:
3540 break;
3541 }
3542
3543 if (e->ref)
3544 {
3545 for (ref = e->ref; ref; ref = ref->next)
3546 {
3547 switch (ref->type)
3548 {
3549 case REF_ARRAY:
3550 for (i = 0; i < ref->u.ar.dimen; i++)
3551 {
3552 rv = rv || find_sym_in_expr (sym, ref->u.ar.start[i]);
3553 rv = rv || find_sym_in_expr (sym, ref->u.ar.end[i]);
3554 rv = rv || find_sym_in_expr (sym, ref->u.ar.stride[i]);
3555 }
3556 break;
3557
3558 case REF_SUBSTRING:
3559 rv = rv || find_sym_in_expr (sym, ref->u.ss.start);
3560 rv = rv || find_sym_in_expr (sym, ref->u.ss.end);
3561 break;
3562
3563 case REF_COMPONENT:
3564 if (ref->u.c.component->ts.type == BT_CHARACTER
3565 && ref->u.c.component->ts.cl->length->expr_type
3566 != EXPR_CONSTANT)
3567 rv = rv || find_sym_in_expr (sym, ref->u.c.component->ts.cl->length);
3568
3569 if (ref->u.c.component->as)
3570 for (i = 0; i < ref->u.c.component->as->rank; i++)
3571 {
3572 rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->lower[i]);
3573 rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->upper[i]);
3574 }
3575 break;
3576 }
3577 }
3578 }
3579 return rv;
3580 }
3581
3582
3583 /* Given the expression node e for an allocatable/pointer of derived type to be
3584 allocated, get the expression node to be initialized afterwards (needed for
3585 derived types with default initializers, and derived types with allocatable
3586 components that need nullification.) */
3587
3588 static gfc_expr *
3589 expr_to_initialize (gfc_expr * e)
3590 {
3591 gfc_expr *result;
3592 gfc_ref *ref;
3593 int i;
3594
3595 result = gfc_copy_expr (e);
3596
3597 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
3598 for (ref = result->ref; ref; ref = ref->next)
3599 if (ref->type == REF_ARRAY && ref->next == NULL)
3600 {
3601 ref->u.ar.type = AR_FULL;
3602
3603 for (i = 0; i < ref->u.ar.dimen; i++)
3604 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
3605
3606 result->rank = ref->u.ar.dimen;
3607 break;
3608 }
3609
3610 return result;
3611 }
3612
3613
3614 /* Resolve the expression in an ALLOCATE statement, doing the additional
3615 checks to see whether the expression is OK or not. The expression must
3616 have a trailing array reference that gives the size of the array. */
3617
3618 static try
3619 resolve_allocate_expr (gfc_expr * e, gfc_code * code)
3620 {
3621 int i, pointer, allocatable, dimension, check_intent_in;
3622 symbol_attribute attr;
3623 gfc_ref *ref, *ref2;
3624 gfc_array_ref *ar;
3625 gfc_code *init_st;
3626 gfc_expr *init_e;
3627 gfc_symbol *sym;
3628 gfc_alloc *a;
3629
3630 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
3631 check_intent_in = 1;
3632
3633 if (gfc_resolve_expr (e) == FAILURE)
3634 return FAILURE;
3635
3636 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
3637 sym = code->expr->symtree->n.sym;
3638 else
3639 sym = NULL;
3640
3641 /* Make sure the expression is allocatable or a pointer. If it is
3642 pointer, the next-to-last reference must be a pointer. */
3643
3644 ref2 = NULL;
3645
3646 if (e->expr_type != EXPR_VARIABLE)
3647 {
3648 allocatable = 0;
3649
3650 attr = gfc_expr_attr (e);
3651 pointer = attr.pointer;
3652 dimension = attr.dimension;
3653
3654 }
3655 else
3656 {
3657 allocatable = e->symtree->n.sym->attr.allocatable;
3658 pointer = e->symtree->n.sym->attr.pointer;
3659 dimension = e->symtree->n.sym->attr.dimension;
3660
3661 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
3662 {
3663 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
3664 "not be allocated in the same statement at %L",
3665 sym->name, &e->where);
3666 return FAILURE;
3667 }
3668
3669 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
3670 {
3671 if (pointer)
3672 check_intent_in = 0;
3673
3674 switch (ref->type)
3675 {
3676 case REF_ARRAY:
3677 if (ref->next != NULL)
3678 pointer = 0;
3679 break;
3680
3681 case REF_COMPONENT:
3682 allocatable = (ref->u.c.component->as != NULL
3683 && ref->u.c.component->as->type == AS_DEFERRED);
3684
3685 pointer = ref->u.c.component->pointer;
3686 dimension = ref->u.c.component->dimension;
3687 break;
3688
3689 case REF_SUBSTRING:
3690 allocatable = 0;
3691 pointer = 0;
3692 break;
3693 }
3694 }
3695 }
3696
3697 if (allocatable == 0 && pointer == 0)
3698 {
3699 gfc_error ("Expression in ALLOCATE statement at %L must be "
3700 "ALLOCATABLE or a POINTER", &e->where);
3701 return FAILURE;
3702 }
3703
3704 if (check_intent_in
3705 && e->symtree->n.sym->attr.intent == INTENT_IN)
3706 {
3707 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
3708 e->symtree->n.sym->name, &e->where);
3709 return FAILURE;
3710 }
3711
3712 /* Add default initializer for those derived types that need them. */
3713 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
3714 {
3715 init_st = gfc_get_code ();
3716 init_st->loc = code->loc;
3717 init_st->op = EXEC_INIT_ASSIGN;
3718 init_st->expr = expr_to_initialize (e);
3719 init_st->expr2 = init_e;
3720 init_st->next = code->next;
3721 code->next = init_st;
3722 }
3723
3724 if (pointer && dimension == 0)
3725 return SUCCESS;
3726
3727 /* Make sure the next-to-last reference node is an array specification. */
3728
3729 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
3730 {
3731 gfc_error ("Array specification required in ALLOCATE statement "
3732 "at %L", &e->where);
3733 return FAILURE;
3734 }
3735
3736 /* Make sure that the array section reference makes sense in the
3737 context of an ALLOCATE specification. */
3738
3739 ar = &ref2->u.ar;
3740
3741 for (i = 0; i < ar->dimen; i++)
3742 {
3743 if (ref2->u.ar.type == AR_ELEMENT)
3744 goto check_symbols;
3745
3746 switch (ar->dimen_type[i])
3747 {
3748 case DIMEN_ELEMENT:
3749 break;
3750
3751 case DIMEN_RANGE:
3752 if (ar->start[i] != NULL
3753 && ar->end[i] != NULL
3754 && ar->stride[i] == NULL)
3755 break;
3756
3757 /* Fall Through... */
3758
3759 case DIMEN_UNKNOWN:
3760 case DIMEN_VECTOR:
3761 gfc_error ("Bad array specification in ALLOCATE statement at %L",
3762 &e->where);
3763 return FAILURE;
3764 }
3765
3766 check_symbols:
3767
3768 for (a = code->ext.alloc_list; a; a = a->next)
3769 {
3770 sym = a->expr->symtree->n.sym;
3771
3772 /* TODO - check derived type components. */
3773 if (sym->ts.type == BT_DERIVED)
3774 continue;
3775
3776 if ((ar->start[i] != NULL && find_sym_in_expr (sym, ar->start[i]))
3777 || (ar->end[i] != NULL && find_sym_in_expr (sym, ar->end[i])))
3778 {
3779 gfc_error ("'%s' must not appear an the array specification at "
3780 "%L in the same ALLOCATE statement where it is "
3781 "itself allocated", sym->name, &ar->where);
3782 return FAILURE;
3783 }
3784 }
3785 }
3786
3787 return SUCCESS;
3788 }
3789
3790
3791 /************ SELECT CASE resolution subroutines ************/
3792
3793 /* Callback function for our mergesort variant. Determines interval
3794 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
3795 op1 > op2. Assumes we're not dealing with the default case.
3796 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
3797 There are nine situations to check. */
3798
3799 static int
3800 compare_cases (const gfc_case * op1, const gfc_case * op2)
3801 {
3802 int retval;
3803
3804 if (op1->low == NULL) /* op1 = (:L) */
3805 {
3806 /* op2 = (:N), so overlap. */
3807 retval = 0;
3808 /* op2 = (M:) or (M:N), L < M */
3809 if (op2->low != NULL
3810 && gfc_compare_expr (op1->high, op2->low) < 0)
3811 retval = -1;
3812 }
3813 else if (op1->high == NULL) /* op1 = (K:) */
3814 {
3815 /* op2 = (M:), so overlap. */
3816 retval = 0;
3817 /* op2 = (:N) or (M:N), K > N */
3818 if (op2->high != NULL
3819 && gfc_compare_expr (op1->low, op2->high) > 0)
3820 retval = 1;
3821 }
3822 else /* op1 = (K:L) */
3823 {
3824 if (op2->low == NULL) /* op2 = (:N), K > N */
3825 retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
3826 else if (op2->high == NULL) /* op2 = (M:), L < M */
3827 retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
3828 else /* op2 = (M:N) */
3829 {
3830 retval = 0;
3831 /* L < M */
3832 if (gfc_compare_expr (op1->high, op2->low) < 0)
3833 retval = -1;
3834 /* K > N */
3835 else if (gfc_compare_expr (op1->low, op2->high) > 0)
3836 retval = 1;
3837 }
3838 }
3839
3840 return retval;
3841 }
3842
3843
3844 /* Merge-sort a double linked case list, detecting overlap in the
3845 process. LIST is the head of the double linked case list before it
3846 is sorted. Returns the head of the sorted list if we don't see any
3847 overlap, or NULL otherwise. */
3848
3849 static gfc_case *
3850 check_case_overlap (gfc_case * list)
3851 {
3852 gfc_case *p, *q, *e, *tail;
3853 int insize, nmerges, psize, qsize, cmp, overlap_seen;
3854
3855 /* If the passed list was empty, return immediately. */
3856 if (!list)
3857 return NULL;
3858
3859 overlap_seen = 0;
3860 insize = 1;
3861
3862 /* Loop unconditionally. The only exit from this loop is a return
3863 statement, when we've finished sorting the case list. */
3864 for (;;)
3865 {
3866 p = list;
3867 list = NULL;
3868 tail = NULL;
3869
3870 /* Count the number of merges we do in this pass. */
3871 nmerges = 0;
3872
3873 /* Loop while there exists a merge to be done. */
3874 while (p)
3875 {
3876 int i;
3877
3878 /* Count this merge. */
3879 nmerges++;
3880
3881 /* Cut the list in two pieces by stepping INSIZE places
3882 forward in the list, starting from P. */
3883 psize = 0;
3884 q = p;
3885 for (i = 0; i < insize; i++)
3886 {
3887 psize++;
3888 q = q->right;
3889 if (!q)
3890 break;
3891 }
3892 qsize = insize;
3893
3894 /* Now we have two lists. Merge them! */
3895 while (psize > 0 || (qsize > 0 && q != NULL))
3896 {
3897
3898 /* See from which the next case to merge comes from. */
3899 if (psize == 0)
3900 {
3901 /* P is empty so the next case must come from Q. */
3902 e = q;
3903 q = q->right;
3904 qsize--;
3905 }
3906 else if (qsize == 0 || q == NULL)
3907 {
3908 /* Q is empty. */
3909 e = p;
3910 p = p->right;
3911 psize--;
3912 }
3913 else
3914 {
3915 cmp = compare_cases (p, q);
3916 if (cmp < 0)
3917 {
3918 /* The whole case range for P is less than the
3919 one for Q. */
3920 e = p;
3921 p = p->right;
3922 psize--;
3923 }
3924 else if (cmp > 0)
3925 {
3926 /* The whole case range for Q is greater than
3927 the case range for P. */
3928 e = q;
3929 q = q->right;
3930 qsize--;
3931 }
3932 else
3933 {
3934 /* The cases overlap, or they are the same
3935 element in the list. Either way, we must
3936 issue an error and get the next case from P. */
3937 /* FIXME: Sort P and Q by line number. */
3938 gfc_error ("CASE label at %L overlaps with CASE "
3939 "label at %L", &p->where, &q->where);
3940 overlap_seen = 1;
3941 e = p;
3942 p = p->right;
3943 psize--;
3944 }
3945 }
3946
3947 /* Add the next element to the merged list. */
3948 if (tail)
3949 tail->right = e;
3950 else
3951 list = e;
3952 e->left = tail;
3953 tail = e;
3954 }
3955
3956 /* P has now stepped INSIZE places along, and so has Q. So
3957 they're the same. */
3958 p = q;
3959 }
3960 tail->right = NULL;
3961
3962 /* If we have done only one merge or none at all, we've
3963 finished sorting the cases. */
3964 if (nmerges <= 1)
3965 {
3966 if (!overlap_seen)
3967 return list;
3968 else
3969 return NULL;
3970 }
3971
3972 /* Otherwise repeat, merging lists twice the size. */
3973 insize *= 2;
3974 }
3975 }
3976
3977
3978 /* Check to see if an expression is suitable for use in a CASE statement.
3979 Makes sure that all case expressions are scalar constants of the same
3980 type. Return FAILURE if anything is wrong. */
3981
3982 static try
3983 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
3984 {
3985 if (e == NULL) return SUCCESS;
3986
3987 if (e->ts.type != case_expr->ts.type)
3988 {
3989 gfc_error ("Expression in CASE statement at %L must be of type %s",
3990 &e->where, gfc_basic_typename (case_expr->ts.type));
3991 return FAILURE;
3992 }
3993
3994 /* C805 (R808) For a given case-construct, each case-value shall be of
3995 the same type as case-expr. For character type, length differences
3996 are allowed, but the kind type parameters shall be the same. */
3997
3998 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
3999 {
4000 gfc_error("Expression in CASE statement at %L must be kind %d",
4001 &e->where, case_expr->ts.kind);
4002 return FAILURE;
4003 }
4004
4005 /* Convert the case value kind to that of case expression kind, if needed.
4006 FIXME: Should a warning be issued? */
4007 if (e->ts.kind != case_expr->ts.kind)
4008 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
4009
4010 if (e->rank != 0)
4011 {
4012 gfc_error ("Expression in CASE statement at %L must be scalar",
4013 &e->where);
4014 return FAILURE;
4015 }
4016
4017 return SUCCESS;
4018 }
4019
4020
4021 /* Given a completely parsed select statement, we:
4022
4023 - Validate all expressions and code within the SELECT.
4024 - Make sure that the selection expression is not of the wrong type.
4025 - Make sure that no case ranges overlap.
4026 - Eliminate unreachable cases and unreachable code resulting from
4027 removing case labels.
4028
4029 The standard does allow unreachable cases, e.g. CASE (5:3). But
4030 they are a hassle for code generation, and to prevent that, we just
4031 cut them out here. This is not necessary for overlapping cases
4032 because they are illegal and we never even try to generate code.
4033
4034 We have the additional caveat that a SELECT construct could have
4035 been a computed GOTO in the source code. Fortunately we can fairly
4036 easily work around that here: The case_expr for a "real" SELECT CASE
4037 is in code->expr1, but for a computed GOTO it is in code->expr2. All
4038 we have to do is make sure that the case_expr is a scalar integer
4039 expression. */
4040
4041 static void
4042 resolve_select (gfc_code * code)
4043 {
4044 gfc_code *body;
4045 gfc_expr *case_expr;
4046 gfc_case *cp, *default_case, *tail, *head;
4047 int seen_unreachable;
4048 int seen_logical;
4049 int ncases;
4050 bt type;
4051 try t;
4052
4053 if (code->expr == NULL)
4054 {
4055 /* This was actually a computed GOTO statement. */
4056 case_expr = code->expr2;
4057 if (case_expr->ts.type != BT_INTEGER
4058 || case_expr->rank != 0)
4059 gfc_error ("Selection expression in computed GOTO statement "
4060 "at %L must be a scalar integer expression",
4061 &case_expr->where);
4062
4063 /* Further checking is not necessary because this SELECT was built
4064 by the compiler, so it should always be OK. Just move the
4065 case_expr from expr2 to expr so that we can handle computed
4066 GOTOs as normal SELECTs from here on. */
4067 code->expr = code->expr2;
4068 code->expr2 = NULL;
4069 return;
4070 }
4071
4072 case_expr = code->expr;
4073
4074 type = case_expr->ts.type;
4075 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
4076 {
4077 gfc_error ("Argument of SELECT statement at %L cannot be %s",
4078 &case_expr->where, gfc_typename (&case_expr->ts));
4079
4080 /* Punt. Going on here just produce more garbage error messages. */
4081 return;
4082 }
4083
4084 if (case_expr->rank != 0)
4085 {
4086 gfc_error ("Argument of SELECT statement at %L must be a scalar "
4087 "expression", &case_expr->where);
4088
4089 /* Punt. */
4090 return;
4091 }
4092
4093 /* PR 19168 has a long discussion concerning a mismatch of the kinds
4094 of the SELECT CASE expression and its CASE values. Walk the lists
4095 of case values, and if we find a mismatch, promote case_expr to
4096 the appropriate kind. */
4097
4098 if (type == BT_LOGICAL || type == BT_INTEGER)
4099 {
4100 for (body = code->block; body; body = body->block)
4101 {
4102 /* Walk the case label list. */
4103 for (cp = body->ext.case_list; cp; cp = cp->next)
4104 {
4105 /* Intercept the DEFAULT case. It does not have a kind. */
4106 if (cp->low == NULL && cp->high == NULL)
4107 continue;
4108
4109 /* Unreachable case ranges are discarded, so ignore. */
4110 if (cp->low != NULL && cp->high != NULL
4111 && cp->low != cp->high
4112 && gfc_compare_expr (cp->low, cp->high) > 0)
4113 continue;
4114
4115 /* FIXME: Should a warning be issued? */
4116 if (cp->low != NULL
4117 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
4118 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
4119
4120 if (cp->high != NULL
4121 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
4122 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
4123 }
4124 }
4125 }
4126
4127 /* Assume there is no DEFAULT case. */
4128 default_case = NULL;
4129 head = tail = NULL;
4130 ncases = 0;
4131 seen_logical = 0;
4132
4133 for (body = code->block; body; body = body->block)
4134 {
4135 /* Assume the CASE list is OK, and all CASE labels can be matched. */
4136 t = SUCCESS;
4137 seen_unreachable = 0;
4138
4139 /* Walk the case label list, making sure that all case labels
4140 are legal. */
4141 for (cp = body->ext.case_list; cp; cp = cp->next)
4142 {
4143 /* Count the number of cases in the whole construct. */
4144 ncases++;
4145
4146 /* Intercept the DEFAULT case. */
4147 if (cp->low == NULL && cp->high == NULL)
4148 {
4149 if (default_case != NULL)
4150 {
4151 gfc_error ("The DEFAULT CASE at %L cannot be followed "
4152 "by a second DEFAULT CASE at %L",
4153 &default_case->where, &cp->where);
4154 t = FAILURE;
4155 break;
4156 }
4157 else
4158 {
4159 default_case = cp;
4160 continue;
4161 }
4162 }
4163
4164 /* Deal with single value cases and case ranges. Errors are
4165 issued from the validation function. */
4166 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
4167 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
4168 {
4169 t = FAILURE;
4170 break;
4171 }
4172
4173 if (type == BT_LOGICAL
4174 && ((cp->low == NULL || cp->high == NULL)
4175 || cp->low != cp->high))
4176 {
4177 gfc_error
4178 ("Logical range in CASE statement at %L is not allowed",
4179 &cp->low->where);
4180 t = FAILURE;
4181 break;
4182 }
4183
4184 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
4185 {
4186 int value;
4187 value = cp->low->value.logical == 0 ? 2 : 1;
4188 if (value & seen_logical)
4189 {
4190 gfc_error ("constant logical value in CASE statement "
4191 "is repeated at %L",
4192 &cp->low->where);
4193 t = FAILURE;
4194 break;
4195 }
4196 seen_logical |= value;
4197 }
4198
4199 if (cp->low != NULL && cp->high != NULL
4200 && cp->low != cp->high
4201 && gfc_compare_expr (cp->low, cp->high) > 0)
4202 {
4203 if (gfc_option.warn_surprising)
4204 gfc_warning ("Range specification at %L can never "
4205 "be matched", &cp->where);
4206
4207 cp->unreachable = 1;
4208 seen_unreachable = 1;
4209 }
4210 else
4211 {
4212 /* If the case range can be matched, it can also overlap with
4213 other cases. To make sure it does not, we put it in a
4214 double linked list here. We sort that with a merge sort
4215 later on to detect any overlapping cases. */
4216 if (!head)
4217 {
4218 head = tail = cp;
4219 head->right = head->left = NULL;
4220 }
4221 else
4222 {
4223 tail->right = cp;
4224 tail->right->left = tail;
4225 tail = tail->right;
4226 tail->right = NULL;
4227 }
4228 }
4229 }
4230
4231 /* It there was a failure in the previous case label, give up
4232 for this case label list. Continue with the next block. */
4233 if (t == FAILURE)
4234 continue;
4235
4236 /* See if any case labels that are unreachable have been seen.
4237 If so, we eliminate them. This is a bit of a kludge because
4238 the case lists for a single case statement (label) is a
4239 single forward linked lists. */
4240 if (seen_unreachable)
4241 {
4242 /* Advance until the first case in the list is reachable. */
4243 while (body->ext.case_list != NULL
4244 && body->ext.case_list->unreachable)
4245 {
4246 gfc_case *n = body->ext.case_list;
4247 body->ext.case_list = body->ext.case_list->next;
4248 n->next = NULL;
4249 gfc_free_case_list (n);
4250 }
4251
4252 /* Strip all other unreachable cases. */
4253 if (body->ext.case_list)
4254 {
4255 for (cp = body->ext.case_list; cp->next; cp = cp->next)
4256 {
4257 if (cp->next->unreachable)
4258 {
4259 gfc_case *n = cp->next;
4260 cp->next = cp->next->next;
4261 n->next = NULL;
4262 gfc_free_case_list (n);
4263 }
4264 }
4265 }
4266 }
4267 }
4268
4269 /* See if there were overlapping cases. If the check returns NULL,
4270 there was overlap. In that case we don't do anything. If head
4271 is non-NULL, we prepend the DEFAULT case. The sorted list can
4272 then used during code generation for SELECT CASE constructs with
4273 a case expression of a CHARACTER type. */
4274 if (head)
4275 {
4276 head = check_case_overlap (head);
4277
4278 /* Prepend the default_case if it is there. */
4279 if (head != NULL && default_case)
4280 {
4281 default_case->left = NULL;
4282 default_case->right = head;
4283 head->left = default_case;
4284 }
4285 }
4286
4287 /* Eliminate dead blocks that may be the result if we've seen
4288 unreachable case labels for a block. */
4289 for (body = code; body && body->block; body = body->block)
4290 {
4291 if (body->block->ext.case_list == NULL)
4292 {
4293 /* Cut the unreachable block from the code chain. */
4294 gfc_code *c = body->block;
4295 body->block = c->block;
4296
4297 /* Kill the dead block, but not the blocks below it. */
4298 c->block = NULL;
4299 gfc_free_statements (c);
4300 }
4301 }
4302
4303 /* More than two cases is legal but insane for logical selects.
4304 Issue a warning for it. */
4305 if (gfc_option.warn_surprising && type == BT_LOGICAL
4306 && ncases > 2)
4307 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
4308 &code->loc);
4309 }
4310
4311
4312 /* Resolve a transfer statement. This is making sure that:
4313 -- a derived type being transferred has only non-pointer components
4314 -- a derived type being transferred doesn't have private components, unless
4315 it's being transferred from the module where the type was defined
4316 -- we're not trying to transfer a whole assumed size array. */
4317
4318 static void
4319 resolve_transfer (gfc_code * code)
4320 {
4321 gfc_typespec *ts;
4322 gfc_symbol *sym;
4323 gfc_ref *ref;
4324 gfc_expr *exp;
4325
4326 exp = code->expr;
4327
4328 if (exp->expr_type != EXPR_VARIABLE
4329 && exp->expr_type != EXPR_FUNCTION)
4330 return;
4331
4332 sym = exp->symtree->n.sym;
4333 ts = &sym->ts;
4334
4335 /* Go to actual component transferred. */
4336 for (ref = code->expr->ref; ref; ref = ref->next)
4337 if (ref->type == REF_COMPONENT)
4338 ts = &ref->u.c.component->ts;
4339
4340 if (ts->type == BT_DERIVED)
4341 {
4342 /* Check that transferred derived type doesn't contain POINTER
4343 components. */
4344 if (derived_pointer (ts->derived))
4345 {
4346 gfc_error ("Data transfer element at %L cannot have "
4347 "POINTER components", &code->loc);
4348 return;
4349 }
4350
4351 if (ts->derived->attr.alloc_comp)
4352 {
4353 gfc_error ("Data transfer element at %L cannot have "
4354 "ALLOCATABLE components", &code->loc);
4355 return;
4356 }
4357
4358 if (derived_inaccessible (ts->derived))
4359 {
4360 gfc_error ("Data transfer element at %L cannot have "
4361 "PRIVATE components",&code->loc);
4362 return;
4363 }
4364 }
4365
4366 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
4367 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
4368 {
4369 gfc_error ("Data transfer element at %L cannot be a full reference to "
4370 "an assumed-size array", &code->loc);
4371 return;
4372 }
4373 }
4374
4375
4376 /*********** Toplevel code resolution subroutines ***********/
4377
4378 /* Given a branch to a label and a namespace, if the branch is conforming.
4379 The code node described where the branch is located. */
4380
4381 static void
4382 resolve_branch (gfc_st_label * label, gfc_code * code)
4383 {
4384 gfc_code *block, *found;
4385 code_stack *stack;
4386 gfc_st_label *lp;
4387
4388 if (label == NULL)
4389 return;
4390 lp = label;
4391
4392 /* Step one: is this a valid branching target? */
4393
4394 if (lp->defined == ST_LABEL_UNKNOWN)
4395 {
4396 gfc_error ("Label %d referenced at %L is never defined", lp->value,
4397 &lp->where);
4398 return;
4399 }
4400
4401 if (lp->defined != ST_LABEL_TARGET)
4402 {
4403 gfc_error ("Statement at %L is not a valid branch target statement "
4404 "for the branch statement at %L", &lp->where, &code->loc);
4405 return;
4406 }
4407
4408 /* Step two: make sure this branch is not a branch to itself ;-) */
4409
4410 if (code->here == label)
4411 {
4412 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
4413 return;
4414 }
4415
4416 /* Step three: Try to find the label in the parse tree. To do this,
4417 we traverse the tree block-by-block: first the block that
4418 contains this GOTO, then the block that it is nested in, etc. We
4419 can ignore other blocks because branching into another block is
4420 not allowed. */
4421
4422 found = NULL;
4423
4424 for (stack = cs_base; stack; stack = stack->prev)
4425 {
4426 for (block = stack->head; block; block = block->next)
4427 {
4428 if (block->here == label)
4429 {
4430 found = block;
4431 break;
4432 }
4433 }
4434
4435 if (found)
4436 break;
4437 }
4438
4439 if (found == NULL)
4440 {
4441 /* The label is not in an enclosing block, so illegal. This was
4442 allowed in Fortran 66, so we allow it as extension. We also
4443 forego further checks if we run into this. */
4444 gfc_notify_std (GFC_STD_LEGACY,
4445 "Label at %L is not in the same block as the "
4446 "GOTO statement at %L", &lp->where, &code->loc);
4447 return;
4448 }
4449
4450 /* Step four: Make sure that the branching target is legal if
4451 the statement is an END {SELECT,DO,IF}. */
4452
4453 if (found->op == EXEC_NOP)
4454 {
4455 for (stack = cs_base; stack; stack = stack->prev)
4456 if (stack->current->next == found)
4457 break;
4458
4459 if (stack == NULL)
4460 gfc_notify_std (GFC_STD_F95_DEL,
4461 "Obsolete: GOTO at %L jumps to END of construct at %L",
4462 &code->loc, &found->loc);
4463 }
4464 }
4465
4466
4467 /* Check whether EXPR1 has the same shape as EXPR2. */
4468
4469 static try
4470 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
4471 {
4472 mpz_t shape[GFC_MAX_DIMENSIONS];
4473 mpz_t shape2[GFC_MAX_DIMENSIONS];
4474 try result = FAILURE;
4475 int i;
4476
4477 /* Compare the rank. */
4478 if (expr1->rank != expr2->rank)
4479 return result;
4480
4481 /* Compare the size of each dimension. */
4482 for (i=0; i<expr1->rank; i++)
4483 {
4484 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
4485 goto ignore;
4486
4487 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
4488 goto ignore;
4489
4490 if (mpz_cmp (shape[i], shape2[i]))
4491 goto over;
4492 }
4493
4494 /* When either of the two expression is an assumed size array, we
4495 ignore the comparison of dimension sizes. */
4496 ignore:
4497 result = SUCCESS;
4498
4499 over:
4500 for (i--; i>=0; i--)
4501 {
4502 mpz_clear (shape[i]);
4503 mpz_clear (shape2[i]);
4504 }
4505 return result;
4506 }
4507
4508
4509 /* Check whether a WHERE assignment target or a WHERE mask expression
4510 has the same shape as the outmost WHERE mask expression. */
4511
4512 static void
4513 resolve_where (gfc_code *code, gfc_expr *mask)
4514 {
4515 gfc_code *cblock;
4516 gfc_code *cnext;
4517 gfc_expr *e = NULL;
4518
4519 cblock = code->block;
4520
4521 /* Store the first WHERE mask-expr of the WHERE statement or construct.
4522 In case of nested WHERE, only the outmost one is stored. */
4523 if (mask == NULL) /* outmost WHERE */
4524 e = cblock->expr;
4525 else /* inner WHERE */
4526 e = mask;
4527
4528 while (cblock)
4529 {
4530 if (cblock->expr)
4531 {
4532 /* Check if the mask-expr has a consistent shape with the
4533 outmost WHERE mask-expr. */
4534 if (resolve_where_shape (cblock->expr, e) == FAILURE)
4535 gfc_error ("WHERE mask at %L has inconsistent shape",
4536 &cblock->expr->where);
4537 }
4538
4539 /* the assignment statement of a WHERE statement, or the first
4540 statement in where-body-construct of a WHERE construct */
4541 cnext = cblock->next;
4542 while (cnext)
4543 {
4544 switch (cnext->op)
4545 {
4546 /* WHERE assignment statement */
4547 case EXEC_ASSIGN:
4548
4549 /* Check shape consistent for WHERE assignment target. */
4550 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
4551 gfc_error ("WHERE assignment target at %L has "
4552 "inconsistent shape", &cnext->expr->where);
4553 break;
4554
4555 /* WHERE or WHERE construct is part of a where-body-construct */
4556 case EXEC_WHERE:
4557 resolve_where (cnext, e);
4558 break;
4559
4560 default:
4561 gfc_error ("Unsupported statement inside WHERE at %L",
4562 &cnext->loc);
4563 }
4564 /* the next statement within the same where-body-construct */
4565 cnext = cnext->next;
4566 }
4567 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
4568 cblock = cblock->block;
4569 }
4570 }
4571
4572
4573 /* Check whether the FORALL index appears in the expression or not. */
4574
4575 static try
4576 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
4577 {
4578 gfc_array_ref ar;
4579 gfc_ref *tmp;
4580 gfc_actual_arglist *args;
4581 int i;
4582
4583 switch (expr->expr_type)
4584 {
4585 case EXPR_VARIABLE:
4586 gcc_assert (expr->symtree->n.sym);
4587
4588 /* A scalar assignment */
4589 if (!expr->ref)
4590 {
4591 if (expr->symtree->n.sym == symbol)
4592 return SUCCESS;
4593 else
4594 return FAILURE;
4595 }
4596
4597 /* the expr is array ref, substring or struct component. */
4598 tmp = expr->ref;
4599 while (tmp != NULL)
4600 {
4601 switch (tmp->type)
4602 {
4603 case REF_ARRAY:
4604 /* Check if the symbol appears in the array subscript. */
4605 ar = tmp->u.ar;
4606 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
4607 {
4608 if (ar.start[i])
4609 if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
4610 return SUCCESS;
4611
4612 if (ar.end[i])
4613 if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
4614 return SUCCESS;
4615
4616 if (ar.stride[i])
4617 if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
4618 return SUCCESS;
4619 } /* end for */
4620 break;
4621
4622 case REF_SUBSTRING:
4623 if (expr->symtree->n.sym == symbol)
4624 return SUCCESS;
4625 tmp = expr->ref;
4626 /* Check if the symbol appears in the substring section. */
4627 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
4628 return SUCCESS;
4629 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
4630 return SUCCESS;
4631 break;
4632
4633 case REF_COMPONENT:
4634 break;
4635
4636 default:
4637 gfc_error("expression reference type error at %L", &expr->where);
4638 }
4639 tmp = tmp->next;
4640 }
4641 break;
4642
4643 /* If the expression is a function call, then check if the symbol
4644 appears in the actual arglist of the function. */
4645 case EXPR_FUNCTION:
4646 for (args = expr->value.function.actual; args; args = args->next)
4647 {
4648 if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
4649 return SUCCESS;
4650 }
4651 break;
4652
4653 /* It seems not to happen. */
4654 case EXPR_SUBSTRING:
4655 if (expr->ref)
4656 {
4657 tmp = expr->ref;
4658 gcc_assert (expr->ref->type == REF_SUBSTRING);
4659 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
4660 return SUCCESS;
4661 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
4662 return SUCCESS;
4663 }
4664 break;
4665
4666 /* It seems not to happen. */
4667 case EXPR_STRUCTURE:
4668 case EXPR_ARRAY:
4669 gfc_error ("Unsupported statement while finding forall index in "
4670 "expression");
4671 break;
4672
4673 case EXPR_OP:
4674 /* Find the FORALL index in the first operand. */
4675 if (expr->value.op.op1)
4676 {
4677 if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
4678 return SUCCESS;
4679 }
4680
4681 /* Find the FORALL index in the second operand. */
4682 if (expr->value.op.op2)
4683 {
4684 if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
4685 return SUCCESS;
4686 }
4687 break;
4688
4689 default:
4690 break;
4691 }
4692
4693 return FAILURE;
4694 }
4695
4696
4697 /* Resolve assignment in FORALL construct.
4698 NVAR is the number of FORALL index variables, and VAR_EXPR records the
4699 FORALL index variables. */
4700
4701 static void
4702 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
4703 {
4704 int n;
4705
4706 for (n = 0; n < nvar; n++)
4707 {
4708 gfc_symbol *forall_index;
4709
4710 forall_index = var_expr[n]->symtree->n.sym;
4711
4712 /* Check whether the assignment target is one of the FORALL index
4713 variable. */
4714 if ((code->expr->expr_type == EXPR_VARIABLE)
4715 && (code->expr->symtree->n.sym == forall_index))
4716 gfc_error ("Assignment to a FORALL index variable at %L",
4717 &code->expr->where);
4718 else
4719 {
4720 /* If one of the FORALL index variables doesn't appear in the
4721 assignment target, then there will be a many-to-one
4722 assignment. */
4723 if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
4724 gfc_error ("The FORALL with index '%s' cause more than one "
4725 "assignment to this object at %L",
4726 var_expr[n]->symtree->name, &code->expr->where);
4727 }
4728 }
4729 }
4730
4731
4732 /* Resolve WHERE statement in FORALL construct. */
4733
4734 static void
4735 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
4736 gfc_code *cblock;
4737 gfc_code *cnext;
4738
4739 cblock = code->block;
4740 while (cblock)
4741 {
4742 /* the assignment statement of a WHERE statement, or the first
4743 statement in where-body-construct of a WHERE construct */
4744 cnext = cblock->next;
4745 while (cnext)
4746 {
4747 switch (cnext->op)
4748 {
4749 /* WHERE assignment statement */
4750 case EXEC_ASSIGN:
4751 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
4752 break;
4753
4754 /* WHERE or WHERE construct is part of a where-body-construct */
4755 case EXEC_WHERE:
4756 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
4757 break;
4758
4759 default:
4760 gfc_error ("Unsupported statement inside WHERE at %L",
4761 &cnext->loc);
4762 }
4763 /* the next statement within the same where-body-construct */
4764 cnext = cnext->next;
4765 }
4766 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
4767 cblock = cblock->block;
4768 }
4769 }
4770
4771
4772 /* Traverse the FORALL body to check whether the following errors exist:
4773 1. For assignment, check if a many-to-one assignment happens.
4774 2. For WHERE statement, check the WHERE body to see if there is any
4775 many-to-one assignment. */
4776
4777 static void
4778 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
4779 {
4780 gfc_code *c;
4781
4782 c = code->block->next;
4783 while (c)
4784 {
4785 switch (c->op)
4786 {
4787 case EXEC_ASSIGN:
4788 case EXEC_POINTER_ASSIGN:
4789 gfc_resolve_assign_in_forall (c, nvar, var_expr);
4790 break;
4791
4792 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
4793 there is no need to handle it here. */
4794 case EXEC_FORALL:
4795 break;
4796 case EXEC_WHERE:
4797 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
4798 break;
4799 default:
4800 break;
4801 }
4802 /* The next statement in the FORALL body. */
4803 c = c->next;
4804 }
4805 }
4806
4807
4808 /* Given a FORALL construct, first resolve the FORALL iterator, then call
4809 gfc_resolve_forall_body to resolve the FORALL body. */
4810
4811 static void
4812 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
4813 {
4814 static gfc_expr **var_expr;
4815 static int total_var = 0;
4816 static int nvar = 0;
4817 gfc_forall_iterator *fa;
4818 gfc_symbol *forall_index;
4819 gfc_code *next;
4820 int i;
4821
4822 /* Start to resolve a FORALL construct */
4823 if (forall_save == 0)
4824 {
4825 /* Count the total number of FORALL index in the nested FORALL
4826 construct in order to allocate the VAR_EXPR with proper size. */
4827 next = code;
4828 while ((next != NULL) && (next->op == EXEC_FORALL))
4829 {
4830 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
4831 total_var ++;
4832 next = next->block->next;
4833 }
4834
4835 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
4836 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
4837 }
4838
4839 /* The information about FORALL iterator, including FORALL index start, end
4840 and stride. The FORALL index can not appear in start, end or stride. */
4841 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
4842 {
4843 /* Check if any outer FORALL index name is the same as the current
4844 one. */
4845 for (i = 0; i < nvar; i++)
4846 {
4847 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
4848 {
4849 gfc_error ("An outer FORALL construct already has an index "
4850 "with this name %L", &fa->var->where);
4851 }
4852 }
4853
4854 /* Record the current FORALL index. */
4855 var_expr[nvar] = gfc_copy_expr (fa->var);
4856
4857 forall_index = fa->var->symtree->n.sym;
4858
4859 /* Check if the FORALL index appears in start, end or stride. */
4860 if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
4861 gfc_error ("A FORALL index must not appear in a limit or stride "
4862 "expression in the same FORALL at %L", &fa->start->where);
4863 if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
4864 gfc_error ("A FORALL index must not appear in a limit or stride "
4865 "expression in the same FORALL at %L", &fa->end->where);
4866 if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
4867 gfc_error ("A FORALL index must not appear in a limit or stride "
4868 "expression in the same FORALL at %L", &fa->stride->where);
4869 nvar++;
4870 }
4871
4872 /* Resolve the FORALL body. */
4873 gfc_resolve_forall_body (code, nvar, var_expr);
4874
4875 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
4876 gfc_resolve_blocks (code->block, ns);
4877
4878 /* Free VAR_EXPR after the whole FORALL construct resolved. */
4879 for (i = 0; i < total_var; i++)
4880 gfc_free_expr (var_expr[i]);
4881
4882 /* Reset the counters. */
4883 total_var = 0;
4884 nvar = 0;
4885 }
4886
4887
4888 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
4889 DO code nodes. */
4890
4891 static void resolve_code (gfc_code *, gfc_namespace *);
4892
4893 void
4894 gfc_resolve_blocks (gfc_code * b, gfc_namespace * ns)
4895 {
4896 try t;
4897
4898 for (; b; b = b->block)
4899 {
4900 t = gfc_resolve_expr (b->expr);
4901 if (gfc_resolve_expr (b->expr2) == FAILURE)
4902 t = FAILURE;
4903
4904 switch (b->op)
4905 {
4906 case EXEC_IF:
4907 if (t == SUCCESS && b->expr != NULL
4908 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
4909 gfc_error
4910 ("IF clause at %L requires a scalar LOGICAL expression",
4911 &b->expr->where);
4912 break;
4913
4914 case EXEC_WHERE:
4915 if (t == SUCCESS
4916 && b->expr != NULL
4917 && (b->expr->ts.type != BT_LOGICAL
4918 || b->expr->rank == 0))
4919 gfc_error
4920 ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
4921 &b->expr->where);
4922 break;
4923
4924 case EXEC_GOTO:
4925 resolve_branch (b->label, b);
4926 break;
4927
4928 case EXEC_SELECT:
4929 case EXEC_FORALL:
4930 case EXEC_DO:
4931 case EXEC_DO_WHILE:
4932 case EXEC_READ:
4933 case EXEC_WRITE:
4934 case EXEC_IOLENGTH:
4935 break;
4936
4937 case EXEC_OMP_ATOMIC:
4938 case EXEC_OMP_CRITICAL:
4939 case EXEC_OMP_DO:
4940 case EXEC_OMP_MASTER:
4941 case EXEC_OMP_ORDERED:
4942 case EXEC_OMP_PARALLEL:
4943 case EXEC_OMP_PARALLEL_DO:
4944 case EXEC_OMP_PARALLEL_SECTIONS:
4945 case EXEC_OMP_PARALLEL_WORKSHARE:
4946 case EXEC_OMP_SECTIONS:
4947 case EXEC_OMP_SINGLE:
4948 case EXEC_OMP_WORKSHARE:
4949 break;
4950
4951 default:
4952 gfc_internal_error ("resolve_block(): Bad block type");
4953 }
4954
4955 resolve_code (b->next, ns);
4956 }
4957 }
4958
4959
4960 /* Given a block of code, recursively resolve everything pointed to by this
4961 code block. */
4962
4963 static void
4964 resolve_code (gfc_code * code, gfc_namespace * ns)
4965 {
4966 int omp_workshare_save;
4967 int forall_save;
4968 code_stack frame;
4969 gfc_alloc *a;
4970 try t;
4971
4972 frame.prev = cs_base;
4973 frame.head = code;
4974 cs_base = &frame;
4975
4976 for (; code; code = code->next)
4977 {
4978 frame.current = code;
4979 forall_save = forall_flag;
4980
4981 if (code->op == EXEC_FORALL)
4982 {
4983 forall_flag = 1;
4984 gfc_resolve_forall (code, ns, forall_save);
4985 forall_flag = 2;
4986 }
4987 else if (code->block)
4988 {
4989 omp_workshare_save = -1;
4990 switch (code->op)
4991 {
4992 case EXEC_OMP_PARALLEL_WORKSHARE:
4993 omp_workshare_save = omp_workshare_flag;
4994 omp_workshare_flag = 1;
4995 gfc_resolve_omp_parallel_blocks (code, ns);
4996 break;
4997 case EXEC_OMP_PARALLEL:
4998 case EXEC_OMP_PARALLEL_DO:
4999 case EXEC_OMP_PARALLEL_SECTIONS:
5000 omp_workshare_save = omp_workshare_flag;
5001 omp_workshare_flag = 0;
5002 gfc_resolve_omp_parallel_blocks (code, ns);
5003 break;
5004 case EXEC_OMP_DO:
5005 gfc_resolve_omp_do_blocks (code, ns);
5006 break;
5007 case EXEC_OMP_WORKSHARE:
5008 omp_workshare_save = omp_workshare_flag;
5009 omp_workshare_flag = 1;
5010 /* FALLTHROUGH */
5011 default:
5012 gfc_resolve_blocks (code->block, ns);
5013 break;
5014 }
5015
5016 if (omp_workshare_save != -1)
5017 omp_workshare_flag = omp_workshare_save;
5018 }
5019
5020 t = gfc_resolve_expr (code->expr);
5021 forall_flag = forall_save;
5022
5023 if (gfc_resolve_expr (code->expr2) == FAILURE)
5024 t = FAILURE;
5025
5026 switch (code->op)
5027 {
5028 case EXEC_NOP:
5029 case EXEC_CYCLE:
5030 case EXEC_PAUSE:
5031 case EXEC_STOP:
5032 case EXEC_EXIT:
5033 case EXEC_CONTINUE:
5034 case EXEC_DT_END:
5035 break;
5036
5037 case EXEC_ENTRY:
5038 /* Keep track of which entry we are up to. */
5039 current_entry_id = code->ext.entry->id;
5040 break;
5041
5042 case EXEC_WHERE:
5043 resolve_where (code, NULL);
5044 break;
5045
5046 case EXEC_GOTO:
5047 if (code->expr != NULL)
5048 {
5049 if (code->expr->ts.type != BT_INTEGER)
5050 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
5051 "variable", &code->expr->where);
5052 else if (code->expr->symtree->n.sym->attr.assign != 1)
5053 gfc_error ("Variable '%s' has not been assigned a target label "
5054 "at %L", code->expr->symtree->n.sym->name,
5055 &code->expr->where);
5056 }
5057 else
5058 resolve_branch (code->label, code);
5059 break;
5060
5061 case EXEC_RETURN:
5062 if (code->expr != NULL
5063 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
5064 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
5065 "INTEGER return specifier", &code->expr->where);
5066 break;
5067
5068 case EXEC_INIT_ASSIGN:
5069 break;
5070
5071 case EXEC_ASSIGN:
5072 if (t == FAILURE)
5073 break;
5074
5075 if (gfc_extend_assign (code, ns) == SUCCESS)
5076 {
5077 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
5078 {
5079 gfc_error ("Subroutine '%s' called instead of assignment at "
5080 "%L must be PURE", code->symtree->n.sym->name,
5081 &code->loc);
5082 break;
5083 }
5084 goto call;
5085 }
5086
5087 if (code->expr->ts.type == BT_CHARACTER
5088 && gfc_option.warn_character_truncation)
5089 {
5090 int llen = 0, rlen = 0;
5091
5092 if (code->expr->ts.cl != NULL
5093 && code->expr->ts.cl->length != NULL
5094 && code->expr->ts.cl->length->expr_type == EXPR_CONSTANT)
5095 llen = mpz_get_si (code->expr->ts.cl->length->value.integer);
5096
5097 if (code->expr2->expr_type == EXPR_CONSTANT)
5098 rlen = code->expr2->value.character.length;
5099
5100 else if (code->expr2->ts.cl != NULL
5101 && code->expr2->ts.cl->length != NULL
5102 && code->expr2->ts.cl->length->expr_type == EXPR_CONSTANT)
5103 rlen = mpz_get_si (code->expr2->ts.cl->length->value.integer);
5104
5105 if (rlen && llen && rlen > llen)
5106 gfc_warning_now ("rhs of CHARACTER assignment at %L will "
5107 "be truncated (%d/%d)", &code->loc, rlen, llen);
5108 }
5109
5110 if (gfc_pure (NULL))
5111 {
5112 if (gfc_impure_variable (code->expr->symtree->n.sym))
5113 {
5114 gfc_error
5115 ("Cannot assign to variable '%s' in PURE procedure at %L",
5116 code->expr->symtree->n.sym->name, &code->expr->where);
5117 break;
5118 }
5119
5120 if (code->expr2->ts.type == BT_DERIVED
5121 && derived_pointer (code->expr2->ts.derived))
5122 {
5123 gfc_error
5124 ("Right side of assignment at %L is a derived type "
5125 "containing a POINTER in a PURE procedure",
5126 &code->expr2->where);
5127 break;
5128 }
5129 }
5130
5131 gfc_check_assign (code->expr, code->expr2, 1);
5132 break;
5133
5134 case EXEC_LABEL_ASSIGN:
5135 if (code->label->defined == ST_LABEL_UNKNOWN)
5136 gfc_error ("Label %d referenced at %L is never defined",
5137 code->label->value, &code->label->where);
5138 if (t == SUCCESS
5139 && (code->expr->expr_type != EXPR_VARIABLE
5140 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
5141 || code->expr->symtree->n.sym->ts.kind
5142 != gfc_default_integer_kind
5143 || code->expr->symtree->n.sym->as != NULL))
5144 gfc_error ("ASSIGN statement at %L requires a scalar "
5145 "default INTEGER variable", &code->expr->where);
5146 break;
5147
5148 case EXEC_POINTER_ASSIGN:
5149 if (t == FAILURE)
5150 break;
5151
5152 gfc_check_pointer_assign (code->expr, code->expr2);
5153 break;
5154
5155 case EXEC_ARITHMETIC_IF:
5156 if (t == SUCCESS
5157 && code->expr->ts.type != BT_INTEGER
5158 && code->expr->ts.type != BT_REAL)
5159 gfc_error ("Arithmetic IF statement at %L requires a numeric "
5160 "expression", &code->expr->where);
5161
5162 resolve_branch (code->label, code);
5163 resolve_branch (code->label2, code);
5164 resolve_branch (code->label3, code);
5165 break;
5166
5167 case EXEC_IF:
5168 if (t == SUCCESS && code->expr != NULL
5169 && (code->expr->ts.type != BT_LOGICAL
5170 || code->expr->rank != 0))
5171 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
5172 &code->expr->where);
5173 break;
5174
5175 case EXEC_CALL:
5176 call:
5177 resolve_call (code);
5178 break;
5179
5180 case EXEC_SELECT:
5181 /* Select is complicated. Also, a SELECT construct could be
5182 a transformed computed GOTO. */
5183 resolve_select (code);
5184 break;
5185
5186 case EXEC_DO:
5187 if (code->ext.iterator != NULL)
5188 {
5189 gfc_iterator *iter = code->ext.iterator;
5190 if (gfc_resolve_iterator (iter, true) != FAILURE)
5191 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
5192 }
5193 break;
5194
5195 case EXEC_DO_WHILE:
5196 if (code->expr == NULL)
5197 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
5198 if (t == SUCCESS
5199 && (code->expr->rank != 0
5200 || code->expr->ts.type != BT_LOGICAL))
5201 gfc_error ("Exit condition of DO WHILE loop at %L must be "
5202 "a scalar LOGICAL expression", &code->expr->where);
5203 break;
5204
5205 case EXEC_ALLOCATE:
5206 if (t == SUCCESS && code->expr != NULL
5207 && code->expr->ts.type != BT_INTEGER)
5208 gfc_error ("STAT tag in ALLOCATE statement at %L must be "
5209 "of type INTEGER", &code->expr->where);
5210
5211 for (a = code->ext.alloc_list; a; a = a->next)
5212 resolve_allocate_expr (a->expr, code);
5213
5214 break;
5215
5216 case EXEC_DEALLOCATE:
5217 if (t == SUCCESS && code->expr != NULL
5218 && code->expr->ts.type != BT_INTEGER)
5219 gfc_error
5220 ("STAT tag in DEALLOCATE statement at %L must be of type "
5221 "INTEGER", &code->expr->where);
5222
5223 for (a = code->ext.alloc_list; a; a = a->next)
5224 resolve_deallocate_expr (a->expr);
5225
5226 break;
5227
5228 case EXEC_OPEN:
5229 if (gfc_resolve_open (code->ext.open) == FAILURE)
5230 break;
5231
5232 resolve_branch (code->ext.open->err, code);
5233 break;
5234
5235 case EXEC_CLOSE:
5236 if (gfc_resolve_close (code->ext.close) == FAILURE)
5237 break;
5238
5239 resolve_branch (code->ext.close->err, code);
5240 break;
5241
5242 case EXEC_BACKSPACE:
5243 case EXEC_ENDFILE:
5244 case EXEC_REWIND:
5245 case EXEC_FLUSH:
5246 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
5247 break;
5248
5249 resolve_branch (code->ext.filepos->err, code);
5250 break;
5251
5252 case EXEC_INQUIRE:
5253 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
5254 break;
5255
5256 resolve_branch (code->ext.inquire->err, code);
5257 break;
5258
5259 case EXEC_IOLENGTH:
5260 gcc_assert (code->ext.inquire != NULL);
5261 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
5262 break;
5263
5264 resolve_branch (code->ext.inquire->err, code);
5265 break;
5266
5267 case EXEC_READ:
5268 case EXEC_WRITE:
5269 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
5270 break;
5271
5272 resolve_branch (code->ext.dt->err, code);
5273 resolve_branch (code->ext.dt->end, code);
5274 resolve_branch (code->ext.dt->eor, code);
5275 break;
5276
5277 case EXEC_TRANSFER:
5278 resolve_transfer (code);
5279 break;
5280
5281 case EXEC_FORALL:
5282 resolve_forall_iterators (code->ext.forall_iterator);
5283
5284 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
5285 gfc_error
5286 ("FORALL mask clause at %L requires a LOGICAL expression",
5287 &code->expr->where);
5288 break;
5289
5290 case EXEC_OMP_ATOMIC:
5291 case EXEC_OMP_BARRIER:
5292 case EXEC_OMP_CRITICAL:
5293 case EXEC_OMP_FLUSH:
5294 case EXEC_OMP_DO:
5295 case EXEC_OMP_MASTER:
5296 case EXEC_OMP_ORDERED:
5297 case EXEC_OMP_SECTIONS:
5298 case EXEC_OMP_SINGLE:
5299 case EXEC_OMP_WORKSHARE:
5300 gfc_resolve_omp_directive (code, ns);
5301 break;
5302
5303 case EXEC_OMP_PARALLEL:
5304 case EXEC_OMP_PARALLEL_DO:
5305 case EXEC_OMP_PARALLEL_SECTIONS:
5306 case EXEC_OMP_PARALLEL_WORKSHARE:
5307 omp_workshare_save = omp_workshare_flag;
5308 omp_workshare_flag = 0;
5309 gfc_resolve_omp_directive (code, ns);
5310 omp_workshare_flag = omp_workshare_save;
5311 break;
5312
5313 default:
5314 gfc_internal_error ("resolve_code(): Bad statement code");
5315 }
5316 }
5317
5318 cs_base = frame.prev;
5319 }
5320
5321
5322 /* Resolve initial values and make sure they are compatible with
5323 the variable. */
5324
5325 static void
5326 resolve_values (gfc_symbol * sym)
5327 {
5328
5329 if (sym->value == NULL)
5330 return;
5331
5332 if (gfc_resolve_expr (sym->value) == FAILURE)
5333 return;
5334
5335 gfc_check_assign_symbol (sym, sym->value);
5336 }
5337
5338
5339 /* Resolve an index expression. */
5340
5341 static try
5342 resolve_index_expr (gfc_expr * e)
5343 {
5344 if (gfc_resolve_expr (e) == FAILURE)
5345 return FAILURE;
5346
5347 if (gfc_simplify_expr (e, 0) == FAILURE)
5348 return FAILURE;
5349
5350 if (gfc_specification_expr (e) == FAILURE)
5351 return FAILURE;
5352
5353 return SUCCESS;
5354 }
5355
5356 /* Resolve a charlen structure. */
5357
5358 static try
5359 resolve_charlen (gfc_charlen *cl)
5360 {
5361 if (cl->resolved)
5362 return SUCCESS;
5363
5364 cl->resolved = 1;
5365
5366 specification_expr = 1;
5367
5368 if (resolve_index_expr (cl->length) == FAILURE)
5369 {
5370 specification_expr = 0;
5371 return FAILURE;
5372 }
5373
5374 return SUCCESS;
5375 }
5376
5377
5378 /* Test for non-constant shape arrays. */
5379
5380 static bool
5381 is_non_constant_shape_array (gfc_symbol *sym)
5382 {
5383 gfc_expr *e;
5384 int i;
5385 bool not_constant;
5386
5387 not_constant = false;
5388 if (sym->as != NULL)
5389 {
5390 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
5391 has not been simplified; parameter array references. Do the
5392 simplification now. */
5393 for (i = 0; i < sym->as->rank; i++)
5394 {
5395 e = sym->as->lower[i];
5396 if (e && (resolve_index_expr (e) == FAILURE
5397 || !gfc_is_constant_expr (e)))
5398 not_constant = true;
5399
5400 e = sym->as->upper[i];
5401 if (e && (resolve_index_expr (e) == FAILURE
5402 || !gfc_is_constant_expr (e)))
5403 not_constant = true;
5404 }
5405 }
5406 return not_constant;
5407 }
5408
5409
5410 /* Assign the default initializer to a derived type variable or result. */
5411
5412 static void
5413 apply_default_init (gfc_symbol *sym)
5414 {
5415 gfc_expr *lval;
5416 gfc_expr *init = NULL;
5417 gfc_code *init_st;
5418 gfc_namespace *ns = sym->ns;
5419
5420 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
5421 return;
5422
5423 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
5424 init = gfc_default_initializer (&sym->ts);
5425
5426 if (init == NULL)
5427 return;
5428
5429 /* Search for the function namespace if this is a contained
5430 function without an explicit result. */
5431 if (sym->attr.function && sym == sym->result
5432 && sym->name != sym->ns->proc_name->name)
5433 {
5434 ns = ns->contained;
5435 for (;ns; ns = ns->sibling)
5436 if (strcmp (ns->proc_name->name, sym->name) == 0)
5437 break;
5438 }
5439
5440 if (ns == NULL)
5441 {
5442 gfc_free_expr (init);
5443 return;
5444 }
5445
5446 /* Build an l-value expression for the result. */
5447 lval = gfc_get_expr ();
5448 lval->expr_type = EXPR_VARIABLE;
5449 lval->where = sym->declared_at;
5450 lval->ts = sym->ts;
5451 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
5452
5453 /* It will always be a full array. */
5454 lval->rank = sym->as ? sym->as->rank : 0;
5455 if (lval->rank)
5456 {
5457 lval->ref = gfc_get_ref ();
5458 lval->ref->type = REF_ARRAY;
5459 lval->ref->u.ar.type = AR_FULL;
5460 lval->ref->u.ar.dimen = lval->rank;
5461 lval->ref->u.ar.where = sym->declared_at;
5462 lval->ref->u.ar.as = sym->as;
5463 }
5464
5465 /* Add the code at scope entry. */
5466 init_st = gfc_get_code ();
5467 init_st->next = ns->code;
5468 ns->code = init_st;
5469
5470 /* Assign the default initializer to the l-value. */
5471 init_st->loc = sym->declared_at;
5472 init_st->op = EXEC_INIT_ASSIGN;
5473 init_st->expr = lval;
5474 init_st->expr2 = init;
5475 }
5476
5477
5478 /* Resolution of common features of flavors variable and procedure. */
5479
5480 static try
5481 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
5482 {
5483 /* Constraints on deferred shape variable. */
5484 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
5485 {
5486 if (sym->attr.allocatable)
5487 {
5488 if (sym->attr.dimension)
5489 gfc_error ("Allocatable array '%s' at %L must have "
5490 "a deferred shape", sym->name, &sym->declared_at);
5491 else
5492 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
5493 sym->name, &sym->declared_at);
5494 return FAILURE;
5495 }
5496
5497 if (sym->attr.pointer && sym->attr.dimension)
5498 {
5499 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
5500 sym->name, &sym->declared_at);
5501 return FAILURE;
5502 }
5503
5504 }
5505 else
5506 {
5507 if (!mp_flag && !sym->attr.allocatable
5508 && !sym->attr.pointer && !sym->attr.dummy)
5509 {
5510 gfc_error ("Array '%s' at %L cannot have a deferred shape",
5511 sym->name, &sym->declared_at);
5512 return FAILURE;
5513 }
5514 }
5515 return SUCCESS;
5516 }
5517
5518 /* Resolve symbols with flavor variable. */
5519
5520 static try
5521 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
5522 {
5523 int flag;
5524 int i;
5525 gfc_expr *e;
5526 gfc_expr *constructor_expr;
5527 const char * auto_save_msg;
5528
5529 auto_save_msg = "automatic object '%s' at %L cannot have the "
5530 "SAVE attribute";
5531
5532 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
5533 return FAILURE;
5534
5535 /* Set this flag to check that variables are parameters of all entries.
5536 This check is effected by the call to gfc_resolve_expr through
5537 is_non_constant_shape_array. */
5538 specification_expr = 1;
5539
5540 if (!sym->attr.use_assoc
5541 && !sym->attr.allocatable
5542 && !sym->attr.pointer
5543 && is_non_constant_shape_array (sym))
5544 {
5545 /* The shape of a main program or module array needs to be constant. */
5546 if (sym->ns->proc_name
5547 && (sym->ns->proc_name->attr.flavor == FL_MODULE
5548 || sym->ns->proc_name->attr.is_main_program))
5549 {
5550 gfc_error ("The module or main program array '%s' at %L must "
5551 "have constant shape", sym->name, &sym->declared_at);
5552 specification_expr = 0;
5553 return FAILURE;
5554 }
5555 }
5556
5557 if (sym->ts.type == BT_CHARACTER)
5558 {
5559 /* Make sure that character string variables with assumed length are
5560 dummy arguments. */
5561 e = sym->ts.cl->length;
5562 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
5563 {
5564 gfc_error ("Entity with assumed character length at %L must be a "
5565 "dummy argument or a PARAMETER", &sym->declared_at);
5566 return FAILURE;
5567 }
5568
5569 if (e && sym->attr.save && !gfc_is_constant_expr (e))
5570 {
5571 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
5572 return FAILURE;
5573 }
5574
5575 if (!gfc_is_constant_expr (e)
5576 && !(e->expr_type == EXPR_VARIABLE
5577 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
5578 && sym->ns->proc_name
5579 && (sym->ns->proc_name->attr.flavor == FL_MODULE
5580 || sym->ns->proc_name->attr.is_main_program)
5581 && !sym->attr.use_assoc)
5582 {
5583 gfc_error ("'%s' at %L must have constant character length "
5584 "in this context", sym->name, &sym->declared_at);
5585 return FAILURE;
5586 }
5587 }
5588
5589 /* Can the symbol have an initializer? */
5590 flag = 0;
5591 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
5592 || sym->attr.intrinsic || sym->attr.result)
5593 flag = 1;
5594 else if (sym->attr.dimension && !sym->attr.pointer)
5595 {
5596 /* Don't allow initialization of automatic arrays. */
5597 for (i = 0; i < sym->as->rank; i++)
5598 {
5599 if (sym->as->lower[i] == NULL
5600 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
5601 || sym->as->upper[i] == NULL
5602 || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
5603 {
5604 flag = 1;
5605 break;
5606 }
5607 }
5608
5609 /* Also, they must not have the SAVE attribute. */
5610 if (flag && sym->attr.save)
5611 {
5612 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
5613 return FAILURE;
5614 }
5615 }
5616
5617 /* Reject illegal initializers. */
5618 if (sym->value && flag)
5619 {
5620 if (sym->attr.allocatable)
5621 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
5622 sym->name, &sym->declared_at);
5623 else if (sym->attr.external)
5624 gfc_error ("External '%s' at %L cannot have an initializer",
5625 sym->name, &sym->declared_at);
5626 else if (sym->attr.dummy)
5627 gfc_error ("Dummy '%s' at %L cannot have an initializer",
5628 sym->name, &sym->declared_at);
5629 else if (sym->attr.intrinsic)
5630 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
5631 sym->name, &sym->declared_at);
5632 else if (sym->attr.result)
5633 gfc_error ("Function result '%s' at %L cannot have an initializer",
5634 sym->name, &sym->declared_at);
5635 else
5636 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
5637 sym->name, &sym->declared_at);
5638 return FAILURE;
5639 }
5640
5641 /* Check to see if a derived type is blocked from being host associated
5642 by the presence of another class I symbol in the same namespace.
5643 14.6.1.3 of the standard and the discussion on comp.lang.fortran. */
5644 if (sym->ts.type == BT_DERIVED && sym->ns != sym->ts.derived->ns)
5645 {
5646 gfc_symbol *s;
5647 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
5648 if (s && (s->attr.flavor != FL_DERIVED
5649 || !gfc_compare_derived_types (s, sym->ts.derived)))
5650 {
5651 gfc_error ("The type %s cannot be host associated at %L because "
5652 "it is blocked by an incompatible object of the same "
5653 "name at %L", sym->ts.derived->name, &sym->declared_at,
5654 &s->declared_at);
5655 return FAILURE;
5656 }
5657 }
5658
5659 /* 4th constraint in section 11.3: "If an object of a type for which
5660 component-initialization is specified (R429) appears in the
5661 specification-part of a module and does not have the ALLOCATABLE
5662 or POINTER attribute, the object shall have the SAVE attribute." */
5663
5664 constructor_expr = NULL;
5665 if (sym->ts.type == BT_DERIVED && !(sym->value || flag))
5666 constructor_expr = gfc_default_initializer (&sym->ts);
5667
5668 if (sym->ns->proc_name
5669 && sym->ns->proc_name->attr.flavor == FL_MODULE
5670 && constructor_expr
5671 && !sym->ns->save_all && !sym->attr.save
5672 && !sym->attr.pointer && !sym->attr.allocatable)
5673 {
5674 gfc_error("Object '%s' at %L must have the SAVE attribute %s",
5675 sym->name, &sym->declared_at,
5676 "for default initialization of a component");
5677 return FAILURE;
5678 }
5679
5680 /* Assign default initializer. */
5681 if (sym->ts.type == BT_DERIVED
5682 && !sym->value
5683 && !sym->attr.pointer
5684 && !sym->attr.allocatable
5685 && (!flag || sym->attr.intent == INTENT_OUT))
5686 sym->value = gfc_default_initializer (&sym->ts);
5687
5688 return SUCCESS;
5689 }
5690
5691
5692 /* Resolve a procedure. */
5693
5694 static try
5695 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
5696 {
5697 gfc_formal_arglist *arg;
5698
5699 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
5700 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
5701 "interfaces", sym->name, &sym->declared_at);
5702
5703 if (sym->attr.function
5704 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
5705 return FAILURE;
5706
5707 if (sym->ts.type == BT_CHARACTER)
5708 {
5709 gfc_charlen *cl = sym->ts.cl;
5710 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
5711 {
5712 if (sym->attr.proc == PROC_ST_FUNCTION)
5713 {
5714 gfc_error ("Character-valued statement function '%s' at %L must "
5715 "have constant length", sym->name, &sym->declared_at);
5716 return FAILURE;
5717 }
5718
5719 if (sym->attr.external && sym->formal == NULL
5720 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
5721 {
5722 gfc_error ("Automatic character length function '%s' at %L must "
5723 "have an explicit interface", sym->name, &sym->declared_at);
5724 return FAILURE;
5725 }
5726 }
5727 }
5728
5729 /* Ensure that derived type for are not of a private type. Internal
5730 module procedures are excluded by 2.2.3.3 - ie. they are not
5731 externally accessible and can access all the objects accessible in
5732 the host. */
5733 if (!(sym->ns->parent
5734 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
5735 && gfc_check_access(sym->attr.access, sym->ns->default_access))
5736 {
5737 for (arg = sym->formal; arg; arg = arg->next)
5738 {
5739 if (arg->sym
5740 && arg->sym->ts.type == BT_DERIVED
5741 && !arg->sym->ts.derived->attr.use_assoc
5742 && !gfc_check_access(arg->sym->ts.derived->attr.access,
5743 arg->sym->ts.derived->ns->default_access))
5744 {
5745 gfc_error_now ("'%s' is of a PRIVATE type and cannot be "
5746 "a dummy argument of '%s', which is "
5747 "PUBLIC at %L", arg->sym->name, sym->name,
5748 &sym->declared_at);
5749 /* Stop this message from recurring. */
5750 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
5751 return FAILURE;
5752 }
5753 }
5754 }
5755
5756 /* An external symbol may not have an initializer because it is taken to be
5757 a procedure. */
5758 if (sym->attr.external && sym->value)
5759 {
5760 gfc_error ("External object '%s' at %L may not have an initializer",
5761 sym->name, &sym->declared_at);
5762 return FAILURE;
5763 }
5764
5765 /* An elemental function is required to return a scalar 12.7.1 */
5766 if (sym->attr.elemental && sym->attr.function && sym->as)
5767 {
5768 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
5769 "result", sym->name, &sym->declared_at);
5770 /* Reset so that the error only occurs once. */
5771 sym->attr.elemental = 0;
5772 return FAILURE;
5773 }
5774
5775 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
5776 char-len-param shall not be array-valued, pointer-valued, recursive
5777 or pure. ....snip... A character value of * may only be used in the
5778 following ways: (i) Dummy arg of procedure - dummy associates with
5779 actual length; (ii) To declare a named constant; or (iii) External
5780 function - but length must be declared in calling scoping unit. */
5781 if (sym->attr.function
5782 && sym->ts.type == BT_CHARACTER
5783 && sym->ts.cl && sym->ts.cl->length == NULL)
5784 {
5785 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
5786 || (sym->attr.recursive) || (sym->attr.pure))
5787 {
5788 if (sym->as && sym->as->rank)
5789 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
5790 "array-valued", sym->name, &sym->declared_at);
5791
5792 if (sym->attr.pointer)
5793 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
5794 "pointer-valued", sym->name, &sym->declared_at);
5795
5796 if (sym->attr.pure)
5797 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
5798 "pure", sym->name, &sym->declared_at);
5799
5800 if (sym->attr.recursive)
5801 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
5802 "recursive", sym->name, &sym->declared_at);
5803
5804 return FAILURE;
5805 }
5806
5807 /* Appendix B.2 of the standard. Contained functions give an
5808 error anyway. Fixed-form is likely to be F77/legacy. */
5809 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
5810 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
5811 "'%s' at %L is obsolescent in fortran 95",
5812 sym->name, &sym->declared_at);
5813 }
5814 return SUCCESS;
5815 }
5816
5817
5818 /* Resolve the components of a derived type. */
5819
5820 static try
5821 resolve_fl_derived (gfc_symbol *sym)
5822 {
5823 gfc_component *c;
5824 gfc_dt_list * dt_list;
5825 int i;
5826
5827 for (c = sym->components; c != NULL; c = c->next)
5828 {
5829 if (c->ts.type == BT_CHARACTER)
5830 {
5831 if (c->ts.cl->length == NULL
5832 || (resolve_charlen (c->ts.cl) == FAILURE)
5833 || !gfc_is_constant_expr (c->ts.cl->length))
5834 {
5835 gfc_error ("Character length of component '%s' needs to "
5836 "be a constant specification expression at %L",
5837 c->name,
5838 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
5839 return FAILURE;
5840 }
5841 }
5842
5843 if (c->ts.type == BT_DERIVED
5844 && sym->component_access != ACCESS_PRIVATE
5845 && gfc_check_access(sym->attr.access, sym->ns->default_access)
5846 && !c->ts.derived->attr.use_assoc
5847 && !gfc_check_access(c->ts.derived->attr.access,
5848 c->ts.derived->ns->default_access))
5849 {
5850 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
5851 "a component of '%s', which is PUBLIC at %L",
5852 c->name, sym->name, &sym->declared_at);
5853 return FAILURE;
5854 }
5855
5856 if (sym->attr.sequence)
5857 {
5858 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
5859 {
5860 gfc_error ("Component %s of SEQUENCE type declared at %L does "
5861 "not have the SEQUENCE attribute",
5862 c->ts.derived->name, &sym->declared_at);
5863 return FAILURE;
5864 }
5865 }
5866
5867 if (c->ts.type == BT_DERIVED && c->pointer
5868 && c->ts.derived->components == NULL)
5869 {
5870 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
5871 "that has not been declared", c->name, sym->name,
5872 &c->loc);
5873 return FAILURE;
5874 }
5875
5876 if (c->pointer || c->allocatable || c->as == NULL)
5877 continue;
5878
5879 for (i = 0; i < c->as->rank; i++)
5880 {
5881 if (c->as->lower[i] == NULL
5882 || !gfc_is_constant_expr (c->as->lower[i])
5883 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
5884 || c->as->upper[i] == NULL
5885 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
5886 || !gfc_is_constant_expr (c->as->upper[i]))
5887 {
5888 gfc_error ("Component '%s' of '%s' at %L must have "
5889 "constant array bounds",
5890 c->name, sym->name, &c->loc);
5891 return FAILURE;
5892 }
5893 }
5894 }
5895
5896 /* Add derived type to the derived type list. */
5897 for (dt_list = sym->ns->derived_types; dt_list; dt_list = dt_list->next)
5898 if (sym == dt_list->derived)
5899 break;
5900
5901 if (dt_list == NULL)
5902 {
5903 dt_list = gfc_get_dt_list ();
5904 dt_list->next = sym->ns->derived_types;
5905 dt_list->derived = sym;
5906 sym->ns->derived_types = dt_list;
5907 }
5908
5909 return SUCCESS;
5910 }
5911
5912
5913 static try
5914 resolve_fl_namelist (gfc_symbol *sym)
5915 {
5916 gfc_namelist *nl;
5917 gfc_symbol *nlsym;
5918
5919 /* Reject PRIVATE objects in a PUBLIC namelist. */
5920 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
5921 {
5922 for (nl = sym->namelist; nl; nl = nl->next)
5923 {
5924 if (!nl->sym->attr.use_assoc
5925 && !(sym->ns->parent == nl->sym->ns)
5926 && !gfc_check_access(nl->sym->attr.access,
5927 nl->sym->ns->default_access))
5928 {
5929 gfc_error ("PRIVATE symbol '%s' cannot be member of "
5930 "PUBLIC namelist at %L", nl->sym->name,
5931 &sym->declared_at);
5932 return FAILURE;
5933 }
5934 }
5935 }
5936
5937 /* Reject namelist arrays that are not constant shape. */
5938 for (nl = sym->namelist; nl; nl = nl->next)
5939 {
5940 if (is_non_constant_shape_array (nl->sym))
5941 {
5942 gfc_error ("The array '%s' must have constant shape to be "
5943 "a NAMELIST object at %L", nl->sym->name,
5944 &sym->declared_at);
5945 return FAILURE;
5946 }
5947 }
5948
5949 /* Namelist objects cannot have allocatable components. */
5950 for (nl = sym->namelist; nl; nl = nl->next)
5951 {
5952 if (nl->sym->ts.type == BT_DERIVED
5953 && nl->sym->ts.derived->attr.alloc_comp)
5954 {
5955 gfc_error ("NAMELIST object '%s' at %L cannot have ALLOCATABLE "
5956 "components", nl->sym->name, &sym->declared_at);
5957 return FAILURE;
5958 }
5959 }
5960
5961 /* 14.1.2 A module or internal procedure represent local entities
5962 of the same type as a namelist member and so are not allowed.
5963 Note that this is sometimes caught by check_conflict so the
5964 same message has been used. */
5965 for (nl = sym->namelist; nl; nl = nl->next)
5966 {
5967 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
5968 continue;
5969 nlsym = NULL;
5970 if (sym->ns->parent && nl->sym && nl->sym->name)
5971 gfc_find_symbol (nl->sym->name, sym->ns->parent, 0, &nlsym);
5972 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
5973 {
5974 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
5975 "attribute in '%s' at %L", nlsym->name,
5976 &sym->declared_at);
5977 return FAILURE;
5978 }
5979 }
5980
5981 return SUCCESS;
5982 }
5983
5984
5985 static try
5986 resolve_fl_parameter (gfc_symbol *sym)
5987 {
5988 /* A parameter array's shape needs to be constant. */
5989 if (sym->as != NULL && !gfc_is_compile_time_shape (sym->as))
5990 {
5991 gfc_error ("Parameter array '%s' at %L cannot be automatic "
5992 "or assumed shape", sym->name, &sym->declared_at);
5993 return FAILURE;
5994 }
5995
5996 /* Make sure a parameter that has been implicitly typed still
5997 matches the implicit type, since PARAMETER statements can precede
5998 IMPLICIT statements. */
5999 if (sym->attr.implicit_type
6000 && !gfc_compare_types (&sym->ts,
6001 gfc_get_default_type (sym, sym->ns)))
6002 {
6003 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
6004 "later IMPLICIT type", sym->name, &sym->declared_at);
6005 return FAILURE;
6006 }
6007
6008 /* Make sure the types of derived parameters are consistent. This
6009 type checking is deferred until resolution because the type may
6010 refer to a derived type from the host. */
6011 if (sym->ts.type == BT_DERIVED
6012 && !gfc_compare_types (&sym->ts, &sym->value->ts))
6013 {
6014 gfc_error ("Incompatible derived type in PARAMETER at %L",
6015 &sym->value->where);
6016 return FAILURE;
6017 }
6018 return SUCCESS;
6019 }
6020
6021
6022 /* Do anything necessary to resolve a symbol. Right now, we just
6023 assume that an otherwise unknown symbol is a variable. This sort
6024 of thing commonly happens for symbols in module. */
6025
6026 static void
6027 resolve_symbol (gfc_symbol * sym)
6028 {
6029 /* Zero if we are checking a formal namespace. */
6030 static int formal_ns_flag = 1;
6031 int formal_ns_save, check_constant, mp_flag;
6032 gfc_symtree *symtree;
6033 gfc_symtree *this_symtree;
6034 gfc_namespace *ns;
6035 gfc_component *c;
6036
6037 if (sym->attr.flavor == FL_UNKNOWN)
6038 {
6039
6040 /* If we find that a flavorless symbol is an interface in one of the
6041 parent namespaces, find its symtree in this namespace, free the
6042 symbol and set the symtree to point to the interface symbol. */
6043 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
6044 {
6045 symtree = gfc_find_symtree (ns->sym_root, sym->name);
6046 if (symtree && symtree->n.sym->generic)
6047 {
6048 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
6049 sym->name);
6050 sym->refs--;
6051 if (!sym->refs)
6052 gfc_free_symbol (sym);
6053 symtree->n.sym->refs++;
6054 this_symtree->n.sym = symtree->n.sym;
6055 return;
6056 }
6057 }
6058
6059 /* Otherwise give it a flavor according to such attributes as
6060 it has. */
6061 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
6062 sym->attr.flavor = FL_VARIABLE;
6063 else
6064 {
6065 sym->attr.flavor = FL_PROCEDURE;
6066 if (sym->attr.dimension)
6067 sym->attr.function = 1;
6068 }
6069 }
6070
6071 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
6072 return;
6073
6074 /* Symbols that are module procedures with results (functions) have
6075 the types and array specification copied for type checking in
6076 procedures that call them, as well as for saving to a module
6077 file. These symbols can't stand the scrutiny that their results
6078 can. */
6079 mp_flag = (sym->result != NULL && sym->result != sym);
6080
6081 /* Assign default type to symbols that need one and don't have one. */
6082 if (sym->ts.type == BT_UNKNOWN)
6083 {
6084 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
6085 gfc_set_default_type (sym, 1, NULL);
6086
6087 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
6088 {
6089 /* The specific case of an external procedure should emit an error
6090 in the case that there is no implicit type. */
6091 if (!mp_flag)
6092 gfc_set_default_type (sym, sym->attr.external, NULL);
6093 else
6094 {
6095 /* Result may be in another namespace. */
6096 resolve_symbol (sym->result);
6097
6098 sym->ts = sym->result->ts;
6099 sym->as = gfc_copy_array_spec (sym->result->as);
6100 sym->attr.dimension = sym->result->attr.dimension;
6101 sym->attr.pointer = sym->result->attr.pointer;
6102 sym->attr.allocatable = sym->result->attr.allocatable;
6103 }
6104 }
6105 }
6106
6107 /* Assumed size arrays and assumed shape arrays must be dummy
6108 arguments. */
6109
6110 if (sym->as != NULL
6111 && (sym->as->type == AS_ASSUMED_SIZE
6112 || sym->as->type == AS_ASSUMED_SHAPE)
6113 && sym->attr.dummy == 0)
6114 {
6115 if (sym->as->type == AS_ASSUMED_SIZE)
6116 gfc_error ("Assumed size array at %L must be a dummy argument",
6117 &sym->declared_at);
6118 else
6119 gfc_error ("Assumed shape array at %L must be a dummy argument",
6120 &sym->declared_at);
6121 return;
6122 }
6123
6124 /* Make sure symbols with known intent or optional are really dummy
6125 variable. Because of ENTRY statement, this has to be deferred
6126 until resolution time. */
6127
6128 if (!sym->attr.dummy
6129 && (sym->attr.optional
6130 || sym->attr.intent != INTENT_UNKNOWN))
6131 {
6132 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
6133 return;
6134 }
6135
6136 if (sym->attr.value && !sym->attr.dummy)
6137 {
6138 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
6139 "it is not a dummy", sym->name, &sym->declared_at);
6140 return;
6141 }
6142
6143
6144 /* If a derived type symbol has reached this point, without its
6145 type being declared, we have an error. Notice that most
6146 conditions that produce undefined derived types have already
6147 been dealt with. However, the likes of:
6148 implicit type(t) (t) ..... call foo (t) will get us here if
6149 the type is not declared in the scope of the implicit
6150 statement. Change the type to BT_UNKNOWN, both because it is so
6151 and to prevent an ICE. */
6152 if (sym->ts.type == BT_DERIVED
6153 && sym->ts.derived->components == NULL)
6154 {
6155 gfc_error ("The derived type '%s' at %L is of type '%s', "
6156 "which has not been defined", sym->name,
6157 &sym->declared_at, sym->ts.derived->name);
6158 sym->ts.type = BT_UNKNOWN;
6159 return;
6160 }
6161
6162 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
6163 default initialization is defined (5.1.2.4.4). */
6164 if (sym->ts.type == BT_DERIVED
6165 && sym->attr.dummy
6166 && sym->attr.intent == INTENT_OUT
6167 && sym->as
6168 && sym->as->type == AS_ASSUMED_SIZE)
6169 {
6170 for (c = sym->ts.derived->components; c; c = c->next)
6171 {
6172 if (c->initializer)
6173 {
6174 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
6175 "ASSUMED SIZE and so cannot have a default initializer",
6176 sym->name, &sym->declared_at);
6177 return;
6178 }
6179 }
6180 }
6181
6182 switch (sym->attr.flavor)
6183 {
6184 case FL_VARIABLE:
6185 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
6186 return;
6187 break;
6188
6189 case FL_PROCEDURE:
6190 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
6191 return;
6192 break;
6193
6194 case FL_NAMELIST:
6195 if (resolve_fl_namelist (sym) == FAILURE)
6196 return;
6197 break;
6198
6199 case FL_PARAMETER:
6200 if (resolve_fl_parameter (sym) == FAILURE)
6201 return;
6202 break;
6203
6204 default:
6205 break;
6206 }
6207
6208 /* Make sure that intrinsic exist */
6209 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
6210 && ! gfc_intrinsic_name(sym->name, 0)
6211 && ! gfc_intrinsic_name(sym->name, 1))
6212 gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
6213
6214 /* Resolve array specifier. Check as well some constraints
6215 on COMMON blocks. */
6216
6217 check_constant = sym->attr.in_common && !sym->attr.pointer;
6218
6219 /* Set the formal_arg_flag so that check_conflict will not throw
6220 an error for host associated variables in the specification
6221 expression for an array_valued function. */
6222 if (sym->attr.function && sym->as)
6223 formal_arg_flag = 1;
6224
6225 gfc_resolve_array_spec (sym->as, check_constant);
6226
6227 formal_arg_flag = 0;
6228
6229 /* Resolve formal namespaces. */
6230
6231 if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
6232 {
6233 formal_ns_save = formal_ns_flag;
6234 formal_ns_flag = 0;
6235 gfc_resolve (sym->formal_ns);
6236 formal_ns_flag = formal_ns_save;
6237 }
6238
6239 /* Check threadprivate restrictions. */
6240 if (sym->attr.threadprivate && !sym->attr.save
6241 && (!sym->attr.in_common
6242 && sym->module == NULL
6243 && (sym->ns->proc_name == NULL
6244 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
6245 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
6246
6247 /* If we have come this far we can apply default-initializers, as
6248 described in 14.7.5, to those variables that have not already
6249 been assigned one. */
6250 if (sym->ts.type == BT_DERIVED
6251 && sym->attr.referenced
6252 && sym->ns == gfc_current_ns
6253 && !sym->value
6254 && !sym->attr.allocatable
6255 && !sym->attr.alloc_comp)
6256 {
6257 symbol_attribute *a = &sym->attr;
6258
6259 if ((!a->save && !a->dummy && !a->pointer
6260 && !a->in_common && !a->use_assoc
6261 && !(a->function && sym != sym->result))
6262 ||
6263 (a->dummy && a->intent == INTENT_OUT))
6264 apply_default_init (sym);
6265 }
6266 }
6267
6268
6269
6270 /************* Resolve DATA statements *************/
6271
6272 static struct
6273 {
6274 gfc_data_value *vnode;
6275 unsigned int left;
6276 }
6277 values;
6278
6279
6280 /* Advance the values structure to point to the next value in the data list. */
6281
6282 static try
6283 next_data_value (void)
6284 {
6285 while (values.left == 0)
6286 {
6287 if (values.vnode->next == NULL)
6288 return FAILURE;
6289
6290 values.vnode = values.vnode->next;
6291 values.left = values.vnode->repeat;
6292 }
6293
6294 return SUCCESS;
6295 }
6296
6297
6298 static try
6299 check_data_variable (gfc_data_variable * var, locus * where)
6300 {
6301 gfc_expr *e;
6302 mpz_t size;
6303 mpz_t offset;
6304 try t;
6305 ar_type mark = AR_UNKNOWN;
6306 int i;
6307 mpz_t section_index[GFC_MAX_DIMENSIONS];
6308 gfc_ref *ref;
6309 gfc_array_ref *ar;
6310
6311 if (gfc_resolve_expr (var->expr) == FAILURE)
6312 return FAILURE;
6313
6314 ar = NULL;
6315 mpz_init_set_si (offset, 0);
6316 e = var->expr;
6317
6318 if (e->expr_type != EXPR_VARIABLE)
6319 gfc_internal_error ("check_data_variable(): Bad expression");
6320
6321 if (e->symtree->n.sym->ns->is_block_data
6322 && !e->symtree->n.sym->attr.in_common)
6323 {
6324 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
6325 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
6326 }
6327
6328 if (e->rank == 0)
6329 {
6330 mpz_init_set_ui (size, 1);
6331 ref = NULL;
6332 }
6333 else
6334 {
6335 ref = e->ref;
6336
6337 /* Find the array section reference. */
6338 for (ref = e->ref; ref; ref = ref->next)
6339 {
6340 if (ref->type != REF_ARRAY)
6341 continue;
6342 if (ref->u.ar.type == AR_ELEMENT)
6343 continue;
6344 break;
6345 }
6346 gcc_assert (ref);
6347
6348 /* Set marks according to the reference pattern. */
6349 switch (ref->u.ar.type)
6350 {
6351 case AR_FULL:
6352 mark = AR_FULL;
6353 break;
6354
6355 case AR_SECTION:
6356 ar = &ref->u.ar;
6357 /* Get the start position of array section. */
6358 gfc_get_section_index (ar, section_index, &offset);
6359 mark = AR_SECTION;
6360 break;
6361
6362 default:
6363 gcc_unreachable ();
6364 }
6365
6366 if (gfc_array_size (e, &size) == FAILURE)
6367 {
6368 gfc_error ("Nonconstant array section at %L in DATA statement",
6369 &e->where);
6370 mpz_clear (offset);
6371 return FAILURE;
6372 }
6373 }
6374
6375 t = SUCCESS;
6376
6377 while (mpz_cmp_ui (size, 0) > 0)
6378 {
6379 if (next_data_value () == FAILURE)
6380 {
6381 gfc_error ("DATA statement at %L has more variables than values",
6382 where);
6383 t = FAILURE;
6384 break;
6385 }
6386
6387 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
6388 if (t == FAILURE)
6389 break;
6390
6391 /* If we have more than one element left in the repeat count,
6392 and we have more than one element left in the target variable,
6393 then create a range assignment. */
6394 /* ??? Only done for full arrays for now, since array sections
6395 seem tricky. */
6396 if (mark == AR_FULL && ref && ref->next == NULL
6397 && values.left > 1 && mpz_cmp_ui (size, 1) > 0)
6398 {
6399 mpz_t range;
6400
6401 if (mpz_cmp_ui (size, values.left) >= 0)
6402 {
6403 mpz_init_set_ui (range, values.left);
6404 mpz_sub_ui (size, size, values.left);
6405 values.left = 0;
6406 }
6407 else
6408 {
6409 mpz_init_set (range, size);
6410 values.left -= mpz_get_ui (size);
6411 mpz_set_ui (size, 0);
6412 }
6413
6414 gfc_assign_data_value_range (var->expr, values.vnode->expr,
6415 offset, range);
6416
6417 mpz_add (offset, offset, range);
6418 mpz_clear (range);
6419 }
6420
6421 /* Assign initial value to symbol. */
6422 else
6423 {
6424 values.left -= 1;
6425 mpz_sub_ui (size, size, 1);
6426
6427 gfc_assign_data_value (var->expr, values.vnode->expr, offset);
6428
6429 if (mark == AR_FULL)
6430 mpz_add_ui (offset, offset, 1);
6431
6432 /* Modify the array section indexes and recalculate the offset
6433 for next element. */
6434 else if (mark == AR_SECTION)
6435 gfc_advance_section (section_index, ar, &offset);
6436 }
6437 }
6438
6439 if (mark == AR_SECTION)
6440 {
6441 for (i = 0; i < ar->dimen; i++)
6442 mpz_clear (section_index[i]);
6443 }
6444
6445 mpz_clear (size);
6446 mpz_clear (offset);
6447
6448 return t;
6449 }
6450
6451
6452 static try traverse_data_var (gfc_data_variable *, locus *);
6453
6454 /* Iterate over a list of elements in a DATA statement. */
6455
6456 static try
6457 traverse_data_list (gfc_data_variable * var, locus * where)
6458 {
6459 mpz_t trip;
6460 iterator_stack frame;
6461 gfc_expr *e, *start, *end, *step;
6462 try retval = SUCCESS;
6463
6464 mpz_init (frame.value);
6465
6466 start = gfc_copy_expr (var->iter.start);
6467 end = gfc_copy_expr (var->iter.end);
6468 step = gfc_copy_expr (var->iter.step);
6469
6470 if (gfc_simplify_expr (start, 1) == FAILURE
6471 || start->expr_type != EXPR_CONSTANT)
6472 {
6473 gfc_error ("iterator start at %L does not simplify",
6474 &start->where);
6475 retval = FAILURE;
6476 goto cleanup;
6477 }
6478 if (gfc_simplify_expr (end, 1) == FAILURE
6479 || end->expr_type != EXPR_CONSTANT)
6480 {
6481 gfc_error ("iterator end at %L does not simplify",
6482 &end->where);
6483 retval = FAILURE;
6484 goto cleanup;
6485 }
6486 if (gfc_simplify_expr (step, 1) == FAILURE
6487 || step->expr_type != EXPR_CONSTANT)
6488 {
6489 gfc_error ("iterator step at %L does not simplify",
6490 &step->where);
6491 retval = FAILURE;
6492 goto cleanup;
6493 }
6494
6495 mpz_init_set (trip, end->value.integer);
6496 mpz_sub (trip, trip, start->value.integer);
6497 mpz_add (trip, trip, step->value.integer);
6498
6499 mpz_div (trip, trip, step->value.integer);
6500
6501 mpz_set (frame.value, start->value.integer);
6502
6503 frame.prev = iter_stack;
6504 frame.variable = var->iter.var->symtree;
6505 iter_stack = &frame;
6506
6507 while (mpz_cmp_ui (trip, 0) > 0)
6508 {
6509 if (traverse_data_var (var->list, where) == FAILURE)
6510 {
6511 mpz_clear (trip);
6512 retval = FAILURE;
6513 goto cleanup;
6514 }
6515
6516 e = gfc_copy_expr (var->expr);
6517 if (gfc_simplify_expr (e, 1) == FAILURE)
6518 {
6519 gfc_free_expr (e);
6520 mpz_clear (trip);
6521 retval = FAILURE;
6522 goto cleanup;
6523 }
6524
6525 mpz_add (frame.value, frame.value, step->value.integer);
6526
6527 mpz_sub_ui (trip, trip, 1);
6528 }
6529
6530 mpz_clear (trip);
6531 cleanup:
6532 mpz_clear (frame.value);
6533
6534 gfc_free_expr (start);
6535 gfc_free_expr (end);
6536 gfc_free_expr (step);
6537
6538 iter_stack = frame.prev;
6539 return retval;
6540 }
6541
6542
6543 /* Type resolve variables in the variable list of a DATA statement. */
6544
6545 static try
6546 traverse_data_var (gfc_data_variable * var, locus * where)
6547 {
6548 try t;
6549
6550 for (; var; var = var->next)
6551 {
6552 if (var->expr == NULL)
6553 t = traverse_data_list (var, where);
6554 else
6555 t = check_data_variable (var, where);
6556
6557 if (t == FAILURE)
6558 return FAILURE;
6559 }
6560
6561 return SUCCESS;
6562 }
6563
6564
6565 /* Resolve the expressions and iterators associated with a data statement.
6566 This is separate from the assignment checking because data lists should
6567 only be resolved once. */
6568
6569 static try
6570 resolve_data_variables (gfc_data_variable * d)
6571 {
6572 for (; d; d = d->next)
6573 {
6574 if (d->list == NULL)
6575 {
6576 if (gfc_resolve_expr (d->expr) == FAILURE)
6577 return FAILURE;
6578 }
6579 else
6580 {
6581 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
6582 return FAILURE;
6583
6584 if (resolve_data_variables (d->list) == FAILURE)
6585 return FAILURE;
6586 }
6587 }
6588
6589 return SUCCESS;
6590 }
6591
6592
6593 /* Resolve a single DATA statement. We implement this by storing a pointer to
6594 the value list into static variables, and then recursively traversing the
6595 variables list, expanding iterators and such. */
6596
6597 static void
6598 resolve_data (gfc_data * d)
6599 {
6600 if (resolve_data_variables (d->var) == FAILURE)
6601 return;
6602
6603 values.vnode = d->value;
6604 values.left = (d->value == NULL) ? 0 : d->value->repeat;
6605
6606 if (traverse_data_var (d->var, &d->where) == FAILURE)
6607 return;
6608
6609 /* At this point, we better not have any values left. */
6610
6611 if (next_data_value () == SUCCESS)
6612 gfc_error ("DATA statement at %L has more values than variables",
6613 &d->where);
6614 }
6615
6616
6617 /* Determines if a variable is not 'pure', ie not assignable within a pure
6618 procedure. Returns zero if assignment is OK, nonzero if there is a problem.
6619 */
6620
6621 int
6622 gfc_impure_variable (gfc_symbol * sym)
6623 {
6624 if (sym->attr.use_assoc || sym->attr.in_common)
6625 return 1;
6626
6627 if (sym->ns != gfc_current_ns)
6628 return !sym->attr.function;
6629
6630 /* TODO: Check storage association through EQUIVALENCE statements */
6631
6632 return 0;
6633 }
6634
6635
6636 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
6637 symbol of the current procedure. */
6638
6639 int
6640 gfc_pure (gfc_symbol * sym)
6641 {
6642 symbol_attribute attr;
6643
6644 if (sym == NULL)
6645 sym = gfc_current_ns->proc_name;
6646 if (sym == NULL)
6647 return 0;
6648
6649 attr = sym->attr;
6650
6651 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
6652 }
6653
6654
6655 /* Test whether the current procedure is elemental or not. */
6656
6657 int
6658 gfc_elemental (gfc_symbol * sym)
6659 {
6660 symbol_attribute attr;
6661
6662 if (sym == NULL)
6663 sym = gfc_current_ns->proc_name;
6664 if (sym == NULL)
6665 return 0;
6666 attr = sym->attr;
6667
6668 return attr.flavor == FL_PROCEDURE && attr.elemental;
6669 }
6670
6671
6672 /* Warn about unused labels. */
6673
6674 static void
6675 warn_unused_fortran_label (gfc_st_label * label)
6676 {
6677 if (label == NULL)
6678 return;
6679
6680 warn_unused_fortran_label (label->left);
6681
6682 if (label->defined == ST_LABEL_UNKNOWN)
6683 return;
6684
6685 switch (label->referenced)
6686 {
6687 case ST_LABEL_UNKNOWN:
6688 gfc_warning ("Label %d at %L defined but not used", label->value,
6689 &label->where);
6690 break;
6691
6692 case ST_LABEL_BAD_TARGET:
6693 gfc_warning ("Label %d at %L defined but cannot be used",
6694 label->value, &label->where);
6695 break;
6696
6697 default:
6698 break;
6699 }
6700
6701 warn_unused_fortran_label (label->right);
6702 }
6703
6704
6705 /* Returns the sequence type of a symbol or sequence. */
6706
6707 static seq_type
6708 sequence_type (gfc_typespec ts)
6709 {
6710 seq_type result;
6711 gfc_component *c;
6712
6713 switch (ts.type)
6714 {
6715 case BT_DERIVED:
6716
6717 if (ts.derived->components == NULL)
6718 return SEQ_NONDEFAULT;
6719
6720 result = sequence_type (ts.derived->components->ts);
6721 for (c = ts.derived->components->next; c; c = c->next)
6722 if (sequence_type (c->ts) != result)
6723 return SEQ_MIXED;
6724
6725 return result;
6726
6727 case BT_CHARACTER:
6728 if (ts.kind != gfc_default_character_kind)
6729 return SEQ_NONDEFAULT;
6730
6731 return SEQ_CHARACTER;
6732
6733 case BT_INTEGER:
6734 if (ts.kind != gfc_default_integer_kind)
6735 return SEQ_NONDEFAULT;
6736
6737 return SEQ_NUMERIC;
6738
6739 case BT_REAL:
6740 if (!(ts.kind == gfc_default_real_kind
6741 || ts.kind == gfc_default_double_kind))
6742 return SEQ_NONDEFAULT;
6743
6744 return SEQ_NUMERIC;
6745
6746 case BT_COMPLEX:
6747 if (ts.kind != gfc_default_complex_kind)
6748 return SEQ_NONDEFAULT;
6749
6750 return SEQ_NUMERIC;
6751
6752 case BT_LOGICAL:
6753 if (ts.kind != gfc_default_logical_kind)
6754 return SEQ_NONDEFAULT;
6755
6756 return SEQ_NUMERIC;
6757
6758 default:
6759 return SEQ_NONDEFAULT;
6760 }
6761 }
6762
6763
6764 /* Resolve derived type EQUIVALENCE object. */
6765
6766 static try
6767 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
6768 {
6769 gfc_symbol *d;
6770 gfc_component *c = derived->components;
6771
6772 if (!derived)
6773 return SUCCESS;
6774
6775 /* Shall not be an object of nonsequence derived type. */
6776 if (!derived->attr.sequence)
6777 {
6778 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
6779 "attribute to be an EQUIVALENCE object", sym->name, &e->where);
6780 return FAILURE;
6781 }
6782
6783 /* Shall not have allocatable components. */
6784 if (derived->attr.alloc_comp)
6785 {
6786 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
6787 "components to be an EQUIVALENCE object",sym->name, &e->where);
6788 return FAILURE;
6789 }
6790
6791 for (; c ; c = c->next)
6792 {
6793 d = c->ts.derived;
6794 if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
6795 return FAILURE;
6796
6797 /* Shall not be an object of sequence derived type containing a pointer
6798 in the structure. */
6799 if (c->pointer)
6800 {
6801 gfc_error ("Derived type variable '%s' at %L with pointer component(s) "
6802 "cannot be an EQUIVALENCE object", sym->name, &e->where);
6803 return FAILURE;
6804 }
6805
6806 if (c->initializer)
6807 {
6808 gfc_error ("Derived type variable '%s' at %L with default initializer "
6809 "cannot be an EQUIVALENCE object", sym->name, &e->where);
6810 return FAILURE;
6811 }
6812 }
6813 return SUCCESS;
6814 }
6815
6816
6817 /* Resolve equivalence object.
6818 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
6819 an allocatable array, an object of nonsequence derived type, an object of
6820 sequence derived type containing a pointer at any level of component
6821 selection, an automatic object, a function name, an entry name, a result
6822 name, a named constant, a structure component, or a subobject of any of
6823 the preceding objects. A substring shall not have length zero. A
6824 derived type shall not have components with default initialization nor
6825 shall two objects of an equivalence group be initialized.
6826 Either all or none of the objects shall have an protected attribute.
6827 The simple constraints are done in symbol.c(check_conflict) and the rest
6828 are implemented here. */
6829
6830 static void
6831 resolve_equivalence (gfc_equiv *eq)
6832 {
6833 gfc_symbol *sym;
6834 gfc_symbol *derived;
6835 gfc_symbol *first_sym;
6836 gfc_expr *e;
6837 gfc_ref *r;
6838 locus *last_where = NULL;
6839 seq_type eq_type, last_eq_type;
6840 gfc_typespec *last_ts;
6841 int object, cnt_protected;
6842 const char *value_name;
6843 const char *msg;
6844
6845 value_name = NULL;
6846 last_ts = &eq->expr->symtree->n.sym->ts;
6847
6848 first_sym = eq->expr->symtree->n.sym;
6849
6850 cnt_protected = 0;
6851
6852 for (object = 1; eq; eq = eq->eq, object++)
6853 {
6854 e = eq->expr;
6855
6856 e->ts = e->symtree->n.sym->ts;
6857 /* match_varspec might not know yet if it is seeing
6858 array reference or substring reference, as it doesn't
6859 know the types. */
6860 if (e->ref && e->ref->type == REF_ARRAY)
6861 {
6862 gfc_ref *ref = e->ref;
6863 sym = e->symtree->n.sym;
6864
6865 if (sym->attr.dimension)
6866 {
6867 ref->u.ar.as = sym->as;
6868 ref = ref->next;
6869 }
6870
6871 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
6872 if (e->ts.type == BT_CHARACTER
6873 && ref
6874 && ref->type == REF_ARRAY
6875 && ref->u.ar.dimen == 1
6876 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
6877 && ref->u.ar.stride[0] == NULL)
6878 {
6879 gfc_expr *start = ref->u.ar.start[0];
6880 gfc_expr *end = ref->u.ar.end[0];
6881 void *mem = NULL;
6882
6883 /* Optimize away the (:) reference. */
6884 if (start == NULL && end == NULL)
6885 {
6886 if (e->ref == ref)
6887 e->ref = ref->next;
6888 else
6889 e->ref->next = ref->next;
6890 mem = ref;
6891 }
6892 else
6893 {
6894 ref->type = REF_SUBSTRING;
6895 if (start == NULL)
6896 start = gfc_int_expr (1);
6897 ref->u.ss.start = start;
6898 if (end == NULL && e->ts.cl)
6899 end = gfc_copy_expr (e->ts.cl->length);
6900 ref->u.ss.end = end;
6901 ref->u.ss.length = e->ts.cl;
6902 e->ts.cl = NULL;
6903 }
6904 ref = ref->next;
6905 gfc_free (mem);
6906 }
6907
6908 /* Any further ref is an error. */
6909 if (ref)
6910 {
6911 gcc_assert (ref->type == REF_ARRAY);
6912 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
6913 &ref->u.ar.where);
6914 continue;
6915 }
6916 }
6917
6918 if (gfc_resolve_expr (e) == FAILURE)
6919 continue;
6920
6921 sym = e->symtree->n.sym;
6922
6923 if (sym->attr.protected)
6924 cnt_protected++;
6925 if (cnt_protected > 0 && cnt_protected != object)
6926 {
6927 gfc_error ("Either all or none of the objects in the "
6928 "EQUIVALENCE set at %L shall have the "
6929 "PROTECTED attribute",
6930 &e->where);
6931 break;
6932 }
6933
6934 /* An equivalence statement cannot have more than one initialized
6935 object. */
6936 if (sym->value)
6937 {
6938 if (value_name != NULL)
6939 {
6940 gfc_error ("Initialized objects '%s' and '%s' cannot both "
6941 "be in the EQUIVALENCE statement at %L",
6942 value_name, sym->name, &e->where);
6943 continue;
6944 }
6945 else
6946 value_name = sym->name;
6947 }
6948
6949 /* Shall not equivalence common block variables in a PURE procedure. */
6950 if (sym->ns->proc_name
6951 && sym->ns->proc_name->attr.pure
6952 && sym->attr.in_common)
6953 {
6954 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
6955 "object in the pure procedure '%s'",
6956 sym->name, &e->where, sym->ns->proc_name->name);
6957 break;
6958 }
6959
6960 /* Shall not be a named constant. */
6961 if (e->expr_type == EXPR_CONSTANT)
6962 {
6963 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
6964 "object", sym->name, &e->where);
6965 continue;
6966 }
6967
6968 derived = e->ts.derived;
6969 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
6970 continue;
6971
6972 /* Check that the types correspond correctly:
6973 Note 5.28:
6974 A numeric sequence structure may be equivalenced to another sequence
6975 structure, an object of default integer type, default real type, double
6976 precision real type, default logical type such that components of the
6977 structure ultimately only become associated to objects of the same
6978 kind. A character sequence structure may be equivalenced to an object
6979 of default character kind or another character sequence structure.
6980 Other objects may be equivalenced only to objects of the same type and
6981 kind parameters. */
6982
6983 /* Identical types are unconditionally OK. */
6984 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
6985 goto identical_types;
6986
6987 last_eq_type = sequence_type (*last_ts);
6988 eq_type = sequence_type (sym->ts);
6989
6990 /* Since the pair of objects is not of the same type, mixed or
6991 non-default sequences can be rejected. */
6992
6993 msg = "Sequence %s with mixed components in EQUIVALENCE "
6994 "statement at %L with different type objects";
6995 if ((object ==2
6996 && last_eq_type == SEQ_MIXED
6997 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
6998 last_where) == FAILURE)
6999 || (eq_type == SEQ_MIXED
7000 && gfc_notify_std (GFC_STD_GNU, msg,sym->name,
7001 &e->where) == FAILURE))
7002 continue;
7003
7004 msg = "Non-default type object or sequence %s in EQUIVALENCE "
7005 "statement at %L with objects of different type";
7006 if ((object ==2
7007 && last_eq_type == SEQ_NONDEFAULT
7008 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
7009 last_where) == FAILURE)
7010 || (eq_type == SEQ_NONDEFAULT
7011 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
7012 &e->where) == FAILURE))
7013 continue;
7014
7015 msg ="Non-CHARACTER object '%s' in default CHARACTER "
7016 "EQUIVALENCE statement at %L";
7017 if (last_eq_type == SEQ_CHARACTER
7018 && eq_type != SEQ_CHARACTER
7019 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
7020 &e->where) == FAILURE)
7021 continue;
7022
7023 msg ="Non-NUMERIC object '%s' in default NUMERIC "
7024 "EQUIVALENCE statement at %L";
7025 if (last_eq_type == SEQ_NUMERIC
7026 && eq_type != SEQ_NUMERIC
7027 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
7028 &e->where) == FAILURE)
7029 continue;
7030
7031 identical_types:
7032 last_ts =&sym->ts;
7033 last_where = &e->where;
7034
7035 if (!e->ref)
7036 continue;
7037
7038 /* Shall not be an automatic array. */
7039 if (e->ref->type == REF_ARRAY
7040 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
7041 {
7042 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
7043 "an EQUIVALENCE object", sym->name, &e->where);
7044 continue;
7045 }
7046
7047 r = e->ref;
7048 while (r)
7049 {
7050 /* Shall not be a structure component. */
7051 if (r->type == REF_COMPONENT)
7052 {
7053 gfc_error ("Structure component '%s' at %L cannot be an "
7054 "EQUIVALENCE object",
7055 r->u.c.component->name, &e->where);
7056 break;
7057 }
7058
7059 /* A substring shall not have length zero. */
7060 if (r->type == REF_SUBSTRING)
7061 {
7062 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
7063 {
7064 gfc_error ("Substring at %L has length zero",
7065 &r->u.ss.start->where);
7066 break;
7067 }
7068 }
7069 r = r->next;
7070 }
7071 }
7072 }
7073
7074
7075 /* Resolve function and ENTRY types, issue diagnostics if needed. */
7076
7077 static void
7078 resolve_fntype (gfc_namespace * ns)
7079 {
7080 gfc_entry_list *el;
7081 gfc_symbol *sym;
7082
7083 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
7084 return;
7085
7086 /* If there are any entries, ns->proc_name is the entry master
7087 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
7088 if (ns->entries)
7089 sym = ns->entries->sym;
7090 else
7091 sym = ns->proc_name;
7092 if (sym->result == sym
7093 && sym->ts.type == BT_UNKNOWN
7094 && gfc_set_default_type (sym, 0, NULL) == FAILURE
7095 && !sym->attr.untyped)
7096 {
7097 gfc_error ("Function '%s' at %L has no IMPLICIT type",
7098 sym->name, &sym->declared_at);
7099 sym->attr.untyped = 1;
7100 }
7101
7102 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
7103 && !gfc_check_access (sym->ts.derived->attr.access,
7104 sym->ts.derived->ns->default_access)
7105 && gfc_check_access (sym->attr.access, sym->ns->default_access))
7106 {
7107 gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
7108 sym->name, &sym->declared_at, sym->ts.derived->name);
7109 }
7110
7111 /* Make sure that the type of a module derived type function is in the
7112 module namespace, by copying it from the namespace's derived type
7113 list, if necessary. */
7114 if (sym->ts.type == BT_DERIVED
7115 && sym->ns->proc_name->attr.flavor == FL_MODULE
7116 && sym->ts.derived->ns
7117 && sym->ns != sym->ts.derived->ns)
7118 {
7119 gfc_dt_list *dt = sym->ns->derived_types;
7120
7121 for (; dt; dt = dt->next)
7122 if (gfc_compare_derived_types (sym->ts.derived, dt->derived))
7123 sym->ts.derived = dt->derived;
7124 }
7125
7126 if (ns->entries)
7127 for (el = ns->entries->next; el; el = el->next)
7128 {
7129 if (el->sym->result == el->sym
7130 && el->sym->ts.type == BT_UNKNOWN
7131 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
7132 && !el->sym->attr.untyped)
7133 {
7134 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
7135 el->sym->name, &el->sym->declared_at);
7136 el->sym->attr.untyped = 1;
7137 }
7138 }
7139 }
7140
7141 /* 12.3.2.1.1 Defined operators. */
7142
7143 static void
7144 gfc_resolve_uops(gfc_symtree *symtree)
7145 {
7146 gfc_interface *itr;
7147 gfc_symbol *sym;
7148 gfc_formal_arglist *formal;
7149
7150 if (symtree == NULL)
7151 return;
7152
7153 gfc_resolve_uops (symtree->left);
7154 gfc_resolve_uops (symtree->right);
7155
7156 for (itr = symtree->n.uop->operator; itr; itr = itr->next)
7157 {
7158 sym = itr->sym;
7159 if (!sym->attr.function)
7160 gfc_error("User operator procedure '%s' at %L must be a FUNCTION",
7161 sym->name, &sym->declared_at);
7162
7163 if (sym->ts.type == BT_CHARACTER
7164 && !(sym->ts.cl && sym->ts.cl->length)
7165 && !(sym->result && sym->result->ts.cl && sym->result->ts.cl->length))
7166 gfc_error("User operator procedure '%s' at %L cannot be assumed character "
7167 "length", sym->name, &sym->declared_at);
7168
7169 formal = sym->formal;
7170 if (!formal || !formal->sym)
7171 {
7172 gfc_error("User operator procedure '%s' at %L must have at least "
7173 "one argument", sym->name, &sym->declared_at);
7174 continue;
7175 }
7176
7177 if (formal->sym->attr.intent != INTENT_IN)
7178 gfc_error ("First argument of operator interface at %L must be "
7179 "INTENT(IN)", &sym->declared_at);
7180
7181 if (formal->sym->attr.optional)
7182 gfc_error ("First argument of operator interface at %L cannot be "
7183 "optional", &sym->declared_at);
7184
7185 formal = formal->next;
7186 if (!formal || !formal->sym)
7187 continue;
7188
7189 if (formal->sym->attr.intent != INTENT_IN)
7190 gfc_error ("Second argument of operator interface at %L must be "
7191 "INTENT(IN)", &sym->declared_at);
7192
7193 if (formal->sym->attr.optional)
7194 gfc_error ("Second argument of operator interface at %L cannot be "
7195 "optional", &sym->declared_at);
7196
7197 if (formal->next)
7198 gfc_error ("Operator interface at %L must have, at most, two "
7199 "arguments", &sym->declared_at);
7200 }
7201 }
7202
7203
7204 /* Examine all of the expressions associated with a program unit,
7205 assign types to all intermediate expressions, make sure that all
7206 assignments are to compatible types and figure out which names
7207 refer to which functions or subroutines. It doesn't check code
7208 block, which is handled by resolve_code. */
7209
7210 static void
7211 resolve_types (gfc_namespace * ns)
7212 {
7213 gfc_namespace *n;
7214 gfc_charlen *cl;
7215 gfc_data *d;
7216 gfc_equiv *eq;
7217
7218 gfc_current_ns = ns;
7219
7220 resolve_entries (ns);
7221
7222 resolve_contained_functions (ns);
7223
7224 gfc_traverse_ns (ns, resolve_symbol);
7225
7226 resolve_fntype (ns);
7227
7228 for (n = ns->contained; n; n = n->sibling)
7229 {
7230 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
7231 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
7232 "also be PURE", n->proc_name->name,
7233 &n->proc_name->declared_at);
7234
7235 resolve_types (n);
7236 }
7237
7238 forall_flag = 0;
7239 gfc_check_interfaces (ns);
7240
7241 for (cl = ns->cl_list; cl; cl = cl->next)
7242 resolve_charlen (cl);
7243
7244 gfc_traverse_ns (ns, resolve_values);
7245
7246 if (ns->save_all)
7247 gfc_save_all (ns);
7248
7249 iter_stack = NULL;
7250 for (d = ns->data; d; d = d->next)
7251 resolve_data (d);
7252
7253 iter_stack = NULL;
7254 gfc_traverse_ns (ns, gfc_formalize_init_value);
7255
7256 for (eq = ns->equiv; eq; eq = eq->next)
7257 resolve_equivalence (eq);
7258
7259 /* Warn about unused labels. */
7260 if (warn_unused_label)
7261 warn_unused_fortran_label (ns->st_labels);
7262
7263 gfc_resolve_uops (ns->uop_root);
7264 }
7265
7266
7267 /* Call resolve_code recursively. */
7268
7269 static void
7270 resolve_codes (gfc_namespace * ns)
7271 {
7272 gfc_namespace *n;
7273
7274 for (n = ns->contained; n; n = n->sibling)
7275 resolve_codes (n);
7276
7277 gfc_current_ns = ns;
7278 cs_base = NULL;
7279 /* Set to an out of range value. */
7280 current_entry_id = -1;
7281 resolve_code (ns->code, ns);
7282 }
7283
7284
7285 /* This function is called after a complete program unit has been compiled.
7286 Its purpose is to examine all of the expressions associated with a program
7287 unit, assign types to all intermediate expressions, make sure that all
7288 assignments are to compatible types and figure out which names refer to
7289 which functions or subroutines. */
7290
7291 void
7292 gfc_resolve (gfc_namespace * ns)
7293 {
7294 gfc_namespace *old_ns;
7295
7296 old_ns = gfc_current_ns;
7297
7298 resolve_types (ns);
7299 resolve_codes (ns);
7300
7301 gfc_current_ns = old_ns;
7302 }
This page took 0.370045 seconds and 5 git commands to generate.