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