Update ChangeLogs for wide-int work.
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
33
34 /* Types used in equivalence statements. */
35
36 typedef enum seq_type
37 {
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39 }
40 seq_type;
41
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
44
45 typedef struct code_stack
46 {
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
49
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
54 }
55 code_stack;
56
57 static code_stack *cs_base = NULL;
58
59
60 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
61
62 static int forall_flag;
63 int gfc_do_concurrent_flag;
64
65 /* True when we are resolving an expression that is an actual argument to
66 a procedure. */
67 static bool actual_arg = false;
68 /* True when we are resolving an expression that is the first actual argument
69 to a procedure. */
70 static bool first_actual_arg = false;
71
72
73 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
74
75 static int omp_workshare_flag;
76
77 /* Nonzero if we are processing a formal arglist. The corresponding function
78 resets the flag each time that it is read. */
79 static int formal_arg_flag = 0;
80
81 /* True if we are resolving a specification expression. */
82 static bool specification_expr = false;
83
84 /* The id of the last entry seen. */
85 static int current_entry_id;
86
87 /* We use bitmaps to determine if a branch target is valid. */
88 static bitmap_obstack labels_obstack;
89
90 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91 static bool inquiry_argument = false;
92
93
94 int
95 gfc_is_formal_arg (void)
96 {
97 return formal_arg_flag;
98 }
99
100 /* Is the symbol host associated? */
101 static bool
102 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
103 {
104 for (ns = ns->parent; ns; ns = ns->parent)
105 {
106 if (sym->ns == ns)
107 return true;
108 }
109
110 return false;
111 }
112
113 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
114 an ABSTRACT derived-type. If where is not NULL, an error message with that
115 locus is printed, optionally using name. */
116
117 static bool
118 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
119 {
120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
121 {
122 if (where)
123 {
124 if (name)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
126 name, where, ts->u.derived->name);
127 else
128 gfc_error ("ABSTRACT type '%s' used at %L",
129 ts->u.derived->name, where);
130 }
131
132 return false;
133 }
134
135 return true;
136 }
137
138
139 static bool
140 check_proc_interface (gfc_symbol *ifc, locus *where)
141 {
142 /* Several checks for F08:C1216. */
143 if (ifc->attr.procedure)
144 {
145 gfc_error ("Interface '%s' at %L is declared "
146 "in a later PROCEDURE statement", ifc->name, where);
147 return false;
148 }
149 if (ifc->generic)
150 {
151 /* For generic interfaces, check if there is
152 a specific procedure with the same name. */
153 gfc_interface *gen = ifc->generic;
154 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
155 gen = gen->next;
156 if (!gen)
157 {
158 gfc_error ("Interface '%s' at %L may not be generic",
159 ifc->name, where);
160 return false;
161 }
162 }
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
164 {
165 gfc_error ("Interface '%s' at %L may not be a statement function",
166 ifc->name, where);
167 return false;
168 }
169 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
170 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
171 ifc->attr.intrinsic = 1;
172 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
173 {
174 gfc_error ("Intrinsic procedure '%s' not allowed in "
175 "PROCEDURE statement at %L", ifc->name, where);
176 return false;
177 }
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
179 {
180 gfc_error ("Interface '%s' at %L must be explicit", ifc->name, where);
181 return false;
182 }
183 return true;
184 }
185
186
187 static void resolve_symbol (gfc_symbol *sym);
188
189
190 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
191
192 static bool
193 resolve_procedure_interface (gfc_symbol *sym)
194 {
195 gfc_symbol *ifc = sym->ts.interface;
196
197 if (!ifc)
198 return true;
199
200 if (ifc == sym)
201 {
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
204 return false;
205 }
206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
208
209 if (ifc->attr.if_source || ifc->attr.intrinsic)
210 {
211 /* Resolve interface and copy attributes. */
212 resolve_symbol (ifc);
213 if (ifc->attr.intrinsic)
214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
215
216 if (ifc->result)
217 {
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
220 }
221 else
222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
226
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.pure = ifc->attr.pure;
230 sym->attr.elemental = ifc->attr.elemental;
231 sym->attr.dimension = ifc->attr.dimension;
232 sym->attr.contiguous = ifc->attr.contiguous;
233 sym->attr.recursive = ifc->attr.recursive;
234 sym->attr.always_explicit = ifc->attr.always_explicit;
235 sym->attr.ext_attr |= ifc->attr.ext_attr;
236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
237 sym->attr.class_ok = ifc->attr.class_ok;
238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
240 /* Copy char length. */
241 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
242 {
243 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
247 }
248 }
249
250 return true;
251 }
252
253
254 /* Resolve types of formal argument lists. These have to be done early so that
255 the formal argument lists of module procedures can be copied to the
256 containing module before the individual procedures are resolved
257 individually. We also resolve argument lists of procedures in interface
258 blocks because they are self-contained scoping units.
259
260 Since a dummy argument cannot be a non-dummy procedure, the only
261 resort left for untyped names are the IMPLICIT types. */
262
263 static void
264 resolve_formal_arglist (gfc_symbol *proc)
265 {
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
268 bool saved_specification_expr;
269 int i;
270
271 if (proc->result != NULL)
272 sym = proc->result;
273 else
274 sym = proc;
275
276 if (gfc_elemental (proc)
277 || sym->attr.pointer || sym->attr.allocatable
278 || (sym->as && sym->as->rank != 0))
279 {
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
282 }
283
284 formal_arg_flag = 1;
285
286 for (f = proc->formal; f; f = f->next)
287 {
288 gfc_array_spec *as;
289
290 sym = f->sym;
291
292 if (sym == NULL)
293 {
294 /* Alternate return placeholder. */
295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
297 "'%s' at %L is not allowed", proc->name,
298 &proc->declared_at);
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
302 &proc->declared_at);
303 continue;
304 }
305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
306 && !resolve_procedure_interface (sym))
307 return;
308
309 if (strcmp (proc->name, sym->name) == 0)
310 {
311 gfc_error ("Self-referential argument "
312 "'%s' at %L is not allowed", sym->name,
313 &proc->declared_at);
314 return;
315 }
316
317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
319
320 if (sym->attr.subroutine || sym->attr.external)
321 {
322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
324 }
325 else
326 {
327 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
328 && (!sym->attr.function || sym->result == sym))
329 gfc_set_default_type (sym, 1, sym->ns);
330 }
331
332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
334
335 saved_specification_expr = specification_expr;
336 specification_expr = true;
337 gfc_resolve_array_spec (as, 0);
338 specification_expr = saved_specification_expr;
339
340 /* We can't tell if an array with dimension (:) is assumed or deferred
341 shape until we know if it has the pointer or allocatable attributes.
342 */
343 if (as && as->rank > 0 && as->type == AS_DEFERRED
344 && ((sym->ts.type != BT_CLASS
345 && !(sym->attr.pointer || sym->attr.allocatable))
346 || (sym->ts.type == BT_CLASS
347 && !(CLASS_DATA (sym)->attr.class_pointer
348 || CLASS_DATA (sym)->attr.allocatable)))
349 && sym->attr.flavor != FL_PROCEDURE)
350 {
351 as->type = AS_ASSUMED_SHAPE;
352 for (i = 0; i < as->rank; i++)
353 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
354 }
355
356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
357 || (as && as->type == AS_ASSUMED_RANK)
358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
359 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
360 && (CLASS_DATA (sym)->attr.class_pointer
361 || CLASS_DATA (sym)->attr.allocatable
362 || CLASS_DATA (sym)->attr.target))
363 || sym->attr.optional)
364 {
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
368 }
369
370 /* If the flavor is unknown at this point, it has to be a variable.
371 A procedure specification would have already set the type. */
372
373 if (sym->attr.flavor == FL_UNKNOWN)
374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
375
376 if (gfc_pure (proc))
377 {
378 if (sym->attr.flavor == FL_PROCEDURE)
379 {
380 /* F08:C1279. */
381 if (!gfc_pure (sym))
382 {
383 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
386 }
387 }
388 else if (!sym->attr.pointer)
389 {
390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
391 {
392 if (sym->attr.value)
393 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
394 " of pure function '%s' at %L with VALUE "
395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
398 gfc_error ("Argument '%s' of pure function '%s' at %L must "
399 "be INTENT(IN) or VALUE", sym->name, proc->name,
400 &sym->declared_at);
401 }
402
403 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
404 {
405 if (sym->attr.value)
406 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
407 " of pure subroutine '%s' at %L with VALUE "
408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
411 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
415 }
416 }
417 }
418
419 if (proc->attr.implicit_pure)
420 {
421 if (sym->attr.flavor == FL_PROCEDURE)
422 {
423 if (!gfc_pure (sym))
424 proc->attr.implicit_pure = 0;
425 }
426 else if (!sym->attr.pointer)
427 {
428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
430 proc->attr.implicit_pure = 0;
431
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
434 proc->attr.implicit_pure = 0;
435 }
436 }
437
438 if (gfc_elemental (proc))
439 {
440 /* F08:C1289. */
441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
444 {
445 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
446 "procedure", sym->name, &sym->declared_at);
447 continue;
448 }
449
450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
452 {
453 gfc_error ("Argument '%s' of elemental procedure at %L must "
454 "be scalar", sym->name, &sym->declared_at);
455 continue;
456 }
457
458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
461 {
462 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
466 }
467
468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
471 {
472 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
475 continue;
476 }
477
478 if (sym->attr.flavor == FL_PROCEDURE)
479 {
480 gfc_error ("Dummy procedure '%s' not allowed in elemental "
481 "procedure '%s' at %L", sym->name, proc->name,
482 &sym->declared_at);
483 continue;
484 }
485
486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
488 {
489 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
492 &sym->declared_at);
493 continue;
494 }
495 }
496
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
499 {
500 if (sym->as != NULL)
501 {
502 gfc_error ("Argument '%s' of statement function at %L must "
503 "be scalar", sym->name, &sym->declared_at);
504 continue;
505 }
506
507 if (sym->ts.type == BT_CHARACTER)
508 {
509 gfc_charlen *cl = sym->ts.u.cl;
510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
511 {
512 gfc_error ("Character-valued argument '%s' of statement "
513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
516 }
517 }
518 }
519 }
520 formal_arg_flag = 0;
521 }
522
523
524 /* Work function called when searching for symbols that have argument lists
525 associated with them. */
526
527 static void
528 find_arglists (gfc_symbol *sym)
529 {
530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
532 return;
533
534 resolve_formal_arglist (sym);
535 }
536
537
538 /* Given a namespace, resolve all formal argument lists within the namespace.
539 */
540
541 static void
542 resolve_formal_arglists (gfc_namespace *ns)
543 {
544 if (ns == NULL)
545 return;
546
547 gfc_traverse_ns (ns, find_arglists);
548 }
549
550
551 static void
552 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
553 {
554 bool t;
555
556 /* If this namespace is not a function or an entry master function,
557 ignore it. */
558 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
559 || sym->attr.entry_master)
560 return;
561
562 /* Try to find out of what the return type is. */
563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
564 {
565 t = gfc_set_default_type (sym->result, 0, ns);
566
567 if (!t && !sym->result->attr.untyped)
568 {
569 if (sym->result == sym)
570 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
571 sym->name, &sym->declared_at);
572 else if (!sym->result->attr.proc_pointer)
573 gfc_error ("Result '%s' of contained function '%s' at %L has "
574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
577 }
578 }
579
580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
585
586 if (sym->result->ts.type == BT_CHARACTER)
587 {
588 gfc_charlen *cl = sym->result->ts.u.cl;
589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
590 {
591 /* See if this is a module-procedure and adapt error message
592 accordingly. */
593 bool module_proc;
594 gcc_assert (ns->parent && ns->parent->proc_name);
595 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
596
597 gfc_error ("Character-valued %s '%s' at %L must not be"
598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
602 }
603 }
604 }
605
606
607 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
608 introduce duplicates. */
609
610 static void
611 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
612 {
613 gfc_formal_arglist *f, *new_arglist;
614 gfc_symbol *new_sym;
615
616 for (; new_args != NULL; new_args = new_args->next)
617 {
618 new_sym = new_args->sym;
619 /* See if this arg is already in the formal argument list. */
620 for (f = proc->formal; f; f = f->next)
621 {
622 if (new_sym == f->sym)
623 break;
624 }
625
626 if (f)
627 continue;
628
629 /* Add a new argument. Argument order is not important. */
630 new_arglist = gfc_get_formal_arglist ();
631 new_arglist->sym = new_sym;
632 new_arglist->next = proc->formal;
633 proc->formal = new_arglist;
634 }
635 }
636
637
638 /* Flag the arguments that are not present in all entries. */
639
640 static void
641 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
642 {
643 gfc_formal_arglist *f, *head;
644 head = new_args;
645
646 for (f = proc->formal; f; f = f->next)
647 {
648 if (f->sym == NULL)
649 continue;
650
651 for (new_args = head; new_args; new_args = new_args->next)
652 {
653 if (new_args->sym == f->sym)
654 break;
655 }
656
657 if (new_args)
658 continue;
659
660 f->sym->attr.not_always_present = 1;
661 }
662 }
663
664
665 /* Resolve alternate entry points. If a symbol has multiple entry points we
666 create a new master symbol for the main routine, and turn the existing
667 symbol into an entry point. */
668
669 static void
670 resolve_entries (gfc_namespace *ns)
671 {
672 gfc_namespace *old_ns;
673 gfc_code *c;
674 gfc_symbol *proc;
675 gfc_entry_list *el;
676 char name[GFC_MAX_SYMBOL_LEN + 1];
677 static int master_count = 0;
678
679 if (ns->proc_name == NULL)
680 return;
681
682 /* No need to do anything if this procedure doesn't have alternate entry
683 points. */
684 if (!ns->entries)
685 return;
686
687 /* We may already have resolved alternate entry points. */
688 if (ns->proc_name->attr.entry_master)
689 return;
690
691 /* If this isn't a procedure something has gone horribly wrong. */
692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
693
694 /* Remember the current namespace. */
695 old_ns = gfc_current_ns;
696
697 gfc_current_ns = ns;
698
699 /* Add the main entry point to the list of entry points. */
700 el = gfc_get_entry_list ();
701 el->sym = ns->proc_name;
702 el->id = 0;
703 el->next = ns->entries;
704 ns->entries = el;
705 ns->proc_name->attr.entry = 1;
706
707 /* If it is a module function, it needs to be in the right namespace
708 so that gfc_get_fake_result_decl can gather up the results. The
709 need for this arose in get_proc_name, where these beasts were
710 left in their own namespace, to keep prior references linked to
711 the entry declaration.*/
712 if (ns->proc_name->attr.function
713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
714 el->sym->ns = ns;
715
716 /* Do the same for entries where the master is not a module
717 procedure. These are retained in the module namespace because
718 of the module procedure declaration. */
719 for (el = el->next; el; el = el->next)
720 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
721 && el->sym->attr.mod_proc)
722 el->sym->ns = ns;
723 el = ns->entries;
724
725 /* Add an entry statement for it. */
726 c = gfc_get_code (EXEC_ENTRY);
727 c->ext.entry = el;
728 c->next = ns->code;
729 ns->code = c;
730
731 /* Create a new symbol for the master function. */
732 /* Give the internal function a unique name (within this file).
733 Also include the function name so the user has some hope of figuring
734 out what is going on. */
735 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
736 master_count++, ns->proc_name->name);
737 gfc_get_ha_symbol (name, &proc);
738 gcc_assert (proc != NULL);
739
740 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
741 if (ns->proc_name->attr.subroutine)
742 gfc_add_subroutine (&proc->attr, proc->name, NULL);
743 else
744 {
745 gfc_symbol *sym;
746 gfc_typespec *ts, *fts;
747 gfc_array_spec *as, *fas;
748 gfc_add_function (&proc->attr, proc->name, NULL);
749 proc->result = proc;
750 fas = ns->entries->sym->as;
751 fas = fas ? fas : ns->entries->sym->result->as;
752 fts = &ns->entries->sym->result->ts;
753 if (fts->type == BT_UNKNOWN)
754 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
755 for (el = ns->entries->next; el; el = el->next)
756 {
757 ts = &el->sym->result->ts;
758 as = el->sym->as;
759 as = as ? as : el->sym->result->as;
760 if (ts->type == BT_UNKNOWN)
761 ts = gfc_get_default_type (el->sym->result->name, NULL);
762
763 if (! gfc_compare_types (ts, fts)
764 || (el->sym->result->attr.dimension
765 != ns->entries->sym->result->attr.dimension)
766 || (el->sym->result->attr.pointer
767 != ns->entries->sym->result->attr.pointer))
768 break;
769 else if (as && fas && ns->entries->sym->result != el->sym->result
770 && gfc_compare_array_spec (as, fas) == 0)
771 gfc_error ("Function %s at %L has entries with mismatched "
772 "array specifications", ns->entries->sym->name,
773 &ns->entries->sym->declared_at);
774 /* The characteristics need to match and thus both need to have
775 the same string length, i.e. both len=*, or both len=4.
776 Having both len=<variable> is also possible, but difficult to
777 check at compile time. */
778 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
779 && (((ts->u.cl->length && !fts->u.cl->length)
780 ||(!ts->u.cl->length && fts->u.cl->length))
781 || (ts->u.cl->length
782 && ts->u.cl->length->expr_type
783 != fts->u.cl->length->expr_type)
784 || (ts->u.cl->length
785 && ts->u.cl->length->expr_type == EXPR_CONSTANT
786 && mpz_cmp (ts->u.cl->length->value.integer,
787 fts->u.cl->length->value.integer) != 0)))
788 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
789 "entries returning variables of different "
790 "string lengths", ns->entries->sym->name,
791 &ns->entries->sym->declared_at);
792 }
793
794 if (el == NULL)
795 {
796 sym = ns->entries->sym->result;
797 /* All result types the same. */
798 proc->ts = *fts;
799 if (sym->attr.dimension)
800 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
801 if (sym->attr.pointer)
802 gfc_add_pointer (&proc->attr, NULL);
803 }
804 else
805 {
806 /* Otherwise the result will be passed through a union by
807 reference. */
808 proc->attr.mixed_entry_master = 1;
809 for (el = ns->entries; el; el = el->next)
810 {
811 sym = el->sym->result;
812 if (sym->attr.dimension)
813 {
814 if (el == ns->entries)
815 gfc_error ("FUNCTION result %s can't be an array in "
816 "FUNCTION %s at %L", sym->name,
817 ns->entries->sym->name, &sym->declared_at);
818 else
819 gfc_error ("ENTRY result %s can't be an array in "
820 "FUNCTION %s at %L", sym->name,
821 ns->entries->sym->name, &sym->declared_at);
822 }
823 else if (sym->attr.pointer)
824 {
825 if (el == ns->entries)
826 gfc_error ("FUNCTION result %s can't be a POINTER in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
829 else
830 gfc_error ("ENTRY result %s can't be a POINTER in "
831 "FUNCTION %s at %L", sym->name,
832 ns->entries->sym->name, &sym->declared_at);
833 }
834 else
835 {
836 ts = &sym->ts;
837 if (ts->type == BT_UNKNOWN)
838 ts = gfc_get_default_type (sym->name, NULL);
839 switch (ts->type)
840 {
841 case BT_INTEGER:
842 if (ts->kind == gfc_default_integer_kind)
843 sym = NULL;
844 break;
845 case BT_REAL:
846 if (ts->kind == gfc_default_real_kind
847 || ts->kind == gfc_default_double_kind)
848 sym = NULL;
849 break;
850 case BT_COMPLEX:
851 if (ts->kind == gfc_default_complex_kind)
852 sym = NULL;
853 break;
854 case BT_LOGICAL:
855 if (ts->kind == gfc_default_logical_kind)
856 sym = NULL;
857 break;
858 case BT_UNKNOWN:
859 /* We will issue error elsewhere. */
860 sym = NULL;
861 break;
862 default:
863 break;
864 }
865 if (sym)
866 {
867 if (el == ns->entries)
868 gfc_error ("FUNCTION result %s can't be of type %s "
869 "in FUNCTION %s at %L", sym->name,
870 gfc_typename (ts), ns->entries->sym->name,
871 &sym->declared_at);
872 else
873 gfc_error ("ENTRY result %s can't be of type %s "
874 "in FUNCTION %s at %L", sym->name,
875 gfc_typename (ts), ns->entries->sym->name,
876 &sym->declared_at);
877 }
878 }
879 }
880 }
881 }
882 proc->attr.access = ACCESS_PRIVATE;
883 proc->attr.entry_master = 1;
884
885 /* Merge all the entry point arguments. */
886 for (el = ns->entries; el; el = el->next)
887 merge_argument_lists (proc, el->sym->formal);
888
889 /* Check the master formal arguments for any that are not
890 present in all entry points. */
891 for (el = ns->entries; el; el = el->next)
892 check_argument_lists (proc, el->sym->formal);
893
894 /* Use the master function for the function body. */
895 ns->proc_name = proc;
896
897 /* Finalize the new symbols. */
898 gfc_commit_symbols ();
899
900 /* Restore the original namespace. */
901 gfc_current_ns = old_ns;
902 }
903
904
905 /* Resolve common variables. */
906 static void
907 resolve_common_vars (gfc_symbol *sym, bool named_common)
908 {
909 gfc_symbol *csym = sym;
910
911 for (; csym; csym = csym->common_next)
912 {
913 if (csym->value || csym->attr.data)
914 {
915 if (!csym->ns->is_block_data)
916 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
917 "but only in BLOCK DATA initialization is "
918 "allowed", csym->name, &csym->declared_at);
919 else if (!named_common)
920 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
921 "in a blank COMMON but initialization is only "
922 "allowed in named common blocks", csym->name,
923 &csym->declared_at);
924 }
925
926 if (UNLIMITED_POLY (csym))
927 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
928 "[F2008:C5100]", csym->name, &csym->declared_at);
929
930 if (csym->ts.type != BT_DERIVED)
931 continue;
932
933 if (!(csym->ts.u.derived->attr.sequence
934 || csym->ts.u.derived->attr.is_bind_c))
935 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
936 "has neither the SEQUENCE nor the BIND(C) "
937 "attribute", csym->name, &csym->declared_at);
938 if (csym->ts.u.derived->attr.alloc_comp)
939 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
940 "has an ultimate component that is "
941 "allocatable", csym->name, &csym->declared_at);
942 if (gfc_has_default_initializer (csym->ts.u.derived))
943 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
944 "may not have default initializer", csym->name,
945 &csym->declared_at);
946
947 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
948 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
949 }
950 }
951
952 /* Resolve common blocks. */
953 static void
954 resolve_common_blocks (gfc_symtree *common_root)
955 {
956 gfc_symbol *sym;
957 gfc_gsymbol * gsym;
958
959 if (common_root == NULL)
960 return;
961
962 if (common_root->left)
963 resolve_common_blocks (common_root->left);
964 if (common_root->right)
965 resolve_common_blocks (common_root->right);
966
967 resolve_common_vars (common_root->n.common->head, true);
968
969 /* The common name is a global name - in Fortran 2003 also if it has a
970 C binding name, since Fortran 2008 only the C binding name is a global
971 identifier. */
972 if (!common_root->n.common->binding_label
973 || gfc_notification_std (GFC_STD_F2008))
974 {
975 gsym = gfc_find_gsymbol (gfc_gsym_root,
976 common_root->n.common->name);
977
978 if (gsym && gfc_notification_std (GFC_STD_F2008)
979 && gsym->type == GSYM_COMMON
980 && ((common_root->n.common->binding_label
981 && (!gsym->binding_label
982 || strcmp (common_root->n.common->binding_label,
983 gsym->binding_label) != 0))
984 || (!common_root->n.common->binding_label
985 && gsym->binding_label)))
986 {
987 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
988 "identifier and must thus have the same binding name "
989 "as the same-named COMMON block at %L: %s vs %s",
990 common_root->n.common->name, &common_root->n.common->where,
991 &gsym->where,
992 common_root->n.common->binding_label
993 ? common_root->n.common->binding_label : "(blank)",
994 gsym->binding_label ? gsym->binding_label : "(blank)");
995 return;
996 }
997
998 if (gsym && gsym->type != GSYM_COMMON
999 && !common_root->n.common->binding_label)
1000 {
1001 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
1002 "as entity at %L",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where);
1005 return;
1006 }
1007 if (gsym && gsym->type != GSYM_COMMON)
1008 {
1009 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
1010 "%L sharing the identifier with global non-COMMON-block "
1011 "entity at %L", common_root->n.common->name,
1012 &common_root->n.common->where, &gsym->where);
1013 return;
1014 }
1015 if (!gsym)
1016 {
1017 gsym = gfc_get_gsymbol (common_root->n.common->name);
1018 gsym->type = GSYM_COMMON;
1019 gsym->where = common_root->n.common->where;
1020 gsym->defined = 1;
1021 }
1022 gsym->used = 1;
1023 }
1024
1025 if (common_root->n.common->binding_label)
1026 {
1027 gsym = gfc_find_gsymbol (gfc_gsym_root,
1028 common_root->n.common->binding_label);
1029 if (gsym && gsym->type != GSYM_COMMON)
1030 {
1031 gfc_error ("COMMON block at %L with binding label %s uses the same "
1032 "global identifier as entity at %L",
1033 &common_root->n.common->where,
1034 common_root->n.common->binding_label, &gsym->where);
1035 return;
1036 }
1037 if (!gsym)
1038 {
1039 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1040 gsym->type = GSYM_COMMON;
1041 gsym->where = common_root->n.common->where;
1042 gsym->defined = 1;
1043 }
1044 gsym->used = 1;
1045 }
1046
1047 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1048 if (sym == NULL)
1049 return;
1050
1051 if (sym->attr.flavor == FL_PARAMETER)
1052 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1053 sym->name, &common_root->n.common->where, &sym->declared_at);
1054
1055 if (sym->attr.external)
1056 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1057 sym->name, &common_root->n.common->where);
1058
1059 if (sym->attr.intrinsic)
1060 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1061 sym->name, &common_root->n.common->where);
1062 else if (sym->attr.result
1063 || gfc_is_function_return_value (sym, gfc_current_ns))
1064 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1065 "that is also a function result", sym->name,
1066 &common_root->n.common->where);
1067 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1068 && sym->attr.proc != PROC_ST_FUNCTION)
1069 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1070 "that is also a global procedure", sym->name,
1071 &common_root->n.common->where);
1072 }
1073
1074
1075 /* Resolve contained function types. Because contained functions can call one
1076 another, they have to be worked out before any of the contained procedures
1077 can be resolved.
1078
1079 The good news is that if a function doesn't already have a type, the only
1080 way it can get one is through an IMPLICIT type or a RESULT variable, because
1081 by definition contained functions are contained namespace they're contained
1082 in, not in a sibling or parent namespace. */
1083
1084 static void
1085 resolve_contained_functions (gfc_namespace *ns)
1086 {
1087 gfc_namespace *child;
1088 gfc_entry_list *el;
1089
1090 resolve_formal_arglists (ns);
1091
1092 for (child = ns->contained; child; child = child->sibling)
1093 {
1094 /* Resolve alternate entry points first. */
1095 resolve_entries (child);
1096
1097 /* Then check function return types. */
1098 resolve_contained_fntype (child->proc_name, child);
1099 for (el = child->entries; el; el = el->next)
1100 resolve_contained_fntype (el->sym, child);
1101 }
1102 }
1103
1104
1105 static bool resolve_fl_derived0 (gfc_symbol *sym);
1106
1107
1108 /* Resolve all of the elements of a structure constructor and make sure that
1109 the types are correct. The 'init' flag indicates that the given
1110 constructor is an initializer. */
1111
1112 static bool
1113 resolve_structure_cons (gfc_expr *expr, int init)
1114 {
1115 gfc_constructor *cons;
1116 gfc_component *comp;
1117 bool t;
1118 symbol_attribute a;
1119
1120 t = true;
1121
1122 if (expr->ts.type == BT_DERIVED)
1123 resolve_fl_derived0 (expr->ts.u.derived);
1124
1125 cons = gfc_constructor_first (expr->value.constructor);
1126
1127 /* A constructor may have references if it is the result of substituting a
1128 parameter variable. In this case we just pull out the component we
1129 want. */
1130 if (expr->ref)
1131 comp = expr->ref->u.c.sym->components;
1132 else
1133 comp = expr->ts.u.derived->components;
1134
1135 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1136 {
1137 int rank;
1138
1139 if (!cons->expr)
1140 continue;
1141
1142 if (!gfc_resolve_expr (cons->expr))
1143 {
1144 t = false;
1145 continue;
1146 }
1147
1148 rank = comp->as ? comp->as->rank : 0;
1149 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1150 && (comp->attr.allocatable || cons->expr->rank))
1151 {
1152 gfc_error ("The rank of the element in the structure "
1153 "constructor at %L does not match that of the "
1154 "component (%d/%d)", &cons->expr->where,
1155 cons->expr->rank, rank);
1156 t = false;
1157 }
1158
1159 /* If we don't have the right type, try to convert it. */
1160
1161 if (!comp->attr.proc_pointer &&
1162 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1163 {
1164 if (strcmp (comp->name, "_extends") == 0)
1165 {
1166 /* Can afford to be brutal with the _extends initializer.
1167 The derived type can get lost because it is PRIVATE
1168 but it is not usage constrained by the standard. */
1169 cons->expr->ts = comp->ts;
1170 }
1171 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1172 {
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s', is %s but should be %s",
1175 &cons->expr->where, comp->name,
1176 gfc_basic_typename (cons->expr->ts.type),
1177 gfc_basic_typename (comp->ts.type));
1178 t = false;
1179 }
1180 else
1181 {
1182 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1183 if (t)
1184 t = t2;
1185 }
1186 }
1187
1188 /* For strings, the length of the constructor should be the same as
1189 the one of the structure, ensure this if the lengths are known at
1190 compile time and when we are dealing with PARAMETER or structure
1191 constructors. */
1192 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1193 && comp->ts.u.cl->length
1194 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1195 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1196 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1197 && cons->expr->rank != 0
1198 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1199 comp->ts.u.cl->length->value.integer) != 0)
1200 {
1201 if (cons->expr->expr_type == EXPR_VARIABLE
1202 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1203 {
1204 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1205 to make use of the gfc_resolve_character_array_constructor
1206 machinery. The expression is later simplified away to
1207 an array of string literals. */
1208 gfc_expr *para = cons->expr;
1209 cons->expr = gfc_get_expr ();
1210 cons->expr->ts = para->ts;
1211 cons->expr->where = para->where;
1212 cons->expr->expr_type = EXPR_ARRAY;
1213 cons->expr->rank = para->rank;
1214 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1215 gfc_constructor_append_expr (&cons->expr->value.constructor,
1216 para, &cons->expr->where);
1217 }
1218 if (cons->expr->expr_type == EXPR_ARRAY)
1219 {
1220 gfc_constructor *p;
1221 p = gfc_constructor_first (cons->expr->value.constructor);
1222 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1223 {
1224 gfc_charlen *cl, *cl2;
1225
1226 cl2 = NULL;
1227 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1228 {
1229 if (cl == cons->expr->ts.u.cl)
1230 break;
1231 cl2 = cl;
1232 }
1233
1234 gcc_assert (cl);
1235
1236 if (cl2)
1237 cl2->next = cl->next;
1238
1239 gfc_free_expr (cl->length);
1240 free (cl);
1241 }
1242
1243 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1244 cons->expr->ts.u.cl->length_from_typespec = true;
1245 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1246 gfc_resolve_character_array_constructor (cons->expr);
1247 }
1248 }
1249
1250 if (cons->expr->expr_type == EXPR_NULL
1251 && !(comp->attr.pointer || comp->attr.allocatable
1252 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1253 || (comp->ts.type == BT_CLASS
1254 && (CLASS_DATA (comp)->attr.class_pointer
1255 || CLASS_DATA (comp)->attr.allocatable))))
1256 {
1257 t = false;
1258 gfc_error ("The NULL in the structure constructor at %L is "
1259 "being applied to component '%s', which is neither "
1260 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1261 comp->name);
1262 }
1263
1264 if (comp->attr.proc_pointer && comp->ts.interface)
1265 {
1266 /* Check procedure pointer interface. */
1267 gfc_symbol *s2 = NULL;
1268 gfc_component *c2;
1269 const char *name;
1270 char err[200];
1271
1272 c2 = gfc_get_proc_ptr_comp (cons->expr);
1273 if (c2)
1274 {
1275 s2 = c2->ts.interface;
1276 name = c2->name;
1277 }
1278 else if (cons->expr->expr_type == EXPR_FUNCTION)
1279 {
1280 s2 = cons->expr->symtree->n.sym->result;
1281 name = cons->expr->symtree->n.sym->result->name;
1282 }
1283 else if (cons->expr->expr_type != EXPR_NULL)
1284 {
1285 s2 = cons->expr->symtree->n.sym;
1286 name = cons->expr->symtree->n.sym->name;
1287 }
1288
1289 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1290 err, sizeof (err), NULL, NULL))
1291 {
1292 gfc_error ("Interface mismatch for procedure-pointer component "
1293 "'%s' in structure constructor at %L: %s",
1294 comp->name, &cons->expr->where, err);
1295 return false;
1296 }
1297 }
1298
1299 if (!comp->attr.pointer || comp->attr.proc_pointer
1300 || cons->expr->expr_type == EXPR_NULL)
1301 continue;
1302
1303 a = gfc_expr_attr (cons->expr);
1304
1305 if (!a.pointer && !a.target)
1306 {
1307 t = false;
1308 gfc_error ("The element in the structure constructor at %L, "
1309 "for pointer component '%s' should be a POINTER or "
1310 "a TARGET", &cons->expr->where, comp->name);
1311 }
1312
1313 if (init)
1314 {
1315 /* F08:C461. Additional checks for pointer initialization. */
1316 if (a.allocatable)
1317 {
1318 t = false;
1319 gfc_error ("Pointer initialization target at %L "
1320 "must not be ALLOCATABLE ", &cons->expr->where);
1321 }
1322 if (!a.save)
1323 {
1324 t = false;
1325 gfc_error ("Pointer initialization target at %L "
1326 "must have the SAVE attribute", &cons->expr->where);
1327 }
1328 }
1329
1330 /* F2003, C1272 (3). */
1331 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1332 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1333 || gfc_is_coindexed (cons->expr));
1334 if (impure && gfc_pure (NULL))
1335 {
1336 t = false;
1337 gfc_error ("Invalid expression in the structure constructor for "
1338 "pointer component '%s' at %L in PURE procedure",
1339 comp->name, &cons->expr->where);
1340 }
1341
1342 if (impure)
1343 gfc_unset_implicit_pure (NULL);
1344 }
1345
1346 return t;
1347 }
1348
1349
1350 /****************** Expression name resolution ******************/
1351
1352 /* Returns 0 if a symbol was not declared with a type or
1353 attribute declaration statement, nonzero otherwise. */
1354
1355 static int
1356 was_declared (gfc_symbol *sym)
1357 {
1358 symbol_attribute a;
1359
1360 a = sym->attr;
1361
1362 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1363 return 1;
1364
1365 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1366 || a.optional || a.pointer || a.save || a.target || a.volatile_
1367 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1368 || a.asynchronous || a.codimension)
1369 return 1;
1370
1371 return 0;
1372 }
1373
1374
1375 /* Determine if a symbol is generic or not. */
1376
1377 static int
1378 generic_sym (gfc_symbol *sym)
1379 {
1380 gfc_symbol *s;
1381
1382 if (sym->attr.generic ||
1383 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1384 return 1;
1385
1386 if (was_declared (sym) || sym->ns->parent == NULL)
1387 return 0;
1388
1389 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1390
1391 if (s != NULL)
1392 {
1393 if (s == sym)
1394 return 0;
1395 else
1396 return generic_sym (s);
1397 }
1398
1399 return 0;
1400 }
1401
1402
1403 /* Determine if a symbol is specific or not. */
1404
1405 static int
1406 specific_sym (gfc_symbol *sym)
1407 {
1408 gfc_symbol *s;
1409
1410 if (sym->attr.if_source == IFSRC_IFBODY
1411 || sym->attr.proc == PROC_MODULE
1412 || sym->attr.proc == PROC_INTERNAL
1413 || sym->attr.proc == PROC_ST_FUNCTION
1414 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1415 || sym->attr.external)
1416 return 1;
1417
1418 if (was_declared (sym) || sym->ns->parent == NULL)
1419 return 0;
1420
1421 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1422
1423 return (s == NULL) ? 0 : specific_sym (s);
1424 }
1425
1426
1427 /* Figure out if the procedure is specific, generic or unknown. */
1428
1429 typedef enum
1430 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1431 proc_type;
1432
1433 static proc_type
1434 procedure_kind (gfc_symbol *sym)
1435 {
1436 if (generic_sym (sym))
1437 return PTYPE_GENERIC;
1438
1439 if (specific_sym (sym))
1440 return PTYPE_SPECIFIC;
1441
1442 return PTYPE_UNKNOWN;
1443 }
1444
1445 /* Check references to assumed size arrays. The flag need_full_assumed_size
1446 is nonzero when matching actual arguments. */
1447
1448 static int need_full_assumed_size = 0;
1449
1450 static bool
1451 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1452 {
1453 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1454 return false;
1455
1456 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1457 What should it be? */
1458 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1459 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1460 && (e->ref->u.ar.type == AR_FULL))
1461 {
1462 gfc_error ("The upper bound in the last dimension must "
1463 "appear in the reference to the assumed size "
1464 "array '%s' at %L", sym->name, &e->where);
1465 return true;
1466 }
1467 return false;
1468 }
1469
1470
1471 /* Look for bad assumed size array references in argument expressions
1472 of elemental and array valued intrinsic procedures. Since this is
1473 called from procedure resolution functions, it only recurses at
1474 operators. */
1475
1476 static bool
1477 resolve_assumed_size_actual (gfc_expr *e)
1478 {
1479 if (e == NULL)
1480 return false;
1481
1482 switch (e->expr_type)
1483 {
1484 case EXPR_VARIABLE:
1485 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1486 return true;
1487 break;
1488
1489 case EXPR_OP:
1490 if (resolve_assumed_size_actual (e->value.op.op1)
1491 || resolve_assumed_size_actual (e->value.op.op2))
1492 return true;
1493 break;
1494
1495 default:
1496 break;
1497 }
1498 return false;
1499 }
1500
1501
1502 /* Check a generic procedure, passed as an actual argument, to see if
1503 there is a matching specific name. If none, it is an error, and if
1504 more than one, the reference is ambiguous. */
1505 static int
1506 count_specific_procs (gfc_expr *e)
1507 {
1508 int n;
1509 gfc_interface *p;
1510 gfc_symbol *sym;
1511
1512 n = 0;
1513 sym = e->symtree->n.sym;
1514
1515 for (p = sym->generic; p; p = p->next)
1516 if (strcmp (sym->name, p->sym->name) == 0)
1517 {
1518 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1519 sym->name);
1520 n++;
1521 }
1522
1523 if (n > 1)
1524 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1525 &e->where);
1526
1527 if (n == 0)
1528 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1529 "argument at %L", sym->name, &e->where);
1530
1531 return n;
1532 }
1533
1534
1535 /* See if a call to sym could possibly be a not allowed RECURSION because of
1536 a missing RECURSIVE declaration. This means that either sym is the current
1537 context itself, or sym is the parent of a contained procedure calling its
1538 non-RECURSIVE containing procedure.
1539 This also works if sym is an ENTRY. */
1540
1541 static bool
1542 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1543 {
1544 gfc_symbol* proc_sym;
1545 gfc_symbol* context_proc;
1546 gfc_namespace* real_context;
1547
1548 if (sym->attr.flavor == FL_PROGRAM
1549 || sym->attr.flavor == FL_DERIVED)
1550 return false;
1551
1552 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1553
1554 /* If we've got an ENTRY, find real procedure. */
1555 if (sym->attr.entry && sym->ns->entries)
1556 proc_sym = sym->ns->entries->sym;
1557 else
1558 proc_sym = sym;
1559
1560 /* If sym is RECURSIVE, all is well of course. */
1561 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1562 return false;
1563
1564 /* Find the context procedure's "real" symbol if it has entries.
1565 We look for a procedure symbol, so recurse on the parents if we don't
1566 find one (like in case of a BLOCK construct). */
1567 for (real_context = context; ; real_context = real_context->parent)
1568 {
1569 /* We should find something, eventually! */
1570 gcc_assert (real_context);
1571
1572 context_proc = (real_context->entries ? real_context->entries->sym
1573 : real_context->proc_name);
1574
1575 /* In some special cases, there may not be a proc_name, like for this
1576 invalid code:
1577 real(bad_kind()) function foo () ...
1578 when checking the call to bad_kind ().
1579 In these cases, we simply return here and assume that the
1580 call is ok. */
1581 if (!context_proc)
1582 return false;
1583
1584 if (context_proc->attr.flavor != FL_LABEL)
1585 break;
1586 }
1587
1588 /* A call from sym's body to itself is recursion, of course. */
1589 if (context_proc == proc_sym)
1590 return true;
1591
1592 /* The same is true if context is a contained procedure and sym the
1593 containing one. */
1594 if (context_proc->attr.contained)
1595 {
1596 gfc_symbol* parent_proc;
1597
1598 gcc_assert (context->parent);
1599 parent_proc = (context->parent->entries ? context->parent->entries->sym
1600 : context->parent->proc_name);
1601
1602 if (parent_proc == proc_sym)
1603 return true;
1604 }
1605
1606 return false;
1607 }
1608
1609
1610 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1611 its typespec and formal argument list. */
1612
1613 bool
1614 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1615 {
1616 gfc_intrinsic_sym* isym = NULL;
1617 const char* symstd;
1618
1619 if (sym->formal)
1620 return true;
1621
1622 /* Already resolved. */
1623 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1624 return true;
1625
1626 /* We already know this one is an intrinsic, so we don't call
1627 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1628 gfc_find_subroutine directly to check whether it is a function or
1629 subroutine. */
1630
1631 if (sym->intmod_sym_id && sym->attr.subroutine)
1632 {
1633 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1634 isym = gfc_intrinsic_subroutine_by_id (id);
1635 }
1636 else if (sym->intmod_sym_id)
1637 {
1638 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1639 isym = gfc_intrinsic_function_by_id (id);
1640 }
1641 else if (!sym->attr.subroutine)
1642 isym = gfc_find_function (sym->name);
1643
1644 if (isym && !sym->attr.subroutine)
1645 {
1646 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1647 && !sym->attr.implicit_type)
1648 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1649 " ignored", sym->name, &sym->declared_at);
1650
1651 if (!sym->attr.function &&
1652 !gfc_add_function(&sym->attr, sym->name, loc))
1653 return false;
1654
1655 sym->ts = isym->ts;
1656 }
1657 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1658 {
1659 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1660 {
1661 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1662 " specifier", sym->name, &sym->declared_at);
1663 return false;
1664 }
1665
1666 if (!sym->attr.subroutine &&
1667 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1668 return false;
1669 }
1670 else
1671 {
1672 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1673 &sym->declared_at);
1674 return false;
1675 }
1676
1677 gfc_copy_formal_args_intr (sym, isym);
1678
1679 sym->attr.pure = isym->pure;
1680 sym->attr.elemental = isym->elemental;
1681
1682 /* Check it is actually available in the standard settings. */
1683 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1684 {
1685 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1686 " available in the current standard settings but %s. Use"
1687 " an appropriate -std=* option or enable -fall-intrinsics"
1688 " in order to use it.",
1689 sym->name, &sym->declared_at, symstd);
1690 return false;
1691 }
1692
1693 return true;
1694 }
1695
1696
1697 /* Resolve a procedure expression, like passing it to a called procedure or as
1698 RHS for a procedure pointer assignment. */
1699
1700 static bool
1701 resolve_procedure_expression (gfc_expr* expr)
1702 {
1703 gfc_symbol* sym;
1704
1705 if (expr->expr_type != EXPR_VARIABLE)
1706 return true;
1707 gcc_assert (expr->symtree);
1708
1709 sym = expr->symtree->n.sym;
1710
1711 if (sym->attr.intrinsic)
1712 gfc_resolve_intrinsic (sym, &expr->where);
1713
1714 if (sym->attr.flavor != FL_PROCEDURE
1715 || (sym->attr.function && sym->result == sym))
1716 return true;
1717
1718 /* A non-RECURSIVE procedure that is used as procedure expression within its
1719 own body is in danger of being called recursively. */
1720 if (is_illegal_recursion (sym, gfc_current_ns))
1721 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1722 " itself recursively. Declare it RECURSIVE or use"
1723 " -frecursive", sym->name, &expr->where);
1724
1725 return true;
1726 }
1727
1728
1729 /* Resolve an actual argument list. Most of the time, this is just
1730 resolving the expressions in the list.
1731 The exception is that we sometimes have to decide whether arguments
1732 that look like procedure arguments are really simple variable
1733 references. */
1734
1735 static bool
1736 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1737 bool no_formal_args)
1738 {
1739 gfc_symbol *sym;
1740 gfc_symtree *parent_st;
1741 gfc_expr *e;
1742 int save_need_full_assumed_size;
1743 bool return_value = false;
1744 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1745
1746 actual_arg = true;
1747 first_actual_arg = true;
1748
1749 for (; arg; arg = arg->next)
1750 {
1751 e = arg->expr;
1752 if (e == NULL)
1753 {
1754 /* Check the label is a valid branching target. */
1755 if (arg->label)
1756 {
1757 if (arg->label->defined == ST_LABEL_UNKNOWN)
1758 {
1759 gfc_error ("Label %d referenced at %L is never defined",
1760 arg->label->value, &arg->label->where);
1761 goto cleanup;
1762 }
1763 }
1764 first_actual_arg = false;
1765 continue;
1766 }
1767
1768 if (e->expr_type == EXPR_VARIABLE
1769 && e->symtree->n.sym->attr.generic
1770 && no_formal_args
1771 && count_specific_procs (e) != 1)
1772 goto cleanup;
1773
1774 if (e->ts.type != BT_PROCEDURE)
1775 {
1776 save_need_full_assumed_size = need_full_assumed_size;
1777 if (e->expr_type != EXPR_VARIABLE)
1778 need_full_assumed_size = 0;
1779 if (!gfc_resolve_expr (e))
1780 goto cleanup;
1781 need_full_assumed_size = save_need_full_assumed_size;
1782 goto argument_list;
1783 }
1784
1785 /* See if the expression node should really be a variable reference. */
1786
1787 sym = e->symtree->n.sym;
1788
1789 if (sym->attr.flavor == FL_PROCEDURE
1790 || sym->attr.intrinsic
1791 || sym->attr.external)
1792 {
1793 int actual_ok;
1794
1795 /* If a procedure is not already determined to be something else
1796 check if it is intrinsic. */
1797 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1798 sym->attr.intrinsic = 1;
1799
1800 if (sym->attr.proc == PROC_ST_FUNCTION)
1801 {
1802 gfc_error ("Statement function '%s' at %L is not allowed as an "
1803 "actual argument", sym->name, &e->where);
1804 }
1805
1806 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1807 sym->attr.subroutine);
1808 if (sym->attr.intrinsic && actual_ok == 0)
1809 {
1810 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1811 "actual argument", sym->name, &e->where);
1812 }
1813
1814 if (sym->attr.contained && !sym->attr.use_assoc
1815 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1816 {
1817 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1818 " used as actual argument at %L",
1819 sym->name, &e->where))
1820 goto cleanup;
1821 }
1822
1823 if (sym->attr.elemental && !sym->attr.intrinsic)
1824 {
1825 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1826 "allowed as an actual argument at %L", sym->name,
1827 &e->where);
1828 }
1829
1830 /* Check if a generic interface has a specific procedure
1831 with the same name before emitting an error. */
1832 if (sym->attr.generic && count_specific_procs (e) != 1)
1833 goto cleanup;
1834
1835 /* Just in case a specific was found for the expression. */
1836 sym = e->symtree->n.sym;
1837
1838 /* If the symbol is the function that names the current (or
1839 parent) scope, then we really have a variable reference. */
1840
1841 if (gfc_is_function_return_value (sym, sym->ns))
1842 goto got_variable;
1843
1844 /* If all else fails, see if we have a specific intrinsic. */
1845 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1846 {
1847 gfc_intrinsic_sym *isym;
1848
1849 isym = gfc_find_function (sym->name);
1850 if (isym == NULL || !isym->specific)
1851 {
1852 gfc_error ("Unable to find a specific INTRINSIC procedure "
1853 "for the reference '%s' at %L", sym->name,
1854 &e->where);
1855 goto cleanup;
1856 }
1857 sym->ts = isym->ts;
1858 sym->attr.intrinsic = 1;
1859 sym->attr.function = 1;
1860 }
1861
1862 if (!gfc_resolve_expr (e))
1863 goto cleanup;
1864 goto argument_list;
1865 }
1866
1867 /* See if the name is a module procedure in a parent unit. */
1868
1869 if (was_declared (sym) || sym->ns->parent == NULL)
1870 goto got_variable;
1871
1872 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1873 {
1874 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1875 goto cleanup;
1876 }
1877
1878 if (parent_st == NULL)
1879 goto got_variable;
1880
1881 sym = parent_st->n.sym;
1882 e->symtree = parent_st; /* Point to the right thing. */
1883
1884 if (sym->attr.flavor == FL_PROCEDURE
1885 || sym->attr.intrinsic
1886 || sym->attr.external)
1887 {
1888 if (!gfc_resolve_expr (e))
1889 goto cleanup;
1890 goto argument_list;
1891 }
1892
1893 got_variable:
1894 e->expr_type = EXPR_VARIABLE;
1895 e->ts = sym->ts;
1896 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1897 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1898 && CLASS_DATA (sym)->as))
1899 {
1900 e->rank = sym->ts.type == BT_CLASS
1901 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1902 e->ref = gfc_get_ref ();
1903 e->ref->type = REF_ARRAY;
1904 e->ref->u.ar.type = AR_FULL;
1905 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1906 ? CLASS_DATA (sym)->as : sym->as;
1907 }
1908
1909 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1910 primary.c (match_actual_arg). If above code determines that it
1911 is a variable instead, it needs to be resolved as it was not
1912 done at the beginning of this function. */
1913 save_need_full_assumed_size = need_full_assumed_size;
1914 if (e->expr_type != EXPR_VARIABLE)
1915 need_full_assumed_size = 0;
1916 if (!gfc_resolve_expr (e))
1917 goto cleanup;
1918 need_full_assumed_size = save_need_full_assumed_size;
1919
1920 argument_list:
1921 /* Check argument list functions %VAL, %LOC and %REF. There is
1922 nothing to do for %REF. */
1923 if (arg->name && arg->name[0] == '%')
1924 {
1925 if (strncmp ("%VAL", arg->name, 4) == 0)
1926 {
1927 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1928 {
1929 gfc_error ("By-value argument at %L is not of numeric "
1930 "type", &e->where);
1931 goto cleanup;
1932 }
1933
1934 if (e->rank)
1935 {
1936 gfc_error ("By-value argument at %L cannot be an array or "
1937 "an array section", &e->where);
1938 goto cleanup;
1939 }
1940
1941 /* Intrinsics are still PROC_UNKNOWN here. However,
1942 since same file external procedures are not resolvable
1943 in gfortran, it is a good deal easier to leave them to
1944 intrinsic.c. */
1945 if (ptype != PROC_UNKNOWN
1946 && ptype != PROC_DUMMY
1947 && ptype != PROC_EXTERNAL
1948 && ptype != PROC_MODULE)
1949 {
1950 gfc_error ("By-value argument at %L is not allowed "
1951 "in this context", &e->where);
1952 goto cleanup;
1953 }
1954 }
1955
1956 /* Statement functions have already been excluded above. */
1957 else if (strncmp ("%LOC", arg->name, 4) == 0
1958 && e->ts.type == BT_PROCEDURE)
1959 {
1960 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1961 {
1962 gfc_error ("Passing internal procedure at %L by location "
1963 "not allowed", &e->where);
1964 goto cleanup;
1965 }
1966 }
1967 }
1968
1969 /* Fortran 2008, C1237. */
1970 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1971 && gfc_has_ultimate_pointer (e))
1972 {
1973 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1974 "component", &e->where);
1975 goto cleanup;
1976 }
1977
1978 first_actual_arg = false;
1979 }
1980
1981 return_value = true;
1982
1983 cleanup:
1984 actual_arg = actual_arg_sav;
1985 first_actual_arg = first_actual_arg_sav;
1986
1987 return return_value;
1988 }
1989
1990
1991 /* Do the checks of the actual argument list that are specific to elemental
1992 procedures. If called with c == NULL, we have a function, otherwise if
1993 expr == NULL, we have a subroutine. */
1994
1995 static bool
1996 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1997 {
1998 gfc_actual_arglist *arg0;
1999 gfc_actual_arglist *arg;
2000 gfc_symbol *esym = NULL;
2001 gfc_intrinsic_sym *isym = NULL;
2002 gfc_expr *e = NULL;
2003 gfc_intrinsic_arg *iformal = NULL;
2004 gfc_formal_arglist *eformal = NULL;
2005 bool formal_optional = false;
2006 bool set_by_optional = false;
2007 int i;
2008 int rank = 0;
2009
2010 /* Is this an elemental procedure? */
2011 if (expr && expr->value.function.actual != NULL)
2012 {
2013 if (expr->value.function.esym != NULL
2014 && expr->value.function.esym->attr.elemental)
2015 {
2016 arg0 = expr->value.function.actual;
2017 esym = expr->value.function.esym;
2018 }
2019 else if (expr->value.function.isym != NULL
2020 && expr->value.function.isym->elemental)
2021 {
2022 arg0 = expr->value.function.actual;
2023 isym = expr->value.function.isym;
2024 }
2025 else
2026 return true;
2027 }
2028 else if (c && c->ext.actual != NULL)
2029 {
2030 arg0 = c->ext.actual;
2031
2032 if (c->resolved_sym)
2033 esym = c->resolved_sym;
2034 else
2035 esym = c->symtree->n.sym;
2036 gcc_assert (esym);
2037
2038 if (!esym->attr.elemental)
2039 return true;
2040 }
2041 else
2042 return true;
2043
2044 /* The rank of an elemental is the rank of its array argument(s). */
2045 for (arg = arg0; arg; arg = arg->next)
2046 {
2047 if (arg->expr != NULL && arg->expr->rank != 0)
2048 {
2049 rank = arg->expr->rank;
2050 if (arg->expr->expr_type == EXPR_VARIABLE
2051 && arg->expr->symtree->n.sym->attr.optional)
2052 set_by_optional = true;
2053
2054 /* Function specific; set the result rank and shape. */
2055 if (expr)
2056 {
2057 expr->rank = rank;
2058 if (!expr->shape && arg->expr->shape)
2059 {
2060 expr->shape = gfc_get_shape (rank);
2061 for (i = 0; i < rank; i++)
2062 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2063 }
2064 }
2065 break;
2066 }
2067 }
2068
2069 /* If it is an array, it shall not be supplied as an actual argument
2070 to an elemental procedure unless an array of the same rank is supplied
2071 as an actual argument corresponding to a nonoptional dummy argument of
2072 that elemental procedure(12.4.1.5). */
2073 formal_optional = false;
2074 if (isym)
2075 iformal = isym->formal;
2076 else
2077 eformal = esym->formal;
2078
2079 for (arg = arg0; arg; arg = arg->next)
2080 {
2081 if (eformal)
2082 {
2083 if (eformal->sym && eformal->sym->attr.optional)
2084 formal_optional = true;
2085 eformal = eformal->next;
2086 }
2087 else if (isym && iformal)
2088 {
2089 if (iformal->optional)
2090 formal_optional = true;
2091 iformal = iformal->next;
2092 }
2093 else if (isym)
2094 formal_optional = true;
2095
2096 if (pedantic && arg->expr != NULL
2097 && arg->expr->expr_type == EXPR_VARIABLE
2098 && arg->expr->symtree->n.sym->attr.optional
2099 && formal_optional
2100 && arg->expr->rank
2101 && (set_by_optional || arg->expr->rank != rank)
2102 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2103 {
2104 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2105 "MISSING, it cannot be the actual argument of an "
2106 "ELEMENTAL procedure unless there is a non-optional "
2107 "argument with the same rank (12.4.1.5)",
2108 arg->expr->symtree->n.sym->name, &arg->expr->where);
2109 }
2110 }
2111
2112 for (arg = arg0; arg; arg = arg->next)
2113 {
2114 if (arg->expr == NULL || arg->expr->rank == 0)
2115 continue;
2116
2117 /* Being elemental, the last upper bound of an assumed size array
2118 argument must be present. */
2119 if (resolve_assumed_size_actual (arg->expr))
2120 return false;
2121
2122 /* Elemental procedure's array actual arguments must conform. */
2123 if (e != NULL)
2124 {
2125 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2126 return false;
2127 }
2128 else
2129 e = arg->expr;
2130 }
2131
2132 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2133 is an array, the intent inout/out variable needs to be also an array. */
2134 if (rank > 0 && esym && expr == NULL)
2135 for (eformal = esym->formal, arg = arg0; arg && eformal;
2136 arg = arg->next, eformal = eformal->next)
2137 if ((eformal->sym->attr.intent == INTENT_OUT
2138 || eformal->sym->attr.intent == INTENT_INOUT)
2139 && arg->expr && arg->expr->rank == 0)
2140 {
2141 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2142 "ELEMENTAL subroutine '%s' is a scalar, but another "
2143 "actual argument is an array", &arg->expr->where,
2144 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2145 : "INOUT", eformal->sym->name, esym->name);
2146 return false;
2147 }
2148 return true;
2149 }
2150
2151
2152 /* This function does the checking of references to global procedures
2153 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2154 77 and 95 standards. It checks for a gsymbol for the name, making
2155 one if it does not already exist. If it already exists, then the
2156 reference being resolved must correspond to the type of gsymbol.
2157 Otherwise, the new symbol is equipped with the attributes of the
2158 reference. The corresponding code that is called in creating
2159 global entities is parse.c.
2160
2161 In addition, for all but -std=legacy, the gsymbols are used to
2162 check the interfaces of external procedures from the same file.
2163 The namespace of the gsymbol is resolved and then, once this is
2164 done the interface is checked. */
2165
2166
2167 static bool
2168 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2169 {
2170 if (!gsym_ns->proc_name->attr.recursive)
2171 return true;
2172
2173 if (sym->ns == gsym_ns)
2174 return false;
2175
2176 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2177 return false;
2178
2179 return true;
2180 }
2181
2182 static bool
2183 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2184 {
2185 if (gsym_ns->entries)
2186 {
2187 gfc_entry_list *entry = gsym_ns->entries;
2188
2189 for (; entry; entry = entry->next)
2190 {
2191 if (strcmp (sym->name, entry->sym->name) == 0)
2192 {
2193 if (strcmp (gsym_ns->proc_name->name,
2194 sym->ns->proc_name->name) == 0)
2195 return false;
2196
2197 if (sym->ns->parent
2198 && strcmp (gsym_ns->proc_name->name,
2199 sym->ns->parent->proc_name->name) == 0)
2200 return false;
2201 }
2202 }
2203 }
2204 return true;
2205 }
2206
2207
2208 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2209
2210 bool
2211 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2212 {
2213 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2214
2215 for ( ; arg; arg = arg->next)
2216 {
2217 if (!arg->sym)
2218 continue;
2219
2220 if (arg->sym->attr.allocatable) /* (2a) */
2221 {
2222 strncpy (errmsg, _("allocatable argument"), err_len);
2223 return true;
2224 }
2225 else if (arg->sym->attr.asynchronous)
2226 {
2227 strncpy (errmsg, _("asynchronous argument"), err_len);
2228 return true;
2229 }
2230 else if (arg->sym->attr.optional)
2231 {
2232 strncpy (errmsg, _("optional argument"), err_len);
2233 return true;
2234 }
2235 else if (arg->sym->attr.pointer)
2236 {
2237 strncpy (errmsg, _("pointer argument"), err_len);
2238 return true;
2239 }
2240 else if (arg->sym->attr.target)
2241 {
2242 strncpy (errmsg, _("target argument"), err_len);
2243 return true;
2244 }
2245 else if (arg->sym->attr.value)
2246 {
2247 strncpy (errmsg, _("value argument"), err_len);
2248 return true;
2249 }
2250 else if (arg->sym->attr.volatile_)
2251 {
2252 strncpy (errmsg, _("volatile argument"), err_len);
2253 return true;
2254 }
2255 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2256 {
2257 strncpy (errmsg, _("assumed-shape argument"), err_len);
2258 return true;
2259 }
2260 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2261 {
2262 strncpy (errmsg, _("assumed-rank argument"), err_len);
2263 return true;
2264 }
2265 else if (arg->sym->attr.codimension) /* (2c) */
2266 {
2267 strncpy (errmsg, _("coarray argument"), err_len);
2268 return true;
2269 }
2270 else if (false) /* (2d) TODO: parametrized derived type */
2271 {
2272 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2273 return true;
2274 }
2275 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2276 {
2277 strncpy (errmsg, _("polymorphic argument"), err_len);
2278 return true;
2279 }
2280 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2281 {
2282 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2283 return true;
2284 }
2285 else if (arg->sym->ts.type == BT_ASSUMED)
2286 {
2287 /* As assumed-type is unlimited polymorphic (cf. above).
2288 See also TS 29113, Note 6.1. */
2289 strncpy (errmsg, _("assumed-type argument"), err_len);
2290 return true;
2291 }
2292 }
2293
2294 if (sym->attr.function)
2295 {
2296 gfc_symbol *res = sym->result ? sym->result : sym;
2297
2298 if (res->attr.dimension) /* (3a) */
2299 {
2300 strncpy (errmsg, _("array result"), err_len);
2301 return true;
2302 }
2303 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2304 {
2305 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2306 return true;
2307 }
2308 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2309 && res->ts.u.cl->length
2310 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2311 {
2312 strncpy (errmsg, _("result with non-constant character length"), err_len);
2313 return true;
2314 }
2315 }
2316
2317 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2318 {
2319 strncpy (errmsg, _("elemental procedure"), err_len);
2320 return true;
2321 }
2322 else if (sym->attr.is_bind_c) /* (5) */
2323 {
2324 strncpy (errmsg, _("bind(c) procedure"), err_len);
2325 return true;
2326 }
2327
2328 return false;
2329 }
2330
2331
2332 static void
2333 resolve_global_procedure (gfc_symbol *sym, locus *where,
2334 gfc_actual_arglist **actual, int sub)
2335 {
2336 gfc_gsymbol * gsym;
2337 gfc_namespace *ns;
2338 enum gfc_symbol_type type;
2339 char reason[200];
2340
2341 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2342
2343 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2344
2345 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2346 gfc_global_used (gsym, where);
2347
2348 if ((sym->attr.if_source == IFSRC_UNKNOWN
2349 || sym->attr.if_source == IFSRC_IFBODY)
2350 && gsym->type != GSYM_UNKNOWN
2351 && !gsym->binding_label
2352 && gsym->ns
2353 && gsym->ns->resolved != -1
2354 && gsym->ns->proc_name
2355 && not_in_recursive (sym, gsym->ns)
2356 && not_entry_self_reference (sym, gsym->ns))
2357 {
2358 gfc_symbol *def_sym;
2359
2360 /* Resolve the gsymbol namespace if needed. */
2361 if (!gsym->ns->resolved)
2362 {
2363 gfc_dt_list *old_dt_list;
2364 struct gfc_omp_saved_state old_omp_state;
2365
2366 /* Stash away derived types so that the backend_decls do not
2367 get mixed up. */
2368 old_dt_list = gfc_derived_types;
2369 gfc_derived_types = NULL;
2370 /* And stash away openmp state. */
2371 gfc_omp_save_and_clear_state (&old_omp_state);
2372
2373 gfc_resolve (gsym->ns);
2374
2375 /* Store the new derived types with the global namespace. */
2376 if (gfc_derived_types)
2377 gsym->ns->derived_types = gfc_derived_types;
2378
2379 /* Restore the derived types of this namespace. */
2380 gfc_derived_types = old_dt_list;
2381 /* And openmp state. */
2382 gfc_omp_restore_state (&old_omp_state);
2383 }
2384
2385 /* Make sure that translation for the gsymbol occurs before
2386 the procedure currently being resolved. */
2387 ns = gfc_global_ns_list;
2388 for (; ns && ns != gsym->ns; ns = ns->sibling)
2389 {
2390 if (ns->sibling == gsym->ns)
2391 {
2392 ns->sibling = gsym->ns->sibling;
2393 gsym->ns->sibling = gfc_global_ns_list;
2394 gfc_global_ns_list = gsym->ns;
2395 break;
2396 }
2397 }
2398
2399 def_sym = gsym->ns->proc_name;
2400
2401 /* This can happen if a binding name has been specified. */
2402 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2403 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2404
2405 if (def_sym->attr.entry_master)
2406 {
2407 gfc_entry_list *entry;
2408 for (entry = gsym->ns->entries; entry; entry = entry->next)
2409 if (strcmp (entry->sym->name, sym->name) == 0)
2410 {
2411 def_sym = entry->sym;
2412 break;
2413 }
2414 }
2415
2416 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2417 {
2418 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2419 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2420 gfc_typename (&def_sym->ts));
2421 goto done;
2422 }
2423
2424 if (sym->attr.if_source == IFSRC_UNKNOWN
2425 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2426 {
2427 gfc_error ("Explicit interface required for '%s' at %L: %s",
2428 sym->name, &sym->declared_at, reason);
2429 goto done;
2430 }
2431
2432 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2433 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2434 gfc_errors_to_warnings (1);
2435
2436 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2437 reason, sizeof(reason), NULL, NULL))
2438 {
2439 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2440 sym->name, &sym->declared_at, reason);
2441 goto done;
2442 }
2443
2444 if (!pedantic
2445 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2446 && !(gfc_option.warn_std & GFC_STD_GNU)))
2447 gfc_errors_to_warnings (1);
2448
2449 if (sym->attr.if_source != IFSRC_IFBODY)
2450 gfc_procedure_use (def_sym, actual, where);
2451 }
2452
2453 done:
2454 gfc_errors_to_warnings (0);
2455
2456 if (gsym->type == GSYM_UNKNOWN)
2457 {
2458 gsym->type = type;
2459 gsym->where = *where;
2460 }
2461
2462 gsym->used = 1;
2463 }
2464
2465
2466 /************* Function resolution *************/
2467
2468 /* Resolve a function call known to be generic.
2469 Section 14.1.2.4.1. */
2470
2471 static match
2472 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2473 {
2474 gfc_symbol *s;
2475
2476 if (sym->attr.generic)
2477 {
2478 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2479 if (s != NULL)
2480 {
2481 expr->value.function.name = s->name;
2482 expr->value.function.esym = s;
2483
2484 if (s->ts.type != BT_UNKNOWN)
2485 expr->ts = s->ts;
2486 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2487 expr->ts = s->result->ts;
2488
2489 if (s->as != NULL)
2490 expr->rank = s->as->rank;
2491 else if (s->result != NULL && s->result->as != NULL)
2492 expr->rank = s->result->as->rank;
2493
2494 gfc_set_sym_referenced (expr->value.function.esym);
2495
2496 return MATCH_YES;
2497 }
2498
2499 /* TODO: Need to search for elemental references in generic
2500 interface. */
2501 }
2502
2503 if (sym->attr.intrinsic)
2504 return gfc_intrinsic_func_interface (expr, 0);
2505
2506 return MATCH_NO;
2507 }
2508
2509
2510 static bool
2511 resolve_generic_f (gfc_expr *expr)
2512 {
2513 gfc_symbol *sym;
2514 match m;
2515 gfc_interface *intr = NULL;
2516
2517 sym = expr->symtree->n.sym;
2518
2519 for (;;)
2520 {
2521 m = resolve_generic_f0 (expr, sym);
2522 if (m == MATCH_YES)
2523 return true;
2524 else if (m == MATCH_ERROR)
2525 return false;
2526
2527 generic:
2528 if (!intr)
2529 for (intr = sym->generic; intr; intr = intr->next)
2530 if (intr->sym->attr.flavor == FL_DERIVED)
2531 break;
2532
2533 if (sym->ns->parent == NULL)
2534 break;
2535 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2536
2537 if (sym == NULL)
2538 break;
2539 if (!generic_sym (sym))
2540 goto generic;
2541 }
2542
2543 /* Last ditch attempt. See if the reference is to an intrinsic
2544 that possesses a matching interface. 14.1.2.4 */
2545 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2546 {
2547 gfc_error ("There is no specific function for the generic '%s' "
2548 "at %L", expr->symtree->n.sym->name, &expr->where);
2549 return false;
2550 }
2551
2552 if (intr)
2553 {
2554 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2555 NULL, false))
2556 return false;
2557 return resolve_structure_cons (expr, 0);
2558 }
2559
2560 m = gfc_intrinsic_func_interface (expr, 0);
2561 if (m == MATCH_YES)
2562 return true;
2563
2564 if (m == MATCH_NO)
2565 gfc_error ("Generic function '%s' at %L is not consistent with a "
2566 "specific intrinsic interface", expr->symtree->n.sym->name,
2567 &expr->where);
2568
2569 return false;
2570 }
2571
2572
2573 /* Resolve a function call known to be specific. */
2574
2575 static match
2576 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2577 {
2578 match m;
2579
2580 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2581 {
2582 if (sym->attr.dummy)
2583 {
2584 sym->attr.proc = PROC_DUMMY;
2585 goto found;
2586 }
2587
2588 sym->attr.proc = PROC_EXTERNAL;
2589 goto found;
2590 }
2591
2592 if (sym->attr.proc == PROC_MODULE
2593 || sym->attr.proc == PROC_ST_FUNCTION
2594 || sym->attr.proc == PROC_INTERNAL)
2595 goto found;
2596
2597 if (sym->attr.intrinsic)
2598 {
2599 m = gfc_intrinsic_func_interface (expr, 1);
2600 if (m == MATCH_YES)
2601 return MATCH_YES;
2602 if (m == MATCH_NO)
2603 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2604 "with an intrinsic", sym->name, &expr->where);
2605
2606 return MATCH_ERROR;
2607 }
2608
2609 return MATCH_NO;
2610
2611 found:
2612 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2613
2614 if (sym->result)
2615 expr->ts = sym->result->ts;
2616 else
2617 expr->ts = sym->ts;
2618 expr->value.function.name = sym->name;
2619 expr->value.function.esym = sym;
2620 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2621 expr->rank = CLASS_DATA (sym)->as->rank;
2622 else if (sym->as != NULL)
2623 expr->rank = sym->as->rank;
2624
2625 return MATCH_YES;
2626 }
2627
2628
2629 static bool
2630 resolve_specific_f (gfc_expr *expr)
2631 {
2632 gfc_symbol *sym;
2633 match m;
2634
2635 sym = expr->symtree->n.sym;
2636
2637 for (;;)
2638 {
2639 m = resolve_specific_f0 (sym, expr);
2640 if (m == MATCH_YES)
2641 return true;
2642 if (m == MATCH_ERROR)
2643 return false;
2644
2645 if (sym->ns->parent == NULL)
2646 break;
2647
2648 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2649
2650 if (sym == NULL)
2651 break;
2652 }
2653
2654 gfc_error ("Unable to resolve the specific function '%s' at %L",
2655 expr->symtree->n.sym->name, &expr->where);
2656
2657 return true;
2658 }
2659
2660
2661 /* Resolve a procedure call not known to be generic nor specific. */
2662
2663 static bool
2664 resolve_unknown_f (gfc_expr *expr)
2665 {
2666 gfc_symbol *sym;
2667 gfc_typespec *ts;
2668
2669 sym = expr->symtree->n.sym;
2670
2671 if (sym->attr.dummy)
2672 {
2673 sym->attr.proc = PROC_DUMMY;
2674 expr->value.function.name = sym->name;
2675 goto set_type;
2676 }
2677
2678 /* See if we have an intrinsic function reference. */
2679
2680 if (gfc_is_intrinsic (sym, 0, expr->where))
2681 {
2682 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2683 return true;
2684 return false;
2685 }
2686
2687 /* The reference is to an external name. */
2688
2689 sym->attr.proc = PROC_EXTERNAL;
2690 expr->value.function.name = sym->name;
2691 expr->value.function.esym = expr->symtree->n.sym;
2692
2693 if (sym->as != NULL)
2694 expr->rank = sym->as->rank;
2695
2696 /* Type of the expression is either the type of the symbol or the
2697 default type of the symbol. */
2698
2699 set_type:
2700 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2701
2702 if (sym->ts.type != BT_UNKNOWN)
2703 expr->ts = sym->ts;
2704 else
2705 {
2706 ts = gfc_get_default_type (sym->name, sym->ns);
2707
2708 if (ts->type == BT_UNKNOWN)
2709 {
2710 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2711 sym->name, &expr->where);
2712 return false;
2713 }
2714 else
2715 expr->ts = *ts;
2716 }
2717
2718 return true;
2719 }
2720
2721
2722 /* Return true, if the symbol is an external procedure. */
2723 static bool
2724 is_external_proc (gfc_symbol *sym)
2725 {
2726 if (!sym->attr.dummy && !sym->attr.contained
2727 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2728 && sym->attr.proc != PROC_ST_FUNCTION
2729 && !sym->attr.proc_pointer
2730 && !sym->attr.use_assoc
2731 && sym->name)
2732 return true;
2733
2734 return false;
2735 }
2736
2737
2738 /* Figure out if a function reference is pure or not. Also set the name
2739 of the function for a potential error message. Return nonzero if the
2740 function is PURE, zero if not. */
2741 static int
2742 pure_stmt_function (gfc_expr *, gfc_symbol *);
2743
2744 static int
2745 pure_function (gfc_expr *e, const char **name)
2746 {
2747 int pure;
2748
2749 *name = NULL;
2750
2751 if (e->symtree != NULL
2752 && e->symtree->n.sym != NULL
2753 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2754 return pure_stmt_function (e, e->symtree->n.sym);
2755
2756 if (e->value.function.esym)
2757 {
2758 pure = gfc_pure (e->value.function.esym);
2759 *name = e->value.function.esym->name;
2760 }
2761 else if (e->value.function.isym)
2762 {
2763 pure = e->value.function.isym->pure
2764 || e->value.function.isym->elemental;
2765 *name = e->value.function.isym->name;
2766 }
2767 else
2768 {
2769 /* Implicit functions are not pure. */
2770 pure = 0;
2771 *name = e->value.function.name;
2772 }
2773
2774 return pure;
2775 }
2776
2777
2778 static bool
2779 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2780 int *f ATTRIBUTE_UNUSED)
2781 {
2782 const char *name;
2783
2784 /* Don't bother recursing into other statement functions
2785 since they will be checked individually for purity. */
2786 if (e->expr_type != EXPR_FUNCTION
2787 || !e->symtree
2788 || e->symtree->n.sym == sym
2789 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2790 return false;
2791
2792 return pure_function (e, &name) ? false : true;
2793 }
2794
2795
2796 static int
2797 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2798 {
2799 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2800 }
2801
2802
2803 /* Resolve a function call, which means resolving the arguments, then figuring
2804 out which entity the name refers to. */
2805
2806 static bool
2807 resolve_function (gfc_expr *expr)
2808 {
2809 gfc_actual_arglist *arg;
2810 gfc_symbol *sym;
2811 const char *name;
2812 bool t;
2813 int temp;
2814 procedure_type p = PROC_INTRINSIC;
2815 bool no_formal_args;
2816
2817 sym = NULL;
2818 if (expr->symtree)
2819 sym = expr->symtree->n.sym;
2820
2821 /* If this is a procedure pointer component, it has already been resolved. */
2822 if (gfc_is_proc_ptr_comp (expr))
2823 return true;
2824
2825 if (sym && sym->attr.intrinsic
2826 && !gfc_resolve_intrinsic (sym, &expr->where))
2827 return false;
2828
2829 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2830 {
2831 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2832 return false;
2833 }
2834
2835 /* If this ia a deferred TBP with an abstract interface (which may
2836 of course be referenced), expr->value.function.esym will be set. */
2837 if (sym && sym->attr.abstract && !expr->value.function.esym)
2838 {
2839 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2840 sym->name, &expr->where);
2841 return false;
2842 }
2843
2844 /* Switch off assumed size checking and do this again for certain kinds
2845 of procedure, once the procedure itself is resolved. */
2846 need_full_assumed_size++;
2847
2848 if (expr->symtree && expr->symtree->n.sym)
2849 p = expr->symtree->n.sym->attr.proc;
2850
2851 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2852 inquiry_argument = true;
2853 no_formal_args = sym && is_external_proc (sym)
2854 && gfc_sym_get_dummy_args (sym) == NULL;
2855
2856 if (!resolve_actual_arglist (expr->value.function.actual,
2857 p, no_formal_args))
2858 {
2859 inquiry_argument = false;
2860 return false;
2861 }
2862
2863 inquiry_argument = false;
2864
2865 /* Resume assumed_size checking. */
2866 need_full_assumed_size--;
2867
2868 /* If the procedure is external, check for usage. */
2869 if (sym && is_external_proc (sym))
2870 resolve_global_procedure (sym, &expr->where,
2871 &expr->value.function.actual, 0);
2872
2873 if (sym && sym->ts.type == BT_CHARACTER
2874 && sym->ts.u.cl
2875 && sym->ts.u.cl->length == NULL
2876 && !sym->attr.dummy
2877 && !sym->ts.deferred
2878 && expr->value.function.esym == NULL
2879 && !sym->attr.contained)
2880 {
2881 /* Internal procedures are taken care of in resolve_contained_fntype. */
2882 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2883 "be used at %L since it is not a dummy argument",
2884 sym->name, &expr->where);
2885 return false;
2886 }
2887
2888 /* See if function is already resolved. */
2889
2890 if (expr->value.function.name != NULL)
2891 {
2892 if (expr->ts.type == BT_UNKNOWN)
2893 expr->ts = sym->ts;
2894 t = true;
2895 }
2896 else
2897 {
2898 /* Apply the rules of section 14.1.2. */
2899
2900 switch (procedure_kind (sym))
2901 {
2902 case PTYPE_GENERIC:
2903 t = resolve_generic_f (expr);
2904 break;
2905
2906 case PTYPE_SPECIFIC:
2907 t = resolve_specific_f (expr);
2908 break;
2909
2910 case PTYPE_UNKNOWN:
2911 t = resolve_unknown_f (expr);
2912 break;
2913
2914 default:
2915 gfc_internal_error ("resolve_function(): bad function type");
2916 }
2917 }
2918
2919 /* If the expression is still a function (it might have simplified),
2920 then we check to see if we are calling an elemental function. */
2921
2922 if (expr->expr_type != EXPR_FUNCTION)
2923 return t;
2924
2925 temp = need_full_assumed_size;
2926 need_full_assumed_size = 0;
2927
2928 if (!resolve_elemental_actual (expr, NULL))
2929 return false;
2930
2931 if (omp_workshare_flag
2932 && expr->value.function.esym
2933 && ! gfc_elemental (expr->value.function.esym))
2934 {
2935 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2936 "in WORKSHARE construct", expr->value.function.esym->name,
2937 &expr->where);
2938 t = false;
2939 }
2940
2941 #define GENERIC_ID expr->value.function.isym->id
2942 else if (expr->value.function.actual != NULL
2943 && expr->value.function.isym != NULL
2944 && GENERIC_ID != GFC_ISYM_LBOUND
2945 && GENERIC_ID != GFC_ISYM_LCOBOUND
2946 && GENERIC_ID != GFC_ISYM_UCOBOUND
2947 && GENERIC_ID != GFC_ISYM_LEN
2948 && GENERIC_ID != GFC_ISYM_LOC
2949 && GENERIC_ID != GFC_ISYM_C_LOC
2950 && GENERIC_ID != GFC_ISYM_PRESENT)
2951 {
2952 /* Array intrinsics must also have the last upper bound of an
2953 assumed size array argument. UBOUND and SIZE have to be
2954 excluded from the check if the second argument is anything
2955 than a constant. */
2956
2957 for (arg = expr->value.function.actual; arg; arg = arg->next)
2958 {
2959 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2960 && arg == expr->value.function.actual
2961 && arg->next != NULL && arg->next->expr)
2962 {
2963 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2964 break;
2965
2966 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2967 break;
2968
2969 if ((int)mpz_get_si (arg->next->expr->value.integer)
2970 < arg->expr->rank)
2971 break;
2972 }
2973
2974 if (arg->expr != NULL
2975 && arg->expr->rank > 0
2976 && resolve_assumed_size_actual (arg->expr))
2977 return false;
2978 }
2979 }
2980 #undef GENERIC_ID
2981
2982 need_full_assumed_size = temp;
2983 name = NULL;
2984
2985 if (!pure_function (expr, &name) && name)
2986 {
2987 if (forall_flag)
2988 {
2989 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2990 "FORALL %s", name, &expr->where,
2991 forall_flag == 2 ? "mask" : "block");
2992 t = false;
2993 }
2994 else if (gfc_do_concurrent_flag)
2995 {
2996 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2997 "DO CONCURRENT %s", name, &expr->where,
2998 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2999 t = false;
3000 }
3001 else if (gfc_pure (NULL))
3002 {
3003 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3004 "procedure within a PURE procedure", name, &expr->where);
3005 t = false;
3006 }
3007
3008 gfc_unset_implicit_pure (NULL);
3009 }
3010
3011 /* Functions without the RECURSIVE attribution are not allowed to
3012 * call themselves. */
3013 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3014 {
3015 gfc_symbol *esym;
3016 esym = expr->value.function.esym;
3017
3018 if (is_illegal_recursion (esym, gfc_current_ns))
3019 {
3020 if (esym->attr.entry && esym->ns->entries)
3021 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3022 " function '%s' is not RECURSIVE",
3023 esym->name, &expr->where, esym->ns->entries->sym->name);
3024 else
3025 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3026 " is not RECURSIVE", esym->name, &expr->where);
3027
3028 t = false;
3029 }
3030 }
3031
3032 /* Character lengths of use associated functions may contains references to
3033 symbols not referenced from the current program unit otherwise. Make sure
3034 those symbols are marked as referenced. */
3035
3036 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3037 && expr->value.function.esym->attr.use_assoc)
3038 {
3039 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3040 }
3041
3042 /* Make sure that the expression has a typespec that works. */
3043 if (expr->ts.type == BT_UNKNOWN)
3044 {
3045 if (expr->symtree->n.sym->result
3046 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3047 && !expr->symtree->n.sym->result->attr.proc_pointer)
3048 expr->ts = expr->symtree->n.sym->result->ts;
3049 }
3050
3051 return t;
3052 }
3053
3054
3055 /************* Subroutine resolution *************/
3056
3057 static void
3058 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3059 {
3060 if (gfc_pure (sym))
3061 return;
3062
3063 if (forall_flag)
3064 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3065 sym->name, &c->loc);
3066 else if (gfc_do_concurrent_flag)
3067 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3068 "PURE", sym->name, &c->loc);
3069 else if (gfc_pure (NULL))
3070 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3071 &c->loc);
3072
3073 gfc_unset_implicit_pure (NULL);
3074 }
3075
3076
3077 static match
3078 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3079 {
3080 gfc_symbol *s;
3081
3082 if (sym->attr.generic)
3083 {
3084 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3085 if (s != NULL)
3086 {
3087 c->resolved_sym = s;
3088 pure_subroutine (c, s);
3089 return MATCH_YES;
3090 }
3091
3092 /* TODO: Need to search for elemental references in generic interface. */
3093 }
3094
3095 if (sym->attr.intrinsic)
3096 return gfc_intrinsic_sub_interface (c, 0);
3097
3098 return MATCH_NO;
3099 }
3100
3101
3102 static bool
3103 resolve_generic_s (gfc_code *c)
3104 {
3105 gfc_symbol *sym;
3106 match m;
3107
3108 sym = c->symtree->n.sym;
3109
3110 for (;;)
3111 {
3112 m = resolve_generic_s0 (c, sym);
3113 if (m == MATCH_YES)
3114 return true;
3115 else if (m == MATCH_ERROR)
3116 return false;
3117
3118 generic:
3119 if (sym->ns->parent == NULL)
3120 break;
3121 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3122
3123 if (sym == NULL)
3124 break;
3125 if (!generic_sym (sym))
3126 goto generic;
3127 }
3128
3129 /* Last ditch attempt. See if the reference is to an intrinsic
3130 that possesses a matching interface. 14.1.2.4 */
3131 sym = c->symtree->n.sym;
3132
3133 if (!gfc_is_intrinsic (sym, 1, c->loc))
3134 {
3135 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3136 sym->name, &c->loc);
3137 return false;
3138 }
3139
3140 m = gfc_intrinsic_sub_interface (c, 0);
3141 if (m == MATCH_YES)
3142 return true;
3143 if (m == MATCH_NO)
3144 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3145 "intrinsic subroutine interface", sym->name, &c->loc);
3146
3147 return false;
3148 }
3149
3150
3151 /* Resolve a subroutine call known to be specific. */
3152
3153 static match
3154 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3155 {
3156 match m;
3157
3158 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3159 {
3160 if (sym->attr.dummy)
3161 {
3162 sym->attr.proc = PROC_DUMMY;
3163 goto found;
3164 }
3165
3166 sym->attr.proc = PROC_EXTERNAL;
3167 goto found;
3168 }
3169
3170 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3171 goto found;
3172
3173 if (sym->attr.intrinsic)
3174 {
3175 m = gfc_intrinsic_sub_interface (c, 1);
3176 if (m == MATCH_YES)
3177 return MATCH_YES;
3178 if (m == MATCH_NO)
3179 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3180 "with an intrinsic", sym->name, &c->loc);
3181
3182 return MATCH_ERROR;
3183 }
3184
3185 return MATCH_NO;
3186
3187 found:
3188 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3189
3190 c->resolved_sym = sym;
3191 pure_subroutine (c, sym);
3192
3193 return MATCH_YES;
3194 }
3195
3196
3197 static bool
3198 resolve_specific_s (gfc_code *c)
3199 {
3200 gfc_symbol *sym;
3201 match m;
3202
3203 sym = c->symtree->n.sym;
3204
3205 for (;;)
3206 {
3207 m = resolve_specific_s0 (c, sym);
3208 if (m == MATCH_YES)
3209 return true;
3210 if (m == MATCH_ERROR)
3211 return false;
3212
3213 if (sym->ns->parent == NULL)
3214 break;
3215
3216 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3217
3218 if (sym == NULL)
3219 break;
3220 }
3221
3222 sym = c->symtree->n.sym;
3223 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3224 sym->name, &c->loc);
3225
3226 return false;
3227 }
3228
3229
3230 /* Resolve a subroutine call not known to be generic nor specific. */
3231
3232 static bool
3233 resolve_unknown_s (gfc_code *c)
3234 {
3235 gfc_symbol *sym;
3236
3237 sym = c->symtree->n.sym;
3238
3239 if (sym->attr.dummy)
3240 {
3241 sym->attr.proc = PROC_DUMMY;
3242 goto found;
3243 }
3244
3245 /* See if we have an intrinsic function reference. */
3246
3247 if (gfc_is_intrinsic (sym, 1, c->loc))
3248 {
3249 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3250 return true;
3251 return false;
3252 }
3253
3254 /* The reference is to an external name. */
3255
3256 found:
3257 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3258
3259 c->resolved_sym = sym;
3260
3261 pure_subroutine (c, sym);
3262
3263 return true;
3264 }
3265
3266
3267 /* Resolve a subroutine call. Although it was tempting to use the same code
3268 for functions, subroutines and functions are stored differently and this
3269 makes things awkward. */
3270
3271 static bool
3272 resolve_call (gfc_code *c)
3273 {
3274 bool t;
3275 procedure_type ptype = PROC_INTRINSIC;
3276 gfc_symbol *csym, *sym;
3277 bool no_formal_args;
3278
3279 csym = c->symtree ? c->symtree->n.sym : NULL;
3280
3281 if (csym && csym->ts.type != BT_UNKNOWN)
3282 {
3283 gfc_error ("'%s' at %L has a type, which is not consistent with "
3284 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3285 return false;
3286 }
3287
3288 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3289 {
3290 gfc_symtree *st;
3291 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3292 sym = st ? st->n.sym : NULL;
3293 if (sym && csym != sym
3294 && sym->ns == gfc_current_ns
3295 && sym->attr.flavor == FL_PROCEDURE
3296 && sym->attr.contained)
3297 {
3298 sym->refs++;
3299 if (csym->attr.generic)
3300 c->symtree->n.sym = sym;
3301 else
3302 c->symtree = st;
3303 csym = c->symtree->n.sym;
3304 }
3305 }
3306
3307 /* If this ia a deferred TBP, c->expr1 will be set. */
3308 if (!c->expr1 && csym)
3309 {
3310 if (csym->attr.abstract)
3311 {
3312 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3313 csym->name, &c->loc);
3314 return false;
3315 }
3316
3317 /* Subroutines without the RECURSIVE attribution are not allowed to
3318 call themselves. */
3319 if (is_illegal_recursion (csym, gfc_current_ns))
3320 {
3321 if (csym->attr.entry && csym->ns->entries)
3322 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3323 "as subroutine '%s' is not RECURSIVE",
3324 csym->name, &c->loc, csym->ns->entries->sym->name);
3325 else
3326 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3327 "as it is not RECURSIVE", csym->name, &c->loc);
3328
3329 t = false;
3330 }
3331 }
3332
3333 /* Switch off assumed size checking and do this again for certain kinds
3334 of procedure, once the procedure itself is resolved. */
3335 need_full_assumed_size++;
3336
3337 if (csym)
3338 ptype = csym->attr.proc;
3339
3340 no_formal_args = csym && is_external_proc (csym)
3341 && gfc_sym_get_dummy_args (csym) == NULL;
3342 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3343 return false;
3344
3345 /* Resume assumed_size checking. */
3346 need_full_assumed_size--;
3347
3348 /* If external, check for usage. */
3349 if (csym && is_external_proc (csym))
3350 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3351
3352 t = true;
3353 if (c->resolved_sym == NULL)
3354 {
3355 c->resolved_isym = NULL;
3356 switch (procedure_kind (csym))
3357 {
3358 case PTYPE_GENERIC:
3359 t = resolve_generic_s (c);
3360 break;
3361
3362 case PTYPE_SPECIFIC:
3363 t = resolve_specific_s (c);
3364 break;
3365
3366 case PTYPE_UNKNOWN:
3367 t = resolve_unknown_s (c);
3368 break;
3369
3370 default:
3371 gfc_internal_error ("resolve_subroutine(): bad function type");
3372 }
3373 }
3374
3375 /* Some checks of elemental subroutine actual arguments. */
3376 if (!resolve_elemental_actual (NULL, c))
3377 return false;
3378
3379 return t;
3380 }
3381
3382
3383 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3384 op1->shape and op2->shape are non-NULL return true if their shapes
3385 match. If both op1->shape and op2->shape are non-NULL return false
3386 if their shapes do not match. If either op1->shape or op2->shape is
3387 NULL, return true. */
3388
3389 static bool
3390 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3391 {
3392 bool t;
3393 int i;
3394
3395 t = true;
3396
3397 if (op1->shape != NULL && op2->shape != NULL)
3398 {
3399 for (i = 0; i < op1->rank; i++)
3400 {
3401 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3402 {
3403 gfc_error ("Shapes for operands at %L and %L are not conformable",
3404 &op1->where, &op2->where);
3405 t = false;
3406 break;
3407 }
3408 }
3409 }
3410
3411 return t;
3412 }
3413
3414
3415 /* Resolve an operator expression node. This can involve replacing the
3416 operation with a user defined function call. */
3417
3418 static bool
3419 resolve_operator (gfc_expr *e)
3420 {
3421 gfc_expr *op1, *op2;
3422 char msg[200];
3423 bool dual_locus_error;
3424 bool t;
3425
3426 /* Resolve all subnodes-- give them types. */
3427
3428 switch (e->value.op.op)
3429 {
3430 default:
3431 if (!gfc_resolve_expr (e->value.op.op2))
3432 return false;
3433
3434 /* Fall through... */
3435
3436 case INTRINSIC_NOT:
3437 case INTRINSIC_UPLUS:
3438 case INTRINSIC_UMINUS:
3439 case INTRINSIC_PARENTHESES:
3440 if (!gfc_resolve_expr (e->value.op.op1))
3441 return false;
3442 break;
3443 }
3444
3445 /* Typecheck the new node. */
3446
3447 op1 = e->value.op.op1;
3448 op2 = e->value.op.op2;
3449 dual_locus_error = false;
3450
3451 if ((op1 && op1->expr_type == EXPR_NULL)
3452 || (op2 && op2->expr_type == EXPR_NULL))
3453 {
3454 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3455 goto bad_op;
3456 }
3457
3458 switch (e->value.op.op)
3459 {
3460 case INTRINSIC_UPLUS:
3461 case INTRINSIC_UMINUS:
3462 if (op1->ts.type == BT_INTEGER
3463 || op1->ts.type == BT_REAL
3464 || op1->ts.type == BT_COMPLEX)
3465 {
3466 e->ts = op1->ts;
3467 break;
3468 }
3469
3470 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3471 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3472 goto bad_op;
3473
3474 case INTRINSIC_PLUS:
3475 case INTRINSIC_MINUS:
3476 case INTRINSIC_TIMES:
3477 case INTRINSIC_DIVIDE:
3478 case INTRINSIC_POWER:
3479 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3480 {
3481 gfc_type_convert_binary (e, 1);
3482 break;
3483 }
3484
3485 sprintf (msg,
3486 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3487 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3488 gfc_typename (&op2->ts));
3489 goto bad_op;
3490
3491 case INTRINSIC_CONCAT:
3492 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3493 && op1->ts.kind == op2->ts.kind)
3494 {
3495 e->ts.type = BT_CHARACTER;
3496 e->ts.kind = op1->ts.kind;
3497 break;
3498 }
3499
3500 sprintf (msg,
3501 _("Operands of string concatenation operator at %%L are %s/%s"),
3502 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3503 goto bad_op;
3504
3505 case INTRINSIC_AND:
3506 case INTRINSIC_OR:
3507 case INTRINSIC_EQV:
3508 case INTRINSIC_NEQV:
3509 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3510 {
3511 e->ts.type = BT_LOGICAL;
3512 e->ts.kind = gfc_kind_max (op1, op2);
3513 if (op1->ts.kind < e->ts.kind)
3514 gfc_convert_type (op1, &e->ts, 2);
3515 else if (op2->ts.kind < e->ts.kind)
3516 gfc_convert_type (op2, &e->ts, 2);
3517 break;
3518 }
3519
3520 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3521 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3522 gfc_typename (&op2->ts));
3523
3524 goto bad_op;
3525
3526 case INTRINSIC_NOT:
3527 if (op1->ts.type == BT_LOGICAL)
3528 {
3529 e->ts.type = BT_LOGICAL;
3530 e->ts.kind = op1->ts.kind;
3531 break;
3532 }
3533
3534 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3535 gfc_typename (&op1->ts));
3536 goto bad_op;
3537
3538 case INTRINSIC_GT:
3539 case INTRINSIC_GT_OS:
3540 case INTRINSIC_GE:
3541 case INTRINSIC_GE_OS:
3542 case INTRINSIC_LT:
3543 case INTRINSIC_LT_OS:
3544 case INTRINSIC_LE:
3545 case INTRINSIC_LE_OS:
3546 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3547 {
3548 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3549 goto bad_op;
3550 }
3551
3552 /* Fall through... */
3553
3554 case INTRINSIC_EQ:
3555 case INTRINSIC_EQ_OS:
3556 case INTRINSIC_NE:
3557 case INTRINSIC_NE_OS:
3558 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3559 && op1->ts.kind == op2->ts.kind)
3560 {
3561 e->ts.type = BT_LOGICAL;
3562 e->ts.kind = gfc_default_logical_kind;
3563 break;
3564 }
3565
3566 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3567 {
3568 gfc_type_convert_binary (e, 1);
3569
3570 e->ts.type = BT_LOGICAL;
3571 e->ts.kind = gfc_default_logical_kind;
3572
3573 if (gfc_option.warn_compare_reals)
3574 {
3575 gfc_intrinsic_op op = e->value.op.op;
3576
3577 /* Type conversion has made sure that the types of op1 and op2
3578 agree, so it is only necessary to check the first one. */
3579 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3580 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3581 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3582 {
3583 const char *msg;
3584
3585 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3586 msg = "Equality comparison for %s at %L";
3587 else
3588 msg = "Inequality comparison for %s at %L";
3589
3590 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3591 }
3592 }
3593
3594 break;
3595 }
3596
3597 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3598 sprintf (msg,
3599 _("Logicals at %%L must be compared with %s instead of %s"),
3600 (e->value.op.op == INTRINSIC_EQ
3601 || e->value.op.op == INTRINSIC_EQ_OS)
3602 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3603 else
3604 sprintf (msg,
3605 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3606 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3607 gfc_typename (&op2->ts));
3608
3609 goto bad_op;
3610
3611 case INTRINSIC_USER:
3612 if (e->value.op.uop->op == NULL)
3613 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3614 else if (op2 == NULL)
3615 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3616 e->value.op.uop->name, gfc_typename (&op1->ts));
3617 else
3618 {
3619 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3620 e->value.op.uop->name, gfc_typename (&op1->ts),
3621 gfc_typename (&op2->ts));
3622 e->value.op.uop->op->sym->attr.referenced = 1;
3623 }
3624
3625 goto bad_op;
3626
3627 case INTRINSIC_PARENTHESES:
3628 e->ts = op1->ts;
3629 if (e->ts.type == BT_CHARACTER)
3630 e->ts.u.cl = op1->ts.u.cl;
3631 break;
3632
3633 default:
3634 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3635 }
3636
3637 /* Deal with arrayness of an operand through an operator. */
3638
3639 t = true;
3640
3641 switch (e->value.op.op)
3642 {
3643 case INTRINSIC_PLUS:
3644 case INTRINSIC_MINUS:
3645 case INTRINSIC_TIMES:
3646 case INTRINSIC_DIVIDE:
3647 case INTRINSIC_POWER:
3648 case INTRINSIC_CONCAT:
3649 case INTRINSIC_AND:
3650 case INTRINSIC_OR:
3651 case INTRINSIC_EQV:
3652 case INTRINSIC_NEQV:
3653 case INTRINSIC_EQ:
3654 case INTRINSIC_EQ_OS:
3655 case INTRINSIC_NE:
3656 case INTRINSIC_NE_OS:
3657 case INTRINSIC_GT:
3658 case INTRINSIC_GT_OS:
3659 case INTRINSIC_GE:
3660 case INTRINSIC_GE_OS:
3661 case INTRINSIC_LT:
3662 case INTRINSIC_LT_OS:
3663 case INTRINSIC_LE:
3664 case INTRINSIC_LE_OS:
3665
3666 if (op1->rank == 0 && op2->rank == 0)
3667 e->rank = 0;
3668
3669 if (op1->rank == 0 && op2->rank != 0)
3670 {
3671 e->rank = op2->rank;
3672
3673 if (e->shape == NULL)
3674 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3675 }
3676
3677 if (op1->rank != 0 && op2->rank == 0)
3678 {
3679 e->rank = op1->rank;
3680
3681 if (e->shape == NULL)
3682 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3683 }
3684
3685 if (op1->rank != 0 && op2->rank != 0)
3686 {
3687 if (op1->rank == op2->rank)
3688 {
3689 e->rank = op1->rank;
3690 if (e->shape == NULL)
3691 {
3692 t = compare_shapes (op1, op2);
3693 if (!t)
3694 e->shape = NULL;
3695 else
3696 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3697 }
3698 }
3699 else
3700 {
3701 /* Allow higher level expressions to work. */
3702 e->rank = 0;
3703
3704 /* Try user-defined operators, and otherwise throw an error. */
3705 dual_locus_error = true;
3706 sprintf (msg,
3707 _("Inconsistent ranks for operator at %%L and %%L"));
3708 goto bad_op;
3709 }
3710 }
3711
3712 break;
3713
3714 case INTRINSIC_PARENTHESES:
3715 case INTRINSIC_NOT:
3716 case INTRINSIC_UPLUS:
3717 case INTRINSIC_UMINUS:
3718 /* Simply copy arrayness attribute */
3719 e->rank = op1->rank;
3720
3721 if (e->shape == NULL)
3722 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3723
3724 break;
3725
3726 default:
3727 break;
3728 }
3729
3730 /* Attempt to simplify the expression. */
3731 if (t)
3732 {
3733 t = gfc_simplify_expr (e, 0);
3734 /* Some calls do not succeed in simplification and return false
3735 even though there is no error; e.g. variable references to
3736 PARAMETER arrays. */
3737 if (!gfc_is_constant_expr (e))
3738 t = true;
3739 }
3740 return t;
3741
3742 bad_op:
3743
3744 {
3745 match m = gfc_extend_expr (e);
3746 if (m == MATCH_YES)
3747 return true;
3748 if (m == MATCH_ERROR)
3749 return false;
3750 }
3751
3752 if (dual_locus_error)
3753 gfc_error (msg, &op1->where, &op2->where);
3754 else
3755 gfc_error (msg, &e->where);
3756
3757 return false;
3758 }
3759
3760
3761 /************** Array resolution subroutines **************/
3762
3763 typedef enum
3764 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3765 comparison;
3766
3767 /* Compare two integer expressions. */
3768
3769 static comparison
3770 compare_bound (gfc_expr *a, gfc_expr *b)
3771 {
3772 int i;
3773
3774 if (a == NULL || a->expr_type != EXPR_CONSTANT
3775 || b == NULL || b->expr_type != EXPR_CONSTANT)
3776 return CMP_UNKNOWN;
3777
3778 /* If either of the types isn't INTEGER, we must have
3779 raised an error earlier. */
3780
3781 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3782 return CMP_UNKNOWN;
3783
3784 i = mpz_cmp (a->value.integer, b->value.integer);
3785
3786 if (i < 0)
3787 return CMP_LT;
3788 if (i > 0)
3789 return CMP_GT;
3790 return CMP_EQ;
3791 }
3792
3793
3794 /* Compare an integer expression with an integer. */
3795
3796 static comparison
3797 compare_bound_int (gfc_expr *a, int b)
3798 {
3799 int i;
3800
3801 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3802 return CMP_UNKNOWN;
3803
3804 if (a->ts.type != BT_INTEGER)
3805 gfc_internal_error ("compare_bound_int(): Bad expression");
3806
3807 i = mpz_cmp_si (a->value.integer, b);
3808
3809 if (i < 0)
3810 return CMP_LT;
3811 if (i > 0)
3812 return CMP_GT;
3813 return CMP_EQ;
3814 }
3815
3816
3817 /* Compare an integer expression with a mpz_t. */
3818
3819 static comparison
3820 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3821 {
3822 int i;
3823
3824 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3825 return CMP_UNKNOWN;
3826
3827 if (a->ts.type != BT_INTEGER)
3828 gfc_internal_error ("compare_bound_int(): Bad expression");
3829
3830 i = mpz_cmp (a->value.integer, b);
3831
3832 if (i < 0)
3833 return CMP_LT;
3834 if (i > 0)
3835 return CMP_GT;
3836 return CMP_EQ;
3837 }
3838
3839
3840 /* Compute the last value of a sequence given by a triplet.
3841 Return 0 if it wasn't able to compute the last value, or if the
3842 sequence if empty, and 1 otherwise. */
3843
3844 static int
3845 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3846 gfc_expr *stride, mpz_t last)
3847 {
3848 mpz_t rem;
3849
3850 if (start == NULL || start->expr_type != EXPR_CONSTANT
3851 || end == NULL || end->expr_type != EXPR_CONSTANT
3852 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3853 return 0;
3854
3855 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3856 || (stride != NULL && stride->ts.type != BT_INTEGER))
3857 return 0;
3858
3859 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3860 {
3861 if (compare_bound (start, end) == CMP_GT)
3862 return 0;
3863 mpz_set (last, end->value.integer);
3864 return 1;
3865 }
3866
3867 if (compare_bound_int (stride, 0) == CMP_GT)
3868 {
3869 /* Stride is positive */
3870 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3871 return 0;
3872 }
3873 else
3874 {
3875 /* Stride is negative */
3876 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3877 return 0;
3878 }
3879
3880 mpz_init (rem);
3881 mpz_sub (rem, end->value.integer, start->value.integer);
3882 mpz_tdiv_r (rem, rem, stride->value.integer);
3883 mpz_sub (last, end->value.integer, rem);
3884 mpz_clear (rem);
3885
3886 return 1;
3887 }
3888
3889
3890 /* Compare a single dimension of an array reference to the array
3891 specification. */
3892
3893 static bool
3894 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3895 {
3896 mpz_t last_value;
3897
3898 if (ar->dimen_type[i] == DIMEN_STAR)
3899 {
3900 gcc_assert (ar->stride[i] == NULL);
3901 /* This implies [*] as [*:] and [*:3] are not possible. */
3902 if (ar->start[i] == NULL)
3903 {
3904 gcc_assert (ar->end[i] == NULL);
3905 return true;
3906 }
3907 }
3908
3909 /* Given start, end and stride values, calculate the minimum and
3910 maximum referenced indexes. */
3911
3912 switch (ar->dimen_type[i])
3913 {
3914 case DIMEN_VECTOR:
3915 case DIMEN_THIS_IMAGE:
3916 break;
3917
3918 case DIMEN_STAR:
3919 case DIMEN_ELEMENT:
3920 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3921 {
3922 if (i < as->rank)
3923 gfc_warning ("Array reference at %L is out of bounds "
3924 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3925 mpz_get_si (ar->start[i]->value.integer),
3926 mpz_get_si (as->lower[i]->value.integer), i+1);
3927 else
3928 gfc_warning ("Array reference at %L is out of bounds "
3929 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3930 mpz_get_si (ar->start[i]->value.integer),
3931 mpz_get_si (as->lower[i]->value.integer),
3932 i + 1 - as->rank);
3933 return true;
3934 }
3935 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3936 {
3937 if (i < as->rank)
3938 gfc_warning ("Array reference at %L is out of bounds "
3939 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3940 mpz_get_si (ar->start[i]->value.integer),
3941 mpz_get_si (as->upper[i]->value.integer), i+1);
3942 else
3943 gfc_warning ("Array reference at %L is out of bounds "
3944 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3945 mpz_get_si (ar->start[i]->value.integer),
3946 mpz_get_si (as->upper[i]->value.integer),
3947 i + 1 - as->rank);
3948 return true;
3949 }
3950
3951 break;
3952
3953 case DIMEN_RANGE:
3954 {
3955 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3956 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3957
3958 comparison comp_start_end = compare_bound (AR_START, AR_END);
3959
3960 /* Check for zero stride, which is not allowed. */
3961 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3962 {
3963 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3964 return false;
3965 }
3966
3967 /* if start == len || (stride > 0 && start < len)
3968 || (stride < 0 && start > len),
3969 then the array section contains at least one element. In this
3970 case, there is an out-of-bounds access if
3971 (start < lower || start > upper). */
3972 if (compare_bound (AR_START, AR_END) == CMP_EQ
3973 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3974 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3975 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3976 && comp_start_end == CMP_GT))
3977 {
3978 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3979 {
3980 gfc_warning ("Lower array reference at %L is out of bounds "
3981 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3982 mpz_get_si (AR_START->value.integer),
3983 mpz_get_si (as->lower[i]->value.integer), i+1);
3984 return true;
3985 }
3986 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3987 {
3988 gfc_warning ("Lower array reference at %L is out of bounds "
3989 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3990 mpz_get_si (AR_START->value.integer),
3991 mpz_get_si (as->upper[i]->value.integer), i+1);
3992 return true;
3993 }
3994 }
3995
3996 /* If we can compute the highest index of the array section,
3997 then it also has to be between lower and upper. */
3998 mpz_init (last_value);
3999 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4000 last_value))
4001 {
4002 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4003 {
4004 gfc_warning ("Upper array reference at %L is out of bounds "
4005 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4006 mpz_get_si (last_value),
4007 mpz_get_si (as->lower[i]->value.integer), i+1);
4008 mpz_clear (last_value);
4009 return true;
4010 }
4011 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4012 {
4013 gfc_warning ("Upper array reference at %L is out of bounds "
4014 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4015 mpz_get_si (last_value),
4016 mpz_get_si (as->upper[i]->value.integer), i+1);
4017 mpz_clear (last_value);
4018 return true;
4019 }
4020 }
4021 mpz_clear (last_value);
4022
4023 #undef AR_START
4024 #undef AR_END
4025 }
4026 break;
4027
4028 default:
4029 gfc_internal_error ("check_dimension(): Bad array reference");
4030 }
4031
4032 return true;
4033 }
4034
4035
4036 /* Compare an array reference with an array specification. */
4037
4038 static bool
4039 compare_spec_to_ref (gfc_array_ref *ar)
4040 {
4041 gfc_array_spec *as;
4042 int i;
4043
4044 as = ar->as;
4045 i = as->rank - 1;
4046 /* TODO: Full array sections are only allowed as actual parameters. */
4047 if (as->type == AS_ASSUMED_SIZE
4048 && (/*ar->type == AR_FULL
4049 ||*/ (ar->type == AR_SECTION
4050 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4051 {
4052 gfc_error ("Rightmost upper bound of assumed size array section "
4053 "not specified at %L", &ar->where);
4054 return false;
4055 }
4056
4057 if (ar->type == AR_FULL)
4058 return true;
4059
4060 if (as->rank != ar->dimen)
4061 {
4062 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4063 &ar->where, ar->dimen, as->rank);
4064 return false;
4065 }
4066
4067 /* ar->codimen == 0 is a local array. */
4068 if (as->corank != ar->codimen && ar->codimen != 0)
4069 {
4070 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4071 &ar->where, ar->codimen, as->corank);
4072 return false;
4073 }
4074
4075 for (i = 0; i < as->rank; i++)
4076 if (!check_dimension (i, ar, as))
4077 return false;
4078
4079 /* Local access has no coarray spec. */
4080 if (ar->codimen != 0)
4081 for (i = as->rank; i < as->rank + as->corank; i++)
4082 {
4083 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4084 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4085 {
4086 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4087 i + 1 - as->rank, &ar->where);
4088 return false;
4089 }
4090 if (!check_dimension (i, ar, as))
4091 return false;
4092 }
4093
4094 return true;
4095 }
4096
4097
4098 /* Resolve one part of an array index. */
4099
4100 static bool
4101 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4102 int force_index_integer_kind)
4103 {
4104 gfc_typespec ts;
4105
4106 if (index == NULL)
4107 return true;
4108
4109 if (!gfc_resolve_expr (index))
4110 return false;
4111
4112 if (check_scalar && index->rank != 0)
4113 {
4114 gfc_error ("Array index at %L must be scalar", &index->where);
4115 return false;
4116 }
4117
4118 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4119 {
4120 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4121 &index->where, gfc_basic_typename (index->ts.type));
4122 return false;
4123 }
4124
4125 if (index->ts.type == BT_REAL)
4126 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4127 &index->where))
4128 return false;
4129
4130 if ((index->ts.kind != gfc_index_integer_kind
4131 && force_index_integer_kind)
4132 || index->ts.type != BT_INTEGER)
4133 {
4134 gfc_clear_ts (&ts);
4135 ts.type = BT_INTEGER;
4136 ts.kind = gfc_index_integer_kind;
4137
4138 gfc_convert_type_warn (index, &ts, 2, 0);
4139 }
4140
4141 return true;
4142 }
4143
4144 /* Resolve one part of an array index. */
4145
4146 bool
4147 gfc_resolve_index (gfc_expr *index, int check_scalar)
4148 {
4149 return gfc_resolve_index_1 (index, check_scalar, 1);
4150 }
4151
4152 /* Resolve a dim argument to an intrinsic function. */
4153
4154 bool
4155 gfc_resolve_dim_arg (gfc_expr *dim)
4156 {
4157 if (dim == NULL)
4158 return true;
4159
4160 if (!gfc_resolve_expr (dim))
4161 return false;
4162
4163 if (dim->rank != 0)
4164 {
4165 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4166 return false;
4167
4168 }
4169
4170 if (dim->ts.type != BT_INTEGER)
4171 {
4172 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4173 return false;
4174 }
4175
4176 if (dim->ts.kind != gfc_index_integer_kind)
4177 {
4178 gfc_typespec ts;
4179
4180 gfc_clear_ts (&ts);
4181 ts.type = BT_INTEGER;
4182 ts.kind = gfc_index_integer_kind;
4183
4184 gfc_convert_type_warn (dim, &ts, 2, 0);
4185 }
4186
4187 return true;
4188 }
4189
4190 /* Given an expression that contains array references, update those array
4191 references to point to the right array specifications. While this is
4192 filled in during matching, this information is difficult to save and load
4193 in a module, so we take care of it here.
4194
4195 The idea here is that the original array reference comes from the
4196 base symbol. We traverse the list of reference structures, setting
4197 the stored reference to references. Component references can
4198 provide an additional array specification. */
4199
4200 static void
4201 find_array_spec (gfc_expr *e)
4202 {
4203 gfc_array_spec *as;
4204 gfc_component *c;
4205 gfc_ref *ref;
4206
4207 if (e->symtree->n.sym->ts.type == BT_CLASS)
4208 as = CLASS_DATA (e->symtree->n.sym)->as;
4209 else
4210 as = e->symtree->n.sym->as;
4211
4212 for (ref = e->ref; ref; ref = ref->next)
4213 switch (ref->type)
4214 {
4215 case REF_ARRAY:
4216 if (as == NULL)
4217 gfc_internal_error ("find_array_spec(): Missing spec");
4218
4219 ref->u.ar.as = as;
4220 as = NULL;
4221 break;
4222
4223 case REF_COMPONENT:
4224 c = ref->u.c.component;
4225 if (c->attr.dimension)
4226 {
4227 if (as != NULL)
4228 gfc_internal_error ("find_array_spec(): unused as(1)");
4229 as = c->as;
4230 }
4231
4232 break;
4233
4234 case REF_SUBSTRING:
4235 break;
4236 }
4237
4238 if (as != NULL)
4239 gfc_internal_error ("find_array_spec(): unused as(2)");
4240 }
4241
4242
4243 /* Resolve an array reference. */
4244
4245 static bool
4246 resolve_array_ref (gfc_array_ref *ar)
4247 {
4248 int i, check_scalar;
4249 gfc_expr *e;
4250
4251 for (i = 0; i < ar->dimen + ar->codimen; i++)
4252 {
4253 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4254
4255 /* Do not force gfc_index_integer_kind for the start. We can
4256 do fine with any integer kind. This avoids temporary arrays
4257 created for indexing with a vector. */
4258 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4259 return false;
4260 if (!gfc_resolve_index (ar->end[i], check_scalar))
4261 return false;
4262 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4263 return false;
4264
4265 e = ar->start[i];
4266
4267 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4268 switch (e->rank)
4269 {
4270 case 0:
4271 ar->dimen_type[i] = DIMEN_ELEMENT;
4272 break;
4273
4274 case 1:
4275 ar->dimen_type[i] = DIMEN_VECTOR;
4276 if (e->expr_type == EXPR_VARIABLE
4277 && e->symtree->n.sym->ts.type == BT_DERIVED)
4278 ar->start[i] = gfc_get_parentheses (e);
4279 break;
4280
4281 default:
4282 gfc_error ("Array index at %L is an array of rank %d",
4283 &ar->c_where[i], e->rank);
4284 return false;
4285 }
4286
4287 /* Fill in the upper bound, which may be lower than the
4288 specified one for something like a(2:10:5), which is
4289 identical to a(2:7:5). Only relevant for strides not equal
4290 to one. Don't try a division by zero. */
4291 if (ar->dimen_type[i] == DIMEN_RANGE
4292 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4293 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4294 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4295 {
4296 mpz_t size, end;
4297
4298 if (gfc_ref_dimen_size (ar, i, &size, &end))
4299 {
4300 if (ar->end[i] == NULL)
4301 {
4302 ar->end[i] =
4303 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4304 &ar->where);
4305 mpz_set (ar->end[i]->value.integer, end);
4306 }
4307 else if (ar->end[i]->ts.type == BT_INTEGER
4308 && ar->end[i]->expr_type == EXPR_CONSTANT)
4309 {
4310 mpz_set (ar->end[i]->value.integer, end);
4311 }
4312 else
4313 gcc_unreachable ();
4314
4315 mpz_clear (size);
4316 mpz_clear (end);
4317 }
4318 }
4319 }
4320
4321 if (ar->type == AR_FULL)
4322 {
4323 if (ar->as->rank == 0)
4324 ar->type = AR_ELEMENT;
4325
4326 /* Make sure array is the same as array(:,:), this way
4327 we don't need to special case all the time. */
4328 ar->dimen = ar->as->rank;
4329 for (i = 0; i < ar->dimen; i++)
4330 {
4331 ar->dimen_type[i] = DIMEN_RANGE;
4332
4333 gcc_assert (ar->start[i] == NULL);
4334 gcc_assert (ar->end[i] == NULL);
4335 gcc_assert (ar->stride[i] == NULL);
4336 }
4337 }
4338
4339 /* If the reference type is unknown, figure out what kind it is. */
4340
4341 if (ar->type == AR_UNKNOWN)
4342 {
4343 ar->type = AR_ELEMENT;
4344 for (i = 0; i < ar->dimen; i++)
4345 if (ar->dimen_type[i] == DIMEN_RANGE
4346 || ar->dimen_type[i] == DIMEN_VECTOR)
4347 {
4348 ar->type = AR_SECTION;
4349 break;
4350 }
4351 }
4352
4353 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4354 return false;
4355
4356 if (ar->as->corank && ar->codimen == 0)
4357 {
4358 int n;
4359 ar->codimen = ar->as->corank;
4360 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4361 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4362 }
4363
4364 return true;
4365 }
4366
4367
4368 static bool
4369 resolve_substring (gfc_ref *ref)
4370 {
4371 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4372
4373 if (ref->u.ss.start != NULL)
4374 {
4375 if (!gfc_resolve_expr (ref->u.ss.start))
4376 return false;
4377
4378 if (ref->u.ss.start->ts.type != BT_INTEGER)
4379 {
4380 gfc_error ("Substring start index at %L must be of type INTEGER",
4381 &ref->u.ss.start->where);
4382 return false;
4383 }
4384
4385 if (ref->u.ss.start->rank != 0)
4386 {
4387 gfc_error ("Substring start index at %L must be scalar",
4388 &ref->u.ss.start->where);
4389 return false;
4390 }
4391
4392 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4393 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4394 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4395 {
4396 gfc_error ("Substring start index at %L is less than one",
4397 &ref->u.ss.start->where);
4398 return false;
4399 }
4400 }
4401
4402 if (ref->u.ss.end != NULL)
4403 {
4404 if (!gfc_resolve_expr (ref->u.ss.end))
4405 return false;
4406
4407 if (ref->u.ss.end->ts.type != BT_INTEGER)
4408 {
4409 gfc_error ("Substring end index at %L must be of type INTEGER",
4410 &ref->u.ss.end->where);
4411 return false;
4412 }
4413
4414 if (ref->u.ss.end->rank != 0)
4415 {
4416 gfc_error ("Substring end index at %L must be scalar",
4417 &ref->u.ss.end->where);
4418 return false;
4419 }
4420
4421 if (ref->u.ss.length != NULL
4422 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4423 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4424 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4425 {
4426 gfc_error ("Substring end index at %L exceeds the string length",
4427 &ref->u.ss.start->where);
4428 return false;
4429 }
4430
4431 if (compare_bound_mpz_t (ref->u.ss.end,
4432 gfc_integer_kinds[k].huge) == CMP_GT
4433 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4434 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4435 {
4436 gfc_error ("Substring end index at %L is too large",
4437 &ref->u.ss.end->where);
4438 return false;
4439 }
4440 }
4441
4442 return true;
4443 }
4444
4445
4446 /* This function supplies missing substring charlens. */
4447
4448 void
4449 gfc_resolve_substring_charlen (gfc_expr *e)
4450 {
4451 gfc_ref *char_ref;
4452 gfc_expr *start, *end;
4453
4454 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4455 if (char_ref->type == REF_SUBSTRING)
4456 break;
4457
4458 if (!char_ref)
4459 return;
4460
4461 gcc_assert (char_ref->next == NULL);
4462
4463 if (e->ts.u.cl)
4464 {
4465 if (e->ts.u.cl->length)
4466 gfc_free_expr (e->ts.u.cl->length);
4467 else if (e->expr_type == EXPR_VARIABLE
4468 && e->symtree->n.sym->attr.dummy)
4469 return;
4470 }
4471
4472 e->ts.type = BT_CHARACTER;
4473 e->ts.kind = gfc_default_character_kind;
4474
4475 if (!e->ts.u.cl)
4476 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4477
4478 if (char_ref->u.ss.start)
4479 start = gfc_copy_expr (char_ref->u.ss.start);
4480 else
4481 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4482
4483 if (char_ref->u.ss.end)
4484 end = gfc_copy_expr (char_ref->u.ss.end);
4485 else if (e->expr_type == EXPR_VARIABLE)
4486 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4487 else
4488 end = NULL;
4489
4490 if (!start || !end)
4491 {
4492 gfc_free_expr (start);
4493 gfc_free_expr (end);
4494 return;
4495 }
4496
4497 /* Length = (end - start +1). */
4498 e->ts.u.cl->length = gfc_subtract (end, start);
4499 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4500 gfc_get_int_expr (gfc_default_integer_kind,
4501 NULL, 1));
4502
4503 e->ts.u.cl->length->ts.type = BT_INTEGER;
4504 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4505
4506 /* Make sure that the length is simplified. */
4507 gfc_simplify_expr (e->ts.u.cl->length, 1);
4508 gfc_resolve_expr (e->ts.u.cl->length);
4509 }
4510
4511
4512 /* Resolve subtype references. */
4513
4514 static bool
4515 resolve_ref (gfc_expr *expr)
4516 {
4517 int current_part_dimension, n_components, seen_part_dimension;
4518 gfc_ref *ref;
4519
4520 for (ref = expr->ref; ref; ref = ref->next)
4521 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4522 {
4523 find_array_spec (expr);
4524 break;
4525 }
4526
4527 for (ref = expr->ref; ref; ref = ref->next)
4528 switch (ref->type)
4529 {
4530 case REF_ARRAY:
4531 if (!resolve_array_ref (&ref->u.ar))
4532 return false;
4533 break;
4534
4535 case REF_COMPONENT:
4536 break;
4537
4538 case REF_SUBSTRING:
4539 if (!resolve_substring (ref))
4540 return false;
4541 break;
4542 }
4543
4544 /* Check constraints on part references. */
4545
4546 current_part_dimension = 0;
4547 seen_part_dimension = 0;
4548 n_components = 0;
4549
4550 for (ref = expr->ref; ref; ref = ref->next)
4551 {
4552 switch (ref->type)
4553 {
4554 case REF_ARRAY:
4555 switch (ref->u.ar.type)
4556 {
4557 case AR_FULL:
4558 /* Coarray scalar. */
4559 if (ref->u.ar.as->rank == 0)
4560 {
4561 current_part_dimension = 0;
4562 break;
4563 }
4564 /* Fall through. */
4565 case AR_SECTION:
4566 current_part_dimension = 1;
4567 break;
4568
4569 case AR_ELEMENT:
4570 current_part_dimension = 0;
4571 break;
4572
4573 case AR_UNKNOWN:
4574 gfc_internal_error ("resolve_ref(): Bad array reference");
4575 }
4576
4577 break;
4578
4579 case REF_COMPONENT:
4580 if (current_part_dimension || seen_part_dimension)
4581 {
4582 /* F03:C614. */
4583 if (ref->u.c.component->attr.pointer
4584 || ref->u.c.component->attr.proc_pointer
4585 || (ref->u.c.component->ts.type == BT_CLASS
4586 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4587 {
4588 gfc_error ("Component to the right of a part reference "
4589 "with nonzero rank must not have the POINTER "
4590 "attribute at %L", &expr->where);
4591 return false;
4592 }
4593 else if (ref->u.c.component->attr.allocatable
4594 || (ref->u.c.component->ts.type == BT_CLASS
4595 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4596
4597 {
4598 gfc_error ("Component to the right of a part reference "
4599 "with nonzero rank must not have the ALLOCATABLE "
4600 "attribute at %L", &expr->where);
4601 return false;
4602 }
4603 }
4604
4605 n_components++;
4606 break;
4607
4608 case REF_SUBSTRING:
4609 break;
4610 }
4611
4612 if (((ref->type == REF_COMPONENT && n_components > 1)
4613 || ref->next == NULL)
4614 && current_part_dimension
4615 && seen_part_dimension)
4616 {
4617 gfc_error ("Two or more part references with nonzero rank must "
4618 "not be specified at %L", &expr->where);
4619 return false;
4620 }
4621
4622 if (ref->type == REF_COMPONENT)
4623 {
4624 if (current_part_dimension)
4625 seen_part_dimension = 1;
4626
4627 /* reset to make sure */
4628 current_part_dimension = 0;
4629 }
4630 }
4631
4632 return true;
4633 }
4634
4635
4636 /* Given an expression, determine its shape. This is easier than it sounds.
4637 Leaves the shape array NULL if it is not possible to determine the shape. */
4638
4639 static void
4640 expression_shape (gfc_expr *e)
4641 {
4642 mpz_t array[GFC_MAX_DIMENSIONS];
4643 int i;
4644
4645 if (e->rank <= 0 || e->shape != NULL)
4646 return;
4647
4648 for (i = 0; i < e->rank; i++)
4649 if (!gfc_array_dimen_size (e, i, &array[i]))
4650 goto fail;
4651
4652 e->shape = gfc_get_shape (e->rank);
4653
4654 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4655
4656 return;
4657
4658 fail:
4659 for (i--; i >= 0; i--)
4660 mpz_clear (array[i]);
4661 }
4662
4663
4664 /* Given a variable expression node, compute the rank of the expression by
4665 examining the base symbol and any reference structures it may have. */
4666
4667 static void
4668 expression_rank (gfc_expr *e)
4669 {
4670 gfc_ref *ref;
4671 int i, rank;
4672
4673 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4674 could lead to serious confusion... */
4675 gcc_assert (e->expr_type != EXPR_COMPCALL);
4676
4677 if (e->ref == NULL)
4678 {
4679 if (e->expr_type == EXPR_ARRAY)
4680 goto done;
4681 /* Constructors can have a rank different from one via RESHAPE(). */
4682
4683 if (e->symtree == NULL)
4684 {
4685 e->rank = 0;
4686 goto done;
4687 }
4688
4689 e->rank = (e->symtree->n.sym->as == NULL)
4690 ? 0 : e->symtree->n.sym->as->rank;
4691 goto done;
4692 }
4693
4694 rank = 0;
4695
4696 for (ref = e->ref; ref; ref = ref->next)
4697 {
4698 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4699 && ref->u.c.component->attr.function && !ref->next)
4700 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4701
4702 if (ref->type != REF_ARRAY)
4703 continue;
4704
4705 if (ref->u.ar.type == AR_FULL)
4706 {
4707 rank = ref->u.ar.as->rank;
4708 break;
4709 }
4710
4711 if (ref->u.ar.type == AR_SECTION)
4712 {
4713 /* Figure out the rank of the section. */
4714 if (rank != 0)
4715 gfc_internal_error ("expression_rank(): Two array specs");
4716
4717 for (i = 0; i < ref->u.ar.dimen; i++)
4718 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4719 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4720 rank++;
4721
4722 break;
4723 }
4724 }
4725
4726 e->rank = rank;
4727
4728 done:
4729 expression_shape (e);
4730 }
4731
4732
4733 /* Resolve a variable expression. */
4734
4735 static bool
4736 resolve_variable (gfc_expr *e)
4737 {
4738 gfc_symbol *sym;
4739 bool t;
4740
4741 t = true;
4742
4743 if (e->symtree == NULL)
4744 return false;
4745 sym = e->symtree->n.sym;
4746
4747 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4748 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4749 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4750 {
4751 if (!actual_arg || inquiry_argument)
4752 {
4753 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4754 "be used as actual argument", sym->name, &e->where);
4755 return false;
4756 }
4757 }
4758 /* TS 29113, 407b. */
4759 else if (e->ts.type == BT_ASSUMED)
4760 {
4761 if (!actual_arg)
4762 {
4763 gfc_error ("Assumed-type variable %s at %L may only be used "
4764 "as actual argument", sym->name, &e->where);
4765 return false;
4766 }
4767 else if (inquiry_argument && !first_actual_arg)
4768 {
4769 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4770 for all inquiry functions in resolve_function; the reason is
4771 that the function-name resolution happens too late in that
4772 function. */
4773 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4774 "an inquiry function shall be the first argument",
4775 sym->name, &e->where);
4776 return false;
4777 }
4778 }
4779 /* TS 29113, C535b. */
4780 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4781 && CLASS_DATA (sym)->as
4782 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4783 || (sym->ts.type != BT_CLASS && sym->as
4784 && sym->as->type == AS_ASSUMED_RANK))
4785 {
4786 if (!actual_arg)
4787 {
4788 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4789 "actual argument", sym->name, &e->where);
4790 return false;
4791 }
4792 else if (inquiry_argument && !first_actual_arg)
4793 {
4794 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4795 for all inquiry functions in resolve_function; the reason is
4796 that the function-name resolution happens too late in that
4797 function. */
4798 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4799 "to an inquiry function shall be the first argument",
4800 sym->name, &e->where);
4801 return false;
4802 }
4803 }
4804
4805 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4806 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4807 && e->ref->next == NULL))
4808 {
4809 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4810 "a subobject reference", sym->name, &e->ref->u.ar.where);
4811 return false;
4812 }
4813 /* TS 29113, 407b. */
4814 else if (e->ts.type == BT_ASSUMED && e->ref
4815 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4816 && e->ref->next == NULL))
4817 {
4818 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4819 "reference", sym->name, &e->ref->u.ar.where);
4820 return false;
4821 }
4822
4823 /* TS 29113, C535b. */
4824 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4825 && CLASS_DATA (sym)->as
4826 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4827 || (sym->ts.type != BT_CLASS && sym->as
4828 && sym->as->type == AS_ASSUMED_RANK))
4829 && e->ref
4830 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4831 && e->ref->next == NULL))
4832 {
4833 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4834 "reference", sym->name, &e->ref->u.ar.where);
4835 return false;
4836 }
4837
4838
4839 /* If this is an associate-name, it may be parsed with an array reference
4840 in error even though the target is scalar. Fail directly in this case.
4841 TODO Understand why class scalar expressions must be excluded. */
4842 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4843 {
4844 if (sym->ts.type == BT_CLASS)
4845 gfc_fix_class_refs (e);
4846 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4847 return false;
4848 }
4849
4850 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4851 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4852
4853 /* On the other hand, the parser may not have known this is an array;
4854 in this case, we have to add a FULL reference. */
4855 if (sym->assoc && sym->attr.dimension && !e->ref)
4856 {
4857 e->ref = gfc_get_ref ();
4858 e->ref->type = REF_ARRAY;
4859 e->ref->u.ar.type = AR_FULL;
4860 e->ref->u.ar.dimen = 0;
4861 }
4862
4863 if (e->ref && !resolve_ref (e))
4864 return false;
4865
4866 if (sym->attr.flavor == FL_PROCEDURE
4867 && (!sym->attr.function
4868 || (sym->attr.function && sym->result
4869 && sym->result->attr.proc_pointer
4870 && !sym->result->attr.function)))
4871 {
4872 e->ts.type = BT_PROCEDURE;
4873 goto resolve_procedure;
4874 }
4875
4876 if (sym->ts.type != BT_UNKNOWN)
4877 gfc_variable_attr (e, &e->ts);
4878 else
4879 {
4880 /* Must be a simple variable reference. */
4881 if (!gfc_set_default_type (sym, 1, sym->ns))
4882 return false;
4883 e->ts = sym->ts;
4884 }
4885
4886 if (check_assumed_size_reference (sym, e))
4887 return false;
4888
4889 /* Deal with forward references to entries during resolve_code, to
4890 satisfy, at least partially, 12.5.2.5. */
4891 if (gfc_current_ns->entries
4892 && current_entry_id == sym->entry_id
4893 && cs_base
4894 && cs_base->current
4895 && cs_base->current->op != EXEC_ENTRY)
4896 {
4897 gfc_entry_list *entry;
4898 gfc_formal_arglist *formal;
4899 int n;
4900 bool seen, saved_specification_expr;
4901
4902 /* If the symbol is a dummy... */
4903 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4904 {
4905 entry = gfc_current_ns->entries;
4906 seen = false;
4907
4908 /* ...test if the symbol is a parameter of previous entries. */
4909 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4910 for (formal = entry->sym->formal; formal; formal = formal->next)
4911 {
4912 if (formal->sym && sym->name == formal->sym->name)
4913 {
4914 seen = true;
4915 break;
4916 }
4917 }
4918
4919 /* If it has not been seen as a dummy, this is an error. */
4920 if (!seen)
4921 {
4922 if (specification_expr)
4923 gfc_error ("Variable '%s', used in a specification expression"
4924 ", is referenced at %L before the ENTRY statement "
4925 "in which it is a parameter",
4926 sym->name, &cs_base->current->loc);
4927 else
4928 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4929 "statement in which it is a parameter",
4930 sym->name, &cs_base->current->loc);
4931 t = false;
4932 }
4933 }
4934
4935 /* Now do the same check on the specification expressions. */
4936 saved_specification_expr = specification_expr;
4937 specification_expr = true;
4938 if (sym->ts.type == BT_CHARACTER
4939 && !gfc_resolve_expr (sym->ts.u.cl->length))
4940 t = false;
4941
4942 if (sym->as)
4943 for (n = 0; n < sym->as->rank; n++)
4944 {
4945 if (!gfc_resolve_expr (sym->as->lower[n]))
4946 t = false;
4947 if (!gfc_resolve_expr (sym->as->upper[n]))
4948 t = false;
4949 }
4950 specification_expr = saved_specification_expr;
4951
4952 if (t)
4953 /* Update the symbol's entry level. */
4954 sym->entry_id = current_entry_id + 1;
4955 }
4956
4957 /* If a symbol has been host_associated mark it. This is used latter,
4958 to identify if aliasing is possible via host association. */
4959 if (sym->attr.flavor == FL_VARIABLE
4960 && gfc_current_ns->parent
4961 && (gfc_current_ns->parent == sym->ns
4962 || (gfc_current_ns->parent->parent
4963 && gfc_current_ns->parent->parent == sym->ns)))
4964 sym->attr.host_assoc = 1;
4965
4966 resolve_procedure:
4967 if (t && !resolve_procedure_expression (e))
4968 t = false;
4969
4970 /* F2008, C617 and C1229. */
4971 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4972 && gfc_is_coindexed (e))
4973 {
4974 gfc_ref *ref, *ref2 = NULL;
4975
4976 for (ref = e->ref; ref; ref = ref->next)
4977 {
4978 if (ref->type == REF_COMPONENT)
4979 ref2 = ref;
4980 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4981 break;
4982 }
4983
4984 for ( ; ref; ref = ref->next)
4985 if (ref->type == REF_COMPONENT)
4986 break;
4987
4988 /* Expression itself is not coindexed object. */
4989 if (ref && e->ts.type == BT_CLASS)
4990 {
4991 gfc_error ("Polymorphic subobject of coindexed object at %L",
4992 &e->where);
4993 t = false;
4994 }
4995
4996 /* Expression itself is coindexed object. */
4997 if (ref == NULL)
4998 {
4999 gfc_component *c;
5000 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5001 for ( ; c; c = c->next)
5002 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5003 {
5004 gfc_error ("Coindexed object with polymorphic allocatable "
5005 "subcomponent at %L", &e->where);
5006 t = false;
5007 break;
5008 }
5009 }
5010 }
5011
5012 return t;
5013 }
5014
5015
5016 /* Checks to see that the correct symbol has been host associated.
5017 The only situation where this arises is that in which a twice
5018 contained function is parsed after the host association is made.
5019 Therefore, on detecting this, change the symbol in the expression
5020 and convert the array reference into an actual arglist if the old
5021 symbol is a variable. */
5022 static bool
5023 check_host_association (gfc_expr *e)
5024 {
5025 gfc_symbol *sym, *old_sym;
5026 gfc_symtree *st;
5027 int n;
5028 gfc_ref *ref;
5029 gfc_actual_arglist *arg, *tail = NULL;
5030 bool retval = e->expr_type == EXPR_FUNCTION;
5031
5032 /* If the expression is the result of substitution in
5033 interface.c(gfc_extend_expr) because there is no way in
5034 which the host association can be wrong. */
5035 if (e->symtree == NULL
5036 || e->symtree->n.sym == NULL
5037 || e->user_operator)
5038 return retval;
5039
5040 old_sym = e->symtree->n.sym;
5041
5042 if (gfc_current_ns->parent
5043 && old_sym->ns != gfc_current_ns)
5044 {
5045 /* Use the 'USE' name so that renamed module symbols are
5046 correctly handled. */
5047 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5048
5049 if (sym && old_sym != sym
5050 && sym->ts.type == old_sym->ts.type
5051 && sym->attr.flavor == FL_PROCEDURE
5052 && sym->attr.contained)
5053 {
5054 /* Clear the shape, since it might not be valid. */
5055 gfc_free_shape (&e->shape, e->rank);
5056
5057 /* Give the expression the right symtree! */
5058 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5059 gcc_assert (st != NULL);
5060
5061 if (old_sym->attr.flavor == FL_PROCEDURE
5062 || e->expr_type == EXPR_FUNCTION)
5063 {
5064 /* Original was function so point to the new symbol, since
5065 the actual argument list is already attached to the
5066 expression. */
5067 e->value.function.esym = NULL;
5068 e->symtree = st;
5069 }
5070 else
5071 {
5072 /* Original was variable so convert array references into
5073 an actual arglist. This does not need any checking now
5074 since resolve_function will take care of it. */
5075 e->value.function.actual = NULL;
5076 e->expr_type = EXPR_FUNCTION;
5077 e->symtree = st;
5078
5079 /* Ambiguity will not arise if the array reference is not
5080 the last reference. */
5081 for (ref = e->ref; ref; ref = ref->next)
5082 if (ref->type == REF_ARRAY && ref->next == NULL)
5083 break;
5084
5085 gcc_assert (ref->type == REF_ARRAY);
5086
5087 /* Grab the start expressions from the array ref and
5088 copy them into actual arguments. */
5089 for (n = 0; n < ref->u.ar.dimen; n++)
5090 {
5091 arg = gfc_get_actual_arglist ();
5092 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5093 if (e->value.function.actual == NULL)
5094 tail = e->value.function.actual = arg;
5095 else
5096 {
5097 tail->next = arg;
5098 tail = arg;
5099 }
5100 }
5101
5102 /* Dump the reference list and set the rank. */
5103 gfc_free_ref_list (e->ref);
5104 e->ref = NULL;
5105 e->rank = sym->as ? sym->as->rank : 0;
5106 }
5107
5108 gfc_resolve_expr (e);
5109 sym->refs++;
5110 }
5111 }
5112 /* This might have changed! */
5113 return e->expr_type == EXPR_FUNCTION;
5114 }
5115
5116
5117 static void
5118 gfc_resolve_character_operator (gfc_expr *e)
5119 {
5120 gfc_expr *op1 = e->value.op.op1;
5121 gfc_expr *op2 = e->value.op.op2;
5122 gfc_expr *e1 = NULL;
5123 gfc_expr *e2 = NULL;
5124
5125 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5126
5127 if (op1->ts.u.cl && op1->ts.u.cl->length)
5128 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5129 else if (op1->expr_type == EXPR_CONSTANT)
5130 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5131 op1->value.character.length);
5132
5133 if (op2->ts.u.cl && op2->ts.u.cl->length)
5134 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5135 else if (op2->expr_type == EXPR_CONSTANT)
5136 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5137 op2->value.character.length);
5138
5139 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5140
5141 if (!e1 || !e2)
5142 {
5143 gfc_free_expr (e1);
5144 gfc_free_expr (e2);
5145
5146 return;
5147 }
5148
5149 e->ts.u.cl->length = gfc_add (e1, e2);
5150 e->ts.u.cl->length->ts.type = BT_INTEGER;
5151 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5152 gfc_simplify_expr (e->ts.u.cl->length, 0);
5153 gfc_resolve_expr (e->ts.u.cl->length);
5154
5155 return;
5156 }
5157
5158
5159 /* Ensure that an character expression has a charlen and, if possible, a
5160 length expression. */
5161
5162 static void
5163 fixup_charlen (gfc_expr *e)
5164 {
5165 /* The cases fall through so that changes in expression type and the need
5166 for multiple fixes are picked up. In all circumstances, a charlen should
5167 be available for the middle end to hang a backend_decl on. */
5168 switch (e->expr_type)
5169 {
5170 case EXPR_OP:
5171 gfc_resolve_character_operator (e);
5172
5173 case EXPR_ARRAY:
5174 if (e->expr_type == EXPR_ARRAY)
5175 gfc_resolve_character_array_constructor (e);
5176
5177 case EXPR_SUBSTRING:
5178 if (!e->ts.u.cl && e->ref)
5179 gfc_resolve_substring_charlen (e);
5180
5181 default:
5182 if (!e->ts.u.cl)
5183 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5184
5185 break;
5186 }
5187 }
5188
5189
5190 /* Update an actual argument to include the passed-object for type-bound
5191 procedures at the right position. */
5192
5193 static gfc_actual_arglist*
5194 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5195 const char *name)
5196 {
5197 gcc_assert (argpos > 0);
5198
5199 if (argpos == 1)
5200 {
5201 gfc_actual_arglist* result;
5202
5203 result = gfc_get_actual_arglist ();
5204 result->expr = po;
5205 result->next = lst;
5206 if (name)
5207 result->name = name;
5208
5209 return result;
5210 }
5211
5212 if (lst)
5213 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5214 else
5215 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5216 return lst;
5217 }
5218
5219
5220 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5221
5222 static gfc_expr*
5223 extract_compcall_passed_object (gfc_expr* e)
5224 {
5225 gfc_expr* po;
5226
5227 gcc_assert (e->expr_type == EXPR_COMPCALL);
5228
5229 if (e->value.compcall.base_object)
5230 po = gfc_copy_expr (e->value.compcall.base_object);
5231 else
5232 {
5233 po = gfc_get_expr ();
5234 po->expr_type = EXPR_VARIABLE;
5235 po->symtree = e->symtree;
5236 po->ref = gfc_copy_ref (e->ref);
5237 po->where = e->where;
5238 }
5239
5240 if (!gfc_resolve_expr (po))
5241 return NULL;
5242
5243 return po;
5244 }
5245
5246
5247 /* Update the arglist of an EXPR_COMPCALL expression to include the
5248 passed-object. */
5249
5250 static bool
5251 update_compcall_arglist (gfc_expr* e)
5252 {
5253 gfc_expr* po;
5254 gfc_typebound_proc* tbp;
5255
5256 tbp = e->value.compcall.tbp;
5257
5258 if (tbp->error)
5259 return false;
5260
5261 po = extract_compcall_passed_object (e);
5262 if (!po)
5263 return false;
5264
5265 if (tbp->nopass || e->value.compcall.ignore_pass)
5266 {
5267 gfc_free_expr (po);
5268 return true;
5269 }
5270
5271 gcc_assert (tbp->pass_arg_num > 0);
5272 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5273 tbp->pass_arg_num,
5274 tbp->pass_arg);
5275
5276 return true;
5277 }
5278
5279
5280 /* Extract the passed object from a PPC call (a copy of it). */
5281
5282 static gfc_expr*
5283 extract_ppc_passed_object (gfc_expr *e)
5284 {
5285 gfc_expr *po;
5286 gfc_ref **ref;
5287
5288 po = gfc_get_expr ();
5289 po->expr_type = EXPR_VARIABLE;
5290 po->symtree = e->symtree;
5291 po->ref = gfc_copy_ref (e->ref);
5292 po->where = e->where;
5293
5294 /* Remove PPC reference. */
5295 ref = &po->ref;
5296 while ((*ref)->next)
5297 ref = &(*ref)->next;
5298 gfc_free_ref_list (*ref);
5299 *ref = NULL;
5300
5301 if (!gfc_resolve_expr (po))
5302 return NULL;
5303
5304 return po;
5305 }
5306
5307
5308 /* Update the actual arglist of a procedure pointer component to include the
5309 passed-object. */
5310
5311 static bool
5312 update_ppc_arglist (gfc_expr* e)
5313 {
5314 gfc_expr* po;
5315 gfc_component *ppc;
5316 gfc_typebound_proc* tb;
5317
5318 ppc = gfc_get_proc_ptr_comp (e);
5319 if (!ppc)
5320 return false;
5321
5322 tb = ppc->tb;
5323
5324 if (tb->error)
5325 return false;
5326 else if (tb->nopass)
5327 return true;
5328
5329 po = extract_ppc_passed_object (e);
5330 if (!po)
5331 return false;
5332
5333 /* F08:R739. */
5334 if (po->rank != 0)
5335 {
5336 gfc_error ("Passed-object at %L must be scalar", &e->where);
5337 return false;
5338 }
5339
5340 /* F08:C611. */
5341 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5342 {
5343 gfc_error ("Base object for procedure-pointer component call at %L is of"
5344 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5345 return false;
5346 }
5347
5348 gcc_assert (tb->pass_arg_num > 0);
5349 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5350 tb->pass_arg_num,
5351 tb->pass_arg);
5352
5353 return true;
5354 }
5355
5356
5357 /* Check that the object a TBP is called on is valid, i.e. it must not be
5358 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5359
5360 static bool
5361 check_typebound_baseobject (gfc_expr* e)
5362 {
5363 gfc_expr* base;
5364 bool return_value = false;
5365
5366 base = extract_compcall_passed_object (e);
5367 if (!base)
5368 return false;
5369
5370 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5371
5372 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5373 return false;
5374
5375 /* F08:C611. */
5376 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5377 {
5378 gfc_error ("Base object for type-bound procedure call at %L is of"
5379 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5380 goto cleanup;
5381 }
5382
5383 /* F08:C1230. If the procedure called is NOPASS,
5384 the base object must be scalar. */
5385 if (e->value.compcall.tbp->nopass && base->rank != 0)
5386 {
5387 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5388 " be scalar", &e->where);
5389 goto cleanup;
5390 }
5391
5392 return_value = true;
5393
5394 cleanup:
5395 gfc_free_expr (base);
5396 return return_value;
5397 }
5398
5399
5400 /* Resolve a call to a type-bound procedure, either function or subroutine,
5401 statically from the data in an EXPR_COMPCALL expression. The adapted
5402 arglist and the target-procedure symtree are returned. */
5403
5404 static bool
5405 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5406 gfc_actual_arglist** actual)
5407 {
5408 gcc_assert (e->expr_type == EXPR_COMPCALL);
5409 gcc_assert (!e->value.compcall.tbp->is_generic);
5410
5411 /* Update the actual arglist for PASS. */
5412 if (!update_compcall_arglist (e))
5413 return false;
5414
5415 *actual = e->value.compcall.actual;
5416 *target = e->value.compcall.tbp->u.specific;
5417
5418 gfc_free_ref_list (e->ref);
5419 e->ref = NULL;
5420 e->value.compcall.actual = NULL;
5421
5422 /* If we find a deferred typebound procedure, check for derived types
5423 that an overriding typebound procedure has not been missed. */
5424 if (e->value.compcall.name
5425 && !e->value.compcall.tbp->non_overridable
5426 && e->value.compcall.base_object
5427 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5428 {
5429 gfc_symtree *st;
5430 gfc_symbol *derived;
5431
5432 /* Use the derived type of the base_object. */
5433 derived = e->value.compcall.base_object->ts.u.derived;
5434 st = NULL;
5435
5436 /* If necessary, go through the inheritance chain. */
5437 while (!st && derived)
5438 {
5439 /* Look for the typebound procedure 'name'. */
5440 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5441 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5442 e->value.compcall.name);
5443 if (!st)
5444 derived = gfc_get_derived_super_type (derived);
5445 }
5446
5447 /* Now find the specific name in the derived type namespace. */
5448 if (st && st->n.tb && st->n.tb->u.specific)
5449 gfc_find_sym_tree (st->n.tb->u.specific->name,
5450 derived->ns, 1, &st);
5451 if (st)
5452 *target = st;
5453 }
5454 return true;
5455 }
5456
5457
5458 /* Get the ultimate declared type from an expression. In addition,
5459 return the last class/derived type reference and the copy of the
5460 reference list. If check_types is set true, derived types are
5461 identified as well as class references. */
5462 static gfc_symbol*
5463 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5464 gfc_expr *e, bool check_types)
5465 {
5466 gfc_symbol *declared;
5467 gfc_ref *ref;
5468
5469 declared = NULL;
5470 if (class_ref)
5471 *class_ref = NULL;
5472 if (new_ref)
5473 *new_ref = gfc_copy_ref (e->ref);
5474
5475 for (ref = e->ref; ref; ref = ref->next)
5476 {
5477 if (ref->type != REF_COMPONENT)
5478 continue;
5479
5480 if ((ref->u.c.component->ts.type == BT_CLASS
5481 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5482 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5483 {
5484 declared = ref->u.c.component->ts.u.derived;
5485 if (class_ref)
5486 *class_ref = ref;
5487 }
5488 }
5489
5490 if (declared == NULL)
5491 declared = e->symtree->n.sym->ts.u.derived;
5492
5493 return declared;
5494 }
5495
5496
5497 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5498 which of the specific bindings (if any) matches the arglist and transform
5499 the expression into a call of that binding. */
5500
5501 static bool
5502 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5503 {
5504 gfc_typebound_proc* genproc;
5505 const char* genname;
5506 gfc_symtree *st;
5507 gfc_symbol *derived;
5508
5509 gcc_assert (e->expr_type == EXPR_COMPCALL);
5510 genname = e->value.compcall.name;
5511 genproc = e->value.compcall.tbp;
5512
5513 if (!genproc->is_generic)
5514 return true;
5515
5516 /* Try the bindings on this type and in the inheritance hierarchy. */
5517 for (; genproc; genproc = genproc->overridden)
5518 {
5519 gfc_tbp_generic* g;
5520
5521 gcc_assert (genproc->is_generic);
5522 for (g = genproc->u.generic; g; g = g->next)
5523 {
5524 gfc_symbol* target;
5525 gfc_actual_arglist* args;
5526 bool matches;
5527
5528 gcc_assert (g->specific);
5529
5530 if (g->specific->error)
5531 continue;
5532
5533 target = g->specific->u.specific->n.sym;
5534
5535 /* Get the right arglist by handling PASS/NOPASS. */
5536 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5537 if (!g->specific->nopass)
5538 {
5539 gfc_expr* po;
5540 po = extract_compcall_passed_object (e);
5541 if (!po)
5542 {
5543 gfc_free_actual_arglist (args);
5544 return false;
5545 }
5546
5547 gcc_assert (g->specific->pass_arg_num > 0);
5548 gcc_assert (!g->specific->error);
5549 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5550 g->specific->pass_arg);
5551 }
5552 resolve_actual_arglist (args, target->attr.proc,
5553 is_external_proc (target)
5554 && gfc_sym_get_dummy_args (target) == NULL);
5555
5556 /* Check if this arglist matches the formal. */
5557 matches = gfc_arglist_matches_symbol (&args, target);
5558
5559 /* Clean up and break out of the loop if we've found it. */
5560 gfc_free_actual_arglist (args);
5561 if (matches)
5562 {
5563 e->value.compcall.tbp = g->specific;
5564 genname = g->specific_st->name;
5565 /* Pass along the name for CLASS methods, where the vtab
5566 procedure pointer component has to be referenced. */
5567 if (name)
5568 *name = genname;
5569 goto success;
5570 }
5571 }
5572 }
5573
5574 /* Nothing matching found! */
5575 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5576 " '%s' at %L", genname, &e->where);
5577 return false;
5578
5579 success:
5580 /* Make sure that we have the right specific instance for the name. */
5581 derived = get_declared_from_expr (NULL, NULL, e, true);
5582
5583 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5584 if (st)
5585 e->value.compcall.tbp = st->n.tb;
5586
5587 return true;
5588 }
5589
5590
5591 /* Resolve a call to a type-bound subroutine. */
5592
5593 static bool
5594 resolve_typebound_call (gfc_code* c, const char **name)
5595 {
5596 gfc_actual_arglist* newactual;
5597 gfc_symtree* target;
5598
5599 /* Check that's really a SUBROUTINE. */
5600 if (!c->expr1->value.compcall.tbp->subroutine)
5601 {
5602 gfc_error ("'%s' at %L should be a SUBROUTINE",
5603 c->expr1->value.compcall.name, &c->loc);
5604 return false;
5605 }
5606
5607 if (!check_typebound_baseobject (c->expr1))
5608 return false;
5609
5610 /* Pass along the name for CLASS methods, where the vtab
5611 procedure pointer component has to be referenced. */
5612 if (name)
5613 *name = c->expr1->value.compcall.name;
5614
5615 if (!resolve_typebound_generic_call (c->expr1, name))
5616 return false;
5617
5618 /* Transform into an ordinary EXEC_CALL for now. */
5619
5620 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5621 return false;
5622
5623 c->ext.actual = newactual;
5624 c->symtree = target;
5625 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5626
5627 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5628
5629 gfc_free_expr (c->expr1);
5630 c->expr1 = gfc_get_expr ();
5631 c->expr1->expr_type = EXPR_FUNCTION;
5632 c->expr1->symtree = target;
5633 c->expr1->where = c->loc;
5634
5635 return resolve_call (c);
5636 }
5637
5638
5639 /* Resolve a component-call expression. */
5640 static bool
5641 resolve_compcall (gfc_expr* e, const char **name)
5642 {
5643 gfc_actual_arglist* newactual;
5644 gfc_symtree* target;
5645
5646 /* Check that's really a FUNCTION. */
5647 if (!e->value.compcall.tbp->function)
5648 {
5649 gfc_error ("'%s' at %L should be a FUNCTION",
5650 e->value.compcall.name, &e->where);
5651 return false;
5652 }
5653
5654 /* These must not be assign-calls! */
5655 gcc_assert (!e->value.compcall.assign);
5656
5657 if (!check_typebound_baseobject (e))
5658 return false;
5659
5660 /* Pass along the name for CLASS methods, where the vtab
5661 procedure pointer component has to be referenced. */
5662 if (name)
5663 *name = e->value.compcall.name;
5664
5665 if (!resolve_typebound_generic_call (e, name))
5666 return false;
5667 gcc_assert (!e->value.compcall.tbp->is_generic);
5668
5669 /* Take the rank from the function's symbol. */
5670 if (e->value.compcall.tbp->u.specific->n.sym->as)
5671 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5672
5673 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5674 arglist to the TBP's binding target. */
5675
5676 if (!resolve_typebound_static (e, &target, &newactual))
5677 return false;
5678
5679 e->value.function.actual = newactual;
5680 e->value.function.name = NULL;
5681 e->value.function.esym = target->n.sym;
5682 e->value.function.isym = NULL;
5683 e->symtree = target;
5684 e->ts = target->n.sym->ts;
5685 e->expr_type = EXPR_FUNCTION;
5686
5687 /* Resolution is not necessary if this is a class subroutine; this
5688 function only has to identify the specific proc. Resolution of
5689 the call will be done next in resolve_typebound_call. */
5690 return gfc_resolve_expr (e);
5691 }
5692
5693
5694 static bool resolve_fl_derived (gfc_symbol *sym);
5695
5696
5697 /* Resolve a typebound function, or 'method'. First separate all
5698 the non-CLASS references by calling resolve_compcall directly. */
5699
5700 static bool
5701 resolve_typebound_function (gfc_expr* e)
5702 {
5703 gfc_symbol *declared;
5704 gfc_component *c;
5705 gfc_ref *new_ref;
5706 gfc_ref *class_ref;
5707 gfc_symtree *st;
5708 const char *name;
5709 gfc_typespec ts;
5710 gfc_expr *expr;
5711 bool overridable;
5712
5713 st = e->symtree;
5714
5715 /* Deal with typebound operators for CLASS objects. */
5716 expr = e->value.compcall.base_object;
5717 overridable = !e->value.compcall.tbp->non_overridable;
5718 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5719 {
5720 /* If the base_object is not a variable, the corresponding actual
5721 argument expression must be stored in e->base_expression so
5722 that the corresponding tree temporary can be used as the base
5723 object in gfc_conv_procedure_call. */
5724 if (expr->expr_type != EXPR_VARIABLE)
5725 {
5726 gfc_actual_arglist *args;
5727
5728 for (args= e->value.function.actual; args; args = args->next)
5729 {
5730 if (expr == args->expr)
5731 expr = args->expr;
5732 }
5733 }
5734
5735 /* Since the typebound operators are generic, we have to ensure
5736 that any delays in resolution are corrected and that the vtab
5737 is present. */
5738 ts = expr->ts;
5739 declared = ts.u.derived;
5740 c = gfc_find_component (declared, "_vptr", true, true);
5741 if (c->ts.u.derived == NULL)
5742 c->ts.u.derived = gfc_find_derived_vtab (declared);
5743
5744 if (!resolve_compcall (e, &name))
5745 return false;
5746
5747 /* Use the generic name if it is there. */
5748 name = name ? name : e->value.function.esym->name;
5749 e->symtree = expr->symtree;
5750 e->ref = gfc_copy_ref (expr->ref);
5751 get_declared_from_expr (&class_ref, NULL, e, false);
5752
5753 /* Trim away the extraneous references that emerge from nested
5754 use of interface.c (extend_expr). */
5755 if (class_ref && class_ref->next)
5756 {
5757 gfc_free_ref_list (class_ref->next);
5758 class_ref->next = NULL;
5759 }
5760 else if (e->ref && !class_ref)
5761 {
5762 gfc_free_ref_list (e->ref);
5763 e->ref = NULL;
5764 }
5765
5766 gfc_add_vptr_component (e);
5767 gfc_add_component_ref (e, name);
5768 e->value.function.esym = NULL;
5769 if (expr->expr_type != EXPR_VARIABLE)
5770 e->base_expr = expr;
5771 return true;
5772 }
5773
5774 if (st == NULL)
5775 return resolve_compcall (e, NULL);
5776
5777 if (!resolve_ref (e))
5778 return false;
5779
5780 /* Get the CLASS declared type. */
5781 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5782
5783 if (!resolve_fl_derived (declared))
5784 return false;
5785
5786 /* Weed out cases of the ultimate component being a derived type. */
5787 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5788 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5789 {
5790 gfc_free_ref_list (new_ref);
5791 return resolve_compcall (e, NULL);
5792 }
5793
5794 c = gfc_find_component (declared, "_data", true, true);
5795 declared = c->ts.u.derived;
5796
5797 /* Treat the call as if it is a typebound procedure, in order to roll
5798 out the correct name for the specific function. */
5799 if (!resolve_compcall (e, &name))
5800 {
5801 gfc_free_ref_list (new_ref);
5802 return false;
5803 }
5804 ts = e->ts;
5805
5806 if (overridable)
5807 {
5808 /* Convert the expression to a procedure pointer component call. */
5809 e->value.function.esym = NULL;
5810 e->symtree = st;
5811
5812 if (new_ref)
5813 e->ref = new_ref;
5814
5815 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5816 gfc_add_vptr_component (e);
5817 gfc_add_component_ref (e, name);
5818
5819 /* Recover the typespec for the expression. This is really only
5820 necessary for generic procedures, where the additional call
5821 to gfc_add_component_ref seems to throw the collection of the
5822 correct typespec. */
5823 e->ts = ts;
5824 }
5825 else if (new_ref)
5826 gfc_free_ref_list (new_ref);
5827
5828 return true;
5829 }
5830
5831 /* Resolve a typebound subroutine, or 'method'. First separate all
5832 the non-CLASS references by calling resolve_typebound_call
5833 directly. */
5834
5835 static bool
5836 resolve_typebound_subroutine (gfc_code *code)
5837 {
5838 gfc_symbol *declared;
5839 gfc_component *c;
5840 gfc_ref *new_ref;
5841 gfc_ref *class_ref;
5842 gfc_symtree *st;
5843 const char *name;
5844 gfc_typespec ts;
5845 gfc_expr *expr;
5846 bool overridable;
5847
5848 st = code->expr1->symtree;
5849
5850 /* Deal with typebound operators for CLASS objects. */
5851 expr = code->expr1->value.compcall.base_object;
5852 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5853 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5854 {
5855 /* If the base_object is not a variable, the corresponding actual
5856 argument expression must be stored in e->base_expression so
5857 that the corresponding tree temporary can be used as the base
5858 object in gfc_conv_procedure_call. */
5859 if (expr->expr_type != EXPR_VARIABLE)
5860 {
5861 gfc_actual_arglist *args;
5862
5863 args= code->expr1->value.function.actual;
5864 for (; args; args = args->next)
5865 if (expr == args->expr)
5866 expr = args->expr;
5867 }
5868
5869 /* Since the typebound operators are generic, we have to ensure
5870 that any delays in resolution are corrected and that the vtab
5871 is present. */
5872 declared = expr->ts.u.derived;
5873 c = gfc_find_component (declared, "_vptr", true, true);
5874 if (c->ts.u.derived == NULL)
5875 c->ts.u.derived = gfc_find_derived_vtab (declared);
5876
5877 if (!resolve_typebound_call (code, &name))
5878 return false;
5879
5880 /* Use the generic name if it is there. */
5881 name = name ? name : code->expr1->value.function.esym->name;
5882 code->expr1->symtree = expr->symtree;
5883 code->expr1->ref = gfc_copy_ref (expr->ref);
5884
5885 /* Trim away the extraneous references that emerge from nested
5886 use of interface.c (extend_expr). */
5887 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5888 if (class_ref && class_ref->next)
5889 {
5890 gfc_free_ref_list (class_ref->next);
5891 class_ref->next = NULL;
5892 }
5893 else if (code->expr1->ref && !class_ref)
5894 {
5895 gfc_free_ref_list (code->expr1->ref);
5896 code->expr1->ref = NULL;
5897 }
5898
5899 /* Now use the procedure in the vtable. */
5900 gfc_add_vptr_component (code->expr1);
5901 gfc_add_component_ref (code->expr1, name);
5902 code->expr1->value.function.esym = NULL;
5903 if (expr->expr_type != EXPR_VARIABLE)
5904 code->expr1->base_expr = expr;
5905 return true;
5906 }
5907
5908 if (st == NULL)
5909 return resolve_typebound_call (code, NULL);
5910
5911 if (!resolve_ref (code->expr1))
5912 return false;
5913
5914 /* Get the CLASS declared type. */
5915 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5916
5917 /* Weed out cases of the ultimate component being a derived type. */
5918 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5919 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5920 {
5921 gfc_free_ref_list (new_ref);
5922 return resolve_typebound_call (code, NULL);
5923 }
5924
5925 if (!resolve_typebound_call (code, &name))
5926 {
5927 gfc_free_ref_list (new_ref);
5928 return false;
5929 }
5930 ts = code->expr1->ts;
5931
5932 if (overridable)
5933 {
5934 /* Convert the expression to a procedure pointer component call. */
5935 code->expr1->value.function.esym = NULL;
5936 code->expr1->symtree = st;
5937
5938 if (new_ref)
5939 code->expr1->ref = new_ref;
5940
5941 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5942 gfc_add_vptr_component (code->expr1);
5943 gfc_add_component_ref (code->expr1, name);
5944
5945 /* Recover the typespec for the expression. This is really only
5946 necessary for generic procedures, where the additional call
5947 to gfc_add_component_ref seems to throw the collection of the
5948 correct typespec. */
5949 code->expr1->ts = ts;
5950 }
5951 else if (new_ref)
5952 gfc_free_ref_list (new_ref);
5953
5954 return true;
5955 }
5956
5957
5958 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5959
5960 static bool
5961 resolve_ppc_call (gfc_code* c)
5962 {
5963 gfc_component *comp;
5964
5965 comp = gfc_get_proc_ptr_comp (c->expr1);
5966 gcc_assert (comp != NULL);
5967
5968 c->resolved_sym = c->expr1->symtree->n.sym;
5969 c->expr1->expr_type = EXPR_VARIABLE;
5970
5971 if (!comp->attr.subroutine)
5972 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5973
5974 if (!resolve_ref (c->expr1))
5975 return false;
5976
5977 if (!update_ppc_arglist (c->expr1))
5978 return false;
5979
5980 c->ext.actual = c->expr1->value.compcall.actual;
5981
5982 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5983 !(comp->ts.interface
5984 && comp->ts.interface->formal)))
5985 return false;
5986
5987 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5988
5989 return true;
5990 }
5991
5992
5993 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5994
5995 static bool
5996 resolve_expr_ppc (gfc_expr* e)
5997 {
5998 gfc_component *comp;
5999
6000 comp = gfc_get_proc_ptr_comp (e);
6001 gcc_assert (comp != NULL);
6002
6003 /* Convert to EXPR_FUNCTION. */
6004 e->expr_type = EXPR_FUNCTION;
6005 e->value.function.isym = NULL;
6006 e->value.function.actual = e->value.compcall.actual;
6007 e->ts = comp->ts;
6008 if (comp->as != NULL)
6009 e->rank = comp->as->rank;
6010
6011 if (!comp->attr.function)
6012 gfc_add_function (&comp->attr, comp->name, &e->where);
6013
6014 if (!resolve_ref (e))
6015 return false;
6016
6017 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6018 !(comp->ts.interface
6019 && comp->ts.interface->formal)))
6020 return false;
6021
6022 if (!update_ppc_arglist (e))
6023 return false;
6024
6025 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6026
6027 return true;
6028 }
6029
6030
6031 static bool
6032 gfc_is_expandable_expr (gfc_expr *e)
6033 {
6034 gfc_constructor *con;
6035
6036 if (e->expr_type == EXPR_ARRAY)
6037 {
6038 /* Traverse the constructor looking for variables that are flavor
6039 parameter. Parameters must be expanded since they are fully used at
6040 compile time. */
6041 con = gfc_constructor_first (e->value.constructor);
6042 for (; con; con = gfc_constructor_next (con))
6043 {
6044 if (con->expr->expr_type == EXPR_VARIABLE
6045 && con->expr->symtree
6046 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6047 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6048 return true;
6049 if (con->expr->expr_type == EXPR_ARRAY
6050 && gfc_is_expandable_expr (con->expr))
6051 return true;
6052 }
6053 }
6054
6055 return false;
6056 }
6057
6058 /* Resolve an expression. That is, make sure that types of operands agree
6059 with their operators, intrinsic operators are converted to function calls
6060 for overloaded types and unresolved function references are resolved. */
6061
6062 bool
6063 gfc_resolve_expr (gfc_expr *e)
6064 {
6065 bool t;
6066 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6067
6068 if (e == NULL)
6069 return true;
6070
6071 /* inquiry_argument only applies to variables. */
6072 inquiry_save = inquiry_argument;
6073 actual_arg_save = actual_arg;
6074 first_actual_arg_save = first_actual_arg;
6075
6076 if (e->expr_type != EXPR_VARIABLE)
6077 {
6078 inquiry_argument = false;
6079 actual_arg = false;
6080 first_actual_arg = false;
6081 }
6082
6083 switch (e->expr_type)
6084 {
6085 case EXPR_OP:
6086 t = resolve_operator (e);
6087 break;
6088
6089 case EXPR_FUNCTION:
6090 case EXPR_VARIABLE:
6091
6092 if (check_host_association (e))
6093 t = resolve_function (e);
6094 else
6095 {
6096 t = resolve_variable (e);
6097 if (t)
6098 expression_rank (e);
6099 }
6100
6101 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6102 && e->ref->type != REF_SUBSTRING)
6103 gfc_resolve_substring_charlen (e);
6104
6105 break;
6106
6107 case EXPR_COMPCALL:
6108 t = resolve_typebound_function (e);
6109 break;
6110
6111 case EXPR_SUBSTRING:
6112 t = resolve_ref (e);
6113 break;
6114
6115 case EXPR_CONSTANT:
6116 case EXPR_NULL:
6117 t = true;
6118 break;
6119
6120 case EXPR_PPC:
6121 t = resolve_expr_ppc (e);
6122 break;
6123
6124 case EXPR_ARRAY:
6125 t = false;
6126 if (!resolve_ref (e))
6127 break;
6128
6129 t = gfc_resolve_array_constructor (e);
6130 /* Also try to expand a constructor. */
6131 if (t)
6132 {
6133 expression_rank (e);
6134 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6135 gfc_expand_constructor (e, false);
6136 }
6137
6138 /* This provides the opportunity for the length of constructors with
6139 character valued function elements to propagate the string length
6140 to the expression. */
6141 if (t && e->ts.type == BT_CHARACTER)
6142 {
6143 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6144 here rather then add a duplicate test for it above. */
6145 gfc_expand_constructor (e, false);
6146 t = gfc_resolve_character_array_constructor (e);
6147 }
6148
6149 break;
6150
6151 case EXPR_STRUCTURE:
6152 t = resolve_ref (e);
6153 if (!t)
6154 break;
6155
6156 t = resolve_structure_cons (e, 0);
6157 if (!t)
6158 break;
6159
6160 t = gfc_simplify_expr (e, 0);
6161 break;
6162
6163 default:
6164 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6165 }
6166
6167 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6168 fixup_charlen (e);
6169
6170 inquiry_argument = inquiry_save;
6171 actual_arg = actual_arg_save;
6172 first_actual_arg = first_actual_arg_save;
6173
6174 return t;
6175 }
6176
6177
6178 /* Resolve an expression from an iterator. They must be scalar and have
6179 INTEGER or (optionally) REAL type. */
6180
6181 static bool
6182 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6183 const char *name_msgid)
6184 {
6185 if (!gfc_resolve_expr (expr))
6186 return false;
6187
6188 if (expr->rank != 0)
6189 {
6190 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6191 return false;
6192 }
6193
6194 if (expr->ts.type != BT_INTEGER)
6195 {
6196 if (expr->ts.type == BT_REAL)
6197 {
6198 if (real_ok)
6199 return gfc_notify_std (GFC_STD_F95_DEL,
6200 "%s at %L must be integer",
6201 _(name_msgid), &expr->where);
6202 else
6203 {
6204 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6205 &expr->where);
6206 return false;
6207 }
6208 }
6209 else
6210 {
6211 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6212 return false;
6213 }
6214 }
6215 return true;
6216 }
6217
6218
6219 /* Resolve the expressions in an iterator structure. If REAL_OK is
6220 false allow only INTEGER type iterators, otherwise allow REAL types.
6221 Set own_scope to true for ac-implied-do and data-implied-do as those
6222 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6223
6224 bool
6225 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6226 {
6227 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6228 return false;
6229
6230 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6231 _("iterator variable")))
6232 return false;
6233
6234 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6235 "Start expression in DO loop"))
6236 return false;
6237
6238 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6239 "End expression in DO loop"))
6240 return false;
6241
6242 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6243 "Step expression in DO loop"))
6244 return false;
6245
6246 if (iter->step->expr_type == EXPR_CONSTANT)
6247 {
6248 if ((iter->step->ts.type == BT_INTEGER
6249 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6250 || (iter->step->ts.type == BT_REAL
6251 && mpfr_sgn (iter->step->value.real) == 0))
6252 {
6253 gfc_error ("Step expression in DO loop at %L cannot be zero",
6254 &iter->step->where);
6255 return false;
6256 }
6257 }
6258
6259 /* Convert start, end, and step to the same type as var. */
6260 if (iter->start->ts.kind != iter->var->ts.kind
6261 || iter->start->ts.type != iter->var->ts.type)
6262 gfc_convert_type (iter->start, &iter->var->ts, 2);
6263
6264 if (iter->end->ts.kind != iter->var->ts.kind
6265 || iter->end->ts.type != iter->var->ts.type)
6266 gfc_convert_type (iter->end, &iter->var->ts, 2);
6267
6268 if (iter->step->ts.kind != iter->var->ts.kind
6269 || iter->step->ts.type != iter->var->ts.type)
6270 gfc_convert_type (iter->step, &iter->var->ts, 2);
6271
6272 if (iter->start->expr_type == EXPR_CONSTANT
6273 && iter->end->expr_type == EXPR_CONSTANT
6274 && iter->step->expr_type == EXPR_CONSTANT)
6275 {
6276 int sgn, cmp;
6277 if (iter->start->ts.type == BT_INTEGER)
6278 {
6279 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6280 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6281 }
6282 else
6283 {
6284 sgn = mpfr_sgn (iter->step->value.real);
6285 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6286 }
6287 if (gfc_option.warn_zerotrip &&
6288 ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6289 gfc_warning ("DO loop at %L will be executed zero times"
6290 " (use -Wno-zerotrip to suppress)",
6291 &iter->step->where);
6292 }
6293
6294 return true;
6295 }
6296
6297
6298 /* Traversal function for find_forall_index. f == 2 signals that
6299 that variable itself is not to be checked - only the references. */
6300
6301 static bool
6302 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6303 {
6304 if (expr->expr_type != EXPR_VARIABLE)
6305 return false;
6306
6307 /* A scalar assignment */
6308 if (!expr->ref || *f == 1)
6309 {
6310 if (expr->symtree->n.sym == sym)
6311 return true;
6312 else
6313 return false;
6314 }
6315
6316 if (*f == 2)
6317 *f = 1;
6318 return false;
6319 }
6320
6321
6322 /* Check whether the FORALL index appears in the expression or not.
6323 Returns true if SYM is found in EXPR. */
6324
6325 bool
6326 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6327 {
6328 if (gfc_traverse_expr (expr, sym, forall_index, f))
6329 return true;
6330 else
6331 return false;
6332 }
6333
6334
6335 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6336 to be a scalar INTEGER variable. The subscripts and stride are scalar
6337 INTEGERs, and if stride is a constant it must be nonzero.
6338 Furthermore "A subscript or stride in a forall-triplet-spec shall
6339 not contain a reference to any index-name in the
6340 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6341
6342 static void
6343 resolve_forall_iterators (gfc_forall_iterator *it)
6344 {
6345 gfc_forall_iterator *iter, *iter2;
6346
6347 for (iter = it; iter; iter = iter->next)
6348 {
6349 if (gfc_resolve_expr (iter->var)
6350 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6351 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6352 &iter->var->where);
6353
6354 if (gfc_resolve_expr (iter->start)
6355 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6356 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6357 &iter->start->where);
6358 if (iter->var->ts.kind != iter->start->ts.kind)
6359 gfc_convert_type (iter->start, &iter->var->ts, 1);
6360
6361 if (gfc_resolve_expr (iter->end)
6362 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6363 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6364 &iter->end->where);
6365 if (iter->var->ts.kind != iter->end->ts.kind)
6366 gfc_convert_type (iter->end, &iter->var->ts, 1);
6367
6368 if (gfc_resolve_expr (iter->stride))
6369 {
6370 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6371 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6372 &iter->stride->where, "INTEGER");
6373
6374 if (iter->stride->expr_type == EXPR_CONSTANT
6375 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6376 gfc_error ("FORALL stride expression at %L cannot be zero",
6377 &iter->stride->where);
6378 }
6379 if (iter->var->ts.kind != iter->stride->ts.kind)
6380 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6381 }
6382
6383 for (iter = it; iter; iter = iter->next)
6384 for (iter2 = iter; iter2; iter2 = iter2->next)
6385 {
6386 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6387 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6388 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6389 gfc_error ("FORALL index '%s' may not appear in triplet "
6390 "specification at %L", iter->var->symtree->name,
6391 &iter2->start->where);
6392 }
6393 }
6394
6395
6396 /* Given a pointer to a symbol that is a derived type, see if it's
6397 inaccessible, i.e. if it's defined in another module and the components are
6398 PRIVATE. The search is recursive if necessary. Returns zero if no
6399 inaccessible components are found, nonzero otherwise. */
6400
6401 static int
6402 derived_inaccessible (gfc_symbol *sym)
6403 {
6404 gfc_component *c;
6405
6406 if (sym->attr.use_assoc && sym->attr.private_comp)
6407 return 1;
6408
6409 for (c = sym->components; c; c = c->next)
6410 {
6411 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6412 return 1;
6413 }
6414
6415 return 0;
6416 }
6417
6418
6419 /* Resolve the argument of a deallocate expression. The expression must be
6420 a pointer or a full array. */
6421
6422 static bool
6423 resolve_deallocate_expr (gfc_expr *e)
6424 {
6425 symbol_attribute attr;
6426 int allocatable, pointer;
6427 gfc_ref *ref;
6428 gfc_symbol *sym;
6429 gfc_component *c;
6430 bool unlimited;
6431
6432 if (!gfc_resolve_expr (e))
6433 return false;
6434
6435 if (e->expr_type != EXPR_VARIABLE)
6436 goto bad;
6437
6438 sym = e->symtree->n.sym;
6439 unlimited = UNLIMITED_POLY(sym);
6440
6441 if (sym->ts.type == BT_CLASS)
6442 {
6443 allocatable = CLASS_DATA (sym)->attr.allocatable;
6444 pointer = CLASS_DATA (sym)->attr.class_pointer;
6445 }
6446 else
6447 {
6448 allocatable = sym->attr.allocatable;
6449 pointer = sym->attr.pointer;
6450 }
6451 for (ref = e->ref; ref; ref = ref->next)
6452 {
6453 switch (ref->type)
6454 {
6455 case REF_ARRAY:
6456 if (ref->u.ar.type != AR_FULL
6457 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6458 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6459 allocatable = 0;
6460 break;
6461
6462 case REF_COMPONENT:
6463 c = ref->u.c.component;
6464 if (c->ts.type == BT_CLASS)
6465 {
6466 allocatable = CLASS_DATA (c)->attr.allocatable;
6467 pointer = CLASS_DATA (c)->attr.class_pointer;
6468 }
6469 else
6470 {
6471 allocatable = c->attr.allocatable;
6472 pointer = c->attr.pointer;
6473 }
6474 break;
6475
6476 case REF_SUBSTRING:
6477 allocatable = 0;
6478 break;
6479 }
6480 }
6481
6482 attr = gfc_expr_attr (e);
6483
6484 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6485 {
6486 bad:
6487 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6488 &e->where);
6489 return false;
6490 }
6491
6492 /* F2008, C644. */
6493 if (gfc_is_coindexed (e))
6494 {
6495 gfc_error ("Coindexed allocatable object at %L", &e->where);
6496 return false;
6497 }
6498
6499 if (pointer
6500 && !gfc_check_vardef_context (e, true, true, false,
6501 _("DEALLOCATE object")))
6502 return false;
6503 if (!gfc_check_vardef_context (e, false, true, false,
6504 _("DEALLOCATE object")))
6505 return false;
6506
6507 return true;
6508 }
6509
6510
6511 /* Returns true if the expression e contains a reference to the symbol sym. */
6512 static bool
6513 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6514 {
6515 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6516 return true;
6517
6518 return false;
6519 }
6520
6521 bool
6522 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6523 {
6524 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6525 }
6526
6527
6528 /* Given the expression node e for an allocatable/pointer of derived type to be
6529 allocated, get the expression node to be initialized afterwards (needed for
6530 derived types with default initializers, and derived types with allocatable
6531 components that need nullification.) */
6532
6533 gfc_expr *
6534 gfc_expr_to_initialize (gfc_expr *e)
6535 {
6536 gfc_expr *result;
6537 gfc_ref *ref;
6538 int i;
6539
6540 result = gfc_copy_expr (e);
6541
6542 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6543 for (ref = result->ref; ref; ref = ref->next)
6544 if (ref->type == REF_ARRAY && ref->next == NULL)
6545 {
6546 ref->u.ar.type = AR_FULL;
6547
6548 for (i = 0; i < ref->u.ar.dimen; i++)
6549 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6550
6551 break;
6552 }
6553
6554 gfc_free_shape (&result->shape, result->rank);
6555
6556 /* Recalculate rank, shape, etc. */
6557 gfc_resolve_expr (result);
6558 return result;
6559 }
6560
6561
6562 /* If the last ref of an expression is an array ref, return a copy of the
6563 expression with that one removed. Otherwise, a copy of the original
6564 expression. This is used for allocate-expressions and pointer assignment
6565 LHS, where there may be an array specification that needs to be stripped
6566 off when using gfc_check_vardef_context. */
6567
6568 static gfc_expr*
6569 remove_last_array_ref (gfc_expr* e)
6570 {
6571 gfc_expr* e2;
6572 gfc_ref** r;
6573
6574 e2 = gfc_copy_expr (e);
6575 for (r = &e2->ref; *r; r = &(*r)->next)
6576 if ((*r)->type == REF_ARRAY && !(*r)->next)
6577 {
6578 gfc_free_ref_list (*r);
6579 *r = NULL;
6580 break;
6581 }
6582
6583 return e2;
6584 }
6585
6586
6587 /* Used in resolve_allocate_expr to check that a allocation-object and
6588 a source-expr are conformable. This does not catch all possible
6589 cases; in particular a runtime checking is needed. */
6590
6591 static bool
6592 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6593 {
6594 gfc_ref *tail;
6595 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6596
6597 /* First compare rank. */
6598 if ((tail && e1->rank != tail->u.ar.as->rank)
6599 || (!tail && e1->rank != e2->rank))
6600 {
6601 gfc_error ("Source-expr at %L must be scalar or have the "
6602 "same rank as the allocate-object at %L",
6603 &e1->where, &e2->where);
6604 return false;
6605 }
6606
6607 if (e1->shape)
6608 {
6609 int i;
6610 mpz_t s;
6611
6612 mpz_init (s);
6613
6614 for (i = 0; i < e1->rank; i++)
6615 {
6616 if (tail->u.ar.start[i] == NULL)
6617 break;
6618
6619 if (tail->u.ar.end[i])
6620 {
6621 mpz_set (s, tail->u.ar.end[i]->value.integer);
6622 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6623 mpz_add_ui (s, s, 1);
6624 }
6625 else
6626 {
6627 mpz_set (s, tail->u.ar.start[i]->value.integer);
6628 }
6629
6630 if (mpz_cmp (e1->shape[i], s) != 0)
6631 {
6632 gfc_error ("Source-expr at %L and allocate-object at %L must "
6633 "have the same shape", &e1->where, &e2->where);
6634 mpz_clear (s);
6635 return false;
6636 }
6637 }
6638
6639 mpz_clear (s);
6640 }
6641
6642 return true;
6643 }
6644
6645
6646 /* Resolve the expression in an ALLOCATE statement, doing the additional
6647 checks to see whether the expression is OK or not. The expression must
6648 have a trailing array reference that gives the size of the array. */
6649
6650 static bool
6651 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6652 {
6653 int i, pointer, allocatable, dimension, is_abstract;
6654 int codimension;
6655 bool coindexed;
6656 bool unlimited;
6657 symbol_attribute attr;
6658 gfc_ref *ref, *ref2;
6659 gfc_expr *e2;
6660 gfc_array_ref *ar;
6661 gfc_symbol *sym = NULL;
6662 gfc_alloc *a;
6663 gfc_component *c;
6664 bool t;
6665
6666 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6667 checking of coarrays. */
6668 for (ref = e->ref; ref; ref = ref->next)
6669 if (ref->next == NULL)
6670 break;
6671
6672 if (ref && ref->type == REF_ARRAY)
6673 ref->u.ar.in_allocate = true;
6674
6675 if (!gfc_resolve_expr (e))
6676 goto failure;
6677
6678 /* Make sure the expression is allocatable or a pointer. If it is
6679 pointer, the next-to-last reference must be a pointer. */
6680
6681 ref2 = NULL;
6682 if (e->symtree)
6683 sym = e->symtree->n.sym;
6684
6685 /* Check whether ultimate component is abstract and CLASS. */
6686 is_abstract = 0;
6687
6688 /* Is the allocate-object unlimited polymorphic? */
6689 unlimited = UNLIMITED_POLY(e);
6690
6691 if (e->expr_type != EXPR_VARIABLE)
6692 {
6693 allocatable = 0;
6694 attr = gfc_expr_attr (e);
6695 pointer = attr.pointer;
6696 dimension = attr.dimension;
6697 codimension = attr.codimension;
6698 }
6699 else
6700 {
6701 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6702 {
6703 allocatable = CLASS_DATA (sym)->attr.allocatable;
6704 pointer = CLASS_DATA (sym)->attr.class_pointer;
6705 dimension = CLASS_DATA (sym)->attr.dimension;
6706 codimension = CLASS_DATA (sym)->attr.codimension;
6707 is_abstract = CLASS_DATA (sym)->attr.abstract;
6708 }
6709 else
6710 {
6711 allocatable = sym->attr.allocatable;
6712 pointer = sym->attr.pointer;
6713 dimension = sym->attr.dimension;
6714 codimension = sym->attr.codimension;
6715 }
6716
6717 coindexed = false;
6718
6719 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6720 {
6721 switch (ref->type)
6722 {
6723 case REF_ARRAY:
6724 if (ref->u.ar.codimen > 0)
6725 {
6726 int n;
6727 for (n = ref->u.ar.dimen;
6728 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6729 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6730 {
6731 coindexed = true;
6732 break;
6733 }
6734 }
6735
6736 if (ref->next != NULL)
6737 pointer = 0;
6738 break;
6739
6740 case REF_COMPONENT:
6741 /* F2008, C644. */
6742 if (coindexed)
6743 {
6744 gfc_error ("Coindexed allocatable object at %L",
6745 &e->where);
6746 goto failure;
6747 }
6748
6749 c = ref->u.c.component;
6750 if (c->ts.type == BT_CLASS)
6751 {
6752 allocatable = CLASS_DATA (c)->attr.allocatable;
6753 pointer = CLASS_DATA (c)->attr.class_pointer;
6754 dimension = CLASS_DATA (c)->attr.dimension;
6755 codimension = CLASS_DATA (c)->attr.codimension;
6756 is_abstract = CLASS_DATA (c)->attr.abstract;
6757 }
6758 else
6759 {
6760 allocatable = c->attr.allocatable;
6761 pointer = c->attr.pointer;
6762 dimension = c->attr.dimension;
6763 codimension = c->attr.codimension;
6764 is_abstract = c->attr.abstract;
6765 }
6766 break;
6767
6768 case REF_SUBSTRING:
6769 allocatable = 0;
6770 pointer = 0;
6771 break;
6772 }
6773 }
6774 }
6775
6776 /* Check for F08:C628. */
6777 if (allocatable == 0 && pointer == 0 && !unlimited)
6778 {
6779 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6780 &e->where);
6781 goto failure;
6782 }
6783
6784 /* Some checks for the SOURCE tag. */
6785 if (code->expr3)
6786 {
6787 /* Check F03:C631. */
6788 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6789 {
6790 gfc_error ("Type of entity at %L is type incompatible with "
6791 "source-expr at %L", &e->where, &code->expr3->where);
6792 goto failure;
6793 }
6794
6795 /* Check F03:C632 and restriction following Note 6.18. */
6796 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
6797 goto failure;
6798
6799 /* Check F03:C633. */
6800 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6801 {
6802 gfc_error ("The allocate-object at %L and the source-expr at %L "
6803 "shall have the same kind type parameter",
6804 &e->where, &code->expr3->where);
6805 goto failure;
6806 }
6807
6808 /* Check F2008, C642. */
6809 if (code->expr3->ts.type == BT_DERIVED
6810 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6811 || (code->expr3->ts.u.derived->from_intmod
6812 == INTMOD_ISO_FORTRAN_ENV
6813 && code->expr3->ts.u.derived->intmod_sym_id
6814 == ISOFORTRAN_LOCK_TYPE)))
6815 {
6816 gfc_error ("The source-expr at %L shall neither be of type "
6817 "LOCK_TYPE nor have a LOCK_TYPE component if "
6818 "allocate-object at %L is a coarray",
6819 &code->expr3->where, &e->where);
6820 goto failure;
6821 }
6822 }
6823
6824 /* Check F08:C629. */
6825 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6826 && !code->expr3)
6827 {
6828 gcc_assert (e->ts.type == BT_CLASS);
6829 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6830 "type-spec or source-expr", sym->name, &e->where);
6831 goto failure;
6832 }
6833
6834 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6835 {
6836 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6837 code->ext.alloc.ts.u.cl->length);
6838 if (cmp == 1 || cmp == -1 || cmp == -3)
6839 {
6840 gfc_error ("Allocating %s at %L with type-spec requires the same "
6841 "character-length parameter as in the declaration",
6842 sym->name, &e->where);
6843 goto failure;
6844 }
6845 }
6846
6847 /* In the variable definition context checks, gfc_expr_attr is used
6848 on the expression. This is fooled by the array specification
6849 present in e, thus we have to eliminate that one temporarily. */
6850 e2 = remove_last_array_ref (e);
6851 t = true;
6852 if (t && pointer)
6853 t = gfc_check_vardef_context (e2, true, true, false,
6854 _("ALLOCATE object"));
6855 if (t)
6856 t = gfc_check_vardef_context (e2, false, true, false,
6857 _("ALLOCATE object"));
6858 gfc_free_expr (e2);
6859 if (!t)
6860 goto failure;
6861
6862 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6863 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6864 {
6865 /* For class arrays, the initialization with SOURCE is done
6866 using _copy and trans_call. It is convenient to exploit that
6867 when the allocated type is different from the declared type but
6868 no SOURCE exists by setting expr3. */
6869 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6870 }
6871 else if (!code->expr3)
6872 {
6873 /* Set up default initializer if needed. */
6874 gfc_typespec ts;
6875 gfc_expr *init_e;
6876
6877 if (code->ext.alloc.ts.type == BT_DERIVED)
6878 ts = code->ext.alloc.ts;
6879 else
6880 ts = e->ts;
6881
6882 if (ts.type == BT_CLASS)
6883 ts = ts.u.derived->components->ts;
6884
6885 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6886 {
6887 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6888 init_st->loc = code->loc;
6889 init_st->expr1 = gfc_expr_to_initialize (e);
6890 init_st->expr2 = init_e;
6891 init_st->next = code->next;
6892 code->next = init_st;
6893 }
6894 }
6895 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6896 {
6897 /* Default initialization via MOLD (non-polymorphic). */
6898 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6899 gfc_resolve_expr (rhs);
6900 gfc_free_expr (code->expr3);
6901 code->expr3 = rhs;
6902 }
6903
6904 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6905 {
6906 /* Make sure the vtab symbol is present when
6907 the module variables are generated. */
6908 gfc_typespec ts = e->ts;
6909 if (code->expr3)
6910 ts = code->expr3->ts;
6911 else if (code->ext.alloc.ts.type == BT_DERIVED)
6912 ts = code->ext.alloc.ts;
6913
6914 gfc_find_derived_vtab (ts.u.derived);
6915
6916 if (dimension)
6917 e = gfc_expr_to_initialize (e);
6918 }
6919 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6920 {
6921 /* Again, make sure the vtab symbol is present when
6922 the module variables are generated. */
6923 gfc_typespec *ts = NULL;
6924 if (code->expr3)
6925 ts = &code->expr3->ts;
6926 else
6927 ts = &code->ext.alloc.ts;
6928
6929 gcc_assert (ts);
6930
6931 gfc_find_vtab (ts);
6932
6933 if (dimension)
6934 e = gfc_expr_to_initialize (e);
6935 }
6936
6937 if (dimension == 0 && codimension == 0)
6938 goto success;
6939
6940 /* Make sure the last reference node is an array specification. */
6941
6942 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6943 || (dimension && ref2->u.ar.dimen == 0))
6944 {
6945 gfc_error ("Array specification required in ALLOCATE statement "
6946 "at %L", &e->where);
6947 goto failure;
6948 }
6949
6950 /* Make sure that the array section reference makes sense in the
6951 context of an ALLOCATE specification. */
6952
6953 ar = &ref2->u.ar;
6954
6955 if (codimension)
6956 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6957 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6958 {
6959 gfc_error ("Coarray specification required in ALLOCATE statement "
6960 "at %L", &e->where);
6961 goto failure;
6962 }
6963
6964 for (i = 0; i < ar->dimen; i++)
6965 {
6966 if (ref2->u.ar.type == AR_ELEMENT)
6967 goto check_symbols;
6968
6969 switch (ar->dimen_type[i])
6970 {
6971 case DIMEN_ELEMENT:
6972 break;
6973
6974 case DIMEN_RANGE:
6975 if (ar->start[i] != NULL
6976 && ar->end[i] != NULL
6977 && ar->stride[i] == NULL)
6978 break;
6979
6980 /* Fall Through... */
6981
6982 case DIMEN_UNKNOWN:
6983 case DIMEN_VECTOR:
6984 case DIMEN_STAR:
6985 case DIMEN_THIS_IMAGE:
6986 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6987 &e->where);
6988 goto failure;
6989 }
6990
6991 check_symbols:
6992 for (a = code->ext.alloc.list; a; a = a->next)
6993 {
6994 sym = a->expr->symtree->n.sym;
6995
6996 /* TODO - check derived type components. */
6997 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6998 continue;
6999
7000 if ((ar->start[i] != NULL
7001 && gfc_find_sym_in_expr (sym, ar->start[i]))
7002 || (ar->end[i] != NULL
7003 && gfc_find_sym_in_expr (sym, ar->end[i])))
7004 {
7005 gfc_error ("'%s' must not appear in the array specification at "
7006 "%L in the same ALLOCATE statement where it is "
7007 "itself allocated", sym->name, &ar->where);
7008 goto failure;
7009 }
7010 }
7011 }
7012
7013 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7014 {
7015 if (ar->dimen_type[i] == DIMEN_ELEMENT
7016 || ar->dimen_type[i] == DIMEN_RANGE)
7017 {
7018 if (i == (ar->dimen + ar->codimen - 1))
7019 {
7020 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7021 "statement at %L", &e->where);
7022 goto failure;
7023 }
7024 continue;
7025 }
7026
7027 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7028 && ar->stride[i] == NULL)
7029 break;
7030
7031 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7032 &e->where);
7033 goto failure;
7034 }
7035
7036 success:
7037 return true;
7038
7039 failure:
7040 return false;
7041 }
7042
7043 static void
7044 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7045 {
7046 gfc_expr *stat, *errmsg, *pe, *qe;
7047 gfc_alloc *a, *p, *q;
7048
7049 stat = code->expr1;
7050 errmsg = code->expr2;
7051
7052 /* Check the stat variable. */
7053 if (stat)
7054 {
7055 gfc_check_vardef_context (stat, false, false, false,
7056 _("STAT variable"));
7057
7058 if ((stat->ts.type != BT_INTEGER
7059 && !(stat->ref && (stat->ref->type == REF_ARRAY
7060 || stat->ref->type == REF_COMPONENT)))
7061 || stat->rank > 0)
7062 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7063 "variable", &stat->where);
7064
7065 for (p = code->ext.alloc.list; p; p = p->next)
7066 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7067 {
7068 gfc_ref *ref1, *ref2;
7069 bool found = true;
7070
7071 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7072 ref1 = ref1->next, ref2 = ref2->next)
7073 {
7074 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7075 continue;
7076 if (ref1->u.c.component->name != ref2->u.c.component->name)
7077 {
7078 found = false;
7079 break;
7080 }
7081 }
7082
7083 if (found)
7084 {
7085 gfc_error ("Stat-variable at %L shall not be %sd within "
7086 "the same %s statement", &stat->where, fcn, fcn);
7087 break;
7088 }
7089 }
7090 }
7091
7092 /* Check the errmsg variable. */
7093 if (errmsg)
7094 {
7095 if (!stat)
7096 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7097 &errmsg->where);
7098
7099 gfc_check_vardef_context (errmsg, false, false, false,
7100 _("ERRMSG variable"));
7101
7102 if ((errmsg->ts.type != BT_CHARACTER
7103 && !(errmsg->ref
7104 && (errmsg->ref->type == REF_ARRAY
7105 || errmsg->ref->type == REF_COMPONENT)))
7106 || errmsg->rank > 0 )
7107 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7108 "variable", &errmsg->where);
7109
7110 for (p = code->ext.alloc.list; p; p = p->next)
7111 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7112 {
7113 gfc_ref *ref1, *ref2;
7114 bool found = true;
7115
7116 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7117 ref1 = ref1->next, ref2 = ref2->next)
7118 {
7119 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7120 continue;
7121 if (ref1->u.c.component->name != ref2->u.c.component->name)
7122 {
7123 found = false;
7124 break;
7125 }
7126 }
7127
7128 if (found)
7129 {
7130 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7131 "the same %s statement", &errmsg->where, fcn, fcn);
7132 break;
7133 }
7134 }
7135 }
7136
7137 /* Check that an allocate-object appears only once in the statement. */
7138
7139 for (p = code->ext.alloc.list; p; p = p->next)
7140 {
7141 pe = p->expr;
7142 for (q = p->next; q; q = q->next)
7143 {
7144 qe = q->expr;
7145 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7146 {
7147 /* This is a potential collision. */
7148 gfc_ref *pr = pe->ref;
7149 gfc_ref *qr = qe->ref;
7150
7151 /* Follow the references until
7152 a) They start to differ, in which case there is no error;
7153 you can deallocate a%b and a%c in a single statement
7154 b) Both of them stop, which is an error
7155 c) One of them stops, which is also an error. */
7156 while (1)
7157 {
7158 if (pr == NULL && qr == NULL)
7159 {
7160 gfc_error ("Allocate-object at %L also appears at %L",
7161 &pe->where, &qe->where);
7162 break;
7163 }
7164 else if (pr != NULL && qr == NULL)
7165 {
7166 gfc_error ("Allocate-object at %L is subobject of"
7167 " object at %L", &pe->where, &qe->where);
7168 break;
7169 }
7170 else if (pr == NULL && qr != NULL)
7171 {
7172 gfc_error ("Allocate-object at %L is subobject of"
7173 " object at %L", &qe->where, &pe->where);
7174 break;
7175 }
7176 /* Here, pr != NULL && qr != NULL */
7177 gcc_assert(pr->type == qr->type);
7178 if (pr->type == REF_ARRAY)
7179 {
7180 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7181 which are legal. */
7182 gcc_assert (qr->type == REF_ARRAY);
7183
7184 if (pr->next && qr->next)
7185 {
7186 int i;
7187 gfc_array_ref *par = &(pr->u.ar);
7188 gfc_array_ref *qar = &(qr->u.ar);
7189
7190 for (i=0; i<par->dimen; i++)
7191 {
7192 if ((par->start[i] != NULL
7193 || qar->start[i] != NULL)
7194 && gfc_dep_compare_expr (par->start[i],
7195 qar->start[i]) != 0)
7196 goto break_label;
7197 }
7198 }
7199 }
7200 else
7201 {
7202 if (pr->u.c.component->name != qr->u.c.component->name)
7203 break;
7204 }
7205
7206 pr = pr->next;
7207 qr = qr->next;
7208 }
7209 break_label:
7210 ;
7211 }
7212 }
7213 }
7214
7215 if (strcmp (fcn, "ALLOCATE") == 0)
7216 {
7217 for (a = code->ext.alloc.list; a; a = a->next)
7218 resolve_allocate_expr (a->expr, code);
7219 }
7220 else
7221 {
7222 for (a = code->ext.alloc.list; a; a = a->next)
7223 resolve_deallocate_expr (a->expr);
7224 }
7225 }
7226
7227
7228 /************ SELECT CASE resolution subroutines ************/
7229
7230 /* Callback function for our mergesort variant. Determines interval
7231 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7232 op1 > op2. Assumes we're not dealing with the default case.
7233 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7234 There are nine situations to check. */
7235
7236 static int
7237 compare_cases (const gfc_case *op1, const gfc_case *op2)
7238 {
7239 int retval;
7240
7241 if (op1->low == NULL) /* op1 = (:L) */
7242 {
7243 /* op2 = (:N), so overlap. */
7244 retval = 0;
7245 /* op2 = (M:) or (M:N), L < M */
7246 if (op2->low != NULL
7247 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7248 retval = -1;
7249 }
7250 else if (op1->high == NULL) /* op1 = (K:) */
7251 {
7252 /* op2 = (M:), so overlap. */
7253 retval = 0;
7254 /* op2 = (:N) or (M:N), K > N */
7255 if (op2->high != NULL
7256 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7257 retval = 1;
7258 }
7259 else /* op1 = (K:L) */
7260 {
7261 if (op2->low == NULL) /* op2 = (:N), K > N */
7262 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7263 ? 1 : 0;
7264 else if (op2->high == NULL) /* op2 = (M:), L < M */
7265 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7266 ? -1 : 0;
7267 else /* op2 = (M:N) */
7268 {
7269 retval = 0;
7270 /* L < M */
7271 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7272 retval = -1;
7273 /* K > N */
7274 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7275 retval = 1;
7276 }
7277 }
7278
7279 return retval;
7280 }
7281
7282
7283 /* Merge-sort a double linked case list, detecting overlap in the
7284 process. LIST is the head of the double linked case list before it
7285 is sorted. Returns the head of the sorted list if we don't see any
7286 overlap, or NULL otherwise. */
7287
7288 static gfc_case *
7289 check_case_overlap (gfc_case *list)
7290 {
7291 gfc_case *p, *q, *e, *tail;
7292 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7293
7294 /* If the passed list was empty, return immediately. */
7295 if (!list)
7296 return NULL;
7297
7298 overlap_seen = 0;
7299 insize = 1;
7300
7301 /* Loop unconditionally. The only exit from this loop is a return
7302 statement, when we've finished sorting the case list. */
7303 for (;;)
7304 {
7305 p = list;
7306 list = NULL;
7307 tail = NULL;
7308
7309 /* Count the number of merges we do in this pass. */
7310 nmerges = 0;
7311
7312 /* Loop while there exists a merge to be done. */
7313 while (p)
7314 {
7315 int i;
7316
7317 /* Count this merge. */
7318 nmerges++;
7319
7320 /* Cut the list in two pieces by stepping INSIZE places
7321 forward in the list, starting from P. */
7322 psize = 0;
7323 q = p;
7324 for (i = 0; i < insize; i++)
7325 {
7326 psize++;
7327 q = q->right;
7328 if (!q)
7329 break;
7330 }
7331 qsize = insize;
7332
7333 /* Now we have two lists. Merge them! */
7334 while (psize > 0 || (qsize > 0 && q != NULL))
7335 {
7336 /* See from which the next case to merge comes from. */
7337 if (psize == 0)
7338 {
7339 /* P is empty so the next case must come from Q. */
7340 e = q;
7341 q = q->right;
7342 qsize--;
7343 }
7344 else if (qsize == 0 || q == NULL)
7345 {
7346 /* Q is empty. */
7347 e = p;
7348 p = p->right;
7349 psize--;
7350 }
7351 else
7352 {
7353 cmp = compare_cases (p, q);
7354 if (cmp < 0)
7355 {
7356 /* The whole case range for P is less than the
7357 one for Q. */
7358 e = p;
7359 p = p->right;
7360 psize--;
7361 }
7362 else if (cmp > 0)
7363 {
7364 /* The whole case range for Q is greater than
7365 the case range for P. */
7366 e = q;
7367 q = q->right;
7368 qsize--;
7369 }
7370 else
7371 {
7372 /* The cases overlap, or they are the same
7373 element in the list. Either way, we must
7374 issue an error and get the next case from P. */
7375 /* FIXME: Sort P and Q by line number. */
7376 gfc_error ("CASE label at %L overlaps with CASE "
7377 "label at %L", &p->where, &q->where);
7378 overlap_seen = 1;
7379 e = p;
7380 p = p->right;
7381 psize--;
7382 }
7383 }
7384
7385 /* Add the next element to the merged list. */
7386 if (tail)
7387 tail->right = e;
7388 else
7389 list = e;
7390 e->left = tail;
7391 tail = e;
7392 }
7393
7394 /* P has now stepped INSIZE places along, and so has Q. So
7395 they're the same. */
7396 p = q;
7397 }
7398 tail->right = NULL;
7399
7400 /* If we have done only one merge or none at all, we've
7401 finished sorting the cases. */
7402 if (nmerges <= 1)
7403 {
7404 if (!overlap_seen)
7405 return list;
7406 else
7407 return NULL;
7408 }
7409
7410 /* Otherwise repeat, merging lists twice the size. */
7411 insize *= 2;
7412 }
7413 }
7414
7415
7416 /* Check to see if an expression is suitable for use in a CASE statement.
7417 Makes sure that all case expressions are scalar constants of the same
7418 type. Return false if anything is wrong. */
7419
7420 static bool
7421 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7422 {
7423 if (e == NULL) return true;
7424
7425 if (e->ts.type != case_expr->ts.type)
7426 {
7427 gfc_error ("Expression in CASE statement at %L must be of type %s",
7428 &e->where, gfc_basic_typename (case_expr->ts.type));
7429 return false;
7430 }
7431
7432 /* C805 (R808) For a given case-construct, each case-value shall be of
7433 the same type as case-expr. For character type, length differences
7434 are allowed, but the kind type parameters shall be the same. */
7435
7436 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7437 {
7438 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7439 &e->where, case_expr->ts.kind);
7440 return false;
7441 }
7442
7443 /* Convert the case value kind to that of case expression kind,
7444 if needed */
7445
7446 if (e->ts.kind != case_expr->ts.kind)
7447 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7448
7449 if (e->rank != 0)
7450 {
7451 gfc_error ("Expression in CASE statement at %L must be scalar",
7452 &e->where);
7453 return false;
7454 }
7455
7456 return true;
7457 }
7458
7459
7460 /* Given a completely parsed select statement, we:
7461
7462 - Validate all expressions and code within the SELECT.
7463 - Make sure that the selection expression is not of the wrong type.
7464 - Make sure that no case ranges overlap.
7465 - Eliminate unreachable cases and unreachable code resulting from
7466 removing case labels.
7467
7468 The standard does allow unreachable cases, e.g. CASE (5:3). But
7469 they are a hassle for code generation, and to prevent that, we just
7470 cut them out here. This is not necessary for overlapping cases
7471 because they are illegal and we never even try to generate code.
7472
7473 We have the additional caveat that a SELECT construct could have
7474 been a computed GOTO in the source code. Fortunately we can fairly
7475 easily work around that here: The case_expr for a "real" SELECT CASE
7476 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7477 we have to do is make sure that the case_expr is a scalar integer
7478 expression. */
7479
7480 static void
7481 resolve_select (gfc_code *code, bool select_type)
7482 {
7483 gfc_code *body;
7484 gfc_expr *case_expr;
7485 gfc_case *cp, *default_case, *tail, *head;
7486 int seen_unreachable;
7487 int seen_logical;
7488 int ncases;
7489 bt type;
7490 bool t;
7491
7492 if (code->expr1 == NULL)
7493 {
7494 /* This was actually a computed GOTO statement. */
7495 case_expr = code->expr2;
7496 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7497 gfc_error ("Selection expression in computed GOTO statement "
7498 "at %L must be a scalar integer expression",
7499 &case_expr->where);
7500
7501 /* Further checking is not necessary because this SELECT was built
7502 by the compiler, so it should always be OK. Just move the
7503 case_expr from expr2 to expr so that we can handle computed
7504 GOTOs as normal SELECTs from here on. */
7505 code->expr1 = code->expr2;
7506 code->expr2 = NULL;
7507 return;
7508 }
7509
7510 case_expr = code->expr1;
7511 type = case_expr->ts.type;
7512
7513 /* F08:C830. */
7514 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7515 {
7516 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7517 &case_expr->where, gfc_typename (&case_expr->ts));
7518
7519 /* Punt. Going on here just produce more garbage error messages. */
7520 return;
7521 }
7522
7523 /* F08:R842. */
7524 if (!select_type && case_expr->rank != 0)
7525 {
7526 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7527 "expression", &case_expr->where);
7528
7529 /* Punt. */
7530 return;
7531 }
7532
7533 /* Raise a warning if an INTEGER case value exceeds the range of
7534 the case-expr. Later, all expressions will be promoted to the
7535 largest kind of all case-labels. */
7536
7537 if (type == BT_INTEGER)
7538 for (body = code->block; body; body = body->block)
7539 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7540 {
7541 if (cp->low
7542 && gfc_check_integer_range (cp->low->value.integer,
7543 case_expr->ts.kind) != ARITH_OK)
7544 gfc_warning ("Expression in CASE statement at %L is "
7545 "not in the range of %s", &cp->low->where,
7546 gfc_typename (&case_expr->ts));
7547
7548 if (cp->high
7549 && cp->low != cp->high
7550 && gfc_check_integer_range (cp->high->value.integer,
7551 case_expr->ts.kind) != ARITH_OK)
7552 gfc_warning ("Expression in CASE statement at %L is "
7553 "not in the range of %s", &cp->high->where,
7554 gfc_typename (&case_expr->ts));
7555 }
7556
7557 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7558 of the SELECT CASE expression and its CASE values. Walk the lists
7559 of case values, and if we find a mismatch, promote case_expr to
7560 the appropriate kind. */
7561
7562 if (type == BT_LOGICAL || type == BT_INTEGER)
7563 {
7564 for (body = code->block; body; body = body->block)
7565 {
7566 /* Walk the case label list. */
7567 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7568 {
7569 /* Intercept the DEFAULT case. It does not have a kind. */
7570 if (cp->low == NULL && cp->high == NULL)
7571 continue;
7572
7573 /* Unreachable case ranges are discarded, so ignore. */
7574 if (cp->low != NULL && cp->high != NULL
7575 && cp->low != cp->high
7576 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7577 continue;
7578
7579 if (cp->low != NULL
7580 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7581 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7582
7583 if (cp->high != NULL
7584 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7585 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7586 }
7587 }
7588 }
7589
7590 /* Assume there is no DEFAULT case. */
7591 default_case = NULL;
7592 head = tail = NULL;
7593 ncases = 0;
7594 seen_logical = 0;
7595
7596 for (body = code->block; body; body = body->block)
7597 {
7598 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7599 t = true;
7600 seen_unreachable = 0;
7601
7602 /* Walk the case label list, making sure that all case labels
7603 are legal. */
7604 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7605 {
7606 /* Count the number of cases in the whole construct. */
7607 ncases++;
7608
7609 /* Intercept the DEFAULT case. */
7610 if (cp->low == NULL && cp->high == NULL)
7611 {
7612 if (default_case != NULL)
7613 {
7614 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7615 "by a second DEFAULT CASE at %L",
7616 &default_case->where, &cp->where);
7617 t = false;
7618 break;
7619 }
7620 else
7621 {
7622 default_case = cp;
7623 continue;
7624 }
7625 }
7626
7627 /* Deal with single value cases and case ranges. Errors are
7628 issued from the validation function. */
7629 if (!validate_case_label_expr (cp->low, case_expr)
7630 || !validate_case_label_expr (cp->high, case_expr))
7631 {
7632 t = false;
7633 break;
7634 }
7635
7636 if (type == BT_LOGICAL
7637 && ((cp->low == NULL || cp->high == NULL)
7638 || cp->low != cp->high))
7639 {
7640 gfc_error ("Logical range in CASE statement at %L is not "
7641 "allowed", &cp->low->where);
7642 t = false;
7643 break;
7644 }
7645
7646 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7647 {
7648 int value;
7649 value = cp->low->value.logical == 0 ? 2 : 1;
7650 if (value & seen_logical)
7651 {
7652 gfc_error ("Constant logical value in CASE statement "
7653 "is repeated at %L",
7654 &cp->low->where);
7655 t = false;
7656 break;
7657 }
7658 seen_logical |= value;
7659 }
7660
7661 if (cp->low != NULL && cp->high != NULL
7662 && cp->low != cp->high
7663 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7664 {
7665 if (gfc_option.warn_surprising)
7666 gfc_warning ("Range specification at %L can never "
7667 "be matched", &cp->where);
7668
7669 cp->unreachable = 1;
7670 seen_unreachable = 1;
7671 }
7672 else
7673 {
7674 /* If the case range can be matched, it can also overlap with
7675 other cases. To make sure it does not, we put it in a
7676 double linked list here. We sort that with a merge sort
7677 later on to detect any overlapping cases. */
7678 if (!head)
7679 {
7680 head = tail = cp;
7681 head->right = head->left = NULL;
7682 }
7683 else
7684 {
7685 tail->right = cp;
7686 tail->right->left = tail;
7687 tail = tail->right;
7688 tail->right = NULL;
7689 }
7690 }
7691 }
7692
7693 /* It there was a failure in the previous case label, give up
7694 for this case label list. Continue with the next block. */
7695 if (!t)
7696 continue;
7697
7698 /* See if any case labels that are unreachable have been seen.
7699 If so, we eliminate them. This is a bit of a kludge because
7700 the case lists for a single case statement (label) is a
7701 single forward linked lists. */
7702 if (seen_unreachable)
7703 {
7704 /* Advance until the first case in the list is reachable. */
7705 while (body->ext.block.case_list != NULL
7706 && body->ext.block.case_list->unreachable)
7707 {
7708 gfc_case *n = body->ext.block.case_list;
7709 body->ext.block.case_list = body->ext.block.case_list->next;
7710 n->next = NULL;
7711 gfc_free_case_list (n);
7712 }
7713
7714 /* Strip all other unreachable cases. */
7715 if (body->ext.block.case_list)
7716 {
7717 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7718 {
7719 if (cp->next->unreachable)
7720 {
7721 gfc_case *n = cp->next;
7722 cp->next = cp->next->next;
7723 n->next = NULL;
7724 gfc_free_case_list (n);
7725 }
7726 }
7727 }
7728 }
7729 }
7730
7731 /* See if there were overlapping cases. If the check returns NULL,
7732 there was overlap. In that case we don't do anything. If head
7733 is non-NULL, we prepend the DEFAULT case. The sorted list can
7734 then used during code generation for SELECT CASE constructs with
7735 a case expression of a CHARACTER type. */
7736 if (head)
7737 {
7738 head = check_case_overlap (head);
7739
7740 /* Prepend the default_case if it is there. */
7741 if (head != NULL && default_case)
7742 {
7743 default_case->left = NULL;
7744 default_case->right = head;
7745 head->left = default_case;
7746 }
7747 }
7748
7749 /* Eliminate dead blocks that may be the result if we've seen
7750 unreachable case labels for a block. */
7751 for (body = code; body && body->block; body = body->block)
7752 {
7753 if (body->block->ext.block.case_list == NULL)
7754 {
7755 /* Cut the unreachable block from the code chain. */
7756 gfc_code *c = body->block;
7757 body->block = c->block;
7758
7759 /* Kill the dead block, but not the blocks below it. */
7760 c->block = NULL;
7761 gfc_free_statements (c);
7762 }
7763 }
7764
7765 /* More than two cases is legal but insane for logical selects.
7766 Issue a warning for it. */
7767 if (gfc_option.warn_surprising && type == BT_LOGICAL
7768 && ncases > 2)
7769 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7770 &code->loc);
7771 }
7772
7773
7774 /* Check if a derived type is extensible. */
7775
7776 bool
7777 gfc_type_is_extensible (gfc_symbol *sym)
7778 {
7779 return !(sym->attr.is_bind_c || sym->attr.sequence
7780 || (sym->attr.is_class
7781 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7782 }
7783
7784
7785 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7786 correct as well as possibly the array-spec. */
7787
7788 static void
7789 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7790 {
7791 gfc_expr* target;
7792
7793 gcc_assert (sym->assoc);
7794 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7795
7796 /* If this is for SELECT TYPE, the target may not yet be set. In that
7797 case, return. Resolution will be called later manually again when
7798 this is done. */
7799 target = sym->assoc->target;
7800 if (!target)
7801 return;
7802 gcc_assert (!sym->assoc->dangling);
7803
7804 if (resolve_target && !gfc_resolve_expr (target))
7805 return;
7806
7807 /* For variable targets, we get some attributes from the target. */
7808 if (target->expr_type == EXPR_VARIABLE)
7809 {
7810 gfc_symbol* tsym;
7811
7812 gcc_assert (target->symtree);
7813 tsym = target->symtree->n.sym;
7814
7815 sym->attr.asynchronous = tsym->attr.asynchronous;
7816 sym->attr.volatile_ = tsym->attr.volatile_;
7817
7818 sym->attr.target = tsym->attr.target
7819 || gfc_expr_attr (target).pointer;
7820 if (is_subref_array (target))
7821 sym->attr.subref_array_pointer = 1;
7822 }
7823
7824 /* Get type if this was not already set. Note that it can be
7825 some other type than the target in case this is a SELECT TYPE
7826 selector! So we must not update when the type is already there. */
7827 if (sym->ts.type == BT_UNKNOWN)
7828 sym->ts = target->ts;
7829 gcc_assert (sym->ts.type != BT_UNKNOWN);
7830
7831 /* See if this is a valid association-to-variable. */
7832 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7833 && !gfc_has_vector_subscript (target));
7834
7835 /* Finally resolve if this is an array or not. */
7836 if (sym->attr.dimension && target->rank == 0)
7837 {
7838 gfc_error ("Associate-name '%s' at %L is used as array",
7839 sym->name, &sym->declared_at);
7840 sym->attr.dimension = 0;
7841 return;
7842 }
7843
7844 /* We cannot deal with class selectors that need temporaries. */
7845 if (target->ts.type == BT_CLASS
7846 && gfc_ref_needs_temporary_p (target->ref))
7847 {
7848 gfc_error ("CLASS selector at %L needs a temporary which is not "
7849 "yet implemented", &target->where);
7850 return;
7851 }
7852
7853 if (target->ts.type != BT_CLASS && target->rank > 0)
7854 sym->attr.dimension = 1;
7855 else if (target->ts.type == BT_CLASS)
7856 gfc_fix_class_refs (target);
7857
7858 /* The associate-name will have a correct type by now. Make absolutely
7859 sure that it has not picked up a dimension attribute. */
7860 if (sym->ts.type == BT_CLASS)
7861 sym->attr.dimension = 0;
7862
7863 if (sym->attr.dimension)
7864 {
7865 sym->as = gfc_get_array_spec ();
7866 sym->as->rank = target->rank;
7867 sym->as->type = AS_DEFERRED;
7868
7869 /* Target must not be coindexed, thus the associate-variable
7870 has no corank. */
7871 sym->as->corank = 0;
7872 }
7873
7874 /* Mark this as an associate variable. */
7875 sym->attr.associate_var = 1;
7876
7877 /* If the target is a good class object, so is the associate variable. */
7878 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7879 sym->attr.class_ok = 1;
7880 }
7881
7882
7883 /* Resolve a SELECT TYPE statement. */
7884
7885 static void
7886 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7887 {
7888 gfc_symbol *selector_type;
7889 gfc_code *body, *new_st, *if_st, *tail;
7890 gfc_code *class_is = NULL, *default_case = NULL;
7891 gfc_case *c;
7892 gfc_symtree *st;
7893 char name[GFC_MAX_SYMBOL_LEN];
7894 gfc_namespace *ns;
7895 int error = 0;
7896 int charlen = 0;
7897
7898 ns = code->ext.block.ns;
7899 gfc_resolve (ns);
7900
7901 /* Check for F03:C813. */
7902 if (code->expr1->ts.type != BT_CLASS
7903 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7904 {
7905 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7906 "at %L", &code->loc);
7907 return;
7908 }
7909
7910 if (!code->expr1->symtree->n.sym->attr.class_ok)
7911 return;
7912
7913 if (code->expr2)
7914 {
7915 if (code->expr1->symtree->n.sym->attr.untyped)
7916 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7917 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7918
7919 /* F2008: C803 The selector expression must not be coindexed. */
7920 if (gfc_is_coindexed (code->expr2))
7921 {
7922 gfc_error ("Selector at %L must not be coindexed",
7923 &code->expr2->where);
7924 return;
7925 }
7926
7927 }
7928 else
7929 {
7930 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7931
7932 if (gfc_is_coindexed (code->expr1))
7933 {
7934 gfc_error ("Selector at %L must not be coindexed",
7935 &code->expr1->where);
7936 return;
7937 }
7938 }
7939
7940 /* Loop over TYPE IS / CLASS IS cases. */
7941 for (body = code->block; body; body = body->block)
7942 {
7943 c = body->ext.block.case_list;
7944
7945 /* Check F03:C815. */
7946 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7947 && !selector_type->attr.unlimited_polymorphic
7948 && !gfc_type_is_extensible (c->ts.u.derived))
7949 {
7950 gfc_error ("Derived type '%s' at %L must be extensible",
7951 c->ts.u.derived->name, &c->where);
7952 error++;
7953 continue;
7954 }
7955
7956 /* Check F03:C816. */
7957 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7958 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7959 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7960 {
7961 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7962 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7963 c->ts.u.derived->name, &c->where, selector_type->name);
7964 else
7965 gfc_error ("Unexpected intrinsic type '%s' at %L",
7966 gfc_basic_typename (c->ts.type), &c->where);
7967 error++;
7968 continue;
7969 }
7970
7971 /* Check F03:C814. */
7972 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7973 {
7974 gfc_error ("The type-spec at %L shall specify that each length "
7975 "type parameter is assumed", &c->where);
7976 error++;
7977 continue;
7978 }
7979
7980 /* Intercept the DEFAULT case. */
7981 if (c->ts.type == BT_UNKNOWN)
7982 {
7983 /* Check F03:C818. */
7984 if (default_case)
7985 {
7986 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7987 "by a second DEFAULT CASE at %L",
7988 &default_case->ext.block.case_list->where, &c->where);
7989 error++;
7990 continue;
7991 }
7992
7993 default_case = body;
7994 }
7995 }
7996
7997 if (error > 0)
7998 return;
7999
8000 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8001 target if present. If there are any EXIT statements referring to the
8002 SELECT TYPE construct, this is no problem because the gfc_code
8003 reference stays the same and EXIT is equally possible from the BLOCK
8004 it is changed to. */
8005 code->op = EXEC_BLOCK;
8006 if (code->expr2)
8007 {
8008 gfc_association_list* assoc;
8009
8010 assoc = gfc_get_association_list ();
8011 assoc->st = code->expr1->symtree;
8012 assoc->target = gfc_copy_expr (code->expr2);
8013 assoc->target->where = code->expr2->where;
8014 /* assoc->variable will be set by resolve_assoc_var. */
8015
8016 code->ext.block.assoc = assoc;
8017 code->expr1->symtree->n.sym->assoc = assoc;
8018
8019 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8020 }
8021 else
8022 code->ext.block.assoc = NULL;
8023
8024 /* Add EXEC_SELECT to switch on type. */
8025 new_st = gfc_get_code (code->op);
8026 new_st->expr1 = code->expr1;
8027 new_st->expr2 = code->expr2;
8028 new_st->block = code->block;
8029 code->expr1 = code->expr2 = NULL;
8030 code->block = NULL;
8031 if (!ns->code)
8032 ns->code = new_st;
8033 else
8034 ns->code->next = new_st;
8035 code = new_st;
8036 code->op = EXEC_SELECT;
8037
8038 gfc_add_vptr_component (code->expr1);
8039 gfc_add_hash_component (code->expr1);
8040
8041 /* Loop over TYPE IS / CLASS IS cases. */
8042 for (body = code->block; body; body = body->block)
8043 {
8044 c = body->ext.block.case_list;
8045
8046 if (c->ts.type == BT_DERIVED)
8047 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8048 c->ts.u.derived->hash_value);
8049 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8050 {
8051 gfc_symbol *ivtab;
8052 gfc_expr *e;
8053
8054 ivtab = gfc_find_vtab (&c->ts);
8055 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8056 e = CLASS_DATA (ivtab)->initializer;
8057 c->low = c->high = gfc_copy_expr (e);
8058 }
8059
8060 else if (c->ts.type == BT_UNKNOWN)
8061 continue;
8062
8063 /* Associate temporary to selector. This should only be done
8064 when this case is actually true, so build a new ASSOCIATE
8065 that does precisely this here (instead of using the
8066 'global' one). */
8067
8068 if (c->ts.type == BT_CLASS)
8069 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8070 else if (c->ts.type == BT_DERIVED)
8071 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8072 else if (c->ts.type == BT_CHARACTER)
8073 {
8074 if (c->ts.u.cl && c->ts.u.cl->length
8075 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8076 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8077 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8078 charlen, c->ts.kind);
8079 }
8080 else
8081 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8082 c->ts.kind);
8083
8084 st = gfc_find_symtree (ns->sym_root, name);
8085 gcc_assert (st->n.sym->assoc);
8086 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8087 st->n.sym->assoc->target->where = code->expr1->where;
8088 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8089 gfc_add_data_component (st->n.sym->assoc->target);
8090
8091 new_st = gfc_get_code (EXEC_BLOCK);
8092 new_st->ext.block.ns = gfc_build_block_ns (ns);
8093 new_st->ext.block.ns->code = body->next;
8094 body->next = new_st;
8095
8096 /* Chain in the new list only if it is marked as dangling. Otherwise
8097 there is a CASE label overlap and this is already used. Just ignore,
8098 the error is diagnosed elsewhere. */
8099 if (st->n.sym->assoc->dangling)
8100 {
8101 new_st->ext.block.assoc = st->n.sym->assoc;
8102 st->n.sym->assoc->dangling = 0;
8103 }
8104
8105 resolve_assoc_var (st->n.sym, false);
8106 }
8107
8108 /* Take out CLASS IS cases for separate treatment. */
8109 body = code;
8110 while (body && body->block)
8111 {
8112 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8113 {
8114 /* Add to class_is list. */
8115 if (class_is == NULL)
8116 {
8117 class_is = body->block;
8118 tail = class_is;
8119 }
8120 else
8121 {
8122 for (tail = class_is; tail->block; tail = tail->block) ;
8123 tail->block = body->block;
8124 tail = tail->block;
8125 }
8126 /* Remove from EXEC_SELECT list. */
8127 body->block = body->block->block;
8128 tail->block = NULL;
8129 }
8130 else
8131 body = body->block;
8132 }
8133
8134 if (class_is)
8135 {
8136 gfc_symbol *vtab;
8137
8138 if (!default_case)
8139 {
8140 /* Add a default case to hold the CLASS IS cases. */
8141 for (tail = code; tail->block; tail = tail->block) ;
8142 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8143 tail = tail->block;
8144 tail->ext.block.case_list = gfc_get_case ();
8145 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8146 tail->next = NULL;
8147 default_case = tail;
8148 }
8149
8150 /* More than one CLASS IS block? */
8151 if (class_is->block)
8152 {
8153 gfc_code **c1,*c2;
8154 bool swapped;
8155 /* Sort CLASS IS blocks by extension level. */
8156 do
8157 {
8158 swapped = false;
8159 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8160 {
8161 c2 = (*c1)->block;
8162 /* F03:C817 (check for doubles). */
8163 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8164 == c2->ext.block.case_list->ts.u.derived->hash_value)
8165 {
8166 gfc_error ("Double CLASS IS block in SELECT TYPE "
8167 "statement at %L",
8168 &c2->ext.block.case_list->where);
8169 return;
8170 }
8171 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8172 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8173 {
8174 /* Swap. */
8175 (*c1)->block = c2->block;
8176 c2->block = *c1;
8177 *c1 = c2;
8178 swapped = true;
8179 }
8180 }
8181 }
8182 while (swapped);
8183 }
8184
8185 /* Generate IF chain. */
8186 if_st = gfc_get_code (EXEC_IF);
8187 new_st = if_st;
8188 for (body = class_is; body; body = body->block)
8189 {
8190 new_st->block = gfc_get_code (EXEC_IF);
8191 new_st = new_st->block;
8192 /* Set up IF condition: Call _gfortran_is_extension_of. */
8193 new_st->expr1 = gfc_get_expr ();
8194 new_st->expr1->expr_type = EXPR_FUNCTION;
8195 new_st->expr1->ts.type = BT_LOGICAL;
8196 new_st->expr1->ts.kind = 4;
8197 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8198 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8199 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8200 /* Set up arguments. */
8201 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8202 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8203 new_st->expr1->value.function.actual->expr->where = code->loc;
8204 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8205 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8206 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8207 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8208 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8209 new_st->next = body->next;
8210 }
8211 if (default_case->next)
8212 {
8213 new_st->block = gfc_get_code (EXEC_IF);
8214 new_st = new_st->block;
8215 new_st->next = default_case->next;
8216 }
8217
8218 /* Replace CLASS DEFAULT code by the IF chain. */
8219 default_case->next = if_st;
8220 }
8221
8222 /* Resolve the internal code. This can not be done earlier because
8223 it requires that the sym->assoc of selectors is set already. */
8224 gfc_current_ns = ns;
8225 gfc_resolve_blocks (code->block, gfc_current_ns);
8226 gfc_current_ns = old_ns;
8227
8228 resolve_select (code, true);
8229 }
8230
8231
8232 /* Resolve a transfer statement. This is making sure that:
8233 -- a derived type being transferred has only non-pointer components
8234 -- a derived type being transferred doesn't have private components, unless
8235 it's being transferred from the module where the type was defined
8236 -- we're not trying to transfer a whole assumed size array. */
8237
8238 static void
8239 resolve_transfer (gfc_code *code)
8240 {
8241 gfc_typespec *ts;
8242 gfc_symbol *sym;
8243 gfc_ref *ref;
8244 gfc_expr *exp;
8245
8246 exp = code->expr1;
8247
8248 while (exp != NULL && exp->expr_type == EXPR_OP
8249 && exp->value.op.op == INTRINSIC_PARENTHESES)
8250 exp = exp->value.op.op1;
8251
8252 if (exp && exp->expr_type == EXPR_NULL
8253 && code->ext.dt)
8254 {
8255 gfc_error ("Invalid context for NULL () intrinsic at %L",
8256 &exp->where);
8257 return;
8258 }
8259
8260 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8261 && exp->expr_type != EXPR_FUNCTION))
8262 return;
8263
8264 /* If we are reading, the variable will be changed. Note that
8265 code->ext.dt may be NULL if the TRANSFER is related to
8266 an INQUIRE statement -- but in this case, we are not reading, either. */
8267 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8268 && !gfc_check_vardef_context (exp, false, false, false,
8269 _("item in READ")))
8270 return;
8271
8272 sym = exp->symtree->n.sym;
8273 ts = &sym->ts;
8274
8275 /* Go to actual component transferred. */
8276 for (ref = exp->ref; ref; ref = ref->next)
8277 if (ref->type == REF_COMPONENT)
8278 ts = &ref->u.c.component->ts;
8279
8280 if (ts->type == BT_CLASS)
8281 {
8282 /* FIXME: Test for defined input/output. */
8283 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8284 "it is processed by a defined input/output procedure",
8285 &code->loc);
8286 return;
8287 }
8288
8289 if (ts->type == BT_DERIVED)
8290 {
8291 /* Check that transferred derived type doesn't contain POINTER
8292 components. */
8293 if (ts->u.derived->attr.pointer_comp)
8294 {
8295 gfc_error ("Data transfer element at %L cannot have POINTER "
8296 "components unless it is processed by a defined "
8297 "input/output procedure", &code->loc);
8298 return;
8299 }
8300
8301 /* F08:C935. */
8302 if (ts->u.derived->attr.proc_pointer_comp)
8303 {
8304 gfc_error ("Data transfer element at %L cannot have "
8305 "procedure pointer components", &code->loc);
8306 return;
8307 }
8308
8309 if (ts->u.derived->attr.alloc_comp)
8310 {
8311 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8312 "components unless it is processed by a defined "
8313 "input/output procedure", &code->loc);
8314 return;
8315 }
8316
8317 /* C_PTR and C_FUNPTR have private components which means they can not
8318 be printed. However, if -std=gnu and not -pedantic, allow
8319 the component to be printed to help debugging. */
8320 if (ts->u.derived->ts.f90_type == BT_VOID)
8321 {
8322 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8323 "cannot have PRIVATE components", &code->loc))
8324 return;
8325 }
8326 else if (derived_inaccessible (ts->u.derived))
8327 {
8328 gfc_error ("Data transfer element at %L cannot have "
8329 "PRIVATE components",&code->loc);
8330 return;
8331 }
8332 }
8333
8334 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8335 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8336 {
8337 gfc_error ("Data transfer element at %L cannot be a full reference to "
8338 "an assumed-size array", &code->loc);
8339 return;
8340 }
8341 }
8342
8343
8344 /*********** Toplevel code resolution subroutines ***********/
8345
8346 /* Find the set of labels that are reachable from this block. We also
8347 record the last statement in each block. */
8348
8349 static void
8350 find_reachable_labels (gfc_code *block)
8351 {
8352 gfc_code *c;
8353
8354 if (!block)
8355 return;
8356
8357 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8358
8359 /* Collect labels in this block. We don't keep those corresponding
8360 to END {IF|SELECT}, these are checked in resolve_branch by going
8361 up through the code_stack. */
8362 for (c = block; c; c = c->next)
8363 {
8364 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8365 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8366 }
8367
8368 /* Merge with labels from parent block. */
8369 if (cs_base->prev)
8370 {
8371 gcc_assert (cs_base->prev->reachable_labels);
8372 bitmap_ior_into (cs_base->reachable_labels,
8373 cs_base->prev->reachable_labels);
8374 }
8375 }
8376
8377
8378 static void
8379 resolve_lock_unlock (gfc_code *code)
8380 {
8381 if (code->expr1->ts.type != BT_DERIVED
8382 || code->expr1->expr_type != EXPR_VARIABLE
8383 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8384 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8385 || code->expr1->rank != 0
8386 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8387 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8388 &code->expr1->where);
8389
8390 /* Check STAT. */
8391 if (code->expr2
8392 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8393 || code->expr2->expr_type != EXPR_VARIABLE))
8394 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8395 &code->expr2->where);
8396
8397 if (code->expr2
8398 && !gfc_check_vardef_context (code->expr2, false, false, false,
8399 _("STAT variable")))
8400 return;
8401
8402 /* Check ERRMSG. */
8403 if (code->expr3
8404 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8405 || code->expr3->expr_type != EXPR_VARIABLE))
8406 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8407 &code->expr3->where);
8408
8409 if (code->expr3
8410 && !gfc_check_vardef_context (code->expr3, false, false, false,
8411 _("ERRMSG variable")))
8412 return;
8413
8414 /* Check ACQUIRED_LOCK. */
8415 if (code->expr4
8416 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8417 || code->expr4->expr_type != EXPR_VARIABLE))
8418 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8419 "variable", &code->expr4->where);
8420
8421 if (code->expr4
8422 && !gfc_check_vardef_context (code->expr4, false, false, false,
8423 _("ACQUIRED_LOCK variable")))
8424 return;
8425 }
8426
8427
8428 static void
8429 resolve_sync (gfc_code *code)
8430 {
8431 /* Check imageset. The * case matches expr1 == NULL. */
8432 if (code->expr1)
8433 {
8434 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8435 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8436 "INTEGER expression", &code->expr1->where);
8437 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8438 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8439 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8440 &code->expr1->where);
8441 else if (code->expr1->expr_type == EXPR_ARRAY
8442 && gfc_simplify_expr (code->expr1, 0))
8443 {
8444 gfc_constructor *cons;
8445 cons = gfc_constructor_first (code->expr1->value.constructor);
8446 for (; cons; cons = gfc_constructor_next (cons))
8447 if (cons->expr->expr_type == EXPR_CONSTANT
8448 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8449 gfc_error ("Imageset argument at %L must between 1 and "
8450 "num_images()", &cons->expr->where);
8451 }
8452 }
8453
8454 /* Check STAT. */
8455 if (code->expr2
8456 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8457 || code->expr2->expr_type != EXPR_VARIABLE))
8458 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8459 &code->expr2->where);
8460
8461 /* Check ERRMSG. */
8462 if (code->expr3
8463 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8464 || code->expr3->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8466 &code->expr3->where);
8467 }
8468
8469
8470 /* Given a branch to a label, see if the branch is conforming.
8471 The code node describes where the branch is located. */
8472
8473 static void
8474 resolve_branch (gfc_st_label *label, gfc_code *code)
8475 {
8476 code_stack *stack;
8477
8478 if (label == NULL)
8479 return;
8480
8481 /* Step one: is this a valid branching target? */
8482
8483 if (label->defined == ST_LABEL_UNKNOWN)
8484 {
8485 gfc_error ("Label %d referenced at %L is never defined", label->value,
8486 &label->where);
8487 return;
8488 }
8489
8490 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8491 {
8492 gfc_error ("Statement at %L is not a valid branch target statement "
8493 "for the branch statement at %L", &label->where, &code->loc);
8494 return;
8495 }
8496
8497 /* Step two: make sure this branch is not a branch to itself ;-) */
8498
8499 if (code->here == label)
8500 {
8501 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8502 return;
8503 }
8504
8505 /* Step three: See if the label is in the same block as the
8506 branching statement. The hard work has been done by setting up
8507 the bitmap reachable_labels. */
8508
8509 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8510 {
8511 /* Check now whether there is a CRITICAL construct; if so, check
8512 whether the label is still visible outside of the CRITICAL block,
8513 which is invalid. */
8514 for (stack = cs_base; stack; stack = stack->prev)
8515 {
8516 if (stack->current->op == EXEC_CRITICAL
8517 && bitmap_bit_p (stack->reachable_labels, label->value))
8518 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8519 "label at %L", &code->loc, &label->where);
8520 else if (stack->current->op == EXEC_DO_CONCURRENT
8521 && bitmap_bit_p (stack->reachable_labels, label->value))
8522 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8523 "for label at %L", &code->loc, &label->where);
8524 }
8525
8526 return;
8527 }
8528
8529 /* Step four: If we haven't found the label in the bitmap, it may
8530 still be the label of the END of the enclosing block, in which
8531 case we find it by going up the code_stack. */
8532
8533 for (stack = cs_base; stack; stack = stack->prev)
8534 {
8535 if (stack->current->next && stack->current->next->here == label)
8536 break;
8537 if (stack->current->op == EXEC_CRITICAL)
8538 {
8539 /* Note: A label at END CRITICAL does not leave the CRITICAL
8540 construct as END CRITICAL is still part of it. */
8541 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8542 " at %L", &code->loc, &label->where);
8543 return;
8544 }
8545 else if (stack->current->op == EXEC_DO_CONCURRENT)
8546 {
8547 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8548 "label at %L", &code->loc, &label->where);
8549 return;
8550 }
8551 }
8552
8553 if (stack)
8554 {
8555 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8556 return;
8557 }
8558
8559 /* The label is not in an enclosing block, so illegal. This was
8560 allowed in Fortran 66, so we allow it as extension. No
8561 further checks are necessary in this case. */
8562 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8563 "as the GOTO statement at %L", &label->where,
8564 &code->loc);
8565 return;
8566 }
8567
8568
8569 /* Check whether EXPR1 has the same shape as EXPR2. */
8570
8571 static bool
8572 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8573 {
8574 mpz_t shape[GFC_MAX_DIMENSIONS];
8575 mpz_t shape2[GFC_MAX_DIMENSIONS];
8576 bool result = false;
8577 int i;
8578
8579 /* Compare the rank. */
8580 if (expr1->rank != expr2->rank)
8581 return result;
8582
8583 /* Compare the size of each dimension. */
8584 for (i=0; i<expr1->rank; i++)
8585 {
8586 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8587 goto ignore;
8588
8589 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8590 goto ignore;
8591
8592 if (mpz_cmp (shape[i], shape2[i]))
8593 goto over;
8594 }
8595
8596 /* When either of the two expression is an assumed size array, we
8597 ignore the comparison of dimension sizes. */
8598 ignore:
8599 result = true;
8600
8601 over:
8602 gfc_clear_shape (shape, i);
8603 gfc_clear_shape (shape2, i);
8604 return result;
8605 }
8606
8607
8608 /* Check whether a WHERE assignment target or a WHERE mask expression
8609 has the same shape as the outmost WHERE mask expression. */
8610
8611 static void
8612 resolve_where (gfc_code *code, gfc_expr *mask)
8613 {
8614 gfc_code *cblock;
8615 gfc_code *cnext;
8616 gfc_expr *e = NULL;
8617
8618 cblock = code->block;
8619
8620 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8621 In case of nested WHERE, only the outmost one is stored. */
8622 if (mask == NULL) /* outmost WHERE */
8623 e = cblock->expr1;
8624 else /* inner WHERE */
8625 e = mask;
8626
8627 while (cblock)
8628 {
8629 if (cblock->expr1)
8630 {
8631 /* Check if the mask-expr has a consistent shape with the
8632 outmost WHERE mask-expr. */
8633 if (!resolve_where_shape (cblock->expr1, e))
8634 gfc_error ("WHERE mask at %L has inconsistent shape",
8635 &cblock->expr1->where);
8636 }
8637
8638 /* the assignment statement of a WHERE statement, or the first
8639 statement in where-body-construct of a WHERE construct */
8640 cnext = cblock->next;
8641 while (cnext)
8642 {
8643 switch (cnext->op)
8644 {
8645 /* WHERE assignment statement */
8646 case EXEC_ASSIGN:
8647
8648 /* Check shape consistent for WHERE assignment target. */
8649 if (e && !resolve_where_shape (cnext->expr1, e))
8650 gfc_error ("WHERE assignment target at %L has "
8651 "inconsistent shape", &cnext->expr1->where);
8652 break;
8653
8654
8655 case EXEC_ASSIGN_CALL:
8656 resolve_call (cnext);
8657 if (!cnext->resolved_sym->attr.elemental)
8658 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8659 &cnext->ext.actual->expr->where);
8660 break;
8661
8662 /* WHERE or WHERE construct is part of a where-body-construct */
8663 case EXEC_WHERE:
8664 resolve_where (cnext, e);
8665 break;
8666
8667 default:
8668 gfc_error ("Unsupported statement inside WHERE at %L",
8669 &cnext->loc);
8670 }
8671 /* the next statement within the same where-body-construct */
8672 cnext = cnext->next;
8673 }
8674 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8675 cblock = cblock->block;
8676 }
8677 }
8678
8679
8680 /* Resolve assignment in FORALL construct.
8681 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8682 FORALL index variables. */
8683
8684 static void
8685 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8686 {
8687 int n;
8688
8689 for (n = 0; n < nvar; n++)
8690 {
8691 gfc_symbol *forall_index;
8692
8693 forall_index = var_expr[n]->symtree->n.sym;
8694
8695 /* Check whether the assignment target is one of the FORALL index
8696 variable. */
8697 if ((code->expr1->expr_type == EXPR_VARIABLE)
8698 && (code->expr1->symtree->n.sym == forall_index))
8699 gfc_error ("Assignment to a FORALL index variable at %L",
8700 &code->expr1->where);
8701 else
8702 {
8703 /* If one of the FORALL index variables doesn't appear in the
8704 assignment variable, then there could be a many-to-one
8705 assignment. Emit a warning rather than an error because the
8706 mask could be resolving this problem. */
8707 if (!find_forall_index (code->expr1, forall_index, 0))
8708 gfc_warning ("The FORALL with index '%s' is not used on the "
8709 "left side of the assignment at %L and so might "
8710 "cause multiple assignment to this object",
8711 var_expr[n]->symtree->name, &code->expr1->where);
8712 }
8713 }
8714 }
8715
8716
8717 /* Resolve WHERE statement in FORALL construct. */
8718
8719 static void
8720 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8721 gfc_expr **var_expr)
8722 {
8723 gfc_code *cblock;
8724 gfc_code *cnext;
8725
8726 cblock = code->block;
8727 while (cblock)
8728 {
8729 /* the assignment statement of a WHERE statement, or the first
8730 statement in where-body-construct of a WHERE construct */
8731 cnext = cblock->next;
8732 while (cnext)
8733 {
8734 switch (cnext->op)
8735 {
8736 /* WHERE assignment statement */
8737 case EXEC_ASSIGN:
8738 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8739 break;
8740
8741 /* WHERE operator assignment statement */
8742 case EXEC_ASSIGN_CALL:
8743 resolve_call (cnext);
8744 if (!cnext->resolved_sym->attr.elemental)
8745 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8746 &cnext->ext.actual->expr->where);
8747 break;
8748
8749 /* WHERE or WHERE construct is part of a where-body-construct */
8750 case EXEC_WHERE:
8751 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8752 break;
8753
8754 default:
8755 gfc_error ("Unsupported statement inside WHERE at %L",
8756 &cnext->loc);
8757 }
8758 /* the next statement within the same where-body-construct */
8759 cnext = cnext->next;
8760 }
8761 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8762 cblock = cblock->block;
8763 }
8764 }
8765
8766
8767 /* Traverse the FORALL body to check whether the following errors exist:
8768 1. For assignment, check if a many-to-one assignment happens.
8769 2. For WHERE statement, check the WHERE body to see if there is any
8770 many-to-one assignment. */
8771
8772 static void
8773 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8774 {
8775 gfc_code *c;
8776
8777 c = code->block->next;
8778 while (c)
8779 {
8780 switch (c->op)
8781 {
8782 case EXEC_ASSIGN:
8783 case EXEC_POINTER_ASSIGN:
8784 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8785 break;
8786
8787 case EXEC_ASSIGN_CALL:
8788 resolve_call (c);
8789 break;
8790
8791 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8792 there is no need to handle it here. */
8793 case EXEC_FORALL:
8794 break;
8795 case EXEC_WHERE:
8796 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8797 break;
8798 default:
8799 break;
8800 }
8801 /* The next statement in the FORALL body. */
8802 c = c->next;
8803 }
8804 }
8805
8806
8807 /* Counts the number of iterators needed inside a forall construct, including
8808 nested forall constructs. This is used to allocate the needed memory
8809 in gfc_resolve_forall. */
8810
8811 static int
8812 gfc_count_forall_iterators (gfc_code *code)
8813 {
8814 int max_iters, sub_iters, current_iters;
8815 gfc_forall_iterator *fa;
8816
8817 gcc_assert(code->op == EXEC_FORALL);
8818 max_iters = 0;
8819 current_iters = 0;
8820
8821 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8822 current_iters ++;
8823
8824 code = code->block->next;
8825
8826 while (code)
8827 {
8828 if (code->op == EXEC_FORALL)
8829 {
8830 sub_iters = gfc_count_forall_iterators (code);
8831 if (sub_iters > max_iters)
8832 max_iters = sub_iters;
8833 }
8834 code = code->next;
8835 }
8836
8837 return current_iters + max_iters;
8838 }
8839
8840
8841 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8842 gfc_resolve_forall_body to resolve the FORALL body. */
8843
8844 static void
8845 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8846 {
8847 static gfc_expr **var_expr;
8848 static int total_var = 0;
8849 static int nvar = 0;
8850 int old_nvar, tmp;
8851 gfc_forall_iterator *fa;
8852 int i;
8853
8854 old_nvar = nvar;
8855
8856 /* Start to resolve a FORALL construct */
8857 if (forall_save == 0)
8858 {
8859 /* Count the total number of FORALL index in the nested FORALL
8860 construct in order to allocate the VAR_EXPR with proper size. */
8861 total_var = gfc_count_forall_iterators (code);
8862
8863 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8864 var_expr = XCNEWVEC (gfc_expr *, total_var);
8865 }
8866
8867 /* The information about FORALL iterator, including FORALL index start, end
8868 and stride. The FORALL index can not appear in start, end or stride. */
8869 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8870 {
8871 /* Check if any outer FORALL index name is the same as the current
8872 one. */
8873 for (i = 0; i < nvar; i++)
8874 {
8875 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8876 {
8877 gfc_error ("An outer FORALL construct already has an index "
8878 "with this name %L", &fa->var->where);
8879 }
8880 }
8881
8882 /* Record the current FORALL index. */
8883 var_expr[nvar] = gfc_copy_expr (fa->var);
8884
8885 nvar++;
8886
8887 /* No memory leak. */
8888 gcc_assert (nvar <= total_var);
8889 }
8890
8891 /* Resolve the FORALL body. */
8892 gfc_resolve_forall_body (code, nvar, var_expr);
8893
8894 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8895 gfc_resolve_blocks (code->block, ns);
8896
8897 tmp = nvar;
8898 nvar = old_nvar;
8899 /* Free only the VAR_EXPRs allocated in this frame. */
8900 for (i = nvar; i < tmp; i++)
8901 gfc_free_expr (var_expr[i]);
8902
8903 if (nvar == 0)
8904 {
8905 /* We are in the outermost FORALL construct. */
8906 gcc_assert (forall_save == 0);
8907
8908 /* VAR_EXPR is not needed any more. */
8909 free (var_expr);
8910 total_var = 0;
8911 }
8912 }
8913
8914
8915 /* Resolve a BLOCK construct statement. */
8916
8917 static void
8918 resolve_block_construct (gfc_code* code)
8919 {
8920 /* Resolve the BLOCK's namespace. */
8921 gfc_resolve (code->ext.block.ns);
8922
8923 /* For an ASSOCIATE block, the associations (and their targets) are already
8924 resolved during resolve_symbol. */
8925 }
8926
8927
8928 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8929 DO code nodes. */
8930
8931 static void resolve_code (gfc_code *, gfc_namespace *);
8932
8933 void
8934 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8935 {
8936 bool t;
8937
8938 for (; b; b = b->block)
8939 {
8940 t = gfc_resolve_expr (b->expr1);
8941 if (!gfc_resolve_expr (b->expr2))
8942 t = false;
8943
8944 switch (b->op)
8945 {
8946 case EXEC_IF:
8947 if (t && b->expr1 != NULL
8948 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8949 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8950 &b->expr1->where);
8951 break;
8952
8953 case EXEC_WHERE:
8954 if (t
8955 && b->expr1 != NULL
8956 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8957 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8958 &b->expr1->where);
8959 break;
8960
8961 case EXEC_GOTO:
8962 resolve_branch (b->label1, b);
8963 break;
8964
8965 case EXEC_BLOCK:
8966 resolve_block_construct (b);
8967 break;
8968
8969 case EXEC_SELECT:
8970 case EXEC_SELECT_TYPE:
8971 case EXEC_FORALL:
8972 case EXEC_DO:
8973 case EXEC_DO_WHILE:
8974 case EXEC_DO_CONCURRENT:
8975 case EXEC_CRITICAL:
8976 case EXEC_READ:
8977 case EXEC_WRITE:
8978 case EXEC_IOLENGTH:
8979 case EXEC_WAIT:
8980 break;
8981
8982 case EXEC_OMP_ATOMIC:
8983 case EXEC_OMP_CRITICAL:
8984 case EXEC_OMP_DO:
8985 case EXEC_OMP_MASTER:
8986 case EXEC_OMP_ORDERED:
8987 case EXEC_OMP_PARALLEL:
8988 case EXEC_OMP_PARALLEL_DO:
8989 case EXEC_OMP_PARALLEL_SECTIONS:
8990 case EXEC_OMP_PARALLEL_WORKSHARE:
8991 case EXEC_OMP_SECTIONS:
8992 case EXEC_OMP_SINGLE:
8993 case EXEC_OMP_TASK:
8994 case EXEC_OMP_TASKWAIT:
8995 case EXEC_OMP_TASKYIELD:
8996 case EXEC_OMP_WORKSHARE:
8997 break;
8998
8999 default:
9000 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9001 }
9002
9003 resolve_code (b->next, ns);
9004 }
9005 }
9006
9007
9008 /* Does everything to resolve an ordinary assignment. Returns true
9009 if this is an interface assignment. */
9010 static bool
9011 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9012 {
9013 bool rval = false;
9014 gfc_expr *lhs;
9015 gfc_expr *rhs;
9016 int llen = 0;
9017 int rlen = 0;
9018 int n;
9019 gfc_ref *ref;
9020 symbol_attribute attr;
9021
9022 if (gfc_extend_assign (code, ns))
9023 {
9024 gfc_expr** rhsptr;
9025
9026 if (code->op == EXEC_ASSIGN_CALL)
9027 {
9028 lhs = code->ext.actual->expr;
9029 rhsptr = &code->ext.actual->next->expr;
9030 }
9031 else
9032 {
9033 gfc_actual_arglist* args;
9034 gfc_typebound_proc* tbp;
9035
9036 gcc_assert (code->op == EXEC_COMPCALL);
9037
9038 args = code->expr1->value.compcall.actual;
9039 lhs = args->expr;
9040 rhsptr = &args->next->expr;
9041
9042 tbp = code->expr1->value.compcall.tbp;
9043 gcc_assert (!tbp->is_generic);
9044 }
9045
9046 /* Make a temporary rhs when there is a default initializer
9047 and rhs is the same symbol as the lhs. */
9048 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9049 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9050 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9051 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9052 *rhsptr = gfc_get_parentheses (*rhsptr);
9053
9054 return true;
9055 }
9056
9057 lhs = code->expr1;
9058 rhs = code->expr2;
9059
9060 if (rhs->is_boz
9061 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9062 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9063 &code->loc))
9064 return false;
9065
9066 /* Handle the case of a BOZ literal on the RHS. */
9067 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9068 {
9069 int rc;
9070 if (gfc_option.warn_surprising)
9071 gfc_warning ("BOZ literal at %L is bitwise transferred "
9072 "non-integer symbol '%s'", &code->loc,
9073 lhs->symtree->n.sym->name);
9074
9075 if (!gfc_convert_boz (rhs, &lhs->ts))
9076 return false;
9077 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9078 {
9079 if (rc == ARITH_UNDERFLOW)
9080 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9081 ". This check can be disabled with the option "
9082 "-fno-range-check", &rhs->where);
9083 else if (rc == ARITH_OVERFLOW)
9084 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9085 ". This check can be disabled with the option "
9086 "-fno-range-check", &rhs->where);
9087 else if (rc == ARITH_NAN)
9088 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9089 ". This check can be disabled with the option "
9090 "-fno-range-check", &rhs->where);
9091 return false;
9092 }
9093 }
9094
9095 if (lhs->ts.type == BT_CHARACTER
9096 && gfc_option.warn_character_truncation)
9097 {
9098 if (lhs->ts.u.cl != NULL
9099 && lhs->ts.u.cl->length != NULL
9100 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9101 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9102
9103 if (rhs->expr_type == EXPR_CONSTANT)
9104 rlen = rhs->value.character.length;
9105
9106 else if (rhs->ts.u.cl != NULL
9107 && rhs->ts.u.cl->length != NULL
9108 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9109 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9110
9111 if (rlen && llen && rlen > llen)
9112 gfc_warning_now ("CHARACTER expression will be truncated "
9113 "in assignment (%d/%d) at %L",
9114 llen, rlen, &code->loc);
9115 }
9116
9117 /* Ensure that a vector index expression for the lvalue is evaluated
9118 to a temporary if the lvalue symbol is referenced in it. */
9119 if (lhs->rank)
9120 {
9121 for (ref = lhs->ref; ref; ref= ref->next)
9122 if (ref->type == REF_ARRAY)
9123 {
9124 for (n = 0; n < ref->u.ar.dimen; n++)
9125 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9126 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9127 ref->u.ar.start[n]))
9128 ref->u.ar.start[n]
9129 = gfc_get_parentheses (ref->u.ar.start[n]);
9130 }
9131 }
9132
9133 if (gfc_pure (NULL))
9134 {
9135 if (lhs->ts.type == BT_DERIVED
9136 && lhs->expr_type == EXPR_VARIABLE
9137 && lhs->ts.u.derived->attr.pointer_comp
9138 && rhs->expr_type == EXPR_VARIABLE
9139 && (gfc_impure_variable (rhs->symtree->n.sym)
9140 || gfc_is_coindexed (rhs)))
9141 {
9142 /* F2008, C1283. */
9143 if (gfc_is_coindexed (rhs))
9144 gfc_error ("Coindexed expression at %L is assigned to "
9145 "a derived type variable with a POINTER "
9146 "component in a PURE procedure",
9147 &rhs->where);
9148 else
9149 gfc_error ("The impure variable at %L is assigned to "
9150 "a derived type variable with a POINTER "
9151 "component in a PURE procedure (12.6)",
9152 &rhs->where);
9153 return rval;
9154 }
9155
9156 /* Fortran 2008, C1283. */
9157 if (gfc_is_coindexed (lhs))
9158 {
9159 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9160 "procedure", &rhs->where);
9161 return rval;
9162 }
9163 }
9164
9165 if (gfc_implicit_pure (NULL))
9166 {
9167 if (lhs->expr_type == EXPR_VARIABLE
9168 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9169 && lhs->symtree->n.sym->ns != gfc_current_ns)
9170 gfc_unset_implicit_pure (NULL);
9171
9172 if (lhs->ts.type == BT_DERIVED
9173 && lhs->expr_type == EXPR_VARIABLE
9174 && lhs->ts.u.derived->attr.pointer_comp
9175 && rhs->expr_type == EXPR_VARIABLE
9176 && (gfc_impure_variable (rhs->symtree->n.sym)
9177 || gfc_is_coindexed (rhs)))
9178 gfc_unset_implicit_pure (NULL);
9179
9180 /* Fortran 2008, C1283. */
9181 if (gfc_is_coindexed (lhs))
9182 gfc_unset_implicit_pure (NULL);
9183 }
9184
9185 /* F2008, 7.2.1.2. */
9186 attr = gfc_expr_attr (lhs);
9187 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9188 {
9189 if (attr.codimension)
9190 {
9191 gfc_error ("Assignment to polymorphic coarray at %L is not "
9192 "permitted", &lhs->where);
9193 return false;
9194 }
9195 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9196 "polymorphic variable at %L", &lhs->where))
9197 return false;
9198 if (!gfc_option.flag_realloc_lhs)
9199 {
9200 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9201 "requires -frealloc-lhs", &lhs->where);
9202 return false;
9203 }
9204 /* See PR 43366. */
9205 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9206 "is not yet supported", &lhs->where);
9207 return false;
9208 }
9209 else if (lhs->ts.type == BT_CLASS)
9210 {
9211 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9212 "assignment at %L - check that there is a matching specific "
9213 "subroutine for '=' operator", &lhs->where);
9214 return false;
9215 }
9216
9217 /* F2008, Section 7.2.1.2. */
9218 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9219 {
9220 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9221 "component in assignment at %L", &lhs->where);
9222 return false;
9223 }
9224
9225 gfc_check_assign (lhs, rhs, 1);
9226 return false;
9227 }
9228
9229
9230 /* Add a component reference onto an expression. */
9231
9232 static void
9233 add_comp_ref (gfc_expr *e, gfc_component *c)
9234 {
9235 gfc_ref **ref;
9236 ref = &(e->ref);
9237 while (*ref)
9238 ref = &((*ref)->next);
9239 *ref = gfc_get_ref ();
9240 (*ref)->type = REF_COMPONENT;
9241 (*ref)->u.c.sym = e->ts.u.derived;
9242 (*ref)->u.c.component = c;
9243 e->ts = c->ts;
9244
9245 /* Add a full array ref, as necessary. */
9246 if (c->as)
9247 {
9248 gfc_add_full_array_ref (e, c->as);
9249 e->rank = c->as->rank;
9250 }
9251 }
9252
9253
9254 /* Build an assignment. Keep the argument 'op' for future use, so that
9255 pointer assignments can be made. */
9256
9257 static gfc_code *
9258 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9259 gfc_component *comp1, gfc_component *comp2, locus loc)
9260 {
9261 gfc_code *this_code;
9262
9263 this_code = gfc_get_code (op);
9264 this_code->next = NULL;
9265 this_code->expr1 = gfc_copy_expr (expr1);
9266 this_code->expr2 = gfc_copy_expr (expr2);
9267 this_code->loc = loc;
9268 if (comp1 && comp2)
9269 {
9270 add_comp_ref (this_code->expr1, comp1);
9271 add_comp_ref (this_code->expr2, comp2);
9272 }
9273
9274 return this_code;
9275 }
9276
9277
9278 /* Makes a temporary variable expression based on the characteristics of
9279 a given variable expression. */
9280
9281 static gfc_expr*
9282 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9283 {
9284 static int serial = 0;
9285 char name[GFC_MAX_SYMBOL_LEN];
9286 gfc_symtree *tmp;
9287 gfc_array_spec *as;
9288 gfc_array_ref *aref;
9289 gfc_ref *ref;
9290
9291 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9292 gfc_get_sym_tree (name, ns, &tmp, false);
9293 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9294
9295 as = NULL;
9296 ref = NULL;
9297 aref = NULL;
9298
9299 /* This function could be expanded to support other expression type
9300 but this is not needed here. */
9301 gcc_assert (e->expr_type == EXPR_VARIABLE);
9302
9303 /* Obtain the arrayspec for the temporary. */
9304 if (e->rank)
9305 {
9306 aref = gfc_find_array_ref (e);
9307 if (e->expr_type == EXPR_VARIABLE
9308 && e->symtree->n.sym->as == aref->as)
9309 as = aref->as;
9310 else
9311 {
9312 for (ref = e->ref; ref; ref = ref->next)
9313 if (ref->type == REF_COMPONENT
9314 && ref->u.c.component->as == aref->as)
9315 {
9316 as = aref->as;
9317 break;
9318 }
9319 }
9320 }
9321
9322 /* Add the attributes and the arrayspec to the temporary. */
9323 tmp->n.sym->attr = gfc_expr_attr (e);
9324 tmp->n.sym->attr.function = 0;
9325 tmp->n.sym->attr.result = 0;
9326 tmp->n.sym->attr.flavor = FL_VARIABLE;
9327
9328 if (as)
9329 {
9330 tmp->n.sym->as = gfc_copy_array_spec (as);
9331 if (!ref)
9332 ref = e->ref;
9333 if (as->type == AS_DEFERRED)
9334 tmp->n.sym->attr.allocatable = 1;
9335 }
9336 else
9337 tmp->n.sym->attr.dimension = 0;
9338
9339 gfc_set_sym_referenced (tmp->n.sym);
9340 gfc_commit_symbol (tmp->n.sym);
9341 e = gfc_lval_expr_from_sym (tmp->n.sym);
9342
9343 /* Should the lhs be a section, use its array ref for the
9344 temporary expression. */
9345 if (aref && aref->type != AR_FULL)
9346 {
9347 gfc_free_ref_list (e->ref);
9348 e->ref = gfc_copy_ref (ref);
9349 }
9350 return e;
9351 }
9352
9353
9354 /* Add one line of code to the code chain, making sure that 'head' and
9355 'tail' are appropriately updated. */
9356
9357 static void
9358 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9359 {
9360 gcc_assert (this_code);
9361 if (*head == NULL)
9362 *head = *tail = *this_code;
9363 else
9364 *tail = gfc_append_code (*tail, *this_code);
9365 *this_code = NULL;
9366 }
9367
9368
9369 /* Counts the potential number of part array references that would
9370 result from resolution of typebound defined assignments. */
9371
9372 static int
9373 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9374 {
9375 gfc_component *c;
9376 int c_depth = 0, t_depth;
9377
9378 for (c= derived->components; c; c = c->next)
9379 {
9380 if ((c->ts.type != BT_DERIVED
9381 || c->attr.pointer
9382 || c->attr.allocatable
9383 || c->attr.proc_pointer_comp
9384 || c->attr.class_pointer
9385 || c->attr.proc_pointer)
9386 && !c->attr.defined_assign_comp)
9387 continue;
9388
9389 if (c->as && c_depth == 0)
9390 c_depth = 1;
9391
9392 if (c->ts.u.derived->attr.defined_assign_comp)
9393 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9394 c->as ? 1 : 0);
9395 else
9396 t_depth = 0;
9397
9398 c_depth = t_depth > c_depth ? t_depth : c_depth;
9399 }
9400 return depth + c_depth;
9401 }
9402
9403
9404 /* Implement 7.2.1.3 of the F08 standard:
9405 "An intrinsic assignment where the variable is of derived type is
9406 performed as if each component of the variable were assigned from the
9407 corresponding component of expr using pointer assignment (7.2.2) for
9408 each pointer component, defined assignment for each nonpointer
9409 nonallocatable component of a type that has a type-bound defined
9410 assignment consistent with the component, intrinsic assignment for
9411 each other nonpointer nonallocatable component, ..."
9412
9413 The pointer assignments are taken care of by the intrinsic
9414 assignment of the structure itself. This function recursively adds
9415 defined assignments where required. The recursion is accomplished
9416 by calling resolve_code.
9417
9418 When the lhs in a defined assignment has intent INOUT, we need a
9419 temporary for the lhs. In pseudo-code:
9420
9421 ! Only call function lhs once.
9422 if (lhs is not a constant or an variable)
9423 temp_x = expr2
9424 expr2 => temp_x
9425 ! Do the intrinsic assignment
9426 expr1 = expr2
9427 ! Now do the defined assignments
9428 do over components with typebound defined assignment [%cmp]
9429 #if one component's assignment procedure is INOUT
9430 t1 = expr1
9431 #if expr2 non-variable
9432 temp_x = expr2
9433 expr2 => temp_x
9434 # endif
9435 expr1 = expr2
9436 # for each cmp
9437 t1%cmp {defined=} expr2%cmp
9438 expr1%cmp = t1%cmp
9439 #else
9440 expr1 = expr2
9441
9442 # for each cmp
9443 expr1%cmp {defined=} expr2%cmp
9444 #endif
9445 */
9446
9447 /* The temporary assignments have to be put on top of the additional
9448 code to avoid the result being changed by the intrinsic assignment.
9449 */
9450 static int component_assignment_level = 0;
9451 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9452
9453 static void
9454 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9455 {
9456 gfc_component *comp1, *comp2;
9457 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9458 gfc_expr *t1;
9459 int error_count, depth;
9460
9461 gfc_get_errors (NULL, &error_count);
9462
9463 /* Filter out continuing processing after an error. */
9464 if (error_count
9465 || (*code)->expr1->ts.type != BT_DERIVED
9466 || (*code)->expr2->ts.type != BT_DERIVED)
9467 return;
9468
9469 /* TODO: Handle more than one part array reference in assignments. */
9470 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9471 (*code)->expr1->rank ? 1 : 0);
9472 if (depth > 1)
9473 {
9474 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9475 "done because multiple part array references would "
9476 "occur in intermediate expressions.", &(*code)->loc);
9477 return;
9478 }
9479
9480 component_assignment_level++;
9481
9482 /* Create a temporary so that functions get called only once. */
9483 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9484 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9485 {
9486 gfc_expr *tmp_expr;
9487
9488 /* Assign the rhs to the temporary. */
9489 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9490 this_code = build_assignment (EXEC_ASSIGN,
9491 tmp_expr, (*code)->expr2,
9492 NULL, NULL, (*code)->loc);
9493 /* Add the code and substitute the rhs expression. */
9494 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9495 gfc_free_expr ((*code)->expr2);
9496 (*code)->expr2 = tmp_expr;
9497 }
9498
9499 /* Do the intrinsic assignment. This is not needed if the lhs is one
9500 of the temporaries generated here, since the intrinsic assignment
9501 to the final result already does this. */
9502 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9503 {
9504 this_code = build_assignment (EXEC_ASSIGN,
9505 (*code)->expr1, (*code)->expr2,
9506 NULL, NULL, (*code)->loc);
9507 add_code_to_chain (&this_code, &head, &tail);
9508 }
9509
9510 comp1 = (*code)->expr1->ts.u.derived->components;
9511 comp2 = (*code)->expr2->ts.u.derived->components;
9512
9513 t1 = NULL;
9514 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9515 {
9516 bool inout = false;
9517
9518 /* The intrinsic assignment does the right thing for pointers
9519 of all kinds and allocatable components. */
9520 if (comp1->ts.type != BT_DERIVED
9521 || comp1->attr.pointer
9522 || comp1->attr.allocatable
9523 || comp1->attr.proc_pointer_comp
9524 || comp1->attr.class_pointer
9525 || comp1->attr.proc_pointer)
9526 continue;
9527
9528 /* Make an assigment for this component. */
9529 this_code = build_assignment (EXEC_ASSIGN,
9530 (*code)->expr1, (*code)->expr2,
9531 comp1, comp2, (*code)->loc);
9532
9533 /* Convert the assignment if there is a defined assignment for
9534 this type. Otherwise, using the call from resolve_code,
9535 recurse into its components. */
9536 resolve_code (this_code, ns);
9537
9538 if (this_code->op == EXEC_ASSIGN_CALL)
9539 {
9540 gfc_formal_arglist *dummy_args;
9541 gfc_symbol *rsym;
9542 /* Check that there is a typebound defined assignment. If not,
9543 then this must be a module defined assignment. We cannot
9544 use the defined_assign_comp attribute here because it must
9545 be this derived type that has the defined assignment and not
9546 a parent type. */
9547 if (!(comp1->ts.u.derived->f2k_derived
9548 && comp1->ts.u.derived->f2k_derived
9549 ->tb_op[INTRINSIC_ASSIGN]))
9550 {
9551 gfc_free_statements (this_code);
9552 this_code = NULL;
9553 continue;
9554 }
9555
9556 /* If the first argument of the subroutine has intent INOUT
9557 a temporary must be generated and used instead. */
9558 rsym = this_code->resolved_sym;
9559 dummy_args = gfc_sym_get_dummy_args (rsym);
9560 if (dummy_args
9561 && dummy_args->sym->attr.intent == INTENT_INOUT)
9562 {
9563 gfc_code *temp_code;
9564 inout = true;
9565
9566 /* Build the temporary required for the assignment and put
9567 it at the head of the generated code. */
9568 if (!t1)
9569 {
9570 t1 = get_temp_from_expr ((*code)->expr1, ns);
9571 temp_code = build_assignment (EXEC_ASSIGN,
9572 t1, (*code)->expr1,
9573 NULL, NULL, (*code)->loc);
9574
9575 /* For allocatable LHS, check whether it is allocated. Note
9576 that allocatable components with defined assignment are
9577 not yet support. See PR 57696. */
9578 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
9579 {
9580 gfc_code *block;
9581 gfc_expr *e =
9582 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9583 block = gfc_get_code (EXEC_IF);
9584 block->block = gfc_get_code (EXEC_IF);
9585 block->block->expr1
9586 = gfc_build_intrinsic_call (ns,
9587 GFC_ISYM_ALLOCATED, "allocated",
9588 (*code)->loc, 1, e);
9589 block->block->next = temp_code;
9590 temp_code = block;
9591 }
9592 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9593 }
9594
9595 /* Replace the first actual arg with the component of the
9596 temporary. */
9597 gfc_free_expr (this_code->ext.actual->expr);
9598 this_code->ext.actual->expr = gfc_copy_expr (t1);
9599 add_comp_ref (this_code->ext.actual->expr, comp1);
9600
9601 /* If the LHS variable is allocatable and wasn't allocated and
9602 the temporary is allocatable, pointer assign the address of
9603 the freshly allocated LHS to the temporary. */
9604 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9605 && gfc_expr_attr ((*code)->expr1).allocatable)
9606 {
9607 gfc_code *block;
9608 gfc_expr *cond;
9609
9610 cond = gfc_get_expr ();
9611 cond->ts.type = BT_LOGICAL;
9612 cond->ts.kind = gfc_default_logical_kind;
9613 cond->expr_type = EXPR_OP;
9614 cond->where = (*code)->loc;
9615 cond->value.op.op = INTRINSIC_NOT;
9616 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
9617 GFC_ISYM_ALLOCATED, "allocated",
9618 (*code)->loc, 1, gfc_copy_expr (t1));
9619 block = gfc_get_code (EXEC_IF);
9620 block->block = gfc_get_code (EXEC_IF);
9621 block->block->expr1 = cond;
9622 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9623 t1, (*code)->expr1,
9624 NULL, NULL, (*code)->loc);
9625 add_code_to_chain (&block, &head, &tail);
9626 }
9627 }
9628 }
9629 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9630 {
9631 /* Don't add intrinsic assignments since they are already
9632 effected by the intrinsic assignment of the structure. */
9633 gfc_free_statements (this_code);
9634 this_code = NULL;
9635 continue;
9636 }
9637
9638 add_code_to_chain (&this_code, &head, &tail);
9639
9640 if (t1 && inout)
9641 {
9642 /* Transfer the value to the final result. */
9643 this_code = build_assignment (EXEC_ASSIGN,
9644 (*code)->expr1, t1,
9645 comp1, comp2, (*code)->loc);
9646 add_code_to_chain (&this_code, &head, &tail);
9647 }
9648 }
9649
9650 /* Put the temporary assignments at the top of the generated code. */
9651 if (tmp_head && component_assignment_level == 1)
9652 {
9653 gfc_append_code (tmp_head, head);
9654 head = tmp_head;
9655 tmp_head = tmp_tail = NULL;
9656 }
9657
9658 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9659 // not accidentally deallocated. Hence, nullify t1.
9660 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9661 && gfc_expr_attr ((*code)->expr1).allocatable)
9662 {
9663 gfc_code *block;
9664 gfc_expr *cond;
9665 gfc_expr *e;
9666
9667 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9668 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9669 (*code)->loc, 2, gfc_copy_expr (t1), e);
9670 block = gfc_get_code (EXEC_IF);
9671 block->block = gfc_get_code (EXEC_IF);
9672 block->block->expr1 = cond;
9673 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9674 t1, gfc_get_null_expr (&(*code)->loc),
9675 NULL, NULL, (*code)->loc);
9676 gfc_append_code (tail, block);
9677 tail = block;
9678 }
9679
9680 /* Now attach the remaining code chain to the input code. Step on
9681 to the end of the new code since resolution is complete. */
9682 gcc_assert ((*code)->op == EXEC_ASSIGN);
9683 tail->next = (*code)->next;
9684 /* Overwrite 'code' because this would place the intrinsic assignment
9685 before the temporary for the lhs is created. */
9686 gfc_free_expr ((*code)->expr1);
9687 gfc_free_expr ((*code)->expr2);
9688 **code = *head;
9689 if (head != tail)
9690 free (head);
9691 *code = tail;
9692
9693 component_assignment_level--;
9694 }
9695
9696
9697 /* Given a block of code, recursively resolve everything pointed to by this
9698 code block. */
9699
9700 static void
9701 resolve_code (gfc_code *code, gfc_namespace *ns)
9702 {
9703 int omp_workshare_save;
9704 int forall_save, do_concurrent_save;
9705 code_stack frame;
9706 bool t;
9707
9708 frame.prev = cs_base;
9709 frame.head = code;
9710 cs_base = &frame;
9711
9712 find_reachable_labels (code);
9713
9714 for (; code; code = code->next)
9715 {
9716 frame.current = code;
9717 forall_save = forall_flag;
9718 do_concurrent_save = gfc_do_concurrent_flag;
9719
9720 if (code->op == EXEC_FORALL)
9721 {
9722 forall_flag = 1;
9723 gfc_resolve_forall (code, ns, forall_save);
9724 forall_flag = 2;
9725 }
9726 else if (code->block)
9727 {
9728 omp_workshare_save = -1;
9729 switch (code->op)
9730 {
9731 case EXEC_OMP_PARALLEL_WORKSHARE:
9732 omp_workshare_save = omp_workshare_flag;
9733 omp_workshare_flag = 1;
9734 gfc_resolve_omp_parallel_blocks (code, ns);
9735 break;
9736 case EXEC_OMP_PARALLEL:
9737 case EXEC_OMP_PARALLEL_DO:
9738 case EXEC_OMP_PARALLEL_SECTIONS:
9739 case EXEC_OMP_TASK:
9740 omp_workshare_save = omp_workshare_flag;
9741 omp_workshare_flag = 0;
9742 gfc_resolve_omp_parallel_blocks (code, ns);
9743 break;
9744 case EXEC_OMP_DO:
9745 gfc_resolve_omp_do_blocks (code, ns);
9746 break;
9747 case EXEC_SELECT_TYPE:
9748 /* Blocks are handled in resolve_select_type because we have
9749 to transform the SELECT TYPE into ASSOCIATE first. */
9750 break;
9751 case EXEC_DO_CONCURRENT:
9752 gfc_do_concurrent_flag = 1;
9753 gfc_resolve_blocks (code->block, ns);
9754 gfc_do_concurrent_flag = 2;
9755 break;
9756 case EXEC_OMP_WORKSHARE:
9757 omp_workshare_save = omp_workshare_flag;
9758 omp_workshare_flag = 1;
9759 /* FALL THROUGH */
9760 default:
9761 gfc_resolve_blocks (code->block, ns);
9762 break;
9763 }
9764
9765 if (omp_workshare_save != -1)
9766 omp_workshare_flag = omp_workshare_save;
9767 }
9768
9769 t = true;
9770 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9771 t = gfc_resolve_expr (code->expr1);
9772 forall_flag = forall_save;
9773 gfc_do_concurrent_flag = do_concurrent_save;
9774
9775 if (!gfc_resolve_expr (code->expr2))
9776 t = false;
9777
9778 if (code->op == EXEC_ALLOCATE
9779 && !gfc_resolve_expr (code->expr3))
9780 t = false;
9781
9782 switch (code->op)
9783 {
9784 case EXEC_NOP:
9785 case EXEC_END_BLOCK:
9786 case EXEC_END_NESTED_BLOCK:
9787 case EXEC_CYCLE:
9788 case EXEC_PAUSE:
9789 case EXEC_STOP:
9790 case EXEC_ERROR_STOP:
9791 case EXEC_EXIT:
9792 case EXEC_CONTINUE:
9793 case EXEC_DT_END:
9794 case EXEC_ASSIGN_CALL:
9795 case EXEC_CRITICAL:
9796 break;
9797
9798 case EXEC_SYNC_ALL:
9799 case EXEC_SYNC_IMAGES:
9800 case EXEC_SYNC_MEMORY:
9801 resolve_sync (code);
9802 break;
9803
9804 case EXEC_LOCK:
9805 case EXEC_UNLOCK:
9806 resolve_lock_unlock (code);
9807 break;
9808
9809 case EXEC_ENTRY:
9810 /* Keep track of which entry we are up to. */
9811 current_entry_id = code->ext.entry->id;
9812 break;
9813
9814 case EXEC_WHERE:
9815 resolve_where (code, NULL);
9816 break;
9817
9818 case EXEC_GOTO:
9819 if (code->expr1 != NULL)
9820 {
9821 if (code->expr1->ts.type != BT_INTEGER)
9822 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9823 "INTEGER variable", &code->expr1->where);
9824 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9825 gfc_error ("Variable '%s' has not been assigned a target "
9826 "label at %L", code->expr1->symtree->n.sym->name,
9827 &code->expr1->where);
9828 }
9829 else
9830 resolve_branch (code->label1, code);
9831 break;
9832
9833 case EXEC_RETURN:
9834 if (code->expr1 != NULL
9835 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9836 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9837 "INTEGER return specifier", &code->expr1->where);
9838 break;
9839
9840 case EXEC_INIT_ASSIGN:
9841 case EXEC_END_PROCEDURE:
9842 break;
9843
9844 case EXEC_ASSIGN:
9845 if (!t)
9846 break;
9847
9848 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9849 _("assignment")))
9850 break;
9851
9852 if (resolve_ordinary_assign (code, ns))
9853 {
9854 if (code->op == EXEC_COMPCALL)
9855 goto compcall;
9856 else
9857 goto call;
9858 }
9859
9860 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9861 if (code->expr1->ts.type == BT_DERIVED
9862 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9863 generate_component_assignments (&code, ns);
9864
9865 break;
9866
9867 case EXEC_LABEL_ASSIGN:
9868 if (code->label1->defined == ST_LABEL_UNKNOWN)
9869 gfc_error ("Label %d referenced at %L is never defined",
9870 code->label1->value, &code->label1->where);
9871 if (t
9872 && (code->expr1->expr_type != EXPR_VARIABLE
9873 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9874 || code->expr1->symtree->n.sym->ts.kind
9875 != gfc_default_integer_kind
9876 || code->expr1->symtree->n.sym->as != NULL))
9877 gfc_error ("ASSIGN statement at %L requires a scalar "
9878 "default INTEGER variable", &code->expr1->where);
9879 break;
9880
9881 case EXEC_POINTER_ASSIGN:
9882 {
9883 gfc_expr* e;
9884
9885 if (!t)
9886 break;
9887
9888 /* This is both a variable definition and pointer assignment
9889 context, so check both of them. For rank remapping, a final
9890 array ref may be present on the LHS and fool gfc_expr_attr
9891 used in gfc_check_vardef_context. Remove it. */
9892 e = remove_last_array_ref (code->expr1);
9893 t = gfc_check_vardef_context (e, true, false, false,
9894 _("pointer assignment"));
9895 if (t)
9896 t = gfc_check_vardef_context (e, false, false, false,
9897 _("pointer assignment"));
9898 gfc_free_expr (e);
9899 if (!t)
9900 break;
9901
9902 gfc_check_pointer_assign (code->expr1, code->expr2);
9903 break;
9904 }
9905
9906 case EXEC_ARITHMETIC_IF:
9907 if (t
9908 && code->expr1->ts.type != BT_INTEGER
9909 && code->expr1->ts.type != BT_REAL)
9910 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9911 "expression", &code->expr1->where);
9912
9913 resolve_branch (code->label1, code);
9914 resolve_branch (code->label2, code);
9915 resolve_branch (code->label3, code);
9916 break;
9917
9918 case EXEC_IF:
9919 if (t && code->expr1 != NULL
9920 && (code->expr1->ts.type != BT_LOGICAL
9921 || code->expr1->rank != 0))
9922 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9923 &code->expr1->where);
9924 break;
9925
9926 case EXEC_CALL:
9927 call:
9928 resolve_call (code);
9929 break;
9930
9931 case EXEC_COMPCALL:
9932 compcall:
9933 resolve_typebound_subroutine (code);
9934 break;
9935
9936 case EXEC_CALL_PPC:
9937 resolve_ppc_call (code);
9938 break;
9939
9940 case EXEC_SELECT:
9941 /* Select is complicated. Also, a SELECT construct could be
9942 a transformed computed GOTO. */
9943 resolve_select (code, false);
9944 break;
9945
9946 case EXEC_SELECT_TYPE:
9947 resolve_select_type (code, ns);
9948 break;
9949
9950 case EXEC_BLOCK:
9951 resolve_block_construct (code);
9952 break;
9953
9954 case EXEC_DO:
9955 if (code->ext.iterator != NULL)
9956 {
9957 gfc_iterator *iter = code->ext.iterator;
9958 if (gfc_resolve_iterator (iter, true, false))
9959 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9960 }
9961 break;
9962
9963 case EXEC_DO_WHILE:
9964 if (code->expr1 == NULL)
9965 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9966 if (t
9967 && (code->expr1->rank != 0
9968 || code->expr1->ts.type != BT_LOGICAL))
9969 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9970 "a scalar LOGICAL expression", &code->expr1->where);
9971 break;
9972
9973 case EXEC_ALLOCATE:
9974 if (t)
9975 resolve_allocate_deallocate (code, "ALLOCATE");
9976
9977 break;
9978
9979 case EXEC_DEALLOCATE:
9980 if (t)
9981 resolve_allocate_deallocate (code, "DEALLOCATE");
9982
9983 break;
9984
9985 case EXEC_OPEN:
9986 if (!gfc_resolve_open (code->ext.open))
9987 break;
9988
9989 resolve_branch (code->ext.open->err, code);
9990 break;
9991
9992 case EXEC_CLOSE:
9993 if (!gfc_resolve_close (code->ext.close))
9994 break;
9995
9996 resolve_branch (code->ext.close->err, code);
9997 break;
9998
9999 case EXEC_BACKSPACE:
10000 case EXEC_ENDFILE:
10001 case EXEC_REWIND:
10002 case EXEC_FLUSH:
10003 if (!gfc_resolve_filepos (code->ext.filepos))
10004 break;
10005
10006 resolve_branch (code->ext.filepos->err, code);
10007 break;
10008
10009 case EXEC_INQUIRE:
10010 if (!gfc_resolve_inquire (code->ext.inquire))
10011 break;
10012
10013 resolve_branch (code->ext.inquire->err, code);
10014 break;
10015
10016 case EXEC_IOLENGTH:
10017 gcc_assert (code->ext.inquire != NULL);
10018 if (!gfc_resolve_inquire (code->ext.inquire))
10019 break;
10020
10021 resolve_branch (code->ext.inquire->err, code);
10022 break;
10023
10024 case EXEC_WAIT:
10025 if (!gfc_resolve_wait (code->ext.wait))
10026 break;
10027
10028 resolve_branch (code->ext.wait->err, code);
10029 resolve_branch (code->ext.wait->end, code);
10030 resolve_branch (code->ext.wait->eor, code);
10031 break;
10032
10033 case EXEC_READ:
10034 case EXEC_WRITE:
10035 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10036 break;
10037
10038 resolve_branch (code->ext.dt->err, code);
10039 resolve_branch (code->ext.dt->end, code);
10040 resolve_branch (code->ext.dt->eor, code);
10041 break;
10042
10043 case EXEC_TRANSFER:
10044 resolve_transfer (code);
10045 break;
10046
10047 case EXEC_DO_CONCURRENT:
10048 case EXEC_FORALL:
10049 resolve_forall_iterators (code->ext.forall_iterator);
10050
10051 if (code->expr1 != NULL
10052 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10053 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10054 "expression", &code->expr1->where);
10055 break;
10056
10057 case EXEC_OMP_ATOMIC:
10058 case EXEC_OMP_BARRIER:
10059 case EXEC_OMP_CRITICAL:
10060 case EXEC_OMP_FLUSH:
10061 case EXEC_OMP_DO:
10062 case EXEC_OMP_MASTER:
10063 case EXEC_OMP_ORDERED:
10064 case EXEC_OMP_SECTIONS:
10065 case EXEC_OMP_SINGLE:
10066 case EXEC_OMP_TASKWAIT:
10067 case EXEC_OMP_TASKYIELD:
10068 case EXEC_OMP_WORKSHARE:
10069 gfc_resolve_omp_directive (code, ns);
10070 break;
10071
10072 case EXEC_OMP_PARALLEL:
10073 case EXEC_OMP_PARALLEL_DO:
10074 case EXEC_OMP_PARALLEL_SECTIONS:
10075 case EXEC_OMP_PARALLEL_WORKSHARE:
10076 case EXEC_OMP_TASK:
10077 omp_workshare_save = omp_workshare_flag;
10078 omp_workshare_flag = 0;
10079 gfc_resolve_omp_directive (code, ns);
10080 omp_workshare_flag = omp_workshare_save;
10081 break;
10082
10083 default:
10084 gfc_internal_error ("resolve_code(): Bad statement code");
10085 }
10086 }
10087
10088 cs_base = frame.prev;
10089 }
10090
10091
10092 /* Resolve initial values and make sure they are compatible with
10093 the variable. */
10094
10095 static void
10096 resolve_values (gfc_symbol *sym)
10097 {
10098 bool t;
10099
10100 if (sym->value == NULL)
10101 return;
10102
10103 if (sym->value->expr_type == EXPR_STRUCTURE)
10104 t= resolve_structure_cons (sym->value, 1);
10105 else
10106 t = gfc_resolve_expr (sym->value);
10107
10108 if (!t)
10109 return;
10110
10111 gfc_check_assign_symbol (sym, NULL, sym->value);
10112 }
10113
10114
10115 /* Verify any BIND(C) derived types in the namespace so we can report errors
10116 for them once, rather than for each variable declared of that type. */
10117
10118 static void
10119 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10120 {
10121 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10122 && derived_sym->attr.is_bind_c == 1)
10123 verify_bind_c_derived_type (derived_sym);
10124
10125 return;
10126 }
10127
10128
10129 /* Verify that any binding labels used in a given namespace do not collide
10130 with the names or binding labels of any global symbols. Multiple INTERFACE
10131 for the same procedure are permitted. */
10132
10133 static void
10134 gfc_verify_binding_labels (gfc_symbol *sym)
10135 {
10136 gfc_gsymbol *gsym;
10137 const char *module;
10138
10139 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10140 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10141 return;
10142
10143 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10144
10145 if (sym->module)
10146 module = sym->module;
10147 else if (sym->ns && sym->ns->proc_name
10148 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10149 module = sym->ns->proc_name->name;
10150 else if (sym->ns && sym->ns->parent
10151 && sym->ns && sym->ns->parent->proc_name
10152 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10153 module = sym->ns->parent->proc_name->name;
10154 else
10155 module = NULL;
10156
10157 if (!gsym
10158 || (!gsym->defined
10159 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10160 {
10161 if (!gsym)
10162 gsym = gfc_get_gsymbol (sym->binding_label);
10163 gsym->where = sym->declared_at;
10164 gsym->sym_name = sym->name;
10165 gsym->binding_label = sym->binding_label;
10166 gsym->ns = sym->ns;
10167 gsym->mod_name = module;
10168 if (sym->attr.function)
10169 gsym->type = GSYM_FUNCTION;
10170 else if (sym->attr.subroutine)
10171 gsym->type = GSYM_SUBROUTINE;
10172 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10173 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10174 return;
10175 }
10176
10177 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10178 {
10179 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10180 "identifier as entity at %L", sym->name,
10181 sym->binding_label, &sym->declared_at, &gsym->where);
10182 /* Clear the binding label to prevent checking multiple times. */
10183 sym->binding_label = NULL;
10184
10185 }
10186 else if (sym->attr.flavor == FL_VARIABLE
10187 && (strcmp (module, gsym->mod_name) != 0
10188 || strcmp (sym->name, gsym->sym_name) != 0))
10189 {
10190 /* This can only happen if the variable is defined in a module - if it
10191 isn't the same module, reject it. */
10192 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10193 "the same global identifier as entity at %L from module %s",
10194 sym->name, module, sym->binding_label,
10195 &sym->declared_at, &gsym->where, gsym->mod_name);
10196 sym->binding_label = NULL;
10197 }
10198 else if ((sym->attr.function || sym->attr.subroutine)
10199 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10200 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10201 && sym != gsym->ns->proc_name
10202 && (module != gsym->mod_name
10203 || strcmp (gsym->sym_name, sym->name) != 0
10204 || (module && strcmp (module, gsym->mod_name) != 0)))
10205 {
10206 /* Print an error if the procedure is defined multiple times; we have to
10207 exclude references to the same procedure via module association or
10208 multiple checks for the same procedure. */
10209 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10210 "global identifier as entity at %L", sym->name,
10211 sym->binding_label, &sym->declared_at, &gsym->where);
10212 sym->binding_label = NULL;
10213 }
10214 }
10215
10216
10217 /* Resolve an index expression. */
10218
10219 static bool
10220 resolve_index_expr (gfc_expr *e)
10221 {
10222 if (!gfc_resolve_expr (e))
10223 return false;
10224
10225 if (!gfc_simplify_expr (e, 0))
10226 return false;
10227
10228 if (!gfc_specification_expr (e))
10229 return false;
10230
10231 return true;
10232 }
10233
10234
10235 /* Resolve a charlen structure. */
10236
10237 static bool
10238 resolve_charlen (gfc_charlen *cl)
10239 {
10240 int i, k;
10241 bool saved_specification_expr;
10242
10243 if (cl->resolved)
10244 return true;
10245
10246 cl->resolved = 1;
10247 saved_specification_expr = specification_expr;
10248 specification_expr = true;
10249
10250 if (cl->length_from_typespec)
10251 {
10252 if (!gfc_resolve_expr (cl->length))
10253 {
10254 specification_expr = saved_specification_expr;
10255 return false;
10256 }
10257
10258 if (!gfc_simplify_expr (cl->length, 0))
10259 {
10260 specification_expr = saved_specification_expr;
10261 return false;
10262 }
10263 }
10264 else
10265 {
10266
10267 if (!resolve_index_expr (cl->length))
10268 {
10269 specification_expr = saved_specification_expr;
10270 return false;
10271 }
10272 }
10273
10274 /* "If the character length parameter value evaluates to a negative
10275 value, the length of character entities declared is zero." */
10276 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10277 {
10278 if (gfc_option.warn_surprising)
10279 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10280 " the length has been set to zero",
10281 &cl->length->where, i);
10282 gfc_replace_expr (cl->length,
10283 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10284 }
10285
10286 /* Check that the character length is not too large. */
10287 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10288 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10289 && cl->length->ts.type == BT_INTEGER
10290 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10291 {
10292 gfc_error ("String length at %L is too large", &cl->length->where);
10293 specification_expr = saved_specification_expr;
10294 return false;
10295 }
10296
10297 specification_expr = saved_specification_expr;
10298 return true;
10299 }
10300
10301
10302 /* Test for non-constant shape arrays. */
10303
10304 static bool
10305 is_non_constant_shape_array (gfc_symbol *sym)
10306 {
10307 gfc_expr *e;
10308 int i;
10309 bool not_constant;
10310
10311 not_constant = false;
10312 if (sym->as != NULL)
10313 {
10314 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10315 has not been simplified; parameter array references. Do the
10316 simplification now. */
10317 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10318 {
10319 e = sym->as->lower[i];
10320 if (e && (!resolve_index_expr(e)
10321 || !gfc_is_constant_expr (e)))
10322 not_constant = true;
10323 e = sym->as->upper[i];
10324 if (e && (!resolve_index_expr(e)
10325 || !gfc_is_constant_expr (e)))
10326 not_constant = true;
10327 }
10328 }
10329 return not_constant;
10330 }
10331
10332 /* Given a symbol and an initialization expression, add code to initialize
10333 the symbol to the function entry. */
10334 static void
10335 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10336 {
10337 gfc_expr *lval;
10338 gfc_code *init_st;
10339 gfc_namespace *ns = sym->ns;
10340
10341 /* Search for the function namespace if this is a contained
10342 function without an explicit result. */
10343 if (sym->attr.function && sym == sym->result
10344 && sym->name != sym->ns->proc_name->name)
10345 {
10346 ns = ns->contained;
10347 for (;ns; ns = ns->sibling)
10348 if (strcmp (ns->proc_name->name, sym->name) == 0)
10349 break;
10350 }
10351
10352 if (ns == NULL)
10353 {
10354 gfc_free_expr (init);
10355 return;
10356 }
10357
10358 /* Build an l-value expression for the result. */
10359 lval = gfc_lval_expr_from_sym (sym);
10360
10361 /* Add the code at scope entry. */
10362 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
10363 init_st->next = ns->code;
10364 ns->code = init_st;
10365
10366 /* Assign the default initializer to the l-value. */
10367 init_st->loc = sym->declared_at;
10368 init_st->expr1 = lval;
10369 init_st->expr2 = init;
10370 }
10371
10372 /* Assign the default initializer to a derived type variable or result. */
10373
10374 static void
10375 apply_default_init (gfc_symbol *sym)
10376 {
10377 gfc_expr *init = NULL;
10378
10379 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10380 return;
10381
10382 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10383 init = gfc_default_initializer (&sym->ts);
10384
10385 if (init == NULL && sym->ts.type != BT_CLASS)
10386 return;
10387
10388 build_init_assign (sym, init);
10389 sym->attr.referenced = 1;
10390 }
10391
10392 /* Build an initializer for a local integer, real, complex, logical, or
10393 character variable, based on the command line flags finit-local-zero,
10394 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10395 null if the symbol should not have a default initialization. */
10396 static gfc_expr *
10397 build_default_init_expr (gfc_symbol *sym)
10398 {
10399 int char_len;
10400 gfc_expr *init_expr;
10401 int i;
10402
10403 /* These symbols should never have a default initialization. */
10404 if (sym->attr.allocatable
10405 || sym->attr.external
10406 || sym->attr.dummy
10407 || sym->attr.pointer
10408 || sym->attr.in_equivalence
10409 || sym->attr.in_common
10410 || sym->attr.data
10411 || sym->module
10412 || sym->attr.cray_pointee
10413 || sym->attr.cray_pointer
10414 || sym->assoc)
10415 return NULL;
10416
10417 /* Now we'll try to build an initializer expression. */
10418 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10419 &sym->declared_at);
10420
10421 /* We will only initialize integers, reals, complex, logicals, and
10422 characters, and only if the corresponding command-line flags
10423 were set. Otherwise, we free init_expr and return null. */
10424 switch (sym->ts.type)
10425 {
10426 case BT_INTEGER:
10427 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10428 mpz_set_si (init_expr->value.integer,
10429 gfc_option.flag_init_integer_value);
10430 else
10431 {
10432 gfc_free_expr (init_expr);
10433 init_expr = NULL;
10434 }
10435 break;
10436
10437 case BT_REAL:
10438 switch (gfc_option.flag_init_real)
10439 {
10440 case GFC_INIT_REAL_SNAN:
10441 init_expr->is_snan = 1;
10442 /* Fall through. */
10443 case GFC_INIT_REAL_NAN:
10444 mpfr_set_nan (init_expr->value.real);
10445 break;
10446
10447 case GFC_INIT_REAL_INF:
10448 mpfr_set_inf (init_expr->value.real, 1);
10449 break;
10450
10451 case GFC_INIT_REAL_NEG_INF:
10452 mpfr_set_inf (init_expr->value.real, -1);
10453 break;
10454
10455 case GFC_INIT_REAL_ZERO:
10456 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10457 break;
10458
10459 default:
10460 gfc_free_expr (init_expr);
10461 init_expr = NULL;
10462 break;
10463 }
10464 break;
10465
10466 case BT_COMPLEX:
10467 switch (gfc_option.flag_init_real)
10468 {
10469 case GFC_INIT_REAL_SNAN:
10470 init_expr->is_snan = 1;
10471 /* Fall through. */
10472 case GFC_INIT_REAL_NAN:
10473 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10474 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10475 break;
10476
10477 case GFC_INIT_REAL_INF:
10478 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10479 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10480 break;
10481
10482 case GFC_INIT_REAL_NEG_INF:
10483 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10484 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10485 break;
10486
10487 case GFC_INIT_REAL_ZERO:
10488 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10489 break;
10490
10491 default:
10492 gfc_free_expr (init_expr);
10493 init_expr = NULL;
10494 break;
10495 }
10496 break;
10497
10498 case BT_LOGICAL:
10499 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10500 init_expr->value.logical = 0;
10501 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10502 init_expr->value.logical = 1;
10503 else
10504 {
10505 gfc_free_expr (init_expr);
10506 init_expr = NULL;
10507 }
10508 break;
10509
10510 case BT_CHARACTER:
10511 /* For characters, the length must be constant in order to
10512 create a default initializer. */
10513 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10514 && sym->ts.u.cl->length
10515 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10516 {
10517 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10518 init_expr->value.character.length = char_len;
10519 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10520 for (i = 0; i < char_len; i++)
10521 init_expr->value.character.string[i]
10522 = (unsigned char) gfc_option.flag_init_character_value;
10523 }
10524 else
10525 {
10526 gfc_free_expr (init_expr);
10527 init_expr = NULL;
10528 }
10529 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10530 && sym->ts.u.cl->length && gfc_option.flag_max_stack_var_size != 0)
10531 {
10532 gfc_actual_arglist *arg;
10533 init_expr = gfc_get_expr ();
10534 init_expr->where = sym->declared_at;
10535 init_expr->ts = sym->ts;
10536 init_expr->expr_type = EXPR_FUNCTION;
10537 init_expr->value.function.isym =
10538 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10539 init_expr->value.function.name = "repeat";
10540 arg = gfc_get_actual_arglist ();
10541 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10542 NULL, 1);
10543 arg->expr->value.character.string[0]
10544 = gfc_option.flag_init_character_value;
10545 arg->next = gfc_get_actual_arglist ();
10546 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10547 init_expr->value.function.actual = arg;
10548 }
10549 break;
10550
10551 default:
10552 gfc_free_expr (init_expr);
10553 init_expr = NULL;
10554 }
10555 return init_expr;
10556 }
10557
10558 /* Add an initialization expression to a local variable. */
10559 static void
10560 apply_default_init_local (gfc_symbol *sym)
10561 {
10562 gfc_expr *init = NULL;
10563
10564 /* The symbol should be a variable or a function return value. */
10565 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10566 || (sym->attr.function && sym->result != sym))
10567 return;
10568
10569 /* Try to build the initializer expression. If we can't initialize
10570 this symbol, then init will be NULL. */
10571 init = build_default_init_expr (sym);
10572 if (init == NULL)
10573 return;
10574
10575 /* For saved variables, we don't want to add an initializer at function
10576 entry, so we just add a static initializer. Note that automatic variables
10577 are stack allocated even with -fno-automatic; we have also to exclude
10578 result variable, which are also nonstatic. */
10579 if (sym->attr.save || sym->ns->save_all
10580 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10581 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10582 {
10583 /* Don't clobber an existing initializer! */
10584 gcc_assert (sym->value == NULL);
10585 sym->value = init;
10586 return;
10587 }
10588
10589 build_init_assign (sym, init);
10590 }
10591
10592
10593 /* Resolution of common features of flavors variable and procedure. */
10594
10595 static bool
10596 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10597 {
10598 gfc_array_spec *as;
10599
10600 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10601 as = CLASS_DATA (sym)->as;
10602 else
10603 as = sym->as;
10604
10605 /* Constraints on deferred shape variable. */
10606 if (as == NULL || as->type != AS_DEFERRED)
10607 {
10608 bool pointer, allocatable, dimension;
10609
10610 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10611 {
10612 pointer = CLASS_DATA (sym)->attr.class_pointer;
10613 allocatable = CLASS_DATA (sym)->attr.allocatable;
10614 dimension = CLASS_DATA (sym)->attr.dimension;
10615 }
10616 else
10617 {
10618 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10619 allocatable = sym->attr.allocatable;
10620 dimension = sym->attr.dimension;
10621 }
10622
10623 if (allocatable)
10624 {
10625 if (dimension && as->type != AS_ASSUMED_RANK)
10626 {
10627 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10628 "shape or assumed rank", sym->name, &sym->declared_at);
10629 return false;
10630 }
10631 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10632 "'%s' at %L may not be ALLOCATABLE",
10633 sym->name, &sym->declared_at))
10634 return false;
10635 }
10636
10637 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10638 {
10639 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10640 "assumed rank", sym->name, &sym->declared_at);
10641 return false;
10642 }
10643 }
10644 else
10645 {
10646 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10647 && sym->ts.type != BT_CLASS && !sym->assoc)
10648 {
10649 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10650 sym->name, &sym->declared_at);
10651 return false;
10652 }
10653 }
10654
10655 /* Constraints on polymorphic variables. */
10656 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10657 {
10658 /* F03:C502. */
10659 if (sym->attr.class_ok
10660 && !sym->attr.select_type_temporary
10661 && !UNLIMITED_POLY (sym)
10662 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10663 {
10664 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10665 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10666 &sym->declared_at);
10667 return false;
10668 }
10669
10670 /* F03:C509. */
10671 /* Assume that use associated symbols were checked in the module ns.
10672 Class-variables that are associate-names are also something special
10673 and excepted from the test. */
10674 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10675 {
10676 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10677 "or pointer", sym->name, &sym->declared_at);
10678 return false;
10679 }
10680 }
10681
10682 return true;
10683 }
10684
10685
10686 /* Additional checks for symbols with flavor variable and derived
10687 type. To be called from resolve_fl_variable. */
10688
10689 static bool
10690 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10691 {
10692 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10693
10694 /* Check to see if a derived type is blocked from being host
10695 associated by the presence of another class I symbol in the same
10696 namespace. 14.6.1.3 of the standard and the discussion on
10697 comp.lang.fortran. */
10698 if (sym->ns != sym->ts.u.derived->ns
10699 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10700 {
10701 gfc_symbol *s;
10702 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10703 if (s && s->attr.generic)
10704 s = gfc_find_dt_in_generic (s);
10705 if (s && s->attr.flavor != FL_DERIVED)
10706 {
10707 gfc_error ("The type '%s' cannot be host associated at %L "
10708 "because it is blocked by an incompatible object "
10709 "of the same name declared at %L",
10710 sym->ts.u.derived->name, &sym->declared_at,
10711 &s->declared_at);
10712 return false;
10713 }
10714 }
10715
10716 /* 4th constraint in section 11.3: "If an object of a type for which
10717 component-initialization is specified (R429) appears in the
10718 specification-part of a module and does not have the ALLOCATABLE
10719 or POINTER attribute, the object shall have the SAVE attribute."
10720
10721 The check for initializers is performed with
10722 gfc_has_default_initializer because gfc_default_initializer generates
10723 a hidden default for allocatable components. */
10724 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10725 && sym->ns->proc_name->attr.flavor == FL_MODULE
10726 && !sym->ns->save_all && !sym->attr.save
10727 && !sym->attr.pointer && !sym->attr.allocatable
10728 && gfc_has_default_initializer (sym->ts.u.derived)
10729 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10730 "'%s' at %L, needed due to the default "
10731 "initialization", sym->name, &sym->declared_at))
10732 return false;
10733
10734 /* Assign default initializer. */
10735 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10736 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10737 {
10738 sym->value = gfc_default_initializer (&sym->ts);
10739 }
10740
10741 return true;
10742 }
10743
10744
10745 /* Resolve symbols with flavor variable. */
10746
10747 static bool
10748 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10749 {
10750 int no_init_flag, automatic_flag;
10751 gfc_expr *e;
10752 const char *auto_save_msg;
10753 bool saved_specification_expr;
10754
10755 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10756 "SAVE attribute";
10757
10758 if (!resolve_fl_var_and_proc (sym, mp_flag))
10759 return false;
10760
10761 /* Set this flag to check that variables are parameters of all entries.
10762 This check is effected by the call to gfc_resolve_expr through
10763 is_non_constant_shape_array. */
10764 saved_specification_expr = specification_expr;
10765 specification_expr = true;
10766
10767 if (sym->ns->proc_name
10768 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10769 || sym->ns->proc_name->attr.is_main_program)
10770 && !sym->attr.use_assoc
10771 && !sym->attr.allocatable
10772 && !sym->attr.pointer
10773 && is_non_constant_shape_array (sym))
10774 {
10775 /* The shape of a main program or module array needs to be
10776 constant. */
10777 gfc_error ("The module or main program array '%s' at %L must "
10778 "have constant shape", sym->name, &sym->declared_at);
10779 specification_expr = saved_specification_expr;
10780 return false;
10781 }
10782
10783 /* Constraints on deferred type parameter. */
10784 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10785 {
10786 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10787 "requires either the pointer or allocatable attribute",
10788 sym->name, &sym->declared_at);
10789 specification_expr = saved_specification_expr;
10790 return false;
10791 }
10792
10793 if (sym->ts.type == BT_CHARACTER)
10794 {
10795 /* Make sure that character string variables with assumed length are
10796 dummy arguments. */
10797 e = sym->ts.u.cl->length;
10798 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10799 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10800 {
10801 gfc_error ("Entity with assumed character length at %L must be a "
10802 "dummy argument or a PARAMETER", &sym->declared_at);
10803 specification_expr = saved_specification_expr;
10804 return false;
10805 }
10806
10807 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10808 {
10809 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10810 specification_expr = saved_specification_expr;
10811 return false;
10812 }
10813
10814 if (!gfc_is_constant_expr (e)
10815 && !(e->expr_type == EXPR_VARIABLE
10816 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10817 {
10818 if (!sym->attr.use_assoc && sym->ns->proc_name
10819 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10820 || sym->ns->proc_name->attr.is_main_program))
10821 {
10822 gfc_error ("'%s' at %L must have constant character length "
10823 "in this context", sym->name, &sym->declared_at);
10824 specification_expr = saved_specification_expr;
10825 return false;
10826 }
10827 if (sym->attr.in_common)
10828 {
10829 gfc_error ("COMMON variable '%s' at %L must have constant "
10830 "character length", sym->name, &sym->declared_at);
10831 specification_expr = saved_specification_expr;
10832 return false;
10833 }
10834 }
10835 }
10836
10837 if (sym->value == NULL && sym->attr.referenced)
10838 apply_default_init_local (sym); /* Try to apply a default initialization. */
10839
10840 /* Determine if the symbol may not have an initializer. */
10841 no_init_flag = automatic_flag = 0;
10842 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10843 || sym->attr.intrinsic || sym->attr.result)
10844 no_init_flag = 1;
10845 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10846 && is_non_constant_shape_array (sym))
10847 {
10848 no_init_flag = automatic_flag = 1;
10849
10850 /* Also, they must not have the SAVE attribute.
10851 SAVE_IMPLICIT is checked below. */
10852 if (sym->as && sym->attr.codimension)
10853 {
10854 int corank = sym->as->corank;
10855 sym->as->corank = 0;
10856 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10857 sym->as->corank = corank;
10858 }
10859 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10860 {
10861 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10862 specification_expr = saved_specification_expr;
10863 return false;
10864 }
10865 }
10866
10867 /* Ensure that any initializer is simplified. */
10868 if (sym->value)
10869 gfc_simplify_expr (sym->value, 1);
10870
10871 /* Reject illegal initializers. */
10872 if (!sym->mark && sym->value)
10873 {
10874 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10875 && CLASS_DATA (sym)->attr.allocatable))
10876 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10877 sym->name, &sym->declared_at);
10878 else if (sym->attr.external)
10879 gfc_error ("External '%s' at %L cannot have an initializer",
10880 sym->name, &sym->declared_at);
10881 else if (sym->attr.dummy
10882 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10883 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10884 sym->name, &sym->declared_at);
10885 else if (sym->attr.intrinsic)
10886 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10887 sym->name, &sym->declared_at);
10888 else if (sym->attr.result)
10889 gfc_error ("Function result '%s' at %L cannot have an initializer",
10890 sym->name, &sym->declared_at);
10891 else if (automatic_flag)
10892 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10893 sym->name, &sym->declared_at);
10894 else
10895 goto no_init_error;
10896 specification_expr = saved_specification_expr;
10897 return false;
10898 }
10899
10900 no_init_error:
10901 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10902 {
10903 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10904 specification_expr = saved_specification_expr;
10905 return res;
10906 }
10907
10908 specification_expr = saved_specification_expr;
10909 return true;
10910 }
10911
10912
10913 /* Resolve a procedure. */
10914
10915 static bool
10916 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10917 {
10918 gfc_formal_arglist *arg;
10919
10920 if (sym->attr.function
10921 && !resolve_fl_var_and_proc (sym, mp_flag))
10922 return false;
10923
10924 if (sym->ts.type == BT_CHARACTER)
10925 {
10926 gfc_charlen *cl = sym->ts.u.cl;
10927
10928 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10929 && !resolve_charlen (cl))
10930 return false;
10931
10932 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10933 && sym->attr.proc == PROC_ST_FUNCTION)
10934 {
10935 gfc_error ("Character-valued statement function '%s' at %L must "
10936 "have constant length", sym->name, &sym->declared_at);
10937 return false;
10938 }
10939 }
10940
10941 /* Ensure that derived type for are not of a private type. Internal
10942 module procedures are excluded by 2.2.3.3 - i.e., they are not
10943 externally accessible and can access all the objects accessible in
10944 the host. */
10945 if (!(sym->ns->parent
10946 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10947 && gfc_check_symbol_access (sym))
10948 {
10949 gfc_interface *iface;
10950
10951 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10952 {
10953 if (arg->sym
10954 && arg->sym->ts.type == BT_DERIVED
10955 && !arg->sym->ts.u.derived->attr.use_assoc
10956 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10957 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10958 "and cannot be a dummy argument"
10959 " of '%s', which is PUBLIC at %L",
10960 arg->sym->name, sym->name,
10961 &sym->declared_at))
10962 {
10963 /* Stop this message from recurring. */
10964 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10965 return false;
10966 }
10967 }
10968
10969 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10970 PRIVATE to the containing module. */
10971 for (iface = sym->generic; iface; iface = iface->next)
10972 {
10973 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10974 {
10975 if (arg->sym
10976 && arg->sym->ts.type == BT_DERIVED
10977 && !arg->sym->ts.u.derived->attr.use_assoc
10978 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10979 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10980 "PUBLIC interface '%s' at %L "
10981 "takes dummy arguments of '%s' which "
10982 "is PRIVATE", iface->sym->name,
10983 sym->name, &iface->sym->declared_at,
10984 gfc_typename(&arg->sym->ts)))
10985 {
10986 /* Stop this message from recurring. */
10987 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10988 return false;
10989 }
10990 }
10991 }
10992
10993 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10994 PRIVATE to the containing module. */
10995 for (iface = sym->generic; iface; iface = iface->next)
10996 {
10997 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10998 {
10999 if (arg->sym
11000 && arg->sym->ts.type == BT_DERIVED
11001 && !arg->sym->ts.u.derived->attr.use_assoc
11002 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11003 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11004 "PUBLIC interface '%s' at %L takes "
11005 "dummy arguments of '%s' which is "
11006 "PRIVATE", iface->sym->name,
11007 sym->name, &iface->sym->declared_at,
11008 gfc_typename(&arg->sym->ts)))
11009 {
11010 /* Stop this message from recurring. */
11011 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11012 return false;
11013 }
11014 }
11015 }
11016 }
11017
11018 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11019 && !sym->attr.proc_pointer)
11020 {
11021 gfc_error ("Function '%s' at %L cannot have an initializer",
11022 sym->name, &sym->declared_at);
11023 return false;
11024 }
11025
11026 /* An external symbol may not have an initializer because it is taken to be
11027 a procedure. Exception: Procedure Pointers. */
11028 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11029 {
11030 gfc_error ("External object '%s' at %L may not have an initializer",
11031 sym->name, &sym->declared_at);
11032 return false;
11033 }
11034
11035 /* An elemental function is required to return a scalar 12.7.1 */
11036 if (sym->attr.elemental && sym->attr.function && sym->as)
11037 {
11038 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11039 "result", sym->name, &sym->declared_at);
11040 /* Reset so that the error only occurs once. */
11041 sym->attr.elemental = 0;
11042 return false;
11043 }
11044
11045 if (sym->attr.proc == PROC_ST_FUNCTION
11046 && (sym->attr.allocatable || sym->attr.pointer))
11047 {
11048 gfc_error ("Statement function '%s' at %L may not have pointer or "
11049 "allocatable attribute", sym->name, &sym->declared_at);
11050 return false;
11051 }
11052
11053 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11054 char-len-param shall not be array-valued, pointer-valued, recursive
11055 or pure. ....snip... A character value of * may only be used in the
11056 following ways: (i) Dummy arg of procedure - dummy associates with
11057 actual length; (ii) To declare a named constant; or (iii) External
11058 function - but length must be declared in calling scoping unit. */
11059 if (sym->attr.function
11060 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11061 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11062 {
11063 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11064 || (sym->attr.recursive) || (sym->attr.pure))
11065 {
11066 if (sym->as && sym->as->rank)
11067 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11068 "array-valued", sym->name, &sym->declared_at);
11069
11070 if (sym->attr.pointer)
11071 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11072 "pointer-valued", sym->name, &sym->declared_at);
11073
11074 if (sym->attr.pure)
11075 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11076 "pure", sym->name, &sym->declared_at);
11077
11078 if (sym->attr.recursive)
11079 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11080 "recursive", sym->name, &sym->declared_at);
11081
11082 return false;
11083 }
11084
11085 /* Appendix B.2 of the standard. Contained functions give an
11086 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11087 character length is an F2003 feature. */
11088 if (!sym->attr.contained
11089 && gfc_current_form != FORM_FIXED
11090 && !sym->ts.deferred)
11091 gfc_notify_std (GFC_STD_F95_OBS,
11092 "CHARACTER(*) function '%s' at %L",
11093 sym->name, &sym->declared_at);
11094 }
11095
11096 /* F2008, C1218. */
11097 if (sym->attr.elemental)
11098 {
11099 if (sym->attr.proc_pointer)
11100 {
11101 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11102 sym->name, &sym->declared_at);
11103 return false;
11104 }
11105 if (sym->attr.dummy)
11106 {
11107 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11108 sym->name, &sym->declared_at);
11109 return false;
11110 }
11111 }
11112
11113 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11114 {
11115 gfc_formal_arglist *curr_arg;
11116 int has_non_interop_arg = 0;
11117
11118 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11119 sym->common_block))
11120 {
11121 /* Clear these to prevent looking at them again if there was an
11122 error. */
11123 sym->attr.is_bind_c = 0;
11124 sym->attr.is_c_interop = 0;
11125 sym->ts.is_c_interop = 0;
11126 }
11127 else
11128 {
11129 /* So far, no errors have been found. */
11130 sym->attr.is_c_interop = 1;
11131 sym->ts.is_c_interop = 1;
11132 }
11133
11134 curr_arg = gfc_sym_get_dummy_args (sym);
11135 while (curr_arg != NULL)
11136 {
11137 /* Skip implicitly typed dummy args here. */
11138 if (curr_arg->sym->attr.implicit_type == 0)
11139 if (!gfc_verify_c_interop_param (curr_arg->sym))
11140 /* If something is found to fail, record the fact so we
11141 can mark the symbol for the procedure as not being
11142 BIND(C) to try and prevent multiple errors being
11143 reported. */
11144 has_non_interop_arg = 1;
11145
11146 curr_arg = curr_arg->next;
11147 }
11148
11149 /* See if any of the arguments were not interoperable and if so, clear
11150 the procedure symbol to prevent duplicate error messages. */
11151 if (has_non_interop_arg != 0)
11152 {
11153 sym->attr.is_c_interop = 0;
11154 sym->ts.is_c_interop = 0;
11155 sym->attr.is_bind_c = 0;
11156 }
11157 }
11158
11159 if (!sym->attr.proc_pointer)
11160 {
11161 if (sym->attr.save == SAVE_EXPLICIT)
11162 {
11163 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11164 "in '%s' at %L", sym->name, &sym->declared_at);
11165 return false;
11166 }
11167 if (sym->attr.intent)
11168 {
11169 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11170 "in '%s' at %L", sym->name, &sym->declared_at);
11171 return false;
11172 }
11173 if (sym->attr.subroutine && sym->attr.result)
11174 {
11175 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11176 "in '%s' at %L", sym->name, &sym->declared_at);
11177 return false;
11178 }
11179 if (sym->attr.external && sym->attr.function
11180 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11181 || sym->attr.contained))
11182 {
11183 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11184 "in '%s' at %L", sym->name, &sym->declared_at);
11185 return false;
11186 }
11187 if (strcmp ("ppr@", sym->name) == 0)
11188 {
11189 gfc_error ("Procedure pointer result '%s' at %L "
11190 "is missing the pointer attribute",
11191 sym->ns->proc_name->name, &sym->declared_at);
11192 return false;
11193 }
11194 }
11195
11196 return true;
11197 }
11198
11199
11200 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11201 been defined and we now know their defined arguments, check that they fulfill
11202 the requirements of the standard for procedures used as finalizers. */
11203
11204 static bool
11205 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11206 {
11207 gfc_finalizer* list;
11208 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11209 bool result = true;
11210 bool seen_scalar = false;
11211 gfc_symbol *vtab;
11212 gfc_component *c;
11213
11214 /* Return early when not finalizable. Additionally, ensure that derived-type
11215 components have a their finalizables resolved. */
11216 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11217 {
11218 bool has_final = false;
11219 for (c = derived->components; c; c = c->next)
11220 if (c->ts.type == BT_DERIVED
11221 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11222 {
11223 bool has_final2 = false;
11224 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11225 return false; /* Error. */
11226 has_final = has_final || has_final2;
11227 }
11228 if (!has_final)
11229 {
11230 if (finalizable)
11231 *finalizable = false;
11232 return true;
11233 }
11234 }
11235
11236 /* Walk over the list of finalizer-procedures, check them, and if any one
11237 does not fit in with the standard's definition, print an error and remove
11238 it from the list. */
11239 prev_link = &derived->f2k_derived->finalizers;
11240 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11241 {
11242 gfc_formal_arglist *dummy_args;
11243 gfc_symbol* arg;
11244 gfc_finalizer* i;
11245 int my_rank;
11246
11247 /* Skip this finalizer if we already resolved it. */
11248 if (list->proc_tree)
11249 {
11250 prev_link = &(list->next);
11251 continue;
11252 }
11253
11254 /* Check this exists and is a SUBROUTINE. */
11255 if (!list->proc_sym->attr.subroutine)
11256 {
11257 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11258 list->proc_sym->name, &list->where);
11259 goto error;
11260 }
11261
11262 /* We should have exactly one argument. */
11263 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11264 if (!dummy_args || dummy_args->next)
11265 {
11266 gfc_error ("FINAL procedure at %L must have exactly one argument",
11267 &list->where);
11268 goto error;
11269 }
11270 arg = dummy_args->sym;
11271
11272 /* This argument must be of our type. */
11273 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11274 {
11275 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11276 &arg->declared_at, derived->name);
11277 goto error;
11278 }
11279
11280 /* It must neither be a pointer nor allocatable nor optional. */
11281 if (arg->attr.pointer)
11282 {
11283 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11284 &arg->declared_at);
11285 goto error;
11286 }
11287 if (arg->attr.allocatable)
11288 {
11289 gfc_error ("Argument of FINAL procedure at %L must not be"
11290 " ALLOCATABLE", &arg->declared_at);
11291 goto error;
11292 }
11293 if (arg->attr.optional)
11294 {
11295 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11296 &arg->declared_at);
11297 goto error;
11298 }
11299
11300 /* It must not be INTENT(OUT). */
11301 if (arg->attr.intent == INTENT_OUT)
11302 {
11303 gfc_error ("Argument of FINAL procedure at %L must not be"
11304 " INTENT(OUT)", &arg->declared_at);
11305 goto error;
11306 }
11307
11308 /* Warn if the procedure is non-scalar and not assumed shape. */
11309 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11310 && arg->as->type != AS_ASSUMED_SHAPE)
11311 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11312 " shape argument", &arg->declared_at);
11313
11314 /* Check that it does not match in kind and rank with a FINAL procedure
11315 defined earlier. To really loop over the *earlier* declarations,
11316 we need to walk the tail of the list as new ones were pushed at the
11317 front. */
11318 /* TODO: Handle kind parameters once they are implemented. */
11319 my_rank = (arg->as ? arg->as->rank : 0);
11320 for (i = list->next; i; i = i->next)
11321 {
11322 gfc_formal_arglist *dummy_args;
11323
11324 /* Argument list might be empty; that is an error signalled earlier,
11325 but we nevertheless continued resolving. */
11326 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11327 if (dummy_args)
11328 {
11329 gfc_symbol* i_arg = dummy_args->sym;
11330 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11331 if (i_rank == my_rank)
11332 {
11333 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11334 " rank (%d) as '%s'",
11335 list->proc_sym->name, &list->where, my_rank,
11336 i->proc_sym->name);
11337 goto error;
11338 }
11339 }
11340 }
11341
11342 /* Is this the/a scalar finalizer procedure? */
11343 if (!arg->as || arg->as->rank == 0)
11344 seen_scalar = true;
11345
11346 /* Find the symtree for this procedure. */
11347 gcc_assert (!list->proc_tree);
11348 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11349
11350 prev_link = &list->next;
11351 continue;
11352
11353 /* Remove wrong nodes immediately from the list so we don't risk any
11354 troubles in the future when they might fail later expectations. */
11355 error:
11356 i = list;
11357 *prev_link = list->next;
11358 gfc_free_finalizer (i);
11359 result = false;
11360 }
11361
11362 if (result == false)
11363 return false;
11364
11365 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11366 were nodes in the list, must have been for arrays. It is surely a good
11367 idea to have a scalar version there if there's something to finalize. */
11368 if (gfc_option.warn_surprising && result && !seen_scalar)
11369 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11370 " defined at %L, suggest also scalar one",
11371 derived->name, &derived->declared_at);
11372
11373 vtab = gfc_find_derived_vtab (derived);
11374 c = vtab->ts.u.derived->components->next->next->next->next->next;
11375 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
11376
11377 if (finalizable)
11378 *finalizable = true;
11379
11380 return true;
11381 }
11382
11383
11384 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11385
11386 static bool
11387 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11388 const char* generic_name, locus where)
11389 {
11390 gfc_symbol *sym1, *sym2;
11391 const char *pass1, *pass2;
11392 gfc_formal_arglist *dummy_args;
11393
11394 gcc_assert (t1->specific && t2->specific);
11395 gcc_assert (!t1->specific->is_generic);
11396 gcc_assert (!t2->specific->is_generic);
11397 gcc_assert (t1->is_operator == t2->is_operator);
11398
11399 sym1 = t1->specific->u.specific->n.sym;
11400 sym2 = t2->specific->u.specific->n.sym;
11401
11402 if (sym1 == sym2)
11403 return true;
11404
11405 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11406 if (sym1->attr.subroutine != sym2->attr.subroutine
11407 || sym1->attr.function != sym2->attr.function)
11408 {
11409 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11410 " GENERIC '%s' at %L",
11411 sym1->name, sym2->name, generic_name, &where);
11412 return false;
11413 }
11414
11415 /* Determine PASS arguments. */
11416 if (t1->specific->nopass)
11417 pass1 = NULL;
11418 else if (t1->specific->pass_arg)
11419 pass1 = t1->specific->pass_arg;
11420 else
11421 {
11422 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
11423 if (dummy_args)
11424 pass1 = dummy_args->sym->name;
11425 else
11426 pass1 = NULL;
11427 }
11428 if (t2->specific->nopass)
11429 pass2 = NULL;
11430 else if (t2->specific->pass_arg)
11431 pass2 = t2->specific->pass_arg;
11432 else
11433 {
11434 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
11435 if (dummy_args)
11436 pass2 = dummy_args->sym->name;
11437 else
11438 pass2 = NULL;
11439 }
11440
11441 /* Compare the interfaces. */
11442 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11443 NULL, 0, pass1, pass2))
11444 {
11445 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11446 sym1->name, sym2->name, generic_name, &where);
11447 return false;
11448 }
11449
11450 return true;
11451 }
11452
11453
11454 /* Worker function for resolving a generic procedure binding; this is used to
11455 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11456
11457 The difference between those cases is finding possible inherited bindings
11458 that are overridden, as one has to look for them in tb_sym_root,
11459 tb_uop_root or tb_op, respectively. Thus the caller must already find
11460 the super-type and set p->overridden correctly. */
11461
11462 static bool
11463 resolve_tb_generic_targets (gfc_symbol* super_type,
11464 gfc_typebound_proc* p, const char* name)
11465 {
11466 gfc_tbp_generic* target;
11467 gfc_symtree* first_target;
11468 gfc_symtree* inherited;
11469
11470 gcc_assert (p && p->is_generic);
11471
11472 /* Try to find the specific bindings for the symtrees in our target-list. */
11473 gcc_assert (p->u.generic);
11474 for (target = p->u.generic; target; target = target->next)
11475 if (!target->specific)
11476 {
11477 gfc_typebound_proc* overridden_tbp;
11478 gfc_tbp_generic* g;
11479 const char* target_name;
11480
11481 target_name = target->specific_st->name;
11482
11483 /* Defined for this type directly. */
11484 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11485 {
11486 target->specific = target->specific_st->n.tb;
11487 goto specific_found;
11488 }
11489
11490 /* Look for an inherited specific binding. */
11491 if (super_type)
11492 {
11493 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11494 true, NULL);
11495
11496 if (inherited)
11497 {
11498 gcc_assert (inherited->n.tb);
11499 target->specific = inherited->n.tb;
11500 goto specific_found;
11501 }
11502 }
11503
11504 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11505 " at %L", target_name, name, &p->where);
11506 return false;
11507
11508 /* Once we've found the specific binding, check it is not ambiguous with
11509 other specifics already found or inherited for the same GENERIC. */
11510 specific_found:
11511 gcc_assert (target->specific);
11512
11513 /* This must really be a specific binding! */
11514 if (target->specific->is_generic)
11515 {
11516 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11517 " '%s' is GENERIC, too", name, &p->where, target_name);
11518 return false;
11519 }
11520
11521 /* Check those already resolved on this type directly. */
11522 for (g = p->u.generic; g; g = g->next)
11523 if (g != target && g->specific
11524 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11525 return false;
11526
11527 /* Check for ambiguity with inherited specific targets. */
11528 for (overridden_tbp = p->overridden; overridden_tbp;
11529 overridden_tbp = overridden_tbp->overridden)
11530 if (overridden_tbp->is_generic)
11531 {
11532 for (g = overridden_tbp->u.generic; g; g = g->next)
11533 {
11534 gcc_assert (g->specific);
11535 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11536 return false;
11537 }
11538 }
11539 }
11540
11541 /* If we attempt to "overwrite" a specific binding, this is an error. */
11542 if (p->overridden && !p->overridden->is_generic)
11543 {
11544 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11545 " the same name", name, &p->where);
11546 return false;
11547 }
11548
11549 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11550 all must have the same attributes here. */
11551 first_target = p->u.generic->specific->u.specific;
11552 gcc_assert (first_target);
11553 p->subroutine = first_target->n.sym->attr.subroutine;
11554 p->function = first_target->n.sym->attr.function;
11555
11556 return true;
11557 }
11558
11559
11560 /* Resolve a GENERIC procedure binding for a derived type. */
11561
11562 static bool
11563 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11564 {
11565 gfc_symbol* super_type;
11566
11567 /* Find the overridden binding if any. */
11568 st->n.tb->overridden = NULL;
11569 super_type = gfc_get_derived_super_type (derived);
11570 if (super_type)
11571 {
11572 gfc_symtree* overridden;
11573 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11574 true, NULL);
11575
11576 if (overridden && overridden->n.tb)
11577 st->n.tb->overridden = overridden->n.tb;
11578 }
11579
11580 /* Resolve using worker function. */
11581 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11582 }
11583
11584
11585 /* Retrieve the target-procedure of an operator binding and do some checks in
11586 common for intrinsic and user-defined type-bound operators. */
11587
11588 static gfc_symbol*
11589 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11590 {
11591 gfc_symbol* target_proc;
11592
11593 gcc_assert (target->specific && !target->specific->is_generic);
11594 target_proc = target->specific->u.specific->n.sym;
11595 gcc_assert (target_proc);
11596
11597 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11598 if (target->specific->nopass)
11599 {
11600 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11601 return NULL;
11602 }
11603
11604 return target_proc;
11605 }
11606
11607
11608 /* Resolve a type-bound intrinsic operator. */
11609
11610 static bool
11611 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11612 gfc_typebound_proc* p)
11613 {
11614 gfc_symbol* super_type;
11615 gfc_tbp_generic* target;
11616
11617 /* If there's already an error here, do nothing (but don't fail again). */
11618 if (p->error)
11619 return true;
11620
11621 /* Operators should always be GENERIC bindings. */
11622 gcc_assert (p->is_generic);
11623
11624 /* Look for an overridden binding. */
11625 super_type = gfc_get_derived_super_type (derived);
11626 if (super_type && super_type->f2k_derived)
11627 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11628 op, true, NULL);
11629 else
11630 p->overridden = NULL;
11631
11632 /* Resolve general GENERIC properties using worker function. */
11633 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11634 goto error;
11635
11636 /* Check the targets to be procedures of correct interface. */
11637 for (target = p->u.generic; target; target = target->next)
11638 {
11639 gfc_symbol* target_proc;
11640
11641 target_proc = get_checked_tb_operator_target (target, p->where);
11642 if (!target_proc)
11643 goto error;
11644
11645 if (!gfc_check_operator_interface (target_proc, op, p->where))
11646 goto error;
11647
11648 /* Add target to non-typebound operator list. */
11649 if (!target->specific->deferred && !derived->attr.use_assoc
11650 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11651 {
11652 gfc_interface *head, *intr;
11653 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11654 return false;
11655 head = derived->ns->op[op];
11656 intr = gfc_get_interface ();
11657 intr->sym = target_proc;
11658 intr->where = p->where;
11659 intr->next = head;
11660 derived->ns->op[op] = intr;
11661 }
11662 }
11663
11664 return true;
11665
11666 error:
11667 p->error = 1;
11668 return false;
11669 }
11670
11671
11672 /* Resolve a type-bound user operator (tree-walker callback). */
11673
11674 static gfc_symbol* resolve_bindings_derived;
11675 static bool resolve_bindings_result;
11676
11677 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11678
11679 static void
11680 resolve_typebound_user_op (gfc_symtree* stree)
11681 {
11682 gfc_symbol* super_type;
11683 gfc_tbp_generic* target;
11684
11685 gcc_assert (stree && stree->n.tb);
11686
11687 if (stree->n.tb->error)
11688 return;
11689
11690 /* Operators should always be GENERIC bindings. */
11691 gcc_assert (stree->n.tb->is_generic);
11692
11693 /* Find overridden procedure, if any. */
11694 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11695 if (super_type && super_type->f2k_derived)
11696 {
11697 gfc_symtree* overridden;
11698 overridden = gfc_find_typebound_user_op (super_type, NULL,
11699 stree->name, true, NULL);
11700
11701 if (overridden && overridden->n.tb)
11702 stree->n.tb->overridden = overridden->n.tb;
11703 }
11704 else
11705 stree->n.tb->overridden = NULL;
11706
11707 /* Resolve basically using worker function. */
11708 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11709 goto error;
11710
11711 /* Check the targets to be functions of correct interface. */
11712 for (target = stree->n.tb->u.generic; target; target = target->next)
11713 {
11714 gfc_symbol* target_proc;
11715
11716 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11717 if (!target_proc)
11718 goto error;
11719
11720 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11721 goto error;
11722 }
11723
11724 return;
11725
11726 error:
11727 resolve_bindings_result = false;
11728 stree->n.tb->error = 1;
11729 }
11730
11731
11732 /* Resolve the type-bound procedures for a derived type. */
11733
11734 static void
11735 resolve_typebound_procedure (gfc_symtree* stree)
11736 {
11737 gfc_symbol* proc;
11738 locus where;
11739 gfc_symbol* me_arg;
11740 gfc_symbol* super_type;
11741 gfc_component* comp;
11742
11743 gcc_assert (stree);
11744
11745 /* Undefined specific symbol from GENERIC target definition. */
11746 if (!stree->n.tb)
11747 return;
11748
11749 if (stree->n.tb->error)
11750 return;
11751
11752 /* If this is a GENERIC binding, use that routine. */
11753 if (stree->n.tb->is_generic)
11754 {
11755 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11756 goto error;
11757 return;
11758 }
11759
11760 /* Get the target-procedure to check it. */
11761 gcc_assert (!stree->n.tb->is_generic);
11762 gcc_assert (stree->n.tb->u.specific);
11763 proc = stree->n.tb->u.specific->n.sym;
11764 where = stree->n.tb->where;
11765
11766 /* Default access should already be resolved from the parser. */
11767 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11768
11769 if (stree->n.tb->deferred)
11770 {
11771 if (!check_proc_interface (proc, &where))
11772 goto error;
11773 }
11774 else
11775 {
11776 /* Check for F08:C465. */
11777 if ((!proc->attr.subroutine && !proc->attr.function)
11778 || (proc->attr.proc != PROC_MODULE
11779 && proc->attr.if_source != IFSRC_IFBODY)
11780 || proc->attr.abstract)
11781 {
11782 gfc_error ("'%s' must be a module procedure or an external procedure with"
11783 " an explicit interface at %L", proc->name, &where);
11784 goto error;
11785 }
11786 }
11787
11788 stree->n.tb->subroutine = proc->attr.subroutine;
11789 stree->n.tb->function = proc->attr.function;
11790
11791 /* Find the super-type of the current derived type. We could do this once and
11792 store in a global if speed is needed, but as long as not I believe this is
11793 more readable and clearer. */
11794 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11795
11796 /* If PASS, resolve and check arguments if not already resolved / loaded
11797 from a .mod file. */
11798 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11799 {
11800 gfc_formal_arglist *dummy_args;
11801
11802 dummy_args = gfc_sym_get_dummy_args (proc);
11803 if (stree->n.tb->pass_arg)
11804 {
11805 gfc_formal_arglist *i;
11806
11807 /* If an explicit passing argument name is given, walk the arg-list
11808 and look for it. */
11809
11810 me_arg = NULL;
11811 stree->n.tb->pass_arg_num = 1;
11812 for (i = dummy_args; i; i = i->next)
11813 {
11814 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11815 {
11816 me_arg = i->sym;
11817 break;
11818 }
11819 ++stree->n.tb->pass_arg_num;
11820 }
11821
11822 if (!me_arg)
11823 {
11824 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11825 " argument '%s'",
11826 proc->name, stree->n.tb->pass_arg, &where,
11827 stree->n.tb->pass_arg);
11828 goto error;
11829 }
11830 }
11831 else
11832 {
11833 /* Otherwise, take the first one; there should in fact be at least
11834 one. */
11835 stree->n.tb->pass_arg_num = 1;
11836 if (!dummy_args)
11837 {
11838 gfc_error ("Procedure '%s' with PASS at %L must have at"
11839 " least one argument", proc->name, &where);
11840 goto error;
11841 }
11842 me_arg = dummy_args->sym;
11843 }
11844
11845 /* Now check that the argument-type matches and the passed-object
11846 dummy argument is generally fine. */
11847
11848 gcc_assert (me_arg);
11849
11850 if (me_arg->ts.type != BT_CLASS)
11851 {
11852 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11853 " at %L", proc->name, &where);
11854 goto error;
11855 }
11856
11857 if (CLASS_DATA (me_arg)->ts.u.derived
11858 != resolve_bindings_derived)
11859 {
11860 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11861 " the derived-type '%s'", me_arg->name, proc->name,
11862 me_arg->name, &where, resolve_bindings_derived->name);
11863 goto error;
11864 }
11865
11866 gcc_assert (me_arg->ts.type == BT_CLASS);
11867 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11868 {
11869 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11870 " scalar", proc->name, &where);
11871 goto error;
11872 }
11873 if (CLASS_DATA (me_arg)->attr.allocatable)
11874 {
11875 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11876 " be ALLOCATABLE", proc->name, &where);
11877 goto error;
11878 }
11879 if (CLASS_DATA (me_arg)->attr.class_pointer)
11880 {
11881 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11882 " be POINTER", proc->name, &where);
11883 goto error;
11884 }
11885 }
11886
11887 /* If we are extending some type, check that we don't override a procedure
11888 flagged NON_OVERRIDABLE. */
11889 stree->n.tb->overridden = NULL;
11890 if (super_type)
11891 {
11892 gfc_symtree* overridden;
11893 overridden = gfc_find_typebound_proc (super_type, NULL,
11894 stree->name, true, NULL);
11895
11896 if (overridden)
11897 {
11898 if (overridden->n.tb)
11899 stree->n.tb->overridden = overridden->n.tb;
11900
11901 if (!gfc_check_typebound_override (stree, overridden))
11902 goto error;
11903 }
11904 }
11905
11906 /* See if there's a name collision with a component directly in this type. */
11907 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11908 if (!strcmp (comp->name, stree->name))
11909 {
11910 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11911 " '%s'",
11912 stree->name, &where, resolve_bindings_derived->name);
11913 goto error;
11914 }
11915
11916 /* Try to find a name collision with an inherited component. */
11917 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11918 {
11919 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11920 " component of '%s'",
11921 stree->name, &where, resolve_bindings_derived->name);
11922 goto error;
11923 }
11924
11925 stree->n.tb->error = 0;
11926 return;
11927
11928 error:
11929 resolve_bindings_result = false;
11930 stree->n.tb->error = 1;
11931 }
11932
11933
11934 static bool
11935 resolve_typebound_procedures (gfc_symbol* derived)
11936 {
11937 int op;
11938 gfc_symbol* super_type;
11939
11940 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11941 return true;
11942
11943 super_type = gfc_get_derived_super_type (derived);
11944 if (super_type)
11945 resolve_symbol (super_type);
11946
11947 resolve_bindings_derived = derived;
11948 resolve_bindings_result = true;
11949
11950 if (derived->f2k_derived->tb_sym_root)
11951 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11952 &resolve_typebound_procedure);
11953
11954 if (derived->f2k_derived->tb_uop_root)
11955 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11956 &resolve_typebound_user_op);
11957
11958 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11959 {
11960 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11961 if (p && !resolve_typebound_intrinsic_op (derived,
11962 (gfc_intrinsic_op)op, p))
11963 resolve_bindings_result = false;
11964 }
11965
11966 return resolve_bindings_result;
11967 }
11968
11969
11970 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11971 to give all identical derived types the same backend_decl. */
11972 static void
11973 add_dt_to_dt_list (gfc_symbol *derived)
11974 {
11975 gfc_dt_list *dt_list;
11976
11977 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11978 if (derived == dt_list->derived)
11979 return;
11980
11981 dt_list = gfc_get_dt_list ();
11982 dt_list->next = gfc_derived_types;
11983 dt_list->derived = derived;
11984 gfc_derived_types = dt_list;
11985 }
11986
11987
11988 /* Ensure that a derived-type is really not abstract, meaning that every
11989 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11990
11991 static bool
11992 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11993 {
11994 if (!st)
11995 return true;
11996
11997 if (!ensure_not_abstract_walker (sub, st->left))
11998 return false;
11999 if (!ensure_not_abstract_walker (sub, st->right))
12000 return false;
12001
12002 if (st->n.tb && st->n.tb->deferred)
12003 {
12004 gfc_symtree* overriding;
12005 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12006 if (!overriding)
12007 return false;
12008 gcc_assert (overriding->n.tb);
12009 if (overriding->n.tb->deferred)
12010 {
12011 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
12012 " '%s' is DEFERRED and not overridden",
12013 sub->name, &sub->declared_at, st->name);
12014 return false;
12015 }
12016 }
12017
12018 return true;
12019 }
12020
12021 static bool
12022 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12023 {
12024 /* The algorithm used here is to recursively travel up the ancestry of sub
12025 and for each ancestor-type, check all bindings. If any of them is
12026 DEFERRED, look it up starting from sub and see if the found (overriding)
12027 binding is not DEFERRED.
12028 This is not the most efficient way to do this, but it should be ok and is
12029 clearer than something sophisticated. */
12030
12031 gcc_assert (ancestor && !sub->attr.abstract);
12032
12033 if (!ancestor->attr.abstract)
12034 return true;
12035
12036 /* Walk bindings of this ancestor. */
12037 if (ancestor->f2k_derived)
12038 {
12039 bool t;
12040 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12041 if (!t)
12042 return false;
12043 }
12044
12045 /* Find next ancestor type and recurse on it. */
12046 ancestor = gfc_get_derived_super_type (ancestor);
12047 if (ancestor)
12048 return ensure_not_abstract (sub, ancestor);
12049
12050 return true;
12051 }
12052
12053
12054 /* This check for typebound defined assignments is done recursively
12055 since the order in which derived types are resolved is not always in
12056 order of the declarations. */
12057
12058 static void
12059 check_defined_assignments (gfc_symbol *derived)
12060 {
12061 gfc_component *c;
12062
12063 for (c = derived->components; c; c = c->next)
12064 {
12065 if (c->ts.type != BT_DERIVED
12066 || c->attr.pointer
12067 || c->attr.allocatable
12068 || c->attr.proc_pointer_comp
12069 || c->attr.class_pointer
12070 || c->attr.proc_pointer)
12071 continue;
12072
12073 if (c->ts.u.derived->attr.defined_assign_comp
12074 || (c->ts.u.derived->f2k_derived
12075 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12076 {
12077 derived->attr.defined_assign_comp = 1;
12078 return;
12079 }
12080
12081 check_defined_assignments (c->ts.u.derived);
12082 if (c->ts.u.derived->attr.defined_assign_comp)
12083 {
12084 derived->attr.defined_assign_comp = 1;
12085 return;
12086 }
12087 }
12088 }
12089
12090
12091 /* Resolve the components of a derived type. This does not have to wait until
12092 resolution stage, but can be done as soon as the dt declaration has been
12093 parsed. */
12094
12095 static bool
12096 resolve_fl_derived0 (gfc_symbol *sym)
12097 {
12098 gfc_symbol* super_type;
12099 gfc_component *c;
12100
12101 if (sym->attr.unlimited_polymorphic)
12102 return true;
12103
12104 super_type = gfc_get_derived_super_type (sym);
12105
12106 /* F2008, C432. */
12107 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12108 {
12109 gfc_error ("As extending type '%s' at %L has a coarray component, "
12110 "parent type '%s' shall also have one", sym->name,
12111 &sym->declared_at, super_type->name);
12112 return false;
12113 }
12114
12115 /* Ensure the extended type gets resolved before we do. */
12116 if (super_type && !resolve_fl_derived0 (super_type))
12117 return false;
12118
12119 /* An ABSTRACT type must be extensible. */
12120 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12121 {
12122 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12123 sym->name, &sym->declared_at);
12124 return false;
12125 }
12126
12127 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12128 : sym->components;
12129
12130 for ( ; c != NULL; c = c->next)
12131 {
12132 if (c->attr.artificial)
12133 continue;
12134
12135 /* F2008, C442. */
12136 if ((!sym->attr.is_class || c != sym->components)
12137 && c->attr.codimension
12138 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12139 {
12140 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12141 "deferred shape", c->name, &c->loc);
12142 return false;
12143 }
12144
12145 /* F2008, C443. */
12146 if (c->attr.codimension && c->ts.type == BT_DERIVED
12147 && c->ts.u.derived->ts.is_iso_c)
12148 {
12149 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12150 "shall not be a coarray", c->name, &c->loc);
12151 return false;
12152 }
12153
12154 /* F2008, C444. */
12155 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12156 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12157 || c->attr.allocatable))
12158 {
12159 gfc_error ("Component '%s' at %L with coarray component "
12160 "shall be a nonpointer, nonallocatable scalar",
12161 c->name, &c->loc);
12162 return false;
12163 }
12164
12165 /* F2008, C448. */
12166 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12167 {
12168 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12169 "is not an array pointer", c->name, &c->loc);
12170 return false;
12171 }
12172
12173 if (c->attr.proc_pointer && c->ts.interface)
12174 {
12175 gfc_symbol *ifc = c->ts.interface;
12176
12177 if (!sym->attr.vtype
12178 && !check_proc_interface (ifc, &c->loc))
12179 return false;
12180
12181 if (ifc->attr.if_source || ifc->attr.intrinsic)
12182 {
12183 /* Resolve interface and copy attributes. */
12184 if (ifc->formal && !ifc->formal_ns)
12185 resolve_symbol (ifc);
12186 if (ifc->attr.intrinsic)
12187 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12188
12189 if (ifc->result)
12190 {
12191 c->ts = ifc->result->ts;
12192 c->attr.allocatable = ifc->result->attr.allocatable;
12193 c->attr.pointer = ifc->result->attr.pointer;
12194 c->attr.dimension = ifc->result->attr.dimension;
12195 c->as = gfc_copy_array_spec (ifc->result->as);
12196 c->attr.class_ok = ifc->result->attr.class_ok;
12197 }
12198 else
12199 {
12200 c->ts = ifc->ts;
12201 c->attr.allocatable = ifc->attr.allocatable;
12202 c->attr.pointer = ifc->attr.pointer;
12203 c->attr.dimension = ifc->attr.dimension;
12204 c->as = gfc_copy_array_spec (ifc->as);
12205 c->attr.class_ok = ifc->attr.class_ok;
12206 }
12207 c->ts.interface = ifc;
12208 c->attr.function = ifc->attr.function;
12209 c->attr.subroutine = ifc->attr.subroutine;
12210
12211 c->attr.pure = ifc->attr.pure;
12212 c->attr.elemental = ifc->attr.elemental;
12213 c->attr.recursive = ifc->attr.recursive;
12214 c->attr.always_explicit = ifc->attr.always_explicit;
12215 c->attr.ext_attr |= ifc->attr.ext_attr;
12216 /* Copy char length. */
12217 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12218 {
12219 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12220 if (cl->length && !cl->resolved
12221 && !gfc_resolve_expr (cl->length))
12222 return false;
12223 c->ts.u.cl = cl;
12224 }
12225 }
12226 }
12227 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12228 {
12229 /* Since PPCs are not implicitly typed, a PPC without an explicit
12230 interface must be a subroutine. */
12231 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12232 }
12233
12234 /* Procedure pointer components: Check PASS arg. */
12235 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12236 && !sym->attr.vtype)
12237 {
12238 gfc_symbol* me_arg;
12239
12240 if (c->tb->pass_arg)
12241 {
12242 gfc_formal_arglist* i;
12243
12244 /* If an explicit passing argument name is given, walk the arg-list
12245 and look for it. */
12246
12247 me_arg = NULL;
12248 c->tb->pass_arg_num = 1;
12249 for (i = c->ts.interface->formal; i; i = i->next)
12250 {
12251 if (!strcmp (i->sym->name, c->tb->pass_arg))
12252 {
12253 me_arg = i->sym;
12254 break;
12255 }
12256 c->tb->pass_arg_num++;
12257 }
12258
12259 if (!me_arg)
12260 {
12261 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12262 "at %L has no argument '%s'", c->name,
12263 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12264 c->tb->error = 1;
12265 return false;
12266 }
12267 }
12268 else
12269 {
12270 /* Otherwise, take the first one; there should in fact be at least
12271 one. */
12272 c->tb->pass_arg_num = 1;
12273 if (!c->ts.interface->formal)
12274 {
12275 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12276 "must have at least one argument",
12277 c->name, &c->loc);
12278 c->tb->error = 1;
12279 return false;
12280 }
12281 me_arg = c->ts.interface->formal->sym;
12282 }
12283
12284 /* Now check that the argument-type matches. */
12285 gcc_assert (me_arg);
12286 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12287 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12288 || (me_arg->ts.type == BT_CLASS
12289 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12290 {
12291 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12292 " the derived type '%s'", me_arg->name, c->name,
12293 me_arg->name, &c->loc, sym->name);
12294 c->tb->error = 1;
12295 return false;
12296 }
12297
12298 /* Check for C453. */
12299 if (me_arg->attr.dimension)
12300 {
12301 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12302 "must be scalar", me_arg->name, c->name, me_arg->name,
12303 &c->loc);
12304 c->tb->error = 1;
12305 return false;
12306 }
12307
12308 if (me_arg->attr.pointer)
12309 {
12310 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12311 "may not have the POINTER attribute", me_arg->name,
12312 c->name, me_arg->name, &c->loc);
12313 c->tb->error = 1;
12314 return false;
12315 }
12316
12317 if (me_arg->attr.allocatable)
12318 {
12319 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12320 "may not be ALLOCATABLE", me_arg->name, c->name,
12321 me_arg->name, &c->loc);
12322 c->tb->error = 1;
12323 return false;
12324 }
12325
12326 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12327 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12328 " at %L", c->name, &c->loc);
12329
12330 }
12331
12332 /* Check type-spec if this is not the parent-type component. */
12333 if (((sym->attr.is_class
12334 && (!sym->components->ts.u.derived->attr.extension
12335 || c != sym->components->ts.u.derived->components))
12336 || (!sym->attr.is_class
12337 && (!sym->attr.extension || c != sym->components)))
12338 && !sym->attr.vtype
12339 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12340 return false;
12341
12342 /* If this type is an extension, set the accessibility of the parent
12343 component. */
12344 if (super_type
12345 && ((sym->attr.is_class
12346 && c == sym->components->ts.u.derived->components)
12347 || (!sym->attr.is_class && c == sym->components))
12348 && strcmp (super_type->name, c->name) == 0)
12349 c->attr.access = super_type->attr.access;
12350
12351 /* If this type is an extension, see if this component has the same name
12352 as an inherited type-bound procedure. */
12353 if (super_type && !sym->attr.is_class
12354 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12355 {
12356 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12357 " inherited type-bound procedure",
12358 c->name, sym->name, &c->loc);
12359 return false;
12360 }
12361
12362 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12363 && !c->ts.deferred)
12364 {
12365 if (c->ts.u.cl->length == NULL
12366 || (!resolve_charlen(c->ts.u.cl))
12367 || !gfc_is_constant_expr (c->ts.u.cl->length))
12368 {
12369 gfc_error ("Character length of component '%s' needs to "
12370 "be a constant specification expression at %L",
12371 c->name,
12372 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12373 return false;
12374 }
12375 }
12376
12377 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12378 && !c->attr.pointer && !c->attr.allocatable)
12379 {
12380 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12381 "length must be a POINTER or ALLOCATABLE",
12382 c->name, sym->name, &c->loc);
12383 return false;
12384 }
12385
12386 /* Add the hidden deferred length field. */
12387 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
12388 && !sym->attr.is_class)
12389 {
12390 char name[GFC_MAX_SYMBOL_LEN+9];
12391 gfc_component *strlen;
12392 sprintf (name, "_%s_length", c->name);
12393 strlen = gfc_find_component (sym, name, true, true);
12394 if (strlen == NULL)
12395 {
12396 if (!gfc_add_component (sym, name, &strlen))
12397 return false;
12398 strlen->ts.type = BT_INTEGER;
12399 strlen->ts.kind = gfc_charlen_int_kind;
12400 strlen->attr.access = ACCESS_PRIVATE;
12401 strlen->attr.deferred_parameter = 1;
12402 }
12403 }
12404
12405 if (c->ts.type == BT_DERIVED
12406 && sym->component_access != ACCESS_PRIVATE
12407 && gfc_check_symbol_access (sym)
12408 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12409 && !c->ts.u.derived->attr.use_assoc
12410 && !gfc_check_symbol_access (c->ts.u.derived)
12411 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12412 "PRIVATE type and cannot be a component of "
12413 "'%s', which is PUBLIC at %L", c->name,
12414 sym->name, &sym->declared_at))
12415 return false;
12416
12417 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12418 {
12419 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12420 "type %s", c->name, &c->loc, sym->name);
12421 return false;
12422 }
12423
12424 if (sym->attr.sequence)
12425 {
12426 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12427 {
12428 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12429 "not have the SEQUENCE attribute",
12430 c->ts.u.derived->name, &sym->declared_at);
12431 return false;
12432 }
12433 }
12434
12435 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12436 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12437 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12438 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12439 CLASS_DATA (c)->ts.u.derived
12440 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12441
12442 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12443 && c->attr.pointer && c->ts.u.derived->components == NULL
12444 && !c->ts.u.derived->attr.zero_comp)
12445 {
12446 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12447 "that has not been declared", c->name, sym->name,
12448 &c->loc);
12449 return false;
12450 }
12451
12452 if (c->ts.type == BT_CLASS && c->attr.class_ok
12453 && CLASS_DATA (c)->attr.class_pointer
12454 && CLASS_DATA (c)->ts.u.derived->components == NULL
12455 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12456 && !UNLIMITED_POLY (c))
12457 {
12458 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12459 "that has not been declared", c->name, sym->name,
12460 &c->loc);
12461 return false;
12462 }
12463
12464 /* C437. */
12465 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12466 && (!c->attr.class_ok
12467 || !(CLASS_DATA (c)->attr.class_pointer
12468 || CLASS_DATA (c)->attr.allocatable)))
12469 {
12470 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12471 "or pointer", c->name, &c->loc);
12472 /* Prevent a recurrence of the error. */
12473 c->ts.type = BT_UNKNOWN;
12474 return false;
12475 }
12476
12477 /* Ensure that all the derived type components are put on the
12478 derived type list; even in formal namespaces, where derived type
12479 pointer components might not have been declared. */
12480 if (c->ts.type == BT_DERIVED
12481 && c->ts.u.derived
12482 && c->ts.u.derived->components
12483 && c->attr.pointer
12484 && sym != c->ts.u.derived)
12485 add_dt_to_dt_list (c->ts.u.derived);
12486
12487 if (!gfc_resolve_array_spec (c->as,
12488 !(c->attr.pointer || c->attr.proc_pointer
12489 || c->attr.allocatable)))
12490 return false;
12491
12492 if (c->initializer && !sym->attr.vtype
12493 && !gfc_check_assign_symbol (sym, c, c->initializer))
12494 return false;
12495 }
12496
12497 check_defined_assignments (sym);
12498
12499 if (!sym->attr.defined_assign_comp && super_type)
12500 sym->attr.defined_assign_comp
12501 = super_type->attr.defined_assign_comp;
12502
12503 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12504 all DEFERRED bindings are overridden. */
12505 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12506 && !sym->attr.is_class
12507 && !ensure_not_abstract (sym, super_type))
12508 return false;
12509
12510 /* Add derived type to the derived type list. */
12511 add_dt_to_dt_list (sym);
12512
12513 return true;
12514 }
12515
12516
12517 /* The following procedure does the full resolution of a derived type,
12518 including resolution of all type-bound procedures (if present). In contrast
12519 to 'resolve_fl_derived0' this can only be done after the module has been
12520 parsed completely. */
12521
12522 static bool
12523 resolve_fl_derived (gfc_symbol *sym)
12524 {
12525 gfc_symbol *gen_dt = NULL;
12526
12527 if (sym->attr.unlimited_polymorphic)
12528 return true;
12529
12530 if (!sym->attr.is_class)
12531 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12532 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12533 && (!gen_dt->generic->sym->attr.use_assoc
12534 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12535 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12536 "'%s' at %L being the same name as derived "
12537 "type at %L", sym->name,
12538 gen_dt->generic->sym == sym
12539 ? gen_dt->generic->next->sym->name
12540 : gen_dt->generic->sym->name,
12541 gen_dt->generic->sym == sym
12542 ? &gen_dt->generic->next->sym->declared_at
12543 : &gen_dt->generic->sym->declared_at,
12544 &sym->declared_at))
12545 return false;
12546
12547 /* Resolve the finalizer procedures. */
12548 if (!gfc_resolve_finalizers (sym, NULL))
12549 return false;
12550
12551 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12552 {
12553 /* Fix up incomplete CLASS symbols. */
12554 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12555 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12556
12557 /* Nothing more to do for unlimited polymorphic entities. */
12558 if (data->ts.u.derived->attr.unlimited_polymorphic)
12559 return true;
12560 else if (vptr->ts.u.derived == NULL)
12561 {
12562 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12563 gcc_assert (vtab);
12564 vptr->ts.u.derived = vtab->ts.u.derived;
12565 }
12566 }
12567
12568 if (!resolve_fl_derived0 (sym))
12569 return false;
12570
12571 /* Resolve the type-bound procedures. */
12572 if (!resolve_typebound_procedures (sym))
12573 return false;
12574
12575 return true;
12576 }
12577
12578
12579 static bool
12580 resolve_fl_namelist (gfc_symbol *sym)
12581 {
12582 gfc_namelist *nl;
12583 gfc_symbol *nlsym;
12584
12585 for (nl = sym->namelist; nl; nl = nl->next)
12586 {
12587 /* Check again, the check in match only works if NAMELIST comes
12588 after the decl. */
12589 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12590 {
12591 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12592 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12593 return false;
12594 }
12595
12596 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12597 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12598 "with assumed shape in namelist '%s' at %L",
12599 nl->sym->name, sym->name, &sym->declared_at))
12600 return false;
12601
12602 if (is_non_constant_shape_array (nl->sym)
12603 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12604 "with nonconstant shape in namelist '%s' at %L",
12605 nl->sym->name, sym->name, &sym->declared_at))
12606 return false;
12607
12608 if (nl->sym->ts.type == BT_CHARACTER
12609 && (nl->sym->ts.u.cl->length == NULL
12610 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12611 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12612 "nonconstant character length in "
12613 "namelist '%s' at %L", nl->sym->name,
12614 sym->name, &sym->declared_at))
12615 return false;
12616
12617 /* FIXME: Once UDDTIO is implemented, the following can be
12618 removed. */
12619 if (nl->sym->ts.type == BT_CLASS)
12620 {
12621 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12622 "polymorphic and requires a defined input/output "
12623 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12624 return false;
12625 }
12626
12627 if (nl->sym->ts.type == BT_DERIVED
12628 && (nl->sym->ts.u.derived->attr.alloc_comp
12629 || nl->sym->ts.u.derived->attr.pointer_comp))
12630 {
12631 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12632 "namelist '%s' at %L with ALLOCATABLE "
12633 "or POINTER components", nl->sym->name,
12634 sym->name, &sym->declared_at))
12635 return false;
12636
12637 /* FIXME: Once UDDTIO is implemented, the following can be
12638 removed. */
12639 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12640 "ALLOCATABLE or POINTER components and thus requires "
12641 "a defined input/output procedure", nl->sym->name,
12642 sym->name, &sym->declared_at);
12643 return false;
12644 }
12645 }
12646
12647 /* Reject PRIVATE objects in a PUBLIC namelist. */
12648 if (gfc_check_symbol_access (sym))
12649 {
12650 for (nl = sym->namelist; nl; nl = nl->next)
12651 {
12652 if (!nl->sym->attr.use_assoc
12653 && !is_sym_host_assoc (nl->sym, sym->ns)
12654 && !gfc_check_symbol_access (nl->sym))
12655 {
12656 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12657 "cannot be member of PUBLIC namelist '%s' at %L",
12658 nl->sym->name, sym->name, &sym->declared_at);
12659 return false;
12660 }
12661
12662 /* Types with private components that came here by USE-association. */
12663 if (nl->sym->ts.type == BT_DERIVED
12664 && derived_inaccessible (nl->sym->ts.u.derived))
12665 {
12666 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12667 "components and cannot be member of namelist '%s' at %L",
12668 nl->sym->name, sym->name, &sym->declared_at);
12669 return false;
12670 }
12671
12672 /* Types with private components that are defined in the same module. */
12673 if (nl->sym->ts.type == BT_DERIVED
12674 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12675 && nl->sym->ts.u.derived->attr.private_comp)
12676 {
12677 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12678 "cannot be a member of PUBLIC namelist '%s' at %L",
12679 nl->sym->name, sym->name, &sym->declared_at);
12680 return false;
12681 }
12682 }
12683 }
12684
12685
12686 /* 14.1.2 A module or internal procedure represent local entities
12687 of the same type as a namelist member and so are not allowed. */
12688 for (nl = sym->namelist; nl; nl = nl->next)
12689 {
12690 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12691 continue;
12692
12693 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12694 if ((nl->sym == sym->ns->proc_name)
12695 ||
12696 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12697 continue;
12698
12699 nlsym = NULL;
12700 if (nl->sym->name)
12701 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12702 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12703 {
12704 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12705 "attribute in '%s' at %L", nlsym->name,
12706 &sym->declared_at);
12707 return false;
12708 }
12709 }
12710
12711 return true;
12712 }
12713
12714
12715 static bool
12716 resolve_fl_parameter (gfc_symbol *sym)
12717 {
12718 /* A parameter array's shape needs to be constant. */
12719 if (sym->as != NULL
12720 && (sym->as->type == AS_DEFERRED
12721 || is_non_constant_shape_array (sym)))
12722 {
12723 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12724 "or of deferred shape", sym->name, &sym->declared_at);
12725 return false;
12726 }
12727
12728 /* Make sure a parameter that has been implicitly typed still
12729 matches the implicit type, since PARAMETER statements can precede
12730 IMPLICIT statements. */
12731 if (sym->attr.implicit_type
12732 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12733 sym->ns)))
12734 {
12735 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12736 "later IMPLICIT type", sym->name, &sym->declared_at);
12737 return false;
12738 }
12739
12740 /* Make sure the types of derived parameters are consistent. This
12741 type checking is deferred until resolution because the type may
12742 refer to a derived type from the host. */
12743 if (sym->ts.type == BT_DERIVED
12744 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12745 {
12746 gfc_error ("Incompatible derived type in PARAMETER at %L",
12747 &sym->value->where);
12748 return false;
12749 }
12750 return true;
12751 }
12752
12753
12754 /* Do anything necessary to resolve a symbol. Right now, we just
12755 assume that an otherwise unknown symbol is a variable. This sort
12756 of thing commonly happens for symbols in module. */
12757
12758 static void
12759 resolve_symbol (gfc_symbol *sym)
12760 {
12761 int check_constant, mp_flag;
12762 gfc_symtree *symtree;
12763 gfc_symtree *this_symtree;
12764 gfc_namespace *ns;
12765 gfc_component *c;
12766 symbol_attribute class_attr;
12767 gfc_array_spec *as;
12768 bool saved_specification_expr;
12769
12770 if (sym->resolved)
12771 return;
12772 sym->resolved = 1;
12773
12774 if (sym->attr.artificial)
12775 return;
12776
12777 if (sym->attr.unlimited_polymorphic)
12778 return;
12779
12780 if (sym->attr.flavor == FL_UNKNOWN
12781 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12782 && !sym->attr.generic && !sym->attr.external
12783 && sym->attr.if_source == IFSRC_UNKNOWN
12784 && sym->ts.type == BT_UNKNOWN))
12785 {
12786
12787 /* If we find that a flavorless symbol is an interface in one of the
12788 parent namespaces, find its symtree in this namespace, free the
12789 symbol and set the symtree to point to the interface symbol. */
12790 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12791 {
12792 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12793 if (symtree && (symtree->n.sym->generic ||
12794 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12795 && sym->ns->construct_entities)))
12796 {
12797 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12798 sym->name);
12799 gfc_release_symbol (sym);
12800 symtree->n.sym->refs++;
12801 this_symtree->n.sym = symtree->n.sym;
12802 return;
12803 }
12804 }
12805
12806 /* Otherwise give it a flavor according to such attributes as
12807 it has. */
12808 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12809 && sym->attr.intrinsic == 0)
12810 sym->attr.flavor = FL_VARIABLE;
12811 else if (sym->attr.flavor == FL_UNKNOWN)
12812 {
12813 sym->attr.flavor = FL_PROCEDURE;
12814 if (sym->attr.dimension)
12815 sym->attr.function = 1;
12816 }
12817 }
12818
12819 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12820 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12821
12822 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12823 && !resolve_procedure_interface (sym))
12824 return;
12825
12826 if (sym->attr.is_protected && !sym->attr.proc_pointer
12827 && (sym->attr.procedure || sym->attr.external))
12828 {
12829 if (sym->attr.external)
12830 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12831 "at %L", &sym->declared_at);
12832 else
12833 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12834 "at %L", &sym->declared_at);
12835
12836 return;
12837 }
12838
12839 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12840 return;
12841
12842 /* Symbols that are module procedures with results (functions) have
12843 the types and array specification copied for type checking in
12844 procedures that call them, as well as for saving to a module
12845 file. These symbols can't stand the scrutiny that their results
12846 can. */
12847 mp_flag = (sym->result != NULL && sym->result != sym);
12848
12849 /* Make sure that the intrinsic is consistent with its internal
12850 representation. This needs to be done before assigning a default
12851 type to avoid spurious warnings. */
12852 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12853 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12854 return;
12855
12856 /* Resolve associate names. */
12857 if (sym->assoc)
12858 resolve_assoc_var (sym, true);
12859
12860 /* Assign default type to symbols that need one and don't have one. */
12861 if (sym->ts.type == BT_UNKNOWN)
12862 {
12863 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12864 {
12865 gfc_set_default_type (sym, 1, NULL);
12866 }
12867
12868 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12869 && !sym->attr.function && !sym->attr.subroutine
12870 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12871 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12872
12873 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12874 {
12875 /* The specific case of an external procedure should emit an error
12876 in the case that there is no implicit type. */
12877 if (!mp_flag)
12878 gfc_set_default_type (sym, sym->attr.external, NULL);
12879 else
12880 {
12881 /* Result may be in another namespace. */
12882 resolve_symbol (sym->result);
12883
12884 if (!sym->result->attr.proc_pointer)
12885 {
12886 sym->ts = sym->result->ts;
12887 sym->as = gfc_copy_array_spec (sym->result->as);
12888 sym->attr.dimension = sym->result->attr.dimension;
12889 sym->attr.pointer = sym->result->attr.pointer;
12890 sym->attr.allocatable = sym->result->attr.allocatable;
12891 sym->attr.contiguous = sym->result->attr.contiguous;
12892 }
12893 }
12894 }
12895 }
12896 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12897 {
12898 bool saved_specification_expr = specification_expr;
12899 specification_expr = true;
12900 gfc_resolve_array_spec (sym->result->as, false);
12901 specification_expr = saved_specification_expr;
12902 }
12903
12904 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12905 {
12906 as = CLASS_DATA (sym)->as;
12907 class_attr = CLASS_DATA (sym)->attr;
12908 class_attr.pointer = class_attr.class_pointer;
12909 }
12910 else
12911 {
12912 class_attr = sym->attr;
12913 as = sym->as;
12914 }
12915
12916 /* F2008, C530. */
12917 if (sym->attr.contiguous
12918 && (!class_attr.dimension
12919 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12920 && !class_attr.pointer)))
12921 {
12922 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12923 "array pointer or an assumed-shape or assumed-rank array",
12924 sym->name, &sym->declared_at);
12925 return;
12926 }
12927
12928 /* Assumed size arrays and assumed shape arrays must be dummy
12929 arguments. Array-spec's of implied-shape should have been resolved to
12930 AS_EXPLICIT already. */
12931
12932 if (as)
12933 {
12934 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12935 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12936 || as->type == AS_ASSUMED_SHAPE)
12937 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12938 {
12939 if (as->type == AS_ASSUMED_SIZE)
12940 gfc_error ("Assumed size array at %L must be a dummy argument",
12941 &sym->declared_at);
12942 else
12943 gfc_error ("Assumed shape array at %L must be a dummy argument",
12944 &sym->declared_at);
12945 return;
12946 }
12947 /* TS 29113, C535a. */
12948 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12949 && !sym->attr.select_type_temporary)
12950 {
12951 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12952 &sym->declared_at);
12953 return;
12954 }
12955 if (as->type == AS_ASSUMED_RANK
12956 && (sym->attr.codimension || sym->attr.value))
12957 {
12958 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12959 "CODIMENSION attribute", &sym->declared_at);
12960 return;
12961 }
12962 }
12963
12964 /* Make sure symbols with known intent or optional are really dummy
12965 variable. Because of ENTRY statement, this has to be deferred
12966 until resolution time. */
12967
12968 if (!sym->attr.dummy
12969 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12970 {
12971 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12972 return;
12973 }
12974
12975 if (sym->attr.value && !sym->attr.dummy)
12976 {
12977 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12978 "it is not a dummy argument", sym->name, &sym->declared_at);
12979 return;
12980 }
12981
12982 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12983 {
12984 gfc_charlen *cl = sym->ts.u.cl;
12985 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12986 {
12987 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12988 "attribute must have constant length",
12989 sym->name, &sym->declared_at);
12990 return;
12991 }
12992
12993 if (sym->ts.is_c_interop
12994 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12995 {
12996 gfc_error ("C interoperable character dummy variable '%s' at %L "
12997 "with VALUE attribute must have length one",
12998 sym->name, &sym->declared_at);
12999 return;
13000 }
13001 }
13002
13003 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13004 && sym->ts.u.derived->attr.generic)
13005 {
13006 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13007 if (!sym->ts.u.derived)
13008 {
13009 gfc_error ("The derived type '%s' at %L is of type '%s', "
13010 "which has not been defined", sym->name,
13011 &sym->declared_at, sym->ts.u.derived->name);
13012 sym->ts.type = BT_UNKNOWN;
13013 return;
13014 }
13015 }
13016
13017 /* Use the same constraints as TYPE(*), except for the type check
13018 and that only scalars and assumed-size arrays are permitted. */
13019 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13020 {
13021 if (!sym->attr.dummy)
13022 {
13023 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13024 "a dummy argument", sym->name, &sym->declared_at);
13025 return;
13026 }
13027
13028 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13029 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13030 && sym->ts.type != BT_COMPLEX)
13031 {
13032 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13033 "of type TYPE(*) or of an numeric intrinsic type",
13034 sym->name, &sym->declared_at);
13035 return;
13036 }
13037
13038 if (sym->attr.allocatable || sym->attr.codimension
13039 || sym->attr.pointer || sym->attr.value)
13040 {
13041 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13042 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13043 "attribute", sym->name, &sym->declared_at);
13044 return;
13045 }
13046
13047 if (sym->attr.intent == INTENT_OUT)
13048 {
13049 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13050 "have the INTENT(OUT) attribute",
13051 sym->name, &sym->declared_at);
13052 return;
13053 }
13054 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13055 {
13056 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13057 "either be a scalar or an assumed-size array",
13058 sym->name, &sym->declared_at);
13059 return;
13060 }
13061
13062 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13063 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13064 packing. */
13065 sym->ts.type = BT_ASSUMED;
13066 sym->as = gfc_get_array_spec ();
13067 sym->as->type = AS_ASSUMED_SIZE;
13068 sym->as->rank = 1;
13069 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13070 }
13071 else if (sym->ts.type == BT_ASSUMED)
13072 {
13073 /* TS 29113, C407a. */
13074 if (!sym->attr.dummy)
13075 {
13076 gfc_error ("Assumed type of variable %s at %L is only permitted "
13077 "for dummy variables", sym->name, &sym->declared_at);
13078 return;
13079 }
13080 if (sym->attr.allocatable || sym->attr.codimension
13081 || sym->attr.pointer || sym->attr.value)
13082 {
13083 gfc_error ("Assumed-type variable %s at %L may not have the "
13084 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13085 sym->name, &sym->declared_at);
13086 return;
13087 }
13088 if (sym->attr.intent == INTENT_OUT)
13089 {
13090 gfc_error ("Assumed-type variable %s at %L may not have the "
13091 "INTENT(OUT) attribute",
13092 sym->name, &sym->declared_at);
13093 return;
13094 }
13095 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13096 {
13097 gfc_error ("Assumed-type variable %s at %L shall not be an "
13098 "explicit-shape array", sym->name, &sym->declared_at);
13099 return;
13100 }
13101 }
13102
13103 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13104 do this for something that was implicitly typed because that is handled
13105 in gfc_set_default_type. Handle dummy arguments and procedure
13106 definitions separately. Also, anything that is use associated is not
13107 handled here but instead is handled in the module it is declared in.
13108 Finally, derived type definitions are allowed to be BIND(C) since that
13109 only implies that they're interoperable, and they are checked fully for
13110 interoperability when a variable is declared of that type. */
13111 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13112 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13113 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13114 {
13115 bool t = true;
13116
13117 /* First, make sure the variable is declared at the
13118 module-level scope (J3/04-007, Section 15.3). */
13119 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13120 sym->attr.in_common == 0)
13121 {
13122 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13123 "is neither a COMMON block nor declared at the "
13124 "module level scope", sym->name, &(sym->declared_at));
13125 t = false;
13126 }
13127 else if (sym->common_head != NULL)
13128 {
13129 t = verify_com_block_vars_c_interop (sym->common_head);
13130 }
13131 else
13132 {
13133 /* If type() declaration, we need to verify that the components
13134 of the given type are all C interoperable, etc. */
13135 if (sym->ts.type == BT_DERIVED &&
13136 sym->ts.u.derived->attr.is_c_interop != 1)
13137 {
13138 /* Make sure the user marked the derived type as BIND(C). If
13139 not, call the verify routine. This could print an error
13140 for the derived type more than once if multiple variables
13141 of that type are declared. */
13142 if (sym->ts.u.derived->attr.is_bind_c != 1)
13143 verify_bind_c_derived_type (sym->ts.u.derived);
13144 t = false;
13145 }
13146
13147 /* Verify the variable itself as C interoperable if it
13148 is BIND(C). It is not possible for this to succeed if
13149 the verify_bind_c_derived_type failed, so don't have to handle
13150 any error returned by verify_bind_c_derived_type. */
13151 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13152 sym->common_block);
13153 }
13154
13155 if (!t)
13156 {
13157 /* clear the is_bind_c flag to prevent reporting errors more than
13158 once if something failed. */
13159 sym->attr.is_bind_c = 0;
13160 return;
13161 }
13162 }
13163
13164 /* If a derived type symbol has reached this point, without its
13165 type being declared, we have an error. Notice that most
13166 conditions that produce undefined derived types have already
13167 been dealt with. However, the likes of:
13168 implicit type(t) (t) ..... call foo (t) will get us here if
13169 the type is not declared in the scope of the implicit
13170 statement. Change the type to BT_UNKNOWN, both because it is so
13171 and to prevent an ICE. */
13172 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13173 && sym->ts.u.derived->components == NULL
13174 && !sym->ts.u.derived->attr.zero_comp)
13175 {
13176 gfc_error ("The derived type '%s' at %L is of type '%s', "
13177 "which has not been defined", sym->name,
13178 &sym->declared_at, sym->ts.u.derived->name);
13179 sym->ts.type = BT_UNKNOWN;
13180 return;
13181 }
13182
13183 /* Make sure that the derived type has been resolved and that the
13184 derived type is visible in the symbol's namespace, if it is a
13185 module function and is not PRIVATE. */
13186 if (sym->ts.type == BT_DERIVED
13187 && sym->ts.u.derived->attr.use_assoc
13188 && sym->ns->proc_name
13189 && sym->ns->proc_name->attr.flavor == FL_MODULE
13190 && !resolve_fl_derived (sym->ts.u.derived))
13191 return;
13192
13193 /* Unless the derived-type declaration is use associated, Fortran 95
13194 does not allow public entries of private derived types.
13195 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13196 161 in 95-006r3. */
13197 if (sym->ts.type == BT_DERIVED
13198 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13199 && !sym->ts.u.derived->attr.use_assoc
13200 && gfc_check_symbol_access (sym)
13201 && !gfc_check_symbol_access (sym->ts.u.derived)
13202 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13203 "derived type '%s'",
13204 (sym->attr.flavor == FL_PARAMETER)
13205 ? "parameter" : "variable",
13206 sym->name, &sym->declared_at,
13207 sym->ts.u.derived->name))
13208 return;
13209
13210 /* F2008, C1302. */
13211 if (sym->ts.type == BT_DERIVED
13212 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13213 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13214 || sym->ts.u.derived->attr.lock_comp)
13215 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13216 {
13217 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13218 "type LOCK_TYPE must be a coarray", sym->name,
13219 &sym->declared_at);
13220 return;
13221 }
13222
13223 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13224 default initialization is defined (5.1.2.4.4). */
13225 if (sym->ts.type == BT_DERIVED
13226 && sym->attr.dummy
13227 && sym->attr.intent == INTENT_OUT
13228 && sym->as
13229 && sym->as->type == AS_ASSUMED_SIZE)
13230 {
13231 for (c = sym->ts.u.derived->components; c; c = c->next)
13232 {
13233 if (c->initializer)
13234 {
13235 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13236 "ASSUMED SIZE and so cannot have a default initializer",
13237 sym->name, &sym->declared_at);
13238 return;
13239 }
13240 }
13241 }
13242
13243 /* F2008, C542. */
13244 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13245 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13246 {
13247 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13248 "INTENT(OUT)", sym->name, &sym->declared_at);
13249 return;
13250 }
13251
13252 /* F2008, C525. */
13253 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13254 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13255 && CLASS_DATA (sym)->attr.coarray_comp))
13256 || class_attr.codimension)
13257 && (sym->attr.result || sym->result == sym))
13258 {
13259 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13260 "a coarray component", sym->name, &sym->declared_at);
13261 return;
13262 }
13263
13264 /* F2008, C524. */
13265 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13266 && sym->ts.u.derived->ts.is_iso_c)
13267 {
13268 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13269 "shall not be a coarray", sym->name, &sym->declared_at);
13270 return;
13271 }
13272
13273 /* F2008, C525. */
13274 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13275 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13276 && CLASS_DATA (sym)->attr.coarray_comp))
13277 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13278 || class_attr.allocatable))
13279 {
13280 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13281 "nonpointer, nonallocatable scalar, which is not a coarray",
13282 sym->name, &sym->declared_at);
13283 return;
13284 }
13285
13286 /* F2008, C526. The function-result case was handled above. */
13287 if (class_attr.codimension
13288 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13289 || sym->attr.select_type_temporary
13290 || sym->ns->save_all
13291 || sym->ns->proc_name->attr.flavor == FL_MODULE
13292 || sym->ns->proc_name->attr.is_main_program
13293 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13294 {
13295 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13296 "nor a dummy argument", sym->name, &sym->declared_at);
13297 return;
13298 }
13299 /* F2008, C528. */
13300 else if (class_attr.codimension && !sym->attr.select_type_temporary
13301 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13302 {
13303 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13304 "deferred shape", sym->name, &sym->declared_at);
13305 return;
13306 }
13307 else if (class_attr.codimension && class_attr.allocatable && as
13308 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13309 {
13310 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13311 "deferred shape", sym->name, &sym->declared_at);
13312 return;
13313 }
13314
13315 /* F2008, C541. */
13316 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13317 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13318 && CLASS_DATA (sym)->attr.coarray_comp))
13319 || (class_attr.codimension && class_attr.allocatable))
13320 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13321 {
13322 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13323 "allocatable coarray or have coarray components",
13324 sym->name, &sym->declared_at);
13325 return;
13326 }
13327
13328 if (class_attr.codimension && sym->attr.dummy
13329 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13330 {
13331 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13332 "procedure '%s'", sym->name, &sym->declared_at,
13333 sym->ns->proc_name->name);
13334 return;
13335 }
13336
13337 if (sym->ts.type == BT_LOGICAL
13338 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13339 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13340 && sym->ns->proc_name->attr.is_bind_c)))
13341 {
13342 int i;
13343 for (i = 0; gfc_logical_kinds[i].kind; i++)
13344 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13345 break;
13346 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13347 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13348 "%L with non-C_Bool kind in BIND(C) procedure "
13349 "'%s'", sym->name, &sym->declared_at,
13350 sym->ns->proc_name->name))
13351 return;
13352 else if (!gfc_logical_kinds[i].c_bool
13353 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13354 "'%s' at %L with non-C_Bool kind in "
13355 "BIND(C) procedure '%s'", sym->name,
13356 &sym->declared_at,
13357 sym->attr.function ? sym->name
13358 : sym->ns->proc_name->name))
13359 return;
13360 }
13361
13362 switch (sym->attr.flavor)
13363 {
13364 case FL_VARIABLE:
13365 if (!resolve_fl_variable (sym, mp_flag))
13366 return;
13367 break;
13368
13369 case FL_PROCEDURE:
13370 if (!resolve_fl_procedure (sym, mp_flag))
13371 return;
13372 break;
13373
13374 case FL_NAMELIST:
13375 if (!resolve_fl_namelist (sym))
13376 return;
13377 break;
13378
13379 case FL_PARAMETER:
13380 if (!resolve_fl_parameter (sym))
13381 return;
13382 break;
13383
13384 default:
13385 break;
13386 }
13387
13388 /* Resolve array specifier. Check as well some constraints
13389 on COMMON blocks. */
13390
13391 check_constant = sym->attr.in_common && !sym->attr.pointer;
13392
13393 /* Set the formal_arg_flag so that check_conflict will not throw
13394 an error for host associated variables in the specification
13395 expression for an array_valued function. */
13396 if (sym->attr.function && sym->as)
13397 formal_arg_flag = 1;
13398
13399 saved_specification_expr = specification_expr;
13400 specification_expr = true;
13401 gfc_resolve_array_spec (sym->as, check_constant);
13402 specification_expr = saved_specification_expr;
13403
13404 formal_arg_flag = 0;
13405
13406 /* Resolve formal namespaces. */
13407 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13408 && !sym->attr.contained && !sym->attr.intrinsic)
13409 gfc_resolve (sym->formal_ns);
13410
13411 /* Make sure the formal namespace is present. */
13412 if (sym->formal && !sym->formal_ns)
13413 {
13414 gfc_formal_arglist *formal = sym->formal;
13415 while (formal && !formal->sym)
13416 formal = formal->next;
13417
13418 if (formal)
13419 {
13420 sym->formal_ns = formal->sym->ns;
13421 if (sym->ns != formal->sym->ns)
13422 sym->formal_ns->refs++;
13423 }
13424 }
13425
13426 /* Check threadprivate restrictions. */
13427 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13428 && (!sym->attr.in_common
13429 && sym->module == NULL
13430 && (sym->ns->proc_name == NULL
13431 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13432 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13433
13434 /* If we have come this far we can apply default-initializers, as
13435 described in 14.7.5, to those variables that have not already
13436 been assigned one. */
13437 if (sym->ts.type == BT_DERIVED
13438 && !sym->value
13439 && !sym->attr.allocatable
13440 && !sym->attr.alloc_comp)
13441 {
13442 symbol_attribute *a = &sym->attr;
13443
13444 if ((!a->save && !a->dummy && !a->pointer
13445 && !a->in_common && !a->use_assoc
13446 && (a->referenced || a->result)
13447 && !(a->function && sym != sym->result))
13448 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13449 apply_default_init (sym);
13450 }
13451
13452 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13453 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13454 && !CLASS_DATA (sym)->attr.class_pointer
13455 && !CLASS_DATA (sym)->attr.allocatable)
13456 apply_default_init (sym);
13457
13458 /* If this symbol has a type-spec, check it. */
13459 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13460 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13461 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13462 return;
13463 }
13464
13465
13466 /************* Resolve DATA statements *************/
13467
13468 static struct
13469 {
13470 gfc_data_value *vnode;
13471 mpz_t left;
13472 }
13473 values;
13474
13475
13476 /* Advance the values structure to point to the next value in the data list. */
13477
13478 static bool
13479 next_data_value (void)
13480 {
13481 while (mpz_cmp_ui (values.left, 0) == 0)
13482 {
13483
13484 if (values.vnode->next == NULL)
13485 return false;
13486
13487 values.vnode = values.vnode->next;
13488 mpz_set (values.left, values.vnode->repeat);
13489 }
13490
13491 return true;
13492 }
13493
13494
13495 static bool
13496 check_data_variable (gfc_data_variable *var, locus *where)
13497 {
13498 gfc_expr *e;
13499 mpz_t size;
13500 mpz_t offset;
13501 bool t;
13502 ar_type mark = AR_UNKNOWN;
13503 int i;
13504 mpz_t section_index[GFC_MAX_DIMENSIONS];
13505 gfc_ref *ref;
13506 gfc_array_ref *ar;
13507 gfc_symbol *sym;
13508 int has_pointer;
13509
13510 if (!gfc_resolve_expr (var->expr))
13511 return false;
13512
13513 ar = NULL;
13514 mpz_init_set_si (offset, 0);
13515 e = var->expr;
13516
13517 if (e->expr_type != EXPR_VARIABLE)
13518 gfc_internal_error ("check_data_variable(): Bad expression");
13519
13520 sym = e->symtree->n.sym;
13521
13522 if (sym->ns->is_block_data && !sym->attr.in_common)
13523 {
13524 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13525 sym->name, &sym->declared_at);
13526 }
13527
13528 if (e->ref == NULL && sym->as)
13529 {
13530 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13531 " declaration", sym->name, where);
13532 return false;
13533 }
13534
13535 has_pointer = sym->attr.pointer;
13536
13537 if (gfc_is_coindexed (e))
13538 {
13539 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13540 where);
13541 return false;
13542 }
13543
13544 for (ref = e->ref; ref; ref = ref->next)
13545 {
13546 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13547 has_pointer = 1;
13548
13549 if (has_pointer
13550 && ref->type == REF_ARRAY
13551 && ref->u.ar.type != AR_FULL)
13552 {
13553 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13554 "be a full array", sym->name, where);
13555 return false;
13556 }
13557 }
13558
13559 if (e->rank == 0 || has_pointer)
13560 {
13561 mpz_init_set_ui (size, 1);
13562 ref = NULL;
13563 }
13564 else
13565 {
13566 ref = e->ref;
13567
13568 /* Find the array section reference. */
13569 for (ref = e->ref; ref; ref = ref->next)
13570 {
13571 if (ref->type != REF_ARRAY)
13572 continue;
13573 if (ref->u.ar.type == AR_ELEMENT)
13574 continue;
13575 break;
13576 }
13577 gcc_assert (ref);
13578
13579 /* Set marks according to the reference pattern. */
13580 switch (ref->u.ar.type)
13581 {
13582 case AR_FULL:
13583 mark = AR_FULL;
13584 break;
13585
13586 case AR_SECTION:
13587 ar = &ref->u.ar;
13588 /* Get the start position of array section. */
13589 gfc_get_section_index (ar, section_index, &offset);
13590 mark = AR_SECTION;
13591 break;
13592
13593 default:
13594 gcc_unreachable ();
13595 }
13596
13597 if (!gfc_array_size (e, &size))
13598 {
13599 gfc_error ("Nonconstant array section at %L in DATA statement",
13600 &e->where);
13601 mpz_clear (offset);
13602 return false;
13603 }
13604 }
13605
13606 t = true;
13607
13608 while (mpz_cmp_ui (size, 0) > 0)
13609 {
13610 if (!next_data_value ())
13611 {
13612 gfc_error ("DATA statement at %L has more variables than values",
13613 where);
13614 t = false;
13615 break;
13616 }
13617
13618 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13619 if (!t)
13620 break;
13621
13622 /* If we have more than one element left in the repeat count,
13623 and we have more than one element left in the target variable,
13624 then create a range assignment. */
13625 /* FIXME: Only done for full arrays for now, since array sections
13626 seem tricky. */
13627 if (mark == AR_FULL && ref && ref->next == NULL
13628 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13629 {
13630 mpz_t range;
13631
13632 if (mpz_cmp (size, values.left) >= 0)
13633 {
13634 mpz_init_set (range, values.left);
13635 mpz_sub (size, size, values.left);
13636 mpz_set_ui (values.left, 0);
13637 }
13638 else
13639 {
13640 mpz_init_set (range, size);
13641 mpz_sub (values.left, values.left, size);
13642 mpz_set_ui (size, 0);
13643 }
13644
13645 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13646 offset, &range);
13647
13648 mpz_add (offset, offset, range);
13649 mpz_clear (range);
13650
13651 if (!t)
13652 break;
13653 }
13654
13655 /* Assign initial value to symbol. */
13656 else
13657 {
13658 mpz_sub_ui (values.left, values.left, 1);
13659 mpz_sub_ui (size, size, 1);
13660
13661 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13662 offset, NULL);
13663 if (!t)
13664 break;
13665
13666 if (mark == AR_FULL)
13667 mpz_add_ui (offset, offset, 1);
13668
13669 /* Modify the array section indexes and recalculate the offset
13670 for next element. */
13671 else if (mark == AR_SECTION)
13672 gfc_advance_section (section_index, ar, &offset);
13673 }
13674 }
13675
13676 if (mark == AR_SECTION)
13677 {
13678 for (i = 0; i < ar->dimen; i++)
13679 mpz_clear (section_index[i]);
13680 }
13681
13682 mpz_clear (size);
13683 mpz_clear (offset);
13684
13685 return t;
13686 }
13687
13688
13689 static bool traverse_data_var (gfc_data_variable *, locus *);
13690
13691 /* Iterate over a list of elements in a DATA statement. */
13692
13693 static bool
13694 traverse_data_list (gfc_data_variable *var, locus *where)
13695 {
13696 mpz_t trip;
13697 iterator_stack frame;
13698 gfc_expr *e, *start, *end, *step;
13699 bool retval = true;
13700
13701 mpz_init (frame.value);
13702 mpz_init (trip);
13703
13704 start = gfc_copy_expr (var->iter.start);
13705 end = gfc_copy_expr (var->iter.end);
13706 step = gfc_copy_expr (var->iter.step);
13707
13708 if (!gfc_simplify_expr (start, 1)
13709 || start->expr_type != EXPR_CONSTANT)
13710 {
13711 gfc_error ("start of implied-do loop at %L could not be "
13712 "simplified to a constant value", &start->where);
13713 retval = false;
13714 goto cleanup;
13715 }
13716 if (!gfc_simplify_expr (end, 1)
13717 || end->expr_type != EXPR_CONSTANT)
13718 {
13719 gfc_error ("end of implied-do loop at %L could not be "
13720 "simplified to a constant value", &start->where);
13721 retval = false;
13722 goto cleanup;
13723 }
13724 if (!gfc_simplify_expr (step, 1)
13725 || step->expr_type != EXPR_CONSTANT)
13726 {
13727 gfc_error ("step of implied-do loop at %L could not be "
13728 "simplified to a constant value", &start->where);
13729 retval = false;
13730 goto cleanup;
13731 }
13732
13733 mpz_set (trip, end->value.integer);
13734 mpz_sub (trip, trip, start->value.integer);
13735 mpz_add (trip, trip, step->value.integer);
13736
13737 mpz_div (trip, trip, step->value.integer);
13738
13739 mpz_set (frame.value, start->value.integer);
13740
13741 frame.prev = iter_stack;
13742 frame.variable = var->iter.var->symtree;
13743 iter_stack = &frame;
13744
13745 while (mpz_cmp_ui (trip, 0) > 0)
13746 {
13747 if (!traverse_data_var (var->list, where))
13748 {
13749 retval = false;
13750 goto cleanup;
13751 }
13752
13753 e = gfc_copy_expr (var->expr);
13754 if (!gfc_simplify_expr (e, 1))
13755 {
13756 gfc_free_expr (e);
13757 retval = false;
13758 goto cleanup;
13759 }
13760
13761 mpz_add (frame.value, frame.value, step->value.integer);
13762
13763 mpz_sub_ui (trip, trip, 1);
13764 }
13765
13766 cleanup:
13767 mpz_clear (frame.value);
13768 mpz_clear (trip);
13769
13770 gfc_free_expr (start);
13771 gfc_free_expr (end);
13772 gfc_free_expr (step);
13773
13774 iter_stack = frame.prev;
13775 return retval;
13776 }
13777
13778
13779 /* Type resolve variables in the variable list of a DATA statement. */
13780
13781 static bool
13782 traverse_data_var (gfc_data_variable *var, locus *where)
13783 {
13784 bool t;
13785
13786 for (; var; var = var->next)
13787 {
13788 if (var->expr == NULL)
13789 t = traverse_data_list (var, where);
13790 else
13791 t = check_data_variable (var, where);
13792
13793 if (!t)
13794 return false;
13795 }
13796
13797 return true;
13798 }
13799
13800
13801 /* Resolve the expressions and iterators associated with a data statement.
13802 This is separate from the assignment checking because data lists should
13803 only be resolved once. */
13804
13805 static bool
13806 resolve_data_variables (gfc_data_variable *d)
13807 {
13808 for (; d; d = d->next)
13809 {
13810 if (d->list == NULL)
13811 {
13812 if (!gfc_resolve_expr (d->expr))
13813 return false;
13814 }
13815 else
13816 {
13817 if (!gfc_resolve_iterator (&d->iter, false, true))
13818 return false;
13819
13820 if (!resolve_data_variables (d->list))
13821 return false;
13822 }
13823 }
13824
13825 return true;
13826 }
13827
13828
13829 /* Resolve a single DATA statement. We implement this by storing a pointer to
13830 the value list into static variables, and then recursively traversing the
13831 variables list, expanding iterators and such. */
13832
13833 static void
13834 resolve_data (gfc_data *d)
13835 {
13836
13837 if (!resolve_data_variables (d->var))
13838 return;
13839
13840 values.vnode = d->value;
13841 if (d->value == NULL)
13842 mpz_set_ui (values.left, 0);
13843 else
13844 mpz_set (values.left, d->value->repeat);
13845
13846 if (!traverse_data_var (d->var, &d->where))
13847 return;
13848
13849 /* At this point, we better not have any values left. */
13850
13851 if (next_data_value ())
13852 gfc_error ("DATA statement at %L has more values than variables",
13853 &d->where);
13854 }
13855
13856
13857 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13858 accessed by host or use association, is a dummy argument to a pure function,
13859 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13860 is storage associated with any such variable, shall not be used in the
13861 following contexts: (clients of this function). */
13862
13863 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13864 procedure. Returns zero if assignment is OK, nonzero if there is a
13865 problem. */
13866 int
13867 gfc_impure_variable (gfc_symbol *sym)
13868 {
13869 gfc_symbol *proc;
13870 gfc_namespace *ns;
13871
13872 if (sym->attr.use_assoc || sym->attr.in_common)
13873 return 1;
13874
13875 /* Check if the symbol's ns is inside the pure procedure. */
13876 for (ns = gfc_current_ns; ns; ns = ns->parent)
13877 {
13878 if (ns == sym->ns)
13879 break;
13880 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13881 return 1;
13882 }
13883
13884 proc = sym->ns->proc_name;
13885 if (sym->attr.dummy
13886 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13887 || proc->attr.function))
13888 return 1;
13889
13890 /* TODO: Sort out what can be storage associated, if anything, and include
13891 it here. In principle equivalences should be scanned but it does not
13892 seem to be possible to storage associate an impure variable this way. */
13893 return 0;
13894 }
13895
13896
13897 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13898 current namespace is inside a pure procedure. */
13899
13900 int
13901 gfc_pure (gfc_symbol *sym)
13902 {
13903 symbol_attribute attr;
13904 gfc_namespace *ns;
13905
13906 if (sym == NULL)
13907 {
13908 /* Check if the current namespace or one of its parents
13909 belongs to a pure procedure. */
13910 for (ns = gfc_current_ns; ns; ns = ns->parent)
13911 {
13912 sym = ns->proc_name;
13913 if (sym == NULL)
13914 return 0;
13915 attr = sym->attr;
13916 if (attr.flavor == FL_PROCEDURE && attr.pure)
13917 return 1;
13918 }
13919 return 0;
13920 }
13921
13922 attr = sym->attr;
13923
13924 return attr.flavor == FL_PROCEDURE && attr.pure;
13925 }
13926
13927
13928 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13929 checks if the current namespace is implicitly pure. Note that this
13930 function returns false for a PURE procedure. */
13931
13932 int
13933 gfc_implicit_pure (gfc_symbol *sym)
13934 {
13935 gfc_namespace *ns;
13936
13937 if (sym == NULL)
13938 {
13939 /* Check if the current procedure is implicit_pure. Walk up
13940 the procedure list until we find a procedure. */
13941 for (ns = gfc_current_ns; ns; ns = ns->parent)
13942 {
13943 sym = ns->proc_name;
13944 if (sym == NULL)
13945 return 0;
13946
13947 if (sym->attr.flavor == FL_PROCEDURE)
13948 break;
13949 }
13950 }
13951
13952 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13953 && !sym->attr.pure;
13954 }
13955
13956
13957 void
13958 gfc_unset_implicit_pure (gfc_symbol *sym)
13959 {
13960 gfc_namespace *ns;
13961
13962 if (sym == NULL)
13963 {
13964 /* Check if the current procedure is implicit_pure. Walk up
13965 the procedure list until we find a procedure. */
13966 for (ns = gfc_current_ns; ns; ns = ns->parent)
13967 {
13968 sym = ns->proc_name;
13969 if (sym == NULL)
13970 return;
13971
13972 if (sym->attr.flavor == FL_PROCEDURE)
13973 break;
13974 }
13975 }
13976
13977 if (sym->attr.flavor == FL_PROCEDURE)
13978 sym->attr.implicit_pure = 0;
13979 else
13980 sym->attr.pure = 0;
13981 }
13982
13983
13984 /* Test whether the current procedure is elemental or not. */
13985
13986 int
13987 gfc_elemental (gfc_symbol *sym)
13988 {
13989 symbol_attribute attr;
13990
13991 if (sym == NULL)
13992 sym = gfc_current_ns->proc_name;
13993 if (sym == NULL)
13994 return 0;
13995 attr = sym->attr;
13996
13997 return attr.flavor == FL_PROCEDURE && attr.elemental;
13998 }
13999
14000
14001 /* Warn about unused labels. */
14002
14003 static void
14004 warn_unused_fortran_label (gfc_st_label *label)
14005 {
14006 if (label == NULL)
14007 return;
14008
14009 warn_unused_fortran_label (label->left);
14010
14011 if (label->defined == ST_LABEL_UNKNOWN)
14012 return;
14013
14014 switch (label->referenced)
14015 {
14016 case ST_LABEL_UNKNOWN:
14017 gfc_warning ("Label %d at %L defined but not used", label->value,
14018 &label->where);
14019 break;
14020
14021 case ST_LABEL_BAD_TARGET:
14022 gfc_warning ("Label %d at %L defined but cannot be used",
14023 label->value, &label->where);
14024 break;
14025
14026 default:
14027 break;
14028 }
14029
14030 warn_unused_fortran_label (label->right);
14031 }
14032
14033
14034 /* Returns the sequence type of a symbol or sequence. */
14035
14036 static seq_type
14037 sequence_type (gfc_typespec ts)
14038 {
14039 seq_type result;
14040 gfc_component *c;
14041
14042 switch (ts.type)
14043 {
14044 case BT_DERIVED:
14045
14046 if (ts.u.derived->components == NULL)
14047 return SEQ_NONDEFAULT;
14048
14049 result = sequence_type (ts.u.derived->components->ts);
14050 for (c = ts.u.derived->components->next; c; c = c->next)
14051 if (sequence_type (c->ts) != result)
14052 return SEQ_MIXED;
14053
14054 return result;
14055
14056 case BT_CHARACTER:
14057 if (ts.kind != gfc_default_character_kind)
14058 return SEQ_NONDEFAULT;
14059
14060 return SEQ_CHARACTER;
14061
14062 case BT_INTEGER:
14063 if (ts.kind != gfc_default_integer_kind)
14064 return SEQ_NONDEFAULT;
14065
14066 return SEQ_NUMERIC;
14067
14068 case BT_REAL:
14069 if (!(ts.kind == gfc_default_real_kind
14070 || ts.kind == gfc_default_double_kind))
14071 return SEQ_NONDEFAULT;
14072
14073 return SEQ_NUMERIC;
14074
14075 case BT_COMPLEX:
14076 if (ts.kind != gfc_default_complex_kind)
14077 return SEQ_NONDEFAULT;
14078
14079 return SEQ_NUMERIC;
14080
14081 case BT_LOGICAL:
14082 if (ts.kind != gfc_default_logical_kind)
14083 return SEQ_NONDEFAULT;
14084
14085 return SEQ_NUMERIC;
14086
14087 default:
14088 return SEQ_NONDEFAULT;
14089 }
14090 }
14091
14092
14093 /* Resolve derived type EQUIVALENCE object. */
14094
14095 static bool
14096 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14097 {
14098 gfc_component *c = derived->components;
14099
14100 if (!derived)
14101 return true;
14102
14103 /* Shall not be an object of nonsequence derived type. */
14104 if (!derived->attr.sequence)
14105 {
14106 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14107 "attribute to be an EQUIVALENCE object", sym->name,
14108 &e->where);
14109 return false;
14110 }
14111
14112 /* Shall not have allocatable components. */
14113 if (derived->attr.alloc_comp)
14114 {
14115 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14116 "components to be an EQUIVALENCE object",sym->name,
14117 &e->where);
14118 return false;
14119 }
14120
14121 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14122 {
14123 gfc_error ("Derived type variable '%s' at %L with default "
14124 "initialization cannot be in EQUIVALENCE with a variable "
14125 "in COMMON", sym->name, &e->where);
14126 return false;
14127 }
14128
14129 for (; c ; c = c->next)
14130 {
14131 if (c->ts.type == BT_DERIVED
14132 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14133 return false;
14134
14135 /* Shall not be an object of sequence derived type containing a pointer
14136 in the structure. */
14137 if (c->attr.pointer)
14138 {
14139 gfc_error ("Derived type variable '%s' at %L with pointer "
14140 "component(s) cannot be an EQUIVALENCE object",
14141 sym->name, &e->where);
14142 return false;
14143 }
14144 }
14145 return true;
14146 }
14147
14148
14149 /* Resolve equivalence object.
14150 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14151 an allocatable array, an object of nonsequence derived type, an object of
14152 sequence derived type containing a pointer at any level of component
14153 selection, an automatic object, a function name, an entry name, a result
14154 name, a named constant, a structure component, or a subobject of any of
14155 the preceding objects. A substring shall not have length zero. A
14156 derived type shall not have components with default initialization nor
14157 shall two objects of an equivalence group be initialized.
14158 Either all or none of the objects shall have an protected attribute.
14159 The simple constraints are done in symbol.c(check_conflict) and the rest
14160 are implemented here. */
14161
14162 static void
14163 resolve_equivalence (gfc_equiv *eq)
14164 {
14165 gfc_symbol *sym;
14166 gfc_symbol *first_sym;
14167 gfc_expr *e;
14168 gfc_ref *r;
14169 locus *last_where = NULL;
14170 seq_type eq_type, last_eq_type;
14171 gfc_typespec *last_ts;
14172 int object, cnt_protected;
14173 const char *msg;
14174
14175 last_ts = &eq->expr->symtree->n.sym->ts;
14176
14177 first_sym = eq->expr->symtree->n.sym;
14178
14179 cnt_protected = 0;
14180
14181 for (object = 1; eq; eq = eq->eq, object++)
14182 {
14183 e = eq->expr;
14184
14185 e->ts = e->symtree->n.sym->ts;
14186 /* match_varspec might not know yet if it is seeing
14187 array reference or substring reference, as it doesn't
14188 know the types. */
14189 if (e->ref && e->ref->type == REF_ARRAY)
14190 {
14191 gfc_ref *ref = e->ref;
14192 sym = e->symtree->n.sym;
14193
14194 if (sym->attr.dimension)
14195 {
14196 ref->u.ar.as = sym->as;
14197 ref = ref->next;
14198 }
14199
14200 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14201 if (e->ts.type == BT_CHARACTER
14202 && ref
14203 && ref->type == REF_ARRAY
14204 && ref->u.ar.dimen == 1
14205 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14206 && ref->u.ar.stride[0] == NULL)
14207 {
14208 gfc_expr *start = ref->u.ar.start[0];
14209 gfc_expr *end = ref->u.ar.end[0];
14210 void *mem = NULL;
14211
14212 /* Optimize away the (:) reference. */
14213 if (start == NULL && end == NULL)
14214 {
14215 if (e->ref == ref)
14216 e->ref = ref->next;
14217 else
14218 e->ref->next = ref->next;
14219 mem = ref;
14220 }
14221 else
14222 {
14223 ref->type = REF_SUBSTRING;
14224 if (start == NULL)
14225 start = gfc_get_int_expr (gfc_default_integer_kind,
14226 NULL, 1);
14227 ref->u.ss.start = start;
14228 if (end == NULL && e->ts.u.cl)
14229 end = gfc_copy_expr (e->ts.u.cl->length);
14230 ref->u.ss.end = end;
14231 ref->u.ss.length = e->ts.u.cl;
14232 e->ts.u.cl = NULL;
14233 }
14234 ref = ref->next;
14235 free (mem);
14236 }
14237
14238 /* Any further ref is an error. */
14239 if (ref)
14240 {
14241 gcc_assert (ref->type == REF_ARRAY);
14242 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14243 &ref->u.ar.where);
14244 continue;
14245 }
14246 }
14247
14248 if (!gfc_resolve_expr (e))
14249 continue;
14250
14251 sym = e->symtree->n.sym;
14252
14253 if (sym->attr.is_protected)
14254 cnt_protected++;
14255 if (cnt_protected > 0 && cnt_protected != object)
14256 {
14257 gfc_error ("Either all or none of the objects in the "
14258 "EQUIVALENCE set at %L shall have the "
14259 "PROTECTED attribute",
14260 &e->where);
14261 break;
14262 }
14263
14264 /* Shall not equivalence common block variables in a PURE procedure. */
14265 if (sym->ns->proc_name
14266 && sym->ns->proc_name->attr.pure
14267 && sym->attr.in_common)
14268 {
14269 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14270 "object in the pure procedure '%s'",
14271 sym->name, &e->where, sym->ns->proc_name->name);
14272 break;
14273 }
14274
14275 /* Shall not be a named constant. */
14276 if (e->expr_type == EXPR_CONSTANT)
14277 {
14278 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14279 "object", sym->name, &e->where);
14280 continue;
14281 }
14282
14283 if (e->ts.type == BT_DERIVED
14284 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14285 continue;
14286
14287 /* Check that the types correspond correctly:
14288 Note 5.28:
14289 A numeric sequence structure may be equivalenced to another sequence
14290 structure, an object of default integer type, default real type, double
14291 precision real type, default logical type such that components of the
14292 structure ultimately only become associated to objects of the same
14293 kind. A character sequence structure may be equivalenced to an object
14294 of default character kind or another character sequence structure.
14295 Other objects may be equivalenced only to objects of the same type and
14296 kind parameters. */
14297
14298 /* Identical types are unconditionally OK. */
14299 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14300 goto identical_types;
14301
14302 last_eq_type = sequence_type (*last_ts);
14303 eq_type = sequence_type (sym->ts);
14304
14305 /* Since the pair of objects is not of the same type, mixed or
14306 non-default sequences can be rejected. */
14307
14308 msg = "Sequence %s with mixed components in EQUIVALENCE "
14309 "statement at %L with different type objects";
14310 if ((object ==2
14311 && last_eq_type == SEQ_MIXED
14312 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14313 || (eq_type == SEQ_MIXED
14314 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14315 continue;
14316
14317 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14318 "statement at %L with objects of different type";
14319 if ((object ==2
14320 && last_eq_type == SEQ_NONDEFAULT
14321 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14322 || (eq_type == SEQ_NONDEFAULT
14323 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14324 continue;
14325
14326 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14327 "EQUIVALENCE statement at %L";
14328 if (last_eq_type == SEQ_CHARACTER
14329 && eq_type != SEQ_CHARACTER
14330 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14331 continue;
14332
14333 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14334 "EQUIVALENCE statement at %L";
14335 if (last_eq_type == SEQ_NUMERIC
14336 && eq_type != SEQ_NUMERIC
14337 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14338 continue;
14339
14340 identical_types:
14341 last_ts =&sym->ts;
14342 last_where = &e->where;
14343
14344 if (!e->ref)
14345 continue;
14346
14347 /* Shall not be an automatic array. */
14348 if (e->ref->type == REF_ARRAY
14349 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14350 {
14351 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14352 "an EQUIVALENCE object", sym->name, &e->where);
14353 continue;
14354 }
14355
14356 r = e->ref;
14357 while (r)
14358 {
14359 /* Shall not be a structure component. */
14360 if (r->type == REF_COMPONENT)
14361 {
14362 gfc_error ("Structure component '%s' at %L cannot be an "
14363 "EQUIVALENCE object",
14364 r->u.c.component->name, &e->where);
14365 break;
14366 }
14367
14368 /* A substring shall not have length zero. */
14369 if (r->type == REF_SUBSTRING)
14370 {
14371 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14372 {
14373 gfc_error ("Substring at %L has length zero",
14374 &r->u.ss.start->where);
14375 break;
14376 }
14377 }
14378 r = r->next;
14379 }
14380 }
14381 }
14382
14383
14384 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14385
14386 static void
14387 resolve_fntype (gfc_namespace *ns)
14388 {
14389 gfc_entry_list *el;
14390 gfc_symbol *sym;
14391
14392 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14393 return;
14394
14395 /* If there are any entries, ns->proc_name is the entry master
14396 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14397 if (ns->entries)
14398 sym = ns->entries->sym;
14399 else
14400 sym = ns->proc_name;
14401 if (sym->result == sym
14402 && sym->ts.type == BT_UNKNOWN
14403 && !gfc_set_default_type (sym, 0, NULL)
14404 && !sym->attr.untyped)
14405 {
14406 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14407 sym->name, &sym->declared_at);
14408 sym->attr.untyped = 1;
14409 }
14410
14411 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14412 && !sym->attr.contained
14413 && !gfc_check_symbol_access (sym->ts.u.derived)
14414 && gfc_check_symbol_access (sym))
14415 {
14416 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14417 "%L of PRIVATE type '%s'", sym->name,
14418 &sym->declared_at, sym->ts.u.derived->name);
14419 }
14420
14421 if (ns->entries)
14422 for (el = ns->entries->next; el; el = el->next)
14423 {
14424 if (el->sym->result == el->sym
14425 && el->sym->ts.type == BT_UNKNOWN
14426 && !gfc_set_default_type (el->sym, 0, NULL)
14427 && !el->sym->attr.untyped)
14428 {
14429 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14430 el->sym->name, &el->sym->declared_at);
14431 el->sym->attr.untyped = 1;
14432 }
14433 }
14434 }
14435
14436
14437 /* 12.3.2.1.1 Defined operators. */
14438
14439 static bool
14440 check_uop_procedure (gfc_symbol *sym, locus where)
14441 {
14442 gfc_formal_arglist *formal;
14443
14444 if (!sym->attr.function)
14445 {
14446 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14447 sym->name, &where);
14448 return false;
14449 }
14450
14451 if (sym->ts.type == BT_CHARACTER
14452 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14453 && !(sym->result && sym->result->ts.u.cl
14454 && sym->result->ts.u.cl->length))
14455 {
14456 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14457 "character length", sym->name, &where);
14458 return false;
14459 }
14460
14461 formal = gfc_sym_get_dummy_args (sym);
14462 if (!formal || !formal->sym)
14463 {
14464 gfc_error ("User operator procedure '%s' at %L must have at least "
14465 "one argument", sym->name, &where);
14466 return false;
14467 }
14468
14469 if (formal->sym->attr.intent != INTENT_IN)
14470 {
14471 gfc_error ("First argument of operator interface at %L must be "
14472 "INTENT(IN)", &where);
14473 return false;
14474 }
14475
14476 if (formal->sym->attr.optional)
14477 {
14478 gfc_error ("First argument of operator interface at %L cannot be "
14479 "optional", &where);
14480 return false;
14481 }
14482
14483 formal = formal->next;
14484 if (!formal || !formal->sym)
14485 return true;
14486
14487 if (formal->sym->attr.intent != INTENT_IN)
14488 {
14489 gfc_error ("Second argument of operator interface at %L must be "
14490 "INTENT(IN)", &where);
14491 return false;
14492 }
14493
14494 if (formal->sym->attr.optional)
14495 {
14496 gfc_error ("Second argument of operator interface at %L cannot be "
14497 "optional", &where);
14498 return false;
14499 }
14500
14501 if (formal->next)
14502 {
14503 gfc_error ("Operator interface at %L must have, at most, two "
14504 "arguments", &where);
14505 return false;
14506 }
14507
14508 return true;
14509 }
14510
14511 static void
14512 gfc_resolve_uops (gfc_symtree *symtree)
14513 {
14514 gfc_interface *itr;
14515
14516 if (symtree == NULL)
14517 return;
14518
14519 gfc_resolve_uops (symtree->left);
14520 gfc_resolve_uops (symtree->right);
14521
14522 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14523 check_uop_procedure (itr->sym, itr->sym->declared_at);
14524 }
14525
14526
14527 /* Examine all of the expressions associated with a program unit,
14528 assign types to all intermediate expressions, make sure that all
14529 assignments are to compatible types and figure out which names
14530 refer to which functions or subroutines. It doesn't check code
14531 block, which is handled by resolve_code. */
14532
14533 static void
14534 resolve_types (gfc_namespace *ns)
14535 {
14536 gfc_namespace *n;
14537 gfc_charlen *cl;
14538 gfc_data *d;
14539 gfc_equiv *eq;
14540 gfc_namespace* old_ns = gfc_current_ns;
14541
14542 /* Check that all IMPLICIT types are ok. */
14543 if (!ns->seen_implicit_none)
14544 {
14545 unsigned letter;
14546 for (letter = 0; letter != GFC_LETTERS; ++letter)
14547 if (ns->set_flag[letter]
14548 && !resolve_typespec_used (&ns->default_type[letter],
14549 &ns->implicit_loc[letter], NULL))
14550 return;
14551 }
14552
14553 gfc_current_ns = ns;
14554
14555 resolve_entries (ns);
14556
14557 resolve_common_vars (ns->blank_common.head, false);
14558 resolve_common_blocks (ns->common_root);
14559
14560 resolve_contained_functions (ns);
14561
14562 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14563 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14564 resolve_formal_arglist (ns->proc_name);
14565
14566 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14567
14568 for (cl = ns->cl_list; cl; cl = cl->next)
14569 resolve_charlen (cl);
14570
14571 gfc_traverse_ns (ns, resolve_symbol);
14572
14573 resolve_fntype (ns);
14574
14575 for (n = ns->contained; n; n = n->sibling)
14576 {
14577 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14578 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14579 "also be PURE", n->proc_name->name,
14580 &n->proc_name->declared_at);
14581
14582 resolve_types (n);
14583 }
14584
14585 forall_flag = 0;
14586 gfc_do_concurrent_flag = 0;
14587 gfc_check_interfaces (ns);
14588
14589 gfc_traverse_ns (ns, resolve_values);
14590
14591 if (ns->save_all)
14592 gfc_save_all (ns);
14593
14594 iter_stack = NULL;
14595 for (d = ns->data; d; d = d->next)
14596 resolve_data (d);
14597
14598 iter_stack = NULL;
14599 gfc_traverse_ns (ns, gfc_formalize_init_value);
14600
14601 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14602
14603 for (eq = ns->equiv; eq; eq = eq->next)
14604 resolve_equivalence (eq);
14605
14606 /* Warn about unused labels. */
14607 if (warn_unused_label)
14608 warn_unused_fortran_label (ns->st_labels);
14609
14610 gfc_resolve_uops (ns->uop_root);
14611
14612 gfc_current_ns = old_ns;
14613 }
14614
14615
14616 /* Call resolve_code recursively. */
14617
14618 static void
14619 resolve_codes (gfc_namespace *ns)
14620 {
14621 gfc_namespace *n;
14622 bitmap_obstack old_obstack;
14623
14624 if (ns->resolved == 1)
14625 return;
14626
14627 for (n = ns->contained; n; n = n->sibling)
14628 resolve_codes (n);
14629
14630 gfc_current_ns = ns;
14631
14632 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14633 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14634 cs_base = NULL;
14635
14636 /* Set to an out of range value. */
14637 current_entry_id = -1;
14638
14639 old_obstack = labels_obstack;
14640 bitmap_obstack_initialize (&labels_obstack);
14641
14642 resolve_code (ns->code, ns);
14643
14644 bitmap_obstack_release (&labels_obstack);
14645 labels_obstack = old_obstack;
14646 }
14647
14648
14649 /* This function is called after a complete program unit has been compiled.
14650 Its purpose is to examine all of the expressions associated with a program
14651 unit, assign types to all intermediate expressions, make sure that all
14652 assignments are to compatible types and figure out which names refer to
14653 which functions or subroutines. */
14654
14655 void
14656 gfc_resolve (gfc_namespace *ns)
14657 {
14658 gfc_namespace *old_ns;
14659 code_stack *old_cs_base;
14660
14661 if (ns->resolved)
14662 return;
14663
14664 ns->resolved = -1;
14665 old_ns = gfc_current_ns;
14666 old_cs_base = cs_base;
14667
14668 resolve_types (ns);
14669 component_assignment_level = 0;
14670 resolve_codes (ns);
14671
14672 gfc_current_ns = old_ns;
14673 cs_base = old_cs_base;
14674 ns->resolved = 1;
14675
14676 gfc_run_passes (ns);
14677 }