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