resolve.c (resolve_symbol): Fix coarray var decl check.
[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 && sym->attr.flavor != FL_PROCEDURE)
320 {
321 sym->as->type = AS_ASSUMED_SHAPE;
322 for (i = 0; i < sym->as->rank; i++)
323 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
324 NULL, 1);
325 }
326
327 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
328 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
329 || sym->attr.optional)
330 {
331 proc->attr.always_explicit = 1;
332 if (proc->result)
333 proc->result->attr.always_explicit = 1;
334 }
335
336 /* If the flavor is unknown at this point, it has to be a variable.
337 A procedure specification would have already set the type. */
338
339 if (sym->attr.flavor == FL_UNKNOWN)
340 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
341
342 if (gfc_pure (proc) && !sym->attr.pointer
343 && sym->attr.flavor != FL_PROCEDURE)
344 {
345 if (proc->attr.function && sym->attr.intent != INTENT_IN)
346 {
347 if (sym->attr.value)
348 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
349 "of pure function '%s' at %L with VALUE "
350 "attribute but without INTENT(IN)", sym->name,
351 proc->name, &sym->declared_at);
352 else
353 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
354 "INTENT(IN) or VALUE", sym->name, proc->name,
355 &sym->declared_at);
356 }
357
358 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
359 {
360 if (sym->attr.value)
361 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
362 "of pure subroutine '%s' at %L with VALUE "
363 "attribute but without INTENT", sym->name,
364 proc->name, &sym->declared_at);
365 else
366 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
367 "have its INTENT specified or have the VALUE "
368 "attribute", sym->name, proc->name, &sym->declared_at);
369 }
370 }
371
372 if (proc->attr.implicit_pure && !sym->attr.pointer
373 && sym->attr.flavor != FL_PROCEDURE)
374 {
375 if (proc->attr.function && sym->attr.intent != INTENT_IN)
376 proc->attr.implicit_pure = 0;
377
378 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
379 proc->attr.implicit_pure = 0;
380 }
381
382 if (gfc_elemental (proc))
383 {
384 /* F2008, C1289. */
385 if (sym->attr.codimension)
386 {
387 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
388 "procedure", sym->name, &sym->declared_at);
389 continue;
390 }
391
392 if (sym->as != NULL)
393 {
394 gfc_error ("Argument '%s' of elemental procedure at %L must "
395 "be scalar", sym->name, &sym->declared_at);
396 continue;
397 }
398
399 if (sym->attr.allocatable)
400 {
401 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
402 "have the ALLOCATABLE attribute", sym->name,
403 &sym->declared_at);
404 continue;
405 }
406
407 if (sym->attr.pointer)
408 {
409 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
410 "have the POINTER attribute", sym->name,
411 &sym->declared_at);
412 continue;
413 }
414
415 if (sym->attr.flavor == FL_PROCEDURE)
416 {
417 gfc_error ("Dummy procedure '%s' not allowed in elemental "
418 "procedure '%s' at %L", sym->name, proc->name,
419 &sym->declared_at);
420 continue;
421 }
422
423 if (sym->attr.intent == INTENT_UNKNOWN)
424 {
425 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
426 "have its INTENT specified", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
429 }
430 }
431
432 /* Each dummy shall be specified to be scalar. */
433 if (proc->attr.proc == PROC_ST_FUNCTION)
434 {
435 if (sym->as != NULL)
436 {
437 gfc_error ("Argument '%s' of statement function at %L must "
438 "be scalar", sym->name, &sym->declared_at);
439 continue;
440 }
441
442 if (sym->ts.type == BT_CHARACTER)
443 {
444 gfc_charlen *cl = sym->ts.u.cl;
445 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
446 {
447 gfc_error ("Character-valued argument '%s' of statement "
448 "function at %L must have constant length",
449 sym->name, &sym->declared_at);
450 continue;
451 }
452 }
453 }
454 }
455 formal_arg_flag = 0;
456 }
457
458
459 /* Work function called when searching for symbols that have argument lists
460 associated with them. */
461
462 static void
463 find_arglists (gfc_symbol *sym)
464 {
465 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
466 return;
467
468 resolve_formal_arglist (sym);
469 }
470
471
472 /* Given a namespace, resolve all formal argument lists within the namespace.
473 */
474
475 static void
476 resolve_formal_arglists (gfc_namespace *ns)
477 {
478 if (ns == NULL)
479 return;
480
481 gfc_traverse_ns (ns, find_arglists);
482 }
483
484
485 static void
486 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
487 {
488 gfc_try t;
489
490 /* If this namespace is not a function or an entry master function,
491 ignore it. */
492 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
493 || sym->attr.entry_master)
494 return;
495
496 /* Try to find out of what the return type is. */
497 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
498 {
499 t = gfc_set_default_type (sym->result, 0, ns);
500
501 if (t == FAILURE && !sym->result->attr.untyped)
502 {
503 if (sym->result == sym)
504 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
505 sym->name, &sym->declared_at);
506 else if (!sym->result->attr.proc_pointer)
507 gfc_error ("Result '%s' of contained function '%s' at %L has "
508 "no IMPLICIT type", sym->result->name, sym->name,
509 &sym->result->declared_at);
510 sym->result->attr.untyped = 1;
511 }
512 }
513
514 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
515 type, lists the only ways a character length value of * can be used:
516 dummy arguments of procedures, named constants, and function results
517 in external functions. Internal function results and results of module
518 procedures are not on this list, ergo, not permitted. */
519
520 if (sym->result->ts.type == BT_CHARACTER)
521 {
522 gfc_charlen *cl = sym->result->ts.u.cl;
523 if ((!cl || !cl->length) && !sym->result->ts.deferred)
524 {
525 /* See if this is a module-procedure and adapt error message
526 accordingly. */
527 bool module_proc;
528 gcc_assert (ns->parent && ns->parent->proc_name);
529 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
530
531 gfc_error ("Character-valued %s '%s' at %L must not be"
532 " assumed length",
533 module_proc ? _("module procedure")
534 : _("internal function"),
535 sym->name, &sym->declared_at);
536 }
537 }
538 }
539
540
541 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
542 introduce duplicates. */
543
544 static void
545 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
546 {
547 gfc_formal_arglist *f, *new_arglist;
548 gfc_symbol *new_sym;
549
550 for (; new_args != NULL; new_args = new_args->next)
551 {
552 new_sym = new_args->sym;
553 /* See if this arg is already in the formal argument list. */
554 for (f = proc->formal; f; f = f->next)
555 {
556 if (new_sym == f->sym)
557 break;
558 }
559
560 if (f)
561 continue;
562
563 /* Add a new argument. Argument order is not important. */
564 new_arglist = gfc_get_formal_arglist ();
565 new_arglist->sym = new_sym;
566 new_arglist->next = proc->formal;
567 proc->formal = new_arglist;
568 }
569 }
570
571
572 /* Flag the arguments that are not present in all entries. */
573
574 static void
575 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
576 {
577 gfc_formal_arglist *f, *head;
578 head = new_args;
579
580 for (f = proc->formal; f; f = f->next)
581 {
582 if (f->sym == NULL)
583 continue;
584
585 for (new_args = head; new_args; new_args = new_args->next)
586 {
587 if (new_args->sym == f->sym)
588 break;
589 }
590
591 if (new_args)
592 continue;
593
594 f->sym->attr.not_always_present = 1;
595 }
596 }
597
598
599 /* Resolve alternate entry points. If a symbol has multiple entry points we
600 create a new master symbol for the main routine, and turn the existing
601 symbol into an entry point. */
602
603 static void
604 resolve_entries (gfc_namespace *ns)
605 {
606 gfc_namespace *old_ns;
607 gfc_code *c;
608 gfc_symbol *proc;
609 gfc_entry_list *el;
610 char name[GFC_MAX_SYMBOL_LEN + 1];
611 static int master_count = 0;
612
613 if (ns->proc_name == NULL)
614 return;
615
616 /* No need to do anything if this procedure doesn't have alternate entry
617 points. */
618 if (!ns->entries)
619 return;
620
621 /* We may already have resolved alternate entry points. */
622 if (ns->proc_name->attr.entry_master)
623 return;
624
625 /* If this isn't a procedure something has gone horribly wrong. */
626 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
627
628 /* Remember the current namespace. */
629 old_ns = gfc_current_ns;
630
631 gfc_current_ns = ns;
632
633 /* Add the main entry point to the list of entry points. */
634 el = gfc_get_entry_list ();
635 el->sym = ns->proc_name;
636 el->id = 0;
637 el->next = ns->entries;
638 ns->entries = el;
639 ns->proc_name->attr.entry = 1;
640
641 /* If it is a module function, it needs to be in the right namespace
642 so that gfc_get_fake_result_decl can gather up the results. The
643 need for this arose in get_proc_name, where these beasts were
644 left in their own namespace, to keep prior references linked to
645 the entry declaration.*/
646 if (ns->proc_name->attr.function
647 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
648 el->sym->ns = ns;
649
650 /* Do the same for entries where the master is not a module
651 procedure. These are retained in the module namespace because
652 of the module procedure declaration. */
653 for (el = el->next; el; el = el->next)
654 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
655 && el->sym->attr.mod_proc)
656 el->sym->ns = ns;
657 el = ns->entries;
658
659 /* Add an entry statement for it. */
660 c = gfc_get_code ();
661 c->op = EXEC_ENTRY;
662 c->ext.entry = el;
663 c->next = ns->code;
664 ns->code = c;
665
666 /* Create a new symbol for the master function. */
667 /* Give the internal function a unique name (within this file).
668 Also include the function name so the user has some hope of figuring
669 out what is going on. */
670 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
671 master_count++, ns->proc_name->name);
672 gfc_get_ha_symbol (name, &proc);
673 gcc_assert (proc != NULL);
674
675 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
676 if (ns->proc_name->attr.subroutine)
677 gfc_add_subroutine (&proc->attr, proc->name, NULL);
678 else
679 {
680 gfc_symbol *sym;
681 gfc_typespec *ts, *fts;
682 gfc_array_spec *as, *fas;
683 gfc_add_function (&proc->attr, proc->name, NULL);
684 proc->result = proc;
685 fas = ns->entries->sym->as;
686 fas = fas ? fas : ns->entries->sym->result->as;
687 fts = &ns->entries->sym->result->ts;
688 if (fts->type == BT_UNKNOWN)
689 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
690 for (el = ns->entries->next; el; el = el->next)
691 {
692 ts = &el->sym->result->ts;
693 as = el->sym->as;
694 as = as ? as : el->sym->result->as;
695 if (ts->type == BT_UNKNOWN)
696 ts = gfc_get_default_type (el->sym->result->name, NULL);
697
698 if (! gfc_compare_types (ts, fts)
699 || (el->sym->result->attr.dimension
700 != ns->entries->sym->result->attr.dimension)
701 || (el->sym->result->attr.pointer
702 != ns->entries->sym->result->attr.pointer))
703 break;
704 else if (as && fas && ns->entries->sym->result != el->sym->result
705 && gfc_compare_array_spec (as, fas) == 0)
706 gfc_error ("Function %s at %L has entries with mismatched "
707 "array specifications", ns->entries->sym->name,
708 &ns->entries->sym->declared_at);
709 /* The characteristics need to match and thus both need to have
710 the same string length, i.e. both len=*, or both len=4.
711 Having both len=<variable> is also possible, but difficult to
712 check at compile time. */
713 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
714 && (((ts->u.cl->length && !fts->u.cl->length)
715 ||(!ts->u.cl->length && fts->u.cl->length))
716 || (ts->u.cl->length
717 && ts->u.cl->length->expr_type
718 != fts->u.cl->length->expr_type)
719 || (ts->u.cl->length
720 && ts->u.cl->length->expr_type == EXPR_CONSTANT
721 && mpz_cmp (ts->u.cl->length->value.integer,
722 fts->u.cl->length->value.integer) != 0)))
723 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
724 "entries returning variables of different "
725 "string lengths", ns->entries->sym->name,
726 &ns->entries->sym->declared_at);
727 }
728
729 if (el == NULL)
730 {
731 sym = ns->entries->sym->result;
732 /* All result types the same. */
733 proc->ts = *fts;
734 if (sym->attr.dimension)
735 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
736 if (sym->attr.pointer)
737 gfc_add_pointer (&proc->attr, NULL);
738 }
739 else
740 {
741 /* Otherwise the result will be passed through a union by
742 reference. */
743 proc->attr.mixed_entry_master = 1;
744 for (el = ns->entries; el; el = el->next)
745 {
746 sym = el->sym->result;
747 if (sym->attr.dimension)
748 {
749 if (el == ns->entries)
750 gfc_error ("FUNCTION result %s can't be an array in "
751 "FUNCTION %s at %L", sym->name,
752 ns->entries->sym->name, &sym->declared_at);
753 else
754 gfc_error ("ENTRY result %s can't be an array in "
755 "FUNCTION %s at %L", sym->name,
756 ns->entries->sym->name, &sym->declared_at);
757 }
758 else if (sym->attr.pointer)
759 {
760 if (el == ns->entries)
761 gfc_error ("FUNCTION result %s can't be a POINTER in "
762 "FUNCTION %s at %L", sym->name,
763 ns->entries->sym->name, &sym->declared_at);
764 else
765 gfc_error ("ENTRY result %s can't be a POINTER in "
766 "FUNCTION %s at %L", sym->name,
767 ns->entries->sym->name, &sym->declared_at);
768 }
769 else
770 {
771 ts = &sym->ts;
772 if (ts->type == BT_UNKNOWN)
773 ts = gfc_get_default_type (sym->name, NULL);
774 switch (ts->type)
775 {
776 case BT_INTEGER:
777 if (ts->kind == gfc_default_integer_kind)
778 sym = NULL;
779 break;
780 case BT_REAL:
781 if (ts->kind == gfc_default_real_kind
782 || ts->kind == gfc_default_double_kind)
783 sym = NULL;
784 break;
785 case BT_COMPLEX:
786 if (ts->kind == gfc_default_complex_kind)
787 sym = NULL;
788 break;
789 case BT_LOGICAL:
790 if (ts->kind == gfc_default_logical_kind)
791 sym = NULL;
792 break;
793 case BT_UNKNOWN:
794 /* We will issue error elsewhere. */
795 sym = NULL;
796 break;
797 default:
798 break;
799 }
800 if (sym)
801 {
802 if (el == ns->entries)
803 gfc_error ("FUNCTION result %s can't be of type %s "
804 "in FUNCTION %s at %L", sym->name,
805 gfc_typename (ts), ns->entries->sym->name,
806 &sym->declared_at);
807 else
808 gfc_error ("ENTRY result %s can't be of type %s "
809 "in FUNCTION %s at %L", sym->name,
810 gfc_typename (ts), ns->entries->sym->name,
811 &sym->declared_at);
812 }
813 }
814 }
815 }
816 }
817 proc->attr.access = ACCESS_PRIVATE;
818 proc->attr.entry_master = 1;
819
820 /* Merge all the entry point arguments. */
821 for (el = ns->entries; el; el = el->next)
822 merge_argument_lists (proc, el->sym->formal);
823
824 /* Check the master formal arguments for any that are not
825 present in all entry points. */
826 for (el = ns->entries; el; el = el->next)
827 check_argument_lists (proc, el->sym->formal);
828
829 /* Use the master function for the function body. */
830 ns->proc_name = proc;
831
832 /* Finalize the new symbols. */
833 gfc_commit_symbols ();
834
835 /* Restore the original namespace. */
836 gfc_current_ns = old_ns;
837 }
838
839
840 /* Resolve common variables. */
841 static void
842 resolve_common_vars (gfc_symbol *sym, bool named_common)
843 {
844 gfc_symbol *csym = sym;
845
846 for (; csym; csym = csym->common_next)
847 {
848 if (csym->value || csym->attr.data)
849 {
850 if (!csym->ns->is_block_data)
851 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
852 "but only in BLOCK DATA initialization is "
853 "allowed", csym->name, &csym->declared_at);
854 else if (!named_common)
855 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
856 "in a blank COMMON but initialization is only "
857 "allowed in named common blocks", csym->name,
858 &csym->declared_at);
859 }
860
861 if (csym->ts.type != BT_DERIVED)
862 continue;
863
864 if (!(csym->ts.u.derived->attr.sequence
865 || csym->ts.u.derived->attr.is_bind_c))
866 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
867 "has neither the SEQUENCE nor the BIND(C) "
868 "attribute", csym->name, &csym->declared_at);
869 if (csym->ts.u.derived->attr.alloc_comp)
870 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
871 "has an ultimate component that is "
872 "allocatable", csym->name, &csym->declared_at);
873 if (gfc_has_default_initializer (csym->ts.u.derived))
874 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
875 "may not have default initializer", csym->name,
876 &csym->declared_at);
877
878 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
879 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
880 }
881 }
882
883 /* Resolve common blocks. */
884 static void
885 resolve_common_blocks (gfc_symtree *common_root)
886 {
887 gfc_symbol *sym;
888
889 if (common_root == NULL)
890 return;
891
892 if (common_root->left)
893 resolve_common_blocks (common_root->left);
894 if (common_root->right)
895 resolve_common_blocks (common_root->right);
896
897 resolve_common_vars (common_root->n.common->head, true);
898
899 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
900 if (sym == NULL)
901 return;
902
903 if (sym->attr.flavor == FL_PARAMETER)
904 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
905 sym->name, &common_root->n.common->where, &sym->declared_at);
906
907 if (sym->attr.intrinsic)
908 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
909 sym->name, &common_root->n.common->where);
910 else if (sym->attr.result
911 || gfc_is_function_return_value (sym, gfc_current_ns))
912 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
913 "that is also a function result", sym->name,
914 &common_root->n.common->where);
915 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
916 && sym->attr.proc != PROC_ST_FUNCTION)
917 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
918 "that is also a global procedure", sym->name,
919 &common_root->n.common->where);
920 }
921
922
923 /* Resolve contained function types. Because contained functions can call one
924 another, they have to be worked out before any of the contained procedures
925 can be resolved.
926
927 The good news is that if a function doesn't already have a type, the only
928 way it can get one is through an IMPLICIT type or a RESULT variable, because
929 by definition contained functions are contained namespace they're contained
930 in, not in a sibling or parent namespace. */
931
932 static void
933 resolve_contained_functions (gfc_namespace *ns)
934 {
935 gfc_namespace *child;
936 gfc_entry_list *el;
937
938 resolve_formal_arglists (ns);
939
940 for (child = ns->contained; child; child = child->sibling)
941 {
942 /* Resolve alternate entry points first. */
943 resolve_entries (child);
944
945 /* Then check function return types. */
946 resolve_contained_fntype (child->proc_name, child);
947 for (el = child->entries; el; el = el->next)
948 resolve_contained_fntype (el->sym, child);
949 }
950 }
951
952
953 /* Resolve all of the elements of a structure constructor and make sure that
954 the types are correct. The 'init' flag indicates that the given
955 constructor is an initializer. */
956
957 static gfc_try
958 resolve_structure_cons (gfc_expr *expr, int init)
959 {
960 gfc_constructor *cons;
961 gfc_component *comp;
962 gfc_try t;
963 symbol_attribute a;
964
965 t = SUCCESS;
966
967 if (expr->ts.type == BT_DERIVED)
968 resolve_symbol (expr->ts.u.derived);
969
970 cons = gfc_constructor_first (expr->value.constructor);
971 /* A constructor may have references if it is the result of substituting a
972 parameter variable. In this case we just pull out the component we
973 want. */
974 if (expr->ref)
975 comp = expr->ref->u.c.sym->components;
976 else
977 comp = expr->ts.u.derived->components;
978
979 /* See if the user is trying to invoke a structure constructor for one of
980 the iso_c_binding derived types. */
981 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
982 && expr->ts.u.derived->ts.is_iso_c && cons
983 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
984 {
985 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
986 expr->ts.u.derived->name, &(expr->where));
987 return FAILURE;
988 }
989
990 /* Return if structure constructor is c_null_(fun)prt. */
991 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
992 && expr->ts.u.derived->ts.is_iso_c && cons
993 && cons->expr && cons->expr->expr_type == EXPR_NULL)
994 return SUCCESS;
995
996 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
997 {
998 int rank;
999
1000 if (!cons->expr)
1001 continue;
1002
1003 if (gfc_resolve_expr (cons->expr) == FAILURE)
1004 {
1005 t = FAILURE;
1006 continue;
1007 }
1008
1009 rank = comp->as ? comp->as->rank : 0;
1010 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1011 && (comp->attr.allocatable || cons->expr->rank))
1012 {
1013 gfc_error ("The rank of the element in the derived type "
1014 "constructor at %L does not match that of the "
1015 "component (%d/%d)", &cons->expr->where,
1016 cons->expr->rank, rank);
1017 t = FAILURE;
1018 }
1019
1020 /* If we don't have the right type, try to convert it. */
1021
1022 if (!comp->attr.proc_pointer &&
1023 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1024 {
1025 t = FAILURE;
1026 if (strcmp (comp->name, "_extends") == 0)
1027 {
1028 /* Can afford to be brutal with the _extends initializer.
1029 The derived type can get lost because it is PRIVATE
1030 but it is not usage constrained by the standard. */
1031 cons->expr->ts = comp->ts;
1032 t = SUCCESS;
1033 }
1034 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1035 gfc_error ("The element in the derived type constructor at %L, "
1036 "for pointer component '%s', is %s but should be %s",
1037 &cons->expr->where, comp->name,
1038 gfc_basic_typename (cons->expr->ts.type),
1039 gfc_basic_typename (comp->ts.type));
1040 else
1041 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1042 }
1043
1044 /* For strings, the length of the constructor should be the same as
1045 the one of the structure, ensure this if the lengths are known at
1046 compile time and when we are dealing with PARAMETER or structure
1047 constructors. */
1048 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1049 && comp->ts.u.cl->length
1050 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1051 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1052 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1053 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1054 comp->ts.u.cl->length->value.integer) != 0)
1055 {
1056 if (cons->expr->expr_type == EXPR_VARIABLE
1057 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1058 {
1059 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1060 to make use of the gfc_resolve_character_array_constructor
1061 machinery. The expression is later simplified away to
1062 an array of string literals. */
1063 gfc_expr *para = cons->expr;
1064 cons->expr = gfc_get_expr ();
1065 cons->expr->ts = para->ts;
1066 cons->expr->where = para->where;
1067 cons->expr->expr_type = EXPR_ARRAY;
1068 cons->expr->rank = para->rank;
1069 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1070 gfc_constructor_append_expr (&cons->expr->value.constructor,
1071 para, &cons->expr->where);
1072 }
1073 if (cons->expr->expr_type == EXPR_ARRAY)
1074 {
1075 gfc_constructor *p;
1076 p = gfc_constructor_first (cons->expr->value.constructor);
1077 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1078 {
1079 gfc_charlen *cl, *cl2;
1080
1081 cl2 = NULL;
1082 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1083 {
1084 if (cl == cons->expr->ts.u.cl)
1085 break;
1086 cl2 = cl;
1087 }
1088
1089 gcc_assert (cl);
1090
1091 if (cl2)
1092 cl2->next = cl->next;
1093
1094 gfc_free_expr (cl->length);
1095 free (cl);
1096 }
1097
1098 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1099 cons->expr->ts.u.cl->length_from_typespec = true;
1100 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1101 gfc_resolve_character_array_constructor (cons->expr);
1102 }
1103 }
1104
1105 if (cons->expr->expr_type == EXPR_NULL
1106 && !(comp->attr.pointer || comp->attr.allocatable
1107 || comp->attr.proc_pointer
1108 || (comp->ts.type == BT_CLASS
1109 && (CLASS_DATA (comp)->attr.class_pointer
1110 || CLASS_DATA (comp)->attr.allocatable))))
1111 {
1112 t = FAILURE;
1113 gfc_error ("The NULL in the derived type constructor at %L is "
1114 "being applied to component '%s', which is neither "
1115 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1116 comp->name);
1117 }
1118
1119 if (!comp->attr.pointer || comp->attr.proc_pointer
1120 || cons->expr->expr_type == EXPR_NULL)
1121 continue;
1122
1123 a = gfc_expr_attr (cons->expr);
1124
1125 if (!a.pointer && !a.target)
1126 {
1127 t = FAILURE;
1128 gfc_error ("The element in the derived type constructor at %L, "
1129 "for pointer component '%s' should be a POINTER or "
1130 "a TARGET", &cons->expr->where, comp->name);
1131 }
1132
1133 if (init)
1134 {
1135 /* F08:C461. Additional checks for pointer initialization. */
1136 if (a.allocatable)
1137 {
1138 t = FAILURE;
1139 gfc_error ("Pointer initialization target at %L "
1140 "must not be ALLOCATABLE ", &cons->expr->where);
1141 }
1142 if (!a.save)
1143 {
1144 t = FAILURE;
1145 gfc_error ("Pointer initialization target at %L "
1146 "must have the SAVE attribute", &cons->expr->where);
1147 }
1148 }
1149
1150 /* F2003, C1272 (3). */
1151 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1152 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1153 || gfc_is_coindexed (cons->expr)))
1154 {
1155 t = FAILURE;
1156 gfc_error ("Invalid expression in the derived type constructor for "
1157 "pointer component '%s' at %L in PURE procedure",
1158 comp->name, &cons->expr->where);
1159 }
1160
1161 if (gfc_implicit_pure (NULL)
1162 && cons->expr->expr_type == EXPR_VARIABLE
1163 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1164 || gfc_is_coindexed (cons->expr)))
1165 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1166
1167 }
1168
1169 return t;
1170 }
1171
1172
1173 /****************** Expression name resolution ******************/
1174
1175 /* Returns 0 if a symbol was not declared with a type or
1176 attribute declaration statement, nonzero otherwise. */
1177
1178 static int
1179 was_declared (gfc_symbol *sym)
1180 {
1181 symbol_attribute a;
1182
1183 a = sym->attr;
1184
1185 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1186 return 1;
1187
1188 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1189 || a.optional || a.pointer || a.save || a.target || a.volatile_
1190 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1191 || a.asynchronous || a.codimension)
1192 return 1;
1193
1194 return 0;
1195 }
1196
1197
1198 /* Determine if a symbol is generic or not. */
1199
1200 static int
1201 generic_sym (gfc_symbol *sym)
1202 {
1203 gfc_symbol *s;
1204
1205 if (sym->attr.generic ||
1206 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1207 return 1;
1208
1209 if (was_declared (sym) || sym->ns->parent == NULL)
1210 return 0;
1211
1212 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1213
1214 if (s != NULL)
1215 {
1216 if (s == sym)
1217 return 0;
1218 else
1219 return generic_sym (s);
1220 }
1221
1222 return 0;
1223 }
1224
1225
1226 /* Determine if a symbol is specific or not. */
1227
1228 static int
1229 specific_sym (gfc_symbol *sym)
1230 {
1231 gfc_symbol *s;
1232
1233 if (sym->attr.if_source == IFSRC_IFBODY
1234 || sym->attr.proc == PROC_MODULE
1235 || sym->attr.proc == PROC_INTERNAL
1236 || sym->attr.proc == PROC_ST_FUNCTION
1237 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1238 || sym->attr.external)
1239 return 1;
1240
1241 if (was_declared (sym) || sym->ns->parent == NULL)
1242 return 0;
1243
1244 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1245
1246 return (s == NULL) ? 0 : specific_sym (s);
1247 }
1248
1249
1250 /* Figure out if the procedure is specific, generic or unknown. */
1251
1252 typedef enum
1253 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1254 proc_type;
1255
1256 static proc_type
1257 procedure_kind (gfc_symbol *sym)
1258 {
1259 if (generic_sym (sym))
1260 return PTYPE_GENERIC;
1261
1262 if (specific_sym (sym))
1263 return PTYPE_SPECIFIC;
1264
1265 return PTYPE_UNKNOWN;
1266 }
1267
1268 /* Check references to assumed size arrays. The flag need_full_assumed_size
1269 is nonzero when matching actual arguments. */
1270
1271 static int need_full_assumed_size = 0;
1272
1273 static bool
1274 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1275 {
1276 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1277 return false;
1278
1279 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1280 What should it be? */
1281 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1282 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1283 && (e->ref->u.ar.type == AR_FULL))
1284 {
1285 gfc_error ("The upper bound in the last dimension must "
1286 "appear in the reference to the assumed size "
1287 "array '%s' at %L", sym->name, &e->where);
1288 return true;
1289 }
1290 return false;
1291 }
1292
1293
1294 /* Look for bad assumed size array references in argument expressions
1295 of elemental and array valued intrinsic procedures. Since this is
1296 called from procedure resolution functions, it only recurses at
1297 operators. */
1298
1299 static bool
1300 resolve_assumed_size_actual (gfc_expr *e)
1301 {
1302 if (e == NULL)
1303 return false;
1304
1305 switch (e->expr_type)
1306 {
1307 case EXPR_VARIABLE:
1308 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1309 return true;
1310 break;
1311
1312 case EXPR_OP:
1313 if (resolve_assumed_size_actual (e->value.op.op1)
1314 || resolve_assumed_size_actual (e->value.op.op2))
1315 return true;
1316 break;
1317
1318 default:
1319 break;
1320 }
1321 return false;
1322 }
1323
1324
1325 /* Check a generic procedure, passed as an actual argument, to see if
1326 there is a matching specific name. If none, it is an error, and if
1327 more than one, the reference is ambiguous. */
1328 static int
1329 count_specific_procs (gfc_expr *e)
1330 {
1331 int n;
1332 gfc_interface *p;
1333 gfc_symbol *sym;
1334
1335 n = 0;
1336 sym = e->symtree->n.sym;
1337
1338 for (p = sym->generic; p; p = p->next)
1339 if (strcmp (sym->name, p->sym->name) == 0)
1340 {
1341 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1342 sym->name);
1343 n++;
1344 }
1345
1346 if (n > 1)
1347 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1348 &e->where);
1349
1350 if (n == 0)
1351 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1352 "argument at %L", sym->name, &e->where);
1353
1354 return n;
1355 }
1356
1357
1358 /* See if a call to sym could possibly be a not allowed RECURSION because of
1359 a missing RECURIVE declaration. This means that either sym is the current
1360 context itself, or sym is the parent of a contained procedure calling its
1361 non-RECURSIVE containing procedure.
1362 This also works if sym is an ENTRY. */
1363
1364 static bool
1365 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1366 {
1367 gfc_symbol* proc_sym;
1368 gfc_symbol* context_proc;
1369 gfc_namespace* real_context;
1370
1371 if (sym->attr.flavor == FL_PROGRAM)
1372 return false;
1373
1374 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1375
1376 /* If we've got an ENTRY, find real procedure. */
1377 if (sym->attr.entry && sym->ns->entries)
1378 proc_sym = sym->ns->entries->sym;
1379 else
1380 proc_sym = sym;
1381
1382 /* If sym is RECURSIVE, all is well of course. */
1383 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1384 return false;
1385
1386 /* Find the context procedure's "real" symbol if it has entries.
1387 We look for a procedure symbol, so recurse on the parents if we don't
1388 find one (like in case of a BLOCK construct). */
1389 for (real_context = context; ; real_context = real_context->parent)
1390 {
1391 /* We should find something, eventually! */
1392 gcc_assert (real_context);
1393
1394 context_proc = (real_context->entries ? real_context->entries->sym
1395 : real_context->proc_name);
1396
1397 /* In some special cases, there may not be a proc_name, like for this
1398 invalid code:
1399 real(bad_kind()) function foo () ...
1400 when checking the call to bad_kind ().
1401 In these cases, we simply return here and assume that the
1402 call is ok. */
1403 if (!context_proc)
1404 return false;
1405
1406 if (context_proc->attr.flavor != FL_LABEL)
1407 break;
1408 }
1409
1410 /* A call from sym's body to itself is recursion, of course. */
1411 if (context_proc == proc_sym)
1412 return true;
1413
1414 /* The same is true if context is a contained procedure and sym the
1415 containing one. */
1416 if (context_proc->attr.contained)
1417 {
1418 gfc_symbol* parent_proc;
1419
1420 gcc_assert (context->parent);
1421 parent_proc = (context->parent->entries ? context->parent->entries->sym
1422 : context->parent->proc_name);
1423
1424 if (parent_proc == proc_sym)
1425 return true;
1426 }
1427
1428 return false;
1429 }
1430
1431
1432 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1433 its typespec and formal argument list. */
1434
1435 static gfc_try
1436 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1437 {
1438 gfc_intrinsic_sym* isym = NULL;
1439 const char* symstd;
1440
1441 if (sym->formal)
1442 return SUCCESS;
1443
1444 /* Already resolved. */
1445 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1446 return SUCCESS;
1447
1448 /* We already know this one is an intrinsic, so we don't call
1449 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1450 gfc_find_subroutine directly to check whether it is a function or
1451 subroutine. */
1452
1453 if (sym->intmod_sym_id)
1454 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1455 else
1456 isym = gfc_find_function (sym->name);
1457
1458 if (isym)
1459 {
1460 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1461 && !sym->attr.implicit_type)
1462 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1463 " ignored", sym->name, &sym->declared_at);
1464
1465 if (!sym->attr.function &&
1466 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1467 return FAILURE;
1468
1469 sym->ts = isym->ts;
1470 }
1471 else if ((isym = gfc_find_subroutine (sym->name)))
1472 {
1473 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1474 {
1475 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1476 " specifier", sym->name, &sym->declared_at);
1477 return FAILURE;
1478 }
1479
1480 if (!sym->attr.subroutine &&
1481 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1482 return FAILURE;
1483 }
1484 else
1485 {
1486 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1487 &sym->declared_at);
1488 return FAILURE;
1489 }
1490
1491 gfc_copy_formal_args_intr (sym, isym);
1492
1493 /* Check it is actually available in the standard settings. */
1494 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1495 == FAILURE)
1496 {
1497 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1498 " available in the current standard settings but %s. Use"
1499 " an appropriate -std=* option or enable -fall-intrinsics"
1500 " in order to use it.",
1501 sym->name, &sym->declared_at, symstd);
1502 return FAILURE;
1503 }
1504
1505 return SUCCESS;
1506 }
1507
1508
1509 /* Resolve a procedure expression, like passing it to a called procedure or as
1510 RHS for a procedure pointer assignment. */
1511
1512 static gfc_try
1513 resolve_procedure_expression (gfc_expr* expr)
1514 {
1515 gfc_symbol* sym;
1516
1517 if (expr->expr_type != EXPR_VARIABLE)
1518 return SUCCESS;
1519 gcc_assert (expr->symtree);
1520
1521 sym = expr->symtree->n.sym;
1522
1523 if (sym->attr.intrinsic)
1524 resolve_intrinsic (sym, &expr->where);
1525
1526 if (sym->attr.flavor != FL_PROCEDURE
1527 || (sym->attr.function && sym->result == sym))
1528 return SUCCESS;
1529
1530 /* A non-RECURSIVE procedure that is used as procedure expression within its
1531 own body is in danger of being called recursively. */
1532 if (is_illegal_recursion (sym, gfc_current_ns))
1533 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1534 " itself recursively. Declare it RECURSIVE or use"
1535 " -frecursive", sym->name, &expr->where);
1536
1537 return SUCCESS;
1538 }
1539
1540
1541 /* Resolve an actual argument list. Most of the time, this is just
1542 resolving the expressions in the list.
1543 The exception is that we sometimes have to decide whether arguments
1544 that look like procedure arguments are really simple variable
1545 references. */
1546
1547 static gfc_try
1548 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1549 bool no_formal_args)
1550 {
1551 gfc_symbol *sym;
1552 gfc_symtree *parent_st;
1553 gfc_expr *e;
1554 int save_need_full_assumed_size;
1555
1556 for (; arg; arg = arg->next)
1557 {
1558 e = arg->expr;
1559 if (e == NULL)
1560 {
1561 /* Check the label is a valid branching target. */
1562 if (arg->label)
1563 {
1564 if (arg->label->defined == ST_LABEL_UNKNOWN)
1565 {
1566 gfc_error ("Label %d referenced at %L is never defined",
1567 arg->label->value, &arg->label->where);
1568 return FAILURE;
1569 }
1570 }
1571 continue;
1572 }
1573
1574 if (e->expr_type == EXPR_VARIABLE
1575 && e->symtree->n.sym->attr.generic
1576 && no_formal_args
1577 && count_specific_procs (e) != 1)
1578 return FAILURE;
1579
1580 if (e->ts.type != BT_PROCEDURE)
1581 {
1582 save_need_full_assumed_size = need_full_assumed_size;
1583 if (e->expr_type != EXPR_VARIABLE)
1584 need_full_assumed_size = 0;
1585 if (gfc_resolve_expr (e) != SUCCESS)
1586 return FAILURE;
1587 need_full_assumed_size = save_need_full_assumed_size;
1588 goto argument_list;
1589 }
1590
1591 /* See if the expression node should really be a variable reference. */
1592
1593 sym = e->symtree->n.sym;
1594
1595 if (sym->attr.flavor == FL_PROCEDURE
1596 || sym->attr.intrinsic
1597 || sym->attr.external)
1598 {
1599 int actual_ok;
1600
1601 /* If a procedure is not already determined to be something else
1602 check if it is intrinsic. */
1603 if (!sym->attr.intrinsic
1604 && !(sym->attr.external || sym->attr.use_assoc
1605 || sym->attr.if_source == IFSRC_IFBODY)
1606 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1607 sym->attr.intrinsic = 1;
1608
1609 if (sym->attr.proc == PROC_ST_FUNCTION)
1610 {
1611 gfc_error ("Statement function '%s' at %L is not allowed as an "
1612 "actual argument", sym->name, &e->where);
1613 }
1614
1615 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1616 sym->attr.subroutine);
1617 if (sym->attr.intrinsic && actual_ok == 0)
1618 {
1619 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1620 "actual argument", sym->name, &e->where);
1621 }
1622
1623 if (sym->attr.contained && !sym->attr.use_assoc
1624 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1625 {
1626 if (gfc_notify_std (GFC_STD_F2008,
1627 "Fortran 2008: Internal procedure '%s' is"
1628 " used as actual argument at %L",
1629 sym->name, &e->where) == FAILURE)
1630 return FAILURE;
1631 }
1632
1633 if (sym->attr.elemental && !sym->attr.intrinsic)
1634 {
1635 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1636 "allowed as an actual argument at %L", sym->name,
1637 &e->where);
1638 }
1639
1640 /* Check if a generic interface has a specific procedure
1641 with the same name before emitting an error. */
1642 if (sym->attr.generic && count_specific_procs (e) != 1)
1643 return FAILURE;
1644
1645 /* Just in case a specific was found for the expression. */
1646 sym = e->symtree->n.sym;
1647
1648 /* If the symbol is the function that names the current (or
1649 parent) scope, then we really have a variable reference. */
1650
1651 if (gfc_is_function_return_value (sym, sym->ns))
1652 goto got_variable;
1653
1654 /* If all else fails, see if we have a specific intrinsic. */
1655 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1656 {
1657 gfc_intrinsic_sym *isym;
1658
1659 isym = gfc_find_function (sym->name);
1660 if (isym == NULL || !isym->specific)
1661 {
1662 gfc_error ("Unable to find a specific INTRINSIC procedure "
1663 "for the reference '%s' at %L", sym->name,
1664 &e->where);
1665 return FAILURE;
1666 }
1667 sym->ts = isym->ts;
1668 sym->attr.intrinsic = 1;
1669 sym->attr.function = 1;
1670 }
1671
1672 if (gfc_resolve_expr (e) == FAILURE)
1673 return FAILURE;
1674 goto argument_list;
1675 }
1676
1677 /* See if the name is a module procedure in a parent unit. */
1678
1679 if (was_declared (sym) || sym->ns->parent == NULL)
1680 goto got_variable;
1681
1682 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1683 {
1684 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1685 return FAILURE;
1686 }
1687
1688 if (parent_st == NULL)
1689 goto got_variable;
1690
1691 sym = parent_st->n.sym;
1692 e->symtree = parent_st; /* Point to the right thing. */
1693
1694 if (sym->attr.flavor == FL_PROCEDURE
1695 || sym->attr.intrinsic
1696 || sym->attr.external)
1697 {
1698 if (gfc_resolve_expr (e) == FAILURE)
1699 return FAILURE;
1700 goto argument_list;
1701 }
1702
1703 got_variable:
1704 e->expr_type = EXPR_VARIABLE;
1705 e->ts = sym->ts;
1706 if (sym->as != NULL)
1707 {
1708 e->rank = sym->as->rank;
1709 e->ref = gfc_get_ref ();
1710 e->ref->type = REF_ARRAY;
1711 e->ref->u.ar.type = AR_FULL;
1712 e->ref->u.ar.as = sym->as;
1713 }
1714
1715 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1716 primary.c (match_actual_arg). If above code determines that it
1717 is a variable instead, it needs to be resolved as it was not
1718 done at the beginning of this function. */
1719 save_need_full_assumed_size = need_full_assumed_size;
1720 if (e->expr_type != EXPR_VARIABLE)
1721 need_full_assumed_size = 0;
1722 if (gfc_resolve_expr (e) != SUCCESS)
1723 return FAILURE;
1724 need_full_assumed_size = save_need_full_assumed_size;
1725
1726 argument_list:
1727 /* Check argument list functions %VAL, %LOC and %REF. There is
1728 nothing to do for %REF. */
1729 if (arg->name && arg->name[0] == '%')
1730 {
1731 if (strncmp ("%VAL", arg->name, 4) == 0)
1732 {
1733 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1734 {
1735 gfc_error ("By-value argument at %L is not of numeric "
1736 "type", &e->where);
1737 return FAILURE;
1738 }
1739
1740 if (e->rank)
1741 {
1742 gfc_error ("By-value argument at %L cannot be an array or "
1743 "an array section", &e->where);
1744 return FAILURE;
1745 }
1746
1747 /* Intrinsics are still PROC_UNKNOWN here. However,
1748 since same file external procedures are not resolvable
1749 in gfortran, it is a good deal easier to leave them to
1750 intrinsic.c. */
1751 if (ptype != PROC_UNKNOWN
1752 && ptype != PROC_DUMMY
1753 && ptype != PROC_EXTERNAL
1754 && ptype != PROC_MODULE)
1755 {
1756 gfc_error ("By-value argument at %L is not allowed "
1757 "in this context", &e->where);
1758 return FAILURE;
1759 }
1760 }
1761
1762 /* Statement functions have already been excluded above. */
1763 else if (strncmp ("%LOC", arg->name, 4) == 0
1764 && e->ts.type == BT_PROCEDURE)
1765 {
1766 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1767 {
1768 gfc_error ("Passing internal procedure at %L by location "
1769 "not allowed", &e->where);
1770 return FAILURE;
1771 }
1772 }
1773 }
1774
1775 /* Fortran 2008, C1237. */
1776 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1777 && gfc_has_ultimate_pointer (e))
1778 {
1779 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1780 "component", &e->where);
1781 return FAILURE;
1782 }
1783 }
1784
1785 return SUCCESS;
1786 }
1787
1788
1789 /* Do the checks of the actual argument list that are specific to elemental
1790 procedures. If called with c == NULL, we have a function, otherwise if
1791 expr == NULL, we have a subroutine. */
1792
1793 static gfc_try
1794 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1795 {
1796 gfc_actual_arglist *arg0;
1797 gfc_actual_arglist *arg;
1798 gfc_symbol *esym = NULL;
1799 gfc_intrinsic_sym *isym = NULL;
1800 gfc_expr *e = NULL;
1801 gfc_intrinsic_arg *iformal = NULL;
1802 gfc_formal_arglist *eformal = NULL;
1803 bool formal_optional = false;
1804 bool set_by_optional = false;
1805 int i;
1806 int rank = 0;
1807
1808 /* Is this an elemental procedure? */
1809 if (expr && expr->value.function.actual != NULL)
1810 {
1811 if (expr->value.function.esym != NULL
1812 && expr->value.function.esym->attr.elemental)
1813 {
1814 arg0 = expr->value.function.actual;
1815 esym = expr->value.function.esym;
1816 }
1817 else if (expr->value.function.isym != NULL
1818 && expr->value.function.isym->elemental)
1819 {
1820 arg0 = expr->value.function.actual;
1821 isym = expr->value.function.isym;
1822 }
1823 else
1824 return SUCCESS;
1825 }
1826 else if (c && c->ext.actual != NULL)
1827 {
1828 arg0 = c->ext.actual;
1829
1830 if (c->resolved_sym)
1831 esym = c->resolved_sym;
1832 else
1833 esym = c->symtree->n.sym;
1834 gcc_assert (esym);
1835
1836 if (!esym->attr.elemental)
1837 return SUCCESS;
1838 }
1839 else
1840 return SUCCESS;
1841
1842 /* The rank of an elemental is the rank of its array argument(s). */
1843 for (arg = arg0; arg; arg = arg->next)
1844 {
1845 if (arg->expr != NULL && arg->expr->rank > 0)
1846 {
1847 rank = arg->expr->rank;
1848 if (arg->expr->expr_type == EXPR_VARIABLE
1849 && arg->expr->symtree->n.sym->attr.optional)
1850 set_by_optional = true;
1851
1852 /* Function specific; set the result rank and shape. */
1853 if (expr)
1854 {
1855 expr->rank = rank;
1856 if (!expr->shape && arg->expr->shape)
1857 {
1858 expr->shape = gfc_get_shape (rank);
1859 for (i = 0; i < rank; i++)
1860 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1861 }
1862 }
1863 break;
1864 }
1865 }
1866
1867 /* If it is an array, it shall not be supplied as an actual argument
1868 to an elemental procedure unless an array of the same rank is supplied
1869 as an actual argument corresponding to a nonoptional dummy argument of
1870 that elemental procedure(12.4.1.5). */
1871 formal_optional = false;
1872 if (isym)
1873 iformal = isym->formal;
1874 else
1875 eformal = esym->formal;
1876
1877 for (arg = arg0; arg; arg = arg->next)
1878 {
1879 if (eformal)
1880 {
1881 if (eformal->sym && eformal->sym->attr.optional)
1882 formal_optional = true;
1883 eformal = eformal->next;
1884 }
1885 else if (isym && iformal)
1886 {
1887 if (iformal->optional)
1888 formal_optional = true;
1889 iformal = iformal->next;
1890 }
1891 else if (isym)
1892 formal_optional = true;
1893
1894 if (pedantic && arg->expr != NULL
1895 && arg->expr->expr_type == EXPR_VARIABLE
1896 && arg->expr->symtree->n.sym->attr.optional
1897 && formal_optional
1898 && arg->expr->rank
1899 && (set_by_optional || arg->expr->rank != rank)
1900 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1901 {
1902 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1903 "MISSING, it cannot be the actual argument of an "
1904 "ELEMENTAL procedure unless there is a non-optional "
1905 "argument with the same rank (12.4.1.5)",
1906 arg->expr->symtree->n.sym->name, &arg->expr->where);
1907 return FAILURE;
1908 }
1909 }
1910
1911 for (arg = arg0; arg; arg = arg->next)
1912 {
1913 if (arg->expr == NULL || arg->expr->rank == 0)
1914 continue;
1915
1916 /* Being elemental, the last upper bound of an assumed size array
1917 argument must be present. */
1918 if (resolve_assumed_size_actual (arg->expr))
1919 return FAILURE;
1920
1921 /* Elemental procedure's array actual arguments must conform. */
1922 if (e != NULL)
1923 {
1924 if (gfc_check_conformance (arg->expr, e,
1925 "elemental procedure") == FAILURE)
1926 return FAILURE;
1927 }
1928 else
1929 e = arg->expr;
1930 }
1931
1932 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1933 is an array, the intent inout/out variable needs to be also an array. */
1934 if (rank > 0 && esym && expr == NULL)
1935 for (eformal = esym->formal, arg = arg0; arg && eformal;
1936 arg = arg->next, eformal = eformal->next)
1937 if ((eformal->sym->attr.intent == INTENT_OUT
1938 || eformal->sym->attr.intent == INTENT_INOUT)
1939 && arg->expr && arg->expr->rank == 0)
1940 {
1941 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1942 "ELEMENTAL subroutine '%s' is a scalar, but another "
1943 "actual argument is an array", &arg->expr->where,
1944 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1945 : "INOUT", eformal->sym->name, esym->name);
1946 return FAILURE;
1947 }
1948 return SUCCESS;
1949 }
1950
1951
1952 /* This function does the checking of references to global procedures
1953 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1954 77 and 95 standards. It checks for a gsymbol for the name, making
1955 one if it does not already exist. If it already exists, then the
1956 reference being resolved must correspond to the type of gsymbol.
1957 Otherwise, the new symbol is equipped with the attributes of the
1958 reference. The corresponding code that is called in creating
1959 global entities is parse.c.
1960
1961 In addition, for all but -std=legacy, the gsymbols are used to
1962 check the interfaces of external procedures from the same file.
1963 The namespace of the gsymbol is resolved and then, once this is
1964 done the interface is checked. */
1965
1966
1967 static bool
1968 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1969 {
1970 if (!gsym_ns->proc_name->attr.recursive)
1971 return true;
1972
1973 if (sym->ns == gsym_ns)
1974 return false;
1975
1976 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1977 return false;
1978
1979 return true;
1980 }
1981
1982 static bool
1983 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1984 {
1985 if (gsym_ns->entries)
1986 {
1987 gfc_entry_list *entry = gsym_ns->entries;
1988
1989 for (; entry; entry = entry->next)
1990 {
1991 if (strcmp (sym->name, entry->sym->name) == 0)
1992 {
1993 if (strcmp (gsym_ns->proc_name->name,
1994 sym->ns->proc_name->name) == 0)
1995 return false;
1996
1997 if (sym->ns->parent
1998 && strcmp (gsym_ns->proc_name->name,
1999 sym->ns->parent->proc_name->name) == 0)
2000 return false;
2001 }
2002 }
2003 }
2004 return true;
2005 }
2006
2007 static void
2008 resolve_global_procedure (gfc_symbol *sym, locus *where,
2009 gfc_actual_arglist **actual, int sub)
2010 {
2011 gfc_gsymbol * gsym;
2012 gfc_namespace *ns;
2013 enum gfc_symbol_type type;
2014
2015 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2016
2017 gsym = gfc_get_gsymbol (sym->name);
2018
2019 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2020 gfc_global_used (gsym, where);
2021
2022 if (gfc_option.flag_whole_file
2023 && (sym->attr.if_source == IFSRC_UNKNOWN
2024 || sym->attr.if_source == IFSRC_IFBODY)
2025 && gsym->type != GSYM_UNKNOWN
2026 && gsym->ns
2027 && gsym->ns->resolved != -1
2028 && gsym->ns->proc_name
2029 && not_in_recursive (sym, gsym->ns)
2030 && not_entry_self_reference (sym, gsym->ns))
2031 {
2032 gfc_symbol *def_sym;
2033
2034 /* Resolve the gsymbol namespace if needed. */
2035 if (!gsym->ns->resolved)
2036 {
2037 gfc_dt_list *old_dt_list;
2038 struct gfc_omp_saved_state old_omp_state;
2039
2040 /* Stash away derived types so that the backend_decls do not
2041 get mixed up. */
2042 old_dt_list = gfc_derived_types;
2043 gfc_derived_types = NULL;
2044 /* And stash away openmp state. */
2045 gfc_omp_save_and_clear_state (&old_omp_state);
2046
2047 gfc_resolve (gsym->ns);
2048
2049 /* Store the new derived types with the global namespace. */
2050 if (gfc_derived_types)
2051 gsym->ns->derived_types = gfc_derived_types;
2052
2053 /* Restore the derived types of this namespace. */
2054 gfc_derived_types = old_dt_list;
2055 /* And openmp state. */
2056 gfc_omp_restore_state (&old_omp_state);
2057 }
2058
2059 /* Make sure that translation for the gsymbol occurs before
2060 the procedure currently being resolved. */
2061 ns = gfc_global_ns_list;
2062 for (; ns && ns != gsym->ns; ns = ns->sibling)
2063 {
2064 if (ns->sibling == gsym->ns)
2065 {
2066 ns->sibling = gsym->ns->sibling;
2067 gsym->ns->sibling = gfc_global_ns_list;
2068 gfc_global_ns_list = gsym->ns;
2069 break;
2070 }
2071 }
2072
2073 def_sym = gsym->ns->proc_name;
2074 if (def_sym->attr.entry_master)
2075 {
2076 gfc_entry_list *entry;
2077 for (entry = gsym->ns->entries; entry; entry = entry->next)
2078 if (strcmp (entry->sym->name, sym->name) == 0)
2079 {
2080 def_sym = entry->sym;
2081 break;
2082 }
2083 }
2084
2085 /* Differences in constant character lengths. */
2086 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2087 {
2088 long int l1 = 0, l2 = 0;
2089 gfc_charlen *cl1 = sym->ts.u.cl;
2090 gfc_charlen *cl2 = def_sym->ts.u.cl;
2091
2092 if (cl1 != NULL
2093 && cl1->length != NULL
2094 && cl1->length->expr_type == EXPR_CONSTANT)
2095 l1 = mpz_get_si (cl1->length->value.integer);
2096
2097 if (cl2 != NULL
2098 && cl2->length != NULL
2099 && cl2->length->expr_type == EXPR_CONSTANT)
2100 l2 = mpz_get_si (cl2->length->value.integer);
2101
2102 if (l1 && l2 && l1 != l2)
2103 gfc_error ("Character length mismatch in return type of "
2104 "function '%s' at %L (%ld/%ld)", sym->name,
2105 &sym->declared_at, l1, l2);
2106 }
2107
2108 /* Type mismatch of function return type and expected type. */
2109 if (sym->attr.function
2110 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2111 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2112 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2113 gfc_typename (&def_sym->ts));
2114
2115 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2116 {
2117 gfc_formal_arglist *arg = def_sym->formal;
2118 for ( ; arg; arg = arg->next)
2119 if (!arg->sym)
2120 continue;
2121 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2122 else if (arg->sym->attr.allocatable
2123 || arg->sym->attr.asynchronous
2124 || arg->sym->attr.optional
2125 || arg->sym->attr.pointer
2126 || arg->sym->attr.target
2127 || arg->sym->attr.value
2128 || arg->sym->attr.volatile_)
2129 {
2130 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2131 "has an attribute that requires an explicit "
2132 "interface for this procedure", arg->sym->name,
2133 sym->name, &sym->declared_at);
2134 break;
2135 }
2136 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2137 else if (arg->sym && arg->sym->as
2138 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2139 {
2140 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2141 "argument '%s' must have an explicit interface",
2142 sym->name, &sym->declared_at, arg->sym->name);
2143 break;
2144 }
2145 /* F2008, 12.4.2.2 (2c) */
2146 else if (arg->sym->attr.codimension)
2147 {
2148 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2149 "'%s' must have an explicit interface",
2150 sym->name, &sym->declared_at, arg->sym->name);
2151 break;
2152 }
2153 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2154 else if (false) /* TODO: is a parametrized derived type */
2155 {
2156 gfc_error ("Procedure '%s' at %L with parametrized derived "
2157 "type argument '%s' must have an explicit "
2158 "interface", sym->name, &sym->declared_at,
2159 arg->sym->name);
2160 break;
2161 }
2162 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2163 else if (arg->sym->ts.type == BT_CLASS)
2164 {
2165 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2166 "argument '%s' must have an explicit interface",
2167 sym->name, &sym->declared_at, arg->sym->name);
2168 break;
2169 }
2170 }
2171
2172 if (def_sym->attr.function)
2173 {
2174 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2175 if (def_sym->as && def_sym->as->rank
2176 && (!sym->as || sym->as->rank != def_sym->as->rank))
2177 gfc_error ("The reference to function '%s' at %L either needs an "
2178 "explicit INTERFACE or the rank is incorrect", sym->name,
2179 where);
2180
2181 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2182 if ((def_sym->result->attr.pointer
2183 || def_sym->result->attr.allocatable)
2184 && (sym->attr.if_source != IFSRC_IFBODY
2185 || def_sym->result->attr.pointer
2186 != sym->result->attr.pointer
2187 || def_sym->result->attr.allocatable
2188 != sym->result->attr.allocatable))
2189 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2190 "result must have an explicit interface", sym->name,
2191 where);
2192
2193 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2194 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2195 && def_sym->ts.type == BT_CHARACTER && def_sym->ts.u.cl->length != NULL)
2196 {
2197 gfc_charlen *cl = sym->ts.u.cl;
2198
2199 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2200 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2201 {
2202 gfc_error ("Nonconstant character-length function '%s' at %L "
2203 "must have an explicit interface", sym->name,
2204 &sym->declared_at);
2205 }
2206 }
2207 }
2208
2209 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2210 if (def_sym->attr.elemental && !sym->attr.elemental)
2211 {
2212 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2213 "interface", sym->name, &sym->declared_at);
2214 }
2215
2216 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2217 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2218 {
2219 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2220 "an explicit interface", sym->name, &sym->declared_at);
2221 }
2222
2223 if (gfc_option.flag_whole_file == 1
2224 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2225 && !(gfc_option.warn_std & GFC_STD_GNU)))
2226 gfc_errors_to_warnings (1);
2227
2228 if (sym->attr.if_source != IFSRC_IFBODY)
2229 gfc_procedure_use (def_sym, actual, where);
2230
2231 gfc_errors_to_warnings (0);
2232 }
2233
2234 if (gsym->type == GSYM_UNKNOWN)
2235 {
2236 gsym->type = type;
2237 gsym->where = *where;
2238 }
2239
2240 gsym->used = 1;
2241 }
2242
2243
2244 /************* Function resolution *************/
2245
2246 /* Resolve a function call known to be generic.
2247 Section 14.1.2.4.1. */
2248
2249 static match
2250 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2251 {
2252 gfc_symbol *s;
2253
2254 if (sym->attr.generic)
2255 {
2256 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2257 if (s != NULL)
2258 {
2259 expr->value.function.name = s->name;
2260 expr->value.function.esym = s;
2261
2262 if (s->ts.type != BT_UNKNOWN)
2263 expr->ts = s->ts;
2264 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2265 expr->ts = s->result->ts;
2266
2267 if (s->as != NULL)
2268 expr->rank = s->as->rank;
2269 else if (s->result != NULL && s->result->as != NULL)
2270 expr->rank = s->result->as->rank;
2271
2272 gfc_set_sym_referenced (expr->value.function.esym);
2273
2274 return MATCH_YES;
2275 }
2276
2277 /* TODO: Need to search for elemental references in generic
2278 interface. */
2279 }
2280
2281 if (sym->attr.intrinsic)
2282 return gfc_intrinsic_func_interface (expr, 0);
2283
2284 return MATCH_NO;
2285 }
2286
2287
2288 static gfc_try
2289 resolve_generic_f (gfc_expr *expr)
2290 {
2291 gfc_symbol *sym;
2292 match m;
2293
2294 sym = expr->symtree->n.sym;
2295
2296 for (;;)
2297 {
2298 m = resolve_generic_f0 (expr, sym);
2299 if (m == MATCH_YES)
2300 return SUCCESS;
2301 else if (m == MATCH_ERROR)
2302 return FAILURE;
2303
2304 generic:
2305 if (sym->ns->parent == NULL)
2306 break;
2307 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2308
2309 if (sym == NULL)
2310 break;
2311 if (!generic_sym (sym))
2312 goto generic;
2313 }
2314
2315 /* Last ditch attempt. See if the reference is to an intrinsic
2316 that possesses a matching interface. 14.1.2.4 */
2317 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2318 {
2319 gfc_error ("There is no specific function for the generic '%s' at %L",
2320 expr->symtree->n.sym->name, &expr->where);
2321 return FAILURE;
2322 }
2323
2324 m = gfc_intrinsic_func_interface (expr, 0);
2325 if (m == MATCH_YES)
2326 return SUCCESS;
2327 if (m == MATCH_NO)
2328 gfc_error ("Generic function '%s' at %L is not consistent with a "
2329 "specific intrinsic interface", expr->symtree->n.sym->name,
2330 &expr->where);
2331
2332 return FAILURE;
2333 }
2334
2335
2336 /* Resolve a function call known to be specific. */
2337
2338 static match
2339 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2340 {
2341 match m;
2342
2343 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2344 {
2345 if (sym->attr.dummy)
2346 {
2347 sym->attr.proc = PROC_DUMMY;
2348 goto found;
2349 }
2350
2351 sym->attr.proc = PROC_EXTERNAL;
2352 goto found;
2353 }
2354
2355 if (sym->attr.proc == PROC_MODULE
2356 || sym->attr.proc == PROC_ST_FUNCTION
2357 || sym->attr.proc == PROC_INTERNAL)
2358 goto found;
2359
2360 if (sym->attr.intrinsic)
2361 {
2362 m = gfc_intrinsic_func_interface (expr, 1);
2363 if (m == MATCH_YES)
2364 return MATCH_YES;
2365 if (m == MATCH_NO)
2366 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2367 "with an intrinsic", sym->name, &expr->where);
2368
2369 return MATCH_ERROR;
2370 }
2371
2372 return MATCH_NO;
2373
2374 found:
2375 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2376
2377 if (sym->result)
2378 expr->ts = sym->result->ts;
2379 else
2380 expr->ts = sym->ts;
2381 expr->value.function.name = sym->name;
2382 expr->value.function.esym = sym;
2383 if (sym->as != NULL)
2384 expr->rank = sym->as->rank;
2385
2386 return MATCH_YES;
2387 }
2388
2389
2390 static gfc_try
2391 resolve_specific_f (gfc_expr *expr)
2392 {
2393 gfc_symbol *sym;
2394 match m;
2395
2396 sym = expr->symtree->n.sym;
2397
2398 for (;;)
2399 {
2400 m = resolve_specific_f0 (sym, expr);
2401 if (m == MATCH_YES)
2402 return SUCCESS;
2403 if (m == MATCH_ERROR)
2404 return FAILURE;
2405
2406 if (sym->ns->parent == NULL)
2407 break;
2408
2409 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2410
2411 if (sym == NULL)
2412 break;
2413 }
2414
2415 gfc_error ("Unable to resolve the specific function '%s' at %L",
2416 expr->symtree->n.sym->name, &expr->where);
2417
2418 return SUCCESS;
2419 }
2420
2421
2422 /* Resolve a procedure call not known to be generic nor specific. */
2423
2424 static gfc_try
2425 resolve_unknown_f (gfc_expr *expr)
2426 {
2427 gfc_symbol *sym;
2428 gfc_typespec *ts;
2429
2430 sym = expr->symtree->n.sym;
2431
2432 if (sym->attr.dummy)
2433 {
2434 sym->attr.proc = PROC_DUMMY;
2435 expr->value.function.name = sym->name;
2436 goto set_type;
2437 }
2438
2439 /* See if we have an intrinsic function reference. */
2440
2441 if (gfc_is_intrinsic (sym, 0, expr->where))
2442 {
2443 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2444 return SUCCESS;
2445 return FAILURE;
2446 }
2447
2448 /* The reference is to an external name. */
2449
2450 sym->attr.proc = PROC_EXTERNAL;
2451 expr->value.function.name = sym->name;
2452 expr->value.function.esym = expr->symtree->n.sym;
2453
2454 if (sym->as != NULL)
2455 expr->rank = sym->as->rank;
2456
2457 /* Type of the expression is either the type of the symbol or the
2458 default type of the symbol. */
2459
2460 set_type:
2461 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2462
2463 if (sym->ts.type != BT_UNKNOWN)
2464 expr->ts = sym->ts;
2465 else
2466 {
2467 ts = gfc_get_default_type (sym->name, sym->ns);
2468
2469 if (ts->type == BT_UNKNOWN)
2470 {
2471 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2472 sym->name, &expr->where);
2473 return FAILURE;
2474 }
2475 else
2476 expr->ts = *ts;
2477 }
2478
2479 return SUCCESS;
2480 }
2481
2482
2483 /* Return true, if the symbol is an external procedure. */
2484 static bool
2485 is_external_proc (gfc_symbol *sym)
2486 {
2487 if (!sym->attr.dummy && !sym->attr.contained
2488 && !(sym->attr.intrinsic
2489 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2490 && sym->attr.proc != PROC_ST_FUNCTION
2491 && !sym->attr.proc_pointer
2492 && !sym->attr.use_assoc
2493 && sym->name)
2494 return true;
2495
2496 return false;
2497 }
2498
2499
2500 /* Figure out if a function reference is pure or not. Also set the name
2501 of the function for a potential error message. Return nonzero if the
2502 function is PURE, zero if not. */
2503 static int
2504 pure_stmt_function (gfc_expr *, gfc_symbol *);
2505
2506 static int
2507 pure_function (gfc_expr *e, const char **name)
2508 {
2509 int pure;
2510
2511 *name = NULL;
2512
2513 if (e->symtree != NULL
2514 && e->symtree->n.sym != NULL
2515 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2516 return pure_stmt_function (e, e->symtree->n.sym);
2517
2518 if (e->value.function.esym)
2519 {
2520 pure = gfc_pure (e->value.function.esym);
2521 *name = e->value.function.esym->name;
2522 }
2523 else if (e->value.function.isym)
2524 {
2525 pure = e->value.function.isym->pure
2526 || e->value.function.isym->elemental;
2527 *name = e->value.function.isym->name;
2528 }
2529 else
2530 {
2531 /* Implicit functions are not pure. */
2532 pure = 0;
2533 *name = e->value.function.name;
2534 }
2535
2536 return pure;
2537 }
2538
2539
2540 static bool
2541 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2542 int *f ATTRIBUTE_UNUSED)
2543 {
2544 const char *name;
2545
2546 /* Don't bother recursing into other statement functions
2547 since they will be checked individually for purity. */
2548 if (e->expr_type != EXPR_FUNCTION
2549 || !e->symtree
2550 || e->symtree->n.sym == sym
2551 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2552 return false;
2553
2554 return pure_function (e, &name) ? false : true;
2555 }
2556
2557
2558 static int
2559 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2560 {
2561 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2562 }
2563
2564
2565 static gfc_try
2566 is_scalar_expr_ptr (gfc_expr *expr)
2567 {
2568 gfc_try retval = SUCCESS;
2569 gfc_ref *ref;
2570 int start;
2571 int end;
2572
2573 /* See if we have a gfc_ref, which means we have a substring, array
2574 reference, or a component. */
2575 if (expr->ref != NULL)
2576 {
2577 ref = expr->ref;
2578 while (ref->next != NULL)
2579 ref = ref->next;
2580
2581 switch (ref->type)
2582 {
2583 case REF_SUBSTRING:
2584 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2585 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2586 retval = FAILURE;
2587 break;
2588
2589 case REF_ARRAY:
2590 if (ref->u.ar.type == AR_ELEMENT)
2591 retval = SUCCESS;
2592 else if (ref->u.ar.type == AR_FULL)
2593 {
2594 /* The user can give a full array if the array is of size 1. */
2595 if (ref->u.ar.as != NULL
2596 && ref->u.ar.as->rank == 1
2597 && ref->u.ar.as->type == AS_EXPLICIT
2598 && ref->u.ar.as->lower[0] != NULL
2599 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2600 && ref->u.ar.as->upper[0] != NULL
2601 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2602 {
2603 /* If we have a character string, we need to check if
2604 its length is one. */
2605 if (expr->ts.type == BT_CHARACTER)
2606 {
2607 if (expr->ts.u.cl == NULL
2608 || expr->ts.u.cl->length == NULL
2609 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2610 != 0)
2611 retval = FAILURE;
2612 }
2613 else
2614 {
2615 /* We have constant lower and upper bounds. If the
2616 difference between is 1, it can be considered a
2617 scalar.
2618 FIXME: Use gfc_dep_compare_expr instead. */
2619 start = (int) mpz_get_si
2620 (ref->u.ar.as->lower[0]->value.integer);
2621 end = (int) mpz_get_si
2622 (ref->u.ar.as->upper[0]->value.integer);
2623 if (end - start + 1 != 1)
2624 retval = FAILURE;
2625 }
2626 }
2627 else
2628 retval = FAILURE;
2629 }
2630 else
2631 retval = FAILURE;
2632 break;
2633 default:
2634 retval = SUCCESS;
2635 break;
2636 }
2637 }
2638 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2639 {
2640 /* Character string. Make sure it's of length 1. */
2641 if (expr->ts.u.cl == NULL
2642 || expr->ts.u.cl->length == NULL
2643 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2644 retval = FAILURE;
2645 }
2646 else if (expr->rank != 0)
2647 retval = FAILURE;
2648
2649 return retval;
2650 }
2651
2652
2653 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2654 and, in the case of c_associated, set the binding label based on
2655 the arguments. */
2656
2657 static gfc_try
2658 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2659 gfc_symbol **new_sym)
2660 {
2661 char name[GFC_MAX_SYMBOL_LEN + 1];
2662 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2663 int optional_arg = 0;
2664 gfc_try retval = SUCCESS;
2665 gfc_symbol *args_sym;
2666 gfc_typespec *arg_ts;
2667 symbol_attribute arg_attr;
2668
2669 if (args->expr->expr_type == EXPR_CONSTANT
2670 || args->expr->expr_type == EXPR_OP
2671 || args->expr->expr_type == EXPR_NULL)
2672 {
2673 gfc_error ("Argument to '%s' at %L is not a variable",
2674 sym->name, &(args->expr->where));
2675 return FAILURE;
2676 }
2677
2678 args_sym = args->expr->symtree->n.sym;
2679
2680 /* The typespec for the actual arg should be that stored in the expr
2681 and not necessarily that of the expr symbol (args_sym), because
2682 the actual expression could be a part-ref of the expr symbol. */
2683 arg_ts = &(args->expr->ts);
2684 arg_attr = gfc_expr_attr (args->expr);
2685
2686 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2687 {
2688 /* If the user gave two args then they are providing something for
2689 the optional arg (the second cptr). Therefore, set the name and
2690 binding label to the c_associated for two cptrs. Otherwise,
2691 set c_associated to expect one cptr. */
2692 if (args->next)
2693 {
2694 /* two args. */
2695 sprintf (name, "%s_2", sym->name);
2696 sprintf (binding_label, "%s_2", sym->binding_label);
2697 optional_arg = 1;
2698 }
2699 else
2700 {
2701 /* one arg. */
2702 sprintf (name, "%s_1", sym->name);
2703 sprintf (binding_label, "%s_1", sym->binding_label);
2704 optional_arg = 0;
2705 }
2706
2707 /* Get a new symbol for the version of c_associated that
2708 will get called. */
2709 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2710 }
2711 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2712 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2713 {
2714 sprintf (name, "%s", sym->name);
2715 sprintf (binding_label, "%s", sym->binding_label);
2716
2717 /* Error check the call. */
2718 if (args->next != NULL)
2719 {
2720 gfc_error_now ("More actual than formal arguments in '%s' "
2721 "call at %L", name, &(args->expr->where));
2722 retval = FAILURE;
2723 }
2724 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2725 {
2726 gfc_ref *ref;
2727 bool seen_section;
2728
2729 /* Make sure we have either the target or pointer attribute. */
2730 if (!arg_attr.target && !arg_attr.pointer)
2731 {
2732 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2733 "a TARGET or an associated pointer",
2734 args_sym->name,
2735 sym->name, &(args->expr->where));
2736 retval = FAILURE;
2737 }
2738
2739 if (gfc_is_coindexed (args->expr))
2740 {
2741 gfc_error_now ("Coindexed argument not permitted"
2742 " in '%s' call at %L", name,
2743 &(args->expr->where));
2744 retval = FAILURE;
2745 }
2746
2747 /* Follow references to make sure there are no array
2748 sections. */
2749 seen_section = false;
2750
2751 for (ref=args->expr->ref; ref; ref = ref->next)
2752 {
2753 if (ref->type == REF_ARRAY)
2754 {
2755 if (ref->u.ar.type == AR_SECTION)
2756 seen_section = true;
2757
2758 if (ref->u.ar.type != AR_ELEMENT)
2759 {
2760 gfc_ref *r;
2761 for (r = ref->next; r; r=r->next)
2762 if (r->type == REF_COMPONENT)
2763 {
2764 gfc_error_now ("Array section not permitted"
2765 " in '%s' call at %L", name,
2766 &(args->expr->where));
2767 retval = FAILURE;
2768 break;
2769 }
2770 }
2771 }
2772 }
2773
2774 if (seen_section && retval == SUCCESS)
2775 gfc_warning ("Array section in '%s' call at %L", name,
2776 &(args->expr->where));
2777
2778 /* See if we have interoperable type and type param. */
2779 if (verify_c_interop (arg_ts) == SUCCESS
2780 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2781 {
2782 if (args_sym->attr.target == 1)
2783 {
2784 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2785 has the target attribute and is interoperable. */
2786 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2787 allocatable variable that has the TARGET attribute and
2788 is not an array of zero size. */
2789 if (args_sym->attr.allocatable == 1)
2790 {
2791 if (args_sym->attr.dimension != 0
2792 && (args_sym->as && args_sym->as->rank == 0))
2793 {
2794 gfc_error_now ("Allocatable variable '%s' used as a "
2795 "parameter to '%s' at %L must not be "
2796 "an array of zero size",
2797 args_sym->name, sym->name,
2798 &(args->expr->where));
2799 retval = FAILURE;
2800 }
2801 }
2802 else
2803 {
2804 /* A non-allocatable target variable with C
2805 interoperable type and type parameters must be
2806 interoperable. */
2807 if (args_sym && args_sym->attr.dimension)
2808 {
2809 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2810 {
2811 gfc_error ("Assumed-shape array '%s' at %L "
2812 "cannot be an argument to the "
2813 "procedure '%s' because "
2814 "it is not C interoperable",
2815 args_sym->name,
2816 &(args->expr->where), sym->name);
2817 retval = FAILURE;
2818 }
2819 else if (args_sym->as->type == AS_DEFERRED)
2820 {
2821 gfc_error ("Deferred-shape array '%s' at %L "
2822 "cannot be an argument to the "
2823 "procedure '%s' because "
2824 "it is not C interoperable",
2825 args_sym->name,
2826 &(args->expr->where), sym->name);
2827 retval = FAILURE;
2828 }
2829 }
2830
2831 /* Make sure it's not a character string. Arrays of
2832 any type should be ok if the variable is of a C
2833 interoperable type. */
2834 if (arg_ts->type == BT_CHARACTER)
2835 if (arg_ts->u.cl != NULL
2836 && (arg_ts->u.cl->length == NULL
2837 || arg_ts->u.cl->length->expr_type
2838 != EXPR_CONSTANT
2839 || mpz_cmp_si
2840 (arg_ts->u.cl->length->value.integer, 1)
2841 != 0)
2842 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2843 {
2844 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2845 "at %L must have a length of 1",
2846 args_sym->name, sym->name,
2847 &(args->expr->where));
2848 retval = FAILURE;
2849 }
2850 }
2851 }
2852 else if (arg_attr.pointer
2853 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2854 {
2855 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2856 scalar pointer. */
2857 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2858 "associated scalar POINTER", args_sym->name,
2859 sym->name, &(args->expr->where));
2860 retval = FAILURE;
2861 }
2862 }
2863 else
2864 {
2865 /* The parameter is not required to be C interoperable. If it
2866 is not C interoperable, it must be a nonpolymorphic scalar
2867 with no length type parameters. It still must have either
2868 the pointer or target attribute, and it can be
2869 allocatable (but must be allocated when c_loc is called). */
2870 if (args->expr->rank != 0
2871 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2872 {
2873 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2874 "scalar", args_sym->name, sym->name,
2875 &(args->expr->where));
2876 retval = FAILURE;
2877 }
2878 else if (arg_ts->type == BT_CHARACTER
2879 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2880 {
2881 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2882 "%L must have a length of 1",
2883 args_sym->name, sym->name,
2884 &(args->expr->where));
2885 retval = FAILURE;
2886 }
2887 else if (arg_ts->type == BT_CLASS)
2888 {
2889 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2890 "polymorphic", args_sym->name, sym->name,
2891 &(args->expr->where));
2892 retval = FAILURE;
2893 }
2894 }
2895 }
2896 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2897 {
2898 if (args_sym->attr.flavor != FL_PROCEDURE)
2899 {
2900 /* TODO: Update this error message to allow for procedure
2901 pointers once they are implemented. */
2902 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2903 "procedure",
2904 args_sym->name, sym->name,
2905 &(args->expr->where));
2906 retval = FAILURE;
2907 }
2908 else if (args_sym->attr.is_bind_c != 1)
2909 {
2910 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2911 "BIND(C)",
2912 args_sym->name, sym->name,
2913 &(args->expr->where));
2914 retval = FAILURE;
2915 }
2916 }
2917
2918 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2919 *new_sym = sym;
2920 }
2921 else
2922 {
2923 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2924 "iso_c_binding function: '%s'!\n", sym->name);
2925 }
2926
2927 return retval;
2928 }
2929
2930
2931 /* Resolve a function call, which means resolving the arguments, then figuring
2932 out which entity the name refers to. */
2933
2934 static gfc_try
2935 resolve_function (gfc_expr *expr)
2936 {
2937 gfc_actual_arglist *arg;
2938 gfc_symbol *sym;
2939 const char *name;
2940 gfc_try t;
2941 int temp;
2942 procedure_type p = PROC_INTRINSIC;
2943 bool no_formal_args;
2944
2945 sym = NULL;
2946 if (expr->symtree)
2947 sym = expr->symtree->n.sym;
2948
2949 /* If this is a procedure pointer component, it has already been resolved. */
2950 if (gfc_is_proc_ptr_comp (expr, NULL))
2951 return SUCCESS;
2952
2953 if (sym && sym->attr.intrinsic
2954 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2955 return FAILURE;
2956
2957 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2958 {
2959 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2960 return FAILURE;
2961 }
2962
2963 /* If this ia a deferred TBP with an abstract interface (which may
2964 of course be referenced), expr->value.function.esym will be set. */
2965 if (sym && sym->attr.abstract && !expr->value.function.esym)
2966 {
2967 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2968 sym->name, &expr->where);
2969 return FAILURE;
2970 }
2971
2972 /* Switch off assumed size checking and do this again for certain kinds
2973 of procedure, once the procedure itself is resolved. */
2974 need_full_assumed_size++;
2975
2976 if (expr->symtree && expr->symtree->n.sym)
2977 p = expr->symtree->n.sym->attr.proc;
2978
2979 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2980 inquiry_argument = true;
2981 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2982
2983 if (resolve_actual_arglist (expr->value.function.actual,
2984 p, no_formal_args) == FAILURE)
2985 {
2986 inquiry_argument = false;
2987 return FAILURE;
2988 }
2989
2990 inquiry_argument = false;
2991
2992 /* Need to setup the call to the correct c_associated, depending on
2993 the number of cptrs to user gives to compare. */
2994 if (sym && sym->attr.is_iso_c == 1)
2995 {
2996 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2997 == FAILURE)
2998 return FAILURE;
2999
3000 /* Get the symtree for the new symbol (resolved func).
3001 the old one will be freed later, when it's no longer used. */
3002 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
3003 }
3004
3005 /* Resume assumed_size checking. */
3006 need_full_assumed_size--;
3007
3008 /* If the procedure is external, check for usage. */
3009 if (sym && is_external_proc (sym))
3010 resolve_global_procedure (sym, &expr->where,
3011 &expr->value.function.actual, 0);
3012
3013 if (sym && sym->ts.type == BT_CHARACTER
3014 && sym->ts.u.cl
3015 && sym->ts.u.cl->length == NULL
3016 && !sym->attr.dummy
3017 && !sym->ts.deferred
3018 && expr->value.function.esym == NULL
3019 && !sym->attr.contained)
3020 {
3021 /* Internal procedures are taken care of in resolve_contained_fntype. */
3022 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3023 "be used at %L since it is not a dummy argument",
3024 sym->name, &expr->where);
3025 return FAILURE;
3026 }
3027
3028 /* See if function is already resolved. */
3029
3030 if (expr->value.function.name != NULL)
3031 {
3032 if (expr->ts.type == BT_UNKNOWN)
3033 expr->ts = sym->ts;
3034 t = SUCCESS;
3035 }
3036 else
3037 {
3038 /* Apply the rules of section 14.1.2. */
3039
3040 switch (procedure_kind (sym))
3041 {
3042 case PTYPE_GENERIC:
3043 t = resolve_generic_f (expr);
3044 break;
3045
3046 case PTYPE_SPECIFIC:
3047 t = resolve_specific_f (expr);
3048 break;
3049
3050 case PTYPE_UNKNOWN:
3051 t = resolve_unknown_f (expr);
3052 break;
3053
3054 default:
3055 gfc_internal_error ("resolve_function(): bad function type");
3056 }
3057 }
3058
3059 /* If the expression is still a function (it might have simplified),
3060 then we check to see if we are calling an elemental function. */
3061
3062 if (expr->expr_type != EXPR_FUNCTION)
3063 return t;
3064
3065 temp = need_full_assumed_size;
3066 need_full_assumed_size = 0;
3067
3068 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3069 return FAILURE;
3070
3071 if (omp_workshare_flag
3072 && expr->value.function.esym
3073 && ! gfc_elemental (expr->value.function.esym))
3074 {
3075 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3076 "in WORKSHARE construct", expr->value.function.esym->name,
3077 &expr->where);
3078 t = FAILURE;
3079 }
3080
3081 #define GENERIC_ID expr->value.function.isym->id
3082 else if (expr->value.function.actual != NULL
3083 && expr->value.function.isym != NULL
3084 && GENERIC_ID != GFC_ISYM_LBOUND
3085 && GENERIC_ID != GFC_ISYM_LEN
3086 && GENERIC_ID != GFC_ISYM_LOC
3087 && GENERIC_ID != GFC_ISYM_PRESENT)
3088 {
3089 /* Array intrinsics must also have the last upper bound of an
3090 assumed size array argument. UBOUND and SIZE have to be
3091 excluded from the check if the second argument is anything
3092 than a constant. */
3093
3094 for (arg = expr->value.function.actual; arg; arg = arg->next)
3095 {
3096 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3097 && arg->next != NULL && arg->next->expr)
3098 {
3099 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3100 break;
3101
3102 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3103 break;
3104
3105 if ((int)mpz_get_si (arg->next->expr->value.integer)
3106 < arg->expr->rank)
3107 break;
3108 }
3109
3110 if (arg->expr != NULL
3111 && arg->expr->rank > 0
3112 && resolve_assumed_size_actual (arg->expr))
3113 return FAILURE;
3114 }
3115 }
3116 #undef GENERIC_ID
3117
3118 need_full_assumed_size = temp;
3119 name = NULL;
3120
3121 if (!pure_function (expr, &name) && name)
3122 {
3123 if (forall_flag)
3124 {
3125 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3126 "FORALL %s", name, &expr->where,
3127 forall_flag == 2 ? "mask" : "block");
3128 t = FAILURE;
3129 }
3130 else if (gfc_pure (NULL))
3131 {
3132 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3133 "procedure within a PURE procedure", name, &expr->where);
3134 t = FAILURE;
3135 }
3136 }
3137
3138 if (!pure_function (expr, &name) && name && gfc_implicit_pure (NULL))
3139 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3140
3141 /* Functions without the RECURSIVE attribution are not allowed to
3142 * call themselves. */
3143 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3144 {
3145 gfc_symbol *esym;
3146 esym = expr->value.function.esym;
3147
3148 if (is_illegal_recursion (esym, gfc_current_ns))
3149 {
3150 if (esym->attr.entry && esym->ns->entries)
3151 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3152 " function '%s' is not RECURSIVE",
3153 esym->name, &expr->where, esym->ns->entries->sym->name);
3154 else
3155 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3156 " is not RECURSIVE", esym->name, &expr->where);
3157
3158 t = FAILURE;
3159 }
3160 }
3161
3162 /* Character lengths of use associated functions may contains references to
3163 symbols not referenced from the current program unit otherwise. Make sure
3164 those symbols are marked as referenced. */
3165
3166 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3167 && expr->value.function.esym->attr.use_assoc)
3168 {
3169 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3170 }
3171
3172 /* Make sure that the expression has a typespec that works. */
3173 if (expr->ts.type == BT_UNKNOWN)
3174 {
3175 if (expr->symtree->n.sym->result
3176 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3177 && !expr->symtree->n.sym->result->attr.proc_pointer)
3178 expr->ts = expr->symtree->n.sym->result->ts;
3179 }
3180
3181 return t;
3182 }
3183
3184
3185 /************* Subroutine resolution *************/
3186
3187 static void
3188 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3189 {
3190 if (gfc_pure (sym))
3191 return;
3192
3193 if (forall_flag)
3194 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3195 sym->name, &c->loc);
3196 else if (gfc_pure (NULL))
3197 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3198 &c->loc);
3199 }
3200
3201
3202 static match
3203 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3204 {
3205 gfc_symbol *s;
3206
3207 if (sym->attr.generic)
3208 {
3209 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3210 if (s != NULL)
3211 {
3212 c->resolved_sym = s;
3213 pure_subroutine (c, s);
3214 return MATCH_YES;
3215 }
3216
3217 /* TODO: Need to search for elemental references in generic interface. */
3218 }
3219
3220 if (sym->attr.intrinsic)
3221 return gfc_intrinsic_sub_interface (c, 0);
3222
3223 return MATCH_NO;
3224 }
3225
3226
3227 static gfc_try
3228 resolve_generic_s (gfc_code *c)
3229 {
3230 gfc_symbol *sym;
3231 match m;
3232
3233 sym = c->symtree->n.sym;
3234
3235 for (;;)
3236 {
3237 m = resolve_generic_s0 (c, sym);
3238 if (m == MATCH_YES)
3239 return SUCCESS;
3240 else if (m == MATCH_ERROR)
3241 return FAILURE;
3242
3243 generic:
3244 if (sym->ns->parent == NULL)
3245 break;
3246 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3247
3248 if (sym == NULL)
3249 break;
3250 if (!generic_sym (sym))
3251 goto generic;
3252 }
3253
3254 /* Last ditch attempt. See if the reference is to an intrinsic
3255 that possesses a matching interface. 14.1.2.4 */
3256 sym = c->symtree->n.sym;
3257
3258 if (!gfc_is_intrinsic (sym, 1, c->loc))
3259 {
3260 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3261 sym->name, &c->loc);
3262 return FAILURE;
3263 }
3264
3265 m = gfc_intrinsic_sub_interface (c, 0);
3266 if (m == MATCH_YES)
3267 return SUCCESS;
3268 if (m == MATCH_NO)
3269 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3270 "intrinsic subroutine interface", sym->name, &c->loc);
3271
3272 return FAILURE;
3273 }
3274
3275
3276 /* Set the name and binding label of the subroutine symbol in the call
3277 expression represented by 'c' to include the type and kind of the
3278 second parameter. This function is for resolving the appropriate
3279 version of c_f_pointer() and c_f_procpointer(). For example, a
3280 call to c_f_pointer() for a default integer pointer could have a
3281 name of c_f_pointer_i4. If no second arg exists, which is an error
3282 for these two functions, it defaults to the generic symbol's name
3283 and binding label. */
3284
3285 static void
3286 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3287 char *name, char *binding_label)
3288 {
3289 gfc_expr *arg = NULL;
3290 char type;
3291 int kind;
3292
3293 /* The second arg of c_f_pointer and c_f_procpointer determines
3294 the type and kind for the procedure name. */
3295 arg = c->ext.actual->next->expr;
3296
3297 if (arg != NULL)
3298 {
3299 /* Set up the name to have the given symbol's name,
3300 plus the type and kind. */
3301 /* a derived type is marked with the type letter 'u' */
3302 if (arg->ts.type == BT_DERIVED)
3303 {
3304 type = 'd';
3305 kind = 0; /* set the kind as 0 for now */
3306 }
3307 else
3308 {
3309 type = gfc_type_letter (arg->ts.type);
3310 kind = arg->ts.kind;
3311 }
3312
3313 if (arg->ts.type == BT_CHARACTER)
3314 /* Kind info for character strings not needed. */
3315 kind = 0;
3316
3317 sprintf (name, "%s_%c%d", sym->name, type, kind);
3318 /* Set up the binding label as the given symbol's label plus
3319 the type and kind. */
3320 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3321 }
3322 else
3323 {
3324 /* If the second arg is missing, set the name and label as
3325 was, cause it should at least be found, and the missing
3326 arg error will be caught by compare_parameters(). */
3327 sprintf (name, "%s", sym->name);
3328 sprintf (binding_label, "%s", sym->binding_label);
3329 }
3330
3331 return;
3332 }
3333
3334
3335 /* Resolve a generic version of the iso_c_binding procedure given
3336 (sym) to the specific one based on the type and kind of the
3337 argument(s). Currently, this function resolves c_f_pointer() and
3338 c_f_procpointer based on the type and kind of the second argument
3339 (FPTR). Other iso_c_binding procedures aren't specially handled.
3340 Upon successfully exiting, c->resolved_sym will hold the resolved
3341 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3342 otherwise. */
3343
3344 match
3345 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3346 {
3347 gfc_symbol *new_sym;
3348 /* this is fine, since we know the names won't use the max */
3349 char name[GFC_MAX_SYMBOL_LEN + 1];
3350 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3351 /* default to success; will override if find error */
3352 match m = MATCH_YES;
3353
3354 /* Make sure the actual arguments are in the necessary order (based on the
3355 formal args) before resolving. */
3356 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3357
3358 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3359 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3360 {
3361 set_name_and_label (c, sym, name, binding_label);
3362
3363 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3364 {
3365 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3366 {
3367 /* Make sure we got a third arg if the second arg has non-zero
3368 rank. We must also check that the type and rank are
3369 correct since we short-circuit this check in
3370 gfc_procedure_use() (called above to sort actual args). */
3371 if (c->ext.actual->next->expr->rank != 0)
3372 {
3373 if(c->ext.actual->next->next == NULL
3374 || c->ext.actual->next->next->expr == NULL)
3375 {
3376 m = MATCH_ERROR;
3377 gfc_error ("Missing SHAPE parameter for call to %s "
3378 "at %L", sym->name, &(c->loc));
3379 }
3380 else if (c->ext.actual->next->next->expr->ts.type
3381 != BT_INTEGER
3382 || c->ext.actual->next->next->expr->rank != 1)
3383 {
3384 m = MATCH_ERROR;
3385 gfc_error ("SHAPE parameter for call to %s at %L must "
3386 "be a rank 1 INTEGER array", sym->name,
3387 &(c->loc));
3388 }
3389 }
3390 }
3391 }
3392
3393 if (m != MATCH_ERROR)
3394 {
3395 /* the 1 means to add the optional arg to formal list */
3396 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3397
3398 /* for error reporting, say it's declared where the original was */
3399 new_sym->declared_at = sym->declared_at;
3400 }
3401 }
3402 else
3403 {
3404 /* no differences for c_loc or c_funloc */
3405 new_sym = sym;
3406 }
3407
3408 /* set the resolved symbol */
3409 if (m != MATCH_ERROR)
3410 c->resolved_sym = new_sym;
3411 else
3412 c->resolved_sym = sym;
3413
3414 return m;
3415 }
3416
3417
3418 /* Resolve a subroutine call known to be specific. */
3419
3420 static match
3421 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3422 {
3423 match m;
3424
3425 if(sym->attr.is_iso_c)
3426 {
3427 m = gfc_iso_c_sub_interface (c,sym);
3428 return m;
3429 }
3430
3431 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3432 {
3433 if (sym->attr.dummy)
3434 {
3435 sym->attr.proc = PROC_DUMMY;
3436 goto found;
3437 }
3438
3439 sym->attr.proc = PROC_EXTERNAL;
3440 goto found;
3441 }
3442
3443 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3444 goto found;
3445
3446 if (sym->attr.intrinsic)
3447 {
3448 m = gfc_intrinsic_sub_interface (c, 1);
3449 if (m == MATCH_YES)
3450 return MATCH_YES;
3451 if (m == MATCH_NO)
3452 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3453 "with an intrinsic", sym->name, &c->loc);
3454
3455 return MATCH_ERROR;
3456 }
3457
3458 return MATCH_NO;
3459
3460 found:
3461 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3462
3463 c->resolved_sym = sym;
3464 pure_subroutine (c, sym);
3465
3466 return MATCH_YES;
3467 }
3468
3469
3470 static gfc_try
3471 resolve_specific_s (gfc_code *c)
3472 {
3473 gfc_symbol *sym;
3474 match m;
3475
3476 sym = c->symtree->n.sym;
3477
3478 for (;;)
3479 {
3480 m = resolve_specific_s0 (c, sym);
3481 if (m == MATCH_YES)
3482 return SUCCESS;
3483 if (m == MATCH_ERROR)
3484 return FAILURE;
3485
3486 if (sym->ns->parent == NULL)
3487 break;
3488
3489 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3490
3491 if (sym == NULL)
3492 break;
3493 }
3494
3495 sym = c->symtree->n.sym;
3496 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3497 sym->name, &c->loc);
3498
3499 return FAILURE;
3500 }
3501
3502
3503 /* Resolve a subroutine call not known to be generic nor specific. */
3504
3505 static gfc_try
3506 resolve_unknown_s (gfc_code *c)
3507 {
3508 gfc_symbol *sym;
3509
3510 sym = c->symtree->n.sym;
3511
3512 if (sym->attr.dummy)
3513 {
3514 sym->attr.proc = PROC_DUMMY;
3515 goto found;
3516 }
3517
3518 /* See if we have an intrinsic function reference. */
3519
3520 if (gfc_is_intrinsic (sym, 1, c->loc))
3521 {
3522 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3523 return SUCCESS;
3524 return FAILURE;
3525 }
3526
3527 /* The reference is to an external name. */
3528
3529 found:
3530 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3531
3532 c->resolved_sym = sym;
3533
3534 pure_subroutine (c, sym);
3535
3536 return SUCCESS;
3537 }
3538
3539
3540 /* Resolve a subroutine call. Although it was tempting to use the same code
3541 for functions, subroutines and functions are stored differently and this
3542 makes things awkward. */
3543
3544 static gfc_try
3545 resolve_call (gfc_code *c)
3546 {
3547 gfc_try t;
3548 procedure_type ptype = PROC_INTRINSIC;
3549 gfc_symbol *csym, *sym;
3550 bool no_formal_args;
3551
3552 csym = c->symtree ? c->symtree->n.sym : NULL;
3553
3554 if (csym && csym->ts.type != BT_UNKNOWN)
3555 {
3556 gfc_error ("'%s' at %L has a type, which is not consistent with "
3557 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3558 return FAILURE;
3559 }
3560
3561 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3562 {
3563 gfc_symtree *st;
3564 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3565 sym = st ? st->n.sym : NULL;
3566 if (sym && csym != sym
3567 && sym->ns == gfc_current_ns
3568 && sym->attr.flavor == FL_PROCEDURE
3569 && sym->attr.contained)
3570 {
3571 sym->refs++;
3572 if (csym->attr.generic)
3573 c->symtree->n.sym = sym;
3574 else
3575 c->symtree = st;
3576 csym = c->symtree->n.sym;
3577 }
3578 }
3579
3580 /* If this ia a deferred TBP with an abstract interface
3581 (which may of course be referenced), c->expr1 will be set. */
3582 if (csym && csym->attr.abstract && !c->expr1)
3583 {
3584 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3585 csym->name, &c->loc);
3586 return FAILURE;
3587 }
3588
3589 /* Subroutines without the RECURSIVE attribution are not allowed to
3590 * call themselves. */
3591 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3592 {
3593 if (csym->attr.entry && csym->ns->entries)
3594 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3595 " subroutine '%s' is not RECURSIVE",
3596 csym->name, &c->loc, csym->ns->entries->sym->name);
3597 else
3598 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3599 " is not RECURSIVE", csym->name, &c->loc);
3600
3601 t = FAILURE;
3602 }
3603
3604 /* Switch off assumed size checking and do this again for certain kinds
3605 of procedure, once the procedure itself is resolved. */
3606 need_full_assumed_size++;
3607
3608 if (csym)
3609 ptype = csym->attr.proc;
3610
3611 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3612 if (resolve_actual_arglist (c->ext.actual, ptype,
3613 no_formal_args) == FAILURE)
3614 return FAILURE;
3615
3616 /* Resume assumed_size checking. */
3617 need_full_assumed_size--;
3618
3619 /* If external, check for usage. */
3620 if (csym && is_external_proc (csym))
3621 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3622
3623 t = SUCCESS;
3624 if (c->resolved_sym == NULL)
3625 {
3626 c->resolved_isym = NULL;
3627 switch (procedure_kind (csym))
3628 {
3629 case PTYPE_GENERIC:
3630 t = resolve_generic_s (c);
3631 break;
3632
3633 case PTYPE_SPECIFIC:
3634 t = resolve_specific_s (c);
3635 break;
3636
3637 case PTYPE_UNKNOWN:
3638 t = resolve_unknown_s (c);
3639 break;
3640
3641 default:
3642 gfc_internal_error ("resolve_subroutine(): bad function type");
3643 }
3644 }
3645
3646 /* Some checks of elemental subroutine actual arguments. */
3647 if (resolve_elemental_actual (NULL, c) == FAILURE)
3648 return FAILURE;
3649
3650 return t;
3651 }
3652
3653
3654 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3655 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3656 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3657 if their shapes do not match. If either op1->shape or op2->shape is
3658 NULL, return SUCCESS. */
3659
3660 static gfc_try
3661 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3662 {
3663 gfc_try t;
3664 int i;
3665
3666 t = SUCCESS;
3667
3668 if (op1->shape != NULL && op2->shape != NULL)
3669 {
3670 for (i = 0; i < op1->rank; i++)
3671 {
3672 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3673 {
3674 gfc_error ("Shapes for operands at %L and %L are not conformable",
3675 &op1->where, &op2->where);
3676 t = FAILURE;
3677 break;
3678 }
3679 }
3680 }
3681
3682 return t;
3683 }
3684
3685
3686 /* Resolve an operator expression node. This can involve replacing the
3687 operation with a user defined function call. */
3688
3689 static gfc_try
3690 resolve_operator (gfc_expr *e)
3691 {
3692 gfc_expr *op1, *op2;
3693 char msg[200];
3694 bool dual_locus_error;
3695 gfc_try t;
3696
3697 /* Resolve all subnodes-- give them types. */
3698
3699 switch (e->value.op.op)
3700 {
3701 default:
3702 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3703 return FAILURE;
3704
3705 /* Fall through... */
3706
3707 case INTRINSIC_NOT:
3708 case INTRINSIC_UPLUS:
3709 case INTRINSIC_UMINUS:
3710 case INTRINSIC_PARENTHESES:
3711 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3712 return FAILURE;
3713 break;
3714 }
3715
3716 /* Typecheck the new node. */
3717
3718 op1 = e->value.op.op1;
3719 op2 = e->value.op.op2;
3720 dual_locus_error = false;
3721
3722 if ((op1 && op1->expr_type == EXPR_NULL)
3723 || (op2 && op2->expr_type == EXPR_NULL))
3724 {
3725 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3726 goto bad_op;
3727 }
3728
3729 switch (e->value.op.op)
3730 {
3731 case INTRINSIC_UPLUS:
3732 case INTRINSIC_UMINUS:
3733 if (op1->ts.type == BT_INTEGER
3734 || op1->ts.type == BT_REAL
3735 || op1->ts.type == BT_COMPLEX)
3736 {
3737 e->ts = op1->ts;
3738 break;
3739 }
3740
3741 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3742 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3743 goto bad_op;
3744
3745 case INTRINSIC_PLUS:
3746 case INTRINSIC_MINUS:
3747 case INTRINSIC_TIMES:
3748 case INTRINSIC_DIVIDE:
3749 case INTRINSIC_POWER:
3750 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3751 {
3752 gfc_type_convert_binary (e, 1);
3753 break;
3754 }
3755
3756 sprintf (msg,
3757 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3758 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3759 gfc_typename (&op2->ts));
3760 goto bad_op;
3761
3762 case INTRINSIC_CONCAT:
3763 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3764 && op1->ts.kind == op2->ts.kind)
3765 {
3766 e->ts.type = BT_CHARACTER;
3767 e->ts.kind = op1->ts.kind;
3768 break;
3769 }
3770
3771 sprintf (msg,
3772 _("Operands of string concatenation operator at %%L are %s/%s"),
3773 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3774 goto bad_op;
3775
3776 case INTRINSIC_AND:
3777 case INTRINSIC_OR:
3778 case INTRINSIC_EQV:
3779 case INTRINSIC_NEQV:
3780 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3781 {
3782 e->ts.type = BT_LOGICAL;
3783 e->ts.kind = gfc_kind_max (op1, op2);
3784 if (op1->ts.kind < e->ts.kind)
3785 gfc_convert_type (op1, &e->ts, 2);
3786 else if (op2->ts.kind < e->ts.kind)
3787 gfc_convert_type (op2, &e->ts, 2);
3788 break;
3789 }
3790
3791 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3792 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3793 gfc_typename (&op2->ts));
3794
3795 goto bad_op;
3796
3797 case INTRINSIC_NOT:
3798 if (op1->ts.type == BT_LOGICAL)
3799 {
3800 e->ts.type = BT_LOGICAL;
3801 e->ts.kind = op1->ts.kind;
3802 break;
3803 }
3804
3805 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3806 gfc_typename (&op1->ts));
3807 goto bad_op;
3808
3809 case INTRINSIC_GT:
3810 case INTRINSIC_GT_OS:
3811 case INTRINSIC_GE:
3812 case INTRINSIC_GE_OS:
3813 case INTRINSIC_LT:
3814 case INTRINSIC_LT_OS:
3815 case INTRINSIC_LE:
3816 case INTRINSIC_LE_OS:
3817 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3818 {
3819 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3820 goto bad_op;
3821 }
3822
3823 /* Fall through... */
3824
3825 case INTRINSIC_EQ:
3826 case INTRINSIC_EQ_OS:
3827 case INTRINSIC_NE:
3828 case INTRINSIC_NE_OS:
3829 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3830 && op1->ts.kind == op2->ts.kind)
3831 {
3832 e->ts.type = BT_LOGICAL;
3833 e->ts.kind = gfc_default_logical_kind;
3834 break;
3835 }
3836
3837 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3838 {
3839 gfc_type_convert_binary (e, 1);
3840
3841 e->ts.type = BT_LOGICAL;
3842 e->ts.kind = gfc_default_logical_kind;
3843 break;
3844 }
3845
3846 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3847 sprintf (msg,
3848 _("Logicals at %%L must be compared with %s instead of %s"),
3849 (e->value.op.op == INTRINSIC_EQ
3850 || e->value.op.op == INTRINSIC_EQ_OS)
3851 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3852 else
3853 sprintf (msg,
3854 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3855 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3856 gfc_typename (&op2->ts));
3857
3858 goto bad_op;
3859
3860 case INTRINSIC_USER:
3861 if (e->value.op.uop->op == NULL)
3862 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3863 else if (op2 == NULL)
3864 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3865 e->value.op.uop->name, gfc_typename (&op1->ts));
3866 else
3867 {
3868 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3869 e->value.op.uop->name, gfc_typename (&op1->ts),
3870 gfc_typename (&op2->ts));
3871 e->value.op.uop->op->sym->attr.referenced = 1;
3872 }
3873
3874 goto bad_op;
3875
3876 case INTRINSIC_PARENTHESES:
3877 e->ts = op1->ts;
3878 if (e->ts.type == BT_CHARACTER)
3879 e->ts.u.cl = op1->ts.u.cl;
3880 break;
3881
3882 default:
3883 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3884 }
3885
3886 /* Deal with arrayness of an operand through an operator. */
3887
3888 t = SUCCESS;
3889
3890 switch (e->value.op.op)
3891 {
3892 case INTRINSIC_PLUS:
3893 case INTRINSIC_MINUS:
3894 case INTRINSIC_TIMES:
3895 case INTRINSIC_DIVIDE:
3896 case INTRINSIC_POWER:
3897 case INTRINSIC_CONCAT:
3898 case INTRINSIC_AND:
3899 case INTRINSIC_OR:
3900 case INTRINSIC_EQV:
3901 case INTRINSIC_NEQV:
3902 case INTRINSIC_EQ:
3903 case INTRINSIC_EQ_OS:
3904 case INTRINSIC_NE:
3905 case INTRINSIC_NE_OS:
3906 case INTRINSIC_GT:
3907 case INTRINSIC_GT_OS:
3908 case INTRINSIC_GE:
3909 case INTRINSIC_GE_OS:
3910 case INTRINSIC_LT:
3911 case INTRINSIC_LT_OS:
3912 case INTRINSIC_LE:
3913 case INTRINSIC_LE_OS:
3914
3915 if (op1->rank == 0 && op2->rank == 0)
3916 e->rank = 0;
3917
3918 if (op1->rank == 0 && op2->rank != 0)
3919 {
3920 e->rank = op2->rank;
3921
3922 if (e->shape == NULL)
3923 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3924 }
3925
3926 if (op1->rank != 0 && op2->rank == 0)
3927 {
3928 e->rank = op1->rank;
3929
3930 if (e->shape == NULL)
3931 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3932 }
3933
3934 if (op1->rank != 0 && op2->rank != 0)
3935 {
3936 if (op1->rank == op2->rank)
3937 {
3938 e->rank = op1->rank;
3939 if (e->shape == NULL)
3940 {
3941 t = compare_shapes (op1, op2);
3942 if (t == FAILURE)
3943 e->shape = NULL;
3944 else
3945 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3946 }
3947 }
3948 else
3949 {
3950 /* Allow higher level expressions to work. */
3951 e->rank = 0;
3952
3953 /* Try user-defined operators, and otherwise throw an error. */
3954 dual_locus_error = true;
3955 sprintf (msg,
3956 _("Inconsistent ranks for operator at %%L and %%L"));
3957 goto bad_op;
3958 }
3959 }
3960
3961 break;
3962
3963 case INTRINSIC_PARENTHESES:
3964 case INTRINSIC_NOT:
3965 case INTRINSIC_UPLUS:
3966 case INTRINSIC_UMINUS:
3967 /* Simply copy arrayness attribute */
3968 e->rank = op1->rank;
3969
3970 if (e->shape == NULL)
3971 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3972
3973 break;
3974
3975 default:
3976 break;
3977 }
3978
3979 /* Attempt to simplify the expression. */
3980 if (t == SUCCESS)
3981 {
3982 t = gfc_simplify_expr (e, 0);
3983 /* Some calls do not succeed in simplification and return FAILURE
3984 even though there is no error; e.g. variable references to
3985 PARAMETER arrays. */
3986 if (!gfc_is_constant_expr (e))
3987 t = SUCCESS;
3988 }
3989 return t;
3990
3991 bad_op:
3992
3993 {
3994 bool real_error;
3995 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3996 return SUCCESS;
3997
3998 if (real_error)
3999 return FAILURE;
4000 }
4001
4002 if (dual_locus_error)
4003 gfc_error (msg, &op1->where, &op2->where);
4004 else
4005 gfc_error (msg, &e->where);
4006
4007 return FAILURE;
4008 }
4009
4010
4011 /************** Array resolution subroutines **************/
4012
4013 typedef enum
4014 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
4015 comparison;
4016
4017 /* Compare two integer expressions. */
4018
4019 static comparison
4020 compare_bound (gfc_expr *a, gfc_expr *b)
4021 {
4022 int i;
4023
4024 if (a == NULL || a->expr_type != EXPR_CONSTANT
4025 || b == NULL || b->expr_type != EXPR_CONSTANT)
4026 return CMP_UNKNOWN;
4027
4028 /* If either of the types isn't INTEGER, we must have
4029 raised an error earlier. */
4030
4031 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4032 return CMP_UNKNOWN;
4033
4034 i = mpz_cmp (a->value.integer, b->value.integer);
4035
4036 if (i < 0)
4037 return CMP_LT;
4038 if (i > 0)
4039 return CMP_GT;
4040 return CMP_EQ;
4041 }
4042
4043
4044 /* Compare an integer expression with an integer. */
4045
4046 static comparison
4047 compare_bound_int (gfc_expr *a, int b)
4048 {
4049 int i;
4050
4051 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4052 return CMP_UNKNOWN;
4053
4054 if (a->ts.type != BT_INTEGER)
4055 gfc_internal_error ("compare_bound_int(): Bad expression");
4056
4057 i = mpz_cmp_si (a->value.integer, b);
4058
4059 if (i < 0)
4060 return CMP_LT;
4061 if (i > 0)
4062 return CMP_GT;
4063 return CMP_EQ;
4064 }
4065
4066
4067 /* Compare an integer expression with a mpz_t. */
4068
4069 static comparison
4070 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4071 {
4072 int i;
4073
4074 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4075 return CMP_UNKNOWN;
4076
4077 if (a->ts.type != BT_INTEGER)
4078 gfc_internal_error ("compare_bound_int(): Bad expression");
4079
4080 i = mpz_cmp (a->value.integer, b);
4081
4082 if (i < 0)
4083 return CMP_LT;
4084 if (i > 0)
4085 return CMP_GT;
4086 return CMP_EQ;
4087 }
4088
4089
4090 /* Compute the last value of a sequence given by a triplet.
4091 Return 0 if it wasn't able to compute the last value, or if the
4092 sequence if empty, and 1 otherwise. */
4093
4094 static int
4095 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4096 gfc_expr *stride, mpz_t last)
4097 {
4098 mpz_t rem;
4099
4100 if (start == NULL || start->expr_type != EXPR_CONSTANT
4101 || end == NULL || end->expr_type != EXPR_CONSTANT
4102 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4103 return 0;
4104
4105 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4106 || (stride != NULL && stride->ts.type != BT_INTEGER))
4107 return 0;
4108
4109 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4110 {
4111 if (compare_bound (start, end) == CMP_GT)
4112 return 0;
4113 mpz_set (last, end->value.integer);
4114 return 1;
4115 }
4116
4117 if (compare_bound_int (stride, 0) == CMP_GT)
4118 {
4119 /* Stride is positive */
4120 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4121 return 0;
4122 }
4123 else
4124 {
4125 /* Stride is negative */
4126 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4127 return 0;
4128 }
4129
4130 mpz_init (rem);
4131 mpz_sub (rem, end->value.integer, start->value.integer);
4132 mpz_tdiv_r (rem, rem, stride->value.integer);
4133 mpz_sub (last, end->value.integer, rem);
4134 mpz_clear (rem);
4135
4136 return 1;
4137 }
4138
4139
4140 /* Compare a single dimension of an array reference to the array
4141 specification. */
4142
4143 static gfc_try
4144 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4145 {
4146 mpz_t last_value;
4147
4148 if (ar->dimen_type[i] == DIMEN_STAR)
4149 {
4150 gcc_assert (ar->stride[i] == NULL);
4151 /* This implies [*] as [*:] and [*:3] are not possible. */
4152 if (ar->start[i] == NULL)
4153 {
4154 gcc_assert (ar->end[i] == NULL);
4155 return SUCCESS;
4156 }
4157 }
4158
4159 /* Given start, end and stride values, calculate the minimum and
4160 maximum referenced indexes. */
4161
4162 switch (ar->dimen_type[i])
4163 {
4164 case DIMEN_VECTOR:
4165 case DIMEN_THIS_IMAGE:
4166 break;
4167
4168 case DIMEN_STAR:
4169 case DIMEN_ELEMENT:
4170 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4171 {
4172 if (i < as->rank)
4173 gfc_warning ("Array reference at %L is out of bounds "
4174 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4175 mpz_get_si (ar->start[i]->value.integer),
4176 mpz_get_si (as->lower[i]->value.integer), i+1);
4177 else
4178 gfc_warning ("Array reference at %L is out of bounds "
4179 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4180 mpz_get_si (ar->start[i]->value.integer),
4181 mpz_get_si (as->lower[i]->value.integer),
4182 i + 1 - as->rank);
4183 return SUCCESS;
4184 }
4185 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4186 {
4187 if (i < as->rank)
4188 gfc_warning ("Array reference at %L is out of bounds "
4189 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4190 mpz_get_si (ar->start[i]->value.integer),
4191 mpz_get_si (as->upper[i]->value.integer), i+1);
4192 else
4193 gfc_warning ("Array reference at %L is out of bounds "
4194 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4195 mpz_get_si (ar->start[i]->value.integer),
4196 mpz_get_si (as->upper[i]->value.integer),
4197 i + 1 - as->rank);
4198 return SUCCESS;
4199 }
4200
4201 break;
4202
4203 case DIMEN_RANGE:
4204 {
4205 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4206 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4207
4208 comparison comp_start_end = compare_bound (AR_START, AR_END);
4209
4210 /* Check for zero stride, which is not allowed. */
4211 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4212 {
4213 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4214 return FAILURE;
4215 }
4216
4217 /* if start == len || (stride > 0 && start < len)
4218 || (stride < 0 && start > len),
4219 then the array section contains at least one element. In this
4220 case, there is an out-of-bounds access if
4221 (start < lower || start > upper). */
4222 if (compare_bound (AR_START, AR_END) == CMP_EQ
4223 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4224 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4225 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4226 && comp_start_end == CMP_GT))
4227 {
4228 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4229 {
4230 gfc_warning ("Lower array reference at %L is out of bounds "
4231 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4232 mpz_get_si (AR_START->value.integer),
4233 mpz_get_si (as->lower[i]->value.integer), i+1);
4234 return SUCCESS;
4235 }
4236 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4237 {
4238 gfc_warning ("Lower array reference at %L is out of bounds "
4239 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4240 mpz_get_si (AR_START->value.integer),
4241 mpz_get_si (as->upper[i]->value.integer), i+1);
4242 return SUCCESS;
4243 }
4244 }
4245
4246 /* If we can compute the highest index of the array section,
4247 then it also has to be between lower and upper. */
4248 mpz_init (last_value);
4249 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4250 last_value))
4251 {
4252 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4253 {
4254 gfc_warning ("Upper array reference at %L is out of bounds "
4255 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4256 mpz_get_si (last_value),
4257 mpz_get_si (as->lower[i]->value.integer), i+1);
4258 mpz_clear (last_value);
4259 return SUCCESS;
4260 }
4261 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4262 {
4263 gfc_warning ("Upper array reference at %L is out of bounds "
4264 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4265 mpz_get_si (last_value),
4266 mpz_get_si (as->upper[i]->value.integer), i+1);
4267 mpz_clear (last_value);
4268 return SUCCESS;
4269 }
4270 }
4271 mpz_clear (last_value);
4272
4273 #undef AR_START
4274 #undef AR_END
4275 }
4276 break;
4277
4278 default:
4279 gfc_internal_error ("check_dimension(): Bad array reference");
4280 }
4281
4282 return SUCCESS;
4283 }
4284
4285
4286 /* Compare an array reference with an array specification. */
4287
4288 static gfc_try
4289 compare_spec_to_ref (gfc_array_ref *ar)
4290 {
4291 gfc_array_spec *as;
4292 int i;
4293
4294 as = ar->as;
4295 i = as->rank - 1;
4296 /* TODO: Full array sections are only allowed as actual parameters. */
4297 if (as->type == AS_ASSUMED_SIZE
4298 && (/*ar->type == AR_FULL
4299 ||*/ (ar->type == AR_SECTION
4300 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4301 {
4302 gfc_error ("Rightmost upper bound of assumed size array section "
4303 "not specified at %L", &ar->where);
4304 return FAILURE;
4305 }
4306
4307 if (ar->type == AR_FULL)
4308 return SUCCESS;
4309
4310 if (as->rank != ar->dimen)
4311 {
4312 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4313 &ar->where, ar->dimen, as->rank);
4314 return FAILURE;
4315 }
4316
4317 /* ar->codimen == 0 is a local array. */
4318 if (as->corank != ar->codimen && ar->codimen != 0)
4319 {
4320 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4321 &ar->where, ar->codimen, as->corank);
4322 return FAILURE;
4323 }
4324
4325 for (i = 0; i < as->rank; i++)
4326 if (check_dimension (i, ar, as) == FAILURE)
4327 return FAILURE;
4328
4329 /* Local access has no coarray spec. */
4330 if (ar->codimen != 0)
4331 for (i = as->rank; i < as->rank + as->corank; i++)
4332 {
4333 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4334 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4335 {
4336 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4337 i + 1 - as->rank, &ar->where);
4338 return FAILURE;
4339 }
4340 if (check_dimension (i, ar, as) == FAILURE)
4341 return FAILURE;
4342 }
4343
4344 if (as->corank && ar->codimen == 0)
4345 {
4346 int n;
4347 ar->codimen = as->corank;
4348 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4349 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4350 }
4351
4352 return SUCCESS;
4353 }
4354
4355
4356 /* Resolve one part of an array index. */
4357
4358 static gfc_try
4359 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4360 int force_index_integer_kind)
4361 {
4362 gfc_typespec ts;
4363
4364 if (index == NULL)
4365 return SUCCESS;
4366
4367 if (gfc_resolve_expr (index) == FAILURE)
4368 return FAILURE;
4369
4370 if (check_scalar && index->rank != 0)
4371 {
4372 gfc_error ("Array index at %L must be scalar", &index->where);
4373 return FAILURE;
4374 }
4375
4376 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4377 {
4378 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4379 &index->where, gfc_basic_typename (index->ts.type));
4380 return FAILURE;
4381 }
4382
4383 if (index->ts.type == BT_REAL)
4384 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4385 &index->where) == FAILURE)
4386 return FAILURE;
4387
4388 if ((index->ts.kind != gfc_index_integer_kind
4389 && force_index_integer_kind)
4390 || index->ts.type != BT_INTEGER)
4391 {
4392 gfc_clear_ts (&ts);
4393 ts.type = BT_INTEGER;
4394 ts.kind = gfc_index_integer_kind;
4395
4396 gfc_convert_type_warn (index, &ts, 2, 0);
4397 }
4398
4399 return SUCCESS;
4400 }
4401
4402 /* Resolve one part of an array index. */
4403
4404 gfc_try
4405 gfc_resolve_index (gfc_expr *index, int check_scalar)
4406 {
4407 return gfc_resolve_index_1 (index, check_scalar, 1);
4408 }
4409
4410 /* Resolve a dim argument to an intrinsic function. */
4411
4412 gfc_try
4413 gfc_resolve_dim_arg (gfc_expr *dim)
4414 {
4415 if (dim == NULL)
4416 return SUCCESS;
4417
4418 if (gfc_resolve_expr (dim) == FAILURE)
4419 return FAILURE;
4420
4421 if (dim->rank != 0)
4422 {
4423 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4424 return FAILURE;
4425
4426 }
4427
4428 if (dim->ts.type != BT_INTEGER)
4429 {
4430 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4431 return FAILURE;
4432 }
4433
4434 if (dim->ts.kind != gfc_index_integer_kind)
4435 {
4436 gfc_typespec ts;
4437
4438 gfc_clear_ts (&ts);
4439 ts.type = BT_INTEGER;
4440 ts.kind = gfc_index_integer_kind;
4441
4442 gfc_convert_type_warn (dim, &ts, 2, 0);
4443 }
4444
4445 return SUCCESS;
4446 }
4447
4448 /* Given an expression that contains array references, update those array
4449 references to point to the right array specifications. While this is
4450 filled in during matching, this information is difficult to save and load
4451 in a module, so we take care of it here.
4452
4453 The idea here is that the original array reference comes from the
4454 base symbol. We traverse the list of reference structures, setting
4455 the stored reference to references. Component references can
4456 provide an additional array specification. */
4457
4458 static void
4459 find_array_spec (gfc_expr *e)
4460 {
4461 gfc_array_spec *as;
4462 gfc_component *c;
4463 gfc_symbol *derived;
4464 gfc_ref *ref;
4465
4466 if (e->symtree->n.sym->ts.type == BT_CLASS)
4467 as = CLASS_DATA (e->symtree->n.sym)->as;
4468 else
4469 as = e->symtree->n.sym->as;
4470 derived = NULL;
4471
4472 for (ref = e->ref; ref; ref = ref->next)
4473 switch (ref->type)
4474 {
4475 case REF_ARRAY:
4476 if (as == NULL)
4477 gfc_internal_error ("find_array_spec(): Missing spec");
4478
4479 ref->u.ar.as = as;
4480 as = NULL;
4481 break;
4482
4483 case REF_COMPONENT:
4484 if (derived == NULL)
4485 derived = e->symtree->n.sym->ts.u.derived;
4486
4487 if (derived->attr.is_class)
4488 derived = derived->components->ts.u.derived;
4489
4490 c = derived->components;
4491
4492 for (; c; c = c->next)
4493 if (c == ref->u.c.component)
4494 {
4495 /* Track the sequence of component references. */
4496 if (c->ts.type == BT_DERIVED)
4497 derived = c->ts.u.derived;
4498 break;
4499 }
4500
4501 if (c == NULL)
4502 gfc_internal_error ("find_array_spec(): Component not found");
4503
4504 if (c->attr.dimension)
4505 {
4506 if (as != NULL)
4507 gfc_internal_error ("find_array_spec(): unused as(1)");
4508 as = c->as;
4509 }
4510
4511 break;
4512
4513 case REF_SUBSTRING:
4514 break;
4515 }
4516
4517 if (as != NULL)
4518 gfc_internal_error ("find_array_spec(): unused as(2)");
4519 }
4520
4521
4522 /* Resolve an array reference. */
4523
4524 static gfc_try
4525 resolve_array_ref (gfc_array_ref *ar)
4526 {
4527 int i, check_scalar;
4528 gfc_expr *e;
4529
4530 for (i = 0; i < ar->dimen + ar->codimen; i++)
4531 {
4532 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4533
4534 /* Do not force gfc_index_integer_kind for the start. We can
4535 do fine with any integer kind. This avoids temporary arrays
4536 created for indexing with a vector. */
4537 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4538 return FAILURE;
4539 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4540 return FAILURE;
4541 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4542 return FAILURE;
4543
4544 e = ar->start[i];
4545
4546 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4547 switch (e->rank)
4548 {
4549 case 0:
4550 ar->dimen_type[i] = DIMEN_ELEMENT;
4551 break;
4552
4553 case 1:
4554 ar->dimen_type[i] = DIMEN_VECTOR;
4555 if (e->expr_type == EXPR_VARIABLE
4556 && e->symtree->n.sym->ts.type == BT_DERIVED)
4557 ar->start[i] = gfc_get_parentheses (e);
4558 break;
4559
4560 default:
4561 gfc_error ("Array index at %L is an array of rank %d",
4562 &ar->c_where[i], e->rank);
4563 return FAILURE;
4564 }
4565
4566 /* Fill in the upper bound, which may be lower than the
4567 specified one for something like a(2:10:5), which is
4568 identical to a(2:7:5). Only relevant for strides not equal
4569 to one. */
4570 if (ar->dimen_type[i] == DIMEN_RANGE
4571 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4572 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0)
4573 {
4574 mpz_t size, end;
4575
4576 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4577 {
4578 if (ar->end[i] == NULL)
4579 {
4580 ar->end[i] =
4581 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4582 &ar->where);
4583 mpz_set (ar->end[i]->value.integer, end);
4584 }
4585 else if (ar->end[i]->ts.type == BT_INTEGER
4586 && ar->end[i]->expr_type == EXPR_CONSTANT)
4587 {
4588 mpz_set (ar->end[i]->value.integer, end);
4589 }
4590 else
4591 gcc_unreachable ();
4592
4593 mpz_clear (size);
4594 mpz_clear (end);
4595 }
4596 }
4597 }
4598
4599 if (ar->type == AR_FULL && ar->as->rank == 0)
4600 ar->type = AR_ELEMENT;
4601
4602 /* If the reference type is unknown, figure out what kind it is. */
4603
4604 if (ar->type == AR_UNKNOWN)
4605 {
4606 ar->type = AR_ELEMENT;
4607 for (i = 0; i < ar->dimen; i++)
4608 if (ar->dimen_type[i] == DIMEN_RANGE
4609 || ar->dimen_type[i] == DIMEN_VECTOR)
4610 {
4611 ar->type = AR_SECTION;
4612 break;
4613 }
4614 }
4615
4616 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4617 return FAILURE;
4618
4619 return SUCCESS;
4620 }
4621
4622
4623 static gfc_try
4624 resolve_substring (gfc_ref *ref)
4625 {
4626 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4627
4628 if (ref->u.ss.start != NULL)
4629 {
4630 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4631 return FAILURE;
4632
4633 if (ref->u.ss.start->ts.type != BT_INTEGER)
4634 {
4635 gfc_error ("Substring start index at %L must be of type INTEGER",
4636 &ref->u.ss.start->where);
4637 return FAILURE;
4638 }
4639
4640 if (ref->u.ss.start->rank != 0)
4641 {
4642 gfc_error ("Substring start index at %L must be scalar",
4643 &ref->u.ss.start->where);
4644 return FAILURE;
4645 }
4646
4647 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4648 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4649 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4650 {
4651 gfc_error ("Substring start index at %L is less than one",
4652 &ref->u.ss.start->where);
4653 return FAILURE;
4654 }
4655 }
4656
4657 if (ref->u.ss.end != NULL)
4658 {
4659 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4660 return FAILURE;
4661
4662 if (ref->u.ss.end->ts.type != BT_INTEGER)
4663 {
4664 gfc_error ("Substring end index at %L must be of type INTEGER",
4665 &ref->u.ss.end->where);
4666 return FAILURE;
4667 }
4668
4669 if (ref->u.ss.end->rank != 0)
4670 {
4671 gfc_error ("Substring end index at %L must be scalar",
4672 &ref->u.ss.end->where);
4673 return FAILURE;
4674 }
4675
4676 if (ref->u.ss.length != NULL
4677 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4678 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4679 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4680 {
4681 gfc_error ("Substring end index at %L exceeds the string length",
4682 &ref->u.ss.start->where);
4683 return FAILURE;
4684 }
4685
4686 if (compare_bound_mpz_t (ref->u.ss.end,
4687 gfc_integer_kinds[k].huge) == CMP_GT
4688 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4689 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4690 {
4691 gfc_error ("Substring end index at %L is too large",
4692 &ref->u.ss.end->where);
4693 return FAILURE;
4694 }
4695 }
4696
4697 return SUCCESS;
4698 }
4699
4700
4701 /* This function supplies missing substring charlens. */
4702
4703 void
4704 gfc_resolve_substring_charlen (gfc_expr *e)
4705 {
4706 gfc_ref *char_ref;
4707 gfc_expr *start, *end;
4708
4709 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4710 if (char_ref->type == REF_SUBSTRING)
4711 break;
4712
4713 if (!char_ref)
4714 return;
4715
4716 gcc_assert (char_ref->next == NULL);
4717
4718 if (e->ts.u.cl)
4719 {
4720 if (e->ts.u.cl->length)
4721 gfc_free_expr (e->ts.u.cl->length);
4722 else if (e->expr_type == EXPR_VARIABLE
4723 && e->symtree->n.sym->attr.dummy)
4724 return;
4725 }
4726
4727 e->ts.type = BT_CHARACTER;
4728 e->ts.kind = gfc_default_character_kind;
4729
4730 if (!e->ts.u.cl)
4731 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4732
4733 if (char_ref->u.ss.start)
4734 start = gfc_copy_expr (char_ref->u.ss.start);
4735 else
4736 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4737
4738 if (char_ref->u.ss.end)
4739 end = gfc_copy_expr (char_ref->u.ss.end);
4740 else if (e->expr_type == EXPR_VARIABLE)
4741 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4742 else
4743 end = NULL;
4744
4745 if (!start || !end)
4746 return;
4747
4748 /* Length = (end - start +1). */
4749 e->ts.u.cl->length = gfc_subtract (end, start);
4750 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4751 gfc_get_int_expr (gfc_default_integer_kind,
4752 NULL, 1));
4753
4754 e->ts.u.cl->length->ts.type = BT_INTEGER;
4755 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4756
4757 /* Make sure that the length is simplified. */
4758 gfc_simplify_expr (e->ts.u.cl->length, 1);
4759 gfc_resolve_expr (e->ts.u.cl->length);
4760 }
4761
4762
4763 /* Resolve subtype references. */
4764
4765 static gfc_try
4766 resolve_ref (gfc_expr *expr)
4767 {
4768 int current_part_dimension, n_components, seen_part_dimension;
4769 gfc_ref *ref;
4770
4771 for (ref = expr->ref; ref; ref = ref->next)
4772 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4773 {
4774 find_array_spec (expr);
4775 break;
4776 }
4777
4778 for (ref = expr->ref; ref; ref = ref->next)
4779 switch (ref->type)
4780 {
4781 case REF_ARRAY:
4782 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4783 return FAILURE;
4784 break;
4785
4786 case REF_COMPONENT:
4787 break;
4788
4789 case REF_SUBSTRING:
4790 resolve_substring (ref);
4791 break;
4792 }
4793
4794 /* Check constraints on part references. */
4795
4796 current_part_dimension = 0;
4797 seen_part_dimension = 0;
4798 n_components = 0;
4799
4800 for (ref = expr->ref; ref; ref = ref->next)
4801 {
4802 switch (ref->type)
4803 {
4804 case REF_ARRAY:
4805 switch (ref->u.ar.type)
4806 {
4807 case AR_FULL:
4808 /* Coarray scalar. */
4809 if (ref->u.ar.as->rank == 0)
4810 {
4811 current_part_dimension = 0;
4812 break;
4813 }
4814 /* Fall through. */
4815 case AR_SECTION:
4816 current_part_dimension = 1;
4817 break;
4818
4819 case AR_ELEMENT:
4820 current_part_dimension = 0;
4821 break;
4822
4823 case AR_UNKNOWN:
4824 gfc_internal_error ("resolve_ref(): Bad array reference");
4825 }
4826
4827 break;
4828
4829 case REF_COMPONENT:
4830 if (current_part_dimension || seen_part_dimension)
4831 {
4832 /* F03:C614. */
4833 if (ref->u.c.component->attr.pointer
4834 || ref->u.c.component->attr.proc_pointer)
4835 {
4836 gfc_error ("Component to the right of a part reference "
4837 "with nonzero rank must not have the POINTER "
4838 "attribute at %L", &expr->where);
4839 return FAILURE;
4840 }
4841 else if (ref->u.c.component->attr.allocatable)
4842 {
4843 gfc_error ("Component to the right of a part reference "
4844 "with nonzero rank must not have the ALLOCATABLE "
4845 "attribute at %L", &expr->where);
4846 return FAILURE;
4847 }
4848 }
4849
4850 n_components++;
4851 break;
4852
4853 case REF_SUBSTRING:
4854 break;
4855 }
4856
4857 if (((ref->type == REF_COMPONENT && n_components > 1)
4858 || ref->next == NULL)
4859 && current_part_dimension
4860 && seen_part_dimension)
4861 {
4862 gfc_error ("Two or more part references with nonzero rank must "
4863 "not be specified at %L", &expr->where);
4864 return FAILURE;
4865 }
4866
4867 if (ref->type == REF_COMPONENT)
4868 {
4869 if (current_part_dimension)
4870 seen_part_dimension = 1;
4871
4872 /* reset to make sure */
4873 current_part_dimension = 0;
4874 }
4875 }
4876
4877 return SUCCESS;
4878 }
4879
4880
4881 /* Given an expression, determine its shape. This is easier than it sounds.
4882 Leaves the shape array NULL if it is not possible to determine the shape. */
4883
4884 static void
4885 expression_shape (gfc_expr *e)
4886 {
4887 mpz_t array[GFC_MAX_DIMENSIONS];
4888 int i;
4889
4890 if (e->rank == 0 || e->shape != NULL)
4891 return;
4892
4893 for (i = 0; i < e->rank; i++)
4894 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4895 goto fail;
4896
4897 e->shape = gfc_get_shape (e->rank);
4898
4899 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4900
4901 return;
4902
4903 fail:
4904 for (i--; i >= 0; i--)
4905 mpz_clear (array[i]);
4906 }
4907
4908
4909 /* Given a variable expression node, compute the rank of the expression by
4910 examining the base symbol and any reference structures it may have. */
4911
4912 static void
4913 expression_rank (gfc_expr *e)
4914 {
4915 gfc_ref *ref;
4916 int i, rank;
4917
4918 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4919 could lead to serious confusion... */
4920 gcc_assert (e->expr_type != EXPR_COMPCALL);
4921
4922 if (e->ref == NULL)
4923 {
4924 if (e->expr_type == EXPR_ARRAY)
4925 goto done;
4926 /* Constructors can have a rank different from one via RESHAPE(). */
4927
4928 if (e->symtree == NULL)
4929 {
4930 e->rank = 0;
4931 goto done;
4932 }
4933
4934 e->rank = (e->symtree->n.sym->as == NULL)
4935 ? 0 : e->symtree->n.sym->as->rank;
4936 goto done;
4937 }
4938
4939 rank = 0;
4940
4941 for (ref = e->ref; ref; ref = ref->next)
4942 {
4943 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4944 && ref->u.c.component->attr.function && !ref->next)
4945 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4946
4947 if (ref->type != REF_ARRAY)
4948 continue;
4949
4950 if (ref->u.ar.type == AR_FULL)
4951 {
4952 rank = ref->u.ar.as->rank;
4953 break;
4954 }
4955
4956 if (ref->u.ar.type == AR_SECTION)
4957 {
4958 /* Figure out the rank of the section. */
4959 if (rank != 0)
4960 gfc_internal_error ("expression_rank(): Two array specs");
4961
4962 for (i = 0; i < ref->u.ar.dimen; i++)
4963 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4964 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4965 rank++;
4966
4967 break;
4968 }
4969 }
4970
4971 e->rank = rank;
4972
4973 done:
4974 expression_shape (e);
4975 }
4976
4977
4978 /* Resolve a variable expression. */
4979
4980 static gfc_try
4981 resolve_variable (gfc_expr *e)
4982 {
4983 gfc_symbol *sym;
4984 gfc_try t;
4985
4986 t = SUCCESS;
4987
4988 if (e->symtree == NULL)
4989 return FAILURE;
4990 sym = e->symtree->n.sym;
4991
4992 /* If this is an associate-name, it may be parsed with an array reference
4993 in error even though the target is scalar. Fail directly in this case. */
4994 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4995 return FAILURE;
4996
4997 /* On the other hand, the parser may not have known this is an array;
4998 in this case, we have to add a FULL reference. */
4999 if (sym->assoc && sym->attr.dimension && !e->ref)
5000 {
5001 e->ref = gfc_get_ref ();
5002 e->ref->type = REF_ARRAY;
5003 e->ref->u.ar.type = AR_FULL;
5004 e->ref->u.ar.dimen = 0;
5005 }
5006
5007 if (e->ref && resolve_ref (e) == FAILURE)
5008 return FAILURE;
5009
5010 if (sym->attr.flavor == FL_PROCEDURE
5011 && (!sym->attr.function
5012 || (sym->attr.function && sym->result
5013 && sym->result->attr.proc_pointer
5014 && !sym->result->attr.function)))
5015 {
5016 e->ts.type = BT_PROCEDURE;
5017 goto resolve_procedure;
5018 }
5019
5020 if (sym->ts.type != BT_UNKNOWN)
5021 gfc_variable_attr (e, &e->ts);
5022 else
5023 {
5024 /* Must be a simple variable reference. */
5025 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
5026 return FAILURE;
5027 e->ts = sym->ts;
5028 }
5029
5030 if (check_assumed_size_reference (sym, e))
5031 return FAILURE;
5032
5033 /* Deal with forward references to entries during resolve_code, to
5034 satisfy, at least partially, 12.5.2.5. */
5035 if (gfc_current_ns->entries
5036 && current_entry_id == sym->entry_id
5037 && cs_base
5038 && cs_base->current
5039 && cs_base->current->op != EXEC_ENTRY)
5040 {
5041 gfc_entry_list *entry;
5042 gfc_formal_arglist *formal;
5043 int n;
5044 bool seen;
5045
5046 /* If the symbol is a dummy... */
5047 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5048 {
5049 entry = gfc_current_ns->entries;
5050 seen = false;
5051
5052 /* ...test if the symbol is a parameter of previous entries. */
5053 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5054 for (formal = entry->sym->formal; formal; formal = formal->next)
5055 {
5056 if (formal->sym && sym->name == formal->sym->name)
5057 seen = true;
5058 }
5059
5060 /* If it has not been seen as a dummy, this is an error. */
5061 if (!seen)
5062 {
5063 if (specification_expr)
5064 gfc_error ("Variable '%s', used in a specification expression"
5065 ", is referenced at %L before the ENTRY statement "
5066 "in which it is a parameter",
5067 sym->name, &cs_base->current->loc);
5068 else
5069 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5070 "statement in which it is a parameter",
5071 sym->name, &cs_base->current->loc);
5072 t = FAILURE;
5073 }
5074 }
5075
5076 /* Now do the same check on the specification expressions. */
5077 specification_expr = 1;
5078 if (sym->ts.type == BT_CHARACTER
5079 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5080 t = FAILURE;
5081
5082 if (sym->as)
5083 for (n = 0; n < sym->as->rank; n++)
5084 {
5085 specification_expr = 1;
5086 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5087 t = FAILURE;
5088 specification_expr = 1;
5089 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5090 t = FAILURE;
5091 }
5092 specification_expr = 0;
5093
5094 if (t == SUCCESS)
5095 /* Update the symbol's entry level. */
5096 sym->entry_id = current_entry_id + 1;
5097 }
5098
5099 /* If a symbol has been host_associated mark it. This is used latter,
5100 to identify if aliasing is possible via host association. */
5101 if (sym->attr.flavor == FL_VARIABLE
5102 && gfc_current_ns->parent
5103 && (gfc_current_ns->parent == sym->ns
5104 || (gfc_current_ns->parent->parent
5105 && gfc_current_ns->parent->parent == sym->ns)))
5106 sym->attr.host_assoc = 1;
5107
5108 resolve_procedure:
5109 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5110 t = FAILURE;
5111
5112 /* F2008, C617 and C1229. */
5113 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5114 && gfc_is_coindexed (e))
5115 {
5116 gfc_ref *ref, *ref2 = NULL;
5117
5118 for (ref = e->ref; ref; ref = ref->next)
5119 {
5120 if (ref->type == REF_COMPONENT)
5121 ref2 = ref;
5122 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5123 break;
5124 }
5125
5126 for ( ; ref; ref = ref->next)
5127 if (ref->type == REF_COMPONENT)
5128 break;
5129
5130 /* Expression itself is not coindexed object. */
5131 if (ref && e->ts.type == BT_CLASS)
5132 {
5133 gfc_error ("Polymorphic subobject of coindexed object at %L",
5134 &e->where);
5135 t = FAILURE;
5136 }
5137
5138 /* Expression itself is coindexed object. */
5139 if (ref == NULL)
5140 {
5141 gfc_component *c;
5142 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5143 for ( ; c; c = c->next)
5144 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5145 {
5146 gfc_error ("Coindexed object with polymorphic allocatable "
5147 "subcomponent at %L", &e->where);
5148 t = FAILURE;
5149 break;
5150 }
5151 }
5152 }
5153
5154 return t;
5155 }
5156
5157
5158 /* Checks to see that the correct symbol has been host associated.
5159 The only situation where this arises is that in which a twice
5160 contained function is parsed after the host association is made.
5161 Therefore, on detecting this, change the symbol in the expression
5162 and convert the array reference into an actual arglist if the old
5163 symbol is a variable. */
5164 static bool
5165 check_host_association (gfc_expr *e)
5166 {
5167 gfc_symbol *sym, *old_sym;
5168 gfc_symtree *st;
5169 int n;
5170 gfc_ref *ref;
5171 gfc_actual_arglist *arg, *tail = NULL;
5172 bool retval = e->expr_type == EXPR_FUNCTION;
5173
5174 /* If the expression is the result of substitution in
5175 interface.c(gfc_extend_expr) because there is no way in
5176 which the host association can be wrong. */
5177 if (e->symtree == NULL
5178 || e->symtree->n.sym == NULL
5179 || e->user_operator)
5180 return retval;
5181
5182 old_sym = e->symtree->n.sym;
5183
5184 if (gfc_current_ns->parent
5185 && old_sym->ns != gfc_current_ns)
5186 {
5187 /* Use the 'USE' name so that renamed module symbols are
5188 correctly handled. */
5189 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5190
5191 if (sym && old_sym != sym
5192 && sym->ts.type == old_sym->ts.type
5193 && sym->attr.flavor == FL_PROCEDURE
5194 && sym->attr.contained)
5195 {
5196 /* Clear the shape, since it might not be valid. */
5197 if (e->shape != NULL)
5198 {
5199 for (n = 0; n < e->rank; n++)
5200 mpz_clear (e->shape[n]);
5201
5202 free (e->shape);
5203 }
5204
5205 /* Give the expression the right symtree! */
5206 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5207 gcc_assert (st != NULL);
5208
5209 if (old_sym->attr.flavor == FL_PROCEDURE
5210 || e->expr_type == EXPR_FUNCTION)
5211 {
5212 /* Original was function so point to the new symbol, since
5213 the actual argument list is already attached to the
5214 expression. */
5215 e->value.function.esym = NULL;
5216 e->symtree = st;
5217 }
5218 else
5219 {
5220 /* Original was variable so convert array references into
5221 an actual arglist. This does not need any checking now
5222 since resolve_function will take care of it. */
5223 e->value.function.actual = NULL;
5224 e->expr_type = EXPR_FUNCTION;
5225 e->symtree = st;
5226
5227 /* Ambiguity will not arise if the array reference is not
5228 the last reference. */
5229 for (ref = e->ref; ref; ref = ref->next)
5230 if (ref->type == REF_ARRAY && ref->next == NULL)
5231 break;
5232
5233 gcc_assert (ref->type == REF_ARRAY);
5234
5235 /* Grab the start expressions from the array ref and
5236 copy them into actual arguments. */
5237 for (n = 0; n < ref->u.ar.dimen; n++)
5238 {
5239 arg = gfc_get_actual_arglist ();
5240 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5241 if (e->value.function.actual == NULL)
5242 tail = e->value.function.actual = arg;
5243 else
5244 {
5245 tail->next = arg;
5246 tail = arg;
5247 }
5248 }
5249
5250 /* Dump the reference list and set the rank. */
5251 gfc_free_ref_list (e->ref);
5252 e->ref = NULL;
5253 e->rank = sym->as ? sym->as->rank : 0;
5254 }
5255
5256 gfc_resolve_expr (e);
5257 sym->refs++;
5258 }
5259 }
5260 /* This might have changed! */
5261 return e->expr_type == EXPR_FUNCTION;
5262 }
5263
5264
5265 static void
5266 gfc_resolve_character_operator (gfc_expr *e)
5267 {
5268 gfc_expr *op1 = e->value.op.op1;
5269 gfc_expr *op2 = e->value.op.op2;
5270 gfc_expr *e1 = NULL;
5271 gfc_expr *e2 = NULL;
5272
5273 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5274
5275 if (op1->ts.u.cl && op1->ts.u.cl->length)
5276 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5277 else if (op1->expr_type == EXPR_CONSTANT)
5278 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5279 op1->value.character.length);
5280
5281 if (op2->ts.u.cl && op2->ts.u.cl->length)
5282 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5283 else if (op2->expr_type == EXPR_CONSTANT)
5284 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5285 op2->value.character.length);
5286
5287 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5288
5289 if (!e1 || !e2)
5290 return;
5291
5292 e->ts.u.cl->length = gfc_add (e1, e2);
5293 e->ts.u.cl->length->ts.type = BT_INTEGER;
5294 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5295 gfc_simplify_expr (e->ts.u.cl->length, 0);
5296 gfc_resolve_expr (e->ts.u.cl->length);
5297
5298 return;
5299 }
5300
5301
5302 /* Ensure that an character expression has a charlen and, if possible, a
5303 length expression. */
5304
5305 static void
5306 fixup_charlen (gfc_expr *e)
5307 {
5308 /* The cases fall through so that changes in expression type and the need
5309 for multiple fixes are picked up. In all circumstances, a charlen should
5310 be available for the middle end to hang a backend_decl on. */
5311 switch (e->expr_type)
5312 {
5313 case EXPR_OP:
5314 gfc_resolve_character_operator (e);
5315
5316 case EXPR_ARRAY:
5317 if (e->expr_type == EXPR_ARRAY)
5318 gfc_resolve_character_array_constructor (e);
5319
5320 case EXPR_SUBSTRING:
5321 if (!e->ts.u.cl && e->ref)
5322 gfc_resolve_substring_charlen (e);
5323
5324 default:
5325 if (!e->ts.u.cl)
5326 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5327
5328 break;
5329 }
5330 }
5331
5332
5333 /* Update an actual argument to include the passed-object for type-bound
5334 procedures at the right position. */
5335
5336 static gfc_actual_arglist*
5337 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5338 const char *name)
5339 {
5340 gcc_assert (argpos > 0);
5341
5342 if (argpos == 1)
5343 {
5344 gfc_actual_arglist* result;
5345
5346 result = gfc_get_actual_arglist ();
5347 result->expr = po;
5348 result->next = lst;
5349 if (name)
5350 result->name = name;
5351
5352 return result;
5353 }
5354
5355 if (lst)
5356 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5357 else
5358 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5359 return lst;
5360 }
5361
5362
5363 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5364
5365 static gfc_expr*
5366 extract_compcall_passed_object (gfc_expr* e)
5367 {
5368 gfc_expr* po;
5369
5370 gcc_assert (e->expr_type == EXPR_COMPCALL);
5371
5372 if (e->value.compcall.base_object)
5373 po = gfc_copy_expr (e->value.compcall.base_object);
5374 else
5375 {
5376 po = gfc_get_expr ();
5377 po->expr_type = EXPR_VARIABLE;
5378 po->symtree = e->symtree;
5379 po->ref = gfc_copy_ref (e->ref);
5380 po->where = e->where;
5381 }
5382
5383 if (gfc_resolve_expr (po) == FAILURE)
5384 return NULL;
5385
5386 return po;
5387 }
5388
5389
5390 /* Update the arglist of an EXPR_COMPCALL expression to include the
5391 passed-object. */
5392
5393 static gfc_try
5394 update_compcall_arglist (gfc_expr* e)
5395 {
5396 gfc_expr* po;
5397 gfc_typebound_proc* tbp;
5398
5399 tbp = e->value.compcall.tbp;
5400
5401 if (tbp->error)
5402 return FAILURE;
5403
5404 po = extract_compcall_passed_object (e);
5405 if (!po)
5406 return FAILURE;
5407
5408 if (tbp->nopass || e->value.compcall.ignore_pass)
5409 {
5410 gfc_free_expr (po);
5411 return SUCCESS;
5412 }
5413
5414 gcc_assert (tbp->pass_arg_num > 0);
5415 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5416 tbp->pass_arg_num,
5417 tbp->pass_arg);
5418
5419 return SUCCESS;
5420 }
5421
5422
5423 /* Extract the passed object from a PPC call (a copy of it). */
5424
5425 static gfc_expr*
5426 extract_ppc_passed_object (gfc_expr *e)
5427 {
5428 gfc_expr *po;
5429 gfc_ref **ref;
5430
5431 po = gfc_get_expr ();
5432 po->expr_type = EXPR_VARIABLE;
5433 po->symtree = e->symtree;
5434 po->ref = gfc_copy_ref (e->ref);
5435 po->where = e->where;
5436
5437 /* Remove PPC reference. */
5438 ref = &po->ref;
5439 while ((*ref)->next)
5440 ref = &(*ref)->next;
5441 gfc_free_ref_list (*ref);
5442 *ref = NULL;
5443
5444 if (gfc_resolve_expr (po) == FAILURE)
5445 return NULL;
5446
5447 return po;
5448 }
5449
5450
5451 /* Update the actual arglist of a procedure pointer component to include the
5452 passed-object. */
5453
5454 static gfc_try
5455 update_ppc_arglist (gfc_expr* e)
5456 {
5457 gfc_expr* po;
5458 gfc_component *ppc;
5459 gfc_typebound_proc* tb;
5460
5461 if (!gfc_is_proc_ptr_comp (e, &ppc))
5462 return FAILURE;
5463
5464 tb = ppc->tb;
5465
5466 if (tb->error)
5467 return FAILURE;
5468 else if (tb->nopass)
5469 return SUCCESS;
5470
5471 po = extract_ppc_passed_object (e);
5472 if (!po)
5473 return FAILURE;
5474
5475 /* F08:R739. */
5476 if (po->rank > 0)
5477 {
5478 gfc_error ("Passed-object at %L must be scalar", &e->where);
5479 return FAILURE;
5480 }
5481
5482 /* F08:C611. */
5483 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5484 {
5485 gfc_error ("Base object for procedure-pointer component call at %L is of"
5486 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5487 return FAILURE;
5488 }
5489
5490 gcc_assert (tb->pass_arg_num > 0);
5491 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5492 tb->pass_arg_num,
5493 tb->pass_arg);
5494
5495 return SUCCESS;
5496 }
5497
5498
5499 /* Check that the object a TBP is called on is valid, i.e. it must not be
5500 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5501
5502 static gfc_try
5503 check_typebound_baseobject (gfc_expr* e)
5504 {
5505 gfc_expr* base;
5506 gfc_try return_value = FAILURE;
5507
5508 base = extract_compcall_passed_object (e);
5509 if (!base)
5510 return FAILURE;
5511
5512 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5513
5514 /* F08:C611. */
5515 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5516 {
5517 gfc_error ("Base object for type-bound procedure call at %L is of"
5518 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5519 goto cleanup;
5520 }
5521
5522 /* F08:C1230. If the procedure called is NOPASS,
5523 the base object must be scalar. */
5524 if (e->value.compcall.tbp->nopass && base->rank > 0)
5525 {
5526 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5527 " be scalar", &e->where);
5528 goto cleanup;
5529 }
5530
5531 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5532 if (base->rank > 0)
5533 {
5534 gfc_error ("Non-scalar base object at %L currently not implemented",
5535 &e->where);
5536 goto cleanup;
5537 }
5538
5539 return_value = SUCCESS;
5540
5541 cleanup:
5542 gfc_free_expr (base);
5543 return return_value;
5544 }
5545
5546
5547 /* Resolve a call to a type-bound procedure, either function or subroutine,
5548 statically from the data in an EXPR_COMPCALL expression. The adapted
5549 arglist and the target-procedure symtree are returned. */
5550
5551 static gfc_try
5552 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5553 gfc_actual_arglist** actual)
5554 {
5555 gcc_assert (e->expr_type == EXPR_COMPCALL);
5556 gcc_assert (!e->value.compcall.tbp->is_generic);
5557
5558 /* Update the actual arglist for PASS. */
5559 if (update_compcall_arglist (e) == FAILURE)
5560 return FAILURE;
5561
5562 *actual = e->value.compcall.actual;
5563 *target = e->value.compcall.tbp->u.specific;
5564
5565 gfc_free_ref_list (e->ref);
5566 e->ref = NULL;
5567 e->value.compcall.actual = NULL;
5568
5569 return SUCCESS;
5570 }
5571
5572
5573 /* Get the ultimate declared type from an expression. In addition,
5574 return the last class/derived type reference and the copy of the
5575 reference list. */
5576 static gfc_symbol*
5577 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5578 gfc_expr *e)
5579 {
5580 gfc_symbol *declared;
5581 gfc_ref *ref;
5582
5583 declared = NULL;
5584 if (class_ref)
5585 *class_ref = NULL;
5586 if (new_ref)
5587 *new_ref = gfc_copy_ref (e->ref);
5588
5589 for (ref = e->ref; ref; ref = ref->next)
5590 {
5591 if (ref->type != REF_COMPONENT)
5592 continue;
5593
5594 if (ref->u.c.component->ts.type == BT_CLASS
5595 || ref->u.c.component->ts.type == BT_DERIVED)
5596 {
5597 declared = ref->u.c.component->ts.u.derived;
5598 if (class_ref)
5599 *class_ref = ref;
5600 }
5601 }
5602
5603 if (declared == NULL)
5604 declared = e->symtree->n.sym->ts.u.derived;
5605
5606 return declared;
5607 }
5608
5609
5610 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5611 which of the specific bindings (if any) matches the arglist and transform
5612 the expression into a call of that binding. */
5613
5614 static gfc_try
5615 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5616 {
5617 gfc_typebound_proc* genproc;
5618 const char* genname;
5619 gfc_symtree *st;
5620 gfc_symbol *derived;
5621
5622 gcc_assert (e->expr_type == EXPR_COMPCALL);
5623 genname = e->value.compcall.name;
5624 genproc = e->value.compcall.tbp;
5625
5626 if (!genproc->is_generic)
5627 return SUCCESS;
5628
5629 /* Try the bindings on this type and in the inheritance hierarchy. */
5630 for (; genproc; genproc = genproc->overridden)
5631 {
5632 gfc_tbp_generic* g;
5633
5634 gcc_assert (genproc->is_generic);
5635 for (g = genproc->u.generic; g; g = g->next)
5636 {
5637 gfc_symbol* target;
5638 gfc_actual_arglist* args;
5639 bool matches;
5640
5641 gcc_assert (g->specific);
5642
5643 if (g->specific->error)
5644 continue;
5645
5646 target = g->specific->u.specific->n.sym;
5647
5648 /* Get the right arglist by handling PASS/NOPASS. */
5649 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5650 if (!g->specific->nopass)
5651 {
5652 gfc_expr* po;
5653 po = extract_compcall_passed_object (e);
5654 if (!po)
5655 return FAILURE;
5656
5657 gcc_assert (g->specific->pass_arg_num > 0);
5658 gcc_assert (!g->specific->error);
5659 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5660 g->specific->pass_arg);
5661 }
5662 resolve_actual_arglist (args, target->attr.proc,
5663 is_external_proc (target) && !target->formal);
5664
5665 /* Check if this arglist matches the formal. */
5666 matches = gfc_arglist_matches_symbol (&args, target);
5667
5668 /* Clean up and break out of the loop if we've found it. */
5669 gfc_free_actual_arglist (args);
5670 if (matches)
5671 {
5672 e->value.compcall.tbp = g->specific;
5673 genname = g->specific_st->name;
5674 /* Pass along the name for CLASS methods, where the vtab
5675 procedure pointer component has to be referenced. */
5676 if (name)
5677 *name = genname;
5678 goto success;
5679 }
5680 }
5681 }
5682
5683 /* Nothing matching found! */
5684 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5685 " '%s' at %L", genname, &e->where);
5686 return FAILURE;
5687
5688 success:
5689 /* Make sure that we have the right specific instance for the name. */
5690 derived = get_declared_from_expr (NULL, NULL, e);
5691
5692 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5693 if (st)
5694 e->value.compcall.tbp = st->n.tb;
5695
5696 return SUCCESS;
5697 }
5698
5699
5700 /* Resolve a call to a type-bound subroutine. */
5701
5702 static gfc_try
5703 resolve_typebound_call (gfc_code* c, const char **name)
5704 {
5705 gfc_actual_arglist* newactual;
5706 gfc_symtree* target;
5707
5708 /* Check that's really a SUBROUTINE. */
5709 if (!c->expr1->value.compcall.tbp->subroutine)
5710 {
5711 gfc_error ("'%s' at %L should be a SUBROUTINE",
5712 c->expr1->value.compcall.name, &c->loc);
5713 return FAILURE;
5714 }
5715
5716 if (check_typebound_baseobject (c->expr1) == FAILURE)
5717 return FAILURE;
5718
5719 /* Pass along the name for CLASS methods, where the vtab
5720 procedure pointer component has to be referenced. */
5721 if (name)
5722 *name = c->expr1->value.compcall.name;
5723
5724 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5725 return FAILURE;
5726
5727 /* Transform into an ordinary EXEC_CALL for now. */
5728
5729 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5730 return FAILURE;
5731
5732 c->ext.actual = newactual;
5733 c->symtree = target;
5734 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5735
5736 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5737
5738 gfc_free_expr (c->expr1);
5739 c->expr1 = gfc_get_expr ();
5740 c->expr1->expr_type = EXPR_FUNCTION;
5741 c->expr1->symtree = target;
5742 c->expr1->where = c->loc;
5743
5744 return resolve_call (c);
5745 }
5746
5747
5748 /* Resolve a component-call expression. */
5749 static gfc_try
5750 resolve_compcall (gfc_expr* e, const char **name)
5751 {
5752 gfc_actual_arglist* newactual;
5753 gfc_symtree* target;
5754
5755 /* Check that's really a FUNCTION. */
5756 if (!e->value.compcall.tbp->function)
5757 {
5758 gfc_error ("'%s' at %L should be a FUNCTION",
5759 e->value.compcall.name, &e->where);
5760 return FAILURE;
5761 }
5762
5763 /* These must not be assign-calls! */
5764 gcc_assert (!e->value.compcall.assign);
5765
5766 if (check_typebound_baseobject (e) == FAILURE)
5767 return FAILURE;
5768
5769 /* Pass along the name for CLASS methods, where the vtab
5770 procedure pointer component has to be referenced. */
5771 if (name)
5772 *name = e->value.compcall.name;
5773
5774 if (resolve_typebound_generic_call (e, name) == FAILURE)
5775 return FAILURE;
5776 gcc_assert (!e->value.compcall.tbp->is_generic);
5777
5778 /* Take the rank from the function's symbol. */
5779 if (e->value.compcall.tbp->u.specific->n.sym->as)
5780 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5781
5782 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5783 arglist to the TBP's binding target. */
5784
5785 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5786 return FAILURE;
5787
5788 e->value.function.actual = newactual;
5789 e->value.function.name = NULL;
5790 e->value.function.esym = target->n.sym;
5791 e->value.function.isym = NULL;
5792 e->symtree = target;
5793 e->ts = target->n.sym->ts;
5794 e->expr_type = EXPR_FUNCTION;
5795
5796 /* Resolution is not necessary if this is a class subroutine; this
5797 function only has to identify the specific proc. Resolution of
5798 the call will be done next in resolve_typebound_call. */
5799 return gfc_resolve_expr (e);
5800 }
5801
5802
5803
5804 /* Resolve a typebound function, or 'method'. First separate all
5805 the non-CLASS references by calling resolve_compcall directly. */
5806
5807 static gfc_try
5808 resolve_typebound_function (gfc_expr* e)
5809 {
5810 gfc_symbol *declared;
5811 gfc_component *c;
5812 gfc_ref *new_ref;
5813 gfc_ref *class_ref;
5814 gfc_symtree *st;
5815 const char *name;
5816 gfc_typespec ts;
5817 gfc_expr *expr;
5818
5819 st = e->symtree;
5820
5821 /* Deal with typebound operators for CLASS objects. */
5822 expr = e->value.compcall.base_object;
5823 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5824 {
5825 /* Since the typebound operators are generic, we have to ensure
5826 that any delays in resolution are corrected and that the vtab
5827 is present. */
5828 ts = expr->ts;
5829 declared = ts.u.derived;
5830 c = gfc_find_component (declared, "_vptr", true, true);
5831 if (c->ts.u.derived == NULL)
5832 c->ts.u.derived = gfc_find_derived_vtab (declared);
5833
5834 if (resolve_compcall (e, &name) == FAILURE)
5835 return FAILURE;
5836
5837 /* Use the generic name if it is there. */
5838 name = name ? name : e->value.function.esym->name;
5839 e->symtree = expr->symtree;
5840 e->ref = gfc_copy_ref (expr->ref);
5841 gfc_add_vptr_component (e);
5842 gfc_add_component_ref (e, name);
5843 e->value.function.esym = NULL;
5844 return SUCCESS;
5845 }
5846
5847 if (st == NULL)
5848 return resolve_compcall (e, NULL);
5849
5850 if (resolve_ref (e) == FAILURE)
5851 return FAILURE;
5852
5853 /* Get the CLASS declared type. */
5854 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5855
5856 /* Weed out cases of the ultimate component being a derived type. */
5857 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5858 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5859 {
5860 gfc_free_ref_list (new_ref);
5861 return resolve_compcall (e, NULL);
5862 }
5863
5864 c = gfc_find_component (declared, "_data", true, true);
5865 declared = c->ts.u.derived;
5866
5867 /* Treat the call as if it is a typebound procedure, in order to roll
5868 out the correct name for the specific function. */
5869 if (resolve_compcall (e, &name) == FAILURE)
5870 return FAILURE;
5871 ts = e->ts;
5872
5873 /* Then convert the expression to a procedure pointer component call. */
5874 e->value.function.esym = NULL;
5875 e->symtree = st;
5876
5877 if (new_ref)
5878 e->ref = new_ref;
5879
5880 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5881 gfc_add_vptr_component (e);
5882 gfc_add_component_ref (e, name);
5883
5884 /* Recover the typespec for the expression. This is really only
5885 necessary for generic procedures, where the additional call
5886 to gfc_add_component_ref seems to throw the collection of the
5887 correct typespec. */
5888 e->ts = ts;
5889 return SUCCESS;
5890 }
5891
5892 /* Resolve a typebound subroutine, or 'method'. First separate all
5893 the non-CLASS references by calling resolve_typebound_call
5894 directly. */
5895
5896 static gfc_try
5897 resolve_typebound_subroutine (gfc_code *code)
5898 {
5899 gfc_symbol *declared;
5900 gfc_component *c;
5901 gfc_ref *new_ref;
5902 gfc_ref *class_ref;
5903 gfc_symtree *st;
5904 const char *name;
5905 gfc_typespec ts;
5906 gfc_expr *expr;
5907
5908 st = code->expr1->symtree;
5909
5910 /* Deal with typebound operators for CLASS objects. */
5911 expr = code->expr1->value.compcall.base_object;
5912 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5913 {
5914 /* Since the typebound operators are generic, we have to ensure
5915 that any delays in resolution are corrected and that the vtab
5916 is present. */
5917 declared = expr->ts.u.derived;
5918 c = gfc_find_component (declared, "_vptr", true, true);
5919 if (c->ts.u.derived == NULL)
5920 c->ts.u.derived = gfc_find_derived_vtab (declared);
5921
5922 if (resolve_typebound_call (code, &name) == FAILURE)
5923 return FAILURE;
5924
5925 /* Use the generic name if it is there. */
5926 name = name ? name : code->expr1->value.function.esym->name;
5927 code->expr1->symtree = expr->symtree;
5928 code->expr1->ref = gfc_copy_ref (expr->ref);
5929 gfc_add_vptr_component (code->expr1);
5930 gfc_add_component_ref (code->expr1, name);
5931 code->expr1->value.function.esym = NULL;
5932 return SUCCESS;
5933 }
5934
5935 if (st == NULL)
5936 return resolve_typebound_call (code, NULL);
5937
5938 if (resolve_ref (code->expr1) == FAILURE)
5939 return FAILURE;
5940
5941 /* Get the CLASS declared type. */
5942 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5943
5944 /* Weed out cases of the ultimate component being a derived type. */
5945 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5946 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5947 {
5948 gfc_free_ref_list (new_ref);
5949 return resolve_typebound_call (code, NULL);
5950 }
5951
5952 if (resolve_typebound_call (code, &name) == FAILURE)
5953 return FAILURE;
5954 ts = code->expr1->ts;
5955
5956 /* Then convert the expression to a procedure pointer component call. */
5957 code->expr1->value.function.esym = NULL;
5958 code->expr1->symtree = st;
5959
5960 if (new_ref)
5961 code->expr1->ref = new_ref;
5962
5963 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5964 gfc_add_vptr_component (code->expr1);
5965 gfc_add_component_ref (code->expr1, name);
5966
5967 /* Recover the typespec for the expression. This is really only
5968 necessary for generic procedures, where the additional call
5969 to gfc_add_component_ref seems to throw the collection of the
5970 correct typespec. */
5971 code->expr1->ts = ts;
5972 return SUCCESS;
5973 }
5974
5975
5976 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5977
5978 static gfc_try
5979 resolve_ppc_call (gfc_code* c)
5980 {
5981 gfc_component *comp;
5982 bool b;
5983
5984 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5985 gcc_assert (b);
5986
5987 c->resolved_sym = c->expr1->symtree->n.sym;
5988 c->expr1->expr_type = EXPR_VARIABLE;
5989
5990 if (!comp->attr.subroutine)
5991 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5992
5993 if (resolve_ref (c->expr1) == FAILURE)
5994 return FAILURE;
5995
5996 if (update_ppc_arglist (c->expr1) == FAILURE)
5997 return FAILURE;
5998
5999 c->ext.actual = c->expr1->value.compcall.actual;
6000
6001 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6002 comp->formal == NULL) == FAILURE)
6003 return FAILURE;
6004
6005 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6006
6007 return SUCCESS;
6008 }
6009
6010
6011 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6012
6013 static gfc_try
6014 resolve_expr_ppc (gfc_expr* e)
6015 {
6016 gfc_component *comp;
6017 bool b;
6018
6019 b = gfc_is_proc_ptr_comp (e, &comp);
6020 gcc_assert (b);
6021
6022 /* Convert to EXPR_FUNCTION. */
6023 e->expr_type = EXPR_FUNCTION;
6024 e->value.function.isym = NULL;
6025 e->value.function.actual = e->value.compcall.actual;
6026 e->ts = comp->ts;
6027 if (comp->as != NULL)
6028 e->rank = comp->as->rank;
6029
6030 if (!comp->attr.function)
6031 gfc_add_function (&comp->attr, comp->name, &e->where);
6032
6033 if (resolve_ref (e) == FAILURE)
6034 return FAILURE;
6035
6036 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6037 comp->formal == NULL) == FAILURE)
6038 return FAILURE;
6039
6040 if (update_ppc_arglist (e) == FAILURE)
6041 return FAILURE;
6042
6043 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6044
6045 return SUCCESS;
6046 }
6047
6048
6049 static bool
6050 gfc_is_expandable_expr (gfc_expr *e)
6051 {
6052 gfc_constructor *con;
6053
6054 if (e->expr_type == EXPR_ARRAY)
6055 {
6056 /* Traverse the constructor looking for variables that are flavor
6057 parameter. Parameters must be expanded since they are fully used at
6058 compile time. */
6059 con = gfc_constructor_first (e->value.constructor);
6060 for (; con; con = gfc_constructor_next (con))
6061 {
6062 if (con->expr->expr_type == EXPR_VARIABLE
6063 && con->expr->symtree
6064 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6065 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6066 return true;
6067 if (con->expr->expr_type == EXPR_ARRAY
6068 && gfc_is_expandable_expr (con->expr))
6069 return true;
6070 }
6071 }
6072
6073 return false;
6074 }
6075
6076 /* Resolve an expression. That is, make sure that types of operands agree
6077 with their operators, intrinsic operators are converted to function calls
6078 for overloaded types and unresolved function references are resolved. */
6079
6080 gfc_try
6081 gfc_resolve_expr (gfc_expr *e)
6082 {
6083 gfc_try t;
6084 bool inquiry_save;
6085
6086 if (e == NULL)
6087 return SUCCESS;
6088
6089 /* inquiry_argument only applies to variables. */
6090 inquiry_save = inquiry_argument;
6091 if (e->expr_type != EXPR_VARIABLE)
6092 inquiry_argument = false;
6093
6094 switch (e->expr_type)
6095 {
6096 case EXPR_OP:
6097 t = resolve_operator (e);
6098 break;
6099
6100 case EXPR_FUNCTION:
6101 case EXPR_VARIABLE:
6102
6103 if (check_host_association (e))
6104 t = resolve_function (e);
6105 else
6106 {
6107 t = resolve_variable (e);
6108 if (t == SUCCESS)
6109 expression_rank (e);
6110 }
6111
6112 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6113 && e->ref->type != REF_SUBSTRING)
6114 gfc_resolve_substring_charlen (e);
6115
6116 break;
6117
6118 case EXPR_COMPCALL:
6119 t = resolve_typebound_function (e);
6120 break;
6121
6122 case EXPR_SUBSTRING:
6123 t = resolve_ref (e);
6124 break;
6125
6126 case EXPR_CONSTANT:
6127 case EXPR_NULL:
6128 t = SUCCESS;
6129 break;
6130
6131 case EXPR_PPC:
6132 t = resolve_expr_ppc (e);
6133 break;
6134
6135 case EXPR_ARRAY:
6136 t = FAILURE;
6137 if (resolve_ref (e) == FAILURE)
6138 break;
6139
6140 t = gfc_resolve_array_constructor (e);
6141 /* Also try to expand a constructor. */
6142 if (t == SUCCESS)
6143 {
6144 expression_rank (e);
6145 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6146 gfc_expand_constructor (e, false);
6147 }
6148
6149 /* This provides the opportunity for the length of constructors with
6150 character valued function elements to propagate the string length
6151 to the expression. */
6152 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6153 {
6154 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6155 here rather then add a duplicate test for it above. */
6156 gfc_expand_constructor (e, false);
6157 t = gfc_resolve_character_array_constructor (e);
6158 }
6159
6160 break;
6161
6162 case EXPR_STRUCTURE:
6163 t = resolve_ref (e);
6164 if (t == FAILURE)
6165 break;
6166
6167 t = resolve_structure_cons (e, 0);
6168 if (t == FAILURE)
6169 break;
6170
6171 t = gfc_simplify_expr (e, 0);
6172 break;
6173
6174 default:
6175 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6176 }
6177
6178 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6179 fixup_charlen (e);
6180
6181 inquiry_argument = inquiry_save;
6182
6183 return t;
6184 }
6185
6186
6187 /* Resolve an expression from an iterator. They must be scalar and have
6188 INTEGER or (optionally) REAL type. */
6189
6190 static gfc_try
6191 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6192 const char *name_msgid)
6193 {
6194 if (gfc_resolve_expr (expr) == FAILURE)
6195 return FAILURE;
6196
6197 if (expr->rank != 0)
6198 {
6199 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6200 return FAILURE;
6201 }
6202
6203 if (expr->ts.type != BT_INTEGER)
6204 {
6205 if (expr->ts.type == BT_REAL)
6206 {
6207 if (real_ok)
6208 return gfc_notify_std (GFC_STD_F95_DEL,
6209 "Deleted feature: %s at %L must be integer",
6210 _(name_msgid), &expr->where);
6211 else
6212 {
6213 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6214 &expr->where);
6215 return FAILURE;
6216 }
6217 }
6218 else
6219 {
6220 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6221 return FAILURE;
6222 }
6223 }
6224 return SUCCESS;
6225 }
6226
6227
6228 /* Resolve the expressions in an iterator structure. If REAL_OK is
6229 false allow only INTEGER type iterators, otherwise allow REAL types. */
6230
6231 gfc_try
6232 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6233 {
6234 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6235 == FAILURE)
6236 return FAILURE;
6237
6238 if (gfc_check_vardef_context (iter->var, false, false, _("iterator variable"))
6239 == FAILURE)
6240 return FAILURE;
6241
6242 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6243 "Start expression in DO loop") == FAILURE)
6244 return FAILURE;
6245
6246 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6247 "End expression in DO loop") == FAILURE)
6248 return FAILURE;
6249
6250 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6251 "Step expression in DO loop") == FAILURE)
6252 return FAILURE;
6253
6254 if (iter->step->expr_type == EXPR_CONSTANT)
6255 {
6256 if ((iter->step->ts.type == BT_INTEGER
6257 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6258 || (iter->step->ts.type == BT_REAL
6259 && mpfr_sgn (iter->step->value.real) == 0))
6260 {
6261 gfc_error ("Step expression in DO loop at %L cannot be zero",
6262 &iter->step->where);
6263 return FAILURE;
6264 }
6265 }
6266
6267 /* Convert start, end, and step to the same type as var. */
6268 if (iter->start->ts.kind != iter->var->ts.kind
6269 || iter->start->ts.type != iter->var->ts.type)
6270 gfc_convert_type (iter->start, &iter->var->ts, 2);
6271
6272 if (iter->end->ts.kind != iter->var->ts.kind
6273 || iter->end->ts.type != iter->var->ts.type)
6274 gfc_convert_type (iter->end, &iter->var->ts, 2);
6275
6276 if (iter->step->ts.kind != iter->var->ts.kind
6277 || iter->step->ts.type != iter->var->ts.type)
6278 gfc_convert_type (iter->step, &iter->var->ts, 2);
6279
6280 if (iter->start->expr_type == EXPR_CONSTANT
6281 && iter->end->expr_type == EXPR_CONSTANT
6282 && iter->step->expr_type == EXPR_CONSTANT)
6283 {
6284 int sgn, cmp;
6285 if (iter->start->ts.type == BT_INTEGER)
6286 {
6287 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6288 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6289 }
6290 else
6291 {
6292 sgn = mpfr_sgn (iter->step->value.real);
6293 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6294 }
6295 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6296 gfc_warning ("DO loop at %L will be executed zero times",
6297 &iter->step->where);
6298 }
6299
6300 return SUCCESS;
6301 }
6302
6303
6304 /* Traversal function for find_forall_index. f == 2 signals that
6305 that variable itself is not to be checked - only the references. */
6306
6307 static bool
6308 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6309 {
6310 if (expr->expr_type != EXPR_VARIABLE)
6311 return false;
6312
6313 /* A scalar assignment */
6314 if (!expr->ref || *f == 1)
6315 {
6316 if (expr->symtree->n.sym == sym)
6317 return true;
6318 else
6319 return false;
6320 }
6321
6322 if (*f == 2)
6323 *f = 1;
6324 return false;
6325 }
6326
6327
6328 /* Check whether the FORALL index appears in the expression or not.
6329 Returns SUCCESS if SYM is found in EXPR. */
6330
6331 gfc_try
6332 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6333 {
6334 if (gfc_traverse_expr (expr, sym, forall_index, f))
6335 return SUCCESS;
6336 else
6337 return FAILURE;
6338 }
6339
6340
6341 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6342 to be a scalar INTEGER variable. The subscripts and stride are scalar
6343 INTEGERs, and if stride is a constant it must be nonzero.
6344 Furthermore "A subscript or stride in a forall-triplet-spec shall
6345 not contain a reference to any index-name in the
6346 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6347
6348 static void
6349 resolve_forall_iterators (gfc_forall_iterator *it)
6350 {
6351 gfc_forall_iterator *iter, *iter2;
6352
6353 for (iter = it; iter; iter = iter->next)
6354 {
6355 if (gfc_resolve_expr (iter->var) == SUCCESS
6356 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6357 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6358 &iter->var->where);
6359
6360 if (gfc_resolve_expr (iter->start) == SUCCESS
6361 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6362 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6363 &iter->start->where);
6364 if (iter->var->ts.kind != iter->start->ts.kind)
6365 gfc_convert_type (iter->start, &iter->var->ts, 2);
6366
6367 if (gfc_resolve_expr (iter->end) == SUCCESS
6368 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6369 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6370 &iter->end->where);
6371 if (iter->var->ts.kind != iter->end->ts.kind)
6372 gfc_convert_type (iter->end, &iter->var->ts, 2);
6373
6374 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6375 {
6376 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6377 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6378 &iter->stride->where, "INTEGER");
6379
6380 if (iter->stride->expr_type == EXPR_CONSTANT
6381 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6382 gfc_error ("FORALL stride expression at %L cannot be zero",
6383 &iter->stride->where);
6384 }
6385 if (iter->var->ts.kind != iter->stride->ts.kind)
6386 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6387 }
6388
6389 for (iter = it; iter; iter = iter->next)
6390 for (iter2 = iter; iter2; iter2 = iter2->next)
6391 {
6392 if (find_forall_index (iter2->start,
6393 iter->var->symtree->n.sym, 0) == SUCCESS
6394 || find_forall_index (iter2->end,
6395 iter->var->symtree->n.sym, 0) == SUCCESS
6396 || find_forall_index (iter2->stride,
6397 iter->var->symtree->n.sym, 0) == SUCCESS)
6398 gfc_error ("FORALL index '%s' may not appear in triplet "
6399 "specification at %L", iter->var->symtree->name,
6400 &iter2->start->where);
6401 }
6402 }
6403
6404
6405 /* Given a pointer to a symbol that is a derived type, see if it's
6406 inaccessible, i.e. if it's defined in another module and the components are
6407 PRIVATE. The search is recursive if necessary. Returns zero if no
6408 inaccessible components are found, nonzero otherwise. */
6409
6410 static int
6411 derived_inaccessible (gfc_symbol *sym)
6412 {
6413 gfc_component *c;
6414
6415 if (sym->attr.use_assoc && sym->attr.private_comp)
6416 return 1;
6417
6418 for (c = sym->components; c; c = c->next)
6419 {
6420 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6421 return 1;
6422 }
6423
6424 return 0;
6425 }
6426
6427
6428 /* Resolve the argument of a deallocate expression. The expression must be
6429 a pointer or a full array. */
6430
6431 static gfc_try
6432 resolve_deallocate_expr (gfc_expr *e)
6433 {
6434 symbol_attribute attr;
6435 int allocatable, pointer;
6436 gfc_ref *ref;
6437 gfc_symbol *sym;
6438 gfc_component *c;
6439
6440 if (gfc_resolve_expr (e) == FAILURE)
6441 return FAILURE;
6442
6443 if (e->expr_type != EXPR_VARIABLE)
6444 goto bad;
6445
6446 sym = e->symtree->n.sym;
6447
6448 if (sym->ts.type == BT_CLASS)
6449 {
6450 allocatable = CLASS_DATA (sym)->attr.allocatable;
6451 pointer = CLASS_DATA (sym)->attr.class_pointer;
6452 }
6453 else
6454 {
6455 allocatable = sym->attr.allocatable;
6456 pointer = sym->attr.pointer;
6457 }
6458 for (ref = e->ref; ref; ref = ref->next)
6459 {
6460 switch (ref->type)
6461 {
6462 case REF_ARRAY:
6463 if (ref->u.ar.type != AR_FULL
6464 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6465 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6466 allocatable = 0;
6467 break;
6468
6469 case REF_COMPONENT:
6470 c = ref->u.c.component;
6471 if (c->ts.type == BT_CLASS)
6472 {
6473 allocatable = CLASS_DATA (c)->attr.allocatable;
6474 pointer = CLASS_DATA (c)->attr.class_pointer;
6475 }
6476 else
6477 {
6478 allocatable = c->attr.allocatable;
6479 pointer = c->attr.pointer;
6480 }
6481 break;
6482
6483 case REF_SUBSTRING:
6484 allocatable = 0;
6485 break;
6486 }
6487 }
6488
6489 attr = gfc_expr_attr (e);
6490
6491 if (allocatable == 0 && attr.pointer == 0)
6492 {
6493 bad:
6494 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6495 &e->where);
6496 return FAILURE;
6497 }
6498
6499 /* F2008, C644. */
6500 if (gfc_is_coindexed (e))
6501 {
6502 gfc_error ("Coindexed allocatable object at %L", &e->where);
6503 return FAILURE;
6504 }
6505
6506 if (pointer
6507 && gfc_check_vardef_context (e, true, true, _("DEALLOCATE object"))
6508 == FAILURE)
6509 return FAILURE;
6510 if (gfc_check_vardef_context (e, false, true, _("DEALLOCATE object"))
6511 == FAILURE)
6512 return FAILURE;
6513
6514 return SUCCESS;
6515 }
6516
6517
6518 /* Returns true if the expression e contains a reference to the symbol sym. */
6519 static bool
6520 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6521 {
6522 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6523 return true;
6524
6525 return false;
6526 }
6527
6528 bool
6529 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6530 {
6531 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6532 }
6533
6534
6535 /* Given the expression node e for an allocatable/pointer of derived type to be
6536 allocated, get the expression node to be initialized afterwards (needed for
6537 derived types with default initializers, and derived types with allocatable
6538 components that need nullification.) */
6539
6540 gfc_expr *
6541 gfc_expr_to_initialize (gfc_expr *e)
6542 {
6543 gfc_expr *result;
6544 gfc_ref *ref;
6545 int i;
6546
6547 result = gfc_copy_expr (e);
6548
6549 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6550 for (ref = result->ref; ref; ref = ref->next)
6551 if (ref->type == REF_ARRAY && ref->next == NULL)
6552 {
6553 ref->u.ar.type = AR_FULL;
6554
6555 for (i = 0; i < ref->u.ar.dimen; i++)
6556 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6557
6558 result->rank = ref->u.ar.dimen;
6559 break;
6560 }
6561
6562 return result;
6563 }
6564
6565
6566 /* If the last ref of an expression is an array ref, return a copy of the
6567 expression with that one removed. Otherwise, a copy of the original
6568 expression. This is used for allocate-expressions and pointer assignment
6569 LHS, where there may be an array specification that needs to be stripped
6570 off when using gfc_check_vardef_context. */
6571
6572 static gfc_expr*
6573 remove_last_array_ref (gfc_expr* e)
6574 {
6575 gfc_expr* e2;
6576 gfc_ref** r;
6577
6578 e2 = gfc_copy_expr (e);
6579 for (r = &e2->ref; *r; r = &(*r)->next)
6580 if ((*r)->type == REF_ARRAY && !(*r)->next)
6581 {
6582 gfc_free_ref_list (*r);
6583 *r = NULL;
6584 break;
6585 }
6586
6587 return e2;
6588 }
6589
6590
6591 /* Used in resolve_allocate_expr to check that a allocation-object and
6592 a source-expr are conformable. This does not catch all possible
6593 cases; in particular a runtime checking is needed. */
6594
6595 static gfc_try
6596 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6597 {
6598 gfc_ref *tail;
6599 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6600
6601 /* First compare rank. */
6602 if (tail && e1->rank != tail->u.ar.as->rank)
6603 {
6604 gfc_error ("Source-expr at %L must be scalar or have the "
6605 "same rank as the allocate-object at %L",
6606 &e1->where, &e2->where);
6607 return FAILURE;
6608 }
6609
6610 if (e1->shape)
6611 {
6612 int i;
6613 mpz_t s;
6614
6615 mpz_init (s);
6616
6617 for (i = 0; i < e1->rank; i++)
6618 {
6619 if (tail->u.ar.end[i])
6620 {
6621 mpz_set (s, tail->u.ar.end[i]->value.integer);
6622 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6623 mpz_add_ui (s, s, 1);
6624 }
6625 else
6626 {
6627 mpz_set (s, tail->u.ar.start[i]->value.integer);
6628 }
6629
6630 if (mpz_cmp (e1->shape[i], s) != 0)
6631 {
6632 gfc_error ("Source-expr at %L and allocate-object at %L must "
6633 "have the same shape", &e1->where, &e2->where);
6634 mpz_clear (s);
6635 return FAILURE;
6636 }
6637 }
6638
6639 mpz_clear (s);
6640 }
6641
6642 return SUCCESS;
6643 }
6644
6645
6646 /* Resolve the expression in an ALLOCATE statement, doing the additional
6647 checks to see whether the expression is OK or not. The expression must
6648 have a trailing array reference that gives the size of the array. */
6649
6650 static gfc_try
6651 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6652 {
6653 int i, pointer, allocatable, dimension, is_abstract;
6654 int codimension;
6655 bool coindexed;
6656 symbol_attribute attr;
6657 gfc_ref *ref, *ref2;
6658 gfc_expr *e2;
6659 gfc_array_ref *ar;
6660 gfc_symbol *sym = NULL;
6661 gfc_alloc *a;
6662 gfc_component *c;
6663 gfc_try t;
6664
6665 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6666 checking of coarrays. */
6667 for (ref = e->ref; ref; ref = ref->next)
6668 if (ref->next == NULL)
6669 break;
6670
6671 if (ref && ref->type == REF_ARRAY)
6672 ref->u.ar.in_allocate = true;
6673
6674 if (gfc_resolve_expr (e) == FAILURE)
6675 goto failure;
6676
6677 /* Make sure the expression is allocatable or a pointer. If it is
6678 pointer, the next-to-last reference must be a pointer. */
6679
6680 ref2 = NULL;
6681 if (e->symtree)
6682 sym = e->symtree->n.sym;
6683
6684 /* Check whether ultimate component is abstract and CLASS. */
6685 is_abstract = 0;
6686
6687 if (e->expr_type != EXPR_VARIABLE)
6688 {
6689 allocatable = 0;
6690 attr = gfc_expr_attr (e);
6691 pointer = attr.pointer;
6692 dimension = attr.dimension;
6693 codimension = attr.codimension;
6694 }
6695 else
6696 {
6697 if (sym->ts.type == BT_CLASS)
6698 {
6699 allocatable = CLASS_DATA (sym)->attr.allocatable;
6700 pointer = CLASS_DATA (sym)->attr.class_pointer;
6701 dimension = CLASS_DATA (sym)->attr.dimension;
6702 codimension = CLASS_DATA (sym)->attr.codimension;
6703 is_abstract = CLASS_DATA (sym)->attr.abstract;
6704 }
6705 else
6706 {
6707 allocatable = sym->attr.allocatable;
6708 pointer = sym->attr.pointer;
6709 dimension = sym->attr.dimension;
6710 codimension = sym->attr.codimension;
6711 }
6712
6713 coindexed = false;
6714
6715 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6716 {
6717 switch (ref->type)
6718 {
6719 case REF_ARRAY:
6720 if (ref->u.ar.codimen > 0)
6721 {
6722 int n;
6723 for (n = ref->u.ar.dimen;
6724 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6725 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6726 {
6727 coindexed = true;
6728 break;
6729 }
6730 }
6731
6732 if (ref->next != NULL)
6733 pointer = 0;
6734 break;
6735
6736 case REF_COMPONENT:
6737 /* F2008, C644. */
6738 if (coindexed)
6739 {
6740 gfc_error ("Coindexed allocatable object at %L",
6741 &e->where);
6742 goto failure;
6743 }
6744
6745 c = ref->u.c.component;
6746 if (c->ts.type == BT_CLASS)
6747 {
6748 allocatable = CLASS_DATA (c)->attr.allocatable;
6749 pointer = CLASS_DATA (c)->attr.class_pointer;
6750 dimension = CLASS_DATA (c)->attr.dimension;
6751 codimension = CLASS_DATA (c)->attr.codimension;
6752 is_abstract = CLASS_DATA (c)->attr.abstract;
6753 }
6754 else
6755 {
6756 allocatable = c->attr.allocatable;
6757 pointer = c->attr.pointer;
6758 dimension = c->attr.dimension;
6759 codimension = c->attr.codimension;
6760 is_abstract = c->attr.abstract;
6761 }
6762 break;
6763
6764 case REF_SUBSTRING:
6765 allocatable = 0;
6766 pointer = 0;
6767 break;
6768 }
6769 }
6770 }
6771
6772 if (allocatable == 0 && pointer == 0)
6773 {
6774 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6775 &e->where);
6776 goto failure;
6777 }
6778
6779 /* Some checks for the SOURCE tag. */
6780 if (code->expr3)
6781 {
6782 /* Check F03:C631. */
6783 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6784 {
6785 gfc_error ("Type of entity at %L is type incompatible with "
6786 "source-expr at %L", &e->where, &code->expr3->where);
6787 goto failure;
6788 }
6789
6790 /* Check F03:C632 and restriction following Note 6.18. */
6791 if (code->expr3->rank > 0
6792 && conformable_arrays (code->expr3, e) == FAILURE)
6793 goto failure;
6794
6795 /* Check F03:C633. */
6796 if (code->expr3->ts.kind != e->ts.kind)
6797 {
6798 gfc_error ("The allocate-object at %L and the source-expr at %L "
6799 "shall have the same kind type parameter",
6800 &e->where, &code->expr3->where);
6801 goto failure;
6802 }
6803
6804 /* Check F2008, C642. */
6805 if (code->expr3->ts.type == BT_DERIVED
6806 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6807 || (code->expr3->ts.u.derived->from_intmod
6808 == INTMOD_ISO_FORTRAN_ENV
6809 && code->expr3->ts.u.derived->intmod_sym_id
6810 == ISOFORTRAN_LOCK_TYPE)))
6811 {
6812 gfc_error ("The source-expr at %L shall neither be of type "
6813 "LOCK_TYPE nor have a LOCK_TYPE component if "
6814 "allocate-object at %L is a coarray",
6815 &code->expr3->where, &e->where);
6816 goto failure;
6817 }
6818 }
6819
6820 /* Check F08:C629. */
6821 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6822 && !code->expr3)
6823 {
6824 gcc_assert (e->ts.type == BT_CLASS);
6825 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6826 "type-spec or source-expr", sym->name, &e->where);
6827 goto failure;
6828 }
6829
6830 /* In the variable definition context checks, gfc_expr_attr is used
6831 on the expression. This is fooled by the array specification
6832 present in e, thus we have to eliminate that one temporarily. */
6833 e2 = remove_last_array_ref (e);
6834 t = SUCCESS;
6835 if (t == SUCCESS && pointer)
6836 t = gfc_check_vardef_context (e2, true, true, _("ALLOCATE object"));
6837 if (t == SUCCESS)
6838 t = gfc_check_vardef_context (e2, false, true, _("ALLOCATE object"));
6839 gfc_free_expr (e2);
6840 if (t == FAILURE)
6841 goto failure;
6842
6843 if (!code->expr3)
6844 {
6845 /* Set up default initializer if needed. */
6846 gfc_typespec ts;
6847 gfc_expr *init_e;
6848
6849 if (code->ext.alloc.ts.type == BT_DERIVED)
6850 ts = code->ext.alloc.ts;
6851 else
6852 ts = e->ts;
6853
6854 if (ts.type == BT_CLASS)
6855 ts = ts.u.derived->components->ts;
6856
6857 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6858 {
6859 gfc_code *init_st = gfc_get_code ();
6860 init_st->loc = code->loc;
6861 init_st->op = EXEC_INIT_ASSIGN;
6862 init_st->expr1 = gfc_expr_to_initialize (e);
6863 init_st->expr2 = init_e;
6864 init_st->next = code->next;
6865 code->next = init_st;
6866 }
6867 }
6868 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6869 {
6870 /* Default initialization via MOLD (non-polymorphic). */
6871 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6872 gfc_resolve_expr (rhs);
6873 gfc_free_expr (code->expr3);
6874 code->expr3 = rhs;
6875 }
6876
6877 if (e->ts.type == BT_CLASS)
6878 {
6879 /* Make sure the vtab symbol is present when
6880 the module variables are generated. */
6881 gfc_typespec ts = e->ts;
6882 if (code->expr3)
6883 ts = code->expr3->ts;
6884 else if (code->ext.alloc.ts.type == BT_DERIVED)
6885 ts = code->ext.alloc.ts;
6886 gfc_find_derived_vtab (ts.u.derived);
6887 }
6888
6889 if (dimension == 0 && codimension == 0)
6890 goto success;
6891
6892 /* Make sure the last reference node is an array specifiction. */
6893
6894 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6895 || (dimension && ref2->u.ar.dimen == 0))
6896 {
6897 gfc_error ("Array specification required in ALLOCATE statement "
6898 "at %L", &e->where);
6899 goto failure;
6900 }
6901
6902 /* Make sure that the array section reference makes sense in the
6903 context of an ALLOCATE specification. */
6904
6905 ar = &ref2->u.ar;
6906
6907 if (codimension)
6908 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6909 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6910 {
6911 gfc_error ("Coarray specification required in ALLOCATE statement "
6912 "at %L", &e->where);
6913 goto failure;
6914 }
6915
6916 for (i = 0; i < ar->dimen; i++)
6917 {
6918 if (ref2->u.ar.type == AR_ELEMENT)
6919 goto check_symbols;
6920
6921 switch (ar->dimen_type[i])
6922 {
6923 case DIMEN_ELEMENT:
6924 break;
6925
6926 case DIMEN_RANGE:
6927 if (ar->start[i] != NULL
6928 && ar->end[i] != NULL
6929 && ar->stride[i] == NULL)
6930 break;
6931
6932 /* Fall Through... */
6933
6934 case DIMEN_UNKNOWN:
6935 case DIMEN_VECTOR:
6936 case DIMEN_STAR:
6937 case DIMEN_THIS_IMAGE:
6938 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6939 &e->where);
6940 goto failure;
6941 }
6942
6943 check_symbols:
6944 for (a = code->ext.alloc.list; a; a = a->next)
6945 {
6946 sym = a->expr->symtree->n.sym;
6947
6948 /* TODO - check derived type components. */
6949 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6950 continue;
6951
6952 if ((ar->start[i] != NULL
6953 && gfc_find_sym_in_expr (sym, ar->start[i]))
6954 || (ar->end[i] != NULL
6955 && gfc_find_sym_in_expr (sym, ar->end[i])))
6956 {
6957 gfc_error ("'%s' must not appear in the array specification at "
6958 "%L in the same ALLOCATE statement where it is "
6959 "itself allocated", sym->name, &ar->where);
6960 goto failure;
6961 }
6962 }
6963 }
6964
6965 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6966 {
6967 if (ar->dimen_type[i] == DIMEN_ELEMENT
6968 || ar->dimen_type[i] == DIMEN_RANGE)
6969 {
6970 if (i == (ar->dimen + ar->codimen - 1))
6971 {
6972 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6973 "statement at %L", &e->where);
6974 goto failure;
6975 }
6976 break;
6977 }
6978
6979 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6980 && ar->stride[i] == NULL)
6981 break;
6982
6983 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6984 &e->where);
6985 goto failure;
6986 }
6987
6988 success:
6989 return SUCCESS;
6990
6991 failure:
6992 return FAILURE;
6993 }
6994
6995 static void
6996 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6997 {
6998 gfc_expr *stat, *errmsg, *pe, *qe;
6999 gfc_alloc *a, *p, *q;
7000
7001 stat = code->expr1;
7002 errmsg = code->expr2;
7003
7004 /* Check the stat variable. */
7005 if (stat)
7006 {
7007 gfc_check_vardef_context (stat, false, false, _("STAT variable"));
7008
7009 if ((stat->ts.type != BT_INTEGER
7010 && !(stat->ref && (stat->ref->type == REF_ARRAY
7011 || stat->ref->type == REF_COMPONENT)))
7012 || stat->rank > 0)
7013 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7014 "variable", &stat->where);
7015
7016 for (p = code->ext.alloc.list; p; p = p->next)
7017 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7018 {
7019 gfc_ref *ref1, *ref2;
7020 bool found = true;
7021
7022 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7023 ref1 = ref1->next, ref2 = ref2->next)
7024 {
7025 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7026 continue;
7027 if (ref1->u.c.component->name != ref2->u.c.component->name)
7028 {
7029 found = false;
7030 break;
7031 }
7032 }
7033
7034 if (found)
7035 {
7036 gfc_error ("Stat-variable at %L shall not be %sd within "
7037 "the same %s statement", &stat->where, fcn, fcn);
7038 break;
7039 }
7040 }
7041 }
7042
7043 /* Check the errmsg variable. */
7044 if (errmsg)
7045 {
7046 if (!stat)
7047 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7048 &errmsg->where);
7049
7050 gfc_check_vardef_context (errmsg, false, false, _("ERRMSG variable"));
7051
7052 if ((errmsg->ts.type != BT_CHARACTER
7053 && !(errmsg->ref
7054 && (errmsg->ref->type == REF_ARRAY
7055 || errmsg->ref->type == REF_COMPONENT)))
7056 || errmsg->rank > 0 )
7057 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7058 "variable", &errmsg->where);
7059
7060 for (p = code->ext.alloc.list; p; p = p->next)
7061 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7062 {
7063 gfc_ref *ref1, *ref2;
7064 bool found = true;
7065
7066 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7067 ref1 = ref1->next, ref2 = ref2->next)
7068 {
7069 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7070 continue;
7071 if (ref1->u.c.component->name != ref2->u.c.component->name)
7072 {
7073 found = false;
7074 break;
7075 }
7076 }
7077
7078 if (found)
7079 {
7080 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7081 "the same %s statement", &errmsg->where, fcn, fcn);
7082 break;
7083 }
7084 }
7085 }
7086
7087 /* Check that an allocate-object appears only once in the statement.
7088 FIXME: Checking derived types is disabled. */
7089 for (p = code->ext.alloc.list; p; p = p->next)
7090 {
7091 pe = p->expr;
7092 for (q = p->next; q; q = q->next)
7093 {
7094 qe = q->expr;
7095 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7096 {
7097 /* This is a potential collision. */
7098 gfc_ref *pr = pe->ref;
7099 gfc_ref *qr = qe->ref;
7100
7101 /* Follow the references until
7102 a) They start to differ, in which case there is no error;
7103 you can deallocate a%b and a%c in a single statement
7104 b) Both of them stop, which is an error
7105 c) One of them stops, which is also an error. */
7106 while (1)
7107 {
7108 if (pr == NULL && qr == NULL)
7109 {
7110 gfc_error ("Allocate-object at %L also appears at %L",
7111 &pe->where, &qe->where);
7112 break;
7113 }
7114 else if (pr != NULL && qr == NULL)
7115 {
7116 gfc_error ("Allocate-object at %L is subobject of"
7117 " object at %L", &pe->where, &qe->where);
7118 break;
7119 }
7120 else if (pr == NULL && qr != NULL)
7121 {
7122 gfc_error ("Allocate-object at %L is subobject of"
7123 " object at %L", &qe->where, &pe->where);
7124 break;
7125 }
7126 /* Here, pr != NULL && qr != NULL */
7127 gcc_assert(pr->type == qr->type);
7128 if (pr->type == REF_ARRAY)
7129 {
7130 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7131 which are legal. */
7132 gcc_assert (qr->type == REF_ARRAY);
7133
7134 if (pr->next && qr->next)
7135 {
7136 gfc_array_ref *par = &(pr->u.ar);
7137 gfc_array_ref *qar = &(qr->u.ar);
7138 if (gfc_dep_compare_expr (par->start[0],
7139 qar->start[0]) != 0)
7140 break;
7141 }
7142 }
7143 else
7144 {
7145 if (pr->u.c.component->name != qr->u.c.component->name)
7146 break;
7147 }
7148
7149 pr = pr->next;
7150 qr = qr->next;
7151 }
7152 }
7153 }
7154 }
7155
7156 if (strcmp (fcn, "ALLOCATE") == 0)
7157 {
7158 for (a = code->ext.alloc.list; a; a = a->next)
7159 resolve_allocate_expr (a->expr, code);
7160 }
7161 else
7162 {
7163 for (a = code->ext.alloc.list; a; a = a->next)
7164 resolve_deallocate_expr (a->expr);
7165 }
7166 }
7167
7168
7169 /************ SELECT CASE resolution subroutines ************/
7170
7171 /* Callback function for our mergesort variant. Determines interval
7172 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7173 op1 > op2. Assumes we're not dealing with the default case.
7174 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7175 There are nine situations to check. */
7176
7177 static int
7178 compare_cases (const gfc_case *op1, const gfc_case *op2)
7179 {
7180 int retval;
7181
7182 if (op1->low == NULL) /* op1 = (:L) */
7183 {
7184 /* op2 = (:N), so overlap. */
7185 retval = 0;
7186 /* op2 = (M:) or (M:N), L < M */
7187 if (op2->low != NULL
7188 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7189 retval = -1;
7190 }
7191 else if (op1->high == NULL) /* op1 = (K:) */
7192 {
7193 /* op2 = (M:), so overlap. */
7194 retval = 0;
7195 /* op2 = (:N) or (M:N), K > N */
7196 if (op2->high != NULL
7197 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7198 retval = 1;
7199 }
7200 else /* op1 = (K:L) */
7201 {
7202 if (op2->low == NULL) /* op2 = (:N), K > N */
7203 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7204 ? 1 : 0;
7205 else if (op2->high == NULL) /* op2 = (M:), L < M */
7206 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7207 ? -1 : 0;
7208 else /* op2 = (M:N) */
7209 {
7210 retval = 0;
7211 /* L < M */
7212 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7213 retval = -1;
7214 /* K > N */
7215 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7216 retval = 1;
7217 }
7218 }
7219
7220 return retval;
7221 }
7222
7223
7224 /* Merge-sort a double linked case list, detecting overlap in the
7225 process. LIST is the head of the double linked case list before it
7226 is sorted. Returns the head of the sorted list if we don't see any
7227 overlap, or NULL otherwise. */
7228
7229 static gfc_case *
7230 check_case_overlap (gfc_case *list)
7231 {
7232 gfc_case *p, *q, *e, *tail;
7233 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7234
7235 /* If the passed list was empty, return immediately. */
7236 if (!list)
7237 return NULL;
7238
7239 overlap_seen = 0;
7240 insize = 1;
7241
7242 /* Loop unconditionally. The only exit from this loop is a return
7243 statement, when we've finished sorting the case list. */
7244 for (;;)
7245 {
7246 p = list;
7247 list = NULL;
7248 tail = NULL;
7249
7250 /* Count the number of merges we do in this pass. */
7251 nmerges = 0;
7252
7253 /* Loop while there exists a merge to be done. */
7254 while (p)
7255 {
7256 int i;
7257
7258 /* Count this merge. */
7259 nmerges++;
7260
7261 /* Cut the list in two pieces by stepping INSIZE places
7262 forward in the list, starting from P. */
7263 psize = 0;
7264 q = p;
7265 for (i = 0; i < insize; i++)
7266 {
7267 psize++;
7268 q = q->right;
7269 if (!q)
7270 break;
7271 }
7272 qsize = insize;
7273
7274 /* Now we have two lists. Merge them! */
7275 while (psize > 0 || (qsize > 0 && q != NULL))
7276 {
7277 /* See from which the next case to merge comes from. */
7278 if (psize == 0)
7279 {
7280 /* P is empty so the next case must come from Q. */
7281 e = q;
7282 q = q->right;
7283 qsize--;
7284 }
7285 else if (qsize == 0 || q == NULL)
7286 {
7287 /* Q is empty. */
7288 e = p;
7289 p = p->right;
7290 psize--;
7291 }
7292 else
7293 {
7294 cmp = compare_cases (p, q);
7295 if (cmp < 0)
7296 {
7297 /* The whole case range for P is less than the
7298 one for Q. */
7299 e = p;
7300 p = p->right;
7301 psize--;
7302 }
7303 else if (cmp > 0)
7304 {
7305 /* The whole case range for Q is greater than
7306 the case range for P. */
7307 e = q;
7308 q = q->right;
7309 qsize--;
7310 }
7311 else
7312 {
7313 /* The cases overlap, or they are the same
7314 element in the list. Either way, we must
7315 issue an error and get the next case from P. */
7316 /* FIXME: Sort P and Q by line number. */
7317 gfc_error ("CASE label at %L overlaps with CASE "
7318 "label at %L", &p->where, &q->where);
7319 overlap_seen = 1;
7320 e = p;
7321 p = p->right;
7322 psize--;
7323 }
7324 }
7325
7326 /* Add the next element to the merged list. */
7327 if (tail)
7328 tail->right = e;
7329 else
7330 list = e;
7331 e->left = tail;
7332 tail = e;
7333 }
7334
7335 /* P has now stepped INSIZE places along, and so has Q. So
7336 they're the same. */
7337 p = q;
7338 }
7339 tail->right = NULL;
7340
7341 /* If we have done only one merge or none at all, we've
7342 finished sorting the cases. */
7343 if (nmerges <= 1)
7344 {
7345 if (!overlap_seen)
7346 return list;
7347 else
7348 return NULL;
7349 }
7350
7351 /* Otherwise repeat, merging lists twice the size. */
7352 insize *= 2;
7353 }
7354 }
7355
7356
7357 /* Check to see if an expression is suitable for use in a CASE statement.
7358 Makes sure that all case expressions are scalar constants of the same
7359 type. Return FAILURE if anything is wrong. */
7360
7361 static gfc_try
7362 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7363 {
7364 if (e == NULL) return SUCCESS;
7365
7366 if (e->ts.type != case_expr->ts.type)
7367 {
7368 gfc_error ("Expression in CASE statement at %L must be of type %s",
7369 &e->where, gfc_basic_typename (case_expr->ts.type));
7370 return FAILURE;
7371 }
7372
7373 /* C805 (R808) For a given case-construct, each case-value shall be of
7374 the same type as case-expr. For character type, length differences
7375 are allowed, but the kind type parameters shall be the same. */
7376
7377 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7378 {
7379 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7380 &e->where, case_expr->ts.kind);
7381 return FAILURE;
7382 }
7383
7384 /* Convert the case value kind to that of case expression kind,
7385 if needed */
7386
7387 if (e->ts.kind != case_expr->ts.kind)
7388 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7389
7390 if (e->rank != 0)
7391 {
7392 gfc_error ("Expression in CASE statement at %L must be scalar",
7393 &e->where);
7394 return FAILURE;
7395 }
7396
7397 return SUCCESS;
7398 }
7399
7400
7401 /* Given a completely parsed select statement, we:
7402
7403 - Validate all expressions and code within the SELECT.
7404 - Make sure that the selection expression is not of the wrong type.
7405 - Make sure that no case ranges overlap.
7406 - Eliminate unreachable cases and unreachable code resulting from
7407 removing case labels.
7408
7409 The standard does allow unreachable cases, e.g. CASE (5:3). But
7410 they are a hassle for code generation, and to prevent that, we just
7411 cut them out here. This is not necessary for overlapping cases
7412 because they are illegal and we never even try to generate code.
7413
7414 We have the additional caveat that a SELECT construct could have
7415 been a computed GOTO in the source code. Fortunately we can fairly
7416 easily work around that here: The case_expr for a "real" SELECT CASE
7417 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7418 we have to do is make sure that the case_expr is a scalar integer
7419 expression. */
7420
7421 static void
7422 resolve_select (gfc_code *code)
7423 {
7424 gfc_code *body;
7425 gfc_expr *case_expr;
7426 gfc_case *cp, *default_case, *tail, *head;
7427 int seen_unreachable;
7428 int seen_logical;
7429 int ncases;
7430 bt type;
7431 gfc_try t;
7432
7433 if (code->expr1 == NULL)
7434 {
7435 /* This was actually a computed GOTO statement. */
7436 case_expr = code->expr2;
7437 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7438 gfc_error ("Selection expression in computed GOTO statement "
7439 "at %L must be a scalar integer expression",
7440 &case_expr->where);
7441
7442 /* Further checking is not necessary because this SELECT was built
7443 by the compiler, so it should always be OK. Just move the
7444 case_expr from expr2 to expr so that we can handle computed
7445 GOTOs as normal SELECTs from here on. */
7446 code->expr1 = code->expr2;
7447 code->expr2 = NULL;
7448 return;
7449 }
7450
7451 case_expr = code->expr1;
7452
7453 type = case_expr->ts.type;
7454 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7455 {
7456 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7457 &case_expr->where, gfc_typename (&case_expr->ts));
7458
7459 /* Punt. Going on here just produce more garbage error messages. */
7460 return;
7461 }
7462
7463 if (case_expr->rank != 0)
7464 {
7465 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7466 "expression", &case_expr->where);
7467
7468 /* Punt. */
7469 return;
7470 }
7471
7472
7473 /* Raise a warning if an INTEGER case value exceeds the range of
7474 the case-expr. Later, all expressions will be promoted to the
7475 largest kind of all case-labels. */
7476
7477 if (type == BT_INTEGER)
7478 for (body = code->block; body; body = body->block)
7479 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7480 {
7481 if (cp->low
7482 && gfc_check_integer_range (cp->low->value.integer,
7483 case_expr->ts.kind) != ARITH_OK)
7484 gfc_warning ("Expression in CASE statement at %L is "
7485 "not in the range of %s", &cp->low->where,
7486 gfc_typename (&case_expr->ts));
7487
7488 if (cp->high
7489 && cp->low != cp->high
7490 && gfc_check_integer_range (cp->high->value.integer,
7491 case_expr->ts.kind) != ARITH_OK)
7492 gfc_warning ("Expression in CASE statement at %L is "
7493 "not in the range of %s", &cp->high->where,
7494 gfc_typename (&case_expr->ts));
7495 }
7496
7497 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7498 of the SELECT CASE expression and its CASE values. Walk the lists
7499 of case values, and if we find a mismatch, promote case_expr to
7500 the appropriate kind. */
7501
7502 if (type == BT_LOGICAL || type == BT_INTEGER)
7503 {
7504 for (body = code->block; body; body = body->block)
7505 {
7506 /* Walk the case label list. */
7507 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7508 {
7509 /* Intercept the DEFAULT case. It does not have a kind. */
7510 if (cp->low == NULL && cp->high == NULL)
7511 continue;
7512
7513 /* Unreachable case ranges are discarded, so ignore. */
7514 if (cp->low != NULL && cp->high != NULL
7515 && cp->low != cp->high
7516 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7517 continue;
7518
7519 if (cp->low != NULL
7520 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7521 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7522
7523 if (cp->high != NULL
7524 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7525 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7526 }
7527 }
7528 }
7529
7530 /* Assume there is no DEFAULT case. */
7531 default_case = NULL;
7532 head = tail = NULL;
7533 ncases = 0;
7534 seen_logical = 0;
7535
7536 for (body = code->block; body; body = body->block)
7537 {
7538 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7539 t = SUCCESS;
7540 seen_unreachable = 0;
7541
7542 /* Walk the case label list, making sure that all case labels
7543 are legal. */
7544 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7545 {
7546 /* Count the number of cases in the whole construct. */
7547 ncases++;
7548
7549 /* Intercept the DEFAULT case. */
7550 if (cp->low == NULL && cp->high == NULL)
7551 {
7552 if (default_case != NULL)
7553 {
7554 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7555 "by a second DEFAULT CASE at %L",
7556 &default_case->where, &cp->where);
7557 t = FAILURE;
7558 break;
7559 }
7560 else
7561 {
7562 default_case = cp;
7563 continue;
7564 }
7565 }
7566
7567 /* Deal with single value cases and case ranges. Errors are
7568 issued from the validation function. */
7569 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7570 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7571 {
7572 t = FAILURE;
7573 break;
7574 }
7575
7576 if (type == BT_LOGICAL
7577 && ((cp->low == NULL || cp->high == NULL)
7578 || cp->low != cp->high))
7579 {
7580 gfc_error ("Logical range in CASE statement at %L is not "
7581 "allowed", &cp->low->where);
7582 t = FAILURE;
7583 break;
7584 }
7585
7586 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7587 {
7588 int value;
7589 value = cp->low->value.logical == 0 ? 2 : 1;
7590 if (value & seen_logical)
7591 {
7592 gfc_error ("Constant logical value in CASE statement "
7593 "is repeated at %L",
7594 &cp->low->where);
7595 t = FAILURE;
7596 break;
7597 }
7598 seen_logical |= value;
7599 }
7600
7601 if (cp->low != NULL && cp->high != NULL
7602 && cp->low != cp->high
7603 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7604 {
7605 if (gfc_option.warn_surprising)
7606 gfc_warning ("Range specification at %L can never "
7607 "be matched", &cp->where);
7608
7609 cp->unreachable = 1;
7610 seen_unreachable = 1;
7611 }
7612 else
7613 {
7614 /* If the case range can be matched, it can also overlap with
7615 other cases. To make sure it does not, we put it in a
7616 double linked list here. We sort that with a merge sort
7617 later on to detect any overlapping cases. */
7618 if (!head)
7619 {
7620 head = tail = cp;
7621 head->right = head->left = NULL;
7622 }
7623 else
7624 {
7625 tail->right = cp;
7626 tail->right->left = tail;
7627 tail = tail->right;
7628 tail->right = NULL;
7629 }
7630 }
7631 }
7632
7633 /* It there was a failure in the previous case label, give up
7634 for this case label list. Continue with the next block. */
7635 if (t == FAILURE)
7636 continue;
7637
7638 /* See if any case labels that are unreachable have been seen.
7639 If so, we eliminate them. This is a bit of a kludge because
7640 the case lists for a single case statement (label) is a
7641 single forward linked lists. */
7642 if (seen_unreachable)
7643 {
7644 /* Advance until the first case in the list is reachable. */
7645 while (body->ext.block.case_list != NULL
7646 && body->ext.block.case_list->unreachable)
7647 {
7648 gfc_case *n = body->ext.block.case_list;
7649 body->ext.block.case_list = body->ext.block.case_list->next;
7650 n->next = NULL;
7651 gfc_free_case_list (n);
7652 }
7653
7654 /* Strip all other unreachable cases. */
7655 if (body->ext.block.case_list)
7656 {
7657 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7658 {
7659 if (cp->next->unreachable)
7660 {
7661 gfc_case *n = cp->next;
7662 cp->next = cp->next->next;
7663 n->next = NULL;
7664 gfc_free_case_list (n);
7665 }
7666 }
7667 }
7668 }
7669 }
7670
7671 /* See if there were overlapping cases. If the check returns NULL,
7672 there was overlap. In that case we don't do anything. If head
7673 is non-NULL, we prepend the DEFAULT case. The sorted list can
7674 then used during code generation for SELECT CASE constructs with
7675 a case expression of a CHARACTER type. */
7676 if (head)
7677 {
7678 head = check_case_overlap (head);
7679
7680 /* Prepend the default_case if it is there. */
7681 if (head != NULL && default_case)
7682 {
7683 default_case->left = NULL;
7684 default_case->right = head;
7685 head->left = default_case;
7686 }
7687 }
7688
7689 /* Eliminate dead blocks that may be the result if we've seen
7690 unreachable case labels for a block. */
7691 for (body = code; body && body->block; body = body->block)
7692 {
7693 if (body->block->ext.block.case_list == NULL)
7694 {
7695 /* Cut the unreachable block from the code chain. */
7696 gfc_code *c = body->block;
7697 body->block = c->block;
7698
7699 /* Kill the dead block, but not the blocks below it. */
7700 c->block = NULL;
7701 gfc_free_statements (c);
7702 }
7703 }
7704
7705 /* More than two cases is legal but insane for logical selects.
7706 Issue a warning for it. */
7707 if (gfc_option.warn_surprising && type == BT_LOGICAL
7708 && ncases > 2)
7709 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7710 &code->loc);
7711 }
7712
7713
7714 /* Check if a derived type is extensible. */
7715
7716 bool
7717 gfc_type_is_extensible (gfc_symbol *sym)
7718 {
7719 return !(sym->attr.is_bind_c || sym->attr.sequence);
7720 }
7721
7722
7723 /* Resolve an associate name: Resolve target and ensure the type-spec is
7724 correct as well as possibly the array-spec. */
7725
7726 static void
7727 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7728 {
7729 gfc_expr* target;
7730
7731 gcc_assert (sym->assoc);
7732 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7733
7734 /* If this is for SELECT TYPE, the target may not yet be set. In that
7735 case, return. Resolution will be called later manually again when
7736 this is done. */
7737 target = sym->assoc->target;
7738 if (!target)
7739 return;
7740 gcc_assert (!sym->assoc->dangling);
7741
7742 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7743 return;
7744
7745 /* For variable targets, we get some attributes from the target. */
7746 if (target->expr_type == EXPR_VARIABLE)
7747 {
7748 gfc_symbol* tsym;
7749
7750 gcc_assert (target->symtree);
7751 tsym = target->symtree->n.sym;
7752
7753 sym->attr.asynchronous = tsym->attr.asynchronous;
7754 sym->attr.volatile_ = tsym->attr.volatile_;
7755
7756 sym->attr.target = (tsym->attr.target || tsym->attr.pointer);
7757 }
7758
7759 /* Get type if this was not already set. Note that it can be
7760 some other type than the target in case this is a SELECT TYPE
7761 selector! So we must not update when the type is already there. */
7762 if (sym->ts.type == BT_UNKNOWN)
7763 sym->ts = target->ts;
7764 gcc_assert (sym->ts.type != BT_UNKNOWN);
7765
7766 /* See if this is a valid association-to-variable. */
7767 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7768 && !gfc_has_vector_subscript (target));
7769
7770 /* Finally resolve if this is an array or not. */
7771 if (sym->attr.dimension && target->rank == 0)
7772 {
7773 gfc_error ("Associate-name '%s' at %L is used as array",
7774 sym->name, &sym->declared_at);
7775 sym->attr.dimension = 0;
7776 return;
7777 }
7778 if (target->rank > 0)
7779 sym->attr.dimension = 1;
7780
7781 if (sym->attr.dimension)
7782 {
7783 sym->as = gfc_get_array_spec ();
7784 sym->as->rank = target->rank;
7785 sym->as->type = AS_DEFERRED;
7786
7787 /* Target must not be coindexed, thus the associate-variable
7788 has no corank. */
7789 sym->as->corank = 0;
7790 }
7791 }
7792
7793
7794 /* Resolve a SELECT TYPE statement. */
7795
7796 static void
7797 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7798 {
7799 gfc_symbol *selector_type;
7800 gfc_code *body, *new_st, *if_st, *tail;
7801 gfc_code *class_is = NULL, *default_case = NULL;
7802 gfc_case *c;
7803 gfc_symtree *st;
7804 char name[GFC_MAX_SYMBOL_LEN];
7805 gfc_namespace *ns;
7806 int error = 0;
7807
7808 ns = code->ext.block.ns;
7809 gfc_resolve (ns);
7810
7811 /* Check for F03:C813. */
7812 if (code->expr1->ts.type != BT_CLASS
7813 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7814 {
7815 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7816 "at %L", &code->loc);
7817 return;
7818 }
7819
7820 if (code->expr2)
7821 {
7822 if (code->expr1->symtree->n.sym->attr.untyped)
7823 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7824 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7825 }
7826 else
7827 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7828
7829 /* Loop over TYPE IS / CLASS IS cases. */
7830 for (body = code->block; body; body = body->block)
7831 {
7832 c = body->ext.block.case_list;
7833
7834 /* Check F03:C815. */
7835 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7836 && !gfc_type_is_extensible (c->ts.u.derived))
7837 {
7838 gfc_error ("Derived type '%s' at %L must be extensible",
7839 c->ts.u.derived->name, &c->where);
7840 error++;
7841 continue;
7842 }
7843
7844 /* Check F03:C816. */
7845 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7846 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7847 {
7848 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7849 c->ts.u.derived->name, &c->where, selector_type->name);
7850 error++;
7851 continue;
7852 }
7853
7854 /* Intercept the DEFAULT case. */
7855 if (c->ts.type == BT_UNKNOWN)
7856 {
7857 /* Check F03:C818. */
7858 if (default_case)
7859 {
7860 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7861 "by a second DEFAULT CASE at %L",
7862 &default_case->ext.block.case_list->where, &c->where);
7863 error++;
7864 continue;
7865 }
7866
7867 default_case = body;
7868 }
7869 }
7870
7871 if (error > 0)
7872 return;
7873
7874 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7875 target if present. If there are any EXIT statements referring to the
7876 SELECT TYPE construct, this is no problem because the gfc_code
7877 reference stays the same and EXIT is equally possible from the BLOCK
7878 it is changed to. */
7879 code->op = EXEC_BLOCK;
7880 if (code->expr2)
7881 {
7882 gfc_association_list* assoc;
7883
7884 assoc = gfc_get_association_list ();
7885 assoc->st = code->expr1->symtree;
7886 assoc->target = gfc_copy_expr (code->expr2);
7887 /* assoc->variable will be set by resolve_assoc_var. */
7888
7889 code->ext.block.assoc = assoc;
7890 code->expr1->symtree->n.sym->assoc = assoc;
7891
7892 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7893 }
7894 else
7895 code->ext.block.assoc = NULL;
7896
7897 /* Add EXEC_SELECT to switch on type. */
7898 new_st = gfc_get_code ();
7899 new_st->op = code->op;
7900 new_st->expr1 = code->expr1;
7901 new_st->expr2 = code->expr2;
7902 new_st->block = code->block;
7903 code->expr1 = code->expr2 = NULL;
7904 code->block = NULL;
7905 if (!ns->code)
7906 ns->code = new_st;
7907 else
7908 ns->code->next = new_st;
7909 code = new_st;
7910 code->op = EXEC_SELECT;
7911 gfc_add_vptr_component (code->expr1);
7912 gfc_add_hash_component (code->expr1);
7913
7914 /* Loop over TYPE IS / CLASS IS cases. */
7915 for (body = code->block; body; body = body->block)
7916 {
7917 c = body->ext.block.case_list;
7918
7919 if (c->ts.type == BT_DERIVED)
7920 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7921 c->ts.u.derived->hash_value);
7922
7923 else if (c->ts.type == BT_UNKNOWN)
7924 continue;
7925
7926 /* Associate temporary to selector. This should only be done
7927 when this case is actually true, so build a new ASSOCIATE
7928 that does precisely this here (instead of using the
7929 'global' one). */
7930
7931 if (c->ts.type == BT_CLASS)
7932 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
7933 else
7934 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
7935 st = gfc_find_symtree (ns->sym_root, name);
7936 gcc_assert (st->n.sym->assoc);
7937 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7938 if (c->ts.type == BT_DERIVED)
7939 gfc_add_data_component (st->n.sym->assoc->target);
7940
7941 new_st = gfc_get_code ();
7942 new_st->op = EXEC_BLOCK;
7943 new_st->ext.block.ns = gfc_build_block_ns (ns);
7944 new_st->ext.block.ns->code = body->next;
7945 body->next = new_st;
7946
7947 /* Chain in the new list only if it is marked as dangling. Otherwise
7948 there is a CASE label overlap and this is already used. Just ignore,
7949 the error is diagonsed elsewhere. */
7950 if (st->n.sym->assoc->dangling)
7951 {
7952 new_st->ext.block.assoc = st->n.sym->assoc;
7953 st->n.sym->assoc->dangling = 0;
7954 }
7955
7956 resolve_assoc_var (st->n.sym, false);
7957 }
7958
7959 /* Take out CLASS IS cases for separate treatment. */
7960 body = code;
7961 while (body && body->block)
7962 {
7963 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
7964 {
7965 /* Add to class_is list. */
7966 if (class_is == NULL)
7967 {
7968 class_is = body->block;
7969 tail = class_is;
7970 }
7971 else
7972 {
7973 for (tail = class_is; tail->block; tail = tail->block) ;
7974 tail->block = body->block;
7975 tail = tail->block;
7976 }
7977 /* Remove from EXEC_SELECT list. */
7978 body->block = body->block->block;
7979 tail->block = NULL;
7980 }
7981 else
7982 body = body->block;
7983 }
7984
7985 if (class_is)
7986 {
7987 gfc_symbol *vtab;
7988
7989 if (!default_case)
7990 {
7991 /* Add a default case to hold the CLASS IS cases. */
7992 for (tail = code; tail->block; tail = tail->block) ;
7993 tail->block = gfc_get_code ();
7994 tail = tail->block;
7995 tail->op = EXEC_SELECT_TYPE;
7996 tail->ext.block.case_list = gfc_get_case ();
7997 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
7998 tail->next = NULL;
7999 default_case = tail;
8000 }
8001
8002 /* More than one CLASS IS block? */
8003 if (class_is->block)
8004 {
8005 gfc_code **c1,*c2;
8006 bool swapped;
8007 /* Sort CLASS IS blocks by extension level. */
8008 do
8009 {
8010 swapped = false;
8011 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8012 {
8013 c2 = (*c1)->block;
8014 /* F03:C817 (check for doubles). */
8015 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8016 == c2->ext.block.case_list->ts.u.derived->hash_value)
8017 {
8018 gfc_error ("Double CLASS IS block in SELECT TYPE "
8019 "statement at %L",
8020 &c2->ext.block.case_list->where);
8021 return;
8022 }
8023 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8024 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8025 {
8026 /* Swap. */
8027 (*c1)->block = c2->block;
8028 c2->block = *c1;
8029 *c1 = c2;
8030 swapped = true;
8031 }
8032 }
8033 }
8034 while (swapped);
8035 }
8036
8037 /* Generate IF chain. */
8038 if_st = gfc_get_code ();
8039 if_st->op = EXEC_IF;
8040 new_st = if_st;
8041 for (body = class_is; body; body = body->block)
8042 {
8043 new_st->block = gfc_get_code ();
8044 new_st = new_st->block;
8045 new_st->op = EXEC_IF;
8046 /* Set up IF condition: Call _gfortran_is_extension_of. */
8047 new_st->expr1 = gfc_get_expr ();
8048 new_st->expr1->expr_type = EXPR_FUNCTION;
8049 new_st->expr1->ts.type = BT_LOGICAL;
8050 new_st->expr1->ts.kind = 4;
8051 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8052 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8053 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8054 /* Set up arguments. */
8055 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8056 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8057 new_st->expr1->value.function.actual->expr->where = code->loc;
8058 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8059 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8060 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8061 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8062 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8063 new_st->next = body->next;
8064 }
8065 if (default_case->next)
8066 {
8067 new_st->block = gfc_get_code ();
8068 new_st = new_st->block;
8069 new_st->op = EXEC_IF;
8070 new_st->next = default_case->next;
8071 }
8072
8073 /* Replace CLASS DEFAULT code by the IF chain. */
8074 default_case->next = if_st;
8075 }
8076
8077 /* Resolve the internal code. This can not be done earlier because
8078 it requires that the sym->assoc of selectors is set already. */
8079 gfc_current_ns = ns;
8080 gfc_resolve_blocks (code->block, gfc_current_ns);
8081 gfc_current_ns = old_ns;
8082
8083 resolve_select (code);
8084 }
8085
8086
8087 /* Resolve a transfer statement. This is making sure that:
8088 -- a derived type being transferred has only non-pointer components
8089 -- a derived type being transferred doesn't have private components, unless
8090 it's being transferred from the module where the type was defined
8091 -- we're not trying to transfer a whole assumed size array. */
8092
8093 static void
8094 resolve_transfer (gfc_code *code)
8095 {
8096 gfc_typespec *ts;
8097 gfc_symbol *sym;
8098 gfc_ref *ref;
8099 gfc_expr *exp;
8100
8101 exp = code->expr1;
8102
8103 while (exp != NULL && exp->expr_type == EXPR_OP
8104 && exp->value.op.op == INTRINSIC_PARENTHESES)
8105 exp = exp->value.op.op1;
8106
8107 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8108 && exp->expr_type != EXPR_FUNCTION))
8109 return;
8110
8111 /* If we are reading, the variable will be changed. Note that
8112 code->ext.dt may be NULL if the TRANSFER is related to
8113 an INQUIRE statement -- but in this case, we are not reading, either. */
8114 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8115 && gfc_check_vardef_context (exp, false, false, _("item in READ"))
8116 == FAILURE)
8117 return;
8118
8119 sym = exp->symtree->n.sym;
8120 ts = &sym->ts;
8121
8122 /* Go to actual component transferred. */
8123 for (ref = exp->ref; ref; ref = ref->next)
8124 if (ref->type == REF_COMPONENT)
8125 ts = &ref->u.c.component->ts;
8126
8127 if (ts->type == BT_CLASS)
8128 {
8129 /* FIXME: Test for defined input/output. */
8130 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8131 "it is processed by a defined input/output procedure",
8132 &code->loc);
8133 return;
8134 }
8135
8136 if (ts->type == BT_DERIVED)
8137 {
8138 /* Check that transferred derived type doesn't contain POINTER
8139 components. */
8140 if (ts->u.derived->attr.pointer_comp)
8141 {
8142 gfc_error ("Data transfer element at %L cannot have POINTER "
8143 "components unless it is processed by a defined "
8144 "input/output procedure", &code->loc);
8145 return;
8146 }
8147
8148 /* F08:C935. */
8149 if (ts->u.derived->attr.proc_pointer_comp)
8150 {
8151 gfc_error ("Data transfer element at %L cannot have "
8152 "procedure pointer components", &code->loc);
8153 return;
8154 }
8155
8156 if (ts->u.derived->attr.alloc_comp)
8157 {
8158 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8159 "components unless it is processed by a defined "
8160 "input/output procedure", &code->loc);
8161 return;
8162 }
8163
8164 if (derived_inaccessible (ts->u.derived))
8165 {
8166 gfc_error ("Data transfer element at %L cannot have "
8167 "PRIVATE components",&code->loc);
8168 return;
8169 }
8170 }
8171
8172 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
8173 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8174 {
8175 gfc_error ("Data transfer element at %L cannot be a full reference to "
8176 "an assumed-size array", &code->loc);
8177 return;
8178 }
8179 }
8180
8181
8182 /*********** Toplevel code resolution subroutines ***********/
8183
8184 /* Find the set of labels that are reachable from this block. We also
8185 record the last statement in each block. */
8186
8187 static void
8188 find_reachable_labels (gfc_code *block)
8189 {
8190 gfc_code *c;
8191
8192 if (!block)
8193 return;
8194
8195 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8196
8197 /* Collect labels in this block. We don't keep those corresponding
8198 to END {IF|SELECT}, these are checked in resolve_branch by going
8199 up through the code_stack. */
8200 for (c = block; c; c = c->next)
8201 {
8202 if (c->here && c->op != EXEC_END_BLOCK)
8203 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8204 }
8205
8206 /* Merge with labels from parent block. */
8207 if (cs_base->prev)
8208 {
8209 gcc_assert (cs_base->prev->reachable_labels);
8210 bitmap_ior_into (cs_base->reachable_labels,
8211 cs_base->prev->reachable_labels);
8212 }
8213 }
8214
8215
8216 static void
8217 resolve_lock_unlock (gfc_code *code)
8218 {
8219 if (code->expr1->ts.type != BT_DERIVED
8220 || code->expr1->expr_type != EXPR_VARIABLE
8221 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8222 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8223 || code->expr1->rank != 0
8224 || !(gfc_expr_attr (code->expr1).codimension
8225 || gfc_is_coindexed (code->expr1)))
8226 gfc_error ("Lock variable at %L must be a scalar coarray of type "
8227 "LOCK_TYPE", &code->expr1->where);
8228
8229 /* Check STAT. */
8230 if (code->expr2
8231 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8232 || code->expr2->expr_type != EXPR_VARIABLE))
8233 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8234 &code->expr2->where);
8235
8236 if (code->expr2
8237 && gfc_check_vardef_context (code->expr2, false, false,
8238 _("STAT variable")) == FAILURE)
8239 return;
8240
8241 /* Check ERRMSG. */
8242 if (code->expr3
8243 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8244 || code->expr3->expr_type != EXPR_VARIABLE))
8245 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8246 &code->expr3->where);
8247
8248 if (code->expr3
8249 && gfc_check_vardef_context (code->expr3, false, false,
8250 _("ERRMSG variable")) == FAILURE)
8251 return;
8252
8253 /* Check ACQUIRED_LOCK. */
8254 if (code->expr4
8255 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8256 || code->expr4->expr_type != EXPR_VARIABLE))
8257 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8258 "variable", &code->expr4->where);
8259
8260 if (code->expr4
8261 && gfc_check_vardef_context (code->expr4, false, false,
8262 _("ACQUIRED_LOCK variable")) == FAILURE)
8263 return;
8264 }
8265
8266
8267 static void
8268 resolve_sync (gfc_code *code)
8269 {
8270 /* Check imageset. The * case matches expr1 == NULL. */
8271 if (code->expr1)
8272 {
8273 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8274 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8275 "INTEGER expression", &code->expr1->where);
8276 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8277 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8278 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8279 &code->expr1->where);
8280 else if (code->expr1->expr_type == EXPR_ARRAY
8281 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8282 {
8283 gfc_constructor *cons;
8284 cons = gfc_constructor_first (code->expr1->value.constructor);
8285 for (; cons; cons = gfc_constructor_next (cons))
8286 if (cons->expr->expr_type == EXPR_CONSTANT
8287 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8288 gfc_error ("Imageset argument at %L must between 1 and "
8289 "num_images()", &cons->expr->where);
8290 }
8291 }
8292
8293 /* Check STAT. */
8294 if (code->expr2
8295 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8296 || code->expr2->expr_type != EXPR_VARIABLE))
8297 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8298 &code->expr2->where);
8299
8300 /* Check ERRMSG. */
8301 if (code->expr3
8302 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8303 || code->expr3->expr_type != EXPR_VARIABLE))
8304 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8305 &code->expr3->where);
8306 }
8307
8308
8309 /* Given a branch to a label, see if the branch is conforming.
8310 The code node describes where the branch is located. */
8311
8312 static void
8313 resolve_branch (gfc_st_label *label, gfc_code *code)
8314 {
8315 code_stack *stack;
8316
8317 if (label == NULL)
8318 return;
8319
8320 /* Step one: is this a valid branching target? */
8321
8322 if (label->defined == ST_LABEL_UNKNOWN)
8323 {
8324 gfc_error ("Label %d referenced at %L is never defined", label->value,
8325 &label->where);
8326 return;
8327 }
8328
8329 if (label->defined != ST_LABEL_TARGET)
8330 {
8331 gfc_error ("Statement at %L is not a valid branch target statement "
8332 "for the branch statement at %L", &label->where, &code->loc);
8333 return;
8334 }
8335
8336 /* Step two: make sure this branch is not a branch to itself ;-) */
8337
8338 if (code->here == label)
8339 {
8340 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8341 return;
8342 }
8343
8344 /* Step three: See if the label is in the same block as the
8345 branching statement. The hard work has been done by setting up
8346 the bitmap reachable_labels. */
8347
8348 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8349 {
8350 /* Check now whether there is a CRITICAL construct; if so, check
8351 whether the label is still visible outside of the CRITICAL block,
8352 which is invalid. */
8353 for (stack = cs_base; stack; stack = stack->prev)
8354 if (stack->current->op == EXEC_CRITICAL
8355 && bitmap_bit_p (stack->reachable_labels, label->value))
8356 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8357 " at %L", &code->loc, &label->where);
8358
8359 return;
8360 }
8361
8362 /* Step four: If we haven't found the label in the bitmap, it may
8363 still be the label of the END of the enclosing block, in which
8364 case we find it by going up the code_stack. */
8365
8366 for (stack = cs_base; stack; stack = stack->prev)
8367 {
8368 if (stack->current->next && stack->current->next->here == label)
8369 break;
8370 if (stack->current->op == EXEC_CRITICAL)
8371 {
8372 /* Note: A label at END CRITICAL does not leave the CRITICAL
8373 construct as END CRITICAL is still part of it. */
8374 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8375 " at %L", &code->loc, &label->where);
8376 return;
8377 }
8378 }
8379
8380 if (stack)
8381 {
8382 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
8383 return;
8384 }
8385
8386 /* The label is not in an enclosing block, so illegal. This was
8387 allowed in Fortran 66, so we allow it as extension. No
8388 further checks are necessary in this case. */
8389 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8390 "as the GOTO statement at %L", &label->where,
8391 &code->loc);
8392 return;
8393 }
8394
8395
8396 /* Check whether EXPR1 has the same shape as EXPR2. */
8397
8398 static gfc_try
8399 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8400 {
8401 mpz_t shape[GFC_MAX_DIMENSIONS];
8402 mpz_t shape2[GFC_MAX_DIMENSIONS];
8403 gfc_try result = FAILURE;
8404 int i;
8405
8406 /* Compare the rank. */
8407 if (expr1->rank != expr2->rank)
8408 return result;
8409
8410 /* Compare the size of each dimension. */
8411 for (i=0; i<expr1->rank; i++)
8412 {
8413 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8414 goto ignore;
8415
8416 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8417 goto ignore;
8418
8419 if (mpz_cmp (shape[i], shape2[i]))
8420 goto over;
8421 }
8422
8423 /* When either of the two expression is an assumed size array, we
8424 ignore the comparison of dimension sizes. */
8425 ignore:
8426 result = SUCCESS;
8427
8428 over:
8429 for (i--; i >= 0; i--)
8430 {
8431 mpz_clear (shape[i]);
8432 mpz_clear (shape2[i]);
8433 }
8434 return result;
8435 }
8436
8437
8438 /* Check whether a WHERE assignment target or a WHERE mask expression
8439 has the same shape as the outmost WHERE mask expression. */
8440
8441 static void
8442 resolve_where (gfc_code *code, gfc_expr *mask)
8443 {
8444 gfc_code *cblock;
8445 gfc_code *cnext;
8446 gfc_expr *e = NULL;
8447
8448 cblock = code->block;
8449
8450 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8451 In case of nested WHERE, only the outmost one is stored. */
8452 if (mask == NULL) /* outmost WHERE */
8453 e = cblock->expr1;
8454 else /* inner WHERE */
8455 e = mask;
8456
8457 while (cblock)
8458 {
8459 if (cblock->expr1)
8460 {
8461 /* Check if the mask-expr has a consistent shape with the
8462 outmost WHERE mask-expr. */
8463 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8464 gfc_error ("WHERE mask at %L has inconsistent shape",
8465 &cblock->expr1->where);
8466 }
8467
8468 /* the assignment statement of a WHERE statement, or the first
8469 statement in where-body-construct of a WHERE construct */
8470 cnext = cblock->next;
8471 while (cnext)
8472 {
8473 switch (cnext->op)
8474 {
8475 /* WHERE assignment statement */
8476 case EXEC_ASSIGN:
8477
8478 /* Check shape consistent for WHERE assignment target. */
8479 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8480 gfc_error ("WHERE assignment target at %L has "
8481 "inconsistent shape", &cnext->expr1->where);
8482 break;
8483
8484
8485 case EXEC_ASSIGN_CALL:
8486 resolve_call (cnext);
8487 if (!cnext->resolved_sym->attr.elemental)
8488 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8489 &cnext->ext.actual->expr->where);
8490 break;
8491
8492 /* WHERE or WHERE construct is part of a where-body-construct */
8493 case EXEC_WHERE:
8494 resolve_where (cnext, e);
8495 break;
8496
8497 default:
8498 gfc_error ("Unsupported statement inside WHERE at %L",
8499 &cnext->loc);
8500 }
8501 /* the next statement within the same where-body-construct */
8502 cnext = cnext->next;
8503 }
8504 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8505 cblock = cblock->block;
8506 }
8507 }
8508
8509
8510 /* Resolve assignment in FORALL construct.
8511 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8512 FORALL index variables. */
8513
8514 static void
8515 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8516 {
8517 int n;
8518
8519 for (n = 0; n < nvar; n++)
8520 {
8521 gfc_symbol *forall_index;
8522
8523 forall_index = var_expr[n]->symtree->n.sym;
8524
8525 /* Check whether the assignment target is one of the FORALL index
8526 variable. */
8527 if ((code->expr1->expr_type == EXPR_VARIABLE)
8528 && (code->expr1->symtree->n.sym == forall_index))
8529 gfc_error ("Assignment to a FORALL index variable at %L",
8530 &code->expr1->where);
8531 else
8532 {
8533 /* If one of the FORALL index variables doesn't appear in the
8534 assignment variable, then there could be a many-to-one
8535 assignment. Emit a warning rather than an error because the
8536 mask could be resolving this problem. */
8537 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8538 gfc_warning ("The FORALL with index '%s' is not used on the "
8539 "left side of the assignment at %L and so might "
8540 "cause multiple assignment to this object",
8541 var_expr[n]->symtree->name, &code->expr1->where);
8542 }
8543 }
8544 }
8545
8546
8547 /* Resolve WHERE statement in FORALL construct. */
8548
8549 static void
8550 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8551 gfc_expr **var_expr)
8552 {
8553 gfc_code *cblock;
8554 gfc_code *cnext;
8555
8556 cblock = code->block;
8557 while (cblock)
8558 {
8559 /* the assignment statement of a WHERE statement, or the first
8560 statement in where-body-construct of a WHERE construct */
8561 cnext = cblock->next;
8562 while (cnext)
8563 {
8564 switch (cnext->op)
8565 {
8566 /* WHERE assignment statement */
8567 case EXEC_ASSIGN:
8568 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8569 break;
8570
8571 /* WHERE operator assignment statement */
8572 case EXEC_ASSIGN_CALL:
8573 resolve_call (cnext);
8574 if (!cnext->resolved_sym->attr.elemental)
8575 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8576 &cnext->ext.actual->expr->where);
8577 break;
8578
8579 /* WHERE or WHERE construct is part of a where-body-construct */
8580 case EXEC_WHERE:
8581 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8582 break;
8583
8584 default:
8585 gfc_error ("Unsupported statement inside WHERE at %L",
8586 &cnext->loc);
8587 }
8588 /* the next statement within the same where-body-construct */
8589 cnext = cnext->next;
8590 }
8591 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8592 cblock = cblock->block;
8593 }
8594 }
8595
8596
8597 /* Traverse the FORALL body to check whether the following errors exist:
8598 1. For assignment, check if a many-to-one assignment happens.
8599 2. For WHERE statement, check the WHERE body to see if there is any
8600 many-to-one assignment. */
8601
8602 static void
8603 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8604 {
8605 gfc_code *c;
8606
8607 c = code->block->next;
8608 while (c)
8609 {
8610 switch (c->op)
8611 {
8612 case EXEC_ASSIGN:
8613 case EXEC_POINTER_ASSIGN:
8614 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8615 break;
8616
8617 case EXEC_ASSIGN_CALL:
8618 resolve_call (c);
8619 break;
8620
8621 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8622 there is no need to handle it here. */
8623 case EXEC_FORALL:
8624 break;
8625 case EXEC_WHERE:
8626 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8627 break;
8628 default:
8629 break;
8630 }
8631 /* The next statement in the FORALL body. */
8632 c = c->next;
8633 }
8634 }
8635
8636
8637 /* Counts the number of iterators needed inside a forall construct, including
8638 nested forall constructs. This is used to allocate the needed memory
8639 in gfc_resolve_forall. */
8640
8641 static int
8642 gfc_count_forall_iterators (gfc_code *code)
8643 {
8644 int max_iters, sub_iters, current_iters;
8645 gfc_forall_iterator *fa;
8646
8647 gcc_assert(code->op == EXEC_FORALL);
8648 max_iters = 0;
8649 current_iters = 0;
8650
8651 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8652 current_iters ++;
8653
8654 code = code->block->next;
8655
8656 while (code)
8657 {
8658 if (code->op == EXEC_FORALL)
8659 {
8660 sub_iters = gfc_count_forall_iterators (code);
8661 if (sub_iters > max_iters)
8662 max_iters = sub_iters;
8663 }
8664 code = code->next;
8665 }
8666
8667 return current_iters + max_iters;
8668 }
8669
8670
8671 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8672 gfc_resolve_forall_body to resolve the FORALL body. */
8673
8674 static void
8675 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8676 {
8677 static gfc_expr **var_expr;
8678 static int total_var = 0;
8679 static int nvar = 0;
8680 int old_nvar, tmp;
8681 gfc_forall_iterator *fa;
8682 int i;
8683
8684 old_nvar = nvar;
8685
8686 /* Start to resolve a FORALL construct */
8687 if (forall_save == 0)
8688 {
8689 /* Count the total number of FORALL index in the nested FORALL
8690 construct in order to allocate the VAR_EXPR with proper size. */
8691 total_var = gfc_count_forall_iterators (code);
8692
8693 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8694 var_expr = XCNEWVEC (gfc_expr *, total_var);
8695 }
8696
8697 /* The information about FORALL iterator, including FORALL index start, end
8698 and stride. The FORALL index can not appear in start, end or stride. */
8699 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8700 {
8701 /* Check if any outer FORALL index name is the same as the current
8702 one. */
8703 for (i = 0; i < nvar; i++)
8704 {
8705 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8706 {
8707 gfc_error ("An outer FORALL construct already has an index "
8708 "with this name %L", &fa->var->where);
8709 }
8710 }
8711
8712 /* Record the current FORALL index. */
8713 var_expr[nvar] = gfc_copy_expr (fa->var);
8714
8715 nvar++;
8716
8717 /* No memory leak. */
8718 gcc_assert (nvar <= total_var);
8719 }
8720
8721 /* Resolve the FORALL body. */
8722 gfc_resolve_forall_body (code, nvar, var_expr);
8723
8724 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8725 gfc_resolve_blocks (code->block, ns);
8726
8727 tmp = nvar;
8728 nvar = old_nvar;
8729 /* Free only the VAR_EXPRs allocated in this frame. */
8730 for (i = nvar; i < tmp; i++)
8731 gfc_free_expr (var_expr[i]);
8732
8733 if (nvar == 0)
8734 {
8735 /* We are in the outermost FORALL construct. */
8736 gcc_assert (forall_save == 0);
8737
8738 /* VAR_EXPR is not needed any more. */
8739 free (var_expr);
8740 total_var = 0;
8741 }
8742 }
8743
8744
8745 /* Resolve a BLOCK construct statement. */
8746
8747 static void
8748 resolve_block_construct (gfc_code* code)
8749 {
8750 /* Resolve the BLOCK's namespace. */
8751 gfc_resolve (code->ext.block.ns);
8752
8753 /* For an ASSOCIATE block, the associations (and their targets) are already
8754 resolved during resolve_symbol. */
8755 }
8756
8757
8758 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8759 DO code nodes. */
8760
8761 static void resolve_code (gfc_code *, gfc_namespace *);
8762
8763 void
8764 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8765 {
8766 gfc_try t;
8767
8768 for (; b; b = b->block)
8769 {
8770 t = gfc_resolve_expr (b->expr1);
8771 if (gfc_resolve_expr (b->expr2) == FAILURE)
8772 t = FAILURE;
8773
8774 switch (b->op)
8775 {
8776 case EXEC_IF:
8777 if (t == SUCCESS && b->expr1 != NULL
8778 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8779 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8780 &b->expr1->where);
8781 break;
8782
8783 case EXEC_WHERE:
8784 if (t == SUCCESS
8785 && b->expr1 != NULL
8786 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8787 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8788 &b->expr1->where);
8789 break;
8790
8791 case EXEC_GOTO:
8792 resolve_branch (b->label1, b);
8793 break;
8794
8795 case EXEC_BLOCK:
8796 resolve_block_construct (b);
8797 break;
8798
8799 case EXEC_SELECT:
8800 case EXEC_SELECT_TYPE:
8801 case EXEC_FORALL:
8802 case EXEC_DO:
8803 case EXEC_DO_WHILE:
8804 case EXEC_CRITICAL:
8805 case EXEC_READ:
8806 case EXEC_WRITE:
8807 case EXEC_IOLENGTH:
8808 case EXEC_WAIT:
8809 break;
8810
8811 case EXEC_OMP_ATOMIC:
8812 case EXEC_OMP_CRITICAL:
8813 case EXEC_OMP_DO:
8814 case EXEC_OMP_MASTER:
8815 case EXEC_OMP_ORDERED:
8816 case EXEC_OMP_PARALLEL:
8817 case EXEC_OMP_PARALLEL_DO:
8818 case EXEC_OMP_PARALLEL_SECTIONS:
8819 case EXEC_OMP_PARALLEL_WORKSHARE:
8820 case EXEC_OMP_SECTIONS:
8821 case EXEC_OMP_SINGLE:
8822 case EXEC_OMP_TASK:
8823 case EXEC_OMP_TASKWAIT:
8824 case EXEC_OMP_WORKSHARE:
8825 break;
8826
8827 default:
8828 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8829 }
8830
8831 resolve_code (b->next, ns);
8832 }
8833 }
8834
8835
8836 /* Does everything to resolve an ordinary assignment. Returns true
8837 if this is an interface assignment. */
8838 static bool
8839 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8840 {
8841 bool rval = false;
8842 gfc_expr *lhs;
8843 gfc_expr *rhs;
8844 int llen = 0;
8845 int rlen = 0;
8846 int n;
8847 gfc_ref *ref;
8848
8849 if (gfc_extend_assign (code, ns) == SUCCESS)
8850 {
8851 gfc_expr** rhsptr;
8852
8853 if (code->op == EXEC_ASSIGN_CALL)
8854 {
8855 lhs = code->ext.actual->expr;
8856 rhsptr = &code->ext.actual->next->expr;
8857 }
8858 else
8859 {
8860 gfc_actual_arglist* args;
8861 gfc_typebound_proc* tbp;
8862
8863 gcc_assert (code->op == EXEC_COMPCALL);
8864
8865 args = code->expr1->value.compcall.actual;
8866 lhs = args->expr;
8867 rhsptr = &args->next->expr;
8868
8869 tbp = code->expr1->value.compcall.tbp;
8870 gcc_assert (!tbp->is_generic);
8871 }
8872
8873 /* Make a temporary rhs when there is a default initializer
8874 and rhs is the same symbol as the lhs. */
8875 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8876 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8877 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8878 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8879 *rhsptr = gfc_get_parentheses (*rhsptr);
8880
8881 return true;
8882 }
8883
8884 lhs = code->expr1;
8885 rhs = code->expr2;
8886
8887 if (rhs->is_boz
8888 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8889 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8890 &code->loc) == FAILURE)
8891 return false;
8892
8893 /* Handle the case of a BOZ literal on the RHS. */
8894 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8895 {
8896 int rc;
8897 if (gfc_option.warn_surprising)
8898 gfc_warning ("BOZ literal at %L is bitwise transferred "
8899 "non-integer symbol '%s'", &code->loc,
8900 lhs->symtree->n.sym->name);
8901
8902 if (!gfc_convert_boz (rhs, &lhs->ts))
8903 return false;
8904 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8905 {
8906 if (rc == ARITH_UNDERFLOW)
8907 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8908 ". This check can be disabled with the option "
8909 "-fno-range-check", &rhs->where);
8910 else if (rc == ARITH_OVERFLOW)
8911 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8912 ". This check can be disabled with the option "
8913 "-fno-range-check", &rhs->where);
8914 else if (rc == ARITH_NAN)
8915 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8916 ". This check can be disabled with the option "
8917 "-fno-range-check", &rhs->where);
8918 return false;
8919 }
8920 }
8921
8922 if (lhs->ts.type == BT_CHARACTER
8923 && gfc_option.warn_character_truncation)
8924 {
8925 if (lhs->ts.u.cl != NULL
8926 && lhs->ts.u.cl->length != NULL
8927 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8928 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8929
8930 if (rhs->expr_type == EXPR_CONSTANT)
8931 rlen = rhs->value.character.length;
8932
8933 else if (rhs->ts.u.cl != NULL
8934 && rhs->ts.u.cl->length != NULL
8935 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8936 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8937
8938 if (rlen && llen && rlen > llen)
8939 gfc_warning_now ("CHARACTER expression will be truncated "
8940 "in assignment (%d/%d) at %L",
8941 llen, rlen, &code->loc);
8942 }
8943
8944 /* Ensure that a vector index expression for the lvalue is evaluated
8945 to a temporary if the lvalue symbol is referenced in it. */
8946 if (lhs->rank)
8947 {
8948 for (ref = lhs->ref; ref; ref= ref->next)
8949 if (ref->type == REF_ARRAY)
8950 {
8951 for (n = 0; n < ref->u.ar.dimen; n++)
8952 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8953 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8954 ref->u.ar.start[n]))
8955 ref->u.ar.start[n]
8956 = gfc_get_parentheses (ref->u.ar.start[n]);
8957 }
8958 }
8959
8960 if (gfc_pure (NULL))
8961 {
8962 if (lhs->ts.type == BT_DERIVED
8963 && lhs->expr_type == EXPR_VARIABLE
8964 && lhs->ts.u.derived->attr.pointer_comp
8965 && rhs->expr_type == EXPR_VARIABLE
8966 && (gfc_impure_variable (rhs->symtree->n.sym)
8967 || gfc_is_coindexed (rhs)))
8968 {
8969 /* F2008, C1283. */
8970 if (gfc_is_coindexed (rhs))
8971 gfc_error ("Coindexed expression at %L is assigned to "
8972 "a derived type variable with a POINTER "
8973 "component in a PURE procedure",
8974 &rhs->where);
8975 else
8976 gfc_error ("The impure variable at %L is assigned to "
8977 "a derived type variable with a POINTER "
8978 "component in a PURE procedure (12.6)",
8979 &rhs->where);
8980 return rval;
8981 }
8982
8983 /* Fortran 2008, C1283. */
8984 if (gfc_is_coindexed (lhs))
8985 {
8986 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8987 "procedure", &rhs->where);
8988 return rval;
8989 }
8990 }
8991
8992 if (gfc_implicit_pure (NULL))
8993 {
8994 if (lhs->expr_type == EXPR_VARIABLE
8995 && lhs->symtree->n.sym != gfc_current_ns->proc_name
8996 && lhs->symtree->n.sym->ns != gfc_current_ns)
8997 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8998
8999 if (lhs->ts.type == BT_DERIVED
9000 && lhs->expr_type == EXPR_VARIABLE
9001 && lhs->ts.u.derived->attr.pointer_comp
9002 && rhs->expr_type == EXPR_VARIABLE
9003 && (gfc_impure_variable (rhs->symtree->n.sym)
9004 || gfc_is_coindexed (rhs)))
9005 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9006
9007 /* Fortran 2008, C1283. */
9008 if (gfc_is_coindexed (lhs))
9009 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9010 }
9011
9012 /* F03:7.4.1.2. */
9013 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9014 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9015 if (lhs->ts.type == BT_CLASS)
9016 {
9017 gfc_error ("Variable must not be polymorphic in assignment at %L",
9018 &lhs->where);
9019 return false;
9020 }
9021
9022 /* F2008, Section 7.2.1.2. */
9023 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9024 {
9025 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9026 "component in assignment at %L", &lhs->where);
9027 return false;
9028 }
9029
9030 gfc_check_assign (lhs, rhs, 1);
9031 return false;
9032 }
9033
9034
9035 /* Given a block of code, recursively resolve everything pointed to by this
9036 code block. */
9037
9038 static void
9039 resolve_code (gfc_code *code, gfc_namespace *ns)
9040 {
9041 int omp_workshare_save;
9042 int forall_save;
9043 code_stack frame;
9044 gfc_try t;
9045
9046 frame.prev = cs_base;
9047 frame.head = code;
9048 cs_base = &frame;
9049
9050 find_reachable_labels (code);
9051
9052 for (; code; code = code->next)
9053 {
9054 frame.current = code;
9055 forall_save = forall_flag;
9056
9057 if (code->op == EXEC_FORALL)
9058 {
9059 forall_flag = 1;
9060 gfc_resolve_forall (code, ns, forall_save);
9061 forall_flag = 2;
9062 }
9063 else if (code->block)
9064 {
9065 omp_workshare_save = -1;
9066 switch (code->op)
9067 {
9068 case EXEC_OMP_PARALLEL_WORKSHARE:
9069 omp_workshare_save = omp_workshare_flag;
9070 omp_workshare_flag = 1;
9071 gfc_resolve_omp_parallel_blocks (code, ns);
9072 break;
9073 case EXEC_OMP_PARALLEL:
9074 case EXEC_OMP_PARALLEL_DO:
9075 case EXEC_OMP_PARALLEL_SECTIONS:
9076 case EXEC_OMP_TASK:
9077 omp_workshare_save = omp_workshare_flag;
9078 omp_workshare_flag = 0;
9079 gfc_resolve_omp_parallel_blocks (code, ns);
9080 break;
9081 case EXEC_OMP_DO:
9082 gfc_resolve_omp_do_blocks (code, ns);
9083 break;
9084 case EXEC_SELECT_TYPE:
9085 /* Blocks are handled in resolve_select_type because we have
9086 to transform the SELECT TYPE into ASSOCIATE first. */
9087 break;
9088 case EXEC_OMP_WORKSHARE:
9089 omp_workshare_save = omp_workshare_flag;
9090 omp_workshare_flag = 1;
9091 /* FALLTHROUGH */
9092 default:
9093 gfc_resolve_blocks (code->block, ns);
9094 break;
9095 }
9096
9097 if (omp_workshare_save != -1)
9098 omp_workshare_flag = omp_workshare_save;
9099 }
9100
9101 t = SUCCESS;
9102 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9103 t = gfc_resolve_expr (code->expr1);
9104 forall_flag = forall_save;
9105
9106 if (gfc_resolve_expr (code->expr2) == FAILURE)
9107 t = FAILURE;
9108
9109 if (code->op == EXEC_ALLOCATE
9110 && gfc_resolve_expr (code->expr3) == FAILURE)
9111 t = FAILURE;
9112
9113 switch (code->op)
9114 {
9115 case EXEC_NOP:
9116 case EXEC_END_BLOCK:
9117 case EXEC_CYCLE:
9118 case EXEC_PAUSE:
9119 case EXEC_STOP:
9120 case EXEC_ERROR_STOP:
9121 case EXEC_EXIT:
9122 case EXEC_CONTINUE:
9123 case EXEC_DT_END:
9124 case EXEC_ASSIGN_CALL:
9125 case EXEC_CRITICAL:
9126 break;
9127
9128 case EXEC_SYNC_ALL:
9129 case EXEC_SYNC_IMAGES:
9130 case EXEC_SYNC_MEMORY:
9131 resolve_sync (code);
9132 break;
9133
9134 case EXEC_LOCK:
9135 case EXEC_UNLOCK:
9136 resolve_lock_unlock (code);
9137 break;
9138
9139 case EXEC_ENTRY:
9140 /* Keep track of which entry we are up to. */
9141 current_entry_id = code->ext.entry->id;
9142 break;
9143
9144 case EXEC_WHERE:
9145 resolve_where (code, NULL);
9146 break;
9147
9148 case EXEC_GOTO:
9149 if (code->expr1 != NULL)
9150 {
9151 if (code->expr1->ts.type != BT_INTEGER)
9152 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9153 "INTEGER variable", &code->expr1->where);
9154 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9155 gfc_error ("Variable '%s' has not been assigned a target "
9156 "label at %L", code->expr1->symtree->n.sym->name,
9157 &code->expr1->where);
9158 }
9159 else
9160 resolve_branch (code->label1, code);
9161 break;
9162
9163 case EXEC_RETURN:
9164 if (code->expr1 != NULL
9165 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9166 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9167 "INTEGER return specifier", &code->expr1->where);
9168 break;
9169
9170 case EXEC_INIT_ASSIGN:
9171 case EXEC_END_PROCEDURE:
9172 break;
9173
9174 case EXEC_ASSIGN:
9175 if (t == FAILURE)
9176 break;
9177
9178 if (gfc_check_vardef_context (code->expr1, false, false,
9179 _("assignment")) == FAILURE)
9180 break;
9181
9182 if (resolve_ordinary_assign (code, ns))
9183 {
9184 if (code->op == EXEC_COMPCALL)
9185 goto compcall;
9186 else
9187 goto call;
9188 }
9189 break;
9190
9191 case EXEC_LABEL_ASSIGN:
9192 if (code->label1->defined == ST_LABEL_UNKNOWN)
9193 gfc_error ("Label %d referenced at %L is never defined",
9194 code->label1->value, &code->label1->where);
9195 if (t == SUCCESS
9196 && (code->expr1->expr_type != EXPR_VARIABLE
9197 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9198 || code->expr1->symtree->n.sym->ts.kind
9199 != gfc_default_integer_kind
9200 || code->expr1->symtree->n.sym->as != NULL))
9201 gfc_error ("ASSIGN statement at %L requires a scalar "
9202 "default INTEGER variable", &code->expr1->where);
9203 break;
9204
9205 case EXEC_POINTER_ASSIGN:
9206 {
9207 gfc_expr* e;
9208
9209 if (t == FAILURE)
9210 break;
9211
9212 /* This is both a variable definition and pointer assignment
9213 context, so check both of them. For rank remapping, a final
9214 array ref may be present on the LHS and fool gfc_expr_attr
9215 used in gfc_check_vardef_context. Remove it. */
9216 e = remove_last_array_ref (code->expr1);
9217 t = gfc_check_vardef_context (e, true, false,
9218 _("pointer assignment"));
9219 if (t == SUCCESS)
9220 t = gfc_check_vardef_context (e, false, false,
9221 _("pointer assignment"));
9222 gfc_free_expr (e);
9223 if (t == FAILURE)
9224 break;
9225
9226 gfc_check_pointer_assign (code->expr1, code->expr2);
9227 break;
9228 }
9229
9230 case EXEC_ARITHMETIC_IF:
9231 if (t == SUCCESS
9232 && code->expr1->ts.type != BT_INTEGER
9233 && code->expr1->ts.type != BT_REAL)
9234 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9235 "expression", &code->expr1->where);
9236
9237 resolve_branch (code->label1, code);
9238 resolve_branch (code->label2, code);
9239 resolve_branch (code->label3, code);
9240 break;
9241
9242 case EXEC_IF:
9243 if (t == SUCCESS && code->expr1 != NULL
9244 && (code->expr1->ts.type != BT_LOGICAL
9245 || code->expr1->rank != 0))
9246 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9247 &code->expr1->where);
9248 break;
9249
9250 case EXEC_CALL:
9251 call:
9252 resolve_call (code);
9253 break;
9254
9255 case EXEC_COMPCALL:
9256 compcall:
9257 resolve_typebound_subroutine (code);
9258 break;
9259
9260 case EXEC_CALL_PPC:
9261 resolve_ppc_call (code);
9262 break;
9263
9264 case EXEC_SELECT:
9265 /* Select is complicated. Also, a SELECT construct could be
9266 a transformed computed GOTO. */
9267 resolve_select (code);
9268 break;
9269
9270 case EXEC_SELECT_TYPE:
9271 resolve_select_type (code, ns);
9272 break;
9273
9274 case EXEC_BLOCK:
9275 resolve_block_construct (code);
9276 break;
9277
9278 case EXEC_DO:
9279 if (code->ext.iterator != NULL)
9280 {
9281 gfc_iterator *iter = code->ext.iterator;
9282 if (gfc_resolve_iterator (iter, true) != FAILURE)
9283 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9284 }
9285 break;
9286
9287 case EXEC_DO_WHILE:
9288 if (code->expr1 == NULL)
9289 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9290 if (t == SUCCESS
9291 && (code->expr1->rank != 0
9292 || code->expr1->ts.type != BT_LOGICAL))
9293 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9294 "a scalar LOGICAL expression", &code->expr1->where);
9295 break;
9296
9297 case EXEC_ALLOCATE:
9298 if (t == SUCCESS)
9299 resolve_allocate_deallocate (code, "ALLOCATE");
9300
9301 break;
9302
9303 case EXEC_DEALLOCATE:
9304 if (t == SUCCESS)
9305 resolve_allocate_deallocate (code, "DEALLOCATE");
9306
9307 break;
9308
9309 case EXEC_OPEN:
9310 if (gfc_resolve_open (code->ext.open) == FAILURE)
9311 break;
9312
9313 resolve_branch (code->ext.open->err, code);
9314 break;
9315
9316 case EXEC_CLOSE:
9317 if (gfc_resolve_close (code->ext.close) == FAILURE)
9318 break;
9319
9320 resolve_branch (code->ext.close->err, code);
9321 break;
9322
9323 case EXEC_BACKSPACE:
9324 case EXEC_ENDFILE:
9325 case EXEC_REWIND:
9326 case EXEC_FLUSH:
9327 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9328 break;
9329
9330 resolve_branch (code->ext.filepos->err, code);
9331 break;
9332
9333 case EXEC_INQUIRE:
9334 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9335 break;
9336
9337 resolve_branch (code->ext.inquire->err, code);
9338 break;
9339
9340 case EXEC_IOLENGTH:
9341 gcc_assert (code->ext.inquire != NULL);
9342 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9343 break;
9344
9345 resolve_branch (code->ext.inquire->err, code);
9346 break;
9347
9348 case EXEC_WAIT:
9349 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9350 break;
9351
9352 resolve_branch (code->ext.wait->err, code);
9353 resolve_branch (code->ext.wait->end, code);
9354 resolve_branch (code->ext.wait->eor, code);
9355 break;
9356
9357 case EXEC_READ:
9358 case EXEC_WRITE:
9359 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9360 break;
9361
9362 resolve_branch (code->ext.dt->err, code);
9363 resolve_branch (code->ext.dt->end, code);
9364 resolve_branch (code->ext.dt->eor, code);
9365 break;
9366
9367 case EXEC_TRANSFER:
9368 resolve_transfer (code);
9369 break;
9370
9371 case EXEC_FORALL:
9372 resolve_forall_iterators (code->ext.forall_iterator);
9373
9374 if (code->expr1 != NULL
9375 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9376 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9377 "expression", &code->expr1->where);
9378 break;
9379
9380 case EXEC_OMP_ATOMIC:
9381 case EXEC_OMP_BARRIER:
9382 case EXEC_OMP_CRITICAL:
9383 case EXEC_OMP_FLUSH:
9384 case EXEC_OMP_DO:
9385 case EXEC_OMP_MASTER:
9386 case EXEC_OMP_ORDERED:
9387 case EXEC_OMP_SECTIONS:
9388 case EXEC_OMP_SINGLE:
9389 case EXEC_OMP_TASKWAIT:
9390 case EXEC_OMP_WORKSHARE:
9391 gfc_resolve_omp_directive (code, ns);
9392 break;
9393
9394 case EXEC_OMP_PARALLEL:
9395 case EXEC_OMP_PARALLEL_DO:
9396 case EXEC_OMP_PARALLEL_SECTIONS:
9397 case EXEC_OMP_PARALLEL_WORKSHARE:
9398 case EXEC_OMP_TASK:
9399 omp_workshare_save = omp_workshare_flag;
9400 omp_workshare_flag = 0;
9401 gfc_resolve_omp_directive (code, ns);
9402 omp_workshare_flag = omp_workshare_save;
9403 break;
9404
9405 default:
9406 gfc_internal_error ("resolve_code(): Bad statement code");
9407 }
9408 }
9409
9410 cs_base = frame.prev;
9411 }
9412
9413
9414 /* Resolve initial values and make sure they are compatible with
9415 the variable. */
9416
9417 static void
9418 resolve_values (gfc_symbol *sym)
9419 {
9420 gfc_try t;
9421
9422 if (sym->value == NULL)
9423 return;
9424
9425 if (sym->value->expr_type == EXPR_STRUCTURE)
9426 t= resolve_structure_cons (sym->value, 1);
9427 else
9428 t = gfc_resolve_expr (sym->value);
9429
9430 if (t == FAILURE)
9431 return;
9432
9433 gfc_check_assign_symbol (sym, sym->value);
9434 }
9435
9436
9437 /* Verify the binding labels for common blocks that are BIND(C). The label
9438 for a BIND(C) common block must be identical in all scoping units in which
9439 the common block is declared. Further, the binding label can not collide
9440 with any other global entity in the program. */
9441
9442 static void
9443 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9444 {
9445 if (comm_block_tree->n.common->is_bind_c == 1)
9446 {
9447 gfc_gsymbol *binding_label_gsym;
9448 gfc_gsymbol *comm_name_gsym;
9449
9450 /* See if a global symbol exists by the common block's name. It may
9451 be NULL if the common block is use-associated. */
9452 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9453 comm_block_tree->n.common->name);
9454 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9455 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9456 "with the global entity '%s' at %L",
9457 comm_block_tree->n.common->binding_label,
9458 comm_block_tree->n.common->name,
9459 &(comm_block_tree->n.common->where),
9460 comm_name_gsym->name, &(comm_name_gsym->where));
9461 else if (comm_name_gsym != NULL
9462 && strcmp (comm_name_gsym->name,
9463 comm_block_tree->n.common->name) == 0)
9464 {
9465 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9466 as expected. */
9467 if (comm_name_gsym->binding_label == NULL)
9468 /* No binding label for common block stored yet; save this one. */
9469 comm_name_gsym->binding_label =
9470 comm_block_tree->n.common->binding_label;
9471 else
9472 if (strcmp (comm_name_gsym->binding_label,
9473 comm_block_tree->n.common->binding_label) != 0)
9474 {
9475 /* Common block names match but binding labels do not. */
9476 gfc_error ("Binding label '%s' for common block '%s' at %L "
9477 "does not match the binding label '%s' for common "
9478 "block '%s' at %L",
9479 comm_block_tree->n.common->binding_label,
9480 comm_block_tree->n.common->name,
9481 &(comm_block_tree->n.common->where),
9482 comm_name_gsym->binding_label,
9483 comm_name_gsym->name,
9484 &(comm_name_gsym->where));
9485 return;
9486 }
9487 }
9488
9489 /* There is no binding label (NAME="") so we have nothing further to
9490 check and nothing to add as a global symbol for the label. */
9491 if (comm_block_tree->n.common->binding_label[0] == '\0' )
9492 return;
9493
9494 binding_label_gsym =
9495 gfc_find_gsymbol (gfc_gsym_root,
9496 comm_block_tree->n.common->binding_label);
9497 if (binding_label_gsym == NULL)
9498 {
9499 /* Need to make a global symbol for the binding label to prevent
9500 it from colliding with another. */
9501 binding_label_gsym =
9502 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9503 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9504 binding_label_gsym->type = GSYM_COMMON;
9505 }
9506 else
9507 {
9508 /* If comm_name_gsym is NULL, the name common block is use
9509 associated and the name could be colliding. */
9510 if (binding_label_gsym->type != GSYM_COMMON)
9511 gfc_error ("Binding label '%s' for common block '%s' at %L "
9512 "collides with the global entity '%s' at %L",
9513 comm_block_tree->n.common->binding_label,
9514 comm_block_tree->n.common->name,
9515 &(comm_block_tree->n.common->where),
9516 binding_label_gsym->name,
9517 &(binding_label_gsym->where));
9518 else if (comm_name_gsym != NULL
9519 && (strcmp (binding_label_gsym->name,
9520 comm_name_gsym->binding_label) != 0)
9521 && (strcmp (binding_label_gsym->sym_name,
9522 comm_name_gsym->name) != 0))
9523 gfc_error ("Binding label '%s' for common block '%s' at %L "
9524 "collides with global entity '%s' at %L",
9525 binding_label_gsym->name, binding_label_gsym->sym_name,
9526 &(comm_block_tree->n.common->where),
9527 comm_name_gsym->name, &(comm_name_gsym->where));
9528 }
9529 }
9530
9531 return;
9532 }
9533
9534
9535 /* Verify any BIND(C) derived types in the namespace so we can report errors
9536 for them once, rather than for each variable declared of that type. */
9537
9538 static void
9539 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9540 {
9541 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9542 && derived_sym->attr.is_bind_c == 1)
9543 verify_bind_c_derived_type (derived_sym);
9544
9545 return;
9546 }
9547
9548
9549 /* Verify that any binding labels used in a given namespace do not collide
9550 with the names or binding labels of any global symbols. */
9551
9552 static void
9553 gfc_verify_binding_labels (gfc_symbol *sym)
9554 {
9555 int has_error = 0;
9556
9557 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9558 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
9559 {
9560 gfc_gsymbol *bind_c_sym;
9561
9562 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9563 if (bind_c_sym != NULL
9564 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9565 {
9566 if (sym->attr.if_source == IFSRC_DECL
9567 && (bind_c_sym->type != GSYM_SUBROUTINE
9568 && bind_c_sym->type != GSYM_FUNCTION)
9569 && ((sym->attr.contained == 1
9570 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9571 || (sym->attr.use_assoc == 1
9572 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9573 {
9574 /* Make sure global procedures don't collide with anything. */
9575 gfc_error ("Binding label '%s' at %L collides with the global "
9576 "entity '%s' at %L", sym->binding_label,
9577 &(sym->declared_at), bind_c_sym->name,
9578 &(bind_c_sym->where));
9579 has_error = 1;
9580 }
9581 else if (sym->attr.contained == 0
9582 && (sym->attr.if_source == IFSRC_IFBODY
9583 && sym->attr.flavor == FL_PROCEDURE)
9584 && (bind_c_sym->sym_name != NULL
9585 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9586 {
9587 /* Make sure procedures in interface bodies don't collide. */
9588 gfc_error ("Binding label '%s' in interface body at %L collides "
9589 "with the global entity '%s' at %L",
9590 sym->binding_label,
9591 &(sym->declared_at), bind_c_sym->name,
9592 &(bind_c_sym->where));
9593 has_error = 1;
9594 }
9595 else if (sym->attr.contained == 0
9596 && sym->attr.if_source == IFSRC_UNKNOWN)
9597 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9598 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9599 || sym->attr.use_assoc == 0)
9600 {
9601 gfc_error ("Binding label '%s' at %L collides with global "
9602 "entity '%s' at %L", sym->binding_label,
9603 &(sym->declared_at), bind_c_sym->name,
9604 &(bind_c_sym->where));
9605 has_error = 1;
9606 }
9607
9608 if (has_error != 0)
9609 /* Clear the binding label to prevent checking multiple times. */
9610 sym->binding_label[0] = '\0';
9611 }
9612 else if (bind_c_sym == NULL)
9613 {
9614 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9615 bind_c_sym->where = sym->declared_at;
9616 bind_c_sym->sym_name = sym->name;
9617
9618 if (sym->attr.use_assoc == 1)
9619 bind_c_sym->mod_name = sym->module;
9620 else
9621 if (sym->ns->proc_name != NULL)
9622 bind_c_sym->mod_name = sym->ns->proc_name->name;
9623
9624 if (sym->attr.contained == 0)
9625 {
9626 if (sym->attr.subroutine)
9627 bind_c_sym->type = GSYM_SUBROUTINE;
9628 else if (sym->attr.function)
9629 bind_c_sym->type = GSYM_FUNCTION;
9630 }
9631 }
9632 }
9633 return;
9634 }
9635
9636
9637 /* Resolve an index expression. */
9638
9639 static gfc_try
9640 resolve_index_expr (gfc_expr *e)
9641 {
9642 if (gfc_resolve_expr (e) == FAILURE)
9643 return FAILURE;
9644
9645 if (gfc_simplify_expr (e, 0) == FAILURE)
9646 return FAILURE;
9647
9648 if (gfc_specification_expr (e) == FAILURE)
9649 return FAILURE;
9650
9651 return SUCCESS;
9652 }
9653
9654
9655 /* Resolve a charlen structure. */
9656
9657 static gfc_try
9658 resolve_charlen (gfc_charlen *cl)
9659 {
9660 int i, k;
9661
9662 if (cl->resolved)
9663 return SUCCESS;
9664
9665 cl->resolved = 1;
9666
9667 specification_expr = 1;
9668
9669 if (resolve_index_expr (cl->length) == FAILURE)
9670 {
9671 specification_expr = 0;
9672 return FAILURE;
9673 }
9674
9675 /* "If the character length parameter value evaluates to a negative
9676 value, the length of character entities declared is zero." */
9677 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9678 {
9679 if (gfc_option.warn_surprising)
9680 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9681 " the length has been set to zero",
9682 &cl->length->where, i);
9683 gfc_replace_expr (cl->length,
9684 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9685 }
9686
9687 /* Check that the character length is not too large. */
9688 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9689 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9690 && cl->length->ts.type == BT_INTEGER
9691 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9692 {
9693 gfc_error ("String length at %L is too large", &cl->length->where);
9694 return FAILURE;
9695 }
9696
9697 return SUCCESS;
9698 }
9699
9700
9701 /* Test for non-constant shape arrays. */
9702
9703 static bool
9704 is_non_constant_shape_array (gfc_symbol *sym)
9705 {
9706 gfc_expr *e;
9707 int i;
9708 bool not_constant;
9709
9710 not_constant = false;
9711 if (sym->as != NULL)
9712 {
9713 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9714 has not been simplified; parameter array references. Do the
9715 simplification now. */
9716 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9717 {
9718 e = sym->as->lower[i];
9719 if (e && (resolve_index_expr (e) == FAILURE
9720 || !gfc_is_constant_expr (e)))
9721 not_constant = true;
9722 e = sym->as->upper[i];
9723 if (e && (resolve_index_expr (e) == FAILURE
9724 || !gfc_is_constant_expr (e)))
9725 not_constant = true;
9726 }
9727 }
9728 return not_constant;
9729 }
9730
9731 /* Given a symbol and an initialization expression, add code to initialize
9732 the symbol to the function entry. */
9733 static void
9734 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9735 {
9736 gfc_expr *lval;
9737 gfc_code *init_st;
9738 gfc_namespace *ns = sym->ns;
9739
9740 /* Search for the function namespace if this is a contained
9741 function without an explicit result. */
9742 if (sym->attr.function && sym == sym->result
9743 && sym->name != sym->ns->proc_name->name)
9744 {
9745 ns = ns->contained;
9746 for (;ns; ns = ns->sibling)
9747 if (strcmp (ns->proc_name->name, sym->name) == 0)
9748 break;
9749 }
9750
9751 if (ns == NULL)
9752 {
9753 gfc_free_expr (init);
9754 return;
9755 }
9756
9757 /* Build an l-value expression for the result. */
9758 lval = gfc_lval_expr_from_sym (sym);
9759
9760 /* Add the code at scope entry. */
9761 init_st = gfc_get_code ();
9762 init_st->next = ns->code;
9763 ns->code = init_st;
9764
9765 /* Assign the default initializer to the l-value. */
9766 init_st->loc = sym->declared_at;
9767 init_st->op = EXEC_INIT_ASSIGN;
9768 init_st->expr1 = lval;
9769 init_st->expr2 = init;
9770 }
9771
9772 /* Assign the default initializer to a derived type variable or result. */
9773
9774 static void
9775 apply_default_init (gfc_symbol *sym)
9776 {
9777 gfc_expr *init = NULL;
9778
9779 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9780 return;
9781
9782 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9783 init = gfc_default_initializer (&sym->ts);
9784
9785 if (init == NULL && sym->ts.type != BT_CLASS)
9786 return;
9787
9788 build_init_assign (sym, init);
9789 sym->attr.referenced = 1;
9790 }
9791
9792 /* Build an initializer for a local integer, real, complex, logical, or
9793 character variable, based on the command line flags finit-local-zero,
9794 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9795 null if the symbol should not have a default initialization. */
9796 static gfc_expr *
9797 build_default_init_expr (gfc_symbol *sym)
9798 {
9799 int char_len;
9800 gfc_expr *init_expr;
9801 int i;
9802
9803 /* These symbols should never have a default initialization. */
9804 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9805 || sym->attr.external
9806 || sym->attr.dummy
9807 || sym->attr.pointer
9808 || sym->attr.in_equivalence
9809 || sym->attr.in_common
9810 || sym->attr.data
9811 || sym->module
9812 || sym->attr.cray_pointee
9813 || sym->attr.cray_pointer)
9814 return NULL;
9815
9816 /* Now we'll try to build an initializer expression. */
9817 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9818 &sym->declared_at);
9819
9820 /* We will only initialize integers, reals, complex, logicals, and
9821 characters, and only if the corresponding command-line flags
9822 were set. Otherwise, we free init_expr and return null. */
9823 switch (sym->ts.type)
9824 {
9825 case BT_INTEGER:
9826 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9827 mpz_set_si (init_expr->value.integer,
9828 gfc_option.flag_init_integer_value);
9829 else
9830 {
9831 gfc_free_expr (init_expr);
9832 init_expr = NULL;
9833 }
9834 break;
9835
9836 case BT_REAL:
9837 switch (gfc_option.flag_init_real)
9838 {
9839 case GFC_INIT_REAL_SNAN:
9840 init_expr->is_snan = 1;
9841 /* Fall through. */
9842 case GFC_INIT_REAL_NAN:
9843 mpfr_set_nan (init_expr->value.real);
9844 break;
9845
9846 case GFC_INIT_REAL_INF:
9847 mpfr_set_inf (init_expr->value.real, 1);
9848 break;
9849
9850 case GFC_INIT_REAL_NEG_INF:
9851 mpfr_set_inf (init_expr->value.real, -1);
9852 break;
9853
9854 case GFC_INIT_REAL_ZERO:
9855 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9856 break;
9857
9858 default:
9859 gfc_free_expr (init_expr);
9860 init_expr = NULL;
9861 break;
9862 }
9863 break;
9864
9865 case BT_COMPLEX:
9866 switch (gfc_option.flag_init_real)
9867 {
9868 case GFC_INIT_REAL_SNAN:
9869 init_expr->is_snan = 1;
9870 /* Fall through. */
9871 case GFC_INIT_REAL_NAN:
9872 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9873 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9874 break;
9875
9876 case GFC_INIT_REAL_INF:
9877 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9878 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9879 break;
9880
9881 case GFC_INIT_REAL_NEG_INF:
9882 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9883 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9884 break;
9885
9886 case GFC_INIT_REAL_ZERO:
9887 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9888 break;
9889
9890 default:
9891 gfc_free_expr (init_expr);
9892 init_expr = NULL;
9893 break;
9894 }
9895 break;
9896
9897 case BT_LOGICAL:
9898 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9899 init_expr->value.logical = 0;
9900 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9901 init_expr->value.logical = 1;
9902 else
9903 {
9904 gfc_free_expr (init_expr);
9905 init_expr = NULL;
9906 }
9907 break;
9908
9909 case BT_CHARACTER:
9910 /* For characters, the length must be constant in order to
9911 create a default initializer. */
9912 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9913 && sym->ts.u.cl->length
9914 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9915 {
9916 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9917 init_expr->value.character.length = char_len;
9918 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9919 for (i = 0; i < char_len; i++)
9920 init_expr->value.character.string[i]
9921 = (unsigned char) gfc_option.flag_init_character_value;
9922 }
9923 else
9924 {
9925 gfc_free_expr (init_expr);
9926 init_expr = NULL;
9927 }
9928 break;
9929
9930 default:
9931 gfc_free_expr (init_expr);
9932 init_expr = NULL;
9933 }
9934 return init_expr;
9935 }
9936
9937 /* Add an initialization expression to a local variable. */
9938 static void
9939 apply_default_init_local (gfc_symbol *sym)
9940 {
9941 gfc_expr *init = NULL;
9942
9943 /* The symbol should be a variable or a function return value. */
9944 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9945 || (sym->attr.function && sym->result != sym))
9946 return;
9947
9948 /* Try to build the initializer expression. If we can't initialize
9949 this symbol, then init will be NULL. */
9950 init = build_default_init_expr (sym);
9951 if (init == NULL)
9952 return;
9953
9954 /* For saved variables, we don't want to add an initializer at
9955 function entry, so we just add a static initializer. */
9956 if (sym->attr.save || sym->ns->save_all
9957 || gfc_option.flag_max_stack_var_size == 0)
9958 {
9959 /* Don't clobber an existing initializer! */
9960 gcc_assert (sym->value == NULL);
9961 sym->value = init;
9962 return;
9963 }
9964
9965 build_init_assign (sym, init);
9966 }
9967
9968
9969 /* Resolution of common features of flavors variable and procedure. */
9970
9971 static gfc_try
9972 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9973 {
9974 /* Avoid double diagnostics for function result symbols. */
9975 if ((sym->result || sym->attr.result) && !sym->attr.dummy
9976 && (sym->ns != gfc_current_ns))
9977 return SUCCESS;
9978
9979 /* Constraints on deferred shape variable. */
9980 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9981 {
9982 if (sym->attr.allocatable)
9983 {
9984 if (sym->attr.dimension)
9985 {
9986 gfc_error ("Allocatable array '%s' at %L must have "
9987 "a deferred shape", sym->name, &sym->declared_at);
9988 return FAILURE;
9989 }
9990 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9991 "may not be ALLOCATABLE", sym->name,
9992 &sym->declared_at) == FAILURE)
9993 return FAILURE;
9994 }
9995
9996 if (sym->attr.pointer && sym->attr.dimension)
9997 {
9998 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9999 sym->name, &sym->declared_at);
10000 return FAILURE;
10001 }
10002 }
10003 else
10004 {
10005 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10006 && sym->ts.type != BT_CLASS && !sym->assoc)
10007 {
10008 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10009 sym->name, &sym->declared_at);
10010 return FAILURE;
10011 }
10012 }
10013
10014 /* Constraints on polymorphic variables. */
10015 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10016 {
10017 /* F03:C502. */
10018 if (sym->attr.class_ok
10019 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10020 {
10021 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10022 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10023 &sym->declared_at);
10024 return FAILURE;
10025 }
10026
10027 /* F03:C509. */
10028 /* Assume that use associated symbols were checked in the module ns.
10029 Class-variables that are associate-names are also something special
10030 and excepted from the test. */
10031 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10032 {
10033 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10034 "or pointer", sym->name, &sym->declared_at);
10035 return FAILURE;
10036 }
10037 }
10038
10039 return SUCCESS;
10040 }
10041
10042
10043 /* Additional checks for symbols with flavor variable and derived
10044 type. To be called from resolve_fl_variable. */
10045
10046 static gfc_try
10047 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10048 {
10049 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10050
10051 /* Check to see if a derived type is blocked from being host
10052 associated by the presence of another class I symbol in the same
10053 namespace. 14.6.1.3 of the standard and the discussion on
10054 comp.lang.fortran. */
10055 if (sym->ns != sym->ts.u.derived->ns
10056 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10057 {
10058 gfc_symbol *s;
10059 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10060 if (s && s->attr.flavor != FL_DERIVED)
10061 {
10062 gfc_error ("The type '%s' cannot be host associated at %L "
10063 "because it is blocked by an incompatible object "
10064 "of the same name declared at %L",
10065 sym->ts.u.derived->name, &sym->declared_at,
10066 &s->declared_at);
10067 return FAILURE;
10068 }
10069 }
10070
10071 /* 4th constraint in section 11.3: "If an object of a type for which
10072 component-initialization is specified (R429) appears in the
10073 specification-part of a module and does not have the ALLOCATABLE
10074 or POINTER attribute, the object shall have the SAVE attribute."
10075
10076 The check for initializers is performed with
10077 gfc_has_default_initializer because gfc_default_initializer generates
10078 a hidden default for allocatable components. */
10079 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10080 && sym->ns->proc_name->attr.flavor == FL_MODULE
10081 && !sym->ns->save_all && !sym->attr.save
10082 && !sym->attr.pointer && !sym->attr.allocatable
10083 && gfc_has_default_initializer (sym->ts.u.derived)
10084 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
10085 "module variable '%s' at %L, needed due to "
10086 "the default initialization", sym->name,
10087 &sym->declared_at) == FAILURE)
10088 return FAILURE;
10089
10090 /* Assign default initializer. */
10091 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10092 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10093 {
10094 sym->value = gfc_default_initializer (&sym->ts);
10095 }
10096
10097 return SUCCESS;
10098 }
10099
10100
10101 /* Resolve symbols with flavor variable. */
10102
10103 static gfc_try
10104 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10105 {
10106 int no_init_flag, automatic_flag;
10107 gfc_expr *e;
10108 const char *auto_save_msg;
10109
10110 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10111 "SAVE attribute";
10112
10113 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10114 return FAILURE;
10115
10116 /* Set this flag to check that variables are parameters of all entries.
10117 This check is effected by the call to gfc_resolve_expr through
10118 is_non_constant_shape_array. */
10119 specification_expr = 1;
10120
10121 if (sym->ns->proc_name
10122 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10123 || sym->ns->proc_name->attr.is_main_program)
10124 && !sym->attr.use_assoc
10125 && !sym->attr.allocatable
10126 && !sym->attr.pointer
10127 && is_non_constant_shape_array (sym))
10128 {
10129 /* The shape of a main program or module array needs to be
10130 constant. */
10131 gfc_error ("The module or main program array '%s' at %L must "
10132 "have constant shape", sym->name, &sym->declared_at);
10133 specification_expr = 0;
10134 return FAILURE;
10135 }
10136
10137 /* Constraints on deferred type parameter. */
10138 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10139 {
10140 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10141 "requires either the pointer or allocatable attribute",
10142 sym->name, &sym->declared_at);
10143 return FAILURE;
10144 }
10145
10146 if (sym->ts.type == BT_CHARACTER)
10147 {
10148 /* Make sure that character string variables with assumed length are
10149 dummy arguments. */
10150 e = sym->ts.u.cl->length;
10151 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10152 && !sym->ts.deferred)
10153 {
10154 gfc_error ("Entity with assumed character length at %L must be a "
10155 "dummy argument or a PARAMETER", &sym->declared_at);
10156 return FAILURE;
10157 }
10158
10159 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10160 {
10161 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10162 return FAILURE;
10163 }
10164
10165 if (!gfc_is_constant_expr (e)
10166 && !(e->expr_type == EXPR_VARIABLE
10167 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
10168 && sym->ns->proc_name
10169 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10170 || sym->ns->proc_name->attr.is_main_program)
10171 && !sym->attr.use_assoc)
10172 {
10173 gfc_error ("'%s' at %L must have constant character length "
10174 "in this context", sym->name, &sym->declared_at);
10175 return FAILURE;
10176 }
10177 }
10178
10179 if (sym->value == NULL && sym->attr.referenced)
10180 apply_default_init_local (sym); /* Try to apply a default initialization. */
10181
10182 /* Determine if the symbol may not have an initializer. */
10183 no_init_flag = automatic_flag = 0;
10184 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10185 || sym->attr.intrinsic || sym->attr.result)
10186 no_init_flag = 1;
10187 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10188 && is_non_constant_shape_array (sym))
10189 {
10190 no_init_flag = automatic_flag = 1;
10191
10192 /* Also, they must not have the SAVE attribute.
10193 SAVE_IMPLICIT is checked below. */
10194 if (sym->as && sym->attr.codimension)
10195 {
10196 int corank = sym->as->corank;
10197 sym->as->corank = 0;
10198 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10199 sym->as->corank = corank;
10200 }
10201 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10202 {
10203 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10204 return FAILURE;
10205 }
10206 }
10207
10208 /* Ensure that any initializer is simplified. */
10209 if (sym->value)
10210 gfc_simplify_expr (sym->value, 1);
10211
10212 /* Reject illegal initializers. */
10213 if (!sym->mark && sym->value)
10214 {
10215 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10216 && CLASS_DATA (sym)->attr.allocatable))
10217 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10218 sym->name, &sym->declared_at);
10219 else if (sym->attr.external)
10220 gfc_error ("External '%s' at %L cannot have an initializer",
10221 sym->name, &sym->declared_at);
10222 else if (sym->attr.dummy
10223 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10224 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10225 sym->name, &sym->declared_at);
10226 else if (sym->attr.intrinsic)
10227 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10228 sym->name, &sym->declared_at);
10229 else if (sym->attr.result)
10230 gfc_error ("Function result '%s' at %L cannot have an initializer",
10231 sym->name, &sym->declared_at);
10232 else if (automatic_flag)
10233 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10234 sym->name, &sym->declared_at);
10235 else
10236 goto no_init_error;
10237 return FAILURE;
10238 }
10239
10240 no_init_error:
10241 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10242 return resolve_fl_variable_derived (sym, no_init_flag);
10243
10244 return SUCCESS;
10245 }
10246
10247
10248 /* Resolve a procedure. */
10249
10250 static gfc_try
10251 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10252 {
10253 gfc_formal_arglist *arg;
10254
10255 if (sym->attr.function
10256 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10257 return FAILURE;
10258
10259 if (sym->ts.type == BT_CHARACTER)
10260 {
10261 gfc_charlen *cl = sym->ts.u.cl;
10262
10263 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10264 && resolve_charlen (cl) == FAILURE)
10265 return FAILURE;
10266
10267 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10268 && sym->attr.proc == PROC_ST_FUNCTION)
10269 {
10270 gfc_error ("Character-valued statement function '%s' at %L must "
10271 "have constant length", sym->name, &sym->declared_at);
10272 return FAILURE;
10273 }
10274 }
10275
10276 /* Ensure that derived type for are not of a private type. Internal
10277 module procedures are excluded by 2.2.3.3 - i.e., they are not
10278 externally accessible and can access all the objects accessible in
10279 the host. */
10280 if (!(sym->ns->parent
10281 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10282 && gfc_check_symbol_access (sym))
10283 {
10284 gfc_interface *iface;
10285
10286 for (arg = sym->formal; arg; arg = arg->next)
10287 {
10288 if (arg->sym
10289 && arg->sym->ts.type == BT_DERIVED
10290 && !arg->sym->ts.u.derived->attr.use_assoc
10291 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10292 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
10293 "PRIVATE type and cannot be a dummy argument"
10294 " of '%s', which is PUBLIC at %L",
10295 arg->sym->name, sym->name, &sym->declared_at)
10296 == FAILURE)
10297 {
10298 /* Stop this message from recurring. */
10299 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10300 return FAILURE;
10301 }
10302 }
10303
10304 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10305 PRIVATE to the containing module. */
10306 for (iface = sym->generic; iface; iface = iface->next)
10307 {
10308 for (arg = iface->sym->formal; arg; arg = arg->next)
10309 {
10310 if (arg->sym
10311 && arg->sym->ts.type == BT_DERIVED
10312 && !arg->sym->ts.u.derived->attr.use_assoc
10313 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10314 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10315 "'%s' in PUBLIC interface '%s' at %L "
10316 "takes dummy arguments of '%s' which is "
10317 "PRIVATE", iface->sym->name, sym->name,
10318 &iface->sym->declared_at,
10319 gfc_typename (&arg->sym->ts)) == FAILURE)
10320 {
10321 /* Stop this message from recurring. */
10322 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10323 return FAILURE;
10324 }
10325 }
10326 }
10327
10328 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10329 PRIVATE to the containing module. */
10330 for (iface = sym->generic; iface; iface = iface->next)
10331 {
10332 for (arg = iface->sym->formal; arg; arg = arg->next)
10333 {
10334 if (arg->sym
10335 && arg->sym->ts.type == BT_DERIVED
10336 && !arg->sym->ts.u.derived->attr.use_assoc
10337 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10338 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10339 "'%s' in PUBLIC interface '%s' at %L "
10340 "takes dummy arguments of '%s' which is "
10341 "PRIVATE", iface->sym->name, sym->name,
10342 &iface->sym->declared_at,
10343 gfc_typename (&arg->sym->ts)) == FAILURE)
10344 {
10345 /* Stop this message from recurring. */
10346 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10347 return FAILURE;
10348 }
10349 }
10350 }
10351 }
10352
10353 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10354 && !sym->attr.proc_pointer)
10355 {
10356 gfc_error ("Function '%s' at %L cannot have an initializer",
10357 sym->name, &sym->declared_at);
10358 return FAILURE;
10359 }
10360
10361 /* An external symbol may not have an initializer because it is taken to be
10362 a procedure. Exception: Procedure Pointers. */
10363 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10364 {
10365 gfc_error ("External object '%s' at %L may not have an initializer",
10366 sym->name, &sym->declared_at);
10367 return FAILURE;
10368 }
10369
10370 /* An elemental function is required to return a scalar 12.7.1 */
10371 if (sym->attr.elemental && sym->attr.function && sym->as)
10372 {
10373 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10374 "result", sym->name, &sym->declared_at);
10375 /* Reset so that the error only occurs once. */
10376 sym->attr.elemental = 0;
10377 return FAILURE;
10378 }
10379
10380 if (sym->attr.proc == PROC_ST_FUNCTION
10381 && (sym->attr.allocatable || sym->attr.pointer))
10382 {
10383 gfc_error ("Statement function '%s' at %L may not have pointer or "
10384 "allocatable attribute", sym->name, &sym->declared_at);
10385 return FAILURE;
10386 }
10387
10388 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10389 char-len-param shall not be array-valued, pointer-valued, recursive
10390 or pure. ....snip... A character value of * may only be used in the
10391 following ways: (i) Dummy arg of procedure - dummy associates with
10392 actual length; (ii) To declare a named constant; or (iii) External
10393 function - but length must be declared in calling scoping unit. */
10394 if (sym->attr.function
10395 && sym->ts.type == BT_CHARACTER
10396 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10397 {
10398 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10399 || (sym->attr.recursive) || (sym->attr.pure))
10400 {
10401 if (sym->as && sym->as->rank)
10402 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10403 "array-valued", sym->name, &sym->declared_at);
10404
10405 if (sym->attr.pointer)
10406 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10407 "pointer-valued", sym->name, &sym->declared_at);
10408
10409 if (sym->attr.pure)
10410 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10411 "pure", sym->name, &sym->declared_at);
10412
10413 if (sym->attr.recursive)
10414 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10415 "recursive", sym->name, &sym->declared_at);
10416
10417 return FAILURE;
10418 }
10419
10420 /* Appendix B.2 of the standard. Contained functions give an
10421 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10422 character length is an F2003 feature. */
10423 if (!sym->attr.contained
10424 && gfc_current_form != FORM_FIXED
10425 && !sym->ts.deferred)
10426 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10427 "CHARACTER(*) function '%s' at %L",
10428 sym->name, &sym->declared_at);
10429 }
10430
10431 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10432 {
10433 gfc_formal_arglist *curr_arg;
10434 int has_non_interop_arg = 0;
10435
10436 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10437 sym->common_block) == FAILURE)
10438 {
10439 /* Clear these to prevent looking at them again if there was an
10440 error. */
10441 sym->attr.is_bind_c = 0;
10442 sym->attr.is_c_interop = 0;
10443 sym->ts.is_c_interop = 0;
10444 }
10445 else
10446 {
10447 /* So far, no errors have been found. */
10448 sym->attr.is_c_interop = 1;
10449 sym->ts.is_c_interop = 1;
10450 }
10451
10452 curr_arg = sym->formal;
10453 while (curr_arg != NULL)
10454 {
10455 /* Skip implicitly typed dummy args here. */
10456 if (curr_arg->sym->attr.implicit_type == 0)
10457 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
10458 /* If something is found to fail, record the fact so we
10459 can mark the symbol for the procedure as not being
10460 BIND(C) to try and prevent multiple errors being
10461 reported. */
10462 has_non_interop_arg = 1;
10463
10464 curr_arg = curr_arg->next;
10465 }
10466
10467 /* See if any of the arguments were not interoperable and if so, clear
10468 the procedure symbol to prevent duplicate error messages. */
10469 if (has_non_interop_arg != 0)
10470 {
10471 sym->attr.is_c_interop = 0;
10472 sym->ts.is_c_interop = 0;
10473 sym->attr.is_bind_c = 0;
10474 }
10475 }
10476
10477 if (!sym->attr.proc_pointer)
10478 {
10479 if (sym->attr.save == SAVE_EXPLICIT)
10480 {
10481 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10482 "in '%s' at %L", sym->name, &sym->declared_at);
10483 return FAILURE;
10484 }
10485 if (sym->attr.intent)
10486 {
10487 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10488 "in '%s' at %L", sym->name, &sym->declared_at);
10489 return FAILURE;
10490 }
10491 if (sym->attr.subroutine && sym->attr.result)
10492 {
10493 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10494 "in '%s' at %L", sym->name, &sym->declared_at);
10495 return FAILURE;
10496 }
10497 if (sym->attr.external && sym->attr.function
10498 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10499 || sym->attr.contained))
10500 {
10501 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10502 "in '%s' at %L", sym->name, &sym->declared_at);
10503 return FAILURE;
10504 }
10505 if (strcmp ("ppr@", sym->name) == 0)
10506 {
10507 gfc_error ("Procedure pointer result '%s' at %L "
10508 "is missing the pointer attribute",
10509 sym->ns->proc_name->name, &sym->declared_at);
10510 return FAILURE;
10511 }
10512 }
10513
10514 return SUCCESS;
10515 }
10516
10517
10518 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10519 been defined and we now know their defined arguments, check that they fulfill
10520 the requirements of the standard for procedures used as finalizers. */
10521
10522 static gfc_try
10523 gfc_resolve_finalizers (gfc_symbol* derived)
10524 {
10525 gfc_finalizer* list;
10526 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10527 gfc_try result = SUCCESS;
10528 bool seen_scalar = false;
10529
10530 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10531 return SUCCESS;
10532
10533 /* Walk over the list of finalizer-procedures, check them, and if any one
10534 does not fit in with the standard's definition, print an error and remove
10535 it from the list. */
10536 prev_link = &derived->f2k_derived->finalizers;
10537 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10538 {
10539 gfc_symbol* arg;
10540 gfc_finalizer* i;
10541 int my_rank;
10542
10543 /* Skip this finalizer if we already resolved it. */
10544 if (list->proc_tree)
10545 {
10546 prev_link = &(list->next);
10547 continue;
10548 }
10549
10550 /* Check this exists and is a SUBROUTINE. */
10551 if (!list->proc_sym->attr.subroutine)
10552 {
10553 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10554 list->proc_sym->name, &list->where);
10555 goto error;
10556 }
10557
10558 /* We should have exactly one argument. */
10559 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10560 {
10561 gfc_error ("FINAL procedure at %L must have exactly one argument",
10562 &list->where);
10563 goto error;
10564 }
10565 arg = list->proc_sym->formal->sym;
10566
10567 /* This argument must be of our type. */
10568 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10569 {
10570 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10571 &arg->declared_at, derived->name);
10572 goto error;
10573 }
10574
10575 /* It must neither be a pointer nor allocatable nor optional. */
10576 if (arg->attr.pointer)
10577 {
10578 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10579 &arg->declared_at);
10580 goto error;
10581 }
10582 if (arg->attr.allocatable)
10583 {
10584 gfc_error ("Argument of FINAL procedure at %L must not be"
10585 " ALLOCATABLE", &arg->declared_at);
10586 goto error;
10587 }
10588 if (arg->attr.optional)
10589 {
10590 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10591 &arg->declared_at);
10592 goto error;
10593 }
10594
10595 /* It must not be INTENT(OUT). */
10596 if (arg->attr.intent == INTENT_OUT)
10597 {
10598 gfc_error ("Argument of FINAL procedure at %L must not be"
10599 " INTENT(OUT)", &arg->declared_at);
10600 goto error;
10601 }
10602
10603 /* Warn if the procedure is non-scalar and not assumed shape. */
10604 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10605 && arg->as->type != AS_ASSUMED_SHAPE)
10606 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10607 " shape argument", &arg->declared_at);
10608
10609 /* Check that it does not match in kind and rank with a FINAL procedure
10610 defined earlier. To really loop over the *earlier* declarations,
10611 we need to walk the tail of the list as new ones were pushed at the
10612 front. */
10613 /* TODO: Handle kind parameters once they are implemented. */
10614 my_rank = (arg->as ? arg->as->rank : 0);
10615 for (i = list->next; i; i = i->next)
10616 {
10617 /* Argument list might be empty; that is an error signalled earlier,
10618 but we nevertheless continued resolving. */
10619 if (i->proc_sym->formal)
10620 {
10621 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10622 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10623 if (i_rank == my_rank)
10624 {
10625 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10626 " rank (%d) as '%s'",
10627 list->proc_sym->name, &list->where, my_rank,
10628 i->proc_sym->name);
10629 goto error;
10630 }
10631 }
10632 }
10633
10634 /* Is this the/a scalar finalizer procedure? */
10635 if (!arg->as || arg->as->rank == 0)
10636 seen_scalar = true;
10637
10638 /* Find the symtree for this procedure. */
10639 gcc_assert (!list->proc_tree);
10640 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10641
10642 prev_link = &list->next;
10643 continue;
10644
10645 /* Remove wrong nodes immediately from the list so we don't risk any
10646 troubles in the future when they might fail later expectations. */
10647 error:
10648 result = FAILURE;
10649 i = list;
10650 *prev_link = list->next;
10651 gfc_free_finalizer (i);
10652 }
10653
10654 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10655 were nodes in the list, must have been for arrays. It is surely a good
10656 idea to have a scalar version there if there's something to finalize. */
10657 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10658 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10659 " defined at %L, suggest also scalar one",
10660 derived->name, &derived->declared_at);
10661
10662 /* TODO: Remove this error when finalization is finished. */
10663 gfc_error ("Finalization at %L is not yet implemented",
10664 &derived->declared_at);
10665
10666 return result;
10667 }
10668
10669
10670 /* Check that it is ok for the typebound procedure proc to override the
10671 procedure old. */
10672
10673 static gfc_try
10674 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
10675 {
10676 locus where;
10677 const gfc_symbol* proc_target;
10678 const gfc_symbol* old_target;
10679 unsigned proc_pass_arg, old_pass_arg, argpos;
10680 gfc_formal_arglist* proc_formal;
10681 gfc_formal_arglist* old_formal;
10682
10683 /* This procedure should only be called for non-GENERIC proc. */
10684 gcc_assert (!proc->n.tb->is_generic);
10685
10686 /* If the overwritten procedure is GENERIC, this is an error. */
10687 if (old->n.tb->is_generic)
10688 {
10689 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10690 old->name, &proc->n.tb->where);
10691 return FAILURE;
10692 }
10693
10694 where = proc->n.tb->where;
10695 proc_target = proc->n.tb->u.specific->n.sym;
10696 old_target = old->n.tb->u.specific->n.sym;
10697
10698 /* Check that overridden binding is not NON_OVERRIDABLE. */
10699 if (old->n.tb->non_overridable)
10700 {
10701 gfc_error ("'%s' at %L overrides a procedure binding declared"
10702 " NON_OVERRIDABLE", proc->name, &where);
10703 return FAILURE;
10704 }
10705
10706 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10707 if (!old->n.tb->deferred && proc->n.tb->deferred)
10708 {
10709 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10710 " non-DEFERRED binding", proc->name, &where);
10711 return FAILURE;
10712 }
10713
10714 /* If the overridden binding is PURE, the overriding must be, too. */
10715 if (old_target->attr.pure && !proc_target->attr.pure)
10716 {
10717 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10718 proc->name, &where);
10719 return FAILURE;
10720 }
10721
10722 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10723 is not, the overriding must not be either. */
10724 if (old_target->attr.elemental && !proc_target->attr.elemental)
10725 {
10726 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10727 " ELEMENTAL", proc->name, &where);
10728 return FAILURE;
10729 }
10730 if (!old_target->attr.elemental && proc_target->attr.elemental)
10731 {
10732 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10733 " be ELEMENTAL, either", proc->name, &where);
10734 return FAILURE;
10735 }
10736
10737 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10738 SUBROUTINE. */
10739 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
10740 {
10741 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10742 " SUBROUTINE", proc->name, &where);
10743 return FAILURE;
10744 }
10745
10746 /* If the overridden binding is a FUNCTION, the overriding must also be a
10747 FUNCTION and have the same characteristics. */
10748 if (old_target->attr.function)
10749 {
10750 if (!proc_target->attr.function)
10751 {
10752 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10753 " FUNCTION", proc->name, &where);
10754 return FAILURE;
10755 }
10756
10757 /* FIXME: Do more comprehensive checking (including, for instance, the
10758 rank and array-shape). */
10759 gcc_assert (proc_target->result && old_target->result);
10760 if (!gfc_compare_types (&proc_target->result->ts,
10761 &old_target->result->ts))
10762 {
10763 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10764 " matching result types", proc->name, &where);
10765 return FAILURE;
10766 }
10767 }
10768
10769 /* If the overridden binding is PUBLIC, the overriding one must not be
10770 PRIVATE. */
10771 if (old->n.tb->access == ACCESS_PUBLIC
10772 && proc->n.tb->access == ACCESS_PRIVATE)
10773 {
10774 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10775 " PRIVATE", proc->name, &where);
10776 return FAILURE;
10777 }
10778
10779 /* Compare the formal argument lists of both procedures. This is also abused
10780 to find the position of the passed-object dummy arguments of both
10781 bindings as at least the overridden one might not yet be resolved and we
10782 need those positions in the check below. */
10783 proc_pass_arg = old_pass_arg = 0;
10784 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
10785 proc_pass_arg = 1;
10786 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
10787 old_pass_arg = 1;
10788 argpos = 1;
10789 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
10790 proc_formal && old_formal;
10791 proc_formal = proc_formal->next, old_formal = old_formal->next)
10792 {
10793 if (proc->n.tb->pass_arg
10794 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
10795 proc_pass_arg = argpos;
10796 if (old->n.tb->pass_arg
10797 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10798 old_pass_arg = argpos;
10799
10800 /* Check that the names correspond. */
10801 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10802 {
10803 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10804 " to match the corresponding argument of the overridden"
10805 " procedure", proc_formal->sym->name, proc->name, &where,
10806 old_formal->sym->name);
10807 return FAILURE;
10808 }
10809
10810 /* Check that the types correspond if neither is the passed-object
10811 argument. */
10812 /* FIXME: Do more comprehensive testing here. */
10813 if (proc_pass_arg != argpos && old_pass_arg != argpos
10814 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10815 {
10816 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10817 "in respect to the overridden procedure",
10818 proc_formal->sym->name, proc->name, &where);
10819 return FAILURE;
10820 }
10821
10822 ++argpos;
10823 }
10824 if (proc_formal || old_formal)
10825 {
10826 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10827 " the overridden procedure", proc->name, &where);
10828 return FAILURE;
10829 }
10830
10831 /* If the overridden binding is NOPASS, the overriding one must also be
10832 NOPASS. */
10833 if (old->n.tb->nopass && !proc->n.tb->nopass)
10834 {
10835 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10836 " NOPASS", proc->name, &where);
10837 return FAILURE;
10838 }
10839
10840 /* If the overridden binding is PASS(x), the overriding one must also be
10841 PASS and the passed-object dummy arguments must correspond. */
10842 if (!old->n.tb->nopass)
10843 {
10844 if (proc->n.tb->nopass)
10845 {
10846 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10847 " PASS", proc->name, &where);
10848 return FAILURE;
10849 }
10850
10851 if (proc_pass_arg != old_pass_arg)
10852 {
10853 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10854 " the same position as the passed-object dummy argument of"
10855 " the overridden procedure", proc->name, &where);
10856 return FAILURE;
10857 }
10858 }
10859
10860 return SUCCESS;
10861 }
10862
10863
10864 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10865
10866 static gfc_try
10867 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10868 const char* generic_name, locus where)
10869 {
10870 gfc_symbol* sym1;
10871 gfc_symbol* sym2;
10872
10873 gcc_assert (t1->specific && t2->specific);
10874 gcc_assert (!t1->specific->is_generic);
10875 gcc_assert (!t2->specific->is_generic);
10876
10877 sym1 = t1->specific->u.specific->n.sym;
10878 sym2 = t2->specific->u.specific->n.sym;
10879
10880 if (sym1 == sym2)
10881 return SUCCESS;
10882
10883 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10884 if (sym1->attr.subroutine != sym2->attr.subroutine
10885 || sym1->attr.function != sym2->attr.function)
10886 {
10887 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10888 " GENERIC '%s' at %L",
10889 sym1->name, sym2->name, generic_name, &where);
10890 return FAILURE;
10891 }
10892
10893 /* Compare the interfaces. */
10894 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10895 {
10896 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10897 sym1->name, sym2->name, generic_name, &where);
10898 return FAILURE;
10899 }
10900
10901 return SUCCESS;
10902 }
10903
10904
10905 /* Worker function for resolving a generic procedure binding; this is used to
10906 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10907
10908 The difference between those cases is finding possible inherited bindings
10909 that are overridden, as one has to look for them in tb_sym_root,
10910 tb_uop_root or tb_op, respectively. Thus the caller must already find
10911 the super-type and set p->overridden correctly. */
10912
10913 static gfc_try
10914 resolve_tb_generic_targets (gfc_symbol* super_type,
10915 gfc_typebound_proc* p, const char* name)
10916 {
10917 gfc_tbp_generic* target;
10918 gfc_symtree* first_target;
10919 gfc_symtree* inherited;
10920
10921 gcc_assert (p && p->is_generic);
10922
10923 /* Try to find the specific bindings for the symtrees in our target-list. */
10924 gcc_assert (p->u.generic);
10925 for (target = p->u.generic; target; target = target->next)
10926 if (!target->specific)
10927 {
10928 gfc_typebound_proc* overridden_tbp;
10929 gfc_tbp_generic* g;
10930 const char* target_name;
10931
10932 target_name = target->specific_st->name;
10933
10934 /* Defined for this type directly. */
10935 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
10936 {
10937 target->specific = target->specific_st->n.tb;
10938 goto specific_found;
10939 }
10940
10941 /* Look for an inherited specific binding. */
10942 if (super_type)
10943 {
10944 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10945 true, NULL);
10946
10947 if (inherited)
10948 {
10949 gcc_assert (inherited->n.tb);
10950 target->specific = inherited->n.tb;
10951 goto specific_found;
10952 }
10953 }
10954
10955 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10956 " at %L", target_name, name, &p->where);
10957 return FAILURE;
10958
10959 /* Once we've found the specific binding, check it is not ambiguous with
10960 other specifics already found or inherited for the same GENERIC. */
10961 specific_found:
10962 gcc_assert (target->specific);
10963
10964 /* This must really be a specific binding! */
10965 if (target->specific->is_generic)
10966 {
10967 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10968 " '%s' is GENERIC, too", name, &p->where, target_name);
10969 return FAILURE;
10970 }
10971
10972 /* Check those already resolved on this type directly. */
10973 for (g = p->u.generic; g; g = g->next)
10974 if (g != target && g->specific
10975 && check_generic_tbp_ambiguity (target, g, name, p->where)
10976 == FAILURE)
10977 return FAILURE;
10978
10979 /* Check for ambiguity with inherited specific targets. */
10980 for (overridden_tbp = p->overridden; overridden_tbp;
10981 overridden_tbp = overridden_tbp->overridden)
10982 if (overridden_tbp->is_generic)
10983 {
10984 for (g = overridden_tbp->u.generic; g; g = g->next)
10985 {
10986 gcc_assert (g->specific);
10987 if (check_generic_tbp_ambiguity (target, g,
10988 name, p->where) == FAILURE)
10989 return FAILURE;
10990 }
10991 }
10992 }
10993
10994 /* If we attempt to "overwrite" a specific binding, this is an error. */
10995 if (p->overridden && !p->overridden->is_generic)
10996 {
10997 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10998 " the same name", name, &p->where);
10999 return FAILURE;
11000 }
11001
11002 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11003 all must have the same attributes here. */
11004 first_target = p->u.generic->specific->u.specific;
11005 gcc_assert (first_target);
11006 p->subroutine = first_target->n.sym->attr.subroutine;
11007 p->function = first_target->n.sym->attr.function;
11008
11009 return SUCCESS;
11010 }
11011
11012
11013 /* Resolve a GENERIC procedure binding for a derived type. */
11014
11015 static gfc_try
11016 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11017 {
11018 gfc_symbol* super_type;
11019
11020 /* Find the overridden binding if any. */
11021 st->n.tb->overridden = NULL;
11022 super_type = gfc_get_derived_super_type (derived);
11023 if (super_type)
11024 {
11025 gfc_symtree* overridden;
11026 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11027 true, NULL);
11028
11029 if (overridden && overridden->n.tb)
11030 st->n.tb->overridden = overridden->n.tb;
11031 }
11032
11033 /* Resolve using worker function. */
11034 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11035 }
11036
11037
11038 /* Retrieve the target-procedure of an operator binding and do some checks in
11039 common for intrinsic and user-defined type-bound operators. */
11040
11041 static gfc_symbol*
11042 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11043 {
11044 gfc_symbol* target_proc;
11045
11046 gcc_assert (target->specific && !target->specific->is_generic);
11047 target_proc = target->specific->u.specific->n.sym;
11048 gcc_assert (target_proc);
11049
11050 /* All operator bindings must have a passed-object dummy argument. */
11051 if (target->specific->nopass)
11052 {
11053 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11054 return NULL;
11055 }
11056
11057 return target_proc;
11058 }
11059
11060
11061 /* Resolve a type-bound intrinsic operator. */
11062
11063 static gfc_try
11064 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11065 gfc_typebound_proc* p)
11066 {
11067 gfc_symbol* super_type;
11068 gfc_tbp_generic* target;
11069
11070 /* If there's already an error here, do nothing (but don't fail again). */
11071 if (p->error)
11072 return SUCCESS;
11073
11074 /* Operators should always be GENERIC bindings. */
11075 gcc_assert (p->is_generic);
11076
11077 /* Look for an overridden binding. */
11078 super_type = gfc_get_derived_super_type (derived);
11079 if (super_type && super_type->f2k_derived)
11080 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11081 op, true, NULL);
11082 else
11083 p->overridden = NULL;
11084
11085 /* Resolve general GENERIC properties using worker function. */
11086 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
11087 goto error;
11088
11089 /* Check the targets to be procedures of correct interface. */
11090 for (target = p->u.generic; target; target = target->next)
11091 {
11092 gfc_symbol* target_proc;
11093
11094 target_proc = get_checked_tb_operator_target (target, p->where);
11095 if (!target_proc)
11096 goto error;
11097
11098 if (!gfc_check_operator_interface (target_proc, op, p->where))
11099 goto error;
11100 }
11101
11102 return SUCCESS;
11103
11104 error:
11105 p->error = 1;
11106 return FAILURE;
11107 }
11108
11109
11110 /* Resolve a type-bound user operator (tree-walker callback). */
11111
11112 static gfc_symbol* resolve_bindings_derived;
11113 static gfc_try resolve_bindings_result;
11114
11115 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
11116
11117 static void
11118 resolve_typebound_user_op (gfc_symtree* stree)
11119 {
11120 gfc_symbol* super_type;
11121 gfc_tbp_generic* target;
11122
11123 gcc_assert (stree && stree->n.tb);
11124
11125 if (stree->n.tb->error)
11126 return;
11127
11128 /* Operators should always be GENERIC bindings. */
11129 gcc_assert (stree->n.tb->is_generic);
11130
11131 /* Find overridden procedure, if any. */
11132 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11133 if (super_type && super_type->f2k_derived)
11134 {
11135 gfc_symtree* overridden;
11136 overridden = gfc_find_typebound_user_op (super_type, NULL,
11137 stree->name, true, NULL);
11138
11139 if (overridden && overridden->n.tb)
11140 stree->n.tb->overridden = overridden->n.tb;
11141 }
11142 else
11143 stree->n.tb->overridden = NULL;
11144
11145 /* Resolve basically using worker function. */
11146 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
11147 == FAILURE)
11148 goto error;
11149
11150 /* Check the targets to be functions of correct interface. */
11151 for (target = stree->n.tb->u.generic; target; target = target->next)
11152 {
11153 gfc_symbol* target_proc;
11154
11155 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11156 if (!target_proc)
11157 goto error;
11158
11159 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
11160 goto error;
11161 }
11162
11163 return;
11164
11165 error:
11166 resolve_bindings_result = FAILURE;
11167 stree->n.tb->error = 1;
11168 }
11169
11170
11171 /* Resolve the type-bound procedures for a derived type. */
11172
11173 static void
11174 resolve_typebound_procedure (gfc_symtree* stree)
11175 {
11176 gfc_symbol* proc;
11177 locus where;
11178 gfc_symbol* me_arg;
11179 gfc_symbol* super_type;
11180 gfc_component* comp;
11181
11182 gcc_assert (stree);
11183
11184 /* Undefined specific symbol from GENERIC target definition. */
11185 if (!stree->n.tb)
11186 return;
11187
11188 if (stree->n.tb->error)
11189 return;
11190
11191 /* If this is a GENERIC binding, use that routine. */
11192 if (stree->n.tb->is_generic)
11193 {
11194 if (resolve_typebound_generic (resolve_bindings_derived, stree)
11195 == FAILURE)
11196 goto error;
11197 return;
11198 }
11199
11200 /* Get the target-procedure to check it. */
11201 gcc_assert (!stree->n.tb->is_generic);
11202 gcc_assert (stree->n.tb->u.specific);
11203 proc = stree->n.tb->u.specific->n.sym;
11204 where = stree->n.tb->where;
11205
11206 /* Default access should already be resolved from the parser. */
11207 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11208
11209 /* It should be a module procedure or an external procedure with explicit
11210 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11211 if ((!proc->attr.subroutine && !proc->attr.function)
11212 || (proc->attr.proc != PROC_MODULE
11213 && proc->attr.if_source != IFSRC_IFBODY)
11214 || (proc->attr.abstract && !stree->n.tb->deferred))
11215 {
11216 gfc_error ("'%s' must be a module procedure or an external procedure with"
11217 " an explicit interface at %L", proc->name, &where);
11218 goto error;
11219 }
11220 stree->n.tb->subroutine = proc->attr.subroutine;
11221 stree->n.tb->function = proc->attr.function;
11222
11223 /* Find the super-type of the current derived type. We could do this once and
11224 store in a global if speed is needed, but as long as not I believe this is
11225 more readable and clearer. */
11226 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11227
11228 /* If PASS, resolve and check arguments if not already resolved / loaded
11229 from a .mod file. */
11230 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11231 {
11232 if (stree->n.tb->pass_arg)
11233 {
11234 gfc_formal_arglist* i;
11235
11236 /* If an explicit passing argument name is given, walk the arg-list
11237 and look for it. */
11238
11239 me_arg = NULL;
11240 stree->n.tb->pass_arg_num = 1;
11241 for (i = proc->formal; i; i = i->next)
11242 {
11243 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11244 {
11245 me_arg = i->sym;
11246 break;
11247 }
11248 ++stree->n.tb->pass_arg_num;
11249 }
11250
11251 if (!me_arg)
11252 {
11253 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11254 " argument '%s'",
11255 proc->name, stree->n.tb->pass_arg, &where,
11256 stree->n.tb->pass_arg);
11257 goto error;
11258 }
11259 }
11260 else
11261 {
11262 /* Otherwise, take the first one; there should in fact be at least
11263 one. */
11264 stree->n.tb->pass_arg_num = 1;
11265 if (!proc->formal)
11266 {
11267 gfc_error ("Procedure '%s' with PASS at %L must have at"
11268 " least one argument", proc->name, &where);
11269 goto error;
11270 }
11271 me_arg = proc->formal->sym;
11272 }
11273
11274 /* Now check that the argument-type matches and the passed-object
11275 dummy argument is generally fine. */
11276
11277 gcc_assert (me_arg);
11278
11279 if (me_arg->ts.type != BT_CLASS)
11280 {
11281 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11282 " at %L", proc->name, &where);
11283 goto error;
11284 }
11285
11286 if (CLASS_DATA (me_arg)->ts.u.derived
11287 != resolve_bindings_derived)
11288 {
11289 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11290 " the derived-type '%s'", me_arg->name, proc->name,
11291 me_arg->name, &where, resolve_bindings_derived->name);
11292 goto error;
11293 }
11294
11295 gcc_assert (me_arg->ts.type == BT_CLASS);
11296 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
11297 {
11298 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11299 " scalar", proc->name, &where);
11300 goto error;
11301 }
11302 if (CLASS_DATA (me_arg)->attr.allocatable)
11303 {
11304 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11305 " be ALLOCATABLE", proc->name, &where);
11306 goto error;
11307 }
11308 if (CLASS_DATA (me_arg)->attr.class_pointer)
11309 {
11310 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11311 " be POINTER", proc->name, &where);
11312 goto error;
11313 }
11314 }
11315
11316 /* If we are extending some type, check that we don't override a procedure
11317 flagged NON_OVERRIDABLE. */
11318 stree->n.tb->overridden = NULL;
11319 if (super_type)
11320 {
11321 gfc_symtree* overridden;
11322 overridden = gfc_find_typebound_proc (super_type, NULL,
11323 stree->name, true, NULL);
11324
11325 if (overridden && overridden->n.tb)
11326 stree->n.tb->overridden = overridden->n.tb;
11327
11328 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
11329 goto error;
11330 }
11331
11332 /* See if there's a name collision with a component directly in this type. */
11333 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11334 if (!strcmp (comp->name, stree->name))
11335 {
11336 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11337 " '%s'",
11338 stree->name, &where, resolve_bindings_derived->name);
11339 goto error;
11340 }
11341
11342 /* Try to find a name collision with an inherited component. */
11343 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11344 {
11345 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11346 " component of '%s'",
11347 stree->name, &where, resolve_bindings_derived->name);
11348 goto error;
11349 }
11350
11351 stree->n.tb->error = 0;
11352 return;
11353
11354 error:
11355 resolve_bindings_result = FAILURE;
11356 stree->n.tb->error = 1;
11357 }
11358
11359
11360 static gfc_try
11361 resolve_typebound_procedures (gfc_symbol* derived)
11362 {
11363 int op;
11364
11365 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11366 return SUCCESS;
11367
11368 resolve_bindings_derived = derived;
11369 resolve_bindings_result = SUCCESS;
11370
11371 /* Make sure the vtab has been generated. */
11372 gfc_find_derived_vtab (derived);
11373
11374 if (derived->f2k_derived->tb_sym_root)
11375 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11376 &resolve_typebound_procedure);
11377
11378 if (derived->f2k_derived->tb_uop_root)
11379 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11380 &resolve_typebound_user_op);
11381
11382 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11383 {
11384 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11385 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11386 p) == FAILURE)
11387 resolve_bindings_result = FAILURE;
11388 }
11389
11390 return resolve_bindings_result;
11391 }
11392
11393
11394 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11395 to give all identical derived types the same backend_decl. */
11396 static void
11397 add_dt_to_dt_list (gfc_symbol *derived)
11398 {
11399 gfc_dt_list *dt_list;
11400
11401 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11402 if (derived == dt_list->derived)
11403 return;
11404
11405 dt_list = gfc_get_dt_list ();
11406 dt_list->next = gfc_derived_types;
11407 dt_list->derived = derived;
11408 gfc_derived_types = dt_list;
11409 }
11410
11411
11412 /* Ensure that a derived-type is really not abstract, meaning that every
11413 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11414
11415 static gfc_try
11416 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11417 {
11418 if (!st)
11419 return SUCCESS;
11420
11421 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11422 return FAILURE;
11423 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11424 return FAILURE;
11425
11426 if (st->n.tb && st->n.tb->deferred)
11427 {
11428 gfc_symtree* overriding;
11429 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11430 if (!overriding)
11431 return FAILURE;
11432 gcc_assert (overriding->n.tb);
11433 if (overriding->n.tb->deferred)
11434 {
11435 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11436 " '%s' is DEFERRED and not overridden",
11437 sub->name, &sub->declared_at, st->name);
11438 return FAILURE;
11439 }
11440 }
11441
11442 return SUCCESS;
11443 }
11444
11445 static gfc_try
11446 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11447 {
11448 /* The algorithm used here is to recursively travel up the ancestry of sub
11449 and for each ancestor-type, check all bindings. If any of them is
11450 DEFERRED, look it up starting from sub and see if the found (overriding)
11451 binding is not DEFERRED.
11452 This is not the most efficient way to do this, but it should be ok and is
11453 clearer than something sophisticated. */
11454
11455 gcc_assert (ancestor && !sub->attr.abstract);
11456
11457 if (!ancestor->attr.abstract)
11458 return SUCCESS;
11459
11460 /* Walk bindings of this ancestor. */
11461 if (ancestor->f2k_derived)
11462 {
11463 gfc_try t;
11464 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11465 if (t == FAILURE)
11466 return FAILURE;
11467 }
11468
11469 /* Find next ancestor type and recurse on it. */
11470 ancestor = gfc_get_derived_super_type (ancestor);
11471 if (ancestor)
11472 return ensure_not_abstract (sub, ancestor);
11473
11474 return SUCCESS;
11475 }
11476
11477
11478 /* Resolve the components of a derived type. */
11479
11480 static gfc_try
11481 resolve_fl_derived (gfc_symbol *sym)
11482 {
11483 gfc_symbol* super_type;
11484 gfc_component *c;
11485
11486 super_type = gfc_get_derived_super_type (sym);
11487
11488 if (sym->attr.is_class && sym->ts.u.derived == NULL)
11489 {
11490 /* Fix up incomplete CLASS symbols. */
11491 gfc_component *data = gfc_find_component (sym, "_data", true, true);
11492 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
11493 if (vptr->ts.u.derived == NULL)
11494 {
11495 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
11496 gcc_assert (vtab);
11497 vptr->ts.u.derived = vtab->ts.u.derived;
11498 }
11499 }
11500
11501 /* F2008, C432. */
11502 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11503 {
11504 gfc_error ("As extending type '%s' at %L has a coarray component, "
11505 "parent type '%s' shall also have one", sym->name,
11506 &sym->declared_at, super_type->name);
11507 return FAILURE;
11508 }
11509
11510 /* Ensure the extended type gets resolved before we do. */
11511 if (super_type && resolve_fl_derived (super_type) == FAILURE)
11512 return FAILURE;
11513
11514 /* An ABSTRACT type must be extensible. */
11515 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11516 {
11517 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11518 sym->name, &sym->declared_at);
11519 return FAILURE;
11520 }
11521
11522 for (c = sym->components; c != NULL; c = c->next)
11523 {
11524 /* F2008, C442. */
11525 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
11526 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11527 {
11528 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11529 "deferred shape", c->name, &c->loc);
11530 return FAILURE;
11531 }
11532
11533 /* F2008, C443. */
11534 if (c->attr.codimension && c->ts.type == BT_DERIVED
11535 && c->ts.u.derived->ts.is_iso_c)
11536 {
11537 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11538 "shall not be a coarray", c->name, &c->loc);
11539 return FAILURE;
11540 }
11541
11542 /* F2008, C444. */
11543 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11544 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11545 || c->attr.allocatable))
11546 {
11547 gfc_error ("Component '%s' at %L with coarray component "
11548 "shall be a nonpointer, nonallocatable scalar",
11549 c->name, &c->loc);
11550 return FAILURE;
11551 }
11552
11553 /* F2008, C448. */
11554 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11555 {
11556 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11557 "is not an array pointer", c->name, &c->loc);
11558 return FAILURE;
11559 }
11560
11561 if (c->attr.proc_pointer && c->ts.interface)
11562 {
11563 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11564 gfc_error ("Interface '%s', used by procedure pointer component "
11565 "'%s' at %L, is declared in a later PROCEDURE statement",
11566 c->ts.interface->name, c->name, &c->loc);
11567
11568 /* Get the attributes from the interface (now resolved). */
11569 if (c->ts.interface->attr.if_source
11570 || c->ts.interface->attr.intrinsic)
11571 {
11572 gfc_symbol *ifc = c->ts.interface;
11573
11574 if (ifc->formal && !ifc->formal_ns)
11575 resolve_symbol (ifc);
11576
11577 if (ifc->attr.intrinsic)
11578 resolve_intrinsic (ifc, &ifc->declared_at);
11579
11580 if (ifc->result)
11581 {
11582 c->ts = ifc->result->ts;
11583 c->attr.allocatable = ifc->result->attr.allocatable;
11584 c->attr.pointer = ifc->result->attr.pointer;
11585 c->attr.dimension = ifc->result->attr.dimension;
11586 c->as = gfc_copy_array_spec (ifc->result->as);
11587 }
11588 else
11589 {
11590 c->ts = ifc->ts;
11591 c->attr.allocatable = ifc->attr.allocatable;
11592 c->attr.pointer = ifc->attr.pointer;
11593 c->attr.dimension = ifc->attr.dimension;
11594 c->as = gfc_copy_array_spec (ifc->as);
11595 }
11596 c->ts.interface = ifc;
11597 c->attr.function = ifc->attr.function;
11598 c->attr.subroutine = ifc->attr.subroutine;
11599 gfc_copy_formal_args_ppc (c, ifc);
11600
11601 c->attr.pure = ifc->attr.pure;
11602 c->attr.elemental = ifc->attr.elemental;
11603 c->attr.recursive = ifc->attr.recursive;
11604 c->attr.always_explicit = ifc->attr.always_explicit;
11605 c->attr.ext_attr |= ifc->attr.ext_attr;
11606 /* Replace symbols in array spec. */
11607 if (c->as)
11608 {
11609 int i;
11610 for (i = 0; i < c->as->rank; i++)
11611 {
11612 gfc_expr_replace_comp (c->as->lower[i], c);
11613 gfc_expr_replace_comp (c->as->upper[i], c);
11614 }
11615 }
11616 /* Copy char length. */
11617 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11618 {
11619 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11620 gfc_expr_replace_comp (cl->length, c);
11621 if (cl->length && !cl->resolved
11622 && gfc_resolve_expr (cl->length) == FAILURE)
11623 return FAILURE;
11624 c->ts.u.cl = cl;
11625 }
11626 }
11627 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11628 {
11629 gfc_error ("Interface '%s' of procedure pointer component "
11630 "'%s' at %L must be explicit", c->ts.interface->name,
11631 c->name, &c->loc);
11632 return FAILURE;
11633 }
11634 }
11635 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11636 {
11637 /* Since PPCs are not implicitly typed, a PPC without an explicit
11638 interface must be a subroutine. */
11639 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11640 }
11641
11642 /* Procedure pointer components: Check PASS arg. */
11643 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11644 && !sym->attr.vtype)
11645 {
11646 gfc_symbol* me_arg;
11647
11648 if (c->tb->pass_arg)
11649 {
11650 gfc_formal_arglist* i;
11651
11652 /* If an explicit passing argument name is given, walk the arg-list
11653 and look for it. */
11654
11655 me_arg = NULL;
11656 c->tb->pass_arg_num = 1;
11657 for (i = c->formal; i; i = i->next)
11658 {
11659 if (!strcmp (i->sym->name, c->tb->pass_arg))
11660 {
11661 me_arg = i->sym;
11662 break;
11663 }
11664 c->tb->pass_arg_num++;
11665 }
11666
11667 if (!me_arg)
11668 {
11669 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11670 "at %L has no argument '%s'", c->name,
11671 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11672 c->tb->error = 1;
11673 return FAILURE;
11674 }
11675 }
11676 else
11677 {
11678 /* Otherwise, take the first one; there should in fact be at least
11679 one. */
11680 c->tb->pass_arg_num = 1;
11681 if (!c->formal)
11682 {
11683 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11684 "must have at least one argument",
11685 c->name, &c->loc);
11686 c->tb->error = 1;
11687 return FAILURE;
11688 }
11689 me_arg = c->formal->sym;
11690 }
11691
11692 /* Now check that the argument-type matches. */
11693 gcc_assert (me_arg);
11694 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11695 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11696 || (me_arg->ts.type == BT_CLASS
11697 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11698 {
11699 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11700 " the derived type '%s'", me_arg->name, c->name,
11701 me_arg->name, &c->loc, sym->name);
11702 c->tb->error = 1;
11703 return FAILURE;
11704 }
11705
11706 /* Check for C453. */
11707 if (me_arg->attr.dimension)
11708 {
11709 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11710 "must be scalar", me_arg->name, c->name, me_arg->name,
11711 &c->loc);
11712 c->tb->error = 1;
11713 return FAILURE;
11714 }
11715
11716 if (me_arg->attr.pointer)
11717 {
11718 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11719 "may not have the POINTER attribute", me_arg->name,
11720 c->name, me_arg->name, &c->loc);
11721 c->tb->error = 1;
11722 return FAILURE;
11723 }
11724
11725 if (me_arg->attr.allocatable)
11726 {
11727 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11728 "may not be ALLOCATABLE", me_arg->name, c->name,
11729 me_arg->name, &c->loc);
11730 c->tb->error = 1;
11731 return FAILURE;
11732 }
11733
11734 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11735 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11736 " at %L", c->name, &c->loc);
11737
11738 }
11739
11740 /* Check type-spec if this is not the parent-type component. */
11741 if ((!sym->attr.extension || c != sym->components) && !sym->attr.vtype
11742 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11743 return FAILURE;
11744
11745 /* If this type is an extension, set the accessibility of the parent
11746 component. */
11747 if (super_type && c == sym->components
11748 && strcmp (super_type->name, c->name) == 0)
11749 c->attr.access = super_type->attr.access;
11750
11751 /* If this type is an extension, see if this component has the same name
11752 as an inherited type-bound procedure. */
11753 if (super_type && !sym->attr.is_class
11754 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11755 {
11756 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11757 " inherited type-bound procedure",
11758 c->name, sym->name, &c->loc);
11759 return FAILURE;
11760 }
11761
11762 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
11763 && !c->ts.deferred)
11764 {
11765 if (c->ts.u.cl->length == NULL
11766 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11767 || !gfc_is_constant_expr (c->ts.u.cl->length))
11768 {
11769 gfc_error ("Character length of component '%s' needs to "
11770 "be a constant specification expression at %L",
11771 c->name,
11772 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11773 return FAILURE;
11774 }
11775 }
11776
11777 if (c->ts.type == BT_CHARACTER && c->ts.deferred
11778 && !c->attr.pointer && !c->attr.allocatable)
11779 {
11780 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11781 "length must be a POINTER or ALLOCATABLE",
11782 c->name, sym->name, &c->loc);
11783 return FAILURE;
11784 }
11785
11786 if (c->ts.type == BT_DERIVED
11787 && sym->component_access != ACCESS_PRIVATE
11788 && gfc_check_symbol_access (sym)
11789 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11790 && !c->ts.u.derived->attr.use_assoc
11791 && !gfc_check_symbol_access (c->ts.u.derived)
11792 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11793 "is a PRIVATE type and cannot be a component of "
11794 "'%s', which is PUBLIC at %L", c->name,
11795 sym->name, &sym->declared_at) == FAILURE)
11796 return FAILURE;
11797
11798 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
11799 {
11800 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11801 "type %s", c->name, &c->loc, sym->name);
11802 return FAILURE;
11803 }
11804
11805 if (sym->attr.sequence)
11806 {
11807 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11808 {
11809 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11810 "not have the SEQUENCE attribute",
11811 c->ts.u.derived->name, &sym->declared_at);
11812 return FAILURE;
11813 }
11814 }
11815
11816 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11817 && c->attr.pointer && c->ts.u.derived->components == NULL
11818 && !c->ts.u.derived->attr.zero_comp)
11819 {
11820 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11821 "that has not been declared", c->name, sym->name,
11822 &c->loc);
11823 return FAILURE;
11824 }
11825
11826 if (c->ts.type == BT_CLASS && c->attr.class_ok
11827 && CLASS_DATA (c)->attr.class_pointer
11828 && CLASS_DATA (c)->ts.u.derived->components == NULL
11829 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11830 {
11831 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11832 "that has not been declared", c->name, sym->name,
11833 &c->loc);
11834 return FAILURE;
11835 }
11836
11837 /* C437. */
11838 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
11839 && (!c->attr.class_ok
11840 || !(CLASS_DATA (c)->attr.class_pointer
11841 || CLASS_DATA (c)->attr.allocatable)))
11842 {
11843 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11844 "or pointer", c->name, &c->loc);
11845 return FAILURE;
11846 }
11847
11848 /* Ensure that all the derived type components are put on the
11849 derived type list; even in formal namespaces, where derived type
11850 pointer components might not have been declared. */
11851 if (c->ts.type == BT_DERIVED
11852 && c->ts.u.derived
11853 && c->ts.u.derived->components
11854 && c->attr.pointer
11855 && sym != c->ts.u.derived)
11856 add_dt_to_dt_list (c->ts.u.derived);
11857
11858 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11859 || c->attr.proc_pointer
11860 || c->attr.allocatable)) == FAILURE)
11861 return FAILURE;
11862 }
11863
11864 /* Resolve the type-bound procedures. */
11865 if (resolve_typebound_procedures (sym) == FAILURE)
11866 return FAILURE;
11867
11868 /* Resolve the finalizer procedures. */
11869 if (gfc_resolve_finalizers (sym) == FAILURE)
11870 return FAILURE;
11871
11872 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11873 all DEFERRED bindings are overridden. */
11874 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11875 && !sym->attr.is_class
11876 && ensure_not_abstract (sym, super_type) == FAILURE)
11877 return FAILURE;
11878
11879 /* Add derived type to the derived type list. */
11880 add_dt_to_dt_list (sym);
11881
11882 return SUCCESS;
11883 }
11884
11885
11886 static gfc_try
11887 resolve_fl_namelist (gfc_symbol *sym)
11888 {
11889 gfc_namelist *nl;
11890 gfc_symbol *nlsym;
11891
11892 for (nl = sym->namelist; nl; nl = nl->next)
11893 {
11894 /* Check again, the check in match only works if NAMELIST comes
11895 after the decl. */
11896 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
11897 {
11898 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
11899 "allowed", nl->sym->name, sym->name, &sym->declared_at);
11900 return FAILURE;
11901 }
11902
11903 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11904 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11905 "object '%s' with assumed shape in namelist "
11906 "'%s' at %L", nl->sym->name, sym->name,
11907 &sym->declared_at) == FAILURE)
11908 return FAILURE;
11909
11910 if (is_non_constant_shape_array (nl->sym)
11911 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11912 "object '%s' with nonconstant shape in namelist "
11913 "'%s' at %L", nl->sym->name, sym->name,
11914 &sym->declared_at) == FAILURE)
11915 return FAILURE;
11916
11917 if (nl->sym->ts.type == BT_CHARACTER
11918 && (nl->sym->ts.u.cl->length == NULL
11919 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
11920 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11921 "'%s' with nonconstant character length in "
11922 "namelist '%s' at %L", nl->sym->name, sym->name,
11923 &sym->declared_at) == FAILURE)
11924 return FAILURE;
11925
11926 /* FIXME: Once UDDTIO is implemented, the following can be
11927 removed. */
11928 if (nl->sym->ts.type == BT_CLASS)
11929 {
11930 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
11931 "polymorphic and requires a defined input/output "
11932 "procedure", nl->sym->name, sym->name, &sym->declared_at);
11933 return FAILURE;
11934 }
11935
11936 if (nl->sym->ts.type == BT_DERIVED
11937 && (nl->sym->ts.u.derived->attr.alloc_comp
11938 || nl->sym->ts.u.derived->attr.pointer_comp))
11939 {
11940 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11941 "'%s' in namelist '%s' at %L with ALLOCATABLE "
11942 "or POINTER components", nl->sym->name,
11943 sym->name, &sym->declared_at) == FAILURE)
11944 return FAILURE;
11945
11946 /* FIXME: Once UDDTIO is implemented, the following can be
11947 removed. */
11948 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
11949 "ALLOCATABLE or POINTER components and thus requires "
11950 "a defined input/output procedure", nl->sym->name,
11951 sym->name, &sym->declared_at);
11952 return FAILURE;
11953 }
11954 }
11955
11956 /* Reject PRIVATE objects in a PUBLIC namelist. */
11957 if (gfc_check_symbol_access (sym))
11958 {
11959 for (nl = sym->namelist; nl; nl = nl->next)
11960 {
11961 if (!nl->sym->attr.use_assoc
11962 && !is_sym_host_assoc (nl->sym, sym->ns)
11963 && !gfc_check_symbol_access (nl->sym))
11964 {
11965 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11966 "cannot be member of PUBLIC namelist '%s' at %L",
11967 nl->sym->name, sym->name, &sym->declared_at);
11968 return FAILURE;
11969 }
11970
11971 /* Types with private components that came here by USE-association. */
11972 if (nl->sym->ts.type == BT_DERIVED
11973 && derived_inaccessible (nl->sym->ts.u.derived))
11974 {
11975 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11976 "components and cannot be member of namelist '%s' at %L",
11977 nl->sym->name, sym->name, &sym->declared_at);
11978 return FAILURE;
11979 }
11980
11981 /* Types with private components that are defined in the same module. */
11982 if (nl->sym->ts.type == BT_DERIVED
11983 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11984 && nl->sym->ts.u.derived->attr.private_comp)
11985 {
11986 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11987 "cannot be a member of PUBLIC namelist '%s' at %L",
11988 nl->sym->name, sym->name, &sym->declared_at);
11989 return FAILURE;
11990 }
11991 }
11992 }
11993
11994
11995 /* 14.1.2 A module or internal procedure represent local entities
11996 of the same type as a namelist member and so are not allowed. */
11997 for (nl = sym->namelist; nl; nl = nl->next)
11998 {
11999 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12000 continue;
12001
12002 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12003 if ((nl->sym == sym->ns->proc_name)
12004 ||
12005 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12006 continue;
12007
12008 nlsym = NULL;
12009 if (nl->sym && nl->sym->name)
12010 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12011 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12012 {
12013 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12014 "attribute in '%s' at %L", nlsym->name,
12015 &sym->declared_at);
12016 return FAILURE;
12017 }
12018 }
12019
12020 return SUCCESS;
12021 }
12022
12023
12024 static gfc_try
12025 resolve_fl_parameter (gfc_symbol *sym)
12026 {
12027 /* A parameter array's shape needs to be constant. */
12028 if (sym->as != NULL
12029 && (sym->as->type == AS_DEFERRED
12030 || is_non_constant_shape_array (sym)))
12031 {
12032 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12033 "or of deferred shape", sym->name, &sym->declared_at);
12034 return FAILURE;
12035 }
12036
12037 /* Make sure a parameter that has been implicitly typed still
12038 matches the implicit type, since PARAMETER statements can precede
12039 IMPLICIT statements. */
12040 if (sym->attr.implicit_type
12041 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12042 sym->ns)))
12043 {
12044 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12045 "later IMPLICIT type", sym->name, &sym->declared_at);
12046 return FAILURE;
12047 }
12048
12049 /* Make sure the types of derived parameters are consistent. This
12050 type checking is deferred until resolution because the type may
12051 refer to a derived type from the host. */
12052 if (sym->ts.type == BT_DERIVED
12053 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12054 {
12055 gfc_error ("Incompatible derived type in PARAMETER at %L",
12056 &sym->value->where);
12057 return FAILURE;
12058 }
12059 return SUCCESS;
12060 }
12061
12062
12063 /* Do anything necessary to resolve a symbol. Right now, we just
12064 assume that an otherwise unknown symbol is a variable. This sort
12065 of thing commonly happens for symbols in module. */
12066
12067 static void
12068 resolve_symbol (gfc_symbol *sym)
12069 {
12070 int check_constant, mp_flag;
12071 gfc_symtree *symtree;
12072 gfc_symtree *this_symtree;
12073 gfc_namespace *ns;
12074 gfc_component *c;
12075
12076 if (sym->attr.flavor == FL_UNKNOWN)
12077 {
12078
12079 /* If we find that a flavorless symbol is an interface in one of the
12080 parent namespaces, find its symtree in this namespace, free the
12081 symbol and set the symtree to point to the interface symbol. */
12082 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12083 {
12084 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12085 if (symtree && (symtree->n.sym->generic ||
12086 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12087 && sym->ns->construct_entities)))
12088 {
12089 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12090 sym->name);
12091 gfc_release_symbol (sym);
12092 symtree->n.sym->refs++;
12093 this_symtree->n.sym = symtree->n.sym;
12094 return;
12095 }
12096 }
12097
12098 /* Otherwise give it a flavor according to such attributes as
12099 it has. */
12100 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
12101 sym->attr.flavor = FL_VARIABLE;
12102 else
12103 {
12104 sym->attr.flavor = FL_PROCEDURE;
12105 if (sym->attr.dimension)
12106 sym->attr.function = 1;
12107 }
12108 }
12109
12110 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12111 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12112
12113 if (sym->attr.procedure && sym->ts.interface
12114 && sym->attr.if_source != IFSRC_DECL
12115 && resolve_procedure_interface (sym) == FAILURE)
12116 return;
12117
12118 if (sym->attr.is_protected && !sym->attr.proc_pointer
12119 && (sym->attr.procedure || sym->attr.external))
12120 {
12121 if (sym->attr.external)
12122 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12123 "at %L", &sym->declared_at);
12124 else
12125 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12126 "at %L", &sym->declared_at);
12127
12128 return;
12129 }
12130
12131
12132 /* F2008, C530. */
12133 if (sym->attr.contiguous
12134 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
12135 && !sym->attr.pointer)))
12136 {
12137 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12138 "array pointer or an assumed-shape array", sym->name,
12139 &sym->declared_at);
12140 return;
12141 }
12142
12143 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
12144 return;
12145
12146 /* Symbols that are module procedures with results (functions) have
12147 the types and array specification copied for type checking in
12148 procedures that call them, as well as for saving to a module
12149 file. These symbols can't stand the scrutiny that their results
12150 can. */
12151 mp_flag = (sym->result != NULL && sym->result != sym);
12152
12153 /* Make sure that the intrinsic is consistent with its internal
12154 representation. This needs to be done before assigning a default
12155 type to avoid spurious warnings. */
12156 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12157 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
12158 return;
12159
12160 /* Resolve associate names. */
12161 if (sym->assoc)
12162 resolve_assoc_var (sym, true);
12163
12164 /* Assign default type to symbols that need one and don't have one. */
12165 if (sym->ts.type == BT_UNKNOWN)
12166 {
12167 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12168 gfc_set_default_type (sym, 1, NULL);
12169
12170 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12171 && !sym->attr.function && !sym->attr.subroutine
12172 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12173 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12174
12175 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12176 {
12177 /* The specific case of an external procedure should emit an error
12178 in the case that there is no implicit type. */
12179 if (!mp_flag)
12180 gfc_set_default_type (sym, sym->attr.external, NULL);
12181 else
12182 {
12183 /* Result may be in another namespace. */
12184 resolve_symbol (sym->result);
12185
12186 if (!sym->result->attr.proc_pointer)
12187 {
12188 sym->ts = sym->result->ts;
12189 sym->as = gfc_copy_array_spec (sym->result->as);
12190 sym->attr.dimension = sym->result->attr.dimension;
12191 sym->attr.pointer = sym->result->attr.pointer;
12192 sym->attr.allocatable = sym->result->attr.allocatable;
12193 sym->attr.contiguous = sym->result->attr.contiguous;
12194 }
12195 }
12196 }
12197 }
12198 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12199 gfc_resolve_array_spec (sym->result->as, false);
12200
12201 /* Assumed size arrays and assumed shape arrays must be dummy
12202 arguments. Array-spec's of implied-shape should have been resolved to
12203 AS_EXPLICIT already. */
12204
12205 if (sym->as)
12206 {
12207 gcc_assert (sym->as->type != AS_IMPLIED_SHAPE);
12208 if (((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
12209 || sym->as->type == AS_ASSUMED_SHAPE)
12210 && sym->attr.dummy == 0)
12211 {
12212 if (sym->as->type == AS_ASSUMED_SIZE)
12213 gfc_error ("Assumed size array at %L must be a dummy argument",
12214 &sym->declared_at);
12215 else
12216 gfc_error ("Assumed shape array at %L must be a dummy argument",
12217 &sym->declared_at);
12218 return;
12219 }
12220 }
12221
12222 /* Make sure symbols with known intent or optional are really dummy
12223 variable. Because of ENTRY statement, this has to be deferred
12224 until resolution time. */
12225
12226 if (!sym->attr.dummy
12227 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12228 {
12229 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12230 return;
12231 }
12232
12233 if (sym->attr.value && !sym->attr.dummy)
12234 {
12235 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12236 "it is not a dummy argument", sym->name, &sym->declared_at);
12237 return;
12238 }
12239
12240 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12241 {
12242 gfc_charlen *cl = sym->ts.u.cl;
12243 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12244 {
12245 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12246 "attribute must have constant length",
12247 sym->name, &sym->declared_at);
12248 return;
12249 }
12250
12251 if (sym->ts.is_c_interop
12252 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12253 {
12254 gfc_error ("C interoperable character dummy variable '%s' at %L "
12255 "with VALUE attribute must have length one",
12256 sym->name, &sym->declared_at);
12257 return;
12258 }
12259 }
12260
12261 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12262 do this for something that was implicitly typed because that is handled
12263 in gfc_set_default_type. Handle dummy arguments and procedure
12264 definitions separately. Also, anything that is use associated is not
12265 handled here but instead is handled in the module it is declared in.
12266 Finally, derived type definitions are allowed to be BIND(C) since that
12267 only implies that they're interoperable, and they are checked fully for
12268 interoperability when a variable is declared of that type. */
12269 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12270 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12271 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12272 {
12273 gfc_try t = SUCCESS;
12274
12275 /* First, make sure the variable is declared at the
12276 module-level scope (J3/04-007, Section 15.3). */
12277 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12278 sym->attr.in_common == 0)
12279 {
12280 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12281 "is neither a COMMON block nor declared at the "
12282 "module level scope", sym->name, &(sym->declared_at));
12283 t = FAILURE;
12284 }
12285 else if (sym->common_head != NULL)
12286 {
12287 t = verify_com_block_vars_c_interop (sym->common_head);
12288 }
12289 else
12290 {
12291 /* If type() declaration, we need to verify that the components
12292 of the given type are all C interoperable, etc. */
12293 if (sym->ts.type == BT_DERIVED &&
12294 sym->ts.u.derived->attr.is_c_interop != 1)
12295 {
12296 /* Make sure the user marked the derived type as BIND(C). If
12297 not, call the verify routine. This could print an error
12298 for the derived type more than once if multiple variables
12299 of that type are declared. */
12300 if (sym->ts.u.derived->attr.is_bind_c != 1)
12301 verify_bind_c_derived_type (sym->ts.u.derived);
12302 t = FAILURE;
12303 }
12304
12305 /* Verify the variable itself as C interoperable if it
12306 is BIND(C). It is not possible for this to succeed if
12307 the verify_bind_c_derived_type failed, so don't have to handle
12308 any error returned by verify_bind_c_derived_type. */
12309 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12310 sym->common_block);
12311 }
12312
12313 if (t == FAILURE)
12314 {
12315 /* clear the is_bind_c flag to prevent reporting errors more than
12316 once if something failed. */
12317 sym->attr.is_bind_c = 0;
12318 return;
12319 }
12320 }
12321
12322 /* If a derived type symbol has reached this point, without its
12323 type being declared, we have an error. Notice that most
12324 conditions that produce undefined derived types have already
12325 been dealt with. However, the likes of:
12326 implicit type(t) (t) ..... call foo (t) will get us here if
12327 the type is not declared in the scope of the implicit
12328 statement. Change the type to BT_UNKNOWN, both because it is so
12329 and to prevent an ICE. */
12330 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
12331 && !sym->ts.u.derived->attr.zero_comp)
12332 {
12333 gfc_error ("The derived type '%s' at %L is of type '%s', "
12334 "which has not been defined", sym->name,
12335 &sym->declared_at, sym->ts.u.derived->name);
12336 sym->ts.type = BT_UNKNOWN;
12337 return;
12338 }
12339
12340 /* Make sure that the derived type has been resolved and that the
12341 derived type is visible in the symbol's namespace, if it is a
12342 module function and is not PRIVATE. */
12343 if (sym->ts.type == BT_DERIVED
12344 && sym->ts.u.derived->attr.use_assoc
12345 && sym->ns->proc_name
12346 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12347 {
12348 gfc_symbol *ds;
12349
12350 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12351 return;
12352
12353 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
12354 if (!ds && sym->attr.function && gfc_check_symbol_access (sym))
12355 {
12356 symtree = gfc_new_symtree (&sym->ns->sym_root,
12357 sym->ts.u.derived->name);
12358 symtree->n.sym = sym->ts.u.derived;
12359 sym->ts.u.derived->refs++;
12360 }
12361 }
12362
12363 /* Unless the derived-type declaration is use associated, Fortran 95
12364 does not allow public entries of private derived types.
12365 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12366 161 in 95-006r3. */
12367 if (sym->ts.type == BT_DERIVED
12368 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12369 && !sym->ts.u.derived->attr.use_assoc
12370 && gfc_check_symbol_access (sym)
12371 && !gfc_check_symbol_access (sym->ts.u.derived)
12372 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12373 "of PRIVATE derived type '%s'",
12374 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12375 : "variable", sym->name, &sym->declared_at,
12376 sym->ts.u.derived->name) == FAILURE)
12377 return;
12378
12379 /* F2008, C1302. */
12380 if (sym->ts.type == BT_DERIVED
12381 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
12382 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
12383 && !sym->attr.codimension)
12384 {
12385 gfc_error ("Variable '%s' at %L of type LOCK_TYPE must be a coarray",
12386 sym->name, &sym->declared_at);
12387 return;
12388 }
12389
12390 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12391 default initialization is defined (5.1.2.4.4). */
12392 if (sym->ts.type == BT_DERIVED
12393 && sym->attr.dummy
12394 && sym->attr.intent == INTENT_OUT
12395 && sym->as
12396 && sym->as->type == AS_ASSUMED_SIZE)
12397 {
12398 for (c = sym->ts.u.derived->components; c; c = c->next)
12399 {
12400 if (c->initializer)
12401 {
12402 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12403 "ASSUMED SIZE and so cannot have a default initializer",
12404 sym->name, &sym->declared_at);
12405 return;
12406 }
12407 }
12408 }
12409
12410 /* F2008, C542. */
12411 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
12412 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
12413 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
12414 "INTENT(OUT)", sym->name, &sym->declared_at);
12415
12416 /* F2008, C526. */
12417 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12418 || sym->attr.codimension)
12419 && sym->attr.result)
12420 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12421 "a coarray component", sym->name, &sym->declared_at);
12422
12423 /* F2008, C524. */
12424 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12425 && sym->ts.u.derived->ts.is_iso_c)
12426 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12427 "shall not be a coarray", sym->name, &sym->declared_at);
12428
12429 /* F2008, C525. */
12430 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
12431 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
12432 || sym->attr.allocatable))
12433 gfc_error ("Variable '%s' at %L with coarray component "
12434 "shall be a nonpointer, nonallocatable scalar",
12435 sym->name, &sym->declared_at);
12436
12437 /* F2008, C526. The function-result case was handled above. */
12438 if (sym->attr.codimension
12439 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
12440 || sym->ns->save_all
12441 || sym->ns->proc_name->attr.flavor == FL_MODULE
12442 || sym->ns->proc_name->attr.is_main_program
12443 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12444 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
12445 "nor a dummy argument", sym->name, &sym->declared_at);
12446 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12447 else if (sym->attr.codimension && !sym->attr.allocatable
12448 && sym->as && sym->as->cotype == AS_DEFERRED)
12449 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12450 "deferred shape", sym->name, &sym->declared_at);
12451 else if (sym->attr.codimension && sym->attr.allocatable
12452 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
12453 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12454 "deferred shape", sym->name, &sym->declared_at);
12455
12456
12457 /* F2008, C541. */
12458 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12459 || (sym->attr.codimension && sym->attr.allocatable))
12460 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12461 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12462 "allocatable coarray or have coarray components",
12463 sym->name, &sym->declared_at);
12464
12465 if (sym->attr.codimension && sym->attr.dummy
12466 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12467 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12468 "procedure '%s'", sym->name, &sym->declared_at,
12469 sym->ns->proc_name->name);
12470
12471 switch (sym->attr.flavor)
12472 {
12473 case FL_VARIABLE:
12474 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12475 return;
12476 break;
12477
12478 case FL_PROCEDURE:
12479 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12480 return;
12481 break;
12482
12483 case FL_NAMELIST:
12484 if (resolve_fl_namelist (sym) == FAILURE)
12485 return;
12486 break;
12487
12488 case FL_PARAMETER:
12489 if (resolve_fl_parameter (sym) == FAILURE)
12490 return;
12491 break;
12492
12493 default:
12494 break;
12495 }
12496
12497 /* Resolve array specifier. Check as well some constraints
12498 on COMMON blocks. */
12499
12500 check_constant = sym->attr.in_common && !sym->attr.pointer;
12501
12502 /* Set the formal_arg_flag so that check_conflict will not throw
12503 an error for host associated variables in the specification
12504 expression for an array_valued function. */
12505 if (sym->attr.function && sym->as)
12506 formal_arg_flag = 1;
12507
12508 gfc_resolve_array_spec (sym->as, check_constant);
12509
12510 formal_arg_flag = 0;
12511
12512 /* Resolve formal namespaces. */
12513 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12514 && !sym->attr.contained && !sym->attr.intrinsic)
12515 gfc_resolve (sym->formal_ns);
12516
12517 /* Make sure the formal namespace is present. */
12518 if (sym->formal && !sym->formal_ns)
12519 {
12520 gfc_formal_arglist *formal = sym->formal;
12521 while (formal && !formal->sym)
12522 formal = formal->next;
12523
12524 if (formal)
12525 {
12526 sym->formal_ns = formal->sym->ns;
12527 sym->formal_ns->refs++;
12528 }
12529 }
12530
12531 /* Check threadprivate restrictions. */
12532 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12533 && (!sym->attr.in_common
12534 && sym->module == NULL
12535 && (sym->ns->proc_name == NULL
12536 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12537 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12538
12539 /* If we have come this far we can apply default-initializers, as
12540 described in 14.7.5, to those variables that have not already
12541 been assigned one. */
12542 if (sym->ts.type == BT_DERIVED
12543 && sym->ns == gfc_current_ns
12544 && !sym->value
12545 && !sym->attr.allocatable
12546 && !sym->attr.alloc_comp)
12547 {
12548 symbol_attribute *a = &sym->attr;
12549
12550 if ((!a->save && !a->dummy && !a->pointer
12551 && !a->in_common && !a->use_assoc
12552 && (a->referenced || a->result)
12553 && !(a->function && sym != sym->result))
12554 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12555 apply_default_init (sym);
12556 }
12557
12558 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12559 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12560 && !CLASS_DATA (sym)->attr.class_pointer
12561 && !CLASS_DATA (sym)->attr.allocatable)
12562 apply_default_init (sym);
12563
12564 /* If this symbol has a type-spec, check it. */
12565 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12566 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12567 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12568 == FAILURE)
12569 return;
12570 }
12571
12572
12573 /************* Resolve DATA statements *************/
12574
12575 static struct
12576 {
12577 gfc_data_value *vnode;
12578 mpz_t left;
12579 }
12580 values;
12581
12582
12583 /* Advance the values structure to point to the next value in the data list. */
12584
12585 static gfc_try
12586 next_data_value (void)
12587 {
12588 while (mpz_cmp_ui (values.left, 0) == 0)
12589 {
12590
12591 if (values.vnode->next == NULL)
12592 return FAILURE;
12593
12594 values.vnode = values.vnode->next;
12595 mpz_set (values.left, values.vnode->repeat);
12596 }
12597
12598 return SUCCESS;
12599 }
12600
12601
12602 static gfc_try
12603 check_data_variable (gfc_data_variable *var, locus *where)
12604 {
12605 gfc_expr *e;
12606 mpz_t size;
12607 mpz_t offset;
12608 gfc_try t;
12609 ar_type mark = AR_UNKNOWN;
12610 int i;
12611 mpz_t section_index[GFC_MAX_DIMENSIONS];
12612 gfc_ref *ref;
12613 gfc_array_ref *ar;
12614 gfc_symbol *sym;
12615 int has_pointer;
12616
12617 if (gfc_resolve_expr (var->expr) == FAILURE)
12618 return FAILURE;
12619
12620 ar = NULL;
12621 mpz_init_set_si (offset, 0);
12622 e = var->expr;
12623
12624 if (e->expr_type != EXPR_VARIABLE)
12625 gfc_internal_error ("check_data_variable(): Bad expression");
12626
12627 sym = e->symtree->n.sym;
12628
12629 if (sym->ns->is_block_data && !sym->attr.in_common)
12630 {
12631 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12632 sym->name, &sym->declared_at);
12633 }
12634
12635 if (e->ref == NULL && sym->as)
12636 {
12637 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12638 " declaration", sym->name, where);
12639 return FAILURE;
12640 }
12641
12642 has_pointer = sym->attr.pointer;
12643
12644 if (gfc_is_coindexed (e))
12645 {
12646 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
12647 where);
12648 return FAILURE;
12649 }
12650
12651 for (ref = e->ref; ref; ref = ref->next)
12652 {
12653 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12654 has_pointer = 1;
12655
12656 if (has_pointer
12657 && ref->type == REF_ARRAY
12658 && ref->u.ar.type != AR_FULL)
12659 {
12660 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12661 "be a full array", sym->name, where);
12662 return FAILURE;
12663 }
12664 }
12665
12666 if (e->rank == 0 || has_pointer)
12667 {
12668 mpz_init_set_ui (size, 1);
12669 ref = NULL;
12670 }
12671 else
12672 {
12673 ref = e->ref;
12674
12675 /* Find the array section reference. */
12676 for (ref = e->ref; ref; ref = ref->next)
12677 {
12678 if (ref->type != REF_ARRAY)
12679 continue;
12680 if (ref->u.ar.type == AR_ELEMENT)
12681 continue;
12682 break;
12683 }
12684 gcc_assert (ref);
12685
12686 /* Set marks according to the reference pattern. */
12687 switch (ref->u.ar.type)
12688 {
12689 case AR_FULL:
12690 mark = AR_FULL;
12691 break;
12692
12693 case AR_SECTION:
12694 ar = &ref->u.ar;
12695 /* Get the start position of array section. */
12696 gfc_get_section_index (ar, section_index, &offset);
12697 mark = AR_SECTION;
12698 break;
12699
12700 default:
12701 gcc_unreachable ();
12702 }
12703
12704 if (gfc_array_size (e, &size) == FAILURE)
12705 {
12706 gfc_error ("Nonconstant array section at %L in DATA statement",
12707 &e->where);
12708 mpz_clear (offset);
12709 return FAILURE;
12710 }
12711 }
12712
12713 t = SUCCESS;
12714
12715 while (mpz_cmp_ui (size, 0) > 0)
12716 {
12717 if (next_data_value () == FAILURE)
12718 {
12719 gfc_error ("DATA statement at %L has more variables than values",
12720 where);
12721 t = FAILURE;
12722 break;
12723 }
12724
12725 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12726 if (t == FAILURE)
12727 break;
12728
12729 /* If we have more than one element left in the repeat count,
12730 and we have more than one element left in the target variable,
12731 then create a range assignment. */
12732 /* FIXME: Only done for full arrays for now, since array sections
12733 seem tricky. */
12734 if (mark == AR_FULL && ref && ref->next == NULL
12735 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12736 {
12737 mpz_t range;
12738
12739 if (mpz_cmp (size, values.left) >= 0)
12740 {
12741 mpz_init_set (range, values.left);
12742 mpz_sub (size, size, values.left);
12743 mpz_set_ui (values.left, 0);
12744 }
12745 else
12746 {
12747 mpz_init_set (range, size);
12748 mpz_sub (values.left, values.left, size);
12749 mpz_set_ui (size, 0);
12750 }
12751
12752 t = gfc_assign_data_value (var->expr, values.vnode->expr,
12753 offset, &range);
12754
12755 mpz_add (offset, offset, range);
12756 mpz_clear (range);
12757
12758 if (t == FAILURE)
12759 break;
12760 }
12761
12762 /* Assign initial value to symbol. */
12763 else
12764 {
12765 mpz_sub_ui (values.left, values.left, 1);
12766 mpz_sub_ui (size, size, 1);
12767
12768 t = gfc_assign_data_value (var->expr, values.vnode->expr,
12769 offset, NULL);
12770 if (t == FAILURE)
12771 break;
12772
12773 if (mark == AR_FULL)
12774 mpz_add_ui (offset, offset, 1);
12775
12776 /* Modify the array section indexes and recalculate the offset
12777 for next element. */
12778 else if (mark == AR_SECTION)
12779 gfc_advance_section (section_index, ar, &offset);
12780 }
12781 }
12782
12783 if (mark == AR_SECTION)
12784 {
12785 for (i = 0; i < ar->dimen; i++)
12786 mpz_clear (section_index[i]);
12787 }
12788
12789 mpz_clear (size);
12790 mpz_clear (offset);
12791
12792 return t;
12793 }
12794
12795
12796 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12797
12798 /* Iterate over a list of elements in a DATA statement. */
12799
12800 static gfc_try
12801 traverse_data_list (gfc_data_variable *var, locus *where)
12802 {
12803 mpz_t trip;
12804 iterator_stack frame;
12805 gfc_expr *e, *start, *end, *step;
12806 gfc_try retval = SUCCESS;
12807
12808 mpz_init (frame.value);
12809 mpz_init (trip);
12810
12811 start = gfc_copy_expr (var->iter.start);
12812 end = gfc_copy_expr (var->iter.end);
12813 step = gfc_copy_expr (var->iter.step);
12814
12815 if (gfc_simplify_expr (start, 1) == FAILURE
12816 || start->expr_type != EXPR_CONSTANT)
12817 {
12818 gfc_error ("start of implied-do loop at %L could not be "
12819 "simplified to a constant value", &start->where);
12820 retval = FAILURE;
12821 goto cleanup;
12822 }
12823 if (gfc_simplify_expr (end, 1) == FAILURE
12824 || end->expr_type != EXPR_CONSTANT)
12825 {
12826 gfc_error ("end of implied-do loop at %L could not be "
12827 "simplified to a constant value", &start->where);
12828 retval = FAILURE;
12829 goto cleanup;
12830 }
12831 if (gfc_simplify_expr (step, 1) == FAILURE
12832 || step->expr_type != EXPR_CONSTANT)
12833 {
12834 gfc_error ("step of implied-do loop at %L could not be "
12835 "simplified to a constant value", &start->where);
12836 retval = FAILURE;
12837 goto cleanup;
12838 }
12839
12840 mpz_set (trip, end->value.integer);
12841 mpz_sub (trip, trip, start->value.integer);
12842 mpz_add (trip, trip, step->value.integer);
12843
12844 mpz_div (trip, trip, step->value.integer);
12845
12846 mpz_set (frame.value, start->value.integer);
12847
12848 frame.prev = iter_stack;
12849 frame.variable = var->iter.var->symtree;
12850 iter_stack = &frame;
12851
12852 while (mpz_cmp_ui (trip, 0) > 0)
12853 {
12854 if (traverse_data_var (var->list, where) == FAILURE)
12855 {
12856 retval = FAILURE;
12857 goto cleanup;
12858 }
12859
12860 e = gfc_copy_expr (var->expr);
12861 if (gfc_simplify_expr (e, 1) == FAILURE)
12862 {
12863 gfc_free_expr (e);
12864 retval = FAILURE;
12865 goto cleanup;
12866 }
12867
12868 mpz_add (frame.value, frame.value, step->value.integer);
12869
12870 mpz_sub_ui (trip, trip, 1);
12871 }
12872
12873 cleanup:
12874 mpz_clear (frame.value);
12875 mpz_clear (trip);
12876
12877 gfc_free_expr (start);
12878 gfc_free_expr (end);
12879 gfc_free_expr (step);
12880
12881 iter_stack = frame.prev;
12882 return retval;
12883 }
12884
12885
12886 /* Type resolve variables in the variable list of a DATA statement. */
12887
12888 static gfc_try
12889 traverse_data_var (gfc_data_variable *var, locus *where)
12890 {
12891 gfc_try t;
12892
12893 for (; var; var = var->next)
12894 {
12895 if (var->expr == NULL)
12896 t = traverse_data_list (var, where);
12897 else
12898 t = check_data_variable (var, where);
12899
12900 if (t == FAILURE)
12901 return FAILURE;
12902 }
12903
12904 return SUCCESS;
12905 }
12906
12907
12908 /* Resolve the expressions and iterators associated with a data statement.
12909 This is separate from the assignment checking because data lists should
12910 only be resolved once. */
12911
12912 static gfc_try
12913 resolve_data_variables (gfc_data_variable *d)
12914 {
12915 for (; d; d = d->next)
12916 {
12917 if (d->list == NULL)
12918 {
12919 if (gfc_resolve_expr (d->expr) == FAILURE)
12920 return FAILURE;
12921 }
12922 else
12923 {
12924 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12925 return FAILURE;
12926
12927 if (resolve_data_variables (d->list) == FAILURE)
12928 return FAILURE;
12929 }
12930 }
12931
12932 return SUCCESS;
12933 }
12934
12935
12936 /* Resolve a single DATA statement. We implement this by storing a pointer to
12937 the value list into static variables, and then recursively traversing the
12938 variables list, expanding iterators and such. */
12939
12940 static void
12941 resolve_data (gfc_data *d)
12942 {
12943
12944 if (resolve_data_variables (d->var) == FAILURE)
12945 return;
12946
12947 values.vnode = d->value;
12948 if (d->value == NULL)
12949 mpz_set_ui (values.left, 0);
12950 else
12951 mpz_set (values.left, d->value->repeat);
12952
12953 if (traverse_data_var (d->var, &d->where) == FAILURE)
12954 return;
12955
12956 /* At this point, we better not have any values left. */
12957
12958 if (next_data_value () == SUCCESS)
12959 gfc_error ("DATA statement at %L has more values than variables",
12960 &d->where);
12961 }
12962
12963
12964 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12965 accessed by host or use association, is a dummy argument to a pure function,
12966 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12967 is storage associated with any such variable, shall not be used in the
12968 following contexts: (clients of this function). */
12969
12970 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12971 procedure. Returns zero if assignment is OK, nonzero if there is a
12972 problem. */
12973 int
12974 gfc_impure_variable (gfc_symbol *sym)
12975 {
12976 gfc_symbol *proc;
12977 gfc_namespace *ns;
12978
12979 if (sym->attr.use_assoc || sym->attr.in_common)
12980 return 1;
12981
12982 /* Check if the symbol's ns is inside the pure procedure. */
12983 for (ns = gfc_current_ns; ns; ns = ns->parent)
12984 {
12985 if (ns == sym->ns)
12986 break;
12987 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12988 return 1;
12989 }
12990
12991 proc = sym->ns->proc_name;
12992 if (sym->attr.dummy && gfc_pure (proc)
12993 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12994 ||
12995 proc->attr.function))
12996 return 1;
12997
12998 /* TODO: Sort out what can be storage associated, if anything, and include
12999 it here. In principle equivalences should be scanned but it does not
13000 seem to be possible to storage associate an impure variable this way. */
13001 return 0;
13002 }
13003
13004
13005 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13006 current namespace is inside a pure procedure. */
13007
13008 int
13009 gfc_pure (gfc_symbol *sym)
13010 {
13011 symbol_attribute attr;
13012 gfc_namespace *ns;
13013
13014 if (sym == NULL)
13015 {
13016 /* Check if the current namespace or one of its parents
13017 belongs to a pure procedure. */
13018 for (ns = gfc_current_ns; ns; ns = ns->parent)
13019 {
13020 sym = ns->proc_name;
13021 if (sym == NULL)
13022 return 0;
13023 attr = sym->attr;
13024 if (attr.flavor == FL_PROCEDURE && attr.pure)
13025 return 1;
13026 }
13027 return 0;
13028 }
13029
13030 attr = sym->attr;
13031
13032 return attr.flavor == FL_PROCEDURE && attr.pure;
13033 }
13034
13035
13036 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13037 checks if the current namespace is implicitly pure. Note that this
13038 function returns false for a PURE procedure. */
13039
13040 int
13041 gfc_implicit_pure (gfc_symbol *sym)
13042 {
13043 symbol_attribute attr;
13044
13045 if (sym == NULL)
13046 {
13047 /* Check if the current namespace is implicit_pure. */
13048 sym = gfc_current_ns->proc_name;
13049 if (sym == NULL)
13050 return 0;
13051 attr = sym->attr;
13052 if (attr.flavor == FL_PROCEDURE
13053 && attr.implicit_pure && !attr.pure)
13054 return 1;
13055 return 0;
13056 }
13057
13058 attr = sym->attr;
13059
13060 return attr.flavor == FL_PROCEDURE && attr.implicit_pure && !attr.pure;
13061 }
13062
13063
13064 /* Test whether the current procedure is elemental or not. */
13065
13066 int
13067 gfc_elemental (gfc_symbol *sym)
13068 {
13069 symbol_attribute attr;
13070
13071 if (sym == NULL)
13072 sym = gfc_current_ns->proc_name;
13073 if (sym == NULL)
13074 return 0;
13075 attr = sym->attr;
13076
13077 return attr.flavor == FL_PROCEDURE && attr.elemental;
13078 }
13079
13080
13081 /* Warn about unused labels. */
13082
13083 static void
13084 warn_unused_fortran_label (gfc_st_label *label)
13085 {
13086 if (label == NULL)
13087 return;
13088
13089 warn_unused_fortran_label (label->left);
13090
13091 if (label->defined == ST_LABEL_UNKNOWN)
13092 return;
13093
13094 switch (label->referenced)
13095 {
13096 case ST_LABEL_UNKNOWN:
13097 gfc_warning ("Label %d at %L defined but not used", label->value,
13098 &label->where);
13099 break;
13100
13101 case ST_LABEL_BAD_TARGET:
13102 gfc_warning ("Label %d at %L defined but cannot be used",
13103 label->value, &label->where);
13104 break;
13105
13106 default:
13107 break;
13108 }
13109
13110 warn_unused_fortran_label (label->right);
13111 }
13112
13113
13114 /* Returns the sequence type of a symbol or sequence. */
13115
13116 static seq_type
13117 sequence_type (gfc_typespec ts)
13118 {
13119 seq_type result;
13120 gfc_component *c;
13121
13122 switch (ts.type)
13123 {
13124 case BT_DERIVED:
13125
13126 if (ts.u.derived->components == NULL)
13127 return SEQ_NONDEFAULT;
13128
13129 result = sequence_type (ts.u.derived->components->ts);
13130 for (c = ts.u.derived->components->next; c; c = c->next)
13131 if (sequence_type (c->ts) != result)
13132 return SEQ_MIXED;
13133
13134 return result;
13135
13136 case BT_CHARACTER:
13137 if (ts.kind != gfc_default_character_kind)
13138 return SEQ_NONDEFAULT;
13139
13140 return SEQ_CHARACTER;
13141
13142 case BT_INTEGER:
13143 if (ts.kind != gfc_default_integer_kind)
13144 return SEQ_NONDEFAULT;
13145
13146 return SEQ_NUMERIC;
13147
13148 case BT_REAL:
13149 if (!(ts.kind == gfc_default_real_kind
13150 || ts.kind == gfc_default_double_kind))
13151 return SEQ_NONDEFAULT;
13152
13153 return SEQ_NUMERIC;
13154
13155 case BT_COMPLEX:
13156 if (ts.kind != gfc_default_complex_kind)
13157 return SEQ_NONDEFAULT;
13158
13159 return SEQ_NUMERIC;
13160
13161 case BT_LOGICAL:
13162 if (ts.kind != gfc_default_logical_kind)
13163 return SEQ_NONDEFAULT;
13164
13165 return SEQ_NUMERIC;
13166
13167 default:
13168 return SEQ_NONDEFAULT;
13169 }
13170 }
13171
13172
13173 /* Resolve derived type EQUIVALENCE object. */
13174
13175 static gfc_try
13176 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13177 {
13178 gfc_component *c = derived->components;
13179
13180 if (!derived)
13181 return SUCCESS;
13182
13183 /* Shall not be an object of nonsequence derived type. */
13184 if (!derived->attr.sequence)
13185 {
13186 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13187 "attribute to be an EQUIVALENCE object", sym->name,
13188 &e->where);
13189 return FAILURE;
13190 }
13191
13192 /* Shall not have allocatable components. */
13193 if (derived->attr.alloc_comp)
13194 {
13195 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13196 "components to be an EQUIVALENCE object",sym->name,
13197 &e->where);
13198 return FAILURE;
13199 }
13200
13201 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13202 {
13203 gfc_error ("Derived type variable '%s' at %L with default "
13204 "initialization cannot be in EQUIVALENCE with a variable "
13205 "in COMMON", sym->name, &e->where);
13206 return FAILURE;
13207 }
13208
13209 for (; c ; c = c->next)
13210 {
13211 if (c->ts.type == BT_DERIVED
13212 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
13213 return FAILURE;
13214
13215 /* Shall not be an object of sequence derived type containing a pointer
13216 in the structure. */
13217 if (c->attr.pointer)
13218 {
13219 gfc_error ("Derived type variable '%s' at %L with pointer "
13220 "component(s) cannot be an EQUIVALENCE object",
13221 sym->name, &e->where);
13222 return FAILURE;
13223 }
13224 }
13225 return SUCCESS;
13226 }
13227
13228
13229 /* Resolve equivalence object.
13230 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13231 an allocatable array, an object of nonsequence derived type, an object of
13232 sequence derived type containing a pointer at any level of component
13233 selection, an automatic object, a function name, an entry name, a result
13234 name, a named constant, a structure component, or a subobject of any of
13235 the preceding objects. A substring shall not have length zero. A
13236 derived type shall not have components with default initialization nor
13237 shall two objects of an equivalence group be initialized.
13238 Either all or none of the objects shall have an protected attribute.
13239 The simple constraints are done in symbol.c(check_conflict) and the rest
13240 are implemented here. */
13241
13242 static void
13243 resolve_equivalence (gfc_equiv *eq)
13244 {
13245 gfc_symbol *sym;
13246 gfc_symbol *first_sym;
13247 gfc_expr *e;
13248 gfc_ref *r;
13249 locus *last_where = NULL;
13250 seq_type eq_type, last_eq_type;
13251 gfc_typespec *last_ts;
13252 int object, cnt_protected;
13253 const char *msg;
13254
13255 last_ts = &eq->expr->symtree->n.sym->ts;
13256
13257 first_sym = eq->expr->symtree->n.sym;
13258
13259 cnt_protected = 0;
13260
13261 for (object = 1; eq; eq = eq->eq, object++)
13262 {
13263 e = eq->expr;
13264
13265 e->ts = e->symtree->n.sym->ts;
13266 /* match_varspec might not know yet if it is seeing
13267 array reference or substring reference, as it doesn't
13268 know the types. */
13269 if (e->ref && e->ref->type == REF_ARRAY)
13270 {
13271 gfc_ref *ref = e->ref;
13272 sym = e->symtree->n.sym;
13273
13274 if (sym->attr.dimension)
13275 {
13276 ref->u.ar.as = sym->as;
13277 ref = ref->next;
13278 }
13279
13280 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13281 if (e->ts.type == BT_CHARACTER
13282 && ref
13283 && ref->type == REF_ARRAY
13284 && ref->u.ar.dimen == 1
13285 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13286 && ref->u.ar.stride[0] == NULL)
13287 {
13288 gfc_expr *start = ref->u.ar.start[0];
13289 gfc_expr *end = ref->u.ar.end[0];
13290 void *mem = NULL;
13291
13292 /* Optimize away the (:) reference. */
13293 if (start == NULL && end == NULL)
13294 {
13295 if (e->ref == ref)
13296 e->ref = ref->next;
13297 else
13298 e->ref->next = ref->next;
13299 mem = ref;
13300 }
13301 else
13302 {
13303 ref->type = REF_SUBSTRING;
13304 if (start == NULL)
13305 start = gfc_get_int_expr (gfc_default_integer_kind,
13306 NULL, 1);
13307 ref->u.ss.start = start;
13308 if (end == NULL && e->ts.u.cl)
13309 end = gfc_copy_expr (e->ts.u.cl->length);
13310 ref->u.ss.end = end;
13311 ref->u.ss.length = e->ts.u.cl;
13312 e->ts.u.cl = NULL;
13313 }
13314 ref = ref->next;
13315 free (mem);
13316 }
13317
13318 /* Any further ref is an error. */
13319 if (ref)
13320 {
13321 gcc_assert (ref->type == REF_ARRAY);
13322 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13323 &ref->u.ar.where);
13324 continue;
13325 }
13326 }
13327
13328 if (gfc_resolve_expr (e) == FAILURE)
13329 continue;
13330
13331 sym = e->symtree->n.sym;
13332
13333 if (sym->attr.is_protected)
13334 cnt_protected++;
13335 if (cnt_protected > 0 && cnt_protected != object)
13336 {
13337 gfc_error ("Either all or none of the objects in the "
13338 "EQUIVALENCE set at %L shall have the "
13339 "PROTECTED attribute",
13340 &e->where);
13341 break;
13342 }
13343
13344 /* Shall not equivalence common block variables in a PURE procedure. */
13345 if (sym->ns->proc_name
13346 && sym->ns->proc_name->attr.pure
13347 && sym->attr.in_common)
13348 {
13349 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13350 "object in the pure procedure '%s'",
13351 sym->name, &e->where, sym->ns->proc_name->name);
13352 break;
13353 }
13354
13355 /* Shall not be a named constant. */
13356 if (e->expr_type == EXPR_CONSTANT)
13357 {
13358 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13359 "object", sym->name, &e->where);
13360 continue;
13361 }
13362
13363 if (e->ts.type == BT_DERIVED
13364 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13365 continue;
13366
13367 /* Check that the types correspond correctly:
13368 Note 5.28:
13369 A numeric sequence structure may be equivalenced to another sequence
13370 structure, an object of default integer type, default real type, double
13371 precision real type, default logical type such that components of the
13372 structure ultimately only become associated to objects of the same
13373 kind. A character sequence structure may be equivalenced to an object
13374 of default character kind or another character sequence structure.
13375 Other objects may be equivalenced only to objects of the same type and
13376 kind parameters. */
13377
13378 /* Identical types are unconditionally OK. */
13379 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13380 goto identical_types;
13381
13382 last_eq_type = sequence_type (*last_ts);
13383 eq_type = sequence_type (sym->ts);
13384
13385 /* Since the pair of objects is not of the same type, mixed or
13386 non-default sequences can be rejected. */
13387
13388 msg = "Sequence %s with mixed components in EQUIVALENCE "
13389 "statement at %L with different type objects";
13390 if ((object ==2
13391 && last_eq_type == SEQ_MIXED
13392 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13393 == FAILURE)
13394 || (eq_type == SEQ_MIXED
13395 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13396 &e->where) == FAILURE))
13397 continue;
13398
13399 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13400 "statement at %L with objects of different type";
13401 if ((object ==2
13402 && last_eq_type == SEQ_NONDEFAULT
13403 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13404 last_where) == FAILURE)
13405 || (eq_type == SEQ_NONDEFAULT
13406 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13407 &e->where) == FAILURE))
13408 continue;
13409
13410 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13411 "EQUIVALENCE statement at %L";
13412 if (last_eq_type == SEQ_CHARACTER
13413 && eq_type != SEQ_CHARACTER
13414 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13415 &e->where) == FAILURE)
13416 continue;
13417
13418 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13419 "EQUIVALENCE statement at %L";
13420 if (last_eq_type == SEQ_NUMERIC
13421 && eq_type != SEQ_NUMERIC
13422 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13423 &e->where) == FAILURE)
13424 continue;
13425
13426 identical_types:
13427 last_ts =&sym->ts;
13428 last_where = &e->where;
13429
13430 if (!e->ref)
13431 continue;
13432
13433 /* Shall not be an automatic array. */
13434 if (e->ref->type == REF_ARRAY
13435 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13436 {
13437 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13438 "an EQUIVALENCE object", sym->name, &e->where);
13439 continue;
13440 }
13441
13442 r = e->ref;
13443 while (r)
13444 {
13445 /* Shall not be a structure component. */
13446 if (r->type == REF_COMPONENT)
13447 {
13448 gfc_error ("Structure component '%s' at %L cannot be an "
13449 "EQUIVALENCE object",
13450 r->u.c.component->name, &e->where);
13451 break;
13452 }
13453
13454 /* A substring shall not have length zero. */
13455 if (r->type == REF_SUBSTRING)
13456 {
13457 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13458 {
13459 gfc_error ("Substring at %L has length zero",
13460 &r->u.ss.start->where);
13461 break;
13462 }
13463 }
13464 r = r->next;
13465 }
13466 }
13467 }
13468
13469
13470 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13471
13472 static void
13473 resolve_fntype (gfc_namespace *ns)
13474 {
13475 gfc_entry_list *el;
13476 gfc_symbol *sym;
13477
13478 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13479 return;
13480
13481 /* If there are any entries, ns->proc_name is the entry master
13482 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13483 if (ns->entries)
13484 sym = ns->entries->sym;
13485 else
13486 sym = ns->proc_name;
13487 if (sym->result == sym
13488 && sym->ts.type == BT_UNKNOWN
13489 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13490 && !sym->attr.untyped)
13491 {
13492 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13493 sym->name, &sym->declared_at);
13494 sym->attr.untyped = 1;
13495 }
13496
13497 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13498 && !sym->attr.contained
13499 && !gfc_check_symbol_access (sym->ts.u.derived)
13500 && gfc_check_symbol_access (sym))
13501 {
13502 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13503 "%L of PRIVATE type '%s'", sym->name,
13504 &sym->declared_at, sym->ts.u.derived->name);
13505 }
13506
13507 if (ns->entries)
13508 for (el = ns->entries->next; el; el = el->next)
13509 {
13510 if (el->sym->result == el->sym
13511 && el->sym->ts.type == BT_UNKNOWN
13512 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13513 && !el->sym->attr.untyped)
13514 {
13515 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13516 el->sym->name, &el->sym->declared_at);
13517 el->sym->attr.untyped = 1;
13518 }
13519 }
13520 }
13521
13522
13523 /* 12.3.2.1.1 Defined operators. */
13524
13525 static gfc_try
13526 check_uop_procedure (gfc_symbol *sym, locus where)
13527 {
13528 gfc_formal_arglist *formal;
13529
13530 if (!sym->attr.function)
13531 {
13532 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13533 sym->name, &where);
13534 return FAILURE;
13535 }
13536
13537 if (sym->ts.type == BT_CHARACTER
13538 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13539 && !(sym->result && sym->result->ts.u.cl
13540 && sym->result->ts.u.cl->length))
13541 {
13542 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13543 "character length", sym->name, &where);
13544 return FAILURE;
13545 }
13546
13547 formal = sym->formal;
13548 if (!formal || !formal->sym)
13549 {
13550 gfc_error ("User operator procedure '%s' at %L must have at least "
13551 "one argument", sym->name, &where);
13552 return FAILURE;
13553 }
13554
13555 if (formal->sym->attr.intent != INTENT_IN)
13556 {
13557 gfc_error ("First argument of operator interface at %L must be "
13558 "INTENT(IN)", &where);
13559 return FAILURE;
13560 }
13561
13562 if (formal->sym->attr.optional)
13563 {
13564 gfc_error ("First argument of operator interface at %L cannot be "
13565 "optional", &where);
13566 return FAILURE;
13567 }
13568
13569 formal = formal->next;
13570 if (!formal || !formal->sym)
13571 return SUCCESS;
13572
13573 if (formal->sym->attr.intent != INTENT_IN)
13574 {
13575 gfc_error ("Second argument of operator interface at %L must be "
13576 "INTENT(IN)", &where);
13577 return FAILURE;
13578 }
13579
13580 if (formal->sym->attr.optional)
13581 {
13582 gfc_error ("Second argument of operator interface at %L cannot be "
13583 "optional", &where);
13584 return FAILURE;
13585 }
13586
13587 if (formal->next)
13588 {
13589 gfc_error ("Operator interface at %L must have, at most, two "
13590 "arguments", &where);
13591 return FAILURE;
13592 }
13593
13594 return SUCCESS;
13595 }
13596
13597 static void
13598 gfc_resolve_uops (gfc_symtree *symtree)
13599 {
13600 gfc_interface *itr;
13601
13602 if (symtree == NULL)
13603 return;
13604
13605 gfc_resolve_uops (symtree->left);
13606 gfc_resolve_uops (symtree->right);
13607
13608 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13609 check_uop_procedure (itr->sym, itr->sym->declared_at);
13610 }
13611
13612
13613 /* Examine all of the expressions associated with a program unit,
13614 assign types to all intermediate expressions, make sure that all
13615 assignments are to compatible types and figure out which names
13616 refer to which functions or subroutines. It doesn't check code
13617 block, which is handled by resolve_code. */
13618
13619 static void
13620 resolve_types (gfc_namespace *ns)
13621 {
13622 gfc_namespace *n;
13623 gfc_charlen *cl;
13624 gfc_data *d;
13625 gfc_equiv *eq;
13626 gfc_namespace* old_ns = gfc_current_ns;
13627
13628 /* Check that all IMPLICIT types are ok. */
13629 if (!ns->seen_implicit_none)
13630 {
13631 unsigned letter;
13632 for (letter = 0; letter != GFC_LETTERS; ++letter)
13633 if (ns->set_flag[letter]
13634 && resolve_typespec_used (&ns->default_type[letter],
13635 &ns->implicit_loc[letter],
13636 NULL) == FAILURE)
13637 return;
13638 }
13639
13640 gfc_current_ns = ns;
13641
13642 resolve_entries (ns);
13643
13644 resolve_common_vars (ns->blank_common.head, false);
13645 resolve_common_blocks (ns->common_root);
13646
13647 resolve_contained_functions (ns);
13648
13649 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
13650 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
13651 resolve_formal_arglist (ns->proc_name);
13652
13653 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13654
13655 for (cl = ns->cl_list; cl; cl = cl->next)
13656 resolve_charlen (cl);
13657
13658 gfc_traverse_ns (ns, resolve_symbol);
13659
13660 resolve_fntype (ns);
13661
13662 for (n = ns->contained; n; n = n->sibling)
13663 {
13664 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13665 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13666 "also be PURE", n->proc_name->name,
13667 &n->proc_name->declared_at);
13668
13669 resolve_types (n);
13670 }
13671
13672 forall_flag = 0;
13673 gfc_check_interfaces (ns);
13674
13675 gfc_traverse_ns (ns, resolve_values);
13676
13677 if (ns->save_all)
13678 gfc_save_all (ns);
13679
13680 iter_stack = NULL;
13681 for (d = ns->data; d; d = d->next)
13682 resolve_data (d);
13683
13684 iter_stack = NULL;
13685 gfc_traverse_ns (ns, gfc_formalize_init_value);
13686
13687 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13688
13689 if (ns->common_root != NULL)
13690 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13691
13692 for (eq = ns->equiv; eq; eq = eq->next)
13693 resolve_equivalence (eq);
13694
13695 /* Warn about unused labels. */
13696 if (warn_unused_label)
13697 warn_unused_fortran_label (ns->st_labels);
13698
13699 gfc_resolve_uops (ns->uop_root);
13700
13701 gfc_current_ns = old_ns;
13702 }
13703
13704
13705 /* Call resolve_code recursively. */
13706
13707 static void
13708 resolve_codes (gfc_namespace *ns)
13709 {
13710 gfc_namespace *n;
13711 bitmap_obstack old_obstack;
13712
13713 if (ns->resolved == 1)
13714 return;
13715
13716 for (n = ns->contained; n; n = n->sibling)
13717 resolve_codes (n);
13718
13719 gfc_current_ns = ns;
13720
13721 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13722 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13723 cs_base = NULL;
13724
13725 /* Set to an out of range value. */
13726 current_entry_id = -1;
13727
13728 old_obstack = labels_obstack;
13729 bitmap_obstack_initialize (&labels_obstack);
13730
13731 resolve_code (ns->code, ns);
13732
13733 bitmap_obstack_release (&labels_obstack);
13734 labels_obstack = old_obstack;
13735 }
13736
13737
13738 /* This function is called after a complete program unit has been compiled.
13739 Its purpose is to examine all of the expressions associated with a program
13740 unit, assign types to all intermediate expressions, make sure that all
13741 assignments are to compatible types and figure out which names refer to
13742 which functions or subroutines. */
13743
13744 void
13745 gfc_resolve (gfc_namespace *ns)
13746 {
13747 gfc_namespace *old_ns;
13748 code_stack *old_cs_base;
13749
13750 if (ns->resolved)
13751 return;
13752
13753 ns->resolved = -1;
13754 old_ns = gfc_current_ns;
13755 old_cs_base = cs_base;
13756
13757 resolve_types (ns);
13758 resolve_codes (ns);
13759
13760 gfc_current_ns = old_ns;
13761 cs_base = old_cs_base;
13762 ns->resolved = 1;
13763
13764 gfc_run_passes (ns);
13765 }