re PR fortran/53111 (Derived types cannot be USE-associated again with -std=f95)
[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, 2012
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 or DO CONCURRENT block. */
62
63 static int forall_flag;
64 static int do_concurrent_flag;
65
66 static bool assumed_type_expr_allowed = false;
67
68 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
69
70 static int omp_workshare_flag;
71
72 /* Nonzero if we are processing a formal arglist. The corresponding function
73 resets the flag each time that it is read. */
74 static int formal_arg_flag = 0;
75
76 /* True if we are resolving a specification expression. */
77 static int specification_expr = 0;
78
79 /* The id of the last entry seen. */
80 static int current_entry_id;
81
82 /* We use bitmaps to determine if a branch target is valid. */
83 static bitmap_obstack labels_obstack;
84
85 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
86 static bool inquiry_argument = false;
87
88 int
89 gfc_is_formal_arg (void)
90 {
91 return formal_arg_flag;
92 }
93
94 /* Is the symbol host associated? */
95 static bool
96 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
97 {
98 for (ns = ns->parent; ns; ns = ns->parent)
99 {
100 if (sym->ns == ns)
101 return true;
102 }
103
104 return false;
105 }
106
107 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
108 an ABSTRACT derived-type. If where is not NULL, an error message with that
109 locus is printed, optionally using name. */
110
111 static gfc_try
112 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
113 {
114 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
115 {
116 if (where)
117 {
118 if (name)
119 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
120 name, where, ts->u.derived->name);
121 else
122 gfc_error ("ABSTRACT type '%s' used at %L",
123 ts->u.derived->name, where);
124 }
125
126 return FAILURE;
127 }
128
129 return SUCCESS;
130 }
131
132
133 static void resolve_symbol (gfc_symbol *sym);
134 static gfc_try resolve_intrinsic (gfc_symbol *sym, locus *loc);
135
136
137 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
138
139 static gfc_try
140 resolve_procedure_interface (gfc_symbol *sym)
141 {
142 if (sym->ts.interface == sym)
143 {
144 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
145 sym->name, &sym->declared_at);
146 return FAILURE;
147 }
148 if (sym->ts.interface->attr.procedure)
149 {
150 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
151 "in a later PROCEDURE statement", sym->ts.interface->name,
152 sym->name, &sym->declared_at);
153 return FAILURE;
154 }
155
156 /* Get the attributes from the interface (now resolved). */
157 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
158 {
159 gfc_symbol *ifc = sym->ts.interface;
160 resolve_symbol (ifc);
161
162 if (ifc->attr.intrinsic)
163 resolve_intrinsic (ifc, &ifc->declared_at);
164
165 if (ifc->result)
166 {
167 sym->ts = ifc->result->ts;
168 sym->result = sym;
169 }
170 else
171 sym->ts = ifc->ts;
172 sym->ts.interface = ifc;
173 sym->attr.function = ifc->attr.function;
174 sym->attr.subroutine = ifc->attr.subroutine;
175 gfc_copy_formal_args (sym, ifc);
176
177 sym->attr.allocatable = ifc->attr.allocatable;
178 sym->attr.pointer = ifc->attr.pointer;
179 sym->attr.pure = ifc->attr.pure;
180 sym->attr.elemental = ifc->attr.elemental;
181 sym->attr.dimension = ifc->attr.dimension;
182 sym->attr.contiguous = ifc->attr.contiguous;
183 sym->attr.recursive = ifc->attr.recursive;
184 sym->attr.always_explicit = ifc->attr.always_explicit;
185 sym->attr.ext_attr |= ifc->attr.ext_attr;
186 sym->attr.is_bind_c = ifc->attr.is_bind_c;
187 /* Copy array spec. */
188 sym->as = gfc_copy_array_spec (ifc->as);
189 if (sym->as)
190 {
191 int i;
192 for (i = 0; i < sym->as->rank; i++)
193 {
194 gfc_expr_replace_symbols (sym->as->lower[i], sym);
195 gfc_expr_replace_symbols (sym->as->upper[i], sym);
196 }
197 }
198 /* Copy char length. */
199 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
200 {
201 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
202 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
203 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
204 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
205 return FAILURE;
206 }
207 }
208 else if (sym->ts.interface->name[0] != '\0')
209 {
210 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
211 sym->ts.interface->name, sym->name, &sym->declared_at);
212 return FAILURE;
213 }
214
215 return SUCCESS;
216 }
217
218
219 /* Resolve types of formal argument lists. These have to be done early so that
220 the formal argument lists of module procedures can be copied to the
221 containing module before the individual procedures are resolved
222 individually. We also resolve argument lists of procedures in interface
223 blocks because they are self-contained scoping units.
224
225 Since a dummy argument cannot be a non-dummy procedure, the only
226 resort left for untyped names are the IMPLICIT types. */
227
228 static void
229 resolve_formal_arglist (gfc_symbol *proc)
230 {
231 gfc_formal_arglist *f;
232 gfc_symbol *sym;
233 int i;
234
235 if (proc->result != NULL)
236 sym = proc->result;
237 else
238 sym = proc;
239
240 if (gfc_elemental (proc)
241 || sym->attr.pointer || sym->attr.allocatable
242 || (sym->as && sym->as->rank > 0))
243 {
244 proc->attr.always_explicit = 1;
245 sym->attr.always_explicit = 1;
246 }
247
248 formal_arg_flag = 1;
249
250 for (f = proc->formal; f; f = f->next)
251 {
252 sym = f->sym;
253
254 if (sym == NULL)
255 {
256 /* Alternate return placeholder. */
257 if (gfc_elemental (proc))
258 gfc_error ("Alternate return specifier in elemental subroutine "
259 "'%s' at %L is not allowed", proc->name,
260 &proc->declared_at);
261 if (proc->attr.function)
262 gfc_error ("Alternate return specifier in function "
263 "'%s' at %L is not allowed", proc->name,
264 &proc->declared_at);
265 continue;
266 }
267 else if (sym->attr.procedure && sym->ts.interface
268 && sym->attr.if_source != IFSRC_DECL)
269 resolve_procedure_interface (sym);
270
271 if (sym->attr.if_source != IFSRC_UNKNOWN)
272 resolve_formal_arglist (sym);
273
274 if (sym->attr.subroutine || sym->attr.external)
275 {
276 if (sym->attr.flavor == FL_UNKNOWN)
277 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
278 }
279 else
280 {
281 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
282 && (!sym->attr.function || sym->result == sym))
283 gfc_set_default_type (sym, 1, sym->ns);
284 }
285
286 gfc_resolve_array_spec (sym->as, 0);
287
288 /* We can't tell if an array with dimension (:) is assumed or deferred
289 shape until we know if it has the pointer or allocatable attributes.
290 */
291 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
292 && !(sym->attr.pointer || sym->attr.allocatable)
293 && sym->attr.flavor != FL_PROCEDURE)
294 {
295 sym->as->type = AS_ASSUMED_SHAPE;
296 for (i = 0; i < sym->as->rank; i++)
297 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
298 NULL, 1);
299 }
300
301 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
302 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
303 || sym->attr.optional)
304 {
305 proc->attr.always_explicit = 1;
306 if (proc->result)
307 proc->result->attr.always_explicit = 1;
308 }
309
310 /* If the flavor is unknown at this point, it has to be a variable.
311 A procedure specification would have already set the type. */
312
313 if (sym->attr.flavor == FL_UNKNOWN)
314 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
315
316 if (gfc_pure (proc))
317 {
318 if (sym->attr.flavor == FL_PROCEDURE)
319 {
320 /* F08:C1279. */
321 if (!gfc_pure (sym))
322 {
323 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
324 "also be PURE", sym->name, &sym->declared_at);
325 continue;
326 }
327 }
328 else if (!sym->attr.pointer)
329 {
330 if (proc->attr.function && sym->attr.intent != INTENT_IN)
331 {
332 if (sym->attr.value)
333 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s'"
334 " of pure function '%s' at %L with VALUE "
335 "attribute but without INTENT(IN)",
336 sym->name, proc->name, &sym->declared_at);
337 else
338 gfc_error ("Argument '%s' of pure function '%s' at %L must "
339 "be INTENT(IN) or VALUE", sym->name, proc->name,
340 &sym->declared_at);
341 }
342
343 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
344 {
345 if (sym->attr.value)
346 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s'"
347 " of pure subroutine '%s' at %L with VALUE "
348 "attribute but without INTENT", sym->name,
349 proc->name, &sym->declared_at);
350 else
351 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
352 "must have its INTENT specified or have the "
353 "VALUE attribute", sym->name, proc->name,
354 &sym->declared_at);
355 }
356 }
357 }
358
359 if (proc->attr.implicit_pure)
360 {
361 if (sym->attr.flavor == FL_PROCEDURE)
362 {
363 if (!gfc_pure(sym))
364 proc->attr.implicit_pure = 0;
365 }
366 else if (!sym->attr.pointer)
367 {
368 if (proc->attr.function && sym->attr.intent != INTENT_IN)
369 proc->attr.implicit_pure = 0;
370
371 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
372 proc->attr.implicit_pure = 0;
373 }
374 }
375
376 if (gfc_elemental (proc))
377 {
378 /* F08:C1289. */
379 if (sym->attr.codimension
380 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
381 && CLASS_DATA (sym)->attr.codimension))
382 {
383 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
384 "procedure", sym->name, &sym->declared_at);
385 continue;
386 }
387
388 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
389 && CLASS_DATA (sym)->as))
390 {
391 gfc_error ("Argument '%s' of elemental procedure at %L must "
392 "be scalar", sym->name, &sym->declared_at);
393 continue;
394 }
395
396 if (sym->attr.allocatable
397 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
398 && CLASS_DATA (sym)->attr.allocatable))
399 {
400 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
401 "have the ALLOCATABLE attribute", sym->name,
402 &sym->declared_at);
403 continue;
404 }
405
406 if (sym->attr.pointer
407 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
408 && CLASS_DATA (sym)->attr.class_pointer))
409 {
410 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
411 "have the POINTER attribute", sym->name,
412 &sym->declared_at);
413 continue;
414 }
415
416 if (sym->attr.flavor == FL_PROCEDURE)
417 {
418 gfc_error ("Dummy procedure '%s' not allowed in elemental "
419 "procedure '%s' at %L", sym->name, proc->name,
420 &sym->declared_at);
421 continue;
422 }
423
424 if (sym->attr.intent == INTENT_UNKNOWN)
425 {
426 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
427 "have its INTENT specified", sym->name, proc->name,
428 &sym->declared_at);
429 continue;
430 }
431 }
432
433 /* Each dummy shall be specified to be scalar. */
434 if (proc->attr.proc == PROC_ST_FUNCTION)
435 {
436 if (sym->as != NULL)
437 {
438 gfc_error ("Argument '%s' of statement function at %L must "
439 "be scalar", sym->name, &sym->declared_at);
440 continue;
441 }
442
443 if (sym->ts.type == BT_CHARACTER)
444 {
445 gfc_charlen *cl = sym->ts.u.cl;
446 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
447 {
448 gfc_error ("Character-valued argument '%s' of statement "
449 "function at %L must have constant length",
450 sym->name, &sym->declared_at);
451 continue;
452 }
453 }
454 }
455 }
456 formal_arg_flag = 0;
457 }
458
459
460 /* Work function called when searching for symbols that have argument lists
461 associated with them. */
462
463 static void
464 find_arglists (gfc_symbol *sym)
465 {
466 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
467 || sym->attr.flavor == FL_DERIVED)
468 return;
469
470 resolve_formal_arglist (sym);
471 }
472
473
474 /* Given a namespace, resolve all formal argument lists within the namespace.
475 */
476
477 static void
478 resolve_formal_arglists (gfc_namespace *ns)
479 {
480 if (ns == NULL)
481 return;
482
483 gfc_traverse_ns (ns, find_arglists);
484 }
485
486
487 static void
488 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
489 {
490 gfc_try t;
491
492 /* If this namespace is not a function or an entry master function,
493 ignore it. */
494 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
495 || sym->attr.entry_master)
496 return;
497
498 /* Try to find out of what the return type is. */
499 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
500 {
501 t = gfc_set_default_type (sym->result, 0, ns);
502
503 if (t == FAILURE && !sym->result->attr.untyped)
504 {
505 if (sym->result == sym)
506 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
507 sym->name, &sym->declared_at);
508 else if (!sym->result->attr.proc_pointer)
509 gfc_error ("Result '%s' of contained function '%s' at %L has "
510 "no IMPLICIT type", sym->result->name, sym->name,
511 &sym->result->declared_at);
512 sym->result->attr.untyped = 1;
513 }
514 }
515
516 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
517 type, lists the only ways a character length value of * can be used:
518 dummy arguments of procedures, named constants, and function results
519 in external functions. Internal function results and results of module
520 procedures are not on this list, ergo, not permitted. */
521
522 if (sym->result->ts.type == BT_CHARACTER)
523 {
524 gfc_charlen *cl = sym->result->ts.u.cl;
525 if ((!cl || !cl->length) && !sym->result->ts.deferred)
526 {
527 /* See if this is a module-procedure and adapt error message
528 accordingly. */
529 bool module_proc;
530 gcc_assert (ns->parent && ns->parent->proc_name);
531 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
532
533 gfc_error ("Character-valued %s '%s' at %L must not be"
534 " assumed length",
535 module_proc ? _("module procedure")
536 : _("internal function"),
537 sym->name, &sym->declared_at);
538 }
539 }
540 }
541
542
543 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
544 introduce duplicates. */
545
546 static void
547 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
548 {
549 gfc_formal_arglist *f, *new_arglist;
550 gfc_symbol *new_sym;
551
552 for (; new_args != NULL; new_args = new_args->next)
553 {
554 new_sym = new_args->sym;
555 /* See if this arg is already in the formal argument list. */
556 for (f = proc->formal; f; f = f->next)
557 {
558 if (new_sym == f->sym)
559 break;
560 }
561
562 if (f)
563 continue;
564
565 /* Add a new argument. Argument order is not important. */
566 new_arglist = gfc_get_formal_arglist ();
567 new_arglist->sym = new_sym;
568 new_arglist->next = proc->formal;
569 proc->formal = new_arglist;
570 }
571 }
572
573
574 /* Flag the arguments that are not present in all entries. */
575
576 static void
577 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
578 {
579 gfc_formal_arglist *f, *head;
580 head = new_args;
581
582 for (f = proc->formal; f; f = f->next)
583 {
584 if (f->sym == NULL)
585 continue;
586
587 for (new_args = head; new_args; new_args = new_args->next)
588 {
589 if (new_args->sym == f->sym)
590 break;
591 }
592
593 if (new_args)
594 continue;
595
596 f->sym->attr.not_always_present = 1;
597 }
598 }
599
600
601 /* Resolve alternate entry points. If a symbol has multiple entry points we
602 create a new master symbol for the main routine, and turn the existing
603 symbol into an entry point. */
604
605 static void
606 resolve_entries (gfc_namespace *ns)
607 {
608 gfc_namespace *old_ns;
609 gfc_code *c;
610 gfc_symbol *proc;
611 gfc_entry_list *el;
612 char name[GFC_MAX_SYMBOL_LEN + 1];
613 static int master_count = 0;
614
615 if (ns->proc_name == NULL)
616 return;
617
618 /* No need to do anything if this procedure doesn't have alternate entry
619 points. */
620 if (!ns->entries)
621 return;
622
623 /* We may already have resolved alternate entry points. */
624 if (ns->proc_name->attr.entry_master)
625 return;
626
627 /* If this isn't a procedure something has gone horribly wrong. */
628 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
629
630 /* Remember the current namespace. */
631 old_ns = gfc_current_ns;
632
633 gfc_current_ns = ns;
634
635 /* Add the main entry point to the list of entry points. */
636 el = gfc_get_entry_list ();
637 el->sym = ns->proc_name;
638 el->id = 0;
639 el->next = ns->entries;
640 ns->entries = el;
641 ns->proc_name->attr.entry = 1;
642
643 /* If it is a module function, it needs to be in the right namespace
644 so that gfc_get_fake_result_decl can gather up the results. The
645 need for this arose in get_proc_name, where these beasts were
646 left in their own namespace, to keep prior references linked to
647 the entry declaration.*/
648 if (ns->proc_name->attr.function
649 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
650 el->sym->ns = ns;
651
652 /* Do the same for entries where the master is not a module
653 procedure. These are retained in the module namespace because
654 of the module procedure declaration. */
655 for (el = el->next; el; el = el->next)
656 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
657 && el->sym->attr.mod_proc)
658 el->sym->ns = ns;
659 el = ns->entries;
660
661 /* Add an entry statement for it. */
662 c = gfc_get_code ();
663 c->op = EXEC_ENTRY;
664 c->ext.entry = el;
665 c->next = ns->code;
666 ns->code = c;
667
668 /* Create a new symbol for the master function. */
669 /* Give the internal function a unique name (within this file).
670 Also include the function name so the user has some hope of figuring
671 out what is going on. */
672 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
673 master_count++, ns->proc_name->name);
674 gfc_get_ha_symbol (name, &proc);
675 gcc_assert (proc != NULL);
676
677 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
678 if (ns->proc_name->attr.subroutine)
679 gfc_add_subroutine (&proc->attr, proc->name, NULL);
680 else
681 {
682 gfc_symbol *sym;
683 gfc_typespec *ts, *fts;
684 gfc_array_spec *as, *fas;
685 gfc_add_function (&proc->attr, proc->name, NULL);
686 proc->result = proc;
687 fas = ns->entries->sym->as;
688 fas = fas ? fas : ns->entries->sym->result->as;
689 fts = &ns->entries->sym->result->ts;
690 if (fts->type == BT_UNKNOWN)
691 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
692 for (el = ns->entries->next; el; el = el->next)
693 {
694 ts = &el->sym->result->ts;
695 as = el->sym->as;
696 as = as ? as : el->sym->result->as;
697 if (ts->type == BT_UNKNOWN)
698 ts = gfc_get_default_type (el->sym->result->name, NULL);
699
700 if (! gfc_compare_types (ts, fts)
701 || (el->sym->result->attr.dimension
702 != ns->entries->sym->result->attr.dimension)
703 || (el->sym->result->attr.pointer
704 != ns->entries->sym->result->attr.pointer))
705 break;
706 else if (as && fas && ns->entries->sym->result != el->sym->result
707 && gfc_compare_array_spec (as, fas) == 0)
708 gfc_error ("Function %s at %L has entries with mismatched "
709 "array specifications", ns->entries->sym->name,
710 &ns->entries->sym->declared_at);
711 /* The characteristics need to match and thus both need to have
712 the same string length, i.e. both len=*, or both len=4.
713 Having both len=<variable> is also possible, but difficult to
714 check at compile time. */
715 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
716 && (((ts->u.cl->length && !fts->u.cl->length)
717 ||(!ts->u.cl->length && fts->u.cl->length))
718 || (ts->u.cl->length
719 && ts->u.cl->length->expr_type
720 != fts->u.cl->length->expr_type)
721 || (ts->u.cl->length
722 && ts->u.cl->length->expr_type == EXPR_CONSTANT
723 && mpz_cmp (ts->u.cl->length->value.integer,
724 fts->u.cl->length->value.integer) != 0)))
725 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
726 "entries returning variables of different "
727 "string lengths", ns->entries->sym->name,
728 &ns->entries->sym->declared_at);
729 }
730
731 if (el == NULL)
732 {
733 sym = ns->entries->sym->result;
734 /* All result types the same. */
735 proc->ts = *fts;
736 if (sym->attr.dimension)
737 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
738 if (sym->attr.pointer)
739 gfc_add_pointer (&proc->attr, NULL);
740 }
741 else
742 {
743 /* Otherwise the result will be passed through a union by
744 reference. */
745 proc->attr.mixed_entry_master = 1;
746 for (el = ns->entries; el; el = el->next)
747 {
748 sym = el->sym->result;
749 if (sym->attr.dimension)
750 {
751 if (el == ns->entries)
752 gfc_error ("FUNCTION result %s can't be an array in "
753 "FUNCTION %s at %L", sym->name,
754 ns->entries->sym->name, &sym->declared_at);
755 else
756 gfc_error ("ENTRY result %s can't be an array in "
757 "FUNCTION %s at %L", sym->name,
758 ns->entries->sym->name, &sym->declared_at);
759 }
760 else if (sym->attr.pointer)
761 {
762 if (el == ns->entries)
763 gfc_error ("FUNCTION result %s can't be a POINTER in "
764 "FUNCTION %s at %L", sym->name,
765 ns->entries->sym->name, &sym->declared_at);
766 else
767 gfc_error ("ENTRY result %s can't be a POINTER in "
768 "FUNCTION %s at %L", sym->name,
769 ns->entries->sym->name, &sym->declared_at);
770 }
771 else
772 {
773 ts = &sym->ts;
774 if (ts->type == BT_UNKNOWN)
775 ts = gfc_get_default_type (sym->name, NULL);
776 switch (ts->type)
777 {
778 case BT_INTEGER:
779 if (ts->kind == gfc_default_integer_kind)
780 sym = NULL;
781 break;
782 case BT_REAL:
783 if (ts->kind == gfc_default_real_kind
784 || ts->kind == gfc_default_double_kind)
785 sym = NULL;
786 break;
787 case BT_COMPLEX:
788 if (ts->kind == gfc_default_complex_kind)
789 sym = NULL;
790 break;
791 case BT_LOGICAL:
792 if (ts->kind == gfc_default_logical_kind)
793 sym = NULL;
794 break;
795 case BT_UNKNOWN:
796 /* We will issue error elsewhere. */
797 sym = NULL;
798 break;
799 default:
800 break;
801 }
802 if (sym)
803 {
804 if (el == ns->entries)
805 gfc_error ("FUNCTION result %s can't be of type %s "
806 "in FUNCTION %s at %L", sym->name,
807 gfc_typename (ts), ns->entries->sym->name,
808 &sym->declared_at);
809 else
810 gfc_error ("ENTRY result %s can't be of type %s "
811 "in FUNCTION %s at %L", sym->name,
812 gfc_typename (ts), ns->entries->sym->name,
813 &sym->declared_at);
814 }
815 }
816 }
817 }
818 }
819 proc->attr.access = ACCESS_PRIVATE;
820 proc->attr.entry_master = 1;
821
822 /* Merge all the entry point arguments. */
823 for (el = ns->entries; el; el = el->next)
824 merge_argument_lists (proc, el->sym->formal);
825
826 /* Check the master formal arguments for any that are not
827 present in all entry points. */
828 for (el = ns->entries; el; el = el->next)
829 check_argument_lists (proc, el->sym->formal);
830
831 /* Use the master function for the function body. */
832 ns->proc_name = proc;
833
834 /* Finalize the new symbols. */
835 gfc_commit_symbols ();
836
837 /* Restore the original namespace. */
838 gfc_current_ns = old_ns;
839 }
840
841
842 /* Resolve common variables. */
843 static void
844 resolve_common_vars (gfc_symbol *sym, bool named_common)
845 {
846 gfc_symbol *csym = sym;
847
848 for (; csym; csym = csym->common_next)
849 {
850 if (csym->value || csym->attr.data)
851 {
852 if (!csym->ns->is_block_data)
853 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
854 "but only in BLOCK DATA initialization is "
855 "allowed", csym->name, &csym->declared_at);
856 else if (!named_common)
857 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
858 "in a blank COMMON but initialization is only "
859 "allowed in named common blocks", csym->name,
860 &csym->declared_at);
861 }
862
863 if (csym->ts.type != BT_DERIVED)
864 continue;
865
866 if (!(csym->ts.u.derived->attr.sequence
867 || csym->ts.u.derived->attr.is_bind_c))
868 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
869 "has neither the SEQUENCE nor the BIND(C) "
870 "attribute", csym->name, &csym->declared_at);
871 if (csym->ts.u.derived->attr.alloc_comp)
872 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
873 "has an ultimate component that is "
874 "allocatable", csym->name, &csym->declared_at);
875 if (gfc_has_default_initializer (csym->ts.u.derived))
876 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
877 "may not have default initializer", csym->name,
878 &csym->declared_at);
879
880 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
881 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
882 }
883 }
884
885 /* Resolve common blocks. */
886 static void
887 resolve_common_blocks (gfc_symtree *common_root)
888 {
889 gfc_symbol *sym;
890
891 if (common_root == NULL)
892 return;
893
894 if (common_root->left)
895 resolve_common_blocks (common_root->left);
896 if (common_root->right)
897 resolve_common_blocks (common_root->right);
898
899 resolve_common_vars (common_root->n.common->head, true);
900
901 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
902 if (sym == NULL)
903 return;
904
905 if (sym->attr.flavor == FL_PARAMETER)
906 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
907 sym->name, &common_root->n.common->where, &sym->declared_at);
908
909 if (sym->attr.external)
910 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
911 sym->name, &common_root->n.common->where);
912
913 if (sym->attr.intrinsic)
914 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
915 sym->name, &common_root->n.common->where);
916 else if (sym->attr.result
917 || gfc_is_function_return_value (sym, gfc_current_ns))
918 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
919 "that is also a function result", sym->name,
920 &common_root->n.common->where);
921 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
922 && sym->attr.proc != PROC_ST_FUNCTION)
923 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
924 "that is also a global procedure", sym->name,
925 &common_root->n.common->where);
926 }
927
928
929 /* Resolve contained function types. Because contained functions can call one
930 another, they have to be worked out before any of the contained procedures
931 can be resolved.
932
933 The good news is that if a function doesn't already have a type, the only
934 way it can get one is through an IMPLICIT type or a RESULT variable, because
935 by definition contained functions are contained namespace they're contained
936 in, not in a sibling or parent namespace. */
937
938 static void
939 resolve_contained_functions (gfc_namespace *ns)
940 {
941 gfc_namespace *child;
942 gfc_entry_list *el;
943
944 resolve_formal_arglists (ns);
945
946 for (child = ns->contained; child; child = child->sibling)
947 {
948 /* Resolve alternate entry points first. */
949 resolve_entries (child);
950
951 /* Then check function return types. */
952 resolve_contained_fntype (child->proc_name, child);
953 for (el = child->entries; el; el = el->next)
954 resolve_contained_fntype (el->sym, child);
955 }
956 }
957
958
959 static gfc_try resolve_fl_derived0 (gfc_symbol *sym);
960
961
962 /* Resolve all of the elements of a structure constructor and make sure that
963 the types are correct. The 'init' flag indicates that the given
964 constructor is an initializer. */
965
966 static gfc_try
967 resolve_structure_cons (gfc_expr *expr, int init)
968 {
969 gfc_constructor *cons;
970 gfc_component *comp;
971 gfc_try t;
972 symbol_attribute a;
973
974 t = SUCCESS;
975
976 if (expr->ts.type == BT_DERIVED)
977 resolve_fl_derived0 (expr->ts.u.derived);
978
979 cons = gfc_constructor_first (expr->value.constructor);
980
981 /* See if the user is trying to invoke a structure constructor for one of
982 the iso_c_binding derived types. */
983 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
984 && expr->ts.u.derived->ts.is_iso_c && cons
985 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
986 {
987 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
988 expr->ts.u.derived->name, &(expr->where));
989 return FAILURE;
990 }
991
992 /* Return if structure constructor is c_null_(fun)prt. */
993 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
994 && expr->ts.u.derived->ts.is_iso_c && cons
995 && cons->expr && cons->expr->expr_type == EXPR_NULL)
996 return SUCCESS;
997
998 /* A constructor may have references if it is the result of substituting a
999 parameter variable. In this case we just pull out the component we
1000 want. */
1001 if (expr->ref)
1002 comp = expr->ref->u.c.sym->components;
1003 else
1004 comp = expr->ts.u.derived->components;
1005
1006 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1007 {
1008 int rank;
1009
1010 if (!cons->expr)
1011 continue;
1012
1013 if (gfc_resolve_expr (cons->expr) == FAILURE)
1014 {
1015 t = FAILURE;
1016 continue;
1017 }
1018
1019 rank = comp->as ? comp->as->rank : 0;
1020 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1021 && (comp->attr.allocatable || cons->expr->rank))
1022 {
1023 gfc_error ("The rank of the element in the structure "
1024 "constructor at %L does not match that of the "
1025 "component (%d/%d)", &cons->expr->where,
1026 cons->expr->rank, rank);
1027 t = FAILURE;
1028 }
1029
1030 /* If we don't have the right type, try to convert it. */
1031
1032 if (!comp->attr.proc_pointer &&
1033 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1034 {
1035 t = FAILURE;
1036 if (strcmp (comp->name, "_extends") == 0)
1037 {
1038 /* Can afford to be brutal with the _extends initializer.
1039 The derived type can get lost because it is PRIVATE
1040 but it is not usage constrained by the standard. */
1041 cons->expr->ts = comp->ts;
1042 t = SUCCESS;
1043 }
1044 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1045 gfc_error ("The element in the structure constructor at %L, "
1046 "for pointer component '%s', is %s but should be %s",
1047 &cons->expr->where, comp->name,
1048 gfc_basic_typename (cons->expr->ts.type),
1049 gfc_basic_typename (comp->ts.type));
1050 else
1051 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1052 }
1053
1054 /* For strings, the length of the constructor should be the same as
1055 the one of the structure, ensure this if the lengths are known at
1056 compile time and when we are dealing with PARAMETER or structure
1057 constructors. */
1058 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1059 && comp->ts.u.cl->length
1060 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1061 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1062 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1063 && cons->expr->rank != 0
1064 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1065 comp->ts.u.cl->length->value.integer) != 0)
1066 {
1067 if (cons->expr->expr_type == EXPR_VARIABLE
1068 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1069 {
1070 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1071 to make use of the gfc_resolve_character_array_constructor
1072 machinery. The expression is later simplified away to
1073 an array of string literals. */
1074 gfc_expr *para = cons->expr;
1075 cons->expr = gfc_get_expr ();
1076 cons->expr->ts = para->ts;
1077 cons->expr->where = para->where;
1078 cons->expr->expr_type = EXPR_ARRAY;
1079 cons->expr->rank = para->rank;
1080 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1081 gfc_constructor_append_expr (&cons->expr->value.constructor,
1082 para, &cons->expr->where);
1083 }
1084 if (cons->expr->expr_type == EXPR_ARRAY)
1085 {
1086 gfc_constructor *p;
1087 p = gfc_constructor_first (cons->expr->value.constructor);
1088 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1089 {
1090 gfc_charlen *cl, *cl2;
1091
1092 cl2 = NULL;
1093 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1094 {
1095 if (cl == cons->expr->ts.u.cl)
1096 break;
1097 cl2 = cl;
1098 }
1099
1100 gcc_assert (cl);
1101
1102 if (cl2)
1103 cl2->next = cl->next;
1104
1105 gfc_free_expr (cl->length);
1106 free (cl);
1107 }
1108
1109 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1110 cons->expr->ts.u.cl->length_from_typespec = true;
1111 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1112 gfc_resolve_character_array_constructor (cons->expr);
1113 }
1114 }
1115
1116 if (cons->expr->expr_type == EXPR_NULL
1117 && !(comp->attr.pointer || comp->attr.allocatable
1118 || comp->attr.proc_pointer
1119 || (comp->ts.type == BT_CLASS
1120 && (CLASS_DATA (comp)->attr.class_pointer
1121 || CLASS_DATA (comp)->attr.allocatable))))
1122 {
1123 t = FAILURE;
1124 gfc_error ("The NULL in the structure constructor at %L is "
1125 "being applied to component '%s', which is neither "
1126 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1127 comp->name);
1128 }
1129
1130 if (comp->attr.proc_pointer && comp->ts.interface)
1131 {
1132 /* Check procedure pointer interface. */
1133 gfc_symbol *s2 = NULL;
1134 gfc_component *c2;
1135 const char *name;
1136 char err[200];
1137
1138 if (gfc_is_proc_ptr_comp (cons->expr, &c2))
1139 {
1140 s2 = c2->ts.interface;
1141 name = c2->name;
1142 }
1143 else if (cons->expr->expr_type == EXPR_FUNCTION)
1144 {
1145 s2 = cons->expr->symtree->n.sym->result;
1146 name = cons->expr->symtree->n.sym->result->name;
1147 }
1148 else if (cons->expr->expr_type != EXPR_NULL)
1149 {
1150 s2 = cons->expr->symtree->n.sym;
1151 name = cons->expr->symtree->n.sym->name;
1152 }
1153
1154 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1155 err, sizeof (err)))
1156 {
1157 gfc_error ("Interface mismatch for procedure-pointer component "
1158 "'%s' in structure constructor at %L: %s",
1159 comp->name, &cons->expr->where, err);
1160 return FAILURE;
1161 }
1162 }
1163
1164 if (!comp->attr.pointer || comp->attr.proc_pointer
1165 || cons->expr->expr_type == EXPR_NULL)
1166 continue;
1167
1168 a = gfc_expr_attr (cons->expr);
1169
1170 if (!a.pointer && !a.target)
1171 {
1172 t = FAILURE;
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s' should be a POINTER or "
1175 "a TARGET", &cons->expr->where, comp->name);
1176 }
1177
1178 if (init)
1179 {
1180 /* F08:C461. Additional checks for pointer initialization. */
1181 if (a.allocatable)
1182 {
1183 t = FAILURE;
1184 gfc_error ("Pointer initialization target at %L "
1185 "must not be ALLOCATABLE ", &cons->expr->where);
1186 }
1187 if (!a.save)
1188 {
1189 t = FAILURE;
1190 gfc_error ("Pointer initialization target at %L "
1191 "must have the SAVE attribute", &cons->expr->where);
1192 }
1193 }
1194
1195 /* F2003, C1272 (3). */
1196 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1197 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1198 || gfc_is_coindexed (cons->expr)))
1199 {
1200 t = FAILURE;
1201 gfc_error ("Invalid expression in the structure constructor for "
1202 "pointer component '%s' at %L in PURE procedure",
1203 comp->name, &cons->expr->where);
1204 }
1205
1206 if (gfc_implicit_pure (NULL)
1207 && cons->expr->expr_type == EXPR_VARIABLE
1208 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1209 || gfc_is_coindexed (cons->expr)))
1210 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1211
1212 }
1213
1214 return t;
1215 }
1216
1217
1218 /****************** Expression name resolution ******************/
1219
1220 /* Returns 0 if a symbol was not declared with a type or
1221 attribute declaration statement, nonzero otherwise. */
1222
1223 static int
1224 was_declared (gfc_symbol *sym)
1225 {
1226 symbol_attribute a;
1227
1228 a = sym->attr;
1229
1230 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1231 return 1;
1232
1233 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1234 || a.optional || a.pointer || a.save || a.target || a.volatile_
1235 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1236 || a.asynchronous || a.codimension)
1237 return 1;
1238
1239 return 0;
1240 }
1241
1242
1243 /* Determine if a symbol is generic or not. */
1244
1245 static int
1246 generic_sym (gfc_symbol *sym)
1247 {
1248 gfc_symbol *s;
1249
1250 if (sym->attr.generic ||
1251 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1252 return 1;
1253
1254 if (was_declared (sym) || sym->ns->parent == NULL)
1255 return 0;
1256
1257 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1258
1259 if (s != NULL)
1260 {
1261 if (s == sym)
1262 return 0;
1263 else
1264 return generic_sym (s);
1265 }
1266
1267 return 0;
1268 }
1269
1270
1271 /* Determine if a symbol is specific or not. */
1272
1273 static int
1274 specific_sym (gfc_symbol *sym)
1275 {
1276 gfc_symbol *s;
1277
1278 if (sym->attr.if_source == IFSRC_IFBODY
1279 || sym->attr.proc == PROC_MODULE
1280 || sym->attr.proc == PROC_INTERNAL
1281 || sym->attr.proc == PROC_ST_FUNCTION
1282 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1283 || sym->attr.external)
1284 return 1;
1285
1286 if (was_declared (sym) || sym->ns->parent == NULL)
1287 return 0;
1288
1289 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1290
1291 return (s == NULL) ? 0 : specific_sym (s);
1292 }
1293
1294
1295 /* Figure out if the procedure is specific, generic or unknown. */
1296
1297 typedef enum
1298 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1299 proc_type;
1300
1301 static proc_type
1302 procedure_kind (gfc_symbol *sym)
1303 {
1304 if (generic_sym (sym))
1305 return PTYPE_GENERIC;
1306
1307 if (specific_sym (sym))
1308 return PTYPE_SPECIFIC;
1309
1310 return PTYPE_UNKNOWN;
1311 }
1312
1313 /* Check references to assumed size arrays. The flag need_full_assumed_size
1314 is nonzero when matching actual arguments. */
1315
1316 static int need_full_assumed_size = 0;
1317
1318 static bool
1319 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1320 {
1321 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1322 return false;
1323
1324 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1325 What should it be? */
1326 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1327 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1328 && (e->ref->u.ar.type == AR_FULL))
1329 {
1330 gfc_error ("The upper bound in the last dimension must "
1331 "appear in the reference to the assumed size "
1332 "array '%s' at %L", sym->name, &e->where);
1333 return true;
1334 }
1335 return false;
1336 }
1337
1338
1339 /* Look for bad assumed size array references in argument expressions
1340 of elemental and array valued intrinsic procedures. Since this is
1341 called from procedure resolution functions, it only recurses at
1342 operators. */
1343
1344 static bool
1345 resolve_assumed_size_actual (gfc_expr *e)
1346 {
1347 if (e == NULL)
1348 return false;
1349
1350 switch (e->expr_type)
1351 {
1352 case EXPR_VARIABLE:
1353 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1354 return true;
1355 break;
1356
1357 case EXPR_OP:
1358 if (resolve_assumed_size_actual (e->value.op.op1)
1359 || resolve_assumed_size_actual (e->value.op.op2))
1360 return true;
1361 break;
1362
1363 default:
1364 break;
1365 }
1366 return false;
1367 }
1368
1369
1370 /* Check a generic procedure, passed as an actual argument, to see if
1371 there is a matching specific name. If none, it is an error, and if
1372 more than one, the reference is ambiguous. */
1373 static int
1374 count_specific_procs (gfc_expr *e)
1375 {
1376 int n;
1377 gfc_interface *p;
1378 gfc_symbol *sym;
1379
1380 n = 0;
1381 sym = e->symtree->n.sym;
1382
1383 for (p = sym->generic; p; p = p->next)
1384 if (strcmp (sym->name, p->sym->name) == 0)
1385 {
1386 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1387 sym->name);
1388 n++;
1389 }
1390
1391 if (n > 1)
1392 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1393 &e->where);
1394
1395 if (n == 0)
1396 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1397 "argument at %L", sym->name, &e->where);
1398
1399 return n;
1400 }
1401
1402
1403 /* See if a call to sym could possibly be a not allowed RECURSION because of
1404 a missing RECURIVE declaration. This means that either sym is the current
1405 context itself, or sym is the parent of a contained procedure calling its
1406 non-RECURSIVE containing procedure.
1407 This also works if sym is an ENTRY. */
1408
1409 static bool
1410 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1411 {
1412 gfc_symbol* proc_sym;
1413 gfc_symbol* context_proc;
1414 gfc_namespace* real_context;
1415
1416 if (sym->attr.flavor == FL_PROGRAM
1417 || sym->attr.flavor == FL_DERIVED)
1418 return false;
1419
1420 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1421
1422 /* If we've got an ENTRY, find real procedure. */
1423 if (sym->attr.entry && sym->ns->entries)
1424 proc_sym = sym->ns->entries->sym;
1425 else
1426 proc_sym = sym;
1427
1428 /* If sym is RECURSIVE, all is well of course. */
1429 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1430 return false;
1431
1432 /* Find the context procedure's "real" symbol if it has entries.
1433 We look for a procedure symbol, so recurse on the parents if we don't
1434 find one (like in case of a BLOCK construct). */
1435 for (real_context = context; ; real_context = real_context->parent)
1436 {
1437 /* We should find something, eventually! */
1438 gcc_assert (real_context);
1439
1440 context_proc = (real_context->entries ? real_context->entries->sym
1441 : real_context->proc_name);
1442
1443 /* In some special cases, there may not be a proc_name, like for this
1444 invalid code:
1445 real(bad_kind()) function foo () ...
1446 when checking the call to bad_kind ().
1447 In these cases, we simply return here and assume that the
1448 call is ok. */
1449 if (!context_proc)
1450 return false;
1451
1452 if (context_proc->attr.flavor != FL_LABEL)
1453 break;
1454 }
1455
1456 /* A call from sym's body to itself is recursion, of course. */
1457 if (context_proc == proc_sym)
1458 return true;
1459
1460 /* The same is true if context is a contained procedure and sym the
1461 containing one. */
1462 if (context_proc->attr.contained)
1463 {
1464 gfc_symbol* parent_proc;
1465
1466 gcc_assert (context->parent);
1467 parent_proc = (context->parent->entries ? context->parent->entries->sym
1468 : context->parent->proc_name);
1469
1470 if (parent_proc == proc_sym)
1471 return true;
1472 }
1473
1474 return false;
1475 }
1476
1477
1478 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1479 its typespec and formal argument list. */
1480
1481 static gfc_try
1482 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1483 {
1484 gfc_intrinsic_sym* isym = NULL;
1485 const char* symstd;
1486
1487 if (sym->formal)
1488 return SUCCESS;
1489
1490 /* Already resolved. */
1491 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1492 return SUCCESS;
1493
1494 /* We already know this one is an intrinsic, so we don't call
1495 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1496 gfc_find_subroutine directly to check whether it is a function or
1497 subroutine. */
1498
1499 if (sym->intmod_sym_id)
1500 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1501 else if (!sym->attr.subroutine)
1502 isym = gfc_find_function (sym->name);
1503
1504 if (isym)
1505 {
1506 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1507 && !sym->attr.implicit_type)
1508 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1509 " ignored", sym->name, &sym->declared_at);
1510
1511 if (!sym->attr.function &&
1512 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1513 return FAILURE;
1514
1515 sym->ts = isym->ts;
1516 }
1517 else if ((isym = gfc_find_subroutine (sym->name)))
1518 {
1519 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1520 {
1521 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1522 " specifier", sym->name, &sym->declared_at);
1523 return FAILURE;
1524 }
1525
1526 if (!sym->attr.subroutine &&
1527 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1528 return FAILURE;
1529 }
1530 else
1531 {
1532 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1533 &sym->declared_at);
1534 return FAILURE;
1535 }
1536
1537 gfc_copy_formal_args_intr (sym, isym);
1538
1539 /* Check it is actually available in the standard settings. */
1540 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1541 == FAILURE)
1542 {
1543 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1544 " available in the current standard settings but %s. Use"
1545 " an appropriate -std=* option or enable -fall-intrinsics"
1546 " in order to use it.",
1547 sym->name, &sym->declared_at, symstd);
1548 return FAILURE;
1549 }
1550
1551 return SUCCESS;
1552 }
1553
1554
1555 /* Resolve a procedure expression, like passing it to a called procedure or as
1556 RHS for a procedure pointer assignment. */
1557
1558 static gfc_try
1559 resolve_procedure_expression (gfc_expr* expr)
1560 {
1561 gfc_symbol* sym;
1562
1563 if (expr->expr_type != EXPR_VARIABLE)
1564 return SUCCESS;
1565 gcc_assert (expr->symtree);
1566
1567 sym = expr->symtree->n.sym;
1568
1569 if (sym->attr.intrinsic)
1570 resolve_intrinsic (sym, &expr->where);
1571
1572 if (sym->attr.flavor != FL_PROCEDURE
1573 || (sym->attr.function && sym->result == sym))
1574 return SUCCESS;
1575
1576 /* A non-RECURSIVE procedure that is used as procedure expression within its
1577 own body is in danger of being called recursively. */
1578 if (is_illegal_recursion (sym, gfc_current_ns))
1579 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1580 " itself recursively. Declare it RECURSIVE or use"
1581 " -frecursive", sym->name, &expr->where);
1582
1583 return SUCCESS;
1584 }
1585
1586
1587 /* Resolve an actual argument list. Most of the time, this is just
1588 resolving the expressions in the list.
1589 The exception is that we sometimes have to decide whether arguments
1590 that look like procedure arguments are really simple variable
1591 references. */
1592
1593 static gfc_try
1594 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1595 bool no_formal_args)
1596 {
1597 gfc_symbol *sym;
1598 gfc_symtree *parent_st;
1599 gfc_expr *e;
1600 int save_need_full_assumed_size;
1601
1602 assumed_type_expr_allowed = true;
1603
1604 for (; arg; arg = arg->next)
1605 {
1606 e = arg->expr;
1607 if (e == NULL)
1608 {
1609 /* Check the label is a valid branching target. */
1610 if (arg->label)
1611 {
1612 if (arg->label->defined == ST_LABEL_UNKNOWN)
1613 {
1614 gfc_error ("Label %d referenced at %L is never defined",
1615 arg->label->value, &arg->label->where);
1616 return FAILURE;
1617 }
1618 }
1619 continue;
1620 }
1621
1622 if (e->expr_type == EXPR_VARIABLE
1623 && e->symtree->n.sym->attr.generic
1624 && no_formal_args
1625 && count_specific_procs (e) != 1)
1626 return FAILURE;
1627
1628 if (e->ts.type != BT_PROCEDURE)
1629 {
1630 save_need_full_assumed_size = need_full_assumed_size;
1631 if (e->expr_type != EXPR_VARIABLE)
1632 need_full_assumed_size = 0;
1633 if (gfc_resolve_expr (e) != SUCCESS)
1634 return FAILURE;
1635 need_full_assumed_size = save_need_full_assumed_size;
1636 goto argument_list;
1637 }
1638
1639 /* See if the expression node should really be a variable reference. */
1640
1641 sym = e->symtree->n.sym;
1642
1643 if (sym->attr.flavor == FL_PROCEDURE
1644 || sym->attr.intrinsic
1645 || sym->attr.external)
1646 {
1647 int actual_ok;
1648
1649 /* If a procedure is not already determined to be something else
1650 check if it is intrinsic. */
1651 if (!sym->attr.intrinsic
1652 && !(sym->attr.external || sym->attr.use_assoc
1653 || sym->attr.if_source == IFSRC_IFBODY)
1654 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1655 sym->attr.intrinsic = 1;
1656
1657 if (sym->attr.proc == PROC_ST_FUNCTION)
1658 {
1659 gfc_error ("Statement function '%s' at %L is not allowed as an "
1660 "actual argument", sym->name, &e->where);
1661 }
1662
1663 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1664 sym->attr.subroutine);
1665 if (sym->attr.intrinsic && actual_ok == 0)
1666 {
1667 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1668 "actual argument", sym->name, &e->where);
1669 }
1670
1671 if (sym->attr.contained && !sym->attr.use_assoc
1672 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1673 {
1674 if (gfc_notify_std (GFC_STD_F2008,
1675 "Fortran 2008: Internal procedure '%s' is"
1676 " used as actual argument at %L",
1677 sym->name, &e->where) == FAILURE)
1678 return FAILURE;
1679 }
1680
1681 if (sym->attr.elemental && !sym->attr.intrinsic)
1682 {
1683 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1684 "allowed as an actual argument at %L", sym->name,
1685 &e->where);
1686 }
1687
1688 /* Check if a generic interface has a specific procedure
1689 with the same name before emitting an error. */
1690 if (sym->attr.generic && count_specific_procs (e) != 1)
1691 return FAILURE;
1692
1693 /* Just in case a specific was found for the expression. */
1694 sym = e->symtree->n.sym;
1695
1696 /* If the symbol is the function that names the current (or
1697 parent) scope, then we really have a variable reference. */
1698
1699 if (gfc_is_function_return_value (sym, sym->ns))
1700 goto got_variable;
1701
1702 /* If all else fails, see if we have a specific intrinsic. */
1703 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1704 {
1705 gfc_intrinsic_sym *isym;
1706
1707 isym = gfc_find_function (sym->name);
1708 if (isym == NULL || !isym->specific)
1709 {
1710 gfc_error ("Unable to find a specific INTRINSIC procedure "
1711 "for the reference '%s' at %L", sym->name,
1712 &e->where);
1713 return FAILURE;
1714 }
1715 sym->ts = isym->ts;
1716 sym->attr.intrinsic = 1;
1717 sym->attr.function = 1;
1718 }
1719
1720 if (gfc_resolve_expr (e) == FAILURE)
1721 return FAILURE;
1722 goto argument_list;
1723 }
1724
1725 /* See if the name is a module procedure in a parent unit. */
1726
1727 if (was_declared (sym) || sym->ns->parent == NULL)
1728 goto got_variable;
1729
1730 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1731 {
1732 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1733 return FAILURE;
1734 }
1735
1736 if (parent_st == NULL)
1737 goto got_variable;
1738
1739 sym = parent_st->n.sym;
1740 e->symtree = parent_st; /* Point to the right thing. */
1741
1742 if (sym->attr.flavor == FL_PROCEDURE
1743 || sym->attr.intrinsic
1744 || sym->attr.external)
1745 {
1746 if (gfc_resolve_expr (e) == FAILURE)
1747 return FAILURE;
1748 goto argument_list;
1749 }
1750
1751 got_variable:
1752 e->expr_type = EXPR_VARIABLE;
1753 e->ts = sym->ts;
1754 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1755 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1756 && CLASS_DATA (sym)->as))
1757 {
1758 e->rank = sym->ts.type == BT_CLASS
1759 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1760 e->ref = gfc_get_ref ();
1761 e->ref->type = REF_ARRAY;
1762 e->ref->u.ar.type = AR_FULL;
1763 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1764 ? CLASS_DATA (sym)->as : sym->as;
1765 }
1766
1767 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1768 primary.c (match_actual_arg). If above code determines that it
1769 is a variable instead, it needs to be resolved as it was not
1770 done at the beginning of this function. */
1771 save_need_full_assumed_size = need_full_assumed_size;
1772 if (e->expr_type != EXPR_VARIABLE)
1773 need_full_assumed_size = 0;
1774 if (gfc_resolve_expr (e) != SUCCESS)
1775 return FAILURE;
1776 need_full_assumed_size = save_need_full_assumed_size;
1777
1778 argument_list:
1779 /* Check argument list functions %VAL, %LOC and %REF. There is
1780 nothing to do for %REF. */
1781 if (arg->name && arg->name[0] == '%')
1782 {
1783 if (strncmp ("%VAL", arg->name, 4) == 0)
1784 {
1785 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1786 {
1787 gfc_error ("By-value argument at %L is not of numeric "
1788 "type", &e->where);
1789 return FAILURE;
1790 }
1791
1792 if (e->rank)
1793 {
1794 gfc_error ("By-value argument at %L cannot be an array or "
1795 "an array section", &e->where);
1796 return FAILURE;
1797 }
1798
1799 /* Intrinsics are still PROC_UNKNOWN here. However,
1800 since same file external procedures are not resolvable
1801 in gfortran, it is a good deal easier to leave them to
1802 intrinsic.c. */
1803 if (ptype != PROC_UNKNOWN
1804 && ptype != PROC_DUMMY
1805 && ptype != PROC_EXTERNAL
1806 && ptype != PROC_MODULE)
1807 {
1808 gfc_error ("By-value argument at %L is not allowed "
1809 "in this context", &e->where);
1810 return FAILURE;
1811 }
1812 }
1813
1814 /* Statement functions have already been excluded above. */
1815 else if (strncmp ("%LOC", arg->name, 4) == 0
1816 && e->ts.type == BT_PROCEDURE)
1817 {
1818 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1819 {
1820 gfc_error ("Passing internal procedure at %L by location "
1821 "not allowed", &e->where);
1822 return FAILURE;
1823 }
1824 }
1825 }
1826
1827 /* Fortran 2008, C1237. */
1828 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1829 && gfc_has_ultimate_pointer (e))
1830 {
1831 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1832 "component", &e->where);
1833 return FAILURE;
1834 }
1835 }
1836 assumed_type_expr_allowed = false;
1837
1838 return SUCCESS;
1839 }
1840
1841
1842 /* Do the checks of the actual argument list that are specific to elemental
1843 procedures. If called with c == NULL, we have a function, otherwise if
1844 expr == NULL, we have a subroutine. */
1845
1846 static gfc_try
1847 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1848 {
1849 gfc_actual_arglist *arg0;
1850 gfc_actual_arglist *arg;
1851 gfc_symbol *esym = NULL;
1852 gfc_intrinsic_sym *isym = NULL;
1853 gfc_expr *e = NULL;
1854 gfc_intrinsic_arg *iformal = NULL;
1855 gfc_formal_arglist *eformal = NULL;
1856 bool formal_optional = false;
1857 bool set_by_optional = false;
1858 int i;
1859 int rank = 0;
1860
1861 /* Is this an elemental procedure? */
1862 if (expr && expr->value.function.actual != NULL)
1863 {
1864 if (expr->value.function.esym != NULL
1865 && expr->value.function.esym->attr.elemental)
1866 {
1867 arg0 = expr->value.function.actual;
1868 esym = expr->value.function.esym;
1869 }
1870 else if (expr->value.function.isym != NULL
1871 && expr->value.function.isym->elemental)
1872 {
1873 arg0 = expr->value.function.actual;
1874 isym = expr->value.function.isym;
1875 }
1876 else
1877 return SUCCESS;
1878 }
1879 else if (c && c->ext.actual != NULL)
1880 {
1881 arg0 = c->ext.actual;
1882
1883 if (c->resolved_sym)
1884 esym = c->resolved_sym;
1885 else
1886 esym = c->symtree->n.sym;
1887 gcc_assert (esym);
1888
1889 if (!esym->attr.elemental)
1890 return SUCCESS;
1891 }
1892 else
1893 return SUCCESS;
1894
1895 /* The rank of an elemental is the rank of its array argument(s). */
1896 for (arg = arg0; arg; arg = arg->next)
1897 {
1898 if (arg->expr != NULL && arg->expr->rank > 0)
1899 {
1900 rank = arg->expr->rank;
1901 if (arg->expr->expr_type == EXPR_VARIABLE
1902 && arg->expr->symtree->n.sym->attr.optional)
1903 set_by_optional = true;
1904
1905 /* Function specific; set the result rank and shape. */
1906 if (expr)
1907 {
1908 expr->rank = rank;
1909 if (!expr->shape && arg->expr->shape)
1910 {
1911 expr->shape = gfc_get_shape (rank);
1912 for (i = 0; i < rank; i++)
1913 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1914 }
1915 }
1916 break;
1917 }
1918 }
1919
1920 /* If it is an array, it shall not be supplied as an actual argument
1921 to an elemental procedure unless an array of the same rank is supplied
1922 as an actual argument corresponding to a nonoptional dummy argument of
1923 that elemental procedure(12.4.1.5). */
1924 formal_optional = false;
1925 if (isym)
1926 iformal = isym->formal;
1927 else
1928 eformal = esym->formal;
1929
1930 for (arg = arg0; arg; arg = arg->next)
1931 {
1932 if (eformal)
1933 {
1934 if (eformal->sym && eformal->sym->attr.optional)
1935 formal_optional = true;
1936 eformal = eformal->next;
1937 }
1938 else if (isym && iformal)
1939 {
1940 if (iformal->optional)
1941 formal_optional = true;
1942 iformal = iformal->next;
1943 }
1944 else if (isym)
1945 formal_optional = true;
1946
1947 if (pedantic && arg->expr != NULL
1948 && arg->expr->expr_type == EXPR_VARIABLE
1949 && arg->expr->symtree->n.sym->attr.optional
1950 && formal_optional
1951 && arg->expr->rank
1952 && (set_by_optional || arg->expr->rank != rank)
1953 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1954 {
1955 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1956 "MISSING, it cannot be the actual argument of an "
1957 "ELEMENTAL procedure unless there is a non-optional "
1958 "argument with the same rank (12.4.1.5)",
1959 arg->expr->symtree->n.sym->name, &arg->expr->where);
1960 return FAILURE;
1961 }
1962 }
1963
1964 for (arg = arg0; arg; arg = arg->next)
1965 {
1966 if (arg->expr == NULL || arg->expr->rank == 0)
1967 continue;
1968
1969 /* Being elemental, the last upper bound of an assumed size array
1970 argument must be present. */
1971 if (resolve_assumed_size_actual (arg->expr))
1972 return FAILURE;
1973
1974 /* Elemental procedure's array actual arguments must conform. */
1975 if (e != NULL)
1976 {
1977 if (gfc_check_conformance (arg->expr, e,
1978 "elemental procedure") == FAILURE)
1979 return FAILURE;
1980 }
1981 else
1982 e = arg->expr;
1983 }
1984
1985 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1986 is an array, the intent inout/out variable needs to be also an array. */
1987 if (rank > 0 && esym && expr == NULL)
1988 for (eformal = esym->formal, arg = arg0; arg && eformal;
1989 arg = arg->next, eformal = eformal->next)
1990 if ((eformal->sym->attr.intent == INTENT_OUT
1991 || eformal->sym->attr.intent == INTENT_INOUT)
1992 && arg->expr && arg->expr->rank == 0)
1993 {
1994 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1995 "ELEMENTAL subroutine '%s' is a scalar, but another "
1996 "actual argument is an array", &arg->expr->where,
1997 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1998 : "INOUT", eformal->sym->name, esym->name);
1999 return FAILURE;
2000 }
2001 return SUCCESS;
2002 }
2003
2004
2005 /* This function does the checking of references to global procedures
2006 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2007 77 and 95 standards. It checks for a gsymbol for the name, making
2008 one if it does not already exist. If it already exists, then the
2009 reference being resolved must correspond to the type of gsymbol.
2010 Otherwise, the new symbol is equipped with the attributes of the
2011 reference. The corresponding code that is called in creating
2012 global entities is parse.c.
2013
2014 In addition, for all but -std=legacy, the gsymbols are used to
2015 check the interfaces of external procedures from the same file.
2016 The namespace of the gsymbol is resolved and then, once this is
2017 done the interface is checked. */
2018
2019
2020 static bool
2021 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2022 {
2023 if (!gsym_ns->proc_name->attr.recursive)
2024 return true;
2025
2026 if (sym->ns == gsym_ns)
2027 return false;
2028
2029 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2030 return false;
2031
2032 return true;
2033 }
2034
2035 static bool
2036 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2037 {
2038 if (gsym_ns->entries)
2039 {
2040 gfc_entry_list *entry = gsym_ns->entries;
2041
2042 for (; entry; entry = entry->next)
2043 {
2044 if (strcmp (sym->name, entry->sym->name) == 0)
2045 {
2046 if (strcmp (gsym_ns->proc_name->name,
2047 sym->ns->proc_name->name) == 0)
2048 return false;
2049
2050 if (sym->ns->parent
2051 && strcmp (gsym_ns->proc_name->name,
2052 sym->ns->parent->proc_name->name) == 0)
2053 return false;
2054 }
2055 }
2056 }
2057 return true;
2058 }
2059
2060 static void
2061 resolve_global_procedure (gfc_symbol *sym, locus *where,
2062 gfc_actual_arglist **actual, int sub)
2063 {
2064 gfc_gsymbol * gsym;
2065 gfc_namespace *ns;
2066 enum gfc_symbol_type type;
2067
2068 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2069
2070 gsym = gfc_get_gsymbol (sym->name);
2071
2072 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2073 gfc_global_used (gsym, where);
2074
2075 if (gfc_option.flag_whole_file
2076 && (sym->attr.if_source == IFSRC_UNKNOWN
2077 || sym->attr.if_source == IFSRC_IFBODY)
2078 && gsym->type != GSYM_UNKNOWN
2079 && gsym->ns
2080 && gsym->ns->resolved != -1
2081 && gsym->ns->proc_name
2082 && not_in_recursive (sym, gsym->ns)
2083 && not_entry_self_reference (sym, gsym->ns))
2084 {
2085 gfc_symbol *def_sym;
2086
2087 /* Resolve the gsymbol namespace if needed. */
2088 if (!gsym->ns->resolved)
2089 {
2090 gfc_dt_list *old_dt_list;
2091 struct gfc_omp_saved_state old_omp_state;
2092
2093 /* Stash away derived types so that the backend_decls do not
2094 get mixed up. */
2095 old_dt_list = gfc_derived_types;
2096 gfc_derived_types = NULL;
2097 /* And stash away openmp state. */
2098 gfc_omp_save_and_clear_state (&old_omp_state);
2099
2100 gfc_resolve (gsym->ns);
2101
2102 /* Store the new derived types with the global namespace. */
2103 if (gfc_derived_types)
2104 gsym->ns->derived_types = gfc_derived_types;
2105
2106 /* Restore the derived types of this namespace. */
2107 gfc_derived_types = old_dt_list;
2108 /* And openmp state. */
2109 gfc_omp_restore_state (&old_omp_state);
2110 }
2111
2112 /* Make sure that translation for the gsymbol occurs before
2113 the procedure currently being resolved. */
2114 ns = gfc_global_ns_list;
2115 for (; ns && ns != gsym->ns; ns = ns->sibling)
2116 {
2117 if (ns->sibling == gsym->ns)
2118 {
2119 ns->sibling = gsym->ns->sibling;
2120 gsym->ns->sibling = gfc_global_ns_list;
2121 gfc_global_ns_list = gsym->ns;
2122 break;
2123 }
2124 }
2125
2126 def_sym = gsym->ns->proc_name;
2127 if (def_sym->attr.entry_master)
2128 {
2129 gfc_entry_list *entry;
2130 for (entry = gsym->ns->entries; entry; entry = entry->next)
2131 if (strcmp (entry->sym->name, sym->name) == 0)
2132 {
2133 def_sym = entry->sym;
2134 break;
2135 }
2136 }
2137
2138 /* Differences in constant character lengths. */
2139 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2140 {
2141 long int l1 = 0, l2 = 0;
2142 gfc_charlen *cl1 = sym->ts.u.cl;
2143 gfc_charlen *cl2 = def_sym->ts.u.cl;
2144
2145 if (cl1 != NULL
2146 && cl1->length != NULL
2147 && cl1->length->expr_type == EXPR_CONSTANT)
2148 l1 = mpz_get_si (cl1->length->value.integer);
2149
2150 if (cl2 != NULL
2151 && cl2->length != NULL
2152 && cl2->length->expr_type == EXPR_CONSTANT)
2153 l2 = mpz_get_si (cl2->length->value.integer);
2154
2155 if (l1 && l2 && l1 != l2)
2156 gfc_error ("Character length mismatch in return type of "
2157 "function '%s' at %L (%ld/%ld)", sym->name,
2158 &sym->declared_at, l1, l2);
2159 }
2160
2161 /* Type mismatch of function return type and expected type. */
2162 if (sym->attr.function
2163 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2164 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2165 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2166 gfc_typename (&def_sym->ts));
2167
2168 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2169 {
2170 gfc_formal_arglist *arg = def_sym->formal;
2171 for ( ; arg; arg = arg->next)
2172 if (!arg->sym)
2173 continue;
2174 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2175 else if (arg->sym->attr.allocatable
2176 || arg->sym->attr.asynchronous
2177 || arg->sym->attr.optional
2178 || arg->sym->attr.pointer
2179 || arg->sym->attr.target
2180 || arg->sym->attr.value
2181 || arg->sym->attr.volatile_)
2182 {
2183 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2184 "has an attribute that requires an explicit "
2185 "interface for this procedure", arg->sym->name,
2186 sym->name, &sym->declared_at);
2187 break;
2188 }
2189 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2190 else if (arg->sym && arg->sym->as
2191 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2192 {
2193 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2194 "argument '%s' must have an explicit interface",
2195 sym->name, &sym->declared_at, arg->sym->name);
2196 break;
2197 }
2198 /* F2008, 12.4.2.2 (2c) */
2199 else if (arg->sym->attr.codimension)
2200 {
2201 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2202 "'%s' must have an explicit interface",
2203 sym->name, &sym->declared_at, arg->sym->name);
2204 break;
2205 }
2206 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2207 else if (false) /* TODO: is a parametrized derived type */
2208 {
2209 gfc_error ("Procedure '%s' at %L with parametrized derived "
2210 "type argument '%s' must have an explicit "
2211 "interface", sym->name, &sym->declared_at,
2212 arg->sym->name);
2213 break;
2214 }
2215 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2216 else if (arg->sym->ts.type == BT_CLASS)
2217 {
2218 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2219 "argument '%s' must have an explicit interface",
2220 sym->name, &sym->declared_at, arg->sym->name);
2221 break;
2222 }
2223 }
2224
2225 if (def_sym->attr.function)
2226 {
2227 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2228 if (def_sym->as && def_sym->as->rank
2229 && (!sym->as || sym->as->rank != def_sym->as->rank))
2230 gfc_error ("The reference to function '%s' at %L either needs an "
2231 "explicit INTERFACE or the rank is incorrect", sym->name,
2232 where);
2233
2234 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2235 if ((def_sym->result->attr.pointer
2236 || def_sym->result->attr.allocatable)
2237 && (sym->attr.if_source != IFSRC_IFBODY
2238 || def_sym->result->attr.pointer
2239 != sym->result->attr.pointer
2240 || def_sym->result->attr.allocatable
2241 != sym->result->attr.allocatable))
2242 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2243 "result must have an explicit interface", sym->name,
2244 where);
2245
2246 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2247 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2248 && def_sym->ts.type == BT_CHARACTER && def_sym->ts.u.cl->length != NULL)
2249 {
2250 gfc_charlen *cl = sym->ts.u.cl;
2251
2252 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2253 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2254 {
2255 gfc_error ("Nonconstant character-length function '%s' at %L "
2256 "must have an explicit interface", sym->name,
2257 &sym->declared_at);
2258 }
2259 }
2260 }
2261
2262 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2263 if (def_sym->attr.elemental && !sym->attr.elemental)
2264 {
2265 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2266 "interface", sym->name, &sym->declared_at);
2267 }
2268
2269 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2270 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2271 {
2272 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2273 "an explicit interface", sym->name, &sym->declared_at);
2274 }
2275
2276 if (gfc_option.flag_whole_file == 1
2277 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2278 && !(gfc_option.warn_std & GFC_STD_GNU)))
2279 gfc_errors_to_warnings (1);
2280
2281 if (sym->attr.if_source != IFSRC_IFBODY)
2282 gfc_procedure_use (def_sym, actual, where);
2283
2284 gfc_errors_to_warnings (0);
2285 }
2286
2287 if (gsym->type == GSYM_UNKNOWN)
2288 {
2289 gsym->type = type;
2290 gsym->where = *where;
2291 }
2292
2293 gsym->used = 1;
2294 }
2295
2296
2297 /************* Function resolution *************/
2298
2299 /* Resolve a function call known to be generic.
2300 Section 14.1.2.4.1. */
2301
2302 static match
2303 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2304 {
2305 gfc_symbol *s;
2306
2307 if (sym->attr.generic)
2308 {
2309 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2310 if (s != NULL)
2311 {
2312 expr->value.function.name = s->name;
2313 expr->value.function.esym = s;
2314
2315 if (s->ts.type != BT_UNKNOWN)
2316 expr->ts = s->ts;
2317 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2318 expr->ts = s->result->ts;
2319
2320 if (s->as != NULL)
2321 expr->rank = s->as->rank;
2322 else if (s->result != NULL && s->result->as != NULL)
2323 expr->rank = s->result->as->rank;
2324
2325 gfc_set_sym_referenced (expr->value.function.esym);
2326
2327 return MATCH_YES;
2328 }
2329
2330 /* TODO: Need to search for elemental references in generic
2331 interface. */
2332 }
2333
2334 if (sym->attr.intrinsic)
2335 return gfc_intrinsic_func_interface (expr, 0);
2336
2337 return MATCH_NO;
2338 }
2339
2340
2341 static gfc_try
2342 resolve_generic_f (gfc_expr *expr)
2343 {
2344 gfc_symbol *sym;
2345 match m;
2346 gfc_interface *intr = NULL;
2347
2348 sym = expr->symtree->n.sym;
2349
2350 for (;;)
2351 {
2352 m = resolve_generic_f0 (expr, sym);
2353 if (m == MATCH_YES)
2354 return SUCCESS;
2355 else if (m == MATCH_ERROR)
2356 return FAILURE;
2357
2358 generic:
2359 if (!intr)
2360 for (intr = sym->generic; intr; intr = intr->next)
2361 if (intr->sym->attr.flavor == FL_DERIVED)
2362 break;
2363
2364 if (sym->ns->parent == NULL)
2365 break;
2366 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2367
2368 if (sym == NULL)
2369 break;
2370 if (!generic_sym (sym))
2371 goto generic;
2372 }
2373
2374 /* Last ditch attempt. See if the reference is to an intrinsic
2375 that possesses a matching interface. 14.1.2.4 */
2376 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2377 {
2378 gfc_error ("There is no specific function for the generic '%s' "
2379 "at %L", expr->symtree->n.sym->name, &expr->where);
2380 return FAILURE;
2381 }
2382
2383 if (intr)
2384 {
2385 if (gfc_convert_to_structure_constructor (expr, intr->sym, NULL, NULL,
2386 false) != SUCCESS)
2387 return FAILURE;
2388 return resolve_structure_cons (expr, 0);
2389 }
2390
2391 m = gfc_intrinsic_func_interface (expr, 0);
2392 if (m == MATCH_YES)
2393 return SUCCESS;
2394
2395 if (m == MATCH_NO)
2396 gfc_error ("Generic function '%s' at %L is not consistent with a "
2397 "specific intrinsic interface", expr->symtree->n.sym->name,
2398 &expr->where);
2399
2400 return FAILURE;
2401 }
2402
2403
2404 /* Resolve a function call known to be specific. */
2405
2406 static match
2407 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2408 {
2409 match m;
2410
2411 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2412 {
2413 if (sym->attr.dummy)
2414 {
2415 sym->attr.proc = PROC_DUMMY;
2416 goto found;
2417 }
2418
2419 sym->attr.proc = PROC_EXTERNAL;
2420 goto found;
2421 }
2422
2423 if (sym->attr.proc == PROC_MODULE
2424 || sym->attr.proc == PROC_ST_FUNCTION
2425 || sym->attr.proc == PROC_INTERNAL)
2426 goto found;
2427
2428 if (sym->attr.intrinsic)
2429 {
2430 m = gfc_intrinsic_func_interface (expr, 1);
2431 if (m == MATCH_YES)
2432 return MATCH_YES;
2433 if (m == MATCH_NO)
2434 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2435 "with an intrinsic", sym->name, &expr->where);
2436
2437 return MATCH_ERROR;
2438 }
2439
2440 return MATCH_NO;
2441
2442 found:
2443 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2444
2445 if (sym->result)
2446 expr->ts = sym->result->ts;
2447 else
2448 expr->ts = sym->ts;
2449 expr->value.function.name = sym->name;
2450 expr->value.function.esym = sym;
2451 if (sym->as != NULL)
2452 expr->rank = sym->as->rank;
2453
2454 return MATCH_YES;
2455 }
2456
2457
2458 static gfc_try
2459 resolve_specific_f (gfc_expr *expr)
2460 {
2461 gfc_symbol *sym;
2462 match m;
2463
2464 sym = expr->symtree->n.sym;
2465
2466 for (;;)
2467 {
2468 m = resolve_specific_f0 (sym, expr);
2469 if (m == MATCH_YES)
2470 return SUCCESS;
2471 if (m == MATCH_ERROR)
2472 return FAILURE;
2473
2474 if (sym->ns->parent == NULL)
2475 break;
2476
2477 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2478
2479 if (sym == NULL)
2480 break;
2481 }
2482
2483 gfc_error ("Unable to resolve the specific function '%s' at %L",
2484 expr->symtree->n.sym->name, &expr->where);
2485
2486 return SUCCESS;
2487 }
2488
2489
2490 /* Resolve a procedure call not known to be generic nor specific. */
2491
2492 static gfc_try
2493 resolve_unknown_f (gfc_expr *expr)
2494 {
2495 gfc_symbol *sym;
2496 gfc_typespec *ts;
2497
2498 sym = expr->symtree->n.sym;
2499
2500 if (sym->attr.dummy)
2501 {
2502 sym->attr.proc = PROC_DUMMY;
2503 expr->value.function.name = sym->name;
2504 goto set_type;
2505 }
2506
2507 /* See if we have an intrinsic function reference. */
2508
2509 if (gfc_is_intrinsic (sym, 0, expr->where))
2510 {
2511 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2512 return SUCCESS;
2513 return FAILURE;
2514 }
2515
2516 /* The reference is to an external name. */
2517
2518 sym->attr.proc = PROC_EXTERNAL;
2519 expr->value.function.name = sym->name;
2520 expr->value.function.esym = expr->symtree->n.sym;
2521
2522 if (sym->as != NULL)
2523 expr->rank = sym->as->rank;
2524
2525 /* Type of the expression is either the type of the symbol or the
2526 default type of the symbol. */
2527
2528 set_type:
2529 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2530
2531 if (sym->ts.type != BT_UNKNOWN)
2532 expr->ts = sym->ts;
2533 else
2534 {
2535 ts = gfc_get_default_type (sym->name, sym->ns);
2536
2537 if (ts->type == BT_UNKNOWN)
2538 {
2539 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2540 sym->name, &expr->where);
2541 return FAILURE;
2542 }
2543 else
2544 expr->ts = *ts;
2545 }
2546
2547 return SUCCESS;
2548 }
2549
2550
2551 /* Return true, if the symbol is an external procedure. */
2552 static bool
2553 is_external_proc (gfc_symbol *sym)
2554 {
2555 if (!sym->attr.dummy && !sym->attr.contained
2556 && !(sym->attr.intrinsic
2557 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2558 && sym->attr.proc != PROC_ST_FUNCTION
2559 && !sym->attr.proc_pointer
2560 && !sym->attr.use_assoc
2561 && sym->name)
2562 return true;
2563
2564 return false;
2565 }
2566
2567
2568 /* Figure out if a function reference is pure or not. Also set the name
2569 of the function for a potential error message. Return nonzero if the
2570 function is PURE, zero if not. */
2571 static int
2572 pure_stmt_function (gfc_expr *, gfc_symbol *);
2573
2574 static int
2575 pure_function (gfc_expr *e, const char **name)
2576 {
2577 int pure;
2578
2579 *name = NULL;
2580
2581 if (e->symtree != NULL
2582 && e->symtree->n.sym != NULL
2583 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2584 return pure_stmt_function (e, e->symtree->n.sym);
2585
2586 if (e->value.function.esym)
2587 {
2588 pure = gfc_pure (e->value.function.esym);
2589 *name = e->value.function.esym->name;
2590 }
2591 else if (e->value.function.isym)
2592 {
2593 pure = e->value.function.isym->pure
2594 || e->value.function.isym->elemental;
2595 *name = e->value.function.isym->name;
2596 }
2597 else
2598 {
2599 /* Implicit functions are not pure. */
2600 pure = 0;
2601 *name = e->value.function.name;
2602 }
2603
2604 return pure;
2605 }
2606
2607
2608 static bool
2609 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2610 int *f ATTRIBUTE_UNUSED)
2611 {
2612 const char *name;
2613
2614 /* Don't bother recursing into other statement functions
2615 since they will be checked individually for purity. */
2616 if (e->expr_type != EXPR_FUNCTION
2617 || !e->symtree
2618 || e->symtree->n.sym == sym
2619 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2620 return false;
2621
2622 return pure_function (e, &name) ? false : true;
2623 }
2624
2625
2626 static int
2627 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2628 {
2629 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2630 }
2631
2632
2633 static gfc_try
2634 is_scalar_expr_ptr (gfc_expr *expr)
2635 {
2636 gfc_try retval = SUCCESS;
2637 gfc_ref *ref;
2638 int start;
2639 int end;
2640
2641 /* See if we have a gfc_ref, which means we have a substring, array
2642 reference, or a component. */
2643 if (expr->ref != NULL)
2644 {
2645 ref = expr->ref;
2646 while (ref->next != NULL)
2647 ref = ref->next;
2648
2649 switch (ref->type)
2650 {
2651 case REF_SUBSTRING:
2652 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2653 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2654 retval = FAILURE;
2655 break;
2656
2657 case REF_ARRAY:
2658 if (ref->u.ar.type == AR_ELEMENT)
2659 retval = SUCCESS;
2660 else if (ref->u.ar.type == AR_FULL)
2661 {
2662 /* The user can give a full array if the array is of size 1. */
2663 if (ref->u.ar.as != NULL
2664 && ref->u.ar.as->rank == 1
2665 && ref->u.ar.as->type == AS_EXPLICIT
2666 && ref->u.ar.as->lower[0] != NULL
2667 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2668 && ref->u.ar.as->upper[0] != NULL
2669 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2670 {
2671 /* If we have a character string, we need to check if
2672 its length is one. */
2673 if (expr->ts.type == BT_CHARACTER)
2674 {
2675 if (expr->ts.u.cl == NULL
2676 || expr->ts.u.cl->length == NULL
2677 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2678 != 0)
2679 retval = FAILURE;
2680 }
2681 else
2682 {
2683 /* We have constant lower and upper bounds. If the
2684 difference between is 1, it can be considered a
2685 scalar.
2686 FIXME: Use gfc_dep_compare_expr instead. */
2687 start = (int) mpz_get_si
2688 (ref->u.ar.as->lower[0]->value.integer);
2689 end = (int) mpz_get_si
2690 (ref->u.ar.as->upper[0]->value.integer);
2691 if (end - start + 1 != 1)
2692 retval = FAILURE;
2693 }
2694 }
2695 else
2696 retval = FAILURE;
2697 }
2698 else
2699 retval = FAILURE;
2700 break;
2701 default:
2702 retval = SUCCESS;
2703 break;
2704 }
2705 }
2706 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2707 {
2708 /* Character string. Make sure it's of length 1. */
2709 if (expr->ts.u.cl == NULL
2710 || expr->ts.u.cl->length == NULL
2711 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2712 retval = FAILURE;
2713 }
2714 else if (expr->rank != 0)
2715 retval = FAILURE;
2716
2717 return retval;
2718 }
2719
2720
2721 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2722 and, in the case of c_associated, set the binding label based on
2723 the arguments. */
2724
2725 static gfc_try
2726 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2727 gfc_symbol **new_sym)
2728 {
2729 char name[GFC_MAX_SYMBOL_LEN + 1];
2730 int optional_arg = 0;
2731 gfc_try retval = SUCCESS;
2732 gfc_symbol *args_sym;
2733 gfc_typespec *arg_ts;
2734 symbol_attribute arg_attr;
2735
2736 if (args->expr->expr_type == EXPR_CONSTANT
2737 || args->expr->expr_type == EXPR_OP
2738 || args->expr->expr_type == EXPR_NULL)
2739 {
2740 gfc_error ("Argument to '%s' at %L is not a variable",
2741 sym->name, &(args->expr->where));
2742 return FAILURE;
2743 }
2744
2745 args_sym = args->expr->symtree->n.sym;
2746
2747 /* The typespec for the actual arg should be that stored in the expr
2748 and not necessarily that of the expr symbol (args_sym), because
2749 the actual expression could be a part-ref of the expr symbol. */
2750 arg_ts = &(args->expr->ts);
2751 arg_attr = gfc_expr_attr (args->expr);
2752
2753 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2754 {
2755 /* If the user gave two args then they are providing something for
2756 the optional arg (the second cptr). Therefore, set the name and
2757 binding label to the c_associated for two cptrs. Otherwise,
2758 set c_associated to expect one cptr. */
2759 if (args->next)
2760 {
2761 /* two args. */
2762 sprintf (name, "%s_2", sym->name);
2763 optional_arg = 1;
2764 }
2765 else
2766 {
2767 /* one arg. */
2768 sprintf (name, "%s_1", sym->name);
2769 optional_arg = 0;
2770 }
2771
2772 /* Get a new symbol for the version of c_associated that
2773 will get called. */
2774 *new_sym = get_iso_c_sym (sym, name, NULL, optional_arg);
2775 }
2776 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2777 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2778 {
2779 sprintf (name, "%s", sym->name);
2780
2781 /* Error check the call. */
2782 if (args->next != NULL)
2783 {
2784 gfc_error_now ("More actual than formal arguments in '%s' "
2785 "call at %L", name, &(args->expr->where));
2786 retval = FAILURE;
2787 }
2788 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2789 {
2790 gfc_ref *ref;
2791 bool seen_section;
2792
2793 /* Make sure we have either the target or pointer attribute. */
2794 if (!arg_attr.target && !arg_attr.pointer)
2795 {
2796 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2797 "a TARGET or an associated pointer",
2798 args_sym->name,
2799 sym->name, &(args->expr->where));
2800 retval = FAILURE;
2801 }
2802
2803 if (gfc_is_coindexed (args->expr))
2804 {
2805 gfc_error_now ("Coindexed argument not permitted"
2806 " in '%s' call at %L", name,
2807 &(args->expr->where));
2808 retval = FAILURE;
2809 }
2810
2811 /* Follow references to make sure there are no array
2812 sections. */
2813 seen_section = false;
2814
2815 for (ref=args->expr->ref; ref; ref = ref->next)
2816 {
2817 if (ref->type == REF_ARRAY)
2818 {
2819 if (ref->u.ar.type == AR_SECTION)
2820 seen_section = true;
2821
2822 if (ref->u.ar.type != AR_ELEMENT)
2823 {
2824 gfc_ref *r;
2825 for (r = ref->next; r; r=r->next)
2826 if (r->type == REF_COMPONENT)
2827 {
2828 gfc_error_now ("Array section not permitted"
2829 " in '%s' call at %L", name,
2830 &(args->expr->where));
2831 retval = FAILURE;
2832 break;
2833 }
2834 }
2835 }
2836 }
2837
2838 if (seen_section && retval == SUCCESS)
2839 gfc_warning ("Array section in '%s' call at %L", name,
2840 &(args->expr->where));
2841
2842 /* See if we have interoperable type and type param. */
2843 if (gfc_verify_c_interop (arg_ts) == SUCCESS
2844 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2845 {
2846 if (args_sym->attr.target == 1)
2847 {
2848 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2849 has the target attribute and is interoperable. */
2850 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2851 allocatable variable that has the TARGET attribute and
2852 is not an array of zero size. */
2853 if (args_sym->attr.allocatable == 1)
2854 {
2855 if (args_sym->attr.dimension != 0
2856 && (args_sym->as && args_sym->as->rank == 0))
2857 {
2858 gfc_error_now ("Allocatable variable '%s' used as a "
2859 "parameter to '%s' at %L must not be "
2860 "an array of zero size",
2861 args_sym->name, sym->name,
2862 &(args->expr->where));
2863 retval = FAILURE;
2864 }
2865 }
2866 else
2867 {
2868 /* A non-allocatable target variable with C
2869 interoperable type and type parameters must be
2870 interoperable. */
2871 if (args_sym && args_sym->attr.dimension)
2872 {
2873 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2874 {
2875 gfc_error ("Assumed-shape array '%s' at %L "
2876 "cannot be an argument to the "
2877 "procedure '%s' because "
2878 "it is not C interoperable",
2879 args_sym->name,
2880 &(args->expr->where), sym->name);
2881 retval = FAILURE;
2882 }
2883 else if (args_sym->as->type == AS_DEFERRED)
2884 {
2885 gfc_error ("Deferred-shape array '%s' at %L "
2886 "cannot be an argument to the "
2887 "procedure '%s' because "
2888 "it is not C interoperable",
2889 args_sym->name,
2890 &(args->expr->where), sym->name);
2891 retval = FAILURE;
2892 }
2893 }
2894
2895 /* Make sure it's not a character string. Arrays of
2896 any type should be ok if the variable is of a C
2897 interoperable type. */
2898 if (arg_ts->type == BT_CHARACTER)
2899 if (arg_ts->u.cl != NULL
2900 && (arg_ts->u.cl->length == NULL
2901 || arg_ts->u.cl->length->expr_type
2902 != EXPR_CONSTANT
2903 || mpz_cmp_si
2904 (arg_ts->u.cl->length->value.integer, 1)
2905 != 0)
2906 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2907 {
2908 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2909 "at %L must have a length of 1",
2910 args_sym->name, sym->name,
2911 &(args->expr->where));
2912 retval = FAILURE;
2913 }
2914 }
2915 }
2916 else if (arg_attr.pointer
2917 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2918 {
2919 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2920 scalar pointer. */
2921 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2922 "associated scalar POINTER", args_sym->name,
2923 sym->name, &(args->expr->where));
2924 retval = FAILURE;
2925 }
2926 }
2927 else
2928 {
2929 /* The parameter is not required to be C interoperable. If it
2930 is not C interoperable, it must be a nonpolymorphic scalar
2931 with no length type parameters. It still must have either
2932 the pointer or target attribute, and it can be
2933 allocatable (but must be allocated when c_loc is called). */
2934 if (args->expr->rank != 0
2935 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2936 {
2937 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2938 "scalar", args_sym->name, sym->name,
2939 &(args->expr->where));
2940 retval = FAILURE;
2941 }
2942 else if (arg_ts->type == BT_CHARACTER
2943 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2944 {
2945 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2946 "%L must have a length of 1",
2947 args_sym->name, sym->name,
2948 &(args->expr->where));
2949 retval = FAILURE;
2950 }
2951 else if (arg_ts->type == BT_CLASS)
2952 {
2953 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2954 "polymorphic", args_sym->name, sym->name,
2955 &(args->expr->where));
2956 retval = FAILURE;
2957 }
2958 }
2959 }
2960 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2961 {
2962 if (args_sym->attr.flavor != FL_PROCEDURE)
2963 {
2964 /* TODO: Update this error message to allow for procedure
2965 pointers once they are implemented. */
2966 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2967 "procedure",
2968 args_sym->name, sym->name,
2969 &(args->expr->where));
2970 retval = FAILURE;
2971 }
2972 else if (args_sym->attr.is_bind_c != 1)
2973 {
2974 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2975 "BIND(C)",
2976 args_sym->name, sym->name,
2977 &(args->expr->where));
2978 retval = FAILURE;
2979 }
2980 }
2981
2982 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2983 *new_sym = sym;
2984 }
2985 else
2986 {
2987 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2988 "iso_c_binding function: '%s'!\n", sym->name);
2989 }
2990
2991 return retval;
2992 }
2993
2994
2995 /* Resolve a function call, which means resolving the arguments, then figuring
2996 out which entity the name refers to. */
2997
2998 static gfc_try
2999 resolve_function (gfc_expr *expr)
3000 {
3001 gfc_actual_arglist *arg;
3002 gfc_symbol *sym;
3003 const char *name;
3004 gfc_try t;
3005 int temp;
3006 procedure_type p = PROC_INTRINSIC;
3007 bool no_formal_args;
3008
3009 sym = NULL;
3010 if (expr->symtree)
3011 sym = expr->symtree->n.sym;
3012
3013 /* If this is a procedure pointer component, it has already been resolved. */
3014 if (gfc_is_proc_ptr_comp (expr, NULL))
3015 return SUCCESS;
3016
3017 if (sym && sym->attr.intrinsic
3018 && resolve_intrinsic (sym, &expr->where) == FAILURE)
3019 return FAILURE;
3020
3021 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3022 {
3023 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
3024 return FAILURE;
3025 }
3026
3027 /* If this ia a deferred TBP with an abstract interface (which may
3028 of course be referenced), expr->value.function.esym will be set. */
3029 if (sym && sym->attr.abstract && !expr->value.function.esym)
3030 {
3031 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3032 sym->name, &expr->where);
3033 return FAILURE;
3034 }
3035
3036 /* Switch off assumed size checking and do this again for certain kinds
3037 of procedure, once the procedure itself is resolved. */
3038 need_full_assumed_size++;
3039
3040 if (expr->symtree && expr->symtree->n.sym)
3041 p = expr->symtree->n.sym->attr.proc;
3042
3043 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3044 inquiry_argument = true;
3045 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
3046
3047 if (resolve_actual_arglist (expr->value.function.actual,
3048 p, no_formal_args) == FAILURE)
3049 {
3050 inquiry_argument = false;
3051 return FAILURE;
3052 }
3053
3054 inquiry_argument = false;
3055
3056 /* Need to setup the call to the correct c_associated, depending on
3057 the number of cptrs to user gives to compare. */
3058 if (sym && sym->attr.is_iso_c == 1)
3059 {
3060 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
3061 == FAILURE)
3062 return FAILURE;
3063
3064 /* Get the symtree for the new symbol (resolved func).
3065 the old one will be freed later, when it's no longer used. */
3066 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
3067 }
3068
3069 /* Resume assumed_size checking. */
3070 need_full_assumed_size--;
3071
3072 /* If the procedure is external, check for usage. */
3073 if (sym && is_external_proc (sym))
3074 resolve_global_procedure (sym, &expr->where,
3075 &expr->value.function.actual, 0);
3076
3077 if (sym && sym->ts.type == BT_CHARACTER
3078 && sym->ts.u.cl
3079 && sym->ts.u.cl->length == NULL
3080 && !sym->attr.dummy
3081 && !sym->ts.deferred
3082 && expr->value.function.esym == NULL
3083 && !sym->attr.contained)
3084 {
3085 /* Internal procedures are taken care of in resolve_contained_fntype. */
3086 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3087 "be used at %L since it is not a dummy argument",
3088 sym->name, &expr->where);
3089 return FAILURE;
3090 }
3091
3092 /* See if function is already resolved. */
3093
3094 if (expr->value.function.name != NULL)
3095 {
3096 if (expr->ts.type == BT_UNKNOWN)
3097 expr->ts = sym->ts;
3098 t = SUCCESS;
3099 }
3100 else
3101 {
3102 /* Apply the rules of section 14.1.2. */
3103
3104 switch (procedure_kind (sym))
3105 {
3106 case PTYPE_GENERIC:
3107 t = resolve_generic_f (expr);
3108 break;
3109
3110 case PTYPE_SPECIFIC:
3111 t = resolve_specific_f (expr);
3112 break;
3113
3114 case PTYPE_UNKNOWN:
3115 t = resolve_unknown_f (expr);
3116 break;
3117
3118 default:
3119 gfc_internal_error ("resolve_function(): bad function type");
3120 }
3121 }
3122
3123 /* If the expression is still a function (it might have simplified),
3124 then we check to see if we are calling an elemental function. */
3125
3126 if (expr->expr_type != EXPR_FUNCTION)
3127 return t;
3128
3129 temp = need_full_assumed_size;
3130 need_full_assumed_size = 0;
3131
3132 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3133 return FAILURE;
3134
3135 if (omp_workshare_flag
3136 && expr->value.function.esym
3137 && ! gfc_elemental (expr->value.function.esym))
3138 {
3139 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3140 "in WORKSHARE construct", expr->value.function.esym->name,
3141 &expr->where);
3142 t = FAILURE;
3143 }
3144
3145 #define GENERIC_ID expr->value.function.isym->id
3146 else if (expr->value.function.actual != NULL
3147 && expr->value.function.isym != NULL
3148 && GENERIC_ID != GFC_ISYM_LBOUND
3149 && GENERIC_ID != GFC_ISYM_LEN
3150 && GENERIC_ID != GFC_ISYM_LOC
3151 && GENERIC_ID != GFC_ISYM_PRESENT)
3152 {
3153 /* Array intrinsics must also have the last upper bound of an
3154 assumed size array argument. UBOUND and SIZE have to be
3155 excluded from the check if the second argument is anything
3156 than a constant. */
3157
3158 for (arg = expr->value.function.actual; arg; arg = arg->next)
3159 {
3160 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3161 && arg->next != NULL && arg->next->expr)
3162 {
3163 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3164 break;
3165
3166 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3167 break;
3168
3169 if ((int)mpz_get_si (arg->next->expr->value.integer)
3170 < arg->expr->rank)
3171 break;
3172 }
3173
3174 if (arg->expr != NULL
3175 && arg->expr->rank > 0
3176 && resolve_assumed_size_actual (arg->expr))
3177 return FAILURE;
3178 }
3179 }
3180 #undef GENERIC_ID
3181
3182 need_full_assumed_size = temp;
3183 name = NULL;
3184
3185 if (!pure_function (expr, &name) && name)
3186 {
3187 if (forall_flag)
3188 {
3189 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3190 "FORALL %s", name, &expr->where,
3191 forall_flag == 2 ? "mask" : "block");
3192 t = FAILURE;
3193 }
3194 else if (do_concurrent_flag)
3195 {
3196 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3197 "DO CONCURRENT %s", name, &expr->where,
3198 do_concurrent_flag == 2 ? "mask" : "block");
3199 t = FAILURE;
3200 }
3201 else if (gfc_pure (NULL))
3202 {
3203 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3204 "procedure within a PURE procedure", name, &expr->where);
3205 t = FAILURE;
3206 }
3207
3208 if (gfc_implicit_pure (NULL))
3209 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3210 }
3211
3212 /* Functions without the RECURSIVE attribution are not allowed to
3213 * call themselves. */
3214 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3215 {
3216 gfc_symbol *esym;
3217 esym = expr->value.function.esym;
3218
3219 if (is_illegal_recursion (esym, gfc_current_ns))
3220 {
3221 if (esym->attr.entry && esym->ns->entries)
3222 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3223 " function '%s' is not RECURSIVE",
3224 esym->name, &expr->where, esym->ns->entries->sym->name);
3225 else
3226 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3227 " is not RECURSIVE", esym->name, &expr->where);
3228
3229 t = FAILURE;
3230 }
3231 }
3232
3233 /* Character lengths of use associated functions may contains references to
3234 symbols not referenced from the current program unit otherwise. Make sure
3235 those symbols are marked as referenced. */
3236
3237 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3238 && expr->value.function.esym->attr.use_assoc)
3239 {
3240 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3241 }
3242
3243 /* Make sure that the expression has a typespec that works. */
3244 if (expr->ts.type == BT_UNKNOWN)
3245 {
3246 if (expr->symtree->n.sym->result
3247 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3248 && !expr->symtree->n.sym->result->attr.proc_pointer)
3249 expr->ts = expr->symtree->n.sym->result->ts;
3250 }
3251
3252 return t;
3253 }
3254
3255
3256 /************* Subroutine resolution *************/
3257
3258 static void
3259 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3260 {
3261 if (gfc_pure (sym))
3262 return;
3263
3264 if (forall_flag)
3265 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3266 sym->name, &c->loc);
3267 else if (do_concurrent_flag)
3268 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3269 "PURE", sym->name, &c->loc);
3270 else if (gfc_pure (NULL))
3271 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3272 &c->loc);
3273
3274 if (gfc_implicit_pure (NULL))
3275 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3276 }
3277
3278
3279 static match
3280 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3281 {
3282 gfc_symbol *s;
3283
3284 if (sym->attr.generic)
3285 {
3286 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3287 if (s != NULL)
3288 {
3289 c->resolved_sym = s;
3290 pure_subroutine (c, s);
3291 return MATCH_YES;
3292 }
3293
3294 /* TODO: Need to search for elemental references in generic interface. */
3295 }
3296
3297 if (sym->attr.intrinsic)
3298 return gfc_intrinsic_sub_interface (c, 0);
3299
3300 return MATCH_NO;
3301 }
3302
3303
3304 static gfc_try
3305 resolve_generic_s (gfc_code *c)
3306 {
3307 gfc_symbol *sym;
3308 match m;
3309
3310 sym = c->symtree->n.sym;
3311
3312 for (;;)
3313 {
3314 m = resolve_generic_s0 (c, sym);
3315 if (m == MATCH_YES)
3316 return SUCCESS;
3317 else if (m == MATCH_ERROR)
3318 return FAILURE;
3319
3320 generic:
3321 if (sym->ns->parent == NULL)
3322 break;
3323 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3324
3325 if (sym == NULL)
3326 break;
3327 if (!generic_sym (sym))
3328 goto generic;
3329 }
3330
3331 /* Last ditch attempt. See if the reference is to an intrinsic
3332 that possesses a matching interface. 14.1.2.4 */
3333 sym = c->symtree->n.sym;
3334
3335 if (!gfc_is_intrinsic (sym, 1, c->loc))
3336 {
3337 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3338 sym->name, &c->loc);
3339 return FAILURE;
3340 }
3341
3342 m = gfc_intrinsic_sub_interface (c, 0);
3343 if (m == MATCH_YES)
3344 return SUCCESS;
3345 if (m == MATCH_NO)
3346 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3347 "intrinsic subroutine interface", sym->name, &c->loc);
3348
3349 return FAILURE;
3350 }
3351
3352
3353 /* Set the name and binding label of the subroutine symbol in the call
3354 expression represented by 'c' to include the type and kind of the
3355 second parameter. This function is for resolving the appropriate
3356 version of c_f_pointer() and c_f_procpointer(). For example, a
3357 call to c_f_pointer() for a default integer pointer could have a
3358 name of c_f_pointer_i4. If no second arg exists, which is an error
3359 for these two functions, it defaults to the generic symbol's name
3360 and binding label. */
3361
3362 static void
3363 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3364 char *name, const char **binding_label)
3365 {
3366 gfc_expr *arg = NULL;
3367 char type;
3368 int kind;
3369
3370 /* The second arg of c_f_pointer and c_f_procpointer determines
3371 the type and kind for the procedure name. */
3372 arg = c->ext.actual->next->expr;
3373
3374 if (arg != NULL)
3375 {
3376 /* Set up the name to have the given symbol's name,
3377 plus the type and kind. */
3378 /* a derived type is marked with the type letter 'u' */
3379 if (arg->ts.type == BT_DERIVED)
3380 {
3381 type = 'd';
3382 kind = 0; /* set the kind as 0 for now */
3383 }
3384 else
3385 {
3386 type = gfc_type_letter (arg->ts.type);
3387 kind = arg->ts.kind;
3388 }
3389
3390 if (arg->ts.type == BT_CHARACTER)
3391 /* Kind info for character strings not needed. */
3392 kind = 0;
3393
3394 sprintf (name, "%s_%c%d", sym->name, type, kind);
3395 /* Set up the binding label as the given symbol's label plus
3396 the type and kind. */
3397 *binding_label = gfc_get_string ("%s_%c%d", sym->binding_label, type,
3398 kind);
3399 }
3400 else
3401 {
3402 /* If the second arg is missing, set the name and label as
3403 was, cause it should at least be found, and the missing
3404 arg error will be caught by compare_parameters(). */
3405 sprintf (name, "%s", sym->name);
3406 *binding_label = sym->binding_label;
3407 }
3408
3409 return;
3410 }
3411
3412
3413 /* Resolve a generic version of the iso_c_binding procedure given
3414 (sym) to the specific one based on the type and kind of the
3415 argument(s). Currently, this function resolves c_f_pointer() and
3416 c_f_procpointer based on the type and kind of the second argument
3417 (FPTR). Other iso_c_binding procedures aren't specially handled.
3418 Upon successfully exiting, c->resolved_sym will hold the resolved
3419 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3420 otherwise. */
3421
3422 match
3423 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3424 {
3425 gfc_symbol *new_sym;
3426 /* this is fine, since we know the names won't use the max */
3427 char name[GFC_MAX_SYMBOL_LEN + 1];
3428 const char* binding_label;
3429 /* default to success; will override if find error */
3430 match m = MATCH_YES;
3431
3432 /* Make sure the actual arguments are in the necessary order (based on the
3433 formal args) before resolving. */
3434 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3435
3436 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3437 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3438 {
3439 set_name_and_label (c, sym, name, &binding_label);
3440
3441 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3442 {
3443 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3444 {
3445 /* Make sure we got a third arg if the second arg has non-zero
3446 rank. We must also check that the type and rank are
3447 correct since we short-circuit this check in
3448 gfc_procedure_use() (called above to sort actual args). */
3449 if (c->ext.actual->next->expr->rank != 0)
3450 {
3451 if(c->ext.actual->next->next == NULL
3452 || c->ext.actual->next->next->expr == NULL)
3453 {
3454 m = MATCH_ERROR;
3455 gfc_error ("Missing SHAPE parameter for call to %s "
3456 "at %L", sym->name, &(c->loc));
3457 }
3458 else if (c->ext.actual->next->next->expr->ts.type
3459 != BT_INTEGER
3460 || c->ext.actual->next->next->expr->rank != 1)
3461 {
3462 m = MATCH_ERROR;
3463 gfc_error ("SHAPE parameter for call to %s at %L must "
3464 "be a rank 1 INTEGER array", sym->name,
3465 &(c->loc));
3466 }
3467 }
3468 }
3469 }
3470
3471 if (m != MATCH_ERROR)
3472 {
3473 /* the 1 means to add the optional arg to formal list */
3474 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3475
3476 /* for error reporting, say it's declared where the original was */
3477 new_sym->declared_at = sym->declared_at;
3478 }
3479 }
3480 else
3481 {
3482 /* no differences for c_loc or c_funloc */
3483 new_sym = sym;
3484 }
3485
3486 /* set the resolved symbol */
3487 if (m != MATCH_ERROR)
3488 c->resolved_sym = new_sym;
3489 else
3490 c->resolved_sym = sym;
3491
3492 return m;
3493 }
3494
3495
3496 /* Resolve a subroutine call known to be specific. */
3497
3498 static match
3499 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3500 {
3501 match m;
3502
3503 if(sym->attr.is_iso_c)
3504 {
3505 m = gfc_iso_c_sub_interface (c,sym);
3506 return m;
3507 }
3508
3509 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3510 {
3511 if (sym->attr.dummy)
3512 {
3513 sym->attr.proc = PROC_DUMMY;
3514 goto found;
3515 }
3516
3517 sym->attr.proc = PROC_EXTERNAL;
3518 goto found;
3519 }
3520
3521 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3522 goto found;
3523
3524 if (sym->attr.intrinsic)
3525 {
3526 m = gfc_intrinsic_sub_interface (c, 1);
3527 if (m == MATCH_YES)
3528 return MATCH_YES;
3529 if (m == MATCH_NO)
3530 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3531 "with an intrinsic", sym->name, &c->loc);
3532
3533 return MATCH_ERROR;
3534 }
3535
3536 return MATCH_NO;
3537
3538 found:
3539 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3540
3541 c->resolved_sym = sym;
3542 pure_subroutine (c, sym);
3543
3544 return MATCH_YES;
3545 }
3546
3547
3548 static gfc_try
3549 resolve_specific_s (gfc_code *c)
3550 {
3551 gfc_symbol *sym;
3552 match m;
3553
3554 sym = c->symtree->n.sym;
3555
3556 for (;;)
3557 {
3558 m = resolve_specific_s0 (c, sym);
3559 if (m == MATCH_YES)
3560 return SUCCESS;
3561 if (m == MATCH_ERROR)
3562 return FAILURE;
3563
3564 if (sym->ns->parent == NULL)
3565 break;
3566
3567 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3568
3569 if (sym == NULL)
3570 break;
3571 }
3572
3573 sym = c->symtree->n.sym;
3574 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3575 sym->name, &c->loc);
3576
3577 return FAILURE;
3578 }
3579
3580
3581 /* Resolve a subroutine call not known to be generic nor specific. */
3582
3583 static gfc_try
3584 resolve_unknown_s (gfc_code *c)
3585 {
3586 gfc_symbol *sym;
3587
3588 sym = c->symtree->n.sym;
3589
3590 if (sym->attr.dummy)
3591 {
3592 sym->attr.proc = PROC_DUMMY;
3593 goto found;
3594 }
3595
3596 /* See if we have an intrinsic function reference. */
3597
3598 if (gfc_is_intrinsic (sym, 1, c->loc))
3599 {
3600 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3601 return SUCCESS;
3602 return FAILURE;
3603 }
3604
3605 /* The reference is to an external name. */
3606
3607 found:
3608 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3609
3610 c->resolved_sym = sym;
3611
3612 pure_subroutine (c, sym);
3613
3614 return SUCCESS;
3615 }
3616
3617
3618 /* Resolve a subroutine call. Although it was tempting to use the same code
3619 for functions, subroutines and functions are stored differently and this
3620 makes things awkward. */
3621
3622 static gfc_try
3623 resolve_call (gfc_code *c)
3624 {
3625 gfc_try t;
3626 procedure_type ptype = PROC_INTRINSIC;
3627 gfc_symbol *csym, *sym;
3628 bool no_formal_args;
3629
3630 csym = c->symtree ? c->symtree->n.sym : NULL;
3631
3632 if (csym && csym->ts.type != BT_UNKNOWN)
3633 {
3634 gfc_error ("'%s' at %L has a type, which is not consistent with "
3635 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3636 return FAILURE;
3637 }
3638
3639 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3640 {
3641 gfc_symtree *st;
3642 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3643 sym = st ? st->n.sym : NULL;
3644 if (sym && csym != sym
3645 && sym->ns == gfc_current_ns
3646 && sym->attr.flavor == FL_PROCEDURE
3647 && sym->attr.contained)
3648 {
3649 sym->refs++;
3650 if (csym->attr.generic)
3651 c->symtree->n.sym = sym;
3652 else
3653 c->symtree = st;
3654 csym = c->symtree->n.sym;
3655 }
3656 }
3657
3658 /* If this ia a deferred TBP with an abstract interface
3659 (which may of course be referenced), c->expr1 will be set. */
3660 if (csym && csym->attr.abstract && !c->expr1)
3661 {
3662 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3663 csym->name, &c->loc);
3664 return FAILURE;
3665 }
3666
3667 /* Subroutines without the RECURSIVE attribution are not allowed to
3668 * call themselves. */
3669 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3670 {
3671 if (csym->attr.entry && csym->ns->entries)
3672 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3673 " subroutine '%s' is not RECURSIVE",
3674 csym->name, &c->loc, csym->ns->entries->sym->name);
3675 else
3676 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3677 " is not RECURSIVE", csym->name, &c->loc);
3678
3679 t = FAILURE;
3680 }
3681
3682 /* Switch off assumed size checking and do this again for certain kinds
3683 of procedure, once the procedure itself is resolved. */
3684 need_full_assumed_size++;
3685
3686 if (csym)
3687 ptype = csym->attr.proc;
3688
3689 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3690 if (resolve_actual_arglist (c->ext.actual, ptype,
3691 no_formal_args) == FAILURE)
3692 return FAILURE;
3693
3694 /* Resume assumed_size checking. */
3695 need_full_assumed_size--;
3696
3697 /* If external, check for usage. */
3698 if (csym && is_external_proc (csym))
3699 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3700
3701 t = SUCCESS;
3702 if (c->resolved_sym == NULL)
3703 {
3704 c->resolved_isym = NULL;
3705 switch (procedure_kind (csym))
3706 {
3707 case PTYPE_GENERIC:
3708 t = resolve_generic_s (c);
3709 break;
3710
3711 case PTYPE_SPECIFIC:
3712 t = resolve_specific_s (c);
3713 break;
3714
3715 case PTYPE_UNKNOWN:
3716 t = resolve_unknown_s (c);
3717 break;
3718
3719 default:
3720 gfc_internal_error ("resolve_subroutine(): bad function type");
3721 }
3722 }
3723
3724 /* Some checks of elemental subroutine actual arguments. */
3725 if (resolve_elemental_actual (NULL, c) == FAILURE)
3726 return FAILURE;
3727
3728 return t;
3729 }
3730
3731
3732 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3733 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3734 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3735 if their shapes do not match. If either op1->shape or op2->shape is
3736 NULL, return SUCCESS. */
3737
3738 static gfc_try
3739 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3740 {
3741 gfc_try t;
3742 int i;
3743
3744 t = SUCCESS;
3745
3746 if (op1->shape != NULL && op2->shape != NULL)
3747 {
3748 for (i = 0; i < op1->rank; i++)
3749 {
3750 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3751 {
3752 gfc_error ("Shapes for operands at %L and %L are not conformable",
3753 &op1->where, &op2->where);
3754 t = FAILURE;
3755 break;
3756 }
3757 }
3758 }
3759
3760 return t;
3761 }
3762
3763
3764 /* Resolve an operator expression node. This can involve replacing the
3765 operation with a user defined function call. */
3766
3767 static gfc_try
3768 resolve_operator (gfc_expr *e)
3769 {
3770 gfc_expr *op1, *op2;
3771 char msg[200];
3772 bool dual_locus_error;
3773 gfc_try t;
3774
3775 /* Resolve all subnodes-- give them types. */
3776
3777 switch (e->value.op.op)
3778 {
3779 default:
3780 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3781 return FAILURE;
3782
3783 /* Fall through... */
3784
3785 case INTRINSIC_NOT:
3786 case INTRINSIC_UPLUS:
3787 case INTRINSIC_UMINUS:
3788 case INTRINSIC_PARENTHESES:
3789 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3790 return FAILURE;
3791 break;
3792 }
3793
3794 /* Typecheck the new node. */
3795
3796 op1 = e->value.op.op1;
3797 op2 = e->value.op.op2;
3798 dual_locus_error = false;
3799
3800 if ((op1 && op1->expr_type == EXPR_NULL)
3801 || (op2 && op2->expr_type == EXPR_NULL))
3802 {
3803 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3804 goto bad_op;
3805 }
3806
3807 switch (e->value.op.op)
3808 {
3809 case INTRINSIC_UPLUS:
3810 case INTRINSIC_UMINUS:
3811 if (op1->ts.type == BT_INTEGER
3812 || op1->ts.type == BT_REAL
3813 || op1->ts.type == BT_COMPLEX)
3814 {
3815 e->ts = op1->ts;
3816 break;
3817 }
3818
3819 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3820 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3821 goto bad_op;
3822
3823 case INTRINSIC_PLUS:
3824 case INTRINSIC_MINUS:
3825 case INTRINSIC_TIMES:
3826 case INTRINSIC_DIVIDE:
3827 case INTRINSIC_POWER:
3828 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3829 {
3830 gfc_type_convert_binary (e, 1);
3831 break;
3832 }
3833
3834 sprintf (msg,
3835 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3836 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3837 gfc_typename (&op2->ts));
3838 goto bad_op;
3839
3840 case INTRINSIC_CONCAT:
3841 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3842 && op1->ts.kind == op2->ts.kind)
3843 {
3844 e->ts.type = BT_CHARACTER;
3845 e->ts.kind = op1->ts.kind;
3846 break;
3847 }
3848
3849 sprintf (msg,
3850 _("Operands of string concatenation operator at %%L are %s/%s"),
3851 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3852 goto bad_op;
3853
3854 case INTRINSIC_AND:
3855 case INTRINSIC_OR:
3856 case INTRINSIC_EQV:
3857 case INTRINSIC_NEQV:
3858 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3859 {
3860 e->ts.type = BT_LOGICAL;
3861 e->ts.kind = gfc_kind_max (op1, op2);
3862 if (op1->ts.kind < e->ts.kind)
3863 gfc_convert_type (op1, &e->ts, 2);
3864 else if (op2->ts.kind < e->ts.kind)
3865 gfc_convert_type (op2, &e->ts, 2);
3866 break;
3867 }
3868
3869 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3870 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3871 gfc_typename (&op2->ts));
3872
3873 goto bad_op;
3874
3875 case INTRINSIC_NOT:
3876 if (op1->ts.type == BT_LOGICAL)
3877 {
3878 e->ts.type = BT_LOGICAL;
3879 e->ts.kind = op1->ts.kind;
3880 break;
3881 }
3882
3883 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3884 gfc_typename (&op1->ts));
3885 goto bad_op;
3886
3887 case INTRINSIC_GT:
3888 case INTRINSIC_GT_OS:
3889 case INTRINSIC_GE:
3890 case INTRINSIC_GE_OS:
3891 case INTRINSIC_LT:
3892 case INTRINSIC_LT_OS:
3893 case INTRINSIC_LE:
3894 case INTRINSIC_LE_OS:
3895 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3896 {
3897 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3898 goto bad_op;
3899 }
3900
3901 /* Fall through... */
3902
3903 case INTRINSIC_EQ:
3904 case INTRINSIC_EQ_OS:
3905 case INTRINSIC_NE:
3906 case INTRINSIC_NE_OS:
3907 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3908 && op1->ts.kind == op2->ts.kind)
3909 {
3910 e->ts.type = BT_LOGICAL;
3911 e->ts.kind = gfc_default_logical_kind;
3912 break;
3913 }
3914
3915 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3916 {
3917 gfc_type_convert_binary (e, 1);
3918
3919 e->ts.type = BT_LOGICAL;
3920 e->ts.kind = gfc_default_logical_kind;
3921 break;
3922 }
3923
3924 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3925 sprintf (msg,
3926 _("Logicals at %%L must be compared with %s instead of %s"),
3927 (e->value.op.op == INTRINSIC_EQ
3928 || e->value.op.op == INTRINSIC_EQ_OS)
3929 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3930 else
3931 sprintf (msg,
3932 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3933 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3934 gfc_typename (&op2->ts));
3935
3936 goto bad_op;
3937
3938 case INTRINSIC_USER:
3939 if (e->value.op.uop->op == NULL)
3940 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3941 else if (op2 == NULL)
3942 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3943 e->value.op.uop->name, gfc_typename (&op1->ts));
3944 else
3945 {
3946 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3947 e->value.op.uop->name, gfc_typename (&op1->ts),
3948 gfc_typename (&op2->ts));
3949 e->value.op.uop->op->sym->attr.referenced = 1;
3950 }
3951
3952 goto bad_op;
3953
3954 case INTRINSIC_PARENTHESES:
3955 e->ts = op1->ts;
3956 if (e->ts.type == BT_CHARACTER)
3957 e->ts.u.cl = op1->ts.u.cl;
3958 break;
3959
3960 default:
3961 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3962 }
3963
3964 /* Deal with arrayness of an operand through an operator. */
3965
3966 t = SUCCESS;
3967
3968 switch (e->value.op.op)
3969 {
3970 case INTRINSIC_PLUS:
3971 case INTRINSIC_MINUS:
3972 case INTRINSIC_TIMES:
3973 case INTRINSIC_DIVIDE:
3974 case INTRINSIC_POWER:
3975 case INTRINSIC_CONCAT:
3976 case INTRINSIC_AND:
3977 case INTRINSIC_OR:
3978 case INTRINSIC_EQV:
3979 case INTRINSIC_NEQV:
3980 case INTRINSIC_EQ:
3981 case INTRINSIC_EQ_OS:
3982 case INTRINSIC_NE:
3983 case INTRINSIC_NE_OS:
3984 case INTRINSIC_GT:
3985 case INTRINSIC_GT_OS:
3986 case INTRINSIC_GE:
3987 case INTRINSIC_GE_OS:
3988 case INTRINSIC_LT:
3989 case INTRINSIC_LT_OS:
3990 case INTRINSIC_LE:
3991 case INTRINSIC_LE_OS:
3992
3993 if (op1->rank == 0 && op2->rank == 0)
3994 e->rank = 0;
3995
3996 if (op1->rank == 0 && op2->rank != 0)
3997 {
3998 e->rank = op2->rank;
3999
4000 if (e->shape == NULL)
4001 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4002 }
4003
4004 if (op1->rank != 0 && op2->rank == 0)
4005 {
4006 e->rank = op1->rank;
4007
4008 if (e->shape == NULL)
4009 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4010 }
4011
4012 if (op1->rank != 0 && op2->rank != 0)
4013 {
4014 if (op1->rank == op2->rank)
4015 {
4016 e->rank = op1->rank;
4017 if (e->shape == NULL)
4018 {
4019 t = compare_shapes (op1, op2);
4020 if (t == FAILURE)
4021 e->shape = NULL;
4022 else
4023 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4024 }
4025 }
4026 else
4027 {
4028 /* Allow higher level expressions to work. */
4029 e->rank = 0;
4030
4031 /* Try user-defined operators, and otherwise throw an error. */
4032 dual_locus_error = true;
4033 sprintf (msg,
4034 _("Inconsistent ranks for operator at %%L and %%L"));
4035 goto bad_op;
4036 }
4037 }
4038
4039 break;
4040
4041 case INTRINSIC_PARENTHESES:
4042 case INTRINSIC_NOT:
4043 case INTRINSIC_UPLUS:
4044 case INTRINSIC_UMINUS:
4045 /* Simply copy arrayness attribute */
4046 e->rank = op1->rank;
4047
4048 if (e->shape == NULL)
4049 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4050
4051 break;
4052
4053 default:
4054 break;
4055 }
4056
4057 /* Attempt to simplify the expression. */
4058 if (t == SUCCESS)
4059 {
4060 t = gfc_simplify_expr (e, 0);
4061 /* Some calls do not succeed in simplification and return FAILURE
4062 even though there is no error; e.g. variable references to
4063 PARAMETER arrays. */
4064 if (!gfc_is_constant_expr (e))
4065 t = SUCCESS;
4066 }
4067 return t;
4068
4069 bad_op:
4070
4071 {
4072 match m = gfc_extend_expr (e);
4073 if (m == MATCH_YES)
4074 return SUCCESS;
4075 if (m == MATCH_ERROR)
4076 return FAILURE;
4077 }
4078
4079 if (dual_locus_error)
4080 gfc_error (msg, &op1->where, &op2->where);
4081 else
4082 gfc_error (msg, &e->where);
4083
4084 return FAILURE;
4085 }
4086
4087
4088 /************** Array resolution subroutines **************/
4089
4090 typedef enum
4091 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
4092 comparison;
4093
4094 /* Compare two integer expressions. */
4095
4096 static comparison
4097 compare_bound (gfc_expr *a, gfc_expr *b)
4098 {
4099 int i;
4100
4101 if (a == NULL || a->expr_type != EXPR_CONSTANT
4102 || b == NULL || b->expr_type != EXPR_CONSTANT)
4103 return CMP_UNKNOWN;
4104
4105 /* If either of the types isn't INTEGER, we must have
4106 raised an error earlier. */
4107
4108 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4109 return CMP_UNKNOWN;
4110
4111 i = mpz_cmp (a->value.integer, b->value.integer);
4112
4113 if (i < 0)
4114 return CMP_LT;
4115 if (i > 0)
4116 return CMP_GT;
4117 return CMP_EQ;
4118 }
4119
4120
4121 /* Compare an integer expression with an integer. */
4122
4123 static comparison
4124 compare_bound_int (gfc_expr *a, int b)
4125 {
4126 int i;
4127
4128 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4129 return CMP_UNKNOWN;
4130
4131 if (a->ts.type != BT_INTEGER)
4132 gfc_internal_error ("compare_bound_int(): Bad expression");
4133
4134 i = mpz_cmp_si (a->value.integer, b);
4135
4136 if (i < 0)
4137 return CMP_LT;
4138 if (i > 0)
4139 return CMP_GT;
4140 return CMP_EQ;
4141 }
4142
4143
4144 /* Compare an integer expression with a mpz_t. */
4145
4146 static comparison
4147 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4148 {
4149 int i;
4150
4151 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4152 return CMP_UNKNOWN;
4153
4154 if (a->ts.type != BT_INTEGER)
4155 gfc_internal_error ("compare_bound_int(): Bad expression");
4156
4157 i = mpz_cmp (a->value.integer, b);
4158
4159 if (i < 0)
4160 return CMP_LT;
4161 if (i > 0)
4162 return CMP_GT;
4163 return CMP_EQ;
4164 }
4165
4166
4167 /* Compute the last value of a sequence given by a triplet.
4168 Return 0 if it wasn't able to compute the last value, or if the
4169 sequence if empty, and 1 otherwise. */
4170
4171 static int
4172 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4173 gfc_expr *stride, mpz_t last)
4174 {
4175 mpz_t rem;
4176
4177 if (start == NULL || start->expr_type != EXPR_CONSTANT
4178 || end == NULL || end->expr_type != EXPR_CONSTANT
4179 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4180 return 0;
4181
4182 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4183 || (stride != NULL && stride->ts.type != BT_INTEGER))
4184 return 0;
4185
4186 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4187 {
4188 if (compare_bound (start, end) == CMP_GT)
4189 return 0;
4190 mpz_set (last, end->value.integer);
4191 return 1;
4192 }
4193
4194 if (compare_bound_int (stride, 0) == CMP_GT)
4195 {
4196 /* Stride is positive */
4197 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4198 return 0;
4199 }
4200 else
4201 {
4202 /* Stride is negative */
4203 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4204 return 0;
4205 }
4206
4207 mpz_init (rem);
4208 mpz_sub (rem, end->value.integer, start->value.integer);
4209 mpz_tdiv_r (rem, rem, stride->value.integer);
4210 mpz_sub (last, end->value.integer, rem);
4211 mpz_clear (rem);
4212
4213 return 1;
4214 }
4215
4216
4217 /* Compare a single dimension of an array reference to the array
4218 specification. */
4219
4220 static gfc_try
4221 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4222 {
4223 mpz_t last_value;
4224
4225 if (ar->dimen_type[i] == DIMEN_STAR)
4226 {
4227 gcc_assert (ar->stride[i] == NULL);
4228 /* This implies [*] as [*:] and [*:3] are not possible. */
4229 if (ar->start[i] == NULL)
4230 {
4231 gcc_assert (ar->end[i] == NULL);
4232 return SUCCESS;
4233 }
4234 }
4235
4236 /* Given start, end and stride values, calculate the minimum and
4237 maximum referenced indexes. */
4238
4239 switch (ar->dimen_type[i])
4240 {
4241 case DIMEN_VECTOR:
4242 case DIMEN_THIS_IMAGE:
4243 break;
4244
4245 case DIMEN_STAR:
4246 case DIMEN_ELEMENT:
4247 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4248 {
4249 if (i < as->rank)
4250 gfc_warning ("Array reference at %L is out of bounds "
4251 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4252 mpz_get_si (ar->start[i]->value.integer),
4253 mpz_get_si (as->lower[i]->value.integer), i+1);
4254 else
4255 gfc_warning ("Array reference at %L is out of bounds "
4256 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4257 mpz_get_si (ar->start[i]->value.integer),
4258 mpz_get_si (as->lower[i]->value.integer),
4259 i + 1 - as->rank);
4260 return SUCCESS;
4261 }
4262 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4263 {
4264 if (i < as->rank)
4265 gfc_warning ("Array reference at %L is out of bounds "
4266 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4267 mpz_get_si (ar->start[i]->value.integer),
4268 mpz_get_si (as->upper[i]->value.integer), i+1);
4269 else
4270 gfc_warning ("Array reference at %L is out of bounds "
4271 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4272 mpz_get_si (ar->start[i]->value.integer),
4273 mpz_get_si (as->upper[i]->value.integer),
4274 i + 1 - as->rank);
4275 return SUCCESS;
4276 }
4277
4278 break;
4279
4280 case DIMEN_RANGE:
4281 {
4282 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4283 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4284
4285 comparison comp_start_end = compare_bound (AR_START, AR_END);
4286
4287 /* Check for zero stride, which is not allowed. */
4288 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4289 {
4290 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4291 return FAILURE;
4292 }
4293
4294 /* if start == len || (stride > 0 && start < len)
4295 || (stride < 0 && start > len),
4296 then the array section contains at least one element. In this
4297 case, there is an out-of-bounds access if
4298 (start < lower || start > upper). */
4299 if (compare_bound (AR_START, AR_END) == CMP_EQ
4300 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4301 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4302 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4303 && comp_start_end == CMP_GT))
4304 {
4305 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4306 {
4307 gfc_warning ("Lower array reference at %L is out of bounds "
4308 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4309 mpz_get_si (AR_START->value.integer),
4310 mpz_get_si (as->lower[i]->value.integer), i+1);
4311 return SUCCESS;
4312 }
4313 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4314 {
4315 gfc_warning ("Lower array reference at %L is out of bounds "
4316 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4317 mpz_get_si (AR_START->value.integer),
4318 mpz_get_si (as->upper[i]->value.integer), i+1);
4319 return SUCCESS;
4320 }
4321 }
4322
4323 /* If we can compute the highest index of the array section,
4324 then it also has to be between lower and upper. */
4325 mpz_init (last_value);
4326 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4327 last_value))
4328 {
4329 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4330 {
4331 gfc_warning ("Upper array reference at %L is out of bounds "
4332 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4333 mpz_get_si (last_value),
4334 mpz_get_si (as->lower[i]->value.integer), i+1);
4335 mpz_clear (last_value);
4336 return SUCCESS;
4337 }
4338 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4339 {
4340 gfc_warning ("Upper array reference at %L is out of bounds "
4341 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4342 mpz_get_si (last_value),
4343 mpz_get_si (as->upper[i]->value.integer), i+1);
4344 mpz_clear (last_value);
4345 return SUCCESS;
4346 }
4347 }
4348 mpz_clear (last_value);
4349
4350 #undef AR_START
4351 #undef AR_END
4352 }
4353 break;
4354
4355 default:
4356 gfc_internal_error ("check_dimension(): Bad array reference");
4357 }
4358
4359 return SUCCESS;
4360 }
4361
4362
4363 /* Compare an array reference with an array specification. */
4364
4365 static gfc_try
4366 compare_spec_to_ref (gfc_array_ref *ar)
4367 {
4368 gfc_array_spec *as;
4369 int i;
4370
4371 as = ar->as;
4372 i = as->rank - 1;
4373 /* TODO: Full array sections are only allowed as actual parameters. */
4374 if (as->type == AS_ASSUMED_SIZE
4375 && (/*ar->type == AR_FULL
4376 ||*/ (ar->type == AR_SECTION
4377 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4378 {
4379 gfc_error ("Rightmost upper bound of assumed size array section "
4380 "not specified at %L", &ar->where);
4381 return FAILURE;
4382 }
4383
4384 if (ar->type == AR_FULL)
4385 return SUCCESS;
4386
4387 if (as->rank != ar->dimen)
4388 {
4389 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4390 &ar->where, ar->dimen, as->rank);
4391 return FAILURE;
4392 }
4393
4394 /* ar->codimen == 0 is a local array. */
4395 if (as->corank != ar->codimen && ar->codimen != 0)
4396 {
4397 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4398 &ar->where, ar->codimen, as->corank);
4399 return FAILURE;
4400 }
4401
4402 for (i = 0; i < as->rank; i++)
4403 if (check_dimension (i, ar, as) == FAILURE)
4404 return FAILURE;
4405
4406 /* Local access has no coarray spec. */
4407 if (ar->codimen != 0)
4408 for (i = as->rank; i < as->rank + as->corank; i++)
4409 {
4410 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4411 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4412 {
4413 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4414 i + 1 - as->rank, &ar->where);
4415 return FAILURE;
4416 }
4417 if (check_dimension (i, ar, as) == FAILURE)
4418 return FAILURE;
4419 }
4420
4421 return SUCCESS;
4422 }
4423
4424
4425 /* Resolve one part of an array index. */
4426
4427 static gfc_try
4428 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4429 int force_index_integer_kind)
4430 {
4431 gfc_typespec ts;
4432
4433 if (index == NULL)
4434 return SUCCESS;
4435
4436 if (gfc_resolve_expr (index) == FAILURE)
4437 return FAILURE;
4438
4439 if (check_scalar && index->rank != 0)
4440 {
4441 gfc_error ("Array index at %L must be scalar", &index->where);
4442 return FAILURE;
4443 }
4444
4445 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4446 {
4447 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4448 &index->where, gfc_basic_typename (index->ts.type));
4449 return FAILURE;
4450 }
4451
4452 if (index->ts.type == BT_REAL)
4453 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4454 &index->where) == FAILURE)
4455 return FAILURE;
4456
4457 if ((index->ts.kind != gfc_index_integer_kind
4458 && force_index_integer_kind)
4459 || index->ts.type != BT_INTEGER)
4460 {
4461 gfc_clear_ts (&ts);
4462 ts.type = BT_INTEGER;
4463 ts.kind = gfc_index_integer_kind;
4464
4465 gfc_convert_type_warn (index, &ts, 2, 0);
4466 }
4467
4468 return SUCCESS;
4469 }
4470
4471 /* Resolve one part of an array index. */
4472
4473 gfc_try
4474 gfc_resolve_index (gfc_expr *index, int check_scalar)
4475 {
4476 return gfc_resolve_index_1 (index, check_scalar, 1);
4477 }
4478
4479 /* Resolve a dim argument to an intrinsic function. */
4480
4481 gfc_try
4482 gfc_resolve_dim_arg (gfc_expr *dim)
4483 {
4484 if (dim == NULL)
4485 return SUCCESS;
4486
4487 if (gfc_resolve_expr (dim) == FAILURE)
4488 return FAILURE;
4489
4490 if (dim->rank != 0)
4491 {
4492 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4493 return FAILURE;
4494
4495 }
4496
4497 if (dim->ts.type != BT_INTEGER)
4498 {
4499 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4500 return FAILURE;
4501 }
4502
4503 if (dim->ts.kind != gfc_index_integer_kind)
4504 {
4505 gfc_typespec ts;
4506
4507 gfc_clear_ts (&ts);
4508 ts.type = BT_INTEGER;
4509 ts.kind = gfc_index_integer_kind;
4510
4511 gfc_convert_type_warn (dim, &ts, 2, 0);
4512 }
4513
4514 return SUCCESS;
4515 }
4516
4517 /* Given an expression that contains array references, update those array
4518 references to point to the right array specifications. While this is
4519 filled in during matching, this information is difficult to save and load
4520 in a module, so we take care of it here.
4521
4522 The idea here is that the original array reference comes from the
4523 base symbol. We traverse the list of reference structures, setting
4524 the stored reference to references. Component references can
4525 provide an additional array specification. */
4526
4527 static void
4528 find_array_spec (gfc_expr *e)
4529 {
4530 gfc_array_spec *as;
4531 gfc_component *c;
4532 gfc_ref *ref;
4533
4534 if (e->symtree->n.sym->ts.type == BT_CLASS)
4535 as = CLASS_DATA (e->symtree->n.sym)->as;
4536 else
4537 as = e->symtree->n.sym->as;
4538
4539 for (ref = e->ref; ref; ref = ref->next)
4540 switch (ref->type)
4541 {
4542 case REF_ARRAY:
4543 if (as == NULL)
4544 gfc_internal_error ("find_array_spec(): Missing spec");
4545
4546 ref->u.ar.as = as;
4547 as = NULL;
4548 break;
4549
4550 case REF_COMPONENT:
4551 c = ref->u.c.component;
4552 if (c->attr.dimension)
4553 {
4554 if (as != NULL)
4555 gfc_internal_error ("find_array_spec(): unused as(1)");
4556 as = c->as;
4557 }
4558
4559 break;
4560
4561 case REF_SUBSTRING:
4562 break;
4563 }
4564
4565 if (as != NULL)
4566 gfc_internal_error ("find_array_spec(): unused as(2)");
4567 }
4568
4569
4570 /* Resolve an array reference. */
4571
4572 static gfc_try
4573 resolve_array_ref (gfc_array_ref *ar)
4574 {
4575 int i, check_scalar;
4576 gfc_expr *e;
4577
4578 for (i = 0; i < ar->dimen + ar->codimen; i++)
4579 {
4580 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4581
4582 /* Do not force gfc_index_integer_kind for the start. We can
4583 do fine with any integer kind. This avoids temporary arrays
4584 created for indexing with a vector. */
4585 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4586 return FAILURE;
4587 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4588 return FAILURE;
4589 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4590 return FAILURE;
4591
4592 e = ar->start[i];
4593
4594 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4595 switch (e->rank)
4596 {
4597 case 0:
4598 ar->dimen_type[i] = DIMEN_ELEMENT;
4599 break;
4600
4601 case 1:
4602 ar->dimen_type[i] = DIMEN_VECTOR;
4603 if (e->expr_type == EXPR_VARIABLE
4604 && e->symtree->n.sym->ts.type == BT_DERIVED)
4605 ar->start[i] = gfc_get_parentheses (e);
4606 break;
4607
4608 default:
4609 gfc_error ("Array index at %L is an array of rank %d",
4610 &ar->c_where[i], e->rank);
4611 return FAILURE;
4612 }
4613
4614 /* Fill in the upper bound, which may be lower than the
4615 specified one for something like a(2:10:5), which is
4616 identical to a(2:7:5). Only relevant for strides not equal
4617 to one. Don't try a division by zero. */
4618 if (ar->dimen_type[i] == DIMEN_RANGE
4619 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4620 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4621 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4622 {
4623 mpz_t size, end;
4624
4625 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4626 {
4627 if (ar->end[i] == NULL)
4628 {
4629 ar->end[i] =
4630 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4631 &ar->where);
4632 mpz_set (ar->end[i]->value.integer, end);
4633 }
4634 else if (ar->end[i]->ts.type == BT_INTEGER
4635 && ar->end[i]->expr_type == EXPR_CONSTANT)
4636 {
4637 mpz_set (ar->end[i]->value.integer, end);
4638 }
4639 else
4640 gcc_unreachable ();
4641
4642 mpz_clear (size);
4643 mpz_clear (end);
4644 }
4645 }
4646 }
4647
4648 if (ar->type == AR_FULL)
4649 {
4650 if (ar->as->rank == 0)
4651 ar->type = AR_ELEMENT;
4652
4653 /* Make sure array is the same as array(:,:), this way
4654 we don't need to special case all the time. */
4655 ar->dimen = ar->as->rank;
4656 for (i = 0; i < ar->dimen; i++)
4657 {
4658 ar->dimen_type[i] = DIMEN_RANGE;
4659
4660 gcc_assert (ar->start[i] == NULL);
4661 gcc_assert (ar->end[i] == NULL);
4662 gcc_assert (ar->stride[i] == NULL);
4663 }
4664 }
4665
4666 /* If the reference type is unknown, figure out what kind it is. */
4667
4668 if (ar->type == AR_UNKNOWN)
4669 {
4670 ar->type = AR_ELEMENT;
4671 for (i = 0; i < ar->dimen; i++)
4672 if (ar->dimen_type[i] == DIMEN_RANGE
4673 || ar->dimen_type[i] == DIMEN_VECTOR)
4674 {
4675 ar->type = AR_SECTION;
4676 break;
4677 }
4678 }
4679
4680 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4681 return FAILURE;
4682
4683 if (ar->as->corank && ar->codimen == 0)
4684 {
4685 int n;
4686 ar->codimen = ar->as->corank;
4687 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4688 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4689 }
4690
4691 return SUCCESS;
4692 }
4693
4694
4695 static gfc_try
4696 resolve_substring (gfc_ref *ref)
4697 {
4698 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4699
4700 if (ref->u.ss.start != NULL)
4701 {
4702 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4703 return FAILURE;
4704
4705 if (ref->u.ss.start->ts.type != BT_INTEGER)
4706 {
4707 gfc_error ("Substring start index at %L must be of type INTEGER",
4708 &ref->u.ss.start->where);
4709 return FAILURE;
4710 }
4711
4712 if (ref->u.ss.start->rank != 0)
4713 {
4714 gfc_error ("Substring start index at %L must be scalar",
4715 &ref->u.ss.start->where);
4716 return FAILURE;
4717 }
4718
4719 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4720 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4721 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4722 {
4723 gfc_error ("Substring start index at %L is less than one",
4724 &ref->u.ss.start->where);
4725 return FAILURE;
4726 }
4727 }
4728
4729 if (ref->u.ss.end != NULL)
4730 {
4731 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4732 return FAILURE;
4733
4734 if (ref->u.ss.end->ts.type != BT_INTEGER)
4735 {
4736 gfc_error ("Substring end index at %L must be of type INTEGER",
4737 &ref->u.ss.end->where);
4738 return FAILURE;
4739 }
4740
4741 if (ref->u.ss.end->rank != 0)
4742 {
4743 gfc_error ("Substring end index at %L must be scalar",
4744 &ref->u.ss.end->where);
4745 return FAILURE;
4746 }
4747
4748 if (ref->u.ss.length != NULL
4749 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4750 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4751 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4752 {
4753 gfc_error ("Substring end index at %L exceeds the string length",
4754 &ref->u.ss.start->where);
4755 return FAILURE;
4756 }
4757
4758 if (compare_bound_mpz_t (ref->u.ss.end,
4759 gfc_integer_kinds[k].huge) == CMP_GT
4760 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4761 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4762 {
4763 gfc_error ("Substring end index at %L is too large",
4764 &ref->u.ss.end->where);
4765 return FAILURE;
4766 }
4767 }
4768
4769 return SUCCESS;
4770 }
4771
4772
4773 /* This function supplies missing substring charlens. */
4774
4775 void
4776 gfc_resolve_substring_charlen (gfc_expr *e)
4777 {
4778 gfc_ref *char_ref;
4779 gfc_expr *start, *end;
4780
4781 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4782 if (char_ref->type == REF_SUBSTRING)
4783 break;
4784
4785 if (!char_ref)
4786 return;
4787
4788 gcc_assert (char_ref->next == NULL);
4789
4790 if (e->ts.u.cl)
4791 {
4792 if (e->ts.u.cl->length)
4793 gfc_free_expr (e->ts.u.cl->length);
4794 else if (e->expr_type == EXPR_VARIABLE
4795 && e->symtree->n.sym->attr.dummy)
4796 return;
4797 }
4798
4799 e->ts.type = BT_CHARACTER;
4800 e->ts.kind = gfc_default_character_kind;
4801
4802 if (!e->ts.u.cl)
4803 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4804
4805 if (char_ref->u.ss.start)
4806 start = gfc_copy_expr (char_ref->u.ss.start);
4807 else
4808 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4809
4810 if (char_ref->u.ss.end)
4811 end = gfc_copy_expr (char_ref->u.ss.end);
4812 else if (e->expr_type == EXPR_VARIABLE)
4813 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4814 else
4815 end = NULL;
4816
4817 if (!start || !end)
4818 return;
4819
4820 /* Length = (end - start +1). */
4821 e->ts.u.cl->length = gfc_subtract (end, start);
4822 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4823 gfc_get_int_expr (gfc_default_integer_kind,
4824 NULL, 1));
4825
4826 e->ts.u.cl->length->ts.type = BT_INTEGER;
4827 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4828
4829 /* Make sure that the length is simplified. */
4830 gfc_simplify_expr (e->ts.u.cl->length, 1);
4831 gfc_resolve_expr (e->ts.u.cl->length);
4832 }
4833
4834
4835 /* Resolve subtype references. */
4836
4837 static gfc_try
4838 resolve_ref (gfc_expr *expr)
4839 {
4840 int current_part_dimension, n_components, seen_part_dimension;
4841 gfc_ref *ref;
4842
4843 for (ref = expr->ref; ref; ref = ref->next)
4844 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4845 {
4846 find_array_spec (expr);
4847 break;
4848 }
4849
4850 for (ref = expr->ref; ref; ref = ref->next)
4851 switch (ref->type)
4852 {
4853 case REF_ARRAY:
4854 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4855 return FAILURE;
4856 break;
4857
4858 case REF_COMPONENT:
4859 break;
4860
4861 case REF_SUBSTRING:
4862 if (resolve_substring (ref) == FAILURE)
4863 return FAILURE;
4864 break;
4865 }
4866
4867 /* Check constraints on part references. */
4868
4869 current_part_dimension = 0;
4870 seen_part_dimension = 0;
4871 n_components = 0;
4872
4873 for (ref = expr->ref; ref; ref = ref->next)
4874 {
4875 switch (ref->type)
4876 {
4877 case REF_ARRAY:
4878 switch (ref->u.ar.type)
4879 {
4880 case AR_FULL:
4881 /* Coarray scalar. */
4882 if (ref->u.ar.as->rank == 0)
4883 {
4884 current_part_dimension = 0;
4885 break;
4886 }
4887 /* Fall through. */
4888 case AR_SECTION:
4889 current_part_dimension = 1;
4890 break;
4891
4892 case AR_ELEMENT:
4893 current_part_dimension = 0;
4894 break;
4895
4896 case AR_UNKNOWN:
4897 gfc_internal_error ("resolve_ref(): Bad array reference");
4898 }
4899
4900 break;
4901
4902 case REF_COMPONENT:
4903 if (current_part_dimension || seen_part_dimension)
4904 {
4905 /* F03:C614. */
4906 if (ref->u.c.component->attr.pointer
4907 || ref->u.c.component->attr.proc_pointer)
4908 {
4909 gfc_error ("Component to the right of a part reference "
4910 "with nonzero rank must not have the POINTER "
4911 "attribute at %L", &expr->where);
4912 return FAILURE;
4913 }
4914 else if (ref->u.c.component->attr.allocatable)
4915 {
4916 gfc_error ("Component to the right of a part reference "
4917 "with nonzero rank must not have the ALLOCATABLE "
4918 "attribute at %L", &expr->where);
4919 return FAILURE;
4920 }
4921 }
4922
4923 n_components++;
4924 break;
4925
4926 case REF_SUBSTRING:
4927 break;
4928 }
4929
4930 if (((ref->type == REF_COMPONENT && n_components > 1)
4931 || ref->next == NULL)
4932 && current_part_dimension
4933 && seen_part_dimension)
4934 {
4935 gfc_error ("Two or more part references with nonzero rank must "
4936 "not be specified at %L", &expr->where);
4937 return FAILURE;
4938 }
4939
4940 if (ref->type == REF_COMPONENT)
4941 {
4942 if (current_part_dimension)
4943 seen_part_dimension = 1;
4944
4945 /* reset to make sure */
4946 current_part_dimension = 0;
4947 }
4948 }
4949
4950 return SUCCESS;
4951 }
4952
4953
4954 /* Given an expression, determine its shape. This is easier than it sounds.
4955 Leaves the shape array NULL if it is not possible to determine the shape. */
4956
4957 static void
4958 expression_shape (gfc_expr *e)
4959 {
4960 mpz_t array[GFC_MAX_DIMENSIONS];
4961 int i;
4962
4963 if (e->rank == 0 || e->shape != NULL)
4964 return;
4965
4966 for (i = 0; i < e->rank; i++)
4967 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4968 goto fail;
4969
4970 e->shape = gfc_get_shape (e->rank);
4971
4972 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4973
4974 return;
4975
4976 fail:
4977 for (i--; i >= 0; i--)
4978 mpz_clear (array[i]);
4979 }
4980
4981
4982 /* Given a variable expression node, compute the rank of the expression by
4983 examining the base symbol and any reference structures it may have. */
4984
4985 static void
4986 expression_rank (gfc_expr *e)
4987 {
4988 gfc_ref *ref;
4989 int i, rank;
4990
4991 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4992 could lead to serious confusion... */
4993 gcc_assert (e->expr_type != EXPR_COMPCALL);
4994
4995 if (e->ref == NULL)
4996 {
4997 if (e->expr_type == EXPR_ARRAY)
4998 goto done;
4999 /* Constructors can have a rank different from one via RESHAPE(). */
5000
5001 if (e->symtree == NULL)
5002 {
5003 e->rank = 0;
5004 goto done;
5005 }
5006
5007 e->rank = (e->symtree->n.sym->as == NULL)
5008 ? 0 : e->symtree->n.sym->as->rank;
5009 goto done;
5010 }
5011
5012 rank = 0;
5013
5014 for (ref = e->ref; ref; ref = ref->next)
5015 {
5016 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5017 && ref->u.c.component->attr.function && !ref->next)
5018 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5019
5020 if (ref->type != REF_ARRAY)
5021 continue;
5022
5023 if (ref->u.ar.type == AR_FULL)
5024 {
5025 rank = ref->u.ar.as->rank;
5026 break;
5027 }
5028
5029 if (ref->u.ar.type == AR_SECTION)
5030 {
5031 /* Figure out the rank of the section. */
5032 if (rank != 0)
5033 gfc_internal_error ("expression_rank(): Two array specs");
5034
5035 for (i = 0; i < ref->u.ar.dimen; i++)
5036 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5037 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5038 rank++;
5039
5040 break;
5041 }
5042 }
5043
5044 e->rank = rank;
5045
5046 done:
5047 expression_shape (e);
5048 }
5049
5050
5051 /* Resolve a variable expression. */
5052
5053 static gfc_try
5054 resolve_variable (gfc_expr *e)
5055 {
5056 gfc_symbol *sym;
5057 gfc_try t;
5058
5059 t = SUCCESS;
5060
5061 if (e->symtree == NULL)
5062 return FAILURE;
5063 sym = e->symtree->n.sym;
5064
5065 /* TS 29113, 407b. */
5066 if (e->ts.type == BT_ASSUMED && !assumed_type_expr_allowed)
5067 {
5068 gfc_error ("Invalid expression with assumed-type variable %s at %L",
5069 sym->name, &e->where);
5070 return FAILURE;
5071 }
5072
5073 /* TS 29113, 407b. */
5074 if (e->ts.type == BT_ASSUMED && e->ref
5075 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5076 && e->ref->next == NULL))
5077 {
5078 gfc_error ("Assumed-type variable %s with designator at %L",
5079 sym->name, &e->ref->u.ar.where);
5080 return FAILURE;
5081 }
5082
5083 /* If this is an associate-name, it may be parsed with an array reference
5084 in error even though the target is scalar. Fail directly in this case. */
5085 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5086 return FAILURE;
5087
5088 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5089 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5090
5091 /* On the other hand, the parser may not have known this is an array;
5092 in this case, we have to add a FULL reference. */
5093 if (sym->assoc && sym->attr.dimension && !e->ref)
5094 {
5095 e->ref = gfc_get_ref ();
5096 e->ref->type = REF_ARRAY;
5097 e->ref->u.ar.type = AR_FULL;
5098 e->ref->u.ar.dimen = 0;
5099 }
5100
5101 if (e->ref && resolve_ref (e) == FAILURE)
5102 return FAILURE;
5103
5104 if (sym->attr.flavor == FL_PROCEDURE
5105 && (!sym->attr.function
5106 || (sym->attr.function && sym->result
5107 && sym->result->attr.proc_pointer
5108 && !sym->result->attr.function)))
5109 {
5110 e->ts.type = BT_PROCEDURE;
5111 goto resolve_procedure;
5112 }
5113
5114 if (sym->ts.type != BT_UNKNOWN)
5115 gfc_variable_attr (e, &e->ts);
5116 else
5117 {
5118 /* Must be a simple variable reference. */
5119 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
5120 return FAILURE;
5121 e->ts = sym->ts;
5122 }
5123
5124 if (check_assumed_size_reference (sym, e))
5125 return FAILURE;
5126
5127 /* Deal with forward references to entries during resolve_code, to
5128 satisfy, at least partially, 12.5.2.5. */
5129 if (gfc_current_ns->entries
5130 && current_entry_id == sym->entry_id
5131 && cs_base
5132 && cs_base->current
5133 && cs_base->current->op != EXEC_ENTRY)
5134 {
5135 gfc_entry_list *entry;
5136 gfc_formal_arglist *formal;
5137 int n;
5138 bool seen;
5139
5140 /* If the symbol is a dummy... */
5141 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5142 {
5143 entry = gfc_current_ns->entries;
5144 seen = false;
5145
5146 /* ...test if the symbol is a parameter of previous entries. */
5147 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5148 for (formal = entry->sym->formal; formal; formal = formal->next)
5149 {
5150 if (formal->sym && sym->name == formal->sym->name)
5151 seen = true;
5152 }
5153
5154 /* If it has not been seen as a dummy, this is an error. */
5155 if (!seen)
5156 {
5157 if (specification_expr)
5158 gfc_error ("Variable '%s', used in a specification expression"
5159 ", is referenced at %L before the ENTRY statement "
5160 "in which it is a parameter",
5161 sym->name, &cs_base->current->loc);
5162 else
5163 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5164 "statement in which it is a parameter",
5165 sym->name, &cs_base->current->loc);
5166 t = FAILURE;
5167 }
5168 }
5169
5170 /* Now do the same check on the specification expressions. */
5171 specification_expr = 1;
5172 if (sym->ts.type == BT_CHARACTER
5173 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5174 t = FAILURE;
5175
5176 if (sym->as)
5177 for (n = 0; n < sym->as->rank; n++)
5178 {
5179 specification_expr = 1;
5180 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5181 t = FAILURE;
5182 specification_expr = 1;
5183 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5184 t = FAILURE;
5185 }
5186 specification_expr = 0;
5187
5188 if (t == SUCCESS)
5189 /* Update the symbol's entry level. */
5190 sym->entry_id = current_entry_id + 1;
5191 }
5192
5193 /* If a symbol has been host_associated mark it. This is used latter,
5194 to identify if aliasing is possible via host association. */
5195 if (sym->attr.flavor == FL_VARIABLE
5196 && gfc_current_ns->parent
5197 && (gfc_current_ns->parent == sym->ns
5198 || (gfc_current_ns->parent->parent
5199 && gfc_current_ns->parent->parent == sym->ns)))
5200 sym->attr.host_assoc = 1;
5201
5202 resolve_procedure:
5203 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5204 t = FAILURE;
5205
5206 /* F2008, C617 and C1229. */
5207 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5208 && gfc_is_coindexed (e))
5209 {
5210 gfc_ref *ref, *ref2 = NULL;
5211
5212 for (ref = e->ref; ref; ref = ref->next)
5213 {
5214 if (ref->type == REF_COMPONENT)
5215 ref2 = ref;
5216 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5217 break;
5218 }
5219
5220 for ( ; ref; ref = ref->next)
5221 if (ref->type == REF_COMPONENT)
5222 break;
5223
5224 /* Expression itself is not coindexed object. */
5225 if (ref && e->ts.type == BT_CLASS)
5226 {
5227 gfc_error ("Polymorphic subobject of coindexed object at %L",
5228 &e->where);
5229 t = FAILURE;
5230 }
5231
5232 /* Expression itself is coindexed object. */
5233 if (ref == NULL)
5234 {
5235 gfc_component *c;
5236 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5237 for ( ; c; c = c->next)
5238 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5239 {
5240 gfc_error ("Coindexed object with polymorphic allocatable "
5241 "subcomponent at %L", &e->where);
5242 t = FAILURE;
5243 break;
5244 }
5245 }
5246 }
5247
5248 return t;
5249 }
5250
5251
5252 /* Checks to see that the correct symbol has been host associated.
5253 The only situation where this arises is that in which a twice
5254 contained function is parsed after the host association is made.
5255 Therefore, on detecting this, change the symbol in the expression
5256 and convert the array reference into an actual arglist if the old
5257 symbol is a variable. */
5258 static bool
5259 check_host_association (gfc_expr *e)
5260 {
5261 gfc_symbol *sym, *old_sym;
5262 gfc_symtree *st;
5263 int n;
5264 gfc_ref *ref;
5265 gfc_actual_arglist *arg, *tail = NULL;
5266 bool retval = e->expr_type == EXPR_FUNCTION;
5267
5268 /* If the expression is the result of substitution in
5269 interface.c(gfc_extend_expr) because there is no way in
5270 which the host association can be wrong. */
5271 if (e->symtree == NULL
5272 || e->symtree->n.sym == NULL
5273 || e->user_operator)
5274 return retval;
5275
5276 old_sym = e->symtree->n.sym;
5277
5278 if (gfc_current_ns->parent
5279 && old_sym->ns != gfc_current_ns)
5280 {
5281 /* Use the 'USE' name so that renamed module symbols are
5282 correctly handled. */
5283 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5284
5285 if (sym && old_sym != sym
5286 && sym->ts.type == old_sym->ts.type
5287 && sym->attr.flavor == FL_PROCEDURE
5288 && sym->attr.contained)
5289 {
5290 /* Clear the shape, since it might not be valid. */
5291 gfc_free_shape (&e->shape, e->rank);
5292
5293 /* Give the expression the right symtree! */
5294 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5295 gcc_assert (st != NULL);
5296
5297 if (old_sym->attr.flavor == FL_PROCEDURE
5298 || e->expr_type == EXPR_FUNCTION)
5299 {
5300 /* Original was function so point to the new symbol, since
5301 the actual argument list is already attached to the
5302 expression. */
5303 e->value.function.esym = NULL;
5304 e->symtree = st;
5305 }
5306 else
5307 {
5308 /* Original was variable so convert array references into
5309 an actual arglist. This does not need any checking now
5310 since resolve_function will take care of it. */
5311 e->value.function.actual = NULL;
5312 e->expr_type = EXPR_FUNCTION;
5313 e->symtree = st;
5314
5315 /* Ambiguity will not arise if the array reference is not
5316 the last reference. */
5317 for (ref = e->ref; ref; ref = ref->next)
5318 if (ref->type == REF_ARRAY && ref->next == NULL)
5319 break;
5320
5321 gcc_assert (ref->type == REF_ARRAY);
5322
5323 /* Grab the start expressions from the array ref and
5324 copy them into actual arguments. */
5325 for (n = 0; n < ref->u.ar.dimen; n++)
5326 {
5327 arg = gfc_get_actual_arglist ();
5328 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5329 if (e->value.function.actual == NULL)
5330 tail = e->value.function.actual = arg;
5331 else
5332 {
5333 tail->next = arg;
5334 tail = arg;
5335 }
5336 }
5337
5338 /* Dump the reference list and set the rank. */
5339 gfc_free_ref_list (e->ref);
5340 e->ref = NULL;
5341 e->rank = sym->as ? sym->as->rank : 0;
5342 }
5343
5344 gfc_resolve_expr (e);
5345 sym->refs++;
5346 }
5347 }
5348 /* This might have changed! */
5349 return e->expr_type == EXPR_FUNCTION;
5350 }
5351
5352
5353 static void
5354 gfc_resolve_character_operator (gfc_expr *e)
5355 {
5356 gfc_expr *op1 = e->value.op.op1;
5357 gfc_expr *op2 = e->value.op.op2;
5358 gfc_expr *e1 = NULL;
5359 gfc_expr *e2 = NULL;
5360
5361 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5362
5363 if (op1->ts.u.cl && op1->ts.u.cl->length)
5364 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5365 else if (op1->expr_type == EXPR_CONSTANT)
5366 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5367 op1->value.character.length);
5368
5369 if (op2->ts.u.cl && op2->ts.u.cl->length)
5370 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5371 else if (op2->expr_type == EXPR_CONSTANT)
5372 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5373 op2->value.character.length);
5374
5375 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5376
5377 if (!e1 || !e2)
5378 return;
5379
5380 e->ts.u.cl->length = gfc_add (e1, e2);
5381 e->ts.u.cl->length->ts.type = BT_INTEGER;
5382 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5383 gfc_simplify_expr (e->ts.u.cl->length, 0);
5384 gfc_resolve_expr (e->ts.u.cl->length);
5385
5386 return;
5387 }
5388
5389
5390 /* Ensure that an character expression has a charlen and, if possible, a
5391 length expression. */
5392
5393 static void
5394 fixup_charlen (gfc_expr *e)
5395 {
5396 /* The cases fall through so that changes in expression type and the need
5397 for multiple fixes are picked up. In all circumstances, a charlen should
5398 be available for the middle end to hang a backend_decl on. */
5399 switch (e->expr_type)
5400 {
5401 case EXPR_OP:
5402 gfc_resolve_character_operator (e);
5403
5404 case EXPR_ARRAY:
5405 if (e->expr_type == EXPR_ARRAY)
5406 gfc_resolve_character_array_constructor (e);
5407
5408 case EXPR_SUBSTRING:
5409 if (!e->ts.u.cl && e->ref)
5410 gfc_resolve_substring_charlen (e);
5411
5412 default:
5413 if (!e->ts.u.cl)
5414 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5415
5416 break;
5417 }
5418 }
5419
5420
5421 /* Update an actual argument to include the passed-object for type-bound
5422 procedures at the right position. */
5423
5424 static gfc_actual_arglist*
5425 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5426 const char *name)
5427 {
5428 gcc_assert (argpos > 0);
5429
5430 if (argpos == 1)
5431 {
5432 gfc_actual_arglist* result;
5433
5434 result = gfc_get_actual_arglist ();
5435 result->expr = po;
5436 result->next = lst;
5437 if (name)
5438 result->name = name;
5439
5440 return result;
5441 }
5442
5443 if (lst)
5444 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5445 else
5446 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5447 return lst;
5448 }
5449
5450
5451 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5452
5453 static gfc_expr*
5454 extract_compcall_passed_object (gfc_expr* e)
5455 {
5456 gfc_expr* po;
5457
5458 gcc_assert (e->expr_type == EXPR_COMPCALL);
5459
5460 if (e->value.compcall.base_object)
5461 po = gfc_copy_expr (e->value.compcall.base_object);
5462 else
5463 {
5464 po = gfc_get_expr ();
5465 po->expr_type = EXPR_VARIABLE;
5466 po->symtree = e->symtree;
5467 po->ref = gfc_copy_ref (e->ref);
5468 po->where = e->where;
5469 }
5470
5471 if (gfc_resolve_expr (po) == FAILURE)
5472 return NULL;
5473
5474 return po;
5475 }
5476
5477
5478 /* Update the arglist of an EXPR_COMPCALL expression to include the
5479 passed-object. */
5480
5481 static gfc_try
5482 update_compcall_arglist (gfc_expr* e)
5483 {
5484 gfc_expr* po;
5485 gfc_typebound_proc* tbp;
5486
5487 tbp = e->value.compcall.tbp;
5488
5489 if (tbp->error)
5490 return FAILURE;
5491
5492 po = extract_compcall_passed_object (e);
5493 if (!po)
5494 return FAILURE;
5495
5496 if (tbp->nopass || e->value.compcall.ignore_pass)
5497 {
5498 gfc_free_expr (po);
5499 return SUCCESS;
5500 }
5501
5502 gcc_assert (tbp->pass_arg_num > 0);
5503 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5504 tbp->pass_arg_num,
5505 tbp->pass_arg);
5506
5507 return SUCCESS;
5508 }
5509
5510
5511 /* Extract the passed object from a PPC call (a copy of it). */
5512
5513 static gfc_expr*
5514 extract_ppc_passed_object (gfc_expr *e)
5515 {
5516 gfc_expr *po;
5517 gfc_ref **ref;
5518
5519 po = gfc_get_expr ();
5520 po->expr_type = EXPR_VARIABLE;
5521 po->symtree = e->symtree;
5522 po->ref = gfc_copy_ref (e->ref);
5523 po->where = e->where;
5524
5525 /* Remove PPC reference. */
5526 ref = &po->ref;
5527 while ((*ref)->next)
5528 ref = &(*ref)->next;
5529 gfc_free_ref_list (*ref);
5530 *ref = NULL;
5531
5532 if (gfc_resolve_expr (po) == FAILURE)
5533 return NULL;
5534
5535 return po;
5536 }
5537
5538
5539 /* Update the actual arglist of a procedure pointer component to include the
5540 passed-object. */
5541
5542 static gfc_try
5543 update_ppc_arglist (gfc_expr* e)
5544 {
5545 gfc_expr* po;
5546 gfc_component *ppc;
5547 gfc_typebound_proc* tb;
5548
5549 if (!gfc_is_proc_ptr_comp (e, &ppc))
5550 return FAILURE;
5551
5552 tb = ppc->tb;
5553
5554 if (tb->error)
5555 return FAILURE;
5556 else if (tb->nopass)
5557 return SUCCESS;
5558
5559 po = extract_ppc_passed_object (e);
5560 if (!po)
5561 return FAILURE;
5562
5563 /* F08:R739. */
5564 if (po->rank > 0)
5565 {
5566 gfc_error ("Passed-object at %L must be scalar", &e->where);
5567 return FAILURE;
5568 }
5569
5570 /* F08:C611. */
5571 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5572 {
5573 gfc_error ("Base object for procedure-pointer component call at %L is of"
5574 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5575 return FAILURE;
5576 }
5577
5578 gcc_assert (tb->pass_arg_num > 0);
5579 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5580 tb->pass_arg_num,
5581 tb->pass_arg);
5582
5583 return SUCCESS;
5584 }
5585
5586
5587 /* Check that the object a TBP is called on is valid, i.e. it must not be
5588 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5589
5590 static gfc_try
5591 check_typebound_baseobject (gfc_expr* e)
5592 {
5593 gfc_expr* base;
5594 gfc_try return_value = FAILURE;
5595
5596 base = extract_compcall_passed_object (e);
5597 if (!base)
5598 return FAILURE;
5599
5600 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5601
5602 /* F08:C611. */
5603 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5604 {
5605 gfc_error ("Base object for type-bound procedure call at %L is of"
5606 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5607 goto cleanup;
5608 }
5609
5610 /* F08:C1230. If the procedure called is NOPASS,
5611 the base object must be scalar. */
5612 if (e->value.compcall.tbp->nopass && base->rank > 0)
5613 {
5614 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5615 " be scalar", &e->where);
5616 goto cleanup;
5617 }
5618
5619 return_value = SUCCESS;
5620
5621 cleanup:
5622 gfc_free_expr (base);
5623 return return_value;
5624 }
5625
5626
5627 /* Resolve a call to a type-bound procedure, either function or subroutine,
5628 statically from the data in an EXPR_COMPCALL expression. The adapted
5629 arglist and the target-procedure symtree are returned. */
5630
5631 static gfc_try
5632 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5633 gfc_actual_arglist** actual)
5634 {
5635 gcc_assert (e->expr_type == EXPR_COMPCALL);
5636 gcc_assert (!e->value.compcall.tbp->is_generic);
5637
5638 /* Update the actual arglist for PASS. */
5639 if (update_compcall_arglist (e) == FAILURE)
5640 return FAILURE;
5641
5642 *actual = e->value.compcall.actual;
5643 *target = e->value.compcall.tbp->u.specific;
5644
5645 gfc_free_ref_list (e->ref);
5646 e->ref = NULL;
5647 e->value.compcall.actual = NULL;
5648
5649 /* If we find a deferred typebound procedure, check for derived types
5650 that an over-riding typebound procedure has not been missed. */
5651 if (e->value.compcall.tbp->deferred
5652 && e->value.compcall.name
5653 && !e->value.compcall.tbp->non_overridable
5654 && e->value.compcall.base_object
5655 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5656 {
5657 gfc_symtree *st;
5658 gfc_symbol *derived;
5659
5660 /* Use the derived type of the base_object. */
5661 derived = e->value.compcall.base_object->ts.u.derived;
5662 st = NULL;
5663
5664 /* If necessary, go throught the inheritance chain. */
5665 while (!st && derived)
5666 {
5667 /* Look for the typebound procedure 'name'. */
5668 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5669 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5670 e->value.compcall.name);
5671 if (!st)
5672 derived = gfc_get_derived_super_type (derived);
5673 }
5674
5675 /* Now find the specific name in the derived type namespace. */
5676 if (st && st->n.tb && st->n.tb->u.specific)
5677 gfc_find_sym_tree (st->n.tb->u.specific->name,
5678 derived->ns, 1, &st);
5679 if (st)
5680 *target = st;
5681 }
5682 return SUCCESS;
5683 }
5684
5685
5686 /* Get the ultimate declared type from an expression. In addition,
5687 return the last class/derived type reference and the copy of the
5688 reference list. If check_types is set true, derived types are
5689 identified as well as class references. */
5690 static gfc_symbol*
5691 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5692 gfc_expr *e, bool check_types)
5693 {
5694 gfc_symbol *declared;
5695 gfc_ref *ref;
5696
5697 declared = NULL;
5698 if (class_ref)
5699 *class_ref = NULL;
5700 if (new_ref)
5701 *new_ref = gfc_copy_ref (e->ref);
5702
5703 for (ref = e->ref; ref; ref = ref->next)
5704 {
5705 if (ref->type != REF_COMPONENT)
5706 continue;
5707
5708 if ((ref->u.c.component->ts.type == BT_CLASS
5709 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5710 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5711 {
5712 declared = ref->u.c.component->ts.u.derived;
5713 if (class_ref)
5714 *class_ref = ref;
5715 }
5716 }
5717
5718 if (declared == NULL)
5719 declared = e->symtree->n.sym->ts.u.derived;
5720
5721 return declared;
5722 }
5723
5724
5725 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5726 which of the specific bindings (if any) matches the arglist and transform
5727 the expression into a call of that binding. */
5728
5729 static gfc_try
5730 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5731 {
5732 gfc_typebound_proc* genproc;
5733 const char* genname;
5734 gfc_symtree *st;
5735 gfc_symbol *derived;
5736
5737 gcc_assert (e->expr_type == EXPR_COMPCALL);
5738 genname = e->value.compcall.name;
5739 genproc = e->value.compcall.tbp;
5740
5741 if (!genproc->is_generic)
5742 return SUCCESS;
5743
5744 /* Try the bindings on this type and in the inheritance hierarchy. */
5745 for (; genproc; genproc = genproc->overridden)
5746 {
5747 gfc_tbp_generic* g;
5748
5749 gcc_assert (genproc->is_generic);
5750 for (g = genproc->u.generic; g; g = g->next)
5751 {
5752 gfc_symbol* target;
5753 gfc_actual_arglist* args;
5754 bool matches;
5755
5756 gcc_assert (g->specific);
5757
5758 if (g->specific->error)
5759 continue;
5760
5761 target = g->specific->u.specific->n.sym;
5762
5763 /* Get the right arglist by handling PASS/NOPASS. */
5764 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5765 if (!g->specific->nopass)
5766 {
5767 gfc_expr* po;
5768 po = extract_compcall_passed_object (e);
5769 if (!po)
5770 return FAILURE;
5771
5772 gcc_assert (g->specific->pass_arg_num > 0);
5773 gcc_assert (!g->specific->error);
5774 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5775 g->specific->pass_arg);
5776 }
5777 resolve_actual_arglist (args, target->attr.proc,
5778 is_external_proc (target) && !target->formal);
5779
5780 /* Check if this arglist matches the formal. */
5781 matches = gfc_arglist_matches_symbol (&args, target);
5782
5783 /* Clean up and break out of the loop if we've found it. */
5784 gfc_free_actual_arglist (args);
5785 if (matches)
5786 {
5787 e->value.compcall.tbp = g->specific;
5788 genname = g->specific_st->name;
5789 /* Pass along the name for CLASS methods, where the vtab
5790 procedure pointer component has to be referenced. */
5791 if (name)
5792 *name = genname;
5793 goto success;
5794 }
5795 }
5796 }
5797
5798 /* Nothing matching found! */
5799 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5800 " '%s' at %L", genname, &e->where);
5801 return FAILURE;
5802
5803 success:
5804 /* Make sure that we have the right specific instance for the name. */
5805 derived = get_declared_from_expr (NULL, NULL, e, true);
5806
5807 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5808 if (st)
5809 e->value.compcall.tbp = st->n.tb;
5810
5811 return SUCCESS;
5812 }
5813
5814
5815 /* Resolve a call to a type-bound subroutine. */
5816
5817 static gfc_try
5818 resolve_typebound_call (gfc_code* c, const char **name)
5819 {
5820 gfc_actual_arglist* newactual;
5821 gfc_symtree* target;
5822
5823 /* Check that's really a SUBROUTINE. */
5824 if (!c->expr1->value.compcall.tbp->subroutine)
5825 {
5826 gfc_error ("'%s' at %L should be a SUBROUTINE",
5827 c->expr1->value.compcall.name, &c->loc);
5828 return FAILURE;
5829 }
5830
5831 if (check_typebound_baseobject (c->expr1) == FAILURE)
5832 return FAILURE;
5833
5834 /* Pass along the name for CLASS methods, where the vtab
5835 procedure pointer component has to be referenced. */
5836 if (name)
5837 *name = c->expr1->value.compcall.name;
5838
5839 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5840 return FAILURE;
5841
5842 /* Transform into an ordinary EXEC_CALL for now. */
5843
5844 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5845 return FAILURE;
5846
5847 c->ext.actual = newactual;
5848 c->symtree = target;
5849 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5850
5851 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5852
5853 gfc_free_expr (c->expr1);
5854 c->expr1 = gfc_get_expr ();
5855 c->expr1->expr_type = EXPR_FUNCTION;
5856 c->expr1->symtree = target;
5857 c->expr1->where = c->loc;
5858
5859 return resolve_call (c);
5860 }
5861
5862
5863 /* Resolve a component-call expression. */
5864 static gfc_try
5865 resolve_compcall (gfc_expr* e, const char **name)
5866 {
5867 gfc_actual_arglist* newactual;
5868 gfc_symtree* target;
5869
5870 /* Check that's really a FUNCTION. */
5871 if (!e->value.compcall.tbp->function)
5872 {
5873 gfc_error ("'%s' at %L should be a FUNCTION",
5874 e->value.compcall.name, &e->where);
5875 return FAILURE;
5876 }
5877
5878 /* These must not be assign-calls! */
5879 gcc_assert (!e->value.compcall.assign);
5880
5881 if (check_typebound_baseobject (e) == FAILURE)
5882 return FAILURE;
5883
5884 /* Pass along the name for CLASS methods, where the vtab
5885 procedure pointer component has to be referenced. */
5886 if (name)
5887 *name = e->value.compcall.name;
5888
5889 if (resolve_typebound_generic_call (e, name) == FAILURE)
5890 return FAILURE;
5891 gcc_assert (!e->value.compcall.tbp->is_generic);
5892
5893 /* Take the rank from the function's symbol. */
5894 if (e->value.compcall.tbp->u.specific->n.sym->as)
5895 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5896
5897 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5898 arglist to the TBP's binding target. */
5899
5900 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5901 return FAILURE;
5902
5903 e->value.function.actual = newactual;
5904 e->value.function.name = NULL;
5905 e->value.function.esym = target->n.sym;
5906 e->value.function.isym = NULL;
5907 e->symtree = target;
5908 e->ts = target->n.sym->ts;
5909 e->expr_type = EXPR_FUNCTION;
5910
5911 /* Resolution is not necessary if this is a class subroutine; this
5912 function only has to identify the specific proc. Resolution of
5913 the call will be done next in resolve_typebound_call. */
5914 return gfc_resolve_expr (e);
5915 }
5916
5917
5918
5919 /* Resolve a typebound function, or 'method'. First separate all
5920 the non-CLASS references by calling resolve_compcall directly. */
5921
5922 static gfc_try
5923 resolve_typebound_function (gfc_expr* e)
5924 {
5925 gfc_symbol *declared;
5926 gfc_component *c;
5927 gfc_ref *new_ref;
5928 gfc_ref *class_ref;
5929 gfc_symtree *st;
5930 const char *name;
5931 gfc_typespec ts;
5932 gfc_expr *expr;
5933 bool overridable;
5934
5935 st = e->symtree;
5936
5937 /* Deal with typebound operators for CLASS objects. */
5938 expr = e->value.compcall.base_object;
5939 overridable = !e->value.compcall.tbp->non_overridable;
5940 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5941 {
5942 /* If the base_object is not a variable, the corresponding actual
5943 argument expression must be stored in e->base_expression so
5944 that the corresponding tree temporary can be used as the base
5945 object in gfc_conv_procedure_call. */
5946 if (expr->expr_type != EXPR_VARIABLE)
5947 {
5948 gfc_actual_arglist *args;
5949
5950 for (args= e->value.function.actual; args; args = args->next)
5951 {
5952 if (expr == args->expr)
5953 expr = args->expr;
5954 }
5955 }
5956
5957 /* Since the typebound operators are generic, we have to ensure
5958 that any delays in resolution are corrected and that the vtab
5959 is present. */
5960 ts = expr->ts;
5961 declared = ts.u.derived;
5962 c = gfc_find_component (declared, "_vptr", true, true);
5963 if (c->ts.u.derived == NULL)
5964 c->ts.u.derived = gfc_find_derived_vtab (declared);
5965
5966 if (resolve_compcall (e, &name) == FAILURE)
5967 return FAILURE;
5968
5969 /* Use the generic name if it is there. */
5970 name = name ? name : e->value.function.esym->name;
5971 e->symtree = expr->symtree;
5972 e->ref = gfc_copy_ref (expr->ref);
5973 get_declared_from_expr (&class_ref, NULL, e, false);
5974
5975 /* Trim away the extraneous references that emerge from nested
5976 use of interface.c (extend_expr). */
5977 if (class_ref && class_ref->next)
5978 {
5979 gfc_free_ref_list (class_ref->next);
5980 class_ref->next = NULL;
5981 }
5982 else if (e->ref && !class_ref)
5983 {
5984 gfc_free_ref_list (e->ref);
5985 e->ref = NULL;
5986 }
5987
5988 gfc_add_vptr_component (e);
5989 gfc_add_component_ref (e, name);
5990 e->value.function.esym = NULL;
5991 if (expr->expr_type != EXPR_VARIABLE)
5992 e->base_expr = expr;
5993 return SUCCESS;
5994 }
5995
5996 if (st == NULL)
5997 return resolve_compcall (e, NULL);
5998
5999 if (resolve_ref (e) == FAILURE)
6000 return FAILURE;
6001
6002 /* Get the CLASS declared type. */
6003 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6004
6005 /* Weed out cases of the ultimate component being a derived type. */
6006 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6007 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6008 {
6009 gfc_free_ref_list (new_ref);
6010 return resolve_compcall (e, NULL);
6011 }
6012
6013 c = gfc_find_component (declared, "_data", true, true);
6014 declared = c->ts.u.derived;
6015
6016 /* Treat the call as if it is a typebound procedure, in order to roll
6017 out the correct name for the specific function. */
6018 if (resolve_compcall (e, &name) == FAILURE)
6019 return FAILURE;
6020 ts = e->ts;
6021
6022 if (overridable)
6023 {
6024 /* Convert the expression to a procedure pointer component call. */
6025 e->value.function.esym = NULL;
6026 e->symtree = st;
6027
6028 if (new_ref)
6029 e->ref = new_ref;
6030
6031 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6032 gfc_add_vptr_component (e);
6033 gfc_add_component_ref (e, name);
6034
6035 /* Recover the typespec for the expression. This is really only
6036 necessary for generic procedures, where the additional call
6037 to gfc_add_component_ref seems to throw the collection of the
6038 correct typespec. */
6039 e->ts = ts;
6040 }
6041
6042 return SUCCESS;
6043 }
6044
6045 /* Resolve a typebound subroutine, or 'method'. First separate all
6046 the non-CLASS references by calling resolve_typebound_call
6047 directly. */
6048
6049 static gfc_try
6050 resolve_typebound_subroutine (gfc_code *code)
6051 {
6052 gfc_symbol *declared;
6053 gfc_component *c;
6054 gfc_ref *new_ref;
6055 gfc_ref *class_ref;
6056 gfc_symtree *st;
6057 const char *name;
6058 gfc_typespec ts;
6059 gfc_expr *expr;
6060 bool overridable;
6061
6062 st = code->expr1->symtree;
6063
6064 /* Deal with typebound operators for CLASS objects. */
6065 expr = code->expr1->value.compcall.base_object;
6066 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6067 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6068 {
6069 /* If the base_object is not a variable, the corresponding actual
6070 argument expression must be stored in e->base_expression so
6071 that the corresponding tree temporary can be used as the base
6072 object in gfc_conv_procedure_call. */
6073 if (expr->expr_type != EXPR_VARIABLE)
6074 {
6075 gfc_actual_arglist *args;
6076
6077 args= code->expr1->value.function.actual;
6078 for (; args; args = args->next)
6079 if (expr == args->expr)
6080 expr = args->expr;
6081 }
6082
6083 /* Since the typebound operators are generic, we have to ensure
6084 that any delays in resolution are corrected and that the vtab
6085 is present. */
6086 declared = expr->ts.u.derived;
6087 c = gfc_find_component (declared, "_vptr", true, true);
6088 if (c->ts.u.derived == NULL)
6089 c->ts.u.derived = gfc_find_derived_vtab (declared);
6090
6091 if (resolve_typebound_call (code, &name) == FAILURE)
6092 return FAILURE;
6093
6094 /* Use the generic name if it is there. */
6095 name = name ? name : code->expr1->value.function.esym->name;
6096 code->expr1->symtree = expr->symtree;
6097 code->expr1->ref = gfc_copy_ref (expr->ref);
6098
6099 /* Trim away the extraneous references that emerge from nested
6100 use of interface.c (extend_expr). */
6101 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6102 if (class_ref && class_ref->next)
6103 {
6104 gfc_free_ref_list (class_ref->next);
6105 class_ref->next = NULL;
6106 }
6107 else if (code->expr1->ref && !class_ref)
6108 {
6109 gfc_free_ref_list (code->expr1->ref);
6110 code->expr1->ref = NULL;
6111 }
6112
6113 /* Now use the procedure in the vtable. */
6114 gfc_add_vptr_component (code->expr1);
6115 gfc_add_component_ref (code->expr1, name);
6116 code->expr1->value.function.esym = NULL;
6117 if (expr->expr_type != EXPR_VARIABLE)
6118 code->expr1->base_expr = expr;
6119 return SUCCESS;
6120 }
6121
6122 if (st == NULL)
6123 return resolve_typebound_call (code, NULL);
6124
6125 if (resolve_ref (code->expr1) == FAILURE)
6126 return FAILURE;
6127
6128 /* Get the CLASS declared type. */
6129 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6130
6131 /* Weed out cases of the ultimate component being a derived type. */
6132 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6133 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6134 {
6135 gfc_free_ref_list (new_ref);
6136 return resolve_typebound_call (code, NULL);
6137 }
6138
6139 if (resolve_typebound_call (code, &name) == FAILURE)
6140 return FAILURE;
6141 ts = code->expr1->ts;
6142
6143 if (overridable)
6144 {
6145 /* Convert the expression to a procedure pointer component call. */
6146 code->expr1->value.function.esym = NULL;
6147 code->expr1->symtree = st;
6148
6149 if (new_ref)
6150 code->expr1->ref = new_ref;
6151
6152 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6153 gfc_add_vptr_component (code->expr1);
6154 gfc_add_component_ref (code->expr1, name);
6155
6156 /* Recover the typespec for the expression. This is really only
6157 necessary for generic procedures, where the additional call
6158 to gfc_add_component_ref seems to throw the collection of the
6159 correct typespec. */
6160 code->expr1->ts = ts;
6161 }
6162
6163 return SUCCESS;
6164 }
6165
6166
6167 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6168
6169 static gfc_try
6170 resolve_ppc_call (gfc_code* c)
6171 {
6172 gfc_component *comp;
6173 bool b;
6174
6175 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
6176 gcc_assert (b);
6177
6178 c->resolved_sym = c->expr1->symtree->n.sym;
6179 c->expr1->expr_type = EXPR_VARIABLE;
6180
6181 if (!comp->attr.subroutine)
6182 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6183
6184 if (resolve_ref (c->expr1) == FAILURE)
6185 return FAILURE;
6186
6187 if (update_ppc_arglist (c->expr1) == FAILURE)
6188 return FAILURE;
6189
6190 c->ext.actual = c->expr1->value.compcall.actual;
6191
6192 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6193 comp->formal == NULL) == FAILURE)
6194 return FAILURE;
6195
6196 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6197
6198 return SUCCESS;
6199 }
6200
6201
6202 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6203
6204 static gfc_try
6205 resolve_expr_ppc (gfc_expr* e)
6206 {
6207 gfc_component *comp;
6208 bool b;
6209
6210 b = gfc_is_proc_ptr_comp (e, &comp);
6211 gcc_assert (b);
6212
6213 /* Convert to EXPR_FUNCTION. */
6214 e->expr_type = EXPR_FUNCTION;
6215 e->value.function.isym = NULL;
6216 e->value.function.actual = e->value.compcall.actual;
6217 e->ts = comp->ts;
6218 if (comp->as != NULL)
6219 e->rank = comp->as->rank;
6220
6221 if (!comp->attr.function)
6222 gfc_add_function (&comp->attr, comp->name, &e->where);
6223
6224 if (resolve_ref (e) == FAILURE)
6225 return FAILURE;
6226
6227 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6228 comp->formal == NULL) == FAILURE)
6229 return FAILURE;
6230
6231 if (update_ppc_arglist (e) == FAILURE)
6232 return FAILURE;
6233
6234 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6235
6236 return SUCCESS;
6237 }
6238
6239
6240 static bool
6241 gfc_is_expandable_expr (gfc_expr *e)
6242 {
6243 gfc_constructor *con;
6244
6245 if (e->expr_type == EXPR_ARRAY)
6246 {
6247 /* Traverse the constructor looking for variables that are flavor
6248 parameter. Parameters must be expanded since they are fully used at
6249 compile time. */
6250 con = gfc_constructor_first (e->value.constructor);
6251 for (; con; con = gfc_constructor_next (con))
6252 {
6253 if (con->expr->expr_type == EXPR_VARIABLE
6254 && con->expr->symtree
6255 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6256 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6257 return true;
6258 if (con->expr->expr_type == EXPR_ARRAY
6259 && gfc_is_expandable_expr (con->expr))
6260 return true;
6261 }
6262 }
6263
6264 return false;
6265 }
6266
6267 /* Resolve an expression. That is, make sure that types of operands agree
6268 with their operators, intrinsic operators are converted to function calls
6269 for overloaded types and unresolved function references are resolved. */
6270
6271 gfc_try
6272 gfc_resolve_expr (gfc_expr *e)
6273 {
6274 gfc_try t;
6275 bool inquiry_save;
6276
6277 if (e == NULL)
6278 return SUCCESS;
6279
6280 /* inquiry_argument only applies to variables. */
6281 inquiry_save = inquiry_argument;
6282 if (e->expr_type != EXPR_VARIABLE)
6283 inquiry_argument = false;
6284
6285 switch (e->expr_type)
6286 {
6287 case EXPR_OP:
6288 t = resolve_operator (e);
6289 break;
6290
6291 case EXPR_FUNCTION:
6292 case EXPR_VARIABLE:
6293
6294 if (check_host_association (e))
6295 t = resolve_function (e);
6296 else
6297 {
6298 t = resolve_variable (e);
6299 if (t == SUCCESS)
6300 expression_rank (e);
6301 }
6302
6303 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6304 && e->ref->type != REF_SUBSTRING)
6305 gfc_resolve_substring_charlen (e);
6306
6307 break;
6308
6309 case EXPR_COMPCALL:
6310 t = resolve_typebound_function (e);
6311 break;
6312
6313 case EXPR_SUBSTRING:
6314 t = resolve_ref (e);
6315 break;
6316
6317 case EXPR_CONSTANT:
6318 case EXPR_NULL:
6319 t = SUCCESS;
6320 break;
6321
6322 case EXPR_PPC:
6323 t = resolve_expr_ppc (e);
6324 break;
6325
6326 case EXPR_ARRAY:
6327 t = FAILURE;
6328 if (resolve_ref (e) == FAILURE)
6329 break;
6330
6331 t = gfc_resolve_array_constructor (e);
6332 /* Also try to expand a constructor. */
6333 if (t == SUCCESS)
6334 {
6335 expression_rank (e);
6336 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6337 gfc_expand_constructor (e, false);
6338 }
6339
6340 /* This provides the opportunity for the length of constructors with
6341 character valued function elements to propagate the string length
6342 to the expression. */
6343 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6344 {
6345 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6346 here rather then add a duplicate test for it above. */
6347 gfc_expand_constructor (e, false);
6348 t = gfc_resolve_character_array_constructor (e);
6349 }
6350
6351 break;
6352
6353 case EXPR_STRUCTURE:
6354 t = resolve_ref (e);
6355 if (t == FAILURE)
6356 break;
6357
6358 t = resolve_structure_cons (e, 0);
6359 if (t == FAILURE)
6360 break;
6361
6362 t = gfc_simplify_expr (e, 0);
6363 break;
6364
6365 default:
6366 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6367 }
6368
6369 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6370 fixup_charlen (e);
6371
6372 inquiry_argument = inquiry_save;
6373
6374 return t;
6375 }
6376
6377
6378 /* Resolve an expression from an iterator. They must be scalar and have
6379 INTEGER or (optionally) REAL type. */
6380
6381 static gfc_try
6382 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6383 const char *name_msgid)
6384 {
6385 if (gfc_resolve_expr (expr) == FAILURE)
6386 return FAILURE;
6387
6388 if (expr->rank != 0)
6389 {
6390 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6391 return FAILURE;
6392 }
6393
6394 if (expr->ts.type != BT_INTEGER)
6395 {
6396 if (expr->ts.type == BT_REAL)
6397 {
6398 if (real_ok)
6399 return gfc_notify_std (GFC_STD_F95_DEL,
6400 "Deleted feature: %s at %L must be integer",
6401 _(name_msgid), &expr->where);
6402 else
6403 {
6404 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6405 &expr->where);
6406 return FAILURE;
6407 }
6408 }
6409 else
6410 {
6411 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6412 return FAILURE;
6413 }
6414 }
6415 return SUCCESS;
6416 }
6417
6418
6419 /* Resolve the expressions in an iterator structure. If REAL_OK is
6420 false allow only INTEGER type iterators, otherwise allow REAL types. */
6421
6422 gfc_try
6423 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6424 {
6425 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6426 == FAILURE)
6427 return FAILURE;
6428
6429 if (gfc_check_vardef_context (iter->var, false, false, _("iterator variable"))
6430 == FAILURE)
6431 return FAILURE;
6432
6433 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6434 "Start expression in DO loop") == FAILURE)
6435 return FAILURE;
6436
6437 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6438 "End expression in DO loop") == FAILURE)
6439 return FAILURE;
6440
6441 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6442 "Step expression in DO loop") == FAILURE)
6443 return FAILURE;
6444
6445 if (iter->step->expr_type == EXPR_CONSTANT)
6446 {
6447 if ((iter->step->ts.type == BT_INTEGER
6448 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6449 || (iter->step->ts.type == BT_REAL
6450 && mpfr_sgn (iter->step->value.real) == 0))
6451 {
6452 gfc_error ("Step expression in DO loop at %L cannot be zero",
6453 &iter->step->where);
6454 return FAILURE;
6455 }
6456 }
6457
6458 /* Convert start, end, and step to the same type as var. */
6459 if (iter->start->ts.kind != iter->var->ts.kind
6460 || iter->start->ts.type != iter->var->ts.type)
6461 gfc_convert_type (iter->start, &iter->var->ts, 2);
6462
6463 if (iter->end->ts.kind != iter->var->ts.kind
6464 || iter->end->ts.type != iter->var->ts.type)
6465 gfc_convert_type (iter->end, &iter->var->ts, 2);
6466
6467 if (iter->step->ts.kind != iter->var->ts.kind
6468 || iter->step->ts.type != iter->var->ts.type)
6469 gfc_convert_type (iter->step, &iter->var->ts, 2);
6470
6471 if (iter->start->expr_type == EXPR_CONSTANT
6472 && iter->end->expr_type == EXPR_CONSTANT
6473 && iter->step->expr_type == EXPR_CONSTANT)
6474 {
6475 int sgn, cmp;
6476 if (iter->start->ts.type == BT_INTEGER)
6477 {
6478 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6479 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6480 }
6481 else
6482 {
6483 sgn = mpfr_sgn (iter->step->value.real);
6484 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6485 }
6486 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6487 gfc_warning ("DO loop at %L will be executed zero times",
6488 &iter->step->where);
6489 }
6490
6491 return SUCCESS;
6492 }
6493
6494
6495 /* Traversal function for find_forall_index. f == 2 signals that
6496 that variable itself is not to be checked - only the references. */
6497
6498 static bool
6499 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6500 {
6501 if (expr->expr_type != EXPR_VARIABLE)
6502 return false;
6503
6504 /* A scalar assignment */
6505 if (!expr->ref || *f == 1)
6506 {
6507 if (expr->symtree->n.sym == sym)
6508 return true;
6509 else
6510 return false;
6511 }
6512
6513 if (*f == 2)
6514 *f = 1;
6515 return false;
6516 }
6517
6518
6519 /* Check whether the FORALL index appears in the expression or not.
6520 Returns SUCCESS if SYM is found in EXPR. */
6521
6522 gfc_try
6523 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6524 {
6525 if (gfc_traverse_expr (expr, sym, forall_index, f))
6526 return SUCCESS;
6527 else
6528 return FAILURE;
6529 }
6530
6531
6532 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6533 to be a scalar INTEGER variable. The subscripts and stride are scalar
6534 INTEGERs, and if stride is a constant it must be nonzero.
6535 Furthermore "A subscript or stride in a forall-triplet-spec shall
6536 not contain a reference to any index-name in the
6537 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6538
6539 static void
6540 resolve_forall_iterators (gfc_forall_iterator *it)
6541 {
6542 gfc_forall_iterator *iter, *iter2;
6543
6544 for (iter = it; iter; iter = iter->next)
6545 {
6546 if (gfc_resolve_expr (iter->var) == SUCCESS
6547 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6548 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6549 &iter->var->where);
6550
6551 if (gfc_resolve_expr (iter->start) == SUCCESS
6552 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6553 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6554 &iter->start->where);
6555 if (iter->var->ts.kind != iter->start->ts.kind)
6556 gfc_convert_type (iter->start, &iter->var->ts, 1);
6557
6558 if (gfc_resolve_expr (iter->end) == SUCCESS
6559 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6560 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6561 &iter->end->where);
6562 if (iter->var->ts.kind != iter->end->ts.kind)
6563 gfc_convert_type (iter->end, &iter->var->ts, 1);
6564
6565 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6566 {
6567 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6568 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6569 &iter->stride->where, "INTEGER");
6570
6571 if (iter->stride->expr_type == EXPR_CONSTANT
6572 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6573 gfc_error ("FORALL stride expression at %L cannot be zero",
6574 &iter->stride->where);
6575 }
6576 if (iter->var->ts.kind != iter->stride->ts.kind)
6577 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6578 }
6579
6580 for (iter = it; iter; iter = iter->next)
6581 for (iter2 = iter; iter2; iter2 = iter2->next)
6582 {
6583 if (find_forall_index (iter2->start,
6584 iter->var->symtree->n.sym, 0) == SUCCESS
6585 || find_forall_index (iter2->end,
6586 iter->var->symtree->n.sym, 0) == SUCCESS
6587 || find_forall_index (iter2->stride,
6588 iter->var->symtree->n.sym, 0) == SUCCESS)
6589 gfc_error ("FORALL index '%s' may not appear in triplet "
6590 "specification at %L", iter->var->symtree->name,
6591 &iter2->start->where);
6592 }
6593 }
6594
6595
6596 /* Given a pointer to a symbol that is a derived type, see if it's
6597 inaccessible, i.e. if it's defined in another module and the components are
6598 PRIVATE. The search is recursive if necessary. Returns zero if no
6599 inaccessible components are found, nonzero otherwise. */
6600
6601 static int
6602 derived_inaccessible (gfc_symbol *sym)
6603 {
6604 gfc_component *c;
6605
6606 if (sym->attr.use_assoc && sym->attr.private_comp)
6607 return 1;
6608
6609 for (c = sym->components; c; c = c->next)
6610 {
6611 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6612 return 1;
6613 }
6614
6615 return 0;
6616 }
6617
6618
6619 /* Resolve the argument of a deallocate expression. The expression must be
6620 a pointer or a full array. */
6621
6622 static gfc_try
6623 resolve_deallocate_expr (gfc_expr *e)
6624 {
6625 symbol_attribute attr;
6626 int allocatable, pointer;
6627 gfc_ref *ref;
6628 gfc_symbol *sym;
6629 gfc_component *c;
6630
6631 if (gfc_resolve_expr (e) == FAILURE)
6632 return FAILURE;
6633
6634 if (e->expr_type != EXPR_VARIABLE)
6635 goto bad;
6636
6637 sym = e->symtree->n.sym;
6638
6639 if (sym->ts.type == BT_CLASS)
6640 {
6641 allocatable = CLASS_DATA (sym)->attr.allocatable;
6642 pointer = CLASS_DATA (sym)->attr.class_pointer;
6643 }
6644 else
6645 {
6646 allocatable = sym->attr.allocatable;
6647 pointer = sym->attr.pointer;
6648 }
6649 for (ref = e->ref; ref; ref = ref->next)
6650 {
6651 switch (ref->type)
6652 {
6653 case REF_ARRAY:
6654 if (ref->u.ar.type != AR_FULL
6655 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6656 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6657 allocatable = 0;
6658 break;
6659
6660 case REF_COMPONENT:
6661 c = ref->u.c.component;
6662 if (c->ts.type == BT_CLASS)
6663 {
6664 allocatable = CLASS_DATA (c)->attr.allocatable;
6665 pointer = CLASS_DATA (c)->attr.class_pointer;
6666 }
6667 else
6668 {
6669 allocatable = c->attr.allocatable;
6670 pointer = c->attr.pointer;
6671 }
6672 break;
6673
6674 case REF_SUBSTRING:
6675 allocatable = 0;
6676 break;
6677 }
6678 }
6679
6680 attr = gfc_expr_attr (e);
6681
6682 if (allocatable == 0 && attr.pointer == 0)
6683 {
6684 bad:
6685 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6686 &e->where);
6687 return FAILURE;
6688 }
6689
6690 /* F2008, C644. */
6691 if (gfc_is_coindexed (e))
6692 {
6693 gfc_error ("Coindexed allocatable object at %L", &e->where);
6694 return FAILURE;
6695 }
6696
6697 if (pointer
6698 && gfc_check_vardef_context (e, true, true, _("DEALLOCATE object"))
6699 == FAILURE)
6700 return FAILURE;
6701 if (gfc_check_vardef_context (e, false, true, _("DEALLOCATE object"))
6702 == FAILURE)
6703 return FAILURE;
6704
6705 return SUCCESS;
6706 }
6707
6708
6709 /* Returns true if the expression e contains a reference to the symbol sym. */
6710 static bool
6711 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6712 {
6713 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6714 return true;
6715
6716 return false;
6717 }
6718
6719 bool
6720 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6721 {
6722 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6723 }
6724
6725
6726 /* Given the expression node e for an allocatable/pointer of derived type to be
6727 allocated, get the expression node to be initialized afterwards (needed for
6728 derived types with default initializers, and derived types with allocatable
6729 components that need nullification.) */
6730
6731 gfc_expr *
6732 gfc_expr_to_initialize (gfc_expr *e)
6733 {
6734 gfc_expr *result;
6735 gfc_ref *ref;
6736 int i;
6737
6738 result = gfc_copy_expr (e);
6739
6740 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6741 for (ref = result->ref; ref; ref = ref->next)
6742 if (ref->type == REF_ARRAY && ref->next == NULL)
6743 {
6744 ref->u.ar.type = AR_FULL;
6745
6746 for (i = 0; i < ref->u.ar.dimen; i++)
6747 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6748
6749 break;
6750 }
6751
6752 gfc_free_shape (&result->shape, result->rank);
6753
6754 /* Recalculate rank, shape, etc. */
6755 gfc_resolve_expr (result);
6756 return result;
6757 }
6758
6759
6760 /* If the last ref of an expression is an array ref, return a copy of the
6761 expression with that one removed. Otherwise, a copy of the original
6762 expression. This is used for allocate-expressions and pointer assignment
6763 LHS, where there may be an array specification that needs to be stripped
6764 off when using gfc_check_vardef_context. */
6765
6766 static gfc_expr*
6767 remove_last_array_ref (gfc_expr* e)
6768 {
6769 gfc_expr* e2;
6770 gfc_ref** r;
6771
6772 e2 = gfc_copy_expr (e);
6773 for (r = &e2->ref; *r; r = &(*r)->next)
6774 if ((*r)->type == REF_ARRAY && !(*r)->next)
6775 {
6776 gfc_free_ref_list (*r);
6777 *r = NULL;
6778 break;
6779 }
6780
6781 return e2;
6782 }
6783
6784
6785 /* Used in resolve_allocate_expr to check that a allocation-object and
6786 a source-expr are conformable. This does not catch all possible
6787 cases; in particular a runtime checking is needed. */
6788
6789 static gfc_try
6790 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6791 {
6792 gfc_ref *tail;
6793 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6794
6795 /* First compare rank. */
6796 if (tail && e1->rank != tail->u.ar.as->rank)
6797 {
6798 gfc_error ("Source-expr at %L must be scalar or have the "
6799 "same rank as the allocate-object at %L",
6800 &e1->where, &e2->where);
6801 return FAILURE;
6802 }
6803
6804 if (e1->shape)
6805 {
6806 int i;
6807 mpz_t s;
6808
6809 mpz_init (s);
6810
6811 for (i = 0; i < e1->rank; i++)
6812 {
6813 if (tail->u.ar.end[i])
6814 {
6815 mpz_set (s, tail->u.ar.end[i]->value.integer);
6816 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6817 mpz_add_ui (s, s, 1);
6818 }
6819 else
6820 {
6821 mpz_set (s, tail->u.ar.start[i]->value.integer);
6822 }
6823
6824 if (mpz_cmp (e1->shape[i], s) != 0)
6825 {
6826 gfc_error ("Source-expr at %L and allocate-object at %L must "
6827 "have the same shape", &e1->where, &e2->where);
6828 mpz_clear (s);
6829 return FAILURE;
6830 }
6831 }
6832
6833 mpz_clear (s);
6834 }
6835
6836 return SUCCESS;
6837 }
6838
6839
6840 /* Resolve the expression in an ALLOCATE statement, doing the additional
6841 checks to see whether the expression is OK or not. The expression must
6842 have a trailing array reference that gives the size of the array. */
6843
6844 static gfc_try
6845 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6846 {
6847 int i, pointer, allocatable, dimension, is_abstract;
6848 int codimension;
6849 bool coindexed;
6850 symbol_attribute attr;
6851 gfc_ref *ref, *ref2;
6852 gfc_expr *e2;
6853 gfc_array_ref *ar;
6854 gfc_symbol *sym = NULL;
6855 gfc_alloc *a;
6856 gfc_component *c;
6857 gfc_try t;
6858
6859 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6860 checking of coarrays. */
6861 for (ref = e->ref; ref; ref = ref->next)
6862 if (ref->next == NULL)
6863 break;
6864
6865 if (ref && ref->type == REF_ARRAY)
6866 ref->u.ar.in_allocate = true;
6867
6868 if (gfc_resolve_expr (e) == FAILURE)
6869 goto failure;
6870
6871 /* Make sure the expression is allocatable or a pointer. If it is
6872 pointer, the next-to-last reference must be a pointer. */
6873
6874 ref2 = NULL;
6875 if (e->symtree)
6876 sym = e->symtree->n.sym;
6877
6878 /* Check whether ultimate component is abstract and CLASS. */
6879 is_abstract = 0;
6880
6881 if (e->expr_type != EXPR_VARIABLE)
6882 {
6883 allocatable = 0;
6884 attr = gfc_expr_attr (e);
6885 pointer = attr.pointer;
6886 dimension = attr.dimension;
6887 codimension = attr.codimension;
6888 }
6889 else
6890 {
6891 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6892 {
6893 allocatable = CLASS_DATA (sym)->attr.allocatable;
6894 pointer = CLASS_DATA (sym)->attr.class_pointer;
6895 dimension = CLASS_DATA (sym)->attr.dimension;
6896 codimension = CLASS_DATA (sym)->attr.codimension;
6897 is_abstract = CLASS_DATA (sym)->attr.abstract;
6898 }
6899 else
6900 {
6901 allocatable = sym->attr.allocatable;
6902 pointer = sym->attr.pointer;
6903 dimension = sym->attr.dimension;
6904 codimension = sym->attr.codimension;
6905 }
6906
6907 coindexed = false;
6908
6909 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6910 {
6911 switch (ref->type)
6912 {
6913 case REF_ARRAY:
6914 if (ref->u.ar.codimen > 0)
6915 {
6916 int n;
6917 for (n = ref->u.ar.dimen;
6918 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6919 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6920 {
6921 coindexed = true;
6922 break;
6923 }
6924 }
6925
6926 if (ref->next != NULL)
6927 pointer = 0;
6928 break;
6929
6930 case REF_COMPONENT:
6931 /* F2008, C644. */
6932 if (coindexed)
6933 {
6934 gfc_error ("Coindexed allocatable object at %L",
6935 &e->where);
6936 goto failure;
6937 }
6938
6939 c = ref->u.c.component;
6940 if (c->ts.type == BT_CLASS)
6941 {
6942 allocatable = CLASS_DATA (c)->attr.allocatable;
6943 pointer = CLASS_DATA (c)->attr.class_pointer;
6944 dimension = CLASS_DATA (c)->attr.dimension;
6945 codimension = CLASS_DATA (c)->attr.codimension;
6946 is_abstract = CLASS_DATA (c)->attr.abstract;
6947 }
6948 else
6949 {
6950 allocatable = c->attr.allocatable;
6951 pointer = c->attr.pointer;
6952 dimension = c->attr.dimension;
6953 codimension = c->attr.codimension;
6954 is_abstract = c->attr.abstract;
6955 }
6956 break;
6957
6958 case REF_SUBSTRING:
6959 allocatable = 0;
6960 pointer = 0;
6961 break;
6962 }
6963 }
6964 }
6965
6966 if (allocatable == 0 && pointer == 0)
6967 {
6968 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6969 &e->where);
6970 goto failure;
6971 }
6972
6973 /* Some checks for the SOURCE tag. */
6974 if (code->expr3)
6975 {
6976 /* Check F03:C631. */
6977 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6978 {
6979 gfc_error ("Type of entity at %L is type incompatible with "
6980 "source-expr at %L", &e->where, &code->expr3->where);
6981 goto failure;
6982 }
6983
6984 /* Check F03:C632 and restriction following Note 6.18. */
6985 if (code->expr3->rank > 0
6986 && conformable_arrays (code->expr3, e) == FAILURE)
6987 goto failure;
6988
6989 /* Check F03:C633. */
6990 if (code->expr3->ts.kind != e->ts.kind)
6991 {
6992 gfc_error ("The allocate-object at %L and the source-expr at %L "
6993 "shall have the same kind type parameter",
6994 &e->where, &code->expr3->where);
6995 goto failure;
6996 }
6997
6998 /* Check F2008, C642. */
6999 if (code->expr3->ts.type == BT_DERIVED
7000 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7001 || (code->expr3->ts.u.derived->from_intmod
7002 == INTMOD_ISO_FORTRAN_ENV
7003 && code->expr3->ts.u.derived->intmod_sym_id
7004 == ISOFORTRAN_LOCK_TYPE)))
7005 {
7006 gfc_error ("The source-expr at %L shall neither be of type "
7007 "LOCK_TYPE nor have a LOCK_TYPE component if "
7008 "allocate-object at %L is a coarray",
7009 &code->expr3->where, &e->where);
7010 goto failure;
7011 }
7012 }
7013
7014 /* Check F08:C629. */
7015 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7016 && !code->expr3)
7017 {
7018 gcc_assert (e->ts.type == BT_CLASS);
7019 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7020 "type-spec or source-expr", sym->name, &e->where);
7021 goto failure;
7022 }
7023
7024 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
7025 {
7026 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7027 code->ext.alloc.ts.u.cl->length);
7028 if (cmp == 1 || cmp == -1 || cmp == -3)
7029 {
7030 gfc_error ("Allocating %s at %L with type-spec requires the same "
7031 "character-length parameter as in the declaration",
7032 sym->name, &e->where);
7033 goto failure;
7034 }
7035 }
7036
7037 /* In the variable definition context checks, gfc_expr_attr is used
7038 on the expression. This is fooled by the array specification
7039 present in e, thus we have to eliminate that one temporarily. */
7040 e2 = remove_last_array_ref (e);
7041 t = SUCCESS;
7042 if (t == SUCCESS && pointer)
7043 t = gfc_check_vardef_context (e2, true, true, _("ALLOCATE object"));
7044 if (t == SUCCESS)
7045 t = gfc_check_vardef_context (e2, false, true, _("ALLOCATE object"));
7046 gfc_free_expr (e2);
7047 if (t == FAILURE)
7048 goto failure;
7049
7050 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7051 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7052 {
7053 /* For class arrays, the initialization with SOURCE is done
7054 using _copy and trans_call. It is convenient to exploit that
7055 when the allocated type is different from the declared type but
7056 no SOURCE exists by setting expr3. */
7057 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7058 }
7059 else if (!code->expr3)
7060 {
7061 /* Set up default initializer if needed. */
7062 gfc_typespec ts;
7063 gfc_expr *init_e;
7064
7065 if (code->ext.alloc.ts.type == BT_DERIVED)
7066 ts = code->ext.alloc.ts;
7067 else
7068 ts = e->ts;
7069
7070 if (ts.type == BT_CLASS)
7071 ts = ts.u.derived->components->ts;
7072
7073 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
7074 {
7075 gfc_code *init_st = gfc_get_code ();
7076 init_st->loc = code->loc;
7077 init_st->op = EXEC_INIT_ASSIGN;
7078 init_st->expr1 = gfc_expr_to_initialize (e);
7079 init_st->expr2 = init_e;
7080 init_st->next = code->next;
7081 code->next = init_st;
7082 }
7083 }
7084 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7085 {
7086 /* Default initialization via MOLD (non-polymorphic). */
7087 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7088 gfc_resolve_expr (rhs);
7089 gfc_free_expr (code->expr3);
7090 code->expr3 = rhs;
7091 }
7092
7093 if (e->ts.type == BT_CLASS)
7094 {
7095 /* Make sure the vtab symbol is present when
7096 the module variables are generated. */
7097 gfc_typespec ts = e->ts;
7098 if (code->expr3)
7099 ts = code->expr3->ts;
7100 else if (code->ext.alloc.ts.type == BT_DERIVED)
7101 ts = code->ext.alloc.ts;
7102 gfc_find_derived_vtab (ts.u.derived);
7103 if (dimension)
7104 e = gfc_expr_to_initialize (e);
7105 }
7106
7107 if (dimension == 0 && codimension == 0)
7108 goto success;
7109
7110 /* Make sure the last reference node is an array specifiction. */
7111
7112 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7113 || (dimension && ref2->u.ar.dimen == 0))
7114 {
7115 gfc_error ("Array specification required in ALLOCATE statement "
7116 "at %L", &e->where);
7117 goto failure;
7118 }
7119
7120 /* Make sure that the array section reference makes sense in the
7121 context of an ALLOCATE specification. */
7122
7123 ar = &ref2->u.ar;
7124
7125 if (codimension)
7126 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7127 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7128 {
7129 gfc_error ("Coarray specification required in ALLOCATE statement "
7130 "at %L", &e->where);
7131 goto failure;
7132 }
7133
7134 for (i = 0; i < ar->dimen; i++)
7135 {
7136 if (ref2->u.ar.type == AR_ELEMENT)
7137 goto check_symbols;
7138
7139 switch (ar->dimen_type[i])
7140 {
7141 case DIMEN_ELEMENT:
7142 break;
7143
7144 case DIMEN_RANGE:
7145 if (ar->start[i] != NULL
7146 && ar->end[i] != NULL
7147 && ar->stride[i] == NULL)
7148 break;
7149
7150 /* Fall Through... */
7151
7152 case DIMEN_UNKNOWN:
7153 case DIMEN_VECTOR:
7154 case DIMEN_STAR:
7155 case DIMEN_THIS_IMAGE:
7156 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7157 &e->where);
7158 goto failure;
7159 }
7160
7161 check_symbols:
7162 for (a = code->ext.alloc.list; a; a = a->next)
7163 {
7164 sym = a->expr->symtree->n.sym;
7165
7166 /* TODO - check derived type components. */
7167 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7168 continue;
7169
7170 if ((ar->start[i] != NULL
7171 && gfc_find_sym_in_expr (sym, ar->start[i]))
7172 || (ar->end[i] != NULL
7173 && gfc_find_sym_in_expr (sym, ar->end[i])))
7174 {
7175 gfc_error ("'%s' must not appear in the array specification at "
7176 "%L in the same ALLOCATE statement where it is "
7177 "itself allocated", sym->name, &ar->where);
7178 goto failure;
7179 }
7180 }
7181 }
7182
7183 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7184 {
7185 if (ar->dimen_type[i] == DIMEN_ELEMENT
7186 || ar->dimen_type[i] == DIMEN_RANGE)
7187 {
7188 if (i == (ar->dimen + ar->codimen - 1))
7189 {
7190 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7191 "statement at %L", &e->where);
7192 goto failure;
7193 }
7194 break;
7195 }
7196
7197 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7198 && ar->stride[i] == NULL)
7199 break;
7200
7201 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7202 &e->where);
7203 goto failure;
7204 }
7205
7206 success:
7207 return SUCCESS;
7208
7209 failure:
7210 return FAILURE;
7211 }
7212
7213 static void
7214 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7215 {
7216 gfc_expr *stat, *errmsg, *pe, *qe;
7217 gfc_alloc *a, *p, *q;
7218
7219 stat = code->expr1;
7220 errmsg = code->expr2;
7221
7222 /* Check the stat variable. */
7223 if (stat)
7224 {
7225 gfc_check_vardef_context (stat, false, false, _("STAT variable"));
7226
7227 if ((stat->ts.type != BT_INTEGER
7228 && !(stat->ref && (stat->ref->type == REF_ARRAY
7229 || stat->ref->type == REF_COMPONENT)))
7230 || stat->rank > 0)
7231 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7232 "variable", &stat->where);
7233
7234 for (p = code->ext.alloc.list; p; p = p->next)
7235 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7236 {
7237 gfc_ref *ref1, *ref2;
7238 bool found = true;
7239
7240 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7241 ref1 = ref1->next, ref2 = ref2->next)
7242 {
7243 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7244 continue;
7245 if (ref1->u.c.component->name != ref2->u.c.component->name)
7246 {
7247 found = false;
7248 break;
7249 }
7250 }
7251
7252 if (found)
7253 {
7254 gfc_error ("Stat-variable at %L shall not be %sd within "
7255 "the same %s statement", &stat->where, fcn, fcn);
7256 break;
7257 }
7258 }
7259 }
7260
7261 /* Check the errmsg variable. */
7262 if (errmsg)
7263 {
7264 if (!stat)
7265 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7266 &errmsg->where);
7267
7268 gfc_check_vardef_context (errmsg, false, false, _("ERRMSG variable"));
7269
7270 if ((errmsg->ts.type != BT_CHARACTER
7271 && !(errmsg->ref
7272 && (errmsg->ref->type == REF_ARRAY
7273 || errmsg->ref->type == REF_COMPONENT)))
7274 || errmsg->rank > 0 )
7275 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7276 "variable", &errmsg->where);
7277
7278 for (p = code->ext.alloc.list; p; p = p->next)
7279 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7280 {
7281 gfc_ref *ref1, *ref2;
7282 bool found = true;
7283
7284 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7285 ref1 = ref1->next, ref2 = ref2->next)
7286 {
7287 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7288 continue;
7289 if (ref1->u.c.component->name != ref2->u.c.component->name)
7290 {
7291 found = false;
7292 break;
7293 }
7294 }
7295
7296 if (found)
7297 {
7298 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7299 "the same %s statement", &errmsg->where, fcn, fcn);
7300 break;
7301 }
7302 }
7303 }
7304
7305 /* Check that an allocate-object appears only once in the statement.
7306 FIXME: Checking derived types is disabled. */
7307 for (p = code->ext.alloc.list; p; p = p->next)
7308 {
7309 pe = p->expr;
7310 for (q = p->next; q; q = q->next)
7311 {
7312 qe = q->expr;
7313 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7314 {
7315 /* This is a potential collision. */
7316 gfc_ref *pr = pe->ref;
7317 gfc_ref *qr = qe->ref;
7318
7319 /* Follow the references until
7320 a) They start to differ, in which case there is no error;
7321 you can deallocate a%b and a%c in a single statement
7322 b) Both of them stop, which is an error
7323 c) One of them stops, which is also an error. */
7324 while (1)
7325 {
7326 if (pr == NULL && qr == NULL)
7327 {
7328 gfc_error ("Allocate-object at %L also appears at %L",
7329 &pe->where, &qe->where);
7330 break;
7331 }
7332 else if (pr != NULL && qr == NULL)
7333 {
7334 gfc_error ("Allocate-object at %L is subobject of"
7335 " object at %L", &pe->where, &qe->where);
7336 break;
7337 }
7338 else if (pr == NULL && qr != NULL)
7339 {
7340 gfc_error ("Allocate-object at %L is subobject of"
7341 " object at %L", &qe->where, &pe->where);
7342 break;
7343 }
7344 /* Here, pr != NULL && qr != NULL */
7345 gcc_assert(pr->type == qr->type);
7346 if (pr->type == REF_ARRAY)
7347 {
7348 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7349 which are legal. */
7350 gcc_assert (qr->type == REF_ARRAY);
7351
7352 if (pr->next && qr->next)
7353 {
7354 gfc_array_ref *par = &(pr->u.ar);
7355 gfc_array_ref *qar = &(qr->u.ar);
7356 if (gfc_dep_compare_expr (par->start[0],
7357 qar->start[0]) != 0)
7358 break;
7359 }
7360 }
7361 else
7362 {
7363 if (pr->u.c.component->name != qr->u.c.component->name)
7364 break;
7365 }
7366
7367 pr = pr->next;
7368 qr = qr->next;
7369 }
7370 }
7371 }
7372 }
7373
7374 if (strcmp (fcn, "ALLOCATE") == 0)
7375 {
7376 for (a = code->ext.alloc.list; a; a = a->next)
7377 resolve_allocate_expr (a->expr, code);
7378 }
7379 else
7380 {
7381 for (a = code->ext.alloc.list; a; a = a->next)
7382 resolve_deallocate_expr (a->expr);
7383 }
7384 }
7385
7386
7387 /************ SELECT CASE resolution subroutines ************/
7388
7389 /* Callback function for our mergesort variant. Determines interval
7390 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7391 op1 > op2. Assumes we're not dealing with the default case.
7392 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7393 There are nine situations to check. */
7394
7395 static int
7396 compare_cases (const gfc_case *op1, const gfc_case *op2)
7397 {
7398 int retval;
7399
7400 if (op1->low == NULL) /* op1 = (:L) */
7401 {
7402 /* op2 = (:N), so overlap. */
7403 retval = 0;
7404 /* op2 = (M:) or (M:N), L < M */
7405 if (op2->low != NULL
7406 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7407 retval = -1;
7408 }
7409 else if (op1->high == NULL) /* op1 = (K:) */
7410 {
7411 /* op2 = (M:), so overlap. */
7412 retval = 0;
7413 /* op2 = (:N) or (M:N), K > N */
7414 if (op2->high != NULL
7415 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7416 retval = 1;
7417 }
7418 else /* op1 = (K:L) */
7419 {
7420 if (op2->low == NULL) /* op2 = (:N), K > N */
7421 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7422 ? 1 : 0;
7423 else if (op2->high == NULL) /* op2 = (M:), L < M */
7424 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7425 ? -1 : 0;
7426 else /* op2 = (M:N) */
7427 {
7428 retval = 0;
7429 /* L < M */
7430 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7431 retval = -1;
7432 /* K > N */
7433 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7434 retval = 1;
7435 }
7436 }
7437
7438 return retval;
7439 }
7440
7441
7442 /* Merge-sort a double linked case list, detecting overlap in the
7443 process. LIST is the head of the double linked case list before it
7444 is sorted. Returns the head of the sorted list if we don't see any
7445 overlap, or NULL otherwise. */
7446
7447 static gfc_case *
7448 check_case_overlap (gfc_case *list)
7449 {
7450 gfc_case *p, *q, *e, *tail;
7451 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7452
7453 /* If the passed list was empty, return immediately. */
7454 if (!list)
7455 return NULL;
7456
7457 overlap_seen = 0;
7458 insize = 1;
7459
7460 /* Loop unconditionally. The only exit from this loop is a return
7461 statement, when we've finished sorting the case list. */
7462 for (;;)
7463 {
7464 p = list;
7465 list = NULL;
7466 tail = NULL;
7467
7468 /* Count the number of merges we do in this pass. */
7469 nmerges = 0;
7470
7471 /* Loop while there exists a merge to be done. */
7472 while (p)
7473 {
7474 int i;
7475
7476 /* Count this merge. */
7477 nmerges++;
7478
7479 /* Cut the list in two pieces by stepping INSIZE places
7480 forward in the list, starting from P. */
7481 psize = 0;
7482 q = p;
7483 for (i = 0; i < insize; i++)
7484 {
7485 psize++;
7486 q = q->right;
7487 if (!q)
7488 break;
7489 }
7490 qsize = insize;
7491
7492 /* Now we have two lists. Merge them! */
7493 while (psize > 0 || (qsize > 0 && q != NULL))
7494 {
7495 /* See from which the next case to merge comes from. */
7496 if (psize == 0)
7497 {
7498 /* P is empty so the next case must come from Q. */
7499 e = q;
7500 q = q->right;
7501 qsize--;
7502 }
7503 else if (qsize == 0 || q == NULL)
7504 {
7505 /* Q is empty. */
7506 e = p;
7507 p = p->right;
7508 psize--;
7509 }
7510 else
7511 {
7512 cmp = compare_cases (p, q);
7513 if (cmp < 0)
7514 {
7515 /* The whole case range for P is less than the
7516 one for Q. */
7517 e = p;
7518 p = p->right;
7519 psize--;
7520 }
7521 else if (cmp > 0)
7522 {
7523 /* The whole case range for Q is greater than
7524 the case range for P. */
7525 e = q;
7526 q = q->right;
7527 qsize--;
7528 }
7529 else
7530 {
7531 /* The cases overlap, or they are the same
7532 element in the list. Either way, we must
7533 issue an error and get the next case from P. */
7534 /* FIXME: Sort P and Q by line number. */
7535 gfc_error ("CASE label at %L overlaps with CASE "
7536 "label at %L", &p->where, &q->where);
7537 overlap_seen = 1;
7538 e = p;
7539 p = p->right;
7540 psize--;
7541 }
7542 }
7543
7544 /* Add the next element to the merged list. */
7545 if (tail)
7546 tail->right = e;
7547 else
7548 list = e;
7549 e->left = tail;
7550 tail = e;
7551 }
7552
7553 /* P has now stepped INSIZE places along, and so has Q. So
7554 they're the same. */
7555 p = q;
7556 }
7557 tail->right = NULL;
7558
7559 /* If we have done only one merge or none at all, we've
7560 finished sorting the cases. */
7561 if (nmerges <= 1)
7562 {
7563 if (!overlap_seen)
7564 return list;
7565 else
7566 return NULL;
7567 }
7568
7569 /* Otherwise repeat, merging lists twice the size. */
7570 insize *= 2;
7571 }
7572 }
7573
7574
7575 /* Check to see if an expression is suitable for use in a CASE statement.
7576 Makes sure that all case expressions are scalar constants of the same
7577 type. Return FAILURE if anything is wrong. */
7578
7579 static gfc_try
7580 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7581 {
7582 if (e == NULL) return SUCCESS;
7583
7584 if (e->ts.type != case_expr->ts.type)
7585 {
7586 gfc_error ("Expression in CASE statement at %L must be of type %s",
7587 &e->where, gfc_basic_typename (case_expr->ts.type));
7588 return FAILURE;
7589 }
7590
7591 /* C805 (R808) For a given case-construct, each case-value shall be of
7592 the same type as case-expr. For character type, length differences
7593 are allowed, but the kind type parameters shall be the same. */
7594
7595 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7596 {
7597 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7598 &e->where, case_expr->ts.kind);
7599 return FAILURE;
7600 }
7601
7602 /* Convert the case value kind to that of case expression kind,
7603 if needed */
7604
7605 if (e->ts.kind != case_expr->ts.kind)
7606 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7607
7608 if (e->rank != 0)
7609 {
7610 gfc_error ("Expression in CASE statement at %L must be scalar",
7611 &e->where);
7612 return FAILURE;
7613 }
7614
7615 return SUCCESS;
7616 }
7617
7618
7619 /* Given a completely parsed select statement, we:
7620
7621 - Validate all expressions and code within the SELECT.
7622 - Make sure that the selection expression is not of the wrong type.
7623 - Make sure that no case ranges overlap.
7624 - Eliminate unreachable cases and unreachable code resulting from
7625 removing case labels.
7626
7627 The standard does allow unreachable cases, e.g. CASE (5:3). But
7628 they are a hassle for code generation, and to prevent that, we just
7629 cut them out here. This is not necessary for overlapping cases
7630 because they are illegal and we never even try to generate code.
7631
7632 We have the additional caveat that a SELECT construct could have
7633 been a computed GOTO in the source code. Fortunately we can fairly
7634 easily work around that here: The case_expr for a "real" SELECT CASE
7635 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7636 we have to do is make sure that the case_expr is a scalar integer
7637 expression. */
7638
7639 static void
7640 resolve_select (gfc_code *code)
7641 {
7642 gfc_code *body;
7643 gfc_expr *case_expr;
7644 gfc_case *cp, *default_case, *tail, *head;
7645 int seen_unreachable;
7646 int seen_logical;
7647 int ncases;
7648 bt type;
7649 gfc_try t;
7650
7651 if (code->expr1 == NULL)
7652 {
7653 /* This was actually a computed GOTO statement. */
7654 case_expr = code->expr2;
7655 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7656 gfc_error ("Selection expression in computed GOTO statement "
7657 "at %L must be a scalar integer expression",
7658 &case_expr->where);
7659
7660 /* Further checking is not necessary because this SELECT was built
7661 by the compiler, so it should always be OK. Just move the
7662 case_expr from expr2 to expr so that we can handle computed
7663 GOTOs as normal SELECTs from here on. */
7664 code->expr1 = code->expr2;
7665 code->expr2 = NULL;
7666 return;
7667 }
7668
7669 case_expr = code->expr1;
7670
7671 type = case_expr->ts.type;
7672 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7673 {
7674 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7675 &case_expr->where, gfc_typename (&case_expr->ts));
7676
7677 /* Punt. Going on here just produce more garbage error messages. */
7678 return;
7679 }
7680
7681 /* Raise a warning if an INTEGER case value exceeds the range of
7682 the case-expr. Later, all expressions will be promoted to the
7683 largest kind of all case-labels. */
7684
7685 if (type == BT_INTEGER)
7686 for (body = code->block; body; body = body->block)
7687 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7688 {
7689 if (cp->low
7690 && gfc_check_integer_range (cp->low->value.integer,
7691 case_expr->ts.kind) != ARITH_OK)
7692 gfc_warning ("Expression in CASE statement at %L is "
7693 "not in the range of %s", &cp->low->where,
7694 gfc_typename (&case_expr->ts));
7695
7696 if (cp->high
7697 && cp->low != cp->high
7698 && gfc_check_integer_range (cp->high->value.integer,
7699 case_expr->ts.kind) != ARITH_OK)
7700 gfc_warning ("Expression in CASE statement at %L is "
7701 "not in the range of %s", &cp->high->where,
7702 gfc_typename (&case_expr->ts));
7703 }
7704
7705 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7706 of the SELECT CASE expression and its CASE values. Walk the lists
7707 of case values, and if we find a mismatch, promote case_expr to
7708 the appropriate kind. */
7709
7710 if (type == BT_LOGICAL || type == BT_INTEGER)
7711 {
7712 for (body = code->block; body; body = body->block)
7713 {
7714 /* Walk the case label list. */
7715 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7716 {
7717 /* Intercept the DEFAULT case. It does not have a kind. */
7718 if (cp->low == NULL && cp->high == NULL)
7719 continue;
7720
7721 /* Unreachable case ranges are discarded, so ignore. */
7722 if (cp->low != NULL && cp->high != NULL
7723 && cp->low != cp->high
7724 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7725 continue;
7726
7727 if (cp->low != NULL
7728 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7729 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7730
7731 if (cp->high != NULL
7732 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7733 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7734 }
7735 }
7736 }
7737
7738 /* Assume there is no DEFAULT case. */
7739 default_case = NULL;
7740 head = tail = NULL;
7741 ncases = 0;
7742 seen_logical = 0;
7743
7744 for (body = code->block; body; body = body->block)
7745 {
7746 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7747 t = SUCCESS;
7748 seen_unreachable = 0;
7749
7750 /* Walk the case label list, making sure that all case labels
7751 are legal. */
7752 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7753 {
7754 /* Count the number of cases in the whole construct. */
7755 ncases++;
7756
7757 /* Intercept the DEFAULT case. */
7758 if (cp->low == NULL && cp->high == NULL)
7759 {
7760 if (default_case != NULL)
7761 {
7762 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7763 "by a second DEFAULT CASE at %L",
7764 &default_case->where, &cp->where);
7765 t = FAILURE;
7766 break;
7767 }
7768 else
7769 {
7770 default_case = cp;
7771 continue;
7772 }
7773 }
7774
7775 /* Deal with single value cases and case ranges. Errors are
7776 issued from the validation function. */
7777 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7778 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7779 {
7780 t = FAILURE;
7781 break;
7782 }
7783
7784 if (type == BT_LOGICAL
7785 && ((cp->low == NULL || cp->high == NULL)
7786 || cp->low != cp->high))
7787 {
7788 gfc_error ("Logical range in CASE statement at %L is not "
7789 "allowed", &cp->low->where);
7790 t = FAILURE;
7791 break;
7792 }
7793
7794 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7795 {
7796 int value;
7797 value = cp->low->value.logical == 0 ? 2 : 1;
7798 if (value & seen_logical)
7799 {
7800 gfc_error ("Constant logical value in CASE statement "
7801 "is repeated at %L",
7802 &cp->low->where);
7803 t = FAILURE;
7804 break;
7805 }
7806 seen_logical |= value;
7807 }
7808
7809 if (cp->low != NULL && cp->high != NULL
7810 && cp->low != cp->high
7811 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7812 {
7813 if (gfc_option.warn_surprising)
7814 gfc_warning ("Range specification at %L can never "
7815 "be matched", &cp->where);
7816
7817 cp->unreachable = 1;
7818 seen_unreachable = 1;
7819 }
7820 else
7821 {
7822 /* If the case range can be matched, it can also overlap with
7823 other cases. To make sure it does not, we put it in a
7824 double linked list here. We sort that with a merge sort
7825 later on to detect any overlapping cases. */
7826 if (!head)
7827 {
7828 head = tail = cp;
7829 head->right = head->left = NULL;
7830 }
7831 else
7832 {
7833 tail->right = cp;
7834 tail->right->left = tail;
7835 tail = tail->right;
7836 tail->right = NULL;
7837 }
7838 }
7839 }
7840
7841 /* It there was a failure in the previous case label, give up
7842 for this case label list. Continue with the next block. */
7843 if (t == FAILURE)
7844 continue;
7845
7846 /* See if any case labels that are unreachable have been seen.
7847 If so, we eliminate them. This is a bit of a kludge because
7848 the case lists for a single case statement (label) is a
7849 single forward linked lists. */
7850 if (seen_unreachable)
7851 {
7852 /* Advance until the first case in the list is reachable. */
7853 while (body->ext.block.case_list != NULL
7854 && body->ext.block.case_list->unreachable)
7855 {
7856 gfc_case *n = body->ext.block.case_list;
7857 body->ext.block.case_list = body->ext.block.case_list->next;
7858 n->next = NULL;
7859 gfc_free_case_list (n);
7860 }
7861
7862 /* Strip all other unreachable cases. */
7863 if (body->ext.block.case_list)
7864 {
7865 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7866 {
7867 if (cp->next->unreachable)
7868 {
7869 gfc_case *n = cp->next;
7870 cp->next = cp->next->next;
7871 n->next = NULL;
7872 gfc_free_case_list (n);
7873 }
7874 }
7875 }
7876 }
7877 }
7878
7879 /* See if there were overlapping cases. If the check returns NULL,
7880 there was overlap. In that case we don't do anything. If head
7881 is non-NULL, we prepend the DEFAULT case. The sorted list can
7882 then used during code generation for SELECT CASE constructs with
7883 a case expression of a CHARACTER type. */
7884 if (head)
7885 {
7886 head = check_case_overlap (head);
7887
7888 /* Prepend the default_case if it is there. */
7889 if (head != NULL && default_case)
7890 {
7891 default_case->left = NULL;
7892 default_case->right = head;
7893 head->left = default_case;
7894 }
7895 }
7896
7897 /* Eliminate dead blocks that may be the result if we've seen
7898 unreachable case labels for a block. */
7899 for (body = code; body && body->block; body = body->block)
7900 {
7901 if (body->block->ext.block.case_list == NULL)
7902 {
7903 /* Cut the unreachable block from the code chain. */
7904 gfc_code *c = body->block;
7905 body->block = c->block;
7906
7907 /* Kill the dead block, but not the blocks below it. */
7908 c->block = NULL;
7909 gfc_free_statements (c);
7910 }
7911 }
7912
7913 /* More than two cases is legal but insane for logical selects.
7914 Issue a warning for it. */
7915 if (gfc_option.warn_surprising && type == BT_LOGICAL
7916 && ncases > 2)
7917 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7918 &code->loc);
7919 }
7920
7921
7922 /* Check if a derived type is extensible. */
7923
7924 bool
7925 gfc_type_is_extensible (gfc_symbol *sym)
7926 {
7927 return !(sym->attr.is_bind_c || sym->attr.sequence);
7928 }
7929
7930
7931 /* Resolve an associate name: Resolve target and ensure the type-spec is
7932 correct as well as possibly the array-spec. */
7933
7934 static void
7935 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7936 {
7937 gfc_expr* target;
7938
7939 gcc_assert (sym->assoc);
7940 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7941
7942 /* If this is for SELECT TYPE, the target may not yet be set. In that
7943 case, return. Resolution will be called later manually again when
7944 this is done. */
7945 target = sym->assoc->target;
7946 if (!target)
7947 return;
7948 gcc_assert (!sym->assoc->dangling);
7949
7950 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7951 return;
7952
7953 /* For variable targets, we get some attributes from the target. */
7954 if (target->expr_type == EXPR_VARIABLE)
7955 {
7956 gfc_symbol* tsym;
7957
7958 gcc_assert (target->symtree);
7959 tsym = target->symtree->n.sym;
7960
7961 sym->attr.asynchronous = tsym->attr.asynchronous;
7962 sym->attr.volatile_ = tsym->attr.volatile_;
7963
7964 sym->attr.target = tsym->attr.target
7965 || gfc_expr_attr (target).pointer;
7966 }
7967
7968 /* Get type if this was not already set. Note that it can be
7969 some other type than the target in case this is a SELECT TYPE
7970 selector! So we must not update when the type is already there. */
7971 if (sym->ts.type == BT_UNKNOWN)
7972 sym->ts = target->ts;
7973 gcc_assert (sym->ts.type != BT_UNKNOWN);
7974
7975 /* See if this is a valid association-to-variable. */
7976 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7977 && !gfc_has_vector_subscript (target));
7978
7979 /* Finally resolve if this is an array or not. */
7980 if (sym->attr.dimension && target->rank == 0)
7981 {
7982 gfc_error ("Associate-name '%s' at %L is used as array",
7983 sym->name, &sym->declared_at);
7984 sym->attr.dimension = 0;
7985 return;
7986 }
7987 if (target->rank > 0)
7988 sym->attr.dimension = 1;
7989
7990 if (sym->attr.dimension)
7991 {
7992 sym->as = gfc_get_array_spec ();
7993 sym->as->rank = target->rank;
7994 sym->as->type = AS_DEFERRED;
7995
7996 /* Target must not be coindexed, thus the associate-variable
7997 has no corank. */
7998 sym->as->corank = 0;
7999 }
8000 }
8001
8002
8003 /* Resolve a SELECT TYPE statement. */
8004
8005 static void
8006 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8007 {
8008 gfc_symbol *selector_type;
8009 gfc_code *body, *new_st, *if_st, *tail;
8010 gfc_code *class_is = NULL, *default_case = NULL;
8011 gfc_case *c;
8012 gfc_symtree *st;
8013 char name[GFC_MAX_SYMBOL_LEN];
8014 gfc_namespace *ns;
8015 int error = 0;
8016
8017 ns = code->ext.block.ns;
8018 gfc_resolve (ns);
8019
8020 /* Check for F03:C813. */
8021 if (code->expr1->ts.type != BT_CLASS
8022 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8023 {
8024 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8025 "at %L", &code->loc);
8026 return;
8027 }
8028
8029 if (!code->expr1->symtree->n.sym->attr.class_ok)
8030 return;
8031
8032 if (code->expr2)
8033 {
8034 if (code->expr1->symtree->n.sym->attr.untyped)
8035 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8036 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8037 }
8038 else
8039 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8040
8041 /* Loop over TYPE IS / CLASS IS cases. */
8042 for (body = code->block; body; body = body->block)
8043 {
8044 c = body->ext.block.case_list;
8045
8046 /* Check F03:C815. */
8047 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8048 && !gfc_type_is_extensible (c->ts.u.derived))
8049 {
8050 gfc_error ("Derived type '%s' at %L must be extensible",
8051 c->ts.u.derived->name, &c->where);
8052 error++;
8053 continue;
8054 }
8055
8056 /* Check F03:C816. */
8057 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8058 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
8059 {
8060 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
8061 c->ts.u.derived->name, &c->where, selector_type->name);
8062 error++;
8063 continue;
8064 }
8065
8066 /* Intercept the DEFAULT case. */
8067 if (c->ts.type == BT_UNKNOWN)
8068 {
8069 /* Check F03:C818. */
8070 if (default_case)
8071 {
8072 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8073 "by a second DEFAULT CASE at %L",
8074 &default_case->ext.block.case_list->where, &c->where);
8075 error++;
8076 continue;
8077 }
8078
8079 default_case = body;
8080 }
8081 }
8082
8083 if (error > 0)
8084 return;
8085
8086 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8087 target if present. If there are any EXIT statements referring to the
8088 SELECT TYPE construct, this is no problem because the gfc_code
8089 reference stays the same and EXIT is equally possible from the BLOCK
8090 it is changed to. */
8091 code->op = EXEC_BLOCK;
8092 if (code->expr2)
8093 {
8094 gfc_association_list* assoc;
8095
8096 assoc = gfc_get_association_list ();
8097 assoc->st = code->expr1->symtree;
8098 assoc->target = gfc_copy_expr (code->expr2);
8099 assoc->target->where = code->expr2->where;
8100 /* assoc->variable will be set by resolve_assoc_var. */
8101
8102 code->ext.block.assoc = assoc;
8103 code->expr1->symtree->n.sym->assoc = assoc;
8104
8105 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8106 }
8107 else
8108 code->ext.block.assoc = NULL;
8109
8110 /* Add EXEC_SELECT to switch on type. */
8111 new_st = gfc_get_code ();
8112 new_st->op = code->op;
8113 new_st->expr1 = code->expr1;
8114 new_st->expr2 = code->expr2;
8115 new_st->block = code->block;
8116 code->expr1 = code->expr2 = NULL;
8117 code->block = NULL;
8118 if (!ns->code)
8119 ns->code = new_st;
8120 else
8121 ns->code->next = new_st;
8122 code = new_st;
8123 code->op = EXEC_SELECT;
8124 gfc_add_vptr_component (code->expr1);
8125 gfc_add_hash_component (code->expr1);
8126
8127 /* Loop over TYPE IS / CLASS IS cases. */
8128 for (body = code->block; body; body = body->block)
8129 {
8130 c = body->ext.block.case_list;
8131
8132 if (c->ts.type == BT_DERIVED)
8133 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8134 c->ts.u.derived->hash_value);
8135
8136 else if (c->ts.type == BT_UNKNOWN)
8137 continue;
8138
8139 /* Associate temporary to selector. This should only be done
8140 when this case is actually true, so build a new ASSOCIATE
8141 that does precisely this here (instead of using the
8142 'global' one). */
8143
8144 if (c->ts.type == BT_CLASS)
8145 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8146 else
8147 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8148 st = gfc_find_symtree (ns->sym_root, name);
8149 gcc_assert (st->n.sym->assoc);
8150 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8151 st->n.sym->assoc->target->where = code->expr1->where;
8152 if (c->ts.type == BT_DERIVED)
8153 gfc_add_data_component (st->n.sym->assoc->target);
8154
8155 new_st = gfc_get_code ();
8156 new_st->op = EXEC_BLOCK;
8157 new_st->ext.block.ns = gfc_build_block_ns (ns);
8158 new_st->ext.block.ns->code = body->next;
8159 body->next = new_st;
8160
8161 /* Chain in the new list only if it is marked as dangling. Otherwise
8162 there is a CASE label overlap and this is already used. Just ignore,
8163 the error is diagonsed elsewhere. */
8164 if (st->n.sym->assoc->dangling)
8165 {
8166 new_st->ext.block.assoc = st->n.sym->assoc;
8167 st->n.sym->assoc->dangling = 0;
8168 }
8169
8170 resolve_assoc_var (st->n.sym, false);
8171 }
8172
8173 /* Take out CLASS IS cases for separate treatment. */
8174 body = code;
8175 while (body && body->block)
8176 {
8177 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8178 {
8179 /* Add to class_is list. */
8180 if (class_is == NULL)
8181 {
8182 class_is = body->block;
8183 tail = class_is;
8184 }
8185 else
8186 {
8187 for (tail = class_is; tail->block; tail = tail->block) ;
8188 tail->block = body->block;
8189 tail = tail->block;
8190 }
8191 /* Remove from EXEC_SELECT list. */
8192 body->block = body->block->block;
8193 tail->block = NULL;
8194 }
8195 else
8196 body = body->block;
8197 }
8198
8199 if (class_is)
8200 {
8201 gfc_symbol *vtab;
8202
8203 if (!default_case)
8204 {
8205 /* Add a default case to hold the CLASS IS cases. */
8206 for (tail = code; tail->block; tail = tail->block) ;
8207 tail->block = gfc_get_code ();
8208 tail = tail->block;
8209 tail->op = EXEC_SELECT_TYPE;
8210 tail->ext.block.case_list = gfc_get_case ();
8211 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8212 tail->next = NULL;
8213 default_case = tail;
8214 }
8215
8216 /* More than one CLASS IS block? */
8217 if (class_is->block)
8218 {
8219 gfc_code **c1,*c2;
8220 bool swapped;
8221 /* Sort CLASS IS blocks by extension level. */
8222 do
8223 {
8224 swapped = false;
8225 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8226 {
8227 c2 = (*c1)->block;
8228 /* F03:C817 (check for doubles). */
8229 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8230 == c2->ext.block.case_list->ts.u.derived->hash_value)
8231 {
8232 gfc_error ("Double CLASS IS block in SELECT TYPE "
8233 "statement at %L",
8234 &c2->ext.block.case_list->where);
8235 return;
8236 }
8237 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8238 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8239 {
8240 /* Swap. */
8241 (*c1)->block = c2->block;
8242 c2->block = *c1;
8243 *c1 = c2;
8244 swapped = true;
8245 }
8246 }
8247 }
8248 while (swapped);
8249 }
8250
8251 /* Generate IF chain. */
8252 if_st = gfc_get_code ();
8253 if_st->op = EXEC_IF;
8254 new_st = if_st;
8255 for (body = class_is; body; body = body->block)
8256 {
8257 new_st->block = gfc_get_code ();
8258 new_st = new_st->block;
8259 new_st->op = EXEC_IF;
8260 /* Set up IF condition: Call _gfortran_is_extension_of. */
8261 new_st->expr1 = gfc_get_expr ();
8262 new_st->expr1->expr_type = EXPR_FUNCTION;
8263 new_st->expr1->ts.type = BT_LOGICAL;
8264 new_st->expr1->ts.kind = 4;
8265 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8266 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8267 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8268 /* Set up arguments. */
8269 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8270 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8271 new_st->expr1->value.function.actual->expr->where = code->loc;
8272 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8273 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8274 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8275 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8276 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8277 new_st->next = body->next;
8278 }
8279 if (default_case->next)
8280 {
8281 new_st->block = gfc_get_code ();
8282 new_st = new_st->block;
8283 new_st->op = EXEC_IF;
8284 new_st->next = default_case->next;
8285 }
8286
8287 /* Replace CLASS DEFAULT code by the IF chain. */
8288 default_case->next = if_st;
8289 }
8290
8291 /* Resolve the internal code. This can not be done earlier because
8292 it requires that the sym->assoc of selectors is set already. */
8293 gfc_current_ns = ns;
8294 gfc_resolve_blocks (code->block, gfc_current_ns);
8295 gfc_current_ns = old_ns;
8296
8297 resolve_select (code);
8298 }
8299
8300
8301 /* Resolve a transfer statement. This is making sure that:
8302 -- a derived type being transferred has only non-pointer components
8303 -- a derived type being transferred doesn't have private components, unless
8304 it's being transferred from the module where the type was defined
8305 -- we're not trying to transfer a whole assumed size array. */
8306
8307 static void
8308 resolve_transfer (gfc_code *code)
8309 {
8310 gfc_typespec *ts;
8311 gfc_symbol *sym;
8312 gfc_ref *ref;
8313 gfc_expr *exp;
8314
8315 exp = code->expr1;
8316
8317 while (exp != NULL && exp->expr_type == EXPR_OP
8318 && exp->value.op.op == INTRINSIC_PARENTHESES)
8319 exp = exp->value.op.op1;
8320
8321 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8322 {
8323 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8324 "MOLD=", &exp->where);
8325 return;
8326 }
8327
8328 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8329 && exp->expr_type != EXPR_FUNCTION))
8330 return;
8331
8332 /* If we are reading, the variable will be changed. Note that
8333 code->ext.dt may be NULL if the TRANSFER is related to
8334 an INQUIRE statement -- but in this case, we are not reading, either. */
8335 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8336 && gfc_check_vardef_context (exp, false, false, _("item in READ"))
8337 == FAILURE)
8338 return;
8339
8340 sym = exp->symtree->n.sym;
8341 ts = &sym->ts;
8342
8343 /* Go to actual component transferred. */
8344 for (ref = exp->ref; ref; ref = ref->next)
8345 if (ref->type == REF_COMPONENT)
8346 ts = &ref->u.c.component->ts;
8347
8348 if (ts->type == BT_CLASS)
8349 {
8350 /* FIXME: Test for defined input/output. */
8351 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8352 "it is processed by a defined input/output procedure",
8353 &code->loc);
8354 return;
8355 }
8356
8357 if (ts->type == BT_DERIVED)
8358 {
8359 /* Check that transferred derived type doesn't contain POINTER
8360 components. */
8361 if (ts->u.derived->attr.pointer_comp)
8362 {
8363 gfc_error ("Data transfer element at %L cannot have POINTER "
8364 "components unless it is processed by a defined "
8365 "input/output procedure", &code->loc);
8366 return;
8367 }
8368
8369 /* F08:C935. */
8370 if (ts->u.derived->attr.proc_pointer_comp)
8371 {
8372 gfc_error ("Data transfer element at %L cannot have "
8373 "procedure pointer components", &code->loc);
8374 return;
8375 }
8376
8377 if (ts->u.derived->attr.alloc_comp)
8378 {
8379 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8380 "components unless it is processed by a defined "
8381 "input/output procedure", &code->loc);
8382 return;
8383 }
8384
8385 if (derived_inaccessible (ts->u.derived))
8386 {
8387 gfc_error ("Data transfer element at %L cannot have "
8388 "PRIVATE components",&code->loc);
8389 return;
8390 }
8391 }
8392
8393 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8394 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8395 {
8396 gfc_error ("Data transfer element at %L cannot be a full reference to "
8397 "an assumed-size array", &code->loc);
8398 return;
8399 }
8400 }
8401
8402
8403 /*********** Toplevel code resolution subroutines ***********/
8404
8405 /* Find the set of labels that are reachable from this block. We also
8406 record the last statement in each block. */
8407
8408 static void
8409 find_reachable_labels (gfc_code *block)
8410 {
8411 gfc_code *c;
8412
8413 if (!block)
8414 return;
8415
8416 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8417
8418 /* Collect labels in this block. We don't keep those corresponding
8419 to END {IF|SELECT}, these are checked in resolve_branch by going
8420 up through the code_stack. */
8421 for (c = block; c; c = c->next)
8422 {
8423 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8424 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8425 }
8426
8427 /* Merge with labels from parent block. */
8428 if (cs_base->prev)
8429 {
8430 gcc_assert (cs_base->prev->reachable_labels);
8431 bitmap_ior_into (cs_base->reachable_labels,
8432 cs_base->prev->reachable_labels);
8433 }
8434 }
8435
8436
8437 static void
8438 resolve_lock_unlock (gfc_code *code)
8439 {
8440 if (code->expr1->ts.type != BT_DERIVED
8441 || code->expr1->expr_type != EXPR_VARIABLE
8442 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8443 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8444 || code->expr1->rank != 0
8445 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8446 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8447 &code->expr1->where);
8448
8449 /* Check STAT. */
8450 if (code->expr2
8451 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8452 || code->expr2->expr_type != EXPR_VARIABLE))
8453 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8454 &code->expr2->where);
8455
8456 if (code->expr2
8457 && gfc_check_vardef_context (code->expr2, false, false,
8458 _("STAT variable")) == FAILURE)
8459 return;
8460
8461 /* Check ERRMSG. */
8462 if (code->expr3
8463 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8464 || code->expr3->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8466 &code->expr3->where);
8467
8468 if (code->expr3
8469 && gfc_check_vardef_context (code->expr3, false, false,
8470 _("ERRMSG variable")) == FAILURE)
8471 return;
8472
8473 /* Check ACQUIRED_LOCK. */
8474 if (code->expr4
8475 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8476 || code->expr4->expr_type != EXPR_VARIABLE))
8477 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8478 "variable", &code->expr4->where);
8479
8480 if (code->expr4
8481 && gfc_check_vardef_context (code->expr4, false, false,
8482 _("ACQUIRED_LOCK variable")) == FAILURE)
8483 return;
8484 }
8485
8486
8487 static void
8488 resolve_sync (gfc_code *code)
8489 {
8490 /* Check imageset. The * case matches expr1 == NULL. */
8491 if (code->expr1)
8492 {
8493 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8494 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8495 "INTEGER expression", &code->expr1->where);
8496 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8497 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8498 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8499 &code->expr1->where);
8500 else if (code->expr1->expr_type == EXPR_ARRAY
8501 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8502 {
8503 gfc_constructor *cons;
8504 cons = gfc_constructor_first (code->expr1->value.constructor);
8505 for (; cons; cons = gfc_constructor_next (cons))
8506 if (cons->expr->expr_type == EXPR_CONSTANT
8507 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8508 gfc_error ("Imageset argument at %L must between 1 and "
8509 "num_images()", &cons->expr->where);
8510 }
8511 }
8512
8513 /* Check STAT. */
8514 if (code->expr2
8515 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8516 || code->expr2->expr_type != EXPR_VARIABLE))
8517 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8518 &code->expr2->where);
8519
8520 /* Check ERRMSG. */
8521 if (code->expr3
8522 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8523 || code->expr3->expr_type != EXPR_VARIABLE))
8524 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8525 &code->expr3->where);
8526 }
8527
8528
8529 /* Given a branch to a label, see if the branch is conforming.
8530 The code node describes where the branch is located. */
8531
8532 static void
8533 resolve_branch (gfc_st_label *label, gfc_code *code)
8534 {
8535 code_stack *stack;
8536
8537 if (label == NULL)
8538 return;
8539
8540 /* Step one: is this a valid branching target? */
8541
8542 if (label->defined == ST_LABEL_UNKNOWN)
8543 {
8544 gfc_error ("Label %d referenced at %L is never defined", label->value,
8545 &label->where);
8546 return;
8547 }
8548
8549 if (label->defined != ST_LABEL_TARGET)
8550 {
8551 gfc_error ("Statement at %L is not a valid branch target statement "
8552 "for the branch statement at %L", &label->where, &code->loc);
8553 return;
8554 }
8555
8556 /* Step two: make sure this branch is not a branch to itself ;-) */
8557
8558 if (code->here == label)
8559 {
8560 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8561 return;
8562 }
8563
8564 /* Step three: See if the label is in the same block as the
8565 branching statement. The hard work has been done by setting up
8566 the bitmap reachable_labels. */
8567
8568 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8569 {
8570 /* Check now whether there is a CRITICAL construct; if so, check
8571 whether the label is still visible outside of the CRITICAL block,
8572 which is invalid. */
8573 for (stack = cs_base; stack; stack = stack->prev)
8574 {
8575 if (stack->current->op == EXEC_CRITICAL
8576 && bitmap_bit_p (stack->reachable_labels, label->value))
8577 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8578 "label at %L", &code->loc, &label->where);
8579 else if (stack->current->op == EXEC_DO_CONCURRENT
8580 && bitmap_bit_p (stack->reachable_labels, label->value))
8581 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8582 "for label at %L", &code->loc, &label->where);
8583 }
8584
8585 return;
8586 }
8587
8588 /* Step four: If we haven't found the label in the bitmap, it may
8589 still be the label of the END of the enclosing block, in which
8590 case we find it by going up the code_stack. */
8591
8592 for (stack = cs_base; stack; stack = stack->prev)
8593 {
8594 if (stack->current->next && stack->current->next->here == label)
8595 break;
8596 if (stack->current->op == EXEC_CRITICAL)
8597 {
8598 /* Note: A label at END CRITICAL does not leave the CRITICAL
8599 construct as END CRITICAL is still part of it. */
8600 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8601 " at %L", &code->loc, &label->where);
8602 return;
8603 }
8604 else if (stack->current->op == EXEC_DO_CONCURRENT)
8605 {
8606 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8607 "label at %L", &code->loc, &label->where);
8608 return;
8609 }
8610 }
8611
8612 if (stack)
8613 {
8614 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8615 return;
8616 }
8617
8618 /* The label is not in an enclosing block, so illegal. This was
8619 allowed in Fortran 66, so we allow it as extension. No
8620 further checks are necessary in this case. */
8621 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8622 "as the GOTO statement at %L", &label->where,
8623 &code->loc);
8624 return;
8625 }
8626
8627
8628 /* Check whether EXPR1 has the same shape as EXPR2. */
8629
8630 static gfc_try
8631 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8632 {
8633 mpz_t shape[GFC_MAX_DIMENSIONS];
8634 mpz_t shape2[GFC_MAX_DIMENSIONS];
8635 gfc_try result = FAILURE;
8636 int i;
8637
8638 /* Compare the rank. */
8639 if (expr1->rank != expr2->rank)
8640 return result;
8641
8642 /* Compare the size of each dimension. */
8643 for (i=0; i<expr1->rank; i++)
8644 {
8645 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8646 goto ignore;
8647
8648 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8649 goto ignore;
8650
8651 if (mpz_cmp (shape[i], shape2[i]))
8652 goto over;
8653 }
8654
8655 /* When either of the two expression is an assumed size array, we
8656 ignore the comparison of dimension sizes. */
8657 ignore:
8658 result = SUCCESS;
8659
8660 over:
8661 gfc_clear_shape (shape, i);
8662 gfc_clear_shape (shape2, i);
8663 return result;
8664 }
8665
8666
8667 /* Check whether a WHERE assignment target or a WHERE mask expression
8668 has the same shape as the outmost WHERE mask expression. */
8669
8670 static void
8671 resolve_where (gfc_code *code, gfc_expr *mask)
8672 {
8673 gfc_code *cblock;
8674 gfc_code *cnext;
8675 gfc_expr *e = NULL;
8676
8677 cblock = code->block;
8678
8679 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8680 In case of nested WHERE, only the outmost one is stored. */
8681 if (mask == NULL) /* outmost WHERE */
8682 e = cblock->expr1;
8683 else /* inner WHERE */
8684 e = mask;
8685
8686 while (cblock)
8687 {
8688 if (cblock->expr1)
8689 {
8690 /* Check if the mask-expr has a consistent shape with the
8691 outmost WHERE mask-expr. */
8692 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8693 gfc_error ("WHERE mask at %L has inconsistent shape",
8694 &cblock->expr1->where);
8695 }
8696
8697 /* the assignment statement of a WHERE statement, or the first
8698 statement in where-body-construct of a WHERE construct */
8699 cnext = cblock->next;
8700 while (cnext)
8701 {
8702 switch (cnext->op)
8703 {
8704 /* WHERE assignment statement */
8705 case EXEC_ASSIGN:
8706
8707 /* Check shape consistent for WHERE assignment target. */
8708 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8709 gfc_error ("WHERE assignment target at %L has "
8710 "inconsistent shape", &cnext->expr1->where);
8711 break;
8712
8713
8714 case EXEC_ASSIGN_CALL:
8715 resolve_call (cnext);
8716 if (!cnext->resolved_sym->attr.elemental)
8717 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8718 &cnext->ext.actual->expr->where);
8719 break;
8720
8721 /* WHERE or WHERE construct is part of a where-body-construct */
8722 case EXEC_WHERE:
8723 resolve_where (cnext, e);
8724 break;
8725
8726 default:
8727 gfc_error ("Unsupported statement inside WHERE at %L",
8728 &cnext->loc);
8729 }
8730 /* the next statement within the same where-body-construct */
8731 cnext = cnext->next;
8732 }
8733 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8734 cblock = cblock->block;
8735 }
8736 }
8737
8738
8739 /* Resolve assignment in FORALL construct.
8740 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8741 FORALL index variables. */
8742
8743 static void
8744 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8745 {
8746 int n;
8747
8748 for (n = 0; n < nvar; n++)
8749 {
8750 gfc_symbol *forall_index;
8751
8752 forall_index = var_expr[n]->symtree->n.sym;
8753
8754 /* Check whether the assignment target is one of the FORALL index
8755 variable. */
8756 if ((code->expr1->expr_type == EXPR_VARIABLE)
8757 && (code->expr1->symtree->n.sym == forall_index))
8758 gfc_error ("Assignment to a FORALL index variable at %L",
8759 &code->expr1->where);
8760 else
8761 {
8762 /* If one of the FORALL index variables doesn't appear in the
8763 assignment variable, then there could be a many-to-one
8764 assignment. Emit a warning rather than an error because the
8765 mask could be resolving this problem. */
8766 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8767 gfc_warning ("The FORALL with index '%s' is not used on the "
8768 "left side of the assignment at %L and so might "
8769 "cause multiple assignment to this object",
8770 var_expr[n]->symtree->name, &code->expr1->where);
8771 }
8772 }
8773 }
8774
8775
8776 /* Resolve WHERE statement in FORALL construct. */
8777
8778 static void
8779 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8780 gfc_expr **var_expr)
8781 {
8782 gfc_code *cblock;
8783 gfc_code *cnext;
8784
8785 cblock = code->block;
8786 while (cblock)
8787 {
8788 /* the assignment statement of a WHERE statement, or the first
8789 statement in where-body-construct of a WHERE construct */
8790 cnext = cblock->next;
8791 while (cnext)
8792 {
8793 switch (cnext->op)
8794 {
8795 /* WHERE assignment statement */
8796 case EXEC_ASSIGN:
8797 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8798 break;
8799
8800 /* WHERE operator assignment statement */
8801 case EXEC_ASSIGN_CALL:
8802 resolve_call (cnext);
8803 if (!cnext->resolved_sym->attr.elemental)
8804 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8805 &cnext->ext.actual->expr->where);
8806 break;
8807
8808 /* WHERE or WHERE construct is part of a where-body-construct */
8809 case EXEC_WHERE:
8810 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8811 break;
8812
8813 default:
8814 gfc_error ("Unsupported statement inside WHERE at %L",
8815 &cnext->loc);
8816 }
8817 /* the next statement within the same where-body-construct */
8818 cnext = cnext->next;
8819 }
8820 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8821 cblock = cblock->block;
8822 }
8823 }
8824
8825
8826 /* Traverse the FORALL body to check whether the following errors exist:
8827 1. For assignment, check if a many-to-one assignment happens.
8828 2. For WHERE statement, check the WHERE body to see if there is any
8829 many-to-one assignment. */
8830
8831 static void
8832 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8833 {
8834 gfc_code *c;
8835
8836 c = code->block->next;
8837 while (c)
8838 {
8839 switch (c->op)
8840 {
8841 case EXEC_ASSIGN:
8842 case EXEC_POINTER_ASSIGN:
8843 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8844 break;
8845
8846 case EXEC_ASSIGN_CALL:
8847 resolve_call (c);
8848 break;
8849
8850 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8851 there is no need to handle it here. */
8852 case EXEC_FORALL:
8853 break;
8854 case EXEC_WHERE:
8855 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8856 break;
8857 default:
8858 break;
8859 }
8860 /* The next statement in the FORALL body. */
8861 c = c->next;
8862 }
8863 }
8864
8865
8866 /* Counts the number of iterators needed inside a forall construct, including
8867 nested forall constructs. This is used to allocate the needed memory
8868 in gfc_resolve_forall. */
8869
8870 static int
8871 gfc_count_forall_iterators (gfc_code *code)
8872 {
8873 int max_iters, sub_iters, current_iters;
8874 gfc_forall_iterator *fa;
8875
8876 gcc_assert(code->op == EXEC_FORALL);
8877 max_iters = 0;
8878 current_iters = 0;
8879
8880 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8881 current_iters ++;
8882
8883 code = code->block->next;
8884
8885 while (code)
8886 {
8887 if (code->op == EXEC_FORALL)
8888 {
8889 sub_iters = gfc_count_forall_iterators (code);
8890 if (sub_iters > max_iters)
8891 max_iters = sub_iters;
8892 }
8893 code = code->next;
8894 }
8895
8896 return current_iters + max_iters;
8897 }
8898
8899
8900 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8901 gfc_resolve_forall_body to resolve the FORALL body. */
8902
8903 static void
8904 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8905 {
8906 static gfc_expr **var_expr;
8907 static int total_var = 0;
8908 static int nvar = 0;
8909 int old_nvar, tmp;
8910 gfc_forall_iterator *fa;
8911 int i;
8912
8913 old_nvar = nvar;
8914
8915 /* Start to resolve a FORALL construct */
8916 if (forall_save == 0)
8917 {
8918 /* Count the total number of FORALL index in the nested FORALL
8919 construct in order to allocate the VAR_EXPR with proper size. */
8920 total_var = gfc_count_forall_iterators (code);
8921
8922 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8923 var_expr = XCNEWVEC (gfc_expr *, total_var);
8924 }
8925
8926 /* The information about FORALL iterator, including FORALL index start, end
8927 and stride. The FORALL index can not appear in start, end or stride. */
8928 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8929 {
8930 /* Check if any outer FORALL index name is the same as the current
8931 one. */
8932 for (i = 0; i < nvar; i++)
8933 {
8934 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8935 {
8936 gfc_error ("An outer FORALL construct already has an index "
8937 "with this name %L", &fa->var->where);
8938 }
8939 }
8940
8941 /* Record the current FORALL index. */
8942 var_expr[nvar] = gfc_copy_expr (fa->var);
8943
8944 nvar++;
8945
8946 /* No memory leak. */
8947 gcc_assert (nvar <= total_var);
8948 }
8949
8950 /* Resolve the FORALL body. */
8951 gfc_resolve_forall_body (code, nvar, var_expr);
8952
8953 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8954 gfc_resolve_blocks (code->block, ns);
8955
8956 tmp = nvar;
8957 nvar = old_nvar;
8958 /* Free only the VAR_EXPRs allocated in this frame. */
8959 for (i = nvar; i < tmp; i++)
8960 gfc_free_expr (var_expr[i]);
8961
8962 if (nvar == 0)
8963 {
8964 /* We are in the outermost FORALL construct. */
8965 gcc_assert (forall_save == 0);
8966
8967 /* VAR_EXPR is not needed any more. */
8968 free (var_expr);
8969 total_var = 0;
8970 }
8971 }
8972
8973
8974 /* Resolve a BLOCK construct statement. */
8975
8976 static void
8977 resolve_block_construct (gfc_code* code)
8978 {
8979 /* Resolve the BLOCK's namespace. */
8980 gfc_resolve (code->ext.block.ns);
8981
8982 /* For an ASSOCIATE block, the associations (and their targets) are already
8983 resolved during resolve_symbol. */
8984 }
8985
8986
8987 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8988 DO code nodes. */
8989
8990 static void resolve_code (gfc_code *, gfc_namespace *);
8991
8992 void
8993 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8994 {
8995 gfc_try t;
8996
8997 for (; b; b = b->block)
8998 {
8999 t = gfc_resolve_expr (b->expr1);
9000 if (gfc_resolve_expr (b->expr2) == FAILURE)
9001 t = FAILURE;
9002
9003 switch (b->op)
9004 {
9005 case EXEC_IF:
9006 if (t == SUCCESS && b->expr1 != NULL
9007 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9008 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9009 &b->expr1->where);
9010 break;
9011
9012 case EXEC_WHERE:
9013 if (t == SUCCESS
9014 && b->expr1 != NULL
9015 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9016 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9017 &b->expr1->where);
9018 break;
9019
9020 case EXEC_GOTO:
9021 resolve_branch (b->label1, b);
9022 break;
9023
9024 case EXEC_BLOCK:
9025 resolve_block_construct (b);
9026 break;
9027
9028 case EXEC_SELECT:
9029 case EXEC_SELECT_TYPE:
9030 case EXEC_FORALL:
9031 case EXEC_DO:
9032 case EXEC_DO_WHILE:
9033 case EXEC_DO_CONCURRENT:
9034 case EXEC_CRITICAL:
9035 case EXEC_READ:
9036 case EXEC_WRITE:
9037 case EXEC_IOLENGTH:
9038 case EXEC_WAIT:
9039 break;
9040
9041 case EXEC_OMP_ATOMIC:
9042 case EXEC_OMP_CRITICAL:
9043 case EXEC_OMP_DO:
9044 case EXEC_OMP_MASTER:
9045 case EXEC_OMP_ORDERED:
9046 case EXEC_OMP_PARALLEL:
9047 case EXEC_OMP_PARALLEL_DO:
9048 case EXEC_OMP_PARALLEL_SECTIONS:
9049 case EXEC_OMP_PARALLEL_WORKSHARE:
9050 case EXEC_OMP_SECTIONS:
9051 case EXEC_OMP_SINGLE:
9052 case EXEC_OMP_TASK:
9053 case EXEC_OMP_TASKWAIT:
9054 case EXEC_OMP_TASKYIELD:
9055 case EXEC_OMP_WORKSHARE:
9056 break;
9057
9058 default:
9059 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9060 }
9061
9062 resolve_code (b->next, ns);
9063 }
9064 }
9065
9066
9067 /* Does everything to resolve an ordinary assignment. Returns true
9068 if this is an interface assignment. */
9069 static bool
9070 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9071 {
9072 bool rval = false;
9073 gfc_expr *lhs;
9074 gfc_expr *rhs;
9075 int llen = 0;
9076 int rlen = 0;
9077 int n;
9078 gfc_ref *ref;
9079
9080 if (gfc_extend_assign (code, ns) == SUCCESS)
9081 {
9082 gfc_expr** rhsptr;
9083
9084 if (code->op == EXEC_ASSIGN_CALL)
9085 {
9086 lhs = code->ext.actual->expr;
9087 rhsptr = &code->ext.actual->next->expr;
9088 }
9089 else
9090 {
9091 gfc_actual_arglist* args;
9092 gfc_typebound_proc* tbp;
9093
9094 gcc_assert (code->op == EXEC_COMPCALL);
9095
9096 args = code->expr1->value.compcall.actual;
9097 lhs = args->expr;
9098 rhsptr = &args->next->expr;
9099
9100 tbp = code->expr1->value.compcall.tbp;
9101 gcc_assert (!tbp->is_generic);
9102 }
9103
9104 /* Make a temporary rhs when there is a default initializer
9105 and rhs is the same symbol as the lhs. */
9106 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9107 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9108 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9109 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9110 *rhsptr = gfc_get_parentheses (*rhsptr);
9111
9112 return true;
9113 }
9114
9115 lhs = code->expr1;
9116 rhs = code->expr2;
9117
9118 if (rhs->is_boz
9119 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
9120 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9121 &code->loc) == FAILURE)
9122 return false;
9123
9124 /* Handle the case of a BOZ literal on the RHS. */
9125 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9126 {
9127 int rc;
9128 if (gfc_option.warn_surprising)
9129 gfc_warning ("BOZ literal at %L is bitwise transferred "
9130 "non-integer symbol '%s'", &code->loc,
9131 lhs->symtree->n.sym->name);
9132
9133 if (!gfc_convert_boz (rhs, &lhs->ts))
9134 return false;
9135 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9136 {
9137 if (rc == ARITH_UNDERFLOW)
9138 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9139 ". This check can be disabled with the option "
9140 "-fno-range-check", &rhs->where);
9141 else if (rc == ARITH_OVERFLOW)
9142 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9143 ". This check can be disabled with the option "
9144 "-fno-range-check", &rhs->where);
9145 else if (rc == ARITH_NAN)
9146 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9147 ". This check can be disabled with the option "
9148 "-fno-range-check", &rhs->where);
9149 return false;
9150 }
9151 }
9152
9153 if (lhs->ts.type == BT_CHARACTER
9154 && gfc_option.warn_character_truncation)
9155 {
9156 if (lhs->ts.u.cl != NULL
9157 && lhs->ts.u.cl->length != NULL
9158 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9159 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9160
9161 if (rhs->expr_type == EXPR_CONSTANT)
9162 rlen = rhs->value.character.length;
9163
9164 else if (rhs->ts.u.cl != NULL
9165 && rhs->ts.u.cl->length != NULL
9166 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9167 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9168
9169 if (rlen && llen && rlen > llen)
9170 gfc_warning_now ("CHARACTER expression will be truncated "
9171 "in assignment (%d/%d) at %L",
9172 llen, rlen, &code->loc);
9173 }
9174
9175 /* Ensure that a vector index expression for the lvalue is evaluated
9176 to a temporary if the lvalue symbol is referenced in it. */
9177 if (lhs->rank)
9178 {
9179 for (ref = lhs->ref; ref; ref= ref->next)
9180 if (ref->type == REF_ARRAY)
9181 {
9182 for (n = 0; n < ref->u.ar.dimen; n++)
9183 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9184 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9185 ref->u.ar.start[n]))
9186 ref->u.ar.start[n]
9187 = gfc_get_parentheses (ref->u.ar.start[n]);
9188 }
9189 }
9190
9191 if (gfc_pure (NULL))
9192 {
9193 if (lhs->ts.type == BT_DERIVED
9194 && lhs->expr_type == EXPR_VARIABLE
9195 && lhs->ts.u.derived->attr.pointer_comp
9196 && rhs->expr_type == EXPR_VARIABLE
9197 && (gfc_impure_variable (rhs->symtree->n.sym)
9198 || gfc_is_coindexed (rhs)))
9199 {
9200 /* F2008, C1283. */
9201 if (gfc_is_coindexed (rhs))
9202 gfc_error ("Coindexed expression at %L is assigned to "
9203 "a derived type variable with a POINTER "
9204 "component in a PURE procedure",
9205 &rhs->where);
9206 else
9207 gfc_error ("The impure variable at %L is assigned to "
9208 "a derived type variable with a POINTER "
9209 "component in a PURE procedure (12.6)",
9210 &rhs->where);
9211 return rval;
9212 }
9213
9214 /* Fortran 2008, C1283. */
9215 if (gfc_is_coindexed (lhs))
9216 {
9217 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9218 "procedure", &rhs->where);
9219 return rval;
9220 }
9221 }
9222
9223 if (gfc_implicit_pure (NULL))
9224 {
9225 if (lhs->expr_type == EXPR_VARIABLE
9226 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9227 && lhs->symtree->n.sym->ns != gfc_current_ns)
9228 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9229
9230 if (lhs->ts.type == BT_DERIVED
9231 && lhs->expr_type == EXPR_VARIABLE
9232 && lhs->ts.u.derived->attr.pointer_comp
9233 && rhs->expr_type == EXPR_VARIABLE
9234 && (gfc_impure_variable (rhs->symtree->n.sym)
9235 || gfc_is_coindexed (rhs)))
9236 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9237
9238 /* Fortran 2008, C1283. */
9239 if (gfc_is_coindexed (lhs))
9240 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9241 }
9242
9243 /* F03:7.4.1.2. */
9244 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9245 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9246 if (lhs->ts.type == BT_CLASS)
9247 {
9248 gfc_error ("Variable must not be polymorphic in intrinsic assignment at "
9249 "%L - check that there is a matching specific subroutine "
9250 "for '=' operator", &lhs->where);
9251 return false;
9252 }
9253
9254 /* F2008, Section 7.2.1.2. */
9255 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9256 {
9257 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9258 "component in assignment at %L", &lhs->where);
9259 return false;
9260 }
9261
9262 gfc_check_assign (lhs, rhs, 1);
9263 return false;
9264 }
9265
9266
9267 /* Given a block of code, recursively resolve everything pointed to by this
9268 code block. */
9269
9270 static void
9271 resolve_code (gfc_code *code, gfc_namespace *ns)
9272 {
9273 int omp_workshare_save;
9274 int forall_save, do_concurrent_save;
9275 code_stack frame;
9276 gfc_try t;
9277
9278 frame.prev = cs_base;
9279 frame.head = code;
9280 cs_base = &frame;
9281
9282 find_reachable_labels (code);
9283
9284 for (; code; code = code->next)
9285 {
9286 frame.current = code;
9287 forall_save = forall_flag;
9288 do_concurrent_save = do_concurrent_flag;
9289
9290 if (code->op == EXEC_FORALL)
9291 {
9292 forall_flag = 1;
9293 gfc_resolve_forall (code, ns, forall_save);
9294 forall_flag = 2;
9295 }
9296 else if (code->block)
9297 {
9298 omp_workshare_save = -1;
9299 switch (code->op)
9300 {
9301 case EXEC_OMP_PARALLEL_WORKSHARE:
9302 omp_workshare_save = omp_workshare_flag;
9303 omp_workshare_flag = 1;
9304 gfc_resolve_omp_parallel_blocks (code, ns);
9305 break;
9306 case EXEC_OMP_PARALLEL:
9307 case EXEC_OMP_PARALLEL_DO:
9308 case EXEC_OMP_PARALLEL_SECTIONS:
9309 case EXEC_OMP_TASK:
9310 omp_workshare_save = omp_workshare_flag;
9311 omp_workshare_flag = 0;
9312 gfc_resolve_omp_parallel_blocks (code, ns);
9313 break;
9314 case EXEC_OMP_DO:
9315 gfc_resolve_omp_do_blocks (code, ns);
9316 break;
9317 case EXEC_SELECT_TYPE:
9318 /* Blocks are handled in resolve_select_type because we have
9319 to transform the SELECT TYPE into ASSOCIATE first. */
9320 break;
9321 case EXEC_DO_CONCURRENT:
9322 do_concurrent_flag = 1;
9323 gfc_resolve_blocks (code->block, ns);
9324 do_concurrent_flag = 2;
9325 break;
9326 case EXEC_OMP_WORKSHARE:
9327 omp_workshare_save = omp_workshare_flag;
9328 omp_workshare_flag = 1;
9329 /* FALLTHROUGH */
9330 default:
9331 gfc_resolve_blocks (code->block, ns);
9332 break;
9333 }
9334
9335 if (omp_workshare_save != -1)
9336 omp_workshare_flag = omp_workshare_save;
9337 }
9338
9339 t = SUCCESS;
9340 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9341 t = gfc_resolve_expr (code->expr1);
9342 forall_flag = forall_save;
9343 do_concurrent_flag = do_concurrent_save;
9344
9345 if (gfc_resolve_expr (code->expr2) == FAILURE)
9346 t = FAILURE;
9347
9348 if (code->op == EXEC_ALLOCATE
9349 && gfc_resolve_expr (code->expr3) == FAILURE)
9350 t = FAILURE;
9351
9352 switch (code->op)
9353 {
9354 case EXEC_NOP:
9355 case EXEC_END_BLOCK:
9356 case EXEC_END_NESTED_BLOCK:
9357 case EXEC_CYCLE:
9358 case EXEC_PAUSE:
9359 case EXEC_STOP:
9360 case EXEC_ERROR_STOP:
9361 case EXEC_EXIT:
9362 case EXEC_CONTINUE:
9363 case EXEC_DT_END:
9364 case EXEC_ASSIGN_CALL:
9365 case EXEC_CRITICAL:
9366 break;
9367
9368 case EXEC_SYNC_ALL:
9369 case EXEC_SYNC_IMAGES:
9370 case EXEC_SYNC_MEMORY:
9371 resolve_sync (code);
9372 break;
9373
9374 case EXEC_LOCK:
9375 case EXEC_UNLOCK:
9376 resolve_lock_unlock (code);
9377 break;
9378
9379 case EXEC_ENTRY:
9380 /* Keep track of which entry we are up to. */
9381 current_entry_id = code->ext.entry->id;
9382 break;
9383
9384 case EXEC_WHERE:
9385 resolve_where (code, NULL);
9386 break;
9387
9388 case EXEC_GOTO:
9389 if (code->expr1 != NULL)
9390 {
9391 if (code->expr1->ts.type != BT_INTEGER)
9392 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9393 "INTEGER variable", &code->expr1->where);
9394 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9395 gfc_error ("Variable '%s' has not been assigned a target "
9396 "label at %L", code->expr1->symtree->n.sym->name,
9397 &code->expr1->where);
9398 }
9399 else
9400 resolve_branch (code->label1, code);
9401 break;
9402
9403 case EXEC_RETURN:
9404 if (code->expr1 != NULL
9405 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9406 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9407 "INTEGER return specifier", &code->expr1->where);
9408 break;
9409
9410 case EXEC_INIT_ASSIGN:
9411 case EXEC_END_PROCEDURE:
9412 break;
9413
9414 case EXEC_ASSIGN:
9415 if (t == FAILURE)
9416 break;
9417
9418 if (gfc_check_vardef_context (code->expr1, false, false,
9419 _("assignment")) == FAILURE)
9420 break;
9421
9422 if (resolve_ordinary_assign (code, ns))
9423 {
9424 if (code->op == EXEC_COMPCALL)
9425 goto compcall;
9426 else
9427 goto call;
9428 }
9429 break;
9430
9431 case EXEC_LABEL_ASSIGN:
9432 if (code->label1->defined == ST_LABEL_UNKNOWN)
9433 gfc_error ("Label %d referenced at %L is never defined",
9434 code->label1->value, &code->label1->where);
9435 if (t == SUCCESS
9436 && (code->expr1->expr_type != EXPR_VARIABLE
9437 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9438 || code->expr1->symtree->n.sym->ts.kind
9439 != gfc_default_integer_kind
9440 || code->expr1->symtree->n.sym->as != NULL))
9441 gfc_error ("ASSIGN statement at %L requires a scalar "
9442 "default INTEGER variable", &code->expr1->where);
9443 break;
9444
9445 case EXEC_POINTER_ASSIGN:
9446 {
9447 gfc_expr* e;
9448
9449 if (t == FAILURE)
9450 break;
9451
9452 /* This is both a variable definition and pointer assignment
9453 context, so check both of them. For rank remapping, a final
9454 array ref may be present on the LHS and fool gfc_expr_attr
9455 used in gfc_check_vardef_context. Remove it. */
9456 e = remove_last_array_ref (code->expr1);
9457 t = gfc_check_vardef_context (e, true, false,
9458 _("pointer assignment"));
9459 if (t == SUCCESS)
9460 t = gfc_check_vardef_context (e, false, false,
9461 _("pointer assignment"));
9462 gfc_free_expr (e);
9463 if (t == FAILURE)
9464 break;
9465
9466 gfc_check_pointer_assign (code->expr1, code->expr2);
9467 break;
9468 }
9469
9470 case EXEC_ARITHMETIC_IF:
9471 if (t == SUCCESS
9472 && code->expr1->ts.type != BT_INTEGER
9473 && code->expr1->ts.type != BT_REAL)
9474 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9475 "expression", &code->expr1->where);
9476
9477 resolve_branch (code->label1, code);
9478 resolve_branch (code->label2, code);
9479 resolve_branch (code->label3, code);
9480 break;
9481
9482 case EXEC_IF:
9483 if (t == SUCCESS && code->expr1 != NULL
9484 && (code->expr1->ts.type != BT_LOGICAL
9485 || code->expr1->rank != 0))
9486 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9487 &code->expr1->where);
9488 break;
9489
9490 case EXEC_CALL:
9491 call:
9492 resolve_call (code);
9493 break;
9494
9495 case EXEC_COMPCALL:
9496 compcall:
9497 resolve_typebound_subroutine (code);
9498 break;
9499
9500 case EXEC_CALL_PPC:
9501 resolve_ppc_call (code);
9502 break;
9503
9504 case EXEC_SELECT:
9505 /* Select is complicated. Also, a SELECT construct could be
9506 a transformed computed GOTO. */
9507 resolve_select (code);
9508 break;
9509
9510 case EXEC_SELECT_TYPE:
9511 resolve_select_type (code, ns);
9512 break;
9513
9514 case EXEC_BLOCK:
9515 resolve_block_construct (code);
9516 break;
9517
9518 case EXEC_DO:
9519 if (code->ext.iterator != NULL)
9520 {
9521 gfc_iterator *iter = code->ext.iterator;
9522 if (gfc_resolve_iterator (iter, true) != FAILURE)
9523 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9524 }
9525 break;
9526
9527 case EXEC_DO_WHILE:
9528 if (code->expr1 == NULL)
9529 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9530 if (t == SUCCESS
9531 && (code->expr1->rank != 0
9532 || code->expr1->ts.type != BT_LOGICAL))
9533 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9534 "a scalar LOGICAL expression", &code->expr1->where);
9535 break;
9536
9537 case EXEC_ALLOCATE:
9538 if (t == SUCCESS)
9539 resolve_allocate_deallocate (code, "ALLOCATE");
9540
9541 break;
9542
9543 case EXEC_DEALLOCATE:
9544 if (t == SUCCESS)
9545 resolve_allocate_deallocate (code, "DEALLOCATE");
9546
9547 break;
9548
9549 case EXEC_OPEN:
9550 if (gfc_resolve_open (code->ext.open) == FAILURE)
9551 break;
9552
9553 resolve_branch (code->ext.open->err, code);
9554 break;
9555
9556 case EXEC_CLOSE:
9557 if (gfc_resolve_close (code->ext.close) == FAILURE)
9558 break;
9559
9560 resolve_branch (code->ext.close->err, code);
9561 break;
9562
9563 case EXEC_BACKSPACE:
9564 case EXEC_ENDFILE:
9565 case EXEC_REWIND:
9566 case EXEC_FLUSH:
9567 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9568 break;
9569
9570 resolve_branch (code->ext.filepos->err, code);
9571 break;
9572
9573 case EXEC_INQUIRE:
9574 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9575 break;
9576
9577 resolve_branch (code->ext.inquire->err, code);
9578 break;
9579
9580 case EXEC_IOLENGTH:
9581 gcc_assert (code->ext.inquire != NULL);
9582 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9583 break;
9584
9585 resolve_branch (code->ext.inquire->err, code);
9586 break;
9587
9588 case EXEC_WAIT:
9589 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9590 break;
9591
9592 resolve_branch (code->ext.wait->err, code);
9593 resolve_branch (code->ext.wait->end, code);
9594 resolve_branch (code->ext.wait->eor, code);
9595 break;
9596
9597 case EXEC_READ:
9598 case EXEC_WRITE:
9599 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9600 break;
9601
9602 resolve_branch (code->ext.dt->err, code);
9603 resolve_branch (code->ext.dt->end, code);
9604 resolve_branch (code->ext.dt->eor, code);
9605 break;
9606
9607 case EXEC_TRANSFER:
9608 resolve_transfer (code);
9609 break;
9610
9611 case EXEC_DO_CONCURRENT:
9612 case EXEC_FORALL:
9613 resolve_forall_iterators (code->ext.forall_iterator);
9614
9615 if (code->expr1 != NULL
9616 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9617 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9618 "expression", &code->expr1->where);
9619 break;
9620
9621 case EXEC_OMP_ATOMIC:
9622 case EXEC_OMP_BARRIER:
9623 case EXEC_OMP_CRITICAL:
9624 case EXEC_OMP_FLUSH:
9625 case EXEC_OMP_DO:
9626 case EXEC_OMP_MASTER:
9627 case EXEC_OMP_ORDERED:
9628 case EXEC_OMP_SECTIONS:
9629 case EXEC_OMP_SINGLE:
9630 case EXEC_OMP_TASKWAIT:
9631 case EXEC_OMP_TASKYIELD:
9632 case EXEC_OMP_WORKSHARE:
9633 gfc_resolve_omp_directive (code, ns);
9634 break;
9635
9636 case EXEC_OMP_PARALLEL:
9637 case EXEC_OMP_PARALLEL_DO:
9638 case EXEC_OMP_PARALLEL_SECTIONS:
9639 case EXEC_OMP_PARALLEL_WORKSHARE:
9640 case EXEC_OMP_TASK:
9641 omp_workshare_save = omp_workshare_flag;
9642 omp_workshare_flag = 0;
9643 gfc_resolve_omp_directive (code, ns);
9644 omp_workshare_flag = omp_workshare_save;
9645 break;
9646
9647 default:
9648 gfc_internal_error ("resolve_code(): Bad statement code");
9649 }
9650 }
9651
9652 cs_base = frame.prev;
9653 }
9654
9655
9656 /* Resolve initial values and make sure they are compatible with
9657 the variable. */
9658
9659 static void
9660 resolve_values (gfc_symbol *sym)
9661 {
9662 gfc_try t;
9663
9664 if (sym->value == NULL)
9665 return;
9666
9667 if (sym->value->expr_type == EXPR_STRUCTURE)
9668 t= resolve_structure_cons (sym->value, 1);
9669 else
9670 t = gfc_resolve_expr (sym->value);
9671
9672 if (t == FAILURE)
9673 return;
9674
9675 gfc_check_assign_symbol (sym, sym->value);
9676 }
9677
9678
9679 /* Verify the binding labels for common blocks that are BIND(C). The label
9680 for a BIND(C) common block must be identical in all scoping units in which
9681 the common block is declared. Further, the binding label can not collide
9682 with any other global entity in the program. */
9683
9684 static void
9685 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9686 {
9687 if (comm_block_tree->n.common->is_bind_c == 1)
9688 {
9689 gfc_gsymbol *binding_label_gsym;
9690 gfc_gsymbol *comm_name_gsym;
9691 const char * bind_label = comm_block_tree->n.common->binding_label
9692 ? comm_block_tree->n.common->binding_label : "";
9693
9694 /* See if a global symbol exists by the common block's name. It may
9695 be NULL if the common block is use-associated. */
9696 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9697 comm_block_tree->n.common->name);
9698 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9699 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9700 "with the global entity '%s' at %L",
9701 bind_label,
9702 comm_block_tree->n.common->name,
9703 &(comm_block_tree->n.common->where),
9704 comm_name_gsym->name, &(comm_name_gsym->where));
9705 else if (comm_name_gsym != NULL
9706 && strcmp (comm_name_gsym->name,
9707 comm_block_tree->n.common->name) == 0)
9708 {
9709 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9710 as expected. */
9711 if (comm_name_gsym->binding_label == NULL)
9712 /* No binding label for common block stored yet; save this one. */
9713 comm_name_gsym->binding_label = bind_label;
9714 else if (strcmp (comm_name_gsym->binding_label, bind_label) != 0)
9715 {
9716 /* Common block names match but binding labels do not. */
9717 gfc_error ("Binding label '%s' for common block '%s' at %L "
9718 "does not match the binding label '%s' for common "
9719 "block '%s' at %L",
9720 bind_label,
9721 comm_block_tree->n.common->name,
9722 &(comm_block_tree->n.common->where),
9723 comm_name_gsym->binding_label,
9724 comm_name_gsym->name,
9725 &(comm_name_gsym->where));
9726 return;
9727 }
9728 }
9729
9730 /* There is no binding label (NAME="") so we have nothing further to
9731 check and nothing to add as a global symbol for the label. */
9732 if (!comm_block_tree->n.common->binding_label)
9733 return;
9734
9735 binding_label_gsym =
9736 gfc_find_gsymbol (gfc_gsym_root,
9737 comm_block_tree->n.common->binding_label);
9738 if (binding_label_gsym == NULL)
9739 {
9740 /* Need to make a global symbol for the binding label to prevent
9741 it from colliding with another. */
9742 binding_label_gsym =
9743 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9744 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9745 binding_label_gsym->type = GSYM_COMMON;
9746 }
9747 else
9748 {
9749 /* If comm_name_gsym is NULL, the name common block is use
9750 associated and the name could be colliding. */
9751 if (binding_label_gsym->type != GSYM_COMMON)
9752 gfc_error ("Binding label '%s' for common block '%s' at %L "
9753 "collides with the global entity '%s' at %L",
9754 comm_block_tree->n.common->binding_label,
9755 comm_block_tree->n.common->name,
9756 &(comm_block_tree->n.common->where),
9757 binding_label_gsym->name,
9758 &(binding_label_gsym->where));
9759 else if (comm_name_gsym != NULL
9760 && (strcmp (binding_label_gsym->name,
9761 comm_name_gsym->binding_label) != 0)
9762 && (strcmp (binding_label_gsym->sym_name,
9763 comm_name_gsym->name) != 0))
9764 gfc_error ("Binding label '%s' for common block '%s' at %L "
9765 "collides with global entity '%s' at %L",
9766 binding_label_gsym->name, binding_label_gsym->sym_name,
9767 &(comm_block_tree->n.common->where),
9768 comm_name_gsym->name, &(comm_name_gsym->where));
9769 }
9770 }
9771
9772 return;
9773 }
9774
9775
9776 /* Verify any BIND(C) derived types in the namespace so we can report errors
9777 for them once, rather than for each variable declared of that type. */
9778
9779 static void
9780 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9781 {
9782 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9783 && derived_sym->attr.is_bind_c == 1)
9784 verify_bind_c_derived_type (derived_sym);
9785
9786 return;
9787 }
9788
9789
9790 /* Verify that any binding labels used in a given namespace do not collide
9791 with the names or binding labels of any global symbols. */
9792
9793 static void
9794 gfc_verify_binding_labels (gfc_symbol *sym)
9795 {
9796 int has_error = 0;
9797
9798 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9799 && sym->attr.flavor != FL_DERIVED && sym->binding_label)
9800 {
9801 gfc_gsymbol *bind_c_sym;
9802
9803 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9804 if (bind_c_sym != NULL
9805 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9806 {
9807 if (sym->attr.if_source == IFSRC_DECL
9808 && (bind_c_sym->type != GSYM_SUBROUTINE
9809 && bind_c_sym->type != GSYM_FUNCTION)
9810 && ((sym->attr.contained == 1
9811 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9812 || (sym->attr.use_assoc == 1
9813 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9814 {
9815 /* Make sure global procedures don't collide with anything. */
9816 gfc_error ("Binding label '%s' at %L collides with the global "
9817 "entity '%s' at %L", sym->binding_label,
9818 &(sym->declared_at), bind_c_sym->name,
9819 &(bind_c_sym->where));
9820 has_error = 1;
9821 }
9822 else if (sym->attr.contained == 0
9823 && (sym->attr.if_source == IFSRC_IFBODY
9824 && sym->attr.flavor == FL_PROCEDURE)
9825 && (bind_c_sym->sym_name != NULL
9826 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9827 {
9828 /* Make sure procedures in interface bodies don't collide. */
9829 gfc_error ("Binding label '%s' in interface body at %L collides "
9830 "with the global entity '%s' at %L",
9831 sym->binding_label,
9832 &(sym->declared_at), bind_c_sym->name,
9833 &(bind_c_sym->where));
9834 has_error = 1;
9835 }
9836 else if (sym->attr.contained == 0
9837 && sym->attr.if_source == IFSRC_UNKNOWN)
9838 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9839 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9840 || sym->attr.use_assoc == 0)
9841 {
9842 gfc_error ("Binding label '%s' at %L collides with global "
9843 "entity '%s' at %L", sym->binding_label,
9844 &(sym->declared_at), bind_c_sym->name,
9845 &(bind_c_sym->where));
9846 has_error = 1;
9847 }
9848
9849 if (has_error != 0)
9850 /* Clear the binding label to prevent checking multiple times. */
9851 sym->binding_label = NULL;
9852 }
9853 else if (bind_c_sym == NULL)
9854 {
9855 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9856 bind_c_sym->where = sym->declared_at;
9857 bind_c_sym->sym_name = sym->name;
9858
9859 if (sym->attr.use_assoc == 1)
9860 bind_c_sym->mod_name = sym->module;
9861 else
9862 if (sym->ns->proc_name != NULL)
9863 bind_c_sym->mod_name = sym->ns->proc_name->name;
9864
9865 if (sym->attr.contained == 0)
9866 {
9867 if (sym->attr.subroutine)
9868 bind_c_sym->type = GSYM_SUBROUTINE;
9869 else if (sym->attr.function)
9870 bind_c_sym->type = GSYM_FUNCTION;
9871 }
9872 }
9873 }
9874 return;
9875 }
9876
9877
9878 /* Resolve an index expression. */
9879
9880 static gfc_try
9881 resolve_index_expr (gfc_expr *e)
9882 {
9883 if (gfc_resolve_expr (e) == FAILURE)
9884 return FAILURE;
9885
9886 if (gfc_simplify_expr (e, 0) == FAILURE)
9887 return FAILURE;
9888
9889 if (gfc_specification_expr (e) == FAILURE)
9890 return FAILURE;
9891
9892 return SUCCESS;
9893 }
9894
9895
9896 /* Resolve a charlen structure. */
9897
9898 static gfc_try
9899 resolve_charlen (gfc_charlen *cl)
9900 {
9901 int i, k;
9902
9903 if (cl->resolved)
9904 return SUCCESS;
9905
9906 cl->resolved = 1;
9907
9908 specification_expr = 1;
9909
9910 if (resolve_index_expr (cl->length) == FAILURE)
9911 {
9912 specification_expr = 0;
9913 return FAILURE;
9914 }
9915
9916 /* "If the character length parameter value evaluates to a negative
9917 value, the length of character entities declared is zero." */
9918 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9919 {
9920 if (gfc_option.warn_surprising)
9921 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9922 " the length has been set to zero",
9923 &cl->length->where, i);
9924 gfc_replace_expr (cl->length,
9925 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9926 }
9927
9928 /* Check that the character length is not too large. */
9929 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9930 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9931 && cl->length->ts.type == BT_INTEGER
9932 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9933 {
9934 gfc_error ("String length at %L is too large", &cl->length->where);
9935 return FAILURE;
9936 }
9937
9938 return SUCCESS;
9939 }
9940
9941
9942 /* Test for non-constant shape arrays. */
9943
9944 static bool
9945 is_non_constant_shape_array (gfc_symbol *sym)
9946 {
9947 gfc_expr *e;
9948 int i;
9949 bool not_constant;
9950
9951 not_constant = false;
9952 if (sym->as != NULL)
9953 {
9954 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9955 has not been simplified; parameter array references. Do the
9956 simplification now. */
9957 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9958 {
9959 e = sym->as->lower[i];
9960 if (e && (resolve_index_expr (e) == FAILURE
9961 || !gfc_is_constant_expr (e)))
9962 not_constant = true;
9963 e = sym->as->upper[i];
9964 if (e && (resolve_index_expr (e) == FAILURE
9965 || !gfc_is_constant_expr (e)))
9966 not_constant = true;
9967 }
9968 }
9969 return not_constant;
9970 }
9971
9972 /* Given a symbol and an initialization expression, add code to initialize
9973 the symbol to the function entry. */
9974 static void
9975 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9976 {
9977 gfc_expr *lval;
9978 gfc_code *init_st;
9979 gfc_namespace *ns = sym->ns;
9980
9981 /* Search for the function namespace if this is a contained
9982 function without an explicit result. */
9983 if (sym->attr.function && sym == sym->result
9984 && sym->name != sym->ns->proc_name->name)
9985 {
9986 ns = ns->contained;
9987 for (;ns; ns = ns->sibling)
9988 if (strcmp (ns->proc_name->name, sym->name) == 0)
9989 break;
9990 }
9991
9992 if (ns == NULL)
9993 {
9994 gfc_free_expr (init);
9995 return;
9996 }
9997
9998 /* Build an l-value expression for the result. */
9999 lval = gfc_lval_expr_from_sym (sym);
10000
10001 /* Add the code at scope entry. */
10002 init_st = gfc_get_code ();
10003 init_st->next = ns->code;
10004 ns->code = init_st;
10005
10006 /* Assign the default initializer to the l-value. */
10007 init_st->loc = sym->declared_at;
10008 init_st->op = EXEC_INIT_ASSIGN;
10009 init_st->expr1 = lval;
10010 init_st->expr2 = init;
10011 }
10012
10013 /* Assign the default initializer to a derived type variable or result. */
10014
10015 static void
10016 apply_default_init (gfc_symbol *sym)
10017 {
10018 gfc_expr *init = NULL;
10019
10020 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10021 return;
10022
10023 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10024 init = gfc_default_initializer (&sym->ts);
10025
10026 if (init == NULL && sym->ts.type != BT_CLASS)
10027 return;
10028
10029 build_init_assign (sym, init);
10030 sym->attr.referenced = 1;
10031 }
10032
10033 /* Build an initializer for a local integer, real, complex, logical, or
10034 character variable, based on the command line flags finit-local-zero,
10035 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10036 null if the symbol should not have a default initialization. */
10037 static gfc_expr *
10038 build_default_init_expr (gfc_symbol *sym)
10039 {
10040 int char_len;
10041 gfc_expr *init_expr;
10042 int i;
10043
10044 /* These symbols should never have a default initialization. */
10045 if (sym->attr.allocatable
10046 || sym->attr.external
10047 || sym->attr.dummy
10048 || sym->attr.pointer
10049 || sym->attr.in_equivalence
10050 || sym->attr.in_common
10051 || sym->attr.data
10052 || sym->module
10053 || sym->attr.cray_pointee
10054 || sym->attr.cray_pointer)
10055 return NULL;
10056
10057 /* Now we'll try to build an initializer expression. */
10058 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10059 &sym->declared_at);
10060
10061 /* We will only initialize integers, reals, complex, logicals, and
10062 characters, and only if the corresponding command-line flags
10063 were set. Otherwise, we free init_expr and return null. */
10064 switch (sym->ts.type)
10065 {
10066 case BT_INTEGER:
10067 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10068 mpz_set_si (init_expr->value.integer,
10069 gfc_option.flag_init_integer_value);
10070 else
10071 {
10072 gfc_free_expr (init_expr);
10073 init_expr = NULL;
10074 }
10075 break;
10076
10077 case BT_REAL:
10078 switch (gfc_option.flag_init_real)
10079 {
10080 case GFC_INIT_REAL_SNAN:
10081 init_expr->is_snan = 1;
10082 /* Fall through. */
10083 case GFC_INIT_REAL_NAN:
10084 mpfr_set_nan (init_expr->value.real);
10085 break;
10086
10087 case GFC_INIT_REAL_INF:
10088 mpfr_set_inf (init_expr->value.real, 1);
10089 break;
10090
10091 case GFC_INIT_REAL_NEG_INF:
10092 mpfr_set_inf (init_expr->value.real, -1);
10093 break;
10094
10095 case GFC_INIT_REAL_ZERO:
10096 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10097 break;
10098
10099 default:
10100 gfc_free_expr (init_expr);
10101 init_expr = NULL;
10102 break;
10103 }
10104 break;
10105
10106 case BT_COMPLEX:
10107 switch (gfc_option.flag_init_real)
10108 {
10109 case GFC_INIT_REAL_SNAN:
10110 init_expr->is_snan = 1;
10111 /* Fall through. */
10112 case GFC_INIT_REAL_NAN:
10113 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10114 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10115 break;
10116
10117 case GFC_INIT_REAL_INF:
10118 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10119 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10120 break;
10121
10122 case GFC_INIT_REAL_NEG_INF:
10123 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10124 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10125 break;
10126
10127 case GFC_INIT_REAL_ZERO:
10128 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10129 break;
10130
10131 default:
10132 gfc_free_expr (init_expr);
10133 init_expr = NULL;
10134 break;
10135 }
10136 break;
10137
10138 case BT_LOGICAL:
10139 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10140 init_expr->value.logical = 0;
10141 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10142 init_expr->value.logical = 1;
10143 else
10144 {
10145 gfc_free_expr (init_expr);
10146 init_expr = NULL;
10147 }
10148 break;
10149
10150 case BT_CHARACTER:
10151 /* For characters, the length must be constant in order to
10152 create a default initializer. */
10153 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10154 && sym->ts.u.cl->length
10155 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10156 {
10157 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10158 init_expr->value.character.length = char_len;
10159 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10160 for (i = 0; i < char_len; i++)
10161 init_expr->value.character.string[i]
10162 = (unsigned char) gfc_option.flag_init_character_value;
10163 }
10164 else
10165 {
10166 gfc_free_expr (init_expr);
10167 init_expr = NULL;
10168 }
10169 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10170 && sym->ts.u.cl->length)
10171 {
10172 gfc_actual_arglist *arg;
10173 init_expr = gfc_get_expr ();
10174 init_expr->where = sym->declared_at;
10175 init_expr->ts = sym->ts;
10176 init_expr->expr_type = EXPR_FUNCTION;
10177 init_expr->value.function.isym =
10178 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10179 init_expr->value.function.name = "repeat";
10180 arg = gfc_get_actual_arglist ();
10181 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10182 NULL, 1);
10183 arg->expr->value.character.string[0]
10184 = gfc_option.flag_init_character_value;
10185 arg->next = gfc_get_actual_arglist ();
10186 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10187 init_expr->value.function.actual = arg;
10188 }
10189 break;
10190
10191 default:
10192 gfc_free_expr (init_expr);
10193 init_expr = NULL;
10194 }
10195 return init_expr;
10196 }
10197
10198 /* Add an initialization expression to a local variable. */
10199 static void
10200 apply_default_init_local (gfc_symbol *sym)
10201 {
10202 gfc_expr *init = NULL;
10203
10204 /* The symbol should be a variable or a function return value. */
10205 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10206 || (sym->attr.function && sym->result != sym))
10207 return;
10208
10209 /* Try to build the initializer expression. If we can't initialize
10210 this symbol, then init will be NULL. */
10211 init = build_default_init_expr (sym);
10212 if (init == NULL)
10213 return;
10214
10215 /* For saved variables, we don't want to add an initializer at function
10216 entry, so we just add a static initializer. Note that automatic variables
10217 are stack allocated even with -fno-automatic. */
10218 if (sym->attr.save || sym->ns->save_all
10219 || (gfc_option.flag_max_stack_var_size == 0
10220 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10221 {
10222 /* Don't clobber an existing initializer! */
10223 gcc_assert (sym->value == NULL);
10224 sym->value = init;
10225 return;
10226 }
10227
10228 build_init_assign (sym, init);
10229 }
10230
10231
10232 /* Resolution of common features of flavors variable and procedure. */
10233
10234 static gfc_try
10235 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10236 {
10237 gfc_array_spec *as;
10238
10239 /* Avoid double diagnostics for function result symbols. */
10240 if ((sym->result || sym->attr.result) && !sym->attr.dummy
10241 && (sym->ns != gfc_current_ns))
10242 return SUCCESS;
10243
10244 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10245 as = CLASS_DATA (sym)->as;
10246 else
10247 as = sym->as;
10248
10249 /* Constraints on deferred shape variable. */
10250 if (as == NULL || as->type != AS_DEFERRED)
10251 {
10252 bool pointer, allocatable, dimension;
10253
10254 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10255 {
10256 pointer = CLASS_DATA (sym)->attr.class_pointer;
10257 allocatable = CLASS_DATA (sym)->attr.allocatable;
10258 dimension = CLASS_DATA (sym)->attr.dimension;
10259 }
10260 else
10261 {
10262 pointer = sym->attr.pointer;
10263 allocatable = sym->attr.allocatable;
10264 dimension = sym->attr.dimension;
10265 }
10266
10267 if (allocatable)
10268 {
10269 if (dimension)
10270 {
10271 gfc_error ("Allocatable array '%s' at %L must have "
10272 "a deferred shape", sym->name, &sym->declared_at);
10273 return FAILURE;
10274 }
10275 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
10276 "may not be ALLOCATABLE", sym->name,
10277 &sym->declared_at) == FAILURE)
10278 return FAILURE;
10279 }
10280
10281 if (pointer && dimension)
10282 {
10283 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
10284 sym->name, &sym->declared_at);
10285 return FAILURE;
10286 }
10287 }
10288 else
10289 {
10290 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10291 && sym->ts.type != BT_CLASS && !sym->assoc)
10292 {
10293 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10294 sym->name, &sym->declared_at);
10295 return FAILURE;
10296 }
10297 }
10298
10299 /* Constraints on polymorphic variables. */
10300 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10301 {
10302 /* F03:C502. */
10303 if (sym->attr.class_ok
10304 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10305 {
10306 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10307 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10308 &sym->declared_at);
10309 return FAILURE;
10310 }
10311
10312 /* F03:C509. */
10313 /* Assume that use associated symbols were checked in the module ns.
10314 Class-variables that are associate-names are also something special
10315 and excepted from the test. */
10316 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10317 {
10318 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10319 "or pointer", sym->name, &sym->declared_at);
10320 return FAILURE;
10321 }
10322 }
10323
10324 return SUCCESS;
10325 }
10326
10327
10328 /* Additional checks for symbols with flavor variable and derived
10329 type. To be called from resolve_fl_variable. */
10330
10331 static gfc_try
10332 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10333 {
10334 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10335
10336 /* Check to see if a derived type is blocked from being host
10337 associated by the presence of another class I symbol in the same
10338 namespace. 14.6.1.3 of the standard and the discussion on
10339 comp.lang.fortran. */
10340 if (sym->ns != sym->ts.u.derived->ns
10341 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10342 {
10343 gfc_symbol *s;
10344 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10345 if (s && s->attr.generic)
10346 s = gfc_find_dt_in_generic (s);
10347 if (s && s->attr.flavor != FL_DERIVED)
10348 {
10349 gfc_error ("The type '%s' cannot be host associated at %L "
10350 "because it is blocked by an incompatible object "
10351 "of the same name declared at %L",
10352 sym->ts.u.derived->name, &sym->declared_at,
10353 &s->declared_at);
10354 return FAILURE;
10355 }
10356 }
10357
10358 /* 4th constraint in section 11.3: "If an object of a type for which
10359 component-initialization is specified (R429) appears in the
10360 specification-part of a module and does not have the ALLOCATABLE
10361 or POINTER attribute, the object shall have the SAVE attribute."
10362
10363 The check for initializers is performed with
10364 gfc_has_default_initializer because gfc_default_initializer generates
10365 a hidden default for allocatable components. */
10366 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10367 && sym->ns->proc_name->attr.flavor == FL_MODULE
10368 && !sym->ns->save_all && !sym->attr.save
10369 && !sym->attr.pointer && !sym->attr.allocatable
10370 && gfc_has_default_initializer (sym->ts.u.derived)
10371 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
10372 "module variable '%s' at %L, needed due to "
10373 "the default initialization", sym->name,
10374 &sym->declared_at) == FAILURE)
10375 return FAILURE;
10376
10377 /* Assign default initializer. */
10378 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10379 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10380 {
10381 sym->value = gfc_default_initializer (&sym->ts);
10382 }
10383
10384 return SUCCESS;
10385 }
10386
10387
10388 /* Resolve symbols with flavor variable. */
10389
10390 static gfc_try
10391 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10392 {
10393 int no_init_flag, automatic_flag;
10394 gfc_expr *e;
10395 const char *auto_save_msg;
10396
10397 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10398 "SAVE attribute";
10399
10400 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10401 return FAILURE;
10402
10403 /* Set this flag to check that variables are parameters of all entries.
10404 This check is effected by the call to gfc_resolve_expr through
10405 is_non_constant_shape_array. */
10406 specification_expr = 1;
10407
10408 if (sym->ns->proc_name
10409 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10410 || sym->ns->proc_name->attr.is_main_program)
10411 && !sym->attr.use_assoc
10412 && !sym->attr.allocatable
10413 && !sym->attr.pointer
10414 && is_non_constant_shape_array (sym))
10415 {
10416 /* The shape of a main program or module array needs to be
10417 constant. */
10418 gfc_error ("The module or main program array '%s' at %L must "
10419 "have constant shape", sym->name, &sym->declared_at);
10420 specification_expr = 0;
10421 return FAILURE;
10422 }
10423
10424 /* Constraints on deferred type parameter. */
10425 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10426 {
10427 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10428 "requires either the pointer or allocatable attribute",
10429 sym->name, &sym->declared_at);
10430 return FAILURE;
10431 }
10432
10433 if (sym->ts.type == BT_CHARACTER)
10434 {
10435 /* Make sure that character string variables with assumed length are
10436 dummy arguments. */
10437 e = sym->ts.u.cl->length;
10438 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10439 && !sym->ts.deferred)
10440 {
10441 gfc_error ("Entity with assumed character length at %L must be a "
10442 "dummy argument or a PARAMETER", &sym->declared_at);
10443 return FAILURE;
10444 }
10445
10446 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10447 {
10448 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10449 return FAILURE;
10450 }
10451
10452 if (!gfc_is_constant_expr (e)
10453 && !(e->expr_type == EXPR_VARIABLE
10454 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10455 {
10456 if (!sym->attr.use_assoc && sym->ns->proc_name
10457 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10458 || sym->ns->proc_name->attr.is_main_program))
10459 {
10460 gfc_error ("'%s' at %L must have constant character length "
10461 "in this context", sym->name, &sym->declared_at);
10462 return FAILURE;
10463 }
10464 if (sym->attr.in_common)
10465 {
10466 gfc_error ("COMMON variable '%s' at %L must have constant "
10467 "character length", sym->name, &sym->declared_at);
10468 return FAILURE;
10469 }
10470 }
10471 }
10472
10473 if (sym->value == NULL && sym->attr.referenced)
10474 apply_default_init_local (sym); /* Try to apply a default initialization. */
10475
10476 /* Determine if the symbol may not have an initializer. */
10477 no_init_flag = automatic_flag = 0;
10478 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10479 || sym->attr.intrinsic || sym->attr.result)
10480 no_init_flag = 1;
10481 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10482 && is_non_constant_shape_array (sym))
10483 {
10484 no_init_flag = automatic_flag = 1;
10485
10486 /* Also, they must not have the SAVE attribute.
10487 SAVE_IMPLICIT is checked below. */
10488 if (sym->as && sym->attr.codimension)
10489 {
10490 int corank = sym->as->corank;
10491 sym->as->corank = 0;
10492 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10493 sym->as->corank = corank;
10494 }
10495 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10496 {
10497 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10498 return FAILURE;
10499 }
10500 }
10501
10502 /* Ensure that any initializer is simplified. */
10503 if (sym->value)
10504 gfc_simplify_expr (sym->value, 1);
10505
10506 /* Reject illegal initializers. */
10507 if (!sym->mark && sym->value)
10508 {
10509 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10510 && CLASS_DATA (sym)->attr.allocatable))
10511 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10512 sym->name, &sym->declared_at);
10513 else if (sym->attr.external)
10514 gfc_error ("External '%s' at %L cannot have an initializer",
10515 sym->name, &sym->declared_at);
10516 else if (sym->attr.dummy
10517 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10518 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10519 sym->name, &sym->declared_at);
10520 else if (sym->attr.intrinsic)
10521 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10522 sym->name, &sym->declared_at);
10523 else if (sym->attr.result)
10524 gfc_error ("Function result '%s' at %L cannot have an initializer",
10525 sym->name, &sym->declared_at);
10526 else if (automatic_flag)
10527 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10528 sym->name, &sym->declared_at);
10529 else
10530 goto no_init_error;
10531 return FAILURE;
10532 }
10533
10534 no_init_error:
10535 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10536 return resolve_fl_variable_derived (sym, no_init_flag);
10537
10538 return SUCCESS;
10539 }
10540
10541
10542 /* Resolve a procedure. */
10543
10544 static gfc_try
10545 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10546 {
10547 gfc_formal_arglist *arg;
10548
10549 if (sym->attr.function
10550 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10551 return FAILURE;
10552
10553 if (sym->ts.type == BT_CHARACTER)
10554 {
10555 gfc_charlen *cl = sym->ts.u.cl;
10556
10557 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10558 && resolve_charlen (cl) == FAILURE)
10559 return FAILURE;
10560
10561 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10562 && sym->attr.proc == PROC_ST_FUNCTION)
10563 {
10564 gfc_error ("Character-valued statement function '%s' at %L must "
10565 "have constant length", sym->name, &sym->declared_at);
10566 return FAILURE;
10567 }
10568 }
10569
10570 /* Ensure that derived type for are not of a private type. Internal
10571 module procedures are excluded by 2.2.3.3 - i.e., they are not
10572 externally accessible and can access all the objects accessible in
10573 the host. */
10574 if (!(sym->ns->parent
10575 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10576 && gfc_check_symbol_access (sym))
10577 {
10578 gfc_interface *iface;
10579
10580 for (arg = sym->formal; arg; arg = arg->next)
10581 {
10582 if (arg->sym
10583 && arg->sym->ts.type == BT_DERIVED
10584 && !arg->sym->ts.u.derived->attr.use_assoc
10585 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10586 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
10587 "PRIVATE type and cannot be a dummy argument"
10588 " of '%s', which is PUBLIC at %L",
10589 arg->sym->name, sym->name, &sym->declared_at)
10590 == FAILURE)
10591 {
10592 /* Stop this message from recurring. */
10593 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10594 return FAILURE;
10595 }
10596 }
10597
10598 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10599 PRIVATE to the containing module. */
10600 for (iface = sym->generic; iface; iface = iface->next)
10601 {
10602 for (arg = iface->sym->formal; arg; arg = arg->next)
10603 {
10604 if (arg->sym
10605 && arg->sym->ts.type == BT_DERIVED
10606 && !arg->sym->ts.u.derived->attr.use_assoc
10607 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10608 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10609 "'%s' in PUBLIC interface '%s' at %L "
10610 "takes dummy arguments of '%s' which is "
10611 "PRIVATE", iface->sym->name, sym->name,
10612 &iface->sym->declared_at,
10613 gfc_typename (&arg->sym->ts)) == FAILURE)
10614 {
10615 /* Stop this message from recurring. */
10616 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10617 return FAILURE;
10618 }
10619 }
10620 }
10621
10622 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10623 PRIVATE to the containing module. */
10624 for (iface = sym->generic; iface; iface = iface->next)
10625 {
10626 for (arg = iface->sym->formal; arg; arg = arg->next)
10627 {
10628 if (arg->sym
10629 && arg->sym->ts.type == BT_DERIVED
10630 && !arg->sym->ts.u.derived->attr.use_assoc
10631 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10632 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10633 "'%s' in PUBLIC interface '%s' at %L "
10634 "takes dummy arguments of '%s' which is "
10635 "PRIVATE", iface->sym->name, sym->name,
10636 &iface->sym->declared_at,
10637 gfc_typename (&arg->sym->ts)) == FAILURE)
10638 {
10639 /* Stop this message from recurring. */
10640 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10641 return FAILURE;
10642 }
10643 }
10644 }
10645 }
10646
10647 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10648 && !sym->attr.proc_pointer)
10649 {
10650 gfc_error ("Function '%s' at %L cannot have an initializer",
10651 sym->name, &sym->declared_at);
10652 return FAILURE;
10653 }
10654
10655 /* An external symbol may not have an initializer because it is taken to be
10656 a procedure. Exception: Procedure Pointers. */
10657 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10658 {
10659 gfc_error ("External object '%s' at %L may not have an initializer",
10660 sym->name, &sym->declared_at);
10661 return FAILURE;
10662 }
10663
10664 /* An elemental function is required to return a scalar 12.7.1 */
10665 if (sym->attr.elemental && sym->attr.function && sym->as)
10666 {
10667 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10668 "result", sym->name, &sym->declared_at);
10669 /* Reset so that the error only occurs once. */
10670 sym->attr.elemental = 0;
10671 return FAILURE;
10672 }
10673
10674 if (sym->attr.proc == PROC_ST_FUNCTION
10675 && (sym->attr.allocatable || sym->attr.pointer))
10676 {
10677 gfc_error ("Statement function '%s' at %L may not have pointer or "
10678 "allocatable attribute", sym->name, &sym->declared_at);
10679 return FAILURE;
10680 }
10681
10682 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10683 char-len-param shall not be array-valued, pointer-valued, recursive
10684 or pure. ....snip... A character value of * may only be used in the
10685 following ways: (i) Dummy arg of procedure - dummy associates with
10686 actual length; (ii) To declare a named constant; or (iii) External
10687 function - but length must be declared in calling scoping unit. */
10688 if (sym->attr.function
10689 && sym->ts.type == BT_CHARACTER
10690 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10691 {
10692 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10693 || (sym->attr.recursive) || (sym->attr.pure))
10694 {
10695 if (sym->as && sym->as->rank)
10696 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10697 "array-valued", sym->name, &sym->declared_at);
10698
10699 if (sym->attr.pointer)
10700 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10701 "pointer-valued", sym->name, &sym->declared_at);
10702
10703 if (sym->attr.pure)
10704 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10705 "pure", sym->name, &sym->declared_at);
10706
10707 if (sym->attr.recursive)
10708 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10709 "recursive", sym->name, &sym->declared_at);
10710
10711 return FAILURE;
10712 }
10713
10714 /* Appendix B.2 of the standard. Contained functions give an
10715 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10716 character length is an F2003 feature. */
10717 if (!sym->attr.contained
10718 && gfc_current_form != FORM_FIXED
10719 && !sym->ts.deferred)
10720 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10721 "CHARACTER(*) function '%s' at %L",
10722 sym->name, &sym->declared_at);
10723 }
10724
10725 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10726 {
10727 gfc_formal_arglist *curr_arg;
10728 int has_non_interop_arg = 0;
10729
10730 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10731 sym->common_block) == FAILURE)
10732 {
10733 /* Clear these to prevent looking at them again if there was an
10734 error. */
10735 sym->attr.is_bind_c = 0;
10736 sym->attr.is_c_interop = 0;
10737 sym->ts.is_c_interop = 0;
10738 }
10739 else
10740 {
10741 /* So far, no errors have been found. */
10742 sym->attr.is_c_interop = 1;
10743 sym->ts.is_c_interop = 1;
10744 }
10745
10746 curr_arg = sym->formal;
10747 while (curr_arg != NULL)
10748 {
10749 /* Skip implicitly typed dummy args here. */
10750 if (curr_arg->sym->attr.implicit_type == 0)
10751 if (gfc_verify_c_interop_param (curr_arg->sym) == FAILURE)
10752 /* If something is found to fail, record the fact so we
10753 can mark the symbol for the procedure as not being
10754 BIND(C) to try and prevent multiple errors being
10755 reported. */
10756 has_non_interop_arg = 1;
10757
10758 curr_arg = curr_arg->next;
10759 }
10760
10761 /* See if any of the arguments were not interoperable and if so, clear
10762 the procedure symbol to prevent duplicate error messages. */
10763 if (has_non_interop_arg != 0)
10764 {
10765 sym->attr.is_c_interop = 0;
10766 sym->ts.is_c_interop = 0;
10767 sym->attr.is_bind_c = 0;
10768 }
10769 }
10770
10771 if (!sym->attr.proc_pointer)
10772 {
10773 if (sym->attr.save == SAVE_EXPLICIT)
10774 {
10775 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10776 "in '%s' at %L", sym->name, &sym->declared_at);
10777 return FAILURE;
10778 }
10779 if (sym->attr.intent)
10780 {
10781 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10782 "in '%s' at %L", sym->name, &sym->declared_at);
10783 return FAILURE;
10784 }
10785 if (sym->attr.subroutine && sym->attr.result)
10786 {
10787 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10788 "in '%s' at %L", sym->name, &sym->declared_at);
10789 return FAILURE;
10790 }
10791 if (sym->attr.external && sym->attr.function
10792 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10793 || sym->attr.contained))
10794 {
10795 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10796 "in '%s' at %L", sym->name, &sym->declared_at);
10797 return FAILURE;
10798 }
10799 if (strcmp ("ppr@", sym->name) == 0)
10800 {
10801 gfc_error ("Procedure pointer result '%s' at %L "
10802 "is missing the pointer attribute",
10803 sym->ns->proc_name->name, &sym->declared_at);
10804 return FAILURE;
10805 }
10806 }
10807
10808 return SUCCESS;
10809 }
10810
10811
10812 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10813 been defined and we now know their defined arguments, check that they fulfill
10814 the requirements of the standard for procedures used as finalizers. */
10815
10816 static gfc_try
10817 gfc_resolve_finalizers (gfc_symbol* derived)
10818 {
10819 gfc_finalizer* list;
10820 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10821 gfc_try result = SUCCESS;
10822 bool seen_scalar = false;
10823
10824 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10825 return SUCCESS;
10826
10827 /* Walk over the list of finalizer-procedures, check them, and if any one
10828 does not fit in with the standard's definition, print an error and remove
10829 it from the list. */
10830 prev_link = &derived->f2k_derived->finalizers;
10831 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10832 {
10833 gfc_symbol* arg;
10834 gfc_finalizer* i;
10835 int my_rank;
10836
10837 /* Skip this finalizer if we already resolved it. */
10838 if (list->proc_tree)
10839 {
10840 prev_link = &(list->next);
10841 continue;
10842 }
10843
10844 /* Check this exists and is a SUBROUTINE. */
10845 if (!list->proc_sym->attr.subroutine)
10846 {
10847 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10848 list->proc_sym->name, &list->where);
10849 goto error;
10850 }
10851
10852 /* We should have exactly one argument. */
10853 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10854 {
10855 gfc_error ("FINAL procedure at %L must have exactly one argument",
10856 &list->where);
10857 goto error;
10858 }
10859 arg = list->proc_sym->formal->sym;
10860
10861 /* This argument must be of our type. */
10862 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10863 {
10864 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10865 &arg->declared_at, derived->name);
10866 goto error;
10867 }
10868
10869 /* It must neither be a pointer nor allocatable nor optional. */
10870 if (arg->attr.pointer)
10871 {
10872 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10873 &arg->declared_at);
10874 goto error;
10875 }
10876 if (arg->attr.allocatable)
10877 {
10878 gfc_error ("Argument of FINAL procedure at %L must not be"
10879 " ALLOCATABLE", &arg->declared_at);
10880 goto error;
10881 }
10882 if (arg->attr.optional)
10883 {
10884 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10885 &arg->declared_at);
10886 goto error;
10887 }
10888
10889 /* It must not be INTENT(OUT). */
10890 if (arg->attr.intent == INTENT_OUT)
10891 {
10892 gfc_error ("Argument of FINAL procedure at %L must not be"
10893 " INTENT(OUT)", &arg->declared_at);
10894 goto error;
10895 }
10896
10897 /* Warn if the procedure is non-scalar and not assumed shape. */
10898 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10899 && arg->as->type != AS_ASSUMED_SHAPE)
10900 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10901 " shape argument", &arg->declared_at);
10902
10903 /* Check that it does not match in kind and rank with a FINAL procedure
10904 defined earlier. To really loop over the *earlier* declarations,
10905 we need to walk the tail of the list as new ones were pushed at the
10906 front. */
10907 /* TODO: Handle kind parameters once they are implemented. */
10908 my_rank = (arg->as ? arg->as->rank : 0);
10909 for (i = list->next; i; i = i->next)
10910 {
10911 /* Argument list might be empty; that is an error signalled earlier,
10912 but we nevertheless continued resolving. */
10913 if (i->proc_sym->formal)
10914 {
10915 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10916 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10917 if (i_rank == my_rank)
10918 {
10919 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10920 " rank (%d) as '%s'",
10921 list->proc_sym->name, &list->where, my_rank,
10922 i->proc_sym->name);
10923 goto error;
10924 }
10925 }
10926 }
10927
10928 /* Is this the/a scalar finalizer procedure? */
10929 if (!arg->as || arg->as->rank == 0)
10930 seen_scalar = true;
10931
10932 /* Find the symtree for this procedure. */
10933 gcc_assert (!list->proc_tree);
10934 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10935
10936 prev_link = &list->next;
10937 continue;
10938
10939 /* Remove wrong nodes immediately from the list so we don't risk any
10940 troubles in the future when they might fail later expectations. */
10941 error:
10942 result = FAILURE;
10943 i = list;
10944 *prev_link = list->next;
10945 gfc_free_finalizer (i);
10946 }
10947
10948 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10949 were nodes in the list, must have been for arrays. It is surely a good
10950 idea to have a scalar version there if there's something to finalize. */
10951 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10952 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10953 " defined at %L, suggest also scalar one",
10954 derived->name, &derived->declared_at);
10955
10956 /* TODO: Remove this error when finalization is finished. */
10957 gfc_error ("Finalization at %L is not yet implemented",
10958 &derived->declared_at);
10959
10960 return result;
10961 }
10962
10963
10964 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10965
10966 static gfc_try
10967 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10968 const char* generic_name, locus where)
10969 {
10970 gfc_symbol* sym1;
10971 gfc_symbol* sym2;
10972
10973 gcc_assert (t1->specific && t2->specific);
10974 gcc_assert (!t1->specific->is_generic);
10975 gcc_assert (!t2->specific->is_generic);
10976 gcc_assert (t1->is_operator == t2->is_operator);
10977
10978 sym1 = t1->specific->u.specific->n.sym;
10979 sym2 = t2->specific->u.specific->n.sym;
10980
10981 if (sym1 == sym2)
10982 return SUCCESS;
10983
10984 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10985 if (sym1->attr.subroutine != sym2->attr.subroutine
10986 || sym1->attr.function != sym2->attr.function)
10987 {
10988 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10989 " GENERIC '%s' at %L",
10990 sym1->name, sym2->name, generic_name, &where);
10991 return FAILURE;
10992 }
10993
10994 /* Compare the interfaces. */
10995 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
10996 NULL, 0))
10997 {
10998 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10999 sym1->name, sym2->name, generic_name, &where);
11000 return FAILURE;
11001 }
11002
11003 return SUCCESS;
11004 }
11005
11006
11007 /* Worker function for resolving a generic procedure binding; this is used to
11008 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11009
11010 The difference between those cases is finding possible inherited bindings
11011 that are overridden, as one has to look for them in tb_sym_root,
11012 tb_uop_root or tb_op, respectively. Thus the caller must already find
11013 the super-type and set p->overridden correctly. */
11014
11015 static gfc_try
11016 resolve_tb_generic_targets (gfc_symbol* super_type,
11017 gfc_typebound_proc* p, const char* name)
11018 {
11019 gfc_tbp_generic* target;
11020 gfc_symtree* first_target;
11021 gfc_symtree* inherited;
11022
11023 gcc_assert (p && p->is_generic);
11024
11025 /* Try to find the specific bindings for the symtrees in our target-list. */
11026 gcc_assert (p->u.generic);
11027 for (target = p->u.generic; target; target = target->next)
11028 if (!target->specific)
11029 {
11030 gfc_typebound_proc* overridden_tbp;
11031 gfc_tbp_generic* g;
11032 const char* target_name;
11033
11034 target_name = target->specific_st->name;
11035
11036 /* Defined for this type directly. */
11037 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11038 {
11039 target->specific = target->specific_st->n.tb;
11040 goto specific_found;
11041 }
11042
11043 /* Look for an inherited specific binding. */
11044 if (super_type)
11045 {
11046 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11047 true, NULL);
11048
11049 if (inherited)
11050 {
11051 gcc_assert (inherited->n.tb);
11052 target->specific = inherited->n.tb;
11053 goto specific_found;
11054 }
11055 }
11056
11057 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11058 " at %L", target_name, name, &p->where);
11059 return FAILURE;
11060
11061 /* Once we've found the specific binding, check it is not ambiguous with
11062 other specifics already found or inherited for the same GENERIC. */
11063 specific_found:
11064 gcc_assert (target->specific);
11065
11066 /* This must really be a specific binding! */
11067 if (target->specific->is_generic)
11068 {
11069 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11070 " '%s' is GENERIC, too", name, &p->where, target_name);
11071 return FAILURE;
11072 }
11073
11074 /* Check those already resolved on this type directly. */
11075 for (g = p->u.generic; g; g = g->next)
11076 if (g != target && g->specific
11077 && check_generic_tbp_ambiguity (target, g, name, p->where)
11078 == FAILURE)
11079 return FAILURE;
11080
11081 /* Check for ambiguity with inherited specific targets. */
11082 for (overridden_tbp = p->overridden; overridden_tbp;
11083 overridden_tbp = overridden_tbp->overridden)
11084 if (overridden_tbp->is_generic)
11085 {
11086 for (g = overridden_tbp->u.generic; g; g = g->next)
11087 {
11088 gcc_assert (g->specific);
11089 if (check_generic_tbp_ambiguity (target, g,
11090 name, p->where) == FAILURE)
11091 return FAILURE;
11092 }
11093 }
11094 }
11095
11096 /* If we attempt to "overwrite" a specific binding, this is an error. */
11097 if (p->overridden && !p->overridden->is_generic)
11098 {
11099 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11100 " the same name", name, &p->where);
11101 return FAILURE;
11102 }
11103
11104 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11105 all must have the same attributes here. */
11106 first_target = p->u.generic->specific->u.specific;
11107 gcc_assert (first_target);
11108 p->subroutine = first_target->n.sym->attr.subroutine;
11109 p->function = first_target->n.sym->attr.function;
11110
11111 return SUCCESS;
11112 }
11113
11114
11115 /* Resolve a GENERIC procedure binding for a derived type. */
11116
11117 static gfc_try
11118 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11119 {
11120 gfc_symbol* super_type;
11121
11122 /* Find the overridden binding if any. */
11123 st->n.tb->overridden = NULL;
11124 super_type = gfc_get_derived_super_type (derived);
11125 if (super_type)
11126 {
11127 gfc_symtree* overridden;
11128 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11129 true, NULL);
11130
11131 if (overridden && overridden->n.tb)
11132 st->n.tb->overridden = overridden->n.tb;
11133 }
11134
11135 /* Resolve using worker function. */
11136 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11137 }
11138
11139
11140 /* Retrieve the target-procedure of an operator binding and do some checks in
11141 common for intrinsic and user-defined type-bound operators. */
11142
11143 static gfc_symbol*
11144 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11145 {
11146 gfc_symbol* target_proc;
11147
11148 gcc_assert (target->specific && !target->specific->is_generic);
11149 target_proc = target->specific->u.specific->n.sym;
11150 gcc_assert (target_proc);
11151
11152 /* All operator bindings must have a passed-object dummy argument. */
11153 if (target->specific->nopass)
11154 {
11155 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11156 return NULL;
11157 }
11158
11159 return target_proc;
11160 }
11161
11162
11163 /* Resolve a type-bound intrinsic operator. */
11164
11165 static gfc_try
11166 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11167 gfc_typebound_proc* p)
11168 {
11169 gfc_symbol* super_type;
11170 gfc_tbp_generic* target;
11171
11172 /* If there's already an error here, do nothing (but don't fail again). */
11173 if (p->error)
11174 return SUCCESS;
11175
11176 /* Operators should always be GENERIC bindings. */
11177 gcc_assert (p->is_generic);
11178
11179 /* Look for an overridden binding. */
11180 super_type = gfc_get_derived_super_type (derived);
11181 if (super_type && super_type->f2k_derived)
11182 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11183 op, true, NULL);
11184 else
11185 p->overridden = NULL;
11186
11187 /* Resolve general GENERIC properties using worker function. */
11188 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
11189 goto error;
11190
11191 /* Check the targets to be procedures of correct interface. */
11192 for (target = p->u.generic; target; target = target->next)
11193 {
11194 gfc_symbol* target_proc;
11195
11196 target_proc = get_checked_tb_operator_target (target, p->where);
11197 if (!target_proc)
11198 goto error;
11199
11200 if (!gfc_check_operator_interface (target_proc, op, p->where))
11201 goto error;
11202 }
11203
11204 return SUCCESS;
11205
11206 error:
11207 p->error = 1;
11208 return FAILURE;
11209 }
11210
11211
11212 /* Resolve a type-bound user operator (tree-walker callback). */
11213
11214 static gfc_symbol* resolve_bindings_derived;
11215 static gfc_try resolve_bindings_result;
11216
11217 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
11218
11219 static void
11220 resolve_typebound_user_op (gfc_symtree* stree)
11221 {
11222 gfc_symbol* super_type;
11223 gfc_tbp_generic* target;
11224
11225 gcc_assert (stree && stree->n.tb);
11226
11227 if (stree->n.tb->error)
11228 return;
11229
11230 /* Operators should always be GENERIC bindings. */
11231 gcc_assert (stree->n.tb->is_generic);
11232
11233 /* Find overridden procedure, if any. */
11234 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11235 if (super_type && super_type->f2k_derived)
11236 {
11237 gfc_symtree* overridden;
11238 overridden = gfc_find_typebound_user_op (super_type, NULL,
11239 stree->name, true, NULL);
11240
11241 if (overridden && overridden->n.tb)
11242 stree->n.tb->overridden = overridden->n.tb;
11243 }
11244 else
11245 stree->n.tb->overridden = NULL;
11246
11247 /* Resolve basically using worker function. */
11248 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
11249 == FAILURE)
11250 goto error;
11251
11252 /* Check the targets to be functions of correct interface. */
11253 for (target = stree->n.tb->u.generic; target; target = target->next)
11254 {
11255 gfc_symbol* target_proc;
11256
11257 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11258 if (!target_proc)
11259 goto error;
11260
11261 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
11262 goto error;
11263 }
11264
11265 return;
11266
11267 error:
11268 resolve_bindings_result = FAILURE;
11269 stree->n.tb->error = 1;
11270 }
11271
11272
11273 /* Resolve the type-bound procedures for a derived type. */
11274
11275 static void
11276 resolve_typebound_procedure (gfc_symtree* stree)
11277 {
11278 gfc_symbol* proc;
11279 locus where;
11280 gfc_symbol* me_arg;
11281 gfc_symbol* super_type;
11282 gfc_component* comp;
11283
11284 gcc_assert (stree);
11285
11286 /* Undefined specific symbol from GENERIC target definition. */
11287 if (!stree->n.tb)
11288 return;
11289
11290 if (stree->n.tb->error)
11291 return;
11292
11293 /* If this is a GENERIC binding, use that routine. */
11294 if (stree->n.tb->is_generic)
11295 {
11296 if (resolve_typebound_generic (resolve_bindings_derived, stree)
11297 == FAILURE)
11298 goto error;
11299 return;
11300 }
11301
11302 /* Get the target-procedure to check it. */
11303 gcc_assert (!stree->n.tb->is_generic);
11304 gcc_assert (stree->n.tb->u.specific);
11305 proc = stree->n.tb->u.specific->n.sym;
11306 where = stree->n.tb->where;
11307 proc->attr.public_used = 1;
11308
11309 /* Default access should already be resolved from the parser. */
11310 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11311
11312 /* It should be a module procedure or an external procedure with explicit
11313 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11314 if ((!proc->attr.subroutine && !proc->attr.function)
11315 || (proc->attr.proc != PROC_MODULE
11316 && proc->attr.if_source != IFSRC_IFBODY)
11317 || (proc->attr.abstract && !stree->n.tb->deferred))
11318 {
11319 gfc_error ("'%s' must be a module procedure or an external procedure with"
11320 " an explicit interface at %L", proc->name, &where);
11321 goto error;
11322 }
11323 stree->n.tb->subroutine = proc->attr.subroutine;
11324 stree->n.tb->function = proc->attr.function;
11325
11326 /* Find the super-type of the current derived type. We could do this once and
11327 store in a global if speed is needed, but as long as not I believe this is
11328 more readable and clearer. */
11329 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11330
11331 /* If PASS, resolve and check arguments if not already resolved / loaded
11332 from a .mod file. */
11333 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11334 {
11335 if (stree->n.tb->pass_arg)
11336 {
11337 gfc_formal_arglist* i;
11338
11339 /* If an explicit passing argument name is given, walk the arg-list
11340 and look for it. */
11341
11342 me_arg = NULL;
11343 stree->n.tb->pass_arg_num = 1;
11344 for (i = proc->formal; i; i = i->next)
11345 {
11346 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11347 {
11348 me_arg = i->sym;
11349 break;
11350 }
11351 ++stree->n.tb->pass_arg_num;
11352 }
11353
11354 if (!me_arg)
11355 {
11356 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11357 " argument '%s'",
11358 proc->name, stree->n.tb->pass_arg, &where,
11359 stree->n.tb->pass_arg);
11360 goto error;
11361 }
11362 }
11363 else
11364 {
11365 /* Otherwise, take the first one; there should in fact be at least
11366 one. */
11367 stree->n.tb->pass_arg_num = 1;
11368 if (!proc->formal)
11369 {
11370 gfc_error ("Procedure '%s' with PASS at %L must have at"
11371 " least one argument", proc->name, &where);
11372 goto error;
11373 }
11374 me_arg = proc->formal->sym;
11375 }
11376
11377 /* Now check that the argument-type matches and the passed-object
11378 dummy argument is generally fine. */
11379
11380 gcc_assert (me_arg);
11381
11382 if (me_arg->ts.type != BT_CLASS)
11383 {
11384 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11385 " at %L", proc->name, &where);
11386 goto error;
11387 }
11388
11389 if (CLASS_DATA (me_arg)->ts.u.derived
11390 != resolve_bindings_derived)
11391 {
11392 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11393 " the derived-type '%s'", me_arg->name, proc->name,
11394 me_arg->name, &where, resolve_bindings_derived->name);
11395 goto error;
11396 }
11397
11398 gcc_assert (me_arg->ts.type == BT_CLASS);
11399 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
11400 {
11401 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11402 " scalar", proc->name, &where);
11403 goto error;
11404 }
11405 if (CLASS_DATA (me_arg)->attr.allocatable)
11406 {
11407 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11408 " be ALLOCATABLE", proc->name, &where);
11409 goto error;
11410 }
11411 if (CLASS_DATA (me_arg)->attr.class_pointer)
11412 {
11413 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11414 " be POINTER", proc->name, &where);
11415 goto error;
11416 }
11417 }
11418
11419 /* If we are extending some type, check that we don't override a procedure
11420 flagged NON_OVERRIDABLE. */
11421 stree->n.tb->overridden = NULL;
11422 if (super_type)
11423 {
11424 gfc_symtree* overridden;
11425 overridden = gfc_find_typebound_proc (super_type, NULL,
11426 stree->name, true, NULL);
11427
11428 if (overridden)
11429 {
11430 if (overridden->n.tb)
11431 stree->n.tb->overridden = overridden->n.tb;
11432
11433 if (gfc_check_typebound_override (stree, overridden) == FAILURE)
11434 goto error;
11435 }
11436 }
11437
11438 /* See if there's a name collision with a component directly in this type. */
11439 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11440 if (!strcmp (comp->name, stree->name))
11441 {
11442 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11443 " '%s'",
11444 stree->name, &where, resolve_bindings_derived->name);
11445 goto error;
11446 }
11447
11448 /* Try to find a name collision with an inherited component. */
11449 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11450 {
11451 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11452 " component of '%s'",
11453 stree->name, &where, resolve_bindings_derived->name);
11454 goto error;
11455 }
11456
11457 stree->n.tb->error = 0;
11458 return;
11459
11460 error:
11461 resolve_bindings_result = FAILURE;
11462 stree->n.tb->error = 1;
11463 }
11464
11465
11466 static gfc_try
11467 resolve_typebound_procedures (gfc_symbol* derived)
11468 {
11469 int op;
11470 gfc_symbol* super_type;
11471
11472 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11473 return SUCCESS;
11474
11475 super_type = gfc_get_derived_super_type (derived);
11476 if (super_type)
11477 resolve_typebound_procedures (super_type);
11478
11479 resolve_bindings_derived = derived;
11480 resolve_bindings_result = SUCCESS;
11481
11482 /* Make sure the vtab has been generated. */
11483 gfc_find_derived_vtab (derived);
11484
11485 if (derived->f2k_derived->tb_sym_root)
11486 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11487 &resolve_typebound_procedure);
11488
11489 if (derived->f2k_derived->tb_uop_root)
11490 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11491 &resolve_typebound_user_op);
11492
11493 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11494 {
11495 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11496 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11497 p) == FAILURE)
11498 resolve_bindings_result = FAILURE;
11499 }
11500
11501 return resolve_bindings_result;
11502 }
11503
11504
11505 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11506 to give all identical derived types the same backend_decl. */
11507 static void
11508 add_dt_to_dt_list (gfc_symbol *derived)
11509 {
11510 gfc_dt_list *dt_list;
11511
11512 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11513 if (derived == dt_list->derived)
11514 return;
11515
11516 dt_list = gfc_get_dt_list ();
11517 dt_list->next = gfc_derived_types;
11518 dt_list->derived = derived;
11519 gfc_derived_types = dt_list;
11520 }
11521
11522
11523 /* Ensure that a derived-type is really not abstract, meaning that every
11524 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11525
11526 static gfc_try
11527 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11528 {
11529 if (!st)
11530 return SUCCESS;
11531
11532 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11533 return FAILURE;
11534 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11535 return FAILURE;
11536
11537 if (st->n.tb && st->n.tb->deferred)
11538 {
11539 gfc_symtree* overriding;
11540 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11541 if (!overriding)
11542 return FAILURE;
11543 gcc_assert (overriding->n.tb);
11544 if (overriding->n.tb->deferred)
11545 {
11546 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11547 " '%s' is DEFERRED and not overridden",
11548 sub->name, &sub->declared_at, st->name);
11549 return FAILURE;
11550 }
11551 }
11552
11553 return SUCCESS;
11554 }
11555
11556 static gfc_try
11557 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11558 {
11559 /* The algorithm used here is to recursively travel up the ancestry of sub
11560 and for each ancestor-type, check all bindings. If any of them is
11561 DEFERRED, look it up starting from sub and see if the found (overriding)
11562 binding is not DEFERRED.
11563 This is not the most efficient way to do this, but it should be ok and is
11564 clearer than something sophisticated. */
11565
11566 gcc_assert (ancestor && !sub->attr.abstract);
11567
11568 if (!ancestor->attr.abstract)
11569 return SUCCESS;
11570
11571 /* Walk bindings of this ancestor. */
11572 if (ancestor->f2k_derived)
11573 {
11574 gfc_try t;
11575 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11576 if (t == FAILURE)
11577 return FAILURE;
11578 }
11579
11580 /* Find next ancestor type and recurse on it. */
11581 ancestor = gfc_get_derived_super_type (ancestor);
11582 if (ancestor)
11583 return ensure_not_abstract (sub, ancestor);
11584
11585 return SUCCESS;
11586 }
11587
11588
11589 /* Resolve the components of a derived type. This does not have to wait until
11590 resolution stage, but can be done as soon as the dt declaration has been
11591 parsed. */
11592
11593 static gfc_try
11594 resolve_fl_derived0 (gfc_symbol *sym)
11595 {
11596 gfc_symbol* super_type;
11597 gfc_component *c;
11598
11599 super_type = gfc_get_derived_super_type (sym);
11600
11601 /* F2008, C432. */
11602 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11603 {
11604 gfc_error ("As extending type '%s' at %L has a coarray component, "
11605 "parent type '%s' shall also have one", sym->name,
11606 &sym->declared_at, super_type->name);
11607 return FAILURE;
11608 }
11609
11610 /* Ensure the extended type gets resolved before we do. */
11611 if (super_type && resolve_fl_derived0 (super_type) == FAILURE)
11612 return FAILURE;
11613
11614 /* An ABSTRACT type must be extensible. */
11615 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11616 {
11617 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11618 sym->name, &sym->declared_at);
11619 return FAILURE;
11620 }
11621
11622 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
11623 : sym->components;
11624
11625 for ( ; c != NULL; c = c->next)
11626 {
11627 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
11628 if (c->ts.type == BT_CHARACTER && c->ts.deferred)
11629 {
11630 gfc_error ("Deferred-length character component '%s' at %L is not "
11631 "yet supported", c->name, &c->loc);
11632 return FAILURE;
11633 }
11634
11635 /* F2008, C442. */
11636 if ((!sym->attr.is_class || c != sym->components)
11637 && c->attr.codimension
11638 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11639 {
11640 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11641 "deferred shape", c->name, &c->loc);
11642 return FAILURE;
11643 }
11644
11645 /* F2008, C443. */
11646 if (c->attr.codimension && c->ts.type == BT_DERIVED
11647 && c->ts.u.derived->ts.is_iso_c)
11648 {
11649 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11650 "shall not be a coarray", c->name, &c->loc);
11651 return FAILURE;
11652 }
11653
11654 /* F2008, C444. */
11655 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11656 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11657 || c->attr.allocatable))
11658 {
11659 gfc_error ("Component '%s' at %L with coarray component "
11660 "shall be a nonpointer, nonallocatable scalar",
11661 c->name, &c->loc);
11662 return FAILURE;
11663 }
11664
11665 /* F2008, C448. */
11666 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11667 {
11668 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11669 "is not an array pointer", c->name, &c->loc);
11670 return FAILURE;
11671 }
11672
11673 if (c->attr.proc_pointer && c->ts.interface)
11674 {
11675 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11676 gfc_error ("Interface '%s', used by procedure pointer component "
11677 "'%s' at %L, is declared in a later PROCEDURE statement",
11678 c->ts.interface->name, c->name, &c->loc);
11679
11680 /* Get the attributes from the interface (now resolved). */
11681 if (c->ts.interface->attr.if_source
11682 || c->ts.interface->attr.intrinsic)
11683 {
11684 gfc_symbol *ifc = c->ts.interface;
11685
11686 if (ifc->formal && !ifc->formal_ns)
11687 resolve_symbol (ifc);
11688
11689 if (ifc->attr.intrinsic)
11690 resolve_intrinsic (ifc, &ifc->declared_at);
11691
11692 if (ifc->result)
11693 {
11694 c->ts = ifc->result->ts;
11695 c->attr.allocatable = ifc->result->attr.allocatable;
11696 c->attr.pointer = ifc->result->attr.pointer;
11697 c->attr.dimension = ifc->result->attr.dimension;
11698 c->as = gfc_copy_array_spec (ifc->result->as);
11699 }
11700 else
11701 {
11702 c->ts = ifc->ts;
11703 c->attr.allocatable = ifc->attr.allocatable;
11704 c->attr.pointer = ifc->attr.pointer;
11705 c->attr.dimension = ifc->attr.dimension;
11706 c->as = gfc_copy_array_spec (ifc->as);
11707 }
11708 c->ts.interface = ifc;
11709 c->attr.function = ifc->attr.function;
11710 c->attr.subroutine = ifc->attr.subroutine;
11711 gfc_copy_formal_args_ppc (c, ifc);
11712
11713 c->attr.pure = ifc->attr.pure;
11714 c->attr.elemental = ifc->attr.elemental;
11715 c->attr.recursive = ifc->attr.recursive;
11716 c->attr.always_explicit = ifc->attr.always_explicit;
11717 c->attr.ext_attr |= ifc->attr.ext_attr;
11718 /* Replace symbols in array spec. */
11719 if (c->as)
11720 {
11721 int i;
11722 for (i = 0; i < c->as->rank; i++)
11723 {
11724 gfc_expr_replace_comp (c->as->lower[i], c);
11725 gfc_expr_replace_comp (c->as->upper[i], c);
11726 }
11727 }
11728 /* Copy char length. */
11729 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11730 {
11731 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11732 gfc_expr_replace_comp (cl->length, c);
11733 if (cl->length && !cl->resolved
11734 && gfc_resolve_expr (cl->length) == FAILURE)
11735 return FAILURE;
11736 c->ts.u.cl = cl;
11737 }
11738 }
11739 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11740 {
11741 gfc_error ("Interface '%s' of procedure pointer component "
11742 "'%s' at %L must be explicit", c->ts.interface->name,
11743 c->name, &c->loc);
11744 return FAILURE;
11745 }
11746 }
11747 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11748 {
11749 /* Since PPCs are not implicitly typed, a PPC without an explicit
11750 interface must be a subroutine. */
11751 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11752 }
11753
11754 /* Procedure pointer components: Check PASS arg. */
11755 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11756 && !sym->attr.vtype)
11757 {
11758 gfc_symbol* me_arg;
11759
11760 if (c->tb->pass_arg)
11761 {
11762 gfc_formal_arglist* i;
11763
11764 /* If an explicit passing argument name is given, walk the arg-list
11765 and look for it. */
11766
11767 me_arg = NULL;
11768 c->tb->pass_arg_num = 1;
11769 for (i = c->formal; i; i = i->next)
11770 {
11771 if (!strcmp (i->sym->name, c->tb->pass_arg))
11772 {
11773 me_arg = i->sym;
11774 break;
11775 }
11776 c->tb->pass_arg_num++;
11777 }
11778
11779 if (!me_arg)
11780 {
11781 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11782 "at %L has no argument '%s'", c->name,
11783 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11784 c->tb->error = 1;
11785 return FAILURE;
11786 }
11787 }
11788 else
11789 {
11790 /* Otherwise, take the first one; there should in fact be at least
11791 one. */
11792 c->tb->pass_arg_num = 1;
11793 if (!c->formal)
11794 {
11795 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11796 "must have at least one argument",
11797 c->name, &c->loc);
11798 c->tb->error = 1;
11799 return FAILURE;
11800 }
11801 me_arg = c->formal->sym;
11802 }
11803
11804 /* Now check that the argument-type matches. */
11805 gcc_assert (me_arg);
11806 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11807 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11808 || (me_arg->ts.type == BT_CLASS
11809 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11810 {
11811 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11812 " the derived type '%s'", me_arg->name, c->name,
11813 me_arg->name, &c->loc, sym->name);
11814 c->tb->error = 1;
11815 return FAILURE;
11816 }
11817
11818 /* Check for C453. */
11819 if (me_arg->attr.dimension)
11820 {
11821 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11822 "must be scalar", me_arg->name, c->name, me_arg->name,
11823 &c->loc);
11824 c->tb->error = 1;
11825 return FAILURE;
11826 }
11827
11828 if (me_arg->attr.pointer)
11829 {
11830 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11831 "may not have the POINTER attribute", me_arg->name,
11832 c->name, me_arg->name, &c->loc);
11833 c->tb->error = 1;
11834 return FAILURE;
11835 }
11836
11837 if (me_arg->attr.allocatable)
11838 {
11839 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11840 "may not be ALLOCATABLE", me_arg->name, c->name,
11841 me_arg->name, &c->loc);
11842 c->tb->error = 1;
11843 return FAILURE;
11844 }
11845
11846 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11847 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11848 " at %L", c->name, &c->loc);
11849
11850 }
11851
11852 /* Check type-spec if this is not the parent-type component. */
11853 if (((sym->attr.is_class
11854 && (!sym->components->ts.u.derived->attr.extension
11855 || c != sym->components->ts.u.derived->components))
11856 || (!sym->attr.is_class
11857 && (!sym->attr.extension || c != sym->components)))
11858 && !sym->attr.vtype
11859 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11860 return FAILURE;
11861
11862 /* If this type is an extension, set the accessibility of the parent
11863 component. */
11864 if (super_type
11865 && ((sym->attr.is_class
11866 && c == sym->components->ts.u.derived->components)
11867 || (!sym->attr.is_class && c == sym->components))
11868 && strcmp (super_type->name, c->name) == 0)
11869 c->attr.access = super_type->attr.access;
11870
11871 /* If this type is an extension, see if this component has the same name
11872 as an inherited type-bound procedure. */
11873 if (super_type && !sym->attr.is_class
11874 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11875 {
11876 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11877 " inherited type-bound procedure",
11878 c->name, sym->name, &c->loc);
11879 return FAILURE;
11880 }
11881
11882 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
11883 && !c->ts.deferred)
11884 {
11885 if (c->ts.u.cl->length == NULL
11886 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11887 || !gfc_is_constant_expr (c->ts.u.cl->length))
11888 {
11889 gfc_error ("Character length of component '%s' needs to "
11890 "be a constant specification expression at %L",
11891 c->name,
11892 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11893 return FAILURE;
11894 }
11895 }
11896
11897 if (c->ts.type == BT_CHARACTER && c->ts.deferred
11898 && !c->attr.pointer && !c->attr.allocatable)
11899 {
11900 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11901 "length must be a POINTER or ALLOCATABLE",
11902 c->name, sym->name, &c->loc);
11903 return FAILURE;
11904 }
11905
11906 if (c->ts.type == BT_DERIVED
11907 && sym->component_access != ACCESS_PRIVATE
11908 && gfc_check_symbol_access (sym)
11909 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11910 && !c->ts.u.derived->attr.use_assoc
11911 && !gfc_check_symbol_access (c->ts.u.derived)
11912 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11913 "is a PRIVATE type and cannot be a component of "
11914 "'%s', which is PUBLIC at %L", c->name,
11915 sym->name, &sym->declared_at) == FAILURE)
11916 return FAILURE;
11917
11918 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
11919 {
11920 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11921 "type %s", c->name, &c->loc, sym->name);
11922 return FAILURE;
11923 }
11924
11925 if (sym->attr.sequence)
11926 {
11927 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11928 {
11929 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11930 "not have the SEQUENCE attribute",
11931 c->ts.u.derived->name, &sym->declared_at);
11932 return FAILURE;
11933 }
11934 }
11935
11936 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
11937 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
11938 else if (c->ts.type == BT_CLASS && c->attr.class_ok
11939 && CLASS_DATA (c)->ts.u.derived->attr.generic)
11940 CLASS_DATA (c)->ts.u.derived
11941 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
11942
11943 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11944 && c->attr.pointer && c->ts.u.derived->components == NULL
11945 && !c->ts.u.derived->attr.zero_comp)
11946 {
11947 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11948 "that has not been declared", c->name, sym->name,
11949 &c->loc);
11950 return FAILURE;
11951 }
11952
11953 if (c->ts.type == BT_CLASS && c->attr.class_ok
11954 && CLASS_DATA (c)->attr.class_pointer
11955 && CLASS_DATA (c)->ts.u.derived->components == NULL
11956 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11957 {
11958 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11959 "that has not been declared", c->name, sym->name,
11960 &c->loc);
11961 return FAILURE;
11962 }
11963
11964 /* C437. */
11965 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
11966 && (!c->attr.class_ok
11967 || !(CLASS_DATA (c)->attr.class_pointer
11968 || CLASS_DATA (c)->attr.allocatable)))
11969 {
11970 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11971 "or pointer", c->name, &c->loc);
11972 return FAILURE;
11973 }
11974
11975 /* Ensure that all the derived type components are put on the
11976 derived type list; even in formal namespaces, where derived type
11977 pointer components might not have been declared. */
11978 if (c->ts.type == BT_DERIVED
11979 && c->ts.u.derived
11980 && c->ts.u.derived->components
11981 && c->attr.pointer
11982 && sym != c->ts.u.derived)
11983 add_dt_to_dt_list (c->ts.u.derived);
11984
11985 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11986 || c->attr.proc_pointer
11987 || c->attr.allocatable)) == FAILURE)
11988 return FAILURE;
11989 }
11990
11991 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11992 all DEFERRED bindings are overridden. */
11993 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11994 && !sym->attr.is_class
11995 && ensure_not_abstract (sym, super_type) == FAILURE)
11996 return FAILURE;
11997
11998 /* Add derived type to the derived type list. */
11999 add_dt_to_dt_list (sym);
12000
12001 return SUCCESS;
12002 }
12003
12004
12005 /* The following procedure does the full resolution of a derived type,
12006 including resolution of all type-bound procedures (if present). In contrast
12007 to 'resolve_fl_derived0' this can only be done after the module has been
12008 parsed completely. */
12009
12010 static gfc_try
12011 resolve_fl_derived (gfc_symbol *sym)
12012 {
12013 gfc_symbol *gen_dt = NULL;
12014
12015 if (!sym->attr.is_class)
12016 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12017 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12018 && (!gen_dt->generic->sym->attr.use_assoc
12019 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12020 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Generic name '%s' of "
12021 "function '%s' at %L being the same name as derived "
12022 "type at %L", sym->name,
12023 gen_dt->generic->sym == sym
12024 ? gen_dt->generic->next->sym->name
12025 : gen_dt->generic->sym->name,
12026 gen_dt->generic->sym == sym
12027 ? &gen_dt->generic->next->sym->declared_at
12028 : &gen_dt->generic->sym->declared_at,
12029 &sym->declared_at) == FAILURE)
12030 return FAILURE;
12031
12032 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12033 {
12034 /* Fix up incomplete CLASS symbols. */
12035 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12036 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12037 if (vptr->ts.u.derived == NULL)
12038 {
12039 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12040 gcc_assert (vtab);
12041 vptr->ts.u.derived = vtab->ts.u.derived;
12042 }
12043 }
12044
12045 if (resolve_fl_derived0 (sym) == FAILURE)
12046 return FAILURE;
12047
12048 /* Resolve the type-bound procedures. */
12049 if (resolve_typebound_procedures (sym) == FAILURE)
12050 return FAILURE;
12051
12052 /* Resolve the finalizer procedures. */
12053 if (gfc_resolve_finalizers (sym) == FAILURE)
12054 return FAILURE;
12055
12056 return SUCCESS;
12057 }
12058
12059
12060 static gfc_try
12061 resolve_fl_namelist (gfc_symbol *sym)
12062 {
12063 gfc_namelist *nl;
12064 gfc_symbol *nlsym;
12065
12066 for (nl = sym->namelist; nl; nl = nl->next)
12067 {
12068 /* Check again, the check in match only works if NAMELIST comes
12069 after the decl. */
12070 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12071 {
12072 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12073 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12074 return FAILURE;
12075 }
12076
12077 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12078 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
12079 "object '%s' with assumed shape in namelist "
12080 "'%s' at %L", nl->sym->name, sym->name,
12081 &sym->declared_at) == FAILURE)
12082 return FAILURE;
12083
12084 if (is_non_constant_shape_array (nl->sym)
12085 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
12086 "object '%s' with nonconstant shape in namelist "
12087 "'%s' at %L", nl->sym->name, sym->name,
12088 &sym->declared_at) == FAILURE)
12089 return FAILURE;
12090
12091 if (nl->sym->ts.type == BT_CHARACTER
12092 && (nl->sym->ts.u.cl->length == NULL
12093 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12094 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
12095 "'%s' with nonconstant character length in "
12096 "namelist '%s' at %L", nl->sym->name, sym->name,
12097 &sym->declared_at) == FAILURE)
12098 return FAILURE;
12099
12100 /* FIXME: Once UDDTIO is implemented, the following can be
12101 removed. */
12102 if (nl->sym->ts.type == BT_CLASS)
12103 {
12104 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12105 "polymorphic and requires a defined input/output "
12106 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12107 return FAILURE;
12108 }
12109
12110 if (nl->sym->ts.type == BT_DERIVED
12111 && (nl->sym->ts.u.derived->attr.alloc_comp
12112 || nl->sym->ts.u.derived->attr.pointer_comp))
12113 {
12114 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
12115 "'%s' in namelist '%s' at %L with ALLOCATABLE "
12116 "or POINTER components", nl->sym->name,
12117 sym->name, &sym->declared_at) == FAILURE)
12118 return FAILURE;
12119
12120 /* FIXME: Once UDDTIO is implemented, the following can be
12121 removed. */
12122 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12123 "ALLOCATABLE or POINTER components and thus requires "
12124 "a defined input/output procedure", nl->sym->name,
12125 sym->name, &sym->declared_at);
12126 return FAILURE;
12127 }
12128 }
12129
12130 /* Reject PRIVATE objects in a PUBLIC namelist. */
12131 if (gfc_check_symbol_access (sym))
12132 {
12133 for (nl = sym->namelist; nl; nl = nl->next)
12134 {
12135 if (!nl->sym->attr.use_assoc
12136 && !is_sym_host_assoc (nl->sym, sym->ns)
12137 && !gfc_check_symbol_access (nl->sym))
12138 {
12139 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12140 "cannot be member of PUBLIC namelist '%s' at %L",
12141 nl->sym->name, sym->name, &sym->declared_at);
12142 return FAILURE;
12143 }
12144
12145 /* Types with private components that came here by USE-association. */
12146 if (nl->sym->ts.type == BT_DERIVED
12147 && derived_inaccessible (nl->sym->ts.u.derived))
12148 {
12149 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12150 "components and cannot be member of namelist '%s' at %L",
12151 nl->sym->name, sym->name, &sym->declared_at);
12152 return FAILURE;
12153 }
12154
12155 /* Types with private components that are defined in the same module. */
12156 if (nl->sym->ts.type == BT_DERIVED
12157 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12158 && nl->sym->ts.u.derived->attr.private_comp)
12159 {
12160 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12161 "cannot be a member of PUBLIC namelist '%s' at %L",
12162 nl->sym->name, sym->name, &sym->declared_at);
12163 return FAILURE;
12164 }
12165 }
12166 }
12167
12168
12169 /* 14.1.2 A module or internal procedure represent local entities
12170 of the same type as a namelist member and so are not allowed. */
12171 for (nl = sym->namelist; nl; nl = nl->next)
12172 {
12173 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12174 continue;
12175
12176 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12177 if ((nl->sym == sym->ns->proc_name)
12178 ||
12179 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12180 continue;
12181
12182 nlsym = NULL;
12183 if (nl->sym && nl->sym->name)
12184 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12185 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12186 {
12187 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12188 "attribute in '%s' at %L", nlsym->name,
12189 &sym->declared_at);
12190 return FAILURE;
12191 }
12192 }
12193
12194 return SUCCESS;
12195 }
12196
12197
12198 static gfc_try
12199 resolve_fl_parameter (gfc_symbol *sym)
12200 {
12201 /* A parameter array's shape needs to be constant. */
12202 if (sym->as != NULL
12203 && (sym->as->type == AS_DEFERRED
12204 || is_non_constant_shape_array (sym)))
12205 {
12206 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12207 "or of deferred shape", sym->name, &sym->declared_at);
12208 return FAILURE;
12209 }
12210
12211 /* Make sure a parameter that has been implicitly typed still
12212 matches the implicit type, since PARAMETER statements can precede
12213 IMPLICIT statements. */
12214 if (sym->attr.implicit_type
12215 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12216 sym->ns)))
12217 {
12218 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12219 "later IMPLICIT type", sym->name, &sym->declared_at);
12220 return FAILURE;
12221 }
12222
12223 /* Make sure the types of derived parameters are consistent. This
12224 type checking is deferred until resolution because the type may
12225 refer to a derived type from the host. */
12226 if (sym->ts.type == BT_DERIVED
12227 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12228 {
12229 gfc_error ("Incompatible derived type in PARAMETER at %L",
12230 &sym->value->where);
12231 return FAILURE;
12232 }
12233 return SUCCESS;
12234 }
12235
12236
12237 /* Do anything necessary to resolve a symbol. Right now, we just
12238 assume that an otherwise unknown symbol is a variable. This sort
12239 of thing commonly happens for symbols in module. */
12240
12241 static void
12242 resolve_symbol (gfc_symbol *sym)
12243 {
12244 int check_constant, mp_flag;
12245 gfc_symtree *symtree;
12246 gfc_symtree *this_symtree;
12247 gfc_namespace *ns;
12248 gfc_component *c;
12249 symbol_attribute class_attr;
12250 gfc_array_spec *as;
12251
12252 if (sym->attr.flavor == FL_UNKNOWN
12253 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12254 && !sym->attr.generic && !sym->attr.external
12255 && sym->attr.if_source == IFSRC_UNKNOWN))
12256 {
12257
12258 /* If we find that a flavorless symbol is an interface in one of the
12259 parent namespaces, find its symtree in this namespace, free the
12260 symbol and set the symtree to point to the interface symbol. */
12261 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12262 {
12263 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12264 if (symtree && (symtree->n.sym->generic ||
12265 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12266 && sym->ns->construct_entities)))
12267 {
12268 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12269 sym->name);
12270 gfc_release_symbol (sym);
12271 symtree->n.sym->refs++;
12272 this_symtree->n.sym = symtree->n.sym;
12273 return;
12274 }
12275 }
12276
12277 /* Otherwise give it a flavor according to such attributes as
12278 it has. */
12279 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12280 && sym->attr.intrinsic == 0)
12281 sym->attr.flavor = FL_VARIABLE;
12282 else if (sym->attr.flavor == FL_UNKNOWN)
12283 {
12284 sym->attr.flavor = FL_PROCEDURE;
12285 if (sym->attr.dimension)
12286 sym->attr.function = 1;
12287 }
12288 }
12289
12290 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12291 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12292
12293 if (sym->attr.procedure && sym->ts.interface
12294 && sym->attr.if_source != IFSRC_DECL
12295 && resolve_procedure_interface (sym) == FAILURE)
12296 return;
12297
12298 if (sym->attr.is_protected && !sym->attr.proc_pointer
12299 && (sym->attr.procedure || sym->attr.external))
12300 {
12301 if (sym->attr.external)
12302 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12303 "at %L", &sym->declared_at);
12304 else
12305 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12306 "at %L", &sym->declared_at);
12307
12308 return;
12309 }
12310
12311 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
12312 return;
12313
12314 /* Symbols that are module procedures with results (functions) have
12315 the types and array specification copied for type checking in
12316 procedures that call them, as well as for saving to a module
12317 file. These symbols can't stand the scrutiny that their results
12318 can. */
12319 mp_flag = (sym->result != NULL && sym->result != sym);
12320
12321 /* Make sure that the intrinsic is consistent with its internal
12322 representation. This needs to be done before assigning a default
12323 type to avoid spurious warnings. */
12324 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12325 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
12326 return;
12327
12328 /* Resolve associate names. */
12329 if (sym->assoc)
12330 resolve_assoc_var (sym, true);
12331
12332 /* Assign default type to symbols that need one and don't have one. */
12333 if (sym->ts.type == BT_UNKNOWN)
12334 {
12335 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12336 {
12337 gfc_set_default_type (sym, 1, NULL);
12338 }
12339
12340 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12341 && !sym->attr.function && !sym->attr.subroutine
12342 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12343 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12344
12345 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12346 {
12347 /* The specific case of an external procedure should emit an error
12348 in the case that there is no implicit type. */
12349 if (!mp_flag)
12350 gfc_set_default_type (sym, sym->attr.external, NULL);
12351 else
12352 {
12353 /* Result may be in another namespace. */
12354 resolve_symbol (sym->result);
12355
12356 if (!sym->result->attr.proc_pointer)
12357 {
12358 sym->ts = sym->result->ts;
12359 sym->as = gfc_copy_array_spec (sym->result->as);
12360 sym->attr.dimension = sym->result->attr.dimension;
12361 sym->attr.pointer = sym->result->attr.pointer;
12362 sym->attr.allocatable = sym->result->attr.allocatable;
12363 sym->attr.contiguous = sym->result->attr.contiguous;
12364 }
12365 }
12366 }
12367 }
12368 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12369 gfc_resolve_array_spec (sym->result->as, false);
12370
12371 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12372 {
12373 as = CLASS_DATA (sym)->as;
12374 class_attr = CLASS_DATA (sym)->attr;
12375 class_attr.pointer = class_attr.class_pointer;
12376 }
12377 else
12378 {
12379 class_attr = sym->attr;
12380 as = sym->as;
12381 }
12382
12383 /* F2008, C530. */
12384 if (sym->attr.contiguous
12385 && (!class_attr.dimension
12386 || (as->type != AS_ASSUMED_SHAPE && !class_attr.pointer)))
12387 {
12388 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12389 "array pointer or an assumed-shape array", sym->name,
12390 &sym->declared_at);
12391 return;
12392 }
12393
12394 /* Assumed size arrays and assumed shape arrays must be dummy
12395 arguments. Array-spec's of implied-shape should have been resolved to
12396 AS_EXPLICIT already. */
12397
12398 if (as)
12399 {
12400 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12401 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12402 || as->type == AS_ASSUMED_SHAPE)
12403 && sym->attr.dummy == 0)
12404 {
12405 if (as->type == AS_ASSUMED_SIZE)
12406 gfc_error ("Assumed size array at %L must be a dummy argument",
12407 &sym->declared_at);
12408 else
12409 gfc_error ("Assumed shape array at %L must be a dummy argument",
12410 &sym->declared_at);
12411 return;
12412 }
12413 }
12414
12415 /* Make sure symbols with known intent or optional are really dummy
12416 variable. Because of ENTRY statement, this has to be deferred
12417 until resolution time. */
12418
12419 if (!sym->attr.dummy
12420 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12421 {
12422 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12423 return;
12424 }
12425
12426 if (sym->attr.value && !sym->attr.dummy)
12427 {
12428 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12429 "it is not a dummy argument", sym->name, &sym->declared_at);
12430 return;
12431 }
12432
12433 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12434 {
12435 gfc_charlen *cl = sym->ts.u.cl;
12436 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12437 {
12438 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12439 "attribute must have constant length",
12440 sym->name, &sym->declared_at);
12441 return;
12442 }
12443
12444 if (sym->ts.is_c_interop
12445 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12446 {
12447 gfc_error ("C interoperable character dummy variable '%s' at %L "
12448 "with VALUE attribute must have length one",
12449 sym->name, &sym->declared_at);
12450 return;
12451 }
12452 }
12453
12454 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12455 && sym->ts.u.derived->attr.generic)
12456 {
12457 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12458 if (!sym->ts.u.derived)
12459 {
12460 gfc_error ("The derived type '%s' at %L is of type '%s', "
12461 "which has not been defined", sym->name,
12462 &sym->declared_at, sym->ts.u.derived->name);
12463 sym->ts.type = BT_UNKNOWN;
12464 return;
12465 }
12466 }
12467
12468 if (sym->ts.type == BT_ASSUMED)
12469 {
12470 /* TS 29113, C407a. */
12471 if (!sym->attr.dummy)
12472 {
12473 gfc_error ("Assumed type of variable %s at %L is only permitted "
12474 "for dummy variables", sym->name, &sym->declared_at);
12475 return;
12476 }
12477 if (sym->attr.allocatable || sym->attr.codimension
12478 || sym->attr.pointer || sym->attr.value)
12479 {
12480 gfc_error ("Assumed-type variable %s at %L may not have the "
12481 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
12482 sym->name, &sym->declared_at);
12483 return;
12484 }
12485 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
12486 {
12487 gfc_error ("Assumed-type variable %s at %L shall not be an "
12488 "explicit-shape array", sym->name, &sym->declared_at);
12489 return;
12490 }
12491 }
12492
12493 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12494 do this for something that was implicitly typed because that is handled
12495 in gfc_set_default_type. Handle dummy arguments and procedure
12496 definitions separately. Also, anything that is use associated is not
12497 handled here but instead is handled in the module it is declared in.
12498 Finally, derived type definitions are allowed to be BIND(C) since that
12499 only implies that they're interoperable, and they are checked fully for
12500 interoperability when a variable is declared of that type. */
12501 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12502 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12503 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12504 {
12505 gfc_try t = SUCCESS;
12506
12507 /* First, make sure the variable is declared at the
12508 module-level scope (J3/04-007, Section 15.3). */
12509 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12510 sym->attr.in_common == 0)
12511 {
12512 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12513 "is neither a COMMON block nor declared at the "
12514 "module level scope", sym->name, &(sym->declared_at));
12515 t = FAILURE;
12516 }
12517 else if (sym->common_head != NULL)
12518 {
12519 t = verify_com_block_vars_c_interop (sym->common_head);
12520 }
12521 else
12522 {
12523 /* If type() declaration, we need to verify that the components
12524 of the given type are all C interoperable, etc. */
12525 if (sym->ts.type == BT_DERIVED &&
12526 sym->ts.u.derived->attr.is_c_interop != 1)
12527 {
12528 /* Make sure the user marked the derived type as BIND(C). If
12529 not, call the verify routine. This could print an error
12530 for the derived type more than once if multiple variables
12531 of that type are declared. */
12532 if (sym->ts.u.derived->attr.is_bind_c != 1)
12533 verify_bind_c_derived_type (sym->ts.u.derived);
12534 t = FAILURE;
12535 }
12536
12537 /* Verify the variable itself as C interoperable if it
12538 is BIND(C). It is not possible for this to succeed if
12539 the verify_bind_c_derived_type failed, so don't have to handle
12540 any error returned by verify_bind_c_derived_type. */
12541 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12542 sym->common_block);
12543 }
12544
12545 if (t == FAILURE)
12546 {
12547 /* clear the is_bind_c flag to prevent reporting errors more than
12548 once if something failed. */
12549 sym->attr.is_bind_c = 0;
12550 return;
12551 }
12552 }
12553
12554 /* If a derived type symbol has reached this point, without its
12555 type being declared, we have an error. Notice that most
12556 conditions that produce undefined derived types have already
12557 been dealt with. However, the likes of:
12558 implicit type(t) (t) ..... call foo (t) will get us here if
12559 the type is not declared in the scope of the implicit
12560 statement. Change the type to BT_UNKNOWN, both because it is so
12561 and to prevent an ICE. */
12562 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12563 && sym->ts.u.derived->components == NULL
12564 && !sym->ts.u.derived->attr.zero_comp)
12565 {
12566 gfc_error ("The derived type '%s' at %L is of type '%s', "
12567 "which has not been defined", sym->name,
12568 &sym->declared_at, sym->ts.u.derived->name);
12569 sym->ts.type = BT_UNKNOWN;
12570 return;
12571 }
12572
12573 /* Make sure that the derived type has been resolved and that the
12574 derived type is visible in the symbol's namespace, if it is a
12575 module function and is not PRIVATE. */
12576 if (sym->ts.type == BT_DERIVED
12577 && sym->ts.u.derived->attr.use_assoc
12578 && sym->ns->proc_name
12579 && sym->ns->proc_name->attr.flavor == FL_MODULE
12580 && resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12581 return;
12582
12583 /* Unless the derived-type declaration is use associated, Fortran 95
12584 does not allow public entries of private derived types.
12585 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12586 161 in 95-006r3. */
12587 if (sym->ts.type == BT_DERIVED
12588 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12589 && !sym->ts.u.derived->attr.use_assoc
12590 && gfc_check_symbol_access (sym)
12591 && !gfc_check_symbol_access (sym->ts.u.derived)
12592 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12593 "of PRIVATE derived type '%s'",
12594 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12595 : "variable", sym->name, &sym->declared_at,
12596 sym->ts.u.derived->name) == FAILURE)
12597 return;
12598
12599 /* F2008, C1302. */
12600 if (sym->ts.type == BT_DERIVED
12601 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
12602 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
12603 || sym->ts.u.derived->attr.lock_comp)
12604 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
12605 {
12606 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
12607 "type LOCK_TYPE must be a coarray", sym->name,
12608 &sym->declared_at);
12609 return;
12610 }
12611
12612 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12613 default initialization is defined (5.1.2.4.4). */
12614 if (sym->ts.type == BT_DERIVED
12615 && sym->attr.dummy
12616 && sym->attr.intent == INTENT_OUT
12617 && sym->as
12618 && sym->as->type == AS_ASSUMED_SIZE)
12619 {
12620 for (c = sym->ts.u.derived->components; c; c = c->next)
12621 {
12622 if (c->initializer)
12623 {
12624 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12625 "ASSUMED SIZE and so cannot have a default initializer",
12626 sym->name, &sym->declared_at);
12627 return;
12628 }
12629 }
12630 }
12631
12632 /* F2008, C542. */
12633 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
12634 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
12635 {
12636 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
12637 "INTENT(OUT)", sym->name, &sym->declared_at);
12638 return;
12639 }
12640
12641 /* F2008, C525. */
12642 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12643 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12644 && CLASS_DATA (sym)->attr.coarray_comp))
12645 || class_attr.codimension)
12646 && (sym->attr.result || sym->result == sym))
12647 {
12648 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12649 "a coarray component", sym->name, &sym->declared_at);
12650 return;
12651 }
12652
12653 /* F2008, C524. */
12654 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12655 && sym->ts.u.derived->ts.is_iso_c)
12656 {
12657 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12658 "shall not be a coarray", sym->name, &sym->declared_at);
12659 return;
12660 }
12661
12662 /* F2008, C525. */
12663 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12664 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12665 && CLASS_DATA (sym)->attr.coarray_comp))
12666 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
12667 || class_attr.allocatable))
12668 {
12669 gfc_error ("Variable '%s' at %L with coarray component "
12670 "shall be a nonpointer, nonallocatable scalar",
12671 sym->name, &sym->declared_at);
12672 return;
12673 }
12674
12675 /* F2008, C526. The function-result case was handled above. */
12676 if (class_attr.codimension
12677 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
12678 || sym->attr.select_type_temporary
12679 || sym->ns->save_all
12680 || sym->ns->proc_name->attr.flavor == FL_MODULE
12681 || sym->ns->proc_name->attr.is_main_program
12682 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12683 {
12684 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
12685 "nor a dummy argument", sym->name, &sym->declared_at);
12686 return;
12687 }
12688 /* F2008, C528. */
12689 else if (class_attr.codimension && !sym->attr.select_type_temporary
12690 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
12691 {
12692 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12693 "deferred shape", sym->name, &sym->declared_at);
12694 return;
12695 }
12696 else if (class_attr.codimension && class_attr.allocatable && as
12697 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
12698 {
12699 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12700 "deferred shape", sym->name, &sym->declared_at);
12701 return;
12702 }
12703
12704 /* F2008, C541. */
12705 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12706 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12707 && CLASS_DATA (sym)->attr.coarray_comp))
12708 || (class_attr.codimension && class_attr.allocatable))
12709 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12710 {
12711 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12712 "allocatable coarray or have coarray components",
12713 sym->name, &sym->declared_at);
12714 return;
12715 }
12716
12717 if (class_attr.codimension && sym->attr.dummy
12718 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12719 {
12720 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12721 "procedure '%s'", sym->name, &sym->declared_at,
12722 sym->ns->proc_name->name);
12723 return;
12724 }
12725
12726 switch (sym->attr.flavor)
12727 {
12728 case FL_VARIABLE:
12729 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12730 return;
12731 break;
12732
12733 case FL_PROCEDURE:
12734 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12735 return;
12736 break;
12737
12738 case FL_NAMELIST:
12739 if (resolve_fl_namelist (sym) == FAILURE)
12740 return;
12741 break;
12742
12743 case FL_PARAMETER:
12744 if (resolve_fl_parameter (sym) == FAILURE)
12745 return;
12746 break;
12747
12748 default:
12749 break;
12750 }
12751
12752 /* Resolve array specifier. Check as well some constraints
12753 on COMMON blocks. */
12754
12755 check_constant = sym->attr.in_common && !sym->attr.pointer;
12756
12757 /* Set the formal_arg_flag so that check_conflict will not throw
12758 an error for host associated variables in the specification
12759 expression for an array_valued function. */
12760 if (sym->attr.function && sym->as)
12761 formal_arg_flag = 1;
12762
12763 gfc_resolve_array_spec (sym->as, check_constant);
12764
12765 formal_arg_flag = 0;
12766
12767 /* Resolve formal namespaces. */
12768 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12769 && !sym->attr.contained && !sym->attr.intrinsic)
12770 gfc_resolve (sym->formal_ns);
12771
12772 /* Make sure the formal namespace is present. */
12773 if (sym->formal && !sym->formal_ns)
12774 {
12775 gfc_formal_arglist *formal = sym->formal;
12776 while (formal && !formal->sym)
12777 formal = formal->next;
12778
12779 if (formal)
12780 {
12781 sym->formal_ns = formal->sym->ns;
12782 sym->formal_ns->refs++;
12783 }
12784 }
12785
12786 /* Check threadprivate restrictions. */
12787 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12788 && (!sym->attr.in_common
12789 && sym->module == NULL
12790 && (sym->ns->proc_name == NULL
12791 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12792 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12793
12794 /* If we have come this far we can apply default-initializers, as
12795 described in 14.7.5, to those variables that have not already
12796 been assigned one. */
12797 if (sym->ts.type == BT_DERIVED
12798 && sym->ns == gfc_current_ns
12799 && !sym->value
12800 && !sym->attr.allocatable
12801 && !sym->attr.alloc_comp)
12802 {
12803 symbol_attribute *a = &sym->attr;
12804
12805 if ((!a->save && !a->dummy && !a->pointer
12806 && !a->in_common && !a->use_assoc
12807 && (a->referenced || a->result)
12808 && !(a->function && sym != sym->result))
12809 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12810 apply_default_init (sym);
12811 }
12812
12813 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12814 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12815 && !CLASS_DATA (sym)->attr.class_pointer
12816 && !CLASS_DATA (sym)->attr.allocatable)
12817 apply_default_init (sym);
12818
12819 /* If this symbol has a type-spec, check it. */
12820 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12821 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12822 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12823 == FAILURE)
12824 return;
12825 }
12826
12827
12828 /************* Resolve DATA statements *************/
12829
12830 static struct
12831 {
12832 gfc_data_value *vnode;
12833 mpz_t left;
12834 }
12835 values;
12836
12837
12838 /* Advance the values structure to point to the next value in the data list. */
12839
12840 static gfc_try
12841 next_data_value (void)
12842 {
12843 while (mpz_cmp_ui (values.left, 0) == 0)
12844 {
12845
12846 if (values.vnode->next == NULL)
12847 return FAILURE;
12848
12849 values.vnode = values.vnode->next;
12850 mpz_set (values.left, values.vnode->repeat);
12851 }
12852
12853 return SUCCESS;
12854 }
12855
12856
12857 static gfc_try
12858 check_data_variable (gfc_data_variable *var, locus *where)
12859 {
12860 gfc_expr *e;
12861 mpz_t size;
12862 mpz_t offset;
12863 gfc_try t;
12864 ar_type mark = AR_UNKNOWN;
12865 int i;
12866 mpz_t section_index[GFC_MAX_DIMENSIONS];
12867 gfc_ref *ref;
12868 gfc_array_ref *ar;
12869 gfc_symbol *sym;
12870 int has_pointer;
12871
12872 if (gfc_resolve_expr (var->expr) == FAILURE)
12873 return FAILURE;
12874
12875 ar = NULL;
12876 mpz_init_set_si (offset, 0);
12877 e = var->expr;
12878
12879 if (e->expr_type != EXPR_VARIABLE)
12880 gfc_internal_error ("check_data_variable(): Bad expression");
12881
12882 sym = e->symtree->n.sym;
12883
12884 if (sym->ns->is_block_data && !sym->attr.in_common)
12885 {
12886 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12887 sym->name, &sym->declared_at);
12888 }
12889
12890 if (e->ref == NULL && sym->as)
12891 {
12892 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12893 " declaration", sym->name, where);
12894 return FAILURE;
12895 }
12896
12897 has_pointer = sym->attr.pointer;
12898
12899 if (gfc_is_coindexed (e))
12900 {
12901 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
12902 where);
12903 return FAILURE;
12904 }
12905
12906 for (ref = e->ref; ref; ref = ref->next)
12907 {
12908 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12909 has_pointer = 1;
12910
12911 if (has_pointer
12912 && ref->type == REF_ARRAY
12913 && ref->u.ar.type != AR_FULL)
12914 {
12915 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12916 "be a full array", sym->name, where);
12917 return FAILURE;
12918 }
12919 }
12920
12921 if (e->rank == 0 || has_pointer)
12922 {
12923 mpz_init_set_ui (size, 1);
12924 ref = NULL;
12925 }
12926 else
12927 {
12928 ref = e->ref;
12929
12930 /* Find the array section reference. */
12931 for (ref = e->ref; ref; ref = ref->next)
12932 {
12933 if (ref->type != REF_ARRAY)
12934 continue;
12935 if (ref->u.ar.type == AR_ELEMENT)
12936 continue;
12937 break;
12938 }
12939 gcc_assert (ref);
12940
12941 /* Set marks according to the reference pattern. */
12942 switch (ref->u.ar.type)
12943 {
12944 case AR_FULL:
12945 mark = AR_FULL;
12946 break;
12947
12948 case AR_SECTION:
12949 ar = &ref->u.ar;
12950 /* Get the start position of array section. */
12951 gfc_get_section_index (ar, section_index, &offset);
12952 mark = AR_SECTION;
12953 break;
12954
12955 default:
12956 gcc_unreachable ();
12957 }
12958
12959 if (gfc_array_size (e, &size) == FAILURE)
12960 {
12961 gfc_error ("Nonconstant array section at %L in DATA statement",
12962 &e->where);
12963 mpz_clear (offset);
12964 return FAILURE;
12965 }
12966 }
12967
12968 t = SUCCESS;
12969
12970 while (mpz_cmp_ui (size, 0) > 0)
12971 {
12972 if (next_data_value () == FAILURE)
12973 {
12974 gfc_error ("DATA statement at %L has more variables than values",
12975 where);
12976 t = FAILURE;
12977 break;
12978 }
12979
12980 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12981 if (t == FAILURE)
12982 break;
12983
12984 /* If we have more than one element left in the repeat count,
12985 and we have more than one element left in the target variable,
12986 then create a range assignment. */
12987 /* FIXME: Only done for full arrays for now, since array sections
12988 seem tricky. */
12989 if (mark == AR_FULL && ref && ref->next == NULL
12990 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12991 {
12992 mpz_t range;
12993
12994 if (mpz_cmp (size, values.left) >= 0)
12995 {
12996 mpz_init_set (range, values.left);
12997 mpz_sub (size, size, values.left);
12998 mpz_set_ui (values.left, 0);
12999 }
13000 else
13001 {
13002 mpz_init_set (range, size);
13003 mpz_sub (values.left, values.left, size);
13004 mpz_set_ui (size, 0);
13005 }
13006
13007 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13008 offset, &range);
13009
13010 mpz_add (offset, offset, range);
13011 mpz_clear (range);
13012
13013 if (t == FAILURE)
13014 break;
13015 }
13016
13017 /* Assign initial value to symbol. */
13018 else
13019 {
13020 mpz_sub_ui (values.left, values.left, 1);
13021 mpz_sub_ui (size, size, 1);
13022
13023 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13024 offset, NULL);
13025 if (t == FAILURE)
13026 break;
13027
13028 if (mark == AR_FULL)
13029 mpz_add_ui (offset, offset, 1);
13030
13031 /* Modify the array section indexes and recalculate the offset
13032 for next element. */
13033 else if (mark == AR_SECTION)
13034 gfc_advance_section (section_index, ar, &offset);
13035 }
13036 }
13037
13038 if (mark == AR_SECTION)
13039 {
13040 for (i = 0; i < ar->dimen; i++)
13041 mpz_clear (section_index[i]);
13042 }
13043
13044 mpz_clear (size);
13045 mpz_clear (offset);
13046
13047 return t;
13048 }
13049
13050
13051 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
13052
13053 /* Iterate over a list of elements in a DATA statement. */
13054
13055 static gfc_try
13056 traverse_data_list (gfc_data_variable *var, locus *where)
13057 {
13058 mpz_t trip;
13059 iterator_stack frame;
13060 gfc_expr *e, *start, *end, *step;
13061 gfc_try retval = SUCCESS;
13062
13063 mpz_init (frame.value);
13064 mpz_init (trip);
13065
13066 start = gfc_copy_expr (var->iter.start);
13067 end = gfc_copy_expr (var->iter.end);
13068 step = gfc_copy_expr (var->iter.step);
13069
13070 if (gfc_simplify_expr (start, 1) == FAILURE
13071 || start->expr_type != EXPR_CONSTANT)
13072 {
13073 gfc_error ("start of implied-do loop at %L could not be "
13074 "simplified to a constant value", &start->where);
13075 retval = FAILURE;
13076 goto cleanup;
13077 }
13078 if (gfc_simplify_expr (end, 1) == FAILURE
13079 || end->expr_type != EXPR_CONSTANT)
13080 {
13081 gfc_error ("end of implied-do loop at %L could not be "
13082 "simplified to a constant value", &start->where);
13083 retval = FAILURE;
13084 goto cleanup;
13085 }
13086 if (gfc_simplify_expr (step, 1) == FAILURE
13087 || step->expr_type != EXPR_CONSTANT)
13088 {
13089 gfc_error ("step of implied-do loop at %L could not be "
13090 "simplified to a constant value", &start->where);
13091 retval = FAILURE;
13092 goto cleanup;
13093 }
13094
13095 mpz_set (trip, end->value.integer);
13096 mpz_sub (trip, trip, start->value.integer);
13097 mpz_add (trip, trip, step->value.integer);
13098
13099 mpz_div (trip, trip, step->value.integer);
13100
13101 mpz_set (frame.value, start->value.integer);
13102
13103 frame.prev = iter_stack;
13104 frame.variable = var->iter.var->symtree;
13105 iter_stack = &frame;
13106
13107 while (mpz_cmp_ui (trip, 0) > 0)
13108 {
13109 if (traverse_data_var (var->list, where) == FAILURE)
13110 {
13111 retval = FAILURE;
13112 goto cleanup;
13113 }
13114
13115 e = gfc_copy_expr (var->expr);
13116 if (gfc_simplify_expr (e, 1) == FAILURE)
13117 {
13118 gfc_free_expr (e);
13119 retval = FAILURE;
13120 goto cleanup;
13121 }
13122
13123 mpz_add (frame.value, frame.value, step->value.integer);
13124
13125 mpz_sub_ui (trip, trip, 1);
13126 }
13127
13128 cleanup:
13129 mpz_clear (frame.value);
13130 mpz_clear (trip);
13131
13132 gfc_free_expr (start);
13133 gfc_free_expr (end);
13134 gfc_free_expr (step);
13135
13136 iter_stack = frame.prev;
13137 return retval;
13138 }
13139
13140
13141 /* Type resolve variables in the variable list of a DATA statement. */
13142
13143 static gfc_try
13144 traverse_data_var (gfc_data_variable *var, locus *where)
13145 {
13146 gfc_try t;
13147
13148 for (; var; var = var->next)
13149 {
13150 if (var->expr == NULL)
13151 t = traverse_data_list (var, where);
13152 else
13153 t = check_data_variable (var, where);
13154
13155 if (t == FAILURE)
13156 return FAILURE;
13157 }
13158
13159 return SUCCESS;
13160 }
13161
13162
13163 /* Resolve the expressions and iterators associated with a data statement.
13164 This is separate from the assignment checking because data lists should
13165 only be resolved once. */
13166
13167 static gfc_try
13168 resolve_data_variables (gfc_data_variable *d)
13169 {
13170 for (; d; d = d->next)
13171 {
13172 if (d->list == NULL)
13173 {
13174 if (gfc_resolve_expr (d->expr) == FAILURE)
13175 return FAILURE;
13176 }
13177 else
13178 {
13179 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
13180 return FAILURE;
13181
13182 if (resolve_data_variables (d->list) == FAILURE)
13183 return FAILURE;
13184 }
13185 }
13186
13187 return SUCCESS;
13188 }
13189
13190
13191 /* Resolve a single DATA statement. We implement this by storing a pointer to
13192 the value list into static variables, and then recursively traversing the
13193 variables list, expanding iterators and such. */
13194
13195 static void
13196 resolve_data (gfc_data *d)
13197 {
13198
13199 if (resolve_data_variables (d->var) == FAILURE)
13200 return;
13201
13202 values.vnode = d->value;
13203 if (d->value == NULL)
13204 mpz_set_ui (values.left, 0);
13205 else
13206 mpz_set (values.left, d->value->repeat);
13207
13208 if (traverse_data_var (d->var, &d->where) == FAILURE)
13209 return;
13210
13211 /* At this point, we better not have any values left. */
13212
13213 if (next_data_value () == SUCCESS)
13214 gfc_error ("DATA statement at %L has more values than variables",
13215 &d->where);
13216 }
13217
13218
13219 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13220 accessed by host or use association, is a dummy argument to a pure function,
13221 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13222 is storage associated with any such variable, shall not be used in the
13223 following contexts: (clients of this function). */
13224
13225 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13226 procedure. Returns zero if assignment is OK, nonzero if there is a
13227 problem. */
13228 int
13229 gfc_impure_variable (gfc_symbol *sym)
13230 {
13231 gfc_symbol *proc;
13232 gfc_namespace *ns;
13233
13234 if (sym->attr.use_assoc || sym->attr.in_common)
13235 return 1;
13236
13237 /* Check if the symbol's ns is inside the pure procedure. */
13238 for (ns = gfc_current_ns; ns; ns = ns->parent)
13239 {
13240 if (ns == sym->ns)
13241 break;
13242 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13243 return 1;
13244 }
13245
13246 proc = sym->ns->proc_name;
13247 if (sym->attr.dummy && gfc_pure (proc)
13248 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13249 ||
13250 proc->attr.function))
13251 return 1;
13252
13253 /* TODO: Sort out what can be storage associated, if anything, and include
13254 it here. In principle equivalences should be scanned but it does not
13255 seem to be possible to storage associate an impure variable this way. */
13256 return 0;
13257 }
13258
13259
13260 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13261 current namespace is inside a pure procedure. */
13262
13263 int
13264 gfc_pure (gfc_symbol *sym)
13265 {
13266 symbol_attribute attr;
13267 gfc_namespace *ns;
13268
13269 if (sym == NULL)
13270 {
13271 /* Check if the current namespace or one of its parents
13272 belongs to a pure procedure. */
13273 for (ns = gfc_current_ns; ns; ns = ns->parent)
13274 {
13275 sym = ns->proc_name;
13276 if (sym == NULL)
13277 return 0;
13278 attr = sym->attr;
13279 if (attr.flavor == FL_PROCEDURE && attr.pure)
13280 return 1;
13281 }
13282 return 0;
13283 }
13284
13285 attr = sym->attr;
13286
13287 return attr.flavor == FL_PROCEDURE && attr.pure;
13288 }
13289
13290
13291 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13292 checks if the current namespace is implicitly pure. Note that this
13293 function returns false for a PURE procedure. */
13294
13295 int
13296 gfc_implicit_pure (gfc_symbol *sym)
13297 {
13298 gfc_namespace *ns;
13299
13300 if (sym == NULL)
13301 {
13302 /* Check if the current procedure is implicit_pure. Walk up
13303 the procedure list until we find a procedure. */
13304 for (ns = gfc_current_ns; ns; ns = ns->parent)
13305 {
13306 sym = ns->proc_name;
13307 if (sym == NULL)
13308 return 0;
13309
13310 if (sym->attr.flavor == FL_PROCEDURE)
13311 break;
13312 }
13313 }
13314
13315 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13316 && !sym->attr.pure;
13317 }
13318
13319
13320 /* Test whether the current procedure is elemental or not. */
13321
13322 int
13323 gfc_elemental (gfc_symbol *sym)
13324 {
13325 symbol_attribute attr;
13326
13327 if (sym == NULL)
13328 sym = gfc_current_ns->proc_name;
13329 if (sym == NULL)
13330 return 0;
13331 attr = sym->attr;
13332
13333 return attr.flavor == FL_PROCEDURE && attr.elemental;
13334 }
13335
13336
13337 /* Warn about unused labels. */
13338
13339 static void
13340 warn_unused_fortran_label (gfc_st_label *label)
13341 {
13342 if (label == NULL)
13343 return;
13344
13345 warn_unused_fortran_label (label->left);
13346
13347 if (label->defined == ST_LABEL_UNKNOWN)
13348 return;
13349
13350 switch (label->referenced)
13351 {
13352 case ST_LABEL_UNKNOWN:
13353 gfc_warning ("Label %d at %L defined but not used", label->value,
13354 &label->where);
13355 break;
13356
13357 case ST_LABEL_BAD_TARGET:
13358 gfc_warning ("Label %d at %L defined but cannot be used",
13359 label->value, &label->where);
13360 break;
13361
13362 default:
13363 break;
13364 }
13365
13366 warn_unused_fortran_label (label->right);
13367 }
13368
13369
13370 /* Returns the sequence type of a symbol or sequence. */
13371
13372 static seq_type
13373 sequence_type (gfc_typespec ts)
13374 {
13375 seq_type result;
13376 gfc_component *c;
13377
13378 switch (ts.type)
13379 {
13380 case BT_DERIVED:
13381
13382 if (ts.u.derived->components == NULL)
13383 return SEQ_NONDEFAULT;
13384
13385 result = sequence_type (ts.u.derived->components->ts);
13386 for (c = ts.u.derived->components->next; c; c = c->next)
13387 if (sequence_type (c->ts) != result)
13388 return SEQ_MIXED;
13389
13390 return result;
13391
13392 case BT_CHARACTER:
13393 if (ts.kind != gfc_default_character_kind)
13394 return SEQ_NONDEFAULT;
13395
13396 return SEQ_CHARACTER;
13397
13398 case BT_INTEGER:
13399 if (ts.kind != gfc_default_integer_kind)
13400 return SEQ_NONDEFAULT;
13401
13402 return SEQ_NUMERIC;
13403
13404 case BT_REAL:
13405 if (!(ts.kind == gfc_default_real_kind
13406 || ts.kind == gfc_default_double_kind))
13407 return SEQ_NONDEFAULT;
13408
13409 return SEQ_NUMERIC;
13410
13411 case BT_COMPLEX:
13412 if (ts.kind != gfc_default_complex_kind)
13413 return SEQ_NONDEFAULT;
13414
13415 return SEQ_NUMERIC;
13416
13417 case BT_LOGICAL:
13418 if (ts.kind != gfc_default_logical_kind)
13419 return SEQ_NONDEFAULT;
13420
13421 return SEQ_NUMERIC;
13422
13423 default:
13424 return SEQ_NONDEFAULT;
13425 }
13426 }
13427
13428
13429 /* Resolve derived type EQUIVALENCE object. */
13430
13431 static gfc_try
13432 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13433 {
13434 gfc_component *c = derived->components;
13435
13436 if (!derived)
13437 return SUCCESS;
13438
13439 /* Shall not be an object of nonsequence derived type. */
13440 if (!derived->attr.sequence)
13441 {
13442 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13443 "attribute to be an EQUIVALENCE object", sym->name,
13444 &e->where);
13445 return FAILURE;
13446 }
13447
13448 /* Shall not have allocatable components. */
13449 if (derived->attr.alloc_comp)
13450 {
13451 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13452 "components to be an EQUIVALENCE object",sym->name,
13453 &e->where);
13454 return FAILURE;
13455 }
13456
13457 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13458 {
13459 gfc_error ("Derived type variable '%s' at %L with default "
13460 "initialization cannot be in EQUIVALENCE with a variable "
13461 "in COMMON", sym->name, &e->where);
13462 return FAILURE;
13463 }
13464
13465 for (; c ; c = c->next)
13466 {
13467 if (c->ts.type == BT_DERIVED
13468 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
13469 return FAILURE;
13470
13471 /* Shall not be an object of sequence derived type containing a pointer
13472 in the structure. */
13473 if (c->attr.pointer)
13474 {
13475 gfc_error ("Derived type variable '%s' at %L with pointer "
13476 "component(s) cannot be an EQUIVALENCE object",
13477 sym->name, &e->where);
13478 return FAILURE;
13479 }
13480 }
13481 return SUCCESS;
13482 }
13483
13484
13485 /* Resolve equivalence object.
13486 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13487 an allocatable array, an object of nonsequence derived type, an object of
13488 sequence derived type containing a pointer at any level of component
13489 selection, an automatic object, a function name, an entry name, a result
13490 name, a named constant, a structure component, or a subobject of any of
13491 the preceding objects. A substring shall not have length zero. A
13492 derived type shall not have components with default initialization nor
13493 shall two objects of an equivalence group be initialized.
13494 Either all or none of the objects shall have an protected attribute.
13495 The simple constraints are done in symbol.c(check_conflict) and the rest
13496 are implemented here. */
13497
13498 static void
13499 resolve_equivalence (gfc_equiv *eq)
13500 {
13501 gfc_symbol *sym;
13502 gfc_symbol *first_sym;
13503 gfc_expr *e;
13504 gfc_ref *r;
13505 locus *last_where = NULL;
13506 seq_type eq_type, last_eq_type;
13507 gfc_typespec *last_ts;
13508 int object, cnt_protected;
13509 const char *msg;
13510
13511 last_ts = &eq->expr->symtree->n.sym->ts;
13512
13513 first_sym = eq->expr->symtree->n.sym;
13514
13515 cnt_protected = 0;
13516
13517 for (object = 1; eq; eq = eq->eq, object++)
13518 {
13519 e = eq->expr;
13520
13521 e->ts = e->symtree->n.sym->ts;
13522 /* match_varspec might not know yet if it is seeing
13523 array reference or substring reference, as it doesn't
13524 know the types. */
13525 if (e->ref && e->ref->type == REF_ARRAY)
13526 {
13527 gfc_ref *ref = e->ref;
13528 sym = e->symtree->n.sym;
13529
13530 if (sym->attr.dimension)
13531 {
13532 ref->u.ar.as = sym->as;
13533 ref = ref->next;
13534 }
13535
13536 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13537 if (e->ts.type == BT_CHARACTER
13538 && ref
13539 && ref->type == REF_ARRAY
13540 && ref->u.ar.dimen == 1
13541 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13542 && ref->u.ar.stride[0] == NULL)
13543 {
13544 gfc_expr *start = ref->u.ar.start[0];
13545 gfc_expr *end = ref->u.ar.end[0];
13546 void *mem = NULL;
13547
13548 /* Optimize away the (:) reference. */
13549 if (start == NULL && end == NULL)
13550 {
13551 if (e->ref == ref)
13552 e->ref = ref->next;
13553 else
13554 e->ref->next = ref->next;
13555 mem = ref;
13556 }
13557 else
13558 {
13559 ref->type = REF_SUBSTRING;
13560 if (start == NULL)
13561 start = gfc_get_int_expr (gfc_default_integer_kind,
13562 NULL, 1);
13563 ref->u.ss.start = start;
13564 if (end == NULL && e->ts.u.cl)
13565 end = gfc_copy_expr (e->ts.u.cl->length);
13566 ref->u.ss.end = end;
13567 ref->u.ss.length = e->ts.u.cl;
13568 e->ts.u.cl = NULL;
13569 }
13570 ref = ref->next;
13571 free (mem);
13572 }
13573
13574 /* Any further ref is an error. */
13575 if (ref)
13576 {
13577 gcc_assert (ref->type == REF_ARRAY);
13578 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13579 &ref->u.ar.where);
13580 continue;
13581 }
13582 }
13583
13584 if (gfc_resolve_expr (e) == FAILURE)
13585 continue;
13586
13587 sym = e->symtree->n.sym;
13588
13589 if (sym->attr.is_protected)
13590 cnt_protected++;
13591 if (cnt_protected > 0 && cnt_protected != object)
13592 {
13593 gfc_error ("Either all or none of the objects in the "
13594 "EQUIVALENCE set at %L shall have the "
13595 "PROTECTED attribute",
13596 &e->where);
13597 break;
13598 }
13599
13600 /* Shall not equivalence common block variables in a PURE procedure. */
13601 if (sym->ns->proc_name
13602 && sym->ns->proc_name->attr.pure
13603 && sym->attr.in_common)
13604 {
13605 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13606 "object in the pure procedure '%s'",
13607 sym->name, &e->where, sym->ns->proc_name->name);
13608 break;
13609 }
13610
13611 /* Shall not be a named constant. */
13612 if (e->expr_type == EXPR_CONSTANT)
13613 {
13614 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13615 "object", sym->name, &e->where);
13616 continue;
13617 }
13618
13619 if (e->ts.type == BT_DERIVED
13620 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13621 continue;
13622
13623 /* Check that the types correspond correctly:
13624 Note 5.28:
13625 A numeric sequence structure may be equivalenced to another sequence
13626 structure, an object of default integer type, default real type, double
13627 precision real type, default logical type such that components of the
13628 structure ultimately only become associated to objects of the same
13629 kind. A character sequence structure may be equivalenced to an object
13630 of default character kind or another character sequence structure.
13631 Other objects may be equivalenced only to objects of the same type and
13632 kind parameters. */
13633
13634 /* Identical types are unconditionally OK. */
13635 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13636 goto identical_types;
13637
13638 last_eq_type = sequence_type (*last_ts);
13639 eq_type = sequence_type (sym->ts);
13640
13641 /* Since the pair of objects is not of the same type, mixed or
13642 non-default sequences can be rejected. */
13643
13644 msg = "Sequence %s with mixed components in EQUIVALENCE "
13645 "statement at %L with different type objects";
13646 if ((object ==2
13647 && last_eq_type == SEQ_MIXED
13648 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13649 == FAILURE)
13650 || (eq_type == SEQ_MIXED
13651 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13652 &e->where) == FAILURE))
13653 continue;
13654
13655 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13656 "statement at %L with objects of different type";
13657 if ((object ==2
13658 && last_eq_type == SEQ_NONDEFAULT
13659 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13660 last_where) == FAILURE)
13661 || (eq_type == SEQ_NONDEFAULT
13662 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13663 &e->where) == FAILURE))
13664 continue;
13665
13666 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13667 "EQUIVALENCE statement at %L";
13668 if (last_eq_type == SEQ_CHARACTER
13669 && eq_type != SEQ_CHARACTER
13670 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13671 &e->where) == FAILURE)
13672 continue;
13673
13674 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13675 "EQUIVALENCE statement at %L";
13676 if (last_eq_type == SEQ_NUMERIC
13677 && eq_type != SEQ_NUMERIC
13678 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13679 &e->where) == FAILURE)
13680 continue;
13681
13682 identical_types:
13683 last_ts =&sym->ts;
13684 last_where = &e->where;
13685
13686 if (!e->ref)
13687 continue;
13688
13689 /* Shall not be an automatic array. */
13690 if (e->ref->type == REF_ARRAY
13691 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13692 {
13693 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13694 "an EQUIVALENCE object", sym->name, &e->where);
13695 continue;
13696 }
13697
13698 r = e->ref;
13699 while (r)
13700 {
13701 /* Shall not be a structure component. */
13702 if (r->type == REF_COMPONENT)
13703 {
13704 gfc_error ("Structure component '%s' at %L cannot be an "
13705 "EQUIVALENCE object",
13706 r->u.c.component->name, &e->where);
13707 break;
13708 }
13709
13710 /* A substring shall not have length zero. */
13711 if (r->type == REF_SUBSTRING)
13712 {
13713 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13714 {
13715 gfc_error ("Substring at %L has length zero",
13716 &r->u.ss.start->where);
13717 break;
13718 }
13719 }
13720 r = r->next;
13721 }
13722 }
13723 }
13724
13725
13726 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13727
13728 static void
13729 resolve_fntype (gfc_namespace *ns)
13730 {
13731 gfc_entry_list *el;
13732 gfc_symbol *sym;
13733
13734 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13735 return;
13736
13737 /* If there are any entries, ns->proc_name is the entry master
13738 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13739 if (ns->entries)
13740 sym = ns->entries->sym;
13741 else
13742 sym = ns->proc_name;
13743 if (sym->result == sym
13744 && sym->ts.type == BT_UNKNOWN
13745 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13746 && !sym->attr.untyped)
13747 {
13748 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13749 sym->name, &sym->declared_at);
13750 sym->attr.untyped = 1;
13751 }
13752
13753 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13754 && !sym->attr.contained
13755 && !gfc_check_symbol_access (sym->ts.u.derived)
13756 && gfc_check_symbol_access (sym))
13757 {
13758 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13759 "%L of PRIVATE type '%s'", sym->name,
13760 &sym->declared_at, sym->ts.u.derived->name);
13761 }
13762
13763 if (ns->entries)
13764 for (el = ns->entries->next; el; el = el->next)
13765 {
13766 if (el->sym->result == el->sym
13767 && el->sym->ts.type == BT_UNKNOWN
13768 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13769 && !el->sym->attr.untyped)
13770 {
13771 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13772 el->sym->name, &el->sym->declared_at);
13773 el->sym->attr.untyped = 1;
13774 }
13775 }
13776 }
13777
13778
13779 /* 12.3.2.1.1 Defined operators. */
13780
13781 static gfc_try
13782 check_uop_procedure (gfc_symbol *sym, locus where)
13783 {
13784 gfc_formal_arglist *formal;
13785
13786 if (!sym->attr.function)
13787 {
13788 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13789 sym->name, &where);
13790 return FAILURE;
13791 }
13792
13793 if (sym->ts.type == BT_CHARACTER
13794 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13795 && !(sym->result && sym->result->ts.u.cl
13796 && sym->result->ts.u.cl->length))
13797 {
13798 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13799 "character length", sym->name, &where);
13800 return FAILURE;
13801 }
13802
13803 formal = sym->formal;
13804 if (!formal || !formal->sym)
13805 {
13806 gfc_error ("User operator procedure '%s' at %L must have at least "
13807 "one argument", sym->name, &where);
13808 return FAILURE;
13809 }
13810
13811 if (formal->sym->attr.intent != INTENT_IN)
13812 {
13813 gfc_error ("First argument of operator interface at %L must be "
13814 "INTENT(IN)", &where);
13815 return FAILURE;
13816 }
13817
13818 if (formal->sym->attr.optional)
13819 {
13820 gfc_error ("First argument of operator interface at %L cannot be "
13821 "optional", &where);
13822 return FAILURE;
13823 }
13824
13825 formal = formal->next;
13826 if (!formal || !formal->sym)
13827 return SUCCESS;
13828
13829 if (formal->sym->attr.intent != INTENT_IN)
13830 {
13831 gfc_error ("Second argument of operator interface at %L must be "
13832 "INTENT(IN)", &where);
13833 return FAILURE;
13834 }
13835
13836 if (formal->sym->attr.optional)
13837 {
13838 gfc_error ("Second argument of operator interface at %L cannot be "
13839 "optional", &where);
13840 return FAILURE;
13841 }
13842
13843 if (formal->next)
13844 {
13845 gfc_error ("Operator interface at %L must have, at most, two "
13846 "arguments", &where);
13847 return FAILURE;
13848 }
13849
13850 return SUCCESS;
13851 }
13852
13853 static void
13854 gfc_resolve_uops (gfc_symtree *symtree)
13855 {
13856 gfc_interface *itr;
13857
13858 if (symtree == NULL)
13859 return;
13860
13861 gfc_resolve_uops (symtree->left);
13862 gfc_resolve_uops (symtree->right);
13863
13864 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13865 check_uop_procedure (itr->sym, itr->sym->declared_at);
13866 }
13867
13868
13869 /* Examine all of the expressions associated with a program unit,
13870 assign types to all intermediate expressions, make sure that all
13871 assignments are to compatible types and figure out which names
13872 refer to which functions or subroutines. It doesn't check code
13873 block, which is handled by resolve_code. */
13874
13875 static void
13876 resolve_types (gfc_namespace *ns)
13877 {
13878 gfc_namespace *n;
13879 gfc_charlen *cl;
13880 gfc_data *d;
13881 gfc_equiv *eq;
13882 gfc_namespace* old_ns = gfc_current_ns;
13883
13884 /* Check that all IMPLICIT types are ok. */
13885 if (!ns->seen_implicit_none)
13886 {
13887 unsigned letter;
13888 for (letter = 0; letter != GFC_LETTERS; ++letter)
13889 if (ns->set_flag[letter]
13890 && resolve_typespec_used (&ns->default_type[letter],
13891 &ns->implicit_loc[letter],
13892 NULL) == FAILURE)
13893 return;
13894 }
13895
13896 gfc_current_ns = ns;
13897
13898 resolve_entries (ns);
13899
13900 resolve_common_vars (ns->blank_common.head, false);
13901 resolve_common_blocks (ns->common_root);
13902
13903 resolve_contained_functions (ns);
13904
13905 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
13906 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
13907 resolve_formal_arglist (ns->proc_name);
13908
13909 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13910
13911 for (cl = ns->cl_list; cl; cl = cl->next)
13912 resolve_charlen (cl);
13913
13914 gfc_traverse_ns (ns, resolve_symbol);
13915
13916 resolve_fntype (ns);
13917
13918 for (n = ns->contained; n; n = n->sibling)
13919 {
13920 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13921 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13922 "also be PURE", n->proc_name->name,
13923 &n->proc_name->declared_at);
13924
13925 resolve_types (n);
13926 }
13927
13928 forall_flag = 0;
13929 do_concurrent_flag = 0;
13930 gfc_check_interfaces (ns);
13931
13932 gfc_traverse_ns (ns, resolve_values);
13933
13934 if (ns->save_all)
13935 gfc_save_all (ns);
13936
13937 iter_stack = NULL;
13938 for (d = ns->data; d; d = d->next)
13939 resolve_data (d);
13940
13941 iter_stack = NULL;
13942 gfc_traverse_ns (ns, gfc_formalize_init_value);
13943
13944 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13945
13946 if (ns->common_root != NULL)
13947 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13948
13949 for (eq = ns->equiv; eq; eq = eq->next)
13950 resolve_equivalence (eq);
13951
13952 /* Warn about unused labels. */
13953 if (warn_unused_label)
13954 warn_unused_fortran_label (ns->st_labels);
13955
13956 gfc_resolve_uops (ns->uop_root);
13957
13958 gfc_current_ns = old_ns;
13959 }
13960
13961
13962 /* Call resolve_code recursively. */
13963
13964 static void
13965 resolve_codes (gfc_namespace *ns)
13966 {
13967 gfc_namespace *n;
13968 bitmap_obstack old_obstack;
13969
13970 if (ns->resolved == 1)
13971 return;
13972
13973 for (n = ns->contained; n; n = n->sibling)
13974 resolve_codes (n);
13975
13976 gfc_current_ns = ns;
13977
13978 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13979 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13980 cs_base = NULL;
13981
13982 /* Set to an out of range value. */
13983 current_entry_id = -1;
13984
13985 old_obstack = labels_obstack;
13986 bitmap_obstack_initialize (&labels_obstack);
13987
13988 resolve_code (ns->code, ns);
13989
13990 bitmap_obstack_release (&labels_obstack);
13991 labels_obstack = old_obstack;
13992 }
13993
13994
13995 /* This function is called after a complete program unit has been compiled.
13996 Its purpose is to examine all of the expressions associated with a program
13997 unit, assign types to all intermediate expressions, make sure that all
13998 assignments are to compatible types and figure out which names refer to
13999 which functions or subroutines. */
14000
14001 void
14002 gfc_resolve (gfc_namespace *ns)
14003 {
14004 gfc_namespace *old_ns;
14005 code_stack *old_cs_base;
14006
14007 if (ns->resolved)
14008 return;
14009
14010 ns->resolved = -1;
14011 old_ns = gfc_current_ns;
14012 old_cs_base = cs_base;
14013
14014 resolve_types (ns);
14015 resolve_codes (ns);
14016
14017 gfc_current_ns = old_ns;
14018 cs_base = old_cs_base;
14019 ns->resolved = 1;
14020
14021 gfc_run_passes (ns);
14022 }