]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/resolve.c
ed705c062756860ee54d67297704ee7f04840459
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2021 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
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
59
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
62
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
69
70
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
72
73 static int omp_workshare_flag;
74
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
78
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
81
82 /* The id of the last entry seen. */
83 static int current_entry_id;
84
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
87
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
90
91
92 bool
93 gfc_is_formal_arg (void)
94 {
95 return formal_arg_flag;
96 }
97
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
101 {
102 for (ns = ns->parent; ns; ns = ns->parent)
103 {
104 if (sym->ns == ns)
105 return true;
106 }
107
108 return false;
109 }
110
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
114
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
117 {
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
119 {
120 if (where)
121 {
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
128 }
129
130 return false;
131 }
132
133 return true;
134 }
135
136
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
139 {
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
142 {
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
146 }
147 if (ifc->generic)
148 {
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
155 {
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
159 }
160 }
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
162 {
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
166 }
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
171 {
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
175 }
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
177 {
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
180 }
181 return true;
182 }
183
184
185 static void resolve_symbol (gfc_symbol *sym);
186
187
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
189
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
192 {
193 gfc_symbol *ifc = sym->ts.interface;
194
195 if (!ifc)
196 return true;
197
198 if (ifc == sym)
199 {
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
203 }
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
206
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
208 {
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
213
214 if (ifc->result)
215 {
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
223 }
224 else
225 {
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
232 }
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
236
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
246 {
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
251 }
252 }
253
254 return true;
255 }
256
257
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
263
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
266
267 void
268 gfc_resolve_formal_arglist (gfc_symbol *proc)
269 {
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
274
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
279
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
283 {
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
286 }
287
288 formal_arg_flag = true;
289
290 for (f = proc->formal; f; f = f->next)
291 {
292 gfc_array_spec *as;
293
294 sym = f->sym;
295
296 if (sym == NULL)
297 {
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
308 }
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
312
313 if (strcmp (proc->name, sym->name) == 0)
314 {
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
319 }
320
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 gfc_resolve_formal_arglist (sym);
323
324 if (sym->attr.subroutine || sym->attr.external)
325 {
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 }
329 else
330 {
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
334 }
335
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
338
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
343
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
346 */
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
354 {
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 }
359
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
368 {
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
372 }
373
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
376
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379
380 if (gfc_pure (proc))
381 {
382 if (sym->attr.flavor == FL_PROCEDURE)
383 {
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
386 {
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
390 }
391 }
392 else if (!sym->attr.pointer)
393 {
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 {
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
405 }
406
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 {
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
419 }
420 }
421
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 {
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
429 }
430 }
431
432 if (proc->attr.implicit_pure)
433 {
434 if (sym->attr.flavor == FL_PROCEDURE)
435 {
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
438 }
439 else if (!sym->attr.pointer)
440 {
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
444
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
448 }
449 }
450
451 if (gfc_elemental (proc))
452 {
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
457 {
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
461 }
462
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
465 {
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
469 }
470
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
474 {
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
479 }
480
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
484 {
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
489 }
490
491 if (sym->attr.flavor == FL_PROCEDURE)
492 {
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
497 }
498
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 {
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
507 }
508 }
509
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
512 {
513 if (sym->as != NULL)
514 {
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym->name, proc->name,
519 &proc->declared_at);
520 continue;
521 }
522
523 if (sym->ts.type == BT_CHARACTER)
524 {
525 gfc_charlen *cl = sym->ts.u.cl;
526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 {
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym->name, &sym->declared_at);
531 continue;
532 }
533 }
534 }
535 }
536 formal_arg_flag = false;
537 }
538
539
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
542
543 static void
544 find_arglists (gfc_symbol *sym)
545 {
546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
548 return;
549
550 gfc_resolve_formal_arglist (sym);
551 }
552
553
554 /* Given a namespace, resolve all formal argument lists within the namespace.
555 */
556
557 static void
558 resolve_formal_arglists (gfc_namespace *ns)
559 {
560 if (ns == NULL)
561 return;
562
563 gfc_traverse_ns (ns, find_arglists);
564 }
565
566
567 static void
568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
569 {
570 bool t;
571
572 if (sym && sym->attr.flavor == FL_PROCEDURE
573 && sym->ns->parent
574 && sym->ns->parent->proc_name
575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
576 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym->name, &sym->declared_at);
579
580 /* If this namespace is not a function or an entry master function,
581 ignore it. */
582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
583 || sym->attr.entry_master)
584 return;
585
586 if (!sym->result)
587 return;
588
589 /* Try to find out of what the return type is. */
590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
591 {
592 t = gfc_set_default_type (sym->result, 0, ns);
593
594 if (!t && !sym->result->attr.untyped)
595 {
596 if (sym->result == sym)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym->name, &sym->declared_at);
599 else if (!sym->result->attr.proc_pointer)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym->result->name, sym->name,
602 &sym->result->declared_at);
603 sym->result->attr.untyped = 1;
604 }
605 }
606
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
613
614 if (sym->result->ts.type == BT_CHARACTER)
615 {
616 gfc_charlen *cl = sym->result->ts.u.cl;
617 if ((!cl || !cl->length) && !sym->result->ts.deferred)
618 {
619 /* See if this is a module-procedure and adapt error message
620 accordingly. */
621 bool module_proc;
622 gcc_assert (ns->parent && ns->parent->proc_name);
623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
624
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym->name, &sym->declared_at);
631 }
632 }
633 }
634
635
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
638
639 static void
640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
641 {
642 gfc_formal_arglist *f, *new_arglist;
643 gfc_symbol *new_sym;
644
645 for (; new_args != NULL; new_args = new_args->next)
646 {
647 new_sym = new_args->sym;
648 /* See if this arg is already in the formal argument list. */
649 for (f = proc->formal; f; f = f->next)
650 {
651 if (new_sym == f->sym)
652 break;
653 }
654
655 if (f)
656 continue;
657
658 /* Add a new argument. Argument order is not important. */
659 new_arglist = gfc_get_formal_arglist ();
660 new_arglist->sym = new_sym;
661 new_arglist->next = proc->formal;
662 proc->formal = new_arglist;
663 }
664 }
665
666
667 /* Flag the arguments that are not present in all entries. */
668
669 static void
670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
671 {
672 gfc_formal_arglist *f, *head;
673 head = new_args;
674
675 for (f = proc->formal; f; f = f->next)
676 {
677 if (f->sym == NULL)
678 continue;
679
680 for (new_args = head; new_args; new_args = new_args->next)
681 {
682 if (new_args->sym == f->sym)
683 break;
684 }
685
686 if (new_args)
687 continue;
688
689 f->sym->attr.not_always_present = 1;
690 }
691 }
692
693
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
697
698 static void
699 resolve_entries (gfc_namespace *ns)
700 {
701 gfc_namespace *old_ns;
702 gfc_code *c;
703 gfc_symbol *proc;
704 gfc_entry_list *el;
705 char name[GFC_MAX_SYMBOL_LEN + 1];
706 static int master_count = 0;
707
708 if (ns->proc_name == NULL)
709 return;
710
711 /* No need to do anything if this procedure doesn't have alternate entry
712 points. */
713 if (!ns->entries)
714 return;
715
716 /* We may already have resolved alternate entry points. */
717 if (ns->proc_name->attr.entry_master)
718 return;
719
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
722
723 /* Remember the current namespace. */
724 old_ns = gfc_current_ns;
725
726 gfc_current_ns = ns;
727
728 /* Add the main entry point to the list of entry points. */
729 el = gfc_get_entry_list ();
730 el->sym = ns->proc_name;
731 el->id = 0;
732 el->next = ns->entries;
733 ns->entries = el;
734 ns->proc_name->attr.entry = 1;
735
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns->proc_name->attr.function
742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
743 el->sym->ns = ns;
744
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el = el->next; el; el = el->next)
749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
750 && el->sym->attr.mod_proc)
751 el->sym->ns = ns;
752 el = ns->entries;
753
754 /* Add an entry statement for it. */
755 c = gfc_get_code (EXEC_ENTRY);
756 c->ext.entry = el;
757 c->next = ns->code;
758 ns->code = c;
759
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
765 master_count++, ns->proc_name->name);
766 gfc_get_ha_symbol (name, &proc);
767 gcc_assert (proc != NULL);
768
769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
770 if (ns->proc_name->attr.subroutine)
771 gfc_add_subroutine (&proc->attr, proc->name, NULL);
772 else
773 {
774 gfc_symbol *sym;
775 gfc_typespec *ts, *fts;
776 gfc_array_spec *as, *fas;
777 gfc_add_function (&proc->attr, proc->name, NULL);
778 proc->result = proc;
779 fas = ns->entries->sym->as;
780 fas = fas ? fas : ns->entries->sym->result->as;
781 fts = &ns->entries->sym->result->ts;
782 if (fts->type == BT_UNKNOWN)
783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
784 for (el = ns->entries->next; el; el = el->next)
785 {
786 ts = &el->sym->result->ts;
787 as = el->sym->as;
788 as = as ? as : el->sym->result->as;
789 if (ts->type == BT_UNKNOWN)
790 ts = gfc_get_default_type (el->sym->result->name, NULL);
791
792 if (! gfc_compare_types (ts, fts)
793 || (el->sym->result->attr.dimension
794 != ns->entries->sym->result->attr.dimension)
795 || (el->sym->result->attr.pointer
796 != ns->entries->sym->result->attr.pointer))
797 break;
798 else if (as && fas && ns->entries->sym->result != el->sym->result
799 && gfc_compare_array_spec (as, fas) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns->entries->sym->name,
802 &ns->entries->sym->declared_at);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
808 && (((ts->u.cl->length && !fts->u.cl->length)
809 ||(!ts->u.cl->length && fts->u.cl->length))
810 || (ts->u.cl->length
811 && ts->u.cl->length->expr_type
812 != fts->u.cl->length->expr_type)
813 || (ts->u.cl->length
814 && ts->u.cl->length->expr_type == EXPR_CONSTANT
815 && mpz_cmp (ts->u.cl->length->value.integer,
816 fts->u.cl->length->value.integer) != 0)))
817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns->entries->sym->name,
820 &ns->entries->sym->declared_at);
821 }
822
823 if (el == NULL)
824 {
825 sym = ns->entries->sym->result;
826 /* All result types the same. */
827 proc->ts = *fts;
828 if (sym->attr.dimension)
829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
830 if (sym->attr.pointer)
831 gfc_add_pointer (&proc->attr, NULL);
832 }
833 else
834 {
835 /* Otherwise the result will be passed through a union by
836 reference. */
837 proc->attr.mixed_entry_master = 1;
838 for (el = ns->entries; el; el = el->next)
839 {
840 sym = el->sym->result;
841 if (sym->attr.dimension)
842 {
843 if (el == ns->entries)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym->name,
846 ns->entries->sym->name, &sym->declared_at);
847 else
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
851 }
852 else if (sym->attr.pointer)
853 {
854 if (el == ns->entries)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym->name,
857 ns->entries->sym->name, &sym->declared_at);
858 else
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym->name,
861 ns->entries->sym->name, &sym->declared_at);
862 }
863 else
864 {
865 ts = &sym->ts;
866 if (ts->type == BT_UNKNOWN)
867 ts = gfc_get_default_type (sym->name, NULL);
868 switch (ts->type)
869 {
870 case BT_INTEGER:
871 if (ts->kind == gfc_default_integer_kind)
872 sym = NULL;
873 break;
874 case BT_REAL:
875 if (ts->kind == gfc_default_real_kind
876 || ts->kind == gfc_default_double_kind)
877 sym = NULL;
878 break;
879 case BT_COMPLEX:
880 if (ts->kind == gfc_default_complex_kind)
881 sym = NULL;
882 break;
883 case BT_LOGICAL:
884 if (ts->kind == gfc_default_logical_kind)
885 sym = NULL;
886 break;
887 case BT_UNKNOWN:
888 /* We will issue error elsewhere. */
889 sym = NULL;
890 break;
891 default:
892 break;
893 }
894 if (sym)
895 {
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym->name,
899 gfc_typename (ts), ns->entries->sym->name,
900 &sym->declared_at);
901 else
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym->name,
904 gfc_typename (ts), ns->entries->sym->name,
905 &sym->declared_at);
906 }
907 }
908 }
909 }
910 }
911 proc->attr.access = ACCESS_PRIVATE;
912 proc->attr.entry_master = 1;
913
914 /* Merge all the entry point arguments. */
915 for (el = ns->entries; el; el = el->next)
916 merge_argument_lists (proc, el->sym->formal);
917
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el = ns->entries; el; el = el->next)
921 check_argument_lists (proc, el->sym->formal);
922
923 /* Use the master function for the function body. */
924 ns->proc_name = proc;
925
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
928
929 /* Restore the original namespace. */
930 gfc_current_ns = old_ns;
931 }
932
933
934 /* Resolve common variables. */
935 static void
936 resolve_common_vars (gfc_common_head *common_block, bool named_common)
937 {
938 gfc_symbol *csym = common_block->head;
939 gfc_gsymbol *gsym;
940
941 for (; csym; csym = csym->common_next)
942 {
943 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
944 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
945 gfc_error_now ("Global entity %qs at %L cannot appear in a "
946 "COMMON block at %L", gsym->name,
947 &gsym->where, &csym->common_block->where);
948
949 /* gfc_add_in_common may have been called before, but the reported errors
950 have been ignored to continue parsing.
951 We do the checks again here. */
952 if (!csym->attr.use_assoc)
953 {
954 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
955 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
956 &common_block->where);
957 }
958
959 if (csym->value || csym->attr.data)
960 {
961 if (!csym->ns->is_block_data)
962 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
963 "but only in BLOCK DATA initialization is "
964 "allowed", csym->name, &csym->declared_at);
965 else if (!named_common)
966 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
967 "in a blank COMMON but initialization is only "
968 "allowed in named common blocks", csym->name,
969 &csym->declared_at);
970 }
971
972 if (UNLIMITED_POLY (csym))
973 gfc_error_now ("%qs in cannot appear in COMMON at %L "
974 "[F2008:C5100]", csym->name, &csym->declared_at);
975
976 if (csym->ts.type != BT_DERIVED)
977 continue;
978
979 if (!(csym->ts.u.derived->attr.sequence
980 || csym->ts.u.derived->attr.is_bind_c))
981 gfc_error_now ("Derived type variable %qs in COMMON at %L "
982 "has neither the SEQUENCE nor the BIND(C) "
983 "attribute", csym->name, &csym->declared_at);
984 if (csym->ts.u.derived->attr.alloc_comp)
985 gfc_error_now ("Derived type variable %qs in COMMON at %L "
986 "has an ultimate component that is "
987 "allocatable", csym->name, &csym->declared_at);
988 if (gfc_has_default_initializer (csym->ts.u.derived))
989 gfc_error_now ("Derived type variable %qs in COMMON at %L "
990 "may not have default initializer", csym->name,
991 &csym->declared_at);
992
993 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
994 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
995 }
996 }
997
998 /* Resolve common blocks. */
999 static void
1000 resolve_common_blocks (gfc_symtree *common_root)
1001 {
1002 gfc_symbol *sym;
1003 gfc_gsymbol * gsym;
1004
1005 if (common_root == NULL)
1006 return;
1007
1008 if (common_root->left)
1009 resolve_common_blocks (common_root->left);
1010 if (common_root->right)
1011 resolve_common_blocks (common_root->right);
1012
1013 resolve_common_vars (common_root->n.common, true);
1014
1015 /* The common name is a global name - in Fortran 2003 also if it has a
1016 C binding name, since Fortran 2008 only the C binding name is a global
1017 identifier. */
1018 if (!common_root->n.common->binding_label
1019 || gfc_notification_std (GFC_STD_F2008))
1020 {
1021 gsym = gfc_find_gsymbol (gfc_gsym_root,
1022 common_root->n.common->name);
1023
1024 if (gsym && gfc_notification_std (GFC_STD_F2008)
1025 && gsym->type == GSYM_COMMON
1026 && ((common_root->n.common->binding_label
1027 && (!gsym->binding_label
1028 || strcmp (common_root->n.common->binding_label,
1029 gsym->binding_label) != 0))
1030 || (!common_root->n.common->binding_label
1031 && gsym->binding_label)))
1032 {
1033 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1034 "identifier and must thus have the same binding name "
1035 "as the same-named COMMON block at %L: %s vs %s",
1036 common_root->n.common->name, &common_root->n.common->where,
1037 &gsym->where,
1038 common_root->n.common->binding_label
1039 ? common_root->n.common->binding_label : "(blank)",
1040 gsym->binding_label ? gsym->binding_label : "(blank)");
1041 return;
1042 }
1043
1044 if (gsym && gsym->type != GSYM_COMMON
1045 && !common_root->n.common->binding_label)
1046 {
1047 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1048 "as entity at %L",
1049 common_root->n.common->name, &common_root->n.common->where,
1050 &gsym->where);
1051 return;
1052 }
1053 if (gsym && gsym->type != GSYM_COMMON)
1054 {
1055 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1056 "%L sharing the identifier with global non-COMMON-block "
1057 "entity at %L", common_root->n.common->name,
1058 &common_root->n.common->where, &gsym->where);
1059 return;
1060 }
1061 if (!gsym)
1062 {
1063 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1064 gsym->type = GSYM_COMMON;
1065 gsym->where = common_root->n.common->where;
1066 gsym->defined = 1;
1067 }
1068 gsym->used = 1;
1069 }
1070
1071 if (common_root->n.common->binding_label)
1072 {
1073 gsym = gfc_find_gsymbol (gfc_gsym_root,
1074 common_root->n.common->binding_label);
1075 if (gsym && gsym->type != GSYM_COMMON)
1076 {
1077 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1078 "global identifier as entity at %L",
1079 &common_root->n.common->where,
1080 common_root->n.common->binding_label, &gsym->where);
1081 return;
1082 }
1083 if (!gsym)
1084 {
1085 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1086 gsym->type = GSYM_COMMON;
1087 gsym->where = common_root->n.common->where;
1088 gsym->defined = 1;
1089 }
1090 gsym->used = 1;
1091 }
1092
1093 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1094 if (sym == NULL)
1095 return;
1096
1097 if (sym->attr.flavor == FL_PARAMETER)
1098 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1099 sym->name, &common_root->n.common->where, &sym->declared_at);
1100
1101 if (sym->attr.external)
1102 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1103 sym->name, &common_root->n.common->where);
1104
1105 if (sym->attr.intrinsic)
1106 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1107 sym->name, &common_root->n.common->where);
1108 else if (sym->attr.result
1109 || gfc_is_function_return_value (sym, gfc_current_ns))
1110 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1111 "that is also a function result", sym->name,
1112 &common_root->n.common->where);
1113 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1114 && sym->attr.proc != PROC_ST_FUNCTION)
1115 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1116 "that is also a global procedure", sym->name,
1117 &common_root->n.common->where);
1118 }
1119
1120
1121 /* Resolve contained function types. Because contained functions can call one
1122 another, they have to be worked out before any of the contained procedures
1123 can be resolved.
1124
1125 The good news is that if a function doesn't already have a type, the only
1126 way it can get one is through an IMPLICIT type or a RESULT variable, because
1127 by definition contained functions are contained namespace they're contained
1128 in, not in a sibling or parent namespace. */
1129
1130 static void
1131 resolve_contained_functions (gfc_namespace *ns)
1132 {
1133 gfc_namespace *child;
1134 gfc_entry_list *el;
1135
1136 resolve_formal_arglists (ns);
1137
1138 for (child = ns->contained; child; child = child->sibling)
1139 {
1140 /* Resolve alternate entry points first. */
1141 resolve_entries (child);
1142
1143 /* Then check function return types. */
1144 resolve_contained_fntype (child->proc_name, child);
1145 for (el = child->entries; el; el = el->next)
1146 resolve_contained_fntype (el->sym, child);
1147 }
1148 }
1149
1150
1151
1152 /* A Parameterized Derived Type constructor must contain values for
1153 the PDT KIND parameters or they must have a default initializer.
1154 Go through the constructor picking out the KIND expressions,
1155 storing them in 'param_list' and then call gfc_get_pdt_instance
1156 to obtain the PDT instance. */
1157
1158 static gfc_actual_arglist *param_list, *param_tail, *param;
1159
1160 static bool
1161 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1162 {
1163 param = gfc_get_actual_arglist ();
1164 if (!param_list)
1165 param_list = param_tail = param;
1166 else
1167 {
1168 param_tail->next = param;
1169 param_tail = param_tail->next;
1170 }
1171
1172 param_tail->name = c->name;
1173 if (expr)
1174 param_tail->expr = gfc_copy_expr (expr);
1175 else if (c->initializer)
1176 param_tail->expr = gfc_copy_expr (c->initializer);
1177 else
1178 {
1179 param_tail->spec_type = SPEC_ASSUMED;
1180 if (c->attr.pdt_kind)
1181 {
1182 gfc_error ("The KIND parameter %qs in the PDT constructor "
1183 "at %C has no value", param->name);
1184 return false;
1185 }
1186 }
1187
1188 return true;
1189 }
1190
1191 static bool
1192 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1193 gfc_symbol *derived)
1194 {
1195 gfc_constructor *cons = NULL;
1196 gfc_component *comp;
1197 bool t = true;
1198
1199 if (expr && expr->expr_type == EXPR_STRUCTURE)
1200 cons = gfc_constructor_first (expr->value.constructor);
1201 else if (constr)
1202 cons = *constr;
1203 gcc_assert (cons);
1204
1205 comp = derived->components;
1206
1207 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1208 {
1209 if (cons->expr
1210 && cons->expr->expr_type == EXPR_STRUCTURE
1211 && comp->ts.type == BT_DERIVED)
1212 {
1213 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1214 if (!t)
1215 return t;
1216 }
1217 else if (comp->ts.type == BT_DERIVED)
1218 {
1219 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1220 if (!t)
1221 return t;
1222 }
1223 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1224 && derived->attr.pdt_template)
1225 {
1226 t = get_pdt_spec_expr (comp, cons->expr);
1227 if (!t)
1228 return t;
1229 }
1230 }
1231 return t;
1232 }
1233
1234
1235 static bool resolve_fl_derived0 (gfc_symbol *sym);
1236 static bool resolve_fl_struct (gfc_symbol *sym);
1237
1238
1239 /* Resolve all of the elements of a structure constructor and make sure that
1240 the types are correct. The 'init' flag indicates that the given
1241 constructor is an initializer. */
1242
1243 static bool
1244 resolve_structure_cons (gfc_expr *expr, int init)
1245 {
1246 gfc_constructor *cons;
1247 gfc_component *comp;
1248 bool t;
1249 symbol_attribute a;
1250
1251 t = true;
1252
1253 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1254 {
1255 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1256 resolve_fl_derived0 (expr->ts.u.derived);
1257 else
1258 resolve_fl_struct (expr->ts.u.derived);
1259
1260 /* If this is a Parameterized Derived Type template, find the
1261 instance corresponding to the PDT kind parameters. */
1262 if (expr->ts.u.derived->attr.pdt_template)
1263 {
1264 param_list = NULL;
1265 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1266 if (!t)
1267 return t;
1268 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1269
1270 expr->param_list = gfc_copy_actual_arglist (param_list);
1271
1272 if (param_list)
1273 gfc_free_actual_arglist (param_list);
1274
1275 if (!expr->ts.u.derived->attr.pdt_type)
1276 return false;
1277 }
1278 }
1279
1280 cons = gfc_constructor_first (expr->value.constructor);
1281
1282 /* A constructor may have references if it is the result of substituting a
1283 parameter variable. In this case we just pull out the component we
1284 want. */
1285 if (expr->ref)
1286 comp = expr->ref->u.c.sym->components;
1287 else
1288 comp = expr->ts.u.derived->components;
1289
1290 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1291 {
1292 int rank;
1293
1294 if (!cons->expr)
1295 continue;
1296
1297 /* Unions use an EXPR_NULL contrived expression to tell the translation
1298 phase to generate an initializer of the appropriate length.
1299 Ignore it here. */
1300 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1301 continue;
1302
1303 if (!gfc_resolve_expr (cons->expr))
1304 {
1305 t = false;
1306 continue;
1307 }
1308
1309 rank = comp->as ? comp->as->rank : 0;
1310 if (comp->ts.type == BT_CLASS
1311 && !comp->ts.u.derived->attr.unlimited_polymorphic
1312 && CLASS_DATA (comp)->as)
1313 rank = CLASS_DATA (comp)->as->rank;
1314
1315 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1316 && (comp->attr.allocatable || cons->expr->rank))
1317 {
1318 gfc_error ("The rank of the element in the structure "
1319 "constructor at %L does not match that of the "
1320 "component (%d/%d)", &cons->expr->where,
1321 cons->expr->rank, rank);
1322 t = false;
1323 }
1324
1325 /* If we don't have the right type, try to convert it. */
1326
1327 if (!comp->attr.proc_pointer &&
1328 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1329 {
1330 if (strcmp (comp->name, "_extends") == 0)
1331 {
1332 /* Can afford to be brutal with the _extends initializer.
1333 The derived type can get lost because it is PRIVATE
1334 but it is not usage constrained by the standard. */
1335 cons->expr->ts = comp->ts;
1336 }
1337 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1338 {
1339 gfc_error ("The element in the structure constructor at %L, "
1340 "for pointer component %qs, is %s but should be %s",
1341 &cons->expr->where, comp->name,
1342 gfc_basic_typename (cons->expr->ts.type),
1343 gfc_basic_typename (comp->ts.type));
1344 t = false;
1345 }
1346 else
1347 {
1348 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1349 if (t)
1350 t = t2;
1351 }
1352 }
1353
1354 /* For strings, the length of the constructor should be the same as
1355 the one of the structure, ensure this if the lengths are known at
1356 compile time and when we are dealing with PARAMETER or structure
1357 constructors. */
1358 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1359 && comp->ts.u.cl->length
1360 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1361 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1362 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1363 && cons->expr->rank != 0
1364 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1365 comp->ts.u.cl->length->value.integer) != 0)
1366 {
1367 if (cons->expr->expr_type == EXPR_VARIABLE
1368 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1369 {
1370 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1371 to make use of the gfc_resolve_character_array_constructor
1372 machinery. The expression is later simplified away to
1373 an array of string literals. */
1374 gfc_expr *para = cons->expr;
1375 cons->expr = gfc_get_expr ();
1376 cons->expr->ts = para->ts;
1377 cons->expr->where = para->where;
1378 cons->expr->expr_type = EXPR_ARRAY;
1379 cons->expr->rank = para->rank;
1380 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1381 gfc_constructor_append_expr (&cons->expr->value.constructor,
1382 para, &cons->expr->where);
1383 }
1384
1385 if (cons->expr->expr_type == EXPR_ARRAY)
1386 {
1387 /* Rely on the cleanup of the namespace to deal correctly with
1388 the old charlen. (There was a block here that attempted to
1389 remove the charlen but broke the chain in so doing.) */
1390 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1391 cons->expr->ts.u.cl->length_from_typespec = true;
1392 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1393 gfc_resolve_character_array_constructor (cons->expr);
1394 }
1395 }
1396
1397 if (cons->expr->expr_type == EXPR_NULL
1398 && !(comp->attr.pointer || comp->attr.allocatable
1399 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1400 || (comp->ts.type == BT_CLASS
1401 && (CLASS_DATA (comp)->attr.class_pointer
1402 || CLASS_DATA (comp)->attr.allocatable))))
1403 {
1404 t = false;
1405 gfc_error ("The NULL in the structure constructor at %L is "
1406 "being applied to component %qs, which is neither "
1407 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1408 comp->name);
1409 }
1410
1411 if (comp->attr.proc_pointer && comp->ts.interface)
1412 {
1413 /* Check procedure pointer interface. */
1414 gfc_symbol *s2 = NULL;
1415 gfc_component *c2;
1416 const char *name;
1417 char err[200];
1418
1419 c2 = gfc_get_proc_ptr_comp (cons->expr);
1420 if (c2)
1421 {
1422 s2 = c2->ts.interface;
1423 name = c2->name;
1424 }
1425 else if (cons->expr->expr_type == EXPR_FUNCTION)
1426 {
1427 s2 = cons->expr->symtree->n.sym->result;
1428 name = cons->expr->symtree->n.sym->result->name;
1429 }
1430 else if (cons->expr->expr_type != EXPR_NULL)
1431 {
1432 s2 = cons->expr->symtree->n.sym;
1433 name = cons->expr->symtree->n.sym->name;
1434 }
1435
1436 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1437 err, sizeof (err), NULL, NULL))
1438 {
1439 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1440 "component %qs in structure constructor at %L:"
1441 " %s", comp->name, &cons->expr->where, err);
1442 return false;
1443 }
1444 }
1445
1446 if (!comp->attr.pointer || comp->attr.proc_pointer
1447 || cons->expr->expr_type == EXPR_NULL)
1448 continue;
1449
1450 a = gfc_expr_attr (cons->expr);
1451
1452 if (!a.pointer && !a.target)
1453 {
1454 t = false;
1455 gfc_error ("The element in the structure constructor at %L, "
1456 "for pointer component %qs should be a POINTER or "
1457 "a TARGET", &cons->expr->where, comp->name);
1458 }
1459
1460 if (init)
1461 {
1462 /* F08:C461. Additional checks for pointer initialization. */
1463 if (a.allocatable)
1464 {
1465 t = false;
1466 gfc_error ("Pointer initialization target at %L "
1467 "must not be ALLOCATABLE", &cons->expr->where);
1468 }
1469 if (!a.save)
1470 {
1471 t = false;
1472 gfc_error ("Pointer initialization target at %L "
1473 "must have the SAVE attribute", &cons->expr->where);
1474 }
1475 }
1476
1477 /* F2003, C1272 (3). */
1478 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1479 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1480 || gfc_is_coindexed (cons->expr));
1481 if (impure && gfc_pure (NULL))
1482 {
1483 t = false;
1484 gfc_error ("Invalid expression in the structure constructor for "
1485 "pointer component %qs at %L in PURE procedure",
1486 comp->name, &cons->expr->where);
1487 }
1488
1489 if (impure)
1490 gfc_unset_implicit_pure (NULL);
1491 }
1492
1493 return t;
1494 }
1495
1496
1497 /****************** Expression name resolution ******************/
1498
1499 /* Returns 0 if a symbol was not declared with a type or
1500 attribute declaration statement, nonzero otherwise. */
1501
1502 static int
1503 was_declared (gfc_symbol *sym)
1504 {
1505 symbol_attribute a;
1506
1507 a = sym->attr;
1508
1509 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1510 return 1;
1511
1512 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1513 || a.optional || a.pointer || a.save || a.target || a.volatile_
1514 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1515 || a.asynchronous || a.codimension)
1516 return 1;
1517
1518 return 0;
1519 }
1520
1521
1522 /* Determine if a symbol is generic or not. */
1523
1524 static int
1525 generic_sym (gfc_symbol *sym)
1526 {
1527 gfc_symbol *s;
1528
1529 if (sym->attr.generic ||
1530 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1531 return 1;
1532
1533 if (was_declared (sym) || sym->ns->parent == NULL)
1534 return 0;
1535
1536 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1537
1538 if (s != NULL)
1539 {
1540 if (s == sym)
1541 return 0;
1542 else
1543 return generic_sym (s);
1544 }
1545
1546 return 0;
1547 }
1548
1549
1550 /* Determine if a symbol is specific or not. */
1551
1552 static int
1553 specific_sym (gfc_symbol *sym)
1554 {
1555 gfc_symbol *s;
1556
1557 if (sym->attr.if_source == IFSRC_IFBODY
1558 || sym->attr.proc == PROC_MODULE
1559 || sym->attr.proc == PROC_INTERNAL
1560 || sym->attr.proc == PROC_ST_FUNCTION
1561 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1562 || sym->attr.external)
1563 return 1;
1564
1565 if (was_declared (sym) || sym->ns->parent == NULL)
1566 return 0;
1567
1568 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1569
1570 return (s == NULL) ? 0 : specific_sym (s);
1571 }
1572
1573
1574 /* Figure out if the procedure is specific, generic or unknown. */
1575
1576 enum proc_type
1577 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1578
1579 static proc_type
1580 procedure_kind (gfc_symbol *sym)
1581 {
1582 if (generic_sym (sym))
1583 return PTYPE_GENERIC;
1584
1585 if (specific_sym (sym))
1586 return PTYPE_SPECIFIC;
1587
1588 return PTYPE_UNKNOWN;
1589 }
1590
1591 /* Check references to assumed size arrays. The flag need_full_assumed_size
1592 is nonzero when matching actual arguments. */
1593
1594 static int need_full_assumed_size = 0;
1595
1596 static bool
1597 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1598 {
1599 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1600 return false;
1601
1602 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1603 What should it be? */
1604 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1605 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1606 && (e->ref->u.ar.type == AR_FULL))
1607 {
1608 gfc_error ("The upper bound in the last dimension must "
1609 "appear in the reference to the assumed size "
1610 "array %qs at %L", sym->name, &e->where);
1611 return true;
1612 }
1613 return false;
1614 }
1615
1616
1617 /* Look for bad assumed size array references in argument expressions
1618 of elemental and array valued intrinsic procedures. Since this is
1619 called from procedure resolution functions, it only recurses at
1620 operators. */
1621
1622 static bool
1623 resolve_assumed_size_actual (gfc_expr *e)
1624 {
1625 if (e == NULL)
1626 return false;
1627
1628 switch (e->expr_type)
1629 {
1630 case EXPR_VARIABLE:
1631 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1632 return true;
1633 break;
1634
1635 case EXPR_OP:
1636 if (resolve_assumed_size_actual (e->value.op.op1)
1637 || resolve_assumed_size_actual (e->value.op.op2))
1638 return true;
1639 break;
1640
1641 default:
1642 break;
1643 }
1644 return false;
1645 }
1646
1647
1648 /* Check a generic procedure, passed as an actual argument, to see if
1649 there is a matching specific name. If none, it is an error, and if
1650 more than one, the reference is ambiguous. */
1651 static int
1652 count_specific_procs (gfc_expr *e)
1653 {
1654 int n;
1655 gfc_interface *p;
1656 gfc_symbol *sym;
1657
1658 n = 0;
1659 sym = e->symtree->n.sym;
1660
1661 for (p = sym->generic; p; p = p->next)
1662 if (strcmp (sym->name, p->sym->name) == 0)
1663 {
1664 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1665 sym->name);
1666 n++;
1667 }
1668
1669 if (n > 1)
1670 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1671 &e->where);
1672
1673 if (n == 0)
1674 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1675 "argument at %L", sym->name, &e->where);
1676
1677 return n;
1678 }
1679
1680
1681 /* See if a call to sym could possibly be a not allowed RECURSION because of
1682 a missing RECURSIVE declaration. This means that either sym is the current
1683 context itself, or sym is the parent of a contained procedure calling its
1684 non-RECURSIVE containing procedure.
1685 This also works if sym is an ENTRY. */
1686
1687 static bool
1688 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1689 {
1690 gfc_symbol* proc_sym;
1691 gfc_symbol* context_proc;
1692 gfc_namespace* real_context;
1693
1694 if (sym->attr.flavor == FL_PROGRAM
1695 || gfc_fl_struct (sym->attr.flavor))
1696 return false;
1697
1698 /* If we've got an ENTRY, find real procedure. */
1699 if (sym->attr.entry && sym->ns->entries)
1700 proc_sym = sym->ns->entries->sym;
1701 else
1702 proc_sym = sym;
1703
1704 /* If sym is RECURSIVE, all is well of course. */
1705 if (proc_sym->attr.recursive || flag_recursive)
1706 return false;
1707
1708 /* Find the context procedure's "real" symbol if it has entries.
1709 We look for a procedure symbol, so recurse on the parents if we don't
1710 find one (like in case of a BLOCK construct). */
1711 for (real_context = context; ; real_context = real_context->parent)
1712 {
1713 /* We should find something, eventually! */
1714 gcc_assert (real_context);
1715
1716 context_proc = (real_context->entries ? real_context->entries->sym
1717 : real_context->proc_name);
1718
1719 /* In some special cases, there may not be a proc_name, like for this
1720 invalid code:
1721 real(bad_kind()) function foo () ...
1722 when checking the call to bad_kind ().
1723 In these cases, we simply return here and assume that the
1724 call is ok. */
1725 if (!context_proc)
1726 return false;
1727
1728 if (context_proc->attr.flavor != FL_LABEL)
1729 break;
1730 }
1731
1732 /* A call from sym's body to itself is recursion, of course. */
1733 if (context_proc == proc_sym)
1734 return true;
1735
1736 /* The same is true if context is a contained procedure and sym the
1737 containing one. */
1738 if (context_proc->attr.contained)
1739 {
1740 gfc_symbol* parent_proc;
1741
1742 gcc_assert (context->parent);
1743 parent_proc = (context->parent->entries ? context->parent->entries->sym
1744 : context->parent->proc_name);
1745
1746 if (parent_proc == proc_sym)
1747 return true;
1748 }
1749
1750 return false;
1751 }
1752
1753
1754 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1755 its typespec and formal argument list. */
1756
1757 bool
1758 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1759 {
1760 gfc_intrinsic_sym* isym = NULL;
1761 const char* symstd;
1762
1763 if (sym->resolve_symbol_called >= 2)
1764 return true;
1765
1766 sym->resolve_symbol_called = 2;
1767
1768 /* Already resolved. */
1769 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1770 return true;
1771
1772 /* We already know this one is an intrinsic, so we don't call
1773 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1774 gfc_find_subroutine directly to check whether it is a function or
1775 subroutine. */
1776
1777 if (sym->intmod_sym_id && sym->attr.subroutine)
1778 {
1779 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1780 isym = gfc_intrinsic_subroutine_by_id (id);
1781 }
1782 else if (sym->intmod_sym_id)
1783 {
1784 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1785 isym = gfc_intrinsic_function_by_id (id);
1786 }
1787 else if (!sym->attr.subroutine)
1788 isym = gfc_find_function (sym->name);
1789
1790 if (isym && !sym->attr.subroutine)
1791 {
1792 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1793 && !sym->attr.implicit_type)
1794 gfc_warning (OPT_Wsurprising,
1795 "Type specified for intrinsic function %qs at %L is"
1796 " ignored", sym->name, &sym->declared_at);
1797
1798 if (!sym->attr.function &&
1799 !gfc_add_function(&sym->attr, sym->name, loc))
1800 return false;
1801
1802 sym->ts = isym->ts;
1803 }
1804 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1805 {
1806 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1807 {
1808 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1809 " specifier", sym->name, &sym->declared_at);
1810 return false;
1811 }
1812
1813 if (!sym->attr.subroutine &&
1814 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1815 return false;
1816 }
1817 else
1818 {
1819 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1820 &sym->declared_at);
1821 return false;
1822 }
1823
1824 gfc_copy_formal_args_intr (sym, isym, NULL);
1825
1826 sym->attr.pure = isym->pure;
1827 sym->attr.elemental = isym->elemental;
1828
1829 /* Check it is actually available in the standard settings. */
1830 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1831 {
1832 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1833 "available in the current standard settings but %s. Use "
1834 "an appropriate %<-std=*%> option or enable "
1835 "%<-fall-intrinsics%> in order to use it.",
1836 sym->name, &sym->declared_at, symstd);
1837 return false;
1838 }
1839
1840 return true;
1841 }
1842
1843
1844 /* Resolve a procedure expression, like passing it to a called procedure or as
1845 RHS for a procedure pointer assignment. */
1846
1847 static bool
1848 resolve_procedure_expression (gfc_expr* expr)
1849 {
1850 gfc_symbol* sym;
1851
1852 if (expr->expr_type != EXPR_VARIABLE)
1853 return true;
1854 gcc_assert (expr->symtree);
1855
1856 sym = expr->symtree->n.sym;
1857
1858 if (sym->attr.intrinsic)
1859 gfc_resolve_intrinsic (sym, &expr->where);
1860
1861 if (sym->attr.flavor != FL_PROCEDURE
1862 || (sym->attr.function && sym->result == sym))
1863 return true;
1864
1865 /* A non-RECURSIVE procedure that is used as procedure expression within its
1866 own body is in danger of being called recursively. */
1867 if (is_illegal_recursion (sym, gfc_current_ns))
1868 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1869 " itself recursively. Declare it RECURSIVE or use"
1870 " %<-frecursive%>", sym->name, &expr->where);
1871
1872 return true;
1873 }
1874
1875
1876 /* Check that name is not a derived type. */
1877
1878 static bool
1879 is_dt_name (const char *name)
1880 {
1881 gfc_symbol *dt_list, *dt_first;
1882
1883 dt_list = dt_first = gfc_derived_types;
1884 for (; dt_list; dt_list = dt_list->dt_next)
1885 {
1886 if (strcmp(dt_list->name, name) == 0)
1887 return true;
1888 if (dt_first == dt_list->dt_next)
1889 break;
1890 }
1891 return false;
1892 }
1893
1894
1895 /* Resolve an actual argument list. Most of the time, this is just
1896 resolving the expressions in the list.
1897 The exception is that we sometimes have to decide whether arguments
1898 that look like procedure arguments are really simple variable
1899 references. */
1900
1901 static bool
1902 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1903 bool no_formal_args)
1904 {
1905 gfc_symbol *sym;
1906 gfc_symtree *parent_st;
1907 gfc_expr *e;
1908 gfc_component *comp;
1909 int save_need_full_assumed_size;
1910 bool return_value = false;
1911 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1912
1913 actual_arg = true;
1914 first_actual_arg = true;
1915
1916 for (; arg; arg = arg->next)
1917 {
1918 e = arg->expr;
1919 if (e == NULL)
1920 {
1921 /* Check the label is a valid branching target. */
1922 if (arg->label)
1923 {
1924 if (arg->label->defined == ST_LABEL_UNKNOWN)
1925 {
1926 gfc_error ("Label %d referenced at %L is never defined",
1927 arg->label->value, &arg->label->where);
1928 goto cleanup;
1929 }
1930 }
1931 first_actual_arg = false;
1932 continue;
1933 }
1934
1935 if (e->expr_type == EXPR_VARIABLE
1936 && e->symtree->n.sym->attr.generic
1937 && no_formal_args
1938 && count_specific_procs (e) != 1)
1939 goto cleanup;
1940
1941 if (e->ts.type != BT_PROCEDURE)
1942 {
1943 save_need_full_assumed_size = need_full_assumed_size;
1944 if (e->expr_type != EXPR_VARIABLE)
1945 need_full_assumed_size = 0;
1946 if (!gfc_resolve_expr (e))
1947 goto cleanup;
1948 need_full_assumed_size = save_need_full_assumed_size;
1949 goto argument_list;
1950 }
1951
1952 /* See if the expression node should really be a variable reference. */
1953
1954 sym = e->symtree->n.sym;
1955
1956 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1957 {
1958 gfc_error ("Derived type %qs is used as an actual "
1959 "argument at %L", sym->name, &e->where);
1960 goto cleanup;
1961 }
1962
1963 if (sym->attr.flavor == FL_PROCEDURE
1964 || sym->attr.intrinsic
1965 || sym->attr.external)
1966 {
1967 int actual_ok;
1968
1969 /* If a procedure is not already determined to be something else
1970 check if it is intrinsic. */
1971 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1972 sym->attr.intrinsic = 1;
1973
1974 if (sym->attr.proc == PROC_ST_FUNCTION)
1975 {
1976 gfc_error ("Statement function %qs at %L is not allowed as an "
1977 "actual argument", sym->name, &e->where);
1978 }
1979
1980 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1981 sym->attr.subroutine);
1982 if (sym->attr.intrinsic && actual_ok == 0)
1983 {
1984 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1985 "actual argument", sym->name, &e->where);
1986 }
1987
1988 if (sym->attr.contained && !sym->attr.use_assoc
1989 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1990 {
1991 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1992 " used as actual argument at %L",
1993 sym->name, &e->where))
1994 goto cleanup;
1995 }
1996
1997 if (sym->attr.elemental && !sym->attr.intrinsic)
1998 {
1999 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2000 "allowed as an actual argument at %L", sym->name,
2001 &e->where);
2002 }
2003
2004 /* Check if a generic interface has a specific procedure
2005 with the same name before emitting an error. */
2006 if (sym->attr.generic && count_specific_procs (e) != 1)
2007 goto cleanup;
2008
2009 /* Just in case a specific was found for the expression. */
2010 sym = e->symtree->n.sym;
2011
2012 /* If the symbol is the function that names the current (or
2013 parent) scope, then we really have a variable reference. */
2014
2015 if (gfc_is_function_return_value (sym, sym->ns))
2016 goto got_variable;
2017
2018 /* If all else fails, see if we have a specific intrinsic. */
2019 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2020 {
2021 gfc_intrinsic_sym *isym;
2022
2023 isym = gfc_find_function (sym->name);
2024 if (isym == NULL || !isym->specific)
2025 {
2026 gfc_error ("Unable to find a specific INTRINSIC procedure "
2027 "for the reference %qs at %L", sym->name,
2028 &e->where);
2029 goto cleanup;
2030 }
2031 sym->ts = isym->ts;
2032 sym->attr.intrinsic = 1;
2033 sym->attr.function = 1;
2034 }
2035
2036 if (!gfc_resolve_expr (e))
2037 goto cleanup;
2038 goto argument_list;
2039 }
2040
2041 /* See if the name is a module procedure in a parent unit. */
2042
2043 if (was_declared (sym) || sym->ns->parent == NULL)
2044 goto got_variable;
2045
2046 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2047 {
2048 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2049 goto cleanup;
2050 }
2051
2052 if (parent_st == NULL)
2053 goto got_variable;
2054
2055 sym = parent_st->n.sym;
2056 e->symtree = parent_st; /* Point to the right thing. */
2057
2058 if (sym->attr.flavor == FL_PROCEDURE
2059 || sym->attr.intrinsic
2060 || sym->attr.external)
2061 {
2062 if (!gfc_resolve_expr (e))
2063 goto cleanup;
2064 goto argument_list;
2065 }
2066
2067 got_variable:
2068 e->expr_type = EXPR_VARIABLE;
2069 e->ts = sym->ts;
2070 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2071 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2072 && CLASS_DATA (sym)->as))
2073 {
2074 e->rank = sym->ts.type == BT_CLASS
2075 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2076 e->ref = gfc_get_ref ();
2077 e->ref->type = REF_ARRAY;
2078 e->ref->u.ar.type = AR_FULL;
2079 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2080 ? CLASS_DATA (sym)->as : sym->as;
2081 }
2082
2083 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2084 primary.c (match_actual_arg). If above code determines that it
2085 is a variable instead, it needs to be resolved as it was not
2086 done at the beginning of this function. */
2087 save_need_full_assumed_size = need_full_assumed_size;
2088 if (e->expr_type != EXPR_VARIABLE)
2089 need_full_assumed_size = 0;
2090 if (!gfc_resolve_expr (e))
2091 goto cleanup;
2092 need_full_assumed_size = save_need_full_assumed_size;
2093
2094 argument_list:
2095 /* Check argument list functions %VAL, %LOC and %REF. There is
2096 nothing to do for %REF. */
2097 if (arg->name && arg->name[0] == '%')
2098 {
2099 if (strcmp ("%VAL", arg->name) == 0)
2100 {
2101 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2102 {
2103 gfc_error ("By-value argument at %L is not of numeric "
2104 "type", &e->where);
2105 goto cleanup;
2106 }
2107
2108 if (e->rank)
2109 {
2110 gfc_error ("By-value argument at %L cannot be an array or "
2111 "an array section", &e->where);
2112 goto cleanup;
2113 }
2114
2115 /* Intrinsics are still PROC_UNKNOWN here. However,
2116 since same file external procedures are not resolvable
2117 in gfortran, it is a good deal easier to leave them to
2118 intrinsic.c. */
2119 if (ptype != PROC_UNKNOWN
2120 && ptype != PROC_DUMMY
2121 && ptype != PROC_EXTERNAL
2122 && ptype != PROC_MODULE)
2123 {
2124 gfc_error ("By-value argument at %L is not allowed "
2125 "in this context", &e->where);
2126 goto cleanup;
2127 }
2128 }
2129
2130 /* Statement functions have already been excluded above. */
2131 else if (strcmp ("%LOC", arg->name) == 0
2132 && e->ts.type == BT_PROCEDURE)
2133 {
2134 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2135 {
2136 gfc_error ("Passing internal procedure at %L by location "
2137 "not allowed", &e->where);
2138 goto cleanup;
2139 }
2140 }
2141 }
2142
2143 comp = gfc_get_proc_ptr_comp(e);
2144 if (e->expr_type == EXPR_VARIABLE
2145 && comp && comp->attr.elemental)
2146 {
2147 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2148 "allowed as an actual argument at %L", comp->name,
2149 &e->where);
2150 }
2151
2152 /* Fortran 2008, C1237. */
2153 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2154 && gfc_has_ultimate_pointer (e))
2155 {
2156 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2157 "component", &e->where);
2158 goto cleanup;
2159 }
2160
2161 first_actual_arg = false;
2162 }
2163
2164 return_value = true;
2165
2166 cleanup:
2167 actual_arg = actual_arg_sav;
2168 first_actual_arg = first_actual_arg_sav;
2169
2170 return return_value;
2171 }
2172
2173
2174 /* Do the checks of the actual argument list that are specific to elemental
2175 procedures. If called with c == NULL, we have a function, otherwise if
2176 expr == NULL, we have a subroutine. */
2177
2178 static bool
2179 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2180 {
2181 gfc_actual_arglist *arg0;
2182 gfc_actual_arglist *arg;
2183 gfc_symbol *esym = NULL;
2184 gfc_intrinsic_sym *isym = NULL;
2185 gfc_expr *e = NULL;
2186 gfc_intrinsic_arg *iformal = NULL;
2187 gfc_formal_arglist *eformal = NULL;
2188 bool formal_optional = false;
2189 bool set_by_optional = false;
2190 int i;
2191 int rank = 0;
2192
2193 /* Is this an elemental procedure? */
2194 if (expr && expr->value.function.actual != NULL)
2195 {
2196 if (expr->value.function.esym != NULL
2197 && expr->value.function.esym->attr.elemental)
2198 {
2199 arg0 = expr->value.function.actual;
2200 esym = expr->value.function.esym;
2201 }
2202 else if (expr->value.function.isym != NULL
2203 && expr->value.function.isym->elemental)
2204 {
2205 arg0 = expr->value.function.actual;
2206 isym = expr->value.function.isym;
2207 }
2208 else
2209 return true;
2210 }
2211 else if (c && c->ext.actual != NULL)
2212 {
2213 arg0 = c->ext.actual;
2214
2215 if (c->resolved_sym)
2216 esym = c->resolved_sym;
2217 else
2218 esym = c->symtree->n.sym;
2219 gcc_assert (esym);
2220
2221 if (!esym->attr.elemental)
2222 return true;
2223 }
2224 else
2225 return true;
2226
2227 /* The rank of an elemental is the rank of its array argument(s). */
2228 for (arg = arg0; arg; arg = arg->next)
2229 {
2230 if (arg->expr != NULL && arg->expr->rank != 0)
2231 {
2232 rank = arg->expr->rank;
2233 if (arg->expr->expr_type == EXPR_VARIABLE
2234 && arg->expr->symtree->n.sym->attr.optional)
2235 set_by_optional = true;
2236
2237 /* Function specific; set the result rank and shape. */
2238 if (expr)
2239 {
2240 expr->rank = rank;
2241 if (!expr->shape && arg->expr->shape)
2242 {
2243 expr->shape = gfc_get_shape (rank);
2244 for (i = 0; i < rank; i++)
2245 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2246 }
2247 }
2248 break;
2249 }
2250 }
2251
2252 /* If it is an array, it shall not be supplied as an actual argument
2253 to an elemental procedure unless an array of the same rank is supplied
2254 as an actual argument corresponding to a nonoptional dummy argument of
2255 that elemental procedure(12.4.1.5). */
2256 formal_optional = false;
2257 if (isym)
2258 iformal = isym->formal;
2259 else
2260 eformal = esym->formal;
2261
2262 for (arg = arg0; arg; arg = arg->next)
2263 {
2264 if (eformal)
2265 {
2266 if (eformal->sym && eformal->sym->attr.optional)
2267 formal_optional = true;
2268 eformal = eformal->next;
2269 }
2270 else if (isym && iformal)
2271 {
2272 if (iformal->optional)
2273 formal_optional = true;
2274 iformal = iformal->next;
2275 }
2276 else if (isym)
2277 formal_optional = true;
2278
2279 if (pedantic && arg->expr != NULL
2280 && arg->expr->expr_type == EXPR_VARIABLE
2281 && arg->expr->symtree->n.sym->attr.optional
2282 && formal_optional
2283 && arg->expr->rank
2284 && (set_by_optional || arg->expr->rank != rank)
2285 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2286 {
2287 bool t = false;
2288 gfc_actual_arglist *a;
2289
2290 /* Scan the argument list for a non-optional argument with the
2291 same rank as arg. */
2292 for (a = arg0; a; a = a->next)
2293 if (a != arg
2294 && a->expr->rank == arg->expr->rank
2295 && !a->expr->symtree->n.sym->attr.optional)
2296 {
2297 t = true;
2298 break;
2299 }
2300
2301 if (!t)
2302 gfc_warning (OPT_Wpedantic,
2303 "%qs at %L is an array and OPTIONAL; If it is not "
2304 "present, then it cannot be the actual argument of "
2305 "an ELEMENTAL procedure unless there is a non-optional"
2306 " argument with the same rank "
2307 "(Fortran 2018, 15.5.2.12)",
2308 arg->expr->symtree->n.sym->name, &arg->expr->where);
2309 }
2310 }
2311
2312 for (arg = arg0; arg; arg = arg->next)
2313 {
2314 if (arg->expr == NULL || arg->expr->rank == 0)
2315 continue;
2316
2317 /* Being elemental, the last upper bound of an assumed size array
2318 argument must be present. */
2319 if (resolve_assumed_size_actual (arg->expr))
2320 return false;
2321
2322 /* Elemental procedure's array actual arguments must conform. */
2323 if (e != NULL)
2324 {
2325 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2326 return false;
2327 }
2328 else
2329 e = arg->expr;
2330 }
2331
2332 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2333 is an array, the intent inout/out variable needs to be also an array. */
2334 if (rank > 0 && esym && expr == NULL)
2335 for (eformal = esym->formal, arg = arg0; arg && eformal;
2336 arg = arg->next, eformal = eformal->next)
2337 if ((eformal->sym->attr.intent == INTENT_OUT
2338 || eformal->sym->attr.intent == INTENT_INOUT)
2339 && arg->expr && arg->expr->rank == 0)
2340 {
2341 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2342 "ELEMENTAL subroutine %qs is a scalar, but another "
2343 "actual argument is an array", &arg->expr->where,
2344 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2345 : "INOUT", eformal->sym->name, esym->name);
2346 return false;
2347 }
2348 return true;
2349 }
2350
2351
2352 /* This function does the checking of references to global procedures
2353 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2354 77 and 95 standards. It checks for a gsymbol for the name, making
2355 one if it does not already exist. If it already exists, then the
2356 reference being resolved must correspond to the type of gsymbol.
2357 Otherwise, the new symbol is equipped with the attributes of the
2358 reference. The corresponding code that is called in creating
2359 global entities is parse.c.
2360
2361 In addition, for all but -std=legacy, the gsymbols are used to
2362 check the interfaces of external procedures from the same file.
2363 The namespace of the gsymbol is resolved and then, once this is
2364 done the interface is checked. */
2365
2366
2367 static bool
2368 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2369 {
2370 if (!gsym_ns->proc_name->attr.recursive)
2371 return true;
2372
2373 if (sym->ns == gsym_ns)
2374 return false;
2375
2376 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2377 return false;
2378
2379 return true;
2380 }
2381
2382 static bool
2383 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2384 {
2385 if (gsym_ns->entries)
2386 {
2387 gfc_entry_list *entry = gsym_ns->entries;
2388
2389 for (; entry; entry = entry->next)
2390 {
2391 if (strcmp (sym->name, entry->sym->name) == 0)
2392 {
2393 if (strcmp (gsym_ns->proc_name->name,
2394 sym->ns->proc_name->name) == 0)
2395 return false;
2396
2397 if (sym->ns->parent
2398 && strcmp (gsym_ns->proc_name->name,
2399 sym->ns->parent->proc_name->name) == 0)
2400 return false;
2401 }
2402 }
2403 }
2404 return true;
2405 }
2406
2407
2408 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2409
2410 bool
2411 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2412 {
2413 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2414
2415 for ( ; arg; arg = arg->next)
2416 {
2417 if (!arg->sym)
2418 continue;
2419
2420 if (arg->sym->attr.allocatable) /* (2a) */
2421 {
2422 strncpy (errmsg, _("allocatable argument"), err_len);
2423 return true;
2424 }
2425 else if (arg->sym->attr.asynchronous)
2426 {
2427 strncpy (errmsg, _("asynchronous argument"), err_len);
2428 return true;
2429 }
2430 else if (arg->sym->attr.optional)
2431 {
2432 strncpy (errmsg, _("optional argument"), err_len);
2433 return true;
2434 }
2435 else if (arg->sym->attr.pointer)
2436 {
2437 strncpy (errmsg, _("pointer argument"), err_len);
2438 return true;
2439 }
2440 else if (arg->sym->attr.target)
2441 {
2442 strncpy (errmsg, _("target argument"), err_len);
2443 return true;
2444 }
2445 else if (arg->sym->attr.value)
2446 {
2447 strncpy (errmsg, _("value argument"), err_len);
2448 return true;
2449 }
2450 else if (arg->sym->attr.volatile_)
2451 {
2452 strncpy (errmsg, _("volatile argument"), err_len);
2453 return true;
2454 }
2455 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2456 {
2457 strncpy (errmsg, _("assumed-shape argument"), err_len);
2458 return true;
2459 }
2460 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2461 {
2462 strncpy (errmsg, _("assumed-rank argument"), err_len);
2463 return true;
2464 }
2465 else if (arg->sym->attr.codimension) /* (2c) */
2466 {
2467 strncpy (errmsg, _("coarray argument"), err_len);
2468 return true;
2469 }
2470 else if (false) /* (2d) TODO: parametrized derived type */
2471 {
2472 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2473 return true;
2474 }
2475 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2476 {
2477 strncpy (errmsg, _("polymorphic argument"), err_len);
2478 return true;
2479 }
2480 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2481 {
2482 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2483 return true;
2484 }
2485 else if (arg->sym->ts.type == BT_ASSUMED)
2486 {
2487 /* As assumed-type is unlimited polymorphic (cf. above).
2488 See also TS 29113, Note 6.1. */
2489 strncpy (errmsg, _("assumed-type argument"), err_len);
2490 return true;
2491 }
2492 }
2493
2494 if (sym->attr.function)
2495 {
2496 gfc_symbol *res = sym->result ? sym->result : sym;
2497
2498 if (res->attr.dimension) /* (3a) */
2499 {
2500 strncpy (errmsg, _("array result"), err_len);
2501 return true;
2502 }
2503 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2504 {
2505 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2506 return true;
2507 }
2508 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2509 && res->ts.u.cl->length
2510 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2511 {
2512 strncpy (errmsg, _("result with non-constant character length"), err_len);
2513 return true;
2514 }
2515 }
2516
2517 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2518 {
2519 strncpy (errmsg, _("elemental procedure"), err_len);
2520 return true;
2521 }
2522 else if (sym->attr.is_bind_c) /* (5) */
2523 {
2524 strncpy (errmsg, _("bind(c) procedure"), err_len);
2525 return true;
2526 }
2527
2528 return false;
2529 }
2530
2531
2532 static void
2533 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2534 {
2535 gfc_gsymbol * gsym;
2536 gfc_namespace *ns;
2537 enum gfc_symbol_type type;
2538 char reason[200];
2539
2540 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2541
2542 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2543 sym->binding_label != NULL);
2544
2545 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2546 gfc_global_used (gsym, where);
2547
2548 if ((sym->attr.if_source == IFSRC_UNKNOWN
2549 || sym->attr.if_source == IFSRC_IFBODY)
2550 && gsym->type != GSYM_UNKNOWN
2551 && !gsym->binding_label
2552 && gsym->ns
2553 && gsym->ns->proc_name
2554 && not_in_recursive (sym, gsym->ns)
2555 && not_entry_self_reference (sym, gsym->ns))
2556 {
2557 gfc_symbol *def_sym;
2558 def_sym = gsym->ns->proc_name;
2559
2560 if (gsym->ns->resolved != -1)
2561 {
2562
2563 /* Resolve the gsymbol namespace if needed. */
2564 if (!gsym->ns->resolved)
2565 {
2566 gfc_symbol *old_dt_list;
2567
2568 /* Stash away derived types so that the backend_decls
2569 do not get mixed up. */
2570 old_dt_list = gfc_derived_types;
2571 gfc_derived_types = NULL;
2572
2573 gfc_resolve (gsym->ns);
2574
2575 /* Store the new derived types with the global namespace. */
2576 if (gfc_derived_types)
2577 gsym->ns->derived_types = gfc_derived_types;
2578
2579 /* Restore the derived types of this namespace. */
2580 gfc_derived_types = old_dt_list;
2581 }
2582
2583 /* Make sure that translation for the gsymbol occurs before
2584 the procedure currently being resolved. */
2585 ns = gfc_global_ns_list;
2586 for (; ns && ns != gsym->ns; ns = ns->sibling)
2587 {
2588 if (ns->sibling == gsym->ns)
2589 {
2590 ns->sibling = gsym->ns->sibling;
2591 gsym->ns->sibling = gfc_global_ns_list;
2592 gfc_global_ns_list = gsym->ns;
2593 break;
2594 }
2595 }
2596
2597 /* This can happen if a binding name has been specified. */
2598 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2599 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2600
2601 if (def_sym->attr.entry_master || def_sym->attr.entry)
2602 {
2603 gfc_entry_list *entry;
2604 for (entry = gsym->ns->entries; entry; entry = entry->next)
2605 if (strcmp (entry->sym->name, sym->name) == 0)
2606 {
2607 def_sym = entry->sym;
2608 break;
2609 }
2610 }
2611 }
2612
2613 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2614 {
2615 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2616 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2617 gfc_typename (&def_sym->ts));
2618 goto done;
2619 }
2620
2621 if (sym->attr.if_source == IFSRC_UNKNOWN
2622 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2623 {
2624 gfc_error ("Explicit interface required for %qs at %L: %s",
2625 sym->name, &sym->declared_at, reason);
2626 goto done;
2627 }
2628
2629 bool bad_result_characteristics;
2630 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2631 reason, sizeof(reason), NULL, NULL,
2632 &bad_result_characteristics))
2633 {
2634 /* Turn erros into warnings with -std=gnu and -std=legacy,
2635 unless a function returns a wrong type, which can lead
2636 to all kinds of ICEs and wrong code. */
2637
2638 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2639 && !bad_result_characteristics)
2640 gfc_errors_to_warnings (true);
2641
2642 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2643 sym->name, &sym->declared_at, reason);
2644 sym->error = 1;
2645 gfc_errors_to_warnings (false);
2646 goto done;
2647 }
2648 }
2649
2650 done:
2651
2652 if (gsym->type == GSYM_UNKNOWN)
2653 {
2654 gsym->type = type;
2655 gsym->where = *where;
2656 }
2657
2658 gsym->used = 1;
2659 }
2660
2661
2662 /************* Function resolution *************/
2663
2664 /* Resolve a function call known to be generic.
2665 Section 14.1.2.4.1. */
2666
2667 static match
2668 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2669 {
2670 gfc_symbol *s;
2671
2672 if (sym->attr.generic)
2673 {
2674 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2675 if (s != NULL)
2676 {
2677 expr->value.function.name = s->name;
2678 expr->value.function.esym = s;
2679
2680 if (s->ts.type != BT_UNKNOWN)
2681 expr->ts = s->ts;
2682 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2683 expr->ts = s->result->ts;
2684
2685 if (s->as != NULL)
2686 expr->rank = s->as->rank;
2687 else if (s->result != NULL && s->result->as != NULL)
2688 expr->rank = s->result->as->rank;
2689
2690 gfc_set_sym_referenced (expr->value.function.esym);
2691
2692 return MATCH_YES;
2693 }
2694
2695 /* TODO: Need to search for elemental references in generic
2696 interface. */
2697 }
2698
2699 if (sym->attr.intrinsic)
2700 return gfc_intrinsic_func_interface (expr, 0);
2701
2702 return MATCH_NO;
2703 }
2704
2705
2706 static bool
2707 resolve_generic_f (gfc_expr *expr)
2708 {
2709 gfc_symbol *sym;
2710 match m;
2711 gfc_interface *intr = NULL;
2712
2713 sym = expr->symtree->n.sym;
2714
2715 for (;;)
2716 {
2717 m = resolve_generic_f0 (expr, sym);
2718 if (m == MATCH_YES)
2719 return true;
2720 else if (m == MATCH_ERROR)
2721 return false;
2722
2723 generic:
2724 if (!intr)
2725 for (intr = sym->generic; intr; intr = intr->next)
2726 if (gfc_fl_struct (intr->sym->attr.flavor))
2727 break;
2728
2729 if (sym->ns->parent == NULL)
2730 break;
2731 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2732
2733 if (sym == NULL)
2734 break;
2735 if (!generic_sym (sym))
2736 goto generic;
2737 }
2738
2739 /* Last ditch attempt. See if the reference is to an intrinsic
2740 that possesses a matching interface. 14.1.2.4 */
2741 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2742 {
2743 if (gfc_init_expr_flag)
2744 gfc_error ("Function %qs in initialization expression at %L "
2745 "must be an intrinsic function",
2746 expr->symtree->n.sym->name, &expr->where);
2747 else
2748 gfc_error ("There is no specific function for the generic %qs "
2749 "at %L", expr->symtree->n.sym->name, &expr->where);
2750 return false;
2751 }
2752
2753 if (intr)
2754 {
2755 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2756 NULL, false))
2757 return false;
2758 if (!gfc_use_derived (expr->ts.u.derived))
2759 return false;
2760 return resolve_structure_cons (expr, 0);
2761 }
2762
2763 m = gfc_intrinsic_func_interface (expr, 0);
2764 if (m == MATCH_YES)
2765 return true;
2766
2767 if (m == MATCH_NO)
2768 gfc_error ("Generic function %qs at %L is not consistent with a "
2769 "specific intrinsic interface", expr->symtree->n.sym->name,
2770 &expr->where);
2771
2772 return false;
2773 }
2774
2775
2776 /* Resolve a function call known to be specific. */
2777
2778 static match
2779 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2780 {
2781 match m;
2782
2783 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2784 {
2785 if (sym->attr.dummy)
2786 {
2787 sym->attr.proc = PROC_DUMMY;
2788 goto found;
2789 }
2790
2791 sym->attr.proc = PROC_EXTERNAL;
2792 goto found;
2793 }
2794
2795 if (sym->attr.proc == PROC_MODULE
2796 || sym->attr.proc == PROC_ST_FUNCTION
2797 || sym->attr.proc == PROC_INTERNAL)
2798 goto found;
2799
2800 if (sym->attr.intrinsic)
2801 {
2802 m = gfc_intrinsic_func_interface (expr, 1);
2803 if (m == MATCH_YES)
2804 return MATCH_YES;
2805 if (m == MATCH_NO)
2806 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2807 "with an intrinsic", sym->name, &expr->where);
2808
2809 return MATCH_ERROR;
2810 }
2811
2812 return MATCH_NO;
2813
2814 found:
2815 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2816
2817 if (sym->result)
2818 expr->ts = sym->result->ts;
2819 else
2820 expr->ts = sym->ts;
2821 expr->value.function.name = sym->name;
2822 expr->value.function.esym = sym;
2823 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2824 error(s). */
2825 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2826 return MATCH_ERROR;
2827 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2828 expr->rank = CLASS_DATA (sym)->as->rank;
2829 else if (sym->as != NULL)
2830 expr->rank = sym->as->rank;
2831
2832 return MATCH_YES;
2833 }
2834
2835
2836 static bool
2837 resolve_specific_f (gfc_expr *expr)
2838 {
2839 gfc_symbol *sym;
2840 match m;
2841
2842 sym = expr->symtree->n.sym;
2843
2844 for (;;)
2845 {
2846 m = resolve_specific_f0 (sym, expr);
2847 if (m == MATCH_YES)
2848 return true;
2849 if (m == MATCH_ERROR)
2850 return false;
2851
2852 if (sym->ns->parent == NULL)
2853 break;
2854
2855 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2856
2857 if (sym == NULL)
2858 break;
2859 }
2860
2861 gfc_error ("Unable to resolve the specific function %qs at %L",
2862 expr->symtree->n.sym->name, &expr->where);
2863
2864 return true;
2865 }
2866
2867 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2868 candidates in CANDIDATES_LEN. */
2869
2870 static void
2871 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2872 char **&candidates,
2873 size_t &candidates_len)
2874 {
2875 gfc_symtree *p;
2876
2877 if (sym == NULL)
2878 return;
2879 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2880 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2881 vec_push (candidates, candidates_len, sym->name);
2882
2883 p = sym->left;
2884 if (p)
2885 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2886
2887 p = sym->right;
2888 if (p)
2889 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2890 }
2891
2892
2893 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2894
2895 const char*
2896 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2897 {
2898 char **candidates = NULL;
2899 size_t candidates_len = 0;
2900 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2901 return gfc_closest_fuzzy_match (fn, candidates);
2902 }
2903
2904
2905 /* Resolve a procedure call not known to be generic nor specific. */
2906
2907 static bool
2908 resolve_unknown_f (gfc_expr *expr)
2909 {
2910 gfc_symbol *sym;
2911 gfc_typespec *ts;
2912
2913 sym = expr->symtree->n.sym;
2914
2915 if (sym->attr.dummy)
2916 {
2917 sym->attr.proc = PROC_DUMMY;
2918 expr->value.function.name = sym->name;
2919 goto set_type;
2920 }
2921
2922 /* See if we have an intrinsic function reference. */
2923
2924 if (gfc_is_intrinsic (sym, 0, expr->where))
2925 {
2926 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2927 return true;
2928 return false;
2929 }
2930
2931 /* The reference is to an external name. */
2932
2933 sym->attr.proc = PROC_EXTERNAL;
2934 expr->value.function.name = sym->name;
2935 expr->value.function.esym = expr->symtree->n.sym;
2936
2937 if (sym->as != NULL)
2938 expr->rank = sym->as->rank;
2939
2940 /* Type of the expression is either the type of the symbol or the
2941 default type of the symbol. */
2942
2943 set_type:
2944 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2945
2946 if (sym->ts.type != BT_UNKNOWN)
2947 expr->ts = sym->ts;
2948 else
2949 {
2950 ts = gfc_get_default_type (sym->name, sym->ns);
2951
2952 if (ts->type == BT_UNKNOWN)
2953 {
2954 const char *guessed
2955 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2956 if (guessed)
2957 gfc_error ("Function %qs at %L has no IMPLICIT type"
2958 "; did you mean %qs?",
2959 sym->name, &expr->where, guessed);
2960 else
2961 gfc_error ("Function %qs at %L has no IMPLICIT type",
2962 sym->name, &expr->where);
2963 return false;
2964 }
2965 else
2966 expr->ts = *ts;
2967 }
2968
2969 return true;
2970 }
2971
2972
2973 /* Return true, if the symbol is an external procedure. */
2974 static bool
2975 is_external_proc (gfc_symbol *sym)
2976 {
2977 if (!sym->attr.dummy && !sym->attr.contained
2978 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2979 && sym->attr.proc != PROC_ST_FUNCTION
2980 && !sym->attr.proc_pointer
2981 && !sym->attr.use_assoc
2982 && sym->name)
2983 return true;
2984
2985 return false;
2986 }
2987
2988
2989 /* Figure out if a function reference is pure or not. Also set the name
2990 of the function for a potential error message. Return nonzero if the
2991 function is PURE, zero if not. */
2992 static int
2993 pure_stmt_function (gfc_expr *, gfc_symbol *);
2994
2995 int
2996 gfc_pure_function (gfc_expr *e, const char **name)
2997 {
2998 int pure;
2999 gfc_component *comp;
3000
3001 *name = NULL;
3002
3003 if (e->symtree != NULL
3004 && e->symtree->n.sym != NULL
3005 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3006 return pure_stmt_function (e, e->symtree->n.sym);
3007
3008 comp = gfc_get_proc_ptr_comp (e);
3009 if (comp)
3010 {
3011 pure = gfc_pure (comp->ts.interface);
3012 *name = comp->name;
3013 }
3014 else if (e->value.function.esym)
3015 {
3016 pure = gfc_pure (e->value.function.esym);
3017 *name = e->value.function.esym->name;
3018 }
3019 else if (e->value.function.isym)
3020 {
3021 pure = e->value.function.isym->pure
3022 || e->value.function.isym->elemental;
3023 *name = e->value.function.isym->name;
3024 }
3025 else
3026 {
3027 /* Implicit functions are not pure. */
3028 pure = 0;
3029 *name = e->value.function.name;
3030 }
3031
3032 return pure;
3033 }
3034
3035
3036 /* Check if the expression is a reference to an implicitly pure function. */
3037
3038 int
3039 gfc_implicit_pure_function (gfc_expr *e)
3040 {
3041 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3042 if (comp)
3043 return gfc_implicit_pure (comp->ts.interface);
3044 else if (e->value.function.esym)
3045 return gfc_implicit_pure (e->value.function.esym);
3046 else
3047 return 0;
3048 }
3049
3050
3051 static bool
3052 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3053 int *f ATTRIBUTE_UNUSED)
3054 {
3055 const char *name;
3056
3057 /* Don't bother recursing into other statement functions
3058 since they will be checked individually for purity. */
3059 if (e->expr_type != EXPR_FUNCTION
3060 || !e->symtree
3061 || e->symtree->n.sym == sym
3062 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3063 return false;
3064
3065 return gfc_pure_function (e, &name) ? false : true;
3066 }
3067
3068
3069 static int
3070 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3071 {
3072 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3073 }
3074
3075
3076 /* Check if an impure function is allowed in the current context. */
3077
3078 static bool check_pure_function (gfc_expr *e)
3079 {
3080 const char *name = NULL;
3081 if (!gfc_pure_function (e, &name) && name)
3082 {
3083 if (forall_flag)
3084 {
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "FORALL %s", name, &e->where,
3087 forall_flag == 2 ? "mask" : "block");
3088 return false;
3089 }
3090 else if (gfc_do_concurrent_flag)
3091 {
3092 gfc_error ("Reference to impure function %qs at %L inside a "
3093 "DO CONCURRENT %s", name, &e->where,
3094 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3095 return false;
3096 }
3097 else if (gfc_pure (NULL))
3098 {
3099 gfc_error ("Reference to impure function %qs at %L "
3100 "within a PURE procedure", name, &e->where);
3101 return false;
3102 }
3103 if (!gfc_implicit_pure_function (e))
3104 gfc_unset_implicit_pure (NULL);
3105 }
3106 return true;
3107 }
3108
3109
3110 /* Update current procedure's array_outer_dependency flag, considering
3111 a call to procedure SYM. */
3112
3113 static void
3114 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3115 {
3116 /* Check to see if this is a sibling function that has not yet
3117 been resolved. */
3118 gfc_namespace *sibling = gfc_current_ns->sibling;
3119 for (; sibling; sibling = sibling->sibling)
3120 {
3121 if (sibling->proc_name == sym)
3122 {
3123 gfc_resolve (sibling);
3124 break;
3125 }
3126 }
3127
3128 /* If SYM has references to outer arrays, so has the procedure calling
3129 SYM. If SYM is a procedure pointer, we can assume the worst. */
3130 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3131 && gfc_current_ns->proc_name)
3132 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3133 }
3134
3135
3136 /* Resolve a function call, which means resolving the arguments, then figuring
3137 out which entity the name refers to. */
3138
3139 static bool
3140 resolve_function (gfc_expr *expr)
3141 {
3142 gfc_actual_arglist *arg;
3143 gfc_symbol *sym;
3144 bool t;
3145 int temp;
3146 procedure_type p = PROC_INTRINSIC;
3147 bool no_formal_args;
3148
3149 sym = NULL;
3150 if (expr->symtree)
3151 sym = expr->symtree->n.sym;
3152
3153 /* If this is a procedure pointer component, it has already been resolved. */
3154 if (gfc_is_proc_ptr_comp (expr))
3155 return true;
3156
3157 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3158 another caf_get. */
3159 if (sym && sym->attr.intrinsic
3160 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3161 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3162 return true;
3163
3164 if (expr->ref)
3165 {
3166 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3167 &expr->where);
3168 return false;
3169 }
3170
3171 if (sym && sym->attr.intrinsic
3172 && !gfc_resolve_intrinsic (sym, &expr->where))
3173 return false;
3174
3175 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3176 {
3177 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3178 return false;
3179 }
3180
3181 /* If this is a deferred TBP with an abstract interface (which may
3182 of course be referenced), expr->value.function.esym will be set. */
3183 if (sym && sym->attr.abstract && !expr->value.function.esym)
3184 {
3185 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3186 sym->name, &expr->where);
3187 return false;
3188 }
3189
3190 /* If this is a deferred TBP with an abstract interface, its result
3191 cannot be an assumed length character (F2003: C418). */
3192 if (sym && sym->attr.abstract && sym->attr.function
3193 && sym->result->ts.u.cl
3194 && sym->result->ts.u.cl->length == NULL
3195 && !sym->result->ts.deferred)
3196 {
3197 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3198 "character length result (F2008: C418)", sym->name,
3199 &sym->declared_at);
3200 return false;
3201 }
3202
3203 /* Switch off assumed size checking and do this again for certain kinds
3204 of procedure, once the procedure itself is resolved. */
3205 need_full_assumed_size++;
3206
3207 if (expr->symtree && expr->symtree->n.sym)
3208 p = expr->symtree->n.sym->attr.proc;
3209
3210 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3211 inquiry_argument = true;
3212 no_formal_args = sym && is_external_proc (sym)
3213 && gfc_sym_get_dummy_args (sym) == NULL;
3214
3215 if (!resolve_actual_arglist (expr->value.function.actual,
3216 p, no_formal_args))
3217 {
3218 inquiry_argument = false;
3219 return false;
3220 }
3221
3222 inquiry_argument = false;
3223
3224 /* Resume assumed_size checking. */
3225 need_full_assumed_size--;
3226
3227 /* If the procedure is external, check for usage. */
3228 if (sym && is_external_proc (sym))
3229 resolve_global_procedure (sym, &expr->where, 0);
3230
3231 if (sym && sym->ts.type == BT_CHARACTER
3232 && sym->ts.u.cl
3233 && sym->ts.u.cl->length == NULL
3234 && !sym->attr.dummy
3235 && !sym->ts.deferred
3236 && expr->value.function.esym == NULL
3237 && !sym->attr.contained)
3238 {
3239 /* Internal procedures are taken care of in resolve_contained_fntype. */
3240 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3241 "be used at %L since it is not a dummy argument",
3242 sym->name, &expr->where);
3243 return false;
3244 }
3245
3246 /* See if function is already resolved. */
3247
3248 if (expr->value.function.name != NULL
3249 || expr->value.function.isym != NULL)
3250 {
3251 if (expr->ts.type == BT_UNKNOWN)
3252 expr->ts = sym->ts;
3253 t = true;
3254 }
3255 else
3256 {
3257 /* Apply the rules of section 14.1.2. */
3258
3259 switch (procedure_kind (sym))
3260 {
3261 case PTYPE_GENERIC:
3262 t = resolve_generic_f (expr);
3263 break;
3264
3265 case PTYPE_SPECIFIC:
3266 t = resolve_specific_f (expr);
3267 break;
3268
3269 case PTYPE_UNKNOWN:
3270 t = resolve_unknown_f (expr);
3271 break;
3272
3273 default:
3274 gfc_internal_error ("resolve_function(): bad function type");
3275 }
3276 }
3277
3278 /* If the expression is still a function (it might have simplified),
3279 then we check to see if we are calling an elemental function. */
3280
3281 if (expr->expr_type != EXPR_FUNCTION)
3282 return t;
3283
3284 /* Walk the argument list looking for invalid BOZ. */
3285 for (arg = expr->value.function.actual; arg; arg = arg->next)
3286 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3287 {
3288 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3289 "actual argument in a function reference",
3290 &arg->expr->where);
3291 return false;
3292 }
3293
3294 temp = need_full_assumed_size;
3295 need_full_assumed_size = 0;
3296
3297 if (!resolve_elemental_actual (expr, NULL))
3298 return false;
3299
3300 if (omp_workshare_flag
3301 && expr->value.function.esym
3302 && ! gfc_elemental (expr->value.function.esym))
3303 {
3304 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3305 "in WORKSHARE construct", expr->value.function.esym->name,
3306 &expr->where);
3307 t = false;
3308 }
3309
3310 #define GENERIC_ID expr->value.function.isym->id
3311 else if (expr->value.function.actual != NULL
3312 && expr->value.function.isym != NULL
3313 && GENERIC_ID != GFC_ISYM_LBOUND
3314 && GENERIC_ID != GFC_ISYM_LCOBOUND
3315 && GENERIC_ID != GFC_ISYM_UCOBOUND
3316 && GENERIC_ID != GFC_ISYM_LEN
3317 && GENERIC_ID != GFC_ISYM_LOC
3318 && GENERIC_ID != GFC_ISYM_C_LOC
3319 && GENERIC_ID != GFC_ISYM_PRESENT)
3320 {
3321 /* Array intrinsics must also have the last upper bound of an
3322 assumed size array argument. UBOUND and SIZE have to be
3323 excluded from the check if the second argument is anything
3324 than a constant. */
3325
3326 for (arg = expr->value.function.actual; arg; arg = arg->next)
3327 {
3328 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3329 && arg == expr->value.function.actual
3330 && arg->next != NULL && arg->next->expr)
3331 {
3332 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3333 break;
3334
3335 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3336 break;
3337
3338 if ((int)mpz_get_si (arg->next->expr->value.integer)
3339 < arg->expr->rank)
3340 break;
3341 }
3342
3343 if (arg->expr != NULL
3344 && arg->expr->rank > 0
3345 && resolve_assumed_size_actual (arg->expr))
3346 return false;
3347 }
3348 }
3349 #undef GENERIC_ID
3350
3351 need_full_assumed_size = temp;
3352
3353 if (!check_pure_function(expr))
3354 t = false;
3355
3356 /* Functions without the RECURSIVE attribution are not allowed to
3357 * call themselves. */
3358 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3359 {
3360 gfc_symbol *esym;
3361 esym = expr->value.function.esym;
3362
3363 if (is_illegal_recursion (esym, gfc_current_ns))
3364 {
3365 if (esym->attr.entry && esym->ns->entries)
3366 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3367 " function %qs is not RECURSIVE",
3368 esym->name, &expr->where, esym->ns->entries->sym->name);
3369 else
3370 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3371 " is not RECURSIVE", esym->name, &expr->where);
3372
3373 t = false;
3374 }
3375 }
3376
3377 /* Character lengths of use associated functions may contains references to
3378 symbols not referenced from the current program unit otherwise. Make sure
3379 those symbols are marked as referenced. */
3380
3381 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3382 && expr->value.function.esym->attr.use_assoc)
3383 {
3384 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3385 }
3386
3387 /* Make sure that the expression has a typespec that works. */
3388 if (expr->ts.type == BT_UNKNOWN)
3389 {
3390 if (expr->symtree->n.sym->result
3391 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3392 && !expr->symtree->n.sym->result->attr.proc_pointer)
3393 expr->ts = expr->symtree->n.sym->result->ts;
3394 }
3395
3396 if (!expr->ref && !expr->value.function.isym)
3397 {
3398 if (expr->value.function.esym)
3399 update_current_proc_array_outer_dependency (expr->value.function.esym);
3400 else
3401 update_current_proc_array_outer_dependency (sym);
3402 }
3403 else if (expr->ref)
3404 /* typebound procedure: Assume the worst. */
3405 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3406
3407 if (expr->value.function.esym
3408 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3409 gfc_warning (OPT_Wdeprecated_declarations,
3410 "Using function %qs at %L is deprecated",
3411 sym->name, &expr->where);
3412 return t;
3413 }
3414
3415
3416 /************* Subroutine resolution *************/
3417
3418 static bool
3419 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3420 {
3421 if (gfc_pure (sym))
3422 return true;
3423
3424 if (forall_flag)
3425 {
3426 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3427 name, loc);
3428 return false;
3429 }
3430 else if (gfc_do_concurrent_flag)
3431 {
3432 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3433 "PURE", name, loc);
3434 return false;
3435 }
3436 else if (gfc_pure (NULL))
3437 {
3438 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3439 return false;
3440 }
3441
3442 gfc_unset_implicit_pure (NULL);
3443 return true;
3444 }
3445
3446
3447 static match
3448 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3449 {
3450 gfc_symbol *s;
3451
3452 if (sym->attr.generic)
3453 {
3454 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3455 if (s != NULL)
3456 {
3457 c->resolved_sym = s;
3458 if (!pure_subroutine (s, s->name, &c->loc))
3459 return MATCH_ERROR;
3460 return MATCH_YES;
3461 }
3462
3463 /* TODO: Need to search for elemental references in generic interface. */
3464 }
3465
3466 if (sym->attr.intrinsic)
3467 return gfc_intrinsic_sub_interface (c, 0);
3468
3469 return MATCH_NO;
3470 }
3471
3472
3473 static bool
3474 resolve_generic_s (gfc_code *c)
3475 {
3476 gfc_symbol *sym;
3477 match m;
3478
3479 sym = c->symtree->n.sym;
3480
3481 for (;;)
3482 {
3483 m = resolve_generic_s0 (c, sym);
3484 if (m == MATCH_YES)
3485 return true;
3486 else if (m == MATCH_ERROR)
3487 return false;
3488
3489 generic:
3490 if (sym->ns->parent == NULL)
3491 break;
3492 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3493
3494 if (sym == NULL)
3495 break;
3496 if (!generic_sym (sym))
3497 goto generic;
3498 }
3499
3500 /* Last ditch attempt. See if the reference is to an intrinsic
3501 that possesses a matching interface. 14.1.2.4 */
3502 sym = c->symtree->n.sym;
3503
3504 if (!gfc_is_intrinsic (sym, 1, c->loc))
3505 {
3506 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3507 sym->name, &c->loc);
3508 return false;
3509 }
3510
3511 m = gfc_intrinsic_sub_interface (c, 0);
3512 if (m == MATCH_YES)
3513 return true;
3514 if (m == MATCH_NO)
3515 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3516 "intrinsic subroutine interface", sym->name, &c->loc);
3517
3518 return false;
3519 }
3520
3521
3522 /* Resolve a subroutine call known to be specific. */
3523
3524 static match
3525 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3526 {
3527 match m;
3528
3529 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3530 {
3531 if (sym->attr.dummy)
3532 {
3533 sym->attr.proc = PROC_DUMMY;
3534 goto found;
3535 }
3536
3537 sym->attr.proc = PROC_EXTERNAL;
3538 goto found;
3539 }
3540
3541 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3542 goto found;
3543
3544 if (sym->attr.intrinsic)
3545 {
3546 m = gfc_intrinsic_sub_interface (c, 1);
3547 if (m == MATCH_YES)
3548 return MATCH_YES;
3549 if (m == MATCH_NO)
3550 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3551 "with an intrinsic", sym->name, &c->loc);
3552
3553 return MATCH_ERROR;
3554 }
3555
3556 return MATCH_NO;
3557
3558 found:
3559 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3560
3561 c->resolved_sym = sym;
3562 if (!pure_subroutine (sym, sym->name, &c->loc))
3563 return MATCH_ERROR;
3564
3565 return MATCH_YES;
3566 }
3567
3568
3569 static bool
3570 resolve_specific_s (gfc_code *c)
3571 {
3572 gfc_symbol *sym;
3573 match m;
3574
3575 sym = c->symtree->n.sym;
3576
3577 for (;;)
3578 {
3579 m = resolve_specific_s0 (c, sym);
3580 if (m == MATCH_YES)
3581 return true;
3582 if (m == MATCH_ERROR)
3583 return false;
3584
3585 if (sym->ns->parent == NULL)
3586 break;
3587
3588 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3589
3590 if (sym == NULL)
3591 break;
3592 }
3593
3594 sym = c->symtree->n.sym;
3595 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3596 sym->name, &c->loc);
3597
3598 return false;
3599 }
3600
3601
3602 /* Resolve a subroutine call not known to be generic nor specific. */
3603
3604 static bool
3605 resolve_unknown_s (gfc_code *c)
3606 {
3607 gfc_symbol *sym;
3608
3609 sym = c->symtree->n.sym;
3610
3611 if (sym->attr.dummy)
3612 {
3613 sym->attr.proc = PROC_DUMMY;
3614 goto found;
3615 }
3616
3617 /* See if we have an intrinsic function reference. */
3618
3619 if (gfc_is_intrinsic (sym, 1, c->loc))
3620 {
3621 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3622 return true;
3623 return false;
3624 }
3625
3626 /* The reference is to an external name. */
3627
3628 found:
3629 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3630
3631 c->resolved_sym = sym;
3632
3633 return pure_subroutine (sym, sym->name, &c->loc);
3634 }
3635
3636
3637 /* Resolve a subroutine call. Although it was tempting to use the same code
3638 for functions, subroutines and functions are stored differently and this
3639 makes things awkward. */
3640
3641 static bool
3642 resolve_call (gfc_code *c)
3643 {
3644 bool t;
3645 procedure_type ptype = PROC_INTRINSIC;
3646 gfc_symbol *csym, *sym;
3647 bool no_formal_args;
3648
3649 csym = c->symtree ? c->symtree->n.sym : NULL;
3650
3651 if (csym && csym->ts.type != BT_UNKNOWN)
3652 {
3653 gfc_error ("%qs at %L has a type, which is not consistent with "
3654 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3655 return false;
3656 }
3657
3658 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3659 {
3660 gfc_symtree *st;
3661 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3662 sym = st ? st->n.sym : NULL;
3663 if (sym && csym != sym
3664 && sym->ns == gfc_current_ns
3665 && sym->attr.flavor == FL_PROCEDURE
3666 && sym->attr.contained)
3667 {
3668 sym->refs++;
3669 if (csym->attr.generic)
3670 c->symtree->n.sym = sym;
3671 else
3672 c->symtree = st;
3673 csym = c->symtree->n.sym;
3674 }
3675 }
3676
3677 /* If this ia a deferred TBP, c->expr1 will be set. */
3678 if (!c->expr1 && csym)
3679 {
3680 if (csym->attr.abstract)
3681 {
3682 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3683 csym->name, &c->loc);
3684 return false;
3685 }
3686
3687 /* Subroutines without the RECURSIVE attribution are not allowed to
3688 call themselves. */
3689 if (is_illegal_recursion (csym, gfc_current_ns))
3690 {
3691 if (csym->attr.entry && csym->ns->entries)
3692 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3693 "as subroutine %qs is not RECURSIVE",
3694 csym->name, &c->loc, csym->ns->entries->sym->name);
3695 else
3696 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3697 "as it is not RECURSIVE", csym->name, &c->loc);
3698
3699 t = false;
3700 }
3701 }
3702
3703 /* Switch off assumed size checking and do this again for certain kinds
3704 of procedure, once the procedure itself is resolved. */
3705 need_full_assumed_size++;
3706
3707 if (csym)
3708 ptype = csym->attr.proc;
3709
3710 no_formal_args = csym && is_external_proc (csym)
3711 && gfc_sym_get_dummy_args (csym) == NULL;
3712 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3713 return false;
3714
3715 /* Resume assumed_size checking. */
3716 need_full_assumed_size--;
3717
3718 /* If external, check for usage. */
3719 if (csym && is_external_proc (csym))
3720 resolve_global_procedure (csym, &c->loc, 1);
3721
3722 t = true;
3723 if (c->resolved_sym == NULL)
3724 {
3725 c->resolved_isym = NULL;
3726 switch (procedure_kind (csym))
3727 {
3728 case PTYPE_GENERIC:
3729 t = resolve_generic_s (c);
3730 break;
3731
3732 case PTYPE_SPECIFIC:
3733 t = resolve_specific_s (c);
3734 break;
3735
3736 case PTYPE_UNKNOWN:
3737 t = resolve_unknown_s (c);
3738 break;
3739
3740 default:
3741 gfc_internal_error ("resolve_subroutine(): bad function type");
3742 }
3743 }
3744
3745 /* Some checks of elemental subroutine actual arguments. */
3746 if (!resolve_elemental_actual (NULL, c))
3747 return false;
3748
3749 if (!c->expr1)
3750 update_current_proc_array_outer_dependency (csym);
3751 else
3752 /* Typebound procedure: Assume the worst. */
3753 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3754
3755 if (c->resolved_sym
3756 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3757 gfc_warning (OPT_Wdeprecated_declarations,
3758 "Using subroutine %qs at %L is deprecated",
3759 c->resolved_sym->name, &c->loc);
3760
3761 return t;
3762 }
3763
3764
3765 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3766 op1->shape and op2->shape are non-NULL return true if their shapes
3767 match. If both op1->shape and op2->shape are non-NULL return false
3768 if their shapes do not match. If either op1->shape or op2->shape is
3769 NULL, return true. */
3770
3771 static bool
3772 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3773 {
3774 bool t;
3775 int i;
3776
3777 t = true;
3778
3779 if (op1->shape != NULL && op2->shape != NULL)
3780 {
3781 for (i = 0; i < op1->rank; i++)
3782 {
3783 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3784 {
3785 gfc_error ("Shapes for operands at %L and %L are not conformable",
3786 &op1->where, &op2->where);
3787 t = false;
3788 break;
3789 }
3790 }
3791 }
3792
3793 return t;
3794 }
3795
3796 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3797 For example A .AND. B becomes IAND(A, B). */
3798 static gfc_expr *
3799 logical_to_bitwise (gfc_expr *e)
3800 {
3801 gfc_expr *tmp, *op1, *op2;
3802 gfc_isym_id isym;
3803 gfc_actual_arglist *args = NULL;
3804
3805 gcc_assert (e->expr_type == EXPR_OP);
3806
3807 isym = GFC_ISYM_NONE;
3808 op1 = e->value.op.op1;
3809 op2 = e->value.op.op2;
3810
3811 switch (e->value.op.op)
3812 {
3813 case INTRINSIC_NOT:
3814 isym = GFC_ISYM_NOT;
3815 break;
3816 case INTRINSIC_AND:
3817 isym = GFC_ISYM_IAND;
3818 break;
3819 case INTRINSIC_OR:
3820 isym = GFC_ISYM_IOR;
3821 break;
3822 case INTRINSIC_NEQV:
3823 isym = GFC_ISYM_IEOR;
3824 break;
3825 case INTRINSIC_EQV:
3826 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3827 Change the old expression to NEQV, which will get replaced by IEOR,
3828 and wrap it in NOT. */
3829 tmp = gfc_copy_expr (e);
3830 tmp->value.op.op = INTRINSIC_NEQV;
3831 tmp = logical_to_bitwise (tmp);
3832 isym = GFC_ISYM_NOT;
3833 op1 = tmp;
3834 op2 = NULL;
3835 break;
3836 default:
3837 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3838 }
3839
3840 /* Inherit the original operation's operands as arguments. */
3841 args = gfc_get_actual_arglist ();
3842 args->expr = op1;
3843 if (op2)
3844 {
3845 args->next = gfc_get_actual_arglist ();
3846 args->next->expr = op2;
3847 }
3848
3849 /* Convert the expression to a function call. */
3850 e->expr_type = EXPR_FUNCTION;
3851 e->value.function.actual = args;
3852 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3853 e->value.function.name = e->value.function.isym->name;
3854 e->value.function.esym = NULL;
3855
3856 /* Make up a pre-resolved function call symtree if we need to. */
3857 if (!e->symtree || !e->symtree->n.sym)
3858 {
3859 gfc_symbol *sym;
3860 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3861 sym = e->symtree->n.sym;
3862 sym->result = sym;
3863 sym->attr.flavor = FL_PROCEDURE;
3864 sym->attr.function = 1;
3865 sym->attr.elemental = 1;
3866 sym->attr.pure = 1;
3867 sym->attr.referenced = 1;
3868 gfc_intrinsic_symbol (sym);
3869 gfc_commit_symbol (sym);
3870 }
3871
3872 args->name = e->value.function.isym->formal->name;
3873 if (e->value.function.isym->formal->next)
3874 args->next->name = e->value.function.isym->formal->next->name;
3875
3876 return e;
3877 }
3878
3879 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3880 candidates in CANDIDATES_LEN. */
3881 static void
3882 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3883 char **&candidates,
3884 size_t &candidates_len)
3885 {
3886 gfc_symtree *p;
3887
3888 if (uop == NULL)
3889 return;
3890
3891 /* Not sure how to properly filter here. Use all for a start.
3892 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3893 these as i suppose they don't make terribly sense. */
3894
3895 if (uop->n.uop->op != NULL)
3896 vec_push (candidates, candidates_len, uop->name);
3897
3898 p = uop->left;
3899 if (p)
3900 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3901
3902 p = uop->right;
3903 if (p)
3904 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3905 }
3906
3907 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3908
3909 static const char*
3910 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3911 {
3912 char **candidates = NULL;
3913 size_t candidates_len = 0;
3914 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3915 return gfc_closest_fuzzy_match (op, candidates);
3916 }
3917
3918
3919 /* Callback finding an impure function as an operand to an .and. or
3920 .or. expression. Remember the last function warned about to
3921 avoid double warnings when recursing. */
3922
3923 static int
3924 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3925 void *data)
3926 {
3927 gfc_expr *f = *e;
3928 const char *name;
3929 static gfc_expr *last = NULL;
3930 bool *found = (bool *) data;
3931
3932 if (f->expr_type == EXPR_FUNCTION)
3933 {
3934 *found = 1;
3935 if (f != last && !gfc_pure_function (f, &name)
3936 && !gfc_implicit_pure_function (f))
3937 {
3938 if (name)
3939 gfc_warning (OPT_Wfunction_elimination,
3940 "Impure function %qs at %L might not be evaluated",
3941 name, &f->where);
3942 else
3943 gfc_warning (OPT_Wfunction_elimination,
3944 "Impure function at %L might not be evaluated",
3945 &f->where);
3946 }
3947 last = f;
3948 }
3949
3950 return 0;
3951 }
3952
3953 /* Return true if TYPE is character based, false otherwise. */
3954
3955 static int
3956 is_character_based (bt type)
3957 {
3958 return type == BT_CHARACTER || type == BT_HOLLERITH;
3959 }
3960
3961
3962 /* If expression is a hollerith, convert it to character and issue a warning
3963 for the conversion. */
3964
3965 static void
3966 convert_hollerith_to_character (gfc_expr *e)
3967 {
3968 if (e->ts.type == BT_HOLLERITH)
3969 {
3970 gfc_typespec t;
3971 gfc_clear_ts (&t);
3972 t.type = BT_CHARACTER;
3973 t.kind = e->ts.kind;
3974 gfc_convert_type_warn (e, &t, 2, 1);
3975 }
3976 }
3977
3978 /* Convert to numeric and issue a warning for the conversion. */
3979
3980 static void
3981 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3982 {
3983 gfc_typespec t;
3984 gfc_clear_ts (&t);
3985 t.type = b->ts.type;
3986 t.kind = b->ts.kind;
3987 gfc_convert_type_warn (a, &t, 2, 1);
3988 }
3989
3990 /* Resolve an operator expression node. This can involve replacing the
3991 operation with a user defined function call. */
3992
3993 static bool
3994 resolve_operator (gfc_expr *e)
3995 {
3996 gfc_expr *op1, *op2;
3997 /* One error uses 3 names; additional space for wording (also via gettext). */
3998 char msg[3*GFC_MAX_SYMBOL_LEN + 1 + 50];
3999 bool dual_locus_error;
4000 bool t = true;
4001
4002 /* Resolve all subnodes-- give them types. */
4003
4004 switch (e->value.op.op)
4005 {
4006 default:
4007 if (!gfc_resolve_expr (e->value.op.op2))
4008 return false;
4009
4010 /* Fall through. */
4011
4012 case INTRINSIC_NOT:
4013 case INTRINSIC_UPLUS:
4014 case INTRINSIC_UMINUS:
4015 case INTRINSIC_PARENTHESES:
4016 if (!gfc_resolve_expr (e->value.op.op1))
4017 return false;
4018 if (e->value.op.op1
4019 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4020 {
4021 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4022 "unary operator %qs", &e->value.op.op1->where,
4023 gfc_op2string (e->value.op.op));
4024 return false;
4025 }
4026 break;
4027 }
4028
4029 /* Typecheck the new node. */
4030
4031 op1 = e->value.op.op1;
4032 op2 = e->value.op.op2;
4033 if (op1 == NULL && op2 == NULL)
4034 return false;
4035
4036 dual_locus_error = false;
4037
4038 /* op1 and op2 cannot both be BOZ. */
4039 if (op1 && op1->ts.type == BT_BOZ
4040 && op2 && op2->ts.type == BT_BOZ)
4041 {
4042 gfc_error ("Operands at %L and %L cannot appear as operands of "
4043 "binary operator %qs", &op1->where, &op2->where,
4044 gfc_op2string (e->value.op.op));
4045 return false;
4046 }
4047
4048 if ((op1 && op1->expr_type == EXPR_NULL)
4049 || (op2 && op2->expr_type == EXPR_NULL))
4050 {
4051 snprintf (msg, sizeof (msg),
4052 _("Invalid context for NULL() pointer at %%L"));
4053 goto bad_op;
4054 }
4055
4056 switch (e->value.op.op)
4057 {
4058 case INTRINSIC_UPLUS:
4059 case INTRINSIC_UMINUS:
4060 if (op1->ts.type == BT_INTEGER
4061 || op1->ts.type == BT_REAL
4062 || op1->ts.type == BT_COMPLEX)
4063 {
4064 e->ts = op1->ts;
4065 break;
4066 }
4067
4068 snprintf (msg, sizeof (msg),
4069 _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4070 gfc_op2string (e->value.op.op), gfc_typename (e));
4071 goto bad_op;
4072
4073 case INTRINSIC_PLUS:
4074 case INTRINSIC_MINUS:
4075 case INTRINSIC_TIMES:
4076 case INTRINSIC_DIVIDE:
4077 case INTRINSIC_POWER:
4078 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4079 {
4080 gfc_type_convert_binary (e, 1);
4081 break;
4082 }
4083
4084 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4085 snprintf (msg, sizeof (msg),
4086 _("Unexpected derived-type entities in binary intrinsic "
4087 "numeric operator %%<%s%%> at %%L"),
4088 gfc_op2string (e->value.op.op));
4089 else
4090 snprintf (msg, sizeof(msg),
4091 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4092 gfc_op2string (e->value.op.op), gfc_typename (op1),
4093 gfc_typename (op2));
4094 goto bad_op;
4095
4096 case INTRINSIC_CONCAT:
4097 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4098 && op1->ts.kind == op2->ts.kind)
4099 {
4100 e->ts.type = BT_CHARACTER;
4101 e->ts.kind = op1->ts.kind;
4102 break;
4103 }
4104
4105 snprintf (msg, sizeof (msg),
4106 _("Operands of string concatenation operator at %%L are %s/%s"),
4107 gfc_typename (op1), gfc_typename (op2));
4108 goto bad_op;
4109
4110 case INTRINSIC_AND:
4111 case INTRINSIC_OR:
4112 case INTRINSIC_EQV:
4113 case INTRINSIC_NEQV:
4114 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4115 {
4116 e->ts.type = BT_LOGICAL;
4117 e->ts.kind = gfc_kind_max (op1, op2);
4118 if (op1->ts.kind < e->ts.kind)
4119 gfc_convert_type (op1, &e->ts, 2);
4120 else if (op2->ts.kind < e->ts.kind)
4121 gfc_convert_type (op2, &e->ts, 2);
4122
4123 if (flag_frontend_optimize &&
4124 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4125 {
4126 /* Warn about short-circuiting
4127 with impure function as second operand. */
4128 bool op2_f = false;
4129 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4130 }
4131 break;
4132 }
4133
4134 /* Logical ops on integers become bitwise ops with -fdec. */
4135 else if (flag_dec
4136 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4137 {
4138 e->ts.type = BT_INTEGER;
4139 e->ts.kind = gfc_kind_max (op1, op2);
4140 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4141 gfc_convert_type (op1, &e->ts, 1);
4142 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4143 gfc_convert_type (op2, &e->ts, 1);
4144 e = logical_to_bitwise (e);
4145 goto simplify_op;
4146 }
4147
4148 snprintf (msg, sizeof (msg),
4149 _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4150 gfc_op2string (e->value.op.op), gfc_typename (op1),
4151 gfc_typename (op2));
4152
4153 goto bad_op;
4154
4155 case INTRINSIC_NOT:
4156 /* Logical ops on integers become bitwise ops with -fdec. */
4157 if (flag_dec && op1->ts.type == BT_INTEGER)
4158 {
4159 e->ts.type = BT_INTEGER;
4160 e->ts.kind = op1->ts.kind;
4161 e = logical_to_bitwise (e);
4162 goto simplify_op;
4163 }
4164
4165 if (op1->ts.type == BT_LOGICAL)
4166 {
4167 e->ts.type = BT_LOGICAL;
4168 e->ts.kind = op1->ts.kind;
4169 break;
4170 }
4171
4172 snprintf (msg, sizeof (msg), _("Operand of .not. operator at %%L is %s"),
4173 gfc_typename (op1));
4174 goto bad_op;
4175
4176 case INTRINSIC_GT:
4177 case INTRINSIC_GT_OS:
4178 case INTRINSIC_GE:
4179 case INTRINSIC_GE_OS:
4180 case INTRINSIC_LT:
4181 case INTRINSIC_LT_OS:
4182 case INTRINSIC_LE:
4183 case INTRINSIC_LE_OS:
4184 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4185 {
4186 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4187 goto bad_op;
4188 }
4189
4190 /* Fall through. */
4191
4192 case INTRINSIC_EQ:
4193 case INTRINSIC_EQ_OS:
4194 case INTRINSIC_NE:
4195 case INTRINSIC_NE_OS:
4196
4197 if (flag_dec
4198 && is_character_based (op1->ts.type)
4199 && is_character_based (op2->ts.type))
4200 {
4201 convert_hollerith_to_character (op1);
4202 convert_hollerith_to_character (op2);
4203 }
4204
4205 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4206 && op1->ts.kind == op2->ts.kind)
4207 {
4208 e->ts.type = BT_LOGICAL;
4209 e->ts.kind = gfc_default_logical_kind;
4210 break;
4211 }
4212
4213 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4214 if (op1->ts.type == BT_BOZ)
4215 {
4216 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4217 "as an operand of a relational operator"),
4218 &op1->where))
4219 return false;
4220
4221 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4222 return false;
4223
4224 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4225 return false;
4226 }
4227
4228 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4229 if (op2->ts.type == BT_BOZ)
4230 {
4231 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4232 " as an operand of a relational operator"),
4233 &op2->where))
4234 return false;
4235
4236 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4237 return false;
4238
4239 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4240 return false;
4241 }
4242 if (flag_dec
4243 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4244 convert_to_numeric (op1, op2);
4245
4246 if (flag_dec
4247 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4248 convert_to_numeric (op2, op1);
4249
4250 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4251 {
4252 gfc_type_convert_binary (e, 1);
4253
4254 e->ts.type = BT_LOGICAL;
4255 e->ts.kind = gfc_default_logical_kind;
4256
4257 if (warn_compare_reals)
4258 {
4259 gfc_intrinsic_op op = e->value.op.op;
4260
4261 /* Type conversion has made sure that the types of op1 and op2
4262 agree, so it is only necessary to check the first one. */
4263 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4264 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4265 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4266 {
4267 const char *msg;
4268
4269 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4270 msg = G_("Equality comparison for %s at %L");
4271 else
4272 msg = G_("Inequality comparison for %s at %L");
4273
4274 gfc_warning (OPT_Wcompare_reals, msg,
4275 gfc_typename (op1), &op1->where);
4276 }
4277 }
4278
4279 break;
4280 }
4281
4282 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4283 snprintf (msg, sizeof (msg),
4284 _("Logicals at %%L must be compared with %s instead of %s"),
4285 (e->value.op.op == INTRINSIC_EQ
4286 || e->value.op.op == INTRINSIC_EQ_OS)
4287 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4288 else
4289 snprintf (msg, sizeof (msg),
4290 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4291 gfc_op2string (e->value.op.op), gfc_typename (op1),
4292 gfc_typename (op2));
4293
4294 goto bad_op;
4295
4296 case INTRINSIC_USER:
4297 if (e->value.op.uop->op == NULL)
4298 {
4299 const char *name = e->value.op.uop->name;
4300 const char *guessed;
4301 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4302 if (guessed)
4303 snprintf (msg, sizeof (msg),
4304 _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4305 name, guessed);
4306 else
4307 snprintf (msg, sizeof (msg), _("Unknown operator %%<%s%%> at %%L"),
4308 name);
4309 }
4310 else if (op2 == NULL)
4311 snprintf (msg, sizeof (msg),
4312 _("Operand of user operator %%<%s%%> at %%L is %s"),
4313 e->value.op.uop->name, gfc_typename (op1));
4314 else
4315 {
4316 snprintf (msg, sizeof (msg),
4317 _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4318 e->value.op.uop->name, gfc_typename (op1),
4319 gfc_typename (op2));
4320 e->value.op.uop->op->sym->attr.referenced = 1;
4321 }
4322
4323 goto bad_op;
4324
4325 case INTRINSIC_PARENTHESES:
4326 e->ts = op1->ts;
4327 if (e->ts.type == BT_CHARACTER)
4328 e->ts.u.cl = op1->ts.u.cl;
4329 break;
4330
4331 default:
4332 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4333 }
4334
4335 /* Deal with arrayness of an operand through an operator. */
4336
4337 switch (e->value.op.op)
4338 {
4339 case INTRINSIC_PLUS:
4340 case INTRINSIC_MINUS:
4341 case INTRINSIC_TIMES:
4342 case INTRINSIC_DIVIDE:
4343 case INTRINSIC_POWER:
4344 case INTRINSIC_CONCAT:
4345 case INTRINSIC_AND:
4346 case INTRINSIC_OR:
4347 case INTRINSIC_EQV:
4348 case INTRINSIC_NEQV:
4349 case INTRINSIC_EQ:
4350 case INTRINSIC_EQ_OS:
4351 case INTRINSIC_NE:
4352 case INTRINSIC_NE_OS:
4353 case INTRINSIC_GT:
4354 case INTRINSIC_GT_OS:
4355 case INTRINSIC_GE:
4356 case INTRINSIC_GE_OS:
4357 case INTRINSIC_LT:
4358 case INTRINSIC_LT_OS:
4359 case INTRINSIC_LE:
4360 case INTRINSIC_LE_OS:
4361
4362 if (op1->rank == 0 && op2->rank == 0)
4363 e->rank = 0;
4364
4365 if (op1->rank == 0 && op2->rank != 0)
4366 {
4367 e->rank = op2->rank;
4368
4369 if (e->shape == NULL)
4370 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4371 }
4372
4373 if (op1->rank != 0 && op2->rank == 0)
4374 {
4375 e->rank = op1->rank;
4376
4377 if (e->shape == NULL)
4378 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4379 }
4380
4381 if (op1->rank != 0 && op2->rank != 0)
4382 {
4383 if (op1->rank == op2->rank)
4384 {
4385 e->rank = op1->rank;
4386 if (e->shape == NULL)
4387 {
4388 t = compare_shapes (op1, op2);
4389 if (!t)
4390 e->shape = NULL;
4391 else
4392 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4393 }
4394 }
4395 else
4396 {
4397 /* Allow higher level expressions to work. */
4398 e->rank = 0;
4399
4400 /* Try user-defined operators, and otherwise throw an error. */
4401 dual_locus_error = true;
4402 snprintf (msg, sizeof (msg),
4403 _("Inconsistent ranks for operator at %%L and %%L"));
4404 goto bad_op;
4405 }
4406 }
4407
4408 break;
4409
4410 case INTRINSIC_PARENTHESES:
4411 case INTRINSIC_NOT:
4412 case INTRINSIC_UPLUS:
4413 case INTRINSIC_UMINUS:
4414 /* Simply copy arrayness attribute */
4415 e->rank = op1->rank;
4416
4417 if (e->shape == NULL)
4418 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4419
4420 break;
4421
4422 default:
4423 break;
4424 }
4425
4426 simplify_op:
4427
4428 /* Attempt to simplify the expression. */
4429 if (t)
4430 {
4431 t = gfc_simplify_expr (e, 0);
4432 /* Some calls do not succeed in simplification and return false
4433 even though there is no error; e.g. variable references to
4434 PARAMETER arrays. */
4435 if (!gfc_is_constant_expr (e))
4436 t = true;
4437 }
4438 return t;
4439
4440 bad_op:
4441
4442 {
4443 match m = gfc_extend_expr (e);
4444 if (m == MATCH_YES)
4445 return true;
4446 if (m == MATCH_ERROR)
4447 return false;
4448 }
4449
4450 if (dual_locus_error)
4451 gfc_error (msg, &op1->where, &op2->where);
4452 else
4453 gfc_error (msg, &e->where);
4454
4455 return false;
4456 }
4457
4458
4459 /************** Array resolution subroutines **************/
4460
4461 enum compare_result
4462 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4463
4464 /* Compare two integer expressions. */
4465
4466 static compare_result
4467 compare_bound (gfc_expr *a, gfc_expr *b)
4468 {
4469 int i;
4470
4471 if (a == NULL || a->expr_type != EXPR_CONSTANT
4472 || b == NULL || b->expr_type != EXPR_CONSTANT)
4473 return CMP_UNKNOWN;
4474
4475 /* If either of the types isn't INTEGER, we must have
4476 raised an error earlier. */
4477
4478 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4479 return CMP_UNKNOWN;
4480
4481 i = mpz_cmp (a->value.integer, b->value.integer);
4482
4483 if (i < 0)
4484 return CMP_LT;
4485 if (i > 0)
4486 return CMP_GT;
4487 return CMP_EQ;
4488 }
4489
4490
4491 /* Compare an integer expression with an integer. */
4492
4493 static compare_result
4494 compare_bound_int (gfc_expr *a, int b)
4495 {
4496 int i;
4497
4498 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4499 return CMP_UNKNOWN;
4500
4501 if (a->ts.type != BT_INTEGER)
4502 gfc_internal_error ("compare_bound_int(): Bad expression");
4503
4504 i = mpz_cmp_si (a->value.integer, b);
4505
4506 if (i < 0)
4507 return CMP_LT;
4508 if (i > 0)
4509 return CMP_GT;
4510 return CMP_EQ;
4511 }
4512
4513
4514 /* Compare an integer expression with a mpz_t. */
4515
4516 static compare_result
4517 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4518 {
4519 int i;
4520
4521 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4522 return CMP_UNKNOWN;
4523
4524 if (a->ts.type != BT_INTEGER)
4525 gfc_internal_error ("compare_bound_int(): Bad expression");
4526
4527 i = mpz_cmp (a->value.integer, b);
4528
4529 if (i < 0)
4530 return CMP_LT;
4531 if (i > 0)
4532 return CMP_GT;
4533 return CMP_EQ;
4534 }
4535
4536
4537 /* Compute the last value of a sequence given by a triplet.
4538 Return 0 if it wasn't able to compute the last value, or if the
4539 sequence if empty, and 1 otherwise. */
4540
4541 static int
4542 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4543 gfc_expr *stride, mpz_t last)
4544 {
4545 mpz_t rem;
4546
4547 if (start == NULL || start->expr_type != EXPR_CONSTANT
4548 || end == NULL || end->expr_type != EXPR_CONSTANT
4549 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4550 return 0;
4551
4552 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4553 || (stride != NULL && stride->ts.type != BT_INTEGER))
4554 return 0;
4555
4556 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4557 {
4558 if (compare_bound (start, end) == CMP_GT)
4559 return 0;
4560 mpz_set (last, end->value.integer);
4561 return 1;
4562 }
4563
4564 if (compare_bound_int (stride, 0) == CMP_GT)
4565 {
4566 /* Stride is positive */
4567 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4568 return 0;
4569 }
4570 else
4571 {
4572 /* Stride is negative */
4573 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4574 return 0;
4575 }
4576
4577 mpz_init (rem);
4578 mpz_sub (rem, end->value.integer, start->value.integer);
4579 mpz_tdiv_r (rem, rem, stride->value.integer);
4580 mpz_sub (last, end->value.integer, rem);
4581 mpz_clear (rem);
4582
4583 return 1;
4584 }
4585
4586
4587 /* Compare a single dimension of an array reference to the array
4588 specification. */
4589
4590 static bool
4591 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4592 {
4593 mpz_t last_value;
4594
4595 if (ar->dimen_type[i] == DIMEN_STAR)
4596 {
4597 gcc_assert (ar->stride[i] == NULL);
4598 /* This implies [*] as [*:] and [*:3] are not possible. */
4599 if (ar->start[i] == NULL)
4600 {
4601 gcc_assert (ar->end[i] == NULL);
4602 return true;
4603 }
4604 }
4605
4606 /* Given start, end and stride values, calculate the minimum and
4607 maximum referenced indexes. */
4608
4609 switch (ar->dimen_type[i])
4610 {
4611 case DIMEN_VECTOR:
4612 case DIMEN_THIS_IMAGE:
4613 break;
4614
4615 case DIMEN_STAR:
4616 case DIMEN_ELEMENT:
4617 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4618 {
4619 if (i < as->rank)
4620 gfc_warning (0, "Array reference at %L is out of bounds "
4621 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4622 mpz_get_si (ar->start[i]->value.integer),
4623 mpz_get_si (as->lower[i]->value.integer), i+1);
4624 else
4625 gfc_warning (0, "Array reference at %L is out of bounds "
4626 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4627 mpz_get_si (ar->start[i]->value.integer),
4628 mpz_get_si (as->lower[i]->value.integer),
4629 i + 1 - as->rank);
4630 return true;
4631 }
4632 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4633 {
4634 if (i < as->rank)
4635 gfc_warning (0, "Array reference at %L is out of bounds "
4636 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4637 mpz_get_si (ar->start[i]->value.integer),
4638 mpz_get_si (as->upper[i]->value.integer), i+1);
4639 else
4640 gfc_warning (0, "Array reference at %L is out of bounds "
4641 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4642 mpz_get_si (ar->start[i]->value.integer),
4643 mpz_get_si (as->upper[i]->value.integer),
4644 i + 1 - as->rank);
4645 return true;
4646 }
4647
4648 break;
4649
4650 case DIMEN_RANGE:
4651 {
4652 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4653 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4654
4655 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4656
4657 /* Check for zero stride, which is not allowed. */
4658 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4659 {
4660 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4661 return false;
4662 }
4663
4664 /* if start == len || (stride > 0 && start < len)
4665 || (stride < 0 && start > len),
4666 then the array section contains at least one element. In this
4667 case, there is an out-of-bounds access if
4668 (start < lower || start > upper). */
4669 if (compare_bound (AR_START, AR_END) == CMP_EQ
4670 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4671 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4672 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4673 && comp_start_end == CMP_GT))
4674 {
4675 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4676 {
4677 gfc_warning (0, "Lower array reference at %L is out of bounds "
4678 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4679 mpz_get_si (AR_START->value.integer),
4680 mpz_get_si (as->lower[i]->value.integer), i+1);
4681 return true;
4682 }
4683 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4684 {
4685 gfc_warning (0, "Lower array reference at %L is out of bounds "
4686 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4687 mpz_get_si (AR_START->value.integer),
4688 mpz_get_si (as->upper[i]->value.integer), i+1);
4689 return true;
4690 }
4691 }
4692
4693 /* If we can compute the highest index of the array section,
4694 then it also has to be between lower and upper. */
4695 mpz_init (last_value);
4696 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4697 last_value))
4698 {
4699 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4700 {
4701 gfc_warning (0, "Upper array reference at %L is out of bounds "
4702 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4703 mpz_get_si (last_value),
4704 mpz_get_si (as->lower[i]->value.integer), i+1);
4705 mpz_clear (last_value);
4706 return true;
4707 }
4708 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4709 {
4710 gfc_warning (0, "Upper array reference at %L is out of bounds "
4711 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4712 mpz_get_si (last_value),
4713 mpz_get_si (as->upper[i]->value.integer), i+1);
4714 mpz_clear (last_value);
4715 return true;
4716 }
4717 }
4718 mpz_clear (last_value);
4719
4720 #undef AR_START
4721 #undef AR_END
4722 }
4723 break;
4724
4725 default:
4726 gfc_internal_error ("check_dimension(): Bad array reference");
4727 }
4728
4729 return true;
4730 }
4731
4732
4733 /* Compare an array reference with an array specification. */
4734
4735 static bool
4736 compare_spec_to_ref (gfc_array_ref *ar)
4737 {
4738 gfc_array_spec *as;
4739 int i;
4740
4741 as = ar->as;
4742 i = as->rank - 1;
4743 /* TODO: Full array sections are only allowed as actual parameters. */
4744 if (as->type == AS_ASSUMED_SIZE
4745 && (/*ar->type == AR_FULL
4746 ||*/ (ar->type == AR_SECTION
4747 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4748 {
4749 gfc_error ("Rightmost upper bound of assumed size array section "
4750 "not specified at %L", &ar->where);
4751 return false;
4752 }
4753
4754 if (ar->type == AR_FULL)
4755 return true;
4756
4757 if (as->rank != ar->dimen)
4758 {
4759 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4760 &ar->where, ar->dimen, as->rank);
4761 return false;
4762 }
4763
4764 /* ar->codimen == 0 is a local array. */
4765 if (as->corank != ar->codimen && ar->codimen != 0)
4766 {
4767 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4768 &ar->where, ar->codimen, as->corank);
4769 return false;
4770 }
4771
4772 for (i = 0; i < as->rank; i++)
4773 if (!check_dimension (i, ar, as))
4774 return false;
4775
4776 /* Local access has no coarray spec. */
4777 if (ar->codimen != 0)
4778 for (i = as->rank; i < as->rank + as->corank; i++)
4779 {
4780 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4781 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4782 {
4783 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4784 i + 1 - as->rank, &ar->where);
4785 return false;
4786 }
4787 if (!check_dimension (i, ar, as))
4788 return false;
4789 }
4790
4791 return true;
4792 }
4793
4794
4795 /* Resolve one part of an array index. */
4796
4797 static bool
4798 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4799 int force_index_integer_kind)
4800 {
4801 gfc_typespec ts;
4802
4803 if (index == NULL)
4804 return true;
4805
4806 if (!gfc_resolve_expr (index))
4807 return false;
4808
4809 if (check_scalar && index->rank != 0)
4810 {
4811 gfc_error ("Array index at %L must be scalar", &index->where);
4812 return false;
4813 }
4814
4815 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4816 {
4817 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4818 &index->where, gfc_basic_typename (index->ts.type));
4819 return false;
4820 }
4821
4822 if (index->ts.type == BT_REAL)
4823 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4824 &index->where))
4825 return false;
4826
4827 if ((index->ts.kind != gfc_index_integer_kind
4828 && force_index_integer_kind)
4829 || index->ts.type != BT_INTEGER)
4830 {
4831 gfc_clear_ts (&ts);
4832 ts.type = BT_INTEGER;
4833 ts.kind = gfc_index_integer_kind;
4834
4835 gfc_convert_type_warn (index, &ts, 2, 0);
4836 }
4837
4838 return true;
4839 }
4840
4841 /* Resolve one part of an array index. */
4842
4843 bool
4844 gfc_resolve_index (gfc_expr *index, int check_scalar)
4845 {
4846 return gfc_resolve_index_1 (index, check_scalar, 1);
4847 }
4848
4849 /* Resolve a dim argument to an intrinsic function. */
4850
4851 bool
4852 gfc_resolve_dim_arg (gfc_expr *dim)
4853 {
4854 if (dim == NULL)
4855 return true;
4856
4857 if (!gfc_resolve_expr (dim))
4858 return false;
4859
4860 if (dim->rank != 0)
4861 {
4862 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4863 return false;
4864
4865 }
4866
4867 if (dim->ts.type != BT_INTEGER)
4868 {
4869 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4870 return false;
4871 }
4872
4873 if (dim->ts.kind != gfc_index_integer_kind)
4874 {
4875 gfc_typespec ts;
4876
4877 gfc_clear_ts (&ts);
4878 ts.type = BT_INTEGER;
4879 ts.kind = gfc_index_integer_kind;
4880
4881 gfc_convert_type_warn (dim, &ts, 2, 0);
4882 }
4883
4884 return true;
4885 }
4886
4887 /* Given an expression that contains array references, update those array
4888 references to point to the right array specifications. While this is
4889 filled in during matching, this information is difficult to save and load
4890 in a module, so we take care of it here.
4891
4892 The idea here is that the original array reference comes from the
4893 base symbol. We traverse the list of reference structures, setting
4894 the stored reference to references. Component references can
4895 provide an additional array specification. */
4896 static void
4897 resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
4898
4899 static void
4900 find_array_spec (gfc_expr *e)
4901 {
4902 gfc_array_spec *as;
4903 gfc_component *c;
4904 gfc_ref *ref;
4905 bool class_as = false;
4906
4907 if (e->symtree->n.sym->assoc)
4908 {
4909 if (e->symtree->n.sym->assoc->target)
4910 gfc_resolve_expr (e->symtree->n.sym->assoc->target);
4911 resolve_assoc_var (e->symtree->n.sym, false);
4912 }
4913
4914 if (e->symtree->n.sym->ts.type == BT_CLASS)
4915 {
4916 as = CLASS_DATA (e->symtree->n.sym)->as;
4917 class_as = true;
4918 }
4919 else
4920 as = e->symtree->n.sym->as;
4921
4922 for (ref = e->ref; ref; ref = ref->next)
4923 switch (ref->type)
4924 {
4925 case REF_ARRAY:
4926 if (as == NULL)
4927 gfc_internal_error ("find_array_spec(): Missing spec");
4928
4929 ref->u.ar.as = as;
4930 as = NULL;
4931 break;
4932
4933 case REF_COMPONENT:
4934 c = ref->u.c.component;
4935 if (c->attr.dimension)
4936 {
4937 if (as != NULL && !(class_as && as == c->as))
4938 gfc_internal_error ("find_array_spec(): unused as(1)");
4939 as = c->as;
4940 }
4941
4942 break;
4943
4944 case REF_SUBSTRING:
4945 case REF_INQUIRY:
4946 break;
4947 }
4948
4949 if (as != NULL)
4950 gfc_internal_error ("find_array_spec(): unused as(2)");
4951 }
4952
4953
4954 /* Resolve an array reference. */
4955
4956 static bool
4957 resolve_array_ref (gfc_array_ref *ar)
4958 {
4959 int i, check_scalar;
4960 gfc_expr *e;
4961
4962 for (i = 0; i < ar->dimen + ar->codimen; i++)
4963 {
4964 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4965
4966 /* Do not force gfc_index_integer_kind for the start. We can
4967 do fine with any integer kind. This avoids temporary arrays
4968 created for indexing with a vector. */
4969 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4970 return false;
4971 if (!gfc_resolve_index (ar->end[i], check_scalar))
4972 return false;
4973 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4974 return false;
4975
4976 e = ar->start[i];
4977
4978 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4979 switch (e->rank)
4980 {
4981 case 0:
4982 ar->dimen_type[i] = DIMEN_ELEMENT;
4983 break;
4984
4985 case 1:
4986 ar->dimen_type[i] = DIMEN_VECTOR;
4987 if (e->expr_type == EXPR_VARIABLE
4988 && e->symtree->n.sym->ts.type == BT_DERIVED)
4989 ar->start[i] = gfc_get_parentheses (e);
4990 break;
4991
4992 default:
4993 gfc_error ("Array index at %L is an array of rank %d",
4994 &ar->c_where[i], e->rank);
4995 return false;
4996 }
4997
4998 /* Fill in the upper bound, which may be lower than the
4999 specified one for something like a(2:10:5), which is
5000 identical to a(2:7:5). Only relevant for strides not equal
5001 to one. Don't try a division by zero. */
5002 if (ar->dimen_type[i] == DIMEN_RANGE
5003 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5004 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5005 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5006 {
5007 mpz_t size, end;
5008
5009 if (gfc_ref_dimen_size (ar, i, &size, &end))
5010 {
5011 if (ar->end[i] == NULL)
5012 {
5013 ar->end[i] =
5014 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5015 &ar->where);
5016 mpz_set (ar->end[i]->value.integer, end);
5017 }
5018 else if (ar->end[i]->ts.type == BT_INTEGER
5019 && ar->end[i]->expr_type == EXPR_CONSTANT)
5020 {
5021 mpz_set (ar->end[i]->value.integer, end);
5022 }
5023 else
5024 gcc_unreachable ();
5025
5026 mpz_clear (size);
5027 mpz_clear (end);
5028 }
5029 }
5030 }
5031
5032 if (ar->type == AR_FULL)
5033 {
5034 if (ar->as->rank == 0)
5035 ar->type = AR_ELEMENT;
5036
5037 /* Make sure array is the same as array(:,:), this way
5038 we don't need to special case all the time. */
5039 ar->dimen = ar->as->rank;
5040 for (i = 0; i < ar->dimen; i++)
5041 {
5042 ar->dimen_type[i] = DIMEN_RANGE;
5043
5044 gcc_assert (ar->start[i] == NULL);
5045 gcc_assert (ar->end[i] == NULL);
5046 gcc_assert (ar->stride[i] == NULL);
5047 }
5048 }
5049
5050 /* If the reference type is unknown, figure out what kind it is. */
5051
5052 if (ar->type == AR_UNKNOWN)
5053 {
5054 ar->type = AR_ELEMENT;
5055 for (i = 0; i < ar->dimen; i++)
5056 if (ar->dimen_type[i] == DIMEN_RANGE
5057 || ar->dimen_type[i] == DIMEN_VECTOR)
5058 {
5059 ar->type = AR_SECTION;
5060 break;
5061 }
5062 }
5063
5064 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5065 return false;
5066
5067 if (ar->as->corank && ar->codimen == 0)
5068 {
5069 int n;
5070 ar->codimen = ar->as->corank;
5071 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5072 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5073 }
5074
5075 return true;
5076 }
5077
5078
5079 bool
5080 gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5081 {
5082 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5083
5084 if (ref->u.ss.start != NULL)
5085 {
5086 if (!gfc_resolve_expr (ref->u.ss.start))
5087 return false;
5088
5089 if (ref->u.ss.start->ts.type != BT_INTEGER)
5090 {
5091 gfc_error ("Substring start index at %L must be of type INTEGER",
5092 &ref->u.ss.start->where);
5093 return false;
5094 }
5095
5096 if (ref->u.ss.start->rank != 0)
5097 {
5098 gfc_error ("Substring start index at %L must be scalar",
5099 &ref->u.ss.start->where);
5100 return false;
5101 }
5102
5103 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5104 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5105 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5106 {
5107 gfc_error ("Substring start index at %L is less than one",
5108 &ref->u.ss.start->where);
5109 return false;
5110 }
5111 }
5112
5113 if (ref->u.ss.end != NULL)
5114 {
5115 if (!gfc_resolve_expr (ref->u.ss.end))
5116 return false;
5117
5118 if (ref->u.ss.end->ts.type != BT_INTEGER)
5119 {
5120 gfc_error ("Substring end index at %L must be of type INTEGER",
5121 &ref->u.ss.end->where);
5122 return false;
5123 }
5124
5125 if (ref->u.ss.end->rank != 0)
5126 {
5127 gfc_error ("Substring end index at %L must be scalar",
5128 &ref->u.ss.end->where);
5129 return false;
5130 }
5131
5132 if (ref->u.ss.length != NULL
5133 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5134 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5135 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5136 {
5137 gfc_error ("Substring end index at %L exceeds the string length",
5138 &ref->u.ss.start->where);
5139 return false;
5140 }
5141
5142 if (compare_bound_mpz_t (ref->u.ss.end,
5143 gfc_integer_kinds[k].huge) == CMP_GT
5144 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5145 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5146 {
5147 gfc_error ("Substring end index at %L is too large",
5148 &ref->u.ss.end->where);
5149 return false;
5150 }
5151 /* If the substring has the same length as the original
5152 variable, the reference itself can be deleted. */
5153
5154 if (ref->u.ss.length != NULL
5155 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5156 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5157 *equal_length = true;
5158 }
5159
5160 return true;
5161 }
5162
5163
5164 /* This function supplies missing substring charlens. */
5165
5166 void
5167 gfc_resolve_substring_charlen (gfc_expr *e)
5168 {
5169 gfc_ref *char_ref;
5170 gfc_expr *start, *end;
5171 gfc_typespec *ts = NULL;
5172 mpz_t diff;
5173
5174 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5175 {
5176 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5177 break;
5178 if (char_ref->type == REF_COMPONENT)
5179 ts = &char_ref->u.c.component->ts;
5180 }
5181
5182 if (!char_ref || char_ref->type == REF_INQUIRY)
5183 return;
5184
5185 gcc_assert (char_ref->next == NULL);
5186
5187 if (e->ts.u.cl)
5188 {
5189 if (e->ts.u.cl->length)
5190 gfc_free_expr (e->ts.u.cl->length);
5191 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5192 return;
5193 }
5194
5195 if (!e->ts.u.cl)
5196 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5197
5198 if (char_ref->u.ss.start)
5199 start = gfc_copy_expr (char_ref->u.ss.start);
5200 else
5201 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5202
5203 if (char_ref->u.ss.end)
5204 end = gfc_copy_expr (char_ref->u.ss.end);
5205 else if (e->expr_type == EXPR_VARIABLE)
5206 {
5207 if (!ts)
5208 ts = &e->symtree->n.sym->ts;
5209 end = gfc_copy_expr (ts->u.cl->length);
5210 }
5211 else
5212 end = NULL;
5213
5214 if (!start || !end)
5215 {
5216 gfc_free_expr (start);
5217 gfc_free_expr (end);
5218 return;
5219 }
5220
5221 /* Length = (end - start + 1).
5222 Check first whether it has a constant length. */
5223 if (gfc_dep_difference (end, start, &diff))
5224 {
5225 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5226 &e->where);
5227
5228 mpz_add_ui (len->value.integer, diff, 1);
5229 mpz_clear (diff);
5230 e->ts.u.cl->length = len;
5231 /* The check for length < 0 is handled below */
5232 }
5233 else
5234 {
5235 e->ts.u.cl->length = gfc_subtract (end, start);
5236 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5237 gfc_get_int_expr (gfc_charlen_int_kind,
5238 NULL, 1));
5239 }
5240
5241 /* F2008, 6.4.1: Both the starting point and the ending point shall
5242 be within the range 1, 2, ..., n unless the starting point exceeds
5243 the ending point, in which case the substring has length zero. */
5244
5245 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5246 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5247
5248 e->ts.u.cl->length->ts.type = BT_INTEGER;
5249 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5250
5251 /* Make sure that the length is simplified. */
5252 gfc_simplify_expr (e->ts.u.cl->length, 1);
5253 gfc_resolve_expr (e->ts.u.cl->length);
5254 }
5255
5256
5257 /* Resolve subtype references. */
5258
5259 bool
5260 gfc_resolve_ref (gfc_expr *expr)
5261 {
5262 int current_part_dimension, n_components, seen_part_dimension, dim;
5263 gfc_ref *ref, **prev, *array_ref;
5264 bool equal_length;
5265
5266 for (ref = expr->ref; ref; ref = ref->next)
5267 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5268 {
5269 find_array_spec (expr);
5270 break;
5271 }
5272
5273 for (prev = &expr->ref; *prev != NULL;
5274 prev = *prev == NULL ? prev : &(*prev)->next)
5275 switch ((*prev)->type)
5276 {
5277 case REF_ARRAY:
5278 if (!resolve_array_ref (&(*prev)->u.ar))
5279 return false;
5280 break;
5281
5282 case REF_COMPONENT:
5283 case REF_INQUIRY:
5284 break;
5285
5286 case REF_SUBSTRING:
5287 equal_length = false;
5288 if (!gfc_resolve_substring (*prev, &equal_length))
5289 return false;
5290
5291 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5292 {
5293 /* Remove the reference and move the charlen, if any. */
5294 ref = *prev;
5295 *prev = ref->next;
5296 ref->next = NULL;
5297 expr->ts.u.cl = ref->u.ss.length;
5298 ref->u.ss.length = NULL;
5299 gfc_free_ref_list (ref);
5300 }
5301 break;
5302 }
5303
5304 /* Check constraints on part references. */
5305
5306 current_part_dimension = 0;
5307 seen_part_dimension = 0;
5308 n_components = 0;
5309 array_ref = NULL;
5310
5311 for (ref = expr->ref; ref; ref = ref->next)
5312 {
5313 switch (ref->type)
5314 {
5315 case REF_ARRAY:
5316 array_ref = ref;
5317 switch (ref->u.ar.type)
5318 {
5319 case AR_FULL:
5320 /* Coarray scalar. */
5321 if (ref->u.ar.as->rank == 0)
5322 {
5323 current_part_dimension = 0;
5324 break;
5325 }
5326 /* Fall through. */
5327 case AR_SECTION:
5328 current_part_dimension = 1;
5329 break;
5330
5331 case AR_ELEMENT:
5332 array_ref = NULL;
5333 current_part_dimension = 0;
5334 break;
5335
5336 case AR_UNKNOWN:
5337 gfc_internal_error ("resolve_ref(): Bad array reference");
5338 }
5339
5340 break;
5341
5342 case REF_COMPONENT:
5343 if (current_part_dimension || seen_part_dimension)
5344 {
5345 /* F03:C614. */
5346 if (ref->u.c.component->attr.pointer
5347 || ref->u.c.component->attr.proc_pointer
5348 || (ref->u.c.component->ts.type == BT_CLASS
5349 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5350 {
5351 gfc_error ("Component to the right of a part reference "
5352 "with nonzero rank must not have the POINTER "
5353 "attribute at %L", &expr->where);
5354 return false;
5355 }
5356 else if (ref->u.c.component->attr.allocatable
5357 || (ref->u.c.component->ts.type == BT_CLASS
5358 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5359
5360 {
5361 gfc_error ("Component to the right of a part reference "
5362 "with nonzero rank must not have the ALLOCATABLE "
5363 "attribute at %L", &expr->where);
5364 return false;
5365 }
5366 }
5367
5368 n_components++;
5369 break;
5370
5371 case REF_SUBSTRING:
5372 break;
5373
5374 case REF_INQUIRY:
5375 /* Implement requirement in note 9.7 of F2018 that the result of the
5376 LEN inquiry be a scalar. */
5377 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5378 {
5379 array_ref->u.ar.type = AR_ELEMENT;
5380 expr->rank = 0;
5381 /* INQUIRY_LEN is not evaluated from the rest of the expr
5382 but directly from the string length. This means that setting
5383 the array indices to one does not matter but might trigger
5384 a runtime bounds error. Suppress the check. */
5385 expr->no_bounds_check = 1;
5386 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5387 {
5388 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5389 if (array_ref->u.ar.start[dim])
5390 gfc_free_expr (array_ref->u.ar.start[dim]);
5391 array_ref->u.ar.start[dim]
5392 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5393 if (array_ref->u.ar.end[dim])
5394 gfc_free_expr (array_ref->u.ar.end[dim]);
5395 if (array_ref->u.ar.stride[dim])
5396 gfc_free_expr (array_ref->u.ar.stride[dim]);
5397 }
5398 }
5399 break;
5400 }
5401
5402 if (((ref->type == REF_COMPONENT && n_components > 1)
5403 || ref->next == NULL)
5404 && current_part_dimension
5405 && seen_part_dimension)
5406 {
5407 gfc_error ("Two or more part references with nonzero rank must "
5408 "not be specified at %L", &expr->where);
5409 return false;
5410 }
5411
5412 if (ref->type == REF_COMPONENT)
5413 {
5414 if (current_part_dimension)
5415 seen_part_dimension = 1;
5416
5417 /* reset to make sure */
5418 current_part_dimension = 0;
5419 }
5420 }
5421
5422 return true;
5423 }
5424
5425
5426 /* Given an expression, determine its shape. This is easier than it sounds.
5427 Leaves the shape array NULL if it is not possible to determine the shape. */
5428
5429 static void
5430 expression_shape (gfc_expr *e)
5431 {
5432 mpz_t array[GFC_MAX_DIMENSIONS];
5433 int i;
5434
5435 if (e->rank <= 0 || e->shape != NULL)
5436 return;
5437
5438 for (i = 0; i < e->rank; i++)
5439 if (!gfc_array_dimen_size (e, i, &array[i]))
5440 goto fail;
5441
5442 e->shape = gfc_get_shape (e->rank);
5443
5444 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5445
5446 return;
5447
5448 fail:
5449 for (i--; i >= 0; i--)
5450 mpz_clear (array[i]);
5451 }
5452
5453
5454 /* Given a variable expression node, compute the rank of the expression by
5455 examining the base symbol and any reference structures it may have. */
5456
5457 void
5458 gfc_expression_rank (gfc_expr *e)
5459 {
5460 gfc_ref *ref;
5461 int i, rank;
5462
5463 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5464 could lead to serious confusion... */
5465 gcc_assert (e->expr_type != EXPR_COMPCALL);
5466
5467 if (e->ref == NULL)
5468 {
5469 if (e->expr_type == EXPR_ARRAY)
5470 goto done;
5471 /* Constructors can have a rank different from one via RESHAPE(). */
5472
5473 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5474 ? 0 : e->symtree->n.sym->as->rank);
5475 goto done;
5476 }
5477
5478 rank = 0;
5479
5480 for (ref = e->ref; ref; ref = ref->next)
5481 {
5482 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5483 && ref->u.c.component->attr.function && !ref->next)
5484 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5485
5486 if (ref->type != REF_ARRAY)
5487 continue;
5488
5489 if (ref->u.ar.type == AR_FULL)
5490 {
5491 rank = ref->u.ar.as->rank;
5492 break;
5493 }
5494
5495 if (ref->u.ar.type == AR_SECTION)
5496 {
5497 /* Figure out the rank of the section. */
5498 if (rank != 0)
5499 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5500
5501 for (i = 0; i < ref->u.ar.dimen; i++)
5502 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5503 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5504 rank++;
5505
5506 break;
5507 }
5508 }
5509
5510 e->rank = rank;
5511
5512 done:
5513 expression_shape (e);
5514 }
5515
5516
5517 static void
5518 add_caf_get_intrinsic (gfc_expr *e)
5519 {
5520 gfc_expr *wrapper, *tmp_expr;
5521 gfc_ref *ref;
5522 int n;
5523
5524 for (ref = e->ref; ref; ref = ref->next)
5525 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5526 break;
5527 if (ref == NULL)
5528 return;
5529
5530 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5531 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5532 return;
5533
5534 tmp_expr = XCNEW (gfc_expr);
5535 *tmp_expr = *e;
5536 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5537 "caf_get", tmp_expr->where, 1, tmp_expr);
5538 wrapper->ts = e->ts;
5539 wrapper->rank = e->rank;
5540 if (e->rank)
5541 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5542 *e = *wrapper;
5543 free (wrapper);
5544 }
5545
5546
5547 static void
5548 remove_caf_get_intrinsic (gfc_expr *e)
5549 {
5550 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5551 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5552 gfc_expr *e2 = e->value.function.actual->expr;
5553 e->value.function.actual->expr = NULL;
5554 gfc_free_actual_arglist (e->value.function.actual);
5555 gfc_free_shape (&e->shape, e->rank);
5556 *e = *e2;
5557 free (e2);
5558 }
5559
5560
5561 /* Resolve a variable expression. */
5562
5563 static bool
5564 resolve_variable (gfc_expr *e)
5565 {
5566 gfc_symbol *sym;
5567 bool t;
5568
5569 t = true;
5570
5571 if (e->symtree == NULL)
5572 return false;
5573 sym = e->symtree->n.sym;
5574
5575 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5576 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5577 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5578 {
5579 if (!actual_arg || inquiry_argument)
5580 {
5581 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5582 "be used as actual argument", sym->name, &e->where);
5583 return false;
5584 }
5585 }
5586 /* TS 29113, 407b. */
5587 else if (e->ts.type == BT_ASSUMED)
5588 {
5589 if (!actual_arg)
5590 {
5591 gfc_error ("Assumed-type variable %s at %L may only be used "
5592 "as actual argument", sym->name, &e->where);
5593 return false;
5594 }
5595 else if (inquiry_argument && !first_actual_arg)
5596 {
5597 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5598 for all inquiry functions in resolve_function; the reason is
5599 that the function-name resolution happens too late in that
5600 function. */
5601 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5602 "an inquiry function shall be the first argument",
5603 sym->name, &e->where);
5604 return false;
5605 }
5606 }
5607 /* TS 29113, C535b. */
5608 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5609 && sym->ts.u.derived && CLASS_DATA (sym)
5610 && CLASS_DATA (sym)->as
5611 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5612 || (sym->ts.type != BT_CLASS && sym->as
5613 && sym->as->type == AS_ASSUMED_RANK))
5614 && !sym->attr.select_rank_temporary)
5615 {
5616 if (!actual_arg
5617 && !(cs_base && cs_base->current
5618 && cs_base->current->op == EXEC_SELECT_RANK))
5619 {
5620 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5621 "actual argument", sym->name, &e->where);
5622 return false;
5623 }
5624 else if (inquiry_argument && !first_actual_arg)
5625 {
5626 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5627 for all inquiry functions in resolve_function; the reason is
5628 that the function-name resolution happens too late in that
5629 function. */
5630 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5631 "to an inquiry function shall be the first argument",
5632 sym->name, &e->where);
5633 return false;
5634 }
5635 }
5636
5637 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5638 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5639 && e->ref->next == NULL))
5640 {
5641 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5642 "a subobject reference", sym->name, &e->ref->u.ar.where);
5643 return false;
5644 }
5645 /* TS 29113, 407b. */
5646 else if (e->ts.type == BT_ASSUMED && e->ref
5647 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5648 && e->ref->next == NULL))
5649 {
5650 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5651 "reference", sym->name, &e->ref->u.ar.where);
5652 return false;
5653 }
5654
5655 /* TS 29113, C535b. */
5656 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5657 && sym->ts.u.derived && CLASS_DATA (sym)
5658 && CLASS_DATA (sym)->as
5659 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5660 || (sym->ts.type != BT_CLASS && sym->as
5661 && sym->as->type == AS_ASSUMED_RANK))
5662 && e->ref
5663 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5664 && e->ref->next == NULL))
5665 {
5666 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5667 "reference", sym->name, &e->ref->u.ar.where);
5668 return false;
5669 }
5670
5671 /* For variables that are used in an associate (target => object) where
5672 the object's basetype is array valued while the target is scalar,
5673 the ts' type of the component refs is still array valued, which
5674 can't be translated that way. */
5675 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5676 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5677 && CLASS_DATA (sym->assoc->target)->as)
5678 {
5679 gfc_ref *ref = e->ref;
5680 while (ref)
5681 {
5682 switch (ref->type)
5683 {
5684 case REF_COMPONENT:
5685 ref->u.c.sym = sym->ts.u.derived;
5686 /* Stop the loop. */
5687 ref = NULL;
5688 break;
5689 default:
5690 ref = ref->next;
5691 break;
5692 }
5693 }
5694 }
5695
5696 /* If this is an associate-name, it may be parsed with an array reference
5697 in error even though the target is scalar. Fail directly in this case.
5698 TODO Understand why class scalar expressions must be excluded. */
5699 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5700 {
5701 if (sym->ts.type == BT_CLASS)
5702 gfc_fix_class_refs (e);
5703 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5704 return false;
5705 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5706 {
5707 /* This can happen because the parser did not detect that the
5708 associate name is an array and the expression had no array
5709 part_ref. */
5710 gfc_ref *ref = gfc_get_ref ();
5711 ref->type = REF_ARRAY;
5712 ref->u.ar = *gfc_get_array_ref();
5713 ref->u.ar.type = AR_FULL;
5714 if (sym->as)
5715 {
5716 ref->u.ar.as = sym->as;
5717 ref->u.ar.dimen = sym->as->rank;
5718 }
5719 ref->next = e->ref;
5720 e->ref = ref;
5721
5722 }
5723 }
5724
5725 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5726 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5727
5728 /* On the other hand, the parser may not have known this is an array;
5729 in this case, we have to add a FULL reference. */
5730 if (sym->assoc && sym->attr.dimension && !e->ref)
5731 {
5732 e->ref = gfc_get_ref ();
5733 e->ref->type = REF_ARRAY;
5734 e->ref->u.ar.type = AR_FULL;
5735 e->ref->u.ar.dimen = 0;
5736 }
5737
5738 /* Like above, but for class types, where the checking whether an array
5739 ref is present is more complicated. Furthermore make sure not to add
5740 the full array ref to _vptr or _len refs. */
5741 if (sym->assoc && sym->ts.type == BT_CLASS
5742 && CLASS_DATA (sym)->attr.dimension
5743 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5744 {
5745 gfc_ref *ref, *newref;
5746
5747 newref = gfc_get_ref ();
5748 newref->type = REF_ARRAY;
5749 newref->u.ar.type = AR_FULL;
5750 newref->u.ar.dimen = 0;
5751 /* Because this is an associate var and the first ref either is a ref to
5752 the _data component or not, no traversal of the ref chain is
5753 needed. The array ref needs to be inserted after the _data ref,
5754 or when that is not present, which may happend for polymorphic
5755 types, then at the first position. */
5756 ref = e->ref;
5757 if (!ref)
5758 e->ref = newref;
5759 else if (ref->type == REF_COMPONENT
5760 && strcmp ("_data", ref->u.c.component->name) == 0)
5761 {
5762 if (!ref->next || ref->next->type != REF_ARRAY)
5763 {
5764 newref->next = ref->next;
5765 ref->next = newref;
5766 }
5767 else
5768 /* Array ref present already. */
5769 gfc_free_ref_list (newref);
5770 }
5771 else if (ref->type == REF_ARRAY)
5772 /* Array ref present already. */
5773 gfc_free_ref_list (newref);
5774 else
5775 {
5776 newref->next = ref;
5777 e->ref = newref;
5778 }
5779 }
5780
5781 if (e->ref && !gfc_resolve_ref (e))
5782 return false;
5783
5784 if (sym->attr.flavor == FL_PROCEDURE
5785 && (!sym->attr.function
5786 || (sym->attr.function && sym->result
5787 && sym->result->attr.proc_pointer
5788 && !sym->result->attr.function)))
5789 {
5790 e->ts.type = BT_PROCEDURE;
5791 goto resolve_procedure;
5792 }
5793
5794 if (sym->ts.type != BT_UNKNOWN)
5795 gfc_variable_attr (e, &e->ts);
5796 else if (sym->attr.flavor == FL_PROCEDURE
5797 && sym->attr.function && sym->result
5798 && sym->result->ts.type != BT_UNKNOWN
5799 && sym->result->attr.proc_pointer)
5800 e->ts = sym->result->ts;
5801 else
5802 {
5803 /* Must be a simple variable reference. */
5804 if (!gfc_set_default_type (sym, 1, sym->ns))
5805 return false;
5806 e->ts = sym->ts;
5807 }
5808
5809 if (check_assumed_size_reference (sym, e))
5810 return false;
5811
5812 /* Deal with forward references to entries during gfc_resolve_code, to
5813 satisfy, at least partially, 12.5.2.5. */
5814 if (gfc_current_ns->entries
5815 && current_entry_id == sym->entry_id
5816 && cs_base
5817 && cs_base->current
5818 && cs_base->current->op != EXEC_ENTRY)
5819 {
5820 gfc_entry_list *entry;
5821 gfc_formal_arglist *formal;
5822 int n;
5823 bool seen, saved_specification_expr;
5824
5825 /* If the symbol is a dummy... */
5826 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5827 {
5828 entry = gfc_current_ns->entries;
5829 seen = false;
5830
5831 /* ...test if the symbol is a parameter of previous entries. */
5832 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5833 for (formal = entry->sym->formal; formal; formal = formal->next)
5834 {
5835 if (formal->sym && sym->name == formal->sym->name)
5836 {
5837 seen = true;
5838 break;
5839 }
5840 }
5841
5842 /* If it has not been seen as a dummy, this is an error. */
5843 if (!seen)
5844 {
5845 if (specification_expr)
5846 gfc_error ("Variable %qs, used in a specification expression"
5847 ", is referenced at %L before the ENTRY statement "
5848 "in which it is a parameter",
5849 sym->name, &cs_base->current->loc);
5850 else
5851 gfc_error ("Variable %qs is used at %L before the ENTRY "
5852 "statement in which it is a parameter",
5853 sym->name, &cs_base->current->loc);
5854 t = false;
5855 }
5856 }
5857
5858 /* Now do the same check on the specification expressions. */
5859 saved_specification_expr = specification_expr;
5860 specification_expr = true;
5861 if (sym->ts.type == BT_CHARACTER
5862 && !gfc_resolve_expr (sym->ts.u.cl->length))
5863 t = false;
5864
5865 if (sym->as)
5866 for (n = 0; n < sym->as->rank; n++)
5867 {
5868 if (!gfc_resolve_expr (sym->as->lower[n]))
5869 t = false;
5870 if (!gfc_resolve_expr (sym->as->upper[n]))
5871 t = false;
5872 }
5873 specification_expr = saved_specification_expr;
5874
5875 if (t)
5876 /* Update the symbol's entry level. */
5877 sym->entry_id = current_entry_id + 1;
5878 }
5879
5880 /* If a symbol has been host_associated mark it. This is used latter,
5881 to identify if aliasing is possible via host association. */
5882 if (sym->attr.flavor == FL_VARIABLE
5883 && gfc_current_ns->parent
5884 && (gfc_current_ns->parent == sym->ns
5885 || (gfc_current_ns->parent->parent
5886 && gfc_current_ns->parent->parent == sym->ns)))
5887 sym->attr.host_assoc = 1;
5888
5889 if (gfc_current_ns->proc_name
5890 && sym->attr.dimension
5891 && (sym->ns != gfc_current_ns
5892 || sym->attr.use_assoc
5893 || sym->attr.in_common))
5894 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5895
5896 resolve_procedure:
5897 if (t && !resolve_procedure_expression (e))
5898 t = false;
5899
5900 /* F2008, C617 and C1229. */
5901 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5902 && gfc_is_coindexed (e))
5903 {
5904 gfc_ref *ref, *ref2 = NULL;
5905
5906 for (ref = e->ref; ref; ref = ref->next)
5907 {
5908 if (ref->type == REF_COMPONENT)
5909 ref2 = ref;
5910 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5911 break;
5912 }
5913
5914 for ( ; ref; ref = ref->next)
5915 if (ref->type == REF_COMPONENT)
5916 break;
5917
5918 /* Expression itself is not coindexed object. */
5919 if (ref && e->ts.type == BT_CLASS)
5920 {
5921 gfc_error ("Polymorphic subobject of coindexed object at %L",
5922 &e->where);
5923 t = false;
5924 }
5925
5926 /* Expression itself is coindexed object. */
5927 if (ref == NULL)
5928 {
5929 gfc_component *c;
5930 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5931 for ( ; c; c = c->next)
5932 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5933 {
5934 gfc_error ("Coindexed object with polymorphic allocatable "
5935 "subcomponent at %L", &e->where);
5936 t = false;
5937 break;
5938 }
5939 }
5940 }
5941
5942 if (t)
5943 gfc_expression_rank (e);
5944
5945 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5946 add_caf_get_intrinsic (e);
5947
5948 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
5949 gfc_warning (OPT_Wdeprecated_declarations,
5950 "Using variable %qs at %L is deprecated",
5951 sym->name, &e->where);
5952 /* Simplify cases where access to a parameter array results in a
5953 single constant. Suppress errors since those will have been
5954 issued before, as warnings. */
5955 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5956 {
5957 gfc_push_suppress_errors ();
5958 gfc_simplify_expr (e, 1);
5959 gfc_pop_suppress_errors ();
5960 }
5961
5962 return t;
5963 }
5964
5965
5966 /* Checks to see that the correct symbol has been host associated.
5967 The only situation where this arises is that in which a twice
5968 contained function is parsed after the host association is made.
5969 Therefore, on detecting this, change the symbol in the expression
5970 and convert the array reference into an actual arglist if the old
5971 symbol is a variable. */
5972 static bool
5973 check_host_association (gfc_expr *e)
5974 {
5975 gfc_symbol *sym, *old_sym;
5976 gfc_symtree *st;
5977 int n;
5978 gfc_ref *ref;
5979 gfc_actual_arglist *arg, *tail = NULL;
5980 bool retval = e->expr_type == EXPR_FUNCTION;
5981
5982 /* If the expression is the result of substitution in
5983 interface.c(gfc_extend_expr) because there is no way in
5984 which the host association can be wrong. */
5985 if (e->symtree == NULL
5986 || e->symtree->n.sym == NULL
5987 || e->user_operator)
5988 return retval;
5989
5990 old_sym = e->symtree->n.sym;
5991
5992 if (gfc_current_ns->parent
5993 && old_sym->ns != gfc_current_ns)
5994 {
5995 /* Use the 'USE' name so that renamed module symbols are
5996 correctly handled. */
5997 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5998
5999 if (sym && old_sym != sym
6000 && sym->ts.type == old_sym->ts.type
6001 && sym->attr.flavor == FL_PROCEDURE
6002 && sym->attr.contained)
6003 {
6004 /* Clear the shape, since it might not be valid. */
6005 gfc_free_shape (&e->shape, e->rank);
6006
6007 /* Give the expression the right symtree! */
6008 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6009 gcc_assert (st != NULL);
6010
6011 if (old_sym->attr.flavor == FL_PROCEDURE
6012 || e->expr_type == EXPR_FUNCTION)
6013 {
6014 /* Original was function so point to the new symbol, since
6015 the actual argument list is already attached to the
6016 expression. */
6017 e->value.function.esym = NULL;
6018 e->symtree = st;
6019 }
6020 else
6021 {
6022 /* Original was variable so convert array references into
6023 an actual arglist. This does not need any checking now
6024 since resolve_function will take care of it. */
6025 e->value.function.actual = NULL;
6026 e->expr_type = EXPR_FUNCTION;
6027 e->symtree = st;
6028
6029 /* Ambiguity will not arise if the array reference is not
6030 the last reference. */
6031 for (ref = e->ref; ref; ref = ref->next)
6032 if (ref->type == REF_ARRAY && ref->next == NULL)
6033 break;
6034
6035 if ((ref == NULL || ref->type != REF_ARRAY)
6036 && sym->attr.proc == PROC_INTERNAL)
6037 {
6038 gfc_error ("%qs at %L is host associated at %L into "
6039 "a contained procedure with an internal "
6040 "procedure of the same name", sym->name,
6041 &old_sym->declared_at, &e->where);
6042 return false;
6043 }
6044
6045 gcc_assert (ref->type == REF_ARRAY);
6046
6047 /* Grab the start expressions from the array ref and
6048 copy them into actual arguments. */
6049 for (n = 0; n < ref->u.ar.dimen; n++)
6050 {
6051 arg = gfc_get_actual_arglist ();
6052 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6053 if (e->value.function.actual == NULL)
6054 tail = e->value.function.actual = arg;
6055 else
6056 {
6057 tail->next = arg;
6058 tail = arg;
6059 }
6060 }
6061
6062 /* Dump the reference list and set the rank. */
6063 gfc_free_ref_list (e->ref);
6064 e->ref = NULL;
6065 e->rank = sym->as ? sym->as->rank : 0;
6066 }
6067
6068 gfc_resolve_expr (e);
6069 sym->refs++;
6070 }
6071 }
6072 /* This might have changed! */
6073 return e->expr_type == EXPR_FUNCTION;
6074 }
6075
6076
6077 static void
6078 gfc_resolve_character_operator (gfc_expr *e)
6079 {
6080 gfc_expr *op1 = e->value.op.op1;
6081 gfc_expr *op2 = e->value.op.op2;
6082 gfc_expr *e1 = NULL;
6083 gfc_expr *e2 = NULL;
6084
6085 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6086
6087 if (op1->ts.u.cl && op1->ts.u.cl->length)
6088 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6089 else if (op1->expr_type == EXPR_CONSTANT)
6090 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6091 op1->value.character.length);
6092
6093 if (op2->ts.u.cl && op2->ts.u.cl->length)
6094 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6095 else if (op2->expr_type == EXPR_CONSTANT)
6096 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6097 op2->value.character.length);
6098
6099 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6100
6101 if (!e1 || !e2)
6102 {
6103 gfc_free_expr (e1);
6104 gfc_free_expr (e2);
6105
6106 return;
6107 }
6108
6109 e->ts.u.cl->length = gfc_add (e1, e2);
6110 e->ts.u.cl->length->ts.type = BT_INTEGER;
6111 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6112 gfc_simplify_expr (e->ts.u.cl->length, 0);
6113 gfc_resolve_expr (e->ts.u.cl->length);
6114
6115 return;
6116 }
6117
6118
6119 /* Ensure that an character expression has a charlen and, if possible, a
6120 length expression. */
6121
6122 static void
6123 fixup_charlen (gfc_expr *e)
6124 {
6125 /* The cases fall through so that changes in expression type and the need
6126 for multiple fixes are picked up. In all circumstances, a charlen should
6127 be available for the middle end to hang a backend_decl on. */
6128 switch (e->expr_type)
6129 {
6130 case EXPR_OP:
6131 gfc_resolve_character_operator (e);
6132 /* FALLTHRU */
6133
6134 case EXPR_ARRAY:
6135 if (e->expr_type == EXPR_ARRAY)
6136 gfc_resolve_character_array_constructor (e);
6137 /* FALLTHRU */
6138
6139 case EXPR_SUBSTRING:
6140 if (!e->ts.u.cl && e->ref)
6141 gfc_resolve_substring_charlen (e);
6142 /* FALLTHRU */
6143
6144 default:
6145 if (!e->ts.u.cl)
6146 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6147
6148 break;
6149 }
6150 }
6151
6152
6153 /* Update an actual argument to include the passed-object for type-bound
6154 procedures at the right position. */
6155
6156 static gfc_actual_arglist*
6157 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6158 const char *name)
6159 {
6160 gcc_assert (argpos > 0);
6161
6162 if (argpos == 1)
6163 {
6164 gfc_actual_arglist* result;
6165
6166 result = gfc_get_actual_arglist ();
6167 result->expr = po;
6168 result->next = lst;
6169 if (name)
6170 result->name = name;
6171
6172 return result;
6173 }
6174
6175 if (lst)
6176 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6177 else
6178 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6179 return lst;
6180 }
6181
6182
6183 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6184
6185 static gfc_expr*
6186 extract_compcall_passed_object (gfc_expr* e)
6187 {
6188 gfc_expr* po;
6189
6190 if (e->expr_type == EXPR_UNKNOWN)
6191 {
6192 gfc_error ("Error in typebound call at %L",
6193 &e->where);
6194 return NULL;
6195 }
6196
6197 gcc_assert (e->expr_type == EXPR_COMPCALL);
6198
6199 if (e->value.compcall.base_object)
6200 po = gfc_copy_expr (e->value.compcall.base_object);
6201 else
6202 {
6203 po = gfc_get_expr ();
6204 po->expr_type = EXPR_VARIABLE;
6205 po->symtree = e->symtree;
6206 po->ref = gfc_copy_ref (e->ref);
6207 po->where = e->where;
6208 }
6209
6210 if (!gfc_resolve_expr (po))
6211 return NULL;
6212
6213 return po;
6214 }
6215
6216
6217 /* Update the arglist of an EXPR_COMPCALL expression to include the
6218 passed-object. */
6219
6220 static bool
6221 update_compcall_arglist (gfc_expr* e)
6222 {
6223 gfc_expr* po;
6224 gfc_typebound_proc* tbp;
6225
6226 tbp = e->value.compcall.tbp;
6227
6228 if (tbp->error)
6229 return false;
6230
6231 po = extract_compcall_passed_object (e);
6232 if (!po)
6233 return false;
6234
6235 if (tbp->nopass || e->value.compcall.ignore_pass)
6236 {
6237 gfc_free_expr (po);
6238 return true;
6239 }
6240
6241 if (tbp->pass_arg_num <= 0)
6242 return false;
6243
6244 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6245 tbp->pass_arg_num,
6246 tbp->pass_arg);
6247
6248 return true;
6249 }
6250
6251
6252 /* Extract the passed object from a PPC call (a copy of it). */
6253
6254 static gfc_expr*
6255 extract_ppc_passed_object (gfc_expr *e)
6256 {
6257 gfc_expr *po;
6258 gfc_ref **ref;
6259
6260 po = gfc_get_expr ();
6261 po->expr_type = EXPR_VARIABLE;
6262 po->symtree = e->symtree;
6263 po->ref = gfc_copy_ref (e->ref);
6264 po->where = e->where;
6265
6266 /* Remove PPC reference. */
6267 ref = &po->ref;
6268 while ((*ref)->next)
6269 ref = &(*ref)->next;
6270 gfc_free_ref_list (*ref);
6271 *ref = NULL;
6272
6273 if (!gfc_resolve_expr (po))
6274 return NULL;
6275
6276 return po;
6277 }
6278
6279
6280 /* Update the actual arglist of a procedure pointer component to include the
6281 passed-object. */
6282
6283 static bool
6284 update_ppc_arglist (gfc_expr* e)
6285 {
6286 gfc_expr* po;
6287 gfc_component *ppc;
6288 gfc_typebound_proc* tb;
6289
6290 ppc = gfc_get_proc_ptr_comp (e);
6291 if (!ppc)
6292 return false;
6293
6294 tb = ppc->tb;
6295
6296 if (tb->error)
6297 return false;
6298 else if (tb->nopass)
6299 return true;
6300
6301 po = extract_ppc_passed_object (e);
6302 if (!po)
6303 return false;
6304
6305 /* F08:R739. */
6306 if (po->rank != 0)
6307 {
6308 gfc_error ("Passed-object at %L must be scalar", &e->where);
6309 return false;
6310 }
6311
6312 /* F08:C611. */
6313 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6314 {
6315 gfc_error ("Base object for procedure-pointer component call at %L is of"
6316 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6317 return false;
6318 }
6319
6320 gcc_assert (tb->pass_arg_num > 0);
6321 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6322 tb->pass_arg_num,
6323 tb->pass_arg);
6324
6325 return true;
6326 }
6327
6328
6329 /* Check that the object a TBP is called on is valid, i.e. it must not be
6330 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6331
6332 static bool
6333 check_typebound_baseobject (gfc_expr* e)
6334 {
6335 gfc_expr* base;
6336 bool return_value = false;
6337
6338 base = extract_compcall_passed_object (e);
6339 if (!base)
6340 return false;
6341
6342 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6343 {
6344 gfc_error ("Error in typebound call at %L", &e->where);
6345 goto cleanup;
6346 }
6347
6348 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6349 return false;
6350
6351 /* F08:C611. */
6352 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6353 {
6354 gfc_error ("Base object for type-bound procedure call at %L is of"
6355 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6356 goto cleanup;
6357 }
6358
6359 /* F08:C1230. If the procedure called is NOPASS,
6360 the base object must be scalar. */
6361 if (e->value.compcall.tbp->nopass && base->rank != 0)
6362 {
6363 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6364 " be scalar", &e->where);
6365 goto cleanup;
6366 }
6367
6368 return_value = true;
6369
6370 cleanup:
6371 gfc_free_expr (base);
6372 return return_value;
6373 }
6374
6375
6376 /* Resolve a call to a type-bound procedure, either function or subroutine,
6377 statically from the data in an EXPR_COMPCALL expression. The adapted
6378 arglist and the target-procedure symtree are returned. */
6379
6380 static bool
6381 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6382 gfc_actual_arglist** actual)
6383 {
6384 gcc_assert (e->expr_type == EXPR_COMPCALL);
6385 gcc_assert (!e->value.compcall.tbp->is_generic);
6386
6387 /* Update the actual arglist for PASS. */
6388 if (!update_compcall_arglist (e))
6389 return false;
6390
6391 *actual = e->value.compcall.actual;
6392 *target = e->value.compcall.tbp->u.specific;
6393
6394 gfc_free_ref_list (e->ref);
6395 e->ref = NULL;
6396 e->value.compcall.actual = NULL;
6397
6398 /* If we find a deferred typebound procedure, check for derived types
6399 that an overriding typebound procedure has not been missed. */
6400 if (e->value.compcall.name
6401 && !e->value.compcall.tbp->non_overridable
6402 && e->value.compcall.base_object
6403 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6404 {
6405 gfc_symtree *st;
6406 gfc_symbol *derived;
6407
6408 /* Use the derived type of the base_object. */
6409 derived = e->value.compcall.base_object->ts.u.derived;
6410 st = NULL;
6411
6412 /* If necessary, go through the inheritance chain. */
6413 while (!st && derived)
6414 {
6415 /* Look for the typebound procedure 'name'. */
6416 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6417 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6418 e->value.compcall.name);
6419 if (!st)
6420 derived = gfc_get_derived_super_type (derived);
6421 }
6422
6423 /* Now find the specific name in the derived type namespace. */
6424 if (st && st->n.tb && st->n.tb->u.specific)
6425 gfc_find_sym_tree (st->n.tb->u.specific->name,
6426 derived->ns, 1, &st);
6427 if (st)
6428 *target = st;
6429 }
6430 return true;
6431 }
6432
6433
6434 /* Get the ultimate declared type from an expression. In addition,
6435 return the last class/derived type reference and the copy of the
6436 reference list. If check_types is set true, derived types are
6437 identified as well as class references. */
6438 static gfc_symbol*
6439 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6440 gfc_expr *e, bool check_types)
6441 {
6442 gfc_symbol *declared;
6443 gfc_ref *ref;
6444
6445 declared = NULL;
6446 if (class_ref)
6447 *class_ref = NULL;
6448 if (new_ref)
6449 *new_ref = gfc_copy_ref (e->ref);
6450
6451 for (ref = e->ref; ref; ref = ref->next)
6452 {
6453 if (ref->type != REF_COMPONENT)
6454 continue;
6455
6456 if ((ref->u.c.component->ts.type == BT_CLASS
6457 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6458 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6459 {
6460 declared = ref->u.c.component->ts.u.derived;
6461 if (class_ref)
6462 *class_ref = ref;
6463 }
6464 }
6465
6466 if (declared == NULL)
6467 declared = e->symtree->n.sym->ts.u.derived;
6468
6469 return declared;
6470 }
6471
6472
6473 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6474 which of the specific bindings (if any) matches the arglist and transform
6475 the expression into a call of that binding. */
6476
6477 static bool
6478 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6479 {
6480 gfc_typebound_proc* genproc;
6481 const char* genname;
6482 gfc_symtree *st;
6483 gfc_symbol *derived;
6484
6485 gcc_assert (e->expr_type == EXPR_COMPCALL);
6486 genname = e->value.compcall.name;
6487 genproc = e->value.compcall.tbp;
6488
6489 if (!genproc->is_generic)
6490 return true;
6491
6492 /* Try the bindings on this type and in the inheritance hierarchy. */
6493 for (; genproc; genproc = genproc->overridden)
6494 {
6495 gfc_tbp_generic* g;
6496
6497 gcc_assert (genproc->is_generic);
6498 for (g = genproc->u.generic; g; g = g->next)
6499 {
6500 gfc_symbol* target;
6501 gfc_actual_arglist* args;
6502 bool matches;
6503
6504 gcc_assert (g->specific);
6505
6506 if (g->specific->error)
6507 continue;
6508
6509 target = g->specific->u.specific->n.sym;
6510
6511 /* Get the right arglist by handling PASS/NOPASS. */
6512 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6513 if (!g->specific->nopass)
6514 {
6515 gfc_expr* po;
6516 po = extract_compcall_passed_object (e);
6517 if (!po)
6518 {
6519 gfc_free_actual_arglist (args);
6520 return false;
6521 }
6522
6523 gcc_assert (g->specific->pass_arg_num > 0);
6524 gcc_assert (!g->specific->error);
6525 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6526 g->specific->pass_arg);
6527 }
6528 resolve_actual_arglist (args, target->attr.proc,
6529 is_external_proc (target)
6530 && gfc_sym_get_dummy_args (target) == NULL);
6531
6532 /* Check if this arglist matches the formal. */
6533 matches = gfc_arglist_matches_symbol (&args, target);
6534
6535 /* Clean up and break out of the loop if we've found it. */
6536 gfc_free_actual_arglist (args);
6537 if (matches)
6538 {
6539 e->value.compcall.tbp = g->specific;
6540 genname = g->specific_st->name;
6541 /* Pass along the name for CLASS methods, where the vtab
6542 procedure pointer component has to be referenced. */
6543 if (name)
6544 *name = genname;
6545 goto success;
6546 }
6547 }
6548 }
6549
6550 /* Nothing matching found! */
6551 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6552 " %qs at %L", genname, &e->where);
6553 return false;
6554
6555 success:
6556 /* Make sure that we have the right specific instance for the name. */
6557 derived = get_declared_from_expr (NULL, NULL, e, true);
6558
6559 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6560 if (st)
6561 e->value.compcall.tbp = st->n.tb;
6562
6563 return true;
6564 }
6565
6566
6567 /* Resolve a call to a type-bound subroutine. */
6568
6569 static bool
6570 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6571 {
6572 gfc_actual_arglist* newactual;
6573 gfc_symtree* target;
6574
6575 /* Check that's really a SUBROUTINE. */
6576 if (!c->expr1->value.compcall.tbp->subroutine)
6577 {
6578 if (!c->expr1->value.compcall.tbp->is_generic
6579 && c->expr1->value.compcall.tbp->u.specific
6580 && c->expr1->value.compcall.tbp->u.specific->n.sym
6581 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6582 c->expr1->value.compcall.tbp->subroutine = 1;
6583 else
6584 {
6585 gfc_error ("%qs at %L should be a SUBROUTINE",
6586 c->expr1->value.compcall.name, &c->loc);
6587 return false;
6588 }
6589 }
6590
6591 if (!check_typebound_baseobject (c->expr1))
6592 return false;
6593
6594 /* Pass along the name for CLASS methods, where the vtab
6595 procedure pointer component has to be referenced. */
6596 if (name)
6597 *name = c->expr1->value.compcall.name;
6598
6599 if (!resolve_typebound_generic_call (c->expr1, name))
6600 return false;
6601
6602 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6603 if (overridable)
6604 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6605
6606 /* Transform into an ordinary EXEC_CALL for now. */
6607
6608 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6609 return false;
6610
6611 c->ext.actual = newactual;
6612 c->symtree = target;
6613 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6614
6615 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6616
6617 gfc_free_expr (c->expr1);
6618 c->expr1 = gfc_get_expr ();
6619 c->expr1->expr_type = EXPR_FUNCTION;
6620 c->expr1->symtree = target;
6621 c->expr1->where = c->loc;
6622
6623 return resolve_call (c);
6624 }
6625
6626
6627 /* Resolve a component-call expression. */
6628 static bool
6629 resolve_compcall (gfc_expr* e, const char **name)
6630 {
6631 gfc_actual_arglist* newactual;
6632 gfc_symtree* target;
6633
6634 /* Check that's really a FUNCTION. */
6635 if (!e->value.compcall.tbp->function)
6636 {
6637 gfc_error ("%qs at %L should be a FUNCTION",
6638 e->value.compcall.name, &e->where);
6639 return false;
6640 }
6641
6642
6643 /* These must not be assign-calls! */
6644 gcc_assert (!e->value.compcall.assign);
6645
6646 if (!check_typebound_baseobject (e))
6647 return false;
6648
6649 /* Pass along the name for CLASS methods, where the vtab
6650 procedure pointer component has to be referenced. */
6651 if (name)
6652 *name = e->value.compcall.name;
6653
6654 if (!resolve_typebound_generic_call (e, name))
6655 return false;
6656 gcc_assert (!e->value.compcall.tbp->is_generic);
6657
6658 /* Take the rank from the function's symbol. */
6659 if (e->value.compcall.tbp->u.specific->n.sym->as)
6660 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6661
6662 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6663 arglist to the TBP's binding target. */
6664
6665 if (!resolve_typebound_static (e, &target, &newactual))
6666 return false;
6667
6668 e->value.function.actual = newactual;
6669 e->value.function.name = NULL;
6670 e->value.function.esym = target->n.sym;
6671 e->value.function.isym = NULL;
6672 e->symtree = target;
6673 e->ts = target->n.sym->ts;
6674 e->expr_type = EXPR_FUNCTION;
6675
6676 /* Resolution is not necessary if this is a class subroutine; this
6677 function only has to identify the specific proc. Resolution of
6678 the call will be done next in resolve_typebound_call. */
6679 return gfc_resolve_expr (e);
6680 }
6681
6682
6683 static bool resolve_fl_derived (gfc_symbol *sym);
6684
6685
6686 /* Resolve a typebound function, or 'method'. First separate all
6687 the non-CLASS references by calling resolve_compcall directly. */
6688
6689 static bool
6690 resolve_typebound_function (gfc_expr* e)
6691 {
6692 gfc_symbol *declared;
6693 gfc_component *c;
6694 gfc_ref *new_ref;
6695 gfc_ref *class_ref;
6696 gfc_symtree *st;
6697 const char *name;
6698 gfc_typespec ts;
6699 gfc_expr *expr;
6700 bool overridable;
6701
6702 st = e->symtree;
6703
6704 /* Deal with typebound operators for CLASS objects. */
6705 expr = e->value.compcall.base_object;
6706 overridable = !e->value.compcall.tbp->non_overridable;
6707 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6708 {
6709 /* Since the typebound operators are generic, we have to ensure
6710 that any delays in resolution are corrected and that the vtab
6711 is present. */
6712 ts = expr->ts;
6713 declared = ts.u.derived;
6714 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6715 if (c->ts.u.derived == NULL)
6716 c->ts.u.derived = gfc_find_derived_vtab (declared);
6717
6718 if (!resolve_compcall (e, &name))
6719 return false;
6720
6721 /* Use the generic name if it is there. */
6722 name = name ? name : e->value.function.esym->name;
6723 e->symtree = expr->symtree;
6724 e->ref = gfc_copy_ref (expr->ref);
6725 get_declared_from_expr (&class_ref, NULL, e, false);
6726
6727 /* Trim away the extraneous references that emerge from nested
6728 use of interface.c (extend_expr). */
6729 if (class_ref && class_ref->next)
6730 {
6731 gfc_free_ref_list (class_ref->next);
6732 class_ref->next = NULL;
6733 }
6734 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6735 {
6736 gfc_free_ref_list (e->ref);
6737 e->ref = NULL;
6738 }
6739
6740 gfc_add_vptr_component (e);
6741 gfc_add_component_ref (e, name);
6742 e->value.function.esym = NULL;
6743 if (expr->expr_type != EXPR_VARIABLE)
6744 e->base_expr = expr;
6745 return true;
6746 }
6747
6748 if (st == NULL)
6749 return resolve_compcall (e, NULL);
6750
6751 if (!gfc_resolve_ref (e))
6752 return false;
6753
6754 /* Get the CLASS declared type. */
6755 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6756
6757 if (!resolve_fl_derived (declared))
6758 return false;
6759
6760 /* Weed out cases of the ultimate component being a derived type. */
6761 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6762 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6763 {
6764 gfc_free_ref_list (new_ref);
6765 return resolve_compcall (e, NULL);
6766 }
6767
6768 c = gfc_find_component (declared, "_data", true, true, NULL);
6769
6770 /* Treat the call as if it is a typebound procedure, in order to roll
6771 out the correct name for the specific function. */
6772 if (!resolve_compcall (e, &name))
6773 {
6774 gfc_free_ref_list (new_ref);
6775 return false;
6776 }
6777 ts = e->ts;
6778
6779 if (overridable)
6780 {
6781 /* Convert the expression to a procedure pointer component call. */
6782 e->value.function.esym = NULL;
6783 e->symtree = st;
6784
6785 if (new_ref)
6786 e->ref = new_ref;
6787
6788 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6789 gfc_add_vptr_component (e);
6790 gfc_add_component_ref (e, name);
6791
6792 /* Recover the typespec for the expression. This is really only
6793 necessary for generic procedures, where the additional call
6794 to gfc_add_component_ref seems to throw the collection of the
6795 correct typespec. */
6796 e->ts = ts;
6797 }
6798 else if (new_ref)
6799 gfc_free_ref_list (new_ref);
6800
6801 return true;
6802 }
6803
6804 /* Resolve a typebound subroutine, or 'method'. First separate all
6805 the non-CLASS references by calling resolve_typebound_call
6806 directly. */
6807
6808 static bool
6809 resolve_typebound_subroutine (gfc_code *code)
6810 {
6811 gfc_symbol *declared;
6812 gfc_component *c;
6813 gfc_ref *new_ref;
6814 gfc_ref *class_ref;
6815 gfc_symtree *st;
6816 const char *name;
6817 gfc_typespec ts;
6818 gfc_expr *expr;
6819 bool overridable;
6820
6821 st = code->expr1->symtree;
6822
6823 /* Deal with typebound operators for CLASS objects. */
6824 expr = code->expr1->value.compcall.base_object;
6825 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6826 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6827 {
6828 /* If the base_object is not a variable, the corresponding actual
6829 argument expression must be stored in e->base_expression so
6830 that the corresponding tree temporary can be used as the base
6831 object in gfc_conv_procedure_call. */
6832 if (expr->expr_type != EXPR_VARIABLE)
6833 {
6834 gfc_actual_arglist *args;
6835
6836 args= code->expr1->value.function.actual;
6837 for (; args; args = args->next)
6838 if (expr == args->expr)
6839 expr = args->expr;
6840 }
6841
6842 /* Since the typebound operators are generic, we have to ensure
6843 that any delays in resolution are corrected and that the vtab
6844 is present. */
6845 declared = expr->ts.u.derived;
6846 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6847 if (c->ts.u.derived == NULL)
6848 c->ts.u.derived = gfc_find_derived_vtab (declared);
6849
6850 if (!resolve_typebound_call (code, &name, NULL))
6851 return false;
6852
6853 /* Use the generic name if it is there. */
6854 name = name ? name : code->expr1->value.function.esym->name;
6855 code->expr1->symtree = expr->symtree;
6856 code->expr1->ref = gfc_copy_ref (expr->ref);
6857
6858 /* Trim away the extraneous references that emerge from nested
6859 use of interface.c (extend_expr). */
6860 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6861 if (class_ref && class_ref->next)
6862 {
6863 gfc_free_ref_list (class_ref->next);
6864 class_ref->next = NULL;
6865 }
6866 else if (code->expr1->ref && !class_ref)
6867 {
6868 gfc_free_ref_list (code->expr1->ref);
6869 code->expr1->ref = NULL;
6870 }
6871
6872 /* Now use the procedure in the vtable. */
6873 gfc_add_vptr_component (code->expr1);
6874 gfc_add_component_ref (code->expr1, name);
6875 code->expr1->value.function.esym = NULL;
6876 if (expr->expr_type != EXPR_VARIABLE)
6877 code->expr1->base_expr = expr;
6878 return true;
6879 }
6880
6881 if (st == NULL)
6882 return resolve_typebound_call (code, NULL, NULL);
6883
6884 if (!gfc_resolve_ref (code->expr1))
6885 return false;
6886
6887 /* Get the CLASS declared type. */
6888 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6889
6890 /* Weed out cases of the ultimate component being a derived type. */
6891 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6892 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6893 {
6894 gfc_free_ref_list (new_ref);
6895 return resolve_typebound_call (code, NULL, NULL);
6896 }
6897
6898 if (!resolve_typebound_call (code, &name, &overridable))
6899 {
6900 gfc_free_ref_list (new_ref);
6901 return false;
6902 }
6903 ts = code->expr1->ts;
6904
6905 if (overridable)
6906 {
6907 /* Convert the expression to a procedure pointer component call. */
6908 code->expr1->value.function.esym = NULL;
6909 code->expr1->symtree = st;
6910
6911 if (new_ref)
6912 code->expr1->ref = new_ref;
6913
6914 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6915 gfc_add_vptr_component (code->expr1);
6916 gfc_add_component_ref (code->expr1, name);
6917
6918 /* Recover the typespec for the expression. This is really only
6919 necessary for generic procedures, where the additional call
6920 to gfc_add_component_ref seems to throw the collection of the
6921 correct typespec. */
6922 code->expr1->ts = ts;
6923 }
6924 else if (new_ref)
6925 gfc_free_ref_list (new_ref);
6926
6927 return true;
6928 }
6929
6930
6931 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6932
6933 static bool
6934 resolve_ppc_call (gfc_code* c)
6935 {
6936 gfc_component *comp;
6937
6938 comp = gfc_get_proc_ptr_comp (c->expr1);
6939 gcc_assert (comp != NULL);
6940
6941 c->resolved_sym = c->expr1->symtree->n.sym;
6942 c->expr1->expr_type = EXPR_VARIABLE;
6943
6944 if (!comp->attr.subroutine)
6945 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6946
6947 if (!gfc_resolve_ref (c->expr1))
6948 return false;
6949
6950 if (!update_ppc_arglist (c->expr1))
6951 return false;
6952
6953 c->ext.actual = c->expr1->value.compcall.actual;
6954
6955 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6956 !(comp->ts.interface
6957 && comp->ts.interface->formal)))
6958 return false;
6959
6960 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6961 return false;
6962
6963 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6964
6965 return true;
6966 }
6967
6968
6969 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6970
6971 static bool
6972 resolve_expr_ppc (gfc_expr* e)
6973 {
6974 gfc_component *comp;
6975
6976 comp = gfc_get_proc_ptr_comp (e);
6977 gcc_assert (comp != NULL);
6978
6979 /* Convert to EXPR_FUNCTION. */
6980 e->expr_type = EXPR_FUNCTION;
6981 e->value.function.isym = NULL;
6982 e->value.function.actual = e->value.compcall.actual;
6983 e->ts = comp->ts;
6984 if (comp->as != NULL)
6985 e->rank = comp->as->rank;
6986
6987 if (!comp->attr.function)
6988 gfc_add_function (&comp->attr, comp->name, &e->where);
6989
6990 if (!gfc_resolve_ref (e))
6991 return false;
6992
6993 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6994 !(comp->ts.interface
6995 && comp->ts.interface->formal)))
6996 return false;
6997
6998 if (!update_ppc_arglist (e))
6999 return false;
7000
7001 if (!check_pure_function(e))
7002 return false;
7003
7004 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7005
7006 return true;
7007 }
7008
7009
7010 static bool
7011 gfc_is_expandable_expr (gfc_expr *e)
7012 {
7013 gfc_constructor *con;
7014
7015 if (e->expr_type == EXPR_ARRAY)
7016 {
7017 /* Traverse the constructor looking for variables that are flavor
7018 parameter. Parameters must be expanded since they are fully used at
7019 compile time. */
7020 con = gfc_constructor_first (e->value.constructor);
7021 for (; con; con = gfc_constructor_next (con))
7022 {
7023 if (con->expr->expr_type == EXPR_VARIABLE
7024 && con->expr->symtree
7025 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7026 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7027 return true;
7028 if (con->expr->expr_type == EXPR_ARRAY
7029 && gfc_is_expandable_expr (con->expr))
7030 return true;
7031 }
7032 }
7033
7034 return false;
7035 }
7036
7037
7038 /* Sometimes variables in specification expressions of the result
7039 of module procedures in submodules wind up not being the 'real'
7040 dummy. Find this, if possible, in the namespace of the first
7041 formal argument. */
7042
7043 static void
7044 fixup_unique_dummy (gfc_expr *e)
7045 {
7046 gfc_symtree *st = NULL;
7047 gfc_symbol *s = NULL;
7048
7049 if (e->symtree->n.sym->ns->proc_name
7050 && e->symtree->n.sym->ns->proc_name->formal)
7051 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7052
7053 if (s != NULL)
7054 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7055
7056 if (st != NULL
7057 && st->n.sym != NULL
7058 && st->n.sym->attr.dummy)
7059 e->symtree = st;
7060 }
7061
7062 /* Resolve an expression. That is, make sure that types of operands agree
7063 with their operators, intrinsic operators are converted to function calls
7064 for overloaded types and unresolved function references are resolved. */
7065
7066 bool
7067 gfc_resolve_expr (gfc_expr *e)
7068 {
7069 bool t;
7070 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7071
7072 if (e == NULL || e->do_not_resolve_again)
7073 return true;
7074
7075 /* inquiry_argument only applies to variables. */
7076 inquiry_save = inquiry_argument;
7077 actual_arg_save = actual_arg;
7078 first_actual_arg_save = first_actual_arg;
7079
7080 if (e->expr_type != EXPR_VARIABLE)
7081 {
7082 inquiry_argument = false;
7083 actual_arg = false;
7084 first_actual_arg = false;
7085 }
7086 else if (e->symtree != NULL
7087 && *e->symtree->name == '@'
7088 && e->symtree->n.sym->attr.dummy)
7089 {
7090 /* Deal with submodule specification expressions that are not
7091 found to be referenced in module.c(read_cleanup). */
7092 fixup_unique_dummy (e);
7093 }
7094
7095 switch (e->expr_type)
7096 {
7097 case EXPR_OP:
7098 t = resolve_operator (e);
7099 break;
7100
7101 case EXPR_FUNCTION:
7102 case EXPR_VARIABLE:
7103
7104 if (check_host_association (e))
7105 t = resolve_function (e);
7106 else
7107 t = resolve_variable (e);
7108
7109 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7110 && e->ref->type != REF_SUBSTRING)
7111 gfc_resolve_substring_charlen (e);
7112
7113 break;
7114
7115 case EXPR_COMPCALL:
7116 t = resolve_typebound_function (e);
7117 break;
7118
7119 case EXPR_SUBSTRING:
7120 t = gfc_resolve_ref (e);
7121 break;
7122
7123 case EXPR_CONSTANT:
7124 case EXPR_NULL:
7125 t = true;
7126 break;
7127
7128 case EXPR_PPC:
7129 t = resolve_expr_ppc (e);
7130 break;
7131
7132 case EXPR_ARRAY:
7133 t = false;
7134 if (!gfc_resolve_ref (e))
7135 break;
7136
7137 t = gfc_resolve_array_constructor (e);
7138 /* Also try to expand a constructor. */
7139 if (t)
7140 {
7141 gfc_expression_rank (e);
7142 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7143 gfc_expand_constructor (e, false);
7144 }
7145
7146 /* This provides the opportunity for the length of constructors with
7147 character valued function elements to propagate the string length
7148 to the expression. */
7149 if (t && e->ts.type == BT_CHARACTER)
7150 {
7151 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7152 here rather then add a duplicate test for it above. */
7153 gfc_expand_constructor (e, false);
7154 t = gfc_resolve_character_array_constructor (e);
7155 }
7156
7157 break;
7158
7159 case EXPR_STRUCTURE:
7160 t = gfc_resolve_ref (e);
7161 if (!t)
7162 break;
7163
7164 t = resolve_structure_cons (e, 0);
7165 if (!t)
7166 break;
7167
7168 t = gfc_simplify_expr (e, 0);
7169 break;
7170
7171 default:
7172 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7173 }
7174
7175 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7176 fixup_charlen (e);
7177
7178 inquiry_argument = inquiry_save;
7179 actual_arg = actual_arg_save;
7180 first_actual_arg = first_actual_arg_save;
7181
7182 /* For some reason, resolving these expressions a second time mangles
7183 the typespec of the expression itself. */
7184 if (t && e->expr_type == EXPR_VARIABLE
7185 && e->symtree->n.sym->attr.select_rank_temporary
7186 && UNLIMITED_POLY (e->symtree->n.sym))
7187 e->do_not_resolve_again = 1;
7188
7189 return t;
7190 }
7191
7192
7193 /* Resolve an expression from an iterator. They must be scalar and have
7194 INTEGER or (optionally) REAL type. */
7195
7196 static bool
7197 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7198 const char *name_msgid)
7199 {
7200 if (!gfc_resolve_expr (expr))
7201 return false;
7202
7203 if (expr->rank != 0)
7204 {
7205 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7206 return false;
7207 }
7208
7209 if (expr->ts.type != BT_INTEGER)
7210 {
7211 if (expr->ts.type == BT_REAL)
7212 {
7213 if (real_ok)
7214 return gfc_notify_std (GFC_STD_F95_DEL,
7215 "%s at %L must be integer",
7216 _(name_msgid), &expr->where);
7217 else
7218 {
7219 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7220 &expr->where);
7221 return false;
7222 }
7223 }
7224 else
7225 {
7226 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7227 return false;
7228 }
7229 }
7230 return true;
7231 }
7232
7233
7234 /* Resolve the expressions in an iterator structure. If REAL_OK is
7235 false allow only INTEGER type iterators, otherwise allow REAL types.
7236 Set own_scope to true for ac-implied-do and data-implied-do as those
7237 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7238
7239 bool
7240 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7241 {
7242 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7243 return false;
7244
7245 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7246 _("iterator variable")))
7247 return false;
7248
7249 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7250 "Start expression in DO loop"))
7251 return false;
7252
7253 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7254 "End expression in DO loop"))
7255 return false;
7256
7257 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7258 "Step expression in DO loop"))
7259 return false;
7260
7261 /* Convert start, end, and step to the same type as var. */
7262 if (iter->start->ts.kind != iter->var->ts.kind
7263 || iter->start->ts.type != iter->var->ts.type)
7264 gfc_convert_type (iter->start, &iter->var->ts, 1);
7265
7266 if (iter->end->ts.kind != iter->var->ts.kind
7267 || iter->end->ts.type != iter->var->ts.type)
7268 gfc_convert_type (iter->end, &iter->var->ts, 1);
7269
7270 if (iter->step->ts.kind != iter->var->ts.kind
7271 || iter->step->ts.type != iter->var->ts.type)
7272 gfc_convert_type (iter->step, &iter->var->ts, 1);
7273
7274 if (iter->step->expr_type == EXPR_CONSTANT)
7275 {
7276 if ((iter->step->ts.type == BT_INTEGER
7277 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7278 || (iter->step->ts.type == BT_REAL
7279 && mpfr_sgn (iter->step->value.real) == 0))
7280 {
7281 gfc_error ("Step expression in DO loop at %L cannot be zero",
7282 &iter->step->where);
7283 return false;
7284 }
7285 }
7286
7287 if (iter->start->expr_type == EXPR_CONSTANT
7288 && iter->end->expr_type == EXPR_CONSTANT
7289 && iter->step->expr_type == EXPR_CONSTANT)
7290 {
7291 int sgn, cmp;
7292 if (iter->start->ts.type == BT_INTEGER)
7293 {
7294 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7295 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7296 }
7297 else
7298 {
7299 sgn = mpfr_sgn (iter->step->value.real);
7300 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7301 }
7302 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7303 gfc_warning (OPT_Wzerotrip,
7304 "DO loop at %L will be executed zero times",
7305 &iter->step->where);
7306 }
7307
7308 if (iter->end->expr_type == EXPR_CONSTANT
7309 && iter->end->ts.type == BT_INTEGER
7310 && iter->step->expr_type == EXPR_CONSTANT
7311 && iter->step->ts.type == BT_INTEGER
7312 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7313 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7314 {
7315 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7316 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7317
7318 if (is_step_positive
7319 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7320 gfc_warning (OPT_Wundefined_do_loop,
7321 "DO loop at %L is undefined as it overflows",
7322 &iter->step->where);
7323 else if (!is_step_positive
7324 && mpz_cmp (iter->end->value.integer,
7325 gfc_integer_kinds[k].min_int) == 0)
7326 gfc_warning (OPT_Wundefined_do_loop,
7327 "DO loop at %L is undefined as it underflows",
7328 &iter->step->where);
7329 }
7330
7331 return true;
7332 }
7333
7334
7335 /* Traversal function for find_forall_index. f == 2 signals that
7336 that variable itself is not to be checked - only the references. */
7337
7338 static bool
7339 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7340 {
7341 if (expr->expr_type != EXPR_VARIABLE)
7342 return false;
7343
7344 /* A scalar assignment */
7345 if (!expr->ref || *f == 1)
7346 {
7347 if (expr->symtree->n.sym == sym)
7348 return true;
7349 else
7350 return false;
7351 }
7352
7353 if (*f == 2)
7354 *f = 1;
7355 return false;
7356 }
7357
7358
7359 /* Check whether the FORALL index appears in the expression or not.
7360 Returns true if SYM is found in EXPR. */
7361
7362 bool
7363 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7364 {
7365 if (gfc_traverse_expr (expr, sym, forall_index, f))
7366 return true;
7367 else
7368 return false;
7369 }
7370
7371
7372 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7373 to be a scalar INTEGER variable. The subscripts and stride are scalar
7374 INTEGERs, and if stride is a constant it must be nonzero.
7375 Furthermore "A subscript or stride in a forall-triplet-spec shall
7376 not contain a reference to any index-name in the
7377 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7378
7379 static void
7380 resolve_forall_iterators (gfc_forall_iterator *it)
7381 {
7382 gfc_forall_iterator *iter, *iter2;
7383
7384 for (iter = it; iter; iter = iter->next)
7385 {
7386 if (gfc_resolve_expr (iter->var)
7387 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7388 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7389 &iter->var->where);
7390
7391 if (gfc_resolve_expr (iter->start)
7392 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7393 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7394 &iter->start->where);
7395 if (iter->var->ts.kind != iter->start->ts.kind)
7396 gfc_convert_type (iter->start, &iter->var->ts, 1);
7397
7398 if (gfc_resolve_expr (iter->end)
7399 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7400 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7401 &iter->end->where);
7402 if (iter->var->ts.kind != iter->end->ts.kind)
7403 gfc_convert_type (iter->end, &iter->var->ts, 1);
7404
7405 if (gfc_resolve_expr (iter->stride))
7406 {
7407 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7408 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7409 &iter->stride->where, "INTEGER");
7410
7411 if (iter->stride->expr_type == EXPR_CONSTANT
7412 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7413 gfc_error ("FORALL stride expression at %L cannot be zero",
7414 &iter->stride->where);
7415 }
7416 if (iter->var->ts.kind != iter->stride->ts.kind)
7417 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7418 }
7419
7420 for (iter = it; iter; iter = iter->next)
7421 for (iter2 = iter; iter2; iter2 = iter2->next)
7422 {
7423 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7424 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7425 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7426 gfc_error ("FORALL index %qs may not appear in triplet "
7427 "specification at %L", iter->var->symtree->name,
7428 &iter2->start->where);
7429 }
7430 }
7431
7432
7433 /* Given a pointer to a symbol that is a derived type, see if it's
7434 inaccessible, i.e. if it's defined in another module and the components are
7435 PRIVATE. The search is recursive if necessary. Returns zero if no
7436 inaccessible components are found, nonzero otherwise. */
7437
7438 static int
7439 derived_inaccessible (gfc_symbol *sym)
7440 {
7441 gfc_component *c;
7442
7443 if (sym->attr.use_assoc && sym->attr.private_comp)
7444 return 1;
7445
7446 for (c = sym->components; c; c = c->next)
7447 {
7448 /* Prevent an infinite loop through this function. */
7449 if (c->ts.type == BT_DERIVED && c->attr.pointer
7450 && sym == c->ts.u.derived)
7451 continue;
7452
7453 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7454 return 1;
7455 }
7456
7457 return 0;
7458 }
7459
7460
7461 /* Resolve the argument of a deallocate expression. The expression must be
7462 a pointer or a full array. */
7463
7464 static bool
7465 resolve_deallocate_expr (gfc_expr *e)
7466 {
7467 symbol_attribute attr;
7468 int allocatable, pointer;
7469 gfc_ref *ref;
7470 gfc_symbol *sym;
7471 gfc_component *c;
7472 bool unlimited;
7473
7474 if (!gfc_resolve_expr (e))
7475 return false;
7476
7477 if (e->expr_type != EXPR_VARIABLE)
7478 goto bad;
7479
7480 sym = e->symtree->n.sym;
7481 unlimited = UNLIMITED_POLY(sym);
7482
7483 if (sym->ts.type == BT_CLASS)
7484 {
7485 allocatable = CLASS_DATA (sym)->attr.allocatable;
7486 pointer = CLASS_DATA (sym)->attr.class_pointer;
7487 }
7488 else
7489 {
7490 allocatable = sym->attr.allocatable;
7491 pointer = sym->attr.pointer;
7492 }
7493 for (ref = e->ref; ref; ref = ref->next)
7494 {
7495 switch (ref->type)
7496 {
7497 case REF_ARRAY:
7498 if (ref->u.ar.type != AR_FULL
7499 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7500 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7501 allocatable = 0;
7502 break;
7503
7504 case REF_COMPONENT:
7505 c = ref->u.c.component;
7506 if (c->ts.type == BT_CLASS)
7507 {
7508 allocatable = CLASS_DATA (c)->attr.allocatable;
7509 pointer = CLASS_DATA (c)->attr.class_pointer;
7510 }
7511 else
7512 {
7513 allocatable = c->attr.allocatable;
7514 pointer = c->attr.pointer;
7515 }
7516 break;
7517
7518 case REF_SUBSTRING:
7519 case REF_INQUIRY:
7520 allocatable = 0;
7521 break;
7522 }
7523 }
7524
7525 attr = gfc_expr_attr (e);
7526
7527 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7528 {
7529 bad:
7530 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7531 &e->where);
7532 return false;
7533 }
7534
7535 /* F2008, C644. */
7536 if (gfc_is_coindexed (e))
7537 {
7538 gfc_error ("Coindexed allocatable object at %L", &e->where);
7539 return false;
7540 }
7541
7542 if (pointer
7543 && !gfc_check_vardef_context (e, true, true, false,
7544 _("DEALLOCATE object")))
7545 return false;
7546 if (!gfc_check_vardef_context (e, false, true, false,
7547 _("DEALLOCATE object")))
7548 return false;
7549
7550 return true;
7551 }
7552
7553
7554 /* Returns true if the expression e contains a reference to the symbol sym. */
7555 static bool
7556 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7557 {
7558 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7559 return true;
7560
7561 return false;
7562 }
7563
7564 bool
7565 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7566 {
7567 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7568 }
7569
7570
7571 /* Given the expression node e for an allocatable/pointer of derived type to be
7572 allocated, get the expression node to be initialized afterwards (needed for
7573 derived types with default initializers, and derived types with allocatable
7574 components that need nullification.) */
7575
7576 gfc_expr *
7577 gfc_expr_to_initialize (gfc_expr *e)
7578 {
7579 gfc_expr *result;
7580 gfc_ref *ref;
7581 int i;
7582
7583 result = gfc_copy_expr (e);
7584
7585 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7586 for (ref = result->ref; ref; ref = ref->next)
7587 if (ref->type == REF_ARRAY && ref->next == NULL)
7588 {
7589 if (ref->u.ar.dimen == 0
7590 && ref->u.ar.as && ref->u.ar.as->corank)
7591 return result;
7592
7593 ref->u.ar.type = AR_FULL;
7594
7595 for (i = 0; i < ref->u.ar.dimen; i++)
7596 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7597
7598 break;
7599 }
7600
7601 gfc_free_shape (&result->shape, result->rank);
7602
7603 /* Recalculate rank, shape, etc. */
7604 gfc_resolve_expr (result);
7605 return result;
7606 }
7607
7608
7609 /* If the last ref of an expression is an array ref, return a copy of the
7610 expression with that one removed. Otherwise, a copy of the original
7611 expression. This is used for allocate-expressions and pointer assignment
7612 LHS, where there may be an array specification that needs to be stripped
7613 off when using gfc_check_vardef_context. */
7614
7615 static gfc_expr*
7616 remove_last_array_ref (gfc_expr* e)
7617 {
7618 gfc_expr* e2;
7619 gfc_ref** r;
7620
7621 e2 = gfc_copy_expr (e);
7622 for (r = &e2->ref; *r; r = &(*r)->next)
7623 if ((*r)->type == REF_ARRAY && !(*r)->next)
7624 {
7625 gfc_free_ref_list (*r);
7626 *r = NULL;
7627 break;
7628 }
7629
7630 return e2;
7631 }
7632
7633
7634 /* Used in resolve_allocate_expr to check that a allocation-object and
7635 a source-expr are conformable. This does not catch all possible
7636 cases; in particular a runtime checking is needed. */
7637
7638 static bool
7639 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7640 {
7641 gfc_ref *tail;
7642 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7643
7644 /* First compare rank. */
7645 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7646 || (!tail && e1->rank != e2->rank))
7647 {
7648 gfc_error ("Source-expr at %L must be scalar or have the "
7649 "same rank as the allocate-object at %L",
7650 &e1->where, &e2->where);
7651 return false;
7652 }
7653
7654 if (e1->shape)
7655 {
7656 int i;
7657 mpz_t s;
7658
7659 mpz_init (s);
7660
7661 for (i = 0; i < e1->rank; i++)
7662 {
7663 if (tail->u.ar.start[i] == NULL)
7664 break;
7665
7666 if (tail->u.ar.end[i])
7667 {
7668 mpz_set (s, tail->u.ar.end[i]->value.integer);
7669 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7670 mpz_add_ui (s, s, 1);
7671 }
7672 else
7673 {
7674 mpz_set (s, tail->u.ar.start[i]->value.integer);
7675 }
7676
7677 if (mpz_cmp (e1->shape[i], s) != 0)
7678 {
7679 gfc_error ("Source-expr at %L and allocate-object at %L must "
7680 "have the same shape", &e1->where, &e2->where);
7681 mpz_clear (s);
7682 return false;
7683 }
7684 }
7685
7686 mpz_clear (s);
7687 }
7688
7689 return true;
7690 }
7691
7692
7693 /* Resolve the expression in an ALLOCATE statement, doing the additional
7694 checks to see whether the expression is OK or not. The expression must
7695 have a trailing array reference that gives the size of the array. */
7696
7697 static bool
7698 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7699 {
7700 int i, pointer, allocatable, dimension, is_abstract;
7701 int codimension;
7702 bool coindexed;
7703 bool unlimited;
7704 symbol_attribute attr;
7705 gfc_ref *ref, *ref2;
7706 gfc_expr *e2;
7707 gfc_array_ref *ar;
7708 gfc_symbol *sym = NULL;
7709 gfc_alloc *a;
7710 gfc_component *c;
7711 bool t;
7712
7713 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7714 checking of coarrays. */
7715 for (ref = e->ref; ref; ref = ref->next)
7716 if (ref->next == NULL)
7717 break;
7718
7719 if (ref && ref->type == REF_ARRAY)
7720 ref->u.ar.in_allocate = true;
7721
7722 if (!gfc_resolve_expr (e))
7723 goto failure;
7724
7725 /* Make sure the expression is allocatable or a pointer. If it is
7726 pointer, the next-to-last reference must be a pointer. */
7727
7728 ref2 = NULL;
7729 if (e->symtree)
7730 sym = e->symtree->n.sym;
7731
7732 /* Check whether ultimate component is abstract and CLASS. */
7733 is_abstract = 0;
7734
7735 /* Is the allocate-object unlimited polymorphic? */
7736 unlimited = UNLIMITED_POLY(e);
7737
7738 if (e->expr_type != EXPR_VARIABLE)
7739 {
7740 allocatable = 0;
7741 attr = gfc_expr_attr (e);
7742 pointer = attr.pointer;
7743 dimension = attr.dimension;
7744 codimension = attr.codimension;
7745 }
7746 else
7747 {
7748 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7749 {
7750 allocatable = CLASS_DATA (sym)->attr.allocatable;
7751 pointer = CLASS_DATA (sym)->attr.class_pointer;
7752 dimension = CLASS_DATA (sym)->attr.dimension;
7753 codimension = CLASS_DATA (sym)->attr.codimension;
7754 is_abstract = CLASS_DATA (sym)->attr.abstract;
7755 }
7756 else
7757 {
7758 allocatable = sym->attr.allocatable;
7759 pointer = sym->attr.pointer;
7760 dimension = sym->attr.dimension;
7761 codimension = sym->attr.codimension;
7762 }
7763
7764 coindexed = false;
7765
7766 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7767 {
7768 switch (ref->type)
7769 {
7770 case REF_ARRAY:
7771 if (ref->u.ar.codimen > 0)
7772 {
7773 int n;
7774 for (n = ref->u.ar.dimen;
7775 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7776 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7777 {
7778 coindexed = true;
7779 break;
7780 }
7781 }
7782
7783 if (ref->next != NULL)
7784 pointer = 0;
7785 break;
7786
7787 case REF_COMPONENT:
7788 /* F2008, C644. */
7789 if (coindexed)
7790 {
7791 gfc_error ("Coindexed allocatable object at %L",
7792 &e->where);
7793 goto failure;
7794 }
7795
7796 c = ref->u.c.component;
7797 if (c->ts.type == BT_CLASS)
7798 {
7799 allocatable = CLASS_DATA (c)->attr.allocatable;
7800 pointer = CLASS_DATA (c)->attr.class_pointer;
7801 dimension = CLASS_DATA (c)->attr.dimension;
7802 codimension = CLASS_DATA (c)->attr.codimension;
7803 is_abstract = CLASS_DATA (c)->attr.abstract;
7804 }
7805 else
7806 {
7807 allocatable = c->attr.allocatable;
7808 pointer = c->attr.pointer;
7809 dimension = c->attr.dimension;
7810 codimension = c->attr.codimension;
7811 is_abstract = c->attr.abstract;
7812 }
7813 break;
7814
7815 case REF_SUBSTRING:
7816 case REF_INQUIRY:
7817 allocatable = 0;
7818 pointer = 0;
7819 break;
7820 }
7821 }
7822 }
7823
7824 /* Check for F08:C628. */
7825 if (allocatable == 0 && pointer == 0 && !unlimited)
7826 {
7827 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7828 &e->where);
7829 goto failure;
7830 }
7831
7832 /* Some checks for the SOURCE tag. */
7833 if (code->expr3)
7834 {
7835 /* Check F03:C631. */
7836 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7837 {
7838 gfc_error ("Type of entity at %L is type incompatible with "
7839 "source-expr at %L", &e->where, &code->expr3->where);
7840 goto failure;
7841 }
7842
7843 /* Check F03:C632 and restriction following Note 6.18. */
7844 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7845 goto failure;
7846
7847 /* Check F03:C633. */
7848 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7849 {
7850 gfc_error ("The allocate-object at %L and the source-expr at %L "
7851 "shall have the same kind type parameter",
7852 &e->where, &code->expr3->where);
7853 goto failure;
7854 }
7855
7856 /* Check F2008, C642. */
7857 if (code->expr3->ts.type == BT_DERIVED
7858 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7859 || (code->expr3->ts.u.derived->from_intmod
7860 == INTMOD_ISO_FORTRAN_ENV
7861 && code->expr3->ts.u.derived->intmod_sym_id
7862 == ISOFORTRAN_LOCK_TYPE)))
7863 {
7864 gfc_error ("The source-expr at %L shall neither be of type "
7865 "LOCK_TYPE nor have a LOCK_TYPE component if "
7866 "allocate-object at %L is a coarray",
7867 &code->expr3->where, &e->where);
7868 goto failure;
7869 }
7870
7871 /* Check TS18508, C702/C703. */
7872 if (code->expr3->ts.type == BT_DERIVED
7873 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7874 || (code->expr3->ts.u.derived->from_intmod
7875 == INTMOD_ISO_FORTRAN_ENV
7876 && code->expr3->ts.u.derived->intmod_sym_id
7877 == ISOFORTRAN_EVENT_TYPE)))
7878 {
7879 gfc_error ("The source-expr at %L shall neither be of type "
7880 "EVENT_TYPE nor have a EVENT_TYPE component if "
7881 "allocate-object at %L is a coarray",
7882 &code->expr3->where, &e->where);
7883 goto failure;
7884 }
7885 }
7886
7887 /* Check F08:C629. */
7888 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7889 && !code->expr3)
7890 {
7891 gcc_assert (e->ts.type == BT_CLASS);
7892 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7893 "type-spec or source-expr", sym->name, &e->where);
7894 goto failure;
7895 }
7896
7897 /* Check F08:C632. */
7898 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7899 && !UNLIMITED_POLY (e))
7900 {
7901 int cmp;
7902
7903 if (!e->ts.u.cl->length)
7904 goto failure;
7905
7906 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7907 code->ext.alloc.ts.u.cl->length);
7908 if (cmp == 1 || cmp == -1 || cmp == -3)
7909 {
7910 gfc_error ("Allocating %s at %L with type-spec requires the same "
7911 "character-length parameter as in the declaration",
7912 sym->name, &e->where);
7913 goto failure;
7914 }
7915 }
7916
7917 /* In the variable definition context checks, gfc_expr_attr is used
7918 on the expression. This is fooled by the array specification
7919 present in e, thus we have to eliminate that one temporarily. */
7920 e2 = remove_last_array_ref (e);
7921 t = true;
7922 if (t && pointer)
7923 t = gfc_check_vardef_context (e2, true, true, false,
7924 _("ALLOCATE object"));
7925 if (t)
7926 t = gfc_check_vardef_context (e2, false, true, false,
7927 _("ALLOCATE object"));
7928 gfc_free_expr (e2);
7929 if (!t)
7930 goto failure;
7931
7932 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7933 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7934 {
7935 /* For class arrays, the initialization with SOURCE is done
7936 using _copy and trans_call. It is convenient to exploit that
7937 when the allocated type is different from the declared type but
7938 no SOURCE exists by setting expr3. */
7939 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7940 }
7941 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7942 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7943 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7944 {
7945 /* We have to zero initialize the integer variable. */
7946 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7947 }
7948
7949 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7950 {
7951 /* Make sure the vtab symbol is present when
7952 the module variables are generated. */
7953 gfc_typespec ts = e->ts;
7954 if (code->expr3)
7955 ts = code->expr3->ts;
7956 else if (code->ext.alloc.ts.type == BT_DERIVED)
7957 ts = code->ext.alloc.ts;
7958
7959 /* Finding the vtab also publishes the type's symbol. Therefore this
7960 statement is necessary. */
7961 gfc_find_derived_vtab (ts.u.derived);
7962 }
7963 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7964 {
7965 /* Again, make sure the vtab symbol is present when
7966 the module variables are generated. */
7967 gfc_typespec *ts = NULL;
7968 if (code->expr3)
7969 ts = &code->expr3->ts;
7970 else
7971 ts = &code->ext.alloc.ts;
7972
7973 gcc_assert (ts);
7974
7975 /* Finding the vtab also publishes the type's symbol. Therefore this
7976 statement is necessary. */
7977 gfc_find_vtab (ts);
7978 }
7979
7980 if (dimension == 0 && codimension == 0)
7981 goto success;
7982
7983 /* Make sure the last reference node is an array specification. */
7984
7985 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7986 || (dimension && ref2->u.ar.dimen == 0))
7987 {
7988 /* F08:C633. */
7989 if (code->expr3)
7990 {
7991 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7992 "in ALLOCATE statement at %L", &e->where))
7993 goto failure;
7994 if (code->expr3->rank != 0)
7995 *array_alloc_wo_spec = true;
7996 else
7997 {
7998 gfc_error ("Array specification or array-valued SOURCE= "
7999 "expression required in ALLOCATE statement at %L",
8000 &e->where);
8001 goto failure;
8002 }
8003 }
8004 else
8005 {
8006 gfc_error ("Array specification required in ALLOCATE statement "
8007 "at %L", &e->where);
8008 goto failure;
8009 }
8010 }
8011
8012 /* Make sure that the array section reference makes sense in the
8013 context of an ALLOCATE specification. */
8014
8015 ar = &ref2->u.ar;
8016
8017 if (codimension)
8018 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
8019 {
8020 switch (ar->dimen_type[i])
8021 {
8022 case DIMEN_THIS_IMAGE:
8023 gfc_error ("Coarray specification required in ALLOCATE statement "
8024 "at %L", &e->where);
8025 goto failure;
8026
8027 case DIMEN_RANGE:
8028 if (ar->start[i] == 0 || ar->end[i] == 0)
8029 {
8030 /* If ar->stride[i] is NULL, we issued a previous error. */
8031 if (ar->stride[i] == NULL)
8032 gfc_error ("Bad array specification in ALLOCATE statement "
8033 "at %L", &e->where);
8034 goto failure;
8035 }
8036 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8037 {
8038 gfc_error ("Upper cobound is less than lower cobound at %L",
8039 &ar->start[i]->where);
8040 goto failure;
8041 }
8042 break;
8043
8044 case DIMEN_ELEMENT:
8045 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8046 {
8047 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8048 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8049 {
8050 gfc_error ("Upper cobound is less than lower cobound "
8051 "of 1 at %L", &ar->start[i]->where);
8052 goto failure;
8053 }
8054 }
8055 break;
8056
8057 case DIMEN_STAR:
8058 break;
8059
8060 default:
8061 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8062 &e->where);
8063 goto failure;
8064
8065 }
8066 }
8067 for (i = 0; i < ar->dimen; i++)
8068 {
8069 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8070 goto check_symbols;
8071
8072 switch (ar->dimen_type[i])
8073 {
8074 case DIMEN_ELEMENT:
8075 break;
8076
8077 case DIMEN_RANGE:
8078 if (ar->start[i] != NULL
8079 && ar->end[i] != NULL
8080 && ar->stride[i] == NULL)
8081 break;
8082
8083 /* Fall through. */
8084
8085 case DIMEN_UNKNOWN:
8086 case DIMEN_VECTOR:
8087 case DIMEN_STAR:
8088 case DIMEN_THIS_IMAGE:
8089 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8090 &e->where);
8091 goto failure;
8092 }
8093
8094 check_symbols:
8095 for (a = code->ext.alloc.list; a; a = a->next)
8096 {
8097 sym = a->expr->symtree->n.sym;
8098
8099 /* TODO - check derived type components. */
8100 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8101 continue;
8102
8103 if ((ar->start[i] != NULL
8104 && gfc_find_sym_in_expr (sym, ar->start[i]))
8105 || (ar->end[i] != NULL
8106 && gfc_find_sym_in_expr (sym, ar->end[i])))
8107 {
8108 gfc_error ("%qs must not appear in the array specification at "
8109 "%L in the same ALLOCATE statement where it is "
8110 "itself allocated", sym->name, &ar->where);
8111 goto failure;
8112 }
8113 }
8114 }
8115
8116 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8117 {
8118 if (ar->dimen_type[i] == DIMEN_ELEMENT
8119 || ar->dimen_type[i] == DIMEN_RANGE)
8120 {
8121 if (i == (ar->dimen + ar->codimen - 1))
8122 {
8123 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8124 "statement at %L", &e->where);
8125 goto failure;
8126 }
8127 continue;
8128 }
8129
8130 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8131 && ar->stride[i] == NULL)
8132 break;
8133
8134 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8135 &e->where);
8136 goto failure;
8137 }
8138
8139 success:
8140 return true;
8141
8142 failure:
8143 return false;
8144 }
8145
8146
8147 static void
8148 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8149 {
8150 gfc_expr *stat, *errmsg, *pe, *qe;
8151 gfc_alloc *a, *p, *q;
8152
8153 stat = code->expr1;
8154 errmsg = code->expr2;
8155
8156 /* Check the stat variable. */
8157 if (stat)
8158 {
8159 if (!gfc_check_vardef_context (stat, false, false, false,
8160 _("STAT variable")))
8161 goto done_stat;
8162
8163 if (stat->ts.type != BT_INTEGER
8164 || stat->rank > 0)
8165 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8166 "variable", &stat->where);
8167
8168 if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
8169 goto done_stat;
8170
8171 /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
8172 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8173 */
8174 for (p = code->ext.alloc.list; p; p = p->next)
8175 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8176 {
8177 gfc_ref *ref1, *ref2;
8178 bool found = true;
8179
8180 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8181 ref1 = ref1->next, ref2 = ref2->next)
8182 {
8183 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8184 continue;
8185 if (ref1->u.c.component->name != ref2->u.c.component->name)
8186 {
8187 found = false;
8188 break;
8189 }
8190 }
8191
8192 if (found)
8193 {
8194 gfc_error ("Stat-variable at %L shall not be %sd within "
8195 "the same %s statement", &stat->where, fcn, fcn);
8196 break;
8197 }
8198 }
8199 }
8200
8201 done_stat:
8202
8203 /* Check the errmsg variable. */
8204 if (errmsg)
8205 {
8206 if (!stat)
8207 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8208 &errmsg->where);
8209
8210 if (!gfc_check_vardef_context (errmsg, false, false, false,
8211 _("ERRMSG variable")))
8212 goto done_errmsg;
8213
8214 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8215 F18:R930 errmsg-variable is scalar-default-char-variable
8216 F18:R906 default-char-variable is variable
8217 F18:C906 default-char-variable shall be default character. */
8218 if (errmsg->ts.type != BT_CHARACTER
8219 || errmsg->rank > 0
8220 || errmsg->ts.kind != gfc_default_character_kind)
8221 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8222 "variable", &errmsg->where);
8223
8224 if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
8225 goto done_errmsg;
8226
8227 /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
8228 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8229 */
8230 for (p = code->ext.alloc.list; p; p = p->next)
8231 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8232 {
8233 gfc_ref *ref1, *ref2;
8234 bool found = true;
8235
8236 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8237 ref1 = ref1->next, ref2 = ref2->next)
8238 {
8239 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8240 continue;
8241 if (ref1->u.c.component->name != ref2->u.c.component->name)
8242 {
8243 found = false;
8244 break;
8245 }
8246 }
8247
8248 if (found)
8249 {
8250 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8251 "the same %s statement", &errmsg->where, fcn, fcn);
8252 break;
8253 }
8254 }
8255 }
8256
8257 done_errmsg:
8258
8259 /* Check that an allocate-object appears only once in the statement. */
8260
8261 for (p = code->ext.alloc.list; p; p = p->next)
8262 {
8263 pe = p->expr;
8264 for (q = p->next; q; q = q->next)
8265 {
8266 qe = q->expr;
8267 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8268 {
8269 /* This is a potential collision. */
8270 gfc_ref *pr = pe->ref;
8271 gfc_ref *qr = qe->ref;
8272
8273 /* Follow the references until
8274 a) They start to differ, in which case there is no error;
8275 you can deallocate a%b and a%c in a single statement
8276 b) Both of them stop, which is an error
8277 c) One of them stops, which is also an error. */
8278 while (1)
8279 {
8280 if (pr == NULL && qr == NULL)
8281 {
8282 gfc_error ("Allocate-object at %L also appears at %L",
8283 &pe->where, &qe->where);
8284 break;
8285 }
8286 else if (pr != NULL && qr == NULL)
8287 {
8288 gfc_error ("Allocate-object at %L is subobject of"
8289 " object at %L", &pe->where, &qe->where);
8290 break;
8291 }
8292 else if (pr == NULL && qr != NULL)
8293 {
8294 gfc_error ("Allocate-object at %L is subobject of"
8295 " object at %L", &qe->where, &pe->where);
8296 break;
8297 }
8298 /* Here, pr != NULL && qr != NULL */
8299 gcc_assert(pr->type == qr->type);
8300 if (pr->type == REF_ARRAY)
8301 {
8302 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8303 which are legal. */
8304 gcc_assert (qr->type == REF_ARRAY);
8305
8306 if (pr->next && qr->next)
8307 {
8308 int i;
8309 gfc_array_ref *par = &(pr->u.ar);
8310 gfc_array_ref *qar = &(qr->u.ar);
8311
8312 for (i=0; i<par->dimen; i++)
8313 {
8314 if ((par->start[i] != NULL
8315 || qar->start[i] != NULL)
8316 && gfc_dep_compare_expr (par->start[i],
8317 qar->start[i]) != 0)
8318 goto break_label;
8319 }
8320 }
8321 }
8322 else
8323 {
8324 if (pr->u.c.component->name != qr->u.c.component->name)
8325 break;
8326 }
8327
8328 pr = pr->next;
8329 qr = qr->next;
8330 }
8331 break_label:
8332 ;
8333 }
8334 }
8335 }
8336
8337 if (strcmp (fcn, "ALLOCATE") == 0)
8338 {
8339 bool arr_alloc_wo_spec = false;
8340
8341 /* Resolving the expr3 in the loop over all objects to allocate would
8342 execute loop invariant code for each loop item. Therefore do it just
8343 once here. */
8344 if (code->expr3 && code->expr3->mold
8345 && code->expr3->ts.type == BT_DERIVED)
8346 {
8347 /* Default initialization via MOLD (non-polymorphic). */
8348 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8349 if (rhs != NULL)
8350 {
8351 gfc_resolve_expr (rhs);
8352 gfc_free_expr (code->expr3);
8353 code->expr3 = rhs;
8354 }
8355 }
8356 for (a = code->ext.alloc.list; a; a = a->next)
8357 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8358
8359 if (arr_alloc_wo_spec && code->expr3)
8360 {
8361 /* Mark the allocate to have to take the array specification
8362 from the expr3. */
8363 code->ext.alloc.arr_spec_from_expr3 = 1;
8364 }
8365 }
8366 else
8367 {
8368 for (a = code->ext.alloc.list; a; a = a->next)
8369 resolve_deallocate_expr (a->expr);
8370 }
8371 }
8372
8373
8374 /************ SELECT CASE resolution subroutines ************/
8375
8376 /* Callback function for our mergesort variant. Determines interval
8377 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8378 op1 > op2. Assumes we're not dealing with the default case.
8379 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8380 There are nine situations to check. */
8381
8382 static int
8383 compare_cases (const gfc_case *op1, const gfc_case *op2)
8384 {
8385 int retval;
8386
8387 if (op1->low == NULL) /* op1 = (:L) */
8388 {
8389 /* op2 = (:N), so overlap. */
8390 retval = 0;
8391 /* op2 = (M:) or (M:N), L < M */
8392 if (op2->low != NULL
8393 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8394 retval = -1;
8395 }
8396 else if (op1->high == NULL) /* op1 = (K:) */
8397 {
8398 /* op2 = (M:), so overlap. */
8399 retval = 0;
8400 /* op2 = (:N) or (M:N), K > N */
8401 if (op2->high != NULL
8402 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8403 retval = 1;
8404 }
8405 else /* op1 = (K:L) */
8406 {
8407 if (op2->low == NULL) /* op2 = (:N), K > N */
8408 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8409 ? 1 : 0;
8410 else if (op2->high == NULL) /* op2 = (M:), L < M */
8411 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8412 ? -1 : 0;
8413 else /* op2 = (M:N) */
8414 {
8415 retval = 0;
8416 /* L < M */
8417 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8418 retval = -1;
8419 /* K > N */
8420 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8421 retval = 1;
8422 }
8423 }
8424
8425 return retval;
8426 }
8427
8428
8429 /* Merge-sort a double linked case list, detecting overlap in the
8430 process. LIST is the head of the double linked case list before it
8431 is sorted. Returns the head of the sorted list if we don't see any
8432 overlap, or NULL otherwise. */
8433
8434 static gfc_case *
8435 check_case_overlap (gfc_case *list)
8436 {
8437 gfc_case *p, *q, *e, *tail;
8438 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8439
8440 /* If the passed list was empty, return immediately. */
8441 if (!list)
8442 return NULL;
8443
8444 overlap_seen = 0;
8445 insize = 1;
8446
8447 /* Loop unconditionally. The only exit from this loop is a return
8448 statement, when we've finished sorting the case list. */
8449 for (;;)
8450 {
8451 p = list;
8452 list = NULL;
8453 tail = NULL;
8454
8455 /* Count the number of merges we do in this pass. */
8456 nmerges = 0;
8457
8458 /* Loop while there exists a merge to be done. */
8459 while (p)
8460 {
8461 int i;
8462
8463 /* Count this merge. */
8464 nmerges++;
8465
8466 /* Cut the list in two pieces by stepping INSIZE places
8467 forward in the list, starting from P. */
8468 psize = 0;
8469 q = p;
8470 for (i = 0; i < insize; i++)
8471 {
8472 psize++;
8473 q = q->right;
8474 if (!q)
8475 break;
8476 }
8477 qsize = insize;
8478
8479 /* Now we have two lists. Merge them! */
8480 while (psize > 0 || (qsize > 0 && q != NULL))
8481 {
8482 /* See from which the next case to merge comes from. */
8483 if (psize == 0)
8484 {
8485 /* P is empty so the next case must come from Q. */
8486 e = q;
8487 q = q->right;
8488 qsize--;
8489 }
8490 else if (qsize == 0 || q == NULL)
8491 {
8492 /* Q is empty. */
8493 e = p;
8494 p = p->right;
8495 psize--;
8496 }
8497 else
8498 {
8499 cmp = compare_cases (p, q);
8500 if (cmp < 0)
8501 {
8502 /* The whole case range for P is less than the
8503 one for Q. */
8504 e = p;
8505 p = p->right;
8506 psize--;
8507 }
8508 else if (cmp > 0)
8509 {
8510 /* The whole case range for Q is greater than
8511 the case range for P. */
8512 e = q;
8513 q = q->right;
8514 qsize--;
8515 }
8516 else
8517 {
8518 /* The cases overlap, or they are the same
8519 element in the list. Either way, we must
8520 issue an error and get the next case from P. */
8521 /* FIXME: Sort P and Q by line number. */
8522 gfc_error ("CASE label at %L overlaps with CASE "
8523 "label at %L", &p->where, &q->where);
8524 overlap_seen = 1;
8525 e = p;
8526 p = p->right;
8527 psize--;
8528 }
8529 }
8530
8531 /* Add the next element to the merged list. */
8532 if (tail)
8533 tail->right = e;
8534 else
8535 list = e;
8536 e->left = tail;
8537 tail = e;
8538 }
8539
8540 /* P has now stepped INSIZE places along, and so has Q. So
8541 they're the same. */
8542 p = q;
8543 }
8544 tail->right = NULL;
8545
8546 /* If we have done only one merge or none at all, we've
8547 finished sorting the cases. */
8548 if (nmerges <= 1)
8549 {
8550 if (!overlap_seen)
8551 return list;
8552 else
8553 return NULL;
8554 }
8555
8556 /* Otherwise repeat, merging lists twice the size. */
8557 insize *= 2;
8558 }
8559 }
8560
8561
8562 /* Check to see if an expression is suitable for use in a CASE statement.
8563 Makes sure that all case expressions are scalar constants of the same
8564 type. Return false if anything is wrong. */
8565
8566 static bool
8567 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8568 {
8569 if (e == NULL) return true;
8570
8571 if (e->ts.type != case_expr->ts.type)
8572 {
8573 gfc_error ("Expression in CASE statement at %L must be of type %s",
8574 &e->where, gfc_basic_typename (case_expr->ts.type));
8575 return false;
8576 }
8577
8578 /* C805 (R808) For a given case-construct, each case-value shall be of
8579 the same type as case-expr. For character type, length differences
8580 are allowed, but the kind type parameters shall be the same. */
8581
8582 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8583 {
8584 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8585 &e->where, case_expr->ts.kind);
8586 return false;
8587 }
8588
8589 /* Convert the case value kind to that of case expression kind,
8590 if needed */
8591
8592 if (e->ts.kind != case_expr->ts.kind)
8593 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8594
8595 if (e->rank != 0)
8596 {
8597 gfc_error ("Expression in CASE statement at %L must be scalar",
8598 &e->where);
8599 return false;
8600 }
8601
8602 return true;
8603 }
8604
8605
8606 /* Given a completely parsed select statement, we:
8607
8608 - Validate all expressions and code within the SELECT.
8609 - Make sure that the selection expression is not of the wrong type.
8610 - Make sure that no case ranges overlap.
8611 - Eliminate unreachable cases and unreachable code resulting from
8612 removing case labels.
8613
8614 The standard does allow unreachable cases, e.g. CASE (5:3). But
8615 they are a hassle for code generation, and to prevent that, we just
8616 cut them out here. This is not necessary for overlapping cases
8617 because they are illegal and we never even try to generate code.
8618
8619 We have the additional caveat that a SELECT construct could have
8620 been a computed GOTO in the source code. Fortunately we can fairly
8621 easily work around that here: The case_expr for a "real" SELECT CASE
8622 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8623 we have to do is make sure that the case_expr is a scalar integer
8624 expression. */
8625
8626 static void
8627 resolve_select (gfc_code *code, bool select_type)
8628 {
8629 gfc_code *body;
8630 gfc_expr *case_expr;
8631 gfc_case *cp, *default_case, *tail, *head;
8632 int seen_unreachable;
8633 int seen_logical;
8634 int ncases;
8635 bt type;
8636 bool t;
8637
8638 if (code->expr1 == NULL)
8639 {
8640 /* This was actually a computed GOTO statement. */
8641 case_expr = code->expr2;
8642 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8643 gfc_error ("Selection expression in computed GOTO statement "
8644 "at %L must be a scalar integer expression",
8645 &case_expr->where);
8646
8647 /* Further checking is not necessary because this SELECT was built
8648 by the compiler, so it should always be OK. Just move the
8649 case_expr from expr2 to expr so that we can handle computed
8650 GOTOs as normal SELECTs from here on. */
8651 code->expr1 = code->expr2;
8652 code->expr2 = NULL;
8653 return;
8654 }
8655
8656 case_expr = code->expr1;
8657 type = case_expr->ts.type;
8658
8659 /* F08:C830. */
8660 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8661 {
8662 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8663 &case_expr->where, gfc_typename (case_expr));
8664
8665 /* Punt. Going on here just produce more garbage error messages. */
8666 return;
8667 }
8668
8669 /* F08:R842. */
8670 if (!select_type && case_expr->rank != 0)
8671 {
8672 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8673 "expression", &case_expr->where);
8674
8675 /* Punt. */
8676 return;
8677 }
8678
8679 /* Raise a warning if an INTEGER case value exceeds the range of
8680 the case-expr. Later, all expressions will be promoted to the
8681 largest kind of all case-labels. */
8682
8683 if (type == BT_INTEGER)
8684 for (body = code->block; body; body = body->block)
8685 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8686 {
8687 if (cp->low
8688 && gfc_check_integer_range (cp->low->value.integer,
8689 case_expr->ts.kind) != ARITH_OK)
8690 gfc_warning (0, "Expression in CASE statement at %L is "
8691 "not in the range of %s", &cp->low->where,
8692 gfc_typename (case_expr));
8693
8694 if (cp->high
8695 && cp->low != cp->high
8696 && gfc_check_integer_range (cp->high->value.integer,
8697 case_expr->ts.kind) != ARITH_OK)
8698 gfc_warning (0, "Expression in CASE statement at %L is "
8699 "not in the range of %s", &cp->high->where,
8700 gfc_typename (case_expr));
8701 }
8702
8703 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8704 of the SELECT CASE expression and its CASE values. Walk the lists
8705 of case values, and if we find a mismatch, promote case_expr to
8706 the appropriate kind. */
8707
8708 if (type == BT_LOGICAL || type == BT_INTEGER)
8709 {
8710 for (body = code->block; body; body = body->block)
8711 {
8712 /* Walk the case label list. */
8713 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8714 {
8715 /* Intercept the DEFAULT case. It does not have a kind. */
8716 if (cp->low == NULL && cp->high == NULL)
8717 continue;
8718
8719 /* Unreachable case ranges are discarded, so ignore. */
8720 if (cp->low != NULL && cp->high != NULL
8721 && cp->low != cp->high
8722 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8723 continue;
8724
8725 if (cp->low != NULL
8726 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8727 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8728
8729 if (cp->high != NULL
8730 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8731 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8732 }
8733 }
8734 }
8735
8736 /* Assume there is no DEFAULT case. */
8737 default_case = NULL;
8738 head = tail = NULL;
8739 ncases = 0;
8740 seen_logical = 0;
8741
8742 for (body = code->block; body; body = body->block)
8743 {
8744 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8745 t = true;
8746 seen_unreachable = 0;
8747
8748 /* Walk the case label list, making sure that all case labels
8749 are legal. */
8750 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8751 {
8752 /* Count the number of cases in the whole construct. */
8753 ncases++;
8754
8755 /* Intercept the DEFAULT case. */
8756 if (cp->low == NULL && cp->high == NULL)
8757 {
8758 if (default_case != NULL)
8759 {
8760 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8761 "by a second DEFAULT CASE at %L",
8762 &default_case->where, &cp->where);
8763 t = false;
8764 break;
8765 }
8766 else
8767 {
8768 default_case = cp;
8769 continue;
8770 }
8771 }
8772
8773 /* Deal with single value cases and case ranges. Errors are
8774 issued from the validation function. */
8775 if (!validate_case_label_expr (cp->low, case_expr)
8776 || !validate_case_label_expr (cp->high, case_expr))
8777 {
8778 t = false;
8779 break;
8780 }
8781
8782 if (type == BT_LOGICAL
8783 && ((cp->low == NULL || cp->high == NULL)
8784 || cp->low != cp->high))
8785 {
8786 gfc_error ("Logical range in CASE statement at %L is not "
8787 "allowed", &cp->low->where);
8788 t = false;
8789 break;
8790 }
8791
8792 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8793 {
8794 int value;
8795 value = cp->low->value.logical == 0 ? 2 : 1;
8796 if (value & seen_logical)
8797 {
8798 gfc_error ("Constant logical value in CASE statement "
8799 "is repeated at %L",
8800 &cp->low->where);
8801 t = false;
8802 break;
8803 }
8804 seen_logical |= value;
8805 }
8806
8807 if (cp->low != NULL && cp->high != NULL
8808 && cp->low != cp->high
8809 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8810 {
8811 if (warn_surprising)
8812 gfc_warning (OPT_Wsurprising,
8813 "Range specification at %L can never be matched",
8814 &cp->where);
8815
8816 cp->unreachable = 1;
8817 seen_unreachable = 1;
8818 }
8819 else
8820 {
8821 /* If the case range can be matched, it can also overlap with
8822 other cases. To make sure it does not, we put it in a
8823 double linked list here. We sort that with a merge sort
8824 later on to detect any overlapping cases. */
8825 if (!head)
8826 {
8827 head = tail = cp;
8828 head->right = head->left = NULL;
8829 }
8830 else
8831 {
8832 tail->right = cp;
8833 tail->right->left = tail;
8834 tail = tail->right;
8835 tail->right = NULL;
8836 }
8837 }
8838 }
8839
8840 /* It there was a failure in the previous case label, give up
8841 for this case label list. Continue with the next block. */
8842 if (!t)
8843 continue;
8844
8845 /* See if any case labels that are unreachable have been seen.
8846 If so, we eliminate them. This is a bit of a kludge because
8847 the case lists for a single case statement (label) is a
8848 single forward linked lists. */
8849 if (seen_unreachable)
8850 {
8851 /* Advance until the first case in the list is reachable. */
8852 while (body->ext.block.case_list != NULL
8853 && body->ext.block.case_list->unreachable)
8854 {
8855 gfc_case *n = body->ext.block.case_list;
8856 body->ext.block.case_list = body->ext.block.case_list->next;
8857 n->next = NULL;
8858 gfc_free_case_list (n);
8859 }
8860
8861 /* Strip all other unreachable cases. */
8862 if (body->ext.block.case_list)
8863 {
8864 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8865 {
8866 if (cp->next->unreachable)
8867 {
8868 gfc_case *n = cp->next;
8869 cp->next = cp->next->next;
8870 n->next = NULL;
8871 gfc_free_case_list (n);
8872 }
8873 }
8874 }
8875 }
8876 }
8877
8878 /* See if there were overlapping cases. If the check returns NULL,
8879 there was overlap. In that case we don't do anything. If head
8880 is non-NULL, we prepend the DEFAULT case. The sorted list can
8881 then used during code generation for SELECT CASE constructs with
8882 a case expression of a CHARACTER type. */
8883 if (head)
8884 {
8885 head = check_case_overlap (head);
8886
8887 /* Prepend the default_case if it is there. */
8888 if (head != NULL && default_case)
8889 {
8890 default_case->left = NULL;
8891 default_case->right = head;
8892 head->left = default_case;
8893 }
8894 }
8895
8896 /* Eliminate dead blocks that may be the result if we've seen
8897 unreachable case labels for a block. */
8898 for (body = code; body && body->block; body = body->block)
8899 {
8900 if (body->block->ext.block.case_list == NULL)
8901 {
8902 /* Cut the unreachable block from the code chain. */
8903 gfc_code *c = body->block;
8904 body->block = c->block;
8905
8906 /* Kill the dead block, but not the blocks below it. */
8907 c->block = NULL;
8908 gfc_free_statements (c);
8909 }
8910 }
8911
8912 /* More than two cases is legal but insane for logical selects.
8913 Issue a warning for it. */
8914 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8915 gfc_warning (OPT_Wsurprising,
8916 "Logical SELECT CASE block at %L has more that two cases",
8917 &code->loc);
8918 }
8919
8920
8921 /* Check if a derived type is extensible. */
8922
8923 bool
8924 gfc_type_is_extensible (gfc_symbol *sym)
8925 {
8926 return !(sym->attr.is_bind_c || sym->attr.sequence
8927 || (sym->attr.is_class
8928 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8929 }
8930
8931
8932 static void
8933 resolve_types (gfc_namespace *ns);
8934
8935 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8936 correct as well as possibly the array-spec. */
8937
8938 static void
8939 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8940 {
8941 gfc_expr* target;
8942
8943 gcc_assert (sym->assoc);
8944 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8945
8946 /* If this is for SELECT TYPE, the target may not yet be set. In that
8947 case, return. Resolution will be called later manually again when
8948 this is done. */
8949 target = sym->assoc->target;
8950 if (!target)
8951 return;
8952 gcc_assert (!sym->assoc->dangling);
8953
8954 if (resolve_target && !gfc_resolve_expr (target))
8955 return;
8956
8957 /* For variable targets, we get some attributes from the target. */
8958 if (target->expr_type == EXPR_VARIABLE)
8959 {
8960 gfc_symbol *tsym, *dsym;
8961
8962 gcc_assert (target->symtree);
8963 tsym = target->symtree->n.sym;
8964
8965 if (gfc_expr_attr (target).proc_pointer)
8966 {
8967 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8968 tsym->name, &target->where);
8969 return;
8970 }
8971
8972 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8973 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8974 && dsym->attr.flavor == FL_DERIVED)
8975 {
8976 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8977 tsym->name, &target->where);
8978 return;
8979 }
8980
8981 if (tsym->attr.flavor == FL_PROCEDURE)
8982 {
8983 bool is_error = true;
8984 if (tsym->attr.function && tsym->result == tsym)
8985 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8986 if (tsym == ns->proc_name)
8987 {
8988 is_error = false;
8989 break;
8990 }
8991 if (is_error)
8992 {
8993 gfc_error ("Associating entity %qs at %L is a procedure name",
8994 tsym->name, &target->where);
8995 return;
8996 }
8997 }
8998
8999 sym->attr.asynchronous = tsym->attr.asynchronous;
9000 sym->attr.volatile_ = tsym->attr.volatile_;
9001
9002 sym->attr.target = tsym->attr.target
9003 || gfc_expr_attr (target).pointer;
9004 if (is_subref_array (target))
9005 sym->attr.subref_array_pointer = 1;
9006 }
9007 else if (target->ts.type == BT_PROCEDURE)
9008 {
9009 gfc_error ("Associating selector-expression at %L yields a procedure",
9010 &target->where);
9011 return;
9012 }
9013
9014 if (target->expr_type == EXPR_NULL)
9015 {
9016 gfc_error ("Selector at %L cannot be NULL()", &target->where);
9017 return;
9018 }
9019 else if (target->ts.type == BT_UNKNOWN)
9020 {
9021 gfc_error ("Selector at %L has no type", &target->where);
9022 return;
9023 }
9024
9025 /* Get type if this was not already set. Note that it can be
9026 some other type than the target in case this is a SELECT TYPE
9027 selector! So we must not update when the type is already there. */
9028 if (sym->ts.type == BT_UNKNOWN)
9029 sym->ts = target->ts;
9030
9031 gcc_assert (sym->ts.type != BT_UNKNOWN);
9032
9033 /* See if this is a valid association-to-variable. */
9034 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
9035 && !gfc_has_vector_subscript (target));
9036
9037 /* Finally resolve if this is an array or not. */
9038 if (sym->attr.dimension && target->rank == 0)
9039 {
9040 /* primary.c makes the assumption that a reference to an associate
9041 name followed by a left parenthesis is an array reference. */
9042 if (sym->ts.type != BT_CHARACTER)
9043 gfc_error ("Associate-name %qs at %L is used as array",
9044 sym->name, &sym->declared_at);
9045 sym->attr.dimension = 0;
9046 return;
9047 }
9048
9049
9050 /* We cannot deal with class selectors that need temporaries. */
9051 if (target->ts.type == BT_CLASS
9052 && gfc_ref_needs_temporary_p (target->ref))
9053 {
9054 gfc_error ("CLASS selector at %L needs a temporary which is not "
9055 "yet implemented", &target->where);
9056 return;
9057 }
9058
9059 if (target->ts.type == BT_CLASS)
9060 gfc_fix_class_refs (target);
9061
9062 if (target->rank != 0 && !sym->attr.select_rank_temporary)
9063 {
9064 gfc_array_spec *as;
9065 /* The rank may be incorrectly guessed at parsing, therefore make sure
9066 it is corrected now. */
9067 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9068 {
9069 if (!sym->as)
9070 sym->as = gfc_get_array_spec ();
9071 as = sym->as;
9072 as->rank = target->rank;
9073 as->type = AS_DEFERRED;
9074 as->corank = gfc_get_corank (target);
9075 sym->attr.dimension = 1;
9076 if (as->corank != 0)
9077 sym->attr.codimension = 1;
9078 }
9079 else if (sym->ts.type == BT_CLASS
9080 && CLASS_DATA (sym)
9081 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9082 {
9083 if (!CLASS_DATA (sym)->as)
9084 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9085 as = CLASS_DATA (sym)->as;
9086 as->rank = target->rank;
9087 as->type = AS_DEFERRED;
9088 as->corank = gfc_get_corank (target);
9089 CLASS_DATA (sym)->attr.dimension = 1;
9090 if (as->corank != 0)
9091 CLASS_DATA (sym)->attr.codimension = 1;
9092 }
9093 }
9094 else if (!sym->attr.select_rank_temporary)
9095 {
9096 /* target's rank is 0, but the type of the sym is still array valued,
9097 which has to be corrected. */
9098 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9099 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9100 {
9101 gfc_array_spec *as;
9102 symbol_attribute attr;
9103 /* The associated variable's type is still the array type
9104 correct this now. */
9105 gfc_typespec *ts = &target->ts;
9106 gfc_ref *ref;
9107 gfc_component *c;
9108 for (ref = target->ref; ref != NULL; ref = ref->next)
9109 {
9110 switch (ref->type)
9111 {
9112 case REF_COMPONENT:
9113 ts = &ref->u.c.component->ts;
9114 break;
9115 case REF_ARRAY:
9116 if (ts->type == BT_CLASS)
9117 ts = &ts->u.derived->components->ts;
9118 break;
9119 default:
9120 break;
9121 }
9122 }
9123 /* Create a scalar instance of the current class type. Because the
9124 rank of a class array goes into its name, the type has to be
9125 rebuild. The alternative of (re-)setting just the attributes
9126 and as in the current type, destroys the type also in other
9127 places. */
9128 as = NULL;
9129 sym->ts = *ts;
9130 sym->ts.type = BT_CLASS;
9131 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9132 attr.class_ok = 0;
9133 attr.associate_var = 1;
9134 attr.dimension = attr.codimension = 0;
9135 attr.class_pointer = 1;
9136 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9137 gcc_unreachable ();
9138 /* Make sure the _vptr is set. */
9139 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9140 if (c->ts.u.derived == NULL)
9141 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9142 CLASS_DATA (sym)->attr.pointer = 1;
9143 CLASS_DATA (sym)->attr.class_pointer = 1;
9144 gfc_set_sym_referenced (sym->ts.u.derived);
9145 gfc_commit_symbol (sym->ts.u.derived);
9146 /* _vptr now has the _vtab in it, change it to the _vtype. */
9147 if (c->ts.u.derived->attr.vtab)
9148 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9149 c->ts.u.derived->ns->types_resolved = 0;
9150 resolve_types (c->ts.u.derived->ns);
9151 }
9152 }
9153
9154 /* Mark this as an associate variable. */
9155 sym->attr.associate_var = 1;
9156
9157 /* Fix up the type-spec for CHARACTER types. */
9158 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9159 {
9160 if (!sym->ts.u.cl)
9161 sym->ts.u.cl = target->ts.u.cl;
9162
9163 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9164 && target->symtree->n.sym->attr.dummy
9165 && sym->ts.u.cl == target->ts.u.cl)
9166 {
9167 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9168 sym->ts.deferred = 1;
9169 }
9170
9171 if (!sym->ts.u.cl->length
9172 && !sym->ts.deferred
9173 && target->expr_type == EXPR_CONSTANT)
9174 {
9175 sym->ts.u.cl->length =
9176 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9177 target->value.character.length);
9178 }
9179 else if ((!sym->ts.u.cl->length
9180 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9181 && target->expr_type != EXPR_VARIABLE)
9182 {
9183 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9184 sym->ts.deferred = 1;
9185
9186 /* This is reset in trans-stmt.c after the assignment
9187 of the target expression to the associate name. */
9188 sym->attr.allocatable = 1;
9189 }
9190 }
9191
9192 /* If the target is a good class object, so is the associate variable. */
9193 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9194 sym->attr.class_ok = 1;
9195 }
9196
9197
9198 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9199 array reference, where necessary. The symbols are artificial and so
9200 the dimension attribute and arrayspec can also be set. In addition,
9201 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9202 This is corrected here as well.*/
9203
9204 static void
9205 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9206 int rank, gfc_ref *ref)
9207 {
9208 gfc_ref *nref = (*expr1)->ref;
9209 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9210 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9211 (*expr1)->rank = rank;
9212 if (sym1->ts.type == BT_CLASS)
9213 {
9214 if ((*expr1)->ts.type != BT_CLASS)
9215 (*expr1)->ts = sym1->ts;
9216
9217 CLASS_DATA (sym1)->attr.dimension = 1;
9218 if (CLASS_DATA (sym1)->as == NULL && sym2)
9219 CLASS_DATA (sym1)->as
9220 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9221 }
9222 else
9223 {
9224 sym1->attr.dimension = 1;
9225 if (sym1->as == NULL && sym2)
9226 sym1->as = gfc_copy_array_spec (sym2->as);
9227 }
9228
9229 for (; nref; nref = nref->next)
9230 if (nref->next == NULL)
9231 break;
9232
9233 if (ref && nref && nref->type != REF_ARRAY)
9234 nref->next = gfc_copy_ref (ref);
9235 else if (ref && !nref)
9236 (*expr1)->ref = gfc_copy_ref (ref);
9237 }
9238
9239
9240 static gfc_expr *
9241 build_loc_call (gfc_expr *sym_expr)
9242 {
9243 gfc_expr *loc_call;
9244 loc_call = gfc_get_expr ();
9245 loc_call->expr_type = EXPR_FUNCTION;
9246 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9247 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9248 loc_call->symtree->n.sym->attr.intrinsic = 1;
9249 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9250 gfc_commit_symbol (loc_call->symtree->n.sym);
9251 loc_call->ts.type = BT_INTEGER;
9252 loc_call->ts.kind = gfc_index_integer_kind;
9253 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9254 loc_call->value.function.actual = gfc_get_actual_arglist ();
9255 loc_call->value.function.actual->expr = sym_expr;
9256 loc_call->where = sym_expr->where;
9257 return loc_call;
9258 }
9259
9260 /* Resolve a SELECT TYPE statement. */
9261
9262 static void
9263 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9264 {
9265 gfc_symbol *selector_type;
9266 gfc_code *body, *new_st, *if_st, *tail;
9267 gfc_code *class_is = NULL, *default_case = NULL;
9268 gfc_case *c;
9269 gfc_symtree *st;
9270 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
9271 gfc_namespace *ns;
9272 int error = 0;
9273 int rank = 0;
9274 gfc_ref* ref = NULL;
9275 gfc_expr *selector_expr = NULL;
9276
9277 ns = code->ext.block.ns;
9278 gfc_resolve (ns);
9279
9280 /* Check for F03:C813. */
9281 if (code->expr1->ts.type != BT_CLASS
9282 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9283 {
9284 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9285 "at %L", &code->loc);
9286 return;
9287 }
9288
9289 if (!code->expr1->symtree->n.sym->attr.class_ok)
9290 return;
9291
9292 if (code->expr2)
9293 {
9294 gfc_ref *ref2 = NULL;
9295 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9296 if (ref->type == REF_COMPONENT
9297 && ref->u.c.component->ts.type == BT_CLASS)
9298 ref2 = ref;
9299
9300 if (ref2)
9301 {
9302 if (code->expr1->symtree->n.sym->attr.untyped)
9303 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9304 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9305 }
9306 else
9307 {
9308 if (code->expr1->symtree->n.sym->attr.untyped)
9309 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9310 selector_type = CLASS_DATA (code->expr2)
9311 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9312 }
9313
9314 if (code->expr2->rank
9315 && code->expr1->ts.type == BT_CLASS
9316 && CLASS_DATA (code->expr1)->as)
9317 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9318
9319 /* F2008: C803 The selector expression must not be coindexed. */
9320 if (gfc_is_coindexed (code->expr2))
9321 {
9322 gfc_error ("Selector at %L must not be coindexed",
9323 &code->expr2->where);
9324 return;
9325 }
9326
9327 }
9328 else
9329 {
9330 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9331
9332 if (gfc_is_coindexed (code->expr1))
9333 {
9334 gfc_error ("Selector at %L must not be coindexed",
9335 &code->expr1->where);
9336 return;
9337 }
9338 }
9339
9340 /* Loop over TYPE IS / CLASS IS cases. */
9341 for (body = code->block; body; body = body->block)
9342 {
9343 c = body->ext.block.case_list;
9344
9345 if (!error)
9346 {
9347 /* Check for repeated cases. */
9348 for (tail = code->block; tail; tail = tail->block)
9349 {
9350 gfc_case *d = tail->ext.block.case_list;
9351 if (tail == body)
9352 break;
9353
9354 if (c->ts.type == d->ts.type
9355 && ((c->ts.type == BT_DERIVED
9356 && c->ts.u.derived && d->ts.u.derived
9357 && !strcmp (c->ts.u.derived->name,
9358 d->ts.u.derived->name))
9359 || c->ts.type == BT_UNKNOWN
9360 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9361 && c->ts.kind == d->ts.kind)))
9362 {
9363 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9364 &c->where, &d->where);
9365 return;
9366 }
9367 }
9368 }
9369
9370 /* Check F03:C815. */
9371 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9372 && !selector_type->attr.unlimited_polymorphic
9373 && !gfc_type_is_extensible (c->ts.u.derived))
9374 {
9375 gfc_error ("Derived type %qs at %L must be extensible",
9376 c->ts.u.derived->name, &c->where);
9377 error++;
9378 continue;
9379 }
9380
9381 /* Check F03:C816. */
9382 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9383 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9384 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9385 {
9386 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9387 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9388 c->ts.u.derived->name, &c->where, selector_type->name);
9389 else
9390 gfc_error ("Unexpected intrinsic type %qs at %L",
9391 gfc_basic_typename (c->ts.type), &c->where);
9392 error++;
9393 continue;
9394 }
9395
9396 /* Check F03:C814. */
9397 if (c->ts.type == BT_CHARACTER
9398 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9399 {
9400 gfc_error ("The type-spec at %L shall specify that each length "
9401 "type parameter is assumed", &c->where);
9402 error++;
9403 continue;
9404 }
9405
9406 /* Intercept the DEFAULT case. */
9407 if (c->ts.type == BT_UNKNOWN)
9408 {
9409 /* Check F03:C818. */
9410 if (default_case)
9411 {
9412 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9413 "by a second DEFAULT CASE at %L",
9414 &default_case->ext.block.case_list->where, &c->where);
9415 error++;
9416 continue;
9417 }
9418
9419 default_case = body;
9420 }
9421 }
9422
9423 if (error > 0)
9424 return;
9425
9426 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9427 target if present. If there are any EXIT statements referring to the
9428 SELECT TYPE construct, this is no problem because the gfc_code
9429 reference stays the same and EXIT is equally possible from the BLOCK
9430 it is changed to. */
9431 code->op = EXEC_BLOCK;
9432 if (code->expr2)
9433 {
9434 gfc_association_list* assoc;
9435
9436 assoc = gfc_get_association_list ();
9437 assoc->st = code->expr1->symtree;
9438 assoc->target = gfc_copy_expr (code->expr2);
9439 assoc->target->where = code->expr2->where;
9440 /* assoc->variable will be set by resolve_assoc_var. */
9441
9442 code->ext.block.assoc = assoc;
9443 code->expr1->symtree->n.sym->assoc = assoc;
9444
9445 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9446 }
9447 else
9448 code->ext.block.assoc = NULL;
9449
9450 /* Ensure that the selector rank and arrayspec are available to
9451 correct expressions in which they might be missing. */
9452 if (code->expr2 && code->expr2->rank)
9453 {
9454 rank = code->expr2->rank;
9455 for (ref = code->expr2->ref; ref; ref = ref->next)
9456 if (ref->next == NULL)
9457 break;
9458 if (ref && ref->type == REF_ARRAY)
9459 ref = gfc_copy_ref (ref);
9460
9461 /* Fixup expr1 if necessary. */
9462 if (rank)
9463 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9464 }
9465 else if (code->expr1->rank)
9466 {
9467 rank = code->expr1->rank;
9468 for (ref = code->expr1->ref; ref; ref = ref->next)
9469 if (ref->next == NULL)
9470 break;
9471 if (ref && ref->type == REF_ARRAY)
9472 ref = gfc_copy_ref (ref);
9473 }
9474
9475 /* Add EXEC_SELECT to switch on type. */
9476 new_st = gfc_get_code (code->op);
9477 new_st->expr1 = code->expr1;
9478 new_st->expr2 = code->expr2;
9479 new_st->block = code->block;
9480 code->expr1 = code->expr2 = NULL;
9481 code->block = NULL;
9482 if (!ns->code)
9483 ns->code = new_st;
9484 else
9485 ns->code->next = new_st;
9486 code = new_st;
9487 code->op = EXEC_SELECT_TYPE;
9488
9489 /* Use the intrinsic LOC function to generate an integer expression
9490 for the vtable of the selector. Note that the rank of the selector
9491 expression has to be set to zero. */
9492 gfc_add_vptr_component (code->expr1);
9493 code->expr1->rank = 0;
9494 code->expr1 = build_loc_call (code->expr1);
9495 selector_expr = code->expr1->value.function.actual->expr;
9496
9497 /* Loop over TYPE IS / CLASS IS cases. */
9498 for (body = code->block; body; body = body->block)
9499 {
9500 gfc_symbol *vtab;
9501 gfc_expr *e;
9502 c = body->ext.block.case_list;
9503
9504 /* Generate an index integer expression for address of the
9505 TYPE/CLASS vtable and store it in c->low. The hash expression
9506 is stored in c->high and is used to resolve intrinsic cases. */
9507 if (c->ts.type != BT_UNKNOWN)
9508 {
9509 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9510 {
9511 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9512 gcc_assert (vtab);
9513 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9514 c->ts.u.derived->hash_value);
9515 }
9516 else
9517 {
9518 vtab = gfc_find_vtab (&c->ts);
9519 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9520 e = CLASS_DATA (vtab)->initializer;
9521 c->high = gfc_copy_expr (e);
9522 if (c->high->ts.kind != gfc_integer_4_kind)
9523 {
9524 gfc_typespec ts;
9525 ts.kind = gfc_integer_4_kind;
9526 ts.type = BT_INTEGER;
9527 gfc_convert_type_warn (c->high, &ts, 2, 0);
9528 }
9529 }
9530
9531 e = gfc_lval_expr_from_sym (vtab);
9532 c->low = build_loc_call (e);
9533 }
9534 else
9535 continue;
9536
9537 /* Associate temporary to selector. This should only be done
9538 when this case is actually true, so build a new ASSOCIATE
9539 that does precisely this here (instead of using the
9540 'global' one). */
9541
9542 if (c->ts.type == BT_CLASS)
9543 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9544 else if (c->ts.type == BT_DERIVED)
9545 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9546 else if (c->ts.type == BT_CHARACTER)
9547 {
9548 HOST_WIDE_INT charlen = 0;
9549 if (c->ts.u.cl && c->ts.u.cl->length
9550 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9551 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9552 snprintf (name, sizeof (name),
9553 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9554 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9555 }
9556 else
9557 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9558 c->ts.kind);
9559
9560 st = gfc_find_symtree (ns->sym_root, name);
9561 gcc_assert (st->n.sym->assoc);
9562 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9563 st->n.sym->assoc->target->where = selector_expr->where;
9564 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9565 {
9566 gfc_add_data_component (st->n.sym->assoc->target);
9567 /* Fixup the target expression if necessary. */
9568 if (rank)
9569 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9570 }
9571
9572 new_st = gfc_get_code (EXEC_BLOCK);
9573 new_st->ext.block.ns = gfc_build_block_ns (ns);
9574 new_st->ext.block.ns->code = body->next;
9575 body->next = new_st;
9576
9577 /* Chain in the new list only if it is marked as dangling. Otherwise
9578 there is a CASE label overlap and this is already used. Just ignore,
9579 the error is diagnosed elsewhere. */
9580 if (st->n.sym->assoc->dangling)
9581 {
9582 new_st->ext.block.assoc = st->n.sym->assoc;
9583 st->n.sym->assoc->dangling = 0;
9584 }
9585
9586 resolve_assoc_var (st->n.sym, false);
9587 }
9588
9589 /* Take out CLASS IS cases for separate treatment. */
9590 body = code;
9591 while (body && body->block)
9592 {
9593 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9594 {
9595 /* Add to class_is list. */
9596 if (class_is == NULL)
9597 {
9598 class_is = body->block;
9599 tail = class_is;
9600 }
9601 else
9602 {
9603 for (tail = class_is; tail->block; tail = tail->block) ;
9604 tail->block = body->block;
9605 tail = tail->block;
9606 }
9607 /* Remove from EXEC_SELECT list. */
9608 body->block = body->block->block;
9609 tail->block = NULL;
9610 }
9611 else
9612 body = body->block;
9613 }
9614
9615 if (class_is)
9616 {
9617 gfc_symbol *vtab;
9618
9619 if (!default_case)
9620 {
9621 /* Add a default case to hold the CLASS IS cases. */
9622 for (tail = code; tail->block; tail = tail->block) ;
9623 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9624 tail = tail->block;
9625 tail->ext.block.case_list = gfc_get_case ();
9626 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9627 tail->next = NULL;
9628 default_case = tail;
9629 }
9630
9631 /* More than one CLASS IS block? */
9632 if (class_is->block)
9633 {
9634 gfc_code **c1,*c2;
9635 bool swapped;
9636 /* Sort CLASS IS blocks by extension level. */
9637 do
9638 {
9639 swapped = false;
9640 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9641 {
9642 c2 = (*c1)->block;
9643 /* F03:C817 (check for doubles). */
9644 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9645 == c2->ext.block.case_list->ts.u.derived->hash_value)
9646 {
9647 gfc_error ("Double CLASS IS block in SELECT TYPE "
9648 "statement at %L",
9649 &c2->ext.block.case_list->where);
9650 return;
9651 }
9652 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9653 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9654 {
9655 /* Swap. */
9656 (*c1)->block = c2->block;
9657 c2->block = *c1;
9658 *c1 = c2;
9659 swapped = true;
9660 }
9661 }
9662 }
9663 while (swapped);
9664 }
9665
9666 /* Generate IF chain. */
9667 if_st = gfc_get_code (EXEC_IF);
9668 new_st = if_st;
9669 for (body = class_is; body; body = body->block)
9670 {
9671 new_st->block = gfc_get_code (EXEC_IF);
9672 new_st = new_st->block;
9673 /* Set up IF condition: Call _gfortran_is_extension_of. */
9674 new_st->expr1 = gfc_get_expr ();
9675 new_st->expr1->expr_type = EXPR_FUNCTION;
9676 new_st->expr1->ts.type = BT_LOGICAL;
9677 new_st->expr1->ts.kind = 4;
9678 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9679 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9680 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9681 /* Set up arguments. */
9682 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9683 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9684 new_st->expr1->value.function.actual->expr->where = code->loc;
9685 new_st->expr1->where = code->loc;
9686 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9687 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9688 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9689 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9690 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9691 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9692 /* Set up types in formal arg list. */
9693 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
9694 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
9695 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
9696 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
9697
9698 new_st->next = body->next;
9699 }
9700 if (default_case->next)
9701 {
9702 new_st->block = gfc_get_code (EXEC_IF);
9703 new_st = new_st->block;
9704 new_st->next = default_case->next;
9705 }
9706
9707 /* Replace CLASS DEFAULT code by the IF chain. */
9708 default_case->next = if_st;
9709 }
9710
9711 /* Resolve the internal code. This cannot be done earlier because
9712 it requires that the sym->assoc of selectors is set already. */
9713 gfc_current_ns = ns;
9714 gfc_resolve_blocks (code->block, gfc_current_ns);
9715 gfc_current_ns = old_ns;
9716
9717 if (ref)
9718 free (ref);
9719 }
9720
9721
9722 /* Resolve a SELECT RANK statement. */
9723
9724 static void
9725 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9726 {
9727 gfc_namespace *ns;
9728 gfc_code *body, *new_st, *tail;
9729 gfc_case *c;
9730 char tname[GFC_MAX_SYMBOL_LEN + 7];
9731 char name[2 * GFC_MAX_SYMBOL_LEN];
9732 gfc_symtree *st;
9733 gfc_expr *selector_expr = NULL;
9734 int case_value;
9735 HOST_WIDE_INT charlen = 0;
9736
9737 ns = code->ext.block.ns;
9738 gfc_resolve (ns);
9739
9740 code->op = EXEC_BLOCK;
9741 if (code->expr2)
9742 {
9743 gfc_association_list* assoc;
9744
9745 assoc = gfc_get_association_list ();
9746 assoc->st = code->expr1->symtree;
9747 assoc->target = gfc_copy_expr (code->expr2);
9748 assoc->target->where = code->expr2->where;
9749 /* assoc->variable will be set by resolve_assoc_var. */
9750
9751 code->ext.block.assoc = assoc;
9752 code->expr1->symtree->n.sym->assoc = assoc;
9753
9754 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9755 }
9756 else
9757 code->ext.block.assoc = NULL;
9758
9759 /* Loop over RANK cases. Note that returning on the errors causes a
9760 cascade of further errors because the case blocks do not compile
9761 correctly. */
9762 for (body = code->block; body; body = body->block)
9763 {
9764 c = body->ext.block.case_list;
9765 if (c->low)
9766 case_value = (int) mpz_get_si (c->low->value.integer);
9767 else
9768 case_value = -2;
9769
9770 /* Check for repeated cases. */
9771 for (tail = code->block; tail; tail = tail->block)
9772 {
9773 gfc_case *d = tail->ext.block.case_list;
9774 int case_value2;
9775
9776 if (tail == body)
9777 break;
9778
9779 /* Check F2018: C1153. */
9780 if (!c->low && !d->low)
9781 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9782 &c->where, &d->where);
9783
9784 if (!c->low || !d->low)
9785 continue;
9786
9787 /* Check F2018: C1153. */
9788 case_value2 = (int) mpz_get_si (d->low->value.integer);
9789 if ((case_value == case_value2) && case_value == -1)
9790 gfc_error ("RANK (*) at %L is repeated at %L",
9791 &c->where, &d->where);
9792 else if (case_value == case_value2)
9793 gfc_error ("RANK (%i) at %L is repeated at %L",
9794 case_value, &c->where, &d->where);
9795 }
9796
9797 if (!c->low)
9798 continue;
9799
9800 /* Check F2018: C1155. */
9801 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9802 || gfc_expr_attr (code->expr1).pointer))
9803 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9804 "allocatable selector at %L", &c->where, &code->expr1->where);
9805
9806 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9807 || gfc_expr_attr (code->expr1).pointer))
9808 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9809 "allocatable selector at %L", &c->where, &code->expr1->where);
9810 }
9811
9812 /* Add EXEC_SELECT to switch on rank. */
9813 new_st = gfc_get_code (code->op);
9814 new_st->expr1 = code->expr1;
9815 new_st->expr2 = code->expr2;
9816 new_st->block = code->block;
9817 code->expr1 = code->expr2 = NULL;
9818 code->block = NULL;
9819 if (!ns->code)
9820 ns->code = new_st;
9821 else
9822 ns->code->next = new_st;
9823 code = new_st;
9824 code->op = EXEC_SELECT_RANK;
9825
9826 selector_expr = code->expr1;
9827
9828 /* Loop over SELECT RANK cases. */
9829 for (body = code->block; body; body = body->block)
9830 {
9831 c = body->ext.block.case_list;
9832 int case_value;
9833
9834 /* Pass on the default case. */
9835 if (c->low == NULL)
9836 continue;
9837
9838 /* Associate temporary to selector. This should only be done
9839 when this case is actually true, so build a new ASSOCIATE
9840 that does precisely this here (instead of using the
9841 'global' one). */
9842 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9843 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9844 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9845
9846 if (c->ts.type == BT_CLASS)
9847 sprintf (tname, "class_%s", c->ts.u.derived->name);
9848 else if (c->ts.type == BT_DERIVED)
9849 sprintf (tname, "type_%s", c->ts.u.derived->name);
9850 else if (c->ts.type != BT_CHARACTER)
9851 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9852 else
9853 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9854 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9855
9856 case_value = (int) mpz_get_si (c->low->value.integer);
9857 if (case_value >= 0)
9858 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9859 else
9860 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9861
9862 st = gfc_find_symtree (ns->sym_root, name);
9863 gcc_assert (st->n.sym->assoc);
9864
9865 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9866 st->n.sym->assoc->target->where = selector_expr->where;
9867
9868 new_st = gfc_get_code (EXEC_BLOCK);
9869 new_st->ext.block.ns = gfc_build_block_ns (ns);
9870 new_st->ext.block.ns->code = body->next;
9871 body->next = new_st;
9872
9873 /* Chain in the new list only if it is marked as dangling. Otherwise
9874 there is a CASE label overlap and this is already used. Just ignore,
9875 the error is diagnosed elsewhere. */
9876 if (st->n.sym->assoc->dangling)
9877 {
9878 new_st->ext.block.assoc = st->n.sym->assoc;
9879 st->n.sym->assoc->dangling = 0;
9880 }
9881
9882 resolve_assoc_var (st->n.sym, false);
9883 }
9884
9885 gfc_current_ns = ns;
9886 gfc_resolve_blocks (code->block, gfc_current_ns);
9887 gfc_current_ns = old_ns;
9888 }
9889
9890
9891 /* Resolve a transfer statement. This is making sure that:
9892 -- a derived type being transferred has only non-pointer components
9893 -- a derived type being transferred doesn't have private components, unless
9894 it's being transferred from the module where the type was defined
9895 -- we're not trying to transfer a whole assumed size array. */
9896
9897 static void
9898 resolve_transfer (gfc_code *code)
9899 {
9900 gfc_symbol *sym, *derived;
9901 gfc_ref *ref;
9902 gfc_expr *exp;
9903 bool write = false;
9904 bool formatted = false;
9905 gfc_dt *dt = code->ext.dt;
9906 gfc_symbol *dtio_sub = NULL;
9907
9908 exp = code->expr1;
9909
9910 while (exp != NULL && exp->expr_type == EXPR_OP
9911 && exp->value.op.op == INTRINSIC_PARENTHESES)
9912 exp = exp->value.op.op1;
9913
9914 if (exp && exp->expr_type == EXPR_NULL
9915 && code->ext.dt)
9916 {
9917 gfc_error ("Invalid context for NULL () intrinsic at %L",
9918 &exp->where);
9919 return;
9920 }
9921
9922 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9923 && exp->expr_type != EXPR_FUNCTION
9924 && exp->expr_type != EXPR_STRUCTURE))
9925 return;
9926
9927 /* If we are reading, the variable will be changed. Note that
9928 code->ext.dt may be NULL if the TRANSFER is related to
9929 an INQUIRE statement -- but in this case, we are not reading, either. */
9930 if (dt && dt->dt_io_kind->value.iokind == M_READ
9931 && !gfc_check_vardef_context (exp, false, false, false,
9932 _("item in READ")))
9933 return;
9934
9935 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9936 || exp->expr_type == EXPR_FUNCTION
9937 ? &exp->ts : &exp->symtree->n.sym->ts;
9938
9939 /* Go to actual component transferred. */
9940 for (ref = exp->ref; ref; ref = ref->next)
9941 if (ref->type == REF_COMPONENT)
9942 ts = &ref->u.c.component->ts;
9943
9944 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9945 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9946 {
9947 derived = ts->u.derived;
9948
9949 /* Determine when to use the formatted DTIO procedure. */
9950 if (dt && (dt->format_expr || dt->format_label))
9951 formatted = true;
9952
9953 write = dt->dt_io_kind->value.iokind == M_WRITE
9954 || dt->dt_io_kind->value.iokind == M_PRINT;
9955 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9956
9957 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9958 {
9959 dt->udtio = exp;
9960 sym = exp->symtree->n.sym->ns->proc_name;
9961 /* Check to see if this is a nested DTIO call, with the
9962 dummy as the io-list object. */
9963 if (sym && sym == dtio_sub && sym->formal
9964 && sym->formal->sym == exp->symtree->n.sym
9965 && exp->ref == NULL)
9966 {
9967 if (!sym->attr.recursive)
9968 {
9969 gfc_error ("DTIO %s procedure at %L must be recursive",
9970 sym->name, &sym->declared_at);
9971 return;
9972 }
9973 }
9974 }
9975 }
9976
9977 if (ts->type == BT_CLASS && dtio_sub == NULL)
9978 {
9979 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9980 "it is processed by a defined input/output procedure",
9981 &code->loc);
9982 return;
9983 }
9984
9985 if (ts->type == BT_DERIVED)
9986 {
9987 /* Check that transferred derived type doesn't contain POINTER
9988 components unless it is processed by a defined input/output
9989 procedure". */
9990 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9991 {
9992 gfc_error ("Data transfer element at %L cannot have POINTER "
9993 "components unless it is processed by a defined "
9994 "input/output procedure", &code->loc);
9995 return;
9996 }
9997
9998 /* F08:C935. */
9999 if (ts->u.derived->attr.proc_pointer_comp)
10000 {
10001 gfc_error ("Data transfer element at %L cannot have "
10002 "procedure pointer components", &code->loc);
10003 return;
10004 }
10005
10006 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
10007 {
10008 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
10009 "components unless it is processed by a defined "
10010 "input/output procedure", &code->loc);
10011 return;
10012 }
10013
10014 /* C_PTR and C_FUNPTR have private components which means they cannot
10015 be printed. However, if -std=gnu and not -pedantic, allow
10016 the component to be printed to help debugging. */
10017 if (ts->u.derived->ts.f90_type == BT_VOID)
10018 {
10019 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
10020 "cannot have PRIVATE components", &code->loc))
10021 return;
10022 }
10023 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
10024 {
10025 gfc_error ("Data transfer element at %L cannot have "
10026 "PRIVATE components unless it is processed by "
10027 "a defined input/output procedure", &code->loc);
10028 return;
10029 }
10030 }
10031
10032 if (exp->expr_type == EXPR_STRUCTURE)
10033 return;
10034
10035 sym = exp->symtree->n.sym;
10036
10037 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
10038 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
10039 {
10040 gfc_error ("Data transfer element at %L cannot be a full reference to "
10041 "an assumed-size array", &code->loc);
10042 return;
10043 }
10044 }
10045
10046
10047 /*********** Toplevel code resolution subroutines ***********/
10048
10049 /* Find the set of labels that are reachable from this block. We also
10050 record the last statement in each block. */
10051
10052 static void
10053 find_reachable_labels (gfc_code *block)
10054 {
10055 gfc_code *c;
10056
10057 if (!block)
10058 return;
10059
10060 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10061
10062 /* Collect labels in this block. We don't keep those corresponding
10063 to END {IF|SELECT}, these are checked in resolve_branch by going
10064 up through the code_stack. */
10065 for (c = block; c; c = c->next)
10066 {
10067 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10068 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10069 }
10070
10071 /* Merge with labels from parent block. */
10072 if (cs_base->prev)
10073 {
10074 gcc_assert (cs_base->prev->reachable_labels);
10075 bitmap_ior_into (cs_base->reachable_labels,
10076 cs_base->prev->reachable_labels);
10077 }
10078 }
10079
10080
10081 static void
10082 resolve_lock_unlock_event (gfc_code *code)
10083 {
10084 if (code->expr1->expr_type == EXPR_FUNCTION
10085 && code->expr1->value.function.isym
10086 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10087 remove_caf_get_intrinsic (code->expr1);
10088
10089 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10090 && (code->expr1->ts.type != BT_DERIVED
10091 || code->expr1->expr_type != EXPR_VARIABLE
10092 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10093 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10094 || code->expr1->rank != 0
10095 || (!gfc_is_coarray (code->expr1) &&
10096 !gfc_is_coindexed (code->expr1))))
10097 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10098 &code->expr1->where);
10099 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10100 && (code->expr1->ts.type != BT_DERIVED
10101 || code->expr1->expr_type != EXPR_VARIABLE
10102 || code->expr1->ts.u.derived->from_intmod
10103 != INTMOD_ISO_FORTRAN_ENV
10104 || code->expr1->ts.u.derived->intmod_sym_id
10105 != ISOFORTRAN_EVENT_TYPE
10106 || code->expr1->rank != 0))
10107 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10108 &code->expr1->where);
10109 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10110 && !gfc_is_coindexed (code->expr1))
10111 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10112 &code->expr1->where);
10113 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10114 gfc_error ("Event variable argument at %L must be a coarray but not "
10115 "coindexed", &code->expr1->where);
10116
10117 /* Check STAT. */
10118 if (code->expr2
10119 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10120 || code->expr2->expr_type != EXPR_VARIABLE))
10121 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10122 &code->expr2->where);
10123
10124 if (code->expr2
10125 && !gfc_check_vardef_context (code->expr2, false, false, false,
10126 _("STAT variable")))
10127 return;
10128
10129 /* Check ERRMSG. */
10130 if (code->expr3
10131 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10132 || code->expr3->expr_type != EXPR_VARIABLE))
10133 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10134 &code->expr3->where);
10135
10136 if (code->expr3
10137 && !gfc_check_vardef_context (code->expr3, false, false, false,
10138 _("ERRMSG variable")))
10139 return;
10140
10141 /* Check for LOCK the ACQUIRED_LOCK. */
10142 if (code->op != EXEC_EVENT_WAIT && code->expr4
10143 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10144 || code->expr4->expr_type != EXPR_VARIABLE))
10145 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10146 "variable", &code->expr4->where);
10147
10148 if (code->op != EXEC_EVENT_WAIT && code->expr4
10149 && !gfc_check_vardef_context (code->expr4, false, false, false,
10150 _("ACQUIRED_LOCK variable")))
10151 return;
10152
10153 /* Check for EVENT WAIT the UNTIL_COUNT. */
10154 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10155 {
10156 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10157 || code->expr4->rank != 0)
10158 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10159 "expression", &code->expr4->where);
10160 }
10161 }
10162
10163
10164 static void
10165 resolve_critical (gfc_code *code)
10166 {
10167 gfc_symtree *symtree;
10168 gfc_symbol *lock_type;
10169 char name[GFC_MAX_SYMBOL_LEN];
10170 static int serial = 0;
10171
10172 if (flag_coarray != GFC_FCOARRAY_LIB)
10173 return;
10174
10175 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10176 GFC_PREFIX ("lock_type"));
10177 if (symtree)
10178 lock_type = symtree->n.sym;
10179 else
10180 {
10181 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10182 false) != 0)
10183 gcc_unreachable ();
10184 lock_type = symtree->n.sym;
10185 lock_type->attr.flavor = FL_DERIVED;
10186 lock_type->attr.zero_comp = 1;
10187 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10188 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10189 }
10190
10191 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10192 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10193 gcc_unreachable ();
10194
10195 code->resolved_sym = symtree->n.sym;
10196 symtree->n.sym->attr.flavor = FL_VARIABLE;
10197 symtree->n.sym->attr.referenced = 1;
10198 symtree->n.sym->attr.artificial = 1;
10199 symtree->n.sym->attr.codimension = 1;
10200 symtree->n.sym->ts.type = BT_DERIVED;
10201 symtree->n.sym->ts.u.derived = lock_type;
10202 symtree->n.sym->as = gfc_get_array_spec ();
10203 symtree->n.sym->as->corank = 1;
10204 symtree->n.sym->as->type = AS_EXPLICIT;
10205 symtree->n.sym->as->cotype = AS_EXPLICIT;
10206 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10207 NULL, 1);
10208 gfc_commit_symbols();
10209 }
10210
10211
10212 static void
10213 resolve_sync (gfc_code *code)
10214 {
10215 /* Check imageset. The * case matches expr1 == NULL. */
10216 if (code->expr1)
10217 {
10218 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10219 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10220 "INTEGER expression", &code->expr1->where);
10221 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10222 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10223 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10224 &code->expr1->where);
10225 else if (code->expr1->expr_type == EXPR_ARRAY
10226 && gfc_simplify_expr (code->expr1, 0))
10227 {
10228 gfc_constructor *cons;
10229 cons = gfc_constructor_first (code->expr1->value.constructor);
10230 for (; cons; cons = gfc_constructor_next (cons))
10231 if (cons->expr->expr_type == EXPR_CONSTANT
10232 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10233 gfc_error ("Imageset argument at %L must between 1 and "
10234 "num_images()", &cons->expr->where);
10235 }
10236 }
10237
10238 /* Check STAT. */
10239 gfc_resolve_expr (code->expr2);
10240 if (code->expr2
10241 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10242 || code->expr2->expr_type != EXPR_VARIABLE))
10243 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10244 &code->expr2->where);
10245
10246 /* Check ERRMSG. */
10247 gfc_resolve_expr (code->expr3);
10248 if (code->expr3
10249 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10250 || code->expr3->expr_type != EXPR_VARIABLE))
10251 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10252 &code->expr3->where);
10253 }
10254
10255
10256 /* Given a branch to a label, see if the branch is conforming.
10257 The code node describes where the branch is located. */
10258
10259 static void
10260 resolve_branch (gfc_st_label *label, gfc_code *code)
10261 {
10262 code_stack *stack;
10263
10264 if (label == NULL)
10265 return;
10266
10267 /* Step one: is this a valid branching target? */
10268
10269 if (label->defined == ST_LABEL_UNKNOWN)
10270 {
10271 gfc_error ("Label %d referenced at %L is never defined", label->value,
10272 &code->loc);
10273 return;
10274 }
10275
10276 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10277 {
10278 gfc_error ("Statement at %L is not a valid branch target statement "
10279 "for the branch statement at %L", &label->where, &code->loc);
10280 return;
10281 }
10282
10283 /* Step two: make sure this branch is not a branch to itself ;-) */
10284
10285 if (code->here == label)
10286 {
10287 gfc_warning (0,
10288 "Branch at %L may result in an infinite loop", &code->loc);
10289 return;
10290 }
10291
10292 /* Step three: See if the label is in the same block as the
10293 branching statement. The hard work has been done by setting up
10294 the bitmap reachable_labels. */
10295
10296 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10297 {
10298 /* Check now whether there is a CRITICAL construct; if so, check
10299 whether the label is still visible outside of the CRITICAL block,
10300 which is invalid. */
10301 for (stack = cs_base; stack; stack = stack->prev)
10302 {
10303 if (stack->current->op == EXEC_CRITICAL
10304 && bitmap_bit_p (stack->reachable_labels, label->value))
10305 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10306 "label at %L", &code->loc, &label->where);
10307 else if (stack->current->op == EXEC_DO_CONCURRENT
10308 && bitmap_bit_p (stack->reachable_labels, label->value))
10309 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10310 "for label at %L", &code->loc, &label->where);
10311 }
10312
10313 return;
10314 }
10315
10316 /* Step four: If we haven't found the label in the bitmap, it may
10317 still be the label of the END of the enclosing block, in which
10318 case we find it by going up the code_stack. */
10319
10320 for (stack = cs_base; stack; stack = stack->prev)
10321 {
10322 if (stack->current->next && stack->current->next->here == label)
10323 break;
10324 if (stack->current->op == EXEC_CRITICAL)
10325 {
10326 /* Note: A label at END CRITICAL does not leave the CRITICAL
10327 construct as END CRITICAL is still part of it. */
10328 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10329 " at %L", &code->loc, &label->where);
10330 return;
10331 }
10332 else if (stack->current->op == EXEC_DO_CONCURRENT)
10333 {
10334 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10335 "label at %L", &code->loc, &label->where);
10336 return;
10337 }
10338 }
10339
10340 if (stack)
10341 {
10342 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10343 return;
10344 }
10345
10346 /* The label is not in an enclosing block, so illegal. This was
10347 allowed in Fortran 66, so we allow it as extension. No
10348 further checks are necessary in this case. */
10349 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10350 "as the GOTO statement at %L", &label->where,
10351 &code->loc);
10352 return;
10353 }
10354
10355
10356 /* Check whether EXPR1 has the same shape as EXPR2. */
10357
10358 static bool
10359 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10360 {
10361 mpz_t shape[GFC_MAX_DIMENSIONS];
10362 mpz_t shape2[GFC_MAX_DIMENSIONS];
10363 bool result = false;
10364 int i;
10365
10366 /* Compare the rank. */
10367 if (expr1->rank != expr2->rank)
10368 return result;
10369
10370 /* Compare the size of each dimension. */
10371 for (i=0; i<expr1->rank; i++)
10372 {
10373 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10374 goto ignore;
10375
10376 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10377 goto ignore;
10378
10379 if (mpz_cmp (shape[i], shape2[i]))
10380 goto over;
10381 }
10382
10383 /* When either of the two expression is an assumed size array, we
10384 ignore the comparison of dimension sizes. */
10385 ignore:
10386 result = true;
10387
10388 over:
10389 gfc_clear_shape (shape, i);
10390 gfc_clear_shape (shape2, i);
10391 return result;
10392 }
10393
10394
10395 /* Check whether a WHERE assignment target or a WHERE mask expression
10396 has the same shape as the outmost WHERE mask expression. */
10397
10398 static void
10399 resolve_where (gfc_code *code, gfc_expr *mask)
10400 {
10401 gfc_code *cblock;
10402 gfc_code *cnext;
10403 gfc_expr *e = NULL;
10404
10405 cblock = code->block;
10406
10407 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10408 In case of nested WHERE, only the outmost one is stored. */
10409 if (mask == NULL) /* outmost WHERE */
10410 e = cblock->expr1;
10411 else /* inner WHERE */
10412 e = mask;
10413
10414 while (cblock)
10415 {
10416 if (cblock->expr1)
10417 {
10418 /* Check if the mask-expr has a consistent shape with the
10419 outmost WHERE mask-expr. */
10420 if (!resolve_where_shape (cblock->expr1, e))
10421 gfc_error ("WHERE mask at %L has inconsistent shape",
10422 &cblock->expr1->where);
10423 }
10424
10425 /* the assignment statement of a WHERE statement, or the first
10426 statement in where-body-construct of a WHERE construct */
10427 cnext = cblock->next;
10428 while (cnext)
10429 {
10430 switch (cnext->op)
10431 {
10432 /* WHERE assignment statement */
10433 case EXEC_ASSIGN:
10434
10435 /* Check shape consistent for WHERE assignment target. */
10436 if (e && !resolve_where_shape (cnext->expr1, e))
10437 gfc_error ("WHERE assignment target at %L has "
10438 "inconsistent shape", &cnext->expr1->where);
10439 break;
10440
10441
10442 case EXEC_ASSIGN_CALL:
10443 resolve_call (cnext);
10444 if (!cnext->resolved_sym->attr.elemental)
10445 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10446 &cnext->ext.actual->expr->where);
10447 break;
10448
10449 /* WHERE or WHERE construct is part of a where-body-construct */
10450 case EXEC_WHERE:
10451 resolve_where (cnext, e);
10452 break;
10453
10454 default:
10455 gfc_error ("Unsupported statement inside WHERE at %L",
10456 &cnext->loc);
10457 }
10458 /* the next statement within the same where-body-construct */
10459 cnext = cnext->next;
10460 }
10461 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10462 cblock = cblock->block;
10463 }
10464 }
10465
10466
10467 /* Resolve assignment in FORALL construct.
10468 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10469 FORALL index variables. */
10470
10471 static void
10472 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10473 {
10474 int n;
10475
10476 for (n = 0; n < nvar; n++)
10477 {
10478 gfc_symbol *forall_index;
10479
10480 forall_index = var_expr[n]->symtree->n.sym;
10481
10482 /* Check whether the assignment target is one of the FORALL index
10483 variable. */
10484 if ((code->expr1->expr_type == EXPR_VARIABLE)
10485 && (code->expr1->symtree->n.sym == forall_index))
10486 gfc_error ("Assignment to a FORALL index variable at %L",
10487 &code->expr1->where);
10488 else
10489 {
10490 /* If one of the FORALL index variables doesn't appear in the
10491 assignment variable, then there could be a many-to-one
10492 assignment. Emit a warning rather than an error because the
10493 mask could be resolving this problem. */
10494 if (!find_forall_index (code->expr1, forall_index, 0))
10495 gfc_warning (0, "The FORALL with index %qs is not used on the "
10496 "left side of the assignment at %L and so might "
10497 "cause multiple assignment to this object",
10498 var_expr[n]->symtree->name, &code->expr1->where);
10499 }
10500 }
10501 }
10502
10503
10504 /* Resolve WHERE statement in FORALL construct. */
10505
10506 static void
10507 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10508 gfc_expr **var_expr)
10509 {
10510 gfc_code *cblock;
10511 gfc_code *cnext;
10512
10513 cblock = code->block;
10514 while (cblock)
10515 {
10516 /* the assignment statement of a WHERE statement, or the first
10517 statement in where-body-construct of a WHERE construct */
10518 cnext = cblock->next;
10519 while (cnext)
10520 {
10521 switch (cnext->op)
10522 {
10523 /* WHERE assignment statement */
10524 case EXEC_ASSIGN:
10525 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10526 break;
10527
10528 /* WHERE operator assignment statement */
10529 case EXEC_ASSIGN_CALL:
10530 resolve_call (cnext);
10531 if (!cnext->resolved_sym->attr.elemental)
10532 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10533 &cnext->ext.actual->expr->where);
10534 break;
10535
10536 /* WHERE or WHERE construct is part of a where-body-construct */
10537 case EXEC_WHERE:
10538 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10539 break;
10540
10541 default:
10542 gfc_error ("Unsupported statement inside WHERE at %L",
10543 &cnext->loc);
10544 }
10545 /* the next statement within the same where-body-construct */
10546 cnext = cnext->next;
10547 }
10548 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10549 cblock = cblock->block;
10550 }
10551 }
10552
10553
10554 /* Traverse the FORALL body to check whether the following errors exist:
10555 1. For assignment, check if a many-to-one assignment happens.
10556 2. For WHERE statement, check the WHERE body to see if there is any
10557 many-to-one assignment. */
10558
10559 static void
10560 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10561 {
10562 gfc_code *c;
10563
10564 c = code->block->next;
10565 while (c)
10566 {
10567 switch (c->op)
10568 {
10569 case EXEC_ASSIGN:
10570 case EXEC_POINTER_ASSIGN:
10571 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10572 break;
10573
10574 case EXEC_ASSIGN_CALL:
10575 resolve_call (c);
10576 break;
10577
10578 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10579 there is no need to handle it here. */
10580 case EXEC_FORALL:
10581 break;
10582 case EXEC_WHERE:
10583 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10584 break;
10585 default:
10586 break;
10587 }
10588 /* The next statement in the FORALL body. */
10589 c = c->next;
10590 }
10591 }
10592
10593
10594 /* Counts the number of iterators needed inside a forall construct, including
10595 nested forall constructs. This is used to allocate the needed memory
10596 in gfc_resolve_forall. */
10597
10598 static int
10599 gfc_count_forall_iterators (gfc_code *code)
10600 {
10601 int max_iters, sub_iters, current_iters;
10602 gfc_forall_iterator *fa;
10603
10604 gcc_assert(code->op == EXEC_FORALL);
10605 max_iters = 0;
10606 current_iters = 0;
10607
10608 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10609 current_iters ++;
10610
10611 code = code->block->next;
10612
10613 while (code)
10614 {
10615 if (code->op == EXEC_FORALL)
10616 {
10617 sub_iters = gfc_count_forall_iterators (code);
10618 if (sub_iters > max_iters)
10619 max_iters = sub_iters;
10620 }
10621 code = code->next;
10622 }
10623
10624 return current_iters + max_iters;
10625 }
10626
10627
10628 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10629 gfc_resolve_forall_body to resolve the FORALL body. */
10630
10631 static void
10632 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10633 {
10634 static gfc_expr **var_expr;
10635 static int total_var = 0;
10636 static int nvar = 0;
10637 int i, old_nvar, tmp;
10638 gfc_forall_iterator *fa;
10639
10640 old_nvar = nvar;
10641
10642 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10643 return;
10644
10645 /* Start to resolve a FORALL construct */
10646 if (forall_save == 0)
10647 {
10648 /* Count the total number of FORALL indices in the nested FORALL
10649 construct in order to allocate the VAR_EXPR with proper size. */
10650 total_var = gfc_count_forall_iterators (code);
10651
10652 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10653 var_expr = XCNEWVEC (gfc_expr *, total_var);
10654 }
10655
10656 /* The information about FORALL iterator, including FORALL indices start, end
10657 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10658 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10659 {
10660 /* Fortran 20008: C738 (R753). */
10661 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10662 {
10663 gfc_error ("FORALL index-name at %L must be a scalar variable "
10664 "of type integer", &fa->var->where);
10665 continue;
10666 }
10667
10668 /* Check if any outer FORALL index name is the same as the current
10669 one. */
10670 for (i = 0; i < nvar; i++)
10671 {
10672 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10673 gfc_error ("An outer FORALL construct already has an index "
10674 "with this name %L", &fa->var->where);
10675 }
10676
10677 /* Record the current FORALL index. */
10678 var_expr[nvar] = gfc_copy_expr (fa->var);
10679
10680 nvar++;
10681
10682 /* No memory leak. */
10683 gcc_assert (nvar <= total_var);
10684 }
10685
10686 /* Resolve the FORALL body. */
10687 gfc_resolve_forall_body (code, nvar, var_expr);
10688
10689 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10690 gfc_resolve_blocks (code->block, ns);
10691
10692 tmp = nvar;
10693 nvar = old_nvar;
10694 /* Free only the VAR_EXPRs allocated in this frame. */
10695 for (i = nvar; i < tmp; i++)
10696 gfc_free_expr (var_expr[i]);
10697
10698 if (nvar == 0)
10699 {
10700 /* We are in the outermost FORALL construct. */
10701 gcc_assert (forall_save == 0);
10702
10703 /* VAR_EXPR is not needed any more. */
10704 free (var_expr);
10705 total_var = 0;
10706 }
10707 }
10708
10709
10710 /* Resolve a BLOCK construct statement. */
10711
10712 static void
10713 resolve_block_construct (gfc_code* code)
10714 {
10715 /* Resolve the BLOCK's namespace. */
10716 gfc_resolve (code->ext.block.ns);
10717
10718 /* For an ASSOCIATE block, the associations (and their targets) are already
10719 resolved during resolve_symbol. */
10720 }
10721
10722
10723 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10724 DO code nodes. */
10725
10726 void
10727 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10728 {
10729 bool t;
10730
10731 for (; b; b = b->block)
10732 {
10733 t = gfc_resolve_expr (b->expr1);
10734 if (!gfc_resolve_expr (b->expr2))
10735 t = false;
10736
10737 switch (b->op)
10738 {
10739 case EXEC_IF:
10740 if (t && b->expr1 != NULL
10741 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10742 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10743 &b->expr1->where);
10744 break;
10745
10746 case EXEC_WHERE:
10747 if (t
10748 && b->expr1 != NULL
10749 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10750 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10751 &b->expr1->where);
10752 break;
10753
10754 case EXEC_GOTO:
10755 resolve_branch (b->label1, b);
10756 break;
10757
10758 case EXEC_BLOCK:
10759 resolve_block_construct (b);
10760 break;
10761
10762 case EXEC_SELECT:
10763 case EXEC_SELECT_TYPE:
10764 case EXEC_SELECT_RANK:
10765 case EXEC_FORALL:
10766 case EXEC_DO:
10767 case EXEC_DO_WHILE:
10768 case EXEC_DO_CONCURRENT:
10769 case EXEC_CRITICAL:
10770 case EXEC_READ:
10771 case EXEC_WRITE:
10772 case EXEC_IOLENGTH:
10773 case EXEC_WAIT:
10774 break;
10775
10776 case EXEC_OMP_ATOMIC:
10777 case EXEC_OACC_ATOMIC:
10778 {
10779 /* Verify this before calling gfc_resolve_code, which might
10780 change it. */
10781 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10782 gcc_assert ((!b->ext.omp_clauses->capture
10783 && b->next->next == NULL)
10784 || (b->ext.omp_clauses->capture
10785 && b->next->next != NULL
10786 && b->next->next->op == EXEC_ASSIGN
10787 && b->next->next->next == NULL));
10788 }
10789 break;
10790
10791 case EXEC_OACC_PARALLEL_LOOP:
10792 case EXEC_OACC_PARALLEL:
10793 case EXEC_OACC_KERNELS_LOOP:
10794 case EXEC_OACC_KERNELS:
10795 case EXEC_OACC_SERIAL_LOOP:
10796 case EXEC_OACC_SERIAL:
10797 case EXEC_OACC_DATA:
10798 case EXEC_OACC_HOST_DATA:
10799 case EXEC_OACC_LOOP:
10800 case EXEC_OACC_UPDATE:
10801 case EXEC_OACC_WAIT:
10802 case EXEC_OACC_CACHE:
10803 case EXEC_OACC_ENTER_DATA:
10804 case EXEC_OACC_EXIT_DATA:
10805 case EXEC_OACC_ROUTINE:
10806 case EXEC_OMP_CRITICAL:
10807 case EXEC_OMP_DISTRIBUTE:
10808 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10809 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10810 case EXEC_OMP_DISTRIBUTE_SIMD:
10811 case EXEC_OMP_DO:
10812 case EXEC_OMP_DO_SIMD:
10813 case EXEC_OMP_LOOP:
10814 case EXEC_OMP_MASTER:
10815 case EXEC_OMP_MASTER_TASKLOOP:
10816 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
10817 case EXEC_OMP_ORDERED:
10818 case EXEC_OMP_PARALLEL:
10819 case EXEC_OMP_PARALLEL_DO:
10820 case EXEC_OMP_PARALLEL_DO_SIMD:
10821 case EXEC_OMP_PARALLEL_LOOP:
10822 case EXEC_OMP_PARALLEL_MASTER:
10823 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
10824 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
10825 case EXEC_OMP_PARALLEL_SECTIONS:
10826 case EXEC_OMP_PARALLEL_WORKSHARE:
10827 case EXEC_OMP_SECTIONS:
10828 case EXEC_OMP_SIMD:
10829 case EXEC_OMP_SINGLE:
10830 case EXEC_OMP_TARGET:
10831 case EXEC_OMP_TARGET_DATA:
10832 case EXEC_OMP_TARGET_ENTER_DATA:
10833 case EXEC_OMP_TARGET_EXIT_DATA:
10834 case EXEC_OMP_TARGET_PARALLEL:
10835 case EXEC_OMP_TARGET_PARALLEL_DO:
10836 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10837 case EXEC_OMP_TARGET_PARALLEL_LOOP:
10838 case EXEC_OMP_TARGET_SIMD:
10839 case EXEC_OMP_TARGET_TEAMS:
10840 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10841 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10842 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10843 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10844 case EXEC_OMP_TARGET_TEAMS_LOOP:
10845 case EXEC_OMP_TARGET_UPDATE:
10846 case EXEC_OMP_TASK:
10847 case EXEC_OMP_TASKGROUP:
10848 case EXEC_OMP_TASKLOOP:
10849 case EXEC_OMP_TASKLOOP_SIMD:
10850 case EXEC_OMP_TASKWAIT:
10851 case EXEC_OMP_TASKYIELD:
10852 case EXEC_OMP_TEAMS:
10853 case EXEC_OMP_TEAMS_DISTRIBUTE:
10854 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10855 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10856 case EXEC_OMP_TEAMS_LOOP:
10857 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10858 case EXEC_OMP_WORKSHARE:
10859 break;
10860
10861 default:
10862 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10863 }
10864
10865 gfc_resolve_code (b->next, ns);
10866 }
10867 }
10868
10869
10870 /* Does everything to resolve an ordinary assignment. Returns true
10871 if this is an interface assignment. */
10872 static bool
10873 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10874 {
10875 bool rval = false;
10876 gfc_expr *lhs;
10877 gfc_expr *rhs;
10878 int n;
10879 gfc_ref *ref;
10880 symbol_attribute attr;
10881
10882 if (gfc_extend_assign (code, ns))
10883 {
10884 gfc_expr** rhsptr;
10885
10886 if (code->op == EXEC_ASSIGN_CALL)
10887 {
10888 lhs = code->ext.actual->expr;
10889 rhsptr = &code->ext.actual->next->expr;
10890 }
10891 else
10892 {
10893 gfc_actual_arglist* args;
10894 gfc_typebound_proc* tbp;
10895
10896 gcc_assert (code->op == EXEC_COMPCALL);
10897
10898 args = code->expr1->value.compcall.actual;
10899 lhs = args->expr;
10900 rhsptr = &args->next->expr;
10901
10902 tbp = code->expr1->value.compcall.tbp;
10903 gcc_assert (!tbp->is_generic);
10904 }
10905
10906 /* Make a temporary rhs when there is a default initializer
10907 and rhs is the same symbol as the lhs. */
10908 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10909 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10910 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10911 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10912 *rhsptr = gfc_get_parentheses (*rhsptr);
10913
10914 return true;
10915 }
10916
10917 lhs = code->expr1;
10918 rhs = code->expr2;
10919
10920 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10921 && rhs->ts.type == BT_CHARACTER
10922 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10923 {
10924 /* Use of -fdec-char-conversions allows assignment of character data
10925 to non-character variables. This not permited for nonconstant
10926 strings. */
10927 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10928 gfc_typename (lhs), &rhs->where);
10929 return false;
10930 }
10931
10932 /* Handle the case of a BOZ literal on the RHS. */
10933 if (rhs->ts.type == BT_BOZ)
10934 {
10935 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10936 "statement value nor an actual argument of "
10937 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10938 &rhs->where))
10939 return false;
10940
10941 switch (lhs->ts.type)
10942 {
10943 case BT_INTEGER:
10944 if (!gfc_boz2int (rhs, lhs->ts.kind))
10945 return false;
10946 break;
10947 case BT_REAL:
10948 if (!gfc_boz2real (rhs, lhs->ts.kind))
10949 return false;
10950 break;
10951 default:
10952 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10953 return false;
10954 }
10955 }
10956
10957 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10958 {
10959 HOST_WIDE_INT llen = 0, rlen = 0;
10960 if (lhs->ts.u.cl != NULL
10961 && lhs->ts.u.cl->length != NULL
10962 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10963 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10964
10965 if (rhs->expr_type == EXPR_CONSTANT)
10966 rlen = rhs->value.character.length;
10967
10968 else if (rhs->ts.u.cl != NULL
10969 && rhs->ts.u.cl->length != NULL
10970 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10971 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10972
10973 if (rlen && llen && rlen > llen)
10974 gfc_warning_now (OPT_Wcharacter_truncation,
10975 "CHARACTER expression will be truncated "
10976 "in assignment (%ld/%ld) at %L",
10977 (long) llen, (long) rlen, &code->loc);
10978 }
10979
10980 /* Ensure that a vector index expression for the lvalue is evaluated
10981 to a temporary if the lvalue symbol is referenced in it. */
10982 if (lhs->rank)
10983 {
10984 for (ref = lhs->ref; ref; ref= ref->next)
10985 if (ref->type == REF_ARRAY)
10986 {
10987 for (n = 0; n < ref->u.ar.dimen; n++)
10988 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10989 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10990 ref->u.ar.start[n]))
10991 ref->u.ar.start[n]
10992 = gfc_get_parentheses (ref->u.ar.start[n]);
10993 }
10994 }
10995
10996 if (gfc_pure (NULL))
10997 {
10998 if (lhs->ts.type == BT_DERIVED
10999 && lhs->expr_type == EXPR_VARIABLE
11000 && lhs->ts.u.derived->attr.pointer_comp
11001 && rhs->expr_type == EXPR_VARIABLE
11002 && (gfc_impure_variable (rhs->symtree->n.sym)
11003 || gfc_is_coindexed (rhs)))
11004 {
11005 /* F2008, C1283. */
11006 if (gfc_is_coindexed (rhs))
11007 gfc_error ("Coindexed expression at %L is assigned to "
11008 "a derived type variable with a POINTER "
11009 "component in a PURE procedure",
11010 &rhs->where);
11011 else
11012 /* F2008, C1283 (4). */
11013 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
11014 "shall not be used as the expr at %L of an intrinsic "
11015 "assignment statement in which the variable is of a "
11016 "derived type if the derived type has a pointer "
11017 "component at any level of component selection.",
11018 &rhs->where);
11019 return rval;
11020 }
11021
11022 /* Fortran 2008, C1283. */
11023 if (gfc_is_coindexed (lhs))
11024 {
11025 gfc_error ("Assignment to coindexed variable at %L in a PURE "
11026 "procedure", &rhs->where);
11027 return rval;
11028 }
11029 }
11030
11031 if (gfc_implicit_pure (NULL))
11032 {
11033 if (lhs->expr_type == EXPR_VARIABLE
11034 && lhs->symtree->n.sym != gfc_current_ns->proc_name
11035 && lhs->symtree->n.sym->ns != gfc_current_ns)
11036 gfc_unset_implicit_pure (NULL);
11037
11038 if (lhs->ts.type == BT_DERIVED
11039 && lhs->expr_type == EXPR_VARIABLE
11040 && lhs->ts.u.derived->attr.pointer_comp
11041 && rhs->expr_type == EXPR_VARIABLE
11042 && (gfc_impure_variable (rhs->symtree->n.sym)
11043 || gfc_is_coindexed (rhs)))
11044 gfc_unset_implicit_pure (NULL);
11045
11046 /* Fortran 2008, C1283. */
11047 if (gfc_is_coindexed (lhs))
11048 gfc_unset_implicit_pure (NULL);
11049 }
11050
11051 /* F2008, 7.2.1.2. */
11052 attr = gfc_expr_attr (lhs);
11053 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11054 {
11055 if (attr.codimension)
11056 {
11057 gfc_error ("Assignment to polymorphic coarray at %L is not "
11058 "permitted", &lhs->where);
11059 return false;
11060 }
11061 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11062 "polymorphic variable at %L", &lhs->where))
11063 return false;
11064 if (!flag_realloc_lhs)
11065 {
11066 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11067 "requires %<-frealloc-lhs%>", &lhs->where);
11068 return false;
11069 }
11070 }
11071 else if (lhs->ts.type == BT_CLASS)
11072 {
11073 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11074 "assignment at %L - check that there is a matching specific "
11075 "subroutine for '=' operator", &lhs->where);
11076 return false;
11077 }
11078
11079 bool lhs_coindexed = gfc_is_coindexed (lhs);
11080
11081 /* F2008, Section 7.2.1.2. */
11082 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11083 {
11084 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11085 "component in assignment at %L", &lhs->where);
11086 return false;
11087 }
11088
11089 /* Assign the 'data' of a class object to a derived type. */
11090 if (lhs->ts.type == BT_DERIVED
11091 && rhs->ts.type == BT_CLASS
11092 && rhs->expr_type != EXPR_ARRAY)
11093 gfc_add_data_component (rhs);
11094
11095 /* Make sure there is a vtable and, in particular, a _copy for the
11096 rhs type. */
11097 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
11098 gfc_find_vtab (&rhs->ts);
11099
11100 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11101 && (lhs_coindexed
11102 || (code->expr2->expr_type == EXPR_FUNCTION
11103 && code->expr2->value.function.isym
11104 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11105 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11106 && !gfc_expr_attr (rhs).allocatable
11107 && !gfc_has_vector_subscript (rhs)));
11108
11109 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11110
11111 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11112 Additionally, insert this code when the RHS is a CAF as we then use the
11113 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11114 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11115 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11116 path. */
11117 if (caf_convert_to_send)
11118 {
11119 if (code->expr2->expr_type == EXPR_FUNCTION
11120 && code->expr2->value.function.isym
11121 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11122 remove_caf_get_intrinsic (code->expr2);
11123 code->op = EXEC_CALL;
11124 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11125 code->resolved_sym = code->symtree->n.sym;
11126 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11127 code->resolved_sym->attr.intrinsic = 1;
11128 code->resolved_sym->attr.subroutine = 1;
11129 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11130 gfc_commit_symbol (code->resolved_sym);
11131 code->ext.actual = gfc_get_actual_arglist ();
11132 code->ext.actual->expr = lhs;
11133 code->ext.actual->next = gfc_get_actual_arglist ();
11134 code->ext.actual->next->expr = rhs;
11135 code->expr1 = NULL;
11136 code->expr2 = NULL;
11137 }
11138
11139 return false;
11140 }
11141
11142
11143 /* Add a component reference onto an expression. */
11144
11145 static void
11146 add_comp_ref (gfc_expr *e, gfc_component *c)
11147 {
11148 gfc_ref **ref;
11149 ref = &(e->ref);
11150 while (*ref)
11151 ref = &((*ref)->next);
11152 *ref = gfc_get_ref ();
11153 (*ref)->type = REF_COMPONENT;
11154 (*ref)->u.c.sym = e->ts.u.derived;
11155 (*ref)->u.c.component = c;
11156 e->ts = c->ts;
11157
11158 /* Add a full array ref, as necessary. */
11159 if (c->as)
11160 {
11161 gfc_add_full_array_ref (e, c->as);
11162 e->rank = c->as->rank;
11163 }
11164 }
11165
11166
11167 /* Build an assignment. Keep the argument 'op' for future use, so that
11168 pointer assignments can be made. */
11169
11170 static gfc_code *
11171 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11172 gfc_component *comp1, gfc_component *comp2, locus loc)
11173 {
11174 gfc_code *this_code;
11175
11176 this_code = gfc_get_code (op);
11177 this_code->next = NULL;
11178 this_code->expr1 = gfc_copy_expr (expr1);
11179 this_code->expr2 = gfc_copy_expr (expr2);
11180 this_code->loc = loc;
11181 if (comp1 && comp2)
11182 {
11183 add_comp_ref (this_code->expr1, comp1);
11184 add_comp_ref (this_code->expr2, comp2);
11185 }
11186
11187 return this_code;
11188 }
11189
11190
11191 /* Makes a temporary variable expression based on the characteristics of
11192 a given variable expression. */
11193
11194 static gfc_expr*
11195 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11196 {
11197 static int serial = 0;
11198 char name[GFC_MAX_SYMBOL_LEN];
11199 gfc_symtree *tmp;
11200 gfc_array_spec *as;
11201 gfc_array_ref *aref;
11202 gfc_ref *ref;
11203
11204 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11205 gfc_get_sym_tree (name, ns, &tmp, false);
11206 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11207
11208 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11209 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11210 NULL,
11211 e->value.character.length);
11212
11213 as = NULL;
11214 ref = NULL;
11215 aref = NULL;
11216
11217 /* Obtain the arrayspec for the temporary. */
11218 if (e->rank && e->expr_type != EXPR_ARRAY
11219 && e->expr_type != EXPR_FUNCTION
11220 && e->expr_type != EXPR_OP)
11221 {
11222 aref = gfc_find_array_ref (e);
11223 if (e->expr_type == EXPR_VARIABLE
11224 && e->symtree->n.sym->as == aref->as)
11225 as = aref->as;
11226 else
11227 {
11228 for (ref = e->ref; ref; ref = ref->next)
11229 if (ref->type == REF_COMPONENT
11230 && ref->u.c.component->as == aref->as)
11231 {
11232 as = aref->as;
11233 break;
11234 }
11235 }
11236 }
11237
11238 /* Add the attributes and the arrayspec to the temporary. */
11239 tmp->n.sym->attr = gfc_expr_attr (e);
11240 tmp->n.sym->attr.function = 0;
11241 tmp->n.sym->attr.proc_pointer = 0;
11242 tmp->n.sym->attr.result = 0;
11243 tmp->n.sym->attr.flavor = FL_VARIABLE;
11244 tmp->n.sym->attr.dummy = 0;
11245 tmp->n.sym->attr.use_assoc = 0;
11246 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11247
11248 if (as)
11249 {
11250 tmp->n.sym->as = gfc_copy_array_spec (as);
11251 if (!ref)
11252 ref = e->ref;
11253 if (as->type == AS_DEFERRED)
11254 tmp->n.sym->attr.allocatable = 1;
11255 }
11256 else if (e->rank && (e->expr_type == EXPR_ARRAY
11257 || e->expr_type == EXPR_FUNCTION
11258 || e->expr_type == EXPR_OP))
11259 {
11260 tmp->n.sym->as = gfc_get_array_spec ();
11261 tmp->n.sym->as->type = AS_DEFERRED;
11262 tmp->n.sym->as->rank = e->rank;
11263 tmp->n.sym->attr.allocatable = 1;
11264 tmp->n.sym->attr.dimension = 1;
11265 }
11266 else
11267 tmp->n.sym->attr.dimension = 0;
11268
11269 gfc_set_sym_referenced (tmp->n.sym);
11270 gfc_commit_symbol (tmp->n.sym);
11271 e = gfc_lval_expr_from_sym (tmp->n.sym);
11272
11273 /* Should the lhs be a section, use its array ref for the
11274 temporary expression. */
11275 if (aref && aref->type != AR_FULL)
11276 {
11277 gfc_free_ref_list (e->ref);
11278 e->ref = gfc_copy_ref (ref);
11279 }
11280 return e;
11281 }
11282
11283
11284 /* Add one line of code to the code chain, making sure that 'head' and
11285 'tail' are appropriately updated. */
11286
11287 static void
11288 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11289 {
11290 gcc_assert (this_code);
11291 if (*head == NULL)
11292 *head = *tail = *this_code;
11293 else
11294 *tail = gfc_append_code (*tail, *this_code);
11295 *this_code = NULL;
11296 }
11297
11298
11299 /* Counts the potential number of part array references that would
11300 result from resolution of typebound defined assignments. */
11301
11302 static int
11303 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11304 {
11305 gfc_component *c;
11306 int c_depth = 0, t_depth;
11307
11308 for (c= derived->components; c; c = c->next)
11309 {
11310 if ((!gfc_bt_struct (c->ts.type)
11311 || c->attr.pointer
11312 || c->attr.allocatable
11313 || c->attr.proc_pointer_comp
11314 || c->attr.class_pointer
11315 || c->attr.proc_pointer)
11316 && !c->attr.defined_assign_comp)
11317 continue;
11318
11319 if (c->as && c_depth == 0)
11320 c_depth = 1;
11321
11322 if (c->ts.u.derived->attr.defined_assign_comp)
11323 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11324 c->as ? 1 : 0);
11325 else
11326 t_depth = 0;
11327
11328 c_depth = t_depth > c_depth ? t_depth : c_depth;
11329 }
11330 return depth + c_depth;
11331 }
11332
11333
11334 /* Implement 7.2.1.3 of the F08 standard:
11335 "An intrinsic assignment where the variable is of derived type is
11336 performed as if each component of the variable were assigned from the
11337 corresponding component of expr using pointer assignment (7.2.2) for
11338 each pointer component, defined assignment for each nonpointer
11339 nonallocatable component of a type that has a type-bound defined
11340 assignment consistent with the component, intrinsic assignment for
11341 each other nonpointer nonallocatable component, ..."
11342
11343 The pointer assignments are taken care of by the intrinsic
11344 assignment of the structure itself. This function recursively adds
11345 defined assignments where required. The recursion is accomplished
11346 by calling gfc_resolve_code.
11347
11348 When the lhs in a defined assignment has intent INOUT, we need a
11349 temporary for the lhs. In pseudo-code:
11350
11351 ! Only call function lhs once.
11352 if (lhs is not a constant or an variable)
11353 temp_x = expr2
11354 expr2 => temp_x
11355 ! Do the intrinsic assignment
11356 expr1 = expr2
11357 ! Now do the defined assignments
11358 do over components with typebound defined assignment [%cmp]
11359 #if one component's assignment procedure is INOUT
11360 t1 = expr1
11361 #if expr2 non-variable
11362 temp_x = expr2
11363 expr2 => temp_x
11364 # endif
11365 expr1 = expr2
11366 # for each cmp
11367 t1%cmp {defined=} expr2%cmp
11368 expr1%cmp = t1%cmp
11369 #else
11370 expr1 = expr2
11371
11372 # for each cmp
11373 expr1%cmp {defined=} expr2%cmp
11374 #endif
11375 */
11376
11377 /* The temporary assignments have to be put on top of the additional
11378 code to avoid the result being changed by the intrinsic assignment.
11379 */
11380 static int component_assignment_level = 0;
11381 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11382
11383 static void
11384 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11385 {
11386 gfc_component *comp1, *comp2;
11387 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11388 gfc_expr *t1;
11389 int error_count, depth;
11390
11391 gfc_get_errors (NULL, &error_count);
11392
11393 /* Filter out continuing processing after an error. */
11394 if (error_count
11395 || (*code)->expr1->ts.type != BT_DERIVED
11396 || (*code)->expr2->ts.type != BT_DERIVED)
11397 return;
11398
11399 /* TODO: Handle more than one part array reference in assignments. */
11400 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11401 (*code)->expr1->rank ? 1 : 0);
11402 if (depth > 1)
11403 {
11404 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11405 "done because multiple part array references would "
11406 "occur in intermediate expressions.", &(*code)->loc);
11407 return;
11408 }
11409
11410 component_assignment_level++;
11411
11412 /* Create a temporary so that functions get called only once. */
11413 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11414 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11415 {
11416 gfc_expr *tmp_expr;
11417
11418 /* Assign the rhs to the temporary. */
11419 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11420 this_code = build_assignment (EXEC_ASSIGN,
11421 tmp_expr, (*code)->expr2,
11422 NULL, NULL, (*code)->loc);
11423 /* Add the code and substitute the rhs expression. */
11424 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11425 gfc_free_expr ((*code)->expr2);
11426 (*code)->expr2 = tmp_expr;
11427 }
11428
11429 /* Do the intrinsic assignment. This is not needed if the lhs is one
11430 of the temporaries generated here, since the intrinsic assignment
11431 to the final result already does this. */
11432 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11433 {
11434 this_code = build_assignment (EXEC_ASSIGN,
11435 (*code)->expr1, (*code)->expr2,
11436 NULL, NULL, (*code)->loc);
11437 add_code_to_chain (&this_code, &head, &tail);
11438 }
11439
11440 comp1 = (*code)->expr1->ts.u.derived->components;
11441 comp2 = (*code)->expr2->ts.u.derived->components;
11442
11443 t1 = NULL;
11444 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11445 {
11446 bool inout = false;
11447
11448 /* The intrinsic assignment does the right thing for pointers
11449 of all kinds and allocatable components. */
11450 if (!gfc_bt_struct (comp1->ts.type)
11451 || comp1->attr.pointer
11452 || comp1->attr.allocatable
11453 || comp1->attr.proc_pointer_comp
11454 || comp1->attr.class_pointer
11455 || comp1->attr.proc_pointer)
11456 continue;
11457
11458 /* Make an assigment for this component. */
11459 this_code = build_assignment (EXEC_ASSIGN,
11460 (*code)->expr1, (*code)->expr2,
11461 comp1, comp2, (*code)->loc);
11462
11463 /* Convert the assignment if there is a defined assignment for
11464 this type. Otherwise, using the call from gfc_resolve_code,
11465 recurse into its components. */
11466 gfc_resolve_code (this_code, ns);
11467
11468 if (this_code->op == EXEC_ASSIGN_CALL)
11469 {
11470 gfc_formal_arglist *dummy_args;
11471 gfc_symbol *rsym;
11472 /* Check that there is a typebound defined assignment. If not,
11473 then this must be a module defined assignment. We cannot
11474 use the defined_assign_comp attribute here because it must
11475 be this derived type that has the defined assignment and not
11476 a parent type. */
11477 if (!(comp1->ts.u.derived->f2k_derived
11478 && comp1->ts.u.derived->f2k_derived
11479 ->tb_op[INTRINSIC_ASSIGN]))
11480 {
11481 gfc_free_statements (this_code);
11482 this_code = NULL;
11483 continue;
11484 }
11485
11486 /* If the first argument of the subroutine has intent INOUT
11487 a temporary must be generated and used instead. */
11488 rsym = this_code->resolved_sym;
11489 dummy_args = gfc_sym_get_dummy_args (rsym);
11490 if (dummy_args
11491 && dummy_args->sym->attr.intent == INTENT_INOUT)
11492 {
11493 gfc_code *temp_code;
11494 inout = true;
11495
11496 /* Build the temporary required for the assignment and put
11497 it at the head of the generated code. */
11498 if (!t1)
11499 {
11500 t1 = get_temp_from_expr ((*code)->expr1, ns);
11501 temp_code = build_assignment (EXEC_ASSIGN,
11502 t1, (*code)->expr1,
11503 NULL, NULL, (*code)->loc);
11504
11505 /* For allocatable LHS, check whether it is allocated. Note
11506 that allocatable components with defined assignment are
11507 not yet support. See PR 57696. */
11508 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11509 {
11510 gfc_code *block;
11511 gfc_expr *e =
11512 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11513 block = gfc_get_code (EXEC_IF);
11514 block->block = gfc_get_code (EXEC_IF);
11515 block->block->expr1
11516 = gfc_build_intrinsic_call (ns,
11517 GFC_ISYM_ALLOCATED, "allocated",
11518 (*code)->loc, 1, e);
11519 block->block->next = temp_code;
11520 temp_code = block;
11521 }
11522 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11523 }
11524
11525 /* Replace the first actual arg with the component of the
11526 temporary. */
11527 gfc_free_expr (this_code->ext.actual->expr);
11528 this_code->ext.actual->expr = gfc_copy_expr (t1);
11529 add_comp_ref (this_code->ext.actual->expr, comp1);
11530
11531 /* If the LHS variable is allocatable and wasn't allocated and
11532 the temporary is allocatable, pointer assign the address of
11533 the freshly allocated LHS to the temporary. */
11534 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11535 && gfc_expr_attr ((*code)->expr1).allocatable)
11536 {
11537 gfc_code *block;
11538 gfc_expr *cond;
11539
11540 cond = gfc_get_expr ();
11541 cond->ts.type = BT_LOGICAL;
11542 cond->ts.kind = gfc_default_logical_kind;
11543 cond->expr_type = EXPR_OP;
11544 cond->where = (*code)->loc;
11545 cond->value.op.op = INTRINSIC_NOT;
11546 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11547 GFC_ISYM_ALLOCATED, "allocated",
11548 (*code)->loc, 1, gfc_copy_expr (t1));
11549 block = gfc_get_code (EXEC_IF);
11550 block->block = gfc_get_code (EXEC_IF);
11551 block->block->expr1 = cond;
11552 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11553 t1, (*code)->expr1,
11554 NULL, NULL, (*code)->loc);
11555 add_code_to_chain (&block, &head, &tail);
11556 }
11557 }
11558 }
11559 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11560 {
11561 /* Don't add intrinsic assignments since they are already
11562 effected by the intrinsic assignment of the structure. */
11563 gfc_free_statements (this_code);
11564 this_code = NULL;
11565 continue;
11566 }
11567
11568 add_code_to_chain (&this_code, &head, &tail);
11569
11570 if (t1 && inout)
11571 {
11572 /* Transfer the value to the final result. */
11573 this_code = build_assignment (EXEC_ASSIGN,
11574 (*code)->expr1, t1,
11575 comp1, comp2, (*code)->loc);
11576 add_code_to_chain (&this_code, &head, &tail);
11577 }
11578 }
11579
11580 /* Put the temporary assignments at the top of the generated code. */
11581 if (tmp_head && component_assignment_level == 1)
11582 {
11583 gfc_append_code (tmp_head, head);
11584 head = tmp_head;
11585 tmp_head = tmp_tail = NULL;
11586 }
11587
11588 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11589 // not accidentally deallocated. Hence, nullify t1.
11590 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11591 && gfc_expr_attr ((*code)->expr1).allocatable)
11592 {
11593 gfc_code *block;
11594 gfc_expr *cond;
11595 gfc_expr *e;
11596
11597 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11598 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11599 (*code)->loc, 2, gfc_copy_expr (t1), e);
11600 block = gfc_get_code (EXEC_IF);
11601 block->block = gfc_get_code (EXEC_IF);
11602 block->block->expr1 = cond;
11603 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11604 t1, gfc_get_null_expr (&(*code)->loc),
11605 NULL, NULL, (*code)->loc);
11606 gfc_append_code (tail, block);
11607 tail = block;
11608 }
11609
11610 /* Now attach the remaining code chain to the input code. Step on
11611 to the end of the new code since resolution is complete. */
11612 gcc_assert ((*code)->op == EXEC_ASSIGN);
11613 tail->next = (*code)->next;
11614 /* Overwrite 'code' because this would place the intrinsic assignment
11615 before the temporary for the lhs is created. */
11616 gfc_free_expr ((*code)->expr1);
11617 gfc_free_expr ((*code)->expr2);
11618 **code = *head;
11619 if (head != tail)
11620 free (head);
11621 *code = tail;
11622
11623 component_assignment_level--;
11624 }
11625
11626
11627 /* F2008: Pointer function assignments are of the form:
11628 ptr_fcn (args) = expr
11629 This function breaks these assignments into two statements:
11630 temporary_pointer => ptr_fcn(args)
11631 temporary_pointer = expr */
11632
11633 static bool
11634 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11635 {
11636 gfc_expr *tmp_ptr_expr;
11637 gfc_code *this_code;
11638 gfc_component *comp;
11639 gfc_symbol *s;
11640
11641 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11642 return false;
11643
11644 /* Even if standard does not support this feature, continue to build
11645 the two statements to avoid upsetting frontend_passes.c. */
11646 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11647 "%L", &(*code)->loc);
11648
11649 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11650
11651 if (comp)
11652 s = comp->ts.interface;
11653 else
11654 s = (*code)->expr1->symtree->n.sym;
11655
11656 if (s == NULL || !s->result->attr.pointer)
11657 {
11658 gfc_error ("The function result on the lhs of the assignment at "
11659 "%L must have the pointer attribute.",
11660 &(*code)->expr1->where);
11661 (*code)->op = EXEC_NOP;
11662 return false;
11663 }
11664
11665 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
11666
11667 /* get_temp_from_expression is set up for ordinary assignments. To that
11668 end, where array bounds are not known, arrays are made allocatable.
11669 Change the temporary to a pointer here. */
11670 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11671 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11672 tmp_ptr_expr->where = (*code)->loc;
11673
11674 this_code = build_assignment (EXEC_ASSIGN,
11675 tmp_ptr_expr, (*code)->expr2,
11676 NULL, NULL, (*code)->loc);
11677 this_code->next = (*code)->next;
11678 (*code)->next = this_code;
11679 (*code)->op = EXEC_POINTER_ASSIGN;
11680 (*code)->expr2 = (*code)->expr1;
11681 (*code)->expr1 = tmp_ptr_expr;
11682
11683 return true;
11684 }
11685
11686
11687 /* Deferred character length assignments from an operator expression
11688 require a temporary because the character length of the lhs can
11689 change in the course of the assignment. */
11690
11691 static bool
11692 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11693 {
11694 gfc_expr *tmp_expr;
11695 gfc_code *this_code;
11696
11697 if (!((*code)->expr1->ts.type == BT_CHARACTER
11698 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11699 && (*code)->expr2->expr_type == EXPR_OP))
11700 return false;
11701
11702 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11703 return false;
11704
11705 if (gfc_expr_attr ((*code)->expr1).pointer)
11706 return false;
11707
11708 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11709 tmp_expr->where = (*code)->loc;
11710
11711 /* A new charlen is required to ensure that the variable string
11712 length is different to that of the original lhs. */
11713 tmp_expr->ts.u.cl = gfc_get_charlen();
11714 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11715 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11716 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11717
11718 tmp_expr->symtree->n.sym->ts.deferred = 1;
11719
11720 this_code = build_assignment (EXEC_ASSIGN,
11721 (*code)->expr1,
11722 gfc_copy_expr (tmp_expr),
11723 NULL, NULL, (*code)->loc);
11724
11725 (*code)->expr1 = tmp_expr;
11726
11727 this_code->next = (*code)->next;
11728 (*code)->next = this_code;
11729
11730 return true;
11731 }
11732
11733
11734 /* Given a block of code, recursively resolve everything pointed to by this
11735 code block. */
11736
11737 void
11738 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11739 {
11740 int omp_workshare_save;
11741 int forall_save, do_concurrent_save;
11742 code_stack frame;
11743 bool t;
11744
11745 frame.prev = cs_base;
11746 frame.head = code;
11747 cs_base = &frame;
11748
11749 find_reachable_labels (code);
11750
11751 for (; code; code = code->next)
11752 {
11753 frame.current = code;
11754 forall_save = forall_flag;
11755 do_concurrent_save = gfc_do_concurrent_flag;
11756
11757 if (code->op == EXEC_FORALL)
11758 {
11759 forall_flag = 1;
11760 gfc_resolve_forall (code, ns, forall_save);
11761 forall_flag = 2;
11762 }
11763 else if (code->block)
11764 {
11765 omp_workshare_save = -1;
11766 switch (code->op)
11767 {
11768 case EXEC_OACC_PARALLEL_LOOP:
11769 case EXEC_OACC_PARALLEL:
11770 case EXEC_OACC_KERNELS_LOOP:
11771 case EXEC_OACC_KERNELS:
11772 case EXEC_OACC_SERIAL_LOOP:
11773 case EXEC_OACC_SERIAL:
11774 case EXEC_OACC_DATA:
11775 case EXEC_OACC_HOST_DATA:
11776 case EXEC_OACC_LOOP:
11777 gfc_resolve_oacc_blocks (code, ns);
11778 break;
11779 case EXEC_OMP_PARALLEL_WORKSHARE:
11780 omp_workshare_save = omp_workshare_flag;
11781 omp_workshare_flag = 1;
11782 gfc_resolve_omp_parallel_blocks (code, ns);
11783 break;
11784 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11785 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11786 case EXEC_OMP_PARALLEL:
11787 case EXEC_OMP_PARALLEL_DO:
11788 case EXEC_OMP_PARALLEL_DO_SIMD:
11789 case EXEC_OMP_PARALLEL_MASTER:
11790 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
11791 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
11792 case EXEC_OMP_PARALLEL_SECTIONS:
11793 case EXEC_OMP_TARGET_PARALLEL:
11794 case EXEC_OMP_TARGET_PARALLEL_DO:
11795 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11796 case EXEC_OMP_TARGET_TEAMS:
11797 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11798 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11799 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11800 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11801 case EXEC_OMP_TASK:
11802 case EXEC_OMP_TASKLOOP:
11803 case EXEC_OMP_TASKLOOP_SIMD:
11804 case EXEC_OMP_TEAMS:
11805 case EXEC_OMP_TEAMS_DISTRIBUTE:
11806 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11807 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11808 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11809 omp_workshare_save = omp_workshare_flag;
11810 omp_workshare_flag = 0;
11811 gfc_resolve_omp_parallel_blocks (code, ns);
11812 break;
11813 case EXEC_OMP_DISTRIBUTE:
11814 case EXEC_OMP_DISTRIBUTE_SIMD:
11815 case EXEC_OMP_DO:
11816 case EXEC_OMP_DO_SIMD:
11817 case EXEC_OMP_SIMD:
11818 case EXEC_OMP_TARGET_SIMD:
11819 gfc_resolve_omp_do_blocks (code, ns);
11820 break;
11821 case EXEC_SELECT_TYPE:
11822 case EXEC_SELECT_RANK:
11823 /* Blocks are handled in resolve_select_type/rank because we
11824 have to transform the SELECT TYPE into ASSOCIATE first. */
11825 break;
11826 case EXEC_DO_CONCURRENT:
11827 gfc_do_concurrent_flag = 1;
11828 gfc_resolve_blocks (code->block, ns);
11829 gfc_do_concurrent_flag = 2;
11830 break;
11831 case EXEC_OMP_WORKSHARE:
11832 omp_workshare_save = omp_workshare_flag;
11833 omp_workshare_flag = 1;
11834 /* FALL THROUGH */
11835 default:
11836 gfc_resolve_blocks (code->block, ns);
11837 break;
11838 }
11839
11840 if (omp_workshare_save != -1)
11841 omp_workshare_flag = omp_workshare_save;
11842 }
11843 start:
11844 t = true;
11845 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11846 t = gfc_resolve_expr (code->expr1);
11847 forall_flag = forall_save;
11848 gfc_do_concurrent_flag = do_concurrent_save;
11849
11850 if (!gfc_resolve_expr (code->expr2))
11851 t = false;
11852
11853 if (code->op == EXEC_ALLOCATE
11854 && !gfc_resolve_expr (code->expr3))
11855 t = false;
11856
11857 switch (code->op)
11858 {
11859 case EXEC_NOP:
11860 case EXEC_END_BLOCK:
11861 case EXEC_END_NESTED_BLOCK:
11862 case EXEC_CYCLE:
11863 case EXEC_PAUSE:
11864 case EXEC_STOP:
11865 case EXEC_ERROR_STOP:
11866 case EXEC_EXIT:
11867 case EXEC_CONTINUE:
11868 case EXEC_DT_END:
11869 case EXEC_ASSIGN_CALL:
11870 break;
11871
11872 case EXEC_CRITICAL:
11873 resolve_critical (code);
11874 break;
11875
11876 case EXEC_SYNC_ALL:
11877 case EXEC_SYNC_IMAGES:
11878 case EXEC_SYNC_MEMORY:
11879 resolve_sync (code);
11880 break;
11881
11882 case EXEC_LOCK:
11883 case EXEC_UNLOCK:
11884 case EXEC_EVENT_POST:
11885 case EXEC_EVENT_WAIT:
11886 resolve_lock_unlock_event (code);
11887 break;
11888
11889 case EXEC_FAIL_IMAGE:
11890 case EXEC_FORM_TEAM:
11891 case EXEC_CHANGE_TEAM:
11892 case EXEC_END_TEAM:
11893 case EXEC_SYNC_TEAM:
11894 break;
11895
11896 case EXEC_ENTRY:
11897 /* Keep track of which entry we are up to. */
11898 current_entry_id = code->ext.entry->id;
11899 break;
11900
11901 case EXEC_WHERE:
11902 resolve_where (code, NULL);
11903 break;
11904
11905 case EXEC_GOTO:
11906 if (code->expr1 != NULL)
11907 {
11908 if (code->expr1->expr_type != EXPR_VARIABLE
11909 || code->expr1->ts.type != BT_INTEGER
11910 || (code->expr1->ref
11911 && code->expr1->ref->type == REF_ARRAY)
11912 || code->expr1->symtree == NULL
11913 || (code->expr1->symtree->n.sym
11914 && (code->expr1->symtree->n.sym->attr.flavor
11915 == FL_PARAMETER)))
11916 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11917 "scalar INTEGER variable", &code->expr1->where);
11918 else if (code->expr1->symtree->n.sym
11919 && code->expr1->symtree->n.sym->attr.assign != 1)
11920 gfc_error ("Variable %qs has not been assigned a target "
11921 "label at %L", code->expr1->symtree->n.sym->name,
11922 &code->expr1->where);
11923 }
11924 else
11925 resolve_branch (code->label1, code);
11926 break;
11927
11928 case EXEC_RETURN:
11929 if (code->expr1 != NULL
11930 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11931 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11932 "INTEGER return specifier", &code->expr1->where);
11933 break;
11934
11935 case EXEC_INIT_ASSIGN:
11936 case EXEC_END_PROCEDURE:
11937 break;
11938
11939 case EXEC_ASSIGN:
11940 if (!t)
11941 break;
11942
11943 if (code->expr1->ts.type == BT_CLASS)
11944 gfc_find_vtab (&code->expr2->ts);
11945
11946 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11947 the LHS. */
11948 if (code->expr1->expr_type == EXPR_FUNCTION
11949 && code->expr1->value.function.isym
11950 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11951 remove_caf_get_intrinsic (code->expr1);
11952
11953 /* If this is a pointer function in an lvalue variable context,
11954 the new code will have to be resolved afresh. This is also the
11955 case with an error, where the code is transformed into NOP to
11956 prevent ICEs downstream. */
11957 if (resolve_ptr_fcn_assign (&code, ns)
11958 || code->op == EXEC_NOP)
11959 goto start;
11960
11961 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11962 _("assignment")))
11963 break;
11964
11965 if (resolve_ordinary_assign (code, ns))
11966 {
11967 if (omp_workshare_flag)
11968 {
11969 gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
11970 "at %L", &code->loc);
11971 break;
11972 }
11973 if (code->op == EXEC_COMPCALL)
11974 goto compcall;
11975 else
11976 goto call;
11977 }
11978
11979 /* Check for dependencies in deferred character length array
11980 assignments and generate a temporary, if necessary. */
11981 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11982 break;
11983
11984 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11985 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11986 && code->expr1->ts.u.derived
11987 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11988 generate_component_assignments (&code, ns);
11989
11990 break;
11991
11992 case EXEC_LABEL_ASSIGN:
11993 if (code->label1->defined == ST_LABEL_UNKNOWN)
11994 gfc_error ("Label %d referenced at %L is never defined",
11995 code->label1->value, &code->label1->where);
11996 if (t
11997 && (code->expr1->expr_type != EXPR_VARIABLE
11998 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11999 || code->expr1->symtree->n.sym->ts.kind
12000 != gfc_default_integer_kind
12001 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
12002 || code->expr1->symtree->n.sym->as != NULL))
12003 gfc_error ("ASSIGN statement at %L requires a scalar "
12004 "default INTEGER variable", &code->expr1->where);
12005 break;
12006
12007 case EXEC_POINTER_ASSIGN:
12008 {
12009 gfc_expr* e;
12010
12011 if (!t)
12012 break;
12013
12014 /* This is both a variable definition and pointer assignment
12015 context, so check both of them. For rank remapping, a final
12016 array ref may be present on the LHS and fool gfc_expr_attr
12017 used in gfc_check_vardef_context. Remove it. */
12018 e = remove_last_array_ref (code->expr1);
12019 t = gfc_check_vardef_context (e, true, false, false,
12020 _("pointer assignment"));
12021 if (t)
12022 t = gfc_check_vardef_context (e, false, false, false,
12023 _("pointer assignment"));
12024 gfc_free_expr (e);
12025
12026 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
12027
12028 if (!t)
12029 break;
12030
12031 /* Assigning a class object always is a regular assign. */
12032 if (code->expr2->ts.type == BT_CLASS
12033 && code->expr1->ts.type == BT_CLASS
12034 && CLASS_DATA (code->expr2)
12035 && !CLASS_DATA (code->expr2)->attr.dimension
12036 && !(gfc_expr_attr (code->expr1).proc_pointer
12037 && code->expr2->expr_type == EXPR_VARIABLE
12038 && code->expr2->symtree->n.sym->attr.flavor
12039 == FL_PROCEDURE))
12040 code->op = EXEC_ASSIGN;
12041 break;
12042 }
12043
12044 case EXEC_ARITHMETIC_IF:
12045 {
12046 gfc_expr *e = code->expr1;
12047
12048 gfc_resolve_expr (e);
12049 if (e->expr_type == EXPR_NULL)
12050 gfc_error ("Invalid NULL at %L", &e->where);
12051
12052 if (t && (e->rank > 0
12053 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
12054 gfc_error ("Arithmetic IF statement at %L requires a scalar "
12055 "REAL or INTEGER expression", &e->where);
12056
12057 resolve_branch (code->label1, code);
12058 resolve_branch (code->label2, code);
12059 resolve_branch (code->label3, code);
12060 }
12061 break;
12062
12063 case EXEC_IF:
12064 if (t && code->expr1 != NULL
12065 && (code->expr1->ts.type != BT_LOGICAL
12066 || code->expr1->rank != 0))
12067 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12068 &code->expr1->where);
12069 break;
12070
12071 case EXEC_CALL:
12072 call:
12073 resolve_call (code);
12074 break;
12075
12076 case EXEC_COMPCALL:
12077 compcall:
12078 resolve_typebound_subroutine (code);
12079 break;
12080
12081 case EXEC_CALL_PPC:
12082 resolve_ppc_call (code);
12083 break;
12084
12085 case EXEC_SELECT:
12086 /* Select is complicated. Also, a SELECT construct could be
12087 a transformed computed GOTO. */
12088 resolve_select (code, false);
12089 break;
12090
12091 case EXEC_SELECT_TYPE:
12092 resolve_select_type (code, ns);
12093 break;
12094
12095 case EXEC_SELECT_RANK:
12096 resolve_select_rank (code, ns);
12097 break;
12098
12099 case EXEC_BLOCK:
12100 resolve_block_construct (code);
12101 break;
12102
12103 case EXEC_DO:
12104 if (code->ext.iterator != NULL)
12105 {
12106 gfc_iterator *iter = code->ext.iterator;
12107 if (gfc_resolve_iterator (iter, true, false))
12108 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12109 true);
12110 }
12111 break;
12112
12113 case EXEC_DO_WHILE:
12114 if (code->expr1 == NULL)
12115 gfc_internal_error ("gfc_resolve_code(): No expression on "
12116 "DO WHILE");
12117 if (t
12118 && (code->expr1->rank != 0
12119 || code->expr1->ts.type != BT_LOGICAL))
12120 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12121 "a scalar LOGICAL expression", &code->expr1->where);
12122 break;
12123
12124 case EXEC_ALLOCATE:
12125 if (t)
12126 resolve_allocate_deallocate (code, "ALLOCATE");
12127
12128 break;
12129
12130 case EXEC_DEALLOCATE:
12131 if (t)
12132 resolve_allocate_deallocate (code, "DEALLOCATE");
12133
12134 break;
12135
12136 case EXEC_OPEN:
12137 if (!gfc_resolve_open (code->ext.open, &code->loc))
12138 break;
12139
12140 resolve_branch (code->ext.open->err, code);
12141 break;
12142
12143 case EXEC_CLOSE:
12144 if (!gfc_resolve_close (code->ext.close, &code->loc))
12145 break;
12146
12147 resolve_branch (code->ext.close->err, code);
12148 break;
12149
12150 case EXEC_BACKSPACE:
12151 case EXEC_ENDFILE:
12152 case EXEC_REWIND:
12153 case EXEC_FLUSH:
12154 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12155 break;
12156
12157 resolve_branch (code->ext.filepos->err, code);
12158 break;
12159
12160 case EXEC_INQUIRE:
12161 if (!gfc_resolve_inquire (code->ext.inquire))
12162 break;
12163
12164 resolve_branch (code->ext.inquire->err, code);
12165 break;
12166
12167 case EXEC_IOLENGTH:
12168 gcc_assert (code->ext.inquire != NULL);
12169 if (!gfc_resolve_inquire (code->ext.inquire))
12170 break;
12171
12172 resolve_branch (code->ext.inquire->err, code);
12173 break;
12174
12175 case EXEC_WAIT:
12176 if (!gfc_resolve_wait (code->ext.wait))
12177 break;
12178
12179 resolve_branch (code->ext.wait->err, code);
12180 resolve_branch (code->ext.wait->end, code);
12181 resolve_branch (code->ext.wait->eor, code);
12182 break;
12183
12184 case EXEC_READ:
12185 case EXEC_WRITE:
12186 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12187 break;
12188
12189 resolve_branch (code->ext.dt->err, code);
12190 resolve_branch (code->ext.dt->end, code);
12191 resolve_branch (code->ext.dt->eor, code);
12192 break;
12193
12194 case EXEC_TRANSFER:
12195 resolve_transfer (code);
12196 break;
12197
12198 case EXEC_DO_CONCURRENT:
12199 case EXEC_FORALL:
12200 resolve_forall_iterators (code->ext.forall_iterator);
12201
12202 if (code->expr1 != NULL
12203 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12204 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12205 "expression", &code->expr1->where);
12206 break;
12207
12208 case EXEC_OACC_PARALLEL_LOOP:
12209 case EXEC_OACC_PARALLEL:
12210 case EXEC_OACC_KERNELS_LOOP:
12211 case EXEC_OACC_KERNELS:
12212 case EXEC_OACC_SERIAL_LOOP:
12213 case EXEC_OACC_SERIAL:
12214 case EXEC_OACC_DATA:
12215 case EXEC_OACC_HOST_DATA:
12216 case EXEC_OACC_LOOP:
12217 case EXEC_OACC_UPDATE:
12218 case EXEC_OACC_WAIT:
12219 case EXEC_OACC_CACHE:
12220 case EXEC_OACC_ENTER_DATA:
12221 case EXEC_OACC_EXIT_DATA:
12222 case EXEC_OACC_ATOMIC:
12223 case EXEC_OACC_DECLARE:
12224 gfc_resolve_oacc_directive (code, ns);
12225 break;
12226
12227 case EXEC_OMP_ATOMIC:
12228 case EXEC_OMP_BARRIER:
12229 case EXEC_OMP_CANCEL:
12230 case EXEC_OMP_CANCELLATION_POINT:
12231 case EXEC_OMP_CRITICAL:
12232 case EXEC_OMP_FLUSH:
12233 case EXEC_OMP_DEPOBJ:
12234 case EXEC_OMP_DISTRIBUTE:
12235 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12236 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12237 case EXEC_OMP_DISTRIBUTE_SIMD:
12238 case EXEC_OMP_DO:
12239 case EXEC_OMP_DO_SIMD:
12240 case EXEC_OMP_LOOP:
12241 case EXEC_OMP_MASTER:
12242 case EXEC_OMP_MASTER_TASKLOOP:
12243 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12244 case EXEC_OMP_ORDERED:
12245 case EXEC_OMP_SCAN:
12246 case EXEC_OMP_SECTIONS:
12247 case EXEC_OMP_SIMD:
12248 case EXEC_OMP_SINGLE:
12249 case EXEC_OMP_TARGET:
12250 case EXEC_OMP_TARGET_DATA:
12251 case EXEC_OMP_TARGET_ENTER_DATA:
12252 case EXEC_OMP_TARGET_EXIT_DATA:
12253 case EXEC_OMP_TARGET_PARALLEL:
12254 case EXEC_OMP_TARGET_PARALLEL_DO:
12255 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12256 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12257 case EXEC_OMP_TARGET_SIMD:
12258 case EXEC_OMP_TARGET_TEAMS:
12259 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12260 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12261 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12262 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12263 case EXEC_OMP_TARGET_TEAMS_LOOP:
12264 case EXEC_OMP_TARGET_UPDATE:
12265 case EXEC_OMP_TASK:
12266 case EXEC_OMP_TASKGROUP:
12267 case EXEC_OMP_TASKLOOP:
12268 case EXEC_OMP_TASKLOOP_SIMD:
12269 case EXEC_OMP_TASKWAIT:
12270 case EXEC_OMP_TASKYIELD:
12271 case EXEC_OMP_TEAMS:
12272 case EXEC_OMP_TEAMS_DISTRIBUTE:
12273 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12274 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12275 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12276 case EXEC_OMP_TEAMS_LOOP:
12277 case EXEC_OMP_WORKSHARE:
12278 gfc_resolve_omp_directive (code, ns);
12279 break;
12280
12281 case EXEC_OMP_PARALLEL:
12282 case EXEC_OMP_PARALLEL_DO:
12283 case EXEC_OMP_PARALLEL_DO_SIMD:
12284 case EXEC_OMP_PARALLEL_LOOP:
12285 case EXEC_OMP_PARALLEL_MASTER:
12286 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12287 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12288 case EXEC_OMP_PARALLEL_SECTIONS:
12289 case EXEC_OMP_PARALLEL_WORKSHARE:
12290 omp_workshare_save = omp_workshare_flag;
12291 omp_workshare_flag = 0;
12292 gfc_resolve_omp_directive (code, ns);
12293 omp_workshare_flag = omp_workshare_save;
12294 break;
12295
12296 default:
12297 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12298 }
12299 }
12300
12301 cs_base = frame.prev;
12302 }
12303
12304
12305 /* Resolve initial values and make sure they are compatible with
12306 the variable. */
12307
12308 static void
12309 resolve_values (gfc_symbol *sym)
12310 {
12311 bool t;
12312
12313 if (sym->value == NULL)
12314 return;
12315
12316 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
12317 gfc_warning (OPT_Wdeprecated_declarations,
12318 "Using parameter %qs declared at %L is deprecated",
12319 sym->name, &sym->declared_at);
12320
12321 if (sym->value->expr_type == EXPR_STRUCTURE)
12322 t= resolve_structure_cons (sym->value, 1);
12323 else
12324 t = gfc_resolve_expr (sym->value);
12325
12326 if (!t)
12327 return;
12328
12329 gfc_check_assign_symbol (sym, NULL, sym->value);
12330 }
12331
12332
12333 /* Verify any BIND(C) derived types in the namespace so we can report errors
12334 for them once, rather than for each variable declared of that type. */
12335
12336 static void
12337 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12338 {
12339 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12340 && derived_sym->attr.is_bind_c == 1)
12341 verify_bind_c_derived_type (derived_sym);
12342
12343 return;
12344 }
12345
12346
12347 /* Check the interfaces of DTIO procedures associated with derived
12348 type 'sym'. These procedures can either have typebound bindings or
12349 can appear in DTIO generic interfaces. */
12350
12351 static void
12352 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12353 {
12354 if (!sym || sym->attr.flavor != FL_DERIVED)
12355 return;
12356
12357 gfc_check_dtio_interfaces (sym);
12358
12359 return;
12360 }
12361
12362 /* Verify that any binding labels used in a given namespace do not collide
12363 with the names or binding labels of any global symbols. Multiple INTERFACE
12364 for the same procedure are permitted. */
12365
12366 static void
12367 gfc_verify_binding_labels (gfc_symbol *sym)
12368 {
12369 gfc_gsymbol *gsym;
12370 const char *module;
12371
12372 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12373 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12374 return;
12375
12376 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12377
12378 if (sym->module)
12379 module = sym->module;
12380 else if (sym->ns && sym->ns->proc_name
12381 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12382 module = sym->ns->proc_name->name;
12383 else if (sym->ns && sym->ns->parent
12384 && sym->ns && sym->ns->parent->proc_name
12385 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12386 module = sym->ns->parent->proc_name->name;
12387 else
12388 module = NULL;
12389
12390 if (!gsym
12391 || (!gsym->defined
12392 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12393 {
12394 if (!gsym)
12395 gsym = gfc_get_gsymbol (sym->binding_label, true);
12396 gsym->where = sym->declared_at;
12397 gsym->sym_name = sym->name;
12398 gsym->binding_label = sym->binding_label;
12399 gsym->ns = sym->ns;
12400 gsym->mod_name = module;
12401 if (sym->attr.function)
12402 gsym->type = GSYM_FUNCTION;
12403 else if (sym->attr.subroutine)
12404 gsym->type = GSYM_SUBROUTINE;
12405 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12406 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12407 return;
12408 }
12409
12410 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12411 {
12412 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12413 "identifier as entity at %L", sym->name,
12414 sym->binding_label, &sym->declared_at, &gsym->where);
12415 /* Clear the binding label to prevent checking multiple times. */
12416 sym->binding_label = NULL;
12417 return;
12418 }
12419
12420 if (sym->attr.flavor == FL_VARIABLE && module
12421 && (strcmp (module, gsym->mod_name) != 0
12422 || strcmp (sym->name, gsym->sym_name) != 0))
12423 {
12424 /* This can only happen if the variable is defined in a module - if it
12425 isn't the same module, reject it. */
12426 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12427 "uses the same global identifier as entity at %L from module %qs",
12428 sym->name, module, sym->binding_label,
12429 &sym->declared_at, &gsym->where, gsym->mod_name);
12430 sym->binding_label = NULL;
12431 return;
12432 }
12433
12434 if ((sym->attr.function || sym->attr.subroutine)
12435 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12436 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12437 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12438 && (module != gsym->mod_name
12439 || strcmp (gsym->sym_name, sym->name) != 0
12440 || (module && strcmp (module, gsym->mod_name) != 0)))
12441 {
12442 /* Print an error if the procedure is defined multiple times; we have to
12443 exclude references to the same procedure via module association or
12444 multiple checks for the same procedure. */
12445 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12446 "global identifier as entity at %L", sym->name,
12447 sym->binding_label, &sym->declared_at, &gsym->where);
12448 sym->binding_label = NULL;
12449 }
12450 }
12451
12452
12453 /* Resolve an index expression. */
12454
12455 static bool
12456 resolve_index_expr (gfc_expr *e)
12457 {
12458 if (!gfc_resolve_expr (e))
12459 return false;
12460
12461 if (!gfc_simplify_expr (e, 0))
12462 return false;
12463
12464 if (!gfc_specification_expr (e))
12465 return false;
12466
12467 return true;
12468 }
12469
12470
12471 /* Resolve a charlen structure. */
12472
12473 static bool
12474 resolve_charlen (gfc_charlen *cl)
12475 {
12476 int k;
12477 bool saved_specification_expr;
12478
12479 if (cl->resolved)
12480 return true;
12481
12482 cl->resolved = 1;
12483 saved_specification_expr = specification_expr;
12484 specification_expr = true;
12485
12486 if (cl->length_from_typespec)
12487 {
12488 if (!gfc_resolve_expr (cl->length))
12489 {
12490 specification_expr = saved_specification_expr;
12491 return false;
12492 }
12493
12494 if (!gfc_simplify_expr (cl->length, 0))
12495 {
12496 specification_expr = saved_specification_expr;
12497 return false;
12498 }
12499
12500 /* cl->length has been resolved. It should have an integer type. */
12501 if (cl->length
12502 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
12503 {
12504 gfc_error ("Scalar INTEGER expression expected at %L",
12505 &cl->length->where);
12506 return false;
12507 }
12508 }
12509 else
12510 {
12511 if (!resolve_index_expr (cl->length))
12512 {
12513 specification_expr = saved_specification_expr;
12514 return false;
12515 }
12516 }
12517
12518 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12519 a negative value, the length of character entities declared is zero. */
12520 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12521 && mpz_sgn (cl->length->value.integer) < 0)
12522 gfc_replace_expr (cl->length,
12523 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12524
12525 /* Check that the character length is not too large. */
12526 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12527 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12528 && cl->length->ts.type == BT_INTEGER
12529 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12530 {
12531 gfc_error ("String length at %L is too large", &cl->length->where);
12532 specification_expr = saved_specification_expr;
12533 return false;
12534 }
12535
12536 specification_expr = saved_specification_expr;
12537 return true;
12538 }
12539
12540
12541 /* Test for non-constant shape arrays. */
12542
12543 static bool
12544 is_non_constant_shape_array (gfc_symbol *sym)
12545 {
12546 gfc_expr *e;
12547 int i;
12548 bool not_constant;
12549
12550 not_constant = false;
12551 if (sym->as != NULL)
12552 {
12553 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12554 has not been simplified; parameter array references. Do the
12555 simplification now. */
12556 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12557 {
12558 if (i == GFC_MAX_DIMENSIONS)
12559 break;
12560
12561 e = sym->as->lower[i];
12562 if (e && (!resolve_index_expr(e)
12563 || !gfc_is_constant_expr (e)))
12564 not_constant = true;
12565 e = sym->as->upper[i];
12566 if (e && (!resolve_index_expr(e)
12567 || !gfc_is_constant_expr (e)))
12568 not_constant = true;
12569 }
12570 }
12571 return not_constant;
12572 }
12573
12574 /* Given a symbol and an initialization expression, add code to initialize
12575 the symbol to the function entry. */
12576 static void
12577 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12578 {
12579 gfc_expr *lval;
12580 gfc_code *init_st;
12581 gfc_namespace *ns = sym->ns;
12582
12583 /* Search for the function namespace if this is a contained
12584 function without an explicit result. */
12585 if (sym->attr.function && sym == sym->result
12586 && sym->name != sym->ns->proc_name->name)
12587 {
12588 ns = ns->contained;
12589 for (;ns; ns = ns->sibling)
12590 if (strcmp (ns->proc_name->name, sym->name) == 0)
12591 break;
12592 }
12593
12594 if (ns == NULL)
12595 {
12596 gfc_free_expr (init);
12597 return;
12598 }
12599
12600 /* Build an l-value expression for the result. */
12601 lval = gfc_lval_expr_from_sym (sym);
12602
12603 /* Add the code at scope entry. */
12604 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12605 init_st->next = ns->code;
12606 ns->code = init_st;
12607
12608 /* Assign the default initializer to the l-value. */
12609 init_st->loc = sym->declared_at;
12610 init_st->expr1 = lval;
12611 init_st->expr2 = init;
12612 }
12613
12614
12615 /* Whether or not we can generate a default initializer for a symbol. */
12616
12617 static bool
12618 can_generate_init (gfc_symbol *sym)
12619 {
12620 symbol_attribute *a;
12621 if (!sym)
12622 return false;
12623 a = &sym->attr;
12624
12625 /* These symbols should never have a default initialization. */
12626 return !(
12627 a->allocatable
12628 || a->external
12629 || a->pointer
12630 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12631 && (CLASS_DATA (sym)->attr.class_pointer
12632 || CLASS_DATA (sym)->attr.proc_pointer))
12633 || a->in_equivalence
12634 || a->in_common
12635 || a->data
12636 || sym->module
12637 || a->cray_pointee
12638 || a->cray_pointer
12639 || sym->assoc
12640 || (!a->referenced && !a->result)
12641 || (a->dummy && a->intent != INTENT_OUT)
12642 || (a->function && sym != sym->result)
12643 );
12644 }
12645
12646
12647 /* Assign the default initializer to a derived type variable or result. */
12648
12649 static void
12650 apply_default_init (gfc_symbol *sym)
12651 {
12652 gfc_expr *init = NULL;
12653
12654 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12655 return;
12656
12657 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12658 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12659
12660 if (init == NULL && sym->ts.type != BT_CLASS)
12661 return;
12662
12663 build_init_assign (sym, init);
12664 sym->attr.referenced = 1;
12665 }
12666
12667
12668 /* Build an initializer for a local. Returns null if the symbol should not have
12669 a default initialization. */
12670
12671 static gfc_expr *
12672 build_default_init_expr (gfc_symbol *sym)
12673 {
12674 /* These symbols should never have a default initialization. */
12675 if (sym->attr.allocatable
12676 || sym->attr.external
12677 || sym->attr.dummy
12678 || sym->attr.pointer
12679 || sym->attr.in_equivalence
12680 || sym->attr.in_common
12681 || sym->attr.data
12682 || sym->module
12683 || sym->attr.cray_pointee
12684 || sym->attr.cray_pointer
12685 || sym->assoc)
12686 return NULL;
12687
12688 /* Get the appropriate init expression. */
12689 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12690 }
12691
12692 /* Add an initialization expression to a local variable. */
12693 static void
12694 apply_default_init_local (gfc_symbol *sym)
12695 {
12696 gfc_expr *init = NULL;
12697
12698 /* The symbol should be a variable or a function return value. */
12699 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12700 || (sym->attr.function && sym->result != sym))
12701 return;
12702
12703 /* Try to build the initializer expression. If we can't initialize
12704 this symbol, then init will be NULL. */
12705 init = build_default_init_expr (sym);
12706 if (init == NULL)
12707 return;
12708
12709 /* For saved variables, we don't want to add an initializer at function
12710 entry, so we just add a static initializer. Note that automatic variables
12711 are stack allocated even with -fno-automatic; we have also to exclude
12712 result variable, which are also nonstatic. */
12713 if (!sym->attr.automatic
12714 && (sym->attr.save || sym->ns->save_all
12715 || (flag_max_stack_var_size == 0 && !sym->attr.result
12716 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12717 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12718 {
12719 /* Don't clobber an existing initializer! */
12720 gcc_assert (sym->value == NULL);
12721 sym->value = init;
12722 return;
12723 }
12724
12725 build_init_assign (sym, init);
12726 }
12727
12728
12729 /* Resolution of common features of flavors variable and procedure. */
12730
12731 static bool
12732 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12733 {
12734 gfc_array_spec *as;
12735
12736 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12737 && sym->ts.u.derived && CLASS_DATA (sym))
12738 as = CLASS_DATA (sym)->as;
12739 else
12740 as = sym->as;
12741
12742 /* Constraints on deferred shape variable. */
12743 if (as == NULL || as->type != AS_DEFERRED)
12744 {
12745 bool pointer, allocatable, dimension;
12746
12747 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12748 && sym->ts.u.derived && CLASS_DATA (sym))
12749 {
12750 pointer = CLASS_DATA (sym)->attr.class_pointer;
12751 allocatable = CLASS_DATA (sym)->attr.allocatable;
12752 dimension = CLASS_DATA (sym)->attr.dimension;
12753 }
12754 else
12755 {
12756 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12757 allocatable = sym->attr.allocatable;
12758 dimension = sym->attr.dimension;
12759 }
12760
12761 if (allocatable)
12762 {
12763 if (dimension && as->type != AS_ASSUMED_RANK)
12764 {
12765 gfc_error ("Allocatable array %qs at %L must have a deferred "
12766 "shape or assumed rank", sym->name, &sym->declared_at);
12767 return false;
12768 }
12769 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12770 "%qs at %L may not be ALLOCATABLE",
12771 sym->name, &sym->declared_at))
12772 return false;
12773 }
12774
12775 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12776 {
12777 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12778 "assumed rank", sym->name, &sym->declared_at);
12779 sym->error = 1;
12780 return false;
12781 }
12782 }
12783 else
12784 {
12785 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12786 && sym->ts.type != BT_CLASS && !sym->assoc)
12787 {
12788 gfc_error ("Array %qs at %L cannot have a deferred shape",
12789 sym->name, &sym->declared_at);
12790 return false;
12791 }
12792 }
12793
12794 /* Constraints on polymorphic variables. */
12795 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12796 {
12797 /* F03:C502. */
12798 if (sym->attr.class_ok
12799 && sym->ts.u.derived
12800 && !sym->attr.select_type_temporary
12801 && !UNLIMITED_POLY (sym)
12802 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12803 {
12804 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12805 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12806 &sym->declared_at);
12807 return false;
12808 }
12809
12810 /* F03:C509. */
12811 /* Assume that use associated symbols were checked in the module ns.
12812 Class-variables that are associate-names are also something special
12813 and excepted from the test. */
12814 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12815 {
12816 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12817 "or pointer", sym->name, &sym->declared_at);
12818 return false;
12819 }
12820 }
12821
12822 return true;
12823 }
12824
12825
12826 /* Additional checks for symbols with flavor variable and derived
12827 type. To be called from resolve_fl_variable. */
12828
12829 static bool
12830 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12831 {
12832 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12833
12834 /* Check to see if a derived type is blocked from being host
12835 associated by the presence of another class I symbol in the same
12836 namespace. 14.6.1.3 of the standard and the discussion on
12837 comp.lang.fortran. */
12838 if (sym->ts.u.derived
12839 && sym->ns != sym->ts.u.derived->ns
12840 && !sym->ts.u.derived->attr.use_assoc
12841 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12842 {
12843 gfc_symbol *s;
12844 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12845 if (s && s->attr.generic)
12846 s = gfc_find_dt_in_generic (s);
12847 if (s && !gfc_fl_struct (s->attr.flavor))
12848 {
12849 gfc_error ("The type %qs cannot be host associated at %L "
12850 "because it is blocked by an incompatible object "
12851 "of the same name declared at %L",
12852 sym->ts.u.derived->name, &sym->declared_at,
12853 &s->declared_at);
12854 return false;
12855 }
12856 }
12857
12858 /* 4th constraint in section 11.3: "If an object of a type for which
12859 component-initialization is specified (R429) appears in the
12860 specification-part of a module and does not have the ALLOCATABLE
12861 or POINTER attribute, the object shall have the SAVE attribute."
12862
12863 The check for initializers is performed with
12864 gfc_has_default_initializer because gfc_default_initializer generates
12865 a hidden default for allocatable components. */
12866 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12867 && sym->ns->proc_name->attr.flavor == FL_MODULE
12868 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12869 && !sym->attr.pointer && !sym->attr.allocatable
12870 && gfc_has_default_initializer (sym->ts.u.derived)
12871 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12872 "%qs at %L, needed due to the default "
12873 "initialization", sym->name, &sym->declared_at))
12874 return false;
12875
12876 /* Assign default initializer. */
12877 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12878 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12879 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12880
12881 return true;
12882 }
12883
12884
12885 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12886 except in the declaration of an entity or component that has the POINTER
12887 or ALLOCATABLE attribute. */
12888
12889 static bool
12890 deferred_requirements (gfc_symbol *sym)
12891 {
12892 if (sym->ts.deferred
12893 && !(sym->attr.pointer
12894 || sym->attr.allocatable
12895 || sym->attr.associate_var
12896 || sym->attr.omp_udr_artificial_var))
12897 {
12898 /* If a function has a result variable, only check the variable. */
12899 if (sym->result && sym->name != sym->result->name)
12900 return true;
12901
12902 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12903 "requires either the POINTER or ALLOCATABLE attribute",
12904 sym->name, &sym->declared_at);
12905 return false;
12906 }
12907 return true;
12908 }
12909
12910
12911 /* Resolve symbols with flavor variable. */
12912
12913 static bool
12914 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12915 {
12916 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12917 "SAVE attribute";
12918
12919 if (!resolve_fl_var_and_proc (sym, mp_flag))
12920 return false;
12921
12922 /* Set this flag to check that variables are parameters of all entries.
12923 This check is effected by the call to gfc_resolve_expr through
12924 is_non_constant_shape_array. */
12925 bool saved_specification_expr = specification_expr;
12926 specification_expr = true;
12927
12928 if (sym->ns->proc_name
12929 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12930 || sym->ns->proc_name->attr.is_main_program)
12931 && !sym->attr.use_assoc
12932 && !sym->attr.allocatable
12933 && !sym->attr.pointer
12934 && is_non_constant_shape_array (sym))
12935 {
12936 /* F08:C541. The shape of an array defined in a main program or module
12937 * needs to be constant. */
12938 gfc_error ("The module or main program array %qs at %L must "
12939 "have constant shape", sym->name, &sym->declared_at);
12940 specification_expr = saved_specification_expr;
12941 return false;
12942 }
12943
12944 /* Constraints on deferred type parameter. */
12945 if (!deferred_requirements (sym))
12946 return false;
12947
12948 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12949 {
12950 /* Make sure that character string variables with assumed length are
12951 dummy arguments. */
12952 gfc_expr *e = NULL;
12953
12954 if (sym->ts.u.cl)
12955 e = sym->ts.u.cl->length;
12956 else
12957 return false;
12958
12959 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12960 && !sym->ts.deferred && !sym->attr.select_type_temporary
12961 && !sym->attr.omp_udr_artificial_var)
12962 {
12963 gfc_error ("Entity with assumed character length at %L must be a "
12964 "dummy argument or a PARAMETER", &sym->declared_at);
12965 specification_expr = saved_specification_expr;
12966 return false;
12967 }
12968
12969 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12970 {
12971 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12972 specification_expr = saved_specification_expr;
12973 return false;
12974 }
12975
12976 if (!gfc_is_constant_expr (e)
12977 && !(e->expr_type == EXPR_VARIABLE
12978 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12979 {
12980 if (!sym->attr.use_assoc && sym->ns->proc_name
12981 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12982 || sym->ns->proc_name->attr.is_main_program))
12983 {
12984 gfc_error ("%qs at %L must have constant character length "
12985 "in this context", sym->name, &sym->declared_at);
12986 specification_expr = saved_specification_expr;
12987 return false;
12988 }
12989 if (sym->attr.in_common)
12990 {
12991 gfc_error ("COMMON variable %qs at %L must have constant "
12992 "character length", sym->name, &sym->declared_at);
12993 specification_expr = saved_specification_expr;
12994 return false;
12995 }
12996 }
12997 }
12998
12999 if (sym->value == NULL && sym->attr.referenced)
13000 apply_default_init_local (sym); /* Try to apply a default initialization. */
13001
13002 /* Determine if the symbol may not have an initializer. */
13003 int no_init_flag = 0, automatic_flag = 0;
13004 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
13005 || sym->attr.intrinsic || sym->attr.result)
13006 no_init_flag = 1;
13007 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
13008 && is_non_constant_shape_array (sym))
13009 {
13010 no_init_flag = automatic_flag = 1;
13011
13012 /* Also, they must not have the SAVE attribute.
13013 SAVE_IMPLICIT is checked below. */
13014 if (sym->as && sym->attr.codimension)
13015 {
13016 int corank = sym->as->corank;
13017 sym->as->corank = 0;
13018 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
13019 sym->as->corank = corank;
13020 }
13021 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
13022 {
13023 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
13024 specification_expr = saved_specification_expr;
13025 return false;
13026 }
13027 }
13028
13029 /* Ensure that any initializer is simplified. */
13030 if (sym->value)
13031 gfc_simplify_expr (sym->value, 1);
13032
13033 /* Reject illegal initializers. */
13034 if (!sym->mark && sym->value)
13035 {
13036 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
13037 && CLASS_DATA (sym)->attr.allocatable))
13038 gfc_error ("Allocatable %qs at %L cannot have an initializer",
13039 sym->name, &sym->declared_at);
13040 else if (sym->attr.external)
13041 gfc_error ("External %qs at %L cannot have an initializer",
13042 sym->name, &sym->declared_at);
13043 else if (sym->attr.dummy)
13044 gfc_error ("Dummy %qs at %L cannot have an initializer",
13045 sym->name, &sym->declared_at);
13046 else if (sym->attr.intrinsic)
13047 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
13048 sym->name, &sym->declared_at);
13049 else if (sym->attr.result)
13050 gfc_error ("Function result %qs at %L cannot have an initializer",
13051 sym->name, &sym->declared_at);
13052 else if (automatic_flag)
13053 gfc_error ("Automatic array %qs at %L cannot have an initializer",
13054 sym->name, &sym->declared_at);
13055 else
13056 goto no_init_error;
13057 specification_expr = saved_specification_expr;
13058 return false;
13059 }
13060
13061 no_init_error:
13062 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
13063 {
13064 bool res = resolve_fl_variable_derived (sym, no_init_flag);
13065 specification_expr = saved_specification_expr;
13066 return res;
13067 }
13068
13069 specification_expr = saved_specification_expr;
13070 return true;
13071 }
13072
13073
13074 /* Compare the dummy characteristics of a module procedure interface
13075 declaration with the corresponding declaration in a submodule. */
13076 static gfc_formal_arglist *new_formal;
13077 static char errmsg[200];
13078
13079 static void
13080 compare_fsyms (gfc_symbol *sym)
13081 {
13082 gfc_symbol *fsym;
13083
13084 if (sym == NULL || new_formal == NULL)
13085 return;
13086
13087 fsym = new_formal->sym;
13088
13089 if (sym == fsym)
13090 return;
13091
13092 if (strcmp (sym->name, fsym->name) == 0)
13093 {
13094 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
13095 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
13096 }
13097 }
13098
13099
13100 /* Resolve a procedure. */
13101
13102 static bool
13103 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
13104 {
13105 gfc_formal_arglist *arg;
13106 bool allocatable_or_pointer;
13107
13108 if (sym->attr.function
13109 && !resolve_fl_var_and_proc (sym, mp_flag))
13110 return false;
13111
13112 /* Constraints on deferred type parameter. */
13113 if (!deferred_requirements (sym))
13114 return false;
13115
13116 if (sym->ts.type == BT_CHARACTER)
13117 {
13118 gfc_charlen *cl = sym->ts.u.cl;
13119
13120 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13121 && !resolve_charlen (cl))
13122 return false;
13123
13124 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13125 && sym->attr.proc == PROC_ST_FUNCTION)
13126 {
13127 gfc_error ("Character-valued statement function %qs at %L must "
13128 "have constant length", sym->name, &sym->declared_at);
13129 return false;
13130 }
13131 }
13132
13133 /* Ensure that derived type for are not of a private type. Internal
13134 module procedures are excluded by 2.2.3.3 - i.e., they are not
13135 externally accessible and can access all the objects accessible in
13136 the host. */
13137 if (!(sym->ns->parent && sym->ns->parent->proc_name
13138 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13139 && gfc_check_symbol_access (sym))
13140 {
13141 gfc_interface *iface;
13142
13143 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13144 {
13145 if (arg->sym
13146 && arg->sym->ts.type == BT_DERIVED
13147 && arg->sym->ts.u.derived
13148 && !arg->sym->ts.u.derived->attr.use_assoc
13149 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13150 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13151 "and cannot be a dummy argument"
13152 " of %qs, which is PUBLIC at %L",
13153 arg->sym->name, sym->name,
13154 &sym->declared_at))
13155 {
13156 /* Stop this message from recurring. */
13157 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13158 return false;
13159 }
13160 }
13161
13162 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13163 PRIVATE to the containing module. */
13164 for (iface = sym->generic; iface; iface = iface->next)
13165 {
13166 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13167 {
13168 if (arg->sym
13169 && arg->sym->ts.type == BT_DERIVED
13170 && !arg->sym->ts.u.derived->attr.use_assoc
13171 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13172 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13173 "PUBLIC interface %qs at %L "
13174 "takes dummy arguments of %qs which "
13175 "is PRIVATE", iface->sym->name,
13176 sym->name, &iface->sym->declared_at,
13177 gfc_typename(&arg->sym->ts)))
13178 {
13179 /* Stop this message from recurring. */
13180 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13181 return false;
13182 }
13183 }
13184 }
13185 }
13186
13187 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13188 && !sym->attr.proc_pointer)
13189 {
13190 gfc_error ("Function %qs at %L cannot have an initializer",
13191 sym->name, &sym->declared_at);
13192
13193 /* Make sure no second error is issued for this. */
13194 sym->value->error = 1;
13195 return false;
13196 }
13197
13198 /* An external symbol may not have an initializer because it is taken to be
13199 a procedure. Exception: Procedure Pointers. */
13200 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13201 {
13202 gfc_error ("External object %qs at %L may not have an initializer",
13203 sym->name, &sym->declared_at);
13204 return false;
13205 }
13206
13207 /* An elemental function is required to return a scalar 12.7.1 */
13208 if (sym->attr.elemental && sym->attr.function
13209 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13210 {
13211 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13212 "result", sym->name, &sym->declared_at);
13213 /* Reset so that the error only occurs once. */
13214 sym->attr.elemental = 0;
13215 return false;
13216 }
13217
13218 if (sym->attr.proc == PROC_ST_FUNCTION
13219 && (sym->attr.allocatable || sym->attr.pointer))
13220 {
13221 gfc_error ("Statement function %qs at %L may not have pointer or "
13222 "allocatable attribute", sym->name, &sym->declared_at);
13223 return false;
13224 }
13225
13226 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13227 char-len-param shall not be array-valued, pointer-valued, recursive
13228 or pure. ....snip... A character value of * may only be used in the
13229 following ways: (i) Dummy arg of procedure - dummy associates with
13230 actual length; (ii) To declare a named constant; or (iii) External
13231 function - but length must be declared in calling scoping unit. */
13232 if (sym->attr.function
13233 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13234 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13235 {
13236 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13237 || (sym->attr.recursive) || (sym->attr.pure))
13238 {
13239 if (sym->as && sym->as->rank)
13240 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13241 "array-valued", sym->name, &sym->declared_at);
13242
13243 if (sym->attr.pointer)
13244 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13245 "pointer-valued", sym->name, &sym->declared_at);
13246
13247 if (sym->attr.pure)
13248 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13249 "pure", sym->name, &sym->declared_at);
13250
13251 if (sym->attr.recursive)
13252 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13253 "recursive", sym->name, &sym->declared_at);
13254
13255 return false;
13256 }
13257
13258 /* Appendix B.2 of the standard. Contained functions give an
13259 error anyway. Deferred character length is an F2003 feature.
13260 Don't warn on intrinsic conversion functions, which start
13261 with two underscores. */
13262 if (!sym->attr.contained && !sym->ts.deferred
13263 && (sym->name[0] != '_' || sym->name[1] != '_'))
13264 gfc_notify_std (GFC_STD_F95_OBS,
13265 "CHARACTER(*) function %qs at %L",
13266 sym->name, &sym->declared_at);
13267 }
13268
13269 /* F2008, C1218. */
13270 if (sym->attr.elemental)
13271 {
13272 if (sym->attr.proc_pointer)
13273 {
13274 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13275 : sym->name);
13276 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13277 name, &sym->declared_at);
13278 return false;
13279 }
13280 if (sym->attr.dummy)
13281 {
13282 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13283 sym->name, &sym->declared_at);
13284 return false;
13285 }
13286 }
13287
13288 /* F2018, C15100: "The result of an elemental function shall be scalar,
13289 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13290 pointer is tested and caught elsewhere. */
13291 if (sym->result)
13292 allocatable_or_pointer = sym->result->ts.type == BT_CLASS
13293 && CLASS_DATA (sym->result) ?
13294 (CLASS_DATA (sym->result)->attr.allocatable
13295 || CLASS_DATA (sym->result)->attr.pointer) :
13296 (sym->result->attr.allocatable
13297 || sym->result->attr.pointer);
13298
13299 if (sym->attr.elemental && sym->result
13300 && allocatable_or_pointer)
13301 {
13302 gfc_error ("Function result variable %qs at %L of elemental "
13303 "function %qs shall not have an ALLOCATABLE or POINTER "
13304 "attribute", sym->result->name,
13305 &sym->result->declared_at, sym->name);
13306 return false;
13307 }
13308
13309 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13310 {
13311 gfc_formal_arglist *curr_arg;
13312 int has_non_interop_arg = 0;
13313
13314 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13315 sym->common_block))
13316 {
13317 /* Clear these to prevent looking at them again if there was an
13318 error. */
13319 sym->attr.is_bind_c = 0;
13320 sym->attr.is_c_interop = 0;
13321 sym->ts.is_c_interop = 0;
13322 }
13323 else
13324 {
13325 /* So far, no errors have been found. */
13326 sym->attr.is_c_interop = 1;
13327 sym->ts.is_c_interop = 1;
13328 }
13329
13330 curr_arg = gfc_sym_get_dummy_args (sym);
13331 while (curr_arg != NULL)
13332 {
13333 /* Skip implicitly typed dummy args here. */
13334 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13335 if (!gfc_verify_c_interop_param (curr_arg->sym))
13336 /* If something is found to fail, record the fact so we
13337 can mark the symbol for the procedure as not being
13338 BIND(C) to try and prevent multiple errors being
13339 reported. */
13340 has_non_interop_arg = 1;
13341
13342 curr_arg = curr_arg->next;
13343 }
13344
13345 /* See if any of the arguments were not interoperable and if so, clear
13346 the procedure symbol to prevent duplicate error messages. */
13347 if (has_non_interop_arg != 0)
13348 {
13349 sym->attr.is_c_interop = 0;
13350 sym->ts.is_c_interop = 0;
13351 sym->attr.is_bind_c = 0;
13352 }
13353 }
13354
13355 if (!sym->attr.proc_pointer)
13356 {
13357 if (sym->attr.save == SAVE_EXPLICIT)
13358 {
13359 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13360 "in %qs at %L", sym->name, &sym->declared_at);
13361 return false;
13362 }
13363 if (sym->attr.intent)
13364 {
13365 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13366 "in %qs at %L", sym->name, &sym->declared_at);
13367 return false;
13368 }
13369 if (sym->attr.subroutine && sym->attr.result)
13370 {
13371 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13372 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13373 return false;
13374 }
13375 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13376 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13377 || sym->attr.contained))
13378 {
13379 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13380 "in %qs at %L", sym->name, &sym->declared_at);
13381 return false;
13382 }
13383 if (strcmp ("ppr@", sym->name) == 0)
13384 {
13385 gfc_error ("Procedure pointer result %qs at %L "
13386 "is missing the pointer attribute",
13387 sym->ns->proc_name->name, &sym->declared_at);
13388 return false;
13389 }
13390 }
13391
13392 /* Assume that a procedure whose body is not known has references
13393 to external arrays. */
13394 if (sym->attr.if_source != IFSRC_DECL)
13395 sym->attr.array_outer_dependency = 1;
13396
13397 /* Compare the characteristics of a module procedure with the
13398 interface declaration. Ideally this would be done with
13399 gfc_compare_interfaces but, at present, the formal interface
13400 cannot be copied to the ts.interface. */
13401 if (sym->attr.module_procedure
13402 && sym->attr.if_source == IFSRC_DECL)
13403 {
13404 gfc_symbol *iface;
13405 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13406 char *module_name;
13407 char *submodule_name;
13408 strcpy (name, sym->ns->proc_name->name);
13409 module_name = strtok (name, ".");
13410 submodule_name = strtok (NULL, ".");
13411
13412 iface = sym->tlink;
13413 sym->tlink = NULL;
13414
13415 /* Make sure that the result uses the correct charlen for deferred
13416 length results. */
13417 if (iface && sym->result
13418 && iface->ts.type == BT_CHARACTER
13419 && iface->ts.deferred)
13420 sym->result->ts.u.cl = iface->ts.u.cl;
13421
13422 if (iface == NULL)
13423 goto check_formal;
13424
13425 /* Check the procedure characteristics. */
13426 if (sym->attr.elemental != iface->attr.elemental)
13427 {
13428 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13429 "PROCEDURE at %L and its interface in %s",
13430 &sym->declared_at, module_name);
13431 return false;
13432 }
13433
13434 if (sym->attr.pure != iface->attr.pure)
13435 {
13436 gfc_error ("Mismatch in PURE attribute between MODULE "
13437 "PROCEDURE at %L and its interface in %s",
13438 &sym->declared_at, module_name);
13439 return false;
13440 }
13441
13442 if (sym->attr.recursive != iface->attr.recursive)
13443 {
13444 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13445 "PROCEDURE at %L and its interface in %s",
13446 &sym->declared_at, module_name);
13447 return false;
13448 }
13449
13450 /* Check the result characteristics. */
13451 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13452 {
13453 gfc_error ("%s between the MODULE PROCEDURE declaration "
13454 "in MODULE %qs and the declaration at %L in "
13455 "(SUB)MODULE %qs",
13456 errmsg, module_name, &sym->declared_at,
13457 submodule_name ? submodule_name : module_name);
13458 return false;
13459 }
13460
13461 check_formal:
13462 /* Check the characteristics of the formal arguments. */
13463 if (sym->formal && sym->formal_ns)
13464 {
13465 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13466 {
13467 new_formal = arg;
13468 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13469 }
13470 }
13471 }
13472 return true;
13473 }
13474
13475
13476 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13477 been defined and we now know their defined arguments, check that they fulfill
13478 the requirements of the standard for procedures used as finalizers. */
13479
13480 static bool
13481 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13482 {
13483 gfc_finalizer* list;
13484 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13485 bool result = true;
13486 bool seen_scalar = false;
13487 gfc_symbol *vtab;
13488 gfc_component *c;
13489 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13490
13491 if (parent)
13492 gfc_resolve_finalizers (parent, finalizable);
13493
13494 /* Ensure that derived-type components have a their finalizers resolved. */
13495 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13496 for (c = derived->components; c; c = c->next)
13497 if (c->ts.type == BT_DERIVED
13498 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13499 {
13500 bool has_final2 = false;
13501 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13502 return false; /* Error. */
13503 has_final = has_final || has_final2;
13504 }
13505 /* Return early if not finalizable. */
13506 if (!has_final)
13507 {
13508 if (finalizable)
13509 *finalizable = false;
13510 return true;
13511 }
13512
13513 /* Walk over the list of finalizer-procedures, check them, and if any one
13514 does not fit in with the standard's definition, print an error and remove
13515 it from the list. */
13516 prev_link = &derived->f2k_derived->finalizers;
13517 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13518 {
13519 gfc_formal_arglist *dummy_args;
13520 gfc_symbol* arg;
13521 gfc_finalizer* i;
13522 int my_rank;
13523
13524 /* Skip this finalizer if we already resolved it. */
13525 if (list->proc_tree)
13526 {
13527 if (list->proc_tree->n.sym->formal->sym->as == NULL
13528 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13529 seen_scalar = true;
13530 prev_link = &(list->next);
13531 continue;
13532 }
13533
13534 /* Check this exists and is a SUBROUTINE. */
13535 if (!list->proc_sym->attr.subroutine)
13536 {
13537 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13538 list->proc_sym->name, &list->where);
13539 goto error;
13540 }
13541
13542 /* We should have exactly one argument. */
13543 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13544 if (!dummy_args || dummy_args->next)
13545 {
13546 gfc_error ("FINAL procedure at %L must have exactly one argument",
13547 &list->where);
13548 goto error;
13549 }
13550 arg = dummy_args->sym;
13551
13552 /* This argument must be of our type. */
13553 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13554 {
13555 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13556 &arg->declared_at, derived->name);
13557 goto error;
13558 }
13559
13560 /* It must neither be a pointer nor allocatable nor optional. */
13561 if (arg->attr.pointer)
13562 {
13563 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13564 &arg->declared_at);
13565 goto error;
13566 }
13567 if (arg->attr.allocatable)
13568 {
13569 gfc_error ("Argument of FINAL procedure at %L must not be"
13570 " ALLOCATABLE", &arg->declared_at);
13571 goto error;
13572 }
13573 if (arg->attr.optional)
13574 {
13575 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13576 &arg->declared_at);
13577 goto error;
13578 }
13579
13580 /* It must not be INTENT(OUT). */
13581 if (arg->attr.intent == INTENT_OUT)
13582 {
13583 gfc_error ("Argument of FINAL procedure at %L must not be"
13584 " INTENT(OUT)", &arg->declared_at);
13585 goto error;
13586 }
13587
13588 /* Warn if the procedure is non-scalar and not assumed shape. */
13589 if (warn_surprising && arg->as && arg->as->rank != 0
13590 && arg->as->type != AS_ASSUMED_SHAPE)
13591 gfc_warning (OPT_Wsurprising,
13592 "Non-scalar FINAL procedure at %L should have assumed"
13593 " shape argument", &arg->declared_at);
13594
13595 /* Check that it does not match in kind and rank with a FINAL procedure
13596 defined earlier. To really loop over the *earlier* declarations,
13597 we need to walk the tail of the list as new ones were pushed at the
13598 front. */
13599 /* TODO: Handle kind parameters once they are implemented. */
13600 my_rank = (arg->as ? arg->as->rank : 0);
13601 for (i = list->next; i; i = i->next)
13602 {
13603 gfc_formal_arglist *dummy_args;
13604
13605 /* Argument list might be empty; that is an error signalled earlier,
13606 but we nevertheless continued resolving. */
13607 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13608 if (dummy_args)
13609 {
13610 gfc_symbol* i_arg = dummy_args->sym;
13611 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13612 if (i_rank == my_rank)
13613 {
13614 gfc_error ("FINAL procedure %qs declared at %L has the same"
13615 " rank (%d) as %qs",
13616 list->proc_sym->name, &list->where, my_rank,
13617 i->proc_sym->name);
13618 goto error;
13619 }
13620 }
13621 }
13622
13623 /* Is this the/a scalar finalizer procedure? */
13624 if (my_rank == 0)
13625 seen_scalar = true;
13626
13627 /* Find the symtree for this procedure. */
13628 gcc_assert (!list->proc_tree);
13629 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13630
13631 prev_link = &list->next;
13632 continue;
13633
13634 /* Remove wrong nodes immediately from the list so we don't risk any
13635 troubles in the future when they might fail later expectations. */
13636 error:
13637 i = list;
13638 *prev_link = list->next;
13639 gfc_free_finalizer (i);
13640 result = false;
13641 }
13642
13643 if (result == false)
13644 return false;
13645
13646 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13647 were nodes in the list, must have been for arrays. It is surely a good
13648 idea to have a scalar version there if there's something to finalize. */
13649 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13650 gfc_warning (OPT_Wsurprising,
13651 "Only array FINAL procedures declared for derived type %qs"
13652 " defined at %L, suggest also scalar one",
13653 derived->name, &derived->declared_at);
13654
13655 vtab = gfc_find_derived_vtab (derived);
13656 c = vtab->ts.u.derived->components->next->next->next->next->next;
13657 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13658
13659 if (finalizable)
13660 *finalizable = true;
13661
13662 return true;
13663 }
13664
13665
13666 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13667
13668 static bool
13669 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13670 const char* generic_name, locus where)
13671 {
13672 gfc_symbol *sym1, *sym2;
13673 const char *pass1, *pass2;
13674 gfc_formal_arglist *dummy_args;
13675
13676 gcc_assert (t1->specific && t2->specific);
13677 gcc_assert (!t1->specific->is_generic);
13678 gcc_assert (!t2->specific->is_generic);
13679 gcc_assert (t1->is_operator == t2->is_operator);
13680
13681 sym1 = t1->specific->u.specific->n.sym;
13682 sym2 = t2->specific->u.specific->n.sym;
13683
13684 if (sym1 == sym2)
13685 return true;
13686
13687 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13688 if (sym1->attr.subroutine != sym2->attr.subroutine
13689 || sym1->attr.function != sym2->attr.function)
13690 {
13691 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13692 " GENERIC %qs at %L",
13693 sym1->name, sym2->name, generic_name, &where);
13694 return false;
13695 }
13696
13697 /* Determine PASS arguments. */
13698 if (t1->specific->nopass)
13699 pass1 = NULL;
13700 else if (t1->specific->pass_arg)
13701 pass1 = t1->specific->pass_arg;
13702 else
13703 {
13704 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13705 if (dummy_args)
13706 pass1 = dummy_args->sym->name;
13707 else
13708 pass1 = NULL;
13709 }
13710 if (t2->specific->nopass)
13711 pass2 = NULL;
13712 else if (t2->specific->pass_arg)
13713 pass2 = t2->specific->pass_arg;
13714 else
13715 {
13716 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13717 if (dummy_args)
13718 pass2 = dummy_args->sym->name;
13719 else
13720 pass2 = NULL;
13721 }
13722
13723 /* Compare the interfaces. */
13724 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13725 NULL, 0, pass1, pass2))
13726 {
13727 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13728 sym1->name, sym2->name, generic_name, &where);
13729 return false;
13730 }
13731
13732 return true;
13733 }
13734
13735
13736 /* Worker function for resolving a generic procedure binding; this is used to
13737 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13738
13739 The difference between those cases is finding possible inherited bindings
13740 that are overridden, as one has to look for them in tb_sym_root,
13741 tb_uop_root or tb_op, respectively. Thus the caller must already find
13742 the super-type and set p->overridden correctly. */
13743
13744 static bool
13745 resolve_tb_generic_targets (gfc_symbol* super_type,
13746 gfc_typebound_proc* p, const char* name)
13747 {
13748 gfc_tbp_generic* target;
13749 gfc_symtree* first_target;
13750 gfc_symtree* inherited;
13751
13752 gcc_assert (p && p->is_generic);
13753
13754 /* Try to find the specific bindings for the symtrees in our target-list. */
13755 gcc_assert (p->u.generic);
13756 for (target = p->u.generic; target; target = target->next)
13757 if (!target->specific)
13758 {
13759 gfc_typebound_proc* overridden_tbp;
13760 gfc_tbp_generic* g;
13761 const char* target_name;
13762
13763 target_name = target->specific_st->name;
13764
13765 /* Defined for this type directly. */
13766 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13767 {
13768 target->specific = target->specific_st->n.tb;
13769 goto specific_found;
13770 }
13771
13772 /* Look for an inherited specific binding. */
13773 if (super_type)
13774 {
13775 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13776 true, NULL);
13777
13778 if (inherited)
13779 {
13780 gcc_assert (inherited->n.tb);
13781 target->specific = inherited->n.tb;
13782 goto specific_found;
13783 }
13784 }
13785
13786 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13787 " at %L", target_name, name, &p->where);
13788 return false;
13789
13790 /* Once we've found the specific binding, check it is not ambiguous with
13791 other specifics already found or inherited for the same GENERIC. */
13792 specific_found:
13793 gcc_assert (target->specific);
13794
13795 /* This must really be a specific binding! */
13796 if (target->specific->is_generic)
13797 {
13798 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13799 " %qs is GENERIC, too", name, &p->where, target_name);
13800 return false;
13801 }
13802
13803 /* Check those already resolved on this type directly. */
13804 for (g = p->u.generic; g; g = g->next)
13805 if (g != target && g->specific
13806 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13807 return false;
13808
13809 /* Check for ambiguity with inherited specific targets. */
13810 for (overridden_tbp = p->overridden; overridden_tbp;
13811 overridden_tbp = overridden_tbp->overridden)
13812 if (overridden_tbp->is_generic)
13813 {
13814 for (g = overridden_tbp->u.generic; g; g = g->next)
13815 {
13816 gcc_assert (g->specific);
13817 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13818 return false;
13819 }
13820 }
13821 }
13822
13823 /* If we attempt to "overwrite" a specific binding, this is an error. */
13824 if (p->overridden && !p->overridden->is_generic)
13825 {
13826 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13827 " the same name", name, &p->where);
13828 return false;
13829 }
13830
13831 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13832 all must have the same attributes here. */
13833 first_target = p->u.generic->specific->u.specific;
13834 gcc_assert (first_target);
13835 p->subroutine = first_target->n.sym->attr.subroutine;
13836 p->function = first_target->n.sym->attr.function;
13837
13838 return true;
13839 }
13840
13841
13842 /* Resolve a GENERIC procedure binding for a derived type. */
13843
13844 static bool
13845 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13846 {
13847 gfc_symbol* super_type;
13848
13849 /* Find the overridden binding if any. */
13850 st->n.tb->overridden = NULL;
13851 super_type = gfc_get_derived_super_type (derived);
13852 if (super_type)
13853 {
13854 gfc_symtree* overridden;
13855 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13856 true, NULL);
13857
13858 if (overridden && overridden->n.tb)
13859 st->n.tb->overridden = overridden->n.tb;
13860 }
13861
13862 /* Resolve using worker function. */
13863 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13864 }
13865
13866
13867 /* Retrieve the target-procedure of an operator binding and do some checks in
13868 common for intrinsic and user-defined type-bound operators. */
13869
13870 static gfc_symbol*
13871 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13872 {
13873 gfc_symbol* target_proc;
13874
13875 gcc_assert (target->specific && !target->specific->is_generic);
13876 target_proc = target->specific->u.specific->n.sym;
13877 gcc_assert (target_proc);
13878
13879 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13880 if (target->specific->nopass)
13881 {
13882 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13883 return NULL;
13884 }
13885
13886 return target_proc;
13887 }
13888
13889
13890 /* Resolve a type-bound intrinsic operator. */
13891
13892 static bool
13893 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13894 gfc_typebound_proc* p)
13895 {
13896 gfc_symbol* super_type;
13897 gfc_tbp_generic* target;
13898
13899 /* If there's already an error here, do nothing (but don't fail again). */
13900 if (p->error)
13901 return true;
13902
13903 /* Operators should always be GENERIC bindings. */
13904 gcc_assert (p->is_generic);
13905
13906 /* Look for an overridden binding. */
13907 super_type = gfc_get_derived_super_type (derived);
13908 if (super_type && super_type->f2k_derived)
13909 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13910 op, true, NULL);
13911 else
13912 p->overridden = NULL;
13913
13914 /* Resolve general GENERIC properties using worker function. */
13915 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13916 goto error;
13917
13918 /* Check the targets to be procedures of correct interface. */
13919 for (target = p->u.generic; target; target = target->next)
13920 {
13921 gfc_symbol* target_proc;
13922
13923 target_proc = get_checked_tb_operator_target (target, p->where);
13924 if (!target_proc)
13925 goto error;
13926
13927 if (!gfc_check_operator_interface (target_proc, op, p->where))
13928 goto error;
13929
13930 /* Add target to non-typebound operator list. */
13931 if (!target->specific->deferred && !derived->attr.use_assoc
13932 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13933 {
13934 gfc_interface *head, *intr;
13935
13936 /* Preempt 'gfc_check_new_interface' for submodules, where the
13937 mechanism for handling module procedures winds up resolving
13938 operator interfaces twice and would otherwise cause an error. */
13939 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13940 if (intr->sym == target_proc
13941 && target_proc->attr.used_in_submodule)
13942 return true;
13943
13944 if (!gfc_check_new_interface (derived->ns->op[op],
13945 target_proc, p->where))
13946 return false;
13947 head = derived->ns->op[op];
13948 intr = gfc_get_interface ();
13949 intr->sym = target_proc;
13950 intr->where = p->where;
13951 intr->next = head;
13952 derived->ns->op[op] = intr;
13953 }
13954 }
13955
13956 return true;
13957
13958 error:
13959 p->error = 1;
13960 return false;
13961 }
13962
13963
13964 /* Resolve a type-bound user operator (tree-walker callback). */
13965
13966 static gfc_symbol* resolve_bindings_derived;
13967 static bool resolve_bindings_result;
13968
13969 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13970
13971 static void
13972 resolve_typebound_user_op (gfc_symtree* stree)
13973 {
13974 gfc_symbol* super_type;
13975 gfc_tbp_generic* target;
13976
13977 gcc_assert (stree && stree->n.tb);
13978
13979 if (stree->n.tb->error)
13980 return;
13981
13982 /* Operators should always be GENERIC bindings. */
13983 gcc_assert (stree->n.tb->is_generic);
13984
13985 /* Find overridden procedure, if any. */
13986 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13987 if (super_type && super_type->f2k_derived)
13988 {
13989 gfc_symtree* overridden;
13990 overridden = gfc_find_typebound_user_op (super_type, NULL,
13991 stree->name, true, NULL);
13992
13993 if (overridden && overridden->n.tb)
13994 stree->n.tb->overridden = overridden->n.tb;
13995 }
13996 else
13997 stree->n.tb->overridden = NULL;
13998
13999 /* Resolve basically using worker function. */
14000 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
14001 goto error;
14002
14003 /* Check the targets to be functions of correct interface. */
14004 for (target = stree->n.tb->u.generic; target; target = target->next)
14005 {
14006 gfc_symbol* target_proc;
14007
14008 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
14009 if (!target_proc)
14010 goto error;
14011
14012 if (!check_uop_procedure (target_proc, stree->n.tb->where))
14013 goto error;
14014 }
14015
14016 return;
14017
14018 error:
14019 resolve_bindings_result = false;
14020 stree->n.tb->error = 1;
14021 }
14022
14023
14024 /* Resolve the type-bound procedures for a derived type. */
14025
14026 static void
14027 resolve_typebound_procedure (gfc_symtree* stree)
14028 {
14029 gfc_symbol* proc;
14030 locus where;
14031 gfc_symbol* me_arg;
14032 gfc_symbol* super_type;
14033 gfc_component* comp;
14034
14035 gcc_assert (stree);
14036
14037 /* Undefined specific symbol from GENERIC target definition. */
14038 if (!stree->n.tb)
14039 return;
14040
14041 if (stree->n.tb->error)
14042 return;
14043
14044 /* If this is a GENERIC binding, use that routine. */
14045 if (stree->n.tb->is_generic)
14046 {
14047 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
14048 goto error;
14049 return;
14050 }
14051
14052 /* Get the target-procedure to check it. */
14053 gcc_assert (!stree->n.tb->is_generic);
14054 gcc_assert (stree->n.tb->u.specific);
14055 proc = stree->n.tb->u.specific->n.sym;
14056 where = stree->n.tb->where;
14057
14058 /* Default access should already be resolved from the parser. */
14059 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
14060
14061 if (stree->n.tb->deferred)
14062 {
14063 if (!check_proc_interface (proc, &where))
14064 goto error;
14065 }
14066 else
14067 {
14068 /* If proc has not been resolved at this point, proc->name may
14069 actually be a USE associated entity. See PR fortran/89647. */
14070 if (!proc->resolve_symbol_called
14071 && proc->attr.function == 0 && proc->attr.subroutine == 0)
14072 {
14073 gfc_symbol *tmp;
14074 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
14075 if (tmp && tmp->attr.use_assoc)
14076 {
14077 proc->module = tmp->module;
14078 proc->attr.proc = tmp->attr.proc;
14079 proc->attr.function = tmp->attr.function;
14080 proc->attr.subroutine = tmp->attr.subroutine;
14081 proc->attr.use_assoc = tmp->attr.use_assoc;
14082 proc->ts = tmp->ts;
14083 proc->result = tmp->result;
14084 }
14085 }
14086
14087 /* Check for F08:C465. */
14088 if ((!proc->attr.subroutine && !proc->attr.function)
14089 || (proc->attr.proc != PROC_MODULE
14090 && proc->attr.if_source != IFSRC_IFBODY
14091 && !proc->attr.module_procedure)
14092 || proc->attr.abstract)
14093 {
14094 gfc_error ("%qs must be a module procedure or an external "
14095 "procedure with an explicit interface at %L",
14096 proc->name, &where);
14097 goto error;
14098 }
14099 }
14100
14101 stree->n.tb->subroutine = proc->attr.subroutine;
14102 stree->n.tb->function = proc->attr.function;
14103
14104 /* Find the super-type of the current derived type. We could do this once and
14105 store in a global if speed is needed, but as long as not I believe this is
14106 more readable and clearer. */
14107 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14108
14109 /* If PASS, resolve and check arguments if not already resolved / loaded
14110 from a .mod file. */
14111 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
14112 {
14113 gfc_formal_arglist *dummy_args;
14114
14115 dummy_args = gfc_sym_get_dummy_args (proc);
14116 if (stree->n.tb->pass_arg)
14117 {
14118 gfc_formal_arglist *i;
14119
14120 /* If an explicit passing argument name is given, walk the arg-list
14121 and look for it. */
14122
14123 me_arg = NULL;
14124 stree->n.tb->pass_arg_num = 1;
14125 for (i = dummy_args; i; i = i->next)
14126 {
14127 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
14128 {
14129 me_arg = i->sym;
14130 break;
14131 }
14132 ++stree->n.tb->pass_arg_num;
14133 }
14134
14135 if (!me_arg)
14136 {
14137 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14138 " argument %qs",
14139 proc->name, stree->n.tb->pass_arg, &where,
14140 stree->n.tb->pass_arg);
14141 goto error;
14142 }
14143 }
14144 else
14145 {
14146 /* Otherwise, take the first one; there should in fact be at least
14147 one. */
14148 stree->n.tb->pass_arg_num = 1;
14149 if (!dummy_args)
14150 {
14151 gfc_error ("Procedure %qs with PASS at %L must have at"
14152 " least one argument", proc->name, &where);
14153 goto error;
14154 }
14155 me_arg = dummy_args->sym;
14156 }
14157
14158 /* Now check that the argument-type matches and the passed-object
14159 dummy argument is generally fine. */
14160
14161 gcc_assert (me_arg);
14162
14163 if (me_arg->ts.type != BT_CLASS)
14164 {
14165 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14166 " at %L", proc->name, &where);
14167 goto error;
14168 }
14169
14170 if (CLASS_DATA (me_arg)->ts.u.derived
14171 != resolve_bindings_derived)
14172 {
14173 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14174 " the derived-type %qs", me_arg->name, proc->name,
14175 me_arg->name, &where, resolve_bindings_derived->name);
14176 goto error;
14177 }
14178
14179 gcc_assert (me_arg->ts.type == BT_CLASS);
14180 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14181 {
14182 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14183 " scalar", proc->name, &where);
14184 goto error;
14185 }
14186 if (CLASS_DATA (me_arg)->attr.allocatable)
14187 {
14188 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14189 " be ALLOCATABLE", proc->name, &where);
14190 goto error;
14191 }
14192 if (CLASS_DATA (me_arg)->attr.class_pointer)
14193 {
14194 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14195 " be POINTER", proc->name, &where);
14196 goto error;
14197 }
14198 }
14199
14200 /* If we are extending some type, check that we don't override a procedure
14201 flagged NON_OVERRIDABLE. */
14202 stree->n.tb->overridden = NULL;
14203 if (super_type)
14204 {
14205 gfc_symtree* overridden;
14206 overridden = gfc_find_typebound_proc (super_type, NULL,
14207 stree->name, true, NULL);
14208
14209 if (overridden)
14210 {
14211 if (overridden->n.tb)
14212 stree->n.tb->overridden = overridden->n.tb;
14213
14214 if (!gfc_check_typebound_override (stree, overridden))
14215 goto error;
14216 }
14217 }
14218
14219 /* See if there's a name collision with a component directly in this type. */
14220 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14221 if (!strcmp (comp->name, stree->name))
14222 {
14223 gfc_error ("Procedure %qs at %L has the same name as a component of"
14224 " %qs",
14225 stree->name, &where, resolve_bindings_derived->name);
14226 goto error;
14227 }
14228
14229 /* Try to find a name collision with an inherited component. */
14230 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14231 NULL))
14232 {
14233 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14234 " component of %qs",
14235 stree->name, &where, resolve_bindings_derived->name);
14236 goto error;
14237 }
14238
14239 stree->n.tb->error = 0;
14240 return;
14241
14242 error:
14243 resolve_bindings_result = false;
14244 stree->n.tb->error = 1;
14245 }
14246
14247
14248 static bool
14249 resolve_typebound_procedures (gfc_symbol* derived)
14250 {
14251 int op;
14252 gfc_symbol* super_type;
14253
14254 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14255 return true;
14256
14257 super_type = gfc_get_derived_super_type (derived);
14258 if (super_type)
14259 resolve_symbol (super_type);
14260
14261 resolve_bindings_derived = derived;
14262 resolve_bindings_result = true;
14263
14264 if (derived->f2k_derived->tb_sym_root)
14265 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14266 &resolve_typebound_procedure);
14267
14268 if (derived->f2k_derived->tb_uop_root)
14269 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14270 &resolve_typebound_user_op);
14271
14272 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14273 {
14274 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14275 if (p && !resolve_typebound_intrinsic_op (derived,
14276 (gfc_intrinsic_op)op, p))
14277 resolve_bindings_result = false;
14278 }
14279
14280 return resolve_bindings_result;
14281 }
14282
14283
14284 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14285 to give all identical derived types the same backend_decl. */
14286 static void
14287 add_dt_to_dt_list (gfc_symbol *derived)
14288 {
14289 if (!derived->dt_next)
14290 {
14291 if (gfc_derived_types)
14292 {
14293 derived->dt_next = gfc_derived_types->dt_next;
14294 gfc_derived_types->dt_next = derived;
14295 }
14296 else
14297 {
14298 derived->dt_next = derived;
14299 }
14300 gfc_derived_types = derived;
14301 }
14302 }
14303
14304
14305 /* Ensure that a derived-type is really not abstract, meaning that every
14306 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14307
14308 static bool
14309 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14310 {
14311 if (!st)
14312 return true;
14313
14314 if (!ensure_not_abstract_walker (sub, st->left))
14315 return false;
14316 if (!ensure_not_abstract_walker (sub, st->right))
14317 return false;
14318
14319 if (st->n.tb && st->n.tb->deferred)
14320 {
14321 gfc_symtree* overriding;
14322 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14323 if (!overriding)
14324 return false;
14325 gcc_assert (overriding->n.tb);
14326 if (overriding->n.tb->deferred)
14327 {
14328 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14329 " %qs is DEFERRED and not overridden",
14330 sub->name, &sub->declared_at, st->name);
14331 return false;
14332 }
14333 }
14334
14335 return true;
14336 }
14337
14338 static bool
14339 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14340 {
14341 /* The algorithm used here is to recursively travel up the ancestry of sub
14342 and for each ancestor-type, check all bindings. If any of them is
14343 DEFERRED, look it up starting from sub and see if the found (overriding)
14344 binding is not DEFERRED.
14345 This is not the most efficient way to do this, but it should be ok and is
14346 clearer than something sophisticated. */
14347
14348 gcc_assert (ancestor && !sub->attr.abstract);
14349
14350 if (!ancestor->attr.abstract)
14351 return true;
14352
14353 /* Walk bindings of this ancestor. */
14354 if (ancestor->f2k_derived)
14355 {
14356 bool t;
14357 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14358 if (!t)
14359 return false;
14360 }
14361
14362 /* Find next ancestor type and recurse on it. */
14363 ancestor = gfc_get_derived_super_type (ancestor);
14364 if (ancestor)
14365 return ensure_not_abstract (sub, ancestor);
14366
14367 return true;
14368 }
14369
14370
14371 /* This check for typebound defined assignments is done recursively
14372 since the order in which derived types are resolved is not always in
14373 order of the declarations. */
14374
14375 static void
14376 check_defined_assignments (gfc_symbol *derived)
14377 {
14378 gfc_component *c;
14379
14380 for (c = derived->components; c; c = c->next)
14381 {
14382 if (!gfc_bt_struct (c->ts.type)
14383 || c->attr.pointer
14384 || c->attr.allocatable
14385 || c->attr.proc_pointer_comp
14386 || c->attr.class_pointer
14387 || c->attr.proc_pointer)
14388 continue;
14389
14390 if (c->ts.u.derived->attr.defined_assign_comp
14391 || (c->ts.u.derived->f2k_derived
14392 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14393 {
14394 derived->attr.defined_assign_comp = 1;
14395 return;
14396 }
14397
14398 check_defined_assignments (c->ts.u.derived);
14399 if (c->ts.u.derived->attr.defined_assign_comp)
14400 {
14401 derived->attr.defined_assign_comp = 1;
14402 return;
14403 }
14404 }
14405 }
14406
14407
14408 /* Resolve a single component of a derived type or structure. */
14409
14410 static bool
14411 resolve_component (gfc_component *c, gfc_symbol *sym)
14412 {
14413 gfc_symbol *super_type;
14414 symbol_attribute *attr;
14415
14416 if (c->attr.artificial)
14417 return true;
14418
14419 /* Do not allow vtype components to be resolved in nameless namespaces
14420 such as block data because the procedure pointers will cause ICEs
14421 and vtables are not needed in these contexts. */
14422 if (sym->attr.vtype && sym->attr.use_assoc
14423 && sym->ns->proc_name == NULL)
14424 return true;
14425
14426 /* F2008, C442. */
14427 if ((!sym->attr.is_class || c != sym->components)
14428 && c->attr.codimension
14429 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14430 {
14431 gfc_error ("Coarray component %qs at %L must be allocatable with "
14432 "deferred shape", c->name, &c->loc);
14433 return false;
14434 }
14435
14436 /* F2008, C443. */
14437 if (c->attr.codimension && c->ts.type == BT_DERIVED
14438 && c->ts.u.derived->ts.is_iso_c)
14439 {
14440 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14441 "shall not be a coarray", c->name, &c->loc);
14442 return false;
14443 }
14444
14445 /* F2008, C444. */
14446 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14447 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14448 || c->attr.allocatable))
14449 {
14450 gfc_error ("Component %qs at %L with coarray component "
14451 "shall be a nonpointer, nonallocatable scalar",
14452 c->name, &c->loc);
14453 return false;
14454 }
14455
14456 /* F2008, C448. */
14457 if (c->ts.type == BT_CLASS)
14458 {
14459 if (c->attr.class_ok && CLASS_DATA (c))
14460 {
14461 attr = &(CLASS_DATA (c)->attr);
14462
14463 /* Fix up contiguous attribute. */
14464 if (c->attr.contiguous)
14465 attr->contiguous = 1;
14466 }
14467 else
14468 attr = NULL;
14469 }
14470 else
14471 attr = &c->attr;
14472
14473 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14474 {
14475 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14476 "is not an array pointer", c->name, &c->loc);
14477 return false;
14478 }
14479
14480 /* F2003, 15.2.1 - length has to be one. */
14481 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14482 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14483 || !gfc_is_constant_expr (c->ts.u.cl->length)
14484 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14485 {
14486 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14487 c->name, &c->loc);
14488 return false;
14489 }
14490
14491 if (c->attr.proc_pointer && c->ts.interface)
14492 {
14493 gfc_symbol *ifc = c->ts.interface;
14494
14495 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14496 {
14497 c->tb->error = 1;
14498 return false;
14499 }
14500
14501 if (ifc->attr.if_source || ifc->attr.intrinsic)
14502 {
14503 /* Resolve interface and copy attributes. */
14504 if (ifc->formal && !ifc->formal_ns)
14505 resolve_symbol (ifc);
14506 if (ifc->attr.intrinsic)
14507 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14508
14509 if (ifc->result)
14510 {
14511 c->ts = ifc->result->ts;
14512 c->attr.allocatable = ifc->result->attr.allocatable;
14513 c->attr.pointer = ifc->result->attr.pointer;
14514 c->attr.dimension = ifc->result->attr.dimension;
14515 c->as = gfc_copy_array_spec (ifc->result->as);
14516 c->attr.class_ok = ifc->result->attr.class_ok;
14517 }
14518 else
14519 {
14520 c->ts = ifc->ts;
14521 c->attr.allocatable = ifc->attr.allocatable;
14522 c->attr.pointer = ifc->attr.pointer;
14523 c->attr.dimension = ifc->attr.dimension;
14524 c->as = gfc_copy_array_spec (ifc->as);
14525 c->attr.class_ok = ifc->attr.class_ok;
14526 }
14527 c->ts.interface = ifc;
14528 c->attr.function = ifc->attr.function;
14529 c->attr.subroutine = ifc->attr.subroutine;
14530
14531 c->attr.pure = ifc->attr.pure;
14532 c->attr.elemental = ifc->attr.elemental;
14533 c->attr.recursive = ifc->attr.recursive;
14534 c->attr.always_explicit = ifc->attr.always_explicit;
14535 c->attr.ext_attr |= ifc->attr.ext_attr;
14536 /* Copy char length. */
14537 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14538 {
14539 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14540 if (cl->length && !cl->resolved
14541 && !gfc_resolve_expr (cl->length))
14542 {
14543 c->tb->error = 1;
14544 return false;
14545 }
14546 c->ts.u.cl = cl;
14547 }
14548 }
14549 }
14550 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14551 {
14552 /* Since PPCs are not implicitly typed, a PPC without an explicit
14553 interface must be a subroutine. */
14554 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14555 }
14556
14557 /* Procedure pointer components: Check PASS arg. */
14558 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14559 && !sym->attr.vtype)
14560 {
14561 gfc_symbol* me_arg;
14562
14563 if (c->tb->pass_arg)
14564 {
14565 gfc_formal_arglist* i;
14566
14567 /* If an explicit passing argument name is given, walk the arg-list
14568 and look for it. */
14569
14570 me_arg = NULL;
14571 c->tb->pass_arg_num = 1;
14572 for (i = c->ts.interface->formal; i; i = i->next)
14573 {
14574 if (!strcmp (i->sym->name, c->tb->pass_arg))
14575 {
14576 me_arg = i->sym;
14577 break;
14578 }
14579 c->tb->pass_arg_num++;
14580 }
14581
14582 if (!me_arg)
14583 {
14584 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14585 "at %L has no argument %qs", c->name,
14586 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14587 c->tb->error = 1;
14588 return false;
14589 }
14590 }
14591 else
14592 {
14593 /* Otherwise, take the first one; there should in fact be at least
14594 one. */
14595 c->tb->pass_arg_num = 1;
14596 if (!c->ts.interface->formal)
14597 {
14598 gfc_error ("Procedure pointer component %qs with PASS at %L "
14599 "must have at least one argument",
14600 c->name, &c->loc);
14601 c->tb->error = 1;
14602 return false;
14603 }
14604 me_arg = c->ts.interface->formal->sym;
14605 }
14606
14607 /* Now check that the argument-type matches. */
14608 gcc_assert (me_arg);
14609 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14610 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14611 || (me_arg->ts.type == BT_CLASS
14612 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14613 {
14614 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14615 " the derived type %qs", me_arg->name, c->name,
14616 me_arg->name, &c->loc, sym->name);
14617 c->tb->error = 1;
14618 return false;
14619 }
14620
14621 /* Check for F03:C453. */
14622 if (CLASS_DATA (me_arg)->attr.dimension)
14623 {
14624 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14625 "must be scalar", me_arg->name, c->name, me_arg->name,
14626 &c->loc);
14627 c->tb->error = 1;
14628 return false;
14629 }
14630
14631 if (CLASS_DATA (me_arg)->attr.class_pointer)
14632 {
14633 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14634 "may not have the POINTER attribute", me_arg->name,
14635 c->name, me_arg->name, &c->loc);
14636 c->tb->error = 1;
14637 return false;
14638 }
14639
14640 if (CLASS_DATA (me_arg)->attr.allocatable)
14641 {
14642 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14643 "may not be ALLOCATABLE", me_arg->name, c->name,
14644 me_arg->name, &c->loc);
14645 c->tb->error = 1;
14646 return false;
14647 }
14648
14649 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14650 {
14651 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14652 " at %L", c->name, &c->loc);
14653 return false;
14654 }
14655
14656 }
14657
14658 /* Check type-spec if this is not the parent-type component. */
14659 if (((sym->attr.is_class
14660 && (!sym->components->ts.u.derived->attr.extension
14661 || c != sym->components->ts.u.derived->components))
14662 || (!sym->attr.is_class
14663 && (!sym->attr.extension || c != sym->components)))
14664 && !sym->attr.vtype
14665 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14666 return false;
14667
14668 super_type = gfc_get_derived_super_type (sym);
14669
14670 /* If this type is an extension, set the accessibility of the parent
14671 component. */
14672 if (super_type
14673 && ((sym->attr.is_class
14674 && c == sym->components->ts.u.derived->components)
14675 || (!sym->attr.is_class && c == sym->components))
14676 && strcmp (super_type->name, c->name) == 0)
14677 c->attr.access = super_type->attr.access;
14678
14679 /* If this type is an extension, see if this component has the same name
14680 as an inherited type-bound procedure. */
14681 if (super_type && !sym->attr.is_class
14682 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14683 {
14684 gfc_error ("Component %qs of %qs at %L has the same name as an"
14685 " inherited type-bound procedure",
14686 c->name, sym->name, &c->loc);
14687 return false;
14688 }
14689
14690 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14691 && !c->ts.deferred)
14692 {
14693 if (c->ts.u.cl->length == NULL
14694 || (!resolve_charlen(c->ts.u.cl))
14695 || !gfc_is_constant_expr (c->ts.u.cl->length))
14696 {
14697 gfc_error ("Character length of component %qs needs to "
14698 "be a constant specification expression at %L",
14699 c->name,
14700 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14701 return false;
14702 }
14703 }
14704
14705 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14706 && !c->attr.pointer && !c->attr.allocatable)
14707 {
14708 gfc_error ("Character component %qs of %qs at %L with deferred "
14709 "length must be a POINTER or ALLOCATABLE",
14710 c->name, sym->name, &c->loc);
14711 return false;
14712 }
14713
14714 /* Add the hidden deferred length field. */
14715 if (c->ts.type == BT_CHARACTER
14716 && (c->ts.deferred || c->attr.pdt_string)
14717 && !c->attr.function
14718 && !sym->attr.is_class)
14719 {
14720 char name[GFC_MAX_SYMBOL_LEN+9];
14721 gfc_component *strlen;
14722 sprintf (name, "_%s_length", c->name);
14723 strlen = gfc_find_component (sym, name, true, true, NULL);
14724 if (strlen == NULL)
14725 {
14726 if (!gfc_add_component (sym, name, &strlen))
14727 return false;
14728 strlen->ts.type = BT_INTEGER;
14729 strlen->ts.kind = gfc_charlen_int_kind;
14730 strlen->attr.access = ACCESS_PRIVATE;
14731 strlen->attr.artificial = 1;
14732 }
14733 }
14734
14735 if (c->ts.type == BT_DERIVED
14736 && sym->component_access != ACCESS_PRIVATE
14737 && gfc_check_symbol_access (sym)
14738 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14739 && !c->ts.u.derived->attr.use_assoc
14740 && !gfc_check_symbol_access (c->ts.u.derived)
14741 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14742 "PRIVATE type and cannot be a component of "
14743 "%qs, which is PUBLIC at %L", c->name,
14744 sym->name, &sym->declared_at))
14745 return false;
14746
14747 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14748 {
14749 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14750 "type %s", c->name, &c->loc, sym->name);
14751 return false;
14752 }
14753
14754 if (sym->attr.sequence)
14755 {
14756 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14757 {
14758 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14759 "not have the SEQUENCE attribute",
14760 c->ts.u.derived->name, &sym->declared_at);
14761 return false;
14762 }
14763 }
14764
14765 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14766 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14767 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14768 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14769 CLASS_DATA (c)->ts.u.derived
14770 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14771
14772 /* If an allocatable component derived type is of the same type as
14773 the enclosing derived type, we need a vtable generating so that
14774 the __deallocate procedure is created. */
14775 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14776 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14777 gfc_find_vtab (&c->ts);
14778
14779 /* Ensure that all the derived type components are put on the
14780 derived type list; even in formal namespaces, where derived type
14781 pointer components might not have been declared. */
14782 if (c->ts.type == BT_DERIVED
14783 && c->ts.u.derived
14784 && c->ts.u.derived->components
14785 && c->attr.pointer
14786 && sym != c->ts.u.derived)
14787 add_dt_to_dt_list (c->ts.u.derived);
14788
14789 if (c->as && c->as->type != AS_DEFERRED
14790 && (c->attr.pointer || c->attr.allocatable))
14791 return false;
14792
14793 if (!gfc_resolve_array_spec (c->as,
14794 !(c->attr.pointer || c->attr.proc_pointer
14795 || c->attr.allocatable)))
14796 return false;
14797
14798 if (c->initializer && !sym->attr.vtype
14799 && !c->attr.pdt_kind && !c->attr.pdt_len
14800 && !gfc_check_assign_symbol (sym, c, c->initializer))
14801 return false;
14802
14803 return true;
14804 }
14805
14806
14807 /* Be nice about the locus for a structure expression - show the locus of the
14808 first non-null sub-expression if we can. */
14809
14810 static locus *
14811 cons_where (gfc_expr *struct_expr)
14812 {
14813 gfc_constructor *cons;
14814
14815 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14816
14817 cons = gfc_constructor_first (struct_expr->value.constructor);
14818 for (; cons; cons = gfc_constructor_next (cons))
14819 {
14820 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14821 return &cons->expr->where;
14822 }
14823
14824 return &struct_expr->where;
14825 }
14826
14827 /* Resolve the components of a structure type. Much less work than derived
14828 types. */
14829
14830 static bool
14831 resolve_fl_struct (gfc_symbol *sym)
14832 {
14833 gfc_component *c;
14834 gfc_expr *init = NULL;
14835 bool success;
14836
14837 /* Make sure UNIONs do not have overlapping initializers. */
14838 if (sym->attr.flavor == FL_UNION)
14839 {
14840 for (c = sym->components; c; c = c->next)
14841 {
14842 if (init && c->initializer)
14843 {
14844 gfc_error ("Conflicting initializers in union at %L and %L",
14845 cons_where (init), cons_where (c->initializer));
14846 gfc_free_expr (c->initializer);
14847 c->initializer = NULL;
14848 }
14849 if (init == NULL)
14850 init = c->initializer;
14851 }
14852 }
14853
14854 success = true;
14855 for (c = sym->components; c; c = c->next)
14856 if (!resolve_component (c, sym))
14857 success = false;
14858
14859 if (!success)
14860 return false;
14861
14862 if (sym->components)
14863 add_dt_to_dt_list (sym);
14864
14865 return true;
14866 }
14867
14868
14869 /* Resolve the components of a derived type. This does not have to wait until
14870 resolution stage, but can be done as soon as the dt declaration has been
14871 parsed. */
14872
14873 static bool
14874 resolve_fl_derived0 (gfc_symbol *sym)
14875 {
14876 gfc_symbol* super_type;
14877 gfc_component *c;
14878 gfc_formal_arglist *f;
14879 bool success;
14880
14881 if (sym->attr.unlimited_polymorphic)
14882 return true;
14883
14884 super_type = gfc_get_derived_super_type (sym);
14885
14886 /* F2008, C432. */
14887 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14888 {
14889 gfc_error ("As extending type %qs at %L has a coarray component, "
14890 "parent type %qs shall also have one", sym->name,
14891 &sym->declared_at, super_type->name);
14892 return false;
14893 }
14894
14895 /* Ensure the extended type gets resolved before we do. */
14896 if (super_type && !resolve_fl_derived0 (super_type))
14897 return false;
14898
14899 /* An ABSTRACT type must be extensible. */
14900 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14901 {
14902 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14903 sym->name, &sym->declared_at);
14904 return false;
14905 }
14906
14907 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14908 : sym->components;
14909
14910 success = true;
14911 for ( ; c != NULL; c = c->next)
14912 if (!resolve_component (c, sym))
14913 success = false;
14914
14915 if (!success)
14916 return false;
14917
14918 /* Now add the caf token field, where needed. */
14919 if (flag_coarray != GFC_FCOARRAY_NONE
14920 && !sym->attr.is_class && !sym->attr.vtype)
14921 {
14922 for (c = sym->components; c; c = c->next)
14923 if (!c->attr.dimension && !c->attr.codimension
14924 && (c->attr.allocatable || c->attr.pointer))
14925 {
14926 char name[GFC_MAX_SYMBOL_LEN+9];
14927 gfc_component *token;
14928 sprintf (name, "_caf_%s", c->name);
14929 token = gfc_find_component (sym, name, true, true, NULL);
14930 if (token == NULL)
14931 {
14932 if (!gfc_add_component (sym, name, &token))
14933 return false;
14934 token->ts.type = BT_VOID;
14935 token->ts.kind = gfc_default_integer_kind;
14936 token->attr.access = ACCESS_PRIVATE;
14937 token->attr.artificial = 1;
14938 token->attr.caf_token = 1;
14939 }
14940 }
14941 }
14942
14943 check_defined_assignments (sym);
14944
14945 if (!sym->attr.defined_assign_comp && super_type)
14946 sym->attr.defined_assign_comp
14947 = super_type->attr.defined_assign_comp;
14948
14949 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14950 all DEFERRED bindings are overridden. */
14951 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14952 && !sym->attr.is_class
14953 && !ensure_not_abstract (sym, super_type))
14954 return false;
14955
14956 /* Check that there is a component for every PDT parameter. */
14957 if (sym->attr.pdt_template)
14958 {
14959 for (f = sym->formal; f; f = f->next)
14960 {
14961 if (!f->sym)
14962 continue;
14963 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14964 if (c == NULL)
14965 {
14966 gfc_error ("Parameterized type %qs does not have a component "
14967 "corresponding to parameter %qs at %L", sym->name,
14968 f->sym->name, &sym->declared_at);
14969 break;
14970 }
14971 }
14972 }
14973
14974 /* Add derived type to the derived type list. */
14975 add_dt_to_dt_list (sym);
14976
14977 return true;
14978 }
14979
14980
14981 /* The following procedure does the full resolution of a derived type,
14982 including resolution of all type-bound procedures (if present). In contrast
14983 to 'resolve_fl_derived0' this can only be done after the module has been
14984 parsed completely. */
14985
14986 static bool
14987 resolve_fl_derived (gfc_symbol *sym)
14988 {
14989 gfc_symbol *gen_dt = NULL;
14990
14991 if (sym->attr.unlimited_polymorphic)
14992 return true;
14993
14994 if (!sym->attr.is_class)
14995 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14996 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14997 && (!gen_dt->generic->sym->attr.use_assoc
14998 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14999 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
15000 "%qs at %L being the same name as derived "
15001 "type at %L", sym->name,
15002 gen_dt->generic->sym == sym
15003 ? gen_dt->generic->next->sym->name
15004 : gen_dt->generic->sym->name,
15005 gen_dt->generic->sym == sym
15006 ? &gen_dt->generic->next->sym->declared_at
15007 : &gen_dt->generic->sym->declared_at,
15008 &sym->declared_at))
15009 return false;
15010
15011 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
15012 {
15013 gfc_error ("Derived type %qs at %L has not been declared",
15014 sym->name, &sym->declared_at);
15015 return false;
15016 }
15017
15018 /* Resolve the finalizer procedures. */
15019 if (!gfc_resolve_finalizers (sym, NULL))
15020 return false;
15021
15022 if (sym->attr.is_class && sym->ts.u.derived == NULL)
15023 {
15024 /* Fix up incomplete CLASS symbols. */
15025 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
15026 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
15027
15028 /* Nothing more to do for unlimited polymorphic entities. */
15029 if (data->ts.u.derived->attr.unlimited_polymorphic)
15030 return true;
15031 else if (vptr->ts.u.derived == NULL)
15032 {
15033 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
15034 gcc_assert (vtab);
15035 vptr->ts.u.derived = vtab->ts.u.derived;
15036 if (!resolve_fl_derived0 (vptr->ts.u.derived))
15037 return false;
15038 }
15039 }
15040
15041 if (!resolve_fl_derived0 (sym))
15042 return false;
15043
15044 /* Resolve the type-bound procedures. */
15045 if (!resolve_typebound_procedures (sym))
15046 return false;
15047
15048 /* Generate module vtables subject to their accessibility and their not
15049 being vtables or pdt templates. If this is not done class declarations
15050 in external procedures wind up with their own version and so SELECT TYPE
15051 fails because the vptrs do not have the same address. */
15052 if (gfc_option.allow_std & GFC_STD_F2003
15053 && sym->ns->proc_name
15054 && sym->ns->proc_name->attr.flavor == FL_MODULE
15055 && sym->attr.access != ACCESS_PRIVATE
15056 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
15057 {
15058 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
15059 gfc_set_sym_referenced (vtab);
15060 }
15061
15062 return true;
15063 }
15064
15065
15066 static bool
15067 resolve_fl_namelist (gfc_symbol *sym)
15068 {
15069 gfc_namelist *nl;
15070 gfc_symbol *nlsym;
15071
15072 for (nl = sym->namelist; nl; nl = nl->next)
15073 {
15074 /* Check again, the check in match only works if NAMELIST comes
15075 after the decl. */
15076 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
15077 {
15078 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
15079 "allowed", nl->sym->name, sym->name, &sym->declared_at);
15080 return false;
15081 }
15082
15083 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
15084 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15085 "with assumed shape in namelist %qs at %L",
15086 nl->sym->name, sym->name, &sym->declared_at))
15087 return false;
15088
15089 if (is_non_constant_shape_array (nl->sym)
15090 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15091 "with nonconstant shape in namelist %qs at %L",
15092 nl->sym->name, sym->name, &sym->declared_at))
15093 return false;
15094
15095 if (nl->sym->ts.type == BT_CHARACTER
15096 && (nl->sym->ts.u.cl->length == NULL
15097 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
15098 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
15099 "nonconstant character length in "
15100 "namelist %qs at %L", nl->sym->name,
15101 sym->name, &sym->declared_at))
15102 return false;
15103
15104 }
15105
15106 /* Reject PRIVATE objects in a PUBLIC namelist. */
15107 if (gfc_check_symbol_access (sym))
15108 {
15109 for (nl = sym->namelist; nl; nl = nl->next)
15110 {
15111 if (!nl->sym->attr.use_assoc
15112 && !is_sym_host_assoc (nl->sym, sym->ns)
15113 && !gfc_check_symbol_access (nl->sym))
15114 {
15115 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
15116 "cannot be member of PUBLIC namelist %qs at %L",
15117 nl->sym->name, sym->name, &sym->declared_at);
15118 return false;
15119 }
15120
15121 if (nl->sym->ts.type == BT_DERIVED
15122 && (nl->sym->ts.u.derived->attr.alloc_comp
15123 || nl->sym->ts.u.derived->attr.pointer_comp))
15124 {
15125 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
15126 "namelist %qs at %L with ALLOCATABLE "
15127 "or POINTER components", nl->sym->name,
15128 sym->name, &sym->declared_at))
15129 return false;
15130 return true;
15131 }
15132
15133 /* Types with private components that came here by USE-association. */
15134 if (nl->sym->ts.type == BT_DERIVED
15135 && derived_inaccessible (nl->sym->ts.u.derived))
15136 {
15137 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15138 "components and cannot be member of namelist %qs at %L",
15139 nl->sym->name, sym->name, &sym->declared_at);
15140 return false;
15141 }
15142
15143 /* Types with private components that are defined in the same module. */
15144 if (nl->sym->ts.type == BT_DERIVED
15145 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15146 && nl->sym->ts.u.derived->attr.private_comp)
15147 {
15148 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15149 "cannot be a member of PUBLIC namelist %qs at %L",
15150 nl->sym->name, sym->name, &sym->declared_at);
15151 return false;
15152 }
15153 }
15154 }
15155
15156
15157 /* 14.1.2 A module or internal procedure represent local entities
15158 of the same type as a namelist member and so are not allowed. */
15159 for (nl = sym->namelist; nl; nl = nl->next)
15160 {
15161 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15162 continue;
15163
15164 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15165 if ((nl->sym == sym->ns->proc_name)
15166 ||
15167 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15168 continue;
15169
15170 nlsym = NULL;
15171 if (nl->sym->name)
15172 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15173 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15174 {
15175 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15176 "attribute in %qs at %L", nlsym->name,
15177 &sym->declared_at);
15178 return false;
15179 }
15180 }
15181
15182 return true;
15183 }
15184
15185
15186 static bool
15187 resolve_fl_parameter (gfc_symbol *sym)
15188 {
15189 /* A parameter array's shape needs to be constant. */
15190 if (sym->as != NULL
15191 && (sym->as->type == AS_DEFERRED
15192 || is_non_constant_shape_array (sym)))
15193 {
15194 gfc_error ("Parameter array %qs at %L cannot be automatic "
15195 "or of deferred shape", sym->name, &sym->declared_at);
15196 return false;
15197 }
15198
15199 /* Constraints on deferred type parameter. */
15200 if (!deferred_requirements (sym))
15201 return false;
15202
15203 /* Make sure a parameter that has been implicitly typed still
15204 matches the implicit type, since PARAMETER statements can precede
15205 IMPLICIT statements. */
15206 if (sym->attr.implicit_type
15207 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15208 sym->ns)))
15209 {
15210 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15211 "later IMPLICIT type", sym->name, &sym->declared_at);
15212 return false;
15213 }
15214
15215 /* Make sure the types of derived parameters are consistent. This
15216 type checking is deferred until resolution because the type may
15217 refer to a derived type from the host. */
15218 if (sym->ts.type == BT_DERIVED
15219 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15220 {
15221 gfc_error ("Incompatible derived type in PARAMETER at %L",
15222 &sym->value->where);
15223 return false;
15224 }
15225
15226 /* F03:C509,C514. */
15227 if (sym->ts.type == BT_CLASS)
15228 {
15229 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15230 sym->name, &sym->declared_at);
15231 return false;
15232 }
15233
15234 return true;
15235 }
15236
15237
15238 /* Called by resolve_symbol to check PDTs. */
15239
15240 static void
15241 resolve_pdt (gfc_symbol* sym)
15242 {
15243 gfc_symbol *derived = NULL;
15244 gfc_actual_arglist *param;
15245 gfc_component *c;
15246 bool const_len_exprs = true;
15247 bool assumed_len_exprs = false;
15248 symbol_attribute *attr;
15249
15250 if (sym->ts.type == BT_DERIVED)
15251 {
15252 derived = sym->ts.u.derived;
15253 attr = &(sym->attr);
15254 }
15255 else if (sym->ts.type == BT_CLASS)
15256 {
15257 derived = CLASS_DATA (sym)->ts.u.derived;
15258 attr = &(CLASS_DATA (sym)->attr);
15259 }
15260 else
15261 gcc_unreachable ();
15262
15263 gcc_assert (derived->attr.pdt_type);
15264
15265 for (param = sym->param_list; param; param = param->next)
15266 {
15267 c = gfc_find_component (derived, param->name, false, true, NULL);
15268 gcc_assert (c);
15269 if (c->attr.pdt_kind)
15270 continue;
15271
15272 if (param->expr && !gfc_is_constant_expr (param->expr)
15273 && c->attr.pdt_len)
15274 const_len_exprs = false;
15275 else if (param->spec_type == SPEC_ASSUMED)
15276 assumed_len_exprs = true;
15277
15278 if (param->spec_type == SPEC_DEFERRED
15279 && !attr->allocatable && !attr->pointer)
15280 gfc_error ("The object %qs at %L has a deferred LEN "
15281 "parameter %qs and is neither allocatable "
15282 "nor a pointer", sym->name, &sym->declared_at,
15283 param->name);
15284
15285 }
15286
15287 if (!const_len_exprs
15288 && (sym->ns->proc_name->attr.is_main_program
15289 || sym->ns->proc_name->attr.flavor == FL_MODULE
15290 || sym->attr.save != SAVE_NONE))
15291 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15292 "SAVE attribute or be a variable declared in the "
15293 "main program, a module or a submodule(F08/C513)",
15294 sym->name, &sym->declared_at);
15295
15296 if (assumed_len_exprs && !(sym->attr.dummy
15297 || sym->attr.select_type_temporary || sym->attr.associate_var))
15298 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15299 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15300 sym->name, &sym->declared_at);
15301 }
15302
15303
15304 /* Do anything necessary to resolve a symbol. Right now, we just
15305 assume that an otherwise unknown symbol is a variable. This sort
15306 of thing commonly happens for symbols in module. */
15307
15308 static void
15309 resolve_symbol (gfc_symbol *sym)
15310 {
15311 int check_constant, mp_flag;
15312 gfc_symtree *symtree;
15313 gfc_symtree *this_symtree;
15314 gfc_namespace *ns;
15315 gfc_component *c;
15316 symbol_attribute class_attr;
15317 gfc_array_spec *as;
15318 bool saved_specification_expr;
15319
15320 if (sym->resolve_symbol_called >= 1)
15321 return;
15322 sym->resolve_symbol_called = 1;
15323
15324 /* No symbol will ever have union type; only components can be unions.
15325 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15326 (just like derived type declaration symbols have flavor FL_DERIVED). */
15327 gcc_assert (sym->ts.type != BT_UNION);
15328
15329 /* Coarrayed polymorphic objects with allocatable or pointer components are
15330 yet unsupported for -fcoarray=lib. */
15331 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15332 && sym->ts.u.derived && CLASS_DATA (sym)
15333 && CLASS_DATA (sym)->attr.codimension
15334 && CLASS_DATA (sym)->ts.u.derived
15335 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15336 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15337 {
15338 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15339 "type coarrays at %L are unsupported", &sym->declared_at);
15340 return;
15341 }
15342
15343 if (sym->attr.artificial)
15344 return;
15345
15346 if (sym->attr.unlimited_polymorphic)
15347 return;
15348
15349 if (sym->attr.flavor == FL_UNKNOWN
15350 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15351 && !sym->attr.generic && !sym->attr.external
15352 && sym->attr.if_source == IFSRC_UNKNOWN
15353 && sym->ts.type == BT_UNKNOWN))
15354 {
15355
15356 /* If we find that a flavorless symbol is an interface in one of the
15357 parent namespaces, find its symtree in this namespace, free the
15358 symbol and set the symtree to point to the interface symbol. */
15359 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15360 {
15361 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15362 if (symtree && (symtree->n.sym->generic ||
15363 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15364 && sym->ns->construct_entities)))
15365 {
15366 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15367 sym->name);
15368 if (this_symtree->n.sym == sym)
15369 {
15370 symtree->n.sym->refs++;
15371 gfc_release_symbol (sym);
15372 this_symtree->n.sym = symtree->n.sym;
15373 return;
15374 }
15375 }
15376 }
15377
15378 /* Otherwise give it a flavor according to such attributes as
15379 it has. */
15380 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15381 && sym->attr.intrinsic == 0)
15382 sym->attr.flavor = FL_VARIABLE;
15383 else if (sym->attr.flavor == FL_UNKNOWN)
15384 {
15385 sym->attr.flavor = FL_PROCEDURE;
15386 if (sym->attr.dimension)
15387 sym->attr.function = 1;
15388 }
15389 }
15390
15391 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15392 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15393
15394 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15395 && !resolve_procedure_interface (sym))
15396 return;
15397
15398 if (sym->attr.is_protected && !sym->attr.proc_pointer
15399 && (sym->attr.procedure || sym->attr.external))
15400 {
15401 if (sym->attr.external)
15402 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15403 "at %L", &sym->declared_at);
15404 else
15405 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15406 "at %L", &sym->declared_at);
15407
15408 return;
15409 }
15410
15411 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15412 return;
15413
15414 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15415 && !resolve_fl_struct (sym))
15416 return;
15417
15418 /* Symbols that are module procedures with results (functions) have
15419 the types and array specification copied for type checking in
15420 procedures that call them, as well as for saving to a module
15421 file. These symbols can't stand the scrutiny that their results
15422 can. */
15423 mp_flag = (sym->result != NULL && sym->result != sym);
15424
15425 /* Make sure that the intrinsic is consistent with its internal
15426 representation. This needs to be done before assigning a default
15427 type to avoid spurious warnings. */
15428 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15429 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15430 return;
15431
15432 /* Resolve associate names. */
15433 if (sym->assoc)
15434 resolve_assoc_var (sym, true);
15435
15436 /* Assign default type to symbols that need one and don't have one. */
15437 if (sym->ts.type == BT_UNKNOWN)
15438 {
15439 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15440 {
15441 gfc_set_default_type (sym, 1, NULL);
15442 }
15443
15444 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15445 && !sym->attr.function && !sym->attr.subroutine
15446 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15447 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15448
15449 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15450 {
15451 /* The specific case of an external procedure should emit an error
15452 in the case that there is no implicit type. */
15453 if (!mp_flag)
15454 {
15455 if (!sym->attr.mixed_entry_master)
15456 gfc_set_default_type (sym, sym->attr.external, NULL);
15457 }
15458 else
15459 {
15460 /* Result may be in another namespace. */
15461 resolve_symbol (sym->result);
15462
15463 if (!sym->result->attr.proc_pointer)
15464 {
15465 sym->ts = sym->result->ts;
15466 sym->as = gfc_copy_array_spec (sym->result->as);
15467 sym->attr.dimension = sym->result->attr.dimension;
15468 sym->attr.pointer = sym->result->attr.pointer;
15469 sym->attr.allocatable = sym->result->attr.allocatable;
15470 sym->attr.contiguous = sym->result->attr.contiguous;
15471 }
15472 }
15473 }
15474 }
15475 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15476 {
15477 bool saved_specification_expr = specification_expr;
15478 bool saved_formal_arg_flag = formal_arg_flag;
15479
15480 specification_expr = true;
15481 formal_arg_flag = true;
15482 gfc_resolve_array_spec (sym->result->as, false);
15483 formal_arg_flag = saved_formal_arg_flag;
15484 specification_expr = saved_specification_expr;
15485 }
15486
15487 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
15488 {
15489 as = CLASS_DATA (sym)->as;
15490 class_attr = CLASS_DATA (sym)->attr;
15491 class_attr.pointer = class_attr.class_pointer;
15492 }
15493 else
15494 {
15495 class_attr = sym->attr;
15496 as = sym->as;
15497 }
15498
15499 /* F2008, C530. */
15500 if (sym->attr.contiguous
15501 && (!class_attr.dimension
15502 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15503 && !class_attr.pointer)))
15504 {
15505 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15506 "array pointer or an assumed-shape or assumed-rank array",
15507 sym->name, &sym->declared_at);
15508 return;
15509 }
15510
15511 /* Assumed size arrays and assumed shape arrays must be dummy
15512 arguments. Array-spec's of implied-shape should have been resolved to
15513 AS_EXPLICIT already. */
15514
15515 if (as)
15516 {
15517 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15518 specification expression. */
15519 if (as->type == AS_IMPLIED_SHAPE)
15520 {
15521 int i;
15522 for (i=0; i<as->rank; i++)
15523 {
15524 if (as->lower[i] != NULL && as->upper[i] == NULL)
15525 {
15526 gfc_error ("Bad specification for assumed size array at %L",
15527 &as->lower[i]->where);
15528 return;
15529 }
15530 }
15531 gcc_unreachable();
15532 }
15533
15534 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15535 || as->type == AS_ASSUMED_SHAPE)
15536 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15537 {
15538 if (as->type == AS_ASSUMED_SIZE)
15539 gfc_error ("Assumed size array at %L must be a dummy argument",
15540 &sym->declared_at);
15541 else
15542 gfc_error ("Assumed shape array at %L must be a dummy argument",
15543 &sym->declared_at);
15544 return;
15545 }
15546 /* TS 29113, C535a. */
15547 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15548 && !sym->attr.select_type_temporary
15549 && !(cs_base && cs_base->current
15550 && cs_base->current->op == EXEC_SELECT_RANK))
15551 {
15552 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15553 &sym->declared_at);
15554 return;
15555 }
15556 if (as->type == AS_ASSUMED_RANK
15557 && (sym->attr.codimension || sym->attr.value))
15558 {
15559 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15560 "CODIMENSION attribute", &sym->declared_at);
15561 return;
15562 }
15563 }
15564
15565 /* Make sure symbols with known intent or optional are really dummy
15566 variable. Because of ENTRY statement, this has to be deferred
15567 until resolution time. */
15568
15569 if (!sym->attr.dummy
15570 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15571 {
15572 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15573 return;
15574 }
15575
15576 if (sym->attr.value && !sym->attr.dummy)
15577 {
15578 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15579 "it is not a dummy argument", sym->name, &sym->declared_at);
15580 return;
15581 }
15582
15583 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15584 {
15585 gfc_charlen *cl = sym->ts.u.cl;
15586 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15587 {
15588 gfc_error ("Character dummy variable %qs at %L with VALUE "
15589 "attribute must have constant length",
15590 sym->name, &sym->declared_at);
15591 return;
15592 }
15593
15594 if (sym->ts.is_c_interop
15595 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15596 {
15597 gfc_error ("C interoperable character dummy variable %qs at %L "
15598 "with VALUE attribute must have length one",
15599 sym->name, &sym->declared_at);
15600 return;
15601 }
15602 }
15603
15604 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15605 && sym->ts.u.derived->attr.generic)
15606 {
15607 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15608 if (!sym->ts.u.derived)
15609 {
15610 gfc_error ("The derived type %qs at %L is of type %qs, "
15611 "which has not been defined", sym->name,
15612 &sym->declared_at, sym->ts.u.derived->name);
15613 sym->ts.type = BT_UNKNOWN;
15614 return;
15615 }
15616 }
15617
15618 /* Use the same constraints as TYPE(*), except for the type check
15619 and that only scalars and assumed-size arrays are permitted. */
15620 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15621 {
15622 if (!sym->attr.dummy)
15623 {
15624 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15625 "a dummy argument", sym->name, &sym->declared_at);
15626 return;
15627 }
15628
15629 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15630 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15631 && sym->ts.type != BT_COMPLEX)
15632 {
15633 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15634 "of type TYPE(*) or of an numeric intrinsic type",
15635 sym->name, &sym->declared_at);
15636 return;
15637 }
15638
15639 if (sym->attr.allocatable || sym->attr.codimension
15640 || sym->attr.pointer || sym->attr.value)
15641 {
15642 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15643 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15644 "attribute", sym->name, &sym->declared_at);
15645 return;
15646 }
15647
15648 if (sym->attr.intent == INTENT_OUT)
15649 {
15650 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15651 "have the INTENT(OUT) attribute",
15652 sym->name, &sym->declared_at);
15653 return;
15654 }
15655 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15656 {
15657 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15658 "either be a scalar or an assumed-size array",
15659 sym->name, &sym->declared_at);
15660 return;
15661 }
15662
15663 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15664 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15665 packing. */
15666 sym->ts.type = BT_ASSUMED;
15667 sym->as = gfc_get_array_spec ();
15668 sym->as->type = AS_ASSUMED_SIZE;
15669 sym->as->rank = 1;
15670 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15671 }
15672 else if (sym->ts.type == BT_ASSUMED)
15673 {
15674 /* TS 29113, C407a. */
15675 if (!sym->attr.dummy)
15676 {
15677 gfc_error ("Assumed type of variable %s at %L is only permitted "
15678 "for dummy variables", sym->name, &sym->declared_at);
15679 return;
15680 }
15681 if (sym->attr.allocatable || sym->attr.codimension
15682 || sym->attr.pointer || sym->attr.value)
15683 {
15684 gfc_error ("Assumed-type variable %s at %L may not have the "
15685 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15686 sym->name, &sym->declared_at);
15687 return;
15688 }
15689 if (sym->attr.intent == INTENT_OUT)
15690 {
15691 gfc_error ("Assumed-type variable %s at %L may not have the "
15692 "INTENT(OUT) attribute",
15693 sym->name, &sym->declared_at);
15694 return;
15695 }
15696 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15697 {
15698 gfc_error ("Assumed-type variable %s at %L shall not be an "
15699 "explicit-shape array", sym->name, &sym->declared_at);
15700 return;
15701 }
15702 }
15703
15704 /* If the symbol is marked as bind(c), that it is declared at module level
15705 scope and verify its type and kind. Do not do the latter for symbols
15706 that are implicitly typed because that is handled in
15707 gfc_set_default_type. Handle dummy arguments and procedure definitions
15708 separately. Also, anything that is use associated is not handled here
15709 but instead is handled in the module it is declared in. Finally, derived
15710 type definitions are allowed to be BIND(C) since that only implies that
15711 they're interoperable, and they are checked fully for interoperability
15712 when a variable is declared of that type. */
15713 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15714 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15715 && sym->attr.flavor != FL_DERIVED)
15716 {
15717 bool t = true;
15718
15719 /* First, make sure the variable is declared at the
15720 module-level scope (J3/04-007, Section 15.3). */
15721 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15722 sym->attr.in_common == 0)
15723 {
15724 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15725 "is neither a COMMON block nor declared at the "
15726 "module level scope", sym->name, &(sym->declared_at));
15727 t = false;
15728 }
15729 else if (sym->ts.type == BT_CHARACTER
15730 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15731 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15732 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15733 {
15734 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15735 sym->name, &sym->declared_at);
15736 t = false;
15737 }
15738 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15739 {
15740 t = verify_com_block_vars_c_interop (sym->common_head);
15741 }
15742 else if (sym->attr.implicit_type == 0)
15743 {
15744 /* If type() declaration, we need to verify that the components
15745 of the given type are all C interoperable, etc. */
15746 if (sym->ts.type == BT_DERIVED &&
15747 sym->ts.u.derived->attr.is_c_interop != 1)
15748 {
15749 /* Make sure the user marked the derived type as BIND(C). If
15750 not, call the verify routine. This could print an error
15751 for the derived type more than once if multiple variables
15752 of that type are declared. */
15753 if (sym->ts.u.derived->attr.is_bind_c != 1)
15754 verify_bind_c_derived_type (sym->ts.u.derived);
15755 t = false;
15756 }
15757
15758 /* Verify the variable itself as C interoperable if it
15759 is BIND(C). It is not possible for this to succeed if
15760 the verify_bind_c_derived_type failed, so don't have to handle
15761 any error returned by verify_bind_c_derived_type. */
15762 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15763 sym->common_block);
15764 }
15765
15766 if (!t)
15767 {
15768 /* clear the is_bind_c flag to prevent reporting errors more than
15769 once if something failed. */
15770 sym->attr.is_bind_c = 0;
15771 return;
15772 }
15773 }
15774
15775 /* If a derived type symbol has reached this point, without its
15776 type being declared, we have an error. Notice that most
15777 conditions that produce undefined derived types have already
15778 been dealt with. However, the likes of:
15779 implicit type(t) (t) ..... call foo (t) will get us here if
15780 the type is not declared in the scope of the implicit
15781 statement. Change the type to BT_UNKNOWN, both because it is so
15782 and to prevent an ICE. */
15783 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15784 && sym->ts.u.derived->components == NULL
15785 && !sym->ts.u.derived->attr.zero_comp)
15786 {
15787 gfc_error ("The derived type %qs at %L is of type %qs, "
15788 "which has not been defined", sym->name,
15789 &sym->declared_at, sym->ts.u.derived->name);
15790 sym->ts.type = BT_UNKNOWN;
15791 return;
15792 }
15793
15794 /* Make sure that the derived type has been resolved and that the
15795 derived type is visible in the symbol's namespace, if it is a
15796 module function and is not PRIVATE. */
15797 if (sym->ts.type == BT_DERIVED
15798 && sym->ts.u.derived->attr.use_assoc
15799 && sym->ns->proc_name
15800 && sym->ns->proc_name->attr.flavor == FL_MODULE
15801 && !resolve_fl_derived (sym->ts.u.derived))
15802 return;
15803
15804 /* Unless the derived-type declaration is use associated, Fortran 95
15805 does not allow public entries of private derived types.
15806 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15807 161 in 95-006r3. */
15808 if (sym->ts.type == BT_DERIVED
15809 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15810 && !sym->ts.u.derived->attr.use_assoc
15811 && gfc_check_symbol_access (sym)
15812 && !gfc_check_symbol_access (sym->ts.u.derived)
15813 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15814 "derived type %qs",
15815 (sym->attr.flavor == FL_PARAMETER)
15816 ? "parameter" : "variable",
15817 sym->name, &sym->declared_at,
15818 sym->ts.u.derived->name))
15819 return;
15820
15821 /* F2008, C1302. */
15822 if (sym->ts.type == BT_DERIVED
15823 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15824 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15825 || sym->ts.u.derived->attr.lock_comp)
15826 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15827 {
15828 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15829 "type LOCK_TYPE must be a coarray", sym->name,
15830 &sym->declared_at);
15831 return;
15832 }
15833
15834 /* TS18508, C702/C703. */
15835 if (sym->ts.type == BT_DERIVED
15836 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15837 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15838 || sym->ts.u.derived->attr.event_comp)
15839 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15840 {
15841 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15842 "type EVENT_TYPE must be a coarray", sym->name,
15843 &sym->declared_at);
15844 return;
15845 }
15846
15847 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15848 default initialization is defined (5.1.2.4.4). */
15849 if (sym->ts.type == BT_DERIVED
15850 && sym->attr.dummy
15851 && sym->attr.intent == INTENT_OUT
15852 && sym->as
15853 && sym->as->type == AS_ASSUMED_SIZE)
15854 {
15855 for (c = sym->ts.u.derived->components; c; c = c->next)
15856 {
15857 if (c->initializer)
15858 {
15859 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15860 "ASSUMED SIZE and so cannot have a default initializer",
15861 sym->name, &sym->declared_at);
15862 return;
15863 }
15864 }
15865 }
15866
15867 /* F2008, C542. */
15868 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15869 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15870 {
15871 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15872 "INTENT(OUT)", sym->name, &sym->declared_at);
15873 return;
15874 }
15875
15876 /* TS18508. */
15877 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15878 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15879 {
15880 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15881 "INTENT(OUT)", sym->name, &sym->declared_at);
15882 return;
15883 }
15884
15885 /* F2008, C525. */
15886 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15887 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15888 && sym->ts.u.derived && CLASS_DATA (sym)
15889 && CLASS_DATA (sym)->attr.coarray_comp))
15890 || class_attr.codimension)
15891 && (sym->attr.result || sym->result == sym))
15892 {
15893 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15894 "a coarray component", sym->name, &sym->declared_at);
15895 return;
15896 }
15897
15898 /* F2008, C524. */
15899 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15900 && sym->ts.u.derived->ts.is_iso_c)
15901 {
15902 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15903 "shall not be a coarray", sym->name, &sym->declared_at);
15904 return;
15905 }
15906
15907 /* F2008, C525. */
15908 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15909 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15910 && sym->ts.u.derived && CLASS_DATA (sym)
15911 && CLASS_DATA (sym)->attr.coarray_comp))
15912 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15913 || class_attr.allocatable))
15914 {
15915 gfc_error ("Variable %qs at %L with coarray component shall be a "
15916 "nonpointer, nonallocatable scalar, which is not a coarray",
15917 sym->name, &sym->declared_at);
15918 return;
15919 }
15920
15921 /* F2008, C526. The function-result case was handled above. */
15922 if (class_attr.codimension
15923 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15924 || sym->attr.select_type_temporary
15925 || sym->attr.associate_var
15926 || (sym->ns->save_all && !sym->attr.automatic)
15927 || sym->ns->proc_name->attr.flavor == FL_MODULE
15928 || sym->ns->proc_name->attr.is_main_program
15929 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15930 {
15931 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15932 "nor a dummy argument", sym->name, &sym->declared_at);
15933 return;
15934 }
15935 /* F2008, C528. */
15936 else if (class_attr.codimension && !sym->attr.select_type_temporary
15937 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15938 {
15939 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15940 "deferred shape", sym->name, &sym->declared_at);
15941 return;
15942 }
15943 else if (class_attr.codimension && class_attr.allocatable && as
15944 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15945 {
15946 gfc_error ("Allocatable coarray variable %qs at %L must have "
15947 "deferred shape", sym->name, &sym->declared_at);
15948 return;
15949 }
15950
15951 /* F2008, C541. */
15952 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15953 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15954 && sym->ts.u.derived && CLASS_DATA (sym)
15955 && CLASS_DATA (sym)->attr.coarray_comp))
15956 || (class_attr.codimension && class_attr.allocatable))
15957 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15958 {
15959 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15960 "allocatable coarray or have coarray components",
15961 sym->name, &sym->declared_at);
15962 return;
15963 }
15964
15965 if (class_attr.codimension && sym->attr.dummy
15966 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15967 {
15968 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15969 "procedure %qs", sym->name, &sym->declared_at,
15970 sym->ns->proc_name->name);
15971 return;
15972 }
15973
15974 if (sym->ts.type == BT_LOGICAL
15975 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15976 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15977 && sym->ns->proc_name->attr.is_bind_c)))
15978 {
15979 int i;
15980 for (i = 0; gfc_logical_kinds[i].kind; i++)
15981 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15982 break;
15983 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15984 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15985 "%L with non-C_Bool kind in BIND(C) procedure "
15986 "%qs", sym->name, &sym->declared_at,
15987 sym->ns->proc_name->name))
15988 return;
15989 else if (!gfc_logical_kinds[i].c_bool
15990 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15991 "%qs at %L with non-C_Bool kind in "
15992 "BIND(C) procedure %qs", sym->name,
15993 &sym->declared_at,
15994 sym->attr.function ? sym->name
15995 : sym->ns->proc_name->name))
15996 return;
15997 }
15998
15999 switch (sym->attr.flavor)
16000 {
16001 case FL_VARIABLE:
16002 if (!resolve_fl_variable (sym, mp_flag))
16003 return;
16004 break;
16005
16006 case FL_PROCEDURE:
16007 if (sym->formal && !sym->formal_ns)
16008 {
16009 /* Check that none of the arguments are a namelist. */
16010 gfc_formal_arglist *formal = sym->formal;
16011
16012 for (; formal; formal = formal->next)
16013 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
16014 {
16015 gfc_error ("Namelist %qs cannot be an argument to "
16016 "subroutine or function at %L",
16017 formal->sym->name, &sym->declared_at);
16018 return;
16019 }
16020 }
16021
16022 if (!resolve_fl_procedure (sym, mp_flag))
16023 return;
16024 break;
16025
16026 case FL_NAMELIST:
16027 if (!resolve_fl_namelist (sym))
16028 return;
16029 break;
16030
16031 case FL_PARAMETER:
16032 if (!resolve_fl_parameter (sym))
16033 return;
16034 break;
16035
16036 default:
16037 break;
16038 }
16039
16040 /* Resolve array specifier. Check as well some constraints
16041 on COMMON blocks. */
16042
16043 check_constant = sym->attr.in_common && !sym->attr.pointer;
16044
16045 /* Set the formal_arg_flag so that check_conflict will not throw
16046 an error for host associated variables in the specification
16047 expression for an array_valued function. */
16048 if ((sym->attr.function || sym->attr.result) && sym->as)
16049 formal_arg_flag = true;
16050
16051 saved_specification_expr = specification_expr;
16052 specification_expr = true;
16053 gfc_resolve_array_spec (sym->as, check_constant);
16054 specification_expr = saved_specification_expr;
16055
16056 formal_arg_flag = false;
16057
16058 /* Resolve formal namespaces. */
16059 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
16060 && !sym->attr.contained && !sym->attr.intrinsic)
16061 gfc_resolve (sym->formal_ns);
16062
16063 /* Make sure the formal namespace is present. */
16064 if (sym->formal && !sym->formal_ns)
16065 {
16066 gfc_formal_arglist *formal = sym->formal;
16067 while (formal && !formal->sym)
16068 formal = formal->next;
16069
16070 if (formal)
16071 {
16072 sym->formal_ns = formal->sym->ns;
16073 if (sym->formal_ns && sym->ns != formal->sym->ns)
16074 sym->formal_ns->refs++;
16075 }
16076 }
16077
16078 /* Check threadprivate restrictions. */
16079 if (sym->attr.threadprivate
16080 && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
16081 && !(sym->ns->save_all && !sym->attr.automatic)
16082 && sym->module == NULL
16083 && (sym->ns->proc_name == NULL
16084 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16085 && !sym->ns->proc_name->attr.is_main_program)))
16086 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
16087
16088 /* Check omp declare target restrictions. */
16089 if (sym->attr.omp_declare_target
16090 && sym->attr.flavor == FL_VARIABLE
16091 && !sym->attr.save
16092 && !(sym->ns->save_all && !sym->attr.automatic)
16093 && (!sym->attr.in_common
16094 && sym->module == NULL
16095 && (sym->ns->proc_name == NULL
16096 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16097 && !sym->ns->proc_name->attr.is_main_program))))
16098 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
16099 sym->name, &sym->declared_at);
16100
16101 /* If we have come this far we can apply default-initializers, as
16102 described in 14.7.5, to those variables that have not already
16103 been assigned one. */
16104 if (sym->ts.type == BT_DERIVED
16105 && !sym->value
16106 && !sym->attr.allocatable
16107 && !sym->attr.alloc_comp)
16108 {
16109 symbol_attribute *a = &sym->attr;
16110
16111 if ((!a->save && !a->dummy && !a->pointer
16112 && !a->in_common && !a->use_assoc
16113 && a->referenced
16114 && !((a->function || a->result)
16115 && (!a->dimension
16116 || sym->ts.u.derived->attr.alloc_comp
16117 || sym->ts.u.derived->attr.pointer_comp))
16118 && !(a->function && sym != sym->result))
16119 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
16120 apply_default_init (sym);
16121 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
16122 && (sym->ts.u.derived->attr.alloc_comp
16123 || sym->ts.u.derived->attr.pointer_comp))
16124 /* Mark the result symbol to be referenced, when it has allocatable
16125 components. */
16126 sym->result->attr.referenced = 1;
16127 }
16128
16129 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
16130 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
16131 && !CLASS_DATA (sym)->attr.class_pointer
16132 && !CLASS_DATA (sym)->attr.allocatable)
16133 apply_default_init (sym);
16134
16135 /* If this symbol has a type-spec, check it. */
16136 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
16137 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
16138 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
16139 return;
16140
16141 if (sym->param_list)
16142 resolve_pdt (sym);
16143 }
16144
16145
16146 /************* Resolve DATA statements *************/
16147
16148 static struct
16149 {
16150 gfc_data_value *vnode;
16151 mpz_t left;
16152 }
16153 values;
16154
16155
16156 /* Advance the values structure to point to the next value in the data list. */
16157
16158 static bool
16159 next_data_value (void)
16160 {
16161 while (mpz_cmp_ui (values.left, 0) == 0)
16162 {
16163
16164 if (values.vnode->next == NULL)
16165 return false;
16166
16167 values.vnode = values.vnode->next;
16168 mpz_set (values.left, values.vnode->repeat);
16169 }
16170
16171 return true;
16172 }
16173
16174
16175 static bool
16176 check_data_variable (gfc_data_variable *var, locus *where)
16177 {
16178 gfc_expr *e;
16179 mpz_t size;
16180 mpz_t offset;
16181 bool t;
16182 ar_type mark = AR_UNKNOWN;
16183 int i;
16184 mpz_t section_index[GFC_MAX_DIMENSIONS];
16185 gfc_ref *ref;
16186 gfc_array_ref *ar;
16187 gfc_symbol *sym;
16188 int has_pointer;
16189
16190 if (!gfc_resolve_expr (var->expr))
16191 return false;
16192
16193 ar = NULL;
16194 mpz_init_set_si (offset, 0);
16195 e = var->expr;
16196
16197 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16198 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16199 e = e->value.function.actual->expr;
16200
16201 if (e->expr_type != EXPR_VARIABLE)
16202 {
16203 gfc_error ("Expecting definable entity near %L", where);
16204 return false;
16205 }
16206
16207 sym = e->symtree->n.sym;
16208
16209 if (sym->ns->is_block_data && !sym->attr.in_common)
16210 {
16211 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16212 sym->name, &sym->declared_at);
16213 return false;
16214 }
16215
16216 if (e->ref == NULL && sym->as)
16217 {
16218 gfc_error ("DATA array %qs at %L must be specified in a previous"
16219 " declaration", sym->name, where);
16220 return false;
16221 }
16222
16223 if (gfc_is_coindexed (e))
16224 {
16225 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16226 where);
16227 return false;
16228 }
16229
16230 has_pointer = sym->attr.pointer;
16231
16232 for (ref = e->ref; ref; ref = ref->next)
16233 {
16234 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16235 has_pointer = 1;
16236
16237 if (has_pointer)
16238 {
16239 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16240 {
16241 gfc_error ("DATA element %qs at %L is a pointer and so must "
16242 "be a full array", sym->name, where);
16243 return false;
16244 }
16245
16246 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16247 {
16248 gfc_error ("DATA object near %L has the pointer attribute "
16249 "and the corresponding DATA value is not a valid "
16250 "initial-data-target", where);
16251 return false;
16252 }
16253 }
16254
16255 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
16256 {
16257 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
16258 "attribute", ref->u.c.component->name, &e->where);
16259 return false;
16260 }
16261 }
16262
16263 if (e->rank == 0 || has_pointer)
16264 {
16265 mpz_init_set_ui (size, 1);
16266 ref = NULL;
16267 }
16268 else
16269 {
16270 ref = e->ref;
16271
16272 /* Find the array section reference. */
16273 for (ref = e->ref; ref; ref = ref->next)
16274 {
16275 if (ref->type != REF_ARRAY)
16276 continue;
16277 if (ref->u.ar.type == AR_ELEMENT)
16278 continue;
16279 break;
16280 }
16281 gcc_assert (ref);
16282
16283 /* Set marks according to the reference pattern. */
16284 switch (ref->u.ar.type)
16285 {
16286 case AR_FULL:
16287 mark = AR_FULL;
16288 break;
16289
16290 case AR_SECTION:
16291 ar = &ref->u.ar;
16292 /* Get the start position of array section. */
16293 gfc_get_section_index (ar, section_index, &offset);
16294 mark = AR_SECTION;
16295 break;
16296
16297 default:
16298 gcc_unreachable ();
16299 }
16300
16301 if (!gfc_array_size (e, &size))
16302 {
16303 gfc_error ("Nonconstant array section at %L in DATA statement",
16304 where);
16305 mpz_clear (offset);
16306 return false;
16307 }
16308 }
16309
16310 t = true;
16311
16312 while (mpz_cmp_ui (size, 0) > 0)
16313 {
16314 if (!next_data_value ())
16315 {
16316 gfc_error ("DATA statement at %L has more variables than values",
16317 where);
16318 t = false;
16319 break;
16320 }
16321
16322 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16323 if (!t)
16324 break;
16325
16326 /* If we have more than one element left in the repeat count,
16327 and we have more than one element left in the target variable,
16328 then create a range assignment. */
16329 /* FIXME: Only done for full arrays for now, since array sections
16330 seem tricky. */
16331 if (mark == AR_FULL && ref && ref->next == NULL
16332 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16333 {
16334 mpz_t range;
16335
16336 if (mpz_cmp (size, values.left) >= 0)
16337 {
16338 mpz_init_set (range, values.left);
16339 mpz_sub (size, size, values.left);
16340 mpz_set_ui (values.left, 0);
16341 }
16342 else
16343 {
16344 mpz_init_set (range, size);
16345 mpz_sub (values.left, values.left, size);
16346 mpz_set_ui (size, 0);
16347 }
16348
16349 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16350 offset, &range);
16351
16352 mpz_add (offset, offset, range);
16353 mpz_clear (range);
16354
16355 if (!t)
16356 break;
16357 }
16358
16359 /* Assign initial value to symbol. */
16360 else
16361 {
16362 mpz_sub_ui (values.left, values.left, 1);
16363 mpz_sub_ui (size, size, 1);
16364
16365 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16366 offset, NULL);
16367 if (!t)
16368 break;
16369
16370 if (mark == AR_FULL)
16371 mpz_add_ui (offset, offset, 1);
16372
16373 /* Modify the array section indexes and recalculate the offset
16374 for next element. */
16375 else if (mark == AR_SECTION)
16376 gfc_advance_section (section_index, ar, &offset);
16377 }
16378 }
16379
16380 if (mark == AR_SECTION)
16381 {
16382 for (i = 0; i < ar->dimen; i++)
16383 mpz_clear (section_index[i]);
16384 }
16385
16386 mpz_clear (size);
16387 mpz_clear (offset);
16388
16389 return t;
16390 }
16391
16392
16393 static bool traverse_data_var (gfc_data_variable *, locus *);
16394
16395 /* Iterate over a list of elements in a DATA statement. */
16396
16397 static bool
16398 traverse_data_list (gfc_data_variable *var, locus *where)
16399 {
16400 mpz_t trip;
16401 iterator_stack frame;
16402 gfc_expr *e, *start, *end, *step;
16403 bool retval = true;
16404
16405 mpz_init (frame.value);
16406 mpz_init (trip);
16407
16408 start = gfc_copy_expr (var->iter.start);
16409 end = gfc_copy_expr (var->iter.end);
16410 step = gfc_copy_expr (var->iter.step);
16411
16412 if (!gfc_simplify_expr (start, 1)
16413 || start->expr_type != EXPR_CONSTANT)
16414 {
16415 gfc_error ("start of implied-do loop at %L could not be "
16416 "simplified to a constant value", &start->where);
16417 retval = false;
16418 goto cleanup;
16419 }
16420 if (!gfc_simplify_expr (end, 1)
16421 || end->expr_type != EXPR_CONSTANT)
16422 {
16423 gfc_error ("end of implied-do loop at %L could not be "
16424 "simplified to a constant value", &end->where);
16425 retval = false;
16426 goto cleanup;
16427 }
16428 if (!gfc_simplify_expr (step, 1)
16429 || step->expr_type != EXPR_CONSTANT)
16430 {
16431 gfc_error ("step of implied-do loop at %L could not be "
16432 "simplified to a constant value", &step->where);
16433 retval = false;
16434 goto cleanup;
16435 }
16436 if (mpz_cmp_si (step->value.integer, 0) == 0)
16437 {
16438 gfc_error ("step of implied-do loop at %L shall not be zero",
16439 &step->where);
16440 retval = false;
16441 goto cleanup;
16442 }
16443
16444 mpz_set (trip, end->value.integer);
16445 mpz_sub (trip, trip, start->value.integer);
16446 mpz_add (trip, trip, step->value.integer);
16447
16448 mpz_div (trip, trip, step->value.integer);
16449
16450 mpz_set (frame.value, start->value.integer);
16451
16452 frame.prev = iter_stack;
16453 frame.variable = var->iter.var->symtree;
16454 iter_stack = &frame;
16455
16456 while (mpz_cmp_ui (trip, 0) > 0)
16457 {
16458 if (!traverse_data_var (var->list, where))
16459 {
16460 retval = false;
16461 goto cleanup;
16462 }
16463
16464 e = gfc_copy_expr (var->expr);
16465 if (!gfc_simplify_expr (e, 1))
16466 {
16467 gfc_free_expr (e);
16468 retval = false;
16469 goto cleanup;
16470 }
16471
16472 mpz_add (frame.value, frame.value, step->value.integer);
16473
16474 mpz_sub_ui (trip, trip, 1);
16475 }
16476
16477 cleanup:
16478 mpz_clear (frame.value);
16479 mpz_clear (trip);
16480
16481 gfc_free_expr (start);
16482 gfc_free_expr (end);
16483 gfc_free_expr (step);
16484
16485 iter_stack = frame.prev;
16486 return retval;
16487 }
16488
16489
16490 /* Type resolve variables in the variable list of a DATA statement. */
16491
16492 static bool
16493 traverse_data_var (gfc_data_variable *var, locus *where)
16494 {
16495 bool t;
16496
16497 for (; var; var = var->next)
16498 {
16499 if (var->expr == NULL)
16500 t = traverse_data_list (var, where);
16501 else
16502 t = check_data_variable (var, where);
16503
16504 if (!t)
16505 return false;
16506 }
16507
16508 return true;
16509 }
16510
16511
16512 /* Resolve the expressions and iterators associated with a data statement.
16513 This is separate from the assignment checking because data lists should
16514 only be resolved once. */
16515
16516 static bool
16517 resolve_data_variables (gfc_data_variable *d)
16518 {
16519 for (; d; d = d->next)
16520 {
16521 if (d->list == NULL)
16522 {
16523 if (!gfc_resolve_expr (d->expr))
16524 return false;
16525 }
16526 else
16527 {
16528 if (!gfc_resolve_iterator (&d->iter, false, true))
16529 return false;
16530
16531 if (!resolve_data_variables (d->list))
16532 return false;
16533 }
16534 }
16535
16536 return true;
16537 }
16538
16539
16540 /* Resolve a single DATA statement. We implement this by storing a pointer to
16541 the value list into static variables, and then recursively traversing the
16542 variables list, expanding iterators and such. */
16543
16544 static void
16545 resolve_data (gfc_data *d)
16546 {
16547
16548 if (!resolve_data_variables (d->var))
16549 return;
16550
16551 values.vnode = d->value;
16552 if (d->value == NULL)
16553 mpz_set_ui (values.left, 0);
16554 else
16555 mpz_set (values.left, d->value->repeat);
16556
16557 if (!traverse_data_var (d->var, &d->where))
16558 return;
16559
16560 /* At this point, we better not have any values left. */
16561
16562 if (next_data_value ())
16563 gfc_error ("DATA statement at %L has more values than variables",
16564 &d->where);
16565 }
16566
16567
16568 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16569 accessed by host or use association, is a dummy argument to a pure function,
16570 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16571 is storage associated with any such variable, shall not be used in the
16572 following contexts: (clients of this function). */
16573
16574 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16575 procedure. Returns zero if assignment is OK, nonzero if there is a
16576 problem. */
16577 int
16578 gfc_impure_variable (gfc_symbol *sym)
16579 {
16580 gfc_symbol *proc;
16581 gfc_namespace *ns;
16582
16583 if (sym->attr.use_assoc || sym->attr.in_common)
16584 return 1;
16585
16586 /* Check if the symbol's ns is inside the pure procedure. */
16587 for (ns = gfc_current_ns; ns; ns = ns->parent)
16588 {
16589 if (ns == sym->ns)
16590 break;
16591 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16592 return 1;
16593 }
16594
16595 proc = sym->ns->proc_name;
16596 if (sym->attr.dummy
16597 && !sym->attr.value
16598 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16599 || proc->attr.function))
16600 return 1;
16601
16602 /* TODO: Sort out what can be storage associated, if anything, and include
16603 it here. In principle equivalences should be scanned but it does not
16604 seem to be possible to storage associate an impure variable this way. */
16605 return 0;
16606 }
16607
16608
16609 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16610 current namespace is inside a pure procedure. */
16611
16612 int
16613 gfc_pure (gfc_symbol *sym)
16614 {
16615 symbol_attribute attr;
16616 gfc_namespace *ns;
16617
16618 if (sym == NULL)
16619 {
16620 /* Check if the current namespace or one of its parents
16621 belongs to a pure procedure. */
16622 for (ns = gfc_current_ns; ns; ns = ns->parent)
16623 {
16624 sym = ns->proc_name;
16625 if (sym == NULL)
16626 return 0;
16627 attr = sym->attr;
16628 if (attr.flavor == FL_PROCEDURE && attr.pure)
16629 return 1;
16630 }
16631 return 0;
16632 }
16633
16634 attr = sym->attr;
16635
16636 return attr.flavor == FL_PROCEDURE && attr.pure;
16637 }
16638
16639
16640 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16641 checks if the current namespace is implicitly pure. Note that this
16642 function returns false for a PURE procedure. */
16643
16644 int
16645 gfc_implicit_pure (gfc_symbol *sym)
16646 {
16647 gfc_namespace *ns;
16648
16649 if (sym == NULL)
16650 {
16651 /* Check if the current procedure is implicit_pure. Walk up
16652 the procedure list until we find a procedure. */
16653 for (ns = gfc_current_ns; ns; ns = ns->parent)
16654 {
16655 sym = ns->proc_name;
16656 if (sym == NULL)
16657 return 0;
16658
16659 if (sym->attr.flavor == FL_PROCEDURE)
16660 break;
16661 }
16662 }
16663
16664 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16665 && !sym->attr.pure;
16666 }
16667
16668
16669 void
16670 gfc_unset_implicit_pure (gfc_symbol *sym)
16671 {
16672 gfc_namespace *ns;
16673
16674 if (sym == NULL)
16675 {
16676 /* Check if the current procedure is implicit_pure. Walk up
16677 the procedure list until we find a procedure. */
16678 for (ns = gfc_current_ns; ns; ns = ns->parent)
16679 {
16680 sym = ns->proc_name;
16681 if (sym == NULL)
16682 return;
16683
16684 if (sym->attr.flavor == FL_PROCEDURE)
16685 break;
16686 }
16687 }
16688
16689 if (sym->attr.flavor == FL_PROCEDURE)
16690 sym->attr.implicit_pure = 0;
16691 else
16692 sym->attr.pure = 0;
16693 }
16694
16695
16696 /* Test whether the current procedure is elemental or not. */
16697
16698 int
16699 gfc_elemental (gfc_symbol *sym)
16700 {
16701 symbol_attribute attr;
16702
16703 if (sym == NULL)
16704 sym = gfc_current_ns->proc_name;
16705 if (sym == NULL)
16706 return 0;
16707 attr = sym->attr;
16708
16709 return attr.flavor == FL_PROCEDURE && attr.elemental;
16710 }
16711
16712
16713 /* Warn about unused labels. */
16714
16715 static void
16716 warn_unused_fortran_label (gfc_st_label *label)
16717 {
16718 if (label == NULL)
16719 return;
16720
16721 warn_unused_fortran_label (label->left);
16722
16723 if (label->defined == ST_LABEL_UNKNOWN)
16724 return;
16725
16726 switch (label->referenced)
16727 {
16728 case ST_LABEL_UNKNOWN:
16729 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16730 label->value, &label->where);
16731 break;
16732
16733 case ST_LABEL_BAD_TARGET:
16734 gfc_warning (OPT_Wunused_label,
16735 "Label %d at %L defined but cannot be used",
16736 label->value, &label->where);
16737 break;
16738
16739 default:
16740 break;
16741 }
16742
16743 warn_unused_fortran_label (label->right);
16744 }
16745
16746
16747 /* Returns the sequence type of a symbol or sequence. */
16748
16749 static seq_type
16750 sequence_type (gfc_typespec ts)
16751 {
16752 seq_type result;
16753 gfc_component *c;
16754
16755 switch (ts.type)
16756 {
16757 case BT_DERIVED:
16758
16759 if (ts.u.derived->components == NULL)
16760 return SEQ_NONDEFAULT;
16761
16762 result = sequence_type (ts.u.derived->components->ts);
16763 for (c = ts.u.derived->components->next; c; c = c->next)
16764 if (sequence_type (c->ts) != result)
16765 return SEQ_MIXED;
16766
16767 return result;
16768
16769 case BT_CHARACTER:
16770 if (ts.kind != gfc_default_character_kind)
16771 return SEQ_NONDEFAULT;
16772
16773 return SEQ_CHARACTER;
16774
16775 case BT_INTEGER:
16776 if (ts.kind != gfc_default_integer_kind)
16777 return SEQ_NONDEFAULT;
16778
16779 return SEQ_NUMERIC;
16780
16781 case BT_REAL:
16782 if (!(ts.kind == gfc_default_real_kind
16783 || ts.kind == gfc_default_double_kind))
16784 return SEQ_NONDEFAULT;
16785
16786 return SEQ_NUMERIC;
16787
16788 case BT_COMPLEX:
16789 if (ts.kind != gfc_default_complex_kind)
16790 return SEQ_NONDEFAULT;
16791
16792 return SEQ_NUMERIC;
16793
16794 case BT_LOGICAL:
16795 if (ts.kind != gfc_default_logical_kind)
16796 return SEQ_NONDEFAULT;
16797
16798 return SEQ_NUMERIC;
16799
16800 default:
16801 return SEQ_NONDEFAULT;
16802 }
16803 }
16804
16805
16806 /* Resolve derived type EQUIVALENCE object. */
16807
16808 static bool
16809 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16810 {
16811 gfc_component *c = derived->components;
16812
16813 if (!derived)
16814 return true;
16815
16816 /* Shall not be an object of nonsequence derived type. */
16817 if (!derived->attr.sequence)
16818 {
16819 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16820 "attribute to be an EQUIVALENCE object", sym->name,
16821 &e->where);
16822 return false;
16823 }
16824
16825 /* Shall not have allocatable components. */
16826 if (derived->attr.alloc_comp)
16827 {
16828 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16829 "components to be an EQUIVALENCE object",sym->name,
16830 &e->where);
16831 return false;
16832 }
16833
16834 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16835 {
16836 gfc_error ("Derived type variable %qs at %L with default "
16837 "initialization cannot be in EQUIVALENCE with a variable "
16838 "in COMMON", sym->name, &e->where);
16839 return false;
16840 }
16841
16842 for (; c ; c = c->next)
16843 {
16844 if (gfc_bt_struct (c->ts.type)
16845 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16846 return false;
16847
16848 /* Shall not be an object of sequence derived type containing a pointer
16849 in the structure. */
16850 if (c->attr.pointer)
16851 {
16852 gfc_error ("Derived type variable %qs at %L with pointer "
16853 "component(s) cannot be an EQUIVALENCE object",
16854 sym->name, &e->where);
16855 return false;
16856 }
16857 }
16858 return true;
16859 }
16860
16861
16862 /* Resolve equivalence object.
16863 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16864 an allocatable array, an object of nonsequence derived type, an object of
16865 sequence derived type containing a pointer at any level of component
16866 selection, an automatic object, a function name, an entry name, a result
16867 name, a named constant, a structure component, or a subobject of any of
16868 the preceding objects. A substring shall not have length zero. A
16869 derived type shall not have components with default initialization nor
16870 shall two objects of an equivalence group be initialized.
16871 Either all or none of the objects shall have an protected attribute.
16872 The simple constraints are done in symbol.c(check_conflict) and the rest
16873 are implemented here. */
16874
16875 static void
16876 resolve_equivalence (gfc_equiv *eq)
16877 {
16878 gfc_symbol *sym;
16879 gfc_symbol *first_sym;
16880 gfc_expr *e;
16881 gfc_ref *r;
16882 locus *last_where = NULL;
16883 seq_type eq_type, last_eq_type;
16884 gfc_typespec *last_ts;
16885 int object, cnt_protected;
16886 const char *msg;
16887
16888 last_ts = &eq->expr->symtree->n.sym->ts;
16889
16890 first_sym = eq->expr->symtree->n.sym;
16891
16892 cnt_protected = 0;
16893
16894 for (object = 1; eq; eq = eq->eq, object++)
16895 {
16896 e = eq->expr;
16897
16898 e->ts = e->symtree->n.sym->ts;
16899 /* match_varspec might not know yet if it is seeing
16900 array reference or substring reference, as it doesn't
16901 know the types. */
16902 if (e->ref && e->ref->type == REF_ARRAY)
16903 {
16904 gfc_ref *ref = e->ref;
16905 sym = e->symtree->n.sym;
16906
16907 if (sym->attr.dimension)
16908 {
16909 ref->u.ar.as = sym->as;
16910 ref = ref->next;
16911 }
16912
16913 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16914 if (e->ts.type == BT_CHARACTER
16915 && ref
16916 && ref->type == REF_ARRAY
16917 && ref->u.ar.dimen == 1
16918 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16919 && ref->u.ar.stride[0] == NULL)
16920 {
16921 gfc_expr *start = ref->u.ar.start[0];
16922 gfc_expr *end = ref->u.ar.end[0];
16923 void *mem = NULL;
16924
16925 /* Optimize away the (:) reference. */
16926 if (start == NULL && end == NULL)
16927 {
16928 if (e->ref == ref)
16929 e->ref = ref->next;
16930 else
16931 e->ref->next = ref->next;
16932 mem = ref;
16933 }
16934 else
16935 {
16936 ref->type = REF_SUBSTRING;
16937 if (start == NULL)
16938 start = gfc_get_int_expr (gfc_charlen_int_kind,
16939 NULL, 1);
16940 ref->u.ss.start = start;
16941 if (end == NULL && e->ts.u.cl)
16942 end = gfc_copy_expr (e->ts.u.cl->length);
16943 ref->u.ss.end = end;
16944 ref->u.ss.length = e->ts.u.cl;
16945 e->ts.u.cl = NULL;
16946 }
16947 ref = ref->next;
16948 free (mem);
16949 }
16950
16951 /* Any further ref is an error. */
16952 if (ref)
16953 {
16954 gcc_assert (ref->type == REF_ARRAY);
16955 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16956 &ref->u.ar.where);
16957 continue;
16958 }
16959 }
16960
16961 if (!gfc_resolve_expr (e))
16962 continue;
16963
16964 sym = e->symtree->n.sym;
16965
16966 if (sym->attr.is_protected)
16967 cnt_protected++;
16968 if (cnt_protected > 0 && cnt_protected != object)
16969 {
16970 gfc_error ("Either all or none of the objects in the "
16971 "EQUIVALENCE set at %L shall have the "
16972 "PROTECTED attribute",
16973 &e->where);
16974 break;
16975 }
16976
16977 /* Shall not equivalence common block variables in a PURE procedure. */
16978 if (sym->ns->proc_name
16979 && sym->ns->proc_name->attr.pure
16980 && sym->attr.in_common)
16981 {
16982 /* Need to check for symbols that may have entered the pure
16983 procedure via a USE statement. */
16984 bool saw_sym = false;
16985 if (sym->ns->use_stmts)
16986 {
16987 gfc_use_rename *r;
16988 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16989 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16990 }
16991 else
16992 saw_sym = true;
16993
16994 if (saw_sym)
16995 gfc_error ("COMMON block member %qs at %L cannot be an "
16996 "EQUIVALENCE object in the pure procedure %qs",
16997 sym->name, &e->where, sym->ns->proc_name->name);
16998 break;
16999 }
17000
17001 /* Shall not be a named constant. */
17002 if (e->expr_type == EXPR_CONSTANT)
17003 {
17004 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
17005 "object", sym->name, &e->where);
17006 continue;
17007 }
17008
17009 if (e->ts.type == BT_DERIVED
17010 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
17011 continue;
17012
17013 /* Check that the types correspond correctly:
17014 Note 5.28:
17015 A numeric sequence structure may be equivalenced to another sequence
17016 structure, an object of default integer type, default real type, double
17017 precision real type, default logical type such that components of the
17018 structure ultimately only become associated to objects of the same
17019 kind. A character sequence structure may be equivalenced to an object
17020 of default character kind or another character sequence structure.
17021 Other objects may be equivalenced only to objects of the same type and
17022 kind parameters. */
17023
17024 /* Identical types are unconditionally OK. */
17025 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
17026 goto identical_types;
17027
17028 last_eq_type = sequence_type (*last_ts);
17029 eq_type = sequence_type (sym->ts);
17030
17031 /* Since the pair of objects is not of the same type, mixed or
17032 non-default sequences can be rejected. */
17033
17034 msg = "Sequence %s with mixed components in EQUIVALENCE "
17035 "statement at %L with different type objects";
17036 if ((object ==2
17037 && last_eq_type == SEQ_MIXED
17038 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17039 || (eq_type == SEQ_MIXED
17040 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17041 continue;
17042
17043 msg = "Non-default type object or sequence %s in EQUIVALENCE "
17044 "statement at %L with objects of different type";
17045 if ((object ==2
17046 && last_eq_type == SEQ_NONDEFAULT
17047 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17048 || (eq_type == SEQ_NONDEFAULT
17049 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17050 continue;
17051
17052 msg ="Non-CHARACTER object %qs in default CHARACTER "
17053 "EQUIVALENCE statement at %L";
17054 if (last_eq_type == SEQ_CHARACTER
17055 && eq_type != SEQ_CHARACTER
17056 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17057 continue;
17058
17059 msg ="Non-NUMERIC object %qs in default NUMERIC "
17060 "EQUIVALENCE statement at %L";
17061 if (last_eq_type == SEQ_NUMERIC
17062 && eq_type != SEQ_NUMERIC
17063 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17064 continue;
17065
17066 identical_types:
17067
17068 last_ts =&sym->ts;
17069 last_where = &e->where;
17070
17071 if (!e->ref)
17072 continue;
17073
17074 /* Shall not be an automatic array. */
17075 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
17076 {
17077 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
17078 "an EQUIVALENCE object", sym->name, &e->where);
17079 continue;
17080 }
17081
17082 r = e->ref;
17083 while (r)
17084 {
17085 /* Shall not be a structure component. */
17086 if (r->type == REF_COMPONENT)
17087 {
17088 gfc_error ("Structure component %qs at %L cannot be an "
17089 "EQUIVALENCE object",
17090 r->u.c.component->name, &e->where);
17091 break;
17092 }
17093
17094 /* A substring shall not have length zero. */
17095 if (r->type == REF_SUBSTRING)
17096 {
17097 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
17098 {
17099 gfc_error ("Substring at %L has length zero",
17100 &r->u.ss.start->where);
17101 break;
17102 }
17103 }
17104 r = r->next;
17105 }
17106 }
17107 }
17108
17109
17110 /* Function called by resolve_fntype to flag other symbols used in the
17111 length type parameter specification of function results. */
17112
17113 static bool
17114 flag_fn_result_spec (gfc_expr *expr,
17115 gfc_symbol *sym,
17116 int *f ATTRIBUTE_UNUSED)
17117 {
17118 gfc_namespace *ns;
17119 gfc_symbol *s;
17120
17121 if (expr->expr_type == EXPR_VARIABLE)
17122 {
17123 s = expr->symtree->n.sym;
17124 for (ns = s->ns; ns; ns = ns->parent)
17125 if (!ns->parent)
17126 break;
17127
17128 if (sym == s)
17129 {
17130 gfc_error ("Self reference in character length expression "
17131 "for %qs at %L", sym->name, &expr->where);
17132 return true;
17133 }
17134
17135 if (!s->fn_result_spec
17136 && s->attr.flavor == FL_PARAMETER)
17137 {
17138 /* Function contained in a module.... */
17139 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
17140 {
17141 gfc_symtree *st;
17142 s->fn_result_spec = 1;
17143 /* Make sure that this symbol is translated as a module
17144 variable. */
17145 st = gfc_get_unique_symtree (ns);
17146 st->n.sym = s;
17147 s->refs++;
17148 }
17149 /* ... which is use associated and called. */
17150 else if (s->attr.use_assoc || s->attr.used_in_submodule
17151 ||
17152 /* External function matched with an interface. */
17153 (s->ns->proc_name
17154 && ((s->ns == ns
17155 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
17156 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
17157 && s->ns->proc_name->attr.function))
17158 s->fn_result_spec = 1;
17159 }
17160 }
17161 return false;
17162 }
17163
17164
17165 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17166
17167 static void
17168 resolve_fntype (gfc_namespace *ns)
17169 {
17170 gfc_entry_list *el;
17171 gfc_symbol *sym;
17172
17173 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17174 return;
17175
17176 /* If there are any entries, ns->proc_name is the entry master
17177 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17178 if (ns->entries)
17179 sym = ns->entries->sym;
17180 else
17181 sym = ns->proc_name;
17182 if (sym->result == sym
17183 && sym->ts.type == BT_UNKNOWN
17184 && !gfc_set_default_type (sym, 0, NULL)
17185 && !sym->attr.untyped)
17186 {
17187 gfc_error ("Function %qs at %L has no IMPLICIT type",
17188 sym->name, &sym->declared_at);
17189 sym->attr.untyped = 1;
17190 }
17191
17192 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17193 && !sym->attr.contained
17194 && !gfc_check_symbol_access (sym->ts.u.derived)
17195 && gfc_check_symbol_access (sym))
17196 {
17197 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17198 "%L of PRIVATE type %qs", sym->name,
17199 &sym->declared_at, sym->ts.u.derived->name);
17200 }
17201
17202 if (ns->entries)
17203 for (el = ns->entries->next; el; el = el->next)
17204 {
17205 if (el->sym->result == el->sym
17206 && el->sym->ts.type == BT_UNKNOWN
17207 && !gfc_set_default_type (el->sym, 0, NULL)
17208 && !el->sym->attr.untyped)
17209 {
17210 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17211 el->sym->name, &el->sym->declared_at);
17212 el->sym->attr.untyped = 1;
17213 }
17214 }
17215
17216 if (sym->ts.type == BT_CHARACTER)
17217 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17218 }
17219
17220
17221 /* 12.3.2.1.1 Defined operators. */
17222
17223 static bool
17224 check_uop_procedure (gfc_symbol *sym, locus where)
17225 {
17226 gfc_formal_arglist *formal;
17227
17228 if (!sym->attr.function)
17229 {
17230 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17231 sym->name, &where);
17232 return false;
17233 }
17234
17235 if (sym->ts.type == BT_CHARACTER
17236 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17237 && !(sym->result && ((sym->result->ts.u.cl
17238 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17239 {
17240 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17241 "character length", sym->name, &where);
17242 return false;
17243 }
17244
17245 formal = gfc_sym_get_dummy_args (sym);
17246 if (!formal || !formal->sym)
17247 {
17248 gfc_error ("User operator procedure %qs at %L must have at least "
17249 "one argument", sym->name, &where);
17250 return false;
17251 }
17252
17253 if (formal->sym->attr.intent != INTENT_IN)
17254 {
17255 gfc_error ("First argument of operator interface at %L must be "
17256 "INTENT(IN)", &where);
17257 return false;
17258 }
17259
17260 if (formal->sym->attr.optional)
17261 {
17262 gfc_error ("First argument of operator interface at %L cannot be "
17263 "optional", &where);
17264 return false;
17265 }
17266
17267 formal = formal->next;
17268 if (!formal || !formal->sym)
17269 return true;
17270
17271 if (formal->sym->attr.intent != INTENT_IN)
17272 {
17273 gfc_error ("Second argument of operator interface at %L must be "
17274 "INTENT(IN)", &where);
17275 return false;
17276 }
17277
17278 if (formal->sym->attr.optional)
17279 {
17280 gfc_error ("Second argument of operator interface at %L cannot be "
17281 "optional", &where);
17282 return false;
17283 }
17284
17285 if (formal->next)
17286 {
17287 gfc_error ("Operator interface at %L must have, at most, two "
17288 "arguments", &where);
17289 return false;
17290 }
17291
17292 return true;
17293 }
17294
17295 static void
17296 gfc_resolve_uops (gfc_symtree *symtree)
17297 {
17298 gfc_interface *itr;
17299
17300 if (symtree == NULL)
17301 return;
17302
17303 gfc_resolve_uops (symtree->left);
17304 gfc_resolve_uops (symtree->right);
17305
17306 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17307 check_uop_procedure (itr->sym, itr->sym->declared_at);
17308 }
17309
17310
17311 /* Examine all of the expressions associated with a program unit,
17312 assign types to all intermediate expressions, make sure that all
17313 assignments are to compatible types and figure out which names
17314 refer to which functions or subroutines. It doesn't check code
17315 block, which is handled by gfc_resolve_code. */
17316
17317 static void
17318 resolve_types (gfc_namespace *ns)
17319 {
17320 gfc_namespace *n;
17321 gfc_charlen *cl;
17322 gfc_data *d;
17323 gfc_equiv *eq;
17324 gfc_namespace* old_ns = gfc_current_ns;
17325 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17326
17327 if (ns->types_resolved)
17328 return;
17329
17330 /* Check that all IMPLICIT types are ok. */
17331 if (!ns->seen_implicit_none)
17332 {
17333 unsigned letter;
17334 for (letter = 0; letter != GFC_LETTERS; ++letter)
17335 if (ns->set_flag[letter]
17336 && !resolve_typespec_used (&ns->default_type[letter],
17337 &ns->implicit_loc[letter], NULL))
17338 return;
17339 }
17340
17341 gfc_current_ns = ns;
17342
17343 resolve_entries (ns);
17344
17345 resolve_common_vars (&ns->blank_common, false);
17346 resolve_common_blocks (ns->common_root);
17347
17348 resolve_contained_functions (ns);
17349
17350 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17351 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17352 gfc_resolve_formal_arglist (ns->proc_name);
17353
17354 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17355
17356 for (cl = ns->cl_list; cl; cl = cl->next)
17357 resolve_charlen (cl);
17358
17359 gfc_traverse_ns (ns, resolve_symbol);
17360
17361 resolve_fntype (ns);
17362
17363 for (n = ns->contained; n; n = n->sibling)
17364 {
17365 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17366 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17367 "also be PURE", n->proc_name->name,
17368 &n->proc_name->declared_at);
17369
17370 resolve_types (n);
17371 }
17372
17373 forall_flag = 0;
17374 gfc_do_concurrent_flag = 0;
17375 gfc_check_interfaces (ns);
17376
17377 gfc_traverse_ns (ns, resolve_values);
17378
17379 if (ns->save_all || (!flag_automatic && !recursive))
17380 gfc_save_all (ns);
17381
17382 iter_stack = NULL;
17383 for (d = ns->data; d; d = d->next)
17384 resolve_data (d);
17385
17386 iter_stack = NULL;
17387 gfc_traverse_ns (ns, gfc_formalize_init_value);
17388
17389 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17390
17391 for (eq = ns->equiv; eq; eq = eq->next)
17392 resolve_equivalence (eq);
17393
17394 /* Warn about unused labels. */
17395 if (warn_unused_label)
17396 warn_unused_fortran_label (ns->st_labels);
17397
17398 gfc_resolve_uops (ns->uop_root);
17399
17400 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17401
17402 gfc_resolve_omp_declare_simd (ns);
17403
17404 gfc_resolve_omp_udrs (ns->omp_udr_root);
17405
17406 ns->types_resolved = 1;
17407
17408 gfc_current_ns = old_ns;
17409 }
17410
17411
17412 /* Call gfc_resolve_code recursively. */
17413
17414 static void
17415 resolve_codes (gfc_namespace *ns)
17416 {
17417 gfc_namespace *n;
17418 bitmap_obstack old_obstack;
17419
17420 if (ns->resolved == 1)
17421 return;
17422
17423 for (n = ns->contained; n; n = n->sibling)
17424 resolve_codes (n);
17425
17426 gfc_current_ns = ns;
17427
17428 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17429 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17430 cs_base = NULL;
17431
17432 /* Set to an out of range value. */
17433 current_entry_id = -1;
17434
17435 old_obstack = labels_obstack;
17436 bitmap_obstack_initialize (&labels_obstack);
17437
17438 gfc_resolve_oacc_declare (ns);
17439 gfc_resolve_oacc_routines (ns);
17440 gfc_resolve_omp_local_vars (ns);
17441 gfc_resolve_code (ns->code, ns);
17442
17443 bitmap_obstack_release (&labels_obstack);
17444 labels_obstack = old_obstack;
17445 }
17446
17447
17448 /* This function is called after a complete program unit has been compiled.
17449 Its purpose is to examine all of the expressions associated with a program
17450 unit, assign types to all intermediate expressions, make sure that all
17451 assignments are to compatible types and figure out which names refer to
17452 which functions or subroutines. */
17453
17454 void
17455 gfc_resolve (gfc_namespace *ns)
17456 {
17457 gfc_namespace *old_ns;
17458 code_stack *old_cs_base;
17459 struct gfc_omp_saved_state old_omp_state;
17460
17461 if (ns->resolved)
17462 return;
17463
17464 ns->resolved = -1;
17465 old_ns = gfc_current_ns;
17466 old_cs_base = cs_base;
17467
17468 /* As gfc_resolve can be called during resolution of an OpenMP construct
17469 body, we should clear any state associated to it, so that say NS's
17470 DO loops are not interpreted as OpenMP loops. */
17471 if (!ns->construct_entities)
17472 gfc_omp_save_and_clear_state (&old_omp_state);
17473
17474 resolve_types (ns);
17475 component_assignment_level = 0;
17476 resolve_codes (ns);
17477
17478 gfc_current_ns = old_ns;
17479 cs_base = old_cs_base;
17480 ns->resolved = 1;
17481
17482 gfc_run_passes (ns);
17483
17484 if (!ns->construct_entities)
17485 gfc_omp_restore_state (&old_omp_state);
17486 }
This page took 0.855504 seconds and 5 git commands to generate.