re PR fortran/48112 (generic interface to external function in module)
[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 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "arith.h" /* For gfc_compare_expr(). */
30 #include "dependency.h"
31 #include "data.h"
32 #include "target-memory.h" /* for gfc_simplify_transfer */
33 #include "constructor.h"
34
35 /* Types used in equivalence statements. */
36
37 typedef enum seq_type
38 {
39 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 }
41 seq_type;
42
43 /* Stack to keep track of the nesting of blocks as we move through the
44 code. See resolve_branch() and resolve_code(). */
45
46 typedef struct code_stack
47 {
48 struct gfc_code *head, *current;
49 struct code_stack *prev;
50
51 /* This bitmap keeps track of the targets valid for a branch from
52 inside this block except for END {IF|SELECT}s of enclosing
53 blocks. */
54 bitmap reachable_labels;
55 }
56 code_stack;
57
58 static code_stack *cs_base = NULL;
59
60
61 /* Nonzero if we're inside a FORALL block. */
62
63 static int forall_flag;
64
65 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
66
67 static int omp_workshare_flag;
68
69 /* Nonzero if we are processing a formal arglist. The corresponding function
70 resets the flag each time that it is read. */
71 static int formal_arg_flag = 0;
72
73 /* True if we are resolving a specification expression. */
74 static int specification_expr = 0;
75
76 /* The id of the last entry seen. */
77 static int current_entry_id;
78
79 /* We use bitmaps to determine if a branch target is valid. */
80 static bitmap_obstack labels_obstack;
81
82 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
83 static bool inquiry_argument = false;
84
85 int
86 gfc_is_formal_arg (void)
87 {
88 return formal_arg_flag;
89 }
90
91 /* Is the symbol host associated? */
92 static bool
93 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
94 {
95 for (ns = ns->parent; ns; ns = ns->parent)
96 {
97 if (sym->ns == ns)
98 return true;
99 }
100
101 return false;
102 }
103
104 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
105 an ABSTRACT derived-type. If where is not NULL, an error message with that
106 locus is printed, optionally using name. */
107
108 static gfc_try
109 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
110 {
111 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
112 {
113 if (where)
114 {
115 if (name)
116 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
117 name, where, ts->u.derived->name);
118 else
119 gfc_error ("ABSTRACT type '%s' used at %L",
120 ts->u.derived->name, where);
121 }
122
123 return FAILURE;
124 }
125
126 return SUCCESS;
127 }
128
129
130 static void resolve_symbol (gfc_symbol *sym);
131 static gfc_try resolve_intrinsic (gfc_symbol *sym, locus *loc);
132
133
134 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
135
136 static gfc_try
137 resolve_procedure_interface (gfc_symbol *sym)
138 {
139 if (sym->ts.interface == sym)
140 {
141 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
142 sym->name, &sym->declared_at);
143 return FAILURE;
144 }
145 if (sym->ts.interface->attr.procedure)
146 {
147 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
148 "in a later PROCEDURE statement", sym->ts.interface->name,
149 sym->name, &sym->declared_at);
150 return FAILURE;
151 }
152
153 /* Get the attributes from the interface (now resolved). */
154 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
155 {
156 gfc_symbol *ifc = sym->ts.interface;
157 resolve_symbol (ifc);
158
159 if (ifc->attr.intrinsic)
160 resolve_intrinsic (ifc, &ifc->declared_at);
161
162 if (ifc->result)
163 {
164 sym->ts = ifc->result->ts;
165 sym->result = sym;
166 }
167 else
168 sym->ts = ifc->ts;
169 sym->ts.interface = ifc;
170 sym->attr.function = ifc->attr.function;
171 sym->attr.subroutine = ifc->attr.subroutine;
172 gfc_copy_formal_args (sym, ifc);
173
174 sym->attr.allocatable = ifc->attr.allocatable;
175 sym->attr.pointer = ifc->attr.pointer;
176 sym->attr.pure = ifc->attr.pure;
177 sym->attr.elemental = ifc->attr.elemental;
178 sym->attr.dimension = ifc->attr.dimension;
179 sym->attr.contiguous = ifc->attr.contiguous;
180 sym->attr.recursive = ifc->attr.recursive;
181 sym->attr.always_explicit = ifc->attr.always_explicit;
182 sym->attr.ext_attr |= ifc->attr.ext_attr;
183 sym->attr.is_bind_c = ifc->attr.is_bind_c;
184 /* Copy array spec. */
185 sym->as = gfc_copy_array_spec (ifc->as);
186 if (sym->as)
187 {
188 int i;
189 for (i = 0; i < sym->as->rank; i++)
190 {
191 gfc_expr_replace_symbols (sym->as->lower[i], sym);
192 gfc_expr_replace_symbols (sym->as->upper[i], sym);
193 }
194 }
195 /* Copy char length. */
196 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
197 {
198 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
199 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
200 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
201 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
202 return FAILURE;
203 }
204 }
205 else if (sym->ts.interface->name[0] != '\0')
206 {
207 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
208 sym->ts.interface->name, sym->name, &sym->declared_at);
209 return FAILURE;
210 }
211
212 return SUCCESS;
213 }
214
215
216 /* Resolve types of formal argument lists. These have to be done early so that
217 the formal argument lists of module procedures can be copied to the
218 containing module before the individual procedures are resolved
219 individually. We also resolve argument lists of procedures in interface
220 blocks because they are self-contained scoping units.
221
222 Since a dummy argument cannot be a non-dummy procedure, the only
223 resort left for untyped names are the IMPLICIT types. */
224
225 static void
226 resolve_formal_arglist (gfc_symbol *proc)
227 {
228 gfc_formal_arglist *f;
229 gfc_symbol *sym;
230 int i;
231
232 if (proc->result != NULL)
233 sym = proc->result;
234 else
235 sym = proc;
236
237 if (gfc_elemental (proc)
238 || sym->attr.pointer || sym->attr.allocatable
239 || (sym->as && sym->as->rank > 0))
240 {
241 proc->attr.always_explicit = 1;
242 sym->attr.always_explicit = 1;
243 }
244
245 formal_arg_flag = 1;
246
247 for (f = proc->formal; f; f = f->next)
248 {
249 sym = f->sym;
250
251 if (sym == NULL)
252 {
253 /* Alternate return placeholder. */
254 if (gfc_elemental (proc))
255 gfc_error ("Alternate return specifier in elemental subroutine "
256 "'%s' at %L is not allowed", proc->name,
257 &proc->declared_at);
258 if (proc->attr.function)
259 gfc_error ("Alternate return specifier in function "
260 "'%s' at %L is not allowed", proc->name,
261 &proc->declared_at);
262 continue;
263 }
264 else if (sym->attr.procedure && sym->ts.interface
265 && sym->attr.if_source != IFSRC_DECL)
266 resolve_procedure_interface (sym);
267
268 if (sym->attr.if_source != IFSRC_UNKNOWN)
269 resolve_formal_arglist (sym);
270
271 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
272 {
273 if (gfc_pure (proc) && !gfc_pure (sym))
274 {
275 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
276 "also be PURE", sym->name, &sym->declared_at);
277 continue;
278 }
279
280 if (proc->attr.implicit_pure && !gfc_pure(sym))
281 proc->attr.implicit_pure = 0;
282
283 if (gfc_elemental (proc))
284 {
285 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
286 "procedure", &sym->declared_at);
287 continue;
288 }
289
290 if (sym->attr.function
291 && sym->ts.type == BT_UNKNOWN
292 && sym->attr.intrinsic)
293 {
294 gfc_intrinsic_sym *isym;
295 isym = gfc_find_function (sym->name);
296 if (isym == NULL || !isym->specific)
297 {
298 gfc_error ("Unable to find a specific INTRINSIC procedure "
299 "for the reference '%s' at %L", sym->name,
300 &sym->declared_at);
301 }
302 sym->ts = isym->ts;
303 }
304
305 continue;
306 }
307
308 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
309 && (!sym->attr.function || sym->result == sym))
310 gfc_set_default_type (sym, 1, sym->ns);
311
312 gfc_resolve_array_spec (sym->as, 0);
313
314 /* We can't tell if an array with dimension (:) is assumed or deferred
315 shape until we know if it has the pointer or allocatable attributes.
316 */
317 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
318 && !(sym->attr.pointer || sym->attr.allocatable))
319 {
320 sym->as->type = AS_ASSUMED_SHAPE;
321 for (i = 0; i < sym->as->rank; i++)
322 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
323 NULL, 1);
324 }
325
326 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
327 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
328 || sym->attr.optional)
329 {
330 proc->attr.always_explicit = 1;
331 if (proc->result)
332 proc->result->attr.always_explicit = 1;
333 }
334
335 /* If the flavor is unknown at this point, it has to be a variable.
336 A procedure specification would have already set the type. */
337
338 if (sym->attr.flavor == FL_UNKNOWN)
339 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
340
341 if (gfc_pure (proc) && !sym->attr.pointer
342 && sym->attr.flavor != FL_PROCEDURE)
343 {
344 if (proc->attr.function && sym->attr.intent != INTENT_IN)
345 {
346 if (sym->attr.value)
347 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
348 "of pure function '%s' at %L with VALUE "
349 "attribute but without INTENT(IN)", sym->name,
350 proc->name, &sym->declared_at);
351 else
352 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
353 "INTENT(IN) or VALUE", sym->name, proc->name,
354 &sym->declared_at);
355 }
356
357 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
358 {
359 if (sym->attr.value)
360 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
361 "of pure subroutine '%s' at %L with VALUE "
362 "attribute but without INTENT", sym->name,
363 proc->name, &sym->declared_at);
364 else
365 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
366 "have its INTENT specified or have the VALUE "
367 "attribute", sym->name, proc->name, &sym->declared_at);
368 }
369 }
370
371 if (proc->attr.implicit_pure && !sym->attr.pointer
372 && sym->attr.flavor != FL_PROCEDURE)
373 {
374 if (proc->attr.function && sym->attr.intent != INTENT_IN)
375 proc->attr.implicit_pure = 0;
376
377 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
378 proc->attr.implicit_pure = 0;
379 }
380
381 if (gfc_elemental (proc))
382 {
383 /* F2008, C1289. */
384 if (sym->attr.codimension)
385 {
386 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
387 "procedure", sym->name, &sym->declared_at);
388 continue;
389 }
390
391 if (sym->as != NULL)
392 {
393 gfc_error ("Argument '%s' of elemental procedure at %L must "
394 "be scalar", sym->name, &sym->declared_at);
395 continue;
396 }
397
398 if (sym->attr.allocatable)
399 {
400 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
401 "have the ALLOCATABLE attribute", sym->name,
402 &sym->declared_at);
403 continue;
404 }
405
406 if (sym->attr.pointer)
407 {
408 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
409 "have the POINTER attribute", sym->name,
410 &sym->declared_at);
411 continue;
412 }
413
414 if (sym->attr.flavor == FL_PROCEDURE)
415 {
416 gfc_error ("Dummy procedure '%s' not allowed in elemental "
417 "procedure '%s' at %L", sym->name, proc->name,
418 &sym->declared_at);
419 continue;
420 }
421
422 if (sym->attr.intent == INTENT_UNKNOWN)
423 {
424 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
425 "have its INTENT specified", sym->name, proc->name,
426 &sym->declared_at);
427 continue;
428 }
429 }
430
431 /* Each dummy shall be specified to be scalar. */
432 if (proc->attr.proc == PROC_ST_FUNCTION)
433 {
434 if (sym->as != NULL)
435 {
436 gfc_error ("Argument '%s' of statement function at %L must "
437 "be scalar", sym->name, &sym->declared_at);
438 continue;
439 }
440
441 if (sym->ts.type == BT_CHARACTER)
442 {
443 gfc_charlen *cl = sym->ts.u.cl;
444 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
445 {
446 gfc_error ("Character-valued argument '%s' of statement "
447 "function at %L must have constant length",
448 sym->name, &sym->declared_at);
449 continue;
450 }
451 }
452 }
453 }
454 formal_arg_flag = 0;
455 }
456
457
458 /* Work function called when searching for symbols that have argument lists
459 associated with them. */
460
461 static void
462 find_arglists (gfc_symbol *sym)
463 {
464 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
465 return;
466
467 resolve_formal_arglist (sym);
468 }
469
470
471 /* Given a namespace, resolve all formal argument lists within the namespace.
472 */
473
474 static void
475 resolve_formal_arglists (gfc_namespace *ns)
476 {
477 if (ns == NULL)
478 return;
479
480 gfc_traverse_ns (ns, find_arglists);
481 }
482
483
484 static void
485 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
486 {
487 gfc_try t;
488
489 /* If this namespace is not a function or an entry master function,
490 ignore it. */
491 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
492 || sym->attr.entry_master)
493 return;
494
495 /* Try to find out of what the return type is. */
496 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
497 {
498 t = gfc_set_default_type (sym->result, 0, ns);
499
500 if (t == FAILURE && !sym->result->attr.untyped)
501 {
502 if (sym->result == sym)
503 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
504 sym->name, &sym->declared_at);
505 else if (!sym->result->attr.proc_pointer)
506 gfc_error ("Result '%s' of contained function '%s' at %L has "
507 "no IMPLICIT type", sym->result->name, sym->name,
508 &sym->result->declared_at);
509 sym->result->attr.untyped = 1;
510 }
511 }
512
513 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
514 type, lists the only ways a character length value of * can be used:
515 dummy arguments of procedures, named constants, and function results
516 in external functions. Internal function results and results of module
517 procedures are not on this list, ergo, not permitted. */
518
519 if (sym->result->ts.type == BT_CHARACTER)
520 {
521 gfc_charlen *cl = sym->result->ts.u.cl;
522 if ((!cl || !cl->length) && !sym->result->ts.deferred)
523 {
524 /* See if this is a module-procedure and adapt error message
525 accordingly. */
526 bool module_proc;
527 gcc_assert (ns->parent && ns->parent->proc_name);
528 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
529
530 gfc_error ("Character-valued %s '%s' at %L must not be"
531 " assumed length",
532 module_proc ? _("module procedure")
533 : _("internal function"),
534 sym->name, &sym->declared_at);
535 }
536 }
537 }
538
539
540 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
541 introduce duplicates. */
542
543 static void
544 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
545 {
546 gfc_formal_arglist *f, *new_arglist;
547 gfc_symbol *new_sym;
548
549 for (; new_args != NULL; new_args = new_args->next)
550 {
551 new_sym = new_args->sym;
552 /* See if this arg is already in the formal argument list. */
553 for (f = proc->formal; f; f = f->next)
554 {
555 if (new_sym == f->sym)
556 break;
557 }
558
559 if (f)
560 continue;
561
562 /* Add a new argument. Argument order is not important. */
563 new_arglist = gfc_get_formal_arglist ();
564 new_arglist->sym = new_sym;
565 new_arglist->next = proc->formal;
566 proc->formal = new_arglist;
567 }
568 }
569
570
571 /* Flag the arguments that are not present in all entries. */
572
573 static void
574 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
575 {
576 gfc_formal_arglist *f, *head;
577 head = new_args;
578
579 for (f = proc->formal; f; f = f->next)
580 {
581 if (f->sym == NULL)
582 continue;
583
584 for (new_args = head; new_args; new_args = new_args->next)
585 {
586 if (new_args->sym == f->sym)
587 break;
588 }
589
590 if (new_args)
591 continue;
592
593 f->sym->attr.not_always_present = 1;
594 }
595 }
596
597
598 /* Resolve alternate entry points. If a symbol has multiple entry points we
599 create a new master symbol for the main routine, and turn the existing
600 symbol into an entry point. */
601
602 static void
603 resolve_entries (gfc_namespace *ns)
604 {
605 gfc_namespace *old_ns;
606 gfc_code *c;
607 gfc_symbol *proc;
608 gfc_entry_list *el;
609 char name[GFC_MAX_SYMBOL_LEN + 1];
610 static int master_count = 0;
611
612 if (ns->proc_name == NULL)
613 return;
614
615 /* No need to do anything if this procedure doesn't have alternate entry
616 points. */
617 if (!ns->entries)
618 return;
619
620 /* We may already have resolved alternate entry points. */
621 if (ns->proc_name->attr.entry_master)
622 return;
623
624 /* If this isn't a procedure something has gone horribly wrong. */
625 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
626
627 /* Remember the current namespace. */
628 old_ns = gfc_current_ns;
629
630 gfc_current_ns = ns;
631
632 /* Add the main entry point to the list of entry points. */
633 el = gfc_get_entry_list ();
634 el->sym = ns->proc_name;
635 el->id = 0;
636 el->next = ns->entries;
637 ns->entries = el;
638 ns->proc_name->attr.entry = 1;
639
640 /* If it is a module function, it needs to be in the right namespace
641 so that gfc_get_fake_result_decl can gather up the results. The
642 need for this arose in get_proc_name, where these beasts were
643 left in their own namespace, to keep prior references linked to
644 the entry declaration.*/
645 if (ns->proc_name->attr.function
646 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
647 el->sym->ns = ns;
648
649 /* Do the same for entries where the master is not a module
650 procedure. These are retained in the module namespace because
651 of the module procedure declaration. */
652 for (el = el->next; el; el = el->next)
653 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
654 && el->sym->attr.mod_proc)
655 el->sym->ns = ns;
656 el = ns->entries;
657
658 /* Add an entry statement for it. */
659 c = gfc_get_code ();
660 c->op = EXEC_ENTRY;
661 c->ext.entry = el;
662 c->next = ns->code;
663 ns->code = c;
664
665 /* Create a new symbol for the master function. */
666 /* Give the internal function a unique name (within this file).
667 Also include the function name so the user has some hope of figuring
668 out what is going on. */
669 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
670 master_count++, ns->proc_name->name);
671 gfc_get_ha_symbol (name, &proc);
672 gcc_assert (proc != NULL);
673
674 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
675 if (ns->proc_name->attr.subroutine)
676 gfc_add_subroutine (&proc->attr, proc->name, NULL);
677 else
678 {
679 gfc_symbol *sym;
680 gfc_typespec *ts, *fts;
681 gfc_array_spec *as, *fas;
682 gfc_add_function (&proc->attr, proc->name, NULL);
683 proc->result = proc;
684 fas = ns->entries->sym->as;
685 fas = fas ? fas : ns->entries->sym->result->as;
686 fts = &ns->entries->sym->result->ts;
687 if (fts->type == BT_UNKNOWN)
688 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
689 for (el = ns->entries->next; el; el = el->next)
690 {
691 ts = &el->sym->result->ts;
692 as = el->sym->as;
693 as = as ? as : el->sym->result->as;
694 if (ts->type == BT_UNKNOWN)
695 ts = gfc_get_default_type (el->sym->result->name, NULL);
696
697 if (! gfc_compare_types (ts, fts)
698 || (el->sym->result->attr.dimension
699 != ns->entries->sym->result->attr.dimension)
700 || (el->sym->result->attr.pointer
701 != ns->entries->sym->result->attr.pointer))
702 break;
703 else if (as && fas && ns->entries->sym->result != el->sym->result
704 && gfc_compare_array_spec (as, fas) == 0)
705 gfc_error ("Function %s at %L has entries with mismatched "
706 "array specifications", ns->entries->sym->name,
707 &ns->entries->sym->declared_at);
708 /* The characteristics need to match and thus both need to have
709 the same string length, i.e. both len=*, or both len=4.
710 Having both len=<variable> is also possible, but difficult to
711 check at compile time. */
712 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
713 && (((ts->u.cl->length && !fts->u.cl->length)
714 ||(!ts->u.cl->length && fts->u.cl->length))
715 || (ts->u.cl->length
716 && ts->u.cl->length->expr_type
717 != fts->u.cl->length->expr_type)
718 || (ts->u.cl->length
719 && ts->u.cl->length->expr_type == EXPR_CONSTANT
720 && mpz_cmp (ts->u.cl->length->value.integer,
721 fts->u.cl->length->value.integer) != 0)))
722 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
723 "entries returning variables of different "
724 "string lengths", ns->entries->sym->name,
725 &ns->entries->sym->declared_at);
726 }
727
728 if (el == NULL)
729 {
730 sym = ns->entries->sym->result;
731 /* All result types the same. */
732 proc->ts = *fts;
733 if (sym->attr.dimension)
734 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
735 if (sym->attr.pointer)
736 gfc_add_pointer (&proc->attr, NULL);
737 }
738 else
739 {
740 /* Otherwise the result will be passed through a union by
741 reference. */
742 proc->attr.mixed_entry_master = 1;
743 for (el = ns->entries; el; el = el->next)
744 {
745 sym = el->sym->result;
746 if (sym->attr.dimension)
747 {
748 if (el == ns->entries)
749 gfc_error ("FUNCTION result %s can't be an array in "
750 "FUNCTION %s at %L", sym->name,
751 ns->entries->sym->name, &sym->declared_at);
752 else
753 gfc_error ("ENTRY result %s can't be an array in "
754 "FUNCTION %s at %L", sym->name,
755 ns->entries->sym->name, &sym->declared_at);
756 }
757 else if (sym->attr.pointer)
758 {
759 if (el == ns->entries)
760 gfc_error ("FUNCTION result %s can't be a POINTER in "
761 "FUNCTION %s at %L", sym->name,
762 ns->entries->sym->name, &sym->declared_at);
763 else
764 gfc_error ("ENTRY result %s can't be a POINTER in "
765 "FUNCTION %s at %L", sym->name,
766 ns->entries->sym->name, &sym->declared_at);
767 }
768 else
769 {
770 ts = &sym->ts;
771 if (ts->type == BT_UNKNOWN)
772 ts = gfc_get_default_type (sym->name, NULL);
773 switch (ts->type)
774 {
775 case BT_INTEGER:
776 if (ts->kind == gfc_default_integer_kind)
777 sym = NULL;
778 break;
779 case BT_REAL:
780 if (ts->kind == gfc_default_real_kind
781 || ts->kind == gfc_default_double_kind)
782 sym = NULL;
783 break;
784 case BT_COMPLEX:
785 if (ts->kind == gfc_default_complex_kind)
786 sym = NULL;
787 break;
788 case BT_LOGICAL:
789 if (ts->kind == gfc_default_logical_kind)
790 sym = NULL;
791 break;
792 case BT_UNKNOWN:
793 /* We will issue error elsewhere. */
794 sym = NULL;
795 break;
796 default:
797 break;
798 }
799 if (sym)
800 {
801 if (el == ns->entries)
802 gfc_error ("FUNCTION result %s can't be of type %s "
803 "in FUNCTION %s at %L", sym->name,
804 gfc_typename (ts), ns->entries->sym->name,
805 &sym->declared_at);
806 else
807 gfc_error ("ENTRY result %s can't be of type %s "
808 "in FUNCTION %s at %L", sym->name,
809 gfc_typename (ts), ns->entries->sym->name,
810 &sym->declared_at);
811 }
812 }
813 }
814 }
815 }
816 proc->attr.access = ACCESS_PRIVATE;
817 proc->attr.entry_master = 1;
818
819 /* Merge all the entry point arguments. */
820 for (el = ns->entries; el; el = el->next)
821 merge_argument_lists (proc, el->sym->formal);
822
823 /* Check the master formal arguments for any that are not
824 present in all entry points. */
825 for (el = ns->entries; el; el = el->next)
826 check_argument_lists (proc, el->sym->formal);
827
828 /* Use the master function for the function body. */
829 ns->proc_name = proc;
830
831 /* Finalize the new symbols. */
832 gfc_commit_symbols ();
833
834 /* Restore the original namespace. */
835 gfc_current_ns = old_ns;
836 }
837
838
839 /* Resolve common variables. */
840 static void
841 resolve_common_vars (gfc_symbol *sym, bool named_common)
842 {
843 gfc_symbol *csym = sym;
844
845 for (; csym; csym = csym->common_next)
846 {
847 if (csym->value || csym->attr.data)
848 {
849 if (!csym->ns->is_block_data)
850 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
851 "but only in BLOCK DATA initialization is "
852 "allowed", csym->name, &csym->declared_at);
853 else if (!named_common)
854 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
855 "in a blank COMMON but initialization is only "
856 "allowed in named common blocks", csym->name,
857 &csym->declared_at);
858 }
859
860 if (csym->ts.type != BT_DERIVED)
861 continue;
862
863 if (!(csym->ts.u.derived->attr.sequence
864 || csym->ts.u.derived->attr.is_bind_c))
865 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
866 "has neither the SEQUENCE nor the BIND(C) "
867 "attribute", csym->name, &csym->declared_at);
868 if (csym->ts.u.derived->attr.alloc_comp)
869 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
870 "has an ultimate component that is "
871 "allocatable", csym->name, &csym->declared_at);
872 if (gfc_has_default_initializer (csym->ts.u.derived))
873 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
874 "may not have default initializer", csym->name,
875 &csym->declared_at);
876
877 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
878 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
879 }
880 }
881
882 /* Resolve common blocks. */
883 static void
884 resolve_common_blocks (gfc_symtree *common_root)
885 {
886 gfc_symbol *sym;
887
888 if (common_root == NULL)
889 return;
890
891 if (common_root->left)
892 resolve_common_blocks (common_root->left);
893 if (common_root->right)
894 resolve_common_blocks (common_root->right);
895
896 resolve_common_vars (common_root->n.common->head, true);
897
898 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
899 if (sym == NULL)
900 return;
901
902 if (sym->attr.flavor == FL_PARAMETER)
903 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
904 sym->name, &common_root->n.common->where, &sym->declared_at);
905
906 if (sym->attr.intrinsic)
907 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
908 sym->name, &common_root->n.common->where);
909 else if (sym->attr.result
910 || gfc_is_function_return_value (sym, gfc_current_ns))
911 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
912 "that is also a function result", sym->name,
913 &common_root->n.common->where);
914 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
915 && sym->attr.proc != PROC_ST_FUNCTION)
916 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
917 "that is also a global procedure", sym->name,
918 &common_root->n.common->where);
919 }
920
921
922 /* Resolve contained function types. Because contained functions can call one
923 another, they have to be worked out before any of the contained procedures
924 can be resolved.
925
926 The good news is that if a function doesn't already have a type, the only
927 way it can get one is through an IMPLICIT type or a RESULT variable, because
928 by definition contained functions are contained namespace they're contained
929 in, not in a sibling or parent namespace. */
930
931 static void
932 resolve_contained_functions (gfc_namespace *ns)
933 {
934 gfc_namespace *child;
935 gfc_entry_list *el;
936
937 resolve_formal_arglists (ns);
938
939 for (child = ns->contained; child; child = child->sibling)
940 {
941 /* Resolve alternate entry points first. */
942 resolve_entries (child);
943
944 /* Then check function return types. */
945 resolve_contained_fntype (child->proc_name, child);
946 for (el = child->entries; el; el = el->next)
947 resolve_contained_fntype (el->sym, child);
948 }
949 }
950
951
952 /* Resolve all of the elements of a structure constructor and make sure that
953 the types are correct. The 'init' flag indicates that the given
954 constructor is an initializer. */
955
956 static gfc_try
957 resolve_structure_cons (gfc_expr *expr, int init)
958 {
959 gfc_constructor *cons;
960 gfc_component *comp;
961 gfc_try t;
962 symbol_attribute a;
963
964 t = SUCCESS;
965
966 if (expr->ts.type == BT_DERIVED)
967 resolve_symbol (expr->ts.u.derived);
968
969 cons = gfc_constructor_first (expr->value.constructor);
970 /* A constructor may have references if it is the result of substituting a
971 parameter variable. In this case we just pull out the component we
972 want. */
973 if (expr->ref)
974 comp = expr->ref->u.c.sym->components;
975 else
976 comp = expr->ts.u.derived->components;
977
978 /* See if the user is trying to invoke a structure constructor for one of
979 the iso_c_binding derived types. */
980 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
981 && expr->ts.u.derived->ts.is_iso_c && cons
982 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
983 {
984 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
985 expr->ts.u.derived->name, &(expr->where));
986 return FAILURE;
987 }
988
989 /* Return if structure constructor is c_null_(fun)prt. */
990 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
991 && expr->ts.u.derived->ts.is_iso_c && cons
992 && cons->expr && cons->expr->expr_type == EXPR_NULL)
993 return SUCCESS;
994
995 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
996 {
997 int rank;
998
999 if (!cons->expr)
1000 continue;
1001
1002 if (gfc_resolve_expr (cons->expr) == FAILURE)
1003 {
1004 t = FAILURE;
1005 continue;
1006 }
1007
1008 rank = comp->as ? comp->as->rank : 0;
1009 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1010 && (comp->attr.allocatable || cons->expr->rank))
1011 {
1012 gfc_error ("The rank of the element in the derived type "
1013 "constructor at %L does not match that of the "
1014 "component (%d/%d)", &cons->expr->where,
1015 cons->expr->rank, rank);
1016 t = FAILURE;
1017 }
1018
1019 /* If we don't have the right type, try to convert it. */
1020
1021 if (!comp->attr.proc_pointer &&
1022 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1023 {
1024 t = FAILURE;
1025 if (strcmp (comp->name, "_extends") == 0)
1026 {
1027 /* Can afford to be brutal with the _extends initializer.
1028 The derived type can get lost because it is PRIVATE
1029 but it is not usage constrained by the standard. */
1030 cons->expr->ts = comp->ts;
1031 t = SUCCESS;
1032 }
1033 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1034 gfc_error ("The element in the derived type constructor at %L, "
1035 "for pointer component '%s', is %s but should be %s",
1036 &cons->expr->where, comp->name,
1037 gfc_basic_typename (cons->expr->ts.type),
1038 gfc_basic_typename (comp->ts.type));
1039 else
1040 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1041 }
1042
1043 /* For strings, the length of the constructor should be the same as
1044 the one of the structure, ensure this if the lengths are known at
1045 compile time and when we are dealing with PARAMETER or structure
1046 constructors. */
1047 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1048 && comp->ts.u.cl->length
1049 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1050 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1051 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1052 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1053 comp->ts.u.cl->length->value.integer) != 0)
1054 {
1055 if (cons->expr->expr_type == EXPR_VARIABLE
1056 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1057 {
1058 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1059 to make use of the gfc_resolve_character_array_constructor
1060 machinery. The expression is later simplified away to
1061 an array of string literals. */
1062 gfc_expr *para = cons->expr;
1063 cons->expr = gfc_get_expr ();
1064 cons->expr->ts = para->ts;
1065 cons->expr->where = para->where;
1066 cons->expr->expr_type = EXPR_ARRAY;
1067 cons->expr->rank = para->rank;
1068 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1069 gfc_constructor_append_expr (&cons->expr->value.constructor,
1070 para, &cons->expr->where);
1071 }
1072 if (cons->expr->expr_type == EXPR_ARRAY)
1073 {
1074 gfc_constructor *p;
1075 p = gfc_constructor_first (cons->expr->value.constructor);
1076 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1077 {
1078 gfc_charlen *cl, *cl2;
1079
1080 cl2 = NULL;
1081 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1082 {
1083 if (cl == cons->expr->ts.u.cl)
1084 break;
1085 cl2 = cl;
1086 }
1087
1088 gcc_assert (cl);
1089
1090 if (cl2)
1091 cl2->next = cl->next;
1092
1093 gfc_free_expr (cl->length);
1094 free (cl);
1095 }
1096
1097 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1098 cons->expr->ts.u.cl->length_from_typespec = true;
1099 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1100 gfc_resolve_character_array_constructor (cons->expr);
1101 }
1102 }
1103
1104 if (cons->expr->expr_type == EXPR_NULL
1105 && !(comp->attr.pointer || comp->attr.allocatable
1106 || comp->attr.proc_pointer
1107 || (comp->ts.type == BT_CLASS
1108 && (CLASS_DATA (comp)->attr.class_pointer
1109 || CLASS_DATA (comp)->attr.allocatable))))
1110 {
1111 t = FAILURE;
1112 gfc_error ("The NULL in the derived type constructor at %L is "
1113 "being applied to component '%s', which is neither "
1114 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1115 comp->name);
1116 }
1117
1118 if (!comp->attr.pointer || comp->attr.proc_pointer
1119 || cons->expr->expr_type == EXPR_NULL)
1120 continue;
1121
1122 a = gfc_expr_attr (cons->expr);
1123
1124 if (!a.pointer && !a.target)
1125 {
1126 t = FAILURE;
1127 gfc_error ("The element in the derived type constructor at %L, "
1128 "for pointer component '%s' should be a POINTER or "
1129 "a TARGET", &cons->expr->where, comp->name);
1130 }
1131
1132 if (init)
1133 {
1134 /* F08:C461. Additional checks for pointer initialization. */
1135 if (a.allocatable)
1136 {
1137 t = FAILURE;
1138 gfc_error ("Pointer initialization target at %L "
1139 "must not be ALLOCATABLE ", &cons->expr->where);
1140 }
1141 if (!a.save)
1142 {
1143 t = FAILURE;
1144 gfc_error ("Pointer initialization target at %L "
1145 "must have the SAVE attribute", &cons->expr->where);
1146 }
1147 }
1148
1149 /* F2003, C1272 (3). */
1150 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1151 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1152 || gfc_is_coindexed (cons->expr)))
1153 {
1154 t = FAILURE;
1155 gfc_error ("Invalid expression in the derived type constructor for "
1156 "pointer component '%s' at %L in PURE procedure",
1157 comp->name, &cons->expr->where);
1158 }
1159
1160 if (gfc_implicit_pure (NULL)
1161 && cons->expr->expr_type == EXPR_VARIABLE
1162 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1163 || gfc_is_coindexed (cons->expr)))
1164 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1165
1166 }
1167
1168 return t;
1169 }
1170
1171
1172 /****************** Expression name resolution ******************/
1173
1174 /* Returns 0 if a symbol was not declared with a type or
1175 attribute declaration statement, nonzero otherwise. */
1176
1177 static int
1178 was_declared (gfc_symbol *sym)
1179 {
1180 symbol_attribute a;
1181
1182 a = sym->attr;
1183
1184 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1185 return 1;
1186
1187 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1188 || a.optional || a.pointer || a.save || a.target || a.volatile_
1189 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1190 || a.asynchronous || a.codimension)
1191 return 1;
1192
1193 return 0;
1194 }
1195
1196
1197 /* Determine if a symbol is generic or not. */
1198
1199 static int
1200 generic_sym (gfc_symbol *sym)
1201 {
1202 gfc_symbol *s;
1203
1204 if (sym->attr.generic ||
1205 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1206 return 1;
1207
1208 if (was_declared (sym) || sym->ns->parent == NULL)
1209 return 0;
1210
1211 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1212
1213 if (s != NULL)
1214 {
1215 if (s == sym)
1216 return 0;
1217 else
1218 return generic_sym (s);
1219 }
1220
1221 return 0;
1222 }
1223
1224
1225 /* Determine if a symbol is specific or not. */
1226
1227 static int
1228 specific_sym (gfc_symbol *sym)
1229 {
1230 gfc_symbol *s;
1231
1232 if (sym->attr.if_source == IFSRC_IFBODY
1233 || sym->attr.proc == PROC_MODULE
1234 || sym->attr.proc == PROC_INTERNAL
1235 || sym->attr.proc == PROC_ST_FUNCTION
1236 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1237 || sym->attr.external)
1238 return 1;
1239
1240 if (was_declared (sym) || sym->ns->parent == NULL)
1241 return 0;
1242
1243 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1244
1245 return (s == NULL) ? 0 : specific_sym (s);
1246 }
1247
1248
1249 /* Figure out if the procedure is specific, generic or unknown. */
1250
1251 typedef enum
1252 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1253 proc_type;
1254
1255 static proc_type
1256 procedure_kind (gfc_symbol *sym)
1257 {
1258 if (generic_sym (sym))
1259 return PTYPE_GENERIC;
1260
1261 if (specific_sym (sym))
1262 return PTYPE_SPECIFIC;
1263
1264 return PTYPE_UNKNOWN;
1265 }
1266
1267 /* Check references to assumed size arrays. The flag need_full_assumed_size
1268 is nonzero when matching actual arguments. */
1269
1270 static int need_full_assumed_size = 0;
1271
1272 static bool
1273 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1274 {
1275 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1276 return false;
1277
1278 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1279 What should it be? */
1280 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1281 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1282 && (e->ref->u.ar.type == AR_FULL))
1283 {
1284 gfc_error ("The upper bound in the last dimension must "
1285 "appear in the reference to the assumed size "
1286 "array '%s' at %L", sym->name, &e->where);
1287 return true;
1288 }
1289 return false;
1290 }
1291
1292
1293 /* Look for bad assumed size array references in argument expressions
1294 of elemental and array valued intrinsic procedures. Since this is
1295 called from procedure resolution functions, it only recurses at
1296 operators. */
1297
1298 static bool
1299 resolve_assumed_size_actual (gfc_expr *e)
1300 {
1301 if (e == NULL)
1302 return false;
1303
1304 switch (e->expr_type)
1305 {
1306 case EXPR_VARIABLE:
1307 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1308 return true;
1309 break;
1310
1311 case EXPR_OP:
1312 if (resolve_assumed_size_actual (e->value.op.op1)
1313 || resolve_assumed_size_actual (e->value.op.op2))
1314 return true;
1315 break;
1316
1317 default:
1318 break;
1319 }
1320 return false;
1321 }
1322
1323
1324 /* Check a generic procedure, passed as an actual argument, to see if
1325 there is a matching specific name. If none, it is an error, and if
1326 more than one, the reference is ambiguous. */
1327 static int
1328 count_specific_procs (gfc_expr *e)
1329 {
1330 int n;
1331 gfc_interface *p;
1332 gfc_symbol *sym;
1333
1334 n = 0;
1335 sym = e->symtree->n.sym;
1336
1337 for (p = sym->generic; p; p = p->next)
1338 if (strcmp (sym->name, p->sym->name) == 0)
1339 {
1340 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1341 sym->name);
1342 n++;
1343 }
1344
1345 if (n > 1)
1346 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1347 &e->where);
1348
1349 if (n == 0)
1350 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1351 "argument at %L", sym->name, &e->where);
1352
1353 return n;
1354 }
1355
1356
1357 /* See if a call to sym could possibly be a not allowed RECURSION because of
1358 a missing RECURIVE declaration. This means that either sym is the current
1359 context itself, or sym is the parent of a contained procedure calling its
1360 non-RECURSIVE containing procedure.
1361 This also works if sym is an ENTRY. */
1362
1363 static bool
1364 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1365 {
1366 gfc_symbol* proc_sym;
1367 gfc_symbol* context_proc;
1368 gfc_namespace* real_context;
1369
1370 if (sym->attr.flavor == FL_PROGRAM)
1371 return false;
1372
1373 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1374
1375 /* If we've got an ENTRY, find real procedure. */
1376 if (sym->attr.entry && sym->ns->entries)
1377 proc_sym = sym->ns->entries->sym;
1378 else
1379 proc_sym = sym;
1380
1381 /* If sym is RECURSIVE, all is well of course. */
1382 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1383 return false;
1384
1385 /* Find the context procedure's "real" symbol if it has entries.
1386 We look for a procedure symbol, so recurse on the parents if we don't
1387 find one (like in case of a BLOCK construct). */
1388 for (real_context = context; ; real_context = real_context->parent)
1389 {
1390 /* We should find something, eventually! */
1391 gcc_assert (real_context);
1392
1393 context_proc = (real_context->entries ? real_context->entries->sym
1394 : real_context->proc_name);
1395
1396 /* In some special cases, there may not be a proc_name, like for this
1397 invalid code:
1398 real(bad_kind()) function foo () ...
1399 when checking the call to bad_kind ().
1400 In these cases, we simply return here and assume that the
1401 call is ok. */
1402 if (!context_proc)
1403 return false;
1404
1405 if (context_proc->attr.flavor != FL_LABEL)
1406 break;
1407 }
1408
1409 /* A call from sym's body to itself is recursion, of course. */
1410 if (context_proc == proc_sym)
1411 return true;
1412
1413 /* The same is true if context is a contained procedure and sym the
1414 containing one. */
1415 if (context_proc->attr.contained)
1416 {
1417 gfc_symbol* parent_proc;
1418
1419 gcc_assert (context->parent);
1420 parent_proc = (context->parent->entries ? context->parent->entries->sym
1421 : context->parent->proc_name);
1422
1423 if (parent_proc == proc_sym)
1424 return true;
1425 }
1426
1427 return false;
1428 }
1429
1430
1431 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1432 its typespec and formal argument list. */
1433
1434 static gfc_try
1435 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1436 {
1437 gfc_intrinsic_sym* isym = NULL;
1438 const char* symstd;
1439
1440 if (sym->formal)
1441 return SUCCESS;
1442
1443 /* We already know this one is an intrinsic, so we don't call
1444 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1445 gfc_find_subroutine directly to check whether it is a function or
1446 subroutine. */
1447
1448 if (sym->intmod_sym_id)
1449 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1450 else
1451 isym = gfc_find_function (sym->name);
1452
1453 if (isym)
1454 {
1455 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1456 && !sym->attr.implicit_type)
1457 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1458 " ignored", sym->name, &sym->declared_at);
1459
1460 if (!sym->attr.function &&
1461 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1462 return FAILURE;
1463
1464 sym->ts = isym->ts;
1465 }
1466 else if ((isym = gfc_find_subroutine (sym->name)))
1467 {
1468 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1469 {
1470 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1471 " specifier", sym->name, &sym->declared_at);
1472 return FAILURE;
1473 }
1474
1475 if (!sym->attr.subroutine &&
1476 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1477 return FAILURE;
1478 }
1479 else
1480 {
1481 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1482 &sym->declared_at);
1483 return FAILURE;
1484 }
1485
1486 gfc_copy_formal_args_intr (sym, isym);
1487
1488 /* Check it is actually available in the standard settings. */
1489 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1490 == FAILURE)
1491 {
1492 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1493 " available in the current standard settings but %s. Use"
1494 " an appropriate -std=* option or enable -fall-intrinsics"
1495 " in order to use it.",
1496 sym->name, &sym->declared_at, symstd);
1497 return FAILURE;
1498 }
1499
1500 return SUCCESS;
1501 }
1502
1503
1504 /* Resolve a procedure expression, like passing it to a called procedure or as
1505 RHS for a procedure pointer assignment. */
1506
1507 static gfc_try
1508 resolve_procedure_expression (gfc_expr* expr)
1509 {
1510 gfc_symbol* sym;
1511
1512 if (expr->expr_type != EXPR_VARIABLE)
1513 return SUCCESS;
1514 gcc_assert (expr->symtree);
1515
1516 sym = expr->symtree->n.sym;
1517
1518 if (sym->attr.intrinsic)
1519 resolve_intrinsic (sym, &expr->where);
1520
1521 if (sym->attr.flavor != FL_PROCEDURE
1522 || (sym->attr.function && sym->result == sym))
1523 return SUCCESS;
1524
1525 /* A non-RECURSIVE procedure that is used as procedure expression within its
1526 own body is in danger of being called recursively. */
1527 if (is_illegal_recursion (sym, gfc_current_ns))
1528 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1529 " itself recursively. Declare it RECURSIVE or use"
1530 " -frecursive", sym->name, &expr->where);
1531
1532 return SUCCESS;
1533 }
1534
1535
1536 /* Resolve an actual argument list. Most of the time, this is just
1537 resolving the expressions in the list.
1538 The exception is that we sometimes have to decide whether arguments
1539 that look like procedure arguments are really simple variable
1540 references. */
1541
1542 static gfc_try
1543 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1544 bool no_formal_args)
1545 {
1546 gfc_symbol *sym;
1547 gfc_symtree *parent_st;
1548 gfc_expr *e;
1549 int save_need_full_assumed_size;
1550
1551 for (; arg; arg = arg->next)
1552 {
1553 e = arg->expr;
1554 if (e == NULL)
1555 {
1556 /* Check the label is a valid branching target. */
1557 if (arg->label)
1558 {
1559 if (arg->label->defined == ST_LABEL_UNKNOWN)
1560 {
1561 gfc_error ("Label %d referenced at %L is never defined",
1562 arg->label->value, &arg->label->where);
1563 return FAILURE;
1564 }
1565 }
1566 continue;
1567 }
1568
1569 if (e->expr_type == EXPR_VARIABLE
1570 && e->symtree->n.sym->attr.generic
1571 && no_formal_args
1572 && count_specific_procs (e) != 1)
1573 return FAILURE;
1574
1575 if (e->ts.type != BT_PROCEDURE)
1576 {
1577 save_need_full_assumed_size = need_full_assumed_size;
1578 if (e->expr_type != EXPR_VARIABLE)
1579 need_full_assumed_size = 0;
1580 if (gfc_resolve_expr (e) != SUCCESS)
1581 return FAILURE;
1582 need_full_assumed_size = save_need_full_assumed_size;
1583 goto argument_list;
1584 }
1585
1586 /* See if the expression node should really be a variable reference. */
1587
1588 sym = e->symtree->n.sym;
1589
1590 if (sym->attr.flavor == FL_PROCEDURE
1591 || sym->attr.intrinsic
1592 || sym->attr.external)
1593 {
1594 int actual_ok;
1595
1596 /* If a procedure is not already determined to be something else
1597 check if it is intrinsic. */
1598 if (!sym->attr.intrinsic
1599 && !(sym->attr.external || sym->attr.use_assoc
1600 || sym->attr.if_source == IFSRC_IFBODY)
1601 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1602 sym->attr.intrinsic = 1;
1603
1604 if (sym->attr.proc == PROC_ST_FUNCTION)
1605 {
1606 gfc_error ("Statement function '%s' at %L is not allowed as an "
1607 "actual argument", sym->name, &e->where);
1608 }
1609
1610 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1611 sym->attr.subroutine);
1612 if (sym->attr.intrinsic && actual_ok == 0)
1613 {
1614 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1615 "actual argument", sym->name, &e->where);
1616 }
1617
1618 if (sym->attr.contained && !sym->attr.use_assoc
1619 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1620 {
1621 if (gfc_notify_std (GFC_STD_F2008,
1622 "Fortran 2008: Internal procedure '%s' is"
1623 " used as actual argument at %L",
1624 sym->name, &e->where) == FAILURE)
1625 return FAILURE;
1626 }
1627
1628 if (sym->attr.elemental && !sym->attr.intrinsic)
1629 {
1630 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1631 "allowed as an actual argument at %L", sym->name,
1632 &e->where);
1633 }
1634
1635 /* Check if a generic interface has a specific procedure
1636 with the same name before emitting an error. */
1637 if (sym->attr.generic && count_specific_procs (e) != 1)
1638 return FAILURE;
1639
1640 /* Just in case a specific was found for the expression. */
1641 sym = e->symtree->n.sym;
1642
1643 /* If the symbol is the function that names the current (or
1644 parent) scope, then we really have a variable reference. */
1645
1646 if (gfc_is_function_return_value (sym, sym->ns))
1647 goto got_variable;
1648
1649 /* If all else fails, see if we have a specific intrinsic. */
1650 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1651 {
1652 gfc_intrinsic_sym *isym;
1653
1654 isym = gfc_find_function (sym->name);
1655 if (isym == NULL || !isym->specific)
1656 {
1657 gfc_error ("Unable to find a specific INTRINSIC procedure "
1658 "for the reference '%s' at %L", sym->name,
1659 &e->where);
1660 return FAILURE;
1661 }
1662 sym->ts = isym->ts;
1663 sym->attr.intrinsic = 1;
1664 sym->attr.function = 1;
1665 }
1666
1667 if (gfc_resolve_expr (e) == FAILURE)
1668 return FAILURE;
1669 goto argument_list;
1670 }
1671
1672 /* See if the name is a module procedure in a parent unit. */
1673
1674 if (was_declared (sym) || sym->ns->parent == NULL)
1675 goto got_variable;
1676
1677 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1678 {
1679 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1680 return FAILURE;
1681 }
1682
1683 if (parent_st == NULL)
1684 goto got_variable;
1685
1686 sym = parent_st->n.sym;
1687 e->symtree = parent_st; /* Point to the right thing. */
1688
1689 if (sym->attr.flavor == FL_PROCEDURE
1690 || sym->attr.intrinsic
1691 || sym->attr.external)
1692 {
1693 if (gfc_resolve_expr (e) == FAILURE)
1694 return FAILURE;
1695 goto argument_list;
1696 }
1697
1698 got_variable:
1699 e->expr_type = EXPR_VARIABLE;
1700 e->ts = sym->ts;
1701 if (sym->as != NULL)
1702 {
1703 e->rank = sym->as->rank;
1704 e->ref = gfc_get_ref ();
1705 e->ref->type = REF_ARRAY;
1706 e->ref->u.ar.type = AR_FULL;
1707 e->ref->u.ar.as = sym->as;
1708 }
1709
1710 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1711 primary.c (match_actual_arg). If above code determines that it
1712 is a variable instead, it needs to be resolved as it was not
1713 done at the beginning of this function. */
1714 save_need_full_assumed_size = need_full_assumed_size;
1715 if (e->expr_type != EXPR_VARIABLE)
1716 need_full_assumed_size = 0;
1717 if (gfc_resolve_expr (e) != SUCCESS)
1718 return FAILURE;
1719 need_full_assumed_size = save_need_full_assumed_size;
1720
1721 argument_list:
1722 /* Check argument list functions %VAL, %LOC and %REF. There is
1723 nothing to do for %REF. */
1724 if (arg->name && arg->name[0] == '%')
1725 {
1726 if (strncmp ("%VAL", arg->name, 4) == 0)
1727 {
1728 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1729 {
1730 gfc_error ("By-value argument at %L is not of numeric "
1731 "type", &e->where);
1732 return FAILURE;
1733 }
1734
1735 if (e->rank)
1736 {
1737 gfc_error ("By-value argument at %L cannot be an array or "
1738 "an array section", &e->where);
1739 return FAILURE;
1740 }
1741
1742 /* Intrinsics are still PROC_UNKNOWN here. However,
1743 since same file external procedures are not resolvable
1744 in gfortran, it is a good deal easier to leave them to
1745 intrinsic.c. */
1746 if (ptype != PROC_UNKNOWN
1747 && ptype != PROC_DUMMY
1748 && ptype != PROC_EXTERNAL
1749 && ptype != PROC_MODULE)
1750 {
1751 gfc_error ("By-value argument at %L is not allowed "
1752 "in this context", &e->where);
1753 return FAILURE;
1754 }
1755 }
1756
1757 /* Statement functions have already been excluded above. */
1758 else if (strncmp ("%LOC", arg->name, 4) == 0
1759 && e->ts.type == BT_PROCEDURE)
1760 {
1761 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1762 {
1763 gfc_error ("Passing internal procedure at %L by location "
1764 "not allowed", &e->where);
1765 return FAILURE;
1766 }
1767 }
1768 }
1769
1770 /* Fortran 2008, C1237. */
1771 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1772 && gfc_has_ultimate_pointer (e))
1773 {
1774 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1775 "component", &e->where);
1776 return FAILURE;
1777 }
1778 }
1779
1780 return SUCCESS;
1781 }
1782
1783
1784 /* Do the checks of the actual argument list that are specific to elemental
1785 procedures. If called with c == NULL, we have a function, otherwise if
1786 expr == NULL, we have a subroutine. */
1787
1788 static gfc_try
1789 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1790 {
1791 gfc_actual_arglist *arg0;
1792 gfc_actual_arglist *arg;
1793 gfc_symbol *esym = NULL;
1794 gfc_intrinsic_sym *isym = NULL;
1795 gfc_expr *e = NULL;
1796 gfc_intrinsic_arg *iformal = NULL;
1797 gfc_formal_arglist *eformal = NULL;
1798 bool formal_optional = false;
1799 bool set_by_optional = false;
1800 int i;
1801 int rank = 0;
1802
1803 /* Is this an elemental procedure? */
1804 if (expr && expr->value.function.actual != NULL)
1805 {
1806 if (expr->value.function.esym != NULL
1807 && expr->value.function.esym->attr.elemental)
1808 {
1809 arg0 = expr->value.function.actual;
1810 esym = expr->value.function.esym;
1811 }
1812 else if (expr->value.function.isym != NULL
1813 && expr->value.function.isym->elemental)
1814 {
1815 arg0 = expr->value.function.actual;
1816 isym = expr->value.function.isym;
1817 }
1818 else
1819 return SUCCESS;
1820 }
1821 else if (c && c->ext.actual != NULL)
1822 {
1823 arg0 = c->ext.actual;
1824
1825 if (c->resolved_sym)
1826 esym = c->resolved_sym;
1827 else
1828 esym = c->symtree->n.sym;
1829 gcc_assert (esym);
1830
1831 if (!esym->attr.elemental)
1832 return SUCCESS;
1833 }
1834 else
1835 return SUCCESS;
1836
1837 /* The rank of an elemental is the rank of its array argument(s). */
1838 for (arg = arg0; arg; arg = arg->next)
1839 {
1840 if (arg->expr != NULL && arg->expr->rank > 0)
1841 {
1842 rank = arg->expr->rank;
1843 if (arg->expr->expr_type == EXPR_VARIABLE
1844 && arg->expr->symtree->n.sym->attr.optional)
1845 set_by_optional = true;
1846
1847 /* Function specific; set the result rank and shape. */
1848 if (expr)
1849 {
1850 expr->rank = rank;
1851 if (!expr->shape && arg->expr->shape)
1852 {
1853 expr->shape = gfc_get_shape (rank);
1854 for (i = 0; i < rank; i++)
1855 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1856 }
1857 }
1858 break;
1859 }
1860 }
1861
1862 /* If it is an array, it shall not be supplied as an actual argument
1863 to an elemental procedure unless an array of the same rank is supplied
1864 as an actual argument corresponding to a nonoptional dummy argument of
1865 that elemental procedure(12.4.1.5). */
1866 formal_optional = false;
1867 if (isym)
1868 iformal = isym->formal;
1869 else
1870 eformal = esym->formal;
1871
1872 for (arg = arg0; arg; arg = arg->next)
1873 {
1874 if (eformal)
1875 {
1876 if (eformal->sym && eformal->sym->attr.optional)
1877 formal_optional = true;
1878 eformal = eformal->next;
1879 }
1880 else if (isym && iformal)
1881 {
1882 if (iformal->optional)
1883 formal_optional = true;
1884 iformal = iformal->next;
1885 }
1886 else if (isym)
1887 formal_optional = true;
1888
1889 if (pedantic && arg->expr != NULL
1890 && arg->expr->expr_type == EXPR_VARIABLE
1891 && arg->expr->symtree->n.sym->attr.optional
1892 && formal_optional
1893 && arg->expr->rank
1894 && (set_by_optional || arg->expr->rank != rank)
1895 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1896 {
1897 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1898 "MISSING, it cannot be the actual argument of an "
1899 "ELEMENTAL procedure unless there is a non-optional "
1900 "argument with the same rank (12.4.1.5)",
1901 arg->expr->symtree->n.sym->name, &arg->expr->where);
1902 return FAILURE;
1903 }
1904 }
1905
1906 for (arg = arg0; arg; arg = arg->next)
1907 {
1908 if (arg->expr == NULL || arg->expr->rank == 0)
1909 continue;
1910
1911 /* Being elemental, the last upper bound of an assumed size array
1912 argument must be present. */
1913 if (resolve_assumed_size_actual (arg->expr))
1914 return FAILURE;
1915
1916 /* Elemental procedure's array actual arguments must conform. */
1917 if (e != NULL)
1918 {
1919 if (gfc_check_conformance (arg->expr, e,
1920 "elemental procedure") == FAILURE)
1921 return FAILURE;
1922 }
1923 else
1924 e = arg->expr;
1925 }
1926
1927 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1928 is an array, the intent inout/out variable needs to be also an array. */
1929 if (rank > 0 && esym && expr == NULL)
1930 for (eformal = esym->formal, arg = arg0; arg && eformal;
1931 arg = arg->next, eformal = eformal->next)
1932 if ((eformal->sym->attr.intent == INTENT_OUT
1933 || eformal->sym->attr.intent == INTENT_INOUT)
1934 && arg->expr && arg->expr->rank == 0)
1935 {
1936 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1937 "ELEMENTAL subroutine '%s' is a scalar, but another "
1938 "actual argument is an array", &arg->expr->where,
1939 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1940 : "INOUT", eformal->sym->name, esym->name);
1941 return FAILURE;
1942 }
1943 return SUCCESS;
1944 }
1945
1946
1947 /* This function does the checking of references to global procedures
1948 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1949 77 and 95 standards. It checks for a gsymbol for the name, making
1950 one if it does not already exist. If it already exists, then the
1951 reference being resolved must correspond to the type of gsymbol.
1952 Otherwise, the new symbol is equipped with the attributes of the
1953 reference. The corresponding code that is called in creating
1954 global entities is parse.c.
1955
1956 In addition, for all but -std=legacy, the gsymbols are used to
1957 check the interfaces of external procedures from the same file.
1958 The namespace of the gsymbol is resolved and then, once this is
1959 done the interface is checked. */
1960
1961
1962 static bool
1963 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1964 {
1965 if (!gsym_ns->proc_name->attr.recursive)
1966 return true;
1967
1968 if (sym->ns == gsym_ns)
1969 return false;
1970
1971 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1972 return false;
1973
1974 return true;
1975 }
1976
1977 static bool
1978 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1979 {
1980 if (gsym_ns->entries)
1981 {
1982 gfc_entry_list *entry = gsym_ns->entries;
1983
1984 for (; entry; entry = entry->next)
1985 {
1986 if (strcmp (sym->name, entry->sym->name) == 0)
1987 {
1988 if (strcmp (gsym_ns->proc_name->name,
1989 sym->ns->proc_name->name) == 0)
1990 return false;
1991
1992 if (sym->ns->parent
1993 && strcmp (gsym_ns->proc_name->name,
1994 sym->ns->parent->proc_name->name) == 0)
1995 return false;
1996 }
1997 }
1998 }
1999 return true;
2000 }
2001
2002 static void
2003 resolve_global_procedure (gfc_symbol *sym, locus *where,
2004 gfc_actual_arglist **actual, int sub)
2005 {
2006 gfc_gsymbol * gsym;
2007 gfc_namespace *ns;
2008 enum gfc_symbol_type type;
2009
2010 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2011
2012 gsym = gfc_get_gsymbol (sym->name);
2013
2014 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2015 gfc_global_used (gsym, where);
2016
2017 if (gfc_option.flag_whole_file
2018 && (sym->attr.if_source == IFSRC_UNKNOWN
2019 || sym->attr.if_source == IFSRC_IFBODY)
2020 && gsym->type != GSYM_UNKNOWN
2021 && gsym->ns
2022 && gsym->ns->resolved != -1
2023 && gsym->ns->proc_name
2024 && not_in_recursive (sym, gsym->ns)
2025 && not_entry_self_reference (sym, gsym->ns))
2026 {
2027 gfc_symbol *def_sym;
2028
2029 /* Resolve the gsymbol namespace if needed. */
2030 if (!gsym->ns->resolved)
2031 {
2032 gfc_dt_list *old_dt_list;
2033 struct gfc_omp_saved_state old_omp_state;
2034
2035 /* Stash away derived types so that the backend_decls do not
2036 get mixed up. */
2037 old_dt_list = gfc_derived_types;
2038 gfc_derived_types = NULL;
2039 /* And stash away openmp state. */
2040 gfc_omp_save_and_clear_state (&old_omp_state);
2041
2042 gfc_resolve (gsym->ns);
2043
2044 /* Store the new derived types with the global namespace. */
2045 if (gfc_derived_types)
2046 gsym->ns->derived_types = gfc_derived_types;
2047
2048 /* Restore the derived types of this namespace. */
2049 gfc_derived_types = old_dt_list;
2050 /* And openmp state. */
2051 gfc_omp_restore_state (&old_omp_state);
2052 }
2053
2054 /* Make sure that translation for the gsymbol occurs before
2055 the procedure currently being resolved. */
2056 ns = gfc_global_ns_list;
2057 for (; ns && ns != gsym->ns; ns = ns->sibling)
2058 {
2059 if (ns->sibling == gsym->ns)
2060 {
2061 ns->sibling = gsym->ns->sibling;
2062 gsym->ns->sibling = gfc_global_ns_list;
2063 gfc_global_ns_list = gsym->ns;
2064 break;
2065 }
2066 }
2067
2068 def_sym = gsym->ns->proc_name;
2069 if (def_sym->attr.entry_master)
2070 {
2071 gfc_entry_list *entry;
2072 for (entry = gsym->ns->entries; entry; entry = entry->next)
2073 if (strcmp (entry->sym->name, sym->name) == 0)
2074 {
2075 def_sym = entry->sym;
2076 break;
2077 }
2078 }
2079
2080 /* Differences in constant character lengths. */
2081 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2082 {
2083 long int l1 = 0, l2 = 0;
2084 gfc_charlen *cl1 = sym->ts.u.cl;
2085 gfc_charlen *cl2 = def_sym->ts.u.cl;
2086
2087 if (cl1 != NULL
2088 && cl1->length != NULL
2089 && cl1->length->expr_type == EXPR_CONSTANT)
2090 l1 = mpz_get_si (cl1->length->value.integer);
2091
2092 if (cl2 != NULL
2093 && cl2->length != NULL
2094 && cl2->length->expr_type == EXPR_CONSTANT)
2095 l2 = mpz_get_si (cl2->length->value.integer);
2096
2097 if (l1 && l2 && l1 != l2)
2098 gfc_error ("Character length mismatch in return type of "
2099 "function '%s' at %L (%ld/%ld)", sym->name,
2100 &sym->declared_at, l1, l2);
2101 }
2102
2103 /* Type mismatch of function return type and expected type. */
2104 if (sym->attr.function
2105 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2106 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2107 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2108 gfc_typename (&def_sym->ts));
2109
2110 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2111 {
2112 gfc_formal_arglist *arg = def_sym->formal;
2113 for ( ; arg; arg = arg->next)
2114 if (!arg->sym)
2115 continue;
2116 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2117 else if (arg->sym->attr.allocatable
2118 || arg->sym->attr.asynchronous
2119 || arg->sym->attr.optional
2120 || arg->sym->attr.pointer
2121 || arg->sym->attr.target
2122 || arg->sym->attr.value
2123 || arg->sym->attr.volatile_)
2124 {
2125 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2126 "has an attribute that requires an explicit "
2127 "interface for this procedure", arg->sym->name,
2128 sym->name, &sym->declared_at);
2129 break;
2130 }
2131 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2132 else if (arg->sym && arg->sym->as
2133 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2134 {
2135 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2136 "argument '%s' must have an explicit interface",
2137 sym->name, &sym->declared_at, arg->sym->name);
2138 break;
2139 }
2140 /* F2008, 12.4.2.2 (2c) */
2141 else if (arg->sym->attr.codimension)
2142 {
2143 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2144 "'%s' must have an explicit interface",
2145 sym->name, &sym->declared_at, arg->sym->name);
2146 break;
2147 }
2148 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2149 else if (false) /* TODO: is a parametrized derived type */
2150 {
2151 gfc_error ("Procedure '%s' at %L with parametrized derived "
2152 "type argument '%s' must have an explicit "
2153 "interface", sym->name, &sym->declared_at,
2154 arg->sym->name);
2155 break;
2156 }
2157 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2158 else if (arg->sym->ts.type == BT_CLASS)
2159 {
2160 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2161 "argument '%s' must have an explicit interface",
2162 sym->name, &sym->declared_at, arg->sym->name);
2163 break;
2164 }
2165 }
2166
2167 if (def_sym->attr.function)
2168 {
2169 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2170 if (def_sym->as && def_sym->as->rank
2171 && (!sym->as || sym->as->rank != def_sym->as->rank))
2172 gfc_error ("The reference to function '%s' at %L either needs an "
2173 "explicit INTERFACE or the rank is incorrect", sym->name,
2174 where);
2175
2176 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2177 if ((def_sym->result->attr.pointer
2178 || def_sym->result->attr.allocatable)
2179 && (sym->attr.if_source != IFSRC_IFBODY
2180 || def_sym->result->attr.pointer
2181 != sym->result->attr.pointer
2182 || def_sym->result->attr.allocatable
2183 != sym->result->attr.allocatable))
2184 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2185 "result must have an explicit interface", sym->name,
2186 where);
2187
2188 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2189 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2190 && def_sym->ts.type == BT_CHARACTER && def_sym->ts.u.cl->length != NULL)
2191 {
2192 gfc_charlen *cl = sym->ts.u.cl;
2193
2194 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2195 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2196 {
2197 gfc_error ("Nonconstant character-length function '%s' at %L "
2198 "must have an explicit interface", sym->name,
2199 &sym->declared_at);
2200 }
2201 }
2202 }
2203
2204 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2205 if (def_sym->attr.elemental && !sym->attr.elemental)
2206 {
2207 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2208 "interface", sym->name, &sym->declared_at);
2209 }
2210
2211 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2212 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2213 {
2214 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2215 "an explicit interface", sym->name, &sym->declared_at);
2216 }
2217
2218 if (gfc_option.flag_whole_file == 1
2219 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2220 && !(gfc_option.warn_std & GFC_STD_GNU)))
2221 gfc_errors_to_warnings (1);
2222
2223 if (sym->attr.if_source != IFSRC_IFBODY)
2224 gfc_procedure_use (def_sym, actual, where);
2225
2226 gfc_errors_to_warnings (0);
2227 }
2228
2229 if (gsym->type == GSYM_UNKNOWN)
2230 {
2231 gsym->type = type;
2232 gsym->where = *where;
2233 }
2234
2235 gsym->used = 1;
2236 }
2237
2238
2239 /************* Function resolution *************/
2240
2241 /* Resolve a function call known to be generic.
2242 Section 14.1.2.4.1. */
2243
2244 static match
2245 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2246 {
2247 gfc_symbol *s;
2248
2249 if (sym->attr.generic)
2250 {
2251 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2252 if (s != NULL)
2253 {
2254 expr->value.function.name = s->name;
2255 expr->value.function.esym = s;
2256
2257 if (s->ts.type != BT_UNKNOWN)
2258 expr->ts = s->ts;
2259 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2260 expr->ts = s->result->ts;
2261
2262 if (s->as != NULL)
2263 expr->rank = s->as->rank;
2264 else if (s->result != NULL && s->result->as != NULL)
2265 expr->rank = s->result->as->rank;
2266
2267 gfc_set_sym_referenced (expr->value.function.esym);
2268
2269 return MATCH_YES;
2270 }
2271
2272 /* TODO: Need to search for elemental references in generic
2273 interface. */
2274 }
2275
2276 if (sym->attr.intrinsic)
2277 return gfc_intrinsic_func_interface (expr, 0);
2278
2279 return MATCH_NO;
2280 }
2281
2282
2283 static gfc_try
2284 resolve_generic_f (gfc_expr *expr)
2285 {
2286 gfc_symbol *sym;
2287 match m;
2288
2289 sym = expr->symtree->n.sym;
2290
2291 for (;;)
2292 {
2293 m = resolve_generic_f0 (expr, sym);
2294 if (m == MATCH_YES)
2295 return SUCCESS;
2296 else if (m == MATCH_ERROR)
2297 return FAILURE;
2298
2299 generic:
2300 if (sym->ns->parent == NULL)
2301 break;
2302 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2303
2304 if (sym == NULL)
2305 break;
2306 if (!generic_sym (sym))
2307 goto generic;
2308 }
2309
2310 /* Last ditch attempt. See if the reference is to an intrinsic
2311 that possesses a matching interface. 14.1.2.4 */
2312 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2313 {
2314 gfc_error ("There is no specific function for the generic '%s' at %L",
2315 expr->symtree->n.sym->name, &expr->where);
2316 return FAILURE;
2317 }
2318
2319 m = gfc_intrinsic_func_interface (expr, 0);
2320 if (m == MATCH_YES)
2321 return SUCCESS;
2322 if (m == MATCH_NO)
2323 gfc_error ("Generic function '%s' at %L is not consistent with a "
2324 "specific intrinsic interface", expr->symtree->n.sym->name,
2325 &expr->where);
2326
2327 return FAILURE;
2328 }
2329
2330
2331 /* Resolve a function call known to be specific. */
2332
2333 static match
2334 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2335 {
2336 match m;
2337
2338 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2339 {
2340 if (sym->attr.dummy)
2341 {
2342 sym->attr.proc = PROC_DUMMY;
2343 goto found;
2344 }
2345
2346 sym->attr.proc = PROC_EXTERNAL;
2347 goto found;
2348 }
2349
2350 if (sym->attr.proc == PROC_MODULE
2351 || sym->attr.proc == PROC_ST_FUNCTION
2352 || sym->attr.proc == PROC_INTERNAL)
2353 goto found;
2354
2355 if (sym->attr.intrinsic)
2356 {
2357 m = gfc_intrinsic_func_interface (expr, 1);
2358 if (m == MATCH_YES)
2359 return MATCH_YES;
2360 if (m == MATCH_NO)
2361 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2362 "with an intrinsic", sym->name, &expr->where);
2363
2364 return MATCH_ERROR;
2365 }
2366
2367 return MATCH_NO;
2368
2369 found:
2370 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2371
2372 if (sym->result)
2373 expr->ts = sym->result->ts;
2374 else
2375 expr->ts = sym->ts;
2376 expr->value.function.name = sym->name;
2377 expr->value.function.esym = sym;
2378 if (sym->as != NULL)
2379 expr->rank = sym->as->rank;
2380
2381 return MATCH_YES;
2382 }
2383
2384
2385 static gfc_try
2386 resolve_specific_f (gfc_expr *expr)
2387 {
2388 gfc_symbol *sym;
2389 match m;
2390
2391 sym = expr->symtree->n.sym;
2392
2393 for (;;)
2394 {
2395 m = resolve_specific_f0 (sym, expr);
2396 if (m == MATCH_YES)
2397 return SUCCESS;
2398 if (m == MATCH_ERROR)
2399 return FAILURE;
2400
2401 if (sym->ns->parent == NULL)
2402 break;
2403
2404 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2405
2406 if (sym == NULL)
2407 break;
2408 }
2409
2410 gfc_error ("Unable to resolve the specific function '%s' at %L",
2411 expr->symtree->n.sym->name, &expr->where);
2412
2413 return SUCCESS;
2414 }
2415
2416
2417 /* Resolve a procedure call not known to be generic nor specific. */
2418
2419 static gfc_try
2420 resolve_unknown_f (gfc_expr *expr)
2421 {
2422 gfc_symbol *sym;
2423 gfc_typespec *ts;
2424
2425 sym = expr->symtree->n.sym;
2426
2427 if (sym->attr.dummy)
2428 {
2429 sym->attr.proc = PROC_DUMMY;
2430 expr->value.function.name = sym->name;
2431 goto set_type;
2432 }
2433
2434 /* See if we have an intrinsic function reference. */
2435
2436 if (gfc_is_intrinsic (sym, 0, expr->where))
2437 {
2438 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2439 return SUCCESS;
2440 return FAILURE;
2441 }
2442
2443 /* The reference is to an external name. */
2444
2445 sym->attr.proc = PROC_EXTERNAL;
2446 expr->value.function.name = sym->name;
2447 expr->value.function.esym = expr->symtree->n.sym;
2448
2449 if (sym->as != NULL)
2450 expr->rank = sym->as->rank;
2451
2452 /* Type of the expression is either the type of the symbol or the
2453 default type of the symbol. */
2454
2455 set_type:
2456 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2457
2458 if (sym->ts.type != BT_UNKNOWN)
2459 expr->ts = sym->ts;
2460 else
2461 {
2462 ts = gfc_get_default_type (sym->name, sym->ns);
2463
2464 if (ts->type == BT_UNKNOWN)
2465 {
2466 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2467 sym->name, &expr->where);
2468 return FAILURE;
2469 }
2470 else
2471 expr->ts = *ts;
2472 }
2473
2474 return SUCCESS;
2475 }
2476
2477
2478 /* Return true, if the symbol is an external procedure. */
2479 static bool
2480 is_external_proc (gfc_symbol *sym)
2481 {
2482 if (!sym->attr.dummy && !sym->attr.contained
2483 && !(sym->attr.intrinsic
2484 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2485 && sym->attr.proc != PROC_ST_FUNCTION
2486 && !sym->attr.proc_pointer
2487 && !sym->attr.use_assoc
2488 && sym->name)
2489 return true;
2490
2491 return false;
2492 }
2493
2494
2495 /* Figure out if a function reference is pure or not. Also set the name
2496 of the function for a potential error message. Return nonzero if the
2497 function is PURE, zero if not. */
2498 static int
2499 pure_stmt_function (gfc_expr *, gfc_symbol *);
2500
2501 static int
2502 pure_function (gfc_expr *e, const char **name)
2503 {
2504 int pure;
2505
2506 *name = NULL;
2507
2508 if (e->symtree != NULL
2509 && e->symtree->n.sym != NULL
2510 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2511 return pure_stmt_function (e, e->symtree->n.sym);
2512
2513 if (e->value.function.esym)
2514 {
2515 pure = gfc_pure (e->value.function.esym);
2516 *name = e->value.function.esym->name;
2517 }
2518 else if (e->value.function.isym)
2519 {
2520 pure = e->value.function.isym->pure
2521 || e->value.function.isym->elemental;
2522 *name = e->value.function.isym->name;
2523 }
2524 else
2525 {
2526 /* Implicit functions are not pure. */
2527 pure = 0;
2528 *name = e->value.function.name;
2529 }
2530
2531 return pure;
2532 }
2533
2534
2535 static bool
2536 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2537 int *f ATTRIBUTE_UNUSED)
2538 {
2539 const char *name;
2540
2541 /* Don't bother recursing into other statement functions
2542 since they will be checked individually for purity. */
2543 if (e->expr_type != EXPR_FUNCTION
2544 || !e->symtree
2545 || e->symtree->n.sym == sym
2546 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2547 return false;
2548
2549 return pure_function (e, &name) ? false : true;
2550 }
2551
2552
2553 static int
2554 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2555 {
2556 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2557 }
2558
2559
2560 static gfc_try
2561 is_scalar_expr_ptr (gfc_expr *expr)
2562 {
2563 gfc_try retval = SUCCESS;
2564 gfc_ref *ref;
2565 int start;
2566 int end;
2567
2568 /* See if we have a gfc_ref, which means we have a substring, array
2569 reference, or a component. */
2570 if (expr->ref != NULL)
2571 {
2572 ref = expr->ref;
2573 while (ref->next != NULL)
2574 ref = ref->next;
2575
2576 switch (ref->type)
2577 {
2578 case REF_SUBSTRING:
2579 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2580 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2581 retval = FAILURE;
2582 break;
2583
2584 case REF_ARRAY:
2585 if (ref->u.ar.type == AR_ELEMENT)
2586 retval = SUCCESS;
2587 else if (ref->u.ar.type == AR_FULL)
2588 {
2589 /* The user can give a full array if the array is of size 1. */
2590 if (ref->u.ar.as != NULL
2591 && ref->u.ar.as->rank == 1
2592 && ref->u.ar.as->type == AS_EXPLICIT
2593 && ref->u.ar.as->lower[0] != NULL
2594 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2595 && ref->u.ar.as->upper[0] != NULL
2596 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2597 {
2598 /* If we have a character string, we need to check if
2599 its length is one. */
2600 if (expr->ts.type == BT_CHARACTER)
2601 {
2602 if (expr->ts.u.cl == NULL
2603 || expr->ts.u.cl->length == NULL
2604 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2605 != 0)
2606 retval = FAILURE;
2607 }
2608 else
2609 {
2610 /* We have constant lower and upper bounds. If the
2611 difference between is 1, it can be considered a
2612 scalar.
2613 FIXME: Use gfc_dep_compare_expr instead. */
2614 start = (int) mpz_get_si
2615 (ref->u.ar.as->lower[0]->value.integer);
2616 end = (int) mpz_get_si
2617 (ref->u.ar.as->upper[0]->value.integer);
2618 if (end - start + 1 != 1)
2619 retval = FAILURE;
2620 }
2621 }
2622 else
2623 retval = FAILURE;
2624 }
2625 else
2626 retval = FAILURE;
2627 break;
2628 default:
2629 retval = SUCCESS;
2630 break;
2631 }
2632 }
2633 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2634 {
2635 /* Character string. Make sure it's of length 1. */
2636 if (expr->ts.u.cl == NULL
2637 || expr->ts.u.cl->length == NULL
2638 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2639 retval = FAILURE;
2640 }
2641 else if (expr->rank != 0)
2642 retval = FAILURE;
2643
2644 return retval;
2645 }
2646
2647
2648 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2649 and, in the case of c_associated, set the binding label based on
2650 the arguments. */
2651
2652 static gfc_try
2653 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2654 gfc_symbol **new_sym)
2655 {
2656 char name[GFC_MAX_SYMBOL_LEN + 1];
2657 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2658 int optional_arg = 0;
2659 gfc_try retval = SUCCESS;
2660 gfc_symbol *args_sym;
2661 gfc_typespec *arg_ts;
2662 symbol_attribute arg_attr;
2663
2664 if (args->expr->expr_type == EXPR_CONSTANT
2665 || args->expr->expr_type == EXPR_OP
2666 || args->expr->expr_type == EXPR_NULL)
2667 {
2668 gfc_error ("Argument to '%s' at %L is not a variable",
2669 sym->name, &(args->expr->where));
2670 return FAILURE;
2671 }
2672
2673 args_sym = args->expr->symtree->n.sym;
2674
2675 /* The typespec for the actual arg should be that stored in the expr
2676 and not necessarily that of the expr symbol (args_sym), because
2677 the actual expression could be a part-ref of the expr symbol. */
2678 arg_ts = &(args->expr->ts);
2679 arg_attr = gfc_expr_attr (args->expr);
2680
2681 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2682 {
2683 /* If the user gave two args then they are providing something for
2684 the optional arg (the second cptr). Therefore, set the name and
2685 binding label to the c_associated for two cptrs. Otherwise,
2686 set c_associated to expect one cptr. */
2687 if (args->next)
2688 {
2689 /* two args. */
2690 sprintf (name, "%s_2", sym->name);
2691 sprintf (binding_label, "%s_2", sym->binding_label);
2692 optional_arg = 1;
2693 }
2694 else
2695 {
2696 /* one arg. */
2697 sprintf (name, "%s_1", sym->name);
2698 sprintf (binding_label, "%s_1", sym->binding_label);
2699 optional_arg = 0;
2700 }
2701
2702 /* Get a new symbol for the version of c_associated that
2703 will get called. */
2704 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2705 }
2706 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2707 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2708 {
2709 sprintf (name, "%s", sym->name);
2710 sprintf (binding_label, "%s", sym->binding_label);
2711
2712 /* Error check the call. */
2713 if (args->next != NULL)
2714 {
2715 gfc_error_now ("More actual than formal arguments in '%s' "
2716 "call at %L", name, &(args->expr->where));
2717 retval = FAILURE;
2718 }
2719 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2720 {
2721 gfc_ref *ref;
2722 bool seen_section;
2723
2724 /* Make sure we have either the target or pointer attribute. */
2725 if (!arg_attr.target && !arg_attr.pointer)
2726 {
2727 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2728 "a TARGET or an associated pointer",
2729 args_sym->name,
2730 sym->name, &(args->expr->where));
2731 retval = FAILURE;
2732 }
2733
2734 if (gfc_is_coindexed (args->expr))
2735 {
2736 gfc_error_now ("Coindexed argument not permitted"
2737 " in '%s' call at %L", name,
2738 &(args->expr->where));
2739 retval = FAILURE;
2740 }
2741
2742 /* Follow references to make sure there are no array
2743 sections. */
2744 seen_section = false;
2745
2746 for (ref=args->expr->ref; ref; ref = ref->next)
2747 {
2748 if (ref->type == REF_ARRAY)
2749 {
2750 if (ref->u.ar.type == AR_SECTION)
2751 seen_section = true;
2752
2753 if (ref->u.ar.type != AR_ELEMENT)
2754 {
2755 gfc_ref *r;
2756 for (r = ref->next; r; r=r->next)
2757 if (r->type == REF_COMPONENT)
2758 {
2759 gfc_error_now ("Array section not permitted"
2760 " in '%s' call at %L", name,
2761 &(args->expr->where));
2762 retval = FAILURE;
2763 break;
2764 }
2765 }
2766 }
2767 }
2768
2769 if (seen_section && retval == SUCCESS)
2770 gfc_warning ("Array section in '%s' call at %L", name,
2771 &(args->expr->where));
2772
2773 /* See if we have interoperable type and type param. */
2774 if (verify_c_interop (arg_ts) == SUCCESS
2775 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2776 {
2777 if (args_sym->attr.target == 1)
2778 {
2779 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2780 has the target attribute and is interoperable. */
2781 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2782 allocatable variable that has the TARGET attribute and
2783 is not an array of zero size. */
2784 if (args_sym->attr.allocatable == 1)
2785 {
2786 if (args_sym->attr.dimension != 0
2787 && (args_sym->as && args_sym->as->rank == 0))
2788 {
2789 gfc_error_now ("Allocatable variable '%s' used as a "
2790 "parameter to '%s' at %L must not be "
2791 "an array of zero size",
2792 args_sym->name, sym->name,
2793 &(args->expr->where));
2794 retval = FAILURE;
2795 }
2796 }
2797 else
2798 {
2799 /* A non-allocatable target variable with C
2800 interoperable type and type parameters must be
2801 interoperable. */
2802 if (args_sym && args_sym->attr.dimension)
2803 {
2804 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2805 {
2806 gfc_error ("Assumed-shape array '%s' at %L "
2807 "cannot be an argument to the "
2808 "procedure '%s' because "
2809 "it is not C interoperable",
2810 args_sym->name,
2811 &(args->expr->where), sym->name);
2812 retval = FAILURE;
2813 }
2814 else if (args_sym->as->type == AS_DEFERRED)
2815 {
2816 gfc_error ("Deferred-shape array '%s' at %L "
2817 "cannot be an argument to the "
2818 "procedure '%s' because "
2819 "it is not C interoperable",
2820 args_sym->name,
2821 &(args->expr->where), sym->name);
2822 retval = FAILURE;
2823 }
2824 }
2825
2826 /* Make sure it's not a character string. Arrays of
2827 any type should be ok if the variable is of a C
2828 interoperable type. */
2829 if (arg_ts->type == BT_CHARACTER)
2830 if (arg_ts->u.cl != NULL
2831 && (arg_ts->u.cl->length == NULL
2832 || arg_ts->u.cl->length->expr_type
2833 != EXPR_CONSTANT
2834 || mpz_cmp_si
2835 (arg_ts->u.cl->length->value.integer, 1)
2836 != 0)
2837 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2838 {
2839 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2840 "at %L must have a length of 1",
2841 args_sym->name, sym->name,
2842 &(args->expr->where));
2843 retval = FAILURE;
2844 }
2845 }
2846 }
2847 else if (arg_attr.pointer
2848 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2849 {
2850 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2851 scalar pointer. */
2852 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2853 "associated scalar POINTER", args_sym->name,
2854 sym->name, &(args->expr->where));
2855 retval = FAILURE;
2856 }
2857 }
2858 else
2859 {
2860 /* The parameter is not required to be C interoperable. If it
2861 is not C interoperable, it must be a nonpolymorphic scalar
2862 with no length type parameters. It still must have either
2863 the pointer or target attribute, and it can be
2864 allocatable (but must be allocated when c_loc is called). */
2865 if (args->expr->rank != 0
2866 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2867 {
2868 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2869 "scalar", args_sym->name, sym->name,
2870 &(args->expr->where));
2871 retval = FAILURE;
2872 }
2873 else if (arg_ts->type == BT_CHARACTER
2874 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2875 {
2876 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2877 "%L must have a length of 1",
2878 args_sym->name, sym->name,
2879 &(args->expr->where));
2880 retval = FAILURE;
2881 }
2882 else if (arg_ts->type == BT_CLASS)
2883 {
2884 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2885 "polymorphic", args_sym->name, sym->name,
2886 &(args->expr->where));
2887 retval = FAILURE;
2888 }
2889 }
2890 }
2891 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2892 {
2893 if (args_sym->attr.flavor != FL_PROCEDURE)
2894 {
2895 /* TODO: Update this error message to allow for procedure
2896 pointers once they are implemented. */
2897 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2898 "procedure",
2899 args_sym->name, sym->name,
2900 &(args->expr->where));
2901 retval = FAILURE;
2902 }
2903 else if (args_sym->attr.is_bind_c != 1)
2904 {
2905 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2906 "BIND(C)",
2907 args_sym->name, sym->name,
2908 &(args->expr->where));
2909 retval = FAILURE;
2910 }
2911 }
2912
2913 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2914 *new_sym = sym;
2915 }
2916 else
2917 {
2918 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2919 "iso_c_binding function: '%s'!\n", sym->name);
2920 }
2921
2922 return retval;
2923 }
2924
2925
2926 /* Resolve a function call, which means resolving the arguments, then figuring
2927 out which entity the name refers to. */
2928
2929 static gfc_try
2930 resolve_function (gfc_expr *expr)
2931 {
2932 gfc_actual_arglist *arg;
2933 gfc_symbol *sym;
2934 const char *name;
2935 gfc_try t;
2936 int temp;
2937 procedure_type p = PROC_INTRINSIC;
2938 bool no_formal_args;
2939
2940 sym = NULL;
2941 if (expr->symtree)
2942 sym = expr->symtree->n.sym;
2943
2944 /* If this is a procedure pointer component, it has already been resolved. */
2945 if (gfc_is_proc_ptr_comp (expr, NULL))
2946 return SUCCESS;
2947
2948 if (sym && sym->attr.intrinsic
2949 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2950 return FAILURE;
2951
2952 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2953 {
2954 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2955 return FAILURE;
2956 }
2957
2958 /* If this ia a deferred TBP with an abstract interface (which may
2959 of course be referenced), expr->value.function.esym will be set. */
2960 if (sym && sym->attr.abstract && !expr->value.function.esym)
2961 {
2962 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2963 sym->name, &expr->where);
2964 return FAILURE;
2965 }
2966
2967 /* Switch off assumed size checking and do this again for certain kinds
2968 of procedure, once the procedure itself is resolved. */
2969 need_full_assumed_size++;
2970
2971 if (expr->symtree && expr->symtree->n.sym)
2972 p = expr->symtree->n.sym->attr.proc;
2973
2974 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2975 inquiry_argument = true;
2976 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2977
2978 if (resolve_actual_arglist (expr->value.function.actual,
2979 p, no_formal_args) == FAILURE)
2980 {
2981 inquiry_argument = false;
2982 return FAILURE;
2983 }
2984
2985 inquiry_argument = false;
2986
2987 /* Need to setup the call to the correct c_associated, depending on
2988 the number of cptrs to user gives to compare. */
2989 if (sym && sym->attr.is_iso_c == 1)
2990 {
2991 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2992 == FAILURE)
2993 return FAILURE;
2994
2995 /* Get the symtree for the new symbol (resolved func).
2996 the old one will be freed later, when it's no longer used. */
2997 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2998 }
2999
3000 /* Resume assumed_size checking. */
3001 need_full_assumed_size--;
3002
3003 /* If the procedure is external, check for usage. */
3004 if (sym && is_external_proc (sym))
3005 resolve_global_procedure (sym, &expr->where,
3006 &expr->value.function.actual, 0);
3007
3008 if (sym && sym->ts.type == BT_CHARACTER
3009 && sym->ts.u.cl
3010 && sym->ts.u.cl->length == NULL
3011 && !sym->attr.dummy
3012 && !sym->ts.deferred
3013 && expr->value.function.esym == NULL
3014 && !sym->attr.contained)
3015 {
3016 /* Internal procedures are taken care of in resolve_contained_fntype. */
3017 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3018 "be used at %L since it is not a dummy argument",
3019 sym->name, &expr->where);
3020 return FAILURE;
3021 }
3022
3023 /* See if function is already resolved. */
3024
3025 if (expr->value.function.name != NULL)
3026 {
3027 if (expr->ts.type == BT_UNKNOWN)
3028 expr->ts = sym->ts;
3029 t = SUCCESS;
3030 }
3031 else
3032 {
3033 /* Apply the rules of section 14.1.2. */
3034
3035 switch (procedure_kind (sym))
3036 {
3037 case PTYPE_GENERIC:
3038 t = resolve_generic_f (expr);
3039 break;
3040
3041 case PTYPE_SPECIFIC:
3042 t = resolve_specific_f (expr);
3043 break;
3044
3045 case PTYPE_UNKNOWN:
3046 t = resolve_unknown_f (expr);
3047 break;
3048
3049 default:
3050 gfc_internal_error ("resolve_function(): bad function type");
3051 }
3052 }
3053
3054 /* If the expression is still a function (it might have simplified),
3055 then we check to see if we are calling an elemental function. */
3056
3057 if (expr->expr_type != EXPR_FUNCTION)
3058 return t;
3059
3060 temp = need_full_assumed_size;
3061 need_full_assumed_size = 0;
3062
3063 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3064 return FAILURE;
3065
3066 if (omp_workshare_flag
3067 && expr->value.function.esym
3068 && ! gfc_elemental (expr->value.function.esym))
3069 {
3070 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3071 "in WORKSHARE construct", expr->value.function.esym->name,
3072 &expr->where);
3073 t = FAILURE;
3074 }
3075
3076 #define GENERIC_ID expr->value.function.isym->id
3077 else if (expr->value.function.actual != NULL
3078 && expr->value.function.isym != NULL
3079 && GENERIC_ID != GFC_ISYM_LBOUND
3080 && GENERIC_ID != GFC_ISYM_LEN
3081 && GENERIC_ID != GFC_ISYM_LOC
3082 && GENERIC_ID != GFC_ISYM_PRESENT)
3083 {
3084 /* Array intrinsics must also have the last upper bound of an
3085 assumed size array argument. UBOUND and SIZE have to be
3086 excluded from the check if the second argument is anything
3087 than a constant. */
3088
3089 for (arg = expr->value.function.actual; arg; arg = arg->next)
3090 {
3091 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3092 && arg->next != NULL && arg->next->expr)
3093 {
3094 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3095 break;
3096
3097 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3098 break;
3099
3100 if ((int)mpz_get_si (arg->next->expr->value.integer)
3101 < arg->expr->rank)
3102 break;
3103 }
3104
3105 if (arg->expr != NULL
3106 && arg->expr->rank > 0
3107 && resolve_assumed_size_actual (arg->expr))
3108 return FAILURE;
3109 }
3110 }
3111 #undef GENERIC_ID
3112
3113 need_full_assumed_size = temp;
3114 name = NULL;
3115
3116 if (!pure_function (expr, &name) && name)
3117 {
3118 if (forall_flag)
3119 {
3120 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3121 "FORALL %s", name, &expr->where,
3122 forall_flag == 2 ? "mask" : "block");
3123 t = FAILURE;
3124 }
3125 else if (gfc_pure (NULL))
3126 {
3127 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3128 "procedure within a PURE procedure", name, &expr->where);
3129 t = FAILURE;
3130 }
3131 }
3132
3133 if (!pure_function (expr, &name) && name && gfc_implicit_pure (NULL))
3134 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3135
3136 /* Functions without the RECURSIVE attribution are not allowed to
3137 * call themselves. */
3138 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3139 {
3140 gfc_symbol *esym;
3141 esym = expr->value.function.esym;
3142
3143 if (is_illegal_recursion (esym, gfc_current_ns))
3144 {
3145 if (esym->attr.entry && esym->ns->entries)
3146 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3147 " function '%s' is not RECURSIVE",
3148 esym->name, &expr->where, esym->ns->entries->sym->name);
3149 else
3150 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3151 " is not RECURSIVE", esym->name, &expr->where);
3152
3153 t = FAILURE;
3154 }
3155 }
3156
3157 /* Character lengths of use associated functions may contains references to
3158 symbols not referenced from the current program unit otherwise. Make sure
3159 those symbols are marked as referenced. */
3160
3161 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3162 && expr->value.function.esym->attr.use_assoc)
3163 {
3164 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3165 }
3166
3167 /* Make sure that the expression has a typespec that works. */
3168 if (expr->ts.type == BT_UNKNOWN)
3169 {
3170 if (expr->symtree->n.sym->result
3171 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3172 && !expr->symtree->n.sym->result->attr.proc_pointer)
3173 expr->ts = expr->symtree->n.sym->result->ts;
3174 }
3175
3176 return t;
3177 }
3178
3179
3180 /************* Subroutine resolution *************/
3181
3182 static void
3183 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3184 {
3185 if (gfc_pure (sym))
3186 return;
3187
3188 if (forall_flag)
3189 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3190 sym->name, &c->loc);
3191 else if (gfc_pure (NULL))
3192 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3193 &c->loc);
3194 }
3195
3196
3197 static match
3198 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3199 {
3200 gfc_symbol *s;
3201
3202 if (sym->attr.generic)
3203 {
3204 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3205 if (s != NULL)
3206 {
3207 c->resolved_sym = s;
3208 pure_subroutine (c, s);
3209 return MATCH_YES;
3210 }
3211
3212 /* TODO: Need to search for elemental references in generic interface. */
3213 }
3214
3215 if (sym->attr.intrinsic)
3216 return gfc_intrinsic_sub_interface (c, 0);
3217
3218 return MATCH_NO;
3219 }
3220
3221
3222 static gfc_try
3223 resolve_generic_s (gfc_code *c)
3224 {
3225 gfc_symbol *sym;
3226 match m;
3227
3228 sym = c->symtree->n.sym;
3229
3230 for (;;)
3231 {
3232 m = resolve_generic_s0 (c, sym);
3233 if (m == MATCH_YES)
3234 return SUCCESS;
3235 else if (m == MATCH_ERROR)
3236 return FAILURE;
3237
3238 generic:
3239 if (sym->ns->parent == NULL)
3240 break;
3241 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3242
3243 if (sym == NULL)
3244 break;
3245 if (!generic_sym (sym))
3246 goto generic;
3247 }
3248
3249 /* Last ditch attempt. See if the reference is to an intrinsic
3250 that possesses a matching interface. 14.1.2.4 */
3251 sym = c->symtree->n.sym;
3252
3253 if (!gfc_is_intrinsic (sym, 1, c->loc))
3254 {
3255 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3256 sym->name, &c->loc);
3257 return FAILURE;
3258 }
3259
3260 m = gfc_intrinsic_sub_interface (c, 0);
3261 if (m == MATCH_YES)
3262 return SUCCESS;
3263 if (m == MATCH_NO)
3264 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3265 "intrinsic subroutine interface", sym->name, &c->loc);
3266
3267 return FAILURE;
3268 }
3269
3270
3271 /* Set the name and binding label of the subroutine symbol in the call
3272 expression represented by 'c' to include the type and kind of the
3273 second parameter. This function is for resolving the appropriate
3274 version of c_f_pointer() and c_f_procpointer(). For example, a
3275 call to c_f_pointer() for a default integer pointer could have a
3276 name of c_f_pointer_i4. If no second arg exists, which is an error
3277 for these two functions, it defaults to the generic symbol's name
3278 and binding label. */
3279
3280 static void
3281 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3282 char *name, char *binding_label)
3283 {
3284 gfc_expr *arg = NULL;
3285 char type;
3286 int kind;
3287
3288 /* The second arg of c_f_pointer and c_f_procpointer determines
3289 the type and kind for the procedure name. */
3290 arg = c->ext.actual->next->expr;
3291
3292 if (arg != NULL)
3293 {
3294 /* Set up the name to have the given symbol's name,
3295 plus the type and kind. */
3296 /* a derived type is marked with the type letter 'u' */
3297 if (arg->ts.type == BT_DERIVED)
3298 {
3299 type = 'd';
3300 kind = 0; /* set the kind as 0 for now */
3301 }
3302 else
3303 {
3304 type = gfc_type_letter (arg->ts.type);
3305 kind = arg->ts.kind;
3306 }
3307
3308 if (arg->ts.type == BT_CHARACTER)
3309 /* Kind info for character strings not needed. */
3310 kind = 0;
3311
3312 sprintf (name, "%s_%c%d", sym->name, type, kind);
3313 /* Set up the binding label as the given symbol's label plus
3314 the type and kind. */
3315 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3316 }
3317 else
3318 {
3319 /* If the second arg is missing, set the name and label as
3320 was, cause it should at least be found, and the missing
3321 arg error will be caught by compare_parameters(). */
3322 sprintf (name, "%s", sym->name);
3323 sprintf (binding_label, "%s", sym->binding_label);
3324 }
3325
3326 return;
3327 }
3328
3329
3330 /* Resolve a generic version of the iso_c_binding procedure given
3331 (sym) to the specific one based on the type and kind of the
3332 argument(s). Currently, this function resolves c_f_pointer() and
3333 c_f_procpointer based on the type and kind of the second argument
3334 (FPTR). Other iso_c_binding procedures aren't specially handled.
3335 Upon successfully exiting, c->resolved_sym will hold the resolved
3336 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3337 otherwise. */
3338
3339 match
3340 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3341 {
3342 gfc_symbol *new_sym;
3343 /* this is fine, since we know the names won't use the max */
3344 char name[GFC_MAX_SYMBOL_LEN + 1];
3345 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3346 /* default to success; will override if find error */
3347 match m = MATCH_YES;
3348
3349 /* Make sure the actual arguments are in the necessary order (based on the
3350 formal args) before resolving. */
3351 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3352
3353 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3354 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3355 {
3356 set_name_and_label (c, sym, name, binding_label);
3357
3358 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3359 {
3360 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3361 {
3362 /* Make sure we got a third arg if the second arg has non-zero
3363 rank. We must also check that the type and rank are
3364 correct since we short-circuit this check in
3365 gfc_procedure_use() (called above to sort actual args). */
3366 if (c->ext.actual->next->expr->rank != 0)
3367 {
3368 if(c->ext.actual->next->next == NULL
3369 || c->ext.actual->next->next->expr == NULL)
3370 {
3371 m = MATCH_ERROR;
3372 gfc_error ("Missing SHAPE parameter for call to %s "
3373 "at %L", sym->name, &(c->loc));
3374 }
3375 else if (c->ext.actual->next->next->expr->ts.type
3376 != BT_INTEGER
3377 || c->ext.actual->next->next->expr->rank != 1)
3378 {
3379 m = MATCH_ERROR;
3380 gfc_error ("SHAPE parameter for call to %s at %L must "
3381 "be a rank 1 INTEGER array", sym->name,
3382 &(c->loc));
3383 }
3384 }
3385 }
3386 }
3387
3388 if (m != MATCH_ERROR)
3389 {
3390 /* the 1 means to add the optional arg to formal list */
3391 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3392
3393 /* for error reporting, say it's declared where the original was */
3394 new_sym->declared_at = sym->declared_at;
3395 }
3396 }
3397 else
3398 {
3399 /* no differences for c_loc or c_funloc */
3400 new_sym = sym;
3401 }
3402
3403 /* set the resolved symbol */
3404 if (m != MATCH_ERROR)
3405 c->resolved_sym = new_sym;
3406 else
3407 c->resolved_sym = sym;
3408
3409 return m;
3410 }
3411
3412
3413 /* Resolve a subroutine call known to be specific. */
3414
3415 static match
3416 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3417 {
3418 match m;
3419
3420 if(sym->attr.is_iso_c)
3421 {
3422 m = gfc_iso_c_sub_interface (c,sym);
3423 return m;
3424 }
3425
3426 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3427 {
3428 if (sym->attr.dummy)
3429 {
3430 sym->attr.proc = PROC_DUMMY;
3431 goto found;
3432 }
3433
3434 sym->attr.proc = PROC_EXTERNAL;
3435 goto found;
3436 }
3437
3438 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3439 goto found;
3440
3441 if (sym->attr.intrinsic)
3442 {
3443 m = gfc_intrinsic_sub_interface (c, 1);
3444 if (m == MATCH_YES)
3445 return MATCH_YES;
3446 if (m == MATCH_NO)
3447 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3448 "with an intrinsic", sym->name, &c->loc);
3449
3450 return MATCH_ERROR;
3451 }
3452
3453 return MATCH_NO;
3454
3455 found:
3456 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3457
3458 c->resolved_sym = sym;
3459 pure_subroutine (c, sym);
3460
3461 return MATCH_YES;
3462 }
3463
3464
3465 static gfc_try
3466 resolve_specific_s (gfc_code *c)
3467 {
3468 gfc_symbol *sym;
3469 match m;
3470
3471 sym = c->symtree->n.sym;
3472
3473 for (;;)
3474 {
3475 m = resolve_specific_s0 (c, sym);
3476 if (m == MATCH_YES)
3477 return SUCCESS;
3478 if (m == MATCH_ERROR)
3479 return FAILURE;
3480
3481 if (sym->ns->parent == NULL)
3482 break;
3483
3484 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3485
3486 if (sym == NULL)
3487 break;
3488 }
3489
3490 sym = c->symtree->n.sym;
3491 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3492 sym->name, &c->loc);
3493
3494 return FAILURE;
3495 }
3496
3497
3498 /* Resolve a subroutine call not known to be generic nor specific. */
3499
3500 static gfc_try
3501 resolve_unknown_s (gfc_code *c)
3502 {
3503 gfc_symbol *sym;
3504
3505 sym = c->symtree->n.sym;
3506
3507 if (sym->attr.dummy)
3508 {
3509 sym->attr.proc = PROC_DUMMY;
3510 goto found;
3511 }
3512
3513 /* See if we have an intrinsic function reference. */
3514
3515 if (gfc_is_intrinsic (sym, 1, c->loc))
3516 {
3517 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3518 return SUCCESS;
3519 return FAILURE;
3520 }
3521
3522 /* The reference is to an external name. */
3523
3524 found:
3525 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3526
3527 c->resolved_sym = sym;
3528
3529 pure_subroutine (c, sym);
3530
3531 return SUCCESS;
3532 }
3533
3534
3535 /* Resolve a subroutine call. Although it was tempting to use the same code
3536 for functions, subroutines and functions are stored differently and this
3537 makes things awkward. */
3538
3539 static gfc_try
3540 resolve_call (gfc_code *c)
3541 {
3542 gfc_try t;
3543 procedure_type ptype = PROC_INTRINSIC;
3544 gfc_symbol *csym, *sym;
3545 bool no_formal_args;
3546
3547 csym = c->symtree ? c->symtree->n.sym : NULL;
3548
3549 if (csym && csym->ts.type != BT_UNKNOWN)
3550 {
3551 gfc_error ("'%s' at %L has a type, which is not consistent with "
3552 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3553 return FAILURE;
3554 }
3555
3556 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3557 {
3558 gfc_symtree *st;
3559 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3560 sym = st ? st->n.sym : NULL;
3561 if (sym && csym != sym
3562 && sym->ns == gfc_current_ns
3563 && sym->attr.flavor == FL_PROCEDURE
3564 && sym->attr.contained)
3565 {
3566 sym->refs++;
3567 if (csym->attr.generic)
3568 c->symtree->n.sym = sym;
3569 else
3570 c->symtree = st;
3571 csym = c->symtree->n.sym;
3572 }
3573 }
3574
3575 /* If this ia a deferred TBP with an abstract interface
3576 (which may of course be referenced), c->expr1 will be set. */
3577 if (csym && csym->attr.abstract && !c->expr1)
3578 {
3579 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3580 csym->name, &c->loc);
3581 return FAILURE;
3582 }
3583
3584 /* Subroutines without the RECURSIVE attribution are not allowed to
3585 * call themselves. */
3586 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3587 {
3588 if (csym->attr.entry && csym->ns->entries)
3589 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3590 " subroutine '%s' is not RECURSIVE",
3591 csym->name, &c->loc, csym->ns->entries->sym->name);
3592 else
3593 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3594 " is not RECURSIVE", csym->name, &c->loc);
3595
3596 t = FAILURE;
3597 }
3598
3599 /* Switch off assumed size checking and do this again for certain kinds
3600 of procedure, once the procedure itself is resolved. */
3601 need_full_assumed_size++;
3602
3603 if (csym)
3604 ptype = csym->attr.proc;
3605
3606 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3607 if (resolve_actual_arglist (c->ext.actual, ptype,
3608 no_formal_args) == FAILURE)
3609 return FAILURE;
3610
3611 /* Resume assumed_size checking. */
3612 need_full_assumed_size--;
3613
3614 /* If external, check for usage. */
3615 if (csym && is_external_proc (csym))
3616 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3617
3618 t = SUCCESS;
3619 if (c->resolved_sym == NULL)
3620 {
3621 c->resolved_isym = NULL;
3622 switch (procedure_kind (csym))
3623 {
3624 case PTYPE_GENERIC:
3625 t = resolve_generic_s (c);
3626 break;
3627
3628 case PTYPE_SPECIFIC:
3629 t = resolve_specific_s (c);
3630 break;
3631
3632 case PTYPE_UNKNOWN:
3633 t = resolve_unknown_s (c);
3634 break;
3635
3636 default:
3637 gfc_internal_error ("resolve_subroutine(): bad function type");
3638 }
3639 }
3640
3641 /* Some checks of elemental subroutine actual arguments. */
3642 if (resolve_elemental_actual (NULL, c) == FAILURE)
3643 return FAILURE;
3644
3645 return t;
3646 }
3647
3648
3649 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3650 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3651 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3652 if their shapes do not match. If either op1->shape or op2->shape is
3653 NULL, return SUCCESS. */
3654
3655 static gfc_try
3656 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3657 {
3658 gfc_try t;
3659 int i;
3660
3661 t = SUCCESS;
3662
3663 if (op1->shape != NULL && op2->shape != NULL)
3664 {
3665 for (i = 0; i < op1->rank; i++)
3666 {
3667 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3668 {
3669 gfc_error ("Shapes for operands at %L and %L are not conformable",
3670 &op1->where, &op2->where);
3671 t = FAILURE;
3672 break;
3673 }
3674 }
3675 }
3676
3677 return t;
3678 }
3679
3680
3681 /* Resolve an operator expression node. This can involve replacing the
3682 operation with a user defined function call. */
3683
3684 static gfc_try
3685 resolve_operator (gfc_expr *e)
3686 {
3687 gfc_expr *op1, *op2;
3688 char msg[200];
3689 bool dual_locus_error;
3690 gfc_try t;
3691
3692 /* Resolve all subnodes-- give them types. */
3693
3694 switch (e->value.op.op)
3695 {
3696 default:
3697 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3698 return FAILURE;
3699
3700 /* Fall through... */
3701
3702 case INTRINSIC_NOT:
3703 case INTRINSIC_UPLUS:
3704 case INTRINSIC_UMINUS:
3705 case INTRINSIC_PARENTHESES:
3706 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3707 return FAILURE;
3708 break;
3709 }
3710
3711 /* Typecheck the new node. */
3712
3713 op1 = e->value.op.op1;
3714 op2 = e->value.op.op2;
3715 dual_locus_error = false;
3716
3717 if ((op1 && op1->expr_type == EXPR_NULL)
3718 || (op2 && op2->expr_type == EXPR_NULL))
3719 {
3720 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3721 goto bad_op;
3722 }
3723
3724 switch (e->value.op.op)
3725 {
3726 case INTRINSIC_UPLUS:
3727 case INTRINSIC_UMINUS:
3728 if (op1->ts.type == BT_INTEGER
3729 || op1->ts.type == BT_REAL
3730 || op1->ts.type == BT_COMPLEX)
3731 {
3732 e->ts = op1->ts;
3733 break;
3734 }
3735
3736 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3737 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3738 goto bad_op;
3739
3740 case INTRINSIC_PLUS:
3741 case INTRINSIC_MINUS:
3742 case INTRINSIC_TIMES:
3743 case INTRINSIC_DIVIDE:
3744 case INTRINSIC_POWER:
3745 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3746 {
3747 gfc_type_convert_binary (e, 1);
3748 break;
3749 }
3750
3751 sprintf (msg,
3752 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3753 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3754 gfc_typename (&op2->ts));
3755 goto bad_op;
3756
3757 case INTRINSIC_CONCAT:
3758 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3759 && op1->ts.kind == op2->ts.kind)
3760 {
3761 e->ts.type = BT_CHARACTER;
3762 e->ts.kind = op1->ts.kind;
3763 break;
3764 }
3765
3766 sprintf (msg,
3767 _("Operands of string concatenation operator at %%L are %s/%s"),
3768 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3769 goto bad_op;
3770
3771 case INTRINSIC_AND:
3772 case INTRINSIC_OR:
3773 case INTRINSIC_EQV:
3774 case INTRINSIC_NEQV:
3775 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3776 {
3777 e->ts.type = BT_LOGICAL;
3778 e->ts.kind = gfc_kind_max (op1, op2);
3779 if (op1->ts.kind < e->ts.kind)
3780 gfc_convert_type (op1, &e->ts, 2);
3781 else if (op2->ts.kind < e->ts.kind)
3782 gfc_convert_type (op2, &e->ts, 2);
3783 break;
3784 }
3785
3786 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3787 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3788 gfc_typename (&op2->ts));
3789
3790 goto bad_op;
3791
3792 case INTRINSIC_NOT:
3793 if (op1->ts.type == BT_LOGICAL)
3794 {
3795 e->ts.type = BT_LOGICAL;
3796 e->ts.kind = op1->ts.kind;
3797 break;
3798 }
3799
3800 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3801 gfc_typename (&op1->ts));
3802 goto bad_op;
3803
3804 case INTRINSIC_GT:
3805 case INTRINSIC_GT_OS:
3806 case INTRINSIC_GE:
3807 case INTRINSIC_GE_OS:
3808 case INTRINSIC_LT:
3809 case INTRINSIC_LT_OS:
3810 case INTRINSIC_LE:
3811 case INTRINSIC_LE_OS:
3812 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3813 {
3814 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3815 goto bad_op;
3816 }
3817
3818 /* Fall through... */
3819
3820 case INTRINSIC_EQ:
3821 case INTRINSIC_EQ_OS:
3822 case INTRINSIC_NE:
3823 case INTRINSIC_NE_OS:
3824 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3825 && op1->ts.kind == op2->ts.kind)
3826 {
3827 e->ts.type = BT_LOGICAL;
3828 e->ts.kind = gfc_default_logical_kind;
3829 break;
3830 }
3831
3832 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3833 {
3834 gfc_type_convert_binary (e, 1);
3835
3836 e->ts.type = BT_LOGICAL;
3837 e->ts.kind = gfc_default_logical_kind;
3838 break;
3839 }
3840
3841 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3842 sprintf (msg,
3843 _("Logicals at %%L must be compared with %s instead of %s"),
3844 (e->value.op.op == INTRINSIC_EQ
3845 || e->value.op.op == INTRINSIC_EQ_OS)
3846 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3847 else
3848 sprintf (msg,
3849 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3850 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3851 gfc_typename (&op2->ts));
3852
3853 goto bad_op;
3854
3855 case INTRINSIC_USER:
3856 if (e->value.op.uop->op == NULL)
3857 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3858 else if (op2 == NULL)
3859 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3860 e->value.op.uop->name, gfc_typename (&op1->ts));
3861 else
3862 {
3863 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3864 e->value.op.uop->name, gfc_typename (&op1->ts),
3865 gfc_typename (&op2->ts));
3866 e->value.op.uop->op->sym->attr.referenced = 1;
3867 }
3868
3869 goto bad_op;
3870
3871 case INTRINSIC_PARENTHESES:
3872 e->ts = op1->ts;
3873 if (e->ts.type == BT_CHARACTER)
3874 e->ts.u.cl = op1->ts.u.cl;
3875 break;
3876
3877 default:
3878 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3879 }
3880
3881 /* Deal with arrayness of an operand through an operator. */
3882
3883 t = SUCCESS;
3884
3885 switch (e->value.op.op)
3886 {
3887 case INTRINSIC_PLUS:
3888 case INTRINSIC_MINUS:
3889 case INTRINSIC_TIMES:
3890 case INTRINSIC_DIVIDE:
3891 case INTRINSIC_POWER:
3892 case INTRINSIC_CONCAT:
3893 case INTRINSIC_AND:
3894 case INTRINSIC_OR:
3895 case INTRINSIC_EQV:
3896 case INTRINSIC_NEQV:
3897 case INTRINSIC_EQ:
3898 case INTRINSIC_EQ_OS:
3899 case INTRINSIC_NE:
3900 case INTRINSIC_NE_OS:
3901 case INTRINSIC_GT:
3902 case INTRINSIC_GT_OS:
3903 case INTRINSIC_GE:
3904 case INTRINSIC_GE_OS:
3905 case INTRINSIC_LT:
3906 case INTRINSIC_LT_OS:
3907 case INTRINSIC_LE:
3908 case INTRINSIC_LE_OS:
3909
3910 if (op1->rank == 0 && op2->rank == 0)
3911 e->rank = 0;
3912
3913 if (op1->rank == 0 && op2->rank != 0)
3914 {
3915 e->rank = op2->rank;
3916
3917 if (e->shape == NULL)
3918 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3919 }
3920
3921 if (op1->rank != 0 && op2->rank == 0)
3922 {
3923 e->rank = op1->rank;
3924
3925 if (e->shape == NULL)
3926 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3927 }
3928
3929 if (op1->rank != 0 && op2->rank != 0)
3930 {
3931 if (op1->rank == op2->rank)
3932 {
3933 e->rank = op1->rank;
3934 if (e->shape == NULL)
3935 {
3936 t = compare_shapes (op1, op2);
3937 if (t == FAILURE)
3938 e->shape = NULL;
3939 else
3940 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3941 }
3942 }
3943 else
3944 {
3945 /* Allow higher level expressions to work. */
3946 e->rank = 0;
3947
3948 /* Try user-defined operators, and otherwise throw an error. */
3949 dual_locus_error = true;
3950 sprintf (msg,
3951 _("Inconsistent ranks for operator at %%L and %%L"));
3952 goto bad_op;
3953 }
3954 }
3955
3956 break;
3957
3958 case INTRINSIC_PARENTHESES:
3959 case INTRINSIC_NOT:
3960 case INTRINSIC_UPLUS:
3961 case INTRINSIC_UMINUS:
3962 /* Simply copy arrayness attribute */
3963 e->rank = op1->rank;
3964
3965 if (e->shape == NULL)
3966 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3967
3968 break;
3969
3970 default:
3971 break;
3972 }
3973
3974 /* Attempt to simplify the expression. */
3975 if (t == SUCCESS)
3976 {
3977 t = gfc_simplify_expr (e, 0);
3978 /* Some calls do not succeed in simplification and return FAILURE
3979 even though there is no error; e.g. variable references to
3980 PARAMETER arrays. */
3981 if (!gfc_is_constant_expr (e))
3982 t = SUCCESS;
3983 }
3984 return t;
3985
3986 bad_op:
3987
3988 {
3989 bool real_error;
3990 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3991 return SUCCESS;
3992
3993 if (real_error)
3994 return FAILURE;
3995 }
3996
3997 if (dual_locus_error)
3998 gfc_error (msg, &op1->where, &op2->where);
3999 else
4000 gfc_error (msg, &e->where);
4001
4002 return FAILURE;
4003 }
4004
4005
4006 /************** Array resolution subroutines **************/
4007
4008 typedef enum
4009 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
4010 comparison;
4011
4012 /* Compare two integer expressions. */
4013
4014 static comparison
4015 compare_bound (gfc_expr *a, gfc_expr *b)
4016 {
4017 int i;
4018
4019 if (a == NULL || a->expr_type != EXPR_CONSTANT
4020 || b == NULL || b->expr_type != EXPR_CONSTANT)
4021 return CMP_UNKNOWN;
4022
4023 /* If either of the types isn't INTEGER, we must have
4024 raised an error earlier. */
4025
4026 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4027 return CMP_UNKNOWN;
4028
4029 i = mpz_cmp (a->value.integer, b->value.integer);
4030
4031 if (i < 0)
4032 return CMP_LT;
4033 if (i > 0)
4034 return CMP_GT;
4035 return CMP_EQ;
4036 }
4037
4038
4039 /* Compare an integer expression with an integer. */
4040
4041 static comparison
4042 compare_bound_int (gfc_expr *a, int b)
4043 {
4044 int i;
4045
4046 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4047 return CMP_UNKNOWN;
4048
4049 if (a->ts.type != BT_INTEGER)
4050 gfc_internal_error ("compare_bound_int(): Bad expression");
4051
4052 i = mpz_cmp_si (a->value.integer, b);
4053
4054 if (i < 0)
4055 return CMP_LT;
4056 if (i > 0)
4057 return CMP_GT;
4058 return CMP_EQ;
4059 }
4060
4061
4062 /* Compare an integer expression with a mpz_t. */
4063
4064 static comparison
4065 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4066 {
4067 int i;
4068
4069 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4070 return CMP_UNKNOWN;
4071
4072 if (a->ts.type != BT_INTEGER)
4073 gfc_internal_error ("compare_bound_int(): Bad expression");
4074
4075 i = mpz_cmp (a->value.integer, b);
4076
4077 if (i < 0)
4078 return CMP_LT;
4079 if (i > 0)
4080 return CMP_GT;
4081 return CMP_EQ;
4082 }
4083
4084
4085 /* Compute the last value of a sequence given by a triplet.
4086 Return 0 if it wasn't able to compute the last value, or if the
4087 sequence if empty, and 1 otherwise. */
4088
4089 static int
4090 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4091 gfc_expr *stride, mpz_t last)
4092 {
4093 mpz_t rem;
4094
4095 if (start == NULL || start->expr_type != EXPR_CONSTANT
4096 || end == NULL || end->expr_type != EXPR_CONSTANT
4097 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4098 return 0;
4099
4100 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4101 || (stride != NULL && stride->ts.type != BT_INTEGER))
4102 return 0;
4103
4104 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4105 {
4106 if (compare_bound (start, end) == CMP_GT)
4107 return 0;
4108 mpz_set (last, end->value.integer);
4109 return 1;
4110 }
4111
4112 if (compare_bound_int (stride, 0) == CMP_GT)
4113 {
4114 /* Stride is positive */
4115 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4116 return 0;
4117 }
4118 else
4119 {
4120 /* Stride is negative */
4121 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4122 return 0;
4123 }
4124
4125 mpz_init (rem);
4126 mpz_sub (rem, end->value.integer, start->value.integer);
4127 mpz_tdiv_r (rem, rem, stride->value.integer);
4128 mpz_sub (last, end->value.integer, rem);
4129 mpz_clear (rem);
4130
4131 return 1;
4132 }
4133
4134
4135 /* Compare a single dimension of an array reference to the array
4136 specification. */
4137
4138 static gfc_try
4139 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4140 {
4141 mpz_t last_value;
4142
4143 if (ar->dimen_type[i] == DIMEN_STAR)
4144 {
4145 gcc_assert (ar->stride[i] == NULL);
4146 /* This implies [*] as [*:] and [*:3] are not possible. */
4147 if (ar->start[i] == NULL)
4148 {
4149 gcc_assert (ar->end[i] == NULL);
4150 return SUCCESS;
4151 }
4152 }
4153
4154 /* Given start, end and stride values, calculate the minimum and
4155 maximum referenced indexes. */
4156
4157 switch (ar->dimen_type[i])
4158 {
4159 case DIMEN_VECTOR:
4160 case DIMEN_THIS_IMAGE:
4161 break;
4162
4163 case DIMEN_STAR:
4164 case DIMEN_ELEMENT:
4165 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4166 {
4167 if (i < as->rank)
4168 gfc_warning ("Array reference at %L is out of bounds "
4169 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4170 mpz_get_si (ar->start[i]->value.integer),
4171 mpz_get_si (as->lower[i]->value.integer), i+1);
4172 else
4173 gfc_warning ("Array reference at %L is out of bounds "
4174 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4175 mpz_get_si (ar->start[i]->value.integer),
4176 mpz_get_si (as->lower[i]->value.integer),
4177 i + 1 - as->rank);
4178 return SUCCESS;
4179 }
4180 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4181 {
4182 if (i < as->rank)
4183 gfc_warning ("Array reference at %L is out of bounds "
4184 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4185 mpz_get_si (ar->start[i]->value.integer),
4186 mpz_get_si (as->upper[i]->value.integer), i+1);
4187 else
4188 gfc_warning ("Array reference at %L is out of bounds "
4189 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4190 mpz_get_si (ar->start[i]->value.integer),
4191 mpz_get_si (as->upper[i]->value.integer),
4192 i + 1 - as->rank);
4193 return SUCCESS;
4194 }
4195
4196 break;
4197
4198 case DIMEN_RANGE:
4199 {
4200 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4201 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4202
4203 comparison comp_start_end = compare_bound (AR_START, AR_END);
4204
4205 /* Check for zero stride, which is not allowed. */
4206 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4207 {
4208 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4209 return FAILURE;
4210 }
4211
4212 /* if start == len || (stride > 0 && start < len)
4213 || (stride < 0 && start > len),
4214 then the array section contains at least one element. In this
4215 case, there is an out-of-bounds access if
4216 (start < lower || start > upper). */
4217 if (compare_bound (AR_START, AR_END) == CMP_EQ
4218 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4219 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4220 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4221 && comp_start_end == CMP_GT))
4222 {
4223 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4224 {
4225 gfc_warning ("Lower array reference at %L is out of bounds "
4226 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4227 mpz_get_si (AR_START->value.integer),
4228 mpz_get_si (as->lower[i]->value.integer), i+1);
4229 return SUCCESS;
4230 }
4231 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4232 {
4233 gfc_warning ("Lower array reference at %L is out of bounds "
4234 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4235 mpz_get_si (AR_START->value.integer),
4236 mpz_get_si (as->upper[i]->value.integer), i+1);
4237 return SUCCESS;
4238 }
4239 }
4240
4241 /* If we can compute the highest index of the array section,
4242 then it also has to be between lower and upper. */
4243 mpz_init (last_value);
4244 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4245 last_value))
4246 {
4247 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4248 {
4249 gfc_warning ("Upper array reference at %L is out of bounds "
4250 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4251 mpz_get_si (last_value),
4252 mpz_get_si (as->lower[i]->value.integer), i+1);
4253 mpz_clear (last_value);
4254 return SUCCESS;
4255 }
4256 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4257 {
4258 gfc_warning ("Upper array reference at %L is out of bounds "
4259 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4260 mpz_get_si (last_value),
4261 mpz_get_si (as->upper[i]->value.integer), i+1);
4262 mpz_clear (last_value);
4263 return SUCCESS;
4264 }
4265 }
4266 mpz_clear (last_value);
4267
4268 #undef AR_START
4269 #undef AR_END
4270 }
4271 break;
4272
4273 default:
4274 gfc_internal_error ("check_dimension(): Bad array reference");
4275 }
4276
4277 return SUCCESS;
4278 }
4279
4280
4281 /* Compare an array reference with an array specification. */
4282
4283 static gfc_try
4284 compare_spec_to_ref (gfc_array_ref *ar)
4285 {
4286 gfc_array_spec *as;
4287 int i;
4288
4289 as = ar->as;
4290 i = as->rank - 1;
4291 /* TODO: Full array sections are only allowed as actual parameters. */
4292 if (as->type == AS_ASSUMED_SIZE
4293 && (/*ar->type == AR_FULL
4294 ||*/ (ar->type == AR_SECTION
4295 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4296 {
4297 gfc_error ("Rightmost upper bound of assumed size array section "
4298 "not specified at %L", &ar->where);
4299 return FAILURE;
4300 }
4301
4302 if (ar->type == AR_FULL)
4303 return SUCCESS;
4304
4305 if (as->rank != ar->dimen)
4306 {
4307 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4308 &ar->where, ar->dimen, as->rank);
4309 return FAILURE;
4310 }
4311
4312 /* ar->codimen == 0 is a local array. */
4313 if (as->corank != ar->codimen && ar->codimen != 0)
4314 {
4315 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4316 &ar->where, ar->codimen, as->corank);
4317 return FAILURE;
4318 }
4319
4320 for (i = 0; i < as->rank; i++)
4321 if (check_dimension (i, ar, as) == FAILURE)
4322 return FAILURE;
4323
4324 /* Local access has no coarray spec. */
4325 if (ar->codimen != 0)
4326 for (i = as->rank; i < as->rank + as->corank; i++)
4327 {
4328 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4329 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4330 {
4331 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4332 i + 1 - as->rank, &ar->where);
4333 return FAILURE;
4334 }
4335 if (check_dimension (i, ar, as) == FAILURE)
4336 return FAILURE;
4337 }
4338
4339 if (as->corank && ar->codimen == 0)
4340 {
4341 int n;
4342 ar->codimen = as->corank;
4343 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4344 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4345 }
4346
4347 return SUCCESS;
4348 }
4349
4350
4351 /* Resolve one part of an array index. */
4352
4353 static gfc_try
4354 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4355 int force_index_integer_kind)
4356 {
4357 gfc_typespec ts;
4358
4359 if (index == NULL)
4360 return SUCCESS;
4361
4362 if (gfc_resolve_expr (index) == FAILURE)
4363 return FAILURE;
4364
4365 if (check_scalar && index->rank != 0)
4366 {
4367 gfc_error ("Array index at %L must be scalar", &index->where);
4368 return FAILURE;
4369 }
4370
4371 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4372 {
4373 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4374 &index->where, gfc_basic_typename (index->ts.type));
4375 return FAILURE;
4376 }
4377
4378 if (index->ts.type == BT_REAL)
4379 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4380 &index->where) == FAILURE)
4381 return FAILURE;
4382
4383 if ((index->ts.kind != gfc_index_integer_kind
4384 && force_index_integer_kind)
4385 || index->ts.type != BT_INTEGER)
4386 {
4387 gfc_clear_ts (&ts);
4388 ts.type = BT_INTEGER;
4389 ts.kind = gfc_index_integer_kind;
4390
4391 gfc_convert_type_warn (index, &ts, 2, 0);
4392 }
4393
4394 return SUCCESS;
4395 }
4396
4397 /* Resolve one part of an array index. */
4398
4399 gfc_try
4400 gfc_resolve_index (gfc_expr *index, int check_scalar)
4401 {
4402 return gfc_resolve_index_1 (index, check_scalar, 1);
4403 }
4404
4405 /* Resolve a dim argument to an intrinsic function. */
4406
4407 gfc_try
4408 gfc_resolve_dim_arg (gfc_expr *dim)
4409 {
4410 if (dim == NULL)
4411 return SUCCESS;
4412
4413 if (gfc_resolve_expr (dim) == FAILURE)
4414 return FAILURE;
4415
4416 if (dim->rank != 0)
4417 {
4418 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4419 return FAILURE;
4420
4421 }
4422
4423 if (dim->ts.type != BT_INTEGER)
4424 {
4425 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4426 return FAILURE;
4427 }
4428
4429 if (dim->ts.kind != gfc_index_integer_kind)
4430 {
4431 gfc_typespec ts;
4432
4433 gfc_clear_ts (&ts);
4434 ts.type = BT_INTEGER;
4435 ts.kind = gfc_index_integer_kind;
4436
4437 gfc_convert_type_warn (dim, &ts, 2, 0);
4438 }
4439
4440 return SUCCESS;
4441 }
4442
4443 /* Given an expression that contains array references, update those array
4444 references to point to the right array specifications. While this is
4445 filled in during matching, this information is difficult to save and load
4446 in a module, so we take care of it here.
4447
4448 The idea here is that the original array reference comes from the
4449 base symbol. We traverse the list of reference structures, setting
4450 the stored reference to references. Component references can
4451 provide an additional array specification. */
4452
4453 static void
4454 find_array_spec (gfc_expr *e)
4455 {
4456 gfc_array_spec *as;
4457 gfc_component *c;
4458 gfc_symbol *derived;
4459 gfc_ref *ref;
4460
4461 if (e->symtree->n.sym->ts.type == BT_CLASS)
4462 as = CLASS_DATA (e->symtree->n.sym)->as;
4463 else
4464 as = e->symtree->n.sym->as;
4465 derived = NULL;
4466
4467 for (ref = e->ref; ref; ref = ref->next)
4468 switch (ref->type)
4469 {
4470 case REF_ARRAY:
4471 if (as == NULL)
4472 gfc_internal_error ("find_array_spec(): Missing spec");
4473
4474 ref->u.ar.as = as;
4475 as = NULL;
4476 break;
4477
4478 case REF_COMPONENT:
4479 if (derived == NULL)
4480 derived = e->symtree->n.sym->ts.u.derived;
4481
4482 if (derived->attr.is_class)
4483 derived = derived->components->ts.u.derived;
4484
4485 c = derived->components;
4486
4487 for (; c; c = c->next)
4488 if (c == ref->u.c.component)
4489 {
4490 /* Track the sequence of component references. */
4491 if (c->ts.type == BT_DERIVED)
4492 derived = c->ts.u.derived;
4493 break;
4494 }
4495
4496 if (c == NULL)
4497 gfc_internal_error ("find_array_spec(): Component not found");
4498
4499 if (c->attr.dimension)
4500 {
4501 if (as != NULL)
4502 gfc_internal_error ("find_array_spec(): unused as(1)");
4503 as = c->as;
4504 }
4505
4506 break;
4507
4508 case REF_SUBSTRING:
4509 break;
4510 }
4511
4512 if (as != NULL)
4513 gfc_internal_error ("find_array_spec(): unused as(2)");
4514 }
4515
4516
4517 /* Resolve an array reference. */
4518
4519 static gfc_try
4520 resolve_array_ref (gfc_array_ref *ar)
4521 {
4522 int i, check_scalar;
4523 gfc_expr *e;
4524
4525 for (i = 0; i < ar->dimen + ar->codimen; i++)
4526 {
4527 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4528
4529 /* Do not force gfc_index_integer_kind for the start. We can
4530 do fine with any integer kind. This avoids temporary arrays
4531 created for indexing with a vector. */
4532 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4533 return FAILURE;
4534 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4535 return FAILURE;
4536 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4537 return FAILURE;
4538
4539 e = ar->start[i];
4540
4541 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4542 switch (e->rank)
4543 {
4544 case 0:
4545 ar->dimen_type[i] = DIMEN_ELEMENT;
4546 break;
4547
4548 case 1:
4549 ar->dimen_type[i] = DIMEN_VECTOR;
4550 if (e->expr_type == EXPR_VARIABLE
4551 && e->symtree->n.sym->ts.type == BT_DERIVED)
4552 ar->start[i] = gfc_get_parentheses (e);
4553 break;
4554
4555 default:
4556 gfc_error ("Array index at %L is an array of rank %d",
4557 &ar->c_where[i], e->rank);
4558 return FAILURE;
4559 }
4560
4561 /* Fill in the upper bound, which may be lower than the
4562 specified one for something like a(2:10:5), which is
4563 identical to a(2:7:5). Only relevant for strides not equal
4564 to one. */
4565 if (ar->dimen_type[i] == DIMEN_RANGE
4566 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4567 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0)
4568 {
4569 mpz_t size, end;
4570
4571 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4572 {
4573 if (ar->end[i] == NULL)
4574 {
4575 ar->end[i] =
4576 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4577 &ar->where);
4578 mpz_set (ar->end[i]->value.integer, end);
4579 }
4580 else if (ar->end[i]->ts.type == BT_INTEGER
4581 && ar->end[i]->expr_type == EXPR_CONSTANT)
4582 {
4583 mpz_set (ar->end[i]->value.integer, end);
4584 }
4585 else
4586 gcc_unreachable ();
4587
4588 mpz_clear (size);
4589 mpz_clear (end);
4590 }
4591 }
4592 }
4593
4594 if (ar->type == AR_FULL && ar->as->rank == 0)
4595 ar->type = AR_ELEMENT;
4596
4597 /* If the reference type is unknown, figure out what kind it is. */
4598
4599 if (ar->type == AR_UNKNOWN)
4600 {
4601 ar->type = AR_ELEMENT;
4602 for (i = 0; i < ar->dimen; i++)
4603 if (ar->dimen_type[i] == DIMEN_RANGE
4604 || ar->dimen_type[i] == DIMEN_VECTOR)
4605 {
4606 ar->type = AR_SECTION;
4607 break;
4608 }
4609 }
4610
4611 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4612 return FAILURE;
4613
4614 return SUCCESS;
4615 }
4616
4617
4618 static gfc_try
4619 resolve_substring (gfc_ref *ref)
4620 {
4621 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4622
4623 if (ref->u.ss.start != NULL)
4624 {
4625 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4626 return FAILURE;
4627
4628 if (ref->u.ss.start->ts.type != BT_INTEGER)
4629 {
4630 gfc_error ("Substring start index at %L must be of type INTEGER",
4631 &ref->u.ss.start->where);
4632 return FAILURE;
4633 }
4634
4635 if (ref->u.ss.start->rank != 0)
4636 {
4637 gfc_error ("Substring start index at %L must be scalar",
4638 &ref->u.ss.start->where);
4639 return FAILURE;
4640 }
4641
4642 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4643 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4644 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4645 {
4646 gfc_error ("Substring start index at %L is less than one",
4647 &ref->u.ss.start->where);
4648 return FAILURE;
4649 }
4650 }
4651
4652 if (ref->u.ss.end != NULL)
4653 {
4654 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4655 return FAILURE;
4656
4657 if (ref->u.ss.end->ts.type != BT_INTEGER)
4658 {
4659 gfc_error ("Substring end index at %L must be of type INTEGER",
4660 &ref->u.ss.end->where);
4661 return FAILURE;
4662 }
4663
4664 if (ref->u.ss.end->rank != 0)
4665 {
4666 gfc_error ("Substring end index at %L must be scalar",
4667 &ref->u.ss.end->where);
4668 return FAILURE;
4669 }
4670
4671 if (ref->u.ss.length != NULL
4672 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4673 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4674 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4675 {
4676 gfc_error ("Substring end index at %L exceeds the string length",
4677 &ref->u.ss.start->where);
4678 return FAILURE;
4679 }
4680
4681 if (compare_bound_mpz_t (ref->u.ss.end,
4682 gfc_integer_kinds[k].huge) == CMP_GT
4683 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4684 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4685 {
4686 gfc_error ("Substring end index at %L is too large",
4687 &ref->u.ss.end->where);
4688 return FAILURE;
4689 }
4690 }
4691
4692 return SUCCESS;
4693 }
4694
4695
4696 /* This function supplies missing substring charlens. */
4697
4698 void
4699 gfc_resolve_substring_charlen (gfc_expr *e)
4700 {
4701 gfc_ref *char_ref;
4702 gfc_expr *start, *end;
4703
4704 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4705 if (char_ref->type == REF_SUBSTRING)
4706 break;
4707
4708 if (!char_ref)
4709 return;
4710
4711 gcc_assert (char_ref->next == NULL);
4712
4713 if (e->ts.u.cl)
4714 {
4715 if (e->ts.u.cl->length)
4716 gfc_free_expr (e->ts.u.cl->length);
4717 else if (e->expr_type == EXPR_VARIABLE
4718 && e->symtree->n.sym->attr.dummy)
4719 return;
4720 }
4721
4722 e->ts.type = BT_CHARACTER;
4723 e->ts.kind = gfc_default_character_kind;
4724
4725 if (!e->ts.u.cl)
4726 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4727
4728 if (char_ref->u.ss.start)
4729 start = gfc_copy_expr (char_ref->u.ss.start);
4730 else
4731 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4732
4733 if (char_ref->u.ss.end)
4734 end = gfc_copy_expr (char_ref->u.ss.end);
4735 else if (e->expr_type == EXPR_VARIABLE)
4736 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4737 else
4738 end = NULL;
4739
4740 if (!start || !end)
4741 return;
4742
4743 /* Length = (end - start +1). */
4744 e->ts.u.cl->length = gfc_subtract (end, start);
4745 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4746 gfc_get_int_expr (gfc_default_integer_kind,
4747 NULL, 1));
4748
4749 e->ts.u.cl->length->ts.type = BT_INTEGER;
4750 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4751
4752 /* Make sure that the length is simplified. */
4753 gfc_simplify_expr (e->ts.u.cl->length, 1);
4754 gfc_resolve_expr (e->ts.u.cl->length);
4755 }
4756
4757
4758 /* Resolve subtype references. */
4759
4760 static gfc_try
4761 resolve_ref (gfc_expr *expr)
4762 {
4763 int current_part_dimension, n_components, seen_part_dimension;
4764 gfc_ref *ref;
4765
4766 for (ref = expr->ref; ref; ref = ref->next)
4767 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4768 {
4769 find_array_spec (expr);
4770 break;
4771 }
4772
4773 for (ref = expr->ref; ref; ref = ref->next)
4774 switch (ref->type)
4775 {
4776 case REF_ARRAY:
4777 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4778 return FAILURE;
4779 break;
4780
4781 case REF_COMPONENT:
4782 break;
4783
4784 case REF_SUBSTRING:
4785 resolve_substring (ref);
4786 break;
4787 }
4788
4789 /* Check constraints on part references. */
4790
4791 current_part_dimension = 0;
4792 seen_part_dimension = 0;
4793 n_components = 0;
4794
4795 for (ref = expr->ref; ref; ref = ref->next)
4796 {
4797 switch (ref->type)
4798 {
4799 case REF_ARRAY:
4800 switch (ref->u.ar.type)
4801 {
4802 case AR_FULL:
4803 /* Coarray scalar. */
4804 if (ref->u.ar.as->rank == 0)
4805 {
4806 current_part_dimension = 0;
4807 break;
4808 }
4809 /* Fall through. */
4810 case AR_SECTION:
4811 current_part_dimension = 1;
4812 break;
4813
4814 case AR_ELEMENT:
4815 current_part_dimension = 0;
4816 break;
4817
4818 case AR_UNKNOWN:
4819 gfc_internal_error ("resolve_ref(): Bad array reference");
4820 }
4821
4822 break;
4823
4824 case REF_COMPONENT:
4825 if (current_part_dimension || seen_part_dimension)
4826 {
4827 /* F03:C614. */
4828 if (ref->u.c.component->attr.pointer
4829 || ref->u.c.component->attr.proc_pointer)
4830 {
4831 gfc_error ("Component to the right of a part reference "
4832 "with nonzero rank must not have the POINTER "
4833 "attribute at %L", &expr->where);
4834 return FAILURE;
4835 }
4836 else if (ref->u.c.component->attr.allocatable)
4837 {
4838 gfc_error ("Component to the right of a part reference "
4839 "with nonzero rank must not have the ALLOCATABLE "
4840 "attribute at %L", &expr->where);
4841 return FAILURE;
4842 }
4843 }
4844
4845 n_components++;
4846 break;
4847
4848 case REF_SUBSTRING:
4849 break;
4850 }
4851
4852 if (((ref->type == REF_COMPONENT && n_components > 1)
4853 || ref->next == NULL)
4854 && current_part_dimension
4855 && seen_part_dimension)
4856 {
4857 gfc_error ("Two or more part references with nonzero rank must "
4858 "not be specified at %L", &expr->where);
4859 return FAILURE;
4860 }
4861
4862 if (ref->type == REF_COMPONENT)
4863 {
4864 if (current_part_dimension)
4865 seen_part_dimension = 1;
4866
4867 /* reset to make sure */
4868 current_part_dimension = 0;
4869 }
4870 }
4871
4872 return SUCCESS;
4873 }
4874
4875
4876 /* Given an expression, determine its shape. This is easier than it sounds.
4877 Leaves the shape array NULL if it is not possible to determine the shape. */
4878
4879 static void
4880 expression_shape (gfc_expr *e)
4881 {
4882 mpz_t array[GFC_MAX_DIMENSIONS];
4883 int i;
4884
4885 if (e->rank == 0 || e->shape != NULL)
4886 return;
4887
4888 for (i = 0; i < e->rank; i++)
4889 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4890 goto fail;
4891
4892 e->shape = gfc_get_shape (e->rank);
4893
4894 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4895
4896 return;
4897
4898 fail:
4899 for (i--; i >= 0; i--)
4900 mpz_clear (array[i]);
4901 }
4902
4903
4904 /* Given a variable expression node, compute the rank of the expression by
4905 examining the base symbol and any reference structures it may have. */
4906
4907 static void
4908 expression_rank (gfc_expr *e)
4909 {
4910 gfc_ref *ref;
4911 int i, rank;
4912
4913 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4914 could lead to serious confusion... */
4915 gcc_assert (e->expr_type != EXPR_COMPCALL);
4916
4917 if (e->ref == NULL)
4918 {
4919 if (e->expr_type == EXPR_ARRAY)
4920 goto done;
4921 /* Constructors can have a rank different from one via RESHAPE(). */
4922
4923 if (e->symtree == NULL)
4924 {
4925 e->rank = 0;
4926 goto done;
4927 }
4928
4929 e->rank = (e->symtree->n.sym->as == NULL)
4930 ? 0 : e->symtree->n.sym->as->rank;
4931 goto done;
4932 }
4933
4934 rank = 0;
4935
4936 for (ref = e->ref; ref; ref = ref->next)
4937 {
4938 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4939 && ref->u.c.component->attr.function && !ref->next)
4940 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4941
4942 if (ref->type != REF_ARRAY)
4943 continue;
4944
4945 if (ref->u.ar.type == AR_FULL)
4946 {
4947 rank = ref->u.ar.as->rank;
4948 break;
4949 }
4950
4951 if (ref->u.ar.type == AR_SECTION)
4952 {
4953 /* Figure out the rank of the section. */
4954 if (rank != 0)
4955 gfc_internal_error ("expression_rank(): Two array specs");
4956
4957 for (i = 0; i < ref->u.ar.dimen; i++)
4958 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4959 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4960 rank++;
4961
4962 break;
4963 }
4964 }
4965
4966 e->rank = rank;
4967
4968 done:
4969 expression_shape (e);
4970 }
4971
4972
4973 /* Resolve a variable expression. */
4974
4975 static gfc_try
4976 resolve_variable (gfc_expr *e)
4977 {
4978 gfc_symbol *sym;
4979 gfc_try t;
4980
4981 t = SUCCESS;
4982
4983 if (e->symtree == NULL)
4984 return FAILURE;
4985 sym = e->symtree->n.sym;
4986
4987 /* If this is an associate-name, it may be parsed with an array reference
4988 in error even though the target is scalar. Fail directly in this case. */
4989 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4990 return FAILURE;
4991
4992 /* On the other hand, the parser may not have known this is an array;
4993 in this case, we have to add a FULL reference. */
4994 if (sym->assoc && sym->attr.dimension && !e->ref)
4995 {
4996 e->ref = gfc_get_ref ();
4997 e->ref->type = REF_ARRAY;
4998 e->ref->u.ar.type = AR_FULL;
4999 e->ref->u.ar.dimen = 0;
5000 }
5001
5002 if (e->ref && resolve_ref (e) == FAILURE)
5003 return FAILURE;
5004
5005 if (sym->attr.flavor == FL_PROCEDURE
5006 && (!sym->attr.function
5007 || (sym->attr.function && sym->result
5008 && sym->result->attr.proc_pointer
5009 && !sym->result->attr.function)))
5010 {
5011 e->ts.type = BT_PROCEDURE;
5012 goto resolve_procedure;
5013 }
5014
5015 if (sym->ts.type != BT_UNKNOWN)
5016 gfc_variable_attr (e, &e->ts);
5017 else
5018 {
5019 /* Must be a simple variable reference. */
5020 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
5021 return FAILURE;
5022 e->ts = sym->ts;
5023 }
5024
5025 if (check_assumed_size_reference (sym, e))
5026 return FAILURE;
5027
5028 /* Deal with forward references to entries during resolve_code, to
5029 satisfy, at least partially, 12.5.2.5. */
5030 if (gfc_current_ns->entries
5031 && current_entry_id == sym->entry_id
5032 && cs_base
5033 && cs_base->current
5034 && cs_base->current->op != EXEC_ENTRY)
5035 {
5036 gfc_entry_list *entry;
5037 gfc_formal_arglist *formal;
5038 int n;
5039 bool seen;
5040
5041 /* If the symbol is a dummy... */
5042 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5043 {
5044 entry = gfc_current_ns->entries;
5045 seen = false;
5046
5047 /* ...test if the symbol is a parameter of previous entries. */
5048 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5049 for (formal = entry->sym->formal; formal; formal = formal->next)
5050 {
5051 if (formal->sym && sym->name == formal->sym->name)
5052 seen = true;
5053 }
5054
5055 /* If it has not been seen as a dummy, this is an error. */
5056 if (!seen)
5057 {
5058 if (specification_expr)
5059 gfc_error ("Variable '%s', used in a specification expression"
5060 ", is referenced at %L before the ENTRY statement "
5061 "in which it is a parameter",
5062 sym->name, &cs_base->current->loc);
5063 else
5064 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5065 "statement in which it is a parameter",
5066 sym->name, &cs_base->current->loc);
5067 t = FAILURE;
5068 }
5069 }
5070
5071 /* Now do the same check on the specification expressions. */
5072 specification_expr = 1;
5073 if (sym->ts.type == BT_CHARACTER
5074 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5075 t = FAILURE;
5076
5077 if (sym->as)
5078 for (n = 0; n < sym->as->rank; n++)
5079 {
5080 specification_expr = 1;
5081 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5082 t = FAILURE;
5083 specification_expr = 1;
5084 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5085 t = FAILURE;
5086 }
5087 specification_expr = 0;
5088
5089 if (t == SUCCESS)
5090 /* Update the symbol's entry level. */
5091 sym->entry_id = current_entry_id + 1;
5092 }
5093
5094 /* If a symbol has been host_associated mark it. This is used latter,
5095 to identify if aliasing is possible via host association. */
5096 if (sym->attr.flavor == FL_VARIABLE
5097 && gfc_current_ns->parent
5098 && (gfc_current_ns->parent == sym->ns
5099 || (gfc_current_ns->parent->parent
5100 && gfc_current_ns->parent->parent == sym->ns)))
5101 sym->attr.host_assoc = 1;
5102
5103 resolve_procedure:
5104 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5105 t = FAILURE;
5106
5107 /* F2008, C617 and C1229. */
5108 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5109 && gfc_is_coindexed (e))
5110 {
5111 gfc_ref *ref, *ref2 = NULL;
5112
5113 for (ref = e->ref; ref; ref = ref->next)
5114 {
5115 if (ref->type == REF_COMPONENT)
5116 ref2 = ref;
5117 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5118 break;
5119 }
5120
5121 for ( ; ref; ref = ref->next)
5122 if (ref->type == REF_COMPONENT)
5123 break;
5124
5125 /* Expression itself is not coindexed object. */
5126 if (ref && e->ts.type == BT_CLASS)
5127 {
5128 gfc_error ("Polymorphic subobject of coindexed object at %L",
5129 &e->where);
5130 t = FAILURE;
5131 }
5132
5133 /* Expression itself is coindexed object. */
5134 if (ref == NULL)
5135 {
5136 gfc_component *c;
5137 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5138 for ( ; c; c = c->next)
5139 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5140 {
5141 gfc_error ("Coindexed object with polymorphic allocatable "
5142 "subcomponent at %L", &e->where);
5143 t = FAILURE;
5144 break;
5145 }
5146 }
5147 }
5148
5149 return t;
5150 }
5151
5152
5153 /* Checks to see that the correct symbol has been host associated.
5154 The only situation where this arises is that in which a twice
5155 contained function is parsed after the host association is made.
5156 Therefore, on detecting this, change the symbol in the expression
5157 and convert the array reference into an actual arglist if the old
5158 symbol is a variable. */
5159 static bool
5160 check_host_association (gfc_expr *e)
5161 {
5162 gfc_symbol *sym, *old_sym;
5163 gfc_symtree *st;
5164 int n;
5165 gfc_ref *ref;
5166 gfc_actual_arglist *arg, *tail = NULL;
5167 bool retval = e->expr_type == EXPR_FUNCTION;
5168
5169 /* If the expression is the result of substitution in
5170 interface.c(gfc_extend_expr) because there is no way in
5171 which the host association can be wrong. */
5172 if (e->symtree == NULL
5173 || e->symtree->n.sym == NULL
5174 || e->user_operator)
5175 return retval;
5176
5177 old_sym = e->symtree->n.sym;
5178
5179 if (gfc_current_ns->parent
5180 && old_sym->ns != gfc_current_ns)
5181 {
5182 /* Use the 'USE' name so that renamed module symbols are
5183 correctly handled. */
5184 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5185
5186 if (sym && old_sym != sym
5187 && sym->ts.type == old_sym->ts.type
5188 && sym->attr.flavor == FL_PROCEDURE
5189 && sym->attr.contained)
5190 {
5191 /* Clear the shape, since it might not be valid. */
5192 if (e->shape != NULL)
5193 {
5194 for (n = 0; n < e->rank; n++)
5195 mpz_clear (e->shape[n]);
5196
5197 free (e->shape);
5198 }
5199
5200 /* Give the expression the right symtree! */
5201 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5202 gcc_assert (st != NULL);
5203
5204 if (old_sym->attr.flavor == FL_PROCEDURE
5205 || e->expr_type == EXPR_FUNCTION)
5206 {
5207 /* Original was function so point to the new symbol, since
5208 the actual argument list is already attached to the
5209 expression. */
5210 e->value.function.esym = NULL;
5211 e->symtree = st;
5212 }
5213 else
5214 {
5215 /* Original was variable so convert array references into
5216 an actual arglist. This does not need any checking now
5217 since gfc_resolve_function will take care of it. */
5218 e->value.function.actual = NULL;
5219 e->expr_type = EXPR_FUNCTION;
5220 e->symtree = st;
5221
5222 /* Ambiguity will not arise if the array reference is not
5223 the last reference. */
5224 for (ref = e->ref; ref; ref = ref->next)
5225 if (ref->type == REF_ARRAY && ref->next == NULL)
5226 break;
5227
5228 gcc_assert (ref->type == REF_ARRAY);
5229
5230 /* Grab the start expressions from the array ref and
5231 copy them into actual arguments. */
5232 for (n = 0; n < ref->u.ar.dimen; n++)
5233 {
5234 arg = gfc_get_actual_arglist ();
5235 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5236 if (e->value.function.actual == NULL)
5237 tail = e->value.function.actual = arg;
5238 else
5239 {
5240 tail->next = arg;
5241 tail = arg;
5242 }
5243 }
5244
5245 /* Dump the reference list and set the rank. */
5246 gfc_free_ref_list (e->ref);
5247 e->ref = NULL;
5248 e->rank = sym->as ? sym->as->rank : 0;
5249 }
5250
5251 gfc_resolve_expr (e);
5252 sym->refs++;
5253 }
5254 }
5255 /* This might have changed! */
5256 return e->expr_type == EXPR_FUNCTION;
5257 }
5258
5259
5260 static void
5261 gfc_resolve_character_operator (gfc_expr *e)
5262 {
5263 gfc_expr *op1 = e->value.op.op1;
5264 gfc_expr *op2 = e->value.op.op2;
5265 gfc_expr *e1 = NULL;
5266 gfc_expr *e2 = NULL;
5267
5268 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5269
5270 if (op1->ts.u.cl && op1->ts.u.cl->length)
5271 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5272 else if (op1->expr_type == EXPR_CONSTANT)
5273 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5274 op1->value.character.length);
5275
5276 if (op2->ts.u.cl && op2->ts.u.cl->length)
5277 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5278 else if (op2->expr_type == EXPR_CONSTANT)
5279 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5280 op2->value.character.length);
5281
5282 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5283
5284 if (!e1 || !e2)
5285 return;
5286
5287 e->ts.u.cl->length = gfc_add (e1, e2);
5288 e->ts.u.cl->length->ts.type = BT_INTEGER;
5289 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5290 gfc_simplify_expr (e->ts.u.cl->length, 0);
5291 gfc_resolve_expr (e->ts.u.cl->length);
5292
5293 return;
5294 }
5295
5296
5297 /* Ensure that an character expression has a charlen and, if possible, a
5298 length expression. */
5299
5300 static void
5301 fixup_charlen (gfc_expr *e)
5302 {
5303 /* The cases fall through so that changes in expression type and the need
5304 for multiple fixes are picked up. In all circumstances, a charlen should
5305 be available for the middle end to hang a backend_decl on. */
5306 switch (e->expr_type)
5307 {
5308 case EXPR_OP:
5309 gfc_resolve_character_operator (e);
5310
5311 case EXPR_ARRAY:
5312 if (e->expr_type == EXPR_ARRAY)
5313 gfc_resolve_character_array_constructor (e);
5314
5315 case EXPR_SUBSTRING:
5316 if (!e->ts.u.cl && e->ref)
5317 gfc_resolve_substring_charlen (e);
5318
5319 default:
5320 if (!e->ts.u.cl)
5321 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5322
5323 break;
5324 }
5325 }
5326
5327
5328 /* Update an actual argument to include the passed-object for type-bound
5329 procedures at the right position. */
5330
5331 static gfc_actual_arglist*
5332 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5333 const char *name)
5334 {
5335 gcc_assert (argpos > 0);
5336
5337 if (argpos == 1)
5338 {
5339 gfc_actual_arglist* result;
5340
5341 result = gfc_get_actual_arglist ();
5342 result->expr = po;
5343 result->next = lst;
5344 if (name)
5345 result->name = name;
5346
5347 return result;
5348 }
5349
5350 if (lst)
5351 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5352 else
5353 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5354 return lst;
5355 }
5356
5357
5358 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5359
5360 static gfc_expr*
5361 extract_compcall_passed_object (gfc_expr* e)
5362 {
5363 gfc_expr* po;
5364
5365 gcc_assert (e->expr_type == EXPR_COMPCALL);
5366
5367 if (e->value.compcall.base_object)
5368 po = gfc_copy_expr (e->value.compcall.base_object);
5369 else
5370 {
5371 po = gfc_get_expr ();
5372 po->expr_type = EXPR_VARIABLE;
5373 po->symtree = e->symtree;
5374 po->ref = gfc_copy_ref (e->ref);
5375 po->where = e->where;
5376 }
5377
5378 if (gfc_resolve_expr (po) == FAILURE)
5379 return NULL;
5380
5381 return po;
5382 }
5383
5384
5385 /* Update the arglist of an EXPR_COMPCALL expression to include the
5386 passed-object. */
5387
5388 static gfc_try
5389 update_compcall_arglist (gfc_expr* e)
5390 {
5391 gfc_expr* po;
5392 gfc_typebound_proc* tbp;
5393
5394 tbp = e->value.compcall.tbp;
5395
5396 if (tbp->error)
5397 return FAILURE;
5398
5399 po = extract_compcall_passed_object (e);
5400 if (!po)
5401 return FAILURE;
5402
5403 if (tbp->nopass || e->value.compcall.ignore_pass)
5404 {
5405 gfc_free_expr (po);
5406 return SUCCESS;
5407 }
5408
5409 gcc_assert (tbp->pass_arg_num > 0);
5410 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5411 tbp->pass_arg_num,
5412 tbp->pass_arg);
5413
5414 return SUCCESS;
5415 }
5416
5417
5418 /* Extract the passed object from a PPC call (a copy of it). */
5419
5420 static gfc_expr*
5421 extract_ppc_passed_object (gfc_expr *e)
5422 {
5423 gfc_expr *po;
5424 gfc_ref **ref;
5425
5426 po = gfc_get_expr ();
5427 po->expr_type = EXPR_VARIABLE;
5428 po->symtree = e->symtree;
5429 po->ref = gfc_copy_ref (e->ref);
5430 po->where = e->where;
5431
5432 /* Remove PPC reference. */
5433 ref = &po->ref;
5434 while ((*ref)->next)
5435 ref = &(*ref)->next;
5436 gfc_free_ref_list (*ref);
5437 *ref = NULL;
5438
5439 if (gfc_resolve_expr (po) == FAILURE)
5440 return NULL;
5441
5442 return po;
5443 }
5444
5445
5446 /* Update the actual arglist of a procedure pointer component to include the
5447 passed-object. */
5448
5449 static gfc_try
5450 update_ppc_arglist (gfc_expr* e)
5451 {
5452 gfc_expr* po;
5453 gfc_component *ppc;
5454 gfc_typebound_proc* tb;
5455
5456 if (!gfc_is_proc_ptr_comp (e, &ppc))
5457 return FAILURE;
5458
5459 tb = ppc->tb;
5460
5461 if (tb->error)
5462 return FAILURE;
5463 else if (tb->nopass)
5464 return SUCCESS;
5465
5466 po = extract_ppc_passed_object (e);
5467 if (!po)
5468 return FAILURE;
5469
5470 /* F08:R739. */
5471 if (po->rank > 0)
5472 {
5473 gfc_error ("Passed-object at %L must be scalar", &e->where);
5474 return FAILURE;
5475 }
5476
5477 /* F08:C611. */
5478 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5479 {
5480 gfc_error ("Base object for procedure-pointer component call at %L is of"
5481 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5482 return FAILURE;
5483 }
5484
5485 gcc_assert (tb->pass_arg_num > 0);
5486 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5487 tb->pass_arg_num,
5488 tb->pass_arg);
5489
5490 return SUCCESS;
5491 }
5492
5493
5494 /* Check that the object a TBP is called on is valid, i.e. it must not be
5495 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5496
5497 static gfc_try
5498 check_typebound_baseobject (gfc_expr* e)
5499 {
5500 gfc_expr* base;
5501 gfc_try return_value = FAILURE;
5502
5503 base = extract_compcall_passed_object (e);
5504 if (!base)
5505 return FAILURE;
5506
5507 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5508
5509 /* F08:C611. */
5510 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5511 {
5512 gfc_error ("Base object for type-bound procedure call at %L is of"
5513 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5514 goto cleanup;
5515 }
5516
5517 /* F08:C1230. If the procedure called is NOPASS,
5518 the base object must be scalar. */
5519 if (e->value.compcall.tbp->nopass && base->rank > 0)
5520 {
5521 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5522 " be scalar", &e->where);
5523 goto cleanup;
5524 }
5525
5526 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5527 if (base->rank > 0)
5528 {
5529 gfc_error ("Non-scalar base object at %L currently not implemented",
5530 &e->where);
5531 goto cleanup;
5532 }
5533
5534 return_value = SUCCESS;
5535
5536 cleanup:
5537 gfc_free_expr (base);
5538 return return_value;
5539 }
5540
5541
5542 /* Resolve a call to a type-bound procedure, either function or subroutine,
5543 statically from the data in an EXPR_COMPCALL expression. The adapted
5544 arglist and the target-procedure symtree are returned. */
5545
5546 static gfc_try
5547 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5548 gfc_actual_arglist** actual)
5549 {
5550 gcc_assert (e->expr_type == EXPR_COMPCALL);
5551 gcc_assert (!e->value.compcall.tbp->is_generic);
5552
5553 /* Update the actual arglist for PASS. */
5554 if (update_compcall_arglist (e) == FAILURE)
5555 return FAILURE;
5556
5557 *actual = e->value.compcall.actual;
5558 *target = e->value.compcall.tbp->u.specific;
5559
5560 gfc_free_ref_list (e->ref);
5561 e->ref = NULL;
5562 e->value.compcall.actual = NULL;
5563
5564 return SUCCESS;
5565 }
5566
5567
5568 /* Get the ultimate declared type from an expression. In addition,
5569 return the last class/derived type reference and the copy of the
5570 reference list. */
5571 static gfc_symbol*
5572 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5573 gfc_expr *e)
5574 {
5575 gfc_symbol *declared;
5576 gfc_ref *ref;
5577
5578 declared = NULL;
5579 if (class_ref)
5580 *class_ref = NULL;
5581 if (new_ref)
5582 *new_ref = gfc_copy_ref (e->ref);
5583
5584 for (ref = e->ref; ref; ref = ref->next)
5585 {
5586 if (ref->type != REF_COMPONENT)
5587 continue;
5588
5589 if (ref->u.c.component->ts.type == BT_CLASS
5590 || ref->u.c.component->ts.type == BT_DERIVED)
5591 {
5592 declared = ref->u.c.component->ts.u.derived;
5593 if (class_ref)
5594 *class_ref = ref;
5595 }
5596 }
5597
5598 if (declared == NULL)
5599 declared = e->symtree->n.sym->ts.u.derived;
5600
5601 return declared;
5602 }
5603
5604
5605 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5606 which of the specific bindings (if any) matches the arglist and transform
5607 the expression into a call of that binding. */
5608
5609 static gfc_try
5610 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5611 {
5612 gfc_typebound_proc* genproc;
5613 const char* genname;
5614 gfc_symtree *st;
5615 gfc_symbol *derived;
5616
5617 gcc_assert (e->expr_type == EXPR_COMPCALL);
5618 genname = e->value.compcall.name;
5619 genproc = e->value.compcall.tbp;
5620
5621 if (!genproc->is_generic)
5622 return SUCCESS;
5623
5624 /* Try the bindings on this type and in the inheritance hierarchy. */
5625 for (; genproc; genproc = genproc->overridden)
5626 {
5627 gfc_tbp_generic* g;
5628
5629 gcc_assert (genproc->is_generic);
5630 for (g = genproc->u.generic; g; g = g->next)
5631 {
5632 gfc_symbol* target;
5633 gfc_actual_arglist* args;
5634 bool matches;
5635
5636 gcc_assert (g->specific);
5637
5638 if (g->specific->error)
5639 continue;
5640
5641 target = g->specific->u.specific->n.sym;
5642
5643 /* Get the right arglist by handling PASS/NOPASS. */
5644 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5645 if (!g->specific->nopass)
5646 {
5647 gfc_expr* po;
5648 po = extract_compcall_passed_object (e);
5649 if (!po)
5650 return FAILURE;
5651
5652 gcc_assert (g->specific->pass_arg_num > 0);
5653 gcc_assert (!g->specific->error);
5654 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5655 g->specific->pass_arg);
5656 }
5657 resolve_actual_arglist (args, target->attr.proc,
5658 is_external_proc (target) && !target->formal);
5659
5660 /* Check if this arglist matches the formal. */
5661 matches = gfc_arglist_matches_symbol (&args, target);
5662
5663 /* Clean up and break out of the loop if we've found it. */
5664 gfc_free_actual_arglist (args);
5665 if (matches)
5666 {
5667 e->value.compcall.tbp = g->specific;
5668 genname = g->specific_st->name;
5669 /* Pass along the name for CLASS methods, where the vtab
5670 procedure pointer component has to be referenced. */
5671 if (name)
5672 *name = genname;
5673 goto success;
5674 }
5675 }
5676 }
5677
5678 /* Nothing matching found! */
5679 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5680 " '%s' at %L", genname, &e->where);
5681 return FAILURE;
5682
5683 success:
5684 /* Make sure that we have the right specific instance for the name. */
5685 derived = get_declared_from_expr (NULL, NULL, e);
5686
5687 st = gfc_find_typebound_proc (derived, NULL, genname, false, &e->where);
5688 if (st)
5689 e->value.compcall.tbp = st->n.tb;
5690
5691 return SUCCESS;
5692 }
5693
5694
5695 /* Resolve a call to a type-bound subroutine. */
5696
5697 static gfc_try
5698 resolve_typebound_call (gfc_code* c, const char **name)
5699 {
5700 gfc_actual_arglist* newactual;
5701 gfc_symtree* target;
5702
5703 /* Check that's really a SUBROUTINE. */
5704 if (!c->expr1->value.compcall.tbp->subroutine)
5705 {
5706 gfc_error ("'%s' at %L should be a SUBROUTINE",
5707 c->expr1->value.compcall.name, &c->loc);
5708 return FAILURE;
5709 }
5710
5711 if (check_typebound_baseobject (c->expr1) == FAILURE)
5712 return FAILURE;
5713
5714 /* Pass along the name for CLASS methods, where the vtab
5715 procedure pointer component has to be referenced. */
5716 if (name)
5717 *name = c->expr1->value.compcall.name;
5718
5719 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5720 return FAILURE;
5721
5722 /* Transform into an ordinary EXEC_CALL for now. */
5723
5724 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5725 return FAILURE;
5726
5727 c->ext.actual = newactual;
5728 c->symtree = target;
5729 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5730
5731 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5732
5733 gfc_free_expr (c->expr1);
5734 c->expr1 = gfc_get_expr ();
5735 c->expr1->expr_type = EXPR_FUNCTION;
5736 c->expr1->symtree = target;
5737 c->expr1->where = c->loc;
5738
5739 return resolve_call (c);
5740 }
5741
5742
5743 /* Resolve a component-call expression. */
5744 static gfc_try
5745 resolve_compcall (gfc_expr* e, const char **name)
5746 {
5747 gfc_actual_arglist* newactual;
5748 gfc_symtree* target;
5749
5750 /* Check that's really a FUNCTION. */
5751 if (!e->value.compcall.tbp->function)
5752 {
5753 gfc_error ("'%s' at %L should be a FUNCTION",
5754 e->value.compcall.name, &e->where);
5755 return FAILURE;
5756 }
5757
5758 /* These must not be assign-calls! */
5759 gcc_assert (!e->value.compcall.assign);
5760
5761 if (check_typebound_baseobject (e) == FAILURE)
5762 return FAILURE;
5763
5764 /* Pass along the name for CLASS methods, where the vtab
5765 procedure pointer component has to be referenced. */
5766 if (name)
5767 *name = e->value.compcall.name;
5768
5769 if (resolve_typebound_generic_call (e, name) == FAILURE)
5770 return FAILURE;
5771 gcc_assert (!e->value.compcall.tbp->is_generic);
5772
5773 /* Take the rank from the function's symbol. */
5774 if (e->value.compcall.tbp->u.specific->n.sym->as)
5775 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5776
5777 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5778 arglist to the TBP's binding target. */
5779
5780 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5781 return FAILURE;
5782
5783 e->value.function.actual = newactual;
5784 e->value.function.name = NULL;
5785 e->value.function.esym = target->n.sym;
5786 e->value.function.isym = NULL;
5787 e->symtree = target;
5788 e->ts = target->n.sym->ts;
5789 e->expr_type = EXPR_FUNCTION;
5790
5791 /* Resolution is not necessary if this is a class subroutine; this
5792 function only has to identify the specific proc. Resolution of
5793 the call will be done next in resolve_typebound_call. */
5794 return gfc_resolve_expr (e);
5795 }
5796
5797
5798
5799 /* Resolve a typebound function, or 'method'. First separate all
5800 the non-CLASS references by calling resolve_compcall directly. */
5801
5802 static gfc_try
5803 resolve_typebound_function (gfc_expr* e)
5804 {
5805 gfc_symbol *declared;
5806 gfc_component *c;
5807 gfc_ref *new_ref;
5808 gfc_ref *class_ref;
5809 gfc_symtree *st;
5810 const char *name;
5811 gfc_typespec ts;
5812 gfc_expr *expr;
5813
5814 st = e->symtree;
5815
5816 /* Deal with typebound operators for CLASS objects. */
5817 expr = e->value.compcall.base_object;
5818 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5819 {
5820 /* Since the typebound operators are generic, we have to ensure
5821 that any delays in resolution are corrected and that the vtab
5822 is present. */
5823 ts = expr->ts;
5824 declared = ts.u.derived;
5825 c = gfc_find_component (declared, "_vptr", true, true);
5826 if (c->ts.u.derived == NULL)
5827 c->ts.u.derived = gfc_find_derived_vtab (declared);
5828
5829 if (resolve_compcall (e, &name) == FAILURE)
5830 return FAILURE;
5831
5832 /* Use the generic name if it is there. */
5833 name = name ? name : e->value.function.esym->name;
5834 e->symtree = expr->symtree;
5835 e->ref = gfc_copy_ref (expr->ref);
5836 gfc_add_vptr_component (e);
5837 gfc_add_component_ref (e, name);
5838 e->value.function.esym = NULL;
5839 return SUCCESS;
5840 }
5841
5842 if (st == NULL)
5843 return resolve_compcall (e, NULL);
5844
5845 if (resolve_ref (e) == FAILURE)
5846 return FAILURE;
5847
5848 /* Get the CLASS declared type. */
5849 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5850
5851 /* Weed out cases of the ultimate component being a derived type. */
5852 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5853 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5854 {
5855 gfc_free_ref_list (new_ref);
5856 return resolve_compcall (e, NULL);
5857 }
5858
5859 c = gfc_find_component (declared, "_data", true, true);
5860 declared = c->ts.u.derived;
5861
5862 /* Treat the call as if it is a typebound procedure, in order to roll
5863 out the correct name for the specific function. */
5864 if (resolve_compcall (e, &name) == FAILURE)
5865 return FAILURE;
5866 ts = e->ts;
5867
5868 /* Then convert the expression to a procedure pointer component call. */
5869 e->value.function.esym = NULL;
5870 e->symtree = st;
5871
5872 if (new_ref)
5873 e->ref = new_ref;
5874
5875 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5876 gfc_add_vptr_component (e);
5877 gfc_add_component_ref (e, name);
5878
5879 /* Recover the typespec for the expression. This is really only
5880 necessary for generic procedures, where the additional call
5881 to gfc_add_component_ref seems to throw the collection of the
5882 correct typespec. */
5883 e->ts = ts;
5884 return SUCCESS;
5885 }
5886
5887 /* Resolve a typebound subroutine, or 'method'. First separate all
5888 the non-CLASS references by calling resolve_typebound_call
5889 directly. */
5890
5891 static gfc_try
5892 resolve_typebound_subroutine (gfc_code *code)
5893 {
5894 gfc_symbol *declared;
5895 gfc_component *c;
5896 gfc_ref *new_ref;
5897 gfc_ref *class_ref;
5898 gfc_symtree *st;
5899 const char *name;
5900 gfc_typespec ts;
5901 gfc_expr *expr;
5902
5903 st = code->expr1->symtree;
5904
5905 /* Deal with typebound operators for CLASS objects. */
5906 expr = code->expr1->value.compcall.base_object;
5907 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5908 {
5909 /* Since the typebound operators are generic, we have to ensure
5910 that any delays in resolution are corrected and that the vtab
5911 is present. */
5912 declared = expr->ts.u.derived;
5913 c = gfc_find_component (declared, "_vptr", true, true);
5914 if (c->ts.u.derived == NULL)
5915 c->ts.u.derived = gfc_find_derived_vtab (declared);
5916
5917 if (resolve_typebound_call (code, &name) == FAILURE)
5918 return FAILURE;
5919
5920 /* Use the generic name if it is there. */
5921 name = name ? name : code->expr1->value.function.esym->name;
5922 code->expr1->symtree = expr->symtree;
5923 code->expr1->ref = gfc_copy_ref (expr->ref);
5924 gfc_add_vptr_component (code->expr1);
5925 gfc_add_component_ref (code->expr1, name);
5926 code->expr1->value.function.esym = NULL;
5927 return SUCCESS;
5928 }
5929
5930 if (st == NULL)
5931 return resolve_typebound_call (code, NULL);
5932
5933 if (resolve_ref (code->expr1) == FAILURE)
5934 return FAILURE;
5935
5936 /* Get the CLASS declared type. */
5937 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5938
5939 /* Weed out cases of the ultimate component being a derived type. */
5940 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5941 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5942 {
5943 gfc_free_ref_list (new_ref);
5944 return resolve_typebound_call (code, NULL);
5945 }
5946
5947 if (resolve_typebound_call (code, &name) == FAILURE)
5948 return FAILURE;
5949 ts = code->expr1->ts;
5950
5951 /* Then convert the expression to a procedure pointer component call. */
5952 code->expr1->value.function.esym = NULL;
5953 code->expr1->symtree = st;
5954
5955 if (new_ref)
5956 code->expr1->ref = new_ref;
5957
5958 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5959 gfc_add_vptr_component (code->expr1);
5960 gfc_add_component_ref (code->expr1, name);
5961
5962 /* Recover the typespec for the expression. This is really only
5963 necessary for generic procedures, where the additional call
5964 to gfc_add_component_ref seems to throw the collection of the
5965 correct typespec. */
5966 code->expr1->ts = ts;
5967 return SUCCESS;
5968 }
5969
5970
5971 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5972
5973 static gfc_try
5974 resolve_ppc_call (gfc_code* c)
5975 {
5976 gfc_component *comp;
5977 bool b;
5978
5979 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5980 gcc_assert (b);
5981
5982 c->resolved_sym = c->expr1->symtree->n.sym;
5983 c->expr1->expr_type = EXPR_VARIABLE;
5984
5985 if (!comp->attr.subroutine)
5986 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5987
5988 if (resolve_ref (c->expr1) == FAILURE)
5989 return FAILURE;
5990
5991 if (update_ppc_arglist (c->expr1) == FAILURE)
5992 return FAILURE;
5993
5994 c->ext.actual = c->expr1->value.compcall.actual;
5995
5996 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5997 comp->formal == NULL) == FAILURE)
5998 return FAILURE;
5999
6000 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6001
6002 return SUCCESS;
6003 }
6004
6005
6006 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6007
6008 static gfc_try
6009 resolve_expr_ppc (gfc_expr* e)
6010 {
6011 gfc_component *comp;
6012 bool b;
6013
6014 b = gfc_is_proc_ptr_comp (e, &comp);
6015 gcc_assert (b);
6016
6017 /* Convert to EXPR_FUNCTION. */
6018 e->expr_type = EXPR_FUNCTION;
6019 e->value.function.isym = NULL;
6020 e->value.function.actual = e->value.compcall.actual;
6021 e->ts = comp->ts;
6022 if (comp->as != NULL)
6023 e->rank = comp->as->rank;
6024
6025 if (!comp->attr.function)
6026 gfc_add_function (&comp->attr, comp->name, &e->where);
6027
6028 if (resolve_ref (e) == FAILURE)
6029 return FAILURE;
6030
6031 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6032 comp->formal == NULL) == FAILURE)
6033 return FAILURE;
6034
6035 if (update_ppc_arglist (e) == FAILURE)
6036 return FAILURE;
6037
6038 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6039
6040 return SUCCESS;
6041 }
6042
6043
6044 static bool
6045 gfc_is_expandable_expr (gfc_expr *e)
6046 {
6047 gfc_constructor *con;
6048
6049 if (e->expr_type == EXPR_ARRAY)
6050 {
6051 /* Traverse the constructor looking for variables that are flavor
6052 parameter. Parameters must be expanded since they are fully used at
6053 compile time. */
6054 con = gfc_constructor_first (e->value.constructor);
6055 for (; con; con = gfc_constructor_next (con))
6056 {
6057 if (con->expr->expr_type == EXPR_VARIABLE
6058 && con->expr->symtree
6059 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6060 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6061 return true;
6062 if (con->expr->expr_type == EXPR_ARRAY
6063 && gfc_is_expandable_expr (con->expr))
6064 return true;
6065 }
6066 }
6067
6068 return false;
6069 }
6070
6071 /* Resolve an expression. That is, make sure that types of operands agree
6072 with their operators, intrinsic operators are converted to function calls
6073 for overloaded types and unresolved function references are resolved. */
6074
6075 gfc_try
6076 gfc_resolve_expr (gfc_expr *e)
6077 {
6078 gfc_try t;
6079 bool inquiry_save;
6080
6081 if (e == NULL)
6082 return SUCCESS;
6083
6084 /* inquiry_argument only applies to variables. */
6085 inquiry_save = inquiry_argument;
6086 if (e->expr_type != EXPR_VARIABLE)
6087 inquiry_argument = false;
6088
6089 switch (e->expr_type)
6090 {
6091 case EXPR_OP:
6092 t = resolve_operator (e);
6093 break;
6094
6095 case EXPR_FUNCTION:
6096 case EXPR_VARIABLE:
6097
6098 if (check_host_association (e))
6099 t = resolve_function (e);
6100 else
6101 {
6102 t = resolve_variable (e);
6103 if (t == SUCCESS)
6104 expression_rank (e);
6105 }
6106
6107 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6108 && e->ref->type != REF_SUBSTRING)
6109 gfc_resolve_substring_charlen (e);
6110
6111 break;
6112
6113 case EXPR_COMPCALL:
6114 t = resolve_typebound_function (e);
6115 break;
6116
6117 case EXPR_SUBSTRING:
6118 t = resolve_ref (e);
6119 break;
6120
6121 case EXPR_CONSTANT:
6122 case EXPR_NULL:
6123 t = SUCCESS;
6124 break;
6125
6126 case EXPR_PPC:
6127 t = resolve_expr_ppc (e);
6128 break;
6129
6130 case EXPR_ARRAY:
6131 t = FAILURE;
6132 if (resolve_ref (e) == FAILURE)
6133 break;
6134
6135 t = gfc_resolve_array_constructor (e);
6136 /* Also try to expand a constructor. */
6137 if (t == SUCCESS)
6138 {
6139 expression_rank (e);
6140 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6141 gfc_expand_constructor (e, false);
6142 }
6143
6144 /* This provides the opportunity for the length of constructors with
6145 character valued function elements to propagate the string length
6146 to the expression. */
6147 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6148 {
6149 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6150 here rather then add a duplicate test for it above. */
6151 gfc_expand_constructor (e, false);
6152 t = gfc_resolve_character_array_constructor (e);
6153 }
6154
6155 break;
6156
6157 case EXPR_STRUCTURE:
6158 t = resolve_ref (e);
6159 if (t == FAILURE)
6160 break;
6161
6162 t = resolve_structure_cons (e, 0);
6163 if (t == FAILURE)
6164 break;
6165
6166 t = gfc_simplify_expr (e, 0);
6167 break;
6168
6169 default:
6170 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6171 }
6172
6173 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6174 fixup_charlen (e);
6175
6176 inquiry_argument = inquiry_save;
6177
6178 return t;
6179 }
6180
6181
6182 /* Resolve an expression from an iterator. They must be scalar and have
6183 INTEGER or (optionally) REAL type. */
6184
6185 static gfc_try
6186 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6187 const char *name_msgid)
6188 {
6189 if (gfc_resolve_expr (expr) == FAILURE)
6190 return FAILURE;
6191
6192 if (expr->rank != 0)
6193 {
6194 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6195 return FAILURE;
6196 }
6197
6198 if (expr->ts.type != BT_INTEGER)
6199 {
6200 if (expr->ts.type == BT_REAL)
6201 {
6202 if (real_ok)
6203 return gfc_notify_std (GFC_STD_F95_DEL,
6204 "Deleted feature: %s at %L must be integer",
6205 _(name_msgid), &expr->where);
6206 else
6207 {
6208 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6209 &expr->where);
6210 return FAILURE;
6211 }
6212 }
6213 else
6214 {
6215 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6216 return FAILURE;
6217 }
6218 }
6219 return SUCCESS;
6220 }
6221
6222
6223 /* Resolve the expressions in an iterator structure. If REAL_OK is
6224 false allow only INTEGER type iterators, otherwise allow REAL types. */
6225
6226 gfc_try
6227 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6228 {
6229 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6230 == FAILURE)
6231 return FAILURE;
6232
6233 if (gfc_check_vardef_context (iter->var, false, _("iterator variable"))
6234 == FAILURE)
6235 return FAILURE;
6236
6237 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6238 "Start expression in DO loop") == FAILURE)
6239 return FAILURE;
6240
6241 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6242 "End expression in DO loop") == FAILURE)
6243 return FAILURE;
6244
6245 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6246 "Step expression in DO loop") == FAILURE)
6247 return FAILURE;
6248
6249 if (iter->step->expr_type == EXPR_CONSTANT)
6250 {
6251 if ((iter->step->ts.type == BT_INTEGER
6252 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6253 || (iter->step->ts.type == BT_REAL
6254 && mpfr_sgn (iter->step->value.real) == 0))
6255 {
6256 gfc_error ("Step expression in DO loop at %L cannot be zero",
6257 &iter->step->where);
6258 return FAILURE;
6259 }
6260 }
6261
6262 /* Convert start, end, and step to the same type as var. */
6263 if (iter->start->ts.kind != iter->var->ts.kind
6264 || iter->start->ts.type != iter->var->ts.type)
6265 gfc_convert_type (iter->start, &iter->var->ts, 2);
6266
6267 if (iter->end->ts.kind != iter->var->ts.kind
6268 || iter->end->ts.type != iter->var->ts.type)
6269 gfc_convert_type (iter->end, &iter->var->ts, 2);
6270
6271 if (iter->step->ts.kind != iter->var->ts.kind
6272 || iter->step->ts.type != iter->var->ts.type)
6273 gfc_convert_type (iter->step, &iter->var->ts, 2);
6274
6275 if (iter->start->expr_type == EXPR_CONSTANT
6276 && iter->end->expr_type == EXPR_CONSTANT
6277 && iter->step->expr_type == EXPR_CONSTANT)
6278 {
6279 int sgn, cmp;
6280 if (iter->start->ts.type == BT_INTEGER)
6281 {
6282 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6283 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6284 }
6285 else
6286 {
6287 sgn = mpfr_sgn (iter->step->value.real);
6288 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6289 }
6290 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6291 gfc_warning ("DO loop at %L will be executed zero times",
6292 &iter->step->where);
6293 }
6294
6295 return SUCCESS;
6296 }
6297
6298
6299 /* Traversal function for find_forall_index. f == 2 signals that
6300 that variable itself is not to be checked - only the references. */
6301
6302 static bool
6303 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6304 {
6305 if (expr->expr_type != EXPR_VARIABLE)
6306 return false;
6307
6308 /* A scalar assignment */
6309 if (!expr->ref || *f == 1)
6310 {
6311 if (expr->symtree->n.sym == sym)
6312 return true;
6313 else
6314 return false;
6315 }
6316
6317 if (*f == 2)
6318 *f = 1;
6319 return false;
6320 }
6321
6322
6323 /* Check whether the FORALL index appears in the expression or not.
6324 Returns SUCCESS if SYM is found in EXPR. */
6325
6326 gfc_try
6327 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6328 {
6329 if (gfc_traverse_expr (expr, sym, forall_index, f))
6330 return SUCCESS;
6331 else
6332 return FAILURE;
6333 }
6334
6335
6336 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6337 to be a scalar INTEGER variable. The subscripts and stride are scalar
6338 INTEGERs, and if stride is a constant it must be nonzero.
6339 Furthermore "A subscript or stride in a forall-triplet-spec shall
6340 not contain a reference to any index-name in the
6341 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6342
6343 static void
6344 resolve_forall_iterators (gfc_forall_iterator *it)
6345 {
6346 gfc_forall_iterator *iter, *iter2;
6347
6348 for (iter = it; iter; iter = iter->next)
6349 {
6350 if (gfc_resolve_expr (iter->var) == SUCCESS
6351 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6352 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6353 &iter->var->where);
6354
6355 if (gfc_resolve_expr (iter->start) == SUCCESS
6356 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6357 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6358 &iter->start->where);
6359 if (iter->var->ts.kind != iter->start->ts.kind)
6360 gfc_convert_type (iter->start, &iter->var->ts, 2);
6361
6362 if (gfc_resolve_expr (iter->end) == SUCCESS
6363 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6364 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6365 &iter->end->where);
6366 if (iter->var->ts.kind != iter->end->ts.kind)
6367 gfc_convert_type (iter->end, &iter->var->ts, 2);
6368
6369 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6370 {
6371 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6372 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6373 &iter->stride->where, "INTEGER");
6374
6375 if (iter->stride->expr_type == EXPR_CONSTANT
6376 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6377 gfc_error ("FORALL stride expression at %L cannot be zero",
6378 &iter->stride->where);
6379 }
6380 if (iter->var->ts.kind != iter->stride->ts.kind)
6381 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6382 }
6383
6384 for (iter = it; iter; iter = iter->next)
6385 for (iter2 = iter; iter2; iter2 = iter2->next)
6386 {
6387 if (find_forall_index (iter2->start,
6388 iter->var->symtree->n.sym, 0) == SUCCESS
6389 || find_forall_index (iter2->end,
6390 iter->var->symtree->n.sym, 0) == SUCCESS
6391 || find_forall_index (iter2->stride,
6392 iter->var->symtree->n.sym, 0) == SUCCESS)
6393 gfc_error ("FORALL index '%s' may not appear in triplet "
6394 "specification at %L", iter->var->symtree->name,
6395 &iter2->start->where);
6396 }
6397 }
6398
6399
6400 /* Given a pointer to a symbol that is a derived type, see if it's
6401 inaccessible, i.e. if it's defined in another module and the components are
6402 PRIVATE. The search is recursive if necessary. Returns zero if no
6403 inaccessible components are found, nonzero otherwise. */
6404
6405 static int
6406 derived_inaccessible (gfc_symbol *sym)
6407 {
6408 gfc_component *c;
6409
6410 if (sym->attr.use_assoc && sym->attr.private_comp)
6411 return 1;
6412
6413 for (c = sym->components; c; c = c->next)
6414 {
6415 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6416 return 1;
6417 }
6418
6419 return 0;
6420 }
6421
6422
6423 /* Resolve the argument of a deallocate expression. The expression must be
6424 a pointer or a full array. */
6425
6426 static gfc_try
6427 resolve_deallocate_expr (gfc_expr *e)
6428 {
6429 symbol_attribute attr;
6430 int allocatable, pointer;
6431 gfc_ref *ref;
6432 gfc_symbol *sym;
6433 gfc_component *c;
6434
6435 if (gfc_resolve_expr (e) == FAILURE)
6436 return FAILURE;
6437
6438 if (e->expr_type != EXPR_VARIABLE)
6439 goto bad;
6440
6441 sym = e->symtree->n.sym;
6442
6443 if (sym->ts.type == BT_CLASS)
6444 {
6445 allocatable = CLASS_DATA (sym)->attr.allocatable;
6446 pointer = CLASS_DATA (sym)->attr.class_pointer;
6447 }
6448 else
6449 {
6450 allocatable = sym->attr.allocatable;
6451 pointer = sym->attr.pointer;
6452 }
6453 for (ref = e->ref; ref; ref = ref->next)
6454 {
6455 switch (ref->type)
6456 {
6457 case REF_ARRAY:
6458 if (ref->u.ar.type != AR_FULL)
6459 allocatable = 0;
6460 break;
6461
6462 case REF_COMPONENT:
6463 c = ref->u.c.component;
6464 if (c->ts.type == BT_CLASS)
6465 {
6466 allocatable = CLASS_DATA (c)->attr.allocatable;
6467 pointer = CLASS_DATA (c)->attr.class_pointer;
6468 }
6469 else
6470 {
6471 allocatable = c->attr.allocatable;
6472 pointer = c->attr.pointer;
6473 }
6474 break;
6475
6476 case REF_SUBSTRING:
6477 allocatable = 0;
6478 break;
6479 }
6480 }
6481
6482 attr = gfc_expr_attr (e);
6483
6484 if (allocatable == 0 && attr.pointer == 0)
6485 {
6486 bad:
6487 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6488 &e->where);
6489 return FAILURE;
6490 }
6491
6492 if (pointer
6493 && gfc_check_vardef_context (e, true, _("DEALLOCATE object")) == FAILURE)
6494 return FAILURE;
6495 if (gfc_check_vardef_context (e, false, _("DEALLOCATE object")) == FAILURE)
6496 return FAILURE;
6497
6498 return SUCCESS;
6499 }
6500
6501
6502 /* Returns true if the expression e contains a reference to the symbol sym. */
6503 static bool
6504 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6505 {
6506 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6507 return true;
6508
6509 return false;
6510 }
6511
6512 bool
6513 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6514 {
6515 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6516 }
6517
6518
6519 /* Given the expression node e for an allocatable/pointer of derived type to be
6520 allocated, get the expression node to be initialized afterwards (needed for
6521 derived types with default initializers, and derived types with allocatable
6522 components that need nullification.) */
6523
6524 gfc_expr *
6525 gfc_expr_to_initialize (gfc_expr *e)
6526 {
6527 gfc_expr *result;
6528 gfc_ref *ref;
6529 int i;
6530
6531 result = gfc_copy_expr (e);
6532
6533 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6534 for (ref = result->ref; ref; ref = ref->next)
6535 if (ref->type == REF_ARRAY && ref->next == NULL)
6536 {
6537 ref->u.ar.type = AR_FULL;
6538
6539 for (i = 0; i < ref->u.ar.dimen; i++)
6540 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6541
6542 result->rank = ref->u.ar.dimen;
6543 break;
6544 }
6545
6546 return result;
6547 }
6548
6549
6550 /* If the last ref of an expression is an array ref, return a copy of the
6551 expression with that one removed. Otherwise, a copy of the original
6552 expression. This is used for allocate-expressions and pointer assignment
6553 LHS, where there may be an array specification that needs to be stripped
6554 off when using gfc_check_vardef_context. */
6555
6556 static gfc_expr*
6557 remove_last_array_ref (gfc_expr* e)
6558 {
6559 gfc_expr* e2;
6560 gfc_ref** r;
6561
6562 e2 = gfc_copy_expr (e);
6563 for (r = &e2->ref; *r; r = &(*r)->next)
6564 if ((*r)->type == REF_ARRAY && !(*r)->next)
6565 {
6566 gfc_free_ref_list (*r);
6567 *r = NULL;
6568 break;
6569 }
6570
6571 return e2;
6572 }
6573
6574
6575 /* Used in resolve_allocate_expr to check that a allocation-object and
6576 a source-expr are conformable. This does not catch all possible
6577 cases; in particular a runtime checking is needed. */
6578
6579 static gfc_try
6580 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6581 {
6582 gfc_ref *tail;
6583 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6584
6585 /* First compare rank. */
6586 if (tail && e1->rank != tail->u.ar.as->rank)
6587 {
6588 gfc_error ("Source-expr at %L must be scalar or have the "
6589 "same rank as the allocate-object at %L",
6590 &e1->where, &e2->where);
6591 return FAILURE;
6592 }
6593
6594 if (e1->shape)
6595 {
6596 int i;
6597 mpz_t s;
6598
6599 mpz_init (s);
6600
6601 for (i = 0; i < e1->rank; i++)
6602 {
6603 if (tail->u.ar.end[i])
6604 {
6605 mpz_set (s, tail->u.ar.end[i]->value.integer);
6606 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6607 mpz_add_ui (s, s, 1);
6608 }
6609 else
6610 {
6611 mpz_set (s, tail->u.ar.start[i]->value.integer);
6612 }
6613
6614 if (mpz_cmp (e1->shape[i], s) != 0)
6615 {
6616 gfc_error ("Source-expr at %L and allocate-object at %L must "
6617 "have the same shape", &e1->where, &e2->where);
6618 mpz_clear (s);
6619 return FAILURE;
6620 }
6621 }
6622
6623 mpz_clear (s);
6624 }
6625
6626 return SUCCESS;
6627 }
6628
6629
6630 /* Resolve the expression in an ALLOCATE statement, doing the additional
6631 checks to see whether the expression is OK or not. The expression must
6632 have a trailing array reference that gives the size of the array. */
6633
6634 static gfc_try
6635 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6636 {
6637 int i, pointer, allocatable, dimension, is_abstract;
6638 int codimension;
6639 bool coindexed;
6640 symbol_attribute attr;
6641 gfc_ref *ref, *ref2;
6642 gfc_expr *e2;
6643 gfc_array_ref *ar;
6644 gfc_symbol *sym = NULL;
6645 gfc_alloc *a;
6646 gfc_component *c;
6647 gfc_try t;
6648
6649 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6650 checking of coarrays. */
6651 for (ref = e->ref; ref; ref = ref->next)
6652 if (ref->next == NULL)
6653 break;
6654
6655 if (ref && ref->type == REF_ARRAY)
6656 ref->u.ar.in_allocate = true;
6657
6658 if (gfc_resolve_expr (e) == FAILURE)
6659 goto failure;
6660
6661 /* Make sure the expression is allocatable or a pointer. If it is
6662 pointer, the next-to-last reference must be a pointer. */
6663
6664 ref2 = NULL;
6665 if (e->symtree)
6666 sym = e->symtree->n.sym;
6667
6668 /* Check whether ultimate component is abstract and CLASS. */
6669 is_abstract = 0;
6670
6671 if (e->expr_type != EXPR_VARIABLE)
6672 {
6673 allocatable = 0;
6674 attr = gfc_expr_attr (e);
6675 pointer = attr.pointer;
6676 dimension = attr.dimension;
6677 codimension = attr.codimension;
6678 }
6679 else
6680 {
6681 if (sym->ts.type == BT_CLASS)
6682 {
6683 allocatable = CLASS_DATA (sym)->attr.allocatable;
6684 pointer = CLASS_DATA (sym)->attr.class_pointer;
6685 dimension = CLASS_DATA (sym)->attr.dimension;
6686 codimension = CLASS_DATA (sym)->attr.codimension;
6687 is_abstract = CLASS_DATA (sym)->attr.abstract;
6688 }
6689 else
6690 {
6691 allocatable = sym->attr.allocatable;
6692 pointer = sym->attr.pointer;
6693 dimension = sym->attr.dimension;
6694 codimension = sym->attr.codimension;
6695 }
6696
6697 coindexed = false;
6698
6699 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6700 {
6701 switch (ref->type)
6702 {
6703 case REF_ARRAY:
6704 if (ref->u.ar.codimen > 0)
6705 {
6706 int n;
6707 for (n = ref->u.ar.dimen;
6708 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6709 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6710 {
6711 coindexed = true;
6712 break;
6713 }
6714 }
6715
6716 if (ref->next != NULL)
6717 pointer = 0;
6718 break;
6719
6720 case REF_COMPONENT:
6721 /* F2008, C644. */
6722 if (coindexed)
6723 {
6724 gfc_error ("Coindexed allocatable object at %L",
6725 &e->where);
6726 goto failure;
6727 }
6728
6729 c = ref->u.c.component;
6730 if (c->ts.type == BT_CLASS)
6731 {
6732 allocatable = CLASS_DATA (c)->attr.allocatable;
6733 pointer = CLASS_DATA (c)->attr.class_pointer;
6734 dimension = CLASS_DATA (c)->attr.dimension;
6735 codimension = CLASS_DATA (c)->attr.codimension;
6736 is_abstract = CLASS_DATA (c)->attr.abstract;
6737 }
6738 else
6739 {
6740 allocatable = c->attr.allocatable;
6741 pointer = c->attr.pointer;
6742 dimension = c->attr.dimension;
6743 codimension = c->attr.codimension;
6744 is_abstract = c->attr.abstract;
6745 }
6746 break;
6747
6748 case REF_SUBSTRING:
6749 allocatable = 0;
6750 pointer = 0;
6751 break;
6752 }
6753 }
6754 }
6755
6756 if (allocatable == 0 && pointer == 0)
6757 {
6758 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6759 &e->where);
6760 goto failure;
6761 }
6762
6763 /* Some checks for the SOURCE tag. */
6764 if (code->expr3)
6765 {
6766 /* Check F03:C631. */
6767 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6768 {
6769 gfc_error ("Type of entity at %L is type incompatible with "
6770 "source-expr at %L", &e->where, &code->expr3->where);
6771 goto failure;
6772 }
6773
6774 /* Check F03:C632 and restriction following Note 6.18. */
6775 if (code->expr3->rank > 0
6776 && conformable_arrays (code->expr3, e) == FAILURE)
6777 goto failure;
6778
6779 /* Check F03:C633. */
6780 if (code->expr3->ts.kind != e->ts.kind)
6781 {
6782 gfc_error ("The allocate-object at %L and the source-expr at %L "
6783 "shall have the same kind type parameter",
6784 &e->where, &code->expr3->where);
6785 goto failure;
6786 }
6787 }
6788
6789 /* Check F08:C629. */
6790 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6791 && !code->expr3)
6792 {
6793 gcc_assert (e->ts.type == BT_CLASS);
6794 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6795 "type-spec or source-expr", sym->name, &e->where);
6796 goto failure;
6797 }
6798
6799 /* In the variable definition context checks, gfc_expr_attr is used
6800 on the expression. This is fooled by the array specification
6801 present in e, thus we have to eliminate that one temporarily. */
6802 e2 = remove_last_array_ref (e);
6803 t = SUCCESS;
6804 if (t == SUCCESS && pointer)
6805 t = gfc_check_vardef_context (e2, true, _("ALLOCATE object"));
6806 if (t == SUCCESS)
6807 t = gfc_check_vardef_context (e2, false, _("ALLOCATE object"));
6808 gfc_free_expr (e2);
6809 if (t == FAILURE)
6810 goto failure;
6811
6812 if (!code->expr3)
6813 {
6814 /* Set up default initializer if needed. */
6815 gfc_typespec ts;
6816 gfc_expr *init_e;
6817
6818 if (code->ext.alloc.ts.type == BT_DERIVED)
6819 ts = code->ext.alloc.ts;
6820 else
6821 ts = e->ts;
6822
6823 if (ts.type == BT_CLASS)
6824 ts = ts.u.derived->components->ts;
6825
6826 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6827 {
6828 gfc_code *init_st = gfc_get_code ();
6829 init_st->loc = code->loc;
6830 init_st->op = EXEC_INIT_ASSIGN;
6831 init_st->expr1 = gfc_expr_to_initialize (e);
6832 init_st->expr2 = init_e;
6833 init_st->next = code->next;
6834 code->next = init_st;
6835 }
6836 }
6837 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6838 {
6839 /* Default initialization via MOLD (non-polymorphic). */
6840 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6841 gfc_resolve_expr (rhs);
6842 gfc_free_expr (code->expr3);
6843 code->expr3 = rhs;
6844 }
6845
6846 if (e->ts.type == BT_CLASS)
6847 {
6848 /* Make sure the vtab symbol is present when
6849 the module variables are generated. */
6850 gfc_typespec ts = e->ts;
6851 if (code->expr3)
6852 ts = code->expr3->ts;
6853 else if (code->ext.alloc.ts.type == BT_DERIVED)
6854 ts = code->ext.alloc.ts;
6855 gfc_find_derived_vtab (ts.u.derived);
6856 }
6857
6858 if (pointer || (dimension == 0 && codimension == 0))
6859 goto success;
6860
6861 /* Make sure the last reference node is an array specifiction. */
6862
6863 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6864 || (dimension && ref2->u.ar.dimen == 0))
6865 {
6866 gfc_error ("Array specification required in ALLOCATE statement "
6867 "at %L", &e->where);
6868 goto failure;
6869 }
6870
6871 /* Make sure that the array section reference makes sense in the
6872 context of an ALLOCATE specification. */
6873
6874 ar = &ref2->u.ar;
6875
6876 if (codimension)
6877 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6878 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6879 {
6880 gfc_error ("Coarray specification required in ALLOCATE statement "
6881 "at %L", &e->where);
6882 goto failure;
6883 }
6884
6885 for (i = 0; i < ar->dimen; i++)
6886 {
6887 if (ref2->u.ar.type == AR_ELEMENT)
6888 goto check_symbols;
6889
6890 switch (ar->dimen_type[i])
6891 {
6892 case DIMEN_ELEMENT:
6893 break;
6894
6895 case DIMEN_RANGE:
6896 if (ar->start[i] != NULL
6897 && ar->end[i] != NULL
6898 && ar->stride[i] == NULL)
6899 break;
6900
6901 /* Fall Through... */
6902
6903 case DIMEN_UNKNOWN:
6904 case DIMEN_VECTOR:
6905 case DIMEN_STAR:
6906 case DIMEN_THIS_IMAGE:
6907 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6908 &e->where);
6909 goto failure;
6910 }
6911
6912 check_symbols:
6913 for (a = code->ext.alloc.list; a; a = a->next)
6914 {
6915 sym = a->expr->symtree->n.sym;
6916
6917 /* TODO - check derived type components. */
6918 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6919 continue;
6920
6921 if ((ar->start[i] != NULL
6922 && gfc_find_sym_in_expr (sym, ar->start[i]))
6923 || (ar->end[i] != NULL
6924 && gfc_find_sym_in_expr (sym, ar->end[i])))
6925 {
6926 gfc_error ("'%s' must not appear in the array specification at "
6927 "%L in the same ALLOCATE statement where it is "
6928 "itself allocated", sym->name, &ar->where);
6929 goto failure;
6930 }
6931 }
6932 }
6933
6934 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6935 {
6936 if (ar->dimen_type[i] == DIMEN_ELEMENT
6937 || ar->dimen_type[i] == DIMEN_RANGE)
6938 {
6939 if (i == (ar->dimen + ar->codimen - 1))
6940 {
6941 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6942 "statement at %L", &e->where);
6943 goto failure;
6944 }
6945 break;
6946 }
6947
6948 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6949 && ar->stride[i] == NULL)
6950 break;
6951
6952 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6953 &e->where);
6954 goto failure;
6955 }
6956
6957 if (codimension && ar->as->rank == 0)
6958 {
6959 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6960 "at %L", &e->where);
6961 goto failure;
6962 }
6963
6964 success:
6965 return SUCCESS;
6966
6967 failure:
6968 return FAILURE;
6969 }
6970
6971 static void
6972 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6973 {
6974 gfc_expr *stat, *errmsg, *pe, *qe;
6975 gfc_alloc *a, *p, *q;
6976
6977 stat = code->expr1;
6978 errmsg = code->expr2;
6979
6980 /* Check the stat variable. */
6981 if (stat)
6982 {
6983 gfc_check_vardef_context (stat, false, _("STAT variable"));
6984
6985 if ((stat->ts.type != BT_INTEGER
6986 && !(stat->ref && (stat->ref->type == REF_ARRAY
6987 || stat->ref->type == REF_COMPONENT)))
6988 || stat->rank > 0)
6989 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6990 "variable", &stat->where);
6991
6992 for (p = code->ext.alloc.list; p; p = p->next)
6993 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6994 {
6995 gfc_ref *ref1, *ref2;
6996 bool found = true;
6997
6998 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6999 ref1 = ref1->next, ref2 = ref2->next)
7000 {
7001 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7002 continue;
7003 if (ref1->u.c.component->name != ref2->u.c.component->name)
7004 {
7005 found = false;
7006 break;
7007 }
7008 }
7009
7010 if (found)
7011 {
7012 gfc_error ("Stat-variable at %L shall not be %sd within "
7013 "the same %s statement", &stat->where, fcn, fcn);
7014 break;
7015 }
7016 }
7017 }
7018
7019 /* Check the errmsg variable. */
7020 if (errmsg)
7021 {
7022 if (!stat)
7023 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7024 &errmsg->where);
7025
7026 gfc_check_vardef_context (errmsg, false, _("ERRMSG variable"));
7027
7028 if ((errmsg->ts.type != BT_CHARACTER
7029 && !(errmsg->ref
7030 && (errmsg->ref->type == REF_ARRAY
7031 || errmsg->ref->type == REF_COMPONENT)))
7032 || errmsg->rank > 0 )
7033 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7034 "variable", &errmsg->where);
7035
7036 for (p = code->ext.alloc.list; p; p = p->next)
7037 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7038 {
7039 gfc_ref *ref1, *ref2;
7040 bool found = true;
7041
7042 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7043 ref1 = ref1->next, ref2 = ref2->next)
7044 {
7045 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7046 continue;
7047 if (ref1->u.c.component->name != ref2->u.c.component->name)
7048 {
7049 found = false;
7050 break;
7051 }
7052 }
7053
7054 if (found)
7055 {
7056 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7057 "the same %s statement", &errmsg->where, fcn, fcn);
7058 break;
7059 }
7060 }
7061 }
7062
7063 /* Check that an allocate-object appears only once in the statement.
7064 FIXME: Checking derived types is disabled. */
7065 for (p = code->ext.alloc.list; p; p = p->next)
7066 {
7067 pe = p->expr;
7068 for (q = p->next; q; q = q->next)
7069 {
7070 qe = q->expr;
7071 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7072 {
7073 /* This is a potential collision. */
7074 gfc_ref *pr = pe->ref;
7075 gfc_ref *qr = qe->ref;
7076
7077 /* Follow the references until
7078 a) They start to differ, in which case there is no error;
7079 you can deallocate a%b and a%c in a single statement
7080 b) Both of them stop, which is an error
7081 c) One of them stops, which is also an error. */
7082 while (1)
7083 {
7084 if (pr == NULL && qr == NULL)
7085 {
7086 gfc_error ("Allocate-object at %L also appears at %L",
7087 &pe->where, &qe->where);
7088 break;
7089 }
7090 else if (pr != NULL && qr == NULL)
7091 {
7092 gfc_error ("Allocate-object at %L is subobject of"
7093 " object at %L", &pe->where, &qe->where);
7094 break;
7095 }
7096 else if (pr == NULL && qr != NULL)
7097 {
7098 gfc_error ("Allocate-object at %L is subobject of"
7099 " object at %L", &qe->where, &pe->where);
7100 break;
7101 }
7102 /* Here, pr != NULL && qr != NULL */
7103 gcc_assert(pr->type == qr->type);
7104 if (pr->type == REF_ARRAY)
7105 {
7106 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7107 which are legal. */
7108 gcc_assert (qr->type == REF_ARRAY);
7109
7110 if (pr->next && qr->next)
7111 {
7112 gfc_array_ref *par = &(pr->u.ar);
7113 gfc_array_ref *qar = &(qr->u.ar);
7114 if (gfc_dep_compare_expr (par->start[0],
7115 qar->start[0]) != 0)
7116 break;
7117 }
7118 }
7119 else
7120 {
7121 if (pr->u.c.component->name != qr->u.c.component->name)
7122 break;
7123 }
7124
7125 pr = pr->next;
7126 qr = qr->next;
7127 }
7128 }
7129 }
7130 }
7131
7132 if (strcmp (fcn, "ALLOCATE") == 0)
7133 {
7134 for (a = code->ext.alloc.list; a; a = a->next)
7135 resolve_allocate_expr (a->expr, code);
7136 }
7137 else
7138 {
7139 for (a = code->ext.alloc.list; a; a = a->next)
7140 resolve_deallocate_expr (a->expr);
7141 }
7142 }
7143
7144
7145 /************ SELECT CASE resolution subroutines ************/
7146
7147 /* Callback function for our mergesort variant. Determines interval
7148 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7149 op1 > op2. Assumes we're not dealing with the default case.
7150 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7151 There are nine situations to check. */
7152
7153 static int
7154 compare_cases (const gfc_case *op1, const gfc_case *op2)
7155 {
7156 int retval;
7157
7158 if (op1->low == NULL) /* op1 = (:L) */
7159 {
7160 /* op2 = (:N), so overlap. */
7161 retval = 0;
7162 /* op2 = (M:) or (M:N), L < M */
7163 if (op2->low != NULL
7164 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7165 retval = -1;
7166 }
7167 else if (op1->high == NULL) /* op1 = (K:) */
7168 {
7169 /* op2 = (M:), so overlap. */
7170 retval = 0;
7171 /* op2 = (:N) or (M:N), K > N */
7172 if (op2->high != NULL
7173 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7174 retval = 1;
7175 }
7176 else /* op1 = (K:L) */
7177 {
7178 if (op2->low == NULL) /* op2 = (:N), K > N */
7179 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7180 ? 1 : 0;
7181 else if (op2->high == NULL) /* op2 = (M:), L < M */
7182 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7183 ? -1 : 0;
7184 else /* op2 = (M:N) */
7185 {
7186 retval = 0;
7187 /* L < M */
7188 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7189 retval = -1;
7190 /* K > N */
7191 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7192 retval = 1;
7193 }
7194 }
7195
7196 return retval;
7197 }
7198
7199
7200 /* Merge-sort a double linked case list, detecting overlap in the
7201 process. LIST is the head of the double linked case list before it
7202 is sorted. Returns the head of the sorted list if we don't see any
7203 overlap, or NULL otherwise. */
7204
7205 static gfc_case *
7206 check_case_overlap (gfc_case *list)
7207 {
7208 gfc_case *p, *q, *e, *tail;
7209 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7210
7211 /* If the passed list was empty, return immediately. */
7212 if (!list)
7213 return NULL;
7214
7215 overlap_seen = 0;
7216 insize = 1;
7217
7218 /* Loop unconditionally. The only exit from this loop is a return
7219 statement, when we've finished sorting the case list. */
7220 for (;;)
7221 {
7222 p = list;
7223 list = NULL;
7224 tail = NULL;
7225
7226 /* Count the number of merges we do in this pass. */
7227 nmerges = 0;
7228
7229 /* Loop while there exists a merge to be done. */
7230 while (p)
7231 {
7232 int i;
7233
7234 /* Count this merge. */
7235 nmerges++;
7236
7237 /* Cut the list in two pieces by stepping INSIZE places
7238 forward in the list, starting from P. */
7239 psize = 0;
7240 q = p;
7241 for (i = 0; i < insize; i++)
7242 {
7243 psize++;
7244 q = q->right;
7245 if (!q)
7246 break;
7247 }
7248 qsize = insize;
7249
7250 /* Now we have two lists. Merge them! */
7251 while (psize > 0 || (qsize > 0 && q != NULL))
7252 {
7253 /* See from which the next case to merge comes from. */
7254 if (psize == 0)
7255 {
7256 /* P is empty so the next case must come from Q. */
7257 e = q;
7258 q = q->right;
7259 qsize--;
7260 }
7261 else if (qsize == 0 || q == NULL)
7262 {
7263 /* Q is empty. */
7264 e = p;
7265 p = p->right;
7266 psize--;
7267 }
7268 else
7269 {
7270 cmp = compare_cases (p, q);
7271 if (cmp < 0)
7272 {
7273 /* The whole case range for P is less than the
7274 one for Q. */
7275 e = p;
7276 p = p->right;
7277 psize--;
7278 }
7279 else if (cmp > 0)
7280 {
7281 /* The whole case range for Q is greater than
7282 the case range for P. */
7283 e = q;
7284 q = q->right;
7285 qsize--;
7286 }
7287 else
7288 {
7289 /* The cases overlap, or they are the same
7290 element in the list. Either way, we must
7291 issue an error and get the next case from P. */
7292 /* FIXME: Sort P and Q by line number. */
7293 gfc_error ("CASE label at %L overlaps with CASE "
7294 "label at %L", &p->where, &q->where);
7295 overlap_seen = 1;
7296 e = p;
7297 p = p->right;
7298 psize--;
7299 }
7300 }
7301
7302 /* Add the next element to the merged list. */
7303 if (tail)
7304 tail->right = e;
7305 else
7306 list = e;
7307 e->left = tail;
7308 tail = e;
7309 }
7310
7311 /* P has now stepped INSIZE places along, and so has Q. So
7312 they're the same. */
7313 p = q;
7314 }
7315 tail->right = NULL;
7316
7317 /* If we have done only one merge or none at all, we've
7318 finished sorting the cases. */
7319 if (nmerges <= 1)
7320 {
7321 if (!overlap_seen)
7322 return list;
7323 else
7324 return NULL;
7325 }
7326
7327 /* Otherwise repeat, merging lists twice the size. */
7328 insize *= 2;
7329 }
7330 }
7331
7332
7333 /* Check to see if an expression is suitable for use in a CASE statement.
7334 Makes sure that all case expressions are scalar constants of the same
7335 type. Return FAILURE if anything is wrong. */
7336
7337 static gfc_try
7338 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7339 {
7340 if (e == NULL) return SUCCESS;
7341
7342 if (e->ts.type != case_expr->ts.type)
7343 {
7344 gfc_error ("Expression in CASE statement at %L must be of type %s",
7345 &e->where, gfc_basic_typename (case_expr->ts.type));
7346 return FAILURE;
7347 }
7348
7349 /* C805 (R808) For a given case-construct, each case-value shall be of
7350 the same type as case-expr. For character type, length differences
7351 are allowed, but the kind type parameters shall be the same. */
7352
7353 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7354 {
7355 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7356 &e->where, case_expr->ts.kind);
7357 return FAILURE;
7358 }
7359
7360 /* Convert the case value kind to that of case expression kind,
7361 if needed */
7362
7363 if (e->ts.kind != case_expr->ts.kind)
7364 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7365
7366 if (e->rank != 0)
7367 {
7368 gfc_error ("Expression in CASE statement at %L must be scalar",
7369 &e->where);
7370 return FAILURE;
7371 }
7372
7373 return SUCCESS;
7374 }
7375
7376
7377 /* Given a completely parsed select statement, we:
7378
7379 - Validate all expressions and code within the SELECT.
7380 - Make sure that the selection expression is not of the wrong type.
7381 - Make sure that no case ranges overlap.
7382 - Eliminate unreachable cases and unreachable code resulting from
7383 removing case labels.
7384
7385 The standard does allow unreachable cases, e.g. CASE (5:3). But
7386 they are a hassle for code generation, and to prevent that, we just
7387 cut them out here. This is not necessary for overlapping cases
7388 because they are illegal and we never even try to generate code.
7389
7390 We have the additional caveat that a SELECT construct could have
7391 been a computed GOTO in the source code. Fortunately we can fairly
7392 easily work around that here: The case_expr for a "real" SELECT CASE
7393 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7394 we have to do is make sure that the case_expr is a scalar integer
7395 expression. */
7396
7397 static void
7398 resolve_select (gfc_code *code)
7399 {
7400 gfc_code *body;
7401 gfc_expr *case_expr;
7402 gfc_case *cp, *default_case, *tail, *head;
7403 int seen_unreachable;
7404 int seen_logical;
7405 int ncases;
7406 bt type;
7407 gfc_try t;
7408
7409 if (code->expr1 == NULL)
7410 {
7411 /* This was actually a computed GOTO statement. */
7412 case_expr = code->expr2;
7413 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7414 gfc_error ("Selection expression in computed GOTO statement "
7415 "at %L must be a scalar integer expression",
7416 &case_expr->where);
7417
7418 /* Further checking is not necessary because this SELECT was built
7419 by the compiler, so it should always be OK. Just move the
7420 case_expr from expr2 to expr so that we can handle computed
7421 GOTOs as normal SELECTs from here on. */
7422 code->expr1 = code->expr2;
7423 code->expr2 = NULL;
7424 return;
7425 }
7426
7427 case_expr = code->expr1;
7428
7429 type = case_expr->ts.type;
7430 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7431 {
7432 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7433 &case_expr->where, gfc_typename (&case_expr->ts));
7434
7435 /* Punt. Going on here just produce more garbage error messages. */
7436 return;
7437 }
7438
7439 if (case_expr->rank != 0)
7440 {
7441 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7442 "expression", &case_expr->where);
7443
7444 /* Punt. */
7445 return;
7446 }
7447
7448
7449 /* Raise a warning if an INTEGER case value exceeds the range of
7450 the case-expr. Later, all expressions will be promoted to the
7451 largest kind of all case-labels. */
7452
7453 if (type == BT_INTEGER)
7454 for (body = code->block; body; body = body->block)
7455 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7456 {
7457 if (cp->low
7458 && gfc_check_integer_range (cp->low->value.integer,
7459 case_expr->ts.kind) != ARITH_OK)
7460 gfc_warning ("Expression in CASE statement at %L is "
7461 "not in the range of %s", &cp->low->where,
7462 gfc_typename (&case_expr->ts));
7463
7464 if (cp->high
7465 && cp->low != cp->high
7466 && gfc_check_integer_range (cp->high->value.integer,
7467 case_expr->ts.kind) != ARITH_OK)
7468 gfc_warning ("Expression in CASE statement at %L is "
7469 "not in the range of %s", &cp->high->where,
7470 gfc_typename (&case_expr->ts));
7471 }
7472
7473 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7474 of the SELECT CASE expression and its CASE values. Walk the lists
7475 of case values, and if we find a mismatch, promote case_expr to
7476 the appropriate kind. */
7477
7478 if (type == BT_LOGICAL || type == BT_INTEGER)
7479 {
7480 for (body = code->block; body; body = body->block)
7481 {
7482 /* Walk the case label list. */
7483 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7484 {
7485 /* Intercept the DEFAULT case. It does not have a kind. */
7486 if (cp->low == NULL && cp->high == NULL)
7487 continue;
7488
7489 /* Unreachable case ranges are discarded, so ignore. */
7490 if (cp->low != NULL && cp->high != NULL
7491 && cp->low != cp->high
7492 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7493 continue;
7494
7495 if (cp->low != NULL
7496 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7497 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7498
7499 if (cp->high != NULL
7500 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7501 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7502 }
7503 }
7504 }
7505
7506 /* Assume there is no DEFAULT case. */
7507 default_case = NULL;
7508 head = tail = NULL;
7509 ncases = 0;
7510 seen_logical = 0;
7511
7512 for (body = code->block; body; body = body->block)
7513 {
7514 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7515 t = SUCCESS;
7516 seen_unreachable = 0;
7517
7518 /* Walk the case label list, making sure that all case labels
7519 are legal. */
7520 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7521 {
7522 /* Count the number of cases in the whole construct. */
7523 ncases++;
7524
7525 /* Intercept the DEFAULT case. */
7526 if (cp->low == NULL && cp->high == NULL)
7527 {
7528 if (default_case != NULL)
7529 {
7530 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7531 "by a second DEFAULT CASE at %L",
7532 &default_case->where, &cp->where);
7533 t = FAILURE;
7534 break;
7535 }
7536 else
7537 {
7538 default_case = cp;
7539 continue;
7540 }
7541 }
7542
7543 /* Deal with single value cases and case ranges. Errors are
7544 issued from the validation function. */
7545 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7546 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7547 {
7548 t = FAILURE;
7549 break;
7550 }
7551
7552 if (type == BT_LOGICAL
7553 && ((cp->low == NULL || cp->high == NULL)
7554 || cp->low != cp->high))
7555 {
7556 gfc_error ("Logical range in CASE statement at %L is not "
7557 "allowed", &cp->low->where);
7558 t = FAILURE;
7559 break;
7560 }
7561
7562 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7563 {
7564 int value;
7565 value = cp->low->value.logical == 0 ? 2 : 1;
7566 if (value & seen_logical)
7567 {
7568 gfc_error ("Constant logical value in CASE statement "
7569 "is repeated at %L",
7570 &cp->low->where);
7571 t = FAILURE;
7572 break;
7573 }
7574 seen_logical |= value;
7575 }
7576
7577 if (cp->low != NULL && cp->high != NULL
7578 && cp->low != cp->high
7579 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7580 {
7581 if (gfc_option.warn_surprising)
7582 gfc_warning ("Range specification at %L can never "
7583 "be matched", &cp->where);
7584
7585 cp->unreachable = 1;
7586 seen_unreachable = 1;
7587 }
7588 else
7589 {
7590 /* If the case range can be matched, it can also overlap with
7591 other cases. To make sure it does not, we put it in a
7592 double linked list here. We sort that with a merge sort
7593 later on to detect any overlapping cases. */
7594 if (!head)
7595 {
7596 head = tail = cp;
7597 head->right = head->left = NULL;
7598 }
7599 else
7600 {
7601 tail->right = cp;
7602 tail->right->left = tail;
7603 tail = tail->right;
7604 tail->right = NULL;
7605 }
7606 }
7607 }
7608
7609 /* It there was a failure in the previous case label, give up
7610 for this case label list. Continue with the next block. */
7611 if (t == FAILURE)
7612 continue;
7613
7614 /* See if any case labels that are unreachable have been seen.
7615 If so, we eliminate them. This is a bit of a kludge because
7616 the case lists for a single case statement (label) is a
7617 single forward linked lists. */
7618 if (seen_unreachable)
7619 {
7620 /* Advance until the first case in the list is reachable. */
7621 while (body->ext.block.case_list != NULL
7622 && body->ext.block.case_list->unreachable)
7623 {
7624 gfc_case *n = body->ext.block.case_list;
7625 body->ext.block.case_list = body->ext.block.case_list->next;
7626 n->next = NULL;
7627 gfc_free_case_list (n);
7628 }
7629
7630 /* Strip all other unreachable cases. */
7631 if (body->ext.block.case_list)
7632 {
7633 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7634 {
7635 if (cp->next->unreachable)
7636 {
7637 gfc_case *n = cp->next;
7638 cp->next = cp->next->next;
7639 n->next = NULL;
7640 gfc_free_case_list (n);
7641 }
7642 }
7643 }
7644 }
7645 }
7646
7647 /* See if there were overlapping cases. If the check returns NULL,
7648 there was overlap. In that case we don't do anything. If head
7649 is non-NULL, we prepend the DEFAULT case. The sorted list can
7650 then used during code generation for SELECT CASE constructs with
7651 a case expression of a CHARACTER type. */
7652 if (head)
7653 {
7654 head = check_case_overlap (head);
7655
7656 /* Prepend the default_case if it is there. */
7657 if (head != NULL && default_case)
7658 {
7659 default_case->left = NULL;
7660 default_case->right = head;
7661 head->left = default_case;
7662 }
7663 }
7664
7665 /* Eliminate dead blocks that may be the result if we've seen
7666 unreachable case labels for a block. */
7667 for (body = code; body && body->block; body = body->block)
7668 {
7669 if (body->block->ext.block.case_list == NULL)
7670 {
7671 /* Cut the unreachable block from the code chain. */
7672 gfc_code *c = body->block;
7673 body->block = c->block;
7674
7675 /* Kill the dead block, but not the blocks below it. */
7676 c->block = NULL;
7677 gfc_free_statements (c);
7678 }
7679 }
7680
7681 /* More than two cases is legal but insane for logical selects.
7682 Issue a warning for it. */
7683 if (gfc_option.warn_surprising && type == BT_LOGICAL
7684 && ncases > 2)
7685 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7686 &code->loc);
7687 }
7688
7689
7690 /* Check if a derived type is extensible. */
7691
7692 bool
7693 gfc_type_is_extensible (gfc_symbol *sym)
7694 {
7695 return !(sym->attr.is_bind_c || sym->attr.sequence);
7696 }
7697
7698
7699 /* Resolve an associate name: Resolve target and ensure the type-spec is
7700 correct as well as possibly the array-spec. */
7701
7702 static void
7703 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7704 {
7705 gfc_expr* target;
7706
7707 gcc_assert (sym->assoc);
7708 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7709
7710 /* If this is for SELECT TYPE, the target may not yet be set. In that
7711 case, return. Resolution will be called later manually again when
7712 this is done. */
7713 target = sym->assoc->target;
7714 if (!target)
7715 return;
7716 gcc_assert (!sym->assoc->dangling);
7717
7718 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7719 return;
7720
7721 /* For variable targets, we get some attributes from the target. */
7722 if (target->expr_type == EXPR_VARIABLE)
7723 {
7724 gfc_symbol* tsym;
7725
7726 gcc_assert (target->symtree);
7727 tsym = target->symtree->n.sym;
7728
7729 sym->attr.asynchronous = tsym->attr.asynchronous;
7730 sym->attr.volatile_ = tsym->attr.volatile_;
7731
7732 sym->attr.target = (tsym->attr.target || tsym->attr.pointer);
7733 }
7734
7735 /* Get type if this was not already set. Note that it can be
7736 some other type than the target in case this is a SELECT TYPE
7737 selector! So we must not update when the type is already there. */
7738 if (sym->ts.type == BT_UNKNOWN)
7739 sym->ts = target->ts;
7740 gcc_assert (sym->ts.type != BT_UNKNOWN);
7741
7742 /* See if this is a valid association-to-variable. */
7743 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7744 && !gfc_has_vector_subscript (target));
7745
7746 /* Finally resolve if this is an array or not. */
7747 if (sym->attr.dimension && target->rank == 0)
7748 {
7749 gfc_error ("Associate-name '%s' at %L is used as array",
7750 sym->name, &sym->declared_at);
7751 sym->attr.dimension = 0;
7752 return;
7753 }
7754 if (target->rank > 0)
7755 sym->attr.dimension = 1;
7756
7757 if (sym->attr.dimension)
7758 {
7759 sym->as = gfc_get_array_spec ();
7760 sym->as->rank = target->rank;
7761 sym->as->type = AS_DEFERRED;
7762
7763 /* Target must not be coindexed, thus the associate-variable
7764 has no corank. */
7765 sym->as->corank = 0;
7766 }
7767 }
7768
7769
7770 /* Resolve a SELECT TYPE statement. */
7771
7772 static void
7773 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7774 {
7775 gfc_symbol *selector_type;
7776 gfc_code *body, *new_st, *if_st, *tail;
7777 gfc_code *class_is = NULL, *default_case = NULL;
7778 gfc_case *c;
7779 gfc_symtree *st;
7780 char name[GFC_MAX_SYMBOL_LEN];
7781 gfc_namespace *ns;
7782 int error = 0;
7783
7784 ns = code->ext.block.ns;
7785 gfc_resolve (ns);
7786
7787 /* Check for F03:C813. */
7788 if (code->expr1->ts.type != BT_CLASS
7789 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7790 {
7791 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7792 "at %L", &code->loc);
7793 return;
7794 }
7795
7796 if (code->expr2)
7797 {
7798 if (code->expr1->symtree->n.sym->attr.untyped)
7799 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7800 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7801 }
7802 else
7803 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7804
7805 /* Loop over TYPE IS / CLASS IS cases. */
7806 for (body = code->block; body; body = body->block)
7807 {
7808 c = body->ext.block.case_list;
7809
7810 /* Check F03:C815. */
7811 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7812 && !gfc_type_is_extensible (c->ts.u.derived))
7813 {
7814 gfc_error ("Derived type '%s' at %L must be extensible",
7815 c->ts.u.derived->name, &c->where);
7816 error++;
7817 continue;
7818 }
7819
7820 /* Check F03:C816. */
7821 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7822 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7823 {
7824 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7825 c->ts.u.derived->name, &c->where, selector_type->name);
7826 error++;
7827 continue;
7828 }
7829
7830 /* Intercept the DEFAULT case. */
7831 if (c->ts.type == BT_UNKNOWN)
7832 {
7833 /* Check F03:C818. */
7834 if (default_case)
7835 {
7836 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7837 "by a second DEFAULT CASE at %L",
7838 &default_case->ext.block.case_list->where, &c->where);
7839 error++;
7840 continue;
7841 }
7842
7843 default_case = body;
7844 }
7845 }
7846
7847 if (error > 0)
7848 return;
7849
7850 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7851 target if present. If there are any EXIT statements referring to the
7852 SELECT TYPE construct, this is no problem because the gfc_code
7853 reference stays the same and EXIT is equally possible from the BLOCK
7854 it is changed to. */
7855 code->op = EXEC_BLOCK;
7856 if (code->expr2)
7857 {
7858 gfc_association_list* assoc;
7859
7860 assoc = gfc_get_association_list ();
7861 assoc->st = code->expr1->symtree;
7862 assoc->target = gfc_copy_expr (code->expr2);
7863 /* assoc->variable will be set by resolve_assoc_var. */
7864
7865 code->ext.block.assoc = assoc;
7866 code->expr1->symtree->n.sym->assoc = assoc;
7867
7868 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7869 }
7870 else
7871 code->ext.block.assoc = NULL;
7872
7873 /* Add EXEC_SELECT to switch on type. */
7874 new_st = gfc_get_code ();
7875 new_st->op = code->op;
7876 new_st->expr1 = code->expr1;
7877 new_st->expr2 = code->expr2;
7878 new_st->block = code->block;
7879 code->expr1 = code->expr2 = NULL;
7880 code->block = NULL;
7881 if (!ns->code)
7882 ns->code = new_st;
7883 else
7884 ns->code->next = new_st;
7885 code = new_st;
7886 code->op = EXEC_SELECT;
7887 gfc_add_vptr_component (code->expr1);
7888 gfc_add_hash_component (code->expr1);
7889
7890 /* Loop over TYPE IS / CLASS IS cases. */
7891 for (body = code->block; body; body = body->block)
7892 {
7893 c = body->ext.block.case_list;
7894
7895 if (c->ts.type == BT_DERIVED)
7896 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7897 c->ts.u.derived->hash_value);
7898
7899 else if (c->ts.type == BT_UNKNOWN)
7900 continue;
7901
7902 /* Associate temporary to selector. This should only be done
7903 when this case is actually true, so build a new ASSOCIATE
7904 that does precisely this here (instead of using the
7905 'global' one). */
7906
7907 if (c->ts.type == BT_CLASS)
7908 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
7909 else
7910 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
7911 st = gfc_find_symtree (ns->sym_root, name);
7912 gcc_assert (st->n.sym->assoc);
7913 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7914 if (c->ts.type == BT_DERIVED)
7915 gfc_add_data_component (st->n.sym->assoc->target);
7916
7917 new_st = gfc_get_code ();
7918 new_st->op = EXEC_BLOCK;
7919 new_st->ext.block.ns = gfc_build_block_ns (ns);
7920 new_st->ext.block.ns->code = body->next;
7921 body->next = new_st;
7922
7923 /* Chain in the new list only if it is marked as dangling. Otherwise
7924 there is a CASE label overlap and this is already used. Just ignore,
7925 the error is diagonsed elsewhere. */
7926 if (st->n.sym->assoc->dangling)
7927 {
7928 new_st->ext.block.assoc = st->n.sym->assoc;
7929 st->n.sym->assoc->dangling = 0;
7930 }
7931
7932 resolve_assoc_var (st->n.sym, false);
7933 }
7934
7935 /* Take out CLASS IS cases for separate treatment. */
7936 body = code;
7937 while (body && body->block)
7938 {
7939 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
7940 {
7941 /* Add to class_is list. */
7942 if (class_is == NULL)
7943 {
7944 class_is = body->block;
7945 tail = class_is;
7946 }
7947 else
7948 {
7949 for (tail = class_is; tail->block; tail = tail->block) ;
7950 tail->block = body->block;
7951 tail = tail->block;
7952 }
7953 /* Remove from EXEC_SELECT list. */
7954 body->block = body->block->block;
7955 tail->block = NULL;
7956 }
7957 else
7958 body = body->block;
7959 }
7960
7961 if (class_is)
7962 {
7963 gfc_symbol *vtab;
7964
7965 if (!default_case)
7966 {
7967 /* Add a default case to hold the CLASS IS cases. */
7968 for (tail = code; tail->block; tail = tail->block) ;
7969 tail->block = gfc_get_code ();
7970 tail = tail->block;
7971 tail->op = EXEC_SELECT_TYPE;
7972 tail->ext.block.case_list = gfc_get_case ();
7973 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
7974 tail->next = NULL;
7975 default_case = tail;
7976 }
7977
7978 /* More than one CLASS IS block? */
7979 if (class_is->block)
7980 {
7981 gfc_code **c1,*c2;
7982 bool swapped;
7983 /* Sort CLASS IS blocks by extension level. */
7984 do
7985 {
7986 swapped = false;
7987 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7988 {
7989 c2 = (*c1)->block;
7990 /* F03:C817 (check for doubles). */
7991 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
7992 == c2->ext.block.case_list->ts.u.derived->hash_value)
7993 {
7994 gfc_error ("Double CLASS IS block in SELECT TYPE "
7995 "statement at %L",
7996 &c2->ext.block.case_list->where);
7997 return;
7998 }
7999 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8000 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8001 {
8002 /* Swap. */
8003 (*c1)->block = c2->block;
8004 c2->block = *c1;
8005 *c1 = c2;
8006 swapped = true;
8007 }
8008 }
8009 }
8010 while (swapped);
8011 }
8012
8013 /* Generate IF chain. */
8014 if_st = gfc_get_code ();
8015 if_st->op = EXEC_IF;
8016 new_st = if_st;
8017 for (body = class_is; body; body = body->block)
8018 {
8019 new_st->block = gfc_get_code ();
8020 new_st = new_st->block;
8021 new_st->op = EXEC_IF;
8022 /* Set up IF condition: Call _gfortran_is_extension_of. */
8023 new_st->expr1 = gfc_get_expr ();
8024 new_st->expr1->expr_type = EXPR_FUNCTION;
8025 new_st->expr1->ts.type = BT_LOGICAL;
8026 new_st->expr1->ts.kind = 4;
8027 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8028 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8029 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8030 /* Set up arguments. */
8031 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8032 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8033 new_st->expr1->value.function.actual->expr->where = code->loc;
8034 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8035 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8036 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8037 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8038 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8039 new_st->next = body->next;
8040 }
8041 if (default_case->next)
8042 {
8043 new_st->block = gfc_get_code ();
8044 new_st = new_st->block;
8045 new_st->op = EXEC_IF;
8046 new_st->next = default_case->next;
8047 }
8048
8049 /* Replace CLASS DEFAULT code by the IF chain. */
8050 default_case->next = if_st;
8051 }
8052
8053 /* Resolve the internal code. This can not be done earlier because
8054 it requires that the sym->assoc of selectors is set already. */
8055 gfc_current_ns = ns;
8056 gfc_resolve_blocks (code->block, gfc_current_ns);
8057 gfc_current_ns = old_ns;
8058
8059 resolve_select (code);
8060 }
8061
8062
8063 /* Resolve a transfer statement. This is making sure that:
8064 -- a derived type being transferred has only non-pointer components
8065 -- a derived type being transferred doesn't have private components, unless
8066 it's being transferred from the module where the type was defined
8067 -- we're not trying to transfer a whole assumed size array. */
8068
8069 static void
8070 resolve_transfer (gfc_code *code)
8071 {
8072 gfc_typespec *ts;
8073 gfc_symbol *sym;
8074 gfc_ref *ref;
8075 gfc_expr *exp;
8076
8077 exp = code->expr1;
8078
8079 while (exp != NULL && exp->expr_type == EXPR_OP
8080 && exp->value.op.op == INTRINSIC_PARENTHESES)
8081 exp = exp->value.op.op1;
8082
8083 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8084 && exp->expr_type != EXPR_FUNCTION))
8085 return;
8086
8087 /* If we are reading, the variable will be changed. Note that
8088 code->ext.dt may be NULL if the TRANSFER is related to
8089 an INQUIRE statement -- but in this case, we are not reading, either. */
8090 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8091 && gfc_check_vardef_context (exp, false, _("item in READ")) == FAILURE)
8092 return;
8093
8094 sym = exp->symtree->n.sym;
8095 ts = &sym->ts;
8096
8097 /* Go to actual component transferred. */
8098 for (ref = exp->ref; ref; ref = ref->next)
8099 if (ref->type == REF_COMPONENT)
8100 ts = &ref->u.c.component->ts;
8101
8102 if (ts->type == BT_CLASS)
8103 {
8104 /* FIXME: Test for defined input/output. */
8105 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8106 "it is processed by a defined input/output procedure",
8107 &code->loc);
8108 return;
8109 }
8110
8111 if (ts->type == BT_DERIVED)
8112 {
8113 /* Check that transferred derived type doesn't contain POINTER
8114 components. */
8115 if (ts->u.derived->attr.pointer_comp)
8116 {
8117 gfc_error ("Data transfer element at %L cannot have "
8118 "POINTER components", &code->loc);
8119 return;
8120 }
8121
8122 /* F08:C935. */
8123 if (ts->u.derived->attr.proc_pointer_comp)
8124 {
8125 gfc_error ("Data transfer element at %L cannot have "
8126 "procedure pointer components", &code->loc);
8127 return;
8128 }
8129
8130 if (ts->u.derived->attr.alloc_comp)
8131 {
8132 gfc_error ("Data transfer element at %L cannot have "
8133 "ALLOCATABLE components", &code->loc);
8134 return;
8135 }
8136
8137 if (derived_inaccessible (ts->u.derived))
8138 {
8139 gfc_error ("Data transfer element at %L cannot have "
8140 "PRIVATE components",&code->loc);
8141 return;
8142 }
8143 }
8144
8145 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
8146 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8147 {
8148 gfc_error ("Data transfer element at %L cannot be a full reference to "
8149 "an assumed-size array", &code->loc);
8150 return;
8151 }
8152 }
8153
8154
8155 /*********** Toplevel code resolution subroutines ***********/
8156
8157 /* Find the set of labels that are reachable from this block. We also
8158 record the last statement in each block. */
8159
8160 static void
8161 find_reachable_labels (gfc_code *block)
8162 {
8163 gfc_code *c;
8164
8165 if (!block)
8166 return;
8167
8168 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8169
8170 /* Collect labels in this block. We don't keep those corresponding
8171 to END {IF|SELECT}, these are checked in resolve_branch by going
8172 up through the code_stack. */
8173 for (c = block; c; c = c->next)
8174 {
8175 if (c->here && c->op != EXEC_END_BLOCK)
8176 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8177 }
8178
8179 /* Merge with labels from parent block. */
8180 if (cs_base->prev)
8181 {
8182 gcc_assert (cs_base->prev->reachable_labels);
8183 bitmap_ior_into (cs_base->reachable_labels,
8184 cs_base->prev->reachable_labels);
8185 }
8186 }
8187
8188
8189 static void
8190 resolve_sync (gfc_code *code)
8191 {
8192 /* Check imageset. The * case matches expr1 == NULL. */
8193 if (code->expr1)
8194 {
8195 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8196 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8197 "INTEGER expression", &code->expr1->where);
8198 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8199 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8200 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8201 &code->expr1->where);
8202 else if (code->expr1->expr_type == EXPR_ARRAY
8203 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8204 {
8205 gfc_constructor *cons;
8206 cons = gfc_constructor_first (code->expr1->value.constructor);
8207 for (; cons; cons = gfc_constructor_next (cons))
8208 if (cons->expr->expr_type == EXPR_CONSTANT
8209 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8210 gfc_error ("Imageset argument at %L must between 1 and "
8211 "num_images()", &cons->expr->where);
8212 }
8213 }
8214
8215 /* Check STAT. */
8216 if (code->expr2
8217 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8218 || code->expr2->expr_type != EXPR_VARIABLE))
8219 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8220 &code->expr2->where);
8221
8222 /* Check ERRMSG. */
8223 if (code->expr3
8224 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8225 || code->expr3->expr_type != EXPR_VARIABLE))
8226 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8227 &code->expr3->where);
8228 }
8229
8230
8231 /* Given a branch to a label, see if the branch is conforming.
8232 The code node describes where the branch is located. */
8233
8234 static void
8235 resolve_branch (gfc_st_label *label, gfc_code *code)
8236 {
8237 code_stack *stack;
8238
8239 if (label == NULL)
8240 return;
8241
8242 /* Step one: is this a valid branching target? */
8243
8244 if (label->defined == ST_LABEL_UNKNOWN)
8245 {
8246 gfc_error ("Label %d referenced at %L is never defined", label->value,
8247 &label->where);
8248 return;
8249 }
8250
8251 if (label->defined != ST_LABEL_TARGET)
8252 {
8253 gfc_error ("Statement at %L is not a valid branch target statement "
8254 "for the branch statement at %L", &label->where, &code->loc);
8255 return;
8256 }
8257
8258 /* Step two: make sure this branch is not a branch to itself ;-) */
8259
8260 if (code->here == label)
8261 {
8262 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8263 return;
8264 }
8265
8266 /* Step three: See if the label is in the same block as the
8267 branching statement. The hard work has been done by setting up
8268 the bitmap reachable_labels. */
8269
8270 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8271 {
8272 /* Check now whether there is a CRITICAL construct; if so, check
8273 whether the label is still visible outside of the CRITICAL block,
8274 which is invalid. */
8275 for (stack = cs_base; stack; stack = stack->prev)
8276 if (stack->current->op == EXEC_CRITICAL
8277 && bitmap_bit_p (stack->reachable_labels, label->value))
8278 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8279 " at %L", &code->loc, &label->where);
8280
8281 return;
8282 }
8283
8284 /* Step four: If we haven't found the label in the bitmap, it may
8285 still be the label of the END of the enclosing block, in which
8286 case we find it by going up the code_stack. */
8287
8288 for (stack = cs_base; stack; stack = stack->prev)
8289 {
8290 if (stack->current->next && stack->current->next->here == label)
8291 break;
8292 if (stack->current->op == EXEC_CRITICAL)
8293 {
8294 /* Note: A label at END CRITICAL does not leave the CRITICAL
8295 construct as END CRITICAL is still part of it. */
8296 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8297 " at %L", &code->loc, &label->where);
8298 return;
8299 }
8300 }
8301
8302 if (stack)
8303 {
8304 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
8305 return;
8306 }
8307
8308 /* The label is not in an enclosing block, so illegal. This was
8309 allowed in Fortran 66, so we allow it as extension. No
8310 further checks are necessary in this case. */
8311 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8312 "as the GOTO statement at %L", &label->where,
8313 &code->loc);
8314 return;
8315 }
8316
8317
8318 /* Check whether EXPR1 has the same shape as EXPR2. */
8319
8320 static gfc_try
8321 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8322 {
8323 mpz_t shape[GFC_MAX_DIMENSIONS];
8324 mpz_t shape2[GFC_MAX_DIMENSIONS];
8325 gfc_try result = FAILURE;
8326 int i;
8327
8328 /* Compare the rank. */
8329 if (expr1->rank != expr2->rank)
8330 return result;
8331
8332 /* Compare the size of each dimension. */
8333 for (i=0; i<expr1->rank; i++)
8334 {
8335 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8336 goto ignore;
8337
8338 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8339 goto ignore;
8340
8341 if (mpz_cmp (shape[i], shape2[i]))
8342 goto over;
8343 }
8344
8345 /* When either of the two expression is an assumed size array, we
8346 ignore the comparison of dimension sizes. */
8347 ignore:
8348 result = SUCCESS;
8349
8350 over:
8351 for (i--; i >= 0; i--)
8352 {
8353 mpz_clear (shape[i]);
8354 mpz_clear (shape2[i]);
8355 }
8356 return result;
8357 }
8358
8359
8360 /* Check whether a WHERE assignment target or a WHERE mask expression
8361 has the same shape as the outmost WHERE mask expression. */
8362
8363 static void
8364 resolve_where (gfc_code *code, gfc_expr *mask)
8365 {
8366 gfc_code *cblock;
8367 gfc_code *cnext;
8368 gfc_expr *e = NULL;
8369
8370 cblock = code->block;
8371
8372 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8373 In case of nested WHERE, only the outmost one is stored. */
8374 if (mask == NULL) /* outmost WHERE */
8375 e = cblock->expr1;
8376 else /* inner WHERE */
8377 e = mask;
8378
8379 while (cblock)
8380 {
8381 if (cblock->expr1)
8382 {
8383 /* Check if the mask-expr has a consistent shape with the
8384 outmost WHERE mask-expr. */
8385 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8386 gfc_error ("WHERE mask at %L has inconsistent shape",
8387 &cblock->expr1->where);
8388 }
8389
8390 /* the assignment statement of a WHERE statement, or the first
8391 statement in where-body-construct of a WHERE construct */
8392 cnext = cblock->next;
8393 while (cnext)
8394 {
8395 switch (cnext->op)
8396 {
8397 /* WHERE assignment statement */
8398 case EXEC_ASSIGN:
8399
8400 /* Check shape consistent for WHERE assignment target. */
8401 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8402 gfc_error ("WHERE assignment target at %L has "
8403 "inconsistent shape", &cnext->expr1->where);
8404 break;
8405
8406
8407 case EXEC_ASSIGN_CALL:
8408 resolve_call (cnext);
8409 if (!cnext->resolved_sym->attr.elemental)
8410 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8411 &cnext->ext.actual->expr->where);
8412 break;
8413
8414 /* WHERE or WHERE construct is part of a where-body-construct */
8415 case EXEC_WHERE:
8416 resolve_where (cnext, e);
8417 break;
8418
8419 default:
8420 gfc_error ("Unsupported statement inside WHERE at %L",
8421 &cnext->loc);
8422 }
8423 /* the next statement within the same where-body-construct */
8424 cnext = cnext->next;
8425 }
8426 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8427 cblock = cblock->block;
8428 }
8429 }
8430
8431
8432 /* Resolve assignment in FORALL construct.
8433 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8434 FORALL index variables. */
8435
8436 static void
8437 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8438 {
8439 int n;
8440
8441 for (n = 0; n < nvar; n++)
8442 {
8443 gfc_symbol *forall_index;
8444
8445 forall_index = var_expr[n]->symtree->n.sym;
8446
8447 /* Check whether the assignment target is one of the FORALL index
8448 variable. */
8449 if ((code->expr1->expr_type == EXPR_VARIABLE)
8450 && (code->expr1->symtree->n.sym == forall_index))
8451 gfc_error ("Assignment to a FORALL index variable at %L",
8452 &code->expr1->where);
8453 else
8454 {
8455 /* If one of the FORALL index variables doesn't appear in the
8456 assignment variable, then there could be a many-to-one
8457 assignment. Emit a warning rather than an error because the
8458 mask could be resolving this problem. */
8459 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8460 gfc_warning ("The FORALL with index '%s' is not used on the "
8461 "left side of the assignment at %L and so might "
8462 "cause multiple assignment to this object",
8463 var_expr[n]->symtree->name, &code->expr1->where);
8464 }
8465 }
8466 }
8467
8468
8469 /* Resolve WHERE statement in FORALL construct. */
8470
8471 static void
8472 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8473 gfc_expr **var_expr)
8474 {
8475 gfc_code *cblock;
8476 gfc_code *cnext;
8477
8478 cblock = code->block;
8479 while (cblock)
8480 {
8481 /* the assignment statement of a WHERE statement, or the first
8482 statement in where-body-construct of a WHERE construct */
8483 cnext = cblock->next;
8484 while (cnext)
8485 {
8486 switch (cnext->op)
8487 {
8488 /* WHERE assignment statement */
8489 case EXEC_ASSIGN:
8490 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8491 break;
8492
8493 /* WHERE operator assignment statement */
8494 case EXEC_ASSIGN_CALL:
8495 resolve_call (cnext);
8496 if (!cnext->resolved_sym->attr.elemental)
8497 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8498 &cnext->ext.actual->expr->where);
8499 break;
8500
8501 /* WHERE or WHERE construct is part of a where-body-construct */
8502 case EXEC_WHERE:
8503 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8504 break;
8505
8506 default:
8507 gfc_error ("Unsupported statement inside WHERE at %L",
8508 &cnext->loc);
8509 }
8510 /* the next statement within the same where-body-construct */
8511 cnext = cnext->next;
8512 }
8513 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8514 cblock = cblock->block;
8515 }
8516 }
8517
8518
8519 /* Traverse the FORALL body to check whether the following errors exist:
8520 1. For assignment, check if a many-to-one assignment happens.
8521 2. For WHERE statement, check the WHERE body to see if there is any
8522 many-to-one assignment. */
8523
8524 static void
8525 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8526 {
8527 gfc_code *c;
8528
8529 c = code->block->next;
8530 while (c)
8531 {
8532 switch (c->op)
8533 {
8534 case EXEC_ASSIGN:
8535 case EXEC_POINTER_ASSIGN:
8536 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8537 break;
8538
8539 case EXEC_ASSIGN_CALL:
8540 resolve_call (c);
8541 break;
8542
8543 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8544 there is no need to handle it here. */
8545 case EXEC_FORALL:
8546 break;
8547 case EXEC_WHERE:
8548 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8549 break;
8550 default:
8551 break;
8552 }
8553 /* The next statement in the FORALL body. */
8554 c = c->next;
8555 }
8556 }
8557
8558
8559 /* Counts the number of iterators needed inside a forall construct, including
8560 nested forall constructs. This is used to allocate the needed memory
8561 in gfc_resolve_forall. */
8562
8563 static int
8564 gfc_count_forall_iterators (gfc_code *code)
8565 {
8566 int max_iters, sub_iters, current_iters;
8567 gfc_forall_iterator *fa;
8568
8569 gcc_assert(code->op == EXEC_FORALL);
8570 max_iters = 0;
8571 current_iters = 0;
8572
8573 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8574 current_iters ++;
8575
8576 code = code->block->next;
8577
8578 while (code)
8579 {
8580 if (code->op == EXEC_FORALL)
8581 {
8582 sub_iters = gfc_count_forall_iterators (code);
8583 if (sub_iters > max_iters)
8584 max_iters = sub_iters;
8585 }
8586 code = code->next;
8587 }
8588
8589 return current_iters + max_iters;
8590 }
8591
8592
8593 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8594 gfc_resolve_forall_body to resolve the FORALL body. */
8595
8596 static void
8597 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8598 {
8599 static gfc_expr **var_expr;
8600 static int total_var = 0;
8601 static int nvar = 0;
8602 int old_nvar, tmp;
8603 gfc_forall_iterator *fa;
8604 int i;
8605
8606 old_nvar = nvar;
8607
8608 /* Start to resolve a FORALL construct */
8609 if (forall_save == 0)
8610 {
8611 /* Count the total number of FORALL index in the nested FORALL
8612 construct in order to allocate the VAR_EXPR with proper size. */
8613 total_var = gfc_count_forall_iterators (code);
8614
8615 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8616 var_expr = XCNEWVEC (gfc_expr *, total_var);
8617 }
8618
8619 /* The information about FORALL iterator, including FORALL index start, end
8620 and stride. The FORALL index can not appear in start, end or stride. */
8621 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8622 {
8623 /* Check if any outer FORALL index name is the same as the current
8624 one. */
8625 for (i = 0; i < nvar; i++)
8626 {
8627 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8628 {
8629 gfc_error ("An outer FORALL construct already has an index "
8630 "with this name %L", &fa->var->where);
8631 }
8632 }
8633
8634 /* Record the current FORALL index. */
8635 var_expr[nvar] = gfc_copy_expr (fa->var);
8636
8637 nvar++;
8638
8639 /* No memory leak. */
8640 gcc_assert (nvar <= total_var);
8641 }
8642
8643 /* Resolve the FORALL body. */
8644 gfc_resolve_forall_body (code, nvar, var_expr);
8645
8646 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8647 gfc_resolve_blocks (code->block, ns);
8648
8649 tmp = nvar;
8650 nvar = old_nvar;
8651 /* Free only the VAR_EXPRs allocated in this frame. */
8652 for (i = nvar; i < tmp; i++)
8653 gfc_free_expr (var_expr[i]);
8654
8655 if (nvar == 0)
8656 {
8657 /* We are in the outermost FORALL construct. */
8658 gcc_assert (forall_save == 0);
8659
8660 /* VAR_EXPR is not needed any more. */
8661 free (var_expr);
8662 total_var = 0;
8663 }
8664 }
8665
8666
8667 /* Resolve a BLOCK construct statement. */
8668
8669 static void
8670 resolve_block_construct (gfc_code* code)
8671 {
8672 /* Resolve the BLOCK's namespace. */
8673 gfc_resolve (code->ext.block.ns);
8674
8675 /* For an ASSOCIATE block, the associations (and their targets) are already
8676 resolved during resolve_symbol. */
8677 }
8678
8679
8680 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8681 DO code nodes. */
8682
8683 static void resolve_code (gfc_code *, gfc_namespace *);
8684
8685 void
8686 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8687 {
8688 gfc_try t;
8689
8690 for (; b; b = b->block)
8691 {
8692 t = gfc_resolve_expr (b->expr1);
8693 if (gfc_resolve_expr (b->expr2) == FAILURE)
8694 t = FAILURE;
8695
8696 switch (b->op)
8697 {
8698 case EXEC_IF:
8699 if (t == SUCCESS && b->expr1 != NULL
8700 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8701 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8702 &b->expr1->where);
8703 break;
8704
8705 case EXEC_WHERE:
8706 if (t == SUCCESS
8707 && b->expr1 != NULL
8708 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8709 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8710 &b->expr1->where);
8711 break;
8712
8713 case EXEC_GOTO:
8714 resolve_branch (b->label1, b);
8715 break;
8716
8717 case EXEC_BLOCK:
8718 resolve_block_construct (b);
8719 break;
8720
8721 case EXEC_SELECT:
8722 case EXEC_SELECT_TYPE:
8723 case EXEC_FORALL:
8724 case EXEC_DO:
8725 case EXEC_DO_WHILE:
8726 case EXEC_CRITICAL:
8727 case EXEC_READ:
8728 case EXEC_WRITE:
8729 case EXEC_IOLENGTH:
8730 case EXEC_WAIT:
8731 break;
8732
8733 case EXEC_OMP_ATOMIC:
8734 case EXEC_OMP_CRITICAL:
8735 case EXEC_OMP_DO:
8736 case EXEC_OMP_MASTER:
8737 case EXEC_OMP_ORDERED:
8738 case EXEC_OMP_PARALLEL:
8739 case EXEC_OMP_PARALLEL_DO:
8740 case EXEC_OMP_PARALLEL_SECTIONS:
8741 case EXEC_OMP_PARALLEL_WORKSHARE:
8742 case EXEC_OMP_SECTIONS:
8743 case EXEC_OMP_SINGLE:
8744 case EXEC_OMP_TASK:
8745 case EXEC_OMP_TASKWAIT:
8746 case EXEC_OMP_WORKSHARE:
8747 break;
8748
8749 default:
8750 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8751 }
8752
8753 resolve_code (b->next, ns);
8754 }
8755 }
8756
8757
8758 /* Does everything to resolve an ordinary assignment. Returns true
8759 if this is an interface assignment. */
8760 static bool
8761 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8762 {
8763 bool rval = false;
8764 gfc_expr *lhs;
8765 gfc_expr *rhs;
8766 int llen = 0;
8767 int rlen = 0;
8768 int n;
8769 gfc_ref *ref;
8770
8771 if (gfc_extend_assign (code, ns) == SUCCESS)
8772 {
8773 gfc_expr** rhsptr;
8774
8775 if (code->op == EXEC_ASSIGN_CALL)
8776 {
8777 lhs = code->ext.actual->expr;
8778 rhsptr = &code->ext.actual->next->expr;
8779 }
8780 else
8781 {
8782 gfc_actual_arglist* args;
8783 gfc_typebound_proc* tbp;
8784
8785 gcc_assert (code->op == EXEC_COMPCALL);
8786
8787 args = code->expr1->value.compcall.actual;
8788 lhs = args->expr;
8789 rhsptr = &args->next->expr;
8790
8791 tbp = code->expr1->value.compcall.tbp;
8792 gcc_assert (!tbp->is_generic);
8793 }
8794
8795 /* Make a temporary rhs when there is a default initializer
8796 and rhs is the same symbol as the lhs. */
8797 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8798 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8799 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8800 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8801 *rhsptr = gfc_get_parentheses (*rhsptr);
8802
8803 return true;
8804 }
8805
8806 lhs = code->expr1;
8807 rhs = code->expr2;
8808
8809 if (rhs->is_boz
8810 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8811 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8812 &code->loc) == FAILURE)
8813 return false;
8814
8815 /* Handle the case of a BOZ literal on the RHS. */
8816 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8817 {
8818 int rc;
8819 if (gfc_option.warn_surprising)
8820 gfc_warning ("BOZ literal at %L is bitwise transferred "
8821 "non-integer symbol '%s'", &code->loc,
8822 lhs->symtree->n.sym->name);
8823
8824 if (!gfc_convert_boz (rhs, &lhs->ts))
8825 return false;
8826 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8827 {
8828 if (rc == ARITH_UNDERFLOW)
8829 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8830 ". This check can be disabled with the option "
8831 "-fno-range-check", &rhs->where);
8832 else if (rc == ARITH_OVERFLOW)
8833 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8834 ". This check can be disabled with the option "
8835 "-fno-range-check", &rhs->where);
8836 else if (rc == ARITH_NAN)
8837 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8838 ". This check can be disabled with the option "
8839 "-fno-range-check", &rhs->where);
8840 return false;
8841 }
8842 }
8843
8844 if (lhs->ts.type == BT_CHARACTER
8845 && gfc_option.warn_character_truncation)
8846 {
8847 if (lhs->ts.u.cl != NULL
8848 && lhs->ts.u.cl->length != NULL
8849 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8850 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8851
8852 if (rhs->expr_type == EXPR_CONSTANT)
8853 rlen = rhs->value.character.length;
8854
8855 else if (rhs->ts.u.cl != NULL
8856 && rhs->ts.u.cl->length != NULL
8857 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8858 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8859
8860 if (rlen && llen && rlen > llen)
8861 gfc_warning_now ("CHARACTER expression will be truncated "
8862 "in assignment (%d/%d) at %L",
8863 llen, rlen, &code->loc);
8864 }
8865
8866 /* Ensure that a vector index expression for the lvalue is evaluated
8867 to a temporary if the lvalue symbol is referenced in it. */
8868 if (lhs->rank)
8869 {
8870 for (ref = lhs->ref; ref; ref= ref->next)
8871 if (ref->type == REF_ARRAY)
8872 {
8873 for (n = 0; n < ref->u.ar.dimen; n++)
8874 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8875 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8876 ref->u.ar.start[n]))
8877 ref->u.ar.start[n]
8878 = gfc_get_parentheses (ref->u.ar.start[n]);
8879 }
8880 }
8881
8882 if (gfc_pure (NULL))
8883 {
8884 if (lhs->ts.type == BT_DERIVED
8885 && lhs->expr_type == EXPR_VARIABLE
8886 && lhs->ts.u.derived->attr.pointer_comp
8887 && rhs->expr_type == EXPR_VARIABLE
8888 && (gfc_impure_variable (rhs->symtree->n.sym)
8889 || gfc_is_coindexed (rhs)))
8890 {
8891 /* F2008, C1283. */
8892 if (gfc_is_coindexed (rhs))
8893 gfc_error ("Coindexed expression at %L is assigned to "
8894 "a derived type variable with a POINTER "
8895 "component in a PURE procedure",
8896 &rhs->where);
8897 else
8898 gfc_error ("The impure variable at %L is assigned to "
8899 "a derived type variable with a POINTER "
8900 "component in a PURE procedure (12.6)",
8901 &rhs->where);
8902 return rval;
8903 }
8904
8905 /* Fortran 2008, C1283. */
8906 if (gfc_is_coindexed (lhs))
8907 {
8908 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8909 "procedure", &rhs->where);
8910 return rval;
8911 }
8912 }
8913
8914 if (gfc_implicit_pure (NULL))
8915 {
8916 if (lhs->expr_type == EXPR_VARIABLE
8917 && lhs->symtree->n.sym != gfc_current_ns->proc_name
8918 && lhs->symtree->n.sym->ns != gfc_current_ns)
8919 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8920
8921 if (lhs->ts.type == BT_DERIVED
8922 && lhs->expr_type == EXPR_VARIABLE
8923 && lhs->ts.u.derived->attr.pointer_comp
8924 && rhs->expr_type == EXPR_VARIABLE
8925 && (gfc_impure_variable (rhs->symtree->n.sym)
8926 || gfc_is_coindexed (rhs)))
8927 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8928
8929 /* Fortran 2008, C1283. */
8930 if (gfc_is_coindexed (lhs))
8931 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8932 }
8933
8934 /* F03:7.4.1.2. */
8935 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8936 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8937 if (lhs->ts.type == BT_CLASS)
8938 {
8939 gfc_error ("Variable must not be polymorphic in assignment at %L",
8940 &lhs->where);
8941 return false;
8942 }
8943
8944 /* F2008, Section 7.2.1.2. */
8945 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8946 {
8947 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8948 "component in assignment at %L", &lhs->where);
8949 return false;
8950 }
8951
8952 gfc_check_assign (lhs, rhs, 1);
8953 return false;
8954 }
8955
8956
8957 /* Given a block of code, recursively resolve everything pointed to by this
8958 code block. */
8959
8960 static void
8961 resolve_code (gfc_code *code, gfc_namespace *ns)
8962 {
8963 int omp_workshare_save;
8964 int forall_save;
8965 code_stack frame;
8966 gfc_try t;
8967
8968 frame.prev = cs_base;
8969 frame.head = code;
8970 cs_base = &frame;
8971
8972 find_reachable_labels (code);
8973
8974 for (; code; code = code->next)
8975 {
8976 frame.current = code;
8977 forall_save = forall_flag;
8978
8979 if (code->op == EXEC_FORALL)
8980 {
8981 forall_flag = 1;
8982 gfc_resolve_forall (code, ns, forall_save);
8983 forall_flag = 2;
8984 }
8985 else if (code->block)
8986 {
8987 omp_workshare_save = -1;
8988 switch (code->op)
8989 {
8990 case EXEC_OMP_PARALLEL_WORKSHARE:
8991 omp_workshare_save = omp_workshare_flag;
8992 omp_workshare_flag = 1;
8993 gfc_resolve_omp_parallel_blocks (code, ns);
8994 break;
8995 case EXEC_OMP_PARALLEL:
8996 case EXEC_OMP_PARALLEL_DO:
8997 case EXEC_OMP_PARALLEL_SECTIONS:
8998 case EXEC_OMP_TASK:
8999 omp_workshare_save = omp_workshare_flag;
9000 omp_workshare_flag = 0;
9001 gfc_resolve_omp_parallel_blocks (code, ns);
9002 break;
9003 case EXEC_OMP_DO:
9004 gfc_resolve_omp_do_blocks (code, ns);
9005 break;
9006 case EXEC_SELECT_TYPE:
9007 /* Blocks are handled in resolve_select_type because we have
9008 to transform the SELECT TYPE into ASSOCIATE first. */
9009 break;
9010 case EXEC_OMP_WORKSHARE:
9011 omp_workshare_save = omp_workshare_flag;
9012 omp_workshare_flag = 1;
9013 /* FALLTHROUGH */
9014 default:
9015 gfc_resolve_blocks (code->block, ns);
9016 break;
9017 }
9018
9019 if (omp_workshare_save != -1)
9020 omp_workshare_flag = omp_workshare_save;
9021 }
9022
9023 t = SUCCESS;
9024 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9025 t = gfc_resolve_expr (code->expr1);
9026 forall_flag = forall_save;
9027
9028 if (gfc_resolve_expr (code->expr2) == FAILURE)
9029 t = FAILURE;
9030
9031 if (code->op == EXEC_ALLOCATE
9032 && gfc_resolve_expr (code->expr3) == FAILURE)
9033 t = FAILURE;
9034
9035 switch (code->op)
9036 {
9037 case EXEC_NOP:
9038 case EXEC_END_BLOCK:
9039 case EXEC_CYCLE:
9040 case EXEC_PAUSE:
9041 case EXEC_STOP:
9042 case EXEC_ERROR_STOP:
9043 case EXEC_EXIT:
9044 case EXEC_CONTINUE:
9045 case EXEC_DT_END:
9046 case EXEC_ASSIGN_CALL:
9047 case EXEC_CRITICAL:
9048 break;
9049
9050 case EXEC_SYNC_ALL:
9051 case EXEC_SYNC_IMAGES:
9052 case EXEC_SYNC_MEMORY:
9053 resolve_sync (code);
9054 break;
9055
9056 case EXEC_ENTRY:
9057 /* Keep track of which entry we are up to. */
9058 current_entry_id = code->ext.entry->id;
9059 break;
9060
9061 case EXEC_WHERE:
9062 resolve_where (code, NULL);
9063 break;
9064
9065 case EXEC_GOTO:
9066 if (code->expr1 != NULL)
9067 {
9068 if (code->expr1->ts.type != BT_INTEGER)
9069 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9070 "INTEGER variable", &code->expr1->where);
9071 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9072 gfc_error ("Variable '%s' has not been assigned a target "
9073 "label at %L", code->expr1->symtree->n.sym->name,
9074 &code->expr1->where);
9075 }
9076 else
9077 resolve_branch (code->label1, code);
9078 break;
9079
9080 case EXEC_RETURN:
9081 if (code->expr1 != NULL
9082 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9083 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9084 "INTEGER return specifier", &code->expr1->where);
9085 break;
9086
9087 case EXEC_INIT_ASSIGN:
9088 case EXEC_END_PROCEDURE:
9089 break;
9090
9091 case EXEC_ASSIGN:
9092 if (t == FAILURE)
9093 break;
9094
9095 if (gfc_check_vardef_context (code->expr1, false, _("assignment"))
9096 == FAILURE)
9097 break;
9098
9099 if (resolve_ordinary_assign (code, ns))
9100 {
9101 if (code->op == EXEC_COMPCALL)
9102 goto compcall;
9103 else
9104 goto call;
9105 }
9106 break;
9107
9108 case EXEC_LABEL_ASSIGN:
9109 if (code->label1->defined == ST_LABEL_UNKNOWN)
9110 gfc_error ("Label %d referenced at %L is never defined",
9111 code->label1->value, &code->label1->where);
9112 if (t == SUCCESS
9113 && (code->expr1->expr_type != EXPR_VARIABLE
9114 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9115 || code->expr1->symtree->n.sym->ts.kind
9116 != gfc_default_integer_kind
9117 || code->expr1->symtree->n.sym->as != NULL))
9118 gfc_error ("ASSIGN statement at %L requires a scalar "
9119 "default INTEGER variable", &code->expr1->where);
9120 break;
9121
9122 case EXEC_POINTER_ASSIGN:
9123 {
9124 gfc_expr* e;
9125
9126 if (t == FAILURE)
9127 break;
9128
9129 /* This is both a variable definition and pointer assignment
9130 context, so check both of them. For rank remapping, a final
9131 array ref may be present on the LHS and fool gfc_expr_attr
9132 used in gfc_check_vardef_context. Remove it. */
9133 e = remove_last_array_ref (code->expr1);
9134 t = gfc_check_vardef_context (e, true, _("pointer assignment"));
9135 if (t == SUCCESS)
9136 t = gfc_check_vardef_context (e, false, _("pointer assignment"));
9137 gfc_free_expr (e);
9138 if (t == FAILURE)
9139 break;
9140
9141 gfc_check_pointer_assign (code->expr1, code->expr2);
9142 break;
9143 }
9144
9145 case EXEC_ARITHMETIC_IF:
9146 if (t == SUCCESS
9147 && code->expr1->ts.type != BT_INTEGER
9148 && code->expr1->ts.type != BT_REAL)
9149 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9150 "expression", &code->expr1->where);
9151
9152 resolve_branch (code->label1, code);
9153 resolve_branch (code->label2, code);
9154 resolve_branch (code->label3, code);
9155 break;
9156
9157 case EXEC_IF:
9158 if (t == SUCCESS && code->expr1 != NULL
9159 && (code->expr1->ts.type != BT_LOGICAL
9160 || code->expr1->rank != 0))
9161 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9162 &code->expr1->where);
9163 break;
9164
9165 case EXEC_CALL:
9166 call:
9167 resolve_call (code);
9168 break;
9169
9170 case EXEC_COMPCALL:
9171 compcall:
9172 resolve_typebound_subroutine (code);
9173 break;
9174
9175 case EXEC_CALL_PPC:
9176 resolve_ppc_call (code);
9177 break;
9178
9179 case EXEC_SELECT:
9180 /* Select is complicated. Also, a SELECT construct could be
9181 a transformed computed GOTO. */
9182 resolve_select (code);
9183 break;
9184
9185 case EXEC_SELECT_TYPE:
9186 resolve_select_type (code, ns);
9187 break;
9188
9189 case EXEC_BLOCK:
9190 resolve_block_construct (code);
9191 break;
9192
9193 case EXEC_DO:
9194 if (code->ext.iterator != NULL)
9195 {
9196 gfc_iterator *iter = code->ext.iterator;
9197 if (gfc_resolve_iterator (iter, true) != FAILURE)
9198 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9199 }
9200 break;
9201
9202 case EXEC_DO_WHILE:
9203 if (code->expr1 == NULL)
9204 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9205 if (t == SUCCESS
9206 && (code->expr1->rank != 0
9207 || code->expr1->ts.type != BT_LOGICAL))
9208 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9209 "a scalar LOGICAL expression", &code->expr1->where);
9210 break;
9211
9212 case EXEC_ALLOCATE:
9213 if (t == SUCCESS)
9214 resolve_allocate_deallocate (code, "ALLOCATE");
9215
9216 break;
9217
9218 case EXEC_DEALLOCATE:
9219 if (t == SUCCESS)
9220 resolve_allocate_deallocate (code, "DEALLOCATE");
9221
9222 break;
9223
9224 case EXEC_OPEN:
9225 if (gfc_resolve_open (code->ext.open) == FAILURE)
9226 break;
9227
9228 resolve_branch (code->ext.open->err, code);
9229 break;
9230
9231 case EXEC_CLOSE:
9232 if (gfc_resolve_close (code->ext.close) == FAILURE)
9233 break;
9234
9235 resolve_branch (code->ext.close->err, code);
9236 break;
9237
9238 case EXEC_BACKSPACE:
9239 case EXEC_ENDFILE:
9240 case EXEC_REWIND:
9241 case EXEC_FLUSH:
9242 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9243 break;
9244
9245 resolve_branch (code->ext.filepos->err, code);
9246 break;
9247
9248 case EXEC_INQUIRE:
9249 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9250 break;
9251
9252 resolve_branch (code->ext.inquire->err, code);
9253 break;
9254
9255 case EXEC_IOLENGTH:
9256 gcc_assert (code->ext.inquire != NULL);
9257 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9258 break;
9259
9260 resolve_branch (code->ext.inquire->err, code);
9261 break;
9262
9263 case EXEC_WAIT:
9264 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9265 break;
9266
9267 resolve_branch (code->ext.wait->err, code);
9268 resolve_branch (code->ext.wait->end, code);
9269 resolve_branch (code->ext.wait->eor, code);
9270 break;
9271
9272 case EXEC_READ:
9273 case EXEC_WRITE:
9274 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9275 break;
9276
9277 resolve_branch (code->ext.dt->err, code);
9278 resolve_branch (code->ext.dt->end, code);
9279 resolve_branch (code->ext.dt->eor, code);
9280 break;
9281
9282 case EXEC_TRANSFER:
9283 resolve_transfer (code);
9284 break;
9285
9286 case EXEC_FORALL:
9287 resolve_forall_iterators (code->ext.forall_iterator);
9288
9289 if (code->expr1 != NULL
9290 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9291 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9292 "expression", &code->expr1->where);
9293 break;
9294
9295 case EXEC_OMP_ATOMIC:
9296 case EXEC_OMP_BARRIER:
9297 case EXEC_OMP_CRITICAL:
9298 case EXEC_OMP_FLUSH:
9299 case EXEC_OMP_DO:
9300 case EXEC_OMP_MASTER:
9301 case EXEC_OMP_ORDERED:
9302 case EXEC_OMP_SECTIONS:
9303 case EXEC_OMP_SINGLE:
9304 case EXEC_OMP_TASKWAIT:
9305 case EXEC_OMP_WORKSHARE:
9306 gfc_resolve_omp_directive (code, ns);
9307 break;
9308
9309 case EXEC_OMP_PARALLEL:
9310 case EXEC_OMP_PARALLEL_DO:
9311 case EXEC_OMP_PARALLEL_SECTIONS:
9312 case EXEC_OMP_PARALLEL_WORKSHARE:
9313 case EXEC_OMP_TASK:
9314 omp_workshare_save = omp_workshare_flag;
9315 omp_workshare_flag = 0;
9316 gfc_resolve_omp_directive (code, ns);
9317 omp_workshare_flag = omp_workshare_save;
9318 break;
9319
9320 default:
9321 gfc_internal_error ("resolve_code(): Bad statement code");
9322 }
9323 }
9324
9325 cs_base = frame.prev;
9326 }
9327
9328
9329 /* Resolve initial values and make sure they are compatible with
9330 the variable. */
9331
9332 static void
9333 resolve_values (gfc_symbol *sym)
9334 {
9335 gfc_try t;
9336
9337 if (sym->value == NULL)
9338 return;
9339
9340 if (sym->value->expr_type == EXPR_STRUCTURE)
9341 t= resolve_structure_cons (sym->value, 1);
9342 else
9343 t = gfc_resolve_expr (sym->value);
9344
9345 if (t == FAILURE)
9346 return;
9347
9348 gfc_check_assign_symbol (sym, sym->value);
9349 }
9350
9351
9352 /* Verify the binding labels for common blocks that are BIND(C). The label
9353 for a BIND(C) common block must be identical in all scoping units in which
9354 the common block is declared. Further, the binding label can not collide
9355 with any other global entity in the program. */
9356
9357 static void
9358 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9359 {
9360 if (comm_block_tree->n.common->is_bind_c == 1)
9361 {
9362 gfc_gsymbol *binding_label_gsym;
9363 gfc_gsymbol *comm_name_gsym;
9364
9365 /* See if a global symbol exists by the common block's name. It may
9366 be NULL if the common block is use-associated. */
9367 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9368 comm_block_tree->n.common->name);
9369 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9370 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9371 "with the global entity '%s' at %L",
9372 comm_block_tree->n.common->binding_label,
9373 comm_block_tree->n.common->name,
9374 &(comm_block_tree->n.common->where),
9375 comm_name_gsym->name, &(comm_name_gsym->where));
9376 else if (comm_name_gsym != NULL
9377 && strcmp (comm_name_gsym->name,
9378 comm_block_tree->n.common->name) == 0)
9379 {
9380 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9381 as expected. */
9382 if (comm_name_gsym->binding_label == NULL)
9383 /* No binding label for common block stored yet; save this one. */
9384 comm_name_gsym->binding_label =
9385 comm_block_tree->n.common->binding_label;
9386 else
9387 if (strcmp (comm_name_gsym->binding_label,
9388 comm_block_tree->n.common->binding_label) != 0)
9389 {
9390 /* Common block names match but binding labels do not. */
9391 gfc_error ("Binding label '%s' for common block '%s' at %L "
9392 "does not match the binding label '%s' for common "
9393 "block '%s' at %L",
9394 comm_block_tree->n.common->binding_label,
9395 comm_block_tree->n.common->name,
9396 &(comm_block_tree->n.common->where),
9397 comm_name_gsym->binding_label,
9398 comm_name_gsym->name,
9399 &(comm_name_gsym->where));
9400 return;
9401 }
9402 }
9403
9404 /* There is no binding label (NAME="") so we have nothing further to
9405 check and nothing to add as a global symbol for the label. */
9406 if (comm_block_tree->n.common->binding_label[0] == '\0' )
9407 return;
9408
9409 binding_label_gsym =
9410 gfc_find_gsymbol (gfc_gsym_root,
9411 comm_block_tree->n.common->binding_label);
9412 if (binding_label_gsym == NULL)
9413 {
9414 /* Need to make a global symbol for the binding label to prevent
9415 it from colliding with another. */
9416 binding_label_gsym =
9417 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9418 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9419 binding_label_gsym->type = GSYM_COMMON;
9420 }
9421 else
9422 {
9423 /* If comm_name_gsym is NULL, the name common block is use
9424 associated and the name could be colliding. */
9425 if (binding_label_gsym->type != GSYM_COMMON)
9426 gfc_error ("Binding label '%s' for common block '%s' at %L "
9427 "collides with the global entity '%s' at %L",
9428 comm_block_tree->n.common->binding_label,
9429 comm_block_tree->n.common->name,
9430 &(comm_block_tree->n.common->where),
9431 binding_label_gsym->name,
9432 &(binding_label_gsym->where));
9433 else if (comm_name_gsym != NULL
9434 && (strcmp (binding_label_gsym->name,
9435 comm_name_gsym->binding_label) != 0)
9436 && (strcmp (binding_label_gsym->sym_name,
9437 comm_name_gsym->name) != 0))
9438 gfc_error ("Binding label '%s' for common block '%s' at %L "
9439 "collides with global entity '%s' at %L",
9440 binding_label_gsym->name, binding_label_gsym->sym_name,
9441 &(comm_block_tree->n.common->where),
9442 comm_name_gsym->name, &(comm_name_gsym->where));
9443 }
9444 }
9445
9446 return;
9447 }
9448
9449
9450 /* Verify any BIND(C) derived types in the namespace so we can report errors
9451 for them once, rather than for each variable declared of that type. */
9452
9453 static void
9454 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9455 {
9456 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9457 && derived_sym->attr.is_bind_c == 1)
9458 verify_bind_c_derived_type (derived_sym);
9459
9460 return;
9461 }
9462
9463
9464 /* Verify that any binding labels used in a given namespace do not collide
9465 with the names or binding labels of any global symbols. */
9466
9467 static void
9468 gfc_verify_binding_labels (gfc_symbol *sym)
9469 {
9470 int has_error = 0;
9471
9472 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9473 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
9474 {
9475 gfc_gsymbol *bind_c_sym;
9476
9477 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9478 if (bind_c_sym != NULL
9479 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9480 {
9481 if (sym->attr.if_source == IFSRC_DECL
9482 && (bind_c_sym->type != GSYM_SUBROUTINE
9483 && bind_c_sym->type != GSYM_FUNCTION)
9484 && ((sym->attr.contained == 1
9485 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9486 || (sym->attr.use_assoc == 1
9487 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9488 {
9489 /* Make sure global procedures don't collide with anything. */
9490 gfc_error ("Binding label '%s' at %L collides with the global "
9491 "entity '%s' at %L", sym->binding_label,
9492 &(sym->declared_at), bind_c_sym->name,
9493 &(bind_c_sym->where));
9494 has_error = 1;
9495 }
9496 else if (sym->attr.contained == 0
9497 && (sym->attr.if_source == IFSRC_IFBODY
9498 && sym->attr.flavor == FL_PROCEDURE)
9499 && (bind_c_sym->sym_name != NULL
9500 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9501 {
9502 /* Make sure procedures in interface bodies don't collide. */
9503 gfc_error ("Binding label '%s' in interface body at %L collides "
9504 "with the global entity '%s' at %L",
9505 sym->binding_label,
9506 &(sym->declared_at), bind_c_sym->name,
9507 &(bind_c_sym->where));
9508 has_error = 1;
9509 }
9510 else if (sym->attr.contained == 0
9511 && sym->attr.if_source == IFSRC_UNKNOWN)
9512 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9513 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9514 || sym->attr.use_assoc == 0)
9515 {
9516 gfc_error ("Binding label '%s' at %L collides with global "
9517 "entity '%s' at %L", sym->binding_label,
9518 &(sym->declared_at), bind_c_sym->name,
9519 &(bind_c_sym->where));
9520 has_error = 1;
9521 }
9522
9523 if (has_error != 0)
9524 /* Clear the binding label to prevent checking multiple times. */
9525 sym->binding_label[0] = '\0';
9526 }
9527 else if (bind_c_sym == NULL)
9528 {
9529 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9530 bind_c_sym->where = sym->declared_at;
9531 bind_c_sym->sym_name = sym->name;
9532
9533 if (sym->attr.use_assoc == 1)
9534 bind_c_sym->mod_name = sym->module;
9535 else
9536 if (sym->ns->proc_name != NULL)
9537 bind_c_sym->mod_name = sym->ns->proc_name->name;
9538
9539 if (sym->attr.contained == 0)
9540 {
9541 if (sym->attr.subroutine)
9542 bind_c_sym->type = GSYM_SUBROUTINE;
9543 else if (sym->attr.function)
9544 bind_c_sym->type = GSYM_FUNCTION;
9545 }
9546 }
9547 }
9548 return;
9549 }
9550
9551
9552 /* Resolve an index expression. */
9553
9554 static gfc_try
9555 resolve_index_expr (gfc_expr *e)
9556 {
9557 if (gfc_resolve_expr (e) == FAILURE)
9558 return FAILURE;
9559
9560 if (gfc_simplify_expr (e, 0) == FAILURE)
9561 return FAILURE;
9562
9563 if (gfc_specification_expr (e) == FAILURE)
9564 return FAILURE;
9565
9566 return SUCCESS;
9567 }
9568
9569
9570 /* Resolve a charlen structure. */
9571
9572 static gfc_try
9573 resolve_charlen (gfc_charlen *cl)
9574 {
9575 int i, k;
9576
9577 if (cl->resolved)
9578 return SUCCESS;
9579
9580 cl->resolved = 1;
9581
9582 specification_expr = 1;
9583
9584 if (resolve_index_expr (cl->length) == FAILURE)
9585 {
9586 specification_expr = 0;
9587 return FAILURE;
9588 }
9589
9590 /* "If the character length parameter value evaluates to a negative
9591 value, the length of character entities declared is zero." */
9592 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9593 {
9594 if (gfc_option.warn_surprising)
9595 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9596 " the length has been set to zero",
9597 &cl->length->where, i);
9598 gfc_replace_expr (cl->length,
9599 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9600 }
9601
9602 /* Check that the character length is not too large. */
9603 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9604 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9605 && cl->length->ts.type == BT_INTEGER
9606 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9607 {
9608 gfc_error ("String length at %L is too large", &cl->length->where);
9609 return FAILURE;
9610 }
9611
9612 return SUCCESS;
9613 }
9614
9615
9616 /* Test for non-constant shape arrays. */
9617
9618 static bool
9619 is_non_constant_shape_array (gfc_symbol *sym)
9620 {
9621 gfc_expr *e;
9622 int i;
9623 bool not_constant;
9624
9625 not_constant = false;
9626 if (sym->as != NULL)
9627 {
9628 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9629 has not been simplified; parameter array references. Do the
9630 simplification now. */
9631 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9632 {
9633 e = sym->as->lower[i];
9634 if (e && (resolve_index_expr (e) == FAILURE
9635 || !gfc_is_constant_expr (e)))
9636 not_constant = true;
9637 e = sym->as->upper[i];
9638 if (e && (resolve_index_expr (e) == FAILURE
9639 || !gfc_is_constant_expr (e)))
9640 not_constant = true;
9641 }
9642 }
9643 return not_constant;
9644 }
9645
9646 /* Given a symbol and an initialization expression, add code to initialize
9647 the symbol to the function entry. */
9648 static void
9649 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9650 {
9651 gfc_expr *lval;
9652 gfc_code *init_st;
9653 gfc_namespace *ns = sym->ns;
9654
9655 /* Search for the function namespace if this is a contained
9656 function without an explicit result. */
9657 if (sym->attr.function && sym == sym->result
9658 && sym->name != sym->ns->proc_name->name)
9659 {
9660 ns = ns->contained;
9661 for (;ns; ns = ns->sibling)
9662 if (strcmp (ns->proc_name->name, sym->name) == 0)
9663 break;
9664 }
9665
9666 if (ns == NULL)
9667 {
9668 gfc_free_expr (init);
9669 return;
9670 }
9671
9672 /* Build an l-value expression for the result. */
9673 lval = gfc_lval_expr_from_sym (sym);
9674
9675 /* Add the code at scope entry. */
9676 init_st = gfc_get_code ();
9677 init_st->next = ns->code;
9678 ns->code = init_st;
9679
9680 /* Assign the default initializer to the l-value. */
9681 init_st->loc = sym->declared_at;
9682 init_st->op = EXEC_INIT_ASSIGN;
9683 init_st->expr1 = lval;
9684 init_st->expr2 = init;
9685 }
9686
9687 /* Assign the default initializer to a derived type variable or result. */
9688
9689 static void
9690 apply_default_init (gfc_symbol *sym)
9691 {
9692 gfc_expr *init = NULL;
9693
9694 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9695 return;
9696
9697 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9698 init = gfc_default_initializer (&sym->ts);
9699
9700 if (init == NULL && sym->ts.type != BT_CLASS)
9701 return;
9702
9703 build_init_assign (sym, init);
9704 sym->attr.referenced = 1;
9705 }
9706
9707 /* Build an initializer for a local integer, real, complex, logical, or
9708 character variable, based on the command line flags finit-local-zero,
9709 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9710 null if the symbol should not have a default initialization. */
9711 static gfc_expr *
9712 build_default_init_expr (gfc_symbol *sym)
9713 {
9714 int char_len;
9715 gfc_expr *init_expr;
9716 int i;
9717
9718 /* These symbols should never have a default initialization. */
9719 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9720 || sym->attr.external
9721 || sym->attr.dummy
9722 || sym->attr.pointer
9723 || sym->attr.in_equivalence
9724 || sym->attr.in_common
9725 || sym->attr.data
9726 || sym->module
9727 || sym->attr.cray_pointee
9728 || sym->attr.cray_pointer)
9729 return NULL;
9730
9731 /* Now we'll try to build an initializer expression. */
9732 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9733 &sym->declared_at);
9734
9735 /* We will only initialize integers, reals, complex, logicals, and
9736 characters, and only if the corresponding command-line flags
9737 were set. Otherwise, we free init_expr and return null. */
9738 switch (sym->ts.type)
9739 {
9740 case BT_INTEGER:
9741 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9742 mpz_set_si (init_expr->value.integer,
9743 gfc_option.flag_init_integer_value);
9744 else
9745 {
9746 gfc_free_expr (init_expr);
9747 init_expr = NULL;
9748 }
9749 break;
9750
9751 case BT_REAL:
9752 switch (gfc_option.flag_init_real)
9753 {
9754 case GFC_INIT_REAL_SNAN:
9755 init_expr->is_snan = 1;
9756 /* Fall through. */
9757 case GFC_INIT_REAL_NAN:
9758 mpfr_set_nan (init_expr->value.real);
9759 break;
9760
9761 case GFC_INIT_REAL_INF:
9762 mpfr_set_inf (init_expr->value.real, 1);
9763 break;
9764
9765 case GFC_INIT_REAL_NEG_INF:
9766 mpfr_set_inf (init_expr->value.real, -1);
9767 break;
9768
9769 case GFC_INIT_REAL_ZERO:
9770 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9771 break;
9772
9773 default:
9774 gfc_free_expr (init_expr);
9775 init_expr = NULL;
9776 break;
9777 }
9778 break;
9779
9780 case BT_COMPLEX:
9781 switch (gfc_option.flag_init_real)
9782 {
9783 case GFC_INIT_REAL_SNAN:
9784 init_expr->is_snan = 1;
9785 /* Fall through. */
9786 case GFC_INIT_REAL_NAN:
9787 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9788 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9789 break;
9790
9791 case GFC_INIT_REAL_INF:
9792 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9793 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9794 break;
9795
9796 case GFC_INIT_REAL_NEG_INF:
9797 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9798 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9799 break;
9800
9801 case GFC_INIT_REAL_ZERO:
9802 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9803 break;
9804
9805 default:
9806 gfc_free_expr (init_expr);
9807 init_expr = NULL;
9808 break;
9809 }
9810 break;
9811
9812 case BT_LOGICAL:
9813 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9814 init_expr->value.logical = 0;
9815 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9816 init_expr->value.logical = 1;
9817 else
9818 {
9819 gfc_free_expr (init_expr);
9820 init_expr = NULL;
9821 }
9822 break;
9823
9824 case BT_CHARACTER:
9825 /* For characters, the length must be constant in order to
9826 create a default initializer. */
9827 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9828 && sym->ts.u.cl->length
9829 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9830 {
9831 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9832 init_expr->value.character.length = char_len;
9833 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9834 for (i = 0; i < char_len; i++)
9835 init_expr->value.character.string[i]
9836 = (unsigned char) gfc_option.flag_init_character_value;
9837 }
9838 else
9839 {
9840 gfc_free_expr (init_expr);
9841 init_expr = NULL;
9842 }
9843 break;
9844
9845 default:
9846 gfc_free_expr (init_expr);
9847 init_expr = NULL;
9848 }
9849 return init_expr;
9850 }
9851
9852 /* Add an initialization expression to a local variable. */
9853 static void
9854 apply_default_init_local (gfc_symbol *sym)
9855 {
9856 gfc_expr *init = NULL;
9857
9858 /* The symbol should be a variable or a function return value. */
9859 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9860 || (sym->attr.function && sym->result != sym))
9861 return;
9862
9863 /* Try to build the initializer expression. If we can't initialize
9864 this symbol, then init will be NULL. */
9865 init = build_default_init_expr (sym);
9866 if (init == NULL)
9867 return;
9868
9869 /* For saved variables, we don't want to add an initializer at
9870 function entry, so we just add a static initializer. */
9871 if (sym->attr.save || sym->ns->save_all
9872 || gfc_option.flag_max_stack_var_size == 0)
9873 {
9874 /* Don't clobber an existing initializer! */
9875 gcc_assert (sym->value == NULL);
9876 sym->value = init;
9877 return;
9878 }
9879
9880 build_init_assign (sym, init);
9881 }
9882
9883
9884 /* Resolution of common features of flavors variable and procedure. */
9885
9886 static gfc_try
9887 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9888 {
9889 /* Avoid double diagnostics for function result symbols. */
9890 if ((sym->result || sym->attr.result) && !sym->attr.dummy
9891 && (sym->ns != gfc_current_ns))
9892 return SUCCESS;
9893
9894 /* Constraints on deferred shape variable. */
9895 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9896 {
9897 if (sym->attr.allocatable)
9898 {
9899 if (sym->attr.dimension)
9900 {
9901 gfc_error ("Allocatable array '%s' at %L must have "
9902 "a deferred shape", sym->name, &sym->declared_at);
9903 return FAILURE;
9904 }
9905 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9906 "may not be ALLOCATABLE", sym->name,
9907 &sym->declared_at) == FAILURE)
9908 return FAILURE;
9909 }
9910
9911 if (sym->attr.pointer && sym->attr.dimension)
9912 {
9913 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9914 sym->name, &sym->declared_at);
9915 return FAILURE;
9916 }
9917 }
9918 else
9919 {
9920 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9921 && !sym->attr.dummy && sym->ts.type != BT_CLASS && !sym->assoc)
9922 {
9923 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9924 sym->name, &sym->declared_at);
9925 return FAILURE;
9926 }
9927 }
9928
9929 /* Constraints on polymorphic variables. */
9930 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
9931 {
9932 /* F03:C502. */
9933 if (sym->attr.class_ok
9934 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
9935 {
9936 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9937 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
9938 &sym->declared_at);
9939 return FAILURE;
9940 }
9941
9942 /* F03:C509. */
9943 /* Assume that use associated symbols were checked in the module ns.
9944 Class-variables that are associate-names are also something special
9945 and excepted from the test. */
9946 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
9947 {
9948 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9949 "or pointer", sym->name, &sym->declared_at);
9950 return FAILURE;
9951 }
9952 }
9953
9954 return SUCCESS;
9955 }
9956
9957
9958 /* Additional checks for symbols with flavor variable and derived
9959 type. To be called from resolve_fl_variable. */
9960
9961 static gfc_try
9962 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9963 {
9964 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9965
9966 /* Check to see if a derived type is blocked from being host
9967 associated by the presence of another class I symbol in the same
9968 namespace. 14.6.1.3 of the standard and the discussion on
9969 comp.lang.fortran. */
9970 if (sym->ns != sym->ts.u.derived->ns
9971 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9972 {
9973 gfc_symbol *s;
9974 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9975 if (s && s->attr.flavor != FL_DERIVED)
9976 {
9977 gfc_error ("The type '%s' cannot be host associated at %L "
9978 "because it is blocked by an incompatible object "
9979 "of the same name declared at %L",
9980 sym->ts.u.derived->name, &sym->declared_at,
9981 &s->declared_at);
9982 return FAILURE;
9983 }
9984 }
9985
9986 /* 4th constraint in section 11.3: "If an object of a type for which
9987 component-initialization is specified (R429) appears in the
9988 specification-part of a module and does not have the ALLOCATABLE
9989 or POINTER attribute, the object shall have the SAVE attribute."
9990
9991 The check for initializers is performed with
9992 gfc_has_default_initializer because gfc_default_initializer generates
9993 a hidden default for allocatable components. */
9994 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9995 && sym->ns->proc_name->attr.flavor == FL_MODULE
9996 && !sym->ns->save_all && !sym->attr.save
9997 && !sym->attr.pointer && !sym->attr.allocatable
9998 && gfc_has_default_initializer (sym->ts.u.derived)
9999 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
10000 "module variable '%s' at %L, needed due to "
10001 "the default initialization", sym->name,
10002 &sym->declared_at) == FAILURE)
10003 return FAILURE;
10004
10005 /* Assign default initializer. */
10006 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10007 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10008 {
10009 sym->value = gfc_default_initializer (&sym->ts);
10010 }
10011
10012 return SUCCESS;
10013 }
10014
10015
10016 /* Resolve symbols with flavor variable. */
10017
10018 static gfc_try
10019 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10020 {
10021 int no_init_flag, automatic_flag;
10022 gfc_expr *e;
10023 const char *auto_save_msg;
10024
10025 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10026 "SAVE attribute";
10027
10028 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10029 return FAILURE;
10030
10031 /* Set this flag to check that variables are parameters of all entries.
10032 This check is effected by the call to gfc_resolve_expr through
10033 is_non_constant_shape_array. */
10034 specification_expr = 1;
10035
10036 if (sym->ns->proc_name
10037 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10038 || sym->ns->proc_name->attr.is_main_program)
10039 && !sym->attr.use_assoc
10040 && !sym->attr.allocatable
10041 && !sym->attr.pointer
10042 && is_non_constant_shape_array (sym))
10043 {
10044 /* The shape of a main program or module array needs to be
10045 constant. */
10046 gfc_error ("The module or main program array '%s' at %L must "
10047 "have constant shape", sym->name, &sym->declared_at);
10048 specification_expr = 0;
10049 return FAILURE;
10050 }
10051
10052 /* Constraints on deferred type parameter. */
10053 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10054 {
10055 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10056 "requires either the pointer or allocatable attribute",
10057 sym->name, &sym->declared_at);
10058 return FAILURE;
10059 }
10060
10061 if (sym->ts.type == BT_CHARACTER)
10062 {
10063 /* Make sure that character string variables with assumed length are
10064 dummy arguments. */
10065 e = sym->ts.u.cl->length;
10066 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10067 && !sym->ts.deferred)
10068 {
10069 gfc_error ("Entity with assumed character length at %L must be a "
10070 "dummy argument or a PARAMETER", &sym->declared_at);
10071 return FAILURE;
10072 }
10073
10074 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10075 {
10076 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10077 return FAILURE;
10078 }
10079
10080 if (!gfc_is_constant_expr (e)
10081 && !(e->expr_type == EXPR_VARIABLE
10082 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
10083 && sym->ns->proc_name
10084 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10085 || sym->ns->proc_name->attr.is_main_program)
10086 && !sym->attr.use_assoc)
10087 {
10088 gfc_error ("'%s' at %L must have constant character length "
10089 "in this context", sym->name, &sym->declared_at);
10090 return FAILURE;
10091 }
10092 }
10093
10094 if (sym->value == NULL && sym->attr.referenced)
10095 apply_default_init_local (sym); /* Try to apply a default initialization. */
10096
10097 /* Determine if the symbol may not have an initializer. */
10098 no_init_flag = automatic_flag = 0;
10099 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10100 || sym->attr.intrinsic || sym->attr.result)
10101 no_init_flag = 1;
10102 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10103 && is_non_constant_shape_array (sym))
10104 {
10105 no_init_flag = automatic_flag = 1;
10106
10107 /* Also, they must not have the SAVE attribute.
10108 SAVE_IMPLICIT is checked below. */
10109 if (sym->attr.save == SAVE_EXPLICIT)
10110 {
10111 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10112 return FAILURE;
10113 }
10114 }
10115
10116 /* Ensure that any initializer is simplified. */
10117 if (sym->value)
10118 gfc_simplify_expr (sym->value, 1);
10119
10120 /* Reject illegal initializers. */
10121 if (!sym->mark && sym->value)
10122 {
10123 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10124 && CLASS_DATA (sym)->attr.allocatable))
10125 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10126 sym->name, &sym->declared_at);
10127 else if (sym->attr.external)
10128 gfc_error ("External '%s' at %L cannot have an initializer",
10129 sym->name, &sym->declared_at);
10130 else if (sym->attr.dummy
10131 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10132 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10133 sym->name, &sym->declared_at);
10134 else if (sym->attr.intrinsic)
10135 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10136 sym->name, &sym->declared_at);
10137 else if (sym->attr.result)
10138 gfc_error ("Function result '%s' at %L cannot have an initializer",
10139 sym->name, &sym->declared_at);
10140 else if (automatic_flag)
10141 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10142 sym->name, &sym->declared_at);
10143 else
10144 goto no_init_error;
10145 return FAILURE;
10146 }
10147
10148 no_init_error:
10149 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10150 return resolve_fl_variable_derived (sym, no_init_flag);
10151
10152 return SUCCESS;
10153 }
10154
10155
10156 /* Resolve a procedure. */
10157
10158 static gfc_try
10159 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10160 {
10161 gfc_formal_arglist *arg;
10162
10163 if (sym->attr.function
10164 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10165 return FAILURE;
10166
10167 if (sym->ts.type == BT_CHARACTER)
10168 {
10169 gfc_charlen *cl = sym->ts.u.cl;
10170
10171 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10172 && resolve_charlen (cl) == FAILURE)
10173 return FAILURE;
10174
10175 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10176 && sym->attr.proc == PROC_ST_FUNCTION)
10177 {
10178 gfc_error ("Character-valued statement function '%s' at %L must "
10179 "have constant length", sym->name, &sym->declared_at);
10180 return FAILURE;
10181 }
10182 }
10183
10184 /* Ensure that derived type for are not of a private type. Internal
10185 module procedures are excluded by 2.2.3.3 - i.e., they are not
10186 externally accessible and can access all the objects accessible in
10187 the host. */
10188 if (!(sym->ns->parent
10189 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10190 && gfc_check_symbol_access (sym))
10191 {
10192 gfc_interface *iface;
10193
10194 for (arg = sym->formal; arg; arg = arg->next)
10195 {
10196 if (arg->sym
10197 && arg->sym->ts.type == BT_DERIVED
10198 && !arg->sym->ts.u.derived->attr.use_assoc
10199 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10200 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
10201 "PRIVATE type and cannot be a dummy argument"
10202 " of '%s', which is PUBLIC at %L",
10203 arg->sym->name, sym->name, &sym->declared_at)
10204 == FAILURE)
10205 {
10206 /* Stop this message from recurring. */
10207 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10208 return FAILURE;
10209 }
10210 }
10211
10212 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10213 PRIVATE to the containing module. */
10214 for (iface = sym->generic; iface; iface = iface->next)
10215 {
10216 for (arg = iface->sym->formal; arg; arg = arg->next)
10217 {
10218 if (arg->sym
10219 && arg->sym->ts.type == BT_DERIVED
10220 && !arg->sym->ts.u.derived->attr.use_assoc
10221 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10222 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10223 "'%s' in PUBLIC interface '%s' at %L "
10224 "takes dummy arguments of '%s' which is "
10225 "PRIVATE", iface->sym->name, sym->name,
10226 &iface->sym->declared_at,
10227 gfc_typename (&arg->sym->ts)) == FAILURE)
10228 {
10229 /* Stop this message from recurring. */
10230 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10231 return FAILURE;
10232 }
10233 }
10234 }
10235
10236 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10237 PRIVATE to the containing module. */
10238 for (iface = sym->generic; iface; iface = iface->next)
10239 {
10240 for (arg = iface->sym->formal; arg; arg = arg->next)
10241 {
10242 if (arg->sym
10243 && arg->sym->ts.type == BT_DERIVED
10244 && !arg->sym->ts.u.derived->attr.use_assoc
10245 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10246 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10247 "'%s' in PUBLIC interface '%s' at %L "
10248 "takes dummy arguments of '%s' which is "
10249 "PRIVATE", iface->sym->name, sym->name,
10250 &iface->sym->declared_at,
10251 gfc_typename (&arg->sym->ts)) == FAILURE)
10252 {
10253 /* Stop this message from recurring. */
10254 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10255 return FAILURE;
10256 }
10257 }
10258 }
10259 }
10260
10261 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10262 && !sym->attr.proc_pointer)
10263 {
10264 gfc_error ("Function '%s' at %L cannot have an initializer",
10265 sym->name, &sym->declared_at);
10266 return FAILURE;
10267 }
10268
10269 /* An external symbol may not have an initializer because it is taken to be
10270 a procedure. Exception: Procedure Pointers. */
10271 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10272 {
10273 gfc_error ("External object '%s' at %L may not have an initializer",
10274 sym->name, &sym->declared_at);
10275 return FAILURE;
10276 }
10277
10278 /* An elemental function is required to return a scalar 12.7.1 */
10279 if (sym->attr.elemental && sym->attr.function && sym->as)
10280 {
10281 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10282 "result", sym->name, &sym->declared_at);
10283 /* Reset so that the error only occurs once. */
10284 sym->attr.elemental = 0;
10285 return FAILURE;
10286 }
10287
10288 if (sym->attr.proc == PROC_ST_FUNCTION
10289 && (sym->attr.allocatable || sym->attr.pointer))
10290 {
10291 gfc_error ("Statement function '%s' at %L may not have pointer or "
10292 "allocatable attribute", sym->name, &sym->declared_at);
10293 return FAILURE;
10294 }
10295
10296 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10297 char-len-param shall not be array-valued, pointer-valued, recursive
10298 or pure. ....snip... A character value of * may only be used in the
10299 following ways: (i) Dummy arg of procedure - dummy associates with
10300 actual length; (ii) To declare a named constant; or (iii) External
10301 function - but length must be declared in calling scoping unit. */
10302 if (sym->attr.function
10303 && sym->ts.type == BT_CHARACTER
10304 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10305 {
10306 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10307 || (sym->attr.recursive) || (sym->attr.pure))
10308 {
10309 if (sym->as && sym->as->rank)
10310 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10311 "array-valued", sym->name, &sym->declared_at);
10312
10313 if (sym->attr.pointer)
10314 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10315 "pointer-valued", sym->name, &sym->declared_at);
10316
10317 if (sym->attr.pure)
10318 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10319 "pure", sym->name, &sym->declared_at);
10320
10321 if (sym->attr.recursive)
10322 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10323 "recursive", sym->name, &sym->declared_at);
10324
10325 return FAILURE;
10326 }
10327
10328 /* Appendix B.2 of the standard. Contained functions give an
10329 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10330 character length is an F2003 feature. */
10331 if (!sym->attr.contained
10332 && gfc_current_form != FORM_FIXED
10333 && !sym->ts.deferred)
10334 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10335 "CHARACTER(*) function '%s' at %L",
10336 sym->name, &sym->declared_at);
10337 }
10338
10339 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10340 {
10341 gfc_formal_arglist *curr_arg;
10342 int has_non_interop_arg = 0;
10343
10344 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10345 sym->common_block) == FAILURE)
10346 {
10347 /* Clear these to prevent looking at them again if there was an
10348 error. */
10349 sym->attr.is_bind_c = 0;
10350 sym->attr.is_c_interop = 0;
10351 sym->ts.is_c_interop = 0;
10352 }
10353 else
10354 {
10355 /* So far, no errors have been found. */
10356 sym->attr.is_c_interop = 1;
10357 sym->ts.is_c_interop = 1;
10358 }
10359
10360 curr_arg = sym->formal;
10361 while (curr_arg != NULL)
10362 {
10363 /* Skip implicitly typed dummy args here. */
10364 if (curr_arg->sym->attr.implicit_type == 0)
10365 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
10366 /* If something is found to fail, record the fact so we
10367 can mark the symbol for the procedure as not being
10368 BIND(C) to try and prevent multiple errors being
10369 reported. */
10370 has_non_interop_arg = 1;
10371
10372 curr_arg = curr_arg->next;
10373 }
10374
10375 /* See if any of the arguments were not interoperable and if so, clear
10376 the procedure symbol to prevent duplicate error messages. */
10377 if (has_non_interop_arg != 0)
10378 {
10379 sym->attr.is_c_interop = 0;
10380 sym->ts.is_c_interop = 0;
10381 sym->attr.is_bind_c = 0;
10382 }
10383 }
10384
10385 if (!sym->attr.proc_pointer)
10386 {
10387 if (sym->attr.save == SAVE_EXPLICIT)
10388 {
10389 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10390 "in '%s' at %L", sym->name, &sym->declared_at);
10391 return FAILURE;
10392 }
10393 if (sym->attr.intent)
10394 {
10395 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10396 "in '%s' at %L", sym->name, &sym->declared_at);
10397 return FAILURE;
10398 }
10399 if (sym->attr.subroutine && sym->attr.result)
10400 {
10401 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10402 "in '%s' at %L", sym->name, &sym->declared_at);
10403 return FAILURE;
10404 }
10405 if (sym->attr.external && sym->attr.function
10406 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10407 || sym->attr.contained))
10408 {
10409 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10410 "in '%s' at %L", sym->name, &sym->declared_at);
10411 return FAILURE;
10412 }
10413 if (strcmp ("ppr@", sym->name) == 0)
10414 {
10415 gfc_error ("Procedure pointer result '%s' at %L "
10416 "is missing the pointer attribute",
10417 sym->ns->proc_name->name, &sym->declared_at);
10418 return FAILURE;
10419 }
10420 }
10421
10422 return SUCCESS;
10423 }
10424
10425
10426 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10427 been defined and we now know their defined arguments, check that they fulfill
10428 the requirements of the standard for procedures used as finalizers. */
10429
10430 static gfc_try
10431 gfc_resolve_finalizers (gfc_symbol* derived)
10432 {
10433 gfc_finalizer* list;
10434 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10435 gfc_try result = SUCCESS;
10436 bool seen_scalar = false;
10437
10438 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10439 return SUCCESS;
10440
10441 /* Walk over the list of finalizer-procedures, check them, and if any one
10442 does not fit in with the standard's definition, print an error and remove
10443 it from the list. */
10444 prev_link = &derived->f2k_derived->finalizers;
10445 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10446 {
10447 gfc_symbol* arg;
10448 gfc_finalizer* i;
10449 int my_rank;
10450
10451 /* Skip this finalizer if we already resolved it. */
10452 if (list->proc_tree)
10453 {
10454 prev_link = &(list->next);
10455 continue;
10456 }
10457
10458 /* Check this exists and is a SUBROUTINE. */
10459 if (!list->proc_sym->attr.subroutine)
10460 {
10461 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10462 list->proc_sym->name, &list->where);
10463 goto error;
10464 }
10465
10466 /* We should have exactly one argument. */
10467 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10468 {
10469 gfc_error ("FINAL procedure at %L must have exactly one argument",
10470 &list->where);
10471 goto error;
10472 }
10473 arg = list->proc_sym->formal->sym;
10474
10475 /* This argument must be of our type. */
10476 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10477 {
10478 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10479 &arg->declared_at, derived->name);
10480 goto error;
10481 }
10482
10483 /* It must neither be a pointer nor allocatable nor optional. */
10484 if (arg->attr.pointer)
10485 {
10486 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10487 &arg->declared_at);
10488 goto error;
10489 }
10490 if (arg->attr.allocatable)
10491 {
10492 gfc_error ("Argument of FINAL procedure at %L must not be"
10493 " ALLOCATABLE", &arg->declared_at);
10494 goto error;
10495 }
10496 if (arg->attr.optional)
10497 {
10498 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10499 &arg->declared_at);
10500 goto error;
10501 }
10502
10503 /* It must not be INTENT(OUT). */
10504 if (arg->attr.intent == INTENT_OUT)
10505 {
10506 gfc_error ("Argument of FINAL procedure at %L must not be"
10507 " INTENT(OUT)", &arg->declared_at);
10508 goto error;
10509 }
10510
10511 /* Warn if the procedure is non-scalar and not assumed shape. */
10512 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10513 && arg->as->type != AS_ASSUMED_SHAPE)
10514 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10515 " shape argument", &arg->declared_at);
10516
10517 /* Check that it does not match in kind and rank with a FINAL procedure
10518 defined earlier. To really loop over the *earlier* declarations,
10519 we need to walk the tail of the list as new ones were pushed at the
10520 front. */
10521 /* TODO: Handle kind parameters once they are implemented. */
10522 my_rank = (arg->as ? arg->as->rank : 0);
10523 for (i = list->next; i; i = i->next)
10524 {
10525 /* Argument list might be empty; that is an error signalled earlier,
10526 but we nevertheless continued resolving. */
10527 if (i->proc_sym->formal)
10528 {
10529 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10530 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10531 if (i_rank == my_rank)
10532 {
10533 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10534 " rank (%d) as '%s'",
10535 list->proc_sym->name, &list->where, my_rank,
10536 i->proc_sym->name);
10537 goto error;
10538 }
10539 }
10540 }
10541
10542 /* Is this the/a scalar finalizer procedure? */
10543 if (!arg->as || arg->as->rank == 0)
10544 seen_scalar = true;
10545
10546 /* Find the symtree for this procedure. */
10547 gcc_assert (!list->proc_tree);
10548 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10549
10550 prev_link = &list->next;
10551 continue;
10552
10553 /* Remove wrong nodes immediately from the list so we don't risk any
10554 troubles in the future when they might fail later expectations. */
10555 error:
10556 result = FAILURE;
10557 i = list;
10558 *prev_link = list->next;
10559 gfc_free_finalizer (i);
10560 }
10561
10562 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10563 were nodes in the list, must have been for arrays. It is surely a good
10564 idea to have a scalar version there if there's something to finalize. */
10565 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10566 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10567 " defined at %L, suggest also scalar one",
10568 derived->name, &derived->declared_at);
10569
10570 /* TODO: Remove this error when finalization is finished. */
10571 gfc_error ("Finalization at %L is not yet implemented",
10572 &derived->declared_at);
10573
10574 return result;
10575 }
10576
10577
10578 /* Check that it is ok for the typebound procedure proc to override the
10579 procedure old. */
10580
10581 static gfc_try
10582 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
10583 {
10584 locus where;
10585 const gfc_symbol* proc_target;
10586 const gfc_symbol* old_target;
10587 unsigned proc_pass_arg, old_pass_arg, argpos;
10588 gfc_formal_arglist* proc_formal;
10589 gfc_formal_arglist* old_formal;
10590
10591 /* This procedure should only be called for non-GENERIC proc. */
10592 gcc_assert (!proc->n.tb->is_generic);
10593
10594 /* If the overwritten procedure is GENERIC, this is an error. */
10595 if (old->n.tb->is_generic)
10596 {
10597 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10598 old->name, &proc->n.tb->where);
10599 return FAILURE;
10600 }
10601
10602 where = proc->n.tb->where;
10603 proc_target = proc->n.tb->u.specific->n.sym;
10604 old_target = old->n.tb->u.specific->n.sym;
10605
10606 /* Check that overridden binding is not NON_OVERRIDABLE. */
10607 if (old->n.tb->non_overridable)
10608 {
10609 gfc_error ("'%s' at %L overrides a procedure binding declared"
10610 " NON_OVERRIDABLE", proc->name, &where);
10611 return FAILURE;
10612 }
10613
10614 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10615 if (!old->n.tb->deferred && proc->n.tb->deferred)
10616 {
10617 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10618 " non-DEFERRED binding", proc->name, &where);
10619 return FAILURE;
10620 }
10621
10622 /* If the overridden binding is PURE, the overriding must be, too. */
10623 if (old_target->attr.pure && !proc_target->attr.pure)
10624 {
10625 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10626 proc->name, &where);
10627 return FAILURE;
10628 }
10629
10630 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10631 is not, the overriding must not be either. */
10632 if (old_target->attr.elemental && !proc_target->attr.elemental)
10633 {
10634 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10635 " ELEMENTAL", proc->name, &where);
10636 return FAILURE;
10637 }
10638 if (!old_target->attr.elemental && proc_target->attr.elemental)
10639 {
10640 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10641 " be ELEMENTAL, either", proc->name, &where);
10642 return FAILURE;
10643 }
10644
10645 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10646 SUBROUTINE. */
10647 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
10648 {
10649 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10650 " SUBROUTINE", proc->name, &where);
10651 return FAILURE;
10652 }
10653
10654 /* If the overridden binding is a FUNCTION, the overriding must also be a
10655 FUNCTION and have the same characteristics. */
10656 if (old_target->attr.function)
10657 {
10658 if (!proc_target->attr.function)
10659 {
10660 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10661 " FUNCTION", proc->name, &where);
10662 return FAILURE;
10663 }
10664
10665 /* FIXME: Do more comprehensive checking (including, for instance, the
10666 rank and array-shape). */
10667 gcc_assert (proc_target->result && old_target->result);
10668 if (!gfc_compare_types (&proc_target->result->ts,
10669 &old_target->result->ts))
10670 {
10671 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10672 " matching result types", proc->name, &where);
10673 return FAILURE;
10674 }
10675 }
10676
10677 /* If the overridden binding is PUBLIC, the overriding one must not be
10678 PRIVATE. */
10679 if (old->n.tb->access == ACCESS_PUBLIC
10680 && proc->n.tb->access == ACCESS_PRIVATE)
10681 {
10682 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10683 " PRIVATE", proc->name, &where);
10684 return FAILURE;
10685 }
10686
10687 /* Compare the formal argument lists of both procedures. This is also abused
10688 to find the position of the passed-object dummy arguments of both
10689 bindings as at least the overridden one might not yet be resolved and we
10690 need those positions in the check below. */
10691 proc_pass_arg = old_pass_arg = 0;
10692 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
10693 proc_pass_arg = 1;
10694 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
10695 old_pass_arg = 1;
10696 argpos = 1;
10697 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
10698 proc_formal && old_formal;
10699 proc_formal = proc_formal->next, old_formal = old_formal->next)
10700 {
10701 if (proc->n.tb->pass_arg
10702 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
10703 proc_pass_arg = argpos;
10704 if (old->n.tb->pass_arg
10705 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10706 old_pass_arg = argpos;
10707
10708 /* Check that the names correspond. */
10709 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10710 {
10711 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10712 " to match the corresponding argument of the overridden"
10713 " procedure", proc_formal->sym->name, proc->name, &where,
10714 old_formal->sym->name);
10715 return FAILURE;
10716 }
10717
10718 /* Check that the types correspond if neither is the passed-object
10719 argument. */
10720 /* FIXME: Do more comprehensive testing here. */
10721 if (proc_pass_arg != argpos && old_pass_arg != argpos
10722 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10723 {
10724 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10725 "in respect to the overridden procedure",
10726 proc_formal->sym->name, proc->name, &where);
10727 return FAILURE;
10728 }
10729
10730 ++argpos;
10731 }
10732 if (proc_formal || old_formal)
10733 {
10734 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10735 " the overridden procedure", proc->name, &where);
10736 return FAILURE;
10737 }
10738
10739 /* If the overridden binding is NOPASS, the overriding one must also be
10740 NOPASS. */
10741 if (old->n.tb->nopass && !proc->n.tb->nopass)
10742 {
10743 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10744 " NOPASS", proc->name, &where);
10745 return FAILURE;
10746 }
10747
10748 /* If the overridden binding is PASS(x), the overriding one must also be
10749 PASS and the passed-object dummy arguments must correspond. */
10750 if (!old->n.tb->nopass)
10751 {
10752 if (proc->n.tb->nopass)
10753 {
10754 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10755 " PASS", proc->name, &where);
10756 return FAILURE;
10757 }
10758
10759 if (proc_pass_arg != old_pass_arg)
10760 {
10761 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10762 " the same position as the passed-object dummy argument of"
10763 " the overridden procedure", proc->name, &where);
10764 return FAILURE;
10765 }
10766 }
10767
10768 return SUCCESS;
10769 }
10770
10771
10772 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10773
10774 static gfc_try
10775 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10776 const char* generic_name, locus where)
10777 {
10778 gfc_symbol* sym1;
10779 gfc_symbol* sym2;
10780
10781 gcc_assert (t1->specific && t2->specific);
10782 gcc_assert (!t1->specific->is_generic);
10783 gcc_assert (!t2->specific->is_generic);
10784
10785 sym1 = t1->specific->u.specific->n.sym;
10786 sym2 = t2->specific->u.specific->n.sym;
10787
10788 if (sym1 == sym2)
10789 return SUCCESS;
10790
10791 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10792 if (sym1->attr.subroutine != sym2->attr.subroutine
10793 || sym1->attr.function != sym2->attr.function)
10794 {
10795 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10796 " GENERIC '%s' at %L",
10797 sym1->name, sym2->name, generic_name, &where);
10798 return FAILURE;
10799 }
10800
10801 /* Compare the interfaces. */
10802 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10803 {
10804 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10805 sym1->name, sym2->name, generic_name, &where);
10806 return FAILURE;
10807 }
10808
10809 return SUCCESS;
10810 }
10811
10812
10813 /* Worker function for resolving a generic procedure binding; this is used to
10814 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10815
10816 The difference between those cases is finding possible inherited bindings
10817 that are overridden, as one has to look for them in tb_sym_root,
10818 tb_uop_root or tb_op, respectively. Thus the caller must already find
10819 the super-type and set p->overridden correctly. */
10820
10821 static gfc_try
10822 resolve_tb_generic_targets (gfc_symbol* super_type,
10823 gfc_typebound_proc* p, const char* name)
10824 {
10825 gfc_tbp_generic* target;
10826 gfc_symtree* first_target;
10827 gfc_symtree* inherited;
10828
10829 gcc_assert (p && p->is_generic);
10830
10831 /* Try to find the specific bindings for the symtrees in our target-list. */
10832 gcc_assert (p->u.generic);
10833 for (target = p->u.generic; target; target = target->next)
10834 if (!target->specific)
10835 {
10836 gfc_typebound_proc* overridden_tbp;
10837 gfc_tbp_generic* g;
10838 const char* target_name;
10839
10840 target_name = target->specific_st->name;
10841
10842 /* Defined for this type directly. */
10843 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
10844 {
10845 target->specific = target->specific_st->n.tb;
10846 goto specific_found;
10847 }
10848
10849 /* Look for an inherited specific binding. */
10850 if (super_type)
10851 {
10852 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10853 true, NULL);
10854
10855 if (inherited)
10856 {
10857 gcc_assert (inherited->n.tb);
10858 target->specific = inherited->n.tb;
10859 goto specific_found;
10860 }
10861 }
10862
10863 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10864 " at %L", target_name, name, &p->where);
10865 return FAILURE;
10866
10867 /* Once we've found the specific binding, check it is not ambiguous with
10868 other specifics already found or inherited for the same GENERIC. */
10869 specific_found:
10870 gcc_assert (target->specific);
10871
10872 /* This must really be a specific binding! */
10873 if (target->specific->is_generic)
10874 {
10875 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10876 " '%s' is GENERIC, too", name, &p->where, target_name);
10877 return FAILURE;
10878 }
10879
10880 /* Check those already resolved on this type directly. */
10881 for (g = p->u.generic; g; g = g->next)
10882 if (g != target && g->specific
10883 && check_generic_tbp_ambiguity (target, g, name, p->where)
10884 == FAILURE)
10885 return FAILURE;
10886
10887 /* Check for ambiguity with inherited specific targets. */
10888 for (overridden_tbp = p->overridden; overridden_tbp;
10889 overridden_tbp = overridden_tbp->overridden)
10890 if (overridden_tbp->is_generic)
10891 {
10892 for (g = overridden_tbp->u.generic; g; g = g->next)
10893 {
10894 gcc_assert (g->specific);
10895 if (check_generic_tbp_ambiguity (target, g,
10896 name, p->where) == FAILURE)
10897 return FAILURE;
10898 }
10899 }
10900 }
10901
10902 /* If we attempt to "overwrite" a specific binding, this is an error. */
10903 if (p->overridden && !p->overridden->is_generic)
10904 {
10905 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10906 " the same name", name, &p->where);
10907 return FAILURE;
10908 }
10909
10910 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10911 all must have the same attributes here. */
10912 first_target = p->u.generic->specific->u.specific;
10913 gcc_assert (first_target);
10914 p->subroutine = first_target->n.sym->attr.subroutine;
10915 p->function = first_target->n.sym->attr.function;
10916
10917 return SUCCESS;
10918 }
10919
10920
10921 /* Resolve a GENERIC procedure binding for a derived type. */
10922
10923 static gfc_try
10924 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10925 {
10926 gfc_symbol* super_type;
10927
10928 /* Find the overridden binding if any. */
10929 st->n.tb->overridden = NULL;
10930 super_type = gfc_get_derived_super_type (derived);
10931 if (super_type)
10932 {
10933 gfc_symtree* overridden;
10934 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10935 true, NULL);
10936
10937 if (overridden && overridden->n.tb)
10938 st->n.tb->overridden = overridden->n.tb;
10939 }
10940
10941 /* Resolve using worker function. */
10942 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10943 }
10944
10945
10946 /* Retrieve the target-procedure of an operator binding and do some checks in
10947 common for intrinsic and user-defined type-bound operators. */
10948
10949 static gfc_symbol*
10950 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10951 {
10952 gfc_symbol* target_proc;
10953
10954 gcc_assert (target->specific && !target->specific->is_generic);
10955 target_proc = target->specific->u.specific->n.sym;
10956 gcc_assert (target_proc);
10957
10958 /* All operator bindings must have a passed-object dummy argument. */
10959 if (target->specific->nopass)
10960 {
10961 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10962 return NULL;
10963 }
10964
10965 return target_proc;
10966 }
10967
10968
10969 /* Resolve a type-bound intrinsic operator. */
10970
10971 static gfc_try
10972 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10973 gfc_typebound_proc* p)
10974 {
10975 gfc_symbol* super_type;
10976 gfc_tbp_generic* target;
10977
10978 /* If there's already an error here, do nothing (but don't fail again). */
10979 if (p->error)
10980 return SUCCESS;
10981
10982 /* Operators should always be GENERIC bindings. */
10983 gcc_assert (p->is_generic);
10984
10985 /* Look for an overridden binding. */
10986 super_type = gfc_get_derived_super_type (derived);
10987 if (super_type && super_type->f2k_derived)
10988 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10989 op, true, NULL);
10990 else
10991 p->overridden = NULL;
10992
10993 /* Resolve general GENERIC properties using worker function. */
10994 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10995 goto error;
10996
10997 /* Check the targets to be procedures of correct interface. */
10998 for (target = p->u.generic; target; target = target->next)
10999 {
11000 gfc_symbol* target_proc;
11001
11002 target_proc = get_checked_tb_operator_target (target, p->where);
11003 if (!target_proc)
11004 goto error;
11005
11006 if (!gfc_check_operator_interface (target_proc, op, p->where))
11007 goto error;
11008 }
11009
11010 return SUCCESS;
11011
11012 error:
11013 p->error = 1;
11014 return FAILURE;
11015 }
11016
11017
11018 /* Resolve a type-bound user operator (tree-walker callback). */
11019
11020 static gfc_symbol* resolve_bindings_derived;
11021 static gfc_try resolve_bindings_result;
11022
11023 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
11024
11025 static void
11026 resolve_typebound_user_op (gfc_symtree* stree)
11027 {
11028 gfc_symbol* super_type;
11029 gfc_tbp_generic* target;
11030
11031 gcc_assert (stree && stree->n.tb);
11032
11033 if (stree->n.tb->error)
11034 return;
11035
11036 /* Operators should always be GENERIC bindings. */
11037 gcc_assert (stree->n.tb->is_generic);
11038
11039 /* Find overridden procedure, if any. */
11040 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11041 if (super_type && super_type->f2k_derived)
11042 {
11043 gfc_symtree* overridden;
11044 overridden = gfc_find_typebound_user_op (super_type, NULL,
11045 stree->name, true, NULL);
11046
11047 if (overridden && overridden->n.tb)
11048 stree->n.tb->overridden = overridden->n.tb;
11049 }
11050 else
11051 stree->n.tb->overridden = NULL;
11052
11053 /* Resolve basically using worker function. */
11054 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
11055 == FAILURE)
11056 goto error;
11057
11058 /* Check the targets to be functions of correct interface. */
11059 for (target = stree->n.tb->u.generic; target; target = target->next)
11060 {
11061 gfc_symbol* target_proc;
11062
11063 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11064 if (!target_proc)
11065 goto error;
11066
11067 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
11068 goto error;
11069 }
11070
11071 return;
11072
11073 error:
11074 resolve_bindings_result = FAILURE;
11075 stree->n.tb->error = 1;
11076 }
11077
11078
11079 /* Resolve the type-bound procedures for a derived type. */
11080
11081 static void
11082 resolve_typebound_procedure (gfc_symtree* stree)
11083 {
11084 gfc_symbol* proc;
11085 locus where;
11086 gfc_symbol* me_arg;
11087 gfc_symbol* super_type;
11088 gfc_component* comp;
11089
11090 gcc_assert (stree);
11091
11092 /* Undefined specific symbol from GENERIC target definition. */
11093 if (!stree->n.tb)
11094 return;
11095
11096 if (stree->n.tb->error)
11097 return;
11098
11099 /* If this is a GENERIC binding, use that routine. */
11100 if (stree->n.tb->is_generic)
11101 {
11102 if (resolve_typebound_generic (resolve_bindings_derived, stree)
11103 == FAILURE)
11104 goto error;
11105 return;
11106 }
11107
11108 /* Get the target-procedure to check it. */
11109 gcc_assert (!stree->n.tb->is_generic);
11110 gcc_assert (stree->n.tb->u.specific);
11111 proc = stree->n.tb->u.specific->n.sym;
11112 where = stree->n.tb->where;
11113
11114 /* Default access should already be resolved from the parser. */
11115 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11116
11117 /* It should be a module procedure or an external procedure with explicit
11118 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11119 if ((!proc->attr.subroutine && !proc->attr.function)
11120 || (proc->attr.proc != PROC_MODULE
11121 && proc->attr.if_source != IFSRC_IFBODY)
11122 || (proc->attr.abstract && !stree->n.tb->deferred))
11123 {
11124 gfc_error ("'%s' must be a module procedure or an external procedure with"
11125 " an explicit interface at %L", proc->name, &where);
11126 goto error;
11127 }
11128 stree->n.tb->subroutine = proc->attr.subroutine;
11129 stree->n.tb->function = proc->attr.function;
11130
11131 /* Find the super-type of the current derived type. We could do this once and
11132 store in a global if speed is needed, but as long as not I believe this is
11133 more readable and clearer. */
11134 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11135
11136 /* If PASS, resolve and check arguments if not already resolved / loaded
11137 from a .mod file. */
11138 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11139 {
11140 if (stree->n.tb->pass_arg)
11141 {
11142 gfc_formal_arglist* i;
11143
11144 /* If an explicit passing argument name is given, walk the arg-list
11145 and look for it. */
11146
11147 me_arg = NULL;
11148 stree->n.tb->pass_arg_num = 1;
11149 for (i = proc->formal; i; i = i->next)
11150 {
11151 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11152 {
11153 me_arg = i->sym;
11154 break;
11155 }
11156 ++stree->n.tb->pass_arg_num;
11157 }
11158
11159 if (!me_arg)
11160 {
11161 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11162 " argument '%s'",
11163 proc->name, stree->n.tb->pass_arg, &where,
11164 stree->n.tb->pass_arg);
11165 goto error;
11166 }
11167 }
11168 else
11169 {
11170 /* Otherwise, take the first one; there should in fact be at least
11171 one. */
11172 stree->n.tb->pass_arg_num = 1;
11173 if (!proc->formal)
11174 {
11175 gfc_error ("Procedure '%s' with PASS at %L must have at"
11176 " least one argument", proc->name, &where);
11177 goto error;
11178 }
11179 me_arg = proc->formal->sym;
11180 }
11181
11182 /* Now check that the argument-type matches and the passed-object
11183 dummy argument is generally fine. */
11184
11185 gcc_assert (me_arg);
11186
11187 if (me_arg->ts.type != BT_CLASS)
11188 {
11189 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11190 " at %L", proc->name, &where);
11191 goto error;
11192 }
11193
11194 if (CLASS_DATA (me_arg)->ts.u.derived
11195 != resolve_bindings_derived)
11196 {
11197 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11198 " the derived-type '%s'", me_arg->name, proc->name,
11199 me_arg->name, &where, resolve_bindings_derived->name);
11200 goto error;
11201 }
11202
11203 gcc_assert (me_arg->ts.type == BT_CLASS);
11204 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
11205 {
11206 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11207 " scalar", proc->name, &where);
11208 goto error;
11209 }
11210 if (CLASS_DATA (me_arg)->attr.allocatable)
11211 {
11212 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11213 " be ALLOCATABLE", proc->name, &where);
11214 goto error;
11215 }
11216 if (CLASS_DATA (me_arg)->attr.class_pointer)
11217 {
11218 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11219 " be POINTER", proc->name, &where);
11220 goto error;
11221 }
11222 }
11223
11224 /* If we are extending some type, check that we don't override a procedure
11225 flagged NON_OVERRIDABLE. */
11226 stree->n.tb->overridden = NULL;
11227 if (super_type)
11228 {
11229 gfc_symtree* overridden;
11230 overridden = gfc_find_typebound_proc (super_type, NULL,
11231 stree->name, true, NULL);
11232
11233 if (overridden && overridden->n.tb)
11234 stree->n.tb->overridden = overridden->n.tb;
11235
11236 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
11237 goto error;
11238 }
11239
11240 /* See if there's a name collision with a component directly in this type. */
11241 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11242 if (!strcmp (comp->name, stree->name))
11243 {
11244 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11245 " '%s'",
11246 stree->name, &where, resolve_bindings_derived->name);
11247 goto error;
11248 }
11249
11250 /* Try to find a name collision with an inherited component. */
11251 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11252 {
11253 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11254 " component of '%s'",
11255 stree->name, &where, resolve_bindings_derived->name);
11256 goto error;
11257 }
11258
11259 stree->n.tb->error = 0;
11260 return;
11261
11262 error:
11263 resolve_bindings_result = FAILURE;
11264 stree->n.tb->error = 1;
11265 }
11266
11267
11268 static gfc_try
11269 resolve_typebound_procedures (gfc_symbol* derived)
11270 {
11271 int op;
11272
11273 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11274 return SUCCESS;
11275
11276 resolve_bindings_derived = derived;
11277 resolve_bindings_result = SUCCESS;
11278
11279 /* Make sure the vtab has been generated. */
11280 gfc_find_derived_vtab (derived);
11281
11282 if (derived->f2k_derived->tb_sym_root)
11283 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11284 &resolve_typebound_procedure);
11285
11286 if (derived->f2k_derived->tb_uop_root)
11287 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11288 &resolve_typebound_user_op);
11289
11290 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11291 {
11292 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11293 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11294 p) == FAILURE)
11295 resolve_bindings_result = FAILURE;
11296 }
11297
11298 return resolve_bindings_result;
11299 }
11300
11301
11302 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11303 to give all identical derived types the same backend_decl. */
11304 static void
11305 add_dt_to_dt_list (gfc_symbol *derived)
11306 {
11307 gfc_dt_list *dt_list;
11308
11309 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11310 if (derived == dt_list->derived)
11311 return;
11312
11313 dt_list = gfc_get_dt_list ();
11314 dt_list->next = gfc_derived_types;
11315 dt_list->derived = derived;
11316 gfc_derived_types = dt_list;
11317 }
11318
11319
11320 /* Ensure that a derived-type is really not abstract, meaning that every
11321 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11322
11323 static gfc_try
11324 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11325 {
11326 if (!st)
11327 return SUCCESS;
11328
11329 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11330 return FAILURE;
11331 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11332 return FAILURE;
11333
11334 if (st->n.tb && st->n.tb->deferred)
11335 {
11336 gfc_symtree* overriding;
11337 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11338 if (!overriding)
11339 return FAILURE;
11340 gcc_assert (overriding->n.tb);
11341 if (overriding->n.tb->deferred)
11342 {
11343 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11344 " '%s' is DEFERRED and not overridden",
11345 sub->name, &sub->declared_at, st->name);
11346 return FAILURE;
11347 }
11348 }
11349
11350 return SUCCESS;
11351 }
11352
11353 static gfc_try
11354 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11355 {
11356 /* The algorithm used here is to recursively travel up the ancestry of sub
11357 and for each ancestor-type, check all bindings. If any of them is
11358 DEFERRED, look it up starting from sub and see if the found (overriding)
11359 binding is not DEFERRED.
11360 This is not the most efficient way to do this, but it should be ok and is
11361 clearer than something sophisticated. */
11362
11363 gcc_assert (ancestor && !sub->attr.abstract);
11364
11365 if (!ancestor->attr.abstract)
11366 return SUCCESS;
11367
11368 /* Walk bindings of this ancestor. */
11369 if (ancestor->f2k_derived)
11370 {
11371 gfc_try t;
11372 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11373 if (t == FAILURE)
11374 return FAILURE;
11375 }
11376
11377 /* Find next ancestor type and recurse on it. */
11378 ancestor = gfc_get_derived_super_type (ancestor);
11379 if (ancestor)
11380 return ensure_not_abstract (sub, ancestor);
11381
11382 return SUCCESS;
11383 }
11384
11385
11386 /* Resolve the components of a derived type. */
11387
11388 static gfc_try
11389 resolve_fl_derived (gfc_symbol *sym)
11390 {
11391 gfc_symbol* super_type;
11392 gfc_component *c;
11393
11394 super_type = gfc_get_derived_super_type (sym);
11395
11396 if (sym->attr.is_class && sym->ts.u.derived == NULL)
11397 {
11398 /* Fix up incomplete CLASS symbols. */
11399 gfc_component *data = gfc_find_component (sym, "_data", true, true);
11400 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
11401 if (vptr->ts.u.derived == NULL)
11402 {
11403 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
11404 gcc_assert (vtab);
11405 vptr->ts.u.derived = vtab->ts.u.derived;
11406 }
11407 }
11408
11409 /* F2008, C432. */
11410 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11411 {
11412 gfc_error ("As extending type '%s' at %L has a coarray component, "
11413 "parent type '%s' shall also have one", sym->name,
11414 &sym->declared_at, super_type->name);
11415 return FAILURE;
11416 }
11417
11418 /* Ensure the extended type gets resolved before we do. */
11419 if (super_type && resolve_fl_derived (super_type) == FAILURE)
11420 return FAILURE;
11421
11422 /* An ABSTRACT type must be extensible. */
11423 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11424 {
11425 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11426 sym->name, &sym->declared_at);
11427 return FAILURE;
11428 }
11429
11430 for (c = sym->components; c != NULL; c = c->next)
11431 {
11432 /* F2008, C442. */
11433 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
11434 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11435 {
11436 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11437 "deferred shape", c->name, &c->loc);
11438 return FAILURE;
11439 }
11440
11441 /* F2008, C443. */
11442 if (c->attr.codimension && c->ts.type == BT_DERIVED
11443 && c->ts.u.derived->ts.is_iso_c)
11444 {
11445 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11446 "shall not be a coarray", c->name, &c->loc);
11447 return FAILURE;
11448 }
11449
11450 /* F2008, C444. */
11451 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11452 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11453 || c->attr.allocatable))
11454 {
11455 gfc_error ("Component '%s' at %L with coarray component "
11456 "shall be a nonpointer, nonallocatable scalar",
11457 c->name, &c->loc);
11458 return FAILURE;
11459 }
11460
11461 /* F2008, C448. */
11462 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11463 {
11464 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11465 "is not an array pointer", c->name, &c->loc);
11466 return FAILURE;
11467 }
11468
11469 if (c->attr.proc_pointer && c->ts.interface)
11470 {
11471 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11472 gfc_error ("Interface '%s', used by procedure pointer component "
11473 "'%s' at %L, is declared in a later PROCEDURE statement",
11474 c->ts.interface->name, c->name, &c->loc);
11475
11476 /* Get the attributes from the interface (now resolved). */
11477 if (c->ts.interface->attr.if_source
11478 || c->ts.interface->attr.intrinsic)
11479 {
11480 gfc_symbol *ifc = c->ts.interface;
11481
11482 if (ifc->formal && !ifc->formal_ns)
11483 resolve_symbol (ifc);
11484
11485 if (ifc->attr.intrinsic)
11486 resolve_intrinsic (ifc, &ifc->declared_at);
11487
11488 if (ifc->result)
11489 {
11490 c->ts = ifc->result->ts;
11491 c->attr.allocatable = ifc->result->attr.allocatable;
11492 c->attr.pointer = ifc->result->attr.pointer;
11493 c->attr.dimension = ifc->result->attr.dimension;
11494 c->as = gfc_copy_array_spec (ifc->result->as);
11495 }
11496 else
11497 {
11498 c->ts = ifc->ts;
11499 c->attr.allocatable = ifc->attr.allocatable;
11500 c->attr.pointer = ifc->attr.pointer;
11501 c->attr.dimension = ifc->attr.dimension;
11502 c->as = gfc_copy_array_spec (ifc->as);
11503 }
11504 c->ts.interface = ifc;
11505 c->attr.function = ifc->attr.function;
11506 c->attr.subroutine = ifc->attr.subroutine;
11507 gfc_copy_formal_args_ppc (c, ifc);
11508
11509 c->attr.pure = ifc->attr.pure;
11510 c->attr.elemental = ifc->attr.elemental;
11511 c->attr.recursive = ifc->attr.recursive;
11512 c->attr.always_explicit = ifc->attr.always_explicit;
11513 c->attr.ext_attr |= ifc->attr.ext_attr;
11514 /* Replace symbols in array spec. */
11515 if (c->as)
11516 {
11517 int i;
11518 for (i = 0; i < c->as->rank; i++)
11519 {
11520 gfc_expr_replace_comp (c->as->lower[i], c);
11521 gfc_expr_replace_comp (c->as->upper[i], c);
11522 }
11523 }
11524 /* Copy char length. */
11525 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11526 {
11527 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11528 gfc_expr_replace_comp (cl->length, c);
11529 if (cl->length && !cl->resolved
11530 && gfc_resolve_expr (cl->length) == FAILURE)
11531 return FAILURE;
11532 c->ts.u.cl = cl;
11533 }
11534 }
11535 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11536 {
11537 gfc_error ("Interface '%s' of procedure pointer component "
11538 "'%s' at %L must be explicit", c->ts.interface->name,
11539 c->name, &c->loc);
11540 return FAILURE;
11541 }
11542 }
11543 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11544 {
11545 /* Since PPCs are not implicitly typed, a PPC without an explicit
11546 interface must be a subroutine. */
11547 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11548 }
11549
11550 /* Procedure pointer components: Check PASS arg. */
11551 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11552 && !sym->attr.vtype)
11553 {
11554 gfc_symbol* me_arg;
11555
11556 if (c->tb->pass_arg)
11557 {
11558 gfc_formal_arglist* i;
11559
11560 /* If an explicit passing argument name is given, walk the arg-list
11561 and look for it. */
11562
11563 me_arg = NULL;
11564 c->tb->pass_arg_num = 1;
11565 for (i = c->formal; i; i = i->next)
11566 {
11567 if (!strcmp (i->sym->name, c->tb->pass_arg))
11568 {
11569 me_arg = i->sym;
11570 break;
11571 }
11572 c->tb->pass_arg_num++;
11573 }
11574
11575 if (!me_arg)
11576 {
11577 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11578 "at %L has no argument '%s'", c->name,
11579 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11580 c->tb->error = 1;
11581 return FAILURE;
11582 }
11583 }
11584 else
11585 {
11586 /* Otherwise, take the first one; there should in fact be at least
11587 one. */
11588 c->tb->pass_arg_num = 1;
11589 if (!c->formal)
11590 {
11591 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11592 "must have at least one argument",
11593 c->name, &c->loc);
11594 c->tb->error = 1;
11595 return FAILURE;
11596 }
11597 me_arg = c->formal->sym;
11598 }
11599
11600 /* Now check that the argument-type matches. */
11601 gcc_assert (me_arg);
11602 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11603 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11604 || (me_arg->ts.type == BT_CLASS
11605 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11606 {
11607 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11608 " the derived type '%s'", me_arg->name, c->name,
11609 me_arg->name, &c->loc, sym->name);
11610 c->tb->error = 1;
11611 return FAILURE;
11612 }
11613
11614 /* Check for C453. */
11615 if (me_arg->attr.dimension)
11616 {
11617 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11618 "must be scalar", me_arg->name, c->name, me_arg->name,
11619 &c->loc);
11620 c->tb->error = 1;
11621 return FAILURE;
11622 }
11623
11624 if (me_arg->attr.pointer)
11625 {
11626 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11627 "may not have the POINTER attribute", me_arg->name,
11628 c->name, me_arg->name, &c->loc);
11629 c->tb->error = 1;
11630 return FAILURE;
11631 }
11632
11633 if (me_arg->attr.allocatable)
11634 {
11635 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11636 "may not be ALLOCATABLE", me_arg->name, c->name,
11637 me_arg->name, &c->loc);
11638 c->tb->error = 1;
11639 return FAILURE;
11640 }
11641
11642 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11643 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11644 " at %L", c->name, &c->loc);
11645
11646 }
11647
11648 /* Check type-spec if this is not the parent-type component. */
11649 if ((!sym->attr.extension || c != sym->components) && !sym->attr.vtype
11650 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11651 return FAILURE;
11652
11653 /* If this type is an extension, set the accessibility of the parent
11654 component. */
11655 if (super_type && c == sym->components
11656 && strcmp (super_type->name, c->name) == 0)
11657 c->attr.access = super_type->attr.access;
11658
11659 /* If this type is an extension, see if this component has the same name
11660 as an inherited type-bound procedure. */
11661 if (super_type && !sym->attr.is_class
11662 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11663 {
11664 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11665 " inherited type-bound procedure",
11666 c->name, sym->name, &c->loc);
11667 return FAILURE;
11668 }
11669
11670 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
11671 && !c->ts.deferred)
11672 {
11673 if (c->ts.u.cl->length == NULL
11674 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11675 || !gfc_is_constant_expr (c->ts.u.cl->length))
11676 {
11677 gfc_error ("Character length of component '%s' needs to "
11678 "be a constant specification expression at %L",
11679 c->name,
11680 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11681 return FAILURE;
11682 }
11683 }
11684
11685 if (c->ts.type == BT_CHARACTER && c->ts.deferred
11686 && !c->attr.pointer && !c->attr.allocatable)
11687 {
11688 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11689 "length must be a POINTER or ALLOCATABLE",
11690 c->name, sym->name, &c->loc);
11691 return FAILURE;
11692 }
11693
11694 if (c->ts.type == BT_DERIVED
11695 && sym->component_access != ACCESS_PRIVATE
11696 && gfc_check_symbol_access (sym)
11697 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11698 && !c->ts.u.derived->attr.use_assoc
11699 && !gfc_check_symbol_access (c->ts.u.derived)
11700 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11701 "is a PRIVATE type and cannot be a component of "
11702 "'%s', which is PUBLIC at %L", c->name,
11703 sym->name, &sym->declared_at) == FAILURE)
11704 return FAILURE;
11705
11706 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
11707 {
11708 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11709 "type %s", c->name, &c->loc, sym->name);
11710 return FAILURE;
11711 }
11712
11713 if (sym->attr.sequence)
11714 {
11715 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11716 {
11717 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11718 "not have the SEQUENCE attribute",
11719 c->ts.u.derived->name, &sym->declared_at);
11720 return FAILURE;
11721 }
11722 }
11723
11724 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11725 && c->attr.pointer && c->ts.u.derived->components == NULL
11726 && !c->ts.u.derived->attr.zero_comp)
11727 {
11728 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11729 "that has not been declared", c->name, sym->name,
11730 &c->loc);
11731 return FAILURE;
11732 }
11733
11734 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.class_pointer
11735 && CLASS_DATA (c)->ts.u.derived->components == NULL
11736 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11737 {
11738 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11739 "that has not been declared", c->name, sym->name,
11740 &c->loc);
11741 return FAILURE;
11742 }
11743
11744 /* C437. */
11745 if (c->ts.type == BT_CLASS
11746 && !(CLASS_DATA (c)->attr.class_pointer
11747 || CLASS_DATA (c)->attr.allocatable))
11748 {
11749 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11750 "or pointer", c->name, &c->loc);
11751 return FAILURE;
11752 }
11753
11754 /* Ensure that all the derived type components are put on the
11755 derived type list; even in formal namespaces, where derived type
11756 pointer components might not have been declared. */
11757 if (c->ts.type == BT_DERIVED
11758 && c->ts.u.derived
11759 && c->ts.u.derived->components
11760 && c->attr.pointer
11761 && sym != c->ts.u.derived)
11762 add_dt_to_dt_list (c->ts.u.derived);
11763
11764 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11765 || c->attr.proc_pointer
11766 || c->attr.allocatable)) == FAILURE)
11767 return FAILURE;
11768 }
11769
11770 /* Resolve the type-bound procedures. */
11771 if (resolve_typebound_procedures (sym) == FAILURE)
11772 return FAILURE;
11773
11774 /* Resolve the finalizer procedures. */
11775 if (gfc_resolve_finalizers (sym) == FAILURE)
11776 return FAILURE;
11777
11778 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11779 all DEFERRED bindings are overridden. */
11780 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11781 && !sym->attr.is_class
11782 && ensure_not_abstract (sym, super_type) == FAILURE)
11783 return FAILURE;
11784
11785 /* Add derived type to the derived type list. */
11786 add_dt_to_dt_list (sym);
11787
11788 return SUCCESS;
11789 }
11790
11791
11792 static gfc_try
11793 resolve_fl_namelist (gfc_symbol *sym)
11794 {
11795 gfc_namelist *nl;
11796 gfc_symbol *nlsym;
11797
11798 for (nl = sym->namelist; nl; nl = nl->next)
11799 {
11800 /* Check again, the check in match only works if NAMELIST comes
11801 after the decl. */
11802 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
11803 {
11804 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
11805 "allowed", nl->sym->name, sym->name, &sym->declared_at);
11806 return FAILURE;
11807 }
11808
11809 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11810 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11811 "object '%s' with assumed shape in namelist "
11812 "'%s' at %L", nl->sym->name, sym->name,
11813 &sym->declared_at) == FAILURE)
11814 return FAILURE;
11815
11816 if (is_non_constant_shape_array (nl->sym)
11817 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11818 "object '%s' with nonconstant shape in namelist "
11819 "'%s' at %L", nl->sym->name, sym->name,
11820 &sym->declared_at) == FAILURE)
11821 return FAILURE;
11822
11823 if (nl->sym->ts.type == BT_CHARACTER
11824 && (nl->sym->ts.u.cl->length == NULL
11825 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
11826 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11827 "'%s' with nonconstant character length in "
11828 "namelist '%s' at %L", nl->sym->name, sym->name,
11829 &sym->declared_at) == FAILURE)
11830 return FAILURE;
11831
11832 /* FIXME: Once UDDTIO is implemented, the following can be
11833 removed. */
11834 if (nl->sym->ts.type == BT_CLASS)
11835 {
11836 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
11837 "polymorphic and requires a defined input/output "
11838 "procedure", nl->sym->name, sym->name, &sym->declared_at);
11839 return FAILURE;
11840 }
11841
11842 if (nl->sym->ts.type == BT_DERIVED
11843 && (nl->sym->ts.u.derived->attr.alloc_comp
11844 || nl->sym->ts.u.derived->attr.pointer_comp))
11845 {
11846 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11847 "'%s' in namelist '%s' at %L with ALLOCATABLE "
11848 "or POINTER components", nl->sym->name,
11849 sym->name, &sym->declared_at) == FAILURE)
11850 return FAILURE;
11851
11852 /* FIXME: Once UDDTIO is implemented, the following can be
11853 removed. */
11854 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
11855 "ALLOCATABLE or POINTER components and thus requires "
11856 "a defined input/output procedure", nl->sym->name,
11857 sym->name, &sym->declared_at);
11858 return FAILURE;
11859 }
11860 }
11861
11862 /* Reject PRIVATE objects in a PUBLIC namelist. */
11863 if (gfc_check_symbol_access (sym))
11864 {
11865 for (nl = sym->namelist; nl; nl = nl->next)
11866 {
11867 if (!nl->sym->attr.use_assoc
11868 && !is_sym_host_assoc (nl->sym, sym->ns)
11869 && !gfc_check_symbol_access (nl->sym))
11870 {
11871 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11872 "cannot be member of PUBLIC namelist '%s' at %L",
11873 nl->sym->name, sym->name, &sym->declared_at);
11874 return FAILURE;
11875 }
11876
11877 /* Types with private components that came here by USE-association. */
11878 if (nl->sym->ts.type == BT_DERIVED
11879 && derived_inaccessible (nl->sym->ts.u.derived))
11880 {
11881 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11882 "components and cannot be member of namelist '%s' at %L",
11883 nl->sym->name, sym->name, &sym->declared_at);
11884 return FAILURE;
11885 }
11886
11887 /* Types with private components that are defined in the same module. */
11888 if (nl->sym->ts.type == BT_DERIVED
11889 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11890 && nl->sym->ts.u.derived->attr.private_comp)
11891 {
11892 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11893 "cannot be a member of PUBLIC namelist '%s' at %L",
11894 nl->sym->name, sym->name, &sym->declared_at);
11895 return FAILURE;
11896 }
11897 }
11898 }
11899
11900
11901 /* 14.1.2 A module or internal procedure represent local entities
11902 of the same type as a namelist member and so are not allowed. */
11903 for (nl = sym->namelist; nl; nl = nl->next)
11904 {
11905 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11906 continue;
11907
11908 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11909 if ((nl->sym == sym->ns->proc_name)
11910 ||
11911 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11912 continue;
11913
11914 nlsym = NULL;
11915 if (nl->sym && nl->sym->name)
11916 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11917 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11918 {
11919 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11920 "attribute in '%s' at %L", nlsym->name,
11921 &sym->declared_at);
11922 return FAILURE;
11923 }
11924 }
11925
11926 return SUCCESS;
11927 }
11928
11929
11930 static gfc_try
11931 resolve_fl_parameter (gfc_symbol *sym)
11932 {
11933 /* A parameter array's shape needs to be constant. */
11934 if (sym->as != NULL
11935 && (sym->as->type == AS_DEFERRED
11936 || is_non_constant_shape_array (sym)))
11937 {
11938 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11939 "or of deferred shape", sym->name, &sym->declared_at);
11940 return FAILURE;
11941 }
11942
11943 /* Make sure a parameter that has been implicitly typed still
11944 matches the implicit type, since PARAMETER statements can precede
11945 IMPLICIT statements. */
11946 if (sym->attr.implicit_type
11947 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11948 sym->ns)))
11949 {
11950 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11951 "later IMPLICIT type", sym->name, &sym->declared_at);
11952 return FAILURE;
11953 }
11954
11955 /* Make sure the types of derived parameters are consistent. This
11956 type checking is deferred until resolution because the type may
11957 refer to a derived type from the host. */
11958 if (sym->ts.type == BT_DERIVED
11959 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11960 {
11961 gfc_error ("Incompatible derived type in PARAMETER at %L",
11962 &sym->value->where);
11963 return FAILURE;
11964 }
11965 return SUCCESS;
11966 }
11967
11968
11969 /* Do anything necessary to resolve a symbol. Right now, we just
11970 assume that an otherwise unknown symbol is a variable. This sort
11971 of thing commonly happens for symbols in module. */
11972
11973 static void
11974 resolve_symbol (gfc_symbol *sym)
11975 {
11976 int check_constant, mp_flag;
11977 gfc_symtree *symtree;
11978 gfc_symtree *this_symtree;
11979 gfc_namespace *ns;
11980 gfc_component *c;
11981
11982 if (sym->attr.flavor == FL_UNKNOWN)
11983 {
11984
11985 /* If we find that a flavorless symbol is an interface in one of the
11986 parent namespaces, find its symtree in this namespace, free the
11987 symbol and set the symtree to point to the interface symbol. */
11988 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11989 {
11990 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11991 if (symtree && (symtree->n.sym->generic ||
11992 (symtree->n.sym->attr.flavor == FL_PROCEDURE
11993 && sym->ns->construct_entities)))
11994 {
11995 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11996 sym->name);
11997 gfc_release_symbol (sym);
11998 symtree->n.sym->refs++;
11999 this_symtree->n.sym = symtree->n.sym;
12000 return;
12001 }
12002 }
12003
12004 /* Otherwise give it a flavor according to such attributes as
12005 it has. */
12006 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
12007 sym->attr.flavor = FL_VARIABLE;
12008 else
12009 {
12010 sym->attr.flavor = FL_PROCEDURE;
12011 if (sym->attr.dimension)
12012 sym->attr.function = 1;
12013 }
12014 }
12015
12016 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12017 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12018
12019 if (sym->attr.procedure && sym->ts.interface
12020 && sym->attr.if_source != IFSRC_DECL
12021 && resolve_procedure_interface (sym) == FAILURE)
12022 return;
12023
12024 if (sym->attr.is_protected && !sym->attr.proc_pointer
12025 && (sym->attr.procedure || sym->attr.external))
12026 {
12027 if (sym->attr.external)
12028 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12029 "at %L", &sym->declared_at);
12030 else
12031 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12032 "at %L", &sym->declared_at);
12033
12034 return;
12035 }
12036
12037
12038 /* F2008, C530. */
12039 if (sym->attr.contiguous
12040 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
12041 && !sym->attr.pointer)))
12042 {
12043 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12044 "array pointer or an assumed-shape array", sym->name,
12045 &sym->declared_at);
12046 return;
12047 }
12048
12049 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
12050 return;
12051
12052 /* Symbols that are module procedures with results (functions) have
12053 the types and array specification copied for type checking in
12054 procedures that call them, as well as for saving to a module
12055 file. These symbols can't stand the scrutiny that their results
12056 can. */
12057 mp_flag = (sym->result != NULL && sym->result != sym);
12058
12059 /* Make sure that the intrinsic is consistent with its internal
12060 representation. This needs to be done before assigning a default
12061 type to avoid spurious warnings. */
12062 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12063 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
12064 return;
12065
12066 /* Resolve associate names. */
12067 if (sym->assoc)
12068 resolve_assoc_var (sym, true);
12069
12070 /* Assign default type to symbols that need one and don't have one. */
12071 if (sym->ts.type == BT_UNKNOWN)
12072 {
12073 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12074 gfc_set_default_type (sym, 1, NULL);
12075
12076 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12077 && !sym->attr.function && !sym->attr.subroutine
12078 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12079 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12080
12081 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12082 {
12083 /* The specific case of an external procedure should emit an error
12084 in the case that there is no implicit type. */
12085 if (!mp_flag)
12086 gfc_set_default_type (sym, sym->attr.external, NULL);
12087 else
12088 {
12089 /* Result may be in another namespace. */
12090 resolve_symbol (sym->result);
12091
12092 if (!sym->result->attr.proc_pointer)
12093 {
12094 sym->ts = sym->result->ts;
12095 sym->as = gfc_copy_array_spec (sym->result->as);
12096 sym->attr.dimension = sym->result->attr.dimension;
12097 sym->attr.pointer = sym->result->attr.pointer;
12098 sym->attr.allocatable = sym->result->attr.allocatable;
12099 sym->attr.contiguous = sym->result->attr.contiguous;
12100 }
12101 }
12102 }
12103 }
12104
12105 /* Assumed size arrays and assumed shape arrays must be dummy
12106 arguments. Array-spec's of implied-shape should have been resolved to
12107 AS_EXPLICIT already. */
12108
12109 if (sym->as)
12110 {
12111 gcc_assert (sym->as->type != AS_IMPLIED_SHAPE);
12112 if (((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
12113 || sym->as->type == AS_ASSUMED_SHAPE)
12114 && sym->attr.dummy == 0)
12115 {
12116 if (sym->as->type == AS_ASSUMED_SIZE)
12117 gfc_error ("Assumed size array at %L must be a dummy argument",
12118 &sym->declared_at);
12119 else
12120 gfc_error ("Assumed shape array at %L must be a dummy argument",
12121 &sym->declared_at);
12122 return;
12123 }
12124 }
12125
12126 /* Make sure symbols with known intent or optional are really dummy
12127 variable. Because of ENTRY statement, this has to be deferred
12128 until resolution time. */
12129
12130 if (!sym->attr.dummy
12131 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12132 {
12133 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12134 return;
12135 }
12136
12137 if (sym->attr.value && !sym->attr.dummy)
12138 {
12139 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12140 "it is not a dummy argument", sym->name, &sym->declared_at);
12141 return;
12142 }
12143
12144 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12145 {
12146 gfc_charlen *cl = sym->ts.u.cl;
12147 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12148 {
12149 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12150 "attribute must have constant length",
12151 sym->name, &sym->declared_at);
12152 return;
12153 }
12154
12155 if (sym->ts.is_c_interop
12156 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12157 {
12158 gfc_error ("C interoperable character dummy variable '%s' at %L "
12159 "with VALUE attribute must have length one",
12160 sym->name, &sym->declared_at);
12161 return;
12162 }
12163 }
12164
12165 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12166 do this for something that was implicitly typed because that is handled
12167 in gfc_set_default_type. Handle dummy arguments and procedure
12168 definitions separately. Also, anything that is use associated is not
12169 handled here but instead is handled in the module it is declared in.
12170 Finally, derived type definitions are allowed to be BIND(C) since that
12171 only implies that they're interoperable, and they are checked fully for
12172 interoperability when a variable is declared of that type. */
12173 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12174 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12175 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12176 {
12177 gfc_try t = SUCCESS;
12178
12179 /* First, make sure the variable is declared at the
12180 module-level scope (J3/04-007, Section 15.3). */
12181 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12182 sym->attr.in_common == 0)
12183 {
12184 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12185 "is neither a COMMON block nor declared at the "
12186 "module level scope", sym->name, &(sym->declared_at));
12187 t = FAILURE;
12188 }
12189 else if (sym->common_head != NULL)
12190 {
12191 t = verify_com_block_vars_c_interop (sym->common_head);
12192 }
12193 else
12194 {
12195 /* If type() declaration, we need to verify that the components
12196 of the given type are all C interoperable, etc. */
12197 if (sym->ts.type == BT_DERIVED &&
12198 sym->ts.u.derived->attr.is_c_interop != 1)
12199 {
12200 /* Make sure the user marked the derived type as BIND(C). If
12201 not, call the verify routine. This could print an error
12202 for the derived type more than once if multiple variables
12203 of that type are declared. */
12204 if (sym->ts.u.derived->attr.is_bind_c != 1)
12205 verify_bind_c_derived_type (sym->ts.u.derived);
12206 t = FAILURE;
12207 }
12208
12209 /* Verify the variable itself as C interoperable if it
12210 is BIND(C). It is not possible for this to succeed if
12211 the verify_bind_c_derived_type failed, so don't have to handle
12212 any error returned by verify_bind_c_derived_type. */
12213 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12214 sym->common_block);
12215 }
12216
12217 if (t == FAILURE)
12218 {
12219 /* clear the is_bind_c flag to prevent reporting errors more than
12220 once if something failed. */
12221 sym->attr.is_bind_c = 0;
12222 return;
12223 }
12224 }
12225
12226 /* If a derived type symbol has reached this point, without its
12227 type being declared, we have an error. Notice that most
12228 conditions that produce undefined derived types have already
12229 been dealt with. However, the likes of:
12230 implicit type(t) (t) ..... call foo (t) will get us here if
12231 the type is not declared in the scope of the implicit
12232 statement. Change the type to BT_UNKNOWN, both because it is so
12233 and to prevent an ICE. */
12234 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
12235 && !sym->ts.u.derived->attr.zero_comp)
12236 {
12237 gfc_error ("The derived type '%s' at %L is of type '%s', "
12238 "which has not been defined", sym->name,
12239 &sym->declared_at, sym->ts.u.derived->name);
12240 sym->ts.type = BT_UNKNOWN;
12241 return;
12242 }
12243
12244 /* Make sure that the derived type has been resolved and that the
12245 derived type is visible in the symbol's namespace, if it is a
12246 module function and is not PRIVATE. */
12247 if (sym->ts.type == BT_DERIVED
12248 && sym->ts.u.derived->attr.use_assoc
12249 && sym->ns->proc_name
12250 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12251 {
12252 gfc_symbol *ds;
12253
12254 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12255 return;
12256
12257 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
12258 if (!ds && sym->attr.function && gfc_check_symbol_access (sym))
12259 {
12260 symtree = gfc_new_symtree (&sym->ns->sym_root,
12261 sym->ts.u.derived->name);
12262 symtree->n.sym = sym->ts.u.derived;
12263 sym->ts.u.derived->refs++;
12264 }
12265 }
12266
12267 /* Unless the derived-type declaration is use associated, Fortran 95
12268 does not allow public entries of private derived types.
12269 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12270 161 in 95-006r3. */
12271 if (sym->ts.type == BT_DERIVED
12272 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12273 && !sym->ts.u.derived->attr.use_assoc
12274 && gfc_check_symbol_access (sym)
12275 && !gfc_check_symbol_access (sym->ts.u.derived)
12276 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12277 "of PRIVATE derived type '%s'",
12278 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12279 : "variable", sym->name, &sym->declared_at,
12280 sym->ts.u.derived->name) == FAILURE)
12281 return;
12282
12283 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12284 default initialization is defined (5.1.2.4.4). */
12285 if (sym->ts.type == BT_DERIVED
12286 && sym->attr.dummy
12287 && sym->attr.intent == INTENT_OUT
12288 && sym->as
12289 && sym->as->type == AS_ASSUMED_SIZE)
12290 {
12291 for (c = sym->ts.u.derived->components; c; c = c->next)
12292 {
12293 if (c->initializer)
12294 {
12295 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12296 "ASSUMED SIZE and so cannot have a default initializer",
12297 sym->name, &sym->declared_at);
12298 return;
12299 }
12300 }
12301 }
12302
12303 /* F2008, C526. */
12304 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12305 || sym->attr.codimension)
12306 && sym->attr.result)
12307 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12308 "a coarray component", sym->name, &sym->declared_at);
12309
12310 /* F2008, C524. */
12311 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12312 && sym->ts.u.derived->ts.is_iso_c)
12313 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12314 "shall not be a coarray", sym->name, &sym->declared_at);
12315
12316 /* F2008, C525. */
12317 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
12318 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
12319 || sym->attr.allocatable))
12320 gfc_error ("Variable '%s' at %L with coarray component "
12321 "shall be a nonpointer, nonallocatable scalar",
12322 sym->name, &sym->declared_at);
12323
12324 /* F2008, C526. The function-result case was handled above. */
12325 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12326 || sym->attr.codimension)
12327 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
12328 || sym->ns->proc_name->attr.flavor == FL_MODULE
12329 || sym->ns->proc_name->attr.is_main_program
12330 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12331 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
12332 "component and is not ALLOCATABLE, SAVE nor a "
12333 "dummy argument", sym->name, &sym->declared_at);
12334 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12335 else if (sym->attr.codimension && !sym->attr.allocatable
12336 && sym->as && sym->as->cotype == AS_DEFERRED)
12337 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12338 "deferred shape", sym->name, &sym->declared_at);
12339 else if (sym->attr.codimension && sym->attr.allocatable
12340 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
12341 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12342 "deferred shape", sym->name, &sym->declared_at);
12343
12344
12345 /* F2008, C541. */
12346 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12347 || (sym->attr.codimension && sym->attr.allocatable))
12348 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12349 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12350 "allocatable coarray or have coarray components",
12351 sym->name, &sym->declared_at);
12352
12353 if (sym->attr.codimension && sym->attr.dummy
12354 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12355 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12356 "procedure '%s'", sym->name, &sym->declared_at,
12357 sym->ns->proc_name->name);
12358
12359 switch (sym->attr.flavor)
12360 {
12361 case FL_VARIABLE:
12362 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12363 return;
12364 break;
12365
12366 case FL_PROCEDURE:
12367 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12368 return;
12369 break;
12370
12371 case FL_NAMELIST:
12372 if (resolve_fl_namelist (sym) == FAILURE)
12373 return;
12374 break;
12375
12376 case FL_PARAMETER:
12377 if (resolve_fl_parameter (sym) == FAILURE)
12378 return;
12379 break;
12380
12381 default:
12382 break;
12383 }
12384
12385 /* Resolve array specifier. Check as well some constraints
12386 on COMMON blocks. */
12387
12388 check_constant = sym->attr.in_common && !sym->attr.pointer;
12389
12390 /* Set the formal_arg_flag so that check_conflict will not throw
12391 an error for host associated variables in the specification
12392 expression for an array_valued function. */
12393 if (sym->attr.function && sym->as)
12394 formal_arg_flag = 1;
12395
12396 gfc_resolve_array_spec (sym->as, check_constant);
12397
12398 formal_arg_flag = 0;
12399
12400 /* Resolve formal namespaces. */
12401 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12402 && !sym->attr.contained && !sym->attr.intrinsic)
12403 gfc_resolve (sym->formal_ns);
12404
12405 /* Make sure the formal namespace is present. */
12406 if (sym->formal && !sym->formal_ns)
12407 {
12408 gfc_formal_arglist *formal = sym->formal;
12409 while (formal && !formal->sym)
12410 formal = formal->next;
12411
12412 if (formal)
12413 {
12414 sym->formal_ns = formal->sym->ns;
12415 sym->formal_ns->refs++;
12416 }
12417 }
12418
12419 /* Check threadprivate restrictions. */
12420 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12421 && (!sym->attr.in_common
12422 && sym->module == NULL
12423 && (sym->ns->proc_name == NULL
12424 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12425 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12426
12427 /* If we have come this far we can apply default-initializers, as
12428 described in 14.7.5, to those variables that have not already
12429 been assigned one. */
12430 if (sym->ts.type == BT_DERIVED
12431 && sym->ns == gfc_current_ns
12432 && !sym->value
12433 && !sym->attr.allocatable
12434 && !sym->attr.alloc_comp)
12435 {
12436 symbol_attribute *a = &sym->attr;
12437
12438 if ((!a->save && !a->dummy && !a->pointer
12439 && !a->in_common && !a->use_assoc
12440 && (a->referenced || a->result)
12441 && !(a->function && sym != sym->result))
12442 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12443 apply_default_init (sym);
12444 }
12445
12446 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12447 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12448 && !CLASS_DATA (sym)->attr.class_pointer
12449 && !CLASS_DATA (sym)->attr.allocatable)
12450 apply_default_init (sym);
12451
12452 /* If this symbol has a type-spec, check it. */
12453 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12454 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12455 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12456 == FAILURE)
12457 return;
12458 }
12459
12460
12461 /************* Resolve DATA statements *************/
12462
12463 static struct
12464 {
12465 gfc_data_value *vnode;
12466 mpz_t left;
12467 }
12468 values;
12469
12470
12471 /* Advance the values structure to point to the next value in the data list. */
12472
12473 static gfc_try
12474 next_data_value (void)
12475 {
12476 while (mpz_cmp_ui (values.left, 0) == 0)
12477 {
12478
12479 if (values.vnode->next == NULL)
12480 return FAILURE;
12481
12482 values.vnode = values.vnode->next;
12483 mpz_set (values.left, values.vnode->repeat);
12484 }
12485
12486 return SUCCESS;
12487 }
12488
12489
12490 static gfc_try
12491 check_data_variable (gfc_data_variable *var, locus *where)
12492 {
12493 gfc_expr *e;
12494 mpz_t size;
12495 mpz_t offset;
12496 gfc_try t;
12497 ar_type mark = AR_UNKNOWN;
12498 int i;
12499 mpz_t section_index[GFC_MAX_DIMENSIONS];
12500 gfc_ref *ref;
12501 gfc_array_ref *ar;
12502 gfc_symbol *sym;
12503 int has_pointer;
12504
12505 if (gfc_resolve_expr (var->expr) == FAILURE)
12506 return FAILURE;
12507
12508 ar = NULL;
12509 mpz_init_set_si (offset, 0);
12510 e = var->expr;
12511
12512 if (e->expr_type != EXPR_VARIABLE)
12513 gfc_internal_error ("check_data_variable(): Bad expression");
12514
12515 sym = e->symtree->n.sym;
12516
12517 if (sym->ns->is_block_data && !sym->attr.in_common)
12518 {
12519 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12520 sym->name, &sym->declared_at);
12521 }
12522
12523 if (e->ref == NULL && sym->as)
12524 {
12525 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12526 " declaration", sym->name, where);
12527 return FAILURE;
12528 }
12529
12530 has_pointer = sym->attr.pointer;
12531
12532 if (gfc_is_coindexed (e))
12533 {
12534 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
12535 where);
12536 return FAILURE;
12537 }
12538
12539 for (ref = e->ref; ref; ref = ref->next)
12540 {
12541 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12542 has_pointer = 1;
12543
12544 if (has_pointer
12545 && ref->type == REF_ARRAY
12546 && ref->u.ar.type != AR_FULL)
12547 {
12548 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12549 "be a full array", sym->name, where);
12550 return FAILURE;
12551 }
12552 }
12553
12554 if (e->rank == 0 || has_pointer)
12555 {
12556 mpz_init_set_ui (size, 1);
12557 ref = NULL;
12558 }
12559 else
12560 {
12561 ref = e->ref;
12562
12563 /* Find the array section reference. */
12564 for (ref = e->ref; ref; ref = ref->next)
12565 {
12566 if (ref->type != REF_ARRAY)
12567 continue;
12568 if (ref->u.ar.type == AR_ELEMENT)
12569 continue;
12570 break;
12571 }
12572 gcc_assert (ref);
12573
12574 /* Set marks according to the reference pattern. */
12575 switch (ref->u.ar.type)
12576 {
12577 case AR_FULL:
12578 mark = AR_FULL;
12579 break;
12580
12581 case AR_SECTION:
12582 ar = &ref->u.ar;
12583 /* Get the start position of array section. */
12584 gfc_get_section_index (ar, section_index, &offset);
12585 mark = AR_SECTION;
12586 break;
12587
12588 default:
12589 gcc_unreachable ();
12590 }
12591
12592 if (gfc_array_size (e, &size) == FAILURE)
12593 {
12594 gfc_error ("Nonconstant array section at %L in DATA statement",
12595 &e->where);
12596 mpz_clear (offset);
12597 return FAILURE;
12598 }
12599 }
12600
12601 t = SUCCESS;
12602
12603 while (mpz_cmp_ui (size, 0) > 0)
12604 {
12605 if (next_data_value () == FAILURE)
12606 {
12607 gfc_error ("DATA statement at %L has more variables than values",
12608 where);
12609 t = FAILURE;
12610 break;
12611 }
12612
12613 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12614 if (t == FAILURE)
12615 break;
12616
12617 /* If we have more than one element left in the repeat count,
12618 and we have more than one element left in the target variable,
12619 then create a range assignment. */
12620 /* FIXME: Only done for full arrays for now, since array sections
12621 seem tricky. */
12622 if (mark == AR_FULL && ref && ref->next == NULL
12623 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12624 {
12625 mpz_t range;
12626
12627 if (mpz_cmp (size, values.left) >= 0)
12628 {
12629 mpz_init_set (range, values.left);
12630 mpz_sub (size, size, values.left);
12631 mpz_set_ui (values.left, 0);
12632 }
12633 else
12634 {
12635 mpz_init_set (range, size);
12636 mpz_sub (values.left, values.left, size);
12637 mpz_set_ui (size, 0);
12638 }
12639
12640 t = gfc_assign_data_value_range (var->expr, values.vnode->expr,
12641 offset, range);
12642
12643 mpz_add (offset, offset, range);
12644 mpz_clear (range);
12645
12646 if (t == FAILURE)
12647 break;
12648 }
12649
12650 /* Assign initial value to symbol. */
12651 else
12652 {
12653 mpz_sub_ui (values.left, values.left, 1);
12654 mpz_sub_ui (size, size, 1);
12655
12656 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
12657 if (t == FAILURE)
12658 break;
12659
12660 if (mark == AR_FULL)
12661 mpz_add_ui (offset, offset, 1);
12662
12663 /* Modify the array section indexes and recalculate the offset
12664 for next element. */
12665 else if (mark == AR_SECTION)
12666 gfc_advance_section (section_index, ar, &offset);
12667 }
12668 }
12669
12670 if (mark == AR_SECTION)
12671 {
12672 for (i = 0; i < ar->dimen; i++)
12673 mpz_clear (section_index[i]);
12674 }
12675
12676 mpz_clear (size);
12677 mpz_clear (offset);
12678
12679 return t;
12680 }
12681
12682
12683 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12684
12685 /* Iterate over a list of elements in a DATA statement. */
12686
12687 static gfc_try
12688 traverse_data_list (gfc_data_variable *var, locus *where)
12689 {
12690 mpz_t trip;
12691 iterator_stack frame;
12692 gfc_expr *e, *start, *end, *step;
12693 gfc_try retval = SUCCESS;
12694
12695 mpz_init (frame.value);
12696 mpz_init (trip);
12697
12698 start = gfc_copy_expr (var->iter.start);
12699 end = gfc_copy_expr (var->iter.end);
12700 step = gfc_copy_expr (var->iter.step);
12701
12702 if (gfc_simplify_expr (start, 1) == FAILURE
12703 || start->expr_type != EXPR_CONSTANT)
12704 {
12705 gfc_error ("start of implied-do loop at %L could not be "
12706 "simplified to a constant value", &start->where);
12707 retval = FAILURE;
12708 goto cleanup;
12709 }
12710 if (gfc_simplify_expr (end, 1) == FAILURE
12711 || end->expr_type != EXPR_CONSTANT)
12712 {
12713 gfc_error ("end of implied-do loop at %L could not be "
12714 "simplified to a constant value", &start->where);
12715 retval = FAILURE;
12716 goto cleanup;
12717 }
12718 if (gfc_simplify_expr (step, 1) == FAILURE
12719 || step->expr_type != EXPR_CONSTANT)
12720 {
12721 gfc_error ("step of implied-do loop at %L could not be "
12722 "simplified to a constant value", &start->where);
12723 retval = FAILURE;
12724 goto cleanup;
12725 }
12726
12727 mpz_set (trip, end->value.integer);
12728 mpz_sub (trip, trip, start->value.integer);
12729 mpz_add (trip, trip, step->value.integer);
12730
12731 mpz_div (trip, trip, step->value.integer);
12732
12733 mpz_set (frame.value, start->value.integer);
12734
12735 frame.prev = iter_stack;
12736 frame.variable = var->iter.var->symtree;
12737 iter_stack = &frame;
12738
12739 while (mpz_cmp_ui (trip, 0) > 0)
12740 {
12741 if (traverse_data_var (var->list, where) == FAILURE)
12742 {
12743 retval = FAILURE;
12744 goto cleanup;
12745 }
12746
12747 e = gfc_copy_expr (var->expr);
12748 if (gfc_simplify_expr (e, 1) == FAILURE)
12749 {
12750 gfc_free_expr (e);
12751 retval = FAILURE;
12752 goto cleanup;
12753 }
12754
12755 mpz_add (frame.value, frame.value, step->value.integer);
12756
12757 mpz_sub_ui (trip, trip, 1);
12758 }
12759
12760 cleanup:
12761 mpz_clear (frame.value);
12762 mpz_clear (trip);
12763
12764 gfc_free_expr (start);
12765 gfc_free_expr (end);
12766 gfc_free_expr (step);
12767
12768 iter_stack = frame.prev;
12769 return retval;
12770 }
12771
12772
12773 /* Type resolve variables in the variable list of a DATA statement. */
12774
12775 static gfc_try
12776 traverse_data_var (gfc_data_variable *var, locus *where)
12777 {
12778 gfc_try t;
12779
12780 for (; var; var = var->next)
12781 {
12782 if (var->expr == NULL)
12783 t = traverse_data_list (var, where);
12784 else
12785 t = check_data_variable (var, where);
12786
12787 if (t == FAILURE)
12788 return FAILURE;
12789 }
12790
12791 return SUCCESS;
12792 }
12793
12794
12795 /* Resolve the expressions and iterators associated with a data statement.
12796 This is separate from the assignment checking because data lists should
12797 only be resolved once. */
12798
12799 static gfc_try
12800 resolve_data_variables (gfc_data_variable *d)
12801 {
12802 for (; d; d = d->next)
12803 {
12804 if (d->list == NULL)
12805 {
12806 if (gfc_resolve_expr (d->expr) == FAILURE)
12807 return FAILURE;
12808 }
12809 else
12810 {
12811 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12812 return FAILURE;
12813
12814 if (resolve_data_variables (d->list) == FAILURE)
12815 return FAILURE;
12816 }
12817 }
12818
12819 return SUCCESS;
12820 }
12821
12822
12823 /* Resolve a single DATA statement. We implement this by storing a pointer to
12824 the value list into static variables, and then recursively traversing the
12825 variables list, expanding iterators and such. */
12826
12827 static void
12828 resolve_data (gfc_data *d)
12829 {
12830
12831 if (resolve_data_variables (d->var) == FAILURE)
12832 return;
12833
12834 values.vnode = d->value;
12835 if (d->value == NULL)
12836 mpz_set_ui (values.left, 0);
12837 else
12838 mpz_set (values.left, d->value->repeat);
12839
12840 if (traverse_data_var (d->var, &d->where) == FAILURE)
12841 return;
12842
12843 /* At this point, we better not have any values left. */
12844
12845 if (next_data_value () == SUCCESS)
12846 gfc_error ("DATA statement at %L has more values than variables",
12847 &d->where);
12848 }
12849
12850
12851 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12852 accessed by host or use association, is a dummy argument to a pure function,
12853 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12854 is storage associated with any such variable, shall not be used in the
12855 following contexts: (clients of this function). */
12856
12857 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12858 procedure. Returns zero if assignment is OK, nonzero if there is a
12859 problem. */
12860 int
12861 gfc_impure_variable (gfc_symbol *sym)
12862 {
12863 gfc_symbol *proc;
12864 gfc_namespace *ns;
12865
12866 if (sym->attr.use_assoc || sym->attr.in_common)
12867 return 1;
12868
12869 /* Check if the symbol's ns is inside the pure procedure. */
12870 for (ns = gfc_current_ns; ns; ns = ns->parent)
12871 {
12872 if (ns == sym->ns)
12873 break;
12874 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12875 return 1;
12876 }
12877
12878 proc = sym->ns->proc_name;
12879 if (sym->attr.dummy && gfc_pure (proc)
12880 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12881 ||
12882 proc->attr.function))
12883 return 1;
12884
12885 /* TODO: Sort out what can be storage associated, if anything, and include
12886 it here. In principle equivalences should be scanned but it does not
12887 seem to be possible to storage associate an impure variable this way. */
12888 return 0;
12889 }
12890
12891
12892 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12893 current namespace is inside a pure procedure. */
12894
12895 int
12896 gfc_pure (gfc_symbol *sym)
12897 {
12898 symbol_attribute attr;
12899 gfc_namespace *ns;
12900
12901 if (sym == NULL)
12902 {
12903 /* Check if the current namespace or one of its parents
12904 belongs to a pure procedure. */
12905 for (ns = gfc_current_ns; ns; ns = ns->parent)
12906 {
12907 sym = ns->proc_name;
12908 if (sym == NULL)
12909 return 0;
12910 attr = sym->attr;
12911 if (attr.flavor == FL_PROCEDURE && attr.pure)
12912 return 1;
12913 }
12914 return 0;
12915 }
12916
12917 attr = sym->attr;
12918
12919 return attr.flavor == FL_PROCEDURE && attr.pure;
12920 }
12921
12922
12923 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
12924 checks if the current namespace is implicitly pure. Note that this
12925 function returns false for a PURE procedure. */
12926
12927 int
12928 gfc_implicit_pure (gfc_symbol *sym)
12929 {
12930 symbol_attribute attr;
12931
12932 if (sym == NULL)
12933 {
12934 /* Check if the current namespace is implicit_pure. */
12935 sym = gfc_current_ns->proc_name;
12936 if (sym == NULL)
12937 return 0;
12938 attr = sym->attr;
12939 if (attr.flavor == FL_PROCEDURE
12940 && attr.implicit_pure && !attr.pure)
12941 return 1;
12942 return 0;
12943 }
12944
12945 attr = sym->attr;
12946
12947 return attr.flavor == FL_PROCEDURE && attr.implicit_pure && !attr.pure;
12948 }
12949
12950
12951 /* Test whether the current procedure is elemental or not. */
12952
12953 int
12954 gfc_elemental (gfc_symbol *sym)
12955 {
12956 symbol_attribute attr;
12957
12958 if (sym == NULL)
12959 sym = gfc_current_ns->proc_name;
12960 if (sym == NULL)
12961 return 0;
12962 attr = sym->attr;
12963
12964 return attr.flavor == FL_PROCEDURE && attr.elemental;
12965 }
12966
12967
12968 /* Warn about unused labels. */
12969
12970 static void
12971 warn_unused_fortran_label (gfc_st_label *label)
12972 {
12973 if (label == NULL)
12974 return;
12975
12976 warn_unused_fortran_label (label->left);
12977
12978 if (label->defined == ST_LABEL_UNKNOWN)
12979 return;
12980
12981 switch (label->referenced)
12982 {
12983 case ST_LABEL_UNKNOWN:
12984 gfc_warning ("Label %d at %L defined but not used", label->value,
12985 &label->where);
12986 break;
12987
12988 case ST_LABEL_BAD_TARGET:
12989 gfc_warning ("Label %d at %L defined but cannot be used",
12990 label->value, &label->where);
12991 break;
12992
12993 default:
12994 break;
12995 }
12996
12997 warn_unused_fortran_label (label->right);
12998 }
12999
13000
13001 /* Returns the sequence type of a symbol or sequence. */
13002
13003 static seq_type
13004 sequence_type (gfc_typespec ts)
13005 {
13006 seq_type result;
13007 gfc_component *c;
13008
13009 switch (ts.type)
13010 {
13011 case BT_DERIVED:
13012
13013 if (ts.u.derived->components == NULL)
13014 return SEQ_NONDEFAULT;
13015
13016 result = sequence_type (ts.u.derived->components->ts);
13017 for (c = ts.u.derived->components->next; c; c = c->next)
13018 if (sequence_type (c->ts) != result)
13019 return SEQ_MIXED;
13020
13021 return result;
13022
13023 case BT_CHARACTER:
13024 if (ts.kind != gfc_default_character_kind)
13025 return SEQ_NONDEFAULT;
13026
13027 return SEQ_CHARACTER;
13028
13029 case BT_INTEGER:
13030 if (ts.kind != gfc_default_integer_kind)
13031 return SEQ_NONDEFAULT;
13032
13033 return SEQ_NUMERIC;
13034
13035 case BT_REAL:
13036 if (!(ts.kind == gfc_default_real_kind
13037 || ts.kind == gfc_default_double_kind))
13038 return SEQ_NONDEFAULT;
13039
13040 return SEQ_NUMERIC;
13041
13042 case BT_COMPLEX:
13043 if (ts.kind != gfc_default_complex_kind)
13044 return SEQ_NONDEFAULT;
13045
13046 return SEQ_NUMERIC;
13047
13048 case BT_LOGICAL:
13049 if (ts.kind != gfc_default_logical_kind)
13050 return SEQ_NONDEFAULT;
13051
13052 return SEQ_NUMERIC;
13053
13054 default:
13055 return SEQ_NONDEFAULT;
13056 }
13057 }
13058
13059
13060 /* Resolve derived type EQUIVALENCE object. */
13061
13062 static gfc_try
13063 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13064 {
13065 gfc_component *c = derived->components;
13066
13067 if (!derived)
13068 return SUCCESS;
13069
13070 /* Shall not be an object of nonsequence derived type. */
13071 if (!derived->attr.sequence)
13072 {
13073 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13074 "attribute to be an EQUIVALENCE object", sym->name,
13075 &e->where);
13076 return FAILURE;
13077 }
13078
13079 /* Shall not have allocatable components. */
13080 if (derived->attr.alloc_comp)
13081 {
13082 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13083 "components to be an EQUIVALENCE object",sym->name,
13084 &e->where);
13085 return FAILURE;
13086 }
13087
13088 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13089 {
13090 gfc_error ("Derived type variable '%s' at %L with default "
13091 "initialization cannot be in EQUIVALENCE with a variable "
13092 "in COMMON", sym->name, &e->where);
13093 return FAILURE;
13094 }
13095
13096 for (; c ; c = c->next)
13097 {
13098 if (c->ts.type == BT_DERIVED
13099 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
13100 return FAILURE;
13101
13102 /* Shall not be an object of sequence derived type containing a pointer
13103 in the structure. */
13104 if (c->attr.pointer)
13105 {
13106 gfc_error ("Derived type variable '%s' at %L with pointer "
13107 "component(s) cannot be an EQUIVALENCE object",
13108 sym->name, &e->where);
13109 return FAILURE;
13110 }
13111 }
13112 return SUCCESS;
13113 }
13114
13115
13116 /* Resolve equivalence object.
13117 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13118 an allocatable array, an object of nonsequence derived type, an object of
13119 sequence derived type containing a pointer at any level of component
13120 selection, an automatic object, a function name, an entry name, a result
13121 name, a named constant, a structure component, or a subobject of any of
13122 the preceding objects. A substring shall not have length zero. A
13123 derived type shall not have components with default initialization nor
13124 shall two objects of an equivalence group be initialized.
13125 Either all or none of the objects shall have an protected attribute.
13126 The simple constraints are done in symbol.c(check_conflict) and the rest
13127 are implemented here. */
13128
13129 static void
13130 resolve_equivalence (gfc_equiv *eq)
13131 {
13132 gfc_symbol *sym;
13133 gfc_symbol *first_sym;
13134 gfc_expr *e;
13135 gfc_ref *r;
13136 locus *last_where = NULL;
13137 seq_type eq_type, last_eq_type;
13138 gfc_typespec *last_ts;
13139 int object, cnt_protected;
13140 const char *msg;
13141
13142 last_ts = &eq->expr->symtree->n.sym->ts;
13143
13144 first_sym = eq->expr->symtree->n.sym;
13145
13146 cnt_protected = 0;
13147
13148 for (object = 1; eq; eq = eq->eq, object++)
13149 {
13150 e = eq->expr;
13151
13152 e->ts = e->symtree->n.sym->ts;
13153 /* match_varspec might not know yet if it is seeing
13154 array reference or substring reference, as it doesn't
13155 know the types. */
13156 if (e->ref && e->ref->type == REF_ARRAY)
13157 {
13158 gfc_ref *ref = e->ref;
13159 sym = e->symtree->n.sym;
13160
13161 if (sym->attr.dimension)
13162 {
13163 ref->u.ar.as = sym->as;
13164 ref = ref->next;
13165 }
13166
13167 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13168 if (e->ts.type == BT_CHARACTER
13169 && ref
13170 && ref->type == REF_ARRAY
13171 && ref->u.ar.dimen == 1
13172 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13173 && ref->u.ar.stride[0] == NULL)
13174 {
13175 gfc_expr *start = ref->u.ar.start[0];
13176 gfc_expr *end = ref->u.ar.end[0];
13177 void *mem = NULL;
13178
13179 /* Optimize away the (:) reference. */
13180 if (start == NULL && end == NULL)
13181 {
13182 if (e->ref == ref)
13183 e->ref = ref->next;
13184 else
13185 e->ref->next = ref->next;
13186 mem = ref;
13187 }
13188 else
13189 {
13190 ref->type = REF_SUBSTRING;
13191 if (start == NULL)
13192 start = gfc_get_int_expr (gfc_default_integer_kind,
13193 NULL, 1);
13194 ref->u.ss.start = start;
13195 if (end == NULL && e->ts.u.cl)
13196 end = gfc_copy_expr (e->ts.u.cl->length);
13197 ref->u.ss.end = end;
13198 ref->u.ss.length = e->ts.u.cl;
13199 e->ts.u.cl = NULL;
13200 }
13201 ref = ref->next;
13202 free (mem);
13203 }
13204
13205 /* Any further ref is an error. */
13206 if (ref)
13207 {
13208 gcc_assert (ref->type == REF_ARRAY);
13209 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13210 &ref->u.ar.where);
13211 continue;
13212 }
13213 }
13214
13215 if (gfc_resolve_expr (e) == FAILURE)
13216 continue;
13217
13218 sym = e->symtree->n.sym;
13219
13220 if (sym->attr.is_protected)
13221 cnt_protected++;
13222 if (cnt_protected > 0 && cnt_protected != object)
13223 {
13224 gfc_error ("Either all or none of the objects in the "
13225 "EQUIVALENCE set at %L shall have the "
13226 "PROTECTED attribute",
13227 &e->where);
13228 break;
13229 }
13230
13231 /* Shall not equivalence common block variables in a PURE procedure. */
13232 if (sym->ns->proc_name
13233 && sym->ns->proc_name->attr.pure
13234 && sym->attr.in_common)
13235 {
13236 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13237 "object in the pure procedure '%s'",
13238 sym->name, &e->where, sym->ns->proc_name->name);
13239 break;
13240 }
13241
13242 /* Shall not be a named constant. */
13243 if (e->expr_type == EXPR_CONSTANT)
13244 {
13245 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13246 "object", sym->name, &e->where);
13247 continue;
13248 }
13249
13250 if (e->ts.type == BT_DERIVED
13251 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13252 continue;
13253
13254 /* Check that the types correspond correctly:
13255 Note 5.28:
13256 A numeric sequence structure may be equivalenced to another sequence
13257 structure, an object of default integer type, default real type, double
13258 precision real type, default logical type such that components of the
13259 structure ultimately only become associated to objects of the same
13260 kind. A character sequence structure may be equivalenced to an object
13261 of default character kind or another character sequence structure.
13262 Other objects may be equivalenced only to objects of the same type and
13263 kind parameters. */
13264
13265 /* Identical types are unconditionally OK. */
13266 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13267 goto identical_types;
13268
13269 last_eq_type = sequence_type (*last_ts);
13270 eq_type = sequence_type (sym->ts);
13271
13272 /* Since the pair of objects is not of the same type, mixed or
13273 non-default sequences can be rejected. */
13274
13275 msg = "Sequence %s with mixed components in EQUIVALENCE "
13276 "statement at %L with different type objects";
13277 if ((object ==2
13278 && last_eq_type == SEQ_MIXED
13279 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13280 == FAILURE)
13281 || (eq_type == SEQ_MIXED
13282 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13283 &e->where) == FAILURE))
13284 continue;
13285
13286 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13287 "statement at %L with objects of different type";
13288 if ((object ==2
13289 && last_eq_type == SEQ_NONDEFAULT
13290 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13291 last_where) == FAILURE)
13292 || (eq_type == SEQ_NONDEFAULT
13293 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13294 &e->where) == FAILURE))
13295 continue;
13296
13297 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13298 "EQUIVALENCE statement at %L";
13299 if (last_eq_type == SEQ_CHARACTER
13300 && eq_type != SEQ_CHARACTER
13301 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13302 &e->where) == FAILURE)
13303 continue;
13304
13305 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13306 "EQUIVALENCE statement at %L";
13307 if (last_eq_type == SEQ_NUMERIC
13308 && eq_type != SEQ_NUMERIC
13309 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13310 &e->where) == FAILURE)
13311 continue;
13312
13313 identical_types:
13314 last_ts =&sym->ts;
13315 last_where = &e->where;
13316
13317 if (!e->ref)
13318 continue;
13319
13320 /* Shall not be an automatic array. */
13321 if (e->ref->type == REF_ARRAY
13322 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13323 {
13324 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13325 "an EQUIVALENCE object", sym->name, &e->where);
13326 continue;
13327 }
13328
13329 r = e->ref;
13330 while (r)
13331 {
13332 /* Shall not be a structure component. */
13333 if (r->type == REF_COMPONENT)
13334 {
13335 gfc_error ("Structure component '%s' at %L cannot be an "
13336 "EQUIVALENCE object",
13337 r->u.c.component->name, &e->where);
13338 break;
13339 }
13340
13341 /* A substring shall not have length zero. */
13342 if (r->type == REF_SUBSTRING)
13343 {
13344 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13345 {
13346 gfc_error ("Substring at %L has length zero",
13347 &r->u.ss.start->where);
13348 break;
13349 }
13350 }
13351 r = r->next;
13352 }
13353 }
13354 }
13355
13356
13357 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13358
13359 static void
13360 resolve_fntype (gfc_namespace *ns)
13361 {
13362 gfc_entry_list *el;
13363 gfc_symbol *sym;
13364
13365 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13366 return;
13367
13368 /* If there are any entries, ns->proc_name is the entry master
13369 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13370 if (ns->entries)
13371 sym = ns->entries->sym;
13372 else
13373 sym = ns->proc_name;
13374 if (sym->result == sym
13375 && sym->ts.type == BT_UNKNOWN
13376 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13377 && !sym->attr.untyped)
13378 {
13379 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13380 sym->name, &sym->declared_at);
13381 sym->attr.untyped = 1;
13382 }
13383
13384 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13385 && !sym->attr.contained
13386 && !gfc_check_symbol_access (sym->ts.u.derived)
13387 && gfc_check_symbol_access (sym))
13388 {
13389 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13390 "%L of PRIVATE type '%s'", sym->name,
13391 &sym->declared_at, sym->ts.u.derived->name);
13392 }
13393
13394 if (ns->entries)
13395 for (el = ns->entries->next; el; el = el->next)
13396 {
13397 if (el->sym->result == el->sym
13398 && el->sym->ts.type == BT_UNKNOWN
13399 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13400 && !el->sym->attr.untyped)
13401 {
13402 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13403 el->sym->name, &el->sym->declared_at);
13404 el->sym->attr.untyped = 1;
13405 }
13406 }
13407 }
13408
13409
13410 /* 12.3.2.1.1 Defined operators. */
13411
13412 static gfc_try
13413 check_uop_procedure (gfc_symbol *sym, locus where)
13414 {
13415 gfc_formal_arglist *formal;
13416
13417 if (!sym->attr.function)
13418 {
13419 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13420 sym->name, &where);
13421 return FAILURE;
13422 }
13423
13424 if (sym->ts.type == BT_CHARACTER
13425 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13426 && !(sym->result && sym->result->ts.u.cl
13427 && sym->result->ts.u.cl->length))
13428 {
13429 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13430 "character length", sym->name, &where);
13431 return FAILURE;
13432 }
13433
13434 formal = sym->formal;
13435 if (!formal || !formal->sym)
13436 {
13437 gfc_error ("User operator procedure '%s' at %L must have at least "
13438 "one argument", sym->name, &where);
13439 return FAILURE;
13440 }
13441
13442 if (formal->sym->attr.intent != INTENT_IN)
13443 {
13444 gfc_error ("First argument of operator interface at %L must be "
13445 "INTENT(IN)", &where);
13446 return FAILURE;
13447 }
13448
13449 if (formal->sym->attr.optional)
13450 {
13451 gfc_error ("First argument of operator interface at %L cannot be "
13452 "optional", &where);
13453 return FAILURE;
13454 }
13455
13456 formal = formal->next;
13457 if (!formal || !formal->sym)
13458 return SUCCESS;
13459
13460 if (formal->sym->attr.intent != INTENT_IN)
13461 {
13462 gfc_error ("Second argument of operator interface at %L must be "
13463 "INTENT(IN)", &where);
13464 return FAILURE;
13465 }
13466
13467 if (formal->sym->attr.optional)
13468 {
13469 gfc_error ("Second argument of operator interface at %L cannot be "
13470 "optional", &where);
13471 return FAILURE;
13472 }
13473
13474 if (formal->next)
13475 {
13476 gfc_error ("Operator interface at %L must have, at most, two "
13477 "arguments", &where);
13478 return FAILURE;
13479 }
13480
13481 return SUCCESS;
13482 }
13483
13484 static void
13485 gfc_resolve_uops (gfc_symtree *symtree)
13486 {
13487 gfc_interface *itr;
13488
13489 if (symtree == NULL)
13490 return;
13491
13492 gfc_resolve_uops (symtree->left);
13493 gfc_resolve_uops (symtree->right);
13494
13495 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13496 check_uop_procedure (itr->sym, itr->sym->declared_at);
13497 }
13498
13499
13500 /* Examine all of the expressions associated with a program unit,
13501 assign types to all intermediate expressions, make sure that all
13502 assignments are to compatible types and figure out which names
13503 refer to which functions or subroutines. It doesn't check code
13504 block, which is handled by resolve_code. */
13505
13506 static void
13507 resolve_types (gfc_namespace *ns)
13508 {
13509 gfc_namespace *n;
13510 gfc_charlen *cl;
13511 gfc_data *d;
13512 gfc_equiv *eq;
13513 gfc_namespace* old_ns = gfc_current_ns;
13514
13515 /* Check that all IMPLICIT types are ok. */
13516 if (!ns->seen_implicit_none)
13517 {
13518 unsigned letter;
13519 for (letter = 0; letter != GFC_LETTERS; ++letter)
13520 if (ns->set_flag[letter]
13521 && resolve_typespec_used (&ns->default_type[letter],
13522 &ns->implicit_loc[letter],
13523 NULL) == FAILURE)
13524 return;
13525 }
13526
13527 gfc_current_ns = ns;
13528
13529 resolve_entries (ns);
13530
13531 resolve_common_vars (ns->blank_common.head, false);
13532 resolve_common_blocks (ns->common_root);
13533
13534 resolve_contained_functions (ns);
13535
13536 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13537
13538 for (cl = ns->cl_list; cl; cl = cl->next)
13539 resolve_charlen (cl);
13540
13541 gfc_traverse_ns (ns, resolve_symbol);
13542
13543 resolve_fntype (ns);
13544
13545 for (n = ns->contained; n; n = n->sibling)
13546 {
13547 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13548 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13549 "also be PURE", n->proc_name->name,
13550 &n->proc_name->declared_at);
13551
13552 resolve_types (n);
13553 }
13554
13555 forall_flag = 0;
13556 gfc_check_interfaces (ns);
13557
13558 gfc_traverse_ns (ns, resolve_values);
13559
13560 if (ns->save_all)
13561 gfc_save_all (ns);
13562
13563 iter_stack = NULL;
13564 for (d = ns->data; d; d = d->next)
13565 resolve_data (d);
13566
13567 iter_stack = NULL;
13568 gfc_traverse_ns (ns, gfc_formalize_init_value);
13569
13570 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13571
13572 if (ns->common_root != NULL)
13573 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13574
13575 for (eq = ns->equiv; eq; eq = eq->next)
13576 resolve_equivalence (eq);
13577
13578 /* Warn about unused labels. */
13579 if (warn_unused_label)
13580 warn_unused_fortran_label (ns->st_labels);
13581
13582 gfc_resolve_uops (ns->uop_root);
13583
13584 gfc_current_ns = old_ns;
13585 }
13586
13587
13588 /* Call resolve_code recursively. */
13589
13590 static void
13591 resolve_codes (gfc_namespace *ns)
13592 {
13593 gfc_namespace *n;
13594 bitmap_obstack old_obstack;
13595
13596 if (ns->resolved == 1)
13597 return;
13598
13599 for (n = ns->contained; n; n = n->sibling)
13600 resolve_codes (n);
13601
13602 gfc_current_ns = ns;
13603
13604 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13605 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13606 cs_base = NULL;
13607
13608 /* Set to an out of range value. */
13609 current_entry_id = -1;
13610
13611 old_obstack = labels_obstack;
13612 bitmap_obstack_initialize (&labels_obstack);
13613
13614 resolve_code (ns->code, ns);
13615
13616 bitmap_obstack_release (&labels_obstack);
13617 labels_obstack = old_obstack;
13618 }
13619
13620
13621 /* This function is called after a complete program unit has been compiled.
13622 Its purpose is to examine all of the expressions associated with a program
13623 unit, assign types to all intermediate expressions, make sure that all
13624 assignments are to compatible types and figure out which names refer to
13625 which functions or subroutines. */
13626
13627 void
13628 gfc_resolve (gfc_namespace *ns)
13629 {
13630 gfc_namespace *old_ns;
13631 code_stack *old_cs_base;
13632
13633 if (ns->resolved)
13634 return;
13635
13636 ns->resolved = -1;
13637 old_ns = gfc_current_ns;
13638 old_cs_base = cs_base;
13639
13640 resolve_types (ns);
13641 resolve_codes (ns);
13642
13643 gfc_current_ns = old_ns;
13644 cs_base = old_cs_base;
13645 ns->resolved = 1;
13646
13647 gfc_run_passes (ns);
13648 }