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