Debian Codegraph @

gcc/fortran/resolve.cc

raw
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2025 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 "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
32
33 /* Types used in equivalence statements. */
34
35 enum seq_type
36 {
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 };
39
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
42
43 typedef struct code_stack
44 {
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
47
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
52 }
53 code_stack;
54
55 static code_stack *cs_base = NULL;
56
57 struct check_default_none_data
58 {
59 gfc_code *code;
60 hash_set<gfc_symbol *> *sym_hash;
61 gfc_namespace *ns;
62 bool default_none;
63 };
64
65 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
66
67 static int forall_flag;
68 int gfc_do_concurrent_flag;
69
70 /* True when we are resolving an expression that is an actual argument to
71 a procedure. */
72 static bool actual_arg = false;
73 /* True when we are resolving an expression that is the first actual argument
74 to a procedure. */
75 static bool first_actual_arg = false;
76
77
78 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
79
80 static int omp_workshare_flag;
81
82
83 /* True if we are resolving a specification expression. */
84 static bool specification_expr = false;
85 /* The dummy whose character length or array bounds are currently being
86 resolved as a specification expression. */
87 static gfc_symbol *specification_expr_symbol = NULL;
88
89 /* The id of the last entry seen. */
90 static int current_entry_id;
91
92 /* We use bitmaps to determine if a branch target is valid. */
93 static bitmap_obstack labels_obstack;
94
95 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
96 static bool inquiry_argument = false;
97
98 static bool
99 entry_dummy_seen_p (gfc_symbol *sym)
100 {
101 gfc_entry_list *entry;
102 gfc_formal_arglist *formal;
103
104 gcc_checking_assert (sym->attr.dummy && sym->ns == gfc_current_ns);
105
106 for (entry = gfc_current_ns->entries;
107 entry && entry->id <= current_entry_id;
108 entry = entry->next)
109 for (formal = entry->sym->formal; formal; formal = formal->next)
110 if (formal->sym && sym->name == formal->sym->name)
111 return true;
112
113 return false;
114 }
115
116
117 /* Is the symbol host associated? */
118 static bool
119 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
120 {
121 for (ns = ns->parent; ns; ns = ns->parent)
122 {
123 if (sym->ns == ns)
124 return true;
125 }
126
127 return false;
128 }
129
130 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
131 an ABSTRACT derived-type. If where is not NULL, an error message with that
132 locus is printed, optionally using name. */
133
134 static bool
135 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
136 {
137 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
138 {
139 if (where)
140 {
141 if (name)
142 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
143 name, where, ts->u.derived->name);
144 else
145 gfc_error ("ABSTRACT type %qs used at %L",
146 ts->u.derived->name, where);
147 }
148
149 return false;
150 }
151
152 return true;
153 }
154
155
156 static bool
157 check_proc_interface (gfc_symbol *ifc, locus *where)
158 {
159 /* Several checks for F08:C1216. */
160 if (ifc->attr.procedure)
161 {
162 gfc_error ("Interface %qs at %L is declared "
163 "in a later PROCEDURE statement", ifc->name, where);
164 return false;
165 }
166 if (ifc->generic)
167 {
168 /* For generic interfaces, check if there is
169 a specific procedure with the same name. */
170 gfc_interface *gen = ifc->generic;
171 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
172 gen = gen->next;
173 if (!gen)
174 {
175 gfc_error ("Interface %qs at %L may not be generic",
176 ifc->name, where);
177 return false;
178 }
179 }
180 if (ifc->attr.proc == PROC_ST_FUNCTION)
181 {
182 gfc_error ("Interface %qs at %L may not be a statement function",
183 ifc->name, where);
184 return false;
185 }
186 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
187 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
188 ifc->attr.intrinsic = 1;
189 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
190 {
191 gfc_error ("Intrinsic procedure %qs not allowed in "
192 "PROCEDURE statement at %L", ifc->name, where);
193 return false;
194 }
195 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
196 {
197 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
198 return false;
199 }
200 return true;
201 }
202
203
204 static void resolve_symbol (gfc_symbol *sym);
205
206
207 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
208
209 static bool
210 resolve_procedure_interface (gfc_symbol *sym)
211 {
212 gfc_symbol *ifc = sym->ts.interface;
213
214 if (!ifc)
215 return true;
216
217 if (ifc == sym)
218 {
219 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
220 sym->name, &sym->declared_at);
221 return false;
222 }
223 if (!check_proc_interface (ifc, &sym->declared_at))
224 return false;
225
226 if (ifc->attr.if_source || ifc->attr.intrinsic)
227 {
228 /* Resolve interface and copy attributes. */
229 resolve_symbol (ifc);
230 if (ifc->attr.intrinsic)
231 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
232
233 if (ifc->result)
234 {
235 sym->ts = ifc->result->ts;
236 sym->attr.allocatable = ifc->result->attr.allocatable;
237 sym->attr.pointer = ifc->result->attr.pointer;
238 sym->attr.dimension = ifc->result->attr.dimension;
239 sym->attr.class_ok = ifc->result->attr.class_ok;
240 sym->as = gfc_copy_array_spec (ifc->result->as);
241 sym->result = sym;
242 }
243 else
244 {
245 sym->ts = ifc->ts;
246 sym->attr.allocatable = ifc->attr.allocatable;
247 sym->attr.pointer = ifc->attr.pointer;
248 sym->attr.dimension = ifc->attr.dimension;
249 sym->attr.class_ok = ifc->attr.class_ok;
250 sym->as = gfc_copy_array_spec (ifc->as);
251 }
252 sym->ts.interface = ifc;
253 sym->attr.function = ifc->attr.function;
254 sym->attr.subroutine = ifc->attr.subroutine;
255
256 sym->attr.pure = ifc->attr.pure;
257 sym->attr.elemental = ifc->attr.elemental;
258 sym->attr.contiguous = ifc->attr.contiguous;
259 sym->attr.recursive = ifc->attr.recursive;
260 sym->attr.always_explicit = ifc->attr.always_explicit;
261 sym->attr.ext_attr |= ifc->attr.ext_attr;
262 sym->attr.is_bind_c = ifc->attr.is_bind_c;
263 /* Copy char length. */
264 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
265 {
266 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
267 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
268 && !gfc_resolve_expr (sym->ts.u.cl->length))
269 return false;
270 }
271 }
272
273 return true;
274 }
275
276
277 /* Resolve types of formal argument lists. These have to be done early so that
278 the formal argument lists of module procedures can be copied to the
279 containing module before the individual procedures are resolved
280 individually. We also resolve argument lists of procedures in interface
281 blocks because they are self-contained scoping units.
282
283 Since a dummy argument cannot be a non-dummy procedure, the only
284 resort left for untyped names are the IMPLICIT types. */
285
286 void
287 gfc_resolve_formal_arglist (gfc_symbol *proc)
288 {
289 gfc_formal_arglist *f;
290 gfc_symbol *sym;
291 bool saved_specification_expr;
292 int i;
293
294 if (proc->result != NULL)
295 sym = proc->result;
296 else
297 sym = proc;
298
299 if (gfc_elemental (proc)
300 || sym->attr.pointer || sym->attr.allocatable
301 || (sym->as && sym->as->rank != 0))
302 {
303 proc->attr.always_explicit = 1;
304 sym->attr.always_explicit = 1;
305 }
306
307 gfc_namespace *orig_current_ns = gfc_current_ns;
308 gfc_current_ns = gfc_get_procedure_ns (proc);
309
310 for (f = proc->formal; f; f = f->next)
311 {
312 gfc_array_spec *as;
313 gfc_symbol *saved_specification_expr_symbol;
314
315 sym = f->sym;
316
317 if (sym == NULL)
318 {
319 /* Alternate return placeholder. */
320 if (gfc_elemental (proc))
321 gfc_error ("Alternate return specifier in elemental subroutine "
322 "%qs at %L is not allowed", proc->name,
323 &proc->declared_at);
324 if (proc->attr.function)
325 gfc_error ("Alternate return specifier in function "
326 "%qs at %L is not allowed", proc->name,
327 &proc->declared_at);
328 continue;
329 }
330
331 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
332 && !resolve_procedure_interface (sym))
333 break;
334
335 if (strcmp (proc->name, sym->name) == 0)
336 {
337 gfc_error ("Self-referential argument "
338 "%qs at %L is not allowed", sym->name,
339 &proc->declared_at);
340 break;
341 }
342
343 if (sym->attr.if_source != IFSRC_UNKNOWN)
344 gfc_resolve_formal_arglist (sym);
345
346 if (sym->attr.subroutine || sym->attr.external)
347 {
348 if (sym->attr.flavor == FL_UNKNOWN)
349 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
350 }
351 else
352 {
353 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
354 && (!sym->attr.function || sym->result == sym))
355 gfc_set_default_type (sym, 1, sym->ns);
356 }
357
358 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
359 ? CLASS_DATA (sym)->as : sym->as;
360
361 saved_specification_expr = specification_expr;
362 saved_specification_expr_symbol = specification_expr_symbol;
363 specification_expr = true;
364 specification_expr_symbol = sym;
365 gfc_resolve_array_spec (as, 0);
366 specification_expr = saved_specification_expr;
367 specification_expr_symbol = saved_specification_expr_symbol;
368
369 /* We can't tell if an array with dimension (:) is assumed or deferred
370 shape until we know if it has the pointer or allocatable attributes.
371 */
372 if (as && as->rank > 0 && as->type == AS_DEFERRED
373 && ((sym->ts.type != BT_CLASS
374 && !(sym->attr.pointer || sym->attr.allocatable))
375 || (sym->ts.type == BT_CLASS
376 && !(CLASS_DATA (sym)->attr.class_pointer
377 || CLASS_DATA (sym)->attr.allocatable)))
378 && sym->attr.flavor != FL_PROCEDURE)
379 {
380 as->type = AS_ASSUMED_SHAPE;
381 for (i = 0; i < as->rank; i++)
382 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
383 }
384
385 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
386 || (as && as->type == AS_ASSUMED_RANK)
387 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
388 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
389 && (CLASS_DATA (sym)->attr.class_pointer
390 || CLASS_DATA (sym)->attr.allocatable
391 || CLASS_DATA (sym)->attr.target))
392 || sym->attr.optional)
393 {
394 proc->attr.always_explicit = 1;
395 if (proc->result)
396 proc->result->attr.always_explicit = 1;
397 }
398
399 /* If the flavor is unknown at this point, it has to be a variable.
400 A procedure specification would have already set the type. */
401
402 if (sym->attr.flavor == FL_UNKNOWN)
403 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
404
405 if (gfc_pure (proc))
406 {
407 if (sym->attr.flavor == FL_PROCEDURE)
408 {
409 /* F08:C1279. */
410 if (!gfc_pure (sym))
411 {
412 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
413 "also be PURE", sym->name, &sym->declared_at);
414 continue;
415 }
416 }
417 else if (!sym->attr.pointer)
418 {
419 if (proc->attr.function && sym->attr.intent != INTENT_IN)
420 {
421 if (sym->attr.value)
422 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
423 " of pure function %qs at %L with VALUE "
424 "attribute but without INTENT(IN)",
425 sym->name, proc->name, &sym->declared_at);
426 else
427 gfc_error ("Argument %qs of pure function %qs at %L must "
428 "be INTENT(IN) or VALUE", sym->name, proc->name,
429 &sym->declared_at);
430 }
431
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
433 {
434 if (sym->attr.value)
435 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
436 " of pure subroutine %qs at %L with VALUE "
437 "attribute but without INTENT", sym->name,
438 proc->name, &sym->declared_at);
439 else
440 gfc_error ("Argument %qs of pure subroutine %qs at %L "
441 "must have its INTENT specified or have the "
442 "VALUE attribute", sym->name, proc->name,
443 &sym->declared_at);
444 }
445 }
446
447 /* F08:C1278a. */
448 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
449 {
450 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
451 " may not be polymorphic", sym->name, proc->name,
452 &sym->declared_at);
453 continue;
454 }
455 }
456
457 if (proc->attr.implicit_pure)
458 {
459 if (sym->attr.flavor == FL_PROCEDURE)
460 {
461 if (!gfc_pure (sym))
462 proc->attr.implicit_pure = 0;
463 }
464 else if (!sym->attr.pointer)
465 {
466 if (proc->attr.function && sym->attr.intent != INTENT_IN
467 && !sym->value)
468 proc->attr.implicit_pure = 0;
469
470 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
471 && !sym->value)
472 proc->attr.implicit_pure = 0;
473 }
474 }
475
476 if (gfc_elemental (proc))
477 {
478 /* F08:C1289. */
479 if (sym->attr.codimension
480 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
481 && CLASS_DATA (sym)->attr.codimension))
482 {
483 gfc_error ("Coarray dummy argument %qs at %L to elemental "
484 "procedure", sym->name, &sym->declared_at);
485 continue;
486 }
487
488 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
489 && CLASS_DATA (sym)->as))
490 {
491 gfc_error ("Argument %qs of elemental procedure at %L must "
492 "be scalar", sym->name, &sym->declared_at);
493 continue;
494 }
495
496 if (sym->attr.allocatable
497 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
498 && CLASS_DATA (sym)->attr.allocatable))
499 {
500 gfc_error ("Argument %qs of elemental procedure at %L cannot "
501 "have the ALLOCATABLE attribute", sym->name,
502 &sym->declared_at);
503 continue;
504 }
505
506 if (sym->attr.pointer
507 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
508 && CLASS_DATA (sym)->attr.class_pointer))
509 {
510 gfc_error ("Argument %qs of elemental procedure at %L cannot "
511 "have the POINTER attribute", sym->name,
512 &sym->declared_at);
513 continue;
514 }
515
516 if (sym->attr.flavor == FL_PROCEDURE)
517 {
518 gfc_error ("Dummy procedure %qs not allowed in elemental "
519 "procedure %qs at %L", sym->name, proc->name,
520 &sym->declared_at);
521 continue;
522 }
523
524 /* Fortran 2008 Corrigendum 1, C1290a. */
525 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
526 {
527 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
528 "have its INTENT specified or have the VALUE "
529 "attribute", sym->name, proc->name,
530 &sym->declared_at);
531 continue;
532 }
533 }
534
535 /* Each dummy shall be specified to be scalar. */
536 if (proc->attr.proc == PROC_ST_FUNCTION)
537 {
538 if (sym->as != NULL)
539 {
540 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
541 shall be specified, explicitly or implicitly, to be scalar. */
542 gfc_error ("Argument %qs of statement function %qs at %L "
543 "must be scalar", sym->name, proc->name,
544 &proc->declared_at);
545 continue;
546 }
547
548 if (sym->ts.type == BT_CHARACTER)
549 {
550 gfc_charlen *cl = sym->ts.u.cl;
551 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
552 {
553 gfc_error ("Character-valued argument %qs of statement "
554 "function at %L must have constant length",
555 sym->name, &sym->declared_at);
556 continue;
557 }
558 }
559 }
560 }
561 if (sym)
562 sym->formal_resolved = 1;
563 gfc_current_ns = orig_current_ns;
564 }
565
566
567 /* Work function called when searching for symbols that have argument lists
568 associated with them. */
569
570 static void
571 find_arglists (gfc_symbol *sym)
572 {
573 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
574 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
575 return;
576
577 gfc_resolve_formal_arglist (sym);
578 }
579
580
581 /* Given a namespace, resolve all formal argument lists within the namespace.
582 */
583
584 static void
585 resolve_formal_arglists (gfc_namespace *ns)
586 {
587 if (ns == NULL)
588 return;
589
590 gfc_traverse_ns (ns, find_arglists);
591 }
592
593
594 static void
595 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
596 {
597 bool t;
598
599 if (sym && sym->attr.flavor == FL_PROCEDURE
600 && sym->ns->parent
601 && sym->ns->parent->proc_name
602 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
603 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
604 gfc_error ("Contained procedure %qs at %L has the same name as its "
605 "encompassing procedure", sym->name, &sym->declared_at);
606
607 /* If this namespace is not a function or an entry master function,
608 ignore it. */
609 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
610 || sym->attr.entry_master)
611 return;
612
613 if (!sym->result)
614 return;
615
616 /* Try to find out of what the return type is. */
617 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
618 {
619 t = gfc_set_default_type (sym->result, 0, ns);
620
621 if (!t && !sym->result->attr.untyped)
622 {
623 if (sym->result == sym)
624 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
625 sym->name, &sym->declared_at);
626 else if (!sym->result->attr.proc_pointer)
627 gfc_error ("Result %qs of contained function %qs at %L has "
628 "no IMPLICIT type", sym->result->name, sym->name,
629 &sym->result->declared_at);
630 sym->result->attr.untyped = 1;
631 }
632 }
633
634 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
635 type, lists the only ways a character length value of * can be used:
636 dummy arguments of procedures, named constants, function results and
637 in allocate statements if the allocate_object is an assumed length dummy
638 in external functions. Internal function results and results of module
639 procedures are not on this list, ergo, not permitted. */
640
641 if (sym->result->ts.type == BT_CHARACTER)
642 {
643 gfc_charlen *cl = sym->result->ts.u.cl;
644 if ((!cl || !cl->length) && !sym->result->ts.deferred)
645 {
646 /* See if this is a module-procedure and adapt error message
647 accordingly. */
648 bool module_proc;
649 gcc_assert (ns->parent && ns->parent->proc_name);
650 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
651
652 gfc_error (module_proc
653 ? G_("Character-valued module procedure %qs at %L"
654 " must not be assumed length")
655 : G_("Character-valued internal function %qs at %L"
656 " must not be assumed length"),
657 sym->name, &sym->declared_at);
658 }
659 }
660 }
661
662
663 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
664 introduce duplicates. */
665
666 static void
667 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
668 {
669 gfc_formal_arglist *f, *new_arglist;
670 gfc_symbol *new_sym;
671
672 for (; new_args != NULL; new_args = new_args->next)
673 {
674 new_sym = new_args->sym;
675 /* See if this arg is already in the formal argument list. */
676 for (f = proc->formal; f; f = f->next)
677 {
678 if (new_sym == f->sym)
679 break;
680 }
681
682 if (f)
683 continue;
684
685 /* Add a new argument. Argument order is not important. */
686 new_arglist = gfc_get_formal_arglist ();
687 new_arglist->sym = new_sym;
688 new_arglist->next = proc->formal;
689 proc->formal = new_arglist;
690 }
691 }
692
693
694 /* Flag the arguments that are not present in all entries. */
695
696 static void
697 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
698 {
699 gfc_formal_arglist *f, *head;
700 head = new_args;
701
702 for (f = proc->formal; f; f = f->next)
703 {
704 if (f->sym == NULL)
705 continue;
706
707 for (new_args = head; new_args; new_args = new_args->next)
708 {
709 if (new_args->sym == f->sym)
710 break;
711 }
712
713 if (new_args)
714 continue;
715
716 f->sym->attr.not_always_present = 1;
717 }
718 }
719
720
721 /* Resolve alternate entry points. If a symbol has multiple entry points we
722 create a new master symbol for the main routine, and turn the existing
723 symbol into an entry point. */
724
725 static void
726 resolve_entries (gfc_namespace *ns)
727 {
728 gfc_namespace *old_ns;
729 gfc_code *c;
730 gfc_symbol *proc;
731 gfc_entry_list *el;
732 /* Provide sufficient space to hold "master.%d.%s". */
733 char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
734 static int master_count = 0;
735
736 if (ns->proc_name == NULL)
737 return;
738
739 /* No need to do anything if this procedure doesn't have alternate entry
740 points. */
741 if (!ns->entries)
742 return;
743
744 /* We may already have resolved alternate entry points. */
745 if (ns->proc_name->attr.entry_master)
746 return;
747
748 /* If this isn't a procedure something has gone horribly wrong. */
749 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
750
751 /* Remember the current namespace. */
752 old_ns = gfc_current_ns;
753
754 gfc_current_ns = ns;
755
756 /* Add the main entry point to the list of entry points. */
757 el = gfc_get_entry_list ();
758 el->sym = ns->proc_name;
759 el->id = 0;
760 el->next = ns->entries;
761 ns->entries = el;
762 ns->proc_name->attr.entry = 1;
763
764 /* If it is a module function, it needs to be in the right namespace
765 so that gfc_get_fake_result_decl can gather up the results. The
766 need for this arose in get_proc_name, where these beasts were
767 left in their own namespace, to keep prior references linked to
768 the entry declaration.*/
769 if (ns->proc_name->attr.function
770 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
771 el->sym->ns = ns;
772
773 /* Do the same for entries where the master is not a module
774 procedure. These are retained in the module namespace because
775 of the module procedure declaration. */
776 for (el = el->next; el; el = el->next)
777 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
778 && el->sym->attr.mod_proc)
779 el->sym->ns = ns;
780 el = ns->entries;
781
782 /* Add an entry statement for it. */
783 c = gfc_get_code (EXEC_ENTRY);
784 c->ext.entry = el;
785 c->next = ns->code;
786 ns->code = c;
787
788 /* Create a new symbol for the master function. */
789 /* Give the internal function a unique name (within this file).
790 Also include the function name so the user has some hope of figuring
791 out what is going on. */
792 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
793 master_count++, ns->proc_name->name);
794 gfc_get_ha_symbol (name, &proc);
795 gcc_assert (proc != NULL);
796
797 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
798 if (ns->proc_name->attr.subroutine)
799 gfc_add_subroutine (&proc->attr, proc->name, NULL);
800 else
801 {
802 gfc_symbol *sym;
803 gfc_typespec *ts, *fts;
804 gfc_array_spec *as, *fas;
805 gfc_add_function (&proc->attr, proc->name, NULL);
806 proc->result = proc;
807 fas = ns->entries->sym->as;
808 fas = fas ? fas : ns->entries->sym->result->as;
809 fts = &ns->entries->sym->result->ts;
810 if (fts->type == BT_UNKNOWN)
811 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
812 for (el = ns->entries->next; el; el = el->next)
813 {
814 ts = &el->sym->result->ts;
815 as = el->sym->as;
816 as = as ? as : el->sym->result->as;
817 if (ts->type == BT_UNKNOWN)
818 ts = gfc_get_default_type (el->sym->result->name, NULL);
819
820 if (! gfc_compare_types (ts, fts)
821 || (el->sym->result->attr.dimension
822 != ns->entries->sym->result->attr.dimension)
823 || (el->sym->result->attr.pointer
824 != ns->entries->sym->result->attr.pointer))
825 break;
826 else if (as && fas && ns->entries->sym->result != el->sym->result
827 && gfc_compare_array_spec (as, fas) == 0)
828 gfc_error ("Function %s at %L has entries with mismatched "
829 "array specifications", ns->entries->sym->name,
830 &ns->entries->sym->declared_at);
831 /* The characteristics need to match and thus both need to have
832 the same string length, i.e. both len=*, or both len=4.
833 Having both len=<variable> is also possible, but difficult to
834 check at compile time. */
835 else if (ts->type == BT_CHARACTER
836 && (el->sym->result->attr.allocatable
837 != ns->entries->sym->result->attr.allocatable))
838 {
839 gfc_error ("Function %s at %L has entry %s with mismatched "
840 "characteristics", ns->entries->sym->name,
841 &ns->entries->sym->declared_at, el->sym->name);
842 goto cleanup;
843 }
844 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
845 && (((ts->u.cl->length && !fts->u.cl->length)
846 ||(!ts->u.cl->length && fts->u.cl->length))
847 || (ts->u.cl->length
848 && ts->u.cl->length->expr_type
849 != fts->u.cl->length->expr_type)
850 || (ts->u.cl->length
851 && ts->u.cl->length->expr_type == EXPR_CONSTANT
852 && mpz_cmp (ts->u.cl->length->value.integer,
853 fts->u.cl->length->value.integer) != 0)))
854 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
855 "entries returning variables of different "
856 "string lengths", ns->entries->sym->name,
857 &ns->entries->sym->declared_at);
858 else if (el->sym->result->attr.allocatable
859 != ns->entries->sym->result->attr.allocatable)
860 break;
861 }
862
863 if (el == NULL)
864 {
865 sym = ns->entries->sym->result;
866 /* All result types the same. */
867 proc->ts = *fts;
868 if (sym->attr.dimension)
869 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
870 if (sym->attr.pointer)
871 gfc_add_pointer (&proc->attr, NULL);
872 if (sym->attr.allocatable)
873 gfc_add_allocatable (&proc->attr, NULL);
874 }
875 else
876 {
877 /* Otherwise the result will be passed through a union by
878 reference. */
879 proc->attr.mixed_entry_master = 1;
880 for (el = ns->entries; el; el = el->next)
881 {
882 sym = el->sym->result;
883 if (sym->attr.dimension)
884 {
885 if (el == ns->entries)
886 gfc_error ("FUNCTION result %s cannot be an array in "
887 "FUNCTION %s at %L", sym->name,
888 ns->entries->sym->name, &sym->declared_at);
889 else
890 gfc_error ("ENTRY result %s cannot be an array in "
891 "FUNCTION %s at %L", sym->name,
892 ns->entries->sym->name, &sym->declared_at);
893 }
894 else if (sym->attr.pointer)
895 {
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be a POINTER in "
898 "FUNCTION %s at %L", sym->name,
899 ns->entries->sym->name, &sym->declared_at);
900 else
901 gfc_error ("ENTRY result %s cannot be a POINTER in "
902 "FUNCTION %s at %L", sym->name,
903 ns->entries->sym->name, &sym->declared_at);
904 }
905 else if (sym->attr.allocatable)
906 {
907 if (el == ns->entries)
908 gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
909 "FUNCTION %s at %L", sym->name,
910 ns->entries->sym->name, &sym->declared_at);
911 else
912 gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
913 "FUNCTION %s at %L", sym->name,
914 ns->entries->sym->name, &sym->declared_at);
915 }
916 else
917 {
918 ts = &sym->ts;
919 if (ts->type == BT_UNKNOWN)
920 ts = gfc_get_default_type (sym->name, NULL);
921 switch (ts->type)
922 {
923 case BT_INTEGER:
924 if (ts->kind == gfc_default_integer_kind)
925 sym = NULL;
926 break;
927 case BT_REAL:
928 if (ts->kind == gfc_default_real_kind
929 || ts->kind == gfc_default_double_kind)
930 sym = NULL;
931 break;
932 case BT_COMPLEX:
933 if (ts->kind == gfc_default_complex_kind)
934 sym = NULL;
935 break;
936 case BT_LOGICAL:
937 if (ts->kind == gfc_default_logical_kind)
938 sym = NULL;
939 break;
940 case BT_UNKNOWN:
941 /* We will issue error elsewhere. */
942 sym = NULL;
943 break;
944 default:
945 break;
946 }
947 if (sym)
948 {
949 if (el == ns->entries)
950 gfc_error ("FUNCTION result %s cannot be of type %s "
951 "in FUNCTION %s at %L", sym->name,
952 gfc_typename (ts), ns->entries->sym->name,
953 &sym->declared_at);
954 else
955 gfc_error ("ENTRY result %s cannot be of type %s "
956 "in FUNCTION %s at %L", sym->name,
957 gfc_typename (ts), ns->entries->sym->name,
958 &sym->declared_at);
959 }
960 }
961 }
962 }
963 }
964
965 cleanup:
966 proc->attr.access = ACCESS_PRIVATE;
967 proc->attr.entry_master = 1;
968
969 /* Merge all the entry point arguments. */
970 for (el = ns->entries; el; el = el->next)
971 merge_argument_lists (proc, el->sym->formal);
972
973 /* Check the master formal arguments for any that are not
974 present in all entry points. */
975 for (el = ns->entries; el; el = el->next)
976 check_argument_lists (proc, el->sym->formal);
977
978 /* Use the master function for the function body. */
979 ns->proc_name = proc;
980
981 /* Finalize the new symbols. */
982 gfc_commit_symbols ();
983
984 /* Restore the original namespace. */
985 gfc_current_ns = old_ns;
986 }
987
988
989 /* Forward declaration. */
990 static bool is_non_constant_shape_array (gfc_symbol *sym);
991
992
993 /* Resolve common variables. */
994 static void
995 resolve_common_vars (gfc_common_head *common_block, bool named_common)
996 {
997 gfc_symbol *csym = common_block->head;
998 gfc_gsymbol *gsym;
999
1000 for (; csym; csym = csym->common_next)
1001 {
1002 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
1003 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
1004 {
1005 if (csym->common_block)
1006 gfc_error_now ("Global entity %qs at %L cannot appear in a "
1007 "COMMON block at %L", gsym->name,
1008 &gsym->where, &csym->common_block->where);
1009 else
1010 gfc_error_now ("Global entity %qs at %L cannot appear in a "
1011 "COMMON block", gsym->name, &gsym->where);
1012 }
1013
1014 /* gfc_add_in_common may have been called before, but the reported errors
1015 have been ignored to continue parsing.
1016 We do the checks again here, unless the symbol is USE associated. */
1017 if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
1018 {
1019 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
1020 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
1021 &common_block->where);
1022 }
1023
1024 if (csym->value || csym->attr.data)
1025 {
1026 if (!csym->ns->is_block_data)
1027 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1028 "but only in BLOCK DATA initialization is "
1029 "allowed", csym->name, &csym->declared_at);
1030 else if (!named_common)
1031 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1032 "in a blank COMMON but initialization is only "
1033 "allowed in named common blocks", csym->name,
1034 &csym->declared_at);
1035 }
1036
1037 if (UNLIMITED_POLY (csym))
1038 gfc_error_now ("%qs at %L cannot appear in COMMON "
1039 "[F2008:C5100]", csym->name, &csym->declared_at);
1040
1041 if (csym->attr.dimension && is_non_constant_shape_array (csym))
1042 {
1043 gfc_error_now ("Automatic object %qs at %L cannot appear in "
1044 "COMMON at %L", csym->name, &csym->declared_at,
1045 &common_block->where);
1046 /* Avoid confusing follow-on error. */
1047 csym->error = 1;
1048 }
1049
1050 if (csym->ts.type != BT_DERIVED)
1051 continue;
1052
1053 if (!(csym->ts.u.derived->attr.sequence
1054 || csym->ts.u.derived->attr.is_bind_c))
1055 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1056 "has neither the SEQUENCE nor the BIND(C) "
1057 "attribute", csym->name, &csym->declared_at);
1058 if (csym->ts.u.derived->attr.alloc_comp)
1059 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1060 "has an ultimate component that is "
1061 "allocatable", csym->name, &csym->declared_at);
1062 if (gfc_has_default_initializer (csym->ts.u.derived))
1063 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1064 "may not have default initializer", csym->name,
1065 &csym->declared_at);
1066
1067 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1068 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1069 }
1070 }
1071
1072 /* Resolve common blocks. */
1073 static void
1074 resolve_common_blocks (gfc_symtree *common_root)
1075 {
1076 gfc_symbol *sym = NULL;
1077 gfc_gsymbol * gsym;
1078
1079 if (common_root == NULL)
1080 return;
1081
1082 if (common_root->left)
1083 resolve_common_blocks (common_root->left);
1084 if (common_root->right)
1085 resolve_common_blocks (common_root->right);
1086
1087 resolve_common_vars (common_root->n.common, true);
1088
1089 /* The common name is a global name - in Fortran 2003 also if it has a
1090 C binding name, since Fortran 2008 only the C binding name is a global
1091 identifier. */
1092 if (!common_root->n.common->binding_label
1093 || gfc_notification_std (GFC_STD_F2008))
1094 {
1095 gsym = gfc_find_gsymbol (gfc_gsym_root,
1096 common_root->n.common->name);
1097
1098 if (gsym && gfc_notification_std (GFC_STD_F2008)
1099 && gsym->type == GSYM_COMMON
1100 && ((common_root->n.common->binding_label
1101 && (!gsym->binding_label
1102 || strcmp (common_root->n.common->binding_label,
1103 gsym->binding_label) != 0))
1104 || (!common_root->n.common->binding_label
1105 && gsym->binding_label)))
1106 {
1107 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1108 "identifier and must thus have the same binding name "
1109 "as the same-named COMMON block at %L: %s vs %s",
1110 common_root->n.common->name, &common_root->n.common->where,
1111 &gsym->where,
1112 common_root->n.common->binding_label
1113 ? common_root->n.common->binding_label : "(blank)",
1114 gsym->binding_label ? gsym->binding_label : "(blank)");
1115 return;
1116 }
1117
1118 if (gsym && gsym->type != GSYM_COMMON
1119 && !common_root->n.common->binding_label)
1120 {
1121 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1122 "as entity at %L",
1123 common_root->n.common->name, &common_root->n.common->where,
1124 &gsym->where);
1125 return;
1126 }
1127 if (gsym && gsym->type != GSYM_COMMON)
1128 {
1129 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1130 "%L sharing the identifier with global non-COMMON-block "
1131 "entity at %L", common_root->n.common->name,
1132 &common_root->n.common->where, &gsym->where);
1133 return;
1134 }
1135 if (!gsym)
1136 {
1137 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1138 gsym->type = GSYM_COMMON;
1139 gsym->where = common_root->n.common->where;
1140 gsym->defined = 1;
1141 }
1142 gsym->used = 1;
1143 }
1144
1145 if (common_root->n.common->binding_label)
1146 {
1147 gsym = gfc_find_gsymbol (gfc_gsym_root,
1148 common_root->n.common->binding_label);
1149 if (gsym && gsym->type != GSYM_COMMON)
1150 {
1151 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1152 "global identifier as entity at %L",
1153 &common_root->n.common->where,
1154 common_root->n.common->binding_label, &gsym->where);
1155 return;
1156 }
1157 if (!gsym)
1158 {
1159 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1160 gsym->type = GSYM_COMMON;
1161 gsym->where = common_root->n.common->where;
1162 gsym->defined = 1;
1163 }
1164 gsym->used = 1;
1165 }
1166
1167 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1168 if (sym == NULL)
1169 return;
1170
1171 if (sym->attr.flavor == FL_PARAMETER)
1172 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1173 sym->name, &common_root->n.common->where, &sym->declared_at);
1174
1175 if (sym->attr.external)
1176 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1177 sym->name, &common_root->n.common->where);
1178
1179 if (sym->attr.intrinsic)
1180 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1181 sym->name, &common_root->n.common->where);
1182 else if (sym->attr.result
1183 || gfc_is_function_return_value (sym, gfc_current_ns))
1184 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1185 "that is also a function result", sym->name,
1186 &common_root->n.common->where);
1187 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1188 && sym->attr.proc != PROC_ST_FUNCTION)
1189 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1190 "that is also a global procedure", sym->name,
1191 &common_root->n.common->where);
1192 }
1193
1194
1195 /* Resolve contained function types. Because contained functions can call one
1196 another, they have to be worked out before any of the contained procedures
1197 can be resolved.
1198
1199 The good news is that if a function doesn't already have a type, the only
1200 way it can get one is through an IMPLICIT type or a RESULT variable, because
1201 by definition contained functions are contained namespace they're contained
1202 in, not in a sibling or parent namespace. */
1203
1204 static void
1205 resolve_contained_functions (gfc_namespace *ns)
1206 {
1207 gfc_namespace *child;
1208 gfc_entry_list *el;
1209
1210 resolve_formal_arglists (ns);
1211
1212 for (child = ns->contained; child; child = child->sibling)
1213 {
1214 /* Resolve alternate entry points first. */
1215 resolve_entries (child);
1216
1217 /* Then check function return types. */
1218 resolve_contained_fntype (child->proc_name, child);
1219 for (el = child->entries; el; el = el->next)
1220 resolve_contained_fntype (el->sym, child);
1221 }
1222 }
1223
1224
1225
1226 /* A Parameterized Derived Type constructor must contain values for
1227 the PDT KIND parameters or they must have a default initializer.
1228 Go through the constructor picking out the KIND expressions,
1229 storing them in 'param_list' and then call gfc_get_pdt_instance
1230 to obtain the PDT instance. */
1231
1232 static gfc_actual_arglist *param_list, *param_tail, *param;
1233
1234 static bool
1235 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1236 {
1237 param = gfc_get_actual_arglist ();
1238 if (!param_list)
1239 param_list = param_tail = param;
1240 else
1241 {
1242 param_tail->next = param;
1243 param_tail = param_tail->next;
1244 }
1245
1246 param_tail->name = c->name;
1247 if (expr)
1248 param_tail->expr = gfc_copy_expr (expr);
1249 else if (c->initializer)
1250 param_tail->expr = gfc_copy_expr (c->initializer);
1251 else
1252 {
1253 param_tail->spec_type = SPEC_ASSUMED;
1254 if (c->attr.pdt_kind)
1255 {
1256 gfc_error ("The KIND parameter %qs in the PDT constructor "
1257 "at %C has no value", param->name);
1258 return false;
1259 }
1260 }
1261
1262 return true;
1263 }
1264
1265 static bool
1266 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1267 gfc_symbol *derived)
1268 {
1269 gfc_constructor *cons = NULL;
1270 gfc_component *comp;
1271 bool t = true;
1272
1273 if (expr && expr->expr_type == EXPR_STRUCTURE)
1274 cons = gfc_constructor_first (expr->value.constructor);
1275 else if (constr)
1276 cons = *constr;
1277 gcc_assert (cons);
1278
1279 comp = derived->components;
1280
1281 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1282 {
1283 if (cons->expr
1284 && cons->expr->expr_type == EXPR_STRUCTURE
1285 && comp->ts.type == BT_DERIVED)
1286 {
1287 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1288 if (!t)
1289 return t;
1290 }
1291 else if (comp->ts.type == BT_DERIVED)
1292 {
1293 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1294 if (!t)
1295 return t;
1296 }
1297 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1298 && derived->attr.pdt_template)
1299 {
1300 t = get_pdt_spec_expr (comp, cons->expr);
1301 if (!t)
1302 return t;
1303 }
1304 }
1305 return t;
1306 }
1307
1308
1309 static bool resolve_fl_derived0 (gfc_symbol *sym);
1310 static bool resolve_fl_struct (gfc_symbol *sym);
1311
1312
1313 /* Resolve all of the elements of a structure constructor and make sure that
1314 the types are correct. The 'init' flag indicates that the given
1315 constructor is an initializer. */
1316
1317 static bool
1318 resolve_structure_cons (gfc_expr *expr, int init)
1319 {
1320 gfc_constructor *cons;
1321 gfc_component *comp;
1322 bool t;
1323 symbol_attribute a;
1324
1325 t = true;
1326
1327 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1328 {
1329 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1330 resolve_fl_derived0 (expr->ts.u.derived);
1331 else
1332 resolve_fl_struct (expr->ts.u.derived);
1333
1334 /* If this is a Parameterized Derived Type template, find the
1335 instance corresponding to the PDT kind parameters. */
1336 if (expr->ts.u.derived->attr.pdt_template)
1337 {
1338 param_list = NULL;
1339 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1340 if (!t)
1341 return t;
1342 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1343
1344 expr->param_list = gfc_copy_actual_arglist (param_list);
1345
1346 if (param_list)
1347 gfc_free_actual_arglist (param_list);
1348
1349 if (!expr->ts.u.derived->attr.pdt_type)
1350 return false;
1351 }
1352 }
1353
1354 /* A constructor may have references if it is the result of substituting a
1355 parameter variable. In this case we just pull out the component we
1356 want. */
1357 if (expr->ref)
1358 comp = expr->ref->u.c.sym->components;
1359 else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1360 || expr->ts.type == BT_UNION)
1361 && expr->ts.u.derived)
1362 comp = expr->ts.u.derived->components;
1363 else
1364 return false;
1365
1366 cons = gfc_constructor_first (expr->value.constructor);
1367
1368 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1369 {
1370 int rank;
1371
1372 if (!cons->expr)
1373 continue;
1374
1375 /* Unions use an EXPR_NULL contrived expression to tell the translation
1376 phase to generate an initializer of the appropriate length.
1377 Ignore it here. */
1378 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1379 continue;
1380
1381 if (!gfc_resolve_expr (cons->expr))
1382 {
1383 t = false;
1384 continue;
1385 }
1386
1387 rank = comp->as ? comp->as->rank : 0;
1388 if (comp->ts.type == BT_CLASS
1389 && !comp->ts.u.derived->attr.unlimited_polymorphic
1390 && CLASS_DATA (comp)->as)
1391 rank = CLASS_DATA (comp)->as->rank;
1392
1393 if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1394 gfc_find_vtab (&cons->expr->ts);
1395
1396 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1397 && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
1398 {
1399 gfc_error ("The rank of the element in the structure "
1400 "constructor at %L does not match that of the "
1401 "component (%d/%d)", &cons->expr->where,
1402 cons->expr->rank, rank);
1403 t = false;
1404 }
1405
1406 /* If we don't have the right type, try to convert it. */
1407
1408 if (!comp->attr.proc_pointer &&
1409 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1410 {
1411 if (strcmp (comp->name, "_extends") == 0)
1412 {
1413 /* Can afford to be brutal with the _extends initializer.
1414 The derived type can get lost because it is PRIVATE
1415 but it is not usage constrained by the standard. */
1416 cons->expr->ts = comp->ts;
1417 }
1418 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1419 {
1420 gfc_error ("The element in the structure constructor at %L, "
1421 "for pointer component %qs, is %s but should be %s",
1422 &cons->expr->where, comp->name,
1423 gfc_basic_typename (cons->expr->ts.type),
1424 gfc_basic_typename (comp->ts.type));
1425 t = false;
1426 }
1427 else if (!UNLIMITED_POLY (comp))
1428 {
1429 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1430 if (t)
1431 t = t2;
1432 }
1433 }
1434
1435 /* For strings, the length of the constructor should be the same as
1436 the one of the structure, ensure this if the lengths are known at
1437 compile time and when we are dealing with PARAMETER or structure
1438 constructors. */
1439 if (cons->expr->ts.type == BT_CHARACTER
1440 && comp->ts.type == BT_CHARACTER
1441 && comp->ts.u.cl && comp->ts.u.cl->length
1442 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1443 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1444 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1445 && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1446 && comp->ts.u.cl->length->ts.type == BT_INTEGER
1447 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1448 comp->ts.u.cl->length->value.integer) != 0)
1449 {
1450 if (comp->attr.pointer)
1451 {
1452 HOST_WIDE_INT la, lb;
1453 la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1454 lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1455 gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1456 "component %qs in constructor at %L",
1457 la, lb, comp->name, &cons->expr->where);
1458 t = false;
1459 }
1460
1461 if (cons->expr->expr_type == EXPR_VARIABLE
1462 && cons->expr->rank != 0
1463 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1464 {
1465 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1466 to make use of the gfc_resolve_character_array_constructor
1467 machinery. The expression is later simplified away to
1468 an array of string literals. */
1469 gfc_expr *para = cons->expr;
1470 cons->expr = gfc_get_expr ();
1471 cons->expr->ts = para->ts;
1472 cons->expr->where = para->where;
1473 cons->expr->expr_type = EXPR_ARRAY;
1474 cons->expr->rank = para->rank;
1475 cons->expr->corank = para->corank;
1476 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1477 gfc_constructor_append_expr (&cons->expr->value.constructor,
1478 para, &cons->expr->where);
1479 }
1480
1481 if (cons->expr->expr_type == EXPR_ARRAY)
1482 {
1483 /* Rely on the cleanup of the namespace to deal correctly with
1484 the old charlen. (There was a block here that attempted to
1485 remove the charlen but broke the chain in so doing.) */
1486 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1487 cons->expr->ts.u.cl->length_from_typespec = true;
1488 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1489 gfc_resolve_character_array_constructor (cons->expr);
1490 }
1491 }
1492
1493 if (cons->expr->expr_type == EXPR_NULL
1494 && !(comp->attr.pointer || comp->attr.allocatable
1495 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1496 || (comp->ts.type == BT_CLASS
1497 && (CLASS_DATA (comp)->attr.class_pointer
1498 || CLASS_DATA (comp)->attr.allocatable))))
1499 {
1500 t = false;
1501 gfc_error ("The NULL in the structure constructor at %L is "
1502 "being applied to component %qs, which is neither "
1503 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1504 comp->name);
1505 }
1506
1507 if (comp->attr.proc_pointer && comp->ts.interface)
1508 {
1509 /* Check procedure pointer interface. */
1510 gfc_symbol *s2 = NULL;
1511 gfc_component *c2;
1512 const char *name;
1513 char err[200];
1514
1515 c2 = gfc_get_proc_ptr_comp (cons->expr);
1516 if (c2)
1517 {
1518 s2 = c2->ts.interface;
1519 name = c2->name;
1520 }
1521 else if (cons->expr->expr_type == EXPR_FUNCTION)
1522 {
1523 s2 = cons->expr->symtree->n.sym->result;
1524 name = cons->expr->symtree->n.sym->result->name;
1525 }
1526 else if (cons->expr->expr_type != EXPR_NULL)
1527 {
1528 s2 = cons->expr->symtree->n.sym;
1529 name = cons->expr->symtree->n.sym->name;
1530 }
1531
1532 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1533 err, sizeof (err), NULL, NULL))
1534 {
1535 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1536 "component %qs in structure constructor at %L:"
1537 " %s", comp->name, &cons->expr->where, err);
1538 return false;
1539 }
1540 }
1541
1542 /* Validate shape, except for dynamic or PDT arrays. */
1543 if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1544 && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1545 && !comp->attr.pdt_array)
1546 {
1547 mpz_t len;
1548 mpz_init (len);
1549 for (int n = 0; n < rank; n++)
1550 {
1551 if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1552 || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1553 {
1554 gfc_error ("Bad array spec of component %qs referenced in "
1555 "structure constructor at %L",
1556 comp->name, &cons->expr->where);
1557 t = false;
1558 break;
1559 };
1560 if (cons->expr->shape == NULL)
1561 continue;
1562 mpz_set_ui (len, 1);
1563 mpz_add (len, len, comp->as->upper[n]->value.integer);
1564 mpz_sub (len, len, comp->as->lower[n]->value.integer);
1565 if (mpz_cmp (cons->expr->shape[n], len) != 0)
1566 {
1567 gfc_error ("The shape of component %qs in the structure "
1568 "constructor at %L differs from the shape of the "
1569 "declared component for dimension %d (%ld/%ld)",
1570 comp->name, &cons->expr->where, n+1,
1571 mpz_get_si (cons->expr->shape[n]),
1572 mpz_get_si (len));
1573 t = false;
1574 }
1575 }
1576 mpz_clear (len);
1577 }
1578
1579 if (!comp->attr.pointer || comp->attr.proc_pointer
1580 || cons->expr->expr_type == EXPR_NULL)
1581 continue;
1582
1583 a = gfc_expr_attr (cons->expr);
1584
1585 if (!a.pointer && !a.target)
1586 {
1587 t = false;
1588 gfc_error ("The element in the structure constructor at %L, "
1589 "for pointer component %qs should be a POINTER or "
1590 "a TARGET", &cons->expr->where, comp->name);
1591 }
1592
1593 if (init)
1594 {
1595 /* F08:C461. Additional checks for pointer initialization. */
1596 if (a.allocatable)
1597 {
1598 t = false;
1599 gfc_error ("Pointer initialization target at %L "
1600 "must not be ALLOCATABLE", &cons->expr->where);
1601 }
1602 if (!a.save)
1603 {
1604 t = false;
1605 gfc_error ("Pointer initialization target at %L "
1606 "must have the SAVE attribute", &cons->expr->where);
1607 }
1608 }
1609
1610 /* F2023:C770: A designator that is an initial-data-target shall ...
1611 not have a vector subscript. */
1612 if (comp->attr.pointer && (a.pointer || a.target)
1613 && gfc_has_vector_index (cons->expr))
1614 {
1615 gfc_error ("Pointer assignment target at %L has a vector subscript",
1616 &cons->expr->where);
1617 t = false;
1618 }
1619
1620 /* F2003, C1272 (3). */
1621 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1622 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1623 || gfc_is_coindexed (cons->expr));
1624 if (impure && gfc_pure (NULL))
1625 {
1626 t = false;
1627 gfc_error ("Invalid expression in the structure constructor for "
1628 "pointer component %qs at %L in PURE procedure",
1629 comp->name, &cons->expr->where);
1630 }
1631
1632 if (impure)
1633 gfc_unset_implicit_pure (NULL);
1634 }
1635
1636 return t;
1637 }
1638
1639
1640 /****************** Expression name resolution ******************/
1641
1642 /* Returns 0 if a symbol was not declared with a type or
1643 attribute declaration statement, nonzero otherwise. */
1644
1645 static bool
1646 was_declared (gfc_symbol *sym)
1647 {
1648 symbol_attribute a;
1649
1650 a = sym->attr;
1651
1652 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1653 return 1;
1654
1655 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1656 || a.optional || a.pointer || a.save || a.target || a.volatile_
1657 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1658 || a.asynchronous || a.codimension || a.subroutine)
1659 return 1;
1660
1661 return 0;
1662 }
1663
1664
1665 /* Determine if a symbol is generic or not. */
1666
1667 static int
1668 generic_sym (gfc_symbol *sym)
1669 {
1670 gfc_symbol *s;
1671
1672 if (sym->attr.generic ||
1673 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1674 return 1;
1675
1676 if (was_declared (sym) || sym->ns->parent == NULL)
1677 return 0;
1678
1679 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1680
1681 if (s != NULL)
1682 {
1683 if (s == sym)
1684 return 0;
1685 else
1686 return generic_sym (s);
1687 }
1688
1689 return 0;
1690 }
1691
1692
1693 /* Determine if a symbol is specific or not. */
1694
1695 static int
1696 specific_sym (gfc_symbol *sym)
1697 {
1698 gfc_symbol *s;
1699
1700 if (sym->attr.if_source == IFSRC_IFBODY
1701 || sym->attr.proc == PROC_MODULE
1702 || sym->attr.proc == PROC_INTERNAL
1703 || sym->attr.proc == PROC_ST_FUNCTION
1704 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1705 || sym->attr.external)
1706 return 1;
1707
1708 if (was_declared (sym) || sym->ns->parent == NULL)
1709 return 0;
1710
1711 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1712
1713 return (s == NULL) ? 0 : specific_sym (s);
1714 }
1715
1716
1717 /* Figure out if the procedure is specific, generic or unknown. */
1718
1719 enum proc_type
1720 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1721
1722 static proc_type
1723 procedure_kind (gfc_symbol *sym)
1724 {
1725 if (generic_sym (sym))
1726 return PTYPE_GENERIC;
1727
1728 if (specific_sym (sym))
1729 return PTYPE_SPECIFIC;
1730
1731 return PTYPE_UNKNOWN;
1732 }
1733
1734 /* Check references to assumed size arrays. The flag need_full_assumed_size
1735 is nonzero when matching actual arguments. */
1736
1737 static int need_full_assumed_size = 0;
1738
1739 static bool
1740 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1741 {
1742 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1743 return false;
1744
1745 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1746 What should it be? */
1747 if (e->ref
1748 && e->ref->u.ar.as
1749 && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1750 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1751 && (e->ref->u.ar.type == AR_FULL))
1752 {
1753 gfc_error ("The upper bound in the last dimension must "
1754 "appear in the reference to the assumed size "
1755 "array %qs at %L", sym->name, &e->where);
1756 return true;
1757 }
1758 return false;
1759 }
1760
1761
1762 /* Look for bad assumed size array references in argument expressions
1763 of elemental and array valued intrinsic procedures. Since this is
1764 called from procedure resolution functions, it only recurses at
1765 operators. */
1766
1767 static bool
1768 resolve_assumed_size_actual (gfc_expr *e)
1769 {
1770 if (e == NULL)
1771 return false;
1772
1773 switch (e->expr_type)
1774 {
1775 case EXPR_VARIABLE:
1776 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1777 return true;
1778 break;
1779
1780 case EXPR_OP:
1781 if (resolve_assumed_size_actual (e->value.op.op1)
1782 || resolve_assumed_size_actual (e->value.op.op2))
1783 return true;
1784 break;
1785
1786 default:
1787 break;
1788 }
1789 return false;
1790 }
1791
1792
1793 /* Check a generic procedure, passed as an actual argument, to see if
1794 there is a matching specific name. If none, it is an error, and if
1795 more than one, the reference is ambiguous. */
1796 static int
1797 count_specific_procs (gfc_expr *e)
1798 {
1799 int n;
1800 gfc_interface *p;
1801 gfc_symbol *sym;
1802
1803 n = 0;
1804 sym = e->symtree->n.sym;
1805
1806 for (p = sym->generic; p; p = p->next)
1807 if (strcmp (sym->name, p->sym->name) == 0)
1808 {
1809 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1810 sym->name);
1811 n++;
1812 }
1813
1814 if (n > 1)
1815 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1816 &e->where);
1817
1818 if (n == 0)
1819 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1820 "argument at %L", sym->name, &e->where);
1821
1822 return n;
1823 }
1824
1825
1826 /* See if a call to sym could possibly be a not allowed RECURSION because of
1827 a missing RECURSIVE declaration. This means that either sym is the current
1828 context itself, or sym is the parent of a contained procedure calling its
1829 non-RECURSIVE containing procedure.
1830 This also works if sym is an ENTRY. */
1831
1832 static bool
1833 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1834 {
1835 gfc_symbol* proc_sym;
1836 gfc_symbol* context_proc;
1837 gfc_namespace* real_context;
1838
1839 if (sym->attr.flavor == FL_PROGRAM
1840 || gfc_fl_struct (sym->attr.flavor))
1841 return false;
1842
1843 /* If we've got an ENTRY, find real procedure. */
1844 if (sym->attr.entry && sym->ns->entries)
1845 proc_sym = sym->ns->entries->sym;
1846 else
1847 proc_sym = sym;
1848
1849 /* If sym is RECURSIVE, all is well of course. */
1850 if (proc_sym->attr.recursive || flag_recursive)
1851 return false;
1852
1853 /* Find the context procedure's "real" symbol if it has entries.
1854 We look for a procedure symbol, so recurse on the parents if we don't
1855 find one (like in case of a BLOCK construct). */
1856 for (real_context = context; ; real_context = real_context->parent)
1857 {
1858 /* We should find something, eventually! */
1859 gcc_assert (real_context);
1860
1861 context_proc = (real_context->entries ? real_context->entries->sym
1862 : real_context->proc_name);
1863
1864 /* In some special cases, there may not be a proc_name, like for this
1865 invalid code:
1866 real(bad_kind()) function foo () ...
1867 when checking the call to bad_kind ().
1868 In these cases, we simply return here and assume that the
1869 call is ok. */
1870 if (!context_proc)
1871 return false;
1872
1873 if (context_proc->attr.flavor != FL_LABEL)
1874 break;
1875 }
1876
1877 /* A call from sym's body to itself is recursion, of course. */
1878 if (context_proc == proc_sym)
1879 return true;
1880
1881 /* The same is true if context is a contained procedure and sym the
1882 containing one. */
1883 if (context_proc->attr.contained)
1884 {
1885 gfc_symbol* parent_proc;
1886
1887 gcc_assert (context->parent);
1888 parent_proc = (context->parent->entries ? context->parent->entries->sym
1889 : context->parent->proc_name);
1890
1891 if (parent_proc == proc_sym)
1892 return true;
1893 }
1894
1895 return false;
1896 }
1897
1898
1899 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1900 its typespec and formal argument list. */
1901
1902 bool
1903 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1904 {
1905 gfc_intrinsic_sym* isym = NULL;
1906 const char* symstd;
1907
1908 if (sym->resolve_symbol_called >= 2)
1909 return true;
1910
1911 sym->resolve_symbol_called = 2;
1912
1913 /* Already resolved. */
1914 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1915 return true;
1916
1917 /* We already know this one is an intrinsic, so we don't call
1918 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1919 gfc_find_subroutine directly to check whether it is a function or
1920 subroutine. */
1921
1922 if (sym->intmod_sym_id && sym->attr.subroutine)
1923 {
1924 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1925 isym = gfc_intrinsic_subroutine_by_id (id);
1926 }
1927 else if (sym->intmod_sym_id)
1928 {
1929 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1930 isym = gfc_intrinsic_function_by_id (id);
1931 }
1932 else if (!sym->attr.subroutine)
1933 isym = gfc_find_function (sym->name);
1934
1935 if (isym && !sym->attr.subroutine)
1936 {
1937 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1938 && !sym->attr.implicit_type)
1939 gfc_warning (OPT_Wsurprising,
1940 "Type specified for intrinsic function %qs at %L is"
1941 " ignored", sym->name, &sym->declared_at);
1942
1943 if (!sym->attr.function &&
1944 !gfc_add_function(&sym->attr, sym->name, loc))
1945 return false;
1946
1947 sym->ts = isym->ts;
1948 }
1949 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1950 {
1951 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1952 {
1953 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1954 " specifier", sym->name, &sym->declared_at);
1955 return false;
1956 }
1957
1958 if (!sym->attr.subroutine &&
1959 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1960 return false;
1961 }
1962 else
1963 {
1964 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1965 &sym->declared_at);
1966 return false;
1967 }
1968
1969 gfc_copy_formal_args_intr (sym, isym, NULL);
1970
1971 sym->attr.pure = isym->pure;
1972 sym->attr.elemental = isym->elemental;
1973
1974 /* Check it is actually available in the standard settings. */
1975 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1976 {
1977 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1978 "available in the current standard settings but %s. Use "
1979 "an appropriate %<-std=*%> option or enable "
1980 "%<-fall-intrinsics%> in order to use it.",
1981 sym->name, &sym->declared_at, symstd);
1982 return false;
1983 }
1984
1985 return true;
1986 }
1987
1988
1989 /* Resolve a procedure expression, like passing it to a called procedure or as
1990 RHS for a procedure pointer assignment. */
1991
1992 static bool
1993 resolve_procedure_expression (gfc_expr* expr)
1994 {
1995 gfc_symbol* sym;
1996
1997 if (expr->expr_type != EXPR_VARIABLE)
1998 return true;
1999 gcc_assert (expr->symtree);
2000
2001 sym = expr->symtree->n.sym;
2002
2003 if (sym->attr.intrinsic)
2004 gfc_resolve_intrinsic (sym, &expr->where);
2005
2006 if (sym->attr.flavor != FL_PROCEDURE
2007 || (sym->attr.function && sym->result == sym))
2008 return true;
2009
2010 /* A non-RECURSIVE procedure that is used as procedure expression within its
2011 own body is in danger of being called recursively. */
2012 if (is_illegal_recursion (sym, gfc_current_ns))
2013 {
2014 if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
2015 gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
2016 " possibly calling itself recursively in procedure %qs. "
2017 " Declare it RECURSIVE or use %<-frecursive%>",
2018 sym->name, sym->module, gfc_current_ns->proc_name->name);
2019 else
2020 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
2021 " itself recursively. Declare it RECURSIVE or use"
2022 " %<-frecursive%>", sym->name, &expr->where);
2023 }
2024
2025 return true;
2026 }
2027
2028
2029 /* Check that name is not a derived type. */
2030
2031 static bool
2032 is_dt_name (const char *name)
2033 {
2034 gfc_symbol *dt_list, *dt_first;
2035
2036 dt_list = dt_first = gfc_derived_types;
2037 for (; dt_list; dt_list = dt_list->dt_next)
2038 {
2039 if (strcmp(dt_list->name, name) == 0)
2040 return true;
2041 if (dt_first == dt_list->dt_next)
2042 break;
2043 }
2044 return false;
2045 }
2046
2047
2048 /* Resolve an actual argument list. Most of the time, this is just
2049 resolving the expressions in the list.
2050 The exception is that we sometimes have to decide whether arguments
2051 that look like procedure arguments are really simple variable
2052 references. */
2053
2054 static bool
2055 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2056 bool no_formal_args)
2057 {
2058 gfc_symbol *sym;
2059 gfc_symtree *parent_st;
2060 gfc_expr *e;
2061 gfc_component *comp;
2062 int save_need_full_assumed_size;
2063 bool return_value = false;
2064 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2065
2066 actual_arg = true;
2067 first_actual_arg = true;
2068
2069 for (; arg; arg = arg->next)
2070 {
2071 e = arg->expr;
2072 if (e == NULL)
2073 {
2074 /* Check the label is a valid branching target. */
2075 if (arg->label)
2076 {
2077 if (arg->label->defined == ST_LABEL_UNKNOWN)
2078 {
2079 gfc_error ("Label %d referenced at %L is never defined",
2080 arg->label->value, &arg->label->where);
2081 goto cleanup;
2082 }
2083 }
2084 first_actual_arg = false;
2085 continue;
2086 }
2087
2088 if (e->expr_type == EXPR_VARIABLE
2089 && e->symtree->n.sym->attr.generic
2090 && no_formal_args
2091 && count_specific_procs (e) != 1)
2092 goto cleanup;
2093
2094 if (e->ts.type != BT_PROCEDURE)
2095 {
2096 save_need_full_assumed_size = need_full_assumed_size;
2097 if (e->expr_type != EXPR_VARIABLE)
2098 need_full_assumed_size = 0;
2099 if (!gfc_resolve_expr (e))
2100 goto cleanup;
2101 need_full_assumed_size = save_need_full_assumed_size;
2102 goto argument_list;
2103 }
2104
2105 /* See if the expression node should really be a variable reference. */
2106
2107 sym = e->symtree->n.sym;
2108
2109 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2110 {
2111 gfc_error ("Derived type %qs is used as an actual "
2112 "argument at %L", sym->name, &e->where);
2113 goto cleanup;
2114 }
2115
2116 if (sym->attr.flavor == FL_PROCEDURE
2117 || sym->attr.intrinsic
2118 || sym->attr.external)
2119 {
2120 int actual_ok;
2121
2122 /* If a procedure is not already determined to be something else
2123 check if it is intrinsic. */
2124 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2125 sym->attr.intrinsic = 1;
2126
2127 if (sym->attr.proc == PROC_ST_FUNCTION)
2128 {
2129 gfc_error ("Statement function %qs at %L is not allowed as an "
2130 "actual argument", sym->name, &e->where);
2131 }
2132
2133 actual_ok = gfc_intrinsic_actual_ok (sym->name,
2134 sym->attr.subroutine);
2135 if (sym->attr.intrinsic && actual_ok == 0)
2136 {
2137 gfc_error ("Intrinsic %qs at %L is not allowed as an "
2138 "actual argument", sym->name, &e->where);
2139 }
2140
2141 if (sym->attr.contained && !sym->attr.use_assoc
2142 && sym->ns->proc_name->attr.flavor != FL_MODULE)
2143 {
2144 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2145 " used as actual argument at %L",
2146 sym->name, &e->where))
2147 goto cleanup;
2148 }
2149
2150 if (sym->attr.elemental && !sym->attr.intrinsic)
2151 {
2152 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2153 "allowed as an actual argument at %L", sym->name,
2154 &e->where);
2155 }
2156
2157 /* Check if a generic interface has a specific procedure
2158 with the same name before emitting an error. */
2159 if (sym->attr.generic && count_specific_procs (e) != 1)
2160 goto cleanup;
2161
2162 /* Just in case a specific was found for the expression. */
2163 sym = e->symtree->n.sym;
2164
2165 /* If the symbol is the function that names the current (or
2166 parent) scope, then we really have a variable reference. */
2167
2168 if (gfc_is_function_return_value (sym, sym->ns))
2169 goto got_variable;
2170
2171 /* If all else fails, see if we have a specific intrinsic. */
2172 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2173 {
2174 gfc_intrinsic_sym *isym;
2175
2176 isym = gfc_find_function (sym->name);
2177 if (isym == NULL || !isym->specific)
2178 {
2179 gfc_error ("Unable to find a specific INTRINSIC procedure "
2180 "for the reference %qs at %L", sym->name,
2181 &e->where);
2182 goto cleanup;
2183 }
2184 sym->ts = isym->ts;
2185 sym->attr.intrinsic = 1;
2186 sym->attr.function = 1;
2187 }
2188
2189 if (!gfc_resolve_expr (e))
2190 goto cleanup;
2191 goto argument_list;
2192 }
2193
2194 /* See if the name is a module procedure in a parent unit. */
2195
2196 if (was_declared (sym) || sym->ns->parent == NULL)
2197 goto got_variable;
2198
2199 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2200 {
2201 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2202 goto cleanup;
2203 }
2204
2205 if (parent_st == NULL)
2206 goto got_variable;
2207
2208 sym = parent_st->n.sym;
2209 e->symtree = parent_st; /* Point to the right thing. */
2210
2211 if (sym->attr.flavor == FL_PROCEDURE
2212 || sym->attr.intrinsic
2213 || sym->attr.external)
2214 {
2215 if (!gfc_resolve_expr (e))
2216 goto cleanup;
2217 goto argument_list;
2218 }
2219
2220 got_variable:
2221 e->expr_type = EXPR_VARIABLE;
2222 e->ts = sym->ts;
2223 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2224 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2225 && CLASS_DATA (sym)->as))
2226 {
2227 gfc_array_spec *as
2228 = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2229 e->rank = as->rank;
2230 e->corank = as->corank;
2231 e->ref = gfc_get_ref ();
2232 e->ref->type = REF_ARRAY;
2233 e->ref->u.ar.type = AR_FULL;
2234 e->ref->u.ar.as = as;
2235 }
2236
2237 /* These symbols are set untyped by calls to gfc_set_default_type
2238 with 'error_flag' = false. Reset the untyped attribute so that
2239 the error will be generated in gfc_resolve_expr. */
2240 if (e->expr_type == EXPR_VARIABLE
2241 && sym->ts.type == BT_UNKNOWN
2242 && sym->attr.untyped)
2243 sym->attr.untyped = 0;
2244
2245 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2246 primary.cc (match_actual_arg). If above code determines that it
2247 is a variable instead, it needs to be resolved as it was not
2248 done at the beginning of this function. */
2249 save_need_full_assumed_size = need_full_assumed_size;
2250 if (e->expr_type != EXPR_VARIABLE)
2251 need_full_assumed_size = 0;
2252 if (!gfc_resolve_expr (e))
2253 goto cleanup;
2254 need_full_assumed_size = save_need_full_assumed_size;
2255
2256 argument_list:
2257 /* Check argument list functions %VAL, %LOC and %REF. There is
2258 nothing to do for %REF. */
2259 if (arg->name && arg->name[0] == '%')
2260 {
2261 if (strcmp ("%VAL", arg->name) == 0)
2262 {
2263 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2264 {
2265 gfc_error ("By-value argument at %L is not of numeric "
2266 "type", &e->where);
2267 goto cleanup;
2268 }
2269
2270 if (e->rank)
2271 {
2272 gfc_error ("By-value argument at %L cannot be an array or "
2273 "an array section", &e->where);
2274 goto cleanup;
2275 }
2276
2277 /* Intrinsics are still PROC_UNKNOWN here. However,
2278 since same file external procedures are not resolvable
2279 in gfortran, it is a good deal easier to leave them to
2280 intrinsic.cc. */
2281 if (ptype != PROC_UNKNOWN
2282 && ptype != PROC_DUMMY
2283 && ptype != PROC_EXTERNAL
2284 && ptype != PROC_MODULE)
2285 {
2286 gfc_error ("By-value argument at %L is not allowed "
2287 "in this context", &e->where);
2288 goto cleanup;
2289 }
2290 }
2291
2292 /* Statement functions have already been excluded above. */
2293 else if (strcmp ("%LOC", arg->name) == 0
2294 && e->ts.type == BT_PROCEDURE)
2295 {
2296 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2297 {
2298 gfc_error ("Passing internal procedure at %L by location "
2299 "not allowed", &e->where);
2300 goto cleanup;
2301 }
2302 }
2303 }
2304
2305 comp = gfc_get_proc_ptr_comp(e);
2306 if (e->expr_type == EXPR_VARIABLE
2307 && comp && comp->attr.elemental)
2308 {
2309 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2310 "allowed as an actual argument at %L", comp->name,
2311 &e->where);
2312 }
2313
2314 /* Fortran 2008, C1237. */
2315 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2316 && gfc_has_ultimate_pointer (e))
2317 {
2318 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2319 "component", &e->where);
2320 goto cleanup;
2321 }
2322
2323 first_actual_arg = false;
2324 }
2325
2326 return_value = true;
2327
2328 cleanup:
2329 actual_arg = actual_arg_sav;
2330 first_actual_arg = first_actual_arg_sav;
2331
2332 return return_value;
2333 }
2334
2335
2336 /* Do the checks of the actual argument list that are specific to elemental
2337 procedures. If called with c == NULL, we have a function, otherwise if
2338 expr == NULL, we have a subroutine. */
2339
2340 static bool
2341 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2342 {
2343 gfc_actual_arglist *arg0;
2344 gfc_actual_arglist *arg;
2345 gfc_symbol *esym = NULL;
2346 gfc_intrinsic_sym *isym = NULL;
2347 gfc_expr *e = NULL;
2348 gfc_intrinsic_arg *iformal = NULL;
2349 gfc_formal_arglist *eformal = NULL;
2350 bool formal_optional = false;
2351 bool set_by_optional = false;
2352 int i;
2353 int rank = 0;
2354
2355 /* Is this an elemental procedure? */
2356 if (expr && expr->value.function.actual != NULL)
2357 {
2358 if (expr->value.function.esym != NULL
2359 && expr->value.function.esym->attr.elemental)
2360 {
2361 arg0 = expr->value.function.actual;
2362 esym = expr->value.function.esym;
2363 }
2364 else if (expr->value.function.isym != NULL
2365 && expr->value.function.isym->elemental)
2366 {
2367 arg0 = expr->value.function.actual;
2368 isym = expr->value.function.isym;
2369 }
2370 else
2371 return true;
2372 }
2373 else if (c && c->ext.actual != NULL)
2374 {
2375 arg0 = c->ext.actual;
2376
2377 if (c->resolved_sym)
2378 esym = c->resolved_sym;
2379 else
2380 esym = c->symtree->n.sym;
2381 gcc_assert (esym);
2382
2383 if (!esym->attr.elemental)
2384 return true;
2385 }
2386 else
2387 return true;
2388
2389 /* The rank of an elemental is the rank of its array argument(s). */
2390 for (arg = arg0; arg; arg = arg->next)
2391 {
2392 if (arg->expr != NULL && arg->expr->rank != 0)
2393 {
2394 rank = arg->expr->rank;
2395 if (arg->expr->expr_type == EXPR_VARIABLE
2396 && arg->expr->symtree->n.sym->attr.optional)
2397 set_by_optional = true;
2398
2399 /* Function specific; set the result rank and shape. */
2400 if (expr)
2401 {
2402 expr->rank = rank;
2403 expr->corank = arg->expr->corank;
2404 if (!expr->shape && arg->expr->shape)
2405 {
2406 expr->shape = gfc_get_shape (rank);
2407 for (i = 0; i < rank; i++)
2408 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2409 }
2410 }
2411 break;
2412 }
2413 }
2414
2415 /* If it is an array, it shall not be supplied as an actual argument
2416 to an elemental procedure unless an array of the same rank is supplied
2417 as an actual argument corresponding to a nonoptional dummy argument of
2418 that elemental procedure(12.4.1.5). */
2419 formal_optional = false;
2420 if (isym)
2421 iformal = isym->formal;
2422 else
2423 eformal = esym->formal;
2424
2425 for (arg = arg0; arg; arg = arg->next)
2426 {
2427 if (eformal)
2428 {
2429 if (eformal->sym && eformal->sym->attr.optional)
2430 formal_optional = true;
2431 eformal = eformal->next;
2432 }
2433 else if (isym && iformal)
2434 {
2435 if (iformal->optional)
2436 formal_optional = true;
2437 iformal = iformal->next;
2438 }
2439 else if (isym)
2440 formal_optional = true;
2441
2442 if (pedantic && arg->expr != NULL
2443 && arg->expr->expr_type == EXPR_VARIABLE
2444 && arg->expr->symtree->n.sym->attr.optional
2445 && formal_optional
2446 && arg->expr->rank
2447 && (set_by_optional || arg->expr->rank != rank)
2448 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2449 {
2450 bool t = false;
2451 gfc_actual_arglist *a;
2452
2453 /* Scan the argument list for a non-optional argument with the
2454 same rank as arg. */
2455 for (a = arg0; a; a = a->next)
2456 if (a != arg
2457 && a->expr->rank == arg->expr->rank
2458 && (a->expr->expr_type != EXPR_VARIABLE
2459 || (a->expr->expr_type == EXPR_VARIABLE
2460 && !a->expr->symtree->n.sym->attr.optional)))
2461 {
2462 t = true;
2463 break;
2464 }
2465
2466 if (!t)
2467 gfc_warning (OPT_Wpedantic,
2468 "%qs at %L is an array and OPTIONAL; If it is not "
2469 "present, then it cannot be the actual argument of "
2470 "an ELEMENTAL procedure unless there is a non-optional"
2471 " argument with the same rank "
2472 "(Fortran 2018, 15.5.2.12)",
2473 arg->expr->symtree->n.sym->name, &arg->expr->where);
2474 }
2475 }
2476
2477 for (arg = arg0; arg; arg = arg->next)
2478 {
2479 if (arg->expr == NULL || arg->expr->rank == 0)
2480 continue;
2481
2482 /* Being elemental, the last upper bound of an assumed size array
2483 argument must be present. */
2484 if (resolve_assumed_size_actual (arg->expr))
2485 return false;
2486
2487 /* Elemental procedure's array actual arguments must conform. */
2488 if (e != NULL)
2489 {
2490 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2491 return false;
2492 }
2493 else
2494 e = arg->expr;
2495 }
2496
2497 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2498 is an array, the intent inout/out variable needs to be also an array. */
2499 if (rank > 0 && esym && expr == NULL)
2500 for (eformal = esym->formal, arg = arg0; arg && eformal;
2501 arg = arg->next, eformal = eformal->next)
2502 if (eformal->sym
2503 && (eformal->sym->attr.intent == INTENT_OUT
2504 || eformal->sym->attr.intent == INTENT_INOUT)
2505 && arg->expr && arg->expr->rank == 0)
2506 {
2507 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2508 "ELEMENTAL subroutine %qs is a scalar, but another "
2509 "actual argument is an array", &arg->expr->where,
2510 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2511 : "INOUT", eformal->sym->name, esym->name);
2512 return false;
2513 }
2514 return true;
2515 }
2516
2517
2518 /* This function does the checking of references to global procedures
2519 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2520 77 and 95 standards. It checks for a gsymbol for the name, making
2521 one if it does not already exist. If it already exists, then the
2522 reference being resolved must correspond to the type of gsymbol.
2523 Otherwise, the new symbol is equipped with the attributes of the
2524 reference. The corresponding code that is called in creating
2525 global entities is parse.cc.
2526
2527 In addition, for all but -std=legacy, the gsymbols are used to
2528 check the interfaces of external procedures from the same file.
2529 The namespace of the gsymbol is resolved and then, once this is
2530 done the interface is checked. */
2531
2532
2533 static bool
2534 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2535 {
2536 if (!gsym_ns->proc_name->attr.recursive)
2537 return true;
2538
2539 if (sym->ns == gsym_ns)
2540 return false;
2541
2542 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2543 return false;
2544
2545 return true;
2546 }
2547
2548 static bool
2549 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2550 {
2551 if (gsym_ns->entries)
2552 {
2553 gfc_entry_list *entry = gsym_ns->entries;
2554
2555 for (; entry; entry = entry->next)
2556 {
2557 if (strcmp (sym->name, entry->sym->name) == 0)
2558 {
2559 if (strcmp (gsym_ns->proc_name->name,
2560 sym->ns->proc_name->name) == 0)
2561 return false;
2562
2563 if (sym->ns->parent
2564 && strcmp (gsym_ns->proc_name->name,
2565 sym->ns->parent->proc_name->name) == 0)
2566 return false;
2567 }
2568 }
2569 }
2570 return true;
2571 }
2572
2573
2574 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2575
2576 bool
2577 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2578 {
2579 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2580
2581 for ( ; arg; arg = arg->next)
2582 {
2583 if (!arg->sym)
2584 continue;
2585
2586 if (arg->sym->attr.allocatable) /* (2a) */
2587 {
2588 strncpy (errmsg, _("allocatable argument"), err_len);
2589 return true;
2590 }
2591 else if (arg->sym->attr.asynchronous)
2592 {
2593 strncpy (errmsg, _("asynchronous argument"), err_len);
2594 return true;
2595 }
2596 else if (arg->sym->attr.optional)
2597 {
2598 strncpy (errmsg, _("optional argument"), err_len);
2599 return true;
2600 }
2601 else if (arg->sym->attr.pointer)
2602 {
2603 strncpy (errmsg, _("pointer argument"), err_len);
2604 return true;
2605 }
2606 else if (arg->sym->attr.target)
2607 {
2608 strncpy (errmsg, _("target argument"), err_len);
2609 return true;
2610 }
2611 else if (arg->sym->attr.value)
2612 {
2613 strncpy (errmsg, _("value argument"), err_len);
2614 return true;
2615 }
2616 else if (arg->sym->attr.volatile_)
2617 {
2618 strncpy (errmsg, _("volatile argument"), err_len);
2619 return true;
2620 }
2621 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2622 {
2623 strncpy (errmsg, _("assumed-shape argument"), err_len);
2624 return true;
2625 }
2626 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2627 {
2628 strncpy (errmsg, _("assumed-rank argument"), err_len);
2629 return true;
2630 }
2631 else if (arg->sym->attr.codimension) /* (2c) */
2632 {
2633 strncpy (errmsg, _("coarray argument"), err_len);
2634 return true;
2635 }
2636 else if (false) /* (2d) TODO: parametrized derived type */
2637 {
2638 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2639 return true;
2640 }
2641 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2642 {
2643 strncpy (errmsg, _("polymorphic argument"), err_len);
2644 return true;
2645 }
2646 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2647 {
2648 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2649 return true;
2650 }
2651 else if (arg->sym->ts.type == BT_ASSUMED)
2652 {
2653 /* As assumed-type is unlimited polymorphic (cf. above).
2654 See also TS 29113, Note 6.1. */
2655 strncpy (errmsg, _("assumed-type argument"), err_len);
2656 return true;
2657 }
2658 }
2659
2660 if (sym->attr.function)
2661 {
2662 gfc_symbol *res = sym->result ? sym->result : sym;
2663
2664 if (res->attr.dimension) /* (3a) */
2665 {
2666 strncpy (errmsg, _("array result"), err_len);
2667 return true;
2668 }
2669 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2670 {
2671 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2672 return true;
2673 }
2674 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2675 && res->ts.u.cl->length
2676 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2677 {
2678 strncpy (errmsg, _("result with non-constant character length"), err_len);
2679 return true;
2680 }
2681 }
2682
2683 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2684 {
2685 strncpy (errmsg, _("elemental procedure"), err_len);
2686 return true;
2687 }
2688 else if (sym->attr.is_bind_c) /* (5) */
2689 {
2690 strncpy (errmsg, _("bind(c) procedure"), err_len);
2691 return true;
2692 }
2693
2694 return false;
2695 }
2696
2697
2698 static void
2699 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2700 {
2701 gfc_gsymbol * gsym;
2702 gfc_namespace *ns;
2703 enum gfc_symbol_type type;
2704 char reason[200];
2705
2706 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2707
2708 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2709 sym->binding_label != NULL);
2710
2711 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2712 gfc_global_used (gsym, where);
2713
2714 if ((sym->attr.if_source == IFSRC_UNKNOWN
2715 || sym->attr.if_source == IFSRC_IFBODY)
2716 && gsym->type != GSYM_UNKNOWN
2717 && !gsym->binding_label
2718 && gsym->ns
2719 && gsym->ns->proc_name
2720 && not_in_recursive (sym, gsym->ns)
2721 && not_entry_self_reference (sym, gsym->ns))
2722 {
2723 gfc_symbol *def_sym;
2724 def_sym = gsym->ns->proc_name;
2725
2726 if (gsym->ns->resolved != -1)
2727 {
2728
2729 /* Resolve the gsymbol namespace if needed. */
2730 if (!gsym->ns->resolved)
2731 {
2732 gfc_symbol *old_dt_list;
2733
2734 /* Stash away derived types so that the backend_decls
2735 do not get mixed up. */
2736 old_dt_list = gfc_derived_types;
2737 gfc_derived_types = NULL;
2738
2739 gfc_resolve (gsym->ns);
2740
2741 /* Store the new derived types with the global namespace. */
2742 if (gfc_derived_types)
2743 gsym->ns->derived_types = gfc_derived_types;
2744
2745 /* Restore the derived types of this namespace. */
2746 gfc_derived_types = old_dt_list;
2747 }
2748
2749 /* Make sure that translation for the gsymbol occurs before
2750 the procedure currently being resolved. */
2751 ns = gfc_global_ns_list;
2752 for (; ns && ns != gsym->ns; ns = ns->sibling)
2753 {
2754 if (ns->sibling == gsym->ns)
2755 {
2756 ns->sibling = gsym->ns->sibling;
2757 gsym->ns->sibling = gfc_global_ns_list;
2758 gfc_global_ns_list = gsym->ns;
2759 break;
2760 }
2761 }
2762
2763 /* This can happen if a binding name has been specified. */
2764 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2765 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2766 }
2767
2768 /* Look up the specific entry symbol so that interface checks use
2769 the entry's own formal argument list, not the entry master's.
2770 This must run even when resolved == -1 (recursive resolution in
2771 progress), because def_sym starts as the namespace proc_name
2772 which is the entry master with the combined formals. */
2773 if (def_sym->attr.entry_master || def_sym->attr.entry)
2774 {
2775 gfc_entry_list *entry;
2776 for (entry = gsym->ns->entries; entry; entry = entry->next)
2777 if (strcmp (entry->sym->name, sym->name) == 0)
2778 {
2779 def_sym = entry->sym;
2780 break;
2781 }
2782 }
2783
2784 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2785 {
2786 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2787 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2788 gfc_typename (&def_sym->ts));
2789 goto done;
2790 }
2791
2792 if (sym->attr.if_source == IFSRC_UNKNOWN
2793 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2794 {
2795 gfc_error ("Explicit interface required for %qs at %L: %s",
2796 sym->name, &sym->declared_at, reason);
2797 goto done;
2798 }
2799
2800 bool bad_result_characteristics;
2801 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2802 reason, sizeof(reason), NULL, NULL,
2803 &bad_result_characteristics))
2804 {
2805 /* Turn erros into warnings with -std=gnu and -std=legacy,
2806 unless a function returns a wrong type, which can lead
2807 to all kinds of ICEs and wrong code. */
2808
2809 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2810 && !bad_result_characteristics)
2811 gfc_errors_to_warnings (true);
2812
2813 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2814 sym->name, &sym->declared_at, reason);
2815 sym->error = 1;
2816 gfc_errors_to_warnings (false);
2817 goto done;
2818 }
2819 }
2820
2821 done:
2822
2823 if (gsym->type == GSYM_UNKNOWN)
2824 {
2825 gsym->type = type;
2826 gsym->where = *where;
2827 }
2828
2829 gsym->used = 1;
2830 }
2831
2832
2833 /************* Function resolution *************/
2834
2835 /* Resolve a function call known to be generic.
2836 Section 14.1.2.4.1. */
2837
2838 static match
2839 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2840 {
2841 gfc_symbol *s;
2842
2843 if (sym->attr.generic)
2844 {
2845 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2846 if (s != NULL)
2847 {
2848 expr->value.function.name = s->name;
2849 expr->value.function.esym = s;
2850
2851 if (s->ts.type != BT_UNKNOWN)
2852 expr->ts = s->ts;
2853 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2854 expr->ts = s->result->ts;
2855
2856 if (s->as != NULL)
2857 {
2858 expr->rank = s->as->rank;
2859 expr->corank = s->as->corank;
2860 }
2861 else if (s->result != NULL && s->result->as != NULL)
2862 {
2863 expr->rank = s->result->as->rank;
2864 expr->corank = s->result->as->corank;
2865 }
2866
2867 gfc_set_sym_referenced (expr->value.function.esym);
2868
2869 return MATCH_YES;
2870 }
2871
2872 /* TODO: Need to search for elemental references in generic
2873 interface. */
2874 }
2875
2876 if (sym->attr.intrinsic)
2877 return gfc_intrinsic_func_interface (expr, 0);
2878
2879 return MATCH_NO;
2880 }
2881
2882
2883 static bool
2884 resolve_generic_f (gfc_expr *expr)
2885 {
2886 gfc_symbol *sym;
2887 match m;
2888 gfc_interface *intr = NULL;
2889
2890 sym = expr->symtree->n.sym;
2891
2892 for (;;)
2893 {
2894 m = resolve_generic_f0 (expr, sym);
2895 if (m == MATCH_YES)
2896 return true;
2897 else if (m == MATCH_ERROR)
2898 return false;
2899
2900 generic:
2901 if (!intr)
2902 for (intr = sym->generic; intr; intr = intr->next)
2903 if (gfc_fl_struct (intr->sym->attr.flavor))
2904 break;
2905
2906 if (sym->ns->parent == NULL)
2907 break;
2908 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2909
2910 if (sym == NULL)
2911 break;
2912 if (!generic_sym (sym))
2913 goto generic;
2914 }
2915
2916 /* Last ditch attempt. See if the reference is to an intrinsic
2917 that possesses a matching interface. 14.1.2.4 */
2918 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2919 {
2920 if (gfc_init_expr_flag)
2921 gfc_error ("Function %qs in initialization expression at %L "
2922 "must be an intrinsic function",
2923 expr->symtree->n.sym->name, &expr->where);
2924 else
2925 gfc_error ("There is no specific function for the generic %qs "
2926 "at %L", expr->symtree->n.sym->name, &expr->where);
2927 return false;
2928 }
2929
2930 if (intr)
2931 {
2932 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2933 NULL, false))
2934 return false;
2935 if (!gfc_use_derived (expr->ts.u.derived))
2936 return false;
2937 return resolve_structure_cons (expr, 0);
2938 }
2939
2940 m = gfc_intrinsic_func_interface (expr, 0);
2941 if (m == MATCH_YES)
2942 return true;
2943
2944 if (m == MATCH_NO)
2945 gfc_error ("Generic function %qs at %L is not consistent with a "
2946 "specific intrinsic interface", expr->symtree->n.sym->name,
2947 &expr->where);
2948
2949 return false;
2950 }
2951
2952
2953 /* Resolve a function call known to be specific. */
2954
2955 static match
2956 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2957 {
2958 match m;
2959
2960 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2961 {
2962 if (sym->attr.dummy)
2963 {
2964 sym->attr.proc = PROC_DUMMY;
2965 goto found;
2966 }
2967
2968 sym->attr.proc = PROC_EXTERNAL;
2969 goto found;
2970 }
2971
2972 if (sym->attr.proc == PROC_MODULE
2973 || sym->attr.proc == PROC_ST_FUNCTION
2974 || sym->attr.proc == PROC_INTERNAL)
2975 goto found;
2976
2977 if (sym->attr.intrinsic)
2978 {
2979 m = gfc_intrinsic_func_interface (expr, 1);
2980 if (m == MATCH_YES)
2981 return MATCH_YES;
2982 if (m == MATCH_NO)
2983 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2984 "with an intrinsic", sym->name, &expr->where);
2985
2986 return MATCH_ERROR;
2987 }
2988
2989 return MATCH_NO;
2990
2991 found:
2992 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2993
2994 if (sym->result)
2995 expr->ts = sym->result->ts;
2996 else
2997 expr->ts = sym->ts;
2998 expr->value.function.name = sym->name;
2999 expr->value.function.esym = sym;
3000 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
3001 error(s). */
3002 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
3003 return MATCH_ERROR;
3004 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
3005 {
3006 expr->rank = CLASS_DATA (sym)->as->rank;
3007 expr->corank = CLASS_DATA (sym)->as->corank;
3008 }
3009 else if (sym->as != NULL)
3010 {
3011 expr->rank = sym->as->rank;
3012 expr->corank = sym->as->corank;
3013 }
3014
3015 return MATCH_YES;
3016 }
3017
3018
3019 static bool
3020 resolve_specific_f (gfc_expr *expr)
3021 {
3022 gfc_symbol *sym;
3023 match m;
3024
3025 sym = expr->symtree->n.sym;
3026
3027 for (;;)
3028 {
3029 m = resolve_specific_f0 (sym, expr);
3030 if (m == MATCH_YES)
3031 return true;
3032 if (m == MATCH_ERROR)
3033 return false;
3034
3035 if (sym->ns->parent == NULL)
3036 break;
3037
3038 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3039
3040 if (sym == NULL)
3041 break;
3042 }
3043
3044 gfc_error ("Unable to resolve the specific function %qs at %L",
3045 expr->symtree->n.sym->name, &expr->where);
3046
3047 return true;
3048 }
3049
3050 /* Recursively append candidate SYM to CANDIDATES. Store the number of
3051 candidates in CANDIDATES_LEN. */
3052
3053 static void
3054 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3055 char **&candidates,
3056 size_t &candidates_len)
3057 {
3058 gfc_symtree *p;
3059
3060 if (sym == NULL)
3061 return;
3062 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3063 && sym->n.sym->attr.flavor == FL_PROCEDURE)
3064 vec_push (candidates, candidates_len, sym->name);
3065
3066 p = sym->left;
3067 if (p)
3068 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3069
3070 p = sym->right;
3071 if (p)
3072 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3073 }
3074
3075
3076 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3077
3078 const char*
3079 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3080 {
3081 char **candidates = NULL;
3082 size_t candidates_len = 0;
3083 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3084 return gfc_closest_fuzzy_match (fn, candidates);
3085 }
3086
3087
3088 /* Resolve a procedure call not known to be generic nor specific. */
3089
3090 static bool
3091 resolve_unknown_f (gfc_expr *expr)
3092 {
3093 gfc_symbol *sym;
3094 gfc_typespec *ts;
3095
3096 sym = expr->symtree->n.sym;
3097
3098 if (sym->attr.dummy)
3099 {
3100 sym->attr.proc = PROC_DUMMY;
3101 expr->value.function.name = sym->name;
3102 goto set_type;
3103 }
3104
3105 /* See if we have an intrinsic function reference. */
3106
3107 if (gfc_is_intrinsic (sym, 0, expr->where))
3108 {
3109 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3110 return true;
3111 return false;
3112 }
3113
3114 /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3115 /* Intrinsics were handled above, only non-intrinsics left here. */
3116 if (sym->attr.flavor == FL_PROCEDURE
3117 && sym->attr.implicit_type
3118 && sym->ns
3119 && sym->ns->has_implicit_none_export)
3120 {
3121 gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3122 "for symbol %qs at %L", sym->name, &sym->declared_at);
3123 sym->error = 1;
3124 return false;
3125 }
3126
3127 /* The reference is to an external name. */
3128
3129 sym->attr.proc = PROC_EXTERNAL;
3130 expr->value.function.name = sym->name;
3131 expr->value.function.esym = expr->symtree->n.sym;
3132
3133 if (sym->as != NULL)
3134 {
3135 expr->rank = sym->as->rank;
3136 expr->corank = sym->as->corank;
3137 }
3138
3139 /* Type of the expression is either the type of the symbol or the
3140 default type of the symbol. */
3141
3142 set_type:
3143 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3144
3145 if (sym->ts.type != BT_UNKNOWN)
3146 expr->ts = sym->ts;
3147 else
3148 {
3149 ts = gfc_get_default_type (sym->name, sym->ns);
3150
3151 if (ts->type == BT_UNKNOWN)
3152 {
3153 const char *guessed
3154 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3155 if (guessed)
3156 gfc_error ("Function %qs at %L has no IMPLICIT type"
3157 "; did you mean %qs?",
3158 sym->name, &expr->where, guessed);
3159 else
3160 gfc_error ("Function %qs at %L has no IMPLICIT type",
3161 sym->name, &expr->where);
3162 return false;
3163 }
3164 else
3165 expr->ts = *ts;
3166 }
3167
3168 return true;
3169 }
3170
3171
3172 /* Return true, if the symbol is an external procedure. */
3173 static bool
3174 is_external_proc (gfc_symbol *sym)
3175 {
3176 if (!sym->attr.dummy && !sym->attr.contained
3177 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3178 && sym->attr.proc != PROC_ST_FUNCTION
3179 && !sym->attr.proc_pointer
3180 && !sym->attr.use_assoc
3181 && sym->name)
3182 return true;
3183
3184 return false;
3185 }
3186
3187
3188 /* Figure out if a function reference is pure or not. Also set the name
3189 of the function for a potential error message. Return nonzero if the
3190 function is PURE, zero if not. */
3191 static bool
3192 pure_stmt_function (gfc_expr *, gfc_symbol *);
3193
3194 bool
3195 gfc_pure_function (gfc_expr *e, const char **name)
3196 {
3197 bool pure;
3198 gfc_component *comp;
3199
3200 *name = NULL;
3201
3202 if (e->symtree != NULL
3203 && e->symtree->n.sym != NULL
3204 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3205 return pure_stmt_function (e, e->symtree->n.sym);
3206
3207 comp = gfc_get_proc_ptr_comp (e);
3208 if (comp)
3209 {
3210 pure = gfc_pure (comp->ts.interface);
3211 *name = comp->name;
3212 }
3213 else if (e->value.function.esym)
3214 {
3215 pure = gfc_pure (e->value.function.esym);
3216 *name = e->value.function.esym->name;
3217 }
3218 else if (e->value.function.isym)
3219 {
3220 pure = e->value.function.isym->pure
3221 || e->value.function.isym->elemental;
3222 *name = e->value.function.isym->name;
3223 }
3224 else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
3225 {
3226 /* The function has been resolved, but esym is not yet set.
3227 This can happen with functions as dummy argument. */
3228 pure = e->symtree->n.sym->attr.pure;
3229 *name = e->symtree->n.sym->name;
3230 }
3231 else
3232 {
3233 /* Implicit functions are not pure. */
3234 pure = 0;
3235 *name = e->value.function.name;
3236 }
3237
3238 return pure;
3239 }
3240
3241
3242 /* Check if the expression is a reference to an implicitly pure function. */
3243
3244 bool
3245 gfc_implicit_pure_function (gfc_expr *e)
3246 {
3247 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3248 if (comp)
3249 return gfc_implicit_pure (comp->ts.interface);
3250 else if (e->value.function.esym)
3251 return gfc_implicit_pure (e->value.function.esym);
3252 else
3253 return 0;
3254 }
3255
3256
3257 static bool
3258 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3259 int *f ATTRIBUTE_UNUSED)
3260 {
3261 const char *name;
3262
3263 /* Don't bother recursing into other statement functions
3264 since they will be checked individually for purity. */
3265 if (e->expr_type != EXPR_FUNCTION
3266 || !e->symtree
3267 || e->symtree->n.sym == sym
3268 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3269 return false;
3270
3271 return gfc_pure_function (e, &name) ? false : true;
3272 }
3273
3274
3275 static bool
3276 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3277 {
3278 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3279 }
3280
3281
3282 /* Check if an impure function is allowed in the current context. */
3283
3284 static bool check_pure_function (gfc_expr *e)
3285 {
3286 const char *name = NULL;
3287 code_stack *stack;
3288 bool saw_block = false;
3289
3290 /* A BLOCK construct within a DO CONCURRENT construct leads to
3291 gfc_do_concurrent_flag = 0 when the check for an impure function
3292 occurs. Check the stack to see if the source code has a nested
3293 BLOCK construct. */
3294
3295 for (stack = cs_base; stack; stack = stack->prev)
3296 {
3297 if (!saw_block && stack->current->op == EXEC_BLOCK)
3298 {
3299 saw_block = true;
3300 continue;
3301 }
3302
3303 if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3304 {
3305 bool is_pure;
3306 is_pure = (e->value.function.isym
3307 && (e->value.function.isym->pure
3308 || e->value.function.isym->elemental))
3309 || (e->value.function.esym
3310 && (e->value.function.esym->attr.pure
3311 || e->value.function.esym->attr.elemental));
3312 if (!is_pure)
3313 {
3314 gfc_error ("Reference to impure function at %L inside a "
3315 "DO CONCURRENT", &e->where);
3316 return false;
3317 }
3318 }
3319 }
3320
3321 if (!gfc_pure_function (e, &name) && name)
3322 {
3323 if (forall_flag)
3324 {
3325 gfc_error ("Reference to impure function %qs at %L inside a "
3326 "FORALL %s", name, &e->where,
3327 forall_flag == 2 ? "mask" : "block");
3328 return false;
3329 }
3330 else if (gfc_do_concurrent_flag)
3331 {
3332 gfc_error ("Reference to impure function %qs at %L inside a "
3333 "DO CONCURRENT %s", name, &e->where,
3334 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3335 return false;
3336 }
3337 else if (gfc_pure (NULL))
3338 {
3339 gfc_error ("Reference to impure function %qs at %L "
3340 "within a PURE procedure", name, &e->where);
3341 return false;
3342 }
3343 if (!gfc_implicit_pure_function (e))
3344 gfc_unset_implicit_pure (NULL);
3345 }
3346 return true;
3347 }
3348
3349
3350 /* Update current procedure's array_outer_dependency flag, considering
3351 a call to procedure SYM. */
3352
3353 static void
3354 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3355 {
3356 /* Check to see if this is a sibling function that has not yet
3357 been resolved. */
3358 gfc_namespace *sibling = gfc_current_ns->sibling;
3359 for (; sibling; sibling = sibling->sibling)
3360 {
3361 if (sibling->proc_name == sym)
3362 {
3363 gfc_resolve (sibling);
3364 break;
3365 }
3366 }
3367
3368 /* If SYM has references to outer arrays, so has the procedure calling
3369 SYM. If SYM is a procedure pointer, we can assume the worst. */
3370 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3371 && gfc_current_ns->proc_name)
3372 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3373 }
3374
3375
3376 /* Resolve a function call, which means resolving the arguments, then figuring
3377 out which entity the name refers to. */
3378
3379 static bool
3380 resolve_function (gfc_expr *expr)
3381 {
3382 gfc_actual_arglist *arg;
3383 gfc_symbol *sym;
3384 bool t;
3385 int temp;
3386 procedure_type p = PROC_INTRINSIC;
3387 bool no_formal_args;
3388
3389 sym = NULL;
3390 if (expr->symtree)
3391 sym = expr->symtree->n.sym;
3392
3393 /* If this is a procedure pointer component, it has already been resolved. */
3394 if (gfc_is_proc_ptr_comp (expr))
3395 return true;
3396
3397 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3398 another caf_get. */
3399 if (sym && sym->attr.intrinsic
3400 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3401 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3402 return true;
3403
3404 if (expr->ref)
3405 {
3406 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3407 &expr->where);
3408 return false;
3409 }
3410
3411 if (sym && sym->attr.intrinsic
3412 && !gfc_resolve_intrinsic (sym, &expr->where))
3413 return false;
3414
3415 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3416 {
3417 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3418 return false;
3419 }
3420
3421 /* If this is a deferred TBP with an abstract interface (which may
3422 of course be referenced), expr->value.function.esym will be set. */
3423 if (sym && sym->attr.abstract && !expr->value.function.esym)
3424 {
3425 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3426 sym->name, &expr->where);
3427 return false;
3428 }
3429
3430 /* If this is a deferred TBP with an abstract interface, its result
3431 cannot be an assumed length character (F2003: C418). */
3432 if (sym && sym->attr.abstract && sym->attr.function
3433 && sym->result->ts.u.cl
3434 && sym->result->ts.u.cl->length == NULL
3435 && !sym->result->ts.deferred)
3436 {
3437 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3438 "character length result (F2008: C418)", sym->name,
3439 &sym->declared_at);
3440 return false;
3441 }
3442
3443 /* Switch off assumed size checking and do this again for certain kinds
3444 of procedure, once the procedure itself is resolved. */
3445 need_full_assumed_size++;
3446
3447 if (expr->symtree && expr->symtree->n.sym)
3448 p = expr->symtree->n.sym->attr.proc;
3449
3450 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3451 inquiry_argument = true;
3452 no_formal_args = sym && is_external_proc (sym)
3453 && gfc_sym_get_dummy_args (sym) == NULL;
3454
3455 if (!resolve_actual_arglist (expr->value.function.actual,
3456 p, no_formal_args))
3457 {
3458 inquiry_argument = false;
3459 return false;
3460 }
3461
3462 inquiry_argument = false;
3463
3464 /* Resume assumed_size checking. */
3465 need_full_assumed_size--;
3466
3467 /* If the procedure is external, check for usage. */
3468 if (sym && is_external_proc (sym))
3469 resolve_global_procedure (sym, &expr->where, 0);
3470
3471 if (sym && sym->ts.type == BT_CHARACTER
3472 && sym->ts.u.cl
3473 && sym->ts.u.cl->length == NULL
3474 && !sym->attr.dummy
3475 && !sym->ts.deferred
3476 && expr->value.function.esym == NULL
3477 && !sym->attr.contained)
3478 {
3479 /* Internal procedures are taken care of in resolve_contained_fntype. */
3480 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3481 "be used at %L since it is not a dummy argument",
3482 sym->name, &expr->where);
3483 return false;
3484 }
3485
3486 /* Add and check formal interface when -fc-prototypes-external is in
3487 force, see comment in resolve_call(). */
3488
3489 if (warn_external_argument_mismatch && sym && sym->attr.dummy
3490 && sym->attr.external)
3491 {
3492 if (sym->formal)
3493 {
3494 bool conflict;
3495 conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
3496 sym->formal, 0, 0, 0, NULL);
3497 if (conflict)
3498 {
3499 sym->ext_dummy_arglist_mismatch = 1;
3500 gfc_warning (OPT_Wexternal_argument_mismatch,
3501 "Different argument lists in external dummy "
3502 "function %s at %L and %L", sym->name,
3503 &expr->where, &sym->formal_at);
3504 }
3505 }
3506 else if (!sym->formal_resolved)
3507 {
3508 gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
3509 sym->formal_at = expr->where;
3510 }
3511 }
3512 /* See if function is already resolved. */
3513
3514 if (expr->value.function.name != NULL
3515 || expr->value.function.isym != NULL)
3516 {
3517 if (expr->ts.type == BT_UNKNOWN)
3518 expr->ts = sym->ts;
3519 t = true;
3520 }
3521 else
3522 {
3523 /* Apply the rules of section 14.1.2. */
3524
3525 switch (procedure_kind (sym))
3526 {
3527 case PTYPE_GENERIC:
3528 t = resolve_generic_f (expr);
3529 break;
3530
3531 case PTYPE_SPECIFIC:
3532 t = resolve_specific_f (expr);
3533 break;
3534
3535 case PTYPE_UNKNOWN:
3536 t = resolve_unknown_f (expr);
3537 break;
3538
3539 default:
3540 gfc_internal_error ("resolve_function(): bad function type");
3541 }
3542 }
3543
3544 /* If the expression is still a function (it might have simplified),
3545 then we check to see if we are calling an elemental function. */
3546
3547 if (expr->expr_type != EXPR_FUNCTION)
3548 return t;
3549
3550 /* Walk the argument list looking for invalid BOZ. */
3551 for (arg = expr->value.function.actual; arg; arg = arg->next)
3552 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3553 {
3554 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3555 "actual argument in a function reference",
3556 &arg->expr->where);
3557 return false;
3558 }
3559
3560 temp = need_full_assumed_size;
3561 need_full_assumed_size = 0;
3562
3563 if (!resolve_elemental_actual (expr, NULL))
3564 return false;
3565
3566 if (omp_workshare_flag
3567 && expr->value.function.esym
3568 && ! gfc_elemental (expr->value.function.esym))
3569 {
3570 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3571 "in WORKSHARE construct", expr->value.function.esym->name,
3572 &expr->where);
3573 t = false;
3574 }
3575
3576 #define GENERIC_ID expr->value.function.isym->id
3577 else if (expr->value.function.actual != NULL
3578 && expr->value.function.isym != NULL
3579 && GENERIC_ID != GFC_ISYM_LBOUND
3580 && GENERIC_ID != GFC_ISYM_LCOBOUND
3581 && GENERIC_ID != GFC_ISYM_UCOBOUND
3582 && GENERIC_ID != GFC_ISYM_LEN
3583 && GENERIC_ID != GFC_ISYM_LOC
3584 && GENERIC_ID != GFC_ISYM_C_LOC
3585 && GENERIC_ID != GFC_ISYM_PRESENT)
3586 {
3587 /* Array intrinsics must also have the last upper bound of an
3588 assumed size array argument. UBOUND and SIZE have to be
3589 excluded from the check if the second argument is anything
3590 than a constant. */
3591
3592 for (arg = expr->value.function.actual; arg; arg = arg->next)
3593 {
3594 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3595 && arg == expr->value.function.actual
3596 && arg->next != NULL && arg->next->expr)
3597 {
3598 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3599 break;
3600
3601 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3602 break;
3603
3604 if ((int)mpz_get_si (arg->next->expr->value.integer)
3605 < arg->expr->rank)
3606 break;
3607 }
3608
3609 if (arg->expr != NULL
3610 && arg->expr->rank > 0
3611 && resolve_assumed_size_actual (arg->expr))
3612 return false;
3613 }
3614 }
3615 #undef GENERIC_ID
3616
3617 need_full_assumed_size = temp;
3618
3619 if (!check_pure_function(expr))
3620 t = false;
3621
3622 /* Functions without the RECURSIVE attribution are not allowed to
3623 * call themselves. */
3624 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3625 {
3626 gfc_symbol *esym;
3627 esym = expr->value.function.esym;
3628
3629 if (is_illegal_recursion (esym, gfc_current_ns))
3630 {
3631 if (esym->attr.entry && esym->ns->entries)
3632 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3633 " function %qs is not RECURSIVE",
3634 esym->name, &expr->where, esym->ns->entries->sym->name);
3635 else
3636 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3637 " is not RECURSIVE", esym->name, &expr->where);
3638
3639 t = false;
3640 }
3641 }
3642
3643 /* Character lengths of use associated functions may contains references to
3644 symbols not referenced from the current program unit otherwise. Make sure
3645 those symbols are marked as referenced. */
3646
3647 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3648 && expr->value.function.esym->attr.use_assoc)
3649 {
3650 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3651 }
3652
3653 /* Make sure that the expression has a typespec that works. */
3654 if (expr->ts.type == BT_UNKNOWN)
3655 {
3656 if (expr->symtree->n.sym->result
3657 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3658 && !expr->symtree->n.sym->result->attr.proc_pointer)
3659 expr->ts = expr->symtree->n.sym->result->ts;
3660 }
3661
3662 /* These derived types with an incomplete namespace, arising from use
3663 association, cause gfc_get_derived_vtab to segfault. If the function
3664 namespace does not suffice, something is badly wrong. */
3665 if (expr->ts.type == BT_DERIVED
3666 && !expr->ts.u.derived->ns->proc_name)
3667 {
3668 gfc_symbol *der;
3669 gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3670 if (der)
3671 {
3672 expr->ts.u.derived->refs--;
3673 expr->ts.u.derived = der;
3674 der->refs++;
3675 }
3676 else
3677 expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3678 }
3679
3680 if (!expr->ref && !expr->value.function.isym)
3681 {
3682 if (expr->value.function.esym)
3683 update_current_proc_array_outer_dependency (expr->value.function.esym);
3684 else
3685 update_current_proc_array_outer_dependency (sym);
3686 }
3687 else if (expr->ref)
3688 /* typebound procedure: Assume the worst. */
3689 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3690
3691 if (expr->value.function.esym
3692 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3693 gfc_warning (OPT_Wdeprecated_declarations,
3694 "Using function %qs at %L is deprecated",
3695 sym->name, &expr->where);
3696 return t;
3697 }
3698
3699
3700 /************* Subroutine resolution *************/
3701
3702 static bool
3703 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3704 {
3705 code_stack *stack;
3706 bool saw_block = false;
3707
3708 if (gfc_pure (sym))
3709 return true;
3710
3711 /* A BLOCK construct within a DO CONCURRENT construct leads to
3712 gfc_do_concurrent_flag = 0 when the check for an impure subroutine
3713 occurs. Walk up the stack to see if the source code has a nested
3714 construct. */
3715
3716 for (stack = cs_base; stack; stack = stack->prev)
3717 {
3718 if (stack->current->op == EXEC_BLOCK)
3719 {
3720 saw_block = true;
3721 continue;
3722 }
3723
3724 if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3725 {
3726
3727 bool is_pure = true;
3728 is_pure = sym->attr.pure || sym->attr.elemental;
3729
3730 if (!is_pure)
3731 {
3732 gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
3733 "is not PURE", loc);
3734 return false;
3735 }
3736 }
3737 }
3738
3739 if (forall_flag)
3740 {
3741 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3742 name, loc);
3743 return false;
3744 }
3745 else if (gfc_do_concurrent_flag)
3746 {
3747 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3748 "PURE", name, loc);
3749 return false;
3750 }
3751 else if (gfc_pure (NULL))
3752 {
3753 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3754 return false;
3755 }
3756
3757 gfc_unset_implicit_pure (NULL);
3758 return true;
3759 }
3760
3761
3762 static match
3763 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3764 {
3765 gfc_symbol *s;
3766
3767 if (sym->attr.generic)
3768 {
3769 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3770 if (s != NULL)
3771 {
3772 c->resolved_sym = s;
3773 if (!pure_subroutine (s, s->name, &c->loc))
3774 return MATCH_ERROR;
3775 return MATCH_YES;
3776 }
3777
3778 /* TODO: Need to search for elemental references in generic interface. */
3779 }
3780
3781 if (sym->attr.intrinsic)
3782 return gfc_intrinsic_sub_interface (c, 0);
3783
3784 return MATCH_NO;
3785 }
3786
3787
3788 static bool
3789 resolve_generic_s (gfc_code *c)
3790 {
3791 gfc_symbol *sym;
3792 match m;
3793
3794 sym = c->symtree->n.sym;
3795
3796 for (;;)
3797 {
3798 m = resolve_generic_s0 (c, sym);
3799 if (m == MATCH_YES)
3800 return true;
3801 else if (m == MATCH_ERROR)
3802 return false;
3803
3804 generic:
3805 if (sym->ns->parent == NULL)
3806 break;
3807 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3808
3809 if (sym == NULL)
3810 break;
3811 if (!generic_sym (sym))
3812 goto generic;
3813 }
3814
3815 /* Last ditch attempt. See if the reference is to an intrinsic
3816 that possesses a matching interface. 14.1.2.4 */
3817 sym = c->symtree->n.sym;
3818
3819 if (!gfc_is_intrinsic (sym, 1, c->loc))
3820 {
3821 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3822 sym->name, &c->loc);
3823 return false;
3824 }
3825
3826 m = gfc_intrinsic_sub_interface (c, 0);
3827 if (m == MATCH_YES)
3828 return true;
3829 if (m == MATCH_NO)
3830 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3831 "intrinsic subroutine interface", sym->name, &c->loc);
3832
3833 return false;
3834 }
3835
3836
3837 /* Resolve a subroutine call known to be specific. */
3838
3839 static match
3840 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3841 {
3842 match m;
3843
3844 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3845 {
3846 if (sym->attr.dummy)
3847 {
3848 sym->attr.proc = PROC_DUMMY;
3849 goto found;
3850 }
3851
3852 sym->attr.proc = PROC_EXTERNAL;
3853 goto found;
3854 }
3855
3856 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3857 goto found;
3858
3859 if (sym->attr.intrinsic)
3860 {
3861 m = gfc_intrinsic_sub_interface (c, 1);
3862 if (m == MATCH_YES)
3863 return MATCH_YES;
3864 if (m == MATCH_NO)
3865 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3866 "with an intrinsic", sym->name, &c->loc);
3867
3868 return MATCH_ERROR;
3869 }
3870
3871 return MATCH_NO;
3872
3873 found:
3874 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3875
3876 c->resolved_sym = sym;
3877 if (!pure_subroutine (sym, sym->name, &c->loc))
3878 return MATCH_ERROR;
3879
3880 return MATCH_YES;
3881 }
3882
3883
3884 static bool
3885 resolve_specific_s (gfc_code *c)
3886 {
3887 gfc_symbol *sym;
3888 match m;
3889
3890 sym = c->symtree->n.sym;
3891
3892 for (;;)
3893 {
3894 m = resolve_specific_s0 (c, sym);
3895 if (m == MATCH_YES)
3896 return true;
3897 if (m == MATCH_ERROR)
3898 return false;
3899
3900 if (sym->ns->parent == NULL)
3901 break;
3902
3903 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3904
3905 if (sym == NULL)
3906 break;
3907 }
3908
3909 sym = c->symtree->n.sym;
3910 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3911 sym->name, &c->loc);
3912
3913 return false;
3914 }
3915
3916
3917 /* Resolve a subroutine call not known to be generic nor specific. */
3918
3919 static bool
3920 resolve_unknown_s (gfc_code *c)
3921 {
3922 gfc_symbol *sym;
3923
3924 sym = c->symtree->n.sym;
3925
3926 if (sym->attr.dummy)
3927 {
3928 sym->attr.proc = PROC_DUMMY;
3929 goto found;
3930 }
3931
3932 /* See if we have an intrinsic function reference. */
3933
3934 if (gfc_is_intrinsic (sym, 1, c->loc))
3935 {
3936 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3937 return true;
3938 return false;
3939 }
3940
3941 /* The reference is to an external name. */
3942
3943 found:
3944 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3945
3946 c->resolved_sym = sym;
3947
3948 return pure_subroutine (sym, sym->name, &c->loc);
3949 }
3950
3951
3952 /* Resolve a subroutine call. Although it was tempting to use the same code
3953 for functions, subroutines and functions are stored differently and this
3954 makes things awkward. */
3955
3956 static bool
3957 resolve_call (gfc_code *c)
3958 {
3959 bool t;
3960 procedure_type ptype = PROC_INTRINSIC;
3961 gfc_symbol *csym, *sym;
3962 bool no_formal_args;
3963
3964 csym = c->symtree ? c->symtree->n.sym : NULL;
3965
3966 if (csym && csym->ts.type != BT_UNKNOWN)
3967 {
3968 gfc_error ("%qs at %L has a type, which is not consistent with "
3969 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3970 return false;
3971 }
3972
3973 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3974 {
3975 gfc_symtree *st;
3976 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3977 sym = st ? st->n.sym : NULL;
3978 if (sym && csym != sym
3979 && sym->ns == gfc_current_ns
3980 && sym->attr.flavor == FL_PROCEDURE
3981 && sym->attr.contained)
3982 {
3983 sym->refs++;
3984 if (csym->attr.generic)
3985 c->symtree->n.sym = sym;
3986 else
3987 c->symtree = st;
3988 csym = c->symtree->n.sym;
3989 }
3990 }
3991
3992 /* If this ia a deferred TBP, c->expr1 will be set. */
3993 if (!c->expr1 && csym)
3994 {
3995 if (csym->attr.abstract)
3996 {
3997 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3998 csym->name, &c->loc);
3999 return false;
4000 }
4001
4002 /* Subroutines without the RECURSIVE attribution are not allowed to
4003 call themselves. */
4004 if (is_illegal_recursion (csym, gfc_current_ns))
4005 {
4006 if (csym->attr.entry && csym->ns->entries)
4007 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
4008 "as subroutine %qs is not RECURSIVE",
4009 csym->name, &c->loc, csym->ns->entries->sym->name);
4010 else
4011 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
4012 "as it is not RECURSIVE", csym->name, &c->loc);
4013
4014 t = false;
4015 }
4016 }
4017
4018 /* Switch off assumed size checking and do this again for certain kinds
4019 of procedure, once the procedure itself is resolved. */
4020 need_full_assumed_size++;
4021
4022 if (csym)
4023 ptype = csym->attr.proc;
4024
4025 no_formal_args = csym && is_external_proc (csym)
4026 && gfc_sym_get_dummy_args (csym) == NULL;
4027 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
4028 return false;
4029
4030 /* Resume assumed_size checking. */
4031 need_full_assumed_size--;
4032
4033 /* If external, check for usage. */
4034 if (csym && is_external_proc (csym))
4035 resolve_global_procedure (csym, &c->loc, 1);
4036
4037 /* If we have an external dummy argument, we want to write out its arguments
4038 with -fc-prototypes-external. Code like
4039
4040 subroutine foo(a,n)
4041 external a
4042 if (n == 1) call a(1)
4043 if (n == 2) call a(2,3)
4044 end subroutine foo
4045
4046 is actually legal Fortran, but it is not possible to generate a C23-
4047 compliant prototype for this, so we just record the fact here and
4048 handle that during -fc-prototypes-external processing. */
4049
4050 if (warn_external_argument_mismatch && csym && csym->attr.dummy
4051 && csym->attr.external)
4052 {
4053 if (csym->formal)
4054 {
4055 bool conflict;
4056 conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4057 0, 0, 0, NULL);
4058 if (conflict)
4059 {
4060 csym->ext_dummy_arglist_mismatch = 1;
4061 gfc_warning (OPT_Wexternal_argument_mismatch,
4062 "Different argument lists in external dummy "
4063 "subroutine %s at %L and %L", csym->name,
4064 &c->loc, &csym->formal_at);
4065 }
4066 }
4067 else if (!csym->formal_resolved)
4068 {
4069 gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4070 csym->formal_at = c->loc;
4071 }
4072 }
4073
4074 t = true;
4075 if (c->resolved_sym == NULL)
4076 {
4077 c->resolved_isym = NULL;
4078 switch (procedure_kind (csym))
4079 {
4080 case PTYPE_GENERIC:
4081 t = resolve_generic_s (c);
4082 break;
4083
4084 case PTYPE_SPECIFIC:
4085 t = resolve_specific_s (c);
4086 break;
4087
4088 case PTYPE_UNKNOWN:
4089 t = resolve_unknown_s (c);
4090 break;
4091
4092 default:
4093 gfc_internal_error ("resolve_subroutine(): bad function type");
4094 }
4095 }
4096
4097 /* Some checks of elemental subroutine actual arguments. */
4098 if (!resolve_elemental_actual (NULL, c))
4099 return false;
4100
4101 if (!c->expr1)
4102 update_current_proc_array_outer_dependency (csym);
4103 else
4104 /* Typebound procedure: Assume the worst. */
4105 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4106
4107 if (c->resolved_sym
4108 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4109 gfc_warning (OPT_Wdeprecated_declarations,
4110 "Using subroutine %qs at %L is deprecated",
4111 c->resolved_sym->name, &c->loc);
4112
4113 return t;
4114 }
4115
4116
4117 /* Compare the shapes of two arrays that have non-NULL shapes. If both
4118 op1->shape and op2->shape are non-NULL return true if their shapes
4119 match. If both op1->shape and op2->shape are non-NULL return false
4120 if their shapes do not match. If either op1->shape or op2->shape is
4121 NULL, return true. */
4122
4123 static bool
4124 compare_shapes (gfc_expr *op1, gfc_expr *op2)
4125 {
4126 bool t;
4127 int i;
4128
4129 t = true;
4130
4131 if (op1->shape != NULL && op2->shape != NULL)
4132 {
4133 for (i = 0; i < op1->rank; i++)
4134 {
4135 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4136 {
4137 gfc_error ("Shapes for operands at %L and %L are not conformable",
4138 &op1->where, &op2->where);
4139 t = false;
4140 break;
4141 }
4142 }
4143 }
4144
4145 return t;
4146 }
4147
4148 /* Convert a logical operator to the corresponding bitwise intrinsic call.
4149 For example A .AND. B becomes IAND(A, B). */
4150 static gfc_expr *
4151 logical_to_bitwise (gfc_expr *e)
4152 {
4153 gfc_expr *tmp, *op1, *op2;
4154 gfc_isym_id isym;
4155 gfc_actual_arglist *args = NULL;
4156
4157 gcc_assert (e->expr_type == EXPR_OP);
4158
4159 isym = GFC_ISYM_NONE;
4160 op1 = e->value.op.op1;
4161 op2 = e->value.op.op2;
4162
4163 switch (e->value.op.op)
4164 {
4165 case INTRINSIC_NOT:
4166 isym = GFC_ISYM_NOT;
4167 break;
4168 case INTRINSIC_AND:
4169 isym = GFC_ISYM_IAND;
4170 break;
4171 case INTRINSIC_OR:
4172 isym = GFC_ISYM_IOR;
4173 break;
4174 case INTRINSIC_NEQV:
4175 isym = GFC_ISYM_IEOR;
4176 break;
4177 case INTRINSIC_EQV:
4178 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4179 Change the old expression to NEQV, which will get replaced by IEOR,
4180 and wrap it in NOT. */
4181 tmp = gfc_copy_expr (e);
4182 tmp->value.op.op = INTRINSIC_NEQV;
4183 tmp = logical_to_bitwise (tmp);
4184 isym = GFC_ISYM_NOT;
4185 op1 = tmp;
4186 op2 = NULL;
4187 break;
4188 default:
4189 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4190 }
4191
4192 /* Inherit the original operation's operands as arguments. */
4193 args = gfc_get_actual_arglist ();
4194 args->expr = op1;
4195 if (op2)
4196 {
4197 args->next = gfc_get_actual_arglist ();
4198 args->next->expr = op2;
4199 }
4200
4201 /* Convert the expression to a function call. */
4202 e->expr_type = EXPR_FUNCTION;
4203 e->value.function.actual = args;
4204 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4205 e->value.function.name = e->value.function.isym->name;
4206 e->value.function.esym = NULL;
4207
4208 /* Make up a pre-resolved function call symtree if we need to. */
4209 if (!e->symtree || !e->symtree->n.sym)
4210 {
4211 gfc_symbol *sym;
4212 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4213 sym = e->symtree->n.sym;
4214 sym->result = sym;
4215 sym->attr.flavor = FL_PROCEDURE;
4216 sym->attr.function = 1;
4217 sym->attr.elemental = 1;
4218 sym->attr.pure = 1;
4219 sym->attr.referenced = 1;
4220 gfc_intrinsic_symbol (sym);
4221 gfc_commit_symbol (sym);
4222 }
4223
4224 args->name = e->value.function.isym->formal->name;
4225 if (e->value.function.isym->formal->next)
4226 args->next->name = e->value.function.isym->formal->next->name;
4227
4228 return e;
4229 }
4230
4231 /* Recursively append candidate UOP to CANDIDATES. Store the number of
4232 candidates in CANDIDATES_LEN. */
4233 static void
4234 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4235 char **&candidates,
4236 size_t &candidates_len)
4237 {
4238 gfc_symtree *p;
4239
4240 if (uop == NULL)
4241 return;
4242
4243 /* Not sure how to properly filter here. Use all for a start.
4244 n.uop.op is NULL for empty interface operators (is that legal?) disregard
4245 these as i suppose they don't make terribly sense. */
4246
4247 if (uop->n.uop->op != NULL)
4248 vec_push (candidates, candidates_len, uop->name);
4249
4250 p = uop->left;
4251 if (p)
4252 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4253
4254 p = uop->right;
4255 if (p)
4256 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4257 }
4258
4259 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4260
4261 static const char*
4262 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4263 {
4264 char **candidates = NULL;
4265 size_t candidates_len = 0;
4266 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4267 return gfc_closest_fuzzy_match (op, candidates);
4268 }
4269
4270
4271 /* Callback finding an impure function as an operand to an .and. or
4272 .or. expression. Remember the last function warned about to
4273 avoid double warnings when recursing. */
4274
4275 static int
4276 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4277 void *data)
4278 {
4279 gfc_expr *f = *e;
4280 const char *name;
4281 static gfc_expr *last = NULL;
4282 bool *found = (bool *) data;
4283
4284 if (f->expr_type == EXPR_FUNCTION)
4285 {
4286 *found = 1;
4287 if (f != last && !gfc_pure_function (f, &name)
4288 && !gfc_implicit_pure_function (f))
4289 {
4290 if (name)
4291 gfc_warning (OPT_Wfunction_elimination,
4292 "Impure function %qs at %L might not be evaluated",
4293 name, &f->where);
4294 else
4295 gfc_warning (OPT_Wfunction_elimination,
4296 "Impure function at %L might not be evaluated",
4297 &f->where);
4298 }
4299 last = f;
4300 }
4301
4302 return 0;
4303 }
4304
4305 /* Return true if TYPE is character based, false otherwise. */
4306
4307 static int
4308 is_character_based (bt type)
4309 {
4310 return type == BT_CHARACTER || type == BT_HOLLERITH;
4311 }
4312
4313
4314 /* If expression is a hollerith, convert it to character and issue a warning
4315 for the conversion. */
4316
4317 static void
4318 convert_hollerith_to_character (gfc_expr *e)
4319 {
4320 if (e->ts.type == BT_HOLLERITH)
4321 {
4322 gfc_typespec t;
4323 gfc_clear_ts (&t);
4324 t.type = BT_CHARACTER;
4325 t.kind = e->ts.kind;
4326 gfc_convert_type_warn (e, &t, 2, 1);
4327 }
4328 }
4329
4330 /* Convert to numeric and issue a warning for the conversion. */
4331
4332 static void
4333 convert_to_numeric (gfc_expr *a, gfc_expr *b)
4334 {
4335 gfc_typespec t;
4336 gfc_clear_ts (&t);
4337 t.type = b->ts.type;
4338 t.kind = b->ts.kind;
4339 gfc_convert_type_warn (a, &t, 2, 1);
4340 }
4341
4342 /* Resolve an operator expression node. This can involve replacing the
4343 operation with a user defined function call. CHECK_INTERFACES is a
4344 helper macro. */
4345
4346 #define CHECK_INTERFACES \
4347 { \
4348 match m = gfc_extend_expr (e); \
4349 if (m == MATCH_YES) \
4350 return true; \
4351 if (m == MATCH_ERROR) \
4352 return false; \
4353 }
4354
4355 static bool
4356 resolve_operator (gfc_expr *e)
4357 {
4358 gfc_expr *op1, *op2;
4359 /* One error uses 3 names; additional space for wording (also via gettext). */
4360 bool t = true;
4361
4362 /* Reduce stacked parentheses to single pair */
4363 while (e->expr_type == EXPR_OP
4364 && e->value.op.op == INTRINSIC_PARENTHESES
4365 && e->value.op.op1->expr_type == EXPR_OP
4366 && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4367 {
4368 gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4369 gfc_replace_expr (e, tmp);
4370 }
4371
4372 /* Resolve all subnodes-- give them types. */
4373
4374 switch (e->value.op.op)
4375 {
4376 default:
4377 if (!gfc_resolve_expr (e->value.op.op2))
4378 t = false;
4379
4380 /* Fall through. */
4381
4382 case INTRINSIC_NOT:
4383 case INTRINSIC_UPLUS:
4384 case INTRINSIC_UMINUS:
4385 case INTRINSIC_PARENTHESES:
4386 if (!gfc_resolve_expr (e->value.op.op1))
4387 return false;
4388 if (e->value.op.op1
4389 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4390 {
4391 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4392 "unary operator %qs", &e->value.op.op1->where,
4393 gfc_op2string (e->value.op.op));
4394 return false;
4395 }
4396 if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4397 && e->value.op.op == INTRINSIC_UMINUS)
4398 {
4399 gfc_error ("Negation of unsigned expression at %L not permitted ",
4400 &e->value.op.op1->where);
4401 return false;
4402 }
4403 break;
4404 }
4405
4406 /* Typecheck the new node. */
4407
4408 op1 = e->value.op.op1;
4409 op2 = e->value.op.op2;
4410 if (op1 == NULL && op2 == NULL)
4411 return false;
4412 /* Error out if op2 did not resolve. We already diagnosed op1. */
4413 if (t == false)
4414 return false;
4415
4416 /* op1 and op2 cannot both be BOZ. */
4417 if (op1 && op1->ts.type == BT_BOZ
4418 && op2 && op2->ts.type == BT_BOZ)
4419 {
4420 gfc_error ("Operands at %L and %L cannot appear as operands of "
4421 "binary operator %qs", &op1->where, &op2->where,
4422 gfc_op2string (e->value.op.op));
4423 return false;
4424 }
4425
4426 if ((op1 && op1->expr_type == EXPR_NULL)
4427 || (op2 && op2->expr_type == EXPR_NULL))
4428 {
4429 CHECK_INTERFACES
4430 gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4431 return false;
4432 }
4433
4434 switch (e->value.op.op)
4435 {
4436 case INTRINSIC_UPLUS:
4437 case INTRINSIC_UMINUS:
4438 if (op1->ts.type == BT_INTEGER
4439 || op1->ts.type == BT_REAL
4440 || op1->ts.type == BT_COMPLEX
4441 || op1->ts.type == BT_UNSIGNED)
4442 {
4443 e->ts = op1->ts;
4444 break;
4445 }
4446
4447 CHECK_INTERFACES
4448 gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4449 gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4450 return false;
4451
4452 case INTRINSIC_POWER:
4453 case INTRINSIC_PLUS:
4454 case INTRINSIC_MINUS:
4455 case INTRINSIC_TIMES:
4456 case INTRINSIC_DIVIDE:
4457
4458 /* UNSIGNED cannot appear in a mixed expression without explicit
4459 conversion. */
4460 if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4461 {
4462 CHECK_INTERFACES
4463 gfc_error ("Operands of binary numeric operator %qs at %L are "
4464 "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4465 gfc_typename (op1), gfc_typename (op2));
4466 return false;
4467 }
4468
4469 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4470 {
4471 /* Do not perform conversions if operands are not conformable as
4472 required for the binary intrinsic operators (F2018:10.1.5).
4473 Defer to a possibly overloading user-defined operator. */
4474 if (!gfc_op_rank_conformable (op1, op2))
4475 {
4476 CHECK_INTERFACES
4477 gfc_error ("Inconsistent ranks for operator at %L and %L",
4478 &op1->where, &op2->where);
4479 return false;
4480 }
4481
4482 gfc_type_convert_binary (e, 1);
4483 break;
4484 }
4485
4486 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4487 {
4488 CHECK_INTERFACES
4489 gfc_error ("Unexpected derived-type entities in binary intrinsic "
4490 "numeric operator %qs at %L",
4491 gfc_op2string (e->value.op.op), &e->where);
4492 return false;
4493 }
4494 else
4495 {
4496 CHECK_INTERFACES
4497 gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4498 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4499 gfc_typename (op2));
4500 return false;
4501 }
4502
4503 case INTRINSIC_CONCAT:
4504 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4505 && op1->ts.kind == op2->ts.kind)
4506 {
4507 e->ts.type = BT_CHARACTER;
4508 e->ts.kind = op1->ts.kind;
4509 break;
4510 }
4511
4512 CHECK_INTERFACES
4513 gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4514 &e->where, gfc_typename (op1), gfc_typename (op2));
4515 return false;
4516
4517 case INTRINSIC_AND:
4518 case INTRINSIC_OR:
4519 case INTRINSIC_EQV:
4520 case INTRINSIC_NEQV:
4521 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4522 {
4523 e->ts.type = BT_LOGICAL;
4524 e->ts.kind = gfc_kind_max (op1, op2);
4525 if (op1->ts.kind < e->ts.kind)
4526 gfc_convert_type (op1, &e->ts, 2);
4527 else if (op2->ts.kind < e->ts.kind)
4528 gfc_convert_type (op2, &e->ts, 2);
4529
4530 if (flag_frontend_optimize &&
4531 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4532 {
4533 /* Warn about short-circuiting
4534 with impure function as second operand. */
4535 bool op2_f = false;
4536 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4537 }
4538 break;
4539 }
4540
4541 /* Logical ops on integers become bitwise ops with -fdec. */
4542 else if (flag_dec
4543 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4544 {
4545 e->ts.type = BT_INTEGER;
4546 e->ts.kind = gfc_kind_max (op1, op2);
4547 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4548 gfc_convert_type (op1, &e->ts, 1);
4549 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4550 gfc_convert_type (op2, &e->ts, 1);
4551 e = logical_to_bitwise (e);
4552 goto simplify_op;
4553 }
4554
4555 CHECK_INTERFACES
4556 gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4557 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4558 gfc_typename (op2));
4559 return false;
4560
4561 case INTRINSIC_NOT:
4562 /* Logical ops on integers become bitwise ops with -fdec. */
4563 if (flag_dec && op1->ts.type == BT_INTEGER)
4564 {
4565 e->ts.type = BT_INTEGER;
4566 e->ts.kind = op1->ts.kind;
4567 e = logical_to_bitwise (e);
4568 goto simplify_op;
4569 }
4570
4571 if (op1->ts.type == BT_LOGICAL)
4572 {
4573 e->ts.type = BT_LOGICAL;
4574 e->ts.kind = op1->ts.kind;
4575 break;
4576 }
4577
4578 CHECK_INTERFACES
4579 gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4580 gfc_typename (op1));
4581 return false;
4582
4583 case INTRINSIC_GT:
4584 case INTRINSIC_GT_OS:
4585 case INTRINSIC_GE:
4586 case INTRINSIC_GE_OS:
4587 case INTRINSIC_LT:
4588 case INTRINSIC_LT_OS:
4589 case INTRINSIC_LE:
4590 case INTRINSIC_LE_OS:
4591 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4592 {
4593 CHECK_INTERFACES
4594 gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4595 return false;
4596 }
4597
4598 /* Fall through. */
4599
4600 case INTRINSIC_EQ:
4601 case INTRINSIC_EQ_OS:
4602 case INTRINSIC_NE:
4603 case INTRINSIC_NE_OS:
4604
4605 if (flag_dec
4606 && is_character_based (op1->ts.type)
4607 && is_character_based (op2->ts.type))
4608 {
4609 convert_hollerith_to_character (op1);
4610 convert_hollerith_to_character (op2);
4611 }
4612
4613 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4614 && op1->ts.kind == op2->ts.kind)
4615 {
4616 e->ts.type = BT_LOGICAL;
4617 e->ts.kind = gfc_default_logical_kind;
4618 break;
4619 }
4620
4621 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4622 if (op1->ts.type == BT_BOZ)
4623 {
4624 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4625 "as an operand of a relational operator"),
4626 &op1->where))
4627 return false;
4628
4629 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4630 return false;
4631
4632 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4633 return false;
4634 }
4635
4636 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4637 if (op2->ts.type == BT_BOZ)
4638 {
4639 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4640 " as an operand of a relational operator"),
4641 &op2->where))
4642 return false;
4643
4644 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4645 return false;
4646
4647 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4648 return false;
4649 }
4650 if (flag_dec
4651 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4652 convert_to_numeric (op1, op2);
4653
4654 if (flag_dec
4655 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4656 convert_to_numeric (op2, op1);
4657
4658 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4659 {
4660 /* Do not perform conversions if operands are not conformable as
4661 required for the binary intrinsic operators (F2018:10.1.5).
4662 Defer to a possibly overloading user-defined operator. */
4663 if (!gfc_op_rank_conformable (op1, op2))
4664 {
4665 CHECK_INTERFACES
4666 gfc_error ("Inconsistent ranks for operator at %L and %L",
4667 &op1->where, &op2->where);
4668 return false;
4669 }
4670
4671 if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4672 {
4673 CHECK_INTERFACES
4674 gfc_error ("Inconsistent types for operator at %L and %L: "
4675 "%s and %s", &op1->where, &op2->where,
4676 gfc_typename (op1), gfc_typename (op2));
4677 return false;
4678 }
4679
4680 gfc_type_convert_binary (e, 1);
4681
4682 e->ts.type = BT_LOGICAL;
4683 e->ts.kind = gfc_default_logical_kind;
4684
4685 if (warn_compare_reals)
4686 {
4687 gfc_intrinsic_op op = e->value.op.op;
4688
4689 /* Type conversion has made sure that the types of op1 and op2
4690 agree, so it is only necessary to check the first one. */
4691 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4692 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4693 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4694 {
4695 const char *msg;
4696
4697 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4698 msg = G_("Equality comparison for %s at %L");
4699 else
4700 msg = G_("Inequality comparison for %s at %L");
4701
4702 gfc_warning (OPT_Wcompare_reals, msg,
4703 gfc_typename (op1), &op1->where);
4704 }
4705 }
4706
4707 break;
4708 }
4709
4710 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4711 {
4712 CHECK_INTERFACES
4713 gfc_error ("Logicals at %L must be compared with %s instead of %s",
4714 &e->where,
4715 (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4716 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4717 }
4718 else
4719 {
4720 CHECK_INTERFACES
4721 gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4722 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4723 gfc_typename (op2));
4724 }
4725
4726 return false;
4727
4728 case INTRINSIC_USER:
4729 if (e->value.op.uop->op == NULL)
4730 {
4731 const char *name = e->value.op.uop->name;
4732 const char *guessed;
4733 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4734 CHECK_INTERFACES
4735 if (guessed)
4736 gfc_error ("Unknown operator %qs at %L; did you mean "
4737 "%qs?", name, &e->where, guessed);
4738 else
4739 gfc_error ("Unknown operator %qs at %L", name, &e->where);
4740 }
4741 else if (op2 == NULL)
4742 {
4743 CHECK_INTERFACES
4744 gfc_error ("Operand of user operator %qs at %L is %s",
4745 e->value.op.uop->name, &e->where, gfc_typename (op1));
4746 }
4747 else
4748 {
4749 e->value.op.uop->op->sym->attr.referenced = 1;
4750 CHECK_INTERFACES
4751 gfc_error ("Operands of user operator %qs at %L are %s/%s",
4752 e->value.op.uop->name, &e->where, gfc_typename (op1),
4753 gfc_typename (op2));
4754 }
4755
4756 return false;
4757
4758 case INTRINSIC_PARENTHESES:
4759 e->ts = op1->ts;
4760 if (e->ts.type == BT_CHARACTER)
4761 e->ts.u.cl = op1->ts.u.cl;
4762 break;
4763
4764 default:
4765 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4766 }
4767
4768 /* Deal with arrayness of an operand through an operator. */
4769
4770 switch (e->value.op.op)
4771 {
4772 case INTRINSIC_PLUS:
4773 case INTRINSIC_MINUS:
4774 case INTRINSIC_TIMES:
4775 case INTRINSIC_DIVIDE:
4776 case INTRINSIC_POWER:
4777 case INTRINSIC_CONCAT:
4778 case INTRINSIC_AND:
4779 case INTRINSIC_OR:
4780 case INTRINSIC_EQV:
4781 case INTRINSIC_NEQV:
4782 case INTRINSIC_EQ:
4783 case INTRINSIC_EQ_OS:
4784 case INTRINSIC_NE:
4785 case INTRINSIC_NE_OS:
4786 case INTRINSIC_GT:
4787 case INTRINSIC_GT_OS:
4788 case INTRINSIC_GE:
4789 case INTRINSIC_GE_OS:
4790 case INTRINSIC_LT:
4791 case INTRINSIC_LT_OS:
4792 case INTRINSIC_LE:
4793 case INTRINSIC_LE_OS:
4794
4795 if (op1->rank == 0 && op2->rank == 0)
4796 e->rank = 0;
4797
4798 if (op1->rank == 0 && op2->rank != 0)
4799 {
4800 e->rank = op2->rank;
4801
4802 if (e->shape == NULL)
4803 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4804 }
4805
4806 if (op1->rank != 0 && op2->rank == 0)
4807 {
4808 e->rank = op1->rank;
4809
4810 if (e->shape == NULL)
4811 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4812 }
4813
4814 if (op1->rank != 0 && op2->rank != 0)
4815 {
4816 if (op1->rank == op2->rank)
4817 {
4818 e->rank = op1->rank;
4819 if (e->shape == NULL)
4820 {
4821 t = compare_shapes (op1, op2);
4822 if (!t)
4823 e->shape = NULL;
4824 else
4825 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4826 }
4827 }
4828 else
4829 {
4830 /* Allow higher level expressions to work. */
4831 e->rank = 0;
4832
4833 /* Try user-defined operators, and otherwise throw an error. */
4834 CHECK_INTERFACES
4835 gfc_error ("Inconsistent ranks for operator at %L and %L",
4836 &op1->where, &op2->where);
4837 return false;
4838 }
4839 }
4840 break;
4841
4842 case INTRINSIC_PARENTHESES:
4843 case INTRINSIC_NOT:
4844 case INTRINSIC_UPLUS:
4845 case INTRINSIC_UMINUS:
4846 /* Simply copy arrayness attribute */
4847 e->rank = op1->rank;
4848 e->corank = op1->corank;
4849
4850 if (e->shape == NULL)
4851 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4852
4853 break;
4854
4855 default:
4856 break;
4857 }
4858
4859 simplify_op:
4860
4861 /* Attempt to simplify the expression. */
4862 if (t)
4863 {
4864 t = gfc_simplify_expr (e, 0);
4865 /* Some calls do not succeed in simplification and return false
4866 even though there is no error; e.g. variable references to
4867 PARAMETER arrays. */
4868 if (!gfc_is_constant_expr (e))
4869 t = true;
4870 }
4871 return t;
4872 }
4873
4874
4875 /************** Array resolution subroutines **************/
4876
4877 enum compare_result
4878 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4879
4880 /* Compare two integer expressions. */
4881
4882 static compare_result
4883 compare_bound (gfc_expr *a, gfc_expr *b)
4884 {
4885 int i;
4886
4887 if (a == NULL || a->expr_type != EXPR_CONSTANT
4888 || b == NULL || b->expr_type != EXPR_CONSTANT)
4889 return CMP_UNKNOWN;
4890
4891 /* If either of the types isn't INTEGER, we must have
4892 raised an error earlier. */
4893
4894 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4895 return CMP_UNKNOWN;
4896
4897 i = mpz_cmp (a->value.integer, b->value.integer);
4898
4899 if (i < 0)
4900 return CMP_LT;
4901 if (i > 0)
4902 return CMP_GT;
4903 return CMP_EQ;
4904 }
4905
4906
4907 /* Compare an integer expression with an integer. */
4908
4909 static compare_result
4910 compare_bound_int (gfc_expr *a, int b)
4911 {
4912 int i;
4913
4914 if (a == NULL
4915 || a->expr_type != EXPR_CONSTANT
4916 || a->ts.type != BT_INTEGER)
4917 return CMP_UNKNOWN;
4918
4919 i = mpz_cmp_si (a->value.integer, b);
4920
4921 if (i < 0)
4922 return CMP_LT;
4923 if (i > 0)
4924 return CMP_GT;
4925 return CMP_EQ;
4926 }
4927
4928
4929 /* Compare an integer expression with a mpz_t. */
4930
4931 static compare_result
4932 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4933 {
4934 int i;
4935
4936 if (a == NULL
4937 || a->expr_type != EXPR_CONSTANT
4938 || a->ts.type != BT_INTEGER)
4939 return CMP_UNKNOWN;
4940
4941 i = mpz_cmp (a->value.integer, b);
4942
4943 if (i < 0)
4944 return CMP_LT;
4945 if (i > 0)
4946 return CMP_GT;
4947 return CMP_EQ;
4948 }
4949
4950
4951 /* Compute the last value of a sequence given by a triplet.
4952 Return 0 if it wasn't able to compute the last value, or if the
4953 sequence if empty, and 1 otherwise. */
4954
4955 static int
4956 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4957 gfc_expr *stride, mpz_t last)
4958 {
4959 mpz_t rem;
4960
4961 if (start == NULL || start->expr_type != EXPR_CONSTANT
4962 || end == NULL || end->expr_type != EXPR_CONSTANT
4963 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4964 return 0;
4965
4966 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4967 || (stride != NULL && stride->ts.type != BT_INTEGER))
4968 return 0;
4969
4970 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4971 {
4972 if (compare_bound (start, end) == CMP_GT)
4973 return 0;
4974 mpz_set (last, end->value.integer);
4975 return 1;
4976 }
4977
4978 if (compare_bound_int (stride, 0) == CMP_GT)
4979 {
4980 /* Stride is positive */
4981 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4982 return 0;
4983 }
4984 else
4985 {
4986 /* Stride is negative */
4987 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4988 return 0;
4989 }
4990
4991 mpz_init (rem);
4992 mpz_sub (rem, end->value.integer, start->value.integer);
4993 mpz_tdiv_r (rem, rem, stride->value.integer);
4994 mpz_sub (last, end->value.integer, rem);
4995 mpz_clear (rem);
4996
4997 return 1;
4998 }
4999
5000
5001 /* Compare a single dimension of an array reference to the array
5002 specification. */
5003
5004 static bool
5005 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5006 {
5007 mpz_t last_value;
5008
5009 if (ar->dimen_type[i] == DIMEN_STAR)
5010 {
5011 gcc_assert (ar->stride[i] == NULL);
5012 /* This implies [*] as [*:] and [*:3] are not possible. */
5013 if (ar->start[i] == NULL)
5014 {
5015 gcc_assert (ar->end[i] == NULL);
5016 return true;
5017 }
5018 }
5019
5020 /* Given start, end and stride values, calculate the minimum and
5021 maximum referenced indexes. */
5022
5023 switch (ar->dimen_type[i])
5024 {
5025 case DIMEN_VECTOR:
5026 case DIMEN_THIS_IMAGE:
5027 break;
5028
5029 case DIMEN_STAR:
5030 case DIMEN_ELEMENT:
5031 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5032 {
5033 if (i < as->rank)
5034 gfc_warning (0, "Array reference at %L is out of bounds "
5035 "(%ld < %ld) in dimension %d", &ar->c_where[i],
5036 mpz_get_si (ar->start[i]->value.integer),
5037 mpz_get_si (as->lower[i]->value.integer), i+1);
5038 else
5039 gfc_warning (0, "Array reference at %L is out of bounds "
5040 "(%ld < %ld) in codimension %d", &ar->c_where[i],
5041 mpz_get_si (ar->start[i]->value.integer),
5042 mpz_get_si (as->lower[i]->value.integer),
5043 i + 1 - as->rank);
5044 return true;
5045 }
5046 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5047 {
5048 if (i < as->rank)
5049 gfc_warning (0, "Array reference at %L is out of bounds "
5050 "(%ld > %ld) in dimension %d", &ar->c_where[i],
5051 mpz_get_si (ar->start[i]->value.integer),
5052 mpz_get_si (as->upper[i]->value.integer), i+1);
5053 else
5054 gfc_warning (0, "Array reference at %L is out of bounds "
5055 "(%ld > %ld) in codimension %d", &ar->c_where[i],
5056 mpz_get_si (ar->start[i]->value.integer),
5057 mpz_get_si (as->upper[i]->value.integer),
5058 i + 1 - as->rank);
5059 return true;
5060 }
5061
5062 break;
5063
5064 case DIMEN_RANGE:
5065 {
5066 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5067 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5068
5069 compare_result comp_start_end = compare_bound (AR_START, AR_END);
5070 compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5071
5072 /* Check for zero stride, which is not allowed. */
5073 if (comp_stride_zero == CMP_EQ)
5074 {
5075 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5076 return false;
5077 }
5078
5079 /* if start == end || (stride > 0 && start < end)
5080 || (stride < 0 && start > end),
5081 then the array section contains at least one element. In this
5082 case, there is an out-of-bounds access if
5083 (start < lower || start > upper). */
5084 if (comp_start_end == CMP_EQ
5085 || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5086 && comp_start_end == CMP_LT)
5087 || (comp_stride_zero == CMP_LT
5088 && comp_start_end == CMP_GT))
5089 {
5090 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5091 {
5092 gfc_warning (0, "Lower array reference at %L is out of bounds "
5093 "(%ld < %ld) in dimension %d", &ar->c_where[i],
5094 mpz_get_si (AR_START->value.integer),
5095 mpz_get_si (as->lower[i]->value.integer), i+1);
5096 return true;
5097 }
5098 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5099 {
5100 gfc_warning (0, "Lower array reference at %L is out of bounds "
5101 "(%ld > %ld) in dimension %d", &ar->c_where[i],
5102 mpz_get_si (AR_START->value.integer),
5103 mpz_get_si (as->upper[i]->value.integer), i+1);
5104 return true;
5105 }
5106 }
5107
5108 /* If we can compute the highest index of the array section,
5109 then it also has to be between lower and upper. */
5110 mpz_init (last_value);
5111 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5112 last_value))
5113 {
5114 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5115 {
5116 gfc_warning (0, "Upper array reference at %L is out of bounds "
5117 "(%ld < %ld) in dimension %d", &ar->c_where[i],
5118 mpz_get_si (last_value),
5119 mpz_get_si (as->lower[i]->value.integer), i+1);
5120 mpz_clear (last_value);
5121 return true;
5122 }
5123 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5124 {
5125 gfc_warning (0, "Upper array reference at %L is out of bounds "
5126 "(%ld > %ld) in dimension %d", &ar->c_where[i],
5127 mpz_get_si (last_value),
5128 mpz_get_si (as->upper[i]->value.integer), i+1);
5129 mpz_clear (last_value);
5130 return true;
5131 }
5132 }
5133 mpz_clear (last_value);
5134
5135 #undef AR_START
5136 #undef AR_END
5137 }
5138 break;
5139
5140 default:
5141 gfc_internal_error ("check_dimension(): Bad array reference");
5142 }
5143
5144 return true;
5145 }
5146
5147
5148 /* Compare an array reference with an array specification. */
5149
5150 static bool
5151 compare_spec_to_ref (gfc_array_ref *ar)
5152 {
5153 gfc_array_spec *as;
5154 int i;
5155
5156 as = ar->as;
5157 i = as->rank - 1;
5158 /* TODO: Full array sections are only allowed as actual parameters. */
5159 if (as->type == AS_ASSUMED_SIZE
5160 && (/*ar->type == AR_FULL
5161 ||*/ (ar->type == AR_SECTION
5162 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5163 {
5164 gfc_error ("Rightmost upper bound of assumed size array section "
5165 "not specified at %L", &ar->where);
5166 return false;
5167 }
5168
5169 if (ar->type == AR_FULL)
5170 return true;
5171
5172 if (as->rank != ar->dimen)
5173 {
5174 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5175 &ar->where, ar->dimen, as->rank);
5176 return false;
5177 }
5178
5179 /* ar->codimen == 0 is a local array. */
5180 if (as->corank != ar->codimen && ar->codimen != 0)
5181 {
5182 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5183 &ar->where, ar->codimen, as->corank);
5184 return false;
5185 }
5186
5187 for (i = 0; i < as->rank; i++)
5188 if (!check_dimension (i, ar, as))
5189 return false;
5190
5191 /* Local access has no coarray spec. */
5192 if (ar->codimen != 0)
5193 for (i = as->rank; i < as->rank + as->corank; i++)
5194 {
5195 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5196 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5197 {
5198 gfc_error ("Coindex of codimension %d must be a scalar at %L",
5199 i + 1 - as->rank, &ar->where);
5200 return false;
5201 }
5202 if (!check_dimension (i, ar, as))
5203 return false;
5204 }
5205
5206 return true;
5207 }
5208
5209
5210 /* Resolve one part of an array index. */
5211
5212 static bool
5213 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5214 int force_index_integer_kind)
5215 {
5216 gfc_typespec ts;
5217
5218 if (index == NULL)
5219 return true;
5220
5221 if (!gfc_resolve_expr (index))
5222 return false;
5223
5224 if (check_scalar && index->rank != 0)
5225 {
5226 gfc_error ("Array index at %L must be scalar", &index->where);
5227 return false;
5228 }
5229
5230 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5231 {
5232 gfc_error ("Array index at %L must be of INTEGER type, found %s",
5233 &index->where, gfc_basic_typename (index->ts.type));
5234 return false;
5235 }
5236
5237 if (index->ts.type == BT_REAL)
5238 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5239 &index->where))
5240 return false;
5241
5242 if ((index->ts.kind != gfc_index_integer_kind
5243 && force_index_integer_kind)
5244 || (index->ts.type != BT_INTEGER
5245 && index->ts.type != BT_UNKNOWN))
5246 {
5247 gfc_clear_ts (&ts);
5248 ts.type = BT_INTEGER;
5249 ts.kind = gfc_index_integer_kind;
5250
5251 gfc_convert_type_warn (index, &ts, 2, 0);
5252 }
5253
5254 return true;
5255 }
5256
5257 /* Resolve one part of an array index. */
5258
5259 bool
5260 gfc_resolve_index (gfc_expr *index, int check_scalar)
5261 {
5262 return gfc_resolve_index_1 (index, check_scalar, 1);
5263 }
5264
5265 /* Resolve a dim argument to an intrinsic function. */
5266
5267 bool
5268 gfc_resolve_dim_arg (gfc_expr *dim)
5269 {
5270 if (dim == NULL)
5271 return true;
5272
5273 if (!gfc_resolve_expr (dim))
5274 return false;
5275
5276 if (dim->rank != 0)
5277 {
5278 gfc_error ("Argument dim at %L must be scalar", &dim->where);
5279 return false;
5280
5281 }
5282
5283 if (dim->ts.type != BT_INTEGER)
5284 {
5285 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5286 return false;
5287 }
5288
5289 if (dim->ts.kind != gfc_index_integer_kind)
5290 {
5291 gfc_typespec ts;
5292
5293 gfc_clear_ts (&ts);
5294 ts.type = BT_INTEGER;
5295 ts.kind = gfc_index_integer_kind;
5296
5297 gfc_convert_type_warn (dim, &ts, 2, 0);
5298 }
5299
5300 return true;
5301 }
5302
5303 /* Given an expression that contains array references, update those array
5304 references to point to the right array specifications. While this is
5305 filled in during matching, this information is difficult to save and load
5306 in a module, so we take care of it here.
5307
5308 The idea here is that the original array reference comes from the
5309 base symbol. We traverse the list of reference structures, setting
5310 the stored reference to references. Component references can
5311 provide an additional array specification. */
5312 static void
5313 resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5314
5315 static bool
5316 find_array_spec (gfc_expr *e)
5317 {
5318 gfc_array_spec *as;
5319 gfc_component *c;
5320 gfc_ref *ref;
5321 bool class_as = false;
5322
5323 if (e->symtree->n.sym->assoc)
5324 {
5325 if (e->symtree->n.sym->assoc->target)
5326 gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5327 resolve_assoc_var (e->symtree->n.sym, false);
5328 }
5329
5330 if (e->symtree->n.sym->ts.type == BT_CLASS)
5331 {
5332 as = CLASS_DATA (e->symtree->n.sym)->as;
5333 class_as = true;
5334 }
5335 else
5336 as = e->symtree->n.sym->as;
5337
5338 for (ref = e->ref; ref; ref = ref->next)
5339 switch (ref->type)
5340 {
5341 case REF_ARRAY:
5342 if (as == NULL)
5343 {
5344 locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5345 ? ref->u.ar.where : e->where);
5346 gfc_error ("Invalid array reference of a non-array entity at %L",
5347 &loc);
5348 return false;
5349 }
5350
5351 ref->u.ar.as = as;
5352 if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5353 as = NULL;
5354 break;
5355
5356 case REF_COMPONENT:
5357 c = ref->u.c.component;
5358 if (c->attr.dimension)
5359 {
5360 if (as != NULL && !(class_as && as == c->as))
5361 gfc_internal_error ("find_array_spec(): unused as(1)");
5362 as = c->as;
5363 }
5364
5365 break;
5366
5367 case REF_SUBSTRING:
5368 case REF_INQUIRY:
5369 break;
5370 }
5371
5372 if (as != NULL)
5373 gfc_internal_error ("find_array_spec(): unused as(2)");
5374
5375 return true;
5376 }
5377
5378
5379 /* Resolve an array reference. */
5380
5381 static bool
5382 resolve_array_ref (gfc_array_ref *ar)
5383 {
5384 int i, check_scalar;
5385 gfc_expr *e;
5386
5387 for (i = 0; i < ar->dimen + ar->codimen; i++)
5388 {
5389 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5390
5391 /* Do not force gfc_index_integer_kind for the start. We can
5392 do fine with any integer kind. This avoids temporary arrays
5393 created for indexing with a vector. */
5394 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5395 return false;
5396 if (!gfc_resolve_index (ar->end[i], check_scalar))
5397 return false;
5398 if (!gfc_resolve_index (ar->stride[i], check_scalar))
5399 return false;
5400
5401 e = ar->start[i];
5402
5403 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5404 switch (e->rank)
5405 {
5406 case 0:
5407 ar->dimen_type[i] = DIMEN_ELEMENT;
5408 break;
5409
5410 case 1:
5411 ar->dimen_type[i] = DIMEN_VECTOR;
5412 if (e->expr_type == EXPR_VARIABLE
5413 && e->symtree->n.sym->ts.type == BT_DERIVED)
5414 ar->start[i] = gfc_get_parentheses (e);
5415 break;
5416
5417 default:
5418 gfc_error ("Array index at %L is an array of rank %d",
5419 &ar->c_where[i], e->rank);
5420 return false;
5421 }
5422
5423 /* Fill in the upper bound, which may be lower than the
5424 specified one for something like a(2:10:5), which is
5425 identical to a(2:7:5). Only relevant for strides not equal
5426 to one. Don't try a division by zero. */
5427 if (ar->dimen_type[i] == DIMEN_RANGE
5428 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5429 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5430 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5431 {
5432 mpz_t size, end;
5433
5434 if (gfc_ref_dimen_size (ar, i, &size, &end))
5435 {
5436 if (ar->end[i] == NULL)
5437 {
5438 ar->end[i] =
5439 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5440 &ar->where);
5441 mpz_set (ar->end[i]->value.integer, end);
5442 }
5443 else if (ar->end[i]->ts.type == BT_INTEGER
5444 && ar->end[i]->expr_type == EXPR_CONSTANT)
5445 {
5446 mpz_set (ar->end[i]->value.integer, end);
5447 }
5448 else
5449 gcc_unreachable ();
5450
5451 mpz_clear (size);
5452 mpz_clear (end);
5453 }
5454 }
5455 }
5456
5457 if (ar->type == AR_FULL)
5458 {
5459 if (ar->as->rank == 0)
5460 ar->type = AR_ELEMENT;
5461
5462 /* Make sure array is the same as array(:,:), this way
5463 we don't need to special case all the time. */
5464 ar->dimen = ar->as->rank;
5465 for (i = 0; i < ar->dimen; i++)
5466 {
5467 ar->dimen_type[i] = DIMEN_RANGE;
5468
5469 gcc_assert (ar->start[i] == NULL);
5470 gcc_assert (ar->end[i] == NULL);
5471 gcc_assert (ar->stride[i] == NULL);
5472 }
5473 }
5474
5475 /* If the reference type is unknown, figure out what kind it is. */
5476
5477 if (ar->type == AR_UNKNOWN)
5478 {
5479 ar->type = AR_ELEMENT;
5480 for (i = 0; i < ar->dimen; i++)
5481 if (ar->dimen_type[i] == DIMEN_RANGE
5482 || ar->dimen_type[i] == DIMEN_VECTOR)
5483 {
5484 ar->type = AR_SECTION;
5485 break;
5486 }
5487 }
5488
5489 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5490 return false;
5491
5492 if (ar->as->corank && ar->codimen == 0)
5493 {
5494 int n;
5495 ar->codimen = ar->as->corank;
5496 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5497 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5498 }
5499
5500 if (ar->codimen)
5501 {
5502 if (ar->team_type == TEAM_NUMBER)
5503 {
5504 if (!gfc_resolve_expr (ar->team))
5505 return false;
5506
5507 if (ar->team->rank != 0)
5508 {
5509 gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5510 &ar->team->where);
5511 return false;
5512 }
5513
5514 if (ar->team->ts.type != BT_INTEGER)
5515 {
5516 gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5517 "type, found %s",
5518 &ar->team->where,
5519 gfc_basic_typename (ar->team->ts.type));
5520 return false;
5521 }
5522 }
5523 else if (ar->team_type == TEAM_TEAM)
5524 {
5525 if (!gfc_resolve_expr (ar->team))
5526 return false;
5527
5528 if (ar->team->rank != 0)
5529 {
5530 gfc_error ("TEAM argument at %L must be scalar",
5531 &ar->team->where);
5532 return false;
5533 }
5534
5535 if (ar->team->ts.type != BT_DERIVED
5536 || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5537 || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5538 {
5539 gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5540 "the intrinsic module ISO_FORTRAN_ENV, found %s",
5541 &ar->team->where,
5542 gfc_basic_typename (ar->team->ts.type));
5543 return false;
5544 }
5545 }
5546 if (ar->stat)
5547 {
5548 if (!gfc_resolve_expr (ar->stat))
5549 return false;
5550
5551 if (ar->stat->rank != 0)
5552 {
5553 gfc_error ("STAT argument at %L must be scalar",
5554 &ar->stat->where);
5555 return false;
5556 }
5557
5558 if (ar->stat->ts.type != BT_INTEGER)
5559 {
5560 gfc_error ("STAT argument at %L must be of INTEGER "
5561 "type, found %s",
5562 &ar->stat->where,
5563 gfc_basic_typename (ar->stat->ts.type));
5564 return false;
5565 }
5566
5567 if (ar->stat->expr_type != EXPR_VARIABLE)
5568 {
5569 gfc_error ("STAT's expression at %L must be a variable",
5570 &ar->stat->where);
5571 return false;
5572 }
5573 }
5574 }
5575 return true;
5576 }
5577
5578
5579 bool
5580 gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5581 {
5582 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5583
5584 if (ref->u.ss.start != NULL)
5585 {
5586 if (!gfc_resolve_expr (ref->u.ss.start))
5587 return false;
5588
5589 if (ref->u.ss.start->ts.type != BT_INTEGER)
5590 {
5591 gfc_error ("Substring start index at %L must be of type INTEGER",
5592 &ref->u.ss.start->where);
5593 return false;
5594 }
5595
5596 if (ref->u.ss.start->rank != 0)
5597 {
5598 gfc_error ("Substring start index at %L must be scalar",
5599 &ref->u.ss.start->where);
5600 return false;
5601 }
5602
5603 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5604 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5605 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5606 {
5607 gfc_error ("Substring start index at %L is less than one",
5608 &ref->u.ss.start->where);
5609 return false;
5610 }
5611 }
5612
5613 if (ref->u.ss.end != NULL)
5614 {
5615 if (!gfc_resolve_expr (ref->u.ss.end))
5616 return false;
5617
5618 if (ref->u.ss.end->ts.type != BT_INTEGER)
5619 {
5620 gfc_error ("Substring end index at %L must be of type INTEGER",
5621 &ref->u.ss.end->where);
5622 return false;
5623 }
5624
5625 if (ref->u.ss.end->rank != 0)
5626 {
5627 gfc_error ("Substring end index at %L must be scalar",
5628 &ref->u.ss.end->where);
5629 return false;
5630 }
5631
5632 if (ref->u.ss.length != NULL
5633 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5634 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5635 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5636 {
5637 gfc_error ("Substring end index at %L exceeds the string length",
5638 &ref->u.ss.start->where);
5639 return false;
5640 }
5641
5642 if (compare_bound_mpz_t (ref->u.ss.end,
5643 gfc_integer_kinds[k].huge) == CMP_GT
5644 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5645 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5646 {
5647 gfc_error ("Substring end index at %L is too large",
5648 &ref->u.ss.end->where);
5649 return false;
5650 }
5651 /* If the substring has the same length as the original
5652 variable, the reference itself can be deleted. */
5653
5654 if (ref->u.ss.length != NULL
5655 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5656 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5657 *equal_length = true;
5658 }
5659
5660 return true;
5661 }
5662
5663
5664 /* This function supplies missing substring charlens. */
5665
5666 void
5667 gfc_resolve_substring_charlen (gfc_expr *e)
5668 {
5669 gfc_ref *char_ref;
5670 gfc_expr *start, *end;
5671 gfc_typespec *ts = NULL;
5672 mpz_t diff;
5673
5674 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5675 {
5676 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5677 break;
5678 if (char_ref->type == REF_COMPONENT)
5679 ts = &char_ref->u.c.component->ts;
5680 }
5681
5682 if (!char_ref || char_ref->type == REF_INQUIRY)
5683 return;
5684
5685 gcc_assert (char_ref->next == NULL);
5686
5687 if (e->ts.u.cl)
5688 {
5689 if (e->ts.u.cl->length)
5690 gfc_free_expr (e->ts.u.cl->length);
5691 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5692 return;
5693 }
5694
5695 if (!e->ts.u.cl)
5696 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5697
5698 if (char_ref->u.ss.start)
5699 start = gfc_copy_expr (char_ref->u.ss.start);
5700 else
5701 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5702
5703 if (char_ref->u.ss.end)
5704 end = gfc_copy_expr (char_ref->u.ss.end);
5705 else if (e->expr_type == EXPR_VARIABLE)
5706 {
5707 if (!ts)
5708 ts = &e->symtree->n.sym->ts;
5709 end = gfc_copy_expr (ts->u.cl->length);
5710 }
5711 else
5712 end = NULL;
5713
5714 if (!start || !end)
5715 {
5716 gfc_free_expr (start);
5717 gfc_free_expr (end);
5718 return;
5719 }
5720
5721 /* Length = (end - start + 1).
5722 Check first whether it has a constant length. */
5723 if (gfc_dep_difference (end, start, &diff))
5724 {
5725 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5726 &e->where);
5727
5728 mpz_add_ui (len->value.integer, diff, 1);
5729 mpz_clear (diff);
5730 e->ts.u.cl->length = len;
5731 /* The check for length < 0 is handled below */
5732 }
5733 else
5734 {
5735 e->ts.u.cl->length = gfc_subtract (end, start);
5736 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5737 gfc_get_int_expr (gfc_charlen_int_kind,
5738 NULL, 1));
5739 }
5740
5741 /* F2008, 6.4.1: Both the starting point and the ending point shall
5742 be within the range 1, 2, ..., n unless the starting point exceeds
5743 the ending point, in which case the substring has length zero. */
5744
5745 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5746 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5747
5748 e->ts.u.cl->length->ts.type = BT_INTEGER;
5749 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5750
5751 /* Make sure that the length is simplified. */
5752 gfc_simplify_expr (e->ts.u.cl->length, 1);
5753 gfc_resolve_expr (e->ts.u.cl->length);
5754 }
5755
5756
5757 /* Resolve subtype references. */
5758
5759 bool
5760 gfc_resolve_ref (gfc_expr *expr)
5761 {
5762 int current_part_dimension, n_components, seen_part_dimension, dim;
5763 gfc_ref *ref, **prev, *array_ref;
5764 bool equal_length;
5765
5766 for (ref = expr->ref; ref; ref = ref->next)
5767 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5768 {
5769 if (!find_array_spec (expr))
5770 return false;
5771 break;
5772 }
5773
5774 for (prev = &expr->ref; *prev != NULL;
5775 prev = *prev == NULL ? prev : &(*prev)->next)
5776 switch ((*prev)->type)
5777 {
5778 case REF_ARRAY:
5779 if (!resolve_array_ref (&(*prev)->u.ar))
5780 return false;
5781 break;
5782
5783 case REF_COMPONENT:
5784 case REF_INQUIRY:
5785 break;
5786
5787 case REF_SUBSTRING:
5788 equal_length = false;
5789 if (!gfc_resolve_substring (*prev, &equal_length))
5790 return false;
5791
5792 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5793 {
5794 /* Remove the reference and move the charlen, if any. */
5795 ref = *prev;
5796 *prev = ref->next;
5797 ref->next = NULL;
5798 expr->ts.u.cl = ref->u.ss.length;
5799 ref->u.ss.length = NULL;
5800 gfc_free_ref_list (ref);
5801 }
5802 break;
5803 }
5804
5805 /* Check constraints on part references. */
5806
5807 current_part_dimension = 0;
5808 seen_part_dimension = 0;
5809 n_components = 0;
5810 array_ref = NULL;
5811
5812 for (ref = expr->ref; ref; ref = ref->next)
5813 {
5814 switch (ref->type)
5815 {
5816 case REF_ARRAY:
5817 array_ref = ref;
5818 switch (ref->u.ar.type)
5819 {
5820 case AR_FULL:
5821 /* Coarray scalar. */
5822 if (ref->u.ar.as->rank == 0)
5823 {
5824 current_part_dimension = 0;
5825 break;
5826 }
5827 /* Fall through. */
5828 case AR_SECTION:
5829 current_part_dimension = 1;
5830 break;
5831
5832 case AR_ELEMENT:
5833 array_ref = NULL;
5834 current_part_dimension = 0;
5835 break;
5836
5837 case AR_UNKNOWN:
5838 gfc_internal_error ("resolve_ref(): Bad array reference");
5839 }
5840
5841 break;
5842
5843 case REF_COMPONENT:
5844 if (current_part_dimension || seen_part_dimension)
5845 {
5846 /* F03:C614. */
5847 if (ref->u.c.component->attr.pointer
5848 || ref->u.c.component->attr.proc_pointer
5849 || (ref->u.c.component->ts.type == BT_CLASS
5850 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5851 {
5852 gfc_error ("Component to the right of a part reference "
5853 "with nonzero rank must not have the POINTER "
5854 "attribute at %L", &expr->where);
5855 return false;
5856 }
5857 else if (ref->u.c.component->attr.allocatable
5858 || (ref->u.c.component->ts.type == BT_CLASS
5859 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5860
5861 {
5862 gfc_error ("Component to the right of a part reference "
5863 "with nonzero rank must not have the ALLOCATABLE "
5864 "attribute at %L", &expr->where);
5865 return false;
5866 }
5867 }
5868
5869 n_components++;
5870 break;
5871
5872 case REF_SUBSTRING:
5873 break;
5874
5875 case REF_INQUIRY:
5876 /* Implement requirement in note 9.7 of F2018 that the result of the
5877 LEN inquiry be a scalar. */
5878 if (ref->u.i == INQUIRY_LEN && array_ref
5879 && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
5880 || expr->ts.type == BT_INTEGER))
5881 {
5882 array_ref->u.ar.type = AR_ELEMENT;
5883 expr->rank = 0;
5884 /* INQUIRY_LEN is not evaluated from the rest of the expr
5885 but directly from the string length. This means that setting
5886 the array indices to one does not matter but might trigger
5887 a runtime bounds error. Suppress the check. */
5888 expr->no_bounds_check = 1;
5889 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5890 {
5891 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5892 if (array_ref->u.ar.start[dim])
5893 gfc_free_expr (array_ref->u.ar.start[dim]);
5894 array_ref->u.ar.start[dim]
5895 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5896 if (array_ref->u.ar.end[dim])
5897 gfc_free_expr (array_ref->u.ar.end[dim]);
5898 if (array_ref->u.ar.stride[dim])
5899 gfc_free_expr (array_ref->u.ar.stride[dim]);
5900 }
5901 }
5902 break;
5903 }
5904
5905 if (((ref->type == REF_COMPONENT && n_components > 1)
5906 || ref->next == NULL)
5907 && current_part_dimension
5908 && seen_part_dimension)
5909 {
5910 gfc_error ("Two or more part references with nonzero rank must "
5911 "not be specified at %L", &expr->where);
5912 return false;
5913 }
5914
5915 if (ref->type == REF_COMPONENT)
5916 {
5917 if (current_part_dimension)
5918 seen_part_dimension = 1;
5919
5920 /* reset to make sure */
5921 current_part_dimension = 0;
5922 }
5923 }
5924
5925 return true;
5926 }
5927
5928
5929 /* Given an expression, determine its shape. This is easier than it sounds.
5930 Leaves the shape array NULL if it is not possible to determine the shape. */
5931
5932 static void
5933 expression_shape (gfc_expr *e)
5934 {
5935 mpz_t array[GFC_MAX_DIMENSIONS];
5936 int i;
5937
5938 if (e->rank <= 0 || e->shape != NULL)
5939 return;
5940
5941 for (i = 0; i < e->rank; i++)
5942 if (!gfc_array_dimen_size (e, i, &array[i]))
5943 goto fail;
5944
5945 e->shape = gfc_get_shape (e->rank);
5946
5947 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5948
5949 return;
5950
5951 fail:
5952 for (i--; i >= 0; i--)
5953 mpz_clear (array[i]);
5954 }
5955
5956
5957 /* Given a variable expression node, compute the rank of the expression by
5958 examining the base symbol and any reference structures it may have. */
5959
5960 void
5961 gfc_expression_rank (gfc_expr *e)
5962 {
5963 gfc_ref *ref, *last_arr_ref = nullptr;
5964 int i, rank, corank;
5965
5966 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5967 could lead to serious confusion... */
5968 gcc_assert (e->expr_type != EXPR_COMPCALL);
5969
5970 if (e->ref == NULL)
5971 {
5972 if (e->expr_type == EXPR_ARRAY)
5973 goto done;
5974 /* Constructors can have a rank different from one via RESHAPE(). */
5975
5976 if (e->symtree != NULL)
5977 {
5978 /* After errors the ts.u.derived of a CLASS might not be set. */
5979 gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
5980 && e->symtree->n.sym->ts.u.derived
5981 && CLASS_DATA (e->symtree->n.sym))
5982 ? CLASS_DATA (e->symtree->n.sym)->as
5983 : e->symtree->n.sym->as;
5984 if (as)
5985 {
5986 e->rank = as->rank;
5987 e->corank = as->corank;
5988 goto done;
5989 }
5990 }
5991 e->rank = 0;
5992 e->corank = 0;
5993 goto done;
5994 }
5995
5996 rank = 0;
5997 corank = 0;
5998
5999 for (ref = e->ref; ref; ref = ref->next)
6000 {
6001 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6002 && ref->u.c.component->attr.function && !ref->next)
6003 {
6004 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6005 corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6006 }
6007
6008 if (ref->type != REF_ARRAY)
6009 continue;
6010
6011 last_arr_ref = ref;
6012 if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6013 {
6014 rank = ref->u.ar.as->rank;
6015 break;
6016 }
6017
6018 if (ref->u.ar.type == AR_SECTION)
6019 {
6020 /* Figure out the rank of the section. */
6021 if (rank != 0)
6022 gfc_internal_error ("gfc_expression_rank(): Two array specs");
6023
6024 for (i = 0; i < ref->u.ar.dimen; i++)
6025 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6026 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6027 rank++;
6028
6029 break;
6030 }
6031 }
6032 if (last_arr_ref && last_arr_ref->u.ar.as
6033 && last_arr_ref->u.ar.as->rank != -1)
6034 {
6035 for (i = last_arr_ref->u.ar.as->rank;
6036 i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6037 {
6038 /* For unknown dimen in non-resolved as assume full corank. */
6039 if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6040 || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6041 && !last_arr_ref->u.ar.as->resolved))
6042 {
6043 corank = last_arr_ref->u.ar.as->corank;
6044 break;
6045 }
6046 else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6047 || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6048 || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6049 corank++;
6050 else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6051 gfc_internal_error ("Illegal coarray index");
6052 }
6053 }
6054
6055 e->rank = rank;
6056 e->corank = corank;
6057
6058 done:
6059 expression_shape (e);
6060 }
6061
6062
6063 /* Given two expressions, check that their rank is conformable, i.e. either
6064 both have the same rank or at least one is a scalar. */
6065
6066 bool
6067 gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6068 {
6069 if (op1->expr_type == EXPR_VARIABLE)
6070 gfc_expression_rank (op1);
6071 if (op2->expr_type == EXPR_VARIABLE)
6072 gfc_expression_rank (op2);
6073
6074 return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6075 && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
6076 || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
6077 }
6078
6079 /* Resolve a variable expression. */
6080
6081 static bool
6082 resolve_variable (gfc_expr *e)
6083 {
6084 gfc_symbol *sym;
6085 bool t;
6086
6087 t = true;
6088
6089 if (e->symtree == NULL)
6090 return false;
6091 sym = e->symtree->n.sym;
6092
6093 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6094 as ts.type is set to BT_ASSUMED in resolve_symbol. */
6095 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6096 {
6097 if (!actual_arg || inquiry_argument)
6098 {
6099 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6100 "be used as actual argument", sym->name, &e->where);
6101 return false;
6102 }
6103 }
6104 /* TS 29113, 407b. */
6105 else if (e->ts.type == BT_ASSUMED)
6106 {
6107 if (!actual_arg)
6108 {
6109 gfc_error ("Assumed-type variable %s at %L may only be used "
6110 "as actual argument", sym->name, &e->where);
6111 return false;
6112 }
6113 else if (inquiry_argument && !first_actual_arg)
6114 {
6115 /* FIXME: It doesn't work reliably as inquiry_argument is not set
6116 for all inquiry functions in resolve_function; the reason is
6117 that the function-name resolution happens too late in that
6118 function. */
6119 gfc_error ("Assumed-type variable %s at %L as actual argument to "
6120 "an inquiry function shall be the first argument",
6121 sym->name, &e->where);
6122 return false;
6123 }
6124 }
6125 /* TS 29113, C535b. */
6126 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6127 && sym->ts.u.derived && CLASS_DATA (sym)
6128 && CLASS_DATA (sym)->as
6129 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6130 || (sym->ts.type != BT_CLASS && sym->as
6131 && sym->as->type == AS_ASSUMED_RANK))
6132 && !sym->attr.select_rank_temporary
6133 && !(sym->assoc && sym->assoc->ar))
6134 {
6135 if (!actual_arg
6136 && !(cs_base && cs_base->current
6137 && (cs_base->current->op == EXEC_SELECT_RANK
6138 || sym->attr.target)))
6139 {
6140 gfc_error ("Assumed-rank variable %s at %L may only be used as "
6141 "actual argument", sym->name, &e->where);
6142 return false;
6143 }
6144 else if (inquiry_argument && !first_actual_arg)
6145 {
6146 /* FIXME: It doesn't work reliably as inquiry_argument is not set
6147 for all inquiry functions in resolve_function; the reason is
6148 that the function-name resolution happens too late in that
6149 function. */
6150 gfc_error ("Assumed-rank variable %s at %L as actual argument "
6151 "to an inquiry function shall be the first argument",
6152 sym->name, &e->where);
6153 return false;
6154 }
6155 }
6156
6157 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6158 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6159 && e->ref->next == NULL))
6160 {
6161 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6162 "a subobject reference", sym->name, &e->ref->u.ar.where);
6163 return false;
6164 }
6165 /* TS 29113, 407b. */
6166 else if (e->ts.type == BT_ASSUMED && e->ref
6167 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6168 && e->ref->next == NULL))
6169 {
6170 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6171 "reference", sym->name, &e->ref->u.ar.where);
6172 return false;
6173 }
6174
6175 /* TS 29113, C535b. */
6176 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6177 && sym->ts.u.derived && CLASS_DATA (sym)
6178 && CLASS_DATA (sym)->as
6179 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6180 || (sym->ts.type != BT_CLASS && sym->as
6181 && sym->as->type == AS_ASSUMED_RANK))
6182 && !(sym->assoc && sym->assoc->ar)
6183 && e->ref
6184 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6185 && e->ref->next == NULL))
6186 {
6187 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6188 "reference", sym->name, &e->ref->u.ar.where);
6189 return false;
6190 }
6191
6192 /* Guessed type variables are associate_names whose selector had not been
6193 parsed at the time that the construct was parsed. Now the namespace is
6194 being resolved, the TKR of the selector will be available for fixup of
6195 the associate_name. */
6196 if (IS_INFERRED_TYPE (e) && e->ref)
6197 {
6198 gfc_fixup_inferred_type_refs (e);
6199 /* KIND inquiry ref returns the kind of the target. */
6200 if (e->expr_type == EXPR_CONSTANT)
6201 return true;
6202 }
6203 else if (sym->attr.select_type_temporary
6204 && sym->ns->assoc_name_inferred)
6205 gfc_fixup_inferred_type_refs (e);
6206
6207 /* For variables that are used in an associate (target => object) where
6208 the object's basetype is array valued while the target is scalar,
6209 the ts' type of the component refs is still array valued, which
6210 can't be translated that way. */
6211 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6212 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6213 && sym->assoc->target->ts.u.derived
6214 && CLASS_DATA (sym->assoc->target)
6215 && CLASS_DATA (sym->assoc->target)->as)
6216 {
6217 gfc_ref *ref = e->ref;
6218 while (ref)
6219 {
6220 switch (ref->type)
6221 {
6222 case REF_COMPONENT:
6223 ref->u.c.sym = sym->ts.u.derived;
6224 /* Stop the loop. */
6225 ref = NULL;
6226 break;
6227 default:
6228 ref = ref->next;
6229 break;
6230 }
6231 }
6232 }
6233
6234 /* If this is an associate-name, it may be parsed with an array reference
6235 in error even though the target is scalar. Fail directly in this case.
6236 TODO Understand why class scalar expressions must be excluded. */
6237 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6238 {
6239 if (sym->ts.type == BT_CLASS)
6240 gfc_fix_class_refs (e);
6241 if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6242 && e->ref->type == REF_ARRAY)
6243 {
6244 /* Unambiguously scalar! */
6245 if (sym->assoc->target
6246 && (sym->assoc->target->expr_type == EXPR_CONSTANT
6247 || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6248 gfc_error ("Scalar variable %qs has an array reference at %L",
6249 sym->name, &e->where);
6250 return false;
6251 }
6252 else if ((sym->attr.dimension || sym->attr.codimension)
6253 && (!e->ref || e->ref->type != REF_ARRAY))
6254 {
6255 /* This can happen because the parser did not detect that the
6256 associate name is an array and the expression had no array
6257 part_ref. */
6258 gfc_ref *ref = gfc_get_ref ();
6259 ref->type = REF_ARRAY;
6260 ref->u.ar.type = AR_FULL;
6261 if (sym->as)
6262 {
6263 ref->u.ar.as = sym->as;
6264 ref->u.ar.dimen = sym->as->rank;
6265 }
6266 ref->next = e->ref;
6267 e->ref = ref;
6268 }
6269 }
6270
6271 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6272 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6273
6274 /* On the other hand, the parser may not have known this is an array;
6275 in this case, we have to add a FULL reference. */
6276 if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6277 {
6278 e->ref = gfc_get_ref ();
6279 e->ref->type = REF_ARRAY;
6280 e->ref->u.ar.type = AR_FULL;
6281 e->ref->u.ar.dimen = 0;
6282 }
6283
6284 /* Like above, but for class types, where the checking whether an array
6285 ref is present is more complicated. Furthermore make sure not to add
6286 the full array ref to _vptr or _len refs. */
6287 if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6288 && CLASS_DATA (sym)
6289 && (CLASS_DATA (sym)->attr.dimension
6290 || CLASS_DATA (sym)->attr.codimension)
6291 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6292 {
6293 gfc_ref *ref, *newref;
6294
6295 newref = gfc_get_ref ();
6296 newref->type = REF_ARRAY;
6297 newref->u.ar.type = AR_FULL;
6298 newref->u.ar.dimen = 0;
6299
6300 /* Because this is an associate var and the first ref either is a ref to
6301 the _data component or not, no traversal of the ref chain is
6302 needed. The array ref needs to be inserted after the _data ref,
6303 or when that is not present, which may happened for polymorphic
6304 types, then at the first position. */
6305 ref = e->ref;
6306 if (!ref)
6307 e->ref = newref;
6308 else if (ref->type == REF_COMPONENT
6309 && strcmp ("_data", ref->u.c.component->name) == 0)
6310 {
6311 if (!ref->next || ref->next->type != REF_ARRAY)
6312 {
6313 newref->next = ref->next;
6314 ref->next = newref;
6315 }
6316 else
6317 /* Array ref present already. */
6318 gfc_free_ref_list (newref);
6319 }
6320 else if (ref->type == REF_ARRAY)
6321 /* Array ref present already. */
6322 gfc_free_ref_list (newref);
6323 else
6324 {
6325 newref->next = ref;
6326 e->ref = newref;
6327 }
6328 }
6329 else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6330 {
6331 gfc_ref *ref;
6332 for (ref = e->ref; ref; ref = ref->next)
6333 if (ref->type == REF_SUBSTRING)
6334 break;
6335 if (ref == NULL)
6336 e->ts = sym->ts;
6337 }
6338
6339 if (e->ref && !gfc_resolve_ref (e))
6340 return false;
6341
6342 if (sym->attr.flavor == FL_PROCEDURE
6343 && (!sym->attr.function
6344 || (sym->attr.function && sym->result
6345 && sym->result->attr.proc_pointer
6346 && !sym->result->attr.function)))
6347 {
6348 e->ts.type = BT_PROCEDURE;
6349 goto resolve_procedure;
6350 }
6351
6352 if (sym->ts.type != BT_UNKNOWN)
6353 gfc_variable_attr (e, &e->ts);
6354 else if (sym->attr.flavor == FL_PROCEDURE
6355 && sym->attr.function && sym->result
6356 && sym->result->ts.type != BT_UNKNOWN
6357 && sym->result->attr.proc_pointer)
6358 e->ts = sym->result->ts;
6359 else
6360 {
6361 /* Must be a simple variable reference. */
6362 if (!gfc_set_default_type (sym, 1, sym->ns))
6363 return false;
6364 e->ts = sym->ts;
6365 }
6366
6367 if (check_assumed_size_reference (sym, e))
6368 return false;
6369
6370 /* Deal with forward references to entries during gfc_resolve_code, to
6371 satisfy, at least partially, 12.5.2.5. */
6372 if (gfc_current_ns->entries
6373 && current_entry_id == sym->entry_id
6374 && cs_base
6375 && cs_base->current
6376 && cs_base->current->op != EXEC_ENTRY)
6377 {
6378 int n;
6379 bool saved_specification_expr;
6380 gfc_symbol *saved_specification_expr_symbol;
6381
6382 /* If the symbol is a dummy... */
6383 if (sym->attr.dummy && sym->ns == gfc_current_ns)
6384 {
6385 /* If it has not been seen as a dummy, this is an error. */
6386 if (!entry_dummy_seen_p (sym))
6387 {
6388 if (specification_expr
6389 && specification_expr_symbol
6390 && specification_expr_symbol->attr.dummy
6391 && specification_expr_symbol->ns == gfc_current_ns
6392 && !entry_dummy_seen_p (specification_expr_symbol))
6393 ;
6394 else if (specification_expr)
6395 gfc_error ("Variable %qs, used in a specification expression"
6396 ", is referenced at %L before the ENTRY statement "
6397 "in which it is a parameter",
6398 sym->name, &cs_base->current->loc);
6399 else
6400 gfc_error ("Variable %qs is used at %L before the ENTRY "
6401 "statement in which it is a parameter",
6402 sym->name, &cs_base->current->loc);
6403 t = false;
6404 }
6405 }
6406
6407 /* Now do the same check on the specification expressions. */
6408 saved_specification_expr = specification_expr;
6409 saved_specification_expr_symbol = specification_expr_symbol;
6410 specification_expr = true;
6411 specification_expr_symbol = sym;
6412 if (sym->ts.type == BT_CHARACTER
6413 && !gfc_resolve_expr (sym->ts.u.cl->length))
6414 t = false;
6415
6416 if (sym->as)
6417 {
6418 for (n = 0; n < sym->as->rank; n++)
6419 {
6420 if (!gfc_resolve_expr (sym->as->lower[n]))
6421 t = false;
6422 if (!gfc_resolve_expr (sym->as->upper[n]))
6423 t = false;
6424 }
6425 }
6426 specification_expr = saved_specification_expr;
6427 specification_expr_symbol = saved_specification_expr_symbol;
6428
6429 if (t)
6430 /* Update the symbol's entry level. */
6431 sym->entry_id = current_entry_id + 1;
6432 }
6433
6434 /* If a symbol has been host_associated mark it. This is used latter,
6435 to identify if aliasing is possible via host association. */
6436 if (sym->attr.flavor == FL_VARIABLE
6437 && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6438 || !sym->ns->code->ext.block.assoc)
6439 && gfc_current_ns->parent
6440 && (gfc_current_ns->parent == sym->ns
6441 || (gfc_current_ns->parent->parent
6442 && gfc_current_ns->parent->parent == sym->ns)))
6443 sym->attr.host_assoc = 1;
6444
6445 if (gfc_current_ns->proc_name
6446 && sym->attr.dimension
6447 && (sym->ns != gfc_current_ns
6448 || sym->attr.use_assoc
6449 || sym->attr.in_common))
6450 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6451
6452 resolve_procedure:
6453 if (t && !resolve_procedure_expression (e))
6454 t = false;
6455
6456 /* F2008, C617 and C1229. */
6457 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6458 && gfc_is_coindexed (e))
6459 {
6460 gfc_ref *ref, *ref2 = NULL;
6461
6462 for (ref = e->ref; ref; ref = ref->next)
6463 {
6464 if (ref->type == REF_COMPONENT)
6465 ref2 = ref;
6466 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6467 break;
6468 }
6469
6470 for ( ; ref; ref = ref->next)
6471 if (ref->type == REF_COMPONENT)
6472 break;
6473
6474 /* Expression itself is not coindexed object. */
6475 if (ref && e->ts.type == BT_CLASS)
6476 {
6477 gfc_error ("Polymorphic subobject of coindexed object at %L",
6478 &e->where);
6479 t = false;
6480 }
6481
6482 /* Expression itself is coindexed object. */
6483 if (ref == NULL)
6484 {
6485 gfc_component *c;
6486 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6487 for ( ; c; c = c->next)
6488 if (c->attr.allocatable && c->ts.type == BT_CLASS)
6489 {
6490 gfc_error ("Coindexed object with polymorphic allocatable "
6491 "subcomponent at %L", &e->where);
6492 t = false;
6493 break;
6494 }
6495 }
6496 }
6497
6498 if (t)
6499 gfc_expression_rank (e);
6500
6501 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6502 gfc_warning (OPT_Wdeprecated_declarations,
6503 "Using variable %qs at %L is deprecated",
6504 sym->name, &e->where);
6505 /* Simplify cases where access to a parameter array results in a
6506 single constant. Suppress errors since those will have been
6507 issued before, as warnings. */
6508 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6509 {
6510 gfc_push_suppress_errors ();
6511 gfc_simplify_expr (e, 1);
6512 gfc_pop_suppress_errors ();
6513 }
6514
6515 return t;
6516 }
6517
6518
6519 /* 'sym' was initially guessed to be derived type but has been corrected
6520 in resolve_assoc_var to be a class entity or the derived type correcting.
6521 If a class entity it will certainly need the _data reference or the
6522 reference derived type symbol correcting in the first component ref if
6523 a derived type. */
6524
6525 void
6526 gfc_fixup_inferred_type_refs (gfc_expr *e)
6527 {
6528 gfc_ref *ref, *new_ref;
6529 gfc_symbol *sym, *derived;
6530 gfc_expr *target;
6531 sym = e->symtree->n.sym;
6532
6533 /* An associate_name whose selector is (i) a component ref of a selector
6534 that is a inferred type associate_name; or (ii) an intrinsic type that
6535 has been inferred from an inquiry ref. */
6536 if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6537 {
6538 sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6539 sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6540 if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6541 {
6542 ref = e->ref;
6543 /* A substring misidentified as an array section. */
6544 if (sym->ts.type == BT_CHARACTER
6545 && ref->u.ar.start[0] && ref->u.ar.end[0]
6546 && !ref->u.ar.stride[0])
6547 {
6548 new_ref = gfc_get_ref ();
6549 new_ref->type = REF_SUBSTRING;
6550 new_ref->u.ss.start = ref->u.ar.start[0];
6551 new_ref->u.ss.end = ref->u.ar.end[0];
6552 new_ref->u.ss.length = sym->ts.u.cl;
6553 *ref = *new_ref;
6554 free (new_ref);
6555 }
6556 else
6557 {
6558 if (e->ref->u.ar.type == AR_UNKNOWN)
6559 gfc_error ("Invalid array reference at %L", &e->where);
6560 e->ref = ref->next;
6561 free (ref);
6562 }
6563 }
6564
6565 /* It is possible for an inquiry reference to be mistaken for a
6566 component reference. Correct this now. */
6567 ref = e->ref;
6568 if (ref && ref->type == REF_ARRAY)
6569 ref = ref->next;
6570 if (ref && ref->type == REF_COMPONENT
6571 && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6572 {
6573 e->symtree->n.sym = sym;
6574 *ref = *new_ref;
6575 gfc_free_ref_list (new_ref);
6576 }
6577
6578 /* The kind of the associate name is best evaluated directly from the
6579 selector because of the guesses made in primary.cc, when the type
6580 is still unknown. */
6581 if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6582 {
6583 gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6584 sym->assoc->target->ts.kind);
6585 gfc_replace_expr (e, ne);
6586 }
6587
6588 /* Now that the references are all sorted out, set the expression rank
6589 and return. */
6590 gfc_expression_rank (e);
6591 return;
6592 }
6593
6594 derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6595 : sym->ts.u.derived;
6596
6597 /* Ensure that class symbols have an array spec and ensure that there
6598 is a _data field reference following class type references. */
6599 if (sym->ts.type == BT_CLASS
6600 && sym->assoc->target->ts.type == BT_CLASS)
6601 {
6602 e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6603 e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6604 sym->attr.dimension = 0;
6605 sym->attr.codimension = 0;
6606 CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6607 CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6608 if (e->ref && (e->ref->type != REF_COMPONENT
6609 || e->ref->u.c.component->name[0] != '_'))
6610 {
6611 ref = gfc_get_ref ();
6612 ref->type = REF_COMPONENT;
6613 ref->next = e->ref;
6614 e->ref = ref;
6615 ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6616 true, true, NULL);
6617 ref->u.c.sym = sym->ts.u.derived;
6618 }
6619 }
6620
6621 /* Proceed as far as the first component reference and ensure that the
6622 correct derived type is being used. */
6623 for (ref = e->ref; ref; ref = ref->next)
6624 if (ref->type == REF_COMPONENT)
6625 {
6626 if (ref->u.c.component->name[0] != '_')
6627 ref->u.c.sym = derived;
6628 else
6629 ref->u.c.sym = sym->ts.u.derived;
6630 break;
6631 }
6632
6633 /* Verify that the type inferrence mechanism has not introduced a spurious
6634 array reference. This can happen with an associate name, whose selector
6635 is an element of another inferred type. */
6636 target = e->symtree->n.sym->assoc->target;
6637 if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6638 && e != target && !target->rank)
6639 {
6640 /* First case: array ref after the scalar class or derived
6641 associate_name. */
6642 if (e->ref && e->ref->type == REF_ARRAY
6643 && e->ref->u.ar.type != AR_ELEMENT)
6644 {
6645 ref = e->ref;
6646 if (ref->u.ar.type == AR_UNKNOWN)
6647 gfc_error ("Invalid array reference at %L", &e->where);
6648 e->ref = ref->next;
6649 free (ref);
6650
6651 /* If it hasn't a ref to the '_data' field supply one. */
6652 if (sym->ts.type == BT_CLASS
6653 && !(e->ref->type == REF_COMPONENT
6654 && strcmp (e->ref->u.c.component->name, "_data")))
6655 {
6656 gfc_ref *new_ref;
6657 gfc_find_component (e->symtree->n.sym->ts.u.derived,
6658 "_data", true, true, &new_ref);
6659 new_ref->next = e->ref;
6660 e->ref = new_ref;
6661 }
6662 }
6663 /* 2nd case: a ref to the '_data' field followed by an array ref. */
6664 else if (e->ref && e->ref->type == REF_COMPONENT
6665 && strcmp (e->ref->u.c.component->name, "_data") == 0
6666 && e->ref->next && e->ref->next->type == REF_ARRAY
6667 && e->ref->next->u.ar.type != AR_ELEMENT)
6668 {
6669 ref = e->ref->next;
6670 if (ref->u.ar.type == AR_UNKNOWN)
6671 gfc_error ("Invalid array reference at %L", &e->where);
6672 e->ref->next = e->ref->next->next;
6673 free (ref);
6674 }
6675 }
6676
6677 /* Now that all the references are OK, get the expression rank. */
6678 gfc_expression_rank (e);
6679 }
6680
6681
6682 /* Checks to see that the correct symbol has been host associated.
6683 The only situations where this arises are:
6684 (i) That in which a twice contained function is parsed after
6685 the host association is made. On detecting this, change
6686 the symbol in the expression and convert the array reference
6687 into an actual arglist if the old symbol is a variable; or
6688 (ii) That in which an external function is typed but not declared
6689 explicitly to be external. Here, the old symbol is changed
6690 from a variable to an external function. */
6691 static bool
6692 check_host_association (gfc_expr *e)
6693 {
6694 gfc_symbol *sym, *old_sym;
6695 gfc_symtree *st;
6696 int n;
6697 gfc_ref *ref;
6698 gfc_actual_arglist *arg, *tail = NULL;
6699 bool retval = e->expr_type == EXPR_FUNCTION;
6700
6701 /* If the expression is the result of substitution in
6702 interface.cc(gfc_extend_expr) because there is no way in
6703 which the host association can be wrong. */
6704 if (e->symtree == NULL
6705 || e->symtree->n.sym == NULL
6706 || e->user_operator)
6707 return retval;
6708
6709 old_sym = e->symtree->n.sym;
6710
6711 if (gfc_current_ns->parent
6712 && old_sym->ns != gfc_current_ns)
6713 {
6714 /* Use the 'USE' name so that renamed module symbols are
6715 correctly handled. */
6716 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6717
6718 if (sym && old_sym != sym
6719 && sym->attr.flavor == FL_PROCEDURE
6720 && sym->attr.contained)
6721 {
6722 /* Clear the shape, since it might not be valid. */
6723 gfc_free_shape (&e->shape, e->rank);
6724
6725 /* Give the expression the right symtree! */
6726 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6727 gcc_assert (st != NULL);
6728
6729 if (old_sym->attr.flavor == FL_PROCEDURE
6730 || e->expr_type == EXPR_FUNCTION)
6731 {
6732 /* Original was function so point to the new symbol, since
6733 the actual argument list is already attached to the
6734 expression. */
6735 e->value.function.esym = NULL;
6736 e->symtree = st;
6737 }
6738 else
6739 {
6740 /* Original was variable so convert array references into
6741 an actual arglist. This does not need any checking now
6742 since resolve_function will take care of it. */
6743 e->value.function.actual = NULL;
6744 e->expr_type = EXPR_FUNCTION;
6745 e->symtree = st;
6746
6747 /* Ambiguity will not arise if the array reference is not
6748 the last reference. */
6749 for (ref = e->ref; ref; ref = ref->next)
6750 if (ref->type == REF_ARRAY && ref->next == NULL)
6751 break;
6752
6753 if ((ref == NULL || ref->type != REF_ARRAY)
6754 && sym->attr.proc == PROC_INTERNAL)
6755 {
6756 gfc_error ("%qs at %L is host associated at %L into "
6757 "a contained procedure with an internal "
6758 "procedure of the same name", sym->name,
6759 &old_sym->declared_at, &e->where);
6760 return false;
6761 }
6762
6763 if (ref == NULL)
6764 return false;
6765
6766 gcc_assert (ref->type == REF_ARRAY);
6767
6768 /* Grab the start expressions from the array ref and
6769 copy them into actual arguments. */
6770 for (n = 0; n < ref->u.ar.dimen; n++)
6771 {
6772 arg = gfc_get_actual_arglist ();
6773 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6774 if (e->value.function.actual == NULL)
6775 tail = e->value.function.actual = arg;
6776 else
6777 {
6778 tail->next = arg;
6779 tail = arg;
6780 }
6781 }
6782
6783 /* Dump the reference list and set the rank. */
6784 gfc_free_ref_list (e->ref);
6785 e->ref = NULL;
6786 e->rank = sym->as ? sym->as->rank : 0;
6787 e->corank = sym->as ? sym->as->corank : 0;
6788 }
6789
6790 gfc_resolve_expr (e);
6791 sym->refs++;
6792 }
6793 /* This case corresponds to a call, from a block or a contained
6794 procedure, to an external function, which has not been declared
6795 as being external in the main program but has been typed. */
6796 else if (sym && old_sym != sym
6797 && !e->ref
6798 && sym->ts.type == BT_UNKNOWN
6799 && old_sym->ts.type != BT_UNKNOWN
6800 && sym->attr.flavor == FL_PROCEDURE
6801 && old_sym->attr.flavor == FL_VARIABLE
6802 && sym->ns->parent == old_sym->ns
6803 && sym->ns->proc_name
6804 && sym->ns->proc_name->attr.proc != PROC_MODULE
6805 && (sym->ns->proc_name->attr.flavor == FL_LABEL
6806 || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
6807 {
6808 old_sym->attr.flavor = FL_PROCEDURE;
6809 old_sym->attr.external = 1;
6810 old_sym->attr.function = 1;
6811 old_sym->result = old_sym;
6812 gfc_resolve_expr (e);
6813 }
6814 }
6815 /* This might have changed! */
6816 return e->expr_type == EXPR_FUNCTION;
6817 }
6818
6819
6820 static void
6821 gfc_resolve_character_operator (gfc_expr *e)
6822 {
6823 gfc_expr *op1 = e->value.op.op1;
6824 gfc_expr *op2 = e->value.op.op2;
6825 gfc_expr *e1 = NULL;
6826 gfc_expr *e2 = NULL;
6827
6828 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6829
6830 if (op1->ts.u.cl && op1->ts.u.cl->length)
6831 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6832 else if (op1->expr_type == EXPR_CONSTANT)
6833 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6834 op1->value.character.length);
6835
6836 if (op2->ts.u.cl && op2->ts.u.cl->length)
6837 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6838 else if (op2->expr_type == EXPR_CONSTANT)
6839 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6840 op2->value.character.length);
6841
6842 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6843
6844 if (!e1 || !e2)
6845 {
6846 gfc_free_expr (e1);
6847 gfc_free_expr (e2);
6848
6849 return;
6850 }
6851
6852 e->ts.u.cl->length = gfc_add (e1, e2);
6853 e->ts.u.cl->length->ts.type = BT_INTEGER;
6854 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6855 gfc_simplify_expr (e->ts.u.cl->length, 0);
6856 gfc_resolve_expr (e->ts.u.cl->length);
6857
6858 return;
6859 }
6860
6861
6862 /* Ensure that an character expression has a charlen and, if possible, a
6863 length expression. */
6864
6865 static void
6866 fixup_charlen (gfc_expr *e)
6867 {
6868 /* The cases fall through so that changes in expression type and the need
6869 for multiple fixes are picked up. In all circumstances, a charlen should
6870 be available for the middle end to hang a backend_decl on. */
6871 switch (e->expr_type)
6872 {
6873 case EXPR_OP:
6874 gfc_resolve_character_operator (e);
6875 /* FALLTHRU */
6876
6877 case EXPR_ARRAY:
6878 if (e->expr_type == EXPR_ARRAY)
6879 gfc_resolve_character_array_constructor (e);
6880 /* FALLTHRU */
6881
6882 case EXPR_SUBSTRING:
6883 if (!e->ts.u.cl && e->ref)
6884 gfc_resolve_substring_charlen (e);
6885 /* FALLTHRU */
6886
6887 default:
6888 if (!e->ts.u.cl)
6889 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6890
6891 break;
6892 }
6893 }
6894
6895
6896 /* Update an actual argument to include the passed-object for type-bound
6897 procedures at the right position. */
6898
6899 static gfc_actual_arglist*
6900 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6901 const char *name)
6902 {
6903 gcc_assert (argpos > 0);
6904
6905 if (argpos == 1)
6906 {
6907 gfc_actual_arglist* result;
6908
6909 result = gfc_get_actual_arglist ();
6910 result->expr = po;
6911 result->next = lst;
6912 if (name)
6913 result->name = name;
6914
6915 return result;
6916 }
6917
6918 if (lst)
6919 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6920 else
6921 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6922 return lst;
6923 }
6924
6925
6926 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6927
6928 static gfc_expr*
6929 extract_compcall_passed_object (gfc_expr* e)
6930 {
6931 gfc_expr* po;
6932
6933 if (e->expr_type == EXPR_UNKNOWN)
6934 {
6935 gfc_error ("Error in typebound call at %L",
6936 &e->where);
6937 return NULL;
6938 }
6939
6940 gcc_assert (e->expr_type == EXPR_COMPCALL);
6941
6942 if (e->value.compcall.base_object)
6943 po = gfc_copy_expr (e->value.compcall.base_object);
6944 else
6945 {
6946 po = gfc_get_expr ();
6947 po->expr_type = EXPR_VARIABLE;
6948 po->symtree = e->symtree;
6949 po->ref = gfc_copy_ref (e->ref);
6950 po->where = e->where;
6951 }
6952
6953 if (!gfc_resolve_expr (po))
6954 return NULL;
6955
6956 return po;
6957 }
6958
6959
6960 /* Update the arglist of an EXPR_COMPCALL expression to include the
6961 passed-object. */
6962
6963 static bool
6964 update_compcall_arglist (gfc_expr* e)
6965 {
6966 gfc_expr* po;
6967 gfc_typebound_proc* tbp;
6968
6969 tbp = e->value.compcall.tbp;
6970
6971 if (tbp->error)
6972 return false;
6973
6974 po = extract_compcall_passed_object (e);
6975 if (!po)
6976 return false;
6977
6978 if (tbp->nopass || e->value.compcall.ignore_pass)
6979 {
6980 gfc_free_expr (po);
6981 return true;
6982 }
6983
6984 if (tbp->pass_arg_num <= 0)
6985 return false;
6986
6987 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6988 tbp->pass_arg_num,
6989 tbp->pass_arg);
6990
6991 return true;
6992 }
6993
6994
6995 /* Extract the passed object from a PPC call (a copy of it). */
6996
6997 static gfc_expr*
6998 extract_ppc_passed_object (gfc_expr *e)
6999 {
7000 gfc_expr *po;
7001 gfc_ref **ref;
7002
7003 po = gfc_get_expr ();
7004 po->expr_type = EXPR_VARIABLE;
7005 po->symtree = e->symtree;
7006 po->ref = gfc_copy_ref (e->ref);
7007 po->where = e->where;
7008
7009 /* Remove PPC reference. */
7010 ref = &po->ref;
7011 while ((*ref)->next)
7012 ref = &(*ref)->next;
7013 gfc_free_ref_list (*ref);
7014 *ref = NULL;
7015
7016 if (!gfc_resolve_expr (po))
7017 return NULL;
7018
7019 return po;
7020 }
7021
7022
7023 /* Update the actual arglist of a procedure pointer component to include the
7024 passed-object. */
7025
7026 static bool
7027 update_ppc_arglist (gfc_expr* e)
7028 {
7029 gfc_expr* po;
7030 gfc_component *ppc;
7031 gfc_typebound_proc* tb;
7032
7033 ppc = gfc_get_proc_ptr_comp (e);
7034 if (!ppc)
7035 return false;
7036
7037 tb = ppc->tb;
7038
7039 if (tb->error)
7040 return false;
7041 else if (tb->nopass)
7042 return true;
7043
7044 po = extract_ppc_passed_object (e);
7045 if (!po)
7046 return false;
7047
7048 /* F08:R739. */
7049 if (po->rank != 0)
7050 {
7051 gfc_error ("Passed-object at %L must be scalar", &e->where);
7052 return false;
7053 }
7054
7055 /* F08:C611. */
7056 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7057 {
7058 gfc_error ("Base object for procedure-pointer component call at %L is of"
7059 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7060 return false;
7061 }
7062
7063 gcc_assert (tb->pass_arg_num > 0);
7064 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7065 tb->pass_arg_num,
7066 tb->pass_arg);
7067
7068 return true;
7069 }
7070
7071
7072 /* Check that the object a TBP is called on is valid, i.e. it must not be
7073 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7074
7075 static bool
7076 check_typebound_baseobject (gfc_expr* e)
7077 {
7078 gfc_expr* base;
7079 bool return_value = false;
7080
7081 base = extract_compcall_passed_object (e);
7082 if (!base)
7083 return false;
7084
7085 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7086 {
7087 gfc_error ("Error in typebound call at %L", &e->where);
7088 goto cleanup;
7089 }
7090
7091 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7092 return false;
7093
7094 /* F08:C611. */
7095 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7096 {
7097 gfc_error ("Base object for type-bound procedure call at %L is of"
7098 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7099 goto cleanup;
7100 }
7101
7102 /* F08:C1230. If the procedure called is NOPASS,
7103 the base object must be scalar. */
7104 if (e->value.compcall.tbp->nopass && base->rank != 0)
7105 {
7106 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7107 " be scalar", &e->where);
7108 goto cleanup;
7109 }
7110
7111 return_value = true;
7112
7113 cleanup:
7114 gfc_free_expr (base);
7115 return return_value;
7116 }
7117
7118
7119 /* Resolve a call to a type-bound procedure, either function or subroutine,
7120 statically from the data in an EXPR_COMPCALL expression. The adapted
7121 arglist and the target-procedure symtree are returned. */
7122
7123 static bool
7124 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7125 gfc_actual_arglist** actual)
7126 {
7127 gcc_assert (e->expr_type == EXPR_COMPCALL);
7128 gcc_assert (!e->value.compcall.tbp->is_generic);
7129
7130 /* Update the actual arglist for PASS. */
7131 if (!update_compcall_arglist (e))
7132 return false;
7133
7134 *actual = e->value.compcall.actual;
7135 *target = e->value.compcall.tbp->u.specific;
7136
7137 gfc_free_ref_list (e->ref);
7138 e->ref = NULL;
7139 e->value.compcall.actual = NULL;
7140
7141 /* If we find a deferred typebound procedure, check for derived types
7142 that an overriding typebound procedure has not been missed. */
7143 if (e->value.compcall.name
7144 && !e->value.compcall.tbp->non_overridable
7145 && e->value.compcall.base_object
7146 && e->value.compcall.base_object->ts.type == BT_DERIVED)
7147 {
7148 gfc_symtree *st;
7149 gfc_symbol *derived;
7150
7151 /* Use the derived type of the base_object. */
7152 derived = e->value.compcall.base_object->ts.u.derived;
7153 st = NULL;
7154
7155 /* If necessary, go through the inheritance chain. */
7156 while (!st && derived)
7157 {
7158 /* Look for the typebound procedure 'name'. */
7159 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7160 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7161 e->value.compcall.name);
7162 if (!st)
7163 derived = gfc_get_derived_super_type (derived);
7164 }
7165
7166 /* Now find the specific name in the derived type namespace. */
7167 if (st && st->n.tb && st->n.tb->u.specific)
7168 gfc_find_sym_tree (st->n.tb->u.specific->name,
7169 derived->ns, 1, &st);
7170 if (st)
7171 *target = st;
7172 }
7173
7174 if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7175 && !e->value.compcall.tbp->deferred)
7176 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7177 " itself recursively. Declare it RECURSIVE or use"
7178 " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7179
7180 return true;
7181 }
7182
7183
7184 /* Get the ultimate declared type from an expression. In addition,
7185 return the last class/derived type reference and the copy of the
7186 reference list. If check_types is set true, derived types are
7187 identified as well as class references. */
7188 static gfc_symbol*
7189 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7190 gfc_expr *e, bool check_types)
7191 {
7192 gfc_symbol *declared;
7193 gfc_ref *ref;
7194
7195 declared = NULL;
7196 if (class_ref)
7197 *class_ref = NULL;
7198 if (new_ref)
7199 *new_ref = gfc_copy_ref (e->ref);
7200
7201 for (ref = e->ref; ref; ref = ref->next)
7202 {
7203 if (ref->type != REF_COMPONENT)
7204 continue;
7205
7206 if ((ref->u.c.component->ts.type == BT_CLASS
7207 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7208 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7209 {
7210 declared = ref->u.c.component->ts.u.derived;
7211 if (class_ref)
7212 *class_ref = ref;
7213 }
7214 }
7215
7216 if (declared == NULL)
7217 declared = e->symtree->n.sym->ts.u.derived;
7218
7219 return declared;
7220 }
7221
7222
7223 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7224 which of the specific bindings (if any) matches the arglist and transform
7225 the expression into a call of that binding. */
7226
7227 static bool
7228 resolve_typebound_generic_call (gfc_expr* e, const char **name)
7229 {
7230 gfc_typebound_proc* genproc;
7231 const char* genname;
7232 gfc_symtree *st;
7233 gfc_symbol *derived;
7234
7235 gcc_assert (e->expr_type == EXPR_COMPCALL);
7236 genname = e->value.compcall.name;
7237 genproc = e->value.compcall.tbp;
7238
7239 if (!genproc->is_generic)
7240 return true;
7241
7242 /* Try the bindings on this type and in the inheritance hierarchy. */
7243 for (; genproc; genproc = genproc->overridden)
7244 {
7245 gfc_tbp_generic* g;
7246
7247 gcc_assert (genproc->is_generic);
7248 for (g = genproc->u.generic; g; g = g->next)
7249 {
7250 gfc_symbol* target;
7251 gfc_actual_arglist* args;
7252 bool matches;
7253
7254 gcc_assert (g->specific);
7255
7256 if (g->specific->error)
7257 continue;
7258
7259 target = g->specific->u.specific->n.sym;
7260
7261 /* Get the right arglist by handling PASS/NOPASS. */
7262 args = gfc_copy_actual_arglist (e->value.compcall.actual);
7263 if (!g->specific->nopass)
7264 {
7265 gfc_expr* po;
7266 po = extract_compcall_passed_object (e);
7267 if (!po)
7268 {
7269 gfc_free_actual_arglist (args);
7270 return false;
7271 }
7272
7273 gcc_assert (g->specific->pass_arg_num > 0);
7274 gcc_assert (!g->specific->error);
7275 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7276 g->specific->pass_arg);
7277 }
7278 resolve_actual_arglist (args, target->attr.proc,
7279 is_external_proc (target)
7280 && gfc_sym_get_dummy_args (target) == NULL);
7281
7282 /* Check if this arglist matches the formal. */
7283 matches = gfc_arglist_matches_symbol (&args, target);
7284
7285 /* Clean up and break out of the loop if we've found it. */
7286 gfc_free_actual_arglist (args);
7287 if (matches)
7288 {
7289 e->value.compcall.tbp = g->specific;
7290 genname = g->specific_st->name;
7291 /* Pass along the name for CLASS methods, where the vtab
7292 procedure pointer component has to be referenced. */
7293 if (name)
7294 *name = genname;
7295 goto success;
7296 }
7297 }
7298 }
7299
7300 /* Nothing matching found! */
7301 gfc_error ("Found no matching specific binding for the call to the GENERIC"
7302 " %qs at %L", genname, &e->where);
7303 return false;
7304
7305 success:
7306 /* Make sure that we have the right specific instance for the name. */
7307 derived = get_declared_from_expr (NULL, NULL, e, true);
7308
7309 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7310 if (st)
7311 e->value.compcall.tbp = st->n.tb;
7312
7313 return true;
7314 }
7315
7316
7317 /* Resolve a call to a type-bound subroutine. */
7318
7319 static bool
7320 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7321 {
7322 gfc_actual_arglist* newactual;
7323 gfc_symtree* target;
7324
7325 /* Check that's really a SUBROUTINE. */
7326 if (!c->expr1->value.compcall.tbp->subroutine)
7327 {
7328 if (!c->expr1->value.compcall.tbp->is_generic
7329 && c->expr1->value.compcall.tbp->u.specific
7330 && c->expr1->value.compcall.tbp->u.specific->n.sym
7331 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7332 c->expr1->value.compcall.tbp->subroutine = 1;
7333 else
7334 {
7335 gfc_error ("%qs at %L should be a SUBROUTINE",
7336 c->expr1->value.compcall.name, &c->loc);
7337 return false;
7338 }
7339 }
7340
7341 if (!check_typebound_baseobject (c->expr1))
7342 return false;
7343
7344 /* Pass along the name for CLASS methods, where the vtab
7345 procedure pointer component has to be referenced. */
7346 if (name)
7347 *name = c->expr1->value.compcall.name;
7348
7349 if (!resolve_typebound_generic_call (c->expr1, name))
7350 return false;
7351
7352 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7353 if (overridable)
7354 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7355
7356 /* Transform into an ordinary EXEC_CALL for now. */
7357
7358 if (!resolve_typebound_static (c->expr1, &target, &newactual))
7359 return false;
7360
7361 c->ext.actual = newactual;
7362 c->symtree = target;
7363 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7364
7365 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7366
7367 gfc_free_expr (c->expr1);
7368 c->expr1 = gfc_get_expr ();
7369 c->expr1->expr_type = EXPR_FUNCTION;
7370 c->expr1->symtree = target;
7371 c->expr1->where = c->loc;
7372
7373 return resolve_call (c);
7374 }
7375
7376
7377 /* Resolve a component-call expression. */
7378 static bool
7379 resolve_compcall (gfc_expr* e, const char **name)
7380 {
7381 gfc_actual_arglist* newactual;
7382 gfc_symtree* target;
7383
7384 /* Check that's really a FUNCTION. */
7385 if (!e->value.compcall.tbp->function)
7386 {
7387 if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7388 gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7389 &e->where);
7390 return false;
7391 }
7392
7393
7394 /* These must not be assign-calls! */
7395 gcc_assert (!e->value.compcall.assign);
7396
7397 if (!check_typebound_baseobject (e))
7398 return false;
7399
7400 /* Pass along the name for CLASS methods, where the vtab
7401 procedure pointer component has to be referenced. */
7402 if (name)
7403 *name = e->value.compcall.name;
7404
7405 if (!resolve_typebound_generic_call (e, name))
7406 return false;
7407 gcc_assert (!e->value.compcall.tbp->is_generic);
7408
7409 /* Take the rank from the function's symbol. */
7410 if (e->value.compcall.tbp->u.specific->n.sym->as)
7411 {
7412 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7413 e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7414 }
7415
7416 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7417 arglist to the TBP's binding target. */
7418
7419 if (!resolve_typebound_static (e, &target, &newactual))
7420 return false;
7421
7422 e->value.function.actual = newactual;
7423 e->value.function.name = NULL;
7424 e->value.function.esym = target->n.sym;
7425 e->value.function.isym = NULL;
7426 e->symtree = target;
7427 e->ts = target->n.sym->ts;
7428 e->expr_type = EXPR_FUNCTION;
7429
7430 /* Resolution is not necessary if this is a class subroutine; this
7431 function only has to identify the specific proc. Resolution of
7432 the call will be done next in resolve_typebound_call. */
7433 return gfc_resolve_expr (e);
7434 }
7435
7436
7437 static bool resolve_fl_derived (gfc_symbol *sym);
7438
7439
7440 /* Resolve a typebound function, or 'method'. First separate all
7441 the non-CLASS references by calling resolve_compcall directly. */
7442
7443 static bool
7444 resolve_typebound_function (gfc_expr* e)
7445 {
7446 gfc_symbol *declared;
7447 gfc_component *c;
7448 gfc_ref *new_ref;
7449 gfc_ref *class_ref;
7450 gfc_symtree *st;
7451 const char *name;
7452 gfc_typespec ts;
7453 gfc_expr *expr;
7454 bool overridable;
7455
7456 st = e->symtree;
7457
7458 /* Deal with typebound operators for CLASS objects. */
7459 expr = e->value.compcall.base_object;
7460 overridable = !e->value.compcall.tbp->non_overridable;
7461 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7462 {
7463 /* Since the typebound operators are generic, we have to ensure
7464 that any delays in resolution are corrected and that the vtab
7465 is present. */
7466 ts = expr->ts;
7467 declared = ts.u.derived;
7468 c = gfc_find_component (declared, "_vptr", true, true, NULL);
7469 if (c->ts.u.derived == NULL)
7470 c->ts.u.derived = gfc_find_derived_vtab (declared);
7471
7472 if (!resolve_compcall (e, &name))
7473 return false;
7474
7475 /* Use the generic name if it is there. */
7476 name = name ? name : e->value.function.esym->name;
7477 e->symtree = expr->symtree;
7478 e->ref = gfc_copy_ref (expr->ref);
7479 get_declared_from_expr (&class_ref, NULL, e, false);
7480
7481 /* Trim away the extraneous references that emerge from nested
7482 use of interface.cc (extend_expr). */
7483 if (class_ref && class_ref->next)
7484 {
7485 gfc_free_ref_list (class_ref->next);
7486 class_ref->next = NULL;
7487 }
7488 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7489 {
7490 gfc_free_ref_list (e->ref);
7491 e->ref = NULL;
7492 }
7493
7494 gfc_add_vptr_component (e);
7495 gfc_add_component_ref (e, name);
7496 e->value.function.esym = NULL;
7497 if (expr->expr_type != EXPR_VARIABLE)
7498 e->base_expr = expr;
7499 return true;
7500 }
7501
7502 if (st == NULL)
7503 return resolve_compcall (e, NULL);
7504
7505 if (!gfc_resolve_ref (e))
7506 return false;
7507
7508 /* Get the CLASS declared type. */
7509 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7510
7511 if (!resolve_fl_derived (declared))
7512 return false;
7513
7514 /* Weed out cases of the ultimate component being a derived type. */
7515 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7516 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7517 {
7518 gfc_free_ref_list (new_ref);
7519 return resolve_compcall (e, NULL);
7520 }
7521
7522 c = gfc_find_component (declared, "_data", true, true, NULL);
7523
7524 /* Treat the call as if it is a typebound procedure, in order to roll
7525 out the correct name for the specific function. */
7526 if (!resolve_compcall (e, &name))
7527 {
7528 gfc_free_ref_list (new_ref);
7529 return false;
7530 }
7531 ts = e->ts;
7532
7533 if (overridable)
7534 {
7535 /* Convert the expression to a procedure pointer component call. */
7536 e->value.function.esym = NULL;
7537 e->symtree = st;
7538
7539 if (new_ref)
7540 e->ref = new_ref;
7541
7542 /* '_vptr' points to the vtab, which contains the procedure pointers. */
7543 gfc_add_vptr_component (e);
7544 gfc_add_component_ref (e, name);
7545
7546 /* Recover the typespec for the expression. This is really only
7547 necessary for generic procedures, where the additional call
7548 to gfc_add_component_ref seems to throw the collection of the
7549 correct typespec. */
7550 e->ts = ts;
7551 }
7552 else if (new_ref)
7553 gfc_free_ref_list (new_ref);
7554
7555 return true;
7556 }
7557
7558 /* Resolve a typebound subroutine, or 'method'. First separate all
7559 the non-CLASS references by calling resolve_typebound_call
7560 directly. */
7561
7562 static bool
7563 resolve_typebound_subroutine (gfc_code *code)
7564 {
7565 gfc_symbol *declared;
7566 gfc_component *c;
7567 gfc_ref *new_ref;
7568 gfc_ref *class_ref;
7569 gfc_symtree *st;
7570 const char *name;
7571 gfc_typespec ts;
7572 gfc_expr *expr;
7573 bool overridable;
7574
7575 st = code->expr1->symtree;
7576
7577 /* Deal with typebound operators for CLASS objects. */
7578 expr = code->expr1->value.compcall.base_object;
7579 overridable = !code->expr1->value.compcall.tbp->non_overridable;
7580 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7581 {
7582 /* If the base_object is not a variable, the corresponding actual
7583 argument expression must be stored in e->base_expression so
7584 that the corresponding tree temporary can be used as the base
7585 object in gfc_conv_procedure_call. */
7586 if (expr->expr_type != EXPR_VARIABLE)
7587 {
7588 gfc_actual_arglist *args;
7589
7590 args= code->expr1->value.function.actual;
7591 for (; args; args = args->next)
7592 if (expr == args->expr)
7593 expr = args->expr;
7594 }
7595
7596 /* Since the typebound operators are generic, we have to ensure
7597 that any delays in resolution are corrected and that the vtab
7598 is present. */
7599 declared = expr->ts.u.derived;
7600 c = gfc_find_component (declared, "_vptr", true, true, NULL);
7601 if (c->ts.u.derived == NULL)
7602 c->ts.u.derived = gfc_find_derived_vtab (declared);
7603
7604 if (!resolve_typebound_call (code, &name, NULL))
7605 return false;
7606
7607 /* Use the generic name if it is there. */
7608 name = name ? name : code->expr1->value.function.esym->name;
7609 code->expr1->symtree = expr->symtree;
7610 code->expr1->ref = gfc_copy_ref (expr->ref);
7611
7612 /* Trim away the extraneous references that emerge from nested
7613 use of interface.cc (extend_expr). */
7614 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7615 if (class_ref && class_ref->next)
7616 {
7617 gfc_free_ref_list (class_ref->next);
7618 class_ref->next = NULL;
7619 }
7620 else if (code->expr1->ref && !class_ref)
7621 {
7622 gfc_free_ref_list (code->expr1->ref);
7623 code->expr1->ref = NULL;
7624 }
7625
7626 /* Now use the procedure in the vtable. */
7627 gfc_add_vptr_component (code->expr1);
7628 gfc_add_component_ref (code->expr1, name);
7629 code->expr1->value.function.esym = NULL;
7630 if (expr->expr_type != EXPR_VARIABLE)
7631 code->expr1->base_expr = expr;
7632 return true;
7633 }
7634
7635 if (st == NULL)
7636 return resolve_typebound_call (code, NULL, NULL);
7637
7638 if (!gfc_resolve_ref (code->expr1))
7639 return false;
7640
7641 /* Get the CLASS declared type. */
7642 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7643
7644 /* Weed out cases of the ultimate component being a derived type. */
7645 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7646 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7647 {
7648 gfc_free_ref_list (new_ref);
7649 return resolve_typebound_call (code, NULL, NULL);
7650 }
7651
7652 if (!resolve_typebound_call (code, &name, &overridable))
7653 {
7654 gfc_free_ref_list (new_ref);
7655 return false;
7656 }
7657 ts = code->expr1->ts;
7658
7659 if (overridable)
7660 {
7661 /* Convert the expression to a procedure pointer component call. */
7662 code->expr1->value.function.esym = NULL;
7663 code->expr1->symtree = st;
7664
7665 if (new_ref)
7666 code->expr1->ref = new_ref;
7667
7668 /* '_vptr' points to the vtab, which contains the procedure pointers. */
7669 gfc_add_vptr_component (code->expr1);
7670 gfc_add_component_ref (code->expr1, name);
7671
7672 /* Recover the typespec for the expression. This is really only
7673 necessary for generic procedures, where the additional call
7674 to gfc_add_component_ref seems to throw the collection of the
7675 correct typespec. */
7676 code->expr1->ts = ts;
7677 }
7678 else if (new_ref)
7679 gfc_free_ref_list (new_ref);
7680
7681 return true;
7682 }
7683
7684
7685 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7686
7687 static bool
7688 resolve_ppc_call (gfc_code* c)
7689 {
7690 gfc_component *comp;
7691
7692 comp = gfc_get_proc_ptr_comp (c->expr1);
7693 gcc_assert (comp != NULL);
7694
7695 c->resolved_sym = c->expr1->symtree->n.sym;
7696 c->expr1->expr_type = EXPR_VARIABLE;
7697
7698 if (!comp->attr.subroutine)
7699 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7700
7701 if (!gfc_resolve_ref (c->expr1))
7702 return false;
7703
7704 if (!update_ppc_arglist (c->expr1))
7705 return false;
7706
7707 c->ext.actual = c->expr1->value.compcall.actual;
7708
7709 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7710 !(comp->ts.interface
7711 && comp->ts.interface->formal)))
7712 return false;
7713
7714 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7715 return false;
7716
7717 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7718
7719 return true;
7720 }
7721
7722
7723 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7724
7725 static bool
7726 resolve_expr_ppc (gfc_expr* e)
7727 {
7728 gfc_component *comp;
7729
7730 comp = gfc_get_proc_ptr_comp (e);
7731 gcc_assert (comp != NULL);
7732
7733 /* Convert to EXPR_FUNCTION. */
7734 e->expr_type = EXPR_FUNCTION;
7735 e->value.function.isym = NULL;
7736 e->value.function.actual = e->value.compcall.actual;
7737 e->ts = comp->ts;
7738 if (comp->as != NULL)
7739 {
7740 e->rank = comp->as->rank;
7741 e->corank = comp->as->corank;
7742 }
7743
7744 if (!comp->attr.function)
7745 gfc_add_function (&comp->attr, comp->name, &e->where);
7746
7747 if (!gfc_resolve_ref (e))
7748 return false;
7749
7750 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7751 !(comp->ts.interface
7752 && comp->ts.interface->formal)))
7753 return false;
7754
7755 if (!update_ppc_arglist (e))
7756 return false;
7757
7758 if (!check_pure_function(e))
7759 return false;
7760
7761 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7762
7763 return true;
7764 }
7765
7766
7767 static bool
7768 gfc_is_expandable_expr (gfc_expr *e)
7769 {
7770 gfc_constructor *con;
7771
7772 if (e->expr_type == EXPR_ARRAY)
7773 {
7774 /* Traverse the constructor looking for variables that are flavor
7775 parameter. Parameters must be expanded since they are fully used at
7776 compile time. */
7777 con = gfc_constructor_first (e->value.constructor);
7778 for (; con; con = gfc_constructor_next (con))
7779 {
7780 if (con->expr->expr_type == EXPR_VARIABLE
7781 && con->expr->symtree
7782 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7783 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7784 return true;
7785 if (con->expr->expr_type == EXPR_ARRAY
7786 && gfc_is_expandable_expr (con->expr))
7787 return true;
7788 }
7789 }
7790
7791 return false;
7792 }
7793
7794
7795 /* Sometimes variables in specification expressions of the result
7796 of module procedures in submodules wind up not being the 'real'
7797 dummy. Find this, if possible, in the namespace of the first
7798 formal argument. */
7799
7800 static void
7801 fixup_unique_dummy (gfc_expr *e)
7802 {
7803 gfc_symtree *st = NULL;
7804 gfc_symbol *s = NULL;
7805
7806 if (e->symtree->n.sym->ns->proc_name
7807 && e->symtree->n.sym->ns->proc_name->formal)
7808 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7809
7810 if (s != NULL)
7811 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7812
7813 if (st != NULL
7814 && st->n.sym != NULL
7815 && st->n.sym->attr.dummy)
7816 e->symtree = st;
7817 }
7818
7819 /* Resolve an expression. That is, make sure that types of operands agree
7820 with their operators, intrinsic operators are converted to function calls
7821 for overloaded types and unresolved function references are resolved. */
7822
7823 bool
7824 gfc_resolve_expr (gfc_expr *e)
7825 {
7826 bool t;
7827 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7828
7829 if (e == NULL || e->do_not_resolve_again)
7830 return true;
7831
7832 /* inquiry_argument only applies to variables. */
7833 inquiry_save = inquiry_argument;
7834 actual_arg_save = actual_arg;
7835 first_actual_arg_save = first_actual_arg;
7836
7837 if (e->expr_type != EXPR_VARIABLE)
7838 {
7839 inquiry_argument = false;
7840 actual_arg = false;
7841 first_actual_arg = false;
7842 }
7843 else if (e->symtree != NULL
7844 && *e->symtree->name == '@'
7845 && e->symtree->n.sym->attr.dummy)
7846 {
7847 /* Deal with submodule specification expressions that are not
7848 found to be referenced in module.cc(read_cleanup). */
7849 fixup_unique_dummy (e);
7850 }
7851
7852 switch (e->expr_type)
7853 {
7854 case EXPR_OP:
7855 t = resolve_operator (e);
7856 break;
7857
7858 case EXPR_FUNCTION:
7859 case EXPR_VARIABLE:
7860
7861 if (check_host_association (e))
7862 t = resolve_function (e);
7863 else
7864 t = resolve_variable (e);
7865
7866 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7867 && e->ref->type != REF_SUBSTRING)
7868 gfc_resolve_substring_charlen (e);
7869
7870 break;
7871
7872 case EXPR_COMPCALL:
7873 t = resolve_typebound_function (e);
7874 break;
7875
7876 case EXPR_SUBSTRING:
7877 t = gfc_resolve_ref (e);
7878 break;
7879
7880 case EXPR_CONSTANT:
7881 case EXPR_NULL:
7882 t = true;
7883 break;
7884
7885 case EXPR_PPC:
7886 t = resolve_expr_ppc (e);
7887 break;
7888
7889 case EXPR_ARRAY:
7890 t = false;
7891 if (!gfc_resolve_ref (e))
7892 break;
7893
7894 t = gfc_resolve_array_constructor (e);
7895 /* Also try to expand a constructor. */
7896 if (t)
7897 {
7898 gfc_expression_rank (e);
7899 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7900 gfc_expand_constructor (e, false);
7901 }
7902
7903 /* This provides the opportunity for the length of constructors with
7904 character valued function elements to propagate the string length
7905 to the expression. */
7906 if (t && e->ts.type == BT_CHARACTER)
7907 {
7908 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7909 here rather then add a duplicate test for it above. */
7910 gfc_expand_constructor (e, false);
7911 t = gfc_resolve_character_array_constructor (e);
7912 }
7913
7914 break;
7915
7916 case EXPR_STRUCTURE:
7917 t = gfc_resolve_ref (e);
7918 if (!t)
7919 break;
7920
7921 t = resolve_structure_cons (e, 0);
7922 if (!t)
7923 break;
7924
7925 t = gfc_simplify_expr (e, 0);
7926 break;
7927
7928 default:
7929 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7930 }
7931
7932 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7933 fixup_charlen (e);
7934
7935 inquiry_argument = inquiry_save;
7936 actual_arg = actual_arg_save;
7937 first_actual_arg = first_actual_arg_save;
7938
7939 /* For some reason, resolving these expressions a second time mangles
7940 the typespec of the expression itself. */
7941 if (t && e->expr_type == EXPR_VARIABLE
7942 && e->symtree->n.sym->attr.select_rank_temporary
7943 && UNLIMITED_POLY (e->symtree->n.sym))
7944 e->do_not_resolve_again = 1;
7945
7946 return t;
7947 }
7948
7949
7950 /* Resolve an expression from an iterator. They must be scalar and have
7951 INTEGER or (optionally) REAL type. */
7952
7953 static bool
7954 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7955 const char *name_msgid)
7956 {
7957 if (!gfc_resolve_expr (expr))
7958 return false;
7959
7960 if (expr->rank != 0)
7961 {
7962 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7963 return false;
7964 }
7965
7966 if (expr->ts.type != BT_INTEGER)
7967 {
7968 if (expr->ts.type == BT_REAL)
7969 {
7970 if (real_ok)
7971 return gfc_notify_std (GFC_STD_F95_DEL,
7972 "%s at %L must be integer",
7973 _(name_msgid), &expr->where);
7974 else
7975 {
7976 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7977 &expr->where);
7978 return false;
7979 }
7980 }
7981 else
7982 {
7983 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7984 return false;
7985 }
7986 }
7987 return true;
7988 }
7989
7990
7991 /* Resolve the expressions in an iterator structure. If REAL_OK is
7992 false allow only INTEGER type iterators, otherwise allow REAL types.
7993 Set own_scope to true for ac-implied-do and data-implied-do as those
7994 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7995
7996 bool
7997 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7998 {
7999 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8000 return false;
8001
8002 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8003 _("iterator variable")))
8004 return false;
8005
8006 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8007 "Start expression in DO loop"))
8008 return false;
8009
8010 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8011 "End expression in DO loop"))
8012 return false;
8013
8014 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8015 "Step expression in DO loop"))
8016 return false;
8017
8018 /* Convert start, end, and step to the same type as var. */
8019 if (iter->start->ts.kind != iter->var->ts.kind
8020 || iter->start->ts.type != iter->var->ts.type)
8021 gfc_convert_type (iter->start, &iter->var->ts, 1);
8022
8023 if (iter->end->ts.kind != iter->var->ts.kind
8024 || iter->end->ts.type != iter->var->ts.type)
8025 gfc_convert_type (iter->end, &iter->var->ts, 1);
8026
8027 if (iter->step->ts.kind != iter->var->ts.kind
8028 || iter->step->ts.type != iter->var->ts.type)
8029 gfc_convert_type (iter->step, &iter->var->ts, 1);
8030
8031 if (iter->step->expr_type == EXPR_CONSTANT)
8032 {
8033 if ((iter->step->ts.type == BT_INTEGER
8034 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8035 || (iter->step->ts.type == BT_REAL
8036 && mpfr_sgn (iter->step->value.real) == 0))
8037 {
8038 gfc_error ("Step expression in DO loop at %L cannot be zero",
8039 &iter->step->where);
8040 return false;
8041 }
8042 }
8043
8044 if (iter->start->expr_type == EXPR_CONSTANT
8045 && iter->end->expr_type == EXPR_CONSTANT
8046 && iter->step->expr_type == EXPR_CONSTANT)
8047 {
8048 int sgn, cmp;
8049 if (iter->start->ts.type == BT_INTEGER)
8050 {
8051 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8052 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8053 }
8054 else
8055 {
8056 sgn = mpfr_sgn (iter->step->value.real);
8057 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8058 }
8059 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8060 gfc_warning (OPT_Wzerotrip,
8061 "DO loop at %L will be executed zero times",
8062 &iter->step->where);
8063 }
8064
8065 if (iter->end->expr_type == EXPR_CONSTANT
8066 && iter->end->ts.type == BT_INTEGER
8067 && iter->step->expr_type == EXPR_CONSTANT
8068 && iter->step->ts.type == BT_INTEGER
8069 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8070 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8071 {
8072 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8073 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8074
8075 if (is_step_positive
8076 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8077 gfc_warning (OPT_Wundefined_do_loop,
8078 "DO loop at %L is undefined as it overflows",
8079 &iter->step->where);
8080 else if (!is_step_positive
8081 && mpz_cmp (iter->end->value.integer,
8082 gfc_integer_kinds[k].min_int) == 0)
8083 gfc_warning (OPT_Wundefined_do_loop,
8084 "DO loop at %L is undefined as it underflows",
8085 &iter->step->where);
8086 }
8087
8088 return true;
8089 }
8090
8091
8092 /* Traversal function for find_forall_index. f == 2 signals that
8093 that variable itself is not to be checked - only the references. */
8094
8095 static bool
8096 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8097 {
8098 if (expr->expr_type != EXPR_VARIABLE)
8099 return false;
8100
8101 /* A scalar assignment */
8102 if (!expr->ref || *f == 1)
8103 {
8104 if (expr->symtree->n.sym == sym)
8105 return true;
8106 else
8107 return false;
8108 }
8109
8110 if (*f == 2)
8111 *f = 1;
8112 return false;
8113 }
8114
8115
8116 /* Check whether the FORALL index appears in the expression or not.
8117 Returns true if SYM is found in EXPR. */
8118
8119 bool
8120 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8121 {
8122 if (gfc_traverse_expr (expr, sym, forall_index, f))
8123 return true;
8124 else
8125 return false;
8126 }
8127
8128 /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8129 This constraint specifies rules for variables in locality-specs. */
8130
8131 static int
8132 do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8133 {
8134 struct check_default_none_data *dt = (struct check_default_none_data *) data;
8135
8136 if ((*expr)->expr_type == EXPR_VARIABLE)
8137 {
8138 gfc_symbol *sym = (*expr)->symtree->n.sym;
8139 for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8140 list; list = list->next)
8141 {
8142 if (list->expr->symtree->n.sym == sym)
8143 {
8144 gfc_error ("Variable %qs referenced in concurrent-header at %L "
8145 "must not appear in LOCAL locality-spec at %L",
8146 sym->name, &(*expr)->where, &list->expr->where);
8147 *walk_subtrees = 0;
8148 return 1;
8149 }
8150 }
8151 }
8152
8153 *walk_subtrees = 1;
8154 return 0;
8155 }
8156
8157 static int
8158 check_default_none_expr (gfc_expr **e, int *, void *data)
8159 {
8160 struct check_default_none_data *d = (struct check_default_none_data*) data;
8161
8162 if ((*e)->expr_type == EXPR_VARIABLE)
8163 {
8164 gfc_symbol *sym = (*e)->symtree->n.sym;
8165
8166 if (d->sym_hash->contains (sym))
8167 sym->mark = 1;
8168
8169 else if (d->default_none)
8170 {
8171 gfc_namespace *ns2 = d->ns;
8172 while (ns2)
8173 {
8174 if (ns2 == sym->ns)
8175 break;
8176 ns2 = ns2->parent;
8177 }
8178 if (ns2 != NULL)
8179 {
8180 gfc_error ("Variable %qs at %L not specified in a locality spec "
8181 "of DO CONCURRENT at %L but required due to "
8182 "DEFAULT (NONE)",
8183 sym->name, &(*e)->where, &d->code->loc);
8184 d->sym_hash->add (sym);
8185 }
8186 }
8187 }
8188 return 0;
8189 }
8190
8191 static void
8192 resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8193 {
8194 struct check_default_none_data data;
8195 data.code = code;
8196 data.sym_hash = new hash_set<gfc_symbol *>;
8197 data.ns = ns;
8198 data.default_none = code->ext.concur.default_none;
8199
8200 for (int locality = 0; locality < LOCALITY_NUM; locality++)
8201 {
8202 const char *name;
8203 switch (locality)
8204 {
8205 case LOCALITY_LOCAL: name = "LOCAL"; break;
8206 case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8207 case LOCALITY_SHARED: name = "SHARED"; break;
8208 case LOCALITY_REDUCE: name = "REDUCE"; break;
8209 default: gcc_unreachable ();
8210 }
8211
8212 for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8213 list = list->next)
8214 {
8215 gfc_expr *expr = list->expr;
8216
8217 if (locality == LOCALITY_REDUCE
8218 && (expr->expr_type == EXPR_FUNCTION
8219 || expr->expr_type == EXPR_OP))
8220 continue;
8221
8222 if (!gfc_resolve_expr (expr))
8223 continue;
8224
8225 if (expr->expr_type != EXPR_VARIABLE
8226 || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8227 || (expr->ref
8228 && (expr->ref->type != REF_ARRAY
8229 || expr->ref->u.ar.type != AR_FULL
8230 || expr->ref->next)))
8231 {
8232 gfc_error ("Expected variable name in %s locality spec at %L",
8233 name, &expr->where);
8234 continue;
8235 }
8236
8237 gfc_symbol *sym = expr->symtree->n.sym;
8238
8239 if (data.sym_hash->contains (sym))
8240 {
8241 gfc_error ("Variable %qs at %L has already been specified in a "
8242 "locality-spec", sym->name, &expr->where);
8243 continue;
8244 }
8245
8246 for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8247 iter; iter = iter->next)
8248 {
8249 if (iter->var->symtree->n.sym == sym)
8250 {
8251 gfc_error ("Index variable %qs at %L cannot be specified in a "
8252 "locality-spec", sym->name, &expr->where);
8253 continue;
8254 }
8255
8256 data.sym_hash->add (iter->var->symtree->n.sym);
8257 }
8258
8259 if (locality == LOCALITY_LOCAL
8260 || locality == LOCALITY_LOCAL_INIT
8261 || locality == LOCALITY_REDUCE)
8262 {
8263 if (sym->attr.optional)
8264 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8265 "locality-spec at %L",
8266 sym->name, name, &expr->where);
8267
8268 if (sym->attr.dimension
8269 && sym->as
8270 && sym->as->type == AS_ASSUMED_SIZE)
8271 gfc_error ("Assumed-size array not permitted for %qs in %s "
8272 "locality-spec at %L",
8273 sym->name, name, &expr->where);
8274
8275 gfc_check_vardef_context (expr, false, false, false, name);
8276 }
8277
8278 if (locality == LOCALITY_LOCAL
8279 || locality == LOCALITY_LOCAL_INIT)
8280 {
8281 symbol_attribute attr = gfc_expr_attr (expr);
8282
8283 if (attr.allocatable)
8284 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8285 "locality-spec at %L",
8286 sym->name, name, &expr->where);
8287
8288 else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8289 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8290 " for %qs in %s locality-spec at %L",
8291 sym->name, name, &expr->where);
8292
8293 else if (attr.codimension)
8294 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8295 "at %L",
8296 sym->name, name, &expr->where);
8297
8298 else if (expr->ts.type == BT_DERIVED
8299 && gfc_is_finalizable (expr->ts.u.derived, NULL))
8300 gfc_error ("Finalizable type not permitted for %qs in %s "
8301 "locality-spec at %L",
8302 sym->name, name, &expr->where);
8303
8304 else if (gfc_has_ultimate_allocatable (expr))
8305 gfc_error ("Type with ultimate allocatable component not "
8306 "permitted for %qs in %s locality-spec at %L",
8307 sym->name, name, &expr->where);
8308 }
8309
8310 else if (locality == LOCALITY_REDUCE)
8311 {
8312 if (sym->attr.asynchronous)
8313 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8314 "REDUCE locality-spec at %L",
8315 sym->name, &expr->where);
8316 if (sym->attr.volatile_)
8317 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8318 "locality-spec at %L", sym->name, &expr->where);
8319 }
8320
8321 data.sym_hash->add (sym);
8322 }
8323
8324 if (locality == LOCALITY_LOCAL)
8325 {
8326 gcc_assert (locality == 0);
8327
8328 for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8329 iter; iter = iter->next)
8330 {
8331 gfc_expr_walker (&iter->start,
8332 do_concur_locality_specs_f2023,
8333 &data);
8334
8335 gfc_expr_walker (&iter->end,
8336 do_concur_locality_specs_f2023,
8337 &data);
8338
8339 gfc_expr_walker (&iter->stride,
8340 do_concur_locality_specs_f2023,
8341 &data);
8342 }
8343
8344 if (code->expr1)
8345 gfc_expr_walker (&code->expr1,
8346 do_concur_locality_specs_f2023,
8347 &data);
8348 }
8349 }
8350
8351 gfc_expr *reduce_op = NULL;
8352
8353 for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8354 list; list = list->next)
8355 {
8356 gfc_expr *expr = list->expr;
8357
8358 if (expr->expr_type != EXPR_VARIABLE)
8359 {
8360 reduce_op = expr;
8361 continue;
8362 }
8363
8364 if (reduce_op->expr_type == EXPR_OP)
8365 {
8366 switch (reduce_op->value.op.op)
8367 {
8368 case INTRINSIC_PLUS:
8369 case INTRINSIC_TIMES:
8370 if (!gfc_numeric_ts (&expr->ts))
8371 gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8372 "got %s", expr->symtree->n.sym->name,
8373 &expr->where, gfc_basic_typename (expr->ts.type));
8374 break;
8375 case INTRINSIC_AND:
8376 case INTRINSIC_OR:
8377 case INTRINSIC_EQV:
8378 case INTRINSIC_NEQV:
8379 if (expr->ts.type != BT_LOGICAL)
8380 gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8381 "got %qs", expr->symtree->n.sym->name,
8382 &expr->where, gfc_basic_typename (expr->ts.type));
8383 break;
8384 default:
8385 gcc_unreachable ();
8386 }
8387 }
8388
8389 else if (reduce_op->expr_type == EXPR_FUNCTION)
8390 {
8391 switch (reduce_op->value.function.isym->id)
8392 {
8393 case GFC_ISYM_MIN:
8394 case GFC_ISYM_MAX:
8395 if (expr->ts.type != BT_INTEGER
8396 && expr->ts.type != BT_REAL
8397 && expr->ts.type != BT_CHARACTER)
8398 gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8399 "in REDUCE with MIN/MAX at %L, got %s",
8400 expr->symtree->n.sym->name, &expr->where,
8401 gfc_basic_typename (expr->ts.type));
8402 break;
8403 case GFC_ISYM_IAND:
8404 case GFC_ISYM_IOR:
8405 case GFC_ISYM_IEOR:
8406 if (expr->ts.type != BT_INTEGER)
8407 gfc_error ("Expected integer type for %qs in REDUCE with "
8408 "IAND/IOR/IEOR at %L, got %s",
8409 expr->symtree->n.sym->name, &expr->where,
8410 gfc_basic_typename (expr->ts.type));
8411 break;
8412 default:
8413 gcc_unreachable ();
8414 }
8415 }
8416
8417 else
8418 gcc_unreachable ();
8419 }
8420
8421 for (int locality = 0; locality < LOCALITY_NUM; locality++)
8422 {
8423 for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8424 list = list->next)
8425 {
8426 if (list->expr->expr_type == EXPR_VARIABLE)
8427 list->expr->symtree->n.sym->mark = 0;
8428 }
8429 }
8430
8431 gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8432 check_default_none_expr, &data);
8433
8434 for (int locality = 0; locality < LOCALITY_NUM; locality++)
8435 {
8436 gfc_expr_list **plist = &code->ext.concur.locality[locality];
8437 while (*plist)
8438 {
8439 gfc_expr *expr = (*plist)->expr;
8440 if (expr->expr_type == EXPR_VARIABLE)
8441 {
8442 gfc_symbol *sym = expr->symtree->n.sym;
8443 if (sym->mark == 0)
8444 {
8445 gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8446 "locality-spec at %L is not used",
8447 sym->name, &expr->where);
8448 gfc_expr_list *tmp = *plist;
8449 *plist = (*plist)->next;
8450 gfc_free_expr (tmp->expr);
8451 free (tmp);
8452 continue;
8453 }
8454 }
8455 plist = &((*plist)->next);
8456 }
8457 }
8458 }
8459
8460 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8461 to be a scalar INTEGER variable. The subscripts and stride are scalar
8462 INTEGERs, and if stride is a constant it must be nonzero.
8463 Furthermore "A subscript or stride in a forall-triplet-spec shall
8464 not contain a reference to any index-name in the
8465 forall-triplet-spec-list in which it appears." (7.5.4.1) */
8466
8467 static void
8468 resolve_forall_iterators (gfc_forall_iterator *it)
8469 {
8470 gfc_forall_iterator *iter, *iter2;
8471
8472 for (iter = it; iter; iter = iter->next)
8473 {
8474 if (gfc_resolve_expr (iter->var)
8475 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8476 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8477 &iter->var->where);
8478
8479 if (gfc_resolve_expr (iter->start)
8480 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8481 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8482 &iter->start->where);
8483 if (iter->var->ts.kind != iter->start->ts.kind)
8484 gfc_convert_type (iter->start, &iter->var->ts, 1);
8485
8486 if (gfc_resolve_expr (iter->end)
8487 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8488 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8489 &iter->end->where);
8490 if (iter->var->ts.kind != iter->end->ts.kind)
8491 gfc_convert_type (iter->end, &iter->var->ts, 1);
8492
8493 if (gfc_resolve_expr (iter->stride))
8494 {
8495 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8496 gfc_error ("FORALL stride expression at %L must be a scalar %s",
8497 &iter->stride->where, "INTEGER");
8498
8499 if (iter->stride->expr_type == EXPR_CONSTANT
8500 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8501 gfc_error ("FORALL stride expression at %L cannot be zero",
8502 &iter->stride->where);
8503 }
8504 if (iter->var->ts.kind != iter->stride->ts.kind)
8505 gfc_convert_type (iter->stride, &iter->var->ts, 1);
8506 }
8507
8508 for (iter = it; iter; iter = iter->next)
8509 for (iter2 = iter; iter2; iter2 = iter2->next)
8510 {
8511 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8512 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8513 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8514 gfc_error ("FORALL index %qs may not appear in triplet "
8515 "specification at %L", iter->var->symtree->name,
8516 &iter2->start->where);
8517 }
8518 }
8519
8520
8521 /* Given a pointer to a symbol that is a derived type, see if it's
8522 inaccessible, i.e. if it's defined in another module and the components are
8523 PRIVATE. The search is recursive if necessary. Returns zero if no
8524 inaccessible components are found, nonzero otherwise. */
8525
8526 static bool
8527 derived_inaccessible (gfc_symbol *sym)
8528 {
8529 gfc_component *c;
8530
8531 if (sym->attr.use_assoc && sym->attr.private_comp)
8532 return 1;
8533
8534 for (c = sym->components; c; c = c->next)
8535 {
8536 /* Prevent an infinite loop through this function. */
8537 if (c->ts.type == BT_DERIVED
8538 && (c->attr.pointer || c->attr.allocatable)
8539 && sym == c->ts.u.derived)
8540 continue;
8541
8542 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8543 return 1;
8544 }
8545
8546 return 0;
8547 }
8548
8549
8550 /* Resolve the argument of a deallocate expression. The expression must be
8551 a pointer or a full array. */
8552
8553 static bool
8554 resolve_deallocate_expr (gfc_expr *e)
8555 {
8556 symbol_attribute attr;
8557 int allocatable, pointer;
8558 gfc_ref *ref;
8559 gfc_symbol *sym;
8560 gfc_component *c;
8561 bool unlimited;
8562
8563 if (!gfc_resolve_expr (e))
8564 return false;
8565
8566 if (e->expr_type != EXPR_VARIABLE)
8567 goto bad;
8568
8569 sym = e->symtree->n.sym;
8570 unlimited = UNLIMITED_POLY(sym);
8571
8572 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8573 {
8574 allocatable = CLASS_DATA (sym)->attr.allocatable;
8575 pointer = CLASS_DATA (sym)->attr.class_pointer;
8576 }
8577 else
8578 {
8579 allocatable = sym->attr.allocatable;
8580 pointer = sym->attr.pointer;
8581 }
8582 for (ref = e->ref; ref; ref = ref->next)
8583 {
8584 switch (ref->type)
8585 {
8586 case REF_ARRAY:
8587 if (ref->u.ar.type != AR_FULL
8588 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8589 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8590 allocatable = 0;
8591 break;
8592
8593 case REF_COMPONENT:
8594 c = ref->u.c.component;
8595 if (c->ts.type == BT_CLASS)
8596 {
8597 allocatable = CLASS_DATA (c)->attr.allocatable;
8598 pointer = CLASS_DATA (c)->attr.class_pointer;
8599 }
8600 else
8601 {
8602 allocatable = c->attr.allocatable;
8603 pointer = c->attr.pointer;
8604 }
8605 break;
8606
8607 case REF_SUBSTRING:
8608 case REF_INQUIRY:
8609 allocatable = 0;
8610 break;
8611 }
8612 }
8613
8614 attr = gfc_expr_attr (e);
8615
8616 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8617 {
8618 bad:
8619 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8620 &e->where);
8621 return false;
8622 }
8623
8624 /* F2008, C644. */
8625 if (gfc_is_coindexed (e))
8626 {
8627 gfc_error ("Coindexed allocatable object at %L", &e->where);
8628 return false;
8629 }
8630
8631 if (pointer
8632 && !gfc_check_vardef_context (e, true, true, false,
8633 _("DEALLOCATE object")))
8634 return false;
8635 if (!gfc_check_vardef_context (e, false, true, false,
8636 _("DEALLOCATE object")))
8637 return false;
8638
8639 return true;
8640 }
8641
8642
8643 /* Returns true if the expression e contains a reference to the symbol sym. */
8644 static bool
8645 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8646 {
8647 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8648 return true;
8649
8650 return false;
8651 }
8652
8653 bool
8654 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8655 {
8656 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8657 }
8658
8659 /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
8660 of character expressions. */
8661 static bool
8662 gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
8663 {
8664 return gfc_traverse_expr (e, sym, sym_in_expr, -1);
8665 }
8666
8667
8668 /* Given the expression node e for an allocatable/pointer of derived type to be
8669 allocated, get the expression node to be initialized afterwards (needed for
8670 derived types with default initializers, and derived types with allocatable
8671 components that need nullification.) */
8672
8673 gfc_expr *
8674 gfc_expr_to_initialize (gfc_expr *e)
8675 {
8676 gfc_expr *result;
8677 gfc_ref *ref;
8678 int i;
8679
8680 result = gfc_copy_expr (e);
8681
8682 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8683 for (ref = result->ref; ref; ref = ref->next)
8684 if (ref->type == REF_ARRAY && ref->next == NULL)
8685 {
8686 if (ref->u.ar.dimen == 0
8687 && ref->u.ar.as && ref->u.ar.as->corank)
8688 return result;
8689
8690 ref->u.ar.type = AR_FULL;
8691
8692 for (i = 0; i < ref->u.ar.dimen; i++)
8693 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8694
8695 break;
8696 }
8697
8698 gfc_free_shape (&result->shape, result->rank);
8699
8700 /* Recalculate rank, shape, etc. */
8701 gfc_resolve_expr (result);
8702 return result;
8703 }
8704
8705
8706 /* If the last ref of an expression is an array ref, return a copy of the
8707 expression with that one removed. Otherwise, a copy of the original
8708 expression. This is used for allocate-expressions and pointer assignment
8709 LHS, where there may be an array specification that needs to be stripped
8710 off when using gfc_check_vardef_context. */
8711
8712 static gfc_expr*
8713 remove_last_array_ref (gfc_expr* e)
8714 {
8715 gfc_expr* e2;
8716 gfc_ref** r;
8717
8718 e2 = gfc_copy_expr (e);
8719 for (r = &e2->ref; *r; r = &(*r)->next)
8720 if ((*r)->type == REF_ARRAY && !(*r)->next)
8721 {
8722 gfc_free_ref_list (*r);
8723 *r = NULL;
8724 break;
8725 }
8726
8727 return e2;
8728 }
8729
8730
8731 /* Used in resolve_allocate_expr to check that a allocation-object and
8732 a source-expr are conformable. This does not catch all possible
8733 cases; in particular a runtime checking is needed. */
8734
8735 static bool
8736 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
8737 {
8738 gfc_ref *tail;
8739 bool scalar;
8740
8741 for (tail = e2->ref; tail && tail->next; tail = tail->next);
8742
8743 /* If MOLD= is present and is not scalar, and the allocate-object has an
8744 explicit-shape-spec, the ranks need not agree. This may be unintended,
8745 so let's emit a warning if -Wsurprising is given. */
8746 scalar = !tail || tail->type == REF_COMPONENT;
8747 if (e1->mold && e1->rank > 0
8748 && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
8749 {
8750 if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
8751 gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
8752 "but MOLD= expression at %L has rank %d",
8753 &e2->where, scalar ? 0 : tail->u.ar.as->rank,
8754 &e1->where, e1->rank);
8755 return true;
8756 }
8757
8758 /* First compare rank. */
8759 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
8760 || (!tail && e1->rank != e2->rank))
8761 {
8762 gfc_error ("Source-expr at %L must be scalar or have the "
8763 "same rank as the allocate-object at %L",
8764 &e1->where, &e2->where);
8765 return false;
8766 }
8767
8768 if (e1->shape)
8769 {
8770 int i;
8771 mpz_t s;
8772
8773 mpz_init (s);
8774
8775 for (i = 0; i < e1->rank; i++)
8776 {
8777 if (tail->u.ar.start[i] == NULL)
8778 break;
8779
8780 if (tail->u.ar.end[i])
8781 {
8782 mpz_set (s, tail->u.ar.end[i]->value.integer);
8783 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8784 mpz_add_ui (s, s, 1);
8785 }
8786 else
8787 {
8788 mpz_set (s, tail->u.ar.start[i]->value.integer);
8789 }
8790
8791 if (mpz_cmp (e1->shape[i], s) != 0)
8792 {
8793 gfc_error ("Source-expr at %L and allocate-object at %L must "
8794 "have the same shape", &e1->where, &e2->where);
8795 mpz_clear (s);
8796 return false;
8797 }
8798 }
8799
8800 mpz_clear (s);
8801 }
8802
8803 return true;
8804 }
8805
8806
8807 /* Resolve the expression in an ALLOCATE statement, doing the additional
8808 checks to see whether the expression is OK or not. The expression must
8809 have a trailing array reference that gives the size of the array. */
8810
8811 static bool
8812 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
8813 {
8814 int i, pointer, allocatable, dimension, is_abstract;
8815 int codimension;
8816 bool coindexed;
8817 bool unlimited;
8818 symbol_attribute attr;
8819 gfc_ref *ref, *ref2;
8820 gfc_expr *e2;
8821 gfc_array_ref *ar;
8822 gfc_symbol *sym = NULL;
8823 gfc_alloc *a;
8824 gfc_component *c;
8825 bool t;
8826
8827 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
8828 checking of coarrays. */
8829 for (ref = e->ref; ref; ref = ref->next)
8830 if (ref->next == NULL)
8831 break;
8832
8833 if (ref && ref->type == REF_ARRAY)
8834 ref->u.ar.in_allocate = true;
8835
8836 if (!gfc_resolve_expr (e))
8837 goto failure;
8838
8839 /* Make sure the expression is allocatable or a pointer. If it is
8840 pointer, the next-to-last reference must be a pointer. */
8841
8842 ref2 = NULL;
8843 if (e->symtree)
8844 sym = e->symtree->n.sym;
8845
8846 /* Check whether ultimate component is abstract and CLASS. */
8847 is_abstract = 0;
8848
8849 /* Is the allocate-object unlimited polymorphic? */
8850 unlimited = UNLIMITED_POLY(e);
8851
8852 if (e->expr_type != EXPR_VARIABLE)
8853 {
8854 allocatable = 0;
8855 attr = gfc_expr_attr (e);
8856 pointer = attr.pointer;
8857 dimension = attr.dimension;
8858 codimension = attr.codimension;
8859 }
8860 else
8861 {
8862 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
8863 {
8864 allocatable = CLASS_DATA (sym)->attr.allocatable;
8865 pointer = CLASS_DATA (sym)->attr.class_pointer;
8866 dimension = CLASS_DATA (sym)->attr.dimension;
8867 codimension = CLASS_DATA (sym)->attr.codimension;
8868 is_abstract = CLASS_DATA (sym)->attr.abstract;
8869 }
8870 else
8871 {
8872 allocatable = sym->attr.allocatable;
8873 pointer = sym->attr.pointer;
8874 dimension = sym->attr.dimension;
8875 codimension = sym->attr.codimension;
8876 }
8877
8878 coindexed = false;
8879
8880 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
8881 {
8882 switch (ref->type)
8883 {
8884 case REF_ARRAY:
8885 if (ref->u.ar.codimen > 0)
8886 {
8887 int n;
8888 for (n = ref->u.ar.dimen;
8889 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
8890 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
8891 {
8892 coindexed = true;
8893 break;
8894 }
8895 }
8896
8897 if (ref->next != NULL)
8898 pointer = 0;
8899 break;
8900
8901 case REF_COMPONENT:
8902 /* F2008, C644. */
8903 if (coindexed)
8904 {
8905 gfc_error ("Coindexed allocatable object at %L",
8906 &e->where);
8907 goto failure;
8908 }
8909
8910 c = ref->u.c.component;
8911 if (c->ts.type == BT_CLASS)
8912 {
8913 allocatable = CLASS_DATA (c)->attr.allocatable;
8914 pointer = CLASS_DATA (c)->attr.class_pointer;
8915 dimension = CLASS_DATA (c)->attr.dimension;
8916 codimension = CLASS_DATA (c)->attr.codimension;
8917 is_abstract = CLASS_DATA (c)->attr.abstract;
8918 }
8919 else
8920 {
8921 allocatable = c->attr.allocatable;
8922 pointer = c->attr.pointer;
8923 dimension = c->attr.dimension;
8924 codimension = c->attr.codimension;
8925 is_abstract = c->attr.abstract;
8926 }
8927 break;
8928
8929 case REF_SUBSTRING:
8930 case REF_INQUIRY:
8931 allocatable = 0;
8932 pointer = 0;
8933 break;
8934 }
8935 }
8936 }
8937
8938 /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
8939 pointer or an allocatable variable. */
8940 if (allocatable == 0 && pointer == 0)
8941 {
8942 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8943 &e->where);
8944 goto failure;
8945 }
8946
8947 /* Some checks for the SOURCE tag. */
8948 if (code->expr3)
8949 {
8950 /* Check F03:C632: "The source-expr shall be a scalar or have the same
8951 rank as allocate-object". This would require the MOLD argument to
8952 NULL() as source-expr for subsequent checking. However, even the
8953 resulting disassociated pointer or unallocated array has no shape that
8954 could be used for SOURCE= or MOLD=. */
8955 if (code->expr3->expr_type == EXPR_NULL)
8956 {
8957 gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
8958 &code->expr3->where);
8959 goto failure;
8960 }
8961
8962 /* Check F03:C631. */
8963 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
8964 {
8965 gfc_error ("Type of entity at %L is type incompatible with "
8966 "source-expr at %L", &e->where, &code->expr3->where);
8967 goto failure;
8968 }
8969
8970 /* Check F03:C632 and restriction following Note 6.18. */
8971 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
8972 goto failure;
8973
8974 /* Check F03:C633. */
8975 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
8976 {
8977 gfc_error ("The allocate-object at %L and the source-expr at %L "
8978 "shall have the same kind type parameter",
8979 &e->where, &code->expr3->where);
8980 goto failure;
8981 }
8982
8983 /* Check F2008, C642. */
8984 if (code->expr3->ts.type == BT_DERIVED
8985 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
8986 || (code->expr3->ts.u.derived->from_intmod
8987 == INTMOD_ISO_FORTRAN_ENV
8988 && code->expr3->ts.u.derived->intmod_sym_id
8989 == ISOFORTRAN_LOCK_TYPE)))
8990 {
8991 gfc_error ("The source-expr at %L shall neither be of type "
8992 "LOCK_TYPE nor have a LOCK_TYPE component if "
8993 "allocate-object at %L is a coarray",
8994 &code->expr3->where, &e->where);
8995 goto failure;
8996 }
8997
8998 /* Check F2008:C639: "Corresponding kind type parameters of
8999 allocate-object and source-expr shall have the same values." */
9000 if (e->ts.type == BT_CHARACTER
9001 && !e->ts.deferred
9002 && e->ts.u.cl->length
9003 && code->expr3->ts.type == BT_CHARACTER
9004 && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9005 "SOURCE= or MOLD= specifier"))
9006 goto failure;
9007
9008 /* Check TS18508, C702/C703. */
9009 if (code->expr3->ts.type == BT_DERIVED
9010 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9011 || (code->expr3->ts.u.derived->from_intmod
9012 == INTMOD_ISO_FORTRAN_ENV
9013 && code->expr3->ts.u.derived->intmod_sym_id
9014 == ISOFORTRAN_EVENT_TYPE)))
9015 {
9016 gfc_error ("The source-expr at %L shall neither be of type "
9017 "EVENT_TYPE nor have a EVENT_TYPE component if "
9018 "allocate-object at %L is a coarray",
9019 &code->expr3->where, &e->where);
9020 goto failure;
9021 }
9022 }
9023
9024 /* Check F08:C629. */
9025 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9026 && !code->expr3)
9027 {
9028 gcc_assert (e->ts.type == BT_CLASS);
9029 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9030 "type-spec or source-expr", sym->name, &e->where);
9031 goto failure;
9032 }
9033
9034 /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9035 if and only if each allocate-object is a dummy argument for which the
9036 corresponding type parameter is assumed. */
9037 if (code->ext.alloc.ts.type == BT_CHARACTER
9038 && code->ext.alloc.ts.u.cl->length != NULL
9039 && e->ts.type == BT_CHARACTER && !e->ts.deferred
9040 && e->ts.u.cl->length == NULL
9041 && e->symtree->n.sym->attr.dummy)
9042 {
9043 gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9044 "shall be an asterisk as allocate object %qs at %L is a "
9045 "dummy argument with assumed type parameter",
9046 sym->name, &e->where);
9047 goto failure;
9048 }
9049
9050 /* Check F08:C632. */
9051 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9052 && !UNLIMITED_POLY (e))
9053 {
9054 int cmp;
9055
9056 if (!e->ts.u.cl->length)
9057 goto failure;
9058
9059 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9060 code->ext.alloc.ts.u.cl->length);
9061 if (cmp == 1 || cmp == -1 || cmp == -3)
9062 {
9063 gfc_error ("Allocating %s at %L with type-spec requires the same "
9064 "character-length parameter as in the declaration",
9065 sym->name, &e->where);
9066 goto failure;
9067 }
9068 }
9069
9070 /* In the variable definition context checks, gfc_expr_attr is used
9071 on the expression. This is fooled by the array specification
9072 present in e, thus we have to eliminate that one temporarily. */
9073 e2 = remove_last_array_ref (e);
9074 t = true;
9075 if (t && pointer)
9076 t = gfc_check_vardef_context (e2, true, true, false,
9077 _("ALLOCATE object"));
9078 if (t)
9079 t = gfc_check_vardef_context (e2, false, true, false,
9080 _("ALLOCATE object"));
9081 gfc_free_expr (e2);
9082 if (!t)
9083 goto failure;
9084
9085 code->ext.alloc.expr3_not_explicit = 0;
9086 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9087 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9088 {
9089 /* For class arrays, the initialization with SOURCE is done
9090 using _copy and trans_call. It is convenient to exploit that
9091 when the allocated type is different from the declared type but
9092 no SOURCE exists by setting expr3. */
9093 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9094 code->ext.alloc.expr3_not_explicit = 1;
9095 }
9096 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9097 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9098 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9099 {
9100 /* We have to zero initialize the integer variable. */
9101 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9102 code->ext.alloc.expr3_not_explicit = 1;
9103 }
9104
9105 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9106 {
9107 /* Make sure the vtab symbol is present when
9108 the module variables are generated. */
9109 gfc_typespec ts = e->ts;
9110 if (code->expr3)
9111 ts = code->expr3->ts;
9112 else if (code->ext.alloc.ts.type == BT_DERIVED)
9113 ts = code->ext.alloc.ts;
9114
9115 /* Finding the vtab also publishes the type's symbol. Therefore this
9116 statement is necessary. */
9117 gfc_find_derived_vtab (ts.u.derived);
9118 }
9119 else if (unlimited && !UNLIMITED_POLY (code->expr3))
9120 {
9121 /* Again, make sure the vtab symbol is present when
9122 the module variables are generated. */
9123 gfc_typespec *ts = NULL;
9124 if (code->expr3)
9125 ts = &code->expr3->ts;
9126 else
9127 ts = &code->ext.alloc.ts;
9128
9129 gcc_assert (ts);
9130
9131 /* Finding the vtab also publishes the type's symbol. Therefore this
9132 statement is necessary. */
9133 gfc_find_vtab (ts);
9134 }
9135
9136 if (dimension == 0 && codimension == 0)
9137 goto success;
9138
9139 /* Make sure the last reference node is an array specification. */
9140
9141 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9142 || (dimension && ref2->u.ar.dimen == 0))
9143 {
9144 /* F08:C633. */
9145 if (code->expr3)
9146 {
9147 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9148 "in ALLOCATE statement at %L", &e->where))
9149 goto failure;
9150 if (code->expr3->rank != 0)
9151 *array_alloc_wo_spec = true;
9152 else
9153 {
9154 gfc_error ("Array specification or array-valued SOURCE= "
9155 "expression required in ALLOCATE statement at %L",
9156 &e->where);
9157 goto failure;
9158 }
9159 }
9160 else
9161 {
9162 gfc_error ("Array specification required in ALLOCATE statement "
9163 "at %L", &e->where);
9164 goto failure;
9165 }
9166 }
9167
9168 /* Make sure that the array section reference makes sense in the
9169 context of an ALLOCATE specification. */
9170
9171 ar = &ref2->u.ar;
9172
9173 if (codimension)
9174 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9175 {
9176 switch (ar->dimen_type[i])
9177 {
9178 case DIMEN_THIS_IMAGE:
9179 gfc_error ("Coarray specification required in ALLOCATE statement "
9180 "at %L", &e->where);
9181 goto failure;
9182
9183 case DIMEN_RANGE:
9184 /* F2018:R937:
9185 * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9186 */
9187 if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9188 {
9189 gfc_error ("Bad coarray specification in ALLOCATE statement "
9190 "at %L", &e->where);
9191 goto failure;
9192 }
9193 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9194 {
9195 gfc_error ("Upper cobound is less than lower cobound at %L",
9196 &ar->start[i]->where);
9197 goto failure;
9198 }
9199 break;
9200
9201 case DIMEN_ELEMENT:
9202 if (ar->start[i]->expr_type == EXPR_CONSTANT)
9203 {
9204 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9205 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9206 {
9207 gfc_error ("Upper cobound is less than lower cobound "
9208 "of 1 at %L", &ar->start[i]->where);
9209 goto failure;
9210 }
9211 }
9212 break;
9213
9214 case DIMEN_STAR:
9215 break;
9216
9217 default:
9218 gfc_error ("Bad array specification in ALLOCATE statement at %L",
9219 &e->where);
9220 goto failure;
9221
9222 }
9223 }
9224 for (i = 0; i < ar->dimen; i++)
9225 {
9226 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9227 goto check_symbols;
9228
9229 switch (ar->dimen_type[i])
9230 {
9231 case DIMEN_ELEMENT:
9232 break;
9233
9234 case DIMEN_RANGE:
9235 if (ar->start[i] != NULL
9236 && ar->end[i] != NULL
9237 && ar->stride[i] == NULL)
9238 break;
9239
9240 /* Fall through. */
9241
9242 case DIMEN_UNKNOWN:
9243 case DIMEN_VECTOR:
9244 case DIMEN_STAR:
9245 case DIMEN_THIS_IMAGE:
9246 gfc_error ("Bad array specification in ALLOCATE statement at %L",
9247 &e->where);
9248 goto failure;
9249 }
9250
9251 check_symbols:
9252 for (a = code->ext.alloc.list; a; a = a->next)
9253 {
9254 sym = a->expr->symtree->n.sym;
9255
9256 /* TODO - check derived type components. */
9257 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9258 continue;
9259
9260 if ((ar->start[i] != NULL
9261 && gfc_find_var_in_expr (sym, ar->start[i]))
9262 || (ar->end[i] != NULL
9263 && gfc_find_var_in_expr (sym, ar->end[i])))
9264 {
9265 gfc_error ("%qs must not appear in the array specification at "
9266 "%L in the same ALLOCATE statement where it is "
9267 "itself allocated", sym->name, &ar->where);
9268 goto failure;
9269 }
9270 }
9271 }
9272
9273 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9274 {
9275 if (ar->dimen_type[i] == DIMEN_ELEMENT
9276 || ar->dimen_type[i] == DIMEN_RANGE)
9277 {
9278 if (i == (ar->dimen + ar->codimen - 1))
9279 {
9280 gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9281 "statement at %L", &e->where);
9282 goto failure;
9283 }
9284 continue;
9285 }
9286
9287 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9288 && ar->stride[i] == NULL)
9289 break;
9290
9291 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9292 &e->where);
9293 goto failure;
9294 }
9295
9296 success:
9297 return true;
9298
9299 failure:
9300 return false;
9301 }
9302
9303
9304 static void
9305 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9306 {
9307 gfc_expr *stat, *errmsg, *pe, *qe;
9308 gfc_alloc *a, *p, *q;
9309
9310 stat = code->expr1;
9311 errmsg = code->expr2;
9312
9313 /* Check the stat variable. */
9314 if (stat)
9315 {
9316 if (!gfc_check_vardef_context (stat, false, false, false,
9317 _("STAT variable")))
9318 goto done_stat;
9319
9320 if (stat->ts.type != BT_INTEGER
9321 || stat->rank > 0)
9322 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9323 "variable", &stat->where);
9324
9325 if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9326 goto done_stat;
9327
9328 /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9329 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9330 */
9331 for (p = code->ext.alloc.list; p; p = p->next)
9332 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9333 {
9334 gfc_ref *ref1, *ref2;
9335 bool found = true;
9336
9337 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9338 ref1 = ref1->next, ref2 = ref2->next)
9339 {
9340 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9341 continue;
9342 if (ref1->u.c.component->name != ref2->u.c.component->name)
9343 {
9344 found = false;
9345 break;
9346 }
9347 }
9348
9349 if (found)
9350 {
9351 gfc_error ("Stat-variable at %L shall not be %sd within "
9352 "the same %s statement", &stat->where, fcn, fcn);
9353 break;
9354 }
9355 }
9356 }
9357
9358 done_stat:
9359
9360 /* Check the errmsg variable. */
9361 if (errmsg)
9362 {
9363 if (!stat)
9364 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9365 &errmsg->where);
9366
9367 if (!gfc_check_vardef_context (errmsg, false, false, false,
9368 _("ERRMSG variable")))
9369 goto done_errmsg;
9370
9371 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9372 F18:R930 errmsg-variable is scalar-default-char-variable
9373 F18:R906 default-char-variable is variable
9374 F18:C906 default-char-variable shall be default character. */
9375 if (errmsg->ts.type != BT_CHARACTER
9376 || errmsg->rank > 0
9377 || errmsg->ts.kind != gfc_default_character_kind)
9378 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9379 "variable", &errmsg->where);
9380
9381 if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9382 goto done_errmsg;
9383
9384 /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9385 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9386 */
9387 for (p = code->ext.alloc.list; p; p = p->next)
9388 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9389 {
9390 gfc_ref *ref1, *ref2;
9391 bool found = true;
9392
9393 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9394 ref1 = ref1->next, ref2 = ref2->next)
9395 {
9396 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9397 continue;
9398 if (ref1->u.c.component->name != ref2->u.c.component->name)
9399 {
9400 found = false;
9401 break;
9402 }
9403 }
9404
9405 if (found)
9406 {
9407 gfc_error ("Errmsg-variable at %L shall not be %sd within "
9408 "the same %s statement", &errmsg->where, fcn, fcn);
9409 break;
9410 }
9411 }
9412 }
9413
9414 done_errmsg:
9415
9416 /* Check that an allocate-object appears only once in the statement. */
9417
9418 for (p = code->ext.alloc.list; p; p = p->next)
9419 {
9420 pe = p->expr;
9421 for (q = p->next; q; q = q->next)
9422 {
9423 qe = q->expr;
9424 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9425 {
9426 /* This is a potential collision. */
9427 gfc_ref *pr = pe->ref;
9428 gfc_ref *qr = qe->ref;
9429
9430 /* Follow the references until
9431 a) They start to differ, in which case there is no error;
9432 you can deallocate a%b and a%c in a single statement
9433 b) Both of them stop, which is an error
9434 c) One of them stops, which is also an error. */
9435 while (1)
9436 {
9437 if (pr == NULL && qr == NULL)
9438 {
9439 gfc_error ("Allocate-object at %L also appears at %L",
9440 &pe->where, &qe->where);
9441 break;
9442 }
9443 else if (pr != NULL && qr == NULL)
9444 {
9445 gfc_error ("Allocate-object at %L is subobject of"
9446 " object at %L", &pe->where, &qe->where);
9447 break;
9448 }
9449 else if (pr == NULL && qr != NULL)
9450 {
9451 gfc_error ("Allocate-object at %L is subobject of"
9452 " object at %L", &qe->where, &pe->where);
9453 break;
9454 }
9455 /* Here, pr != NULL && qr != NULL */
9456 gcc_assert(pr->type == qr->type);
9457 if (pr->type == REF_ARRAY)
9458 {
9459 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9460 which are legal. */
9461 gcc_assert (qr->type == REF_ARRAY);
9462
9463 if (pr->next && qr->next)
9464 {
9465 int i;
9466 gfc_array_ref *par = &(pr->u.ar);
9467 gfc_array_ref *qar = &(qr->u.ar);
9468
9469 for (i=0; i<par->dimen; i++)
9470 {
9471 if ((par->start[i] != NULL
9472 || qar->start[i] != NULL)
9473 && gfc_dep_compare_expr (par->start[i],
9474 qar->start[i]) != 0)
9475 goto break_label;
9476 }
9477 }
9478 }
9479 else
9480 {
9481 if (pr->u.c.component->name != qr->u.c.component->name)
9482 break;
9483 }
9484
9485 pr = pr->next;
9486 qr = qr->next;
9487 }
9488 break_label:
9489 ;
9490 }
9491 }
9492 }
9493
9494 if (strcmp (fcn, "ALLOCATE") == 0)
9495 {
9496 bool arr_alloc_wo_spec = false;
9497
9498 /* Resolving the expr3 in the loop over all objects to allocate would
9499 execute loop invariant code for each loop item. Therefore do it just
9500 once here. */
9501 if (code->expr3 && code->expr3->mold
9502 && code->expr3->ts.type == BT_DERIVED)
9503 {
9504 /* Default initialization via MOLD (non-polymorphic). */
9505 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9506 if (rhs != NULL)
9507 {
9508 gfc_resolve_expr (rhs);
9509 gfc_free_expr (code->expr3);
9510 code->expr3 = rhs;
9511 }
9512 }
9513 for (a = code->ext.alloc.list; a; a = a->next)
9514 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9515
9516 if (arr_alloc_wo_spec && code->expr3)
9517 {
9518 /* Mark the allocate to have to take the array specification
9519 from the expr3. */
9520 code->ext.alloc.arr_spec_from_expr3 = 1;
9521 }
9522 }
9523 else
9524 {
9525 for (a = code->ext.alloc.list; a; a = a->next)
9526 resolve_deallocate_expr (a->expr);
9527 }
9528 }
9529
9530
9531 /************ SELECT CASE resolution subroutines ************/
9532
9533 /* Callback function for our mergesort variant. Determines interval
9534 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9535 op1 > op2. Assumes we're not dealing with the default case.
9536 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9537 There are nine situations to check. */
9538
9539 static int
9540 compare_cases (const gfc_case *op1, const gfc_case *op2)
9541 {
9542 int retval;
9543
9544 if (op1->low == NULL) /* op1 = (:L) */
9545 {
9546 /* op2 = (:N), so overlap. */
9547 retval = 0;
9548 /* op2 = (M:) or (M:N), L < M */
9549 if (op2->low != NULL
9550 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9551 retval = -1;
9552 }
9553 else if (op1->high == NULL) /* op1 = (K:) */
9554 {
9555 /* op2 = (M:), so overlap. */
9556 retval = 0;
9557 /* op2 = (:N) or (M:N), K > N */
9558 if (op2->high != NULL
9559 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9560 retval = 1;
9561 }
9562 else /* op1 = (K:L) */
9563 {
9564 if (op2->low == NULL) /* op2 = (:N), K > N */
9565 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9566 ? 1 : 0;
9567 else if (op2->high == NULL) /* op2 = (M:), L < M */
9568 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9569 ? -1 : 0;
9570 else /* op2 = (M:N) */
9571 {
9572 retval = 0;
9573 /* L < M */
9574 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9575 retval = -1;
9576 /* K > N */
9577 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9578 retval = 1;
9579 }
9580 }
9581
9582 return retval;
9583 }
9584
9585
9586 /* Merge-sort a double linked case list, detecting overlap in the
9587 process. LIST is the head of the double linked case list before it
9588 is sorted. Returns the head of the sorted list if we don't see any
9589 overlap, or NULL otherwise. */
9590
9591 static gfc_case *
9592 check_case_overlap (gfc_case *list)
9593 {
9594 gfc_case *p, *q, *e, *tail;
9595 int insize, nmerges, psize, qsize, cmp, overlap_seen;
9596
9597 /* If the passed list was empty, return immediately. */
9598 if (!list)
9599 return NULL;
9600
9601 overlap_seen = 0;
9602 insize = 1;
9603
9604 /* Loop unconditionally. The only exit from this loop is a return
9605 statement, when we've finished sorting the case list. */
9606 for (;;)
9607 {
9608 p = list;
9609 list = NULL;
9610 tail = NULL;
9611
9612 /* Count the number of merges we do in this pass. */
9613 nmerges = 0;
9614
9615 /* Loop while there exists a merge to be done. */
9616 while (p)
9617 {
9618 int i;
9619
9620 /* Count this merge. */
9621 nmerges++;
9622
9623 /* Cut the list in two pieces by stepping INSIZE places
9624 forward in the list, starting from P. */
9625 psize = 0;
9626 q = p;
9627 for (i = 0; i < insize; i++)
9628 {
9629 psize++;
9630 q = q->right;
9631 if (!q)
9632 break;
9633 }
9634 qsize = insize;
9635
9636 /* Now we have two lists. Merge them! */
9637 while (psize > 0 || (qsize > 0 && q != NULL))
9638 {
9639 /* See from which the next case to merge comes from. */
9640 if (psize == 0)
9641 {
9642 /* P is empty so the next case must come from Q. */
9643 e = q;
9644 q = q->right;
9645 qsize--;
9646 }
9647 else if (qsize == 0 || q == NULL)
9648 {
9649 /* Q is empty. */
9650 e = p;
9651 p = p->right;
9652 psize--;
9653 }
9654 else
9655 {
9656 cmp = compare_cases (p, q);
9657 if (cmp < 0)
9658 {
9659 /* The whole case range for P is less than the
9660 one for Q. */
9661 e = p;
9662 p = p->right;
9663 psize--;
9664 }
9665 else if (cmp > 0)
9666 {
9667 /* The whole case range for Q is greater than
9668 the case range for P. */
9669 e = q;
9670 q = q->right;
9671 qsize--;
9672 }
9673 else
9674 {
9675 /* The cases overlap, or they are the same
9676 element in the list. Either way, we must
9677 issue an error and get the next case from P. */
9678 /* FIXME: Sort P and Q by line number. */
9679 gfc_error ("CASE label at %L overlaps with CASE "
9680 "label at %L", &p->where, &q->where);
9681 overlap_seen = 1;
9682 e = p;
9683 p = p->right;
9684 psize--;
9685 }
9686 }
9687
9688 /* Add the next element to the merged list. */
9689 if (tail)
9690 tail->right = e;
9691 else
9692 list = e;
9693 e->left = tail;
9694 tail = e;
9695 }
9696
9697 /* P has now stepped INSIZE places along, and so has Q. So
9698 they're the same. */
9699 p = q;
9700 }
9701 tail->right = NULL;
9702
9703 /* If we have done only one merge or none at all, we've
9704 finished sorting the cases. */
9705 if (nmerges <= 1)
9706 {
9707 if (!overlap_seen)
9708 return list;
9709 else
9710 return NULL;
9711 }
9712
9713 /* Otherwise repeat, merging lists twice the size. */
9714 insize *= 2;
9715 }
9716 }
9717
9718
9719 /* Check to see if an expression is suitable for use in a CASE statement.
9720 Makes sure that all case expressions are scalar constants of the same
9721 type. Return false if anything is wrong. */
9722
9723 static bool
9724 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
9725 {
9726 if (e == NULL) return true;
9727
9728 if (e->ts.type != case_expr->ts.type)
9729 {
9730 gfc_error ("Expression in CASE statement at %L must be of type %s",
9731 &e->where, gfc_basic_typename (case_expr->ts.type));
9732 return false;
9733 }
9734
9735 /* C805 (R808) For a given case-construct, each case-value shall be of
9736 the same type as case-expr. For character type, length differences
9737 are allowed, but the kind type parameters shall be the same. */
9738
9739 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
9740 {
9741 gfc_error ("Expression in CASE statement at %L must be of kind %d",
9742 &e->where, case_expr->ts.kind);
9743 return false;
9744 }
9745
9746 /* Convert the case value kind to that of case expression kind,
9747 if needed */
9748
9749 if (e->ts.kind != case_expr->ts.kind)
9750 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
9751
9752 if (e->rank != 0)
9753 {
9754 gfc_error ("Expression in CASE statement at %L must be scalar",
9755 &e->where);
9756 return false;
9757 }
9758
9759 return true;
9760 }
9761
9762
9763 /* Given a completely parsed select statement, we:
9764
9765 - Validate all expressions and code within the SELECT.
9766 - Make sure that the selection expression is not of the wrong type.
9767 - Make sure that no case ranges overlap.
9768 - Eliminate unreachable cases and unreachable code resulting from
9769 removing case labels.
9770
9771 The standard does allow unreachable cases, e.g. CASE (5:3). But
9772 they are a hassle for code generation, and to prevent that, we just
9773 cut them out here. This is not necessary for overlapping cases
9774 because they are illegal and we never even try to generate code.
9775
9776 We have the additional caveat that a SELECT construct could have
9777 been a computed GOTO in the source code. Fortunately we can fairly
9778 easily work around that here: The case_expr for a "real" SELECT CASE
9779 is in code->expr1, but for a computed GOTO it is in code->expr2. All
9780 we have to do is make sure that the case_expr is a scalar integer
9781 expression. */
9782
9783 static void
9784 resolve_select (gfc_code *code, bool select_type)
9785 {
9786 gfc_code *body;
9787 gfc_expr *case_expr;
9788 gfc_case *cp, *default_case, *tail, *head;
9789 int seen_unreachable;
9790 int seen_logical;
9791 int ncases;
9792 bt type;
9793 bool t;
9794
9795 if (code->expr1 == NULL)
9796 {
9797 /* This was actually a computed GOTO statement. */
9798 case_expr = code->expr2;
9799 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
9800 gfc_error ("Selection expression in computed GOTO statement "
9801 "at %L must be a scalar integer expression",
9802 &case_expr->where);
9803
9804 /* Further checking is not necessary because this SELECT was built
9805 by the compiler, so it should always be OK. Just move the
9806 case_expr from expr2 to expr so that we can handle computed
9807 GOTOs as normal SELECTs from here on. */
9808 code->expr1 = code->expr2;
9809 code->expr2 = NULL;
9810 return;
9811 }
9812
9813 case_expr = code->expr1;
9814 type = case_expr->ts.type;
9815
9816 /* F08:C830. */
9817 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
9818 && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
9819
9820 {
9821 gfc_error ("Argument of SELECT statement at %L cannot be %s",
9822 &case_expr->where, gfc_typename (case_expr));
9823
9824 /* Punt. Going on here just produce more garbage error messages. */
9825 return;
9826 }
9827
9828 /* F08:R842. */
9829 if (!select_type && case_expr->rank != 0)
9830 {
9831 gfc_error ("Argument of SELECT statement at %L must be a scalar "
9832 "expression", &case_expr->where);
9833
9834 /* Punt. */
9835 return;
9836 }
9837
9838 /* Raise a warning if an INTEGER case value exceeds the range of
9839 the case-expr. Later, all expressions will be promoted to the
9840 largest kind of all case-labels. */
9841
9842 if (type == BT_INTEGER)
9843 for (body = code->block; body; body = body->block)
9844 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9845 {
9846 if (cp->low
9847 && gfc_check_integer_range (cp->low->value.integer,
9848 case_expr->ts.kind) != ARITH_OK)
9849 gfc_warning (0, "Expression in CASE statement at %L is "
9850 "not in the range of %s", &cp->low->where,
9851 gfc_typename (case_expr));
9852
9853 if (cp->high
9854 && cp->low != cp->high
9855 && gfc_check_integer_range (cp->high->value.integer,
9856 case_expr->ts.kind) != ARITH_OK)
9857 gfc_warning (0, "Expression in CASE statement at %L is "
9858 "not in the range of %s", &cp->high->where,
9859 gfc_typename (case_expr));
9860 }
9861
9862 /* PR 19168 has a long discussion concerning a mismatch of the kinds
9863 of the SELECT CASE expression and its CASE values. Walk the lists
9864 of case values, and if we find a mismatch, promote case_expr to
9865 the appropriate kind. */
9866
9867 if (type == BT_LOGICAL || type == BT_INTEGER)
9868 {
9869 for (body = code->block; body; body = body->block)
9870 {
9871 /* Walk the case label list. */
9872 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9873 {
9874 /* Intercept the DEFAULT case. It does not have a kind. */
9875 if (cp->low == NULL && cp->high == NULL)
9876 continue;
9877
9878 /* Unreachable case ranges are discarded, so ignore. */
9879 if (cp->low != NULL && cp->high != NULL
9880 && cp->low != cp->high
9881 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9882 continue;
9883
9884 if (cp->low != NULL
9885 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
9886 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
9887
9888 if (cp->high != NULL
9889 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
9890 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
9891 }
9892 }
9893 }
9894
9895 /* Assume there is no DEFAULT case. */
9896 default_case = NULL;
9897 head = tail = NULL;
9898 ncases = 0;
9899 seen_logical = 0;
9900
9901 for (body = code->block; body; body = body->block)
9902 {
9903 /* Assume the CASE list is OK, and all CASE labels can be matched. */
9904 t = true;
9905 seen_unreachable = 0;
9906
9907 /* Walk the case label list, making sure that all case labels
9908 are legal. */
9909 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9910 {
9911 /* Count the number of cases in the whole construct. */
9912 ncases++;
9913
9914 /* Intercept the DEFAULT case. */
9915 if (cp->low == NULL && cp->high == NULL)
9916 {
9917 if (default_case != NULL)
9918 {
9919 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9920 "by a second DEFAULT CASE at %L",
9921 &default_case->where, &cp->where);
9922 t = false;
9923 break;
9924 }
9925 else
9926 {
9927 default_case = cp;
9928 continue;
9929 }
9930 }
9931
9932 /* Deal with single value cases and case ranges. Errors are
9933 issued from the validation function. */
9934 if (!validate_case_label_expr (cp->low, case_expr)
9935 || !validate_case_label_expr (cp->high, case_expr))
9936 {
9937 t = false;
9938 break;
9939 }
9940
9941 if (type == BT_LOGICAL
9942 && ((cp->low == NULL || cp->high == NULL)
9943 || cp->low != cp->high))
9944 {
9945 gfc_error ("Logical range in CASE statement at %L is not "
9946 "allowed",
9947 cp->low ? &cp->low->where : &cp->high->where);
9948 t = false;
9949 break;
9950 }
9951
9952 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
9953 {
9954 int value;
9955 value = cp->low->value.logical == 0 ? 2 : 1;
9956 if (value & seen_logical)
9957 {
9958 gfc_error ("Constant logical value in CASE statement "
9959 "is repeated at %L",
9960 &cp->low->where);
9961 t = false;
9962 break;
9963 }
9964 seen_logical |= value;
9965 }
9966
9967 if (cp->low != NULL && cp->high != NULL
9968 && cp->low != cp->high
9969 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9970 {
9971 if (warn_surprising)
9972 gfc_warning (OPT_Wsurprising,
9973 "Range specification at %L can never be matched",
9974 &cp->where);
9975
9976 cp->unreachable = 1;
9977 seen_unreachable = 1;
9978 }
9979 else
9980 {
9981 /* If the case range can be matched, it can also overlap with
9982 other cases. To make sure it does not, we put it in a
9983 double linked list here. We sort that with a merge sort
9984 later on to detect any overlapping cases. */
9985 if (!head)
9986 {
9987 head = tail = cp;
9988 head->right = head->left = NULL;
9989 }
9990 else
9991 {
9992 tail->right = cp;
9993 tail->right->left = tail;
9994 tail = tail->right;
9995 tail->right = NULL;
9996 }
9997 }
9998 }
9999
10000 /* It there was a failure in the previous case label, give up
10001 for this case label list. Continue with the next block. */
10002 if (!t)
10003 continue;
10004
10005 /* See if any case labels that are unreachable have been seen.
10006 If so, we eliminate them. This is a bit of a kludge because
10007 the case lists for a single case statement (label) is a
10008 single forward linked lists. */
10009 if (seen_unreachable)
10010 {
10011 /* Advance until the first case in the list is reachable. */
10012 while (body->ext.block.case_list != NULL
10013 && body->ext.block.case_list->unreachable)
10014 {
10015 gfc_case *n = body->ext.block.case_list;
10016 body->ext.block.case_list = body->ext.block.case_list->next;
10017 n->next = NULL;
10018 gfc_free_case_list (n);
10019 }
10020
10021 /* Strip all other unreachable cases. */
10022 if (body->ext.block.case_list)
10023 {
10024 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10025 {
10026 if (cp->next->unreachable)
10027 {
10028 gfc_case *n = cp->next;
10029 cp->next = cp->next->next;
10030 n->next = NULL;
10031 gfc_free_case_list (n);
10032 }
10033 }
10034 }
10035 }
10036 }
10037
10038 /* See if there were overlapping cases. If the check returns NULL,
10039 there was overlap. In that case we don't do anything. If head
10040 is non-NULL, we prepend the DEFAULT case. The sorted list can
10041 then used during code generation for SELECT CASE constructs with
10042 a case expression of a CHARACTER type. */
10043 if (head)
10044 {
10045 head = check_case_overlap (head);
10046
10047 /* Prepend the default_case if it is there. */
10048 if (head != NULL && default_case)
10049 {
10050 default_case->left = NULL;
10051 default_case->right = head;
10052 head->left = default_case;
10053 }
10054 }
10055
10056 /* Eliminate dead blocks that may be the result if we've seen
10057 unreachable case labels for a block. */
10058 for (body = code; body && body->block; body = body->block)
10059 {
10060 if (body->block->ext.block.case_list == NULL)
10061 {
10062 /* Cut the unreachable block from the code chain. */
10063 gfc_code *c = body->block;
10064 body->block = c->block;
10065
10066 /* Kill the dead block, but not the blocks below it. */
10067 c->block = NULL;
10068 gfc_free_statements (c);
10069 }
10070 }
10071
10072 /* More than two cases is legal but insane for logical selects.
10073 Issue a warning for it. */
10074 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10075 gfc_warning (OPT_Wsurprising,
10076 "Logical SELECT CASE block at %L has more that two cases",
10077 &code->loc);
10078 }
10079
10080
10081 /* Check if a derived type is extensible. */
10082
10083 bool
10084 gfc_type_is_extensible (gfc_symbol *sym)
10085 {
10086 return !(sym->attr.is_bind_c || sym->attr.sequence
10087 || (sym->attr.is_class
10088 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10089 }
10090
10091
10092 static void
10093 resolve_types (gfc_namespace *ns);
10094
10095 /* Resolve an associate-name: Resolve target and ensure the type-spec is
10096 correct as well as possibly the array-spec. */
10097
10098 static void
10099 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10100 {
10101 gfc_expr* target;
10102 bool parentheses = false;
10103
10104 gcc_assert (sym->assoc);
10105 gcc_assert (sym->attr.flavor == FL_VARIABLE);
10106
10107 /* If this is for SELECT TYPE, the target may not yet be set. In that
10108 case, return. Resolution will be called later manually again when
10109 this is done. */
10110 target = sym->assoc->target;
10111 if (!target)
10112 return;
10113 gcc_assert (!sym->assoc->dangling);
10114
10115 if (target->expr_type == EXPR_OP
10116 && target->value.op.op == INTRINSIC_PARENTHESES
10117 && target->value.op.op1->expr_type == EXPR_VARIABLE)
10118 {
10119 sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10120 gfc_free_expr (target);
10121 target = sym->assoc->target;
10122 parentheses = true;
10123 }
10124
10125 if (resolve_target && !gfc_resolve_expr (target))
10126 return;
10127
10128 if (sym->assoc->ar)
10129 {
10130 int dim;
10131 gfc_array_ref *ar = sym->assoc->ar;
10132 for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10133 {
10134 if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10135 && ar->start[dim]->ts.type == BT_INTEGER)
10136 || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10137 && ar->end[dim]->ts.type == BT_INTEGER))
10138 gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10139 "remapping of associate name %s at %L",
10140 sym->name, &sym->declared_at);
10141 }
10142 }
10143
10144 /* For variable targets, we get some attributes from the target. */
10145 if (target->expr_type == EXPR_VARIABLE)
10146 {
10147 gfc_symbol *tsym, *dsym;
10148
10149 gcc_assert (target->symtree);
10150 tsym = target->symtree->n.sym;
10151
10152 if (gfc_expr_attr (target).proc_pointer)
10153 {
10154 gfc_error ("Associating entity %qs at %L is a procedure pointer",
10155 tsym->name, &target->where);
10156 return;
10157 }
10158
10159 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10160 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10161 && dsym->attr.flavor == FL_DERIVED)
10162 {
10163 gfc_error ("Derived type %qs cannot be used as a variable at %L",
10164 tsym->name, &target->where);
10165 return;
10166 }
10167
10168 if (tsym->attr.flavor == FL_PROCEDURE)
10169 {
10170 bool is_error = true;
10171 if (tsym->attr.function && tsym->result == tsym)
10172 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10173 if (tsym == ns->proc_name)
10174 {
10175 is_error = false;
10176 break;
10177 }
10178 if (is_error)
10179 {
10180 gfc_error ("Associating entity %qs at %L is a procedure name",
10181 tsym->name, &target->where);
10182 return;
10183 }
10184 }
10185
10186 sym->attr.asynchronous = tsym->attr.asynchronous;
10187 sym->attr.volatile_ = tsym->attr.volatile_;
10188
10189 sym->attr.target = tsym->attr.target
10190 || gfc_expr_attr (target).pointer;
10191 if (is_subref_array (target))
10192 sym->attr.subref_array_pointer = 1;
10193 }
10194 else if (target->ts.type == BT_PROCEDURE)
10195 {
10196 gfc_error ("Associating selector-expression at %L yields a procedure",
10197 &target->where);
10198 return;
10199 }
10200
10201 if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10202 {
10203 /* By now, the type of the target has been fixed up. */
10204 symbol_attribute attr;
10205
10206 if (sym->ts.type == BT_DERIVED
10207 && target->ts.type == BT_CLASS
10208 && !UNLIMITED_POLY (target))
10209 {
10210 /* Inferred to be derived type but the target has type class. */
10211 sym->ts = CLASS_DATA (target)->ts;
10212 if (!sym->as)
10213 sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10214 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10215 sym->attr.dimension = target->rank ? 1 : 0;
10216 gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10217 target->corank);
10218 sym->as = NULL;
10219 }
10220 else if (target->ts.type == BT_DERIVED
10221 && target->symtree && target->symtree->n.sym
10222 && target->symtree->n.sym->ts.type == BT_CLASS
10223 && IS_INFERRED_TYPE (target)
10224 && target->ref && target->ref->next
10225 && target->ref->next->type == REF_ARRAY
10226 && !target->ref->next->next)
10227 {
10228 /* A inferred type selector whose symbol has been determined to be
10229 a class array but which only has an array reference. Change the
10230 associate name and the selector to class type. */
10231 sym->ts = target->ts;
10232 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10233 sym->attr.dimension = target->rank ? 1 : 0;
10234 gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10235 target->corank);
10236 sym->as = NULL;
10237 target->ts = sym->ts;
10238 }
10239 else if ((target->ts.type == BT_DERIVED)
10240 || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10241 && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10242 /* Confirmed to be either a derived type or misidentified to be a
10243 scalar class object, when the selector is a class array. */
10244 sym->ts = target->ts;
10245 }
10246
10247
10248 if (target->expr_type == EXPR_NULL)
10249 {
10250 gfc_error ("Selector at %L cannot be NULL()", &target->where);
10251 return;
10252 }
10253 else if (target->ts.type == BT_UNKNOWN)
10254 {
10255 gfc_error ("Selector at %L has no type", &target->where);
10256 return;
10257 }
10258
10259 /* Get type if this was not already set. Note that it can be
10260 some other type than the target in case this is a SELECT TYPE
10261 selector! So we must not update when the type is already there. */
10262 if (sym->ts.type == BT_UNKNOWN)
10263 sym->ts = target->ts;
10264
10265 gcc_assert (sym->ts.type != BT_UNKNOWN);
10266
10267 /* See if this is a valid association-to-variable. */
10268 sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10269 && !parentheses
10270 && !gfc_has_vector_subscript (target))
10271 || gfc_is_ptr_fcn (target));
10272
10273 /* Finally resolve if this is an array or not. */
10274 if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10275 && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10276 {
10277 gfc_expression_rank (target);
10278 if (target->ts.type == BT_DERIVED
10279 && !sym->as
10280 && target->symtree->n.sym->as)
10281 {
10282 sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10283 sym->attr.dimension = 1;
10284 }
10285 else if (target->ts.type == BT_CLASS
10286 && CLASS_DATA (target)->as)
10287 {
10288 target->rank = CLASS_DATA (target)->as->rank;
10289 target->corank = CLASS_DATA (target)->as->corank;
10290 if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10291 {
10292 sym->ts = target->ts;
10293 sym->attr.dimension = 0;
10294 }
10295 }
10296 }
10297
10298
10299 if (sym->attr.dimension && target->rank == 0)
10300 {
10301 /* primary.cc makes the assumption that a reference to an associate
10302 name followed by a left parenthesis is an array reference. */
10303 if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10304 {
10305 gfc_expression_rank (sym->assoc->target);
10306 sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10307 if (!sym->attr.dimension && sym->as)
10308 sym->as = NULL;
10309 }
10310
10311 if (sym->attr.dimension && target->rank == 0)
10312 {
10313 if (sym->ts.type != BT_CHARACTER)
10314 gfc_error ("Associate-name %qs at %L is used as array",
10315 sym->name, &sym->declared_at);
10316 sym->attr.dimension = 0;
10317 return;
10318 }
10319 }
10320
10321 /* We cannot deal with class selectors that need temporaries. */
10322 if (target->ts.type == BT_CLASS
10323 && gfc_ref_needs_temporary_p (target->ref))
10324 {
10325 gfc_error ("CLASS selector at %L needs a temporary which is not "
10326 "yet implemented", &target->where);
10327 return;
10328 }
10329
10330 if (target->ts.type == BT_CLASS)
10331 gfc_fix_class_refs (target);
10332
10333 if ((target->rank > 0 || target->corank > 0)
10334 && !sym->attr.select_rank_temporary)
10335 {
10336 gfc_array_spec *as;
10337 /* The rank may be incorrectly guessed at parsing, therefore make sure
10338 it is corrected now. */
10339 if (sym->ts.type != BT_CLASS
10340 && (!sym->as || sym->as->corank != target->corank))
10341 {
10342 if (!sym->as)
10343 sym->as = gfc_get_array_spec ();
10344 as = sym->as;
10345 as->rank = target->rank;
10346 as->type = AS_DEFERRED;
10347 as->corank = target->corank;
10348 sym->attr.dimension = 1;
10349 if (as->corank != 0)
10350 sym->attr.codimension = 1;
10351 }
10352 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10353 && (!CLASS_DATA (sym)->as
10354 || CLASS_DATA (sym)->as->corank != target->corank))
10355 {
10356 if (!CLASS_DATA (sym)->as)
10357 CLASS_DATA (sym)->as = gfc_get_array_spec ();
10358 as = CLASS_DATA (sym)->as;
10359 as->rank = target->rank;
10360 as->type = AS_DEFERRED;
10361 as->corank = target->corank;
10362 CLASS_DATA (sym)->attr.dimension = 1;
10363 if (as->corank != 0)
10364 CLASS_DATA (sym)->attr.codimension = 1;
10365 }
10366 }
10367 else if (!sym->attr.select_rank_temporary)
10368 {
10369 /* target's rank is 0, but the type of the sym is still array valued,
10370 which has to be corrected. */
10371 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10372 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10373 {
10374 gfc_array_spec *as;
10375 symbol_attribute attr;
10376 /* The associated variable's type is still the array type
10377 correct this now. */
10378 gfc_typespec *ts = &target->ts;
10379 gfc_ref *ref;
10380 /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10381 */
10382 bool internal_ref = true;
10383
10384 for (ref = target->ref; ref != NULL; ref = ref->next)
10385 {
10386 switch (ref->type)
10387 {
10388 case REF_COMPONENT:
10389 ts = &ref->u.c.component->ts;
10390 internal_ref
10391 = target->ref == ref && ref->next
10392 && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10393 break;
10394 case REF_ARRAY:
10395 if (ts->type == BT_CLASS)
10396 ts = &ts->u.derived->components->ts;
10397 if (internal_ref && ref->u.ar.codimen > 0)
10398 for (int i = ref->u.ar.dimen;
10399 internal_ref
10400 && i < ref->u.ar.dimen + ref->u.ar.codimen;
10401 ++i)
10402 internal_ref
10403 = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10404 break;
10405 default:
10406 break;
10407 }
10408 }
10409 /* Only rewrite the type of this symbol, when the refs are not the
10410 internal ones for class and co-array this-image. */
10411 if (!internal_ref)
10412 {
10413 /* Create a scalar instance of the current class type. Because
10414 the rank of a class array goes into its name, the type has to
10415 be rebuilt. The alternative of (re-)setting just the
10416 attributes and as in the current type, destroys the type also
10417 in other places. */
10418 as = NULL;
10419 sym->ts = *ts;
10420 sym->ts.type = BT_CLASS;
10421 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10422 gfc_change_class (&sym->ts, &attr, as, 0, 0);
10423 sym->as = NULL;
10424 }
10425 }
10426 }
10427
10428 /* Mark this as an associate variable. */
10429 sym->attr.associate_var = 1;
10430
10431 /* Fix up the type-spec for CHARACTER types. */
10432 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10433 {
10434 gfc_ref *ref;
10435 for (ref = target->ref; ref; ref = ref->next)
10436 if (ref->type == REF_SUBSTRING
10437 && (ref->u.ss.start == NULL
10438 || ref->u.ss.start->expr_type != EXPR_CONSTANT
10439 || ref->u.ss.end == NULL
10440 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10441 break;
10442
10443 if (!sym->ts.u.cl)
10444 sym->ts.u.cl = target->ts.u.cl;
10445
10446 if (sym->ts.deferred
10447 && sym->ts.u.cl == target->ts.u.cl)
10448 {
10449 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10450 sym->ts.deferred = 1;
10451 }
10452
10453 if (!sym->ts.u.cl->length
10454 && !sym->ts.deferred
10455 && target->expr_type == EXPR_CONSTANT)
10456 {
10457 sym->ts.u.cl->length =
10458 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10459 target->value.character.length);
10460 }
10461 else if (((!sym->ts.u.cl->length
10462 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10463 && target->expr_type != EXPR_VARIABLE)
10464 || ref)
10465 {
10466 if (!sym->ts.deferred)
10467 {
10468 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10469 sym->ts.deferred = 1;
10470 }
10471
10472 /* This is reset in trans-stmt.cc after the assignment
10473 of the target expression to the associate name. */
10474 if (ref && sym->as)
10475 sym->attr.pointer = 1;
10476 else
10477 sym->attr.allocatable = 1;
10478 }
10479 }
10480
10481 if (sym->ts.type == BT_CLASS
10482 && IS_INFERRED_TYPE (target)
10483 && target->ts.type == BT_DERIVED
10484 && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10485 && target->ref && target->ref->next && !target->ref->next->next
10486 && target->ref->next->type == REF_ARRAY)
10487 target->ts = target->symtree->n.sym->ts;
10488
10489 /* If the target is a good class object, so is the associate variable. */
10490 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10491 sym->attr.class_ok = 1;
10492
10493 /* If the target is a contiguous pointer, so is the associate variable. */
10494 if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
10495 sym->attr.contiguous = 1;
10496 }
10497
10498
10499 /* Ensure that SELECT TYPE expressions have the correct rank and a full
10500 array reference, where necessary. The symbols are artificial and so
10501 the dimension attribute and arrayspec can also be set. In addition,
10502 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10503 This is corrected here as well.*/
10504
10505 static void
10506 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10507 gfc_ref *ref)
10508 {
10509 gfc_ref *nref = (*expr1)->ref;
10510 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10511 gfc_symbol *sym2;
10512 gfc_expr *selector = gfc_copy_expr (expr2);
10513
10514 (*expr1)->rank = rank;
10515 (*expr1)->corank = corank;
10516 if (selector)
10517 {
10518 gfc_resolve_expr (selector);
10519 if (selector->expr_type == EXPR_OP
10520 && selector->value.op.op == INTRINSIC_PARENTHESES)
10521 sym2 = selector->value.op.op1->symtree->n.sym;
10522 else if (selector->expr_type == EXPR_VARIABLE
10523 || selector->expr_type == EXPR_FUNCTION)
10524 sym2 = selector->symtree->n.sym;
10525 else
10526 gcc_unreachable ();
10527 }
10528 else
10529 sym2 = NULL;
10530
10531 if (sym1->ts.type == BT_CLASS)
10532 {
10533 if ((*expr1)->ts.type != BT_CLASS)
10534 (*expr1)->ts = sym1->ts;
10535
10536 CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
10537 CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
10538 if (CLASS_DATA (sym1)->as == NULL && sym2)
10539 CLASS_DATA (sym1)->as
10540 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
10541 }
10542 else
10543 {
10544 sym1->attr.dimension = rank > 0 ? 1 : 0;
10545 sym1->attr.codimension = corank > 0 ? 1 : 0;
10546 if (sym1->as == NULL && sym2)
10547 sym1->as = gfc_copy_array_spec (sym2->as);
10548 }
10549
10550 for (; nref; nref = nref->next)
10551 if (nref->next == NULL)
10552 break;
10553
10554 if (ref && nref && nref->type != REF_ARRAY)
10555 nref->next = gfc_copy_ref (ref);
10556 else if (ref && !nref)
10557 (*expr1)->ref = gfc_copy_ref (ref);
10558 else if (ref && nref->u.ar.codimen != corank)
10559 {
10560 for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
10561 nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
10562 nref->u.ar.codimen = corank;
10563 }
10564 }
10565
10566
10567 static gfc_expr *
10568 build_loc_call (gfc_expr *sym_expr)
10569 {
10570 gfc_expr *loc_call;
10571 loc_call = gfc_get_expr ();
10572 loc_call->expr_type = EXPR_FUNCTION;
10573 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
10574 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
10575 loc_call->symtree->n.sym->attr.intrinsic = 1;
10576 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
10577 gfc_commit_symbol (loc_call->symtree->n.sym);
10578 loc_call->ts.type = BT_INTEGER;
10579 loc_call->ts.kind = gfc_index_integer_kind;
10580 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
10581 loc_call->value.function.actual = gfc_get_actual_arglist ();
10582 loc_call->value.function.actual->expr = sym_expr;
10583 loc_call->where = sym_expr->where;
10584 return loc_call;
10585 }
10586
10587 /* Resolve a SELECT TYPE statement. */
10588
10589 static void
10590 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
10591 {
10592 gfc_symbol *selector_type;
10593 gfc_code *body, *new_st, *if_st, *tail;
10594 gfc_code *class_is = NULL, *default_case = NULL;
10595 gfc_case *c;
10596 gfc_symtree *st;
10597 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
10598 gfc_namespace *ns;
10599 int error = 0;
10600 int rank = 0, corank = 0;
10601 gfc_ref* ref = NULL;
10602 gfc_expr *selector_expr = NULL;
10603
10604 ns = code->ext.block.ns;
10605 if (code->expr2)
10606 {
10607 /* Set this, or coarray checks in resolve will fail. */
10608 code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10609 }
10610 gfc_resolve (ns);
10611
10612 /* Check for F03:C813. */
10613 if (code->expr1->ts.type != BT_CLASS
10614 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10615 {
10616 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10617 "at %L", &code->loc);
10618 return;
10619 }
10620
10621 /* Prevent segfault, when class type is not initialized due to previous
10622 error. */
10623 if (!code->expr1->symtree->n.sym->attr.class_ok
10624 || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10625 return;
10626
10627 if (code->expr2)
10628 {
10629 gfc_ref *ref2 = NULL;
10630 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10631 if (ref->type == REF_COMPONENT
10632 && ref->u.c.component->ts.type == BT_CLASS)
10633 ref2 = ref;
10634
10635 if (ref2)
10636 {
10637 if (code->expr1->symtree->n.sym->attr.untyped)
10638 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10639 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10640 }
10641 else
10642 {
10643 if (code->expr1->symtree->n.sym->attr.untyped)
10644 code->expr1->symtree->n.sym->ts = code->expr2->ts;
10645 /* Sometimes the selector expression is given the typespec of the
10646 '_data' field, which is logical enough but inappropriate here. */
10647 if (code->expr2->ts.type == BT_DERIVED
10648 && code->expr2->symtree
10649 && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10650 code->expr2->ts = code->expr2->symtree->n.sym->ts;
10651 selector_type = CLASS_DATA (code->expr2)
10652 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10653 }
10654
10655 if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10656 {
10657 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10658 CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10659 CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10660 }
10661
10662 /* F2008: C803 The selector expression must not be coindexed. */
10663 if (gfc_is_coindexed (code->expr2))
10664 {
10665 gfc_error ("Selector at %L must not be coindexed",
10666 &code->expr2->where);
10667 return;
10668 }
10669
10670 }
10671 else
10672 {
10673 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10674
10675 if (gfc_is_coindexed (code->expr1))
10676 {
10677 gfc_error ("Selector at %L must not be coindexed",
10678 &code->expr1->where);
10679 return;
10680 }
10681 }
10682
10683 /* Loop over TYPE IS / CLASS IS cases. */
10684 for (body = code->block; body; body = body->block)
10685 {
10686 c = body->ext.block.case_list;
10687
10688 if (!error)
10689 {
10690 /* Check for repeated cases. */
10691 for (tail = code->block; tail; tail = tail->block)
10692 {
10693 gfc_case *d = tail->ext.block.case_list;
10694 if (tail == body)
10695 break;
10696
10697 if (c->ts.type == d->ts.type
10698 && ((c->ts.type == BT_DERIVED
10699 && c->ts.u.derived && d->ts.u.derived
10700 && !strcmp (c->ts.u.derived->name,
10701 d->ts.u.derived->name))
10702 || c->ts.type == BT_UNKNOWN
10703 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10704 && c->ts.kind == d->ts.kind)))
10705 {
10706 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10707 &c->where, &d->where);
10708 return;
10709 }
10710 }
10711 }
10712
10713 /* Check F03:C815. */
10714 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10715 && selector_type
10716 && !selector_type->attr.unlimited_polymorphic
10717 && !gfc_type_is_extensible (c->ts.u.derived))
10718 {
10719 gfc_error ("Derived type %qs at %L must be extensible",
10720 c->ts.u.derived->name, &c->where);
10721 error++;
10722 continue;
10723 }
10724
10725 /* Check F03:C816. */
10726 if (c->ts.type != BT_UNKNOWN
10727 && selector_type && !selector_type->attr.unlimited_polymorphic
10728 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
10729 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
10730 {
10731 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10732 gfc_error ("Derived type %qs at %L must be an extension of %qs",
10733 c->ts.u.derived->name, &c->where, selector_type->name);
10734 else
10735 gfc_error ("Unexpected intrinsic type %qs at %L",
10736 gfc_basic_typename (c->ts.type), &c->where);
10737 error++;
10738 continue;
10739 }
10740
10741 /* Check F03:C814. */
10742 if (c->ts.type == BT_CHARACTER
10743 && (c->ts.u.cl->length != NULL || c->ts.deferred))
10744 {
10745 gfc_error ("The type-spec at %L shall specify that each length "
10746 "type parameter is assumed", &c->where);
10747 error++;
10748 continue;
10749 }
10750
10751 /* Intercept the DEFAULT case. */
10752 if (c->ts.type == BT_UNKNOWN)
10753 {
10754 /* Check F03:C818. */
10755 if (default_case)
10756 {
10757 gfc_error ("The DEFAULT CASE at %L cannot be followed "
10758 "by a second DEFAULT CASE at %L",
10759 &default_case->ext.block.case_list->where, &c->where);
10760 error++;
10761 continue;
10762 }
10763
10764 default_case = body;
10765 }
10766 }
10767
10768 if (error > 0)
10769 return;
10770
10771 /* Transform SELECT TYPE statement to BLOCK and associate selector to
10772 target if present. If there are any EXIT statements referring to the
10773 SELECT TYPE construct, this is no problem because the gfc_code
10774 reference stays the same and EXIT is equally possible from the BLOCK
10775 it is changed to. */
10776 code->op = EXEC_BLOCK;
10777 if (code->expr2)
10778 {
10779 gfc_association_list* assoc;
10780
10781 assoc = gfc_get_association_list ();
10782 assoc->st = code->expr1->symtree;
10783 assoc->target = gfc_copy_expr (code->expr2);
10784 assoc->target->where = code->expr2->where;
10785 /* assoc->variable will be set by resolve_assoc_var. */
10786
10787 code->ext.block.assoc = assoc;
10788 code->expr1->symtree->n.sym->assoc = assoc;
10789
10790 resolve_assoc_var (code->expr1->symtree->n.sym, false);
10791 }
10792 else
10793 code->ext.block.assoc = NULL;
10794
10795 /* Ensure that the selector rank and arrayspec are available to
10796 correct expressions in which they might be missing. */
10797 if (code->expr2 && (code->expr2->rank || code->expr2->corank))
10798 {
10799 rank = code->expr2->rank;
10800 corank = code->expr2->corank;
10801 for (ref = code->expr2->ref; ref; ref = ref->next)
10802 if (ref->next == NULL)
10803 break;
10804 if (ref && ref->type == REF_ARRAY)
10805 ref = gfc_copy_ref (ref);
10806
10807 /* Fixup expr1 if necessary. */
10808 if (rank || corank)
10809 fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
10810 }
10811 else if (code->expr1->rank || code->expr1->corank)
10812 {
10813 rank = code->expr1->rank;
10814 corank = code->expr1->corank;
10815 for (ref = code->expr1->ref; ref; ref = ref->next)
10816 if (ref->next == NULL)
10817 break;
10818 if (ref && ref->type == REF_ARRAY)
10819 ref = gfc_copy_ref (ref);
10820 }
10821
10822 /* Add EXEC_SELECT to switch on type. */
10823 new_st = gfc_get_code (code->op);
10824 new_st->expr1 = code->expr1;
10825 new_st->expr2 = code->expr2;
10826 new_st->block = code->block;
10827 code->expr1 = code->expr2 = NULL;
10828 code->block = NULL;
10829 if (!ns->code)
10830 ns->code = new_st;
10831 else
10832 ns->code->next = new_st;
10833 code = new_st;
10834 code->op = EXEC_SELECT_TYPE;
10835
10836 /* Use the intrinsic LOC function to generate an integer expression
10837 for the vtable of the selector. Note that the rank of the selector
10838 expression has to be set to zero. */
10839 gfc_add_vptr_component (code->expr1);
10840 code->expr1->rank = 0;
10841 code->expr1->corank = 0;
10842 code->expr1 = build_loc_call (code->expr1);
10843 selector_expr = code->expr1->value.function.actual->expr;
10844
10845 /* Loop over TYPE IS / CLASS IS cases. */
10846 for (body = code->block; body; body = body->block)
10847 {
10848 gfc_symbol *vtab;
10849 gfc_expr *e;
10850 c = body->ext.block.case_list;
10851
10852 /* Generate an index integer expression for address of the
10853 TYPE/CLASS vtable and store it in c->low. The hash expression
10854 is stored in c->high and is used to resolve intrinsic cases. */
10855 if (c->ts.type != BT_UNKNOWN)
10856 {
10857 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10858 {
10859 vtab = gfc_find_derived_vtab (c->ts.u.derived);
10860 gcc_assert (vtab);
10861 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
10862 c->ts.u.derived->hash_value);
10863 }
10864 else
10865 {
10866 vtab = gfc_find_vtab (&c->ts);
10867 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
10868 e = CLASS_DATA (vtab)->initializer;
10869 c->high = gfc_copy_expr (e);
10870 if (c->high->ts.kind != gfc_integer_4_kind)
10871 {
10872 gfc_typespec ts;
10873 ts.kind = gfc_integer_4_kind;
10874 ts.type = BT_INTEGER;
10875 gfc_convert_type_warn (c->high, &ts, 2, 0);
10876 }
10877 }
10878
10879 e = gfc_lval_expr_from_sym (vtab);
10880 c->low = build_loc_call (e);
10881 }
10882 else
10883 continue;
10884
10885 /* Associate temporary to selector. This should only be done
10886 when this case is actually true, so build a new ASSOCIATE
10887 that does precisely this here (instead of using the
10888 'global' one). */
10889
10890 if (c->ts.type == BT_CLASS)
10891 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
10892 else if (c->ts.type == BT_DERIVED)
10893 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
10894 else if (c->ts.type == BT_CHARACTER)
10895 {
10896 HOST_WIDE_INT charlen = 0;
10897 if (c->ts.u.cl && c->ts.u.cl->length
10898 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10899 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
10900 snprintf (name, sizeof (name),
10901 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
10902 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
10903 }
10904 else
10905 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
10906 c->ts.kind);
10907
10908 st = gfc_find_symtree (ns->sym_root, name);
10909 gcc_assert (st->n.sym->assoc);
10910 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
10911 st->n.sym->assoc->target->where = selector_expr->where;
10912 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
10913 {
10914 gfc_add_data_component (st->n.sym->assoc->target);
10915 /* Fixup the target expression if necessary. */
10916 if (rank || corank)
10917 fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
10918 ref);
10919 }
10920
10921 new_st = gfc_get_code (EXEC_BLOCK);
10922 new_st->ext.block.ns = gfc_build_block_ns (ns);
10923 new_st->ext.block.ns->code = body->next;
10924 body->next = new_st;
10925
10926 /* Chain in the new list only if it is marked as dangling. Otherwise
10927 there is a CASE label overlap and this is already used. Just ignore,
10928 the error is diagnosed elsewhere. */
10929 if (st->n.sym->assoc->dangling)
10930 {
10931 new_st->ext.block.assoc = st->n.sym->assoc;
10932 st->n.sym->assoc->dangling = 0;
10933 }
10934
10935 resolve_assoc_var (st->n.sym, false);
10936 }
10937
10938 /* Take out CLASS IS cases for separate treatment. */
10939 body = code;
10940 while (body && body->block)
10941 {
10942 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
10943 {
10944 /* Add to class_is list. */
10945 if (class_is == NULL)
10946 {
10947 class_is = body->block;
10948 tail = class_is;
10949 }
10950 else
10951 {
10952 for (tail = class_is; tail->block; tail = tail->block) ;
10953 tail->block = body->block;
10954 tail = tail->block;
10955 }
10956 /* Remove from EXEC_SELECT list. */
10957 body->block = body->block->block;
10958 tail->block = NULL;
10959 }
10960 else
10961 body = body->block;
10962 }
10963
10964 if (class_is)
10965 {
10966 gfc_symbol *vtab;
10967
10968 if (!default_case)
10969 {
10970 /* Add a default case to hold the CLASS IS cases. */
10971 for (tail = code; tail->block; tail = tail->block) ;
10972 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
10973 tail = tail->block;
10974 tail->ext.block.case_list = gfc_get_case ();
10975 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
10976 tail->next = NULL;
10977 default_case = tail;
10978 }
10979
10980 /* More than one CLASS IS block? */
10981 if (class_is->block)
10982 {
10983 gfc_code **c1,*c2;
10984 bool swapped;
10985 /* Sort CLASS IS blocks by extension level. */
10986 do
10987 {
10988 swapped = false;
10989 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
10990 {
10991 c2 = (*c1)->block;
10992 /* F03:C817 (check for doubles). */
10993 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
10994 == c2->ext.block.case_list->ts.u.derived->hash_value)
10995 {
10996 gfc_error ("Double CLASS IS block in SELECT TYPE "
10997 "statement at %L",
10998 &c2->ext.block.case_list->where);
10999 return;
11000 }
11001 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11002 < c2->ext.block.case_list->ts.u.derived->attr.extension)
11003 {
11004 /* Swap. */
11005 (*c1)->block = c2->block;
11006 c2->block = *c1;
11007 *c1 = c2;
11008 swapped = true;
11009 }
11010 }
11011 }
11012 while (swapped);
11013 }
11014
11015 /* Generate IF chain. */
11016 if_st = gfc_get_code (EXEC_IF);
11017 new_st = if_st;
11018 for (body = class_is; body; body = body->block)
11019 {
11020 new_st->block = gfc_get_code (EXEC_IF);
11021 new_st = new_st->block;
11022 /* Set up IF condition: Call _gfortran_is_extension_of. */
11023 new_st->expr1 = gfc_get_expr ();
11024 new_st->expr1->expr_type = EXPR_FUNCTION;
11025 new_st->expr1->ts.type = BT_LOGICAL;
11026 new_st->expr1->ts.kind = 4;
11027 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11028 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11029 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11030 /* Set up arguments. */
11031 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11032 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11033 new_st->expr1->value.function.actual->expr->where = code->loc;
11034 new_st->expr1->where = code->loc;
11035 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11036 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11037 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11038 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11039 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11040 new_st->expr1->value.function.actual->next->expr->where = code->loc;
11041 /* Set up types in formal arg list. */
11042 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11043 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11044 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11045 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11046
11047 new_st->next = body->next;
11048 }
11049 if (default_case->next)
11050 {
11051 new_st->block = gfc_get_code (EXEC_IF);
11052 new_st = new_st->block;
11053 new_st->next = default_case->next;
11054 }
11055
11056 /* Replace CLASS DEFAULT code by the IF chain. */
11057 default_case->next = if_st;
11058 }
11059
11060 /* Resolve the internal code. This cannot be done earlier because
11061 it requires that the sym->assoc of selectors is set already. */
11062 gfc_current_ns = ns;
11063 gfc_resolve_blocks (code->block, gfc_current_ns);
11064 gfc_current_ns = old_ns;
11065
11066 free (ref);
11067 }
11068
11069
11070 /* Resolve a SELECT RANK statement. */
11071
11072 static void
11073 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11074 {
11075 gfc_namespace *ns;
11076 gfc_code *body, *new_st, *tail;
11077 gfc_case *c;
11078 char tname[GFC_MAX_SYMBOL_LEN + 7];
11079 char name[2 * GFC_MAX_SYMBOL_LEN];
11080 gfc_symtree *st;
11081 gfc_expr *selector_expr = NULL;
11082 int case_value;
11083 HOST_WIDE_INT charlen = 0;
11084
11085 ns = code->ext.block.ns;
11086 gfc_resolve (ns);
11087
11088 code->op = EXEC_BLOCK;
11089 if (code->expr2)
11090 {
11091 gfc_association_list* assoc;
11092
11093 assoc = gfc_get_association_list ();
11094 assoc->st = code->expr1->symtree;
11095 assoc->target = gfc_copy_expr (code->expr2);
11096 assoc->target->where = code->expr2->where;
11097 /* assoc->variable will be set by resolve_assoc_var. */
11098
11099 code->ext.block.assoc = assoc;
11100 code->expr1->symtree->n.sym->assoc = assoc;
11101
11102 resolve_assoc_var (code->expr1->symtree->n.sym, false);
11103 }
11104 else
11105 code->ext.block.assoc = NULL;
11106
11107 /* Loop over RANK cases. Note that returning on the errors causes a
11108 cascade of further errors because the case blocks do not compile
11109 correctly. */
11110 for (body = code->block; body; body = body->block)
11111 {
11112 c = body->ext.block.case_list;
11113 if (c->low)
11114 case_value = (int) mpz_get_si (c->low->value.integer);
11115 else
11116 case_value = -2;
11117
11118 /* Check for repeated cases. */
11119 for (tail = code->block; tail; tail = tail->block)
11120 {
11121 gfc_case *d = tail->ext.block.case_list;
11122 int case_value2;
11123
11124 if (tail == body)
11125 break;
11126
11127 /* Check F2018: C1153. */
11128 if (!c->low && !d->low)
11129 gfc_error ("RANK DEFAULT at %L is repeated at %L",
11130 &c->where, &d->where);
11131
11132 if (!c->low || !d->low)
11133 continue;
11134
11135 /* Check F2018: C1153. */
11136 case_value2 = (int) mpz_get_si (d->low->value.integer);
11137 if ((case_value == case_value2) && case_value == -1)
11138 gfc_error ("RANK (*) at %L is repeated at %L",
11139 &c->where, &d->where);
11140 else if (case_value == case_value2)
11141 gfc_error ("RANK (%i) at %L is repeated at %L",
11142 case_value, &c->where, &d->where);
11143 }
11144
11145 if (!c->low)
11146 continue;
11147
11148 /* Check F2018: C1155. */
11149 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11150 || gfc_expr_attr (code->expr1).pointer))
11151 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11152 "allocatable selector at %L", &c->where, &code->expr1->where);
11153 }
11154
11155 /* Add EXEC_SELECT to switch on rank. */
11156 new_st = gfc_get_code (code->op);
11157 new_st->expr1 = code->expr1;
11158 new_st->expr2 = code->expr2;
11159 new_st->block = code->block;
11160 code->expr1 = code->expr2 = NULL;
11161 code->block = NULL;
11162 if (!ns->code)
11163 ns->code = new_st;
11164 else
11165 ns->code->next = new_st;
11166 code = new_st;
11167 code->op = EXEC_SELECT_RANK;
11168
11169 selector_expr = code->expr1;
11170
11171 /* Loop over SELECT RANK cases. */
11172 for (body = code->block; body; body = body->block)
11173 {
11174 c = body->ext.block.case_list;
11175 int case_value;
11176
11177 /* Pass on the default case. */
11178 if (c->low == NULL)
11179 continue;
11180
11181 /* Associate temporary to selector. This should only be done
11182 when this case is actually true, so build a new ASSOCIATE
11183 that does precisely this here (instead of using the
11184 'global' one). */
11185 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11186 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11187 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11188
11189 if (c->ts.type == BT_CLASS)
11190 sprintf (tname, "class_%s", c->ts.u.derived->name);
11191 else if (c->ts.type == BT_DERIVED)
11192 sprintf (tname, "type_%s", c->ts.u.derived->name);
11193 else if (c->ts.type != BT_CHARACTER)
11194 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11195 else
11196 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11197 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11198
11199 case_value = (int) mpz_get_si (c->low->value.integer);
11200 if (case_value >= 0)
11201 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11202 else
11203 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11204
11205 st = gfc_find_symtree (ns->sym_root, name);
11206 gcc_assert (st->n.sym->assoc);
11207
11208 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11209 st->n.sym->assoc->target->where = selector_expr->where;
11210
11211 new_st = gfc_get_code (EXEC_BLOCK);
11212 new_st->ext.block.ns = gfc_build_block_ns (ns);
11213 new_st->ext.block.ns->code = body->next;
11214 body->next = new_st;
11215
11216 /* Chain in the new list only if it is marked as dangling. Otherwise
11217 there is a CASE label overlap and this is already used. Just ignore,
11218 the error is diagnosed elsewhere. */
11219 if (st->n.sym->assoc->dangling)
11220 {
11221 new_st->ext.block.assoc = st->n.sym->assoc;
11222 st->n.sym->assoc->dangling = 0;
11223 }
11224
11225 resolve_assoc_var (st->n.sym, false);
11226 }
11227
11228 gfc_current_ns = ns;
11229 gfc_resolve_blocks (code->block, gfc_current_ns);
11230 gfc_current_ns = old_ns;
11231 }
11232
11233
11234 /* Resolve a transfer statement. This is making sure that:
11235 -- a derived type being transferred has only non-pointer components
11236 -- a derived type being transferred doesn't have private components, unless
11237 it's being transferred from the module where the type was defined
11238 -- we're not trying to transfer a whole assumed size array. */
11239
11240 static void
11241 resolve_transfer (gfc_code *code)
11242 {
11243 gfc_symbol *sym, *derived;
11244 gfc_ref *ref;
11245 gfc_expr *exp;
11246 bool write = false;
11247 bool formatted = false;
11248 gfc_dt *dt = code->ext.dt;
11249 gfc_symbol *dtio_sub = NULL;
11250
11251 exp = code->expr1;
11252
11253 while (exp != NULL && exp->expr_type == EXPR_OP
11254 && exp->value.op.op == INTRINSIC_PARENTHESES)
11255 exp = exp->value.op.op1;
11256
11257 if (exp && exp->expr_type == EXPR_NULL
11258 && code->ext.dt)
11259 {
11260 gfc_error ("Invalid context for NULL () intrinsic at %L",
11261 &exp->where);
11262 return;
11263 }
11264
11265 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11266 && exp->expr_type != EXPR_FUNCTION
11267 && exp->expr_type != EXPR_ARRAY
11268 && exp->expr_type != EXPR_STRUCTURE))
11269 return;
11270
11271 /* If we are reading, the variable will be changed. Note that
11272 code->ext.dt may be NULL if the TRANSFER is related to
11273 an INQUIRE statement -- but in this case, we are not reading, either. */
11274 if (dt && dt->dt_io_kind->value.iokind == M_READ
11275 && !gfc_check_vardef_context (exp, false, false, false,
11276 _("item in READ")))
11277 return;
11278
11279 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11280 || exp->expr_type == EXPR_FUNCTION
11281 || exp->expr_type == EXPR_ARRAY
11282 ? &exp->ts : &exp->symtree->n.sym->ts;
11283
11284 /* Go to actual component transferred. */
11285 for (ref = exp->ref; ref; ref = ref->next)
11286 if (ref->type == REF_COMPONENT)
11287 ts = &ref->u.c.component->ts;
11288
11289 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11290 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11291 {
11292 derived = ts->u.derived;
11293
11294 /* Determine when to use the formatted DTIO procedure. */
11295 if (dt && (dt->format_expr || dt->format_label))
11296 formatted = true;
11297
11298 write = dt->dt_io_kind->value.iokind == M_WRITE
11299 || dt->dt_io_kind->value.iokind == M_PRINT;
11300 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11301
11302 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11303 {
11304 dt->udtio = exp;
11305 sym = exp->symtree->n.sym->ns->proc_name;
11306 /* Check to see if this is a nested DTIO call, with the
11307 dummy as the io-list object. */
11308 if (sym && sym == dtio_sub && sym->formal
11309 && sym->formal->sym == exp->symtree->n.sym
11310 && exp->ref == NULL)
11311 {
11312 if (!sym->attr.recursive)
11313 {
11314 gfc_error ("DTIO %s procedure at %L must be recursive",
11315 sym->name, &sym->declared_at);
11316 return;
11317 }
11318 }
11319 }
11320 }
11321
11322 if (ts->type == BT_CLASS && dtio_sub == NULL)
11323 {
11324 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11325 "it is processed by a defined input/output procedure",
11326 &code->loc);
11327 return;
11328 }
11329
11330 if (ts->type == BT_DERIVED)
11331 {
11332 /* Check that transferred derived type doesn't contain POINTER
11333 components unless it is processed by a defined input/output
11334 procedure". */
11335 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11336 {
11337 gfc_error ("Data transfer element at %L cannot have POINTER "
11338 "components unless it is processed by a defined "
11339 "input/output procedure", &code->loc);
11340 return;
11341 }
11342
11343 /* F08:C935. */
11344 if (ts->u.derived->attr.proc_pointer_comp)
11345 {
11346 gfc_error ("Data transfer element at %L cannot have "
11347 "procedure pointer components", &code->loc);
11348 return;
11349 }
11350
11351 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11352 {
11353 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11354 "components unless it is processed by a defined "
11355 "input/output procedure", &code->loc);
11356 return;
11357 }
11358
11359 /* C_PTR and C_FUNPTR have private components which means they cannot
11360 be printed. However, if -std=gnu and not -pedantic, allow
11361 the component to be printed to help debugging. */
11362 if (ts->u.derived->ts.f90_type == BT_VOID)
11363 {
11364 gfc_error ("Data transfer element at %L "
11365 "cannot have PRIVATE components", &code->loc);
11366 return;
11367 }
11368 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11369 {
11370 gfc_error ("Data transfer element at %L cannot have "
11371 "PRIVATE components unless it is processed by "
11372 "a defined input/output procedure", &code->loc);
11373 return;
11374 }
11375 }
11376
11377 if (exp->expr_type == EXPR_STRUCTURE)
11378 return;
11379
11380 if (exp->expr_type == EXPR_ARRAY)
11381 return;
11382
11383 sym = exp->symtree->n.sym;
11384
11385 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11386 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11387 {
11388 gfc_error ("Data transfer element at %L cannot be a full reference to "
11389 "an assumed-size array", &code->loc);
11390 return;
11391 }
11392 }
11393
11394
11395 /*********** Toplevel code resolution subroutines ***********/
11396
11397 /* Find the set of labels that are reachable from this block. We also
11398 record the last statement in each block. */
11399
11400 static void
11401 find_reachable_labels (gfc_code *block)
11402 {
11403 gfc_code *c;
11404
11405 if (!block)
11406 return;
11407
11408 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11409
11410 /* Collect labels in this block. We don't keep those corresponding
11411 to END {IF|SELECT}, these are checked in resolve_branch by going
11412 up through the code_stack. */
11413 for (c = block; c; c = c->next)
11414 {
11415 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11416 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11417 }
11418
11419 /* Merge with labels from parent block. */
11420 if (cs_base->prev)
11421 {
11422 gcc_assert (cs_base->prev->reachable_labels);
11423 bitmap_ior_into (cs_base->reachable_labels,
11424 cs_base->prev->reachable_labels);
11425 }
11426 }
11427
11428 static void
11429 resolve_lock_unlock_event (gfc_code *code)
11430 {
11431 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11432 && (code->expr1->ts.type != BT_DERIVED
11433 || code->expr1->expr_type != EXPR_VARIABLE
11434 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11435 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11436 || code->expr1->rank != 0
11437 || (!gfc_is_coarray (code->expr1) &&
11438 !gfc_is_coindexed (code->expr1))))
11439 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11440 &code->expr1->where);
11441 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11442 && (code->expr1->ts.type != BT_DERIVED
11443 || code->expr1->expr_type != EXPR_VARIABLE
11444 || code->expr1->ts.u.derived->from_intmod
11445 != INTMOD_ISO_FORTRAN_ENV
11446 || code->expr1->ts.u.derived->intmod_sym_id
11447 != ISOFORTRAN_EVENT_TYPE
11448 || code->expr1->rank != 0))
11449 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11450 &code->expr1->where);
11451 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11452 && !gfc_is_coindexed (code->expr1))
11453 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11454 &code->expr1->where);
11455 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11456 gfc_error ("Event variable argument at %L must be a coarray but not "
11457 "coindexed", &code->expr1->where);
11458
11459 /* Check STAT. */
11460 if (code->expr2
11461 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11462 || code->expr2->expr_type != EXPR_VARIABLE))
11463 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11464 &code->expr2->where);
11465
11466 if (code->expr2
11467 && !gfc_check_vardef_context (code->expr2, false, false, false,
11468 _("STAT variable")))
11469 return;
11470
11471 /* Check ERRMSG. */
11472 if (code->expr3
11473 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11474 || code->expr3->expr_type != EXPR_VARIABLE))
11475 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11476 &code->expr3->where);
11477
11478 if (code->expr3
11479 && !gfc_check_vardef_context (code->expr3, false, false, false,
11480 _("ERRMSG variable")))
11481 return;
11482
11483 /* Check for LOCK the ACQUIRED_LOCK. */
11484 if (code->op != EXEC_EVENT_WAIT && code->expr4
11485 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11486 || code->expr4->expr_type != EXPR_VARIABLE))
11487 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11488 "variable", &code->expr4->where);
11489
11490 if (code->op != EXEC_EVENT_WAIT && code->expr4
11491 && !gfc_check_vardef_context (code->expr4, false, false, false,
11492 _("ACQUIRED_LOCK variable")))
11493 return;
11494
11495 /* Check for EVENT WAIT the UNTIL_COUNT. */
11496 if (code->op == EXEC_EVENT_WAIT && code->expr4)
11497 {
11498 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11499 || code->expr4->rank != 0)
11500 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11501 "expression", &code->expr4->where);
11502 }
11503 }
11504
11505
11506 static void
11507 resolve_critical (gfc_code *code)
11508 {
11509 gfc_symtree *symtree;
11510 gfc_symbol *lock_type;
11511 char name[GFC_MAX_SYMBOL_LEN];
11512 static int serial = 0;
11513
11514 if (flag_coarray != GFC_FCOARRAY_LIB)
11515 return;
11516
11517 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11518 GFC_PREFIX ("lock_type"));
11519 if (symtree)
11520 lock_type = symtree->n.sym;
11521 else
11522 {
11523 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
11524 false) != 0)
11525 gcc_unreachable ();
11526 lock_type = symtree->n.sym;
11527 lock_type->attr.flavor = FL_DERIVED;
11528 lock_type->attr.zero_comp = 1;
11529 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
11530 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
11531 }
11532
11533 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
11534 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
11535 gcc_unreachable ();
11536
11537 code->resolved_sym = symtree->n.sym;
11538 symtree->n.sym->attr.flavor = FL_VARIABLE;
11539 symtree->n.sym->attr.referenced = 1;
11540 symtree->n.sym->attr.artificial = 1;
11541 symtree->n.sym->attr.codimension = 1;
11542 symtree->n.sym->ts.type = BT_DERIVED;
11543 symtree->n.sym->ts.u.derived = lock_type;
11544 symtree->n.sym->as = gfc_get_array_spec ();
11545 symtree->n.sym->as->corank = 1;
11546 symtree->n.sym->as->type = AS_EXPLICIT;
11547 symtree->n.sym->as->cotype = AS_EXPLICIT;
11548 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
11549 NULL, 1);
11550 gfc_commit_symbols();
11551 }
11552
11553
11554 static void
11555 resolve_sync (gfc_code *code)
11556 {
11557 /* Check imageset. The * case matches expr1 == NULL. */
11558 if (code->expr1)
11559 {
11560 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
11561 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
11562 "INTEGER expression", &code->expr1->where);
11563 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
11564 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
11565 gfc_error ("Imageset argument at %L must between 1 and num_images()",
11566 &code->expr1->where);
11567 else if (code->expr1->expr_type == EXPR_ARRAY
11568 && gfc_simplify_expr (code->expr1, 0))
11569 {
11570 gfc_constructor *cons;
11571 cons = gfc_constructor_first (code->expr1->value.constructor);
11572 for (; cons; cons = gfc_constructor_next (cons))
11573 if (cons->expr->expr_type == EXPR_CONSTANT
11574 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
11575 gfc_error ("Imageset argument at %L must between 1 and "
11576 "num_images()", &cons->expr->where);
11577 }
11578 }
11579
11580 /* Check STAT. */
11581 gfc_resolve_expr (code->expr2);
11582 if (code->expr2)
11583 {
11584 if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
11585 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11586 &code->expr2->where);
11587 else
11588 gfc_check_vardef_context (code->expr2, false, false, false,
11589 _("STAT variable"));
11590 }
11591
11592 /* Check ERRMSG. */
11593 gfc_resolve_expr (code->expr3);
11594 if (code->expr3)
11595 {
11596 if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
11597 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11598 &code->expr3->where);
11599 else
11600 gfc_check_vardef_context (code->expr3, false, false, false,
11601 _("ERRMSG variable"));
11602 }
11603 }
11604
11605
11606 /* Given a branch to a label, see if the branch is conforming.
11607 The code node describes where the branch is located. */
11608
11609 static void
11610 resolve_branch (gfc_st_label *label, gfc_code *code)
11611 {
11612 code_stack *stack;
11613
11614 if (label == NULL)
11615 return;
11616
11617 /* Step one: is this a valid branching target? */
11618
11619 if (label->defined == ST_LABEL_UNKNOWN)
11620 {
11621 gfc_error ("Label %d referenced at %L is never defined", label->value,
11622 &code->loc);
11623 return;
11624 }
11625
11626 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
11627 {
11628 gfc_error ("Statement at %L is not a valid branch target statement "
11629 "for the branch statement at %L", &label->where, &code->loc);
11630 return;
11631 }
11632
11633 /* Step two: make sure this branch is not a branch to itself ;-) */
11634
11635 if (code->here == label)
11636 {
11637 gfc_warning (0,
11638 "Branch at %L may result in an infinite loop", &code->loc);
11639 return;
11640 }
11641
11642 /* Step three: See if the label is in the same block as the
11643 branching statement. The hard work has been done by setting up
11644 the bitmap reachable_labels. */
11645
11646 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
11647 {
11648 /* Check now whether there is a CRITICAL construct; if so, check
11649 whether the label is still visible outside of the CRITICAL block,
11650 which is invalid. */
11651 for (stack = cs_base; stack; stack = stack->prev)
11652 {
11653 if (stack->current->op == EXEC_CRITICAL
11654 && bitmap_bit_p (stack->reachable_labels, label->value))
11655 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
11656 "label at %L", &code->loc, &label->where);
11657 else if (stack->current->op == EXEC_DO_CONCURRENT
11658 && bitmap_bit_p (stack->reachable_labels, label->value))
11659 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
11660 "for label at %L", &code->loc, &label->where);
11661 }
11662
11663 return;
11664 }
11665
11666 /* Step four: If we haven't found the label in the bitmap, it may
11667 still be the label of the END of the enclosing block, in which
11668 case we find it by going up the code_stack. */
11669
11670 for (stack = cs_base; stack; stack = stack->prev)
11671 {
11672 if (stack->current->next && stack->current->next->here == label)
11673 break;
11674 if (stack->current->op == EXEC_CRITICAL)
11675 {
11676 /* Note: A label at END CRITICAL does not leave the CRITICAL
11677 construct as END CRITICAL is still part of it. */
11678 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
11679 " at %L", &code->loc, &label->where);
11680 return;
11681 }
11682 else if (stack->current->op == EXEC_DO_CONCURRENT)
11683 {
11684 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
11685 "label at %L", &code->loc, &label->where);
11686 return;
11687 }
11688 }
11689
11690 if (stack)
11691 {
11692 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
11693 return;
11694 }
11695
11696 /* The label is not in an enclosing block, so illegal. This was
11697 allowed in Fortran 66, so we allow it as extension. No
11698 further checks are necessary in this case. */
11699 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
11700 "as the GOTO statement at %L", &label->where,
11701 &code->loc);
11702 return;
11703 }
11704
11705
11706 /* Check whether EXPR1 has the same shape as EXPR2. */
11707
11708 static bool
11709 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
11710 {
11711 mpz_t shape[GFC_MAX_DIMENSIONS];
11712 mpz_t shape2[GFC_MAX_DIMENSIONS];
11713 bool result = false;
11714 int i;
11715
11716 /* Compare the rank. */
11717 if (expr1->rank != expr2->rank)
11718 return result;
11719
11720 /* Compare the size of each dimension. */
11721 for (i=0; i<expr1->rank; i++)
11722 {
11723 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
11724 goto ignore;
11725
11726 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
11727 goto ignore;
11728
11729 if (mpz_cmp (shape[i], shape2[i]))
11730 goto over;
11731 }
11732
11733 /* When either of the two expression is an assumed size array, we
11734 ignore the comparison of dimension sizes. */
11735 ignore:
11736 result = true;
11737
11738 over:
11739 gfc_clear_shape (shape, i);
11740 gfc_clear_shape (shape2, i);
11741 return result;
11742 }
11743
11744
11745 /* Check whether a WHERE assignment target or a WHERE mask expression
11746 has the same shape as the outmost WHERE mask expression. */
11747
11748 static void
11749 resolve_where (gfc_code *code, gfc_expr *mask)
11750 {
11751 gfc_code *cblock;
11752 gfc_code *cnext;
11753 gfc_expr *e = NULL;
11754
11755 cblock = code->block;
11756
11757 /* Store the first WHERE mask-expr of the WHERE statement or construct.
11758 In case of nested WHERE, only the outmost one is stored. */
11759 if (mask == NULL) /* outmost WHERE */
11760 e = cblock->expr1;
11761 else /* inner WHERE */
11762 e = mask;
11763
11764 while (cblock)
11765 {
11766 if (cblock->expr1)
11767 {
11768 /* Check if the mask-expr has a consistent shape with the
11769 outmost WHERE mask-expr. */
11770 if (!resolve_where_shape (cblock->expr1, e))
11771 gfc_error ("WHERE mask at %L has inconsistent shape",
11772 &cblock->expr1->where);
11773 }
11774
11775 /* the assignment statement of a WHERE statement, or the first
11776 statement in where-body-construct of a WHERE construct */
11777 cnext = cblock->next;
11778 while (cnext)
11779 {
11780 switch (cnext->op)
11781 {
11782 /* WHERE assignment statement */
11783 case EXEC_ASSIGN:
11784
11785 /* Check shape consistent for WHERE assignment target. */
11786 if (e && !resolve_where_shape (cnext->expr1, e))
11787 gfc_error ("WHERE assignment target at %L has "
11788 "inconsistent shape", &cnext->expr1->where);
11789
11790 if (cnext->op == EXEC_ASSIGN
11791 && gfc_may_be_finalized (cnext->expr1->ts))
11792 cnext->expr1->must_finalize = 1;
11793
11794 break;
11795
11796
11797 case EXEC_ASSIGN_CALL:
11798 resolve_call (cnext);
11799 if (!cnext->resolved_sym->attr.elemental)
11800 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
11801 &cnext->ext.actual->expr->where);
11802 break;
11803
11804 /* WHERE or WHERE construct is part of a where-body-construct */
11805 case EXEC_WHERE:
11806 resolve_where (cnext, e);
11807 break;
11808
11809 default:
11810 gfc_error ("Unsupported statement inside WHERE at %L",
11811 &cnext->loc);
11812 }
11813 /* the next statement within the same where-body-construct */
11814 cnext = cnext->next;
11815 }
11816 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
11817 cblock = cblock->block;
11818 }
11819 }
11820
11821
11822 /* Resolve assignment in FORALL construct.
11823 NVAR is the number of FORALL index variables, and VAR_EXPR records the
11824 FORALL index variables. */
11825
11826 static void
11827 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
11828 {
11829 int n;
11830
11831 for (n = 0; n < nvar; n++)
11832 {
11833 gfc_symbol *forall_index;
11834
11835 forall_index = var_expr[n]->symtree->n.sym;
11836
11837 /* Check whether the assignment target is one of the FORALL index
11838 variable. */
11839 if ((code->expr1->expr_type == EXPR_VARIABLE)
11840 && (code->expr1->symtree->n.sym == forall_index))
11841 gfc_error ("Assignment to a FORALL index variable at %L",
11842 &code->expr1->where);
11843 else
11844 {
11845 /* If one of the FORALL index variables doesn't appear in the
11846 assignment variable, then there could be a many-to-one
11847 assignment. Emit a warning rather than an error because the
11848 mask could be resolving this problem. */
11849 if (!find_forall_index (code->expr1, forall_index, 0))
11850 gfc_warning (0, "The FORALL with index %qs is not used on the "
11851 "left side of the assignment at %L and so might "
11852 "cause multiple assignment to this object",
11853 var_expr[n]->symtree->name, &code->expr1->where);
11854 }
11855 }
11856 }
11857
11858
11859 /* Resolve WHERE statement in FORALL construct. */
11860
11861 static void
11862 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
11863 gfc_expr **var_expr)
11864 {
11865 gfc_code *cblock;
11866 gfc_code *cnext;
11867
11868 cblock = code->block;
11869 while (cblock)
11870 {
11871 /* the assignment statement of a WHERE statement, or the first
11872 statement in where-body-construct of a WHERE construct */
11873 cnext = cblock->next;
11874 while (cnext)
11875 {
11876 switch (cnext->op)
11877 {
11878 /* WHERE assignment statement */
11879 case EXEC_ASSIGN:
11880 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
11881
11882 if (cnext->op == EXEC_ASSIGN
11883 && gfc_may_be_finalized (cnext->expr1->ts))
11884 cnext->expr1->must_finalize = 1;
11885
11886 break;
11887
11888 /* WHERE operator assignment statement */
11889 case EXEC_ASSIGN_CALL:
11890 resolve_call (cnext);
11891 if (!cnext->resolved_sym->attr.elemental)
11892 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
11893 &cnext->ext.actual->expr->where);
11894 break;
11895
11896 /* WHERE or WHERE construct is part of a where-body-construct */
11897 case EXEC_WHERE:
11898 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
11899 break;
11900
11901 default:
11902 gfc_error ("Unsupported statement inside WHERE at %L",
11903 &cnext->loc);
11904 }
11905 /* the next statement within the same where-body-construct */
11906 cnext = cnext->next;
11907 }
11908 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
11909 cblock = cblock->block;
11910 }
11911 }
11912
11913
11914 /* Traverse the FORALL body to check whether the following errors exist:
11915 1. For assignment, check if a many-to-one assignment happens.
11916 2. For WHERE statement, check the WHERE body to see if there is any
11917 many-to-one assignment. */
11918
11919 static void
11920 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
11921 {
11922 gfc_code *c;
11923
11924 c = code->block->next;
11925 while (c)
11926 {
11927 switch (c->op)
11928 {
11929 case EXEC_ASSIGN:
11930 case EXEC_POINTER_ASSIGN:
11931 gfc_resolve_assign_in_forall (c, nvar, var_expr);
11932
11933 if (c->op == EXEC_ASSIGN
11934 && gfc_may_be_finalized (c->expr1->ts))
11935 c->expr1->must_finalize = 1;
11936
11937 break;
11938
11939 case EXEC_ASSIGN_CALL:
11940 resolve_call (c);
11941 break;
11942
11943 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
11944 there is no need to handle it here. */
11945 case EXEC_FORALL:
11946 break;
11947 case EXEC_WHERE:
11948 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
11949 break;
11950 default:
11951 break;
11952 }
11953 /* The next statement in the FORALL body. */
11954 c = c->next;
11955 }
11956 }
11957
11958
11959 /* Counts the number of iterators needed inside a forall construct, including
11960 nested forall constructs. This is used to allocate the needed memory
11961 in gfc_resolve_forall. */
11962
11963 static int
11964 gfc_count_forall_iterators (gfc_code *code)
11965 {
11966 int max_iters, sub_iters, current_iters;
11967 gfc_forall_iterator *fa;
11968
11969 gcc_assert(code->op == EXEC_FORALL);
11970 max_iters = 0;
11971 current_iters = 0;
11972
11973 for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
11974 current_iters ++;
11975
11976 code = code->block->next;
11977
11978 while (code)
11979 {
11980 if (code->op == EXEC_FORALL)
11981 {
11982 sub_iters = gfc_count_forall_iterators (code);
11983 if (sub_iters > max_iters)
11984 max_iters = sub_iters;
11985 }
11986 code = code->next;
11987 }
11988
11989 return current_iters + max_iters;
11990 }
11991
11992
11993 /* Given a FORALL construct, first resolve the FORALL iterator, then call
11994 gfc_resolve_forall_body to resolve the FORALL body. */
11995
11996 static void
11997 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
11998 {
11999 static gfc_expr **var_expr;
12000 static int total_var = 0;
12001 static int nvar = 0;
12002 int i, old_nvar, tmp;
12003 gfc_forall_iterator *fa;
12004
12005 old_nvar = nvar;
12006
12007 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12008 return;
12009
12010 /* Start to resolve a FORALL construct */
12011 if (forall_save == 0)
12012 {
12013 /* Count the total number of FORALL indices in the nested FORALL
12014 construct in order to allocate the VAR_EXPR with proper size. */
12015 total_var = gfc_count_forall_iterators (code);
12016
12017 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12018 var_expr = XCNEWVEC (gfc_expr *, total_var);
12019 }
12020
12021 /* The information about FORALL iterator, including FORALL indices start, end
12022 and stride. An outer FORALL indice cannot appear in start, end or stride. */
12023 for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12024 {
12025 /* Fortran 20008: C738 (R753). */
12026 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12027 {
12028 gfc_error ("FORALL index-name at %L must be a scalar variable "
12029 "of type integer", &fa->var->where);
12030 continue;
12031 }
12032
12033 /* Check if any outer FORALL index name is the same as the current
12034 one. */
12035 for (i = 0; i < nvar; i++)
12036 {
12037 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12038 gfc_error ("An outer FORALL construct already has an index "
12039 "with this name %L", &fa->var->where);
12040 }
12041
12042 /* Record the current FORALL index. */
12043 var_expr[nvar] = gfc_copy_expr (fa->var);
12044
12045 nvar++;
12046
12047 /* No memory leak. */
12048 gcc_assert (nvar <= total_var);
12049 }
12050
12051 /* Resolve the FORALL body. */
12052 gfc_resolve_forall_body (code, nvar, var_expr);
12053
12054 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12055 gfc_resolve_blocks (code->block, ns);
12056
12057 tmp = nvar;
12058 nvar = old_nvar;
12059 /* Free only the VAR_EXPRs allocated in this frame. */
12060 for (i = nvar; i < tmp; i++)
12061 gfc_free_expr (var_expr[i]);
12062
12063 if (nvar == 0)
12064 {
12065 /* We are in the outermost FORALL construct. */
12066 gcc_assert (forall_save == 0);
12067
12068 /* VAR_EXPR is not needed any more. */
12069 free (var_expr);
12070 total_var = 0;
12071 }
12072 }
12073
12074
12075 /* Resolve a BLOCK construct statement. */
12076
12077 static void
12078 resolve_block_construct (gfc_code* code)
12079 {
12080 gfc_namespace *ns = code->ext.block.ns;
12081
12082 /* For an ASSOCIATE block, the associations (and their targets) will be
12083 resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12084 namespace. */
12085 gfc_resolve (ns);
12086 }
12087
12088
12089 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12090 DO code nodes. */
12091
12092 void
12093 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12094 {
12095 bool t;
12096
12097 for (; b; b = b->block)
12098 {
12099 t = gfc_resolve_expr (b->expr1);
12100 if (!gfc_resolve_expr (b->expr2))
12101 t = false;
12102
12103 switch (b->op)
12104 {
12105 case EXEC_IF:
12106 if (t && b->expr1 != NULL
12107 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12108 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12109 &b->expr1->where);
12110 break;
12111
12112 case EXEC_WHERE:
12113 if (t
12114 && b->expr1 != NULL
12115 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12116 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12117 &b->expr1->where);
12118 break;
12119
12120 case EXEC_GOTO:
12121 resolve_branch (b->label1, b);
12122 break;
12123
12124 case EXEC_BLOCK:
12125 resolve_block_construct (b);
12126 break;
12127
12128 case EXEC_SELECT:
12129 case EXEC_SELECT_TYPE:
12130 case EXEC_SELECT_RANK:
12131 case EXEC_FORALL:
12132 case EXEC_DO:
12133 case EXEC_DO_WHILE:
12134 case EXEC_DO_CONCURRENT:
12135 case EXEC_CRITICAL:
12136 case EXEC_READ:
12137 case EXEC_WRITE:
12138 case EXEC_IOLENGTH:
12139 case EXEC_WAIT:
12140 break;
12141
12142 case EXEC_OMP_ATOMIC:
12143 case EXEC_OACC_ATOMIC:
12144 {
12145 /* Verify this before calling gfc_resolve_code, which might
12146 change it. */
12147 gcc_assert (b->op == EXEC_OMP_ATOMIC
12148 || (b->next && b->next->op == EXEC_ASSIGN));
12149 }
12150 break;
12151
12152 case EXEC_OACC_PARALLEL_LOOP:
12153 case EXEC_OACC_PARALLEL:
12154 case EXEC_OACC_KERNELS_LOOP:
12155 case EXEC_OACC_KERNELS:
12156 case EXEC_OACC_SERIAL_LOOP:
12157 case EXEC_OACC_SERIAL:
12158 case EXEC_OACC_DATA:
12159 case EXEC_OACC_HOST_DATA:
12160 case EXEC_OACC_LOOP:
12161 case EXEC_OACC_UPDATE:
12162 case EXEC_OACC_WAIT:
12163 case EXEC_OACC_CACHE:
12164 case EXEC_OACC_ENTER_DATA:
12165 case EXEC_OACC_EXIT_DATA:
12166 case EXEC_OACC_ROUTINE:
12167 case EXEC_OMP_ALLOCATE:
12168 case EXEC_OMP_ALLOCATORS:
12169 case EXEC_OMP_ASSUME:
12170 case EXEC_OMP_CRITICAL:
12171 case EXEC_OMP_DISPATCH:
12172 case EXEC_OMP_DISTRIBUTE:
12173 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12174 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12175 case EXEC_OMP_DISTRIBUTE_SIMD:
12176 case EXEC_OMP_DO:
12177 case EXEC_OMP_DO_SIMD:
12178 case EXEC_OMP_ERROR:
12179 case EXEC_OMP_LOOP:
12180 case EXEC_OMP_MASKED:
12181 case EXEC_OMP_MASKED_TASKLOOP:
12182 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12183 case EXEC_OMP_MASTER:
12184 case EXEC_OMP_MASTER_TASKLOOP:
12185 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12186 case EXEC_OMP_ORDERED:
12187 case EXEC_OMP_PARALLEL:
12188 case EXEC_OMP_PARALLEL_DO:
12189 case EXEC_OMP_PARALLEL_DO_SIMD:
12190 case EXEC_OMP_PARALLEL_LOOP:
12191 case EXEC_OMP_PARALLEL_MASKED:
12192 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12193 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12194 case EXEC_OMP_PARALLEL_MASTER:
12195 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12196 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12197 case EXEC_OMP_PARALLEL_SECTIONS:
12198 case EXEC_OMP_PARALLEL_WORKSHARE:
12199 case EXEC_OMP_SECTIONS:
12200 case EXEC_OMP_SIMD:
12201 case EXEC_OMP_SCOPE:
12202 case EXEC_OMP_SINGLE:
12203 case EXEC_OMP_TARGET:
12204 case EXEC_OMP_TARGET_DATA:
12205 case EXEC_OMP_TARGET_ENTER_DATA:
12206 case EXEC_OMP_TARGET_EXIT_DATA:
12207 case EXEC_OMP_TARGET_PARALLEL:
12208 case EXEC_OMP_TARGET_PARALLEL_DO:
12209 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12210 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12211 case EXEC_OMP_TARGET_SIMD:
12212 case EXEC_OMP_TARGET_TEAMS:
12213 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12214 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12215 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12216 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12217 case EXEC_OMP_TARGET_TEAMS_LOOP:
12218 case EXEC_OMP_TARGET_UPDATE:
12219 case EXEC_OMP_TASK:
12220 case EXEC_OMP_TASKGROUP:
12221 case EXEC_OMP_TASKLOOP:
12222 case EXEC_OMP_TASKLOOP_SIMD:
12223 case EXEC_OMP_TASKWAIT:
12224 case EXEC_OMP_TASKYIELD:
12225 case EXEC_OMP_TEAMS:
12226 case EXEC_OMP_TEAMS_DISTRIBUTE:
12227 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12228 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12229 case EXEC_OMP_TEAMS_LOOP:
12230 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12231 case EXEC_OMP_TILE:
12232 case EXEC_OMP_UNROLL:
12233 case EXEC_OMP_WORKSHARE:
12234 break;
12235
12236 default:
12237 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
12238 }
12239
12240 gfc_resolve_code (b->next, ns);
12241 }
12242 }
12243
12244 bool
12245 caf_possible_reallocate (gfc_expr *e)
12246 {
12247 symbol_attribute caf_attr;
12248 gfc_ref *last_arr_ref = nullptr;
12249
12250 caf_attr = gfc_caf_attr (e);
12251 if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
12252 return false;
12253
12254 /* Only full array refs can indicate a needed reallocation. */
12255 for (gfc_ref *ref = e->ref; ref; ref = ref->next)
12256 if (ref->type == REF_ARRAY && ref->u.ar.dimen)
12257 last_arr_ref = ref;
12258
12259 return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
12260 }
12261
12262 /* Does everything to resolve an ordinary assignment. Returns true
12263 if this is an interface assignment. */
12264 static bool
12265 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
12266 {
12267 bool rval = false;
12268 gfc_expr *lhs;
12269 gfc_expr *rhs;
12270 int n;
12271 gfc_ref *ref;
12272 symbol_attribute attr;
12273
12274 if (gfc_extend_assign (code, ns))
12275 {
12276 gfc_expr** rhsptr;
12277
12278 if (code->op == EXEC_ASSIGN_CALL)
12279 {
12280 lhs = code->ext.actual->expr;
12281 rhsptr = &code->ext.actual->next->expr;
12282 }
12283 else
12284 {
12285 gfc_actual_arglist* args;
12286 gfc_typebound_proc* tbp;
12287
12288 gcc_assert (code->op == EXEC_COMPCALL);
12289
12290 args = code->expr1->value.compcall.actual;
12291 lhs = args->expr;
12292 rhsptr = &args->next->expr;
12293
12294 tbp = code->expr1->value.compcall.tbp;
12295 gcc_assert (!tbp->is_generic);
12296 }
12297
12298 /* Make a temporary rhs when there is a default initializer
12299 and rhs is the same symbol as the lhs. */
12300 if ((*rhsptr)->expr_type == EXPR_VARIABLE
12301 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
12302 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
12303 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
12304 *rhsptr = gfc_get_parentheses (*rhsptr);
12305
12306 return true;
12307 }
12308
12309 lhs = code->expr1;
12310 rhs = code->expr2;
12311
12312 if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
12313 || lhs->symtree->n.sym->ts.type == BT_CLASS)
12314 && !lhs->symtree->n.sym->attr.proc_pointer
12315 && gfc_expr_attr (lhs).proc_pointer)
12316 {
12317 gfc_error ("Variable in the ordinary assignment at %L is a procedure "
12318 "pointer component",
12319 &lhs->where);
12320 return false;
12321 }
12322
12323 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
12324 && rhs->ts.type == BT_CHARACTER
12325 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
12326 {
12327 /* Use of -fdec-char-conversions allows assignment of character data
12328 to non-character variables. This not permitted for nonconstant
12329 strings. */
12330 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
12331 gfc_typename (lhs), &rhs->where);
12332 return false;
12333 }
12334
12335 if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
12336 {
12337 gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
12338 gfc_typename (lhs), &rhs->where);
12339 return false;
12340 }
12341
12342 /* Handle the case of a BOZ literal on the RHS. */
12343 if (rhs->ts.type == BT_BOZ)
12344 {
12345 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
12346 "statement value nor an actual argument of "
12347 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
12348 &rhs->where))
12349 return false;
12350
12351 switch (lhs->ts.type)
12352 {
12353 case BT_INTEGER:
12354 if (!gfc_boz2int (rhs, lhs->ts.kind))
12355 return false;
12356 break;
12357 case BT_REAL:
12358 if (!gfc_boz2real (rhs, lhs->ts.kind))
12359 return false;
12360 break;
12361 default:
12362 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
12363 return false;
12364 }
12365 }
12366
12367 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
12368 {
12369 HOST_WIDE_INT llen = 0, rlen = 0;
12370 if (lhs->ts.u.cl != NULL
12371 && lhs->ts.u.cl->length != NULL
12372 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12373 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
12374
12375 if (rhs->expr_type == EXPR_CONSTANT)
12376 rlen = rhs->value.character.length;
12377
12378 else if (rhs->ts.u.cl != NULL
12379 && rhs->ts.u.cl->length != NULL
12380 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12381 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
12382
12383 if (rlen && llen && rlen > llen)
12384 gfc_warning_now (OPT_Wcharacter_truncation,
12385 "CHARACTER expression will be truncated "
12386 "in assignment (%wd/%wd) at %L",
12387 llen, rlen, &code->loc);
12388 }
12389
12390 /* Ensure that a vector index expression for the lvalue is evaluated
12391 to a temporary if the lvalue symbol is referenced in it. */
12392 if (lhs->rank)
12393 {
12394 for (ref = lhs->ref; ref; ref= ref->next)
12395 if (ref->type == REF_ARRAY)
12396 {
12397 for (n = 0; n < ref->u.ar.dimen; n++)
12398 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
12399 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
12400 ref->u.ar.start[n]))
12401 ref->u.ar.start[n]
12402 = gfc_get_parentheses (ref->u.ar.start[n]);
12403 }
12404 }
12405
12406 if (gfc_pure (NULL))
12407 {
12408 if (lhs->ts.type == BT_DERIVED
12409 && lhs->expr_type == EXPR_VARIABLE
12410 && lhs->ts.u.derived->attr.pointer_comp
12411 && rhs->expr_type == EXPR_VARIABLE
12412 && (gfc_impure_variable (rhs->symtree->n.sym)
12413 || gfc_is_coindexed (rhs)))
12414 {
12415 /* F2008, C1283. */
12416 if (gfc_is_coindexed (rhs))
12417 gfc_error ("Coindexed expression at %L is assigned to "
12418 "a derived type variable with a POINTER "
12419 "component in a PURE procedure",
12420 &rhs->where);
12421 else
12422 /* F2008, C1283 (4). */
12423 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
12424 "shall not be used as the expr at %L of an intrinsic "
12425 "assignment statement in which the variable is of a "
12426 "derived type if the derived type has a pointer "
12427 "component at any level of component selection.",
12428 &rhs->where);
12429 return rval;
12430 }
12431
12432 /* Fortran 2008, C1283. */
12433 if (gfc_is_coindexed (lhs))
12434 {
12435 gfc_error ("Assignment to coindexed variable at %L in a PURE "
12436 "procedure", &rhs->where);
12437 return rval;
12438 }
12439 }
12440
12441 if (gfc_implicit_pure (NULL))
12442 {
12443 if (lhs->expr_type == EXPR_VARIABLE
12444 && lhs->symtree->n.sym != gfc_current_ns->proc_name
12445 && lhs->symtree->n.sym->ns != gfc_current_ns)
12446 gfc_unset_implicit_pure (NULL);
12447
12448 if (lhs->ts.type == BT_DERIVED
12449 && lhs->expr_type == EXPR_VARIABLE
12450 && lhs->ts.u.derived->attr.pointer_comp
12451 && rhs->expr_type == EXPR_VARIABLE
12452 && (gfc_impure_variable (rhs->symtree->n.sym)
12453 || gfc_is_coindexed (rhs)))
12454 gfc_unset_implicit_pure (NULL);
12455
12456 /* Fortran 2008, C1283. */
12457 if (gfc_is_coindexed (lhs))
12458 gfc_unset_implicit_pure (NULL);
12459 }
12460
12461 /* F2008, 7.2.1.2. */
12462 attr = gfc_expr_attr (lhs);
12463 if (lhs->ts.type == BT_CLASS && attr.allocatable)
12464 {
12465 if (attr.codimension)
12466 {
12467 gfc_error ("Assignment to polymorphic coarray at %L is not "
12468 "permitted", &lhs->where);
12469 return false;
12470 }
12471 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
12472 "polymorphic variable at %L", &lhs->where))
12473 return false;
12474 if (!flag_realloc_lhs)
12475 {
12476 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
12477 "requires %<-frealloc-lhs%>", &lhs->where);
12478 return false;
12479 }
12480 }
12481 else if (lhs->ts.type == BT_CLASS)
12482 {
12483 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
12484 "assignment at %L - check that there is a matching specific "
12485 "subroutine for %<=%> operator", &lhs->where);
12486 return false;
12487 }
12488
12489 bool lhs_coindexed = gfc_is_coindexed (lhs);
12490
12491 /* F2008, Section 7.2.1.2. */
12492 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
12493 {
12494 gfc_error ("Coindexed variable must not have an allocatable ultimate "
12495 "component in assignment at %L", &lhs->where);
12496 return false;
12497 }
12498
12499 /* Assign the 'data' of a class object to a derived type. */
12500 if (lhs->ts.type == BT_DERIVED
12501 && rhs->ts.type == BT_CLASS
12502 && rhs->expr_type != EXPR_ARRAY)
12503 gfc_add_data_component (rhs);
12504
12505 /* Make sure there is a vtable and, in particular, a _copy for the
12506 rhs type. */
12507 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
12508 gfc_find_vtab (&rhs->ts);
12509
12510 gfc_check_assign (lhs, rhs, 1);
12511
12512 return false;
12513 }
12514
12515
12516 /* Add a component reference onto an expression. */
12517
12518 static void
12519 add_comp_ref (gfc_expr *e, gfc_component *c)
12520 {
12521 gfc_ref **ref;
12522 ref = &(e->ref);
12523 while (*ref)
12524 ref = &((*ref)->next);
12525 *ref = gfc_get_ref ();
12526 (*ref)->type = REF_COMPONENT;
12527 (*ref)->u.c.sym = e->ts.u.derived;
12528 (*ref)->u.c.component = c;
12529 e->ts = c->ts;
12530
12531 /* Add a full array ref, as necessary. */
12532 if (c->as)
12533 {
12534 gfc_add_full_array_ref (e, c->as);
12535 e->rank = c->as->rank;
12536 e->corank = c->as->corank;
12537 }
12538 }
12539
12540
12541 /* Build an assignment. Keep the argument 'op' for future use, so that
12542 pointer assignments can be made. */
12543
12544 static gfc_code *
12545 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
12546 gfc_component *comp1, gfc_component *comp2, locus loc)
12547 {
12548 gfc_code *this_code;
12549
12550 this_code = gfc_get_code (op);
12551 this_code->next = NULL;
12552 this_code->expr1 = gfc_copy_expr (expr1);
12553 this_code->expr2 = gfc_copy_expr (expr2);
12554 this_code->loc = loc;
12555 if (comp1 && comp2)
12556 {
12557 add_comp_ref (this_code->expr1, comp1);
12558 add_comp_ref (this_code->expr2, comp2);
12559 }
12560
12561 return this_code;
12562 }
12563
12564
12565 /* Makes a temporary variable expression based on the characteristics of
12566 a given variable expression. */
12567
12568 static gfc_expr*
12569 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12570 {
12571 static int serial = 0;
12572 char name[GFC_MAX_SYMBOL_LEN];
12573 gfc_symtree *tmp;
12574 gfc_array_spec *as;
12575 gfc_array_ref *aref;
12576 gfc_ref *ref;
12577
12578 sprintf (name, GFC_PREFIX("DA%d"), serial++);
12579 gfc_get_sym_tree (name, ns, &tmp, false);
12580 gfc_add_type (tmp->n.sym, &e->ts, NULL);
12581
12582 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12583 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12584 NULL,
12585 e->value.character.length);
12586
12587 as = NULL;
12588 ref = NULL;
12589 aref = NULL;
12590
12591 /* Obtain the arrayspec for the temporary. */
12592 if (e->rank && e->expr_type != EXPR_ARRAY
12593 && e->expr_type != EXPR_FUNCTION
12594 && e->expr_type != EXPR_OP)
12595 {
12596 aref = gfc_find_array_ref (e);
12597 if (e->expr_type == EXPR_VARIABLE
12598 && e->symtree->n.sym->as == aref->as)
12599 as = aref->as;
12600 else
12601 {
12602 for (ref = e->ref; ref; ref = ref->next)
12603 if (ref->type == REF_COMPONENT
12604 && ref->u.c.component->as == aref->as)
12605 {
12606 as = aref->as;
12607 break;
12608 }
12609 }
12610 }
12611
12612 /* Add the attributes and the arrayspec to the temporary. */
12613 tmp->n.sym->attr = gfc_expr_attr (e);
12614 tmp->n.sym->attr.function = 0;
12615 tmp->n.sym->attr.proc_pointer = 0;
12616 tmp->n.sym->attr.result = 0;
12617 tmp->n.sym->attr.flavor = FL_VARIABLE;
12618 tmp->n.sym->attr.dummy = 0;
12619 tmp->n.sym->attr.use_assoc = 0;
12620 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
12621
12622
12623 if (as)
12624 {
12625 tmp->n.sym->as = gfc_copy_array_spec (as);
12626 if (!ref)
12627 ref = e->ref;
12628 if (as->type == AS_DEFERRED)
12629 tmp->n.sym->attr.allocatable = 1;
12630 }
12631 else if ((e->rank || e->corank)
12632 && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
12633 || e->expr_type == EXPR_OP))
12634 {
12635 tmp->n.sym->as = gfc_get_array_spec ();
12636 tmp->n.sym->as->type = AS_DEFERRED;
12637 tmp->n.sym->as->rank = e->rank;
12638 tmp->n.sym->as->corank = e->corank;
12639 tmp->n.sym->attr.allocatable = 1;
12640 tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
12641 tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
12642 }
12643 else
12644 tmp->n.sym->attr.dimension = 0;
12645
12646 gfc_set_sym_referenced (tmp->n.sym);
12647 gfc_commit_symbol (tmp->n.sym);
12648 e = gfc_lval_expr_from_sym (tmp->n.sym);
12649
12650 /* Should the lhs be a section, use its array ref for the
12651 temporary expression. */
12652 if (aref && aref->type != AR_FULL)
12653 {
12654 gfc_free_ref_list (e->ref);
12655 e->ref = gfc_copy_ref (ref);
12656 }
12657 return e;
12658 }
12659
12660
12661 /* Add one line of code to the code chain, making sure that 'head' and
12662 'tail' are appropriately updated. */
12663
12664 static void
12665 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
12666 {
12667 gcc_assert (this_code);
12668 if (*head == NULL)
12669 *head = *tail = *this_code;
12670 else
12671 *tail = gfc_append_code (*tail, *this_code);
12672 *this_code = NULL;
12673 }
12674
12675
12676 /* Generate a final call from a variable expression */
12677
12678 static void
12679 generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
12680 {
12681 gfc_code *this_code;
12682 gfc_expr *final_expr = NULL;
12683 gfc_expr *size_expr;
12684 gfc_expr *fini_coarray;
12685
12686 gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
12687 if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
12688 return;
12689
12690 /* Now generate the finalizer call. */
12691 this_code = gfc_get_code (EXEC_CALL);
12692 this_code->symtree = final_expr->symtree;
12693 this_code->resolved_sym = final_expr->symtree->n.sym;
12694
12695 //* Expression to be finalized */
12696 this_code->ext.actual = gfc_get_actual_arglist ();
12697 this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
12698
12699 /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
12700 this_code->ext.actual->next = gfc_get_actual_arglist ();
12701 size_expr = gfc_get_expr ();
12702 size_expr->where = gfc_current_locus;
12703 size_expr->expr_type = EXPR_OP;
12704 size_expr->value.op.op = INTRINSIC_DIVIDE;
12705 size_expr->value.op.op1
12706 = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
12707 "storage_size", gfc_current_locus, 2,
12708 gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
12709 gfc_get_int_expr (gfc_index_integer_kind,
12710 NULL, 0));
12711 size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
12712 gfc_character_storage_size);
12713 size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
12714 size_expr->ts = size_expr->value.op.op1->ts;
12715 this_code->ext.actual->next->expr = size_expr;
12716
12717 /* fini_coarray */
12718 this_code->ext.actual->next->next = gfc_get_actual_arglist ();
12719 fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
12720 &tmp_expr->where);
12721 fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
12722 this_code->ext.actual->next->next->expr = fini_coarray;
12723
12724 add_code_to_chain (&this_code, head, tail);
12725
12726 }
12727
12728 /* Counts the potential number of part array references that would
12729 result from resolution of typebound defined assignments. */
12730
12731
12732 static int
12733 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
12734 {
12735 gfc_component *c;
12736 int c_depth = 0, t_depth;
12737
12738 for (c= derived->components; c; c = c->next)
12739 {
12740 if ((!gfc_bt_struct (c->ts.type)
12741 || c->attr.pointer
12742 || c->attr.allocatable
12743 || c->attr.proc_pointer_comp
12744 || c->attr.class_pointer
12745 || c->attr.proc_pointer)
12746 && !c->attr.defined_assign_comp)
12747 continue;
12748
12749 if (c->as && c_depth == 0)
12750 c_depth = 1;
12751
12752 if (c->ts.u.derived->attr.defined_assign_comp)
12753 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
12754 c->as ? 1 : 0);
12755 else
12756 t_depth = 0;
12757
12758 c_depth = t_depth > c_depth ? t_depth : c_depth;
12759 }
12760 return depth + c_depth;
12761 }
12762
12763
12764 /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
12765 "An intrinsic assignment where the variable is of derived type is performed
12766 as if each component of the variable were assigned from the corresponding
12767 component of expr using pointer assignment (10.2.2) for each pointer
12768 component, defined assignment for each nonpointer nonallocatable component
12769 of a type that has a type-bound defined assignment consistent with the
12770 component, intrinsic assignment for each other nonpointer nonallocatable
12771 component, and intrinsic assignment for each allocated coarray component.
12772 For unallocated coarray components, the corresponding component of the
12773 variable shall be unallocated. For a noncoarray allocatable component the
12774 following sequence of operations is applied.
12775 (1) If the component of the variable is allocated, it is deallocated.
12776 (2) If the component of the value of expr is allocated, the
12777 corresponding component of the variable is allocated with the same
12778 dynamic type and type parameters as the component of the value of
12779 expr. If it is an array, it is allocated with the same bounds. The
12780 value of the component of the value of expr is then assigned to the
12781 corresponding component of the variable using defined assignment if
12782 the declared type of the component has a type-bound defined
12783 assignment consistent with the component, and intrinsic assignment
12784 for the dynamic type of that component otherwise."
12785
12786 The pointer assignments are taken care of by the intrinsic assignment of the
12787 structure itself. This function recursively adds defined assignments where
12788 required. The recursion is accomplished by calling gfc_resolve_code.
12789
12790 When the lhs in a defined assignment has intent INOUT or is intent OUT
12791 and the component of 'var' is finalizable, we need a temporary for the
12792 lhs. In pseudo-code for an assignment var = expr:
12793
12794 ! Confine finalization of temporaries, as far as possible.
12795 Enclose the code for the assignment in a block
12796 ! Only call function 'expr' once.
12797 #if ('expr is not a constant or an variable)
12798 temp_expr = expr
12799 expr = temp_x
12800 ! Do the intrinsic assignment
12801 #if typeof ('var') has a typebound final subroutine
12802 finalize (var)
12803 var = expr
12804 ! Now do the component assignments
12805 #do over derived type components [%cmp]
12806 #if (cmp is a pointer of any kind)
12807 continue
12808 build the assignment
12809 resolve the code
12810 #if the code is a typebound assignment
12811 #if (arg1 is INOUT or finalizable OUT && !t1)
12812 t1 = var
12813 arg1 = t1
12814 deal with allocatation or not of var and this component
12815 #elseif the code is an assignment by itself
12816 #if this component does not need finalization
12817 delete code and continue
12818 #else
12819 remove the leading assignment
12820 #endif
12821 commit the code
12822 #if (t1 and (arg1 is INOUT or finalizable OUT))
12823 var%cmp = t1%cmp
12824 #enddo
12825 put all code chunks involving t1 to the top of the generated code
12826 insert the generated block in place of the original code
12827 */
12828
12829 static bool
12830 is_finalizable_type (gfc_typespec ts)
12831 {
12832 gfc_component *c;
12833
12834 if (ts.type != BT_DERIVED)
12835 return false;
12836
12837 /* (1) Check for FINAL subroutines. */
12838 if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
12839 return true;
12840
12841 /* (2) Check for components of finalizable type. */
12842 for (c = ts.u.derived->components; c; c = c->next)
12843 if (c->ts.type == BT_DERIVED
12844 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
12845 && c->ts.u.derived->f2k_derived
12846 && c->ts.u.derived->f2k_derived->finalizers)
12847 return true;
12848
12849 return false;
12850 }
12851
12852 /* The temporary assignments have to be put on top of the additional
12853 code to avoid the result being changed by the intrinsic assignment.
12854 */
12855 static int component_assignment_level = 0;
12856 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
12857 static bool finalizable_comp;
12858
12859 static void
12860 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
12861 {
12862 gfc_component *comp1, *comp2;
12863 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
12864 gfc_code *tmp_code = NULL;
12865 gfc_expr *t1 = NULL;
12866 gfc_expr *tmp_expr = NULL;
12867 int error_count, depth;
12868 bool finalizable_lhs;
12869
12870 gfc_get_errors (NULL, &error_count);
12871
12872 /* Filter out continuing processing after an error. */
12873 if (error_count
12874 || (*code)->expr1->ts.type != BT_DERIVED
12875 || (*code)->expr2->ts.type != BT_DERIVED)
12876 return;
12877
12878 /* TODO: Handle more than one part array reference in assignments. */
12879 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
12880 (*code)->expr1->rank ? 1 : 0);
12881 if (depth > 1)
12882 {
12883 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
12884 "done because multiple part array references would "
12885 "occur in intermediate expressions.", &(*code)->loc);
12886 return;
12887 }
12888
12889 if (!component_assignment_level)
12890 finalizable_comp = true;
12891
12892 /* Build a block so that function result temporaries are finalized
12893 locally on exiting the rather than enclosing scope. */
12894 if (!component_assignment_level)
12895 {
12896 ns = gfc_build_block_ns (ns);
12897 tmp_code = gfc_get_code (EXEC_NOP);
12898 *tmp_code = **code;
12899 tmp_code->next = NULL;
12900 (*code)->op = EXEC_BLOCK;
12901 (*code)->ext.block.ns = ns;
12902 (*code)->ext.block.assoc = NULL;
12903 (*code)->expr1 = (*code)->expr2 = NULL;
12904 ns->code = tmp_code;
12905 code = &ns->code;
12906 }
12907
12908 component_assignment_level++;
12909
12910 finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
12911
12912 /* Create a temporary so that functions get called only once. */
12913 if ((*code)->expr2->expr_type != EXPR_VARIABLE
12914 && (*code)->expr2->expr_type != EXPR_CONSTANT)
12915 {
12916 /* Assign the rhs to the temporary. */
12917 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
12918 if (tmp_expr->symtree->n.sym->attr.pointer)
12919 {
12920 /* Use allocate on assignment for the sake of simplicity. The
12921 temporary must not take on the optional attribute. Assume
12922 that the assignment is guarded by a PRESENT condition if the
12923 lhs is optional. */
12924 tmp_expr->symtree->n.sym->attr.pointer = 0;
12925 tmp_expr->symtree->n.sym->attr.optional = 0;
12926 tmp_expr->symtree->n.sym->attr.allocatable = 1;
12927 }
12928 this_code = build_assignment (EXEC_ASSIGN,
12929 tmp_expr, (*code)->expr2,
12930 NULL, NULL, (*code)->loc);
12931 this_code->expr2->must_finalize = 1;
12932 /* Add the code and substitute the rhs expression. */
12933 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
12934 gfc_free_expr ((*code)->expr2);
12935 (*code)->expr2 = tmp_expr;
12936 }
12937
12938 /* Do the intrinsic assignment. This is not needed if the lhs is one
12939 of the temporaries generated here, since the intrinsic assignment
12940 to the final result already does this. */
12941 if ((*code)->expr1->symtree->n.sym->name[2] != '.')
12942 {
12943 if (finalizable_lhs)
12944 (*code)->expr1->must_finalize = 1;
12945 this_code = build_assignment (EXEC_ASSIGN,
12946 (*code)->expr1, (*code)->expr2,
12947 NULL, NULL, (*code)->loc);
12948 add_code_to_chain (&this_code, &head, &tail);
12949 }
12950
12951 comp1 = (*code)->expr1->ts.u.derived->components;
12952 comp2 = (*code)->expr2->ts.u.derived->components;
12953
12954 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
12955 {
12956 bool inout = false;
12957 bool finalizable_out = false;
12958
12959 /* The intrinsic assignment does the right thing for pointers
12960 of all kinds and allocatable components. */
12961 if (!gfc_bt_struct (comp1->ts.type)
12962 || comp1->attr.pointer
12963 || comp1->attr.allocatable
12964 || comp1->attr.proc_pointer_comp
12965 || comp1->attr.class_pointer
12966 || comp1->attr.proc_pointer)
12967 continue;
12968
12969 finalizable_comp = is_finalizable_type (comp1->ts)
12970 && !finalizable_lhs;
12971
12972 /* Make an assignment for this component. */
12973 this_code = build_assignment (EXEC_ASSIGN,
12974 (*code)->expr1, (*code)->expr2,
12975 comp1, comp2, (*code)->loc);
12976
12977 /* Convert the assignment if there is a defined assignment for
12978 this type. Otherwise, using the call from gfc_resolve_code,
12979 recurse into its components. */
12980 gfc_resolve_code (this_code, ns);
12981
12982 if (this_code->op == EXEC_ASSIGN_CALL)
12983 {
12984 gfc_formal_arglist *dummy_args;
12985 gfc_symbol *rsym;
12986 /* Check that there is a typebound defined assignment. If not,
12987 then this must be a module defined assignment. We cannot
12988 use the defined_assign_comp attribute here because it must
12989 be this derived type that has the defined assignment and not
12990 a parent type. */
12991 if (!(comp1->ts.u.derived->f2k_derived
12992 && comp1->ts.u.derived->f2k_derived
12993 ->tb_op[INTRINSIC_ASSIGN]))
12994 {
12995 gfc_free_statements (this_code);
12996 this_code = NULL;
12997 continue;
12998 }
12999
13000 /* If the first argument of the subroutine has intent INOUT
13001 a temporary must be generated and used instead. */
13002 rsym = this_code->resolved_sym;
13003 dummy_args = gfc_sym_get_dummy_args (rsym);
13004 finalizable_out = gfc_may_be_finalized (comp1->ts)
13005 && dummy_args
13006 && dummy_args->sym->attr.intent == INTENT_OUT;
13007 inout = dummy_args
13008 && dummy_args->sym->attr.intent == INTENT_INOUT;
13009 if ((inout || finalizable_out)
13010 && !comp1->attr.allocatable)
13011 {
13012 gfc_code *temp_code;
13013 inout = true;
13014
13015 /* Build the temporary required for the assignment and put
13016 it at the head of the generated code. */
13017 if (!t1)
13018 {
13019 gfc_namespace *tmp_ns = ns;
13020 if (ns->parent && gfc_may_be_finalized (comp1->ts))
13021 tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13022 t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13023 t1->symtree->n.sym->attr.artificial = 1;
13024 temp_code = build_assignment (EXEC_ASSIGN,
13025 t1, (*code)->expr1,
13026 NULL, NULL, (*code)->loc);
13027
13028 /* For allocatable LHS, check whether it is allocated. Note
13029 that allocatable components with defined assignment are
13030 not yet support. See PR 57696. */
13031 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13032 {
13033 gfc_code *block;
13034 gfc_expr *e =
13035 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13036 block = gfc_get_code (EXEC_IF);
13037 block->block = gfc_get_code (EXEC_IF);
13038 block->block->expr1
13039 = gfc_build_intrinsic_call (ns,
13040 GFC_ISYM_ALLOCATED, "allocated",
13041 (*code)->loc, 1, e);
13042 block->block->next = temp_code;
13043 temp_code = block;
13044 }
13045 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13046 }
13047
13048 /* Replace the first actual arg with the component of the
13049 temporary. */
13050 gfc_free_expr (this_code->ext.actual->expr);
13051 this_code->ext.actual->expr = gfc_copy_expr (t1);
13052 add_comp_ref (this_code->ext.actual->expr, comp1);
13053
13054 /* If the LHS variable is allocatable and wasn't allocated and
13055 the temporary is allocatable, pointer assign the address of
13056 the freshly allocated LHS to the temporary. */
13057 if ((*code)->expr1->symtree->n.sym->attr.allocatable
13058 && gfc_expr_attr ((*code)->expr1).allocatable)
13059 {
13060 gfc_code *block;
13061 gfc_expr *cond;
13062
13063 cond = gfc_get_expr ();
13064 cond->ts.type = BT_LOGICAL;
13065 cond->ts.kind = gfc_default_logical_kind;
13066 cond->expr_type = EXPR_OP;
13067 cond->where = (*code)->loc;
13068 cond->value.op.op = INTRINSIC_NOT;
13069 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13070 GFC_ISYM_ALLOCATED, "allocated",
13071 (*code)->loc, 1, gfc_copy_expr (t1));
13072 block = gfc_get_code (EXEC_IF);
13073 block->block = gfc_get_code (EXEC_IF);
13074 block->block->expr1 = cond;
13075 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13076 t1, (*code)->expr1,
13077 NULL, NULL, (*code)->loc);
13078 add_code_to_chain (&block, &head, &tail);
13079 }
13080 }
13081 }
13082 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13083 {
13084 /* Don't add intrinsic assignments since they are already
13085 effected by the intrinsic assignment of the structure, unless
13086 finalization is required. */
13087 if (finalizable_comp)
13088 this_code->expr1->must_finalize = 1;
13089 else
13090 {
13091 gfc_free_statements (this_code);
13092 this_code = NULL;
13093 continue;
13094 }
13095 }
13096 else
13097 {
13098 /* Resolution has expanded an assignment of a derived type with
13099 defined assigned components. Remove the redundant, leading
13100 assignment. */
13101 gcc_assert (this_code->op == EXEC_ASSIGN);
13102 gfc_code *tmp = this_code;
13103 this_code = this_code->next;
13104 tmp->next = NULL;
13105 gfc_free_statements (tmp);
13106 }
13107
13108 add_code_to_chain (&this_code, &head, &tail);
13109
13110 if (t1 && (inout || finalizable_out))
13111 {
13112 /* Transfer the value to the final result. */
13113 this_code = build_assignment (EXEC_ASSIGN,
13114 (*code)->expr1, t1,
13115 comp1, comp2, (*code)->loc);
13116 this_code->expr1->must_finalize = 0;
13117 add_code_to_chain (&this_code, &head, &tail);
13118 }
13119 }
13120
13121 /* Put the temporary assignments at the top of the generated code. */
13122 if (tmp_head && component_assignment_level == 1)
13123 {
13124 gfc_append_code (tmp_head, head);
13125 head = tmp_head;
13126 tmp_head = tmp_tail = NULL;
13127 }
13128
13129 /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13130 not accidentally deallocated. Hence, nullify t1. */
13131 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13132 && gfc_expr_attr ((*code)->expr1).allocatable)
13133 {
13134 gfc_code *block;
13135 gfc_expr *cond;
13136 gfc_expr *e;
13137
13138 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13139 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
13140 (*code)->loc, 2, gfc_copy_expr (t1), e);
13141 block = gfc_get_code (EXEC_IF);
13142 block->block = gfc_get_code (EXEC_IF);
13143 block->block->expr1 = cond;
13144 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13145 t1, gfc_get_null_expr (&(*code)->loc),
13146 NULL, NULL, (*code)->loc);
13147 gfc_append_code (tail, block);
13148 tail = block;
13149 }
13150
13151 component_assignment_level--;
13152
13153 /* Make an explicit final call for the function result. */
13154 if (tmp_expr)
13155 generate_final_call (tmp_expr, &head, &tail);
13156
13157 if (tmp_code)
13158 {
13159 ns->code = head;
13160 return;
13161 }
13162
13163 /* Now attach the remaining code chain to the input code. Step on
13164 to the end of the new code since resolution is complete. */
13165 gcc_assert ((*code)->op == EXEC_ASSIGN);
13166 tail->next = (*code)->next;
13167 /* Overwrite 'code' because this would place the intrinsic assignment
13168 before the temporary for the lhs is created. */
13169 gfc_free_expr ((*code)->expr1);
13170 gfc_free_expr ((*code)->expr2);
13171 **code = *head;
13172 if (head != tail)
13173 free (head);
13174 *code = tail;
13175 }
13176
13177
13178 /* F2008: Pointer function assignments are of the form:
13179 ptr_fcn (args) = expr
13180 This function breaks these assignments into two statements:
13181 temporary_pointer => ptr_fcn(args)
13182 temporary_pointer = expr */
13183
13184 static bool
13185 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
13186 {
13187 gfc_expr *tmp_ptr_expr;
13188 gfc_code *this_code;
13189 gfc_component *comp;
13190 gfc_symbol *s;
13191
13192 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
13193 return false;
13194
13195 /* Even if standard does not support this feature, continue to build
13196 the two statements to avoid upsetting frontend_passes.c. */
13197 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
13198 "%L", &(*code)->loc);
13199
13200 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
13201
13202 if (comp)
13203 s = comp->ts.interface;
13204 else
13205 s = (*code)->expr1->symtree->n.sym;
13206
13207 if (s == NULL || !s->result->attr.pointer)
13208 {
13209 gfc_error ("The function result on the lhs of the assignment at "
13210 "%L must have the pointer attribute.",
13211 &(*code)->expr1->where);
13212 (*code)->op = EXEC_NOP;
13213 return false;
13214 }
13215
13216 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
13217
13218 /* get_temp_from_expression is set up for ordinary assignments. To that
13219 end, where array bounds are not known, arrays are made allocatable.
13220 Change the temporary to a pointer here. */
13221 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
13222 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
13223 tmp_ptr_expr->where = (*code)->loc;
13224
13225 /* A new charlen is required to ensure that the variable string length
13226 is different to that of the original lhs for deferred results. */
13227 if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
13228 {
13229 tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
13230 tmp_ptr_expr->ts.deferred = 1;
13231 tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
13232 gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
13233 tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
13234 }
13235
13236 this_code = build_assignment (EXEC_ASSIGN,
13237 tmp_ptr_expr, (*code)->expr2,
13238 NULL, NULL, (*code)->loc);
13239 this_code->next = (*code)->next;
13240 (*code)->next = this_code;
13241 (*code)->op = EXEC_POINTER_ASSIGN;
13242 (*code)->expr2 = (*code)->expr1;
13243 (*code)->expr1 = tmp_ptr_expr;
13244
13245 return true;
13246 }
13247
13248
13249 /* Deferred character length assignments from an operator expression
13250 require a temporary because the character length of the lhs can
13251 change in the course of the assignment. */
13252
13253 static bool
13254 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
13255 {
13256 gfc_expr *tmp_expr;
13257 gfc_code *this_code;
13258
13259 if (!((*code)->expr1->ts.type == BT_CHARACTER
13260 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
13261 && (*code)->expr2->ts.type == BT_CHARACTER
13262 && (*code)->expr2->expr_type == EXPR_OP))
13263 return false;
13264
13265 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
13266 return false;
13267
13268 if (gfc_expr_attr ((*code)->expr1).pointer)
13269 return false;
13270
13271 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13272 tmp_expr->where = (*code)->loc;
13273
13274 /* A new charlen is required to ensure that the variable string
13275 length is different to that of the original lhs. */
13276 tmp_expr->ts.u.cl = gfc_get_charlen();
13277 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
13278 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
13279 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
13280
13281 tmp_expr->symtree->n.sym->ts.deferred = 1;
13282
13283 this_code = build_assignment (EXEC_ASSIGN,
13284 (*code)->expr1,
13285 gfc_copy_expr (tmp_expr),
13286 NULL, NULL, (*code)->loc);
13287
13288 (*code)->expr1 = tmp_expr;
13289
13290 this_code->next = (*code)->next;
13291 (*code)->next = this_code;
13292
13293 return true;
13294 }
13295
13296
13297 static bool
13298 check_team (gfc_expr *team, const char *intrinsic)
13299 {
13300 if (team->rank != 0
13301 || team->ts.type != BT_DERIVED
13302 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
13303 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
13304 {
13305 gfc_error ("TEAM argument to %qs at %L must be a scalar expression "
13306 "of type TEAM_TYPE", intrinsic, &team->where);
13307 return false;
13308 }
13309
13310 return true;
13311 }
13312
13313
13314 /* Given a block of code, recursively resolve everything pointed to by this
13315 code block. */
13316
13317 void
13318 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
13319 {
13320 int omp_workshare_save;
13321 int forall_save, do_concurrent_save;
13322 code_stack frame;
13323 bool t;
13324
13325 frame.prev = cs_base;
13326 frame.head = code;
13327 cs_base = &frame;
13328
13329 find_reachable_labels (code);
13330
13331 for (; code; code = code->next)
13332 {
13333 frame.current = code;
13334 forall_save = forall_flag;
13335 do_concurrent_save = gfc_do_concurrent_flag;
13336
13337 if (code->op == EXEC_FORALL)
13338 {
13339 forall_flag = 1;
13340 gfc_resolve_forall (code, ns, forall_save);
13341 forall_flag = 2;
13342 }
13343 else if (code->op == EXEC_OMP_METADIRECTIVE)
13344 for (gfc_omp_variant *variant
13345 = code->ext.omp_variants;
13346 variant; variant = variant->next)
13347 gfc_resolve_code (variant->code, ns);
13348 else if (code->block)
13349 {
13350 omp_workshare_save = -1;
13351 switch (code->op)
13352 {
13353 case EXEC_OACC_PARALLEL_LOOP:
13354 case EXEC_OACC_PARALLEL:
13355 case EXEC_OACC_KERNELS_LOOP:
13356 case EXEC_OACC_KERNELS:
13357 case EXEC_OACC_SERIAL_LOOP:
13358 case EXEC_OACC_SERIAL:
13359 case EXEC_OACC_DATA:
13360 case EXEC_OACC_HOST_DATA:
13361 case EXEC_OACC_LOOP:
13362 gfc_resolve_oacc_blocks (code, ns);
13363 break;
13364 case EXEC_OMP_PARALLEL_WORKSHARE:
13365 omp_workshare_save = omp_workshare_flag;
13366 omp_workshare_flag = 1;
13367 gfc_resolve_omp_parallel_blocks (code, ns);
13368 break;
13369 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13370 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13371 case EXEC_OMP_MASKED_TASKLOOP:
13372 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13373 case EXEC_OMP_MASTER_TASKLOOP:
13374 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13375 case EXEC_OMP_PARALLEL:
13376 case EXEC_OMP_PARALLEL_DO:
13377 case EXEC_OMP_PARALLEL_DO_SIMD:
13378 case EXEC_OMP_PARALLEL_LOOP:
13379 case EXEC_OMP_PARALLEL_MASKED:
13380 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13381 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13382 case EXEC_OMP_PARALLEL_MASTER:
13383 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13384 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13385 case EXEC_OMP_PARALLEL_SECTIONS:
13386 case EXEC_OMP_TARGET_PARALLEL:
13387 case EXEC_OMP_TARGET_PARALLEL_DO:
13388 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13389 case EXEC_OMP_TARGET_PARALLEL_LOOP:
13390 case EXEC_OMP_TARGET_TEAMS:
13391 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13392 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13393 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13394 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13395 case EXEC_OMP_TARGET_TEAMS_LOOP:
13396 case EXEC_OMP_TASK:
13397 case EXEC_OMP_TASKLOOP:
13398 case EXEC_OMP_TASKLOOP_SIMD:
13399 case EXEC_OMP_TEAMS:
13400 case EXEC_OMP_TEAMS_DISTRIBUTE:
13401 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13402 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13403 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13404 case EXEC_OMP_TEAMS_LOOP:
13405 omp_workshare_save = omp_workshare_flag;
13406 omp_workshare_flag = 0;
13407 gfc_resolve_omp_parallel_blocks (code, ns);
13408 break;
13409 case EXEC_OMP_DISTRIBUTE:
13410 case EXEC_OMP_DISTRIBUTE_SIMD:
13411 case EXEC_OMP_DO:
13412 case EXEC_OMP_DO_SIMD:
13413 case EXEC_OMP_LOOP:
13414 case EXEC_OMP_SIMD:
13415 case EXEC_OMP_TARGET_SIMD:
13416 case EXEC_OMP_TILE:
13417 case EXEC_OMP_UNROLL:
13418 gfc_resolve_omp_do_blocks (code, ns);
13419 break;
13420 case EXEC_SELECT_TYPE:
13421 case EXEC_SELECT_RANK:
13422 /* Blocks are handled in resolve_select_type/rank because we
13423 have to transform the SELECT TYPE into ASSOCIATE first. */
13424 break;
13425 case EXEC_DO_CONCURRENT:
13426 gfc_do_concurrent_flag = 1;
13427 gfc_resolve_blocks (code->block, ns);
13428 gfc_do_concurrent_flag = 2;
13429 break;
13430 case EXEC_OMP_WORKSHARE:
13431 omp_workshare_save = omp_workshare_flag;
13432 omp_workshare_flag = 1;
13433 /* FALL THROUGH */
13434 default:
13435 gfc_resolve_blocks (code->block, ns);
13436 break;
13437 }
13438
13439 if (omp_workshare_save != -1)
13440 omp_workshare_flag = omp_workshare_save;
13441 }
13442 start:
13443 t = true;
13444 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
13445 t = gfc_resolve_expr (code->expr1);
13446
13447 forall_flag = forall_save;
13448 gfc_do_concurrent_flag = do_concurrent_save;
13449
13450 if (!gfc_resolve_expr (code->expr2))
13451 t = false;
13452
13453 if (code->op == EXEC_ALLOCATE
13454 && !gfc_resolve_expr (code->expr3))
13455 t = false;
13456
13457 switch (code->op)
13458 {
13459 case EXEC_NOP:
13460 case EXEC_END_BLOCK:
13461 case EXEC_END_NESTED_BLOCK:
13462 case EXEC_CYCLE:
13463 case EXEC_PAUSE:
13464 break;
13465
13466 case EXEC_STOP:
13467 case EXEC_ERROR_STOP:
13468 if (code->expr2 != NULL
13469 && (code->expr2->ts.type != BT_LOGICAL
13470 || code->expr2->rank != 0))
13471 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
13472 &code->expr2->where);
13473 break;
13474
13475 case EXEC_EXIT:
13476 case EXEC_CONTINUE:
13477 case EXEC_DT_END:
13478 case EXEC_ASSIGN_CALL:
13479 break;
13480
13481 case EXEC_CRITICAL:
13482 resolve_critical (code);
13483 break;
13484
13485 case EXEC_SYNC_ALL:
13486 case EXEC_SYNC_IMAGES:
13487 case EXEC_SYNC_MEMORY:
13488 resolve_sync (code);
13489 break;
13490
13491 case EXEC_LOCK:
13492 case EXEC_UNLOCK:
13493 case EXEC_EVENT_POST:
13494 case EXEC_EVENT_WAIT:
13495 resolve_lock_unlock_event (code);
13496 break;
13497
13498 case EXEC_FAIL_IMAGE:
13499 break;
13500
13501 case EXEC_FORM_TEAM:
13502 if (code->expr1 != NULL
13503 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13504 gfc_error ("TEAM NUMBER argument to FORM TEAM at %L must be "
13505 "a scalar INTEGER", &code->expr1->where);
13506 check_team (code->expr2, "FORM TEAM");
13507 break;
13508
13509 case EXEC_CHANGE_TEAM:
13510 check_team (code->expr1, "CHANGE TEAM");
13511 break;
13512
13513 case EXEC_END_TEAM:
13514 break;
13515
13516 case EXEC_SYNC_TEAM:
13517 check_team (code->expr1, "SYNC TEAM");
13518 break;
13519
13520 case EXEC_ENTRY:
13521 /* Keep track of which entry we are up to. */
13522 current_entry_id = code->ext.entry->id;
13523 break;
13524
13525 case EXEC_WHERE:
13526 resolve_where (code, NULL);
13527 break;
13528
13529 case EXEC_GOTO:
13530 if (code->expr1 != NULL)
13531 {
13532 if (code->expr1->expr_type != EXPR_VARIABLE
13533 || code->expr1->ts.type != BT_INTEGER
13534 || (code->expr1->ref
13535 && code->expr1->ref->type == REF_ARRAY)
13536 || code->expr1->symtree == NULL
13537 || (code->expr1->symtree->n.sym
13538 && (code->expr1->symtree->n.sym->attr.flavor
13539 == FL_PARAMETER)))
13540 gfc_error ("ASSIGNED GOTO statement at %L requires a "
13541 "scalar INTEGER variable", &code->expr1->where);
13542 else if (code->expr1->symtree->n.sym
13543 && code->expr1->symtree->n.sym->attr.assign != 1)
13544 gfc_error ("Variable %qs has not been assigned a target "
13545 "label at %L", code->expr1->symtree->n.sym->name,
13546 &code->expr1->where);
13547 }
13548 else
13549 resolve_branch (code->label1, code);
13550 break;
13551
13552 case EXEC_RETURN:
13553 if (code->expr1 != NULL
13554 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13555 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
13556 "INTEGER return specifier", &code->expr1->where);
13557 break;
13558
13559 case EXEC_INIT_ASSIGN:
13560 case EXEC_END_PROCEDURE:
13561 break;
13562
13563 case EXEC_ASSIGN:
13564 if (!t)
13565 break;
13566
13567 if (flag_coarray == GFC_FCOARRAY_LIB
13568 && gfc_is_coindexed (code->expr1))
13569 {
13570 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
13571 coindexed variable. */
13572 code->op = EXEC_CALL;
13573 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
13574 true);
13575 code->resolved_sym = code->symtree->n.sym;
13576 code->resolved_sym->attr.flavor = FL_PROCEDURE;
13577 code->resolved_sym->attr.intrinsic = 1;
13578 code->resolved_sym->attr.subroutine = 1;
13579 code->resolved_isym
13580 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
13581 gfc_commit_symbol (code->resolved_sym);
13582 code->ext.actual = gfc_get_actual_arglist ();
13583 code->ext.actual->expr = code->expr1;
13584 code->ext.actual->next = gfc_get_actual_arglist ();
13585 code->ext.actual->next->expr = code->expr2;
13586
13587 code->expr1 = NULL;
13588 code->expr2 = NULL;
13589 break;
13590 }
13591
13592 if (code->expr1->ts.type == BT_CLASS)
13593 gfc_find_vtab (&code->expr2->ts);
13594
13595 /* If this is a pointer function in an lvalue variable context,
13596 the new code will have to be resolved afresh. This is also the
13597 case with an error, where the code is transformed into NOP to
13598 prevent ICEs downstream. */
13599 if (resolve_ptr_fcn_assign (&code, ns)
13600 || code->op == EXEC_NOP)
13601 goto start;
13602
13603 if (!gfc_check_vardef_context (code->expr1, false, false, false,
13604 _("assignment")))
13605 break;
13606
13607 if (resolve_ordinary_assign (code, ns))
13608 {
13609 if (omp_workshare_flag)
13610 {
13611 gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
13612 "at %L", &code->loc);
13613 break;
13614 }
13615 if (code->op == EXEC_COMPCALL)
13616 goto compcall;
13617 else
13618 goto call;
13619 }
13620
13621 /* Check for dependencies in deferred character length array
13622 assignments and generate a temporary, if necessary. */
13623 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
13624 break;
13625
13626 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
13627 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
13628 && code->expr1->ts.u.derived
13629 && code->expr1->ts.u.derived->attr.defined_assign_comp)
13630 generate_component_assignments (&code, ns);
13631 else if (code->op == EXEC_ASSIGN)
13632 {
13633 if (gfc_may_be_finalized (code->expr1->ts))
13634 code->expr1->must_finalize = 1;
13635 if (code->expr2->expr_type == EXPR_ARRAY
13636 && gfc_may_be_finalized (code->expr2->ts))
13637 code->expr2->must_finalize = 1;
13638 }
13639
13640 break;
13641
13642 case EXEC_LABEL_ASSIGN:
13643 if (code->label1->defined == ST_LABEL_UNKNOWN)
13644 gfc_error ("Label %d referenced at %L is never defined",
13645 code->label1->value, &code->label1->where);
13646 if (t
13647 && (code->expr1->expr_type != EXPR_VARIABLE
13648 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
13649 || code->expr1->symtree->n.sym->ts.kind
13650 != gfc_default_integer_kind
13651 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
13652 || code->expr1->symtree->n.sym->as != NULL))
13653 gfc_error ("ASSIGN statement at %L requires a scalar "
13654 "default INTEGER variable", &code->expr1->where);
13655 break;
13656
13657 case EXEC_POINTER_ASSIGN:
13658 {
13659 gfc_expr* e;
13660
13661 if (!t)
13662 break;
13663
13664 /* This is both a variable definition and pointer assignment
13665 context, so check both of them. For rank remapping, a final
13666 array ref may be present on the LHS and fool gfc_expr_attr
13667 used in gfc_check_vardef_context. Remove it. */
13668 e = remove_last_array_ref (code->expr1);
13669 t = gfc_check_vardef_context (e, true, false, false,
13670 _("pointer assignment"));
13671 if (t)
13672 t = gfc_check_vardef_context (e, false, false, false,
13673 _("pointer assignment"));
13674 gfc_free_expr (e);
13675
13676 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
13677
13678 if (!t)
13679 break;
13680
13681 /* Assigning a class object always is a regular assign. */
13682 if (code->expr2->ts.type == BT_CLASS
13683 && code->expr1->ts.type == BT_CLASS
13684 && CLASS_DATA (code->expr2)
13685 && !CLASS_DATA (code->expr2)->attr.dimension
13686 && !(gfc_expr_attr (code->expr1).proc_pointer
13687 && code->expr2->expr_type == EXPR_VARIABLE
13688 && code->expr2->symtree->n.sym->attr.flavor
13689 == FL_PROCEDURE))
13690 code->op = EXEC_ASSIGN;
13691 break;
13692 }
13693
13694 case EXEC_ARITHMETIC_IF:
13695 {
13696 gfc_expr *e = code->expr1;
13697
13698 gfc_resolve_expr (e);
13699 if (e->expr_type == EXPR_NULL)
13700 gfc_error ("Invalid NULL at %L", &e->where);
13701
13702 if (t && (e->rank > 0
13703 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
13704 gfc_error ("Arithmetic IF statement at %L requires a scalar "
13705 "REAL or INTEGER expression", &e->where);
13706
13707 resolve_branch (code->label1, code);
13708 resolve_branch (code->label2, code);
13709 resolve_branch (code->label3, code);
13710 }
13711 break;
13712
13713 case EXEC_IF:
13714 if (t && code->expr1 != NULL
13715 && (code->expr1->ts.type != BT_LOGICAL
13716 || code->expr1->rank != 0))
13717 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
13718 &code->expr1->where);
13719 break;
13720
13721 case EXEC_CALL:
13722 call:
13723 resolve_call (code);
13724 break;
13725
13726 case EXEC_COMPCALL:
13727 compcall:
13728 resolve_typebound_subroutine (code);
13729 break;
13730
13731 case EXEC_CALL_PPC:
13732 resolve_ppc_call (code);
13733 break;
13734
13735 case EXEC_SELECT:
13736 /* Select is complicated. Also, a SELECT construct could be
13737 a transformed computed GOTO. */
13738 resolve_select (code, false);
13739 break;
13740
13741 case EXEC_SELECT_TYPE:
13742 resolve_select_type (code, ns);
13743 break;
13744
13745 case EXEC_SELECT_RANK:
13746 resolve_select_rank (code, ns);
13747 break;
13748
13749 case EXEC_BLOCK:
13750 resolve_block_construct (code);
13751 break;
13752
13753 case EXEC_DO:
13754 if (code->ext.iterator != NULL)
13755 {
13756 gfc_iterator *iter = code->ext.iterator;
13757 if (gfc_resolve_iterator (iter, true, false))
13758 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
13759 true);
13760 }
13761 break;
13762
13763 case EXEC_DO_WHILE:
13764 if (code->expr1 == NULL)
13765 gfc_internal_error ("gfc_resolve_code(): No expression on "
13766 "DO WHILE");
13767 if (t
13768 && (code->expr1->rank != 0
13769 || code->expr1->ts.type != BT_LOGICAL))
13770 gfc_error ("Exit condition of DO WHILE loop at %L must be "
13771 "a scalar LOGICAL expression", &code->expr1->where);
13772 break;
13773
13774 case EXEC_ALLOCATE:
13775 if (t)
13776 resolve_allocate_deallocate (code, "ALLOCATE");
13777
13778 break;
13779
13780 case EXEC_DEALLOCATE:
13781 if (t)
13782 resolve_allocate_deallocate (code, "DEALLOCATE");
13783
13784 break;
13785
13786 case EXEC_OPEN:
13787 if (!gfc_resolve_open (code->ext.open, &code->loc))
13788 break;
13789
13790 resolve_branch (code->ext.open->err, code);
13791 break;
13792
13793 case EXEC_CLOSE:
13794 if (!gfc_resolve_close (code->ext.close, &code->loc))
13795 break;
13796
13797 resolve_branch (code->ext.close->err, code);
13798 break;
13799
13800 case EXEC_BACKSPACE:
13801 case EXEC_ENDFILE:
13802 case EXEC_REWIND:
13803 case EXEC_FLUSH:
13804 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
13805 break;
13806
13807 resolve_branch (code->ext.filepos->err, code);
13808 break;
13809
13810 case EXEC_INQUIRE:
13811 if (!gfc_resolve_inquire (code->ext.inquire))
13812 break;
13813
13814 resolve_branch (code->ext.inquire->err, code);
13815 break;
13816
13817 case EXEC_IOLENGTH:
13818 gcc_assert (code->ext.inquire != NULL);
13819 if (!gfc_resolve_inquire (code->ext.inquire))
13820 break;
13821
13822 resolve_branch (code->ext.inquire->err, code);
13823 break;
13824
13825 case EXEC_WAIT:
13826 if (!gfc_resolve_wait (code->ext.wait))
13827 break;
13828
13829 resolve_branch (code->ext.wait->err, code);
13830 resolve_branch (code->ext.wait->end, code);
13831 resolve_branch (code->ext.wait->eor, code);
13832 break;
13833
13834 case EXEC_READ:
13835 case EXEC_WRITE:
13836 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
13837 break;
13838
13839 resolve_branch (code->ext.dt->err, code);
13840 resolve_branch (code->ext.dt->end, code);
13841 resolve_branch (code->ext.dt->eor, code);
13842 break;
13843
13844 case EXEC_TRANSFER:
13845 resolve_transfer (code);
13846 break;
13847
13848 case EXEC_DO_CONCURRENT:
13849 case EXEC_FORALL:
13850 resolve_forall_iterators (code->ext.concur.forall_iterator);
13851
13852 if (code->expr1 != NULL
13853 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
13854 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
13855 "expression", &code->expr1->where);
13856
13857 if (code->op == EXEC_DO_CONCURRENT)
13858 resolve_locality_spec (code, ns);
13859 break;
13860
13861 case EXEC_OACC_PARALLEL_LOOP:
13862 case EXEC_OACC_PARALLEL:
13863 case EXEC_OACC_KERNELS_LOOP:
13864 case EXEC_OACC_KERNELS:
13865 case EXEC_OACC_SERIAL_LOOP:
13866 case EXEC_OACC_SERIAL:
13867 case EXEC_OACC_DATA:
13868 case EXEC_OACC_HOST_DATA:
13869 case EXEC_OACC_LOOP:
13870 case EXEC_OACC_UPDATE:
13871 case EXEC_OACC_WAIT:
13872 case EXEC_OACC_CACHE:
13873 case EXEC_OACC_ENTER_DATA:
13874 case EXEC_OACC_EXIT_DATA:
13875 case EXEC_OACC_ATOMIC:
13876 case EXEC_OACC_DECLARE:
13877 gfc_resolve_oacc_directive (code, ns);
13878 break;
13879
13880 case EXEC_OMP_ALLOCATE:
13881 case EXEC_OMP_ALLOCATORS:
13882 case EXEC_OMP_ASSUME:
13883 case EXEC_OMP_ATOMIC:
13884 case EXEC_OMP_BARRIER:
13885 case EXEC_OMP_CANCEL:
13886 case EXEC_OMP_CANCELLATION_POINT:
13887 case EXEC_OMP_CRITICAL:
13888 case EXEC_OMP_FLUSH:
13889 case EXEC_OMP_DEPOBJ:
13890 case EXEC_OMP_DISPATCH:
13891 case EXEC_OMP_DISTRIBUTE:
13892 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13893 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13894 case EXEC_OMP_DISTRIBUTE_SIMD:
13895 case EXEC_OMP_DO:
13896 case EXEC_OMP_DO_SIMD:
13897 case EXEC_OMP_ERROR:
13898 case EXEC_OMP_INTEROP:
13899 case EXEC_OMP_LOOP:
13900 case EXEC_OMP_MASTER:
13901 case EXEC_OMP_MASTER_TASKLOOP:
13902 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13903 case EXEC_OMP_MASKED:
13904 case EXEC_OMP_MASKED_TASKLOOP:
13905 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13906 case EXEC_OMP_METADIRECTIVE:
13907 case EXEC_OMP_ORDERED:
13908 case EXEC_OMP_SCAN:
13909 case EXEC_OMP_SCOPE:
13910 case EXEC_OMP_SECTIONS:
13911 case EXEC_OMP_SIMD:
13912 case EXEC_OMP_SINGLE:
13913 case EXEC_OMP_TARGET:
13914 case EXEC_OMP_TARGET_DATA:
13915 case EXEC_OMP_TARGET_ENTER_DATA:
13916 case EXEC_OMP_TARGET_EXIT_DATA:
13917 case EXEC_OMP_TARGET_PARALLEL:
13918 case EXEC_OMP_TARGET_PARALLEL_DO:
13919 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13920 case EXEC_OMP_TARGET_PARALLEL_LOOP:
13921 case EXEC_OMP_TARGET_SIMD:
13922 case EXEC_OMP_TARGET_TEAMS:
13923 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13924 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13925 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13926 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13927 case EXEC_OMP_TARGET_TEAMS_LOOP:
13928 case EXEC_OMP_TARGET_UPDATE:
13929 case EXEC_OMP_TASK:
13930 case EXEC_OMP_TASKGROUP:
13931 case EXEC_OMP_TASKLOOP:
13932 case EXEC_OMP_TASKLOOP_SIMD:
13933 case EXEC_OMP_TASKWAIT:
13934 case EXEC_OMP_TASKYIELD:
13935 case EXEC_OMP_TEAMS:
13936 case EXEC_OMP_TEAMS_DISTRIBUTE:
13937 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13938 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13939 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13940 case EXEC_OMP_TEAMS_LOOP:
13941 case EXEC_OMP_TILE:
13942 case EXEC_OMP_UNROLL:
13943 case EXEC_OMP_WORKSHARE:
13944 gfc_resolve_omp_directive (code, ns);
13945 break;
13946
13947 case EXEC_OMP_PARALLEL:
13948 case EXEC_OMP_PARALLEL_DO:
13949 case EXEC_OMP_PARALLEL_DO_SIMD:
13950 case EXEC_OMP_PARALLEL_LOOP:
13951 case EXEC_OMP_PARALLEL_MASKED:
13952 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13953 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13954 case EXEC_OMP_PARALLEL_MASTER:
13955 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13956 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13957 case EXEC_OMP_PARALLEL_SECTIONS:
13958 case EXEC_OMP_PARALLEL_WORKSHARE:
13959 omp_workshare_save = omp_workshare_flag;
13960 omp_workshare_flag = 0;
13961 gfc_resolve_omp_directive (code, ns);
13962 omp_workshare_flag = omp_workshare_save;
13963 break;
13964
13965 default:
13966 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
13967 }
13968 }
13969
13970 cs_base = frame.prev;
13971 }
13972
13973
13974 /* Resolve initial values and make sure they are compatible with
13975 the variable. */
13976
13977 static void
13978 resolve_values (gfc_symbol *sym)
13979 {
13980 bool t;
13981
13982 if (sym->value == NULL)
13983 return;
13984
13985 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
13986 gfc_warning (OPT_Wdeprecated_declarations,
13987 "Using parameter %qs declared at %L is deprecated",
13988 sym->name, &sym->declared_at);
13989
13990 if (sym->value->expr_type == EXPR_STRUCTURE)
13991 t= resolve_structure_cons (sym->value, 1);
13992 else
13993 t = gfc_resolve_expr (sym->value);
13994
13995 if (!t)
13996 return;
13997
13998 gfc_check_assign_symbol (sym, NULL, sym->value);
13999 }
14000
14001
14002 /* Verify any BIND(C) derived types in the namespace so we can report errors
14003 for them once, rather than for each variable declared of that type. */
14004
14005 static void
14006 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14007 {
14008 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14009 && derived_sym->attr.is_bind_c == 1)
14010 verify_bind_c_derived_type (derived_sym);
14011
14012 return;
14013 }
14014
14015
14016 /* Check the interfaces of DTIO procedures associated with derived
14017 type 'sym'. These procedures can either have typebound bindings or
14018 can appear in DTIO generic interfaces. */
14019
14020 static void
14021 gfc_verify_DTIO_procedures (gfc_symbol *sym)
14022 {
14023 if (!sym || sym->attr.flavor != FL_DERIVED)
14024 return;
14025
14026 gfc_check_dtio_interfaces (sym);
14027
14028 return;
14029 }
14030
14031 /* Verify that any binding labels used in a given namespace do not collide
14032 with the names or binding labels of any global symbols. Multiple INTERFACE
14033 for the same procedure are permitted. Abstract interfaces and dummy
14034 arguments are not checked. */
14035
14036 static void
14037 gfc_verify_binding_labels (gfc_symbol *sym)
14038 {
14039 gfc_gsymbol *gsym;
14040 const char *module;
14041
14042 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
14043 || sym->attr.flavor == FL_DERIVED || !sym->binding_label
14044 || sym->attr.abstract || sym->attr.dummy)
14045 return;
14046
14047 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
14048
14049 if (sym->module)
14050 module = sym->module;
14051 else if (sym->ns && sym->ns->proc_name
14052 && sym->ns->proc_name->attr.flavor == FL_MODULE)
14053 module = sym->ns->proc_name->name;
14054 else if (sym->ns && sym->ns->parent
14055 && sym->ns && sym->ns->parent->proc_name
14056 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14057 module = sym->ns->parent->proc_name->name;
14058 else
14059 module = NULL;
14060
14061 if (!gsym
14062 || (!gsym->defined
14063 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
14064 {
14065 if (!gsym)
14066 gsym = gfc_get_gsymbol (sym->binding_label, true);
14067 gsym->where = sym->declared_at;
14068 gsym->sym_name = sym->name;
14069 gsym->binding_label = sym->binding_label;
14070 gsym->ns = sym->ns;
14071 gsym->mod_name = module;
14072 if (sym->attr.function)
14073 gsym->type = GSYM_FUNCTION;
14074 else if (sym->attr.subroutine)
14075 gsym->type = GSYM_SUBROUTINE;
14076 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
14077 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
14078 return;
14079 }
14080
14081 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
14082 {
14083 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
14084 "identifier as entity at %L", sym->name,
14085 sym->binding_label, &sym->declared_at, &gsym->where);
14086 /* Clear the binding label to prevent checking multiple times. */
14087 sym->binding_label = NULL;
14088 return;
14089 }
14090
14091 if (sym->attr.flavor == FL_VARIABLE && module
14092 && (strcmp (module, gsym->mod_name) != 0
14093 || strcmp (sym->name, gsym->sym_name) != 0))
14094 {
14095 /* This can only happen if the variable is defined in a module - if it
14096 isn't the same module, reject it. */
14097 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
14098 "uses the same global identifier as entity at %L from module %qs",
14099 sym->name, module, sym->binding_label,
14100 &sym->declared_at, &gsym->where, gsym->mod_name);
14101 sym->binding_label = NULL;
14102 return;
14103 }
14104
14105 if ((sym->attr.function || sym->attr.subroutine)
14106 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
14107 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
14108 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
14109 && (module != gsym->mod_name
14110 || strcmp (gsym->sym_name, sym->name) != 0
14111 || (module && strcmp (module, gsym->mod_name) != 0)))
14112 {
14113 /* Print an error if the procedure is defined multiple times; we have to
14114 exclude references to the same procedure via module association or
14115 multiple checks for the same procedure. */
14116 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
14117 "global identifier as entity at %L", sym->name,
14118 sym->binding_label, &sym->declared_at, &gsym->where);
14119 sym->binding_label = NULL;
14120 }
14121 }
14122
14123
14124 /* Resolve an index expression. */
14125
14126 static bool
14127 resolve_index_expr (gfc_expr *e)
14128 {
14129 if (!gfc_resolve_expr (e))
14130 return false;
14131
14132 if (!gfc_simplify_expr (e, 0))
14133 return false;
14134
14135 if (!gfc_specification_expr (e))
14136 return false;
14137
14138 return true;
14139 }
14140
14141
14142 /* Resolve a charlen structure. */
14143
14144 static bool
14145 resolve_charlen (gfc_charlen *cl)
14146 {
14147 int k;
14148 bool saved_specification_expr;
14149
14150 if (cl->resolved)
14151 return true;
14152
14153 cl->resolved = 1;
14154 saved_specification_expr = specification_expr;
14155 specification_expr = true;
14156
14157 if (cl->length_from_typespec)
14158 {
14159 if (!gfc_resolve_expr (cl->length))
14160 {
14161 specification_expr = saved_specification_expr;
14162 return false;
14163 }
14164
14165 if (!gfc_simplify_expr (cl->length, 0))
14166 {
14167 specification_expr = saved_specification_expr;
14168 return false;
14169 }
14170
14171 /* cl->length has been resolved. It should have an integer type. */
14172 if (cl->length
14173 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
14174 {
14175 gfc_error ("Scalar INTEGER expression expected at %L",
14176 &cl->length->where);
14177 return false;
14178 }
14179 }
14180 else
14181 {
14182 if (!resolve_index_expr (cl->length))
14183 {
14184 specification_expr = saved_specification_expr;
14185 return false;
14186 }
14187 }
14188
14189 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
14190 a negative value, the length of character entities declared is zero. */
14191 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14192 && mpz_sgn (cl->length->value.integer) < 0)
14193 gfc_replace_expr (cl->length,
14194 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
14195
14196 /* Check that the character length is not too large. */
14197 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
14198 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14199 && cl->length->ts.type == BT_INTEGER
14200 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
14201 {
14202 gfc_error ("String length at %L is too large", &cl->length->where);
14203 specification_expr = saved_specification_expr;
14204 return false;
14205 }
14206
14207 specification_expr = saved_specification_expr;
14208 return true;
14209 }
14210
14211
14212 /* Test for non-constant shape arrays. */
14213
14214 static bool
14215 is_non_constant_shape_array (gfc_symbol *sym)
14216 {
14217 gfc_expr *e;
14218 int i;
14219 bool not_constant;
14220
14221 not_constant = false;
14222 if (sym->as != NULL)
14223 {
14224 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
14225 has not been simplified; parameter array references. Do the
14226 simplification now. */
14227 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
14228 {
14229 if (i == GFC_MAX_DIMENSIONS)
14230 break;
14231
14232 e = sym->as->lower[i];
14233 if (e && (!resolve_index_expr(e)
14234 || !gfc_is_constant_expr (e)))
14235 not_constant = true;
14236 e = sym->as->upper[i];
14237 if (e && (!resolve_index_expr(e)
14238 || !gfc_is_constant_expr (e)))
14239 not_constant = true;
14240 }
14241 }
14242 return not_constant;
14243 }
14244
14245 /* Given a symbol and an initialization expression, add code to initialize
14246 the symbol to the function entry. */
14247 static void
14248 build_init_assign (gfc_symbol *sym, gfc_expr *init)
14249 {
14250 gfc_expr *lval;
14251 gfc_code *init_st;
14252 gfc_namespace *ns = sym->ns;
14253
14254 /* Search for the function namespace if this is a contained
14255 function without an explicit result. */
14256 if (sym->attr.function && sym == sym->result
14257 && sym->name != sym->ns->proc_name->name)
14258 {
14259 ns = ns->contained;
14260 for (;ns; ns = ns->sibling)
14261 if (strcmp (ns->proc_name->name, sym->name) == 0)
14262 break;
14263 }
14264
14265 if (ns == NULL)
14266 {
14267 gfc_free_expr (init);
14268 return;
14269 }
14270
14271 /* Build an l-value expression for the result. */
14272 lval = gfc_lval_expr_from_sym (sym);
14273
14274 /* Add the code at scope entry. */
14275 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
14276 init_st->next = ns->code;
14277 ns->code = init_st;
14278
14279 /* Assign the default initializer to the l-value. */
14280 init_st->loc = sym->declared_at;
14281 init_st->expr1 = lval;
14282 init_st->expr2 = init;
14283 }
14284
14285
14286 /* Whether or not we can generate a default initializer for a symbol. */
14287
14288 static bool
14289 can_generate_init (gfc_symbol *sym)
14290 {
14291 symbol_attribute *a;
14292 if (!sym)
14293 return false;
14294 a = &sym->attr;
14295
14296 /* These symbols should never have a default initialization. */
14297 return !(
14298 a->allocatable
14299 || a->external
14300 || a->pointer
14301 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
14302 && (CLASS_DATA (sym)->attr.class_pointer
14303 || CLASS_DATA (sym)->attr.proc_pointer))
14304 || a->in_equivalence
14305 || a->in_common
14306 || a->data
14307 || sym->module
14308 || a->cray_pointee
14309 || a->cray_pointer
14310 || sym->assoc
14311 || (!a->referenced && !a->result)
14312 || (a->dummy && (a->intent != INTENT_OUT
14313 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
14314 || (a->function && sym != sym->result)
14315 );
14316 }
14317
14318
14319 /* Assign the default initializer to a derived type variable or result. */
14320
14321 static void
14322 apply_default_init (gfc_symbol *sym)
14323 {
14324 gfc_expr *init = NULL;
14325
14326 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14327 return;
14328
14329 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
14330 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14331
14332 if (init == NULL && sym->ts.type != BT_CLASS)
14333 return;
14334
14335 build_init_assign (sym, init);
14336 sym->attr.referenced = 1;
14337 }
14338
14339
14340 /* Build an initializer for a local. Returns null if the symbol should not have
14341 a default initialization. */
14342
14343 static gfc_expr *
14344 build_default_init_expr (gfc_symbol *sym)
14345 {
14346 /* These symbols should never have a default initialization. */
14347 if (sym->attr.allocatable
14348 || sym->attr.external
14349 || sym->attr.dummy
14350 || sym->attr.pointer
14351 || sym->attr.in_equivalence
14352 || sym->attr.in_common
14353 || sym->attr.data
14354 || sym->module
14355 || sym->attr.cray_pointee
14356 || sym->attr.cray_pointer
14357 || sym->assoc)
14358 return NULL;
14359
14360 /* Get the appropriate init expression. */
14361 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
14362 }
14363
14364 /* Add an initialization expression to a local variable. */
14365 static void
14366 apply_default_init_local (gfc_symbol *sym)
14367 {
14368 gfc_expr *init = NULL;
14369
14370 /* The symbol should be a variable or a function return value. */
14371 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14372 || (sym->attr.function && sym->result != sym))
14373 return;
14374
14375 /* Try to build the initializer expression. If we can't initialize
14376 this symbol, then init will be NULL. */
14377 init = build_default_init_expr (sym);
14378 if (init == NULL)
14379 return;
14380
14381 /* For saved variables, we don't want to add an initializer at function
14382 entry, so we just add a static initializer. Note that automatic variables
14383 are stack allocated even with -fno-automatic; we have also to exclude
14384 result variable, which are also nonstatic. */
14385 if (!sym->attr.automatic
14386 && (sym->attr.save || sym->ns->save_all
14387 || (flag_max_stack_var_size == 0 && !sym->attr.result
14388 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
14389 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
14390 {
14391 /* Don't clobber an existing initializer! */
14392 gcc_assert (sym->value == NULL);
14393 sym->value = init;
14394 return;
14395 }
14396
14397 build_init_assign (sym, init);
14398 }
14399
14400
14401 /* Resolution of common features of flavors variable and procedure. */
14402
14403 static bool
14404 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
14405 {
14406 gfc_array_spec *as;
14407
14408 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14409 && sym->ts.u.derived && CLASS_DATA (sym))
14410 as = CLASS_DATA (sym)->as;
14411 else
14412 as = sym->as;
14413
14414 /* Constraints on deferred shape variable. */
14415 if (as == NULL || as->type != AS_DEFERRED)
14416 {
14417 bool pointer, allocatable, dimension;
14418
14419 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14420 && sym->ts.u.derived && CLASS_DATA (sym))
14421 {
14422 pointer = CLASS_DATA (sym)->attr.class_pointer;
14423 allocatable = CLASS_DATA (sym)->attr.allocatable;
14424 dimension = CLASS_DATA (sym)->attr.dimension;
14425 }
14426 else
14427 {
14428 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
14429 allocatable = sym->attr.allocatable;
14430 dimension = sym->attr.dimension;
14431 }
14432
14433 if (allocatable)
14434 {
14435 if (dimension
14436 && as
14437 && as->type != AS_ASSUMED_RANK
14438 && !sym->attr.select_rank_temporary)
14439 {
14440 gfc_error ("Allocatable array %qs at %L must have a deferred "
14441 "shape or assumed rank", sym->name, &sym->declared_at);
14442 return false;
14443 }
14444 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
14445 "%qs at %L may not be ALLOCATABLE",
14446 sym->name, &sym->declared_at))
14447 return false;
14448 }
14449
14450 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
14451 {
14452 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
14453 "assumed rank", sym->name, &sym->declared_at);
14454 sym->error = 1;
14455 return false;
14456 }
14457 }
14458 else
14459 {
14460 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
14461 && sym->ts.type != BT_CLASS && !sym->assoc)
14462 {
14463 gfc_error ("Array %qs at %L cannot have a deferred shape",
14464 sym->name, &sym->declared_at);
14465 return false;
14466 }
14467 }
14468
14469 /* Constraints on polymorphic variables. */
14470 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
14471 {
14472 /* F03:C502. */
14473 if (sym->attr.class_ok
14474 && sym->ts.u.derived
14475 && !sym->attr.select_type_temporary
14476 && !UNLIMITED_POLY (sym)
14477 && CLASS_DATA (sym)
14478 && CLASS_DATA (sym)->ts.u.derived
14479 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
14480 {
14481 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
14482 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
14483 &sym->declared_at);
14484 return false;
14485 }
14486
14487 /* F03:C509. */
14488 /* Assume that use associated symbols were checked in the module ns.
14489 Class-variables that are associate-names are also something special
14490 and excepted from the test. */
14491 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
14492 && !sym->attr.select_type_temporary
14493 && !sym->attr.select_rank_temporary)
14494 {
14495 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
14496 "or pointer", sym->name, &sym->declared_at);
14497 return false;
14498 }
14499 }
14500
14501 return true;
14502 }
14503
14504
14505 /* Additional checks for symbols with flavor variable and derived
14506 type. To be called from resolve_fl_variable. */
14507
14508 static bool
14509 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
14510 {
14511 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
14512
14513 /* Check to see if a derived type is blocked from being host
14514 associated by the presence of another class I symbol in the same
14515 namespace. 14.6.1.3 of the standard and the discussion on
14516 comp.lang.fortran. */
14517 if (sym->ts.u.derived
14518 && sym->ns != sym->ts.u.derived->ns
14519 && !sym->ts.u.derived->attr.use_assoc
14520 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
14521 {
14522 gfc_symbol *s;
14523 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
14524 if (s && s->attr.generic)
14525 s = gfc_find_dt_in_generic (s);
14526 if (s && !gfc_fl_struct (s->attr.flavor))
14527 {
14528 gfc_error ("The type %qs cannot be host associated at %L "
14529 "because it is blocked by an incompatible object "
14530 "of the same name declared at %L",
14531 sym->ts.u.derived->name, &sym->declared_at,
14532 &s->declared_at);
14533 return false;
14534 }
14535 }
14536
14537 /* 4th constraint in section 11.3: "If an object of a type for which
14538 component-initialization is specified (R429) appears in the
14539 specification-part of a module and does not have the ALLOCATABLE
14540 or POINTER attribute, the object shall have the SAVE attribute."
14541
14542 The check for initializers is performed with
14543 gfc_has_default_initializer because gfc_default_initializer generates
14544 a hidden default for allocatable components. */
14545 if (!(sym->value || no_init_flag) && sym->ns->proc_name
14546 && sym->ns->proc_name->attr.flavor == FL_MODULE
14547 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
14548 && !sym->attr.pointer && !sym->attr.allocatable
14549 && gfc_has_default_initializer (sym->ts.u.derived)
14550 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
14551 "%qs at %L, needed due to the default "
14552 "initialization", sym->name, &sym->declared_at))
14553 return false;
14554
14555 /* Assign default initializer. */
14556 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
14557 && (!no_init_flag
14558 || (sym->attr.intent == INTENT_OUT
14559 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
14560 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14561
14562 return true;
14563 }
14564
14565
14566 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
14567 except in the declaration of an entity or component that has the POINTER
14568 or ALLOCATABLE attribute. */
14569
14570 static bool
14571 deferred_requirements (gfc_symbol *sym)
14572 {
14573 if (sym->ts.deferred
14574 && !(sym->attr.pointer
14575 || sym->attr.allocatable
14576 || sym->attr.associate_var
14577 || sym->attr.omp_udr_artificial_var))
14578 {
14579 /* If a function has a result variable, only check the variable. */
14580 if (sym->result && sym->name != sym->result->name)
14581 return true;
14582
14583 gfc_error ("Entity %qs at %L has a deferred type parameter and "
14584 "requires either the POINTER or ALLOCATABLE attribute",
14585 sym->name, &sym->declared_at);
14586 return false;
14587 }
14588 return true;
14589 }
14590
14591
14592 /* Resolve symbols with flavor variable. */
14593
14594 static bool
14595 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
14596 {
14597 const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
14598 "SAVE attribute");
14599
14600 if (!resolve_fl_var_and_proc (sym, mp_flag))
14601 return false;
14602
14603 /* Set this flag to check that variables are parameters of all entries.
14604 This check is effected by the call to gfc_resolve_expr through
14605 is_non_constant_shape_array. */
14606 bool saved_specification_expr = specification_expr;
14607 gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
14608 specification_expr = true;
14609 specification_expr_symbol = sym;
14610
14611 if (sym->ns->proc_name
14612 && (sym->ns->proc_name->attr.flavor == FL_MODULE
14613 || sym->ns->proc_name->attr.is_main_program)
14614 && !sym->attr.use_assoc
14615 && !sym->attr.allocatable
14616 && !sym->attr.pointer
14617 && is_non_constant_shape_array (sym))
14618 {
14619 /* F08:C541. The shape of an array defined in a main program or module
14620 * needs to be constant. */
14621 gfc_error ("The module or main program array %qs at %L must "
14622 "have constant shape", sym->name, &sym->declared_at);
14623 specification_expr = saved_specification_expr;
14624 specification_expr_symbol = saved_specification_expr_symbol;
14625 return false;
14626 }
14627
14628 /* Constraints on deferred type parameter. */
14629 if (!deferred_requirements (sym))
14630 return false;
14631
14632 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
14633 {
14634 /* Make sure that character string variables with assumed length are
14635 dummy arguments. */
14636 gfc_expr *e = NULL;
14637
14638 if (sym->ts.u.cl)
14639 e = sym->ts.u.cl->length;
14640 else
14641 return false;
14642
14643 if (e == NULL && !sym->attr.dummy && !sym->attr.result
14644 && !sym->ts.deferred && !sym->attr.select_type_temporary
14645 && !sym->attr.omp_udr_artificial_var)
14646 {
14647 gfc_error ("Entity with assumed character length at %L must be a "
14648 "dummy argument or a PARAMETER", &sym->declared_at);
14649 specification_expr = saved_specification_expr;
14650 specification_expr_symbol = saved_specification_expr_symbol;
14651 return false;
14652 }
14653
14654 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
14655 {
14656 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14657 specification_expr = saved_specification_expr;
14658 specification_expr_symbol = saved_specification_expr_symbol;
14659 return false;
14660 }
14661
14662 if (!gfc_is_constant_expr (e)
14663 && !(e->expr_type == EXPR_VARIABLE
14664 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
14665 {
14666 if (!sym->attr.use_assoc && sym->ns->proc_name
14667 && (sym->ns->proc_name->attr.flavor == FL_MODULE
14668 || sym->ns->proc_name->attr.is_main_program))
14669 {
14670 gfc_error ("%qs at %L must have constant character length "
14671 "in this context", sym->name, &sym->declared_at);
14672 specification_expr = saved_specification_expr;
14673 specification_expr_symbol = saved_specification_expr_symbol;
14674 return false;
14675 }
14676 if (sym->attr.in_common)
14677 {
14678 gfc_error ("COMMON variable %qs at %L must have constant "
14679 "character length", sym->name, &sym->declared_at);
14680 specification_expr = saved_specification_expr;
14681 specification_expr_symbol = saved_specification_expr_symbol;
14682 return false;
14683 }
14684 }
14685 }
14686
14687 if (sym->value == NULL && sym->attr.referenced
14688 && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
14689 apply_default_init_local (sym); /* Try to apply a default initialization. */
14690
14691 /* Determine if the symbol may not have an initializer. */
14692 int no_init_flag = 0, automatic_flag = 0;
14693 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
14694 || sym->attr.intrinsic || sym->attr.result)
14695 no_init_flag = 1;
14696 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
14697 && is_non_constant_shape_array (sym))
14698 {
14699 no_init_flag = automatic_flag = 1;
14700
14701 /* Also, they must not have the SAVE attribute.
14702 SAVE_IMPLICIT is checked below. */
14703 if (sym->as && sym->attr.codimension)
14704 {
14705 int corank = sym->as->corank;
14706 sym->as->corank = 0;
14707 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
14708 sym->as->corank = corank;
14709 }
14710 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
14711 {
14712 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14713 specification_expr = saved_specification_expr;
14714 specification_expr_symbol = saved_specification_expr_symbol;
14715 return false;
14716 }
14717 }
14718
14719 /* Ensure that any initializer is simplified. */
14720 if (sym->value)
14721 gfc_simplify_expr (sym->value, 1);
14722
14723 /* Reject illegal initializers. */
14724 if (!sym->mark && sym->value)
14725 {
14726 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
14727 && CLASS_DATA (sym)->attr.allocatable))
14728 gfc_error ("Allocatable %qs at %L cannot have an initializer",
14729 sym->name, &sym->declared_at);
14730 else if (sym->attr.external)
14731 gfc_error ("External %qs at %L cannot have an initializer",
14732 sym->name, &sym->declared_at);
14733 else if (sym->attr.dummy)
14734 gfc_error ("Dummy %qs at %L cannot have an initializer",
14735 sym->name, &sym->declared_at);
14736 else if (sym->attr.intrinsic)
14737 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
14738 sym->name, &sym->declared_at);
14739 else if (sym->attr.result)
14740 gfc_error ("Function result %qs at %L cannot have an initializer",
14741 sym->name, &sym->declared_at);
14742 else if (automatic_flag)
14743 gfc_error ("Automatic array %qs at %L cannot have an initializer",
14744 sym->name, &sym->declared_at);
14745 else
14746 goto no_init_error;
14747 specification_expr = saved_specification_expr;
14748 specification_expr_symbol = saved_specification_expr_symbol;
14749 return false;
14750 }
14751
14752 no_init_error:
14753 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
14754 {
14755 bool res = resolve_fl_variable_derived (sym, no_init_flag);
14756 specification_expr = saved_specification_expr;
14757 specification_expr_symbol = saved_specification_expr_symbol;
14758 return res;
14759 }
14760
14761 specification_expr = saved_specification_expr;
14762 specification_expr_symbol = saved_specification_expr_symbol;
14763 return true;
14764 }
14765
14766
14767 /* Compare the dummy characteristics of a module procedure interface
14768 declaration with the corresponding declaration in a submodule. */
14769 static gfc_formal_arglist *new_formal;
14770 static char errmsg[200];
14771
14772 static void
14773 compare_fsyms (gfc_symbol *sym)
14774 {
14775 gfc_symbol *fsym;
14776
14777 if (sym == NULL || new_formal == NULL)
14778 return;
14779
14780 fsym = new_formal->sym;
14781
14782 if (sym == fsym)
14783 return;
14784
14785 if (strcmp (sym->name, fsym->name) == 0)
14786 {
14787 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
14788 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
14789 }
14790 }
14791
14792
14793 /* Resolve a procedure. */
14794
14795 static bool
14796 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
14797 {
14798 gfc_formal_arglist *arg;
14799 bool allocatable_or_pointer = false;
14800
14801 if (sym->attr.function
14802 && !resolve_fl_var_and_proc (sym, mp_flag))
14803 return false;
14804
14805 /* Constraints on deferred type parameter. */
14806 if (!deferred_requirements (sym))
14807 return false;
14808
14809 if (sym->ts.type == BT_CHARACTER)
14810 {
14811 gfc_charlen *cl = sym->ts.u.cl;
14812
14813 if (cl && cl->length && gfc_is_constant_expr (cl->length)
14814 && !resolve_charlen (cl))
14815 return false;
14816
14817 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
14818 && sym->attr.proc == PROC_ST_FUNCTION)
14819 {
14820 gfc_error ("Character-valued statement function %qs at %L must "
14821 "have constant length", sym->name, &sym->declared_at);
14822 return false;
14823 }
14824 }
14825
14826 /* Ensure that derived type for are not of a private type. Internal
14827 module procedures are excluded by 2.2.3.3 - i.e., they are not
14828 externally accessible and can access all the objects accessible in
14829 the host. */
14830 if (!(sym->ns->parent && sym->ns->parent->proc_name
14831 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14832 && gfc_check_symbol_access (sym))
14833 {
14834 gfc_interface *iface;
14835
14836 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
14837 {
14838 if (arg->sym
14839 && arg->sym->ts.type == BT_DERIVED
14840 && arg->sym->ts.u.derived
14841 && !arg->sym->ts.u.derived->attr.use_assoc
14842 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14843 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
14844 "and cannot be a dummy argument"
14845 " of %qs, which is PUBLIC at %L",
14846 arg->sym->name, sym->name,
14847 &sym->declared_at))
14848 {
14849 /* Stop this message from recurring. */
14850 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14851 return false;
14852 }
14853 }
14854
14855 /* PUBLIC interfaces may expose PRIVATE procedures that take types
14856 PRIVATE to the containing module. */
14857 for (iface = sym->generic; iface; iface = iface->next)
14858 {
14859 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
14860 {
14861 if (arg->sym
14862 && arg->sym->ts.type == BT_DERIVED
14863 && !arg->sym->ts.u.derived->attr.use_assoc
14864 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14865 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
14866 "PUBLIC interface %qs at %L "
14867 "takes dummy arguments of %qs which "
14868 "is PRIVATE", iface->sym->name,
14869 sym->name, &iface->sym->declared_at,
14870 gfc_typename(&arg->sym->ts)))
14871 {
14872 /* Stop this message from recurring. */
14873 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14874 return false;
14875 }
14876 }
14877 }
14878 }
14879
14880 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
14881 && !sym->attr.proc_pointer)
14882 {
14883 gfc_error ("Function %qs at %L cannot have an initializer",
14884 sym->name, &sym->declared_at);
14885
14886 /* Make sure no second error is issued for this. */
14887 sym->value->error = 1;
14888 return false;
14889 }
14890
14891 /* An external symbol may not have an initializer because it is taken to be
14892 a procedure. Exception: Procedure Pointers. */
14893 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
14894 {
14895 gfc_error ("External object %qs at %L may not have an initializer",
14896 sym->name, &sym->declared_at);
14897 return false;
14898 }
14899
14900 /* An elemental function is required to return a scalar 12.7.1 */
14901 if (sym->attr.elemental && sym->attr.function
14902 && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14903 && CLASS_DATA (sym)->as)))
14904 {
14905 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
14906 "result", sym->name, &sym->declared_at);
14907 /* Reset so that the error only occurs once. */
14908 sym->attr.elemental = 0;
14909 return false;
14910 }
14911
14912 if (sym->attr.proc == PROC_ST_FUNCTION
14913 && (sym->attr.allocatable || sym->attr.pointer))
14914 {
14915 gfc_error ("Statement function %qs at %L may not have pointer or "
14916 "allocatable attribute", sym->name, &sym->declared_at);
14917 return false;
14918 }
14919
14920 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
14921 char-len-param shall not be array-valued, pointer-valued, recursive
14922 or pure. ....snip... A character value of * may only be used in the
14923 following ways: (i) Dummy arg of procedure - dummy associates with
14924 actual length; (ii) To declare a named constant; or (iii) External
14925 function - but length must be declared in calling scoping unit. */
14926 if (sym->attr.function
14927 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
14928 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
14929 {
14930 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
14931 || (sym->attr.recursive) || (sym->attr.pure))
14932 {
14933 if (sym->as && sym->as->rank)
14934 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14935 "array-valued", sym->name, &sym->declared_at);
14936
14937 if (sym->attr.pointer)
14938 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14939 "pointer-valued", sym->name, &sym->declared_at);
14940
14941 if (sym->attr.pure)
14942 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14943 "pure", sym->name, &sym->declared_at);
14944
14945 if (sym->attr.recursive)
14946 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14947 "recursive", sym->name, &sym->declared_at);
14948
14949 return false;
14950 }
14951
14952 /* Appendix B.2 of the standard. Contained functions give an
14953 error anyway. Deferred character length is an F2003 feature.
14954 Don't warn on intrinsic conversion functions, which start
14955 with two underscores. */
14956 if (!sym->attr.contained && !sym->ts.deferred
14957 && (sym->name[0] != '_' || sym->name[1] != '_'))
14958 gfc_notify_std (GFC_STD_F95_OBS,
14959 "CHARACTER(*) function %qs at %L",
14960 sym->name, &sym->declared_at);
14961 }
14962
14963 /* F2008, C1218. */
14964 if (sym->attr.elemental)
14965 {
14966 if (sym->attr.proc_pointer)
14967 {
14968 const char* name = (sym->attr.result ? sym->ns->proc_name->name
14969 : sym->name);
14970 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
14971 name, &sym->declared_at);
14972 return false;
14973 }
14974 if (sym->attr.dummy)
14975 {
14976 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
14977 sym->name, &sym->declared_at);
14978 return false;
14979 }
14980 }
14981
14982 /* F2018, C15100: "The result of an elemental function shall be scalar,
14983 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
14984 pointer is tested and caught elsewhere. */
14985 if (sym->result)
14986 allocatable_or_pointer = sym->result->ts.type == BT_CLASS
14987 && CLASS_DATA (sym->result) ?
14988 (CLASS_DATA (sym->result)->attr.allocatable
14989 || CLASS_DATA (sym->result)->attr.pointer) :
14990 (sym->result->attr.allocatable
14991 || sym->result->attr.pointer);
14992
14993 if (sym->attr.elemental && sym->result
14994 && allocatable_or_pointer)
14995 {
14996 gfc_error ("Function result variable %qs at %L of elemental "
14997 "function %qs shall not have an ALLOCATABLE or POINTER "
14998 "attribute", sym->result->name,
14999 &sym->result->declared_at, sym->name);
15000 return false;
15001 }
15002
15003 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
15004 {
15005 gfc_formal_arglist *curr_arg;
15006 int has_non_interop_arg = 0;
15007
15008 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15009 sym->common_block))
15010 {
15011 /* Clear these to prevent looking at them again if there was an
15012 error. */
15013 sym->attr.is_bind_c = 0;
15014 sym->attr.is_c_interop = 0;
15015 sym->ts.is_c_interop = 0;
15016 }
15017 else
15018 {
15019 /* So far, no errors have been found. */
15020 sym->attr.is_c_interop = 1;
15021 sym->ts.is_c_interop = 1;
15022 }
15023
15024 curr_arg = gfc_sym_get_dummy_args (sym);
15025 while (curr_arg != NULL)
15026 {
15027 /* Skip implicitly typed dummy args here. */
15028 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
15029 if (!gfc_verify_c_interop_param (curr_arg->sym))
15030 /* If something is found to fail, record the fact so we
15031 can mark the symbol for the procedure as not being
15032 BIND(C) to try and prevent multiple errors being
15033 reported. */
15034 has_non_interop_arg = 1;
15035
15036 curr_arg = curr_arg->next;
15037 }
15038
15039 /* See if any of the arguments were not interoperable and if so, clear
15040 the procedure symbol to prevent duplicate error messages. */
15041 if (has_non_interop_arg != 0)
15042 {
15043 sym->attr.is_c_interop = 0;
15044 sym->ts.is_c_interop = 0;
15045 sym->attr.is_bind_c = 0;
15046 }
15047 }
15048
15049 if (!sym->attr.proc_pointer)
15050 {
15051 if (sym->attr.save == SAVE_EXPLICIT)
15052 {
15053 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
15054 "in %qs at %L", sym->name, &sym->declared_at);
15055 return false;
15056 }
15057 if (sym->attr.intent)
15058 {
15059 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
15060 "in %qs at %L", sym->name, &sym->declared_at);
15061 return false;
15062 }
15063 if (sym->attr.subroutine && sym->attr.result)
15064 {
15065 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
15066 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
15067 return false;
15068 }
15069 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
15070 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
15071 || sym->attr.contained))
15072 {
15073 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
15074 "in %qs at %L", sym->name, &sym->declared_at);
15075 return false;
15076 }
15077 if (strcmp ("ppr@", sym->name) == 0)
15078 {
15079 gfc_error ("Procedure pointer result %qs at %L "
15080 "is missing the pointer attribute",
15081 sym->ns->proc_name->name, &sym->declared_at);
15082 return false;
15083 }
15084 }
15085
15086 /* Assume that a procedure whose body is not known has references
15087 to external arrays. */
15088 if (sym->attr.if_source != IFSRC_DECL)
15089 sym->attr.array_outer_dependency = 1;
15090
15091 /* Compare the characteristics of a module procedure with the
15092 interface declaration. Ideally this would be done with
15093 gfc_compare_interfaces but, at present, the formal interface
15094 cannot be copied to the ts.interface. */
15095 if (sym->attr.module_procedure
15096 && sym->attr.if_source == IFSRC_DECL)
15097 {
15098 gfc_symbol *iface;
15099 char name[2*GFC_MAX_SYMBOL_LEN + 1];
15100 char *module_name;
15101 char *submodule_name;
15102 strcpy (name, sym->ns->proc_name->name);
15103 module_name = strtok (name, ".");
15104 submodule_name = strtok (NULL, ".");
15105
15106 iface = sym->tlink;
15107 sym->tlink = NULL;
15108
15109 /* Make sure that the result uses the correct charlen for deferred
15110 length results. */
15111 if (iface && sym->result
15112 && iface->ts.type == BT_CHARACTER
15113 && iface->ts.deferred)
15114 sym->result->ts.u.cl = iface->ts.u.cl;
15115
15116 if (iface == NULL)
15117 goto check_formal;
15118
15119 /* Check the procedure characteristics. */
15120 if (sym->attr.elemental != iface->attr.elemental)
15121 {
15122 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
15123 "PROCEDURE at %L and its interface in %s",
15124 &sym->declared_at, module_name);
15125 return false;
15126 }
15127
15128 if (sym->attr.pure != iface->attr.pure)
15129 {
15130 gfc_error ("Mismatch in PURE attribute between MODULE "
15131 "PROCEDURE at %L and its interface in %s",
15132 &sym->declared_at, module_name);
15133 return false;
15134 }
15135
15136 if (sym->attr.recursive != iface->attr.recursive)
15137 {
15138 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
15139 "PROCEDURE at %L and its interface in %s",
15140 &sym->declared_at, module_name);
15141 return false;
15142 }
15143
15144 /* Check the result characteristics. */
15145 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
15146 {
15147 gfc_error ("%s between the MODULE PROCEDURE declaration "
15148 "in MODULE %qs and the declaration at %L in "
15149 "(SUB)MODULE %qs",
15150 errmsg, module_name, &sym->declared_at,
15151 submodule_name ? submodule_name : module_name);
15152 return false;
15153 }
15154
15155 check_formal:
15156 /* Check the characteristics of the formal arguments. */
15157 if (sym->formal && sym->formal_ns)
15158 {
15159 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
15160 {
15161 new_formal = arg;
15162 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
15163 }
15164 }
15165 }
15166
15167 /* F2018:15.4.2.2 requires an explicit interface for procedures with the
15168 BIND(C) attribute. */
15169 if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
15170 {
15171 gfc_error ("Interface of %qs at %L must be explicit",
15172 sym->name, &sym->declared_at);
15173 return false;
15174 }
15175
15176 return true;
15177 }
15178
15179
15180 /* Resolve a list of finalizer procedures. That is, after they have hopefully
15181 been defined and we now know their defined arguments, check that they fulfill
15182 the requirements of the standard for procedures used as finalizers. */
15183
15184 static bool
15185 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
15186 {
15187 gfc_finalizer* list;
15188 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
15189 bool result = true;
15190 bool seen_scalar = false;
15191 gfc_symbol *vtab;
15192 gfc_component *c;
15193 gfc_symbol *parent = gfc_get_derived_super_type (derived);
15194
15195 if (parent)
15196 gfc_resolve_finalizers (parent, finalizable);
15197
15198 /* Ensure that derived-type components have a their finalizers resolved. */
15199 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
15200 for (c = derived->components; c; c = c->next)
15201 if (c->ts.type == BT_DERIVED
15202 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
15203 {
15204 bool has_final2 = false;
15205 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
15206 return false; /* Error. */
15207 has_final = has_final || has_final2;
15208 }
15209 /* Return early if not finalizable. */
15210 if (!has_final)
15211 {
15212 if (finalizable)
15213 *finalizable = false;
15214 return true;
15215 }
15216
15217 /* Walk over the list of finalizer-procedures, check them, and if any one
15218 does not fit in with the standard's definition, print an error and remove
15219 it from the list. */
15220 prev_link = &derived->f2k_derived->finalizers;
15221 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
15222 {
15223 gfc_formal_arglist *dummy_args;
15224 gfc_symbol* arg;
15225 gfc_finalizer* i;
15226 int my_rank;
15227
15228 /* Skip this finalizer if we already resolved it. */
15229 if (list->proc_tree)
15230 {
15231 if (list->proc_tree->n.sym->formal->sym->as == NULL
15232 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
15233 seen_scalar = true;
15234 prev_link = &(list->next);
15235 continue;
15236 }
15237
15238 /* Check this exists and is a SUBROUTINE. */
15239 if (!list->proc_sym->attr.subroutine)
15240 {
15241 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
15242 list->proc_sym->name, &list->where);
15243 goto error;
15244 }
15245
15246 /* We should have exactly one argument. */
15247 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
15248 if (!dummy_args || dummy_args->next)
15249 {
15250 gfc_error ("FINAL procedure at %L must have exactly one argument",
15251 &list->where);
15252 goto error;
15253 }
15254 arg = dummy_args->sym;
15255
15256 if (!arg)
15257 {
15258 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15259 &list->proc_sym->declared_at, derived->name);
15260 goto error;
15261 }
15262
15263 if (arg->as && arg->as->type == AS_ASSUMED_RANK
15264 && ((list != derived->f2k_derived->finalizers) || list->next))
15265 {
15266 gfc_error ("FINAL procedure at %L with assumed rank argument must "
15267 "be the only finalizer with the same kind/type "
15268 "(F2018: C790)", &list->where);
15269 goto error;
15270 }
15271
15272 /* This argument must be of our type. */
15273 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
15274 {
15275 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15276 &arg->declared_at, derived->name);
15277 goto error;
15278 }
15279
15280 /* It must neither be a pointer nor allocatable nor optional. */
15281 if (arg->attr.pointer)
15282 {
15283 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
15284 &arg->declared_at);
15285 goto error;
15286 }
15287 if (arg->attr.allocatable)
15288 {
15289 gfc_error ("Argument of FINAL procedure at %L must not be"
15290 " ALLOCATABLE", &arg->declared_at);
15291 goto error;
15292 }
15293 if (arg->attr.optional)
15294 {
15295 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
15296 &arg->declared_at);
15297 goto error;
15298 }
15299
15300 /* It must not be INTENT(OUT). */
15301 if (arg->attr.intent == INTENT_OUT)
15302 {
15303 gfc_error ("Argument of FINAL procedure at %L must not be"
15304 " INTENT(OUT)", &arg->declared_at);
15305 goto error;
15306 }
15307
15308 /* Warn if the procedure is non-scalar and not assumed shape. */
15309 if (warn_surprising && arg->as && arg->as->rank != 0
15310 && arg->as->type != AS_ASSUMED_SHAPE)
15311 gfc_warning (OPT_Wsurprising,
15312 "Non-scalar FINAL procedure at %L should have assumed"
15313 " shape argument", &arg->declared_at);
15314
15315 /* Check that it does not match in kind and rank with a FINAL procedure
15316 defined earlier. To really loop over the *earlier* declarations,
15317 we need to walk the tail of the list as new ones were pushed at the
15318 front. */
15319 /* TODO: Handle kind parameters once they are implemented. */
15320 my_rank = (arg->as ? arg->as->rank : 0);
15321 for (i = list->next; i; i = i->next)
15322 {
15323 gfc_formal_arglist *dummy_args;
15324
15325 /* Argument list might be empty; that is an error signalled earlier,
15326 but we nevertheless continued resolving. */
15327 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
15328 if (dummy_args)
15329 {
15330 gfc_symbol* i_arg = dummy_args->sym;
15331 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
15332 if (i_rank == my_rank)
15333 {
15334 gfc_error ("FINAL procedure %qs declared at %L has the same"
15335 " rank (%d) as %qs",
15336 list->proc_sym->name, &list->where, my_rank,
15337 i->proc_sym->name);
15338 goto error;
15339 }
15340 }
15341 }
15342
15343 /* Is this the/a scalar finalizer procedure? */
15344 if (my_rank == 0)
15345 seen_scalar = true;
15346
15347 /* Find the symtree for this procedure. */
15348 gcc_assert (!list->proc_tree);
15349 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
15350
15351 prev_link = &list->next;
15352 continue;
15353
15354 /* Remove wrong nodes immediately from the list so we don't risk any
15355 troubles in the future when they might fail later expectations. */
15356 error:
15357 i = list;
15358 *prev_link = list->next;
15359 gfc_free_finalizer (i);
15360 result = false;
15361 }
15362
15363 if (result == false)
15364 return false;
15365
15366 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
15367 were nodes in the list, must have been for arrays. It is surely a good
15368 idea to have a scalar version there if there's something to finalize. */
15369 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
15370 gfc_warning (OPT_Wsurprising,
15371 "Only array FINAL procedures declared for derived type %qs"
15372 " defined at %L, suggest also scalar one unless an assumed"
15373 " rank finalizer has been declared",
15374 derived->name, &derived->declared_at);
15375
15376 vtab = gfc_find_derived_vtab (derived);
15377 c = vtab->ts.u.derived->components->next->next->next->next->next;
15378 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
15379
15380 if (finalizable)
15381 *finalizable = true;
15382
15383 return true;
15384 }
15385
15386
15387 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
15388
15389 static bool
15390 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
15391 const char* generic_name, locus where)
15392 {
15393 gfc_symbol *sym1, *sym2;
15394 const char *pass1, *pass2;
15395 gfc_formal_arglist *dummy_args;
15396
15397 gcc_assert (t1->specific && t2->specific);
15398 gcc_assert (!t1->specific->is_generic);
15399 gcc_assert (!t2->specific->is_generic);
15400 gcc_assert (t1->is_operator == t2->is_operator);
15401
15402 sym1 = t1->specific->u.specific->n.sym;
15403 sym2 = t2->specific->u.specific->n.sym;
15404
15405 if (sym1 == sym2)
15406 return true;
15407
15408 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
15409 if (sym1->attr.subroutine != sym2->attr.subroutine
15410 || sym1->attr.function != sym2->attr.function)
15411 {
15412 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
15413 " GENERIC %qs at %L",
15414 sym1->name, sym2->name, generic_name, &where);
15415 return false;
15416 }
15417
15418 /* Determine PASS arguments. */
15419 if (t1->specific->nopass)
15420 pass1 = NULL;
15421 else if (t1->specific->pass_arg)
15422 pass1 = t1->specific->pass_arg;
15423 else
15424 {
15425 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
15426 if (dummy_args)
15427 pass1 = dummy_args->sym->name;
15428 else
15429 pass1 = NULL;
15430 }
15431 if (t2->specific->nopass)
15432 pass2 = NULL;
15433 else if (t2->specific->pass_arg)
15434 pass2 = t2->specific->pass_arg;
15435 else
15436 {
15437 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
15438 if (dummy_args)
15439 pass2 = dummy_args->sym->name;
15440 else
15441 pass2 = NULL;
15442 }
15443
15444 /* Compare the interfaces. */
15445 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
15446 NULL, 0, pass1, pass2))
15447 {
15448 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
15449 sym1->name, sym2->name, generic_name, &where);
15450 return false;
15451 }
15452
15453 return true;
15454 }
15455
15456
15457 /* Worker function for resolving a generic procedure binding; this is used to
15458 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
15459
15460 The difference between those cases is finding possible inherited bindings
15461 that are overridden, as one has to look for them in tb_sym_root,
15462 tb_uop_root or tb_op, respectively. Thus the caller must already find
15463 the super-type and set p->overridden correctly. */
15464
15465 static bool
15466 resolve_tb_generic_targets (gfc_symbol* super_type,
15467 gfc_typebound_proc* p, const char* name)
15468 {
15469 gfc_tbp_generic* target;
15470 gfc_symtree* first_target;
15471 gfc_symtree* inherited;
15472
15473 gcc_assert (p && p->is_generic);
15474
15475 /* Try to find the specific bindings for the symtrees in our target-list. */
15476 gcc_assert (p->u.generic);
15477 for (target = p->u.generic; target; target = target->next)
15478 if (!target->specific)
15479 {
15480 gfc_typebound_proc* overridden_tbp;
15481 gfc_tbp_generic* g;
15482 const char* target_name;
15483
15484 target_name = target->specific_st->name;
15485
15486 /* Defined for this type directly. */
15487 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
15488 {
15489 target->specific = target->specific_st->n.tb;
15490 goto specific_found;
15491 }
15492
15493 /* Look for an inherited specific binding. */
15494 if (super_type)
15495 {
15496 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
15497 true, NULL);
15498
15499 if (inherited)
15500 {
15501 gcc_assert (inherited->n.tb);
15502 target->specific = inherited->n.tb;
15503 goto specific_found;
15504 }
15505 }
15506
15507 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
15508 " at %L", target_name, name, &p->where);
15509 return false;
15510
15511 /* Once we've found the specific binding, check it is not ambiguous with
15512 other specifics already found or inherited for the same GENERIC. */
15513 specific_found:
15514 gcc_assert (target->specific);
15515
15516 /* This must really be a specific binding! */
15517 if (target->specific->is_generic)
15518 {
15519 gfc_error ("GENERIC %qs at %L must target a specific binding,"
15520 " %qs is GENERIC, too", name, &p->where, target_name);
15521 return false;
15522 }
15523
15524 /* Check those already resolved on this type directly. */
15525 for (g = p->u.generic; g; g = g->next)
15526 if (g != target && g->specific
15527 && !check_generic_tbp_ambiguity (target, g, name, p->where))
15528 return false;
15529
15530 /* Check for ambiguity with inherited specific targets. */
15531 for (overridden_tbp = p->overridden; overridden_tbp;
15532 overridden_tbp = overridden_tbp->overridden)
15533 if (overridden_tbp->is_generic)
15534 {
15535 for (g = overridden_tbp->u.generic; g; g = g->next)
15536 {
15537 gcc_assert (g->specific);
15538 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
15539 return false;
15540 }
15541 }
15542 }
15543
15544 /* If we attempt to "overwrite" a specific binding, this is an error. */
15545 if (p->overridden && !p->overridden->is_generic)
15546 {
15547 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
15548 " the same name", name, &p->where);
15549 return false;
15550 }
15551
15552 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
15553 all must have the same attributes here. */
15554 first_target = p->u.generic->specific->u.specific;
15555 gcc_assert (first_target);
15556 p->subroutine = first_target->n.sym->attr.subroutine;
15557 p->function = first_target->n.sym->attr.function;
15558
15559 return true;
15560 }
15561
15562
15563 /* Resolve a GENERIC procedure binding for a derived type. */
15564
15565 static bool
15566 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
15567 {
15568 gfc_symbol* super_type;
15569
15570 /* Find the overridden binding if any. */
15571 st->n.tb->overridden = NULL;
15572 super_type = gfc_get_derived_super_type (derived);
15573 if (super_type)
15574 {
15575 gfc_symtree* overridden;
15576 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
15577 true, NULL);
15578
15579 if (overridden && overridden->n.tb)
15580 st->n.tb->overridden = overridden->n.tb;
15581 }
15582
15583 /* Resolve using worker function. */
15584 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
15585 }
15586
15587
15588 /* Retrieve the target-procedure of an operator binding and do some checks in
15589 common for intrinsic and user-defined type-bound operators. */
15590
15591 static gfc_symbol*
15592 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
15593 {
15594 gfc_symbol* target_proc;
15595
15596 gcc_assert (target->specific && !target->specific->is_generic);
15597 target_proc = target->specific->u.specific->n.sym;
15598 gcc_assert (target_proc);
15599
15600 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
15601 if (target->specific->nopass)
15602 {
15603 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
15604 return NULL;
15605 }
15606
15607 return target_proc;
15608 }
15609
15610
15611 /* Resolve a type-bound intrinsic operator. */
15612
15613 static bool
15614 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
15615 gfc_typebound_proc* p)
15616 {
15617 gfc_symbol* super_type;
15618 gfc_tbp_generic* target;
15619
15620 /* If there's already an error here, do nothing (but don't fail again). */
15621 if (p->error)
15622 return true;
15623
15624 /* Operators should always be GENERIC bindings. */
15625 gcc_assert (p->is_generic);
15626
15627 /* Look for an overridden binding. */
15628 super_type = gfc_get_derived_super_type (derived);
15629 if (super_type && super_type->f2k_derived)
15630 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
15631 op, true, NULL);
15632 else
15633 p->overridden = NULL;
15634
15635 /* Resolve general GENERIC properties using worker function. */
15636 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
15637 goto error;
15638
15639 /* Check the targets to be procedures of correct interface. */
15640 for (target = p->u.generic; target; target = target->next)
15641 {
15642 gfc_symbol* target_proc;
15643
15644 target_proc = get_checked_tb_operator_target (target, p->where);
15645 if (!target_proc)
15646 goto error;
15647
15648 if (!gfc_check_operator_interface (target_proc, op, p->where))
15649 goto error;
15650
15651 /* Add target to non-typebound operator list. */
15652 if (!target->specific->deferred && !derived->attr.use_assoc
15653 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
15654 {
15655 gfc_interface *head, *intr;
15656
15657 /* Preempt 'gfc_check_new_interface' for submodules, where the
15658 mechanism for handling module procedures winds up resolving
15659 operator interfaces twice and would otherwise cause an error. */
15660 for (intr = derived->ns->op[op]; intr; intr = intr->next)
15661 if (intr->sym == target_proc
15662 && target_proc->attr.used_in_submodule)
15663 return true;
15664
15665 if (!gfc_check_new_interface (derived->ns->op[op],
15666 target_proc, p->where))
15667 return false;
15668 head = derived->ns->op[op];
15669 intr = gfc_get_interface ();
15670 intr->sym = target_proc;
15671 intr->where = p->where;
15672 intr->next = head;
15673 derived->ns->op[op] = intr;
15674 }
15675 }
15676
15677 return true;
15678
15679 error:
15680 p->error = 1;
15681 return false;
15682 }
15683
15684
15685 /* Resolve a type-bound user operator (tree-walker callback). */
15686
15687 static gfc_symbol* resolve_bindings_derived;
15688 static bool resolve_bindings_result;
15689
15690 static bool check_uop_procedure (gfc_symbol* sym, locus where);
15691
15692 static void
15693 resolve_typebound_user_op (gfc_symtree* stree)
15694 {
15695 gfc_symbol* super_type;
15696 gfc_tbp_generic* target;
15697
15698 gcc_assert (stree && stree->n.tb);
15699
15700 if (stree->n.tb->error)
15701 return;
15702
15703 /* Operators should always be GENERIC bindings. */
15704 gcc_assert (stree->n.tb->is_generic);
15705
15706 /* Find overridden procedure, if any. */
15707 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15708 if (super_type && super_type->f2k_derived)
15709 {
15710 gfc_symtree* overridden;
15711 overridden = gfc_find_typebound_user_op (super_type, NULL,
15712 stree->name, true, NULL);
15713
15714 if (overridden && overridden->n.tb)
15715 stree->n.tb->overridden = overridden->n.tb;
15716 }
15717 else
15718 stree->n.tb->overridden = NULL;
15719
15720 /* Resolve basically using worker function. */
15721 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
15722 goto error;
15723
15724 /* Check the targets to be functions of correct interface. */
15725 for (target = stree->n.tb->u.generic; target; target = target->next)
15726 {
15727 gfc_symbol* target_proc;
15728
15729 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
15730 if (!target_proc)
15731 goto error;
15732
15733 if (!check_uop_procedure (target_proc, stree->n.tb->where))
15734 goto error;
15735 }
15736
15737 return;
15738
15739 error:
15740 resolve_bindings_result = false;
15741 stree->n.tb->error = 1;
15742 }
15743
15744
15745 /* Resolve the type-bound procedures for a derived type. */
15746
15747 static void
15748 resolve_typebound_procedure (gfc_symtree* stree)
15749 {
15750 gfc_symbol* proc;
15751 locus where;
15752 gfc_symbol* me_arg;
15753 gfc_symbol* super_type;
15754 gfc_component* comp;
15755
15756 gcc_assert (stree);
15757
15758 /* Undefined specific symbol from GENERIC target definition. */
15759 if (!stree->n.tb)
15760 return;
15761
15762 if (stree->n.tb->error)
15763 return;
15764
15765 /* If this is a GENERIC binding, use that routine. */
15766 if (stree->n.tb->is_generic)
15767 {
15768 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
15769 goto error;
15770 return;
15771 }
15772
15773 /* Get the target-procedure to check it. */
15774 gcc_assert (!stree->n.tb->is_generic);
15775 gcc_assert (stree->n.tb->u.specific);
15776 proc = stree->n.tb->u.specific->n.sym;
15777 where = stree->n.tb->where;
15778
15779 /* Default access should already be resolved from the parser. */
15780 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
15781
15782 if (stree->n.tb->deferred)
15783 {
15784 if (!check_proc_interface (proc, &where))
15785 goto error;
15786 }
15787 else
15788 {
15789 /* If proc has not been resolved at this point, proc->name may
15790 actually be a USE associated entity. See PR fortran/89647. */
15791 if (!proc->resolve_symbol_called
15792 && proc->attr.function == 0 && proc->attr.subroutine == 0)
15793 {
15794 gfc_symbol *tmp;
15795 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
15796 if (tmp && tmp->attr.use_assoc)
15797 {
15798 proc->module = tmp->module;
15799 proc->attr.proc = tmp->attr.proc;
15800 proc->attr.function = tmp->attr.function;
15801 proc->attr.subroutine = tmp->attr.subroutine;
15802 proc->attr.use_assoc = tmp->attr.use_assoc;
15803 proc->ts = tmp->ts;
15804 proc->result = tmp->result;
15805 }
15806 }
15807
15808 /* Check for F08:C465. */
15809 if ((!proc->attr.subroutine && !proc->attr.function)
15810 || (proc->attr.proc != PROC_MODULE
15811 && proc->attr.if_source != IFSRC_IFBODY
15812 && !proc->attr.module_procedure)
15813 || proc->attr.abstract)
15814 {
15815 gfc_error ("%qs must be a module procedure or an external "
15816 "procedure with an explicit interface at %L",
15817 proc->name, &where);
15818 goto error;
15819 }
15820 }
15821
15822 stree->n.tb->subroutine = proc->attr.subroutine;
15823 stree->n.tb->function = proc->attr.function;
15824
15825 /* Find the super-type of the current derived type. We could do this once and
15826 store in a global if speed is needed, but as long as not I believe this is
15827 more readable and clearer. */
15828 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15829
15830 /* If PASS, resolve and check arguments if not already resolved / loaded
15831 from a .mod file. */
15832 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
15833 {
15834 gfc_formal_arglist *dummy_args;
15835
15836 dummy_args = gfc_sym_get_dummy_args (proc);
15837 if (stree->n.tb->pass_arg)
15838 {
15839 gfc_formal_arglist *i;
15840
15841 /* If an explicit passing argument name is given, walk the arg-list
15842 and look for it. */
15843
15844 me_arg = NULL;
15845 stree->n.tb->pass_arg_num = 1;
15846 for (i = dummy_args; i; i = i->next)
15847 {
15848 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
15849 {
15850 me_arg = i->sym;
15851 break;
15852 }
15853 ++stree->n.tb->pass_arg_num;
15854 }
15855
15856 if (!me_arg)
15857 {
15858 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
15859 " argument %qs",
15860 proc->name, stree->n.tb->pass_arg, &where,
15861 stree->n.tb->pass_arg);
15862 goto error;
15863 }
15864 }
15865 else
15866 {
15867 /* Otherwise, take the first one; there should in fact be at least
15868 one. */
15869 stree->n.tb->pass_arg_num = 1;
15870 if (!dummy_args)
15871 {
15872 gfc_error ("Procedure %qs with PASS at %L must have at"
15873 " least one argument", proc->name, &where);
15874 goto error;
15875 }
15876 me_arg = dummy_args->sym;
15877 }
15878
15879 /* Now check that the argument-type matches and the passed-object
15880 dummy argument is generally fine. */
15881
15882 gcc_assert (me_arg);
15883
15884 if (me_arg->ts.type != BT_CLASS)
15885 {
15886 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
15887 " at %L", proc->name, &where);
15888 goto error;
15889 }
15890
15891 /* The derived type is not a PDT template. Resolve as usual. */
15892 if (!resolve_bindings_derived->attr.pdt_template
15893 && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
15894 {
15895 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15896 "the derived-type %qs", me_arg->name, proc->name,
15897 me_arg->name, &where, resolve_bindings_derived->name);
15898 goto error;
15899 }
15900
15901 if (resolve_bindings_derived->attr.pdt_template
15902 && !gfc_pdt_is_instance_of (resolve_bindings_derived,
15903 CLASS_DATA (me_arg)->ts.u.derived))
15904 {
15905 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15906 "the parametric derived-type %qs", me_arg->name,
15907 proc->name, me_arg->name, &where,
15908 resolve_bindings_derived->name);
15909 goto error;
15910 }
15911
15912 if (resolve_bindings_derived->attr.pdt_template
15913 && gfc_pdt_is_instance_of (resolve_bindings_derived,
15914 CLASS_DATA (me_arg)->ts.u.derived)
15915 && (me_arg->param_list != NULL)
15916 && (gfc_spec_list_type (me_arg->param_list,
15917 CLASS_DATA(me_arg)->ts.u.derived)
15918 != SPEC_ASSUMED))
15919 {
15920
15921 /* Add a check to verify if there are any LEN parameters in the
15922 first place. If there are LEN parameters, throw this error.
15923 If there are only KIND parameters, then don't trigger
15924 this error. */
15925 gfc_component *c;
15926 bool seen_len_param = false;
15927 gfc_actual_arglist *me_arg_param = me_arg->param_list;
15928
15929 for (; me_arg_param; me_arg_param = me_arg_param->next)
15930 {
15931 c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
15932 me_arg_param->name, true, true, NULL);
15933
15934 gcc_assert (c != NULL);
15935
15936 if (c->attr.pdt_kind)
15937 continue;
15938
15939 /* Getting here implies that there is a pdt_len parameter
15940 in the list. */
15941 seen_len_param = true;
15942 break;
15943 }
15944
15945 if (seen_len_param)
15946 {
15947 gfc_error ("All LEN type parameters of the passed dummy "
15948 "argument %qs of %qs at %L must be ASSUMED.",
15949 me_arg->name, proc->name, &where);
15950 goto error;
15951 }
15952 }
15953
15954 gcc_assert (me_arg->ts.type == BT_CLASS);
15955 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
15956 {
15957 gfc_error ("Passed-object dummy argument of %qs at %L must be"
15958 " scalar", proc->name, &where);
15959 goto error;
15960 }
15961 if (CLASS_DATA (me_arg)->attr.allocatable)
15962 {
15963 gfc_error ("Passed-object dummy argument of %qs at %L must not"
15964 " be ALLOCATABLE", proc->name, &where);
15965 goto error;
15966 }
15967 if (CLASS_DATA (me_arg)->attr.class_pointer)
15968 {
15969 gfc_error ("Passed-object dummy argument of %qs at %L must not"
15970 " be POINTER", proc->name, &where);
15971 goto error;
15972 }
15973 }
15974
15975 /* If we are extending some type, check that we don't override a procedure
15976 flagged NON_OVERRIDABLE. */
15977 stree->n.tb->overridden = NULL;
15978 if (super_type)
15979 {
15980 gfc_symtree* overridden;
15981 overridden = gfc_find_typebound_proc (super_type, NULL,
15982 stree->name, true, NULL);
15983
15984 if (overridden)
15985 {
15986 if (overridden->n.tb)
15987 stree->n.tb->overridden = overridden->n.tb;
15988
15989 if (!gfc_check_typebound_override (stree, overridden))
15990 goto error;
15991 }
15992 }
15993
15994 /* See if there's a name collision with a component directly in this type. */
15995 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
15996 if (!strcmp (comp->name, stree->name))
15997 {
15998 gfc_error ("Procedure %qs at %L has the same name as a component of"
15999 " %qs",
16000 stree->name, &where, resolve_bindings_derived->name);
16001 goto error;
16002 }
16003
16004 /* Try to find a name collision with an inherited component. */
16005 if (super_type && gfc_find_component (super_type, stree->name, true, true,
16006 NULL))
16007 {
16008 gfc_error ("Procedure %qs at %L has the same name as an inherited"
16009 " component of %qs",
16010 stree->name, &where, resolve_bindings_derived->name);
16011 goto error;
16012 }
16013
16014 stree->n.tb->error = 0;
16015 return;
16016
16017 error:
16018 resolve_bindings_result = false;
16019 stree->n.tb->error = 1;
16020 }
16021
16022
16023 static bool
16024 resolve_typebound_procedures (gfc_symbol* derived)
16025 {
16026 int op;
16027 gfc_symbol* super_type;
16028
16029 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
16030 return true;
16031
16032 super_type = gfc_get_derived_super_type (derived);
16033 if (super_type)
16034 resolve_symbol (super_type);
16035
16036 resolve_bindings_derived = derived;
16037 resolve_bindings_result = true;
16038
16039 if (derived->f2k_derived->tb_sym_root)
16040 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
16041 &resolve_typebound_procedure);
16042
16043 if (derived->f2k_derived->tb_uop_root)
16044 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
16045 &resolve_typebound_user_op);
16046
16047 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
16048 {
16049 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
16050 if (p && !resolve_typebound_intrinsic_op (derived,
16051 (gfc_intrinsic_op)op, p))
16052 resolve_bindings_result = false;
16053 }
16054
16055 return resolve_bindings_result;
16056 }
16057
16058
16059 /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
16060 to give all identical derived types the same backend_decl. */
16061 static void
16062 add_dt_to_dt_list (gfc_symbol *derived)
16063 {
16064 if (!derived->dt_next)
16065 {
16066 if (gfc_derived_types)
16067 {
16068 derived->dt_next = gfc_derived_types->dt_next;
16069 gfc_derived_types->dt_next = derived;
16070 }
16071 else
16072 {
16073 derived->dt_next = derived;
16074 }
16075 gfc_derived_types = derived;
16076 }
16077 }
16078
16079
16080 /* Ensure that a derived-type is really not abstract, meaning that every
16081 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
16082
16083 static bool
16084 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
16085 {
16086 if (!st)
16087 return true;
16088
16089 if (!ensure_not_abstract_walker (sub, st->left))
16090 return false;
16091 if (!ensure_not_abstract_walker (sub, st->right))
16092 return false;
16093
16094 if (st->n.tb && st->n.tb->deferred)
16095 {
16096 gfc_symtree* overriding;
16097 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
16098 if (!overriding)
16099 return false;
16100 gcc_assert (overriding->n.tb);
16101 if (overriding->n.tb->deferred)
16102 {
16103 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
16104 " %qs is DEFERRED and not overridden",
16105 sub->name, &sub->declared_at, st->name);
16106 return false;
16107 }
16108 }
16109
16110 return true;
16111 }
16112
16113 static bool
16114 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
16115 {
16116 /* The algorithm used here is to recursively travel up the ancestry of sub
16117 and for each ancestor-type, check all bindings. If any of them is
16118 DEFERRED, look it up starting from sub and see if the found (overriding)
16119 binding is not DEFERRED.
16120 This is not the most efficient way to do this, but it should be ok and is
16121 clearer than something sophisticated. */
16122
16123 gcc_assert (ancestor && !sub->attr.abstract);
16124
16125 if (!ancestor->attr.abstract)
16126 return true;
16127
16128 /* Walk bindings of this ancestor. */
16129 if (ancestor->f2k_derived)
16130 {
16131 bool t;
16132 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
16133 if (!t)
16134 return false;
16135 }
16136
16137 /* Find next ancestor type and recurse on it. */
16138 ancestor = gfc_get_derived_super_type (ancestor);
16139 if (ancestor)
16140 return ensure_not_abstract (sub, ancestor);
16141
16142 return true;
16143 }
16144
16145
16146 /* This check for typebound defined assignments is done recursively
16147 since the order in which derived types are resolved is not always in
16148 order of the declarations. */
16149
16150 static void
16151 check_defined_assignments (gfc_symbol *derived)
16152 {
16153 gfc_component *c;
16154
16155 for (c = derived->components; c; c = c->next)
16156 {
16157 if (!gfc_bt_struct (c->ts.type)
16158 || c->attr.pointer
16159 || c->attr.proc_pointer_comp
16160 || c->attr.class_pointer
16161 || c->attr.proc_pointer)
16162 continue;
16163
16164 if (c->ts.u.derived->attr.defined_assign_comp
16165 || (c->ts.u.derived->f2k_derived
16166 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
16167 {
16168 derived->attr.defined_assign_comp = 1;
16169 return;
16170 }
16171
16172 if (c->attr.allocatable)
16173 continue;
16174
16175 check_defined_assignments (c->ts.u.derived);
16176 if (c->ts.u.derived->attr.defined_assign_comp)
16177 {
16178 derived->attr.defined_assign_comp = 1;
16179 return;
16180 }
16181 }
16182 }
16183
16184
16185 /* Resolve a single component of a derived type or structure. */
16186
16187 static bool
16188 resolve_component (gfc_component *c, gfc_symbol *sym)
16189 {
16190 gfc_symbol *super_type;
16191 symbol_attribute *attr;
16192
16193 if (c->attr.artificial)
16194 return true;
16195
16196 /* Do not allow vtype components to be resolved in nameless namespaces
16197 such as block data because the procedure pointers will cause ICEs
16198 and vtables are not needed in these contexts. */
16199 if (sym->attr.vtype && sym->attr.use_assoc
16200 && sym->ns->proc_name == NULL)
16201 return true;
16202
16203 /* F2008, C442. */
16204 if ((!sym->attr.is_class || c != sym->components)
16205 && c->attr.codimension
16206 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
16207 {
16208 gfc_error ("Coarray component %qs at %L must be allocatable with "
16209 "deferred shape", c->name, &c->loc);
16210 return false;
16211 }
16212
16213 /* F2008, C443. */
16214 if (c->attr.codimension && c->ts.type == BT_DERIVED
16215 && c->ts.u.derived->ts.is_iso_c)
16216 {
16217 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16218 "shall not be a coarray", c->name, &c->loc);
16219 return false;
16220 }
16221
16222 /* F2008, C444. */
16223 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
16224 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
16225 || c->attr.allocatable))
16226 {
16227 gfc_error ("Component %qs at %L with coarray component "
16228 "shall be a nonpointer, nonallocatable scalar",
16229 c->name, &c->loc);
16230 return false;
16231 }
16232
16233 /* F2008, C448. */
16234 if (c->ts.type == BT_CLASS)
16235 {
16236 if (c->attr.class_ok && CLASS_DATA (c))
16237 {
16238 attr = &(CLASS_DATA (c)->attr);
16239
16240 /* Fix up contiguous attribute. */
16241 if (c->attr.contiguous)
16242 attr->contiguous = 1;
16243 }
16244 else
16245 attr = NULL;
16246 }
16247 else
16248 attr = &c->attr;
16249
16250 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
16251 {
16252 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
16253 "is not an array pointer", c->name, &c->loc);
16254 return false;
16255 }
16256
16257 /* F2003, 15.2.1 - length has to be one. */
16258 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
16259 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
16260 || !gfc_is_constant_expr (c->ts.u.cl->length)
16261 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
16262 {
16263 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
16264 c->name, &c->loc);
16265 return false;
16266 }
16267
16268 if (c->attr.proc_pointer && c->ts.interface)
16269 {
16270 gfc_symbol *ifc = c->ts.interface;
16271
16272 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
16273 {
16274 c->tb->error = 1;
16275 return false;
16276 }
16277
16278 if (ifc->attr.if_source || ifc->attr.intrinsic)
16279 {
16280 /* Resolve interface and copy attributes. */
16281 if (ifc->formal && !ifc->formal_ns)
16282 resolve_symbol (ifc);
16283 if (ifc->attr.intrinsic)
16284 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
16285
16286 if (ifc->result)
16287 {
16288 c->ts = ifc->result->ts;
16289 c->attr.allocatable = ifc->result->attr.allocatable;
16290 c->attr.pointer = ifc->result->attr.pointer;
16291 c->attr.dimension = ifc->result->attr.dimension;
16292 c->as = gfc_copy_array_spec (ifc->result->as);
16293 c->attr.class_ok = ifc->result->attr.class_ok;
16294 }
16295 else
16296 {
16297 c->ts = ifc->ts;
16298 c->attr.allocatable = ifc->attr.allocatable;
16299 c->attr.pointer = ifc->attr.pointer;
16300 c->attr.dimension = ifc->attr.dimension;
16301 c->as = gfc_copy_array_spec (ifc->as);
16302 c->attr.class_ok = ifc->attr.class_ok;
16303 }
16304 c->ts.interface = ifc;
16305 c->attr.function = ifc->attr.function;
16306 c->attr.subroutine = ifc->attr.subroutine;
16307
16308 c->attr.pure = ifc->attr.pure;
16309 c->attr.elemental = ifc->attr.elemental;
16310 c->attr.recursive = ifc->attr.recursive;
16311 c->attr.always_explicit = ifc->attr.always_explicit;
16312 c->attr.ext_attr |= ifc->attr.ext_attr;
16313 /* Copy char length. */
16314 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
16315 {
16316 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
16317 if (cl->length && !cl->resolved
16318 && !gfc_resolve_expr (cl->length))
16319 {
16320 c->tb->error = 1;
16321 return false;
16322 }
16323 c->ts.u.cl = cl;
16324 }
16325 }
16326 }
16327 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
16328 {
16329 /* Since PPCs are not implicitly typed, a PPC without an explicit
16330 interface must be a subroutine. */
16331 gfc_add_subroutine (&c->attr, c->name, &c->loc);
16332 }
16333
16334 /* Procedure pointer components: Check PASS arg. */
16335 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
16336 && !sym->attr.vtype)
16337 {
16338 gfc_symbol* me_arg;
16339
16340 if (c->tb->pass_arg)
16341 {
16342 gfc_formal_arglist* i;
16343
16344 /* If an explicit passing argument name is given, walk the arg-list
16345 and look for it. */
16346
16347 me_arg = NULL;
16348 c->tb->pass_arg_num = 1;
16349 for (i = c->ts.interface->formal; i; i = i->next)
16350 {
16351 if (!strcmp (i->sym->name, c->tb->pass_arg))
16352 {
16353 me_arg = i->sym;
16354 break;
16355 }
16356 c->tb->pass_arg_num++;
16357 }
16358
16359 if (!me_arg)
16360 {
16361 gfc_error ("Procedure pointer component %qs with PASS(%s) "
16362 "at %L has no argument %qs", c->name,
16363 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
16364 c->tb->error = 1;
16365 return false;
16366 }
16367 }
16368 else
16369 {
16370 /* Otherwise, take the first one; there should in fact be at least
16371 one. */
16372 c->tb->pass_arg_num = 1;
16373 if (!c->ts.interface->formal)
16374 {
16375 gfc_error ("Procedure pointer component %qs with PASS at %L "
16376 "must have at least one argument",
16377 c->name, &c->loc);
16378 c->tb->error = 1;
16379 return false;
16380 }
16381 me_arg = c->ts.interface->formal->sym;
16382 }
16383
16384 /* Now check that the argument-type matches. */
16385 gcc_assert (me_arg);
16386 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
16387 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
16388 || (me_arg->ts.type == BT_CLASS
16389 && CLASS_DATA (me_arg)->ts.u.derived != sym))
16390 {
16391 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
16392 " the derived type %qs", me_arg->name, c->name,
16393 me_arg->name, &c->loc, sym->name);
16394 c->tb->error = 1;
16395 return false;
16396 }
16397
16398 /* Check for F03:C453. */
16399 if (CLASS_DATA (me_arg)->attr.dimension)
16400 {
16401 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16402 "must be scalar", me_arg->name, c->name, me_arg->name,
16403 &c->loc);
16404 c->tb->error = 1;
16405 return false;
16406 }
16407
16408 if (CLASS_DATA (me_arg)->attr.class_pointer)
16409 {
16410 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16411 "may not have the POINTER attribute", me_arg->name,
16412 c->name, me_arg->name, &c->loc);
16413 c->tb->error = 1;
16414 return false;
16415 }
16416
16417 if (CLASS_DATA (me_arg)->attr.allocatable)
16418 {
16419 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16420 "may not be ALLOCATABLE", me_arg->name, c->name,
16421 me_arg->name, &c->loc);
16422 c->tb->error = 1;
16423 return false;
16424 }
16425
16426 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
16427 {
16428 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16429 " at %L", c->name, &c->loc);
16430 return false;
16431 }
16432
16433 }
16434
16435 /* Check type-spec if this is not the parent-type component. */
16436 if (((sym->attr.is_class
16437 && (!sym->components->ts.u.derived->attr.extension
16438 || c != CLASS_DATA (sym->components)))
16439 || (!sym->attr.is_class
16440 && (!sym->attr.extension || c != sym->components)))
16441 && !sym->attr.vtype
16442 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
16443 return false;
16444
16445 super_type = gfc_get_derived_super_type (sym);
16446
16447 /* If this type is an extension, set the accessibility of the parent
16448 component. */
16449 if (super_type
16450 && ((sym->attr.is_class
16451 && c == CLASS_DATA (sym->components))
16452 || (!sym->attr.is_class && c == sym->components))
16453 && strcmp (super_type->name, c->name) == 0)
16454 c->attr.access = super_type->attr.access;
16455
16456 /* If this type is an extension, see if this component has the same name
16457 as an inherited type-bound procedure. */
16458 if (super_type && !sym->attr.is_class
16459 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
16460 {
16461 gfc_error ("Component %qs of %qs at %L has the same name as an"
16462 " inherited type-bound procedure",
16463 c->name, sym->name, &c->loc);
16464 return false;
16465 }
16466
16467 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
16468 && !c->ts.deferred)
16469 {
16470 if (c->ts.u.cl->length == NULL
16471 || (!resolve_charlen(c->ts.u.cl))
16472 || !gfc_is_constant_expr (c->ts.u.cl->length))
16473 {
16474 gfc_error ("Character length of component %qs needs to "
16475 "be a constant specification expression at %L",
16476 c->name,
16477 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
16478 return false;
16479 }
16480
16481 if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
16482 {
16483 if (!c->ts.u.cl->length->error)
16484 {
16485 gfc_error ("Character length expression of component %qs at %L "
16486 "must be of INTEGER type, found %s",
16487 c->name, &c->ts.u.cl->length->where,
16488 gfc_basic_typename (c->ts.u.cl->length->ts.type));
16489 c->ts.u.cl->length->error = 1;
16490 }
16491 return false;
16492 }
16493 }
16494
16495 if (c->ts.type == BT_CHARACTER && c->ts.deferred
16496 && !c->attr.pointer && !c->attr.allocatable)
16497 {
16498 gfc_error ("Character component %qs of %qs at %L with deferred "
16499 "length must be a POINTER or ALLOCATABLE",
16500 c->name, sym->name, &c->loc);
16501 return false;
16502 }
16503
16504 /* Add the hidden deferred length field. */
16505 if (c->ts.type == BT_CHARACTER
16506 && (c->ts.deferred || c->attr.pdt_string)
16507 && !c->attr.function
16508 && !sym->attr.is_class)
16509 {
16510 char name[GFC_MAX_SYMBOL_LEN+9];
16511 gfc_component *strlen;
16512 sprintf (name, "_%s_length", c->name);
16513 strlen = gfc_find_component (sym, name, true, true, NULL);
16514 if (strlen == NULL)
16515 {
16516 if (!gfc_add_component (sym, name, &strlen))
16517 return false;
16518 strlen->ts.type = BT_INTEGER;
16519 strlen->ts.kind = gfc_charlen_int_kind;
16520 strlen->attr.access = ACCESS_PRIVATE;
16521 strlen->attr.artificial = 1;
16522 }
16523 }
16524
16525 if (c->ts.type == BT_DERIVED
16526 && sym->component_access != ACCESS_PRIVATE
16527 && gfc_check_symbol_access (sym)
16528 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
16529 && !c->ts.u.derived->attr.use_assoc
16530 && !gfc_check_symbol_access (c->ts.u.derived)
16531 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
16532 "PRIVATE type and cannot be a component of "
16533 "%qs, which is PUBLIC at %L", c->name,
16534 sym->name, &sym->declared_at))
16535 return false;
16536
16537 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
16538 {
16539 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
16540 "type %s", c->name, &c->loc, sym->name);
16541 return false;
16542 }
16543
16544 if (sym->attr.sequence)
16545 {
16546 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
16547 {
16548 gfc_error ("Component %s of SEQUENCE type declared at %L does "
16549 "not have the SEQUENCE attribute",
16550 c->ts.u.derived->name, &sym->declared_at);
16551 return false;
16552 }
16553 }
16554
16555 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
16556 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
16557 else if (c->ts.type == BT_CLASS && c->attr.class_ok
16558 && CLASS_DATA (c)->ts.u.derived->attr.generic)
16559 CLASS_DATA (c)->ts.u.derived
16560 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
16561
16562 /* If an allocatable component derived type is of the same type as
16563 the enclosing derived type, we need a vtable generating so that
16564 the __deallocate procedure is created. */
16565 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
16566 && c->ts.u.derived == sym && c->attr.allocatable == 1)
16567 gfc_find_vtab (&c->ts);
16568
16569 /* Ensure that all the derived type components are put on the
16570 derived type list; even in formal namespaces, where derived type
16571 pointer components might not have been declared. */
16572 if (c->ts.type == BT_DERIVED
16573 && c->ts.u.derived
16574 && c->ts.u.derived->components
16575 && c->attr.pointer
16576 && sym != c->ts.u.derived)
16577 add_dt_to_dt_list (c->ts.u.derived);
16578
16579 if (c->as && c->as->type != AS_DEFERRED
16580 && (c->attr.pointer || c->attr.allocatable))
16581 return false;
16582
16583 if (!gfc_resolve_array_spec (c->as,
16584 !(c->attr.pointer || c->attr.proc_pointer
16585 || c->attr.allocatable)))
16586 return false;
16587
16588 if (c->initializer && !sym->attr.vtype
16589 && !c->attr.pdt_kind && !c->attr.pdt_len
16590 && !gfc_check_assign_symbol (sym, c, c->initializer))
16591 return false;
16592
16593 return true;
16594 }
16595
16596
16597 /* Be nice about the locus for a structure expression - show the locus of the
16598 first non-null sub-expression if we can. */
16599
16600 static locus *
16601 cons_where (gfc_expr *struct_expr)
16602 {
16603 gfc_constructor *cons;
16604
16605 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
16606
16607 cons = gfc_constructor_first (struct_expr->value.constructor);
16608 for (; cons; cons = gfc_constructor_next (cons))
16609 {
16610 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
16611 return &cons->expr->where;
16612 }
16613
16614 return &struct_expr->where;
16615 }
16616
16617 /* Resolve the components of a structure type. Much less work than derived
16618 types. */
16619
16620 static bool
16621 resolve_fl_struct (gfc_symbol *sym)
16622 {
16623 gfc_component *c;
16624 gfc_expr *init = NULL;
16625 bool success;
16626
16627 /* Make sure UNIONs do not have overlapping initializers. */
16628 if (sym->attr.flavor == FL_UNION)
16629 {
16630 for (c = sym->components; c; c = c->next)
16631 {
16632 if (init && c->initializer)
16633 {
16634 gfc_error ("Conflicting initializers in union at %L and %L",
16635 cons_where (init), cons_where (c->initializer));
16636 gfc_free_expr (c->initializer);
16637 c->initializer = NULL;
16638 }
16639 if (init == NULL)
16640 init = c->initializer;
16641 }
16642 }
16643
16644 success = true;
16645 for (c = sym->components; c; c = c->next)
16646 if (!resolve_component (c, sym))
16647 success = false;
16648
16649 if (!success)
16650 return false;
16651
16652 if (sym->components)
16653 add_dt_to_dt_list (sym);
16654
16655 return true;
16656 }
16657
16658 /* Figure if the derived type is using itself directly in one of its components
16659 or through referencing other derived types. The information is required to
16660 generate the __deallocate and __final type bound procedures to ensure
16661 freeing larger hierarchies of derived types with allocatable objects. */
16662
16663 static void
16664 resolve_cyclic_derived_type (gfc_symbol *derived)
16665 {
16666 hash_set<gfc_symbol *> seen, to_examin;
16667 gfc_component *c;
16668 seen.add (derived);
16669 to_examin.add (derived);
16670 while (!to_examin.is_empty ())
16671 {
16672 gfc_symbol *cand = *to_examin.begin ();
16673 to_examin.remove (cand);
16674 for (c = cand->components; c; c = c->next)
16675 if (c->ts.type == BT_DERIVED)
16676 {
16677 if (c->ts.u.derived == derived)
16678 {
16679 derived->attr.recursive = 1;
16680 return;
16681 }
16682 else if (!seen.contains (c->ts.u.derived))
16683 {
16684 seen.add (c->ts.u.derived);
16685 to_examin.add (c->ts.u.derived);
16686 }
16687 }
16688 else if (c->ts.type == BT_CLASS)
16689 {
16690 if (!c->attr.class_ok)
16691 continue;
16692 if (CLASS_DATA (c)->ts.u.derived == derived)
16693 {
16694 derived->attr.recursive = 1;
16695 return;
16696 }
16697 else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
16698 {
16699 seen.add (CLASS_DATA (c)->ts.u.derived);
16700 to_examin.add (CLASS_DATA (c)->ts.u.derived);
16701 }
16702 }
16703 }
16704 }
16705
16706 /* Resolve the components of a derived type. This does not have to wait until
16707 resolution stage, but can be done as soon as the dt declaration has been
16708 parsed. */
16709
16710 static bool
16711 resolve_fl_derived0 (gfc_symbol *sym)
16712 {
16713 gfc_symbol* super_type;
16714 gfc_component *c;
16715 gfc_formal_arglist *f;
16716 bool success;
16717
16718 if (sym->attr.unlimited_polymorphic)
16719 return true;
16720
16721 super_type = gfc_get_derived_super_type (sym);
16722
16723 /* F2008, C432. */
16724 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
16725 {
16726 gfc_error ("As extending type %qs at %L has a coarray component, "
16727 "parent type %qs shall also have one", sym->name,
16728 &sym->declared_at, super_type->name);
16729 return false;
16730 }
16731
16732 /* Ensure the extended type gets resolved before we do. */
16733 if (super_type && !resolve_fl_derived0 (super_type))
16734 return false;
16735
16736 /* An ABSTRACT type must be extensible. */
16737 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
16738 {
16739 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
16740 sym->name, &sym->declared_at);
16741 return false;
16742 }
16743
16744 /* Resolving components below, may create vtabs for which the cyclic type
16745 information needs to be present. */
16746 if (!sym->attr.vtype)
16747 resolve_cyclic_derived_type (sym);
16748
16749 c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
16750 : sym->components;
16751
16752 success = true;
16753 for ( ; c != NULL; c = c->next)
16754 if (!resolve_component (c, sym))
16755 success = false;
16756
16757 if (!success)
16758 return false;
16759
16760 /* Now add the caf token field, where needed. */
16761 if (flag_coarray != GFC_FCOARRAY_NONE
16762 && !sym->attr.is_class && !sym->attr.vtype)
16763 {
16764 for (c = sym->components; c; c = c->next)
16765 if (!c->attr.dimension && !c->attr.codimension
16766 && (c->attr.allocatable || c->attr.pointer))
16767 {
16768 char name[GFC_MAX_SYMBOL_LEN+9];
16769 gfc_component *token;
16770 sprintf (name, "_caf_%s", c->name);
16771 token = gfc_find_component (sym, name, true, true, NULL);
16772 if (token == NULL)
16773 {
16774 if (!gfc_add_component (sym, name, &token))
16775 return false;
16776 token->ts.type = BT_VOID;
16777 token->ts.kind = gfc_default_integer_kind;
16778 token->attr.access = ACCESS_PRIVATE;
16779 token->attr.artificial = 1;
16780 token->attr.caf_token = 1;
16781 }
16782 c->caf_token = token;
16783 }
16784 }
16785
16786 check_defined_assignments (sym);
16787
16788 if (!sym->attr.defined_assign_comp && super_type)
16789 sym->attr.defined_assign_comp
16790 = super_type->attr.defined_assign_comp;
16791
16792 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
16793 all DEFERRED bindings are overridden. */
16794 if (super_type && super_type->attr.abstract && !sym->attr.abstract
16795 && !sym->attr.is_class
16796 && !ensure_not_abstract (sym, super_type))
16797 return false;
16798
16799 /* Check that there is a component for every PDT parameter. */
16800 if (sym->attr.pdt_template)
16801 {
16802 for (f = sym->formal; f; f = f->next)
16803 {
16804 if (!f->sym)
16805 continue;
16806 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
16807 if (c == NULL)
16808 {
16809 gfc_error ("Parameterized type %qs does not have a component "
16810 "corresponding to parameter %qs at %L", sym->name,
16811 f->sym->name, &sym->declared_at);
16812 break;
16813 }
16814 }
16815 }
16816
16817 /* Add derived type to the derived type list. */
16818 add_dt_to_dt_list (sym);
16819
16820 return true;
16821 }
16822
16823 /* The following procedure does the full resolution of a derived type,
16824 including resolution of all type-bound procedures (if present). In contrast
16825 to 'resolve_fl_derived0' this can only be done after the module has been
16826 parsed completely. */
16827
16828 static bool
16829 resolve_fl_derived (gfc_symbol *sym)
16830 {
16831 gfc_symbol *gen_dt = NULL;
16832
16833 if (sym->attr.unlimited_polymorphic)
16834 return true;
16835
16836 if (!sym->attr.is_class)
16837 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
16838 if (gen_dt && gen_dt->generic && gen_dt->generic->next
16839 && (!gen_dt->generic->sym->attr.use_assoc
16840 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
16841 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
16842 "%qs at %L being the same name as derived "
16843 "type at %L", sym->name,
16844 gen_dt->generic->sym == sym
16845 ? gen_dt->generic->next->sym->name
16846 : gen_dt->generic->sym->name,
16847 gen_dt->generic->sym == sym
16848 ? &gen_dt->generic->next->sym->declared_at
16849 : &gen_dt->generic->sym->declared_at,
16850 &sym->declared_at))
16851 return false;
16852
16853 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
16854 {
16855 gfc_error ("Derived type %qs at %L has not been declared",
16856 sym->name, &sym->declared_at);
16857 return false;
16858 }
16859
16860 /* Resolve the finalizer procedures. */
16861 if (!gfc_resolve_finalizers (sym, NULL))
16862 return false;
16863
16864 if (sym->attr.is_class && sym->ts.u.derived == NULL)
16865 {
16866 /* Fix up incomplete CLASS symbols. */
16867 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
16868 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
16869
16870 /* Nothing more to do for unlimited polymorphic entities. */
16871 if (data->ts.u.derived->attr.unlimited_polymorphic)
16872 {
16873 add_dt_to_dt_list (sym);
16874 return true;
16875 }
16876 else if (vptr->ts.u.derived == NULL)
16877 {
16878 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
16879 gcc_assert (vtab);
16880 vptr->ts.u.derived = vtab->ts.u.derived;
16881 if (!resolve_fl_derived0 (vptr->ts.u.derived))
16882 return false;
16883 }
16884 }
16885
16886 if (!resolve_fl_derived0 (sym))
16887 return false;
16888
16889 /* Resolve the type-bound procedures. */
16890 if (!resolve_typebound_procedures (sym))
16891 return false;
16892
16893 /* Generate module vtables subject to their accessibility and their not
16894 being vtables or pdt templates. If this is not done class declarations
16895 in external procedures wind up with their own version and so SELECT TYPE
16896 fails because the vptrs do not have the same address. */
16897 if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
16898 && (sym->ns->proc_name->attr.flavor == FL_MODULE
16899 || (sym->attr.recursive && sym->attr.alloc_comp))
16900 && sym->attr.access != ACCESS_PRIVATE
16901 && !(sym->attr.vtype || sym->attr.pdt_template))
16902 {
16903 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
16904 gfc_set_sym_referenced (vtab);
16905 }
16906
16907 return true;
16908 }
16909
16910
16911 static bool
16912 resolve_fl_namelist (gfc_symbol *sym)
16913 {
16914 gfc_namelist *nl;
16915 gfc_symbol *nlsym;
16916
16917 for (nl = sym->namelist; nl; nl = nl->next)
16918 {
16919 /* Check again, the check in match only works if NAMELIST comes
16920 after the decl. */
16921 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
16922 {
16923 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
16924 "allowed", nl->sym->name, sym->name, &sym->declared_at);
16925 return false;
16926 }
16927
16928 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
16929 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
16930 "with assumed shape in namelist %qs at %L",
16931 nl->sym->name, sym->name, &sym->declared_at))
16932 return false;
16933
16934 if (is_non_constant_shape_array (nl->sym)
16935 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
16936 "with nonconstant shape in namelist %qs at %L",
16937 nl->sym->name, sym->name, &sym->declared_at))
16938 return false;
16939
16940 if (nl->sym->ts.type == BT_CHARACTER
16941 && (nl->sym->ts.u.cl->length == NULL
16942 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
16943 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
16944 "nonconstant character length in "
16945 "namelist %qs at %L", nl->sym->name,
16946 sym->name, &sym->declared_at))
16947 return false;
16948
16949 }
16950
16951 /* Reject PRIVATE objects in a PUBLIC namelist. */
16952 if (gfc_check_symbol_access (sym))
16953 {
16954 for (nl = sym->namelist; nl; nl = nl->next)
16955 {
16956 if (!nl->sym->attr.use_assoc
16957 && !is_sym_host_assoc (nl->sym, sym->ns)
16958 && !gfc_check_symbol_access (nl->sym))
16959 {
16960 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
16961 "cannot be member of PUBLIC namelist %qs at %L",
16962 nl->sym->name, sym->name, &sym->declared_at);
16963 return false;
16964 }
16965
16966 if (nl->sym->ts.type == BT_DERIVED
16967 && (nl->sym->ts.u.derived->attr.alloc_comp
16968 || nl->sym->ts.u.derived->attr.pointer_comp))
16969 {
16970 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
16971 "namelist %qs at %L with ALLOCATABLE "
16972 "or POINTER components", nl->sym->name,
16973 sym->name, &sym->declared_at))
16974 return false;
16975 return true;
16976 }
16977
16978 /* Types with private components that came here by USE-association. */
16979 if (nl->sym->ts.type == BT_DERIVED
16980 && derived_inaccessible (nl->sym->ts.u.derived))
16981 {
16982 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
16983 "components and cannot be member of namelist %qs at %L",
16984 nl->sym->name, sym->name, &sym->declared_at);
16985 return false;
16986 }
16987
16988 /* Types with private components that are defined in the same module. */
16989 if (nl->sym->ts.type == BT_DERIVED
16990 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
16991 && nl->sym->ts.u.derived->attr.private_comp)
16992 {
16993 gfc_error ("NAMELIST object %qs has PRIVATE components and "
16994 "cannot be a member of PUBLIC namelist %qs at %L",
16995 nl->sym->name, sym->name, &sym->declared_at);
16996 return false;
16997 }
16998 }
16999 }
17000
17001
17002 /* 14.1.2 A module or internal procedure represent local entities
17003 of the same type as a namelist member and so are not allowed. */
17004 for (nl = sym->namelist; nl; nl = nl->next)
17005 {
17006 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
17007 continue;
17008
17009 if (nl->sym->attr.function && nl->sym == nl->sym->result)
17010 if ((nl->sym == sym->ns->proc_name)
17011 ||
17012 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
17013 continue;
17014
17015 nlsym = NULL;
17016 if (nl->sym->name)
17017 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
17018 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
17019 {
17020 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
17021 "attribute in %qs at %L", nlsym->name,
17022 &sym->declared_at);
17023 return false;
17024 }
17025 }
17026
17027 return true;
17028 }
17029
17030
17031 static bool
17032 resolve_fl_parameter (gfc_symbol *sym)
17033 {
17034 /* A parameter array's shape needs to be constant. */
17035 if (sym->as != NULL
17036 && (sym->as->type == AS_DEFERRED
17037 || is_non_constant_shape_array (sym)))
17038 {
17039 gfc_error ("Parameter array %qs at %L cannot be automatic "
17040 "or of deferred shape", sym->name, &sym->declared_at);
17041 return false;
17042 }
17043
17044 /* Constraints on deferred type parameter. */
17045 if (!deferred_requirements (sym))
17046 return false;
17047
17048 /* Make sure a parameter that has been implicitly typed still
17049 matches the implicit type, since PARAMETER statements can precede
17050 IMPLICIT statements. */
17051 if (sym->attr.implicit_type
17052 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
17053 sym->ns)))
17054 {
17055 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
17056 "later IMPLICIT type", sym->name, &sym->declared_at);
17057 return false;
17058 }
17059
17060 /* Make sure the types of derived parameters are consistent. This
17061 type checking is deferred until resolution because the type may
17062 refer to a derived type from the host. */
17063 if (sym->ts.type == BT_DERIVED
17064 && !gfc_compare_types (&sym->ts, &sym->value->ts))
17065 {
17066 gfc_error ("Incompatible derived type in PARAMETER at %L",
17067 &sym->value->where);
17068 return false;
17069 }
17070
17071 /* F03:C509,C514. */
17072 if (sym->ts.type == BT_CLASS)
17073 {
17074 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
17075 sym->name, &sym->declared_at);
17076 return false;
17077 }
17078
17079 return true;
17080 }
17081
17082
17083 /* Called by resolve_symbol to check PDTs. */
17084
17085 static void
17086 resolve_pdt (gfc_symbol* sym)
17087 {
17088 gfc_symbol *derived = NULL;
17089 gfc_actual_arglist *param;
17090 gfc_component *c;
17091 bool const_len_exprs = true;
17092 bool assumed_len_exprs = false;
17093 symbol_attribute *attr;
17094
17095 if (sym->ts.type == BT_DERIVED)
17096 {
17097 derived = sym->ts.u.derived;
17098 attr = &(sym->attr);
17099 }
17100 else if (sym->ts.type == BT_CLASS)
17101 {
17102 derived = CLASS_DATA (sym)->ts.u.derived;
17103 attr = &(CLASS_DATA (sym)->attr);
17104 }
17105 else
17106 gcc_unreachable ();
17107
17108 gcc_assert (derived->attr.pdt_type);
17109
17110 for (param = sym->param_list; param; param = param->next)
17111 {
17112 c = gfc_find_component (derived, param->name, false, true, NULL);
17113 gcc_assert (c);
17114 if (c->attr.pdt_kind)
17115 continue;
17116
17117 if (param->expr && !gfc_is_constant_expr (param->expr)
17118 && c->attr.pdt_len)
17119 const_len_exprs = false;
17120 else if (param->spec_type == SPEC_ASSUMED)
17121 assumed_len_exprs = true;
17122
17123 if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
17124 && ((sym->ts.type == BT_DERIVED && !attr->pointer)
17125 || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
17126 gfc_error ("Entity %qs at %L has a deferred LEN "
17127 "parameter %qs and requires either the POINTER "
17128 "or ALLOCATABLE attribute",
17129 sym->name, &sym->declared_at,
17130 param->name);
17131
17132 }
17133
17134 if (!const_len_exprs
17135 && (sym->ns->proc_name->attr.is_main_program
17136 || sym->ns->proc_name->attr.flavor == FL_MODULE
17137 || sym->attr.save != SAVE_NONE))
17138 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
17139 "SAVE attribute or be a variable declared in the "
17140 "main program, a module or a submodule(F08/C513)",
17141 sym->name, &sym->declared_at);
17142
17143 if (assumed_len_exprs && !(sym->attr.dummy
17144 || sym->attr.select_type_temporary || sym->attr.associate_var))
17145 gfc_error ("The object %qs at %L with ASSUMED type parameters "
17146 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
17147 sym->name, &sym->declared_at);
17148 }
17149
17150
17151 /* Resolve the symbol's array spec. */
17152
17153 static bool
17154 resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
17155 {
17156 gfc_namespace *orig_current_ns = gfc_current_ns;
17157 gfc_current_ns = gfc_get_spec_ns (sym);
17158
17159 bool saved_specification_expr = specification_expr;
17160 gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
17161 specification_expr = true;
17162 specification_expr_symbol = sym;
17163
17164 bool result = gfc_resolve_array_spec (sym->as, check_constant);
17165
17166 specification_expr = saved_specification_expr;
17167 specification_expr_symbol = saved_specification_expr_symbol;
17168 gfc_current_ns = orig_current_ns;
17169
17170 return result;
17171 }
17172
17173
17174 /* Do anything necessary to resolve a symbol. Right now, we just
17175 assume that an otherwise unknown symbol is a variable. This sort
17176 of thing commonly happens for symbols in module. */
17177
17178 static void
17179 resolve_symbol (gfc_symbol *sym)
17180 {
17181 int check_constant, mp_flag;
17182 gfc_symtree *symtree;
17183 gfc_symtree *this_symtree;
17184 gfc_namespace *ns;
17185 gfc_component *c;
17186 symbol_attribute class_attr;
17187 gfc_array_spec *as;
17188
17189 if (sym->resolve_symbol_called >= 1)
17190 return;
17191 sym->resolve_symbol_called = 1;
17192
17193 /* No symbol will ever have union type; only components can be unions.
17194 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
17195 (just like derived type declaration symbols have flavor FL_DERIVED). */
17196 gcc_assert (sym->ts.type != BT_UNION);
17197
17198 /* Coarrayed polymorphic objects with allocatable or pointer components are
17199 yet unsupported for -fcoarray=lib. */
17200 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
17201 && sym->ts.u.derived && CLASS_DATA (sym)
17202 && CLASS_DATA (sym)->attr.codimension
17203 && CLASS_DATA (sym)->ts.u.derived
17204 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
17205 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
17206 {
17207 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
17208 "type coarrays at %L are unsupported", &sym->declared_at);
17209 return;
17210 }
17211
17212 if (sym->attr.artificial)
17213 return;
17214
17215 if (sym->attr.unlimited_polymorphic)
17216 return;
17217
17218 if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
17219 {
17220 gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
17221 "the OpenMP DEPEND clause", &sym->declared_at);
17222 return;
17223 }
17224
17225 if (sym->attr.flavor == FL_UNKNOWN
17226 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
17227 && !sym->attr.generic && !sym->attr.external
17228 && sym->attr.if_source == IFSRC_UNKNOWN
17229 && sym->ts.type == BT_UNKNOWN))
17230 {
17231 /* A symbol in a common block might not have been resolved yet properly.
17232 Do not try to find an interface with the same name. */
17233 if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
17234 && !sym->attr.generic && !sym->attr.external
17235 && sym->attr.in_common)
17236 goto skip_interfaces;
17237
17238 /* If we find that a flavorless symbol is an interface in one of the
17239 parent namespaces, find its symtree in this namespace, free the
17240 symbol and set the symtree to point to the interface symbol. */
17241 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
17242 {
17243 symtree = gfc_find_symtree (ns->sym_root, sym->name);
17244 if (symtree && (symtree->n.sym->generic ||
17245 (symtree->n.sym->attr.flavor == FL_PROCEDURE
17246 && sym->ns->construct_entities)))
17247 {
17248 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
17249 sym->name);
17250 if (this_symtree->n.sym == sym)
17251 {
17252 symtree->n.sym->refs++;
17253 gfc_release_symbol (sym);
17254 this_symtree->n.sym = symtree->n.sym;
17255 return;
17256 }
17257 }
17258 }
17259
17260 skip_interfaces:
17261 /* Otherwise give it a flavor according to such attributes as
17262 it has. */
17263 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
17264 && sym->attr.intrinsic == 0)
17265 sym->attr.flavor = FL_VARIABLE;
17266 else if (sym->attr.flavor == FL_UNKNOWN)
17267 {
17268 sym->attr.flavor = FL_PROCEDURE;
17269 if (sym->attr.dimension)
17270 sym->attr.function = 1;
17271 }
17272 }
17273
17274 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
17275 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
17276
17277 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
17278 && !resolve_procedure_interface (sym))
17279 return;
17280
17281 if (sym->attr.is_protected && !sym->attr.proc_pointer
17282 && (sym->attr.procedure || sym->attr.external))
17283 {
17284 if (sym->attr.external)
17285 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
17286 "at %L", &sym->declared_at);
17287 else
17288 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
17289 "at %L", &sym->declared_at);
17290
17291 return;
17292 }
17293
17294 /* Ensure that variables of derived or class type having a finalizer are
17295 marked used even when the variable is not used anything else in the scope.
17296 This fixes PR118730. */
17297 if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
17298 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
17299 && gfc_may_be_finalized (sym->ts))
17300 gfc_set_sym_referenced (sym);
17301
17302 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
17303 return;
17304
17305 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
17306 && !resolve_fl_struct (sym))
17307 return;
17308
17309 /* Symbols that are module procedures with results (functions) have
17310 the types and array specification copied for type checking in
17311 procedures that call them, as well as for saving to a module
17312 file. These symbols can't stand the scrutiny that their results
17313 can. */
17314 mp_flag = (sym->result != NULL && sym->result != sym);
17315
17316 /* Make sure that the intrinsic is consistent with its internal
17317 representation. This needs to be done before assigning a default
17318 type to avoid spurious warnings. */
17319 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
17320 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
17321 return;
17322
17323 /* Resolve associate names. */
17324 if (sym->assoc)
17325 resolve_assoc_var (sym, true);
17326
17327 /* Assign default type to symbols that need one and don't have one. */
17328 if (sym->ts.type == BT_UNKNOWN)
17329 {
17330 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
17331 {
17332 gfc_set_default_type (sym, 1, NULL);
17333 }
17334
17335 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
17336 && !sym->attr.function && !sym->attr.subroutine
17337 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
17338 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
17339
17340 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17341 {
17342 /* The specific case of an external procedure should emit an error
17343 in the case that there is no implicit type. */
17344 if (!mp_flag)
17345 {
17346 if (!sym->attr.mixed_entry_master)
17347 gfc_set_default_type (sym, sym->attr.external, NULL);
17348 }
17349 else
17350 {
17351 /* Result may be in another namespace. */
17352 resolve_symbol (sym->result);
17353
17354 if (!sym->result->attr.proc_pointer)
17355 {
17356 sym->ts = sym->result->ts;
17357 sym->as = gfc_copy_array_spec (sym->result->as);
17358 sym->attr.dimension = sym->result->attr.dimension;
17359 sym->attr.codimension = sym->result->attr.codimension;
17360 sym->attr.pointer = sym->result->attr.pointer;
17361 sym->attr.allocatable = sym->result->attr.allocatable;
17362 sym->attr.contiguous = sym->result->attr.contiguous;
17363 }
17364 }
17365 }
17366 }
17367 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17368 resolve_symbol_array_spec (sym->result, false);
17369
17370 /* For a CLASS-valued function with a result variable, affirm that it has
17371 been resolved also when looking at the symbol 'sym'. */
17372 if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
17373 sym->attr.class_ok = sym->result->attr.class_ok;
17374
17375 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
17376 && CLASS_DATA (sym))
17377 {
17378 as = CLASS_DATA (sym)->as;
17379 class_attr = CLASS_DATA (sym)->attr;
17380 class_attr.pointer = class_attr.class_pointer;
17381 }
17382 else
17383 {
17384 class_attr = sym->attr;
17385 as = sym->as;
17386 }
17387
17388 /* F2008, C530. */
17389 if (sym->attr.contiguous
17390 && !sym->attr.associate_var
17391 && (!class_attr.dimension
17392 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
17393 && !class_attr.pointer)))
17394 {
17395 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
17396 "array pointer or an assumed-shape or assumed-rank array",
17397 sym->name, &sym->declared_at);
17398 return;
17399 }
17400
17401 /* Assumed size arrays and assumed shape arrays must be dummy
17402 arguments. Array-spec's of implied-shape should have been resolved to
17403 AS_EXPLICIT already. */
17404
17405 if (as)
17406 {
17407 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
17408 specification expression. */
17409 if (as->type == AS_IMPLIED_SHAPE)
17410 {
17411 int i;
17412 for (i=0; i<as->rank; i++)
17413 {
17414 if (as->lower[i] != NULL && as->upper[i] == NULL)
17415 {
17416 gfc_error ("Bad specification for assumed size array at %L",
17417 &as->lower[i]->where);
17418 return;
17419 }
17420 }
17421 gcc_unreachable();
17422 }
17423
17424 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
17425 || as->type == AS_ASSUMED_SHAPE)
17426 && !sym->attr.dummy && !sym->attr.select_type_temporary
17427 && !sym->attr.associate_var)
17428 {
17429 if (as->type == AS_ASSUMED_SIZE)
17430 gfc_error ("Assumed size array at %L must be a dummy argument",
17431 &sym->declared_at);
17432 else
17433 gfc_error ("Assumed shape array at %L must be a dummy argument",
17434 &sym->declared_at);
17435 return;
17436 }
17437 /* TS 29113, C535a. */
17438 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
17439 && !sym->attr.select_type_temporary
17440 && !(cs_base && cs_base->current
17441 && (cs_base->current->op == EXEC_SELECT_RANK
17442 || ((gfc_option.allow_std & GFC_STD_F202Y)
17443 && cs_base->current->op == EXEC_BLOCK))))
17444 {
17445 gfc_error ("Assumed-rank array at %L must be a dummy argument",
17446 &sym->declared_at);
17447 return;
17448 }
17449 if (as->type == AS_ASSUMED_RANK
17450 && (sym->attr.codimension || sym->attr.value))
17451 {
17452 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
17453 "CODIMENSION attribute", &sym->declared_at);
17454 return;
17455 }
17456 }
17457
17458 /* Make sure symbols with known intent or optional are really dummy
17459 variable. Because of ENTRY statement, this has to be deferred
17460 until resolution time. */
17461
17462 if (!sym->attr.dummy
17463 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
17464 {
17465 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
17466 return;
17467 }
17468
17469 if (sym->attr.value && !sym->attr.dummy)
17470 {
17471 gfc_error ("%qs at %L cannot have the VALUE attribute because "
17472 "it is not a dummy argument", sym->name, &sym->declared_at);
17473 return;
17474 }
17475
17476 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
17477 {
17478 gfc_charlen *cl = sym->ts.u.cl;
17479 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
17480 {
17481 gfc_error ("Character dummy variable %qs at %L with VALUE "
17482 "attribute must have constant length",
17483 sym->name, &sym->declared_at);
17484 return;
17485 }
17486
17487 if (sym->ts.is_c_interop
17488 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
17489 {
17490 gfc_error ("C interoperable character dummy variable %qs at %L "
17491 "with VALUE attribute must have length one",
17492 sym->name, &sym->declared_at);
17493 return;
17494 }
17495 }
17496
17497 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17498 && sym->ts.u.derived->attr.generic)
17499 {
17500 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
17501 if (!sym->ts.u.derived)
17502 {
17503 gfc_error ("The derived type %qs at %L is of type %qs, "
17504 "which has not been defined", sym->name,
17505 &sym->declared_at, sym->ts.u.derived->name);
17506 sym->ts.type = BT_UNKNOWN;
17507 return;
17508 }
17509 }
17510
17511 /* Use the same constraints as TYPE(*), except for the type check
17512 and that only scalars and assumed-size arrays are permitted. */
17513 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
17514 {
17515 if (!sym->attr.dummy)
17516 {
17517 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17518 "a dummy argument", sym->name, &sym->declared_at);
17519 return;
17520 }
17521
17522 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
17523 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
17524 && sym->ts.type != BT_COMPLEX)
17525 {
17526 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17527 "of type TYPE(*) or of an numeric intrinsic type",
17528 sym->name, &sym->declared_at);
17529 return;
17530 }
17531
17532 if (sym->attr.allocatable || sym->attr.codimension
17533 || sym->attr.pointer || sym->attr.value)
17534 {
17535 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17536 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
17537 "attribute", sym->name, &sym->declared_at);
17538 return;
17539 }
17540
17541 if (sym->attr.intent == INTENT_OUT)
17542 {
17543 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17544 "have the INTENT(OUT) attribute",
17545 sym->name, &sym->declared_at);
17546 return;
17547 }
17548 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
17549 {
17550 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
17551 "either be a scalar or an assumed-size array",
17552 sym->name, &sym->declared_at);
17553 return;
17554 }
17555
17556 /* Set the type to TYPE(*) and add a dimension(*) to ensure
17557 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
17558 packing. */
17559 sym->ts.type = BT_ASSUMED;
17560 sym->as = gfc_get_array_spec ();
17561 sym->as->type = AS_ASSUMED_SIZE;
17562 sym->as->rank = 1;
17563 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
17564 }
17565 else if (sym->ts.type == BT_ASSUMED)
17566 {
17567 /* TS 29113, C407a. */
17568 if (!sym->attr.dummy)
17569 {
17570 gfc_error ("Assumed type of variable %s at %L is only permitted "
17571 "for dummy variables", sym->name, &sym->declared_at);
17572 return;
17573 }
17574 if (sym->attr.allocatable || sym->attr.codimension
17575 || sym->attr.pointer || sym->attr.value)
17576 {
17577 gfc_error ("Assumed-type variable %s at %L may not have the "
17578 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
17579 sym->name, &sym->declared_at);
17580 return;
17581 }
17582 if (sym->attr.intent == INTENT_OUT)
17583 {
17584 gfc_error ("Assumed-type variable %s at %L may not have the "
17585 "INTENT(OUT) attribute",
17586 sym->name, &sym->declared_at);
17587 return;
17588 }
17589 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
17590 {
17591 gfc_error ("Assumed-type variable %s at %L shall not be an "
17592 "explicit-shape array", sym->name, &sym->declared_at);
17593 return;
17594 }
17595 }
17596
17597 /* If the symbol is marked as bind(c), that it is declared at module level
17598 scope and verify its type and kind. Do not do the latter for symbols
17599 that are implicitly typed because that is handled in
17600 gfc_set_default_type. Handle dummy arguments and procedure definitions
17601 separately. Also, anything that is use associated is not handled here
17602 but instead is handled in the module it is declared in. Finally, derived
17603 type definitions are allowed to be BIND(C) since that only implies that
17604 they're interoperable, and they are checked fully for interoperability
17605 when a variable is declared of that type. */
17606 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
17607 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
17608 && sym->attr.flavor != FL_DERIVED)
17609 {
17610 bool t = true;
17611
17612 /* First, make sure the variable is declared at the
17613 module-level scope (J3/04-007, Section 15.3). */
17614 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
17615 && !sym->attr.in_common)
17616 {
17617 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
17618 "is neither a COMMON block nor declared at the "
17619 "module level scope", sym->name, &(sym->declared_at));
17620 t = false;
17621 }
17622 else if (sym->ts.type == BT_CHARACTER
17623 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
17624 || !gfc_is_constant_expr (sym->ts.u.cl->length)
17625 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
17626 {
17627 gfc_error ("BIND(C) Variable %qs at %L must have length one",
17628 sym->name, &sym->declared_at);
17629 t = false;
17630 }
17631 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
17632 {
17633 t = verify_com_block_vars_c_interop (sym->common_head);
17634 }
17635 else if (sym->attr.implicit_type == 0)
17636 {
17637 /* If type() declaration, we need to verify that the components
17638 of the given type are all C interoperable, etc. */
17639 if (sym->ts.type == BT_DERIVED &&
17640 sym->ts.u.derived->attr.is_c_interop != 1)
17641 {
17642 /* Make sure the user marked the derived type as BIND(C). If
17643 not, call the verify routine. This could print an error
17644 for the derived type more than once if multiple variables
17645 of that type are declared. */
17646 if (sym->ts.u.derived->attr.is_bind_c != 1)
17647 verify_bind_c_derived_type (sym->ts.u.derived);
17648 t = false;
17649 }
17650
17651 /* Verify the variable itself as C interoperable if it
17652 is BIND(C). It is not possible for this to succeed if
17653 the verify_bind_c_derived_type failed, so don't have to handle
17654 any error returned by verify_bind_c_derived_type. */
17655 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
17656 sym->common_block);
17657 }
17658
17659 if (!t)
17660 {
17661 /* clear the is_bind_c flag to prevent reporting errors more than
17662 once if something failed. */
17663 sym->attr.is_bind_c = 0;
17664 return;
17665 }
17666 }
17667
17668 /* If a derived type symbol has reached this point, without its
17669 type being declared, we have an error. Notice that most
17670 conditions that produce undefined derived types have already
17671 been dealt with. However, the likes of:
17672 implicit type(t) (t) ..... call foo (t) will get us here if
17673 the type is not declared in the scope of the implicit
17674 statement. Change the type to BT_UNKNOWN, both because it is so
17675 and to prevent an ICE. */
17676 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17677 && sym->ts.u.derived->components == NULL
17678 && !sym->ts.u.derived->attr.zero_comp)
17679 {
17680 gfc_error ("The derived type %qs at %L is of type %qs, "
17681 "which has not been defined", sym->name,
17682 &sym->declared_at, sym->ts.u.derived->name);
17683 sym->ts.type = BT_UNKNOWN;
17684 return;
17685 }
17686
17687 /* Make sure that the derived type has been resolved and that the
17688 derived type is visible in the symbol's namespace, if it is a
17689 module function and is not PRIVATE. */
17690 if (sym->ts.type == BT_DERIVED
17691 && sym->ts.u.derived->attr.use_assoc
17692 && sym->ns->proc_name
17693 && sym->ns->proc_name->attr.flavor == FL_MODULE
17694 && !resolve_fl_derived (sym->ts.u.derived))
17695 return;
17696
17697 /* Unless the derived-type declaration is use associated, Fortran 95
17698 does not allow public entries of private derived types.
17699 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
17700 161 in 95-006r3. */
17701 if (sym->ts.type == BT_DERIVED
17702 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
17703 && !sym->ts.u.derived->attr.use_assoc
17704 && gfc_check_symbol_access (sym)
17705 && !gfc_check_symbol_access (sym->ts.u.derived)
17706 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
17707 "derived type %qs",
17708 (sym->attr.flavor == FL_PARAMETER)
17709 ? "parameter" : "variable",
17710 sym->name, &sym->declared_at,
17711 sym->ts.u.derived->name))
17712 return;
17713
17714 /* F2008, C1302. */
17715 if (sym->ts.type == BT_DERIVED
17716 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17717 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
17718 || sym->ts.u.derived->attr.lock_comp)
17719 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17720 {
17721 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
17722 "type LOCK_TYPE must be a coarray", sym->name,
17723 &sym->declared_at);
17724 return;
17725 }
17726
17727 /* TS18508, C702/C703. */
17728 if (sym->ts.type == BT_DERIVED
17729 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17730 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
17731 || sym->ts.u.derived->attr.event_comp)
17732 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17733 {
17734 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
17735 "type EVENT_TYPE must be a coarray", sym->name,
17736 &sym->declared_at);
17737 return;
17738 }
17739
17740 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
17741 default initialization is defined (5.1.2.4.4). */
17742 if (sym->ts.type == BT_DERIVED
17743 && sym->attr.dummy
17744 && sym->attr.intent == INTENT_OUT
17745 && sym->as
17746 && sym->as->type == AS_ASSUMED_SIZE)
17747 {
17748 for (c = sym->ts.u.derived->components; c; c = c->next)
17749 {
17750 if (c->initializer)
17751 {
17752 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
17753 "ASSUMED SIZE and so cannot have a default initializer",
17754 sym->name, &sym->declared_at);
17755 return;
17756 }
17757 }
17758 }
17759
17760 /* F2008, C542. */
17761 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17762 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
17763 {
17764 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
17765 "INTENT(OUT)", sym->name, &sym->declared_at);
17766 return;
17767 }
17768
17769 /* TS18508. */
17770 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17771 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
17772 {
17773 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
17774 "INTENT(OUT)", sym->name, &sym->declared_at);
17775 return;
17776 }
17777
17778 /* F2008, C525. */
17779 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17780 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17781 && sym->ts.u.derived && CLASS_DATA (sym)
17782 && CLASS_DATA (sym)->attr.coarray_comp))
17783 || class_attr.codimension)
17784 && (sym->attr.result || sym->result == sym))
17785 {
17786 gfc_error ("Function result %qs at %L shall not be a coarray or have "
17787 "a coarray component", sym->name, &sym->declared_at);
17788 return;
17789 }
17790
17791 /* F2008, C524. */
17792 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
17793 && sym->ts.u.derived->ts.is_iso_c)
17794 {
17795 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
17796 "shall not be a coarray", sym->name, &sym->declared_at);
17797 return;
17798 }
17799
17800 /* F2008, C525. */
17801 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17802 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17803 && sym->ts.u.derived && CLASS_DATA (sym)
17804 && CLASS_DATA (sym)->attr.coarray_comp))
17805 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
17806 || class_attr.allocatable))
17807 {
17808 gfc_error ("Variable %qs at %L with coarray component shall be a "
17809 "nonpointer, nonallocatable scalar, which is not a coarray",
17810 sym->name, &sym->declared_at);
17811 return;
17812 }
17813
17814 /* F2008, C526. The function-result case was handled above. */
17815 if (class_attr.codimension
17816 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
17817 || sym->attr.select_type_temporary
17818 || sym->attr.associate_var
17819 || (sym->ns->save_all && !sym->attr.automatic)
17820 || sym->ns->proc_name->attr.flavor == FL_MODULE
17821 || sym->ns->proc_name->attr.is_main_program
17822 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
17823 {
17824 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
17825 "nor a dummy argument", sym->name, &sym->declared_at);
17826 return;
17827 }
17828 /* F2008, C528. */
17829 else if (class_attr.codimension && !sym->attr.select_type_temporary
17830 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
17831 {
17832 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
17833 "deferred shape without allocatable", sym->name,
17834 &sym->declared_at);
17835 return;
17836 }
17837 else if (class_attr.codimension && class_attr.allocatable && as
17838 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
17839 {
17840 gfc_error ("Allocatable coarray variable %qs at %L must have "
17841 "deferred shape", sym->name, &sym->declared_at);
17842 return;
17843 }
17844
17845 /* F2008, C541. */
17846 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17847 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17848 && sym->ts.u.derived && CLASS_DATA (sym)
17849 && CLASS_DATA (sym)->attr.coarray_comp))
17850 || (class_attr.codimension && class_attr.allocatable))
17851 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
17852 {
17853 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
17854 "allocatable coarray or have coarray components",
17855 sym->name, &sym->declared_at);
17856 return;
17857 }
17858
17859 if (class_attr.codimension && sym->attr.dummy
17860 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
17861 {
17862 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
17863 "procedure %qs", sym->name, &sym->declared_at,
17864 sym->ns->proc_name->name);
17865 return;
17866 }
17867
17868 if (sym->ts.type == BT_LOGICAL
17869 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
17870 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
17871 && sym->ns->proc_name->attr.is_bind_c)))
17872 {
17873 int i;
17874 for (i = 0; gfc_logical_kinds[i].kind; i++)
17875 if (gfc_logical_kinds[i].kind == sym->ts.kind)
17876 break;
17877 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
17878 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
17879 "%L with non-C_Bool kind in BIND(C) procedure "
17880 "%qs", sym->name, &sym->declared_at,
17881 sym->ns->proc_name->name))
17882 return;
17883 else if (!gfc_logical_kinds[i].c_bool
17884 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
17885 "%qs at %L with non-C_Bool kind in "
17886 "BIND(C) procedure %qs", sym->name,
17887 &sym->declared_at,
17888 sym->attr.function ? sym->name
17889 : sym->ns->proc_name->name))
17890 return;
17891 }
17892
17893 switch (sym->attr.flavor)
17894 {
17895 case FL_VARIABLE:
17896 if (!resolve_fl_variable (sym, mp_flag))
17897 return;
17898 break;
17899
17900 case FL_PROCEDURE:
17901 if (sym->formal && !sym->formal_ns)
17902 {
17903 /* Check that none of the arguments are a namelist. */
17904 gfc_formal_arglist *formal = sym->formal;
17905
17906 for (; formal; formal = formal->next)
17907 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
17908 {
17909 gfc_error ("Namelist %qs cannot be an argument to "
17910 "subroutine or function at %L",
17911 formal->sym->name, &sym->declared_at);
17912 return;
17913 }
17914 }
17915
17916 if (!resolve_fl_procedure (sym, mp_flag))
17917 return;
17918 break;
17919
17920 case FL_NAMELIST:
17921 if (!resolve_fl_namelist (sym))
17922 return;
17923 break;
17924
17925 case FL_PARAMETER:
17926 if (!resolve_fl_parameter (sym))
17927 return;
17928 break;
17929
17930 default:
17931 break;
17932 }
17933
17934 /* Resolve array specifier. Check as well some constraints
17935 on COMMON blocks. */
17936
17937 check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
17938
17939 resolve_symbol_array_spec (sym, check_constant);
17940
17941 /* Resolve formal namespaces. */
17942 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
17943 && !sym->attr.contained && !sym->attr.intrinsic)
17944 gfc_resolve (sym->formal_ns);
17945
17946 /* Make sure the formal namespace is present. */
17947 if (sym->formal && !sym->formal_ns)
17948 {
17949 gfc_formal_arglist *formal = sym->formal;
17950 while (formal && !formal->sym)
17951 formal = formal->next;
17952
17953 if (formal)
17954 {
17955 sym->formal_ns = formal->sym->ns;
17956 if (sym->formal_ns && sym->ns != formal->sym->ns)
17957 sym->formal_ns->refs++;
17958 }
17959 }
17960
17961 /* Check threadprivate restrictions. */
17962 if (sym->attr.threadprivate
17963 && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
17964 && !(sym->ns->save_all && !sym->attr.automatic)
17965 && sym->module == NULL
17966 && (sym->ns->proc_name == NULL
17967 || (sym->ns->proc_name->attr.flavor != FL_MODULE
17968 && !sym->ns->proc_name->attr.is_main_program)))
17969 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
17970
17971 /* Check omp declare target restrictions. */
17972 if (sym->attr.omp_declare_target
17973 && sym->attr.flavor == FL_VARIABLE
17974 && !sym->attr.save
17975 && !(sym->ns->save_all && !sym->attr.automatic)
17976 && (!sym->attr.in_common
17977 && sym->module == NULL
17978 && (sym->ns->proc_name == NULL
17979 || (sym->ns->proc_name->attr.flavor != FL_MODULE
17980 && !sym->ns->proc_name->attr.is_main_program))))
17981 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
17982 sym->name, &sym->declared_at);
17983
17984 /* If we have come this far we can apply default-initializers, as
17985 described in 14.7.5, to those variables that have not already
17986 been assigned one. */
17987 if (sym->ts.type == BT_DERIVED
17988 && !sym->value
17989 && !sym->attr.allocatable
17990 && !sym->attr.alloc_comp)
17991 {
17992 symbol_attribute *a = &sym->attr;
17993
17994 if ((!a->save && !a->dummy && !a->pointer
17995 && !a->in_common && !a->use_assoc
17996 && a->referenced
17997 && !((a->function || a->result)
17998 && (!a->dimension
17999 || sym->ts.u.derived->attr.alloc_comp
18000 || sym->ts.u.derived->attr.pointer_comp))
18001 && !(a->function && sym != sym->result))
18002 || (a->dummy && !a->pointer && a->intent == INTENT_OUT
18003 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
18004 apply_default_init (sym);
18005 else if (a->function && !a->pointer && !a->allocatable
18006 && !a->use_assoc && !a->used_in_submodule && sym->result)
18007 /* Default initialization for function results. */
18008 apply_default_init (sym->result);
18009 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
18010 && (sym->ts.u.derived->attr.alloc_comp
18011 || sym->ts.u.derived->attr.pointer_comp))
18012 /* Mark the result symbol to be referenced, when it has allocatable
18013 components. */
18014 sym->result->attr.referenced = 1;
18015 }
18016
18017 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
18018 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
18019 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
18020 && !CLASS_DATA (sym)->attr.class_pointer
18021 && !CLASS_DATA (sym)->attr.allocatable)
18022 apply_default_init (sym);
18023
18024 /* If this symbol has a type-spec, check it. */
18025 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
18026 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
18027 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
18028 return;
18029
18030 if (sym->param_list)
18031 resolve_pdt (sym);
18032 }
18033
18034
18035 /************* Resolve DATA statements *************/
18036
18037 static struct
18038 {
18039 gfc_data_value *vnode;
18040 mpz_t left;
18041 }
18042 values;
18043
18044
18045 /* Advance the values structure to point to the next value in the data list. */
18046
18047 static bool
18048 next_data_value (void)
18049 {
18050 while (mpz_cmp_ui (values.left, 0) == 0)
18051 {
18052
18053 if (values.vnode->next == NULL)
18054 return false;
18055
18056 values.vnode = values.vnode->next;
18057 mpz_set (values.left, values.vnode->repeat);
18058 }
18059
18060 return true;
18061 }
18062
18063
18064 static bool
18065 check_data_variable (gfc_data_variable *var, locus *where)
18066 {
18067 gfc_expr *e;
18068 mpz_t size;
18069 mpz_t offset;
18070 bool t;
18071 ar_type mark = AR_UNKNOWN;
18072 int i;
18073 mpz_t section_index[GFC_MAX_DIMENSIONS];
18074 int vector_offset[GFC_MAX_DIMENSIONS];
18075 gfc_ref *ref;
18076 gfc_array_ref *ar;
18077 gfc_symbol *sym;
18078 int has_pointer;
18079
18080 if (!gfc_resolve_expr (var->expr))
18081 return false;
18082
18083 ar = NULL;
18084 e = var->expr;
18085
18086 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
18087 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
18088 e = e->value.function.actual->expr;
18089
18090 if (e->expr_type != EXPR_VARIABLE)
18091 {
18092 gfc_error ("Expecting definable entity near %L", where);
18093 return false;
18094 }
18095
18096 sym = e->symtree->n.sym;
18097
18098 if (sym->ns->is_block_data && !sym->attr.in_common)
18099 {
18100 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
18101 sym->name, &sym->declared_at);
18102 return false;
18103 }
18104
18105 if (e->ref == NULL && sym->as)
18106 {
18107 gfc_error ("DATA array %qs at %L must be specified in a previous"
18108 " declaration", sym->name, where);
18109 return false;
18110 }
18111
18112 if (gfc_is_coindexed (e))
18113 {
18114 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
18115 where);
18116 return false;
18117 }
18118
18119 has_pointer = sym->attr.pointer;
18120
18121 for (ref = e->ref; ref; ref = ref->next)
18122 {
18123 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
18124 has_pointer = 1;
18125
18126 if (has_pointer)
18127 {
18128 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
18129 {
18130 gfc_error ("DATA element %qs at %L is a pointer and so must "
18131 "be a full array", sym->name, where);
18132 return false;
18133 }
18134
18135 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
18136 {
18137 gfc_error ("DATA object near %L has the pointer attribute "
18138 "and the corresponding DATA value is not a valid "
18139 "initial-data-target", where);
18140 return false;
18141 }
18142 }
18143
18144 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
18145 {
18146 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
18147 "attribute", ref->u.c.component->name, &e->where);
18148 return false;
18149 }
18150
18151 /* Reject substrings of strings of non-constant length. */
18152 if (ref->type == REF_SUBSTRING
18153 && ref->u.ss.length
18154 && ref->u.ss.length->length
18155 && !gfc_is_constant_expr (ref->u.ss.length->length))
18156 goto bad_charlen;
18157 }
18158
18159 /* Reject strings with deferred length or non-constant length. */
18160 if (e->ts.type == BT_CHARACTER
18161 && (e->ts.deferred
18162 || (e->ts.u.cl->length
18163 && !gfc_is_constant_expr (e->ts.u.cl->length))))
18164 goto bad_charlen;
18165
18166 mpz_init_set_si (offset, 0);
18167
18168 if (e->rank == 0 || has_pointer)
18169 {
18170 mpz_init_set_ui (size, 1);
18171 ref = NULL;
18172 }
18173 else
18174 {
18175 ref = e->ref;
18176
18177 /* Find the array section reference. */
18178 for (ref = e->ref; ref; ref = ref->next)
18179 {
18180 if (ref->type != REF_ARRAY)
18181 continue;
18182 if (ref->u.ar.type == AR_ELEMENT)
18183 continue;
18184 break;
18185 }
18186 gcc_assert (ref);
18187
18188 /* Set marks according to the reference pattern. */
18189 switch (ref->u.ar.type)
18190 {
18191 case AR_FULL:
18192 mark = AR_FULL;
18193 break;
18194
18195 case AR_SECTION:
18196 ar = &ref->u.ar;
18197 /* Get the start position of array section. */
18198 gfc_get_section_index (ar, section_index, &offset, vector_offset);
18199 mark = AR_SECTION;
18200 break;
18201
18202 default:
18203 gcc_unreachable ();
18204 }
18205
18206 if (!gfc_array_size (e, &size))
18207 {
18208 gfc_error ("Nonconstant array section at %L in DATA statement",
18209 where);
18210 mpz_clear (offset);
18211 return false;
18212 }
18213 }
18214
18215 t = true;
18216
18217 while (mpz_cmp_ui (size, 0) > 0)
18218 {
18219 if (!next_data_value ())
18220 {
18221 gfc_error ("DATA statement at %L has more variables than values",
18222 where);
18223 t = false;
18224 break;
18225 }
18226
18227 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
18228 if (!t)
18229 break;
18230
18231 /* If we have more than one element left in the repeat count,
18232 and we have more than one element left in the target variable,
18233 then create a range assignment. */
18234 /* FIXME: Only done for full arrays for now, since array sections
18235 seem tricky. */
18236 if (mark == AR_FULL && ref && ref->next == NULL
18237 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
18238 {
18239 mpz_t range;
18240
18241 if (mpz_cmp (size, values.left) >= 0)
18242 {
18243 mpz_init_set (range, values.left);
18244 mpz_sub (size, size, values.left);
18245 mpz_set_ui (values.left, 0);
18246 }
18247 else
18248 {
18249 mpz_init_set (range, size);
18250 mpz_sub (values.left, values.left, size);
18251 mpz_set_ui (size, 0);
18252 }
18253
18254 t = gfc_assign_data_value (var->expr, values.vnode->expr,
18255 offset, &range);
18256
18257 mpz_add (offset, offset, range);
18258 mpz_clear (range);
18259
18260 if (!t)
18261 break;
18262 }
18263
18264 /* Assign initial value to symbol. */
18265 else
18266 {
18267 mpz_sub_ui (values.left, values.left, 1);
18268 mpz_sub_ui (size, size, 1);
18269
18270 t = gfc_assign_data_value (var->expr, values.vnode->expr,
18271 offset, NULL);
18272 if (!t)
18273 break;
18274
18275 if (mark == AR_FULL)
18276 mpz_add_ui (offset, offset, 1);
18277
18278 /* Modify the array section indexes and recalculate the offset
18279 for next element. */
18280 else if (mark == AR_SECTION)
18281 gfc_advance_section (section_index, ar, &offset, vector_offset);
18282 }
18283 }
18284
18285 if (mark == AR_SECTION)
18286 {
18287 for (i = 0; i < ar->dimen; i++)
18288 mpz_clear (section_index[i]);
18289 }
18290
18291 mpz_clear (size);
18292 mpz_clear (offset);
18293
18294 return t;
18295
18296 bad_charlen:
18297 gfc_error ("Non-constant character length at %L in DATA statement",
18298 &e->where);
18299 return false;
18300 }
18301
18302
18303 static bool traverse_data_var (gfc_data_variable *, locus *);
18304
18305 /* Iterate over a list of elements in a DATA statement. */
18306
18307 static bool
18308 traverse_data_list (gfc_data_variable *var, locus *where)
18309 {
18310 mpz_t trip;
18311 iterator_stack frame;
18312 gfc_expr *e, *start, *end, *step;
18313 bool retval = true;
18314
18315 mpz_init (frame.value);
18316 mpz_init (trip);
18317
18318 start = gfc_copy_expr (var->iter.start);
18319 end = gfc_copy_expr (var->iter.end);
18320 step = gfc_copy_expr (var->iter.step);
18321
18322 if (!gfc_simplify_expr (start, 1)
18323 || start->expr_type != EXPR_CONSTANT)
18324 {
18325 gfc_error ("start of implied-do loop at %L could not be "
18326 "simplified to a constant value", &start->where);
18327 retval = false;
18328 goto cleanup;
18329 }
18330 if (!gfc_simplify_expr (end, 1)
18331 || end->expr_type != EXPR_CONSTANT)
18332 {
18333 gfc_error ("end of implied-do loop at %L could not be "
18334 "simplified to a constant value", &end->where);
18335 retval = false;
18336 goto cleanup;
18337 }
18338 if (!gfc_simplify_expr (step, 1)
18339 || step->expr_type != EXPR_CONSTANT)
18340 {
18341 gfc_error ("step of implied-do loop at %L could not be "
18342 "simplified to a constant value", &step->where);
18343 retval = false;
18344 goto cleanup;
18345 }
18346 if (mpz_cmp_si (step->value.integer, 0) == 0)
18347 {
18348 gfc_error ("step of implied-do loop at %L shall not be zero",
18349 &step->where);
18350 retval = false;
18351 goto cleanup;
18352 }
18353
18354 mpz_set (trip, end->value.integer);
18355 mpz_sub (trip, trip, start->value.integer);
18356 mpz_add (trip, trip, step->value.integer);
18357
18358 mpz_div (trip, trip, step->value.integer);
18359
18360 mpz_set (frame.value, start->value.integer);
18361
18362 frame.prev = iter_stack;
18363 frame.variable = var->iter.var->symtree;
18364 iter_stack = &frame;
18365
18366 while (mpz_cmp_ui (trip, 0) > 0)
18367 {
18368 if (!traverse_data_var (var->list, where))
18369 {
18370 retval = false;
18371 goto cleanup;
18372 }
18373
18374 e = gfc_copy_expr (var->expr);
18375 if (!gfc_simplify_expr (e, 1))
18376 {
18377 gfc_free_expr (e);
18378 retval = false;
18379 goto cleanup;
18380 }
18381
18382 mpz_add (frame.value, frame.value, step->value.integer);
18383
18384 mpz_sub_ui (trip, trip, 1);
18385 }
18386
18387 cleanup:
18388 mpz_clear (frame.value);
18389 mpz_clear (trip);
18390
18391 gfc_free_expr (start);
18392 gfc_free_expr (end);
18393 gfc_free_expr (step);
18394
18395 iter_stack = frame.prev;
18396 return retval;
18397 }
18398
18399
18400 /* Type resolve variables in the variable list of a DATA statement. */
18401
18402 static bool
18403 traverse_data_var (gfc_data_variable *var, locus *where)
18404 {
18405 bool t;
18406
18407 for (; var; var = var->next)
18408 {
18409 if (var->expr == NULL)
18410 t = traverse_data_list (var, where);
18411 else
18412 t = check_data_variable (var, where);
18413
18414 if (!t)
18415 return false;
18416 }
18417
18418 return true;
18419 }
18420
18421
18422 /* Resolve the expressions and iterators associated with a data statement.
18423 This is separate from the assignment checking because data lists should
18424 only be resolved once. */
18425
18426 static bool
18427 resolve_data_variables (gfc_data_variable *d)
18428 {
18429 for (; d; d = d->next)
18430 {
18431 if (d->list == NULL)
18432 {
18433 if (!gfc_resolve_expr (d->expr))
18434 return false;
18435 }
18436 else
18437 {
18438 if (!gfc_resolve_iterator (&d->iter, false, true))
18439 return false;
18440
18441 if (!resolve_data_variables (d->list))
18442 return false;
18443 }
18444 }
18445
18446 return true;
18447 }
18448
18449
18450 /* Resolve a single DATA statement. We implement this by storing a pointer to
18451 the value list into static variables, and then recursively traversing the
18452 variables list, expanding iterators and such. */
18453
18454 static void
18455 resolve_data (gfc_data *d)
18456 {
18457
18458 if (!resolve_data_variables (d->var))
18459 return;
18460
18461 values.vnode = d->value;
18462 if (d->value == NULL)
18463 mpz_set_ui (values.left, 0);
18464 else
18465 mpz_set (values.left, d->value->repeat);
18466
18467 if (!traverse_data_var (d->var, &d->where))
18468 return;
18469
18470 /* At this point, we better not have any values left. */
18471
18472 if (next_data_value ())
18473 gfc_error ("DATA statement at %L has more values than variables",
18474 &d->where);
18475 }
18476
18477
18478 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
18479 accessed by host or use association, is a dummy argument to a pure function,
18480 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
18481 is storage associated with any such variable, shall not be used in the
18482 following contexts: (clients of this function). */
18483
18484 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
18485 procedure. Returns zero if assignment is OK, nonzero if there is a
18486 problem. */
18487 bool
18488 gfc_impure_variable (gfc_symbol *sym)
18489 {
18490 gfc_symbol *proc;
18491 gfc_namespace *ns;
18492
18493 if (sym->attr.use_assoc || sym->attr.in_common)
18494 return 1;
18495
18496 /* The namespace of a module procedure interface holds the arguments and
18497 symbols, and so the symbol namespace can be different to that of the
18498 procedure. */
18499 if (sym->ns != gfc_current_ns
18500 && gfc_current_ns->proc_name->abr_modproc_decl
18501 && sym->ns->proc_name->attr.function
18502 && sym->attr.result
18503 && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
18504 return 0;
18505
18506 /* Check if the symbol's ns is inside the pure procedure. */
18507 for (ns = gfc_current_ns; ns; ns = ns->parent)
18508 {
18509 if (ns == sym->ns)
18510 break;
18511 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
18512 return 1;
18513 }
18514
18515 proc = sym->ns->proc_name;
18516 if (sym->attr.dummy
18517 && !sym->attr.value
18518 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
18519 || proc->attr.function))
18520 return 1;
18521
18522 /* TODO: Sort out what can be storage associated, if anything, and include
18523 it here. In principle equivalences should be scanned but it does not
18524 seem to be possible to storage associate an impure variable this way. */
18525 return 0;
18526 }
18527
18528
18529 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
18530 current namespace is inside a pure procedure. */
18531
18532 bool
18533 gfc_pure (gfc_symbol *sym)
18534 {
18535 symbol_attribute attr;
18536 gfc_namespace *ns;
18537
18538 if (sym == NULL)
18539 {
18540 /* Check if the current namespace or one of its parents
18541 belongs to a pure procedure. */
18542 for (ns = gfc_current_ns; ns; ns = ns->parent)
18543 {
18544 sym = ns->proc_name;
18545 if (sym == NULL)
18546 return 0;
18547 attr = sym->attr;
18548 if (attr.flavor == FL_PROCEDURE && attr.pure)
18549 return 1;
18550 }
18551 return 0;
18552 }
18553
18554 attr = sym->attr;
18555
18556 return attr.flavor == FL_PROCEDURE && attr.pure;
18557 }
18558
18559
18560 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
18561 checks if the current namespace is implicitly pure. Note that this
18562 function returns false for a PURE procedure. */
18563
18564 bool
18565 gfc_implicit_pure (gfc_symbol *sym)
18566 {
18567 gfc_namespace *ns;
18568
18569 if (sym == NULL)
18570 {
18571 /* Check if the current procedure is implicit_pure. Walk up
18572 the procedure list until we find a procedure. */
18573 for (ns = gfc_current_ns; ns; ns = ns->parent)
18574 {
18575 sym = ns->proc_name;
18576 if (sym == NULL)
18577 return 0;
18578
18579 if (sym->attr.flavor == FL_PROCEDURE)
18580 break;
18581 }
18582 }
18583
18584 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
18585 && !sym->attr.pure;
18586 }
18587
18588
18589 void
18590 gfc_unset_implicit_pure (gfc_symbol *sym)
18591 {
18592 gfc_namespace *ns;
18593
18594 if (sym == NULL)
18595 {
18596 /* Check if the current procedure is implicit_pure. Walk up
18597 the procedure list until we find a procedure. */
18598 for (ns = gfc_current_ns; ns; ns = ns->parent)
18599 {
18600 sym = ns->proc_name;
18601 if (sym == NULL)
18602 return;
18603
18604 if (sym->attr.flavor == FL_PROCEDURE)
18605 break;
18606 }
18607 }
18608
18609 if (sym->attr.flavor == FL_PROCEDURE)
18610 sym->attr.implicit_pure = 0;
18611 else
18612 sym->attr.pure = 0;
18613 }
18614
18615
18616 /* Test whether the current procedure is elemental or not. */
18617
18618 bool
18619 gfc_elemental (gfc_symbol *sym)
18620 {
18621 symbol_attribute attr;
18622
18623 if (sym == NULL)
18624 sym = gfc_current_ns->proc_name;
18625 if (sym == NULL)
18626 return 0;
18627 attr = sym->attr;
18628
18629 return attr.flavor == FL_PROCEDURE && attr.elemental;
18630 }
18631
18632
18633 /* Warn about unused labels. */
18634
18635 static void
18636 warn_unused_fortran_label (gfc_st_label *label)
18637 {
18638 if (label == NULL)
18639 return;
18640
18641 warn_unused_fortran_label (label->left);
18642
18643 if (label->defined == ST_LABEL_UNKNOWN)
18644 return;
18645
18646 switch (label->referenced)
18647 {
18648 case ST_LABEL_UNKNOWN:
18649 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
18650 label->value, &label->where);
18651 break;
18652
18653 case ST_LABEL_BAD_TARGET:
18654 gfc_warning (OPT_Wunused_label,
18655 "Label %d at %L defined but cannot be used",
18656 label->value, &label->where);
18657 break;
18658
18659 default:
18660 break;
18661 }
18662
18663 warn_unused_fortran_label (label->right);
18664 }
18665
18666
18667 /* Returns the sequence type of a symbol or sequence. */
18668
18669 static seq_type
18670 sequence_type (gfc_typespec ts)
18671 {
18672 seq_type result;
18673 gfc_component *c;
18674
18675 switch (ts.type)
18676 {
18677 case BT_DERIVED:
18678
18679 if (ts.u.derived->components == NULL)
18680 return SEQ_NONDEFAULT;
18681
18682 result = sequence_type (ts.u.derived->components->ts);
18683 for (c = ts.u.derived->components->next; c; c = c->next)
18684 if (sequence_type (c->ts) != result)
18685 return SEQ_MIXED;
18686
18687 return result;
18688
18689 case BT_CHARACTER:
18690 if (ts.kind != gfc_default_character_kind)
18691 return SEQ_NONDEFAULT;
18692
18693 return SEQ_CHARACTER;
18694
18695 case BT_INTEGER:
18696 if (ts.kind != gfc_default_integer_kind)
18697 return SEQ_NONDEFAULT;
18698
18699 return SEQ_NUMERIC;
18700
18701 case BT_REAL:
18702 if (!(ts.kind == gfc_default_real_kind
18703 || ts.kind == gfc_default_double_kind))
18704 return SEQ_NONDEFAULT;
18705
18706 return SEQ_NUMERIC;
18707
18708 case BT_COMPLEX:
18709 if (ts.kind != gfc_default_complex_kind)
18710 return SEQ_NONDEFAULT;
18711
18712 return SEQ_NUMERIC;
18713
18714 case BT_LOGICAL:
18715 if (ts.kind != gfc_default_logical_kind)
18716 return SEQ_NONDEFAULT;
18717
18718 return SEQ_NUMERIC;
18719
18720 default:
18721 return SEQ_NONDEFAULT;
18722 }
18723 }
18724
18725
18726 /* Resolve derived type EQUIVALENCE object. */
18727
18728 static bool
18729 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
18730 {
18731 gfc_component *c = derived->components;
18732
18733 if (!derived)
18734 return true;
18735
18736 /* Shall not be an object of nonsequence derived type. */
18737 if (!derived->attr.sequence)
18738 {
18739 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
18740 "attribute to be an EQUIVALENCE object", sym->name,
18741 &e->where);
18742 return false;
18743 }
18744
18745 /* Shall not have allocatable components. */
18746 if (derived->attr.alloc_comp)
18747 {
18748 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
18749 "components to be an EQUIVALENCE object",sym->name,
18750 &e->where);
18751 return false;
18752 }
18753
18754 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
18755 {
18756 gfc_error ("Derived type variable %qs at %L with default "
18757 "initialization cannot be in EQUIVALENCE with a variable "
18758 "in COMMON", sym->name, &e->where);
18759 return false;
18760 }
18761
18762 for (; c ; c = c->next)
18763 {
18764 if (gfc_bt_struct (c->ts.type)
18765 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
18766 return false;
18767
18768 /* Shall not be an object of sequence derived type containing a pointer
18769 in the structure. */
18770 if (c->attr.pointer)
18771 {
18772 gfc_error ("Derived type variable %qs at %L with pointer "
18773 "component(s) cannot be an EQUIVALENCE object",
18774 sym->name, &e->where);
18775 return false;
18776 }
18777 }
18778 return true;
18779 }
18780
18781
18782 /* Resolve equivalence object.
18783 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
18784 an allocatable array, an object of nonsequence derived type, an object of
18785 sequence derived type containing a pointer at any level of component
18786 selection, an automatic object, a function name, an entry name, a result
18787 name, a named constant, a structure component, or a subobject of any of
18788 the preceding objects. A substring shall not have length zero. A
18789 derived type shall not have components with default initialization nor
18790 shall two objects of an equivalence group be initialized.
18791 Either all or none of the objects shall have an protected attribute.
18792 The simple constraints are done in symbol.cc(check_conflict) and the rest
18793 are implemented here. */
18794
18795 static void
18796 resolve_equivalence (gfc_equiv *eq)
18797 {
18798 gfc_symbol *sym;
18799 gfc_symbol *first_sym;
18800 gfc_expr *e;
18801 gfc_ref *r;
18802 locus *last_where = NULL;
18803 seq_type eq_type, last_eq_type;
18804 gfc_typespec *last_ts;
18805 int object, cnt_protected;
18806 const char *msg;
18807
18808 last_ts = &eq->expr->symtree->n.sym->ts;
18809
18810 first_sym = eq->expr->symtree->n.sym;
18811
18812 cnt_protected = 0;
18813
18814 for (object = 1; eq; eq = eq->eq, object++)
18815 {
18816 e = eq->expr;
18817
18818 e->ts = e->symtree->n.sym->ts;
18819 /* match_varspec might not know yet if it is seeing
18820 array reference or substring reference, as it doesn't
18821 know the types. */
18822 if (e->ref && e->ref->type == REF_ARRAY)
18823 {
18824 gfc_ref *ref = e->ref;
18825 sym = e->symtree->n.sym;
18826
18827 if (sym->attr.dimension)
18828 {
18829 ref->u.ar.as = sym->as;
18830 ref = ref->next;
18831 }
18832
18833 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
18834 if (e->ts.type == BT_CHARACTER
18835 && ref
18836 && ref->type == REF_ARRAY
18837 && ref->u.ar.dimen == 1
18838 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
18839 && ref->u.ar.stride[0] == NULL)
18840 {
18841 gfc_expr *start = ref->u.ar.start[0];
18842 gfc_expr *end = ref->u.ar.end[0];
18843 void *mem = NULL;
18844
18845 /* Optimize away the (:) reference. */
18846 if (start == NULL && end == NULL)
18847 {
18848 if (e->ref == ref)
18849 e->ref = ref->next;
18850 else
18851 e->ref->next = ref->next;
18852 mem = ref;
18853 }
18854 else
18855 {
18856 ref->type = REF_SUBSTRING;
18857 if (start == NULL)
18858 start = gfc_get_int_expr (gfc_charlen_int_kind,
18859 NULL, 1);
18860 ref->u.ss.start = start;
18861 if (end == NULL && e->ts.u.cl)
18862 end = gfc_copy_expr (e->ts.u.cl->length);
18863 ref->u.ss.end = end;
18864 ref->u.ss.length = e->ts.u.cl;
18865 e->ts.u.cl = NULL;
18866 }
18867 ref = ref->next;
18868 free (mem);
18869 }
18870
18871 /* Any further ref is an error. */
18872 if (ref)
18873 {
18874 gcc_assert (ref->type == REF_ARRAY);
18875 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
18876 &ref->u.ar.where);
18877 continue;
18878 }
18879 }
18880
18881 if (!gfc_resolve_expr (e))
18882 continue;
18883
18884 sym = e->symtree->n.sym;
18885
18886 if (sym->attr.is_protected)
18887 cnt_protected++;
18888 if (cnt_protected > 0 && cnt_protected != object)
18889 {
18890 gfc_error ("Either all or none of the objects in the "
18891 "EQUIVALENCE set at %L shall have the "
18892 "PROTECTED attribute",
18893 &e->where);
18894 break;
18895 }
18896
18897 /* Shall not equivalence common block variables in a PURE procedure. */
18898 if (sym->ns->proc_name
18899 && sym->ns->proc_name->attr.pure
18900 && sym->attr.in_common)
18901 {
18902 /* Need to check for symbols that may have entered the pure
18903 procedure via a USE statement. */
18904 bool saw_sym = false;
18905 if (sym->ns->use_stmts)
18906 {
18907 gfc_use_rename *r;
18908 for (r = sym->ns->use_stmts->rename; r; r = r->next)
18909 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
18910 }
18911 else
18912 saw_sym = true;
18913
18914 if (saw_sym)
18915 gfc_error ("COMMON block member %qs at %L cannot be an "
18916 "EQUIVALENCE object in the pure procedure %qs",
18917 sym->name, &e->where, sym->ns->proc_name->name);
18918 break;
18919 }
18920
18921 /* Shall not be a named constant. */
18922 if (e->expr_type == EXPR_CONSTANT)
18923 {
18924 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
18925 "object", sym->name, &e->where);
18926 continue;
18927 }
18928
18929 if (e->ts.type == BT_DERIVED
18930 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
18931 continue;
18932
18933 /* Check that the types correspond correctly:
18934 Note 5.28:
18935 A numeric sequence structure may be equivalenced to another sequence
18936 structure, an object of default integer type, default real type, double
18937 precision real type, default logical type such that components of the
18938 structure ultimately only become associated to objects of the same
18939 kind. A character sequence structure may be equivalenced to an object
18940 of default character kind or another character sequence structure.
18941 Other objects may be equivalenced only to objects of the same type and
18942 kind parameters. */
18943
18944 /* Identical types are unconditionally OK. */
18945 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
18946 goto identical_types;
18947
18948 last_eq_type = sequence_type (*last_ts);
18949 eq_type = sequence_type (sym->ts);
18950
18951 /* Since the pair of objects is not of the same type, mixed or
18952 non-default sequences can be rejected. */
18953
18954 msg = G_("Sequence %s with mixed components in EQUIVALENCE "
18955 "statement at %L with different type objects");
18956 if ((object ==2
18957 && last_eq_type == SEQ_MIXED
18958 && last_where
18959 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
18960 || (eq_type == SEQ_MIXED
18961 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
18962 continue;
18963
18964 msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
18965 "statement at %L with objects of different type");
18966 if ((object ==2
18967 && last_eq_type == SEQ_NONDEFAULT
18968 && last_where
18969 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
18970 || (eq_type == SEQ_NONDEFAULT
18971 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
18972 continue;
18973
18974 msg = G_("Non-CHARACTER object %qs in default CHARACTER "
18975 "EQUIVALENCE statement at %L");
18976 if (last_eq_type == SEQ_CHARACTER
18977 && eq_type != SEQ_CHARACTER
18978 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
18979 continue;
18980
18981 msg = G_("Non-NUMERIC object %qs in default NUMERIC "
18982 "EQUIVALENCE statement at %L");
18983 if (last_eq_type == SEQ_NUMERIC
18984 && eq_type != SEQ_NUMERIC
18985 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
18986 continue;
18987
18988 identical_types:
18989
18990 last_ts =&sym->ts;
18991 last_where = &e->where;
18992
18993 if (!e->ref)
18994 continue;
18995
18996 /* Shall not be an automatic array. */
18997 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
18998 {
18999 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
19000 "an EQUIVALENCE object", sym->name, &e->where);
19001 continue;
19002 }
19003
19004 r = e->ref;
19005 while (r)
19006 {
19007 /* Shall not be a structure component. */
19008 if (r->type == REF_COMPONENT)
19009 {
19010 gfc_error ("Structure component %qs at %L cannot be an "
19011 "EQUIVALENCE object",
19012 r->u.c.component->name, &e->where);
19013 break;
19014 }
19015
19016 /* A substring shall not have length zero. */
19017 if (r->type == REF_SUBSTRING)
19018 {
19019 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
19020 {
19021 gfc_error ("Substring at %L has length zero",
19022 &r->u.ss.start->where);
19023 break;
19024 }
19025 }
19026 r = r->next;
19027 }
19028 }
19029 }
19030
19031
19032 /* Function called by resolve_fntype to flag other symbols used in the
19033 length type parameter specification of function results. */
19034
19035 static bool
19036 flag_fn_result_spec (gfc_expr *expr,
19037 gfc_symbol *sym,
19038 int *f ATTRIBUTE_UNUSED)
19039 {
19040 gfc_namespace *ns;
19041 gfc_symbol *s;
19042
19043 if (expr->expr_type == EXPR_VARIABLE)
19044 {
19045 s = expr->symtree->n.sym;
19046 for (ns = s->ns; ns; ns = ns->parent)
19047 if (!ns->parent)
19048 break;
19049
19050 if (sym == s)
19051 {
19052 gfc_error ("Self reference in character length expression "
19053 "for %qs at %L", sym->name, &expr->where);
19054 return true;
19055 }
19056
19057 if (!s->fn_result_spec
19058 && s->attr.flavor == FL_PARAMETER)
19059 {
19060 /* Function contained in a module.... */
19061 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
19062 {
19063 gfc_symtree *st;
19064 s->fn_result_spec = 1;
19065 /* Make sure that this symbol is translated as a module
19066 variable. */
19067 st = gfc_get_unique_symtree (ns);
19068 st->n.sym = s;
19069 s->refs++;
19070 }
19071 /* ... which is use associated and called. */
19072 else if (s->attr.use_assoc || s->attr.used_in_submodule
19073 ||
19074 /* External function matched with an interface. */
19075 (s->ns->proc_name
19076 && ((s->ns == ns
19077 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
19078 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
19079 && s->ns->proc_name->attr.function))
19080 s->fn_result_spec = 1;
19081 }
19082 }
19083 return false;
19084 }
19085
19086
19087 /* Resolve function and ENTRY types, issue diagnostics if needed. */
19088
19089 static void
19090 resolve_fntype (gfc_namespace *ns)
19091 {
19092 gfc_entry_list *el;
19093 gfc_symbol *sym;
19094
19095 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
19096 return;
19097
19098 /* If there are any entries, ns->proc_name is the entry master
19099 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
19100 if (ns->entries)
19101 sym = ns->entries->sym;
19102 else
19103 sym = ns->proc_name;
19104 if (sym->result == sym
19105 && sym->ts.type == BT_UNKNOWN
19106 && !gfc_set_default_type (sym, 0, NULL)
19107 && !sym->attr.untyped)
19108 {
19109 gfc_error ("Function %qs at %L has no IMPLICIT type",
19110 sym->name, &sym->declared_at);
19111 sym->attr.untyped = 1;
19112 }
19113
19114 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
19115 && !sym->attr.contained
19116 && !gfc_check_symbol_access (sym->ts.u.derived)
19117 && gfc_check_symbol_access (sym))
19118 {
19119 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
19120 "%L of PRIVATE type %qs", sym->name,
19121 &sym->declared_at, sym->ts.u.derived->name);
19122 }
19123
19124 if (ns->entries)
19125 for (el = ns->entries->next; el; el = el->next)
19126 {
19127 if (el->sym->result == el->sym
19128 && el->sym->ts.type == BT_UNKNOWN
19129 && !gfc_set_default_type (el->sym, 0, NULL)
19130 && !el->sym->attr.untyped)
19131 {
19132 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
19133 el->sym->name, &el->sym->declared_at);
19134 el->sym->attr.untyped = 1;
19135 }
19136 }
19137
19138 if (sym->ts.type == BT_CHARACTER
19139 && sym->ts.u.cl->length
19140 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
19141 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
19142 }
19143
19144
19145 /* 12.3.2.1.1 Defined operators. */
19146
19147 static bool
19148 check_uop_procedure (gfc_symbol *sym, locus where)
19149 {
19150 gfc_formal_arglist *formal;
19151
19152 if (!sym->attr.function)
19153 {
19154 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
19155 sym->name, &where);
19156 return false;
19157 }
19158
19159 if (sym->ts.type == BT_CHARACTER
19160 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
19161 && !(sym->result && ((sym->result->ts.u.cl
19162 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
19163 {
19164 gfc_error ("User operator procedure %qs at %L cannot be assumed "
19165 "character length", sym->name, &where);
19166 return false;
19167 }
19168
19169 formal = gfc_sym_get_dummy_args (sym);
19170 if (!formal || !formal->sym)
19171 {
19172 gfc_error ("User operator procedure %qs at %L must have at least "
19173 "one argument", sym->name, &where);
19174 return false;
19175 }
19176
19177 if (formal->sym->attr.intent != INTENT_IN)
19178 {
19179 gfc_error ("First argument of operator interface at %L must be "
19180 "INTENT(IN)", &where);
19181 return false;
19182 }
19183
19184 if (formal->sym->attr.optional)
19185 {
19186 gfc_error ("First argument of operator interface at %L cannot be "
19187 "optional", &where);
19188 return false;
19189 }
19190
19191 formal = formal->next;
19192 if (!formal || !formal->sym)
19193 return true;
19194
19195 if (formal->sym->attr.intent != INTENT_IN)
19196 {
19197 gfc_error ("Second argument of operator interface at %L must be "
19198 "INTENT(IN)", &where);
19199 return false;
19200 }
19201
19202 if (formal->sym->attr.optional)
19203 {
19204 gfc_error ("Second argument of operator interface at %L cannot be "
19205 "optional", &where);
19206 return false;
19207 }
19208
19209 if (formal->next)
19210 {
19211 gfc_error ("Operator interface at %L must have, at most, two "
19212 "arguments", &where);
19213 return false;
19214 }
19215
19216 return true;
19217 }
19218
19219 static void
19220 gfc_resolve_uops (gfc_symtree *symtree)
19221 {
19222 gfc_interface *itr;
19223
19224 if (symtree == NULL)
19225 return;
19226
19227 gfc_resolve_uops (symtree->left);
19228 gfc_resolve_uops (symtree->right);
19229
19230 for (itr = symtree->n.uop->op; itr; itr = itr->next)
19231 check_uop_procedure (itr->sym, itr->sym->declared_at);
19232 }
19233
19234
19235 /* Examine all of the expressions associated with a program unit,
19236 assign types to all intermediate expressions, make sure that all
19237 assignments are to compatible types and figure out which names
19238 refer to which functions or subroutines. It doesn't check code
19239 block, which is handled by gfc_resolve_code. */
19240
19241 static void
19242 resolve_types (gfc_namespace *ns)
19243 {
19244 gfc_namespace *n;
19245 gfc_charlen *cl;
19246 gfc_data *d;
19247 gfc_equiv *eq;
19248 gfc_namespace* old_ns = gfc_current_ns;
19249 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
19250
19251 if (ns->types_resolved)
19252 return;
19253
19254 /* Check that all IMPLICIT types are ok. */
19255 if (!ns->seen_implicit_none)
19256 {
19257 unsigned letter;
19258 for (letter = 0; letter != GFC_LETTERS; ++letter)
19259 if (ns->set_flag[letter]
19260 && !resolve_typespec_used (&ns->default_type[letter],
19261 &ns->implicit_loc[letter], NULL))
19262 return;
19263 }
19264
19265 gfc_current_ns = ns;
19266
19267 resolve_entries (ns);
19268
19269 resolve_common_vars (&ns->blank_common, false);
19270 resolve_common_blocks (ns->common_root);
19271
19272 resolve_contained_functions (ns);
19273
19274 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
19275 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
19276 gfc_resolve_formal_arglist (ns->proc_name);
19277
19278 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
19279
19280 for (cl = ns->cl_list; cl; cl = cl->next)
19281 resolve_charlen (cl);
19282
19283 gfc_traverse_ns (ns, resolve_symbol);
19284
19285 resolve_fntype (ns);
19286
19287 for (n = ns->contained; n; n = n->sibling)
19288 {
19289 /* Exclude final wrappers with the test for the artificial attribute. */
19290 if (gfc_pure (ns->proc_name)
19291 && !gfc_pure (n->proc_name)
19292 && !n->proc_name->attr.artificial)
19293 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
19294 "also be PURE", n->proc_name->name,
19295 &n->proc_name->declared_at);
19296
19297 resolve_types (n);
19298 }
19299
19300 forall_flag = 0;
19301 gfc_do_concurrent_flag = 0;
19302 gfc_check_interfaces (ns);
19303
19304 gfc_traverse_ns (ns, resolve_values);
19305
19306 if (ns->save_all || (!flag_automatic && !recursive))
19307 gfc_save_all (ns);
19308
19309 iter_stack = NULL;
19310 for (d = ns->data; d; d = d->next)
19311 resolve_data (d);
19312
19313 iter_stack = NULL;
19314 gfc_traverse_ns (ns, gfc_formalize_init_value);
19315
19316 gfc_traverse_ns (ns, gfc_verify_binding_labels);
19317
19318 for (eq = ns->equiv; eq; eq = eq->next)
19319 resolve_equivalence (eq);
19320
19321 /* Warn about unused labels. */
19322 if (warn_unused_label)
19323 warn_unused_fortran_label (ns->st_labels);
19324
19325 gfc_resolve_uops (ns->uop_root);
19326
19327 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
19328
19329 gfc_resolve_omp_declare (ns);
19330
19331 gfc_resolve_omp_udrs (ns->omp_udr_root);
19332
19333 ns->types_resolved = 1;
19334
19335 gfc_current_ns = old_ns;
19336 }
19337
19338
19339 /* Call gfc_resolve_code recursively. */
19340
19341 static void
19342 resolve_codes (gfc_namespace *ns)
19343 {
19344 gfc_namespace *n;
19345 bitmap_obstack old_obstack;
19346
19347 if (ns->resolved == 1)
19348 return;
19349
19350 for (n = ns->contained; n; n = n->sibling)
19351 resolve_codes (n);
19352
19353 gfc_current_ns = ns;
19354
19355 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
19356 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
19357 cs_base = NULL;
19358
19359 /* Set to an out of range value. */
19360 current_entry_id = -1;
19361
19362 old_obstack = labels_obstack;
19363 bitmap_obstack_initialize (&labels_obstack);
19364
19365 gfc_resolve_oacc_declare (ns);
19366 gfc_resolve_oacc_routines (ns);
19367 gfc_resolve_omp_local_vars (ns);
19368 if (ns->omp_allocate)
19369 gfc_resolve_omp_allocate (ns, ns->omp_allocate);
19370 gfc_resolve_code (ns->code, ns);
19371
19372 bitmap_obstack_release (&labels_obstack);
19373 labels_obstack = old_obstack;
19374 }
19375
19376
19377 /* This function is called after a complete program unit has been compiled.
19378 Its purpose is to examine all of the expressions associated with a program
19379 unit, assign types to all intermediate expressions, make sure that all
19380 assignments are to compatible types and figure out which names refer to
19381 which functions or subroutines. */
19382
19383 void
19384 gfc_resolve (gfc_namespace *ns)
19385 {
19386 gfc_namespace *old_ns;
19387 code_stack *old_cs_base;
19388 struct gfc_omp_saved_state old_omp_state;
19389
19390 if (ns->resolved)
19391 return;
19392
19393 ns->resolved = -1;
19394 old_ns = gfc_current_ns;
19395 old_cs_base = cs_base;
19396
19397 /* As gfc_resolve can be called during resolution of an OpenMP construct
19398 body, we should clear any state associated to it, so that say NS's
19399 DO loops are not interpreted as OpenMP loops. */
19400 if (!ns->construct_entities)
19401 gfc_omp_save_and_clear_state (&old_omp_state);
19402
19403 resolve_types (ns);
19404 component_assignment_level = 0;
19405 resolve_codes (ns);
19406
19407 if (ns->omp_assumes)
19408 gfc_resolve_omp_assumptions (ns->omp_assumes);
19409
19410 gfc_current_ns = old_ns;
19411 cs_base = old_cs_base;
19412 ns->resolved = 1;
19413
19414 gfc_run_passes (ns);
19415
19416 if (!ns->construct_entities)
19417 gfc_omp_restore_state (&old_omp_state);
19418 }