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