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