re PR fortran/51081 ([F03] Proc-pointer assignment: Rejects valid internal proc)
[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 "coretypes.h"
26 #include "flags.h"
27 #include "gfortran.h"
28 #include "obstack.h"
29 #include "bitmap.h"
30 #include "arith.h" /* For gfc_compare_expr(). */
31 #include "dependency.h"
32 #include "data.h"
33 #include "target-memory.h" /* for gfc_simplify_transfer */
34 #include "constructor.h"
35
36 /* Types used in equivalence statements. */
37
38 typedef enum seq_type
39 {
40 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 }
42 seq_type;
43
44 /* Stack to keep track of the nesting of blocks as we move through the
45 code. See resolve_branch() and resolve_code(). */
46
47 typedef struct code_stack
48 {
49 struct gfc_code *head, *current;
50 struct code_stack *prev;
51
52 /* This bitmap keeps track of the targets valid for a branch from
53 inside this block except for END {IF|SELECT}s of enclosing
54 blocks. */
55 bitmap reachable_labels;
56 }
57 code_stack;
58
59 static code_stack *cs_base = NULL;
60
61
62 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
63
64 static int forall_flag;
65 static int do_concurrent_flag;
66
67 /* True when we are resolving an expression that is an actual argument to
68 a procedure. */
69 static bool actual_arg = false;
70 /* True when we are resolving an expression that is the first actual argument
71 to a procedure. */
72 static bool first_actual_arg = false;
73
74
75 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
76
77 static int omp_workshare_flag;
78
79 /* Nonzero if we are processing a formal arglist. The corresponding function
80 resets the flag each time that it is read. */
81 static int formal_arg_flag = 0;
82
83 /* True if we are resolving a specification expression. */
84 static int specification_expr = 0;
85
86 /* The id of the last entry seen. */
87 static int current_entry_id;
88
89 /* We use bitmaps to determine if a branch target is valid. */
90 static bitmap_obstack labels_obstack;
91
92 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
93 static bool inquiry_argument = false;
94
95
96 int
97 gfc_is_formal_arg (void)
98 {
99 return formal_arg_flag;
100 }
101
102 /* Is the symbol host associated? */
103 static bool
104 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
105 {
106 for (ns = ns->parent; ns; ns = ns->parent)
107 {
108 if (sym->ns == ns)
109 return true;
110 }
111
112 return false;
113 }
114
115 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
116 an ABSTRACT derived-type. If where is not NULL, an error message with that
117 locus is printed, optionally using name. */
118
119 static gfc_try
120 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
121 {
122 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
123 {
124 if (where)
125 {
126 if (name)
127 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
128 name, where, ts->u.derived->name);
129 else
130 gfc_error ("ABSTRACT type '%s' used at %L",
131 ts->u.derived->name, where);
132 }
133
134 return FAILURE;
135 }
136
137 return SUCCESS;
138 }
139
140
141 static void resolve_symbol (gfc_symbol *sym);
142
143
144 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
145
146 static gfc_try
147 resolve_procedure_interface (gfc_symbol *sym)
148 {
149 if (sym->ts.interface == sym)
150 {
151 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
152 sym->name, &sym->declared_at);
153 return FAILURE;
154 }
155 if (sym->ts.interface->attr.procedure)
156 {
157 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
158 "in a later PROCEDURE statement", sym->ts.interface->name,
159 sym->name, &sym->declared_at);
160 return FAILURE;
161 }
162
163 /* Get the attributes from the interface (now resolved). */
164 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
165 {
166 gfc_symbol *ifc = sym->ts.interface;
167 resolve_symbol (ifc);
168
169 if (ifc->attr.intrinsic)
170 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
171
172 if (ifc->result)
173 {
174 sym->ts = ifc->result->ts;
175 sym->result = sym;
176 }
177 else
178 sym->ts = ifc->ts;
179 sym->ts.interface = ifc;
180 sym->attr.function = ifc->attr.function;
181 sym->attr.subroutine = ifc->attr.subroutine;
182 gfc_copy_formal_args (sym, ifc, IFSRC_DECL);
183
184 sym->attr.allocatable = ifc->attr.allocatable;
185 sym->attr.pointer = ifc->attr.pointer;
186 sym->attr.pure = ifc->attr.pure;
187 sym->attr.elemental = ifc->attr.elemental;
188 sym->attr.dimension = ifc->attr.dimension;
189 sym->attr.contiguous = ifc->attr.contiguous;
190 sym->attr.recursive = ifc->attr.recursive;
191 sym->attr.always_explicit = ifc->attr.always_explicit;
192 sym->attr.ext_attr |= ifc->attr.ext_attr;
193 sym->attr.is_bind_c = ifc->attr.is_bind_c;
194 /* Copy array spec. */
195 sym->as = gfc_copy_array_spec (ifc->as);
196 if (sym->as)
197 {
198 int i;
199 for (i = 0; i < sym->as->rank; i++)
200 {
201 gfc_expr_replace_symbols (sym->as->lower[i], sym);
202 gfc_expr_replace_symbols (sym->as->upper[i], sym);
203 }
204 }
205 /* Copy char length. */
206 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
207 {
208 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
209 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
210 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
211 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
212 return FAILURE;
213 }
214 }
215 else if (sym->ts.interface->name[0] != '\0')
216 {
217 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
218 sym->ts.interface->name, sym->name, &sym->declared_at);
219 return FAILURE;
220 }
221
222 return SUCCESS;
223 }
224
225
226 /* Resolve types of formal argument lists. These have to be done early so that
227 the formal argument lists of module procedures can be copied to the
228 containing module before the individual procedures are resolved
229 individually. We also resolve argument lists of procedures in interface
230 blocks because they are self-contained scoping units.
231
232 Since a dummy argument cannot be a non-dummy procedure, the only
233 resort left for untyped names are the IMPLICIT types. */
234
235 static void
236 resolve_formal_arglist (gfc_symbol *proc)
237 {
238 gfc_formal_arglist *f;
239 gfc_symbol *sym;
240 int i;
241
242 if (proc->result != NULL)
243 sym = proc->result;
244 else
245 sym = proc;
246
247 if (gfc_elemental (proc)
248 || sym->attr.pointer || sym->attr.allocatable
249 || (sym->as && sym->as->rank != 0))
250 {
251 proc->attr.always_explicit = 1;
252 sym->attr.always_explicit = 1;
253 }
254
255 formal_arg_flag = 1;
256
257 for (f = proc->formal; f; f = f->next)
258 {
259 gfc_array_spec *as;
260
261 sym = f->sym;
262
263 if (sym == NULL)
264 {
265 /* Alternate return placeholder. */
266 if (gfc_elemental (proc))
267 gfc_error ("Alternate return specifier in elemental subroutine "
268 "'%s' at %L is not allowed", proc->name,
269 &proc->declared_at);
270 if (proc->attr.function)
271 gfc_error ("Alternate return specifier in function "
272 "'%s' at %L is not allowed", proc->name,
273 &proc->declared_at);
274 continue;
275 }
276 else if (sym->attr.procedure && sym->ts.interface
277 && sym->attr.if_source != IFSRC_DECL)
278 resolve_procedure_interface (sym);
279
280 if (sym->attr.if_source != IFSRC_UNKNOWN)
281 resolve_formal_arglist (sym);
282
283 if (sym->attr.subroutine || sym->attr.external)
284 {
285 if (sym->attr.flavor == FL_UNKNOWN)
286 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
287 }
288 else
289 {
290 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
291 && (!sym->attr.function || sym->result == sym))
292 gfc_set_default_type (sym, 1, sym->ns);
293 }
294
295 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
296 ? CLASS_DATA (sym)->as : sym->as;
297
298 gfc_resolve_array_spec (as, 0);
299
300 /* We can't tell if an array with dimension (:) is assumed or deferred
301 shape until we know if it has the pointer or allocatable attributes.
302 */
303 if (as && as->rank > 0 && as->type == AS_DEFERRED
304 && ((sym->ts.type != BT_CLASS
305 && !(sym->attr.pointer || sym->attr.allocatable))
306 || (sym->ts.type == BT_CLASS
307 && !(CLASS_DATA (sym)->attr.class_pointer
308 || CLASS_DATA (sym)->attr.allocatable)))
309 && sym->attr.flavor != FL_PROCEDURE)
310 {
311 as->type = AS_ASSUMED_SHAPE;
312 for (i = 0; i < as->rank; i++)
313 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
314 }
315
316 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
317 || (as && as->type == AS_ASSUMED_RANK)
318 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
319 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
320 && (CLASS_DATA (sym)->attr.class_pointer
321 || CLASS_DATA (sym)->attr.allocatable
322 || CLASS_DATA (sym)->attr.target))
323 || sym->attr.optional)
324 {
325 proc->attr.always_explicit = 1;
326 if (proc->result)
327 proc->result->attr.always_explicit = 1;
328 }
329
330 /* If the flavor is unknown at this point, it has to be a variable.
331 A procedure specification would have already set the type. */
332
333 if (sym->attr.flavor == FL_UNKNOWN)
334 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
335
336 if (gfc_pure (proc))
337 {
338 if (sym->attr.flavor == FL_PROCEDURE)
339 {
340 /* F08:C1279. */
341 if (!gfc_pure (sym))
342 {
343 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
344 "also be PURE", sym->name, &sym->declared_at);
345 continue;
346 }
347 }
348 else if (!sym->attr.pointer)
349 {
350 if (proc->attr.function && sym->attr.intent != INTENT_IN)
351 {
352 if (sym->attr.value)
353 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
354 " of pure function '%s' at %L with VALUE "
355 "attribute but without INTENT(IN)",
356 sym->name, proc->name, &sym->declared_at);
357 else
358 gfc_error ("Argument '%s' of pure function '%s' at %L must "
359 "be INTENT(IN) or VALUE", sym->name, proc->name,
360 &sym->declared_at);
361 }
362
363 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
364 {
365 if (sym->attr.value)
366 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
367 " of pure subroutine '%s' at %L with VALUE "
368 "attribute but without INTENT", sym->name,
369 proc->name, &sym->declared_at);
370 else
371 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
372 "must have its INTENT specified or have the "
373 "VALUE attribute", sym->name, proc->name,
374 &sym->declared_at);
375 }
376 }
377 }
378
379 if (proc->attr.implicit_pure)
380 {
381 if (sym->attr.flavor == FL_PROCEDURE)
382 {
383 if (!gfc_pure(sym))
384 proc->attr.implicit_pure = 0;
385 }
386 else if (!sym->attr.pointer)
387 {
388 if (proc->attr.function && sym->attr.intent != INTENT_IN)
389 proc->attr.implicit_pure = 0;
390
391 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
392 proc->attr.implicit_pure = 0;
393 }
394 }
395
396 if (gfc_elemental (proc))
397 {
398 /* F08:C1289. */
399 if (sym->attr.codimension
400 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
401 && CLASS_DATA (sym)->attr.codimension))
402 {
403 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
404 "procedure", sym->name, &sym->declared_at);
405 continue;
406 }
407
408 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
409 && CLASS_DATA (sym)->as))
410 {
411 gfc_error ("Argument '%s' of elemental procedure at %L must "
412 "be scalar", sym->name, &sym->declared_at);
413 continue;
414 }
415
416 if (sym->attr.allocatable
417 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
418 && CLASS_DATA (sym)->attr.allocatable))
419 {
420 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
421 "have the ALLOCATABLE attribute", sym->name,
422 &sym->declared_at);
423 continue;
424 }
425
426 if (sym->attr.pointer
427 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
428 && CLASS_DATA (sym)->attr.class_pointer))
429 {
430 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
431 "have the POINTER attribute", sym->name,
432 &sym->declared_at);
433 continue;
434 }
435
436 if (sym->attr.flavor == FL_PROCEDURE)
437 {
438 gfc_error ("Dummy procedure '%s' not allowed in elemental "
439 "procedure '%s' at %L", sym->name, proc->name,
440 &sym->declared_at);
441 continue;
442 }
443
444 if (sym->attr.intent == INTENT_UNKNOWN)
445 {
446 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
447 "have its INTENT specified", sym->name, proc->name,
448 &sym->declared_at);
449 continue;
450 }
451 }
452
453 /* Each dummy shall be specified to be scalar. */
454 if (proc->attr.proc == PROC_ST_FUNCTION)
455 {
456 if (sym->as != NULL)
457 {
458 gfc_error ("Argument '%s' of statement function at %L must "
459 "be scalar", sym->name, &sym->declared_at);
460 continue;
461 }
462
463 if (sym->ts.type == BT_CHARACTER)
464 {
465 gfc_charlen *cl = sym->ts.u.cl;
466 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
467 {
468 gfc_error ("Character-valued argument '%s' of statement "
469 "function at %L must have constant length",
470 sym->name, &sym->declared_at);
471 continue;
472 }
473 }
474 }
475 }
476 formal_arg_flag = 0;
477 }
478
479
480 /* Work function called when searching for symbols that have argument lists
481 associated with them. */
482
483 static void
484 find_arglists (gfc_symbol *sym)
485 {
486 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
487 || sym->attr.flavor == FL_DERIVED)
488 return;
489
490 resolve_formal_arglist (sym);
491 }
492
493
494 /* Given a namespace, resolve all formal argument lists within the namespace.
495 */
496
497 static void
498 resolve_formal_arglists (gfc_namespace *ns)
499 {
500 if (ns == NULL)
501 return;
502
503 gfc_traverse_ns (ns, find_arglists);
504 }
505
506
507 static void
508 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
509 {
510 gfc_try t;
511
512 /* If this namespace is not a function or an entry master function,
513 ignore it. */
514 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
515 || sym->attr.entry_master)
516 return;
517
518 /* Try to find out of what the return type is. */
519 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
520 {
521 t = gfc_set_default_type (sym->result, 0, ns);
522
523 if (t == FAILURE && !sym->result->attr.untyped)
524 {
525 if (sym->result == sym)
526 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
527 sym->name, &sym->declared_at);
528 else if (!sym->result->attr.proc_pointer)
529 gfc_error ("Result '%s' of contained function '%s' at %L has "
530 "no IMPLICIT type", sym->result->name, sym->name,
531 &sym->result->declared_at);
532 sym->result->attr.untyped = 1;
533 }
534 }
535
536 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
537 type, lists the only ways a character length value of * can be used:
538 dummy arguments of procedures, named constants, and function results
539 in external functions. Internal function results and results of module
540 procedures are not on this list, ergo, not permitted. */
541
542 if (sym->result->ts.type == BT_CHARACTER)
543 {
544 gfc_charlen *cl = sym->result->ts.u.cl;
545 if ((!cl || !cl->length) && !sym->result->ts.deferred)
546 {
547 /* See if this is a module-procedure and adapt error message
548 accordingly. */
549 bool module_proc;
550 gcc_assert (ns->parent && ns->parent->proc_name);
551 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
552
553 gfc_error ("Character-valued %s '%s' at %L must not be"
554 " assumed length",
555 module_proc ? _("module procedure")
556 : _("internal function"),
557 sym->name, &sym->declared_at);
558 }
559 }
560 }
561
562
563 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
564 introduce duplicates. */
565
566 static void
567 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
568 {
569 gfc_formal_arglist *f, *new_arglist;
570 gfc_symbol *new_sym;
571
572 for (; new_args != NULL; new_args = new_args->next)
573 {
574 new_sym = new_args->sym;
575 /* See if this arg is already in the formal argument list. */
576 for (f = proc->formal; f; f = f->next)
577 {
578 if (new_sym == f->sym)
579 break;
580 }
581
582 if (f)
583 continue;
584
585 /* Add a new argument. Argument order is not important. */
586 new_arglist = gfc_get_formal_arglist ();
587 new_arglist->sym = new_sym;
588 new_arglist->next = proc->formal;
589 proc->formal = new_arglist;
590 }
591 }
592
593
594 /* Flag the arguments that are not present in all entries. */
595
596 static void
597 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
598 {
599 gfc_formal_arglist *f, *head;
600 head = new_args;
601
602 for (f = proc->formal; f; f = f->next)
603 {
604 if (f->sym == NULL)
605 continue;
606
607 for (new_args = head; new_args; new_args = new_args->next)
608 {
609 if (new_args->sym == f->sym)
610 break;
611 }
612
613 if (new_args)
614 continue;
615
616 f->sym->attr.not_always_present = 1;
617 }
618 }
619
620
621 /* Resolve alternate entry points. If a symbol has multiple entry points we
622 create a new master symbol for the main routine, and turn the existing
623 symbol into an entry point. */
624
625 static void
626 resolve_entries (gfc_namespace *ns)
627 {
628 gfc_namespace *old_ns;
629 gfc_code *c;
630 gfc_symbol *proc;
631 gfc_entry_list *el;
632 char name[GFC_MAX_SYMBOL_LEN + 1];
633 static int master_count = 0;
634
635 if (ns->proc_name == NULL)
636 return;
637
638 /* No need to do anything if this procedure doesn't have alternate entry
639 points. */
640 if (!ns->entries)
641 return;
642
643 /* We may already have resolved alternate entry points. */
644 if (ns->proc_name->attr.entry_master)
645 return;
646
647 /* If this isn't a procedure something has gone horribly wrong. */
648 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
649
650 /* Remember the current namespace. */
651 old_ns = gfc_current_ns;
652
653 gfc_current_ns = ns;
654
655 /* Add the main entry point to the list of entry points. */
656 el = gfc_get_entry_list ();
657 el->sym = ns->proc_name;
658 el->id = 0;
659 el->next = ns->entries;
660 ns->entries = el;
661 ns->proc_name->attr.entry = 1;
662
663 /* If it is a module function, it needs to be in the right namespace
664 so that gfc_get_fake_result_decl can gather up the results. The
665 need for this arose in get_proc_name, where these beasts were
666 left in their own namespace, to keep prior references linked to
667 the entry declaration.*/
668 if (ns->proc_name->attr.function
669 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
670 el->sym->ns = ns;
671
672 /* Do the same for entries where the master is not a module
673 procedure. These are retained in the module namespace because
674 of the module procedure declaration. */
675 for (el = el->next; el; el = el->next)
676 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
677 && el->sym->attr.mod_proc)
678 el->sym->ns = ns;
679 el = ns->entries;
680
681 /* Add an entry statement for it. */
682 c = gfc_get_code ();
683 c->op = EXEC_ENTRY;
684 c->ext.entry = el;
685 c->next = ns->code;
686 ns->code = c;
687
688 /* Create a new symbol for the master function. */
689 /* Give the internal function a unique name (within this file).
690 Also include the function name so the user has some hope of figuring
691 out what is going on. */
692 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
693 master_count++, ns->proc_name->name);
694 gfc_get_ha_symbol (name, &proc);
695 gcc_assert (proc != NULL);
696
697 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
698 if (ns->proc_name->attr.subroutine)
699 gfc_add_subroutine (&proc->attr, proc->name, NULL);
700 else
701 {
702 gfc_symbol *sym;
703 gfc_typespec *ts, *fts;
704 gfc_array_spec *as, *fas;
705 gfc_add_function (&proc->attr, proc->name, NULL);
706 proc->result = proc;
707 fas = ns->entries->sym->as;
708 fas = fas ? fas : ns->entries->sym->result->as;
709 fts = &ns->entries->sym->result->ts;
710 if (fts->type == BT_UNKNOWN)
711 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
712 for (el = ns->entries->next; el; el = el->next)
713 {
714 ts = &el->sym->result->ts;
715 as = el->sym->as;
716 as = as ? as : el->sym->result->as;
717 if (ts->type == BT_UNKNOWN)
718 ts = gfc_get_default_type (el->sym->result->name, NULL);
719
720 if (! gfc_compare_types (ts, fts)
721 || (el->sym->result->attr.dimension
722 != ns->entries->sym->result->attr.dimension)
723 || (el->sym->result->attr.pointer
724 != ns->entries->sym->result->attr.pointer))
725 break;
726 else if (as && fas && ns->entries->sym->result != el->sym->result
727 && gfc_compare_array_spec (as, fas) == 0)
728 gfc_error ("Function %s at %L has entries with mismatched "
729 "array specifications", ns->entries->sym->name,
730 &ns->entries->sym->declared_at);
731 /* The characteristics need to match and thus both need to have
732 the same string length, i.e. both len=*, or both len=4.
733 Having both len=<variable> is also possible, but difficult to
734 check at compile time. */
735 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
736 && (((ts->u.cl->length && !fts->u.cl->length)
737 ||(!ts->u.cl->length && fts->u.cl->length))
738 || (ts->u.cl->length
739 && ts->u.cl->length->expr_type
740 != fts->u.cl->length->expr_type)
741 || (ts->u.cl->length
742 && ts->u.cl->length->expr_type == EXPR_CONSTANT
743 && mpz_cmp (ts->u.cl->length->value.integer,
744 fts->u.cl->length->value.integer) != 0)))
745 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
746 "entries returning variables of different "
747 "string lengths", ns->entries->sym->name,
748 &ns->entries->sym->declared_at);
749 }
750
751 if (el == NULL)
752 {
753 sym = ns->entries->sym->result;
754 /* All result types the same. */
755 proc->ts = *fts;
756 if (sym->attr.dimension)
757 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
758 if (sym->attr.pointer)
759 gfc_add_pointer (&proc->attr, NULL);
760 }
761 else
762 {
763 /* Otherwise the result will be passed through a union by
764 reference. */
765 proc->attr.mixed_entry_master = 1;
766 for (el = ns->entries; el; el = el->next)
767 {
768 sym = el->sym->result;
769 if (sym->attr.dimension)
770 {
771 if (el == ns->entries)
772 gfc_error ("FUNCTION result %s can't be an array in "
773 "FUNCTION %s at %L", sym->name,
774 ns->entries->sym->name, &sym->declared_at);
775 else
776 gfc_error ("ENTRY result %s can't be an array in "
777 "FUNCTION %s at %L", sym->name,
778 ns->entries->sym->name, &sym->declared_at);
779 }
780 else if (sym->attr.pointer)
781 {
782 if (el == ns->entries)
783 gfc_error ("FUNCTION result %s can't be a POINTER in "
784 "FUNCTION %s at %L", sym->name,
785 ns->entries->sym->name, &sym->declared_at);
786 else
787 gfc_error ("ENTRY result %s can't be a POINTER in "
788 "FUNCTION %s at %L", sym->name,
789 ns->entries->sym->name, &sym->declared_at);
790 }
791 else
792 {
793 ts = &sym->ts;
794 if (ts->type == BT_UNKNOWN)
795 ts = gfc_get_default_type (sym->name, NULL);
796 switch (ts->type)
797 {
798 case BT_INTEGER:
799 if (ts->kind == gfc_default_integer_kind)
800 sym = NULL;
801 break;
802 case BT_REAL:
803 if (ts->kind == gfc_default_real_kind
804 || ts->kind == gfc_default_double_kind)
805 sym = NULL;
806 break;
807 case BT_COMPLEX:
808 if (ts->kind == gfc_default_complex_kind)
809 sym = NULL;
810 break;
811 case BT_LOGICAL:
812 if (ts->kind == gfc_default_logical_kind)
813 sym = NULL;
814 break;
815 case BT_UNKNOWN:
816 /* We will issue error elsewhere. */
817 sym = NULL;
818 break;
819 default:
820 break;
821 }
822 if (sym)
823 {
824 if (el == ns->entries)
825 gfc_error ("FUNCTION result %s can't be of type %s "
826 "in FUNCTION %s at %L", sym->name,
827 gfc_typename (ts), ns->entries->sym->name,
828 &sym->declared_at);
829 else
830 gfc_error ("ENTRY result %s can't be of type %s "
831 "in FUNCTION %s at %L", sym->name,
832 gfc_typename (ts), ns->entries->sym->name,
833 &sym->declared_at);
834 }
835 }
836 }
837 }
838 }
839 proc->attr.access = ACCESS_PRIVATE;
840 proc->attr.entry_master = 1;
841
842 /* Merge all the entry point arguments. */
843 for (el = ns->entries; el; el = el->next)
844 merge_argument_lists (proc, el->sym->formal);
845
846 /* Check the master formal arguments for any that are not
847 present in all entry points. */
848 for (el = ns->entries; el; el = el->next)
849 check_argument_lists (proc, el->sym->formal);
850
851 /* Use the master function for the function body. */
852 ns->proc_name = proc;
853
854 /* Finalize the new symbols. */
855 gfc_commit_symbols ();
856
857 /* Restore the original namespace. */
858 gfc_current_ns = old_ns;
859 }
860
861
862 /* Resolve common variables. */
863 static void
864 resolve_common_vars (gfc_symbol *sym, bool named_common)
865 {
866 gfc_symbol *csym = sym;
867
868 for (; csym; csym = csym->common_next)
869 {
870 if (csym->value || csym->attr.data)
871 {
872 if (!csym->ns->is_block_data)
873 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
874 "but only in BLOCK DATA initialization is "
875 "allowed", csym->name, &csym->declared_at);
876 else if (!named_common)
877 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
878 "in a blank COMMON but initialization is only "
879 "allowed in named common blocks", csym->name,
880 &csym->declared_at);
881 }
882
883 if (csym->ts.type != BT_DERIVED)
884 continue;
885
886 if (!(csym->ts.u.derived->attr.sequence
887 || csym->ts.u.derived->attr.is_bind_c))
888 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
889 "has neither the SEQUENCE nor the BIND(C) "
890 "attribute", csym->name, &csym->declared_at);
891 if (csym->ts.u.derived->attr.alloc_comp)
892 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
893 "has an ultimate component that is "
894 "allocatable", csym->name, &csym->declared_at);
895 if (gfc_has_default_initializer (csym->ts.u.derived))
896 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
897 "may not have default initializer", csym->name,
898 &csym->declared_at);
899
900 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
901 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
902 }
903 }
904
905 /* Resolve common blocks. */
906 static void
907 resolve_common_blocks (gfc_symtree *common_root)
908 {
909 gfc_symbol *sym;
910
911 if (common_root == NULL)
912 return;
913
914 if (common_root->left)
915 resolve_common_blocks (common_root->left);
916 if (common_root->right)
917 resolve_common_blocks (common_root->right);
918
919 resolve_common_vars (common_root->n.common->head, true);
920
921 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
922 if (sym == NULL)
923 return;
924
925 if (sym->attr.flavor == FL_PARAMETER)
926 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
927 sym->name, &common_root->n.common->where, &sym->declared_at);
928
929 if (sym->attr.external)
930 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
931 sym->name, &common_root->n.common->where);
932
933 if (sym->attr.intrinsic)
934 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
935 sym->name, &common_root->n.common->where);
936 else if (sym->attr.result
937 || gfc_is_function_return_value (sym, gfc_current_ns))
938 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
939 "that is also a function result", sym->name,
940 &common_root->n.common->where);
941 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
942 && sym->attr.proc != PROC_ST_FUNCTION)
943 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
944 "that is also a global procedure", sym->name,
945 &common_root->n.common->where);
946 }
947
948
949 /* Resolve contained function types. Because contained functions can call one
950 another, they have to be worked out before any of the contained procedures
951 can be resolved.
952
953 The good news is that if a function doesn't already have a type, the only
954 way it can get one is through an IMPLICIT type or a RESULT variable, because
955 by definition contained functions are contained namespace they're contained
956 in, not in a sibling or parent namespace. */
957
958 static void
959 resolve_contained_functions (gfc_namespace *ns)
960 {
961 gfc_namespace *child;
962 gfc_entry_list *el;
963
964 resolve_formal_arglists (ns);
965
966 for (child = ns->contained; child; child = child->sibling)
967 {
968 /* Resolve alternate entry points first. */
969 resolve_entries (child);
970
971 /* Then check function return types. */
972 resolve_contained_fntype (child->proc_name, child);
973 for (el = child->entries; el; el = el->next)
974 resolve_contained_fntype (el->sym, child);
975 }
976 }
977
978
979 static gfc_try resolve_fl_derived0 (gfc_symbol *sym);
980
981
982 /* Resolve all of the elements of a structure constructor and make sure that
983 the types are correct. The 'init' flag indicates that the given
984 constructor is an initializer. */
985
986 static gfc_try
987 resolve_structure_cons (gfc_expr *expr, int init)
988 {
989 gfc_constructor *cons;
990 gfc_component *comp;
991 gfc_try t;
992 symbol_attribute a;
993
994 t = SUCCESS;
995
996 if (expr->ts.type == BT_DERIVED)
997 resolve_fl_derived0 (expr->ts.u.derived);
998
999 cons = gfc_constructor_first (expr->value.constructor);
1000
1001 /* See if the user is trying to invoke a structure constructor for one of
1002 the iso_c_binding derived types. */
1003 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
1004 && expr->ts.u.derived->ts.is_iso_c && cons
1005 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
1006 {
1007 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
1008 expr->ts.u.derived->name, &(expr->where));
1009 return FAILURE;
1010 }
1011
1012 /* Return if structure constructor is c_null_(fun)prt. */
1013 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
1014 && expr->ts.u.derived->ts.is_iso_c && cons
1015 && cons->expr && cons->expr->expr_type == EXPR_NULL)
1016 return SUCCESS;
1017
1018 /* A constructor may have references if it is the result of substituting a
1019 parameter variable. In this case we just pull out the component we
1020 want. */
1021 if (expr->ref)
1022 comp = expr->ref->u.c.sym->components;
1023 else
1024 comp = expr->ts.u.derived->components;
1025
1026 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1027 {
1028 int rank;
1029
1030 if (!cons->expr)
1031 continue;
1032
1033 if (gfc_resolve_expr (cons->expr) == FAILURE)
1034 {
1035 t = FAILURE;
1036 continue;
1037 }
1038
1039 rank = comp->as ? comp->as->rank : 0;
1040 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1041 && (comp->attr.allocatable || cons->expr->rank))
1042 {
1043 gfc_error ("The rank of the element in the structure "
1044 "constructor at %L does not match that of the "
1045 "component (%d/%d)", &cons->expr->where,
1046 cons->expr->rank, rank);
1047 t = FAILURE;
1048 }
1049
1050 /* If we don't have the right type, try to convert it. */
1051
1052 if (!comp->attr.proc_pointer &&
1053 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1054 {
1055 t = FAILURE;
1056 if (strcmp (comp->name, "_extends") == 0)
1057 {
1058 /* Can afford to be brutal with the _extends initializer.
1059 The derived type can get lost because it is PRIVATE
1060 but it is not usage constrained by the standard. */
1061 cons->expr->ts = comp->ts;
1062 t = SUCCESS;
1063 }
1064 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1065 gfc_error ("The element in the structure constructor at %L, "
1066 "for pointer component '%s', is %s but should be %s",
1067 &cons->expr->where, comp->name,
1068 gfc_basic_typename (cons->expr->ts.type),
1069 gfc_basic_typename (comp->ts.type));
1070 else
1071 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1072 }
1073
1074 /* For strings, the length of the constructor should be the same as
1075 the one of the structure, ensure this if the lengths are known at
1076 compile time and when we are dealing with PARAMETER or structure
1077 constructors. */
1078 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1079 && comp->ts.u.cl->length
1080 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1081 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1082 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1083 && cons->expr->rank != 0
1084 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1085 comp->ts.u.cl->length->value.integer) != 0)
1086 {
1087 if (cons->expr->expr_type == EXPR_VARIABLE
1088 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1089 {
1090 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1091 to make use of the gfc_resolve_character_array_constructor
1092 machinery. The expression is later simplified away to
1093 an array of string literals. */
1094 gfc_expr *para = cons->expr;
1095 cons->expr = gfc_get_expr ();
1096 cons->expr->ts = para->ts;
1097 cons->expr->where = para->where;
1098 cons->expr->expr_type = EXPR_ARRAY;
1099 cons->expr->rank = para->rank;
1100 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1101 gfc_constructor_append_expr (&cons->expr->value.constructor,
1102 para, &cons->expr->where);
1103 }
1104 if (cons->expr->expr_type == EXPR_ARRAY)
1105 {
1106 gfc_constructor *p;
1107 p = gfc_constructor_first (cons->expr->value.constructor);
1108 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1109 {
1110 gfc_charlen *cl, *cl2;
1111
1112 cl2 = NULL;
1113 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1114 {
1115 if (cl == cons->expr->ts.u.cl)
1116 break;
1117 cl2 = cl;
1118 }
1119
1120 gcc_assert (cl);
1121
1122 if (cl2)
1123 cl2->next = cl->next;
1124
1125 gfc_free_expr (cl->length);
1126 free (cl);
1127 }
1128
1129 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1130 cons->expr->ts.u.cl->length_from_typespec = true;
1131 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1132 gfc_resolve_character_array_constructor (cons->expr);
1133 }
1134 }
1135
1136 if (cons->expr->expr_type == EXPR_NULL
1137 && !(comp->attr.pointer || comp->attr.allocatable
1138 || comp->attr.proc_pointer
1139 || (comp->ts.type == BT_CLASS
1140 && (CLASS_DATA (comp)->attr.class_pointer
1141 || CLASS_DATA (comp)->attr.allocatable))))
1142 {
1143 t = FAILURE;
1144 gfc_error ("The NULL in the structure constructor at %L is "
1145 "being applied to component '%s', which is neither "
1146 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1147 comp->name);
1148 }
1149
1150 if (comp->attr.proc_pointer && comp->ts.interface)
1151 {
1152 /* Check procedure pointer interface. */
1153 gfc_symbol *s2 = NULL;
1154 gfc_component *c2;
1155 const char *name;
1156 char err[200];
1157
1158 if (gfc_is_proc_ptr_comp (cons->expr, &c2))
1159 {
1160 s2 = c2->ts.interface;
1161 name = c2->name;
1162 }
1163 else if (cons->expr->expr_type == EXPR_FUNCTION)
1164 {
1165 s2 = cons->expr->symtree->n.sym->result;
1166 name = cons->expr->symtree->n.sym->result->name;
1167 }
1168 else if (cons->expr->expr_type != EXPR_NULL)
1169 {
1170 s2 = cons->expr->symtree->n.sym;
1171 name = cons->expr->symtree->n.sym->name;
1172 }
1173
1174 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1175 err, sizeof (err), NULL, NULL))
1176 {
1177 gfc_error ("Interface mismatch for procedure-pointer component "
1178 "'%s' in structure constructor at %L: %s",
1179 comp->name, &cons->expr->where, err);
1180 return FAILURE;
1181 }
1182 }
1183
1184 if (!comp->attr.pointer || comp->attr.proc_pointer
1185 || cons->expr->expr_type == EXPR_NULL)
1186 continue;
1187
1188 a = gfc_expr_attr (cons->expr);
1189
1190 if (!a.pointer && !a.target)
1191 {
1192 t = FAILURE;
1193 gfc_error ("The element in the structure constructor at %L, "
1194 "for pointer component '%s' should be a POINTER or "
1195 "a TARGET", &cons->expr->where, comp->name);
1196 }
1197
1198 if (init)
1199 {
1200 /* F08:C461. Additional checks for pointer initialization. */
1201 if (a.allocatable)
1202 {
1203 t = FAILURE;
1204 gfc_error ("Pointer initialization target at %L "
1205 "must not be ALLOCATABLE ", &cons->expr->where);
1206 }
1207 if (!a.save)
1208 {
1209 t = FAILURE;
1210 gfc_error ("Pointer initialization target at %L "
1211 "must have the SAVE attribute", &cons->expr->where);
1212 }
1213 }
1214
1215 /* F2003, C1272 (3). */
1216 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1217 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1218 || gfc_is_coindexed (cons->expr)))
1219 {
1220 t = FAILURE;
1221 gfc_error ("Invalid expression in the structure constructor for "
1222 "pointer component '%s' at %L in PURE procedure",
1223 comp->name, &cons->expr->where);
1224 }
1225
1226 if (gfc_implicit_pure (NULL)
1227 && cons->expr->expr_type == EXPR_VARIABLE
1228 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1229 || gfc_is_coindexed (cons->expr)))
1230 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1231
1232 }
1233
1234 return t;
1235 }
1236
1237
1238 /****************** Expression name resolution ******************/
1239
1240 /* Returns 0 if a symbol was not declared with a type or
1241 attribute declaration statement, nonzero otherwise. */
1242
1243 static int
1244 was_declared (gfc_symbol *sym)
1245 {
1246 symbol_attribute a;
1247
1248 a = sym->attr;
1249
1250 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1251 return 1;
1252
1253 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1254 || a.optional || a.pointer || a.save || a.target || a.volatile_
1255 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1256 || a.asynchronous || a.codimension)
1257 return 1;
1258
1259 return 0;
1260 }
1261
1262
1263 /* Determine if a symbol is generic or not. */
1264
1265 static int
1266 generic_sym (gfc_symbol *sym)
1267 {
1268 gfc_symbol *s;
1269
1270 if (sym->attr.generic ||
1271 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1272 return 1;
1273
1274 if (was_declared (sym) || sym->ns->parent == NULL)
1275 return 0;
1276
1277 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1278
1279 if (s != NULL)
1280 {
1281 if (s == sym)
1282 return 0;
1283 else
1284 return generic_sym (s);
1285 }
1286
1287 return 0;
1288 }
1289
1290
1291 /* Determine if a symbol is specific or not. */
1292
1293 static int
1294 specific_sym (gfc_symbol *sym)
1295 {
1296 gfc_symbol *s;
1297
1298 if (sym->attr.if_source == IFSRC_IFBODY
1299 || sym->attr.proc == PROC_MODULE
1300 || sym->attr.proc == PROC_INTERNAL
1301 || sym->attr.proc == PROC_ST_FUNCTION
1302 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1303 || sym->attr.external)
1304 return 1;
1305
1306 if (was_declared (sym) || sym->ns->parent == NULL)
1307 return 0;
1308
1309 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1310
1311 return (s == NULL) ? 0 : specific_sym (s);
1312 }
1313
1314
1315 /* Figure out if the procedure is specific, generic or unknown. */
1316
1317 typedef enum
1318 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1319 proc_type;
1320
1321 static proc_type
1322 procedure_kind (gfc_symbol *sym)
1323 {
1324 if (generic_sym (sym))
1325 return PTYPE_GENERIC;
1326
1327 if (specific_sym (sym))
1328 return PTYPE_SPECIFIC;
1329
1330 return PTYPE_UNKNOWN;
1331 }
1332
1333 /* Check references to assumed size arrays. The flag need_full_assumed_size
1334 is nonzero when matching actual arguments. */
1335
1336 static int need_full_assumed_size = 0;
1337
1338 static bool
1339 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1340 {
1341 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1342 return false;
1343
1344 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1345 What should it be? */
1346 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1347 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1348 && (e->ref->u.ar.type == AR_FULL))
1349 {
1350 gfc_error ("The upper bound in the last dimension must "
1351 "appear in the reference to the assumed size "
1352 "array '%s' at %L", sym->name, &e->where);
1353 return true;
1354 }
1355 return false;
1356 }
1357
1358
1359 /* Look for bad assumed size array references in argument expressions
1360 of elemental and array valued intrinsic procedures. Since this is
1361 called from procedure resolution functions, it only recurses at
1362 operators. */
1363
1364 static bool
1365 resolve_assumed_size_actual (gfc_expr *e)
1366 {
1367 if (e == NULL)
1368 return false;
1369
1370 switch (e->expr_type)
1371 {
1372 case EXPR_VARIABLE:
1373 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1374 return true;
1375 break;
1376
1377 case EXPR_OP:
1378 if (resolve_assumed_size_actual (e->value.op.op1)
1379 || resolve_assumed_size_actual (e->value.op.op2))
1380 return true;
1381 break;
1382
1383 default:
1384 break;
1385 }
1386 return false;
1387 }
1388
1389
1390 /* Check a generic procedure, passed as an actual argument, to see if
1391 there is a matching specific name. If none, it is an error, and if
1392 more than one, the reference is ambiguous. */
1393 static int
1394 count_specific_procs (gfc_expr *e)
1395 {
1396 int n;
1397 gfc_interface *p;
1398 gfc_symbol *sym;
1399
1400 n = 0;
1401 sym = e->symtree->n.sym;
1402
1403 for (p = sym->generic; p; p = p->next)
1404 if (strcmp (sym->name, p->sym->name) == 0)
1405 {
1406 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1407 sym->name);
1408 n++;
1409 }
1410
1411 if (n > 1)
1412 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1413 &e->where);
1414
1415 if (n == 0)
1416 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1417 "argument at %L", sym->name, &e->where);
1418
1419 return n;
1420 }
1421
1422
1423 /* See if a call to sym could possibly be a not allowed RECURSION because of
1424 a missing RECURSIVE declaration. This means that either sym is the current
1425 context itself, or sym is the parent of a contained procedure calling its
1426 non-RECURSIVE containing procedure.
1427 This also works if sym is an ENTRY. */
1428
1429 static bool
1430 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1431 {
1432 gfc_symbol* proc_sym;
1433 gfc_symbol* context_proc;
1434 gfc_namespace* real_context;
1435
1436 if (sym->attr.flavor == FL_PROGRAM
1437 || sym->attr.flavor == FL_DERIVED)
1438 return false;
1439
1440 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1441
1442 /* If we've got an ENTRY, find real procedure. */
1443 if (sym->attr.entry && sym->ns->entries)
1444 proc_sym = sym->ns->entries->sym;
1445 else
1446 proc_sym = sym;
1447
1448 /* If sym is RECURSIVE, all is well of course. */
1449 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1450 return false;
1451
1452 /* Find the context procedure's "real" symbol if it has entries.
1453 We look for a procedure symbol, so recurse on the parents if we don't
1454 find one (like in case of a BLOCK construct). */
1455 for (real_context = context; ; real_context = real_context->parent)
1456 {
1457 /* We should find something, eventually! */
1458 gcc_assert (real_context);
1459
1460 context_proc = (real_context->entries ? real_context->entries->sym
1461 : real_context->proc_name);
1462
1463 /* In some special cases, there may not be a proc_name, like for this
1464 invalid code:
1465 real(bad_kind()) function foo () ...
1466 when checking the call to bad_kind ().
1467 In these cases, we simply return here and assume that the
1468 call is ok. */
1469 if (!context_proc)
1470 return false;
1471
1472 if (context_proc->attr.flavor != FL_LABEL)
1473 break;
1474 }
1475
1476 /* A call from sym's body to itself is recursion, of course. */
1477 if (context_proc == proc_sym)
1478 return true;
1479
1480 /* The same is true if context is a contained procedure and sym the
1481 containing one. */
1482 if (context_proc->attr.contained)
1483 {
1484 gfc_symbol* parent_proc;
1485
1486 gcc_assert (context->parent);
1487 parent_proc = (context->parent->entries ? context->parent->entries->sym
1488 : context->parent->proc_name);
1489
1490 if (parent_proc == proc_sym)
1491 return true;
1492 }
1493
1494 return false;
1495 }
1496
1497
1498 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1499 its typespec and formal argument list. */
1500
1501 gfc_try
1502 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1503 {
1504 gfc_intrinsic_sym* isym = NULL;
1505 const char* symstd;
1506
1507 if (sym->formal)
1508 return SUCCESS;
1509
1510 /* Already resolved. */
1511 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1512 return SUCCESS;
1513
1514 /* We already know this one is an intrinsic, so we don't call
1515 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1516 gfc_find_subroutine directly to check whether it is a function or
1517 subroutine. */
1518
1519 if (sym->intmod_sym_id)
1520 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1521 else if (!sym->attr.subroutine)
1522 isym = gfc_find_function (sym->name);
1523
1524 if (isym)
1525 {
1526 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1527 && !sym->attr.implicit_type)
1528 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1529 " ignored", sym->name, &sym->declared_at);
1530
1531 if (!sym->attr.function &&
1532 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1533 return FAILURE;
1534
1535 sym->ts = isym->ts;
1536 }
1537 else if ((isym = gfc_find_subroutine (sym->name)))
1538 {
1539 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1540 {
1541 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1542 " specifier", sym->name, &sym->declared_at);
1543 return FAILURE;
1544 }
1545
1546 if (!sym->attr.subroutine &&
1547 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1548 return FAILURE;
1549 }
1550 else
1551 {
1552 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1553 &sym->declared_at);
1554 return FAILURE;
1555 }
1556
1557 gfc_copy_formal_args_intr (sym, isym);
1558
1559 /* Check it is actually available in the standard settings. */
1560 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1561 == FAILURE)
1562 {
1563 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1564 " available in the current standard settings but %s. Use"
1565 " an appropriate -std=* option or enable -fall-intrinsics"
1566 " in order to use it.",
1567 sym->name, &sym->declared_at, symstd);
1568 return FAILURE;
1569 }
1570
1571 return SUCCESS;
1572 }
1573
1574
1575 /* Resolve a procedure expression, like passing it to a called procedure or as
1576 RHS for a procedure pointer assignment. */
1577
1578 static gfc_try
1579 resolve_procedure_expression (gfc_expr* expr)
1580 {
1581 gfc_symbol* sym;
1582
1583 if (expr->expr_type != EXPR_VARIABLE)
1584 return SUCCESS;
1585 gcc_assert (expr->symtree);
1586
1587 sym = expr->symtree->n.sym;
1588
1589 if (sym->attr.intrinsic)
1590 gfc_resolve_intrinsic (sym, &expr->where);
1591
1592 if (sym->attr.flavor != FL_PROCEDURE
1593 || (sym->attr.function && sym->result == sym))
1594 return SUCCESS;
1595
1596 /* A non-RECURSIVE procedure that is used as procedure expression within its
1597 own body is in danger of being called recursively. */
1598 if (is_illegal_recursion (sym, gfc_current_ns))
1599 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1600 " itself recursively. Declare it RECURSIVE or use"
1601 " -frecursive", sym->name, &expr->where);
1602
1603 return SUCCESS;
1604 }
1605
1606
1607 /* Resolve an actual argument list. Most of the time, this is just
1608 resolving the expressions in the list.
1609 The exception is that we sometimes have to decide whether arguments
1610 that look like procedure arguments are really simple variable
1611 references. */
1612
1613 static gfc_try
1614 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1615 bool no_formal_args)
1616 {
1617 gfc_symbol *sym;
1618 gfc_symtree *parent_st;
1619 gfc_expr *e;
1620 int save_need_full_assumed_size;
1621 gfc_try return_value = FAILURE;
1622 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1623
1624 actual_arg = true;
1625 first_actual_arg = true;
1626
1627 for (; arg; arg = arg->next)
1628 {
1629 e = arg->expr;
1630 if (e == NULL)
1631 {
1632 /* Check the label is a valid branching target. */
1633 if (arg->label)
1634 {
1635 if (arg->label->defined == ST_LABEL_UNKNOWN)
1636 {
1637 gfc_error ("Label %d referenced at %L is never defined",
1638 arg->label->value, &arg->label->where);
1639 goto cleanup;
1640 }
1641 }
1642 first_actual_arg = false;
1643 continue;
1644 }
1645
1646 if (e->expr_type == EXPR_VARIABLE
1647 && e->symtree->n.sym->attr.generic
1648 && no_formal_args
1649 && count_specific_procs (e) != 1)
1650 goto cleanup;
1651
1652 if (e->ts.type != BT_PROCEDURE)
1653 {
1654 save_need_full_assumed_size = need_full_assumed_size;
1655 if (e->expr_type != EXPR_VARIABLE)
1656 need_full_assumed_size = 0;
1657 if (gfc_resolve_expr (e) != SUCCESS)
1658 goto cleanup;
1659 need_full_assumed_size = save_need_full_assumed_size;
1660 goto argument_list;
1661 }
1662
1663 /* See if the expression node should really be a variable reference. */
1664
1665 sym = e->symtree->n.sym;
1666
1667 if (sym->attr.flavor == FL_PROCEDURE
1668 || sym->attr.intrinsic
1669 || sym->attr.external)
1670 {
1671 int actual_ok;
1672
1673 /* If a procedure is not already determined to be something else
1674 check if it is intrinsic. */
1675 if (!sym->attr.intrinsic
1676 && !(sym->attr.external || sym->attr.use_assoc
1677 || sym->attr.if_source == IFSRC_IFBODY)
1678 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1679 sym->attr.intrinsic = 1;
1680
1681 if (sym->attr.proc == PROC_ST_FUNCTION)
1682 {
1683 gfc_error ("Statement function '%s' at %L is not allowed as an "
1684 "actual argument", sym->name, &e->where);
1685 }
1686
1687 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1688 sym->attr.subroutine);
1689 if (sym->attr.intrinsic && actual_ok == 0)
1690 {
1691 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1692 "actual argument", sym->name, &e->where);
1693 }
1694
1695 if (sym->attr.contained && !sym->attr.use_assoc
1696 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1697 {
1698 if (gfc_notify_std (GFC_STD_F2008,
1699 "Internal procedure '%s' is"
1700 " used as actual argument at %L",
1701 sym->name, &e->where) == FAILURE)
1702 goto cleanup;
1703 }
1704
1705 if (sym->attr.elemental && !sym->attr.intrinsic)
1706 {
1707 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1708 "allowed as an actual argument at %L", sym->name,
1709 &e->where);
1710 }
1711
1712 /* Check if a generic interface has a specific procedure
1713 with the same name before emitting an error. */
1714 if (sym->attr.generic && count_specific_procs (e) != 1)
1715 goto cleanup;
1716
1717 /* Just in case a specific was found for the expression. */
1718 sym = e->symtree->n.sym;
1719
1720 /* If the symbol is the function that names the current (or
1721 parent) scope, then we really have a variable reference. */
1722
1723 if (gfc_is_function_return_value (sym, sym->ns))
1724 goto got_variable;
1725
1726 /* If all else fails, see if we have a specific intrinsic. */
1727 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1728 {
1729 gfc_intrinsic_sym *isym;
1730
1731 isym = gfc_find_function (sym->name);
1732 if (isym == NULL || !isym->specific)
1733 {
1734 gfc_error ("Unable to find a specific INTRINSIC procedure "
1735 "for the reference '%s' at %L", sym->name,
1736 &e->where);
1737 goto cleanup;
1738 }
1739 sym->ts = isym->ts;
1740 sym->attr.intrinsic = 1;
1741 sym->attr.function = 1;
1742 }
1743
1744 if (gfc_resolve_expr (e) == FAILURE)
1745 goto cleanup;
1746 goto argument_list;
1747 }
1748
1749 /* See if the name is a module procedure in a parent unit. */
1750
1751 if (was_declared (sym) || sym->ns->parent == NULL)
1752 goto got_variable;
1753
1754 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1755 {
1756 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1757 goto cleanup;
1758 }
1759
1760 if (parent_st == NULL)
1761 goto got_variable;
1762
1763 sym = parent_st->n.sym;
1764 e->symtree = parent_st; /* Point to the right thing. */
1765
1766 if (sym->attr.flavor == FL_PROCEDURE
1767 || sym->attr.intrinsic
1768 || sym->attr.external)
1769 {
1770 if (gfc_resolve_expr (e) == FAILURE)
1771 goto cleanup;
1772 goto argument_list;
1773 }
1774
1775 got_variable:
1776 e->expr_type = EXPR_VARIABLE;
1777 e->ts = sym->ts;
1778 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1779 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1780 && CLASS_DATA (sym)->as))
1781 {
1782 e->rank = sym->ts.type == BT_CLASS
1783 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1784 e->ref = gfc_get_ref ();
1785 e->ref->type = REF_ARRAY;
1786 e->ref->u.ar.type = AR_FULL;
1787 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1788 ? CLASS_DATA (sym)->as : sym->as;
1789 }
1790
1791 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1792 primary.c (match_actual_arg). If above code determines that it
1793 is a variable instead, it needs to be resolved as it was not
1794 done at the beginning of this function. */
1795 save_need_full_assumed_size = need_full_assumed_size;
1796 if (e->expr_type != EXPR_VARIABLE)
1797 need_full_assumed_size = 0;
1798 if (gfc_resolve_expr (e) != SUCCESS)
1799 goto cleanup;
1800 need_full_assumed_size = save_need_full_assumed_size;
1801
1802 argument_list:
1803 /* Check argument list functions %VAL, %LOC and %REF. There is
1804 nothing to do for %REF. */
1805 if (arg->name && arg->name[0] == '%')
1806 {
1807 if (strncmp ("%VAL", arg->name, 4) == 0)
1808 {
1809 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1810 {
1811 gfc_error ("By-value argument at %L is not of numeric "
1812 "type", &e->where);
1813 goto cleanup;
1814 }
1815
1816 if (e->rank)
1817 {
1818 gfc_error ("By-value argument at %L cannot be an array or "
1819 "an array section", &e->where);
1820 goto cleanup;
1821 }
1822
1823 /* Intrinsics are still PROC_UNKNOWN here. However,
1824 since same file external procedures are not resolvable
1825 in gfortran, it is a good deal easier to leave them to
1826 intrinsic.c. */
1827 if (ptype != PROC_UNKNOWN
1828 && ptype != PROC_DUMMY
1829 && ptype != PROC_EXTERNAL
1830 && ptype != PROC_MODULE)
1831 {
1832 gfc_error ("By-value argument at %L is not allowed "
1833 "in this context", &e->where);
1834 goto cleanup;
1835 }
1836 }
1837
1838 /* Statement functions have already been excluded above. */
1839 else if (strncmp ("%LOC", arg->name, 4) == 0
1840 && e->ts.type == BT_PROCEDURE)
1841 {
1842 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1843 {
1844 gfc_error ("Passing internal procedure at %L by location "
1845 "not allowed", &e->where);
1846 goto cleanup;
1847 }
1848 }
1849 }
1850
1851 /* Fortran 2008, C1237. */
1852 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1853 && gfc_has_ultimate_pointer (e))
1854 {
1855 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1856 "component", &e->where);
1857 goto cleanup;
1858 }
1859
1860 first_actual_arg = false;
1861 }
1862
1863 return_value = SUCCESS;
1864
1865 cleanup:
1866 actual_arg = actual_arg_sav;
1867 first_actual_arg = first_actual_arg_sav;
1868
1869 return return_value;
1870 }
1871
1872
1873 /* Do the checks of the actual argument list that are specific to elemental
1874 procedures. If called with c == NULL, we have a function, otherwise if
1875 expr == NULL, we have a subroutine. */
1876
1877 static gfc_try
1878 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1879 {
1880 gfc_actual_arglist *arg0;
1881 gfc_actual_arglist *arg;
1882 gfc_symbol *esym = NULL;
1883 gfc_intrinsic_sym *isym = NULL;
1884 gfc_expr *e = NULL;
1885 gfc_intrinsic_arg *iformal = NULL;
1886 gfc_formal_arglist *eformal = NULL;
1887 bool formal_optional = false;
1888 bool set_by_optional = false;
1889 int i;
1890 int rank = 0;
1891
1892 /* Is this an elemental procedure? */
1893 if (expr && expr->value.function.actual != NULL)
1894 {
1895 if (expr->value.function.esym != NULL
1896 && expr->value.function.esym->attr.elemental)
1897 {
1898 arg0 = expr->value.function.actual;
1899 esym = expr->value.function.esym;
1900 }
1901 else if (expr->value.function.isym != NULL
1902 && expr->value.function.isym->elemental)
1903 {
1904 arg0 = expr->value.function.actual;
1905 isym = expr->value.function.isym;
1906 }
1907 else
1908 return SUCCESS;
1909 }
1910 else if (c && c->ext.actual != NULL)
1911 {
1912 arg0 = c->ext.actual;
1913
1914 if (c->resolved_sym)
1915 esym = c->resolved_sym;
1916 else
1917 esym = c->symtree->n.sym;
1918 gcc_assert (esym);
1919
1920 if (!esym->attr.elemental)
1921 return SUCCESS;
1922 }
1923 else
1924 return SUCCESS;
1925
1926 /* The rank of an elemental is the rank of its array argument(s). */
1927 for (arg = arg0; arg; arg = arg->next)
1928 {
1929 if (arg->expr != NULL && arg->expr->rank != 0)
1930 {
1931 rank = arg->expr->rank;
1932 if (arg->expr->expr_type == EXPR_VARIABLE
1933 && arg->expr->symtree->n.sym->attr.optional)
1934 set_by_optional = true;
1935
1936 /* Function specific; set the result rank and shape. */
1937 if (expr)
1938 {
1939 expr->rank = rank;
1940 if (!expr->shape && arg->expr->shape)
1941 {
1942 expr->shape = gfc_get_shape (rank);
1943 for (i = 0; i < rank; i++)
1944 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1945 }
1946 }
1947 break;
1948 }
1949 }
1950
1951 /* If it is an array, it shall not be supplied as an actual argument
1952 to an elemental procedure unless an array of the same rank is supplied
1953 as an actual argument corresponding to a nonoptional dummy argument of
1954 that elemental procedure(12.4.1.5). */
1955 formal_optional = false;
1956 if (isym)
1957 iformal = isym->formal;
1958 else
1959 eformal = esym->formal;
1960
1961 for (arg = arg0; arg; arg = arg->next)
1962 {
1963 if (eformal)
1964 {
1965 if (eformal->sym && eformal->sym->attr.optional)
1966 formal_optional = true;
1967 eformal = eformal->next;
1968 }
1969 else if (isym && iformal)
1970 {
1971 if (iformal->optional)
1972 formal_optional = true;
1973 iformal = iformal->next;
1974 }
1975 else if (isym)
1976 formal_optional = true;
1977
1978 if (pedantic && arg->expr != NULL
1979 && arg->expr->expr_type == EXPR_VARIABLE
1980 && arg->expr->symtree->n.sym->attr.optional
1981 && formal_optional
1982 && arg->expr->rank
1983 && (set_by_optional || arg->expr->rank != rank)
1984 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1985 {
1986 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1987 "MISSING, it cannot be the actual argument of an "
1988 "ELEMENTAL procedure unless there is a non-optional "
1989 "argument with the same rank (12.4.1.5)",
1990 arg->expr->symtree->n.sym->name, &arg->expr->where);
1991 }
1992 }
1993
1994 for (arg = arg0; arg; arg = arg->next)
1995 {
1996 if (arg->expr == NULL || arg->expr->rank == 0)
1997 continue;
1998
1999 /* Being elemental, the last upper bound of an assumed size array
2000 argument must be present. */
2001 if (resolve_assumed_size_actual (arg->expr))
2002 return FAILURE;
2003
2004 /* Elemental procedure's array actual arguments must conform. */
2005 if (e != NULL)
2006 {
2007 if (gfc_check_conformance (arg->expr, e,
2008 "elemental procedure") == FAILURE)
2009 return FAILURE;
2010 }
2011 else
2012 e = arg->expr;
2013 }
2014
2015 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2016 is an array, the intent inout/out variable needs to be also an array. */
2017 if (rank > 0 && esym && expr == NULL)
2018 for (eformal = esym->formal, arg = arg0; arg && eformal;
2019 arg = arg->next, eformal = eformal->next)
2020 if ((eformal->sym->attr.intent == INTENT_OUT
2021 || eformal->sym->attr.intent == INTENT_INOUT)
2022 && arg->expr && arg->expr->rank == 0)
2023 {
2024 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2025 "ELEMENTAL subroutine '%s' is a scalar, but another "
2026 "actual argument is an array", &arg->expr->where,
2027 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2028 : "INOUT", eformal->sym->name, esym->name);
2029 return FAILURE;
2030 }
2031 return SUCCESS;
2032 }
2033
2034
2035 /* This function does the checking of references to global procedures
2036 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2037 77 and 95 standards. It checks for a gsymbol for the name, making
2038 one if it does not already exist. If it already exists, then the
2039 reference being resolved must correspond to the type of gsymbol.
2040 Otherwise, the new symbol is equipped with the attributes of the
2041 reference. The corresponding code that is called in creating
2042 global entities is parse.c.
2043
2044 In addition, for all but -std=legacy, the gsymbols are used to
2045 check the interfaces of external procedures from the same file.
2046 The namespace of the gsymbol is resolved and then, once this is
2047 done the interface is checked. */
2048
2049
2050 static bool
2051 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2052 {
2053 if (!gsym_ns->proc_name->attr.recursive)
2054 return true;
2055
2056 if (sym->ns == gsym_ns)
2057 return false;
2058
2059 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2060 return false;
2061
2062 return true;
2063 }
2064
2065 static bool
2066 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2067 {
2068 if (gsym_ns->entries)
2069 {
2070 gfc_entry_list *entry = gsym_ns->entries;
2071
2072 for (; entry; entry = entry->next)
2073 {
2074 if (strcmp (sym->name, entry->sym->name) == 0)
2075 {
2076 if (strcmp (gsym_ns->proc_name->name,
2077 sym->ns->proc_name->name) == 0)
2078 return false;
2079
2080 if (sym->ns->parent
2081 && strcmp (gsym_ns->proc_name->name,
2082 sym->ns->parent->proc_name->name) == 0)
2083 return false;
2084 }
2085 }
2086 }
2087 return true;
2088 }
2089
2090 static void
2091 resolve_global_procedure (gfc_symbol *sym, locus *where,
2092 gfc_actual_arglist **actual, int sub)
2093 {
2094 gfc_gsymbol * gsym;
2095 gfc_namespace *ns;
2096 enum gfc_symbol_type type;
2097
2098 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2099
2100 gsym = gfc_get_gsymbol (sym->name);
2101
2102 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2103 gfc_global_used (gsym, where);
2104
2105 if (gfc_option.flag_whole_file
2106 && (sym->attr.if_source == IFSRC_UNKNOWN
2107 || sym->attr.if_source == IFSRC_IFBODY)
2108 && gsym->type != GSYM_UNKNOWN
2109 && gsym->ns
2110 && gsym->ns->resolved != -1
2111 && gsym->ns->proc_name
2112 && not_in_recursive (sym, gsym->ns)
2113 && not_entry_self_reference (sym, gsym->ns))
2114 {
2115 gfc_symbol *def_sym;
2116
2117 /* Resolve the gsymbol namespace if needed. */
2118 if (!gsym->ns->resolved)
2119 {
2120 gfc_dt_list *old_dt_list;
2121 struct gfc_omp_saved_state old_omp_state;
2122
2123 /* Stash away derived types so that the backend_decls do not
2124 get mixed up. */
2125 old_dt_list = gfc_derived_types;
2126 gfc_derived_types = NULL;
2127 /* And stash away openmp state. */
2128 gfc_omp_save_and_clear_state (&old_omp_state);
2129
2130 gfc_resolve (gsym->ns);
2131
2132 /* Store the new derived types with the global namespace. */
2133 if (gfc_derived_types)
2134 gsym->ns->derived_types = gfc_derived_types;
2135
2136 /* Restore the derived types of this namespace. */
2137 gfc_derived_types = old_dt_list;
2138 /* And openmp state. */
2139 gfc_omp_restore_state (&old_omp_state);
2140 }
2141
2142 /* Make sure that translation for the gsymbol occurs before
2143 the procedure currently being resolved. */
2144 ns = gfc_global_ns_list;
2145 for (; ns && ns != gsym->ns; ns = ns->sibling)
2146 {
2147 if (ns->sibling == gsym->ns)
2148 {
2149 ns->sibling = gsym->ns->sibling;
2150 gsym->ns->sibling = gfc_global_ns_list;
2151 gfc_global_ns_list = gsym->ns;
2152 break;
2153 }
2154 }
2155
2156 def_sym = gsym->ns->proc_name;
2157 if (def_sym->attr.entry_master)
2158 {
2159 gfc_entry_list *entry;
2160 for (entry = gsym->ns->entries; entry; entry = entry->next)
2161 if (strcmp (entry->sym->name, sym->name) == 0)
2162 {
2163 def_sym = entry->sym;
2164 break;
2165 }
2166 }
2167
2168 /* Differences in constant character lengths. */
2169 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2170 {
2171 long int l1 = 0, l2 = 0;
2172 gfc_charlen *cl1 = sym->ts.u.cl;
2173 gfc_charlen *cl2 = def_sym->ts.u.cl;
2174
2175 if (cl1 != NULL
2176 && cl1->length != NULL
2177 && cl1->length->expr_type == EXPR_CONSTANT)
2178 l1 = mpz_get_si (cl1->length->value.integer);
2179
2180 if (cl2 != NULL
2181 && cl2->length != NULL
2182 && cl2->length->expr_type == EXPR_CONSTANT)
2183 l2 = mpz_get_si (cl2->length->value.integer);
2184
2185 if (l1 && l2 && l1 != l2)
2186 gfc_error ("Character length mismatch in return type of "
2187 "function '%s' at %L (%ld/%ld)", sym->name,
2188 &sym->declared_at, l1, l2);
2189 }
2190
2191 /* Type mismatch of function return type and expected type. */
2192 if (sym->attr.function
2193 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2194 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2195 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2196 gfc_typename (&def_sym->ts));
2197
2198 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2199 {
2200 gfc_formal_arglist *arg = def_sym->formal;
2201 for ( ; arg; arg = arg->next)
2202 if (!arg->sym)
2203 continue;
2204 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2205 else if (arg->sym->attr.allocatable
2206 || arg->sym->attr.asynchronous
2207 || arg->sym->attr.optional
2208 || arg->sym->attr.pointer
2209 || arg->sym->attr.target
2210 || arg->sym->attr.value
2211 || arg->sym->attr.volatile_)
2212 {
2213 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2214 "has an attribute that requires an explicit "
2215 "interface for this procedure", arg->sym->name,
2216 sym->name, &sym->declared_at);
2217 break;
2218 }
2219 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2220 else if (arg->sym && arg->sym->as
2221 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2222 {
2223 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2224 "argument '%s' must have an explicit interface",
2225 sym->name, &sym->declared_at, arg->sym->name);
2226 break;
2227 }
2228 /* TS 29113, 6.2. */
2229 else if (arg->sym && arg->sym->as
2230 && arg->sym->as->type == AS_ASSUMED_RANK)
2231 {
2232 gfc_error ("Procedure '%s' at %L with assumed-rank dummy "
2233 "argument '%s' must have an explicit interface",
2234 sym->name, &sym->declared_at, arg->sym->name);
2235 break;
2236 }
2237 /* F2008, 12.4.2.2 (2c) */
2238 else if (arg->sym->attr.codimension)
2239 {
2240 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2241 "'%s' must have an explicit interface",
2242 sym->name, &sym->declared_at, arg->sym->name);
2243 break;
2244 }
2245 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2246 else if (false) /* TODO: is a parametrized derived type */
2247 {
2248 gfc_error ("Procedure '%s' at %L with parametrized derived "
2249 "type argument '%s' must have an explicit "
2250 "interface", sym->name, &sym->declared_at,
2251 arg->sym->name);
2252 break;
2253 }
2254 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2255 else if (arg->sym->ts.type == BT_CLASS)
2256 {
2257 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2258 "argument '%s' must have an explicit interface",
2259 sym->name, &sym->declared_at, arg->sym->name);
2260 break;
2261 }
2262 /* As assumed-type is unlimited polymorphic (cf. above).
2263 See also TS 29113, Note 6.1. */
2264 else if (arg->sym->ts.type == BT_ASSUMED)
2265 {
2266 gfc_error ("Procedure '%s' at %L with assumed-type dummy "
2267 "argument '%s' must have an explicit interface",
2268 sym->name, &sym->declared_at, arg->sym->name);
2269 break;
2270 }
2271 }
2272
2273 if (def_sym->attr.function)
2274 {
2275 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2276 if (def_sym->as && def_sym->as->rank
2277 && (!sym->as || sym->as->rank != def_sym->as->rank))
2278 gfc_error ("The reference to function '%s' at %L either needs an "
2279 "explicit INTERFACE or the rank is incorrect", sym->name,
2280 where);
2281
2282 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2283 if ((def_sym->result->attr.pointer
2284 || def_sym->result->attr.allocatable)
2285 && (sym->attr.if_source != IFSRC_IFBODY
2286 || def_sym->result->attr.pointer
2287 != sym->result->attr.pointer
2288 || def_sym->result->attr.allocatable
2289 != sym->result->attr.allocatable))
2290 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2291 "result must have an explicit interface", sym->name,
2292 where);
2293
2294 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2295 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2296 && def_sym->ts.type == BT_CHARACTER && def_sym->ts.u.cl->length != NULL)
2297 {
2298 gfc_charlen *cl = sym->ts.u.cl;
2299
2300 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2301 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2302 {
2303 gfc_error ("Nonconstant character-length function '%s' at %L "
2304 "must have an explicit interface", sym->name,
2305 &sym->declared_at);
2306 }
2307 }
2308 }
2309
2310 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2311 if (def_sym->attr.elemental && !sym->attr.elemental)
2312 {
2313 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2314 "interface", sym->name, &sym->declared_at);
2315 }
2316
2317 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2318 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2319 {
2320 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2321 "an explicit interface", sym->name, &sym->declared_at);
2322 }
2323
2324 if (gfc_option.flag_whole_file == 1
2325 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2326 && !(gfc_option.warn_std & GFC_STD_GNU)))
2327 gfc_errors_to_warnings (1);
2328
2329 if (sym->attr.if_source != IFSRC_IFBODY)
2330 gfc_procedure_use (def_sym, actual, where);
2331
2332 gfc_errors_to_warnings (0);
2333 }
2334
2335 if (gsym->type == GSYM_UNKNOWN)
2336 {
2337 gsym->type = type;
2338 gsym->where = *where;
2339 }
2340
2341 gsym->used = 1;
2342 }
2343
2344
2345 /************* Function resolution *************/
2346
2347 /* Resolve a function call known to be generic.
2348 Section 14.1.2.4.1. */
2349
2350 static match
2351 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2352 {
2353 gfc_symbol *s;
2354
2355 if (sym->attr.generic)
2356 {
2357 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2358 if (s != NULL)
2359 {
2360 expr->value.function.name = s->name;
2361 expr->value.function.esym = s;
2362
2363 if (s->ts.type != BT_UNKNOWN)
2364 expr->ts = s->ts;
2365 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2366 expr->ts = s->result->ts;
2367
2368 if (s->as != NULL)
2369 expr->rank = s->as->rank;
2370 else if (s->result != NULL && s->result->as != NULL)
2371 expr->rank = s->result->as->rank;
2372
2373 gfc_set_sym_referenced (expr->value.function.esym);
2374
2375 return MATCH_YES;
2376 }
2377
2378 /* TODO: Need to search for elemental references in generic
2379 interface. */
2380 }
2381
2382 if (sym->attr.intrinsic)
2383 return gfc_intrinsic_func_interface (expr, 0);
2384
2385 return MATCH_NO;
2386 }
2387
2388
2389 static gfc_try
2390 resolve_generic_f (gfc_expr *expr)
2391 {
2392 gfc_symbol *sym;
2393 match m;
2394 gfc_interface *intr = NULL;
2395
2396 sym = expr->symtree->n.sym;
2397
2398 for (;;)
2399 {
2400 m = resolve_generic_f0 (expr, sym);
2401 if (m == MATCH_YES)
2402 return SUCCESS;
2403 else if (m == MATCH_ERROR)
2404 return FAILURE;
2405
2406 generic:
2407 if (!intr)
2408 for (intr = sym->generic; intr; intr = intr->next)
2409 if (intr->sym->attr.flavor == FL_DERIVED)
2410 break;
2411
2412 if (sym->ns->parent == NULL)
2413 break;
2414 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2415
2416 if (sym == NULL)
2417 break;
2418 if (!generic_sym (sym))
2419 goto generic;
2420 }
2421
2422 /* Last ditch attempt. See if the reference is to an intrinsic
2423 that possesses a matching interface. 14.1.2.4 */
2424 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2425 {
2426 gfc_error ("There is no specific function for the generic '%s' "
2427 "at %L", expr->symtree->n.sym->name, &expr->where);
2428 return FAILURE;
2429 }
2430
2431 if (intr)
2432 {
2433 if (gfc_convert_to_structure_constructor (expr, intr->sym, NULL, NULL,
2434 false) != SUCCESS)
2435 return FAILURE;
2436 return resolve_structure_cons (expr, 0);
2437 }
2438
2439 m = gfc_intrinsic_func_interface (expr, 0);
2440 if (m == MATCH_YES)
2441 return SUCCESS;
2442
2443 if (m == MATCH_NO)
2444 gfc_error ("Generic function '%s' at %L is not consistent with a "
2445 "specific intrinsic interface", expr->symtree->n.sym->name,
2446 &expr->where);
2447
2448 return FAILURE;
2449 }
2450
2451
2452 /* Resolve a function call known to be specific. */
2453
2454 static match
2455 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2456 {
2457 match m;
2458
2459 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2460 {
2461 if (sym->attr.dummy)
2462 {
2463 sym->attr.proc = PROC_DUMMY;
2464 goto found;
2465 }
2466
2467 sym->attr.proc = PROC_EXTERNAL;
2468 goto found;
2469 }
2470
2471 if (sym->attr.proc == PROC_MODULE
2472 || sym->attr.proc == PROC_ST_FUNCTION
2473 || sym->attr.proc == PROC_INTERNAL)
2474 goto found;
2475
2476 if (sym->attr.intrinsic)
2477 {
2478 m = gfc_intrinsic_func_interface (expr, 1);
2479 if (m == MATCH_YES)
2480 return MATCH_YES;
2481 if (m == MATCH_NO)
2482 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2483 "with an intrinsic", sym->name, &expr->where);
2484
2485 return MATCH_ERROR;
2486 }
2487
2488 return MATCH_NO;
2489
2490 found:
2491 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2492
2493 if (sym->result)
2494 expr->ts = sym->result->ts;
2495 else
2496 expr->ts = sym->ts;
2497 expr->value.function.name = sym->name;
2498 expr->value.function.esym = sym;
2499 if (sym->as != NULL)
2500 expr->rank = sym->as->rank;
2501
2502 return MATCH_YES;
2503 }
2504
2505
2506 static gfc_try
2507 resolve_specific_f (gfc_expr *expr)
2508 {
2509 gfc_symbol *sym;
2510 match m;
2511
2512 sym = expr->symtree->n.sym;
2513
2514 for (;;)
2515 {
2516 m = resolve_specific_f0 (sym, expr);
2517 if (m == MATCH_YES)
2518 return SUCCESS;
2519 if (m == MATCH_ERROR)
2520 return FAILURE;
2521
2522 if (sym->ns->parent == NULL)
2523 break;
2524
2525 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2526
2527 if (sym == NULL)
2528 break;
2529 }
2530
2531 gfc_error ("Unable to resolve the specific function '%s' at %L",
2532 expr->symtree->n.sym->name, &expr->where);
2533
2534 return SUCCESS;
2535 }
2536
2537
2538 /* Resolve a procedure call not known to be generic nor specific. */
2539
2540 static gfc_try
2541 resolve_unknown_f (gfc_expr *expr)
2542 {
2543 gfc_symbol *sym;
2544 gfc_typespec *ts;
2545
2546 sym = expr->symtree->n.sym;
2547
2548 if (sym->attr.dummy)
2549 {
2550 sym->attr.proc = PROC_DUMMY;
2551 expr->value.function.name = sym->name;
2552 goto set_type;
2553 }
2554
2555 /* See if we have an intrinsic function reference. */
2556
2557 if (gfc_is_intrinsic (sym, 0, expr->where))
2558 {
2559 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2560 return SUCCESS;
2561 return FAILURE;
2562 }
2563
2564 /* The reference is to an external name. */
2565
2566 sym->attr.proc = PROC_EXTERNAL;
2567 expr->value.function.name = sym->name;
2568 expr->value.function.esym = expr->symtree->n.sym;
2569
2570 if (sym->as != NULL)
2571 expr->rank = sym->as->rank;
2572
2573 /* Type of the expression is either the type of the symbol or the
2574 default type of the symbol. */
2575
2576 set_type:
2577 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2578
2579 if (sym->ts.type != BT_UNKNOWN)
2580 expr->ts = sym->ts;
2581 else
2582 {
2583 ts = gfc_get_default_type (sym->name, sym->ns);
2584
2585 if (ts->type == BT_UNKNOWN)
2586 {
2587 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2588 sym->name, &expr->where);
2589 return FAILURE;
2590 }
2591 else
2592 expr->ts = *ts;
2593 }
2594
2595 return SUCCESS;
2596 }
2597
2598
2599 /* Return true, if the symbol is an external procedure. */
2600 static bool
2601 is_external_proc (gfc_symbol *sym)
2602 {
2603 if (!sym->attr.dummy && !sym->attr.contained
2604 && !(sym->attr.intrinsic
2605 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2606 && sym->attr.proc != PROC_ST_FUNCTION
2607 && !sym->attr.proc_pointer
2608 && !sym->attr.use_assoc
2609 && sym->name)
2610 return true;
2611
2612 return false;
2613 }
2614
2615
2616 /* Figure out if a function reference is pure or not. Also set the name
2617 of the function for a potential error message. Return nonzero if the
2618 function is PURE, zero if not. */
2619 static int
2620 pure_stmt_function (gfc_expr *, gfc_symbol *);
2621
2622 static int
2623 pure_function (gfc_expr *e, const char **name)
2624 {
2625 int pure;
2626
2627 *name = NULL;
2628
2629 if (e->symtree != NULL
2630 && e->symtree->n.sym != NULL
2631 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2632 return pure_stmt_function (e, e->symtree->n.sym);
2633
2634 if (e->value.function.esym)
2635 {
2636 pure = gfc_pure (e->value.function.esym);
2637 *name = e->value.function.esym->name;
2638 }
2639 else if (e->value.function.isym)
2640 {
2641 pure = e->value.function.isym->pure
2642 || e->value.function.isym->elemental;
2643 *name = e->value.function.isym->name;
2644 }
2645 else
2646 {
2647 /* Implicit functions are not pure. */
2648 pure = 0;
2649 *name = e->value.function.name;
2650 }
2651
2652 return pure;
2653 }
2654
2655
2656 static bool
2657 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2658 int *f ATTRIBUTE_UNUSED)
2659 {
2660 const char *name;
2661
2662 /* Don't bother recursing into other statement functions
2663 since they will be checked individually for purity. */
2664 if (e->expr_type != EXPR_FUNCTION
2665 || !e->symtree
2666 || e->symtree->n.sym == sym
2667 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2668 return false;
2669
2670 return pure_function (e, &name) ? false : true;
2671 }
2672
2673
2674 static int
2675 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2676 {
2677 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2678 }
2679
2680
2681 static gfc_try
2682 is_scalar_expr_ptr (gfc_expr *expr)
2683 {
2684 gfc_try retval = SUCCESS;
2685 gfc_ref *ref;
2686 int start;
2687 int end;
2688
2689 /* See if we have a gfc_ref, which means we have a substring, array
2690 reference, or a component. */
2691 if (expr->ref != NULL)
2692 {
2693 ref = expr->ref;
2694 while (ref->next != NULL)
2695 ref = ref->next;
2696
2697 switch (ref->type)
2698 {
2699 case REF_SUBSTRING:
2700 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2701 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2702 retval = FAILURE;
2703 break;
2704
2705 case REF_ARRAY:
2706 if (ref->u.ar.type == AR_ELEMENT)
2707 retval = SUCCESS;
2708 else if (ref->u.ar.type == AR_FULL)
2709 {
2710 /* The user can give a full array if the array is of size 1. */
2711 if (ref->u.ar.as != NULL
2712 && ref->u.ar.as->rank == 1
2713 && ref->u.ar.as->type == AS_EXPLICIT
2714 && ref->u.ar.as->lower[0] != NULL
2715 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2716 && ref->u.ar.as->upper[0] != NULL
2717 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2718 {
2719 /* If we have a character string, we need to check if
2720 its length is one. */
2721 if (expr->ts.type == BT_CHARACTER)
2722 {
2723 if (expr->ts.u.cl == NULL
2724 || expr->ts.u.cl->length == NULL
2725 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2726 != 0)
2727 retval = FAILURE;
2728 }
2729 else
2730 {
2731 /* We have constant lower and upper bounds. If the
2732 difference between is 1, it can be considered a
2733 scalar.
2734 FIXME: Use gfc_dep_compare_expr instead. */
2735 start = (int) mpz_get_si
2736 (ref->u.ar.as->lower[0]->value.integer);
2737 end = (int) mpz_get_si
2738 (ref->u.ar.as->upper[0]->value.integer);
2739 if (end - start + 1 != 1)
2740 retval = FAILURE;
2741 }
2742 }
2743 else
2744 retval = FAILURE;
2745 }
2746 else
2747 retval = FAILURE;
2748 break;
2749 default:
2750 retval = SUCCESS;
2751 break;
2752 }
2753 }
2754 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2755 {
2756 /* Character string. Make sure it's of length 1. */
2757 if (expr->ts.u.cl == NULL
2758 || expr->ts.u.cl->length == NULL
2759 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2760 retval = FAILURE;
2761 }
2762 else if (expr->rank != 0)
2763 retval = FAILURE;
2764
2765 return retval;
2766 }
2767
2768
2769 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2770 and, in the case of c_associated, set the binding label based on
2771 the arguments. */
2772
2773 static gfc_try
2774 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2775 gfc_symbol **new_sym)
2776 {
2777 char name[GFC_MAX_SYMBOL_LEN + 1];
2778 int optional_arg = 0;
2779 gfc_try retval = SUCCESS;
2780 gfc_symbol *args_sym;
2781 gfc_typespec *arg_ts;
2782 symbol_attribute arg_attr;
2783
2784 if (args->expr->expr_type == EXPR_CONSTANT
2785 || args->expr->expr_type == EXPR_OP
2786 || args->expr->expr_type == EXPR_NULL)
2787 {
2788 gfc_error ("Argument to '%s' at %L is not a variable",
2789 sym->name, &(args->expr->where));
2790 return FAILURE;
2791 }
2792
2793 args_sym = args->expr->symtree->n.sym;
2794
2795 /* The typespec for the actual arg should be that stored in the expr
2796 and not necessarily that of the expr symbol (args_sym), because
2797 the actual expression could be a part-ref of the expr symbol. */
2798 arg_ts = &(args->expr->ts);
2799 arg_attr = gfc_expr_attr (args->expr);
2800
2801 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2802 {
2803 /* If the user gave two args then they are providing something for
2804 the optional arg (the second cptr). Therefore, set the name and
2805 binding label to the c_associated for two cptrs. Otherwise,
2806 set c_associated to expect one cptr. */
2807 if (args->next)
2808 {
2809 /* two args. */
2810 sprintf (name, "%s_2", sym->name);
2811 optional_arg = 1;
2812 }
2813 else
2814 {
2815 /* one arg. */
2816 sprintf (name, "%s_1", sym->name);
2817 optional_arg = 0;
2818 }
2819
2820 /* Get a new symbol for the version of c_associated that
2821 will get called. */
2822 *new_sym = get_iso_c_sym (sym, name, NULL, optional_arg);
2823 }
2824 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2825 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2826 {
2827 sprintf (name, "%s", sym->name);
2828
2829 /* Error check the call. */
2830 if (args->next != NULL)
2831 {
2832 gfc_error_now ("More actual than formal arguments in '%s' "
2833 "call at %L", name, &(args->expr->where));
2834 retval = FAILURE;
2835 }
2836 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2837 {
2838 gfc_ref *ref;
2839 bool seen_section;
2840
2841 /* Make sure we have either the target or pointer attribute. */
2842 if (!arg_attr.target && !arg_attr.pointer)
2843 {
2844 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2845 "a TARGET or an associated pointer",
2846 args_sym->name,
2847 sym->name, &(args->expr->where));
2848 retval = FAILURE;
2849 }
2850
2851 if (gfc_is_coindexed (args->expr))
2852 {
2853 gfc_error_now ("Coindexed argument not permitted"
2854 " in '%s' call at %L", name,
2855 &(args->expr->where));
2856 retval = FAILURE;
2857 }
2858
2859 /* Follow references to make sure there are no array
2860 sections. */
2861 seen_section = false;
2862
2863 for (ref=args->expr->ref; ref; ref = ref->next)
2864 {
2865 if (ref->type == REF_ARRAY)
2866 {
2867 if (ref->u.ar.type == AR_SECTION)
2868 seen_section = true;
2869
2870 if (ref->u.ar.type != AR_ELEMENT)
2871 {
2872 gfc_ref *r;
2873 for (r = ref->next; r; r=r->next)
2874 if (r->type == REF_COMPONENT)
2875 {
2876 gfc_error_now ("Array section not permitted"
2877 " in '%s' call at %L", name,
2878 &(args->expr->where));
2879 retval = FAILURE;
2880 break;
2881 }
2882 }
2883 }
2884 }
2885
2886 if (seen_section && retval == SUCCESS)
2887 gfc_warning ("Array section in '%s' call at %L", name,
2888 &(args->expr->where));
2889
2890 /* See if we have interoperable type and type param. */
2891 if (gfc_verify_c_interop (arg_ts) == SUCCESS
2892 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2893 {
2894 if (args_sym->attr.target == 1)
2895 {
2896 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2897 has the target attribute and is interoperable. */
2898 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2899 allocatable variable that has the TARGET attribute and
2900 is not an array of zero size. */
2901 if (args_sym->attr.allocatable == 1)
2902 {
2903 if (args_sym->attr.dimension != 0
2904 && (args_sym->as && args_sym->as->rank == 0))
2905 {
2906 gfc_error_now ("Allocatable variable '%s' used as a "
2907 "parameter to '%s' at %L must not be "
2908 "an array of zero size",
2909 args_sym->name, sym->name,
2910 &(args->expr->where));
2911 retval = FAILURE;
2912 }
2913 }
2914 else
2915 {
2916 /* A non-allocatable target variable with C
2917 interoperable type and type parameters must be
2918 interoperable. */
2919 if (args_sym && args_sym->attr.dimension)
2920 {
2921 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2922 {
2923 gfc_error ("Assumed-shape array '%s' at %L "
2924 "cannot be an argument to the "
2925 "procedure '%s' because "
2926 "it is not C interoperable",
2927 args_sym->name,
2928 &(args->expr->where), sym->name);
2929 retval = FAILURE;
2930 }
2931 else if (args_sym->as->type == AS_DEFERRED)
2932 {
2933 gfc_error ("Deferred-shape array '%s' at %L "
2934 "cannot be an argument to the "
2935 "procedure '%s' because "
2936 "it is not C interoperable",
2937 args_sym->name,
2938 &(args->expr->where), sym->name);
2939 retval = FAILURE;
2940 }
2941 }
2942
2943 /* Make sure it's not a character string. Arrays of
2944 any type should be ok if the variable is of a C
2945 interoperable type. */
2946 if (arg_ts->type == BT_CHARACTER)
2947 if (arg_ts->u.cl != NULL
2948 && (arg_ts->u.cl->length == NULL
2949 || arg_ts->u.cl->length->expr_type
2950 != EXPR_CONSTANT
2951 || mpz_cmp_si
2952 (arg_ts->u.cl->length->value.integer, 1)
2953 != 0)
2954 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2955 {
2956 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2957 "at %L must have a length of 1",
2958 args_sym->name, sym->name,
2959 &(args->expr->where));
2960 retval = FAILURE;
2961 }
2962 }
2963 }
2964 else if (arg_attr.pointer
2965 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2966 {
2967 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2968 scalar pointer. */
2969 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2970 "associated scalar POINTER", args_sym->name,
2971 sym->name, &(args->expr->where));
2972 retval = FAILURE;
2973 }
2974 }
2975 else
2976 {
2977 /* The parameter is not required to be C interoperable. If it
2978 is not C interoperable, it must be a nonpolymorphic scalar
2979 with no length type parameters. It still must have either
2980 the pointer or target attribute, and it can be
2981 allocatable (but must be allocated when c_loc is called). */
2982 if (args->expr->rank != 0
2983 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2984 {
2985 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2986 "scalar", args_sym->name, sym->name,
2987 &(args->expr->where));
2988 retval = FAILURE;
2989 }
2990 else if (arg_ts->type == BT_CHARACTER
2991 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2992 {
2993 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2994 "%L must have a length of 1",
2995 args_sym->name, sym->name,
2996 &(args->expr->where));
2997 retval = FAILURE;
2998 }
2999 else if (arg_ts->type == BT_CLASS)
3000 {
3001 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
3002 "polymorphic", args_sym->name, sym->name,
3003 &(args->expr->where));
3004 retval = FAILURE;
3005 }
3006 }
3007 }
3008 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
3009 {
3010 if (args_sym->attr.flavor != FL_PROCEDURE)
3011 {
3012 /* TODO: Update this error message to allow for procedure
3013 pointers once they are implemented. */
3014 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
3015 "procedure",
3016 args_sym->name, sym->name,
3017 &(args->expr->where));
3018 retval = FAILURE;
3019 }
3020 else if (args_sym->attr.is_bind_c != 1)
3021 {
3022 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
3023 "BIND(C)",
3024 args_sym->name, sym->name,
3025 &(args->expr->where));
3026 retval = FAILURE;
3027 }
3028 }
3029
3030 /* for c_loc/c_funloc, the new symbol is the same as the old one */
3031 *new_sym = sym;
3032 }
3033 else
3034 {
3035 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
3036 "iso_c_binding function: '%s'!\n", sym->name);
3037 }
3038
3039 return retval;
3040 }
3041
3042
3043 /* Resolve a function call, which means resolving the arguments, then figuring
3044 out which entity the name refers to. */
3045
3046 static gfc_try
3047 resolve_function (gfc_expr *expr)
3048 {
3049 gfc_actual_arglist *arg;
3050 gfc_symbol *sym;
3051 const char *name;
3052 gfc_try t;
3053 int temp;
3054 procedure_type p = PROC_INTRINSIC;
3055 bool no_formal_args;
3056
3057 sym = NULL;
3058 if (expr->symtree)
3059 sym = expr->symtree->n.sym;
3060
3061 /* If this is a procedure pointer component, it has already been resolved. */
3062 if (gfc_is_proc_ptr_comp (expr, NULL))
3063 return SUCCESS;
3064
3065 if (sym && sym->attr.intrinsic
3066 && gfc_resolve_intrinsic (sym, &expr->where) == FAILURE)
3067 return FAILURE;
3068
3069 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3070 {
3071 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
3072 return FAILURE;
3073 }
3074
3075 /* If this ia a deferred TBP with an abstract interface (which may
3076 of course be referenced), expr->value.function.esym will be set. */
3077 if (sym && sym->attr.abstract && !expr->value.function.esym)
3078 {
3079 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3080 sym->name, &expr->where);
3081 return FAILURE;
3082 }
3083
3084 /* Switch off assumed size checking and do this again for certain kinds
3085 of procedure, once the procedure itself is resolved. */
3086 need_full_assumed_size++;
3087
3088 if (expr->symtree && expr->symtree->n.sym)
3089 p = expr->symtree->n.sym->attr.proc;
3090
3091 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3092 inquiry_argument = true;
3093 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
3094
3095 if (resolve_actual_arglist (expr->value.function.actual,
3096 p, no_formal_args) == FAILURE)
3097 {
3098 inquiry_argument = false;
3099 return FAILURE;
3100 }
3101
3102 inquiry_argument = false;
3103
3104 /* Need to setup the call to the correct c_associated, depending on
3105 the number of cptrs to user gives to compare. */
3106 if (sym && sym->attr.is_iso_c == 1)
3107 {
3108 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
3109 == FAILURE)
3110 return FAILURE;
3111
3112 /* Get the symtree for the new symbol (resolved func).
3113 the old one will be freed later, when it's no longer used. */
3114 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
3115 }
3116
3117 /* Resume assumed_size checking. */
3118 need_full_assumed_size--;
3119
3120 /* If the procedure is external, check for usage. */
3121 if (sym && is_external_proc (sym))
3122 resolve_global_procedure (sym, &expr->where,
3123 &expr->value.function.actual, 0);
3124
3125 if (sym && sym->ts.type == BT_CHARACTER
3126 && sym->ts.u.cl
3127 && sym->ts.u.cl->length == NULL
3128 && !sym->attr.dummy
3129 && !sym->ts.deferred
3130 && expr->value.function.esym == NULL
3131 && !sym->attr.contained)
3132 {
3133 /* Internal procedures are taken care of in resolve_contained_fntype. */
3134 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3135 "be used at %L since it is not a dummy argument",
3136 sym->name, &expr->where);
3137 return FAILURE;
3138 }
3139
3140 /* See if function is already resolved. */
3141
3142 if (expr->value.function.name != NULL)
3143 {
3144 if (expr->ts.type == BT_UNKNOWN)
3145 expr->ts = sym->ts;
3146 t = SUCCESS;
3147 }
3148 else
3149 {
3150 /* Apply the rules of section 14.1.2. */
3151
3152 switch (procedure_kind (sym))
3153 {
3154 case PTYPE_GENERIC:
3155 t = resolve_generic_f (expr);
3156 break;
3157
3158 case PTYPE_SPECIFIC:
3159 t = resolve_specific_f (expr);
3160 break;
3161
3162 case PTYPE_UNKNOWN:
3163 t = resolve_unknown_f (expr);
3164 break;
3165
3166 default:
3167 gfc_internal_error ("resolve_function(): bad function type");
3168 }
3169 }
3170
3171 /* If the expression is still a function (it might have simplified),
3172 then we check to see if we are calling an elemental function. */
3173
3174 if (expr->expr_type != EXPR_FUNCTION)
3175 return t;
3176
3177 temp = need_full_assumed_size;
3178 need_full_assumed_size = 0;
3179
3180 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3181 return FAILURE;
3182
3183 if (omp_workshare_flag
3184 && expr->value.function.esym
3185 && ! gfc_elemental (expr->value.function.esym))
3186 {
3187 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3188 "in WORKSHARE construct", expr->value.function.esym->name,
3189 &expr->where);
3190 t = FAILURE;
3191 }
3192
3193 #define GENERIC_ID expr->value.function.isym->id
3194 else if (expr->value.function.actual != NULL
3195 && expr->value.function.isym != NULL
3196 && GENERIC_ID != GFC_ISYM_LBOUND
3197 && GENERIC_ID != GFC_ISYM_LEN
3198 && GENERIC_ID != GFC_ISYM_LOC
3199 && GENERIC_ID != GFC_ISYM_PRESENT)
3200 {
3201 /* Array intrinsics must also have the last upper bound of an
3202 assumed size array argument. UBOUND and SIZE have to be
3203 excluded from the check if the second argument is anything
3204 than a constant. */
3205
3206 for (arg = expr->value.function.actual; arg; arg = arg->next)
3207 {
3208 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3209 && arg->next != NULL && arg->next->expr)
3210 {
3211 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3212 break;
3213
3214 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3215 break;
3216
3217 if ((int)mpz_get_si (arg->next->expr->value.integer)
3218 < arg->expr->rank)
3219 break;
3220 }
3221
3222 if (arg->expr != NULL
3223 && arg->expr->rank > 0
3224 && resolve_assumed_size_actual (arg->expr))
3225 return FAILURE;
3226 }
3227 }
3228 #undef GENERIC_ID
3229
3230 need_full_assumed_size = temp;
3231 name = NULL;
3232
3233 if (!pure_function (expr, &name) && name)
3234 {
3235 if (forall_flag)
3236 {
3237 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3238 "FORALL %s", name, &expr->where,
3239 forall_flag == 2 ? "mask" : "block");
3240 t = FAILURE;
3241 }
3242 else if (do_concurrent_flag)
3243 {
3244 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3245 "DO CONCURRENT %s", name, &expr->where,
3246 do_concurrent_flag == 2 ? "mask" : "block");
3247 t = FAILURE;
3248 }
3249 else if (gfc_pure (NULL))
3250 {
3251 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3252 "procedure within a PURE procedure", name, &expr->where);
3253 t = FAILURE;
3254 }
3255
3256 if (gfc_implicit_pure (NULL))
3257 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3258 }
3259
3260 /* Functions without the RECURSIVE attribution are not allowed to
3261 * call themselves. */
3262 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3263 {
3264 gfc_symbol *esym;
3265 esym = expr->value.function.esym;
3266
3267 if (is_illegal_recursion (esym, gfc_current_ns))
3268 {
3269 if (esym->attr.entry && esym->ns->entries)
3270 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3271 " function '%s' is not RECURSIVE",
3272 esym->name, &expr->where, esym->ns->entries->sym->name);
3273 else
3274 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3275 " is not RECURSIVE", esym->name, &expr->where);
3276
3277 t = FAILURE;
3278 }
3279 }
3280
3281 /* Character lengths of use associated functions may contains references to
3282 symbols not referenced from the current program unit otherwise. Make sure
3283 those symbols are marked as referenced. */
3284
3285 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3286 && expr->value.function.esym->attr.use_assoc)
3287 {
3288 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3289 }
3290
3291 /* Make sure that the expression has a typespec that works. */
3292 if (expr->ts.type == BT_UNKNOWN)
3293 {
3294 if (expr->symtree->n.sym->result
3295 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3296 && !expr->symtree->n.sym->result->attr.proc_pointer)
3297 expr->ts = expr->symtree->n.sym->result->ts;
3298 }
3299
3300 return t;
3301 }
3302
3303
3304 /************* Subroutine resolution *************/
3305
3306 static void
3307 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3308 {
3309 if (gfc_pure (sym))
3310 return;
3311
3312 if (forall_flag)
3313 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3314 sym->name, &c->loc);
3315 else if (do_concurrent_flag)
3316 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3317 "PURE", sym->name, &c->loc);
3318 else if (gfc_pure (NULL))
3319 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3320 &c->loc);
3321
3322 if (gfc_implicit_pure (NULL))
3323 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3324 }
3325
3326
3327 static match
3328 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3329 {
3330 gfc_symbol *s;
3331
3332 if (sym->attr.generic)
3333 {
3334 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3335 if (s != NULL)
3336 {
3337 c->resolved_sym = s;
3338 pure_subroutine (c, s);
3339 return MATCH_YES;
3340 }
3341
3342 /* TODO: Need to search for elemental references in generic interface. */
3343 }
3344
3345 if (sym->attr.intrinsic)
3346 return gfc_intrinsic_sub_interface (c, 0);
3347
3348 return MATCH_NO;
3349 }
3350
3351
3352 static gfc_try
3353 resolve_generic_s (gfc_code *c)
3354 {
3355 gfc_symbol *sym;
3356 match m;
3357
3358 sym = c->symtree->n.sym;
3359
3360 for (;;)
3361 {
3362 m = resolve_generic_s0 (c, sym);
3363 if (m == MATCH_YES)
3364 return SUCCESS;
3365 else if (m == MATCH_ERROR)
3366 return FAILURE;
3367
3368 generic:
3369 if (sym->ns->parent == NULL)
3370 break;
3371 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3372
3373 if (sym == NULL)
3374 break;
3375 if (!generic_sym (sym))
3376 goto generic;
3377 }
3378
3379 /* Last ditch attempt. See if the reference is to an intrinsic
3380 that possesses a matching interface. 14.1.2.4 */
3381 sym = c->symtree->n.sym;
3382
3383 if (!gfc_is_intrinsic (sym, 1, c->loc))
3384 {
3385 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3386 sym->name, &c->loc);
3387 return FAILURE;
3388 }
3389
3390 m = gfc_intrinsic_sub_interface (c, 0);
3391 if (m == MATCH_YES)
3392 return SUCCESS;
3393 if (m == MATCH_NO)
3394 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3395 "intrinsic subroutine interface", sym->name, &c->loc);
3396
3397 return FAILURE;
3398 }
3399
3400
3401 /* Set the name and binding label of the subroutine symbol in the call
3402 expression represented by 'c' to include the type and kind of the
3403 second parameter. This function is for resolving the appropriate
3404 version of c_f_pointer() and c_f_procpointer(). For example, a
3405 call to c_f_pointer() for a default integer pointer could have a
3406 name of c_f_pointer_i4. If no second arg exists, which is an error
3407 for these two functions, it defaults to the generic symbol's name
3408 and binding label. */
3409
3410 static void
3411 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3412 char *name, const char **binding_label)
3413 {
3414 gfc_expr *arg = NULL;
3415 char type;
3416 int kind;
3417
3418 /* The second arg of c_f_pointer and c_f_procpointer determines
3419 the type and kind for the procedure name. */
3420 arg = c->ext.actual->next->expr;
3421
3422 if (arg != NULL)
3423 {
3424 /* Set up the name to have the given symbol's name,
3425 plus the type and kind. */
3426 /* a derived type is marked with the type letter 'u' */
3427 if (arg->ts.type == BT_DERIVED)
3428 {
3429 type = 'd';
3430 kind = 0; /* set the kind as 0 for now */
3431 }
3432 else
3433 {
3434 type = gfc_type_letter (arg->ts.type);
3435 kind = arg->ts.kind;
3436 }
3437
3438 if (arg->ts.type == BT_CHARACTER)
3439 /* Kind info for character strings not needed. */
3440 kind = 0;
3441
3442 sprintf (name, "%s_%c%d", sym->name, type, kind);
3443 /* Set up the binding label as the given symbol's label plus
3444 the type and kind. */
3445 *binding_label = gfc_get_string ("%s_%c%d", sym->binding_label, type,
3446 kind);
3447 }
3448 else
3449 {
3450 /* If the second arg is missing, set the name and label as
3451 was, cause it should at least be found, and the missing
3452 arg error will be caught by compare_parameters(). */
3453 sprintf (name, "%s", sym->name);
3454 *binding_label = sym->binding_label;
3455 }
3456
3457 return;
3458 }
3459
3460
3461 /* Resolve a generic version of the iso_c_binding procedure given
3462 (sym) to the specific one based on the type and kind of the
3463 argument(s). Currently, this function resolves c_f_pointer() and
3464 c_f_procpointer based on the type and kind of the second argument
3465 (FPTR). Other iso_c_binding procedures aren't specially handled.
3466 Upon successfully exiting, c->resolved_sym will hold the resolved
3467 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3468 otherwise. */
3469
3470 match
3471 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3472 {
3473 gfc_symbol *new_sym;
3474 /* this is fine, since we know the names won't use the max */
3475 char name[GFC_MAX_SYMBOL_LEN + 1];
3476 const char* binding_label;
3477 /* default to success; will override if find error */
3478 match m = MATCH_YES;
3479
3480 /* Make sure the actual arguments are in the necessary order (based on the
3481 formal args) before resolving. */
3482 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3483
3484 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3485 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3486 {
3487 set_name_and_label (c, sym, name, &binding_label);
3488
3489 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3490 {
3491 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3492 {
3493 /* Make sure we got a third arg if the second arg has non-zero
3494 rank. We must also check that the type and rank are
3495 correct since we short-circuit this check in
3496 gfc_procedure_use() (called above to sort actual args). */
3497 if (c->ext.actual->next->expr->rank != 0)
3498 {
3499 if(c->ext.actual->next->next == NULL
3500 || c->ext.actual->next->next->expr == NULL)
3501 {
3502 m = MATCH_ERROR;
3503 gfc_error ("Missing SHAPE parameter for call to %s "
3504 "at %L", sym->name, &(c->loc));
3505 }
3506 else if (c->ext.actual->next->next->expr->ts.type
3507 != BT_INTEGER
3508 || c->ext.actual->next->next->expr->rank != 1)
3509 {
3510 m = MATCH_ERROR;
3511 gfc_error ("SHAPE parameter for call to %s at %L must "
3512 "be a rank 1 INTEGER array", sym->name,
3513 &(c->loc));
3514 }
3515 }
3516 }
3517 }
3518
3519 if (m != MATCH_ERROR)
3520 {
3521 /* the 1 means to add the optional arg to formal list */
3522 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3523
3524 /* for error reporting, say it's declared where the original was */
3525 new_sym->declared_at = sym->declared_at;
3526 }
3527 }
3528 else
3529 {
3530 /* no differences for c_loc or c_funloc */
3531 new_sym = sym;
3532 }
3533
3534 /* set the resolved symbol */
3535 if (m != MATCH_ERROR)
3536 c->resolved_sym = new_sym;
3537 else
3538 c->resolved_sym = sym;
3539
3540 return m;
3541 }
3542
3543
3544 /* Resolve a subroutine call known to be specific. */
3545
3546 static match
3547 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3548 {
3549 match m;
3550
3551 if(sym->attr.is_iso_c)
3552 {
3553 m = gfc_iso_c_sub_interface (c,sym);
3554 return m;
3555 }
3556
3557 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3558 {
3559 if (sym->attr.dummy)
3560 {
3561 sym->attr.proc = PROC_DUMMY;
3562 goto found;
3563 }
3564
3565 sym->attr.proc = PROC_EXTERNAL;
3566 goto found;
3567 }
3568
3569 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3570 goto found;
3571
3572 if (sym->attr.intrinsic)
3573 {
3574 m = gfc_intrinsic_sub_interface (c, 1);
3575 if (m == MATCH_YES)
3576 return MATCH_YES;
3577 if (m == MATCH_NO)
3578 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3579 "with an intrinsic", sym->name, &c->loc);
3580
3581 return MATCH_ERROR;
3582 }
3583
3584 return MATCH_NO;
3585
3586 found:
3587 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3588
3589 c->resolved_sym = sym;
3590 pure_subroutine (c, sym);
3591
3592 return MATCH_YES;
3593 }
3594
3595
3596 static gfc_try
3597 resolve_specific_s (gfc_code *c)
3598 {
3599 gfc_symbol *sym;
3600 match m;
3601
3602 sym = c->symtree->n.sym;
3603
3604 for (;;)
3605 {
3606 m = resolve_specific_s0 (c, sym);
3607 if (m == MATCH_YES)
3608 return SUCCESS;
3609 if (m == MATCH_ERROR)
3610 return FAILURE;
3611
3612 if (sym->ns->parent == NULL)
3613 break;
3614
3615 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3616
3617 if (sym == NULL)
3618 break;
3619 }
3620
3621 sym = c->symtree->n.sym;
3622 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3623 sym->name, &c->loc);
3624
3625 return FAILURE;
3626 }
3627
3628
3629 /* Resolve a subroutine call not known to be generic nor specific. */
3630
3631 static gfc_try
3632 resolve_unknown_s (gfc_code *c)
3633 {
3634 gfc_symbol *sym;
3635
3636 sym = c->symtree->n.sym;
3637
3638 if (sym->attr.dummy)
3639 {
3640 sym->attr.proc = PROC_DUMMY;
3641 goto found;
3642 }
3643
3644 /* See if we have an intrinsic function reference. */
3645
3646 if (gfc_is_intrinsic (sym, 1, c->loc))
3647 {
3648 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3649 return SUCCESS;
3650 return FAILURE;
3651 }
3652
3653 /* The reference is to an external name. */
3654
3655 found:
3656 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3657
3658 c->resolved_sym = sym;
3659
3660 pure_subroutine (c, sym);
3661
3662 return SUCCESS;
3663 }
3664
3665
3666 /* Resolve a subroutine call. Although it was tempting to use the same code
3667 for functions, subroutines and functions are stored differently and this
3668 makes things awkward. */
3669
3670 static gfc_try
3671 resolve_call (gfc_code *c)
3672 {
3673 gfc_try t;
3674 procedure_type ptype = PROC_INTRINSIC;
3675 gfc_symbol *csym, *sym;
3676 bool no_formal_args;
3677
3678 csym = c->symtree ? c->symtree->n.sym : NULL;
3679
3680 if (csym && csym->ts.type != BT_UNKNOWN)
3681 {
3682 gfc_error ("'%s' at %L has a type, which is not consistent with "
3683 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3684 return FAILURE;
3685 }
3686
3687 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3688 {
3689 gfc_symtree *st;
3690 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3691 sym = st ? st->n.sym : NULL;
3692 if (sym && csym != sym
3693 && sym->ns == gfc_current_ns
3694 && sym->attr.flavor == FL_PROCEDURE
3695 && sym->attr.contained)
3696 {
3697 sym->refs++;
3698 if (csym->attr.generic)
3699 c->symtree->n.sym = sym;
3700 else
3701 c->symtree = st;
3702 csym = c->symtree->n.sym;
3703 }
3704 }
3705
3706 /* If this ia a deferred TBP with an abstract interface
3707 (which may of course be referenced), c->expr1 will be set. */
3708 if (csym && csym->attr.abstract && !c->expr1)
3709 {
3710 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3711 csym->name, &c->loc);
3712 return FAILURE;
3713 }
3714
3715 /* Subroutines without the RECURSIVE attribution are not allowed to
3716 * call themselves. */
3717 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3718 {
3719 if (csym->attr.entry && csym->ns->entries)
3720 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3721 " subroutine '%s' is not RECURSIVE",
3722 csym->name, &c->loc, csym->ns->entries->sym->name);
3723 else
3724 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3725 " is not RECURSIVE", csym->name, &c->loc);
3726
3727 t = FAILURE;
3728 }
3729
3730 /* Switch off assumed size checking and do this again for certain kinds
3731 of procedure, once the procedure itself is resolved. */
3732 need_full_assumed_size++;
3733
3734 if (csym)
3735 ptype = csym->attr.proc;
3736
3737 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3738 if (resolve_actual_arglist (c->ext.actual, ptype,
3739 no_formal_args) == FAILURE)
3740 return FAILURE;
3741
3742 /* Resume assumed_size checking. */
3743 need_full_assumed_size--;
3744
3745 /* If external, check for usage. */
3746 if (csym && is_external_proc (csym))
3747 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3748
3749 t = SUCCESS;
3750 if (c->resolved_sym == NULL)
3751 {
3752 c->resolved_isym = NULL;
3753 switch (procedure_kind (csym))
3754 {
3755 case PTYPE_GENERIC:
3756 t = resolve_generic_s (c);
3757 break;
3758
3759 case PTYPE_SPECIFIC:
3760 t = resolve_specific_s (c);
3761 break;
3762
3763 case PTYPE_UNKNOWN:
3764 t = resolve_unknown_s (c);
3765 break;
3766
3767 default:
3768 gfc_internal_error ("resolve_subroutine(): bad function type");
3769 }
3770 }
3771
3772 /* Some checks of elemental subroutine actual arguments. */
3773 if (resolve_elemental_actual (NULL, c) == FAILURE)
3774 return FAILURE;
3775
3776 return t;
3777 }
3778
3779
3780 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3781 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3782 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3783 if their shapes do not match. If either op1->shape or op2->shape is
3784 NULL, return SUCCESS. */
3785
3786 static gfc_try
3787 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3788 {
3789 gfc_try t;
3790 int i;
3791
3792 t = SUCCESS;
3793
3794 if (op1->shape != NULL && op2->shape != NULL)
3795 {
3796 for (i = 0; i < op1->rank; i++)
3797 {
3798 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3799 {
3800 gfc_error ("Shapes for operands at %L and %L are not conformable",
3801 &op1->where, &op2->where);
3802 t = FAILURE;
3803 break;
3804 }
3805 }
3806 }
3807
3808 return t;
3809 }
3810
3811
3812 /* Resolve an operator expression node. This can involve replacing the
3813 operation with a user defined function call. */
3814
3815 static gfc_try
3816 resolve_operator (gfc_expr *e)
3817 {
3818 gfc_expr *op1, *op2;
3819 char msg[200];
3820 bool dual_locus_error;
3821 gfc_try t;
3822
3823 /* Resolve all subnodes-- give them types. */
3824
3825 switch (e->value.op.op)
3826 {
3827 default:
3828 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3829 return FAILURE;
3830
3831 /* Fall through... */
3832
3833 case INTRINSIC_NOT:
3834 case INTRINSIC_UPLUS:
3835 case INTRINSIC_UMINUS:
3836 case INTRINSIC_PARENTHESES:
3837 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3838 return FAILURE;
3839 break;
3840 }
3841
3842 /* Typecheck the new node. */
3843
3844 op1 = e->value.op.op1;
3845 op2 = e->value.op.op2;
3846 dual_locus_error = false;
3847
3848 if ((op1 && op1->expr_type == EXPR_NULL)
3849 || (op2 && op2->expr_type == EXPR_NULL))
3850 {
3851 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3852 goto bad_op;
3853 }
3854
3855 switch (e->value.op.op)
3856 {
3857 case INTRINSIC_UPLUS:
3858 case INTRINSIC_UMINUS:
3859 if (op1->ts.type == BT_INTEGER
3860 || op1->ts.type == BT_REAL
3861 || op1->ts.type == BT_COMPLEX)
3862 {
3863 e->ts = op1->ts;
3864 break;
3865 }
3866
3867 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3868 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3869 goto bad_op;
3870
3871 case INTRINSIC_PLUS:
3872 case INTRINSIC_MINUS:
3873 case INTRINSIC_TIMES:
3874 case INTRINSIC_DIVIDE:
3875 case INTRINSIC_POWER:
3876 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3877 {
3878 gfc_type_convert_binary (e, 1);
3879 break;
3880 }
3881
3882 sprintf (msg,
3883 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3884 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3885 gfc_typename (&op2->ts));
3886 goto bad_op;
3887
3888 case INTRINSIC_CONCAT:
3889 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3890 && op1->ts.kind == op2->ts.kind)
3891 {
3892 e->ts.type = BT_CHARACTER;
3893 e->ts.kind = op1->ts.kind;
3894 break;
3895 }
3896
3897 sprintf (msg,
3898 _("Operands of string concatenation operator at %%L are %s/%s"),
3899 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3900 goto bad_op;
3901
3902 case INTRINSIC_AND:
3903 case INTRINSIC_OR:
3904 case INTRINSIC_EQV:
3905 case INTRINSIC_NEQV:
3906 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3907 {
3908 e->ts.type = BT_LOGICAL;
3909 e->ts.kind = gfc_kind_max (op1, op2);
3910 if (op1->ts.kind < e->ts.kind)
3911 gfc_convert_type (op1, &e->ts, 2);
3912 else if (op2->ts.kind < e->ts.kind)
3913 gfc_convert_type (op2, &e->ts, 2);
3914 break;
3915 }
3916
3917 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3918 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3919 gfc_typename (&op2->ts));
3920
3921 goto bad_op;
3922
3923 case INTRINSIC_NOT:
3924 if (op1->ts.type == BT_LOGICAL)
3925 {
3926 e->ts.type = BT_LOGICAL;
3927 e->ts.kind = op1->ts.kind;
3928 break;
3929 }
3930
3931 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3932 gfc_typename (&op1->ts));
3933 goto bad_op;
3934
3935 case INTRINSIC_GT:
3936 case INTRINSIC_GT_OS:
3937 case INTRINSIC_GE:
3938 case INTRINSIC_GE_OS:
3939 case INTRINSIC_LT:
3940 case INTRINSIC_LT_OS:
3941 case INTRINSIC_LE:
3942 case INTRINSIC_LE_OS:
3943 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3944 {
3945 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3946 goto bad_op;
3947 }
3948
3949 /* Fall through... */
3950
3951 case INTRINSIC_EQ:
3952 case INTRINSIC_EQ_OS:
3953 case INTRINSIC_NE:
3954 case INTRINSIC_NE_OS:
3955 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3956 && op1->ts.kind == op2->ts.kind)
3957 {
3958 e->ts.type = BT_LOGICAL;
3959 e->ts.kind = gfc_default_logical_kind;
3960 break;
3961 }
3962
3963 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3964 {
3965 gfc_type_convert_binary (e, 1);
3966
3967 e->ts.type = BT_LOGICAL;
3968 e->ts.kind = gfc_default_logical_kind;
3969 break;
3970 }
3971
3972 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3973 sprintf (msg,
3974 _("Logicals at %%L must be compared with %s instead of %s"),
3975 (e->value.op.op == INTRINSIC_EQ
3976 || e->value.op.op == INTRINSIC_EQ_OS)
3977 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3978 else
3979 sprintf (msg,
3980 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3981 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3982 gfc_typename (&op2->ts));
3983
3984 goto bad_op;
3985
3986 case INTRINSIC_USER:
3987 if (e->value.op.uop->op == NULL)
3988 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3989 else if (op2 == NULL)
3990 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3991 e->value.op.uop->name, gfc_typename (&op1->ts));
3992 else
3993 {
3994 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3995 e->value.op.uop->name, gfc_typename (&op1->ts),
3996 gfc_typename (&op2->ts));
3997 e->value.op.uop->op->sym->attr.referenced = 1;
3998 }
3999
4000 goto bad_op;
4001
4002 case INTRINSIC_PARENTHESES:
4003 e->ts = op1->ts;
4004 if (e->ts.type == BT_CHARACTER)
4005 e->ts.u.cl = op1->ts.u.cl;
4006 break;
4007
4008 default:
4009 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4010 }
4011
4012 /* Deal with arrayness of an operand through an operator. */
4013
4014 t = SUCCESS;
4015
4016 switch (e->value.op.op)
4017 {
4018 case INTRINSIC_PLUS:
4019 case INTRINSIC_MINUS:
4020 case INTRINSIC_TIMES:
4021 case INTRINSIC_DIVIDE:
4022 case INTRINSIC_POWER:
4023 case INTRINSIC_CONCAT:
4024 case INTRINSIC_AND:
4025 case INTRINSIC_OR:
4026 case INTRINSIC_EQV:
4027 case INTRINSIC_NEQV:
4028 case INTRINSIC_EQ:
4029 case INTRINSIC_EQ_OS:
4030 case INTRINSIC_NE:
4031 case INTRINSIC_NE_OS:
4032 case INTRINSIC_GT:
4033 case INTRINSIC_GT_OS:
4034 case INTRINSIC_GE:
4035 case INTRINSIC_GE_OS:
4036 case INTRINSIC_LT:
4037 case INTRINSIC_LT_OS:
4038 case INTRINSIC_LE:
4039 case INTRINSIC_LE_OS:
4040
4041 if (op1->rank == 0 && op2->rank == 0)
4042 e->rank = 0;
4043
4044 if (op1->rank == 0 && op2->rank != 0)
4045 {
4046 e->rank = op2->rank;
4047
4048 if (e->shape == NULL)
4049 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4050 }
4051
4052 if (op1->rank != 0 && op2->rank == 0)
4053 {
4054 e->rank = op1->rank;
4055
4056 if (e->shape == NULL)
4057 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4058 }
4059
4060 if (op1->rank != 0 && op2->rank != 0)
4061 {
4062 if (op1->rank == op2->rank)
4063 {
4064 e->rank = op1->rank;
4065 if (e->shape == NULL)
4066 {
4067 t = compare_shapes (op1, op2);
4068 if (t == FAILURE)
4069 e->shape = NULL;
4070 else
4071 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4072 }
4073 }
4074 else
4075 {
4076 /* Allow higher level expressions to work. */
4077 e->rank = 0;
4078
4079 /* Try user-defined operators, and otherwise throw an error. */
4080 dual_locus_error = true;
4081 sprintf (msg,
4082 _("Inconsistent ranks for operator at %%L and %%L"));
4083 goto bad_op;
4084 }
4085 }
4086
4087 break;
4088
4089 case INTRINSIC_PARENTHESES:
4090 case INTRINSIC_NOT:
4091 case INTRINSIC_UPLUS:
4092 case INTRINSIC_UMINUS:
4093 /* Simply copy arrayness attribute */
4094 e->rank = op1->rank;
4095
4096 if (e->shape == NULL)
4097 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4098
4099 break;
4100
4101 default:
4102 break;
4103 }
4104
4105 /* Attempt to simplify the expression. */
4106 if (t == SUCCESS)
4107 {
4108 t = gfc_simplify_expr (e, 0);
4109 /* Some calls do not succeed in simplification and return FAILURE
4110 even though there is no error; e.g. variable references to
4111 PARAMETER arrays. */
4112 if (!gfc_is_constant_expr (e))
4113 t = SUCCESS;
4114 }
4115 return t;
4116
4117 bad_op:
4118
4119 {
4120 match m = gfc_extend_expr (e);
4121 if (m == MATCH_YES)
4122 return SUCCESS;
4123 if (m == MATCH_ERROR)
4124 return FAILURE;
4125 }
4126
4127 if (dual_locus_error)
4128 gfc_error (msg, &op1->where, &op2->where);
4129 else
4130 gfc_error (msg, &e->where);
4131
4132 return FAILURE;
4133 }
4134
4135
4136 /************** Array resolution subroutines **************/
4137
4138 typedef enum
4139 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
4140 comparison;
4141
4142 /* Compare two integer expressions. */
4143
4144 static comparison
4145 compare_bound (gfc_expr *a, gfc_expr *b)
4146 {
4147 int i;
4148
4149 if (a == NULL || a->expr_type != EXPR_CONSTANT
4150 || b == NULL || b->expr_type != EXPR_CONSTANT)
4151 return CMP_UNKNOWN;
4152
4153 /* If either of the types isn't INTEGER, we must have
4154 raised an error earlier. */
4155
4156 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4157 return CMP_UNKNOWN;
4158
4159 i = mpz_cmp (a->value.integer, b->value.integer);
4160
4161 if (i < 0)
4162 return CMP_LT;
4163 if (i > 0)
4164 return CMP_GT;
4165 return CMP_EQ;
4166 }
4167
4168
4169 /* Compare an integer expression with an integer. */
4170
4171 static comparison
4172 compare_bound_int (gfc_expr *a, int b)
4173 {
4174 int i;
4175
4176 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4177 return CMP_UNKNOWN;
4178
4179 if (a->ts.type != BT_INTEGER)
4180 gfc_internal_error ("compare_bound_int(): Bad expression");
4181
4182 i = mpz_cmp_si (a->value.integer, b);
4183
4184 if (i < 0)
4185 return CMP_LT;
4186 if (i > 0)
4187 return CMP_GT;
4188 return CMP_EQ;
4189 }
4190
4191
4192 /* Compare an integer expression with a mpz_t. */
4193
4194 static comparison
4195 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4196 {
4197 int i;
4198
4199 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4200 return CMP_UNKNOWN;
4201
4202 if (a->ts.type != BT_INTEGER)
4203 gfc_internal_error ("compare_bound_int(): Bad expression");
4204
4205 i = mpz_cmp (a->value.integer, b);
4206
4207 if (i < 0)
4208 return CMP_LT;
4209 if (i > 0)
4210 return CMP_GT;
4211 return CMP_EQ;
4212 }
4213
4214
4215 /* Compute the last value of a sequence given by a triplet.
4216 Return 0 if it wasn't able to compute the last value, or if the
4217 sequence if empty, and 1 otherwise. */
4218
4219 static int
4220 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4221 gfc_expr *stride, mpz_t last)
4222 {
4223 mpz_t rem;
4224
4225 if (start == NULL || start->expr_type != EXPR_CONSTANT
4226 || end == NULL || end->expr_type != EXPR_CONSTANT
4227 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4228 return 0;
4229
4230 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4231 || (stride != NULL && stride->ts.type != BT_INTEGER))
4232 return 0;
4233
4234 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4235 {
4236 if (compare_bound (start, end) == CMP_GT)
4237 return 0;
4238 mpz_set (last, end->value.integer);
4239 return 1;
4240 }
4241
4242 if (compare_bound_int (stride, 0) == CMP_GT)
4243 {
4244 /* Stride is positive */
4245 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4246 return 0;
4247 }
4248 else
4249 {
4250 /* Stride is negative */
4251 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4252 return 0;
4253 }
4254
4255 mpz_init (rem);
4256 mpz_sub (rem, end->value.integer, start->value.integer);
4257 mpz_tdiv_r (rem, rem, stride->value.integer);
4258 mpz_sub (last, end->value.integer, rem);
4259 mpz_clear (rem);
4260
4261 return 1;
4262 }
4263
4264
4265 /* Compare a single dimension of an array reference to the array
4266 specification. */
4267
4268 static gfc_try
4269 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4270 {
4271 mpz_t last_value;
4272
4273 if (ar->dimen_type[i] == DIMEN_STAR)
4274 {
4275 gcc_assert (ar->stride[i] == NULL);
4276 /* This implies [*] as [*:] and [*:3] are not possible. */
4277 if (ar->start[i] == NULL)
4278 {
4279 gcc_assert (ar->end[i] == NULL);
4280 return SUCCESS;
4281 }
4282 }
4283
4284 /* Given start, end and stride values, calculate the minimum and
4285 maximum referenced indexes. */
4286
4287 switch (ar->dimen_type[i])
4288 {
4289 case DIMEN_VECTOR:
4290 case DIMEN_THIS_IMAGE:
4291 break;
4292
4293 case DIMEN_STAR:
4294 case DIMEN_ELEMENT:
4295 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4296 {
4297 if (i < as->rank)
4298 gfc_warning ("Array reference at %L is out of bounds "
4299 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4300 mpz_get_si (ar->start[i]->value.integer),
4301 mpz_get_si (as->lower[i]->value.integer), i+1);
4302 else
4303 gfc_warning ("Array reference at %L is out of bounds "
4304 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4305 mpz_get_si (ar->start[i]->value.integer),
4306 mpz_get_si (as->lower[i]->value.integer),
4307 i + 1 - as->rank);
4308 return SUCCESS;
4309 }
4310 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4311 {
4312 if (i < as->rank)
4313 gfc_warning ("Array reference at %L is out of bounds "
4314 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4315 mpz_get_si (ar->start[i]->value.integer),
4316 mpz_get_si (as->upper[i]->value.integer), i+1);
4317 else
4318 gfc_warning ("Array reference at %L is out of bounds "
4319 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4320 mpz_get_si (ar->start[i]->value.integer),
4321 mpz_get_si (as->upper[i]->value.integer),
4322 i + 1 - as->rank);
4323 return SUCCESS;
4324 }
4325
4326 break;
4327
4328 case DIMEN_RANGE:
4329 {
4330 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4331 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4332
4333 comparison comp_start_end = compare_bound (AR_START, AR_END);
4334
4335 /* Check for zero stride, which is not allowed. */
4336 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4337 {
4338 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4339 return FAILURE;
4340 }
4341
4342 /* if start == len || (stride > 0 && start < len)
4343 || (stride < 0 && start > len),
4344 then the array section contains at least one element. In this
4345 case, there is an out-of-bounds access if
4346 (start < lower || start > upper). */
4347 if (compare_bound (AR_START, AR_END) == CMP_EQ
4348 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4349 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4350 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4351 && comp_start_end == CMP_GT))
4352 {
4353 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4354 {
4355 gfc_warning ("Lower array reference at %L is out of bounds "
4356 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4357 mpz_get_si (AR_START->value.integer),
4358 mpz_get_si (as->lower[i]->value.integer), i+1);
4359 return SUCCESS;
4360 }
4361 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4362 {
4363 gfc_warning ("Lower array reference at %L is out of bounds "
4364 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4365 mpz_get_si (AR_START->value.integer),
4366 mpz_get_si (as->upper[i]->value.integer), i+1);
4367 return SUCCESS;
4368 }
4369 }
4370
4371 /* If we can compute the highest index of the array section,
4372 then it also has to be between lower and upper. */
4373 mpz_init (last_value);
4374 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4375 last_value))
4376 {
4377 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4378 {
4379 gfc_warning ("Upper array reference at %L is out of bounds "
4380 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4381 mpz_get_si (last_value),
4382 mpz_get_si (as->lower[i]->value.integer), i+1);
4383 mpz_clear (last_value);
4384 return SUCCESS;
4385 }
4386 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4387 {
4388 gfc_warning ("Upper array reference at %L is out of bounds "
4389 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4390 mpz_get_si (last_value),
4391 mpz_get_si (as->upper[i]->value.integer), i+1);
4392 mpz_clear (last_value);
4393 return SUCCESS;
4394 }
4395 }
4396 mpz_clear (last_value);
4397
4398 #undef AR_START
4399 #undef AR_END
4400 }
4401 break;
4402
4403 default:
4404 gfc_internal_error ("check_dimension(): Bad array reference");
4405 }
4406
4407 return SUCCESS;
4408 }
4409
4410
4411 /* Compare an array reference with an array specification. */
4412
4413 static gfc_try
4414 compare_spec_to_ref (gfc_array_ref *ar)
4415 {
4416 gfc_array_spec *as;
4417 int i;
4418
4419 as = ar->as;
4420 i = as->rank - 1;
4421 /* TODO: Full array sections are only allowed as actual parameters. */
4422 if (as->type == AS_ASSUMED_SIZE
4423 && (/*ar->type == AR_FULL
4424 ||*/ (ar->type == AR_SECTION
4425 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4426 {
4427 gfc_error ("Rightmost upper bound of assumed size array section "
4428 "not specified at %L", &ar->where);
4429 return FAILURE;
4430 }
4431
4432 if (ar->type == AR_FULL)
4433 return SUCCESS;
4434
4435 if (as->rank != ar->dimen)
4436 {
4437 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4438 &ar->where, ar->dimen, as->rank);
4439 return FAILURE;
4440 }
4441
4442 /* ar->codimen == 0 is a local array. */
4443 if (as->corank != ar->codimen && ar->codimen != 0)
4444 {
4445 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4446 &ar->where, ar->codimen, as->corank);
4447 return FAILURE;
4448 }
4449
4450 for (i = 0; i < as->rank; i++)
4451 if (check_dimension (i, ar, as) == FAILURE)
4452 return FAILURE;
4453
4454 /* Local access has no coarray spec. */
4455 if (ar->codimen != 0)
4456 for (i = as->rank; i < as->rank + as->corank; i++)
4457 {
4458 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4459 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4460 {
4461 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4462 i + 1 - as->rank, &ar->where);
4463 return FAILURE;
4464 }
4465 if (check_dimension (i, ar, as) == FAILURE)
4466 return FAILURE;
4467 }
4468
4469 return SUCCESS;
4470 }
4471
4472
4473 /* Resolve one part of an array index. */
4474
4475 static gfc_try
4476 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4477 int force_index_integer_kind)
4478 {
4479 gfc_typespec ts;
4480
4481 if (index == NULL)
4482 return SUCCESS;
4483
4484 if (gfc_resolve_expr (index) == FAILURE)
4485 return FAILURE;
4486
4487 if (check_scalar && index->rank != 0)
4488 {
4489 gfc_error ("Array index at %L must be scalar", &index->where);
4490 return FAILURE;
4491 }
4492
4493 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4494 {
4495 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4496 &index->where, gfc_basic_typename (index->ts.type));
4497 return FAILURE;
4498 }
4499
4500 if (index->ts.type == BT_REAL)
4501 if (gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4502 &index->where) == FAILURE)
4503 return FAILURE;
4504
4505 if ((index->ts.kind != gfc_index_integer_kind
4506 && force_index_integer_kind)
4507 || index->ts.type != BT_INTEGER)
4508 {
4509 gfc_clear_ts (&ts);
4510 ts.type = BT_INTEGER;
4511 ts.kind = gfc_index_integer_kind;
4512
4513 gfc_convert_type_warn (index, &ts, 2, 0);
4514 }
4515
4516 return SUCCESS;
4517 }
4518
4519 /* Resolve one part of an array index. */
4520
4521 gfc_try
4522 gfc_resolve_index (gfc_expr *index, int check_scalar)
4523 {
4524 return gfc_resolve_index_1 (index, check_scalar, 1);
4525 }
4526
4527 /* Resolve a dim argument to an intrinsic function. */
4528
4529 gfc_try
4530 gfc_resolve_dim_arg (gfc_expr *dim)
4531 {
4532 if (dim == NULL)
4533 return SUCCESS;
4534
4535 if (gfc_resolve_expr (dim) == FAILURE)
4536 return FAILURE;
4537
4538 if (dim->rank != 0)
4539 {
4540 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4541 return FAILURE;
4542
4543 }
4544
4545 if (dim->ts.type != BT_INTEGER)
4546 {
4547 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4548 return FAILURE;
4549 }
4550
4551 if (dim->ts.kind != gfc_index_integer_kind)
4552 {
4553 gfc_typespec ts;
4554
4555 gfc_clear_ts (&ts);
4556 ts.type = BT_INTEGER;
4557 ts.kind = gfc_index_integer_kind;
4558
4559 gfc_convert_type_warn (dim, &ts, 2, 0);
4560 }
4561
4562 return SUCCESS;
4563 }
4564
4565 /* Given an expression that contains array references, update those array
4566 references to point to the right array specifications. While this is
4567 filled in during matching, this information is difficult to save and load
4568 in a module, so we take care of it here.
4569
4570 The idea here is that the original array reference comes from the
4571 base symbol. We traverse the list of reference structures, setting
4572 the stored reference to references. Component references can
4573 provide an additional array specification. */
4574
4575 static void
4576 find_array_spec (gfc_expr *e)
4577 {
4578 gfc_array_spec *as;
4579 gfc_component *c;
4580 gfc_ref *ref;
4581
4582 if (e->symtree->n.sym->ts.type == BT_CLASS)
4583 as = CLASS_DATA (e->symtree->n.sym)->as;
4584 else
4585 as = e->symtree->n.sym->as;
4586
4587 for (ref = e->ref; ref; ref = ref->next)
4588 switch (ref->type)
4589 {
4590 case REF_ARRAY:
4591 if (as == NULL)
4592 gfc_internal_error ("find_array_spec(): Missing spec");
4593
4594 ref->u.ar.as = as;
4595 as = NULL;
4596 break;
4597
4598 case REF_COMPONENT:
4599 c = ref->u.c.component;
4600 if (c->attr.dimension)
4601 {
4602 if (as != NULL)
4603 gfc_internal_error ("find_array_spec(): unused as(1)");
4604 as = c->as;
4605 }
4606
4607 break;
4608
4609 case REF_SUBSTRING:
4610 break;
4611 }
4612
4613 if (as != NULL)
4614 gfc_internal_error ("find_array_spec(): unused as(2)");
4615 }
4616
4617
4618 /* Resolve an array reference. */
4619
4620 static gfc_try
4621 resolve_array_ref (gfc_array_ref *ar)
4622 {
4623 int i, check_scalar;
4624 gfc_expr *e;
4625
4626 for (i = 0; i < ar->dimen + ar->codimen; i++)
4627 {
4628 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4629
4630 /* Do not force gfc_index_integer_kind for the start. We can
4631 do fine with any integer kind. This avoids temporary arrays
4632 created for indexing with a vector. */
4633 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4634 return FAILURE;
4635 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4636 return FAILURE;
4637 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4638 return FAILURE;
4639
4640 e = ar->start[i];
4641
4642 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4643 switch (e->rank)
4644 {
4645 case 0:
4646 ar->dimen_type[i] = DIMEN_ELEMENT;
4647 break;
4648
4649 case 1:
4650 ar->dimen_type[i] = DIMEN_VECTOR;
4651 if (e->expr_type == EXPR_VARIABLE
4652 && e->symtree->n.sym->ts.type == BT_DERIVED)
4653 ar->start[i] = gfc_get_parentheses (e);
4654 break;
4655
4656 default:
4657 gfc_error ("Array index at %L is an array of rank %d",
4658 &ar->c_where[i], e->rank);
4659 return FAILURE;
4660 }
4661
4662 /* Fill in the upper bound, which may be lower than the
4663 specified one for something like a(2:10:5), which is
4664 identical to a(2:7:5). Only relevant for strides not equal
4665 to one. Don't try a division by zero. */
4666 if (ar->dimen_type[i] == DIMEN_RANGE
4667 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4668 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4669 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4670 {
4671 mpz_t size, end;
4672
4673 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4674 {
4675 if (ar->end[i] == NULL)
4676 {
4677 ar->end[i] =
4678 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4679 &ar->where);
4680 mpz_set (ar->end[i]->value.integer, end);
4681 }
4682 else if (ar->end[i]->ts.type == BT_INTEGER
4683 && ar->end[i]->expr_type == EXPR_CONSTANT)
4684 {
4685 mpz_set (ar->end[i]->value.integer, end);
4686 }
4687 else
4688 gcc_unreachable ();
4689
4690 mpz_clear (size);
4691 mpz_clear (end);
4692 }
4693 }
4694 }
4695
4696 if (ar->type == AR_FULL)
4697 {
4698 if (ar->as->rank == 0)
4699 ar->type = AR_ELEMENT;
4700
4701 /* Make sure array is the same as array(:,:), this way
4702 we don't need to special case all the time. */
4703 ar->dimen = ar->as->rank;
4704 for (i = 0; i < ar->dimen; i++)
4705 {
4706 ar->dimen_type[i] = DIMEN_RANGE;
4707
4708 gcc_assert (ar->start[i] == NULL);
4709 gcc_assert (ar->end[i] == NULL);
4710 gcc_assert (ar->stride[i] == NULL);
4711 }
4712 }
4713
4714 /* If the reference type is unknown, figure out what kind it is. */
4715
4716 if (ar->type == AR_UNKNOWN)
4717 {
4718 ar->type = AR_ELEMENT;
4719 for (i = 0; i < ar->dimen; i++)
4720 if (ar->dimen_type[i] == DIMEN_RANGE
4721 || ar->dimen_type[i] == DIMEN_VECTOR)
4722 {
4723 ar->type = AR_SECTION;
4724 break;
4725 }
4726 }
4727
4728 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4729 return FAILURE;
4730
4731 if (ar->as->corank && ar->codimen == 0)
4732 {
4733 int n;
4734 ar->codimen = ar->as->corank;
4735 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4736 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4737 }
4738
4739 return SUCCESS;
4740 }
4741
4742
4743 static gfc_try
4744 resolve_substring (gfc_ref *ref)
4745 {
4746 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4747
4748 if (ref->u.ss.start != NULL)
4749 {
4750 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4751 return FAILURE;
4752
4753 if (ref->u.ss.start->ts.type != BT_INTEGER)
4754 {
4755 gfc_error ("Substring start index at %L must be of type INTEGER",
4756 &ref->u.ss.start->where);
4757 return FAILURE;
4758 }
4759
4760 if (ref->u.ss.start->rank != 0)
4761 {
4762 gfc_error ("Substring start index at %L must be scalar",
4763 &ref->u.ss.start->where);
4764 return FAILURE;
4765 }
4766
4767 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4768 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4769 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4770 {
4771 gfc_error ("Substring start index at %L is less than one",
4772 &ref->u.ss.start->where);
4773 return FAILURE;
4774 }
4775 }
4776
4777 if (ref->u.ss.end != NULL)
4778 {
4779 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4780 return FAILURE;
4781
4782 if (ref->u.ss.end->ts.type != BT_INTEGER)
4783 {
4784 gfc_error ("Substring end index at %L must be of type INTEGER",
4785 &ref->u.ss.end->where);
4786 return FAILURE;
4787 }
4788
4789 if (ref->u.ss.end->rank != 0)
4790 {
4791 gfc_error ("Substring end index at %L must be scalar",
4792 &ref->u.ss.end->where);
4793 return FAILURE;
4794 }
4795
4796 if (ref->u.ss.length != NULL
4797 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4798 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4799 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4800 {
4801 gfc_error ("Substring end index at %L exceeds the string length",
4802 &ref->u.ss.start->where);
4803 return FAILURE;
4804 }
4805
4806 if (compare_bound_mpz_t (ref->u.ss.end,
4807 gfc_integer_kinds[k].huge) == CMP_GT
4808 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4809 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4810 {
4811 gfc_error ("Substring end index at %L is too large",
4812 &ref->u.ss.end->where);
4813 return FAILURE;
4814 }
4815 }
4816
4817 return SUCCESS;
4818 }
4819
4820
4821 /* This function supplies missing substring charlens. */
4822
4823 void
4824 gfc_resolve_substring_charlen (gfc_expr *e)
4825 {
4826 gfc_ref *char_ref;
4827 gfc_expr *start, *end;
4828
4829 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4830 if (char_ref->type == REF_SUBSTRING)
4831 break;
4832
4833 if (!char_ref)
4834 return;
4835
4836 gcc_assert (char_ref->next == NULL);
4837
4838 if (e->ts.u.cl)
4839 {
4840 if (e->ts.u.cl->length)
4841 gfc_free_expr (e->ts.u.cl->length);
4842 else if (e->expr_type == EXPR_VARIABLE
4843 && e->symtree->n.sym->attr.dummy)
4844 return;
4845 }
4846
4847 e->ts.type = BT_CHARACTER;
4848 e->ts.kind = gfc_default_character_kind;
4849
4850 if (!e->ts.u.cl)
4851 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4852
4853 if (char_ref->u.ss.start)
4854 start = gfc_copy_expr (char_ref->u.ss.start);
4855 else
4856 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4857
4858 if (char_ref->u.ss.end)
4859 end = gfc_copy_expr (char_ref->u.ss.end);
4860 else if (e->expr_type == EXPR_VARIABLE)
4861 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4862 else
4863 end = NULL;
4864
4865 if (!start || !end)
4866 return;
4867
4868 /* Length = (end - start +1). */
4869 e->ts.u.cl->length = gfc_subtract (end, start);
4870 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4871 gfc_get_int_expr (gfc_default_integer_kind,
4872 NULL, 1));
4873
4874 e->ts.u.cl->length->ts.type = BT_INTEGER;
4875 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4876
4877 /* Make sure that the length is simplified. */
4878 gfc_simplify_expr (e->ts.u.cl->length, 1);
4879 gfc_resolve_expr (e->ts.u.cl->length);
4880 }
4881
4882
4883 /* Resolve subtype references. */
4884
4885 static gfc_try
4886 resolve_ref (gfc_expr *expr)
4887 {
4888 int current_part_dimension, n_components, seen_part_dimension;
4889 gfc_ref *ref;
4890
4891 for (ref = expr->ref; ref; ref = ref->next)
4892 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4893 {
4894 find_array_spec (expr);
4895 break;
4896 }
4897
4898 for (ref = expr->ref; ref; ref = ref->next)
4899 switch (ref->type)
4900 {
4901 case REF_ARRAY:
4902 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4903 return FAILURE;
4904 break;
4905
4906 case REF_COMPONENT:
4907 break;
4908
4909 case REF_SUBSTRING:
4910 if (resolve_substring (ref) == FAILURE)
4911 return FAILURE;
4912 break;
4913 }
4914
4915 /* Check constraints on part references. */
4916
4917 current_part_dimension = 0;
4918 seen_part_dimension = 0;
4919 n_components = 0;
4920
4921 for (ref = expr->ref; ref; ref = ref->next)
4922 {
4923 switch (ref->type)
4924 {
4925 case REF_ARRAY:
4926 switch (ref->u.ar.type)
4927 {
4928 case AR_FULL:
4929 /* Coarray scalar. */
4930 if (ref->u.ar.as->rank == 0)
4931 {
4932 current_part_dimension = 0;
4933 break;
4934 }
4935 /* Fall through. */
4936 case AR_SECTION:
4937 current_part_dimension = 1;
4938 break;
4939
4940 case AR_ELEMENT:
4941 current_part_dimension = 0;
4942 break;
4943
4944 case AR_UNKNOWN:
4945 gfc_internal_error ("resolve_ref(): Bad array reference");
4946 }
4947
4948 break;
4949
4950 case REF_COMPONENT:
4951 if (current_part_dimension || seen_part_dimension)
4952 {
4953 /* F03:C614. */
4954 if (ref->u.c.component->attr.pointer
4955 || ref->u.c.component->attr.proc_pointer
4956 || (ref->u.c.component->ts.type == BT_CLASS
4957 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4958 {
4959 gfc_error ("Component to the right of a part reference "
4960 "with nonzero rank must not have the POINTER "
4961 "attribute at %L", &expr->where);
4962 return FAILURE;
4963 }
4964 else if (ref->u.c.component->attr.allocatable
4965 || (ref->u.c.component->ts.type == BT_CLASS
4966 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4967
4968 {
4969 gfc_error ("Component to the right of a part reference "
4970 "with nonzero rank must not have the ALLOCATABLE "
4971 "attribute at %L", &expr->where);
4972 return FAILURE;
4973 }
4974 }
4975
4976 n_components++;
4977 break;
4978
4979 case REF_SUBSTRING:
4980 break;
4981 }
4982
4983 if (((ref->type == REF_COMPONENT && n_components > 1)
4984 || ref->next == NULL)
4985 && current_part_dimension
4986 && seen_part_dimension)
4987 {
4988 gfc_error ("Two or more part references with nonzero rank must "
4989 "not be specified at %L", &expr->where);
4990 return FAILURE;
4991 }
4992
4993 if (ref->type == REF_COMPONENT)
4994 {
4995 if (current_part_dimension)
4996 seen_part_dimension = 1;
4997
4998 /* reset to make sure */
4999 current_part_dimension = 0;
5000 }
5001 }
5002
5003 return SUCCESS;
5004 }
5005
5006
5007 /* Given an expression, determine its shape. This is easier than it sounds.
5008 Leaves the shape array NULL if it is not possible to determine the shape. */
5009
5010 static void
5011 expression_shape (gfc_expr *e)
5012 {
5013 mpz_t array[GFC_MAX_DIMENSIONS];
5014 int i;
5015
5016 if (e->rank <= 0 || e->shape != NULL)
5017 return;
5018
5019 for (i = 0; i < e->rank; i++)
5020 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
5021 goto fail;
5022
5023 e->shape = gfc_get_shape (e->rank);
5024
5025 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5026
5027 return;
5028
5029 fail:
5030 for (i--; i >= 0; i--)
5031 mpz_clear (array[i]);
5032 }
5033
5034
5035 /* Given a variable expression node, compute the rank of the expression by
5036 examining the base symbol and any reference structures it may have. */
5037
5038 static void
5039 expression_rank (gfc_expr *e)
5040 {
5041 gfc_ref *ref;
5042 int i, rank;
5043
5044 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5045 could lead to serious confusion... */
5046 gcc_assert (e->expr_type != EXPR_COMPCALL);
5047
5048 if (e->ref == NULL)
5049 {
5050 if (e->expr_type == EXPR_ARRAY)
5051 goto done;
5052 /* Constructors can have a rank different from one via RESHAPE(). */
5053
5054 if (e->symtree == NULL)
5055 {
5056 e->rank = 0;
5057 goto done;
5058 }
5059
5060 e->rank = (e->symtree->n.sym->as == NULL)
5061 ? 0 : e->symtree->n.sym->as->rank;
5062 goto done;
5063 }
5064
5065 rank = 0;
5066
5067 for (ref = e->ref; ref; ref = ref->next)
5068 {
5069 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5070 && ref->u.c.component->attr.function && !ref->next)
5071 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5072
5073 if (ref->type != REF_ARRAY)
5074 continue;
5075
5076 if (ref->u.ar.type == AR_FULL)
5077 {
5078 rank = ref->u.ar.as->rank;
5079 break;
5080 }
5081
5082 if (ref->u.ar.type == AR_SECTION)
5083 {
5084 /* Figure out the rank of the section. */
5085 if (rank != 0)
5086 gfc_internal_error ("expression_rank(): Two array specs");
5087
5088 for (i = 0; i < ref->u.ar.dimen; i++)
5089 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5090 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5091 rank++;
5092
5093 break;
5094 }
5095 }
5096
5097 e->rank = rank;
5098
5099 done:
5100 expression_shape (e);
5101 }
5102
5103
5104 /* Resolve a variable expression. */
5105
5106 static gfc_try
5107 resolve_variable (gfc_expr *e)
5108 {
5109 gfc_symbol *sym;
5110 gfc_try t;
5111
5112 t = SUCCESS;
5113
5114 if (e->symtree == NULL)
5115 return FAILURE;
5116 sym = e->symtree->n.sym;
5117
5118 /* TS 29113, 407b. */
5119 if (e->ts.type == BT_ASSUMED)
5120 {
5121 if (!actual_arg)
5122 {
5123 gfc_error ("Assumed-type variable %s at %L may only be used "
5124 "as actual argument", sym->name, &e->where);
5125 return FAILURE;
5126 }
5127 else if (inquiry_argument && !first_actual_arg)
5128 {
5129 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5130 for all inquiry functions in resolve_function; the reason is
5131 that the function-name resolution happens too late in that
5132 function. */
5133 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5134 "an inquiry function shall be the first argument",
5135 sym->name, &e->where);
5136 return FAILURE;
5137 }
5138 }
5139
5140 /* TS 29113, C535b. */
5141 if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
5142 && CLASS_DATA (sym)->as
5143 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5144 || (sym->ts.type != BT_CLASS && sym->as
5145 && sym->as->type == AS_ASSUMED_RANK))
5146 {
5147 if (!actual_arg)
5148 {
5149 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5150 "actual argument", sym->name, &e->where);
5151 return FAILURE;
5152 }
5153 else if (inquiry_argument && !first_actual_arg)
5154 {
5155 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5156 for all inquiry functions in resolve_function; the reason is
5157 that the function-name resolution happens too late in that
5158 function. */
5159 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5160 "to an inquiry function shall be the first argument",
5161 sym->name, &e->where);
5162 return FAILURE;
5163 }
5164 }
5165
5166 /* TS 29113, 407b. */
5167 if (e->ts.type == BT_ASSUMED && e->ref
5168 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5169 && e->ref->next == NULL))
5170 {
5171 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5172 "reference", sym->name, &e->ref->u.ar.where);
5173 return FAILURE;
5174 }
5175
5176 /* TS 29113, C535b. */
5177 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5178 && CLASS_DATA (sym)->as
5179 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5180 || (sym->ts.type != BT_CLASS && sym->as
5181 && sym->as->type == AS_ASSUMED_RANK))
5182 && e->ref
5183 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5184 && e->ref->next == NULL))
5185 {
5186 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5187 "reference", sym->name, &e->ref->u.ar.where);
5188 return FAILURE;
5189 }
5190
5191
5192 /* If this is an associate-name, it may be parsed with an array reference
5193 in error even though the target is scalar. Fail directly in this case.
5194 TODO Understand why class scalar expressions must be excluded. */
5195 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5196 {
5197 if (sym->ts.type == BT_CLASS)
5198 gfc_fix_class_refs (e);
5199 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5200 return FAILURE;
5201 }
5202
5203 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5204 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5205
5206 /* On the other hand, the parser may not have known this is an array;
5207 in this case, we have to add a FULL reference. */
5208 if (sym->assoc && sym->attr.dimension && !e->ref)
5209 {
5210 e->ref = gfc_get_ref ();
5211 e->ref->type = REF_ARRAY;
5212 e->ref->u.ar.type = AR_FULL;
5213 e->ref->u.ar.dimen = 0;
5214 }
5215
5216 if (e->ref && resolve_ref (e) == FAILURE)
5217 return FAILURE;
5218
5219 if (sym->attr.flavor == FL_PROCEDURE
5220 && (!sym->attr.function
5221 || (sym->attr.function && sym->result
5222 && sym->result->attr.proc_pointer
5223 && !sym->result->attr.function)))
5224 {
5225 e->ts.type = BT_PROCEDURE;
5226 goto resolve_procedure;
5227 }
5228
5229 if (sym->ts.type != BT_UNKNOWN)
5230 gfc_variable_attr (e, &e->ts);
5231 else
5232 {
5233 /* Must be a simple variable reference. */
5234 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
5235 return FAILURE;
5236 e->ts = sym->ts;
5237 }
5238
5239 if (check_assumed_size_reference (sym, e))
5240 return FAILURE;
5241
5242 /* If a PRIVATE variable is used in the specification expression of the
5243 result variable, it might be accessed from outside the module and can
5244 thus not be TREE_PUBLIC() = 0.
5245 TODO: sym->attr.public_used only has to be set for the result variable's
5246 type-parameter expression and not for dummies or automatic variables.
5247 Additionally, it only has to be set if the function is either PUBLIC or
5248 used in a generic interface or TBP; unfortunately,
5249 proc_name->attr.public_used can get set at a later stage. */
5250 if (specification_expr && sym->attr.access == ACCESS_PRIVATE
5251 && !sym->attr.function && !sym->attr.use_assoc
5252 && gfc_current_ns->proc_name && gfc_current_ns->proc_name->attr.function)
5253 sym->attr.public_used = 1;
5254
5255 /* Deal with forward references to entries during resolve_code, to
5256 satisfy, at least partially, 12.5.2.5. */
5257 if (gfc_current_ns->entries
5258 && current_entry_id == sym->entry_id
5259 && cs_base
5260 && cs_base->current
5261 && cs_base->current->op != EXEC_ENTRY)
5262 {
5263 gfc_entry_list *entry;
5264 gfc_formal_arglist *formal;
5265 int n;
5266 bool seen;
5267
5268 /* If the symbol is a dummy... */
5269 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5270 {
5271 entry = gfc_current_ns->entries;
5272 seen = false;
5273
5274 /* ...test if the symbol is a parameter of previous entries. */
5275 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5276 for (formal = entry->sym->formal; formal; formal = formal->next)
5277 {
5278 if (formal->sym && sym->name == formal->sym->name)
5279 seen = true;
5280 }
5281
5282 /* If it has not been seen as a dummy, this is an error. */
5283 if (!seen)
5284 {
5285 if (specification_expr)
5286 gfc_error ("Variable '%s', used in a specification expression"
5287 ", is referenced at %L before the ENTRY statement "
5288 "in which it is a parameter",
5289 sym->name, &cs_base->current->loc);
5290 else
5291 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5292 "statement in which it is a parameter",
5293 sym->name, &cs_base->current->loc);
5294 t = FAILURE;
5295 }
5296 }
5297
5298 /* Now do the same check on the specification expressions. */
5299 specification_expr = 1;
5300 if (sym->ts.type == BT_CHARACTER
5301 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5302 t = FAILURE;
5303
5304 if (sym->as)
5305 for (n = 0; n < sym->as->rank; n++)
5306 {
5307 specification_expr = 1;
5308 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5309 t = FAILURE;
5310 specification_expr = 1;
5311 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5312 t = FAILURE;
5313 }
5314 specification_expr = 0;
5315
5316 if (t == SUCCESS)
5317 /* Update the symbol's entry level. */
5318 sym->entry_id = current_entry_id + 1;
5319 }
5320
5321 /* If a symbol has been host_associated mark it. This is used latter,
5322 to identify if aliasing is possible via host association. */
5323 if (sym->attr.flavor == FL_VARIABLE
5324 && gfc_current_ns->parent
5325 && (gfc_current_ns->parent == sym->ns
5326 || (gfc_current_ns->parent->parent
5327 && gfc_current_ns->parent->parent == sym->ns)))
5328 sym->attr.host_assoc = 1;
5329
5330 resolve_procedure:
5331 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5332 t = FAILURE;
5333
5334 /* F2008, C617 and C1229. */
5335 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5336 && gfc_is_coindexed (e))
5337 {
5338 gfc_ref *ref, *ref2 = NULL;
5339
5340 for (ref = e->ref; ref; ref = ref->next)
5341 {
5342 if (ref->type == REF_COMPONENT)
5343 ref2 = ref;
5344 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5345 break;
5346 }
5347
5348 for ( ; ref; ref = ref->next)
5349 if (ref->type == REF_COMPONENT)
5350 break;
5351
5352 /* Expression itself is not coindexed object. */
5353 if (ref && e->ts.type == BT_CLASS)
5354 {
5355 gfc_error ("Polymorphic subobject of coindexed object at %L",
5356 &e->where);
5357 t = FAILURE;
5358 }
5359
5360 /* Expression itself is coindexed object. */
5361 if (ref == NULL)
5362 {
5363 gfc_component *c;
5364 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5365 for ( ; c; c = c->next)
5366 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5367 {
5368 gfc_error ("Coindexed object with polymorphic allocatable "
5369 "subcomponent at %L", &e->where);
5370 t = FAILURE;
5371 break;
5372 }
5373 }
5374 }
5375
5376 return t;
5377 }
5378
5379
5380 /* Checks to see that the correct symbol has been host associated.
5381 The only situation where this arises is that in which a twice
5382 contained function is parsed after the host association is made.
5383 Therefore, on detecting this, change the symbol in the expression
5384 and convert the array reference into an actual arglist if the old
5385 symbol is a variable. */
5386 static bool
5387 check_host_association (gfc_expr *e)
5388 {
5389 gfc_symbol *sym, *old_sym;
5390 gfc_symtree *st;
5391 int n;
5392 gfc_ref *ref;
5393 gfc_actual_arglist *arg, *tail = NULL;
5394 bool retval = e->expr_type == EXPR_FUNCTION;
5395
5396 /* If the expression is the result of substitution in
5397 interface.c(gfc_extend_expr) because there is no way in
5398 which the host association can be wrong. */
5399 if (e->symtree == NULL
5400 || e->symtree->n.sym == NULL
5401 || e->user_operator)
5402 return retval;
5403
5404 old_sym = e->symtree->n.sym;
5405
5406 if (gfc_current_ns->parent
5407 && old_sym->ns != gfc_current_ns)
5408 {
5409 /* Use the 'USE' name so that renamed module symbols are
5410 correctly handled. */
5411 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5412
5413 if (sym && old_sym != sym
5414 && sym->ts.type == old_sym->ts.type
5415 && sym->attr.flavor == FL_PROCEDURE
5416 && sym->attr.contained)
5417 {
5418 /* Clear the shape, since it might not be valid. */
5419 gfc_free_shape (&e->shape, e->rank);
5420
5421 /* Give the expression the right symtree! */
5422 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5423 gcc_assert (st != NULL);
5424
5425 if (old_sym->attr.flavor == FL_PROCEDURE
5426 || e->expr_type == EXPR_FUNCTION)
5427 {
5428 /* Original was function so point to the new symbol, since
5429 the actual argument list is already attached to the
5430 expression. */
5431 e->value.function.esym = NULL;
5432 e->symtree = st;
5433 }
5434 else
5435 {
5436 /* Original was variable so convert array references into
5437 an actual arglist. This does not need any checking now
5438 since resolve_function will take care of it. */
5439 e->value.function.actual = NULL;
5440 e->expr_type = EXPR_FUNCTION;
5441 e->symtree = st;
5442
5443 /* Ambiguity will not arise if the array reference is not
5444 the last reference. */
5445 for (ref = e->ref; ref; ref = ref->next)
5446 if (ref->type == REF_ARRAY && ref->next == NULL)
5447 break;
5448
5449 gcc_assert (ref->type == REF_ARRAY);
5450
5451 /* Grab the start expressions from the array ref and
5452 copy them into actual arguments. */
5453 for (n = 0; n < ref->u.ar.dimen; n++)
5454 {
5455 arg = gfc_get_actual_arglist ();
5456 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5457 if (e->value.function.actual == NULL)
5458 tail = e->value.function.actual = arg;
5459 else
5460 {
5461 tail->next = arg;
5462 tail = arg;
5463 }
5464 }
5465
5466 /* Dump the reference list and set the rank. */
5467 gfc_free_ref_list (e->ref);
5468 e->ref = NULL;
5469 e->rank = sym->as ? sym->as->rank : 0;
5470 }
5471
5472 gfc_resolve_expr (e);
5473 sym->refs++;
5474 }
5475 }
5476 /* This might have changed! */
5477 return e->expr_type == EXPR_FUNCTION;
5478 }
5479
5480
5481 static void
5482 gfc_resolve_character_operator (gfc_expr *e)
5483 {
5484 gfc_expr *op1 = e->value.op.op1;
5485 gfc_expr *op2 = e->value.op.op2;
5486 gfc_expr *e1 = NULL;
5487 gfc_expr *e2 = NULL;
5488
5489 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5490
5491 if (op1->ts.u.cl && op1->ts.u.cl->length)
5492 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5493 else if (op1->expr_type == EXPR_CONSTANT)
5494 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5495 op1->value.character.length);
5496
5497 if (op2->ts.u.cl && op2->ts.u.cl->length)
5498 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5499 else if (op2->expr_type == EXPR_CONSTANT)
5500 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5501 op2->value.character.length);
5502
5503 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5504
5505 if (!e1 || !e2)
5506 return;
5507
5508 e->ts.u.cl->length = gfc_add (e1, e2);
5509 e->ts.u.cl->length->ts.type = BT_INTEGER;
5510 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5511 gfc_simplify_expr (e->ts.u.cl->length, 0);
5512 gfc_resolve_expr (e->ts.u.cl->length);
5513
5514 return;
5515 }
5516
5517
5518 /* Ensure that an character expression has a charlen and, if possible, a
5519 length expression. */
5520
5521 static void
5522 fixup_charlen (gfc_expr *e)
5523 {
5524 /* The cases fall through so that changes in expression type and the need
5525 for multiple fixes are picked up. In all circumstances, a charlen should
5526 be available for the middle end to hang a backend_decl on. */
5527 switch (e->expr_type)
5528 {
5529 case EXPR_OP:
5530 gfc_resolve_character_operator (e);
5531
5532 case EXPR_ARRAY:
5533 if (e->expr_type == EXPR_ARRAY)
5534 gfc_resolve_character_array_constructor (e);
5535
5536 case EXPR_SUBSTRING:
5537 if (!e->ts.u.cl && e->ref)
5538 gfc_resolve_substring_charlen (e);
5539
5540 default:
5541 if (!e->ts.u.cl)
5542 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5543
5544 break;
5545 }
5546 }
5547
5548
5549 /* Update an actual argument to include the passed-object for type-bound
5550 procedures at the right position. */
5551
5552 static gfc_actual_arglist*
5553 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5554 const char *name)
5555 {
5556 gcc_assert (argpos > 0);
5557
5558 if (argpos == 1)
5559 {
5560 gfc_actual_arglist* result;
5561
5562 result = gfc_get_actual_arglist ();
5563 result->expr = po;
5564 result->next = lst;
5565 if (name)
5566 result->name = name;
5567
5568 return result;
5569 }
5570
5571 if (lst)
5572 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5573 else
5574 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5575 return lst;
5576 }
5577
5578
5579 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5580
5581 static gfc_expr*
5582 extract_compcall_passed_object (gfc_expr* e)
5583 {
5584 gfc_expr* po;
5585
5586 gcc_assert (e->expr_type == EXPR_COMPCALL);
5587
5588 if (e->value.compcall.base_object)
5589 po = gfc_copy_expr (e->value.compcall.base_object);
5590 else
5591 {
5592 po = gfc_get_expr ();
5593 po->expr_type = EXPR_VARIABLE;
5594 po->symtree = e->symtree;
5595 po->ref = gfc_copy_ref (e->ref);
5596 po->where = e->where;
5597 }
5598
5599 if (gfc_resolve_expr (po) == FAILURE)
5600 return NULL;
5601
5602 return po;
5603 }
5604
5605
5606 /* Update the arglist of an EXPR_COMPCALL expression to include the
5607 passed-object. */
5608
5609 static gfc_try
5610 update_compcall_arglist (gfc_expr* e)
5611 {
5612 gfc_expr* po;
5613 gfc_typebound_proc* tbp;
5614
5615 tbp = e->value.compcall.tbp;
5616
5617 if (tbp->error)
5618 return FAILURE;
5619
5620 po = extract_compcall_passed_object (e);
5621 if (!po)
5622 return FAILURE;
5623
5624 if (tbp->nopass || e->value.compcall.ignore_pass)
5625 {
5626 gfc_free_expr (po);
5627 return SUCCESS;
5628 }
5629
5630 gcc_assert (tbp->pass_arg_num > 0);
5631 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5632 tbp->pass_arg_num,
5633 tbp->pass_arg);
5634
5635 return SUCCESS;
5636 }
5637
5638
5639 /* Extract the passed object from a PPC call (a copy of it). */
5640
5641 static gfc_expr*
5642 extract_ppc_passed_object (gfc_expr *e)
5643 {
5644 gfc_expr *po;
5645 gfc_ref **ref;
5646
5647 po = gfc_get_expr ();
5648 po->expr_type = EXPR_VARIABLE;
5649 po->symtree = e->symtree;
5650 po->ref = gfc_copy_ref (e->ref);
5651 po->where = e->where;
5652
5653 /* Remove PPC reference. */
5654 ref = &po->ref;
5655 while ((*ref)->next)
5656 ref = &(*ref)->next;
5657 gfc_free_ref_list (*ref);
5658 *ref = NULL;
5659
5660 if (gfc_resolve_expr (po) == FAILURE)
5661 return NULL;
5662
5663 return po;
5664 }
5665
5666
5667 /* Update the actual arglist of a procedure pointer component to include the
5668 passed-object. */
5669
5670 static gfc_try
5671 update_ppc_arglist (gfc_expr* e)
5672 {
5673 gfc_expr* po;
5674 gfc_component *ppc;
5675 gfc_typebound_proc* tb;
5676
5677 if (!gfc_is_proc_ptr_comp (e, &ppc))
5678 return FAILURE;
5679
5680 tb = ppc->tb;
5681
5682 if (tb->error)
5683 return FAILURE;
5684 else if (tb->nopass)
5685 return SUCCESS;
5686
5687 po = extract_ppc_passed_object (e);
5688 if (!po)
5689 return FAILURE;
5690
5691 /* F08:R739. */
5692 if (po->rank != 0)
5693 {
5694 gfc_error ("Passed-object at %L must be scalar", &e->where);
5695 return FAILURE;
5696 }
5697
5698 /* F08:C611. */
5699 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5700 {
5701 gfc_error ("Base object for procedure-pointer component call at %L is of"
5702 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5703 return FAILURE;
5704 }
5705
5706 gcc_assert (tb->pass_arg_num > 0);
5707 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5708 tb->pass_arg_num,
5709 tb->pass_arg);
5710
5711 return SUCCESS;
5712 }
5713
5714
5715 /* Check that the object a TBP is called on is valid, i.e. it must not be
5716 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5717
5718 static gfc_try
5719 check_typebound_baseobject (gfc_expr* e)
5720 {
5721 gfc_expr* base;
5722 gfc_try return_value = FAILURE;
5723
5724 base = extract_compcall_passed_object (e);
5725 if (!base)
5726 return FAILURE;
5727
5728 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5729
5730 /* F08:C611. */
5731 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5732 {
5733 gfc_error ("Base object for type-bound procedure call at %L is of"
5734 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5735 goto cleanup;
5736 }
5737
5738 /* F08:C1230. If the procedure called is NOPASS,
5739 the base object must be scalar. */
5740 if (e->value.compcall.tbp->nopass && base->rank != 0)
5741 {
5742 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5743 " be scalar", &e->where);
5744 goto cleanup;
5745 }
5746
5747 return_value = SUCCESS;
5748
5749 cleanup:
5750 gfc_free_expr (base);
5751 return return_value;
5752 }
5753
5754
5755 /* Resolve a call to a type-bound procedure, either function or subroutine,
5756 statically from the data in an EXPR_COMPCALL expression. The adapted
5757 arglist and the target-procedure symtree are returned. */
5758
5759 static gfc_try
5760 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5761 gfc_actual_arglist** actual)
5762 {
5763 gcc_assert (e->expr_type == EXPR_COMPCALL);
5764 gcc_assert (!e->value.compcall.tbp->is_generic);
5765
5766 /* Update the actual arglist for PASS. */
5767 if (update_compcall_arglist (e) == FAILURE)
5768 return FAILURE;
5769
5770 *actual = e->value.compcall.actual;
5771 *target = e->value.compcall.tbp->u.specific;
5772
5773 gfc_free_ref_list (e->ref);
5774 e->ref = NULL;
5775 e->value.compcall.actual = NULL;
5776
5777 /* If we find a deferred typebound procedure, check for derived types
5778 that an overriding typebound procedure has not been missed. */
5779 if (e->value.compcall.name
5780 && !e->value.compcall.tbp->non_overridable
5781 && e->value.compcall.base_object
5782 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5783 {
5784 gfc_symtree *st;
5785 gfc_symbol *derived;
5786
5787 /* Use the derived type of the base_object. */
5788 derived = e->value.compcall.base_object->ts.u.derived;
5789 st = NULL;
5790
5791 /* If necessary, go through the inheritance chain. */
5792 while (!st && derived)
5793 {
5794 /* Look for the typebound procedure 'name'. */
5795 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5796 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5797 e->value.compcall.name);
5798 if (!st)
5799 derived = gfc_get_derived_super_type (derived);
5800 }
5801
5802 /* Now find the specific name in the derived type namespace. */
5803 if (st && st->n.tb && st->n.tb->u.specific)
5804 gfc_find_sym_tree (st->n.tb->u.specific->name,
5805 derived->ns, 1, &st);
5806 if (st)
5807 *target = st;
5808 }
5809 return SUCCESS;
5810 }
5811
5812
5813 /* Get the ultimate declared type from an expression. In addition,
5814 return the last class/derived type reference and the copy of the
5815 reference list. If check_types is set true, derived types are
5816 identified as well as class references. */
5817 static gfc_symbol*
5818 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5819 gfc_expr *e, bool check_types)
5820 {
5821 gfc_symbol *declared;
5822 gfc_ref *ref;
5823
5824 declared = NULL;
5825 if (class_ref)
5826 *class_ref = NULL;
5827 if (new_ref)
5828 *new_ref = gfc_copy_ref (e->ref);
5829
5830 for (ref = e->ref; ref; ref = ref->next)
5831 {
5832 if (ref->type != REF_COMPONENT)
5833 continue;
5834
5835 if ((ref->u.c.component->ts.type == BT_CLASS
5836 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5837 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5838 {
5839 declared = ref->u.c.component->ts.u.derived;
5840 if (class_ref)
5841 *class_ref = ref;
5842 }
5843 }
5844
5845 if (declared == NULL)
5846 declared = e->symtree->n.sym->ts.u.derived;
5847
5848 return declared;
5849 }
5850
5851
5852 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5853 which of the specific bindings (if any) matches the arglist and transform
5854 the expression into a call of that binding. */
5855
5856 static gfc_try
5857 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5858 {
5859 gfc_typebound_proc* genproc;
5860 const char* genname;
5861 gfc_symtree *st;
5862 gfc_symbol *derived;
5863
5864 gcc_assert (e->expr_type == EXPR_COMPCALL);
5865 genname = e->value.compcall.name;
5866 genproc = e->value.compcall.tbp;
5867
5868 if (!genproc->is_generic)
5869 return SUCCESS;
5870
5871 /* Try the bindings on this type and in the inheritance hierarchy. */
5872 for (; genproc; genproc = genproc->overridden)
5873 {
5874 gfc_tbp_generic* g;
5875
5876 gcc_assert (genproc->is_generic);
5877 for (g = genproc->u.generic; g; g = g->next)
5878 {
5879 gfc_symbol* target;
5880 gfc_actual_arglist* args;
5881 bool matches;
5882
5883 gcc_assert (g->specific);
5884
5885 if (g->specific->error)
5886 continue;
5887
5888 target = g->specific->u.specific->n.sym;
5889
5890 /* Get the right arglist by handling PASS/NOPASS. */
5891 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5892 if (!g->specific->nopass)
5893 {
5894 gfc_expr* po;
5895 po = extract_compcall_passed_object (e);
5896 if (!po)
5897 return FAILURE;
5898
5899 gcc_assert (g->specific->pass_arg_num > 0);
5900 gcc_assert (!g->specific->error);
5901 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5902 g->specific->pass_arg);
5903 }
5904 resolve_actual_arglist (args, target->attr.proc,
5905 is_external_proc (target) && !target->formal);
5906
5907 /* Check if this arglist matches the formal. */
5908 matches = gfc_arglist_matches_symbol (&args, target);
5909
5910 /* Clean up and break out of the loop if we've found it. */
5911 gfc_free_actual_arglist (args);
5912 if (matches)
5913 {
5914 e->value.compcall.tbp = g->specific;
5915 genname = g->specific_st->name;
5916 /* Pass along the name for CLASS methods, where the vtab
5917 procedure pointer component has to be referenced. */
5918 if (name)
5919 *name = genname;
5920 goto success;
5921 }
5922 }
5923 }
5924
5925 /* Nothing matching found! */
5926 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5927 " '%s' at %L", genname, &e->where);
5928 return FAILURE;
5929
5930 success:
5931 /* Make sure that we have the right specific instance for the name. */
5932 derived = get_declared_from_expr (NULL, NULL, e, true);
5933
5934 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5935 if (st)
5936 e->value.compcall.tbp = st->n.tb;
5937
5938 return SUCCESS;
5939 }
5940
5941
5942 /* Resolve a call to a type-bound subroutine. */
5943
5944 static gfc_try
5945 resolve_typebound_call (gfc_code* c, const char **name)
5946 {
5947 gfc_actual_arglist* newactual;
5948 gfc_symtree* target;
5949
5950 /* Check that's really a SUBROUTINE. */
5951 if (!c->expr1->value.compcall.tbp->subroutine)
5952 {
5953 gfc_error ("'%s' at %L should be a SUBROUTINE",
5954 c->expr1->value.compcall.name, &c->loc);
5955 return FAILURE;
5956 }
5957
5958 if (check_typebound_baseobject (c->expr1) == FAILURE)
5959 return FAILURE;
5960
5961 /* Pass along the name for CLASS methods, where the vtab
5962 procedure pointer component has to be referenced. */
5963 if (name)
5964 *name = c->expr1->value.compcall.name;
5965
5966 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5967 return FAILURE;
5968
5969 /* Transform into an ordinary EXEC_CALL for now. */
5970
5971 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5972 return FAILURE;
5973
5974 c->ext.actual = newactual;
5975 c->symtree = target;
5976 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5977
5978 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5979
5980 gfc_free_expr (c->expr1);
5981 c->expr1 = gfc_get_expr ();
5982 c->expr1->expr_type = EXPR_FUNCTION;
5983 c->expr1->symtree = target;
5984 c->expr1->where = c->loc;
5985
5986 return resolve_call (c);
5987 }
5988
5989
5990 /* Resolve a component-call expression. */
5991 static gfc_try
5992 resolve_compcall (gfc_expr* e, const char **name)
5993 {
5994 gfc_actual_arglist* newactual;
5995 gfc_symtree* target;
5996
5997 /* Check that's really a FUNCTION. */
5998 if (!e->value.compcall.tbp->function)
5999 {
6000 gfc_error ("'%s' at %L should be a FUNCTION",
6001 e->value.compcall.name, &e->where);
6002 return FAILURE;
6003 }
6004
6005 /* These must not be assign-calls! */
6006 gcc_assert (!e->value.compcall.assign);
6007
6008 if (check_typebound_baseobject (e) == FAILURE)
6009 return FAILURE;
6010
6011 /* Pass along the name for CLASS methods, where the vtab
6012 procedure pointer component has to be referenced. */
6013 if (name)
6014 *name = e->value.compcall.name;
6015
6016 if (resolve_typebound_generic_call (e, name) == FAILURE)
6017 return FAILURE;
6018 gcc_assert (!e->value.compcall.tbp->is_generic);
6019
6020 /* Take the rank from the function's symbol. */
6021 if (e->value.compcall.tbp->u.specific->n.sym->as)
6022 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6023
6024 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6025 arglist to the TBP's binding target. */
6026
6027 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
6028 return FAILURE;
6029
6030 e->value.function.actual = newactual;
6031 e->value.function.name = NULL;
6032 e->value.function.esym = target->n.sym;
6033 e->value.function.isym = NULL;
6034 e->symtree = target;
6035 e->ts = target->n.sym->ts;
6036 e->expr_type = EXPR_FUNCTION;
6037
6038 /* Resolution is not necessary if this is a class subroutine; this
6039 function only has to identify the specific proc. Resolution of
6040 the call will be done next in resolve_typebound_call. */
6041 return gfc_resolve_expr (e);
6042 }
6043
6044
6045
6046 /* Resolve a typebound function, or 'method'. First separate all
6047 the non-CLASS references by calling resolve_compcall directly. */
6048
6049 static gfc_try
6050 resolve_typebound_function (gfc_expr* e)
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 = e->symtree;
6063
6064 /* Deal with typebound operators for CLASS objects. */
6065 expr = e->value.compcall.base_object;
6066 overridable = !e->value.compcall.tbp->non_overridable;
6067 if (expr && expr->ts.type == BT_CLASS && e->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 for (args= e->value.function.actual; args; args = args->next)
6078 {
6079 if (expr == args->expr)
6080 expr = args->expr;
6081 }
6082 }
6083
6084 /* Since the typebound operators are generic, we have to ensure
6085 that any delays in resolution are corrected and that the vtab
6086 is present. */
6087 ts = expr->ts;
6088 declared = ts.u.derived;
6089 c = gfc_find_component (declared, "_vptr", true, true);
6090 if (c->ts.u.derived == NULL)
6091 c->ts.u.derived = gfc_find_derived_vtab (declared);
6092
6093 if (resolve_compcall (e, &name) == FAILURE)
6094 return FAILURE;
6095
6096 /* Use the generic name if it is there. */
6097 name = name ? name : e->value.function.esym->name;
6098 e->symtree = expr->symtree;
6099 e->ref = gfc_copy_ref (expr->ref);
6100 get_declared_from_expr (&class_ref, NULL, e, false);
6101
6102 /* Trim away the extraneous references that emerge from nested
6103 use of interface.c (extend_expr). */
6104 if (class_ref && class_ref->next)
6105 {
6106 gfc_free_ref_list (class_ref->next);
6107 class_ref->next = NULL;
6108 }
6109 else if (e->ref && !class_ref)
6110 {
6111 gfc_free_ref_list (e->ref);
6112 e->ref = NULL;
6113 }
6114
6115 gfc_add_vptr_component (e);
6116 gfc_add_component_ref (e, name);
6117 e->value.function.esym = NULL;
6118 if (expr->expr_type != EXPR_VARIABLE)
6119 e->base_expr = expr;
6120 return SUCCESS;
6121 }
6122
6123 if (st == NULL)
6124 return resolve_compcall (e, NULL);
6125
6126 if (resolve_ref (e) == FAILURE)
6127 return FAILURE;
6128
6129 /* Get the CLASS declared type. */
6130 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6131
6132 /* Weed out cases of the ultimate component being a derived type. */
6133 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6134 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6135 {
6136 gfc_free_ref_list (new_ref);
6137 return resolve_compcall (e, NULL);
6138 }
6139
6140 c = gfc_find_component (declared, "_data", true, true);
6141 declared = c->ts.u.derived;
6142
6143 /* Treat the call as if it is a typebound procedure, in order to roll
6144 out the correct name for the specific function. */
6145 if (resolve_compcall (e, &name) == FAILURE)
6146 return FAILURE;
6147 ts = e->ts;
6148
6149 if (overridable)
6150 {
6151 /* Convert the expression to a procedure pointer component call. */
6152 e->value.function.esym = NULL;
6153 e->symtree = st;
6154
6155 if (new_ref)
6156 e->ref = new_ref;
6157
6158 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6159 gfc_add_vptr_component (e);
6160 gfc_add_component_ref (e, name);
6161
6162 /* Recover the typespec for the expression. This is really only
6163 necessary for generic procedures, where the additional call
6164 to gfc_add_component_ref seems to throw the collection of the
6165 correct typespec. */
6166 e->ts = ts;
6167 }
6168
6169 return SUCCESS;
6170 }
6171
6172 /* Resolve a typebound subroutine, or 'method'. First separate all
6173 the non-CLASS references by calling resolve_typebound_call
6174 directly. */
6175
6176 static gfc_try
6177 resolve_typebound_subroutine (gfc_code *code)
6178 {
6179 gfc_symbol *declared;
6180 gfc_component *c;
6181 gfc_ref *new_ref;
6182 gfc_ref *class_ref;
6183 gfc_symtree *st;
6184 const char *name;
6185 gfc_typespec ts;
6186 gfc_expr *expr;
6187 bool overridable;
6188
6189 st = code->expr1->symtree;
6190
6191 /* Deal with typebound operators for CLASS objects. */
6192 expr = code->expr1->value.compcall.base_object;
6193 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6194 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6195 {
6196 /* If the base_object is not a variable, the corresponding actual
6197 argument expression must be stored in e->base_expression so
6198 that the corresponding tree temporary can be used as the base
6199 object in gfc_conv_procedure_call. */
6200 if (expr->expr_type != EXPR_VARIABLE)
6201 {
6202 gfc_actual_arglist *args;
6203
6204 args= code->expr1->value.function.actual;
6205 for (; args; args = args->next)
6206 if (expr == args->expr)
6207 expr = args->expr;
6208 }
6209
6210 /* Since the typebound operators are generic, we have to ensure
6211 that any delays in resolution are corrected and that the vtab
6212 is present. */
6213 declared = expr->ts.u.derived;
6214 c = gfc_find_component (declared, "_vptr", true, true);
6215 if (c->ts.u.derived == NULL)
6216 c->ts.u.derived = gfc_find_derived_vtab (declared);
6217
6218 if (resolve_typebound_call (code, &name) == FAILURE)
6219 return FAILURE;
6220
6221 /* Use the generic name if it is there. */
6222 name = name ? name : code->expr1->value.function.esym->name;
6223 code->expr1->symtree = expr->symtree;
6224 code->expr1->ref = gfc_copy_ref (expr->ref);
6225
6226 /* Trim away the extraneous references that emerge from nested
6227 use of interface.c (extend_expr). */
6228 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6229 if (class_ref && class_ref->next)
6230 {
6231 gfc_free_ref_list (class_ref->next);
6232 class_ref->next = NULL;
6233 }
6234 else if (code->expr1->ref && !class_ref)
6235 {
6236 gfc_free_ref_list (code->expr1->ref);
6237 code->expr1->ref = NULL;
6238 }
6239
6240 /* Now use the procedure in the vtable. */
6241 gfc_add_vptr_component (code->expr1);
6242 gfc_add_component_ref (code->expr1, name);
6243 code->expr1->value.function.esym = NULL;
6244 if (expr->expr_type != EXPR_VARIABLE)
6245 code->expr1->base_expr = expr;
6246 return SUCCESS;
6247 }
6248
6249 if (st == NULL)
6250 return resolve_typebound_call (code, NULL);
6251
6252 if (resolve_ref (code->expr1) == FAILURE)
6253 return FAILURE;
6254
6255 /* Get the CLASS declared type. */
6256 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6257
6258 /* Weed out cases of the ultimate component being a derived type. */
6259 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6260 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6261 {
6262 gfc_free_ref_list (new_ref);
6263 return resolve_typebound_call (code, NULL);
6264 }
6265
6266 if (resolve_typebound_call (code, &name) == FAILURE)
6267 return FAILURE;
6268 ts = code->expr1->ts;
6269
6270 if (overridable)
6271 {
6272 /* Convert the expression to a procedure pointer component call. */
6273 code->expr1->value.function.esym = NULL;
6274 code->expr1->symtree = st;
6275
6276 if (new_ref)
6277 code->expr1->ref = new_ref;
6278
6279 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6280 gfc_add_vptr_component (code->expr1);
6281 gfc_add_component_ref (code->expr1, name);
6282
6283 /* Recover the typespec for the expression. This is really only
6284 necessary for generic procedures, where the additional call
6285 to gfc_add_component_ref seems to throw the collection of the
6286 correct typespec. */
6287 code->expr1->ts = ts;
6288 }
6289
6290 return SUCCESS;
6291 }
6292
6293
6294 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6295
6296 static gfc_try
6297 resolve_ppc_call (gfc_code* c)
6298 {
6299 gfc_component *comp;
6300 bool b;
6301
6302 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
6303 gcc_assert (b);
6304
6305 c->resolved_sym = c->expr1->symtree->n.sym;
6306 c->expr1->expr_type = EXPR_VARIABLE;
6307
6308 if (!comp->attr.subroutine)
6309 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6310
6311 if (resolve_ref (c->expr1) == FAILURE)
6312 return FAILURE;
6313
6314 if (update_ppc_arglist (c->expr1) == FAILURE)
6315 return FAILURE;
6316
6317 c->ext.actual = c->expr1->value.compcall.actual;
6318
6319 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6320 comp->formal == NULL) == FAILURE)
6321 return FAILURE;
6322
6323 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6324
6325 return SUCCESS;
6326 }
6327
6328
6329 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6330
6331 static gfc_try
6332 resolve_expr_ppc (gfc_expr* e)
6333 {
6334 gfc_component *comp;
6335 bool b;
6336
6337 b = gfc_is_proc_ptr_comp (e, &comp);
6338 gcc_assert (b);
6339
6340 /* Convert to EXPR_FUNCTION. */
6341 e->expr_type = EXPR_FUNCTION;
6342 e->value.function.isym = NULL;
6343 e->value.function.actual = e->value.compcall.actual;
6344 e->ts = comp->ts;
6345 if (comp->as != NULL)
6346 e->rank = comp->as->rank;
6347
6348 if (!comp->attr.function)
6349 gfc_add_function (&comp->attr, comp->name, &e->where);
6350
6351 if (resolve_ref (e) == FAILURE)
6352 return FAILURE;
6353
6354 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6355 comp->formal == NULL) == FAILURE)
6356 return FAILURE;
6357
6358 if (update_ppc_arglist (e) == FAILURE)
6359 return FAILURE;
6360
6361 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6362
6363 return SUCCESS;
6364 }
6365
6366
6367 static bool
6368 gfc_is_expandable_expr (gfc_expr *e)
6369 {
6370 gfc_constructor *con;
6371
6372 if (e->expr_type == EXPR_ARRAY)
6373 {
6374 /* Traverse the constructor looking for variables that are flavor
6375 parameter. Parameters must be expanded since they are fully used at
6376 compile time. */
6377 con = gfc_constructor_first (e->value.constructor);
6378 for (; con; con = gfc_constructor_next (con))
6379 {
6380 if (con->expr->expr_type == EXPR_VARIABLE
6381 && con->expr->symtree
6382 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6383 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6384 return true;
6385 if (con->expr->expr_type == EXPR_ARRAY
6386 && gfc_is_expandable_expr (con->expr))
6387 return true;
6388 }
6389 }
6390
6391 return false;
6392 }
6393
6394 /* Resolve an expression. That is, make sure that types of operands agree
6395 with their operators, intrinsic operators are converted to function calls
6396 for overloaded types and unresolved function references are resolved. */
6397
6398 gfc_try
6399 gfc_resolve_expr (gfc_expr *e)
6400 {
6401 gfc_try t;
6402 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6403
6404 if (e == NULL)
6405 return SUCCESS;
6406
6407 /* inquiry_argument only applies to variables. */
6408 inquiry_save = inquiry_argument;
6409 actual_arg_save = actual_arg;
6410 first_actual_arg_save = first_actual_arg;
6411
6412 if (e->expr_type != EXPR_VARIABLE)
6413 {
6414 inquiry_argument = false;
6415 actual_arg = false;
6416 first_actual_arg = false;
6417 }
6418
6419 switch (e->expr_type)
6420 {
6421 case EXPR_OP:
6422 t = resolve_operator (e);
6423 break;
6424
6425 case EXPR_FUNCTION:
6426 case EXPR_VARIABLE:
6427
6428 if (check_host_association (e))
6429 t = resolve_function (e);
6430 else
6431 {
6432 t = resolve_variable (e);
6433 if (t == SUCCESS)
6434 expression_rank (e);
6435 }
6436
6437 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6438 && e->ref->type != REF_SUBSTRING)
6439 gfc_resolve_substring_charlen (e);
6440
6441 break;
6442
6443 case EXPR_COMPCALL:
6444 t = resolve_typebound_function (e);
6445 break;
6446
6447 case EXPR_SUBSTRING:
6448 t = resolve_ref (e);
6449 break;
6450
6451 case EXPR_CONSTANT:
6452 case EXPR_NULL:
6453 t = SUCCESS;
6454 break;
6455
6456 case EXPR_PPC:
6457 t = resolve_expr_ppc (e);
6458 break;
6459
6460 case EXPR_ARRAY:
6461 t = FAILURE;
6462 if (resolve_ref (e) == FAILURE)
6463 break;
6464
6465 t = gfc_resolve_array_constructor (e);
6466 /* Also try to expand a constructor. */
6467 if (t == SUCCESS)
6468 {
6469 expression_rank (e);
6470 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6471 gfc_expand_constructor (e, false);
6472 }
6473
6474 /* This provides the opportunity for the length of constructors with
6475 character valued function elements to propagate the string length
6476 to the expression. */
6477 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6478 {
6479 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6480 here rather then add a duplicate test for it above. */
6481 gfc_expand_constructor (e, false);
6482 t = gfc_resolve_character_array_constructor (e);
6483 }
6484
6485 break;
6486
6487 case EXPR_STRUCTURE:
6488 t = resolve_ref (e);
6489 if (t == FAILURE)
6490 break;
6491
6492 t = resolve_structure_cons (e, 0);
6493 if (t == FAILURE)
6494 break;
6495
6496 t = gfc_simplify_expr (e, 0);
6497 break;
6498
6499 default:
6500 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6501 }
6502
6503 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6504 fixup_charlen (e);
6505
6506 inquiry_argument = inquiry_save;
6507 actual_arg = actual_arg_save;
6508 first_actual_arg = first_actual_arg_save;
6509
6510 return t;
6511 }
6512
6513
6514 /* Resolve an expression from an iterator. They must be scalar and have
6515 INTEGER or (optionally) REAL type. */
6516
6517 static gfc_try
6518 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6519 const char *name_msgid)
6520 {
6521 if (gfc_resolve_expr (expr) == FAILURE)
6522 return FAILURE;
6523
6524 if (expr->rank != 0)
6525 {
6526 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6527 return FAILURE;
6528 }
6529
6530 if (expr->ts.type != BT_INTEGER)
6531 {
6532 if (expr->ts.type == BT_REAL)
6533 {
6534 if (real_ok)
6535 return gfc_notify_std (GFC_STD_F95_DEL,
6536 "%s at %L must be integer",
6537 _(name_msgid), &expr->where);
6538 else
6539 {
6540 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6541 &expr->where);
6542 return FAILURE;
6543 }
6544 }
6545 else
6546 {
6547 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6548 return FAILURE;
6549 }
6550 }
6551 return SUCCESS;
6552 }
6553
6554
6555 /* Resolve the expressions in an iterator structure. If REAL_OK is
6556 false allow only INTEGER type iterators, otherwise allow REAL types. */
6557
6558 gfc_try
6559 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6560 {
6561 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6562 == FAILURE)
6563 return FAILURE;
6564
6565 if (gfc_check_vardef_context (iter->var, false, false, _("iterator variable"))
6566 == FAILURE)
6567 return FAILURE;
6568
6569 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6570 "Start expression in DO loop") == FAILURE)
6571 return FAILURE;
6572
6573 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6574 "End expression in DO loop") == FAILURE)
6575 return FAILURE;
6576
6577 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6578 "Step expression in DO loop") == FAILURE)
6579 return FAILURE;
6580
6581 if (iter->step->expr_type == EXPR_CONSTANT)
6582 {
6583 if ((iter->step->ts.type == BT_INTEGER
6584 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6585 || (iter->step->ts.type == BT_REAL
6586 && mpfr_sgn (iter->step->value.real) == 0))
6587 {
6588 gfc_error ("Step expression in DO loop at %L cannot be zero",
6589 &iter->step->where);
6590 return FAILURE;
6591 }
6592 }
6593
6594 /* Convert start, end, and step to the same type as var. */
6595 if (iter->start->ts.kind != iter->var->ts.kind
6596 || iter->start->ts.type != iter->var->ts.type)
6597 gfc_convert_type (iter->start, &iter->var->ts, 2);
6598
6599 if (iter->end->ts.kind != iter->var->ts.kind
6600 || iter->end->ts.type != iter->var->ts.type)
6601 gfc_convert_type (iter->end, &iter->var->ts, 2);
6602
6603 if (iter->step->ts.kind != iter->var->ts.kind
6604 || iter->step->ts.type != iter->var->ts.type)
6605 gfc_convert_type (iter->step, &iter->var->ts, 2);
6606
6607 if (iter->start->expr_type == EXPR_CONSTANT
6608 && iter->end->expr_type == EXPR_CONSTANT
6609 && iter->step->expr_type == EXPR_CONSTANT)
6610 {
6611 int sgn, cmp;
6612 if (iter->start->ts.type == BT_INTEGER)
6613 {
6614 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6615 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6616 }
6617 else
6618 {
6619 sgn = mpfr_sgn (iter->step->value.real);
6620 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6621 }
6622 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6623 gfc_warning ("DO loop at %L will be executed zero times",
6624 &iter->step->where);
6625 }
6626
6627 return SUCCESS;
6628 }
6629
6630
6631 /* Traversal function for find_forall_index. f == 2 signals that
6632 that variable itself is not to be checked - only the references. */
6633
6634 static bool
6635 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6636 {
6637 if (expr->expr_type != EXPR_VARIABLE)
6638 return false;
6639
6640 /* A scalar assignment */
6641 if (!expr->ref || *f == 1)
6642 {
6643 if (expr->symtree->n.sym == sym)
6644 return true;
6645 else
6646 return false;
6647 }
6648
6649 if (*f == 2)
6650 *f = 1;
6651 return false;
6652 }
6653
6654
6655 /* Check whether the FORALL index appears in the expression or not.
6656 Returns SUCCESS if SYM is found in EXPR. */
6657
6658 gfc_try
6659 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6660 {
6661 if (gfc_traverse_expr (expr, sym, forall_index, f))
6662 return SUCCESS;
6663 else
6664 return FAILURE;
6665 }
6666
6667
6668 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6669 to be a scalar INTEGER variable. The subscripts and stride are scalar
6670 INTEGERs, and if stride is a constant it must be nonzero.
6671 Furthermore "A subscript or stride in a forall-triplet-spec shall
6672 not contain a reference to any index-name in the
6673 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6674
6675 static void
6676 resolve_forall_iterators (gfc_forall_iterator *it)
6677 {
6678 gfc_forall_iterator *iter, *iter2;
6679
6680 for (iter = it; iter; iter = iter->next)
6681 {
6682 if (gfc_resolve_expr (iter->var) == SUCCESS
6683 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6684 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6685 &iter->var->where);
6686
6687 if (gfc_resolve_expr (iter->start) == SUCCESS
6688 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6689 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6690 &iter->start->where);
6691 if (iter->var->ts.kind != iter->start->ts.kind)
6692 gfc_convert_type (iter->start, &iter->var->ts, 1);
6693
6694 if (gfc_resolve_expr (iter->end) == SUCCESS
6695 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6696 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6697 &iter->end->where);
6698 if (iter->var->ts.kind != iter->end->ts.kind)
6699 gfc_convert_type (iter->end, &iter->var->ts, 1);
6700
6701 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6702 {
6703 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6704 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6705 &iter->stride->where, "INTEGER");
6706
6707 if (iter->stride->expr_type == EXPR_CONSTANT
6708 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6709 gfc_error ("FORALL stride expression at %L cannot be zero",
6710 &iter->stride->where);
6711 }
6712 if (iter->var->ts.kind != iter->stride->ts.kind)
6713 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6714 }
6715
6716 for (iter = it; iter; iter = iter->next)
6717 for (iter2 = iter; iter2; iter2 = iter2->next)
6718 {
6719 if (find_forall_index (iter2->start,
6720 iter->var->symtree->n.sym, 0) == SUCCESS
6721 || find_forall_index (iter2->end,
6722 iter->var->symtree->n.sym, 0) == SUCCESS
6723 || find_forall_index (iter2->stride,
6724 iter->var->symtree->n.sym, 0) == SUCCESS)
6725 gfc_error ("FORALL index '%s' may not appear in triplet "
6726 "specification at %L", iter->var->symtree->name,
6727 &iter2->start->where);
6728 }
6729 }
6730
6731
6732 /* Given a pointer to a symbol that is a derived type, see if it's
6733 inaccessible, i.e. if it's defined in another module and the components are
6734 PRIVATE. The search is recursive if necessary. Returns zero if no
6735 inaccessible components are found, nonzero otherwise. */
6736
6737 static int
6738 derived_inaccessible (gfc_symbol *sym)
6739 {
6740 gfc_component *c;
6741
6742 if (sym->attr.use_assoc && sym->attr.private_comp)
6743 return 1;
6744
6745 for (c = sym->components; c; c = c->next)
6746 {
6747 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6748 return 1;
6749 }
6750
6751 return 0;
6752 }
6753
6754
6755 /* Resolve the argument of a deallocate expression. The expression must be
6756 a pointer or a full array. */
6757
6758 static gfc_try
6759 resolve_deallocate_expr (gfc_expr *e)
6760 {
6761 symbol_attribute attr;
6762 int allocatable, pointer;
6763 gfc_ref *ref;
6764 gfc_symbol *sym;
6765 gfc_component *c;
6766
6767 if (gfc_resolve_expr (e) == FAILURE)
6768 return FAILURE;
6769
6770 if (e->expr_type != EXPR_VARIABLE)
6771 goto bad;
6772
6773 sym = e->symtree->n.sym;
6774
6775 if (sym->ts.type == BT_CLASS)
6776 {
6777 allocatable = CLASS_DATA (sym)->attr.allocatable;
6778 pointer = CLASS_DATA (sym)->attr.class_pointer;
6779 }
6780 else
6781 {
6782 allocatable = sym->attr.allocatable;
6783 pointer = sym->attr.pointer;
6784 }
6785 for (ref = e->ref; ref; ref = ref->next)
6786 {
6787 switch (ref->type)
6788 {
6789 case REF_ARRAY:
6790 if (ref->u.ar.type != AR_FULL
6791 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6792 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6793 allocatable = 0;
6794 break;
6795
6796 case REF_COMPONENT:
6797 c = ref->u.c.component;
6798 if (c->ts.type == BT_CLASS)
6799 {
6800 allocatable = CLASS_DATA (c)->attr.allocatable;
6801 pointer = CLASS_DATA (c)->attr.class_pointer;
6802 }
6803 else
6804 {
6805 allocatable = c->attr.allocatable;
6806 pointer = c->attr.pointer;
6807 }
6808 break;
6809
6810 case REF_SUBSTRING:
6811 allocatable = 0;
6812 break;
6813 }
6814 }
6815
6816 attr = gfc_expr_attr (e);
6817
6818 if (allocatable == 0 && attr.pointer == 0)
6819 {
6820 bad:
6821 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6822 &e->where);
6823 return FAILURE;
6824 }
6825
6826 /* F2008, C644. */
6827 if (gfc_is_coindexed (e))
6828 {
6829 gfc_error ("Coindexed allocatable object at %L", &e->where);
6830 return FAILURE;
6831 }
6832
6833 if (pointer
6834 && gfc_check_vardef_context (e, true, true, _("DEALLOCATE object"))
6835 == FAILURE)
6836 return FAILURE;
6837 if (gfc_check_vardef_context (e, false, true, _("DEALLOCATE object"))
6838 == FAILURE)
6839 return FAILURE;
6840
6841 return SUCCESS;
6842 }
6843
6844
6845 /* Returns true if the expression e contains a reference to the symbol sym. */
6846 static bool
6847 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6848 {
6849 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6850 return true;
6851
6852 return false;
6853 }
6854
6855 bool
6856 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6857 {
6858 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6859 }
6860
6861
6862 /* Given the expression node e for an allocatable/pointer of derived type to be
6863 allocated, get the expression node to be initialized afterwards (needed for
6864 derived types with default initializers, and derived types with allocatable
6865 components that need nullification.) */
6866
6867 gfc_expr *
6868 gfc_expr_to_initialize (gfc_expr *e)
6869 {
6870 gfc_expr *result;
6871 gfc_ref *ref;
6872 int i;
6873
6874 result = gfc_copy_expr (e);
6875
6876 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6877 for (ref = result->ref; ref; ref = ref->next)
6878 if (ref->type == REF_ARRAY && ref->next == NULL)
6879 {
6880 ref->u.ar.type = AR_FULL;
6881
6882 for (i = 0; i < ref->u.ar.dimen; i++)
6883 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6884
6885 break;
6886 }
6887
6888 gfc_free_shape (&result->shape, result->rank);
6889
6890 /* Recalculate rank, shape, etc. */
6891 gfc_resolve_expr (result);
6892 return result;
6893 }
6894
6895
6896 /* If the last ref of an expression is an array ref, return a copy of the
6897 expression with that one removed. Otherwise, a copy of the original
6898 expression. This is used for allocate-expressions and pointer assignment
6899 LHS, where there may be an array specification that needs to be stripped
6900 off when using gfc_check_vardef_context. */
6901
6902 static gfc_expr*
6903 remove_last_array_ref (gfc_expr* e)
6904 {
6905 gfc_expr* e2;
6906 gfc_ref** r;
6907
6908 e2 = gfc_copy_expr (e);
6909 for (r = &e2->ref; *r; r = &(*r)->next)
6910 if ((*r)->type == REF_ARRAY && !(*r)->next)
6911 {
6912 gfc_free_ref_list (*r);
6913 *r = NULL;
6914 break;
6915 }
6916
6917 return e2;
6918 }
6919
6920
6921 /* Used in resolve_allocate_expr to check that a allocation-object and
6922 a source-expr are conformable. This does not catch all possible
6923 cases; in particular a runtime checking is needed. */
6924
6925 static gfc_try
6926 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6927 {
6928 gfc_ref *tail;
6929 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6930
6931 /* First compare rank. */
6932 if (tail && e1->rank != tail->u.ar.as->rank)
6933 {
6934 gfc_error ("Source-expr at %L must be scalar or have the "
6935 "same rank as the allocate-object at %L",
6936 &e1->where, &e2->where);
6937 return FAILURE;
6938 }
6939
6940 if (e1->shape)
6941 {
6942 int i;
6943 mpz_t s;
6944
6945 mpz_init (s);
6946
6947 for (i = 0; i < e1->rank; i++)
6948 {
6949 if (tail->u.ar.end[i])
6950 {
6951 mpz_set (s, tail->u.ar.end[i]->value.integer);
6952 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6953 mpz_add_ui (s, s, 1);
6954 }
6955 else
6956 {
6957 mpz_set (s, tail->u.ar.start[i]->value.integer);
6958 }
6959
6960 if (mpz_cmp (e1->shape[i], s) != 0)
6961 {
6962 gfc_error ("Source-expr at %L and allocate-object at %L must "
6963 "have the same shape", &e1->where, &e2->where);
6964 mpz_clear (s);
6965 return FAILURE;
6966 }
6967 }
6968
6969 mpz_clear (s);
6970 }
6971
6972 return SUCCESS;
6973 }
6974
6975
6976 /* Resolve the expression in an ALLOCATE statement, doing the additional
6977 checks to see whether the expression is OK or not. The expression must
6978 have a trailing array reference that gives the size of the array. */
6979
6980 static gfc_try
6981 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6982 {
6983 int i, pointer, allocatable, dimension, is_abstract;
6984 int codimension;
6985 bool coindexed;
6986 symbol_attribute attr;
6987 gfc_ref *ref, *ref2;
6988 gfc_expr *e2;
6989 gfc_array_ref *ar;
6990 gfc_symbol *sym = NULL;
6991 gfc_alloc *a;
6992 gfc_component *c;
6993 gfc_try t;
6994
6995 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6996 checking of coarrays. */
6997 for (ref = e->ref; ref; ref = ref->next)
6998 if (ref->next == NULL)
6999 break;
7000
7001 if (ref && ref->type == REF_ARRAY)
7002 ref->u.ar.in_allocate = true;
7003
7004 if (gfc_resolve_expr (e) == FAILURE)
7005 goto failure;
7006
7007 /* Make sure the expression is allocatable or a pointer. If it is
7008 pointer, the next-to-last reference must be a pointer. */
7009
7010 ref2 = NULL;
7011 if (e->symtree)
7012 sym = e->symtree->n.sym;
7013
7014 /* Check whether ultimate component is abstract and CLASS. */
7015 is_abstract = 0;
7016
7017 if (e->expr_type != EXPR_VARIABLE)
7018 {
7019 allocatable = 0;
7020 attr = gfc_expr_attr (e);
7021 pointer = attr.pointer;
7022 dimension = attr.dimension;
7023 codimension = attr.codimension;
7024 }
7025 else
7026 {
7027 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7028 {
7029 allocatable = CLASS_DATA (sym)->attr.allocatable;
7030 pointer = CLASS_DATA (sym)->attr.class_pointer;
7031 dimension = CLASS_DATA (sym)->attr.dimension;
7032 codimension = CLASS_DATA (sym)->attr.codimension;
7033 is_abstract = CLASS_DATA (sym)->attr.abstract;
7034 }
7035 else
7036 {
7037 allocatable = sym->attr.allocatable;
7038 pointer = sym->attr.pointer;
7039 dimension = sym->attr.dimension;
7040 codimension = sym->attr.codimension;
7041 }
7042
7043 coindexed = false;
7044
7045 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7046 {
7047 switch (ref->type)
7048 {
7049 case REF_ARRAY:
7050 if (ref->u.ar.codimen > 0)
7051 {
7052 int n;
7053 for (n = ref->u.ar.dimen;
7054 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7055 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7056 {
7057 coindexed = true;
7058 break;
7059 }
7060 }
7061
7062 if (ref->next != NULL)
7063 pointer = 0;
7064 break;
7065
7066 case REF_COMPONENT:
7067 /* F2008, C644. */
7068 if (coindexed)
7069 {
7070 gfc_error ("Coindexed allocatable object at %L",
7071 &e->where);
7072 goto failure;
7073 }
7074
7075 c = ref->u.c.component;
7076 if (c->ts.type == BT_CLASS)
7077 {
7078 allocatable = CLASS_DATA (c)->attr.allocatable;
7079 pointer = CLASS_DATA (c)->attr.class_pointer;
7080 dimension = CLASS_DATA (c)->attr.dimension;
7081 codimension = CLASS_DATA (c)->attr.codimension;
7082 is_abstract = CLASS_DATA (c)->attr.abstract;
7083 }
7084 else
7085 {
7086 allocatable = c->attr.allocatable;
7087 pointer = c->attr.pointer;
7088 dimension = c->attr.dimension;
7089 codimension = c->attr.codimension;
7090 is_abstract = c->attr.abstract;
7091 }
7092 break;
7093
7094 case REF_SUBSTRING:
7095 allocatable = 0;
7096 pointer = 0;
7097 break;
7098 }
7099 }
7100 }
7101
7102 /* Check for F08:C628. */
7103 if (allocatable == 0 && pointer == 0)
7104 {
7105 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7106 &e->where);
7107 goto failure;
7108 }
7109
7110 /* Some checks for the SOURCE tag. */
7111 if (code->expr3)
7112 {
7113 /* Check F03:C631. */
7114 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7115 {
7116 gfc_error ("Type of entity at %L is type incompatible with "
7117 "source-expr at %L", &e->where, &code->expr3->where);
7118 goto failure;
7119 }
7120
7121 /* Check F03:C632 and restriction following Note 6.18. */
7122 if (code->expr3->rank > 0
7123 && conformable_arrays (code->expr3, e) == FAILURE)
7124 goto failure;
7125
7126 /* Check F03:C633. */
7127 if (code->expr3->ts.kind != e->ts.kind)
7128 {
7129 gfc_error ("The allocate-object at %L and the source-expr at %L "
7130 "shall have the same kind type parameter",
7131 &e->where, &code->expr3->where);
7132 goto failure;
7133 }
7134
7135 /* Check F2008, C642. */
7136 if (code->expr3->ts.type == BT_DERIVED
7137 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7138 || (code->expr3->ts.u.derived->from_intmod
7139 == INTMOD_ISO_FORTRAN_ENV
7140 && code->expr3->ts.u.derived->intmod_sym_id
7141 == ISOFORTRAN_LOCK_TYPE)))
7142 {
7143 gfc_error ("The source-expr at %L shall neither be of type "
7144 "LOCK_TYPE nor have a LOCK_TYPE component if "
7145 "allocate-object at %L is a coarray",
7146 &code->expr3->where, &e->where);
7147 goto failure;
7148 }
7149 }
7150
7151 /* Check F08:C629. */
7152 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7153 && !code->expr3)
7154 {
7155 gcc_assert (e->ts.type == BT_CLASS);
7156 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7157 "type-spec or source-expr", sym->name, &e->where);
7158 goto failure;
7159 }
7160
7161 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
7162 {
7163 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7164 code->ext.alloc.ts.u.cl->length);
7165 if (cmp == 1 || cmp == -1 || cmp == -3)
7166 {
7167 gfc_error ("Allocating %s at %L with type-spec requires the same "
7168 "character-length parameter as in the declaration",
7169 sym->name, &e->where);
7170 goto failure;
7171 }
7172 }
7173
7174 /* In the variable definition context checks, gfc_expr_attr is used
7175 on the expression. This is fooled by the array specification
7176 present in e, thus we have to eliminate that one temporarily. */
7177 e2 = remove_last_array_ref (e);
7178 t = SUCCESS;
7179 if (t == SUCCESS && pointer)
7180 t = gfc_check_vardef_context (e2, true, true, _("ALLOCATE object"));
7181 if (t == SUCCESS)
7182 t = gfc_check_vardef_context (e2, false, true, _("ALLOCATE object"));
7183 gfc_free_expr (e2);
7184 if (t == FAILURE)
7185 goto failure;
7186
7187 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7188 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7189 {
7190 /* For class arrays, the initialization with SOURCE is done
7191 using _copy and trans_call. It is convenient to exploit that
7192 when the allocated type is different from the declared type but
7193 no SOURCE exists by setting expr3. */
7194 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7195 }
7196 else if (!code->expr3)
7197 {
7198 /* Set up default initializer if needed. */
7199 gfc_typespec ts;
7200 gfc_expr *init_e;
7201
7202 if (code->ext.alloc.ts.type == BT_DERIVED)
7203 ts = code->ext.alloc.ts;
7204 else
7205 ts = e->ts;
7206
7207 if (ts.type == BT_CLASS)
7208 ts = ts.u.derived->components->ts;
7209
7210 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
7211 {
7212 gfc_code *init_st = gfc_get_code ();
7213 init_st->loc = code->loc;
7214 init_st->op = EXEC_INIT_ASSIGN;
7215 init_st->expr1 = gfc_expr_to_initialize (e);
7216 init_st->expr2 = init_e;
7217 init_st->next = code->next;
7218 code->next = init_st;
7219 }
7220 }
7221 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7222 {
7223 /* Default initialization via MOLD (non-polymorphic). */
7224 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7225 gfc_resolve_expr (rhs);
7226 gfc_free_expr (code->expr3);
7227 code->expr3 = rhs;
7228 }
7229
7230 if (e->ts.type == BT_CLASS)
7231 {
7232 /* Make sure the vtab symbol is present when
7233 the module variables are generated. */
7234 gfc_typespec ts = e->ts;
7235 if (code->expr3)
7236 ts = code->expr3->ts;
7237 else if (code->ext.alloc.ts.type == BT_DERIVED)
7238 ts = code->ext.alloc.ts;
7239 gfc_find_derived_vtab (ts.u.derived);
7240 if (dimension)
7241 e = gfc_expr_to_initialize (e);
7242 }
7243
7244 if (dimension == 0 && codimension == 0)
7245 goto success;
7246
7247 /* Make sure the last reference node is an array specification. */
7248
7249 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7250 || (dimension && ref2->u.ar.dimen == 0))
7251 {
7252 gfc_error ("Array specification required in ALLOCATE statement "
7253 "at %L", &e->where);
7254 goto failure;
7255 }
7256
7257 /* Make sure that the array section reference makes sense in the
7258 context of an ALLOCATE specification. */
7259
7260 ar = &ref2->u.ar;
7261
7262 if (codimension)
7263 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7264 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7265 {
7266 gfc_error ("Coarray specification required in ALLOCATE statement "
7267 "at %L", &e->where);
7268 goto failure;
7269 }
7270
7271 for (i = 0; i < ar->dimen; i++)
7272 {
7273 if (ref2->u.ar.type == AR_ELEMENT)
7274 goto check_symbols;
7275
7276 switch (ar->dimen_type[i])
7277 {
7278 case DIMEN_ELEMENT:
7279 break;
7280
7281 case DIMEN_RANGE:
7282 if (ar->start[i] != NULL
7283 && ar->end[i] != NULL
7284 && ar->stride[i] == NULL)
7285 break;
7286
7287 /* Fall Through... */
7288
7289 case DIMEN_UNKNOWN:
7290 case DIMEN_VECTOR:
7291 case DIMEN_STAR:
7292 case DIMEN_THIS_IMAGE:
7293 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7294 &e->where);
7295 goto failure;
7296 }
7297
7298 check_symbols:
7299 for (a = code->ext.alloc.list; a; a = a->next)
7300 {
7301 sym = a->expr->symtree->n.sym;
7302
7303 /* TODO - check derived type components. */
7304 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7305 continue;
7306
7307 if ((ar->start[i] != NULL
7308 && gfc_find_sym_in_expr (sym, ar->start[i]))
7309 || (ar->end[i] != NULL
7310 && gfc_find_sym_in_expr (sym, ar->end[i])))
7311 {
7312 gfc_error ("'%s' must not appear in the array specification at "
7313 "%L in the same ALLOCATE statement where it is "
7314 "itself allocated", sym->name, &ar->where);
7315 goto failure;
7316 }
7317 }
7318 }
7319
7320 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7321 {
7322 if (ar->dimen_type[i] == DIMEN_ELEMENT
7323 || ar->dimen_type[i] == DIMEN_RANGE)
7324 {
7325 if (i == (ar->dimen + ar->codimen - 1))
7326 {
7327 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7328 "statement at %L", &e->where);
7329 goto failure;
7330 }
7331 break;
7332 }
7333
7334 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7335 && ar->stride[i] == NULL)
7336 break;
7337
7338 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7339 &e->where);
7340 goto failure;
7341 }
7342
7343 success:
7344 return SUCCESS;
7345
7346 failure:
7347 return FAILURE;
7348 }
7349
7350 static void
7351 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7352 {
7353 gfc_expr *stat, *errmsg, *pe, *qe;
7354 gfc_alloc *a, *p, *q;
7355
7356 stat = code->expr1;
7357 errmsg = code->expr2;
7358
7359 /* Check the stat variable. */
7360 if (stat)
7361 {
7362 gfc_check_vardef_context (stat, false, false, _("STAT variable"));
7363
7364 if ((stat->ts.type != BT_INTEGER
7365 && !(stat->ref && (stat->ref->type == REF_ARRAY
7366 || stat->ref->type == REF_COMPONENT)))
7367 || stat->rank > 0)
7368 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7369 "variable", &stat->where);
7370
7371 for (p = code->ext.alloc.list; p; p = p->next)
7372 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7373 {
7374 gfc_ref *ref1, *ref2;
7375 bool found = true;
7376
7377 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7378 ref1 = ref1->next, ref2 = ref2->next)
7379 {
7380 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7381 continue;
7382 if (ref1->u.c.component->name != ref2->u.c.component->name)
7383 {
7384 found = false;
7385 break;
7386 }
7387 }
7388
7389 if (found)
7390 {
7391 gfc_error ("Stat-variable at %L shall not be %sd within "
7392 "the same %s statement", &stat->where, fcn, fcn);
7393 break;
7394 }
7395 }
7396 }
7397
7398 /* Check the errmsg variable. */
7399 if (errmsg)
7400 {
7401 if (!stat)
7402 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7403 &errmsg->where);
7404
7405 gfc_check_vardef_context (errmsg, false, false, _("ERRMSG variable"));
7406
7407 if ((errmsg->ts.type != BT_CHARACTER
7408 && !(errmsg->ref
7409 && (errmsg->ref->type == REF_ARRAY
7410 || errmsg->ref->type == REF_COMPONENT)))
7411 || errmsg->rank > 0 )
7412 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7413 "variable", &errmsg->where);
7414
7415 for (p = code->ext.alloc.list; p; p = p->next)
7416 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7417 {
7418 gfc_ref *ref1, *ref2;
7419 bool found = true;
7420
7421 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7422 ref1 = ref1->next, ref2 = ref2->next)
7423 {
7424 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7425 continue;
7426 if (ref1->u.c.component->name != ref2->u.c.component->name)
7427 {
7428 found = false;
7429 break;
7430 }
7431 }
7432
7433 if (found)
7434 {
7435 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7436 "the same %s statement", &errmsg->where, fcn, fcn);
7437 break;
7438 }
7439 }
7440 }
7441
7442 /* Check that an allocate-object appears only once in the statement. */
7443
7444 for (p = code->ext.alloc.list; p; p = p->next)
7445 {
7446 pe = p->expr;
7447 for (q = p->next; q; q = q->next)
7448 {
7449 qe = q->expr;
7450 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7451 {
7452 /* This is a potential collision. */
7453 gfc_ref *pr = pe->ref;
7454 gfc_ref *qr = qe->ref;
7455
7456 /* Follow the references until
7457 a) They start to differ, in which case there is no error;
7458 you can deallocate a%b and a%c in a single statement
7459 b) Both of them stop, which is an error
7460 c) One of them stops, which is also an error. */
7461 while (1)
7462 {
7463 if (pr == NULL && qr == NULL)
7464 {
7465 gfc_error ("Allocate-object at %L also appears at %L",
7466 &pe->where, &qe->where);
7467 break;
7468 }
7469 else if (pr != NULL && qr == NULL)
7470 {
7471 gfc_error ("Allocate-object at %L is subobject of"
7472 " object at %L", &pe->where, &qe->where);
7473 break;
7474 }
7475 else if (pr == NULL && qr != NULL)
7476 {
7477 gfc_error ("Allocate-object at %L is subobject of"
7478 " object at %L", &qe->where, &pe->where);
7479 break;
7480 }
7481 /* Here, pr != NULL && qr != NULL */
7482 gcc_assert(pr->type == qr->type);
7483 if (pr->type == REF_ARRAY)
7484 {
7485 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7486 which are legal. */
7487 gcc_assert (qr->type == REF_ARRAY);
7488
7489 if (pr->next && qr->next)
7490 {
7491 gfc_array_ref *par = &(pr->u.ar);
7492 gfc_array_ref *qar = &(qr->u.ar);
7493 if ((par->start[0] != NULL || qar->start[0] != NULL)
7494 && gfc_dep_compare_expr (par->start[0],
7495 qar->start[0]) != 0)
7496 break;
7497 }
7498 }
7499 else
7500 {
7501 if (pr->u.c.component->name != qr->u.c.component->name)
7502 break;
7503 }
7504
7505 pr = pr->next;
7506 qr = qr->next;
7507 }
7508 }
7509 }
7510 }
7511
7512 if (strcmp (fcn, "ALLOCATE") == 0)
7513 {
7514 for (a = code->ext.alloc.list; a; a = a->next)
7515 resolve_allocate_expr (a->expr, code);
7516 }
7517 else
7518 {
7519 for (a = code->ext.alloc.list; a; a = a->next)
7520 resolve_deallocate_expr (a->expr);
7521 }
7522 }
7523
7524
7525 /************ SELECT CASE resolution subroutines ************/
7526
7527 /* Callback function for our mergesort variant. Determines interval
7528 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7529 op1 > op2. Assumes we're not dealing with the default case.
7530 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7531 There are nine situations to check. */
7532
7533 static int
7534 compare_cases (const gfc_case *op1, const gfc_case *op2)
7535 {
7536 int retval;
7537
7538 if (op1->low == NULL) /* op1 = (:L) */
7539 {
7540 /* op2 = (:N), so overlap. */
7541 retval = 0;
7542 /* op2 = (M:) or (M:N), L < M */
7543 if (op2->low != NULL
7544 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7545 retval = -1;
7546 }
7547 else if (op1->high == NULL) /* op1 = (K:) */
7548 {
7549 /* op2 = (M:), so overlap. */
7550 retval = 0;
7551 /* op2 = (:N) or (M:N), K > N */
7552 if (op2->high != NULL
7553 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7554 retval = 1;
7555 }
7556 else /* op1 = (K:L) */
7557 {
7558 if (op2->low == NULL) /* op2 = (:N), K > N */
7559 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7560 ? 1 : 0;
7561 else if (op2->high == NULL) /* op2 = (M:), L < M */
7562 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7563 ? -1 : 0;
7564 else /* op2 = (M:N) */
7565 {
7566 retval = 0;
7567 /* L < M */
7568 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7569 retval = -1;
7570 /* K > N */
7571 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7572 retval = 1;
7573 }
7574 }
7575
7576 return retval;
7577 }
7578
7579
7580 /* Merge-sort a double linked case list, detecting overlap in the
7581 process. LIST is the head of the double linked case list before it
7582 is sorted. Returns the head of the sorted list if we don't see any
7583 overlap, or NULL otherwise. */
7584
7585 static gfc_case *
7586 check_case_overlap (gfc_case *list)
7587 {
7588 gfc_case *p, *q, *e, *tail;
7589 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7590
7591 /* If the passed list was empty, return immediately. */
7592 if (!list)
7593 return NULL;
7594
7595 overlap_seen = 0;
7596 insize = 1;
7597
7598 /* Loop unconditionally. The only exit from this loop is a return
7599 statement, when we've finished sorting the case list. */
7600 for (;;)
7601 {
7602 p = list;
7603 list = NULL;
7604 tail = NULL;
7605
7606 /* Count the number of merges we do in this pass. */
7607 nmerges = 0;
7608
7609 /* Loop while there exists a merge to be done. */
7610 while (p)
7611 {
7612 int i;
7613
7614 /* Count this merge. */
7615 nmerges++;
7616
7617 /* Cut the list in two pieces by stepping INSIZE places
7618 forward in the list, starting from P. */
7619 psize = 0;
7620 q = p;
7621 for (i = 0; i < insize; i++)
7622 {
7623 psize++;
7624 q = q->right;
7625 if (!q)
7626 break;
7627 }
7628 qsize = insize;
7629
7630 /* Now we have two lists. Merge them! */
7631 while (psize > 0 || (qsize > 0 && q != NULL))
7632 {
7633 /* See from which the next case to merge comes from. */
7634 if (psize == 0)
7635 {
7636 /* P is empty so the next case must come from Q. */
7637 e = q;
7638 q = q->right;
7639 qsize--;
7640 }
7641 else if (qsize == 0 || q == NULL)
7642 {
7643 /* Q is empty. */
7644 e = p;
7645 p = p->right;
7646 psize--;
7647 }
7648 else
7649 {
7650 cmp = compare_cases (p, q);
7651 if (cmp < 0)
7652 {
7653 /* The whole case range for P is less than the
7654 one for Q. */
7655 e = p;
7656 p = p->right;
7657 psize--;
7658 }
7659 else if (cmp > 0)
7660 {
7661 /* The whole case range for Q is greater than
7662 the case range for P. */
7663 e = q;
7664 q = q->right;
7665 qsize--;
7666 }
7667 else
7668 {
7669 /* The cases overlap, or they are the same
7670 element in the list. Either way, we must
7671 issue an error and get the next case from P. */
7672 /* FIXME: Sort P and Q by line number. */
7673 gfc_error ("CASE label at %L overlaps with CASE "
7674 "label at %L", &p->where, &q->where);
7675 overlap_seen = 1;
7676 e = p;
7677 p = p->right;
7678 psize--;
7679 }
7680 }
7681
7682 /* Add the next element to the merged list. */
7683 if (tail)
7684 tail->right = e;
7685 else
7686 list = e;
7687 e->left = tail;
7688 tail = e;
7689 }
7690
7691 /* P has now stepped INSIZE places along, and so has Q. So
7692 they're the same. */
7693 p = q;
7694 }
7695 tail->right = NULL;
7696
7697 /* If we have done only one merge or none at all, we've
7698 finished sorting the cases. */
7699 if (nmerges <= 1)
7700 {
7701 if (!overlap_seen)
7702 return list;
7703 else
7704 return NULL;
7705 }
7706
7707 /* Otherwise repeat, merging lists twice the size. */
7708 insize *= 2;
7709 }
7710 }
7711
7712
7713 /* Check to see if an expression is suitable for use in a CASE statement.
7714 Makes sure that all case expressions are scalar constants of the same
7715 type. Return FAILURE if anything is wrong. */
7716
7717 static gfc_try
7718 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7719 {
7720 if (e == NULL) return SUCCESS;
7721
7722 if (e->ts.type != case_expr->ts.type)
7723 {
7724 gfc_error ("Expression in CASE statement at %L must be of type %s",
7725 &e->where, gfc_basic_typename (case_expr->ts.type));
7726 return FAILURE;
7727 }
7728
7729 /* C805 (R808) For a given case-construct, each case-value shall be of
7730 the same type as case-expr. For character type, length differences
7731 are allowed, but the kind type parameters shall be the same. */
7732
7733 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7734 {
7735 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7736 &e->where, case_expr->ts.kind);
7737 return FAILURE;
7738 }
7739
7740 /* Convert the case value kind to that of case expression kind,
7741 if needed */
7742
7743 if (e->ts.kind != case_expr->ts.kind)
7744 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7745
7746 if (e->rank != 0)
7747 {
7748 gfc_error ("Expression in CASE statement at %L must be scalar",
7749 &e->where);
7750 return FAILURE;
7751 }
7752
7753 return SUCCESS;
7754 }
7755
7756
7757 /* Given a completely parsed select statement, we:
7758
7759 - Validate all expressions and code within the SELECT.
7760 - Make sure that the selection expression is not of the wrong type.
7761 - Make sure that no case ranges overlap.
7762 - Eliminate unreachable cases and unreachable code resulting from
7763 removing case labels.
7764
7765 The standard does allow unreachable cases, e.g. CASE (5:3). But
7766 they are a hassle for code generation, and to prevent that, we just
7767 cut them out here. This is not necessary for overlapping cases
7768 because they are illegal and we never even try to generate code.
7769
7770 We have the additional caveat that a SELECT construct could have
7771 been a computed GOTO in the source code. Fortunately we can fairly
7772 easily work around that here: The case_expr for a "real" SELECT CASE
7773 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7774 we have to do is make sure that the case_expr is a scalar integer
7775 expression. */
7776
7777 static void
7778 resolve_select (gfc_code *code)
7779 {
7780 gfc_code *body;
7781 gfc_expr *case_expr;
7782 gfc_case *cp, *default_case, *tail, *head;
7783 int seen_unreachable;
7784 int seen_logical;
7785 int ncases;
7786 bt type;
7787 gfc_try t;
7788
7789 if (code->expr1 == NULL)
7790 {
7791 /* This was actually a computed GOTO statement. */
7792 case_expr = code->expr2;
7793 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7794 gfc_error ("Selection expression in computed GOTO statement "
7795 "at %L must be a scalar integer expression",
7796 &case_expr->where);
7797
7798 /* Further checking is not necessary because this SELECT was built
7799 by the compiler, so it should always be OK. Just move the
7800 case_expr from expr2 to expr so that we can handle computed
7801 GOTOs as normal SELECTs from here on. */
7802 code->expr1 = code->expr2;
7803 code->expr2 = NULL;
7804 return;
7805 }
7806
7807 case_expr = code->expr1;
7808
7809 type = case_expr->ts.type;
7810 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7811 {
7812 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7813 &case_expr->where, gfc_typename (&case_expr->ts));
7814
7815 /* Punt. Going on here just produce more garbage error messages. */
7816 return;
7817 }
7818
7819 /* Raise a warning if an INTEGER case value exceeds the range of
7820 the case-expr. Later, all expressions will be promoted to the
7821 largest kind of all case-labels. */
7822
7823 if (type == BT_INTEGER)
7824 for (body = code->block; body; body = body->block)
7825 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7826 {
7827 if (cp->low
7828 && gfc_check_integer_range (cp->low->value.integer,
7829 case_expr->ts.kind) != ARITH_OK)
7830 gfc_warning ("Expression in CASE statement at %L is "
7831 "not in the range of %s", &cp->low->where,
7832 gfc_typename (&case_expr->ts));
7833
7834 if (cp->high
7835 && cp->low != cp->high
7836 && gfc_check_integer_range (cp->high->value.integer,
7837 case_expr->ts.kind) != ARITH_OK)
7838 gfc_warning ("Expression in CASE statement at %L is "
7839 "not in the range of %s", &cp->high->where,
7840 gfc_typename (&case_expr->ts));
7841 }
7842
7843 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7844 of the SELECT CASE expression and its CASE values. Walk the lists
7845 of case values, and if we find a mismatch, promote case_expr to
7846 the appropriate kind. */
7847
7848 if (type == BT_LOGICAL || type == BT_INTEGER)
7849 {
7850 for (body = code->block; body; body = body->block)
7851 {
7852 /* Walk the case label list. */
7853 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7854 {
7855 /* Intercept the DEFAULT case. It does not have a kind. */
7856 if (cp->low == NULL && cp->high == NULL)
7857 continue;
7858
7859 /* Unreachable case ranges are discarded, so ignore. */
7860 if (cp->low != NULL && cp->high != NULL
7861 && cp->low != cp->high
7862 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7863 continue;
7864
7865 if (cp->low != NULL
7866 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7867 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7868
7869 if (cp->high != NULL
7870 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7871 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7872 }
7873 }
7874 }
7875
7876 /* Assume there is no DEFAULT case. */
7877 default_case = NULL;
7878 head = tail = NULL;
7879 ncases = 0;
7880 seen_logical = 0;
7881
7882 for (body = code->block; body; body = body->block)
7883 {
7884 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7885 t = SUCCESS;
7886 seen_unreachable = 0;
7887
7888 /* Walk the case label list, making sure that all case labels
7889 are legal. */
7890 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7891 {
7892 /* Count the number of cases in the whole construct. */
7893 ncases++;
7894
7895 /* Intercept the DEFAULT case. */
7896 if (cp->low == NULL && cp->high == NULL)
7897 {
7898 if (default_case != NULL)
7899 {
7900 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7901 "by a second DEFAULT CASE at %L",
7902 &default_case->where, &cp->where);
7903 t = FAILURE;
7904 break;
7905 }
7906 else
7907 {
7908 default_case = cp;
7909 continue;
7910 }
7911 }
7912
7913 /* Deal with single value cases and case ranges. Errors are
7914 issued from the validation function. */
7915 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7916 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7917 {
7918 t = FAILURE;
7919 break;
7920 }
7921
7922 if (type == BT_LOGICAL
7923 && ((cp->low == NULL || cp->high == NULL)
7924 || cp->low != cp->high))
7925 {
7926 gfc_error ("Logical range in CASE statement at %L is not "
7927 "allowed", &cp->low->where);
7928 t = FAILURE;
7929 break;
7930 }
7931
7932 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7933 {
7934 int value;
7935 value = cp->low->value.logical == 0 ? 2 : 1;
7936 if (value & seen_logical)
7937 {
7938 gfc_error ("Constant logical value in CASE statement "
7939 "is repeated at %L",
7940 &cp->low->where);
7941 t = FAILURE;
7942 break;
7943 }
7944 seen_logical |= value;
7945 }
7946
7947 if (cp->low != NULL && cp->high != NULL
7948 && cp->low != cp->high
7949 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7950 {
7951 if (gfc_option.warn_surprising)
7952 gfc_warning ("Range specification at %L can never "
7953 "be matched", &cp->where);
7954
7955 cp->unreachable = 1;
7956 seen_unreachable = 1;
7957 }
7958 else
7959 {
7960 /* If the case range can be matched, it can also overlap with
7961 other cases. To make sure it does not, we put it in a
7962 double linked list here. We sort that with a merge sort
7963 later on to detect any overlapping cases. */
7964 if (!head)
7965 {
7966 head = tail = cp;
7967 head->right = head->left = NULL;
7968 }
7969 else
7970 {
7971 tail->right = cp;
7972 tail->right->left = tail;
7973 tail = tail->right;
7974 tail->right = NULL;
7975 }
7976 }
7977 }
7978
7979 /* It there was a failure in the previous case label, give up
7980 for this case label list. Continue with the next block. */
7981 if (t == FAILURE)
7982 continue;
7983
7984 /* See if any case labels that are unreachable have been seen.
7985 If so, we eliminate them. This is a bit of a kludge because
7986 the case lists for a single case statement (label) is a
7987 single forward linked lists. */
7988 if (seen_unreachable)
7989 {
7990 /* Advance until the first case in the list is reachable. */
7991 while (body->ext.block.case_list != NULL
7992 && body->ext.block.case_list->unreachable)
7993 {
7994 gfc_case *n = body->ext.block.case_list;
7995 body->ext.block.case_list = body->ext.block.case_list->next;
7996 n->next = NULL;
7997 gfc_free_case_list (n);
7998 }
7999
8000 /* Strip all other unreachable cases. */
8001 if (body->ext.block.case_list)
8002 {
8003 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
8004 {
8005 if (cp->next->unreachable)
8006 {
8007 gfc_case *n = cp->next;
8008 cp->next = cp->next->next;
8009 n->next = NULL;
8010 gfc_free_case_list (n);
8011 }
8012 }
8013 }
8014 }
8015 }
8016
8017 /* See if there were overlapping cases. If the check returns NULL,
8018 there was overlap. In that case we don't do anything. If head
8019 is non-NULL, we prepend the DEFAULT case. The sorted list can
8020 then used during code generation for SELECT CASE constructs with
8021 a case expression of a CHARACTER type. */
8022 if (head)
8023 {
8024 head = check_case_overlap (head);
8025
8026 /* Prepend the default_case if it is there. */
8027 if (head != NULL && default_case)
8028 {
8029 default_case->left = NULL;
8030 default_case->right = head;
8031 head->left = default_case;
8032 }
8033 }
8034
8035 /* Eliminate dead blocks that may be the result if we've seen
8036 unreachable case labels for a block. */
8037 for (body = code; body && body->block; body = body->block)
8038 {
8039 if (body->block->ext.block.case_list == NULL)
8040 {
8041 /* Cut the unreachable block from the code chain. */
8042 gfc_code *c = body->block;
8043 body->block = c->block;
8044
8045 /* Kill the dead block, but not the blocks below it. */
8046 c->block = NULL;
8047 gfc_free_statements (c);
8048 }
8049 }
8050
8051 /* More than two cases is legal but insane for logical selects.
8052 Issue a warning for it. */
8053 if (gfc_option.warn_surprising && type == BT_LOGICAL
8054 && ncases > 2)
8055 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
8056 &code->loc);
8057 }
8058
8059
8060 /* Check if a derived type is extensible. */
8061
8062 bool
8063 gfc_type_is_extensible (gfc_symbol *sym)
8064 {
8065 return !(sym->attr.is_bind_c || sym->attr.sequence);
8066 }
8067
8068
8069 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8070 correct as well as possibly the array-spec. */
8071
8072 static void
8073 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8074 {
8075 gfc_expr* target;
8076
8077 gcc_assert (sym->assoc);
8078 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8079
8080 /* If this is for SELECT TYPE, the target may not yet be set. In that
8081 case, return. Resolution will be called later manually again when
8082 this is done. */
8083 target = sym->assoc->target;
8084 if (!target)
8085 return;
8086 gcc_assert (!sym->assoc->dangling);
8087
8088 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
8089 return;
8090
8091 /* For variable targets, we get some attributes from the target. */
8092 if (target->expr_type == EXPR_VARIABLE)
8093 {
8094 gfc_symbol* tsym;
8095
8096 gcc_assert (target->symtree);
8097 tsym = target->symtree->n.sym;
8098
8099 sym->attr.asynchronous = tsym->attr.asynchronous;
8100 sym->attr.volatile_ = tsym->attr.volatile_;
8101
8102 sym->attr.target = tsym->attr.target
8103 || gfc_expr_attr (target).pointer;
8104 }
8105
8106 /* Get type if this was not already set. Note that it can be
8107 some other type than the target in case this is a SELECT TYPE
8108 selector! So we must not update when the type is already there. */
8109 if (sym->ts.type == BT_UNKNOWN)
8110 sym->ts = target->ts;
8111 gcc_assert (sym->ts.type != BT_UNKNOWN);
8112
8113 /* See if this is a valid association-to-variable. */
8114 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8115 && !gfc_has_vector_subscript (target));
8116
8117 /* Finally resolve if this is an array or not. */
8118 if (sym->attr.dimension && target->rank == 0)
8119 {
8120 gfc_error ("Associate-name '%s' at %L is used as array",
8121 sym->name, &sym->declared_at);
8122 sym->attr.dimension = 0;
8123 return;
8124 }
8125
8126 /* We cannot deal with class selectors that need temporaries. */
8127 if (target->ts.type == BT_CLASS
8128 && gfc_ref_needs_temporary_p (target->ref))
8129 {
8130 gfc_error ("CLASS selector at %L needs a temporary which is not "
8131 "yet implemented", &target->where);
8132 return;
8133 }
8134
8135 if (target->ts.type != BT_CLASS && target->rank > 0)
8136 sym->attr.dimension = 1;
8137 else if (target->ts.type == BT_CLASS)
8138 gfc_fix_class_refs (target);
8139
8140 /* The associate-name will have a correct type by now. Make absolutely
8141 sure that it has not picked up a dimension attribute. */
8142 if (sym->ts.type == BT_CLASS)
8143 sym->attr.dimension = 0;
8144
8145 if (sym->attr.dimension)
8146 {
8147 sym->as = gfc_get_array_spec ();
8148 sym->as->rank = target->rank;
8149 sym->as->type = AS_DEFERRED;
8150
8151 /* Target must not be coindexed, thus the associate-variable
8152 has no corank. */
8153 sym->as->corank = 0;
8154 }
8155 }
8156
8157
8158 /* Resolve a SELECT TYPE statement. */
8159
8160 static void
8161 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8162 {
8163 gfc_symbol *selector_type;
8164 gfc_code *body, *new_st, *if_st, *tail;
8165 gfc_code *class_is = NULL, *default_case = NULL;
8166 gfc_case *c;
8167 gfc_symtree *st;
8168 char name[GFC_MAX_SYMBOL_LEN];
8169 gfc_namespace *ns;
8170 int error = 0;
8171
8172 ns = code->ext.block.ns;
8173 gfc_resolve (ns);
8174
8175 /* Check for F03:C813. */
8176 if (code->expr1->ts.type != BT_CLASS
8177 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8178 {
8179 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8180 "at %L", &code->loc);
8181 return;
8182 }
8183
8184 if (!code->expr1->symtree->n.sym->attr.class_ok)
8185 return;
8186
8187 if (code->expr2)
8188 {
8189 if (code->expr1->symtree->n.sym->attr.untyped)
8190 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8191 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8192 }
8193 else
8194 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8195
8196 /* Loop over TYPE IS / CLASS IS cases. */
8197 for (body = code->block; body; body = body->block)
8198 {
8199 c = body->ext.block.case_list;
8200
8201 /* Check F03:C815. */
8202 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8203 && !gfc_type_is_extensible (c->ts.u.derived))
8204 {
8205 gfc_error ("Derived type '%s' at %L must be extensible",
8206 c->ts.u.derived->name, &c->where);
8207 error++;
8208 continue;
8209 }
8210
8211 /* Check F03:C816. */
8212 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8213 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
8214 {
8215 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
8216 c->ts.u.derived->name, &c->where, selector_type->name);
8217 error++;
8218 continue;
8219 }
8220
8221 /* Intercept the DEFAULT case. */
8222 if (c->ts.type == BT_UNKNOWN)
8223 {
8224 /* Check F03:C818. */
8225 if (default_case)
8226 {
8227 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8228 "by a second DEFAULT CASE at %L",
8229 &default_case->ext.block.case_list->where, &c->where);
8230 error++;
8231 continue;
8232 }
8233
8234 default_case = body;
8235 }
8236 }
8237
8238 if (error > 0)
8239 return;
8240
8241 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8242 target if present. If there are any EXIT statements referring to the
8243 SELECT TYPE construct, this is no problem because the gfc_code
8244 reference stays the same and EXIT is equally possible from the BLOCK
8245 it is changed to. */
8246 code->op = EXEC_BLOCK;
8247 if (code->expr2)
8248 {
8249 gfc_association_list* assoc;
8250
8251 assoc = gfc_get_association_list ();
8252 assoc->st = code->expr1->symtree;
8253 assoc->target = gfc_copy_expr (code->expr2);
8254 assoc->target->where = code->expr2->where;
8255 /* assoc->variable will be set by resolve_assoc_var. */
8256
8257 code->ext.block.assoc = assoc;
8258 code->expr1->symtree->n.sym->assoc = assoc;
8259
8260 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8261 }
8262 else
8263 code->ext.block.assoc = NULL;
8264
8265 /* Add EXEC_SELECT to switch on type. */
8266 new_st = gfc_get_code ();
8267 new_st->op = code->op;
8268 new_st->expr1 = code->expr1;
8269 new_st->expr2 = code->expr2;
8270 new_st->block = code->block;
8271 code->expr1 = code->expr2 = NULL;
8272 code->block = NULL;
8273 if (!ns->code)
8274 ns->code = new_st;
8275 else
8276 ns->code->next = new_st;
8277 code = new_st;
8278 code->op = EXEC_SELECT;
8279 gfc_add_vptr_component (code->expr1);
8280 gfc_add_hash_component (code->expr1);
8281
8282 /* Loop over TYPE IS / CLASS IS cases. */
8283 for (body = code->block; body; body = body->block)
8284 {
8285 c = body->ext.block.case_list;
8286
8287 if (c->ts.type == BT_DERIVED)
8288 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8289 c->ts.u.derived->hash_value);
8290
8291 else if (c->ts.type == BT_UNKNOWN)
8292 continue;
8293
8294 /* Associate temporary to selector. This should only be done
8295 when this case is actually true, so build a new ASSOCIATE
8296 that does precisely this here (instead of using the
8297 'global' one). */
8298
8299 if (c->ts.type == BT_CLASS)
8300 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8301 else
8302 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8303 st = gfc_find_symtree (ns->sym_root, name);
8304 gcc_assert (st->n.sym->assoc);
8305 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8306 st->n.sym->assoc->target->where = code->expr1->where;
8307 if (c->ts.type == BT_DERIVED)
8308 gfc_add_data_component (st->n.sym->assoc->target);
8309
8310 new_st = gfc_get_code ();
8311 new_st->op = EXEC_BLOCK;
8312 new_st->ext.block.ns = gfc_build_block_ns (ns);
8313 new_st->ext.block.ns->code = body->next;
8314 body->next = new_st;
8315
8316 /* Chain in the new list only if it is marked as dangling. Otherwise
8317 there is a CASE label overlap and this is already used. Just ignore,
8318 the error is diagnosed elsewhere. */
8319 if (st->n.sym->assoc->dangling)
8320 {
8321 new_st->ext.block.assoc = st->n.sym->assoc;
8322 st->n.sym->assoc->dangling = 0;
8323 }
8324
8325 resolve_assoc_var (st->n.sym, false);
8326 }
8327
8328 /* Take out CLASS IS cases for separate treatment. */
8329 body = code;
8330 while (body && body->block)
8331 {
8332 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8333 {
8334 /* Add to class_is list. */
8335 if (class_is == NULL)
8336 {
8337 class_is = body->block;
8338 tail = class_is;
8339 }
8340 else
8341 {
8342 for (tail = class_is; tail->block; tail = tail->block) ;
8343 tail->block = body->block;
8344 tail = tail->block;
8345 }
8346 /* Remove from EXEC_SELECT list. */
8347 body->block = body->block->block;
8348 tail->block = NULL;
8349 }
8350 else
8351 body = body->block;
8352 }
8353
8354 if (class_is)
8355 {
8356 gfc_symbol *vtab;
8357
8358 if (!default_case)
8359 {
8360 /* Add a default case to hold the CLASS IS cases. */
8361 for (tail = code; tail->block; tail = tail->block) ;
8362 tail->block = gfc_get_code ();
8363 tail = tail->block;
8364 tail->op = EXEC_SELECT_TYPE;
8365 tail->ext.block.case_list = gfc_get_case ();
8366 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8367 tail->next = NULL;
8368 default_case = tail;
8369 }
8370
8371 /* More than one CLASS IS block? */
8372 if (class_is->block)
8373 {
8374 gfc_code **c1,*c2;
8375 bool swapped;
8376 /* Sort CLASS IS blocks by extension level. */
8377 do
8378 {
8379 swapped = false;
8380 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8381 {
8382 c2 = (*c1)->block;
8383 /* F03:C817 (check for doubles). */
8384 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8385 == c2->ext.block.case_list->ts.u.derived->hash_value)
8386 {
8387 gfc_error ("Double CLASS IS block in SELECT TYPE "
8388 "statement at %L",
8389 &c2->ext.block.case_list->where);
8390 return;
8391 }
8392 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8393 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8394 {
8395 /* Swap. */
8396 (*c1)->block = c2->block;
8397 c2->block = *c1;
8398 *c1 = c2;
8399 swapped = true;
8400 }
8401 }
8402 }
8403 while (swapped);
8404 }
8405
8406 /* Generate IF chain. */
8407 if_st = gfc_get_code ();
8408 if_st->op = EXEC_IF;
8409 new_st = if_st;
8410 for (body = class_is; body; body = body->block)
8411 {
8412 new_st->block = gfc_get_code ();
8413 new_st = new_st->block;
8414 new_st->op = EXEC_IF;
8415 /* Set up IF condition: Call _gfortran_is_extension_of. */
8416 new_st->expr1 = gfc_get_expr ();
8417 new_st->expr1->expr_type = EXPR_FUNCTION;
8418 new_st->expr1->ts.type = BT_LOGICAL;
8419 new_st->expr1->ts.kind = 4;
8420 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8421 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8422 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8423 /* Set up arguments. */
8424 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8425 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8426 new_st->expr1->value.function.actual->expr->where = code->loc;
8427 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8428 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8429 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8430 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8431 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8432 new_st->next = body->next;
8433 }
8434 if (default_case->next)
8435 {
8436 new_st->block = gfc_get_code ();
8437 new_st = new_st->block;
8438 new_st->op = EXEC_IF;
8439 new_st->next = default_case->next;
8440 }
8441
8442 /* Replace CLASS DEFAULT code by the IF chain. */
8443 default_case->next = if_st;
8444 }
8445
8446 /* Resolve the internal code. This can not be done earlier because
8447 it requires that the sym->assoc of selectors is set already. */
8448 gfc_current_ns = ns;
8449 gfc_resolve_blocks (code->block, gfc_current_ns);
8450 gfc_current_ns = old_ns;
8451
8452 resolve_select (code);
8453 }
8454
8455
8456 /* Resolve a transfer statement. This is making sure that:
8457 -- a derived type being transferred has only non-pointer components
8458 -- a derived type being transferred doesn't have private components, unless
8459 it's being transferred from the module where the type was defined
8460 -- we're not trying to transfer a whole assumed size array. */
8461
8462 static void
8463 resolve_transfer (gfc_code *code)
8464 {
8465 gfc_typespec *ts;
8466 gfc_symbol *sym;
8467 gfc_ref *ref;
8468 gfc_expr *exp;
8469
8470 exp = code->expr1;
8471
8472 while (exp != NULL && exp->expr_type == EXPR_OP
8473 && exp->value.op.op == INTRINSIC_PARENTHESES)
8474 exp = exp->value.op.op1;
8475
8476 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8477 {
8478 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8479 "MOLD=", &exp->where);
8480 return;
8481 }
8482
8483 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8484 && exp->expr_type != EXPR_FUNCTION))
8485 return;
8486
8487 /* If we are reading, the variable will be changed. Note that
8488 code->ext.dt may be NULL if the TRANSFER is related to
8489 an INQUIRE statement -- but in this case, we are not reading, either. */
8490 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8491 && gfc_check_vardef_context (exp, false, false, _("item in READ"))
8492 == FAILURE)
8493 return;
8494
8495 sym = exp->symtree->n.sym;
8496 ts = &sym->ts;
8497
8498 /* Go to actual component transferred. */
8499 for (ref = exp->ref; ref; ref = ref->next)
8500 if (ref->type == REF_COMPONENT)
8501 ts = &ref->u.c.component->ts;
8502
8503 if (ts->type == BT_CLASS)
8504 {
8505 /* FIXME: Test for defined input/output. */
8506 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8507 "it is processed by a defined input/output procedure",
8508 &code->loc);
8509 return;
8510 }
8511
8512 if (ts->type == BT_DERIVED)
8513 {
8514 /* Check that transferred derived type doesn't contain POINTER
8515 components. */
8516 if (ts->u.derived->attr.pointer_comp)
8517 {
8518 gfc_error ("Data transfer element at %L cannot have POINTER "
8519 "components unless it is processed by a defined "
8520 "input/output procedure", &code->loc);
8521 return;
8522 }
8523
8524 /* F08:C935. */
8525 if (ts->u.derived->attr.proc_pointer_comp)
8526 {
8527 gfc_error ("Data transfer element at %L cannot have "
8528 "procedure pointer components", &code->loc);
8529 return;
8530 }
8531
8532 if (ts->u.derived->attr.alloc_comp)
8533 {
8534 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8535 "components unless it is processed by a defined "
8536 "input/output procedure", &code->loc);
8537 return;
8538 }
8539
8540 if (derived_inaccessible (ts->u.derived))
8541 {
8542 gfc_error ("Data transfer element at %L cannot have "
8543 "PRIVATE components",&code->loc);
8544 return;
8545 }
8546 }
8547
8548 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8549 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8550 {
8551 gfc_error ("Data transfer element at %L cannot be a full reference to "
8552 "an assumed-size array", &code->loc);
8553 return;
8554 }
8555 }
8556
8557
8558 /*********** Toplevel code resolution subroutines ***********/
8559
8560 /* Find the set of labels that are reachable from this block. We also
8561 record the last statement in each block. */
8562
8563 static void
8564 find_reachable_labels (gfc_code *block)
8565 {
8566 gfc_code *c;
8567
8568 if (!block)
8569 return;
8570
8571 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8572
8573 /* Collect labels in this block. We don't keep those corresponding
8574 to END {IF|SELECT}, these are checked in resolve_branch by going
8575 up through the code_stack. */
8576 for (c = block; c; c = c->next)
8577 {
8578 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8579 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8580 }
8581
8582 /* Merge with labels from parent block. */
8583 if (cs_base->prev)
8584 {
8585 gcc_assert (cs_base->prev->reachable_labels);
8586 bitmap_ior_into (cs_base->reachable_labels,
8587 cs_base->prev->reachable_labels);
8588 }
8589 }
8590
8591
8592 static void
8593 resolve_lock_unlock (gfc_code *code)
8594 {
8595 if (code->expr1->ts.type != BT_DERIVED
8596 || code->expr1->expr_type != EXPR_VARIABLE
8597 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8598 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8599 || code->expr1->rank != 0
8600 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8601 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8602 &code->expr1->where);
8603
8604 /* Check STAT. */
8605 if (code->expr2
8606 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8607 || code->expr2->expr_type != EXPR_VARIABLE))
8608 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8609 &code->expr2->where);
8610
8611 if (code->expr2
8612 && gfc_check_vardef_context (code->expr2, false, false,
8613 _("STAT variable")) == FAILURE)
8614 return;
8615
8616 /* Check ERRMSG. */
8617 if (code->expr3
8618 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8619 || code->expr3->expr_type != EXPR_VARIABLE))
8620 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8621 &code->expr3->where);
8622
8623 if (code->expr3
8624 && gfc_check_vardef_context (code->expr3, false, false,
8625 _("ERRMSG variable")) == FAILURE)
8626 return;
8627
8628 /* Check ACQUIRED_LOCK. */
8629 if (code->expr4
8630 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8631 || code->expr4->expr_type != EXPR_VARIABLE))
8632 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8633 "variable", &code->expr4->where);
8634
8635 if (code->expr4
8636 && gfc_check_vardef_context (code->expr4, false, false,
8637 _("ACQUIRED_LOCK variable")) == FAILURE)
8638 return;
8639 }
8640
8641
8642 static void
8643 resolve_sync (gfc_code *code)
8644 {
8645 /* Check imageset. The * case matches expr1 == NULL. */
8646 if (code->expr1)
8647 {
8648 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8649 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8650 "INTEGER expression", &code->expr1->where);
8651 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8652 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8653 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8654 &code->expr1->where);
8655 else if (code->expr1->expr_type == EXPR_ARRAY
8656 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8657 {
8658 gfc_constructor *cons;
8659 cons = gfc_constructor_first (code->expr1->value.constructor);
8660 for (; cons; cons = gfc_constructor_next (cons))
8661 if (cons->expr->expr_type == EXPR_CONSTANT
8662 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8663 gfc_error ("Imageset argument at %L must between 1 and "
8664 "num_images()", &cons->expr->where);
8665 }
8666 }
8667
8668 /* Check STAT. */
8669 if (code->expr2
8670 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8671 || code->expr2->expr_type != EXPR_VARIABLE))
8672 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8673 &code->expr2->where);
8674
8675 /* Check ERRMSG. */
8676 if (code->expr3
8677 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8678 || code->expr3->expr_type != EXPR_VARIABLE))
8679 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8680 &code->expr3->where);
8681 }
8682
8683
8684 /* Given a branch to a label, see if the branch is conforming.
8685 The code node describes where the branch is located. */
8686
8687 static void
8688 resolve_branch (gfc_st_label *label, gfc_code *code)
8689 {
8690 code_stack *stack;
8691
8692 if (label == NULL)
8693 return;
8694
8695 /* Step one: is this a valid branching target? */
8696
8697 if (label->defined == ST_LABEL_UNKNOWN)
8698 {
8699 gfc_error ("Label %d referenced at %L is never defined", label->value,
8700 &label->where);
8701 return;
8702 }
8703
8704 if (label->defined != ST_LABEL_TARGET)
8705 {
8706 gfc_error ("Statement at %L is not a valid branch target statement "
8707 "for the branch statement at %L", &label->where, &code->loc);
8708 return;
8709 }
8710
8711 /* Step two: make sure this branch is not a branch to itself ;-) */
8712
8713 if (code->here == label)
8714 {
8715 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8716 return;
8717 }
8718
8719 /* Step three: See if the label is in the same block as the
8720 branching statement. The hard work has been done by setting up
8721 the bitmap reachable_labels. */
8722
8723 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8724 {
8725 /* Check now whether there is a CRITICAL construct; if so, check
8726 whether the label is still visible outside of the CRITICAL block,
8727 which is invalid. */
8728 for (stack = cs_base; stack; stack = stack->prev)
8729 {
8730 if (stack->current->op == EXEC_CRITICAL
8731 && bitmap_bit_p (stack->reachable_labels, label->value))
8732 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8733 "label at %L", &code->loc, &label->where);
8734 else if (stack->current->op == EXEC_DO_CONCURRENT
8735 && bitmap_bit_p (stack->reachable_labels, label->value))
8736 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8737 "for label at %L", &code->loc, &label->where);
8738 }
8739
8740 return;
8741 }
8742
8743 /* Step four: If we haven't found the label in the bitmap, it may
8744 still be the label of the END of the enclosing block, in which
8745 case we find it by going up the code_stack. */
8746
8747 for (stack = cs_base; stack; stack = stack->prev)
8748 {
8749 if (stack->current->next && stack->current->next->here == label)
8750 break;
8751 if (stack->current->op == EXEC_CRITICAL)
8752 {
8753 /* Note: A label at END CRITICAL does not leave the CRITICAL
8754 construct as END CRITICAL is still part of it. */
8755 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8756 " at %L", &code->loc, &label->where);
8757 return;
8758 }
8759 else if (stack->current->op == EXEC_DO_CONCURRENT)
8760 {
8761 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8762 "label at %L", &code->loc, &label->where);
8763 return;
8764 }
8765 }
8766
8767 if (stack)
8768 {
8769 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8770 return;
8771 }
8772
8773 /* The label is not in an enclosing block, so illegal. This was
8774 allowed in Fortran 66, so we allow it as extension. No
8775 further checks are necessary in this case. */
8776 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8777 "as the GOTO statement at %L", &label->where,
8778 &code->loc);
8779 return;
8780 }
8781
8782
8783 /* Check whether EXPR1 has the same shape as EXPR2. */
8784
8785 static gfc_try
8786 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8787 {
8788 mpz_t shape[GFC_MAX_DIMENSIONS];
8789 mpz_t shape2[GFC_MAX_DIMENSIONS];
8790 gfc_try result = FAILURE;
8791 int i;
8792
8793 /* Compare the rank. */
8794 if (expr1->rank != expr2->rank)
8795 return result;
8796
8797 /* Compare the size of each dimension. */
8798 for (i=0; i<expr1->rank; i++)
8799 {
8800 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8801 goto ignore;
8802
8803 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8804 goto ignore;
8805
8806 if (mpz_cmp (shape[i], shape2[i]))
8807 goto over;
8808 }
8809
8810 /* When either of the two expression is an assumed size array, we
8811 ignore the comparison of dimension sizes. */
8812 ignore:
8813 result = SUCCESS;
8814
8815 over:
8816 gfc_clear_shape (shape, i);
8817 gfc_clear_shape (shape2, i);
8818 return result;
8819 }
8820
8821
8822 /* Check whether a WHERE assignment target or a WHERE mask expression
8823 has the same shape as the outmost WHERE mask expression. */
8824
8825 static void
8826 resolve_where (gfc_code *code, gfc_expr *mask)
8827 {
8828 gfc_code *cblock;
8829 gfc_code *cnext;
8830 gfc_expr *e = NULL;
8831
8832 cblock = code->block;
8833
8834 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8835 In case of nested WHERE, only the outmost one is stored. */
8836 if (mask == NULL) /* outmost WHERE */
8837 e = cblock->expr1;
8838 else /* inner WHERE */
8839 e = mask;
8840
8841 while (cblock)
8842 {
8843 if (cblock->expr1)
8844 {
8845 /* Check if the mask-expr has a consistent shape with the
8846 outmost WHERE mask-expr. */
8847 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8848 gfc_error ("WHERE mask at %L has inconsistent shape",
8849 &cblock->expr1->where);
8850 }
8851
8852 /* the assignment statement of a WHERE statement, or the first
8853 statement in where-body-construct of a WHERE construct */
8854 cnext = cblock->next;
8855 while (cnext)
8856 {
8857 switch (cnext->op)
8858 {
8859 /* WHERE assignment statement */
8860 case EXEC_ASSIGN:
8861
8862 /* Check shape consistent for WHERE assignment target. */
8863 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8864 gfc_error ("WHERE assignment target at %L has "
8865 "inconsistent shape", &cnext->expr1->where);
8866 break;
8867
8868
8869 case EXEC_ASSIGN_CALL:
8870 resolve_call (cnext);
8871 if (!cnext->resolved_sym->attr.elemental)
8872 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8873 &cnext->ext.actual->expr->where);
8874 break;
8875
8876 /* WHERE or WHERE construct is part of a where-body-construct */
8877 case EXEC_WHERE:
8878 resolve_where (cnext, e);
8879 break;
8880
8881 default:
8882 gfc_error ("Unsupported statement inside WHERE at %L",
8883 &cnext->loc);
8884 }
8885 /* the next statement within the same where-body-construct */
8886 cnext = cnext->next;
8887 }
8888 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8889 cblock = cblock->block;
8890 }
8891 }
8892
8893
8894 /* Resolve assignment in FORALL construct.
8895 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8896 FORALL index variables. */
8897
8898 static void
8899 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8900 {
8901 int n;
8902
8903 for (n = 0; n < nvar; n++)
8904 {
8905 gfc_symbol *forall_index;
8906
8907 forall_index = var_expr[n]->symtree->n.sym;
8908
8909 /* Check whether the assignment target is one of the FORALL index
8910 variable. */
8911 if ((code->expr1->expr_type == EXPR_VARIABLE)
8912 && (code->expr1->symtree->n.sym == forall_index))
8913 gfc_error ("Assignment to a FORALL index variable at %L",
8914 &code->expr1->where);
8915 else
8916 {
8917 /* If one of the FORALL index variables doesn't appear in the
8918 assignment variable, then there could be a many-to-one
8919 assignment. Emit a warning rather than an error because the
8920 mask could be resolving this problem. */
8921 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8922 gfc_warning ("The FORALL with index '%s' is not used on the "
8923 "left side of the assignment at %L and so might "
8924 "cause multiple assignment to this object",
8925 var_expr[n]->symtree->name, &code->expr1->where);
8926 }
8927 }
8928 }
8929
8930
8931 /* Resolve WHERE statement in FORALL construct. */
8932
8933 static void
8934 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8935 gfc_expr **var_expr)
8936 {
8937 gfc_code *cblock;
8938 gfc_code *cnext;
8939
8940 cblock = code->block;
8941 while (cblock)
8942 {
8943 /* the assignment statement of a WHERE statement, or the first
8944 statement in where-body-construct of a WHERE construct */
8945 cnext = cblock->next;
8946 while (cnext)
8947 {
8948 switch (cnext->op)
8949 {
8950 /* WHERE assignment statement */
8951 case EXEC_ASSIGN:
8952 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8953 break;
8954
8955 /* WHERE operator assignment statement */
8956 case EXEC_ASSIGN_CALL:
8957 resolve_call (cnext);
8958 if (!cnext->resolved_sym->attr.elemental)
8959 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8960 &cnext->ext.actual->expr->where);
8961 break;
8962
8963 /* WHERE or WHERE construct is part of a where-body-construct */
8964 case EXEC_WHERE:
8965 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8966 break;
8967
8968 default:
8969 gfc_error ("Unsupported statement inside WHERE at %L",
8970 &cnext->loc);
8971 }
8972 /* the next statement within the same where-body-construct */
8973 cnext = cnext->next;
8974 }
8975 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8976 cblock = cblock->block;
8977 }
8978 }
8979
8980
8981 /* Traverse the FORALL body to check whether the following errors exist:
8982 1. For assignment, check if a many-to-one assignment happens.
8983 2. For WHERE statement, check the WHERE body to see if there is any
8984 many-to-one assignment. */
8985
8986 static void
8987 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8988 {
8989 gfc_code *c;
8990
8991 c = code->block->next;
8992 while (c)
8993 {
8994 switch (c->op)
8995 {
8996 case EXEC_ASSIGN:
8997 case EXEC_POINTER_ASSIGN:
8998 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8999 break;
9000
9001 case EXEC_ASSIGN_CALL:
9002 resolve_call (c);
9003 break;
9004
9005 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9006 there is no need to handle it here. */
9007 case EXEC_FORALL:
9008 break;
9009 case EXEC_WHERE:
9010 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9011 break;
9012 default:
9013 break;
9014 }
9015 /* The next statement in the FORALL body. */
9016 c = c->next;
9017 }
9018 }
9019
9020
9021 /* Counts the number of iterators needed inside a forall construct, including
9022 nested forall constructs. This is used to allocate the needed memory
9023 in gfc_resolve_forall. */
9024
9025 static int
9026 gfc_count_forall_iterators (gfc_code *code)
9027 {
9028 int max_iters, sub_iters, current_iters;
9029 gfc_forall_iterator *fa;
9030
9031 gcc_assert(code->op == EXEC_FORALL);
9032 max_iters = 0;
9033 current_iters = 0;
9034
9035 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9036 current_iters ++;
9037
9038 code = code->block->next;
9039
9040 while (code)
9041 {
9042 if (code->op == EXEC_FORALL)
9043 {
9044 sub_iters = gfc_count_forall_iterators (code);
9045 if (sub_iters > max_iters)
9046 max_iters = sub_iters;
9047 }
9048 code = code->next;
9049 }
9050
9051 return current_iters + max_iters;
9052 }
9053
9054
9055 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9056 gfc_resolve_forall_body to resolve the FORALL body. */
9057
9058 static void
9059 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9060 {
9061 static gfc_expr **var_expr;
9062 static int total_var = 0;
9063 static int nvar = 0;
9064 int old_nvar, tmp;
9065 gfc_forall_iterator *fa;
9066 int i;
9067
9068 old_nvar = nvar;
9069
9070 /* Start to resolve a FORALL construct */
9071 if (forall_save == 0)
9072 {
9073 /* Count the total number of FORALL index in the nested FORALL
9074 construct in order to allocate the VAR_EXPR with proper size. */
9075 total_var = gfc_count_forall_iterators (code);
9076
9077 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9078 var_expr = XCNEWVEC (gfc_expr *, total_var);
9079 }
9080
9081 /* The information about FORALL iterator, including FORALL index start, end
9082 and stride. The FORALL index can not appear in start, end or stride. */
9083 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9084 {
9085 /* Check if any outer FORALL index name is the same as the current
9086 one. */
9087 for (i = 0; i < nvar; i++)
9088 {
9089 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9090 {
9091 gfc_error ("An outer FORALL construct already has an index "
9092 "with this name %L", &fa->var->where);
9093 }
9094 }
9095
9096 /* Record the current FORALL index. */
9097 var_expr[nvar] = gfc_copy_expr (fa->var);
9098
9099 nvar++;
9100
9101 /* No memory leak. */
9102 gcc_assert (nvar <= total_var);
9103 }
9104
9105 /* Resolve the FORALL body. */
9106 gfc_resolve_forall_body (code, nvar, var_expr);
9107
9108 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9109 gfc_resolve_blocks (code->block, ns);
9110
9111 tmp = nvar;
9112 nvar = old_nvar;
9113 /* Free only the VAR_EXPRs allocated in this frame. */
9114 for (i = nvar; i < tmp; i++)
9115 gfc_free_expr (var_expr[i]);
9116
9117 if (nvar == 0)
9118 {
9119 /* We are in the outermost FORALL construct. */
9120 gcc_assert (forall_save == 0);
9121
9122 /* VAR_EXPR is not needed any more. */
9123 free (var_expr);
9124 total_var = 0;
9125 }
9126 }
9127
9128
9129 /* Resolve a BLOCK construct statement. */
9130
9131 static void
9132 resolve_block_construct (gfc_code* code)
9133 {
9134 /* Resolve the BLOCK's namespace. */
9135 gfc_resolve (code->ext.block.ns);
9136
9137 /* For an ASSOCIATE block, the associations (and their targets) are already
9138 resolved during resolve_symbol. */
9139 }
9140
9141
9142 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9143 DO code nodes. */
9144
9145 static void resolve_code (gfc_code *, gfc_namespace *);
9146
9147 void
9148 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9149 {
9150 gfc_try t;
9151
9152 for (; b; b = b->block)
9153 {
9154 t = gfc_resolve_expr (b->expr1);
9155 if (gfc_resolve_expr (b->expr2) == FAILURE)
9156 t = FAILURE;
9157
9158 switch (b->op)
9159 {
9160 case EXEC_IF:
9161 if (t == SUCCESS && b->expr1 != NULL
9162 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9163 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9164 &b->expr1->where);
9165 break;
9166
9167 case EXEC_WHERE:
9168 if (t == SUCCESS
9169 && b->expr1 != NULL
9170 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9171 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9172 &b->expr1->where);
9173 break;
9174
9175 case EXEC_GOTO:
9176 resolve_branch (b->label1, b);
9177 break;
9178
9179 case EXEC_BLOCK:
9180 resolve_block_construct (b);
9181 break;
9182
9183 case EXEC_SELECT:
9184 case EXEC_SELECT_TYPE:
9185 case EXEC_FORALL:
9186 case EXEC_DO:
9187 case EXEC_DO_WHILE:
9188 case EXEC_DO_CONCURRENT:
9189 case EXEC_CRITICAL:
9190 case EXEC_READ:
9191 case EXEC_WRITE:
9192 case EXEC_IOLENGTH:
9193 case EXEC_WAIT:
9194 break;
9195
9196 case EXEC_OMP_ATOMIC:
9197 case EXEC_OMP_CRITICAL:
9198 case EXEC_OMP_DO:
9199 case EXEC_OMP_MASTER:
9200 case EXEC_OMP_ORDERED:
9201 case EXEC_OMP_PARALLEL:
9202 case EXEC_OMP_PARALLEL_DO:
9203 case EXEC_OMP_PARALLEL_SECTIONS:
9204 case EXEC_OMP_PARALLEL_WORKSHARE:
9205 case EXEC_OMP_SECTIONS:
9206 case EXEC_OMP_SINGLE:
9207 case EXEC_OMP_TASK:
9208 case EXEC_OMP_TASKWAIT:
9209 case EXEC_OMP_TASKYIELD:
9210 case EXEC_OMP_WORKSHARE:
9211 break;
9212
9213 default:
9214 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9215 }
9216
9217 resolve_code (b->next, ns);
9218 }
9219 }
9220
9221
9222 /* Does everything to resolve an ordinary assignment. Returns true
9223 if this is an interface assignment. */
9224 static bool
9225 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9226 {
9227 bool rval = false;
9228 gfc_expr *lhs;
9229 gfc_expr *rhs;
9230 int llen = 0;
9231 int rlen = 0;
9232 int n;
9233 gfc_ref *ref;
9234
9235 if (gfc_extend_assign (code, ns) == SUCCESS)
9236 {
9237 gfc_expr** rhsptr;
9238
9239 if (code->op == EXEC_ASSIGN_CALL)
9240 {
9241 lhs = code->ext.actual->expr;
9242 rhsptr = &code->ext.actual->next->expr;
9243 }
9244 else
9245 {
9246 gfc_actual_arglist* args;
9247 gfc_typebound_proc* tbp;
9248
9249 gcc_assert (code->op == EXEC_COMPCALL);
9250
9251 args = code->expr1->value.compcall.actual;
9252 lhs = args->expr;
9253 rhsptr = &args->next->expr;
9254
9255 tbp = code->expr1->value.compcall.tbp;
9256 gcc_assert (!tbp->is_generic);
9257 }
9258
9259 /* Make a temporary rhs when there is a default initializer
9260 and rhs is the same symbol as the lhs. */
9261 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9262 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9263 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9264 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9265 *rhsptr = gfc_get_parentheses (*rhsptr);
9266
9267 return true;
9268 }
9269
9270 lhs = code->expr1;
9271 rhs = code->expr2;
9272
9273 if (rhs->is_boz
9274 && gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9275 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9276 &code->loc) == FAILURE)
9277 return false;
9278
9279 /* Handle the case of a BOZ literal on the RHS. */
9280 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9281 {
9282 int rc;
9283 if (gfc_option.warn_surprising)
9284 gfc_warning ("BOZ literal at %L is bitwise transferred "
9285 "non-integer symbol '%s'", &code->loc,
9286 lhs->symtree->n.sym->name);
9287
9288 if (!gfc_convert_boz (rhs, &lhs->ts))
9289 return false;
9290 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9291 {
9292 if (rc == ARITH_UNDERFLOW)
9293 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9294 ". This check can be disabled with the option "
9295 "-fno-range-check", &rhs->where);
9296 else if (rc == ARITH_OVERFLOW)
9297 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9298 ". This check can be disabled with the option "
9299 "-fno-range-check", &rhs->where);
9300 else if (rc == ARITH_NAN)
9301 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9302 ". This check can be disabled with the option "
9303 "-fno-range-check", &rhs->where);
9304 return false;
9305 }
9306 }
9307
9308 if (lhs->ts.type == BT_CHARACTER
9309 && gfc_option.warn_character_truncation)
9310 {
9311 if (lhs->ts.u.cl != NULL
9312 && lhs->ts.u.cl->length != NULL
9313 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9314 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9315
9316 if (rhs->expr_type == EXPR_CONSTANT)
9317 rlen = rhs->value.character.length;
9318
9319 else if (rhs->ts.u.cl != NULL
9320 && rhs->ts.u.cl->length != NULL
9321 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9322 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9323
9324 if (rlen && llen && rlen > llen)
9325 gfc_warning_now ("CHARACTER expression will be truncated "
9326 "in assignment (%d/%d) at %L",
9327 llen, rlen, &code->loc);
9328 }
9329
9330 /* Ensure that a vector index expression for the lvalue is evaluated
9331 to a temporary if the lvalue symbol is referenced in it. */
9332 if (lhs->rank)
9333 {
9334 for (ref = lhs->ref; ref; ref= ref->next)
9335 if (ref->type == REF_ARRAY)
9336 {
9337 for (n = 0; n < ref->u.ar.dimen; n++)
9338 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9339 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9340 ref->u.ar.start[n]))
9341 ref->u.ar.start[n]
9342 = gfc_get_parentheses (ref->u.ar.start[n]);
9343 }
9344 }
9345
9346 if (gfc_pure (NULL))
9347 {
9348 if (lhs->ts.type == BT_DERIVED
9349 && lhs->expr_type == EXPR_VARIABLE
9350 && lhs->ts.u.derived->attr.pointer_comp
9351 && rhs->expr_type == EXPR_VARIABLE
9352 && (gfc_impure_variable (rhs->symtree->n.sym)
9353 || gfc_is_coindexed (rhs)))
9354 {
9355 /* F2008, C1283. */
9356 if (gfc_is_coindexed (rhs))
9357 gfc_error ("Coindexed expression at %L is assigned to "
9358 "a derived type variable with a POINTER "
9359 "component in a PURE procedure",
9360 &rhs->where);
9361 else
9362 gfc_error ("The impure variable at %L is assigned to "
9363 "a derived type variable with a POINTER "
9364 "component in a PURE procedure (12.6)",
9365 &rhs->where);
9366 return rval;
9367 }
9368
9369 /* Fortran 2008, C1283. */
9370 if (gfc_is_coindexed (lhs))
9371 {
9372 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9373 "procedure", &rhs->where);
9374 return rval;
9375 }
9376 }
9377
9378 if (gfc_implicit_pure (NULL))
9379 {
9380 if (lhs->expr_type == EXPR_VARIABLE
9381 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9382 && lhs->symtree->n.sym->ns != gfc_current_ns)
9383 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9384
9385 if (lhs->ts.type == BT_DERIVED
9386 && lhs->expr_type == EXPR_VARIABLE
9387 && lhs->ts.u.derived->attr.pointer_comp
9388 && rhs->expr_type == EXPR_VARIABLE
9389 && (gfc_impure_variable (rhs->symtree->n.sym)
9390 || gfc_is_coindexed (rhs)))
9391 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9392
9393 /* Fortran 2008, C1283. */
9394 if (gfc_is_coindexed (lhs))
9395 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9396 }
9397
9398 /* F03:7.4.1.2. */
9399 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9400 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9401 if (lhs->ts.type == BT_CLASS)
9402 {
9403 gfc_error ("Variable must not be polymorphic in intrinsic assignment at "
9404 "%L - check that there is a matching specific subroutine "
9405 "for '=' operator", &lhs->where);
9406 return false;
9407 }
9408
9409 /* F2008, Section 7.2.1.2. */
9410 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9411 {
9412 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9413 "component in assignment at %L", &lhs->where);
9414 return false;
9415 }
9416
9417 gfc_check_assign (lhs, rhs, 1);
9418 return false;
9419 }
9420
9421
9422 /* Given a block of code, recursively resolve everything pointed to by this
9423 code block. */
9424
9425 static void
9426 resolve_code (gfc_code *code, gfc_namespace *ns)
9427 {
9428 int omp_workshare_save;
9429 int forall_save, do_concurrent_save;
9430 code_stack frame;
9431 gfc_try t;
9432
9433 frame.prev = cs_base;
9434 frame.head = code;
9435 cs_base = &frame;
9436
9437 find_reachable_labels (code);
9438
9439 for (; code; code = code->next)
9440 {
9441 frame.current = code;
9442 forall_save = forall_flag;
9443 do_concurrent_save = do_concurrent_flag;
9444
9445 if (code->op == EXEC_FORALL)
9446 {
9447 forall_flag = 1;
9448 gfc_resolve_forall (code, ns, forall_save);
9449 forall_flag = 2;
9450 }
9451 else if (code->block)
9452 {
9453 omp_workshare_save = -1;
9454 switch (code->op)
9455 {
9456 case EXEC_OMP_PARALLEL_WORKSHARE:
9457 omp_workshare_save = omp_workshare_flag;
9458 omp_workshare_flag = 1;
9459 gfc_resolve_omp_parallel_blocks (code, ns);
9460 break;
9461 case EXEC_OMP_PARALLEL:
9462 case EXEC_OMP_PARALLEL_DO:
9463 case EXEC_OMP_PARALLEL_SECTIONS:
9464 case EXEC_OMP_TASK:
9465 omp_workshare_save = omp_workshare_flag;
9466 omp_workshare_flag = 0;
9467 gfc_resolve_omp_parallel_blocks (code, ns);
9468 break;
9469 case EXEC_OMP_DO:
9470 gfc_resolve_omp_do_blocks (code, ns);
9471 break;
9472 case EXEC_SELECT_TYPE:
9473 /* Blocks are handled in resolve_select_type because we have
9474 to transform the SELECT TYPE into ASSOCIATE first. */
9475 break;
9476 case EXEC_DO_CONCURRENT:
9477 do_concurrent_flag = 1;
9478 gfc_resolve_blocks (code->block, ns);
9479 do_concurrent_flag = 2;
9480 break;
9481 case EXEC_OMP_WORKSHARE:
9482 omp_workshare_save = omp_workshare_flag;
9483 omp_workshare_flag = 1;
9484 /* FALL THROUGH */
9485 default:
9486 gfc_resolve_blocks (code->block, ns);
9487 break;
9488 }
9489
9490 if (omp_workshare_save != -1)
9491 omp_workshare_flag = omp_workshare_save;
9492 }
9493
9494 t = SUCCESS;
9495 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9496 t = gfc_resolve_expr (code->expr1);
9497 forall_flag = forall_save;
9498 do_concurrent_flag = do_concurrent_save;
9499
9500 if (gfc_resolve_expr (code->expr2) == FAILURE)
9501 t = FAILURE;
9502
9503 if (code->op == EXEC_ALLOCATE
9504 && gfc_resolve_expr (code->expr3) == FAILURE)
9505 t = FAILURE;
9506
9507 switch (code->op)
9508 {
9509 case EXEC_NOP:
9510 case EXEC_END_BLOCK:
9511 case EXEC_END_NESTED_BLOCK:
9512 case EXEC_CYCLE:
9513 case EXEC_PAUSE:
9514 case EXEC_STOP:
9515 case EXEC_ERROR_STOP:
9516 case EXEC_EXIT:
9517 case EXEC_CONTINUE:
9518 case EXEC_DT_END:
9519 case EXEC_ASSIGN_CALL:
9520 case EXEC_CRITICAL:
9521 break;
9522
9523 case EXEC_SYNC_ALL:
9524 case EXEC_SYNC_IMAGES:
9525 case EXEC_SYNC_MEMORY:
9526 resolve_sync (code);
9527 break;
9528
9529 case EXEC_LOCK:
9530 case EXEC_UNLOCK:
9531 resolve_lock_unlock (code);
9532 break;
9533
9534 case EXEC_ENTRY:
9535 /* Keep track of which entry we are up to. */
9536 current_entry_id = code->ext.entry->id;
9537 break;
9538
9539 case EXEC_WHERE:
9540 resolve_where (code, NULL);
9541 break;
9542
9543 case EXEC_GOTO:
9544 if (code->expr1 != NULL)
9545 {
9546 if (code->expr1->ts.type != BT_INTEGER)
9547 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9548 "INTEGER variable", &code->expr1->where);
9549 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9550 gfc_error ("Variable '%s' has not been assigned a target "
9551 "label at %L", code->expr1->symtree->n.sym->name,
9552 &code->expr1->where);
9553 }
9554 else
9555 resolve_branch (code->label1, code);
9556 break;
9557
9558 case EXEC_RETURN:
9559 if (code->expr1 != NULL
9560 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9561 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9562 "INTEGER return specifier", &code->expr1->where);
9563 break;
9564
9565 case EXEC_INIT_ASSIGN:
9566 case EXEC_END_PROCEDURE:
9567 break;
9568
9569 case EXEC_ASSIGN:
9570 if (t == FAILURE)
9571 break;
9572
9573 if (gfc_check_vardef_context (code->expr1, false, false,
9574 _("assignment")) == FAILURE)
9575 break;
9576
9577 if (resolve_ordinary_assign (code, ns))
9578 {
9579 if (code->op == EXEC_COMPCALL)
9580 goto compcall;
9581 else
9582 goto call;
9583 }
9584 break;
9585
9586 case EXEC_LABEL_ASSIGN:
9587 if (code->label1->defined == ST_LABEL_UNKNOWN)
9588 gfc_error ("Label %d referenced at %L is never defined",
9589 code->label1->value, &code->label1->where);
9590 if (t == SUCCESS
9591 && (code->expr1->expr_type != EXPR_VARIABLE
9592 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9593 || code->expr1->symtree->n.sym->ts.kind
9594 != gfc_default_integer_kind
9595 || code->expr1->symtree->n.sym->as != NULL))
9596 gfc_error ("ASSIGN statement at %L requires a scalar "
9597 "default INTEGER variable", &code->expr1->where);
9598 break;
9599
9600 case EXEC_POINTER_ASSIGN:
9601 {
9602 gfc_expr* e;
9603
9604 if (t == FAILURE)
9605 break;
9606
9607 /* This is both a variable definition and pointer assignment
9608 context, so check both of them. For rank remapping, a final
9609 array ref may be present on the LHS and fool gfc_expr_attr
9610 used in gfc_check_vardef_context. Remove it. */
9611 e = remove_last_array_ref (code->expr1);
9612 t = gfc_check_vardef_context (e, true, false,
9613 _("pointer assignment"));
9614 if (t == SUCCESS)
9615 t = gfc_check_vardef_context (e, false, false,
9616 _("pointer assignment"));
9617 gfc_free_expr (e);
9618 if (t == FAILURE)
9619 break;
9620
9621 gfc_check_pointer_assign (code->expr1, code->expr2);
9622 break;
9623 }
9624
9625 case EXEC_ARITHMETIC_IF:
9626 if (t == SUCCESS
9627 && code->expr1->ts.type != BT_INTEGER
9628 && code->expr1->ts.type != BT_REAL)
9629 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9630 "expression", &code->expr1->where);
9631
9632 resolve_branch (code->label1, code);
9633 resolve_branch (code->label2, code);
9634 resolve_branch (code->label3, code);
9635 break;
9636
9637 case EXEC_IF:
9638 if (t == SUCCESS && code->expr1 != NULL
9639 && (code->expr1->ts.type != BT_LOGICAL
9640 || code->expr1->rank != 0))
9641 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9642 &code->expr1->where);
9643 break;
9644
9645 case EXEC_CALL:
9646 call:
9647 resolve_call (code);
9648 break;
9649
9650 case EXEC_COMPCALL:
9651 compcall:
9652 resolve_typebound_subroutine (code);
9653 break;
9654
9655 case EXEC_CALL_PPC:
9656 resolve_ppc_call (code);
9657 break;
9658
9659 case EXEC_SELECT:
9660 /* Select is complicated. Also, a SELECT construct could be
9661 a transformed computed GOTO. */
9662 resolve_select (code);
9663 break;
9664
9665 case EXEC_SELECT_TYPE:
9666 resolve_select_type (code, ns);
9667 break;
9668
9669 case EXEC_BLOCK:
9670 resolve_block_construct (code);
9671 break;
9672
9673 case EXEC_DO:
9674 if (code->ext.iterator != NULL)
9675 {
9676 gfc_iterator *iter = code->ext.iterator;
9677 if (gfc_resolve_iterator (iter, true) != FAILURE)
9678 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9679 }
9680 break;
9681
9682 case EXEC_DO_WHILE:
9683 if (code->expr1 == NULL)
9684 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9685 if (t == SUCCESS
9686 && (code->expr1->rank != 0
9687 || code->expr1->ts.type != BT_LOGICAL))
9688 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9689 "a scalar LOGICAL expression", &code->expr1->where);
9690 break;
9691
9692 case EXEC_ALLOCATE:
9693 if (t == SUCCESS)
9694 resolve_allocate_deallocate (code, "ALLOCATE");
9695
9696 break;
9697
9698 case EXEC_DEALLOCATE:
9699 if (t == SUCCESS)
9700 resolve_allocate_deallocate (code, "DEALLOCATE");
9701
9702 break;
9703
9704 case EXEC_OPEN:
9705 if (gfc_resolve_open (code->ext.open) == FAILURE)
9706 break;
9707
9708 resolve_branch (code->ext.open->err, code);
9709 break;
9710
9711 case EXEC_CLOSE:
9712 if (gfc_resolve_close (code->ext.close) == FAILURE)
9713 break;
9714
9715 resolve_branch (code->ext.close->err, code);
9716 break;
9717
9718 case EXEC_BACKSPACE:
9719 case EXEC_ENDFILE:
9720 case EXEC_REWIND:
9721 case EXEC_FLUSH:
9722 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9723 break;
9724
9725 resolve_branch (code->ext.filepos->err, code);
9726 break;
9727
9728 case EXEC_INQUIRE:
9729 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9730 break;
9731
9732 resolve_branch (code->ext.inquire->err, code);
9733 break;
9734
9735 case EXEC_IOLENGTH:
9736 gcc_assert (code->ext.inquire != NULL);
9737 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9738 break;
9739
9740 resolve_branch (code->ext.inquire->err, code);
9741 break;
9742
9743 case EXEC_WAIT:
9744 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9745 break;
9746
9747 resolve_branch (code->ext.wait->err, code);
9748 resolve_branch (code->ext.wait->end, code);
9749 resolve_branch (code->ext.wait->eor, code);
9750 break;
9751
9752 case EXEC_READ:
9753 case EXEC_WRITE:
9754 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9755 break;
9756
9757 resolve_branch (code->ext.dt->err, code);
9758 resolve_branch (code->ext.dt->end, code);
9759 resolve_branch (code->ext.dt->eor, code);
9760 break;
9761
9762 case EXEC_TRANSFER:
9763 resolve_transfer (code);
9764 break;
9765
9766 case EXEC_DO_CONCURRENT:
9767 case EXEC_FORALL:
9768 resolve_forall_iterators (code->ext.forall_iterator);
9769
9770 if (code->expr1 != NULL
9771 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9772 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9773 "expression", &code->expr1->where);
9774 break;
9775
9776 case EXEC_OMP_ATOMIC:
9777 case EXEC_OMP_BARRIER:
9778 case EXEC_OMP_CRITICAL:
9779 case EXEC_OMP_FLUSH:
9780 case EXEC_OMP_DO:
9781 case EXEC_OMP_MASTER:
9782 case EXEC_OMP_ORDERED:
9783 case EXEC_OMP_SECTIONS:
9784 case EXEC_OMP_SINGLE:
9785 case EXEC_OMP_TASKWAIT:
9786 case EXEC_OMP_TASKYIELD:
9787 case EXEC_OMP_WORKSHARE:
9788 gfc_resolve_omp_directive (code, ns);
9789 break;
9790
9791 case EXEC_OMP_PARALLEL:
9792 case EXEC_OMP_PARALLEL_DO:
9793 case EXEC_OMP_PARALLEL_SECTIONS:
9794 case EXEC_OMP_PARALLEL_WORKSHARE:
9795 case EXEC_OMP_TASK:
9796 omp_workshare_save = omp_workshare_flag;
9797 omp_workshare_flag = 0;
9798 gfc_resolve_omp_directive (code, ns);
9799 omp_workshare_flag = omp_workshare_save;
9800 break;
9801
9802 default:
9803 gfc_internal_error ("resolve_code(): Bad statement code");
9804 }
9805 }
9806
9807 cs_base = frame.prev;
9808 }
9809
9810
9811 /* Resolve initial values and make sure they are compatible with
9812 the variable. */
9813
9814 static void
9815 resolve_values (gfc_symbol *sym)
9816 {
9817 gfc_try t;
9818
9819 if (sym->value == NULL)
9820 return;
9821
9822 if (sym->value->expr_type == EXPR_STRUCTURE)
9823 t= resolve_structure_cons (sym->value, 1);
9824 else
9825 t = gfc_resolve_expr (sym->value);
9826
9827 if (t == FAILURE)
9828 return;
9829
9830 gfc_check_assign_symbol (sym, sym->value);
9831 }
9832
9833
9834 /* Verify the binding labels for common blocks that are BIND(C). The label
9835 for a BIND(C) common block must be identical in all scoping units in which
9836 the common block is declared. Further, the binding label can not collide
9837 with any other global entity in the program. */
9838
9839 static void
9840 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9841 {
9842 if (comm_block_tree->n.common->is_bind_c == 1)
9843 {
9844 gfc_gsymbol *binding_label_gsym;
9845 gfc_gsymbol *comm_name_gsym;
9846 const char * bind_label = comm_block_tree->n.common->binding_label
9847 ? comm_block_tree->n.common->binding_label : "";
9848
9849 /* See if a global symbol exists by the common block's name. It may
9850 be NULL if the common block is use-associated. */
9851 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9852 comm_block_tree->n.common->name);
9853 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9854 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9855 "with the global entity '%s' at %L",
9856 bind_label,
9857 comm_block_tree->n.common->name,
9858 &(comm_block_tree->n.common->where),
9859 comm_name_gsym->name, &(comm_name_gsym->where));
9860 else if (comm_name_gsym != NULL
9861 && strcmp (comm_name_gsym->name,
9862 comm_block_tree->n.common->name) == 0)
9863 {
9864 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9865 as expected. */
9866 if (comm_name_gsym->binding_label == NULL)
9867 /* No binding label for common block stored yet; save this one. */
9868 comm_name_gsym->binding_label = bind_label;
9869 else if (strcmp (comm_name_gsym->binding_label, bind_label) != 0)
9870 {
9871 /* Common block names match but binding labels do not. */
9872 gfc_error ("Binding label '%s' for common block '%s' at %L "
9873 "does not match the binding label '%s' for common "
9874 "block '%s' at %L",
9875 bind_label,
9876 comm_block_tree->n.common->name,
9877 &(comm_block_tree->n.common->where),
9878 comm_name_gsym->binding_label,
9879 comm_name_gsym->name,
9880 &(comm_name_gsym->where));
9881 return;
9882 }
9883 }
9884
9885 /* There is no binding label (NAME="") so we have nothing further to
9886 check and nothing to add as a global symbol for the label. */
9887 if (!comm_block_tree->n.common->binding_label)
9888 return;
9889
9890 binding_label_gsym =
9891 gfc_find_gsymbol (gfc_gsym_root,
9892 comm_block_tree->n.common->binding_label);
9893 if (binding_label_gsym == NULL)
9894 {
9895 /* Need to make a global symbol for the binding label to prevent
9896 it from colliding with another. */
9897 binding_label_gsym =
9898 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9899 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9900 binding_label_gsym->type = GSYM_COMMON;
9901 }
9902 else
9903 {
9904 /* If comm_name_gsym is NULL, the name common block is use
9905 associated and the name could be colliding. */
9906 if (binding_label_gsym->type != GSYM_COMMON)
9907 gfc_error ("Binding label '%s' for common block '%s' at %L "
9908 "collides with the global entity '%s' at %L",
9909 comm_block_tree->n.common->binding_label,
9910 comm_block_tree->n.common->name,
9911 &(comm_block_tree->n.common->where),
9912 binding_label_gsym->name,
9913 &(binding_label_gsym->where));
9914 else if (comm_name_gsym != NULL
9915 && (strcmp (binding_label_gsym->name,
9916 comm_name_gsym->binding_label) != 0)
9917 && (strcmp (binding_label_gsym->sym_name,
9918 comm_name_gsym->name) != 0))
9919 gfc_error ("Binding label '%s' for common block '%s' at %L "
9920 "collides with global entity '%s' at %L",
9921 binding_label_gsym->name, binding_label_gsym->sym_name,
9922 &(comm_block_tree->n.common->where),
9923 comm_name_gsym->name, &(comm_name_gsym->where));
9924 }
9925 }
9926
9927 return;
9928 }
9929
9930
9931 /* Verify any BIND(C) derived types in the namespace so we can report errors
9932 for them once, rather than for each variable declared of that type. */
9933
9934 static void
9935 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9936 {
9937 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9938 && derived_sym->attr.is_bind_c == 1)
9939 verify_bind_c_derived_type (derived_sym);
9940
9941 return;
9942 }
9943
9944
9945 /* Verify that any binding labels used in a given namespace do not collide
9946 with the names or binding labels of any global symbols. */
9947
9948 static void
9949 gfc_verify_binding_labels (gfc_symbol *sym)
9950 {
9951 int has_error = 0;
9952
9953 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9954 && sym->attr.flavor != FL_DERIVED && sym->binding_label)
9955 {
9956 gfc_gsymbol *bind_c_sym;
9957
9958 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9959 if (bind_c_sym != NULL
9960 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9961 {
9962 if (sym->attr.if_source == IFSRC_DECL
9963 && (bind_c_sym->type != GSYM_SUBROUTINE
9964 && bind_c_sym->type != GSYM_FUNCTION)
9965 && ((sym->attr.contained == 1
9966 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9967 || (sym->attr.use_assoc == 1
9968 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9969 {
9970 /* Make sure global procedures don't collide with anything. */
9971 gfc_error ("Binding label '%s' at %L collides with the global "
9972 "entity '%s' at %L", sym->binding_label,
9973 &(sym->declared_at), bind_c_sym->name,
9974 &(bind_c_sym->where));
9975 has_error = 1;
9976 }
9977 else if (sym->attr.contained == 0
9978 && (sym->attr.if_source == IFSRC_IFBODY
9979 && sym->attr.flavor == FL_PROCEDURE)
9980 && (bind_c_sym->sym_name != NULL
9981 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9982 {
9983 /* Make sure procedures in interface bodies don't collide. */
9984 gfc_error ("Binding label '%s' in interface body at %L collides "
9985 "with the global entity '%s' at %L",
9986 sym->binding_label,
9987 &(sym->declared_at), bind_c_sym->name,
9988 &(bind_c_sym->where));
9989 has_error = 1;
9990 }
9991 else if (sym->attr.contained == 0
9992 && sym->attr.if_source == IFSRC_UNKNOWN)
9993 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9994 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9995 || sym->attr.use_assoc == 0)
9996 {
9997 gfc_error ("Binding label '%s' at %L collides with global "
9998 "entity '%s' at %L", sym->binding_label,
9999 &(sym->declared_at), bind_c_sym->name,
10000 &(bind_c_sym->where));
10001 has_error = 1;
10002 }
10003
10004 if (has_error != 0)
10005 /* Clear the binding label to prevent checking multiple times. */
10006 sym->binding_label = NULL;
10007 }
10008 else if (bind_c_sym == NULL)
10009 {
10010 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
10011 bind_c_sym->where = sym->declared_at;
10012 bind_c_sym->sym_name = sym->name;
10013
10014 if (sym->attr.use_assoc == 1)
10015 bind_c_sym->mod_name = sym->module;
10016 else
10017 if (sym->ns->proc_name != NULL)
10018 bind_c_sym->mod_name = sym->ns->proc_name->name;
10019
10020 if (sym->attr.contained == 0)
10021 {
10022 if (sym->attr.subroutine)
10023 bind_c_sym->type = GSYM_SUBROUTINE;
10024 else if (sym->attr.function)
10025 bind_c_sym->type = GSYM_FUNCTION;
10026 }
10027 }
10028 }
10029 return;
10030 }
10031
10032
10033 /* Resolve an index expression. */
10034
10035 static gfc_try
10036 resolve_index_expr (gfc_expr *e)
10037 {
10038 if (gfc_resolve_expr (e) == FAILURE)
10039 return FAILURE;
10040
10041 if (gfc_simplify_expr (e, 0) == FAILURE)
10042 return FAILURE;
10043
10044 if (gfc_specification_expr (e) == FAILURE)
10045 return FAILURE;
10046
10047 return SUCCESS;
10048 }
10049
10050
10051 /* Resolve a charlen structure. */
10052
10053 static gfc_try
10054 resolve_charlen (gfc_charlen *cl)
10055 {
10056 int i, k;
10057
10058 if (cl->resolved)
10059 return SUCCESS;
10060
10061 cl->resolved = 1;
10062
10063
10064 if (cl->length_from_typespec)
10065 {
10066 if (gfc_resolve_expr (cl->length) == FAILURE)
10067 return FAILURE;
10068
10069 if (gfc_simplify_expr (cl->length, 0) == FAILURE)
10070 return FAILURE;
10071 }
10072 else
10073 {
10074 specification_expr = 1;
10075
10076 if (resolve_index_expr (cl->length) == FAILURE)
10077 {
10078 specification_expr = 0;
10079 return FAILURE;
10080 }
10081 }
10082
10083 /* "If the character length parameter value evaluates to a negative
10084 value, the length of character entities declared is zero." */
10085 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10086 {
10087 if (gfc_option.warn_surprising)
10088 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10089 " the length has been set to zero",
10090 &cl->length->where, i);
10091 gfc_replace_expr (cl->length,
10092 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10093 }
10094
10095 /* Check that the character length is not too large. */
10096 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10097 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10098 && cl->length->ts.type == BT_INTEGER
10099 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10100 {
10101 gfc_error ("String length at %L is too large", &cl->length->where);
10102 return FAILURE;
10103 }
10104
10105 return SUCCESS;
10106 }
10107
10108
10109 /* Test for non-constant shape arrays. */
10110
10111 static bool
10112 is_non_constant_shape_array (gfc_symbol *sym)
10113 {
10114 gfc_expr *e;
10115 int i;
10116 bool not_constant;
10117
10118 not_constant = false;
10119 if (sym->as != NULL)
10120 {
10121 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10122 has not been simplified; parameter array references. Do the
10123 simplification now. */
10124 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10125 {
10126 e = sym->as->lower[i];
10127 if (e && (resolve_index_expr (e) == FAILURE
10128 || !gfc_is_constant_expr (e)))
10129 not_constant = true;
10130 e = sym->as->upper[i];
10131 if (e && (resolve_index_expr (e) == FAILURE
10132 || !gfc_is_constant_expr (e)))
10133 not_constant = true;
10134 }
10135 }
10136 return not_constant;
10137 }
10138
10139 /* Given a symbol and an initialization expression, add code to initialize
10140 the symbol to the function entry. */
10141 static void
10142 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10143 {
10144 gfc_expr *lval;
10145 gfc_code *init_st;
10146 gfc_namespace *ns = sym->ns;
10147
10148 /* Search for the function namespace if this is a contained
10149 function without an explicit result. */
10150 if (sym->attr.function && sym == sym->result
10151 && sym->name != sym->ns->proc_name->name)
10152 {
10153 ns = ns->contained;
10154 for (;ns; ns = ns->sibling)
10155 if (strcmp (ns->proc_name->name, sym->name) == 0)
10156 break;
10157 }
10158
10159 if (ns == NULL)
10160 {
10161 gfc_free_expr (init);
10162 return;
10163 }
10164
10165 /* Build an l-value expression for the result. */
10166 lval = gfc_lval_expr_from_sym (sym);
10167
10168 /* Add the code at scope entry. */
10169 init_st = gfc_get_code ();
10170 init_st->next = ns->code;
10171 ns->code = init_st;
10172
10173 /* Assign the default initializer to the l-value. */
10174 init_st->loc = sym->declared_at;
10175 init_st->op = EXEC_INIT_ASSIGN;
10176 init_st->expr1 = lval;
10177 init_st->expr2 = init;
10178 }
10179
10180 /* Assign the default initializer to a derived type variable or result. */
10181
10182 static void
10183 apply_default_init (gfc_symbol *sym)
10184 {
10185 gfc_expr *init = NULL;
10186
10187 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10188 return;
10189
10190 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10191 init = gfc_default_initializer (&sym->ts);
10192
10193 if (init == NULL && sym->ts.type != BT_CLASS)
10194 return;
10195
10196 build_init_assign (sym, init);
10197 sym->attr.referenced = 1;
10198 }
10199
10200 /* Build an initializer for a local integer, real, complex, logical, or
10201 character variable, based on the command line flags finit-local-zero,
10202 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10203 null if the symbol should not have a default initialization. */
10204 static gfc_expr *
10205 build_default_init_expr (gfc_symbol *sym)
10206 {
10207 int char_len;
10208 gfc_expr *init_expr;
10209 int i;
10210
10211 /* These symbols should never have a default initialization. */
10212 if (sym->attr.allocatable
10213 || sym->attr.external
10214 || sym->attr.dummy
10215 || sym->attr.pointer
10216 || sym->attr.in_equivalence
10217 || sym->attr.in_common
10218 || sym->attr.data
10219 || sym->module
10220 || sym->attr.cray_pointee
10221 || sym->attr.cray_pointer
10222 || sym->assoc)
10223 return NULL;
10224
10225 /* Now we'll try to build an initializer expression. */
10226 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10227 &sym->declared_at);
10228
10229 /* We will only initialize integers, reals, complex, logicals, and
10230 characters, and only if the corresponding command-line flags
10231 were set. Otherwise, we free init_expr and return null. */
10232 switch (sym->ts.type)
10233 {
10234 case BT_INTEGER:
10235 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10236 mpz_set_si (init_expr->value.integer,
10237 gfc_option.flag_init_integer_value);
10238 else
10239 {
10240 gfc_free_expr (init_expr);
10241 init_expr = NULL;
10242 }
10243 break;
10244
10245 case BT_REAL:
10246 switch (gfc_option.flag_init_real)
10247 {
10248 case GFC_INIT_REAL_SNAN:
10249 init_expr->is_snan = 1;
10250 /* Fall through. */
10251 case GFC_INIT_REAL_NAN:
10252 mpfr_set_nan (init_expr->value.real);
10253 break;
10254
10255 case GFC_INIT_REAL_INF:
10256 mpfr_set_inf (init_expr->value.real, 1);
10257 break;
10258
10259 case GFC_INIT_REAL_NEG_INF:
10260 mpfr_set_inf (init_expr->value.real, -1);
10261 break;
10262
10263 case GFC_INIT_REAL_ZERO:
10264 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10265 break;
10266
10267 default:
10268 gfc_free_expr (init_expr);
10269 init_expr = NULL;
10270 break;
10271 }
10272 break;
10273
10274 case BT_COMPLEX:
10275 switch (gfc_option.flag_init_real)
10276 {
10277 case GFC_INIT_REAL_SNAN:
10278 init_expr->is_snan = 1;
10279 /* Fall through. */
10280 case GFC_INIT_REAL_NAN:
10281 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10282 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10283 break;
10284
10285 case GFC_INIT_REAL_INF:
10286 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10287 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10288 break;
10289
10290 case GFC_INIT_REAL_NEG_INF:
10291 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10292 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10293 break;
10294
10295 case GFC_INIT_REAL_ZERO:
10296 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10297 break;
10298
10299 default:
10300 gfc_free_expr (init_expr);
10301 init_expr = NULL;
10302 break;
10303 }
10304 break;
10305
10306 case BT_LOGICAL:
10307 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10308 init_expr->value.logical = 0;
10309 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10310 init_expr->value.logical = 1;
10311 else
10312 {
10313 gfc_free_expr (init_expr);
10314 init_expr = NULL;
10315 }
10316 break;
10317
10318 case BT_CHARACTER:
10319 /* For characters, the length must be constant in order to
10320 create a default initializer. */
10321 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10322 && sym->ts.u.cl->length
10323 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10324 {
10325 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10326 init_expr->value.character.length = char_len;
10327 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10328 for (i = 0; i < char_len; i++)
10329 init_expr->value.character.string[i]
10330 = (unsigned char) gfc_option.flag_init_character_value;
10331 }
10332 else
10333 {
10334 gfc_free_expr (init_expr);
10335 init_expr = NULL;
10336 }
10337 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10338 && sym->ts.u.cl->length)
10339 {
10340 gfc_actual_arglist *arg;
10341 init_expr = gfc_get_expr ();
10342 init_expr->where = sym->declared_at;
10343 init_expr->ts = sym->ts;
10344 init_expr->expr_type = EXPR_FUNCTION;
10345 init_expr->value.function.isym =
10346 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10347 init_expr->value.function.name = "repeat";
10348 arg = gfc_get_actual_arglist ();
10349 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10350 NULL, 1);
10351 arg->expr->value.character.string[0]
10352 = gfc_option.flag_init_character_value;
10353 arg->next = gfc_get_actual_arglist ();
10354 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10355 init_expr->value.function.actual = arg;
10356 }
10357 break;
10358
10359 default:
10360 gfc_free_expr (init_expr);
10361 init_expr = NULL;
10362 }
10363 return init_expr;
10364 }
10365
10366 /* Add an initialization expression to a local variable. */
10367 static void
10368 apply_default_init_local (gfc_symbol *sym)
10369 {
10370 gfc_expr *init = NULL;
10371
10372 /* The symbol should be a variable or a function return value. */
10373 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10374 || (sym->attr.function && sym->result != sym))
10375 return;
10376
10377 /* Try to build the initializer expression. If we can't initialize
10378 this symbol, then init will be NULL. */
10379 init = build_default_init_expr (sym);
10380 if (init == NULL)
10381 return;
10382
10383 /* For saved variables, we don't want to add an initializer at function
10384 entry, so we just add a static initializer. Note that automatic variables
10385 are stack allocated even with -fno-automatic. */
10386 if (sym->attr.save || sym->ns->save_all
10387 || (gfc_option.flag_max_stack_var_size == 0
10388 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10389 {
10390 /* Don't clobber an existing initializer! */
10391 gcc_assert (sym->value == NULL);
10392 sym->value = init;
10393 return;
10394 }
10395
10396 build_init_assign (sym, init);
10397 }
10398
10399
10400 /* Resolution of common features of flavors variable and procedure. */
10401
10402 static gfc_try
10403 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10404 {
10405 gfc_array_spec *as;
10406
10407 /* Avoid double diagnostics for function result symbols. */
10408 if ((sym->result || sym->attr.result) && !sym->attr.dummy
10409 && (sym->ns != gfc_current_ns))
10410 return SUCCESS;
10411
10412 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10413 as = CLASS_DATA (sym)->as;
10414 else
10415 as = sym->as;
10416
10417 /* Constraints on deferred shape variable. */
10418 if (as == NULL || as->type != AS_DEFERRED)
10419 {
10420 bool pointer, allocatable, dimension;
10421
10422 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10423 {
10424 pointer = CLASS_DATA (sym)->attr.class_pointer;
10425 allocatable = CLASS_DATA (sym)->attr.allocatable;
10426 dimension = CLASS_DATA (sym)->attr.dimension;
10427 }
10428 else
10429 {
10430 pointer = sym->attr.pointer;
10431 allocatable = sym->attr.allocatable;
10432 dimension = sym->attr.dimension;
10433 }
10434
10435 if (allocatable)
10436 {
10437 if (dimension && as->type != AS_ASSUMED_RANK)
10438 {
10439 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10440 "shape or assumed rank", sym->name, &sym->declared_at);
10441 return FAILURE;
10442 }
10443 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object "
10444 "'%s' at %L may not be ALLOCATABLE",
10445 sym->name, &sym->declared_at) == FAILURE)
10446 return FAILURE;
10447 }
10448
10449 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10450 {
10451 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10452 "assumed rank", sym->name, &sym->declared_at);
10453 return FAILURE;
10454 }
10455 }
10456 else
10457 {
10458 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10459 && sym->ts.type != BT_CLASS && !sym->assoc)
10460 {
10461 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10462 sym->name, &sym->declared_at);
10463 return FAILURE;
10464 }
10465 }
10466
10467 /* Constraints on polymorphic variables. */
10468 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10469 {
10470 /* F03:C502. */
10471 if (sym->attr.class_ok
10472 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10473 {
10474 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10475 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10476 &sym->declared_at);
10477 return FAILURE;
10478 }
10479
10480 /* F03:C509. */
10481 /* Assume that use associated symbols were checked in the module ns.
10482 Class-variables that are associate-names are also something special
10483 and excepted from the test. */
10484 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10485 {
10486 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10487 "or pointer", sym->name, &sym->declared_at);
10488 return FAILURE;
10489 }
10490 }
10491
10492 return SUCCESS;
10493 }
10494
10495
10496 /* Additional checks for symbols with flavor variable and derived
10497 type. To be called from resolve_fl_variable. */
10498
10499 static gfc_try
10500 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10501 {
10502 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10503
10504 /* Check to see if a derived type is blocked from being host
10505 associated by the presence of another class I symbol in the same
10506 namespace. 14.6.1.3 of the standard and the discussion on
10507 comp.lang.fortran. */
10508 if (sym->ns != sym->ts.u.derived->ns
10509 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10510 {
10511 gfc_symbol *s;
10512 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10513 if (s && s->attr.generic)
10514 s = gfc_find_dt_in_generic (s);
10515 if (s && s->attr.flavor != FL_DERIVED)
10516 {
10517 gfc_error ("The type '%s' cannot be host associated at %L "
10518 "because it is blocked by an incompatible object "
10519 "of the same name declared at %L",
10520 sym->ts.u.derived->name, &sym->declared_at,
10521 &s->declared_at);
10522 return FAILURE;
10523 }
10524 }
10525
10526 /* 4th constraint in section 11.3: "If an object of a type for which
10527 component-initialization is specified (R429) appears in the
10528 specification-part of a module and does not have the ALLOCATABLE
10529 or POINTER attribute, the object shall have the SAVE attribute."
10530
10531 The check for initializers is performed with
10532 gfc_has_default_initializer because gfc_default_initializer generates
10533 a hidden default for allocatable components. */
10534 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10535 && sym->ns->proc_name->attr.flavor == FL_MODULE
10536 && !sym->ns->save_all && !sym->attr.save
10537 && !sym->attr.pointer && !sym->attr.allocatable
10538 && gfc_has_default_initializer (sym->ts.u.derived)
10539 && gfc_notify_std (GFC_STD_F2008, "Implied SAVE for "
10540 "module variable '%s' at %L, needed due to "
10541 "the default initialization", sym->name,
10542 &sym->declared_at) == FAILURE)
10543 return FAILURE;
10544
10545 /* Assign default initializer. */
10546 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10547 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10548 {
10549 sym->value = gfc_default_initializer (&sym->ts);
10550 }
10551
10552 return SUCCESS;
10553 }
10554
10555
10556 /* Resolve symbols with flavor variable. */
10557
10558 static gfc_try
10559 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10560 {
10561 int no_init_flag, automatic_flag;
10562 gfc_expr *e;
10563 const char *auto_save_msg;
10564
10565 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10566 "SAVE attribute";
10567
10568 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10569 return FAILURE;
10570
10571 /* Set this flag to check that variables are parameters of all entries.
10572 This check is effected by the call to gfc_resolve_expr through
10573 is_non_constant_shape_array. */
10574 specification_expr = 1;
10575
10576 if (sym->ns->proc_name
10577 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10578 || sym->ns->proc_name->attr.is_main_program)
10579 && !sym->attr.use_assoc
10580 && !sym->attr.allocatable
10581 && !sym->attr.pointer
10582 && is_non_constant_shape_array (sym))
10583 {
10584 /* The shape of a main program or module array needs to be
10585 constant. */
10586 gfc_error ("The module or main program array '%s' at %L must "
10587 "have constant shape", sym->name, &sym->declared_at);
10588 specification_expr = 0;
10589 return FAILURE;
10590 }
10591
10592 /* Constraints on deferred type parameter. */
10593 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10594 {
10595 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10596 "requires either the pointer or allocatable attribute",
10597 sym->name, &sym->declared_at);
10598 return FAILURE;
10599 }
10600
10601 if (sym->ts.type == BT_CHARACTER)
10602 {
10603 /* Make sure that character string variables with assumed length are
10604 dummy arguments. */
10605 e = sym->ts.u.cl->length;
10606 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10607 && !sym->ts.deferred)
10608 {
10609 gfc_error ("Entity with assumed character length at %L must be a "
10610 "dummy argument or a PARAMETER", &sym->declared_at);
10611 return FAILURE;
10612 }
10613
10614 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10615 {
10616 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10617 return FAILURE;
10618 }
10619
10620 if (!gfc_is_constant_expr (e)
10621 && !(e->expr_type == EXPR_VARIABLE
10622 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10623 {
10624 if (!sym->attr.use_assoc && sym->ns->proc_name
10625 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10626 || sym->ns->proc_name->attr.is_main_program))
10627 {
10628 gfc_error ("'%s' at %L must have constant character length "
10629 "in this context", sym->name, &sym->declared_at);
10630 return FAILURE;
10631 }
10632 if (sym->attr.in_common)
10633 {
10634 gfc_error ("COMMON variable '%s' at %L must have constant "
10635 "character length", sym->name, &sym->declared_at);
10636 return FAILURE;
10637 }
10638 }
10639 }
10640
10641 if (sym->value == NULL && sym->attr.referenced)
10642 apply_default_init_local (sym); /* Try to apply a default initialization. */
10643
10644 /* Determine if the symbol may not have an initializer. */
10645 no_init_flag = automatic_flag = 0;
10646 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10647 || sym->attr.intrinsic || sym->attr.result)
10648 no_init_flag = 1;
10649 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10650 && is_non_constant_shape_array (sym))
10651 {
10652 no_init_flag = automatic_flag = 1;
10653
10654 /* Also, they must not have the SAVE attribute.
10655 SAVE_IMPLICIT is checked below. */
10656 if (sym->as && sym->attr.codimension)
10657 {
10658 int corank = sym->as->corank;
10659 sym->as->corank = 0;
10660 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10661 sym->as->corank = corank;
10662 }
10663 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10664 {
10665 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10666 return FAILURE;
10667 }
10668 }
10669
10670 /* Ensure that any initializer is simplified. */
10671 if (sym->value)
10672 gfc_simplify_expr (sym->value, 1);
10673
10674 /* Reject illegal initializers. */
10675 if (!sym->mark && sym->value)
10676 {
10677 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10678 && CLASS_DATA (sym)->attr.allocatable))
10679 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10680 sym->name, &sym->declared_at);
10681 else if (sym->attr.external)
10682 gfc_error ("External '%s' at %L cannot have an initializer",
10683 sym->name, &sym->declared_at);
10684 else if (sym->attr.dummy
10685 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10686 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10687 sym->name, &sym->declared_at);
10688 else if (sym->attr.intrinsic)
10689 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10690 sym->name, &sym->declared_at);
10691 else if (sym->attr.result)
10692 gfc_error ("Function result '%s' at %L cannot have an initializer",
10693 sym->name, &sym->declared_at);
10694 else if (automatic_flag)
10695 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10696 sym->name, &sym->declared_at);
10697 else
10698 goto no_init_error;
10699 return FAILURE;
10700 }
10701
10702 no_init_error:
10703 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10704 return resolve_fl_variable_derived (sym, no_init_flag);
10705
10706 return SUCCESS;
10707 }
10708
10709
10710 /* Resolve a procedure. */
10711
10712 static gfc_try
10713 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10714 {
10715 gfc_formal_arglist *arg;
10716
10717 if (sym->attr.function
10718 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10719 return FAILURE;
10720
10721 if (sym->ts.type == BT_CHARACTER)
10722 {
10723 gfc_charlen *cl = sym->ts.u.cl;
10724
10725 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10726 && resolve_charlen (cl) == FAILURE)
10727 return FAILURE;
10728
10729 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10730 && sym->attr.proc == PROC_ST_FUNCTION)
10731 {
10732 gfc_error ("Character-valued statement function '%s' at %L must "
10733 "have constant length", sym->name, &sym->declared_at);
10734 return FAILURE;
10735 }
10736 }
10737
10738 /* Ensure that derived type for are not of a private type. Internal
10739 module procedures are excluded by 2.2.3.3 - i.e., they are not
10740 externally accessible and can access all the objects accessible in
10741 the host. */
10742 if (!(sym->ns->parent
10743 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10744 && gfc_check_symbol_access (sym))
10745 {
10746 gfc_interface *iface;
10747
10748 for (arg = sym->formal; arg; arg = arg->next)
10749 {
10750 if (arg->sym
10751 && arg->sym->ts.type == BT_DERIVED
10752 && !arg->sym->ts.u.derived->attr.use_assoc
10753 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10754 && gfc_notify_std (GFC_STD_F2003, "'%s' is of a "
10755 "PRIVATE type and cannot be a dummy argument"
10756 " of '%s', which is PUBLIC at %L",
10757 arg->sym->name, sym->name, &sym->declared_at)
10758 == FAILURE)
10759 {
10760 /* Stop this message from recurring. */
10761 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10762 return FAILURE;
10763 }
10764 }
10765
10766 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10767 PRIVATE to the containing module. */
10768 for (iface = sym->generic; iface; iface = iface->next)
10769 {
10770 for (arg = iface->sym->formal; arg; arg = arg->next)
10771 {
10772 if (arg->sym
10773 && arg->sym->ts.type == BT_DERIVED
10774 && !arg->sym->ts.u.derived->attr.use_assoc
10775 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10776 && gfc_notify_std (GFC_STD_F2003, "Procedure "
10777 "'%s' in PUBLIC interface '%s' at %L "
10778 "takes dummy arguments of '%s' which is "
10779 "PRIVATE", iface->sym->name, sym->name,
10780 &iface->sym->declared_at,
10781 gfc_typename (&arg->sym->ts)) == FAILURE)
10782 {
10783 /* Stop this message from recurring. */
10784 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10785 return FAILURE;
10786 }
10787 }
10788 }
10789
10790 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10791 PRIVATE to the containing module. */
10792 for (iface = sym->generic; iface; iface = iface->next)
10793 {
10794 for (arg = iface->sym->formal; arg; arg = arg->next)
10795 {
10796 if (arg->sym
10797 && arg->sym->ts.type == BT_DERIVED
10798 && !arg->sym->ts.u.derived->attr.use_assoc
10799 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10800 && gfc_notify_std (GFC_STD_F2003, "Procedure "
10801 "'%s' in PUBLIC interface '%s' at %L "
10802 "takes dummy arguments of '%s' which is "
10803 "PRIVATE", iface->sym->name, sym->name,
10804 &iface->sym->declared_at,
10805 gfc_typename (&arg->sym->ts)) == FAILURE)
10806 {
10807 /* Stop this message from recurring. */
10808 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10809 return FAILURE;
10810 }
10811 }
10812 }
10813 }
10814
10815 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10816 && !sym->attr.proc_pointer)
10817 {
10818 gfc_error ("Function '%s' at %L cannot have an initializer",
10819 sym->name, &sym->declared_at);
10820 return FAILURE;
10821 }
10822
10823 /* An external symbol may not have an initializer because it is taken to be
10824 a procedure. Exception: Procedure Pointers. */
10825 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10826 {
10827 gfc_error ("External object '%s' at %L may not have an initializer",
10828 sym->name, &sym->declared_at);
10829 return FAILURE;
10830 }
10831
10832 /* An elemental function is required to return a scalar 12.7.1 */
10833 if (sym->attr.elemental && sym->attr.function && sym->as)
10834 {
10835 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10836 "result", sym->name, &sym->declared_at);
10837 /* Reset so that the error only occurs once. */
10838 sym->attr.elemental = 0;
10839 return FAILURE;
10840 }
10841
10842 if (sym->attr.proc == PROC_ST_FUNCTION
10843 && (sym->attr.allocatable || sym->attr.pointer))
10844 {
10845 gfc_error ("Statement function '%s' at %L may not have pointer or "
10846 "allocatable attribute", sym->name, &sym->declared_at);
10847 return FAILURE;
10848 }
10849
10850 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10851 char-len-param shall not be array-valued, pointer-valued, recursive
10852 or pure. ....snip... A character value of * may only be used in the
10853 following ways: (i) Dummy arg of procedure - dummy associates with
10854 actual length; (ii) To declare a named constant; or (iii) External
10855 function - but length must be declared in calling scoping unit. */
10856 if (sym->attr.function
10857 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
10858 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10859 {
10860 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10861 || (sym->attr.recursive) || (sym->attr.pure))
10862 {
10863 if (sym->as && sym->as->rank)
10864 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10865 "array-valued", sym->name, &sym->declared_at);
10866
10867 if (sym->attr.pointer)
10868 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10869 "pointer-valued", sym->name, &sym->declared_at);
10870
10871 if (sym->attr.pure)
10872 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10873 "pure", sym->name, &sym->declared_at);
10874
10875 if (sym->attr.recursive)
10876 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10877 "recursive", sym->name, &sym->declared_at);
10878
10879 return FAILURE;
10880 }
10881
10882 /* Appendix B.2 of the standard. Contained functions give an
10883 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10884 character length is an F2003 feature. */
10885 if (!sym->attr.contained
10886 && gfc_current_form != FORM_FIXED
10887 && !sym->ts.deferred)
10888 gfc_notify_std (GFC_STD_F95_OBS,
10889 "CHARACTER(*) function '%s' at %L",
10890 sym->name, &sym->declared_at);
10891 }
10892
10893 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10894 {
10895 gfc_formal_arglist *curr_arg;
10896 int has_non_interop_arg = 0;
10897
10898 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10899 sym->common_block) == FAILURE)
10900 {
10901 /* Clear these to prevent looking at them again if there was an
10902 error. */
10903 sym->attr.is_bind_c = 0;
10904 sym->attr.is_c_interop = 0;
10905 sym->ts.is_c_interop = 0;
10906 }
10907 else
10908 {
10909 /* So far, no errors have been found. */
10910 sym->attr.is_c_interop = 1;
10911 sym->ts.is_c_interop = 1;
10912 }
10913
10914 curr_arg = sym->formal;
10915 while (curr_arg != NULL)
10916 {
10917 /* Skip implicitly typed dummy args here. */
10918 if (curr_arg->sym->attr.implicit_type == 0)
10919 if (gfc_verify_c_interop_param (curr_arg->sym) == FAILURE)
10920 /* If something is found to fail, record the fact so we
10921 can mark the symbol for the procedure as not being
10922 BIND(C) to try and prevent multiple errors being
10923 reported. */
10924 has_non_interop_arg = 1;
10925
10926 curr_arg = curr_arg->next;
10927 }
10928
10929 /* See if any of the arguments were not interoperable and if so, clear
10930 the procedure symbol to prevent duplicate error messages. */
10931 if (has_non_interop_arg != 0)
10932 {
10933 sym->attr.is_c_interop = 0;
10934 sym->ts.is_c_interop = 0;
10935 sym->attr.is_bind_c = 0;
10936 }
10937 }
10938
10939 if (!sym->attr.proc_pointer)
10940 {
10941 if (sym->attr.save == SAVE_EXPLICIT)
10942 {
10943 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10944 "in '%s' at %L", sym->name, &sym->declared_at);
10945 return FAILURE;
10946 }
10947 if (sym->attr.intent)
10948 {
10949 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10950 "in '%s' at %L", sym->name, &sym->declared_at);
10951 return FAILURE;
10952 }
10953 if (sym->attr.subroutine && sym->attr.result)
10954 {
10955 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10956 "in '%s' at %L", sym->name, &sym->declared_at);
10957 return FAILURE;
10958 }
10959 if (sym->attr.external && sym->attr.function
10960 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10961 || sym->attr.contained))
10962 {
10963 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10964 "in '%s' at %L", sym->name, &sym->declared_at);
10965 return FAILURE;
10966 }
10967 if (strcmp ("ppr@", sym->name) == 0)
10968 {
10969 gfc_error ("Procedure pointer result '%s' at %L "
10970 "is missing the pointer attribute",
10971 sym->ns->proc_name->name, &sym->declared_at);
10972 return FAILURE;
10973 }
10974 }
10975
10976 return SUCCESS;
10977 }
10978
10979
10980 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10981 been defined and we now know their defined arguments, check that they fulfill
10982 the requirements of the standard for procedures used as finalizers. */
10983
10984 static gfc_try
10985 gfc_resolve_finalizers (gfc_symbol* derived)
10986 {
10987 gfc_finalizer* list;
10988 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10989 gfc_try result = SUCCESS;
10990 bool seen_scalar = false;
10991
10992 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10993 return SUCCESS;
10994
10995 /* Walk over the list of finalizer-procedures, check them, and if any one
10996 does not fit in with the standard's definition, print an error and remove
10997 it from the list. */
10998 prev_link = &derived->f2k_derived->finalizers;
10999 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11000 {
11001 gfc_symbol* arg;
11002 gfc_finalizer* i;
11003 int my_rank;
11004
11005 /* Skip this finalizer if we already resolved it. */
11006 if (list->proc_tree)
11007 {
11008 prev_link = &(list->next);
11009 continue;
11010 }
11011
11012 /* Check this exists and is a SUBROUTINE. */
11013 if (!list->proc_sym->attr.subroutine)
11014 {
11015 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11016 list->proc_sym->name, &list->where);
11017 goto error;
11018 }
11019
11020 /* We should have exactly one argument. */
11021 if (!list->proc_sym->formal || list->proc_sym->formal->next)
11022 {
11023 gfc_error ("FINAL procedure at %L must have exactly one argument",
11024 &list->where);
11025 goto error;
11026 }
11027 arg = list->proc_sym->formal->sym;
11028
11029 /* This argument must be of our type. */
11030 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11031 {
11032 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11033 &arg->declared_at, derived->name);
11034 goto error;
11035 }
11036
11037 /* It must neither be a pointer nor allocatable nor optional. */
11038 if (arg->attr.pointer)
11039 {
11040 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11041 &arg->declared_at);
11042 goto error;
11043 }
11044 if (arg->attr.allocatable)
11045 {
11046 gfc_error ("Argument of FINAL procedure at %L must not be"
11047 " ALLOCATABLE", &arg->declared_at);
11048 goto error;
11049 }
11050 if (arg->attr.optional)
11051 {
11052 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11053 &arg->declared_at);
11054 goto error;
11055 }
11056
11057 /* It must not be INTENT(OUT). */
11058 if (arg->attr.intent == INTENT_OUT)
11059 {
11060 gfc_error ("Argument of FINAL procedure at %L must not be"
11061 " INTENT(OUT)", &arg->declared_at);
11062 goto error;
11063 }
11064
11065 /* Warn if the procedure is non-scalar and not assumed shape. */
11066 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11067 && arg->as->type != AS_ASSUMED_SHAPE)
11068 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11069 " shape argument", &arg->declared_at);
11070
11071 /* Check that it does not match in kind and rank with a FINAL procedure
11072 defined earlier. To really loop over the *earlier* declarations,
11073 we need to walk the tail of the list as new ones were pushed at the
11074 front. */
11075 /* TODO: Handle kind parameters once they are implemented. */
11076 my_rank = (arg->as ? arg->as->rank : 0);
11077 for (i = list->next; i; i = i->next)
11078 {
11079 /* Argument list might be empty; that is an error signalled earlier,
11080 but we nevertheless continued resolving. */
11081 if (i->proc_sym->formal)
11082 {
11083 gfc_symbol* i_arg = i->proc_sym->formal->sym;
11084 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11085 if (i_rank == my_rank)
11086 {
11087 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11088 " rank (%d) as '%s'",
11089 list->proc_sym->name, &list->where, my_rank,
11090 i->proc_sym->name);
11091 goto error;
11092 }
11093 }
11094 }
11095
11096 /* Is this the/a scalar finalizer procedure? */
11097 if (!arg->as || arg->as->rank == 0)
11098 seen_scalar = true;
11099
11100 /* Find the symtree for this procedure. */
11101 gcc_assert (!list->proc_tree);
11102 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11103
11104 prev_link = &list->next;
11105 continue;
11106
11107 /* Remove wrong nodes immediately from the list so we don't risk any
11108 troubles in the future when they might fail later expectations. */
11109 error:
11110 result = FAILURE;
11111 i = list;
11112 *prev_link = list->next;
11113 gfc_free_finalizer (i);
11114 }
11115
11116 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11117 were nodes in the list, must have been for arrays. It is surely a good
11118 idea to have a scalar version there if there's something to finalize. */
11119 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
11120 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11121 " defined at %L, suggest also scalar one",
11122 derived->name, &derived->declared_at);
11123
11124 /* TODO: Remove this error when finalization is finished. */
11125 gfc_error ("Finalization at %L is not yet implemented",
11126 &derived->declared_at);
11127
11128 return result;
11129 }
11130
11131
11132 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11133
11134 static gfc_try
11135 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11136 const char* generic_name, locus where)
11137 {
11138 gfc_symbol *sym1, *sym2;
11139 const char *pass1, *pass2;
11140
11141 gcc_assert (t1->specific && t2->specific);
11142 gcc_assert (!t1->specific->is_generic);
11143 gcc_assert (!t2->specific->is_generic);
11144 gcc_assert (t1->is_operator == t2->is_operator);
11145
11146 sym1 = t1->specific->u.specific->n.sym;
11147 sym2 = t2->specific->u.specific->n.sym;
11148
11149 if (sym1 == sym2)
11150 return SUCCESS;
11151
11152 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11153 if (sym1->attr.subroutine != sym2->attr.subroutine
11154 || sym1->attr.function != sym2->attr.function)
11155 {
11156 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11157 " GENERIC '%s' at %L",
11158 sym1->name, sym2->name, generic_name, &where);
11159 return FAILURE;
11160 }
11161
11162 /* Compare the interfaces. */
11163 if (t1->specific->nopass)
11164 pass1 = NULL;
11165 else if (t1->specific->pass_arg)
11166 pass1 = t1->specific->pass_arg;
11167 else
11168 pass1 = t1->specific->u.specific->n.sym->formal->sym->name;
11169 if (t2->specific->nopass)
11170 pass2 = NULL;
11171 else if (t2->specific->pass_arg)
11172 pass2 = t2->specific->pass_arg;
11173 else
11174 pass2 = t2->specific->u.specific->n.sym->formal->sym->name;
11175 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11176 NULL, 0, pass1, pass2))
11177 {
11178 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11179 sym1->name, sym2->name, generic_name, &where);
11180 return FAILURE;
11181 }
11182
11183 return SUCCESS;
11184 }
11185
11186
11187 /* Worker function for resolving a generic procedure binding; this is used to
11188 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11189
11190 The difference between those cases is finding possible inherited bindings
11191 that are overridden, as one has to look for them in tb_sym_root,
11192 tb_uop_root or tb_op, respectively. Thus the caller must already find
11193 the super-type and set p->overridden correctly. */
11194
11195 static gfc_try
11196 resolve_tb_generic_targets (gfc_symbol* super_type,
11197 gfc_typebound_proc* p, const char* name)
11198 {
11199 gfc_tbp_generic* target;
11200 gfc_symtree* first_target;
11201 gfc_symtree* inherited;
11202
11203 gcc_assert (p && p->is_generic);
11204
11205 /* Try to find the specific bindings for the symtrees in our target-list. */
11206 gcc_assert (p->u.generic);
11207 for (target = p->u.generic; target; target = target->next)
11208 if (!target->specific)
11209 {
11210 gfc_typebound_proc* overridden_tbp;
11211 gfc_tbp_generic* g;
11212 const char* target_name;
11213
11214 target_name = target->specific_st->name;
11215
11216 /* Defined for this type directly. */
11217 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11218 {
11219 target->specific = target->specific_st->n.tb;
11220 goto specific_found;
11221 }
11222
11223 /* Look for an inherited specific binding. */
11224 if (super_type)
11225 {
11226 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11227 true, NULL);
11228
11229 if (inherited)
11230 {
11231 gcc_assert (inherited->n.tb);
11232 target->specific = inherited->n.tb;
11233 goto specific_found;
11234 }
11235 }
11236
11237 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11238 " at %L", target_name, name, &p->where);
11239 return FAILURE;
11240
11241 /* Once we've found the specific binding, check it is not ambiguous with
11242 other specifics already found or inherited for the same GENERIC. */
11243 specific_found:
11244 gcc_assert (target->specific);
11245
11246 /* This must really be a specific binding! */
11247 if (target->specific->is_generic)
11248 {
11249 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11250 " '%s' is GENERIC, too", name, &p->where, target_name);
11251 return FAILURE;
11252 }
11253
11254 /* Check those already resolved on this type directly. */
11255 for (g = p->u.generic; g; g = g->next)
11256 if (g != target && g->specific
11257 && check_generic_tbp_ambiguity (target, g, name, p->where)
11258 == FAILURE)
11259 return FAILURE;
11260
11261 /* Check for ambiguity with inherited specific targets. */
11262 for (overridden_tbp = p->overridden; overridden_tbp;
11263 overridden_tbp = overridden_tbp->overridden)
11264 if (overridden_tbp->is_generic)
11265 {
11266 for (g = overridden_tbp->u.generic; g; g = g->next)
11267 {
11268 gcc_assert (g->specific);
11269 if (check_generic_tbp_ambiguity (target, g,
11270 name, p->where) == FAILURE)
11271 return FAILURE;
11272 }
11273 }
11274 }
11275
11276 /* If we attempt to "overwrite" a specific binding, this is an error. */
11277 if (p->overridden && !p->overridden->is_generic)
11278 {
11279 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11280 " the same name", name, &p->where);
11281 return FAILURE;
11282 }
11283
11284 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11285 all must have the same attributes here. */
11286 first_target = p->u.generic->specific->u.specific;
11287 gcc_assert (first_target);
11288 p->subroutine = first_target->n.sym->attr.subroutine;
11289 p->function = first_target->n.sym->attr.function;
11290
11291 return SUCCESS;
11292 }
11293
11294
11295 /* Resolve a GENERIC procedure binding for a derived type. */
11296
11297 static gfc_try
11298 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11299 {
11300 gfc_symbol* super_type;
11301
11302 /* Find the overridden binding if any. */
11303 st->n.tb->overridden = NULL;
11304 super_type = gfc_get_derived_super_type (derived);
11305 if (super_type)
11306 {
11307 gfc_symtree* overridden;
11308 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11309 true, NULL);
11310
11311 if (overridden && overridden->n.tb)
11312 st->n.tb->overridden = overridden->n.tb;
11313 }
11314
11315 /* Resolve using worker function. */
11316 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11317 }
11318
11319
11320 /* Retrieve the target-procedure of an operator binding and do some checks in
11321 common for intrinsic and user-defined type-bound operators. */
11322
11323 static gfc_symbol*
11324 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11325 {
11326 gfc_symbol* target_proc;
11327
11328 gcc_assert (target->specific && !target->specific->is_generic);
11329 target_proc = target->specific->u.specific->n.sym;
11330 gcc_assert (target_proc);
11331
11332 /* All operator bindings must have a passed-object dummy argument. */
11333 if (target->specific->nopass)
11334 {
11335 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11336 return NULL;
11337 }
11338
11339 return target_proc;
11340 }
11341
11342
11343 /* Resolve a type-bound intrinsic operator. */
11344
11345 static gfc_try
11346 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11347 gfc_typebound_proc* p)
11348 {
11349 gfc_symbol* super_type;
11350 gfc_tbp_generic* target;
11351
11352 /* If there's already an error here, do nothing (but don't fail again). */
11353 if (p->error)
11354 return SUCCESS;
11355
11356 /* Operators should always be GENERIC bindings. */
11357 gcc_assert (p->is_generic);
11358
11359 /* Look for an overridden binding. */
11360 super_type = gfc_get_derived_super_type (derived);
11361 if (super_type && super_type->f2k_derived)
11362 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11363 op, true, NULL);
11364 else
11365 p->overridden = NULL;
11366
11367 /* Resolve general GENERIC properties using worker function. */
11368 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
11369 goto error;
11370
11371 /* Check the targets to be procedures of correct interface. */
11372 for (target = p->u.generic; target; target = target->next)
11373 {
11374 gfc_symbol* target_proc;
11375
11376 target_proc = get_checked_tb_operator_target (target, p->where);
11377 if (!target_proc)
11378 goto error;
11379
11380 if (!gfc_check_operator_interface (target_proc, op, p->where))
11381 goto error;
11382
11383 /* Add target to non-typebound operator list. */
11384 if (!target->specific->deferred && !derived->attr.use_assoc
11385 && p->access != ACCESS_PRIVATE)
11386 {
11387 gfc_interface *head, *intr;
11388 if (gfc_check_new_interface (derived->ns->op[op], target_proc,
11389 p->where) == FAILURE)
11390 return FAILURE;
11391 head = derived->ns->op[op];
11392 intr = gfc_get_interface ();
11393 intr->sym = target_proc;
11394 intr->where = p->where;
11395 intr->next = head;
11396 derived->ns->op[op] = intr;
11397 }
11398 }
11399
11400 return SUCCESS;
11401
11402 error:
11403 p->error = 1;
11404 return FAILURE;
11405 }
11406
11407
11408 /* Resolve a type-bound user operator (tree-walker callback). */
11409
11410 static gfc_symbol* resolve_bindings_derived;
11411 static gfc_try resolve_bindings_result;
11412
11413 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
11414
11415 static void
11416 resolve_typebound_user_op (gfc_symtree* stree)
11417 {
11418 gfc_symbol* super_type;
11419 gfc_tbp_generic* target;
11420
11421 gcc_assert (stree && stree->n.tb);
11422
11423 if (stree->n.tb->error)
11424 return;
11425
11426 /* Operators should always be GENERIC bindings. */
11427 gcc_assert (stree->n.tb->is_generic);
11428
11429 /* Find overridden procedure, if any. */
11430 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11431 if (super_type && super_type->f2k_derived)
11432 {
11433 gfc_symtree* overridden;
11434 overridden = gfc_find_typebound_user_op (super_type, NULL,
11435 stree->name, true, NULL);
11436
11437 if (overridden && overridden->n.tb)
11438 stree->n.tb->overridden = overridden->n.tb;
11439 }
11440 else
11441 stree->n.tb->overridden = NULL;
11442
11443 /* Resolve basically using worker function. */
11444 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
11445 == FAILURE)
11446 goto error;
11447
11448 /* Check the targets to be functions of correct interface. */
11449 for (target = stree->n.tb->u.generic; target; target = target->next)
11450 {
11451 gfc_symbol* target_proc;
11452
11453 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11454 if (!target_proc)
11455 goto error;
11456
11457 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
11458 goto error;
11459 }
11460
11461 return;
11462
11463 error:
11464 resolve_bindings_result = FAILURE;
11465 stree->n.tb->error = 1;
11466 }
11467
11468
11469 /* Resolve the type-bound procedures for a derived type. */
11470
11471 static void
11472 resolve_typebound_procedure (gfc_symtree* stree)
11473 {
11474 gfc_symbol* proc;
11475 locus where;
11476 gfc_symbol* me_arg;
11477 gfc_symbol* super_type;
11478 gfc_component* comp;
11479
11480 gcc_assert (stree);
11481
11482 /* Undefined specific symbol from GENERIC target definition. */
11483 if (!stree->n.tb)
11484 return;
11485
11486 if (stree->n.tb->error)
11487 return;
11488
11489 /* If this is a GENERIC binding, use that routine. */
11490 if (stree->n.tb->is_generic)
11491 {
11492 if (resolve_typebound_generic (resolve_bindings_derived, stree)
11493 == FAILURE)
11494 goto error;
11495 return;
11496 }
11497
11498 /* Get the target-procedure to check it. */
11499 gcc_assert (!stree->n.tb->is_generic);
11500 gcc_assert (stree->n.tb->u.specific);
11501 proc = stree->n.tb->u.specific->n.sym;
11502 where = stree->n.tb->where;
11503 proc->attr.public_used = 1;
11504
11505 /* Default access should already be resolved from the parser. */
11506 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11507
11508 /* It should be a module procedure or an external procedure with explicit
11509 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11510 if ((!proc->attr.subroutine && !proc->attr.function)
11511 || (proc->attr.proc != PROC_MODULE
11512 && proc->attr.if_source != IFSRC_IFBODY)
11513 || (proc->attr.abstract && !stree->n.tb->deferred))
11514 {
11515 gfc_error ("'%s' must be a module procedure or an external procedure with"
11516 " an explicit interface at %L", proc->name, &where);
11517 goto error;
11518 }
11519 stree->n.tb->subroutine = proc->attr.subroutine;
11520 stree->n.tb->function = proc->attr.function;
11521
11522 /* Find the super-type of the current derived type. We could do this once and
11523 store in a global if speed is needed, but as long as not I believe this is
11524 more readable and clearer. */
11525 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11526
11527 /* If PASS, resolve and check arguments if not already resolved / loaded
11528 from a .mod file. */
11529 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11530 {
11531 if (stree->n.tb->pass_arg)
11532 {
11533 gfc_formal_arglist* i;
11534
11535 /* If an explicit passing argument name is given, walk the arg-list
11536 and look for it. */
11537
11538 me_arg = NULL;
11539 stree->n.tb->pass_arg_num = 1;
11540 for (i = proc->formal; i; i = i->next)
11541 {
11542 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11543 {
11544 me_arg = i->sym;
11545 break;
11546 }
11547 ++stree->n.tb->pass_arg_num;
11548 }
11549
11550 if (!me_arg)
11551 {
11552 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11553 " argument '%s'",
11554 proc->name, stree->n.tb->pass_arg, &where,
11555 stree->n.tb->pass_arg);
11556 goto error;
11557 }
11558 }
11559 else
11560 {
11561 /* Otherwise, take the first one; there should in fact be at least
11562 one. */
11563 stree->n.tb->pass_arg_num = 1;
11564 if (!proc->formal)
11565 {
11566 gfc_error ("Procedure '%s' with PASS at %L must have at"
11567 " least one argument", proc->name, &where);
11568 goto error;
11569 }
11570 me_arg = proc->formal->sym;
11571 }
11572
11573 /* Now check that the argument-type matches and the passed-object
11574 dummy argument is generally fine. */
11575
11576 gcc_assert (me_arg);
11577
11578 if (me_arg->ts.type != BT_CLASS)
11579 {
11580 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11581 " at %L", proc->name, &where);
11582 goto error;
11583 }
11584
11585 if (CLASS_DATA (me_arg)->ts.u.derived
11586 != resolve_bindings_derived)
11587 {
11588 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11589 " the derived-type '%s'", me_arg->name, proc->name,
11590 me_arg->name, &where, resolve_bindings_derived->name);
11591 goto error;
11592 }
11593
11594 gcc_assert (me_arg->ts.type == BT_CLASS);
11595 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11596 {
11597 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11598 " scalar", proc->name, &where);
11599 goto error;
11600 }
11601 if (CLASS_DATA (me_arg)->attr.allocatable)
11602 {
11603 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11604 " be ALLOCATABLE", proc->name, &where);
11605 goto error;
11606 }
11607 if (CLASS_DATA (me_arg)->attr.class_pointer)
11608 {
11609 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11610 " be POINTER", proc->name, &where);
11611 goto error;
11612 }
11613 }
11614
11615 /* If we are extending some type, check that we don't override a procedure
11616 flagged NON_OVERRIDABLE. */
11617 stree->n.tb->overridden = NULL;
11618 if (super_type)
11619 {
11620 gfc_symtree* overridden;
11621 overridden = gfc_find_typebound_proc (super_type, NULL,
11622 stree->name, true, NULL);
11623
11624 if (overridden)
11625 {
11626 if (overridden->n.tb)
11627 stree->n.tb->overridden = overridden->n.tb;
11628
11629 if (gfc_check_typebound_override (stree, overridden) == FAILURE)
11630 goto error;
11631 }
11632 }
11633
11634 /* See if there's a name collision with a component directly in this type. */
11635 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11636 if (!strcmp (comp->name, stree->name))
11637 {
11638 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11639 " '%s'",
11640 stree->name, &where, resolve_bindings_derived->name);
11641 goto error;
11642 }
11643
11644 /* Try to find a name collision with an inherited component. */
11645 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11646 {
11647 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11648 " component of '%s'",
11649 stree->name, &where, resolve_bindings_derived->name);
11650 goto error;
11651 }
11652
11653 stree->n.tb->error = 0;
11654 return;
11655
11656 error:
11657 resolve_bindings_result = FAILURE;
11658 stree->n.tb->error = 1;
11659 }
11660
11661
11662 static gfc_try
11663 resolve_typebound_procedures (gfc_symbol* derived)
11664 {
11665 int op;
11666 gfc_symbol* super_type;
11667
11668 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11669 return SUCCESS;
11670
11671 super_type = gfc_get_derived_super_type (derived);
11672 if (super_type)
11673 resolve_typebound_procedures (super_type);
11674
11675 resolve_bindings_derived = derived;
11676 resolve_bindings_result = SUCCESS;
11677
11678 /* Make sure the vtab has been generated. */
11679 gfc_find_derived_vtab (derived);
11680
11681 if (derived->f2k_derived->tb_sym_root)
11682 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11683 &resolve_typebound_procedure);
11684
11685 if (derived->f2k_derived->tb_uop_root)
11686 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11687 &resolve_typebound_user_op);
11688
11689 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11690 {
11691 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11692 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11693 p) == FAILURE)
11694 resolve_bindings_result = FAILURE;
11695 }
11696
11697 return resolve_bindings_result;
11698 }
11699
11700
11701 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11702 to give all identical derived types the same backend_decl. */
11703 static void
11704 add_dt_to_dt_list (gfc_symbol *derived)
11705 {
11706 gfc_dt_list *dt_list;
11707
11708 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11709 if (derived == dt_list->derived)
11710 return;
11711
11712 dt_list = gfc_get_dt_list ();
11713 dt_list->next = gfc_derived_types;
11714 dt_list->derived = derived;
11715 gfc_derived_types = dt_list;
11716 }
11717
11718
11719 /* Ensure that a derived-type is really not abstract, meaning that every
11720 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11721
11722 static gfc_try
11723 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11724 {
11725 if (!st)
11726 return SUCCESS;
11727
11728 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11729 return FAILURE;
11730 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11731 return FAILURE;
11732
11733 if (st->n.tb && st->n.tb->deferred)
11734 {
11735 gfc_symtree* overriding;
11736 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11737 if (!overriding)
11738 return FAILURE;
11739 gcc_assert (overriding->n.tb);
11740 if (overriding->n.tb->deferred)
11741 {
11742 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11743 " '%s' is DEFERRED and not overridden",
11744 sub->name, &sub->declared_at, st->name);
11745 return FAILURE;
11746 }
11747 }
11748
11749 return SUCCESS;
11750 }
11751
11752 static gfc_try
11753 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11754 {
11755 /* The algorithm used here is to recursively travel up the ancestry of sub
11756 and for each ancestor-type, check all bindings. If any of them is
11757 DEFERRED, look it up starting from sub and see if the found (overriding)
11758 binding is not DEFERRED.
11759 This is not the most efficient way to do this, but it should be ok and is
11760 clearer than something sophisticated. */
11761
11762 gcc_assert (ancestor && !sub->attr.abstract);
11763
11764 if (!ancestor->attr.abstract)
11765 return SUCCESS;
11766
11767 /* Walk bindings of this ancestor. */
11768 if (ancestor->f2k_derived)
11769 {
11770 gfc_try t;
11771 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11772 if (t == FAILURE)
11773 return FAILURE;
11774 }
11775
11776 /* Find next ancestor type and recurse on it. */
11777 ancestor = gfc_get_derived_super_type (ancestor);
11778 if (ancestor)
11779 return ensure_not_abstract (sub, ancestor);
11780
11781 return SUCCESS;
11782 }
11783
11784
11785 /* Resolve the components of a derived type. This does not have to wait until
11786 resolution stage, but can be done as soon as the dt declaration has been
11787 parsed. */
11788
11789 static gfc_try
11790 resolve_fl_derived0 (gfc_symbol *sym)
11791 {
11792 gfc_symbol* super_type;
11793 gfc_component *c;
11794
11795 super_type = gfc_get_derived_super_type (sym);
11796
11797 /* F2008, C432. */
11798 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11799 {
11800 gfc_error ("As extending type '%s' at %L has a coarray component, "
11801 "parent type '%s' shall also have one", sym->name,
11802 &sym->declared_at, super_type->name);
11803 return FAILURE;
11804 }
11805
11806 /* Ensure the extended type gets resolved before we do. */
11807 if (super_type && resolve_fl_derived0 (super_type) == FAILURE)
11808 return FAILURE;
11809
11810 /* An ABSTRACT type must be extensible. */
11811 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11812 {
11813 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11814 sym->name, &sym->declared_at);
11815 return FAILURE;
11816 }
11817
11818 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
11819 : sym->components;
11820
11821 for ( ; c != NULL; c = c->next)
11822 {
11823 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
11824 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
11825 {
11826 gfc_error ("Deferred-length character component '%s' at %L is not "
11827 "yet supported", c->name, &c->loc);
11828 return FAILURE;
11829 }
11830
11831 /* F2008, C442. */
11832 if ((!sym->attr.is_class || c != sym->components)
11833 && c->attr.codimension
11834 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11835 {
11836 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11837 "deferred shape", c->name, &c->loc);
11838 return FAILURE;
11839 }
11840
11841 /* F2008, C443. */
11842 if (c->attr.codimension && c->ts.type == BT_DERIVED
11843 && c->ts.u.derived->ts.is_iso_c)
11844 {
11845 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11846 "shall not be a coarray", c->name, &c->loc);
11847 return FAILURE;
11848 }
11849
11850 /* F2008, C444. */
11851 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11852 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11853 || c->attr.allocatable))
11854 {
11855 gfc_error ("Component '%s' at %L with coarray component "
11856 "shall be a nonpointer, nonallocatable scalar",
11857 c->name, &c->loc);
11858 return FAILURE;
11859 }
11860
11861 /* F2008, C448. */
11862 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11863 {
11864 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11865 "is not an array pointer", c->name, &c->loc);
11866 return FAILURE;
11867 }
11868
11869 if (c->attr.proc_pointer && c->ts.interface)
11870 {
11871 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11872 gfc_error ("Interface '%s', used by procedure pointer component "
11873 "'%s' at %L, is declared in a later PROCEDURE statement",
11874 c->ts.interface->name, c->name, &c->loc);
11875
11876 /* Get the attributes from the interface (now resolved). */
11877 if (c->ts.interface->attr.if_source
11878 || c->ts.interface->attr.intrinsic)
11879 {
11880 gfc_symbol *ifc = c->ts.interface;
11881
11882 if (ifc->formal && !ifc->formal_ns)
11883 resolve_symbol (ifc);
11884
11885 if (ifc->attr.intrinsic)
11886 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
11887
11888 if (ifc->result)
11889 {
11890 c->ts = ifc->result->ts;
11891 c->attr.allocatable = ifc->result->attr.allocatable;
11892 c->attr.pointer = ifc->result->attr.pointer;
11893 c->attr.dimension = ifc->result->attr.dimension;
11894 c->as = gfc_copy_array_spec (ifc->result->as);
11895 }
11896 else
11897 {
11898 c->ts = ifc->ts;
11899 c->attr.allocatable = ifc->attr.allocatable;
11900 c->attr.pointer = ifc->attr.pointer;
11901 c->attr.dimension = ifc->attr.dimension;
11902 c->as = gfc_copy_array_spec (ifc->as);
11903 }
11904 c->ts.interface = ifc;
11905 c->attr.function = ifc->attr.function;
11906 c->attr.subroutine = ifc->attr.subroutine;
11907 gfc_copy_formal_args_ppc (c, ifc, IFSRC_DECL);
11908
11909 c->attr.pure = ifc->attr.pure;
11910 c->attr.elemental = ifc->attr.elemental;
11911 c->attr.recursive = ifc->attr.recursive;
11912 c->attr.always_explicit = ifc->attr.always_explicit;
11913 c->attr.ext_attr |= ifc->attr.ext_attr;
11914 /* Replace symbols in array spec. */
11915 if (c->as)
11916 {
11917 int i;
11918 for (i = 0; i < c->as->rank; i++)
11919 {
11920 gfc_expr_replace_comp (c->as->lower[i], c);
11921 gfc_expr_replace_comp (c->as->upper[i], c);
11922 }
11923 }
11924 /* Copy char length. */
11925 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11926 {
11927 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11928 gfc_expr_replace_comp (cl->length, c);
11929 if (cl->length && !cl->resolved
11930 && gfc_resolve_expr (cl->length) == FAILURE)
11931 return FAILURE;
11932 c->ts.u.cl = cl;
11933 }
11934 }
11935 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11936 {
11937 gfc_error ("Interface '%s' of procedure pointer component "
11938 "'%s' at %L must be explicit", c->ts.interface->name,
11939 c->name, &c->loc);
11940 return FAILURE;
11941 }
11942 }
11943 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11944 {
11945 /* Since PPCs are not implicitly typed, a PPC without an explicit
11946 interface must be a subroutine. */
11947 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11948 }
11949
11950 /* Procedure pointer components: Check PASS arg. */
11951 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11952 && !sym->attr.vtype)
11953 {
11954 gfc_symbol* me_arg;
11955
11956 if (c->tb->pass_arg)
11957 {
11958 gfc_formal_arglist* i;
11959
11960 /* If an explicit passing argument name is given, walk the arg-list
11961 and look for it. */
11962
11963 me_arg = NULL;
11964 c->tb->pass_arg_num = 1;
11965 for (i = c->formal; i; i = i->next)
11966 {
11967 if (!strcmp (i->sym->name, c->tb->pass_arg))
11968 {
11969 me_arg = i->sym;
11970 break;
11971 }
11972 c->tb->pass_arg_num++;
11973 }
11974
11975 if (!me_arg)
11976 {
11977 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11978 "at %L has no argument '%s'", c->name,
11979 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11980 c->tb->error = 1;
11981 return FAILURE;
11982 }
11983 }
11984 else
11985 {
11986 /* Otherwise, take the first one; there should in fact be at least
11987 one. */
11988 c->tb->pass_arg_num = 1;
11989 if (!c->formal)
11990 {
11991 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11992 "must have at least one argument",
11993 c->name, &c->loc);
11994 c->tb->error = 1;
11995 return FAILURE;
11996 }
11997 me_arg = c->formal->sym;
11998 }
11999
12000 /* Now check that the argument-type matches. */
12001 gcc_assert (me_arg);
12002 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12003 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12004 || (me_arg->ts.type == BT_CLASS
12005 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12006 {
12007 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12008 " the derived type '%s'", me_arg->name, c->name,
12009 me_arg->name, &c->loc, sym->name);
12010 c->tb->error = 1;
12011 return FAILURE;
12012 }
12013
12014 /* Check for C453. */
12015 if (me_arg->attr.dimension)
12016 {
12017 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12018 "must be scalar", me_arg->name, c->name, me_arg->name,
12019 &c->loc);
12020 c->tb->error = 1;
12021 return FAILURE;
12022 }
12023
12024 if (me_arg->attr.pointer)
12025 {
12026 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12027 "may not have the POINTER attribute", me_arg->name,
12028 c->name, me_arg->name, &c->loc);
12029 c->tb->error = 1;
12030 return FAILURE;
12031 }
12032
12033 if (me_arg->attr.allocatable)
12034 {
12035 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12036 "may not be ALLOCATABLE", me_arg->name, c->name,
12037 me_arg->name, &c->loc);
12038 c->tb->error = 1;
12039 return FAILURE;
12040 }
12041
12042 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12043 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12044 " at %L", c->name, &c->loc);
12045
12046 }
12047
12048 /* Check type-spec if this is not the parent-type component. */
12049 if (((sym->attr.is_class
12050 && (!sym->components->ts.u.derived->attr.extension
12051 || c != sym->components->ts.u.derived->components))
12052 || (!sym->attr.is_class
12053 && (!sym->attr.extension || c != sym->components)))
12054 && !sym->attr.vtype
12055 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
12056 return FAILURE;
12057
12058 /* If this type is an extension, set the accessibility of the parent
12059 component. */
12060 if (super_type
12061 && ((sym->attr.is_class
12062 && c == sym->components->ts.u.derived->components)
12063 || (!sym->attr.is_class && c == sym->components))
12064 && strcmp (super_type->name, c->name) == 0)
12065 c->attr.access = super_type->attr.access;
12066
12067 /* If this type is an extension, see if this component has the same name
12068 as an inherited type-bound procedure. */
12069 if (super_type && !sym->attr.is_class
12070 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12071 {
12072 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12073 " inherited type-bound procedure",
12074 c->name, sym->name, &c->loc);
12075 return FAILURE;
12076 }
12077
12078 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12079 && !c->ts.deferred)
12080 {
12081 if (c->ts.u.cl->length == NULL
12082 || (resolve_charlen (c->ts.u.cl) == FAILURE)
12083 || !gfc_is_constant_expr (c->ts.u.cl->length))
12084 {
12085 gfc_error ("Character length of component '%s' needs to "
12086 "be a constant specification expression at %L",
12087 c->name,
12088 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12089 return FAILURE;
12090 }
12091 }
12092
12093 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12094 && !c->attr.pointer && !c->attr.allocatable)
12095 {
12096 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12097 "length must be a POINTER or ALLOCATABLE",
12098 c->name, sym->name, &c->loc);
12099 return FAILURE;
12100 }
12101
12102 if (c->ts.type == BT_DERIVED
12103 && sym->component_access != ACCESS_PRIVATE
12104 && gfc_check_symbol_access (sym)
12105 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12106 && !c->ts.u.derived->attr.use_assoc
12107 && !gfc_check_symbol_access (c->ts.u.derived)
12108 && gfc_notify_std (GFC_STD_F2003, "the component '%s' "
12109 "is a PRIVATE type and cannot be a component of "
12110 "'%s', which is PUBLIC at %L", c->name,
12111 sym->name, &sym->declared_at) == FAILURE)
12112 return FAILURE;
12113
12114 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12115 {
12116 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12117 "type %s", c->name, &c->loc, sym->name);
12118 return FAILURE;
12119 }
12120
12121 if (sym->attr.sequence)
12122 {
12123 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12124 {
12125 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12126 "not have the SEQUENCE attribute",
12127 c->ts.u.derived->name, &sym->declared_at);
12128 return FAILURE;
12129 }
12130 }
12131
12132 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12133 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12134 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12135 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12136 CLASS_DATA (c)->ts.u.derived
12137 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12138
12139 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12140 && c->attr.pointer && c->ts.u.derived->components == NULL
12141 && !c->ts.u.derived->attr.zero_comp)
12142 {
12143 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12144 "that has not been declared", c->name, sym->name,
12145 &c->loc);
12146 return FAILURE;
12147 }
12148
12149 if (c->ts.type == BT_CLASS && c->attr.class_ok
12150 && CLASS_DATA (c)->attr.class_pointer
12151 && CLASS_DATA (c)->ts.u.derived->components == NULL
12152 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
12153 {
12154 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12155 "that has not been declared", c->name, sym->name,
12156 &c->loc);
12157 return FAILURE;
12158 }
12159
12160 /* C437. */
12161 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12162 && (!c->attr.class_ok
12163 || !(CLASS_DATA (c)->attr.class_pointer
12164 || CLASS_DATA (c)->attr.allocatable)))
12165 {
12166 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12167 "or pointer", c->name, &c->loc);
12168 return FAILURE;
12169 }
12170
12171 /* Ensure that all the derived type components are put on the
12172 derived type list; even in formal namespaces, where derived type
12173 pointer components might not have been declared. */
12174 if (c->ts.type == BT_DERIVED
12175 && c->ts.u.derived
12176 && c->ts.u.derived->components
12177 && c->attr.pointer
12178 && sym != c->ts.u.derived)
12179 add_dt_to_dt_list (c->ts.u.derived);
12180
12181 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
12182 || c->attr.proc_pointer
12183 || c->attr.allocatable)) == FAILURE)
12184 return FAILURE;
12185 }
12186
12187 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12188 all DEFERRED bindings are overridden. */
12189 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12190 && !sym->attr.is_class
12191 && ensure_not_abstract (sym, super_type) == FAILURE)
12192 return FAILURE;
12193
12194 /* Add derived type to the derived type list. */
12195 add_dt_to_dt_list (sym);
12196
12197 return SUCCESS;
12198 }
12199
12200
12201 /* The following procedure does the full resolution of a derived type,
12202 including resolution of all type-bound procedures (if present). In contrast
12203 to 'resolve_fl_derived0' this can only be done after the module has been
12204 parsed completely. */
12205
12206 static gfc_try
12207 resolve_fl_derived (gfc_symbol *sym)
12208 {
12209 gfc_symbol *gen_dt = NULL;
12210
12211 if (!sym->attr.is_class)
12212 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12213 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12214 && (!gen_dt->generic->sym->attr.use_assoc
12215 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12216 && gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of "
12217 "function '%s' at %L being the same name as derived "
12218 "type at %L", sym->name,
12219 gen_dt->generic->sym == sym
12220 ? gen_dt->generic->next->sym->name
12221 : gen_dt->generic->sym->name,
12222 gen_dt->generic->sym == sym
12223 ? &gen_dt->generic->next->sym->declared_at
12224 : &gen_dt->generic->sym->declared_at,
12225 &sym->declared_at) == FAILURE)
12226 return FAILURE;
12227
12228 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12229 {
12230 /* Fix up incomplete CLASS symbols. */
12231 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12232 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12233 if (vptr->ts.u.derived == NULL)
12234 {
12235 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12236 gcc_assert (vtab);
12237 vptr->ts.u.derived = vtab->ts.u.derived;
12238 }
12239 }
12240
12241 if (resolve_fl_derived0 (sym) == FAILURE)
12242 return FAILURE;
12243
12244 /* Resolve the type-bound procedures. */
12245 if (resolve_typebound_procedures (sym) == FAILURE)
12246 return FAILURE;
12247
12248 /* Resolve the finalizer procedures. */
12249 if (gfc_resolve_finalizers (sym) == FAILURE)
12250 return FAILURE;
12251
12252 return SUCCESS;
12253 }
12254
12255
12256 static gfc_try
12257 resolve_fl_namelist (gfc_symbol *sym)
12258 {
12259 gfc_namelist *nl;
12260 gfc_symbol *nlsym;
12261
12262 for (nl = sym->namelist; nl; nl = nl->next)
12263 {
12264 /* Check again, the check in match only works if NAMELIST comes
12265 after the decl. */
12266 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12267 {
12268 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12269 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12270 return FAILURE;
12271 }
12272
12273 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12274 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array "
12275 "object '%s' with assumed shape in namelist "
12276 "'%s' at %L", nl->sym->name, sym->name,
12277 &sym->declared_at) == FAILURE)
12278 return FAILURE;
12279
12280 if (is_non_constant_shape_array (nl->sym)
12281 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array "
12282 "object '%s' with nonconstant shape in namelist "
12283 "'%s' at %L", nl->sym->name, sym->name,
12284 &sym->declared_at) == FAILURE)
12285 return FAILURE;
12286
12287 if (nl->sym->ts.type == BT_CHARACTER
12288 && (nl->sym->ts.u.cl->length == NULL
12289 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12290 && gfc_notify_std (GFC_STD_F2003, "NAMELIST object "
12291 "'%s' with nonconstant character length in "
12292 "namelist '%s' at %L", nl->sym->name, sym->name,
12293 &sym->declared_at) == FAILURE)
12294 return FAILURE;
12295
12296 /* FIXME: Once UDDTIO is implemented, the following can be
12297 removed. */
12298 if (nl->sym->ts.type == BT_CLASS)
12299 {
12300 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12301 "polymorphic and requires a defined input/output "
12302 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12303 return FAILURE;
12304 }
12305
12306 if (nl->sym->ts.type == BT_DERIVED
12307 && (nl->sym->ts.u.derived->attr.alloc_comp
12308 || nl->sym->ts.u.derived->attr.pointer_comp))
12309 {
12310 if (gfc_notify_std (GFC_STD_F2003, "NAMELIST object "
12311 "'%s' in namelist '%s' at %L with ALLOCATABLE "
12312 "or POINTER components", nl->sym->name,
12313 sym->name, &sym->declared_at) == FAILURE)
12314 return FAILURE;
12315
12316 /* FIXME: Once UDDTIO is implemented, the following can be
12317 removed. */
12318 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12319 "ALLOCATABLE or POINTER components and thus requires "
12320 "a defined input/output procedure", nl->sym->name,
12321 sym->name, &sym->declared_at);
12322 return FAILURE;
12323 }
12324 }
12325
12326 /* Reject PRIVATE objects in a PUBLIC namelist. */
12327 if (gfc_check_symbol_access (sym))
12328 {
12329 for (nl = sym->namelist; nl; nl = nl->next)
12330 {
12331 if (!nl->sym->attr.use_assoc
12332 && !is_sym_host_assoc (nl->sym, sym->ns)
12333 && !gfc_check_symbol_access (nl->sym))
12334 {
12335 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12336 "cannot be member of PUBLIC namelist '%s' at %L",
12337 nl->sym->name, sym->name, &sym->declared_at);
12338 return FAILURE;
12339 }
12340
12341 /* Types with private components that came here by USE-association. */
12342 if (nl->sym->ts.type == BT_DERIVED
12343 && derived_inaccessible (nl->sym->ts.u.derived))
12344 {
12345 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12346 "components and cannot be member of namelist '%s' at %L",
12347 nl->sym->name, sym->name, &sym->declared_at);
12348 return FAILURE;
12349 }
12350
12351 /* Types with private components that are defined in the same module. */
12352 if (nl->sym->ts.type == BT_DERIVED
12353 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12354 && nl->sym->ts.u.derived->attr.private_comp)
12355 {
12356 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12357 "cannot be a member of PUBLIC namelist '%s' at %L",
12358 nl->sym->name, sym->name, &sym->declared_at);
12359 return FAILURE;
12360 }
12361 }
12362 }
12363
12364
12365 /* 14.1.2 A module or internal procedure represent local entities
12366 of the same type as a namelist member and so are not allowed. */
12367 for (nl = sym->namelist; nl; nl = nl->next)
12368 {
12369 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12370 continue;
12371
12372 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12373 if ((nl->sym == sym->ns->proc_name)
12374 ||
12375 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12376 continue;
12377
12378 nlsym = NULL;
12379 if (nl->sym && nl->sym->name)
12380 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12381 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12382 {
12383 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12384 "attribute in '%s' at %L", nlsym->name,
12385 &sym->declared_at);
12386 return FAILURE;
12387 }
12388 }
12389
12390 return SUCCESS;
12391 }
12392
12393
12394 static gfc_try
12395 resolve_fl_parameter (gfc_symbol *sym)
12396 {
12397 /* A parameter array's shape needs to be constant. */
12398 if (sym->as != NULL
12399 && (sym->as->type == AS_DEFERRED
12400 || is_non_constant_shape_array (sym)))
12401 {
12402 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12403 "or of deferred shape", sym->name, &sym->declared_at);
12404 return FAILURE;
12405 }
12406
12407 /* Make sure a parameter that has been implicitly typed still
12408 matches the implicit type, since PARAMETER statements can precede
12409 IMPLICIT statements. */
12410 if (sym->attr.implicit_type
12411 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12412 sym->ns)))
12413 {
12414 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12415 "later IMPLICIT type", sym->name, &sym->declared_at);
12416 return FAILURE;
12417 }
12418
12419 /* Make sure the types of derived parameters are consistent. This
12420 type checking is deferred until resolution because the type may
12421 refer to a derived type from the host. */
12422 if (sym->ts.type == BT_DERIVED
12423 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12424 {
12425 gfc_error ("Incompatible derived type in PARAMETER at %L",
12426 &sym->value->where);
12427 return FAILURE;
12428 }
12429 return SUCCESS;
12430 }
12431
12432
12433 /* Do anything necessary to resolve a symbol. Right now, we just
12434 assume that an otherwise unknown symbol is a variable. This sort
12435 of thing commonly happens for symbols in module. */
12436
12437 static void
12438 resolve_symbol (gfc_symbol *sym)
12439 {
12440 int check_constant, mp_flag;
12441 gfc_symtree *symtree;
12442 gfc_symtree *this_symtree;
12443 gfc_namespace *ns;
12444 gfc_component *c;
12445 symbol_attribute class_attr;
12446 gfc_array_spec *as;
12447
12448 if (sym->attr.flavor == FL_UNKNOWN
12449 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12450 && !sym->attr.generic && !sym->attr.external
12451 && sym->attr.if_source == IFSRC_UNKNOWN))
12452 {
12453
12454 /* If we find that a flavorless symbol is an interface in one of the
12455 parent namespaces, find its symtree in this namespace, free the
12456 symbol and set the symtree to point to the interface symbol. */
12457 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12458 {
12459 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12460 if (symtree && (symtree->n.sym->generic ||
12461 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12462 && sym->ns->construct_entities)))
12463 {
12464 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12465 sym->name);
12466 gfc_release_symbol (sym);
12467 symtree->n.sym->refs++;
12468 this_symtree->n.sym = symtree->n.sym;
12469 return;
12470 }
12471 }
12472
12473 /* Otherwise give it a flavor according to such attributes as
12474 it has. */
12475 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12476 && sym->attr.intrinsic == 0)
12477 sym->attr.flavor = FL_VARIABLE;
12478 else if (sym->attr.flavor == FL_UNKNOWN)
12479 {
12480 sym->attr.flavor = FL_PROCEDURE;
12481 if (sym->attr.dimension)
12482 sym->attr.function = 1;
12483 }
12484 }
12485
12486 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12487 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12488
12489 if (sym->attr.procedure && sym->ts.interface
12490 && sym->attr.if_source != IFSRC_DECL
12491 && resolve_procedure_interface (sym) == FAILURE)
12492 return;
12493
12494 if (sym->attr.is_protected && !sym->attr.proc_pointer
12495 && (sym->attr.procedure || sym->attr.external))
12496 {
12497 if (sym->attr.external)
12498 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12499 "at %L", &sym->declared_at);
12500 else
12501 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12502 "at %L", &sym->declared_at);
12503
12504 return;
12505 }
12506
12507 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
12508 return;
12509
12510 /* Symbols that are module procedures with results (functions) have
12511 the types and array specification copied for type checking in
12512 procedures that call them, as well as for saving to a module
12513 file. These symbols can't stand the scrutiny that their results
12514 can. */
12515 mp_flag = (sym->result != NULL && sym->result != sym);
12516
12517 /* Make sure that the intrinsic is consistent with its internal
12518 representation. This needs to be done before assigning a default
12519 type to avoid spurious warnings. */
12520 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12521 && gfc_resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
12522 return;
12523
12524 /* Resolve associate names. */
12525 if (sym->assoc)
12526 resolve_assoc_var (sym, true);
12527
12528 /* Assign default type to symbols that need one and don't have one. */
12529 if (sym->ts.type == BT_UNKNOWN)
12530 {
12531 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12532 {
12533 gfc_set_default_type (sym, 1, NULL);
12534 }
12535
12536 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12537 && !sym->attr.function && !sym->attr.subroutine
12538 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12539 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12540
12541 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12542 {
12543 /* The specific case of an external procedure should emit an error
12544 in the case that there is no implicit type. */
12545 if (!mp_flag)
12546 gfc_set_default_type (sym, sym->attr.external, NULL);
12547 else
12548 {
12549 /* Result may be in another namespace. */
12550 resolve_symbol (sym->result);
12551
12552 if (!sym->result->attr.proc_pointer)
12553 {
12554 sym->ts = sym->result->ts;
12555 sym->as = gfc_copy_array_spec (sym->result->as);
12556 sym->attr.dimension = sym->result->attr.dimension;
12557 sym->attr.pointer = sym->result->attr.pointer;
12558 sym->attr.allocatable = sym->result->attr.allocatable;
12559 sym->attr.contiguous = sym->result->attr.contiguous;
12560 }
12561 }
12562 }
12563 }
12564 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12565 gfc_resolve_array_spec (sym->result->as, false);
12566
12567 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12568 {
12569 as = CLASS_DATA (sym)->as;
12570 class_attr = CLASS_DATA (sym)->attr;
12571 class_attr.pointer = class_attr.class_pointer;
12572 }
12573 else
12574 {
12575 class_attr = sym->attr;
12576 as = sym->as;
12577 }
12578
12579 /* F2008, C530. */
12580 if (sym->attr.contiguous
12581 && (!class_attr.dimension
12582 || (as->type != AS_ASSUMED_SHAPE && !class_attr.pointer)))
12583 {
12584 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12585 "array pointer or an assumed-shape array", sym->name,
12586 &sym->declared_at);
12587 return;
12588 }
12589
12590 /* Assumed size arrays and assumed shape arrays must be dummy
12591 arguments. Array-spec's of implied-shape should have been resolved to
12592 AS_EXPLICIT already. */
12593
12594 if (as)
12595 {
12596 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12597 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12598 || as->type == AS_ASSUMED_SHAPE)
12599 && sym->attr.dummy == 0)
12600 {
12601 if (as->type == AS_ASSUMED_SIZE)
12602 gfc_error ("Assumed size array at %L must be a dummy argument",
12603 &sym->declared_at);
12604 else
12605 gfc_error ("Assumed shape array at %L must be a dummy argument",
12606 &sym->declared_at);
12607 return;
12608 }
12609 /* TS 29113, C535a. */
12610 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy)
12611 {
12612 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12613 &sym->declared_at);
12614 return;
12615 }
12616 if (as->type == AS_ASSUMED_RANK
12617 && (sym->attr.codimension || sym->attr.value))
12618 {
12619 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12620 "CODIMENSION attribute", &sym->declared_at);
12621 return;
12622 }
12623 }
12624
12625 /* Make sure symbols with known intent or optional are really dummy
12626 variable. Because of ENTRY statement, this has to be deferred
12627 until resolution time. */
12628
12629 if (!sym->attr.dummy
12630 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12631 {
12632 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12633 return;
12634 }
12635
12636 if (sym->attr.value && !sym->attr.dummy)
12637 {
12638 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12639 "it is not a dummy argument", sym->name, &sym->declared_at);
12640 return;
12641 }
12642
12643 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12644 {
12645 gfc_charlen *cl = sym->ts.u.cl;
12646 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12647 {
12648 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12649 "attribute must have constant length",
12650 sym->name, &sym->declared_at);
12651 return;
12652 }
12653
12654 if (sym->ts.is_c_interop
12655 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12656 {
12657 gfc_error ("C interoperable character dummy variable '%s' at %L "
12658 "with VALUE attribute must have length one",
12659 sym->name, &sym->declared_at);
12660 return;
12661 }
12662 }
12663
12664 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12665 && sym->ts.u.derived->attr.generic)
12666 {
12667 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12668 if (!sym->ts.u.derived)
12669 {
12670 gfc_error ("The derived type '%s' at %L is of type '%s', "
12671 "which has not been defined", sym->name,
12672 &sym->declared_at, sym->ts.u.derived->name);
12673 sym->ts.type = BT_UNKNOWN;
12674 return;
12675 }
12676 }
12677
12678 if (sym->ts.type == BT_ASSUMED)
12679 {
12680 /* TS 29113, C407a. */
12681 if (!sym->attr.dummy)
12682 {
12683 gfc_error ("Assumed type of variable %s at %L is only permitted "
12684 "for dummy variables", sym->name, &sym->declared_at);
12685 return;
12686 }
12687 if (sym->attr.allocatable || sym->attr.codimension
12688 || sym->attr.pointer || sym->attr.value)
12689 {
12690 gfc_error ("Assumed-type variable %s at %L may not have the "
12691 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
12692 sym->name, &sym->declared_at);
12693 return;
12694 }
12695 if (sym->attr.intent == INTENT_OUT)
12696 {
12697 gfc_error ("Assumed-type variable %s at %L may not have the "
12698 "INTENT(OUT) attribute",
12699 sym->name, &sym->declared_at);
12700 return;
12701 }
12702 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
12703 {
12704 gfc_error ("Assumed-type variable %s at %L shall not be an "
12705 "explicit-shape array", sym->name, &sym->declared_at);
12706 return;
12707 }
12708 }
12709
12710 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12711 do this for something that was implicitly typed because that is handled
12712 in gfc_set_default_type. Handle dummy arguments and procedure
12713 definitions separately. Also, anything that is use associated is not
12714 handled here but instead is handled in the module it is declared in.
12715 Finally, derived type definitions are allowed to be BIND(C) since that
12716 only implies that they're interoperable, and they are checked fully for
12717 interoperability when a variable is declared of that type. */
12718 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12719 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12720 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12721 {
12722 gfc_try t = SUCCESS;
12723
12724 /* First, make sure the variable is declared at the
12725 module-level scope (J3/04-007, Section 15.3). */
12726 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12727 sym->attr.in_common == 0)
12728 {
12729 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12730 "is neither a COMMON block nor declared at the "
12731 "module level scope", sym->name, &(sym->declared_at));
12732 t = FAILURE;
12733 }
12734 else if (sym->common_head != NULL)
12735 {
12736 t = verify_com_block_vars_c_interop (sym->common_head);
12737 }
12738 else
12739 {
12740 /* If type() declaration, we need to verify that the components
12741 of the given type are all C interoperable, etc. */
12742 if (sym->ts.type == BT_DERIVED &&
12743 sym->ts.u.derived->attr.is_c_interop != 1)
12744 {
12745 /* Make sure the user marked the derived type as BIND(C). If
12746 not, call the verify routine. This could print an error
12747 for the derived type more than once if multiple variables
12748 of that type are declared. */
12749 if (sym->ts.u.derived->attr.is_bind_c != 1)
12750 verify_bind_c_derived_type (sym->ts.u.derived);
12751 t = FAILURE;
12752 }
12753
12754 /* Verify the variable itself as C interoperable if it
12755 is BIND(C). It is not possible for this to succeed if
12756 the verify_bind_c_derived_type failed, so don't have to handle
12757 any error returned by verify_bind_c_derived_type. */
12758 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12759 sym->common_block);
12760 }
12761
12762 if (t == FAILURE)
12763 {
12764 /* clear the is_bind_c flag to prevent reporting errors more than
12765 once if something failed. */
12766 sym->attr.is_bind_c = 0;
12767 return;
12768 }
12769 }
12770
12771 /* If a derived type symbol has reached this point, without its
12772 type being declared, we have an error. Notice that most
12773 conditions that produce undefined derived types have already
12774 been dealt with. However, the likes of:
12775 implicit type(t) (t) ..... call foo (t) will get us here if
12776 the type is not declared in the scope of the implicit
12777 statement. Change the type to BT_UNKNOWN, both because it is so
12778 and to prevent an ICE. */
12779 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12780 && sym->ts.u.derived->components == NULL
12781 && !sym->ts.u.derived->attr.zero_comp)
12782 {
12783 gfc_error ("The derived type '%s' at %L is of type '%s', "
12784 "which has not been defined", sym->name,
12785 &sym->declared_at, sym->ts.u.derived->name);
12786 sym->ts.type = BT_UNKNOWN;
12787 return;
12788 }
12789
12790 /* Make sure that the derived type has been resolved and that the
12791 derived type is visible in the symbol's namespace, if it is a
12792 module function and is not PRIVATE. */
12793 if (sym->ts.type == BT_DERIVED
12794 && sym->ts.u.derived->attr.use_assoc
12795 && sym->ns->proc_name
12796 && sym->ns->proc_name->attr.flavor == FL_MODULE
12797 && resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12798 return;
12799
12800 /* Unless the derived-type declaration is use associated, Fortran 95
12801 does not allow public entries of private derived types.
12802 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12803 161 in 95-006r3. */
12804 if (sym->ts.type == BT_DERIVED
12805 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12806 && !sym->ts.u.derived->attr.use_assoc
12807 && gfc_check_symbol_access (sym)
12808 && !gfc_check_symbol_access (sym->ts.u.derived)
12809 && gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L "
12810 "of PRIVATE derived type '%s'",
12811 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12812 : "variable", sym->name, &sym->declared_at,
12813 sym->ts.u.derived->name) == FAILURE)
12814 return;
12815
12816 /* F2008, C1302. */
12817 if (sym->ts.type == BT_DERIVED
12818 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
12819 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
12820 || sym->ts.u.derived->attr.lock_comp)
12821 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
12822 {
12823 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
12824 "type LOCK_TYPE must be a coarray", sym->name,
12825 &sym->declared_at);
12826 return;
12827 }
12828
12829 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12830 default initialization is defined (5.1.2.4.4). */
12831 if (sym->ts.type == BT_DERIVED
12832 && sym->attr.dummy
12833 && sym->attr.intent == INTENT_OUT
12834 && sym->as
12835 && sym->as->type == AS_ASSUMED_SIZE)
12836 {
12837 for (c = sym->ts.u.derived->components; c; c = c->next)
12838 {
12839 if (c->initializer)
12840 {
12841 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12842 "ASSUMED SIZE and so cannot have a default initializer",
12843 sym->name, &sym->declared_at);
12844 return;
12845 }
12846 }
12847 }
12848
12849 /* F2008, C542. */
12850 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
12851 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
12852 {
12853 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
12854 "INTENT(OUT)", sym->name, &sym->declared_at);
12855 return;
12856 }
12857
12858 /* F2008, C525. */
12859 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12860 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12861 && CLASS_DATA (sym)->attr.coarray_comp))
12862 || class_attr.codimension)
12863 && (sym->attr.result || sym->result == sym))
12864 {
12865 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12866 "a coarray component", sym->name, &sym->declared_at);
12867 return;
12868 }
12869
12870 /* F2008, C524. */
12871 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12872 && sym->ts.u.derived->ts.is_iso_c)
12873 {
12874 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12875 "shall not be a coarray", sym->name, &sym->declared_at);
12876 return;
12877 }
12878
12879 /* F2008, C525. */
12880 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12881 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12882 && CLASS_DATA (sym)->attr.coarray_comp))
12883 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
12884 || class_attr.allocatable))
12885 {
12886 gfc_error ("Variable '%s' at %L with coarray component "
12887 "shall be a nonpointer, nonallocatable scalar",
12888 sym->name, &sym->declared_at);
12889 return;
12890 }
12891
12892 /* F2008, C526. The function-result case was handled above. */
12893 if (class_attr.codimension
12894 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
12895 || sym->attr.select_type_temporary
12896 || sym->ns->save_all
12897 || sym->ns->proc_name->attr.flavor == FL_MODULE
12898 || sym->ns->proc_name->attr.is_main_program
12899 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12900 {
12901 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
12902 "nor a dummy argument", sym->name, &sym->declared_at);
12903 return;
12904 }
12905 /* F2008, C528. */
12906 else if (class_attr.codimension && !sym->attr.select_type_temporary
12907 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
12908 {
12909 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12910 "deferred shape", sym->name, &sym->declared_at);
12911 return;
12912 }
12913 else if (class_attr.codimension && class_attr.allocatable && as
12914 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
12915 {
12916 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12917 "deferred shape", sym->name, &sym->declared_at);
12918 return;
12919 }
12920
12921 /* F2008, C541. */
12922 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12923 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
12924 && CLASS_DATA (sym)->attr.coarray_comp))
12925 || (class_attr.codimension && class_attr.allocatable))
12926 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12927 {
12928 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12929 "allocatable coarray or have coarray components",
12930 sym->name, &sym->declared_at);
12931 return;
12932 }
12933
12934 if (class_attr.codimension && sym->attr.dummy
12935 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12936 {
12937 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12938 "procedure '%s'", sym->name, &sym->declared_at,
12939 sym->ns->proc_name->name);
12940 return;
12941 }
12942
12943 switch (sym->attr.flavor)
12944 {
12945 case FL_VARIABLE:
12946 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12947 return;
12948 break;
12949
12950 case FL_PROCEDURE:
12951 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12952 return;
12953 break;
12954
12955 case FL_NAMELIST:
12956 if (resolve_fl_namelist (sym) == FAILURE)
12957 return;
12958 break;
12959
12960 case FL_PARAMETER:
12961 if (resolve_fl_parameter (sym) == FAILURE)
12962 return;
12963 break;
12964
12965 default:
12966 break;
12967 }
12968
12969 /* Resolve array specifier. Check as well some constraints
12970 on COMMON blocks. */
12971
12972 check_constant = sym->attr.in_common && !sym->attr.pointer;
12973
12974 /* Set the formal_arg_flag so that check_conflict will not throw
12975 an error for host associated variables in the specification
12976 expression for an array_valued function. */
12977 if (sym->attr.function && sym->as)
12978 formal_arg_flag = 1;
12979
12980 gfc_resolve_array_spec (sym->as, check_constant);
12981
12982 formal_arg_flag = 0;
12983
12984 /* Resolve formal namespaces. */
12985 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12986 && !sym->attr.contained && !sym->attr.intrinsic)
12987 gfc_resolve (sym->formal_ns);
12988
12989 /* Make sure the formal namespace is present. */
12990 if (sym->formal && !sym->formal_ns)
12991 {
12992 gfc_formal_arglist *formal = sym->formal;
12993 while (formal && !formal->sym)
12994 formal = formal->next;
12995
12996 if (formal)
12997 {
12998 sym->formal_ns = formal->sym->ns;
12999 sym->formal_ns->refs++;
13000 }
13001 }
13002
13003 /* Check threadprivate restrictions. */
13004 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13005 && (!sym->attr.in_common
13006 && sym->module == NULL
13007 && (sym->ns->proc_name == NULL
13008 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13009 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13010
13011 /* If we have come this far we can apply default-initializers, as
13012 described in 14.7.5, to those variables that have not already
13013 been assigned one. */
13014 if (sym->ts.type == BT_DERIVED
13015 && sym->ns == gfc_current_ns
13016 && !sym->value
13017 && !sym->attr.allocatable
13018 && !sym->attr.alloc_comp)
13019 {
13020 symbol_attribute *a = &sym->attr;
13021
13022 if ((!a->save && !a->dummy && !a->pointer
13023 && !a->in_common && !a->use_assoc
13024 && (a->referenced || a->result)
13025 && !(a->function && sym != sym->result))
13026 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13027 apply_default_init (sym);
13028 }
13029
13030 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13031 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13032 && !CLASS_DATA (sym)->attr.class_pointer
13033 && !CLASS_DATA (sym)->attr.allocatable)
13034 apply_default_init (sym);
13035
13036 /* If this symbol has a type-spec, check it. */
13037 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13038 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13039 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
13040 == FAILURE)
13041 return;
13042 }
13043
13044
13045 /************* Resolve DATA statements *************/
13046
13047 static struct
13048 {
13049 gfc_data_value *vnode;
13050 mpz_t left;
13051 }
13052 values;
13053
13054
13055 /* Advance the values structure to point to the next value in the data list. */
13056
13057 static gfc_try
13058 next_data_value (void)
13059 {
13060 while (mpz_cmp_ui (values.left, 0) == 0)
13061 {
13062
13063 if (values.vnode->next == NULL)
13064 return FAILURE;
13065
13066 values.vnode = values.vnode->next;
13067 mpz_set (values.left, values.vnode->repeat);
13068 }
13069
13070 return SUCCESS;
13071 }
13072
13073
13074 static gfc_try
13075 check_data_variable (gfc_data_variable *var, locus *where)
13076 {
13077 gfc_expr *e;
13078 mpz_t size;
13079 mpz_t offset;
13080 gfc_try t;
13081 ar_type mark = AR_UNKNOWN;
13082 int i;
13083 mpz_t section_index[GFC_MAX_DIMENSIONS];
13084 gfc_ref *ref;
13085 gfc_array_ref *ar;
13086 gfc_symbol *sym;
13087 int has_pointer;
13088
13089 if (gfc_resolve_expr (var->expr) == FAILURE)
13090 return FAILURE;
13091
13092 ar = NULL;
13093 mpz_init_set_si (offset, 0);
13094 e = var->expr;
13095
13096 if (e->expr_type != EXPR_VARIABLE)
13097 gfc_internal_error ("check_data_variable(): Bad expression");
13098
13099 sym = e->symtree->n.sym;
13100
13101 if (sym->ns->is_block_data && !sym->attr.in_common)
13102 {
13103 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13104 sym->name, &sym->declared_at);
13105 }
13106
13107 if (e->ref == NULL && sym->as)
13108 {
13109 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13110 " declaration", sym->name, where);
13111 return FAILURE;
13112 }
13113
13114 has_pointer = sym->attr.pointer;
13115
13116 if (gfc_is_coindexed (e))
13117 {
13118 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13119 where);
13120 return FAILURE;
13121 }
13122
13123 for (ref = e->ref; ref; ref = ref->next)
13124 {
13125 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13126 has_pointer = 1;
13127
13128 if (has_pointer
13129 && ref->type == REF_ARRAY
13130 && ref->u.ar.type != AR_FULL)
13131 {
13132 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13133 "be a full array", sym->name, where);
13134 return FAILURE;
13135 }
13136 }
13137
13138 if (e->rank == 0 || has_pointer)
13139 {
13140 mpz_init_set_ui (size, 1);
13141 ref = NULL;
13142 }
13143 else
13144 {
13145 ref = e->ref;
13146
13147 /* Find the array section reference. */
13148 for (ref = e->ref; ref; ref = ref->next)
13149 {
13150 if (ref->type != REF_ARRAY)
13151 continue;
13152 if (ref->u.ar.type == AR_ELEMENT)
13153 continue;
13154 break;
13155 }
13156 gcc_assert (ref);
13157
13158 /* Set marks according to the reference pattern. */
13159 switch (ref->u.ar.type)
13160 {
13161 case AR_FULL:
13162 mark = AR_FULL;
13163 break;
13164
13165 case AR_SECTION:
13166 ar = &ref->u.ar;
13167 /* Get the start position of array section. */
13168 gfc_get_section_index (ar, section_index, &offset);
13169 mark = AR_SECTION;
13170 break;
13171
13172 default:
13173 gcc_unreachable ();
13174 }
13175
13176 if (gfc_array_size (e, &size) == FAILURE)
13177 {
13178 gfc_error ("Nonconstant array section at %L in DATA statement",
13179 &e->where);
13180 mpz_clear (offset);
13181 return FAILURE;
13182 }
13183 }
13184
13185 t = SUCCESS;
13186
13187 while (mpz_cmp_ui (size, 0) > 0)
13188 {
13189 if (next_data_value () == FAILURE)
13190 {
13191 gfc_error ("DATA statement at %L has more variables than values",
13192 where);
13193 t = FAILURE;
13194 break;
13195 }
13196
13197 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13198 if (t == FAILURE)
13199 break;
13200
13201 /* If we have more than one element left in the repeat count,
13202 and we have more than one element left in the target variable,
13203 then create a range assignment. */
13204 /* FIXME: Only done for full arrays for now, since array sections
13205 seem tricky. */
13206 if (mark == AR_FULL && ref && ref->next == NULL
13207 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13208 {
13209 mpz_t range;
13210
13211 if (mpz_cmp (size, values.left) >= 0)
13212 {
13213 mpz_init_set (range, values.left);
13214 mpz_sub (size, size, values.left);
13215 mpz_set_ui (values.left, 0);
13216 }
13217 else
13218 {
13219 mpz_init_set (range, size);
13220 mpz_sub (values.left, values.left, size);
13221 mpz_set_ui (size, 0);
13222 }
13223
13224 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13225 offset, &range);
13226
13227 mpz_add (offset, offset, range);
13228 mpz_clear (range);
13229
13230 if (t == FAILURE)
13231 break;
13232 }
13233
13234 /* Assign initial value to symbol. */
13235 else
13236 {
13237 mpz_sub_ui (values.left, values.left, 1);
13238 mpz_sub_ui (size, size, 1);
13239
13240 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13241 offset, NULL);
13242 if (t == FAILURE)
13243 break;
13244
13245 if (mark == AR_FULL)
13246 mpz_add_ui (offset, offset, 1);
13247
13248 /* Modify the array section indexes and recalculate the offset
13249 for next element. */
13250 else if (mark == AR_SECTION)
13251 gfc_advance_section (section_index, ar, &offset);
13252 }
13253 }
13254
13255 if (mark == AR_SECTION)
13256 {
13257 for (i = 0; i < ar->dimen; i++)
13258 mpz_clear (section_index[i]);
13259 }
13260
13261 mpz_clear (size);
13262 mpz_clear (offset);
13263
13264 return t;
13265 }
13266
13267
13268 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
13269
13270 /* Iterate over a list of elements in a DATA statement. */
13271
13272 static gfc_try
13273 traverse_data_list (gfc_data_variable *var, locus *where)
13274 {
13275 mpz_t trip;
13276 iterator_stack frame;
13277 gfc_expr *e, *start, *end, *step;
13278 gfc_try retval = SUCCESS;
13279
13280 mpz_init (frame.value);
13281 mpz_init (trip);
13282
13283 start = gfc_copy_expr (var->iter.start);
13284 end = gfc_copy_expr (var->iter.end);
13285 step = gfc_copy_expr (var->iter.step);
13286
13287 if (gfc_simplify_expr (start, 1) == FAILURE
13288 || start->expr_type != EXPR_CONSTANT)
13289 {
13290 gfc_error ("start of implied-do loop at %L could not be "
13291 "simplified to a constant value", &start->where);
13292 retval = FAILURE;
13293 goto cleanup;
13294 }
13295 if (gfc_simplify_expr (end, 1) == FAILURE
13296 || end->expr_type != EXPR_CONSTANT)
13297 {
13298 gfc_error ("end of implied-do loop at %L could not be "
13299 "simplified to a constant value", &start->where);
13300 retval = FAILURE;
13301 goto cleanup;
13302 }
13303 if (gfc_simplify_expr (step, 1) == FAILURE
13304 || step->expr_type != EXPR_CONSTANT)
13305 {
13306 gfc_error ("step of implied-do loop at %L could not be "
13307 "simplified to a constant value", &start->where);
13308 retval = FAILURE;
13309 goto cleanup;
13310 }
13311
13312 mpz_set (trip, end->value.integer);
13313 mpz_sub (trip, trip, start->value.integer);
13314 mpz_add (trip, trip, step->value.integer);
13315
13316 mpz_div (trip, trip, step->value.integer);
13317
13318 mpz_set (frame.value, start->value.integer);
13319
13320 frame.prev = iter_stack;
13321 frame.variable = var->iter.var->symtree;
13322 iter_stack = &frame;
13323
13324 while (mpz_cmp_ui (trip, 0) > 0)
13325 {
13326 if (traverse_data_var (var->list, where) == FAILURE)
13327 {
13328 retval = FAILURE;
13329 goto cleanup;
13330 }
13331
13332 e = gfc_copy_expr (var->expr);
13333 if (gfc_simplify_expr (e, 1) == FAILURE)
13334 {
13335 gfc_free_expr (e);
13336 retval = FAILURE;
13337 goto cleanup;
13338 }
13339
13340 mpz_add (frame.value, frame.value, step->value.integer);
13341
13342 mpz_sub_ui (trip, trip, 1);
13343 }
13344
13345 cleanup:
13346 mpz_clear (frame.value);
13347 mpz_clear (trip);
13348
13349 gfc_free_expr (start);
13350 gfc_free_expr (end);
13351 gfc_free_expr (step);
13352
13353 iter_stack = frame.prev;
13354 return retval;
13355 }
13356
13357
13358 /* Type resolve variables in the variable list of a DATA statement. */
13359
13360 static gfc_try
13361 traverse_data_var (gfc_data_variable *var, locus *where)
13362 {
13363 gfc_try t;
13364
13365 for (; var; var = var->next)
13366 {
13367 if (var->expr == NULL)
13368 t = traverse_data_list (var, where);
13369 else
13370 t = check_data_variable (var, where);
13371
13372 if (t == FAILURE)
13373 return FAILURE;
13374 }
13375
13376 return SUCCESS;
13377 }
13378
13379
13380 /* Resolve the expressions and iterators associated with a data statement.
13381 This is separate from the assignment checking because data lists should
13382 only be resolved once. */
13383
13384 static gfc_try
13385 resolve_data_variables (gfc_data_variable *d)
13386 {
13387 for (; d; d = d->next)
13388 {
13389 if (d->list == NULL)
13390 {
13391 if (gfc_resolve_expr (d->expr) == FAILURE)
13392 return FAILURE;
13393 }
13394 else
13395 {
13396 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
13397 return FAILURE;
13398
13399 if (resolve_data_variables (d->list) == FAILURE)
13400 return FAILURE;
13401 }
13402 }
13403
13404 return SUCCESS;
13405 }
13406
13407
13408 /* Resolve a single DATA statement. We implement this by storing a pointer to
13409 the value list into static variables, and then recursively traversing the
13410 variables list, expanding iterators and such. */
13411
13412 static void
13413 resolve_data (gfc_data *d)
13414 {
13415
13416 if (resolve_data_variables (d->var) == FAILURE)
13417 return;
13418
13419 values.vnode = d->value;
13420 if (d->value == NULL)
13421 mpz_set_ui (values.left, 0);
13422 else
13423 mpz_set (values.left, d->value->repeat);
13424
13425 if (traverse_data_var (d->var, &d->where) == FAILURE)
13426 return;
13427
13428 /* At this point, we better not have any values left. */
13429
13430 if (next_data_value () == SUCCESS)
13431 gfc_error ("DATA statement at %L has more values than variables",
13432 &d->where);
13433 }
13434
13435
13436 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13437 accessed by host or use association, is a dummy argument to a pure function,
13438 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13439 is storage associated with any such variable, shall not be used in the
13440 following contexts: (clients of this function). */
13441
13442 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13443 procedure. Returns zero if assignment is OK, nonzero if there is a
13444 problem. */
13445 int
13446 gfc_impure_variable (gfc_symbol *sym)
13447 {
13448 gfc_symbol *proc;
13449 gfc_namespace *ns;
13450
13451 if (sym->attr.use_assoc || sym->attr.in_common)
13452 return 1;
13453
13454 /* Check if the symbol's ns is inside the pure procedure. */
13455 for (ns = gfc_current_ns; ns; ns = ns->parent)
13456 {
13457 if (ns == sym->ns)
13458 break;
13459 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13460 return 1;
13461 }
13462
13463 proc = sym->ns->proc_name;
13464 if (sym->attr.dummy && gfc_pure (proc)
13465 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13466 ||
13467 proc->attr.function))
13468 return 1;
13469
13470 /* TODO: Sort out what can be storage associated, if anything, and include
13471 it here. In principle equivalences should be scanned but it does not
13472 seem to be possible to storage associate an impure variable this way. */
13473 return 0;
13474 }
13475
13476
13477 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13478 current namespace is inside a pure procedure. */
13479
13480 int
13481 gfc_pure (gfc_symbol *sym)
13482 {
13483 symbol_attribute attr;
13484 gfc_namespace *ns;
13485
13486 if (sym == NULL)
13487 {
13488 /* Check if the current namespace or one of its parents
13489 belongs to a pure procedure. */
13490 for (ns = gfc_current_ns; ns; ns = ns->parent)
13491 {
13492 sym = ns->proc_name;
13493 if (sym == NULL)
13494 return 0;
13495 attr = sym->attr;
13496 if (attr.flavor == FL_PROCEDURE && attr.pure)
13497 return 1;
13498 }
13499 return 0;
13500 }
13501
13502 attr = sym->attr;
13503
13504 return attr.flavor == FL_PROCEDURE && attr.pure;
13505 }
13506
13507
13508 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13509 checks if the current namespace is implicitly pure. Note that this
13510 function returns false for a PURE procedure. */
13511
13512 int
13513 gfc_implicit_pure (gfc_symbol *sym)
13514 {
13515 gfc_namespace *ns;
13516
13517 if (sym == NULL)
13518 {
13519 /* Check if the current procedure is implicit_pure. Walk up
13520 the procedure list until we find a procedure. */
13521 for (ns = gfc_current_ns; ns; ns = ns->parent)
13522 {
13523 sym = ns->proc_name;
13524 if (sym == NULL)
13525 return 0;
13526
13527 if (sym->attr.flavor == FL_PROCEDURE)
13528 break;
13529 }
13530 }
13531
13532 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13533 && !sym->attr.pure;
13534 }
13535
13536
13537 /* Test whether the current procedure is elemental or not. */
13538
13539 int
13540 gfc_elemental (gfc_symbol *sym)
13541 {
13542 symbol_attribute attr;
13543
13544 if (sym == NULL)
13545 sym = gfc_current_ns->proc_name;
13546 if (sym == NULL)
13547 return 0;
13548 attr = sym->attr;
13549
13550 return attr.flavor == FL_PROCEDURE && attr.elemental;
13551 }
13552
13553
13554 /* Warn about unused labels. */
13555
13556 static void
13557 warn_unused_fortran_label (gfc_st_label *label)
13558 {
13559 if (label == NULL)
13560 return;
13561
13562 warn_unused_fortran_label (label->left);
13563
13564 if (label->defined == ST_LABEL_UNKNOWN)
13565 return;
13566
13567 switch (label->referenced)
13568 {
13569 case ST_LABEL_UNKNOWN:
13570 gfc_warning ("Label %d at %L defined but not used", label->value,
13571 &label->where);
13572 break;
13573
13574 case ST_LABEL_BAD_TARGET:
13575 gfc_warning ("Label %d at %L defined but cannot be used",
13576 label->value, &label->where);
13577 break;
13578
13579 default:
13580 break;
13581 }
13582
13583 warn_unused_fortran_label (label->right);
13584 }
13585
13586
13587 /* Returns the sequence type of a symbol or sequence. */
13588
13589 static seq_type
13590 sequence_type (gfc_typespec ts)
13591 {
13592 seq_type result;
13593 gfc_component *c;
13594
13595 switch (ts.type)
13596 {
13597 case BT_DERIVED:
13598
13599 if (ts.u.derived->components == NULL)
13600 return SEQ_NONDEFAULT;
13601
13602 result = sequence_type (ts.u.derived->components->ts);
13603 for (c = ts.u.derived->components->next; c; c = c->next)
13604 if (sequence_type (c->ts) != result)
13605 return SEQ_MIXED;
13606
13607 return result;
13608
13609 case BT_CHARACTER:
13610 if (ts.kind != gfc_default_character_kind)
13611 return SEQ_NONDEFAULT;
13612
13613 return SEQ_CHARACTER;
13614
13615 case BT_INTEGER:
13616 if (ts.kind != gfc_default_integer_kind)
13617 return SEQ_NONDEFAULT;
13618
13619 return SEQ_NUMERIC;
13620
13621 case BT_REAL:
13622 if (!(ts.kind == gfc_default_real_kind
13623 || ts.kind == gfc_default_double_kind))
13624 return SEQ_NONDEFAULT;
13625
13626 return SEQ_NUMERIC;
13627
13628 case BT_COMPLEX:
13629 if (ts.kind != gfc_default_complex_kind)
13630 return SEQ_NONDEFAULT;
13631
13632 return SEQ_NUMERIC;
13633
13634 case BT_LOGICAL:
13635 if (ts.kind != gfc_default_logical_kind)
13636 return SEQ_NONDEFAULT;
13637
13638 return SEQ_NUMERIC;
13639
13640 default:
13641 return SEQ_NONDEFAULT;
13642 }
13643 }
13644
13645
13646 /* Resolve derived type EQUIVALENCE object. */
13647
13648 static gfc_try
13649 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13650 {
13651 gfc_component *c = derived->components;
13652
13653 if (!derived)
13654 return SUCCESS;
13655
13656 /* Shall not be an object of nonsequence derived type. */
13657 if (!derived->attr.sequence)
13658 {
13659 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13660 "attribute to be an EQUIVALENCE object", sym->name,
13661 &e->where);
13662 return FAILURE;
13663 }
13664
13665 /* Shall not have allocatable components. */
13666 if (derived->attr.alloc_comp)
13667 {
13668 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13669 "components to be an EQUIVALENCE object",sym->name,
13670 &e->where);
13671 return FAILURE;
13672 }
13673
13674 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13675 {
13676 gfc_error ("Derived type variable '%s' at %L with default "
13677 "initialization cannot be in EQUIVALENCE with a variable "
13678 "in COMMON", sym->name, &e->where);
13679 return FAILURE;
13680 }
13681
13682 for (; c ; c = c->next)
13683 {
13684 if (c->ts.type == BT_DERIVED
13685 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
13686 return FAILURE;
13687
13688 /* Shall not be an object of sequence derived type containing a pointer
13689 in the structure. */
13690 if (c->attr.pointer)
13691 {
13692 gfc_error ("Derived type variable '%s' at %L with pointer "
13693 "component(s) cannot be an EQUIVALENCE object",
13694 sym->name, &e->where);
13695 return FAILURE;
13696 }
13697 }
13698 return SUCCESS;
13699 }
13700
13701
13702 /* Resolve equivalence object.
13703 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13704 an allocatable array, an object of nonsequence derived type, an object of
13705 sequence derived type containing a pointer at any level of component
13706 selection, an automatic object, a function name, an entry name, a result
13707 name, a named constant, a structure component, or a subobject of any of
13708 the preceding objects. A substring shall not have length zero. A
13709 derived type shall not have components with default initialization nor
13710 shall two objects of an equivalence group be initialized.
13711 Either all or none of the objects shall have an protected attribute.
13712 The simple constraints are done in symbol.c(check_conflict) and the rest
13713 are implemented here. */
13714
13715 static void
13716 resolve_equivalence (gfc_equiv *eq)
13717 {
13718 gfc_symbol *sym;
13719 gfc_symbol *first_sym;
13720 gfc_expr *e;
13721 gfc_ref *r;
13722 locus *last_where = NULL;
13723 seq_type eq_type, last_eq_type;
13724 gfc_typespec *last_ts;
13725 int object, cnt_protected;
13726 const char *msg;
13727
13728 last_ts = &eq->expr->symtree->n.sym->ts;
13729
13730 first_sym = eq->expr->symtree->n.sym;
13731
13732 cnt_protected = 0;
13733
13734 for (object = 1; eq; eq = eq->eq, object++)
13735 {
13736 e = eq->expr;
13737
13738 e->ts = e->symtree->n.sym->ts;
13739 /* match_varspec might not know yet if it is seeing
13740 array reference or substring reference, as it doesn't
13741 know the types. */
13742 if (e->ref && e->ref->type == REF_ARRAY)
13743 {
13744 gfc_ref *ref = e->ref;
13745 sym = e->symtree->n.sym;
13746
13747 if (sym->attr.dimension)
13748 {
13749 ref->u.ar.as = sym->as;
13750 ref = ref->next;
13751 }
13752
13753 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13754 if (e->ts.type == BT_CHARACTER
13755 && ref
13756 && ref->type == REF_ARRAY
13757 && ref->u.ar.dimen == 1
13758 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13759 && ref->u.ar.stride[0] == NULL)
13760 {
13761 gfc_expr *start = ref->u.ar.start[0];
13762 gfc_expr *end = ref->u.ar.end[0];
13763 void *mem = NULL;
13764
13765 /* Optimize away the (:) reference. */
13766 if (start == NULL && end == NULL)
13767 {
13768 if (e->ref == ref)
13769 e->ref = ref->next;
13770 else
13771 e->ref->next = ref->next;
13772 mem = ref;
13773 }
13774 else
13775 {
13776 ref->type = REF_SUBSTRING;
13777 if (start == NULL)
13778 start = gfc_get_int_expr (gfc_default_integer_kind,
13779 NULL, 1);
13780 ref->u.ss.start = start;
13781 if (end == NULL && e->ts.u.cl)
13782 end = gfc_copy_expr (e->ts.u.cl->length);
13783 ref->u.ss.end = end;
13784 ref->u.ss.length = e->ts.u.cl;
13785 e->ts.u.cl = NULL;
13786 }
13787 ref = ref->next;
13788 free (mem);
13789 }
13790
13791 /* Any further ref is an error. */
13792 if (ref)
13793 {
13794 gcc_assert (ref->type == REF_ARRAY);
13795 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13796 &ref->u.ar.where);
13797 continue;
13798 }
13799 }
13800
13801 if (gfc_resolve_expr (e) == FAILURE)
13802 continue;
13803
13804 sym = e->symtree->n.sym;
13805
13806 if (sym->attr.is_protected)
13807 cnt_protected++;
13808 if (cnt_protected > 0 && cnt_protected != object)
13809 {
13810 gfc_error ("Either all or none of the objects in the "
13811 "EQUIVALENCE set at %L shall have the "
13812 "PROTECTED attribute",
13813 &e->where);
13814 break;
13815 }
13816
13817 /* Shall not equivalence common block variables in a PURE procedure. */
13818 if (sym->ns->proc_name
13819 && sym->ns->proc_name->attr.pure
13820 && sym->attr.in_common)
13821 {
13822 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13823 "object in the pure procedure '%s'",
13824 sym->name, &e->where, sym->ns->proc_name->name);
13825 break;
13826 }
13827
13828 /* Shall not be a named constant. */
13829 if (e->expr_type == EXPR_CONSTANT)
13830 {
13831 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13832 "object", sym->name, &e->where);
13833 continue;
13834 }
13835
13836 if (e->ts.type == BT_DERIVED
13837 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13838 continue;
13839
13840 /* Check that the types correspond correctly:
13841 Note 5.28:
13842 A numeric sequence structure may be equivalenced to another sequence
13843 structure, an object of default integer type, default real type, double
13844 precision real type, default logical type such that components of the
13845 structure ultimately only become associated to objects of the same
13846 kind. A character sequence structure may be equivalenced to an object
13847 of default character kind or another character sequence structure.
13848 Other objects may be equivalenced only to objects of the same type and
13849 kind parameters. */
13850
13851 /* Identical types are unconditionally OK. */
13852 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13853 goto identical_types;
13854
13855 last_eq_type = sequence_type (*last_ts);
13856 eq_type = sequence_type (sym->ts);
13857
13858 /* Since the pair of objects is not of the same type, mixed or
13859 non-default sequences can be rejected. */
13860
13861 msg = "Sequence %s with mixed components in EQUIVALENCE "
13862 "statement at %L with different type objects";
13863 if ((object ==2
13864 && last_eq_type == SEQ_MIXED
13865 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13866 == FAILURE)
13867 || (eq_type == SEQ_MIXED
13868 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13869 &e->where) == FAILURE))
13870 continue;
13871
13872 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13873 "statement at %L with objects of different type";
13874 if ((object ==2
13875 && last_eq_type == SEQ_NONDEFAULT
13876 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13877 last_where) == FAILURE)
13878 || (eq_type == SEQ_NONDEFAULT
13879 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13880 &e->where) == FAILURE))
13881 continue;
13882
13883 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13884 "EQUIVALENCE statement at %L";
13885 if (last_eq_type == SEQ_CHARACTER
13886 && eq_type != SEQ_CHARACTER
13887 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13888 &e->where) == FAILURE)
13889 continue;
13890
13891 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13892 "EQUIVALENCE statement at %L";
13893 if (last_eq_type == SEQ_NUMERIC
13894 && eq_type != SEQ_NUMERIC
13895 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13896 &e->where) == FAILURE)
13897 continue;
13898
13899 identical_types:
13900 last_ts =&sym->ts;
13901 last_where = &e->where;
13902
13903 if (!e->ref)
13904 continue;
13905
13906 /* Shall not be an automatic array. */
13907 if (e->ref->type == REF_ARRAY
13908 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13909 {
13910 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13911 "an EQUIVALENCE object", sym->name, &e->where);
13912 continue;
13913 }
13914
13915 r = e->ref;
13916 while (r)
13917 {
13918 /* Shall not be a structure component. */
13919 if (r->type == REF_COMPONENT)
13920 {
13921 gfc_error ("Structure component '%s' at %L cannot be an "
13922 "EQUIVALENCE object",
13923 r->u.c.component->name, &e->where);
13924 break;
13925 }
13926
13927 /* A substring shall not have length zero. */
13928 if (r->type == REF_SUBSTRING)
13929 {
13930 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13931 {
13932 gfc_error ("Substring at %L has length zero",
13933 &r->u.ss.start->where);
13934 break;
13935 }
13936 }
13937 r = r->next;
13938 }
13939 }
13940 }
13941
13942
13943 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13944
13945 static void
13946 resolve_fntype (gfc_namespace *ns)
13947 {
13948 gfc_entry_list *el;
13949 gfc_symbol *sym;
13950
13951 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13952 return;
13953
13954 /* If there are any entries, ns->proc_name is the entry master
13955 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13956 if (ns->entries)
13957 sym = ns->entries->sym;
13958 else
13959 sym = ns->proc_name;
13960 if (sym->result == sym
13961 && sym->ts.type == BT_UNKNOWN
13962 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13963 && !sym->attr.untyped)
13964 {
13965 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13966 sym->name, &sym->declared_at);
13967 sym->attr.untyped = 1;
13968 }
13969
13970 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13971 && !sym->attr.contained
13972 && !gfc_check_symbol_access (sym->ts.u.derived)
13973 && gfc_check_symbol_access (sym))
13974 {
13975 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
13976 "%L of PRIVATE type '%s'", sym->name,
13977 &sym->declared_at, sym->ts.u.derived->name);
13978 }
13979
13980 if (ns->entries)
13981 for (el = ns->entries->next; el; el = el->next)
13982 {
13983 if (el->sym->result == el->sym
13984 && el->sym->ts.type == BT_UNKNOWN
13985 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13986 && !el->sym->attr.untyped)
13987 {
13988 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13989 el->sym->name, &el->sym->declared_at);
13990 el->sym->attr.untyped = 1;
13991 }
13992 }
13993 }
13994
13995
13996 /* 12.3.2.1.1 Defined operators. */
13997
13998 static gfc_try
13999 check_uop_procedure (gfc_symbol *sym, locus where)
14000 {
14001 gfc_formal_arglist *formal;
14002
14003 if (!sym->attr.function)
14004 {
14005 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14006 sym->name, &where);
14007 return FAILURE;
14008 }
14009
14010 if (sym->ts.type == BT_CHARACTER
14011 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14012 && !(sym->result && sym->result->ts.u.cl
14013 && sym->result->ts.u.cl->length))
14014 {
14015 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14016 "character length", sym->name, &where);
14017 return FAILURE;
14018 }
14019
14020 formal = sym->formal;
14021 if (!formal || !formal->sym)
14022 {
14023 gfc_error ("User operator procedure '%s' at %L must have at least "
14024 "one argument", sym->name, &where);
14025 return FAILURE;
14026 }
14027
14028 if (formal->sym->attr.intent != INTENT_IN)
14029 {
14030 gfc_error ("First argument of operator interface at %L must be "
14031 "INTENT(IN)", &where);
14032 return FAILURE;
14033 }
14034
14035 if (formal->sym->attr.optional)
14036 {
14037 gfc_error ("First argument of operator interface at %L cannot be "
14038 "optional", &where);
14039 return FAILURE;
14040 }
14041
14042 formal = formal->next;
14043 if (!formal || !formal->sym)
14044 return SUCCESS;
14045
14046 if (formal->sym->attr.intent != INTENT_IN)
14047 {
14048 gfc_error ("Second argument of operator interface at %L must be "
14049 "INTENT(IN)", &where);
14050 return FAILURE;
14051 }
14052
14053 if (formal->sym->attr.optional)
14054 {
14055 gfc_error ("Second argument of operator interface at %L cannot be "
14056 "optional", &where);
14057 return FAILURE;
14058 }
14059
14060 if (formal->next)
14061 {
14062 gfc_error ("Operator interface at %L must have, at most, two "
14063 "arguments", &where);
14064 return FAILURE;
14065 }
14066
14067 return SUCCESS;
14068 }
14069
14070 static void
14071 gfc_resolve_uops (gfc_symtree *symtree)
14072 {
14073 gfc_interface *itr;
14074
14075 if (symtree == NULL)
14076 return;
14077
14078 gfc_resolve_uops (symtree->left);
14079 gfc_resolve_uops (symtree->right);
14080
14081 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14082 check_uop_procedure (itr->sym, itr->sym->declared_at);
14083 }
14084
14085
14086 /* Examine all of the expressions associated with a program unit,
14087 assign types to all intermediate expressions, make sure that all
14088 assignments are to compatible types and figure out which names
14089 refer to which functions or subroutines. It doesn't check code
14090 block, which is handled by resolve_code. */
14091
14092 static void
14093 resolve_types (gfc_namespace *ns)
14094 {
14095 gfc_namespace *n;
14096 gfc_charlen *cl;
14097 gfc_data *d;
14098 gfc_equiv *eq;
14099 gfc_namespace* old_ns = gfc_current_ns;
14100
14101 /* Check that all IMPLICIT types are ok. */
14102 if (!ns->seen_implicit_none)
14103 {
14104 unsigned letter;
14105 for (letter = 0; letter != GFC_LETTERS; ++letter)
14106 if (ns->set_flag[letter]
14107 && resolve_typespec_used (&ns->default_type[letter],
14108 &ns->implicit_loc[letter],
14109 NULL) == FAILURE)
14110 return;
14111 }
14112
14113 gfc_current_ns = ns;
14114
14115 resolve_entries (ns);
14116
14117 resolve_common_vars (ns->blank_common.head, false);
14118 resolve_common_blocks (ns->common_root);
14119
14120 resolve_contained_functions (ns);
14121
14122 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14123 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14124 resolve_formal_arglist (ns->proc_name);
14125
14126 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14127
14128 for (cl = ns->cl_list; cl; cl = cl->next)
14129 resolve_charlen (cl);
14130
14131 gfc_traverse_ns (ns, resolve_symbol);
14132
14133 resolve_fntype (ns);
14134
14135 for (n = ns->contained; n; n = n->sibling)
14136 {
14137 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14138 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14139 "also be PURE", n->proc_name->name,
14140 &n->proc_name->declared_at);
14141
14142 resolve_types (n);
14143 }
14144
14145 forall_flag = 0;
14146 do_concurrent_flag = 0;
14147 gfc_check_interfaces (ns);
14148
14149 gfc_traverse_ns (ns, resolve_values);
14150
14151 if (ns->save_all)
14152 gfc_save_all (ns);
14153
14154 iter_stack = NULL;
14155 for (d = ns->data; d; d = d->next)
14156 resolve_data (d);
14157
14158 iter_stack = NULL;
14159 gfc_traverse_ns (ns, gfc_formalize_init_value);
14160
14161 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14162
14163 if (ns->common_root != NULL)
14164 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
14165
14166 for (eq = ns->equiv; eq; eq = eq->next)
14167 resolve_equivalence (eq);
14168
14169 /* Warn about unused labels. */
14170 if (warn_unused_label)
14171 warn_unused_fortran_label (ns->st_labels);
14172
14173 gfc_resolve_uops (ns->uop_root);
14174
14175 gfc_current_ns = old_ns;
14176 }
14177
14178
14179 /* Call resolve_code recursively. */
14180
14181 static void
14182 resolve_codes (gfc_namespace *ns)
14183 {
14184 gfc_namespace *n;
14185 bitmap_obstack old_obstack;
14186
14187 if (ns->resolved == 1)
14188 return;
14189
14190 for (n = ns->contained; n; n = n->sibling)
14191 resolve_codes (n);
14192
14193 gfc_current_ns = ns;
14194
14195 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14196 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14197 cs_base = NULL;
14198
14199 /* Set to an out of range value. */
14200 current_entry_id = -1;
14201
14202 old_obstack = labels_obstack;
14203 bitmap_obstack_initialize (&labels_obstack);
14204
14205 resolve_code (ns->code, ns);
14206
14207 bitmap_obstack_release (&labels_obstack);
14208 labels_obstack = old_obstack;
14209 }
14210
14211
14212 /* This function is called after a complete program unit has been compiled.
14213 Its purpose is to examine all of the expressions associated with a program
14214 unit, assign types to all intermediate expressions, make sure that all
14215 assignments are to compatible types and figure out which names refer to
14216 which functions or subroutines. */
14217
14218 void
14219 gfc_resolve (gfc_namespace *ns)
14220 {
14221 gfc_namespace *old_ns;
14222 code_stack *old_cs_base;
14223
14224 if (ns->resolved)
14225 return;
14226
14227 ns->resolved = -1;
14228 old_ns = gfc_current_ns;
14229 old_cs_base = cs_base;
14230
14231 resolve_types (ns);
14232 resolve_codes (ns);
14233
14234 gfc_current_ns = old_ns;
14235 cs_base = old_cs_base;
14236 ns->resolved = 1;
14237
14238 gfc_run_passes (ns);
14239 }